source: mainline/uspace/lib/c/generic/ipc.c@ 52d2603

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 52d2603 was 52d2603, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Fix: added missing unlock of ipc futex when memory is scarce.

  • Property mode set to 100644
File size: 15.6 KB
Line 
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/** @addtogroup libc
30 * @{
31 * @}
32 */
33
34/** @addtogroup libcipc IPC
35 * @brief HelenOS uspace IPC
36 * @{
37 * @ingroup libc
38 */
39/** @file
40 */
41
42#include <ipc/ipc.h>
43#include <libc.h>
44#include <malloc.h>
45#include <errno.h>
46#include <adt/list.h>
47#include <futex.h>
48#include <fibril.h>
49#include <macros.h>
50
51/**
52 * Structures of this type are used for keeping track
53 * of sent asynchronous calls and queing unsent calls.
54 */
55typedef struct {
56 link_t list;
57
58 ipc_async_callback_t callback;
59 void *private;
60
61 union {
62 ipc_callid_t callid;
63 struct {
64 ipc_call_t data;
65 int phoneid;
66 } msg;
67 } u;
68
69 /** Fibril waiting for sending this call. */
70 fid_t fid;
71} async_call_t;
72
73LIST_INITIALIZE(dispatched_calls);
74
75/** List of asynchronous calls that were not accepted by kernel.
76 *
77 * Protected by async_futex, because if the call is not accepted
78 * by the kernel, the async framework is used automatically.
79 *
80 */
81LIST_INITIALIZE(queued_calls);
82
83static futex_t ipc_futex = FUTEX_INITIALIZER;
84
85/** Send asynchronous message via syscall.
86 *
87 * @param phoneid Phone handle for the call.
88 * @param data Call data with the request.
89 *
90 * @return Hash of the call or an error code.
91 *
92 */
93static ipc_callid_t ipc_call_async_internal(int phoneid, ipc_call_t *data)
94{
95 return __SYSCALL2(SYS_IPC_CALL_ASYNC_SLOW, phoneid, (sysarg_t) data);
96}
97
98/** Prolog for ipc_call_async_*() functions.
99 *
100 * @param private Argument for the answer/error callback.
101 * @param callback Answer/error callback.
102 *
103 * @return New, partially initialized async_call structure or NULL.
104 *
105 */
106static inline async_call_t *ipc_prepare_async(void *private,
107 ipc_async_callback_t callback)
108{
109 async_call_t *call =
110 (async_call_t *) malloc(sizeof(async_call_t));
111 if (!call) {
112 if (callback)
113 callback(private, ENOMEM, NULL);
114
115 return NULL;
116 }
117
118 call->callback = callback;
119 call->private = private;
120
121 return call;
122}
123
124/** Epilog for ipc_call_async_*() functions.
125 *
126 * @param callid Value returned by the SYS_IPC_CALL_ASYNC_* syscall.
127 * @param phoneid Phone handle through which the call was made.
128 * @param call Structure returned by ipc_prepare_async().
129 * @param can_preempt If true, the current fibril can be preempted
130 * in this call.
131 *
132 */
133static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
134 async_call_t *call, bool can_preempt)
135{
136 if (!call) {
137 /* Nothing to do regardless if failed or not */
138 futex_unlock(&ipc_futex);
139 return;
140 }
141
142 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
143 futex_unlock(&ipc_futex);
144
145 /* Call asynchronous handler with error code */
146 if (call->callback)
147 call->callback(call->private, ENOENT, NULL);
148
149 free(call);
150 return;
151 }
152
153 if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
154 futex_unlock(&ipc_futex);
155
156 call->u.msg.phoneid = phoneid;
157
158 futex_down(&async_futex);
159 list_append(&call->list, &queued_calls);
160
161 if (can_preempt) {
162 call->fid = fibril_get_id();
163 fibril_switch(FIBRIL_TO_MANAGER);
164 /* Async futex unlocked by previous call */
165 } else {
166 call->fid = 0;
167 futex_up(&async_futex);
168 }
169
170 return;
171 }
172
173 call->u.callid = callid;
174
175 /* Add call to the list of dispatched calls */
176 list_append(&call->list, &dispatched_calls);
177 futex_unlock(&ipc_futex);
178}
179
180/** Fast asynchronous call.
181 *
182 * This function can only handle four arguments of payload. It is, however,
183 * faster than the more generic ipc_call_async_slow().
184 *
185 * Note that this function is a void function.
186 *
187 * During normal operation, answering this call will trigger the callback.
188 * In case of fatal error, the callback handler is called with the proper
189 * error code. If the call cannot be temporarily made, it is queued.
190 *
191 * @param phoneid Phone handle for the call.
192 * @param imethod Requested interface and method.
193 * @param arg1 Service-defined payload argument.
194 * @param arg2 Service-defined payload argument.
195 * @param arg3 Service-defined payload argument.
196 * @param arg4 Service-defined payload argument.
197 * @param private Argument to be passed to the answer/error callback.
198 * @param callback Answer or error callback.
199 * @param can_preempt If true, the current fibril will be preempted in
200 * case the kernel temporarily refuses to accept more
201 * asynchronous calls.
202 *
203 */
204void ipc_call_async_fast(int phoneid, sysarg_t imethod, sysarg_t arg1,
205 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, void *private,
206 ipc_async_callback_t callback, bool can_preempt)
207{
208 async_call_t *call = NULL;
209
210 if (callback) {
211 call = ipc_prepare_async(private, callback);
212 if (!call)
213 return;
214 }
215
216 /*
217 * We need to make sure that we get callid
218 * before another thread accesses the queue again.
219 */
220
221 futex_lock(&ipc_futex);
222 ipc_callid_t callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid,
223 imethod, arg1, arg2, arg3, arg4);
224
225 if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
226 if (!call) {
227 call = ipc_prepare_async(private, callback);
228 if (!call) {
229 futex_unlock(&ipc_futex);
230 return;
231 }
232 }
233
234 IPC_SET_IMETHOD(call->u.msg.data, imethod);
235 IPC_SET_ARG1(call->u.msg.data, arg1);
236 IPC_SET_ARG2(call->u.msg.data, arg2);
237 IPC_SET_ARG3(call->u.msg.data, arg3);
238 IPC_SET_ARG4(call->u.msg.data, arg4);
239
240 /*
241 * To achieve deterministic behavior, we always zero out the
242 * arguments that are beyond the limits of the fast version.
243 */
244
245 IPC_SET_ARG5(call->u.msg.data, 0);
246 }
247
248 ipc_finish_async(callid, phoneid, call, can_preempt);
249}
250
251/** Asynchronous call transmitting the entire payload.
252 *
253 * Note that this function is a void function.
254 *
255 * During normal operation, answering this call will trigger the callback.
256 * In case of fatal error, the callback handler is called with the proper
257 * error code. If the call cannot be temporarily made, it is queued.
258 *
259 * @param phoneid Phone handle for the call.
260 * @param imethod Requested interface and method.
261 * @param arg1 Service-defined payload argument.
262 * @param arg2 Service-defined payload argument.
263 * @param arg3 Service-defined payload argument.
264 * @param arg4 Service-defined payload argument.
265 * @param arg5 Service-defined payload argument.
266 * @param private Argument to be passed to the answer/error callback.
267 * @param callback Answer or error callback.
268 * @param can_preempt If true, the current fibril will be preempted in
269 * case the kernel temporarily refuses to accept more
270 * asynchronous calls.
271 *
272 */
273void ipc_call_async_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
274 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
275 ipc_async_callback_t callback, bool can_preempt)
276{
277 async_call_t *call = ipc_prepare_async(private, callback);
278 if (!call)
279 return;
280
281 IPC_SET_IMETHOD(call->u.msg.data, imethod);
282 IPC_SET_ARG1(call->u.msg.data, arg1);
283 IPC_SET_ARG2(call->u.msg.data, arg2);
284 IPC_SET_ARG3(call->u.msg.data, arg3);
285 IPC_SET_ARG4(call->u.msg.data, arg4);
286 IPC_SET_ARG5(call->u.msg.data, arg5);
287
288 /*
289 * We need to make sure that we get callid
290 * before another threadaccesses the queue again.
291 */
292
293 futex_lock(&ipc_futex);
294 ipc_callid_t callid =
295 ipc_call_async_internal(phoneid, &call->u.msg.data);
296
297 ipc_finish_async(callid, phoneid, call, can_preempt);
298}
299
300/** Answer received call (fast version).
301 *
302 * The fast answer makes use of passing retval and first four arguments in
303 * registers. If you need to return more, use the ipc_answer_slow() instead.
304 *
305 * @param callid Hash of the call being answered.
306 * @param retval Return value.
307 * @param arg1 First return argument.
308 * @param arg2 Second return argument.
309 * @param arg3 Third return argument.
310 * @param arg4 Fourth return argument.
311 *
312 * @return Zero on success.
313 * @return Value from @ref errno.h on failure.
314 *
315 */
316sysarg_t ipc_answer_fast(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
317 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
318{
319 return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
320 arg4);
321}
322
323/** Answer received call (entire payload).
324 *
325 * @param callid Hash of the call being answered.
326 * @param retval Return value.
327 * @param arg1 First return argument.
328 * @param arg2 Second return argument.
329 * @param arg3 Third return argument.
330 * @param arg4 Fourth return argument.
331 * @param arg5 Fifth return argument.
332 *
333 * @return Zero on success.
334 * @return Value from @ref errno.h on failure.
335 *
336 */
337sysarg_t ipc_answer_slow(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
338 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
339{
340 ipc_call_t data;
341
342 IPC_SET_RETVAL(data, retval);
343 IPC_SET_ARG1(data, arg1);
344 IPC_SET_ARG2(data, arg2);
345 IPC_SET_ARG3(data, arg3);
346 IPC_SET_ARG4(data, arg4);
347 IPC_SET_ARG5(data, arg5);
348
349 return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
350}
351
352/** Try to dispatch queued calls from the async queue.
353 *
354 */
355static void dispatch_queued_calls(void)
356{
357 /** @todo
358 * Integrate intelligently ipc_futex so that it is locked during
359 * ipc_call_async_*() until it is added to dispatched_calls.
360 */
361
362 futex_down(&async_futex);
363
364 while (!list_empty(&queued_calls)) {
365 async_call_t *call =
366 list_get_instance(list_first(&queued_calls), async_call_t, list);
367 ipc_callid_t callid =
368 ipc_call_async_internal(call->u.msg.phoneid, &call->u.msg.data);
369
370 if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY)
371 break;
372
373 list_remove(&call->list);
374
375 futex_up(&async_futex);
376
377 if (call->fid)
378 fibril_add_ready(call->fid);
379
380 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
381 if (call->callback)
382 call->callback(call->private, ENOENT, NULL);
383
384 free(call);
385 } else {
386 call->u.callid = callid;
387
388 futex_lock(&ipc_futex);
389 list_append(&call->list, &dispatched_calls);
390 futex_unlock(&ipc_futex);
391 }
392
393 futex_down(&async_futex);
394 }
395
396 futex_up(&async_futex);
397}
398
399/** Handle received answer.
400 *
401 * Find the hash of the answer and call the answer callback.
402 *
403 * The answer has the same hash as the request OR'ed with
404 * the IPC_CALLID_ANSWERED bit.
405 *
406 * @todo Use hash table.
407 *
408 * @param callid Hash of the received answer.
409 * @param data Call data of the answer.
410 *
411 */
412static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
413{
414 callid &= ~IPC_CALLID_ANSWERED;
415
416 futex_lock(&ipc_futex);
417
418 link_t *item;
419 for (item = dispatched_calls.head.next; item != &dispatched_calls.head;
420 item = item->next) {
421 async_call_t *call =
422 list_get_instance(item, async_call_t, list);
423
424 if (call->u.callid == callid) {
425 list_remove(&call->list);
426
427 futex_unlock(&ipc_futex);
428
429 if (call->callback)
430 call->callback(call->private,
431 IPC_GET_RETVAL(*data), data);
432
433 free(call);
434 return;
435 }
436 }
437
438 futex_unlock(&ipc_futex);
439}
440
441/** Wait for first IPC call to come.
442 *
443 * @param call Incoming call storage.
444 * @param usec Timeout in microseconds
445 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
446 *
447 * @return Hash of the call. Note that certain bits have special
448 * meaning: IPC_CALLID_ANSWERED is set in an answer
449 * and IPC_CALLID_NOTIFICATION is used for notifications.
450 *
451 */
452ipc_callid_t ipc_wait_cycle(ipc_call_t *call, sysarg_t usec,
453 unsigned int flags)
454{
455 ipc_callid_t callid =
456 __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
457
458 /* Handle received answers */
459 if (callid & IPC_CALLID_ANSWERED) {
460 handle_answer(callid, call);
461 dispatch_queued_calls();
462 }
463
464 return callid;
465}
466
467/** Interrupt one thread of this task from waiting for IPC.
468 *
469 */
470void ipc_poke(void)
471{
472 __SYSCALL0(SYS_IPC_POKE);
473}
474
475/** Wait for first IPC call to come.
476 *
477 * Only requests are returned, answers are processed internally.
478 *
479 * @param call Incoming call storage.
480 * @param usec Timeout in microseconds
481 *
482 * @return Hash of the call.
483 *
484 */
485ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, sysarg_t usec)
486{
487 ipc_callid_t callid;
488
489 do {
490 callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
491 } while (callid & IPC_CALLID_ANSWERED);
492
493 return callid;
494}
495
496/** Check if there is an IPC call waiting to be picked up.
497 *
498 * Only requests are returned, answers are processed internally.
499 *
500 * @param call Incoming call storage.
501 *
502 * @return Hash of the call.
503 *
504 */
505ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
506{
507 ipc_callid_t callid;
508
509 do {
510 callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
511 SYNCH_FLAGS_NON_BLOCKING);
512 } while (callid & IPC_CALLID_ANSWERED);
513
514 return callid;
515}
516
517/** Hang up a phone.
518 *
519 * @param phoneid Handle of the phone to be hung up.
520 *
521 * @return Zero on success or a negative error code.
522 *
523 */
524int ipc_hangup(int phoneid)
525{
526 return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
527}
528
529/** Forward a received call to another destination.
530 *
531 * For non-system methods, the old method, arg1 and arg2 are rewritten
532 * by the new values. For system methods, the new method, arg1 and arg2
533 * are written to the old arg1, arg2 and arg3, respectivelly. Calls with
534 * immutable methods are forwarded verbatim.
535 *
536 * @param callid Hash of the call to forward.
537 * @param phoneid Phone handle to use for forwarding.
538 * @param imethod New interface and method for the forwarded call.
539 * @param arg1 New value of the first argument for the forwarded call.
540 * @param arg2 New value of the second argument for the forwarded call.
541 * @param mode Flags specifying mode of the forward operation.
542 *
543 * @return Zero on success or an error code.
544 *
545 */
546int ipc_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
547 sysarg_t arg1, sysarg_t arg2, unsigned int mode)
548{
549 return __SYSCALL6(SYS_IPC_FORWARD_FAST, callid, phoneid, imethod, arg1,
550 arg2, mode);
551}
552
553int ipc_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
554 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
555 unsigned int mode)
556{
557 ipc_call_t data;
558
559 IPC_SET_IMETHOD(data, imethod);
560 IPC_SET_ARG1(data, arg1);
561 IPC_SET_ARG2(data, arg2);
562 IPC_SET_ARG3(data, arg3);
563 IPC_SET_ARG4(data, arg4);
564 IPC_SET_ARG5(data, arg5);
565
566 return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data,
567 mode);
568}
569
570/** Connect to a task specified by id.
571 *
572 */
573int ipc_connect_kbox(task_id_t id)
574{
575#ifdef __32_BITS__
576 sysarg64_t arg = (sysarg64_t) id;
577 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
578#endif
579
580#ifdef __64_BITS__
581 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
582#endif
583}
584
585/** @}
586 */
Note: See TracBrowser for help on using the repository browser.