source: mainline/generic/src/ipc/ipc.c@ 6ecc8bce

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

Make ia64 and sparc64 compile again.
Fix ia64 to set up inits in arch_pre_main().

  • Property mode set to 100644
File size: 10.3 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>
[6d9c49a]36#include <ipc/ipc.h>
37#include <errno.h>
38#include <mm/slab.h>
39#include <arch.h>
40#include <proc/task.h>
41#include <memstr.h>
42#include <debug.h>
43
44#include <print.h>
45#include <proc/thread.h>
46
[e74cb73]47/* Open channel that is assigned automatically to new tasks */
48answerbox_t *ipc_phone_0 = NULL;
[6d9c49a]49
50static slab_cache_t *ipc_call_slab;
51
[ba81cab]52/* Initialize new call */
53static void _ipc_call_init(call_t *call)
54{
55 memsetb((__address)call, sizeof(*call), 0);
56 call->callerbox = &TASK->answerbox;
57 call->sender = TASK;
58}
59
[6d9c49a]60/** Allocate & initialize call structure
61 *
62 * The call is initialized, so that the reply will be directed
63 * to TASK->answerbox
64 */
65call_t * ipc_call_alloc(void)
66{
67 call_t *call;
68
69 call = slab_alloc(ipc_call_slab, 0);
[ba81cab]70 _ipc_call_init(call);
[6d9c49a]71
72 return call;
73}
74
[e74cb73]75/** Initialize allocated call */
[ba81cab]76void ipc_call_static_init(call_t *call)
[e74cb73]77{
[ba81cab]78 _ipc_call_init(call);
79 call->flags |= IPC_CALL_STATIC_ALLOC;
[e74cb73]80}
81
[6d9c49a]82/** Deallocate call stracuture */
83void ipc_call_free(call_t *call)
84{
85 slab_free(ipc_call_slab, call);
86}
87
88/** Initialize answerbox structure
89 */
90void ipc_answerbox_init(answerbox_t *box)
91{
[2ba7810]92 spinlock_initialize(&box->lock, "ipc_box_lock");
93 waitq_initialize(&box->wq);
[6d9c49a]94 list_initialize(&box->connected_phones);
95 list_initialize(&box->calls);
96 list_initialize(&box->dispatched_calls);
97 list_initialize(&box->answers);
[2d5a54f3]98 box->task = TASK;
[6d9c49a]99}
100
[2ba7810]101/** Connect phone to answerbox */
102void ipc_phone_connect(phone_t *phone, answerbox_t *box)
[6d9c49a]103{
[ba81cab]104 spinlock_lock(&phone->lock);
105
[2ba7810]106 ASSERT(!phone->callee);
[9f22213]107 phone->busy = IPC_BUSY_CONNECTED;
[6d9c49a]108 phone->callee = box;
[e74cb73]109
[2ba7810]110 spinlock_lock(&box->lock);
[6d9c49a]111 list_append(&phone->list, &box->connected_phones);
[2ba7810]112 spinlock_unlock(&box->lock);
[ba81cab]113
114 spinlock_unlock(&phone->lock);
[2ba7810]115}
116
[6ecc8bce]117/** Initialize phone structure and connect phone to answerbox
[2ba7810]118 */
119void ipc_phone_init(phone_t *phone)
120{
121 spinlock_initialize(&phone->lock, "phone_lock");
122 phone->callee = NULL;
[9f22213]123 phone->busy = IPC_BUSY_FREE;
124 atomic_set(&phone->active_calls, 0);
[6d9c49a]125}
126
[631ca4d]127/** Helper function to facilitate synchronous calls */
128void ipc_call_sync(phone_t *phone, call_t *request)
129{
130 answerbox_t sync_box;
131
132 ipc_answerbox_init(&sync_box);
133
134 /* We will receive data on special box */
135 request->callerbox = &sync_box;
136
137 ipc_call(phone, request);
138 ipc_wait_for_call(&sync_box, 0);
139}
[6d9c49a]140
[ba81cab]141/** Answer message that was not dispatched and is not entered in
142 * any queue
143 */
144static void _ipc_answer_free_call(call_t *call)
145{
146 answerbox_t *callerbox = call->callerbox;
147
148 call->flags |= IPC_CALL_ANSWERED;
149
150 spinlock_lock(&callerbox->lock);
151 list_append(&call->list, &callerbox->answers);
152 spinlock_unlock(&callerbox->lock);
153 waitq_wakeup(&callerbox->wq, 0);
154}
155
156/** Answer message, that is in callee queue
157 *
158 * @param box Answerbox that is answering the message
159 * @param call Modified request that is being sent back
160 */
161void ipc_answer(answerbox_t *box, call_t *call)
162{
163 /* Remove from active box */
164 spinlock_lock(&box->lock);
165 list_remove(&call->list);
166 spinlock_unlock(&box->lock);
167 /* Send back answer */
168 _ipc_answer_free_call(call);
169}
170
[7c7aae16]171/** Simulate sending back a message
172 *
173 * Most errors are better handled by forming a normal backward
174 * message and sending it as a normal answer.
175 */
176void ipc_backsend_err(phone_t *phone, call_t *call, __native err)
177{
178 call->data.phone = phone;
179 atomic_inc(&phone->active_calls);
180 if (phone->busy == IPC_BUSY_CONNECTED)
181 IPC_SET_RETVAL(call->data, EHANGUP);
182 else
183 IPC_SET_RETVAL(call->data, ENOENT);
184
185 _ipc_answer_free_call(call);
186}
187
[fbcfd458]188/* Unsafe unchecking ipc_call */
189static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
190{
[9f22213]191 if (! (call->flags & IPC_CALL_FORWARDED)) {
192 atomic_inc(&phone->active_calls);
193 call->data.phone = phone;
194 }
[fbcfd458]195
196 spinlock_lock(&box->lock);
197 list_append(&call->list, &box->calls);
198 spinlock_unlock(&box->lock);
199 waitq_wakeup(&box->wq, 0);
200}
201
[631ca4d]202/** Send a asynchronous request using phone to answerbox
[6d9c49a]203 *
204 * @param phone Phone connected to answerbox
205 * @param request Request to be sent
206 */
[9f22213]207int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]208{
[ba81cab]209 answerbox_t *box;
[6d9c49a]210
[ba81cab]211 spinlock_lock(&phone->lock);
[fbcfd458]212
[ba81cab]213 box = phone->callee;
214 if (!box) {
215 /* Trying to send over disconnected phone */
[fbcfd458]216 spinlock_unlock(&phone->lock);
[9f22213]217 if (call->flags & IPC_CALL_FORWARDED) {
218 IPC_SET_RETVAL(call->data, EFORWARD);
[7c7aae16]219 _ipc_answer_free_call(call);
220 } else { /* Simulate sending back a message */
[9f22213]221 if (phone->busy == IPC_BUSY_CONNECTED)
[7c7aae16]222 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]223 else
[7c7aae16]224 ipc_backsend_err(phone, call, ENOENT);
[9f22213]225 }
[fbcfd458]226
[9f22213]227 return ENOENT;
[ba81cab]228 }
[fbcfd458]229 _ipc_call(phone, box, call);
230
231 spinlock_unlock(&phone->lock);
[9f22213]232 return 0;
[fbcfd458]233}
234
235/** Disconnect phone from answerbox
236 *
237 * It is allowed to call disconnect on already disconnected phone
238 *
239 * @return 0 - phone disconnected, -1 - the phone was already disconnected
240 */
241int ipc_phone_hangup(phone_t *phone)
242{
243 answerbox_t *box;
244 call_t *call;
245
246 spinlock_lock(&phone->lock);
247 box = phone->callee;
248 if (!box) {
[9f22213]249 if (phone->busy == IPC_BUSY_CONNECTING) {
250 spinlock_unlock(&phone->lock);
251 return -1;
252 }
253 /* Already disconnected phone */
254 phone->busy = IPC_BUSY_FREE;
[fbcfd458]255 spinlock_unlock(&phone->lock);
[9f22213]256 return 0;
[fbcfd458]257 }
[6d9c49a]258
[2ba7810]259 spinlock_lock(&box->lock);
[fbcfd458]260 list_remove(&phone->list);
261 phone->callee = NULL;
[2ba7810]262 spinlock_unlock(&box->lock);
[fbcfd458]263
264 call = ipc_call_alloc();
265 IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
266 call->flags |= IPC_CALL_DISCARD_ANSWER;
267 _ipc_call(phone, box, call);
268
[9f22213]269 phone->busy = IPC_BUSY_FREE;
[fbcfd458]270
[ba81cab]271 spinlock_unlock(&phone->lock);
[fbcfd458]272
273 return 0;
[2ba7810]274}
275
276/** Forwards call from one answerbox to a new one
277 *
278 * @param request Request to be forwarded
279 * @param newbox Target answerbox
280 * @param oldbox Old answerbox
[9f22213]281 * @return 0 on forward ok, error code, if there was error
282 *
283 * - the return value serves only as an information for the forwarder,
284 * the original caller is notified automatically with EFORWARD
[2ba7810]285 */
[9f22213]286int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox)
[2ba7810]287{
288 spinlock_lock(&oldbox->lock);
289 list_remove(&call->list);
290 spinlock_unlock(&oldbox->lock);
291
[9f22213]292 return ipc_call(newphone, call);
[6d9c49a]293}
294
295
296/** Wait for phone call
297 *
298 * @return Recived message address
299 * - to distinguish between call and answer, look at call->flags
300 */
301call_t * ipc_wait_for_call(answerbox_t *box, int flags)
302{
303 call_t *request;
304
[fbcfd458]305restart:
306 if (flags & IPC_WAIT_NONBLOCKING) {
307 if (waitq_sleep_timeout(&box->wq,0,1) == ESYNCH_WOULD_BLOCK)
308 return NULL;
309 } else
310 waitq_sleep(&box->wq);
311
[2ba7810]312 spinlock_lock(&box->lock);
[fbcfd458]313 if (!list_empty(&box->answers)) {
314 /* Handle asynchronous answers */
315 request = list_get_instance(box->answers.next, call_t, list);
316 list_remove(&request->list);
317 atomic_dec(&request->data.phone->active_calls);
318 } else if (!list_empty(&box->calls)) {
319 /* Handle requests */
320 request = list_get_instance(box->calls.next, call_t, list);
321 list_remove(&request->list);
322 /* Append request to dispatch queue */
323 list_append(&request->list, &box->dispatched_calls);
324 } else {
[ca687ad]325 /* This can happen regularly after ipc_cleanup, remove
326 * the warning in the future when the IPC is
327 * more debugged */
[fbcfd458]328 printf("WARNING: Spurious IPC wakeup.\n");
329 spinlock_unlock(&box->lock);
330 goto restart;
[6d9c49a]331 }
[2ba7810]332 spinlock_unlock(&box->lock);
[6d9c49a]333 return request;
334}
335
336/** Initilize ipc subsystem */
337void ipc_init(void)
338{
339 ipc_call_slab = slab_cache_create("ipc_call",
340 sizeof(call_t),
341 0,
342 NULL, NULL, 0);
343}
[4e49572]344
[ca687ad]345/** Answer all calls from list with EHANGUP msg */
346static void ipc_cleanup_call_list(link_t *lst)
347{
348 call_t *call;
349
350 while (!list_empty(lst)) {
351 call = list_get_instance(lst->next, call_t, list);
352 list_remove(&call->list);
353
354 IPC_SET_RETVAL(call->data, EHANGUP);
355 _ipc_answer_free_call(call);
356 }
357}
358
[4e49572]359/** Cleans up all IPC communication of the given task
360 *
361 *
362 */
363void ipc_cleanup(task_t *task)
364{
[9f22213]365 int i;
[ca687ad]366 call_t *call;
367 phone_t *phone;
368
[9f22213]369 /* Disconnect all our phones ('ipc_phone_hangup') */
370 for (i=0;i < IPC_MAX_PHONES; i++)
371 ipc_phone_hangup(&task->phones[i]);
372
[ca687ad]373 /* Disconnect all phones connected to our answerbox */
374restart_phones:
375 spinlock_lock(&task->answerbox.lock);
376 while (!list_empty(&task->answerbox.connected_phones)) {
377 phone = list_get_instance(task->answerbox.connected_phones.next,
378 phone_t,
379 list);
380 if (! spinlock_trylock(&phone->lock)) {
381 spinlock_unlock(&task->answerbox.lock);
382 goto restart_phones;
383 }
384
385 /* Disconnect phone */
386 phone->callee = NULL;
387 list_remove(&phone->list);
388
389 spinlock_unlock(&phone->lock);
390 }
[9f22213]391
392 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[ca687ad]393 spinlock_lock(&task->answerbox.lock);
394 ipc_cleanup_call_list(&task->answerbox.dispatched_calls);
395 ipc_cleanup_call_list(&task->answerbox.calls);
396 spinlock_unlock(&task->answerbox.lock);
[4e49572]397
[9f22213]398 /* Wait for all async answers to arrive */
[ca687ad]399 while (atomic_get(&task->active_calls)) {
400 call = ipc_wait_for_call(&task->answerbox, 0);
401 ASSERT(call->flags & IPC_CALL_ANSWERED);
402 ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
403
404 atomic_dec(&task->active_calls);
405 ipc_call_free(call);
406 }
[4e49572]407}
Note: See TracBrowser for help on using the repository browser.