source: mainline/uspace/lib/c/generic/ipc.c@ 40e5d66

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

Remove unused member of async_call_t

  • Property mode set to 100644
File size: 12.8 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/**
[35f2bb1b]52 * Structures of this type are used for keeping track of sent asynchronous calls.
[936351c1]53 */
54typedef struct {
55 link_t list;
[10477601]56
[936351c1]57 ipc_async_callback_t callback;
58 void *private;
[10477601]59
[936351c1]60 union {
61 ipc_callid_t callid;
62 struct {
[4c61e60]63 ipc_call_t data;
[936351c1]64 int phoneid;
65 } msg;
[8b243f2]66 } u;
[936351c1]67} async_call_t;
68
69LIST_INITIALIZE(dispatched_calls);
[fc42b28]70
[927a181e]71static futex_t ipc_futex = FUTEX_INITIALIZER;
[35509652]72
[10477601]73/** Send asynchronous message via syscall.
[8b243f2]74 *
[228e490]75 * @param phoneid Phone handle for the call.
76 * @param data Call data with the request.
77 *
78 * @return Hash of the call or an error code.
[8b243f2]79 *
80 */
[10477601]81static ipc_callid_t ipc_call_async_internal(int phoneid, ipc_call_t *data)
[936351c1]82{
[3209923]83 return __SYSCALL2(SYS_IPC_CALL_ASYNC_SLOW, phoneid, (sysarg_t) data);
[936351c1]84}
85
[c170438]86/** Prologue for ipc_call_async_*() functions.
[10477601]87 *
88 * @param private Argument for the answer/error callback.
89 * @param callback Answer/error callback.
[8b243f2]90 *
[10477601]91 * @return New, partially initialized async_call structure or NULL.
[8b243f2]92 *
93 */
94static inline async_call_t *ipc_prepare_async(void *private,
95 ipc_async_callback_t callback)
[b419162]96{
[10477601]97 async_call_t *call =
98 (async_call_t *) malloc(sizeof(async_call_t));
[936351c1]99 if (!call) {
[a784a96]100 if (callback)
101 callback(private, ENOMEM, NULL);
[10477601]102
[c1d2c9d]103 return NULL;
[936351c1]104 }
[10477601]105
[fc42b28]106 call->callback = callback;
107 call->private = private;
[10477601]108
[c1d2c9d]109 return call;
110}
111
[c170438]112/** Epilogue for ipc_call_async_*() functions.
[10477601]113 *
114 * @param callid Value returned by the SYS_IPC_CALL_ASYNC_* syscall.
115 * @param phoneid Phone handle through which the call was made.
116 * @param call Structure returned by ipc_prepare_async().
[8b243f2]117 */
118static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
[dcc150cb]119 async_call_t *call)
[c1d2c9d]120{
[10477601]121 if (!call) {
122 /* Nothing to do regardless if failed or not */
[cdbcf14]123 futex_unlock(&ipc_futex);
[d8b42fb2]124 return;
125 }
[10477601]126
[b78d0bd]127 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
[cdbcf14]128 futex_unlock(&ipc_futex);
[10477601]129
[06502f7d]130 /* Call asynchronous handler with error code */
[c1d2c9d]131 if (call->callback)
132 call->callback(call->private, ENOENT, NULL);
[10477601]133
[936351c1]134 free(call);
[06502f7d]135 return;
136 }
[10477601]137
[936351c1]138 call->u.callid = callid;
[10477601]139
[8b243f2]140 /* Add call to the list of dispatched calls */
[936351c1]141 list_append(&call->list, &dispatched_calls);
[cdbcf14]142 futex_unlock(&ipc_futex);
[c1d2c9d]143}
144
[10477601]145/** Fast asynchronous call.
[8b243f2]146 *
[3209923]147 * This function can only handle four arguments of payload. It is, however,
148 * faster than the more generic ipc_call_async_slow().
[8b243f2]149 *
150 * Note that this function is a void function.
[10477601]151 *
152 * During normal operation, answering this call will trigger the callback.
153 * In case of fatal error, the callback handler is called with the proper
154 * error code. If the call cannot be temporarily made, it is queued.
[c1d2c9d]155 *
[228e490]156 * @param phoneid Phone handle for the call.
157 * @param imethod Requested interface and method.
158 * @param arg1 Service-defined payload argument.
159 * @param arg2 Service-defined payload argument.
160 * @param arg3 Service-defined payload argument.
161 * @param arg4 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_fast(int phoneid, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]166 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, void *private,
[dcc150cb]167 ipc_async_callback_t callback)
[c1d2c9d]168{
[d8b42fb2]169 async_call_t *call = NULL;
[228e490]170
[d8b42fb2]171 if (callback) {
172 call = ipc_prepare_async(private, callback);
173 if (!call)
174 return;
175 }
[228e490]176
[8b243f2]177 /*
[10477601]178 * We need to make sure that we get callid
179 * before another thread accesses the queue again.
[8b243f2]180 */
[10477601]181
[cdbcf14]182 futex_lock(&ipc_futex);
[228e490]183 ipc_callid_t callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid,
184 imethod, arg1, arg2, arg3, arg4);
185
[dcc150cb]186 ipc_finish_async(callid, phoneid, call);
[c1d2c9d]187}
188
[10477601]189/** Asynchronous call transmitting the entire payload.
[8b243f2]190 *
191 * Note that this function is a void function.
[10477601]192 *
193 * During normal operation, answering this call will trigger the callback.
194 * In case of fatal error, the callback handler is called with the proper
195 * error code. If the call cannot be temporarily made, it is queued.
[8b243f2]196 *
[228e490]197 * @param phoneid Phone handle for the call.
198 * @param imethod Requested interface and method.
199 * @param arg1 Service-defined payload argument.
200 * @param arg2 Service-defined payload argument.
201 * @param arg3 Service-defined payload argument.
202 * @param arg4 Service-defined payload argument.
203 * @param arg5 Service-defined payload argument.
204 * @param private Argument to be passed to the answer/error callback.
205 * @param callback Answer or error callback.
[c1d2c9d]206 */
[228e490]207void ipc_call_async_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]208 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
[dcc150cb]209 ipc_async_callback_t callback)
[c1d2c9d]210{
[10477601]211 async_call_t *call = ipc_prepare_async(private, callback);
[c1d2c9d]212 if (!call)
213 return;
[10477601]214
[228e490]215 IPC_SET_IMETHOD(call->u.msg.data, imethod);
[c1d2c9d]216 IPC_SET_ARG1(call->u.msg.data, arg1);
217 IPC_SET_ARG2(call->u.msg.data, arg2);
218 IPC_SET_ARG3(call->u.msg.data, arg3);
[3209923]219 IPC_SET_ARG4(call->u.msg.data, arg4);
220 IPC_SET_ARG5(call->u.msg.data, arg5);
[10477601]221
[8b243f2]222 /*
[10477601]223 * We need to make sure that we get callid
224 * before another threadaccesses the queue again.
[8b243f2]225 */
[10477601]226
[cdbcf14]227 futex_lock(&ipc_futex);
[10477601]228 ipc_callid_t callid =
229 ipc_call_async_internal(phoneid, &call->u.msg.data);
230
[dcc150cb]231 ipc_finish_async(callid, phoneid, call);
[b419162]232}
233
[10477601]234/** Answer received call (fast version).
[250717cc]235 *
[b74959bd]236 * The fast answer makes use of passing retval and first four arguments in
237 * registers. If you need to return more, use the ipc_answer_slow() instead.
[250717cc]238 *
[10477601]239 * @param callid Hash of the call being answered.
240 * @param retval Return value.
241 * @param arg1 First return argument.
242 * @param arg2 Second return argument.
243 * @param arg3 Third return argument.
244 * @param arg4 Fourth return argument.
245 *
246 * @return Zero on success.
247 * @return Value from @ref errno.h on failure.
[250717cc]248 *
249 */
[96b02eb9]250sysarg_t ipc_answer_fast(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
251 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[b419162]252{
[b74959bd]253 return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
254 arg4);
[b419162]255}
[06502f7d]256
[10477601]257/** Answer received call (entire payload).
258 *
259 * @param callid Hash of the call being answered.
260 * @param retval Return value.
261 * @param arg1 First return argument.
262 * @param arg2 Second return argument.
263 * @param arg3 Third return argument.
264 * @param arg4 Fourth return argument.
265 * @param arg5 Fifth return argument.
[250717cc]266 *
[10477601]267 * @return Zero on success.
268 * @return Value from @ref errno.h on failure.
[250717cc]269 *
270 */
[96b02eb9]271sysarg_t ipc_answer_slow(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
272 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
[250717cc]273{
[b74959bd]274 ipc_call_t data;
[10477601]275
[b74959bd]276 IPC_SET_RETVAL(data, retval);
277 IPC_SET_ARG1(data, arg1);
278 IPC_SET_ARG2(data, arg2);
279 IPC_SET_ARG3(data, arg3);
280 IPC_SET_ARG4(data, arg4);
281 IPC_SET_ARG5(data, arg5);
[10477601]282
[b74959bd]283 return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
[250717cc]284}
285
[10477601]286/** Handle received answer.
[936351c1]287 *
[8b243f2]288 * Find the hash of the answer and call the answer callback.
[936351c1]289 *
[10477601]290 * The answer has the same hash as the request OR'ed with
291 * the IPC_CALLID_ANSWERED bit.
292 *
293 * @todo Use hash table.
294 *
295 * @param callid Hash of the received answer.
296 * @param data Call data of the answer.
[8b243f2]297 *
[936351c1]298 */
[4c61e60]299static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
[936351c1]300{
301 callid &= ~IPC_CALLID_ANSWERED;
302
[cdbcf14]303 futex_lock(&ipc_futex);
[10477601]304
305 link_t *item;
[b72efe8]306 for (item = dispatched_calls.head.next; item != &dispatched_calls.head;
[8b243f2]307 item = item->next) {
[10477601]308 async_call_t *call =
309 list_get_instance(item, async_call_t, list);
310
[936351c1]311 if (call->u.callid == callid) {
312 list_remove(&call->list);
[10477601]313
[cdbcf14]314 futex_unlock(&ipc_futex);
[10477601]315
[a784a96]316 if (call->callback)
[10477601]317 call->callback(call->private,
[8b243f2]318 IPC_GET_RETVAL(*data), data);
[10477601]319
[a784a96]320 free(call);
[936351c1]321 return;
322 }
323 }
[10477601]324
[cdbcf14]325 futex_unlock(&ipc_futex);
[936351c1]326}
327
[10477601]328/** Wait for first IPC call to come.
329 *
330 * @param call Incoming call storage.
331 * @param usec Timeout in microseconds
332 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
[8b243f2]333 *
[10477601]334 * @return Hash of the call. Note that certain bits have special
335 * meaning: IPC_CALLID_ANSWERED is set in an answer
336 * and IPC_CALLID_NOTIFICATION is used for notifications.
[b419162]337 *
338 */
[10477601]339ipc_callid_t ipc_wait_cycle(ipc_call_t *call, sysarg_t usec,
340 unsigned int flags)
[b419162]341{
[10477601]342 ipc_callid_t callid =
343 __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
344
[80649a91]345 /* Handle received answers */
[35f2bb1b]346 if (callid & IPC_CALLID_ANSWERED)
[80649a91]347 handle_answer(callid, call);
[10477601]348
[04a73cdf]349 return callid;
350}
351
[10477601]352/** Interrupt one thread of this task from waiting for IPC.
[04a73cdf]353 *
[10477601]354 */
355void ipc_poke(void)
356{
357 __SYSCALL0(SYS_IPC_POKE);
358}
359
360/** Wait for first IPC call to come.
[8b243f2]361 *
[10477601]362 * Only requests are returned, answers are processed internally.
363 *
364 * @param call Incoming call storage.
365 * @param usec Timeout in microseconds
366 *
367 * @return Hash of the call.
[096ba7a]368 *
[04a73cdf]369 */
[10477601]370ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, sysarg_t usec)
[04a73cdf]371{
372 ipc_callid_t callid;
[10477601]373
[04a73cdf]374 do {
[2d22049]375 callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
[04a73cdf]376 } while (callid & IPC_CALLID_ANSWERED);
[10477601]377
[04a73cdf]378 return callid;
379}
380
381/** Check if there is an IPC call waiting to be picked up.
382 *
[10477601]383 * Only requests are returned, answers are processed internally.
384 *
385 * @param call Incoming call storage.
386 *
387 * @return Hash of the call.
388 *
[04a73cdf]389 */
390ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
391{
392 ipc_callid_t callid;
[10477601]393
[04a73cdf]394 do {
[8b243f2]395 callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
396 SYNCH_FLAGS_NON_BLOCKING);
[06502f7d]397 } while (callid & IPC_CALLID_ANSWERED);
[10477601]398
[b419162]399 return callid;
400}
[5106e98]401
[8b243f2]402/** Hang up a phone.
403 *
[10477601]404 * @param phoneid Handle of the phone to be hung up.
405 *
406 * @return Zero on success or a negative error code.
[8b243f2]407 *
408 */
[7048773]409int ipc_hangup(int phoneid)
410{
411 return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
412}
[6180b57]413
[8b243f2]414/** Forward a received call to another destination.
[10477601]415 *
416 * For non-system methods, the old method, arg1 and arg2 are rewritten
417 * by the new values. For system methods, the new method, arg1 and arg2
418 * are written to the old arg1, arg2 and arg3, respectivelly. Calls with
419 * immutable methods are forwarded verbatim.
[8b243f2]420 *
[228e490]421 * @param callid Hash of the call to forward.
422 * @param phoneid Phone handle to use for forwarding.
423 * @param imethod New interface and method for the forwarded call.
424 * @param arg1 New value of the first argument for the forwarded call.
425 * @param arg2 New value of the second argument for the forwarded call.
426 * @param mode Flags specifying mode of the forward operation.
[8b243f2]427 *
[228e490]428 * @return Zero on success or an error code.
[8b243f2]429 *
430 */
[10477601]431int ipc_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
432 sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[043dcc27]433{
[228e490]434 return __SYSCALL6(SYS_IPC_FORWARD_FAST, callid, phoneid, imethod, arg1,
[90c35436]435 arg2, mode);
[043dcc27]436}
437
[10477601]438int ipc_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
[96b02eb9]439 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
[10477601]440 unsigned int mode)
[48daf64]441{
442 ipc_call_t data;
[228e490]443
444 IPC_SET_IMETHOD(data, imethod);
[48daf64]445 IPC_SET_ARG1(data, arg1);
446 IPC_SET_ARG2(data, arg2);
447 IPC_SET_ARG3(data, arg3);
448 IPC_SET_ARG4(data, arg4);
449 IPC_SET_ARG5(data, arg5);
[228e490]450
[10477601]451 return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data,
452 mode);
[48daf64]453}
454
[9a1b20c]455/** Connect to a task specified by id.
[6b10dab]456 *
[9a1b20c]457 */
458int ipc_connect_kbox(task_id_t id)
459{
[6b10dab]460#ifdef __32_BITS__
461 sysarg64_t arg = (sysarg64_t) id;
[9a1b20c]462 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
[6b10dab]463#endif
464
465#ifdef __64_BITS__
466 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
467#endif
[9a1b20c]468}
[6b10dab]469
[fadd381]470/** @}
[b2951e2]471 */
Note: See TracBrowser for help on using the repository browser.