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
RevLine 
[b419162]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[b419162]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.
[b2951e2]27 */
28
29/** @addtogroup libc
30 * @{
31 * @}
32 */
33
[fadd381]34/** @addtogroup libcipc IPC
[b2951e2]35 * @brief HelenOS uspace IPC
36 * @{
37 * @ingroup libc
38 */
39/** @file
[6b10dab]40 */
[b419162]41
[7ee6aff]42#include <ipc/ipc.h>
[b419162]43#include <libc.h>
[936351c1]44#include <malloc.h>
45#include <errno.h>
[d9c8c81]46#include <adt/list.h>
[35509652]47#include <futex.h>
[bc1f1c2]48#include <fibril.h>
[633bcc6]49#include <macros.h>
[b419162]50
[36c9234]51/**
[10477601]52 * Structures of this type are used for keeping track
53 * of sent asynchronous calls and queing unsent calls.
[936351c1]54 */
55typedef struct {
56 link_t list;
[10477601]57
[936351c1]58 ipc_async_callback_t callback;
59 void *private;
[10477601]60
[936351c1]61 union {
62 ipc_callid_t callid;
63 struct {
[4c61e60]64 ipc_call_t data;
[936351c1]65 int phoneid;
66 } msg;
[8b243f2]67 } u;
[10477601]68
69 /** Fibril waiting for sending this call. */
70 fid_t fid;
[936351c1]71} async_call_t;
72
73LIST_INITIALIZE(dispatched_calls);
[fc42b28]74
[8b243f2]75/** List of asynchronous calls that were not accepted by kernel.
76 *
[10477601]77 * Protected by async_futex, because if the call is not accepted
78 * by the kernel, the async framework is used automatically.
79 *
[fc42b28]80 */
[8b243f2]81LIST_INITIALIZE(queued_calls);
[936351c1]82
[927a181e]83static futex_t ipc_futex = FUTEX_INITIALIZER;
[35509652]84
[10477601]85/** Send asynchronous message via syscall.
[8b243f2]86 *
[228e490]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.
[8b243f2]91 *
92 */
[10477601]93static ipc_callid_t ipc_call_async_internal(int phoneid, ipc_call_t *data)
[936351c1]94{
[3209923]95 return __SYSCALL2(SYS_IPC_CALL_ASYNC_SLOW, phoneid, (sysarg_t) data);
[936351c1]96}
97
[c170438]98/** Prologue for ipc_call_async_*() functions.
[10477601]99 *
100 * @param private Argument for the answer/error callback.
101 * @param callback Answer/error callback.
[8b243f2]102 *
[10477601]103 * @return New, partially initialized async_call structure or NULL.
[8b243f2]104 *
105 */
106static inline async_call_t *ipc_prepare_async(void *private,
107 ipc_async_callback_t callback)
[b419162]108{
[10477601]109 async_call_t *call =
110 (async_call_t *) malloc(sizeof(async_call_t));
[936351c1]111 if (!call) {
[a784a96]112 if (callback)
113 callback(private, ENOMEM, NULL);
[10477601]114
[c1d2c9d]115 return NULL;
[936351c1]116 }
[10477601]117
[fc42b28]118 call->callback = callback;
119 call->private = private;
[10477601]120
[c1d2c9d]121 return call;
122}
123
[c170438]124/** Epilogue for ipc_call_async_*() functions.
[10477601]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().
[8b243f2]129 */
130static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
[dcc150cb]131 async_call_t *call)
[c1d2c9d]132{
[10477601]133 if (!call) {
134 /* Nothing to do regardless if failed or not */
[cdbcf14]135 futex_unlock(&ipc_futex);
[d8b42fb2]136 return;
137 }
[10477601]138
[b78d0bd]139 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
[cdbcf14]140 futex_unlock(&ipc_futex);
[10477601]141
[06502f7d]142 /* Call asynchronous handler with error code */
[c1d2c9d]143 if (call->callback)
144 call->callback(call->private, ENOENT, NULL);
[10477601]145
[936351c1]146 free(call);
[06502f7d]147 return;
148 }
[10477601]149
[936351c1]150 call->u.callid = callid;
[10477601]151
[8b243f2]152 /* Add call to the list of dispatched calls */
[936351c1]153 list_append(&call->list, &dispatched_calls);
[cdbcf14]154 futex_unlock(&ipc_futex);
[c1d2c9d]155}
156
[10477601]157/** Fast asynchronous call.
[8b243f2]158 *
[3209923]159 * This function can only handle four arguments of payload. It is, however,
160 * faster than the more generic ipc_call_async_slow().
[8b243f2]161 *
162 * Note that this function is a void function.
[10477601]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.
[c1d2c9d]167 *
[228e490]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.
[c1d2c9d]176 */
[228e490]177void ipc_call_async_fast(int phoneid, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]178 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, void *private,
[dcc150cb]179 ipc_async_callback_t callback)
[c1d2c9d]180{
[d8b42fb2]181 async_call_t *call = NULL;
[228e490]182
[d8b42fb2]183 if (callback) {
184 call = ipc_prepare_async(private, callback);
185 if (!call)
186 return;
187 }
[228e490]188
[8b243f2]189 /*
[10477601]190 * We need to make sure that we get callid
191 * before another thread accesses the queue again.
[8b243f2]192 */
[10477601]193
[cdbcf14]194 futex_lock(&ipc_futex);
[228e490]195 ipc_callid_t callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid,
196 imethod, arg1, arg2, arg3, arg4);
197
[dcc150cb]198 ipc_finish_async(callid, phoneid, call);
[c1d2c9d]199}
200
[10477601]201/** Asynchronous call transmitting the entire payload.
[8b243f2]202 *
203 * Note that this function is a void function.
[10477601]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.
[8b243f2]208 *
[228e490]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.
[c1d2c9d]218 */
[228e490]219void ipc_call_async_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]220 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
[dcc150cb]221 ipc_async_callback_t callback)
[c1d2c9d]222{
[10477601]223 async_call_t *call = ipc_prepare_async(private, callback);
[c1d2c9d]224 if (!call)
225 return;
[10477601]226
[228e490]227 IPC_SET_IMETHOD(call->u.msg.data, imethod);
[c1d2c9d]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);
[3209923]231 IPC_SET_ARG4(call->u.msg.data, arg4);
232 IPC_SET_ARG5(call->u.msg.data, arg5);
[10477601]233
[8b243f2]234 /*
[10477601]235 * We need to make sure that we get callid
236 * before another threadaccesses the queue again.
[8b243f2]237 */
[10477601]238
[cdbcf14]239 futex_lock(&ipc_futex);
[10477601]240 ipc_callid_t callid =
241 ipc_call_async_internal(phoneid, &call->u.msg.data);
242
[dcc150cb]243 ipc_finish_async(callid, phoneid, call);
[b419162]244}
245
[10477601]246/** Answer received call (fast version).
[250717cc]247 *
[b74959bd]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.
[250717cc]250 *
[10477601]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.
[250717cc]260 *
261 */
[96b02eb9]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)
[b419162]264{
[b74959bd]265 return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
266 arg4);
[b419162]267}
[06502f7d]268
[10477601]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.
[250717cc]278 *
[10477601]279 * @return Zero on success.
280 * @return Value from @ref errno.h on failure.
[250717cc]281 *
282 */
[96b02eb9]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)
[250717cc]285{
[b74959bd]286 ipc_call_t data;
[10477601]287
[b74959bd]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);
[10477601]294
[b74959bd]295 return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
[250717cc]296}
297
[10477601]298/** Try to dispatch queued calls from the async queue.
299 *
300 */
301static void dispatch_queued_calls(void)
[936351c1]302{
[8b243f2]303 /** @todo
[10477601]304 * Integrate intelligently ipc_futex so that it is locked during
305 * ipc_call_async_*() until it is added to dispatched_calls.
[fc42b28]306 */
[10477601]307
[fc42b28]308 futex_down(&async_futex);
[10477601]309
[936351c1]310 while (!list_empty(&queued_calls)) {
[10477601]311 async_call_t *call =
[b72efe8]312 list_get_instance(list_first(&queued_calls), async_call_t, list);
[10477601]313 ipc_callid_t callid =
314 ipc_call_async_internal(call->u.msg.phoneid, &call->u.msg.data);
315
[936351c1]316 list_remove(&call->list);
[10477601]317
[fc42b28]318 futex_up(&async_futex);
[10477601]319
[dcc150cb]320 assert(call->fid);
321 fibril_add_ready(call->fid);
[fc42b28]322
[b78d0bd]323 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
[a784a96]324 if (call->callback)
325 call->callback(call->private, ENOENT, NULL);
[10477601]326
[936351c1]327 free(call);
328 } else {
329 call->u.callid = callid;
[10477601]330
[cdbcf14]331 futex_lock(&ipc_futex);
[936351c1]332 list_append(&call->list, &dispatched_calls);
[cdbcf14]333 futex_unlock(&ipc_futex);
[936351c1]334 }
[10477601]335
[fc42b28]336 futex_down(&async_futex);
[936351c1]337 }
[10477601]338
[fc42b28]339 futex_up(&async_futex);
[936351c1]340}
341
[10477601]342/** Handle received answer.
[936351c1]343 *
[8b243f2]344 * Find the hash of the answer and call the answer callback.
[936351c1]345 *
[10477601]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.
[8b243f2]353 *
[936351c1]354 */
[4c61e60]355static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
[936351c1]356{
357 callid &= ~IPC_CALLID_ANSWERED;
358
[cdbcf14]359 futex_lock(&ipc_futex);
[10477601]360
361 link_t *item;
[b72efe8]362 for (item = dispatched_calls.head.next; item != &dispatched_calls.head;
[8b243f2]363 item = item->next) {
[10477601]364 async_call_t *call =
365 list_get_instance(item, async_call_t, list);
366
[936351c1]367 if (call->u.callid == callid) {
368 list_remove(&call->list);
[10477601]369
[cdbcf14]370 futex_unlock(&ipc_futex);
[10477601]371
[a784a96]372 if (call->callback)
[10477601]373 call->callback(call->private,
[8b243f2]374 IPC_GET_RETVAL(*data), data);
[10477601]375
[a784a96]376 free(call);
[936351c1]377 return;
378 }
379 }
[10477601]380
[cdbcf14]381 futex_unlock(&ipc_futex);
[936351c1]382}
383
[10477601]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).
[8b243f2]389 *
[10477601]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.
[b419162]393 *
394 */
[10477601]395ipc_callid_t ipc_wait_cycle(ipc_call_t *call, sysarg_t usec,
396 unsigned int flags)
[b419162]397{
[10477601]398 ipc_callid_t callid =
399 __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
400
[80649a91]401 /* Handle received answers */
[fc42b28]402 if (callid & IPC_CALLID_ANSWERED) {
[80649a91]403 handle_answer(callid, call);
[10477601]404 dispatch_queued_calls();
[fc42b28]405 }
[10477601]406
[04a73cdf]407 return callid;
408}
409
[10477601]410/** Interrupt one thread of this task from waiting for IPC.
[04a73cdf]411 *
[10477601]412 */
413void ipc_poke(void)
414{
415 __SYSCALL0(SYS_IPC_POKE);
416}
417
418/** Wait for first IPC call to come.
[8b243f2]419 *
[10477601]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.
[096ba7a]426 *
[04a73cdf]427 */
[10477601]428ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, sysarg_t usec)
[04a73cdf]429{
430 ipc_callid_t callid;
[10477601]431
[04a73cdf]432 do {
[2d22049]433 callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
[04a73cdf]434 } while (callid & IPC_CALLID_ANSWERED);
[10477601]435
[04a73cdf]436 return callid;
437}
438
439/** Check if there is an IPC call waiting to be picked up.
440 *
[10477601]441 * Only requests are returned, answers are processed internally.
442 *
443 * @param call Incoming call storage.
444 *
445 * @return Hash of the call.
446 *
[04a73cdf]447 */
448ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
449{
450 ipc_callid_t callid;
[10477601]451
[04a73cdf]452 do {
[8b243f2]453 callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
454 SYNCH_FLAGS_NON_BLOCKING);
[06502f7d]455 } while (callid & IPC_CALLID_ANSWERED);
[10477601]456
[b419162]457 return callid;
458}
[5106e98]459
[8b243f2]460/** Hang up a phone.
461 *
[10477601]462 * @param phoneid Handle of the phone to be hung up.
463 *
464 * @return Zero on success or a negative error code.
[8b243f2]465 *
466 */
[7048773]467int ipc_hangup(int phoneid)
468{
469 return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
470}
[6180b57]471
[8b243f2]472/** Forward a received call to another destination.
[10477601]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.
[8b243f2]478 *
[228e490]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.
[8b243f2]485 *
[228e490]486 * @return Zero on success or an error code.
[8b243f2]487 *
488 */
[10477601]489int ipc_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
490 sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[043dcc27]491{
[228e490]492 return __SYSCALL6(SYS_IPC_FORWARD_FAST, callid, phoneid, imethod, arg1,
[90c35436]493 arg2, mode);
[043dcc27]494}
495
[10477601]496int ipc_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
[96b02eb9]497 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
[10477601]498 unsigned int mode)
[48daf64]499{
500 ipc_call_t data;
[228e490]501
502 IPC_SET_IMETHOD(data, imethod);
[48daf64]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);
[228e490]508
[10477601]509 return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data,
510 mode);
[48daf64]511}
512
[9a1b20c]513/** Connect to a task specified by id.
[6b10dab]514 *
[9a1b20c]515 */
516int ipc_connect_kbox(task_id_t id)
517{
[6b10dab]518#ifdef __32_BITS__
519 sysarg64_t arg = (sysarg64_t) id;
[9a1b20c]520 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
[6b10dab]521#endif
522
523#ifdef __64_BITS__
524 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
525#endif
[9a1b20c]526}
[6b10dab]527
[fadd381]528/** @}
[b2951e2]529 */
Note: See TracBrowser for help on using the repository browser.