source: mainline/kernel/generic/src/ipc/ipc.c@ 675fcbd

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

Call the answer_process() callback for answers picked up in IPC cleanup.

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