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