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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2e4343b was bab75df6, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Let kernel code get printf via the standard stdio header. Clean up unused includes.

  • Property mode set to 100644
File size: 24.2 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 kernel_generic_ipc
30 * @{
31 */
32/** @file
33 */
34
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 <macros.h>
54#include <cap/cap.h>
55
56#define STRUCT_TO_USPACE(dst, src) copy_to_uspace((dst), (src), sizeof(*(src)))
57
58/** Decide if the interface and method is a system method.
59 *
60 * @param imethod Interface and method to be decided.
61 *
62 * @return True if the interface and method is a system
63 * interface and method.
64 *
65 */
66static inline bool method_is_system(sysarg_t imethod)
67{
68 if (imethod <= IPC_M_LAST_SYSTEM)
69 return true;
70
71 return false;
72}
73
74/** Decide if the message with this interface and method is forwardable.
75 *
76 * Some system messages may be forwarded, for some of them
77 * it is useless.
78 *
79 * @param imethod Interface and method to be decided.
80 *
81 * @return True if the interface and method is forwardable.
82 *
83 */
84static inline bool method_is_forwardable(sysarg_t imethod)
85{
86 switch (imethod) {
87 case IPC_M_PHONE_HUNGUP:
88 /* This message is meant only for the original recipient. */
89 return false;
90 default:
91 return true;
92 }
93}
94
95/** Decide if the message with this interface and method is immutable on forward.
96 *
97 * Some system messages may be forwarded but their content cannot be altered.
98 *
99 * @param imethod Interface and method to be decided.
100 *
101 * @return True if the interface and method is immutable on forward.
102 *
103 */
104static inline bool method_is_immutable(sysarg_t imethod)
105{
106 switch (imethod) {
107 case IPC_M_PAGE_IN:
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 * Functions that preprocess answer before sending it to the recepient.
121 */
122
123/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
124 * for answer_preprocess().
125 *
126 * @param call Call structure to be decided.
127 *
128 * @return true if the old call contents should be saved.
129 *
130 */
131static inline bool answer_need_old(call_t *call)
132{
133 switch (IPC_GET_IMETHOD(call->data)) {
134 case IPC_M_CONNECT_TO_ME:
135 case IPC_M_CONNECT_ME_TO:
136 case IPC_M_PAGE_IN:
137 case IPC_M_SHARE_OUT:
138 case IPC_M_SHARE_IN:
139 case IPC_M_DATA_WRITE:
140 case IPC_M_DATA_READ:
141 case IPC_M_STATE_CHANGE_AUTHORIZE:
142 return true;
143 default:
144 return false;
145 }
146}
147
148/** Interpret process answer as control information.
149 *
150 * This function is called directly after sys_ipc_answer().
151 *
152 * @param answer Call structure with the answer.
153 * @param olddata Saved data of the request.
154 *
155 * @return Return EOK on success or an error code.
156 *
157 */
158errno_t answer_preprocess(call_t *answer, ipc_data_t *olddata)
159{
160 errno_t rc = EOK;
161
162 spinlock_lock(&answer->forget_lock);
163 if (answer->forget) {
164 /*
165 * This is a forgotten call and answer->sender is not valid.
166 */
167 spinlock_unlock(&answer->forget_lock);
168
169 SYSIPC_OP(answer_cleanup, answer, olddata);
170 return rc;
171 } else {
172 assert(answer->active);
173
174 /*
175 * Mark the call as inactive to prevent _ipc_answer_free_call()
176 * from attempting to remove the call from the active list
177 * itself.
178 */
179 answer->active = false;
180
181 /*
182 * Remove the call from the sender's active call list.
183 * We enforce this locking order so that any potential
184 * concurrently executing forget operation is forced to
185 * release its active_calls_lock and lose the race to
186 * forget this soon to be answered call.
187 */
188 spinlock_lock(&answer->sender->active_calls_lock);
189 list_remove(&answer->ta_link);
190 spinlock_unlock(&answer->sender->active_calls_lock);
191 }
192 spinlock_unlock(&answer->forget_lock);
193
194 if ((errno_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
195 phone_t *phone = answer->caller_phone;
196 mutex_lock(&phone->lock);
197 if (phone->state == IPC_PHONE_CONNECTED) {
198 irq_spinlock_lock(&phone->callee->lock, true);
199 list_remove(&phone->link);
200 /* Drop callee->connected_phones reference */
201 kobject_put(phone->kobject);
202 phone->state = IPC_PHONE_SLAMMED;
203 phone->label = 0;
204 irq_spinlock_unlock(&phone->callee->lock, true);
205 }
206 mutex_unlock(&phone->lock);
207 }
208
209 if (!olddata)
210 return rc;
211
212 return SYSIPC_OP(answer_preprocess, answer, olddata);
213}
214
215/** Called before the request is sent.
216 *
217 * @param call Call structure with the request.
218 * @param phone Phone that the call will be sent through.
219 *
220 * @return Return 0 on success, ELIMIT or EPERM on error.
221 *
222 */
223static errno_t request_preprocess(call_t *call, phone_t *phone)
224{
225 call->request_method = IPC_GET_IMETHOD(call->data);
226 return SYSIPC_OP(request_preprocess, call, phone);
227}
228
229/*
230 * Functions called to process received call/answer before passing it to uspace.
231 */
232
233/** Do basic kernel processing of received call answer.
234 *
235 * @param call Call structure with the answer.
236 *
237 */
238static void process_answer(call_t *call)
239{
240 if (((errno_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
241 (call->flags & IPC_CALL_FORWARDED))
242 IPC_SET_RETVAL(call->data, EFORWARD);
243
244 SYSIPC_OP(answer_process, call);
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 handle Phone capability handle for the call.
264 * @param data[inout] Structure with request/reply data.
265 * @param priv Value to be stored in call->priv.
266 *
267 * @return EOK on success.
268 * @return ENOENT if there is no such phone handle.
269 *
270 */
271errno_t
272ipc_req_internal(cap_phone_handle_t handle, ipc_data_t *data, sysarg_t priv)
273{
274 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
275 if (!kobj->phone)
276 return ENOENT;
277
278 call_t *call = ipc_call_alloc(0);
279 call->priv = priv;
280 memcpy(call->data.args, data->args, sizeof(data->args));
281
282 errno_t rc = request_preprocess(call, kobj->phone);
283 if (!rc) {
284#ifdef CONFIG_UDEBUG
285 udebug_stoppable_begin();
286#endif
287
288 kobject_add_ref(call->kobject);
289 rc = ipc_call_sync(kobj->phone, call);
290 spinlock_lock(&call->forget_lock);
291 bool forgotten = call->forget;
292 spinlock_unlock(&call->forget_lock);
293 kobject_put(call->kobject);
294
295#ifdef CONFIG_UDEBUG
296 udebug_stoppable_end();
297#endif
298
299 if (rc != EOK) {
300 if (!forgotten) {
301 /*
302 * There was an error, but it did not result
303 * in the call being forgotten. In fact, the
304 * call was not even sent. We are still
305 * its owners and are responsible for its
306 * deallocation.
307 */
308 kobject_put(call->kobject);
309 } else {
310 /*
311 * The call was forgotten and it changed hands.
312 * We are no longer expected to free it.
313 */
314 assert(rc == EINTR);
315 }
316 kobject_put(kobj);
317 return rc;
318 }
319
320 process_answer(call);
321 } else
322 IPC_SET_RETVAL(call->data, rc);
323
324 memcpy(data->args, call->data.args, sizeof(data->args));
325 kobject_put(call->kobject);
326 kobject_put(kobj);
327
328 return EOK;
329}
330
331/** Check that the task did not exceed the allowed limit of asynchronous calls
332 * made over a phone.
333 *
334 * @param phone Phone to check the limit against.
335 *
336 * @return 0 if limit not reached or -1 if limit exceeded.
337 *
338 */
339static int check_call_limit(phone_t *phone)
340{
341 if (atomic_load(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
342 return -1;
343
344 return 0;
345}
346
347/** Make a fast asynchronous call over IPC.
348 *
349 * This function can only handle three arguments of payload, but is faster than
350 * the generic function sys_ipc_call_async_slow().
351 *
352 * @param handle Phone capability handle for the call.
353 * @param imethod Interface and method of the call.
354 * @param arg1 Service-defined payload argument.
355 * @param arg2 Service-defined payload argument.
356 * @param arg3 Service-defined payload argument.
357 * @param label User-defined label.
358 *
359 * @return EOK on success.
360 * @return An error code on error.
361 *
362 */
363sys_errno_t sys_ipc_call_async_fast(cap_phone_handle_t handle, sysarg_t imethod,
364 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t label)
365{
366 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
367 if (!kobj)
368 return ENOENT;
369
370 if (check_call_limit(kobj->phone)) {
371 kobject_put(kobj);
372 return ELIMIT;
373 }
374
375 call_t *call = ipc_call_alloc(0);
376 IPC_SET_IMETHOD(call->data, imethod);
377 IPC_SET_ARG1(call->data, arg1);
378 IPC_SET_ARG2(call->data, arg2);
379 IPC_SET_ARG3(call->data, arg3);
380
381 /*
382 * To achieve deterministic behavior, zero out arguments that are beyond
383 * the limits of the fast version.
384 */
385 IPC_SET_ARG5(call->data, 0);
386
387 /* Set the user-defined label */
388 call->data.answer_label = label;
389
390 errno_t res = request_preprocess(call, kobj->phone);
391
392 if (!res)
393 ipc_call(kobj->phone, call);
394 else
395 ipc_backsend_err(kobj->phone, call, res);
396
397 kobject_put(kobj);
398 return EOK;
399}
400
401/** Make an asynchronous IPC call allowing to transmit the entire payload.
402 *
403 * @param handle Phone capability for the call.
404 * @param data Userspace address of call data with the request.
405 * @param label User-defined label.
406 *
407 * @return See sys_ipc_call_async_fast().
408 *
409 */
410sys_errno_t sys_ipc_call_async_slow(cap_phone_handle_t handle, ipc_data_t *data,
411 sysarg_t label)
412{
413 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
414 if (!kobj)
415 return ENOENT;
416
417 if (check_call_limit(kobj->phone)) {
418 kobject_put(kobj);
419 return ELIMIT;
420 }
421
422 call_t *call = ipc_call_alloc(0);
423 errno_t rc = copy_from_uspace(&call->data.args, &data->args,
424 sizeof(call->data.args));
425 if (rc != EOK) {
426 kobject_put(call->kobject);
427 kobject_put(kobj);
428 return (sys_errno_t) rc;
429 }
430
431 /* Set the user-defined label */
432 call->data.answer_label = label;
433
434 errno_t res = request_preprocess(call, kobj->phone);
435
436 if (!res)
437 ipc_call(kobj->phone, call);
438 else
439 ipc_backsend_err(kobj->phone, call, res);
440
441 kobject_put(kobj);
442 return EOK;
443}
444
445/** Forward a received call to another destination
446 *
447 * Common code for both the fast and the slow version.
448 *
449 * @param chandle Call handle of the forwarded call.
450 * @param phandle Phone handle to use for forwarding.
451 * @param imethod New interface and method to use for the forwarded call.
452 * @param arg1 New value of the first argument for the forwarded call.
453 * @param arg2 New value of the second argument for the forwarded call.
454 * @param arg3 New value of the third argument for the forwarded call.
455 * @param arg4 New value of the fourth argument for the forwarded call.
456 * @param arg5 New value of the fifth argument for the forwarded call.
457 * @param mode Flags that specify mode of the forward operation.
458 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
459 * the function considers only the fast version arguments:
460 * i.e. arg1 and arg2.
461 *
462 * @return 0 on succes, otherwise an error code.
463 *
464 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
465 *
466 */
467static sys_errno_t sys_ipc_forward_common(cap_call_handle_t chandle,
468 cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
469 sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
470{
471 kobject_t *ckobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
472 if (!ckobj)
473 return ENOENT;
474
475 call_t *call = ckobj->call;
476
477 ipc_data_t old;
478 bool need_old = answer_need_old(call);
479 if (need_old)
480 old = call->data;
481
482 bool after_forward = false;
483 errno_t rc;
484
485 kobject_t *pkobj = kobject_get(TASK, phandle, KOBJECT_TYPE_PHONE);
486 if (!pkobj) {
487 rc = ENOENT;
488 goto error;
489 }
490
491 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
492 rc = EPERM;
493 goto error;
494 }
495
496 call->flags |= IPC_CALL_FORWARDED;
497
498 /*
499 * User space is not allowed to change interface and method of system
500 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
501 * means of imethod, arg1, arg2 and arg3.
502 * If the interface and method is immutable, don't change anything.
503 */
504 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
505 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
506 if (IPC_GET_IMETHOD(call->data) ==
507 IPC_M_CONNECT_TO_ME) {
508 kobject_put((kobject_t *) call->priv);
509 call->priv = 0;
510 cap_free(TASK,
511 (cap_handle_t) IPC_GET_ARG5(call->data));
512 }
513
514 IPC_SET_ARG1(call->data, imethod);
515 IPC_SET_ARG2(call->data, arg1);
516 IPC_SET_ARG3(call->data, arg2);
517
518 if (slow)
519 IPC_SET_ARG4(call->data, arg3);
520
521 /*
522 * For system methods we deliberately don't
523 * overwrite ARG5.
524 */
525 } else {
526 IPC_SET_IMETHOD(call->data, imethod);
527 IPC_SET_ARG1(call->data, arg1);
528 IPC_SET_ARG2(call->data, arg2);
529 if (slow) {
530 IPC_SET_ARG3(call->data, arg3);
531 IPC_SET_ARG4(call->data, arg4);
532 IPC_SET_ARG5(call->data, arg5);
533 }
534 }
535 }
536
537 rc = ipc_forward(call, pkobj->phone, &TASK->answerbox, mode);
538 if (rc != EOK) {
539 after_forward = true;
540 goto error;
541 }
542
543 cap_free(TASK, chandle);
544 kobject_put(ckobj);
545 kobject_put(pkobj);
546 return EOK;
547
548error:
549 IPC_SET_RETVAL(call->data, EFORWARD);
550 (void) answer_preprocess(call, need_old ? &old : NULL);
551 if (after_forward)
552 _ipc_answer_free_call(call, false);
553 else
554 ipc_answer(&TASK->answerbox, call);
555
556 cap_free(TASK, chandle);
557 kobject_put(ckobj);
558
559 if (pkobj)
560 kobject_put(pkobj);
561 return rc;
562}
563
564/** Forward a received call to another destination - fast version.
565 *
566 * In case the original interface and method is a system method, ARG1, ARG2
567 * and ARG3 are overwritten in the forwarded message with the new method and
568 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
569 * are rewritten with the new interface and method, arg1 and arg2, respectively.
570 * Also note there is a set of immutable methods, for which the new method and
571 * arguments are not set and these values are ignored.
572 *
573 * @param chandle Call handle of the call to forward.
574 * @param phandle Phone handle to use for forwarding.
575 * @param imethod New interface and method to use for the forwarded call.
576 * @param arg1 New value of the first argument for the forwarded call.
577 * @param arg2 New value of the second argument for the forwarded call.
578 * @param mode Flags that specify mode of the forward operation.
579 *
580 * @return 0 on succes, otherwise an error code.
581 *
582 */
583sys_errno_t sys_ipc_forward_fast(cap_call_handle_t chandle,
584 cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
585 unsigned int mode)
586{
587 return sys_ipc_forward_common(chandle, phandle, imethod, arg1, arg2, 0,
588 0, 0, mode, false);
589}
590
591/** Forward a received call to another destination - slow version.
592 *
593 * This function is the slow verision of the sys_ipc_forward_fast interface.
594 * It can copy all five new arguments and the new interface and method from
595 * the userspace. It naturally extends the functionality of the fast version.
596 * For system methods, it additionally stores the new value of arg3 to ARG4.
597 * For non-system methods, it additionally stores the new value of arg3, arg4
598 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
599 *
600 * @param chandle Call handle of the call to forward.
601 * @param phandle Phone handle to use for forwarding.
602 * @param data Userspace address of the new IPC data.
603 * @param mode Flags that specify mode of the forward operation.
604 *
605 * @return 0 on succes, otherwise an error code.
606 *
607 */
608sys_errno_t sys_ipc_forward_slow(cap_call_handle_t chandle,
609 cap_phone_handle_t phandle, ipc_data_t *data, unsigned int mode)
610{
611 ipc_data_t newdata;
612 errno_t rc = copy_from_uspace(&newdata.args, &data->args,
613 sizeof(newdata.args));
614 if (rc != EOK)
615 return (sys_errno_t) rc;
616
617 return sys_ipc_forward_common(chandle, phandle,
618 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
619 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
620 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
621}
622
623/** Answer an IPC call - fast version.
624 *
625 * This function can handle only two return arguments of payload, but is faster
626 * than the generic sys_ipc_answer().
627 *
628 * @param chandle Call handle to be answered.
629 * @param retval Return value of the answer.
630 * @param arg1 Service-defined return value.
631 * @param arg2 Service-defined return value.
632 * @param arg3 Service-defined return value.
633 * @param arg4 Service-defined return value.
634 *
635 * @return 0 on success, otherwise an error code.
636 *
637 */
638sys_errno_t sys_ipc_answer_fast(cap_call_handle_t chandle, sysarg_t retval,
639 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
640{
641 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
642 if (!kobj)
643 return ENOENT;
644
645 call_t *call = kobj->call;
646 assert(!(call->flags & IPC_CALL_ANSWERED));
647
648 ipc_data_t saved_data;
649 bool saved;
650
651 if (answer_need_old(call)) {
652 memcpy(&saved_data, &call->data, sizeof(call->data));
653 saved = true;
654 } else
655 saved = false;
656
657 IPC_SET_RETVAL(call->data, retval);
658 IPC_SET_ARG1(call->data, arg1);
659 IPC_SET_ARG2(call->data, arg2);
660 IPC_SET_ARG3(call->data, arg3);
661 IPC_SET_ARG4(call->data, arg4);
662
663 /*
664 * To achieve deterministic behavior, zero out arguments that are beyond
665 * the limits of the fast version.
666 */
667 IPC_SET_ARG5(call->data, 0);
668 errno_t rc = answer_preprocess(call, saved ? &saved_data : NULL);
669
670 ipc_answer(&TASK->answerbox, call);
671
672 kobject_put(kobj);
673 cap_free(TASK, chandle);
674
675 return rc;
676}
677
678/** Answer an IPC call.
679 *
680 * @param chandle Call handle to be answered.
681 * @param data Userspace address of call data with the answer.
682 *
683 * @return 0 on success, otherwise an error code.
684 *
685 */
686sys_errno_t sys_ipc_answer_slow(cap_call_handle_t chandle, ipc_data_t *data)
687{
688 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
689 if (!kobj)
690 return ENOENT;
691
692 call_t *call = kobj->call;
693 assert(!(call->flags & IPC_CALL_ANSWERED));
694
695 ipc_data_t saved_data;
696 bool saved;
697
698 if (answer_need_old(call)) {
699 memcpy(&saved_data, &call->data, sizeof(call->data));
700 saved = true;
701 } else
702 saved = false;
703
704 errno_t rc = copy_from_uspace(&call->data.args, &data->args,
705 sizeof(call->data.args));
706 if (rc != EOK) {
707 /*
708 * Republish the capability so that the call does not get lost.
709 */
710 cap_publish(TASK, chandle, kobj);
711 return rc;
712 }
713
714 rc = answer_preprocess(call, saved ? &saved_data : NULL);
715
716 ipc_answer(&TASK->answerbox, call);
717
718 kobject_put(kobj);
719 cap_free(TASK, chandle);
720
721 return rc;
722}
723
724/** Hang up a phone.
725 *
726 * @param handle Phone capability handle of the phone to be hung up.
727 *
728 * @return 0 on success or an error code.
729 *
730 */
731sys_errno_t sys_ipc_hangup(cap_phone_handle_t handle)
732{
733 kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
734 if (!kobj)
735 return ENOENT;
736
737 errno_t rc = ipc_phone_hangup(kobj->phone);
738 kobject_put(kobj);
739 cap_free(TASK, handle);
740 return rc;
741}
742
743/** Wait for an incoming IPC call or an answer.
744 *
745 * @param calldata Pointer to buffer where the call/answer data is stored.
746 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
747 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
748 * for explanation.
749 *
750 * @return An error code on error.
751 */
752sys_errno_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
753 unsigned int flags)
754{
755 call_t *call = NULL;
756
757restart:
758
759#ifdef CONFIG_UDEBUG
760 udebug_stoppable_begin();
761#endif
762
763 errno_t rc = ipc_wait_for_call(&TASK->answerbox, usec,
764 flags | SYNCH_FLAGS_INTERRUPTIBLE, &call);
765
766#ifdef CONFIG_UDEBUG
767 udebug_stoppable_end();
768#endif
769
770 if (rc != EOK)
771 return rc;
772
773 assert(call);
774
775 call->data.flags = call->flags;
776 if (call->flags & IPC_CALL_NOTIF) {
777 /* Set the request_label to the interrupt counter */
778 call->data.request_label = (sysarg_t) call->priv;
779
780 call->data.cap_handle = CAP_NIL;
781
782 STRUCT_TO_USPACE(calldata, &call->data);
783 kobject_put(call->kobject);
784
785 return EOK;
786 }
787
788 if (call->flags & IPC_CALL_ANSWERED) {
789 process_answer(call);
790
791 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
792 kobject_put(call->kobject);
793 goto restart;
794 }
795
796 call->data.cap_handle = CAP_NIL;
797
798 STRUCT_TO_USPACE(calldata, &call->data);
799 kobject_put(call->kobject);
800
801 return EOK;
802 }
803
804 if (process_request(&TASK->answerbox, call))
805 goto restart;
806
807 cap_handle_t handle = CAP_NIL;
808 rc = cap_alloc(TASK, &handle);
809 if (rc != EOK) {
810 goto error;
811 }
812
813 call->data.cap_handle = handle;
814
815 /*
816 * Include phone hash of the caller in the request, copy the whole
817 * call->data, not only call->data.args.
818 */
819 rc = STRUCT_TO_USPACE(calldata, &call->data);
820 if (rc != EOK)
821 goto error;
822
823 kobject_add_ref(call->kobject);
824 cap_publish(TASK, handle, call->kobject);
825 return EOK;
826
827error:
828 if (CAP_HANDLE_VALID(handle))
829 cap_free(TASK, handle);
830
831 /*
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.
835 */
836 ipc_data_t saved_data;
837 bool saved;
838
839 if (answer_need_old(call)) {
840 memcpy(&saved_data, &call->data, sizeof(call->data));
841 saved = true;
842 } else
843 saved = false;
844
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);
849
850 return rc;
851}
852
853/** Interrupt one thread from sys_ipc_wait_for_call().
854 *
855 */
856sys_errno_t sys_ipc_poke(void)
857{
858 waitq_wakeup(&TASK->answerbox.wq, WAKEUP_FIRST);
859 return EOK;
860}
861
862/** Connect an IRQ handler to a task.
863 *
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.
867 *
868 * @param[out] uspace_handle Uspace pointer to IRQ capability handle
869 *
870 * @return EPERM
871 * @return Error code returned by ipc_irq_subscribe().
872 *
873 */
874sys_errno_t sys_ipc_irq_subscribe(inr_t inr, sysarg_t imethod,
875 irq_code_t *ucode, cap_irq_handle_t *uspace_handle)
876{
877 if (!(perm_get(TASK) & PERM_IRQ_REG))
878 return EPERM;
879
880 return ipc_irq_subscribe(&TASK->answerbox, inr, imethod, ucode, uspace_handle);
881}
882
883/** Disconnect an IRQ handler from a task.
884 *
885 * @param handle IRQ capability handle.
886 *
887 * @return Zero on success or EPERM on error.
888 *
889 */
890sys_errno_t sys_ipc_irq_unsubscribe(cap_irq_handle_t handle)
891{
892 if (!(perm_get(TASK) & PERM_IRQ_REG))
893 return EPERM;
894
895 ipc_irq_unsubscribe(&TASK->answerbox, handle);
896
897 return 0;
898}
899
900/** Syscall connect to a task by ID
901 *
902 * @return Error code.
903 *
904 */
905sys_errno_t sys_ipc_connect_kbox(task_id_t *uspace_taskid,
906 cap_phone_handle_t *uspace_phone)
907{
908#ifdef CONFIG_UDEBUG
909 task_id_t taskid;
910 cap_phone_handle_t phone;
911
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);
915 }
916
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);
922 }
923 }
924
925 return (sys_errno_t) rc;
926#else
927 return (sys_errno_t) ENOTSUP;
928#endif
929}
930
931/** @}
932 */
Note: See TracBrowser for help on using the repository browser.