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

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

Prevent likely double free of call→buffer.

The buffer is supposed to be eventually freed by ipc_call_free() along
with the call_t structure itself.

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