source: mainline/kernel/generic/src/ipc/ipc.c@ 33adc6ce

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

Introduce the per-task list of active synchronous answerboxes and make
ipc_cleanup() aware of it.

  • Property mode set to 100644
File size: 19.9 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.
[ba81cab]214 */
215static void _ipc_answer_free_call(call_t *call)
216{
217 answerbox_t *callerbox = call->callerbox;
218
219 call->flags |= IPC_CALL_ANSWERED;
220
[74965d2]221 if (call->flags & IPC_CALL_FORWARDED) {
[27526e87]222 if (call->caller_phone) {
[74965d2]223 /* Demasquerade the caller phone. */
[27526e87]224 call->data.phone = call->caller_phone;
[74965d2]225 }
226 }
227
[ba81cab]228 spinlock_lock(&callerbox->lock);
[c4e4507]229 list_append(&call->link, &callerbox->answers);
[ba81cab]230 spinlock_unlock(&callerbox->lock);
[5c8ba05]231 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
[ba81cab]232}
233
[8b243f2]234/** Answer a message which is in a callee queue.
[ba81cab]235 *
[8b243f2]236 * @param box Answerbox that is answering the message.
237 * @param call Modified request that is being sent back.
[ba81cab]238 */
239void ipc_answer(answerbox_t *box, call_t *call)
240{
241 /* Remove from active box */
242 spinlock_lock(&box->lock);
[c4e4507]243 list_remove(&call->link);
[ba81cab]244 spinlock_unlock(&box->lock);
245 /* Send back answer */
246 _ipc_answer_free_call(call);
247}
248
[8b243f2]249/** Simulate sending back a message.
[7c7aae16]250 *
251 * Most errors are better handled by forming a normal backward
252 * message and sending it as a normal answer.
[8b243f2]253 *
254 * @param phone Phone structure the call should appear to come from.
255 * @param call Call structure to be answered.
256 * @param err Return value to be used for the answer.
[7c7aae16]257 */
[7f1c620]258void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
[7c7aae16]259{
260 call->data.phone = phone;
261 atomic_inc(&phone->active_calls);
[eb3d379]262 IPC_SET_RETVAL(call->data, err);
[7c7aae16]263 _ipc_answer_free_call(call);
264}
265
[8b243f2]266/** Unsafe unchecking version of ipc_call.
267 *
268 * @param phone Phone structure the call comes from.
269 * @param box Destination answerbox structure.
[7c5bcc0]270 * @param call Call structure with request.
[8b243f2]271 */
[fbcfd458]272static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
273{
[8b243f2]274 if (!(call->flags & IPC_CALL_FORWARDED)) {
[9f22213]275 atomic_inc(&phone->active_calls);
276 call->data.phone = phone;
277 }
[fbcfd458]278
279 spinlock_lock(&box->lock);
[c4e4507]280 list_append(&call->link, &box->calls);
[fbcfd458]281 spinlock_unlock(&box->lock);
[5c8ba05]282 waitq_wakeup(&box->wq, WAKEUP_FIRST);
[fbcfd458]283}
284
[8b243f2]285/** Send an asynchronous request using a phone to an answerbox.
286 *
287 * @param phone Phone structure the call comes from and which is
288 * connected to the destination answerbox.
289 * @param call Call structure with request.
[6d9c49a]290 *
[8b243f2]291 * @return Return 0 on success, ENOENT on error.
[6d9c49a]292 */
[9f22213]293int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]294{
[ba81cab]295 answerbox_t *box;
[6d9c49a]296
[ff48a15]297 mutex_lock(&phone->lock);
[eb3d379]298 if (phone->state != IPC_PHONE_CONNECTED) {
[ff48a15]299 mutex_unlock(&phone->lock);
[9f22213]300 if (call->flags & IPC_CALL_FORWARDED) {
301 IPC_SET_RETVAL(call->data, EFORWARD);
[7c7aae16]302 _ipc_answer_free_call(call);
[eb3d379]303 } else {
304 if (phone->state == IPC_PHONE_HUNGUP)
[7c7aae16]305 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]306 else
[7c7aae16]307 ipc_backsend_err(phone, call, ENOENT);
[9f22213]308 }
309 return ENOENT;
[ba81cab]310 }
[eb3d379]311 box = phone->callee;
[fbcfd458]312 _ipc_call(phone, box, call);
313
[ff48a15]314 mutex_unlock(&phone->lock);
[9f22213]315 return 0;
[fbcfd458]316}
317
[8b243f2]318/** Disconnect phone from answerbox.
[fbcfd458]319 *
[8b243f2]320 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
[eb3d379]321 * lazily later.
[fbcfd458]322 *
[8b243f2]323 * @param phone Phone structure to be hung up.
[eb3d379]324 *
[8b243f2]325 * @return Return 0 if the phone is disconnected.
326 * Return -1 if the phone was already disconnected.
[fbcfd458]327 */
[d8f7362]328int ipc_phone_hangup(phone_t *phone)
[fbcfd458]329{
330 answerbox_t *box;
331 call_t *call;
332
[ff48a15]333 mutex_lock(&phone->lock);
[7918fce]334 if (phone->state == IPC_PHONE_FREE ||
335 phone->state == IPC_PHONE_HUNGUP ||
[8b243f2]336 phone->state == IPC_PHONE_CONNECTING) {
[ff48a15]337 mutex_unlock(&phone->lock);
[eb3d379]338 return -1;
[fbcfd458]339 }
[eb3d379]340 box = phone->callee;
341 if (phone->state != IPC_PHONE_SLAMMED) {
342 /* Remove myself from answerbox */
343 spinlock_lock(&box->lock);
[c4e4507]344 list_remove(&phone->link);
[eb3d379]345 spinlock_unlock(&box->lock);
[6d9c49a]346
[287e83f]347 call = ipc_call_alloc(0);
348 IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
349 call->flags |= IPC_CALL_DISCARD_ANSWER;
350 _ipc_call(phone, box, call);
[eb3d379]351 }
[fbcfd458]352
[eb3d379]353 phone->state = IPC_PHONE_HUNGUP;
[ff48a15]354 mutex_unlock(&phone->lock);
[fbcfd458]355
356 return 0;
[2ba7810]357}
358
[8b243f2]359/** Forwards call from one answerbox to another one.
[2ba7810]360 *
[8b243f2]361 * @param call Call structure to be redirected.
362 * @param newphone Phone structure to target answerbox.
363 * @param oldbox Old answerbox structure.
[d40a8ff]364 * @param mode Flags that specify mode of the forward operation.
[8b243f2]365 *
366 * @return Return 0 if forwarding succeeded or an error code if
367 * there was error.
[9f22213]368 *
[8b243f2]369 * The return value serves only as an information for the forwarder,
370 * the original caller is notified automatically with EFORWARD.
[2ba7810]371 */
[d40a8ff]372int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode)
[2ba7810]373{
374 spinlock_lock(&oldbox->lock);
[c4e4507]375 list_remove(&call->link);
[2ba7810]376 spinlock_unlock(&oldbox->lock);
377
[645d9ed2]378 if (mode & IPC_FF_ROUTE_FROM_ME) {
[27526e87]379 if (!call->caller_phone)
380 call->caller_phone = call->data.phone;
[9201f47]381 call->data.phone = newphone;
[645d9ed2]382 }
[9201f47]383
[9f22213]384 return ipc_call(newphone, call);
[6d9c49a]385}
386
387
[8b243f2]388/** Wait for a phone call.
[6d9c49a]389 *
[8b243f2]390 * @param box Answerbox expecting the call.
391 * @param usec Timeout in microseconds. See documentation for
392 * waitq_sleep_timeout() for decription of its special
393 * meaning.
394 * @param flags Select mode of sleep operation. See documentation for
395 * waitq_sleep_timeout() for description of its special
396 * meaning.
397 * @return Recived call structure or NULL.
398 *
399 * To distinguish between a call and an answer, have a look at call->flags.
[6d9c49a]400 */
[8b243f2]401call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
[6d9c49a]402{
403 call_t *request;
[5626277]404 ipl_t ipl;
[bd5a663]405 int rc;
[6d9c49a]406
[bd5a663]407restart:
[116d1ef4]408 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]409 if (SYNCH_FAILED(rc))
410 return NULL;
[fbcfd458]411
[2ba7810]412 spinlock_lock(&box->lock);
[5626277]413 if (!list_empty(&box->irq_notifs)) {
414 ipl = interrupts_disable();
415 spinlock_lock(&box->irq_lock);
416
[c4e4507]417 request = list_get_instance(box->irq_notifs.next, call_t, link);
418 list_remove(&request->link);
[5626277]419
420 spinlock_unlock(&box->irq_lock);
421 interrupts_restore(ipl);
422 } else if (!list_empty(&box->answers)) {
[fbcfd458]423 /* Handle asynchronous answers */
[c4e4507]424 request = list_get_instance(box->answers.next, call_t, link);
425 list_remove(&request->link);
[74965d2]426 atomic_dec(&request->data.phone->active_calls);
[fbcfd458]427 } else if (!list_empty(&box->calls)) {
428 /* Handle requests */
[c4e4507]429 request = list_get_instance(box->calls.next, call_t, link);
430 list_remove(&request->link);
[fbcfd458]431 /* Append request to dispatch queue */
[c4e4507]432 list_append(&request->link, &box->dispatched_calls);
[fbcfd458]433 } else {
[874621f]434 /* This can happen regularly after ipc_cleanup */
[fbcfd458]435 spinlock_unlock(&box->lock);
436 goto restart;
[6d9c49a]437 }
[2ba7810]438 spinlock_unlock(&box->lock);
[6d9c49a]439 return request;
440}
441
[8b243f2]442/** Answer all calls from list with EHANGUP answer.
443 *
444 * @param lst Head of the list to be cleaned up.
445 */
[9a1b20c]446void ipc_cleanup_call_list(link_t *lst)
[ca687ad]447{
448 call_t *call;
449
450 while (!list_empty(lst)) {
[c4e4507]451 call = list_get_instance(lst->next, call_t, link);
[a55d5f9f]452 if (call->buffer)
453 free(call->buffer);
[c4e4507]454 list_remove(&call->link);
[ca687ad]455
456 IPC_SET_RETVAL(call->data, EHANGUP);
457 _ipc_answer_free_call(call);
458 }
459}
460
[9a1b20c]461/** Disconnects all phones connected to an answerbox.
[4e49572]462 *
[9a1b20c]463 * @param box Answerbox to disconnect phones from.
464 * @param notify_box If true, the answerbox will get a hangup message for
465 * each disconnected phone.
[4e49572]466 */
[9a1b20c]467void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
[4e49572]468{
[ca687ad]469 phone_t *phone;
[31d8e10]470 DEADLOCK_PROBE_INIT(p_phonelck);
[9a1b20c]471 ipl_t ipl;
472 call_t *call;
[214c5a0]473
[9a1b20c]474 call = notify_box ? ipc_call_alloc(0) : NULL;
[5626277]475
[ca687ad]476 /* Disconnect all phones connected to our answerbox */
477restart_phones:
[9a1b20c]478 ipl = interrupts_disable();
479 spinlock_lock(&box->lock);
480 while (!list_empty(&box->connected_phones)) {
481 phone = list_get_instance(box->connected_phones.next,
[31d8e10]482 phone_t, link);
[ff48a15]483 if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
[9a1b20c]484 spinlock_unlock(&box->lock);
485 interrupts_restore(ipl);
[31d8e10]486 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
[ca687ad]487 goto restart_phones;
488 }
489
490 /* Disconnect phone */
[eb3d379]491 ASSERT(phone->state == IPC_PHONE_CONNECTED);
[9a1b20c]492
[c4e4507]493 list_remove(&phone->link);
[9a1b20c]494 phone->state = IPC_PHONE_SLAMMED;
495
496 if (notify_box) {
497 mutex_unlock(&phone->lock);
498 spinlock_unlock(&box->lock);
499 interrupts_restore(ipl);
500
501 /*
502 * Send one message to the answerbox for each
503 * phone. Used to make sure the kbox thread
504 * wakes up after the last phone has been
505 * disconnected.
506 */
507 IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
508 call->flags |= IPC_CALL_DISCARD_ANSWER;
509 _ipc_call(phone, box, call);
510
511 /* Allocate another call in advance */
512 call = ipc_call_alloc(0);
513
514 /* Must start again */
515 goto restart_phones;
516 }
[ca687ad]517
[ff48a15]518 mutex_unlock(&phone->lock);
[ca687ad]519 }
[9f22213]520
[9a1b20c]521 spinlock_unlock(&box->lock);
522 interrupts_restore(ipl);
523
524 /* Free unused call */
[119c335]525 if (call)
526 ipc_call_free(call);
[9a1b20c]527}
528
529/** Cleans up all IPC communication of the current task.
530 *
531 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
532 * have to change it as well if you want to cleanup other tasks than TASK.
533 */
534void ipc_cleanup(void)
535{
536 int i;
537 call_t *call;
[33adc6ce]538 ipl_t ipl;
[9a1b20c]539
540 /* Disconnect all our phones ('ipc_phone_hangup') */
541 for (i = 0; i < IPC_MAX_PHONES; i++)
542 ipc_phone_hangup(&TASK->phones[i]);
543
[05641a9e]544 /* Unsubscribe from any event notifications. */
545 event_cleanup_answerbox(&TASK->answerbox);
546
[9a1b20c]547 /* Disconnect all connected irqs */
548 ipc_irq_cleanup(&TASK->answerbox);
549
550 /* Disconnect all phones connected to our regular answerbox */
551 ipc_answerbox_slam_phones(&TASK->answerbox, false);
552
553#ifdef CONFIG_UDEBUG
554 /* Clean up kbox thread and communications */
555 ipc_kbox_cleanup();
556#endif
557
[9f22213]558 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[9a1b20c]559 spinlock_lock(&TASK->answerbox.lock);
[214c5a0]560 ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
561 ipc_cleanup_call_list(&TASK->answerbox.calls);
562 spinlock_unlock(&TASK->answerbox.lock);
[4e49572]563
[33adc6ce]564 /* Wait for all answers to interrupted synchronous calls to arrive */
565 ipl = interrupts_disable();
566 while (!list_empty(&TASK->sync_box_head)) {
567 answerbox_t *box = list_get_instance(TASK->sync_box_head.next,
568 answerbox_t, sync_box_link);
569
570 list_remove(&box->sync_box_link);
571 call = ipc_wait_for_call(box, SYNCH_NO_TIMEOUT,
572 SYNCH_FLAGS_NONE);
573 ipc_call_free(call);
574 slab_free(ipc_answerbox_slab, box);
575 }
576 interrupts_restore(ipl);
577
578 /* Wait for all answers to asynchronous calls to arrive */
[eb3d379]579 while (1) {
580 /* Go through all phones, until all are FREE... */
581 /* Locking not needed, no one else should modify
582 * it, when we are in cleanup */
[8b243f2]583 for (i = 0; i < IPC_MAX_PHONES; i++) {
584 if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
[214c5a0]585 atomic_get(&TASK->phones[i].active_calls) == 0)
586 TASK->phones[i].state = IPC_PHONE_FREE;
587
[d8f7362]588 /* Just for sure, we might have had some
589 * IPC_PHONE_CONNECTING phones */
[214c5a0]590 if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
[d8f7362]591 ipc_phone_hangup(&TASK->phones[i]);
592 /* If the hangup succeeded, it has sent a HANGUP
593 * message, the IPC is now in HUNGUP state, we
594 * wait for the reply to come */
[214c5a0]595
596 if (TASK->phones[i].state != IPC_PHONE_FREE)
[eb3d379]597 break;
598 }
599 /* Voila, got into cleanup */
600 if (i == IPC_MAX_PHONES)
601 break;
602
[8b243f2]603 call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
604 SYNCH_FLAGS_NONE);
605 ASSERT((call->flags & IPC_CALL_ANSWERED) ||
606 (call->flags & IPC_CALL_NOTIF));
[ca687ad]607
[119c335]608 /*
609 * Record the receipt of this call in the current task's counter
610 * of active calls. IPC_M_PHONE_HUNGUP calls do not contribute
611 * to this counter so do not record answers to them either.
612 */
[4e5aa02]613 if (!(call->flags & IPC_CALL_DISCARD_ANSWER))
614 atomic_dec(&TASK->active_calls);
[ca687ad]615 ipc_call_free(call);
616 }
[4e49572]617}
[5626277]618
619
[2b017ba]620/** Initilize IPC subsystem */
[5626277]621void ipc_init(void)
622{
[8b243f2]623 ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
624 NULL, 0);
[c70ce74]625 ipc_answerbox_slab = slab_cache_create("ipc_answerbox",
626 sizeof(answerbox_t), 0, NULL, NULL, 0);
[5626277]627}
628
[c4e4507]629
[8b243f2]630/** List answerbox contents.
631 *
632 * @param taskid Task ID.
633 */
[c4e4507]634void ipc_print_task(task_id_t taskid)
635{
636 task_t *task;
637 int i;
638 call_t *call;
639 link_t *tmp;
640
641 spinlock_lock(&tasks_lock);
642 task = task_find_by_id(taskid);
643 if (task)
644 spinlock_lock(&task->lock);
645 spinlock_unlock(&tasks_lock);
646 if (!task)
647 return;
648
649 /* Print opened phones & details */
650 printf("PHONE:\n");
[8b243f2]651 for (i = 0; i < IPC_MAX_PHONES; i++) {
[ff48a15]652 if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
653 printf("%d: mutex busy\n", i);
654 continue;
655 }
[c4e4507]656 if (task->phones[i].state != IPC_PHONE_FREE) {
[8b243f2]657 printf("%d: ", i);
[c4e4507]658 switch (task->phones[i].state) {
659 case IPC_PHONE_CONNECTING:
660 printf("connecting ");
661 break;
662 case IPC_PHONE_CONNECTED:
[fbf7b4c]663 printf("connected to: %p ",
[c4e4507]664 task->phones[i].callee);
665 break;
666 case IPC_PHONE_SLAMMED:
[fbf7b4c]667 printf("slammed by: %p ",
[c4e4507]668 task->phones[i].callee);
669 break;
670 case IPC_PHONE_HUNGUP:
[fbf7b4c]671 printf("hung up - was: %p ",
[c4e4507]672 task->phones[i].callee);
673 break;
674 default:
675 break;
676 }
[9fe962d6]677 printf("active: %ld\n",
[bd72c3e9]678 atomic_get(&task->phones[i].active_calls));
[c4e4507]679 }
[ff48a15]680 mutex_unlock(&task->phones[i].lock);
[c4e4507]681 }
682
683
684 /* Print answerbox - calls */
685 spinlock_lock(&task->answerbox.lock);
686 printf("ABOX - CALLS:\n");
[bd72c3e9]687 for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls;
[78d0da8]688 tmp = tmp->next) {
[c4e4507]689 call = list_get_instance(tmp, call_t, link);
[9fe962d6]690 printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
[e40e3007]691 " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
692 " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
693 call->sender->taskid,
[bd72c3e9]694 IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
695 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]696 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]697 call->flags);
[c4e4507]698 }
699 /* Print answerbox - calls */
700 printf("ABOX - DISPATCHED CALLS:\n");
[9fe962d6]701 for (tmp = task->answerbox.dispatched_calls.next;
[78d0da8]702 tmp != &task->answerbox.dispatched_calls;
703 tmp = tmp->next) {
[c4e4507]704 call = list_get_instance(tmp, call_t, link);
[9fe962d6]705 printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
[e40e3007]706 " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
707 " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
708 call->sender->taskid,
[bd72c3e9]709 IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
710 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]711 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]712 call->flags);
[c4e4507]713 }
714 /* Print answerbox - calls */
715 printf("ABOX - ANSWERS:\n");
[e40e3007]716 for (tmp = task->answerbox.answers.next;
717 tmp != &task->answerbox.answers;
[78d0da8]718 tmp = tmp->next) {
[c4e4507]719 call = list_get_instance(tmp, call_t, link);
[9fe962d6]720 printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun
[e40e3007]721 " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n",
[38c706cc]722 call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]723 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]724 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]725 call->flags);
[c4e4507]726 }
727
728 spinlock_unlock(&task->answerbox.lock);
729 spinlock_unlock(&task->lock);
730}
[b45c443]731
[cc73a8a1]732/** @}
[b45c443]733 */
Note: See TracBrowser for help on using the repository browser.