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

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

ABI split: pass 64-bit task ID as plain 64-bit argument to SYS_IPC_CONNECT_KBOX, SYS_CAP_GRANT, SYS_CAP_REVOKE

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