Rev 1964 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1964 | Rev 1968 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup libc |
29 | /** @addtogroup libc |
30 | * @{ |
30 | * @{ |
31 | * @} |
31 | * @} |
32 | */ |
32 | */ |
33 | 33 | ||
34 | /** @addtogroup libcipc IPC |
34 | /** @addtogroup libcipc IPC |
35 | * @brief HelenOS uspace IPC |
35 | * @brief HelenOS uspace IPC |
36 | * @{ |
36 | * @{ |
37 | * @ingroup libc |
37 | * @ingroup libc |
38 | */ |
38 | */ |
39 | /** @file |
39 | /** @file |
40 | */ |
40 | */ |
41 | 41 | ||
42 | #include <ipc/ipc.h> |
42 | #include <ipc/ipc.h> |
43 | #include <libc.h> |
43 | #include <libc.h> |
44 | #include <malloc.h> |
44 | #include <malloc.h> |
45 | #include <errno.h> |
45 | #include <errno.h> |
46 | #include <libadt/list.h> |
46 | #include <libadt/list.h> |
47 | #include <stdio.h> |
47 | #include <stdio.h> |
48 | #include <unistd.h> |
48 | #include <unistd.h> |
49 | #include <futex.h> |
49 | #include <futex.h> |
50 | #include <kernel/synch/synch.h> |
50 | #include <kernel/synch/synch.h> |
51 | #include <async.h> |
51 | #include <async.h> |
52 | #include <psthread.h> |
52 | #include <psthread.h> |
53 | 53 | ||
54 | /** Structure used for keeping track of sent async msgs |
54 | /** Structure used for keeping track of sent async msgs |
55 | * and queing unsent msgs |
55 | * and queing unsent msgs |
56 | * |
56 | * |
57 | */ |
57 | */ |
58 | typedef struct { |
58 | typedef struct { |
59 | link_t list; |
59 | link_t list; |
60 | 60 | ||
61 | ipc_async_callback_t callback; |
61 | ipc_async_callback_t callback; |
62 | void *private; |
62 | void *private; |
63 | union { |
63 | union { |
64 | ipc_callid_t callid; |
64 | ipc_callid_t callid; |
65 | struct { |
65 | struct { |
66 | ipc_call_t data; |
66 | ipc_call_t data; |
67 | int phoneid; |
67 | int phoneid; |
68 | } msg; |
68 | } msg; |
69 | }u; |
69 | }u; |
70 | pstid_t ptid; /**< Thread waiting for sending this msg */ |
70 | pstid_t ptid; /**< Thread waiting for sending this msg */ |
71 | } async_call_t; |
71 | } async_call_t; |
72 | 72 | ||
73 | LIST_INITIALIZE(dispatched_calls); |
73 | LIST_INITIALIZE(dispatched_calls); |
74 | 74 | ||
75 | /* queued_calls is protcted by async_futex, because if the |
75 | /* queued_calls is protcted by async_futex, because if the |
76 | * call cannot be sent into kernel, async framework is used |
76 | * call cannot be sent into kernel, async framework is used |
77 | * automatically |
77 | * automatically |
78 | */ |
78 | */ |
79 | LIST_INITIALIZE(queued_calls); /**< List of async calls that were not accepted |
79 | LIST_INITIALIZE(queued_calls); /**< List of async calls that were not accepted |
80 | * by kernel */ |
80 | * by kernel */ |
81 | 81 | ||
82 | static atomic_t ipc_futex = FUTEX_INITIALIZER; |
82 | static atomic_t ipc_futex = FUTEX_INITIALIZER; |
83 | 83 | ||
84 | int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1, |
84 | int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1, |
85 | ipcarg_t *result) |
85 | ipcarg_t *result) |
86 | { |
86 | { |
87 | ipc_call_t resdata; |
87 | ipc_call_t resdata; |
88 | int callres; |
88 | int callres; |
89 | 89 | ||
90 | callres = __SYSCALL4(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1, |
90 | callres = __SYSCALL4(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1, |
91 | (sysarg_t)&resdata); |
91 | (sysarg_t)&resdata); |
92 | if (callres) |
92 | if (callres) |
93 | return callres; |
93 | return callres; |
94 | if (result) |
94 | if (result) |
95 | *result = IPC_GET_ARG1(resdata); |
95 | *result = IPC_GET_ARG1(resdata); |
96 | return IPC_GET_RETVAL(resdata); |
96 | return IPC_GET_RETVAL(resdata); |
97 | } |
97 | } |
98 | 98 | ||
99 | int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1, |
99 | int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1, |
100 | ipcarg_t arg2, ipcarg_t arg3, |
100 | ipcarg_t arg2, ipcarg_t arg3, |
101 | ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3) |
101 | ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3) |
102 | { |
102 | { |
103 | ipc_call_t data; |
103 | ipc_call_t data; |
104 | int callres; |
104 | int callres; |
105 | 105 | ||
106 | IPC_SET_METHOD(data, method); |
106 | IPC_SET_METHOD(data, method); |
107 | IPC_SET_ARG1(data, arg1); |
107 | IPC_SET_ARG1(data, arg1); |
108 | IPC_SET_ARG2(data, arg2); |
108 | IPC_SET_ARG2(data, arg2); |
109 | IPC_SET_ARG3(data, arg3); |
109 | IPC_SET_ARG3(data, arg3); |
110 | 110 | ||
111 | callres = __SYSCALL3(SYS_IPC_CALL_SYNC, phoneid, (sysarg_t)&data, |
111 | callres = __SYSCALL3(SYS_IPC_CALL_SYNC, phoneid, (sysarg_t)&data, |
112 | (sysarg_t)&data); |
112 | (sysarg_t)&data); |
113 | if (callres) |
113 | if (callres) |
114 | return callres; |
114 | return callres; |
115 | 115 | ||
116 | if (result1) |
116 | if (result1) |
117 | *result1 = IPC_GET_ARG1(data); |
117 | *result1 = IPC_GET_ARG1(data); |
118 | if (result2) |
118 | if (result2) |
119 | *result2 = IPC_GET_ARG2(data); |
119 | *result2 = IPC_GET_ARG2(data); |
120 | if (result3) |
120 | if (result3) |
121 | *result3 = IPC_GET_ARG3(data); |
121 | *result3 = IPC_GET_ARG3(data); |
122 | return IPC_GET_RETVAL(data); |
122 | return IPC_GET_RETVAL(data); |
123 | } |
123 | } |
124 | 124 | ||
125 | /** Syscall to send asynchronous message */ |
125 | /** Syscall to send asynchronous message */ |
126 | static ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data) |
126 | static ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data) |
127 | { |
127 | { |
128 | return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t)data); |
128 | return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t)data); |
129 | } |
129 | } |
130 | 130 | ||
131 | /** Prolog to ipc_async_send functions */ |
131 | /** Prolog to ipc_async_send functions */ |
132 | static inline async_call_t *ipc_prepare_async(void *private, ipc_async_callback_t callback) |
132 | static inline async_call_t *ipc_prepare_async(void *private, ipc_async_callback_t callback) |
133 | { |
133 | { |
134 | async_call_t *call; |
134 | async_call_t *call; |
135 | 135 | ||
136 | call = malloc(sizeof(*call)); |
136 | call = malloc(sizeof(*call)); |
137 | if (!call) { |
137 | if (!call) { |
138 | if (callback) |
138 | if (callback) |
139 | callback(private, ENOMEM, NULL); |
139 | callback(private, ENOMEM, NULL); |
140 | return NULL; |
140 | return NULL; |
141 | } |
141 | } |
142 | call->callback = callback; |
142 | call->callback = callback; |
143 | call->private = private; |
143 | call->private = private; |
144 | 144 | ||
145 | return call; |
145 | return call; |
146 | } |
146 | } |
147 | 147 | ||
148 | /** Epilogue of ipc_async_send functions */ |
148 | /** Epilogue of ipc_async_send functions */ |
149 | static inline void ipc_finish_async(ipc_callid_t callid, int phoneid, |
149 | static inline void ipc_finish_async(ipc_callid_t callid, int phoneid, |
150 | async_call_t *call, int can_preempt) |
150 | async_call_t *call, int can_preempt) |
151 | { |
151 | { |
152 | if (callid == IPC_CALLRET_FATAL) { |
152 | if (callid == IPC_CALLRET_FATAL) { |
153 | futex_up(&ipc_futex); |
153 | futex_up(&ipc_futex); |
154 | /* Call asynchronous handler with error code */ |
154 | /* Call asynchronous handler with error code */ |
155 | if (call->callback) |
155 | if (call->callback) |
156 | call->callback(call->private, ENOENT, NULL); |
156 | call->callback(call->private, ENOENT, NULL); |
157 | free(call); |
157 | free(call); |
158 | return; |
158 | return; |
159 | } |
159 | } |
160 | 160 | ||
161 | if (callid == IPC_CALLRET_TEMPORARY) { |
161 | if (callid == IPC_CALLRET_TEMPORARY) { |
162 | futex_up(&ipc_futex); |
162 | futex_up(&ipc_futex); |
163 | 163 | ||
164 | call->u.msg.phoneid = phoneid; |
164 | call->u.msg.phoneid = phoneid; |
165 | 165 | ||
166 | futex_down(&async_futex); |
166 | futex_down(&async_futex); |
167 | list_append(&call->list, &queued_calls); |
167 | list_append(&call->list, &queued_calls); |
168 | 168 | ||
169 | if (can_preempt) { |
169 | if (can_preempt) { |
170 | call->ptid = psthread_get_id(); |
170 | call->ptid = psthread_get_id(); |
171 | psthread_schedule_next_adv(PS_TO_MANAGER); |
171 | psthread_schedule_next_adv(PS_TO_MANAGER); |
172 | /* Async futex unlocked by previous call */ |
172 | /* Async futex unlocked by previous call */ |
173 | } else { |
173 | } else { |
174 | call->ptid = 0; |
174 | call->ptid = 0; |
175 | futex_up(&async_futex); |
175 | futex_up(&async_futex); |
176 | } |
176 | } |
177 | return; |
177 | return; |
178 | } |
178 | } |
179 | call->u.callid = callid; |
179 | call->u.callid = callid; |
180 | /* Add call to list of dispatched calls */ |
180 | /* Add call to list of dispatched calls */ |
181 | list_append(&call->list, &dispatched_calls); |
181 | list_append(&call->list, &dispatched_calls); |
182 | futex_up(&ipc_futex); |
182 | futex_up(&ipc_futex); |
183 | 183 | ||
184 | } |
184 | } |
185 | 185 | ||
186 | /** Send asynchronous message |
186 | /** Send asynchronous message |
187 | * |
187 | * |
188 | * - if fatal error, call callback handler with proper error code |
188 | * - if fatal error, call callback handler with proper error code |
189 | * - if message cannot be temporarily sent, add to queue |
189 | * - if message cannot be temporarily sent, add to queue |
190 | */ |
190 | */ |
191 | void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1, |
191 | void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1, |
192 | ipcarg_t arg2, void *private, |
192 | ipcarg_t arg2, void *private, |
193 | ipc_async_callback_t callback, int can_preempt) |
193 | ipc_async_callback_t callback, int can_preempt) |
194 | { |
194 | { |
195 | async_call_t *call; |
195 | async_call_t *call; |
196 | ipc_callid_t callid; |
196 | ipc_callid_t callid; |
197 | 197 | ||
198 | call = ipc_prepare_async(private, callback); |
198 | call = ipc_prepare_async(private, callback); |
199 | if (!call) |
199 | if (!call) |
200 | return; |
200 | return; |
201 | 201 | ||
202 | /* We need to make sure that we get callid before |
202 | /* We need to make sure that we get callid before |
203 | * another thread accesses the queue again */ |
203 | * another thread accesses the queue again */ |
204 | futex_down(&ipc_futex); |
204 | futex_down(&ipc_futex); |
205 | callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2); |
205 | callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2); |
206 | 206 | ||
207 | if (callid == IPC_CALLRET_TEMPORARY) { |
207 | if (callid == IPC_CALLRET_TEMPORARY) { |
208 | IPC_SET_METHOD(call->u.msg.data, method); |
208 | IPC_SET_METHOD(call->u.msg.data, method); |
209 | IPC_SET_ARG1(call->u.msg.data, arg1); |
209 | IPC_SET_ARG1(call->u.msg.data, arg1); |
210 | IPC_SET_ARG2(call->u.msg.data, arg2); |
210 | IPC_SET_ARG2(call->u.msg.data, arg2); |
211 | } |
211 | } |
212 | ipc_finish_async(callid, phoneid, call, can_preempt); |
212 | ipc_finish_async(callid, phoneid, call, can_preempt); |
213 | } |
213 | } |
214 | 214 | ||
215 | /** Send asynchronous message |
215 | /** Send asynchronous message |
216 | * |
216 | * |
217 | * - if fatal error, call callback handler with proper error code |
217 | * - if fatal error, call callback handler with proper error code |
218 | * - if message cannot be temporarily sent, add to queue |
218 | * - if message cannot be temporarily sent, add to queue |
219 | */ |
219 | */ |
220 | void ipc_call_async_3(int phoneid, ipcarg_t method, ipcarg_t arg1, |
220 | void ipc_call_async_3(int phoneid, ipcarg_t method, ipcarg_t arg1, |
221 | ipcarg_t arg2, ipcarg_t arg3, void *private, |
221 | ipcarg_t arg2, ipcarg_t arg3, void *private, |
222 | ipc_async_callback_t callback, int can_preempt) |
222 | ipc_async_callback_t callback, int can_preempt) |
223 | { |
223 | { |
224 | async_call_t *call; |
224 | async_call_t *call; |
225 | ipc_callid_t callid; |
225 | ipc_callid_t callid; |
226 | 226 | ||
227 | call = ipc_prepare_async(private, callback); |
227 | call = ipc_prepare_async(private, callback); |
228 | if (!call) |
228 | if (!call) |
229 | return; |
229 | return; |
230 | 230 | ||
231 | IPC_SET_METHOD(call->u.msg.data, method); |
231 | IPC_SET_METHOD(call->u.msg.data, method); |
232 | IPC_SET_ARG1(call->u.msg.data, arg1); |
232 | IPC_SET_ARG1(call->u.msg.data, arg1); |
233 | IPC_SET_ARG2(call->u.msg.data, arg2); |
233 | IPC_SET_ARG2(call->u.msg.data, arg2); |
234 | IPC_SET_ARG3(call->u.msg.data, arg3); |
234 | IPC_SET_ARG3(call->u.msg.data, arg3); |
235 | /* We need to make sure that we get callid before |
235 | /* We need to make sure that we get callid before |
236 | * another thread accesses the queue again */ |
236 | * another thread accesses the queue again */ |
237 | futex_down(&ipc_futex); |
237 | futex_down(&ipc_futex); |
238 | callid = _ipc_call_async(phoneid, &call->u.msg.data); |
238 | callid = _ipc_call_async(phoneid, &call->u.msg.data); |
239 | 239 | ||
240 | ipc_finish_async(callid, phoneid, call, can_preempt); |
240 | ipc_finish_async(callid, phoneid, call, can_preempt); |
241 | } |
241 | } |
242 | 242 | ||
243 | 243 | ||
244 | /** Send a fast answer to a received call. |
244 | /** Send a fast answer to a received call. |
245 | * |
245 | * |
246 | * The fast answer makes use of passing retval and first two arguments in registers. |
246 | * The fast answer makes use of passing retval and first two arguments in registers. |
247 | * If you need to return more, use the ipc_answer() instead. |
247 | * If you need to return more, use the ipc_answer() instead. |
248 | * |
248 | * |
249 | * @param callid ID of the call being answered. |
249 | * @param callid ID of the call being answered. |
250 | * @param retval Return value. |
250 | * @param retval Return value. |
251 | * @param arg1 First return argument. |
251 | * @param arg1 First return argument. |
252 | * @param arg2 Second return argument. |
252 | * @param arg2 Second return argument. |
253 | * |
253 | * |
254 | * @return Zero on success or a value from @ref errno.h on failure. |
254 | * @return Zero on success or a value from @ref errno.h on failure. |
255 | */ |
255 | */ |
256 | ipcarg_t ipc_answer_fast(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1, |
256 | ipcarg_t ipc_answer_fast(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1, |
257 | ipcarg_t arg2) |
257 | ipcarg_t arg2) |
258 | { |
258 | { |
259 | return __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2); |
259 | return __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2); |
260 | } |
260 | } |
261 | 261 | ||
262 | /** Send a full answer to a received call. |
262 | /** Send a full answer to a received call. |
263 | * |
263 | * |
264 | * @param callid ID of the call being answered. |
264 | * @param callid ID of the call being answered. |
265 | * @param call Call data. Must be already initialized by the responder. |
265 | * @param call Call data. Must be already initialized by the responder. |
266 | * |
266 | * |
267 | * @return Zero on success or a value from @ref errno.h on failure. |
267 | * @return Zero on success or a value from @ref errno.h on failure. |
268 | */ |
268 | */ |
269 | ipcarg_t ipc_answer(ipc_callid_t callid, ipc_call_t *call) |
269 | ipcarg_t ipc_answer(ipc_callid_t callid, ipc_call_t *call) |
270 | { |
270 | { |
271 | return __SYSCALL2(SYS_IPC_ANSWER, callid, (sysarg_t) call); |
271 | return __SYSCALL2(SYS_IPC_ANSWER, callid, (sysarg_t) call); |
272 | } |
272 | } |
273 | 273 | ||
274 | 274 | ||
275 | /** Try to dispatch queed calls from async queue */ |
275 | /** Try to dispatch queed calls from async queue */ |
276 | static void try_dispatch_queued_calls(void) |
276 | static void try_dispatch_queued_calls(void) |
277 | { |
277 | { |
278 | async_call_t *call; |
278 | async_call_t *call; |
279 | ipc_callid_t callid; |
279 | ipc_callid_t callid; |
280 | 280 | ||
281 | /* TODO: integrate intelligently ipc_futex, so that it |
281 | /* TODO: integrate intelligently ipc_futex, so that it |
282 | * is locked during ipc_call_async, until it is added |
282 | * is locked during ipc_call_async, until it is added |
283 | * to dispatched_calls |
283 | * to dispatched_calls |
284 | */ |
284 | */ |
285 | futex_down(&async_futex); |
285 | futex_down(&async_futex); |
286 | while (!list_empty(&queued_calls)) { |
286 | while (!list_empty(&queued_calls)) { |
287 | call = list_get_instance(queued_calls.next, async_call_t, |
287 | call = list_get_instance(queued_calls.next, async_call_t, |
288 | list); |
288 | list); |
289 | 289 | ||
290 | callid = _ipc_call_async(call->u.msg.phoneid, |
290 | callid = _ipc_call_async(call->u.msg.phoneid, |
291 | &call->u.msg.data); |
291 | &call->u.msg.data); |
292 | if (callid == IPC_CALLRET_TEMPORARY) { |
292 | if (callid == IPC_CALLRET_TEMPORARY) { |
293 | break; |
293 | break; |
294 | } |
294 | } |
295 | list_remove(&call->list); |
295 | list_remove(&call->list); |
296 | 296 | ||
297 | futex_up(&async_futex); |
297 | futex_up(&async_futex); |
298 | if (call->ptid) |
298 | if (call->ptid) |
299 | psthread_add_ready(call->ptid); |
299 | psthread_add_ready(call->ptid); |
300 | 300 | ||
301 | if (callid == IPC_CALLRET_FATAL) { |
301 | if (callid == IPC_CALLRET_FATAL) { |
302 | if (call->callback) |
302 | if (call->callback) |
303 | call->callback(call->private, ENOENT, NULL); |
303 | call->callback(call->private, ENOENT, NULL); |
304 | free(call); |
304 | free(call); |
305 | } else { |
305 | } else { |
306 | call->u.callid = callid; |
306 | call->u.callid = callid; |
307 | futex_down(&ipc_futex); |
307 | futex_down(&ipc_futex); |
308 | list_append(&call->list, &dispatched_calls); |
308 | list_append(&call->list, &dispatched_calls); |
309 | futex_up(&ipc_futex); |
309 | futex_up(&ipc_futex); |
310 | } |
310 | } |
311 | futex_down(&async_futex); |
311 | futex_down(&async_futex); |
312 | } |
312 | } |
313 | futex_up(&async_futex); |
313 | futex_up(&async_futex); |
314 | } |
314 | } |
315 | 315 | ||
316 | /** Handle received answer |
316 | /** Handle received answer |
317 | * |
317 | * |
318 | * TODO: Make it use hash table |
318 | * TODO: Make it use hash table |
319 | * |
319 | * |
320 | * @param callid Callid (with first bit set) of the answered call |
320 | * @param callid Callid (with first bit set) of the answered call |
321 | */ |
321 | */ |
322 | static void handle_answer(ipc_callid_t callid, ipc_call_t *data) |
322 | static void handle_answer(ipc_callid_t callid, ipc_call_t *data) |
323 | { |
323 | { |
324 | link_t *item; |
324 | link_t *item; |
325 | async_call_t *call; |
325 | async_call_t *call; |
326 | 326 | ||
327 | callid &= ~IPC_CALLID_ANSWERED; |
327 | callid &= ~IPC_CALLID_ANSWERED; |
328 | 328 | ||
329 | futex_down(&ipc_futex); |
329 | futex_down(&ipc_futex); |
330 | for (item = dispatched_calls.next; item != &dispatched_calls; |
330 | for (item = dispatched_calls.next; item != &dispatched_calls; |
331 | item = item->next) { |
331 | item = item->next) { |
332 | call = list_get_instance(item, async_call_t, list); |
332 | call = list_get_instance(item, async_call_t, list); |
333 | if (call->u.callid == callid) { |
333 | if (call->u.callid == callid) { |
334 | list_remove(&call->list); |
334 | list_remove(&call->list); |
335 | futex_up(&ipc_futex); |
335 | futex_up(&ipc_futex); |
336 | if (call->callback) |
336 | if (call->callback) |
337 | call->callback(call->private, |
337 | call->callback(call->private, |
338 | IPC_GET_RETVAL(*data), |
338 | IPC_GET_RETVAL(*data), |
339 | data); |
339 | data); |
340 | free(call); |
340 | free(call); |
341 | return; |
341 | return; |
342 | } |
342 | } |
343 | } |
343 | } |
344 | futex_up(&ipc_futex); |
344 | futex_up(&ipc_futex); |
345 | printf("Received unidentified answer: %P!!!\n", callid); |
345 | printf("Received unidentified answer: %P!!!\n", callid); |
346 | } |
346 | } |
347 | 347 | ||
348 | 348 | ||
349 | /** One cycle of ipc wait for call call |
349 | /** One cycle of ipc wait for call call |
350 | * |
350 | * |
351 | * - dispatch ASYNC reoutines in the background |
351 | * - dispatch ASYNC reoutines in the background |
352 | * @param call Space where the message is stored |
352 | * @param call Space where the message is stored |
353 | * @param usec Timeout in microseconds |
353 | * @param usec Timeout in microseconds |
354 | * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking) |
354 | * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking) |
355 | * @return Callid of the answer. |
355 | * @return Callid of the answer. |
356 | */ |
356 | */ |
357 | ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags) |
357 | ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags) |
358 | { |
358 | { |
359 | ipc_callid_t callid; |
359 | ipc_callid_t callid; |
360 | 360 | ||
361 | callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags); |
361 | callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags); |
362 | /* Handle received answers */ |
362 | /* Handle received answers */ |
363 | if (callid & IPC_CALLID_ANSWERED) { |
363 | if (callid & IPC_CALLID_ANSWERED) { |
364 | handle_answer(callid, call); |
364 | handle_answer(callid, call); |
365 | try_dispatch_queued_calls(); |
365 | try_dispatch_queued_calls(); |
366 | } |
366 | } |
367 | 367 | ||
368 | return callid; |
368 | return callid; |
369 | } |
369 | } |
370 | 370 | ||
371 | /** Wait some time for an IPC call. |
371 | /** Wait some time for an IPC call. |
372 | * |
372 | * |
373 | * - dispatch ASYNC reoutines in the background |
373 | * - dispatch ASYNC reoutines in the background |
374 | * @param call Space where the message is stored |
374 | * @param call Space where the message is stored |
375 | * @param usec Timeout in microseconds. |
375 | * @param usec Timeout in microseconds. |
376 | * @return Callid of the answer. |
376 | * @return Callid of the answer. |
377 | */ |
377 | */ |
378 | ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec) |
378 | ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec) |
379 | { |
379 | { |
380 | ipc_callid_t callid; |
380 | ipc_callid_t callid; |
381 | 381 | ||
382 | do { |
382 | do { |
383 | callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE); |
383 | callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE); |
384 | } while (callid & IPC_CALLID_ANSWERED); |
384 | } while (callid & IPC_CALLID_ANSWERED); |
385 | 385 | ||
386 | return callid; |
386 | return callid; |
387 | } |
387 | } |
388 | 388 | ||
389 | /** Check if there is an IPC call waiting to be picked up. |
389 | /** Check if there is an IPC call waiting to be picked up. |
390 | * |
390 | * |
391 | * - dispatch ASYNC reoutines in the background |
391 | * - dispatch ASYNC reoutines in the background |
392 | * @param call Space where the message is stored |
392 | * @param call Space where the message is stored |
393 | * @return Callid of the answer. |
393 | * @return Callid of the answer. |
394 | */ |
394 | */ |
395 | ipc_callid_t ipc_trywait_for_call(ipc_call_t *call) |
395 | ipc_callid_t ipc_trywait_for_call(ipc_call_t *call) |
396 | { |
396 | { |
397 | ipc_callid_t callid; |
397 | ipc_callid_t callid; |
398 | 398 | ||
399 | do { |
399 | do { |
400 | callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NON_BLOCKING); |
400 | callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NON_BLOCKING); |
401 | } while (callid & IPC_CALLID_ANSWERED); |
401 | } while (callid & IPC_CALLID_ANSWERED); |
402 | 402 | ||
403 | return callid; |
403 | return callid; |
404 | } |
404 | } |
405 | 405 | ||
406 | /** Ask destination to do a callback connection |
406 | /** Ask destination to do a callback connection |
407 | * |
407 | * |
408 | * @return 0 - OK, error code |
408 | * @return 0 - OK, error code |
409 | */ |
409 | */ |
410 | int ipc_connect_to_me(int phoneid, int arg1, int arg2, ipcarg_t *phone) |
410 | int ipc_connect_to_me(int phoneid, int arg1, int arg2, ipcarg_t *phone) |
411 | { |
411 | { |
412 | return ipc_call_sync_3(phoneid, IPC_M_CONNECT_TO_ME, arg1, |
412 | return ipc_call_sync_3(phoneid, IPC_M_CONNECT_TO_ME, arg1, |
413 | arg2, 0, 0, 0, phone); |
413 | arg2, 0, 0, 0, phone); |
414 | } |
414 | } |
415 | 415 | ||
416 | /** Ask through phone for a new connection to some service |
416 | /** Ask through phone for a new connection to some service |
417 | * |
417 | * |
418 | * @return new phoneid - OK, error code |
418 | * @return new phoneid - OK, error code |
419 | */ |
419 | */ |
420 | int ipc_connect_me_to(int phoneid, int arg1, int arg2) |
420 | int ipc_connect_me_to(int phoneid, int arg1, int arg2) |
421 | { |
421 | { |
422 | ipcarg_t newphid; |
422 | ipcarg_t newphid; |
423 | int res; |
423 | int res; |
424 | 424 | ||
425 | res = ipc_call_sync_3(phoneid, IPC_M_CONNECT_ME_TO, arg1, |
425 | res = ipc_call_sync_3(phoneid, IPC_M_CONNECT_ME_TO, arg1, |
426 | arg2, 0, 0, 0, &newphid); |
426 | arg2, 0, 0, 0, &newphid); |
427 | if (res) |
427 | if (res) |
428 | return res; |
428 | return res; |
429 | return newphid; |
429 | return newphid; |
430 | } |
430 | } |
431 | 431 | ||
432 | /* Hang up specified phone */ |
432 | /* Hang up specified phone */ |
433 | int ipc_hangup(int phoneid) |
433 | int ipc_hangup(int phoneid) |
434 | { |
434 | { |
435 | return __SYSCALL1(SYS_IPC_HANGUP, phoneid); |
435 | return __SYSCALL1(SYS_IPC_HANGUP, phoneid); |
436 | } |
436 | } |
437 | 437 | ||
438 | int ipc_register_irq(int irq, irq_code_t *ucode) |
438 | int ipc_register_irq(int irq, irq_code_t *ucode) |
439 | { |
439 | { |
440 | return __SYSCALL2(SYS_IPC_REGISTER_IRQ, irq, (sysarg_t) ucode); |
440 | return __SYSCALL2(SYS_IPC_REGISTER_IRQ, irq, (sysarg_t) ucode); |
441 | } |
441 | } |
442 | 442 | ||
443 | int ipc_unregister_irq(int irq) |
443 | int ipc_unregister_irq(int irq) |
444 | { |
444 | { |
445 | return __SYSCALL1(SYS_IPC_UNREGISTER_IRQ, irq); |
445 | return __SYSCALL1(SYS_IPC_UNREGISTER_IRQ, irq); |
446 | } |
446 | } |
447 | 447 | ||
448 | int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method, ipcarg_t arg1) |
448 | int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method, ipcarg_t arg1) |
449 | { |
449 | { |
450 | return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1); |
450 | return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1); |
451 | } |
451 | } |
452 | 452 | ||
453 | 453 | ||
454 | 454 | ||
455 | /** @} |
455 | /** @} |
456 | */ |
456 | */ |
457 | 457 | ||
458 | 458 | ||
459 | 459 |