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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0095368 was da1bafb, checked in by Martin Decky <martin@…>, 15 years ago

major code revision

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