source: mainline/kernel/generic/src/ipc/ipc.c@ 8c9b742

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

Change the client task hash only when making the call or when forwarding
it with IPC_FF_ROUTE_FROM_ME flag.

  • Property mode set to 100644
File size: 21.1 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
[ff48a15]40#include <synch/synch.h>
[2ba7810]41#include <synch/spinlock.h>
[ff48a15]42#include <synch/mutex.h>
[2ba7810]43#include <synch/waitq.h>
[bd5a663]44#include <synch/synch.h>
[6d9c49a]45#include <ipc/ipc.h>
[e028660]46#include <ipc/kbox.h>
[13a638d]47#include <ipc/event.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);
[ba81cab]74 call->callerbox = &TASK->answerbox;
75 call->sender = TASK;
[7918fce]76 call->buffer = NULL;
[ba81cab]77}
78
[8b243f2]79/** Allocate and initialize a call structure.
[da1bafb]80 *
[8b243f2]81 * The call is initialized, so that the reply will be directed to
82 * TASK->answerbox.
83 *
[da1bafb]84 * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
85 *
86 * @return If flags permit it, return NULL, or initialized kernel
87 * call structure.
[5626277]88 *
[6d9c49a]89 */
[da1bafb]90call_t *ipc_call_alloc(unsigned int flags)
[6d9c49a]91{
[da1bafb]92 call_t *call = slab_alloc(ipc_call_slab, flags);
[69eac4aa]93 if (call)
94 _ipc_call_init(call);
[da1bafb]95
[6d9c49a]96 return call;
97}
98
[bd72c3e9]99/** Deallocate a call structure.
[8b243f2]100 *
[da1bafb]101 * @param call Call structure to be freed.
102 *
[8b243f2]103 */
[6d9c49a]104void ipc_call_free(call_t *call)
105{
[7918fce]106 /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */
107 if (call->buffer)
108 free(call->buffer);
[6d9c49a]109 slab_free(ipc_call_slab, call);
110}
111
[8b243f2]112/** Initialize an answerbox structure.
113 *
[da1bafb]114 * @param box Answerbox structure to be initialized.
115 * @param task Task to which the answerbox belongs.
116 *
[6d9c49a]117 */
[12ab886]118void ipc_answerbox_init(answerbox_t *box, task_t *task)
[6d9c49a]119{
[da1bafb]120 irq_spinlock_initialize(&box->lock, "ipc.box.lock");
121 irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
[2ba7810]122 waitq_initialize(&box->wq);
[33adc6ce]123 link_initialize(&box->sync_box_link);
[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);
[b14e35f2]129 list_initialize(&box->irq_head);
[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
[8b243f2]166/** Helper function to facilitate synchronous calls.
167 *
[da1bafb]168 * @param phone Destination kernel phone structure.
169 * @param request Call structure with request.
170 *
171 * @return EOK on success or EINTR if the sleep was interrupted.
[79872cd]172 *
[8b243f2]173 */
[79872cd]174int ipc_call_sync(phone_t *phone, call_t *request)
[631ca4d]175{
[da1bafb]176 answerbox_t *sync_box = slab_alloc(ipc_answerbox_slab, 0);
[c70ce74]177 ipc_answerbox_init(sync_box, TASK);
[da1bafb]178
[33adc6ce]179 /*
180 * Put the answerbox on the TASK's list of synchronous answerboxes so
181 * that it can be cleaned up if the call is interrupted.
182 */
[da1bafb]183 irq_spinlock_lock(&TASK->lock, true);
[33adc6ce]184 list_append(&sync_box->sync_box_link, &TASK->sync_box_head);
[da1bafb]185 irq_spinlock_unlock(&TASK->lock, true);
186
[8b243f2]187 /* We will receive data in a special box. */
[c70ce74]188 request->callerbox = sync_box;
[da1bafb]189
[631ca4d]190 ipc_call(phone, request);
[c70ce74]191 if (!ipc_wait_for_call(sync_box, SYNCH_NO_TIMEOUT,
192 SYNCH_FLAGS_INTERRUPTIBLE)) {
[da1bafb]193 /* The answerbox and the call will be freed by ipc_cleanup(). */
[79872cd]194 return EINTR;
[c70ce74]195 }
[da1bafb]196
[33adc6ce]197 /*
198 * The answer arrived without interruption so we can remove the
199 * answerbox from the TASK's list of synchronous answerboxes.
200 */
[da1bafb]201 irq_spinlock_lock(&TASK->lock, true);
[33adc6ce]202 list_remove(&sync_box->sync_box_link);
[da1bafb]203 irq_spinlock_unlock(&TASK->lock, true);
204
[c70ce74]205 slab_free(ipc_answerbox_slab, sync_box);
[79872cd]206 return EOK;
[631ca4d]207}
[6d9c49a]208
[8b243f2]209/** Answer a message which was not dispatched and is not listed in any queue.
210 *
[da1bafb]211 * @param call Call structure to be answered.
212 * @param selflocked If true, then TASK->answebox is locked.
213 *
[ba81cab]214 */
[c713aa56]215static void _ipc_answer_free_call(call_t *call, bool selflocked)
[ba81cab]216{
217 answerbox_t *callerbox = call->callerbox;
[c713aa56]218 bool do_lock = ((!selflocked) || callerbox != (&TASK->answerbox));
[da1bafb]219
[95319bd]220 /* Count sent answer */
[da1bafb]221 irq_spinlock_lock(&TASK->lock, true);
[a307beb]222 TASK->ipc_info.answer_sent++;
[da1bafb]223 irq_spinlock_unlock(&TASK->lock, true);
224
[ba81cab]225 call->flags |= IPC_CALL_ANSWERED;
[da1bafb]226
[74965d2]227 if (call->flags & IPC_CALL_FORWARDED) {
[27526e87]228 if (call->caller_phone) {
[74965d2]229 /* Demasquerade the caller phone. */
[27526e87]230 call->data.phone = call->caller_phone;
[74965d2]231 }
232 }
[da1bafb]233
[c713aa56]234 if (do_lock)
[da1bafb]235 irq_spinlock_lock(&callerbox->lock, true);
236
[c4e4507]237 list_append(&call->link, &callerbox->answers);
[da1bafb]238
[c713aa56]239 if (do_lock)
[da1bafb]240 irq_spinlock_unlock(&callerbox->lock, true);
241
[5c8ba05]242 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
[ba81cab]243}
244
[8b243f2]245/** Answer a message which is in a callee queue.
[ba81cab]246 *
[da1bafb]247 * @param box Answerbox that is answering the message.
248 * @param call Modified request that is being sent back.
249 *
[ba81cab]250 */
251void ipc_answer(answerbox_t *box, call_t *call)
252{
253 /* Remove from active box */
[da1bafb]254 irq_spinlock_lock(&box->lock, true);
[c4e4507]255 list_remove(&call->link);
[da1bafb]256 irq_spinlock_unlock(&box->lock, true);
257
[ba81cab]258 /* Send back answer */
[c713aa56]259 _ipc_answer_free_call(call, false);
[ba81cab]260}
261
[8b243f2]262/** Simulate sending back a message.
[7c7aae16]263 *
264 * Most errors are better handled by forming a normal backward
265 * message and sending it as a normal answer.
[8b243f2]266 *
[da1bafb]267 * @param phone Phone structure the call should appear to come from.
268 * @param call Call structure to be answered.
269 * @param err Return value to be used for the answer.
270 *
[7c7aae16]271 */
[96b02eb9]272void ipc_backsend_err(phone_t *phone, call_t *call, sysarg_t err)
[7c7aae16]273{
274 call->data.phone = phone;
275 atomic_inc(&phone->active_calls);
[eb3d379]276 IPC_SET_RETVAL(call->data, err);
[c713aa56]277 _ipc_answer_free_call(call, false);
[7c7aae16]278}
279
[8b243f2]280/** Unsafe unchecking version of ipc_call.
281 *
[da1bafb]282 * @param phone Phone structure the call comes from.
283 * @param box Destination answerbox structure.
284 * @param call Call structure with request.
285 *
[8b243f2]286 */
[fbcfd458]287static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
288{
[95319bd]289 /* Count sent ipc call */
[da1bafb]290 irq_spinlock_lock(&TASK->lock, true);
[a307beb]291 TASK->ipc_info.call_sent++;
[da1bafb]292 irq_spinlock_unlock(&TASK->lock, true);
293
[8b243f2]294 if (!(call->flags & IPC_CALL_FORWARDED)) {
[9f22213]295 atomic_inc(&phone->active_calls);
296 call->data.phone = phone;
[8c9b742]297 call->data.task = TASK;
[9f22213]298 }
[da1bafb]299
300 irq_spinlock_lock(&box->lock, true);
[c4e4507]301 list_append(&call->link, &box->calls);
[da1bafb]302 irq_spinlock_unlock(&box->lock, true);
303
[5c8ba05]304 waitq_wakeup(&box->wq, WAKEUP_FIRST);
[fbcfd458]305}
306
[8b243f2]307/** Send an asynchronous request using a phone to an answerbox.
308 *
[da1bafb]309 * @param phone Phone structure the call comes from and which is
310 * connected to the destination answerbox.
311 * @param call Call structure with request.
312 *
313 * @return Return 0 on success, ENOENT on error.
[6d9c49a]314 *
315 */
[9f22213]316int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]317{
[ff48a15]318 mutex_lock(&phone->lock);
[eb3d379]319 if (phone->state != IPC_PHONE_CONNECTED) {
[ff48a15]320 mutex_unlock(&phone->lock);
[9f22213]321 if (call->flags & IPC_CALL_FORWARDED) {
322 IPC_SET_RETVAL(call->data, EFORWARD);
[c713aa56]323 _ipc_answer_free_call(call, false);
[eb3d379]324 } else {
325 if (phone->state == IPC_PHONE_HUNGUP)
[7c7aae16]326 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]327 else
[7c7aae16]328 ipc_backsend_err(phone, call, ENOENT);
[9f22213]329 }
[da1bafb]330
[9f22213]331 return ENOENT;
[ba81cab]332 }
[da1bafb]333
334 answerbox_t *box = phone->callee;
[fbcfd458]335 _ipc_call(phone, box, call);
336
[ff48a15]337 mutex_unlock(&phone->lock);
[9f22213]338 return 0;
[fbcfd458]339}
340
[8b243f2]341/** Disconnect phone from answerbox.
[fbcfd458]342 *
[8b243f2]343 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
[eb3d379]344 * lazily later.
[fbcfd458]345 *
[da1bafb]346 * @param phone Phone structure to be hung up.
347 *
348 * @return 0 if the phone is disconnected.
349 * @return -1 if the phone was already disconnected.
350 *
[fbcfd458]351 */
[d8f7362]352int ipc_phone_hangup(phone_t *phone)
[fbcfd458]353{
[ff48a15]354 mutex_lock(&phone->lock);
[7918fce]355 if (phone->state == IPC_PHONE_FREE ||
356 phone->state == IPC_PHONE_HUNGUP ||
[8b243f2]357 phone->state == IPC_PHONE_CONNECTING) {
[ff48a15]358 mutex_unlock(&phone->lock);
[eb3d379]359 return -1;
[fbcfd458]360 }
[da1bafb]361
362 answerbox_t *box = phone->callee;
[eb3d379]363 if (phone->state != IPC_PHONE_SLAMMED) {
364 /* Remove myself from answerbox */
[da1bafb]365 irq_spinlock_lock(&box->lock, true);
[c4e4507]366 list_remove(&phone->link);
[da1bafb]367 irq_spinlock_unlock(&box->lock, true);
368
369 call_t *call = ipc_call_alloc(0);
[228e490]370 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[287e83f]371 call->flags |= IPC_CALL_DISCARD_ANSWER;
372 _ipc_call(phone, box, call);
[eb3d379]373 }
[da1bafb]374
[eb3d379]375 phone->state = IPC_PHONE_HUNGUP;
[ff48a15]376 mutex_unlock(&phone->lock);
[da1bafb]377
[fbcfd458]378 return 0;
[2ba7810]379}
380
[8b243f2]381/** Forwards call from one answerbox to another one.
[2ba7810]382 *
[da1bafb]383 * @param call Call structure to be redirected.
384 * @param newphone Phone structure to target answerbox.
385 * @param oldbox Old answerbox structure.
386 * @param mode Flags that specify mode of the forward operation.
387 *
388 * @return 0 if forwarding succeeded or an error code if
389 * there was an error.
[8b243f2]390 *
391 * The return value serves only as an information for the forwarder,
392 * the original caller is notified automatically with EFORWARD.
[da1bafb]393 *
[2ba7810]394 */
[da1bafb]395int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
396 unsigned int mode)
[2ba7810]397{
[95319bd]398 /* Count forwarded calls */
[da1bafb]399 irq_spinlock_lock(&TASK->lock, true);
[a307beb]400 TASK->ipc_info.forwarded++;
[da1bafb]401 irq_spinlock_pass(&TASK->lock, &oldbox->lock);
[c4e4507]402 list_remove(&call->link);
[da1bafb]403 irq_spinlock_unlock(&oldbox->lock, true);
404
[645d9ed2]405 if (mode & IPC_FF_ROUTE_FROM_ME) {
[27526e87]406 if (!call->caller_phone)
407 call->caller_phone = call->data.phone;
[9201f47]408 call->data.phone = newphone;
[8c9b742]409 call->data.task = TASK;
[645d9ed2]410 }
[da1bafb]411
[9f22213]412 return ipc_call(newphone, call);
[6d9c49a]413}
414
415
[8b243f2]416/** Wait for a phone call.
[6d9c49a]417 *
[da1bafb]418 * @param box Answerbox expecting the call.
419 * @param usec Timeout in microseconds. See documentation for
420 * waitq_sleep_timeout() for decription of its special
421 * meaning.
422 * @param flags Select mode of sleep operation. See documentation for
423 * waitq_sleep_timeout() for description of its special
424 * meaning.
425 *
426 * @return Recived call structure or NULL.
427 *
[8b243f2]428 * To distinguish between a call and an answer, have a look at call->flags.
[da1bafb]429 *
[6d9c49a]430 */
[da1bafb]431call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
[6d9c49a]432{
433 call_t *request;
[aa028db]434 uint64_t irq_cnt = 0;
435 uint64_t answer_cnt = 0;
436 uint64_t call_cnt = 0;
[bd5a663]437 int rc;
[da1bafb]438
[bd5a663]439restart:
[116d1ef4]440 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]441 if (SYNCH_FAILED(rc))
442 return NULL;
[fbcfd458]443
[da1bafb]444 irq_spinlock_lock(&box->lock, true);
[5626277]445 if (!list_empty(&box->irq_notifs)) {
[be06914]446 /* Count received IRQ notification */
[da1bafb]447 irq_cnt++;
448
449 irq_spinlock_lock(&box->irq_lock, false);
450
[c4e4507]451 request = list_get_instance(box->irq_notifs.next, call_t, link);
452 list_remove(&request->link);
[da1bafb]453
454 irq_spinlock_unlock(&box->irq_lock, false);
[5626277]455 } else if (!list_empty(&box->answers)) {
[be06914]456 /* Count received answer */
[aa028db]457 answer_cnt++;
[da1bafb]458
[fbcfd458]459 /* Handle asynchronous answers */
[c4e4507]460 request = list_get_instance(box->answers.next, call_t, link);
461 list_remove(&request->link);
[74965d2]462 atomic_dec(&request->data.phone->active_calls);
[fbcfd458]463 } else if (!list_empty(&box->calls)) {
[be06914]464 /* Count received call */
[aa028db]465 call_cnt++;
[da1bafb]466
[fbcfd458]467 /* Handle requests */
[c4e4507]468 request = list_get_instance(box->calls.next, call_t, link);
469 list_remove(&request->link);
[da1bafb]470
[fbcfd458]471 /* Append request to dispatch queue */
[c4e4507]472 list_append(&request->link, &box->dispatched_calls);
[fbcfd458]473 } else {
[874621f]474 /* This can happen regularly after ipc_cleanup */
[da1bafb]475 irq_spinlock_unlock(&box->lock, true);
[fbcfd458]476 goto restart;
[6d9c49a]477 }
[aa028db]478
[da1bafb]479 irq_spinlock_pass(&box->lock, &TASK->lock);
480
[be06914]481 TASK->ipc_info.irq_notif_received += irq_cnt;
482 TASK->ipc_info.answer_received += answer_cnt;
483 TASK->ipc_info.call_received += call_cnt;
[da1bafb]484
485 irq_spinlock_unlock(&TASK->lock, true);
486
[6d9c49a]487 return request;
488}
489
[8b243f2]490/** Answer all calls from list with EHANGUP answer.
491 *
[da1bafb]492 * @param lst Head of the list to be cleaned up.
493 *
[8b243f2]494 */
[9a1b20c]495void ipc_cleanup_call_list(link_t *lst)
[ca687ad]496{
497 while (!list_empty(lst)) {
[da1bafb]498 call_t *call = list_get_instance(lst->next, call_t, link);
[a55d5f9f]499 if (call->buffer)
500 free(call->buffer);
[da1bafb]501
[c4e4507]502 list_remove(&call->link);
[da1bafb]503
[ca687ad]504 IPC_SET_RETVAL(call->data, EHANGUP);
[c713aa56]505 _ipc_answer_free_call(call, true);
[ca687ad]506 }
507}
508
[9a1b20c]509/** Disconnects all phones connected to an answerbox.
[4e49572]510 *
[da1bafb]511 * @param box Answerbox to disconnect phones from.
512 * @param notify_box If true, the answerbox will get a hangup message for
513 * each disconnected phone.
514 *
[4e49572]515 */
[9a1b20c]516void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
[4e49572]517{
[ca687ad]518 phone_t *phone;
[31d8e10]519 DEADLOCK_PROBE_INIT(p_phonelck);
[da1bafb]520
521 call_t *call = notify_box ? ipc_call_alloc(0) : NULL;
522
[ca687ad]523 /* Disconnect all phones connected to our answerbox */
524restart_phones:
[da1bafb]525 irq_spinlock_lock(&box->lock, true);
[9a1b20c]526 while (!list_empty(&box->connected_phones)) {
527 phone = list_get_instance(box->connected_phones.next,
[31d8e10]528 phone_t, link);
[ff48a15]529 if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
[da1bafb]530 irq_spinlock_unlock(&box->lock, true);
[31d8e10]531 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
[ca687ad]532 goto restart_phones;
533 }
534
535 /* Disconnect phone */
[eb3d379]536 ASSERT(phone->state == IPC_PHONE_CONNECTED);
[da1bafb]537
[c4e4507]538 list_remove(&phone->link);
[9a1b20c]539 phone->state = IPC_PHONE_SLAMMED;
[da1bafb]540
[9a1b20c]541 if (notify_box) {
542 mutex_unlock(&phone->lock);
[da1bafb]543 irq_spinlock_unlock(&box->lock, true);
544
[9a1b20c]545 /*
546 * Send one message to the answerbox for each
547 * phone. Used to make sure the kbox thread
548 * wakes up after the last phone has been
549 * disconnected.
550 */
[228e490]551 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[9a1b20c]552 call->flags |= IPC_CALL_DISCARD_ANSWER;
553 _ipc_call(phone, box, call);
[da1bafb]554
[9a1b20c]555 /* Allocate another call in advance */
556 call = ipc_call_alloc(0);
[da1bafb]557
[9a1b20c]558 /* Must start again */
559 goto restart_phones;
560 }
[da1bafb]561
[ff48a15]562 mutex_unlock(&phone->lock);
[ca687ad]563 }
[da1bafb]564
565 irq_spinlock_unlock(&box->lock, true);
566
[9a1b20c]567 /* Free unused call */
[119c335]568 if (call)
569 ipc_call_free(call);
[9a1b20c]570}
571
[da1bafb]572/** Clean up all IPC communication of the current task.
[9a1b20c]573 *
574 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
575 * have to change it as well if you want to cleanup other tasks than TASK.
[da1bafb]576 *
[9a1b20c]577 */
578void ipc_cleanup(void)
579{
580 /* Disconnect all our phones ('ipc_phone_hangup') */
[da1bafb]581 size_t i;
[9a1b20c]582 for (i = 0; i < IPC_MAX_PHONES; i++)
583 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]584
[05641a9e]585 /* Unsubscribe from any event notifications. */
586 event_cleanup_answerbox(&TASK->answerbox);
[da1bafb]587
[9a1b20c]588 /* Disconnect all connected irqs */
589 ipc_irq_cleanup(&TASK->answerbox);
[da1bafb]590
[9a1b20c]591 /* Disconnect all phones connected to our regular answerbox */
592 ipc_answerbox_slam_phones(&TASK->answerbox, false);
[da1bafb]593
[9a1b20c]594#ifdef CONFIG_UDEBUG
595 /* Clean up kbox thread and communications */
596 ipc_kbox_cleanup();
597#endif
[da1bafb]598
[9f22213]599 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[da1bafb]600 irq_spinlock_lock(&TASK->answerbox.lock, true);
[214c5a0]601 ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
602 ipc_cleanup_call_list(&TASK->answerbox.calls);
[da1bafb]603 irq_spinlock_unlock(&TASK->answerbox.lock, true);
[4e49572]604
[33adc6ce]605 /* Wait for all answers to interrupted synchronous calls to arrive */
[da1bafb]606 ipl_t ipl = interrupts_disable();
[33adc6ce]607 while (!list_empty(&TASK->sync_box_head)) {
608 answerbox_t *box = list_get_instance(TASK->sync_box_head.next,
609 answerbox_t, sync_box_link);
[da1bafb]610
[33adc6ce]611 list_remove(&box->sync_box_link);
[da1bafb]612 call_t *call = ipc_wait_for_call(box, SYNCH_NO_TIMEOUT,
[33adc6ce]613 SYNCH_FLAGS_NONE);
614 ipc_call_free(call);
615 slab_free(ipc_answerbox_slab, box);
616 }
617 interrupts_restore(ipl);
[da1bafb]618
[33adc6ce]619 /* Wait for all answers to asynchronous calls to arrive */
[da1bafb]620 while (true) {
621 /*
622 * Go through all phones, until they are all FREE
623 * Locking is not needed, no one else should modify
624 * it when we are in cleanup
625 */
[8b243f2]626 for (i = 0; i < IPC_MAX_PHONES; i++) {
627 if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
[e701eb1]628 atomic_get(&TASK->phones[i].active_calls) == 0) {
[214c5a0]629 TASK->phones[i].state = IPC_PHONE_FREE;
[e701eb1]630 TASK->phones[i].callee = NULL;
631 }
[214c5a0]632
[da1bafb]633 /*
634 * Just for sure, we might have had some
635 * IPC_PHONE_CONNECTING phones
636 */
[214c5a0]637 if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
[d8f7362]638 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]639
640 /*
641 * If the hangup succeeded, it has sent a HANGUP
[d8f7362]642 * message, the IPC is now in HUNGUP state, we
[da1bafb]643 * wait for the reply to come
644 */
[214c5a0]645
646 if (TASK->phones[i].state != IPC_PHONE_FREE)
[eb3d379]647 break;
648 }
[da1bafb]649
650 /* Got into cleanup */
[eb3d379]651 if (i == IPC_MAX_PHONES)
652 break;
653
[da1bafb]654 call_t *call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
[8b243f2]655 SYNCH_FLAGS_NONE);
656 ASSERT((call->flags & IPC_CALL_ANSWERED) ||
657 (call->flags & IPC_CALL_NOTIF));
[ca687ad]658
659 ipc_call_free(call);
660 }
[4e49572]661}
[5626277]662
[da1bafb]663/** Initilize IPC subsystem
664 *
665 */
[5626277]666void ipc_init(void)
667{
[8b243f2]668 ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
669 NULL, 0);
[c70ce74]670 ipc_answerbox_slab = slab_cache_create("ipc_answerbox",
671 sizeof(answerbox_t), 0, NULL, NULL, 0);
[5626277]672}
673
[8b243f2]674/** List answerbox contents.
675 *
[da1bafb]676 * @param taskid Task ID.
677 *
[8b243f2]678 */
[c4e4507]679void ipc_print_task(task_id_t taskid)
680{
[da1bafb]681 irq_spinlock_lock(&tasks_lock, true);
682 task_t *task = task_find_by_id(taskid);
683
[170332d]684 if (!task) {
[da1bafb]685 irq_spinlock_unlock(&tasks_lock, true);
[c4e4507]686 return;
[170332d]687 }
[da1bafb]688
689 /* Hand-over-hand locking */
690 irq_spinlock_exchange(&tasks_lock, &task->lock);
691
[c4e4507]692 /* Print opened phones & details */
693 printf("PHONE:\n");
[da1bafb]694
695 size_t i;
[8b243f2]696 for (i = 0; i < IPC_MAX_PHONES; i++) {
[ff48a15]697 if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
[7e752b2]698 printf("%zu: mutex busy\n", i);
[ff48a15]699 continue;
700 }
[da1bafb]701
[c4e4507]702 if (task->phones[i].state != IPC_PHONE_FREE) {
[7e752b2]703 printf("%zu: ", i);
[da1bafb]704
[c4e4507]705 switch (task->phones[i].state) {
706 case IPC_PHONE_CONNECTING:
707 printf("connecting ");
708 break;
709 case IPC_PHONE_CONNECTED:
[e186eb5]710 printf("connected to: %p (%" PRIu64 ") ",
711 task->phones[i].callee,
712 task->phones[i].callee->task->taskid);
[c4e4507]713 break;
714 case IPC_PHONE_SLAMMED:
[fbf7b4c]715 printf("slammed by: %p ",
[da1bafb]716 task->phones[i].callee);
[c4e4507]717 break;
718 case IPC_PHONE_HUNGUP:
[fbf7b4c]719 printf("hung up - was: %p ",
[da1bafb]720 task->phones[i].callee);
[c4e4507]721 break;
722 default:
723 break;
724 }
[da1bafb]725
726 printf("active: %" PRIun "\n",
[bd72c3e9]727 atomic_get(&task->phones[i].active_calls));
[c4e4507]728 }
[da1bafb]729
[ff48a15]730 mutex_unlock(&task->phones[i].lock);
[c4e4507]731 }
[da1bafb]732
733 irq_spinlock_lock(&task->answerbox.lock, false);
734
735 link_t *cur;
736
[c4e4507]737 /* Print answerbox - calls */
738 printf("ABOX - CALLS:\n");
[da1bafb]739 for (cur = task->answerbox.calls.next; cur != &task->answerbox.calls;
740 cur = cur->next) {
741 call_t *call = list_get_instance(cur, call_t, link);
[9fe962d6]742 printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
[e40e3007]743 " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
744 " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
745 call->sender->taskid,
[228e490]746 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]747 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]748 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]749 call->flags);
[c4e4507]750 }
[da1bafb]751
752 /* Print answerbox - dispatched calls */
[c4e4507]753 printf("ABOX - DISPATCHED CALLS:\n");
[da1bafb]754 for (cur = task->answerbox.dispatched_calls.next;
755 cur != &task->answerbox.dispatched_calls;
756 cur = cur->next) {
757 call_t *call = list_get_instance(cur, call_t, link);
[9fe962d6]758 printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
[e40e3007]759 " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
760 " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
761 call->sender->taskid,
[228e490]762 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]763 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]764 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]765 call->flags);
[c4e4507]766 }
[da1bafb]767
768 /* Print answerbox - answers */
[c4e4507]769 printf("ABOX - ANSWERS:\n");
[da1bafb]770 for (cur = task->answerbox.answers.next;
771 cur != &task->answerbox.answers;
772 cur = cur->next) {
773 call_t *call = list_get_instance(cur, call_t, link);
[9fe962d6]774 printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun
[e40e3007]775 " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n",
[228e490]776 call, IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]777 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]778 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]779 call->flags);
[c4e4507]780 }
[da1bafb]781
782 irq_spinlock_unlock(&task->answerbox.lock, false);
783 irq_spinlock_unlock(&task->lock, true);
[c4e4507]784}
[b45c443]785
[cc73a8a1]786/** @}
[b45c443]787 */
Note: See TracBrowser for help on using the repository browser.