source: mainline/kernel/generic/src/ipc/ipc.c@ 6c34f587

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

Remove synchronous IPC primitives.

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