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

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

Fix leftover use of task hash in async_connect_to_me().

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