source: mainline/kernel/generic/src/ipc/sysipc.c@ c32b6f0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c32b6f0 was bc117a5, checked in by Jakub Jermar <jakub@…>, 9 years ago

Partially revert jakub@…

This changeset reintroduces portions of the synchronous IPC mechanism for
internal use by the kernel / user_backend. The synchronous IPC is not
exported to user space and is simpler than in its original form.

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