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

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

Remove IPC_CALLID_* macros

  • Property mode set to 100644
File size: 11.1 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>
[38d150e]45#include <stdlib.h>
[936351c1]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 } msg;
[936351c1]62} async_call_t;
63
[c170438]64/** Prologue for ipc_call_async_*() functions.
[10477601]65 *
66 * @param private Argument for the answer/error callback.
67 * @param callback Answer/error callback.
[8b243f2]68 *
[10477601]69 * @return New, partially initialized async_call structure or NULL.
[8b243f2]70 *
71 */
72static inline async_call_t *ipc_prepare_async(void *private,
73 ipc_async_callback_t callback)
[b419162]74{
[10477601]75 async_call_t *call =
76 (async_call_t *) malloc(sizeof(async_call_t));
[936351c1]77 if (!call) {
[a784a96]78 if (callback)
79 callback(private, ENOMEM, NULL);
[10477601]80
[c1d2c9d]81 return NULL;
[936351c1]82 }
[10477601]83
[fc42b28]84 call->callback = callback;
85 call->private = private;
[10477601]86
[c1d2c9d]87 return call;
88}
89
[c170438]90/** Epilogue for ipc_call_async_*() functions.
[10477601]91 *
[01c3bb4]92 * @param rc Value returned by the SYS_IPC_CALL_ASYNC_* syscall.
93 * @param call Structure returned by ipc_prepare_async().
[8b243f2]94 */
[01c3bb4]95static inline void ipc_finish_async(int rc, async_call_t *call)
[c1d2c9d]96{
[10477601]97 if (!call) {
98 /* Nothing to do regardless if failed or not */
[d8b42fb2]99 return;
100 }
[10477601]101
[d7e245a]102 if (rc != EOK) {
[06502f7d]103 /* Call asynchronous handler with error code */
[c1d2c9d]104 if (call->callback)
105 call->callback(call->private, ENOENT, NULL);
[10477601]106
[936351c1]107 free(call);
[06502f7d]108 return;
109 }
[c1d2c9d]110}
111
[10477601]112/** Fast asynchronous call.
[8b243f2]113 *
[7c0e1f5]114 * This function can only handle three arguments of payload. It is, however,
[3209923]115 * faster than the more generic ipc_call_async_slow().
[8b243f2]116 *
117 * Note that this function is a void function.
[10477601]118 *
119 * During normal operation, answering this call will trigger the callback.
120 * In case of fatal error, the callback handler is called with the proper
121 * error code. If the call cannot be temporarily made, it is queued.
[c1d2c9d]122 *
[01c3bb4]123 * @param phandle Phone handle for the call.
124 * @param imethod Requested interface and method.
125 * @param arg1 Service-defined payload argument.
126 * @param arg2 Service-defined payload argument.
127 * @param arg3 Service-defined payload argument.
128 * @param private Argument to be passed to the answer/error callback.
129 * @param callback Answer or error callback.
[c1d2c9d]130 */
[01c3bb4]131void ipc_call_async_fast(cap_handle_t phandle, sysarg_t imethod, sysarg_t arg1,
[7c0e1f5]132 sysarg_t arg2, sysarg_t arg3, void *private, ipc_async_callback_t callback)
[c1d2c9d]133{
[b1f36e3]134 async_call_t *call = ipc_prepare_async(private, callback);
135 if (!call)
136 return;
[228e490]137
[01c3bb4]138 int rc = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phandle, imethod, arg1,
139 arg2, arg3, (sysarg_t) call);
[228e490]140
[01c3bb4]141 ipc_finish_async(rc, call);
[c1d2c9d]142}
143
[10477601]144/** Asynchronous call transmitting the entire payload.
[8b243f2]145 *
146 * Note that this function is a void function.
[10477601]147 *
148 * During normal operation, answering this call will trigger the callback.
149 * In case of fatal error, the callback handler is called with the proper
150 * error code. If the call cannot be temporarily made, it is queued.
[8b243f2]151 *
[01c3bb4]152 * @param phandle Phone handle for the call.
153 * @param imethod Requested interface and method.
154 * @param arg1 Service-defined payload argument.
155 * @param arg2 Service-defined payload argument.
156 * @param arg3 Service-defined payload argument.
157 * @param arg4 Service-defined payload argument.
158 * @param arg5 Service-defined payload argument.
159 * @param private Argument to be passed to the answer/error callback.
160 * @param callback Answer or error callback.
[c1d2c9d]161 */
[01c3bb4]162void ipc_call_async_slow(int phandle, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]163 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
[dcc150cb]164 ipc_async_callback_t callback)
[c1d2c9d]165{
[10477601]166 async_call_t *call = ipc_prepare_async(private, callback);
[c1d2c9d]167 if (!call)
168 return;
[10477601]169
[7c0e1f5]170 IPC_SET_IMETHOD(call->msg.data, imethod);
171 IPC_SET_ARG1(call->msg.data, arg1);
172 IPC_SET_ARG2(call->msg.data, arg2);
173 IPC_SET_ARG3(call->msg.data, arg3);
174 IPC_SET_ARG4(call->msg.data, arg4);
175 IPC_SET_ARG5(call->msg.data, arg5);
[10477601]176
[01c3bb4]177 int rc = __SYSCALL3(SYS_IPC_CALL_ASYNC_SLOW, phandle,
[7c0e1f5]178 (sysarg_t) &call->msg.data, (sysarg_t) call);
[10477601]179
[01c3bb4]180 ipc_finish_async(rc, call);
[b419162]181}
182
[10477601]183/** Answer received call (fast version).
[250717cc]184 *
[b74959bd]185 * The fast answer makes use of passing retval and first four arguments in
186 * registers. If you need to return more, use the ipc_answer_slow() instead.
[250717cc]187 *
[01c3bb4]188 * @param chandle Handle of the call being answered.
189 * @param retval Return value.
190 * @param arg1 First return argument.
191 * @param arg2 Second return argument.
192 * @param arg3 Third return argument.
193 * @param arg4 Fourth return argument.
[10477601]194 *
195 * @return Zero on success.
196 * @return Value from @ref errno.h on failure.
[250717cc]197 *
198 */
[01c3bb4]199sysarg_t ipc_answer_fast(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1,
[96b02eb9]200 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[b419162]201{
[01c3bb4]202 return __SYSCALL6(SYS_IPC_ANSWER_FAST, chandle, retval, arg1, arg2,
203 arg3, arg4);
[b419162]204}
[06502f7d]205
[10477601]206/** Answer received call (entire payload).
207 *
[01c3bb4]208 * @param chandle Handle of the call being answered.
209 * @param retval Return value.
210 * @param arg1 First return argument.
211 * @param arg2 Second return argument.
212 * @param arg3 Third return argument.
213 * @param arg4 Fourth return argument.
214 * @param arg5 Fifth return argument.
[250717cc]215 *
[10477601]216 * @return Zero on success.
217 * @return Value from @ref errno.h on failure.
[250717cc]218 *
219 */
[01c3bb4]220sysarg_t ipc_answer_slow(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1,
[96b02eb9]221 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
[250717cc]222{
[b74959bd]223 ipc_call_t data;
[10477601]224
[b74959bd]225 IPC_SET_RETVAL(data, retval);
226 IPC_SET_ARG1(data, arg1);
227 IPC_SET_ARG2(data, arg2);
228 IPC_SET_ARG3(data, arg3);
229 IPC_SET_ARG4(data, arg4);
230 IPC_SET_ARG5(data, arg5);
[10477601]231
[01c3bb4]232 return __SYSCALL2(SYS_IPC_ANSWER_SLOW, chandle, (sysarg_t) &data);
[250717cc]233}
234
[10477601]235/** Handle received answer.
236 *
[01c3bb4]237 * @param data Call data of the answer.
[936351c1]238 */
[01c3bb4]239static void handle_answer(ipc_call_t *data)
[936351c1]240{
[7c0e1f5]241 async_call_t *call = data->label;
242
243 if (!call)
244 return;
245
246 if (call->callback)
247 call->callback(call->private, IPC_GET_RETVAL(*data), data);
248 free(call);
[936351c1]249}
250
[10477601]251/** Wait for first IPC call to come.
252 *
[01c3bb4]253 * @param call Incoming call storage.
254 * @param usec Timeout in microseconds
255 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
[8b243f2]256 *
[01c3bb4]257 * @return Call handle.
258 * @return Negative error code.
[b419162]259 */
[01c3bb4]260cap_handle_t ipc_wait_cycle(ipc_call_t *call, sysarg_t usec, unsigned int flags)
[b419162]261{
[01c3bb4]262 cap_handle_t chandle =
[10477601]263 __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
264
[80649a91]265 /* Handle received answers */
[addbce4]266 if ((chandle == CAP_NIL) && (call->flags & IPC_CALL_ANSWERED))
[01c3bb4]267 handle_answer(call);
[10477601]268
[01c3bb4]269 return chandle;
[04a73cdf]270}
271
[10477601]272/** Interrupt one thread of this task from waiting for IPC.
[04a73cdf]273 *
[10477601]274 */
275void ipc_poke(void)
276{
277 __SYSCALL0(SYS_IPC_POKE);
278}
279
280/** Wait for first IPC call to come.
[8b243f2]281 *
[10477601]282 * Only requests are returned, answers are processed internally.
283 *
[01c3bb4]284 * @param call Incoming call storage.
285 * @param usec Timeout in microseconds
[10477601]286 *
[01c3bb4]287 * @return Call handle.
288 * @return Negative error code.
[096ba7a]289 *
[04a73cdf]290 */
[01c3bb4]291cap_handle_t ipc_wait_for_call_timeout(ipc_call_t *call, sysarg_t usec)
[04a73cdf]292{
[01c3bb4]293 cap_handle_t chandle;
[10477601]294
[04a73cdf]295 do {
[01c3bb4]296 chandle = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
[addbce4]297 } while ((chandle == CAP_NIL) && (call->flags & IPC_CALL_ANSWERED));
[10477601]298
[01c3bb4]299 return chandle;
[04a73cdf]300}
301
302/** Check if there is an IPC call waiting to be picked up.
303 *
[10477601]304 * Only requests are returned, answers are processed internally.
305 *
[01c3bb4]306 * @param call Incoming call storage.
[10477601]307 *
[01c3bb4]308 * @return Call handle.
309 * @return Negative error code.
[10477601]310 *
[04a73cdf]311 */
[01c3bb4]312cap_handle_t ipc_trywait_for_call(ipc_call_t *call)
[04a73cdf]313{
[01c3bb4]314 cap_handle_t chandle;
[10477601]315
[04a73cdf]316 do {
[01c3bb4]317 chandle = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
[8b243f2]318 SYNCH_FLAGS_NON_BLOCKING);
[addbce4]319 } while ((chandle == CAP_NIL) && (call->flags & IPC_CALL_ANSWERED));
[10477601]320
[01c3bb4]321 return chandle;
[b419162]322}
[5106e98]323
[8b243f2]324/** Hang up a phone.
325 *
[01c3bb4]326 * @param phandle Handle of the phone to be hung up.
[10477601]327 *
[01c3bb4]328 * @return Zero on success or a negative error code.
[8b243f2]329 *
330 */
[01c3bb4]331int ipc_hangup(cap_handle_t phandle)
[7048773]332{
[01c3bb4]333 return __SYSCALL1(SYS_IPC_HANGUP, phandle);
[7048773]334}
[6180b57]335
[8b243f2]336/** Forward a received call to another destination.
[10477601]337 *
[01c3bb4]338 * For non-system methods, the old method, arg1 and arg2 are rewritten by the
339 * new values. For system methods, the new method, arg1 and arg2 are written to
340 * the old arg1, arg2 and arg3, respectivelly. Calls with immutable methods are
341 * forwarded verbatim.
[8b243f2]342 *
[01c3bb4]343 * @param chandle Handle of the call to forward.
344 * @param phandle Phone handle to use for forwarding.
345 * @param imethod New interface and method for the forwarded call.
346 * @param arg1 New value of the first argument for the forwarded call.
347 * @param arg2 New value of the second argument for the forwarded call.
348 * @param mode Flags specifying mode of the forward operation.
[8b243f2]349 *
[01c3bb4]350 * @return Zero on success or an error code.
[8b243f2]351 *
352 */
[01c3bb4]353int ipc_forward_fast(cap_handle_t chandle, cap_handle_t phandle,
354 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[043dcc27]355{
[01c3bb4]356 return __SYSCALL6(SYS_IPC_FORWARD_FAST, chandle, phandle, imethod, arg1,
[90c35436]357 arg2, mode);
[043dcc27]358}
359
[01c3bb4]360int ipc_forward_slow(cap_handle_t chandle, cap_handle_t phandle,
361 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
362 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
[48daf64]363{
364 ipc_call_t data;
[228e490]365
366 IPC_SET_IMETHOD(data, imethod);
[48daf64]367 IPC_SET_ARG1(data, arg1);
368 IPC_SET_ARG2(data, arg2);
369 IPC_SET_ARG3(data, arg3);
370 IPC_SET_ARG4(data, arg4);
371 IPC_SET_ARG5(data, arg5);
[228e490]372
[01c3bb4]373 return __SYSCALL4(SYS_IPC_FORWARD_SLOW, chandle, phandle,
374 (sysarg_t) &data, mode);
[48daf64]375}
376
[9a1b20c]377/** Connect to a task specified by id.
[6b10dab]378 *
[9a1b20c]379 */
380int ipc_connect_kbox(task_id_t id)
381{
[6b10dab]382#ifdef __32_BITS__
383 sysarg64_t arg = (sysarg64_t) id;
[9a1b20c]384 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
[6b10dab]385#endif
386
387#ifdef __64_BITS__
388 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
389#endif
[9a1b20c]390}
[6b10dab]391
[fadd381]392/** @}
[b2951e2]393 */
Note: See TracBrowser for help on using the repository browser.