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

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

Do not deadlock when the task cleans up messages it sent itself.

  • Property mode set to 100644
File size: 20.2 KB
RevLine 
[6d9c49a]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[6d9c49a]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
[cc73a8a1]29/** @addtogroup genericipc
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[6d9c49a]35/* Lock ordering
36 *
[8b243f2]37 * First the answerbox, then the phone.
[6d9c49a]38 */
39
[ff48a15]40#include <synch/synch.h>
[2ba7810]41#include <synch/spinlock.h>
[ff48a15]42#include <synch/mutex.h>
[2ba7810]43#include <synch/waitq.h>
[bd5a663]44#include <synch/synch.h>
[6d9c49a]45#include <ipc/ipc.h>
[e028660]46#include <ipc/kbox.h>
[13a638d]47#include <ipc/event.h>
[6d9c49a]48#include <errno.h>
49#include <mm/slab.h>
50#include <arch.h>
51#include <proc/task.h>
52#include <memstr.h>
53#include <debug.h>
54#include <print.h>
[9a1b20c]55#include <console/console.h>
[6d9c49a]56#include <proc/thread.h>
[5626277]57#include <arch/interrupt.h>
[162f919]58#include <ipc/irq.h>
[6d9c49a]59
[8b243f2]60/** Open channel that is assigned automatically to new tasks */
[e74cb73]61answerbox_t *ipc_phone_0 = NULL;
[6d9c49a]62
63static slab_cache_t *ipc_call_slab;
[c70ce74]64static slab_cache_t *ipc_answerbox_slab;
[6d9c49a]65
[8b243f2]66/** Initialize a call structure.
67 *
68 * @param call Call structure to be initialized.
69 */
[ba81cab]70static void _ipc_call_init(call_t *call)
71{
[e32e092]72 memsetb(call, sizeof(*call), 0);
[ba81cab]73 call->callerbox = &TASK->answerbox;
74 call->sender = TASK;
[7918fce]75 call->buffer = NULL;
[ba81cab]76}
77
[8b243f2]78/** Allocate and initialize a call structure.
[6d9c49a]79 *
[8b243f2]80 * The call is initialized, so that the reply will be directed to
81 * TASK->answerbox.
82 *
83 * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
[5626277]84 *
[8b243f2]85 * @return If flags permit it, return NULL, or initialized kernel
86 * call structure.
[6d9c49a]87 */
[8b243f2]88call_t *ipc_call_alloc(int flags)
[6d9c49a]89{
90 call_t *call;
91
[5626277]92 call = slab_alloc(ipc_call_slab, flags);
[69eac4aa]93 if (call)
94 _ipc_call_init(call);
[6d9c49a]95
96 return call;
97}
98
[bd72c3e9]99/** Deallocate a call structure.
[8b243f2]100 *
101 * @param call Call structure to be freed.
102 */
[6d9c49a]103void ipc_call_free(call_t *call)
104{
[7918fce]105 /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */
106 if (call->buffer)
107 free(call->buffer);
[6d9c49a]108 slab_free(ipc_call_slab, call);
109}
110
[8b243f2]111/** Initialize an answerbox structure.
112 *
113 * @param box Answerbox structure to be initialized.
[12ab886]114 * @param task Task to which the answerbox belongs.
[6d9c49a]115 */
[12ab886]116void ipc_answerbox_init(answerbox_t *box, task_t *task)
[6d9c49a]117{
[2ba7810]118 spinlock_initialize(&box->lock, "ipc_box_lock");
[5626277]119 spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
[2ba7810]120 waitq_initialize(&box->wq);
[33adc6ce]121 link_initialize(&box->sync_box_link);
[6d9c49a]122 list_initialize(&box->connected_phones);
123 list_initialize(&box->calls);
124 list_initialize(&box->dispatched_calls);
125 list_initialize(&box->answers);
[5626277]126 list_initialize(&box->irq_notifs);
[b14e35f2]127 list_initialize(&box->irq_head);
[12ab886]128 box->task = task;
[6d9c49a]129}
130
[8b243f2]131/** Connect a phone to an answerbox.
132 *
133 * @param phone Initialized phone structure.
134 * @param box Initialized answerbox structure.
135 */
[2ba7810]136void ipc_phone_connect(phone_t *phone, answerbox_t *box)
[6d9c49a]137{
[ff48a15]138 mutex_lock(&phone->lock);
[ba81cab]139
[eb3d379]140 phone->state = IPC_PHONE_CONNECTED;
[6d9c49a]141 phone->callee = box;
[e74cb73]142
[2ba7810]143 spinlock_lock(&box->lock);
[c4e4507]144 list_append(&phone->link, &box->connected_phones);
[2ba7810]145 spinlock_unlock(&box->lock);
[ba81cab]146
[ff48a15]147 mutex_unlock(&phone->lock);
[2ba7810]148}
149
[8b243f2]150/** Initialize a phone structure.
151 *
152 * @param phone Phone structure to be initialized.
[2ba7810]153 */
154void ipc_phone_init(phone_t *phone)
155{
[08a19ba]156 mutex_initialize(&phone->lock, MUTEX_PASSIVE);
[2ba7810]157 phone->callee = NULL;
[eb3d379]158 phone->state = IPC_PHONE_FREE;
[9f22213]159 atomic_set(&phone->active_calls, 0);
[6d9c49a]160}
161
[8b243f2]162/** Helper function to facilitate synchronous calls.
163 *
164 * @param phone Destination kernel phone structure.
165 * @param request Call structure with request.
[79872cd]166 *
167 * @return EOK on success or EINTR if the sleep was interrupted.
[8b243f2]168 */
[79872cd]169int ipc_call_sync(phone_t *phone, call_t *request)
[631ca4d]170{
[c70ce74]171 answerbox_t *sync_box;
[33adc6ce]172 ipl_t ipl;
[631ca4d]173
[c70ce74]174 sync_box = slab_alloc(ipc_answerbox_slab, 0);
175 ipc_answerbox_init(sync_box, TASK);
[631ca4d]176
[33adc6ce]177 /*
178 * Put the answerbox on the TASK's list of synchronous answerboxes so
179 * that it can be cleaned up if the call is interrupted.
180 */
181 ipl = interrupts_disable();
182 spinlock_lock(&TASK->lock);
183 list_append(&sync_box->sync_box_link, &TASK->sync_box_head);
184 spinlock_unlock(&TASK->lock);
185 interrupts_restore(ipl);
186
[8b243f2]187 /* We will receive data in a special box. */
[c70ce74]188 request->callerbox = sync_box;
[631ca4d]189
190 ipc_call(phone, request);
[c70ce74]191 if (!ipc_wait_for_call(sync_box, SYNCH_NO_TIMEOUT,
192 SYNCH_FLAGS_INTERRUPTIBLE)) {
[33adc6ce]193 /* The answerbox and the call will be freed by ipc_cleanup(). */
[79872cd]194 return EINTR;
[c70ce74]195 }
[33adc6ce]196
197 /*
198 * The answer arrived without interruption so we can remove the
199 * answerbox from the TASK's list of synchronous answerboxes.
200 */
201 (void) interrupts_disable();
202 spinlock_lock(&TASK->lock);
203 list_remove(&sync_box->sync_box_link);
204 spinlock_unlock(&TASK->lock);
205 interrupts_restore(ipl);
206
[c70ce74]207 slab_free(ipc_answerbox_slab, sync_box);
[79872cd]208 return EOK;
[631ca4d]209}
[6d9c49a]210
[8b243f2]211/** Answer a message which was not dispatched and is not listed in any queue.
212 *
213 * @param call Call structure to be answered.
[c713aa56]214 * @param selflocked If true, then TASK->answebox is locked.
[ba81cab]215 */
[c713aa56]216static void _ipc_answer_free_call(call_t *call, bool selflocked)
[ba81cab]217{
218 answerbox_t *callerbox = call->callerbox;
[c713aa56]219 bool do_lock = ((!selflocked) || callerbox != (&TASK->answerbox));
[ba81cab]220
221 call->flags |= IPC_CALL_ANSWERED;
222
[74965d2]223 if (call->flags & IPC_CALL_FORWARDED) {
[27526e87]224 if (call->caller_phone) {
[74965d2]225 /* Demasquerade the caller phone. */
[27526e87]226 call->data.phone = call->caller_phone;
[74965d2]227 }
228 }
229
[c713aa56]230 if (do_lock)
231 spinlock_lock(&callerbox->lock);
[c4e4507]232 list_append(&call->link, &callerbox->answers);
[c713aa56]233 if (do_lock)
234 spinlock_unlock(&callerbox->lock);
[5c8ba05]235 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
[ba81cab]236}
237
[8b243f2]238/** Answer a message which is in a callee queue.
[ba81cab]239 *
[8b243f2]240 * @param box Answerbox that is answering the message.
241 * @param call Modified request that is being sent back.
[ba81cab]242 */
243void ipc_answer(answerbox_t *box, call_t *call)
244{
245 /* Remove from active box */
246 spinlock_lock(&box->lock);
[c4e4507]247 list_remove(&call->link);
[ba81cab]248 spinlock_unlock(&box->lock);
249 /* Send back answer */
[c713aa56]250 _ipc_answer_free_call(call, false);
[ba81cab]251}
252
[8b243f2]253/** Simulate sending back a message.
[7c7aae16]254 *
255 * Most errors are better handled by forming a normal backward
256 * message and sending it as a normal answer.
[8b243f2]257 *
258 * @param phone Phone structure the call should appear to come from.
259 * @param call Call structure to be answered.
260 * @param err Return value to be used for the answer.
[7c7aae16]261 */
[7f1c620]262void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
[7c7aae16]263{
264 call->data.phone = phone;
265 atomic_inc(&phone->active_calls);
[eb3d379]266 IPC_SET_RETVAL(call->data, err);
[c713aa56]267 _ipc_answer_free_call(call, false);
[7c7aae16]268}
269
[8b243f2]270/** Unsafe unchecking version of ipc_call.
271 *
272 * @param phone Phone structure the call comes from.
273 * @param box Destination answerbox structure.
[7c5bcc0]274 * @param call Call structure with request.
[8b243f2]275 */
[fbcfd458]276static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
277{
[8b243f2]278 if (!(call->flags & IPC_CALL_FORWARDED)) {
[9f22213]279 atomic_inc(&phone->active_calls);
280 call->data.phone = phone;
281 }
[fbcfd458]282
283 spinlock_lock(&box->lock);
[c4e4507]284 list_append(&call->link, &box->calls);
[fbcfd458]285 spinlock_unlock(&box->lock);
[5c8ba05]286 waitq_wakeup(&box->wq, WAKEUP_FIRST);
[fbcfd458]287}
288
[8b243f2]289/** Send an asynchronous request using a phone to an answerbox.
290 *
291 * @param phone Phone structure the call comes from and which is
292 * connected to the destination answerbox.
293 * @param call Call structure with request.
[6d9c49a]294 *
[8b243f2]295 * @return Return 0 on success, ENOENT on error.
[6d9c49a]296 */
[9f22213]297int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]298{
[ba81cab]299 answerbox_t *box;
[6d9c49a]300
[ff48a15]301 mutex_lock(&phone->lock);
[eb3d379]302 if (phone->state != IPC_PHONE_CONNECTED) {
[ff48a15]303 mutex_unlock(&phone->lock);
[9f22213]304 if (call->flags & IPC_CALL_FORWARDED) {
305 IPC_SET_RETVAL(call->data, EFORWARD);
[c713aa56]306 _ipc_answer_free_call(call, false);
[eb3d379]307 } else {
308 if (phone->state == IPC_PHONE_HUNGUP)
[7c7aae16]309 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]310 else
[7c7aae16]311 ipc_backsend_err(phone, call, ENOENT);
[9f22213]312 }
313 return ENOENT;
[ba81cab]314 }
[eb3d379]315 box = phone->callee;
[fbcfd458]316 _ipc_call(phone, box, call);
317
[ff48a15]318 mutex_unlock(&phone->lock);
[9f22213]319 return 0;
[fbcfd458]320}
321
[8b243f2]322/** Disconnect phone from answerbox.
[fbcfd458]323 *
[8b243f2]324 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
[eb3d379]325 * lazily later.
[fbcfd458]326 *
[8b243f2]327 * @param phone Phone structure to be hung up.
[eb3d379]328 *
[8b243f2]329 * @return Return 0 if the phone is disconnected.
330 * Return -1 if the phone was already disconnected.
[fbcfd458]331 */
[d8f7362]332int ipc_phone_hangup(phone_t *phone)
[fbcfd458]333{
334 answerbox_t *box;
335 call_t *call;
336
[ff48a15]337 mutex_lock(&phone->lock);
[7918fce]338 if (phone->state == IPC_PHONE_FREE ||
339 phone->state == IPC_PHONE_HUNGUP ||
[8b243f2]340 phone->state == IPC_PHONE_CONNECTING) {
[ff48a15]341 mutex_unlock(&phone->lock);
[eb3d379]342 return -1;
[fbcfd458]343 }
[eb3d379]344 box = phone->callee;
345 if (phone->state != IPC_PHONE_SLAMMED) {
346 /* Remove myself from answerbox */
347 spinlock_lock(&box->lock);
[c4e4507]348 list_remove(&phone->link);
[eb3d379]349 spinlock_unlock(&box->lock);
[6d9c49a]350
[287e83f]351 call = ipc_call_alloc(0);
352 IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
353 call->flags |= IPC_CALL_DISCARD_ANSWER;
354 _ipc_call(phone, box, call);
[eb3d379]355 }
[fbcfd458]356
[eb3d379]357 phone->state = IPC_PHONE_HUNGUP;
[ff48a15]358 mutex_unlock(&phone->lock);
[fbcfd458]359
360 return 0;
[2ba7810]361}
362
[8b243f2]363/** Forwards call from one answerbox to another one.
[2ba7810]364 *
[8b243f2]365 * @param call Call structure to be redirected.
366 * @param newphone Phone structure to target answerbox.
367 * @param oldbox Old answerbox structure.
[d40a8ff]368 * @param mode Flags that specify mode of the forward operation.
[8b243f2]369 *
370 * @return Return 0 if forwarding succeeded or an error code if
371 * there was error.
[9f22213]372 *
[8b243f2]373 * The return value serves only as an information for the forwarder,
374 * the original caller is notified automatically with EFORWARD.
[2ba7810]375 */
[d40a8ff]376int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode)
[2ba7810]377{
378 spinlock_lock(&oldbox->lock);
[c4e4507]379 list_remove(&call->link);
[2ba7810]380 spinlock_unlock(&oldbox->lock);
381
[645d9ed2]382 if (mode & IPC_FF_ROUTE_FROM_ME) {
[27526e87]383 if (!call->caller_phone)
384 call->caller_phone = call->data.phone;
[9201f47]385 call->data.phone = newphone;
[645d9ed2]386 }
[9201f47]387
[9f22213]388 return ipc_call(newphone, call);
[6d9c49a]389}
390
391
[8b243f2]392/** Wait for a phone call.
[6d9c49a]393 *
[8b243f2]394 * @param box Answerbox expecting the call.
395 * @param usec Timeout in microseconds. See documentation for
396 * waitq_sleep_timeout() for decription of its special
397 * meaning.
398 * @param flags Select mode of sleep operation. See documentation for
399 * waitq_sleep_timeout() for description of its special
400 * meaning.
401 * @return Recived call structure or NULL.
402 *
403 * To distinguish between a call and an answer, have a look at call->flags.
[6d9c49a]404 */
[8b243f2]405call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
[6d9c49a]406{
407 call_t *request;
[5626277]408 ipl_t ipl;
[bd5a663]409 int rc;
[6d9c49a]410
[bd5a663]411restart:
[116d1ef4]412 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]413 if (SYNCH_FAILED(rc))
414 return NULL;
[fbcfd458]415
[2ba7810]416 spinlock_lock(&box->lock);
[5626277]417 if (!list_empty(&box->irq_notifs)) {
418 ipl = interrupts_disable();
419 spinlock_lock(&box->irq_lock);
420
[c4e4507]421 request = list_get_instance(box->irq_notifs.next, call_t, link);
422 list_remove(&request->link);
[5626277]423
424 spinlock_unlock(&box->irq_lock);
425 interrupts_restore(ipl);
426 } else if (!list_empty(&box->answers)) {
[fbcfd458]427 /* Handle asynchronous answers */
[c4e4507]428 request = list_get_instance(box->answers.next, call_t, link);
429 list_remove(&request->link);
[74965d2]430 atomic_dec(&request->data.phone->active_calls);
[fbcfd458]431 } else if (!list_empty(&box->calls)) {
432 /* Handle requests */
[c4e4507]433 request = list_get_instance(box->calls.next, call_t, link);
434 list_remove(&request->link);
[fbcfd458]435 /* Append request to dispatch queue */
[c4e4507]436 list_append(&request->link, &box->dispatched_calls);
[fbcfd458]437 } else {
[874621f]438 /* This can happen regularly after ipc_cleanup */
[fbcfd458]439 spinlock_unlock(&box->lock);
440 goto restart;
[6d9c49a]441 }
[2ba7810]442 spinlock_unlock(&box->lock);
[6d9c49a]443 return request;
444}
445
[8b243f2]446/** Answer all calls from list with EHANGUP answer.
447 *
448 * @param lst Head of the list to be cleaned up.
449 */
[9a1b20c]450void ipc_cleanup_call_list(link_t *lst)
[ca687ad]451{
452 call_t *call;
453
454 while (!list_empty(lst)) {
[c4e4507]455 call = list_get_instance(lst->next, call_t, link);
[a55d5f9f]456 if (call->buffer)
457 free(call->buffer);
[c4e4507]458 list_remove(&call->link);
[ca687ad]459
460 IPC_SET_RETVAL(call->data, EHANGUP);
[c713aa56]461 _ipc_answer_free_call(call, true);
[ca687ad]462 }
463}
464
[9a1b20c]465/** Disconnects all phones connected to an answerbox.
[4e49572]466 *
[9a1b20c]467 * @param box Answerbox to disconnect phones from.
468 * @param notify_box If true, the answerbox will get a hangup message for
469 * each disconnected phone.
[4e49572]470 */
[9a1b20c]471void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
[4e49572]472{
[ca687ad]473 phone_t *phone;
[31d8e10]474 DEADLOCK_PROBE_INIT(p_phonelck);
[9a1b20c]475 ipl_t ipl;
476 call_t *call;
[214c5a0]477
[9a1b20c]478 call = notify_box ? ipc_call_alloc(0) : NULL;
[5626277]479
[ca687ad]480 /* Disconnect all phones connected to our answerbox */
481restart_phones:
[9a1b20c]482 ipl = interrupts_disable();
483 spinlock_lock(&box->lock);
484 while (!list_empty(&box->connected_phones)) {
485 phone = list_get_instance(box->connected_phones.next,
[31d8e10]486 phone_t, link);
[ff48a15]487 if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
[9a1b20c]488 spinlock_unlock(&box->lock);
489 interrupts_restore(ipl);
[31d8e10]490 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
[ca687ad]491 goto restart_phones;
492 }
493
494 /* Disconnect phone */
[eb3d379]495 ASSERT(phone->state == IPC_PHONE_CONNECTED);
[9a1b20c]496
[c4e4507]497 list_remove(&phone->link);
[9a1b20c]498 phone->state = IPC_PHONE_SLAMMED;
499
500 if (notify_box) {
501 mutex_unlock(&phone->lock);
502 spinlock_unlock(&box->lock);
503 interrupts_restore(ipl);
504
505 /*
506 * Send one message to the answerbox for each
507 * phone. Used to make sure the kbox thread
508 * wakes up after the last phone has been
509 * disconnected.
510 */
511 IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
512 call->flags |= IPC_CALL_DISCARD_ANSWER;
513 _ipc_call(phone, box, call);
514
515 /* Allocate another call in advance */
516 call = ipc_call_alloc(0);
517
518 /* Must start again */
519 goto restart_phones;
520 }
[ca687ad]521
[ff48a15]522 mutex_unlock(&phone->lock);
[ca687ad]523 }
[9f22213]524
[9a1b20c]525 spinlock_unlock(&box->lock);
526 interrupts_restore(ipl);
527
528 /* Free unused call */
[119c335]529 if (call)
530 ipc_call_free(call);
[9a1b20c]531}
532
533/** Cleans up all IPC communication of the current task.
534 *
535 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
536 * have to change it as well if you want to cleanup other tasks than TASK.
537 */
538void ipc_cleanup(void)
539{
540 int i;
541 call_t *call;
[33adc6ce]542 ipl_t ipl;
[9a1b20c]543
544 /* Disconnect all our phones ('ipc_phone_hangup') */
545 for (i = 0; i < IPC_MAX_PHONES; i++)
546 ipc_phone_hangup(&TASK->phones[i]);
547
[05641a9e]548 /* Unsubscribe from any event notifications. */
549 event_cleanup_answerbox(&TASK->answerbox);
550
[9a1b20c]551 /* Disconnect all connected irqs */
552 ipc_irq_cleanup(&TASK->answerbox);
553
554 /* Disconnect all phones connected to our regular answerbox */
555 ipc_answerbox_slam_phones(&TASK->answerbox, false);
556
557#ifdef CONFIG_UDEBUG
558 /* Clean up kbox thread and communications */
559 ipc_kbox_cleanup();
560#endif
561
[9f22213]562 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[9a1b20c]563 spinlock_lock(&TASK->answerbox.lock);
[214c5a0]564 ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
565 ipc_cleanup_call_list(&TASK->answerbox.calls);
566 spinlock_unlock(&TASK->answerbox.lock);
[4e49572]567
[33adc6ce]568 /* Wait for all answers to interrupted synchronous calls to arrive */
569 ipl = interrupts_disable();
570 while (!list_empty(&TASK->sync_box_head)) {
571 answerbox_t *box = list_get_instance(TASK->sync_box_head.next,
572 answerbox_t, sync_box_link);
573
574 list_remove(&box->sync_box_link);
575 call = ipc_wait_for_call(box, SYNCH_NO_TIMEOUT,
576 SYNCH_FLAGS_NONE);
577 ipc_call_free(call);
578 slab_free(ipc_answerbox_slab, box);
579 }
580 interrupts_restore(ipl);
581
582 /* Wait for all answers to asynchronous calls to arrive */
[eb3d379]583 while (1) {
584 /* Go through all phones, until all are FREE... */
585 /* Locking not needed, no one else should modify
586 * it, when we are in cleanup */
[8b243f2]587 for (i = 0; i < IPC_MAX_PHONES; i++) {
588 if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
[e701eb1]589 atomic_get(&TASK->phones[i].active_calls) == 0) {
[214c5a0]590 TASK->phones[i].state = IPC_PHONE_FREE;
[e701eb1]591 TASK->phones[i].callee = NULL;
592 }
[214c5a0]593
[d8f7362]594 /* Just for sure, we might have had some
595 * IPC_PHONE_CONNECTING phones */
[214c5a0]596 if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
[d8f7362]597 ipc_phone_hangup(&TASK->phones[i]);
598 /* If the hangup succeeded, it has sent a HANGUP
599 * message, the IPC is now in HUNGUP state, we
600 * wait for the reply to come */
[214c5a0]601
602 if (TASK->phones[i].state != IPC_PHONE_FREE)
[eb3d379]603 break;
604 }
605 /* Voila, got into cleanup */
606 if (i == IPC_MAX_PHONES)
607 break;
608
[8b243f2]609 call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
610 SYNCH_FLAGS_NONE);
611 ASSERT((call->flags & IPC_CALL_ANSWERED) ||
612 (call->flags & IPC_CALL_NOTIF));
[ca687ad]613
[119c335]614 /*
615 * Record the receipt of this call in the current task's counter
616 * of active calls. IPC_M_PHONE_HUNGUP calls do not contribute
617 * to this counter so do not record answers to them either.
618 */
[4e5aa02]619 if (!(call->flags & IPC_CALL_DISCARD_ANSWER))
620 atomic_dec(&TASK->active_calls);
[ca687ad]621 ipc_call_free(call);
622 }
[4e49572]623}
[5626277]624
625
[2b017ba]626/** Initilize IPC subsystem */
[5626277]627void ipc_init(void)
628{
[8b243f2]629 ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
630 NULL, 0);
[c70ce74]631 ipc_answerbox_slab = slab_cache_create("ipc_answerbox",
632 sizeof(answerbox_t), 0, NULL, NULL, 0);
[5626277]633}
634
[c4e4507]635
[8b243f2]636/** List answerbox contents.
637 *
638 * @param taskid Task ID.
639 */
[c4e4507]640void ipc_print_task(task_id_t taskid)
641{
642 task_t *task;
643 int i;
644 call_t *call;
645 link_t *tmp;
646
647 spinlock_lock(&tasks_lock);
648 task = task_find_by_id(taskid);
649 if (task)
650 spinlock_lock(&task->lock);
651 spinlock_unlock(&tasks_lock);
652 if (!task)
653 return;
654
655 /* Print opened phones & details */
656 printf("PHONE:\n");
[8b243f2]657 for (i = 0; i < IPC_MAX_PHONES; i++) {
[ff48a15]658 if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
659 printf("%d: mutex busy\n", i);
660 continue;
661 }
[c4e4507]662 if (task->phones[i].state != IPC_PHONE_FREE) {
[8b243f2]663 printf("%d: ", i);
[c4e4507]664 switch (task->phones[i].state) {
665 case IPC_PHONE_CONNECTING:
666 printf("connecting ");
667 break;
668 case IPC_PHONE_CONNECTED:
[fbf7b4c]669 printf("connected to: %p ",
[c4e4507]670 task->phones[i].callee);
671 break;
672 case IPC_PHONE_SLAMMED:
[fbf7b4c]673 printf("slammed by: %p ",
[c4e4507]674 task->phones[i].callee);
675 break;
676 case IPC_PHONE_HUNGUP:
[fbf7b4c]677 printf("hung up - was: %p ",
[c4e4507]678 task->phones[i].callee);
679 break;
680 default:
681 break;
682 }
[9fe962d6]683 printf("active: %ld\n",
[bd72c3e9]684 atomic_get(&task->phones[i].active_calls));
[c4e4507]685 }
[ff48a15]686 mutex_unlock(&task->phones[i].lock);
[c4e4507]687 }
688
689
690 /* Print answerbox - calls */
691 spinlock_lock(&task->answerbox.lock);
692 printf("ABOX - CALLS:\n");
[bd72c3e9]693 for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls;
[78d0da8]694 tmp = tmp->next) {
[c4e4507]695 call = list_get_instance(tmp, call_t, link);
[9fe962d6]696 printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
[e40e3007]697 " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
698 " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
699 call->sender->taskid,
[bd72c3e9]700 IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
701 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]702 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]703 call->flags);
[c4e4507]704 }
705 /* Print answerbox - calls */
706 printf("ABOX - DISPATCHED CALLS:\n");
[9fe962d6]707 for (tmp = task->answerbox.dispatched_calls.next;
[78d0da8]708 tmp != &task->answerbox.dispatched_calls;
709 tmp = tmp->next) {
[c4e4507]710 call = list_get_instance(tmp, call_t, link);
[9fe962d6]711 printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
[e40e3007]712 " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
713 " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
714 call->sender->taskid,
[bd72c3e9]715 IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
716 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]717 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]718 call->flags);
[c4e4507]719 }
720 /* Print answerbox - calls */
721 printf("ABOX - ANSWERS:\n");
[e40e3007]722 for (tmp = task->answerbox.answers.next;
723 tmp != &task->answerbox.answers;
[78d0da8]724 tmp = tmp->next) {
[c4e4507]725 call = list_get_instance(tmp, call_t, link);
[9fe962d6]726 printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun
[e40e3007]727 " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n",
[38c706cc]728 call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]729 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]730 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]731 call->flags);
[c4e4507]732 }
733
734 spinlock_unlock(&task->answerbox.lock);
735 spinlock_unlock(&task->lock);
736}
[b45c443]737
[cc73a8a1]738/** @}
[b45c443]739 */
Note: See TracBrowser for help on using the repository browser.