source: mainline/uspace/lib/c/generic/ipc.c@ 0eff68e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0eff68e was 79ae36dd, checked in by Martin Decky <martin@…>, 14 years ago

new async framework with integrated exchange tracking

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