source: mainline/kernel/generic/src/ipc/ipc.c@ 455241b

Last change on this file since 455241b was 455241b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 5 months ago

Integrate kobject_t into target structures

This just means fewer allocations and indirections.

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