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

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

Give each system IPC method its dedicated request preprocess hook.

  • Property mode set to 100644
File size: 32.7 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup 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
713/** Do basic kernel processing of received call request.
714 *
715 * @param box Destination answerbox structure.
716 * @param call Call structure with the request.
717 *
718 * @return 0 if the call should be passed to userspace.
719 * @return -1 if the call should be ignored.
720 *
721 */
722static int process_request(answerbox_t *box, call_t *call)
723{
724 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME) {
725 int phoneid = phone_alloc(TASK);
726 if (phoneid < 0) { /* Failed to allocate phone */
727 IPC_SET_RETVAL(call->data, ELIMIT);
728 ipc_answer(box, call);
729 return -1;
730 }
731
732 IPC_SET_ARG5(call->data, phoneid);
733 }
734
735 switch (IPC_GET_IMETHOD(call->data)) {
736 case IPC_M_DEBUG:
737 return -1;
738 default:
739 break;
740 }
741
742 return 0;
743}
744
745/** Check that the task did not exceed the allowed limit of asynchronous calls
746 * made over a phone.
747 *
748 * @param phone Phone to check the limit against.
749 *
750 * @return 0 if limit not reached or -1 if limit exceeded.
751 *
752 */
753static int check_call_limit(phone_t *phone)
754{
755 if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
756 return -1;
757
758 return 0;
759}
760
761/** Make a fast asynchronous call over IPC.
762 *
763 * This function can only handle four arguments of payload, but is faster than
764 * the generic function sys_ipc_call_async_slow().
765 *
766 * @param phoneid Phone handle for the call.
767 * @param imethod Interface and method of the call.
768 * @param arg1 Service-defined payload argument.
769 * @param arg2 Service-defined payload argument.
770 * @param arg3 Service-defined payload argument.
771 * @param arg4 Service-defined payload argument.
772 *
773 * @return Call hash on success.
774 * @return IPC_CALLRET_FATAL in case of a fatal error.
775 * @return IPC_CALLRET_TEMPORARY if there are too many pending
776 * asynchronous requests; answers should be handled first.
777 *
778 */
779sysarg_t sys_ipc_call_async_fast(sysarg_t phoneid, sysarg_t imethod,
780 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
781{
782 phone_t *phone;
783 if (phone_get(phoneid, &phone) != EOK)
784 return IPC_CALLRET_FATAL;
785
786 if (check_call_limit(phone))
787 return IPC_CALLRET_TEMPORARY;
788
789 call_t *call = ipc_call_alloc(0);
790 IPC_SET_IMETHOD(call->data, imethod);
791 IPC_SET_ARG1(call->data, arg1);
792 IPC_SET_ARG2(call->data, arg2);
793 IPC_SET_ARG3(call->data, arg3);
794 IPC_SET_ARG4(call->data, arg4);
795
796 /*
797 * To achieve deterministic behavior, zero out arguments that are beyond
798 * the limits of the fast version.
799 */
800 IPC_SET_ARG5(call->data, 0);
801
802 int res = request_preprocess(call, phone);
803
804 if (!res)
805 ipc_call(phone, call);
806 else
807 ipc_backsend_err(phone, call, res);
808
809 return (sysarg_t) call;
810}
811
812/** Make an asynchronous IPC call allowing to transmit the entire payload.
813 *
814 * @param phoneid Phone handle for the call.
815 * @param data Userspace address of call data with the request.
816 *
817 * @return See sys_ipc_call_async_fast().
818 *
819 */
820sysarg_t sys_ipc_call_async_slow(sysarg_t phoneid, ipc_data_t *data)
821{
822 phone_t *phone;
823 if (phone_get(phoneid, &phone) != EOK)
824 return IPC_CALLRET_FATAL;
825
826 if (check_call_limit(phone))
827 return IPC_CALLRET_TEMPORARY;
828
829 call_t *call = ipc_call_alloc(0);
830 int rc = copy_from_uspace(&call->data.args, &data->args,
831 sizeof(call->data.args));
832 if (rc != 0) {
833 ipc_call_free(call);
834 return (sysarg_t) rc;
835 }
836
837 int res = request_preprocess(call, phone);
838
839 if (!res)
840 ipc_call(phone, call);
841 else
842 ipc_backsend_err(phone, call, res);
843
844 return (sysarg_t) call;
845}
846
847/** Forward a received call to another destination
848 *
849 * Common code for both the fast and the slow version.
850 *
851 * @param callid Hash of the call to forward.
852 * @param phoneid Phone handle to use for forwarding.
853 * @param imethod New interface and method to use for the forwarded call.
854 * @param arg1 New value of the first argument for the forwarded call.
855 * @param arg2 New value of the second argument for the forwarded call.
856 * @param arg3 New value of the third argument for the forwarded call.
857 * @param arg4 New value of the fourth argument for the forwarded call.
858 * @param arg5 New value of the fifth argument for the forwarded call.
859 * @param mode Flags that specify mode of the forward operation.
860 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
861 * the function considers only the fast version arguments:
862 * i.e. arg1 and arg2.
863 *
864 * @return 0 on succes, otherwise an error code.
865 *
866 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
867 *
868 */
869static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t phoneid,
870 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
871 sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
872{
873 call_t *call = get_call(callid);
874 if (!call)
875 return ENOENT;
876
877 call->flags |= IPC_CALL_FORWARDED;
878
879 phone_t *phone;
880 if (phone_get(phoneid, &phone) != EOK) {
881 IPC_SET_RETVAL(call->data, EFORWARD);
882 ipc_answer(&TASK->answerbox, call);
883 return ENOENT;
884 }
885
886 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
887 IPC_SET_RETVAL(call->data, EFORWARD);
888 ipc_answer(&TASK->answerbox, call);
889 return EPERM;
890 }
891
892 /*
893 * User space is not allowed to change interface and method of system
894 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
895 * means of imethod, arg1, arg2 and arg3.
896 * If the interface and method is immutable, don't change anything.
897 */
898 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
899 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
900 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
901 phone_dealloc(IPC_GET_ARG5(call->data));
902
903 IPC_SET_ARG1(call->data, imethod);
904 IPC_SET_ARG2(call->data, arg1);
905 IPC_SET_ARG3(call->data, arg2);
906
907 if (slow)
908 IPC_SET_ARG4(call->data, arg3);
909
910 /*
911 * For system methods we deliberately don't
912 * overwrite ARG5.
913 */
914 } else {
915 IPC_SET_IMETHOD(call->data, imethod);
916 IPC_SET_ARG1(call->data, arg1);
917 IPC_SET_ARG2(call->data, arg2);
918 if (slow) {
919 IPC_SET_ARG3(call->data, arg3);
920 IPC_SET_ARG4(call->data, arg4);
921 IPC_SET_ARG5(call->data, arg5);
922 }
923 }
924 }
925
926 return ipc_forward(call, phone, &TASK->answerbox, mode);
927}
928
929/** Forward a received call to another destination - fast version.
930 *
931 * In case the original interface and method is a system method, ARG1, ARG2
932 * and ARG3 are overwritten in the forwarded message with the new method and
933 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
934 * are rewritten with the new interface and method, arg1 and arg2, respectively.
935 * Also note there is a set of immutable methods, for which the new method and
936 * arguments are not set and these values are ignored.
937 *
938 * @param callid Hash of the call to forward.
939 * @param phoneid Phone handle to use for forwarding.
940 * @param imethod New interface and method to use for the forwarded call.
941 * @param arg1 New value of the first argument for the forwarded call.
942 * @param arg2 New value of the second argument for the forwarded call.
943 * @param mode Flags that specify mode of the forward operation.
944 *
945 * @return 0 on succes, otherwise an error code.
946 *
947 */
948sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t phoneid,
949 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
950{
951 return sys_ipc_forward_common(callid, phoneid, imethod, arg1, arg2, 0, 0,
952 0, mode, false);
953}
954
955/** Forward a received call to another destination - slow version.
956 *
957 * This function is the slow verision of the sys_ipc_forward_fast interface.
958 * It can copy all five new arguments and the new interface and method from
959 * the userspace. It naturally extends the functionality of the fast version.
960 * For system methods, it additionally stores the new value of arg3 to ARG4.
961 * For non-system methods, it additionally stores the new value of arg3, arg4
962 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
963 *
964 * @param callid Hash of the call to forward.
965 * @param phoneid Phone handle to use for forwarding.
966 * @param data Userspace address of the new IPC data.
967 * @param mode Flags that specify mode of the forward operation.
968 *
969 * @return 0 on succes, otherwise an error code.
970 *
971 */
972sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t phoneid,
973 ipc_data_t *data, unsigned int mode)
974{
975 ipc_data_t newdata;
976 int rc = copy_from_uspace(&newdata.args, &data->args,
977 sizeof(newdata.args));
978 if (rc != 0)
979 return (sysarg_t) rc;
980
981 return sys_ipc_forward_common(callid, phoneid,
982 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
983 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
984 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
985}
986
987/** Answer an IPC call - fast version.
988 *
989 * This function can handle only two return arguments of payload, but is faster
990 * than the generic sys_ipc_answer().
991 *
992 * @param callid Hash of the call to be answered.
993 * @param retval Return value of the answer.
994 * @param arg1 Service-defined return value.
995 * @param arg2 Service-defined return value.
996 * @param arg3 Service-defined return value.
997 * @param arg4 Service-defined return value.
998 *
999 * @return 0 on success, otherwise an error code.
1000 *
1001 */
1002sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
1003 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1004{
1005 /* Do not answer notification callids */
1006 if (callid & IPC_CALLID_NOTIFICATION)
1007 return 0;
1008
1009 call_t *call = get_call(callid);
1010 if (!call)
1011 return ENOENT;
1012
1013 ipc_data_t saved_data;
1014 bool saved;
1015
1016 if (answer_need_old(call)) {
1017 memcpy(&saved_data, &call->data, sizeof(call->data));
1018 saved = true;
1019 } else
1020 saved = false;
1021
1022 IPC_SET_RETVAL(call->data, retval);
1023 IPC_SET_ARG1(call->data, arg1);
1024 IPC_SET_ARG2(call->data, arg2);
1025 IPC_SET_ARG3(call->data, arg3);
1026 IPC_SET_ARG4(call->data, arg4);
1027
1028 /*
1029 * To achieve deterministic behavior, zero out arguments that are beyond
1030 * the limits of the fast version.
1031 */
1032 IPC_SET_ARG5(call->data, 0);
1033 int rc = answer_preprocess(call, saved ? &saved_data : NULL);
1034
1035 ipc_answer(&TASK->answerbox, call);
1036 return rc;
1037}
1038
1039/** Answer an IPC call.
1040 *
1041 * @param callid Hash of the call to be answered.
1042 * @param data Userspace address of call data with the answer.
1043 *
1044 * @return 0 on success, otherwise an error code.
1045 *
1046 */
1047sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
1048{
1049 /* Do not answer notification callids */
1050 if (callid & IPC_CALLID_NOTIFICATION)
1051 return 0;
1052
1053 call_t *call = get_call(callid);
1054 if (!call)
1055 return ENOENT;
1056
1057 ipc_data_t saved_data;
1058 bool saved;
1059
1060 if (answer_need_old(call)) {
1061 memcpy(&saved_data, &call->data, sizeof(call->data));
1062 saved = true;
1063 } else
1064 saved = false;
1065
1066 int rc = copy_from_uspace(&call->data.args, &data->args,
1067 sizeof(call->data.args));
1068 if (rc != 0)
1069 return rc;
1070
1071 rc = answer_preprocess(call, saved ? &saved_data : NULL);
1072
1073 ipc_answer(&TASK->answerbox, call);
1074 return rc;
1075}
1076
1077/** Hang up a phone.
1078 *
1079 * @param Phone handle of the phone to be hung up.
1080 *
1081 * @return 0 on success or an error code.
1082 *
1083 */
1084sysarg_t sys_ipc_hangup(sysarg_t phoneid)
1085{
1086 phone_t *phone;
1087
1088 if (phone_get(phoneid, &phone) != EOK)
1089 return ENOENT;
1090
1091 if (ipc_phone_hangup(phone))
1092 return -1;
1093
1094 return 0;
1095}
1096
1097/** Wait for an incoming IPC call or an answer.
1098 *
1099 * @param calldata Pointer to buffer where the call/answer data is stored.
1100 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
1101 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
1102 * for explanation.
1103 *
1104 * @return Hash of the call.
1105 * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
1106 * call is a notification. IPC_CALLID_ANSWERED denotes an
1107 * answer.
1108 *
1109 */
1110sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
1111 unsigned int flags)
1112{
1113 call_t *call;
1114
1115restart:
1116
1117#ifdef CONFIG_UDEBUG
1118 udebug_stoppable_begin();
1119#endif
1120
1121 call = ipc_wait_for_call(&TASK->answerbox, usec,
1122 flags | SYNCH_FLAGS_INTERRUPTIBLE);
1123
1124#ifdef CONFIG_UDEBUG
1125 udebug_stoppable_end();
1126#endif
1127
1128 if (!call)
1129 return 0;
1130
1131 if (call->flags & IPC_CALL_NOTIF) {
1132 /* Set in_phone_hash to the interrupt counter */
1133 call->data.phone = (void *) call->priv;
1134
1135 STRUCT_TO_USPACE(calldata, &call->data);
1136
1137 ipc_call_free(call);
1138
1139 return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
1140 }
1141
1142 if (call->flags & IPC_CALL_ANSWERED) {
1143 process_answer(call);
1144
1145 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
1146 ipc_call_free(call);
1147 goto restart;
1148 }
1149
1150 STRUCT_TO_USPACE(&calldata->args, &call->data.args);
1151 ipc_call_free(call);
1152
1153 return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
1154 }
1155
1156 if (process_request(&TASK->answerbox, call))
1157 goto restart;
1158
1159 /* Include phone address('id') of the caller in the request,
1160 * copy whole call->data, not only call->data.args */
1161 if (STRUCT_TO_USPACE(calldata, &call->data)) {
1162 /*
1163 * The callee will not receive this call and no one else has
1164 * a chance to answer it. Reply with the EPARTY error code.
1165 */
1166 ipc_data_t saved_data;
1167 bool saved;
1168
1169 if (answer_need_old(call)) {
1170 memcpy(&saved_data, &call->data, sizeof(call->data));
1171 saved = true;
1172 } else
1173 saved = false;
1174
1175 IPC_SET_RETVAL(call->data, EPARTY);
1176 (void) answer_preprocess(call, saved ? &saved_data : NULL);
1177 ipc_answer(&TASK->answerbox, call);
1178 return 0;
1179 }
1180
1181 return (sysarg_t) call;
1182}
1183
1184/** Interrupt one thread from sys_ipc_wait_for_call().
1185 *
1186 */
1187sysarg_t sys_ipc_poke(void)
1188{
1189 waitq_unsleep(&TASK->answerbox.wq);
1190 return EOK;
1191}
1192
1193/** Connect an IRQ handler to a task.
1194 *
1195 * @param inr IRQ number.
1196 * @param devno Device number.
1197 * @param imethod Interface and method to be associated with the notification.
1198 * @param ucode Uspace pointer to the top-half pseudocode.
1199 *
1200 * @return EPERM or a return code returned by ipc_irq_register().
1201 *
1202 */
1203sysarg_t sys_irq_register(inr_t inr, devno_t devno, sysarg_t imethod,
1204 irq_code_t *ucode)
1205{
1206 if (!(cap_get(TASK) & CAP_IRQ_REG))
1207 return EPERM;
1208
1209 return ipc_irq_register(&TASK->answerbox, inr, devno, imethod, ucode);
1210}
1211
1212/** Disconnect an IRQ handler from a task.
1213 *
1214 * @param inr IRQ number.
1215 * @param devno Device number.
1216 *
1217 * @return Zero on success or EPERM on error.
1218 *
1219 */
1220sysarg_t sys_irq_unregister(inr_t inr, devno_t devno)
1221{
1222 if (!(cap_get(TASK) & CAP_IRQ_REG))
1223 return EPERM;
1224
1225 ipc_irq_unregister(&TASK->answerbox, inr, devno);
1226
1227 return 0;
1228}
1229
1230#ifdef __32_BITS__
1231
1232/** Syscall connect to a task by ID (32 bits)
1233 *
1234 * @return Phone id on success, or negative error code.
1235 *
1236 */
1237sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
1238{
1239#ifdef CONFIG_UDEBUG
1240 sysarg64_t taskid;
1241 int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
1242 if (rc != 0)
1243 return (sysarg_t) rc;
1244
1245 return ipc_connect_kbox((task_id_t) taskid);
1246#else
1247 return (sysarg_t) ENOTSUP;
1248#endif
1249}
1250
1251#endif /* __32_BITS__ */
1252
1253#ifdef __64_BITS__
1254
1255/** Syscall connect to a task by ID (64 bits)
1256 *
1257 * @return Phone id on success, or negative error code.
1258 *
1259 */
1260sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
1261{
1262#ifdef CONFIG_UDEBUG
1263 return ipc_connect_kbox((task_id_t) taskid);
1264#else
1265 return (sysarg_t) ENOTSUP;
1266#endif
1267}
1268
1269#endif /* __64_BITS__ */
1270
1271/** @}
1272 */
Note: See TracBrowser for help on using the repository browser.