source: mainline/kernel/generic/src/ipc/ipc.c@ 20282ef3

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

When answer_preprocess() wins the race for a call, let it also dequeue
the call from the sender's active list so that we have a real atomic
either-answered-or-forgotten behavior.

This change avoids the possibility that the race is won in
answer_preprocess(), but lost later in _ipc_answer_free_call(), which
would have the effect of skipping some of the necessary call_t cleanup.

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