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

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

Replace the still buggy GET_CHECK_PHONE() macro with a function.

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