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

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

Allocate the answerbox for synchronous calls dynamically.

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