source: mainline/kernel/generic/src/ipc/ipc.c@ f1d5ef8

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

Document two issues in ipc_answerbox_slam_phones() when cleaning up a kbox.

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