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