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

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

Partially revert jakub@…

This changeset reintroduces portions of the synchronous IPC mechanism for
internal use by the kernel / user_backend. The synchronous IPC is not
exported to user space and is simpler than in its original form.

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