1 | /*
|
---|
2 | * Copyright (c) 2006 Ondrej 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.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | * @}
|
---|
32 | */
|
---|
33 |
|
---|
34 | /** @addtogroup libcipc IPC
|
---|
35 | * @brief HelenOS uspace IPC
|
---|
36 | * @{
|
---|
37 | * @ingroup libc
|
---|
38 | */
|
---|
39 | /** @file
|
---|
40 | */
|
---|
41 |
|
---|
42 | #include <ipc/ipc.h>
|
---|
43 | #include <libc.h>
|
---|
44 | #include <malloc.h>
|
---|
45 | #include <errno.h>
|
---|
46 | #include <adt/list.h>
|
---|
47 | #include <futex.h>
|
---|
48 | #include <fibril.h>
|
---|
49 | #include <macros.h>
|
---|
50 | #include "private/libc.h"
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Structures of this type are used for keeping track
|
---|
54 | * of sent asynchronous calls and queing unsent calls.
|
---|
55 | */
|
---|
56 | typedef struct {
|
---|
57 | link_t list;
|
---|
58 |
|
---|
59 | ipc_async_callback_t callback;
|
---|
60 | void *private;
|
---|
61 |
|
---|
62 | union {
|
---|
63 | ipc_callid_t callid;
|
---|
64 | struct {
|
---|
65 | ipc_call_t data;
|
---|
66 | int phoneid;
|
---|
67 | } msg;
|
---|
68 | } u;
|
---|
69 |
|
---|
70 | /** Fibril waiting for sending this call. */
|
---|
71 | fid_t fid;
|
---|
72 | } async_call_t;
|
---|
73 |
|
---|
74 | LIST_INITIALIZE(dispatched_calls);
|
---|
75 |
|
---|
76 | /** List of asynchronous calls that were not accepted by kernel.
|
---|
77 | *
|
---|
78 | * Protected by async_futex, because if the call is not accepted
|
---|
79 | * by the kernel, the async framework is used automatically.
|
---|
80 | *
|
---|
81 | */
|
---|
82 | LIST_INITIALIZE(queued_calls);
|
---|
83 |
|
---|
84 | static atomic_t ipc_futex = FUTEX_INITIALIZER;
|
---|
85 |
|
---|
86 | /** Fast synchronous call.
|
---|
87 | *
|
---|
88 | * Only three payload arguments can be passed using this function. However,
|
---|
89 | * this function is faster than the generic ipc_call_sync_slow() because
|
---|
90 | * the payload is passed directly in registers.
|
---|
91 | *
|
---|
92 | * @param phoneid Phone handle for the call.
|
---|
93 | * @param method Requested method.
|
---|
94 | * @param arg1 Service-defined payload argument.
|
---|
95 | * @param arg2 Service-defined payload argument.
|
---|
96 | * @param arg3 Service-defined payload argument.
|
---|
97 | * @param result1 If non-NULL, the return ARG1 will be stored there.
|
---|
98 | * @param result2 If non-NULL, the return ARG2 will be stored there.
|
---|
99 | * @param result3 If non-NULL, the return ARG3 will be stored there.
|
---|
100 | * @param result4 If non-NULL, the return ARG4 will be stored there.
|
---|
101 | * @param result5 If non-NULL, the return ARG5 will be stored there.
|
---|
102 | *
|
---|
103 | * @return Negative values representing IPC errors.
|
---|
104 | * @return Otherwise the RETVAL of the answer.
|
---|
105 | *
|
---|
106 | */
|
---|
107 | int ipc_call_sync_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
108 | sysarg_t arg2, sysarg_t arg3, sysarg_t *result1, sysarg_t *result2,
|
---|
109 | sysarg_t *result3, sysarg_t *result4, sysarg_t *result5)
|
---|
110 | {
|
---|
111 | ipc_call_t resdata;
|
---|
112 | int callres = __SYSCALL6(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1,
|
---|
113 | arg2, arg3, (sysarg_t) &resdata);
|
---|
114 | if (callres)
|
---|
115 | return callres;
|
---|
116 |
|
---|
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 |
|
---|
128 | return IPC_GET_RETVAL(resdata);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Synchronous call transmitting 5 arguments of payload.
|
---|
132 | *
|
---|
133 | * @param phoneid Phone handle for the call.
|
---|
134 | * @param imethod Requested interface and method.
|
---|
135 | * @param arg1 Service-defined payload argument.
|
---|
136 | * @param arg2 Service-defined payload argument.
|
---|
137 | * @param arg3 Service-defined payload argument.
|
---|
138 | * @param arg4 Service-defined payload argument.
|
---|
139 | * @param arg5 Service-defined payload argument.
|
---|
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.
|
---|
143 | * @param result4 If non-NULL, storage for the fourth return argument.
|
---|
144 | * @param result5 If non-NULL, storage for the fifth return argument.
|
---|
145 | *
|
---|
146 | * @return Negative values representing IPC errors.
|
---|
147 | * @return Otherwise the RETVAL of the answer.
|
---|
148 | *
|
---|
149 | */
|
---|
150 | int ipc_call_sync_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
|
---|
151 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
---|
152 | sysarg_t *result1, sysarg_t *result2, sysarg_t *result3, sysarg_t *result4,
|
---|
153 | sysarg_t *result5)
|
---|
154 | {
|
---|
155 | ipc_call_t data;
|
---|
156 |
|
---|
157 | IPC_SET_IMETHOD(data, imethod);
|
---|
158 | IPC_SET_ARG1(data, arg1);
|
---|
159 | IPC_SET_ARG2(data, arg2);
|
---|
160 | IPC_SET_ARG3(data, arg3);
|
---|
161 | IPC_SET_ARG4(data, arg4);
|
---|
162 | IPC_SET_ARG5(data, arg5);
|
---|
163 |
|
---|
164 | int callres = __SYSCALL3(SYS_IPC_CALL_SYNC_SLOW, phoneid,
|
---|
165 | (sysarg_t) &data, (sysarg_t) &data);
|
---|
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);
|
---|
175 | if (result4)
|
---|
176 | *result4 = IPC_GET_ARG4(data);
|
---|
177 | if (result5)
|
---|
178 | *result5 = IPC_GET_ARG5(data);
|
---|
179 |
|
---|
180 | return IPC_GET_RETVAL(data);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** Send asynchronous message via syscall.
|
---|
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 | */
|
---|
191 | static ipc_callid_t ipc_call_async_internal(int phoneid, ipc_call_t *data)
|
---|
192 | {
|
---|
193 | return __SYSCALL2(SYS_IPC_CALL_ASYNC_SLOW, phoneid, (sysarg_t) data);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** Prolog for ipc_call_async_*() functions.
|
---|
197 | *
|
---|
198 | * @param private Argument for the answer/error callback.
|
---|
199 | * @param callback Answer/error callback.
|
---|
200 | *
|
---|
201 | * @return New, partially initialized async_call structure or NULL.
|
---|
202 | *
|
---|
203 | */
|
---|
204 | static inline async_call_t *ipc_prepare_async(void *private,
|
---|
205 | ipc_async_callback_t callback)
|
---|
206 | {
|
---|
207 | async_call_t *call =
|
---|
208 | (async_call_t *) malloc(sizeof(async_call_t));
|
---|
209 | if (!call) {
|
---|
210 | if (callback)
|
---|
211 | callback(private, ENOMEM, NULL);
|
---|
212 |
|
---|
213 | return NULL;
|
---|
214 | }
|
---|
215 |
|
---|
216 | call->callback = callback;
|
---|
217 | call->private = private;
|
---|
218 |
|
---|
219 | return call;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /** Epilog for ipc_call_async_*() functions.
|
---|
223 | *
|
---|
224 | * @param callid Value returned by the SYS_IPC_CALL_ASYNC_* syscall.
|
---|
225 | * @param phoneid Phone handle through which the call was made.
|
---|
226 | * @param call Structure returned by ipc_prepare_async().
|
---|
227 | * @param can_preempt If true, the current fibril can be preempted
|
---|
228 | * in this call.
|
---|
229 | *
|
---|
230 | */
|
---|
231 | static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
|
---|
232 | async_call_t *call, bool can_preempt)
|
---|
233 | {
|
---|
234 | if (!call) {
|
---|
235 | /* Nothing to do regardless if failed or not */
|
---|
236 | futex_up(&ipc_futex);
|
---|
237 | return;
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
|
---|
241 | futex_up(&ipc_futex);
|
---|
242 |
|
---|
243 | /* Call asynchronous handler with error code */
|
---|
244 | if (call->callback)
|
---|
245 | call->callback(call->private, ENOENT, NULL);
|
---|
246 |
|
---|
247 | free(call);
|
---|
248 | return;
|
---|
249 | }
|
---|
250 |
|
---|
251 | if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
|
---|
252 | futex_up(&ipc_futex);
|
---|
253 |
|
---|
254 | call->u.msg.phoneid = phoneid;
|
---|
255 |
|
---|
256 | futex_down(&async_futex);
|
---|
257 | list_append(&call->list, &queued_calls);
|
---|
258 |
|
---|
259 | if (can_preempt) {
|
---|
260 | call->fid = fibril_get_id();
|
---|
261 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
262 | /* Async futex unlocked by previous call */
|
---|
263 | } else {
|
---|
264 | call->fid = 0;
|
---|
265 | futex_up(&async_futex);
|
---|
266 | }
|
---|
267 |
|
---|
268 | return;
|
---|
269 | }
|
---|
270 |
|
---|
271 | call->u.callid = callid;
|
---|
272 |
|
---|
273 | /* Add call to the list of dispatched calls */
|
---|
274 | list_append(&call->list, &dispatched_calls);
|
---|
275 | futex_up(&ipc_futex);
|
---|
276 | }
|
---|
277 |
|
---|
278 | /** Fast asynchronous call.
|
---|
279 | *
|
---|
280 | * This function can only handle four arguments of payload. It is, however,
|
---|
281 | * faster than the more generic ipc_call_async_slow().
|
---|
282 | *
|
---|
283 | * Note that this function is a void function.
|
---|
284 | *
|
---|
285 | * During normal operation, answering this call will trigger the callback.
|
---|
286 | * In case of fatal error, the callback handler is called with the proper
|
---|
287 | * error code. If the call cannot be temporarily made, it is queued.
|
---|
288 | *
|
---|
289 | * @param phoneid Phone handle for the call.
|
---|
290 | * @param imethod Requested interface and method.
|
---|
291 | * @param arg1 Service-defined payload argument.
|
---|
292 | * @param arg2 Service-defined payload argument.
|
---|
293 | * @param arg3 Service-defined payload argument.
|
---|
294 | * @param arg4 Service-defined payload argument.
|
---|
295 | * @param private Argument to be passed to the answer/error callback.
|
---|
296 | * @param callback Answer or error callback.
|
---|
297 | * @param can_preempt If true, the current fibril will be preempted in
|
---|
298 | * case the kernel temporarily refuses to accept more
|
---|
299 | * asynchronous calls.
|
---|
300 | *
|
---|
301 | */
|
---|
302 | void ipc_call_async_fast(int phoneid, sysarg_t imethod, sysarg_t arg1,
|
---|
303 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, void *private,
|
---|
304 | ipc_async_callback_t callback, bool can_preempt)
|
---|
305 | {
|
---|
306 | async_call_t *call = NULL;
|
---|
307 |
|
---|
308 | if (callback) {
|
---|
309 | call = ipc_prepare_async(private, callback);
|
---|
310 | if (!call)
|
---|
311 | return;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * We need to make sure that we get callid
|
---|
316 | * before another thread accesses the queue again.
|
---|
317 | */
|
---|
318 |
|
---|
319 | futex_down(&ipc_futex);
|
---|
320 | ipc_callid_t callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid,
|
---|
321 | imethod, arg1, arg2, arg3, arg4);
|
---|
322 |
|
---|
323 | if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
|
---|
324 | if (!call) {
|
---|
325 | call = ipc_prepare_async(private, callback);
|
---|
326 | if (!call)
|
---|
327 | return;
|
---|
328 | }
|
---|
329 |
|
---|
330 | IPC_SET_IMETHOD(call->u.msg.data, imethod);
|
---|
331 | IPC_SET_ARG1(call->u.msg.data, arg1);
|
---|
332 | IPC_SET_ARG2(call->u.msg.data, arg2);
|
---|
333 | IPC_SET_ARG3(call->u.msg.data, arg3);
|
---|
334 | IPC_SET_ARG4(call->u.msg.data, arg4);
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * To achieve deterministic behavior, we always zero out the
|
---|
338 | * arguments that are beyond the limits of the fast version.
|
---|
339 | */
|
---|
340 |
|
---|
341 | IPC_SET_ARG5(call->u.msg.data, 0);
|
---|
342 | }
|
---|
343 |
|
---|
344 | ipc_finish_async(callid, phoneid, call, can_preempt);
|
---|
345 | }
|
---|
346 |
|
---|
347 | /** Asynchronous call transmitting the entire payload.
|
---|
348 | *
|
---|
349 | * Note that this function is a void function.
|
---|
350 | *
|
---|
351 | * During normal operation, answering this call will trigger the callback.
|
---|
352 | * In case of fatal error, the callback handler is called with the proper
|
---|
353 | * error code. If the call cannot be temporarily made, it is queued.
|
---|
354 | *
|
---|
355 | * @param phoneid Phone handle for the call.
|
---|
356 | * @param imethod Requested interface and method.
|
---|
357 | * @param arg1 Service-defined payload argument.
|
---|
358 | * @param arg2 Service-defined payload argument.
|
---|
359 | * @param arg3 Service-defined payload argument.
|
---|
360 | * @param arg4 Service-defined payload argument.
|
---|
361 | * @param arg5 Service-defined payload argument.
|
---|
362 | * @param private Argument to be passed to the answer/error callback.
|
---|
363 | * @param callback Answer or error callback.
|
---|
364 | * @param can_preempt If true, the current fibril will be preempted in
|
---|
365 | * case the kernel temporarily refuses to accept more
|
---|
366 | * asynchronous calls.
|
---|
367 | *
|
---|
368 | */
|
---|
369 | void ipc_call_async_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
|
---|
370 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
|
---|
371 | ipc_async_callback_t callback, bool can_preempt)
|
---|
372 | {
|
---|
373 | async_call_t *call = ipc_prepare_async(private, callback);
|
---|
374 | if (!call)
|
---|
375 | return;
|
---|
376 |
|
---|
377 | IPC_SET_IMETHOD(call->u.msg.data, imethod);
|
---|
378 | IPC_SET_ARG1(call->u.msg.data, arg1);
|
---|
379 | IPC_SET_ARG2(call->u.msg.data, arg2);
|
---|
380 | IPC_SET_ARG3(call->u.msg.data, arg3);
|
---|
381 | IPC_SET_ARG4(call->u.msg.data, arg4);
|
---|
382 | IPC_SET_ARG5(call->u.msg.data, arg5);
|
---|
383 |
|
---|
384 | /*
|
---|
385 | * We need to make sure that we get callid
|
---|
386 | * before another threadaccesses the queue again.
|
---|
387 | */
|
---|
388 |
|
---|
389 | futex_down(&ipc_futex);
|
---|
390 | ipc_callid_t callid =
|
---|
391 | ipc_call_async_internal(phoneid, &call->u.msg.data);
|
---|
392 |
|
---|
393 | ipc_finish_async(callid, phoneid, call, can_preempt);
|
---|
394 | }
|
---|
395 |
|
---|
396 | /** Answer received call (fast version).
|
---|
397 | *
|
---|
398 | * The fast answer makes use of passing retval and first four arguments in
|
---|
399 | * registers. If you need to return more, use the ipc_answer_slow() instead.
|
---|
400 | *
|
---|
401 | * @param callid Hash of the call being answered.
|
---|
402 | * @param retval Return value.
|
---|
403 | * @param arg1 First return argument.
|
---|
404 | * @param arg2 Second return argument.
|
---|
405 | * @param arg3 Third return argument.
|
---|
406 | * @param arg4 Fourth return argument.
|
---|
407 | *
|
---|
408 | * @return Zero on success.
|
---|
409 | * @return Value from @ref errno.h on failure.
|
---|
410 | *
|
---|
411 | */
|
---|
412 | sysarg_t ipc_answer_fast(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
413 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
414 | {
|
---|
415 | return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
|
---|
416 | arg4);
|
---|
417 | }
|
---|
418 |
|
---|
419 | /** Answer received call (entire payload).
|
---|
420 | *
|
---|
421 | * @param callid Hash of the call being answered.
|
---|
422 | * @param retval Return value.
|
---|
423 | * @param arg1 First return argument.
|
---|
424 | * @param arg2 Second return argument.
|
---|
425 | * @param arg3 Third return argument.
|
---|
426 | * @param arg4 Fourth return argument.
|
---|
427 | * @param arg5 Fifth return argument.
|
---|
428 | *
|
---|
429 | * @return Zero on success.
|
---|
430 | * @return Value from @ref errno.h on failure.
|
---|
431 | *
|
---|
432 | */
|
---|
433 | sysarg_t ipc_answer_slow(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
434 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
435 | {
|
---|
436 | ipc_call_t data;
|
---|
437 |
|
---|
438 | IPC_SET_RETVAL(data, retval);
|
---|
439 | IPC_SET_ARG1(data, arg1);
|
---|
440 | IPC_SET_ARG2(data, arg2);
|
---|
441 | IPC_SET_ARG3(data, arg3);
|
---|
442 | IPC_SET_ARG4(data, arg4);
|
---|
443 | IPC_SET_ARG5(data, arg5);
|
---|
444 |
|
---|
445 | return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
|
---|
446 | }
|
---|
447 |
|
---|
448 | /** Try to dispatch queued calls from the async queue.
|
---|
449 | *
|
---|
450 | */
|
---|
451 | static void dispatch_queued_calls(void)
|
---|
452 | {
|
---|
453 | /** @todo
|
---|
454 | * Integrate intelligently ipc_futex so that it is locked during
|
---|
455 | * ipc_call_async_*() until it is added to dispatched_calls.
|
---|
456 | */
|
---|
457 |
|
---|
458 | futex_down(&async_futex);
|
---|
459 |
|
---|
460 | while (!list_empty(&queued_calls)) {
|
---|
461 | async_call_t *call =
|
---|
462 | list_get_instance(list_first(&queued_calls), async_call_t, list);
|
---|
463 | ipc_callid_t callid =
|
---|
464 | ipc_call_async_internal(call->u.msg.phoneid, &call->u.msg.data);
|
---|
465 |
|
---|
466 | if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY)
|
---|
467 | break;
|
---|
468 |
|
---|
469 | list_remove(&call->list);
|
---|
470 |
|
---|
471 | futex_up(&async_futex);
|
---|
472 |
|
---|
473 | if (call->fid)
|
---|
474 | fibril_add_ready(call->fid);
|
---|
475 |
|
---|
476 | if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
|
---|
477 | if (call->callback)
|
---|
478 | call->callback(call->private, ENOENT, NULL);
|
---|
479 |
|
---|
480 | free(call);
|
---|
481 | } else {
|
---|
482 | call->u.callid = callid;
|
---|
483 |
|
---|
484 | futex_down(&ipc_futex);
|
---|
485 | list_append(&call->list, &dispatched_calls);
|
---|
486 | futex_up(&ipc_futex);
|
---|
487 | }
|
---|
488 |
|
---|
489 | futex_down(&async_futex);
|
---|
490 | }
|
---|
491 |
|
---|
492 | futex_up(&async_futex);
|
---|
493 | }
|
---|
494 |
|
---|
495 | /** Handle received answer.
|
---|
496 | *
|
---|
497 | * Find the hash of the answer and call the answer callback.
|
---|
498 | *
|
---|
499 | * The answer has the same hash as the request OR'ed with
|
---|
500 | * the IPC_CALLID_ANSWERED bit.
|
---|
501 | *
|
---|
502 | * @todo Use hash table.
|
---|
503 | *
|
---|
504 | * @param callid Hash of the received answer.
|
---|
505 | * @param data Call data of the answer.
|
---|
506 | *
|
---|
507 | */
|
---|
508 | static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
|
---|
509 | {
|
---|
510 | callid &= ~IPC_CALLID_ANSWERED;
|
---|
511 |
|
---|
512 | futex_down(&ipc_futex);
|
---|
513 |
|
---|
514 | link_t *item;
|
---|
515 | for (item = dispatched_calls.head.next; item != &dispatched_calls.head;
|
---|
516 | item = item->next) {
|
---|
517 | async_call_t *call =
|
---|
518 | list_get_instance(item, async_call_t, list);
|
---|
519 |
|
---|
520 | if (call->u.callid == callid) {
|
---|
521 | list_remove(&call->list);
|
---|
522 |
|
---|
523 | futex_up(&ipc_futex);
|
---|
524 |
|
---|
525 | if (call->callback)
|
---|
526 | call->callback(call->private,
|
---|
527 | IPC_GET_RETVAL(*data), data);
|
---|
528 |
|
---|
529 | free(call);
|
---|
530 | return;
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 | futex_up(&ipc_futex);
|
---|
535 | }
|
---|
536 |
|
---|
537 | /** Wait for first IPC call to come.
|
---|
538 | *
|
---|
539 | * @param call Incoming call storage.
|
---|
540 | * @param usec Timeout in microseconds
|
---|
541 | * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
|
---|
542 | *
|
---|
543 | * @return Hash of the call. Note that certain bits have special
|
---|
544 | * meaning: IPC_CALLID_ANSWERED is set in an answer
|
---|
545 | * and IPC_CALLID_NOTIFICATION is used for notifications.
|
---|
546 | *
|
---|
547 | */
|
---|
548 | ipc_callid_t ipc_wait_cycle(ipc_call_t *call, sysarg_t usec,
|
---|
549 | unsigned int flags)
|
---|
550 | {
|
---|
551 | ipc_callid_t callid =
|
---|
552 | __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
|
---|
553 |
|
---|
554 | /* Handle received answers */
|
---|
555 | if (callid & IPC_CALLID_ANSWERED) {
|
---|
556 | handle_answer(callid, call);
|
---|
557 | dispatch_queued_calls();
|
---|
558 | }
|
---|
559 |
|
---|
560 | return callid;
|
---|
561 | }
|
---|
562 |
|
---|
563 | /** Interrupt one thread of this task from waiting for IPC.
|
---|
564 | *
|
---|
565 | */
|
---|
566 | void ipc_poke(void)
|
---|
567 | {
|
---|
568 | __SYSCALL0(SYS_IPC_POKE);
|
---|
569 | }
|
---|
570 |
|
---|
571 | /** Wait for first IPC call to come.
|
---|
572 | *
|
---|
573 | * Only requests are returned, answers are processed internally.
|
---|
574 | *
|
---|
575 | * @param call Incoming call storage.
|
---|
576 | * @param usec Timeout in microseconds
|
---|
577 | *
|
---|
578 | * @return Hash of the call.
|
---|
579 | *
|
---|
580 | */
|
---|
581 | ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, sysarg_t usec)
|
---|
582 | {
|
---|
583 | ipc_callid_t callid;
|
---|
584 |
|
---|
585 | do {
|
---|
586 | callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
|
---|
587 | } while (callid & IPC_CALLID_ANSWERED);
|
---|
588 |
|
---|
589 | return callid;
|
---|
590 | }
|
---|
591 |
|
---|
592 | /** Check if there is an IPC call waiting to be picked up.
|
---|
593 | *
|
---|
594 | * Only requests are returned, answers are processed internally.
|
---|
595 | *
|
---|
596 | * @param call Incoming call storage.
|
---|
597 | *
|
---|
598 | * @return Hash of the call.
|
---|
599 | *
|
---|
600 | */
|
---|
601 | ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
|
---|
602 | {
|
---|
603 | ipc_callid_t callid;
|
---|
604 |
|
---|
605 | do {
|
---|
606 | callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
|
---|
607 | SYNCH_FLAGS_NON_BLOCKING);
|
---|
608 | } while (callid & IPC_CALLID_ANSWERED);
|
---|
609 |
|
---|
610 | return callid;
|
---|
611 | }
|
---|
612 |
|
---|
613 | /** Request callback connection.
|
---|
614 | *
|
---|
615 | * The @a task_id and @a phonehash identifiers returned
|
---|
616 | * by the kernel can be used for connection tracking.
|
---|
617 | *
|
---|
618 | * @param phoneid Phone handle used for contacting the other side.
|
---|
619 | * @param arg1 User defined argument.
|
---|
620 | * @param arg2 User defined argument.
|
---|
621 | * @param arg3 User defined argument.
|
---|
622 | * @param task_id Identifier of the client task.
|
---|
623 | * @param phonehash Opaque identifier of the phone that will
|
---|
624 | * be used for incoming calls.
|
---|
625 | *
|
---|
626 | * @return Zero on success or a negative error code.
|
---|
627 | *
|
---|
628 | */
|
---|
629 | int ipc_connect_to_me(int phoneid, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
630 | task_id_t *task_id, sysarg_t *phonehash)
|
---|
631 | {
|
---|
632 | ipc_call_t data;
|
---|
633 | int rc = __SYSCALL6(SYS_IPC_CALL_SYNC_FAST, phoneid,
|
---|
634 | IPC_M_CONNECT_TO_ME, arg1, arg2, arg3, (sysarg_t) &data);
|
---|
635 | if (rc == EOK) {
|
---|
636 | *task_id = data.in_task_id;
|
---|
637 | *phonehash = IPC_GET_ARG5(data);
|
---|
638 | }
|
---|
639 | return rc;
|
---|
640 | }
|
---|
641 |
|
---|
642 | /** Request cloned connection.
|
---|
643 | *
|
---|
644 | * @param phoneid Phone handle used for contacting the other side.
|
---|
645 | *
|
---|
646 | * @return Cloned phone handle on success or a negative error code.
|
---|
647 | *
|
---|
648 | */
|
---|
649 | int ipc_clone_establish(int phoneid)
|
---|
650 | {
|
---|
651 | sysarg_t newphid;
|
---|
652 | int res = ipc_call_sync_0_5(phoneid, IPC_M_CLONE_ESTABLISH, NULL,
|
---|
653 | NULL, NULL, NULL, &newphid);
|
---|
654 | if (res)
|
---|
655 | return res;
|
---|
656 |
|
---|
657 | return newphid;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /** Request new connection.
|
---|
661 | *
|
---|
662 | * @param phoneid Phone handle used for contacting the other side.
|
---|
663 | * @param arg1 User defined argument.
|
---|
664 | * @param arg2 User defined argument.
|
---|
665 | * @param arg3 User defined argument.
|
---|
666 | *
|
---|
667 | * @return New phone handle on success or a negative error code.
|
---|
668 | *
|
---|
669 | */
|
---|
670 | int ipc_connect_me_to(int phoneid, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
|
---|
671 | {
|
---|
672 | sysarg_t newphid;
|
---|
673 | int res = ipc_call_sync_3_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
|
---|
674 | NULL, NULL, NULL, NULL, &newphid);
|
---|
675 | if (res)
|
---|
676 | return res;
|
---|
677 |
|
---|
678 | return newphid;
|
---|
679 | }
|
---|
680 |
|
---|
681 | /** Request new connection (blocking)
|
---|
682 | *
|
---|
683 | * If the connection is not available at the moment, the
|
---|
684 | * call should block. This has to be, however, implemented
|
---|
685 | * on the server side.
|
---|
686 | *
|
---|
687 | * @param phoneid Phone handle used for contacting the other side.
|
---|
688 | * @param arg1 User defined argument.
|
---|
689 | * @param arg2 User defined argument.
|
---|
690 | * @param arg3 User defined argument.
|
---|
691 | *
|
---|
692 | * @return New phone handle on success or a negative error code.
|
---|
693 | *
|
---|
694 | */
|
---|
695 | int ipc_connect_me_to_blocking(int phoneid, sysarg_t arg1, sysarg_t arg2,
|
---|
696 | sysarg_t arg3)
|
---|
697 | {
|
---|
698 | sysarg_t newphid;
|
---|
699 | int res = ipc_call_sync_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
|
---|
700 | IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
|
---|
701 | if (res)
|
---|
702 | return res;
|
---|
703 |
|
---|
704 | return newphid;
|
---|
705 | }
|
---|
706 |
|
---|
707 | /** Hang up a phone.
|
---|
708 | *
|
---|
709 | * @param phoneid Handle of the phone to be hung up.
|
---|
710 | *
|
---|
711 | * @return Zero on success or a negative error code.
|
---|
712 | *
|
---|
713 | */
|
---|
714 | int ipc_hangup(int phoneid)
|
---|
715 | {
|
---|
716 | return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
|
---|
717 | }
|
---|
718 |
|
---|
719 | /** Forward a received call to another destination.
|
---|
720 | *
|
---|
721 | * For non-system methods, the old method, arg1 and arg2 are rewritten
|
---|
722 | * by the new values. For system methods, the new method, arg1 and arg2
|
---|
723 | * are written to the old arg1, arg2 and arg3, respectivelly. Calls with
|
---|
724 | * immutable methods are forwarded verbatim.
|
---|
725 | *
|
---|
726 | * @param callid Hash of the call to forward.
|
---|
727 | * @param phoneid Phone handle to use for forwarding.
|
---|
728 | * @param imethod New interface and method for the forwarded call.
|
---|
729 | * @param arg1 New value of the first argument for the forwarded call.
|
---|
730 | * @param arg2 New value of the second argument for the forwarded call.
|
---|
731 | * @param mode Flags specifying mode of the forward operation.
|
---|
732 | *
|
---|
733 | * @return Zero on success or an error code.
|
---|
734 | *
|
---|
735 | */
|
---|
736 | int ipc_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
|
---|
737 | sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
738 | {
|
---|
739 | return __SYSCALL6(SYS_IPC_FORWARD_FAST, callid, phoneid, imethod, arg1,
|
---|
740 | arg2, mode);
|
---|
741 | }
|
---|
742 |
|
---|
743 | int ipc_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
|
---|
744 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
---|
745 | unsigned int mode)
|
---|
746 | {
|
---|
747 | ipc_call_t data;
|
---|
748 |
|
---|
749 | IPC_SET_IMETHOD(data, imethod);
|
---|
750 | IPC_SET_ARG1(data, arg1);
|
---|
751 | IPC_SET_ARG2(data, arg2);
|
---|
752 | IPC_SET_ARG3(data, arg3);
|
---|
753 | IPC_SET_ARG4(data, arg4);
|
---|
754 | IPC_SET_ARG5(data, arg5);
|
---|
755 |
|
---|
756 | return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data,
|
---|
757 | mode);
|
---|
758 | }
|
---|
759 |
|
---|
760 | /** Wrapper for IPC_M_SHARE_IN calls.
|
---|
761 | *
|
---|
762 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
763 | * @param size Size of the destination address space area.
|
---|
764 | * @param arg User defined argument.
|
---|
765 | * @param flags Storage for received flags. Can be NULL.
|
---|
766 | * @param dst Destination address space area base. Cannot be NULL.
|
---|
767 | *
|
---|
768 | * @return Zero on success or a negative error code from errno.h.
|
---|
769 | *
|
---|
770 | */
|
---|
771 | int ipc_share_in_start(int phoneid, size_t size, sysarg_t arg,
|
---|
772 | unsigned int *flags, void **dst)
|
---|
773 | {
|
---|
774 | sysarg_t _flags = 0;
|
---|
775 | sysarg_t _dst = (sysarg_t) -1;
|
---|
776 | int res = ipc_call_sync_2_4(phoneid, IPC_M_SHARE_IN, (sysarg_t) size,
|
---|
777 | arg, NULL, &_flags, NULL, &_dst);
|
---|
778 |
|
---|
779 | if (flags)
|
---|
780 | *flags = (unsigned int) _flags;
|
---|
781 |
|
---|
782 | *dst = (void *) _dst;
|
---|
783 | return res;
|
---|
784 | }
|
---|
785 |
|
---|
786 | /** Wrapper for answering the IPC_M_SHARE_IN calls.
|
---|
787 | *
|
---|
788 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
|
---|
789 | * calls so that the user doesn't have to remember the meaning of each
|
---|
790 | * IPC argument.
|
---|
791 | *
|
---|
792 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
793 | * @param src Source address space base.
|
---|
794 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
795 | *
|
---|
796 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
797 | *
|
---|
798 | */
|
---|
799 | int ipc_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
|
---|
800 | {
|
---|
801 | return ipc_answer_3(callid, EOK, (sysarg_t) src, (sysarg_t) flags,
|
---|
802 | (sysarg_t) __entry);
|
---|
803 | }
|
---|
804 |
|
---|
805 | /** Wrapper for IPC_M_SHARE_OUT calls.
|
---|
806 | *
|
---|
807 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
808 | * @param src Source address space area base address.
|
---|
809 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
810 | *
|
---|
811 | * @return Zero on success or a negative error code from errno.h.
|
---|
812 | *
|
---|
813 | */
|
---|
814 | int ipc_share_out_start(int phoneid, void *src, unsigned int flags)
|
---|
815 | {
|
---|
816 | return ipc_call_sync_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
|
---|
817 | (sysarg_t) flags);
|
---|
818 | }
|
---|
819 |
|
---|
820 | /** Wrapper for answering the IPC_M_SHARE_OUT calls.
|
---|
821 | *
|
---|
822 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
|
---|
823 | * calls so that the user doesn't have to remember the meaning of each
|
---|
824 | * IPC argument.
|
---|
825 | *
|
---|
826 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
827 | * @param dst Destination address space area base address.
|
---|
828 | *
|
---|
829 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
830 | *
|
---|
831 | */
|
---|
832 | int ipc_share_out_finalize(ipc_callid_t callid, void **dst)
|
---|
833 | {
|
---|
834 | return ipc_answer_2(callid, EOK, (sysarg_t) __entry, (sysarg_t) dst);
|
---|
835 | }
|
---|
836 |
|
---|
837 | /** Wrapper for IPC_M_DATA_READ calls.
|
---|
838 | *
|
---|
839 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
840 | * @param dst Address of the beginning of the destination buffer.
|
---|
841 | * @param size Size of the destination buffer.
|
---|
842 | *
|
---|
843 | * @return Zero on success or a negative error code from errno.h.
|
---|
844 | *
|
---|
845 | */
|
---|
846 | int ipc_data_read_start(int phoneid, void *dst, size_t size)
|
---|
847 | {
|
---|
848 | return ipc_call_sync_2_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
|
---|
849 | (sysarg_t) size);
|
---|
850 | }
|
---|
851 |
|
---|
852 | /** Wrapper for answering the IPC_M_DATA_READ calls.
|
---|
853 | *
|
---|
854 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
|
---|
855 | * calls so that the user doesn't have to remember the meaning of each
|
---|
856 | * IPC argument.
|
---|
857 | *
|
---|
858 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
859 | * @param src Source address for the IPC_M_DATA_READ call.
|
---|
860 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
---|
861 | * the maximum size announced by the sender.
|
---|
862 | *
|
---|
863 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
864 | *
|
---|
865 | */
|
---|
866 | int ipc_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
|
---|
867 | {
|
---|
868 | return ipc_answer_2(callid, EOK, (sysarg_t) src, (sysarg_t) size);
|
---|
869 | }
|
---|
870 |
|
---|
871 | /** Wrapper for IPC_M_DATA_WRITE calls.
|
---|
872 | *
|
---|
873 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
874 | * @param src Address of the beginning of the source buffer.
|
---|
875 | * @param size Size of the source buffer.
|
---|
876 | *
|
---|
877 | * @return Zero on success or a negative error code from errno.h.
|
---|
878 | *
|
---|
879 | */
|
---|
880 | int ipc_data_write_start(int phoneid, const void *src, size_t size)
|
---|
881 | {
|
---|
882 | return ipc_call_sync_2_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
|
---|
883 | (sysarg_t) size);
|
---|
884 | }
|
---|
885 |
|
---|
886 | /** Wrapper for answering the IPC_M_DATA_WRITE calls.
|
---|
887 | *
|
---|
888 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
|
---|
889 | * calls so that the user doesn't have to remember the meaning of each
|
---|
890 | * IPC argument.
|
---|
891 | *
|
---|
892 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
893 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
---|
894 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
---|
895 | *
|
---|
896 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
897 | *
|
---|
898 | */
|
---|
899 | int ipc_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
|
---|
900 | {
|
---|
901 | return ipc_answer_2(callid, EOK, (sysarg_t) dst, (sysarg_t) size);
|
---|
902 | }
|
---|
903 |
|
---|
904 | /** Connect to a task specified by id.
|
---|
905 | *
|
---|
906 | */
|
---|
907 | int ipc_connect_kbox(task_id_t id)
|
---|
908 | {
|
---|
909 | #ifdef __32_BITS__
|
---|
910 | sysarg64_t arg = (sysarg64_t) id;
|
---|
911 | return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
|
---|
912 | #endif
|
---|
913 |
|
---|
914 | #ifdef __64_BITS__
|
---|
915 | return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
|
---|
916 | #endif
|
---|
917 | }
|
---|
918 |
|
---|
919 | /** @}
|
---|
920 | */
|
---|