source: mainline/uspace/lib/c/generic/ipc.c@ 1a23f6e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1a23f6e was 6aae539d, checked in by Martin Decky <martin@…>, 13 years ago

rename IPC_M_CONNECT_ME to IPC_M_CLONE_ESTABLISH

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