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

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

Introduce the per-task list of active synchronous answerboxes and make
ipc_cleanup() aware of it.

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