Rev 1360 | Rev 1392 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
954 | palkovsky | 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 | |||
1352 | palkovsky | 29 | #include <ipc/ipc.h> |
954 | palkovsky | 30 | #include <libc.h> |
999 | palkovsky | 31 | #include <malloc.h> |
32 | #include <errno.h> |
||
1352 | palkovsky | 33 | #include <libadt/list.h> |
999 | palkovsky | 34 | #include <stdio.h> |
35 | #include <unistd.h> |
||
1350 | palkovsky | 36 | #include <futex.h> |
1365 | jermar | 37 | #include <kernel/synch/synch.h> |
954 | palkovsky | 38 | |
999 | palkovsky | 39 | /** Structure used for keeping track of sent async msgs |
40 | * and queing unsent msgs |
||
41 | * |
||
42 | */ |
||
43 | typedef struct { |
||
44 | link_t list; |
||
45 | |||
46 | ipc_async_callback_t callback; |
||
47 | void *private; |
||
48 | union { |
||
49 | ipc_callid_t callid; |
||
50 | struct { |
||
1091 | palkovsky | 51 | ipc_call_t data; |
999 | palkovsky | 52 | int phoneid; |
53 | } msg; |
||
54 | }u; |
||
55 | } async_call_t; |
||
56 | |||
57 | LIST_INITIALIZE(dispatched_calls); |
||
58 | LIST_INITIALIZE(queued_calls); |
||
59 | |||
1350 | palkovsky | 60 | static atomic_t ipc_futex; |
61 | |||
62 | void _ipc_init(void) |
||
63 | { |
||
64 | futex_initialize(&ipc_futex, 1); |
||
65 | } |
||
66 | |||
999 | palkovsky | 67 | int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1, |
68 | ipcarg_t *result) |
||
954 | palkovsky | 69 | { |
1091 | palkovsky | 70 | ipc_call_t resdata; |
966 | palkovsky | 71 | int callres; |
72 | |||
999 | palkovsky | 73 | callres = __SYSCALL4(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1, |
966 | palkovsky | 74 | (sysarg_t)&resdata); |
75 | if (callres) |
||
76 | return callres; |
||
77 | if (result) |
||
78 | *result = IPC_GET_ARG1(resdata); |
||
79 | return IPC_GET_RETVAL(resdata); |
||
954 | palkovsky | 80 | } |
81 | |||
999 | palkovsky | 82 | int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1, |
83 | ipcarg_t arg2, ipcarg_t arg3, |
||
84 | ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3) |
||
954 | palkovsky | 85 | { |
1091 | palkovsky | 86 | ipc_call_t data; |
966 | palkovsky | 87 | int callres; |
88 | |||
89 | IPC_SET_METHOD(data, method); |
||
90 | IPC_SET_ARG1(data, arg1); |
||
91 | IPC_SET_ARG2(data, arg2); |
||
92 | IPC_SET_ARG3(data, arg3); |
||
93 | |||
1006 | palkovsky | 94 | callres = __SYSCALL3(SYS_IPC_CALL_SYNC, phoneid, (sysarg_t)&data, |
95 | (sysarg_t)&data); |
||
966 | palkovsky | 96 | if (callres) |
97 | return callres; |
||
98 | |||
99 | if (result1) |
||
100 | *result1 = IPC_GET_ARG1(data); |
||
101 | if (result2) |
||
102 | *result2 = IPC_GET_ARG2(data); |
||
103 | if (result3) |
||
104 | *result3 = IPC_GET_ARG3(data); |
||
105 | return IPC_GET_RETVAL(data); |
||
106 | } |
||
107 | |||
999 | palkovsky | 108 | /** Syscall to send asynchronous message */ |
1091 | palkovsky | 109 | static ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data) |
999 | palkovsky | 110 | { |
111 | return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t)data); |
||
112 | } |
||
113 | |||
966 | palkovsky | 114 | /** Send asynchronous message |
115 | * |
||
116 | * - if fatal error, call callback handler with proper error code |
||
117 | * - if message cannot be temporarily sent, add to queue |
||
118 | */ |
||
999 | palkovsky | 119 | void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1, |
120 | ipcarg_t arg2, void *private, |
||
966 | palkovsky | 121 | ipc_async_callback_t callback) |
122 | { |
||
999 | palkovsky | 123 | async_call_t *call; |
966 | palkovsky | 124 | ipc_callid_t callid; |
125 | |||
999 | palkovsky | 126 | call = malloc(sizeof(*call)); |
127 | if (!call) { |
||
128 | callback(private, ENOMEM, NULL); |
||
1028 | palkovsky | 129 | return; |
999 | palkovsky | 130 | } |
131 | |||
132 | callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2); |
||
966 | palkovsky | 133 | if (callid == IPC_CALLRET_FATAL) { |
134 | /* Call asynchronous handler with error code */ |
||
999 | palkovsky | 135 | callback(private, ENOENT, NULL); |
136 | free(call); |
||
966 | palkovsky | 137 | return; |
138 | } |
||
999 | palkovsky | 139 | |
140 | call->callback = callback; |
||
141 | call->private = private; |
||
142 | |||
966 | palkovsky | 143 | if (callid == IPC_CALLRET_TEMPORARY) { |
144 | /* Add asynchronous call to queue of non-dispatched async calls */ |
||
999 | palkovsky | 145 | call->u.msg.phoneid = phoneid; |
146 | IPC_SET_METHOD(call->u.msg.data, method); |
||
147 | IPC_SET_ARG1(call->u.msg.data, arg1); |
||
148 | IPC_SET_ARG2(call->u.msg.data, arg2); |
||
1350 | palkovsky | 149 | |
150 | futex_down(&ipc_futex); |
||
999 | palkovsky | 151 | list_append(&call->list, &queued_calls); |
1350 | palkovsky | 152 | futex_up(&ipc_futex); |
966 | palkovsky | 153 | return; |
154 | } |
||
999 | palkovsky | 155 | call->u.callid = callid; |
156 | /* Add call to list of dispatched calls */ |
||
1350 | palkovsky | 157 | futex_down(&ipc_futex); |
999 | palkovsky | 158 | list_append(&call->list, &dispatched_calls); |
1350 | palkovsky | 159 | futex_up(&ipc_futex); |
954 | palkovsky | 160 | } |
161 | |||
966 | palkovsky | 162 | |
1343 | jermar | 163 | /** Send a fast answer to a received call. |
164 | * |
||
165 | * The fast answer makes use of passing retval and first two arguments in registers. |
||
166 | * If you need to return more, use the ipc_answer() instead. |
||
167 | * |
||
168 | * @param callid ID of the call being answered. |
||
169 | * @param retval Return value. |
||
170 | * @param arg1 First return argument. |
||
171 | * @param arg2 Second return argument. |
||
172 | * |
||
173 | * @return Zero on success or a value from @ref errno.h on failure. |
||
174 | */ |
||
175 | ipcarg_t ipc_answer_fast(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1, |
||
999 | palkovsky | 176 | ipcarg_t arg2) |
954 | palkovsky | 177 | { |
1330 | palkovsky | 178 | return __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2); |
954 | palkovsky | 179 | } |
180 | |||
1343 | jermar | 181 | /** Send a full answer to a received call. |
182 | * |
||
183 | * @param callid ID of the call being answered. |
||
184 | * @param call Call data. Must be already initialized by the responder. |
||
185 | * |
||
186 | * @return Zero on success or a value from @ref errno.h on failure. |
||
187 | */ |
||
188 | ipcarg_t ipc_answer(ipc_callid_t callid, ipc_call_t *call) |
||
189 | { |
||
190 | return __SYSCALL2(SYS_IPC_ANSWER, callid, (sysarg_t) call); |
||
191 | } |
||
192 | |||
193 | |||
999 | palkovsky | 194 | /** Try to dispatch queed calls from async queue */ |
195 | static void try_dispatch_queued_calls(void) |
||
196 | { |
||
197 | async_call_t *call; |
||
198 | ipc_callid_t callid; |
||
199 | |||
1350 | palkovsky | 200 | futex_down(&ipc_futex); |
999 | palkovsky | 201 | while (!list_empty(&queued_calls)) { |
202 | call = list_get_instance(queued_calls.next, async_call_t, |
||
203 | list); |
||
204 | |||
205 | callid = _ipc_call_async(call->u.msg.phoneid, |
||
206 | &call->u.msg.data); |
||
207 | if (callid == IPC_CALLRET_TEMPORARY) |
||
208 | break; |
||
209 | list_remove(&call->list); |
||
1350 | palkovsky | 210 | |
999 | palkovsky | 211 | if (callid == IPC_CALLRET_FATAL) { |
1350 | palkovsky | 212 | futex_up(&ipc_futex); |
999 | palkovsky | 213 | call->callback(call->private, ENOENT, NULL); |
214 | free(call); |
||
1350 | palkovsky | 215 | futex_down(&ipc_futex); |
999 | palkovsky | 216 | } else { |
217 | call->u.callid = callid; |
||
218 | list_append(&call->list, &dispatched_calls); |
||
219 | } |
||
220 | } |
||
1350 | palkovsky | 221 | futex_up(&ipc_futex); |
999 | palkovsky | 222 | } |
223 | |||
224 | /** Handle received answer |
||
225 | * |
||
226 | * TODO: Make it use hash table |
||
227 | * |
||
228 | * @param callid Callid (with first bit set) of the answered call |
||
229 | */ |
||
1091 | palkovsky | 230 | static void handle_answer(ipc_callid_t callid, ipc_call_t *data) |
999 | palkovsky | 231 | { |
232 | link_t *item; |
||
233 | async_call_t *call; |
||
234 | |||
235 | callid &= ~IPC_CALLID_ANSWERED; |
||
236 | |||
1350 | palkovsky | 237 | futex_down(&ipc_futex); |
999 | palkovsky | 238 | for (item = dispatched_calls.next; item != &dispatched_calls; |
239 | item = item->next) { |
||
240 | call = list_get_instance(item, async_call_t, list); |
||
241 | if (call->u.callid == callid) { |
||
242 | list_remove(&call->list); |
||
1350 | palkovsky | 243 | futex_up(&ipc_futex); |
999 | palkovsky | 244 | call->callback(call->private, |
245 | IPC_GET_RETVAL(*data), |
||
246 | data); |
||
247 | return; |
||
248 | } |
||
249 | } |
||
1350 | palkovsky | 250 | futex_up(&ipc_futex); |
999 | palkovsky | 251 | printf("Received unidentified answer: %P!!!\n", callid); |
252 | } |
||
253 | |||
254 | |||
1365 | jermar | 255 | /** Unconditionally wait for an IPC call. |
954 | palkovsky | 256 | * |
257 | * - dispatch ASYNC reoutines in the background |
||
1365 | jermar | 258 | * @param call Space where the message is stored |
259 | * @return Callid of the answer. |
||
954 | palkovsky | 260 | */ |
1365 | jermar | 261 | ipc_callid_t ipc_wait_for_call(ipc_call_t *call) |
954 | palkovsky | 262 | { |
263 | ipc_callid_t callid; |
||
264 | |||
966 | palkovsky | 265 | do { |
999 | palkovsky | 266 | try_dispatch_queued_calls(); |
267 | |||
1365 | jermar | 268 | callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, SYNCH_NO_TIMEOUT, SYNCH_BLOCKING); |
999 | palkovsky | 269 | /* Handle received answers */ |
270 | if (callid & IPC_CALLID_ANSWERED) |
||
1091 | palkovsky | 271 | handle_answer(callid, call); |
966 | palkovsky | 272 | } while (callid & IPC_CALLID_ANSWERED); |
999 | palkovsky | 273 | |
954 | palkovsky | 274 | return callid; |
275 | } |
||
1028 | palkovsky | 276 | |
1365 | jermar | 277 | /** Wait some time for an IPC call. |
278 | * |
||
279 | * - dispatch ASYNC reoutines in the background |
||
280 | * @param call Space where the message is stored |
||
281 | * @param usec Timeout in microseconds. |
||
282 | * @return Callid of the answer. |
||
283 | */ |
||
284 | ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec) |
||
285 | { |
||
286 | ipc_callid_t callid; |
||
287 | |||
288 | do { |
||
289 | try_dispatch_queued_calls(); |
||
290 | |||
291 | callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, SYNCH_BLOCKING); |
||
292 | /* Handle received answers */ |
||
293 | if (callid & IPC_CALLID_ANSWERED) |
||
294 | handle_answer(callid, call); |
||
295 | } while (callid & IPC_CALLID_ANSWERED); |
||
296 | |||
297 | return callid; |
||
298 | } |
||
299 | |||
300 | /** Check if there is an IPC call waiting to be picked up. |
||
301 | * |
||
302 | * - dispatch ASYNC reoutines in the background |
||
303 | * @param call Space where the message is stored |
||
304 | * @return Callid of the answer. |
||
305 | */ |
||
306 | ipc_callid_t ipc_trywait_for_call(ipc_call_t *call) |
||
307 | { |
||
308 | ipc_callid_t callid; |
||
309 | |||
310 | do { |
||
311 | try_dispatch_queued_calls(); |
||
312 | |||
313 | callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t)call, SYNCH_NO_TIMEOUT, SYNCH_NON_BLOCKING); |
||
314 | /* Handle received answers */ |
||
315 | if (callid & IPC_CALLID_ANSWERED) |
||
316 | handle_answer(callid, call); |
||
317 | } while (callid & IPC_CALLID_ANSWERED); |
||
318 | |||
319 | return callid; |
||
320 | } |
||
321 | |||
1091 | palkovsky | 322 | /** Ask destination to do a callback connection |
323 | * |
||
324 | * @return 0 - OK, error code |
||
325 | */ |
||
326 | int ipc_connect_to_me(int phoneid, int arg1, int arg2, ipcarg_t *phone) |
||
1028 | palkovsky | 327 | { |
1091 | palkovsky | 328 | return ipc_call_sync_3(phoneid, IPC_M_CONNECT_TO_ME, arg1, |
329 | arg2, 0, 0, 0, phone); |
||
1028 | palkovsky | 330 | } |
1061 | palkovsky | 331 | |
1091 | palkovsky | 332 | /** Ask through phone for a new connection to some service |
333 | * |
||
334 | * @return new phoneid - OK, error code |
||
335 | */ |
||
1061 | palkovsky | 336 | int ipc_connect_me_to(int phoneid, int arg1, int arg2) |
337 | { |
||
1092 | palkovsky | 338 | ipcarg_t newphid; |
1091 | palkovsky | 339 | int res; |
340 | |||
341 | res = ipc_call_sync_3(phoneid, IPC_M_CONNECT_ME_TO, arg1, |
||
342 | arg2, 0, 0, 0, &newphid); |
||
343 | if (res) |
||
344 | return res; |
||
345 | return newphid; |
||
1061 | palkovsky | 346 | } |
347 | |||
1089 | palkovsky | 348 | /* Hang up specified phone */ |
349 | int ipc_hangup(int phoneid) |
||
350 | { |
||
351 | return __SYSCALL1(SYS_IPC_HANGUP, phoneid); |
||
352 | } |
||
1259 | palkovsky | 353 | |
1282 | palkovsky | 354 | int ipc_register_irq(int irq, irq_code_t *ucode) |
1259 | palkovsky | 355 | { |
1282 | palkovsky | 356 | return __SYSCALL2(SYS_IPC_REGISTER_IRQ, irq, (sysarg_t) ucode); |
1259 | palkovsky | 357 | } |
358 | |||
359 | int ipc_unregister_irq(int irq) |
||
360 | { |
||
361 | return __SYSCALL1(SYS_IPC_UNREGISTER_IRQ, irq); |
||
362 | } |
||
1330 | palkovsky | 363 | |
1336 | jermar | 364 | int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method, ipcarg_t arg1) |
365 | { |
||
366 | return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1); |
||
367 | } |
||
368 | |||
1350 | palkovsky | 369 | |
370 | /** Open shared memory connection over specified phoneid |
||
371 | * |
||
372 | * |
||
1360 | jermar | 373 | * Allocate as_area, notify the other side about our intention |
1350 | palkovsky | 374 | * to open the connection |
375 | * |
||
376 | * @return Connection id identifying this connection |
||
377 | */ |
||
378 | //int ipc_dgr_open(int pohoneid, size_t bufsize) |
||
379 | //{ |
||
380 | /* Find new file descriptor in local descriptor table */ |
||
381 | /* Create AS_area, initialize structures */ |
||
382 | /* Send AS to other side, handle error states */ |
||
383 | |||
384 | //} |
||
1330 | palkovsky | 385 | /* |
1350 | palkovsky | 386 | void ipc_dgr_close(int cid) |
1330 | palkovsky | 387 | { |
388 | } |
||
389 | |||
1350 | palkovsky | 390 | void * ipc_dgr_alloc(int cid, size_t size) |
391 | { |
||
392 | } |
||
1330 | palkovsky | 393 | |
1350 | palkovsky | 394 | void ipc_dgr_free(int cid, void *area) |
395 | { |
||
396 | |||
397 | } |
||
398 | |||
399 | int ipc_dgr_send(int cid, void *area) |
||
400 | { |
||
401 | } |
||
402 | |||
403 | |||
404 | int ipc_dgr_send_data(int cid, void *data, size_t size) |
||
405 | { |
||
406 | } |
||
407 | |||
1330 | palkovsky | 408 | */ |