source: mainline/kernel/generic/src/ipc/ipc.c@ 5a77550

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

Always remember the original caller phone in a call.

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