source: mainline/kernel/generic/src/ipc/ipc.c@ 3cc070d

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

Make IPC_M_CONNECT_TO_ME more consistent with IPC_M_CONNECT_TO_ME.

  • Instead of passing the task ID of the connecting task in IPC argument 3 and 4, pass it in ipc_call_t::in_task_id.
  • Actually, all answers are signed by the answering task ID like this.
  • 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 }
[ab34cc9]232
233 call->data.task_id = TASK->taskid;
[da1bafb]234
[c713aa56]235 if (do_lock)
[da1bafb]236 irq_spinlock_lock(&callerbox->lock, true);
237
[c4e4507]238 list_append(&call->link, &callerbox->answers);
[da1bafb]239
[c713aa56]240 if (do_lock)
[da1bafb]241 irq_spinlock_unlock(&callerbox->lock, true);
242
[5c8ba05]243 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
[ba81cab]244}
245
[8b243f2]246/** Answer a message which is in a callee queue.
[ba81cab]247 *
[da1bafb]248 * @param box Answerbox that is answering the message.
249 * @param call Modified request that is being sent back.
250 *
[ba81cab]251 */
252void ipc_answer(answerbox_t *box, call_t *call)
253{
254 /* Remove from active box */
[da1bafb]255 irq_spinlock_lock(&box->lock, true);
[c4e4507]256 list_remove(&call->link);
[da1bafb]257 irq_spinlock_unlock(&box->lock, true);
258
[ba81cab]259 /* Send back answer */
[c713aa56]260 _ipc_answer_free_call(call, false);
[ba81cab]261}
262
[8b243f2]263/** Simulate sending back a message.
[7c7aae16]264 *
265 * Most errors are better handled by forming a normal backward
266 * message and sending it as a normal answer.
[8b243f2]267 *
[da1bafb]268 * @param phone Phone structure the call should appear to come from.
269 * @param call Call structure to be answered.
270 * @param err Return value to be used for the answer.
271 *
[7c7aae16]272 */
[96b02eb9]273void ipc_backsend_err(phone_t *phone, call_t *call, sysarg_t err)
[7c7aae16]274{
275 call->data.phone = phone;
276 atomic_inc(&phone->active_calls);
[eb3d379]277 IPC_SET_RETVAL(call->data, err);
[c713aa56]278 _ipc_answer_free_call(call, false);
[7c7aae16]279}
280
[8b243f2]281/** Unsafe unchecking version of ipc_call.
282 *
[da1bafb]283 * @param phone Phone structure the call comes from.
284 * @param box Destination answerbox structure.
285 * @param call Call structure with request.
286 *
[8b243f2]287 */
[fbcfd458]288static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
289{
[95319bd]290 /* Count sent ipc call */
[da1bafb]291 irq_spinlock_lock(&TASK->lock, true);
[a307beb]292 TASK->ipc_info.call_sent++;
[da1bafb]293 irq_spinlock_unlock(&TASK->lock, true);
294
[8b243f2]295 if (!(call->flags & IPC_CALL_FORWARDED)) {
[9f22213]296 atomic_inc(&phone->active_calls);
297 call->data.phone = phone;
[e2ab36f1]298 call->data.task_id = TASK->taskid;
[9f22213]299 }
[da1bafb]300
301 irq_spinlock_lock(&box->lock, true);
[c4e4507]302 list_append(&call->link, &box->calls);
[da1bafb]303 irq_spinlock_unlock(&box->lock, true);
304
[5c8ba05]305 waitq_wakeup(&box->wq, WAKEUP_FIRST);
[fbcfd458]306}
307
[8b243f2]308/** Send an asynchronous request using a phone to an answerbox.
309 *
[da1bafb]310 * @param phone Phone structure the call comes from and which is
311 * connected to the destination answerbox.
312 * @param call Call structure with request.
313 *
314 * @return Return 0 on success, ENOENT on error.
[6d9c49a]315 *
316 */
[9f22213]317int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]318{
[ff48a15]319 mutex_lock(&phone->lock);
[eb3d379]320 if (phone->state != IPC_PHONE_CONNECTED) {
[ff48a15]321 mutex_unlock(&phone->lock);
[9f22213]322 if (call->flags & IPC_CALL_FORWARDED) {
323 IPC_SET_RETVAL(call->data, EFORWARD);
[c713aa56]324 _ipc_answer_free_call(call, false);
[eb3d379]325 } else {
326 if (phone->state == IPC_PHONE_HUNGUP)
[7c7aae16]327 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]328 else
[7c7aae16]329 ipc_backsend_err(phone, call, ENOENT);
[9f22213]330 }
[da1bafb]331
[9f22213]332 return ENOENT;
[ba81cab]333 }
[da1bafb]334
335 answerbox_t *box = phone->callee;
[fbcfd458]336 _ipc_call(phone, box, call);
337
[ff48a15]338 mutex_unlock(&phone->lock);
[9f22213]339 return 0;
[fbcfd458]340}
341
[8b243f2]342/** Disconnect phone from answerbox.
[fbcfd458]343 *
[8b243f2]344 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
[eb3d379]345 * lazily later.
[fbcfd458]346 *
[da1bafb]347 * @param phone Phone structure to be hung up.
348 *
349 * @return 0 if the phone is disconnected.
350 * @return -1 if the phone was already disconnected.
351 *
[fbcfd458]352 */
[d8f7362]353int ipc_phone_hangup(phone_t *phone)
[fbcfd458]354{
[ff48a15]355 mutex_lock(&phone->lock);
[7918fce]356 if (phone->state == IPC_PHONE_FREE ||
357 phone->state == IPC_PHONE_HUNGUP ||
[8b243f2]358 phone->state == IPC_PHONE_CONNECTING) {
[ff48a15]359 mutex_unlock(&phone->lock);
[eb3d379]360 return -1;
[fbcfd458]361 }
[da1bafb]362
363 answerbox_t *box = phone->callee;
[eb3d379]364 if (phone->state != IPC_PHONE_SLAMMED) {
365 /* Remove myself from answerbox */
[da1bafb]366 irq_spinlock_lock(&box->lock, true);
[c4e4507]367 list_remove(&phone->link);
[da1bafb]368 irq_spinlock_unlock(&box->lock, true);
369
370 call_t *call = ipc_call_alloc(0);
[228e490]371 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[287e83f]372 call->flags |= IPC_CALL_DISCARD_ANSWER;
373 _ipc_call(phone, box, call);
[eb3d379]374 }
[da1bafb]375
[eb3d379]376 phone->state = IPC_PHONE_HUNGUP;
[ff48a15]377 mutex_unlock(&phone->lock);
[da1bafb]378
[fbcfd458]379 return 0;
[2ba7810]380}
381
[8b243f2]382/** Forwards call from one answerbox to another one.
[2ba7810]383 *
[da1bafb]384 * @param call Call structure to be redirected.
385 * @param newphone Phone structure to target answerbox.
386 * @param oldbox Old answerbox structure.
387 * @param mode Flags that specify mode of the forward operation.
388 *
389 * @return 0 if forwarding succeeded or an error code if
390 * there was an error.
[8b243f2]391 *
392 * The return value serves only as an information for the forwarder,
393 * the original caller is notified automatically with EFORWARD.
[da1bafb]394 *
[2ba7810]395 */
[da1bafb]396int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
397 unsigned int mode)
[2ba7810]398{
[95319bd]399 /* Count forwarded calls */
[da1bafb]400 irq_spinlock_lock(&TASK->lock, true);
[a307beb]401 TASK->ipc_info.forwarded++;
[da1bafb]402 irq_spinlock_pass(&TASK->lock, &oldbox->lock);
[c4e4507]403 list_remove(&call->link);
[da1bafb]404 irq_spinlock_unlock(&oldbox->lock, true);
405
[645d9ed2]406 if (mode & IPC_FF_ROUTE_FROM_ME) {
[27526e87]407 if (!call->caller_phone)
408 call->caller_phone = call->data.phone;
[9201f47]409 call->data.phone = newphone;
[e2ab36f1]410 call->data.task_id = TASK->taskid;
[645d9ed2]411 }
[da1bafb]412
[9f22213]413 return ipc_call(newphone, call);
[6d9c49a]414}
415
416
[8b243f2]417/** Wait for a phone call.
[6d9c49a]418 *
[da1bafb]419 * @param box Answerbox expecting the call.
420 * @param usec Timeout in microseconds. See documentation for
421 * waitq_sleep_timeout() for decription of its special
422 * meaning.
423 * @param flags Select mode of sleep operation. See documentation for
424 * waitq_sleep_timeout() for description of its special
425 * meaning.
426 *
427 * @return Recived call structure or NULL.
428 *
[8b243f2]429 * To distinguish between a call and an answer, have a look at call->flags.
[da1bafb]430 *
[6d9c49a]431 */
[da1bafb]432call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
[6d9c49a]433{
434 call_t *request;
[aa028db]435 uint64_t irq_cnt = 0;
436 uint64_t answer_cnt = 0;
437 uint64_t call_cnt = 0;
[bd5a663]438 int rc;
[da1bafb]439
[bd5a663]440restart:
[116d1ef4]441 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]442 if (SYNCH_FAILED(rc))
443 return NULL;
[fbcfd458]444
[da1bafb]445 irq_spinlock_lock(&box->lock, true);
[5626277]446 if (!list_empty(&box->irq_notifs)) {
[be06914]447 /* Count received IRQ notification */
[da1bafb]448 irq_cnt++;
449
450 irq_spinlock_lock(&box->irq_lock, false);
451
[55b77d9]452 request = list_get_instance(list_first(&box->irq_notifs),
453 call_t, link);
[c4e4507]454 list_remove(&request->link);
[da1bafb]455
456 irq_spinlock_unlock(&box->irq_lock, false);
[5626277]457 } else if (!list_empty(&box->answers)) {
[be06914]458 /* Count received answer */
[aa028db]459 answer_cnt++;
[da1bafb]460
[fbcfd458]461 /* Handle asynchronous answers */
[55b77d9]462 request = list_get_instance(list_first(&box->answers),
463 call_t, link);
[c4e4507]464 list_remove(&request->link);
[74965d2]465 atomic_dec(&request->data.phone->active_calls);
[fbcfd458]466 } else if (!list_empty(&box->calls)) {
[be06914]467 /* Count received call */
[aa028db]468 call_cnt++;
[da1bafb]469
[fbcfd458]470 /* Handle requests */
[55b77d9]471 request = list_get_instance(list_first(&box->calls),
472 call_t, link);
[c4e4507]473 list_remove(&request->link);
[da1bafb]474
[fbcfd458]475 /* Append request to dispatch queue */
[c4e4507]476 list_append(&request->link, &box->dispatched_calls);
[fbcfd458]477 } else {
[874621f]478 /* This can happen regularly after ipc_cleanup */
[da1bafb]479 irq_spinlock_unlock(&box->lock, true);
[fbcfd458]480 goto restart;
[6d9c49a]481 }
[aa028db]482
[da1bafb]483 irq_spinlock_pass(&box->lock, &TASK->lock);
484
[be06914]485 TASK->ipc_info.irq_notif_received += irq_cnt;
486 TASK->ipc_info.answer_received += answer_cnt;
487 TASK->ipc_info.call_received += call_cnt;
[da1bafb]488
489 irq_spinlock_unlock(&TASK->lock, true);
490
[6d9c49a]491 return request;
492}
493
[8b243f2]494/** Answer all calls from list with EHANGUP answer.
495 *
[da1bafb]496 * @param lst Head of the list to be cleaned up.
497 *
[8b243f2]498 */
[55b77d9]499void ipc_cleanup_call_list(list_t *lst)
[ca687ad]500{
501 while (!list_empty(lst)) {
[55b77d9]502 call_t *call = list_get_instance(list_first(lst), call_t, link);
[a55d5f9f]503 if (call->buffer)
504 free(call->buffer);
[da1bafb]505
[c4e4507]506 list_remove(&call->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
[da1bafb]576/** Clean up all IPC communication of the current task.
[9a1b20c]577 *
578 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
579 * have to change it as well if you want to cleanup other tasks than TASK.
[da1bafb]580 *
[9a1b20c]581 */
582void ipc_cleanup(void)
583{
584 /* Disconnect all our phones ('ipc_phone_hangup') */
[da1bafb]585 size_t i;
[9a1b20c]586 for (i = 0; i < IPC_MAX_PHONES; i++)
587 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]588
[05641a9e]589 /* Unsubscribe from any event notifications. */
590 event_cleanup_answerbox(&TASK->answerbox);
[da1bafb]591
[9a1b20c]592 /* Disconnect all connected irqs */
593 ipc_irq_cleanup(&TASK->answerbox);
[da1bafb]594
[9a1b20c]595 /* Disconnect all phones connected to our regular answerbox */
596 ipc_answerbox_slam_phones(&TASK->answerbox, false);
[da1bafb]597
[9a1b20c]598#ifdef CONFIG_UDEBUG
599 /* Clean up kbox thread and communications */
600 ipc_kbox_cleanup();
601#endif
[da1bafb]602
[9f22213]603 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[da1bafb]604 irq_spinlock_lock(&TASK->answerbox.lock, true);
[214c5a0]605 ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
606 ipc_cleanup_call_list(&TASK->answerbox.calls);
[da1bafb]607 irq_spinlock_unlock(&TASK->answerbox.lock, true);
[4e49572]608
[33adc6ce]609 /* Wait for all answers to interrupted synchronous calls to arrive */
[da1bafb]610 ipl_t ipl = interrupts_disable();
[55b77d9]611 while (!list_empty(&TASK->sync_boxes)) {
612 answerbox_t *box = list_get_instance(
613 list_first(&TASK->sync_boxes), answerbox_t, sync_box_link);
[da1bafb]614
[33adc6ce]615 list_remove(&box->sync_box_link);
[da1bafb]616 call_t *call = ipc_wait_for_call(box, SYNCH_NO_TIMEOUT,
[33adc6ce]617 SYNCH_FLAGS_NONE);
618 ipc_call_free(call);
619 slab_free(ipc_answerbox_slab, box);
620 }
621 interrupts_restore(ipl);
[da1bafb]622
[33adc6ce]623 /* Wait for all answers to asynchronous calls to arrive */
[da1bafb]624 while (true) {
625 /*
626 * Go through all phones, until they are all FREE
627 * Locking is not needed, no one else should modify
628 * it when we are in cleanup
629 */
[8b243f2]630 for (i = 0; i < IPC_MAX_PHONES; i++) {
631 if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
[e701eb1]632 atomic_get(&TASK->phones[i].active_calls) == 0) {
[214c5a0]633 TASK->phones[i].state = IPC_PHONE_FREE;
[e701eb1]634 TASK->phones[i].callee = NULL;
635 }
[214c5a0]636
[da1bafb]637 /*
638 * Just for sure, we might have had some
639 * IPC_PHONE_CONNECTING phones
640 */
[214c5a0]641 if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
[d8f7362]642 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]643
644 /*
645 * If the hangup succeeded, it has sent a HANGUP
[d8f7362]646 * message, the IPC is now in HUNGUP state, we
[da1bafb]647 * wait for the reply to come
648 */
[214c5a0]649
650 if (TASK->phones[i].state != IPC_PHONE_FREE)
[eb3d379]651 break;
652 }
[da1bafb]653
654 /* Got into cleanup */
[eb3d379]655 if (i == IPC_MAX_PHONES)
656 break;
657
[da1bafb]658 call_t *call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
[8b243f2]659 SYNCH_FLAGS_NONE);
660 ASSERT((call->flags & IPC_CALL_ANSWERED) ||
661 (call->flags & IPC_CALL_NOTIF));
[ca687ad]662
663 ipc_call_free(call);
664 }
[4e49572]665}
[5626277]666
[da1bafb]667/** Initilize IPC subsystem
668 *
669 */
[5626277]670void ipc_init(void)
671{
[8b243f2]672 ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
673 NULL, 0);
[c70ce74]674 ipc_answerbox_slab = slab_cache_create("ipc_answerbox",
675 sizeof(answerbox_t), 0, NULL, NULL, 0);
[5626277]676}
677
[8b243f2]678/** List answerbox contents.
679 *
[da1bafb]680 * @param taskid Task ID.
681 *
[8b243f2]682 */
[c4e4507]683void ipc_print_task(task_id_t taskid)
684{
[da1bafb]685 irq_spinlock_lock(&tasks_lock, true);
686 task_t *task = task_find_by_id(taskid);
687
[170332d]688 if (!task) {
[da1bafb]689 irq_spinlock_unlock(&tasks_lock, true);
[c4e4507]690 return;
[170332d]691 }
[da1bafb]692
693 /* Hand-over-hand locking */
694 irq_spinlock_exchange(&tasks_lock, &task->lock);
695
[5378f99]696 printf("[phone id] [calls] [state\n");
[da1bafb]697
698 size_t i;
[8b243f2]699 for (i = 0; i < IPC_MAX_PHONES; i++) {
[ff48a15]700 if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
[5378f99]701 printf("%-10zu (mutex busy)\n", i);
[ff48a15]702 continue;
703 }
[da1bafb]704
[c4e4507]705 if (task->phones[i].state != IPC_PHONE_FREE) {
[5378f99]706 printf("%-10zu %7" PRIun " ", i,
707 atomic_get(&task->phones[i].active_calls));
[da1bafb]708
[c4e4507]709 switch (task->phones[i].state) {
710 case IPC_PHONE_CONNECTING:
[5378f99]711 printf("connecting");
[c4e4507]712 break;
713 case IPC_PHONE_CONNECTED:
[5378f99]714 printf("connected to %" PRIu64 " (%s)",
715 task->phones[i].callee->task->taskid,
716 task->phones[i].callee->task->name);
[c4e4507]717 break;
718 case IPC_PHONE_SLAMMED:
[5378f99]719 printf("slammed by %p",
[da1bafb]720 task->phones[i].callee);
[c4e4507]721 break;
722 case IPC_PHONE_HUNGUP:
[5378f99]723 printf("hung up by %p",
[da1bafb]724 task->phones[i].callee);
[c4e4507]725 break;
726 default:
727 break;
728 }
[da1bafb]729
[5378f99]730 printf("\n");
[c4e4507]731 }
[da1bafb]732
[ff48a15]733 mutex_unlock(&task->phones[i].lock);
[c4e4507]734 }
[da1bafb]735
736 irq_spinlock_lock(&task->answerbox.lock, false);
737
[5378f99]738#ifdef __32_BITS__
739 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
740 " [flags] [sender\n");
741#endif
742
743#ifdef __64_BITS__
744 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
745 " [arg5] [flags] [sender\n");
746#endif
747
748 printf(" --- incomming calls ---\n");
[55b77d9]749 list_foreach(task->answerbox.calls, cur) {
[da1bafb]750 call_t *call = list_get_instance(cur, call_t, link);
[5378f99]751
752#ifdef __32_BITS__
753 printf("%10p ", call);
754#endif
755
756#ifdef __64_BITS__
757 printf("%18p ", call);
758#endif
759
760 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
761 " %-6" PRIun " %-6" PRIun " %-7x %" PRIu64 " (%s)\n",
[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),
[5378f99]765 call->flags, call->sender->taskid, call->sender->name);
[c4e4507]766 }
[da1bafb]767
[5378f99]768 printf(" --- dispatched calls ---\n");
[55b77d9]769 list_foreach(task->answerbox.dispatched_calls, cur) {
[da1bafb]770 call_t *call = list_get_instance(cur, call_t, link);
[5378f99]771
772#ifdef __32_BITS__
773 printf("%10p ", call);
774#endif
775
776#ifdef __64_BITS__
777 printf("%18p ", call);
778#endif
779
780 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
781 " %-6" PRIun " %-6" PRIun " %-7x %" PRIu64 " (%s)\n",
[228e490]782 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]783 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]784 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[5378f99]785 call->flags, call->sender->taskid, call->sender->name);
[c4e4507]786 }
[da1bafb]787
[6aef742]788 printf(" --- incoming answers ---\n");
[55b77d9]789 list_foreach(task->answerbox.answers, cur) {
[da1bafb]790 call_t *call = list_get_instance(cur, call_t, link);
[5378f99]791
792#ifdef __32_BITS__
793 printf("%10p ", call);
794#endif
795
796#ifdef __64_BITS__
797 printf("%18p ", call);
798#endif
799
800 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
801 " %-6" PRIun " %-6" PRIun " %-7x %" PRIu64 " (%s)\n",
802 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]803 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]804 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[5378f99]805 call->flags, call->sender->taskid, call->sender->name);
[c4e4507]806 }
[da1bafb]807
808 irq_spinlock_unlock(&task->answerbox.lock, false);
809 irq_spinlock_unlock(&task->lock, true);
[c4e4507]810}
[b45c443]811
[cc73a8a1]812/** @}
[b45c443]813 */
Note: See TracBrowser for help on using the repository browser.