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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a9ac978 was 11675207, checked in by jermar <jermar@…>, 17 years ago

Move everything to kernel/.

  • Property mode set to 100644
File size: 14.5 KB
RevLine 
[6d9c49a]1/*
2 * Copyright (C) 2006 Ondrej Palkovsky
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 *
37 * First the answerbox, then the phone
38 */
39
[2ba7810]40#include <synch/spinlock.h>
41#include <synch/waitq.h>
[bd5a663]42#include <synch/synch.h>
[6d9c49a]43#include <ipc/ipc.h>
44#include <errno.h>
45#include <mm/slab.h>
46#include <arch.h>
47#include <proc/task.h>
48#include <memstr.h>
49#include <debug.h>
50
51#include <print.h>
52#include <proc/thread.h>
[5626277]53#include <arch/interrupt.h>
[162f919]54#include <ipc/irq.h>
[6d9c49a]55
[e74cb73]56/* Open channel that is assigned automatically to new tasks */
57answerbox_t *ipc_phone_0 = NULL;
[6d9c49a]58
59static slab_cache_t *ipc_call_slab;
60
[ba81cab]61/* Initialize new call */
62static void _ipc_call_init(call_t *call)
63{
[7f1c620]64 memsetb((uintptr_t)call, sizeof(*call), 0);
[ba81cab]65 call->callerbox = &TASK->answerbox;
66 call->sender = TASK;
67}
68
[6d9c49a]69/** Allocate & initialize call structure
70 *
71 * The call is initialized, so that the reply will be directed
72 * to TASK->answerbox
[5626277]73 *
74 * @param flags Parameters for slab_alloc (ATOMIC, etc.)
[6d9c49a]75 */
[5626277]76call_t * ipc_call_alloc(int flags)
[6d9c49a]77{
78 call_t *call;
79
[5626277]80 call = slab_alloc(ipc_call_slab, flags);
[ba81cab]81 _ipc_call_init(call);
[6d9c49a]82
83 return call;
84}
85
[e74cb73]86/** Initialize allocated call */
[ba81cab]87void ipc_call_static_init(call_t *call)
[e74cb73]88{
[ba81cab]89 _ipc_call_init(call);
90 call->flags |= IPC_CALL_STATIC_ALLOC;
[e74cb73]91}
92
[6d9c49a]93/** Deallocate call stracuture */
94void ipc_call_free(call_t *call)
95{
96 slab_free(ipc_call_slab, call);
97}
98
99/** Initialize answerbox structure
100 */
101void ipc_answerbox_init(answerbox_t *box)
102{
[2ba7810]103 spinlock_initialize(&box->lock, "ipc_box_lock");
[5626277]104 spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
[2ba7810]105 waitq_initialize(&box->wq);
[6d9c49a]106 list_initialize(&box->connected_phones);
107 list_initialize(&box->calls);
108 list_initialize(&box->dispatched_calls);
109 list_initialize(&box->answers);
[5626277]110 list_initialize(&box->irq_notifs);
[2d5a54f3]111 box->task = TASK;
[6d9c49a]112}
113
[2ba7810]114/** Connect phone to answerbox */
115void ipc_phone_connect(phone_t *phone, answerbox_t *box)
[6d9c49a]116{
[ba81cab]117 spinlock_lock(&phone->lock);
118
[eb3d379]119 phone->state = IPC_PHONE_CONNECTED;
[6d9c49a]120 phone->callee = box;
[e74cb73]121
[2ba7810]122 spinlock_lock(&box->lock);
[c4e4507]123 list_append(&phone->link, &box->connected_phones);
[2ba7810]124 spinlock_unlock(&box->lock);
[ba81cab]125
126 spinlock_unlock(&phone->lock);
[2ba7810]127}
128
[6ecc8bce]129/** Initialize phone structure and connect phone to answerbox
[2ba7810]130 */
131void ipc_phone_init(phone_t *phone)
132{
133 spinlock_initialize(&phone->lock, "phone_lock");
134 phone->callee = NULL;
[eb3d379]135 phone->state = IPC_PHONE_FREE;
[9f22213]136 atomic_set(&phone->active_calls, 0);
[6d9c49a]137}
138
[631ca4d]139/** Helper function to facilitate synchronous calls */
140void ipc_call_sync(phone_t *phone, call_t *request)
141{
142 answerbox_t sync_box;
143
144 ipc_answerbox_init(&sync_box);
145
146 /* We will receive data on special box */
147 request->callerbox = &sync_box;
148
149 ipc_call(phone, request);
[116d1ef4]150 ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
[631ca4d]151}
[6d9c49a]152
[ba81cab]153/** Answer message that was not dispatched and is not entered in
154 * any queue
155 */
156static void _ipc_answer_free_call(call_t *call)
157{
158 answerbox_t *callerbox = call->callerbox;
159
160 call->flags |= IPC_CALL_ANSWERED;
161
162 spinlock_lock(&callerbox->lock);
[c4e4507]163 list_append(&call->link, &callerbox->answers);
[ba81cab]164 spinlock_unlock(&callerbox->lock);
165 waitq_wakeup(&callerbox->wq, 0);
166}
167
168/** Answer message, that is in callee queue
169 *
170 * @param box Answerbox that is answering the message
171 * @param call Modified request that is being sent back
172 */
173void ipc_answer(answerbox_t *box, call_t *call)
174{
175 /* Remove from active box */
176 spinlock_lock(&box->lock);
[c4e4507]177 list_remove(&call->link);
[ba81cab]178 spinlock_unlock(&box->lock);
179 /* Send back answer */
180 _ipc_answer_free_call(call);
181}
182
[7c7aae16]183/** Simulate sending back a message
184 *
185 * Most errors are better handled by forming a normal backward
186 * message and sending it as a normal answer.
187 */
[7f1c620]188void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
[7c7aae16]189{
190 call->data.phone = phone;
191 atomic_inc(&phone->active_calls);
[eb3d379]192 IPC_SET_RETVAL(call->data, err);
[7c7aae16]193 _ipc_answer_free_call(call);
194}
195
[fbcfd458]196/* Unsafe unchecking ipc_call */
197static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
198{
[9f22213]199 if (! (call->flags & IPC_CALL_FORWARDED)) {
200 atomic_inc(&phone->active_calls);
201 call->data.phone = phone;
202 }
[fbcfd458]203
204 spinlock_lock(&box->lock);
[c4e4507]205 list_append(&call->link, &box->calls);
[fbcfd458]206 spinlock_unlock(&box->lock);
207 waitq_wakeup(&box->wq, 0);
208}
209
[631ca4d]210/** Send a asynchronous request using phone to answerbox
[6d9c49a]211 *
[abbc16e]212 * @param phone Phone connected to answerbox.
213 * @param call Structure representing the call.
[6d9c49a]214 */
[9f22213]215int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]216{
[ba81cab]217 answerbox_t *box;
[6d9c49a]218
[ba81cab]219 spinlock_lock(&phone->lock);
[eb3d379]220 if (phone->state != IPC_PHONE_CONNECTED) {
[fbcfd458]221 spinlock_unlock(&phone->lock);
[9f22213]222 if (call->flags & IPC_CALL_FORWARDED) {
223 IPC_SET_RETVAL(call->data, EFORWARD);
[7c7aae16]224 _ipc_answer_free_call(call);
[eb3d379]225 } else {
226 if (phone->state == IPC_PHONE_HUNGUP)
[7c7aae16]227 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]228 else
[7c7aae16]229 ipc_backsend_err(phone, call, ENOENT);
[9f22213]230 }
231 return ENOENT;
[ba81cab]232 }
[eb3d379]233 box = phone->callee;
[fbcfd458]234 _ipc_call(phone, box, call);
235
236 spinlock_unlock(&phone->lock);
[9f22213]237 return 0;
[fbcfd458]238}
239
240/** Disconnect phone from answerbox
241 *
[eb3d379]242 * This call leaves the phone in HUNGUP state. The change to 'free' is done
243 * lazily later.
[fbcfd458]244 *
[eb3d379]245 * @param phone Phone to be hung up
246 *
[fbcfd458]247 * @return 0 - phone disconnected, -1 - the phone was already disconnected
248 */
[d8f7362]249int ipc_phone_hangup(phone_t *phone)
[fbcfd458]250{
251 answerbox_t *box;
252 call_t *call;
253
254 spinlock_lock(&phone->lock);
[eb3d379]255 if (phone->state == IPC_PHONE_FREE || phone->state ==IPC_PHONE_HUNGUP \
256 || phone->state == IPC_PHONE_CONNECTING) {
[fbcfd458]257 spinlock_unlock(&phone->lock);
[eb3d379]258 return -1;
[fbcfd458]259 }
[eb3d379]260 box = phone->callee;
261 if (phone->state != IPC_PHONE_SLAMMED) {
262 /* Remove myself from answerbox */
263 spinlock_lock(&box->lock);
[c4e4507]264 list_remove(&phone->link);
[eb3d379]265 spinlock_unlock(&box->lock);
[6d9c49a]266
[eb3d379]267 if (phone->state != IPC_PHONE_SLAMMED) {
268 call = ipc_call_alloc(0);
269 IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
270 call->flags |= IPC_CALL_DISCARD_ANSWER;
271 _ipc_call(phone, box, call);
272 }
273 }
[fbcfd458]274
[eb3d379]275 phone->state = IPC_PHONE_HUNGUP;
[ba81cab]276 spinlock_unlock(&phone->lock);
[fbcfd458]277
278 return 0;
[2ba7810]279}
280
281/** Forwards call from one answerbox to a new one
282 *
[ad575d7]283 * @param call Call to be redirected.
284 * @param newphone Phone to target answerbox.
[2ba7810]285 * @param oldbox Old answerbox
[9f22213]286 * @return 0 on forward ok, error code, if there was error
287 *
288 * - the return value serves only as an information for the forwarder,
289 * the original caller is notified automatically with EFORWARD
[2ba7810]290 */
[9f22213]291int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox)
[2ba7810]292{
293 spinlock_lock(&oldbox->lock);
[c4e4507]294 list_remove(&call->link);
[2ba7810]295 spinlock_unlock(&oldbox->lock);
296
[9f22213]297 return ipc_call(newphone, call);
[6d9c49a]298}
299
300
301/** Wait for phone call
302 *
[bd5a663]303 * @param box Answerbox expecting the call.
304 * @param usec Timeout in microseconds. See documentation for waitq_sleep_timeout() for
305 * decription of its special meaning.
[116d1ef4]306 * @param flags Select mode of sleep operation. See documentation for waitq_sleep_timeout()i
307 * for description of its special meaning.
[6d9c49a]308 * @return Recived message address
309 * - to distinguish between call and answer, look at call->flags
310 */
[7f1c620]311call_t * ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
[6d9c49a]312{
313 call_t *request;
[5626277]314 ipl_t ipl;
[bd5a663]315 int rc;
[6d9c49a]316
[bd5a663]317restart:
[116d1ef4]318 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]319 if (SYNCH_FAILED(rc))
320 return NULL;
[fbcfd458]321
[2ba7810]322 spinlock_lock(&box->lock);
[5626277]323 if (!list_empty(&box->irq_notifs)) {
324 ipl = interrupts_disable();
325 spinlock_lock(&box->irq_lock);
326
[c4e4507]327 request = list_get_instance(box->irq_notifs.next, call_t, link);
328 list_remove(&request->link);
[5626277]329
330 spinlock_unlock(&box->irq_lock);
331 interrupts_restore(ipl);
332 } else if (!list_empty(&box->answers)) {
[fbcfd458]333 /* Handle asynchronous answers */
[c4e4507]334 request = list_get_instance(box->answers.next, call_t, link);
335 list_remove(&request->link);
[fbcfd458]336 atomic_dec(&request->data.phone->active_calls);
337 } else if (!list_empty(&box->calls)) {
338 /* Handle requests */
[c4e4507]339 request = list_get_instance(box->calls.next, call_t, link);
340 list_remove(&request->link);
[fbcfd458]341 /* Append request to dispatch queue */
[c4e4507]342 list_append(&request->link, &box->dispatched_calls);
[fbcfd458]343 } else {
[874621f]344 /* This can happen regularly after ipc_cleanup */
[fbcfd458]345 spinlock_unlock(&box->lock);
346 goto restart;
[6d9c49a]347 }
[2ba7810]348 spinlock_unlock(&box->lock);
[6d9c49a]349 return request;
350}
351
[ca687ad]352/** Answer all calls from list with EHANGUP msg */
353static void ipc_cleanup_call_list(link_t *lst)
354{
355 call_t *call;
356
357 while (!list_empty(lst)) {
[c4e4507]358 call = list_get_instance(lst->next, call_t, link);
359 list_remove(&call->link);
[ca687ad]360
361 IPC_SET_RETVAL(call->data, EHANGUP);
362 _ipc_answer_free_call(call);
363 }
364}
365
[214c5a0]366/** Cleans up all IPC communication of the current task
[4e49572]367 *
[214c5a0]368 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
369 * have to change it as well if you want to cleanup other current then current.
[4e49572]370 */
[214c5a0]371void ipc_cleanup(void)
[4e49572]372{
[9f22213]373 int i;
[ca687ad]374 call_t *call;
375 phone_t *phone;
[214c5a0]376
[9f22213]377 /* Disconnect all our phones ('ipc_phone_hangup') */
378 for (i=0;i < IPC_MAX_PHONES; i++)
[d8f7362]379 ipc_phone_hangup(&TASK->phones[i]);
[9f22213]380
[5626277]381 /* Disconnect all connected irqs */
[214c5a0]382 ipc_irq_cleanup(&TASK->answerbox);
[5626277]383
[ca687ad]384 /* Disconnect all phones connected to our answerbox */
385restart_phones:
[214c5a0]386 spinlock_lock(&TASK->answerbox.lock);
387 while (!list_empty(&TASK->answerbox.connected_phones)) {
388 phone = list_get_instance(TASK->answerbox.connected_phones.next,
[c4e4507]389 phone_t, link);
[ca687ad]390 if (! spinlock_trylock(&phone->lock)) {
[214c5a0]391 spinlock_unlock(&TASK->answerbox.lock);
[ca687ad]392 goto restart_phones;
393 }
394
395 /* Disconnect phone */
[eb3d379]396 ASSERT(phone->state == IPC_PHONE_CONNECTED);
397 phone->state = IPC_PHONE_SLAMMED;
[c4e4507]398 list_remove(&phone->link);
[ca687ad]399
400 spinlock_unlock(&phone->lock);
401 }
[9f22213]402
403 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[214c5a0]404 ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
405 ipc_cleanup_call_list(&TASK->answerbox.calls);
406 spinlock_unlock(&TASK->answerbox.lock);
[4e49572]407
[9f22213]408 /* Wait for all async answers to arrive */
[eb3d379]409 while (1) {
410 /* Go through all phones, until all are FREE... */
411 /* Locking not needed, no one else should modify
412 * it, when we are in cleanup */
413 for (i=0;i < IPC_MAX_PHONES; i++) {
[214c5a0]414 if (TASK->phones[i].state == IPC_PHONE_HUNGUP && \
415 atomic_get(&TASK->phones[i].active_calls) == 0)
416 TASK->phones[i].state = IPC_PHONE_FREE;
417
[d8f7362]418 /* Just for sure, we might have had some
419 * IPC_PHONE_CONNECTING phones */
[214c5a0]420 if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
[d8f7362]421 ipc_phone_hangup(&TASK->phones[i]);
422 /* If the hangup succeeded, it has sent a HANGUP
423 * message, the IPC is now in HUNGUP state, we
424 * wait for the reply to come */
[214c5a0]425
426 if (TASK->phones[i].state != IPC_PHONE_FREE)
[eb3d379]427 break;
428 }
429 /* Voila, got into cleanup */
430 if (i == IPC_MAX_PHONES)
431 break;
432
[214c5a0]433 call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
[51a7dc1]434 ASSERT((call->flags & IPC_CALL_ANSWERED) || (call->flags & IPC_CALL_NOTIF));
[ca687ad]435 ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
436
[214c5a0]437 atomic_dec(&TASK->active_calls);
[ca687ad]438 ipc_call_free(call);
439 }
[4e49572]440}
[5626277]441
442
443/** Initilize ipc subsystem */
444void ipc_init(void)
445{
446 ipc_call_slab = slab_cache_create("ipc_call",
447 sizeof(call_t),
448 0,
449 NULL, NULL, 0);
450 ipc_irq_make_table(IRQ_COUNT);
451}
452
[c4e4507]453
454/** Kconsole - list answerbox contents */
455void ipc_print_task(task_id_t taskid)
456{
457 task_t *task;
458 int i;
459 call_t *call;
460 link_t *tmp;
461
462 spinlock_lock(&tasks_lock);
463 task = task_find_by_id(taskid);
464 if (task)
465 spinlock_lock(&task->lock);
466 spinlock_unlock(&tasks_lock);
467 if (!task)
468 return;
469
470 /* Print opened phones & details */
471 printf("PHONE:\n");
472 for (i=0; i < IPC_MAX_PHONES;i++) {
473 spinlock_lock(&task->phones[i].lock);
474 if (task->phones[i].state != IPC_PHONE_FREE) {
475 printf("%d: ",i);
476 switch (task->phones[i].state) {
477 case IPC_PHONE_CONNECTING:
478 printf("connecting ");
479 break;
480 case IPC_PHONE_CONNECTED:
[fbf7b4c]481 printf("connected to: %p ",
[c4e4507]482 task->phones[i].callee);
483 break;
484 case IPC_PHONE_SLAMMED:
[fbf7b4c]485 printf("slammed by: %p ",
[c4e4507]486 task->phones[i].callee);
487 break;
488 case IPC_PHONE_HUNGUP:
[fbf7b4c]489 printf("hung up - was: %p ",
[c4e4507]490 task->phones[i].callee);
491 break;
492 default:
493 break;
494 }
495 printf("active: %d\n", atomic_get(&task->phones[i].active_calls));
496 }
497 spinlock_unlock(&task->phones[i].lock);
498 }
499
500
501 /* Print answerbox - calls */
502 spinlock_lock(&task->answerbox.lock);
503 printf("ABOX - CALLS:\n");
504 for (tmp=task->answerbox.calls.next; tmp != &task->answerbox.calls;tmp = tmp->next) {
505 call = list_get_instance(tmp, call_t, link);
[fbf7b4c]506 printf("Callid: %p Srctask:%lld M:%d A1:%d A2:%d A3:%d Flags:%x\n",call,
[c4e4507]507 call->sender->taskid, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
508 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), call->flags);
509 }
510 /* Print answerbox - calls */
511 printf("ABOX - DISPATCHED CALLS:\n");
512 for (tmp=task->answerbox.dispatched_calls.next;
513 tmp != &task->answerbox.dispatched_calls;
514 tmp = tmp->next) {
515 call = list_get_instance(tmp, call_t, link);
[fbf7b4c]516 printf("Callid: %p Srctask:%lld M:%d A1:%d A2:%d A3:%d Flags:%x\n",call,
[c4e4507]517 call->sender->taskid, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
518 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), call->flags);
519 }
520 /* Print answerbox - calls */
521 printf("ABOX - ANSWERS:\n");
522 for (tmp=task->answerbox.answers.next; tmp != &task->answerbox.answers; tmp = tmp->next) {
523 call = list_get_instance(tmp, call_t, link);
[fbf7b4c]524 printf("Callid:%p M:%d A1:%d A2:%d A3:%d Flags:%x\n",call,
[c4e4507]525 IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
526 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), call->flags);
527 }
528
529 spinlock_unlock(&task->answerbox.lock);
530 spinlock_unlock(&task->lock);
531}
[b45c443]532
[cc73a8a1]533/** @}
[b45c443]534 */
Note: See TracBrowser for help on using the repository browser.