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

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

new async framework with integrated exchange tracking

  • strict isolation between low-level IPC and high-level async framework with integrated exchange tracking
    • each IPC connection is represented by an async_sess_t structure
    • each IPC exchange is represented by an async_exch_t structure
    • exchange management is either based on atomic messages (EXCHANGE_ATOMIC), locking (EXCHANGE_SERIALIZE) or connection cloning (EXCHANGE_CLONE)
  • async_obsolete: temporary compatibility layer to keep old async clients working (several pieces of code are currently broken, but only non-essential functionality)
  • IPC_M_PHONE_HANGUP is now method no. 0 (for elegant boolean evaluation)
  • IPC_M_DEBUG_ALL has been renamed to IPC_M_DEBUG
  • IPC_M_PING has been removed (VFS protocol now has VFS_IN_PING)
  • console routines in libc have been rewritten for better abstraction
  • additional use for libc-private header files (FILE structure opaque to the client)
  • various cstyle changes (typos, indentation, missing externs in header files, improved comments, etc.)
  • Property mode set to 100644
File size: 21.5 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>
[79ae36dd]46#include <ipc/ipc_methods.h>
[e028660]47#include <ipc/kbox.h>
[13a638d]48#include <ipc/event.h>
[6d9c49a]49#include <errno.h>
50#include <mm/slab.h>
51#include <arch.h>
52#include <proc/task.h>
53#include <memstr.h>
54#include <debug.h>
55#include <print.h>
[9a1b20c]56#include <console/console.h>
[6d9c49a]57#include <proc/thread.h>
[5626277]58#include <arch/interrupt.h>
[162f919]59#include <ipc/irq.h>
[6d9c49a]60
[8b243f2]61/** Open channel that is assigned automatically to new tasks */
[e74cb73]62answerbox_t *ipc_phone_0 = NULL;
[6d9c49a]63
64static slab_cache_t *ipc_call_slab;
[c70ce74]65static slab_cache_t *ipc_answerbox_slab;
[6d9c49a]66
[8b243f2]67/** Initialize a call structure.
68 *
[da1bafb]69 * @param call Call structure to be initialized.
70 *
[8b243f2]71 */
[ba81cab]72static void _ipc_call_init(call_t *call)
73{
[e32e092]74 memsetb(call, sizeof(*call), 0);
[ba81cab]75 call->callerbox = &TASK->answerbox;
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);
[33adc6ce]124 link_initialize(&box->sync_box_link);
[6d9c49a]125 list_initialize(&box->connected_phones);
126 list_initialize(&box->calls);
127 list_initialize(&box->dispatched_calls);
128 list_initialize(&box->answers);
[5626277]129 list_initialize(&box->irq_notifs);
[b14e35f2]130 list_initialize(&box->irq_head);
[12ab886]131 box->task = task;
[6d9c49a]132}
133
[8b243f2]134/** Connect a phone to an answerbox.
135 *
[da1bafb]136 * @param phone Initialized phone structure.
137 * @param box Initialized answerbox structure.
138 *
[8b243f2]139 */
[2ba7810]140void ipc_phone_connect(phone_t *phone, answerbox_t *box)
[6d9c49a]141{
[ff48a15]142 mutex_lock(&phone->lock);
[da1bafb]143
[eb3d379]144 phone->state = IPC_PHONE_CONNECTED;
[6d9c49a]145 phone->callee = box;
[da1bafb]146
147 irq_spinlock_lock(&box->lock, true);
[c4e4507]148 list_append(&phone->link, &box->connected_phones);
[da1bafb]149 irq_spinlock_unlock(&box->lock, true);
150
[ff48a15]151 mutex_unlock(&phone->lock);
[2ba7810]152}
153
[8b243f2]154/** Initialize a phone structure.
155 *
[da1bafb]156 * @param phone Phone structure to be initialized.
157 *
[2ba7810]158 */
159void ipc_phone_init(phone_t *phone)
160{
[08a19ba]161 mutex_initialize(&phone->lock, MUTEX_PASSIVE);
[2ba7810]162 phone->callee = NULL;
[eb3d379]163 phone->state = IPC_PHONE_FREE;
[9f22213]164 atomic_set(&phone->active_calls, 0);
[6d9c49a]165}
166
[8b243f2]167/** Helper function to facilitate synchronous calls.
168 *
[da1bafb]169 * @param phone Destination kernel phone structure.
170 * @param request Call structure with request.
171 *
172 * @return EOK on success or EINTR if the sleep was interrupted.
[79872cd]173 *
[8b243f2]174 */
[79872cd]175int ipc_call_sync(phone_t *phone, call_t *request)
[631ca4d]176{
[da1bafb]177 answerbox_t *sync_box = slab_alloc(ipc_answerbox_slab, 0);
[c70ce74]178 ipc_answerbox_init(sync_box, TASK);
[da1bafb]179
[33adc6ce]180 /*
181 * Put the answerbox on the TASK's list of synchronous answerboxes so
182 * that it can be cleaned up if the call is interrupted.
183 */
[da1bafb]184 irq_spinlock_lock(&TASK->lock, true);
[33adc6ce]185 list_append(&sync_box->sync_box_link, &TASK->sync_box_head);
[da1bafb]186 irq_spinlock_unlock(&TASK->lock, true);
187
[8b243f2]188 /* We will receive data in a special box. */
[c70ce74]189 request->callerbox = sync_box;
[da1bafb]190
[631ca4d]191 ipc_call(phone, request);
[c70ce74]192 if (!ipc_wait_for_call(sync_box, SYNCH_NO_TIMEOUT,
193 SYNCH_FLAGS_INTERRUPTIBLE)) {
[da1bafb]194 /* The answerbox and the call will be freed by ipc_cleanup(). */
[79872cd]195 return EINTR;
[c70ce74]196 }
[da1bafb]197
[33adc6ce]198 /*
199 * The answer arrived without interruption so we can remove the
200 * answerbox from the TASK's list of synchronous answerboxes.
201 */
[da1bafb]202 irq_spinlock_lock(&TASK->lock, true);
[33adc6ce]203 list_remove(&sync_box->sync_box_link);
[da1bafb]204 irq_spinlock_unlock(&TASK->lock, true);
205
[c70ce74]206 slab_free(ipc_answerbox_slab, sync_box);
[79872cd]207 return EOK;
[631ca4d]208}
[6d9c49a]209
[8b243f2]210/** Answer a message which was not dispatched and is not listed in any queue.
211 *
[da1bafb]212 * @param call Call structure to be answered.
213 * @param selflocked If true, then TASK->answebox is locked.
214 *
[ba81cab]215 */
[c713aa56]216static void _ipc_answer_free_call(call_t *call, bool selflocked)
[ba81cab]217{
218 answerbox_t *callerbox = call->callerbox;
[c713aa56]219 bool do_lock = ((!selflocked) || callerbox != (&TASK->answerbox));
[da1bafb]220
[95319bd]221 /* Count sent answer */
[da1bafb]222 irq_spinlock_lock(&TASK->lock, true);
[a307beb]223 TASK->ipc_info.answer_sent++;
[da1bafb]224 irq_spinlock_unlock(&TASK->lock, true);
225
[ba81cab]226 call->flags |= IPC_CALL_ANSWERED;
[da1bafb]227
[74965d2]228 if (call->flags & IPC_CALL_FORWARDED) {
[27526e87]229 if (call->caller_phone) {
[74965d2]230 /* Demasquerade the caller phone. */
[27526e87]231 call->data.phone = call->caller_phone;
[74965d2]232 }
233 }
[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;
[8c9b742]298 call->data.task = TASK;
[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;
[8c9b742]410 call->data.task = TASK;
[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
[c4e4507]452 request = list_get_instance(box->irq_notifs.next, call_t, link);
453 list_remove(&request->link);
[da1bafb]454
455 irq_spinlock_unlock(&box->irq_lock, false);
[5626277]456 } else if (!list_empty(&box->answers)) {
[be06914]457 /* Count received answer */
[aa028db]458 answer_cnt++;
[da1bafb]459
[fbcfd458]460 /* Handle asynchronous answers */
[c4e4507]461 request = list_get_instance(box->answers.next, call_t, link);
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 */
[c4e4507]469 request = list_get_instance(box->calls.next, call_t, link);
470 list_remove(&request->link);
[da1bafb]471
[fbcfd458]472 /* Append request to dispatch queue */
[c4e4507]473 list_append(&request->link, &box->dispatched_calls);
[fbcfd458]474 } else {
[874621f]475 /* This can happen regularly after ipc_cleanup */
[da1bafb]476 irq_spinlock_unlock(&box->lock, true);
[fbcfd458]477 goto restart;
[6d9c49a]478 }
[aa028db]479
[da1bafb]480 irq_spinlock_pass(&box->lock, &TASK->lock);
481
[be06914]482 TASK->ipc_info.irq_notif_received += irq_cnt;
483 TASK->ipc_info.answer_received += answer_cnt;
484 TASK->ipc_info.call_received += call_cnt;
[da1bafb]485
486 irq_spinlock_unlock(&TASK->lock, true);
487
[6d9c49a]488 return request;
489}
490
[8b243f2]491/** Answer all calls from list with EHANGUP answer.
492 *
[da1bafb]493 * @param lst Head of the list to be cleaned up.
494 *
[8b243f2]495 */
[9a1b20c]496void ipc_cleanup_call_list(link_t *lst)
[ca687ad]497{
498 while (!list_empty(lst)) {
[da1bafb]499 call_t *call = list_get_instance(lst->next, call_t, link);
[a55d5f9f]500 if (call->buffer)
501 free(call->buffer);
[da1bafb]502
[c4e4507]503 list_remove(&call->link);
[da1bafb]504
[ca687ad]505 IPC_SET_RETVAL(call->data, EHANGUP);
[c713aa56]506 _ipc_answer_free_call(call, true);
[ca687ad]507 }
508}
509
[9a1b20c]510/** Disconnects all phones connected to an answerbox.
[4e49572]511 *
[da1bafb]512 * @param box Answerbox to disconnect phones from.
513 * @param notify_box If true, the answerbox will get a hangup message for
514 * each disconnected phone.
515 *
[4e49572]516 */
[9a1b20c]517void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
[4e49572]518{
[ca687ad]519 phone_t *phone;
[31d8e10]520 DEADLOCK_PROBE_INIT(p_phonelck);
[da1bafb]521
522 call_t *call = notify_box ? ipc_call_alloc(0) : NULL;
523
[ca687ad]524 /* Disconnect all phones connected to our answerbox */
525restart_phones:
[da1bafb]526 irq_spinlock_lock(&box->lock, true);
[9a1b20c]527 while (!list_empty(&box->connected_phones)) {
528 phone = list_get_instance(box->connected_phones.next,
[31d8e10]529 phone_t, link);
[ff48a15]530 if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
[da1bafb]531 irq_spinlock_unlock(&box->lock, true);
[31d8e10]532 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
[ca687ad]533 goto restart_phones;
534 }
535
536 /* Disconnect phone */
[eb3d379]537 ASSERT(phone->state == IPC_PHONE_CONNECTED);
[da1bafb]538
[c4e4507]539 list_remove(&phone->link);
[9a1b20c]540 phone->state = IPC_PHONE_SLAMMED;
[da1bafb]541
[9a1b20c]542 if (notify_box) {
543 mutex_unlock(&phone->lock);
[da1bafb]544 irq_spinlock_unlock(&box->lock, true);
545
[9a1b20c]546 /*
547 * Send one message to the answerbox for each
548 * phone. Used to make sure the kbox thread
549 * wakes up after the last phone has been
550 * disconnected.
551 */
[228e490]552 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[9a1b20c]553 call->flags |= IPC_CALL_DISCARD_ANSWER;
554 _ipc_call(phone, box, call);
[da1bafb]555
[9a1b20c]556 /* Allocate another call in advance */
557 call = ipc_call_alloc(0);
[da1bafb]558
[9a1b20c]559 /* Must start again */
560 goto restart_phones;
561 }
[da1bafb]562
[ff48a15]563 mutex_unlock(&phone->lock);
[ca687ad]564 }
[da1bafb]565
566 irq_spinlock_unlock(&box->lock, true);
567
[9a1b20c]568 /* Free unused call */
[119c335]569 if (call)
570 ipc_call_free(call);
[9a1b20c]571}
572
[da1bafb]573/** Clean up all IPC communication of the current task.
[9a1b20c]574 *
575 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
576 * have to change it as well if you want to cleanup other tasks than TASK.
[da1bafb]577 *
[9a1b20c]578 */
579void ipc_cleanup(void)
580{
581 /* Disconnect all our phones ('ipc_phone_hangup') */
[da1bafb]582 size_t i;
[9a1b20c]583 for (i = 0; i < IPC_MAX_PHONES; i++)
584 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]585
[05641a9e]586 /* Unsubscribe from any event notifications. */
587 event_cleanup_answerbox(&TASK->answerbox);
[da1bafb]588
[9a1b20c]589 /* Disconnect all connected irqs */
590 ipc_irq_cleanup(&TASK->answerbox);
[da1bafb]591
[9a1b20c]592 /* Disconnect all phones connected to our regular answerbox */
593 ipc_answerbox_slam_phones(&TASK->answerbox, false);
[da1bafb]594
[9a1b20c]595#ifdef CONFIG_UDEBUG
596 /* Clean up kbox thread and communications */
597 ipc_kbox_cleanup();
598#endif
[da1bafb]599
[9f22213]600 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[da1bafb]601 irq_spinlock_lock(&TASK->answerbox.lock, true);
[214c5a0]602 ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
603 ipc_cleanup_call_list(&TASK->answerbox.calls);
[da1bafb]604 irq_spinlock_unlock(&TASK->answerbox.lock, true);
[4e49572]605
[33adc6ce]606 /* Wait for all answers to interrupted synchronous calls to arrive */
[da1bafb]607 ipl_t ipl = interrupts_disable();
[33adc6ce]608 while (!list_empty(&TASK->sync_box_head)) {
609 answerbox_t *box = list_get_instance(TASK->sync_box_head.next,
610 answerbox_t, sync_box_link);
[da1bafb]611
[33adc6ce]612 list_remove(&box->sync_box_link);
[da1bafb]613 call_t *call = ipc_wait_for_call(box, SYNCH_NO_TIMEOUT,
[33adc6ce]614 SYNCH_FLAGS_NONE);
615 ipc_call_free(call);
616 slab_free(ipc_answerbox_slab, box);
617 }
618 interrupts_restore(ipl);
[da1bafb]619
[33adc6ce]620 /* Wait for all answers to asynchronous calls to arrive */
[da1bafb]621 while (true) {
622 /*
623 * Go through all phones, until they are all FREE
624 * Locking is not needed, no one else should modify
625 * it when we are in cleanup
626 */
[8b243f2]627 for (i = 0; i < IPC_MAX_PHONES; i++) {
628 if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
[e701eb1]629 atomic_get(&TASK->phones[i].active_calls) == 0) {
[214c5a0]630 TASK->phones[i].state = IPC_PHONE_FREE;
[e701eb1]631 TASK->phones[i].callee = NULL;
632 }
[214c5a0]633
[da1bafb]634 /*
635 * Just for sure, we might have had some
636 * IPC_PHONE_CONNECTING phones
637 */
[214c5a0]638 if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
[d8f7362]639 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]640
641 /*
642 * If the hangup succeeded, it has sent a HANGUP
[d8f7362]643 * message, the IPC is now in HUNGUP state, we
[da1bafb]644 * wait for the reply to come
645 */
[214c5a0]646
647 if (TASK->phones[i].state != IPC_PHONE_FREE)
[eb3d379]648 break;
649 }
[da1bafb]650
651 /* Got into cleanup */
[eb3d379]652 if (i == IPC_MAX_PHONES)
653 break;
654
[da1bafb]655 call_t *call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
[8b243f2]656 SYNCH_FLAGS_NONE);
657 ASSERT((call->flags & IPC_CALL_ANSWERED) ||
658 (call->flags & IPC_CALL_NOTIF));
[ca687ad]659
660 ipc_call_free(call);
661 }
[4e49572]662}
[5626277]663
[da1bafb]664/** Initilize IPC subsystem
665 *
666 */
[5626277]667void ipc_init(void)
668{
[8b243f2]669 ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
670 NULL, 0);
[c70ce74]671 ipc_answerbox_slab = slab_cache_create("ipc_answerbox",
672 sizeof(answerbox_t), 0, NULL, NULL, 0);
[5626277]673}
674
[8b243f2]675/** List answerbox contents.
676 *
[da1bafb]677 * @param taskid Task ID.
678 *
[8b243f2]679 */
[c4e4507]680void ipc_print_task(task_id_t taskid)
681{
[da1bafb]682 irq_spinlock_lock(&tasks_lock, true);
683 task_t *task = task_find_by_id(taskid);
684
[170332d]685 if (!task) {
[da1bafb]686 irq_spinlock_unlock(&tasks_lock, true);
[c4e4507]687 return;
[170332d]688 }
[da1bafb]689
690 /* Hand-over-hand locking */
691 irq_spinlock_exchange(&tasks_lock, &task->lock);
692
[5378f99]693 printf("[phone id] [calls] [state\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))) {
[5378f99]698 printf("%-10zu (mutex busy)\n", i);
[ff48a15]699 continue;
700 }
[da1bafb]701
[c4e4507]702 if (task->phones[i].state != IPC_PHONE_FREE) {
[5378f99]703 printf("%-10zu %7" PRIun " ", i,
704 atomic_get(&task->phones[i].active_calls));
[da1bafb]705
[c4e4507]706 switch (task->phones[i].state) {
707 case IPC_PHONE_CONNECTING:
[5378f99]708 printf("connecting");
[c4e4507]709 break;
710 case IPC_PHONE_CONNECTED:
[5378f99]711 printf("connected to %" PRIu64 " (%s)",
712 task->phones[i].callee->task->taskid,
713 task->phones[i].callee->task->name);
[c4e4507]714 break;
715 case IPC_PHONE_SLAMMED:
[5378f99]716 printf("slammed by %p",
[da1bafb]717 task->phones[i].callee);
[c4e4507]718 break;
719 case IPC_PHONE_HUNGUP:
[5378f99]720 printf("hung up by %p",
[da1bafb]721 task->phones[i].callee);
[c4e4507]722 break;
723 default:
724 break;
725 }
[da1bafb]726
[5378f99]727 printf("\n");
[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
[5378f99]735#ifdef __32_BITS__
736 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
737 " [flags] [sender\n");
738#endif
739
740#ifdef __64_BITS__
741 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
742 " [arg5] [flags] [sender\n");
743#endif
744
[da1bafb]745 link_t *cur;
746
[5378f99]747 printf(" --- incomming calls ---\n");
[da1bafb]748 for (cur = task->answerbox.calls.next; cur != &task->answerbox.calls;
749 cur = cur->next) {
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");
[da1bafb]769 for (cur = task->answerbox.dispatched_calls.next;
770 cur != &task->answerbox.dispatched_calls;
771 cur = cur->next) {
772 call_t *call = list_get_instance(cur, call_t, link);
[5378f99]773
774#ifdef __32_BITS__
775 printf("%10p ", call);
776#endif
777
778#ifdef __64_BITS__
779 printf("%18p ", call);
780#endif
781
782 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
783 " %-6" PRIun " %-6" PRIun " %-7x %" PRIu64 " (%s)\n",
[228e490]784 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]785 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]786 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[5378f99]787 call->flags, call->sender->taskid, call->sender->name);
[c4e4507]788 }
[da1bafb]789
[6aef742]790 printf(" --- incoming answers ---\n");
[da1bafb]791 for (cur = task->answerbox.answers.next;
792 cur != &task->answerbox.answers;
793 cur = cur->next) {
794 call_t *call = list_get_instance(cur, call_t, link);
[5378f99]795
796#ifdef __32_BITS__
797 printf("%10p ", call);
798#endif
799
800#ifdef __64_BITS__
801 printf("%18p ", call);
802#endif
803
804 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
805 " %-6" PRIun " %-6" PRIun " %-7x %" PRIu64 " (%s)\n",
806 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]807 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]808 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[5378f99]809 call->flags, call->sender->taskid, call->sender->name);
[c4e4507]810 }
[da1bafb]811
812 irq_spinlock_unlock(&task->answerbox.lock, false);
813 irq_spinlock_unlock(&task->lock, true);
[c4e4507]814}
[b45c443]815
[cc73a8a1]816/** @}
[b45c443]817 */
Note: See TracBrowser for help on using the repository browser.