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

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

Prevent likely double free of call→buffer.

The buffer is supposed to be eventually freed by ipc_call_free() along
with the call_t structure itself.

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