source: mainline/kernel/generic/src/ipc/ipc.c@ 7570a95f

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

Fix vertical spacing with new Ccheck revision.

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