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 <errno.h>
|
---|
37 | #include <memstr.h>
|
---|
38 | #include <ipc/ipc.h>
|
---|
39 | #include <abi/ipc/methods.h>
|
---|
40 | #include <ipc/sysipc.h>
|
---|
41 | #include <ipc/sysipc_ops.h>
|
---|
42 | #include <ipc/sysipc_priv.h>
|
---|
43 | #include <ipc/irq.h>
|
---|
44 | #include <ipc/ipcrsc.h>
|
---|
45 | #include <ipc/event.h>
|
---|
46 | #include <ipc/kbox.h>
|
---|
47 | #include <synch/waitq.h>
|
---|
48 | #include <arch/interrupt.h>
|
---|
49 | #include <syscall/copy.h>
|
---|
50 | #include <security/cap.h>
|
---|
51 | #include <console/console.h>
|
---|
52 | #include <print.h>
|
---|
53 | #include <macros.h>
|
---|
54 |
|
---|
55 | #define STRUCT_TO_USPACE(dst, src) copy_to_uspace((dst), (src), sizeof(*(src)))
|
---|
56 |
|
---|
57 | /** Decide if the interface and method is a system method.
|
---|
58 | *
|
---|
59 | * @param imethod Interface and method to be decided.
|
---|
60 | *
|
---|
61 | * @return True if the interface and method is a system
|
---|
62 | * interface and method.
|
---|
63 | *
|
---|
64 | */
|
---|
65 | static inline bool method_is_system(sysarg_t imethod)
|
---|
66 | {
|
---|
67 | if (imethod <= IPC_M_LAST_SYSTEM)
|
---|
68 | return true;
|
---|
69 |
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /** Decide if the message with this interface and method is forwardable.
|
---|
74 | *
|
---|
75 | * Some system messages may be forwarded, for some of them
|
---|
76 | * it is useless.
|
---|
77 | *
|
---|
78 | * @param imethod Interface and method to be decided.
|
---|
79 | *
|
---|
80 | * @return True if the interface and method is forwardable.
|
---|
81 | *
|
---|
82 | */
|
---|
83 | static inline bool method_is_forwardable(sysarg_t imethod)
|
---|
84 | {
|
---|
85 | switch (imethod) {
|
---|
86 | case IPC_M_CONNECTION_CLONE:
|
---|
87 | case IPC_M_CLONE_ESTABLISH:
|
---|
88 | case IPC_M_PHONE_HUNGUP:
|
---|
89 | /* This message is meant only for the original recipient. */
|
---|
90 | return false;
|
---|
91 | default:
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Decide if the message with this interface and method is immutable on forward.
|
---|
97 | *
|
---|
98 | * Some system messages may be forwarded but their content cannot be altered.
|
---|
99 | *
|
---|
100 | * @param imethod Interface and method to be decided.
|
---|
101 | *
|
---|
102 | * @return True if the interface and method is immutable on forward.
|
---|
103 | *
|
---|
104 | */
|
---|
105 | static inline bool method_is_immutable(sysarg_t imethod)
|
---|
106 | {
|
---|
107 | switch (imethod) {
|
---|
108 | case IPC_M_SHARE_OUT:
|
---|
109 | case IPC_M_SHARE_IN:
|
---|
110 | case IPC_M_DATA_WRITE:
|
---|
111 | case IPC_M_DATA_READ:
|
---|
112 | case IPC_M_STATE_CHANGE_AUTHORIZE:
|
---|
113 | return true;
|
---|
114 | default:
|
---|
115 | return false;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /***********************************************************************
|
---|
121 | * Functions that preprocess answer before sending it to the recepient.
|
---|
122 | ***********************************************************************/
|
---|
123 |
|
---|
124 | /** Decide if the caller (e.g. ipc_answer()) should save the old call contents
|
---|
125 | * for answer_preprocess().
|
---|
126 | *
|
---|
127 | * @param call Call structure to be decided.
|
---|
128 | *
|
---|
129 | * @return true if the old call contents should be saved.
|
---|
130 | *
|
---|
131 | */
|
---|
132 | static inline bool answer_need_old(call_t *call)
|
---|
133 | {
|
---|
134 | switch (IPC_GET_IMETHOD(call->data)) {
|
---|
135 | case IPC_M_CONNECTION_CLONE:
|
---|
136 | case IPC_M_CLONE_ESTABLISH:
|
---|
137 | case IPC_M_CONNECT_TO_ME:
|
---|
138 | case IPC_M_CONNECT_ME_TO:
|
---|
139 | case IPC_M_SHARE_OUT:
|
---|
140 | case IPC_M_SHARE_IN:
|
---|
141 | case IPC_M_DATA_WRITE:
|
---|
142 | case IPC_M_DATA_READ:
|
---|
143 | case IPC_M_STATE_CHANGE_AUTHORIZE:
|
---|
144 | return true;
|
---|
145 | default:
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Interpret process answer as control information.
|
---|
151 | *
|
---|
152 | * This function is called directly after sys_ipc_answer().
|
---|
153 | *
|
---|
154 | * @param answer Call structure with the answer.
|
---|
155 | * @param olddata Saved data of the request.
|
---|
156 | *
|
---|
157 | * @return Return EOK on success or a negative error code.
|
---|
158 | *
|
---|
159 | */
|
---|
160 | int answer_preprocess(call_t *answer, ipc_data_t *olddata)
|
---|
161 | {
|
---|
162 | int rc = EOK;
|
---|
163 | sysipc_ops_t *ops;
|
---|
164 |
|
---|
165 | spinlock_lock(&answer->forget_lock);
|
---|
166 | if (answer->forget) {
|
---|
167 | /*
|
---|
168 | * This is a forgotten call and answer->sender is not valid.
|
---|
169 | */
|
---|
170 | spinlock_unlock(&answer->forget_lock);
|
---|
171 |
|
---|
172 | ops = sysipc_ops_get(answer->request_method);
|
---|
173 | if (ops->answer_cleanup)
|
---|
174 | ops->answer_cleanup(answer, olddata);
|
---|
175 |
|
---|
176 | return rc;
|
---|
177 | } else {
|
---|
178 | ASSERT(answer->active);
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Mark the call as inactive to prevent _ipc_answer_free_call()
|
---|
182 | * from attempting to remove the call from the active list
|
---|
183 | * itself.
|
---|
184 | */
|
---|
185 | answer->active = false;
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Remove the call from the sender's active call list.
|
---|
189 | * We enforce this locking order so that any potential
|
---|
190 | * concurrently executing forget operation is forced to
|
---|
191 | * release its active_calls_lock and lose the race to
|
---|
192 | * forget this soon to be answered call.
|
---|
193 | */
|
---|
194 | spinlock_lock(&answer->sender->active_calls_lock);
|
---|
195 | list_remove(&answer->ta_link);
|
---|
196 | spinlock_unlock(&answer->sender->active_calls_lock);
|
---|
197 | }
|
---|
198 | spinlock_unlock(&answer->forget_lock);
|
---|
199 |
|
---|
200 | if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
|
---|
201 | /* In case of forward, hangup the forwared phone,
|
---|
202 | * not the originator
|
---|
203 | */
|
---|
204 | mutex_lock(&answer->data.phone->lock);
|
---|
205 | irq_spinlock_lock(&TASK->answerbox.lock, true);
|
---|
206 | if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
|
---|
207 | list_remove(&answer->data.phone->link);
|
---|
208 | answer->data.phone->state = IPC_PHONE_SLAMMED;
|
---|
209 | }
|
---|
210 | irq_spinlock_unlock(&TASK->answerbox.lock, true);
|
---|
211 | mutex_unlock(&answer->data.phone->lock);
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (!olddata)
|
---|
215 | return rc;
|
---|
216 |
|
---|
217 | ops = sysipc_ops_get(answer->request_method);
|
---|
218 | if (ops->answer_preprocess)
|
---|
219 | rc = ops->answer_preprocess(answer, olddata);
|
---|
220 |
|
---|
221 | return rc;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /** Called before the request is sent.
|
---|
225 | *
|
---|
226 | * @param call Call structure with the request.
|
---|
227 | * @param phone Phone that the call will be sent through.
|
---|
228 | *
|
---|
229 | * @return Return 0 on success, ELIMIT or EPERM on error.
|
---|
230 | *
|
---|
231 | */
|
---|
232 | static int request_preprocess(call_t *call, phone_t *phone)
|
---|
233 | {
|
---|
234 | int rc = EOK;
|
---|
235 |
|
---|
236 | call->request_method = IPC_GET_IMETHOD(call->data);
|
---|
237 |
|
---|
238 | sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
|
---|
239 | if (ops->request_preprocess)
|
---|
240 | rc = ops->request_preprocess(call, phone);
|
---|
241 |
|
---|
242 | return rc;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /*******************************************************************************
|
---|
246 | * Functions called to process received call/answer before passing it to uspace.
|
---|
247 | *******************************************************************************/
|
---|
248 |
|
---|
249 | /** Do basic kernel processing of received call answer.
|
---|
250 | *
|
---|
251 | * @param call Call structure with the answer.
|
---|
252 | *
|
---|
253 | */
|
---|
254 | static void process_answer(call_t *call)
|
---|
255 | {
|
---|
256 | if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
|
---|
257 | (call->flags & IPC_CALL_FORWARDED))
|
---|
258 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
259 |
|
---|
260 | sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
|
---|
261 | if (ops->answer_process)
|
---|
262 | (void) ops->answer_process(call);
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | /** Do basic kernel processing of received call request.
|
---|
267 | *
|
---|
268 | * @param box Destination answerbox structure.
|
---|
269 | * @param call Call structure with the request.
|
---|
270 | *
|
---|
271 | * @return 0 if the call should be passed to userspace.
|
---|
272 | * @return -1 if the call should be ignored.
|
---|
273 | *
|
---|
274 | */
|
---|
275 | static int process_request(answerbox_t *box, call_t *call)
|
---|
276 | {
|
---|
277 | int rc = EOK;
|
---|
278 |
|
---|
279 | sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
|
---|
280 | if (ops->request_process)
|
---|
281 | rc = ops->request_process(call, box);
|
---|
282 |
|
---|
283 | return rc;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /** Check that the task did not exceed the allowed limit of asynchronous calls
|
---|
287 | * made over a phone.
|
---|
288 | *
|
---|
289 | * @param phone Phone to check the limit against.
|
---|
290 | *
|
---|
291 | * @return 0 if limit not reached or -1 if limit exceeded.
|
---|
292 | *
|
---|
293 | */
|
---|
294 | static int check_call_limit(phone_t *phone)
|
---|
295 | {
|
---|
296 | if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
|
---|
297 | return -1;
|
---|
298 |
|
---|
299 | return 0;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /** Make a fast asynchronous call over IPC.
|
---|
303 | *
|
---|
304 | * This function can only handle four arguments of payload, but is faster than
|
---|
305 | * the generic function sys_ipc_call_async_slow().
|
---|
306 | *
|
---|
307 | * @param phoneid Phone handle for the call.
|
---|
308 | * @param imethod Interface and method of the call.
|
---|
309 | * @param arg1 Service-defined payload argument.
|
---|
310 | * @param arg2 Service-defined payload argument.
|
---|
311 | * @param arg3 Service-defined payload argument.
|
---|
312 | * @param arg4 Service-defined payload argument.
|
---|
313 | *
|
---|
314 | * @return Call hash on success.
|
---|
315 | * @return IPC_CALLRET_FATAL in case of a fatal error.
|
---|
316 | * @return IPC_CALLRET_TEMPORARY if there are too many pending
|
---|
317 | * asynchronous requests; answers should be handled first.
|
---|
318 | *
|
---|
319 | */
|
---|
320 | sysarg_t sys_ipc_call_async_fast(sysarg_t phoneid, sysarg_t imethod,
|
---|
321 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
322 | {
|
---|
323 | phone_t *phone;
|
---|
324 | if (phone_get(phoneid, &phone) != EOK)
|
---|
325 | return IPC_CALLRET_FATAL;
|
---|
326 |
|
---|
327 | if (check_call_limit(phone))
|
---|
328 | return IPC_CALLRET_TEMPORARY;
|
---|
329 |
|
---|
330 | call_t *call = ipc_call_alloc(0);
|
---|
331 | IPC_SET_IMETHOD(call->data, imethod);
|
---|
332 | IPC_SET_ARG1(call->data, arg1);
|
---|
333 | IPC_SET_ARG2(call->data, arg2);
|
---|
334 | IPC_SET_ARG3(call->data, arg3);
|
---|
335 | IPC_SET_ARG4(call->data, arg4);
|
---|
336 |
|
---|
337 | /*
|
---|
338 | * To achieve deterministic behavior, zero out arguments that are beyond
|
---|
339 | * the limits of the fast version.
|
---|
340 | */
|
---|
341 | IPC_SET_ARG5(call->data, 0);
|
---|
342 |
|
---|
343 | int res = request_preprocess(call, phone);
|
---|
344 |
|
---|
345 | if (!res)
|
---|
346 | ipc_call(phone, call);
|
---|
347 | else
|
---|
348 | ipc_backsend_err(phone, call, res);
|
---|
349 |
|
---|
350 | return (sysarg_t) call;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /** Make an asynchronous IPC call allowing to transmit the entire payload.
|
---|
354 | *
|
---|
355 | * @param phoneid Phone handle for the call.
|
---|
356 | * @param data Userspace address of call data with the request.
|
---|
357 | *
|
---|
358 | * @return See sys_ipc_call_async_fast().
|
---|
359 | *
|
---|
360 | */
|
---|
361 | sysarg_t sys_ipc_call_async_slow(sysarg_t phoneid, ipc_data_t *data)
|
---|
362 | {
|
---|
363 | phone_t *phone;
|
---|
364 | if (phone_get(phoneid, &phone) != EOK)
|
---|
365 | return IPC_CALLRET_FATAL;
|
---|
366 |
|
---|
367 | if (check_call_limit(phone))
|
---|
368 | return IPC_CALLRET_TEMPORARY;
|
---|
369 |
|
---|
370 | call_t *call = ipc_call_alloc(0);
|
---|
371 | int rc = copy_from_uspace(&call->data.args, &data->args,
|
---|
372 | sizeof(call->data.args));
|
---|
373 | if (rc != 0) {
|
---|
374 | ipc_call_free(call);
|
---|
375 | return (sysarg_t) rc;
|
---|
376 | }
|
---|
377 |
|
---|
378 | int res = request_preprocess(call, phone);
|
---|
379 |
|
---|
380 | if (!res)
|
---|
381 | ipc_call(phone, call);
|
---|
382 | else
|
---|
383 | ipc_backsend_err(phone, call, res);
|
---|
384 |
|
---|
385 | return (sysarg_t) call;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /** Forward a received call to another destination
|
---|
389 | *
|
---|
390 | * Common code for both the fast and the slow version.
|
---|
391 | *
|
---|
392 | * @param callid Hash of the call to forward.
|
---|
393 | * @param phoneid Phone handle to use for forwarding.
|
---|
394 | * @param imethod New interface and method to use for the forwarded call.
|
---|
395 | * @param arg1 New value of the first argument for the forwarded call.
|
---|
396 | * @param arg2 New value of the second argument for the forwarded call.
|
---|
397 | * @param arg3 New value of the third argument for the forwarded call.
|
---|
398 | * @param arg4 New value of the fourth argument for the forwarded call.
|
---|
399 | * @param arg5 New value of the fifth argument for the forwarded call.
|
---|
400 | * @param mode Flags that specify mode of the forward operation.
|
---|
401 | * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
|
---|
402 | * the function considers only the fast version arguments:
|
---|
403 | * i.e. arg1 and arg2.
|
---|
404 | *
|
---|
405 | * @return 0 on succes, otherwise an error code.
|
---|
406 | *
|
---|
407 | * Warning: Make sure that ARG5 is not rewritten for certain system IPC
|
---|
408 | *
|
---|
409 | */
|
---|
410 | static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t phoneid,
|
---|
411 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
412 | sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
|
---|
413 | {
|
---|
414 | call_t *call = get_call(callid);
|
---|
415 | phone_t *phone;
|
---|
416 | bool need_old = answer_need_old(call);
|
---|
417 | bool after_forward = false;
|
---|
418 | ipc_data_t old;
|
---|
419 | int rc;
|
---|
420 |
|
---|
421 | if (!call)
|
---|
422 | return ENOENT;
|
---|
423 |
|
---|
424 | if (need_old)
|
---|
425 | old = call->data;
|
---|
426 |
|
---|
427 | if (phone_get(phoneid, &phone) != EOK) {
|
---|
428 | rc = ENOENT;
|
---|
429 | goto error;
|
---|
430 | }
|
---|
431 |
|
---|
432 | if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
|
---|
433 | rc = EPERM;
|
---|
434 | goto error;
|
---|
435 | }
|
---|
436 |
|
---|
437 | call->flags |= IPC_CALL_FORWARDED;
|
---|
438 |
|
---|
439 | /*
|
---|
440 | * User space is not allowed to change interface and method of system
|
---|
441 | * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
|
---|
442 | * means of imethod, arg1, arg2 and arg3.
|
---|
443 | * If the interface and method is immutable, don't change anything.
|
---|
444 | */
|
---|
445 | if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
|
---|
446 | if (method_is_system(IPC_GET_IMETHOD(call->data))) {
|
---|
447 | if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
|
---|
448 | phone_dealloc(IPC_GET_ARG5(call->data));
|
---|
449 |
|
---|
450 | IPC_SET_ARG1(call->data, imethod);
|
---|
451 | IPC_SET_ARG2(call->data, arg1);
|
---|
452 | IPC_SET_ARG3(call->data, arg2);
|
---|
453 |
|
---|
454 | if (slow)
|
---|
455 | IPC_SET_ARG4(call->data, arg3);
|
---|
456 |
|
---|
457 | /*
|
---|
458 | * For system methods we deliberately don't
|
---|
459 | * overwrite ARG5.
|
---|
460 | */
|
---|
461 | } else {
|
---|
462 | IPC_SET_IMETHOD(call->data, imethod);
|
---|
463 | IPC_SET_ARG1(call->data, arg1);
|
---|
464 | IPC_SET_ARG2(call->data, arg2);
|
---|
465 | if (slow) {
|
---|
466 | IPC_SET_ARG3(call->data, arg3);
|
---|
467 | IPC_SET_ARG4(call->data, arg4);
|
---|
468 | IPC_SET_ARG5(call->data, arg5);
|
---|
469 | }
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 | rc = ipc_forward(call, phone, &TASK->answerbox, mode);
|
---|
474 | if (rc != EOK) {
|
---|
475 | after_forward = true;
|
---|
476 | goto error;
|
---|
477 | }
|
---|
478 |
|
---|
479 | return EOK;
|
---|
480 |
|
---|
481 | error:
|
---|
482 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
483 | (void) answer_preprocess(call, need_old ? &old : NULL);
|
---|
484 | if (after_forward)
|
---|
485 | _ipc_answer_free_call(call, false);
|
---|
486 | else
|
---|
487 | ipc_answer(&TASK->answerbox, call);
|
---|
488 |
|
---|
489 | return rc;
|
---|
490 | }
|
---|
491 |
|
---|
492 | /** Forward a received call to another destination - fast version.
|
---|
493 | *
|
---|
494 | * In case the original interface and method is a system method, ARG1, ARG2
|
---|
495 | * and ARG3 are overwritten in the forwarded message with the new method and
|
---|
496 | * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
|
---|
497 | * are rewritten with the new interface and method, arg1 and arg2, respectively.
|
---|
498 | * Also note there is a set of immutable methods, for which the new method and
|
---|
499 | * arguments are not set and these values are ignored.
|
---|
500 | *
|
---|
501 | * @param callid Hash of the call to forward.
|
---|
502 | * @param phoneid Phone handle to use for forwarding.
|
---|
503 | * @param imethod New interface and method to use for the forwarded call.
|
---|
504 | * @param arg1 New value of the first argument for the forwarded call.
|
---|
505 | * @param arg2 New value of the second argument for the forwarded call.
|
---|
506 | * @param mode Flags that specify mode of the forward operation.
|
---|
507 | *
|
---|
508 | * @return 0 on succes, otherwise an error code.
|
---|
509 | *
|
---|
510 | */
|
---|
511 | sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t phoneid,
|
---|
512 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
513 | {
|
---|
514 | return sys_ipc_forward_common(callid, phoneid, imethod, arg1, arg2, 0, 0,
|
---|
515 | 0, mode, false);
|
---|
516 | }
|
---|
517 |
|
---|
518 | /** Forward a received call to another destination - slow version.
|
---|
519 | *
|
---|
520 | * This function is the slow verision of the sys_ipc_forward_fast interface.
|
---|
521 | * It can copy all five new arguments and the new interface and method from
|
---|
522 | * the userspace. It naturally extends the functionality of the fast version.
|
---|
523 | * For system methods, it additionally stores the new value of arg3 to ARG4.
|
---|
524 | * For non-system methods, it additionally stores the new value of arg3, arg4
|
---|
525 | * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
|
---|
526 | *
|
---|
527 | * @param callid Hash of the call to forward.
|
---|
528 | * @param phoneid Phone handle to use for forwarding.
|
---|
529 | * @param data Userspace address of the new IPC data.
|
---|
530 | * @param mode Flags that specify mode of the forward operation.
|
---|
531 | *
|
---|
532 | * @return 0 on succes, otherwise an error code.
|
---|
533 | *
|
---|
534 | */
|
---|
535 | sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t phoneid,
|
---|
536 | ipc_data_t *data, unsigned int mode)
|
---|
537 | {
|
---|
538 | ipc_data_t newdata;
|
---|
539 | int rc = copy_from_uspace(&newdata.args, &data->args,
|
---|
540 | sizeof(newdata.args));
|
---|
541 | if (rc != 0)
|
---|
542 | return (sysarg_t) rc;
|
---|
543 |
|
---|
544 | return sys_ipc_forward_common(callid, phoneid,
|
---|
545 | IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
|
---|
546 | IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
|
---|
547 | IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
|
---|
548 | }
|
---|
549 |
|
---|
550 | /** Answer an IPC call - fast version.
|
---|
551 | *
|
---|
552 | * This function can handle only two return arguments of payload, but is faster
|
---|
553 | * than the generic sys_ipc_answer().
|
---|
554 | *
|
---|
555 | * @param callid Hash of the call to be answered.
|
---|
556 | * @param retval Return value of the answer.
|
---|
557 | * @param arg1 Service-defined return value.
|
---|
558 | * @param arg2 Service-defined return value.
|
---|
559 | * @param arg3 Service-defined return value.
|
---|
560 | * @param arg4 Service-defined return value.
|
---|
561 | *
|
---|
562 | * @return 0 on success, otherwise an error code.
|
---|
563 | *
|
---|
564 | */
|
---|
565 | sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
|
---|
566 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
567 | {
|
---|
568 | /* Do not answer notification callids */
|
---|
569 | if (callid & IPC_CALLID_NOTIFICATION)
|
---|
570 | return 0;
|
---|
571 |
|
---|
572 | call_t *call = get_call(callid);
|
---|
573 | if (!call)
|
---|
574 | return ENOENT;
|
---|
575 |
|
---|
576 | ipc_data_t saved_data;
|
---|
577 | bool saved;
|
---|
578 |
|
---|
579 | if (answer_need_old(call)) {
|
---|
580 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
581 | saved = true;
|
---|
582 | } else
|
---|
583 | saved = false;
|
---|
584 |
|
---|
585 | IPC_SET_RETVAL(call->data, retval);
|
---|
586 | IPC_SET_ARG1(call->data, arg1);
|
---|
587 | IPC_SET_ARG2(call->data, arg2);
|
---|
588 | IPC_SET_ARG3(call->data, arg3);
|
---|
589 | IPC_SET_ARG4(call->data, arg4);
|
---|
590 |
|
---|
591 | /*
|
---|
592 | * To achieve deterministic behavior, zero out arguments that are beyond
|
---|
593 | * the limits of the fast version.
|
---|
594 | */
|
---|
595 | IPC_SET_ARG5(call->data, 0);
|
---|
596 | int rc = answer_preprocess(call, saved ? &saved_data : NULL);
|
---|
597 |
|
---|
598 | ipc_answer(&TASK->answerbox, call);
|
---|
599 | return rc;
|
---|
600 | }
|
---|
601 |
|
---|
602 | /** Answer an IPC call.
|
---|
603 | *
|
---|
604 | * @param callid Hash of the call to be answered.
|
---|
605 | * @param data Userspace address of call data with the answer.
|
---|
606 | *
|
---|
607 | * @return 0 on success, otherwise an error code.
|
---|
608 | *
|
---|
609 | */
|
---|
610 | sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
|
---|
611 | {
|
---|
612 | /* Do not answer notification callids */
|
---|
613 | if (callid & IPC_CALLID_NOTIFICATION)
|
---|
614 | return 0;
|
---|
615 |
|
---|
616 | call_t *call = get_call(callid);
|
---|
617 | if (!call)
|
---|
618 | return ENOENT;
|
---|
619 |
|
---|
620 | ipc_data_t saved_data;
|
---|
621 | bool saved;
|
---|
622 |
|
---|
623 | if (answer_need_old(call)) {
|
---|
624 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
625 | saved = true;
|
---|
626 | } else
|
---|
627 | saved = false;
|
---|
628 |
|
---|
629 | int rc = copy_from_uspace(&call->data.args, &data->args,
|
---|
630 | sizeof(call->data.args));
|
---|
631 | if (rc != 0)
|
---|
632 | return rc;
|
---|
633 |
|
---|
634 | rc = answer_preprocess(call, saved ? &saved_data : NULL);
|
---|
635 |
|
---|
636 | ipc_answer(&TASK->answerbox, call);
|
---|
637 | return rc;
|
---|
638 | }
|
---|
639 |
|
---|
640 | /** Hang up a phone.
|
---|
641 | *
|
---|
642 | * @param Phone handle of the phone to be hung up.
|
---|
643 | *
|
---|
644 | * @return 0 on success or an error code.
|
---|
645 | *
|
---|
646 | */
|
---|
647 | sysarg_t sys_ipc_hangup(sysarg_t phoneid)
|
---|
648 | {
|
---|
649 | phone_t *phone;
|
---|
650 |
|
---|
651 | if (phone_get(phoneid, &phone) != EOK)
|
---|
652 | return ENOENT;
|
---|
653 |
|
---|
654 | if (ipc_phone_hangup(phone))
|
---|
655 | return -1;
|
---|
656 |
|
---|
657 | return 0;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /** Wait for an incoming IPC call or an answer.
|
---|
661 | *
|
---|
662 | * @param calldata Pointer to buffer where the call/answer data is stored.
|
---|
663 | * @param usec Timeout. See waitq_sleep_timeout() for explanation.
|
---|
664 | * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
|
---|
665 | * for explanation.
|
---|
666 | *
|
---|
667 | * @return Hash of the call.
|
---|
668 | * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
|
---|
669 | * call is a notification. IPC_CALLID_ANSWERED denotes an
|
---|
670 | * answer.
|
---|
671 | *
|
---|
672 | */
|
---|
673 | sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
|
---|
674 | unsigned int flags)
|
---|
675 | {
|
---|
676 | call_t *call;
|
---|
677 |
|
---|
678 | restart:
|
---|
679 |
|
---|
680 | #ifdef CONFIG_UDEBUG
|
---|
681 | udebug_stoppable_begin();
|
---|
682 | #endif
|
---|
683 |
|
---|
684 | call = ipc_wait_for_call(&TASK->answerbox, usec,
|
---|
685 | flags | SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
686 |
|
---|
687 | #ifdef CONFIG_UDEBUG
|
---|
688 | udebug_stoppable_end();
|
---|
689 | #endif
|
---|
690 |
|
---|
691 | if (!call)
|
---|
692 | return 0;
|
---|
693 |
|
---|
694 | if (call->flags & IPC_CALL_NOTIF) {
|
---|
695 | /* Set in_phone_hash to the interrupt counter */
|
---|
696 | call->data.phone = (void *) call->priv;
|
---|
697 |
|
---|
698 | STRUCT_TO_USPACE(calldata, &call->data);
|
---|
699 |
|
---|
700 | ipc_call_free(call);
|
---|
701 |
|
---|
702 | return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
|
---|
703 | }
|
---|
704 |
|
---|
705 | if (call->flags & IPC_CALL_ANSWERED) {
|
---|
706 | process_answer(call);
|
---|
707 |
|
---|
708 | if (call->flags & IPC_CALL_DISCARD_ANSWER) {
|
---|
709 | ipc_call_free(call);
|
---|
710 | goto restart;
|
---|
711 | }
|
---|
712 |
|
---|
713 | STRUCT_TO_USPACE(&calldata->args, &call->data.args);
|
---|
714 | ipc_call_free(call);
|
---|
715 |
|
---|
716 | return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
|
---|
717 | }
|
---|
718 |
|
---|
719 | if (process_request(&TASK->answerbox, call))
|
---|
720 | goto restart;
|
---|
721 |
|
---|
722 | /* Include phone address('id') of the caller in the request,
|
---|
723 | * copy whole call->data, not only call->data.args */
|
---|
724 | if (STRUCT_TO_USPACE(calldata, &call->data)) {
|
---|
725 | /*
|
---|
726 | * The callee will not receive this call and no one else has
|
---|
727 | * a chance to answer it. Reply with the EPARTY error code.
|
---|
728 | */
|
---|
729 | ipc_data_t saved_data;
|
---|
730 | bool saved;
|
---|
731 |
|
---|
732 | if (answer_need_old(call)) {
|
---|
733 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
734 | saved = true;
|
---|
735 | } else
|
---|
736 | saved = false;
|
---|
737 |
|
---|
738 | IPC_SET_RETVAL(call->data, EPARTY);
|
---|
739 | (void) answer_preprocess(call, saved ? &saved_data : NULL);
|
---|
740 | ipc_answer(&TASK->answerbox, call);
|
---|
741 | return 0;
|
---|
742 | }
|
---|
743 |
|
---|
744 | return (sysarg_t) call;
|
---|
745 | }
|
---|
746 |
|
---|
747 | /** Interrupt one thread from sys_ipc_wait_for_call().
|
---|
748 | *
|
---|
749 | */
|
---|
750 | sysarg_t sys_ipc_poke(void)
|
---|
751 | {
|
---|
752 | waitq_unsleep(&TASK->answerbox.wq);
|
---|
753 | return EOK;
|
---|
754 | }
|
---|
755 |
|
---|
756 | /** Connect an IRQ handler to a task.
|
---|
757 | *
|
---|
758 | * @param inr IRQ number.
|
---|
759 | * @param devno Device number.
|
---|
760 | * @param imethod Interface and method to be associated with the notification.
|
---|
761 | * @param ucode Uspace pointer to the top-half pseudocode.
|
---|
762 | *
|
---|
763 | * @return EPERM or a return code returned by ipc_irq_register().
|
---|
764 | *
|
---|
765 | */
|
---|
766 | sysarg_t sys_irq_register(inr_t inr, devno_t devno, sysarg_t imethod,
|
---|
767 | irq_code_t *ucode)
|
---|
768 | {
|
---|
769 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
---|
770 | return EPERM;
|
---|
771 |
|
---|
772 | return ipc_irq_register(&TASK->answerbox, inr, devno, imethod, ucode);
|
---|
773 | }
|
---|
774 |
|
---|
775 | /** Disconnect an IRQ handler from a task.
|
---|
776 | *
|
---|
777 | * @param inr IRQ number.
|
---|
778 | * @param devno Device number.
|
---|
779 | *
|
---|
780 | * @return Zero on success or EPERM on error.
|
---|
781 | *
|
---|
782 | */
|
---|
783 | sysarg_t sys_irq_unregister(inr_t inr, devno_t devno)
|
---|
784 | {
|
---|
785 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
---|
786 | return EPERM;
|
---|
787 |
|
---|
788 | ipc_irq_unregister(&TASK->answerbox, inr, devno);
|
---|
789 |
|
---|
790 | return 0;
|
---|
791 | }
|
---|
792 |
|
---|
793 | #ifdef __32_BITS__
|
---|
794 |
|
---|
795 | /** Syscall connect to a task by ID (32 bits)
|
---|
796 | *
|
---|
797 | * @return Phone id on success, or negative error code.
|
---|
798 | *
|
---|
799 | */
|
---|
800 | sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
|
---|
801 | {
|
---|
802 | #ifdef CONFIG_UDEBUG
|
---|
803 | sysarg64_t taskid;
|
---|
804 | int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
|
---|
805 | if (rc != 0)
|
---|
806 | return (sysarg_t) rc;
|
---|
807 |
|
---|
808 | return ipc_connect_kbox((task_id_t) taskid);
|
---|
809 | #else
|
---|
810 | return (sysarg_t) ENOTSUP;
|
---|
811 | #endif
|
---|
812 | }
|
---|
813 |
|
---|
814 | #endif /* __32_BITS__ */
|
---|
815 |
|
---|
816 | #ifdef __64_BITS__
|
---|
817 |
|
---|
818 | /** Syscall connect to a task by ID (64 bits)
|
---|
819 | *
|
---|
820 | * @return Phone id on success, or negative error code.
|
---|
821 | *
|
---|
822 | */
|
---|
823 | sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
|
---|
824 | {
|
---|
825 | #ifdef CONFIG_UDEBUG
|
---|
826 | return ipc_connect_kbox((task_id_t) taskid);
|
---|
827 | #else
|
---|
828 | return (sysarg_t) ENOTSUP;
|
---|
829 | #endif
|
---|
830 | }
|
---|
831 |
|
---|
832 | #endif /* __64_BITS__ */
|
---|
833 |
|
---|
834 | /** @}
|
---|
835 | */
|
---|