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

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

Allocate the call structure for synchronous calls dynamically.

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