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

Last change on this file since ff4d31c was 455241b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 9 months ago

Integrate kobject_t into target structures

This just means fewer allocations and indirections.

  • Property mode set to 100644
File size: 24.7 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 <memw.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 * @return ENOMEM if not enough memory to make the call
270 *
271 */
272errno_t
273ipc_req_internal(cap_phone_handle_t handle, ipc_data_t *data, sysarg_t priv)
274{
275 phone_t *phone = phone_from_kobject(
276 kobject_get(TASK, handle, KOBJECT_TYPE_PHONE));
277 if (!phone)
278 return ENOENT;
279
280 call_t *call = ipc_call_alloc();
281 if (!call) {
282 kobject_put(&phone->kobject);
283 return ENOMEM;
284 }
285
286 call->priv = priv;
287 memcpy(call->data.args, data->args, sizeof(data->args));
288
289 errno_t rc = request_preprocess(call, phone);
290 if (!rc) {
291#ifdef CONFIG_UDEBUG
292 udebug_stoppable_begin();
293#endif
294
295 kobject_add_ref(&call->kobject);
296 rc = ipc_call_sync(phone, call);
297 spinlock_lock(&call->forget_lock);
298 bool forgotten = call->forget;
299 spinlock_unlock(&call->forget_lock);
300 kobject_put(&call->kobject);
301
302#ifdef CONFIG_UDEBUG
303 udebug_stoppable_end();
304#endif
305
306 if (rc != EOK) {
307 if (!forgotten) {
308 /*
309 * There was an error, but it did not result
310 * in the call being forgotten. In fact, the
311 * call was not even sent. We are still
312 * its owners and are responsible for its
313 * deallocation.
314 */
315 kobject_put(&call->kobject);
316 } else {
317 /*
318 * The call was forgotten and it changed hands.
319 * We are no longer expected to free it.
320 */
321 assert(rc == EINTR);
322 }
323 kobject_put(&phone->kobject);
324 return rc;
325 }
326
327 process_answer(call);
328 } else
329 ipc_set_retval(&call->data, rc);
330
331 memcpy(data->args, call->data.args, sizeof(data->args));
332 kobject_put(&call->kobject);
333 kobject_put(&phone->kobject);
334
335 return EOK;
336}
337
338/** Check that the task did not exceed the allowed limit of asynchronous calls
339 * made over a phone.
340 *
341 * @param phone Phone to check the limit against.
342 *
343 * @return 0 if limit not reached or -1 if limit exceeded.
344 *
345 */
346static int check_call_limit(phone_t *phone)
347{
348 if (atomic_load(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
349 return -1;
350
351 return 0;
352}
353
354/** Make a fast asynchronous call over IPC.
355 *
356 * This function can only handle three arguments of payload, but is faster than
357 * the generic function sys_ipc_call_async_slow().
358 *
359 * @param handle Phone capability handle for the call.
360 * @param imethod Interface and method of the call.
361 * @param arg1 Service-defined payload argument.
362 * @param arg2 Service-defined payload argument.
363 * @param arg3 Service-defined payload argument.
364 * @param label User-defined label.
365 *
366 * @return EOK on success.
367 * @return An error code on error.
368 *
369 */
370sys_errno_t sys_ipc_call_async_fast(cap_phone_handle_t handle, sysarg_t imethod,
371 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t label)
372{
373 phone_t *phone = phone_from_kobject(
374 kobject_get(TASK, handle, KOBJECT_TYPE_PHONE));
375
376 if (!phone)
377 return ENOENT;
378
379 if (check_call_limit(phone)) {
380 kobject_put(&phone->kobject);
381 return ELIMIT;
382 }
383
384 call_t *call = ipc_call_alloc();
385 if (!call) {
386 kobject_put(&phone->kobject);
387 return ENOMEM;
388 }
389
390 ipc_set_imethod(&call->data, imethod);
391 ipc_set_arg1(&call->data, arg1);
392 ipc_set_arg2(&call->data, arg2);
393 ipc_set_arg3(&call->data, arg3);
394
395 /*
396 * To achieve deterministic behavior, zero out arguments that are beyond
397 * the limits of the fast version.
398 */
399 ipc_set_arg5(&call->data, 0);
400
401 /* Set the user-defined label */
402 call->data.answer_label = label;
403
404 errno_t res = request_preprocess(call, phone);
405
406 if (!res)
407 ipc_call(phone, call);
408 else
409 ipc_backsend_err(phone, call, res);
410
411 kobject_put(&phone->kobject);
412 return EOK;
413}
414
415/** Make an asynchronous IPC call allowing to transmit the entire payload.
416 *
417 * @param handle Phone capability for the call.
418 * @param data Userspace address of call data with the request.
419 * @param label User-defined label.
420 *
421 * @return See sys_ipc_call_async_fast().
422 *
423 */
424sys_errno_t sys_ipc_call_async_slow(cap_phone_handle_t handle, uspace_ptr_ipc_data_t data,
425 sysarg_t label)
426{
427 phone_t *phone = phone_from_kobject(
428 kobject_get(TASK, handle, KOBJECT_TYPE_PHONE));
429 if (!phone)
430 return ENOENT;
431
432 if (check_call_limit(phone)) {
433 kobject_put(&phone->kobject);
434 return ELIMIT;
435 }
436
437 call_t *call = ipc_call_alloc();
438 if (!call) {
439 kobject_put(&phone->kobject);
440 return ENOMEM;
441 }
442
443 errno_t rc = copy_from_uspace(&call->data.args, data + offsetof(ipc_data_t, args),
444 sizeof(call->data.args));
445 if (rc != EOK) {
446 kobject_put(&call->kobject);
447 kobject_put(&phone->kobject);
448 return (sys_errno_t) rc;
449 }
450
451 /* Set the user-defined label */
452 call->data.answer_label = label;
453
454 errno_t res = request_preprocess(call, phone);
455
456 if (!res)
457 ipc_call(phone, call);
458 else
459 ipc_backsend_err(phone, call, res);
460
461 kobject_put(&phone->kobject);
462 return EOK;
463}
464
465/** Forward a received call to another destination
466 *
467 * Common code for both the fast and the slow version.
468 *
469 * @param chandle Call handle of the forwarded call.
470 * @param phandle Phone handle to use for forwarding.
471 * @param imethod New interface and method to use for the forwarded call.
472 * @param arg1 New value of the first argument for the forwarded call.
473 * @param arg2 New value of the second argument for the forwarded call.
474 * @param arg3 New value of the third argument for the forwarded call.
475 * @param arg4 New value of the fourth argument for the forwarded call.
476 * @param arg5 New value of the fifth argument for the forwarded call.
477 * @param mode Flags that specify mode of the forward operation.
478 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
479 * the function considers only the fast version arguments:
480 * i.e. arg1 and arg2.
481 *
482 * @return 0 on succes, otherwise an error code.
483 *
484 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
485 *
486 */
487static sys_errno_t sys_ipc_forward_common(cap_call_handle_t chandle,
488 cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
489 sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
490{
491 kobject_t *ckobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
492 if (!ckobj)
493 return ENOENT;
494
495 call_t *call = call_from_kobject(ckobj);
496
497 ipc_data_t old;
498 bool need_old = answer_need_old(call);
499 if (need_old)
500 old = call->data;
501
502 bool after_forward = false;
503 errno_t rc;
504
505 kobject_t *pkobj = kobject_get(TASK, phandle, KOBJECT_TYPE_PHONE);
506 if (!pkobj) {
507 rc = ENOENT;
508 goto error;
509 }
510
511 if (!method_is_forwardable(ipc_get_imethod(&call->data))) {
512 rc = EPERM;
513 goto error;
514 }
515
516 call->flags |= IPC_CALL_FORWARDED;
517
518 /*
519 * User space is not allowed to change interface and method of system
520 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
521 * means of imethod, arg1, arg2 and arg3.
522 * If the interface and method is immutable, don't change anything.
523 */
524 if (!method_is_immutable(ipc_get_imethod(&call->data))) {
525 if (method_is_system(ipc_get_imethod(&call->data))) {
526 if (ipc_get_imethod(&call->data) ==
527 IPC_M_CONNECT_TO_ME) {
528 kobject_put((kobject_t *) call->priv);
529 call->priv = 0;
530 cap_free(TASK,
531 (cap_handle_t) ipc_get_arg5(&call->data));
532 }
533
534 ipc_set_arg1(&call->data, imethod);
535 ipc_set_arg2(&call->data, arg1);
536 ipc_set_arg3(&call->data, arg2);
537
538 if (slow)
539 ipc_set_arg4(&call->data, arg3);
540
541 /*
542 * For system methods we deliberately don't
543 * overwrite ARG5.
544 */
545 } else {
546 ipc_set_imethod(&call->data, imethod);
547 ipc_set_arg1(&call->data, arg1);
548 ipc_set_arg2(&call->data, arg2);
549 if (slow) {
550 ipc_set_arg3(&call->data, arg3);
551 ipc_set_arg4(&call->data, arg4);
552 ipc_set_arg5(&call->data, arg5);
553 }
554 }
555 }
556
557 rc = ipc_forward(call, phone_from_kobject(pkobj), &TASK->answerbox, mode);
558 if (rc != EOK) {
559 after_forward = true;
560 goto error;
561 }
562
563 cap_free(TASK, chandle);
564 kobject_put(ckobj);
565 kobject_put(pkobj);
566 return EOK;
567
568error:
569 ipc_set_retval(&call->data, EFORWARD);
570 (void) answer_preprocess(call, need_old ? &old : NULL);
571 if (after_forward)
572 _ipc_answer_free_call(call, false);
573 else
574 ipc_answer(&TASK->answerbox, call);
575
576 cap_free(TASK, chandle);
577 kobject_put(ckobj);
578
579 if (pkobj)
580 kobject_put(pkobj);
581 return rc;
582}
583
584/** Forward a received call to another destination - fast version.
585 *
586 * In case the original interface and method is a system method, ARG1, ARG2
587 * and ARG3 are overwritten in the forwarded message with the new method and
588 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
589 * are rewritten with the new interface and method, arg1 and arg2, respectively.
590 * Also note there is a set of immutable methods, for which the new method and
591 * arguments are not set and these values are ignored.
592 *
593 * @param chandle Call handle of the call to forward.
594 * @param phandle Phone handle to use for forwarding.
595 * @param imethod New interface and method to use for the forwarded call.
596 * @param arg1 New value of the first argument for the forwarded call.
597 * @param arg2 New value of the second argument for the forwarded call.
598 * @param mode Flags that specify mode of the forward operation.
599 *
600 * @return 0 on succes, otherwise an error code.
601 *
602 */
603sys_errno_t sys_ipc_forward_fast(cap_call_handle_t chandle,
604 cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
605 unsigned int mode)
606{
607 return sys_ipc_forward_common(chandle, phandle, imethod, arg1, arg2, 0,
608 0, 0, mode, false);
609}
610
611/** Forward a received call to another destination - slow version.
612 *
613 * This function is the slow verision of the sys_ipc_forward_fast interface.
614 * It can copy all five new arguments and the new interface and method from
615 * the userspace. It naturally extends the functionality of the fast version.
616 * For system methods, it additionally stores the new value of arg3 to ARG4.
617 * For non-system methods, it additionally stores the new value of arg3, arg4
618 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
619 *
620 * @param chandle Call handle of the call to forward.
621 * @param phandle Phone handle to use for forwarding.
622 * @param data Userspace address of the new IPC data.
623 * @param mode Flags that specify mode of the forward operation.
624 *
625 * @return 0 on succes, otherwise an error code.
626 *
627 */
628sys_errno_t sys_ipc_forward_slow(cap_call_handle_t chandle,
629 cap_phone_handle_t phandle, uspace_ptr_ipc_data_t data, unsigned int mode)
630{
631 ipc_data_t newdata;
632 errno_t rc = copy_from_uspace(&newdata.args, data + offsetof(ipc_data_t, args),
633 sizeof(newdata.args));
634 if (rc != EOK)
635 return (sys_errno_t) rc;
636
637 return sys_ipc_forward_common(chandle, phandle,
638 ipc_get_imethod(&newdata), ipc_get_arg1(&newdata),
639 ipc_get_arg2(&newdata), ipc_get_arg3(&newdata),
640 ipc_get_arg4(&newdata), ipc_get_arg5(&newdata), mode, true);
641}
642
643/** Answer an IPC call - fast version.
644 *
645 * This function can handle only two return arguments of payload, but is faster
646 * than the generic sys_ipc_answer().
647 *
648 * @param chandle Call handle to be answered.
649 * @param retval Return value of the answer.
650 * @param arg1 Service-defined return value.
651 * @param arg2 Service-defined return value.
652 * @param arg3 Service-defined return value.
653 * @param arg4 Service-defined return value.
654 *
655 * @return 0 on success, otherwise an error code.
656 *
657 */
658sys_errno_t sys_ipc_answer_fast(cap_call_handle_t chandle, sysarg_t retval,
659 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
660{
661 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
662 if (!kobj)
663 return ENOENT;
664
665 call_t *call = call_from_kobject(kobj);
666 assert(!(call->flags & IPC_CALL_ANSWERED));
667
668 ipc_data_t saved_data;
669 bool saved;
670
671 if (answer_need_old(call)) {
672 memcpy(&saved_data, &call->data, sizeof(call->data));
673 saved = true;
674 } else
675 saved = false;
676
677 ipc_set_retval(&call->data, retval);
678 ipc_set_arg1(&call->data, arg1);
679 ipc_set_arg2(&call->data, arg2);
680 ipc_set_arg3(&call->data, arg3);
681 ipc_set_arg4(&call->data, arg4);
682
683 /*
684 * To achieve deterministic behavior, zero out arguments that are beyond
685 * the limits of the fast version.
686 */
687 ipc_set_arg5(&call->data, 0);
688 errno_t rc = answer_preprocess(call, saved ? &saved_data : NULL);
689
690 ipc_answer(&TASK->answerbox, call);
691
692 kobject_put(kobj);
693 cap_free(TASK, chandle);
694
695 return rc;
696}
697
698/** Answer an IPC call.
699 *
700 * @param chandle Call handle to be answered.
701 * @param data Userspace address of call data with the answer.
702 *
703 * @return 0 on success, otherwise an error code.
704 *
705 */
706sys_errno_t sys_ipc_answer_slow(cap_call_handle_t chandle, uspace_ptr_ipc_data_t data)
707{
708 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
709 if (!kobj)
710 return ENOENT;
711
712 call_t *call = call_from_kobject(kobj);
713 assert(!(call->flags & IPC_CALL_ANSWERED));
714
715 ipc_data_t saved_data;
716 bool saved;
717
718 if (answer_need_old(call)) {
719 memcpy(&saved_data, &call->data, sizeof(call->data));
720 saved = true;
721 } else
722 saved = false;
723
724 errno_t rc = copy_from_uspace(&call->data.args, data + offsetof(ipc_data_t, args),
725 sizeof(call->data.args));
726 if (rc != EOK) {
727 /*
728 * Republish the capability so that the call does not get lost.
729 */
730 cap_publish(TASK, chandle, kobj);
731 return rc;
732 }
733
734 rc = answer_preprocess(call, saved ? &saved_data : NULL);
735
736 ipc_answer(&TASK->answerbox, call);
737
738 kobject_put(kobj);
739 cap_free(TASK, chandle);
740
741 return rc;
742}
743
744/** Hang up a phone.
745 *
746 * @param handle Phone capability handle of the phone to be hung up.
747 *
748 * @return 0 on success or an error code.
749 *
750 */
751sys_errno_t sys_ipc_hangup(cap_phone_handle_t handle)
752{
753 kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
754 if (!kobj)
755 return ENOENT;
756
757 errno_t rc = ipc_phone_hangup(phone_from_kobject(kobj));
758 kobject_put(kobj);
759 cap_free(TASK, handle);
760 return rc;
761}
762
763/** Wait for an incoming IPC call or an answer.
764 *
765 * @param calldata Pointer to buffer where the call/answer data is stored.
766 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
767 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
768 * for explanation.
769 *
770 * @return An error code on error.
771 */
772sys_errno_t sys_ipc_wait_for_call(uspace_ptr_ipc_data_t calldata, uint32_t usec,
773 unsigned int flags)
774{
775 call_t *call = NULL;
776 errno_t rc;
777restart:
778
779#ifdef CONFIG_UDEBUG
780 udebug_stoppable_begin();
781#endif
782
783 rc = ipc_wait_for_call(&TASK->answerbox, usec,
784 flags | SYNCH_FLAGS_INTERRUPTIBLE, &call);
785
786#ifdef CONFIG_UDEBUG
787 udebug_stoppable_end();
788#endif
789
790 if (rc != EOK)
791 return rc;
792
793 assert(call);
794
795 call->data.flags = call->flags;
796 if (call->flags & IPC_CALL_NOTIF) {
797 /* Set the request_label to the interrupt counter */
798 call->data.request_label = (sysarg_t) call->priv;
799
800 call->data.cap_handle = CAP_NIL;
801
802 STRUCT_TO_USPACE(calldata, &call->data);
803 kobject_put(&call->kobject);
804
805 return EOK;
806 }
807
808 if (call->flags & IPC_CALL_ANSWERED) {
809 process_answer(call);
810
811 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
812 kobject_put(&call->kobject);
813 goto restart;
814 }
815
816 call->data.cap_handle = CAP_NIL;
817
818 STRUCT_TO_USPACE(calldata, &call->data);
819 kobject_put(&call->kobject);
820
821 return EOK;
822 }
823
824 if (process_request(&TASK->answerbox, call))
825 goto restart;
826
827 cap_handle_t handle = CAP_NIL;
828 rc = cap_alloc(TASK, &handle);
829 if (rc != EOK) {
830 goto error;
831 }
832
833 call->data.cap_handle = handle;
834
835 /*
836 * Copy the whole call->data to include the request label.
837 */
838 rc = STRUCT_TO_USPACE(calldata, &call->data);
839 if (rc != EOK)
840 goto error;
841
842 kobject_add_ref(&call->kobject);
843 cap_publish(TASK, handle, &call->kobject);
844 return EOK;
845
846error:
847 if (cap_handle_valid(handle))
848 cap_free(TASK, handle);
849
850 /*
851 * The callee will not receive this call and no one else has a chance to
852 * answer it. Set the IPC_CALL_AUTO_REPLY flag and return the EPARTY
853 * error code.
854 */
855 ipc_data_t saved_data;
856 bool saved;
857
858 if (answer_need_old(call)) {
859 memcpy(&saved_data, &call->data, sizeof(call->data));
860 saved = true;
861 } else
862 saved = false;
863
864 ipc_set_retval(&call->data, EPARTY);
865 (void) answer_preprocess(call, saved ? &saved_data : NULL);
866 call->flags |= IPC_CALL_AUTO_REPLY;
867 ipc_answer(&TASK->answerbox, call);
868
869 return rc;
870}
871
872/** Interrupt one thread from sys_ipc_wait_for_call().
873 *
874 */
875sys_errno_t sys_ipc_poke(void)
876{
877 waitq_wake_one(&TASK->answerbox.wq);
878 return EOK;
879}
880
881/** Connect an IRQ handler to a task.
882 *
883 * @param inr IRQ number.
884 * @param imethod Interface and method to be associated with the notification.
885 * @param ucode Uspace pointer to the top-half pseudocode.
886 *
887 * @param[out] uspace_handle Uspace pointer to IRQ capability handle
888 *
889 * @return EPERM
890 * @return Error code returned by ipc_irq_subscribe().
891 *
892 */
893sys_errno_t sys_ipc_irq_subscribe(inr_t inr, sysarg_t imethod,
894 uspace_ptr_irq_code_t ucode, uspace_ptr_cap_irq_handle_t uspace_handle)
895{
896 if (!(perm_get(TASK) & PERM_IRQ_REG))
897 return EPERM;
898
899 return ipc_irq_subscribe(&TASK->answerbox, inr, imethod, ucode, uspace_handle);
900}
901
902/** Disconnect an IRQ handler from a task.
903 *
904 * @param handle IRQ capability handle.
905 *
906 * @return Zero on success or EPERM on error.
907 *
908 */
909sys_errno_t sys_ipc_irq_unsubscribe(cap_irq_handle_t handle)
910{
911 if (!(perm_get(TASK) & PERM_IRQ_REG))
912 return EPERM;
913
914 ipc_irq_unsubscribe(&TASK->answerbox, handle);
915
916 return 0;
917}
918
919/** Syscall connect to a task by ID
920 *
921 * @return Error code.
922 *
923 */
924sys_errno_t sys_ipc_connect_kbox(uspace_ptr_task_id_t uspace_taskid,
925 uspace_ptr_cap_phone_handle_t uspace_phone)
926{
927#ifdef CONFIG_UDEBUG
928 task_id_t taskid;
929 cap_phone_handle_t phone;
930
931 errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(task_id_t));
932 if (rc == EOK) {
933 rc = ipc_connect_kbox((task_id_t) taskid, &phone);
934 }
935
936 if (rc == EOK) {
937 rc = copy_to_uspace(uspace_phone, &phone, sizeof(cap_handle_t));
938 if (rc != EOK) {
939 // Clean up the phone on failure.
940 sys_ipc_hangup(phone);
941 }
942 }
943
944 return (sys_errno_t) rc;
945#else
946 return (sys_errno_t) ENOTSUP;
947#endif
948}
949
950/** @}
951 */
Note: See TracBrowser for help on using the repository browser.