source: mainline/kernel/generic/src/ipc/ipc.c@ 5a77550

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

Always remember the original caller phone in a call.

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