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

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

Add two new sysipc_ops_t members:

  • request_forget()
  • answer_cleanup()

Call these members to perform cleanup at appropriate times.

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