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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3751a08 was 3751a08, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Allow IPC read of zero size.

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