Rev 1063 | Rev 1084 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1063 | Rev 1072 | ||
---|---|---|---|
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 | #include <arch.h> |
29 | #include <arch.h> |
30 | #include <proc/task.h> |
30 | #include <proc/task.h> |
31 | 31 | ||
32 | #include <errno.h> |
32 | #include <errno.h> |
33 | #include <mm/page.h> |
33 | #include <mm/page.h> |
34 | #include <memstr.h> |
34 | #include <memstr.h> |
35 | #include <debug.h> |
35 | #include <debug.h> |
36 | #include <ipc/ipc.h> |
36 | #include <ipc/ipc.h> |
37 | #include <ipc/sysipc.h> |
37 | #include <ipc/sysipc.h> |
- | 38 | #include <ipc/ipcrsc.h> |
|
38 | 39 | ||
39 | 40 | ||
40 | #include <print.h> |
41 | #include <print.h> |
41 | #include <arch.h> |
42 | #include <arch.h> |
42 | #include <proc/thread.h> |
43 | #include <proc/thread.h> |
43 | 44 | ||
44 | /* TODO: multi-threaded connect-to-me can cause race condition |
45 | /* TODO: multi-threaded connect-to-me can cause race condition |
45 | * on phone, add counter + thread_kill?? |
46 | * on phone, add counter + thread_kill?? |
46 | * |
47 | * |
47 | */ |
48 | */ |
48 | 49 | ||
49 | /** Return true if the method is a system method */ |
50 | /** Return true if the method is a system method */ |
50 | static inline int is_system_method(__native method) |
51 | static inline int is_system_method(__native method) |
51 | { |
52 | { |
52 | if (method <= IPC_M_LAST_SYSTEM) |
53 | if (method <= IPC_M_LAST_SYSTEM) |
53 | return 1; |
54 | return 1; |
54 | return 0; |
55 | return 0; |
55 | } |
56 | } |
56 | 57 | ||
57 | /** Return true if the message with this method is forwardable |
58 | /** Return true if the message with this method is forwardable |
58 | * |
59 | * |
59 | * - some system messages may be forwarded, for some of them |
60 | * - some system messages may be forwarded, for some of them |
60 | * it is useless |
61 | * it is useless |
61 | */ |
62 | */ |
62 | static inline int is_forwardable(__native method) |
63 | static inline int is_forwardable(__native method) |
63 | { |
64 | { |
64 | return 1; |
65 | return 1; |
65 | } |
66 | } |
66 | 67 | ||
67 | /** Find call_t * in call table according to callid |
- | |
68 | * |
- | |
69 | * @return NULL on not found, otherwise pointer to call structure |
- | |
70 | */ |
- | |
71 | static inline call_t * get_call(__native callid) |
- | |
72 | { |
- | |
73 | /* TODO: Traverse list of dispatched calls and find one */ |
- | |
74 | /* TODO: locking of call, ripping it from dispatched calls etc. */ |
- | |
75 | return (call_t *) callid; |
- | |
76 | } |
- | |
77 | - | ||
78 | /** Return pointer to phone identified by phoneid or NULL if non-existent */ |
- | |
79 | static phone_t * get_phone(__native phoneid) |
- | |
80 | { |
- | |
81 | phone_t *phone; |
- | |
82 | - | ||
83 | if (phoneid >= IPC_MAX_PHONES) |
- | |
84 | return NULL; |
- | |
85 | - | ||
86 | phone = &TASK->phones[phoneid]; |
- | |
87 | if (!phone->callee) |
- | |
88 | return NULL; |
- | |
89 | return phone; |
- | |
90 | } |
- | |
91 | - | ||
92 | /** Allocate new phone slot in current TASK structure */ |
- | |
93 | static int phone_alloc(void) |
- | |
94 | { |
- | |
95 | int i; |
- | |
96 | - | ||
97 | spinlock_lock(&TASK->lock); |
- | |
98 | - | ||
99 | for (i=0; i < IPC_MAX_PHONES; i++) { |
- | |
100 | if (!TASK->phones[i].busy) { |
- | |
101 | TASK->phones[i].busy = 1; |
- | |
102 | break; |
- | |
103 | } |
- | |
104 | } |
- | |
105 | spinlock_unlock(&TASK->lock); |
- | |
106 | - | ||
107 | if (i >= IPC_MAX_PHONES) |
- | |
108 | return -1; |
- | |
109 | return i; |
- | |
110 | } |
- | |
111 | - | ||
112 | /** Disconnect phone */ |
- | |
113 | static void phone_dealloc(int phoneid) |
- | |
114 | { |
- | |
115 | spinlock_lock(&TASK->lock); |
- | |
116 | - | ||
117 | ASSERT(TASK->phones[phoneid].busy); |
- | |
118 | - | ||
119 | if (TASK->phones[phoneid].callee) |
- | |
120 | ipc_phone_destroy(&TASK->phones[phoneid]); |
- | |
121 | - | ||
122 | TASK->phones[phoneid].busy = 0; |
- | |
123 | spinlock_unlock(&TASK->lock); |
- | |
124 | } |
- | |
125 | - | ||
126 | static void phone_connect(int phoneid, answerbox_t *box) |
- | |
127 | { |
- | |
128 | phone_t *phone = &TASK->phones[phoneid]; |
- | |
129 | - | ||
130 | ipc_phone_connect(phone, box); |
- | |
131 | } |
- | |
132 | - | ||
133 | /****************************************************/ |
68 | /****************************************************/ |
134 | /* Functions that preprocess answer before sending |
69 | /* Functions that preprocess answer before sending |
135 | * it to the recepient |
70 | * it to the recepient |
136 | */ |
71 | */ |
137 | 72 | ||
138 | /** Return true if the caller (ipc_answer) should save |
73 | /** Return true if the caller (ipc_answer) should save |
139 | * the old call contents and call answer_preprocess |
74 | * the old call contents and call answer_preprocess |
140 | */ |
75 | */ |
141 | static inline int answer_will_preprocess(call_t *call) |
76 | static inline int answer_will_preprocess(call_t *call) |
142 | { |
77 | { |
143 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) |
78 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) |
144 | return 1; |
79 | return 1; |
145 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTMETO) |
80 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTMETO) |
146 | return 1; |
81 | return 1; |
147 | return 0; |
82 | return 0; |
148 | } |
83 | } |
149 | 84 | ||
150 | /** Interpret process answer as control information */ |
85 | /** Interpret process answer as control information */ |
151 | static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata) |
86 | static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata) |
152 | { |
87 | { |
153 | int phoneid; |
88 | int phoneid; |
154 | 89 | ||
155 | if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTTOME) { |
90 | if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTTOME) { |
156 | phoneid = IPC_GET_ARG3(*olddata); |
91 | phoneid = IPC_GET_ARG3(*olddata); |
157 | if (IPC_GET_RETVAL(answer->data)) { |
92 | if (IPC_GET_RETVAL(answer->data)) { |
158 | /* The connection was not accepted */ |
93 | /* The connection was not accepted */ |
159 | phone_dealloc(phoneid); |
94 | phone_dealloc(phoneid); |
160 | } else { |
95 | } else { |
161 | /* The connection was accepted */ |
96 | /* The connection was accepted */ |
162 | phone_connect(phoneid,&answer->sender->answerbox); |
97 | phone_connect(phoneid,&answer->sender->answerbox); |
163 | } |
98 | } |
164 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTMETO) { |
99 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTMETO) { |
165 | /* If the users accepted call, connect */ |
100 | /* If the users accepted call, connect */ |
166 | if (!IPC_GET_RETVAL(answer->data)) { |
101 | if (!IPC_GET_RETVAL(answer->data)) { |
167 | ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata), |
102 | ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata), |
168 | &TASK->answerbox); |
103 | &TASK->answerbox); |
169 | } |
104 | } |
170 | } |
105 | } |
171 | } |
106 | } |
172 | 107 | ||
173 | /****************************************************/ |
108 | /****************************************************/ |
174 | /* Functions called to process received call/answer |
109 | /* Functions called to process received call/answer |
175 | * before passing to uspace |
110 | * before passing to uspace |
176 | */ |
111 | */ |
177 | 112 | ||
178 | /** Do basic kernel processing of received call answer */ |
113 | /** Do basic kernel processing of received call answer */ |
179 | static int process_answer(answerbox_t *box,call_t *call) |
114 | static int process_answer(answerbox_t *box,call_t *call) |
180 | { |
115 | { |
181 | return 0; |
116 | return 0; |
182 | } |
117 | } |
183 | 118 | ||
184 | /** Do basic kernel processing of received call request |
119 | /** Do basic kernel processing of received call request |
185 | * |
120 | * |
186 | * @return 0 - the call should be passed to userspace, 1 - ignore call |
121 | * @return 0 - the call should be passed to userspace, 1 - ignore call |
187 | */ |
122 | */ |
188 | static int process_request(answerbox_t *box,call_t *call) |
123 | static int process_request(answerbox_t *box,call_t *call) |
189 | { |
124 | { |
190 | int phoneid; |
125 | int phoneid; |
191 | 126 | ||
192 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) { |
127 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) { |
193 | phoneid = phone_alloc(); |
128 | phoneid = phone_alloc(); |
194 | if (phoneid < 0) { /* Failed to allocate phone */ |
129 | if (phoneid < 0) { /* Failed to allocate phone */ |
195 | IPC_SET_RETVAL(call->data, ELIMIT); |
130 | IPC_SET_RETVAL(call->data, ELIMIT); |
196 | ipc_answer(box,call); |
131 | ipc_answer(box,call); |
197 | return -1; |
132 | return -1; |
198 | } |
133 | } |
199 | IPC_SET_ARG3(call->data, phoneid); |
134 | IPC_SET_ARG3(call->data, phoneid); |
200 | } |
135 | } |
201 | return 0; |
136 | return 0; |
202 | } |
137 | } |
203 | 138 | ||
204 | /** Send a call over IPC, wait for reply, return to user |
139 | /** Send a call over IPC, wait for reply, return to user |
205 | * |
140 | * |
206 | * @return Call identification, returns -1 on fatal error, |
141 | * @return Call identification, returns -1 on fatal error, |
207 | -2 on 'Too many async request, handle answers first |
142 | -2 on 'Too many async request, handle answers first |
208 | */ |
143 | */ |
209 | __native sys_ipc_call_sync_fast(__native phoneid, __native method, |
144 | __native sys_ipc_call_sync_fast(__native phoneid, __native method, |
210 | __native arg1, __native *data) |
145 | __native arg1, __native *data) |
211 | { |
146 | { |
212 | call_t call; |
147 | call_t call; |
213 | phone_t *phone; |
148 | phone_t *phone; |
214 | 149 | ||
215 | phone = get_phone(phoneid); |
- | |
216 | if (!phone) |
- | |
217 | return ENOENT; |
- | |
218 | - | ||
219 | if (is_system_method(method)) |
150 | if (is_system_method(method)) |
220 | return EPERM; |
151 | return EPERM; |
221 | 152 | ||
- | 153 | phone = get_phone_and_lock(phoneid); |
|
- | 154 | if (!phone) |
|
- | 155 | return ENOENT; |
|
- | 156 | ||
222 | ipc_call_init(&call); |
157 | ipc_call_init(&call); |
223 | IPC_SET_METHOD(call.data, method); |
158 | IPC_SET_METHOD(call.data, method); |
224 | IPC_SET_ARG1(call.data, arg1); |
159 | IPC_SET_ARG1(call.data, arg1); |
225 | 160 | ||
226 | ipc_call_sync(phone, &call); |
161 | ipc_call_sync(phone, &call); |
227 | 162 | ||
228 | copy_to_uspace(data, &call.data, sizeof(call.data)); |
163 | copy_to_uspace(data, &call.data, sizeof(call.data)); |
229 | 164 | ||
230 | return 0; |
165 | return 0; |
231 | } |
166 | } |
232 | 167 | ||
233 | /** Synchronous IPC call allowing to send whole message */ |
168 | /** Synchronous IPC call allowing to send whole message */ |
234 | __native sys_ipc_call_sync(__native phoneid, __native *question, |
169 | __native sys_ipc_call_sync(__native phoneid, __native *question, |
235 | __native *reply) |
170 | __native *reply) |
236 | { |
171 | { |
237 | call_t call; |
172 | call_t call; |
238 | phone_t *phone; |
173 | phone_t *phone; |
239 | 174 | ||
240 | phone = get_phone(phoneid); |
- | |
241 | if (!phone) |
- | |
242 | return ENOENT; |
- | |
243 | - | ||
244 | ipc_call_init(&call); |
175 | ipc_call_init(&call); |
245 | copy_from_uspace(&call.data, question, sizeof(call.data)); |
176 | copy_from_uspace(&call.data, question, sizeof(call.data)); |
246 | 177 | ||
247 | if (is_system_method(IPC_GET_METHOD(call.data))) |
178 | if (is_system_method(IPC_GET_METHOD(call.data))) |
248 | return EPERM; |
179 | return EPERM; |
249 | 180 | ||
- | 181 | phone = get_phone_and_lock(phoneid); |
|
- | 182 | if (!phone) |
|
- | 183 | return ENOENT; |
|
- | 184 | ||
250 | ipc_call_sync(phone, &call); |
185 | ipc_call_sync(phone, &call); |
251 | 186 | ||
252 | copy_to_uspace(reply, &call.data, sizeof(call.data)); |
187 | copy_to_uspace(reply, &call.data, sizeof(call.data)); |
253 | 188 | ||
254 | return 0; |
189 | return 0; |
255 | } |
190 | } |
256 | 191 | ||
257 | /** Check that the task did not exceed allowed limit |
192 | /** Check that the task did not exceed allowed limit |
258 | * |
193 | * |
259 | * @return 0 - Limit OK, -1 - limit exceeded |
194 | * @return 0 - Limit OK, -1 - limit exceeded |
260 | */ |
195 | */ |
261 | static int check_call_limit(void) |
196 | static int check_call_limit(void) |
262 | { |
197 | { |
263 | if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) { |
198 | if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) { |
264 | atomic_dec(&TASK->active_calls); |
199 | atomic_dec(&TASK->active_calls); |
265 | return -1; |
200 | return -1; |
266 | } |
201 | } |
267 | return 0; |
202 | return 0; |
268 | } |
203 | } |
269 | 204 | ||
270 | /** Send an asynchronous call over ipc |
205 | /** Send an asynchronous call over ipc |
271 | * |
206 | * |
272 | * @return Call identification, returns -1 on fatal error, |
207 | * @return Call identification, returns -1 on fatal error, |
273 | -2 on 'Too many async request, handle answers first |
208 | -2 on 'Too many async request, handle answers first |
274 | */ |
209 | */ |
275 | __native sys_ipc_call_async_fast(__native phoneid, __native method, |
210 | __native sys_ipc_call_async_fast(__native phoneid, __native method, |
276 | __native arg1, __native arg2) |
211 | __native arg1, __native arg2) |
277 | { |
212 | { |
278 | call_t *call; |
213 | call_t *call; |
279 | phone_t *phone; |
214 | phone_t *phone; |
280 | 215 | ||
281 | phone = get_phone(phoneid); |
- | |
282 | if (!phone) |
- | |
283 | return IPC_CALLRET_FATAL; |
- | |
284 | - | ||
285 | if (is_system_method(method)) |
216 | if (is_system_method(method)) |
286 | return IPC_CALLRET_FATAL; |
217 | return IPC_CALLRET_FATAL; |
287 | 218 | ||
288 | if (check_call_limit()) |
219 | if (check_call_limit()) |
289 | return IPC_CALLRET_TEMPORARY; |
220 | return IPC_CALLRET_TEMPORARY; |
290 | 221 | ||
- | 222 | phone = get_phone_and_lock(phoneid); |
|
- | 223 | if (!phone) |
|
- | 224 | return IPC_CALLRET_FATAL; |
|
- | 225 | ||
291 | call = ipc_call_alloc(); |
226 | call = ipc_call_alloc(); |
292 | IPC_SET_METHOD(call->data, method); |
227 | IPC_SET_METHOD(call->data, method); |
293 | IPC_SET_ARG1(call->data, arg1); |
228 | IPC_SET_ARG1(call->data, arg1); |
294 | IPC_SET_ARG2(call->data, arg2); |
229 | IPC_SET_ARG2(call->data, arg2); |
295 | 230 | ||
296 | ipc_call(phone, call); |
231 | ipc_call(phone, call); |
297 | 232 | ||
298 | return (__native) call; |
233 | return (__native) call; |
299 | } |
234 | } |
300 | 235 | ||
301 | /** Synchronous IPC call allowing to send whole message |
236 | /** Synchronous IPC call allowing to send whole message |
302 | * |
237 | * |
303 | * @return The same as sys_ipc_call_async |
238 | * @return The same as sys_ipc_call_async |
304 | */ |
239 | */ |
305 | __native sys_ipc_call_async(__native phoneid, __native *data) |
240 | __native sys_ipc_call_async(__native phoneid, __native *data) |
306 | { |
241 | { |
307 | call_t *call; |
242 | call_t *call; |
308 | phone_t *phone; |
243 | phone_t *phone; |
309 | 244 | ||
310 | phone = get_phone(phoneid); |
- | |
311 | if (!phone) |
- | |
312 | return IPC_CALLRET_FATAL; |
- | |
313 | - | ||
314 | if (check_call_limit()) |
245 | if (check_call_limit()) |
315 | return IPC_CALLRET_TEMPORARY; |
246 | return IPC_CALLRET_TEMPORARY; |
316 | 247 | ||
- | 248 | phone = get_phone_and_lock(phoneid); |
|
- | 249 | if (!phone) |
|
- | 250 | return IPC_CALLRET_FATAL; |
|
- | 251 | ||
317 | call = ipc_call_alloc(); |
252 | call = ipc_call_alloc(); |
318 | copy_from_uspace(&call->data, data, sizeof(call->data)); |
253 | copy_from_uspace(&call->data, data, sizeof(call->data)); |
319 | 254 | ||
320 | if (is_system_method(IPC_GET_METHOD(call->data))) { |
255 | if (is_system_method(IPC_GET_METHOD(call->data))) { |
321 | ipc_call_free(call); |
256 | ipc_call_free(call); |
322 | return EPERM; |
257 | return EPERM; |
323 | } |
258 | } |
324 | 259 | ||
325 | ipc_call(phone, call); |
260 | ipc_call(phone, call); |
326 | 261 | ||
327 | return (__native) call; |
262 | return (__native) call; |
328 | } |
263 | } |
329 | 264 | ||
330 | /** Forward received call to another destination |
265 | /** Forward received call to another destination |
331 | * |
266 | * |
332 | * The arg1 and arg2 are changed in the forwarded message |
267 | * The arg1 and arg2 are changed in the forwarded message |
333 | * |
268 | * |
334 | * Warning: If implementing non-fast version, make sure that |
269 | * Warning: If implementing non-fast version, make sure that |
335 | * arg3 is not rewritten for certain system IPC |
270 | * arg3 is not rewritten for certain system IPC |
336 | */ |
271 | */ |
337 | __native sys_ipc_forward_fast(__native callid, __native phoneid, |
272 | __native sys_ipc_forward_fast(__native callid, __native phoneid, |
338 | __native method, __native arg1) |
273 | __native method, __native arg1) |
339 | { |
274 | { |
340 | call_t *call; |
275 | call_t *call; |
341 | phone_t *phone; |
276 | phone_t *phone; |
342 | 277 | ||
343 | call = get_call(callid); |
278 | call = get_call(callid); |
344 | if (!call) |
279 | if (!call) |
345 | return ENOENT; |
280 | return ENOENT; |
346 | 281 | ||
347 | phone = get_phone(phoneid); |
282 | phone = get_phone_and_lock(phoneid); |
348 | if (!phone) { |
283 | if (!phone) { |
349 | IPC_SET_RETVAL(call->data, EFORWARD); |
284 | IPC_SET_RETVAL(call->data, EFORWARD); |
350 | ipc_answer(&TASK->answerbox, call); |
285 | ipc_answer(&TASK->answerbox, call); |
351 | return ENOENT; |
286 | return ENOENT; |
352 | } |
287 | } |
353 | 288 | ||
354 | if (!is_forwardable(IPC_GET_METHOD(call->data))) { |
289 | if (!is_forwardable(IPC_GET_METHOD(call->data))) { |
355 | IPC_SET_RETVAL(call->data, EFORWARD); |
290 | IPC_SET_RETVAL(call->data, EFORWARD); |
356 | ipc_answer(&TASK->answerbox, call); |
291 | ipc_answer(&TASK->answerbox, call); |
357 | return EPERM; |
292 | return EPERM; |
358 | } |
293 | } |
359 | 294 | ||
360 | /* Userspace is not allowed to change method of system methods |
295 | /* Userspace is not allowed to change method of system methods |
361 | * on forward, allow changing ARG1 and ARG2 by means of method and arg1 |
296 | * on forward, allow changing ARG1 and ARG2 by means of method and arg1 |
362 | */ |
297 | */ |
363 | if (is_system_method(IPC_GET_METHOD(call->data))) { |
298 | if (is_system_method(IPC_GET_METHOD(call->data))) { |
364 | IPC_SET_ARG1(call->data, method); |
299 | IPC_SET_ARG1(call->data, method); |
365 | IPC_SET_ARG2(call->data, arg1); |
300 | IPC_SET_ARG2(call->data, arg1); |
366 | } else { |
301 | } else { |
367 | IPC_SET_METHOD(call->data, method); |
302 | IPC_SET_METHOD(call->data, method); |
368 | IPC_SET_ARG1(call->data, arg1); |
303 | IPC_SET_ARG1(call->data, arg1); |
369 | } |
304 | } |
370 | 305 | ||
371 | ipc_forward(call, phone->callee, &TASK->answerbox); |
306 | ipc_forward(call, phone->callee, &TASK->answerbox); |
372 | 307 | ||
373 | return 0; |
308 | return 0; |
374 | } |
309 | } |
375 | 310 | ||
376 | /** Send IPC answer */ |
311 | /** Send IPC answer */ |
377 | __native sys_ipc_answer_fast(__native callid, __native retval, |
312 | __native sys_ipc_answer_fast(__native callid, __native retval, |
378 | __native arg1, __native arg2) |
313 | __native arg1, __native arg2) |
379 | { |
314 | { |
380 | call_t *call; |
315 | call_t *call; |
381 | ipc_data_t saved_data; |
316 | ipc_data_t saved_data; |
382 | int preprocess = 0; |
317 | int preprocess = 0; |
383 | 318 | ||
384 | call = get_call(callid); |
319 | call = get_call(callid); |
385 | if (!call) |
320 | if (!call) |
386 | return ENOENT; |
321 | return ENOENT; |
387 | 322 | ||
388 | if (answer_will_preprocess(call)) { |
323 | if (answer_will_preprocess(call)) { |
389 | memcpy(&saved_data, &call->data, sizeof(call->data)); |
324 | memcpy(&saved_data, &call->data, sizeof(call->data)); |
390 | preprocess = 1; |
325 | preprocess = 1; |
391 | } |
326 | } |
392 | 327 | ||
393 | IPC_SET_RETVAL(call->data, retval); |
328 | IPC_SET_RETVAL(call->data, retval); |
394 | IPC_SET_ARG1(call->data, arg1); |
329 | IPC_SET_ARG1(call->data, arg1); |
395 | IPC_SET_ARG2(call->data, arg2); |
330 | IPC_SET_ARG2(call->data, arg2); |
396 | 331 | ||
397 | if (preprocess) |
332 | if (preprocess) |
398 | answer_preprocess(call, &saved_data); |
333 | answer_preprocess(call, &saved_data); |
399 | 334 | ||
400 | ipc_answer(&TASK->answerbox, call); |
335 | ipc_answer(&TASK->answerbox, call); |
401 | return 0; |
336 | return 0; |
402 | } |
337 | } |
403 | 338 | ||
404 | /** Send IPC answer */ |
339 | /** Send IPC answer */ |
405 | inline __native sys_ipc_answer(__native callid, __native *data) |
340 | inline __native sys_ipc_answer(__native callid, __native *data) |
406 | { |
341 | { |
407 | call_t *call; |
342 | call_t *call; |
408 | ipc_data_t saved_data; |
343 | ipc_data_t saved_data; |
409 | int preprocess = 0; |
344 | int preprocess = 0; |
410 | 345 | ||
411 | call = get_call(callid); |
346 | call = get_call(callid); |
412 | if (!call) |
347 | if (!call) |
413 | return ENOENT; |
348 | return ENOENT; |
414 | 349 | ||
415 | if (answer_will_preprocess(call)) { |
350 | if (answer_will_preprocess(call)) { |
416 | memcpy(&saved_data, &call->data, sizeof(call->data)); |
351 | memcpy(&saved_data, &call->data, sizeof(call->data)); |
417 | preprocess = 1; |
352 | preprocess = 1; |
418 | } |
353 | } |
419 | copy_from_uspace(&call->data, data, sizeof(call->data)); |
354 | copy_from_uspace(&call->data, data, sizeof(call->data)); |
420 | 355 | ||
421 | if (preprocess) |
356 | if (preprocess) |
422 | answer_preprocess(call, &saved_data); |
357 | answer_preprocess(call, &saved_data); |
423 | 358 | ||
424 | ipc_answer(&TASK->answerbox, call); |
359 | ipc_answer(&TASK->answerbox, call); |
425 | 360 | ||
426 | return 0; |
361 | return 0; |
427 | } |
362 | } |
428 | 363 | ||
429 | /** Ask the other side of connection to do 'callback' connection |
364 | /** Ask the other side of connection to do 'callback' connection |
430 | * |
365 | * |
431 | * @return 0 if no error, error otherwise |
366 | * @return 0 if no error, error otherwise |
432 | */ |
367 | */ |
433 | __native sys_ipc_connect_to_me(__native phoneid, __native arg1, |
368 | __native sys_ipc_connect_to_me(__native phoneid, __native arg1, |
434 | __native arg2, task_id_t *taskid) |
369 | __native arg2, task_id_t *taskid) |
435 | { |
370 | { |
436 | call_t call; |
371 | call_t call; |
437 | phone_t *phone; |
372 | phone_t *phone; |
438 | 373 | ||
439 | phone = get_phone(phoneid); |
- | |
440 | if (!phone) |
- | |
441 | return ENOENT; |
- | |
442 | - | ||
443 | ipc_call_init(&call); |
374 | ipc_call_init(&call); |
444 | IPC_SET_METHOD(call.data, IPC_M_CONNECTTOME); |
375 | IPC_SET_METHOD(call.data, IPC_M_CONNECTTOME); |
445 | IPC_SET_ARG1(call.data, arg1); |
376 | IPC_SET_ARG1(call.data, arg1); |
446 | IPC_SET_ARG2(call.data, arg2); |
377 | IPC_SET_ARG2(call.data, arg2); |
447 | 378 | ||
- | 379 | phone = get_phone_and_lock(phoneid); |
|
- | 380 | if (!phone) |
|
- | 381 | return ENOENT; |
|
- | 382 | ||
448 | ipc_call_sync(phone, &call); |
383 | ipc_call_sync(phone, &call); |
449 | 384 | ||
450 | if (!IPC_GET_RETVAL(call.data) && taskid) |
385 | if (!IPC_GET_RETVAL(call.data) && taskid) |
451 | copy_to_uspace(taskid, |
386 | copy_to_uspace(taskid, |
452 | &phone->callee->task->taskid, |
387 | &phone->callee->task->taskid, |
453 | sizeof(TASK->taskid)); |
388 | sizeof(TASK->taskid)); |
454 | 389 | ||
455 | return IPC_GET_RETVAL(call.data); |
390 | return IPC_GET_RETVAL(call.data); |
456 | } |
391 | } |
457 | 392 | ||
458 | /** Ask target process to connect me somewhere |
393 | /** Ask target process to connect me somewhere |
459 | * |
394 | * |
460 | * @return phoneid - on success, error otherwise |
395 | * @return phoneid - on success, error otherwise |
461 | */ |
396 | */ |
462 | __native sys_ipc_connect_me_to(__native phoneid, __native arg1, |
397 | __native sys_ipc_connect_me_to(__native phoneid, __native arg1, |
463 | __native arg2) |
398 | __native arg2) |
464 | { |
399 | { |
465 | call_t call; |
400 | call_t call; |
466 | phone_t *phone; |
401 | phone_t *phone; |
467 | int newphid; |
402 | int newphid; |
468 | 403 | ||
469 | phone = get_phone(phoneid); |
404 | phone = get_phone_and_lock(phoneid); |
470 | if (!phone) |
405 | if (!phone) |
471 | return ENOENT; |
406 | return ENOENT; |
472 | 407 | ||
473 | newphid = phone_alloc(); |
408 | newphid = phone_alloc(); |
474 | if (newphid < 0) |
409 | if (newphid < 0) |
475 | return ELIMIT; |
410 | return ELIMIT; |
476 | 411 | ||
477 | ipc_call_init(&call); |
412 | ipc_call_init(&call); |
478 | IPC_SET_METHOD(call.data, IPC_M_CONNECTMETO); |
413 | IPC_SET_METHOD(call.data, IPC_M_CONNECTMETO); |
479 | IPC_SET_ARG1(call.data, arg1); |
414 | IPC_SET_ARG1(call.data, arg1); |
480 | IPC_SET_ARG2(call.data, arg2); |
415 | IPC_SET_ARG2(call.data, arg2); |
481 | IPC_SET_ARG3(call.data, (__native)&TASK->phones[newphid]); |
416 | IPC_SET_ARG3(call.data, (__native)&TASK->phones[newphid]); |
482 | 417 | ||
483 | ipc_call_sync(phone, &call); |
418 | ipc_call_sync(phone, &call); |
484 | 419 | ||
485 | if (IPC_GET_RETVAL(call.data)) { /* Connection failed */ |
420 | if (IPC_GET_RETVAL(call.data)) { /* Connection failed */ |
486 | phone_dealloc(newphid); |
421 | phone_dealloc(newphid); |
487 | return IPC_GET_RETVAL(call.data); |
422 | return IPC_GET_RETVAL(call.data); |
488 | } |
423 | } |
489 | 424 | ||
490 | return newphid; |
425 | return newphid; |
491 | } |
426 | } |
492 | 427 | ||
493 | /** Wait for incoming ipc call or answer |
428 | /** Wait for incoming ipc call or answer |
494 | * |
429 | * |
495 | * Generic function - can serve either as inkernel or userspace call |
430 | * Generic function - can serve either as inkernel or userspace call |
496 | * - inside kernel does probably unnecessary copying of data (TODO) |
431 | * - inside kernel does probably unnecessary copying of data (TODO) |
497 | * |
432 | * |
498 | * @param result |
433 | * @param result |
499 | * @param taskid |
434 | * @param taskid |
500 | * @param flags |
435 | * @param flags |
501 | * @return Callid, if callid & 1, then the call is answer |
436 | * @return Callid, if callid & 1, then the call is answer |
502 | */ |
437 | */ |
503 | inline __native sys_ipc_wait_for_call(ipc_data_t *calldata, |
438 | inline __native sys_ipc_wait_for_call(ipc_data_t *calldata, |
504 | task_id_t *taskid, |
439 | task_id_t *taskid, |
505 | __native flags) |
440 | __native flags) |
506 | 441 | ||
507 | { |
442 | { |
508 | call_t *call; |
443 | call_t *call; |
509 | 444 | ||
510 | restart: |
445 | restart: |
511 | call = ipc_wait_for_call(&TASK->answerbox, flags); |
446 | call = ipc_wait_for_call(&TASK->answerbox, flags); |
512 | 447 | ||
513 | if (call->flags & IPC_CALL_ANSWERED) { |
448 | if (call->flags & IPC_CALL_ANSWERED) { |
514 | if (process_answer(&TASK->answerbox, call)) |
449 | if (process_answer(&TASK->answerbox, call)) |
515 | goto restart; |
450 | goto restart; |
516 | 451 | ||
517 | copy_to_uspace(calldata, &call->data, sizeof(call->data)); |
452 | copy_to_uspace(calldata, &call->data, sizeof(call->data)); |
518 | atomic_dec(&TASK->active_calls); |
453 | atomic_dec(&TASK->active_calls); |
519 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC)); |
454 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC)); |
520 | ipc_call_free(call); |
455 | ipc_call_free(call); |
521 | 456 | ||
522 | return ((__native)call) | IPC_CALLID_ANSWERED; |
457 | return ((__native)call) | IPC_CALLID_ANSWERED; |
523 | } |
458 | } |
524 | if (process_request(&TASK->answerbox, call)) |
459 | if (process_request(&TASK->answerbox, call)) |
525 | goto restart; |
460 | goto restart; |
526 | copy_to_uspace(calldata, &call->data, sizeof(call->data)); |
461 | copy_to_uspace(calldata, &call->data, sizeof(call->data)); |
527 | copy_to_uspace(taskid, (void *)&TASK->taskid, sizeof(TASK->taskid)); |
462 | copy_to_uspace(taskid, (void *)&TASK->taskid, sizeof(TASK->taskid)); |
528 | return (__native)call; |
463 | return (__native)call; |
529 | } |
464 | } |
530 | 465 | ||
531 | 466 |