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

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

Add two new sysipc_ops_t members:

  • request_forget()
  • answer_cleanup()

Call these members to perform cleanup at appropriate times.

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