Turn ipc_poke() into a regular wakeup on the waitq.
[helenos.git] / kernel / generic / src / ipc / sysipc.c
bloba57b7765953ca2e15c60dd3d89b2c0574674997d
1 /*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup genericipc
30 * @{
32 /** @file
35 #include <arch.h>
36 #include <assert.h>
37 #include <errno.h>
38 #include <mem.h>
39 #include <ipc/ipc.h>
40 #include <abi/ipc/methods.h>
41 #include <ipc/sysipc.h>
42 #include <ipc/sysipc_ops.h>
43 #include <ipc/sysipc_priv.h>
44 #include <ipc/irq.h>
45 #include <ipc/ipcrsc.h>
46 #include <ipc/event.h>
47 #include <ipc/kbox.h>
48 #include <synch/waitq.h>
49 #include <arch/interrupt.h>
50 #include <syscall/copy.h>
51 #include <security/perm.h>
52 #include <console/console.h>
53 #include <print.h>
54 #include <macros.h>
55 #include <cap/cap.h>
57 #define STRUCT_TO_USPACE(dst, src) copy_to_uspace((dst), (src), sizeof(*(src)))
59 /** Decide if the interface and method is a system method.
61 * @param imethod Interface and method to be decided.
63 * @return True if the interface and method is a system
64 * interface and method.
67 static inline bool method_is_system(sysarg_t imethod)
69 if (imethod <= IPC_M_LAST_SYSTEM)
70 return true;
72 return false;
75 /** Decide if the message with this interface and method is forwardable.
77 * Some system messages may be forwarded, for some of them
78 * it is useless.
80 * @param imethod Interface and method to be decided.
82 * @return True if the interface and method is forwardable.
85 static inline bool method_is_forwardable(sysarg_t imethod)
87 switch (imethod) {
88 case IPC_M_PHONE_HUNGUP:
89 /* This message is meant only for the original recipient. */
90 return false;
91 default:
92 return true;
96 /** Decide if the message with this interface and method is immutable on forward.
98 * Some system messages may be forwarded but their content cannot be altered.
100 * @param imethod Interface and method to be decided.
102 * @return True if the interface and method is immutable on forward.
105 static inline bool method_is_immutable(sysarg_t imethod)
107 switch (imethod) {
108 case IPC_M_PAGE_IN:
109 case IPC_M_SHARE_OUT:
110 case IPC_M_SHARE_IN:
111 case IPC_M_DATA_WRITE:
112 case IPC_M_DATA_READ:
113 case IPC_M_STATE_CHANGE_AUTHORIZE:
114 return true;
115 default:
116 return false;
122 * Functions that preprocess answer before sending it to the recepient.
125 /** Decide if the caller (e.g. ipc_answer()) should save the old call contents
126 * for answer_preprocess().
128 * @param call Call structure to be decided.
130 * @return true if the old call contents should be saved.
133 static inline bool answer_need_old(call_t *call)
135 switch (IPC_GET_IMETHOD(call->data)) {
136 case IPC_M_CONNECT_TO_ME:
137 case IPC_M_CONNECT_ME_TO:
138 case IPC_M_PAGE_IN:
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;
150 /** Interpret process answer as control information.
152 * This function is called directly after sys_ipc_answer().
154 * @param answer Call structure with the answer.
155 * @param olddata Saved data of the request.
157 * @return Return EOK on success or an error code.
160 errno_t answer_preprocess(call_t *answer, ipc_data_t *olddata)
162 errno_t rc = EOK;
164 spinlock_lock(&answer->forget_lock);
165 if (answer->forget) {
167 * This is a forgotten call and answer->sender is not valid.
169 spinlock_unlock(&answer->forget_lock);
171 SYSIPC_OP(answer_cleanup, answer, olddata);
172 return rc;
173 } else {
174 assert(answer->active);
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.
181 answer->active = false;
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.
190 spinlock_lock(&answer->sender->active_calls_lock);
191 list_remove(&answer->ta_link);
192 spinlock_unlock(&answer->sender->active_calls_lock);
194 spinlock_unlock(&answer->forget_lock);
196 if ((errno_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 /* Drop callee->connected_phones reference */
203 kobject_put(phone->kobject);
204 phone->state = IPC_PHONE_SLAMMED;
205 irq_spinlock_unlock(&phone->callee->lock, true);
207 mutex_unlock(&phone->lock);
210 if (!olddata)
211 return rc;
213 return SYSIPC_OP(answer_preprocess, answer, olddata);
216 /** Called before the request is sent.
218 * @param call Call structure with the request.
219 * @param phone Phone that the call will be sent through.
221 * @return Return 0 on success, ELIMIT or EPERM on error.
224 static errno_t request_preprocess(call_t *call, phone_t *phone)
226 call->request_method = IPC_GET_IMETHOD(call->data);
227 return SYSIPC_OP(request_preprocess, call, phone);
231 * Functions called to process received call/answer before passing it to uspace.
234 /** Do basic kernel processing of received call answer.
236 * @param call Call structure with the answer.
239 static void process_answer(call_t *call)
241 if (((errno_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
242 (call->flags & IPC_CALL_FORWARDED))
243 IPC_SET_RETVAL(call->data, EFORWARD);
245 SYSIPC_OP(answer_process, call);
249 /** Do basic kernel processing of received call request.
251 * @param box Destination answerbox structure.
252 * @param call Call structure with the request.
254 * @return 0 if the call should be passed to userspace.
255 * @return -1 if the call should be ignored.
258 static int process_request(answerbox_t *box, call_t *call)
260 return SYSIPC_OP(request_process, call, box);
263 /** Make a call over IPC and wait for reply.
265 * @param handle Phone capability handle for the call.
266 * @param data[inout] Structure with request/reply data.
267 * @param priv Value to be stored in call->priv.
269 * @return EOK on success.
270 * @return ENOENT if there is no such phone handle.
273 errno_t
274 ipc_req_internal(cap_phone_handle_t handle, ipc_data_t *data, sysarg_t priv)
276 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
277 if (!kobj->phone)
278 return ENOENT;
280 call_t *call = ipc_call_alloc(0);
281 call->priv = priv;
282 memcpy(call->data.args, data->args, sizeof(data->args));
284 errno_t rc = request_preprocess(call, kobj->phone);
285 if (!rc) {
286 #ifdef CONFIG_UDEBUG
287 udebug_stoppable_begin();
288 #endif
290 kobject_add_ref(call->kobject);
291 rc = ipc_call_sync(kobj->phone, call);
292 spinlock_lock(&call->forget_lock);
293 bool forgotten = call->forget;
294 spinlock_unlock(&call->forget_lock);
295 kobject_put(call->kobject);
297 #ifdef CONFIG_UDEBUG
298 udebug_stoppable_end();
299 #endif
301 if (rc != EOK) {
302 if (!forgotten) {
304 * There was an error, but it did not result
305 * in the call being forgotten. In fact, the
306 * call was not even sent. We are still
307 * its owners and are responsible for its
308 * deallocation.
310 kobject_put(call->kobject);
311 } else {
313 * The call was forgotten and it changed hands.
314 * We are no longer expected to free it.
316 assert(rc == EINTR);
318 kobject_put(kobj);
319 return rc;
322 process_answer(call);
323 } else
324 IPC_SET_RETVAL(call->data, rc);
326 memcpy(data->args, call->data.args, sizeof(data->args));
327 kobject_put(call->kobject);
328 kobject_put(kobj);
330 return EOK;
333 /** Check that the task did not exceed the allowed limit of asynchronous calls
334 * made over a phone.
336 * @param phone Phone to check the limit against.
338 * @return 0 if limit not reached or -1 if limit exceeded.
341 static int check_call_limit(phone_t *phone)
343 if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
344 return -1;
346 return 0;
349 /** Make a fast asynchronous call over IPC.
351 * This function can only handle three arguments of payload, but is faster than
352 * the generic function sys_ipc_call_async_slow().
354 * @param handle Phone capability handle for the call.
355 * @param imethod Interface and method of the call.
356 * @param arg1 Service-defined payload argument.
357 * @param arg2 Service-defined payload argument.
358 * @param arg3 Service-defined payload argument.
359 * @param label User-defined label.
361 * @return EOK on success.
362 * @return An error code on error.
365 sys_errno_t sys_ipc_call_async_fast(cap_phone_handle_t handle, sysarg_t imethod,
366 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t label)
368 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
369 if (!kobj)
370 return ENOENT;
372 if (check_call_limit(kobj->phone)) {
373 kobject_put(kobj);
374 return ELIMIT;
377 call_t *call = ipc_call_alloc(0);
378 IPC_SET_IMETHOD(call->data, imethod);
379 IPC_SET_ARG1(call->data, arg1);
380 IPC_SET_ARG2(call->data, arg2);
381 IPC_SET_ARG3(call->data, arg3);
384 * To achieve deterministic behavior, zero out arguments that are beyond
385 * the limits of the fast version.
387 IPC_SET_ARG5(call->data, 0);
389 /* Set the user-defined label */
390 call->data.label = label;
392 errno_t res = request_preprocess(call, kobj->phone);
394 if (!res)
395 ipc_call(kobj->phone, call);
396 else
397 ipc_backsend_err(kobj->phone, call, res);
399 kobject_put(kobj);
400 return EOK;
403 /** Make an asynchronous IPC call allowing to transmit the entire payload.
405 * @param handle Phone capability for the call.
406 * @param data Userspace address of call data with the request.
407 * @param label User-defined label.
409 * @return See sys_ipc_call_async_fast().
412 sys_errno_t sys_ipc_call_async_slow(cap_phone_handle_t handle, ipc_data_t *data,
413 sysarg_t label)
415 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
416 if (!kobj)
417 return ENOENT;
419 if (check_call_limit(kobj->phone)) {
420 kobject_put(kobj);
421 return ELIMIT;
424 call_t *call = ipc_call_alloc(0);
425 errno_t rc = copy_from_uspace(&call->data.args, &data->args,
426 sizeof(call->data.args));
427 if (rc != EOK) {
428 kobject_put(call->kobject);
429 kobject_put(kobj);
430 return (sys_errno_t) rc;
433 /* Set the user-defined label */
434 call->data.label = label;
436 errno_t res = request_preprocess(call, kobj->phone);
438 if (!res)
439 ipc_call(kobj->phone, call);
440 else
441 ipc_backsend_err(kobj->phone, call, res);
443 kobject_put(kobj);
444 return EOK;
447 /** Forward a received call to another destination
449 * Common code for both the fast and the slow version.
451 * @param chandle Call handle of the forwarded call.
452 * @param phandle Phone handle to use for forwarding.
453 * @param imethod New interface and method to use for the forwarded call.
454 * @param arg1 New value of the first argument for the forwarded call.
455 * @param arg2 New value of the second argument for the forwarded call.
456 * @param arg3 New value of the third argument for the forwarded call.
457 * @param arg4 New value of the fourth argument for the forwarded call.
458 * @param arg5 New value of the fifth argument for the forwarded call.
459 * @param mode Flags that specify mode of the forward operation.
460 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
461 * the function considers only the fast version arguments:
462 * i.e. arg1 and arg2.
464 * @return 0 on succes, otherwise an error code.
466 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
469 static sys_errno_t sys_ipc_forward_common(cap_call_handle_t chandle,
470 cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
471 sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
473 kobject_t *ckobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
474 if (!ckobj)
475 return ENOENT;
477 call_t *call = ckobj->call;
479 ipc_data_t old;
480 bool need_old = answer_need_old(call);
481 if (need_old)
482 old = call->data;
484 bool after_forward = false;
485 errno_t rc;
487 kobject_t *pkobj = kobject_get(TASK, phandle, KOBJECT_TYPE_PHONE);
488 if (!pkobj) {
489 rc = ENOENT;
490 goto error;
493 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
494 rc = EPERM;
495 goto error;
498 call->flags |= IPC_CALL_FORWARDED;
501 * User space is not allowed to change interface and method of system
502 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
503 * means of imethod, arg1, arg2 and arg3.
504 * If the interface and method is immutable, don't change anything.
506 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
507 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
508 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
509 phone_dealloc((cap_phone_handle_t)
510 IPC_GET_ARG5(call->data));
512 IPC_SET_ARG1(call->data, imethod);
513 IPC_SET_ARG2(call->data, arg1);
514 IPC_SET_ARG3(call->data, arg2);
516 if (slow)
517 IPC_SET_ARG4(call->data, arg3);
520 * For system methods we deliberately don't
521 * overwrite ARG5.
523 } else {
524 IPC_SET_IMETHOD(call->data, imethod);
525 IPC_SET_ARG1(call->data, arg1);
526 IPC_SET_ARG2(call->data, arg2);
527 if (slow) {
528 IPC_SET_ARG3(call->data, arg3);
529 IPC_SET_ARG4(call->data, arg4);
530 IPC_SET_ARG5(call->data, arg5);
535 rc = ipc_forward(call, pkobj->phone, &TASK->answerbox, mode);
536 if (rc != EOK) {
537 after_forward = true;
538 goto error;
541 cap_free(TASK, chandle);
542 kobject_put(ckobj);
543 kobject_put(pkobj);
544 return EOK;
546 error:
547 IPC_SET_RETVAL(call->data, EFORWARD);
548 (void) answer_preprocess(call, need_old ? &old : NULL);
549 if (after_forward)
550 _ipc_answer_free_call(call, false);
551 else
552 ipc_answer(&TASK->answerbox, call);
554 cap_free(TASK, chandle);
555 kobject_put(ckobj);
557 if (pkobj)
558 kobject_put(pkobj);
559 return rc;
562 /** Forward a received call to another destination - fast version.
564 * In case the original interface and method is a system method, ARG1, ARG2
565 * and ARG3 are overwritten in the forwarded message with the new method and
566 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
567 * are rewritten with the new interface and method, arg1 and arg2, respectively.
568 * Also note there is a set of immutable methods, for which the new method and
569 * arguments are not set and these values are ignored.
571 * @param chandle Call handle of the call to forward.
572 * @param phandle Phone handle to use for forwarding.
573 * @param imethod New interface and method to use for the forwarded call.
574 * @param arg1 New value of the first argument for the forwarded call.
575 * @param arg2 New value of the second argument for the forwarded call.
576 * @param mode Flags that specify mode of the forward operation.
578 * @return 0 on succes, otherwise an error code.
581 sys_errno_t sys_ipc_forward_fast(cap_call_handle_t chandle,
582 cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
583 unsigned int mode)
585 return sys_ipc_forward_common(chandle, phandle, imethod, arg1, arg2, 0,
586 0, 0, mode, false);
589 /** Forward a received call to another destination - slow version.
591 * This function is the slow verision of the sys_ipc_forward_fast interface.
592 * It can copy all five new arguments and the new interface and method from
593 * the userspace. It naturally extends the functionality of the fast version.
594 * For system methods, it additionally stores the new value of arg3 to ARG4.
595 * For non-system methods, it additionally stores the new value of arg3, arg4
596 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
598 * @param chandle Call handle of the call to forward.
599 * @param phandle Phone handle to use for forwarding.
600 * @param data Userspace address of the new IPC data.
601 * @param mode Flags that specify mode of the forward operation.
603 * @return 0 on succes, otherwise an error code.
606 sys_errno_t sys_ipc_forward_slow(cap_call_handle_t chandle,
607 cap_phone_handle_t phandle, ipc_data_t *data, unsigned int mode)
609 ipc_data_t newdata;
610 errno_t rc = copy_from_uspace(&newdata.args, &data->args,
611 sizeof(newdata.args));
612 if (rc != EOK)
613 return (sys_errno_t) rc;
615 return sys_ipc_forward_common(chandle, phandle,
616 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
617 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
618 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
621 /** Answer an IPC call - fast version.
623 * This function can handle only two return arguments of payload, but is faster
624 * than the generic sys_ipc_answer().
626 * @param chandle Call handle to be answered.
627 * @param retval Return value of the answer.
628 * @param arg1 Service-defined return value.
629 * @param arg2 Service-defined return value.
630 * @param arg3 Service-defined return value.
631 * @param arg4 Service-defined return value.
633 * @return 0 on success, otherwise an error code.
636 sys_errno_t sys_ipc_answer_fast(cap_call_handle_t chandle, sysarg_t retval,
637 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
639 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
640 if (!kobj)
641 return ENOENT;
643 call_t *call = kobj->call;
644 assert(!(call->flags & IPC_CALL_ANSWERED));
646 ipc_data_t saved_data;
647 bool saved;
649 if (answer_need_old(call)) {
650 memcpy(&saved_data, &call->data, sizeof(call->data));
651 saved = true;
652 } else
653 saved = false;
655 IPC_SET_RETVAL(call->data, retval);
656 IPC_SET_ARG1(call->data, arg1);
657 IPC_SET_ARG2(call->data, arg2);
658 IPC_SET_ARG3(call->data, arg3);
659 IPC_SET_ARG4(call->data, arg4);
662 * To achieve deterministic behavior, zero out arguments that are beyond
663 * the limits of the fast version.
665 IPC_SET_ARG5(call->data, 0);
666 errno_t rc = answer_preprocess(call, saved ? &saved_data : NULL);
668 ipc_answer(&TASK->answerbox, call);
670 kobject_put(kobj);
671 cap_free(TASK, chandle);
673 return rc;
676 /** Answer an IPC call.
678 * @param chandle Call handle to be answered.
679 * @param data Userspace address of call data with the answer.
681 * @return 0 on success, otherwise an error code.
684 sys_errno_t sys_ipc_answer_slow(cap_call_handle_t chandle, ipc_data_t *data)
686 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
687 if (!kobj)
688 return ENOENT;
690 call_t *call = kobj->call;
691 assert(!(call->flags & IPC_CALL_ANSWERED));
693 ipc_data_t saved_data;
694 bool saved;
696 if (answer_need_old(call)) {
697 memcpy(&saved_data, &call->data, sizeof(call->data));
698 saved = true;
699 } else
700 saved = false;
702 errno_t rc = copy_from_uspace(&call->data.args, &data->args,
703 sizeof(call->data.args));
704 if (rc != EOK) {
706 * Republish the capability so that the call does not get lost.
708 cap_publish(TASK, chandle, kobj);
709 return rc;
712 rc = answer_preprocess(call, saved ? &saved_data : NULL);
714 ipc_answer(&TASK->answerbox, call);
716 kobject_put(kobj);
717 cap_free(TASK, chandle);
719 return rc;
722 /** Hang up a phone.
724 * @param handle Phone capability handle of the phone to be hung up.
726 * @return 0 on success or an error code.
729 sys_errno_t sys_ipc_hangup(cap_phone_handle_t handle)
731 kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
732 if (!kobj)
733 return ENOENT;
735 errno_t rc = ipc_phone_hangup(kobj->phone);
736 kobject_put(kobj);
737 cap_free(TASK, handle);
738 return rc;
741 /** Wait for an incoming IPC call or an answer.
743 * @param calldata Pointer to buffer where the call/answer data is stored.
744 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
745 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
746 * for explanation.
748 * @return An error code on error.
750 sys_errno_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
751 unsigned int flags)
753 call_t *call;
755 restart:
757 #ifdef CONFIG_UDEBUG
758 udebug_stoppable_begin();
759 #endif
761 call = ipc_wait_for_call(&TASK->answerbox, usec,
762 flags | SYNCH_FLAGS_INTERRUPTIBLE);
764 #ifdef CONFIG_UDEBUG
765 udebug_stoppable_end();
766 #endif
768 if (!call) {
769 ipc_data_t data = { };
770 data.cap_handle = CAP_NIL;
771 STRUCT_TO_USPACE(calldata, &data);
772 return EOK;
775 call->data.flags = call->flags;
776 if (call->flags & IPC_CALL_NOTIF) {
777 /* Set in_phone_hash to the interrupt counter */
778 call->data.phone = (void *) call->priv;
780 call->data.cap_handle = CAP_NIL;
782 STRUCT_TO_USPACE(calldata, &call->data);
783 kobject_put(call->kobject);
785 return EOK;
788 if (call->flags & IPC_CALL_ANSWERED) {
789 process_answer(call);
791 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
792 kobject_put(call->kobject);
793 goto restart;
796 call->data.cap_handle = CAP_NIL;
798 STRUCT_TO_USPACE(calldata, &call->data);
799 kobject_put(call->kobject);
801 return EOK;
804 if (process_request(&TASK->answerbox, call))
805 goto restart;
807 cap_handle_t handle = CAP_NIL;
808 errno_t rc = cap_alloc(TASK, &handle);
809 if (rc != EOK) {
810 goto error;
813 call->data.cap_handle = handle;
816 * Include phone hash of the caller in the request, copy the whole
817 * call->data, not only call->data.args.
819 rc = STRUCT_TO_USPACE(calldata, &call->data);
820 if (rc != EOK)
821 goto error;
823 kobject_add_ref(call->kobject);
824 cap_publish(TASK, handle, call->kobject);
825 return EOK;
827 error:
828 if (CAP_HANDLE_VALID(handle))
829 cap_free(TASK, handle);
832 * The callee will not receive this call and no one else has a chance to
833 * answer it. Set the IPC_CALL_AUTO_REPLY flag and return the EPARTY
834 * error code.
836 ipc_data_t saved_data;
837 bool saved;
839 if (answer_need_old(call)) {
840 memcpy(&saved_data, &call->data, sizeof(call->data));
841 saved = true;
842 } else
843 saved = false;
845 IPC_SET_RETVAL(call->data, EPARTY);
846 (void) answer_preprocess(call, saved ? &saved_data : NULL);
847 call->flags |= IPC_CALL_AUTO_REPLY;
848 ipc_answer(&TASK->answerbox, call);
850 return rc;
853 /** Interrupt one thread from sys_ipc_wait_for_call().
856 sys_errno_t sys_ipc_poke(void)
858 waitq_wakeup(&TASK->answerbox.wq, WAKEUP_FIRST);
859 return EOK;
862 /** Connect an IRQ handler to a task.
864 * @param inr IRQ number.
865 * @param imethod Interface and method to be associated with the notification.
866 * @param ucode Uspace pointer to the top-half pseudocode.
868 * @param[out] uspace_handle Uspace pointer to IRQ capability handle
870 * @return EPERM
871 * @return Error code returned by ipc_irq_subscribe().
874 sys_errno_t sys_ipc_irq_subscribe(inr_t inr, sysarg_t imethod,
875 irq_code_t *ucode, cap_irq_handle_t *uspace_handle)
877 if (!(perm_get(TASK) & PERM_IRQ_REG))
878 return EPERM;
880 return ipc_irq_subscribe(&TASK->answerbox, inr, imethod, ucode, uspace_handle);
883 /** Disconnect an IRQ handler from a task.
885 * @param handle IRQ capability handle.
887 * @return Zero on success or EPERM on error.
890 sys_errno_t sys_ipc_irq_unsubscribe(cap_irq_handle_t handle)
892 if (!(perm_get(TASK) & PERM_IRQ_REG))
893 return EPERM;
895 ipc_irq_unsubscribe(&TASK->answerbox, handle);
897 return 0;
900 /** Syscall connect to a task by ID
902 * @return Error code.
905 sys_errno_t sys_ipc_connect_kbox(task_id_t *uspace_taskid,
906 cap_phone_handle_t *uspace_phone)
908 #ifdef CONFIG_UDEBUG
909 task_id_t taskid;
910 cap_phone_handle_t phone;
912 errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(task_id_t));
913 if (rc == EOK) {
914 rc = ipc_connect_kbox((task_id_t) taskid, &phone);
917 if (rc == EOK) {
918 rc = copy_to_uspace(uspace_phone, &phone, sizeof(cap_handle_t));
919 if (rc != EOK) {
920 // Clean up the phone on failure.
921 sys_ipc_hangup(phone);
925 return (sys_errno_t) rc;
926 #else
927 return (sys_errno_t) ENOTSUP;
928 #endif
931 /** @}