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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since aca4a04 was 4e5dabf, checked in by Martin Decky <martin@…>, 13 years ago

cstyle (no change in functionality)

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