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

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

Give each system IPC method its dedicated request process hook.

  • Property mode set to 100644
File size: 32.9 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
493static int r_preprocess_m_connection_clone(call_t *call, phone_t *phone)
494{
495 phone_t *cloned_phone;
496
497 if (phone_get(IPC_GET_ARG1(call->data), &cloned_phone) != EOK)
498 return ENOENT;
499
500 phones_lock(cloned_phone, phone);
501
502 if ((cloned_phone->state != IPC_PHONE_CONNECTED) ||
503 phone->state != IPC_PHONE_CONNECTED) {
504 phones_unlock(cloned_phone, phone);
505 return EINVAL;
506 }
507
508 /*
509 * We can be pretty sure now that both tasks exist and we are
510 * connected to them. As we continue to hold the phone locks,
511 * we are effectively preventing them from finishing their
512 * potential cleanup.
513 *
514 */
515 int newphid = phone_alloc(phone->callee->task);
516 if (newphid < 0) {
517 phones_unlock(cloned_phone, phone);
518 return ELIMIT;
519 }
520
521 ipc_phone_connect(&phone->callee->task->phones[newphid],
522 cloned_phone->callee);
523 phones_unlock(cloned_phone, phone);
524
525 /* Set the new phone for the callee. */
526 IPC_SET_ARG1(call->data, newphid);
527
528 return EOK;
529}
530
531static int r_preprocess_m_clone_establish(call_t *call, phone_t *phone)
532{
533 IPC_SET_ARG5(call->data, (sysarg_t) phone);
534
535 return EOK;
536}
537
538static int r_preprocess_m_connect_me_to(call_t *call, phone_t *phone)
539{
540 int newphid = phone_alloc(TASK);
541
542 if (newphid < 0)
543 return ELIMIT;
544
545 /* Set arg5 for server */
546 IPC_SET_ARG5(call->data, (sysarg_t) &TASK->phones[newphid]);
547 call->flags |= IPC_CALL_CONN_ME_TO;
548 call->priv = newphid;
549
550 return EOK;
551}
552
553static int r_preprocess_m_share_out(call_t *call, phone_t *phone)
554{
555 size_t size = as_area_get_size(IPC_GET_ARG1(call->data));
556
557 if (!size)
558 return EPERM;
559 IPC_SET_ARG2(call->data, size);
560
561 return EOK;
562}
563
564static int r_preprocess_m_data_read(call_t *call, phone_t *phone)
565{
566 size_t size = IPC_GET_ARG2(call->data);
567
568 if (size > DATA_XFER_LIMIT) {
569 int flags = IPC_GET_ARG3(call->data);
570
571 if (flags & IPC_XF_RESTRICT)
572 IPC_SET_ARG2(call->data, DATA_XFER_LIMIT);
573 else
574 return ELIMIT;
575 }
576
577 return EOK;
578}
579
580static int r_preprocess_m_data_write(call_t *call, phone_t *phone)
581{
582 uintptr_t src = IPC_GET_ARG1(call->data);
583 size_t size = IPC_GET_ARG2(call->data);
584
585 if (size > DATA_XFER_LIMIT) {
586 int flags = IPC_GET_ARG3(call->data);
587
588 if (flags & IPC_XF_RESTRICT) {
589 size = DATA_XFER_LIMIT;
590 IPC_SET_ARG2(call->data, size);
591 } else
592 return ELIMIT;
593 }
594
595 call->buffer = (uint8_t *) malloc(size, 0);
596 int rc = copy_from_uspace(call->buffer, (void *) src, size);
597 if (rc != 0) {
598 free(call->buffer);
599 return rc;
600 }
601
602 return EOK;
603}
604
605static int r_preprocess_m_state_change_authorize(call_t *call, phone_t *phone)
606{
607 phone_t *sender_phone;
608 task_t *other_task_s;
609
610 if (phone_get(IPC_GET_ARG5(call->data), &sender_phone) != EOK)
611 return ENOENT;
612
613 mutex_lock(&sender_phone->lock);
614 if (sender_phone->state != IPC_PHONE_CONNECTED) {
615 mutex_unlock(&sender_phone->lock);
616 return EINVAL;
617 }
618
619 other_task_s = sender_phone->callee->task;
620
621 mutex_unlock(&sender_phone->lock);
622
623 /* Remember the third party task hash. */
624 IPC_SET_ARG5(call->data, (sysarg_t) other_task_s);
625
626 return EOK;
627}
628
629/** Called before the request is sent.
630 *
631 * @param call Call structure with the request.
632 * @param phone Phone that the call will be sent through.
633 *
634 * @return Return 0 on success, ELIMIT or EPERM on error.
635 *
636 */
637static int request_preprocess(call_t *call, phone_t *phone)
638{
639 int rc = EOK;
640
641 switch (IPC_GET_IMETHOD(call->data)) {
642 case IPC_M_CONNECTION_CLONE:
643 rc = r_preprocess_m_connection_clone(call, phone);
644 break;
645 case IPC_M_CLONE_ESTABLISH:
646 rc = r_preprocess_m_clone_establish(call, phone);
647 break;
648 case IPC_M_CONNECT_ME_TO:
649 rc = r_preprocess_m_connect_me_to(call, phone);
650 break;
651 case IPC_M_SHARE_OUT:
652 rc = r_preprocess_m_share_out(call, phone);
653 break;
654 case IPC_M_DATA_READ:
655 rc = r_preprocess_m_data_read(call, phone);
656 break;
657 case IPC_M_DATA_WRITE:
658 rc = r_preprocess_m_data_write(call, phone);
659 break;
660 case IPC_M_STATE_CHANGE_AUTHORIZE:
661 rc = r_preprocess_m_state_change_authorize(call, phone);
662 break;
663#ifdef CONFIG_UDEBUG
664 case IPC_M_DEBUG:
665 rc = udebug_request_preprocess(call, phone);
666 break;
667#endif
668 default:
669 break;
670 }
671
672 return rc;
673}
674
675/*******************************************************************************
676 * Functions called to process received call/answer before passing it to uspace.
677 *******************************************************************************/
678
679/** Do basic kernel processing of received call answer.
680 *
681 * @param call Call structure with the answer.
682 *
683 */
684static void process_answer(call_t *call)
685{
686 if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
687 (call->flags & IPC_CALL_FORWARDED))
688 IPC_SET_RETVAL(call->data, EFORWARD);
689
690 if (call->flags & IPC_CALL_CONN_ME_TO) {
691 if (IPC_GET_RETVAL(call->data))
692 phone_dealloc(call->priv);
693 else
694 IPC_SET_ARG5(call->data, call->priv);
695 }
696
697 if (call->buffer) {
698 /*
699 * This must be an affirmative answer to IPC_M_DATA_READ
700 * or IPC_M_DEBUG/UDEBUG_M_MEM_READ...
701 *
702 */
703 uintptr_t dst = IPC_GET_ARG1(call->data);
704 size_t size = IPC_GET_ARG2(call->data);
705 int rc = copy_to_uspace((void *) dst, call->buffer, size);
706 if (rc)
707 IPC_SET_RETVAL(call->data, rc);
708 free(call->buffer);
709 call->buffer = NULL;
710 }
711}
712
713static int r_process_m_connect_to_me(answerbox_t *box, call_t *call)
714{
715 int phoneid = phone_alloc(TASK);
716
717 if (phoneid < 0) { /* Failed to allocate phone */
718 IPC_SET_RETVAL(call->data, ELIMIT);
719 ipc_answer(box, call);
720 return -1;
721 }
722
723 IPC_SET_ARG5(call->data, phoneid);
724
725 return EOK;
726}
727
728static int r_process_m_debug(answerbox_t *box, call_t *call)
729{
730 return -1;
731}
732
733/** Do basic kernel processing of received call request.
734 *
735 * @param box Destination answerbox structure.
736 * @param call Call structure with the request.
737 *
738 * @return 0 if the call should be passed to userspace.
739 * @return -1 if the call should be ignored.
740 *
741 */
742static int process_request(answerbox_t *box, call_t *call)
743{
744 int rc = EOK;
745
746 switch (IPC_GET_IMETHOD(call->data)) {
747 case IPC_M_CONNECT_TO_ME:
748 rc = r_process_m_connect_to_me(box, call);
749 break;
750 case IPC_M_DEBUG:
751 rc = r_process_m_debug(box, call);
752 break;
753 default:
754 break;
755 }
756
757 return rc;
758}
759
760/** Check that the task did not exceed the allowed limit of asynchronous calls
761 * made over a phone.
762 *
763 * @param phone Phone to check the limit against.
764 *
765 * @return 0 if limit not reached or -1 if limit exceeded.
766 *
767 */
768static int check_call_limit(phone_t *phone)
769{
770 if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
771 return -1;
772
773 return 0;
774}
775
776/** Make a fast asynchronous call over IPC.
777 *
778 * This function can only handle four arguments of payload, but is faster than
779 * the generic function sys_ipc_call_async_slow().
780 *
781 * @param phoneid Phone handle for the call.
782 * @param imethod Interface and method of the call.
783 * @param arg1 Service-defined payload argument.
784 * @param arg2 Service-defined payload argument.
785 * @param arg3 Service-defined payload argument.
786 * @param arg4 Service-defined payload argument.
787 *
788 * @return Call hash on success.
789 * @return IPC_CALLRET_FATAL in case of a fatal error.
790 * @return IPC_CALLRET_TEMPORARY if there are too many pending
791 * asynchronous requests; answers should be handled first.
792 *
793 */
794sysarg_t sys_ipc_call_async_fast(sysarg_t phoneid, sysarg_t imethod,
795 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
796{
797 phone_t *phone;
798 if (phone_get(phoneid, &phone) != EOK)
799 return IPC_CALLRET_FATAL;
800
801 if (check_call_limit(phone))
802 return IPC_CALLRET_TEMPORARY;
803
804 call_t *call = ipc_call_alloc(0);
805 IPC_SET_IMETHOD(call->data, imethod);
806 IPC_SET_ARG1(call->data, arg1);
807 IPC_SET_ARG2(call->data, arg2);
808 IPC_SET_ARG3(call->data, arg3);
809 IPC_SET_ARG4(call->data, arg4);
810
811 /*
812 * To achieve deterministic behavior, zero out arguments that are beyond
813 * the limits of the fast version.
814 */
815 IPC_SET_ARG5(call->data, 0);
816
817 int res = request_preprocess(call, phone);
818
819 if (!res)
820 ipc_call(phone, call);
821 else
822 ipc_backsend_err(phone, call, res);
823
824 return (sysarg_t) call;
825}
826
827/** Make an asynchronous IPC call allowing to transmit the entire payload.
828 *
829 * @param phoneid Phone handle for the call.
830 * @param data Userspace address of call data with the request.
831 *
832 * @return See sys_ipc_call_async_fast().
833 *
834 */
835sysarg_t sys_ipc_call_async_slow(sysarg_t phoneid, ipc_data_t *data)
836{
837 phone_t *phone;
838 if (phone_get(phoneid, &phone) != EOK)
839 return IPC_CALLRET_FATAL;
840
841 if (check_call_limit(phone))
842 return IPC_CALLRET_TEMPORARY;
843
844 call_t *call = ipc_call_alloc(0);
845 int rc = copy_from_uspace(&call->data.args, &data->args,
846 sizeof(call->data.args));
847 if (rc != 0) {
848 ipc_call_free(call);
849 return (sysarg_t) rc;
850 }
851
852 int res = request_preprocess(call, phone);
853
854 if (!res)
855 ipc_call(phone, call);
856 else
857 ipc_backsend_err(phone, call, res);
858
859 return (sysarg_t) call;
860}
861
862/** Forward a received call to another destination
863 *
864 * Common code for both the fast and the slow version.
865 *
866 * @param callid Hash of the call to forward.
867 * @param phoneid Phone handle to use for forwarding.
868 * @param imethod New interface and method to use for the forwarded call.
869 * @param arg1 New value of the first argument for the forwarded call.
870 * @param arg2 New value of the second argument for the forwarded call.
871 * @param arg3 New value of the third argument for the forwarded call.
872 * @param arg4 New value of the fourth argument for the forwarded call.
873 * @param arg5 New value of the fifth argument for the forwarded call.
874 * @param mode Flags that specify mode of the forward operation.
875 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
876 * the function considers only the fast version arguments:
877 * i.e. arg1 and arg2.
878 *
879 * @return 0 on succes, otherwise an error code.
880 *
881 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
882 *
883 */
884static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t phoneid,
885 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
886 sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
887{
888 call_t *call = get_call(callid);
889 if (!call)
890 return ENOENT;
891
892 call->flags |= IPC_CALL_FORWARDED;
893
894 phone_t *phone;
895 if (phone_get(phoneid, &phone) != EOK) {
896 IPC_SET_RETVAL(call->data, EFORWARD);
897 ipc_answer(&TASK->answerbox, call);
898 return ENOENT;
899 }
900
901 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
902 IPC_SET_RETVAL(call->data, EFORWARD);
903 ipc_answer(&TASK->answerbox, call);
904 return EPERM;
905 }
906
907 /*
908 * User space is not allowed to change interface and method of system
909 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
910 * means of imethod, arg1, arg2 and arg3.
911 * If the interface and method is immutable, don't change anything.
912 */
913 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
914 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
915 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
916 phone_dealloc(IPC_GET_ARG5(call->data));
917
918 IPC_SET_ARG1(call->data, imethod);
919 IPC_SET_ARG2(call->data, arg1);
920 IPC_SET_ARG3(call->data, arg2);
921
922 if (slow)
923 IPC_SET_ARG4(call->data, arg3);
924
925 /*
926 * For system methods we deliberately don't
927 * overwrite ARG5.
928 */
929 } else {
930 IPC_SET_IMETHOD(call->data, imethod);
931 IPC_SET_ARG1(call->data, arg1);
932 IPC_SET_ARG2(call->data, arg2);
933 if (slow) {
934 IPC_SET_ARG3(call->data, arg3);
935 IPC_SET_ARG4(call->data, arg4);
936 IPC_SET_ARG5(call->data, arg5);
937 }
938 }
939 }
940
941 return ipc_forward(call, phone, &TASK->answerbox, mode);
942}
943
944/** Forward a received call to another destination - fast version.
945 *
946 * In case the original interface and method is a system method, ARG1, ARG2
947 * and ARG3 are overwritten in the forwarded message with the new method and
948 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
949 * are rewritten with the new interface and method, arg1 and arg2, respectively.
950 * Also note there is a set of immutable methods, for which the new method and
951 * arguments are not set and these values are ignored.
952 *
953 * @param callid Hash of the call to forward.
954 * @param phoneid Phone handle to use for forwarding.
955 * @param imethod New interface and method to use for the forwarded call.
956 * @param arg1 New value of the first argument for the forwarded call.
957 * @param arg2 New value of the second argument for the forwarded call.
958 * @param mode Flags that specify mode of the forward operation.
959 *
960 * @return 0 on succes, otherwise an error code.
961 *
962 */
963sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t phoneid,
964 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
965{
966 return sys_ipc_forward_common(callid, phoneid, imethod, arg1, arg2, 0, 0,
967 0, mode, false);
968}
969
970/** Forward a received call to another destination - slow version.
971 *
972 * This function is the slow verision of the sys_ipc_forward_fast interface.
973 * It can copy all five new arguments and the new interface and method from
974 * the userspace. It naturally extends the functionality of the fast version.
975 * For system methods, it additionally stores the new value of arg3 to ARG4.
976 * For non-system methods, it additionally stores the new value of arg3, arg4
977 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
978 *
979 * @param callid Hash of the call to forward.
980 * @param phoneid Phone handle to use for forwarding.
981 * @param data Userspace address of the new IPC data.
982 * @param mode Flags that specify mode of the forward operation.
983 *
984 * @return 0 on succes, otherwise an error code.
985 *
986 */
987sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t phoneid,
988 ipc_data_t *data, unsigned int mode)
989{
990 ipc_data_t newdata;
991 int rc = copy_from_uspace(&newdata.args, &data->args,
992 sizeof(newdata.args));
993 if (rc != 0)
994 return (sysarg_t) rc;
995
996 return sys_ipc_forward_common(callid, phoneid,
997 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
998 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
999 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
1000}
1001
1002/** Answer an IPC call - fast version.
1003 *
1004 * This function can handle only two return arguments of payload, but is faster
1005 * than the generic sys_ipc_answer().
1006 *
1007 * @param callid Hash of the call to be answered.
1008 * @param retval Return value of the answer.
1009 * @param arg1 Service-defined return value.
1010 * @param arg2 Service-defined return value.
1011 * @param arg3 Service-defined return value.
1012 * @param arg4 Service-defined return value.
1013 *
1014 * @return 0 on success, otherwise an error code.
1015 *
1016 */
1017sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
1018 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1019{
1020 /* Do not answer notification callids */
1021 if (callid & IPC_CALLID_NOTIFICATION)
1022 return 0;
1023
1024 call_t *call = get_call(callid);
1025 if (!call)
1026 return ENOENT;
1027
1028 ipc_data_t saved_data;
1029 bool saved;
1030
1031 if (answer_need_old(call)) {
1032 memcpy(&saved_data, &call->data, sizeof(call->data));
1033 saved = true;
1034 } else
1035 saved = false;
1036
1037 IPC_SET_RETVAL(call->data, retval);
1038 IPC_SET_ARG1(call->data, arg1);
1039 IPC_SET_ARG2(call->data, arg2);
1040 IPC_SET_ARG3(call->data, arg3);
1041 IPC_SET_ARG4(call->data, arg4);
1042
1043 /*
1044 * To achieve deterministic behavior, zero out arguments that are beyond
1045 * the limits of the fast version.
1046 */
1047 IPC_SET_ARG5(call->data, 0);
1048 int rc = answer_preprocess(call, saved ? &saved_data : NULL);
1049
1050 ipc_answer(&TASK->answerbox, call);
1051 return rc;
1052}
1053
1054/** Answer an IPC call.
1055 *
1056 * @param callid Hash of the call to be answered.
1057 * @param data Userspace address of call data with the answer.
1058 *
1059 * @return 0 on success, otherwise an error code.
1060 *
1061 */
1062sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
1063{
1064 /* Do not answer notification callids */
1065 if (callid & IPC_CALLID_NOTIFICATION)
1066 return 0;
1067
1068 call_t *call = get_call(callid);
1069 if (!call)
1070 return ENOENT;
1071
1072 ipc_data_t saved_data;
1073 bool saved;
1074
1075 if (answer_need_old(call)) {
1076 memcpy(&saved_data, &call->data, sizeof(call->data));
1077 saved = true;
1078 } else
1079 saved = false;
1080
1081 int rc = copy_from_uspace(&call->data.args, &data->args,
1082 sizeof(call->data.args));
1083 if (rc != 0)
1084 return rc;
1085
1086 rc = answer_preprocess(call, saved ? &saved_data : NULL);
1087
1088 ipc_answer(&TASK->answerbox, call);
1089 return rc;
1090}
1091
1092/** Hang up a phone.
1093 *
1094 * @param Phone handle of the phone to be hung up.
1095 *
1096 * @return 0 on success or an error code.
1097 *
1098 */
1099sysarg_t sys_ipc_hangup(sysarg_t phoneid)
1100{
1101 phone_t *phone;
1102
1103 if (phone_get(phoneid, &phone) != EOK)
1104 return ENOENT;
1105
1106 if (ipc_phone_hangup(phone))
1107 return -1;
1108
1109 return 0;
1110}
1111
1112/** Wait for an incoming IPC call or an answer.
1113 *
1114 * @param calldata Pointer to buffer where the call/answer data is stored.
1115 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
1116 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
1117 * for explanation.
1118 *
1119 * @return Hash of the call.
1120 * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
1121 * call is a notification. IPC_CALLID_ANSWERED denotes an
1122 * answer.
1123 *
1124 */
1125sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
1126 unsigned int flags)
1127{
1128 call_t *call;
1129
1130restart:
1131
1132#ifdef CONFIG_UDEBUG
1133 udebug_stoppable_begin();
1134#endif
1135
1136 call = ipc_wait_for_call(&TASK->answerbox, usec,
1137 flags | SYNCH_FLAGS_INTERRUPTIBLE);
1138
1139#ifdef CONFIG_UDEBUG
1140 udebug_stoppable_end();
1141#endif
1142
1143 if (!call)
1144 return 0;
1145
1146 if (call->flags & IPC_CALL_NOTIF) {
1147 /* Set in_phone_hash to the interrupt counter */
1148 call->data.phone = (void *) call->priv;
1149
1150 STRUCT_TO_USPACE(calldata, &call->data);
1151
1152 ipc_call_free(call);
1153
1154 return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
1155 }
1156
1157 if (call->flags & IPC_CALL_ANSWERED) {
1158 process_answer(call);
1159
1160 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
1161 ipc_call_free(call);
1162 goto restart;
1163 }
1164
1165 STRUCT_TO_USPACE(&calldata->args, &call->data.args);
1166 ipc_call_free(call);
1167
1168 return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
1169 }
1170
1171 if (process_request(&TASK->answerbox, call))
1172 goto restart;
1173
1174 /* Include phone address('id') of the caller in the request,
1175 * copy whole call->data, not only call->data.args */
1176 if (STRUCT_TO_USPACE(calldata, &call->data)) {
1177 /*
1178 * The callee will not receive this call and no one else has
1179 * a chance to answer it. Reply with the EPARTY error code.
1180 */
1181 ipc_data_t saved_data;
1182 bool saved;
1183
1184 if (answer_need_old(call)) {
1185 memcpy(&saved_data, &call->data, sizeof(call->data));
1186 saved = true;
1187 } else
1188 saved = false;
1189
1190 IPC_SET_RETVAL(call->data, EPARTY);
1191 (void) answer_preprocess(call, saved ? &saved_data : NULL);
1192 ipc_answer(&TASK->answerbox, call);
1193 return 0;
1194 }
1195
1196 return (sysarg_t) call;
1197}
1198
1199/** Interrupt one thread from sys_ipc_wait_for_call().
1200 *
1201 */
1202sysarg_t sys_ipc_poke(void)
1203{
1204 waitq_unsleep(&TASK->answerbox.wq);
1205 return EOK;
1206}
1207
1208/** Connect an IRQ handler to a task.
1209 *
1210 * @param inr IRQ number.
1211 * @param devno Device number.
1212 * @param imethod Interface and method to be associated with the notification.
1213 * @param ucode Uspace pointer to the top-half pseudocode.
1214 *
1215 * @return EPERM or a return code returned by ipc_irq_register().
1216 *
1217 */
1218sysarg_t sys_irq_register(inr_t inr, devno_t devno, sysarg_t imethod,
1219 irq_code_t *ucode)
1220{
1221 if (!(cap_get(TASK) & CAP_IRQ_REG))
1222 return EPERM;
1223
1224 return ipc_irq_register(&TASK->answerbox, inr, devno, imethod, ucode);
1225}
1226
1227/** Disconnect an IRQ handler from a task.
1228 *
1229 * @param inr IRQ number.
1230 * @param devno Device number.
1231 *
1232 * @return Zero on success or EPERM on error.
1233 *
1234 */
1235sysarg_t sys_irq_unregister(inr_t inr, devno_t devno)
1236{
1237 if (!(cap_get(TASK) & CAP_IRQ_REG))
1238 return EPERM;
1239
1240 ipc_irq_unregister(&TASK->answerbox, inr, devno);
1241
1242 return 0;
1243}
1244
1245#ifdef __32_BITS__
1246
1247/** Syscall connect to a task by ID (32 bits)
1248 *
1249 * @return Phone id on success, or negative error code.
1250 *
1251 */
1252sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
1253{
1254#ifdef CONFIG_UDEBUG
1255 sysarg64_t taskid;
1256 int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
1257 if (rc != 0)
1258 return (sysarg_t) rc;
1259
1260 return ipc_connect_kbox((task_id_t) taskid);
1261#else
1262 return (sysarg_t) ENOTSUP;
1263#endif
1264}
1265
1266#endif /* __32_BITS__ */
1267
1268#ifdef __64_BITS__
1269
1270/** Syscall connect to a task by ID (64 bits)
1271 *
1272 * @return Phone id on success, or negative error code.
1273 *
1274 */
1275sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
1276{
1277#ifdef CONFIG_UDEBUG
1278 return ipc_connect_kbox((task_id_t) taskid);
1279#else
1280 return (sysarg_t) ENOTSUP;
1281#endif
1282}
1283
1284#endif /* __64_BITS__ */
1285
1286/** @}
1287 */
Note: See TracBrowser for help on using the repository browser.