source: mainline/uspace/lib/c/generic/ipc.c@ 40e5d66

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

Remove unused member of async_call_t

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