Rev 2568 | Rev 2618 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
954 | palkovsky | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2006 Ondrej Palkovsky |
954 | palkovsky | 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. |
||
1653 | cejka | 27 | */ |
28 | |||
29 | /** @addtogroup libc |
||
30 | * @{ |
||
31 | * @} |
||
32 | */ |
||
33 | |||
1866 | jermar | 34 | /** @addtogroup libcipc IPC |
1653 | cejka | 35 | * @brief HelenOS uspace IPC |
36 | * @{ |
||
37 | * @ingroup libc |
||
38 | */ |
||
39 | /** @file |
||
954 | palkovsky | 40 | */ |
41 | |||
1352 | palkovsky | 42 | #include <ipc/ipc.h> |
954 | palkovsky | 43 | #include <libc.h> |
999 | palkovsky | 44 | #include <malloc.h> |
45 | #include <errno.h> |
||
1352 | palkovsky | 46 | #include <libadt/list.h> |
999 | palkovsky | 47 | #include <stdio.h> |
48 | #include <unistd.h> |
||
1350 | palkovsky | 49 | #include <futex.h> |
1365 | jermar | 50 | #include <kernel/synch/synch.h> |
1463 | palkovsky | 51 | #include <async.h> |
2482 | jermar | 52 | #include <fibril.h> |
2522 | jermar | 53 | #include <assert.h> |
954 | palkovsky | 54 | |
2490 | jermar | 55 | /** |
56 | * Structures of this type are used for keeping track of sent asynchronous calls |
||
57 | * and queing unsent calls. |
||
999 | palkovsky | 58 | */ |
59 | typedef struct { |
||
60 | link_t list; |
||
61 | |||
62 | ipc_async_callback_t callback; |
||
63 | void *private; |
||
64 | union { |
||
65 | ipc_callid_t callid; |
||
66 | struct { |
||
1091 | palkovsky | 67 | ipc_call_t data; |
999 | palkovsky | 68 | int phoneid; |
69 | } msg; |
||
2471 | jermar | 70 | } u; |
2482 | jermar | 71 | fid_t fid; /**< Fibril waiting for sending this call. */ |
999 | palkovsky | 72 | } async_call_t; |
73 | |||
74 | LIST_INITIALIZE(dispatched_calls); |
||
75 | |||
2471 | jermar | 76 | /** List of asynchronous calls that were not accepted by kernel. |
77 | * |
||
78 | * It is protected by async_futex, because if the call cannot be sent into the |
||
79 | * kernel, the async framework is used automatically. |
||
1463 | palkovsky | 80 | */ |
2471 | jermar | 81 | LIST_INITIALIZE(queued_calls); |
1463 | palkovsky | 82 | |
1392 | palkovsky | 83 | static atomic_t ipc_futex = FUTEX_INITIALIZER; |
1350 | palkovsky | 84 | |
2471 | jermar | 85 | /** Make a fast synchronous call. |
86 | * |
||
2615 | jermar | 87 | * Only three payload arguments can be passed using this function. However, this |
88 | * function is faster than the generic ipc_call_sync_slow() because the payload |
||
89 | * is passed directly in registers. |
||
2471 | jermar | 90 | * |
91 | * @param phoneid Phone handle for the call. |
||
92 | * @param method Requested method. |
||
93 | * @param arg1 Service-defined payload argument. |
||
2615 | jermar | 94 | * @param arg2 Service-defined payload argument. |
95 | * @param arg3 Service-defined payload argument. |
||
96 | * @param result1 If non-NULL, the return ARG1 will be stored there. |
||
97 | * @param result2 If non-NULL, the return ARG2 will be stored there. |
||
98 | * @param result3 If non-NULL, the return ARG3 will be stored there. |
||
99 | * @param result4 If non-NULL, the return ARG4 will be stored there. |
||
100 | * @param result5 If non-NULL, the return ARG5 will be stored there. |
||
2471 | jermar | 101 | * |
102 | * @return Negative values represent errors returned by IPC. |
||
103 | * Otherwise the RETVAL of the answer is returned. |
||
104 | */ |
||
2615 | jermar | 105 | int |
106 | ipc_call_sync_fast(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, |
||
107 | ipcarg_t arg3, ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3, |
||
108 | ipcarg_t *result4, ipcarg_t *result5) |
||
954 | palkovsky | 109 | { |
1091 | palkovsky | 110 | ipc_call_t resdata; |
966 | palkovsky | 111 | int callres; |
112 | |||
2615 | jermar | 113 | callres = __SYSCALL6(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1, |
114 | arg2, arg3, (sysarg_t) &resdata); |
||
966 | palkovsky | 115 | if (callres) |
116 | return callres; |
||
2615 | jermar | 117 | if (result1) |
118 | *result1 = IPC_GET_ARG1(resdata); |
||
119 | if (result2) |
||
120 | *result2 = IPC_GET_ARG2(resdata); |
||
121 | if (result3) |
||
122 | *result3 = IPC_GET_ARG3(resdata); |
||
123 | if (result4) |
||
124 | *result4 = IPC_GET_ARG4(resdata); |
||
125 | if (result5) |
||
126 | *result5 = IPC_GET_ARG5(resdata); |
||
127 | |||
966 | palkovsky | 128 | return IPC_GET_RETVAL(resdata); |
954 | palkovsky | 129 | } |
130 | |||
2615 | jermar | 131 | /** Make a synchronous call transmitting 5 arguments of payload. |
2471 | jermar | 132 | * |
133 | * @param phoneid Phone handle for the call. |
||
134 | * @param method Requested method. |
||
135 | * @param arg1 Service-defined payload argument. |
||
136 | * @param arg2 Service-defined payload argument. |
||
137 | * @param arg3 Service-defined payload argument. |
||
2615 | jermar | 138 | * @param arg4 Service-defined payload argument. |
139 | * @param arg5 Service-defined payload argument. |
||
2471 | jermar | 140 | * @param result1 If non-NULL, storage for the first return argument. |
141 | * @param result2 If non-NULL, storage for the second return argument. |
||
142 | * @param result3 If non-NULL, storage for the third return argument. |
||
2615 | jermar | 143 | * @param result4 If non-NULL, storage for the fourth return argument. |
144 | * @param result5 If non-NULL, storage for the fifth return argument. |
||
2471 | jermar | 145 | * |
146 | * @return Negative value means IPC error. |
||
147 | * Otherwise the RETVAL of the answer. |
||
148 | */ |
||
2615 | jermar | 149 | int |
150 | ipc_call_sync_slow(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, |
||
151 | ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *result1, |
||
152 | ipcarg_t *result2, ipcarg_t *result3, ipcarg_t *result4, ipcarg_t *result5) |
||
954 | palkovsky | 153 | { |
1091 | palkovsky | 154 | ipc_call_t data; |
966 | palkovsky | 155 | int callres; |
156 | |||
157 | IPC_SET_METHOD(data, method); |
||
158 | IPC_SET_ARG1(data, arg1); |
||
159 | IPC_SET_ARG2(data, arg2); |
||
160 | IPC_SET_ARG3(data, arg3); |
||
2615 | jermar | 161 | IPC_SET_ARG4(data, arg4); |
162 | IPC_SET_ARG5(data, arg5); |
||
966 | palkovsky | 163 | |
2615 | jermar | 164 | callres = __SYSCALL3(SYS_IPC_CALL_SYNC_SLOW, phoneid, (sysarg_t) &data, |
2471 | jermar | 165 | (sysarg_t) &data); |
966 | palkovsky | 166 | if (callres) |
167 | return callres; |
||
168 | |||
169 | if (result1) |
||
170 | *result1 = IPC_GET_ARG1(data); |
||
171 | if (result2) |
||
172 | *result2 = IPC_GET_ARG2(data); |
||
173 | if (result3) |
||
174 | *result3 = IPC_GET_ARG3(data); |
||
2615 | jermar | 175 | if (result4) |
176 | *result4 = IPC_GET_ARG4(data); |
||
177 | if (result5) |
||
178 | *result5 = IPC_GET_ARG5(data); |
||
179 | |||
966 | palkovsky | 180 | return IPC_GET_RETVAL(data); |
181 | } |
||
182 | |||
2471 | jermar | 183 | /** Syscall to send asynchronous message. |
184 | * |
||
185 | * @param phoneid Phone handle for the call. |
||
186 | * @param data Call data with the request. |
||
187 | * |
||
188 | * @return Hash of the call or an error code. |
||
189 | */ |
||
190 | static ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data) |
||
999 | palkovsky | 191 | { |
2471 | jermar | 192 | return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t) data); |
999 | palkovsky | 193 | } |
194 | |||
2471 | jermar | 195 | /** Prolog to ipc_call_async_*() functions. |
196 | * |
||
197 | * @param private Argument for the answer/error callback. |
||
198 | * @param callback Answer/error callback. |
||
199 | * |
||
200 | * @return New, partially initialized async_call structure or NULL. |
||
201 | */ |
||
202 | static inline async_call_t *ipc_prepare_async(void *private, |
||
203 | ipc_async_callback_t callback) |
||
966 | palkovsky | 204 | { |
999 | palkovsky | 205 | async_call_t *call; |
966 | palkovsky | 206 | |
999 | palkovsky | 207 | call = malloc(sizeof(*call)); |
208 | if (!call) { |
||
1443 | palkovsky | 209 | if (callback) |
210 | callback(private, ENOMEM, NULL); |
||
1489 | palkovsky | 211 | return NULL; |
999 | palkovsky | 212 | } |
1463 | palkovsky | 213 | call->callback = callback; |
214 | call->private = private; |
||
215 | |||
1489 | palkovsky | 216 | return call; |
217 | } |
||
218 | |||
2471 | jermar | 219 | /** Epilogue of ipc_call_async_*() functions. |
220 | * |
||
221 | * @param callid Value returned by the SYS_IPC_CALL_ASYNC_* syscall. |
||
222 | * @param phoneid Phone handle through which the call was made. |
||
223 | * @param call async_call structure returned by ipc_prepare_async(). |
||
2530 | jermar | 224 | * @param can_preempt If non-zero, the current fibril can be preempted in this |
225 | * call. |
||
2471 | jermar | 226 | */ |
227 | static inline void ipc_finish_async(ipc_callid_t callid, int phoneid, |
||
228 | async_call_t *call, int can_preempt) |
||
1489 | palkovsky | 229 | { |
1751 | palkovsky | 230 | if (!call) { /* Nothing to do regardless if failed or not */ |
231 | futex_up(&ipc_futex); |
||
232 | return; |
||
233 | } |
||
234 | |||
966 | palkovsky | 235 | if (callid == IPC_CALLRET_FATAL) { |
1463 | palkovsky | 236 | futex_up(&ipc_futex); |
966 | palkovsky | 237 | /* Call asynchronous handler with error code */ |
1489 | palkovsky | 238 | if (call->callback) |
239 | call->callback(call->private, ENOENT, NULL); |
||
999 | palkovsky | 240 | free(call); |
966 | palkovsky | 241 | return; |
242 | } |
||
999 | palkovsky | 243 | |
1463 | palkovsky | 244 | if (callid == IPC_CALLRET_TEMPORARY) { |
245 | futex_up(&ipc_futex); |
||
999 | palkovsky | 246 | |
247 | call->u.msg.phoneid = phoneid; |
||
1518 | palkovsky | 248 | |
1463 | palkovsky | 249 | futex_down(&async_futex); |
999 | palkovsky | 250 | list_append(&call->list, &queued_calls); |
1463 | palkovsky | 251 | |
1518 | palkovsky | 252 | if (can_preempt) { |
2482 | jermar | 253 | call->fid = fibril_get_id(); |
2568 | jermar | 254 | fibril_switch(FIBRIL_TO_MANAGER); |
1518 | palkovsky | 255 | /* Async futex unlocked by previous call */ |
256 | } else { |
||
2482 | jermar | 257 | call->fid = 0; |
1518 | palkovsky | 258 | futex_up(&async_futex); |
259 | } |
||
966 | palkovsky | 260 | return; |
261 | } |
||
999 | palkovsky | 262 | call->u.callid = callid; |
2471 | jermar | 263 | /* Add call to the list of dispatched calls */ |
999 | palkovsky | 264 | list_append(&call->list, &dispatched_calls); |
1350 | palkovsky | 265 | futex_up(&ipc_futex); |
1489 | palkovsky | 266 | |
954 | palkovsky | 267 | } |
268 | |||
2471 | jermar | 269 | /** Make a fast asynchronous call. |
1489 | palkovsky | 270 | * |
2471 | jermar | 271 | * This function can only handle two arguments of payload. It is, however, |
272 | * faster than the more generic ipc_call_async_3(). |
||
273 | * |
||
274 | * Note that this function is a void function. |
||
275 | * During normal opertation, answering this call will trigger the callback. |
||
276 | * In case of fatal error, call the callback handler with the proper error code. |
||
277 | * If the call cannot be temporarily made, queue it. |
||
278 | * |
||
279 | * @param phoneid Phone handle for the call. |
||
280 | * @param method Requested method. |
||
281 | * @param arg1 Service-defined payload argument. |
||
282 | * @param arg2 Service-defined payload argument. |
||
283 | * @param private Argument to be passed to the answer/error callback. |
||
284 | * @param callback Answer or error callback. |
||
2530 | jermar | 285 | * @param can_preempt If non-zero, the current fibril will be preempted in |
286 | * case the kernel temporarily refuses to accept more |
||
2471 | jermar | 287 | * asynchronous calls. |
1489 | palkovsky | 288 | */ |
289 | void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1, |
||
2471 | jermar | 290 | ipcarg_t arg2, void *private, ipc_async_callback_t callback, |
291 | int can_preempt) |
||
1489 | palkovsky | 292 | { |
1751 | palkovsky | 293 | async_call_t *call = NULL; |
1489 | palkovsky | 294 | ipc_callid_t callid; |
966 | palkovsky | 295 | |
1751 | palkovsky | 296 | if (callback) { |
297 | call = ipc_prepare_async(private, callback); |
||
298 | if (!call) |
||
299 | return; |
||
300 | } |
||
1489 | palkovsky | 301 | |
2471 | jermar | 302 | /* |
303 | * We need to make sure that we get callid before another thread |
||
304 | * accesses the queue again. |
||
305 | */ |
||
1489 | palkovsky | 306 | futex_down(&ipc_futex); |
2471 | jermar | 307 | callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, |
308 | arg2); |
||
1489 | palkovsky | 309 | |
310 | if (callid == IPC_CALLRET_TEMPORARY) { |
||
1751 | palkovsky | 311 | if (!call) { |
312 | call = ipc_prepare_async(private, callback); |
||
313 | if (!call) |
||
314 | return; |
||
315 | } |
||
1489 | palkovsky | 316 | IPC_SET_METHOD(call->u.msg.data, method); |
317 | IPC_SET_ARG1(call->u.msg.data, arg1); |
||
318 | IPC_SET_ARG2(call->u.msg.data, arg2); |
||
319 | } |
||
1518 | palkovsky | 320 | ipc_finish_async(callid, phoneid, call, can_preempt); |
1489 | palkovsky | 321 | } |
322 | |||
2471 | jermar | 323 | /** Make an asynchronous call transmitting the entire payload. |
1489 | palkovsky | 324 | * |
2471 | jermar | 325 | * Note that this function is a void function. |
326 | * During normal opertation, answering this call will trigger the callback. |
||
327 | * In case of fatal error, call the callback handler with the proper error code. |
||
328 | * If the call cannot be temporarily made, queue it. |
||
329 | * |
||
330 | * @param phoneid Phone handle for the call. |
||
331 | * @param method Requested method. |
||
332 | * @param arg1 Service-defined payload argument. |
||
333 | * @param arg2 Service-defined payload argument. |
||
334 | * @param arg3 Service-defined payload argument. |
||
335 | * @param private Argument to be passed to the answer/error callback. |
||
336 | * @param callback Answer or error callback. |
||
2530 | jermar | 337 | * @param can_preempt If non-zero, the current fibril will be preempted in |
338 | * case the kernel temporarily refuses to accept more |
||
2471 | jermar | 339 | * asynchronous calls. |
340 | * |
||
1489 | palkovsky | 341 | */ |
342 | void ipc_call_async_3(int phoneid, ipcarg_t method, ipcarg_t arg1, |
||
2471 | jermar | 343 | ipcarg_t arg2, ipcarg_t arg3, void *private, ipc_async_callback_t callback, |
344 | int can_preempt) |
||
1489 | palkovsky | 345 | { |
346 | async_call_t *call; |
||
347 | ipc_callid_t callid; |
||
348 | |||
349 | call = ipc_prepare_async(private, callback); |
||
350 | if (!call) |
||
351 | return; |
||
352 | |||
353 | IPC_SET_METHOD(call->u.msg.data, method); |
||
354 | IPC_SET_ARG1(call->u.msg.data, arg1); |
||
355 | IPC_SET_ARG2(call->u.msg.data, arg2); |
||
356 | IPC_SET_ARG3(call->u.msg.data, arg3); |
||
2471 | jermar | 357 | /* |
358 | * We need to make sure that we get callid before another thread accesses |
||
359 | * the queue again. |
||
360 | */ |
||
1489 | palkovsky | 361 | futex_down(&ipc_futex); |
362 | callid = _ipc_call_async(phoneid, &call->u.msg.data); |
||
363 | |||
1518 | palkovsky | 364 | ipc_finish_async(callid, phoneid, call, can_preempt); |
1489 | palkovsky | 365 | } |
366 | |||
367 | |||
2471 | jermar | 368 | /** Answer a received call - fast version. |
1343 | jermar | 369 | * |
2471 | jermar | 370 | * The fast answer makes use of passing retval and first two arguments in |
371 | * registers. If you need to return more, use the ipc_answer() instead. |
||
1343 | jermar | 372 | * |
2471 | jermar | 373 | * @param callid Hash of the call being answered. |
374 | * @param retval Return value. |
||
375 | * @param arg1 First return argument. |
||
376 | * @param arg2 Second return argument. |
||
1343 | jermar | 377 | * |
2471 | jermar | 378 | * @return Zero on success or a value from @ref errno.h on failure. |
1343 | jermar | 379 | */ |
380 | ipcarg_t ipc_answer_fast(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1, |
||
2471 | jermar | 381 | ipcarg_t arg2) |
954 | palkovsky | 382 | { |
1330 | palkovsky | 383 | return __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2); |
954 | palkovsky | 384 | } |
385 | |||
2471 | jermar | 386 | /** Answer a received call - full version. |
1343 | jermar | 387 | * |
2471 | jermar | 388 | * @param callid Hash of the call being answered. |
389 | * @param call Call structure with the answer. |
||
390 | * Must be already initialized by the responder. |
||
1343 | jermar | 391 | * |
2471 | jermar | 392 | * @return Zero on success or a value from @ref errno.h on failure. |
1343 | jermar | 393 | */ |
394 | ipcarg_t ipc_answer(ipc_callid_t callid, ipc_call_t *call) |
||
395 | { |
||
396 | return __SYSCALL2(SYS_IPC_ANSWER, callid, (sysarg_t) call); |
||
397 | } |
||
398 | |||
399 | |||
2471 | jermar | 400 | /** Try to dispatch queued calls from the async queue. */ |
999 | palkovsky | 401 | static void try_dispatch_queued_calls(void) |
402 | { |
||
403 | async_call_t *call; |
||
404 | ipc_callid_t callid; |
||
405 | |||
2471 | jermar | 406 | /** @todo |
407 | * Integrate intelligently ipc_futex, so that it is locked during |
||
408 | * ipc_call_async_*(), until it is added to dispatched_calls. |
||
1463 | palkovsky | 409 | */ |
410 | futex_down(&async_futex); |
||
999 | palkovsky | 411 | while (!list_empty(&queued_calls)) { |
2471 | jermar | 412 | call = list_get_instance(queued_calls.next, async_call_t, list); |
413 | callid = _ipc_call_async(call->u.msg.phoneid, &call->u.msg.data); |
||
1463 | palkovsky | 414 | if (callid == IPC_CALLRET_TEMPORARY) { |
999 | palkovsky | 415 | break; |
1463 | palkovsky | 416 | } |
999 | palkovsky | 417 | list_remove(&call->list); |
1350 | palkovsky | 418 | |
1463 | palkovsky | 419 | futex_up(&async_futex); |
2482 | jermar | 420 | if (call->fid) |
421 | fibril_add_ready(call->fid); |
||
1463 | palkovsky | 422 | |
999 | palkovsky | 423 | if (callid == IPC_CALLRET_FATAL) { |
1443 | palkovsky | 424 | if (call->callback) |
425 | call->callback(call->private, ENOENT, NULL); |
||
999 | palkovsky | 426 | free(call); |
427 | } else { |
||
428 | call->u.callid = callid; |
||
1463 | palkovsky | 429 | futex_down(&ipc_futex); |
999 | palkovsky | 430 | list_append(&call->list, &dispatched_calls); |
1463 | palkovsky | 431 | futex_up(&ipc_futex); |
999 | palkovsky | 432 | } |
1463 | palkovsky | 433 | futex_down(&async_futex); |
999 | palkovsky | 434 | } |
1463 | palkovsky | 435 | futex_up(&async_futex); |
999 | palkovsky | 436 | } |
437 | |||
2471 | jermar | 438 | /** Handle a received answer. |
999 | palkovsky | 439 | * |
2471 | jermar | 440 | * Find the hash of the answer and call the answer callback. |
999 | palkovsky | 441 | * |
2471 | jermar | 442 | * @todo Make it use hash table. |
443 | * |
||
444 | * @param callid Hash of the received answer. |
||
445 | * The answer has the same hash as the request OR'ed with |
||
446 | * the IPC_CALLID_ANSWERED bit. |
||
447 | * @param data Call data of the answer. |
||
999 | palkovsky | 448 | */ |
1091 | palkovsky | 449 | static void handle_answer(ipc_callid_t callid, ipc_call_t *data) |
999 | palkovsky | 450 | { |
451 | link_t *item; |
||
452 | async_call_t *call; |
||
453 | |||
454 | callid &= ~IPC_CALLID_ANSWERED; |
||
455 | |||
1350 | palkovsky | 456 | futex_down(&ipc_futex); |
999 | palkovsky | 457 | for (item = dispatched_calls.next; item != &dispatched_calls; |
2471 | jermar | 458 | item = item->next) { |
999 | palkovsky | 459 | call = list_get_instance(item, async_call_t, list); |
460 | if (call->u.callid == callid) { |
||
461 | list_remove(&call->list); |
||
1350 | palkovsky | 462 | futex_up(&ipc_futex); |
1443 | palkovsky | 463 | if (call->callback) |
464 | call->callback(call->private, |
||
2471 | jermar | 465 | IPC_GET_RETVAL(*data), data); |
1443 | palkovsky | 466 | free(call); |
999 | palkovsky | 467 | return; |
468 | } |
||
469 | } |
||
1350 | palkovsky | 470 | futex_up(&ipc_futex); |
999 | palkovsky | 471 | } |
472 | |||
473 | |||
2471 | jermar | 474 | /** Wait for a first call to come. |
954 | palkovsky | 475 | * |
2471 | jermar | 476 | * @param call Storage where the incoming call data will be stored. |
477 | * @param usec Timeout in microseconds |
||
478 | * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking). |
||
479 | * |
||
480 | * @return Hash of the call. Note that certain bits have special |
||
481 | * meaning. IPC_CALLID_ANSWERED will be set in an answer |
||
482 | * and IPC_CALLID_NOTIFICATION is used for notifications. |
||
483 | * |
||
954 | palkovsky | 484 | */ |
1392 | palkovsky | 485 | ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags) |
954 | palkovsky | 486 | { |
487 | ipc_callid_t callid; |
||
488 | |||
1392 | palkovsky | 489 | callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags); |
490 | /* Handle received answers */ |
||
1463 | palkovsky | 491 | if (callid & IPC_CALLID_ANSWERED) { |
1392 | palkovsky | 492 | handle_answer(callid, call); |
1463 | palkovsky | 493 | try_dispatch_queued_calls(); |
494 | } |
||
999 | palkovsky | 495 | |
954 | palkovsky | 496 | return callid; |
497 | } |
||
1028 | palkovsky | 498 | |
1365 | jermar | 499 | /** Wait some time for an IPC call. |
500 | * |
||
2471 | jermar | 501 | * The call will return after an answer is received. |
1709 | jermar | 502 | * |
2471 | jermar | 503 | * @param call Storage where the incoming call data will be stored. |
504 | * @param usec Timeout in microseconds. |
||
505 | * |
||
506 | * @return Hash of the answer. |
||
1365 | jermar | 507 | */ |
508 | ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec) |
||
509 | { |
||
510 | ipc_callid_t callid; |
||
511 | |||
512 | do { |
||
1503 | jermar | 513 | callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE); |
1365 | jermar | 514 | } while (callid & IPC_CALLID_ANSWERED); |
515 | |||
516 | return callid; |
||
517 | } |
||
518 | |||
519 | /** Check if there is an IPC call waiting to be picked up. |
||
520 | * |
||
2471 | jermar | 521 | * @param call Storage where the incoming call will be stored. |
522 | * @return Hash of the answer. |
||
1365 | jermar | 523 | */ |
524 | ipc_callid_t ipc_trywait_for_call(ipc_call_t *call) |
||
525 | { |
||
526 | ipc_callid_t callid; |
||
527 | |||
528 | do { |
||
2471 | jermar | 529 | callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT, |
530 | SYNCH_FLAGS_NON_BLOCKING); |
||
1365 | jermar | 531 | } while (callid & IPC_CALLID_ANSWERED); |
532 | |||
533 | return callid; |
||
534 | } |
||
535 | |||
2359 | jermar | 536 | /** Ask destination to do a callback connection. |
1091 | palkovsky | 537 | * |
2471 | jermar | 538 | * @param phoneid Phone handle used for contacting the other side. |
539 | * @param arg1 Service-defined argument. |
||
540 | * @param arg2 Service-defined argument. |
||
541 | * @param phonehash Storage where the library will store an opaque |
||
2359 | jermar | 542 | * identifier of the phone that will be used for incoming |
2471 | jermar | 543 | * calls. This identifier can be used for connection |
544 | * tracking. |
||
545 | * |
||
546 | * @return Zero on success or a negative error code. |
||
1091 | palkovsky | 547 | */ |
2359 | jermar | 548 | int ipc_connect_to_me(int phoneid, int arg1, int arg2, ipcarg_t *phonehash) |
1028 | palkovsky | 549 | { |
2615 | jermar | 550 | return ipc_call_sync_2_3(phoneid, IPC_M_CONNECT_TO_ME, arg1, arg2, |
551 | NULL, NULL, phonehash); |
||
1028 | palkovsky | 552 | } |
1061 | palkovsky | 553 | |
2359 | jermar | 554 | /** Ask through phone for a new connection to some service. |
1091 | palkovsky | 555 | * |
2471 | jermar | 556 | * @param phoneid Phone handle used for contacting the other side. |
2359 | jermar | 557 | * @param arg1 User defined argument. |
558 | * @param arg2 User defined argument. |
||
559 | * |
||
2471 | jermar | 560 | * @return New phone handle on success or a negative error code. |
1091 | palkovsky | 561 | */ |
1061 | palkovsky | 562 | int ipc_connect_me_to(int phoneid, int arg1, int arg2) |
563 | { |
||
1092 | palkovsky | 564 | ipcarg_t newphid; |
1091 | palkovsky | 565 | int res; |
566 | |||
2615 | jermar | 567 | res = ipc_call_sync_2_3(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, NULL, |
568 | NULL, &newphid); |
||
1091 | palkovsky | 569 | if (res) |
570 | return res; |
||
571 | return newphid; |
||
1061 | palkovsky | 572 | } |
573 | |||
2471 | jermar | 574 | /** Hang up a phone. |
575 | * |
||
576 | * @param phoneid Handle of the phone to be hung up. |
||
577 | * |
||
578 | * @return Zero on success or a negative error code. |
||
579 | */ |
||
1089 | palkovsky | 580 | int ipc_hangup(int phoneid) |
581 | { |
||
582 | return __SYSCALL1(SYS_IPC_HANGUP, phoneid); |
||
583 | } |
||
1259 | palkovsky | 584 | |
1923 | jermar | 585 | /** Register IRQ notification. |
586 | * |
||
2471 | jermar | 587 | * @param inr IRQ number. |
588 | * @param devno Device number of the device generating inr. |
||
589 | * @param method Use this method for notifying me. |
||
590 | * @param ucode Top-half pseudocode handler. |
||
1923 | jermar | 591 | * |
2471 | jermar | 592 | * @return Value returned by the kernel. |
1923 | jermar | 593 | */ |
594 | int ipc_register_irq(int inr, int devno, int method, irq_code_t *ucode) |
||
1259 | palkovsky | 595 | { |
2471 | jermar | 596 | return __SYSCALL4(SYS_IPC_REGISTER_IRQ, inr, devno, method, |
597 | (sysarg_t) ucode); |
||
1259 | palkovsky | 598 | } |
599 | |||
1923 | jermar | 600 | /** Unregister IRQ notification. |
601 | * |
||
2471 | jermar | 602 | * @param inr IRQ number. |
603 | * @param devno Device number of the device generating inr. |
||
1923 | jermar | 604 | * |
2471 | jermar | 605 | * @return Value returned by the kernel. |
1923 | jermar | 606 | */ |
607 | int ipc_unregister_irq(int inr, int devno) |
||
1259 | palkovsky | 608 | { |
1923 | jermar | 609 | return __SYSCALL2(SYS_IPC_UNREGISTER_IRQ, inr, devno); |
1259 | palkovsky | 610 | } |
1330 | palkovsky | 611 | |
2471 | jermar | 612 | /** Forward a received call to another destination. |
613 | * |
||
614 | * @param callid Hash of the call to forward. |
||
615 | * @param phoneid Phone handle to use for forwarding. |
||
616 | * @param method New method for the forwarded call. |
||
617 | * @param arg1 New value of the first argument for the forwarded call. |
||
618 | * |
||
619 | * @return Zero on success or an error code. |
||
620 | * |
||
621 | * For non-system methods, the old method and arg1 are rewritten by the new |
||
622 | * values. For system methods, the new method and arg1 are written to the old |
||
623 | * arg1 and arg2, respectivelly. |
||
624 | */ |
||
1336 | jermar | 625 | int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method, ipcarg_t arg1) |
626 | { |
||
627 | return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1); |
||
628 | } |
||
629 | |||
2531 | jermar | 630 | /** Wrapper for making IPC_M_DATA_SEND calls. |
2522 | jermar | 631 | * |
2531 | jermar | 632 | * @param phoneid Phone that will be used to contact the receiving side. |
633 | * @param src Address of the beginning of the source buffer. |
||
634 | * @param size Size of the source buffer. |
||
635 | * |
||
636 | * @return Zero on success or a negative error code from errno.h. |
||
637 | */ |
||
638 | int ipc_data_send(int phoneid, void *src, size_t size) |
||
639 | { |
||
2615 | jermar | 640 | return ipc_call_sync_3_0(phoneid, IPC_M_DATA_SEND, 0, (ipcarg_t) src, |
641 | (ipcarg_t) size); |
||
2531 | jermar | 642 | } |
643 | |||
644 | /** Wrapper for receiving the IPC_M_DATA_SEND calls. |
||
645 | * |
||
646 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_SEND calls |
||
2522 | jermar | 647 | * so that the user doesn't have to remember the meaning of each IPC argument. |
648 | * |
||
649 | * So far, this wrapper is to be used from within a connection fibril. |
||
650 | * |
||
651 | * @param callid Storage where the hash of the IPC_M_DATA_SEND call will |
||
652 | * be stored. |
||
653 | * @param call Storage where the incoming call will be stored. |
||
654 | * @param dst Storage where the suggested destination address will |
||
655 | * be stored. May be NULL. |
||
656 | * @param size Storage where the suggested size will be stored. May be |
||
657 | * NULL |
||
658 | * |
||
659 | * @return Non-zero on success, zero on failure. |
||
660 | */ |
||
2531 | jermar | 661 | int ipc_data_receive(ipc_callid_t *callid, ipc_call_t *call, void **dst, |
2522 | jermar | 662 | size_t *size) |
663 | { |
||
664 | assert(callid); |
||
665 | assert(call); |
||
666 | |||
667 | *callid = async_get_call(call); |
||
668 | if (IPC_GET_METHOD(*call) != IPC_M_DATA_SEND) |
||
669 | return 0; |
||
670 | if (dst) |
||
671 | *dst = (void *) IPC_GET_ARG1(*call); |
||
672 | if (size) |
||
673 | *size = (size_t) IPC_GET_ARG3(*call); |
||
674 | return 1; |
||
675 | } |
||
676 | |||
677 | /** Wrapper for answering the IPC_M_DATA_SEND calls. |
||
678 | * |
||
679 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_SEND calls |
||
680 | * so that the user doesn't have to remember the meaning of each IPC argument. |
||
681 | * |
||
682 | * @param callid Hash of the IPC_M_DATA_SEND call to answer. |
||
683 | * @param call Call structure with the request. |
||
684 | * @param dst Final destination address for the IPC_M_DATA_SEND call. |
||
685 | * @param size Final size for the IPC_M_DATA_SEND call. |
||
686 | * |
||
687 | * @return Zero on success or a value from @ref errno.h on failure. |
||
688 | */ |
||
2531 | jermar | 689 | ipcarg_t ipc_data_deliver(ipc_callid_t callid, ipc_call_t *call, void *dst, |
690 | size_t size) |
||
2522 | jermar | 691 | { |
692 | IPC_SET_RETVAL(*call, EOK); |
||
693 | IPC_SET_ARG1(*call, (ipcarg_t) dst); |
||
694 | IPC_SET_ARG3(*call, (ipcarg_t) size); |
||
695 | return ipc_answer(callid, call); |
||
696 | } |
||
697 | |||
1866 | jermar | 698 | /** @} |
1653 | cejka | 699 | */ |