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

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

Partially revert jakub@…

This changeset reintroduces portions of the synchronous IPC mechanism for
internal use by the kernel / user_backend. The synchronous IPC is not
exported to user space and is simpler than in its original form.

  • Property mode set to 100644
File size: 23.4 KB
RevLine 
[6d9c49a]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[6d9c49a]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[cc73a8a1]29/** @addtogroup genericipc
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[6d9c49a]35/* Lock ordering
36 *
[8b243f2]37 * First the answerbox, then the phone.
[6d9c49a]38 */
39
[2ba7810]40#include <synch/spinlock.h>
[ff48a15]41#include <synch/mutex.h>
[2ba7810]42#include <synch/waitq.h>
[6d9c49a]43#include <ipc/ipc.h>
[c0699467]44#include <abi/ipc/methods.h>
[e028660]45#include <ipc/kbox.h>
[13a638d]46#include <ipc/event.h>
[b1e6269]47#include <ipc/sysipc_ops.h>
[5d3ed34]48#include <ipc/sysipc_priv.h>
[6d9c49a]49#include <errno.h>
50#include <mm/slab.h>
51#include <arch.h>
52#include <proc/task.h>
53#include <memstr.h>
54#include <debug.h>
55#include <print.h>
[9a1b20c]56#include <console/console.h>
[6d9c49a]57#include <proc/thread.h>
[5626277]58#include <arch/interrupt.h>
[162f919]59#include <ipc/irq.h>
[6d9c49a]60
[8b243f2]61/** Open channel that is assigned automatically to new tasks */
[e74cb73]62answerbox_t *ipc_phone_0 = NULL;
[6d9c49a]63
64static slab_cache_t *ipc_call_slab;
[c70ce74]65static slab_cache_t *ipc_answerbox_slab;
[6d9c49a]66
[8b243f2]67/** Initialize a call structure.
68 *
[da1bafb]69 * @param call Call structure to be initialized.
70 *
[8b243f2]71 */
[ba81cab]72static void _ipc_call_init(call_t *call)
73{
[e32e092]74 memsetb(call, sizeof(*call), 0);
[2405bb5]75 spinlock_initialize(&call->forget_lock, "forget_lock");
[20282ef3]76 call->active = false;
[2405bb5]77 call->forget = false;
[03a8a8e]78 call->sender = NULL;
[6d351e6]79 call->callerbox = &TASK->answerbox;
[7918fce]80 call->buffer = NULL;
[ba81cab]81}
82
[cd671c3]83void ipc_call_hold(call_t *call)
84{
85 atomic_inc(&call->refcnt);
86}
87
88void ipc_call_release(call_t *call)
89{
90 if (atomic_predec(&call->refcnt) == 0) {
91 if (call->buffer)
92 free(call->buffer);
93 slab_free(ipc_call_slab, call);
94 }
95}
96
[8b243f2]97/** Allocate and initialize a call structure.
[da1bafb]98 *
[8b243f2]99 * The call is initialized, so that the reply will be directed to
100 * TASK->answerbox.
101 *
[da1bafb]102 * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
103 *
104 * @return If flags permit it, return NULL, or initialized kernel
[cd671c3]105 * call structure with one reference.
[5626277]106 *
[6d9c49a]107 */
[da1bafb]108call_t *ipc_call_alloc(unsigned int flags)
[6d9c49a]109{
[da1bafb]110 call_t *call = slab_alloc(ipc_call_slab, flags);
[cd671c3]111 if (call) {
[69eac4aa]112 _ipc_call_init(call);
[cd671c3]113 ipc_call_hold(call);
114 }
[da1bafb]115
[6d9c49a]116 return call;
117}
118
[bd72c3e9]119/** Deallocate a call structure.
[8b243f2]120 *
[da1bafb]121 * @param call Call structure to be freed.
122 *
[8b243f2]123 */
[6d9c49a]124void ipc_call_free(call_t *call)
125{
[cd671c3]126 ipc_call_release(call);
[6d9c49a]127}
128
[8b243f2]129/** Initialize an answerbox structure.
130 *
[da1bafb]131 * @param box Answerbox structure to be initialized.
132 * @param task Task to which the answerbox belongs.
133 *
[6d9c49a]134 */
[12ab886]135void ipc_answerbox_init(answerbox_t *box, task_t *task)
[6d9c49a]136{
[da1bafb]137 irq_spinlock_initialize(&box->lock, "ipc.box.lock");
138 irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
[2ba7810]139 waitq_initialize(&box->wq);
[6d9c49a]140 list_initialize(&box->connected_phones);
141 list_initialize(&box->calls);
142 list_initialize(&box->dispatched_calls);
143 list_initialize(&box->answers);
[5626277]144 list_initialize(&box->irq_notifs);
[55b77d9]145 list_initialize(&box->irq_list);
[12ab886]146 box->task = task;
[6d9c49a]147}
148
[8b243f2]149/** Connect a phone to an answerbox.
150 *
[da1bafb]151 * @param phone Initialized phone structure.
152 * @param box Initialized answerbox structure.
[c33f39f]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{
[c33f39f]157 bool active;
158
[ff48a15]159 mutex_lock(&phone->lock);
[da1bafb]160 irq_spinlock_lock(&box->lock, true);
[c33f39f]161
162 active = box->active;
163 if (active) {
164 phone->state = IPC_PHONE_CONNECTED;
165 phone->callee = box;
166 list_append(&phone->link, &box->connected_phones);
167 }
168
[da1bafb]169 irq_spinlock_unlock(&box->lock, true);
[ff48a15]170 mutex_unlock(&phone->lock);
[c33f39f]171
172 return active;
[2ba7810]173}
174
[8b243f2]175/** Initialize a phone structure.
176 *
[da1bafb]177 * @param phone Phone structure to be initialized.
[03a8a8e]178 * @param caller Owning task.
[da1bafb]179 *
[2ba7810]180 */
[03a8a8e]181void ipc_phone_init(phone_t *phone, task_t *caller)
[2ba7810]182{
[08a19ba]183 mutex_initialize(&phone->lock, MUTEX_PASSIVE);
[03a8a8e]184 phone->caller = caller;
[2ba7810]185 phone->callee = NULL;
[eb3d379]186 phone->state = IPC_PHONE_FREE;
[9f22213]187 atomic_set(&phone->active_calls, 0);
[6d9c49a]188}
189
[bc117a5]190/** Helper function to facilitate synchronous calls.
191 *
192 * @param phone Destination kernel phone structure.
193 * @param request Call structure with request.
194 *
195 * @return EOK on success or a negative error code.
196 *
197 */
198int ipc_call_sync(phone_t *phone, call_t *request)
199{
200 answerbox_t *mybox = slab_alloc(ipc_answerbox_slab, 0);
201 ipc_answerbox_init(mybox, TASK);
202
203 /* We will receive data in a special box. */
204 request->callerbox = mybox;
205
206 int rc = ipc_call(phone, request);
207 if (rc != EOK) {
208 slab_free(ipc_answerbox_slab, mybox);
209 return rc;
210 }
211 // TODO: forget the call if interrupted
212 (void) ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
213
214 slab_free(ipc_answerbox_slab, mybox);
215 return EOK;
216}
217
[8b243f2]218/** Answer a message which was not dispatched and is not listed in any queue.
219 *
[da1bafb]220 * @param call Call structure to be answered.
221 * @param selflocked If true, then TASK->answebox is locked.
222 *
[ba81cab]223 */
[f9841e69]224void _ipc_answer_free_call(call_t *call, bool selflocked)
[ba81cab]225{
[95319bd]226 /* Count sent answer */
[da1bafb]227 irq_spinlock_lock(&TASK->lock, true);
[a307beb]228 TASK->ipc_info.answer_sent++;
[da1bafb]229 irq_spinlock_unlock(&TASK->lock, true);
[2405bb5]230
231 spinlock_lock(&call->forget_lock);
232 if (call->forget) {
233 /* This is a forgotten call and call->sender is not valid. */
234 spinlock_unlock(&call->forget_lock);
[7975433]235 ipc_call_free(call);
[2405bb5]236 return;
237 } else {
238 /*
[20282ef3]239 * If the call is still active, i.e. it was answered
240 * in a non-standard way, remove the call from the
241 * sender's active call list.
[2405bb5]242 */
[20282ef3]243 if (call->active) {
244 spinlock_lock(&call->sender->active_calls_lock);
245 list_remove(&call->ta_link);
246 spinlock_unlock(&call->sender->active_calls_lock);
247 }
[2405bb5]248 }
249 spinlock_unlock(&call->forget_lock);
250
[6d351e6]251 answerbox_t *callerbox = call->callerbox;
[2405bb5]252 bool do_lock = ((!selflocked) || (callerbox != &TASK->answerbox));
[da1bafb]253
[ba81cab]254 call->flags |= IPC_CALL_ANSWERED;
[da1bafb]255
[ab34cc9]256 call->data.task_id = TASK->taskid;
[da1bafb]257
[c713aa56]258 if (do_lock)
[da1bafb]259 irq_spinlock_lock(&callerbox->lock, true);
260
[cfaa35a]261 list_append(&call->ab_link, &callerbox->answers);
[da1bafb]262
[c713aa56]263 if (do_lock)
[da1bafb]264 irq_spinlock_unlock(&callerbox->lock, true);
265
[5c8ba05]266 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
[ba81cab]267}
268
[8b243f2]269/** Answer a message which is in a callee queue.
[ba81cab]270 *
[da1bafb]271 * @param box Answerbox that is answering the message.
272 * @param call Modified request that is being sent back.
273 *
[ba81cab]274 */
275void ipc_answer(answerbox_t *box, call_t *call)
276{
277 /* Remove from active box */
[da1bafb]278 irq_spinlock_lock(&box->lock, true);
[cfaa35a]279 list_remove(&call->ab_link);
[da1bafb]280 irq_spinlock_unlock(&box->lock, true);
281
[ba81cab]282 /* Send back answer */
[c713aa56]283 _ipc_answer_free_call(call, false);
[ba81cab]284}
285
[6f9c8f6]286static void _ipc_call_actions_internal(phone_t *phone, call_t *call)
[7c7aae16]287{
[03a8a8e]288 task_t *caller = phone->caller;
289
[7c7aae16]290 atomic_inc(&phone->active_calls);
[c97b086]291 call->caller_phone = phone;
[03a8a8e]292 call->sender = caller;
[86939b1]293
[c97b086]294 call->active = true;
[03a8a8e]295 spinlock_lock(&caller->active_calls_lock);
296 list_append(&call->ta_link, &caller->active_calls);
297 spinlock_unlock(&caller->active_calls_lock);
[86939b1]298
[c97b086]299 call->data.phone = phone;
[6f9c8f6]300 call->data.task_id = caller->taskid;
301}
[c97b086]302
[6f9c8f6]303/** Simulate sending back a message.
304 *
305 * Most errors are better handled by forming a normal backward
306 * message and sending it as a normal answer.
307 *
308 * @param phone Phone structure the call should appear to come from.
309 * @param call Call structure to be answered.
310 * @param err Return value to be used for the answer.
311 *
312 */
313void ipc_backsend_err(phone_t *phone, call_t *call, sysarg_t err)
314{
315 _ipc_call_actions_internal(phone, call);
[eb3d379]316 IPC_SET_RETVAL(call->data, err);
[c713aa56]317 _ipc_answer_free_call(call, false);
[7c7aae16]318}
319
[8b243f2]320/** Unsafe unchecking version of ipc_call.
321 *
[da1bafb]322 * @param phone Phone structure the call comes from.
323 * @param box Destination answerbox structure.
324 * @param call Call structure with request.
325 *
[8b243f2]326 */
[fbcfd458]327static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
328{
[03a8a8e]329 task_t *caller = phone->caller;
330
[95319bd]331 /* Count sent ipc call */
[03a8a8e]332 irq_spinlock_lock(&caller->lock, true);
333 caller->ipc_info.call_sent++;
334 irq_spinlock_unlock(&caller->lock, true);
[da1bafb]335
[6f9c8f6]336 if (!(call->flags & IPC_CALL_FORWARDED))
337 _ipc_call_actions_internal(phone, call);
[da1bafb]338
339 irq_spinlock_lock(&box->lock, true);
[cfaa35a]340 list_append(&call->ab_link, &box->calls);
[da1bafb]341 irq_spinlock_unlock(&box->lock, true);
342
[5c8ba05]343 waitq_wakeup(&box->wq, WAKEUP_FIRST);
[fbcfd458]344}
345
[8b243f2]346/** Send an asynchronous request using a phone to an answerbox.
347 *
[da1bafb]348 * @param phone Phone structure the call comes from and which is
349 * connected to the destination answerbox.
350 * @param call Call structure with request.
351 *
352 * @return Return 0 on success, ENOENT on error.
[6d9c49a]353 *
354 */
[9f22213]355int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]356{
[ff48a15]357 mutex_lock(&phone->lock);
[eb3d379]358 if (phone->state != IPC_PHONE_CONNECTED) {
[ff48a15]359 mutex_unlock(&phone->lock);
[f9841e69]360 if (!(call->flags & IPC_CALL_FORWARDED)) {
[eb3d379]361 if (phone->state == IPC_PHONE_HUNGUP)
[7c7aae16]362 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]363 else
[7c7aae16]364 ipc_backsend_err(phone, call, ENOENT);
[9f22213]365 }
[da1bafb]366
[9f22213]367 return ENOENT;
[ba81cab]368 }
[da1bafb]369
370 answerbox_t *box = phone->callee;
[fbcfd458]371 _ipc_call(phone, box, call);
372
[ff48a15]373 mutex_unlock(&phone->lock);
[9f22213]374 return 0;
[fbcfd458]375}
376
[8b243f2]377/** Disconnect phone from answerbox.
[fbcfd458]378 *
[8b243f2]379 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
[eb3d379]380 * lazily later.
[fbcfd458]381 *
[da1bafb]382 * @param phone Phone structure to be hung up.
383 *
384 * @return 0 if the phone is disconnected.
385 * @return -1 if the phone was already disconnected.
386 *
[fbcfd458]387 */
[d8f7362]388int ipc_phone_hangup(phone_t *phone)
[fbcfd458]389{
[ff48a15]390 mutex_lock(&phone->lock);
[7918fce]391 if (phone->state == IPC_PHONE_FREE ||
392 phone->state == IPC_PHONE_HUNGUP ||
[8b243f2]393 phone->state == IPC_PHONE_CONNECTING) {
[ff48a15]394 mutex_unlock(&phone->lock);
[eb3d379]395 return -1;
[fbcfd458]396 }
[da1bafb]397
398 answerbox_t *box = phone->callee;
[eb3d379]399 if (phone->state != IPC_PHONE_SLAMMED) {
400 /* Remove myself from answerbox */
[da1bafb]401 irq_spinlock_lock(&box->lock, true);
[c4e4507]402 list_remove(&phone->link);
[da1bafb]403 irq_spinlock_unlock(&box->lock, true);
404
405 call_t *call = ipc_call_alloc(0);
[228e490]406 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[796692c]407 call->request_method = IPC_M_PHONE_HUNGUP;
[287e83f]408 call->flags |= IPC_CALL_DISCARD_ANSWER;
409 _ipc_call(phone, box, call);
[eb3d379]410 }
[da1bafb]411
[eb3d379]412 phone->state = IPC_PHONE_HUNGUP;
[ff48a15]413 mutex_unlock(&phone->lock);
[da1bafb]414
[fbcfd458]415 return 0;
[2ba7810]416}
417
[8b243f2]418/** Forwards call from one answerbox to another one.
[2ba7810]419 *
[da1bafb]420 * @param call Call structure to be redirected.
421 * @param newphone Phone structure to target answerbox.
422 * @param oldbox Old answerbox structure.
423 * @param mode Flags that specify mode of the forward operation.
424 *
425 * @return 0 if forwarding succeeded or an error code if
426 * there was an error.
[8b243f2]427 *
428 * The return value serves only as an information for the forwarder,
429 * the original caller is notified automatically with EFORWARD.
[da1bafb]430 *
[2ba7810]431 */
[da1bafb]432int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
433 unsigned int mode)
[2ba7810]434{
[95319bd]435 /* Count forwarded calls */
[da1bafb]436 irq_spinlock_lock(&TASK->lock, true);
[a307beb]437 TASK->ipc_info.forwarded++;
[da1bafb]438 irq_spinlock_pass(&TASK->lock, &oldbox->lock);
[cfaa35a]439 list_remove(&call->ab_link);
[da1bafb]440 irq_spinlock_unlock(&oldbox->lock, true);
441
[645d9ed2]442 if (mode & IPC_FF_ROUTE_FROM_ME) {
[9201f47]443 call->data.phone = newphone;
[e2ab36f1]444 call->data.task_id = TASK->taskid;
[645d9ed2]445 }
[da1bafb]446
[9f22213]447 return ipc_call(newphone, call);
[6d9c49a]448}
449
450
[8b243f2]451/** Wait for a phone call.
[6d9c49a]452 *
[da1bafb]453 * @param box Answerbox expecting the call.
454 * @param usec Timeout in microseconds. See documentation for
455 * waitq_sleep_timeout() for decription of its special
456 * meaning.
457 * @param flags Select mode of sleep operation. See documentation for
458 * waitq_sleep_timeout() for description of its special
459 * meaning.
460 *
461 * @return Recived call structure or NULL.
462 *
[8b243f2]463 * To distinguish between a call and an answer, have a look at call->flags.
[da1bafb]464 *
[6d9c49a]465 */
[da1bafb]466call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
[6d9c49a]467{
468 call_t *request;
[aa028db]469 uint64_t irq_cnt = 0;
470 uint64_t answer_cnt = 0;
471 uint64_t call_cnt = 0;
[bd5a663]472 int rc;
[da1bafb]473
[bd5a663]474restart:
[116d1ef4]475 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]476 if (SYNCH_FAILED(rc))
477 return NULL;
[fbcfd458]478
[da1bafb]479 irq_spinlock_lock(&box->lock, true);
[5626277]480 if (!list_empty(&box->irq_notifs)) {
[be06914]481 /* Count received IRQ notification */
[da1bafb]482 irq_cnt++;
483
484 irq_spinlock_lock(&box->irq_lock, false);
485
[55b77d9]486 request = list_get_instance(list_first(&box->irq_notifs),
[cfaa35a]487 call_t, ab_link);
488 list_remove(&request->ab_link);
[da1bafb]489
490 irq_spinlock_unlock(&box->irq_lock, false);
[5626277]491 } else if (!list_empty(&box->answers)) {
[be06914]492 /* Count received answer */
[aa028db]493 answer_cnt++;
[da1bafb]494
[fbcfd458]495 /* Handle asynchronous answers */
[55b77d9]496 request = list_get_instance(list_first(&box->answers),
[cfaa35a]497 call_t, ab_link);
498 list_remove(&request->ab_link);
[5a77550]499 atomic_dec(&request->caller_phone->active_calls);
[fbcfd458]500 } else if (!list_empty(&box->calls)) {
[be06914]501 /* Count received call */
[aa028db]502 call_cnt++;
[da1bafb]503
[fbcfd458]504 /* Handle requests */
[55b77d9]505 request = list_get_instance(list_first(&box->calls),
[cfaa35a]506 call_t, ab_link);
507 list_remove(&request->ab_link);
[da1bafb]508
[fbcfd458]509 /* Append request to dispatch queue */
[cfaa35a]510 list_append(&request->ab_link, &box->dispatched_calls);
[fbcfd458]511 } else {
[874621f]512 /* This can happen regularly after ipc_cleanup */
[da1bafb]513 irq_spinlock_unlock(&box->lock, true);
[fbcfd458]514 goto restart;
[6d9c49a]515 }
[aa028db]516
[da1bafb]517 irq_spinlock_pass(&box->lock, &TASK->lock);
518
[be06914]519 TASK->ipc_info.irq_notif_received += irq_cnt;
520 TASK->ipc_info.answer_received += answer_cnt;
521 TASK->ipc_info.call_received += call_cnt;
[da1bafb]522
523 irq_spinlock_unlock(&TASK->lock, true);
524
[6d9c49a]525 return request;
526}
527
[8b243f2]528/** Answer all calls from list with EHANGUP answer.
529 *
[5d3ed34]530 * @param box Answerbox with the list.
[da1bafb]531 * @param lst Head of the list to be cleaned up.
[8b243f2]532 */
[5d3ed34]533void ipc_cleanup_call_list(answerbox_t *box, list_t *lst)
[ca687ad]534{
[5d3ed34]535 irq_spinlock_lock(&box->lock, true);
[ca687ad]536 while (!list_empty(lst)) {
[cfaa35a]537 call_t *call = list_get_instance(list_first(lst), call_t,
538 ab_link);
[da1bafb]539
[cfaa35a]540 list_remove(&call->ab_link);
[5d3ed34]541
542 irq_spinlock_unlock(&box->lock, true);
543
[466e95f7]544 if (lst == &box->calls)
545 SYSIPC_OP(request_process, call, box);
[716185d]546
[5d3ed34]547 ipc_data_t old = call->data;
[ca687ad]548 IPC_SET_RETVAL(call->data, EHANGUP);
[5d3ed34]549 answer_preprocess(call, &old);
[c713aa56]550 _ipc_answer_free_call(call, true);
[5d3ed34]551
552 irq_spinlock_lock(&box->lock, true);
[ca687ad]553 }
[5d3ed34]554 irq_spinlock_unlock(&box->lock, true);
[ca687ad]555}
556
[9a1b20c]557/** Disconnects all phones connected to an answerbox.
[4e49572]558 *
[da1bafb]559 * @param box Answerbox to disconnect phones from.
560 * @param notify_box If true, the answerbox will get a hangup message for
561 * each disconnected phone.
562 *
[4e49572]563 */
[9a1b20c]564void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
[4e49572]565{
[ca687ad]566 phone_t *phone;
[31d8e10]567 DEADLOCK_PROBE_INIT(p_phonelck);
[da1bafb]568
569 call_t *call = notify_box ? ipc_call_alloc(0) : NULL;
570
[ca687ad]571 /* Disconnect all phones connected to our answerbox */
572restart_phones:
[da1bafb]573 irq_spinlock_lock(&box->lock, true);
[9a1b20c]574 while (!list_empty(&box->connected_phones)) {
[55b77d9]575 phone = list_get_instance(list_first(&box->connected_phones),
[31d8e10]576 phone_t, link);
[ff48a15]577 if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
[da1bafb]578 irq_spinlock_unlock(&box->lock, true);
[31d8e10]579 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
[ca687ad]580 goto restart_phones;
581 }
582
583 /* Disconnect phone */
[eb3d379]584 ASSERT(phone->state == IPC_PHONE_CONNECTED);
[da1bafb]585
[c4e4507]586 list_remove(&phone->link);
[9a1b20c]587 phone->state = IPC_PHONE_SLAMMED;
[da1bafb]588
[9a1b20c]589 if (notify_box) {
590 mutex_unlock(&phone->lock);
[da1bafb]591 irq_spinlock_unlock(&box->lock, true);
[f1d5ef8]592
593 // FIXME: phone can become deallocated at any time now
594
[9a1b20c]595 /*
596 * Send one message to the answerbox for each
597 * phone. Used to make sure the kbox thread
598 * wakes up after the last phone has been
599 * disconnected.
600 */
[228e490]601 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[796692c]602 call->request_method = IPC_M_PHONE_HUNGUP;
[9a1b20c]603 call->flags |= IPC_CALL_DISCARD_ANSWER;
604 _ipc_call(phone, box, call);
[da1bafb]605
[9a1b20c]606 /* Allocate another call in advance */
607 call = ipc_call_alloc(0);
[da1bafb]608
[9a1b20c]609 /* Must start again */
610 goto restart_phones;
611 }
[da1bafb]612
[ff48a15]613 mutex_unlock(&phone->lock);
[ca687ad]614 }
[da1bafb]615
616 irq_spinlock_unlock(&box->lock, true);
617
[9a1b20c]618 /* Free unused call */
[119c335]619 if (call)
620 ipc_call_free(call);
[9a1b20c]621}
622
[2405bb5]623static void ipc_forget_all_active_calls(void)
624{
625 call_t *call;
626
627restart:
628 spinlock_lock(&TASK->active_calls_lock);
629 if (list_empty(&TASK->active_calls)) {
630 /*
631 * We are done, there are no more active calls.
632 * Nota bene: there may still be answers waiting for pick up.
633 */
634 spinlock_unlock(&TASK->active_calls_lock);
635 return;
636 }
637
638 call = list_get_instance(list_first(&TASK->active_calls), call_t,
639 ta_link);
640
641 if (!spinlock_trylock(&call->forget_lock)) {
642 /*
[20282ef3]643 * Avoid deadlock and let async_answer() or
644 * _ipc_answer_free_call() win the race to dequeue the first
645 * call on the list.
[2405bb5]646 */
647 spinlock_unlock(&TASK->active_calls_lock);
648 goto restart;
649 }
650
651 /*
652 * Forget the call and donate it to the task which holds up the answer.
653 */
654
655 call->forget = true;
656 call->sender = NULL;
657 list_remove(&call->ta_link);
658
[cd671c3]659 /*
660 * The call may be freed by _ipc_answer_free_call() before we are done
661 * with it; to avoid working with a destroyed call_t structure, we
662 * must hold a reference to it.
663 */
664 ipc_call_hold(call);
665
[2405bb5]666 spinlock_unlock(&call->forget_lock);
667 spinlock_unlock(&TASK->active_calls_lock);
[b1e6269]668
[5a77550]669 atomic_dec(&call->caller_phone->active_calls);
670
[466e95f7]671 SYSIPC_OP(request_forget, call);
[b1e6269]672
[cd671c3]673 ipc_call_release(call);
674
[2405bb5]675 goto restart;
676}
677
678/** Wait for all answers to asynchronous calls to arrive. */
679static void ipc_wait_for_all_answered_calls(void)
680{
681 call_t *call;
682 size_t i;
683
684restart:
685 /*
[d891cba]686 * Go through all phones, until they are all free.
687 * Locking is needed as there may be connection handshakes in progress.
[2405bb5]688 */
689 for (i = 0; i < IPC_MAX_PHONES; i++) {
[d891cba]690 phone_t *phone = &TASK->phones[i];
691
692 mutex_lock(&phone->lock);
693 if ((phone->state == IPC_PHONE_HUNGUP) &&
694 (atomic_get(&phone->active_calls) == 0)) {
695 phone->state = IPC_PHONE_FREE;
696 phone->callee = NULL;
[2405bb5]697 }
698
699 /*
[f9bd2e3]700 * We might have had some IPC_PHONE_CONNECTING phones at the
701 * beginning of ipc_cleanup(). Depending on whether these were
702 * forgotten or answered, they will eventually enter the
703 * IPC_PHONE_FREE or IPC_PHONE_CONNECTED states, respectively.
704 * In the latter case, the other side may slam the open phones
705 * at any time, in which case we will get an IPC_PHONE_SLAMMED
706 * phone.
[2405bb5]707 */
[d891cba]708 if ((phone->state == IPC_PHONE_CONNECTED) ||
709 (phone->state == IPC_PHONE_SLAMMED)) {
710 mutex_unlock(&phone->lock);
711 ipc_phone_hangup(phone);
[8f6858d0]712 /*
[f9bd2e3]713 * Now there may be one extra active call, which needs
714 * to be forgotten.
[8f6858d0]715 */
716 ipc_forget_all_active_calls();
717 goto restart;
718 }
[2405bb5]719
720 /*
721 * If the hangup succeeded, it has sent a HANGUP message, the
722 * IPC is now in HUNGUP state, we wait for the reply to come
723 */
[d891cba]724 if (phone->state != IPC_PHONE_FREE) {
725 mutex_unlock(&phone->lock);
[2405bb5]726 break;
[d891cba]727 }
728
729 mutex_unlock(&phone->lock);
[2405bb5]730 }
731
732 /* Got into cleanup */
733 if (i == IPC_MAX_PHONES)
734 return;
735
736 call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
737 SYNCH_FLAGS_NONE);
[525e91b]738 ASSERT(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
[675fcbd]739
[466e95f7]740 SYSIPC_OP(answer_process, call);
[675fcbd]741
[2405bb5]742 ipc_call_free(call);
743 goto restart;
744}
745
[da1bafb]746/** Clean up all IPC communication of the current task.
[9a1b20c]747 *
748 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
749 * have to change it as well if you want to cleanup other tasks than TASK.
[da1bafb]750 *
[9a1b20c]751 */
752void ipc_cleanup(void)
753{
[c33f39f]754 /*
755 * Mark the answerbox as inactive.
756 *
757 * The main purpose for doing this is to prevent any pending callback
758 * connections from getting established beyond this point.
759 */
760 irq_spinlock_lock(&TASK->answerbox.lock, true);
761 TASK->answerbox.active = false;
762 irq_spinlock_unlock(&TASK->answerbox.lock, true);
763
[9a1b20c]764 /* Disconnect all our phones ('ipc_phone_hangup') */
[2405bb5]765 for (size_t i = 0; i < IPC_MAX_PHONES; i++)
[9a1b20c]766 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]767
[05641a9e]768 /* Unsubscribe from any event notifications. */
769 event_cleanup_answerbox(&TASK->answerbox);
[da1bafb]770
[9a1b20c]771 /* Disconnect all connected irqs */
772 ipc_irq_cleanup(&TASK->answerbox);
[da1bafb]773
[9a1b20c]774 /* Disconnect all phones connected to our regular answerbox */
775 ipc_answerbox_slam_phones(&TASK->answerbox, false);
[da1bafb]776
[9a1b20c]777#ifdef CONFIG_UDEBUG
778 /* Clean up kbox thread and communications */
779 ipc_kbox_cleanup();
780#endif
[da1bafb]781
[9f22213]782 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[716185d]783 ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
[5d3ed34]784 ipc_cleanup_call_list(&TASK->answerbox,
785 &TASK->answerbox.dispatched_calls);
[bc117a5]786
[2405bb5]787 ipc_forget_all_active_calls();
788 ipc_wait_for_all_answered_calls();
[4e49572]789}
[5626277]790
[da1bafb]791/** Initilize IPC subsystem
792 *
793 */
[5626277]794void ipc_init(void)
795{
[f97f1e51]796 ipc_call_slab = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
[8b243f2]797 NULL, 0);
[f97f1e51]798 ipc_answerbox_slab = slab_cache_create("answerbox_t",
[c70ce74]799 sizeof(answerbox_t), 0, NULL, NULL, 0);
[5626277]800}
801
[9c9903a]802
803static void ipc_print_call_list(list_t *list)
804{
[feeac0d]805 list_foreach(*list, ab_link, call_t, call) {
[9c9903a]806#ifdef __32_BITS__
807 printf("%10p ", call);
808#endif
809
810#ifdef __64_BITS__
811 printf("%18p ", call);
812#endif
813
814 spinlock_lock(&call->forget_lock);
815
816 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
817 " %-6" PRIun " %-6" PRIun " %-7x",
818 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
819 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
820 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
821 call->flags);
822
823 if (call->forget) {
824 printf(" ? (call forgotten)\n");
825 } else {
826 printf(" %" PRIu64 " (%s)\n",
827 call->sender->taskid, call->sender->name);
828 }
829
830 spinlock_unlock(&call->forget_lock);
831 }
832}
833
[8b243f2]834/** List answerbox contents.
835 *
[da1bafb]836 * @param taskid Task ID.
837 *
[8b243f2]838 */
[c4e4507]839void ipc_print_task(task_id_t taskid)
840{
[da1bafb]841 irq_spinlock_lock(&tasks_lock, true);
842 task_t *task = task_find_by_id(taskid);
843
[170332d]844 if (!task) {
[da1bafb]845 irq_spinlock_unlock(&tasks_lock, true);
[c4e4507]846 return;
[170332d]847 }
[da1bafb]848
849 /* Hand-over-hand locking */
850 irq_spinlock_exchange(&tasks_lock, &task->lock);
851
[5378f99]852 printf("[phone id] [calls] [state\n");
[da1bafb]853
854 size_t i;
[8b243f2]855 for (i = 0; i < IPC_MAX_PHONES; i++) {
[ff48a15]856 if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
[5378f99]857 printf("%-10zu (mutex busy)\n", i);
[ff48a15]858 continue;
859 }
[da1bafb]860
[c4e4507]861 if (task->phones[i].state != IPC_PHONE_FREE) {
[5378f99]862 printf("%-10zu %7" PRIun " ", i,
863 atomic_get(&task->phones[i].active_calls));
[da1bafb]864
[c4e4507]865 switch (task->phones[i].state) {
866 case IPC_PHONE_CONNECTING:
[5378f99]867 printf("connecting");
[c4e4507]868 break;
869 case IPC_PHONE_CONNECTED:
[5378f99]870 printf("connected to %" PRIu64 " (%s)",
871 task->phones[i].callee->task->taskid,
872 task->phones[i].callee->task->name);
[c4e4507]873 break;
874 case IPC_PHONE_SLAMMED:
[5378f99]875 printf("slammed by %p",
[da1bafb]876 task->phones[i].callee);
[c4e4507]877 break;
878 case IPC_PHONE_HUNGUP:
[5378f99]879 printf("hung up by %p",
[da1bafb]880 task->phones[i].callee);
[c4e4507]881 break;
882 default:
883 break;
884 }
[da1bafb]885
[5378f99]886 printf("\n");
[c4e4507]887 }
[da1bafb]888
[ff48a15]889 mutex_unlock(&task->phones[i].lock);
[c4e4507]890 }
[da1bafb]891
892 irq_spinlock_lock(&task->answerbox.lock, false);
893
[5378f99]894#ifdef __32_BITS__
895 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
896 " [flags] [sender\n");
897#endif
898
899#ifdef __64_BITS__
900 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
901 " [arg5] [flags] [sender\n");
902#endif
903
904 printf(" --- incomming calls ---\n");
[9c9903a]905 ipc_print_call_list(&task->answerbox.calls);
[5378f99]906 printf(" --- dispatched calls ---\n");
[9c9903a]907 ipc_print_call_list(&task->answerbox.dispatched_calls);
[6aef742]908 printf(" --- incoming answers ---\n");
[9c9903a]909 ipc_print_call_list(&task->answerbox.answers);
[da1bafb]910
911 irq_spinlock_unlock(&task->answerbox.lock, false);
912 irq_spinlock_unlock(&task->lock, true);
[c4e4507]913}
[b45c443]914
[cc73a8a1]915/** @}
[b45c443]916 */
Note: See TracBrowser for help on using the repository browser.