source: mainline/kernel/generic/src/ipc/ipc.c@ 1cb75de

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

Remove an answered call from the task's active list when the call is
answered in _ipc_answer_free_call() rather than when the answer is
picked up by the sender task in ipc_wait_for_call().

This makes the maintainance of the active list symmetric because the
addition takes place in _ipc_call().

Moreover, unlike when tracking the phone's number of active calls, there
is no reason to postpone the removal from the active calls list until
ipc_wait_for_call().

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