source: mainline/kernel/generic/src/ipc/ipc.c@ 8f29e336

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

Revive kernel notifications.

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