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

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

Simplify ipc_call_async_fast()

  • Property mode set to 100644
File size: 11.3 KB
RevLine 
[b419162]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[7c0e1f5]3 * Copyright (c) 2017 Jakub Jermar
[b419162]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[b2951e2]28 */
29
30/** @addtogroup libc
31 * @{
32 * @}
33 */
34
[fadd381]35/** @addtogroup libcipc IPC
[b2951e2]36 * @brief HelenOS uspace IPC
37 * @{
38 * @ingroup libc
39 */
40/** @file
[6b10dab]41 */
[b419162]42
[7ee6aff]43#include <ipc/ipc.h>
[b419162]44#include <libc.h>
[936351c1]45#include <malloc.h>
46#include <errno.h>
[d9c8c81]47#include <adt/list.h>
[35509652]48#include <futex.h>
[bc1f1c2]49#include <fibril.h>
[633bcc6]50#include <macros.h>
[b419162]51
[36c9234]52/**
[35f2bb1b]53 * Structures of this type are used for keeping track of sent asynchronous calls.
[936351c1]54 */
[7c0e1f5]55typedef struct async_call {
[936351c1]56 ipc_async_callback_t callback;
57 void *private;
[10477601]58
[7c0e1f5]59 struct {
60 ipc_call_t data;
61 int phoneid;
62 } msg;
[936351c1]63} async_call_t;
64
[c170438]65/** Prologue for ipc_call_async_*() functions.
[10477601]66 *
67 * @param private Argument for the answer/error callback.
68 * @param callback Answer/error callback.
[8b243f2]69 *
[10477601]70 * @return New, partially initialized async_call structure or NULL.
[8b243f2]71 *
72 */
73static inline async_call_t *ipc_prepare_async(void *private,
74 ipc_async_callback_t callback)
[b419162]75{
[10477601]76 async_call_t *call =
77 (async_call_t *) malloc(sizeof(async_call_t));
[936351c1]78 if (!call) {
[a784a96]79 if (callback)
80 callback(private, ENOMEM, NULL);
[10477601]81
[c1d2c9d]82 return NULL;
[936351c1]83 }
[10477601]84
[fc42b28]85 call->callback = callback;
86 call->private = private;
[10477601]87
[c1d2c9d]88 return call;
89}
90
[c170438]91/** Epilogue for ipc_call_async_*() functions.
[10477601]92 *
93 * @param callid Value returned by the SYS_IPC_CALL_ASYNC_* syscall.
94 * @param phoneid Phone handle through which the call was made.
95 * @param call Structure returned by ipc_prepare_async().
[8b243f2]96 */
97static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
[dcc150cb]98 async_call_t *call)
[c1d2c9d]99{
[10477601]100 if (!call) {
101 /* Nothing to do regardless if failed or not */
[d8b42fb2]102 return;
103 }
[10477601]104
[b78d0bd]105 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
[06502f7d]106 /* Call asynchronous handler with error code */
[c1d2c9d]107 if (call->callback)
108 call->callback(call->private, ENOENT, NULL);
[10477601]109
[936351c1]110 free(call);
[06502f7d]111 return;
112 }
[c1d2c9d]113}
114
[10477601]115/** Fast asynchronous call.
[8b243f2]116 *
[7c0e1f5]117 * This function can only handle three arguments of payload. It is, however,
[3209923]118 * faster than the more generic ipc_call_async_slow().
[8b243f2]119 *
120 * Note that this function is a void function.
[10477601]121 *
122 * During normal operation, answering this call will trigger the callback.
123 * In case of fatal error, the callback handler is called with the proper
124 * error code. If the call cannot be temporarily made, it is queued.
[c1d2c9d]125 *
[228e490]126 * @param phoneid Phone handle for the call.
127 * @param imethod Requested interface and method.
128 * @param arg1 Service-defined payload argument.
129 * @param arg2 Service-defined payload argument.
130 * @param arg3 Service-defined payload argument.
131 * @param private Argument to be passed to the answer/error callback.
132 * @param callback Answer or error callback.
[c1d2c9d]133 */
[228e490]134void ipc_call_async_fast(int phoneid, sysarg_t imethod, sysarg_t arg1,
[7c0e1f5]135 sysarg_t arg2, sysarg_t arg3, void *private, ipc_async_callback_t callback)
[c1d2c9d]136{
[b1f36e3]137 async_call_t *call = ipc_prepare_async(private, callback);
138 if (!call)
139 return;
[228e490]140
141 ipc_callid_t callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid,
[7c0e1f5]142 imethod, arg1, arg2, arg3, (sysarg_t) call);
[228e490]143
[dcc150cb]144 ipc_finish_async(callid, phoneid, call);
[c1d2c9d]145}
146
[10477601]147/** Asynchronous call transmitting the entire payload.
[8b243f2]148 *
149 * Note that this function is a void function.
[10477601]150 *
151 * During normal operation, answering this call will trigger the callback.
152 * In case of fatal error, the callback handler is called with the proper
153 * error code. If the call cannot be temporarily made, it is queued.
[8b243f2]154 *
[228e490]155 * @param phoneid Phone handle for the call.
156 * @param imethod Requested interface and method.
157 * @param arg1 Service-defined payload argument.
158 * @param arg2 Service-defined payload argument.
159 * @param arg3 Service-defined payload argument.
160 * @param arg4 Service-defined payload argument.
161 * @param arg5 Service-defined payload argument.
162 * @param private Argument to be passed to the answer/error callback.
163 * @param callback Answer or error callback.
[c1d2c9d]164 */
[228e490]165void ipc_call_async_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]166 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
[dcc150cb]167 ipc_async_callback_t callback)
[c1d2c9d]168{
[10477601]169 async_call_t *call = ipc_prepare_async(private, callback);
[c1d2c9d]170 if (!call)
171 return;
[10477601]172
[7c0e1f5]173 IPC_SET_IMETHOD(call->msg.data, imethod);
174 IPC_SET_ARG1(call->msg.data, arg1);
175 IPC_SET_ARG2(call->msg.data, arg2);
176 IPC_SET_ARG3(call->msg.data, arg3);
177 IPC_SET_ARG4(call->msg.data, arg4);
178 IPC_SET_ARG5(call->msg.data, arg5);
[10477601]179
[7c0e1f5]180 ipc_callid_t callid = __SYSCALL3(SYS_IPC_CALL_ASYNC_SLOW, phoneid,
181 (sysarg_t) &call->msg.data, (sysarg_t) call);
[10477601]182
[dcc150cb]183 ipc_finish_async(callid, phoneid, call);
[b419162]184}
185
[10477601]186/** Answer received call (fast version).
[250717cc]187 *
[b74959bd]188 * The fast answer makes use of passing retval and first four arguments in
189 * registers. If you need to return more, use the ipc_answer_slow() instead.
[250717cc]190 *
[10477601]191 * @param callid Hash of the call being answered.
192 * @param retval Return value.
193 * @param arg1 First return argument.
194 * @param arg2 Second return argument.
195 * @param arg3 Third return argument.
196 * @param arg4 Fourth return argument.
197 *
198 * @return Zero on success.
199 * @return Value from @ref errno.h on failure.
[250717cc]200 *
201 */
[96b02eb9]202sysarg_t ipc_answer_fast(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
203 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[b419162]204{
[b74959bd]205 return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
206 arg4);
[b419162]207}
[06502f7d]208
[10477601]209/** Answer received call (entire payload).
210 *
211 * @param callid Hash of the call being answered.
212 * @param retval Return value.
213 * @param arg1 First return argument.
214 * @param arg2 Second return argument.
215 * @param arg3 Third return argument.
216 * @param arg4 Fourth return argument.
217 * @param arg5 Fifth return argument.
[250717cc]218 *
[10477601]219 * @return Zero on success.
220 * @return Value from @ref errno.h on failure.
[250717cc]221 *
222 */
[96b02eb9]223sysarg_t ipc_answer_slow(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
224 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
[250717cc]225{
[b74959bd]226 ipc_call_t data;
[10477601]227
[b74959bd]228 IPC_SET_RETVAL(data, retval);
229 IPC_SET_ARG1(data, arg1);
230 IPC_SET_ARG2(data, arg2);
231 IPC_SET_ARG3(data, arg3);
232 IPC_SET_ARG4(data, arg4);
233 IPC_SET_ARG5(data, arg5);
[10477601]234
[b74959bd]235 return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
[250717cc]236}
237
[10477601]238/** Handle received answer.
239 *
240 * @param callid Hash of the received answer.
241 * @param data Call data of the answer.
[936351c1]242 */
[4c61e60]243static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
[936351c1]244{
[7c0e1f5]245 async_call_t *call = data->label;
246
247 if (!call)
248 return;
249
250 if (call->callback)
251 call->callback(call->private, IPC_GET_RETVAL(*data), data);
252 free(call);
[936351c1]253}
254
[10477601]255/** Wait for first IPC call to come.
256 *
257 * @param call Incoming call storage.
258 * @param usec Timeout in microseconds
259 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
[8b243f2]260 *
[10477601]261 * @return Hash of the call. Note that certain bits have special
262 * meaning: IPC_CALLID_ANSWERED is set in an answer
263 * and IPC_CALLID_NOTIFICATION is used for notifications.
[b419162]264 *
265 */
[10477601]266ipc_callid_t ipc_wait_cycle(ipc_call_t *call, sysarg_t usec,
267 unsigned int flags)
[b419162]268{
[10477601]269 ipc_callid_t callid =
270 __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
271
[80649a91]272 /* Handle received answers */
[35f2bb1b]273 if (callid & IPC_CALLID_ANSWERED)
[80649a91]274 handle_answer(callid, call);
[10477601]275
[04a73cdf]276 return callid;
277}
278
[10477601]279/** Interrupt one thread of this task from waiting for IPC.
[04a73cdf]280 *
[10477601]281 */
282void ipc_poke(void)
283{
284 __SYSCALL0(SYS_IPC_POKE);
285}
286
287/** Wait for first IPC call to come.
[8b243f2]288 *
[10477601]289 * Only requests are returned, answers are processed internally.
290 *
291 * @param call Incoming call storage.
292 * @param usec Timeout in microseconds
293 *
294 * @return Hash of the call.
[096ba7a]295 *
[04a73cdf]296 */
[10477601]297ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, sysarg_t usec)
[04a73cdf]298{
299 ipc_callid_t callid;
[10477601]300
[04a73cdf]301 do {
[2d22049]302 callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
[04a73cdf]303 } while (callid & IPC_CALLID_ANSWERED);
[10477601]304
[04a73cdf]305 return callid;
306}
307
308/** Check if there is an IPC call waiting to be picked up.
309 *
[10477601]310 * Only requests are returned, answers are processed internally.
311 *
312 * @param call Incoming call storage.
313 *
314 * @return Hash of the call.
315 *
[04a73cdf]316 */
317ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
318{
319 ipc_callid_t callid;
[10477601]320
[04a73cdf]321 do {
[8b243f2]322 callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
323 SYNCH_FLAGS_NON_BLOCKING);
[06502f7d]324 } while (callid & IPC_CALLID_ANSWERED);
[10477601]325
[b419162]326 return callid;
327}
[5106e98]328
[8b243f2]329/** Hang up a phone.
330 *
[10477601]331 * @param phoneid Handle of the phone to be hung up.
332 *
333 * @return Zero on success or a negative error code.
[8b243f2]334 *
335 */
[7048773]336int ipc_hangup(int phoneid)
337{
338 return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
339}
[6180b57]340
[8b243f2]341/** Forward a received call to another destination.
[10477601]342 *
343 * For non-system methods, the old method, arg1 and arg2 are rewritten
344 * by the new values. For system methods, the new method, arg1 and arg2
345 * are written to the old arg1, arg2 and arg3, respectivelly. Calls with
346 * immutable methods are forwarded verbatim.
[8b243f2]347 *
[228e490]348 * @param callid Hash of the call to forward.
349 * @param phoneid Phone handle to use for forwarding.
350 * @param imethod New interface and method for the forwarded call.
351 * @param arg1 New value of the first argument for the forwarded call.
352 * @param arg2 New value of the second argument for the forwarded call.
353 * @param mode Flags specifying mode of the forward operation.
[8b243f2]354 *
[228e490]355 * @return Zero on success or an error code.
[8b243f2]356 *
357 */
[10477601]358int ipc_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
359 sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[043dcc27]360{
[228e490]361 return __SYSCALL6(SYS_IPC_FORWARD_FAST, callid, phoneid, imethod, arg1,
[90c35436]362 arg2, mode);
[043dcc27]363}
364
[10477601]365int ipc_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
[96b02eb9]366 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
[10477601]367 unsigned int mode)
[48daf64]368{
369 ipc_call_t data;
[228e490]370
371 IPC_SET_IMETHOD(data, imethod);
[48daf64]372 IPC_SET_ARG1(data, arg1);
373 IPC_SET_ARG2(data, arg2);
374 IPC_SET_ARG3(data, arg3);
375 IPC_SET_ARG4(data, arg4);
376 IPC_SET_ARG5(data, arg5);
[228e490]377
[10477601]378 return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data,
379 mode);
[48daf64]380}
381
[9a1b20c]382/** Connect to a task specified by id.
[6b10dab]383 *
[9a1b20c]384 */
385int ipc_connect_kbox(task_id_t id)
386{
[6b10dab]387#ifdef __32_BITS__
388 sysarg64_t arg = (sysarg64_t) id;
[9a1b20c]389 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
[6b10dab]390#endif
391
392#ifdef __64_BITS__
393 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
394#endif
[9a1b20c]395}
[6b10dab]396
[fadd381]397/** @}
[b2951e2]398 */
Note: See TracBrowser for help on using the repository browser.