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

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

Store capability's handle inside of it

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