source: mainline/kernel/generic/src/ipc/ipc.c@ 2f7d77c6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2f7d77c6 was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

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