source: mainline/generic/src/ipc/ipc.c@ 116d1ef4

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

Replace nonblocking argument of waitq_sleep_timeout with flags that specify mode of operation.
Now a flag can be used to specify interruptible sleep.
Modify waitq_interrupt_sleep() to only interrupt threads that used this flag.
O

  • Property mode set to 100644
File size: 11.2 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
[2ba7810]113 ASSERT(!phone->callee);
[9f22213]114 phone->busy = IPC_BUSY_CONNECTED;
[6d9c49a]115 phone->callee = box;
[e74cb73]116
[2ba7810]117 spinlock_lock(&box->lock);
[6d9c49a]118 list_append(&phone->list, &box->connected_phones);
[2ba7810]119 spinlock_unlock(&box->lock);
[ba81cab]120
121 spinlock_unlock(&phone->lock);
[2ba7810]122}
123
[6ecc8bce]124/** Initialize phone structure and connect phone to answerbox
[2ba7810]125 */
126void ipc_phone_init(phone_t *phone)
127{
128 spinlock_initialize(&phone->lock, "phone_lock");
129 phone->callee = NULL;
[9f22213]130 phone->busy = IPC_BUSY_FREE;
131 atomic_set(&phone->active_calls, 0);
[6d9c49a]132}
133
[631ca4d]134/** Helper function to facilitate synchronous calls */
135void ipc_call_sync(phone_t *phone, call_t *request)
136{
137 answerbox_t sync_box;
138
139 ipc_answerbox_init(&sync_box);
140
141 /* We will receive data on special box */
142 request->callerbox = &sync_box;
143
144 ipc_call(phone, request);
[116d1ef4]145 ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
[631ca4d]146}
[6d9c49a]147
[ba81cab]148/** Answer message that was not dispatched and is not entered in
149 * any queue
150 */
151static void _ipc_answer_free_call(call_t *call)
152{
153 answerbox_t *callerbox = call->callerbox;
154
155 call->flags |= IPC_CALL_ANSWERED;
156
157 spinlock_lock(&callerbox->lock);
158 list_append(&call->list, &callerbox->answers);
159 spinlock_unlock(&callerbox->lock);
160 waitq_wakeup(&callerbox->wq, 0);
161}
162
163/** Answer message, that is in callee queue
164 *
165 * @param box Answerbox that is answering the message
166 * @param call Modified request that is being sent back
167 */
168void ipc_answer(answerbox_t *box, call_t *call)
169{
170 /* Remove from active box */
171 spinlock_lock(&box->lock);
172 list_remove(&call->list);
173 spinlock_unlock(&box->lock);
174 /* Send back answer */
175 _ipc_answer_free_call(call);
176}
177
[7c7aae16]178/** Simulate sending back a message
179 *
180 * Most errors are better handled by forming a normal backward
181 * message and sending it as a normal answer.
182 */
183void ipc_backsend_err(phone_t *phone, call_t *call, __native err)
184{
185 call->data.phone = phone;
186 atomic_inc(&phone->active_calls);
187 if (phone->busy == IPC_BUSY_CONNECTED)
188 IPC_SET_RETVAL(call->data, EHANGUP);
189 else
190 IPC_SET_RETVAL(call->data, ENOENT);
191
192 _ipc_answer_free_call(call);
193}
194
[fbcfd458]195/* Unsafe unchecking ipc_call */
196static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
197{
[9f22213]198 if (! (call->flags & IPC_CALL_FORWARDED)) {
199 atomic_inc(&phone->active_calls);
200 call->data.phone = phone;
201 }
[fbcfd458]202
203 spinlock_lock(&box->lock);
204 list_append(&call->list, &box->calls);
205 spinlock_unlock(&box->lock);
206 waitq_wakeup(&box->wq, 0);
207}
208
[631ca4d]209/** Send a asynchronous request using phone to answerbox
[6d9c49a]210 *
211 * @param phone Phone connected to answerbox
212 * @param request Request to be sent
213 */
[9f22213]214int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]215{
[ba81cab]216 answerbox_t *box;
[6d9c49a]217
[ba81cab]218 spinlock_lock(&phone->lock);
[fbcfd458]219
[ba81cab]220 box = phone->callee;
221 if (!box) {
222 /* Trying to send over disconnected phone */
[fbcfd458]223 spinlock_unlock(&phone->lock);
[9f22213]224 if (call->flags & IPC_CALL_FORWARDED) {
225 IPC_SET_RETVAL(call->data, EFORWARD);
[7c7aae16]226 _ipc_answer_free_call(call);
227 } else { /* Simulate sending back a message */
[9f22213]228 if (phone->busy == IPC_BUSY_CONNECTED)
[7c7aae16]229 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]230 else
[7c7aae16]231 ipc_backsend_err(phone, call, ENOENT);
[9f22213]232 }
[fbcfd458]233
[9f22213]234 return ENOENT;
[ba81cab]235 }
[fbcfd458]236 _ipc_call(phone, box, call);
237
238 spinlock_unlock(&phone->lock);
[9f22213]239 return 0;
[fbcfd458]240}
241
242/** Disconnect phone from answerbox
243 *
244 * It is allowed to call disconnect on already disconnected phone
245 *
246 * @return 0 - phone disconnected, -1 - the phone was already disconnected
247 */
248int ipc_phone_hangup(phone_t *phone)
249{
250 answerbox_t *box;
251 call_t *call;
252
253 spinlock_lock(&phone->lock);
254 box = phone->callee;
255 if (!box) {
[9f22213]256 if (phone->busy == IPC_BUSY_CONNECTING) {
257 spinlock_unlock(&phone->lock);
258 return -1;
259 }
260 /* Already disconnected phone */
261 phone->busy = IPC_BUSY_FREE;
[fbcfd458]262 spinlock_unlock(&phone->lock);
[9f22213]263 return 0;
[fbcfd458]264 }
[6d9c49a]265
[2ba7810]266 spinlock_lock(&box->lock);
[fbcfd458]267 list_remove(&phone->list);
268 phone->callee = NULL;
[2ba7810]269 spinlock_unlock(&box->lock);
[fbcfd458]270
[5626277]271 call = ipc_call_alloc(0);
[fbcfd458]272 IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
273 call->flags |= IPC_CALL_DISCARD_ANSWER;
274 _ipc_call(phone, box, call);
275
[9f22213]276 phone->busy = IPC_BUSY_FREE;
[fbcfd458]277
[ba81cab]278 spinlock_unlock(&phone->lock);
[fbcfd458]279
280 return 0;
[2ba7810]281}
282
283/** Forwards call from one answerbox to a new one
284 *
[ad575d7]285 * @param call Call to be redirected.
286 * @param newphone Phone to target answerbox.
[2ba7810]287 * @param oldbox Old answerbox
[9f22213]288 * @return 0 on forward ok, error code, if there was error
289 *
290 * - the return value serves only as an information for the forwarder,
291 * the original caller is notified automatically with EFORWARD
[2ba7810]292 */
[9f22213]293int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox)
[2ba7810]294{
295 spinlock_lock(&oldbox->lock);
296 list_remove(&call->list);
297 spinlock_unlock(&oldbox->lock);
298
[9f22213]299 return ipc_call(newphone, call);
[6d9c49a]300}
301
302
303/** Wait for phone call
304 *
[bd5a663]305 * @param box Answerbox expecting the call.
306 * @param usec Timeout in microseconds. See documentation for waitq_sleep_timeout() for
307 * decription of its special meaning.
[116d1ef4]308 * @param flags Select mode of sleep operation. See documentation for waitq_sleep_timeout()i
309 * for description of its special meaning.
[6d9c49a]310 * @return Recived message address
311 * - to distinguish between call and answer, look at call->flags
312 */
[116d1ef4]313call_t * ipc_wait_for_call(answerbox_t *box, __u32 usec, int flags)
[6d9c49a]314{
315 call_t *request;
[5626277]316 ipl_t ipl;
[bd5a663]317 int rc;
[6d9c49a]318
[bd5a663]319restart:
[116d1ef4]320 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]321 if (SYNCH_FAILED(rc))
322 return NULL;
[fbcfd458]323
[2ba7810]324 spinlock_lock(&box->lock);
[5626277]325 if (!list_empty(&box->irq_notifs)) {
326 ipl = interrupts_disable();
327 spinlock_lock(&box->irq_lock);
328
[162f919]329 request = list_get_instance(box->irq_notifs.next, call_t, list);
[5626277]330 list_remove(&request->list);
331
332 spinlock_unlock(&box->irq_lock);
333 interrupts_restore(ipl);
334 } else if (!list_empty(&box->answers)) {
[fbcfd458]335 /* Handle asynchronous answers */
336 request = list_get_instance(box->answers.next, call_t, list);
337 list_remove(&request->list);
338 atomic_dec(&request->data.phone->active_calls);
339 } else if (!list_empty(&box->calls)) {
340 /* Handle requests */
341 request = list_get_instance(box->calls.next, call_t, list);
342 list_remove(&request->list);
343 /* Append request to dispatch queue */
344 list_append(&request->list, &box->dispatched_calls);
345 } else {
[ca687ad]346 /* This can happen regularly after ipc_cleanup, remove
347 * the warning in the future when the IPC is
348 * more debugged */
[fbcfd458]349 printf("WARNING: Spurious IPC wakeup.\n");
350 spinlock_unlock(&box->lock);
351 goto restart;
[6d9c49a]352 }
[2ba7810]353 spinlock_unlock(&box->lock);
[6d9c49a]354 return request;
355}
356
[ca687ad]357/** Answer all calls from list with EHANGUP msg */
358static void ipc_cleanup_call_list(link_t *lst)
359{
360 call_t *call;
361
362 while (!list_empty(lst)) {
363 call = list_get_instance(lst->next, call_t, list);
364 list_remove(&call->list);
365
366 IPC_SET_RETVAL(call->data, EHANGUP);
367 _ipc_answer_free_call(call);
368 }
369}
370
[4e49572]371/** Cleans up all IPC communication of the given task
372 *
373 *
374 */
375void ipc_cleanup(task_t *task)
376{
[9f22213]377 int i;
[ca687ad]378 call_t *call;
379 phone_t *phone;
380
[9f22213]381 /* Disconnect all our phones ('ipc_phone_hangup') */
382 for (i=0;i < IPC_MAX_PHONES; i++)
383 ipc_phone_hangup(&task->phones[i]);
384
[5626277]385 /* Disconnect all connected irqs */
386 ipc_irq_cleanup(&task->answerbox);
387
[ca687ad]388 /* Disconnect all phones connected to our answerbox */
389restart_phones:
390 spinlock_lock(&task->answerbox.lock);
391 while (!list_empty(&task->answerbox.connected_phones)) {
392 phone = list_get_instance(task->answerbox.connected_phones.next,
393 phone_t,
394 list);
395 if (! spinlock_trylock(&phone->lock)) {
396 spinlock_unlock(&task->answerbox.lock);
397 goto restart_phones;
398 }
399
400 /* Disconnect phone */
401 phone->callee = NULL;
402 list_remove(&phone->list);
403
404 spinlock_unlock(&phone->lock);
405 }
[9f22213]406
407 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[ca687ad]408 spinlock_lock(&task->answerbox.lock);
409 ipc_cleanup_call_list(&task->answerbox.dispatched_calls);
410 ipc_cleanup_call_list(&task->answerbox.calls);
411 spinlock_unlock(&task->answerbox.lock);
[4e49572]412
[9f22213]413 /* Wait for all async answers to arrive */
[ca687ad]414 while (atomic_get(&task->active_calls)) {
[116d1ef4]415 call = ipc_wait_for_call(&task->answerbox, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
[51a7dc1]416 ASSERT((call->flags & IPC_CALL_ANSWERED) || (call->flags & IPC_CALL_NOTIF));
[ca687ad]417 ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
418
419 atomic_dec(&task->active_calls);
420 ipc_call_free(call);
421 }
[4e49572]422}
[5626277]423
424
425/** Initilize ipc subsystem */
426void ipc_init(void)
427{
428 ipc_call_slab = slab_cache_create("ipc_call",
429 sizeof(call_t),
430 0,
431 NULL, NULL, 0);
432 ipc_irq_make_table(IRQ_COUNT);
433}
434
Note: See TracBrowser for help on using the repository browser.