source: mainline/kernel/generic/src/ipc/ipc.c@ 566f4cfb

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 566f4cfb was 13a638d, checked in by Martin Decky <martin@…>, 17 years ago

move event notification to the ipc directory (where it probably belogs to, side-by-side to IRQ notifications)
cleanup the notification code a little bit (there is probably no need to allocate two structured dynamically)

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