1 | /*
|
---|
2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
3 | * Copyright (c) 2017 Jakub Jermar
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libc
|
---|
31 | * @{
|
---|
32 | * @}
|
---|
33 | */
|
---|
34 |
|
---|
35 | /** @addtogroup libcipc IPC
|
---|
36 | * @brief HelenOS uspace IPC
|
---|
37 | * @{
|
---|
38 | * @ingroup libc
|
---|
39 | */
|
---|
40 | /** @file
|
---|
41 | */
|
---|
42 |
|
---|
43 | #include <ipc/ipc.h>
|
---|
44 | #include <libc.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <errno.h>
|
---|
47 | #include <adt/list.h>
|
---|
48 | #include <futex.h>
|
---|
49 | #include <fibril.h>
|
---|
50 | #include <macros.h>
|
---|
51 |
|
---|
52 | /** Fast asynchronous call.
|
---|
53 | *
|
---|
54 | * This function can only handle three arguments of payload. It is, however,
|
---|
55 | * faster than the more generic ipc_call_async_slow().
|
---|
56 | *
|
---|
57 | * Note that this function is a void function.
|
---|
58 | *
|
---|
59 | * During normal operation, answering this call will trigger the callback.
|
---|
60 | * In case of fatal error, the callback handler is called with the proper
|
---|
61 | * error code. If the call cannot be temporarily made, it is queued.
|
---|
62 | *
|
---|
63 | * @param phandle Phone handle for the call.
|
---|
64 | * @param imethod Requested interface and method.
|
---|
65 | * @param arg1 Service-defined payload argument.
|
---|
66 | * @param arg2 Service-defined payload argument.
|
---|
67 | * @param arg3 Service-defined payload argument.
|
---|
68 | * @param label A value to set to the label field of the answer.
|
---|
69 | */
|
---|
70 | errno_t ipc_call_async_fast(cap_phone_handle_t phandle, sysarg_t imethod,
|
---|
71 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, void *label)
|
---|
72 | {
|
---|
73 | return __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST,
|
---|
74 | CAP_HANDLE_RAW(phandle), imethod, arg1, arg2, arg3,
|
---|
75 | (sysarg_t) label);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Asynchronous call transmitting the entire payload.
|
---|
79 | *
|
---|
80 | * Note that this function is a void function.
|
---|
81 | *
|
---|
82 | * During normal operation, answering this call will trigger the callback.
|
---|
83 | * In case of fatal error, the callback handler is called with the proper
|
---|
84 | * error code. If the call cannot be temporarily made, it is queued.
|
---|
85 | *
|
---|
86 | * @param phandle Phone handle for the call.
|
---|
87 | * @param imethod Requested interface and method.
|
---|
88 | * @param arg1 Service-defined payload argument.
|
---|
89 | * @param arg2 Service-defined payload argument.
|
---|
90 | * @param arg3 Service-defined payload argument.
|
---|
91 | * @param arg4 Service-defined payload argument.
|
---|
92 | * @param arg5 Service-defined payload argument.
|
---|
93 | * @param label A value to set to the label field of the answer.
|
---|
94 | */
|
---|
95 | errno_t ipc_call_async_slow(cap_phone_handle_t phandle, sysarg_t imethod,
|
---|
96 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
---|
97 | void *label)
|
---|
98 | {
|
---|
99 | ipc_call_t data;
|
---|
100 |
|
---|
101 | IPC_SET_IMETHOD(data, imethod);
|
---|
102 | IPC_SET_ARG1(data, arg1);
|
---|
103 | IPC_SET_ARG2(data, arg2);
|
---|
104 | IPC_SET_ARG3(data, arg3);
|
---|
105 | IPC_SET_ARG4(data, arg4);
|
---|
106 | IPC_SET_ARG5(data, arg5);
|
---|
107 |
|
---|
108 | return __SYSCALL3(SYS_IPC_CALL_ASYNC_SLOW,
|
---|
109 | CAP_HANDLE_RAW(phandle), (sysarg_t) &data,
|
---|
110 | (sysarg_t) label);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** Answer received call (fast version).
|
---|
114 | *
|
---|
115 | * The fast answer makes use of passing retval and first four arguments in
|
---|
116 | * registers. If you need to return more, use the ipc_answer_slow() instead.
|
---|
117 | *
|
---|
118 | * @param chandle Handle of the call being answered.
|
---|
119 | * @param retval Return value.
|
---|
120 | * @param arg1 First return argument.
|
---|
121 | * @param arg2 Second return argument.
|
---|
122 | * @param arg3 Third return argument.
|
---|
123 | * @param arg4 Fourth return argument.
|
---|
124 | *
|
---|
125 | * @return Zero on success.
|
---|
126 | * @return Value from @ref errno.h on failure.
|
---|
127 | *
|
---|
128 | */
|
---|
129 | errno_t ipc_answer_fast(cap_call_handle_t chandle, errno_t retval,
|
---|
130 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
131 | {
|
---|
132 | return (errno_t) __SYSCALL6(SYS_IPC_ANSWER_FAST,
|
---|
133 | CAP_HANDLE_RAW(chandle), (sysarg_t) retval, arg1, arg2, arg3, arg4);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** Answer received call (entire payload).
|
---|
137 | *
|
---|
138 | * @param chandle Handle of the call being answered.
|
---|
139 | * @param retval Return value.
|
---|
140 | * @param arg1 First return argument.
|
---|
141 | * @param arg2 Second return argument.
|
---|
142 | * @param arg3 Third return argument.
|
---|
143 | * @param arg4 Fourth return argument.
|
---|
144 | * @param arg5 Fifth return argument.
|
---|
145 | *
|
---|
146 | * @return Zero on success.
|
---|
147 | * @return Value from @ref errno.h on failure.
|
---|
148 | *
|
---|
149 | */
|
---|
150 | errno_t ipc_answer_slow(cap_call_handle_t chandle, errno_t retval,
|
---|
151 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
152 | {
|
---|
153 | ipc_call_t data;
|
---|
154 |
|
---|
155 | IPC_SET_RETVAL(data, retval);
|
---|
156 | IPC_SET_ARG1(data, arg1);
|
---|
157 | IPC_SET_ARG2(data, arg2);
|
---|
158 | IPC_SET_ARG3(data, arg3);
|
---|
159 | IPC_SET_ARG4(data, arg4);
|
---|
160 | IPC_SET_ARG5(data, arg5);
|
---|
161 |
|
---|
162 | return (errno_t) __SYSCALL2(SYS_IPC_ANSWER_SLOW,
|
---|
163 | CAP_HANDLE_RAW(chandle), (sysarg_t) &data);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** Interrupt one thread of this task from waiting for IPC.
|
---|
167 | *
|
---|
168 | */
|
---|
169 | void ipc_poke(void)
|
---|
170 | {
|
---|
171 | __SYSCALL0(SYS_IPC_POKE);
|
---|
172 | }
|
---|
173 |
|
---|
174 | errno_t ipc_wait(ipc_call_t *call, sysarg_t usec, unsigned int flags)
|
---|
175 | {
|
---|
176 | // TODO: Use expiration time instead of timeout.
|
---|
177 | return __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /** Hang up a phone.
|
---|
181 | *
|
---|
182 | * @param phandle Handle of the phone to be hung up.
|
---|
183 | *
|
---|
184 | * @return Zero on success or an error code.
|
---|
185 | *
|
---|
186 | */
|
---|
187 | errno_t ipc_hangup(cap_phone_handle_t phandle)
|
---|
188 | {
|
---|
189 | return (errno_t) __SYSCALL1(SYS_IPC_HANGUP, CAP_HANDLE_RAW(phandle));
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** Forward a received call to another destination.
|
---|
193 | *
|
---|
194 | * For non-system methods, the old method, arg1 and arg2 are rewritten by the
|
---|
195 | * new values. For system methods, the new method, arg1 and arg2 are written to
|
---|
196 | * the old arg1, arg2 and arg3, respectivelly. Calls with immutable methods are
|
---|
197 | * forwarded verbatim.
|
---|
198 | *
|
---|
199 | * @param chandle Handle of the call to forward.
|
---|
200 | * @param phandle Phone handle to use for forwarding.
|
---|
201 | * @param imethod New interface and method for the forwarded call.
|
---|
202 | * @param arg1 New value of the first argument for the forwarded call.
|
---|
203 | * @param arg2 New value of the second argument for the forwarded call.
|
---|
204 | * @param mode Flags specifying mode of the forward operation.
|
---|
205 | *
|
---|
206 | * @return Zero on success or an error code.
|
---|
207 | *
|
---|
208 | */
|
---|
209 | errno_t ipc_forward_fast(cap_call_handle_t chandle, cap_phone_handle_t phandle,
|
---|
210 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
211 | {
|
---|
212 | return (errno_t) __SYSCALL6(SYS_IPC_FORWARD_FAST,
|
---|
213 | CAP_HANDLE_RAW(chandle), CAP_HANDLE_RAW(phandle), imethod, arg1,
|
---|
214 | arg2, mode);
|
---|
215 | }
|
---|
216 |
|
---|
217 | errno_t ipc_forward_slow(cap_call_handle_t chandle, cap_phone_handle_t phandle,
|
---|
218 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
219 | sysarg_t arg4, sysarg_t arg5, unsigned int mode)
|
---|
220 | {
|
---|
221 | ipc_call_t data;
|
---|
222 |
|
---|
223 | IPC_SET_IMETHOD(data, imethod);
|
---|
224 | IPC_SET_ARG1(data, arg1);
|
---|
225 | IPC_SET_ARG2(data, arg2);
|
---|
226 | IPC_SET_ARG3(data, arg3);
|
---|
227 | IPC_SET_ARG4(data, arg4);
|
---|
228 | IPC_SET_ARG5(data, arg5);
|
---|
229 |
|
---|
230 | return (errno_t) __SYSCALL4(SYS_IPC_FORWARD_SLOW,
|
---|
231 | CAP_HANDLE_RAW(chandle), CAP_HANDLE_RAW(phandle), (sysarg_t) &data,
|
---|
232 | mode);
|
---|
233 | }
|
---|
234 |
|
---|
235 | /** Connect to a task specified by id.
|
---|
236 | *
|
---|
237 | */
|
---|
238 | errno_t ipc_connect_kbox(task_id_t id, cap_phone_handle_t *phone)
|
---|
239 | {
|
---|
240 | return (errno_t) __SYSCALL2(SYS_IPC_CONNECT_KBOX, (sysarg_t) &id, (sysarg_t) phone);
|
---|
241 | }
|
---|
242 |
|
---|
243 | /** @}
|
---|
244 | */
|
---|