source: mainline/kernel/generic/src/ipc/ipc.c@ be6e37a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since be6e37a was abf6c01, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Add FRAME_ATOMIC to some allocations

  • Property mode set to 100644
File size: 26.1 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup kernel_generic_ipc
30 * @{
31 */
32/** @file
33 */
34
35/*
36 * Lock ordering
37 *
38 * First the answerbox, then the phone.
39 */
40
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
64static void ipc_forget_call(call_t *);
65
66/** Answerbox that new tasks are automatically connected to */
67answerbox_t *ipc_box_0 = NULL;
68
69static slab_cache_t *call_cache;
70static slab_cache_t *answerbox_cache;
71
72slab_cache_t *phone_cache = NULL;
73
74/** Initialize a call structure.
75 *
76 * @param call Call structure to be initialized.
77 *
78 */
79static void _ipc_call_init(call_t *call)
80{
81 memsetb(call, sizeof(*call), 0);
82 spinlock_initialize(&call->forget_lock, "forget_lock");
83 call->active = false;
84 call->forget = false;
85 call->sender = NULL;
86 call->callerbox = NULL;
87 call->buffer = NULL;
88}
89
90static void call_destroy(void *arg)
91{
92 call_t *call = (call_t *) arg;
93
94 if (call->buffer)
95 free(call->buffer);
96 if (call->caller_phone)
97 kobject_put(call->caller_phone->kobject);
98 slab_free(call_cache, call);
99}
100
101static kobject_ops_t call_kobject_ops = {
102 .destroy = call_destroy
103};
104
105/** Allocate and initialize a call structure.
106 *
107 * The call is initialized, so that the reply will be directed to
108 * TASK->answerbox.
109 *
110 * @return Initialized kernel call structure with one reference, or NULL.
111 *
112 */
113call_t *ipc_call_alloc(void)
114{
115 // TODO: Allocate call and kobject in single allocation
116
117 call_t *call = slab_alloc(call_cache, FRAME_ATOMIC);
118 if (!call)
119 return NULL;
120
121 kobject_t *kobj = (kobject_t *) malloc(sizeof(kobject_t));
122 if (!kobj) {
123 slab_free(call_cache, call);
124 return NULL;
125 }
126
127 _ipc_call_init(call);
128 kobject_initialize(kobj, KOBJECT_TYPE_CALL, call, &call_kobject_ops);
129 call->kobject = kobj;
130
131 return call;
132}
133
134/** Initialize an answerbox structure.
135 *
136 * @param box Answerbox structure to be initialized.
137 * @param task Task to which the answerbox belongs.
138 *
139 */
140void ipc_answerbox_init(answerbox_t *box, task_t *task)
141{
142 irq_spinlock_initialize(&box->lock, "ipc.box.lock");
143 irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
144 waitq_initialize(&box->wq);
145 list_initialize(&box->connected_phones);
146 list_initialize(&box->calls);
147 list_initialize(&box->dispatched_calls);
148 list_initialize(&box->answers);
149 list_initialize(&box->irq_notifs);
150 atomic_store(&box->active_calls, 0);
151 box->task = task;
152}
153
154/** Connect a phone to an answerbox.
155 *
156 * This function must be passed a reference to phone->kobject.
157 *
158 * @param phone Initialized phone structure.
159 * @param box Initialized answerbox structure.
160 * @return True if the phone was connected, false otherwise.
161 */
162bool ipc_phone_connect(phone_t *phone, answerbox_t *box)
163{
164 bool connected;
165
166 mutex_lock(&phone->lock);
167 irq_spinlock_lock(&box->lock, true);
168
169 connected = box->active && (phone->state == IPC_PHONE_CONNECTING);
170 if (connected) {
171 phone->state = IPC_PHONE_CONNECTED;
172 phone->callee = box;
173 /* Pass phone->kobject reference to box->connected_phones */
174 list_append(&phone->link, &box->connected_phones);
175 }
176
177 irq_spinlock_unlock(&box->lock, true);
178 mutex_unlock(&phone->lock);
179
180 if (!connected) {
181 /* We still have phone->kobject's reference; drop it */
182 kobject_put(phone->kobject);
183 }
184
185 return connected;
186}
187
188/** Initialize a phone structure.
189 *
190 * @param phone Phone structure to be initialized.
191 * @param caller Owning task.
192 *
193 */
194void ipc_phone_init(phone_t *phone, task_t *caller)
195{
196 mutex_initialize(&phone->lock, MUTEX_PASSIVE);
197 phone->caller = caller;
198 phone->callee = NULL;
199 phone->state = IPC_PHONE_FREE;
200 atomic_store(&phone->active_calls, 0);
201 phone->label = 0;
202 phone->kobject = NULL;
203}
204
205/** Helper function to facilitate synchronous calls.
206 *
207 * @param phone Destination kernel phone structure.
208 * @param request Call structure with request.
209 *
210 * @return EOK on success or an error code.
211 *
212 */
213errno_t ipc_call_sync(phone_t *phone, call_t *request)
214{
215 answerbox_t *mybox = slab_alloc(answerbox_cache, FRAME_ATOMIC);
216 if (!mybox)
217 return ENOMEM;
218
219 ipc_answerbox_init(mybox, TASK);
220
221 /* We will receive data in a special box. */
222 request->callerbox = mybox;
223
224 errno_t rc = ipc_call(phone, request);
225 if (rc != EOK) {
226 slab_free(answerbox_cache, mybox);
227 return rc;
228 }
229
230 call_t *answer = NULL;
231 (void) ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT,
232 SYNCH_FLAGS_INTERRUPTIBLE, &answer);
233 if (!answer) {
234
235 /*
236 * The sleep was interrupted.
237 *
238 * There are two possibilities now:
239 * 1) the call gets answered before we manage to forget it
240 * 2) we manage to forget the call before it gets answered
241 */
242
243 spinlock_lock(&request->forget_lock);
244 spinlock_lock(&TASK->active_calls_lock);
245
246 assert(!request->forget);
247
248 bool answered = !request->active;
249 if (!answered) {
250 /*
251 * The call is not yet answered and we won the race to
252 * forget it.
253 */
254 ipc_forget_call(request); /* releases locks */
255 rc = EINTR;
256
257 } else {
258 spinlock_unlock(&TASK->active_calls_lock);
259 spinlock_unlock(&request->forget_lock);
260 }
261
262 if (answered) {
263 /*
264 * The other side won the race to answer the call.
265 * It is safe to wait for the answer uninterruptibly
266 * now.
267 */
268 (void) ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT,
269 SYNCH_FLAGS_NONE, &answer);
270 }
271 }
272 assert(!answer || request == answer);
273
274 slab_free(answerbox_cache, mybox);
275 return rc;
276}
277
278/** Answer a message which was not dispatched and is not listed in any queue.
279 *
280 * @param call Call structure to be answered.
281 * @param selflocked If true, then TASK->answebox is locked.
282 *
283 */
284void _ipc_answer_free_call(call_t *call, bool selflocked)
285{
286 /* Count sent answer */
287 irq_spinlock_lock(&TASK->lock, true);
288 TASK->ipc_info.answer_sent++;
289 irq_spinlock_unlock(&TASK->lock, true);
290
291 spinlock_lock(&call->forget_lock);
292 if (call->forget) {
293 /* This is a forgotten call and call->sender is not valid. */
294 spinlock_unlock(&call->forget_lock);
295 kobject_put(call->kobject);
296 return;
297 } else {
298 /*
299 * If the call is still active, i.e. it was answered
300 * in a non-standard way, remove the call from the
301 * sender's active call list.
302 */
303 if (call->active) {
304 spinlock_lock(&call->sender->active_calls_lock);
305 list_remove(&call->ta_link);
306 spinlock_unlock(&call->sender->active_calls_lock);
307 }
308 }
309 spinlock_unlock(&call->forget_lock);
310
311 answerbox_t *callerbox = call->callerbox ? call->callerbox :
312 &call->sender->answerbox;
313 bool do_lock = ((!selflocked) || (callerbox != &TASK->answerbox));
314
315 call->flags |= IPC_CALL_ANSWERED;
316
317 call->data.task_id = TASK->taskid;
318
319 if (do_lock)
320 irq_spinlock_lock(&callerbox->lock, true);
321
322 list_append(&call->ab_link, &callerbox->answers);
323
324 if (do_lock)
325 irq_spinlock_unlock(&callerbox->lock, true);
326
327 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
328}
329
330/** Answer a message which is in a callee queue.
331 *
332 * @param box Answerbox that is answering the message.
333 * @param call Modified request that is being sent back.
334 *
335 */
336void ipc_answer(answerbox_t *box, call_t *call)
337{
338 /* Remove from active box */
339 irq_spinlock_lock(&box->lock, true);
340 list_remove(&call->ab_link);
341 irq_spinlock_unlock(&box->lock, true);
342
343 /* Send back answer */
344 _ipc_answer_free_call(call, false);
345}
346
347static void _ipc_call_actions_internal(phone_t *phone, call_t *call,
348 bool preforget)
349{
350 task_t *caller = phone->caller;
351
352 call->caller_phone = phone;
353 kobject_add_ref(phone->kobject);
354
355 if (preforget) {
356 call->forget = true;
357 } else {
358 atomic_inc(&phone->active_calls);
359 if (call->callerbox)
360 atomic_inc(&call->callerbox->active_calls);
361 else
362 atomic_inc(&caller->answerbox.active_calls);
363 kobject_add_ref(phone->kobject);
364 call->sender = caller;
365 call->active = true;
366 spinlock_lock(&caller->active_calls_lock);
367 list_append(&call->ta_link, &caller->active_calls);
368 spinlock_unlock(&caller->active_calls_lock);
369 }
370
371 call->data.request_label = phone->label;
372 call->data.task_id = caller->taskid;
373}
374
375/** Simulate sending back a message.
376 *
377 * Most errors are better handled by forming a normal backward
378 * message and sending it as a normal answer.
379 *
380 * @param phone Phone structure the call should appear to come from.
381 * @param call Call structure to be answered.
382 * @param err Return value to be used for the answer.
383 *
384 */
385void ipc_backsend_err(phone_t *phone, call_t *call, errno_t err)
386{
387 _ipc_call_actions_internal(phone, call, false);
388 IPC_SET_RETVAL(call->data, err);
389 _ipc_answer_free_call(call, false);
390}
391
392/** Unsafe unchecking version of ipc_call.
393 *
394 * @param phone Phone structure the call comes from.
395 * @param box Destination answerbox structure.
396 * @param call Call structure with request.
397 * @param preforget If true, the call will be delivered already forgotten.
398 *
399 */
400static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call,
401 bool preforget)
402{
403 task_t *caller = phone->caller;
404
405 /* Count sent ipc call */
406 irq_spinlock_lock(&caller->lock, true);
407 caller->ipc_info.call_sent++;
408 irq_spinlock_unlock(&caller->lock, true);
409
410 if (!(call->flags & IPC_CALL_FORWARDED))
411 _ipc_call_actions_internal(phone, call, preforget);
412
413 irq_spinlock_lock(&box->lock, true);
414 list_append(&call->ab_link, &box->calls);
415 irq_spinlock_unlock(&box->lock, true);
416
417 waitq_wakeup(&box->wq, WAKEUP_FIRST);
418}
419
420/** Send an asynchronous request using a phone to an answerbox.
421 *
422 * @param phone Phone structure the call comes from and which is
423 * connected to the destination answerbox.
424 * @param call Call structure with request.
425 *
426 * @return Return 0 on success, ENOENT on error.
427 *
428 */
429errno_t ipc_call(phone_t *phone, call_t *call)
430{
431 mutex_lock(&phone->lock);
432 if (phone->state != IPC_PHONE_CONNECTED) {
433 mutex_unlock(&phone->lock);
434 if (!(call->flags & IPC_CALL_FORWARDED)) {
435 if (phone->state == IPC_PHONE_HUNGUP)
436 ipc_backsend_err(phone, call, EHANGUP);
437 else
438 ipc_backsend_err(phone, call, ENOENT);
439 }
440
441 return ENOENT;
442 }
443
444 answerbox_t *box = phone->callee;
445 _ipc_call(phone, box, call, false);
446
447 mutex_unlock(&phone->lock);
448 return 0;
449}
450
451/** Disconnect phone from answerbox.
452 *
453 * This call leaves the phone in the hung-up state. The phone is destroyed when
454 * its last active call is answered and there are no references to it.
455 *
456 * @param phone Phone structure to be hung up.
457 *
458 * @return EOK if the phone is disconnected.
459 * @return EINVAL if the phone was already disconnected.
460 *
461 */
462errno_t ipc_phone_hangup(phone_t *phone)
463{
464 mutex_lock(&phone->lock);
465 if (phone->state == IPC_PHONE_FREE ||
466 phone->state == IPC_PHONE_HUNGUP ||
467 phone->state == IPC_PHONE_CONNECTING) {
468 mutex_unlock(&phone->lock);
469 return EINVAL;
470 }
471
472 answerbox_t *box = phone->callee;
473 if (phone->state != IPC_PHONE_SLAMMED) {
474 /* Remove myself from answerbox */
475 irq_spinlock_lock(&box->lock, true);
476 list_remove(&phone->link);
477 irq_spinlock_unlock(&box->lock, true);
478
479 /* Drop the answerbox reference */
480 kobject_put(phone->kobject);
481
482 call_t *call = phone->hangup_call;
483 phone->hangup_call = NULL;
484 assert(call);
485
486 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
487 call->request_method = IPC_M_PHONE_HUNGUP;
488 call->flags |= IPC_CALL_DISCARD_ANSWER;
489 _ipc_call(phone, box, call, false);
490 }
491
492 phone->state = IPC_PHONE_HUNGUP;
493 mutex_unlock(&phone->lock);
494
495 return EOK;
496}
497
498/** Forwards call from one answerbox to another one.
499 *
500 * @param call Call structure to be redirected.
501 * @param newphone Phone structure to target answerbox.
502 * @param oldbox Old answerbox structure.
503 * @param mode Flags that specify mode of the forward operation.
504 *
505 * @return 0 if forwarding succeeded or an error code if
506 * there was an error.
507 *
508 * The return value serves only as an information for the forwarder,
509 * the original caller is notified automatically with EFORWARD.
510 *
511 */
512errno_t ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
513 unsigned int mode)
514{
515 /* Count forwarded calls */
516 irq_spinlock_lock(&TASK->lock, true);
517 TASK->ipc_info.forwarded++;
518 irq_spinlock_pass(&TASK->lock, &oldbox->lock);
519 list_remove(&call->ab_link);
520 irq_spinlock_unlock(&oldbox->lock, true);
521
522 if (mode & IPC_FF_ROUTE_FROM_ME) {
523 call->data.request_label = newphone->label;
524 call->data.task_id = TASK->taskid;
525 }
526
527 return ipc_call(newphone, call);
528}
529
530/** Wait for a phone call.
531 *
532 * @param box Answerbox expecting the call.
533 * @param usec Timeout in microseconds. See documentation for
534 * waitq_sleep_timeout() for decription of its special
535 * meaning.
536 * @param flags Select mode of sleep operation. See documentation for
537 * waitq_sleep_timeout() for description of its special
538 * meaning.
539 * @param call Received call structure or NULL.
540 *
541 * @return Error code from waitq_sleep_timeout.
542 * ENOENT if sleep returns successfully, but there is no call.
543 *
544 * To distinguish between a call and an answer, have a look at call->flags.
545 *
546 */
547errno_t ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags,
548 call_t **call)
549{
550 call_t *request;
551 uint64_t irq_cnt = 0;
552 uint64_t answer_cnt = 0;
553 uint64_t call_cnt = 0;
554 errno_t rc;
555
556 rc = waitq_sleep_timeout(&box->wq, usec, flags, NULL);
557 if (rc != EOK)
558 return rc;
559
560 irq_spinlock_lock(&box->lock, true);
561 if (!list_empty(&box->irq_notifs)) {
562 /* Count received IRQ notification */
563 irq_cnt++;
564
565 irq_spinlock_lock(&box->irq_lock, false);
566
567 request = list_get_instance(list_first(&box->irq_notifs),
568 call_t, ab_link);
569 list_remove(&request->ab_link);
570
571 irq_spinlock_unlock(&box->irq_lock, false);
572 } else if (!list_empty(&box->answers)) {
573 /* Count received answer */
574 answer_cnt++;
575
576 /* Handle asynchronous answers */
577 request = list_get_instance(list_first(&box->answers),
578 call_t, ab_link);
579 list_remove(&request->ab_link);
580 atomic_dec(&request->caller_phone->active_calls);
581 atomic_dec(&box->active_calls);
582 kobject_put(request->caller_phone->kobject);
583 } else if (!list_empty(&box->calls)) {
584 /* Count received call */
585 call_cnt++;
586
587 /* Handle requests */
588 request = list_get_instance(list_first(&box->calls),
589 call_t, ab_link);
590 list_remove(&request->ab_link);
591
592 /* Append request to dispatch queue */
593 list_append(&request->ab_link, &box->dispatched_calls);
594 } else {
595 /*
596 * This can happen regularly after ipc_cleanup, or in
597 * response to ipc_poke(). Let the caller sort out the wakeup.
598 */
599 irq_spinlock_unlock(&box->lock, true);
600 return ENOENT;
601 }
602
603 irq_spinlock_pass(&box->lock, &TASK->lock);
604
605 TASK->ipc_info.irq_notif_received += irq_cnt;
606 TASK->ipc_info.answer_received += answer_cnt;
607 TASK->ipc_info.call_received += call_cnt;
608
609 irq_spinlock_unlock(&TASK->lock, true);
610
611 *call = request;
612 return EOK;
613}
614
615/** Answer all calls from list with EHANGUP answer.
616 *
617 * @param box Answerbox with the list.
618 * @param lst Head of the list to be cleaned up.
619 */
620void ipc_cleanup_call_list(answerbox_t *box, list_t *lst)
621{
622 irq_spinlock_lock(&box->lock, true);
623 while (!list_empty(lst)) {
624 call_t *call = list_get_instance(list_first(lst), call_t,
625 ab_link);
626
627 list_remove(&call->ab_link);
628
629 irq_spinlock_unlock(&box->lock, true);
630
631 if (lst == &box->calls)
632 SYSIPC_OP(request_process, call, box);
633
634 ipc_data_t old = call->data;
635 IPC_SET_RETVAL(call->data, EHANGUP);
636 answer_preprocess(call, &old);
637 _ipc_answer_free_call(call, true);
638
639 irq_spinlock_lock(&box->lock, true);
640 }
641 irq_spinlock_unlock(&box->lock, true);
642}
643
644/** Disconnects all phones connected to an answerbox.
645 *
646 * @param box Answerbox to disconnect phones from.
647 * @param notify_box If true, the answerbox will get a hangup message for
648 * each disconnected phone.
649 *
650 */
651void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
652{
653 phone_t *phone;
654 DEADLOCK_PROBE_INIT(p_phonelck);
655
656 /* Disconnect all phones connected to our answerbox */
657restart_phones:
658 irq_spinlock_lock(&box->lock, true);
659 while (!list_empty(&box->connected_phones)) {
660 phone = list_get_instance(list_first(&box->connected_phones),
661 phone_t, link);
662 if (mutex_trylock(&phone->lock) != EOK) {
663 irq_spinlock_unlock(&box->lock, true);
664 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
665 goto restart_phones;
666 }
667
668 /* Disconnect phone */
669 assert(phone->state == IPC_PHONE_CONNECTED);
670
671 list_remove(&phone->link);
672 phone->state = IPC_PHONE_SLAMMED;
673 phone->label = 0;
674
675 if (notify_box) {
676 task_hold(phone->caller);
677 mutex_unlock(&phone->lock);
678 irq_spinlock_unlock(&box->lock, true);
679
680 /*
681 * Send one call to the answerbox for each phone.
682 * Used to make sure the kbox thread wakes up after
683 * the last phone has been disconnected. The call is
684 * forgotten upon sending, so the "caller" may cease
685 * to exist as soon as we release it.
686 */
687 call_t *call = phone->hangup_call;
688 phone->hangup_call = NULL;
689 assert(call);
690
691 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
692 call->request_method = IPC_M_PHONE_HUNGUP;
693 call->flags |= IPC_CALL_DISCARD_ANSWER;
694 _ipc_call(phone, box, call, true);
695
696 task_release(phone->caller);
697
698 kobject_put(phone->kobject);
699
700 /* Must start again */
701 goto restart_phones;
702 }
703
704 mutex_unlock(&phone->lock);
705 kobject_put(phone->kobject);
706 }
707
708 irq_spinlock_unlock(&box->lock, true);
709}
710
711static void ipc_forget_call(call_t *call)
712{
713 assert(spinlock_locked(&TASK->active_calls_lock));
714 assert(spinlock_locked(&call->forget_lock));
715
716 /*
717 * Forget the call and donate it to the task which holds up the answer.
718 */
719
720 call->forget = true;
721 call->sender = NULL;
722 list_remove(&call->ta_link);
723
724 /*
725 * The call may be freed by _ipc_answer_free_call() before we are done
726 * with it; to avoid working with a destroyed call_t structure, we
727 * must hold a reference to it.
728 */
729 kobject_add_ref(call->kobject);
730
731 spinlock_unlock(&call->forget_lock);
732 spinlock_unlock(&TASK->active_calls_lock);
733
734 atomic_dec(&call->caller_phone->active_calls);
735 atomic_dec(&TASK->answerbox.active_calls);
736 kobject_put(call->caller_phone->kobject);
737
738 SYSIPC_OP(request_forget, call);
739
740 kobject_put(call->kobject);
741}
742
743static void ipc_forget_all_active_calls(void)
744{
745 call_t *call;
746
747restart:
748 spinlock_lock(&TASK->active_calls_lock);
749 if (list_empty(&TASK->active_calls)) {
750 /*
751 * We are done, there are no more active calls.
752 * Nota bene: there may still be answers waiting for pick up.
753 */
754 spinlock_unlock(&TASK->active_calls_lock);
755 return;
756 }
757
758 call = list_get_instance(list_first(&TASK->active_calls), call_t,
759 ta_link);
760
761 if (!spinlock_trylock(&call->forget_lock)) {
762 /*
763 * Avoid deadlock and let async_answer() or
764 * _ipc_answer_free_call() win the race to dequeue the first
765 * call on the list.
766 */
767 spinlock_unlock(&TASK->active_calls_lock);
768 goto restart;
769 }
770
771 ipc_forget_call(call);
772
773 goto restart;
774}
775
776static bool phone_cap_cleanup_cb(cap_t *cap, void *arg)
777{
778 ipc_phone_hangup(cap->kobject->phone);
779 kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
780 KOBJECT_TYPE_PHONE);
781 kobject_put(kobj);
782 cap_free(cap->task, cap->handle);
783 return true;
784}
785
786/** Wait for all answers to asynchronous calls to arrive. */
787static void ipc_wait_for_all_answered_calls(void)
788{
789 while (atomic_load(&TASK->answerbox.active_calls) != 0) {
790 call_t *call = NULL;
791 if (ipc_wait_for_call(&TASK->answerbox,
792 SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE, &call) == ENOENT)
793 continue;
794 assert(call);
795 assert(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
796
797 SYSIPC_OP(answer_process, call);
798
799 kobject_put(call->kobject);
800
801 /*
802 * Now there may be some new phones and new hangup calls to
803 * immediately forget.
804 */
805 caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_PHONE,
806 phone_cap_cleanup_cb, NULL);
807 ipc_forget_all_active_calls();
808 }
809}
810
811static bool irq_cap_cleanup_cb(cap_t *cap, void *arg)
812{
813 ipc_irq_unsubscribe(&TASK->answerbox, cap->handle);
814 return true;
815}
816
817static bool call_cap_cleanup_cb(cap_t *cap, void *arg)
818{
819 /*
820 * Here we just free the capability and release the kobject.
821 * The kernel answers the remaining calls elsewhere in ipc_cleanup().
822 */
823 kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
824 KOBJECT_TYPE_CALL);
825 kobject_put(kobj);
826 cap_free(cap->task, cap->handle);
827 return true;
828}
829
830/** Clean up all IPC communication of the current task.
831 *
832 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
833 * have to change it as well if you want to cleanup other tasks than TASK.
834 *
835 */
836void ipc_cleanup(void)
837{
838 /*
839 * Mark the answerbox as inactive.
840 *
841 * The main purpose for doing this is to prevent any pending callback
842 * connections from getting established beyond this point.
843 */
844 irq_spinlock_lock(&TASK->answerbox.lock, true);
845 TASK->answerbox.active = false;
846 irq_spinlock_unlock(&TASK->answerbox.lock, true);
847
848 /* Hangup all phones and destroy all phone capabilities */
849 caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_PHONE,
850 phone_cap_cleanup_cb, NULL);
851
852 /* Unsubscribe from any event notifications */
853 event_cleanup_answerbox(&TASK->answerbox);
854
855 /* Disconnect all connected IRQs */
856 caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_IRQ, irq_cap_cleanup_cb,
857 NULL);
858
859 /* Disconnect all phones connected to our regular answerbox */
860 ipc_answerbox_slam_phones(&TASK->answerbox, false);
861
862#ifdef CONFIG_UDEBUG
863 /* Clean up kbox thread and communications */
864 ipc_kbox_cleanup();
865#endif
866
867 /* Destroy all call capabilities */
868 caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_CALL, call_cap_cleanup_cb,
869 NULL);
870
871 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
872 ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
873 ipc_cleanup_call_list(&TASK->answerbox,
874 &TASK->answerbox.dispatched_calls);
875
876 ipc_forget_all_active_calls();
877 ipc_wait_for_all_answered_calls();
878
879 assert(atomic_load(&TASK->answerbox.active_calls) == 0);
880}
881
882/** Initilize IPC subsystem
883 *
884 */
885void ipc_init(void)
886{
887 call_cache = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
888 NULL, 0);
889 phone_cache = slab_cache_create("phone_t", sizeof(phone_t), 0, NULL,
890 NULL, 0);
891 answerbox_cache = slab_cache_create("answerbox_t", sizeof(answerbox_t),
892 0, NULL, NULL, 0);
893}
894
895static void ipc_print_call_list(list_t *list)
896{
897 list_foreach(*list, ab_link, call_t, call) {
898#ifdef __32_BITS__
899 printf("%10p ", call);
900#endif
901
902#ifdef __64_BITS__
903 printf("%18p ", call);
904#endif
905
906 spinlock_lock(&call->forget_lock);
907
908 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
909 " %-6" PRIun " %-6" PRIun " %-7x",
910 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
911 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
912 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
913 call->flags);
914
915 if (call->forget) {
916 printf(" ? (call forgotten)\n");
917 } else {
918 printf(" %" PRIu64 " (%s)\n",
919 call->sender->taskid, call->sender->name);
920 }
921
922 spinlock_unlock(&call->forget_lock);
923 }
924}
925
926static bool print_task_phone_cb(cap_t *cap, void *arg)
927{
928 phone_t *phone = cap->kobject->phone;
929
930 mutex_lock(&phone->lock);
931 if (phone->state != IPC_PHONE_FREE) {
932 printf("%-11d %7" PRIun " ", (int) CAP_HANDLE_RAW(cap->handle),
933 atomic_load(&phone->active_calls));
934
935 switch (phone->state) {
936 case IPC_PHONE_CONNECTING:
937 printf("connecting");
938 break;
939 case IPC_PHONE_CONNECTED:
940 printf("connected to %" PRIu64 " (%s)",
941 phone->callee->task->taskid,
942 phone->callee->task->name);
943 break;
944 case IPC_PHONE_SLAMMED:
945 printf("slammed by %p", phone->callee);
946 break;
947 case IPC_PHONE_HUNGUP:
948 printf("hung up to %p", phone->callee);
949 break;
950 default:
951 break;
952 }
953
954 printf("\n");
955 }
956 mutex_unlock(&phone->lock);
957
958 return true;
959}
960
961/** List answerbox contents.
962 *
963 * @param taskid Task ID.
964 *
965 */
966void ipc_print_task(task_id_t taskid)
967{
968 irq_spinlock_lock(&tasks_lock, true);
969 task_t *task = task_find_by_id(taskid);
970 if (!task) {
971 irq_spinlock_unlock(&tasks_lock, true);
972 return;
973 }
974 task_hold(task);
975 irq_spinlock_unlock(&tasks_lock, true);
976
977 printf("[phone cap] [calls] [state\n");
978
979 caps_apply_to_kobject_type(task, KOBJECT_TYPE_PHONE,
980 print_task_phone_cb, NULL);
981
982 irq_spinlock_lock(&task->lock, true);
983 irq_spinlock_lock(&task->answerbox.lock, false);
984
985 printf("Active calls: %" PRIun "\n",
986 atomic_load(&task->answerbox.active_calls));
987
988#ifdef __32_BITS__
989 printf("[call adr] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
990 " [flags] [sender\n");
991#endif
992
993#ifdef __64_BITS__
994 printf("[call address ] [method] [arg1] [arg2] [arg3] [arg4]"
995 " [arg5] [flags] [sender\n");
996#endif
997
998 printf(" --- incomming calls ---\n");
999 ipc_print_call_list(&task->answerbox.calls);
1000 printf(" --- dispatched calls ---\n");
1001 ipc_print_call_list(&task->answerbox.dispatched_calls);
1002 printf(" --- incoming answers ---\n");
1003 ipc_print_call_list(&task->answerbox.answers);
1004
1005 irq_spinlock_unlock(&task->answerbox.lock, false);
1006 irq_spinlock_unlock(&task->lock, true);
1007
1008 task_release(task);
1009}
1010
1011/** @}
1012 */
Note: See TracBrowser for help on using the repository browser.