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

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

Use the caller local variable instead of the full dereference.

  • Property mode set to 100644
File size: 22.8 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>
[b1e6269]47#include <ipc/sysipc_ops.h>
[5d3ed34]48#include <ipc/sysipc_priv.h>
[6d9c49a]49#include <errno.h>
50#include <mm/slab.h>
51#include <arch.h>
52#include <proc/task.h>
53#include <memstr.h>
54#include <debug.h>
55#include <print.h>
[9a1b20c]56#include <console/console.h>
[6d9c49a]57#include <proc/thread.h>
[5626277]58#include <arch/interrupt.h>
[162f919]59#include <ipc/irq.h>
[6d9c49a]60
[8b243f2]61/** Open channel that is assigned automatically to new tasks */
[e74cb73]62answerbox_t *ipc_phone_0 = NULL;
[6d9c49a]63
64static slab_cache_t *ipc_call_slab;
[c70ce74]65static slab_cache_t *ipc_answerbox_slab;
[6d9c49a]66
[8b243f2]67/** Initialize a call structure.
68 *
[da1bafb]69 * @param call Call structure to be initialized.
70 *
[8b243f2]71 */
[ba81cab]72static void _ipc_call_init(call_t *call)
73{
[e32e092]74 memsetb(call, sizeof(*call), 0);
[2405bb5]75 spinlock_initialize(&call->forget_lock, "forget_lock");
[20282ef3]76 call->active = false;
[2405bb5]77 call->forget = false;
[03a8a8e]78 call->sender = NULL;
[7918fce]79 call->buffer = NULL;
[ba81cab]80}
81
[cd671c3]82void ipc_call_hold(call_t *call)
83{
84 atomic_inc(&call->refcnt);
85}
86
87void ipc_call_release(call_t *call)
88{
89 if (atomic_predec(&call->refcnt) == 0) {
90 if (call->buffer)
91 free(call->buffer);
92 slab_free(ipc_call_slab, call);
93 }
94}
95
[8b243f2]96/** Allocate and initialize a call structure.
[da1bafb]97 *
[8b243f2]98 * The call is initialized, so that the reply will be directed to
99 * TASK->answerbox.
100 *
[da1bafb]101 * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
102 *
103 * @return If flags permit it, return NULL, or initialized kernel
[cd671c3]104 * call structure with one reference.
[5626277]105 *
[6d9c49a]106 */
[da1bafb]107call_t *ipc_call_alloc(unsigned int flags)
[6d9c49a]108{
[da1bafb]109 call_t *call = slab_alloc(ipc_call_slab, flags);
[cd671c3]110 if (call) {
[69eac4aa]111 _ipc_call_init(call);
[cd671c3]112 ipc_call_hold(call);
113 }
[da1bafb]114
[6d9c49a]115 return call;
116}
117
[bd72c3e9]118/** Deallocate a call structure.
[8b243f2]119 *
[da1bafb]120 * @param call Call structure to be freed.
121 *
[8b243f2]122 */
[6d9c49a]123void ipc_call_free(call_t *call)
124{
[cd671c3]125 ipc_call_release(call);
[6d9c49a]126}
127
[8b243f2]128/** Initialize an answerbox structure.
129 *
[da1bafb]130 * @param box Answerbox structure to be initialized.
131 * @param task Task to which the answerbox belongs.
132 *
[6d9c49a]133 */
[12ab886]134void ipc_answerbox_init(answerbox_t *box, task_t *task)
[6d9c49a]135{
[da1bafb]136 irq_spinlock_initialize(&box->lock, "ipc.box.lock");
137 irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
[2ba7810]138 waitq_initialize(&box->wq);
[6d9c49a]139 list_initialize(&box->connected_phones);
140 list_initialize(&box->calls);
141 list_initialize(&box->dispatched_calls);
142 list_initialize(&box->answers);
[5626277]143 list_initialize(&box->irq_notifs);
[55b77d9]144 list_initialize(&box->irq_list);
[12ab886]145 box->task = task;
[6d9c49a]146}
147
[8b243f2]148/** Connect a phone to an answerbox.
149 *
[da1bafb]150 * @param phone Initialized phone structure.
151 * @param box Initialized answerbox structure.
[c33f39f]152 * @return True if the phone was connected, false otherwise.
[8b243f2]153 */
[c33f39f]154bool ipc_phone_connect(phone_t *phone, answerbox_t *box)
[6d9c49a]155{
[c33f39f]156 bool active;
157
[ff48a15]158 mutex_lock(&phone->lock);
[da1bafb]159 irq_spinlock_lock(&box->lock, true);
[c33f39f]160
161 active = box->active;
162 if (active) {
163 phone->state = IPC_PHONE_CONNECTED;
164 phone->callee = box;
165 list_append(&phone->link, &box->connected_phones);
166 }
167
[da1bafb]168 irq_spinlock_unlock(&box->lock, true);
[ff48a15]169 mutex_unlock(&phone->lock);
[c33f39f]170
171 return active;
[2ba7810]172}
173
[8b243f2]174/** Initialize a phone structure.
175 *
[da1bafb]176 * @param phone Phone structure to be initialized.
[03a8a8e]177 * @param caller Owning task.
[da1bafb]178 *
[2ba7810]179 */
[03a8a8e]180void ipc_phone_init(phone_t *phone, task_t *caller)
[2ba7810]181{
[08a19ba]182 mutex_initialize(&phone->lock, MUTEX_PASSIVE);
[03a8a8e]183 phone->caller = caller;
[2ba7810]184 phone->callee = NULL;
[eb3d379]185 phone->state = IPC_PHONE_FREE;
[9f22213]186 atomic_set(&phone->active_calls, 0);
[6d9c49a]187}
188
[8b243f2]189/** Answer a message which was not dispatched and is not listed in any queue.
190 *
[da1bafb]191 * @param call Call structure to be answered.
192 * @param selflocked If true, then TASK->answebox is locked.
193 *
[ba81cab]194 */
[f9841e69]195void _ipc_answer_free_call(call_t *call, bool selflocked)
[ba81cab]196{
[95319bd]197 /* Count sent answer */
[da1bafb]198 irq_spinlock_lock(&TASK->lock, true);
[a307beb]199 TASK->ipc_info.answer_sent++;
[da1bafb]200 irq_spinlock_unlock(&TASK->lock, true);
[2405bb5]201
202 spinlock_lock(&call->forget_lock);
203 if (call->forget) {
204 /* This is a forgotten call and call->sender is not valid. */
205 spinlock_unlock(&call->forget_lock);
[7975433]206 ipc_call_free(call);
[2405bb5]207 return;
208 } else {
209 /*
[20282ef3]210 * If the call is still active, i.e. it was answered
211 * in a non-standard way, remove the call from the
212 * sender's active call list.
[2405bb5]213 */
[20282ef3]214 if (call->active) {
215 spinlock_lock(&call->sender->active_calls_lock);
216 list_remove(&call->ta_link);
217 spinlock_unlock(&call->sender->active_calls_lock);
218 }
[2405bb5]219 }
220 spinlock_unlock(&call->forget_lock);
221
222 answerbox_t *callerbox = &call->sender->answerbox;
223 bool do_lock = ((!selflocked) || (callerbox != &TASK->answerbox));
[da1bafb]224
[ba81cab]225 call->flags |= IPC_CALL_ANSWERED;
[da1bafb]226
[ab34cc9]227 call->data.task_id = TASK->taskid;
[da1bafb]228
[c713aa56]229 if (do_lock)
[da1bafb]230 irq_spinlock_lock(&callerbox->lock, true);
231
[cfaa35a]232 list_append(&call->ab_link, &callerbox->answers);
[da1bafb]233
[c713aa56]234 if (do_lock)
[da1bafb]235 irq_spinlock_unlock(&callerbox->lock, true);
236
[5c8ba05]237 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
[ba81cab]238}
239
[8b243f2]240/** Answer a message which is in a callee queue.
[ba81cab]241 *
[da1bafb]242 * @param box Answerbox that is answering the message.
243 * @param call Modified request that is being sent back.
244 *
[ba81cab]245 */
246void ipc_answer(answerbox_t *box, call_t *call)
247{
248 /* Remove from active box */
[da1bafb]249 irq_spinlock_lock(&box->lock, true);
[cfaa35a]250 list_remove(&call->ab_link);
[da1bafb]251 irq_spinlock_unlock(&box->lock, true);
252
[ba81cab]253 /* Send back answer */
[c713aa56]254 _ipc_answer_free_call(call, false);
[ba81cab]255}
256
[8b243f2]257/** Simulate sending back a message.
[7c7aae16]258 *
259 * Most errors are better handled by forming a normal backward
260 * message and sending it as a normal answer.
[8b243f2]261 *
[da1bafb]262 * @param phone Phone structure the call should appear to come from.
263 * @param call Call structure to be answered.
264 * @param err Return value to be used for the answer.
265 *
[7c7aae16]266 */
[96b02eb9]267void ipc_backsend_err(phone_t *phone, call_t *call, sysarg_t err)
[7c7aae16]268{
[03a8a8e]269 task_t *caller = phone->caller;
270
[7c7aae16]271 atomic_inc(&phone->active_calls);
[c97b086]272 call->caller_phone = phone;
[03a8a8e]273 call->sender = caller;
[86939b1]274
[c97b086]275 call->active = true;
[03a8a8e]276 spinlock_lock(&caller->active_calls_lock);
277 list_append(&call->ta_link, &caller->active_calls);
278 spinlock_unlock(&caller->active_calls_lock);
[86939b1]279
[c97b086]280 call->data.phone = phone;
281
[eb3d379]282 IPC_SET_RETVAL(call->data, err);
[c713aa56]283 _ipc_answer_free_call(call, false);
[7c7aae16]284}
285
[8b243f2]286/** Unsafe unchecking version of ipc_call.
287 *
[da1bafb]288 * @param phone Phone structure the call comes from.
289 * @param box Destination answerbox structure.
290 * @param call Call structure with request.
291 *
[8b243f2]292 */
[fbcfd458]293static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
294{
[03a8a8e]295 task_t *caller = phone->caller;
296
[95319bd]297 /* Count sent ipc call */
[03a8a8e]298 irq_spinlock_lock(&caller->lock, true);
299 caller->ipc_info.call_sent++;
300 irq_spinlock_unlock(&caller->lock, true);
[da1bafb]301
[8b243f2]302 if (!(call->flags & IPC_CALL_FORWARDED)) {
[9f22213]303 atomic_inc(&phone->active_calls);
[5a77550]304 call->caller_phone = phone;
[03a8a8e]305 call->sender = caller;
[20282ef3]306
[c97b086]307 call->active = true;
[03a8a8e]308 spinlock_lock(&caller->active_calls_lock);
309 list_append(&call->ta_link, &caller->active_calls);
310 spinlock_unlock(&caller->active_calls_lock);
[86939b1]311
[9f22213]312 call->data.phone = phone;
[e9fe33b]313 call->data.task_id = caller->taskid;
[9f22213]314 }
[da1bafb]315
316 irq_spinlock_lock(&box->lock, true);
[cfaa35a]317 list_append(&call->ab_link, &box->calls);
[da1bafb]318 irq_spinlock_unlock(&box->lock, true);
319
[5c8ba05]320 waitq_wakeup(&box->wq, WAKEUP_FIRST);
[fbcfd458]321}
322
[8b243f2]323/** Send an asynchronous request using a phone to an answerbox.
324 *
[da1bafb]325 * @param phone Phone structure the call comes from and which is
326 * connected to the destination answerbox.
327 * @param call Call structure with request.
328 *
329 * @return Return 0 on success, ENOENT on error.
[6d9c49a]330 *
331 */
[9f22213]332int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]333{
[ff48a15]334 mutex_lock(&phone->lock);
[eb3d379]335 if (phone->state != IPC_PHONE_CONNECTED) {
[ff48a15]336 mutex_unlock(&phone->lock);
[f9841e69]337 if (!(call->flags & IPC_CALL_FORWARDED)) {
[eb3d379]338 if (phone->state == IPC_PHONE_HUNGUP)
[7c7aae16]339 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]340 else
[7c7aae16]341 ipc_backsend_err(phone, call, ENOENT);
[9f22213]342 }
[da1bafb]343
[9f22213]344 return ENOENT;
[ba81cab]345 }
[da1bafb]346
347 answerbox_t *box = phone->callee;
[fbcfd458]348 _ipc_call(phone, box, call);
349
[ff48a15]350 mutex_unlock(&phone->lock);
[9f22213]351 return 0;
[fbcfd458]352}
353
[8b243f2]354/** Disconnect phone from answerbox.
[fbcfd458]355 *
[8b243f2]356 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
[eb3d379]357 * lazily later.
[fbcfd458]358 *
[da1bafb]359 * @param phone Phone structure to be hung up.
360 *
361 * @return 0 if the phone is disconnected.
362 * @return -1 if the phone was already disconnected.
363 *
[fbcfd458]364 */
[d8f7362]365int ipc_phone_hangup(phone_t *phone)
[fbcfd458]366{
[ff48a15]367 mutex_lock(&phone->lock);
[7918fce]368 if (phone->state == IPC_PHONE_FREE ||
369 phone->state == IPC_PHONE_HUNGUP ||
[8b243f2]370 phone->state == IPC_PHONE_CONNECTING) {
[ff48a15]371 mutex_unlock(&phone->lock);
[eb3d379]372 return -1;
[fbcfd458]373 }
[da1bafb]374
375 answerbox_t *box = phone->callee;
[eb3d379]376 if (phone->state != IPC_PHONE_SLAMMED) {
377 /* Remove myself from answerbox */
[da1bafb]378 irq_spinlock_lock(&box->lock, true);
[c4e4507]379 list_remove(&phone->link);
[da1bafb]380 irq_spinlock_unlock(&box->lock, true);
381
382 call_t *call = ipc_call_alloc(0);
[228e490]383 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[796692c]384 call->request_method = IPC_M_PHONE_HUNGUP;
[287e83f]385 call->flags |= IPC_CALL_DISCARD_ANSWER;
386 _ipc_call(phone, box, call);
[eb3d379]387 }
[da1bafb]388
[eb3d379]389 phone->state = IPC_PHONE_HUNGUP;
[ff48a15]390 mutex_unlock(&phone->lock);
[da1bafb]391
[fbcfd458]392 return 0;
[2ba7810]393}
394
[8b243f2]395/** Forwards call from one answerbox to another one.
[2ba7810]396 *
[da1bafb]397 * @param call Call structure to be redirected.
398 * @param newphone Phone structure to target answerbox.
399 * @param oldbox Old answerbox structure.
400 * @param mode Flags that specify mode of the forward operation.
401 *
402 * @return 0 if forwarding succeeded or an error code if
403 * there was an error.
[8b243f2]404 *
405 * The return value serves only as an information for the forwarder,
406 * the original caller is notified automatically with EFORWARD.
[da1bafb]407 *
[2ba7810]408 */
[da1bafb]409int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
410 unsigned int mode)
[2ba7810]411{
[95319bd]412 /* Count forwarded calls */
[da1bafb]413 irq_spinlock_lock(&TASK->lock, true);
[a307beb]414 TASK->ipc_info.forwarded++;
[da1bafb]415 irq_spinlock_pass(&TASK->lock, &oldbox->lock);
[cfaa35a]416 list_remove(&call->ab_link);
[da1bafb]417 irq_spinlock_unlock(&oldbox->lock, true);
418
[645d9ed2]419 if (mode & IPC_FF_ROUTE_FROM_ME) {
[9201f47]420 call->data.phone = newphone;
[e2ab36f1]421 call->data.task_id = TASK->taskid;
[645d9ed2]422 }
[da1bafb]423
[9f22213]424 return ipc_call(newphone, call);
[6d9c49a]425}
426
427
[8b243f2]428/** Wait for a phone call.
[6d9c49a]429 *
[da1bafb]430 * @param box Answerbox expecting the call.
431 * @param usec Timeout in microseconds. See documentation for
432 * waitq_sleep_timeout() for decription of its special
433 * meaning.
434 * @param flags Select mode of sleep operation. See documentation for
435 * waitq_sleep_timeout() for description of its special
436 * meaning.
437 *
438 * @return Recived call structure or NULL.
439 *
[8b243f2]440 * To distinguish between a call and an answer, have a look at call->flags.
[da1bafb]441 *
[6d9c49a]442 */
[da1bafb]443call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
[6d9c49a]444{
445 call_t *request;
[aa028db]446 uint64_t irq_cnt = 0;
447 uint64_t answer_cnt = 0;
448 uint64_t call_cnt = 0;
[bd5a663]449 int rc;
[da1bafb]450
[bd5a663]451restart:
[116d1ef4]452 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]453 if (SYNCH_FAILED(rc))
454 return NULL;
[fbcfd458]455
[da1bafb]456 irq_spinlock_lock(&box->lock, true);
[5626277]457 if (!list_empty(&box->irq_notifs)) {
[be06914]458 /* Count received IRQ notification */
[da1bafb]459 irq_cnt++;
460
461 irq_spinlock_lock(&box->irq_lock, false);
462
[55b77d9]463 request = list_get_instance(list_first(&box->irq_notifs),
[cfaa35a]464 call_t, ab_link);
465 list_remove(&request->ab_link);
[da1bafb]466
467 irq_spinlock_unlock(&box->irq_lock, false);
[5626277]468 } else if (!list_empty(&box->answers)) {
[be06914]469 /* Count received answer */
[aa028db]470 answer_cnt++;
[da1bafb]471
[fbcfd458]472 /* Handle asynchronous answers */
[55b77d9]473 request = list_get_instance(list_first(&box->answers),
[cfaa35a]474 call_t, ab_link);
475 list_remove(&request->ab_link);
[5a77550]476 atomic_dec(&request->caller_phone->active_calls);
[fbcfd458]477 } else if (!list_empty(&box->calls)) {
[be06914]478 /* Count received call */
[aa028db]479 call_cnt++;
[da1bafb]480
[fbcfd458]481 /* Handle requests */
[55b77d9]482 request = list_get_instance(list_first(&box->calls),
[cfaa35a]483 call_t, ab_link);
484 list_remove(&request->ab_link);
[da1bafb]485
[fbcfd458]486 /* Append request to dispatch queue */
[cfaa35a]487 list_append(&request->ab_link, &box->dispatched_calls);
[fbcfd458]488 } else {
[874621f]489 /* This can happen regularly after ipc_cleanup */
[da1bafb]490 irq_spinlock_unlock(&box->lock, true);
[fbcfd458]491 goto restart;
[6d9c49a]492 }
[aa028db]493
[da1bafb]494 irq_spinlock_pass(&box->lock, &TASK->lock);
495
[be06914]496 TASK->ipc_info.irq_notif_received += irq_cnt;
497 TASK->ipc_info.answer_received += answer_cnt;
498 TASK->ipc_info.call_received += call_cnt;
[da1bafb]499
500 irq_spinlock_unlock(&TASK->lock, true);
501
[6d9c49a]502 return request;
503}
504
[8b243f2]505/** Answer all calls from list with EHANGUP answer.
506 *
[5d3ed34]507 * @param box Answerbox with the list.
[da1bafb]508 * @param lst Head of the list to be cleaned up.
[8b243f2]509 */
[5d3ed34]510void ipc_cleanup_call_list(answerbox_t *box, list_t *lst)
[ca687ad]511{
[5d3ed34]512 irq_spinlock_lock(&box->lock, true);
[ca687ad]513 while (!list_empty(lst)) {
[cfaa35a]514 call_t *call = list_get_instance(list_first(lst), call_t,
515 ab_link);
[da1bafb]516
[cfaa35a]517 list_remove(&call->ab_link);
[5d3ed34]518
519 irq_spinlock_unlock(&box->lock, true);
520
521 ipc_data_t old = call->data;
[ca687ad]522 IPC_SET_RETVAL(call->data, EHANGUP);
[5d3ed34]523 answer_preprocess(call, &old);
[c713aa56]524 _ipc_answer_free_call(call, true);
[5d3ed34]525
526 irq_spinlock_lock(&box->lock, true);
[ca687ad]527 }
[5d3ed34]528 irq_spinlock_unlock(&box->lock, true);
[ca687ad]529}
530
[9a1b20c]531/** Disconnects all phones connected to an answerbox.
[4e49572]532 *
[da1bafb]533 * @param box Answerbox to disconnect phones from.
534 * @param notify_box If true, the answerbox will get a hangup message for
535 * each disconnected phone.
536 *
[4e49572]537 */
[9a1b20c]538void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
[4e49572]539{
[ca687ad]540 phone_t *phone;
[31d8e10]541 DEADLOCK_PROBE_INIT(p_phonelck);
[da1bafb]542
543 call_t *call = notify_box ? ipc_call_alloc(0) : NULL;
544
[ca687ad]545 /* Disconnect all phones connected to our answerbox */
546restart_phones:
[da1bafb]547 irq_spinlock_lock(&box->lock, true);
[9a1b20c]548 while (!list_empty(&box->connected_phones)) {
[55b77d9]549 phone = list_get_instance(list_first(&box->connected_phones),
[31d8e10]550 phone_t, link);
[ff48a15]551 if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
[da1bafb]552 irq_spinlock_unlock(&box->lock, true);
[31d8e10]553 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
[ca687ad]554 goto restart_phones;
555 }
556
557 /* Disconnect phone */
[eb3d379]558 ASSERT(phone->state == IPC_PHONE_CONNECTED);
[da1bafb]559
[c4e4507]560 list_remove(&phone->link);
[9a1b20c]561 phone->state = IPC_PHONE_SLAMMED;
[da1bafb]562
[9a1b20c]563 if (notify_box) {
564 mutex_unlock(&phone->lock);
[da1bafb]565 irq_spinlock_unlock(&box->lock, true);
[f1d5ef8]566
567 // FIXME: phone can become deallocated at any time now
568
[9a1b20c]569 /*
570 * Send one message to the answerbox for each
571 * phone. Used to make sure the kbox thread
572 * wakes up after the last phone has been
573 * disconnected.
574 */
[228e490]575 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[796692c]576 call->request_method = IPC_M_PHONE_HUNGUP;
[9a1b20c]577 call->flags |= IPC_CALL_DISCARD_ANSWER;
578 _ipc_call(phone, box, call);
[da1bafb]579
[9a1b20c]580 /* Allocate another call in advance */
581 call = ipc_call_alloc(0);
[da1bafb]582
[9a1b20c]583 /* Must start again */
584 goto restart_phones;
585 }
[da1bafb]586
[ff48a15]587 mutex_unlock(&phone->lock);
[ca687ad]588 }
[da1bafb]589
590 irq_spinlock_unlock(&box->lock, true);
591
[9a1b20c]592 /* Free unused call */
[119c335]593 if (call)
594 ipc_call_free(call);
[9a1b20c]595}
596
[2405bb5]597static void ipc_forget_all_active_calls(void)
598{
599 call_t *call;
600
601restart:
602 spinlock_lock(&TASK->active_calls_lock);
603 if (list_empty(&TASK->active_calls)) {
604 /*
605 * We are done, there are no more active calls.
606 * Nota bene: there may still be answers waiting for pick up.
607 */
608 spinlock_unlock(&TASK->active_calls_lock);
609 return;
610 }
611
612 call = list_get_instance(list_first(&TASK->active_calls), call_t,
613 ta_link);
614
615 if (!spinlock_trylock(&call->forget_lock)) {
616 /*
[20282ef3]617 * Avoid deadlock and let async_answer() or
618 * _ipc_answer_free_call() win the race to dequeue the first
619 * call on the list.
[2405bb5]620 */
621 spinlock_unlock(&TASK->active_calls_lock);
622 goto restart;
623 }
624
625 /*
626 * Forget the call and donate it to the task which holds up the answer.
627 */
628
629 call->forget = true;
630 call->sender = NULL;
631 list_remove(&call->ta_link);
632
[cd671c3]633 /*
634 * The call may be freed by _ipc_answer_free_call() before we are done
635 * with it; to avoid working with a destroyed call_t structure, we
636 * must hold a reference to it.
637 */
638 ipc_call_hold(call);
639
[2405bb5]640 spinlock_unlock(&call->forget_lock);
641 spinlock_unlock(&TASK->active_calls_lock);
[b1e6269]642
[5a77550]643 atomic_dec(&call->caller_phone->active_calls);
644
[b1e6269]645 sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
646 if (ops->request_forget)
647 ops->request_forget(call);
648
[cd671c3]649 ipc_call_release(call);
650
[2405bb5]651 goto restart;
652}
653
654/** Wait for all answers to asynchronous calls to arrive. */
655static void ipc_wait_for_all_answered_calls(void)
656{
657 call_t *call;
658 size_t i;
659
660restart:
661 /*
[d891cba]662 * Go through all phones, until they are all free.
663 * Locking is needed as there may be connection handshakes in progress.
[2405bb5]664 */
665 for (i = 0; i < IPC_MAX_PHONES; i++) {
[d891cba]666 phone_t *phone = &TASK->phones[i];
667
668 mutex_lock(&phone->lock);
669 if ((phone->state == IPC_PHONE_HUNGUP) &&
670 (atomic_get(&phone->active_calls) == 0)) {
671 phone->state = IPC_PHONE_FREE;
672 phone->callee = NULL;
[2405bb5]673 }
674
675 /*
[f9bd2e3]676 * We might have had some IPC_PHONE_CONNECTING phones at the
677 * beginning of ipc_cleanup(). Depending on whether these were
678 * forgotten or answered, they will eventually enter the
679 * IPC_PHONE_FREE or IPC_PHONE_CONNECTED states, respectively.
680 * In the latter case, the other side may slam the open phones
681 * at any time, in which case we will get an IPC_PHONE_SLAMMED
682 * phone.
[2405bb5]683 */
[d891cba]684 if ((phone->state == IPC_PHONE_CONNECTED) ||
685 (phone->state == IPC_PHONE_SLAMMED)) {
686 mutex_unlock(&phone->lock);
687 ipc_phone_hangup(phone);
[8f6858d0]688 /*
[f9bd2e3]689 * Now there may be one extra active call, which needs
690 * to be forgotten.
[8f6858d0]691 */
692 ipc_forget_all_active_calls();
693 goto restart;
694 }
[2405bb5]695
696 /*
697 * If the hangup succeeded, it has sent a HANGUP message, the
698 * IPC is now in HUNGUP state, we wait for the reply to come
699 */
[d891cba]700 if (phone->state != IPC_PHONE_FREE) {
701 mutex_unlock(&phone->lock);
[2405bb5]702 break;
[d891cba]703 }
704
705 mutex_unlock(&phone->lock);
[2405bb5]706 }
707
708 /* Got into cleanup */
709 if (i == IPC_MAX_PHONES)
710 return;
711
712 call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
713 SYNCH_FLAGS_NONE);
[525e91b]714 ASSERT(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
[2405bb5]715 ipc_call_free(call);
716 goto restart;
717}
718
[da1bafb]719/** Clean up all IPC communication of the current task.
[9a1b20c]720 *
721 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
722 * have to change it as well if you want to cleanup other tasks than TASK.
[da1bafb]723 *
[9a1b20c]724 */
725void ipc_cleanup(void)
726{
[c33f39f]727 /*
728 * Mark the answerbox as inactive.
729 *
730 * The main purpose for doing this is to prevent any pending callback
731 * connections from getting established beyond this point.
732 */
733 irq_spinlock_lock(&TASK->answerbox.lock, true);
734 TASK->answerbox.active = false;
735 irq_spinlock_unlock(&TASK->answerbox.lock, true);
736
[9a1b20c]737 /* Disconnect all our phones ('ipc_phone_hangup') */
[2405bb5]738 for (size_t i = 0; i < IPC_MAX_PHONES; i++)
[9a1b20c]739 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]740
[05641a9e]741 /* Unsubscribe from any event notifications. */
742 event_cleanup_answerbox(&TASK->answerbox);
[da1bafb]743
[9a1b20c]744 /* Disconnect all connected irqs */
745 ipc_irq_cleanup(&TASK->answerbox);
[da1bafb]746
[9a1b20c]747 /* Disconnect all phones connected to our regular answerbox */
748 ipc_answerbox_slam_phones(&TASK->answerbox, false);
[da1bafb]749
[9a1b20c]750#ifdef CONFIG_UDEBUG
751 /* Clean up kbox thread and communications */
752 ipc_kbox_cleanup();
753#endif
[da1bafb]754
[9f22213]755 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[5d3ed34]756 ipc_cleanup_call_list(&TASK->answerbox,
757 &TASK->answerbox.dispatched_calls);
758 ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
[2405bb5]759
760 ipc_forget_all_active_calls();
761 ipc_wait_for_all_answered_calls();
[4e49572]762}
[5626277]763
[da1bafb]764/** Initilize IPC subsystem
765 *
766 */
[5626277]767void ipc_init(void)
768{
[f97f1e51]769 ipc_call_slab = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
[8b243f2]770 NULL, 0);
[f97f1e51]771 ipc_answerbox_slab = slab_cache_create("answerbox_t",
[c70ce74]772 sizeof(answerbox_t), 0, NULL, NULL, 0);
[5626277]773}
774
[9c9903a9]775
776static void ipc_print_call_list(list_t *list)
777{
778 list_foreach(*list, cur) {
779 call_t *call = list_get_instance(cur, call_t, ab_link);
780
781#ifdef __32_BITS__
782 printf("%10p ", call);
783#endif
784
785#ifdef __64_BITS__
786 printf("%18p ", call);
787#endif
788
789 spinlock_lock(&call->forget_lock);
790
791 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
792 " %-6" PRIun " %-6" PRIun " %-7x",
793 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
794 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
795 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
796 call->flags);
797
798 if (call->forget) {
799 printf(" ? (call forgotten)\n");
800 } else {
801 printf(" %" PRIu64 " (%s)\n",
802 call->sender->taskid, call->sender->name);
803 }
804
805 spinlock_unlock(&call->forget_lock);
806 }
807}
808
[8b243f2]809/** List answerbox contents.
810 *
[da1bafb]811 * @param taskid Task ID.
812 *
[8b243f2]813 */
[c4e4507]814void ipc_print_task(task_id_t taskid)
815{
[da1bafb]816 irq_spinlock_lock(&tasks_lock, true);
817 task_t *task = task_find_by_id(taskid);
818
[170332d]819 if (!task) {
[da1bafb]820 irq_spinlock_unlock(&tasks_lock, true);
[c4e4507]821 return;
[170332d]822 }
[da1bafb]823
824 /* Hand-over-hand locking */
825 irq_spinlock_exchange(&tasks_lock, &task->lock);
826
[5378f99]827 printf("[phone id] [calls] [state\n");
[da1bafb]828
829 size_t i;
[8b243f2]830 for (i = 0; i < IPC_MAX_PHONES; i++) {
[ff48a15]831 if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
[5378f99]832 printf("%-10zu (mutex busy)\n", i);
[ff48a15]833 continue;
834 }
[da1bafb]835
[c4e4507]836 if (task->phones[i].state != IPC_PHONE_FREE) {
[5378f99]837 printf("%-10zu %7" PRIun " ", i,
838 atomic_get(&task->phones[i].active_calls));
[da1bafb]839
[c4e4507]840 switch (task->phones[i].state) {
841 case IPC_PHONE_CONNECTING:
[5378f99]842 printf("connecting");
[c4e4507]843 break;
844 case IPC_PHONE_CONNECTED:
[5378f99]845 printf("connected to %" PRIu64 " (%s)",
846 task->phones[i].callee->task->taskid,
847 task->phones[i].callee->task->name);
[c4e4507]848 break;
849 case IPC_PHONE_SLAMMED:
[5378f99]850 printf("slammed by %p",
[da1bafb]851 task->phones[i].callee);
[c4e4507]852 break;
853 case IPC_PHONE_HUNGUP:
[5378f99]854 printf("hung up by %p",
[da1bafb]855 task->phones[i].callee);
[c4e4507]856 break;
857 default:
858 break;
859 }
[da1bafb]860
[5378f99]861 printf("\n");
[c4e4507]862 }
[da1bafb]863
[ff48a15]864 mutex_unlock(&task->phones[i].lock);
[c4e4507]865 }
[da1bafb]866
867 irq_spinlock_lock(&task->answerbox.lock, false);
868
[5378f99]869#ifdef __32_BITS__
870 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
871 " [flags] [sender\n");
872#endif
873
874#ifdef __64_BITS__
875 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
876 " [arg5] [flags] [sender\n");
877#endif
878
879 printf(" --- incomming calls ---\n");
[9c9903a9]880 ipc_print_call_list(&task->answerbox.calls);
[5378f99]881 printf(" --- dispatched calls ---\n");
[9c9903a9]882 ipc_print_call_list(&task->answerbox.dispatched_calls);
[6aef742]883 printf(" --- incoming answers ---\n");
[9c9903a9]884 ipc_print_call_list(&task->answerbox.answers);
[da1bafb]885
886 irq_spinlock_unlock(&task->answerbox.lock, false);
887 irq_spinlock_unlock(&task->lock, true);
[c4e4507]888}
[b45c443]889
[cc73a8a1]890/** @}
[b45c443]891 */
Note: See TracBrowser for help on using the repository browser.