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

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

Remove unused connection cloning

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