source: mainline/kernel/generic/src/ipc/ipc.c@ 455241b

Last change on this file since 455241b was 455241b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 5 months ago

Integrate kobject_t into target structures

This just means fewer allocations and indirections.

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