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

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

Allow special flags that control processing of IPC_M_DATA_READ/WRITE in
the kernel:

  • IPC_XF_NONE: default behavior
  • IPC_XF_RESTRICT: restrict the transfer size if necessary

Make read() and write() use IPC_XF_RESTRICT.

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