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

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

Forget active calls when a task exits.

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