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

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

Be nice and provide a default switch case in answer_preprocess().

  • Property mode set to 100644
File size: 31.8 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 <proc/task.h>
37#include <proc/thread.h>
38#include <errno.h>
39#include <memstr.h>
40#include <debug.h>
41#include <ipc/ipc.h>
42#include <abi/ipc/methods.h>
43#include <ipc/sysipc.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 <udebug/udebug_ipc.h>
50#include <arch/interrupt.h>
51#include <syscall/copy.h>
52#include <security/cap.h>
53#include <console/console.h>
54#include <mm/as.h>
55#include <print.h>
56#include <macros.h>
57
58/**
59 * Maximum buffer size allowed for IPC_M_DATA_WRITE and IPC_M_DATA_READ
60 * requests.
61 */
62#define DATA_XFER_LIMIT (64 * 1024)
63
64#define STRUCT_TO_USPACE(dst, src) copy_to_uspace((dst), (src), sizeof(*(src)))
65
66/** Get phone from the current task by ID.
67 *
68 * @param phoneid Phone ID.
69 * @param phone Place to store pointer to phone.
70 *
71 * @return EOK on success, EINVAL if ID is invalid.
72 *
73 */
74static int phone_get(sysarg_t phoneid, phone_t **phone)
75{
76 if (phoneid >= IPC_MAX_PHONES)
77 return EINVAL;
78
79 *phone = &TASK->phones[phoneid];
80 return EOK;
81}
82
83/** Decide if the interface and method is a system method.
84 *
85 * @param imethod Interface and method to be decided.
86 *
87 * @return True if the interface and method is a system
88 * interface and method.
89 *
90 */
91static inline bool method_is_system(sysarg_t imethod)
92{
93 if (imethod <= IPC_M_LAST_SYSTEM)
94 return true;
95
96 return false;
97}
98
99/** Decide if the message with this interface and method is forwardable.
100 *
101 * Some system messages may be forwarded, for some of them
102 * it is useless.
103 *
104 * @param imethod Interface and method to be decided.
105 *
106 * @return True if the interface and method is forwardable.
107 *
108 */
109static inline bool method_is_forwardable(sysarg_t imethod)
110{
111 switch (imethod) {
112 case IPC_M_CONNECTION_CLONE:
113 case IPC_M_CLONE_ESTABLISH:
114 case IPC_M_PHONE_HUNGUP:
115 /* This message is meant only for the original recipient. */
116 return false;
117 default:
118 return true;
119 }
120}
121
122/** Decide if the message with this interface and method is immutable on forward.
123 *
124 * Some system messages may be forwarded but their content cannot be altered.
125 *
126 * @param imethod Interface and method to be decided.
127 *
128 * @return True if the interface and method is immutable on forward.
129 *
130 */
131static inline bool method_is_immutable(sysarg_t imethod)
132{
133 switch (imethod) {
134 case IPC_M_SHARE_OUT:
135 case IPC_M_SHARE_IN:
136 case IPC_M_DATA_WRITE:
137 case IPC_M_DATA_READ:
138 case IPC_M_STATE_CHANGE_AUTHORIZE:
139 return true;
140 default:
141 return false;
142 }
143}
144
145
146/***********************************************************************
147 * Functions that preprocess answer before sending it to the recepient.
148 ***********************************************************************/
149
150/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
151 * for answer_preprocess().
152 *
153 * @param call Call structure to be decided.
154 *
155 * @return true if the old call contents should be saved.
156 *
157 */
158static inline bool answer_need_old(call_t *call)
159{
160 switch (IPC_GET_IMETHOD(call->data)) {
161 case IPC_M_CONNECTION_CLONE:
162 case IPC_M_CLONE_ESTABLISH:
163 case IPC_M_CONNECT_TO_ME:
164 case IPC_M_CONNECT_ME_TO:
165 case IPC_M_SHARE_OUT:
166 case IPC_M_SHARE_IN:
167 case IPC_M_DATA_WRITE:
168 case IPC_M_DATA_READ:
169 case IPC_M_STATE_CHANGE_AUTHORIZE:
170 return true;
171 default:
172 return false;
173 }
174}
175
176static int a_preprocess_m_connection_clone(call_t *answer, ipc_data_t *olddata)
177{
178 int phoneid = (int) IPC_GET_ARG1(*olddata);
179 phone_t *phone = &TASK->phones[phoneid];
180
181 if (IPC_GET_RETVAL(answer->data) != EOK) {
182 /*
183 * The recipient of the cloned phone rejected the offer. In
184 * this case, the connection was established at the request
185 * time and therefore we need to slam the phone. We don't
186 * merely hangup as that would result in sending IPC_M_HUNGUP
187 * to the third party on the other side of the cloned phone.
188 */
189 mutex_lock(&phone->lock);
190 if (phone->state == IPC_PHONE_CONNECTED) {
191 irq_spinlock_lock(&phone->callee->lock, true);
192 list_remove(&phone->link);
193 phone->state = IPC_PHONE_SLAMMED;
194 irq_spinlock_unlock(&phone->callee->lock, true);
195 }
196 mutex_unlock(&phone->lock);
197 }
198
199 return EOK;
200}
201
202static int a_preprocess_m_clone_establish(call_t *answer, ipc_data_t *olddata)
203{
204 phone_t *phone = (phone_t *) IPC_GET_ARG5(*olddata);
205
206 if (IPC_GET_RETVAL(answer->data) != EOK) {
207 /*
208 * The other party on the cloned phoned rejected our request
209 * for connection on the protocol level. We need to break the
210 * connection without sending IPC_M_HUNGUP back.
211 */
212 mutex_lock(&phone->lock);
213 if (phone->state == IPC_PHONE_CONNECTED) {
214 irq_spinlock_lock(&phone->callee->lock, true);
215 list_remove(&phone->link);
216 phone->state = IPC_PHONE_SLAMMED;
217 irq_spinlock_unlock(&phone->callee->lock, true);
218 }
219 mutex_unlock(&phone->lock);
220 }
221
222 return EOK;
223}
224
225static int a_preprocess_m_connect_to_me(call_t *answer, ipc_data_t *olddata)
226{
227 int phoneid = (int) IPC_GET_ARG5(*olddata);
228
229 if (IPC_GET_RETVAL(answer->data) != EOK) {
230 /* The connection was not accepted */
231 phone_dealloc(phoneid);
232 } else {
233 /* The connection was accepted */
234 phone_connect(phoneid, &answer->sender->answerbox);
235 /* Set 'phone hash' as arg5 of response */
236 IPC_SET_ARG5(answer->data, (sysarg_t) &TASK->phones[phoneid]);
237 }
238
239 return EOK;
240}
241
242static int a_preprocess_m_connect_me_to(call_t *answer, ipc_data_t *olddata)
243{
244 phone_t *phone = (phone_t *) IPC_GET_ARG5(*olddata);
245
246 /* If the users accepted call, connect */
247 if (IPC_GET_RETVAL(answer->data) == EOK)
248 ipc_phone_connect(phone, &TASK->answerbox);
249
250 return EOK;
251}
252
253static int a_preprocess_m_share_out(call_t *answer, ipc_data_t *olddata)
254{
255 int rc = EOK;
256
257 if (!IPC_GET_RETVAL(answer->data)) {
258 /* Accepted, handle as_area receipt */
259
260 irq_spinlock_lock(&answer->sender->lock, true);
261 as_t *as = answer->sender->as;
262 irq_spinlock_unlock(&answer->sender->lock, true);
263
264 uintptr_t dst_base = (uintptr_t) -1;
265 rc = as_area_share(as, IPC_GET_ARG1(*olddata),
266 IPC_GET_ARG2(*olddata), AS, IPC_GET_ARG3(*olddata),
267 &dst_base, IPC_GET_ARG1(answer->data));
268
269 if (rc == EOK) {
270 rc = copy_to_uspace((void *) IPC_GET_ARG2(answer->data),
271 &dst_base, sizeof(dst_base));
272 }
273
274 IPC_SET_RETVAL(answer->data, rc);
275 }
276
277 return rc;
278}
279
280static int a_preprocess_m_share_in(call_t *answer, ipc_data_t *olddata)
281{
282 if (!IPC_GET_RETVAL(answer->data)) {
283 irq_spinlock_lock(&answer->sender->lock, true);
284 as_t *as = answer->sender->as;
285 irq_spinlock_unlock(&answer->sender->lock, true);
286
287 uintptr_t dst_base = (uintptr_t) -1;
288 int rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
289 IPC_GET_ARG1(*olddata), as, IPC_GET_ARG2(answer->data),
290 &dst_base, IPC_GET_ARG3(answer->data));
291 IPC_SET_ARG4(answer->data, dst_base);
292 IPC_SET_RETVAL(answer->data, rc);
293 }
294
295 return EOK;
296}
297
298static int a_preprocess_m_data_read(call_t *answer, ipc_data_t *olddata)
299{
300 ASSERT(!answer->buffer);
301 if (!IPC_GET_RETVAL(answer->data)) {
302 /* The recipient agreed to send data. */
303 uintptr_t src = IPC_GET_ARG1(answer->data);
304 uintptr_t dst = IPC_GET_ARG1(*olddata);
305 size_t max_size = IPC_GET_ARG2(*olddata);
306 size_t size = IPC_GET_ARG2(answer->data);
307 if (size && size <= max_size) {
308 /*
309 * Copy the destination VA so that this piece of
310 * information is not lost.
311 */
312 IPC_SET_ARG1(answer->data, dst);
313
314 answer->buffer = malloc(size, 0);
315 int rc = copy_from_uspace(answer->buffer,
316 (void *) src, size);
317 if (rc) {
318 IPC_SET_RETVAL(answer->data, rc);
319 free(answer->buffer);
320 answer->buffer = NULL;
321 }
322 } else if (!size) {
323 IPC_SET_RETVAL(answer->data, EOK);
324 } else {
325 IPC_SET_RETVAL(answer->data, ELIMIT);
326 }
327 }
328
329 return EOK;
330}
331
332static int a_preprocess_m_data_write(call_t *answer, ipc_data_t *olddata)
333{
334 ASSERT(answer->buffer);
335 if (!IPC_GET_RETVAL(answer->data)) {
336 /* The recipient agreed to receive data. */
337 uintptr_t dst = (uintptr_t)IPC_GET_ARG1(answer->data);
338 size_t size = (size_t)IPC_GET_ARG2(answer->data);
339 size_t max_size = (size_t)IPC_GET_ARG2(*olddata);
340
341 if (size <= max_size) {
342 int rc = copy_to_uspace((void *) dst,
343 answer->buffer, size);
344 if (rc)
345 IPC_SET_RETVAL(answer->data, rc);
346 } else {
347 IPC_SET_RETVAL(answer->data, ELIMIT);
348 }
349 }
350 free(answer->buffer);
351 answer->buffer = NULL;
352
353 return EOK;
354}
355
356static int
357a_preprocess_m_state_change_authorize(call_t *answer, ipc_data_t *olddata)
358{
359 int rc = EOK;
360
361 if (!IPC_GET_RETVAL(answer->data)) {
362 /* The recipient authorized the change of state. */
363 phone_t *recipient_phone;
364 task_t *other_task_s;
365 task_t *other_task_r;
366
367 rc = phone_get(IPC_GET_ARG1(answer->data),
368 &recipient_phone);
369 if (rc != EOK) {
370 IPC_SET_RETVAL(answer->data, ENOENT);
371 return ENOENT;
372 }
373
374 mutex_lock(&recipient_phone->lock);
375 if (recipient_phone->state != IPC_PHONE_CONNECTED) {
376 mutex_unlock(&recipient_phone->lock);
377 IPC_SET_RETVAL(answer->data, EINVAL);
378 return EINVAL;
379 }
380
381 other_task_r = recipient_phone->callee->task;
382 other_task_s = (task_t *) IPC_GET_ARG5(*olddata);
383
384 /*
385 * See if both the sender and the recipient meant the
386 * same third party task.
387 */
388 if (other_task_r != other_task_s) {
389 IPC_SET_RETVAL(answer->data, EINVAL);
390 rc = EINVAL;
391 } else {
392 rc = event_task_notify_5(other_task_r,
393 EVENT_TASK_STATE_CHANGE, false,
394 IPC_GET_ARG1(*olddata),
395 IPC_GET_ARG2(*olddata),
396 IPC_GET_ARG3(*olddata),
397 LOWER32(olddata->task_id),
398 UPPER32(olddata->task_id));
399 IPC_SET_RETVAL(answer->data, rc);
400 }
401
402 mutex_unlock(&recipient_phone->lock);
403 }
404
405 return rc;
406}
407
408/** Interpret process answer as control information.
409 *
410 * This function is called directly after sys_ipc_answer().
411 *
412 * @param answer Call structure with the answer.
413 * @param olddata Saved data of the request.
414 *
415 * @return Return EOK on success or a negative error code.
416 *
417 */
418static int answer_preprocess(call_t *answer, ipc_data_t *olddata)
419{
420 int rc = EOK;
421
422 if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
423 /* In case of forward, hangup the forwared phone,
424 * not the originator
425 */
426 mutex_lock(&answer->data.phone->lock);
427 irq_spinlock_lock(&TASK->answerbox.lock, true);
428 if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
429 list_remove(&answer->data.phone->link);
430 answer->data.phone->state = IPC_PHONE_SLAMMED;
431 }
432 irq_spinlock_unlock(&TASK->answerbox.lock, true);
433 mutex_unlock(&answer->data.phone->lock);
434 }
435
436 if (!olddata)
437 return rc;
438
439 switch (IPC_GET_IMETHOD(*olddata)) {
440 case IPC_M_CONNECTION_CLONE:
441 rc = a_preprocess_m_connection_clone(answer, olddata);
442 break;
443 case IPC_M_CLONE_ESTABLISH:
444 rc = a_preprocess_m_clone_establish(answer, olddata);
445 break;
446 case IPC_M_CONNECT_TO_ME:
447 rc = a_preprocess_m_connect_to_me(answer, olddata);
448 break;
449 case IPC_M_CONNECT_ME_TO:
450 rc = a_preprocess_m_connect_me_to(answer, olddata);
451 break;
452 case IPC_M_SHARE_OUT:
453 rc = a_preprocess_m_share_out(answer, olddata);
454 break;
455 case IPC_M_SHARE_IN:
456 rc = a_preprocess_m_share_in(answer, olddata);
457 break;
458 case IPC_M_DATA_READ:
459 rc = a_preprocess_m_data_read(answer, olddata);
460 break;
461 case IPC_M_DATA_WRITE:
462 rc = a_preprocess_m_data_write(answer, olddata);
463 break;
464 case IPC_M_STATE_CHANGE_AUTHORIZE:
465 rc = a_preprocess_m_state_change_authorize(answer, olddata);
466 break;
467 default:
468 break;
469 }
470
471 return rc;
472}
473
474static void phones_lock(phone_t *p1, phone_t *p2)
475{
476 if (p1 < p2) {
477 mutex_lock(&p1->lock);
478 mutex_lock(&p2->lock);
479 } else if (p1 > p2) {
480 mutex_lock(&p2->lock);
481 mutex_lock(&p1->lock);
482 } else
483 mutex_lock(&p1->lock);
484}
485
486static void phones_unlock(phone_t *p1, phone_t *p2)
487{
488 mutex_unlock(&p1->lock);
489 if (p1 != p2)
490 mutex_unlock(&p2->lock);
491}
492
493/** Called before the request is sent.
494 *
495 * @param call Call structure with the request.
496 * @param phone Phone that the call will be sent through.
497 *
498 * @return Return 0 on success, ELIMIT or EPERM on error.
499 *
500 */
501static int request_preprocess(call_t *call, phone_t *phone)
502{
503 switch (IPC_GET_IMETHOD(call->data)) {
504 case IPC_M_CONNECTION_CLONE: {
505 phone_t *cloned_phone;
506 if (phone_get(IPC_GET_ARG1(call->data), &cloned_phone) != EOK)
507 return ENOENT;
508
509 phones_lock(cloned_phone, phone);
510
511 if ((cloned_phone->state != IPC_PHONE_CONNECTED) ||
512 phone->state != IPC_PHONE_CONNECTED) {
513 phones_unlock(cloned_phone, phone);
514 return EINVAL;
515 }
516
517 /*
518 * We can be pretty sure now that both tasks exist and we are
519 * connected to them. As we continue to hold the phone locks,
520 * we are effectively preventing them from finishing their
521 * potential cleanup.
522 *
523 */
524 int newphid = phone_alloc(phone->callee->task);
525 if (newphid < 0) {
526 phones_unlock(cloned_phone, phone);
527 return ELIMIT;
528 }
529
530 ipc_phone_connect(&phone->callee->task->phones[newphid],
531 cloned_phone->callee);
532 phones_unlock(cloned_phone, phone);
533
534 /* Set the new phone for the callee. */
535 IPC_SET_ARG1(call->data, newphid);
536 break;
537 }
538 case IPC_M_CLONE_ESTABLISH:
539 IPC_SET_ARG5(call->data, (sysarg_t) phone);
540 break;
541 case IPC_M_CONNECT_ME_TO: {
542 int newphid = phone_alloc(TASK);
543 if (newphid < 0)
544 return ELIMIT;
545
546 /* Set arg5 for server */
547 IPC_SET_ARG5(call->data, (sysarg_t) &TASK->phones[newphid]);
548 call->flags |= IPC_CALL_CONN_ME_TO;
549 call->priv = newphid;
550 break;
551 }
552 case IPC_M_SHARE_OUT: {
553 size_t size = as_area_get_size(IPC_GET_ARG1(call->data));
554 if (!size)
555 return EPERM;
556
557 IPC_SET_ARG2(call->data, size);
558 break;
559 }
560 case IPC_M_DATA_READ: {
561 size_t size = IPC_GET_ARG2(call->data);
562 if (size > DATA_XFER_LIMIT) {
563 int flags = IPC_GET_ARG3(call->data);
564 if (flags & IPC_XF_RESTRICT)
565 IPC_SET_ARG2(call->data, DATA_XFER_LIMIT);
566 else
567 return ELIMIT;
568 }
569 break;
570 }
571 case IPC_M_DATA_WRITE: {
572 uintptr_t src = IPC_GET_ARG1(call->data);
573 size_t size = IPC_GET_ARG2(call->data);
574
575 if (size > DATA_XFER_LIMIT) {
576 int flags = IPC_GET_ARG3(call->data);
577 if (flags & IPC_XF_RESTRICT) {
578 size = DATA_XFER_LIMIT;
579 IPC_SET_ARG2(call->data, size);
580 } else
581 return ELIMIT;
582 }
583
584 call->buffer = (uint8_t *) malloc(size, 0);
585 int rc = copy_from_uspace(call->buffer, (void *) src, size);
586 if (rc != 0) {
587 free(call->buffer);
588 return rc;
589 }
590
591 break;
592 }
593 case IPC_M_STATE_CHANGE_AUTHORIZE: {
594 phone_t *sender_phone;
595 task_t *other_task_s;
596
597 if (phone_get(IPC_GET_ARG5(call->data), &sender_phone) != EOK)
598 return ENOENT;
599
600 mutex_lock(&sender_phone->lock);
601 if (sender_phone->state != IPC_PHONE_CONNECTED) {
602 mutex_unlock(&sender_phone->lock);
603 return EINVAL;
604 }
605
606 other_task_s = sender_phone->callee->task;
607
608 mutex_unlock(&sender_phone->lock);
609
610 /* Remember the third party task hash. */
611 IPC_SET_ARG5(call->data, (sysarg_t) other_task_s);
612 break;
613 }
614#ifdef CONFIG_UDEBUG
615 case IPC_M_DEBUG:
616 return udebug_request_preprocess(call, phone);
617#endif
618 default:
619 break;
620 }
621
622 return 0;
623}
624
625/*******************************************************************************
626 * Functions called to process received call/answer before passing it to uspace.
627 *******************************************************************************/
628
629/** Do basic kernel processing of received call answer.
630 *
631 * @param call Call structure with the answer.
632 *
633 */
634static void process_answer(call_t *call)
635{
636 if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
637 (call->flags & IPC_CALL_FORWARDED))
638 IPC_SET_RETVAL(call->data, EFORWARD);
639
640 if (call->flags & IPC_CALL_CONN_ME_TO) {
641 if (IPC_GET_RETVAL(call->data))
642 phone_dealloc(call->priv);
643 else
644 IPC_SET_ARG5(call->data, call->priv);
645 }
646
647 if (call->buffer) {
648 /*
649 * This must be an affirmative answer to IPC_M_DATA_READ
650 * or IPC_M_DEBUG/UDEBUG_M_MEM_READ...
651 *
652 */
653 uintptr_t dst = IPC_GET_ARG1(call->data);
654 size_t size = IPC_GET_ARG2(call->data);
655 int rc = copy_to_uspace((void *) dst, call->buffer, size);
656 if (rc)
657 IPC_SET_RETVAL(call->data, rc);
658 free(call->buffer);
659 call->buffer = NULL;
660 }
661}
662
663/** Do basic kernel processing of received call request.
664 *
665 * @param box Destination answerbox structure.
666 * @param call Call structure with the request.
667 *
668 * @return 0 if the call should be passed to userspace.
669 * @return -1 if the call should be ignored.
670 *
671 */
672static int process_request(answerbox_t *box, call_t *call)
673{
674 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME) {
675 int phoneid = phone_alloc(TASK);
676 if (phoneid < 0) { /* Failed to allocate phone */
677 IPC_SET_RETVAL(call->data, ELIMIT);
678 ipc_answer(box, call);
679 return -1;
680 }
681
682 IPC_SET_ARG5(call->data, phoneid);
683 }
684
685 switch (IPC_GET_IMETHOD(call->data)) {
686 case IPC_M_DEBUG:
687 return -1;
688 default:
689 break;
690 }
691
692 return 0;
693}
694
695/** Check that the task did not exceed the allowed limit of asynchronous calls
696 * made over a phone.
697 *
698 * @param phone Phone to check the limit against.
699 *
700 * @return 0 if limit not reached or -1 if limit exceeded.
701 *
702 */
703static int check_call_limit(phone_t *phone)
704{
705 if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
706 return -1;
707
708 return 0;
709}
710
711/** Make a fast asynchronous call over IPC.
712 *
713 * This function can only handle four arguments of payload, but is faster than
714 * the generic function sys_ipc_call_async_slow().
715 *
716 * @param phoneid Phone handle for the call.
717 * @param imethod Interface and method of the call.
718 * @param arg1 Service-defined payload argument.
719 * @param arg2 Service-defined payload argument.
720 * @param arg3 Service-defined payload argument.
721 * @param arg4 Service-defined payload argument.
722 *
723 * @return Call hash on success.
724 * @return IPC_CALLRET_FATAL in case of a fatal error.
725 * @return IPC_CALLRET_TEMPORARY if there are too many pending
726 * asynchronous requests; answers should be handled first.
727 *
728 */
729sysarg_t sys_ipc_call_async_fast(sysarg_t phoneid, sysarg_t imethod,
730 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
731{
732 phone_t *phone;
733 if (phone_get(phoneid, &phone) != EOK)
734 return IPC_CALLRET_FATAL;
735
736 if (check_call_limit(phone))
737 return IPC_CALLRET_TEMPORARY;
738
739 call_t *call = ipc_call_alloc(0);
740 IPC_SET_IMETHOD(call->data, imethod);
741 IPC_SET_ARG1(call->data, arg1);
742 IPC_SET_ARG2(call->data, arg2);
743 IPC_SET_ARG3(call->data, arg3);
744 IPC_SET_ARG4(call->data, arg4);
745
746 /*
747 * To achieve deterministic behavior, zero out arguments that are beyond
748 * the limits of the fast version.
749 */
750 IPC_SET_ARG5(call->data, 0);
751
752 int res = request_preprocess(call, phone);
753
754 if (!res)
755 ipc_call(phone, call);
756 else
757 ipc_backsend_err(phone, call, res);
758
759 return (sysarg_t) call;
760}
761
762/** Make an asynchronous IPC call allowing to transmit the entire payload.
763 *
764 * @param phoneid Phone handle for the call.
765 * @param data Userspace address of call data with the request.
766 *
767 * @return See sys_ipc_call_async_fast().
768 *
769 */
770sysarg_t sys_ipc_call_async_slow(sysarg_t phoneid, ipc_data_t *data)
771{
772 phone_t *phone;
773 if (phone_get(phoneid, &phone) != EOK)
774 return IPC_CALLRET_FATAL;
775
776 if (check_call_limit(phone))
777 return IPC_CALLRET_TEMPORARY;
778
779 call_t *call = ipc_call_alloc(0);
780 int rc = copy_from_uspace(&call->data.args, &data->args,
781 sizeof(call->data.args));
782 if (rc != 0) {
783 ipc_call_free(call);
784 return (sysarg_t) rc;
785 }
786
787 int res = request_preprocess(call, phone);
788
789 if (!res)
790 ipc_call(phone, call);
791 else
792 ipc_backsend_err(phone, call, res);
793
794 return (sysarg_t) call;
795}
796
797/** Forward a received call to another destination
798 *
799 * Common code for both the fast and the slow version.
800 *
801 * @param callid Hash of the call to forward.
802 * @param phoneid Phone handle to use for forwarding.
803 * @param imethod New interface and method to use for the forwarded call.
804 * @param arg1 New value of the first argument for the forwarded call.
805 * @param arg2 New value of the second argument for the forwarded call.
806 * @param arg3 New value of the third argument for the forwarded call.
807 * @param arg4 New value of the fourth argument for the forwarded call.
808 * @param arg5 New value of the fifth argument for the forwarded call.
809 * @param mode Flags that specify mode of the forward operation.
810 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
811 * the function considers only the fast version arguments:
812 * i.e. arg1 and arg2.
813 *
814 * @return 0 on succes, otherwise an error code.
815 *
816 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
817 *
818 */
819static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t phoneid,
820 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
821 sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
822{
823 call_t *call = get_call(callid);
824 if (!call)
825 return ENOENT;
826
827 call->flags |= IPC_CALL_FORWARDED;
828
829 phone_t *phone;
830 if (phone_get(phoneid, &phone) != EOK) {
831 IPC_SET_RETVAL(call->data, EFORWARD);
832 ipc_answer(&TASK->answerbox, call);
833 return ENOENT;
834 }
835
836 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
837 IPC_SET_RETVAL(call->data, EFORWARD);
838 ipc_answer(&TASK->answerbox, call);
839 return EPERM;
840 }
841
842 /*
843 * User space is not allowed to change interface and method of system
844 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
845 * means of imethod, arg1, arg2 and arg3.
846 * If the interface and method is immutable, don't change anything.
847 */
848 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
849 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
850 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
851 phone_dealloc(IPC_GET_ARG5(call->data));
852
853 IPC_SET_ARG1(call->data, imethod);
854 IPC_SET_ARG2(call->data, arg1);
855 IPC_SET_ARG3(call->data, arg2);
856
857 if (slow)
858 IPC_SET_ARG4(call->data, arg3);
859
860 /*
861 * For system methods we deliberately don't
862 * overwrite ARG5.
863 */
864 } else {
865 IPC_SET_IMETHOD(call->data, imethod);
866 IPC_SET_ARG1(call->data, arg1);
867 IPC_SET_ARG2(call->data, arg2);
868 if (slow) {
869 IPC_SET_ARG3(call->data, arg3);
870 IPC_SET_ARG4(call->data, arg4);
871 IPC_SET_ARG5(call->data, arg5);
872 }
873 }
874 }
875
876 return ipc_forward(call, phone, &TASK->answerbox, mode);
877}
878
879/** Forward a received call to another destination - fast version.
880 *
881 * In case the original interface and method is a system method, ARG1, ARG2
882 * and ARG3 are overwritten in the forwarded message with the new method and
883 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
884 * are rewritten with the new interface and method, arg1 and arg2, respectively.
885 * Also note there is a set of immutable methods, for which the new method and
886 * arguments are not set and these values are ignored.
887 *
888 * @param callid Hash of the call to forward.
889 * @param phoneid Phone handle to use for forwarding.
890 * @param imethod New interface and method to use for the forwarded call.
891 * @param arg1 New value of the first argument for the forwarded call.
892 * @param arg2 New value of the second argument for the forwarded call.
893 * @param mode Flags that specify mode of the forward operation.
894 *
895 * @return 0 on succes, otherwise an error code.
896 *
897 */
898sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t phoneid,
899 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
900{
901 return sys_ipc_forward_common(callid, phoneid, imethod, arg1, arg2, 0, 0,
902 0, mode, false);
903}
904
905/** Forward a received call to another destination - slow version.
906 *
907 * This function is the slow verision of the sys_ipc_forward_fast interface.
908 * It can copy all five new arguments and the new interface and method from
909 * the userspace. It naturally extends the functionality of the fast version.
910 * For system methods, it additionally stores the new value of arg3 to ARG4.
911 * For non-system methods, it additionally stores the new value of arg3, arg4
912 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
913 *
914 * @param callid Hash of the call to forward.
915 * @param phoneid Phone handle to use for forwarding.
916 * @param data Userspace address of the new IPC data.
917 * @param mode Flags that specify mode of the forward operation.
918 *
919 * @return 0 on succes, otherwise an error code.
920 *
921 */
922sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t phoneid,
923 ipc_data_t *data, unsigned int mode)
924{
925 ipc_data_t newdata;
926 int rc = copy_from_uspace(&newdata.args, &data->args,
927 sizeof(newdata.args));
928 if (rc != 0)
929 return (sysarg_t) rc;
930
931 return sys_ipc_forward_common(callid, phoneid,
932 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
933 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
934 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
935}
936
937/** Answer an IPC call - fast version.
938 *
939 * This function can handle only two return arguments of payload, but is faster
940 * than the generic sys_ipc_answer().
941 *
942 * @param callid Hash of the call to be answered.
943 * @param retval Return value of the answer.
944 * @param arg1 Service-defined return value.
945 * @param arg2 Service-defined return value.
946 * @param arg3 Service-defined return value.
947 * @param arg4 Service-defined return value.
948 *
949 * @return 0 on success, otherwise an error code.
950 *
951 */
952sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
953 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
954{
955 /* Do not answer notification callids */
956 if (callid & IPC_CALLID_NOTIFICATION)
957 return 0;
958
959 call_t *call = get_call(callid);
960 if (!call)
961 return ENOENT;
962
963 ipc_data_t saved_data;
964 bool saved;
965
966 if (answer_need_old(call)) {
967 memcpy(&saved_data, &call->data, sizeof(call->data));
968 saved = true;
969 } else
970 saved = false;
971
972 IPC_SET_RETVAL(call->data, retval);
973 IPC_SET_ARG1(call->data, arg1);
974 IPC_SET_ARG2(call->data, arg2);
975 IPC_SET_ARG3(call->data, arg3);
976 IPC_SET_ARG4(call->data, arg4);
977
978 /*
979 * To achieve deterministic behavior, zero out arguments that are beyond
980 * the limits of the fast version.
981 */
982 IPC_SET_ARG5(call->data, 0);
983 int rc = answer_preprocess(call, saved ? &saved_data : NULL);
984
985 ipc_answer(&TASK->answerbox, call);
986 return rc;
987}
988
989/** Answer an IPC call.
990 *
991 * @param callid Hash of the call to be answered.
992 * @param data Userspace address of call data with the answer.
993 *
994 * @return 0 on success, otherwise an error code.
995 *
996 */
997sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
998{
999 /* Do not answer notification callids */
1000 if (callid & IPC_CALLID_NOTIFICATION)
1001 return 0;
1002
1003 call_t *call = get_call(callid);
1004 if (!call)
1005 return ENOENT;
1006
1007 ipc_data_t saved_data;
1008 bool saved;
1009
1010 if (answer_need_old(call)) {
1011 memcpy(&saved_data, &call->data, sizeof(call->data));
1012 saved = true;
1013 } else
1014 saved = false;
1015
1016 int rc = copy_from_uspace(&call->data.args, &data->args,
1017 sizeof(call->data.args));
1018 if (rc != 0)
1019 return rc;
1020
1021 rc = answer_preprocess(call, saved ? &saved_data : NULL);
1022
1023 ipc_answer(&TASK->answerbox, call);
1024 return rc;
1025}
1026
1027/** Hang up a phone.
1028 *
1029 * @param Phone handle of the phone to be hung up.
1030 *
1031 * @return 0 on success or an error code.
1032 *
1033 */
1034sysarg_t sys_ipc_hangup(sysarg_t phoneid)
1035{
1036 phone_t *phone;
1037
1038 if (phone_get(phoneid, &phone) != EOK)
1039 return ENOENT;
1040
1041 if (ipc_phone_hangup(phone))
1042 return -1;
1043
1044 return 0;
1045}
1046
1047/** Wait for an incoming IPC call or an answer.
1048 *
1049 * @param calldata Pointer to buffer where the call/answer data is stored.
1050 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
1051 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
1052 * for explanation.
1053 *
1054 * @return Hash of the call.
1055 * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
1056 * call is a notification. IPC_CALLID_ANSWERED denotes an
1057 * answer.
1058 *
1059 */
1060sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
1061 unsigned int flags)
1062{
1063 call_t *call;
1064
1065restart:
1066
1067#ifdef CONFIG_UDEBUG
1068 udebug_stoppable_begin();
1069#endif
1070
1071 call = ipc_wait_for_call(&TASK->answerbox, usec,
1072 flags | SYNCH_FLAGS_INTERRUPTIBLE);
1073
1074#ifdef CONFIG_UDEBUG
1075 udebug_stoppable_end();
1076#endif
1077
1078 if (!call)
1079 return 0;
1080
1081 if (call->flags & IPC_CALL_NOTIF) {
1082 /* Set in_phone_hash to the interrupt counter */
1083 call->data.phone = (void *) call->priv;
1084
1085 STRUCT_TO_USPACE(calldata, &call->data);
1086
1087 ipc_call_free(call);
1088
1089 return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
1090 }
1091
1092 if (call->flags & IPC_CALL_ANSWERED) {
1093 process_answer(call);
1094
1095 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
1096 ipc_call_free(call);
1097 goto restart;
1098 }
1099
1100 STRUCT_TO_USPACE(&calldata->args, &call->data.args);
1101 ipc_call_free(call);
1102
1103 return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
1104 }
1105
1106 if (process_request(&TASK->answerbox, call))
1107 goto restart;
1108
1109 /* Include phone address('id') of the caller in the request,
1110 * copy whole call->data, not only call->data.args */
1111 if (STRUCT_TO_USPACE(calldata, &call->data)) {
1112 /*
1113 * The callee will not receive this call and no one else has
1114 * a chance to answer it. Reply with the EPARTY error code.
1115 */
1116 ipc_data_t saved_data;
1117 bool saved;
1118
1119 if (answer_need_old(call)) {
1120 memcpy(&saved_data, &call->data, sizeof(call->data));
1121 saved = true;
1122 } else
1123 saved = false;
1124
1125 IPC_SET_RETVAL(call->data, EPARTY);
1126 (void) answer_preprocess(call, saved ? &saved_data : NULL);
1127 ipc_answer(&TASK->answerbox, call);
1128 return 0;
1129 }
1130
1131 return (sysarg_t) call;
1132}
1133
1134/** Interrupt one thread from sys_ipc_wait_for_call().
1135 *
1136 */
1137sysarg_t sys_ipc_poke(void)
1138{
1139 waitq_unsleep(&TASK->answerbox.wq);
1140 return EOK;
1141}
1142
1143/** Connect an IRQ handler to a task.
1144 *
1145 * @param inr IRQ number.
1146 * @param devno Device number.
1147 * @param imethod Interface and method to be associated with the notification.
1148 * @param ucode Uspace pointer to the top-half pseudocode.
1149 *
1150 * @return EPERM or a return code returned by ipc_irq_register().
1151 *
1152 */
1153sysarg_t sys_irq_register(inr_t inr, devno_t devno, sysarg_t imethod,
1154 irq_code_t *ucode)
1155{
1156 if (!(cap_get(TASK) & CAP_IRQ_REG))
1157 return EPERM;
1158
1159 return ipc_irq_register(&TASK->answerbox, inr, devno, imethod, ucode);
1160}
1161
1162/** Disconnect an IRQ handler from a task.
1163 *
1164 * @param inr IRQ number.
1165 * @param devno Device number.
1166 *
1167 * @return Zero on success or EPERM on error.
1168 *
1169 */
1170sysarg_t sys_irq_unregister(inr_t inr, devno_t devno)
1171{
1172 if (!(cap_get(TASK) & CAP_IRQ_REG))
1173 return EPERM;
1174
1175 ipc_irq_unregister(&TASK->answerbox, inr, devno);
1176
1177 return 0;
1178}
1179
1180#ifdef __32_BITS__
1181
1182/** Syscall connect to a task by ID (32 bits)
1183 *
1184 * @return Phone id on success, or negative error code.
1185 *
1186 */
1187sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
1188{
1189#ifdef CONFIG_UDEBUG
1190 sysarg64_t taskid;
1191 int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
1192 if (rc != 0)
1193 return (sysarg_t) rc;
1194
1195 return ipc_connect_kbox((task_id_t) taskid);
1196#else
1197 return (sysarg_t) ENOTSUP;
1198#endif
1199}
1200
1201#endif /* __32_BITS__ */
1202
1203#ifdef __64_BITS__
1204
1205/** Syscall connect to a task by ID (64 bits)
1206 *
1207 * @return Phone id on success, or negative error code.
1208 *
1209 */
1210sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
1211{
1212#ifdef CONFIG_UDEBUG
1213 return ipc_connect_kbox((task_id_t) taskid);
1214#else
1215 return (sysarg_t) ENOTSUP;
1216#endif
1217}
1218
1219#endif /* __64_BITS__ */
1220
1221/** @}
1222 */
Note: See TracBrowser for help on using the repository browser.