source: mainline/kernel/generic/src/ipc/ipc.c@ 566da7f8

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

Simplify use of list_foreach.

  • Property mode set to 100644
File size: 22.6 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/* Lock ordering
36 *
37 * First the answerbox, then the phone.
38 */
39
40#include <synch/spinlock.h>
41#include <synch/mutex.h>
42#include <synch/waitq.h>
43#include <ipc/ipc.h>
44#include <abi/ipc/methods.h>
45#include <ipc/kbox.h>
46#include <ipc/event.h>
47#include <ipc/sysipc_ops.h>
48#include <ipc/sysipc_priv.h>
49#include <errno.h>
50#include <mm/slab.h>
51#include <arch.h>
52#include <proc/task.h>
53#include <memstr.h>
54#include <debug.h>
55#include <print.h>
56#include <console/console.h>
57#include <proc/thread.h>
58#include <arch/interrupt.h>
59#include <ipc/irq.h>
60
61/** Open channel that is assigned automatically to new tasks */
62answerbox_t *ipc_phone_0 = NULL;
63
64static slab_cache_t *ipc_call_slab;
65static slab_cache_t *ipc_answerbox_slab;
66
67/** Initialize a call structure.
68 *
69 * @param call Call structure to be initialized.
70 *
71 */
72static void _ipc_call_init(call_t *call)
73{
74 memsetb(call, sizeof(*call), 0);
75 spinlock_initialize(&call->forget_lock, "forget_lock");
76 call->active = false;
77 call->forget = false;
78 call->sender = NULL;
79 call->buffer = NULL;
80}
81
82void ipc_call_hold(call_t *call)
83{
84 atomic_inc(&call->refcnt);
85}
86
87void ipc_call_release(call_t *call)
88{
89 if (atomic_predec(&call->refcnt) == 0) {
90 if (call->buffer)
91 free(call->buffer);
92 slab_free(ipc_call_slab, call);
93 }
94}
95
96/** Allocate and initialize a call structure.
97 *
98 * The call is initialized, so that the reply will be directed to
99 * TASK->answerbox.
100 *
101 * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
102 *
103 * @return If flags permit it, return NULL, or initialized kernel
104 * call structure with one reference.
105 *
106 */
107call_t *ipc_call_alloc(unsigned int flags)
108{
109 call_t *call = slab_alloc(ipc_call_slab, flags);
110 if (call) {
111 _ipc_call_init(call);
112 ipc_call_hold(call);
113 }
114
115 return call;
116}
117
118/** Deallocate a call structure.
119 *
120 * @param call Call structure to be freed.
121 *
122 */
123void ipc_call_free(call_t *call)
124{
125 ipc_call_release(call);
126}
127
128/** Initialize an answerbox structure.
129 *
130 * @param box Answerbox structure to be initialized.
131 * @param task Task to which the answerbox belongs.
132 *
133 */
134void ipc_answerbox_init(answerbox_t *box, task_t *task)
135{
136 irq_spinlock_initialize(&box->lock, "ipc.box.lock");
137 irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
138 waitq_initialize(&box->wq);
139 list_initialize(&box->connected_phones);
140 list_initialize(&box->calls);
141 list_initialize(&box->dispatched_calls);
142 list_initialize(&box->answers);
143 list_initialize(&box->irq_notifs);
144 list_initialize(&box->irq_list);
145 box->task = task;
146}
147
148/** Connect a phone to an answerbox.
149 *
150 * @param phone Initialized phone structure.
151 * @param box Initialized answerbox structure.
152 * @return True if the phone was connected, false otherwise.
153 */
154bool ipc_phone_connect(phone_t *phone, answerbox_t *box)
155{
156 bool active;
157
158 mutex_lock(&phone->lock);
159 irq_spinlock_lock(&box->lock, true);
160
161 active = box->active;
162 if (active) {
163 phone->state = IPC_PHONE_CONNECTED;
164 phone->callee = box;
165 list_append(&phone->link, &box->connected_phones);
166 }
167
168 irq_spinlock_unlock(&box->lock, true);
169 mutex_unlock(&phone->lock);
170
171 return active;
172}
173
174/** Initialize a phone structure.
175 *
176 * @param phone Phone structure to be initialized.
177 * @param caller Owning task.
178 *
179 */
180void ipc_phone_init(phone_t *phone, task_t *caller)
181{
182 mutex_initialize(&phone->lock, MUTEX_PASSIVE);
183 phone->caller = caller;
184 phone->callee = NULL;
185 phone->state = IPC_PHONE_FREE;
186 atomic_set(&phone->active_calls, 0);
187}
188
189/** Answer a message which was not dispatched and is not listed in any queue.
190 *
191 * @param call Call structure to be answered.
192 * @param selflocked If true, then TASK->answebox is locked.
193 *
194 */
195void _ipc_answer_free_call(call_t *call, bool selflocked)
196{
197 /* Count sent answer */
198 irq_spinlock_lock(&TASK->lock, true);
199 TASK->ipc_info.answer_sent++;
200 irq_spinlock_unlock(&TASK->lock, true);
201
202 spinlock_lock(&call->forget_lock);
203 if (call->forget) {
204 /* This is a forgotten call and call->sender is not valid. */
205 spinlock_unlock(&call->forget_lock);
206 ipc_call_free(call);
207 return;
208 } else {
209 /*
210 * If the call is still active, i.e. it was answered
211 * in a non-standard way, remove the call from the
212 * sender's active call list.
213 */
214 if (call->active) {
215 spinlock_lock(&call->sender->active_calls_lock);
216 list_remove(&call->ta_link);
217 spinlock_unlock(&call->sender->active_calls_lock);
218 }
219 }
220 spinlock_unlock(&call->forget_lock);
221
222 answerbox_t *callerbox = &call->sender->answerbox;
223 bool do_lock = ((!selflocked) || (callerbox != &TASK->answerbox));
224
225 call->flags |= IPC_CALL_ANSWERED;
226
227 call->data.task_id = TASK->taskid;
228
229 if (do_lock)
230 irq_spinlock_lock(&callerbox->lock, true);
231
232 list_append(&call->ab_link, &callerbox->answers);
233
234 if (do_lock)
235 irq_spinlock_unlock(&callerbox->lock, true);
236
237 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
238}
239
240/** Answer a message which is in a callee queue.
241 *
242 * @param box Answerbox that is answering the message.
243 * @param call Modified request that is being sent back.
244 *
245 */
246void ipc_answer(answerbox_t *box, call_t *call)
247{
248 /* Remove from active box */
249 irq_spinlock_lock(&box->lock, true);
250 list_remove(&call->ab_link);
251 irq_spinlock_unlock(&box->lock, true);
252
253 /* Send back answer */
254 _ipc_answer_free_call(call, false);
255}
256
257static void _ipc_call_actions_internal(phone_t *phone, call_t *call)
258{
259 task_t *caller = phone->caller;
260
261 atomic_inc(&phone->active_calls);
262 call->caller_phone = phone;
263 call->sender = caller;
264
265 call->active = true;
266 spinlock_lock(&caller->active_calls_lock);
267 list_append(&call->ta_link, &caller->active_calls);
268 spinlock_unlock(&caller->active_calls_lock);
269
270 call->data.phone = phone;
271 call->data.task_id = caller->taskid;
272}
273
274/** Simulate sending back a message.
275 *
276 * Most errors are better handled by forming a normal backward
277 * message and sending it as a normal answer.
278 *
279 * @param phone Phone structure the call should appear to come from.
280 * @param call Call structure to be answered.
281 * @param err Return value to be used for the answer.
282 *
283 */
284void ipc_backsend_err(phone_t *phone, call_t *call, sysarg_t err)
285{
286 _ipc_call_actions_internal(phone, call);
287 IPC_SET_RETVAL(call->data, err);
288 _ipc_answer_free_call(call, false);
289}
290
291/** Unsafe unchecking version of ipc_call.
292 *
293 * @param phone Phone structure the call comes from.
294 * @param box Destination answerbox structure.
295 * @param call Call structure with request.
296 *
297 */
298static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
299{
300 task_t *caller = phone->caller;
301
302 /* Count sent ipc call */
303 irq_spinlock_lock(&caller->lock, true);
304 caller->ipc_info.call_sent++;
305 irq_spinlock_unlock(&caller->lock, true);
306
307 if (!(call->flags & IPC_CALL_FORWARDED))
308 _ipc_call_actions_internal(phone, call);
309
310 irq_spinlock_lock(&box->lock, true);
311 list_append(&call->ab_link, &box->calls);
312 irq_spinlock_unlock(&box->lock, true);
313
314 waitq_wakeup(&box->wq, WAKEUP_FIRST);
315}
316
317/** Send an asynchronous request using a phone to an answerbox.
318 *
319 * @param phone Phone structure the call comes from and which is
320 * connected to the destination answerbox.
321 * @param call Call structure with request.
322 *
323 * @return Return 0 on success, ENOENT on error.
324 *
325 */
326int ipc_call(phone_t *phone, call_t *call)
327{
328 mutex_lock(&phone->lock);
329 if (phone->state != IPC_PHONE_CONNECTED) {
330 mutex_unlock(&phone->lock);
331 if (!(call->flags & IPC_CALL_FORWARDED)) {
332 if (phone->state == IPC_PHONE_HUNGUP)
333 ipc_backsend_err(phone, call, EHANGUP);
334 else
335 ipc_backsend_err(phone, call, ENOENT);
336 }
337
338 return ENOENT;
339 }
340
341 answerbox_t *box = phone->callee;
342 _ipc_call(phone, box, call);
343
344 mutex_unlock(&phone->lock);
345 return 0;
346}
347
348/** Disconnect phone from answerbox.
349 *
350 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
351 * lazily later.
352 *
353 * @param phone Phone structure to be hung up.
354 *
355 * @return 0 if the phone is disconnected.
356 * @return -1 if the phone was already disconnected.
357 *
358 */
359int ipc_phone_hangup(phone_t *phone)
360{
361 mutex_lock(&phone->lock);
362 if (phone->state == IPC_PHONE_FREE ||
363 phone->state == IPC_PHONE_HUNGUP ||
364 phone->state == IPC_PHONE_CONNECTING) {
365 mutex_unlock(&phone->lock);
366 return -1;
367 }
368
369 answerbox_t *box = phone->callee;
370 if (phone->state != IPC_PHONE_SLAMMED) {
371 /* Remove myself from answerbox */
372 irq_spinlock_lock(&box->lock, true);
373 list_remove(&phone->link);
374 irq_spinlock_unlock(&box->lock, true);
375
376 call_t *call = ipc_call_alloc(0);
377 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
378 call->request_method = IPC_M_PHONE_HUNGUP;
379 call->flags |= IPC_CALL_DISCARD_ANSWER;
380 _ipc_call(phone, box, call);
381 }
382
383 phone->state = IPC_PHONE_HUNGUP;
384 mutex_unlock(&phone->lock);
385
386 return 0;
387}
388
389/** Forwards call from one answerbox to another one.
390 *
391 * @param call Call structure to be redirected.
392 * @param newphone Phone structure to target answerbox.
393 * @param oldbox Old answerbox structure.
394 * @param mode Flags that specify mode of the forward operation.
395 *
396 * @return 0 if forwarding succeeded or an error code if
397 * there was an error.
398 *
399 * The return value serves only as an information for the forwarder,
400 * the original caller is notified automatically with EFORWARD.
401 *
402 */
403int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
404 unsigned int mode)
405{
406 /* Count forwarded calls */
407 irq_spinlock_lock(&TASK->lock, true);
408 TASK->ipc_info.forwarded++;
409 irq_spinlock_pass(&TASK->lock, &oldbox->lock);
410 list_remove(&call->ab_link);
411 irq_spinlock_unlock(&oldbox->lock, true);
412
413 if (mode & IPC_FF_ROUTE_FROM_ME) {
414 call->data.phone = newphone;
415 call->data.task_id = TASK->taskid;
416 }
417
418 return ipc_call(newphone, call);
419}
420
421
422/** Wait for a phone call.
423 *
424 * @param box Answerbox expecting the call.
425 * @param usec Timeout in microseconds. See documentation for
426 * waitq_sleep_timeout() for decription of its special
427 * meaning.
428 * @param flags Select mode of sleep operation. See documentation for
429 * waitq_sleep_timeout() for description of its special
430 * meaning.
431 *
432 * @return Recived call structure or NULL.
433 *
434 * To distinguish between a call and an answer, have a look at call->flags.
435 *
436 */
437call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
438{
439 call_t *request;
440 uint64_t irq_cnt = 0;
441 uint64_t answer_cnt = 0;
442 uint64_t call_cnt = 0;
443 int rc;
444
445restart:
446 rc = waitq_sleep_timeout(&box->wq, usec, flags);
447 if (SYNCH_FAILED(rc))
448 return NULL;
449
450 irq_spinlock_lock(&box->lock, true);
451 if (!list_empty(&box->irq_notifs)) {
452 /* Count received IRQ notification */
453 irq_cnt++;
454
455 irq_spinlock_lock(&box->irq_lock, false);
456
457 request = list_get_instance(list_first(&box->irq_notifs),
458 call_t, ab_link);
459 list_remove(&request->ab_link);
460
461 irq_spinlock_unlock(&box->irq_lock, false);
462 } else if (!list_empty(&box->answers)) {
463 /* Count received answer */
464 answer_cnt++;
465
466 /* Handle asynchronous answers */
467 request = list_get_instance(list_first(&box->answers),
468 call_t, ab_link);
469 list_remove(&request->ab_link);
470 atomic_dec(&request->caller_phone->active_calls);
471 } else if (!list_empty(&box->calls)) {
472 /* Count received call */
473 call_cnt++;
474
475 /* Handle requests */
476 request = list_get_instance(list_first(&box->calls),
477 call_t, ab_link);
478 list_remove(&request->ab_link);
479
480 /* Append request to dispatch queue */
481 list_append(&request->ab_link, &box->dispatched_calls);
482 } else {
483 /* This can happen regularly after ipc_cleanup */
484 irq_spinlock_unlock(&box->lock, true);
485 goto restart;
486 }
487
488 irq_spinlock_pass(&box->lock, &TASK->lock);
489
490 TASK->ipc_info.irq_notif_received += irq_cnt;
491 TASK->ipc_info.answer_received += answer_cnt;
492 TASK->ipc_info.call_received += call_cnt;
493
494 irq_spinlock_unlock(&TASK->lock, true);
495
496 return request;
497}
498
499/** Answer all calls from list with EHANGUP answer.
500 *
501 * @param box Answerbox with the list.
502 * @param lst Head of the list to be cleaned up.
503 */
504void ipc_cleanup_call_list(answerbox_t *box, list_t *lst)
505{
506 irq_spinlock_lock(&box->lock, true);
507 while (!list_empty(lst)) {
508 call_t *call = list_get_instance(list_first(lst), call_t,
509 ab_link);
510
511 list_remove(&call->ab_link);
512
513 irq_spinlock_unlock(&box->lock, true);
514
515 if (lst == &box->calls)
516 SYSIPC_OP(request_process, call, box);
517
518 ipc_data_t old = call->data;
519 IPC_SET_RETVAL(call->data, EHANGUP);
520 answer_preprocess(call, &old);
521 _ipc_answer_free_call(call, true);
522
523 irq_spinlock_lock(&box->lock, true);
524 }
525 irq_spinlock_unlock(&box->lock, true);
526}
527
528/** Disconnects all phones connected to an answerbox.
529 *
530 * @param box Answerbox to disconnect phones from.
531 * @param notify_box If true, the answerbox will get a hangup message for
532 * each disconnected phone.
533 *
534 */
535void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
536{
537 phone_t *phone;
538 DEADLOCK_PROBE_INIT(p_phonelck);
539
540 call_t *call = notify_box ? ipc_call_alloc(0) : NULL;
541
542 /* Disconnect all phones connected to our answerbox */
543restart_phones:
544 irq_spinlock_lock(&box->lock, true);
545 while (!list_empty(&box->connected_phones)) {
546 phone = list_get_instance(list_first(&box->connected_phones),
547 phone_t, link);
548 if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
549 irq_spinlock_unlock(&box->lock, true);
550 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
551 goto restart_phones;
552 }
553
554 /* Disconnect phone */
555 ASSERT(phone->state == IPC_PHONE_CONNECTED);
556
557 list_remove(&phone->link);
558 phone->state = IPC_PHONE_SLAMMED;
559
560 if (notify_box) {
561 mutex_unlock(&phone->lock);
562 irq_spinlock_unlock(&box->lock, true);
563
564 // FIXME: phone can become deallocated at any time now
565
566 /*
567 * Send one message to the answerbox for each
568 * phone. Used to make sure the kbox thread
569 * wakes up after the last phone has been
570 * disconnected.
571 */
572 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
573 call->request_method = IPC_M_PHONE_HUNGUP;
574 call->flags |= IPC_CALL_DISCARD_ANSWER;
575 _ipc_call(phone, box, call);
576
577 /* Allocate another call in advance */
578 call = ipc_call_alloc(0);
579
580 /* Must start again */
581 goto restart_phones;
582 }
583
584 mutex_unlock(&phone->lock);
585 }
586
587 irq_spinlock_unlock(&box->lock, true);
588
589 /* Free unused call */
590 if (call)
591 ipc_call_free(call);
592}
593
594static void ipc_forget_all_active_calls(void)
595{
596 call_t *call;
597
598restart:
599 spinlock_lock(&TASK->active_calls_lock);
600 if (list_empty(&TASK->active_calls)) {
601 /*
602 * We are done, there are no more active calls.
603 * Nota bene: there may still be answers waiting for pick up.
604 */
605 spinlock_unlock(&TASK->active_calls_lock);
606 return;
607 }
608
609 call = list_get_instance(list_first(&TASK->active_calls), call_t,
610 ta_link);
611
612 if (!spinlock_trylock(&call->forget_lock)) {
613 /*
614 * Avoid deadlock and let async_answer() or
615 * _ipc_answer_free_call() win the race to dequeue the first
616 * call on the list.
617 */
618 spinlock_unlock(&TASK->active_calls_lock);
619 goto restart;
620 }
621
622 /*
623 * Forget the call and donate it to the task which holds up the answer.
624 */
625
626 call->forget = true;
627 call->sender = NULL;
628 list_remove(&call->ta_link);
629
630 /*
631 * The call may be freed by _ipc_answer_free_call() before we are done
632 * with it; to avoid working with a destroyed call_t structure, we
633 * must hold a reference to it.
634 */
635 ipc_call_hold(call);
636
637 spinlock_unlock(&call->forget_lock);
638 spinlock_unlock(&TASK->active_calls_lock);
639
640 atomic_dec(&call->caller_phone->active_calls);
641
642 SYSIPC_OP(request_forget, call);
643
644 ipc_call_release(call);
645
646 goto restart;
647}
648
649/** Wait for all answers to asynchronous calls to arrive. */
650static void ipc_wait_for_all_answered_calls(void)
651{
652 call_t *call;
653 size_t i;
654
655restart:
656 /*
657 * Go through all phones, until they are all free.
658 * Locking is needed as there may be connection handshakes in progress.
659 */
660 for (i = 0; i < IPC_MAX_PHONES; i++) {
661 phone_t *phone = &TASK->phones[i];
662
663 mutex_lock(&phone->lock);
664 if ((phone->state == IPC_PHONE_HUNGUP) &&
665 (atomic_get(&phone->active_calls) == 0)) {
666 phone->state = IPC_PHONE_FREE;
667 phone->callee = NULL;
668 }
669
670 /*
671 * We might have had some IPC_PHONE_CONNECTING phones at the
672 * beginning of ipc_cleanup(). Depending on whether these were
673 * forgotten or answered, they will eventually enter the
674 * IPC_PHONE_FREE or IPC_PHONE_CONNECTED states, respectively.
675 * In the latter case, the other side may slam the open phones
676 * at any time, in which case we will get an IPC_PHONE_SLAMMED
677 * phone.
678 */
679 if ((phone->state == IPC_PHONE_CONNECTED) ||
680 (phone->state == IPC_PHONE_SLAMMED)) {
681 mutex_unlock(&phone->lock);
682 ipc_phone_hangup(phone);
683 /*
684 * Now there may be one extra active call, which needs
685 * to be forgotten.
686 */
687 ipc_forget_all_active_calls();
688 goto restart;
689 }
690
691 /*
692 * If the hangup succeeded, it has sent a HANGUP message, the
693 * IPC is now in HUNGUP state, we wait for the reply to come
694 */
695 if (phone->state != IPC_PHONE_FREE) {
696 mutex_unlock(&phone->lock);
697 break;
698 }
699
700 mutex_unlock(&phone->lock);
701 }
702
703 /* Got into cleanup */
704 if (i == IPC_MAX_PHONES)
705 return;
706
707 call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
708 SYNCH_FLAGS_NONE);
709 ASSERT(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
710
711 SYSIPC_OP(answer_process, call);
712
713 ipc_call_free(call);
714 goto restart;
715}
716
717/** Clean up all IPC communication of the current task.
718 *
719 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
720 * have to change it as well if you want to cleanup other tasks than TASK.
721 *
722 */
723void ipc_cleanup(void)
724{
725 /*
726 * Mark the answerbox as inactive.
727 *
728 * The main purpose for doing this is to prevent any pending callback
729 * connections from getting established beyond this point.
730 */
731 irq_spinlock_lock(&TASK->answerbox.lock, true);
732 TASK->answerbox.active = false;
733 irq_spinlock_unlock(&TASK->answerbox.lock, true);
734
735 /* Disconnect all our phones ('ipc_phone_hangup') */
736 for (size_t i = 0; i < IPC_MAX_PHONES; i++)
737 ipc_phone_hangup(&TASK->phones[i]);
738
739 /* Unsubscribe from any event notifications. */
740 event_cleanup_answerbox(&TASK->answerbox);
741
742 /* Disconnect all connected irqs */
743 ipc_irq_cleanup(&TASK->answerbox);
744
745 /* Disconnect all phones connected to our regular answerbox */
746 ipc_answerbox_slam_phones(&TASK->answerbox, false);
747
748#ifdef CONFIG_UDEBUG
749 /* Clean up kbox thread and communications */
750 ipc_kbox_cleanup();
751#endif
752
753 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
754 ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
755 ipc_cleanup_call_list(&TASK->answerbox,
756 &TASK->answerbox.dispatched_calls);
757
758 ipc_forget_all_active_calls();
759 ipc_wait_for_all_answered_calls();
760}
761
762/** Initilize IPC subsystem
763 *
764 */
765void ipc_init(void)
766{
767 ipc_call_slab = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
768 NULL, 0);
769 ipc_answerbox_slab = slab_cache_create("answerbox_t",
770 sizeof(answerbox_t), 0, NULL, NULL, 0);
771}
772
773
774static void ipc_print_call_list(list_t *list)
775{
776 list_foreach(*list, ab_link, call_t, call) {
777#ifdef __32_BITS__
778 printf("%10p ", call);
779#endif
780
781#ifdef __64_BITS__
782 printf("%18p ", call);
783#endif
784
785 spinlock_lock(&call->forget_lock);
786
787 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
788 " %-6" PRIun " %-6" PRIun " %-7x",
789 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
790 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
791 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
792 call->flags);
793
794 if (call->forget) {
795 printf(" ? (call forgotten)\n");
796 } else {
797 printf(" %" PRIu64 " (%s)\n",
798 call->sender->taskid, call->sender->name);
799 }
800
801 spinlock_unlock(&call->forget_lock);
802 }
803}
804
805/** List answerbox contents.
806 *
807 * @param taskid Task ID.
808 *
809 */
810void ipc_print_task(task_id_t taskid)
811{
812 irq_spinlock_lock(&tasks_lock, true);
813 task_t *task = task_find_by_id(taskid);
814
815 if (!task) {
816 irq_spinlock_unlock(&tasks_lock, true);
817 return;
818 }
819
820 /* Hand-over-hand locking */
821 irq_spinlock_exchange(&tasks_lock, &task->lock);
822
823 printf("[phone id] [calls] [state\n");
824
825 size_t i;
826 for (i = 0; i < IPC_MAX_PHONES; i++) {
827 if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
828 printf("%-10zu (mutex busy)\n", i);
829 continue;
830 }
831
832 if (task->phones[i].state != IPC_PHONE_FREE) {
833 printf("%-10zu %7" PRIun " ", i,
834 atomic_get(&task->phones[i].active_calls));
835
836 switch (task->phones[i].state) {
837 case IPC_PHONE_CONNECTING:
838 printf("connecting");
839 break;
840 case IPC_PHONE_CONNECTED:
841 printf("connected to %" PRIu64 " (%s)",
842 task->phones[i].callee->task->taskid,
843 task->phones[i].callee->task->name);
844 break;
845 case IPC_PHONE_SLAMMED:
846 printf("slammed by %p",
847 task->phones[i].callee);
848 break;
849 case IPC_PHONE_HUNGUP:
850 printf("hung up by %p",
851 task->phones[i].callee);
852 break;
853 default:
854 break;
855 }
856
857 printf("\n");
858 }
859
860 mutex_unlock(&task->phones[i].lock);
861 }
862
863 irq_spinlock_lock(&task->answerbox.lock, false);
864
865#ifdef __32_BITS__
866 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
867 " [flags] [sender\n");
868#endif
869
870#ifdef __64_BITS__
871 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
872 " [arg5] [flags] [sender\n");
873#endif
874
875 printf(" --- incomming calls ---\n");
876 ipc_print_call_list(&task->answerbox.calls);
877 printf(" --- dispatched calls ---\n");
878 ipc_print_call_list(&task->answerbox.dispatched_calls);
879 printf(" --- incoming answers ---\n");
880 ipc_print_call_list(&task->answerbox.answers);
881
882 irq_spinlock_unlock(&task->answerbox.lock, false);
883 irq_spinlock_unlock(&task->lock, true);
884}
885
886/** @}
887 */
Note: See TracBrowser for help on using the repository browser.