source: mainline/uspace/lib/c/generic/ipc.c@ d7978525

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

Implement ipc_*_finalize() functionality directly in async framework.

  • Remove ipc_*_finalize() as these interfaces do not make much sense only on their own.
  • 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 atomic_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_up(&ipc_futex);
139 return;
140 }
141
142 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
143 futex_up(&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_up(&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_up(&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_down(&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 return;
230 }
231
232 IPC_SET_IMETHOD(call->u.msg.data, imethod);
233 IPC_SET_ARG1(call->u.msg.data, arg1);
234 IPC_SET_ARG2(call->u.msg.data, arg2);
235 IPC_SET_ARG3(call->u.msg.data, arg3);
236 IPC_SET_ARG4(call->u.msg.data, arg4);
237
238 /*
239 * To achieve deterministic behavior, we always zero out the
240 * arguments that are beyond the limits of the fast version.
241 */
242
243 IPC_SET_ARG5(call->u.msg.data, 0);
244 }
245
246 ipc_finish_async(callid, phoneid, call, can_preempt);
247}
248
249/** Asynchronous call transmitting the entire payload.
250 *
251 * Note that this function is a void function.
252 *
253 * During normal operation, answering this call will trigger the callback.
254 * In case of fatal error, the callback handler is called with the proper
255 * error code. If the call cannot be temporarily made, it is queued.
256 *
257 * @param phoneid Phone handle for the call.
258 * @param imethod Requested interface and method.
259 * @param arg1 Service-defined payload argument.
260 * @param arg2 Service-defined payload argument.
261 * @param arg3 Service-defined payload argument.
262 * @param arg4 Service-defined payload argument.
263 * @param arg5 Service-defined payload argument.
264 * @param private Argument to be passed to the answer/error callback.
265 * @param callback Answer or error callback.
266 * @param can_preempt If true, the current fibril will be preempted in
267 * case the kernel temporarily refuses to accept more
268 * asynchronous calls.
269 *
270 */
271void ipc_call_async_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
272 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
273 ipc_async_callback_t callback, bool can_preempt)
274{
275 async_call_t *call = ipc_prepare_async(private, callback);
276 if (!call)
277 return;
278
279 IPC_SET_IMETHOD(call->u.msg.data, imethod);
280 IPC_SET_ARG1(call->u.msg.data, arg1);
281 IPC_SET_ARG2(call->u.msg.data, arg2);
282 IPC_SET_ARG3(call->u.msg.data, arg3);
283 IPC_SET_ARG4(call->u.msg.data, arg4);
284 IPC_SET_ARG5(call->u.msg.data, arg5);
285
286 /*
287 * We need to make sure that we get callid
288 * before another threadaccesses the queue again.
289 */
290
291 futex_down(&ipc_futex);
292 ipc_callid_t callid =
293 ipc_call_async_internal(phoneid, &call->u.msg.data);
294
295 ipc_finish_async(callid, phoneid, call, can_preempt);
296}
297
298/** Answer received call (fast version).
299 *
300 * The fast answer makes use of passing retval and first four arguments in
301 * registers. If you need to return more, use the ipc_answer_slow() instead.
302 *
303 * @param callid Hash of the call being answered.
304 * @param retval Return value.
305 * @param arg1 First return argument.
306 * @param arg2 Second return argument.
307 * @param arg3 Third return argument.
308 * @param arg4 Fourth return argument.
309 *
310 * @return Zero on success.
311 * @return Value from @ref errno.h on failure.
312 *
313 */
314sysarg_t ipc_answer_fast(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
315 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
316{
317 return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
318 arg4);
319}
320
321/** Answer received call (entire payload).
322 *
323 * @param callid Hash of the call being answered.
324 * @param retval Return value.
325 * @param arg1 First return argument.
326 * @param arg2 Second return argument.
327 * @param arg3 Third return argument.
328 * @param arg4 Fourth return argument.
329 * @param arg5 Fifth return argument.
330 *
331 * @return Zero on success.
332 * @return Value from @ref errno.h on failure.
333 *
334 */
335sysarg_t ipc_answer_slow(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
336 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
337{
338 ipc_call_t data;
339
340 IPC_SET_RETVAL(data, retval);
341 IPC_SET_ARG1(data, arg1);
342 IPC_SET_ARG2(data, arg2);
343 IPC_SET_ARG3(data, arg3);
344 IPC_SET_ARG4(data, arg4);
345 IPC_SET_ARG5(data, arg5);
346
347 return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
348}
349
350/** Try to dispatch queued calls from the async queue.
351 *
352 */
353static void dispatch_queued_calls(void)
354{
355 /** @todo
356 * Integrate intelligently ipc_futex so that it is locked during
357 * ipc_call_async_*() until it is added to dispatched_calls.
358 */
359
360 futex_down(&async_futex);
361
362 while (!list_empty(&queued_calls)) {
363 async_call_t *call =
364 list_get_instance(list_first(&queued_calls), async_call_t, list);
365 ipc_callid_t callid =
366 ipc_call_async_internal(call->u.msg.phoneid, &call->u.msg.data);
367
368 if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY)
369 break;
370
371 list_remove(&call->list);
372
373 futex_up(&async_futex);
374
375 if (call->fid)
376 fibril_add_ready(call->fid);
377
378 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
379 if (call->callback)
380 call->callback(call->private, ENOENT, NULL);
381
382 free(call);
383 } else {
384 call->u.callid = callid;
385
386 futex_down(&ipc_futex);
387 list_append(&call->list, &dispatched_calls);
388 futex_up(&ipc_futex);
389 }
390
391 futex_down(&async_futex);
392 }
393
394 futex_up(&async_futex);
395}
396
397/** Handle received answer.
398 *
399 * Find the hash of the answer and call the answer callback.
400 *
401 * The answer has the same hash as the request OR'ed with
402 * the IPC_CALLID_ANSWERED bit.
403 *
404 * @todo Use hash table.
405 *
406 * @param callid Hash of the received answer.
407 * @param data Call data of the answer.
408 *
409 */
410static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
411{
412 callid &= ~IPC_CALLID_ANSWERED;
413
414 futex_down(&ipc_futex);
415
416 link_t *item;
417 for (item = dispatched_calls.head.next; item != &dispatched_calls.head;
418 item = item->next) {
419 async_call_t *call =
420 list_get_instance(item, async_call_t, list);
421
422 if (call->u.callid == callid) {
423 list_remove(&call->list);
424
425 futex_up(&ipc_futex);
426
427 if (call->callback)
428 call->callback(call->private,
429 IPC_GET_RETVAL(*data), data);
430
431 free(call);
432 return;
433 }
434 }
435
436 futex_up(&ipc_futex);
437}
438
439/** Wait for first IPC call to come.
440 *
441 * @param call Incoming call storage.
442 * @param usec Timeout in microseconds
443 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
444 *
445 * @return Hash of the call. Note that certain bits have special
446 * meaning: IPC_CALLID_ANSWERED is set in an answer
447 * and IPC_CALLID_NOTIFICATION is used for notifications.
448 *
449 */
450ipc_callid_t ipc_wait_cycle(ipc_call_t *call, sysarg_t usec,
451 unsigned int flags)
452{
453 ipc_callid_t callid =
454 __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
455
456 /* Handle received answers */
457 if (callid & IPC_CALLID_ANSWERED) {
458 handle_answer(callid, call);
459 dispatch_queued_calls();
460 }
461
462 return callid;
463}
464
465/** Interrupt one thread of this task from waiting for IPC.
466 *
467 */
468void ipc_poke(void)
469{
470 __SYSCALL0(SYS_IPC_POKE);
471}
472
473/** Wait for first IPC call to come.
474 *
475 * Only requests are returned, answers are processed internally.
476 *
477 * @param call Incoming call storage.
478 * @param usec Timeout in microseconds
479 *
480 * @return Hash of the call.
481 *
482 */
483ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, sysarg_t usec)
484{
485 ipc_callid_t callid;
486
487 do {
488 callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
489 } while (callid & IPC_CALLID_ANSWERED);
490
491 return callid;
492}
493
494/** Check if there is an IPC call waiting to be picked up.
495 *
496 * Only requests are returned, answers are processed internally.
497 *
498 * @param call Incoming call storage.
499 *
500 * @return Hash of the call.
501 *
502 */
503ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
504{
505 ipc_callid_t callid;
506
507 do {
508 callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
509 SYNCH_FLAGS_NON_BLOCKING);
510 } while (callid & IPC_CALLID_ANSWERED);
511
512 return callid;
513}
514
515/** Hang up a phone.
516 *
517 * @param phoneid Handle of the phone to be hung up.
518 *
519 * @return Zero on success or a negative error code.
520 *
521 */
522int ipc_hangup(int phoneid)
523{
524 return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
525}
526
527/** Forward a received call to another destination.
528 *
529 * For non-system methods, the old method, arg1 and arg2 are rewritten
530 * by the new values. For system methods, the new method, arg1 and arg2
531 * are written to the old arg1, arg2 and arg3, respectivelly. Calls with
532 * immutable methods are forwarded verbatim.
533 *
534 * @param callid Hash of the call to forward.
535 * @param phoneid Phone handle to use for forwarding.
536 * @param imethod New interface and method for the forwarded call.
537 * @param arg1 New value of the first argument for the forwarded call.
538 * @param arg2 New value of the second argument for the forwarded call.
539 * @param mode Flags specifying mode of the forward operation.
540 *
541 * @return Zero on success or an error code.
542 *
543 */
544int ipc_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
545 sysarg_t arg1, sysarg_t arg2, unsigned int mode)
546{
547 return __SYSCALL6(SYS_IPC_FORWARD_FAST, callid, phoneid, imethod, arg1,
548 arg2, mode);
549}
550
551int ipc_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
552 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
553 unsigned int mode)
554{
555 ipc_call_t data;
556
557 IPC_SET_IMETHOD(data, imethod);
558 IPC_SET_ARG1(data, arg1);
559 IPC_SET_ARG2(data, arg2);
560 IPC_SET_ARG3(data, arg3);
561 IPC_SET_ARG4(data, arg4);
562 IPC_SET_ARG5(data, arg5);
563
564 return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data,
565 mode);
566}
567
568/** Connect to a task specified by id.
569 *
570 */
571int ipc_connect_kbox(task_id_t id)
572{
573#ifdef __32_BITS__
574 sysarg64_t arg = (sysarg64_t) id;
575 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
576#endif
577
578#ifdef __64_BITS__
579 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
580#endif
581}
582
583/** @}
584 */
Note: See TracBrowser for help on using the repository browser.