source: mainline/kernel/generic/src/ipc/ipc.c@ 4ca28512

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

Remove ipc_call_static_init() and IPC_CALL_STATIC_ALLOC.

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