source: mainline/uspace/lib/c/generic/ipc.c@ 7c0e1f5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7c0e1f5 was 7c0e1f5, checked in by Jakub Jermar <jakub@…>, 8 years ago

Rework userspace call tracking

Setting the address of the userspace call structure in the kernel
call_t structure on send allows us to remove lots of userspace
scaffolding. More importantly, it also opens the door for not needing
the callid (later capability) on answer receive.

  • Property mode set to 100644
File size: 11.3 KB
Line 
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 <malloc.h>
46#include <errno.h>
47#include <adt/list.h>
48#include <futex.h>
49#include <fibril.h>
50#include <macros.h>
51
52/**
53 * Structures of this type are used for keeping track of sent asynchronous calls.
54 */
55typedef struct async_call {
56 ipc_async_callback_t callback;
57 void *private;
58
59 struct {
60 ipc_call_t data;
61 int phoneid;
62 } msg;
63} async_call_t;
64
65/** Prologue for ipc_call_async_*() functions.
66 *
67 * @param private Argument for the answer/error callback.
68 * @param callback Answer/error callback.
69 *
70 * @return New, partially initialized async_call structure or NULL.
71 *
72 */
73static inline async_call_t *ipc_prepare_async(void *private,
74 ipc_async_callback_t callback)
75{
76 async_call_t *call =
77 (async_call_t *) malloc(sizeof(async_call_t));
78 if (!call) {
79 if (callback)
80 callback(private, ENOMEM, NULL);
81
82 return NULL;
83 }
84
85 call->callback = callback;
86 call->private = private;
87
88 return call;
89}
90
91/** Epilogue for ipc_call_async_*() functions.
92 *
93 * @param callid Value returned by the SYS_IPC_CALL_ASYNC_* syscall.
94 * @param phoneid Phone handle through which the call was made.
95 * @param call Structure returned by ipc_prepare_async().
96 */
97static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
98 async_call_t *call)
99{
100 if (!call) {
101 /* Nothing to do regardless if failed or not */
102 return;
103 }
104
105 if (callid == (ipc_callid_t) IPC_CALLRET_FATAL) {
106 /* Call asynchronous handler with error code */
107 if (call->callback)
108 call->callback(call->private, ENOENT, NULL);
109
110 free(call);
111 return;
112 }
113}
114
115/** Fast asynchronous call.
116 *
117 * This function can only handle three arguments of payload. It is, however,
118 * faster than the more generic ipc_call_async_slow().
119 *
120 * Note that this function is a void function.
121 *
122 * During normal operation, answering this call will trigger the callback.
123 * In case of fatal error, the callback handler is called with the proper
124 * error code. If the call cannot be temporarily made, it is queued.
125 *
126 * @param phoneid Phone handle for the call.
127 * @param imethod Requested interface and method.
128 * @param arg1 Service-defined payload argument.
129 * @param arg2 Service-defined payload argument.
130 * @param arg3 Service-defined payload argument.
131 * @param private Argument to be passed to the answer/error callback.
132 * @param callback Answer or error callback.
133 */
134void ipc_call_async_fast(int phoneid, sysarg_t imethod, sysarg_t arg1,
135 sysarg_t arg2, sysarg_t arg3, void *private, ipc_async_callback_t callback)
136{
137 async_call_t *call = NULL;
138
139 if (callback) {
140 call = ipc_prepare_async(private, callback);
141 if (!call)
142 return;
143 }
144
145 ipc_callid_t callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid,
146 imethod, arg1, arg2, arg3, (sysarg_t) call);
147
148 ipc_finish_async(callid, phoneid, call);
149}
150
151/** Asynchronous call transmitting the entire payload.
152 *
153 * Note that this function is a void function.
154 *
155 * During normal operation, answering this call will trigger the callback.
156 * In case of fatal error, the callback handler is called with the proper
157 * error code. If the call cannot be temporarily made, it is queued.
158 *
159 * @param phoneid Phone handle for the call.
160 * @param imethod Requested interface and method.
161 * @param arg1 Service-defined payload argument.
162 * @param arg2 Service-defined payload argument.
163 * @param arg3 Service-defined payload argument.
164 * @param arg4 Service-defined payload argument.
165 * @param arg5 Service-defined payload argument.
166 * @param private Argument to be passed to the answer/error callback.
167 * @param callback Answer or error callback.
168 */
169void ipc_call_async_slow(int phoneid, sysarg_t imethod, sysarg_t arg1,
170 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, void *private,
171 ipc_async_callback_t callback)
172{
173 async_call_t *call = ipc_prepare_async(private, callback);
174 if (!call)
175 return;
176
177 IPC_SET_IMETHOD(call->msg.data, imethod);
178 IPC_SET_ARG1(call->msg.data, arg1);
179 IPC_SET_ARG2(call->msg.data, arg2);
180 IPC_SET_ARG3(call->msg.data, arg3);
181 IPC_SET_ARG4(call->msg.data, arg4);
182 IPC_SET_ARG5(call->msg.data, arg5);
183
184 ipc_callid_t callid = __SYSCALL3(SYS_IPC_CALL_ASYNC_SLOW, phoneid,
185 (sysarg_t) &call->msg.data, (sysarg_t) call);
186
187 ipc_finish_async(callid, phoneid, call);
188}
189
190/** Answer received call (fast version).
191 *
192 * The fast answer makes use of passing retval and first four arguments in
193 * registers. If you need to return more, use the ipc_answer_slow() instead.
194 *
195 * @param callid Hash of the call being answered.
196 * @param retval Return value.
197 * @param arg1 First return argument.
198 * @param arg2 Second return argument.
199 * @param arg3 Third return argument.
200 * @param arg4 Fourth return argument.
201 *
202 * @return Zero on success.
203 * @return Value from @ref errno.h on failure.
204 *
205 */
206sysarg_t ipc_answer_fast(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
207 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
208{
209 return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
210 arg4);
211}
212
213/** Answer received call (entire payload).
214 *
215 * @param callid Hash of the call being answered.
216 * @param retval Return value.
217 * @param arg1 First return argument.
218 * @param arg2 Second return argument.
219 * @param arg3 Third return argument.
220 * @param arg4 Fourth return argument.
221 * @param arg5 Fifth return argument.
222 *
223 * @return Zero on success.
224 * @return Value from @ref errno.h on failure.
225 *
226 */
227sysarg_t ipc_answer_slow(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
228 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
229{
230 ipc_call_t data;
231
232 IPC_SET_RETVAL(data, retval);
233 IPC_SET_ARG1(data, arg1);
234 IPC_SET_ARG2(data, arg2);
235 IPC_SET_ARG3(data, arg3);
236 IPC_SET_ARG4(data, arg4);
237 IPC_SET_ARG5(data, arg5);
238
239 return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
240}
241
242/** Handle received answer.
243 *
244 * @param callid Hash of the received answer.
245 * @param data Call data of the answer.
246 */
247static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
248{
249 async_call_t *call = data->label;
250
251 if (!call)
252 return;
253
254 if (call->callback)
255 call->callback(call->private, IPC_GET_RETVAL(*data), data);
256 free(call);
257}
258
259/** Wait for first IPC call to come.
260 *
261 * @param call Incoming call storage.
262 * @param usec Timeout in microseconds
263 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
264 *
265 * @return Hash of the call. Note that certain bits have special
266 * meaning: IPC_CALLID_ANSWERED is set in an answer
267 * and IPC_CALLID_NOTIFICATION is used for notifications.
268 *
269 */
270ipc_callid_t ipc_wait_cycle(ipc_call_t *call, sysarg_t usec,
271 unsigned int flags)
272{
273 ipc_callid_t callid =
274 __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
275
276 /* Handle received answers */
277 if (callid & IPC_CALLID_ANSWERED)
278 handle_answer(callid, call);
279
280 return callid;
281}
282
283/** Interrupt one thread of this task from waiting for IPC.
284 *
285 */
286void ipc_poke(void)
287{
288 __SYSCALL0(SYS_IPC_POKE);
289}
290
291/** Wait for first IPC call to come.
292 *
293 * Only requests are returned, answers are processed internally.
294 *
295 * @param call Incoming call storage.
296 * @param usec Timeout in microseconds
297 *
298 * @return Hash of the call.
299 *
300 */
301ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, sysarg_t usec)
302{
303 ipc_callid_t callid;
304
305 do {
306 callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
307 } while (callid & IPC_CALLID_ANSWERED);
308
309 return callid;
310}
311
312/** Check if there is an IPC call waiting to be picked up.
313 *
314 * Only requests are returned, answers are processed internally.
315 *
316 * @param call Incoming call storage.
317 *
318 * @return Hash of the call.
319 *
320 */
321ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
322{
323 ipc_callid_t callid;
324
325 do {
326 callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
327 SYNCH_FLAGS_NON_BLOCKING);
328 } while (callid & IPC_CALLID_ANSWERED);
329
330 return callid;
331}
332
333/** Hang up a phone.
334 *
335 * @param phoneid Handle of the phone to be hung up.
336 *
337 * @return Zero on success or a negative error code.
338 *
339 */
340int ipc_hangup(int phoneid)
341{
342 return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
343}
344
345/** Forward a received call to another destination.
346 *
347 * For non-system methods, the old method, arg1 and arg2 are rewritten
348 * by the new values. For system methods, the new method, arg1 and arg2
349 * are written to the old arg1, arg2 and arg3, respectivelly. Calls with
350 * immutable methods are forwarded verbatim.
351 *
352 * @param callid Hash of the call to forward.
353 * @param phoneid Phone handle to use for forwarding.
354 * @param imethod New interface and method for the forwarded call.
355 * @param arg1 New value of the first argument for the forwarded call.
356 * @param arg2 New value of the second argument for the forwarded call.
357 * @param mode Flags specifying mode of the forward operation.
358 *
359 * @return Zero on success or an error code.
360 *
361 */
362int ipc_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
363 sysarg_t arg1, sysarg_t arg2, unsigned int mode)
364{
365 return __SYSCALL6(SYS_IPC_FORWARD_FAST, callid, phoneid, imethod, arg1,
366 arg2, mode);
367}
368
369int ipc_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
370 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
371 unsigned int mode)
372{
373 ipc_call_t data;
374
375 IPC_SET_IMETHOD(data, imethod);
376 IPC_SET_ARG1(data, arg1);
377 IPC_SET_ARG2(data, arg2);
378 IPC_SET_ARG3(data, arg3);
379 IPC_SET_ARG4(data, arg4);
380 IPC_SET_ARG5(data, arg5);
381
382 return __SYSCALL4(SYS_IPC_FORWARD_SLOW, callid, phoneid, (sysarg_t) &data,
383 mode);
384}
385
386/** Connect to a task specified by id.
387 *
388 */
389int ipc_connect_kbox(task_id_t id)
390{
391#ifdef __32_BITS__
392 sysarg64_t arg = (sysarg64_t) id;
393 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) &arg);
394#endif
395
396#ifdef __64_BITS__
397 return __SYSCALL1(SYS_IPC_CONNECT_KBOX, (sysarg_t) id);
398#endif
399}
400
401/** @}
402 */
Note: See TracBrowser for help on using the repository browser.