source: mainline/kernel/generic/src/ipc/ipc.c@ 716185d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 716185d was 716185d, checked in by Jakub Jermar <jakub@…>, 13 years ago

Call request_process() callback for incoming calls during IPC cleanup.

  • Property mode set to 100644
File size: 22.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 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_ops_t *ops;
517
518 ops = sysipc_ops_get(call->request_method);
519 if (ops->request_process)
520 (void) ops->request_process(call, box);
521 }
522
523 ipc_data_t old = call->data;
524 IPC_SET_RETVAL(call->data, EHANGUP);
525 answer_preprocess(call, &old);
526 _ipc_answer_free_call(call, true);
527
528 irq_spinlock_lock(&box->lock, true);
529 }
530 irq_spinlock_unlock(&box->lock, true);
531}
532
533/** Disconnects all phones connected to an answerbox.
534 *
535 * @param box Answerbox to disconnect phones from.
536 * @param notify_box If true, the answerbox will get a hangup message for
537 * each disconnected phone.
538 *
539 */
540void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
541{
542 phone_t *phone;
543 DEADLOCK_PROBE_INIT(p_phonelck);
544
545 call_t *call = notify_box ? ipc_call_alloc(0) : NULL;
546
547 /* Disconnect all phones connected to our answerbox */
548restart_phones:
549 irq_spinlock_lock(&box->lock, true);
550 while (!list_empty(&box->connected_phones)) {
551 phone = list_get_instance(list_first(&box->connected_phones),
552 phone_t, link);
553 if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
554 irq_spinlock_unlock(&box->lock, true);
555 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
556 goto restart_phones;
557 }
558
559 /* Disconnect phone */
560 ASSERT(phone->state == IPC_PHONE_CONNECTED);
561
562 list_remove(&phone->link);
563 phone->state = IPC_PHONE_SLAMMED;
564
565 if (notify_box) {
566 mutex_unlock(&phone->lock);
567 irq_spinlock_unlock(&box->lock, true);
568
569 // FIXME: phone can become deallocated at any time now
570
571 /*
572 * Send one message to the answerbox for each
573 * phone. Used to make sure the kbox thread
574 * wakes up after the last phone has been
575 * disconnected.
576 */
577 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
578 call->request_method = IPC_M_PHONE_HUNGUP;
579 call->flags |= IPC_CALL_DISCARD_ANSWER;
580 _ipc_call(phone, box, call);
581
582 /* Allocate another call in advance */
583 call = ipc_call_alloc(0);
584
585 /* Must start again */
586 goto restart_phones;
587 }
588
589 mutex_unlock(&phone->lock);
590 }
591
592 irq_spinlock_unlock(&box->lock, true);
593
594 /* Free unused call */
595 if (call)
596 ipc_call_free(call);
597}
598
599static void ipc_forget_all_active_calls(void)
600{
601 call_t *call;
602
603restart:
604 spinlock_lock(&TASK->active_calls_lock);
605 if (list_empty(&TASK->active_calls)) {
606 /*
607 * We are done, there are no more active calls.
608 * Nota bene: there may still be answers waiting for pick up.
609 */
610 spinlock_unlock(&TASK->active_calls_lock);
611 return;
612 }
613
614 call = list_get_instance(list_first(&TASK->active_calls), call_t,
615 ta_link);
616
617 if (!spinlock_trylock(&call->forget_lock)) {
618 /*
619 * Avoid deadlock and let async_answer() or
620 * _ipc_answer_free_call() win the race to dequeue the first
621 * call on the list.
622 */
623 spinlock_unlock(&TASK->active_calls_lock);
624 goto restart;
625 }
626
627 /*
628 * Forget the call and donate it to the task which holds up the answer.
629 */
630
631 call->forget = true;
632 call->sender = NULL;
633 list_remove(&call->ta_link);
634
635 /*
636 * The call may be freed by _ipc_answer_free_call() before we are done
637 * with it; to avoid working with a destroyed call_t structure, we
638 * must hold a reference to it.
639 */
640 ipc_call_hold(call);
641
642 spinlock_unlock(&call->forget_lock);
643 spinlock_unlock(&TASK->active_calls_lock);
644
645 atomic_dec(&call->caller_phone->active_calls);
646
647 sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
648 if (ops->request_forget)
649 ops->request_forget(call);
650
651 ipc_call_release(call);
652
653 goto restart;
654}
655
656/** Wait for all answers to asynchronous calls to arrive. */
657static void ipc_wait_for_all_answered_calls(void)
658{
659 call_t *call;
660 size_t i;
661
662restart:
663 /*
664 * Go through all phones, until they are all free.
665 * Locking is needed as there may be connection handshakes in progress.
666 */
667 for (i = 0; i < IPC_MAX_PHONES; i++) {
668 phone_t *phone = &TASK->phones[i];
669
670 mutex_lock(&phone->lock);
671 if ((phone->state == IPC_PHONE_HUNGUP) &&
672 (atomic_get(&phone->active_calls) == 0)) {
673 phone->state = IPC_PHONE_FREE;
674 phone->callee = NULL;
675 }
676
677 /*
678 * We might have had some IPC_PHONE_CONNECTING phones at the
679 * beginning of ipc_cleanup(). Depending on whether these were
680 * forgotten or answered, they will eventually enter the
681 * IPC_PHONE_FREE or IPC_PHONE_CONNECTED states, respectively.
682 * In the latter case, the other side may slam the open phones
683 * at any time, in which case we will get an IPC_PHONE_SLAMMED
684 * phone.
685 */
686 if ((phone->state == IPC_PHONE_CONNECTED) ||
687 (phone->state == IPC_PHONE_SLAMMED)) {
688 mutex_unlock(&phone->lock);
689 ipc_phone_hangup(phone);
690 /*
691 * Now there may be one extra active call, which needs
692 * to be forgotten.
693 */
694 ipc_forget_all_active_calls();
695 goto restart;
696 }
697
698 /*
699 * If the hangup succeeded, it has sent a HANGUP message, the
700 * IPC is now in HUNGUP state, we wait for the reply to come
701 */
702 if (phone->state != IPC_PHONE_FREE) {
703 mutex_unlock(&phone->lock);
704 break;
705 }
706
707 mutex_unlock(&phone->lock);
708 }
709
710 /* Got into cleanup */
711 if (i == IPC_MAX_PHONES)
712 return;
713
714 call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
715 SYNCH_FLAGS_NONE);
716 ASSERT(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
717
718 sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
719 if (ops->answer_process)
720 ops->answer_process(call);
721
722 ipc_call_free(call);
723 goto restart;
724}
725
726/** Clean up all IPC communication of the current task.
727 *
728 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
729 * have to change it as well if you want to cleanup other tasks than TASK.
730 *
731 */
732void ipc_cleanup(void)
733{
734 /*
735 * Mark the answerbox as inactive.
736 *
737 * The main purpose for doing this is to prevent any pending callback
738 * connections from getting established beyond this point.
739 */
740 irq_spinlock_lock(&TASK->answerbox.lock, true);
741 TASK->answerbox.active = false;
742 irq_spinlock_unlock(&TASK->answerbox.lock, true);
743
744 /* Disconnect all our phones ('ipc_phone_hangup') */
745 for (size_t i = 0; i < IPC_MAX_PHONES; i++)
746 ipc_phone_hangup(&TASK->phones[i]);
747
748 /* Unsubscribe from any event notifications. */
749 event_cleanup_answerbox(&TASK->answerbox);
750
751 /* Disconnect all connected irqs */
752 ipc_irq_cleanup(&TASK->answerbox);
753
754 /* Disconnect all phones connected to our regular answerbox */
755 ipc_answerbox_slam_phones(&TASK->answerbox, false);
756
757#ifdef CONFIG_UDEBUG
758 /* Clean up kbox thread and communications */
759 ipc_kbox_cleanup();
760#endif
761
762 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
763 ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
764 ipc_cleanup_call_list(&TASK->answerbox,
765 &TASK->answerbox.dispatched_calls);
766
767 ipc_forget_all_active_calls();
768 ipc_wait_for_all_answered_calls();
769}
770
771/** Initilize IPC subsystem
772 *
773 */
774void ipc_init(void)
775{
776 ipc_call_slab = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
777 NULL, 0);
778 ipc_answerbox_slab = slab_cache_create("answerbox_t",
779 sizeof(answerbox_t), 0, NULL, NULL, 0);
780}
781
782
783static void ipc_print_call_list(list_t *list)
784{
785 list_foreach(*list, cur) {
786 call_t *call = list_get_instance(cur, call_t, ab_link);
787
788#ifdef __32_BITS__
789 printf("%10p ", call);
790#endif
791
792#ifdef __64_BITS__
793 printf("%18p ", call);
794#endif
795
796 spinlock_lock(&call->forget_lock);
797
798 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
799 " %-6" PRIun " %-6" PRIun " %-7x",
800 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
801 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
802 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
803 call->flags);
804
805 if (call->forget) {
806 printf(" ? (call forgotten)\n");
807 } else {
808 printf(" %" PRIu64 " (%s)\n",
809 call->sender->taskid, call->sender->name);
810 }
811
812 spinlock_unlock(&call->forget_lock);
813 }
814}
815
816/** List answerbox contents.
817 *
818 * @param taskid Task ID.
819 *
820 */
821void ipc_print_task(task_id_t taskid)
822{
823 irq_spinlock_lock(&tasks_lock, true);
824 task_t *task = task_find_by_id(taskid);
825
826 if (!task) {
827 irq_spinlock_unlock(&tasks_lock, true);
828 return;
829 }
830
831 /* Hand-over-hand locking */
832 irq_spinlock_exchange(&tasks_lock, &task->lock);
833
834 printf("[phone id] [calls] [state\n");
835
836 size_t i;
837 for (i = 0; i < IPC_MAX_PHONES; i++) {
838 if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
839 printf("%-10zu (mutex busy)\n", i);
840 continue;
841 }
842
843 if (task->phones[i].state != IPC_PHONE_FREE) {
844 printf("%-10zu %7" PRIun " ", i,
845 atomic_get(&task->phones[i].active_calls));
846
847 switch (task->phones[i].state) {
848 case IPC_PHONE_CONNECTING:
849 printf("connecting");
850 break;
851 case IPC_PHONE_CONNECTED:
852 printf("connected to %" PRIu64 " (%s)",
853 task->phones[i].callee->task->taskid,
854 task->phones[i].callee->task->name);
855 break;
856 case IPC_PHONE_SLAMMED:
857 printf("slammed by %p",
858 task->phones[i].callee);
859 break;
860 case IPC_PHONE_HUNGUP:
861 printf("hung up by %p",
862 task->phones[i].callee);
863 break;
864 default:
865 break;
866 }
867
868 printf("\n");
869 }
870
871 mutex_unlock(&task->phones[i].lock);
872 }
873
874 irq_spinlock_lock(&task->answerbox.lock, false);
875
876#ifdef __32_BITS__
877 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
878 " [flags] [sender\n");
879#endif
880
881#ifdef __64_BITS__
882 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
883 " [arg5] [flags] [sender\n");
884#endif
885
886 printf(" --- incomming calls ---\n");
887 ipc_print_call_list(&task->answerbox.calls);
888 printf(" --- dispatched calls ---\n");
889 ipc_print_call_list(&task->answerbox.dispatched_calls);
890 printf(" --- incoming answers ---\n");
891 ipc_print_call_list(&task->answerbox.answers);
892
893 irq_spinlock_unlock(&task->answerbox.lock, false);
894 irq_spinlock_unlock(&task->lock, true);
895}
896
897/** @}
898 */
Note: See TracBrowser for help on using the repository browser.