Use a dedicated cache for instances of kobject_t
[helenos.git] / kernel / generic / src / ipc / ipc.c
blobb3ef0ffe6ccdde76262de86f17e60e6c47b5ca54
1 /*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup kernel_generic_ipc
30 * @{
32 /** @file
36 * Lock ordering
38 * First the answerbox, then the phone.
41 #include <assert.h>
42 #include <synch/spinlock.h>
43 #include <synch/mutex.h>
44 #include <synch/waitq.h>
45 #include <ipc/ipc.h>
46 #include <ipc/ipcrsc.h>
47 #include <abi/ipc/methods.h>
48 #include <ipc/kbox.h>
49 #include <ipc/event.h>
50 #include <ipc/sysipc_ops.h>
51 #include <ipc/sysipc_priv.h>
52 #include <errno.h>
53 #include <mm/slab.h>
54 #include <arch.h>
55 #include <proc/task.h>
56 #include <mem.h>
57 #include <stdio.h>
58 #include <console/console.h>
59 #include <proc/thread.h>
60 #include <arch/interrupt.h>
61 #include <ipc/irq.h>
62 #include <cap/cap.h>
63 #include <stdlib.h>
65 static void ipc_forget_call(call_t *);
67 /** Answerbox that new tasks are automatically connected to */
68 answerbox_t *ipc_box_0 = NULL;
70 static slab_cache_t *call_cache;
71 static slab_cache_t *answerbox_cache;
73 slab_cache_t *phone_cache = NULL;
75 /** Initialize a call structure.
77 * @param call Call structure to be initialized.
80 static void _ipc_call_init(call_t *call)
82 memsetb(call, sizeof(*call), 0);
83 spinlock_initialize(&call->forget_lock, "forget_lock");
84 call->active = false;
85 call->forget = false;
86 call->sender = NULL;
87 call->callerbox = NULL;
88 call->buffer = NULL;
91 static void call_destroy(void *arg)
93 call_t *call = (call_t *) arg;
95 if (call->buffer)
96 free(call->buffer);
97 if (call->caller_phone)
98 kobject_put(call->caller_phone->kobject);
99 slab_free(call_cache, call);
102 static kobject_ops_t call_kobject_ops = {
103 .destroy = call_destroy
106 /** Allocate and initialize a call structure.
108 * The call is initialized, so that the reply will be directed to
109 * TASK->answerbox.
111 * @return Initialized kernel call structure with one reference, or NULL.
114 call_t *ipc_call_alloc(void)
116 // TODO: Allocate call and kobject in single allocation
118 call_t *call = slab_alloc(call_cache, FRAME_ATOMIC);
119 if (!call)
120 return NULL;
122 kobject_t *kobj = kobject_alloc(0);
123 if (!kobj) {
124 slab_free(call_cache, call);
125 return NULL;
128 _ipc_call_init(call);
129 kobject_initialize(kobj, KOBJECT_TYPE_CALL, call, &call_kobject_ops);
130 call->kobject = kobj;
132 return call;
135 /** Initialize an answerbox structure.
137 * @param box Answerbox structure to be initialized.
138 * @param task Task to which the answerbox belongs.
141 void ipc_answerbox_init(answerbox_t *box, task_t *task)
143 irq_spinlock_initialize(&box->lock, "ipc.box.lock");
144 irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
145 waitq_initialize(&box->wq);
146 list_initialize(&box->connected_phones);
147 list_initialize(&box->calls);
148 list_initialize(&box->dispatched_calls);
149 list_initialize(&box->answers);
150 list_initialize(&box->irq_notifs);
151 atomic_store(&box->active_calls, 0);
152 box->task = task;
155 /** Connect a phone to an answerbox.
157 * This function must be passed a reference to phone->kobject.
159 * @param phone Initialized phone structure.
160 * @param box Initialized answerbox structure.
161 * @return True if the phone was connected, false otherwise.
163 bool ipc_phone_connect(phone_t *phone, answerbox_t *box)
165 bool connected;
167 mutex_lock(&phone->lock);
168 irq_spinlock_lock(&box->lock, true);
170 connected = box->active && (phone->state == IPC_PHONE_CONNECTING);
171 if (connected) {
172 phone->state = IPC_PHONE_CONNECTED;
173 phone->callee = box;
174 /* Pass phone->kobject reference to box->connected_phones */
175 list_append(&phone->link, &box->connected_phones);
178 irq_spinlock_unlock(&box->lock, true);
179 mutex_unlock(&phone->lock);
181 if (!connected) {
182 /* We still have phone->kobject's reference; drop it */
183 kobject_put(phone->kobject);
186 return connected;
189 /** Initialize a phone structure.
191 * @param phone Phone structure to be initialized.
192 * @param caller Owning task.
195 void ipc_phone_init(phone_t *phone, task_t *caller)
197 mutex_initialize(&phone->lock, MUTEX_PASSIVE);
198 phone->caller = caller;
199 phone->callee = NULL;
200 phone->state = IPC_PHONE_FREE;
201 atomic_store(&phone->active_calls, 0);
202 phone->label = 0;
203 phone->kobject = NULL;
206 /** Helper function to facilitate synchronous calls.
208 * @param phone Destination kernel phone structure.
209 * @param request Call structure with request.
211 * @return EOK on success or an error code.
214 errno_t ipc_call_sync(phone_t *phone, call_t *request)
216 answerbox_t *mybox = slab_alloc(answerbox_cache, FRAME_ATOMIC);
217 if (!mybox)
218 return ENOMEM;
220 ipc_answerbox_init(mybox, TASK);
222 /* We will receive data in a special box. */
223 request->callerbox = mybox;
225 errno_t rc = ipc_call(phone, request);
226 if (rc != EOK) {
227 slab_free(answerbox_cache, mybox);
228 return rc;
231 call_t *answer = NULL;
232 (void) ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT,
233 SYNCH_FLAGS_INTERRUPTIBLE, &answer);
234 if (!answer) {
237 * The sleep was interrupted.
239 * There are two possibilities now:
240 * 1) the call gets answered before we manage to forget it
241 * 2) we manage to forget the call before it gets answered
244 spinlock_lock(&request->forget_lock);
245 spinlock_lock(&TASK->active_calls_lock);
247 assert(!request->forget);
249 bool answered = !request->active;
250 if (!answered) {
252 * The call is not yet answered and we won the race to
253 * forget it.
255 ipc_forget_call(request); /* releases locks */
256 rc = EINTR;
258 } else {
259 spinlock_unlock(&TASK->active_calls_lock);
260 spinlock_unlock(&request->forget_lock);
263 if (answered) {
265 * The other side won the race to answer the call.
266 * It is safe to wait for the answer uninterruptibly
267 * now.
269 (void) ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT,
270 SYNCH_FLAGS_NONE, &answer);
273 assert(!answer || request == answer);
275 slab_free(answerbox_cache, mybox);
276 return rc;
279 /** Answer a message which was not dispatched and is not listed in any queue.
281 * @param call Call structure to be answered.
282 * @param selflocked If true, then TASK->answebox is locked.
285 void _ipc_answer_free_call(call_t *call, bool selflocked)
287 /* Count sent answer */
288 irq_spinlock_lock(&TASK->lock, true);
289 TASK->ipc_info.answer_sent++;
290 irq_spinlock_unlock(&TASK->lock, true);
292 spinlock_lock(&call->forget_lock);
293 if (call->forget) {
294 /* This is a forgotten call and call->sender is not valid. */
295 spinlock_unlock(&call->forget_lock);
296 kobject_put(call->kobject);
297 return;
298 } else {
300 * If the call is still active, i.e. it was answered
301 * in a non-standard way, remove the call from the
302 * sender's active call list.
304 if (call->active) {
305 spinlock_lock(&call->sender->active_calls_lock);
306 list_remove(&call->ta_link);
307 spinlock_unlock(&call->sender->active_calls_lock);
310 spinlock_unlock(&call->forget_lock);
312 answerbox_t *callerbox = call->callerbox ? call->callerbox :
313 &call->sender->answerbox;
314 bool do_lock = ((!selflocked) || (callerbox != &TASK->answerbox));
316 call->flags |= IPC_CALL_ANSWERED;
318 call->data.task_id = TASK->taskid;
320 if (do_lock)
321 irq_spinlock_lock(&callerbox->lock, true);
323 list_append(&call->ab_link, &callerbox->answers);
325 if (do_lock)
326 irq_spinlock_unlock(&callerbox->lock, true);
328 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
331 /** Answer a message which is in a callee queue.
333 * @param box Answerbox that is answering the message.
334 * @param call Modified request that is being sent back.
337 void ipc_answer(answerbox_t *box, call_t *call)
339 /* Remove from active box */
340 irq_spinlock_lock(&box->lock, true);
341 list_remove(&call->ab_link);
342 irq_spinlock_unlock(&box->lock, true);
344 /* Send back answer */
345 _ipc_answer_free_call(call, false);
348 static void _ipc_call_actions_internal(phone_t *phone, call_t *call,
349 bool preforget)
351 task_t *caller = phone->caller;
353 call->caller_phone = phone;
354 kobject_add_ref(phone->kobject);
356 if (preforget) {
357 call->forget = true;
358 } else {
359 atomic_inc(&phone->active_calls);
360 if (call->callerbox)
361 atomic_inc(&call->callerbox->active_calls);
362 else
363 atomic_inc(&caller->answerbox.active_calls);
364 kobject_add_ref(phone->kobject);
365 call->sender = caller;
366 call->active = true;
367 spinlock_lock(&caller->active_calls_lock);
368 list_append(&call->ta_link, &caller->active_calls);
369 spinlock_unlock(&caller->active_calls_lock);
372 call->data.request_label = phone->label;
373 call->data.task_id = caller->taskid;
376 /** Simulate sending back a message.
378 * Most errors are better handled by forming a normal backward
379 * message and sending it as a normal answer.
381 * @param phone Phone structure the call should appear to come from.
382 * @param call Call structure to be answered.
383 * @param err Return value to be used for the answer.
386 void ipc_backsend_err(phone_t *phone, call_t *call, errno_t err)
388 _ipc_call_actions_internal(phone, call, false);
389 IPC_SET_RETVAL(call->data, err);
390 _ipc_answer_free_call(call, false);
393 /** Unsafe unchecking version of ipc_call.
395 * @param phone Phone structure the call comes from.
396 * @param box Destination answerbox structure.
397 * @param call Call structure with request.
398 * @param preforget If true, the call will be delivered already forgotten.
401 static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call,
402 bool preforget)
404 task_t *caller = phone->caller;
406 /* Count sent ipc call */
407 irq_spinlock_lock(&caller->lock, true);
408 caller->ipc_info.call_sent++;
409 irq_spinlock_unlock(&caller->lock, true);
411 if (!(call->flags & IPC_CALL_FORWARDED))
412 _ipc_call_actions_internal(phone, call, preforget);
414 irq_spinlock_lock(&box->lock, true);
415 list_append(&call->ab_link, &box->calls);
416 irq_spinlock_unlock(&box->lock, true);
418 waitq_wakeup(&box->wq, WAKEUP_FIRST);
421 /** Send an asynchronous request using a phone to an answerbox.
423 * @param phone Phone structure the call comes from and which is
424 * connected to the destination answerbox.
425 * @param call Call structure with request.
427 * @return Return 0 on success, ENOENT on error.
430 errno_t ipc_call(phone_t *phone, call_t *call)
432 mutex_lock(&phone->lock);
433 if (phone->state != IPC_PHONE_CONNECTED) {
434 mutex_unlock(&phone->lock);
435 if (!(call->flags & IPC_CALL_FORWARDED)) {
436 if (phone->state == IPC_PHONE_HUNGUP)
437 ipc_backsend_err(phone, call, EHANGUP);
438 else
439 ipc_backsend_err(phone, call, ENOENT);
442 return ENOENT;
445 answerbox_t *box = phone->callee;
446 _ipc_call(phone, box, call, false);
448 mutex_unlock(&phone->lock);
449 return 0;
452 /** Disconnect phone from answerbox.
454 * This call leaves the phone in the hung-up state. The phone is destroyed when
455 * its last active call is answered and there are no references to it.
457 * @param phone Phone structure to be hung up.
459 * @return EOK if the phone is disconnected.
460 * @return EINVAL if the phone was already disconnected.
463 errno_t ipc_phone_hangup(phone_t *phone)
465 mutex_lock(&phone->lock);
466 if (phone->state == IPC_PHONE_FREE ||
467 phone->state == IPC_PHONE_HUNGUP ||
468 phone->state == IPC_PHONE_CONNECTING) {
469 mutex_unlock(&phone->lock);
470 return EINVAL;
473 answerbox_t *box = phone->callee;
474 if (phone->state != IPC_PHONE_SLAMMED) {
475 /* Remove myself from answerbox */
476 irq_spinlock_lock(&box->lock, true);
477 list_remove(&phone->link);
478 irq_spinlock_unlock(&box->lock, true);
480 /* Drop the answerbox reference */
481 kobject_put(phone->kobject);
483 call_t *call = phone->hangup_call;
484 phone->hangup_call = NULL;
485 assert(call);
487 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
488 call->request_method = IPC_M_PHONE_HUNGUP;
489 call->flags |= IPC_CALL_DISCARD_ANSWER;
490 _ipc_call(phone, box, call, false);
493 phone->state = IPC_PHONE_HUNGUP;
494 mutex_unlock(&phone->lock);
496 return EOK;
499 /** Forwards call from one answerbox to another one.
501 * @param call Call structure to be redirected.
502 * @param newphone Phone structure to target answerbox.
503 * @param oldbox Old answerbox structure.
504 * @param mode Flags that specify mode of the forward operation.
506 * @return 0 if forwarding succeeded or an error code if
507 * there was an error.
509 * The return value serves only as an information for the forwarder,
510 * the original caller is notified automatically with EFORWARD.
513 errno_t ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
514 unsigned int mode)
516 /* Count forwarded calls */
517 irq_spinlock_lock(&TASK->lock, true);
518 TASK->ipc_info.forwarded++;
519 irq_spinlock_pass(&TASK->lock, &oldbox->lock);
520 list_remove(&call->ab_link);
521 irq_spinlock_unlock(&oldbox->lock, true);
523 if (mode & IPC_FF_ROUTE_FROM_ME) {
524 call->data.request_label = newphone->label;
525 call->data.task_id = TASK->taskid;
528 return ipc_call(newphone, call);
531 /** Wait for a phone call.
533 * @param box Answerbox expecting the call.
534 * @param usec Timeout in microseconds. See documentation for
535 * waitq_sleep_timeout() for decription of its special
536 * meaning.
537 * @param flags Select mode of sleep operation. See documentation for
538 * waitq_sleep_timeout() for description of its special
539 * meaning.
540 * @param call Received call structure or NULL.
542 * @return Error code from waitq_sleep_timeout.
543 * ENOENT if sleep returns successfully, but there is no call.
545 * To distinguish between a call and an answer, have a look at call->flags.
548 errno_t ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags,
549 call_t **call)
551 call_t *request;
552 uint64_t irq_cnt = 0;
553 uint64_t answer_cnt = 0;
554 uint64_t call_cnt = 0;
555 errno_t rc;
557 rc = waitq_sleep_timeout(&box->wq, usec, flags, NULL);
558 if (rc != EOK)
559 return rc;
561 irq_spinlock_lock(&box->lock, true);
562 if (!list_empty(&box->irq_notifs)) {
563 /* Count received IRQ notification */
564 irq_cnt++;
566 irq_spinlock_lock(&box->irq_lock, false);
568 request = list_get_instance(list_first(&box->irq_notifs),
569 call_t, ab_link);
570 list_remove(&request->ab_link);
572 irq_spinlock_unlock(&box->irq_lock, false);
573 } else if (!list_empty(&box->answers)) {
574 /* Count received answer */
575 answer_cnt++;
577 /* Handle asynchronous answers */
578 request = list_get_instance(list_first(&box->answers),
579 call_t, ab_link);
580 list_remove(&request->ab_link);
581 atomic_dec(&request->caller_phone->active_calls);
582 atomic_dec(&box->active_calls);
583 kobject_put(request->caller_phone->kobject);
584 } else if (!list_empty(&box->calls)) {
585 /* Count received call */
586 call_cnt++;
588 /* Handle requests */
589 request = list_get_instance(list_first(&box->calls),
590 call_t, ab_link);
591 list_remove(&request->ab_link);
593 /* Append request to dispatch queue */
594 list_append(&request->ab_link, &box->dispatched_calls);
595 } else {
597 * This can happen regularly after ipc_cleanup, or in
598 * response to ipc_poke(). Let the caller sort out the wakeup.
600 irq_spinlock_unlock(&box->lock, true);
601 return ENOENT;
604 irq_spinlock_pass(&box->lock, &TASK->lock);
606 TASK->ipc_info.irq_notif_received += irq_cnt;
607 TASK->ipc_info.answer_received += answer_cnt;
608 TASK->ipc_info.call_received += call_cnt;
610 irq_spinlock_unlock(&TASK->lock, true);
612 *call = request;
613 return EOK;
616 /** Answer all calls from list with EHANGUP answer.
618 * @param box Answerbox with the list.
619 * @param lst Head of the list to be cleaned up.
621 void ipc_cleanup_call_list(answerbox_t *box, list_t *lst)
623 irq_spinlock_lock(&box->lock, true);
624 while (!list_empty(lst)) {
625 call_t *call = list_get_instance(list_first(lst), call_t,
626 ab_link);
628 list_remove(&call->ab_link);
630 irq_spinlock_unlock(&box->lock, true);
632 if (lst == &box->calls)
633 SYSIPC_OP(request_process, call, box);
635 ipc_data_t old = call->data;
636 IPC_SET_RETVAL(call->data, EHANGUP);
637 answer_preprocess(call, &old);
638 _ipc_answer_free_call(call, true);
640 irq_spinlock_lock(&box->lock, true);
642 irq_spinlock_unlock(&box->lock, true);
645 /** Disconnects all phones connected to an answerbox.
647 * @param box Answerbox to disconnect phones from.
648 * @param notify_box If true, the answerbox will get a hangup message for
649 * each disconnected phone.
652 void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
654 phone_t *phone;
655 DEADLOCK_PROBE_INIT(p_phonelck);
657 /* Disconnect all phones connected to our answerbox */
658 restart_phones:
659 irq_spinlock_lock(&box->lock, true);
660 while (!list_empty(&box->connected_phones)) {
661 phone = list_get_instance(list_first(&box->connected_phones),
662 phone_t, link);
663 if (mutex_trylock(&phone->lock) != EOK) {
664 irq_spinlock_unlock(&box->lock, true);
665 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
666 goto restart_phones;
669 /* Disconnect phone */
670 assert(phone->state == IPC_PHONE_CONNECTED);
672 list_remove(&phone->link);
673 phone->state = IPC_PHONE_SLAMMED;
674 phone->label = 0;
676 if (notify_box) {
677 task_hold(phone->caller);
678 mutex_unlock(&phone->lock);
679 irq_spinlock_unlock(&box->lock, true);
682 * Send one call to the answerbox for each phone.
683 * Used to make sure the kbox thread wakes up after
684 * the last phone has been disconnected. The call is
685 * forgotten upon sending, so the "caller" may cease
686 * to exist as soon as we release it.
688 call_t *call = phone->hangup_call;
689 phone->hangup_call = NULL;
690 assert(call);
692 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
693 call->request_method = IPC_M_PHONE_HUNGUP;
694 call->flags |= IPC_CALL_DISCARD_ANSWER;
695 _ipc_call(phone, box, call, true);
697 task_release(phone->caller);
699 kobject_put(phone->kobject);
701 /* Must start again */
702 goto restart_phones;
705 mutex_unlock(&phone->lock);
706 kobject_put(phone->kobject);
709 irq_spinlock_unlock(&box->lock, true);
712 static void ipc_forget_call(call_t *call)
714 assert(spinlock_locked(&TASK->active_calls_lock));
715 assert(spinlock_locked(&call->forget_lock));
718 * Forget the call and donate it to the task which holds up the answer.
721 call->forget = true;
722 call->sender = NULL;
723 list_remove(&call->ta_link);
726 * The call may be freed by _ipc_answer_free_call() before we are done
727 * with it; to avoid working with a destroyed call_t structure, we
728 * must hold a reference to it.
730 kobject_add_ref(call->kobject);
732 spinlock_unlock(&call->forget_lock);
733 spinlock_unlock(&TASK->active_calls_lock);
735 atomic_dec(&call->caller_phone->active_calls);
736 atomic_dec(&TASK->answerbox.active_calls);
737 kobject_put(call->caller_phone->kobject);
739 SYSIPC_OP(request_forget, call);
741 kobject_put(call->kobject);
744 static void ipc_forget_all_active_calls(void)
746 call_t *call;
748 restart:
749 spinlock_lock(&TASK->active_calls_lock);
750 if (list_empty(&TASK->active_calls)) {
752 * We are done, there are no more active calls.
753 * Nota bene: there may still be answers waiting for pick up.
755 spinlock_unlock(&TASK->active_calls_lock);
756 return;
759 call = list_get_instance(list_first(&TASK->active_calls), call_t,
760 ta_link);
762 if (!spinlock_trylock(&call->forget_lock)) {
764 * Avoid deadlock and let async_answer() or
765 * _ipc_answer_free_call() win the race to dequeue the first
766 * call on the list.
768 spinlock_unlock(&TASK->active_calls_lock);
769 goto restart;
772 ipc_forget_call(call);
774 goto restart;
777 static bool phone_cap_cleanup_cb(cap_t *cap, void *arg)
779 ipc_phone_hangup(cap->kobject->phone);
780 kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
781 KOBJECT_TYPE_PHONE);
782 kobject_put(kobj);
783 cap_free(cap->task, cap->handle);
784 return true;
787 /** Wait for all answers to asynchronous calls to arrive. */
788 static void ipc_wait_for_all_answered_calls(void)
790 while (atomic_load(&TASK->answerbox.active_calls) != 0) {
791 call_t *call = NULL;
792 if (ipc_wait_for_call(&TASK->answerbox,
793 SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE, &call) == ENOENT)
794 continue;
795 assert(call);
796 assert(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
798 SYSIPC_OP(answer_process, call);
800 kobject_put(call->kobject);
803 * Now there may be some new phones and new hangup calls to
804 * immediately forget.
806 caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_PHONE,
807 phone_cap_cleanup_cb, NULL);
808 ipc_forget_all_active_calls();
812 static bool irq_cap_cleanup_cb(cap_t *cap, void *arg)
814 ipc_irq_unsubscribe(&TASK->answerbox, cap->handle);
815 return true;
818 static bool call_cap_cleanup_cb(cap_t *cap, void *arg)
821 * Here we just free the capability and release the kobject.
822 * The kernel answers the remaining calls elsewhere in ipc_cleanup().
824 kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
825 KOBJECT_TYPE_CALL);
826 kobject_put(kobj);
827 cap_free(cap->task, cap->handle);
828 return true;
831 /** Clean up all IPC communication of the current task.
833 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
834 * have to change it as well if you want to cleanup other tasks than TASK.
837 void ipc_cleanup(void)
840 * Mark the answerbox as inactive.
842 * The main purpose for doing this is to prevent any pending callback
843 * connections from getting established beyond this point.
845 irq_spinlock_lock(&TASK->answerbox.lock, true);
846 TASK->answerbox.active = false;
847 irq_spinlock_unlock(&TASK->answerbox.lock, true);
849 /* Hangup all phones and destroy all phone capabilities */
850 caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_PHONE,
851 phone_cap_cleanup_cb, NULL);
853 /* Unsubscribe from any event notifications */
854 event_cleanup_answerbox(&TASK->answerbox);
856 /* Disconnect all connected IRQs */
857 caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_IRQ, irq_cap_cleanup_cb,
858 NULL);
860 /* Disconnect all phones connected to our regular answerbox */
861 ipc_answerbox_slam_phones(&TASK->answerbox, false);
863 #ifdef CONFIG_UDEBUG
864 /* Clean up kbox thread and communications */
865 ipc_kbox_cleanup();
866 #endif
868 /* Destroy all call capabilities */
869 caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_CALL, call_cap_cleanup_cb,
870 NULL);
872 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
873 ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
874 ipc_cleanup_call_list(&TASK->answerbox,
875 &TASK->answerbox.dispatched_calls);
877 ipc_forget_all_active_calls();
878 ipc_wait_for_all_answered_calls();
880 assert(atomic_load(&TASK->answerbox.active_calls) == 0);
883 /** Initilize IPC subsystem
886 void ipc_init(void)
888 call_cache = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
889 NULL, 0);
890 phone_cache = slab_cache_create("phone_t", sizeof(phone_t), 0, NULL,
891 NULL, 0);
892 answerbox_cache = slab_cache_create("answerbox_t", sizeof(answerbox_t),
893 0, NULL, NULL, 0);
896 static void ipc_print_call_list(list_t *list)
898 list_foreach(*list, ab_link, call_t, call) {
899 #ifdef __32_BITS__
900 printf("%10p ", call);
901 #endif
903 #ifdef __64_BITS__
904 printf("%18p ", call);
905 #endif
907 spinlock_lock(&call->forget_lock);
909 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
910 " %-6" PRIun " %-6" PRIun " %-7x",
911 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
912 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
913 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
914 call->flags);
916 if (call->forget) {
917 printf(" ? (call forgotten)\n");
918 } else {
919 printf(" %" PRIu64 " (%s)\n",
920 call->sender->taskid, call->sender->name);
923 spinlock_unlock(&call->forget_lock);
927 static bool print_task_phone_cb(cap_t *cap, void *arg)
929 phone_t *phone = cap->kobject->phone;
931 mutex_lock(&phone->lock);
932 if (phone->state != IPC_PHONE_FREE) {
933 printf("%-11d %7" PRIun " ", (int) CAP_HANDLE_RAW(cap->handle),
934 atomic_load(&phone->active_calls));
936 switch (phone->state) {
937 case IPC_PHONE_CONNECTING:
938 printf("connecting");
939 break;
940 case IPC_PHONE_CONNECTED:
941 printf("connected to %" PRIu64 " (%s)",
942 phone->callee->task->taskid,
943 phone->callee->task->name);
944 break;
945 case IPC_PHONE_SLAMMED:
946 printf("slammed by %p", phone->callee);
947 break;
948 case IPC_PHONE_HUNGUP:
949 printf("hung up to %p", phone->callee);
950 break;
951 default:
952 break;
955 printf("\n");
957 mutex_unlock(&phone->lock);
959 return true;
962 /** List answerbox contents.
964 * @param taskid Task ID.
967 void ipc_print_task(task_id_t taskid)
969 irq_spinlock_lock(&tasks_lock, true);
970 task_t *task = task_find_by_id(taskid);
971 if (!task) {
972 irq_spinlock_unlock(&tasks_lock, true);
973 return;
975 task_hold(task);
976 irq_spinlock_unlock(&tasks_lock, true);
978 printf("[phone cap] [calls] [state\n");
980 caps_apply_to_kobject_type(task, KOBJECT_TYPE_PHONE,
981 print_task_phone_cb, NULL);
983 irq_spinlock_lock(&task->lock, true);
984 irq_spinlock_lock(&task->answerbox.lock, false);
986 printf("Active calls: %" PRIun "\n",
987 atomic_load(&task->answerbox.active_calls));
989 #ifdef __32_BITS__
990 printf("[call adr] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
991 " [flags] [sender\n");
992 #endif
994 #ifdef __64_BITS__
995 printf("[call address ] [method] [arg1] [arg2] [arg3] [arg4]"
996 " [arg5] [flags] [sender\n");
997 #endif
999 printf(" --- incomming calls ---\n");
1000 ipc_print_call_list(&task->answerbox.calls);
1001 printf(" --- dispatched calls ---\n");
1002 ipc_print_call_list(&task->answerbox.dispatched_calls);
1003 printf(" --- incoming answers ---\n");
1004 ipc_print_call_list(&task->answerbox.answers);
1006 irq_spinlock_unlock(&task->answerbox.lock, false);
1007 irq_spinlock_unlock(&task->lock, true);
1009 task_release(task);
1012 /** @}