source: mainline/kernel/generic/src/ipc/ipc.c@ 63d8f43

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

Allocate the kobjects referenced by capabilities dynamically

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