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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c0699467 was c0699467, checked in by Martin Decky <martin@…>, 14 years ago

do not provide general access to kernel headers from uspace, only allow specific headers to be accessed or shared
externalize headers which serve as kernel/uspace API/ABI into a special tree

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