1 | /*
|
---|
2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup genericipc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <arch.h>
|
---|
36 | #include <proc/task.h>
|
---|
37 | #include <proc/thread.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <memstr.h>
|
---|
40 | #include <debug.h>
|
---|
41 | #include <ipc/ipc.h>
|
---|
42 | #include <ipc/sysipc.h>
|
---|
43 | #include <ipc/irq.h>
|
---|
44 | #include <ipc/ipcrsc.h>
|
---|
45 | #include <ipc/kbox.h>
|
---|
46 | #include <synch/waitq.h>
|
---|
47 | #include <udebug/udebug_ipc.h>
|
---|
48 | #include <arch/interrupt.h>
|
---|
49 | #include <syscall/copy.h>
|
---|
50 | #include <security/cap.h>
|
---|
51 | #include <mm/as.h>
|
---|
52 | #include <print.h>
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Maximum buffer size allowed for IPC_M_DATA_WRITE and IPC_M_DATA_READ
|
---|
56 | * requests.
|
---|
57 | */
|
---|
58 | #define DATA_XFER_LIMIT (64 * 1024)
|
---|
59 |
|
---|
60 | #define GET_CHECK_PHONE(phone, phoneid, err) \
|
---|
61 | { \
|
---|
62 | if (phoneid > IPC_MAX_PHONES) { \
|
---|
63 | err; \
|
---|
64 | } \
|
---|
65 | phone = &TASK->phones[phoneid]; \
|
---|
66 | }
|
---|
67 |
|
---|
68 | #define STRUCT_TO_USPACE(dst, src) copy_to_uspace(dst, src, sizeof(*(src)))
|
---|
69 |
|
---|
70 | /** Decide if the method is a system method.
|
---|
71 | *
|
---|
72 | * @param method Method to be decided.
|
---|
73 | *
|
---|
74 | * @return Return 1 if the method is a system method.
|
---|
75 | * Otherwise return 0.
|
---|
76 | */
|
---|
77 | static inline int method_is_system(unative_t method)
|
---|
78 | {
|
---|
79 | if (method <= IPC_M_LAST_SYSTEM)
|
---|
80 | return 1;
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Decide if the message with this method is forwardable.
|
---|
85 | *
|
---|
86 | * - some system messages may be forwarded, for some of them
|
---|
87 | * it is useless
|
---|
88 | *
|
---|
89 | * @param method Method to be decided.
|
---|
90 | *
|
---|
91 | * @return Return 1 if the method is forwardable.
|
---|
92 | * Otherwise return 0.
|
---|
93 | */
|
---|
94 | static inline int method_is_forwardable(unative_t method)
|
---|
95 | {
|
---|
96 | switch (method) {
|
---|
97 | case IPC_M_CONNECTION_CLONE:
|
---|
98 | case IPC_M_CONNECT_ME:
|
---|
99 | case IPC_M_PHONE_HUNGUP:
|
---|
100 | /* This message is meant only for the original recipient. */
|
---|
101 | return 0;
|
---|
102 | default:
|
---|
103 | return 1;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Decide if the message with this method is immutable on forward.
|
---|
108 | *
|
---|
109 | * - some system messages may be forwarded but their content cannot be altered
|
---|
110 | *
|
---|
111 | * @param method Method to be decided.
|
---|
112 | *
|
---|
113 | * @return Return 1 if the method is immutable on forward.
|
---|
114 | * Otherwise return 0.
|
---|
115 | */
|
---|
116 | static inline int method_is_immutable(unative_t method)
|
---|
117 | {
|
---|
118 | switch (method) {
|
---|
119 | case IPC_M_SHARE_OUT:
|
---|
120 | case IPC_M_SHARE_IN:
|
---|
121 | case IPC_M_DATA_WRITE:
|
---|
122 | case IPC_M_DATA_READ:
|
---|
123 | return 1;
|
---|
124 | break;
|
---|
125 | default:
|
---|
126 | return 0;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /***********************************************************************
|
---|
132 | * Functions that preprocess answer before sending it to the recepient.
|
---|
133 | ***********************************************************************/
|
---|
134 |
|
---|
135 | /** Decide if the caller (e.g. ipc_answer()) should save the old call contents
|
---|
136 | * for answer_preprocess().
|
---|
137 | *
|
---|
138 | * @param call Call structure to be decided.
|
---|
139 | *
|
---|
140 | * @return Return 1 if the old call contents should be saved.
|
---|
141 | * Return 0 otherwise.
|
---|
142 | */
|
---|
143 | static inline int answer_need_old(call_t *call)
|
---|
144 | {
|
---|
145 | switch (IPC_GET_METHOD(call->data)) {
|
---|
146 | case IPC_M_CONNECTION_CLONE:
|
---|
147 | case IPC_M_CONNECT_ME:
|
---|
148 | case IPC_M_CONNECT_TO_ME:
|
---|
149 | case IPC_M_CONNECT_ME_TO:
|
---|
150 | case IPC_M_SHARE_OUT:
|
---|
151 | case IPC_M_SHARE_IN:
|
---|
152 | case IPC_M_DATA_WRITE:
|
---|
153 | case IPC_M_DATA_READ:
|
---|
154 | return 1;
|
---|
155 | default:
|
---|
156 | return 0;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** Interpret process answer as control information.
|
---|
161 | *
|
---|
162 | * This function is called directly after sys_ipc_answer().
|
---|
163 | *
|
---|
164 | * @param answer Call structure with the answer.
|
---|
165 | * @param olddata Saved data of the request.
|
---|
166 | *
|
---|
167 | * @return Return 0 on success or an error code.
|
---|
168 | */
|
---|
169 | static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
|
---|
170 | {
|
---|
171 | int phoneid;
|
---|
172 |
|
---|
173 | if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
|
---|
174 | /* In case of forward, hangup the forwared phone,
|
---|
175 | * not the originator
|
---|
176 | */
|
---|
177 | mutex_lock(&answer->data.phone->lock);
|
---|
178 | spinlock_lock(&TASK->answerbox.lock);
|
---|
179 | if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
|
---|
180 | list_remove(&answer->data.phone->link);
|
---|
181 | answer->data.phone->state = IPC_PHONE_SLAMMED;
|
---|
182 | }
|
---|
183 | spinlock_unlock(&TASK->answerbox.lock);
|
---|
184 | mutex_unlock(&answer->data.phone->lock);
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (!olddata)
|
---|
188 | return 0;
|
---|
189 |
|
---|
190 | if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTION_CLONE) {
|
---|
191 | phoneid = IPC_GET_ARG1(*olddata);
|
---|
192 | phone_t *phone = &TASK->phones[phoneid];
|
---|
193 | if (IPC_GET_RETVAL(answer->data) != EOK) {
|
---|
194 | /*
|
---|
195 | * The recipient of the cloned phone rejected the offer.
|
---|
196 | * In this case, the connection was established at the
|
---|
197 | * request time and therefore we need to slam the phone.
|
---|
198 | * We don't merely hangup as that would result in
|
---|
199 | * sending IPC_M_HUNGUP to the third party on the
|
---|
200 | * other side of the cloned phone.
|
---|
201 | */
|
---|
202 | mutex_lock(&phone->lock);
|
---|
203 | if (phone->state == IPC_PHONE_CONNECTED) {
|
---|
204 | spinlock_lock(&phone->callee->lock);
|
---|
205 | list_remove(&phone->link);
|
---|
206 | phone->state = IPC_PHONE_SLAMMED;
|
---|
207 | spinlock_unlock(&phone->callee->lock);
|
---|
208 | }
|
---|
209 | mutex_unlock(&phone->lock);
|
---|
210 | }
|
---|
211 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME) {
|
---|
212 | phone_t *phone = (phone_t *)IPC_GET_ARG5(*olddata);
|
---|
213 | if (IPC_GET_RETVAL(answer->data) != EOK) {
|
---|
214 | /*
|
---|
215 | * The other party on the cloned phoned rejected our
|
---|
216 | * request for connection on the protocol level.
|
---|
217 | * We need to break the connection without sending
|
---|
218 | * IPC_M_HUNGUP back.
|
---|
219 | */
|
---|
220 | mutex_lock(&phone->lock);
|
---|
221 | if (phone->state == IPC_PHONE_CONNECTED) {
|
---|
222 | spinlock_lock(&phone->callee->lock);
|
---|
223 | list_remove(&phone->link);
|
---|
224 | phone->state = IPC_PHONE_SLAMMED;
|
---|
225 | spinlock_unlock(&phone->callee->lock);
|
---|
226 | }
|
---|
227 | mutex_unlock(&phone->lock);
|
---|
228 | }
|
---|
229 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
|
---|
230 | phoneid = IPC_GET_ARG5(*olddata);
|
---|
231 | if (IPC_GET_RETVAL(answer->data) != EOK) {
|
---|
232 | /* The connection was not accepted */
|
---|
233 | phone_dealloc(phoneid);
|
---|
234 | } else {
|
---|
235 | /* The connection was accepted */
|
---|
236 | phone_connect(phoneid, &answer->sender->answerbox);
|
---|
237 | /* Set 'phone hash' as arg5 of response */
|
---|
238 | IPC_SET_ARG5(answer->data,
|
---|
239 | (unative_t) &TASK->phones[phoneid]);
|
---|
240 | }
|
---|
241 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
|
---|
242 | /* If the users accepted call, connect */
|
---|
243 | if (IPC_GET_RETVAL(answer->data) == EOK) {
|
---|
244 | ipc_phone_connect((phone_t *) IPC_GET_ARG5(*olddata),
|
---|
245 | &TASK->answerbox);
|
---|
246 | }
|
---|
247 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_SHARE_OUT) {
|
---|
248 | if (!IPC_GET_RETVAL(answer->data)) {
|
---|
249 | /* Accepted, handle as_area receipt */
|
---|
250 | ipl_t ipl;
|
---|
251 | int rc;
|
---|
252 | as_t *as;
|
---|
253 |
|
---|
254 | ipl = interrupts_disable();
|
---|
255 | spinlock_lock(&answer->sender->lock);
|
---|
256 | as = answer->sender->as;
|
---|
257 | spinlock_unlock(&answer->sender->lock);
|
---|
258 | interrupts_restore(ipl);
|
---|
259 |
|
---|
260 | rc = as_area_share(as, IPC_GET_ARG1(*olddata),
|
---|
261 | IPC_GET_ARG2(*olddata), AS,
|
---|
262 | IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
|
---|
263 | IPC_SET_RETVAL(answer->data, rc);
|
---|
264 | return rc;
|
---|
265 | }
|
---|
266 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_SHARE_IN) {
|
---|
267 | if (!IPC_GET_RETVAL(answer->data)) {
|
---|
268 | ipl_t ipl;
|
---|
269 | as_t *as;
|
---|
270 | int rc;
|
---|
271 |
|
---|
272 | ipl = interrupts_disable();
|
---|
273 | spinlock_lock(&answer->sender->lock);
|
---|
274 | as = answer->sender->as;
|
---|
275 | spinlock_unlock(&answer->sender->lock);
|
---|
276 | interrupts_restore(ipl);
|
---|
277 |
|
---|
278 | rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
|
---|
279 | IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
|
---|
280 | IPC_GET_ARG2(answer->data));
|
---|
281 | IPC_SET_RETVAL(answer->data, rc);
|
---|
282 | }
|
---|
283 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_READ) {
|
---|
284 | ASSERT(!answer->buffer);
|
---|
285 | if (!IPC_GET_RETVAL(answer->data)) {
|
---|
286 | /* The recipient agreed to send data. */
|
---|
287 | uintptr_t src = IPC_GET_ARG1(answer->data);
|
---|
288 | uintptr_t dst = IPC_GET_ARG1(*olddata);
|
---|
289 | size_t max_size = IPC_GET_ARG2(*olddata);
|
---|
290 | size_t size = IPC_GET_ARG2(answer->data);
|
---|
291 | if (size && size <= max_size) {
|
---|
292 | /*
|
---|
293 | * Copy the destination VA so that this piece of
|
---|
294 | * information is not lost.
|
---|
295 | */
|
---|
296 | IPC_SET_ARG1(answer->data, dst);
|
---|
297 |
|
---|
298 | answer->buffer = malloc(size, 0);
|
---|
299 | int rc = copy_from_uspace(answer->buffer,
|
---|
300 | (void *) src, size);
|
---|
301 | if (rc) {
|
---|
302 | IPC_SET_RETVAL(answer->data, rc);
|
---|
303 | free(answer->buffer);
|
---|
304 | answer->buffer = NULL;
|
---|
305 | }
|
---|
306 | } else if (!size) {
|
---|
307 | IPC_SET_RETVAL(answer->data, EOK);
|
---|
308 | } else {
|
---|
309 | IPC_SET_RETVAL(answer->data, ELIMIT);
|
---|
310 | }
|
---|
311 | }
|
---|
312 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_WRITE) {
|
---|
313 | ASSERT(answer->buffer);
|
---|
314 | if (!IPC_GET_RETVAL(answer->data)) {
|
---|
315 | /* The recipient agreed to receive data. */
|
---|
316 | int rc;
|
---|
317 | uintptr_t dst;
|
---|
318 | size_t size;
|
---|
319 | size_t max_size;
|
---|
320 |
|
---|
321 | dst = (uintptr_t)IPC_GET_ARG1(answer->data);
|
---|
322 | size = (size_t)IPC_GET_ARG2(answer->data);
|
---|
323 | max_size = (size_t)IPC_GET_ARG2(*olddata);
|
---|
324 |
|
---|
325 | if (size <= max_size) {
|
---|
326 | rc = copy_to_uspace((void *) dst,
|
---|
327 | answer->buffer, size);
|
---|
328 | if (rc)
|
---|
329 | IPC_SET_RETVAL(answer->data, rc);
|
---|
330 | } else {
|
---|
331 | IPC_SET_RETVAL(answer->data, ELIMIT);
|
---|
332 | }
|
---|
333 | }
|
---|
334 | free(answer->buffer);
|
---|
335 | answer->buffer = NULL;
|
---|
336 | }
|
---|
337 | return 0;
|
---|
338 | }
|
---|
339 |
|
---|
340 | static void phones_lock(phone_t *p1, phone_t *p2)
|
---|
341 | {
|
---|
342 | if (p1 < p2) {
|
---|
343 | mutex_lock(&p1->lock);
|
---|
344 | mutex_lock(&p2->lock);
|
---|
345 | } else if (p1 > p2) {
|
---|
346 | mutex_lock(&p2->lock);
|
---|
347 | mutex_lock(&p1->lock);
|
---|
348 | } else {
|
---|
349 | mutex_lock(&p1->lock);
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | static void phones_unlock(phone_t *p1, phone_t *p2)
|
---|
354 | {
|
---|
355 | mutex_unlock(&p1->lock);
|
---|
356 | if (p1 != p2)
|
---|
357 | mutex_unlock(&p2->lock);
|
---|
358 | }
|
---|
359 |
|
---|
360 | /** Called before the request is sent.
|
---|
361 | *
|
---|
362 | * @param call Call structure with the request.
|
---|
363 | * @param phone Phone that the call will be sent through.
|
---|
364 | *
|
---|
365 | * @return Return 0 on success, ELIMIT or EPERM on error.
|
---|
366 | */
|
---|
367 | static int request_preprocess(call_t *call, phone_t *phone)
|
---|
368 | {
|
---|
369 | int newphid;
|
---|
370 | size_t size;
|
---|
371 | uintptr_t src;
|
---|
372 | int rc;
|
---|
373 |
|
---|
374 | switch (IPC_GET_METHOD(call->data)) {
|
---|
375 | case IPC_M_CONNECTION_CLONE: {
|
---|
376 | phone_t *cloned_phone;
|
---|
377 | GET_CHECK_PHONE(cloned_phone, IPC_GET_ARG1(call->data),
|
---|
378 | return ENOENT);
|
---|
379 | phones_lock(cloned_phone, phone);
|
---|
380 | if ((cloned_phone->state != IPC_PHONE_CONNECTED) ||
|
---|
381 | phone->state != IPC_PHONE_CONNECTED) {
|
---|
382 | phones_unlock(cloned_phone, phone);
|
---|
383 | return EINVAL;
|
---|
384 | }
|
---|
385 | /*
|
---|
386 | * We can be pretty sure now that both tasks exist and we are
|
---|
387 | * connected to them. As we continue to hold the phone locks,
|
---|
388 | * we are effectively preventing them from finishing their
|
---|
389 | * potential cleanup.
|
---|
390 | */
|
---|
391 | newphid = phone_alloc(phone->callee->task);
|
---|
392 | if (newphid < 0) {
|
---|
393 | phones_unlock(cloned_phone, phone);
|
---|
394 | return ELIMIT;
|
---|
395 | }
|
---|
396 | ipc_phone_connect(&phone->callee->task->phones[newphid],
|
---|
397 | cloned_phone->callee);
|
---|
398 | phones_unlock(cloned_phone, phone);
|
---|
399 | /* Set the new phone for the callee. */
|
---|
400 | IPC_SET_ARG1(call->data, newphid);
|
---|
401 | break;
|
---|
402 | }
|
---|
403 | case IPC_M_CONNECT_ME:
|
---|
404 | IPC_SET_ARG5(call->data, (unative_t) phone);
|
---|
405 | break;
|
---|
406 | case IPC_M_CONNECT_ME_TO:
|
---|
407 | newphid = phone_alloc(TASK);
|
---|
408 | if (newphid < 0)
|
---|
409 | return ELIMIT;
|
---|
410 | /* Set arg5 for server */
|
---|
411 | IPC_SET_ARG5(call->data, (unative_t) &TASK->phones[newphid]);
|
---|
412 | call->flags |= IPC_CALL_CONN_ME_TO;
|
---|
413 | call->priv = newphid;
|
---|
414 | break;
|
---|
415 | case IPC_M_SHARE_OUT:
|
---|
416 | size = as_area_get_size(IPC_GET_ARG1(call->data));
|
---|
417 | if (!size)
|
---|
418 | return EPERM;
|
---|
419 | IPC_SET_ARG2(call->data, size);
|
---|
420 | break;
|
---|
421 | case IPC_M_DATA_READ:
|
---|
422 | size = IPC_GET_ARG2(call->data);
|
---|
423 | if ((size <= 0 || (size > DATA_XFER_LIMIT)))
|
---|
424 | return ELIMIT;
|
---|
425 | break;
|
---|
426 | case IPC_M_DATA_WRITE:
|
---|
427 | src = IPC_GET_ARG1(call->data);
|
---|
428 | size = IPC_GET_ARG2(call->data);
|
---|
429 |
|
---|
430 | if (size > DATA_XFER_LIMIT)
|
---|
431 | return ELIMIT;
|
---|
432 |
|
---|
433 | call->buffer = (uint8_t *) malloc(size, 0);
|
---|
434 | rc = copy_from_uspace(call->buffer, (void *) src, size);
|
---|
435 | if (rc != 0) {
|
---|
436 | free(call->buffer);
|
---|
437 | return rc;
|
---|
438 | }
|
---|
439 | break;
|
---|
440 | #ifdef CONFIG_UDEBUG
|
---|
441 | case IPC_M_DEBUG_ALL:
|
---|
442 | return udebug_request_preprocess(call, phone);
|
---|
443 | #endif
|
---|
444 | default:
|
---|
445 | break;
|
---|
446 | }
|
---|
447 | return 0;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /*******************************************************************************
|
---|
451 | * Functions called to process received call/answer before passing it to uspace.
|
---|
452 | *******************************************************************************/
|
---|
453 |
|
---|
454 | /** Do basic kernel processing of received call answer.
|
---|
455 | *
|
---|
456 | * @param call Call structure with the answer.
|
---|
457 | */
|
---|
458 | static void process_answer(call_t *call)
|
---|
459 | {
|
---|
460 | if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
|
---|
461 | (call->flags & IPC_CALL_FORWARDED))
|
---|
462 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
463 |
|
---|
464 | if (call->flags & IPC_CALL_CONN_ME_TO) {
|
---|
465 | if (IPC_GET_RETVAL(call->data))
|
---|
466 | phone_dealloc(call->priv);
|
---|
467 | else
|
---|
468 | IPC_SET_ARG5(call->data, call->priv);
|
---|
469 | }
|
---|
470 |
|
---|
471 | if (call->buffer) {
|
---|
472 | /* This must be an affirmative answer to IPC_M_DATA_READ. */
|
---|
473 | /* or IPC_M_DEBUG_ALL/UDEBUG_M_MEM_READ... */
|
---|
474 | uintptr_t dst = IPC_GET_ARG1(call->data);
|
---|
475 | size_t size = IPC_GET_ARG2(call->data);
|
---|
476 | int rc = copy_to_uspace((void *) dst, call->buffer, size);
|
---|
477 | if (rc)
|
---|
478 | IPC_SET_RETVAL(call->data, rc);
|
---|
479 | free(call->buffer);
|
---|
480 | call->buffer = NULL;
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | /** Do basic kernel processing of received call request.
|
---|
485 | *
|
---|
486 | * @param box Destination answerbox structure.
|
---|
487 | * @param call Call structure with the request.
|
---|
488 | *
|
---|
489 | * @return Return 0 if the call should be passed to userspace.
|
---|
490 | * Return -1 if the call should be ignored.
|
---|
491 | */
|
---|
492 | static int process_request(answerbox_t *box, call_t *call)
|
---|
493 | {
|
---|
494 | int phoneid;
|
---|
495 |
|
---|
496 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
|
---|
497 | phoneid = phone_alloc(TASK);
|
---|
498 | if (phoneid < 0) { /* Failed to allocate phone */
|
---|
499 | IPC_SET_RETVAL(call->data, ELIMIT);
|
---|
500 | ipc_answer(box, call);
|
---|
501 | return -1;
|
---|
502 | }
|
---|
503 | IPC_SET_ARG5(call->data, phoneid);
|
---|
504 | }
|
---|
505 | switch (IPC_GET_METHOD(call->data)) {
|
---|
506 | case IPC_M_DEBUG_ALL:
|
---|
507 | return -1;
|
---|
508 | default:
|
---|
509 | break;
|
---|
510 | }
|
---|
511 | return 0;
|
---|
512 | }
|
---|
513 |
|
---|
514 | /** Make a fast call over IPC, wait for reply and return to user.
|
---|
515 | *
|
---|
516 | * This function can handle only three arguments of payload, but is faster than
|
---|
517 | * the generic function (i.e. sys_ipc_call_sync_slow()).
|
---|
518 | *
|
---|
519 | * @param phoneid Phone handle for the call.
|
---|
520 | * @param method Method of the call.
|
---|
521 | * @param arg1 Service-defined payload argument.
|
---|
522 | * @param arg2 Service-defined payload argument.
|
---|
523 | * @param arg3 Service-defined payload argument.
|
---|
524 | * @param data Address of userspace structure where the reply call will
|
---|
525 | * be stored.
|
---|
526 | *
|
---|
527 | * @return Returns 0 on success.
|
---|
528 | * Return ENOENT if there is no such phone handle.
|
---|
529 | */
|
---|
530 | unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
|
---|
531 | unative_t arg1, unative_t arg2, unative_t arg3, ipc_data_t *data)
|
---|
532 | {
|
---|
533 | call_t call;
|
---|
534 | phone_t *phone;
|
---|
535 | int res;
|
---|
536 | int rc;
|
---|
537 |
|
---|
538 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
539 |
|
---|
540 | ipc_call_static_init(&call);
|
---|
541 | IPC_SET_METHOD(call.data, method);
|
---|
542 | IPC_SET_ARG1(call.data, arg1);
|
---|
543 | IPC_SET_ARG2(call.data, arg2);
|
---|
544 | IPC_SET_ARG3(call.data, arg3);
|
---|
545 | /*
|
---|
546 | * To achieve deterministic behavior, zero out arguments that are beyond
|
---|
547 | * the limits of the fast version.
|
---|
548 | */
|
---|
549 | IPC_SET_ARG4(call.data, 0);
|
---|
550 | IPC_SET_ARG5(call.data, 0);
|
---|
551 |
|
---|
552 | if (!(res = request_preprocess(&call, phone))) {
|
---|
553 | #ifdef CONFIG_UDEBUG
|
---|
554 | udebug_stoppable_begin();
|
---|
555 | #endif
|
---|
556 | rc = ipc_call_sync(phone, &call);
|
---|
557 | #ifdef CONFIG_UDEBUG
|
---|
558 | udebug_stoppable_end();
|
---|
559 | #endif
|
---|
560 | if (rc != EOK)
|
---|
561 | return rc;
|
---|
562 | process_answer(&call);
|
---|
563 |
|
---|
564 | } else {
|
---|
565 | IPC_SET_RETVAL(call.data, res);
|
---|
566 | }
|
---|
567 | rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
|
---|
568 | if (rc != 0)
|
---|
569 | return rc;
|
---|
570 |
|
---|
571 | return 0;
|
---|
572 | }
|
---|
573 |
|
---|
574 | /** Make a synchronous IPC call allowing to transmit the entire payload.
|
---|
575 | *
|
---|
576 | * @param phoneid Phone handle for the call.
|
---|
577 | * @param question Userspace address of call data with the request.
|
---|
578 | * @param reply Userspace address of call data where to store the
|
---|
579 | * answer.
|
---|
580 | *
|
---|
581 | * @return Zero on success or an error code.
|
---|
582 | */
|
---|
583 | unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question,
|
---|
584 | ipc_data_t *reply)
|
---|
585 | {
|
---|
586 | call_t call;
|
---|
587 | phone_t *phone;
|
---|
588 | int res;
|
---|
589 | int rc;
|
---|
590 |
|
---|
591 | ipc_call_static_init(&call);
|
---|
592 | rc = copy_from_uspace(&call.data.args, &question->args,
|
---|
593 | sizeof(call.data.args));
|
---|
594 | if (rc != 0)
|
---|
595 | return (unative_t) rc;
|
---|
596 |
|
---|
597 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
598 |
|
---|
599 | if (!(res = request_preprocess(&call, phone))) {
|
---|
600 | #ifdef CONFIG_UDEBUG
|
---|
601 | udebug_stoppable_begin();
|
---|
602 | #endif
|
---|
603 | rc = ipc_call_sync(phone, &call);
|
---|
604 | #ifdef CONFIG_UDEBUG
|
---|
605 | udebug_stoppable_end();
|
---|
606 | #endif
|
---|
607 | if (rc != EOK)
|
---|
608 | return rc;
|
---|
609 | process_answer(&call);
|
---|
610 | } else
|
---|
611 | IPC_SET_RETVAL(call.data, res);
|
---|
612 |
|
---|
613 | rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
|
---|
614 | if (rc != 0)
|
---|
615 | return rc;
|
---|
616 |
|
---|
617 | return 0;
|
---|
618 | }
|
---|
619 |
|
---|
620 | /** Check that the task did not exceed the allowed limit of asynchronous calls.
|
---|
621 | *
|
---|
622 | * @return Return 0 if limit not reached or -1 if limit exceeded.
|
---|
623 | */
|
---|
624 | static int check_call_limit(void)
|
---|
625 | {
|
---|
626 | if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
|
---|
627 | atomic_dec(&TASK->active_calls);
|
---|
628 | return -1;
|
---|
629 | }
|
---|
630 | return 0;
|
---|
631 | }
|
---|
632 |
|
---|
633 | /** Make a fast asynchronous call over IPC.
|
---|
634 | *
|
---|
635 | * This function can only handle four arguments of payload, but is faster than
|
---|
636 | * the generic function sys_ipc_call_async_slow().
|
---|
637 | *
|
---|
638 | * @param phoneid Phone handle for the call.
|
---|
639 | * @param method Method of the call.
|
---|
640 | * @param arg1 Service-defined payload argument.
|
---|
641 | * @param arg2 Service-defined payload argument.
|
---|
642 | * @param arg3 Service-defined payload argument.
|
---|
643 | * @param arg4 Service-defined payload argument.
|
---|
644 | *
|
---|
645 | * @return Return call hash on success.
|
---|
646 | * Return IPC_CALLRET_FATAL in case of a fatal error and
|
---|
647 | * IPC_CALLRET_TEMPORARY if there are too many pending
|
---|
648 | * asynchronous requests; answers should be handled first.
|
---|
649 | */
|
---|
650 | unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
|
---|
651 | unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
|
---|
652 | {
|
---|
653 | call_t *call;
|
---|
654 | phone_t *phone;
|
---|
655 | int res;
|
---|
656 |
|
---|
657 | if (check_call_limit())
|
---|
658 | return IPC_CALLRET_TEMPORARY;
|
---|
659 |
|
---|
660 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
|
---|
661 |
|
---|
662 | call = ipc_call_alloc(0);
|
---|
663 | IPC_SET_METHOD(call->data, method);
|
---|
664 | IPC_SET_ARG1(call->data, arg1);
|
---|
665 | IPC_SET_ARG2(call->data, arg2);
|
---|
666 | IPC_SET_ARG3(call->data, arg3);
|
---|
667 | IPC_SET_ARG4(call->data, arg4);
|
---|
668 | /*
|
---|
669 | * To achieve deterministic behavior, zero out arguments that are beyond
|
---|
670 | * the limits of the fast version.
|
---|
671 | */
|
---|
672 | IPC_SET_ARG5(call->data, 0);
|
---|
673 |
|
---|
674 | if (!(res = request_preprocess(call, phone)))
|
---|
675 | ipc_call(phone, call);
|
---|
676 | else
|
---|
677 | ipc_backsend_err(phone, call, res);
|
---|
678 |
|
---|
679 | return (unative_t) call;
|
---|
680 | }
|
---|
681 |
|
---|
682 | /** Make an asynchronous IPC call allowing to transmit the entire payload.
|
---|
683 | *
|
---|
684 | * @param phoneid Phone handle for the call.
|
---|
685 | * @param data Userspace address of call data with the request.
|
---|
686 | *
|
---|
687 | * @return See sys_ipc_call_async_fast().
|
---|
688 | */
|
---|
689 | unative_t sys_ipc_call_async_slow(unative_t phoneid, ipc_data_t *data)
|
---|
690 | {
|
---|
691 | call_t *call;
|
---|
692 | phone_t *phone;
|
---|
693 | int res;
|
---|
694 | int rc;
|
---|
695 |
|
---|
696 | if (check_call_limit())
|
---|
697 | return IPC_CALLRET_TEMPORARY;
|
---|
698 |
|
---|
699 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
|
---|
700 |
|
---|
701 | call = ipc_call_alloc(0);
|
---|
702 | rc = copy_from_uspace(&call->data.args, &data->args,
|
---|
703 | sizeof(call->data.args));
|
---|
704 | if (rc != 0) {
|
---|
705 | ipc_call_free(call);
|
---|
706 | return (unative_t) rc;
|
---|
707 | }
|
---|
708 | if (!(res = request_preprocess(call, phone)))
|
---|
709 | ipc_call(phone, call);
|
---|
710 | else
|
---|
711 | ipc_backsend_err(phone, call, res);
|
---|
712 |
|
---|
713 | return (unative_t) call;
|
---|
714 | }
|
---|
715 |
|
---|
716 | /** Forward a received call to another destination - common code for both the
|
---|
717 | * fast and the slow version.
|
---|
718 | *
|
---|
719 | * @param callid Hash of the call to forward.
|
---|
720 | * @param phoneid Phone handle to use for forwarding.
|
---|
721 | * @param method New method to use for the forwarded call.
|
---|
722 | * @param arg1 New value of the first argument for the forwarded call.
|
---|
723 | * @param arg2 New value of the second argument for the forwarded call.
|
---|
724 | * @param arg3 New value of the third argument for the forwarded call.
|
---|
725 | * @param arg4 New value of the fourth argument for the forwarded call.
|
---|
726 | * @param arg5 New value of the fifth argument for the forwarded call.
|
---|
727 | * @param mode Flags that specify mode of the forward operation.
|
---|
728 | * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
|
---|
729 | * the function considers only the fast version arguments:
|
---|
730 | * i.e. arg1 and arg2.
|
---|
731 | *
|
---|
732 | * @return Return 0 on succes, otherwise return an error code.
|
---|
733 | *
|
---|
734 | * Warning: Make sure that ARG5 is not rewritten for certain system IPC
|
---|
735 | */
|
---|
736 | static unative_t sys_ipc_forward_common(unative_t callid, unative_t phoneid,
|
---|
737 | unative_t method, unative_t arg1, unative_t arg2, unative_t arg3,
|
---|
738 | unative_t arg4, unative_t arg5, int mode, bool slow)
|
---|
739 | {
|
---|
740 | call_t *call;
|
---|
741 | phone_t *phone;
|
---|
742 |
|
---|
743 | call = get_call(callid);
|
---|
744 | if (!call)
|
---|
745 | return ENOENT;
|
---|
746 |
|
---|
747 | call->flags |= IPC_CALL_FORWARDED;
|
---|
748 |
|
---|
749 | GET_CHECK_PHONE(phone, phoneid, {
|
---|
750 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
751 | ipc_answer(&TASK->answerbox, call);
|
---|
752 | return ENOENT;
|
---|
753 | });
|
---|
754 |
|
---|
755 | if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
|
---|
756 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
757 | ipc_answer(&TASK->answerbox, call);
|
---|
758 | return EPERM;
|
---|
759 | }
|
---|
760 |
|
---|
761 | /*
|
---|
762 | * Userspace is not allowed to change method of system methods on
|
---|
763 | * forward, allow changing ARG1, ARG2, ARG3 and ARG4 by means of method,
|
---|
764 | * arg1, arg2 and arg3.
|
---|
765 | * If the method is immutable, don't change anything.
|
---|
766 | */
|
---|
767 | if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
|
---|
768 | if (method_is_system(IPC_GET_METHOD(call->data))) {
|
---|
769 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
|
---|
770 | phone_dealloc(IPC_GET_ARG5(call->data));
|
---|
771 |
|
---|
772 | IPC_SET_ARG1(call->data, method);
|
---|
773 | IPC_SET_ARG2(call->data, arg1);
|
---|
774 | IPC_SET_ARG3(call->data, arg2);
|
---|
775 | if (slow) {
|
---|
776 | IPC_SET_ARG4(call->data, arg3);
|
---|
777 | /*
|
---|
778 | * For system methods we deliberately don't
|
---|
779 | * overwrite ARG5.
|
---|
780 | */
|
---|
781 | }
|
---|
782 | } else {
|
---|
783 | IPC_SET_METHOD(call->data, method);
|
---|
784 | IPC_SET_ARG1(call->data, arg1);
|
---|
785 | IPC_SET_ARG2(call->data, arg2);
|
---|
786 | if (slow) {
|
---|
787 | IPC_SET_ARG3(call->data, arg3);
|
---|
788 | IPC_SET_ARG4(call->data, arg4);
|
---|
789 | IPC_SET_ARG5(call->data, arg5);
|
---|
790 | }
|
---|
791 | }
|
---|
792 | }
|
---|
793 |
|
---|
794 | return ipc_forward(call, phone, &TASK->answerbox, mode);
|
---|
795 | }
|
---|
796 |
|
---|
797 | /** Forward a received call to another destination - fast version.
|
---|
798 | *
|
---|
799 | * @param callid Hash of the call to forward.
|
---|
800 | * @param phoneid Phone handle to use for forwarding.
|
---|
801 | * @param method New method to use for the forwarded call.
|
---|
802 | * @param arg1 New value of the first argument for the forwarded call.
|
---|
803 | * @param arg2 New value of the second argument for the forwarded call.
|
---|
804 | * @param mode Flags that specify mode of the forward operation.
|
---|
805 | *
|
---|
806 | * @return Return 0 on succes, otherwise return an error code.
|
---|
807 | *
|
---|
808 | * In case the original method is a system method, ARG1, ARG2 and ARG3 are
|
---|
809 | * overwritten in the forwarded message with the new method and the new
|
---|
810 | * arg1 and arg2, respectively. Otherwise the METHOD, ARG1 and ARG2 are
|
---|
811 | * rewritten with the new method, arg1 and arg2, respectively. Also note there
|
---|
812 | * is a set of immutable methods, for which the new method and arguments are not
|
---|
813 | * set and these values are ignored.
|
---|
814 | */
|
---|
815 | unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
|
---|
816 | unative_t method, unative_t arg1, unative_t arg2, int mode)
|
---|
817 | {
|
---|
818 | return sys_ipc_forward_common(callid, phoneid, method, arg1, arg2, 0, 0,
|
---|
819 | 0, mode, false);
|
---|
820 | }
|
---|
821 |
|
---|
822 | /** Forward a received call to another destination - slow version.
|
---|
823 | *
|
---|
824 | * @param callid Hash of the call to forward.
|
---|
825 | * @param phoneid Phone handle to use for forwarding.
|
---|
826 | * @param data Userspace address of the new IPC data.
|
---|
827 | * @param mode Flags that specify mode of the forward operation.
|
---|
828 | *
|
---|
829 | * @return Return 0 on succes, otherwise return an error code.
|
---|
830 | *
|
---|
831 | * This function is the slow verision of the sys_ipc_forward_fast interface.
|
---|
832 | * It can copy all five new arguments and the new method from the userspace.
|
---|
833 | * It naturally extends the functionality of the fast version. For system
|
---|
834 | * methods, it additionally stores the new value of arg3 to ARG4. For non-system
|
---|
835 | * methods, it additionally stores the new value of arg3, arg4 and arg5,
|
---|
836 | * respectively, to ARG3, ARG4 and ARG5, respectively.
|
---|
837 | */
|
---|
838 | unative_t sys_ipc_forward_slow(unative_t callid, unative_t phoneid,
|
---|
839 | ipc_data_t *data, int mode)
|
---|
840 | {
|
---|
841 | ipc_data_t newdata;
|
---|
842 | int rc;
|
---|
843 |
|
---|
844 | rc = copy_from_uspace(&newdata.args, &data->args,
|
---|
845 | sizeof(newdata.args));
|
---|
846 | if (rc != 0)
|
---|
847 | return (unative_t) rc;
|
---|
848 |
|
---|
849 | return sys_ipc_forward_common(callid, phoneid,
|
---|
850 | IPC_GET_METHOD(newdata), IPC_GET_ARG1(newdata),
|
---|
851 | IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
|
---|
852 | IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
|
---|
853 | }
|
---|
854 |
|
---|
855 | /** Answer an IPC call - fast version.
|
---|
856 | *
|
---|
857 | * This function can handle only two return arguments of payload, but is faster
|
---|
858 | * than the generic sys_ipc_answer().
|
---|
859 | *
|
---|
860 | * @param callid Hash of the call to be answered.
|
---|
861 | * @param retval Return value of the answer.
|
---|
862 | * @param arg1 Service-defined return value.
|
---|
863 | * @param arg2 Service-defined return value.
|
---|
864 | * @param arg3 Service-defined return value.
|
---|
865 | * @param arg4 Service-defined return value.
|
---|
866 | *
|
---|
867 | * @return Return 0 on success, otherwise return an error code.
|
---|
868 | */
|
---|
869 | unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
|
---|
870 | unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
|
---|
871 | {
|
---|
872 | call_t *call;
|
---|
873 | ipc_data_t saved_data;
|
---|
874 | int saveddata = 0;
|
---|
875 | int rc;
|
---|
876 |
|
---|
877 | /* Do not answer notification callids */
|
---|
878 | if (callid & IPC_CALLID_NOTIFICATION)
|
---|
879 | return 0;
|
---|
880 |
|
---|
881 | call = get_call(callid);
|
---|
882 | if (!call)
|
---|
883 | return ENOENT;
|
---|
884 |
|
---|
885 | if (answer_need_old(call)) {
|
---|
886 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
887 | saveddata = 1;
|
---|
888 | }
|
---|
889 |
|
---|
890 | IPC_SET_RETVAL(call->data, retval);
|
---|
891 | IPC_SET_ARG1(call->data, arg1);
|
---|
892 | IPC_SET_ARG2(call->data, arg2);
|
---|
893 | IPC_SET_ARG3(call->data, arg3);
|
---|
894 | IPC_SET_ARG4(call->data, arg4);
|
---|
895 | /*
|
---|
896 | * To achieve deterministic behavior, zero out arguments that are beyond
|
---|
897 | * the limits of the fast version.
|
---|
898 | */
|
---|
899 | IPC_SET_ARG5(call->data, 0);
|
---|
900 | rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
|
---|
901 |
|
---|
902 | ipc_answer(&TASK->answerbox, call);
|
---|
903 | return rc;
|
---|
904 | }
|
---|
905 |
|
---|
906 | /** Answer an IPC call.
|
---|
907 | *
|
---|
908 | * @param callid Hash of the call to be answered.
|
---|
909 | * @param data Userspace address of call data with the answer.
|
---|
910 | *
|
---|
911 | * @return Return 0 on success, otherwise return an error code.
|
---|
912 | */
|
---|
913 | unative_t sys_ipc_answer_slow(unative_t callid, ipc_data_t *data)
|
---|
914 | {
|
---|
915 | call_t *call;
|
---|
916 | ipc_data_t saved_data;
|
---|
917 | int saveddata = 0;
|
---|
918 | int rc;
|
---|
919 |
|
---|
920 | /* Do not answer notification callids */
|
---|
921 | if (callid & IPC_CALLID_NOTIFICATION)
|
---|
922 | return 0;
|
---|
923 |
|
---|
924 | call = get_call(callid);
|
---|
925 | if (!call)
|
---|
926 | return ENOENT;
|
---|
927 |
|
---|
928 | if (answer_need_old(call)) {
|
---|
929 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
930 | saveddata = 1;
|
---|
931 | }
|
---|
932 | rc = copy_from_uspace(&call->data.args, &data->args,
|
---|
933 | sizeof(call->data.args));
|
---|
934 | if (rc != 0)
|
---|
935 | return rc;
|
---|
936 |
|
---|
937 | rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
|
---|
938 |
|
---|
939 | ipc_answer(&TASK->answerbox, call);
|
---|
940 |
|
---|
941 | return rc;
|
---|
942 | }
|
---|
943 |
|
---|
944 | /** Hang up a phone.
|
---|
945 | *
|
---|
946 | * @param Phone handle of the phone to be hung up.
|
---|
947 | *
|
---|
948 | * @return Return 0 on success or an error code.
|
---|
949 | */
|
---|
950 | unative_t sys_ipc_hangup(int phoneid)
|
---|
951 | {
|
---|
952 | phone_t *phone;
|
---|
953 |
|
---|
954 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
955 |
|
---|
956 | if (ipc_phone_hangup(phone))
|
---|
957 | return -1;
|
---|
958 |
|
---|
959 | return 0;
|
---|
960 | }
|
---|
961 |
|
---|
962 | /** Wait for an incoming IPC call or an answer.
|
---|
963 | *
|
---|
964 | * @param calldata Pointer to buffer where the call/answer data is stored.
|
---|
965 | * @param usec Timeout. See waitq_sleep_timeout() for explanation.
|
---|
966 | * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
|
---|
967 | * for explanation.
|
---|
968 | *
|
---|
969 | * @return Hash of the call.
|
---|
970 | * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
|
---|
971 | * call is a notification. IPC_CALLID_ANSWERED denotes an
|
---|
972 | * answer.
|
---|
973 | */
|
---|
974 | unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
|
---|
975 | {
|
---|
976 | call_t *call;
|
---|
977 |
|
---|
978 | restart:
|
---|
979 |
|
---|
980 | #ifdef CONFIG_UDEBUG
|
---|
981 | udebug_stoppable_begin();
|
---|
982 | #endif
|
---|
983 | call = ipc_wait_for_call(&TASK->answerbox, usec,
|
---|
984 | flags | SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
985 |
|
---|
986 | #ifdef CONFIG_UDEBUG
|
---|
987 | udebug_stoppable_end();
|
---|
988 | #endif
|
---|
989 | if (!call)
|
---|
990 | return 0;
|
---|
991 |
|
---|
992 | if (call->flags & IPC_CALL_NOTIF) {
|
---|
993 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
994 |
|
---|
995 | /* Set in_phone_hash to the interrupt counter */
|
---|
996 | call->data.phone = (void *) call->priv;
|
---|
997 |
|
---|
998 | STRUCT_TO_USPACE(calldata, &call->data);
|
---|
999 |
|
---|
1000 | ipc_call_free(call);
|
---|
1001 |
|
---|
1002 | return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | if (call->flags & IPC_CALL_ANSWERED) {
|
---|
1006 | process_answer(call);
|
---|
1007 |
|
---|
1008 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
1009 |
|
---|
1010 | if (call->flags & IPC_CALL_DISCARD_ANSWER) {
|
---|
1011 | ipc_call_free(call);
|
---|
1012 | goto restart;
|
---|
1013 | } else {
|
---|
1014 | /*
|
---|
1015 | * Decrement the counter of active calls only if the
|
---|
1016 | * call is not an answer to IPC_M_PHONE_HUNGUP,
|
---|
1017 | * which doesn't contribute to the counter.
|
---|
1018 | */
|
---|
1019 | atomic_dec(&TASK->active_calls);
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | STRUCT_TO_USPACE(&calldata->args, &call->data.args);
|
---|
1023 | ipc_call_free(call);
|
---|
1024 |
|
---|
1025 | return ((unative_t) call) | IPC_CALLID_ANSWERED;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | if (process_request(&TASK->answerbox, call))
|
---|
1029 | goto restart;
|
---|
1030 |
|
---|
1031 | /* Include phone address('id') of the caller in the request,
|
---|
1032 | * copy whole call->data, not only call->data.args */
|
---|
1033 | if (STRUCT_TO_USPACE(calldata, &call->data)) {
|
---|
1034 | /*
|
---|
1035 | * The callee will not receive this call and no one else has
|
---|
1036 | * a chance to answer it. Reply with the EPARTY error code.
|
---|
1037 | */
|
---|
1038 | ipc_data_t saved_data;
|
---|
1039 | int saveddata = 0;
|
---|
1040 |
|
---|
1041 | if (answer_need_old(call)) {
|
---|
1042 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
1043 | saveddata = 1;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | IPC_SET_RETVAL(call->data, EPARTY);
|
---|
1047 | (void) answer_preprocess(call, saveddata ? &saved_data : NULL);
|
---|
1048 | ipc_answer(&TASK->answerbox, call);
|
---|
1049 | return 0;
|
---|
1050 | }
|
---|
1051 | return (unative_t)call;
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | /** Interrupt one thread from sys_ipc_wait_for_call(). */
|
---|
1055 | unative_t sys_ipc_poke(void)
|
---|
1056 | {
|
---|
1057 | waitq_unsleep(&TASK->answerbox.wq);
|
---|
1058 | return EOK;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | /** Connect an IRQ handler to a task.
|
---|
1062 | *
|
---|
1063 | * @param inr IRQ number.
|
---|
1064 | * @param devno Device number.
|
---|
1065 | * @param method Method to be associated with the notification.
|
---|
1066 | * @param ucode Uspace pointer to the top-half pseudocode.
|
---|
1067 | *
|
---|
1068 | * @return EPERM or a return code returned by ipc_irq_register().
|
---|
1069 | */
|
---|
1070 | unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
|
---|
1071 | irq_code_t *ucode)
|
---|
1072 | {
|
---|
1073 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
---|
1074 | return EPERM;
|
---|
1075 |
|
---|
1076 | return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | /** Disconnect an IRQ handler from a task.
|
---|
1080 | *
|
---|
1081 | * @param inr IRQ number.
|
---|
1082 | * @param devno Device number.
|
---|
1083 | *
|
---|
1084 | * @return Zero on success or EPERM on error..
|
---|
1085 | */
|
---|
1086 | unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
|
---|
1087 | {
|
---|
1088 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
---|
1089 | return EPERM;
|
---|
1090 |
|
---|
1091 | ipc_irq_unregister(&TASK->answerbox, inr, devno);
|
---|
1092 |
|
---|
1093 | return 0;
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | #include <console/console.h>
|
---|
1097 |
|
---|
1098 | /**
|
---|
1099 | * Syscall connect to a task by id.
|
---|
1100 | *
|
---|
1101 | * @return Phone id on success, or negative error code.
|
---|
1102 | */
|
---|
1103 | unative_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid_arg)
|
---|
1104 | {
|
---|
1105 | #ifdef CONFIG_UDEBUG
|
---|
1106 | sysarg64_t taskid_arg;
|
---|
1107 | int rc;
|
---|
1108 |
|
---|
1109 | rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
|
---|
1110 | if (rc != 0)
|
---|
1111 | return (unative_t) rc;
|
---|
1112 |
|
---|
1113 | LOG("sys_ipc_connect_kbox(%" PRIu64 ")\n", taskid_arg.value);
|
---|
1114 |
|
---|
1115 | return ipc_connect_kbox(taskid_arg.value);
|
---|
1116 | #else
|
---|
1117 | return (unative_t) ENOTSUP;
|
---|
1118 | #endif
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | /** @}
|
---|
1122 | */
|
---|