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

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

Remove support for IPC_CALLRET_TEMPORARY

  • Property mode set to 100644
File size: 14.1 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/** Prologue 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/** Epilogue 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 */
130static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
131 async_call_t *call)
132{
133 if (!call) {
134 /* Nothing to do regardless if failed or not */
135 futex_unlock(&ipc_futex);
136 return;
137 }
138
139 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
140 futex_unlock(&ipc_futex);
141
142 /* Call asynchronous handler with error code */
143 if (call->callback)
144 call->callback(call->private, ENOENT, NULL);
145
146 free(call);
147 return;
148 }
149
150 call->u.callid = callid;
151
152 /* Add call to the list of dispatched calls */
153 list_append(&call->list, &dispatched_calls);
154 futex_unlock(&ipc_futex);
155}
156
157/** Fast asynchronous call.
158 *
159 * This function can only handle four arguments of payload. It is, however,
160 * faster than the more generic ipc_call_async_slow().
161 *
162 * Note that this function is a void function.
163 *
164 * During normal operation, answering this call will trigger the callback.
165 * In case of fatal error, the callback handler is called with the proper
166 * error code. If the call cannot be temporarily made, it is queued.
167 *
168 * @param phoneid Phone handle for the call.
169 * @param imethod Requested interface and method.
170 * @param arg1 Service-defined payload argument.
171 * @param arg2 Service-defined payload argument.
172 * @param arg3 Service-defined payload argument.
173 * @param arg4 Service-defined payload argument.
174 * @param private Argument to be passed to the answer/error callback.
175 * @param callback Answer or error callback.
176 */
177void ipc_call_async_fast(int phoneid, sysarg_t imethod, sysarg_t arg1,
178 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, void *private,
179 ipc_async_callback_t callback)
180{
181 async_call_t *call = NULL;
182
183 if (callback) {
184 call = ipc_prepare_async(private, callback);
185 if (!call)
186 return;
187 }
188
189 /*
190 * We need to make sure that we get callid
191 * before another thread accesses the queue again.
192 */
193
194 futex_lock(&ipc_futex);
195 ipc_callid_t callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid,
196 imethod, arg1, arg2, arg3, arg4);
197
198 ipc_finish_async(callid, phoneid, call);
199}
200
201/** Asynchronous call transmitting the entire payload.
202 *
203 * Note that this function is a void function.
204 *
205 * During normal operation, answering this call will trigger the callback.
206 * In case of fatal error, the callback handler is called with the proper
207 * error code. If the call cannot be temporarily made, it is queued.
208 *
209 * @param phoneid Phone handle for the call.
210 * @param imethod Requested interface and method.
211 * @param arg1 Service-defined payload argument.
212 * @param arg2 Service-defined payload argument.
213 * @param arg3 Service-defined payload argument.
214 * @param arg4 Service-defined payload argument.
215 * @param arg5 Service-defined payload argument.
216 * @param private Argument to be passed to the answer/error callback.
217 * @param callback Answer or error callback.
218 */
219void ipc_call_async_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
220 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
221 ipc_async_callback_t callback)
222{
223 async_call_t *call = ipc_prepare_async(private, callback);
224 if (!call)
225 return;
226
227 IPC_SET_IMETHOD(call->u.msg.data, imethod);
228 IPC_SET_ARG1(call->u.msg.data, arg1);
229 IPC_SET_ARG2(call->u.msg.data, arg2);
230 IPC_SET_ARG3(call->u.msg.data, arg3);
231 IPC_SET_ARG4(call->u.msg.data, arg4);
232 IPC_SET_ARG5(call->u.msg.data, arg5);
233
234 /*
235 * We need to make sure that we get callid
236 * before another threadaccesses the queue again.
237 */
238
239 futex_lock(&ipc_futex);
240 ipc_callid_t callid =
241 ipc_call_async_internal(phoneid, &call->u.msg.data);
242
243 ipc_finish_async(callid, phoneid, call);
244}
245
246/** Answer received call (fast version).
247 *
248 * The fast answer makes use of passing retval and first four arguments in
249 * registers. If you need to return more, use the ipc_answer_slow() instead.
250 *
251 * @param callid Hash of the call being answered.
252 * @param retval Return value.
253 * @param arg1 First return argument.
254 * @param arg2 Second return argument.
255 * @param arg3 Third return argument.
256 * @param arg4 Fourth return argument.
257 *
258 * @return Zero on success.
259 * @return Value from @ref errno.h on failure.
260 *
261 */
262sysarg_t ipc_answer_fast(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
263 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
264{
265 return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
266 arg4);
267}
268
269/** Answer received call (entire payload).
270 *
271 * @param callid Hash of the call being answered.
272 * @param retval Return value.
273 * @param arg1 First return argument.
274 * @param arg2 Second return argument.
275 * @param arg3 Third return argument.
276 * @param arg4 Fourth return argument.
277 * @param arg5 Fifth return argument.
278 *
279 * @return Zero on success.
280 * @return Value from @ref errno.h on failure.
281 *
282 */
283sysarg_t ipc_answer_slow(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
284 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
285{
286 ipc_call_t data;
287
288 IPC_SET_RETVAL(data, retval);
289 IPC_SET_ARG1(data, arg1);
290 IPC_SET_ARG2(data, arg2);
291 IPC_SET_ARG3(data, arg3);
292 IPC_SET_ARG4(data, arg4);
293 IPC_SET_ARG5(data, arg5);
294
295 return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
296}
297
298/** Try to dispatch queued calls from the async queue.
299 *
300 */
301static void dispatch_queued_calls(void)
302{
303 /** @todo
304 * Integrate intelligently ipc_futex so that it is locked during
305 * ipc_call_async_*() until it is added to dispatched_calls.
306 */
307
308 futex_down(&async_futex);
309
310 while (!list_empty(&queued_calls)) {
311 async_call_t *call =
312 list_get_instance(list_first(&queued_calls), async_call_t, list);
313 ipc_callid_t callid =
314 ipc_call_async_internal(call->u.msg.phoneid, &call->u.msg.data);
315
316 list_remove(&call->list);
317
318 futex_up(&async_futex);
319
320 assert(call->fid);
321 fibril_add_ready(call->fid);
322
323 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
324 if (call->callback)
325 call->callback(call->private, ENOENT, NULL);
326
327 free(call);
328 } else {
329 call->u.callid = callid;
330
331 futex_lock(&ipc_futex);
332 list_append(&call->list, &dispatched_calls);
333 futex_unlock(&ipc_futex);
334 }
335
336 futex_down(&async_futex);
337 }
338
339 futex_up(&async_futex);
340}
341
342/** Handle received answer.
343 *
344 * Find the hash of the answer and call the answer callback.
345 *
346 * The answer has the same hash as the request OR'ed with
347 * the IPC_CALLID_ANSWERED bit.
348 *
349 * @todo Use hash table.
350 *
351 * @param callid Hash of the received answer.
352 * @param data Call data of the answer.
353 *
354 */
355static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
356{
357 callid &= ~IPC_CALLID_ANSWERED;
358
359 futex_lock(&ipc_futex);
360
361 link_t *item;
362 for (item = dispatched_calls.head.next; item != &dispatched_calls.head;
363 item = item->next) {
364 async_call_t *call =
365 list_get_instance(item, async_call_t, list);
366
367 if (call->u.callid == callid) {
368 list_remove(&call->list);
369
370 futex_unlock(&ipc_futex);
371
372 if (call->callback)
373 call->callback(call->private,
374 IPC_GET_RETVAL(*data), data);
375
376 free(call);
377 return;
378 }
379 }
380
381 futex_unlock(&ipc_futex);
382}
383
384/** Wait for first IPC call to come.
385 *
386 * @param call Incoming call storage.
387 * @param usec Timeout in microseconds
388 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
389 *
390 * @return Hash of the call. Note that certain bits have special
391 * meaning: IPC_CALLID_ANSWERED is set in an answer
392 * and IPC_CALLID_NOTIFICATION is used for notifications.
393 *
394 */
395ipc_callid_t ipc_wait_cycle(ipc_call_t *call, sysarg_t usec,
396 unsigned int flags)
397{
398 ipc_callid_t callid =
399 __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
400
401 /* Handle received answers */
402 if (callid & IPC_CALLID_ANSWERED) {
403 handle_answer(callid, call);
404 dispatch_queued_calls();
405 }
406
407 return callid;
408}
409
410/** Interrupt one thread of this task from waiting for IPC.
411 *
412 */
413void ipc_poke(void)
414{
415 __SYSCALL0(SYS_IPC_POKE);
416}
417
418/** Wait for first IPC call to come.
419 *
420 * Only requests are returned, answers are processed internally.
421 *
422 * @param call Incoming call storage.
423 * @param usec Timeout in microseconds
424 *
425 * @return Hash of the call.
426 *
427 */
428ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, sysarg_t usec)
429{
430 ipc_callid_t callid;
431
432 do {
433 callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
434 } while (callid & IPC_CALLID_ANSWERED);
435
436 return callid;
437}
438
439/** Check if there is an IPC call waiting to be picked up.
440 *
441 * Only requests are returned, answers are processed internally.
442 *
443 * @param call Incoming call storage.
444 *
445 * @return Hash of the call.
446 *
447 */
448ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
449{
450 ipc_callid_t callid;
451
452 do {
453 callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
454 SYNCH_FLAGS_NON_BLOCKING);
455 } while (callid & IPC_CALLID_ANSWERED);
456
457 return callid;
458}
459
460/** Hang up a phone.
461 *
462 * @param phoneid Handle of the phone to be hung up.
463 *
464 * @return Zero on success or a negative error code.
465 *
466 */
467int ipc_hangup(int phoneid)
468{
469 return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
470}
471
472/** Forward a received call to another destination.
473 *
474 * For non-system methods, the old method, arg1 and arg2 are rewritten
475 * by the new values. For system methods, the new method, arg1 and arg2
476 * are written to the old arg1, arg2 and arg3, respectivelly. Calls with
477 * immutable methods are forwarded verbatim.
478 *
479 * @param callid Hash of the call to forward.
480 * @param phoneid Phone handle to use for forwarding.
481 * @param imethod New interface and method for the forwarded call.
482 * @param arg1 New value of the first argument for the forwarded call.
483 * @param arg2 New value of the second argument for the forwarded call.
484 * @param mode Flags specifying mode of the forward operation.
485 *
486 * @return Zero on success or an error code.
487 *
488 */
489int ipc_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
490 sysarg_t arg1, sysarg_t arg2, unsigned int mode)
491{
492 return __SYSCALL6(SYS_IPC_FORWARD_FAST, callid, phoneid, imethod, arg1,
493 arg2, mode);
494}
495
496int ipc_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
497 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
498 unsigned int mode)
499{
500 ipc_call_t data;
501
502 IPC_SET_IMETHOD(data, imethod);
503 IPC_SET_ARG1(data, arg1);
504 IPC_SET_ARG2(data, arg2);
505 IPC_SET_ARG3(data, arg3);
506 IPC_SET_ARG4(data, arg4);
507 IPC_SET_ARG5(data, arg5);
508
509 return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data,
510 mode);
511}
512
513/** Connect to a task specified by id.
514 *
515 */
516int ipc_connect_kbox(task_id_t id)
517{
518#ifdef __32_BITS__
519 sysarg64_t arg = (sysarg64_t) id;
520 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
521#endif
522
523#ifdef __64_BITS__
524 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
525#endif
526}
527
528/** @}
529 */
Note: See TracBrowser for help on using the repository browser.