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

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

Fix ipc_wait_for_call() not to take the TASK spinlock while holding the
answerbox spinlock. Also disable interrutps when holding the TASK spinlock.

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