source: mainline/kernel/generic/src/ipc/ipc.c@ 96c30c8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 96c30c8 was 96c30c8, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Turn ipc_poke() into a regular wakeup on the waitq.

With prior behavior of ignoring the poke when no thread is blocking,
a thread could go to sleep just after a poke meant for it and sleep
indefinitely despite there being work to do.

After the change, worst case scenario, thread enters ipc wait, exits
immediately due to a previous poke, finds no work to do and enters ipc wait
again (possibly repeating the spurious wakeup a few times).

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