source: mainline/kernel/generic/src/ipc/ipc.c@ 6d351e6

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

Revert changeset jakub@…

We will use the possibility to specify a different answerbox for replies
for sending page in and page out requests from the user_backend page
fault handler.

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