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

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

ipc_connect_to_me() now takes one extra argument to store the client task hash.

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