source: mainline/uspace/lib/c/generic/ipc.c@ 7907cf9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7907cf9 was 228e490, checked in by Martin Decky <martin@…>, 15 years ago

initial modifications for supporting declarative IPC interfaces

  • Property mode set to 100644
File size: 25.8 KB
RevLine 
[b419162]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[b419162]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[b2951e2]27 */
28
29/** @addtogroup libc
30 * @{
31 * @}
32 */
33
[fadd381]34/** @addtogroup libcipc IPC
[b2951e2]35 * @brief HelenOS uspace IPC
36 * @{
37 * @ingroup libc
38 */
39/** @file
[6b10dab]40 */
[b419162]41
[7ee6aff]42#include <ipc/ipc.h>
[b419162]43#include <libc.h>
[936351c1]44#include <malloc.h>
45#include <errno.h>
[d9c8c81]46#include <adt/list.h>
[936351c1]47#include <stdio.h>
48#include <unistd.h>
[35509652]49#include <futex.h>
[04a73cdf]50#include <kernel/synch/synch.h>
[fc42b28]51#include <async.h>
[bc1f1c2]52#include <fibril.h>
[183b4a0]53#include <assert.h>
[b419162]54
[36c9234]55/**
56 * Structures of this type are used for keeping track of sent asynchronous calls
57 * and queing unsent calls.
[936351c1]58 */
59typedef struct {
60 link_t list;
61
62 ipc_async_callback_t callback;
63 void *private;
64 union {
65 ipc_callid_t callid;
66 struct {
[4c61e60]67 ipc_call_t data;
[936351c1]68 int phoneid;
69 } msg;
[8b243f2]70 } u;
[bc1f1c2]71 fid_t fid; /**< Fibril waiting for sending this call. */
[936351c1]72} async_call_t;
73
74LIST_INITIALIZE(dispatched_calls);
[fc42b28]75
[8b243f2]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.
[fc42b28]80 */
[8b243f2]81LIST_INITIALIZE(queued_calls);
[936351c1]82
[80649a91]83static atomic_t ipc_futex = FUTEX_INITIALIZER;
[35509652]84
[8b243f2]85/** Make a fast synchronous call.
86 *
[2e51969]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.
[8b243f2]90 *
91 * @param phoneid Phone handle for the call.
92 * @param method Requested method.
93 * @param arg1 Service-defined payload argument.
[2e51969]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.
[8b243f2]101 *
102 * @return Negative values represent errors returned by IPC.
103 * Otherwise the RETVAL of the answer is returned.
104 */
[2e51969]105int
[96b02eb9]106ipc_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)
[b419162]109{
[4c61e60]110 ipc_call_t resdata;
[06502f7d]111 int callres;
112
[2e51969]113 callres = __SYSCALL6(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1,
114 arg2, arg3, (sysarg_t) &resdata);
[06502f7d]115 if (callres)
116 return callres;
[2e51969]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
[06502f7d]128 return IPC_GET_RETVAL(resdata);
[b419162]129}
130
[2e51969]131/** Make a synchronous call transmitting 5 arguments of payload.
[8b243f2]132 *
[228e490]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 *
[8b243f2]149 */
[2e51969]150int
[228e490]151ipc_call_sync_slow(int phoneid, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
[96b02eb9]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)
[06502f7d]154{
[4c61e60]155 ipc_call_t data;
[228e490]156
157 IPC_SET_IMETHOD(data, imethod);
[06502f7d]158 IPC_SET_ARG1(data, arg1);
159 IPC_SET_ARG2(data, arg2);
160 IPC_SET_ARG3(data, arg3);
[2e51969]161 IPC_SET_ARG4(data, arg4);
162 IPC_SET_ARG5(data, arg5);
[228e490]163
164 int callres = __SYSCALL3(SYS_IPC_CALL_SYNC_SLOW, phoneid,
165 (sysarg_t) &data, (sysarg_t) &data);
[06502f7d]166 if (callres)
167 return callres;
[228e490]168
[06502f7d]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);
[2e51969]175 if (result4)
176 *result4 = IPC_GET_ARG4(data);
177 if (result5)
178 *result5 = IPC_GET_ARG5(data);
[228e490]179
[06502f7d]180 return IPC_GET_RETVAL(data);
181}
182
[8b243f2]183/** Syscall to send asynchronous message.
184 *
[228e490]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.
[8b243f2]189 *
190 */
191static ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data)
[936351c1]192{
[3209923]193 return __SYSCALL2(SYS_IPC_CALL_ASYNC_SLOW, phoneid, (sysarg_t) data);
[936351c1]194}
195
[8b243f2]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 */
203static inline async_call_t *ipc_prepare_async(void *private,
204 ipc_async_callback_t callback)
[b419162]205{
[936351c1]206 async_call_t *call;
[06502f7d]207
[936351c1]208 call = malloc(sizeof(*call));
209 if (!call) {
[a784a96]210 if (callback)
211 callback(private, ENOMEM, NULL);
[c1d2c9d]212 return NULL;
[936351c1]213 }
[fc42b28]214 call->callback = callback;
215 call->private = private;
216
[c1d2c9d]217 return call;
218}
219
[8b243f2]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().
[c3b25510]225 * @param can_preempt If non-zero, the current fibril can be preempted in this
226 * call.
[8b243f2]227 */
228static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
229 async_call_t *call, int can_preempt)
[c1d2c9d]230{
[d8b42fb2]231 if (!call) { /* Nothing to do regardless if failed or not */
232 futex_up(&ipc_futex);
233 return;
234 }
235
[b78d0bd]236 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
[fc42b28]237 futex_up(&ipc_futex);
[06502f7d]238 /* Call asynchronous handler with error code */
[c1d2c9d]239 if (call->callback)
240 call->callback(call->private, ENOENT, NULL);
[936351c1]241 free(call);
[06502f7d]242 return;
243 }
[936351c1]244
[b78d0bd]245 if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
[fc42b28]246 futex_up(&ipc_futex);
247
[936351c1]248 call->u.msg.phoneid = phoneid;
[b1f51f0]249
[fc42b28]250 futex_down(&async_futex);
[936351c1]251 list_append(&call->list, &queued_calls);
[fc42b28]252
[b1f51f0]253 if (can_preempt) {
[bc1f1c2]254 call->fid = fibril_get_id();
[116d3f6f]255 fibril_switch(FIBRIL_TO_MANAGER);
[b1f51f0]256 /* Async futex unlocked by previous call */
257 } else {
[bc1f1c2]258 call->fid = 0;
[b1f51f0]259 futex_up(&async_futex);
260 }
[06502f7d]261 return;
262 }
[936351c1]263 call->u.callid = callid;
[8b243f2]264 /* Add call to the list of dispatched calls */
[936351c1]265 list_append(&call->list, &dispatched_calls);
[35509652]266 futex_up(&ipc_futex);
[c1d2c9d]267
268}
269
[8b243f2]270/** Make a fast asynchronous call.
271 *
[3209923]272 * This function can only handle four arguments of payload. It is, however,
273 * faster than the more generic ipc_call_async_slow().
[8b243f2]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.
[c1d2c9d]279 *
[228e490]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 *
[c1d2c9d]292 */
[228e490]293void ipc_call_async_fast(int phoneid, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]294 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, void *private,
[3209923]295 ipc_async_callback_t callback, int can_preempt)
[c1d2c9d]296{
[d8b42fb2]297 async_call_t *call = NULL;
[228e490]298
[d8b42fb2]299 if (callback) {
300 call = ipc_prepare_async(private, callback);
301 if (!call)
302 return;
303 }
[228e490]304
[8b243f2]305 /*
306 * We need to make sure that we get callid before another thread
307 * accesses the queue again.
308 */
[c1d2c9d]309 futex_down(&ipc_futex);
[228e490]310 ipc_callid_t callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid,
311 imethod, arg1, arg2, arg3, arg4);
312
[b78d0bd]313 if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
[d8b42fb2]314 if (!call) {
315 call = ipc_prepare_async(private, callback);
316 if (!call)
317 return;
318 }
[228e490]319 IPC_SET_IMETHOD(call->u.msg.data, imethod);
[c1d2c9d]320 IPC_SET_ARG1(call->u.msg.data, arg1);
321 IPC_SET_ARG2(call->u.msg.data, arg2);
[3209923]322 IPC_SET_ARG3(call->u.msg.data, arg3);
323 IPC_SET_ARG4(call->u.msg.data, arg4);
[8498915]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);
[c1d2c9d]329 }
[b1f51f0]330 ipc_finish_async(callid, phoneid, call, can_preempt);
[c1d2c9d]331}
332
[8b243f2]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 *
[228e490]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.
[c1d2c9d]352 *
353 */
[228e490]354void ipc_call_async_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]355 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
[3209923]356 ipc_async_callback_t callback, int can_preempt)
[c1d2c9d]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
[228e490]365 IPC_SET_IMETHOD(call->u.msg.data, imethod);
[c1d2c9d]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);
[3209923]369 IPC_SET_ARG4(call->u.msg.data, arg4);
370 IPC_SET_ARG5(call->u.msg.data, arg5);
[8b243f2]371 /*
[3209923]372 * We need to make sure that we get callid before another thread
373 * accesses the queue again.
[8b243f2]374 */
[c1d2c9d]375 futex_down(&ipc_futex);
376 callid = _ipc_call_async(phoneid, &call->u.msg.data);
377
[b1f51f0]378 ipc_finish_async(callid, phoneid, call, can_preempt);
[b419162]379}
380
[06502f7d]381
[8b243f2]382/** Answer a received call - fast version.
[250717cc]383 *
[b74959bd]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.
[250717cc]386 *
[8b243f2]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.
[b74959bd]391 * @param arg3 Third return argument.
392 * @param arg4 Fourth return argument.
[250717cc]393 *
[8b243f2]394 * @return Zero on success or a value from @ref errno.h on failure.
[250717cc]395 */
[96b02eb9]396sysarg_t ipc_answer_fast(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
397 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[b419162]398{
[b74959bd]399 return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
400 arg4);
[b419162]401}
[06502f7d]402
[b74959bd]403/** Answer a received call - slow full version.
[250717cc]404 *
[8b243f2]405 * @param callid Hash of the call being answered.
[b74959bd]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.
[250717cc]412 *
[8b243f2]413 * @return Zero on success or a value from @ref errno.h on failure.
[250717cc]414 */
[96b02eb9]415sysarg_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)
[250717cc]417{
[b74959bd]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);
[250717cc]428}
429
430
[8b243f2]431/** Try to dispatch queued calls from the async queue. */
[936351c1]432static void try_dispatch_queued_calls(void)
433{
434 async_call_t *call;
435 ipc_callid_t callid;
436
[8b243f2]437 /** @todo
438 * Integrate intelligently ipc_futex, so that it is locked during
439 * ipc_call_async_*(), until it is added to dispatched_calls.
[fc42b28]440 */
441 futex_down(&async_futex);
[936351c1]442 while (!list_empty(&queued_calls)) {
[8b243f2]443 call = list_get_instance(queued_calls.next, async_call_t, list);
[8498915]444 callid = _ipc_call_async(call->u.msg.phoneid,
445 &call->u.msg.data);
[b78d0bd]446 if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
[936351c1]447 break;
[fc42b28]448 }
[936351c1]449 list_remove(&call->list);
[35509652]450
[fc42b28]451 futex_up(&async_futex);
[bc1f1c2]452 if (call->fid)
453 fibril_add_ready(call->fid);
[fc42b28]454
[b78d0bd]455 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
[a784a96]456 if (call->callback)
457 call->callback(call->private, ENOENT, NULL);
[936351c1]458 free(call);
459 } else {
460 call->u.callid = callid;
[fc42b28]461 futex_down(&ipc_futex);
[936351c1]462 list_append(&call->list, &dispatched_calls);
[fc42b28]463 futex_up(&ipc_futex);
[936351c1]464 }
[fc42b28]465 futex_down(&async_futex);
[936351c1]466 }
[fc42b28]467 futex_up(&async_futex);
[936351c1]468}
469
[8b243f2]470/** Handle a received answer.
[936351c1]471 *
[8b243f2]472 * Find the hash of the answer and call the answer callback.
[936351c1]473 *
[8b243f2]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.
[936351c1]480 */
[4c61e60]481static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
[936351c1]482{
483 link_t *item;
484 async_call_t *call;
485
486 callid &= ~IPC_CALLID_ANSWERED;
487
[35509652]488 futex_down(&ipc_futex);
[936351c1]489 for (item = dispatched_calls.next; item != &dispatched_calls;
[8b243f2]490 item = item->next) {
[936351c1]491 call = list_get_instance(item, async_call_t, list);
492 if (call->u.callid == callid) {
493 list_remove(&call->list);
[35509652]494 futex_up(&ipc_futex);
[a784a96]495 if (call->callback)
496 call->callback(call->private,
[8b243f2]497 IPC_GET_RETVAL(*data), data);
[a784a96]498 free(call);
[936351c1]499 return;
500 }
501 }
[35509652]502 futex_up(&ipc_futex);
[936351c1]503}
504
505
[8b243f2]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).
[b419162]511 *
[8b243f2]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 *
[b419162]516 */
[80649a91]517ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags)
[b419162]518{
519 ipc_callid_t callid;
520
[80649a91]521 callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
522 /* Handle received answers */
[fc42b28]523 if (callid & IPC_CALLID_ANSWERED) {
[80649a91]524 handle_answer(callid, call);
[fc42b28]525 try_dispatch_queued_calls();
526 }
[04a73cdf]527
528 return callid;
529}
530
531/** Wait some time for an IPC call.
532 *
[8b243f2]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.
[096ba7a]537 *
[8b243f2]538 * @return Hash of the answer.
[04a73cdf]539 */
540ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec)
541{
542 ipc_callid_t callid;
543
544 do {
[2d22049]545 callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
[04a73cdf]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 *
[8b243f2]553 * @param call Storage where the incoming call will be stored.
554 * @return Hash of the answer.
[04a73cdf]555 */
556ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
557{
558 ipc_callid_t callid;
559
560 do {
[8b243f2]561 callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
562 SYNCH_FLAGS_NON_BLOCKING);
[06502f7d]563 } while (callid & IPC_CALLID_ANSWERED);
[936351c1]564
[b419162]565 return callid;
566}
[5106e98]567
[057d21a]568/** Interrupt one thread of this task from waiting for IPC. */
569void ipc_poke(void)
570{
571 __SYSCALL0(SYS_IPC_POKE);
572}
573
[51ec40f]574/** Ask destination to do a callback connection.
[4c61e60]575 *
[8b243f2]576 * @param phoneid Phone handle used for contacting the other side.
577 * @param arg1 Service-defined argument.
578 * @param arg2 Service-defined argument.
[38c706cc]579 * @param arg3 Service-defined argument.
[8b243f2]580 * @param phonehash Storage where the library will store an opaque
[51ec40f]581 * identifier of the phone that will be used for incoming
[8b243f2]582 * calls. This identifier can be used for connection
583 * tracking.
584 *
585 * @return Zero on success or a negative error code.
[4c61e60]586 */
[38c706cc]587int ipc_connect_to_me(int phoneid, int arg1, int arg2, int arg3,
[96b02eb9]588 sysarg_t *phonehash)
[5106e98]589{
[38c706cc]590 return ipc_call_sync_3_5(phoneid, IPC_M_CONNECT_TO_ME, arg1, arg2,
591 arg3, NULL, NULL, NULL, NULL, phonehash);
[5106e98]592}
[11eae82]593
[51ec40f]594/** Ask through phone for a new connection to some service.
[4c61e60]595 *
[8b243f2]596 * @param phoneid Phone handle used for contacting the other side.
[51ec40f]597 * @param arg1 User defined argument.
598 * @param arg2 User defined argument.
[b61d47d]599 * @param arg3 User defined argument.
[51ec40f]600 *
[8b243f2]601 * @return New phone handle on success or a negative error code.
[4c61e60]602 */
[b61d47d]603int ipc_connect_me_to(int phoneid, int arg1, int arg2, int arg3)
[11eae82]604{
[96b02eb9]605 sysarg_t newphid;
[4c61e60]606 int res;
607
[19b28b0]608 res = ipc_call_sync_3_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
[90c35436]609 NULL, NULL, NULL, NULL, &newphid);
[4c61e60]610 if (res)
611 return res;
612 return newphid;
[11eae82]613}
614
[19b28b0]615/** Ask through phone for a new connection to some service.
616 *
617 * If the connection is not available at the moment, the
618 * call will block.
619 *
620 * @param phoneid Phone handle used for contacting the other side.
621 * @param arg1 User defined argument.
622 * @param arg2 User defined argument.
623 * @param arg3 User defined argument.
624 *
625 * @return New phone handle on success or a negative error code.
626 */
627int ipc_connect_me_to_blocking(int phoneid, int arg1, int arg2, int arg3)
628{
[96b02eb9]629 sysarg_t newphid;
[19b28b0]630 int res;
631
632 res = ipc_call_sync_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
633 IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
634 if (res)
635 return res;
636 return newphid;
637}
638
[8b243f2]639/** Hang up a phone.
640 *
641 * @param phoneid Handle of the phone to be hung up.
642 *
643 * @return Zero on success or a negative error code.
644 */
[7048773]645int ipc_hangup(int phoneid)
646{
647 return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
648}
[6180b57]649
[2b017ba]650/** Register IRQ notification.
651 *
[8b243f2]652 * @param inr IRQ number.
653 * @param devno Device number of the device generating inr.
654 * @param method Use this method for notifying me.
655 * @param ucode Top-half pseudocode handler.
[2b017ba]656 *
[8b243f2]657 * @return Value returned by the kernel.
[2b017ba]658 */
659int ipc_register_irq(int inr, int devno, int method, irq_code_t *ucode)
[6180b57]660{
[8b243f2]661 return __SYSCALL4(SYS_IPC_REGISTER_IRQ, inr, devno, method,
662 (sysarg_t) ucode);
[6180b57]663}
664
[2b017ba]665/** Unregister IRQ notification.
666 *
[8b243f2]667 * @param inr IRQ number.
668 * @param devno Device number of the device generating inr.
[2b017ba]669 *
[8b243f2]670 * @return Value returned by the kernel.
[2b017ba]671 */
672int ipc_unregister_irq(int inr, int devno)
[6180b57]673{
[2b017ba]674 return __SYSCALL2(SYS_IPC_UNREGISTER_IRQ, inr, devno);
[6180b57]675}
[8a568e3]676
[8b243f2]677/** Forward a received call to another destination.
678 *
[228e490]679 * @param callid Hash of the call to forward.
680 * @param phoneid Phone handle to use for forwarding.
681 * @param imethod New interface and method for the forwarded call.
682 * @param arg1 New value of the first argument for the forwarded call.
683 * @param arg2 New value of the second argument for the forwarded call.
684 * @param mode Flags specifying mode of the forward operation.
[8b243f2]685 *
[228e490]686 * @return Zero on success or an error code.
[8b243f2]687 *
[90c35436]688 * For non-system methods, the old method, arg1 and arg2 are rewritten by the
689 * new values. For system methods, the new method, arg1 and arg2 are written
[b61d47d]690 * to the old arg1, arg2 and arg3, respectivelly. Calls with immutable
691 * methods are forwarded verbatim.
[8b243f2]692 */
[228e490]693int ipc_forward_fast(ipc_callid_t callid, int phoneid, int imethod,
[96b02eb9]694 sysarg_t arg1, sysarg_t arg2, int mode)
[043dcc27]695{
[228e490]696 return __SYSCALL6(SYS_IPC_FORWARD_FAST, callid, phoneid, imethod, arg1,
[90c35436]697 arg2, mode);
[043dcc27]698}
699
[48daf64]700
[228e490]701int ipc_forward_slow(ipc_callid_t callid, int phoneid, int imethod,
[96b02eb9]702 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
[48daf64]703 int mode)
704{
705 ipc_call_t data;
[228e490]706
707 IPC_SET_IMETHOD(data, imethod);
[48daf64]708 IPC_SET_ARG1(data, arg1);
709 IPC_SET_ARG2(data, arg2);
710 IPC_SET_ARG3(data, arg3);
711 IPC_SET_ARG4(data, arg4);
712 IPC_SET_ARG5(data, arg5);
[228e490]713
[5b9d80c5]714 return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data, mode);
[48daf64]715}
716
[27d293a]717/** Wrapper for making IPC_M_SHARE_IN calls.
718 *
719 * @param phoneid Phone that will be used to contact the receiving side.
720 * @param dst Destination address space area base.
721 * @param size Size of the destination address space area.
722 * @param arg User defined argument.
723 * @param flags Storage where the received flags will be stored. Can be
724 * NULL.
725 *
726 * @return Zero on success or a negative error code from errno.h.
727 */
[96b02eb9]728int ipc_share_in_start(int phoneid, void *dst, size_t size, sysarg_t arg,
[27d293a]729 int *flags)
730{
[36e9cd1]731 sysarg_t tmp_flags = 0;
[96b02eb9]732 int res = ipc_call_sync_3_2(phoneid, IPC_M_SHARE_IN, (sysarg_t) dst,
733 (sysarg_t) size, arg, NULL, &tmp_flags);
[36e9cd1]734
[27d293a]735 if (flags)
736 *flags = tmp_flags;
[36e9cd1]737
[27d293a]738 return res;
739}
740
741/** Wrapper for answering the IPC_M_SHARE_IN calls.
742 *
743 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ 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_READ call to answer.
747 * @param src Source address space base.
748 * @param flags Flags to be used for sharing. Bits can be only cleared.
749 *
750 * @return Zero on success or a value from @ref errno.h on failure.
751 */
[215e375]752int ipc_share_in_finalize(ipc_callid_t callid, void *src, int flags)
[27d293a]753{
[96b02eb9]754 return ipc_answer_2(callid, EOK, (sysarg_t) src, (sysarg_t) flags);
[27d293a]755}
756
757/** Wrapper for making IPC_M_SHARE_OUT calls.
758 *
759 * @param phoneid Phone that will be used to contact the receiving side.
760 * @param src Source address space area base address.
761 * @param flags Flags to be used for sharing. Bits can be only cleared.
762 *
763 * @return Zero on success or a negative error code from errno.h.
764 */
[215e375]765int ipc_share_out_start(int phoneid, void *src, int flags)
[27d293a]766{
[96b02eb9]767 return ipc_call_sync_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
768 (sysarg_t) flags);
[27d293a]769}
770
771/** Wrapper for answering the IPC_M_SHARE_OUT calls.
772 *
773 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT 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_WRITE call to answer.
777 * @param dst Destination address space area base address.
778 *
779 * @return Zero on success or a value from @ref errno.h on failure.
780 */
[215e375]781int ipc_share_out_finalize(ipc_callid_t callid, void *dst)
[27d293a]782{
[96b02eb9]783 return ipc_answer_1(callid, EOK, (sysarg_t) dst);
[27d293a]784}
785
786
[a55d5f9f]787/** Wrapper for making IPC_M_DATA_READ calls.
788 *
789 * @param phoneid Phone that will be used to contact the receiving side.
790 * @param dst Address of the beginning of the destination buffer.
791 * @param size Size of the destination buffer.
792 *
793 * @return Zero on success or a negative error code from errno.h.
794 */
[215e375]795int ipc_data_read_start(int phoneid, void *dst, size_t size)
[a55d5f9f]796{
[96b02eb9]797 return ipc_call_sync_2_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
798 (sysarg_t) size);
[a55d5f9f]799}
800
801/** Wrapper for answering the IPC_M_DATA_READ calls.
802 *
803 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
804 * so that the user doesn't have to remember the meaning of each IPC argument.
805 *
806 * @param callid Hash of the IPC_M_DATA_READ call to answer.
807 * @param src Source address for the IPC_M_DATA_READ call.
808 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
809 * the maximum size announced by the sender.
810 *
811 * @return Zero on success or a value from @ref errno.h on failure.
812 */
[c61d34b]813int ipc_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
[a55d5f9f]814{
[96b02eb9]815 return ipc_answer_2(callid, EOK, (sysarg_t) src, (sysarg_t) size);
[a55d5f9f]816}
817
[36d852c]818/** Wrapper for making IPC_M_DATA_WRITE calls.
[183b4a0]819 *
[26f2af0]820 * @param phoneid Phone that will be used to contact the receiving side.
821 * @param src Address of the beginning of the source buffer.
822 * @param size Size of the source buffer.
823 *
824 * @return Zero on success or a negative error code from errno.h.
825 */
[c61d34b]826int ipc_data_write_start(int phoneid, const void *src, size_t size)
[26f2af0]827{
[96b02eb9]828 return ipc_call_sync_2_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
829 (sysarg_t) size);
[26f2af0]830}
831
[36d852c]832/** Wrapper for answering the IPC_M_DATA_WRITE calls.
[183b4a0]833 *
[36d852c]834 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE calls
[183b4a0]835 * so that the user doesn't have to remember the meaning of each IPC argument.
836 *
[36d852c]837 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
838 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
839 * @param size Final size for the IPC_M_DATA_WRITE call.
[183b4a0]840 *
841 * @return Zero on success or a value from @ref errno.h on failure.
842 */
[215e375]843int ipc_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
[183b4a0]844{
[96b02eb9]845 return ipc_answer_2(callid, EOK, (sysarg_t) dst, (sysarg_t) size);
[183b4a0]846}
[9a1b20c]847
848/** Connect to a task specified by id.
[6b10dab]849 *
[9a1b20c]850 */
851int ipc_connect_kbox(task_id_t id)
852{
[6b10dab]853#ifdef __32_BITS__
854 sysarg64_t arg = (sysarg64_t) id;
[9a1b20c]855 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
[6b10dab]856#endif
857
858#ifdef __64_BITS__
859 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
860#endif
[9a1b20c]861}
[6b10dab]862
[fadd381]863/** @}
[b2951e2]864 */
Note: See TracBrowser for help on using the repository browser.