Rev 1090 | Rev 1248 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1090 | Rev 1141 | ||
---|---|---|---|
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 | #include <ipc/ipcrsc.h> |
39 | 39 | ||
40 | 40 | ||
41 | #include <print.h> |
41 | #include <print.h> |
42 | #include <arch.h> |
42 | #include <arch.h> |
43 | #include <proc/thread.h> |
43 | #include <proc/thread.h> |
44 | 44 | ||
45 | #define GET_CHECK_PHONE(phone,phoneid,err) { \ |
45 | #define GET_CHECK_PHONE(phone,phoneid,err) { \ |
46 | if (phoneid > IPC_MAX_PHONES) { err; } \ |
46 | if (phoneid > IPC_MAX_PHONES) { err; } \ |
47 | phone = &TASK->phones[phoneid]; \ |
47 | phone = &TASK->phones[phoneid]; \ |
48 | } |
48 | } |
49 | 49 | ||
50 | #define STRUCT_TO_USPACE(dst,src) copy_to_uspace(dst,src,sizeof(*(src))) |
50 | #define STRUCT_TO_USPACE(dst,src) copy_to_uspace(dst,src,sizeof(*(src))) |
51 | 51 | ||
52 | /** Return true if the method is a system method */ |
52 | /** Return true if the method is a system method */ |
53 | static inline int is_system_method(__native method) |
53 | static inline int is_system_method(__native method) |
54 | { |
54 | { |
55 | if (method <= IPC_M_LAST_SYSTEM) |
55 | if (method <= IPC_M_LAST_SYSTEM) |
56 | return 1; |
56 | return 1; |
57 | return 0; |
57 | return 0; |
58 | } |
58 | } |
59 | 59 | ||
60 | /** Return true if the message with this method is forwardable |
60 | /** Return true if the message with this method is forwardable |
61 | * |
61 | * |
62 | * - some system messages may be forwarded, for some of them |
62 | * - some system messages may be forwarded, for some of them |
63 | * it is useless |
63 | * it is useless |
64 | */ |
64 | */ |
65 | static inline int is_forwardable(__native method) |
65 | static inline int is_forwardable(__native method) |
66 | { |
66 | { |
67 | if (method == IPC_M_PHONE_HUNGUP) |
67 | if (method == IPC_M_PHONE_HUNGUP) |
68 | return 0; /* This message is meant only for the receiver */ |
68 | return 0; /* This message is meant only for the receiver */ |
69 | return 1; |
69 | return 1; |
70 | } |
70 | } |
71 | 71 | ||
72 | /****************************************************/ |
72 | /****************************************************/ |
73 | /* Functions that preprocess answer before sending |
73 | /* Functions that preprocess answer before sending |
74 | * it to the recepient |
74 | * it to the recepient |
75 | */ |
75 | */ |
76 | 76 | ||
77 | /** Return true if the caller (ipc_answer) should save |
77 | /** Return true if the caller (ipc_answer) should save |
78 | * the old call contents for answer_preprocess |
78 | * the old call contents for answer_preprocess |
79 | */ |
79 | */ |
80 | static inline int answer_need_old(call_t *call) |
80 | static inline int answer_need_old(call_t *call) |
81 | { |
81 | { |
82 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) |
82 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) |
83 | return 1; |
83 | return 1; |
84 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_ME_TO) |
84 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_ME_TO) |
85 | return 1; |
85 | return 1; |
86 | return 0; |
86 | return 0; |
87 | } |
87 | } |
88 | 88 | ||
89 | /** Interpret process answer as control information */ |
89 | /** Interpret process answer as control information */ |
90 | static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata) |
90 | static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata) |
91 | { |
91 | { |
92 | int phoneid; |
92 | int phoneid; |
93 | 93 | ||
94 | if (IPC_GET_RETVAL(answer->data) == EHANGUP) { |
94 | if (IPC_GET_RETVAL(answer->data) == EHANGUP) { |
- | 95 | /* In case of forward, hangup the forwared phone, |
|
95 | /* Atomic operation */ |
96 | * not the originator |
- | 97 | */ |
|
- | 98 | spinlock_lock(&answer->data.phone->lock); |
|
- | 99 | spinlock_lock(&TASK->answerbox.lock); |
|
- | 100 | if (answer->data.phone->callee) { |
|
- | 101 | list_remove(&answer->data.phone->list); |
|
96 | answer->data.phone->callee = NULL; |
102 | answer->data.phone->callee = 0; |
- | 103 | } |
|
- | 104 | spinlock_unlock(&TASK->answerbox.lock); |
|
- | 105 | spinlock_unlock(&answer->data.phone->lock); |
|
97 | } |
106 | } |
98 | 107 | ||
99 | if (!olddata) |
108 | if (!olddata) |
100 | return; |
109 | return; |
101 | 110 | ||
102 | if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) { |
111 | if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) { |
103 | phoneid = IPC_GET_ARG3(*olddata); |
112 | phoneid = IPC_GET_ARG3(*olddata); |
104 | if (IPC_GET_RETVAL(answer->data)) { |
113 | if (IPC_GET_RETVAL(answer->data)) { |
105 | /* The connection was not accepted */ |
114 | /* The connection was not accepted */ |
106 | phone_dealloc(phoneid); |
115 | phone_dealloc(phoneid); |
107 | } else { |
116 | } else { |
108 | /* The connection was accepted */ |
117 | /* The connection was accepted */ |
109 | phone_connect(phoneid,&answer->sender->answerbox); |
118 | phone_connect(phoneid,&answer->sender->answerbox); |
110 | /* Set 'phone identification' as arg3 of response */ |
119 | /* Set 'phone identification' as arg3 of response */ |
111 | IPC_SET_ARG3(answer->data, (__native)&TASK->phones[phoneid]); |
120 | IPC_SET_ARG3(answer->data, (__native)&TASK->phones[phoneid]); |
112 | } |
121 | } |
113 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) { |
122 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) { |
114 | /* If the users accepted call, connect */ |
123 | /* If the users accepted call, connect */ |
115 | if (!IPC_GET_RETVAL(answer->data)) { |
124 | if (!IPC_GET_RETVAL(answer->data)) { |
116 | ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata), |
125 | ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata), |
117 | &TASK->answerbox); |
126 | &TASK->answerbox); |
118 | } |
127 | } |
119 | } |
128 | } |
120 | } |
129 | } |
121 | 130 | ||
122 | /** Called before the request is sent |
131 | /** Called before the request is sent |
123 | * |
132 | * |
124 | * @return 0 - no error, -1 - report error to user |
133 | * @return 0 - no error, -1 - report error to user |
125 | */ |
134 | */ |
126 | static int request_preprocess(call_t *call) |
135 | static int request_preprocess(call_t *call) |
127 | { |
136 | { |
128 | int newphid; |
137 | int newphid; |
129 | 138 | ||
130 | switch (IPC_GET_METHOD(call->data)) { |
139 | switch (IPC_GET_METHOD(call->data)) { |
131 | case IPC_M_CONNECT_ME_TO: |
140 | case IPC_M_CONNECT_ME_TO: |
132 | newphid = phone_alloc(); |
141 | newphid = phone_alloc(); |
133 | if (newphid < 0) |
142 | if (newphid < 0) |
134 | return ELIMIT; |
143 | return ELIMIT; |
135 | /* Set arg3 for server */ |
144 | /* Set arg3 for server */ |
136 | IPC_SET_ARG3(call->data, (__native)&TASK->phones[newphid]); |
145 | IPC_SET_ARG3(call->data, (__native)&TASK->phones[newphid]); |
137 | call->flags |= IPC_CALL_CONN_ME_TO; |
146 | call->flags |= IPC_CALL_CONN_ME_TO; |
138 | call->private = newphid; |
147 | call->private = newphid; |
139 | break; |
148 | break; |
140 | default: |
149 | default: |
141 | break; |
150 | break; |
142 | } |
151 | } |
143 | return 0; |
152 | return 0; |
144 | } |
153 | } |
145 | 154 | ||
146 | /****************************************************/ |
155 | /****************************************************/ |
147 | /* Functions called to process received call/answer |
156 | /* Functions called to process received call/answer |
148 | * before passing to uspace |
157 | * before passing to uspace |
149 | */ |
158 | */ |
150 | 159 | ||
151 | /** Do basic kernel processing of received call answer */ |
160 | /** Do basic kernel processing of received call answer */ |
152 | static void process_answer(call_t *call) |
161 | static void process_answer(call_t *call) |
153 | { |
162 | { |
154 | if (IPC_GET_RETVAL(call->data) == EHANGUP && \ |
163 | if (IPC_GET_RETVAL(call->data) == EHANGUP && \ |
155 | call->flags & IPC_CALL_FORWARDED) |
164 | call->flags & IPC_CALL_FORWARDED) |
156 | IPC_SET_RETVAL(call->data, EFORWARD); |
165 | IPC_SET_RETVAL(call->data, EFORWARD); |
157 | 166 | ||
158 | if (call->flags & IPC_CALL_CONN_ME_TO) { |
167 | if (call->flags & IPC_CALL_CONN_ME_TO) { |
159 | if (IPC_GET_RETVAL(call->data)) |
168 | if (IPC_GET_RETVAL(call->data)) |
160 | phone_dealloc(call->private); |
169 | phone_dealloc(call->private); |
161 | else |
170 | else |
162 | IPC_SET_ARG3(call->data, call->private); |
171 | IPC_SET_ARG3(call->data, call->private); |
163 | } |
172 | } |
164 | } |
173 | } |
165 | 174 | ||
166 | /** Do basic kernel processing of received call request |
175 | /** Do basic kernel processing of received call request |
167 | * |
176 | * |
168 | * @return 0 - the call should be passed to userspace, 1 - ignore call |
177 | * @return 0 - the call should be passed to userspace, 1 - ignore call |
169 | */ |
178 | */ |
170 | static int process_request(answerbox_t *box,call_t *call) |
179 | static int process_request(answerbox_t *box,call_t *call) |
171 | { |
180 | { |
172 | int phoneid; |
181 | int phoneid; |
173 | 182 | ||
174 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) { |
183 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) { |
175 | phoneid = phone_alloc(); |
184 | phoneid = phone_alloc(); |
176 | if (phoneid < 0) { /* Failed to allocate phone */ |
185 | if (phoneid < 0) { /* Failed to allocate phone */ |
177 | IPC_SET_RETVAL(call->data, ELIMIT); |
186 | IPC_SET_RETVAL(call->data, ELIMIT); |
178 | ipc_answer(box,call); |
187 | ipc_answer(box,call); |
179 | return -1; |
188 | return -1; |
180 | } |
189 | } |
181 | IPC_SET_ARG3(call->data, phoneid); |
190 | IPC_SET_ARG3(call->data, phoneid); |
182 | } |
191 | } |
183 | return 0; |
192 | return 0; |
184 | } |
193 | } |
185 | 194 | ||
186 | /** Send a call over IPC, wait for reply, return to user |
195 | /** Send a call over IPC, wait for reply, return to user |
187 | * |
196 | * |
188 | * @return Call identification, returns -1 on fatal error, |
197 | * @return Call identification, returns -1 on fatal error, |
189 | -2 on 'Too many async request, handle answers first |
198 | -2 on 'Too many async request, handle answers first |
190 | */ |
199 | */ |
191 | __native sys_ipc_call_sync_fast(__native phoneid, __native method, |
200 | __native sys_ipc_call_sync_fast(__native phoneid, __native method, |
192 | __native arg1, ipc_data_t *data) |
201 | __native arg1, ipc_data_t *data) |
193 | { |
202 | { |
194 | call_t call; |
203 | call_t call; |
195 | phone_t *phone; |
204 | phone_t *phone; |
196 | int res; |
205 | int res; |
197 | 206 | ||
198 | GET_CHECK_PHONE(phone, phoneid, return ENOENT); |
207 | GET_CHECK_PHONE(phone, phoneid, return ENOENT); |
199 | 208 | ||
200 | ipc_call_static_init(&call); |
209 | ipc_call_static_init(&call); |
201 | IPC_SET_METHOD(call.data, method); |
210 | IPC_SET_METHOD(call.data, method); |
202 | IPC_SET_ARG1(call.data, arg1); |
211 | IPC_SET_ARG1(call.data, arg1); |
203 | 212 | ||
204 | if (!(res=request_preprocess(&call))) { |
213 | if (!(res=request_preprocess(&call))) { |
205 | ipc_call_sync(phone, &call); |
214 | ipc_call_sync(phone, &call); |
206 | process_answer(&call); |
215 | process_answer(&call); |
207 | } else |
216 | } else |
208 | IPC_SET_RETVAL(call.data, res); |
217 | IPC_SET_RETVAL(call.data, res); |
209 | STRUCT_TO_USPACE(&data->args, &call.data.args); |
218 | STRUCT_TO_USPACE(&data->args, &call.data.args); |
210 | 219 | ||
211 | return 0; |
220 | return 0; |
212 | } |
221 | } |
213 | 222 | ||
214 | /** Synchronous IPC call allowing to send whole message */ |
223 | /** Synchronous IPC call allowing to send whole message */ |
215 | __native sys_ipc_call_sync(__native phoneid, ipc_data_t *question, |
224 | __native sys_ipc_call_sync(__native phoneid, ipc_data_t *question, |
216 | ipc_data_t *reply) |
225 | ipc_data_t *reply) |
217 | { |
226 | { |
218 | call_t call; |
227 | call_t call; |
219 | phone_t *phone; |
228 | phone_t *phone; |
220 | int res; |
229 | int res; |
221 | 230 | ||
222 | ipc_call_static_init(&call); |
231 | ipc_call_static_init(&call); |
223 | copy_from_uspace(&call.data.args, &question->args, sizeof(call.data.args)); |
232 | copy_from_uspace(&call.data.args, &question->args, sizeof(call.data.args)); |
224 | 233 | ||
225 | GET_CHECK_PHONE(phone, phoneid, return ENOENT); |
234 | GET_CHECK_PHONE(phone, phoneid, return ENOENT); |
226 | 235 | ||
227 | if (!(res=request_preprocess(&call))) { |
236 | if (!(res=request_preprocess(&call))) { |
228 | ipc_call_sync(phone, &call); |
237 | ipc_call_sync(phone, &call); |
229 | process_answer(&call); |
238 | process_answer(&call); |
230 | } else |
239 | } else |
231 | IPC_SET_RETVAL(call.data, res); |
240 | IPC_SET_RETVAL(call.data, res); |
232 | 241 | ||
233 | STRUCT_TO_USPACE(&reply->args, &call.data.args); |
242 | STRUCT_TO_USPACE(&reply->args, &call.data.args); |
234 | 243 | ||
235 | return 0; |
244 | return 0; |
236 | } |
245 | } |
237 | 246 | ||
238 | /** Check that the task did not exceed allowed limit |
247 | /** Check that the task did not exceed allowed limit |
239 | * |
248 | * |
240 | * @return 0 - Limit OK, -1 - limit exceeded |
249 | * @return 0 - Limit OK, -1 - limit exceeded |
241 | */ |
250 | */ |
242 | static int check_call_limit(void) |
251 | static int check_call_limit(void) |
243 | { |
252 | { |
244 | if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) { |
253 | if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) { |
245 | atomic_dec(&TASK->active_calls); |
254 | atomic_dec(&TASK->active_calls); |
246 | return -1; |
255 | return -1; |
247 | } |
256 | } |
248 | return 0; |
257 | return 0; |
249 | } |
258 | } |
250 | 259 | ||
251 | /** Send an asynchronous call over ipc |
260 | /** Send an asynchronous call over ipc |
252 | * |
261 | * |
253 | * @return Call identification, returns -1 on fatal error, |
262 | * @return Call identification, returns -1 on fatal error, |
254 | -2 on 'Too many async request, handle answers first |
263 | -2 on 'Too many async request, handle answers first |
255 | */ |
264 | */ |
256 | __native sys_ipc_call_async_fast(__native phoneid, __native method, |
265 | __native sys_ipc_call_async_fast(__native phoneid, __native method, |
257 | __native arg1, __native arg2) |
266 | __native arg1, __native arg2) |
258 | { |
267 | { |
259 | call_t *call; |
268 | call_t *call; |
260 | phone_t *phone; |
269 | phone_t *phone; |
261 | int res; |
270 | int res; |
262 | 271 | ||
263 | if (check_call_limit()) |
272 | if (check_call_limit()) |
264 | return IPC_CALLRET_TEMPORARY; |
273 | return IPC_CALLRET_TEMPORARY; |
265 | 274 | ||
266 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL); |
275 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL); |
267 | 276 | ||
268 | call = ipc_call_alloc(); |
277 | call = ipc_call_alloc(); |
269 | IPC_SET_METHOD(call->data, method); |
278 | IPC_SET_METHOD(call->data, method); |
270 | IPC_SET_ARG1(call->data, arg1); |
279 | IPC_SET_ARG1(call->data, arg1); |
271 | IPC_SET_ARG2(call->data, arg2); |
280 | IPC_SET_ARG2(call->data, arg2); |
272 | 281 | ||
273 | if (!(res=request_preprocess(call))) |
282 | if (!(res=request_preprocess(call))) |
274 | ipc_call(phone, call); |
283 | ipc_call(phone, call); |
275 | else |
284 | else |
276 | ipc_backsend_err(phone, call, res); |
285 | ipc_backsend_err(phone, call, res); |
277 | 286 | ||
278 | return (__native) call; |
287 | return (__native) call; |
279 | } |
288 | } |
280 | 289 | ||
281 | /** Synchronous IPC call allowing to send whole message |
290 | /** Synchronous IPC call allowing to send whole message |
282 | * |
291 | * |
283 | * @return The same as sys_ipc_call_async |
292 | * @return The same as sys_ipc_call_async |
284 | */ |
293 | */ |
285 | __native sys_ipc_call_async(__native phoneid, ipc_data_t *data) |
294 | __native sys_ipc_call_async(__native phoneid, ipc_data_t *data) |
286 | { |
295 | { |
287 | call_t *call; |
296 | call_t *call; |
288 | phone_t *phone; |
297 | phone_t *phone; |
289 | int res; |
298 | int res; |
290 | 299 | ||
291 | if (check_call_limit()) |
300 | if (check_call_limit()) |
292 | return IPC_CALLRET_TEMPORARY; |
301 | return IPC_CALLRET_TEMPORARY; |
293 | 302 | ||
294 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL); |
303 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL); |
295 | 304 | ||
296 | call = ipc_call_alloc(); |
305 | call = ipc_call_alloc(); |
297 | copy_from_uspace(&call->data.args, &data->args, sizeof(call->data.args)); |
306 | copy_from_uspace(&call->data.args, &data->args, sizeof(call->data.args)); |
298 | if (!(res=request_preprocess(call))) |
307 | if (!(res=request_preprocess(call))) |
299 | ipc_call(phone, call); |
308 | ipc_call(phone, call); |
300 | else |
309 | else |
301 | ipc_backsend_err(phone, call, res); |
310 | ipc_backsend_err(phone, call, res); |
302 | 311 | ||
303 | return (__native) call; |
312 | return (__native) call; |
304 | } |
313 | } |
305 | 314 | ||
306 | /** Forward received call to another destination |
315 | /** Forward received call to another destination |
307 | * |
316 | * |
308 | * The arg1 and arg2 are changed in the forwarded message |
317 | * The arg1 and arg2 are changed in the forwarded message |
309 | * |
318 | * |
310 | * Warning: If implementing non-fast version, make sure that |
319 | * Warning: If implementing non-fast version, make sure that |
311 | * arg3 is not rewritten for certain system IPC |
320 | * arg3 is not rewritten for certain system IPC |
312 | */ |
321 | */ |
313 | __native sys_ipc_forward_fast(__native callid, __native phoneid, |
322 | __native sys_ipc_forward_fast(__native callid, __native phoneid, |
314 | __native method, __native arg1) |
323 | __native method, __native arg1) |
315 | { |
324 | { |
316 | call_t *call; |
325 | call_t *call; |
317 | phone_t *phone; |
326 | phone_t *phone; |
318 | 327 | ||
319 | call = get_call(callid); |
328 | call = get_call(callid); |
320 | if (!call) |
329 | if (!call) |
321 | return ENOENT; |
330 | return ENOENT; |
322 | 331 | ||
323 | call->flags |= IPC_CALL_FORWARDED; |
332 | call->flags |= IPC_CALL_FORWARDED; |
324 | 333 | ||
325 | GET_CHECK_PHONE(phone, phoneid, { |
334 | GET_CHECK_PHONE(phone, phoneid, { |
326 | IPC_SET_RETVAL(call->data, EFORWARD); |
335 | IPC_SET_RETVAL(call->data, EFORWARD); |
327 | ipc_answer(&TASK->answerbox, call); |
336 | ipc_answer(&TASK->answerbox, call); |
328 | return ENOENT; |
337 | return ENOENT; |
329 | }); |
338 | }); |
330 | 339 | ||
331 | if (!is_forwardable(IPC_GET_METHOD(call->data))) { |
340 | if (!is_forwardable(IPC_GET_METHOD(call->data))) { |
332 | IPC_SET_RETVAL(call->data, EFORWARD); |
341 | IPC_SET_RETVAL(call->data, EFORWARD); |
333 | ipc_answer(&TASK->answerbox, call); |
342 | ipc_answer(&TASK->answerbox, call); |
334 | return EPERM; |
343 | return EPERM; |
335 | } |
344 | } |
336 | 345 | ||
337 | /* Userspace is not allowed to change method of system methods |
346 | /* Userspace is not allowed to change method of system methods |
338 | * on forward, allow changing ARG1 and ARG2 by means of method and arg1 |
347 | * on forward, allow changing ARG1 and ARG2 by means of method and arg1 |
339 | */ |
348 | */ |
340 | if (is_system_method(IPC_GET_METHOD(call->data))) { |
349 | if (is_system_method(IPC_GET_METHOD(call->data))) { |
341 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) |
350 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) |
342 | phone_dealloc(IPC_GET_ARG3(call->data)); |
351 | phone_dealloc(IPC_GET_ARG3(call->data)); |
343 | 352 | ||
344 | IPC_SET_ARG1(call->data, method); |
353 | IPC_SET_ARG1(call->data, method); |
345 | IPC_SET_ARG2(call->data, arg1); |
354 | IPC_SET_ARG2(call->data, arg1); |
346 | } else { |
355 | } else { |
347 | IPC_SET_METHOD(call->data, method); |
356 | IPC_SET_METHOD(call->data, method); |
348 | IPC_SET_ARG1(call->data, arg1); |
357 | IPC_SET_ARG1(call->data, arg1); |
349 | } |
358 | } |
350 | 359 | ||
351 | return ipc_forward(call, phone, &TASK->answerbox); |
360 | return ipc_forward(call, phone, &TASK->answerbox); |
352 | } |
361 | } |
353 | 362 | ||
354 | /** Send IPC answer */ |
363 | /** Send IPC answer */ |
355 | __native sys_ipc_answer_fast(__native callid, __native retval, |
364 | __native sys_ipc_answer_fast(__native callid, __native retval, |
356 | __native arg1, __native arg2) |
365 | __native arg1, __native arg2) |
357 | { |
366 | { |
358 | call_t *call; |
367 | call_t *call; |
359 | ipc_data_t saved_data; |
368 | ipc_data_t saved_data; |
360 | int saveddata = 0; |
369 | int saveddata = 0; |
361 | 370 | ||
362 | call = get_call(callid); |
371 | call = get_call(callid); |
363 | if (!call) |
372 | if (!call) |
364 | return ENOENT; |
373 | return ENOENT; |
365 | 374 | ||
366 | if (answer_need_old(call)) { |
375 | if (answer_need_old(call)) { |
367 | memcpy(&saved_data, &call->data, sizeof(call->data)); |
376 | memcpy(&saved_data, &call->data, sizeof(call->data)); |
368 | saveddata = 1; |
377 | saveddata = 1; |
369 | } |
378 | } |
370 | 379 | ||
371 | IPC_SET_RETVAL(call->data, retval); |
380 | IPC_SET_RETVAL(call->data, retval); |
372 | IPC_SET_ARG1(call->data, arg1); |
381 | IPC_SET_ARG1(call->data, arg1); |
373 | IPC_SET_ARG2(call->data, arg2); |
382 | IPC_SET_ARG2(call->data, arg2); |
374 | answer_preprocess(call, saveddata ? &saved_data : NULL); |
383 | answer_preprocess(call, saveddata ? &saved_data : NULL); |
375 | 384 | ||
376 | ipc_answer(&TASK->answerbox, call); |
385 | ipc_answer(&TASK->answerbox, call); |
377 | return 0; |
386 | return 0; |
378 | } |
387 | } |
379 | 388 | ||
380 | /** Send IPC answer */ |
389 | /** Send IPC answer */ |
381 | __native sys_ipc_answer(__native callid, ipc_data_t *data) |
390 | __native sys_ipc_answer(__native callid, ipc_data_t *data) |
382 | { |
391 | { |
383 | call_t *call; |
392 | call_t *call; |
384 | ipc_data_t saved_data; |
393 | ipc_data_t saved_data; |
385 | int saveddata = 0; |
394 | int saveddata = 0; |
386 | 395 | ||
387 | call = get_call(callid); |
396 | call = get_call(callid); |
388 | if (!call) |
397 | if (!call) |
389 | return ENOENT; |
398 | return ENOENT; |
390 | 399 | ||
391 | if (answer_need_old(call)) { |
400 | if (answer_need_old(call)) { |
392 | memcpy(&saved_data, &call->data, sizeof(call->data)); |
401 | memcpy(&saved_data, &call->data, sizeof(call->data)); |
393 | saveddata = 1; |
402 | saveddata = 1; |
394 | } |
403 | } |
395 | copy_from_uspace(&call->data.args, &data->args, |
404 | copy_from_uspace(&call->data.args, &data->args, |
396 | sizeof(call->data.args)); |
405 | sizeof(call->data.args)); |
397 | 406 | ||
398 | answer_preprocess(call, saveddata ? &saved_data : NULL); |
407 | answer_preprocess(call, saveddata ? &saved_data : NULL); |
399 | 408 | ||
400 | ipc_answer(&TASK->answerbox, call); |
409 | ipc_answer(&TASK->answerbox, call); |
401 | 410 | ||
402 | return 0; |
411 | return 0; |
403 | } |
412 | } |
404 | 413 | ||
405 | /** Hang up the phone |
414 | /** Hang up the phone |
406 | * |
415 | * |
407 | */ |
416 | */ |
408 | __native sys_ipc_hangup(int phoneid) |
417 | __native sys_ipc_hangup(int phoneid) |
409 | { |
418 | { |
410 | phone_t *phone; |
419 | phone_t *phone; |
411 | 420 | ||
412 | GET_CHECK_PHONE(phone, phoneid, return ENOENT); |
421 | GET_CHECK_PHONE(phone, phoneid, return ENOENT); |
413 | 422 | ||
414 | if (ipc_phone_hangup(phone)) |
423 | if (ipc_phone_hangup(phone)) |
415 | return -1; |
424 | return -1; |
416 | 425 | ||
417 | return 0; |
426 | return 0; |
418 | } |
427 | } |
419 | 428 | ||
420 | /** Wait for incoming ipc call or answer |
429 | /** Wait for incoming ipc call or answer |
421 | * |
430 | * |
422 | * @param calldata Pointer to buffer where the call/answer data is stored |
431 | * @param calldata Pointer to buffer where the call/answer data is stored |
423 | * @param taskid On 'CONNECT_ME_TO' call it is filled with 'taskid' of |
432 | * @param taskid On 'CONNECT_ME_TO' call it is filled with 'taskid' of |
424 | * the caller. |
433 | * the caller. |
425 | * @param flags |
434 | * @param flags |
426 | * @return Callid, if callid & 1, then the call is answer |
435 | * @return Callid, if callid & 1, then the call is answer |
427 | */ |
436 | */ |
428 | __native sys_ipc_wait_for_call(ipc_data_t *calldata, __native flags) |
437 | __native sys_ipc_wait_for_call(ipc_data_t *calldata, __native flags) |
429 | { |
438 | { |
430 | call_t *call; |
439 | call_t *call; |
431 | 440 | ||
432 | restart: |
441 | restart: |
433 | call = ipc_wait_for_call(&TASK->answerbox, flags); |
442 | call = ipc_wait_for_call(&TASK->answerbox, flags); |
434 | if (!call) |
443 | if (!call) |
435 | return 0; |
444 | return 0; |
436 | 445 | ||
437 | if (call->flags & IPC_CALL_ANSWERED) { |
446 | if (call->flags & IPC_CALL_ANSWERED) { |
438 | process_answer(call); |
447 | process_answer(call); |
439 | 448 | ||
440 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC)); |
449 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC)); |
441 | 450 | ||
442 | atomic_dec(&TASK->active_calls); |
451 | atomic_dec(&TASK->active_calls); |
443 | 452 | ||
444 | if (call->flags & IPC_CALL_DISCARD_ANSWER) { |
453 | if (call->flags & IPC_CALL_DISCARD_ANSWER) { |
445 | ipc_call_free(call); |
454 | ipc_call_free(call); |
446 | goto restart; |
455 | goto restart; |
447 | } |
456 | } |
448 | 457 | ||
449 | STRUCT_TO_USPACE(&calldata->args, &call->data.args); |
458 | STRUCT_TO_USPACE(&calldata->args, &call->data.args); |
450 | ipc_call_free(call); |
459 | ipc_call_free(call); |
451 | 460 | ||
452 | return ((__native)call) | IPC_CALLID_ANSWERED; |
461 | return ((__native)call) | IPC_CALLID_ANSWERED; |
453 | } |
462 | } |
454 | 463 | ||
455 | if (process_request(&TASK->answerbox, call)) |
464 | if (process_request(&TASK->answerbox, call)) |
456 | goto restart; |
465 | goto restart; |
457 | 466 | ||
458 | /* Include phone address('id') of the caller in the request, |
467 | /* Include phone address('id') of the caller in the request, |
459 | * copy whole call->data, not only call->data.args */ |
468 | * copy whole call->data, not only call->data.args */ |
460 | STRUCT_TO_USPACE(calldata, &call->data); |
469 | STRUCT_TO_USPACE(calldata, &call->data); |
461 | return (__native)call; |
470 | return (__native)call; |
462 | } |
471 | } |
463 | 472 |