source: mainline/generic/src/ipc/ipc.c@ c74804f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c74804f was d8f7362, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Small updates to ipc.

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