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

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

For connected phones, print also the task ID of the target task.

  • Property mode set to 100644
File size: 21.0 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;
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;
[645d9ed2]408 }
[da1bafb]409
[9f22213]410 return ipc_call(newphone, call);
[6d9c49a]411}
412
413
[8b243f2]414/** Wait for a phone call.
[6d9c49a]415 *
[da1bafb]416 * @param box Answerbox expecting the call.
417 * @param usec Timeout in microseconds. See documentation for
418 * waitq_sleep_timeout() for decription of its special
419 * meaning.
420 * @param flags Select mode of sleep operation. See documentation for
421 * waitq_sleep_timeout() for description of its special
422 * meaning.
423 *
424 * @return Recived call structure or NULL.
425 *
[8b243f2]426 * To distinguish between a call and an answer, have a look at call->flags.
[da1bafb]427 *
[6d9c49a]428 */
[da1bafb]429call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
[6d9c49a]430{
431 call_t *request;
[aa028db]432 uint64_t irq_cnt = 0;
433 uint64_t answer_cnt = 0;
434 uint64_t call_cnt = 0;
[bd5a663]435 int rc;
[da1bafb]436
[bd5a663]437restart:
[116d1ef4]438 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]439 if (SYNCH_FAILED(rc))
440 return NULL;
[fbcfd458]441
[da1bafb]442 irq_spinlock_lock(&box->lock, true);
[5626277]443 if (!list_empty(&box->irq_notifs)) {
[be06914]444 /* Count received IRQ notification */
[da1bafb]445 irq_cnt++;
446
447 irq_spinlock_lock(&box->irq_lock, false);
448
[c4e4507]449 request = list_get_instance(box->irq_notifs.next, call_t, link);
450 list_remove(&request->link);
[da1bafb]451
452 irq_spinlock_unlock(&box->irq_lock, false);
[5626277]453 } else if (!list_empty(&box->answers)) {
[be06914]454 /* Count received answer */
[aa028db]455 answer_cnt++;
[da1bafb]456
[fbcfd458]457 /* Handle asynchronous answers */
[c4e4507]458 request = list_get_instance(box->answers.next, call_t, link);
459 list_remove(&request->link);
[74965d2]460 atomic_dec(&request->data.phone->active_calls);
[fbcfd458]461 } else if (!list_empty(&box->calls)) {
[be06914]462 /* Count received call */
[aa028db]463 call_cnt++;
[da1bafb]464
[fbcfd458]465 /* Handle requests */
[c4e4507]466 request = list_get_instance(box->calls.next, call_t, link);
467 list_remove(&request->link);
[da1bafb]468
[fbcfd458]469 /* Append request to dispatch queue */
[c4e4507]470 list_append(&request->link, &box->dispatched_calls);
[fbcfd458]471 } else {
[874621f]472 /* This can happen regularly after ipc_cleanup */
[da1bafb]473 irq_spinlock_unlock(&box->lock, true);
[fbcfd458]474 goto restart;
[6d9c49a]475 }
[aa028db]476
[da1bafb]477 irq_spinlock_pass(&box->lock, &TASK->lock);
478
[be06914]479 TASK->ipc_info.irq_notif_received += irq_cnt;
480 TASK->ipc_info.answer_received += answer_cnt;
481 TASK->ipc_info.call_received += call_cnt;
[da1bafb]482
483 irq_spinlock_unlock(&TASK->lock, true);
484
[6d9c49a]485 return request;
486}
487
[8b243f2]488/** Answer all calls from list with EHANGUP answer.
489 *
[da1bafb]490 * @param lst Head of the list to be cleaned up.
491 *
[8b243f2]492 */
[9a1b20c]493void ipc_cleanup_call_list(link_t *lst)
[ca687ad]494{
495 while (!list_empty(lst)) {
[da1bafb]496 call_t *call = list_get_instance(lst->next, call_t, link);
[a55d5f9f]497 if (call->buffer)
498 free(call->buffer);
[da1bafb]499
[c4e4507]500 list_remove(&call->link);
[da1bafb]501
[ca687ad]502 IPC_SET_RETVAL(call->data, EHANGUP);
[c713aa56]503 _ipc_answer_free_call(call, true);
[ca687ad]504 }
505}
506
[9a1b20c]507/** Disconnects all phones connected to an answerbox.
[4e49572]508 *
[da1bafb]509 * @param box Answerbox to disconnect phones from.
510 * @param notify_box If true, the answerbox will get a hangup message for
511 * each disconnected phone.
512 *
[4e49572]513 */
[9a1b20c]514void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
[4e49572]515{
[ca687ad]516 phone_t *phone;
[31d8e10]517 DEADLOCK_PROBE_INIT(p_phonelck);
[da1bafb]518
519 call_t *call = notify_box ? ipc_call_alloc(0) : NULL;
520
[ca687ad]521 /* Disconnect all phones connected to our answerbox */
522restart_phones:
[da1bafb]523 irq_spinlock_lock(&box->lock, true);
[9a1b20c]524 while (!list_empty(&box->connected_phones)) {
525 phone = list_get_instance(box->connected_phones.next,
[31d8e10]526 phone_t, link);
[ff48a15]527 if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
[da1bafb]528 irq_spinlock_unlock(&box->lock, true);
[31d8e10]529 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
[ca687ad]530 goto restart_phones;
531 }
532
533 /* Disconnect phone */
[eb3d379]534 ASSERT(phone->state == IPC_PHONE_CONNECTED);
[da1bafb]535
[c4e4507]536 list_remove(&phone->link);
[9a1b20c]537 phone->state = IPC_PHONE_SLAMMED;
[da1bafb]538
[9a1b20c]539 if (notify_box) {
540 mutex_unlock(&phone->lock);
[da1bafb]541 irq_spinlock_unlock(&box->lock, true);
542
[9a1b20c]543 /*
544 * Send one message to the answerbox for each
545 * phone. Used to make sure the kbox thread
546 * wakes up after the last phone has been
547 * disconnected.
548 */
[228e490]549 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[9a1b20c]550 call->flags |= IPC_CALL_DISCARD_ANSWER;
551 _ipc_call(phone, box, call);
[da1bafb]552
[9a1b20c]553 /* Allocate another call in advance */
554 call = ipc_call_alloc(0);
[da1bafb]555
[9a1b20c]556 /* Must start again */
557 goto restart_phones;
558 }
[da1bafb]559
[ff48a15]560 mutex_unlock(&phone->lock);
[ca687ad]561 }
[da1bafb]562
563 irq_spinlock_unlock(&box->lock, true);
564
[9a1b20c]565 /* Free unused call */
[119c335]566 if (call)
567 ipc_call_free(call);
[9a1b20c]568}
569
[da1bafb]570/** Clean up all IPC communication of the current task.
[9a1b20c]571 *
572 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
573 * have to change it as well if you want to cleanup other tasks than TASK.
[da1bafb]574 *
[9a1b20c]575 */
576void ipc_cleanup(void)
577{
578 /* Disconnect all our phones ('ipc_phone_hangup') */
[da1bafb]579 size_t i;
[9a1b20c]580 for (i = 0; i < IPC_MAX_PHONES; i++)
581 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]582
[05641a9e]583 /* Unsubscribe from any event notifications. */
584 event_cleanup_answerbox(&TASK->answerbox);
[da1bafb]585
[9a1b20c]586 /* Disconnect all connected irqs */
587 ipc_irq_cleanup(&TASK->answerbox);
[da1bafb]588
[9a1b20c]589 /* Disconnect all phones connected to our regular answerbox */
590 ipc_answerbox_slam_phones(&TASK->answerbox, false);
[da1bafb]591
[9a1b20c]592#ifdef CONFIG_UDEBUG
593 /* Clean up kbox thread and communications */
594 ipc_kbox_cleanup();
595#endif
[da1bafb]596
[9f22213]597 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[da1bafb]598 irq_spinlock_lock(&TASK->answerbox.lock, true);
[214c5a0]599 ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
600 ipc_cleanup_call_list(&TASK->answerbox.calls);
[da1bafb]601 irq_spinlock_unlock(&TASK->answerbox.lock, true);
[4e49572]602
[33adc6ce]603 /* Wait for all answers to interrupted synchronous calls to arrive */
[da1bafb]604 ipl_t ipl = interrupts_disable();
[33adc6ce]605 while (!list_empty(&TASK->sync_box_head)) {
606 answerbox_t *box = list_get_instance(TASK->sync_box_head.next,
607 answerbox_t, sync_box_link);
[da1bafb]608
[33adc6ce]609 list_remove(&box->sync_box_link);
[da1bafb]610 call_t *call = ipc_wait_for_call(box, SYNCH_NO_TIMEOUT,
[33adc6ce]611 SYNCH_FLAGS_NONE);
612 ipc_call_free(call);
613 slab_free(ipc_answerbox_slab, box);
614 }
615 interrupts_restore(ipl);
[da1bafb]616
[33adc6ce]617 /* Wait for all answers to asynchronous calls to arrive */
[da1bafb]618 while (true) {
619 /*
620 * Go through all phones, until they are all FREE
621 * Locking is not needed, no one else should modify
622 * it when we are in cleanup
623 */
[8b243f2]624 for (i = 0; i < IPC_MAX_PHONES; i++) {
625 if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
[e701eb1]626 atomic_get(&TASK->phones[i].active_calls) == 0) {
[214c5a0]627 TASK->phones[i].state = IPC_PHONE_FREE;
[e701eb1]628 TASK->phones[i].callee = NULL;
629 }
[214c5a0]630
[da1bafb]631 /*
632 * Just for sure, we might have had some
633 * IPC_PHONE_CONNECTING phones
634 */
[214c5a0]635 if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
[d8f7362]636 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]637
638 /*
639 * If the hangup succeeded, it has sent a HANGUP
[d8f7362]640 * message, the IPC is now in HUNGUP state, we
[da1bafb]641 * wait for the reply to come
642 */
[214c5a0]643
644 if (TASK->phones[i].state != IPC_PHONE_FREE)
[eb3d379]645 break;
646 }
[da1bafb]647
648 /* Got into cleanup */
[eb3d379]649 if (i == IPC_MAX_PHONES)
650 break;
651
[da1bafb]652 call_t *call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
[8b243f2]653 SYNCH_FLAGS_NONE);
654 ASSERT((call->flags & IPC_CALL_ANSWERED) ||
655 (call->flags & IPC_CALL_NOTIF));
[ca687ad]656
657 ipc_call_free(call);
658 }
[4e49572]659}
[5626277]660
[da1bafb]661/** Initilize IPC subsystem
662 *
663 */
[5626277]664void ipc_init(void)
665{
[8b243f2]666 ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
667 NULL, 0);
[c70ce74]668 ipc_answerbox_slab = slab_cache_create("ipc_answerbox",
669 sizeof(answerbox_t), 0, NULL, NULL, 0);
[5626277]670}
671
[8b243f2]672/** List answerbox contents.
673 *
[da1bafb]674 * @param taskid Task ID.
675 *
[8b243f2]676 */
[c4e4507]677void ipc_print_task(task_id_t taskid)
678{
[da1bafb]679 irq_spinlock_lock(&tasks_lock, true);
680 task_t *task = task_find_by_id(taskid);
681
[170332d]682 if (!task) {
[da1bafb]683 irq_spinlock_unlock(&tasks_lock, true);
[c4e4507]684 return;
[170332d]685 }
[da1bafb]686
687 /* Hand-over-hand locking */
688 irq_spinlock_exchange(&tasks_lock, &task->lock);
689
[c4e4507]690 /* Print opened phones & details */
691 printf("PHONE:\n");
[da1bafb]692
693 size_t i;
[8b243f2]694 for (i = 0; i < IPC_MAX_PHONES; i++) {
[ff48a15]695 if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
[7e752b2]696 printf("%zu: mutex busy\n", i);
[ff48a15]697 continue;
698 }
[da1bafb]699
[c4e4507]700 if (task->phones[i].state != IPC_PHONE_FREE) {
[7e752b2]701 printf("%zu: ", i);
[da1bafb]702
[c4e4507]703 switch (task->phones[i].state) {
704 case IPC_PHONE_CONNECTING:
705 printf("connecting ");
706 break;
707 case IPC_PHONE_CONNECTED:
[e186eb5]708 printf("connected to: %p (%" PRIu64 ") ",
709 task->phones[i].callee,
710 task->phones[i].callee->task->taskid);
[c4e4507]711 break;
712 case IPC_PHONE_SLAMMED:
[fbf7b4c]713 printf("slammed by: %p ",
[da1bafb]714 task->phones[i].callee);
[c4e4507]715 break;
716 case IPC_PHONE_HUNGUP:
[fbf7b4c]717 printf("hung up - was: %p ",
[da1bafb]718 task->phones[i].callee);
[c4e4507]719 break;
720 default:
721 break;
722 }
[da1bafb]723
724 printf("active: %" PRIun "\n",
[bd72c3e9]725 atomic_get(&task->phones[i].active_calls));
[c4e4507]726 }
[da1bafb]727
[ff48a15]728 mutex_unlock(&task->phones[i].lock);
[c4e4507]729 }
[da1bafb]730
731 irq_spinlock_lock(&task->answerbox.lock, false);
732
733 link_t *cur;
734
[c4e4507]735 /* Print answerbox - calls */
736 printf("ABOX - CALLS:\n");
[da1bafb]737 for (cur = task->answerbox.calls.next; cur != &task->answerbox.calls;
738 cur = cur->next) {
739 call_t *call = list_get_instance(cur, call_t, link);
[9fe962d6]740 printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
[e40e3007]741 " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
742 " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
743 call->sender->taskid,
[228e490]744 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]745 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]746 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]747 call->flags);
[c4e4507]748 }
[da1bafb]749
750 /* Print answerbox - dispatched calls */
[c4e4507]751 printf("ABOX - DISPATCHED CALLS:\n");
[da1bafb]752 for (cur = task->answerbox.dispatched_calls.next;
753 cur != &task->answerbox.dispatched_calls;
754 cur = cur->next) {
755 call_t *call = list_get_instance(cur, call_t, link);
[9fe962d6]756 printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
[e40e3007]757 " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
758 " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
759 call->sender->taskid,
[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),
[bd72c3e9]763 call->flags);
[c4e4507]764 }
[da1bafb]765
766 /* Print answerbox - answers */
[c4e4507]767 printf("ABOX - ANSWERS:\n");
[da1bafb]768 for (cur = task->answerbox.answers.next;
769 cur != &task->answerbox.answers;
770 cur = cur->next) {
771 call_t *call = list_get_instance(cur, call_t, link);
[9fe962d6]772 printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun
[e40e3007]773 " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n",
[228e490]774 call, IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
[bd72c3e9]775 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
[38c706cc]776 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
[bd72c3e9]777 call->flags);
[c4e4507]778 }
[da1bafb]779
780 irq_spinlock_unlock(&task->answerbox.lock, false);
781 irq_spinlock_unlock(&task->lock, true);
[c4e4507]782}
[b45c443]783
[cc73a8a1]784/** @}
[b45c443]785 */
Note: See TracBrowser for help on using the repository browser.