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