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

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

Rename call_t's link to ab_link as this link is exclusively used for
linking the call into one of the answerbox lists.

  • 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->sender = TASK;
[7918fce]74 call->buffer = NULL;
[ba81cab]75}
76
[8b243f2]77/** Allocate and initialize a call structure.
[da1bafb]78 *
[8b243f2]79 * The call is initialized, so that the reply will be directed to
80 * TASK->answerbox.
81 *
[da1bafb]82 * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
83 *
84 * @return If flags permit it, return NULL, or initialized kernel
85 * call structure.
[5626277]86 *
[6d9c49a]87 */
[da1bafb]88call_t *ipc_call_alloc(unsigned int flags)
[6d9c49a]89{
[da1bafb]90 call_t *call = slab_alloc(ipc_call_slab, flags);
[69eac4aa]91 if (call)
92 _ipc_call_init(call);
[da1bafb]93
[6d9c49a]94 return call;
95}
96
[bd72c3e9]97/** Deallocate a call structure.
[8b243f2]98 *
[da1bafb]99 * @param call Call structure to be freed.
100 *
[8b243f2]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 *
[da1bafb]112 * @param box Answerbox structure to be initialized.
113 * @param task Task to which the answerbox belongs.
114 *
[6d9c49a]115 */
[12ab886]116void ipc_answerbox_init(answerbox_t *box, task_t *task)
[6d9c49a]117{
[da1bafb]118 irq_spinlock_initialize(&box->lock, "ipc.box.lock");
119 irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
[2ba7810]120 waitq_initialize(&box->wq);
[6d9c49a]121 list_initialize(&box->connected_phones);
122 list_initialize(&box->calls);
123 list_initialize(&box->dispatched_calls);
124 list_initialize(&box->answers);
[5626277]125 list_initialize(&box->irq_notifs);
[55b77d9]126 list_initialize(&box->irq_list);
[12ab886]127 box->task = task;
[6d9c49a]128}
129
[8b243f2]130/** Connect a phone to an answerbox.
131 *
[da1bafb]132 * @param phone Initialized phone structure.
133 * @param box Initialized answerbox structure.
134 *
[8b243f2]135 */
[2ba7810]136void ipc_phone_connect(phone_t *phone, answerbox_t *box)
[6d9c49a]137{
[ff48a15]138 mutex_lock(&phone->lock);
[da1bafb]139
[eb3d379]140 phone->state = IPC_PHONE_CONNECTED;
[6d9c49a]141 phone->callee = box;
[da1bafb]142
143 irq_spinlock_lock(&box->lock, true);
[c4e4507]144 list_append(&phone->link, &box->connected_phones);
[da1bafb]145 irq_spinlock_unlock(&box->lock, true);
146
[ff48a15]147 mutex_unlock(&phone->lock);
[2ba7810]148}
149
[8b243f2]150/** Initialize a phone structure.
151 *
[da1bafb]152 * @param phone Phone structure to be initialized.
153 *
[2ba7810]154 */
155void ipc_phone_init(phone_t *phone)
156{
[08a19ba]157 mutex_initialize(&phone->lock, MUTEX_PASSIVE);
[2ba7810]158 phone->callee = NULL;
[eb3d379]159 phone->state = IPC_PHONE_FREE;
[9f22213]160 atomic_set(&phone->active_calls, 0);
[6d9c49a]161}
162
[8b243f2]163/** Answer a message which was not dispatched and is not listed in any queue.
164 *
[da1bafb]165 * @param call Call structure to be answered.
166 * @param selflocked If true, then TASK->answebox is locked.
167 *
[ba81cab]168 */
[c713aa56]169static void _ipc_answer_free_call(call_t *call, bool selflocked)
[ba81cab]170{
[cd529c4]171 answerbox_t *callerbox = &call->sender->answerbox;
[c713aa56]172 bool do_lock = ((!selflocked) || callerbox != (&TASK->answerbox));
[da1bafb]173
[95319bd]174 /* Count sent answer */
[da1bafb]175 irq_spinlock_lock(&TASK->lock, true);
[a307beb]176 TASK->ipc_info.answer_sent++;
[da1bafb]177 irq_spinlock_unlock(&TASK->lock, true);
178
[ba81cab]179 call->flags |= IPC_CALL_ANSWERED;
[da1bafb]180
[74965d2]181 if (call->flags & IPC_CALL_FORWARDED) {
[27526e87]182 if (call->caller_phone) {
[74965d2]183 /* Demasquerade the caller phone. */
[27526e87]184 call->data.phone = call->caller_phone;
[74965d2]185 }
186 }
[ab34cc9]187
188 call->data.task_id = TASK->taskid;
[da1bafb]189
[c713aa56]190 if (do_lock)
[da1bafb]191 irq_spinlock_lock(&callerbox->lock, true);
192
[cfaa35a]193 list_append(&call->ab_link, &callerbox->answers);
[da1bafb]194
[c713aa56]195 if (do_lock)
[da1bafb]196 irq_spinlock_unlock(&callerbox->lock, true);
197
[5c8ba05]198 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
[ba81cab]199}
200
[8b243f2]201/** Answer a message which is in a callee queue.
[ba81cab]202 *
[da1bafb]203 * @param box Answerbox that is answering the message.
204 * @param call Modified request that is being sent back.
205 *
[ba81cab]206 */
207void ipc_answer(answerbox_t *box, call_t *call)
208{
209 /* Remove from active box */
[da1bafb]210 irq_spinlock_lock(&box->lock, true);
[cfaa35a]211 list_remove(&call->ab_link);
[da1bafb]212 irq_spinlock_unlock(&box->lock, true);
213
[ba81cab]214 /* Send back answer */
[c713aa56]215 _ipc_answer_free_call(call, false);
[ba81cab]216}
217
[8b243f2]218/** Simulate sending back a message.
[7c7aae16]219 *
220 * Most errors are better handled by forming a normal backward
221 * message and sending it as a normal answer.
[8b243f2]222 *
[da1bafb]223 * @param phone Phone structure the call should appear to come from.
224 * @param call Call structure to be answered.
225 * @param err Return value to be used for the answer.
226 *
[7c7aae16]227 */
[96b02eb9]228void ipc_backsend_err(phone_t *phone, call_t *call, sysarg_t err)
[7c7aae16]229{
230 call->data.phone = phone;
231 atomic_inc(&phone->active_calls);
[eb3d379]232 IPC_SET_RETVAL(call->data, err);
[c713aa56]233 _ipc_answer_free_call(call, false);
[7c7aae16]234}
235
[8b243f2]236/** Unsafe unchecking version of ipc_call.
237 *
[da1bafb]238 * @param phone Phone structure the call comes from.
239 * @param box Destination answerbox structure.
240 * @param call Call structure with request.
241 *
[8b243f2]242 */
[fbcfd458]243static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
244{
[95319bd]245 /* Count sent ipc call */
[da1bafb]246 irq_spinlock_lock(&TASK->lock, true);
[a307beb]247 TASK->ipc_info.call_sent++;
[da1bafb]248 irq_spinlock_unlock(&TASK->lock, true);
249
[8b243f2]250 if (!(call->flags & IPC_CALL_FORWARDED)) {
[9f22213]251 atomic_inc(&phone->active_calls);
252 call->data.phone = phone;
[e2ab36f1]253 call->data.task_id = TASK->taskid;
[9f22213]254 }
[da1bafb]255
256 irq_spinlock_lock(&box->lock, true);
[cfaa35a]257 list_append(&call->ab_link, &box->calls);
[da1bafb]258 irq_spinlock_unlock(&box->lock, true);
259
[5c8ba05]260 waitq_wakeup(&box->wq, WAKEUP_FIRST);
[fbcfd458]261}
262
[8b243f2]263/** Send an asynchronous request using a phone to an answerbox.
264 *
[da1bafb]265 * @param phone Phone structure the call comes from and which is
266 * connected to the destination answerbox.
267 * @param call Call structure with request.
268 *
269 * @return Return 0 on success, ENOENT on error.
[6d9c49a]270 *
271 */
[9f22213]272int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]273{
[ff48a15]274 mutex_lock(&phone->lock);
[eb3d379]275 if (phone->state != IPC_PHONE_CONNECTED) {
[ff48a15]276 mutex_unlock(&phone->lock);
[9f22213]277 if (call->flags & IPC_CALL_FORWARDED) {
278 IPC_SET_RETVAL(call->data, EFORWARD);
[c713aa56]279 _ipc_answer_free_call(call, false);
[eb3d379]280 } else {
281 if (phone->state == IPC_PHONE_HUNGUP)
[7c7aae16]282 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]283 else
[7c7aae16]284 ipc_backsend_err(phone, call, ENOENT);
[9f22213]285 }
[da1bafb]286
[9f22213]287 return ENOENT;
[ba81cab]288 }
[da1bafb]289
290 answerbox_t *box = phone->callee;
[fbcfd458]291 _ipc_call(phone, box, call);
292
[ff48a15]293 mutex_unlock(&phone->lock);
[9f22213]294 return 0;
[fbcfd458]295}
296
[8b243f2]297/** Disconnect phone from answerbox.
[fbcfd458]298 *
[8b243f2]299 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
[eb3d379]300 * lazily later.
[fbcfd458]301 *
[da1bafb]302 * @param phone Phone structure to be hung up.
303 *
304 * @return 0 if the phone is disconnected.
305 * @return -1 if the phone was already disconnected.
306 *
[fbcfd458]307 */
[d8f7362]308int ipc_phone_hangup(phone_t *phone)
[fbcfd458]309{
[ff48a15]310 mutex_lock(&phone->lock);
[7918fce]311 if (phone->state == IPC_PHONE_FREE ||
312 phone->state == IPC_PHONE_HUNGUP ||
[8b243f2]313 phone->state == IPC_PHONE_CONNECTING) {
[ff48a15]314 mutex_unlock(&phone->lock);
[eb3d379]315 return -1;
[fbcfd458]316 }
[da1bafb]317
318 answerbox_t *box = phone->callee;
[eb3d379]319 if (phone->state != IPC_PHONE_SLAMMED) {
320 /* Remove myself from answerbox */
[da1bafb]321 irq_spinlock_lock(&box->lock, true);
[c4e4507]322 list_remove(&phone->link);
[da1bafb]323 irq_spinlock_unlock(&box->lock, true);
324
325 call_t *call = ipc_call_alloc(0);
[228e490]326 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[287e83f]327 call->flags |= IPC_CALL_DISCARD_ANSWER;
328 _ipc_call(phone, box, call);
[eb3d379]329 }
[da1bafb]330
[eb3d379]331 phone->state = IPC_PHONE_HUNGUP;
[ff48a15]332 mutex_unlock(&phone->lock);
[da1bafb]333
[fbcfd458]334 return 0;
[2ba7810]335}
336
[8b243f2]337/** Forwards call from one answerbox to another one.
[2ba7810]338 *
[da1bafb]339 * @param call Call structure to be redirected.
340 * @param newphone Phone structure to target answerbox.
341 * @param oldbox Old answerbox structure.
342 * @param mode Flags that specify mode of the forward operation.
343 *
344 * @return 0 if forwarding succeeded or an error code if
345 * there was an error.
[8b243f2]346 *
347 * The return value serves only as an information for the forwarder,
348 * the original caller is notified automatically with EFORWARD.
[da1bafb]349 *
[2ba7810]350 */
[da1bafb]351int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
352 unsigned int mode)
[2ba7810]353{
[95319bd]354 /* Count forwarded calls */
[da1bafb]355 irq_spinlock_lock(&TASK->lock, true);
[a307beb]356 TASK->ipc_info.forwarded++;
[da1bafb]357 irq_spinlock_pass(&TASK->lock, &oldbox->lock);
[cfaa35a]358 list_remove(&call->ab_link);
[da1bafb]359 irq_spinlock_unlock(&oldbox->lock, true);
360
[645d9ed2]361 if (mode & IPC_FF_ROUTE_FROM_ME) {
[27526e87]362 if (!call->caller_phone)
363 call->caller_phone = call->data.phone;
[9201f47]364 call->data.phone = newphone;
[e2ab36f1]365 call->data.task_id = TASK->taskid;
[645d9ed2]366 }
[da1bafb]367
[9f22213]368 return ipc_call(newphone, call);
[6d9c49a]369}
370
371
[8b243f2]372/** Wait for a phone call.
[6d9c49a]373 *
[da1bafb]374 * @param box Answerbox expecting the call.
375 * @param usec Timeout in microseconds. See documentation for
376 * waitq_sleep_timeout() for decription of its special
377 * meaning.
378 * @param flags Select mode of sleep operation. See documentation for
379 * waitq_sleep_timeout() for description of its special
380 * meaning.
381 *
382 * @return Recived call structure or NULL.
383 *
[8b243f2]384 * To distinguish between a call and an answer, have a look at call->flags.
[da1bafb]385 *
[6d9c49a]386 */
[da1bafb]387call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
[6d9c49a]388{
389 call_t *request;
[aa028db]390 uint64_t irq_cnt = 0;
391 uint64_t answer_cnt = 0;
392 uint64_t call_cnt = 0;
[bd5a663]393 int rc;
[da1bafb]394
[bd5a663]395restart:
[116d1ef4]396 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]397 if (SYNCH_FAILED(rc))
398 return NULL;
[fbcfd458]399
[da1bafb]400 irq_spinlock_lock(&box->lock, true);
[5626277]401 if (!list_empty(&box->irq_notifs)) {
[be06914]402 /* Count received IRQ notification */
[da1bafb]403 irq_cnt++;
404
405 irq_spinlock_lock(&box->irq_lock, false);
406
[55b77d9]407 request = list_get_instance(list_first(&box->irq_notifs),
[cfaa35a]408 call_t, ab_link);
409 list_remove(&request->ab_link);
[da1bafb]410
411 irq_spinlock_unlock(&box->irq_lock, false);
[5626277]412 } else if (!list_empty(&box->answers)) {
[be06914]413 /* Count received answer */
[aa028db]414 answer_cnt++;
[da1bafb]415
[fbcfd458]416 /* Handle asynchronous answers */
[55b77d9]417 request = list_get_instance(list_first(&box->answers),
[cfaa35a]418 call_t, ab_link);
419 list_remove(&request->ab_link);
[74965d2]420 atomic_dec(&request->data.phone->active_calls);
[fbcfd458]421 } else if (!list_empty(&box->calls)) {
[be06914]422 /* Count received call */
[aa028db]423 call_cnt++;
[da1bafb]424
[fbcfd458]425 /* Handle requests */
[55b77d9]426 request = list_get_instance(list_first(&box->calls),
[cfaa35a]427 call_t, ab_link);
428 list_remove(&request->ab_link);
[da1bafb]429
[fbcfd458]430 /* Append request to dispatch queue */
[cfaa35a]431 list_append(&request->ab_link, &box->dispatched_calls);
[fbcfd458]432 } else {
[874621f]433 /* This can happen regularly after ipc_cleanup */
[da1bafb]434 irq_spinlock_unlock(&box->lock, true);
[fbcfd458]435 goto restart;
[6d9c49a]436 }
[aa028db]437
[da1bafb]438 irq_spinlock_pass(&box->lock, &TASK->lock);
439
[be06914]440 TASK->ipc_info.irq_notif_received += irq_cnt;
441 TASK->ipc_info.answer_received += answer_cnt;
442 TASK->ipc_info.call_received += call_cnt;
[da1bafb]443
444 irq_spinlock_unlock(&TASK->lock, true);
445
[6d9c49a]446 return request;
447}
448
[8b243f2]449/** Answer all calls from list with EHANGUP answer.
450 *
[da1bafb]451 * @param lst Head of the list to be cleaned up.
452 *
[8b243f2]453 */
[55b77d9]454void ipc_cleanup_call_list(list_t *lst)
[ca687ad]455{
456 while (!list_empty(lst)) {
[cfaa35a]457 call_t *call = list_get_instance(list_first(lst), call_t,
458 ab_link);
[a55d5f9f]459 if (call->buffer)
460 free(call->buffer);
[da1bafb]461
[cfaa35a]462 list_remove(&call->ab_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) {
[cfaa35a]692 call_t *call = list_get_instance(cur, call_t, ab_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) {
[cfaa35a]712 call_t *call = list_get_instance(cur, call_t, ab_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) {
[cfaa35a]732 call_t *call = list_get_instance(cur, call_t, ab_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.