source: mainline/kernel/generic/src/ipc/ipc.c@ 314f4b59

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

Remove dead code

Hungup phones with zero active calls are automatically destroyed with
the last reference.

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