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

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

do not provide general access to kernel headers from uspace, only allow specific headers to be accessed or shared
externalize headers which serve as kernel/uspace API/ABI into a special tree

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