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