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

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

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 24.1 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 <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>
56
57#define STRUCT_TO_USPACE(dst, src) copy_to_uspace((dst), (src), sizeof(*(src)))
58
59/** Decide if the interface and method is a system method.
60 *
61 * @param imethod Interface and method to be decided.
62 *
63 * @return True if the interface and method is a system
64 * interface and method.
65 *
66 */
67static inline bool method_is_system(sysarg_t imethod)
68{
69 if (imethod <= IPC_M_LAST_SYSTEM)
70 return true;
71
72 return false;
73}
74
75/** Decide if the message with this interface and method is forwardable.
76 *
77 * Some system messages may be forwarded, for some of them
78 * it is useless.
79 *
80 * @param imethod Interface and method to be decided.
81 *
82 * @return True if the interface and method is forwardable.
83 *
84 */
85static inline bool method_is_forwardable(sysarg_t imethod)
86{
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;
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_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;
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_CONNECT_TO_ME:
136 case IPC_M_CONNECT_ME_TO:
137 case IPC_M_PAGE_IN:
138 case IPC_M_SHARE_OUT:
139 case IPC_M_SHARE_IN:
140 case IPC_M_DATA_WRITE:
141 case IPC_M_DATA_READ:
142 case IPC_M_STATE_CHANGE_AUTHORIZE:
143 return true;
144 default:
145 return false;
146 }
147}
148
149/** Interpret process answer as control information.
150 *
151 * This function is called directly after sys_ipc_answer().
152 *
153 * @param answer Call structure with the answer.
154 * @param olddata Saved data of the request.
155 *
156 * @return Return EOK on success or an error code.
157 *
158 */
159errno_t answer_preprocess(call_t *answer, ipc_data_t *olddata)
160{
161 errno_t rc = EOK;
162
163 spinlock_lock(&answer->forget_lock);
164 if (answer->forget) {
165 /*
166 * This is a forgotten call and answer->sender is not valid.
167 */
168 spinlock_unlock(&answer->forget_lock);
169
170 SYSIPC_OP(answer_cleanup, answer, olddata);
171 return rc;
172 } else {
173 assert(answer->active);
174
175 /*
176 * Mark the call as inactive to prevent _ipc_answer_free_call()
177 * from attempting to remove the call from the active list
178 * itself.
179 */
180 answer->active = false;
181
182 /*
183 * Remove the call from the sender's active call list.
184 * We enforce this locking order so that any potential
185 * concurrently executing forget operation is forced to
186 * release its active_calls_lock and lose the race to
187 * forget this soon to be answered call.
188 */
189 spinlock_lock(&answer->sender->active_calls_lock);
190 list_remove(&answer->ta_link);
191 spinlock_unlock(&answer->sender->active_calls_lock);
192 }
193 spinlock_unlock(&answer->forget_lock);
194
195 if ((errno_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
196 phone_t *phone = answer->caller_phone;
197 mutex_lock(&phone->lock);
198 if (phone->state == IPC_PHONE_CONNECTED) {
199 irq_spinlock_lock(&phone->callee->lock, true);
200 list_remove(&phone->link);
201 /* Drop callee->connected_phones reference */
202 kobject_put(phone->kobject);
203 phone->state = IPC_PHONE_SLAMMED;
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.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.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) == IPC_M_CONNECT_TO_ME)
507 phone_dealloc((cap_phone_handle_t)
508 IPC_GET_ARG5(call->data));
509
510 IPC_SET_ARG1(call->data, imethod);
511 IPC_SET_ARG2(call->data, arg1);
512 IPC_SET_ARG3(call->data, arg2);
513
514 if (slow)
515 IPC_SET_ARG4(call->data, arg3);
516
517 /*
518 * For system methods we deliberately don't
519 * overwrite ARG5.
520 */
521 } else {
522 IPC_SET_IMETHOD(call->data, imethod);
523 IPC_SET_ARG1(call->data, arg1);
524 IPC_SET_ARG2(call->data, arg2);
525 if (slow) {
526 IPC_SET_ARG3(call->data, arg3);
527 IPC_SET_ARG4(call->data, arg4);
528 IPC_SET_ARG5(call->data, arg5);
529 }
530 }
531 }
532
533 rc = ipc_forward(call, pkobj->phone, &TASK->answerbox, mode);
534 if (rc != EOK) {
535 after_forward = true;
536 goto error;
537 }
538
539 cap_free(TASK, chandle);
540 kobject_put(ckobj);
541 kobject_put(pkobj);
542 return EOK;
543
544error:
545 IPC_SET_RETVAL(call->data, EFORWARD);
546 (void) answer_preprocess(call, need_old ? &old : NULL);
547 if (after_forward)
548 _ipc_answer_free_call(call, false);
549 else
550 ipc_answer(&TASK->answerbox, call);
551
552 cap_free(TASK, chandle);
553 kobject_put(ckobj);
554
555 if (pkobj)
556 kobject_put(pkobj);
557 return rc;
558}
559
560/** Forward a received call to another destination - fast version.
561 *
562 * In case the original interface and method is a system method, ARG1, ARG2
563 * and ARG3 are overwritten in the forwarded message with the new method and
564 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
565 * are rewritten with the new interface and method, arg1 and arg2, respectively.
566 * Also note there is a set of immutable methods, for which the new method and
567 * arguments are not set and these values are ignored.
568 *
569 * @param chandle Call handle of the call to forward.
570 * @param phandle Phone handle to use for forwarding.
571 * @param imethod New interface and method to use for the forwarded call.
572 * @param arg1 New value of the first argument for the forwarded call.
573 * @param arg2 New value of the second argument for the forwarded call.
574 * @param mode Flags that specify mode of the forward operation.
575 *
576 * @return 0 on succes, otherwise an error code.
577 *
578 */
579sys_errno_t sys_ipc_forward_fast(cap_call_handle_t chandle,
580 cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
581 unsigned int mode)
582{
583 return sys_ipc_forward_common(chandle, phandle, imethod, arg1, arg2, 0,
584 0, 0, mode, false);
585}
586
587/** Forward a received call to another destination - slow version.
588 *
589 * This function is the slow verision of the sys_ipc_forward_fast interface.
590 * It can copy all five new arguments and the new interface and method from
591 * the userspace. It naturally extends the functionality of the fast version.
592 * For system methods, it additionally stores the new value of arg3 to ARG4.
593 * For non-system methods, it additionally stores the new value of arg3, arg4
594 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
595 *
596 * @param chandle Call handle of the call to forward.
597 * @param phandle Phone handle to use for forwarding.
598 * @param data Userspace address of the new IPC data.
599 * @param mode Flags that specify mode of the forward operation.
600 *
601 * @return 0 on succes, otherwise an error code.
602 *
603 */
604sys_errno_t sys_ipc_forward_slow(cap_call_handle_t chandle,
605 cap_phone_handle_t phandle, ipc_data_t *data, unsigned int mode)
606{
607 ipc_data_t newdata;
608 errno_t rc = copy_from_uspace(&newdata.args, &data->args,
609 sizeof(newdata.args));
610 if (rc != EOK)
611 return (sys_errno_t) rc;
612
613 return sys_ipc_forward_common(chandle, phandle,
614 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
615 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
616 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
617}
618
619/** Answer an IPC call - fast version.
620 *
621 * This function can handle only two return arguments of payload, but is faster
622 * than the generic sys_ipc_answer().
623 *
624 * @param chandle Call handle to be answered.
625 * @param retval Return value of the answer.
626 * @param arg1 Service-defined return value.
627 * @param arg2 Service-defined return value.
628 * @param arg3 Service-defined return value.
629 * @param arg4 Service-defined return value.
630 *
631 * @return 0 on success, otherwise an error code.
632 *
633 */
634sys_errno_t sys_ipc_answer_fast(cap_call_handle_t chandle, sysarg_t retval,
635 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
636{
637 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
638 if (!kobj)
639 return ENOENT;
640
641 call_t *call = kobj->call;
642 assert(!(call->flags & IPC_CALL_ANSWERED));
643
644 ipc_data_t saved_data;
645 bool saved;
646
647 if (answer_need_old(call)) {
648 memcpy(&saved_data, &call->data, sizeof(call->data));
649 saved = true;
650 } else
651 saved = false;
652
653 IPC_SET_RETVAL(call->data, retval);
654 IPC_SET_ARG1(call->data, arg1);
655 IPC_SET_ARG2(call->data, arg2);
656 IPC_SET_ARG3(call->data, arg3);
657 IPC_SET_ARG4(call->data, arg4);
658
659 /*
660 * To achieve deterministic behavior, zero out arguments that are beyond
661 * the limits of the fast version.
662 */
663 IPC_SET_ARG5(call->data, 0);
664 errno_t rc = answer_preprocess(call, saved ? &saved_data : NULL);
665
666 ipc_answer(&TASK->answerbox, call);
667
668 kobject_put(kobj);
669 cap_free(TASK, chandle);
670
671 return rc;
672}
673
674/** Answer an IPC call.
675 *
676 * @param chandle Call handle to be answered.
677 * @param data Userspace address of call data with the answer.
678 *
679 * @return 0 on success, otherwise an error code.
680 *
681 */
682sys_errno_t sys_ipc_answer_slow(cap_call_handle_t chandle, ipc_data_t *data)
683{
684 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
685 if (!kobj)
686 return ENOENT;
687
688 call_t *call = kobj->call;
689 assert(!(call->flags & IPC_CALL_ANSWERED));
690
691 ipc_data_t saved_data;
692 bool saved;
693
694 if (answer_need_old(call)) {
695 memcpy(&saved_data, &call->data, sizeof(call->data));
696 saved = true;
697 } else
698 saved = false;
699
700 errno_t rc = copy_from_uspace(&call->data.args, &data->args,
701 sizeof(call->data.args));
702 if (rc != EOK) {
703 /*
704 * Republish the capability so that the call does not get lost.
705 */
706 cap_publish(TASK, chandle, kobj);
707 return rc;
708 }
709
710 rc = answer_preprocess(call, saved ? &saved_data : NULL);
711
712 ipc_answer(&TASK->answerbox, call);
713
714 kobject_put(kobj);
715 cap_free(TASK, chandle);
716
717 return rc;
718}
719
720/** Hang up a phone.
721 *
722 * @param handle Phone capability handle of the phone to be hung up.
723 *
724 * @return 0 on success or an error code.
725 *
726 */
727sys_errno_t sys_ipc_hangup(cap_phone_handle_t handle)
728{
729 kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
730 if (!kobj)
731 return ENOENT;
732
733 errno_t rc = ipc_phone_hangup(kobj->phone);
734 kobject_put(kobj);
735 cap_free(TASK, handle);
736 return rc;
737}
738
739/** Wait for an incoming IPC call or an answer.
740 *
741 * @param calldata Pointer to buffer where the call/answer data is stored.
742 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
743 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
744 * for explanation.
745 *
746 * @return An error code on error.
747 */
748sys_errno_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
749 unsigned int flags)
750{
751 call_t *call = NULL;
752
753restart:
754
755#ifdef CONFIG_UDEBUG
756 udebug_stoppable_begin();
757#endif
758
759 errno_t rc = ipc_wait_for_call(&TASK->answerbox, usec,
760 flags | SYNCH_FLAGS_INTERRUPTIBLE, &call);
761
762#ifdef CONFIG_UDEBUG
763 udebug_stoppable_end();
764#endif
765
766 if (rc != EOK)
767 return rc;
768
769 assert(call);
770
771 call->data.flags = call->flags;
772 if (call->flags & IPC_CALL_NOTIF) {
773 /* Set in_phone_hash to the interrupt counter */
774 call->data.phone = (void *) call->priv;
775
776 call->data.cap_handle = CAP_NIL;
777
778 STRUCT_TO_USPACE(calldata, &call->data);
779 kobject_put(call->kobject);
780
781 return EOK;
782 }
783
784 if (call->flags & IPC_CALL_ANSWERED) {
785 process_answer(call);
786
787 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
788 kobject_put(call->kobject);
789 goto restart;
790 }
791
792 call->data.cap_handle = CAP_NIL;
793
794 STRUCT_TO_USPACE(calldata, &call->data);
795 kobject_put(call->kobject);
796
797 return EOK;
798 }
799
800 if (process_request(&TASK->answerbox, call))
801 goto restart;
802
803 cap_handle_t handle = CAP_NIL;
804 rc = cap_alloc(TASK, &handle);
805 if (rc != EOK) {
806 goto error;
807 }
808
809 call->data.cap_handle = handle;
810
811 /*
812 * Include phone hash of the caller in the request, copy the whole
813 * call->data, not only call->data.args.
814 */
815 rc = STRUCT_TO_USPACE(calldata, &call->data);
816 if (rc != EOK)
817 goto error;
818
819 kobject_add_ref(call->kobject);
820 cap_publish(TASK, handle, call->kobject);
821 return EOK;
822
823error:
824 if (CAP_HANDLE_VALID(handle))
825 cap_free(TASK, handle);
826
827 /*
828 * The callee will not receive this call and no one else has a chance to
829 * answer it. Set the IPC_CALL_AUTO_REPLY flag and return the EPARTY
830 * error code.
831 */
832 ipc_data_t saved_data;
833 bool saved;
834
835 if (answer_need_old(call)) {
836 memcpy(&saved_data, &call->data, sizeof(call->data));
837 saved = true;
838 } else
839 saved = false;
840
841 IPC_SET_RETVAL(call->data, EPARTY);
842 (void) answer_preprocess(call, saved ? &saved_data : NULL);
843 call->flags |= IPC_CALL_AUTO_REPLY;
844 ipc_answer(&TASK->answerbox, call);
845
846 return rc;
847}
848
849/** Interrupt one thread from sys_ipc_wait_for_call().
850 *
851 */
852sys_errno_t sys_ipc_poke(void)
853{
854 waitq_wakeup(&TASK->answerbox.wq, WAKEUP_FIRST);
855 return EOK;
856}
857
858/** Connect an IRQ handler to a task.
859 *
860 * @param inr IRQ number.
861 * @param imethod Interface and method to be associated with the notification.
862 * @param ucode Uspace pointer to the top-half pseudocode.
863 *
864 * @param[out] uspace_handle Uspace pointer to IRQ capability handle
865 *
866 * @return EPERM
867 * @return Error code returned by ipc_irq_subscribe().
868 *
869 */
870sys_errno_t sys_ipc_irq_subscribe(inr_t inr, sysarg_t imethod,
871 irq_code_t *ucode, cap_irq_handle_t *uspace_handle)
872{
873 if (!(perm_get(TASK) & PERM_IRQ_REG))
874 return EPERM;
875
876 return ipc_irq_subscribe(&TASK->answerbox, inr, imethod, ucode, uspace_handle);
877}
878
879/** Disconnect an IRQ handler from a task.
880 *
881 * @param handle IRQ capability handle.
882 *
883 * @return Zero on success or EPERM on error.
884 *
885 */
886sys_errno_t sys_ipc_irq_unsubscribe(cap_irq_handle_t handle)
887{
888 if (!(perm_get(TASK) & PERM_IRQ_REG))
889 return EPERM;
890
891 ipc_irq_unsubscribe(&TASK->answerbox, handle);
892
893 return 0;
894}
895
896/** Syscall connect to a task by ID
897 *
898 * @return Error code.
899 *
900 */
901sys_errno_t sys_ipc_connect_kbox(task_id_t *uspace_taskid,
902 cap_phone_handle_t *uspace_phone)
903{
904#ifdef CONFIG_UDEBUG
905 task_id_t taskid;
906 cap_phone_handle_t phone;
907
908 errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(task_id_t));
909 if (rc == EOK) {
910 rc = ipc_connect_kbox((task_id_t) taskid, &phone);
911 }
912
913 if (rc == EOK) {
914 rc = copy_to_uspace(uspace_phone, &phone, sizeof(cap_handle_t));
915 if (rc != EOK) {
916 // Clean up the phone on failure.
917 sys_ipc_hangup(phone);
918 }
919 }
920
921 return (sys_errno_t) rc;
922#else
923 return (sys_errno_t) ENOTSUP;
924#endif
925}
926
927/** @}
928 */
Note: See TracBrowser for help on using the repository browser.