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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ad896eb was 11b285d, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 8 years ago

Use standard signature for malloc() in kernel.

The remaining instances of blocking allocation are replaced with
a new separate function named nfmalloc (short for non-failing malloc).

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