source: mainline/uspace/lib/c/generic/ipc.c@ 311bc25

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 311bc25 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
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 * @}
32 */
33
34/** @addtogroup libcipc IPC
35 * @brief HelenOS uspace IPC
36 * @{
37 * @ingroup libc
38 */
39/** @file
40 */
41
42#include <ipc/ipc.h>
43#include <libc.h>
44#include <malloc.h>
45#include <errno.h>
46#include <adt/list.h>
47#include <stdio.h>
48#include <unistd.h>
49#include <futex.h>
50#include <kernel/synch/synch.h>
51#include <async.h>
52#include <fibril.h>
53#include <assert.h>
54
55/**
56 * Structures of this type are used for keeping track of sent asynchronous calls
57 * and queing unsent calls.
58 */
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 {
67 ipc_call_t data;
68 int phoneid;
69 } msg;
70 } u;
71 fid_t fid; /**< Fibril waiting for sending this call. */
72} async_call_t;
73
74LIST_INITIALIZE(dispatched_calls);
75
76/** List of asynchronous calls that were not accepted by kernel.
77 *
78 * It is protected by async_futex, because if the call cannot be sent into the
79 * kernel, the async framework is used automatically.
80 */
81LIST_INITIALIZE(queued_calls);
82
83static atomic_t ipc_futex = FUTEX_INITIALIZER;
84
85/** Make a fast synchronous call.
86 *
87 * Only three payload arguments can be passed using this function. However, this
88 * function is faster than the generic ipc_call_sync_slow() because the payload
89 * is passed directly in registers.
90 *
91 * @param phoneid Phone handle for the call.
92 * @param method Requested method.
93 * @param arg1 Service-defined payload argument.
94 * @param arg2 Service-defined payload argument.
95 * @param arg3 Service-defined payload argument.
96 * @param result1 If non-NULL, the return ARG1 will be stored there.
97 * @param result2 If non-NULL, the return ARG2 will be stored there.
98 * @param result3 If non-NULL, the return ARG3 will be stored there.
99 * @param result4 If non-NULL, the return ARG4 will be stored there.
100 * @param result5 If non-NULL, the return ARG5 will be stored there.
101 *
102 * @return Negative values represent errors returned by IPC.
103 * Otherwise the RETVAL of the answer is returned.
104 */
105int
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)
109{
110 ipc_call_t resdata;
111 int callres;
112
113 callres = __SYSCALL6(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1,
114 arg2, arg3, (sysarg_t) &resdata);
115 if (callres)
116 return callres;
117 if (result1)
118 *result1 = IPC_GET_ARG1(resdata);
119 if (result2)
120 *result2 = IPC_GET_ARG2(resdata);
121 if (result3)
122 *result3 = IPC_GET_ARG3(resdata);
123 if (result4)
124 *result4 = IPC_GET_ARG4(resdata);
125 if (result5)
126 *result5 = IPC_GET_ARG5(resdata);
127
128 return IPC_GET_RETVAL(resdata);
129}
130
131/** Make a synchronous call transmitting 5 arguments of payload.
132 *
133 * @param phoneid Phone handle for the call.
134 * @param imethod Requested interface and method.
135 * @param arg1 Service-defined payload argument.
136 * @param arg2 Service-defined payload argument.
137 * @param arg3 Service-defined payload argument.
138 * @param arg4 Service-defined payload argument.
139 * @param arg5 Service-defined payload argument.
140 * @param result1 If non-NULL, storage for the first return argument.
141 * @param result2 If non-NULL, storage for the second return argument.
142 * @param result3 If non-NULL, storage for the third return argument.
143 * @param result4 If non-NULL, storage for the fourth return argument.
144 * @param result5 If non-NULL, storage for the fifth return argument.
145 *
146 * @return Negative value means IPC error.
147 * Otherwise the RETVAL of the answer.
148 *
149 */
150int
151ipc_call_sync_slow(int phoneid, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
152 sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *result1,
153 sysarg_t *result2, sysarg_t *result3, sysarg_t *result4, sysarg_t *result5)
154{
155 ipc_call_t data;
156
157 IPC_SET_IMETHOD(data, imethod);
158 IPC_SET_ARG1(data, arg1);
159 IPC_SET_ARG2(data, arg2);
160 IPC_SET_ARG3(data, arg3);
161 IPC_SET_ARG4(data, arg4);
162 IPC_SET_ARG5(data, arg5);
163
164 int callres = __SYSCALL3(SYS_IPC_CALL_SYNC_SLOW, phoneid,
165 (sysarg_t) &data, (sysarg_t) &data);
166 if (callres)
167 return callres;
168
169 if (result1)
170 *result1 = IPC_GET_ARG1(data);
171 if (result2)
172 *result2 = IPC_GET_ARG2(data);
173 if (result3)
174 *result3 = IPC_GET_ARG3(data);
175 if (result4)
176 *result4 = IPC_GET_ARG4(data);
177 if (result5)
178 *result5 = IPC_GET_ARG5(data);
179
180 return IPC_GET_RETVAL(data);
181}
182
183/** Syscall to send asynchronous message.
184 *
185 * @param phoneid Phone handle for the call.
186 * @param data Call data with the request.
187 *
188 * @return Hash of the call or an error code.
189 *
190 */
191static ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data)
192{
193 return __SYSCALL2(SYS_IPC_CALL_ASYNC_SLOW, phoneid, (sysarg_t) data);
194}
195
196/** Prolog to ipc_call_async_*() functions.
197 *
198 * @param private Argument for the answer/error callback.
199 * @param callback Answer/error callback.
200 *
201 * @return New, partially initialized async_call structure or NULL.
202 */
203static inline async_call_t *ipc_prepare_async(void *private,
204 ipc_async_callback_t callback)
205{
206 async_call_t *call;
207
208 call = malloc(sizeof(*call));
209 if (!call) {
210 if (callback)
211 callback(private, ENOMEM, NULL);
212 return NULL;
213 }
214 call->callback = callback;
215 call->private = private;
216
217 return call;
218}
219
220/** Epilogue of ipc_call_async_*() functions.
221 *
222 * @param callid Value returned by the SYS_IPC_CALL_ASYNC_* syscall.
223 * @param phoneid Phone handle through which the call was made.
224 * @param call async_call structure returned by ipc_prepare_async().
225 * @param can_preempt If non-zero, the current fibril can be preempted in this
226 * call.
227 */
228static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
229 async_call_t *call, int can_preempt)
230{
231 if (!call) { /* Nothing to do regardless if failed or not */
232 futex_up(&ipc_futex);
233 return;
234 }
235
236 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
237 futex_up(&ipc_futex);
238 /* Call asynchronous handler with error code */
239 if (call->callback)
240 call->callback(call->private, ENOENT, NULL);
241 free(call);
242 return;
243 }
244
245 if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
246 futex_up(&ipc_futex);
247
248 call->u.msg.phoneid = phoneid;
249
250 futex_down(&async_futex);
251 list_append(&call->list, &queued_calls);
252
253 if (can_preempt) {
254 call->fid = fibril_get_id();
255 fibril_switch(FIBRIL_TO_MANAGER);
256 /* Async futex unlocked by previous call */
257 } else {
258 call->fid = 0;
259 futex_up(&async_futex);
260 }
261 return;
262 }
263 call->u.callid = callid;
264 /* Add call to the list of dispatched calls */
265 list_append(&call->list, &dispatched_calls);
266 futex_up(&ipc_futex);
267
268}
269
270/** Make a fast asynchronous call.
271 *
272 * This function can only handle four arguments of payload. It is, however,
273 * faster than the more generic ipc_call_async_slow().
274 *
275 * Note that this function is a void function.
276 * During normal opertation, answering this call will trigger the callback.
277 * In case of fatal error, call the callback handler with the proper error code.
278 * If the call cannot be temporarily made, queue it.
279 *
280 * @param phoneid Phone handle for the call.
281 * @param imethod Requested interface and method.
282 * @param arg1 Service-defined payload argument.
283 * @param arg2 Service-defined payload argument.
284 * @param arg3 Service-defined payload argument.
285 * @param arg4 Service-defined payload argument.
286 * @param private Argument to be passed to the answer/error callback.
287 * @param callback Answer or error callback.
288 * @param can_preempt If non-zero, the current fibril will be preempted in
289 * case the kernel temporarily refuses to accept more
290 * asynchronous calls.
291 *
292 */
293void ipc_call_async_fast(int phoneid, sysarg_t imethod, sysarg_t arg1,
294 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, void *private,
295 ipc_async_callback_t callback, int can_preempt)
296{
297 async_call_t *call = NULL;
298
299 if (callback) {
300 call = ipc_prepare_async(private, callback);
301 if (!call)
302 return;
303 }
304
305 /*
306 * We need to make sure that we get callid before another thread
307 * accesses the queue again.
308 */
309 futex_down(&ipc_futex);
310 ipc_callid_t callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid,
311 imethod, arg1, arg2, arg3, arg4);
312
313 if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
314 if (!call) {
315 call = ipc_prepare_async(private, callback);
316 if (!call)
317 return;
318 }
319 IPC_SET_IMETHOD(call->u.msg.data, imethod);
320 IPC_SET_ARG1(call->u.msg.data, arg1);
321 IPC_SET_ARG2(call->u.msg.data, arg2);
322 IPC_SET_ARG3(call->u.msg.data, arg3);
323 IPC_SET_ARG4(call->u.msg.data, arg4);
324 /*
325 * To achieve deterministic behavior, we always zero out the
326 * arguments that are beyond the limits of the fast version.
327 */
328 IPC_SET_ARG5(call->u.msg.data, 0);
329 }
330 ipc_finish_async(callid, phoneid, call, can_preempt);
331}
332
333/** Make an asynchronous call transmitting the entire payload.
334 *
335 * Note that this function is a void function.
336 * During normal opertation, answering this call will trigger the callback.
337 * In case of fatal error, call the callback handler with the proper error code.
338 * If the call cannot be temporarily made, queue it.
339 *
340 * @param phoneid Phone handle for the call.
341 * @param imethod Requested interface and method.
342 * @param arg1 Service-defined payload argument.
343 * @param arg2 Service-defined payload argument.
344 * @param arg3 Service-defined payload argument.
345 * @param arg4 Service-defined payload argument.
346 * @param arg5 Service-defined payload argument.
347 * @param private Argument to be passed to the answer/error callback.
348 * @param callback Answer or error callback.
349 * @param can_preempt If non-zero, the current fibril will be preempted in
350 * case the kernel temporarily refuses to accept more
351 * asynchronous calls.
352 *
353 */
354void ipc_call_async_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
355 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
356 ipc_async_callback_t callback, int can_preempt)
357{
358 async_call_t *call;
359 ipc_callid_t callid;
360
361 call = ipc_prepare_async(private, callback);
362 if (!call)
363 return;
364
365 IPC_SET_IMETHOD(call->u.msg.data, imethod);
366 IPC_SET_ARG1(call->u.msg.data, arg1);
367 IPC_SET_ARG2(call->u.msg.data, arg2);
368 IPC_SET_ARG3(call->u.msg.data, arg3);
369 IPC_SET_ARG4(call->u.msg.data, arg4);
370 IPC_SET_ARG5(call->u.msg.data, arg5);
371 /*
372 * We need to make sure that we get callid before another thread
373 * accesses the queue again.
374 */
375 futex_down(&ipc_futex);
376 callid = _ipc_call_async(phoneid, &call->u.msg.data);
377
378 ipc_finish_async(callid, phoneid, call, can_preempt);
379}
380
381
382/** Answer a received call - fast version.
383 *
384 * The fast answer makes use of passing retval and first four arguments in
385 * registers. If you need to return more, use the ipc_answer_slow() instead.
386 *
387 * @param callid Hash of the call being answered.
388 * @param retval Return value.
389 * @param arg1 First return argument.
390 * @param arg2 Second return argument.
391 * @param arg3 Third return argument.
392 * @param arg4 Fourth return argument.
393 *
394 * @return Zero on success or a value from @ref errno.h on failure.
395 */
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)
398{
399 return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
400 arg4);
401}
402
403/** Answer a received call - slow full version.
404 *
405 * @param callid Hash of the call being answered.
406 * @param retval Return value.
407 * @param arg1 First return argument.
408 * @param arg2 Second return argument.
409 * @param arg3 Third return argument.
410 * @param arg4 Fourth return argument.
411 * @param arg5 Fifth return argument.
412 *
413 * @return Zero on success or a value from @ref errno.h on failure.
414 */
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)
417{
418 ipc_call_t data;
419
420 IPC_SET_RETVAL(data, retval);
421 IPC_SET_ARG1(data, arg1);
422 IPC_SET_ARG2(data, arg2);
423 IPC_SET_ARG3(data, arg3);
424 IPC_SET_ARG4(data, arg4);
425 IPC_SET_ARG5(data, arg5);
426
427 return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
428}
429
430
431/** Try to dispatch queued calls from the async queue. */
432static void try_dispatch_queued_calls(void)
433{
434 async_call_t *call;
435 ipc_callid_t callid;
436
437 /** @todo
438 * Integrate intelligently ipc_futex, so that it is locked during
439 * ipc_call_async_*(), until it is added to dispatched_calls.
440 */
441 futex_down(&async_futex);
442 while (!list_empty(&queued_calls)) {
443 call = list_get_instance(queued_calls.next, async_call_t, list);
444 callid = _ipc_call_async(call->u.msg.phoneid,
445 &call->u.msg.data);
446 if (callid == (ipc_callid_t) IPC_CALLRET_TEMPORARY) {
447 break;
448 }
449 list_remove(&call->list);
450
451 futex_up(&async_futex);
452 if (call->fid)
453 fibril_add_ready(call->fid);
454
455 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
456 if (call->callback)
457 call->callback(call->private, ENOENT, NULL);
458 free(call);
459 } else {
460 call->u.callid = callid;
461 futex_down(&ipc_futex);
462 list_append(&call->list, &dispatched_calls);
463 futex_up(&ipc_futex);
464 }
465 futex_down(&async_futex);
466 }
467 futex_up(&async_futex);
468}
469
470/** Handle a received answer.
471 *
472 * Find the hash of the answer and call the answer callback.
473 *
474 * @todo Make it use hash table.
475 *
476 * @param callid Hash of the received answer.
477 * The answer has the same hash as the request OR'ed with
478 * the IPC_CALLID_ANSWERED bit.
479 * @param data Call data of the answer.
480 */
481static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
482{
483 link_t *item;
484 async_call_t *call;
485
486 callid &= ~IPC_CALLID_ANSWERED;
487
488 futex_down(&ipc_futex);
489 for (item = dispatched_calls.next; item != &dispatched_calls;
490 item = item->next) {
491 call = list_get_instance(item, async_call_t, list);
492 if (call->u.callid == callid) {
493 list_remove(&call->list);
494 futex_up(&ipc_futex);
495 if (call->callback)
496 call->callback(call->private,
497 IPC_GET_RETVAL(*data), data);
498 free(call);
499 return;
500 }
501 }
502 futex_up(&ipc_futex);
503}
504
505
506/** Wait for a first call to come.
507 *
508 * @param call Storage where the incoming call data will be stored.
509 * @param usec Timeout in microseconds
510 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
511 *
512 * @return Hash of the call. Note that certain bits have special
513 * meaning. IPC_CALLID_ANSWERED will be set in an answer
514 * and IPC_CALLID_NOTIFICATION is used for notifications.
515 *
516 */
517ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags)
518{
519 ipc_callid_t callid;
520
521 callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
522 /* Handle received answers */
523 if (callid & IPC_CALLID_ANSWERED) {
524 handle_answer(callid, call);
525 try_dispatch_queued_calls();
526 }
527
528 return callid;
529}
530
531/** Wait some time for an IPC call.
532 *
533 * The call will return after an answer is received.
534 *
535 * @param call Storage where the incoming call data will be stored.
536 * @param usec Timeout in microseconds.
537 *
538 * @return Hash of the answer.
539 */
540ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec)
541{
542 ipc_callid_t callid;
543
544 do {
545 callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
546 } while (callid & IPC_CALLID_ANSWERED);
547
548 return callid;
549}
550
551/** Check if there is an IPC call waiting to be picked up.
552 *
553 * @param call Storage where the incoming call will be stored.
554 * @return Hash of the answer.
555 */
556ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
557{
558 ipc_callid_t callid;
559
560 do {
561 callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
562 SYNCH_FLAGS_NON_BLOCKING);
563 } while (callid & IPC_CALLID_ANSWERED);
564
565 return callid;
566}
567
568/** Interrupt one thread of this task from waiting for IPC. */
569void ipc_poke(void)
570{
571 __SYSCALL0(SYS_IPC_POKE);
572}
573
574/** Ask destination to do a callback connection.
575 *
576 * @param phoneid Phone handle used for contacting the other side.
577 * @param arg1 Service-defined argument.
578 * @param arg2 Service-defined argument.
579 * @param arg3 Service-defined argument.
580 * @param phonehash Storage where the library will store an opaque
581 * identifier of the phone that will be used for incoming
582 * calls. This identifier can be used for connection
583 * tracking.
584 *
585 * @return Zero on success or a negative error code.
586 */
587int ipc_connect_to_me(int phoneid, int arg1, int arg2, int arg3,
588 sysarg_t *phonehash)
589{
590 return ipc_call_sync_3_5(phoneid, IPC_M_CONNECT_TO_ME, arg1, arg2,
591 arg3, NULL, NULL, NULL, NULL, phonehash);
592}
593
594/** Ask through phone for a new connection to some service.
595 *
596 * @param phoneid Phone handle used for contacting the other side.
597 * @param arg1 User defined argument.
598 * @param arg2 User defined argument.
599 * @param arg3 User defined argument.
600 *
601 * @return New phone handle on success or a negative error code.
602 */
603int ipc_connect_me_to(int phoneid, int arg1, int arg2, int arg3)
604{
605 sysarg_t newphid;
606 int res;
607
608 res = ipc_call_sync_3_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
609 NULL, NULL, NULL, NULL, &newphid);
610 if (res)
611 return res;
612 return newphid;
613}
614
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{
629 sysarg_t newphid;
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
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 */
645int ipc_hangup(int phoneid)
646{
647 return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
648}
649
650/** Register IRQ notification.
651 *
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.
656 *
657 * @return Value returned by the kernel.
658 */
659int ipc_register_irq(int inr, int devno, int method, irq_code_t *ucode)
660{
661 return __SYSCALL4(SYS_IPC_REGISTER_IRQ, inr, devno, method,
662 (sysarg_t) ucode);
663}
664
665/** Unregister IRQ notification.
666 *
667 * @param inr IRQ number.
668 * @param devno Device number of the device generating inr.
669 *
670 * @return Value returned by the kernel.
671 */
672int ipc_unregister_irq(int inr, int devno)
673{
674 return __SYSCALL2(SYS_IPC_UNREGISTER_IRQ, inr, devno);
675}
676
677/** Forward a received call to another destination.
678 *
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.
685 *
686 * @return Zero on success or an error code.
687 *
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
690 * to the old arg1, arg2 and arg3, respectivelly. Calls with immutable
691 * methods are forwarded verbatim.
692 */
693int ipc_forward_fast(ipc_callid_t callid, int phoneid, int imethod,
694 sysarg_t arg1, sysarg_t arg2, int mode)
695{
696 return __SYSCALL6(SYS_IPC_FORWARD_FAST, callid, phoneid, imethod, arg1,
697 arg2, mode);
698}
699
700
701int ipc_forward_slow(ipc_callid_t callid, int phoneid, int imethod,
702 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
703 int mode)
704{
705 ipc_call_t data;
706
707 IPC_SET_IMETHOD(data, imethod);
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);
713
714 return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data, mode);
715}
716
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 */
728int ipc_share_in_start(int phoneid, void *dst, size_t size, sysarg_t arg,
729 int *flags)
730{
731 sysarg_t tmp_flags = 0;
732 int res = ipc_call_sync_3_2(phoneid, IPC_M_SHARE_IN, (sysarg_t) dst,
733 (sysarg_t) size, arg, NULL, &tmp_flags);
734
735 if (flags)
736 *flags = tmp_flags;
737
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 */
752int ipc_share_in_finalize(ipc_callid_t callid, void *src, int flags)
753{
754 return ipc_answer_2(callid, EOK, (sysarg_t) src, (sysarg_t) flags);
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 */
765int ipc_share_out_start(int phoneid, void *src, int flags)
766{
767 return ipc_call_sync_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
768 (sysarg_t) flags);
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 */
781int ipc_share_out_finalize(ipc_callid_t callid, void *dst)
782{
783 return ipc_answer_1(callid, EOK, (sysarg_t) dst);
784}
785
786
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 */
795int ipc_data_read_start(int phoneid, void *dst, size_t size)
796{
797 return ipc_call_sync_2_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
798 (sysarg_t) size);
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 */
813int ipc_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
814{
815 return ipc_answer_2(callid, EOK, (sysarg_t) src, (sysarg_t) size);
816}
817
818/** Wrapper for making IPC_M_DATA_WRITE calls.
819 *
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 */
826int ipc_data_write_start(int phoneid, const void *src, size_t size)
827{
828 return ipc_call_sync_2_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
829 (sysarg_t) size);
830}
831
832/** Wrapper for answering the IPC_M_DATA_WRITE calls.
833 *
834 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE calls
835 * so that the user doesn't have to remember the meaning of each IPC argument.
836 *
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.
840 *
841 * @return Zero on success or a value from @ref errno.h on failure.
842 */
843int ipc_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
844{
845 return ipc_answer_2(callid, EOK, (sysarg_t) dst, (sysarg_t) size);
846}
847
848/** Connect to a task specified by id.
849 *
850 */
851int ipc_connect_kbox(task_id_t id)
852{
853#ifdef __32_BITS__
854 sysarg64_t arg = (sysarg64_t) id;
855 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
856#endif
857
858#ifdef __64_BITS__
859 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
860#endif
861}
862
863/** @}
864 */
Note: See TracBrowser for help on using the repository browser.