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

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

Streamline code

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