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

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

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

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