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

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

Add SYS_IPC_POKE syscall.

  • Property mode set to 100644
File size: 30.5 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{
533 call_t call;
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
[ba81cab]540 ipc_call_static_init(&call);
[2d5a54f3]541 IPC_SET_METHOD(call.data, method);
542 IPC_SET_ARG1(call.data, arg1);
[2e51969]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 */
549 IPC_SET_ARG4(call.data, 0);
550 IPC_SET_ARG5(call.data, 0);
[2d5a54f3]551
[9a1b20c]552 if (!(res = request_preprocess(&call, phone))) {
[741fd16]553#ifdef CONFIG_UDEBUG
554 udebug_stoppable_begin();
555#endif
[79872cd]556 rc = ipc_call_sync(phone, &call);
[741fd16]557#ifdef CONFIG_UDEBUG
558 udebug_stoppable_end();
559#endif
[79872cd]560 if (rc != EOK)
561 return rc;
[7c7aae16]562 process_answer(&call);
[741fd16]563
[8b243f2]564 } else {
[7c7aae16]565 IPC_SET_RETVAL(call.data, res);
[8b243f2]566 }
[bc50fc42]567 rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
568 if (rc != 0)
569 return rc;
[2d5a54f3]570
571 return 0;
572}
573
[8b243f2]574/** Make a synchronous IPC call allowing to transmit the entire payload.
575 *
576 * @param phoneid Phone handle for the call.
577 * @param question Userspace address of call data with the request.
[7918fce]578 * @param reply Userspace address of call data where to store the
579 * answer.
[8b243f2]580 *
581 * @return Zero on success or an error code.
582 */
[2e51969]583unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question,
[51ec40f]584 ipc_data_t *reply)
[2d5a54f3]585{
586 call_t call;
587 phone_t *phone;
[7c7aae16]588 int res;
[e3c762cd]589 int rc;
[2d5a54f3]590
[ba81cab]591 ipc_call_static_init(&call);
[51ec40f]592 rc = copy_from_uspace(&call.data.args, &question->args,
593 sizeof(call.data.args));
[e3c762cd]594 if (rc != 0)
[7f1c620]595 return (unative_t) rc;
[2ba7810]596
[ba81cab]597 GET_CHECK_PHONE(phone, phoneid, return ENOENT);
[4e49572]598
[9a1b20c]599 if (!(res = request_preprocess(&call, phone))) {
[741fd16]600#ifdef CONFIG_UDEBUG
601 udebug_stoppable_begin();
602#endif
[79872cd]603 rc = ipc_call_sync(phone, &call);
[741fd16]604#ifdef CONFIG_UDEBUG
605 udebug_stoppable_end();
606#endif
[79872cd]607 if (rc != EOK)
608 return rc;
[7c7aae16]609 process_answer(&call);
610 } else
611 IPC_SET_RETVAL(call.data, res);
[2d5a54f3]612
[e3c762cd]613 rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
614 if (rc != 0)
615 return rc;
[2d5a54f3]616
617 return 0;
618}
619
[8b243f2]620/** Check that the task did not exceed the allowed limit of asynchronous calls.
[2d5a54f3]621 *
[8b243f2]622 * @return Return 0 if limit not reached or -1 if limit exceeded.
[2d5a54f3]623 */
624static int check_call_limit(void)
625{
626 if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
627 atomic_dec(&TASK->active_calls);
628 return -1;
629 }
630 return 0;
631}
632
[8b243f2]633/** Make a fast asynchronous call over IPC.
[2d5a54f3]634 *
[3209923]635 * This function can only handle four arguments of payload, but is faster than
636 * the generic function sys_ipc_call_async_slow().
[8b243f2]637 *
638 * @param phoneid Phone handle for the call.
639 * @param method Method of the call.
640 * @param arg1 Service-defined payload argument.
641 * @param arg2 Service-defined payload argument.
[3209923]642 * @param arg3 Service-defined payload argument.
643 * @param arg4 Service-defined payload argument.
[8b243f2]644 *
645 * @return Return call hash on success.
646 * Return IPC_CALLRET_FATAL in case of a fatal error and
647 * IPC_CALLRET_TEMPORARY if there are too many pending
648 * asynchronous requests; answers should be handled first.
[2d5a54f3]649 */
[51ec40f]650unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
[3209923]651 unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
[2d5a54f3]652{
653 call_t *call;
654 phone_t *phone;
[7c7aae16]655 int res;
[2ba7810]656
[2d5a54f3]657 if (check_call_limit())
658 return IPC_CALLRET_TEMPORARY;
659
[7c7aae16]660 GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
[4e49572]661
[5626277]662 call = ipc_call_alloc(0);
[2d5a54f3]663 IPC_SET_METHOD(call->data, method);
664 IPC_SET_ARG1(call->data, arg1);
665 IPC_SET_ARG2(call->data, arg2);
[3209923]666 IPC_SET_ARG3(call->data, arg3);
667 IPC_SET_ARG4(call->data, arg4);
[8498915]668 /*
669 * To achieve deterministic behavior, zero out arguments that are beyond
670 * the limits of the fast version.
671 */
672 IPC_SET_ARG5(call->data, 0);
[2d5a54f3]673
[9a1b20c]674 if (!(res = request_preprocess(call, phone)))
[7c7aae16]675 ipc_call(phone, call);
676 else
677 ipc_backsend_err(phone, call, res);
[2d5a54f3]678
[7f1c620]679 return (unative_t) call;
[2d5a54f3]680}
681
[8b243f2]682/** Make an asynchronous IPC call allowing to transmit the entire payload.
683 *
684 * @param phoneid Phone handle for the call.
685 * @param data Userspace address of call data with the request.
[2d5a54f3]686 *
[8b243f2]687 * @return See sys_ipc_call_async_fast().
[2d5a54f3]688 */
[3209923]689unative_t sys_ipc_call_async_slow(unative_t phoneid, ipc_data_t *data)
[2d5a54f3]690{
691 call_t *call;
692 phone_t *phone;
[7c7aae16]693 int res;
[e3c762cd]694 int rc;
[2d5a54f3]695
696 if (check_call_limit())
697 return IPC_CALLRET_TEMPORARY;
698
[7c7aae16]699 GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
[4e49572]700
[5626277]701 call = ipc_call_alloc(0);
[51ec40f]702 rc = copy_from_uspace(&call->data.args, &data->args,
703 sizeof(call->data.args));
[f58af46]704 if (rc != 0) {
705 ipc_call_free(call);
[7f1c620]706 return (unative_t) rc;
[f58af46]707 }
[9a1b20c]708 if (!(res = request_preprocess(call, phone)))
[7c7aae16]709 ipc_call(phone, call);
710 else
711 ipc_backsend_err(phone, call, res);
[2d5a54f3]712
[7f1c620]713 return (unative_t) call;
[2d5a54f3]714}
715
[48daf64]716/** Forward a received call to another destination - common code for both the
717 * fast and the slow version.
[8b243f2]718 *
719 * @param callid Hash of the call to forward.
720 * @param phoneid Phone handle to use for forwarding.
721 * @param method New method to use for the forwarded call.
722 * @param arg1 New value of the first argument for the forwarded call.
[90c35436]723 * @param arg2 New value of the second argument for the forwarded call.
[48daf64]724 * @param arg3 New value of the third argument for the forwarded call.
725 * @param arg4 New value of the fourth argument for the forwarded call.
726 * @param arg5 New value of the fifth argument for the forwarded call.
[d40a8ff]727 * @param mode Flags that specify mode of the forward operation.
[48daf64]728 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
729 * the function considers only the fast version arguments:
730 * i.e. arg1 and arg2.
[8b243f2]731 *
732 * @return Return 0 on succes, otherwise return an error code.
[2ba7810]733 *
[48daf64]734 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
[2ba7810]735 */
[48daf64]736static unative_t sys_ipc_forward_common(unative_t callid, unative_t phoneid,
737 unative_t method, unative_t arg1, unative_t arg2, unative_t arg3,
738 unative_t arg4, unative_t arg5, int mode, bool slow)
[2ba7810]739{
740 call_t *call;
741 phone_t *phone;
742
743 call = get_call(callid);
744 if (!call)
745 return ENOENT;
[48daf64]746
[9f22213]747 call->flags |= IPC_CALL_FORWARDED;
748
[ba81cab]749 GET_CHECK_PHONE(phone, phoneid, {
[2ba7810]750 IPC_SET_RETVAL(call->data, EFORWARD);
751 ipc_answer(&TASK->answerbox, call);
752 return ENOENT;
[9fe962d6]753 });
[2ba7810]754
[0dc4258]755 if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
[2ba7810]756 IPC_SET_RETVAL(call->data, EFORWARD);
757 ipc_answer(&TASK->answerbox, call);
758 return EPERM;
759 }
760
[0dc4258]761 /*
762 * Userspace is not allowed to change method of system methods on
[48daf64]763 * forward, allow changing ARG1, ARG2, ARG3 and ARG4 by means of method,
764 * arg1, arg2 and arg3.
[0dc4258]765 * If the method is immutable, don't change anything.
[2ba7810]766 */
[0dc4258]767 if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
768 if (method_is_system(IPC_GET_METHOD(call->data))) {
769 if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
[b61d47d]770 phone_dealloc(IPC_GET_ARG5(call->data));
[7c7aae16]771
[0dc4258]772 IPC_SET_ARG1(call->data, method);
773 IPC_SET_ARG2(call->data, arg1);
[b61d47d]774 IPC_SET_ARG3(call->data, arg2);
[48daf64]775 if (slow) {
776 IPC_SET_ARG4(call->data, arg3);
777 /*
778 * For system methods we deliberately don't
779 * overwrite ARG5.
780 */
781 }
[0dc4258]782 } else {
783 IPC_SET_METHOD(call->data, method);
784 IPC_SET_ARG1(call->data, arg1);
[b61d47d]785 IPC_SET_ARG2(call->data, arg2);
[48daf64]786 if (slow) {
787 IPC_SET_ARG3(call->data, arg3);
788 IPC_SET_ARG4(call->data, arg4);
789 IPC_SET_ARG5(call->data, arg5);
790 }
[0dc4258]791 }
[2ba7810]792 }
793
[d40a8ff]794 return ipc_forward(call, phone, &TASK->answerbox, mode);
[2ba7810]795}
796
[48daf64]797/** Forward a received call to another destination - fast version.
798 *
799 * @param callid Hash of the call to forward.
800 * @param phoneid Phone handle to use for forwarding.
801 * @param method New method to use for the forwarded call.
802 * @param arg1 New value of the first argument for the forwarded call.
803 * @param arg2 New value of the second argument for the forwarded call.
804 * @param mode Flags that specify mode of the forward operation.
805 *
806 * @return Return 0 on succes, otherwise return an error code.
807 *
808 * In case the original method is a system method, ARG1, ARG2 and ARG3 are
809 * overwritten in the forwarded message with the new method and the new
810 * arg1 and arg2, respectively. Otherwise the METHOD, ARG1 and ARG2 are
811 * rewritten with the new method, arg1 and arg2, respectively. Also note there
812 * is a set of immutable methods, for which the new method and arguments are not
813 * set and these values are ignored.
814 */
815unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
816 unative_t method, unative_t arg1, unative_t arg2, int mode)
817{
818 return sys_ipc_forward_common(callid, phoneid, method, arg1, arg2, 0, 0,
819 0, mode, false);
820}
821
822/** Forward a received call to another destination - slow version.
823 *
824 * @param callid Hash of the call to forward.
825 * @param phoneid Phone handle to use for forwarding.
826 * @param data Userspace address of the new IPC data.
827 * @param mode Flags that specify mode of the forward operation.
828 *
829 * @return Return 0 on succes, otherwise return an error code.
830 *
831 * This function is the slow verision of the sys_ipc_forward_fast interface.
832 * It can copy all five new arguments and the new method from the userspace.
833 * It naturally extends the functionality of the fast version. For system
834 * methods, it additionally stores the new value of arg3 to ARG4. For non-system
835 * methods, it additionally stores the new value of arg3, arg4 and arg5,
836 * respectively, to ARG3, ARG4 and ARG5, respectively.
837 */
838unative_t sys_ipc_forward_slow(unative_t callid, unative_t phoneid,
839 ipc_data_t *data, int mode)
840{
841 ipc_data_t newdata;
842 int rc;
843
844 rc = copy_from_uspace(&newdata.args, &data->args,
845 sizeof(newdata.args));
846 if (rc != 0)
847 return (unative_t) rc;
848
849 return sys_ipc_forward_common(callid, phoneid,
850 IPC_GET_METHOD(newdata), IPC_GET_ARG1(newdata),
851 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
852 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
853}
854
[8b243f2]855/** Answer an IPC call - fast version.
856 *
857 * This function can handle only two return arguments of payload, but is faster
858 * than the generic sys_ipc_answer().
859 *
860 * @param callid Hash of the call to be answered.
861 * @param retval Return value of the answer.
862 * @param arg1 Service-defined return value.
863 * @param arg2 Service-defined return value.
[b74959bd]864 * @param arg3 Service-defined return value.
865 * @param arg4 Service-defined return value.
[8b243f2]866 *
867 * @return Return 0 on success, otherwise return an error code.
868 */
[51ec40f]869unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
[b74959bd]870 unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
[2d5a54f3]871{
872 call_t *call;
[2ba7810]873 ipc_data_t saved_data;
[9f22213]874 int saveddata = 0;
[7c23af9]875 int rc;
[2d5a54f3]876
[897f2e76]877 /* Do not answer notification callids */
878 if (callid & IPC_CALLID_NOTIFICATION)
879 return 0;
880
[2ba7810]881 call = get_call(callid);
882 if (!call)
883 return ENOENT;
[2d5a54f3]884
[9f22213]885 if (answer_need_old(call)) {
[2ba7810]886 memcpy(&saved_data, &call->data, sizeof(call->data));
[9f22213]887 saveddata = 1;
[2d5a54f3]888 }
889
890 IPC_SET_RETVAL(call->data, retval);
891 IPC_SET_ARG1(call->data, arg1);
892 IPC_SET_ARG2(call->data, arg2);
[b74959bd]893 IPC_SET_ARG3(call->data, arg3);
894 IPC_SET_ARG4(call->data, arg4);
[8498915]895 /*
896 * To achieve deterministic behavior, zero out arguments that are beyond
897 * the limits of the fast version.
898 */
899 IPC_SET_ARG5(call->data, 0);
[7c23af9]900 rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
[2d5a54f3]901
902 ipc_answer(&TASK->answerbox, call);
[7c23af9]903 return rc;
[2d5a54f3]904}
905
[8b243f2]906/** Answer an IPC call.
907 *
908 * @param callid Hash of the call to be answered.
909 * @param data Userspace address of call data with the answer.
910 *
911 * @return Return 0 on success, otherwise return an error code.
912 */
[b74959bd]913unative_t sys_ipc_answer_slow(unative_t callid, ipc_data_t *data)
[2d5a54f3]914{
915 call_t *call;
[2ba7810]916 ipc_data_t saved_data;
[9f22213]917 int saveddata = 0;
[e3c762cd]918 int rc;
[2d5a54f3]919
[897f2e76]920 /* Do not answer notification callids */
921 if (callid & IPC_CALLID_NOTIFICATION)
922 return 0;
923
[2ba7810]924 call = get_call(callid);
925 if (!call)
926 return ENOENT;
[2d5a54f3]927
[9f22213]928 if (answer_need_old(call)) {
[2ba7810]929 memcpy(&saved_data, &call->data, sizeof(call->data));
[9f22213]930 saveddata = 1;
[2d5a54f3]931 }
[e3c762cd]932 rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]933 sizeof(call->data.args));
[e3c762cd]934 if (rc != 0)
935 return rc;
[2d5a54f3]936
[7c23af9]937 rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
[2d5a54f3]938
939 ipc_answer(&TASK->answerbox, call);
940
[7c23af9]941 return rc;
[2d5a54f3]942}
943
[8b243f2]944/** Hang up a phone.
[2d5a54f3]945 *
[8b243f2]946 * @param Phone handle of the phone to be hung up.
947 *
948 * @return Return 0 on success or an error code.
[fbcfd458]949 */
[7f1c620]950unative_t sys_ipc_hangup(int phoneid)
[fbcfd458]951{
952 phone_t *phone;
953
954 GET_CHECK_PHONE(phone, phoneid, return ENOENT);
955
[d8f7362]956 if (ipc_phone_hangup(phone))
[fbcfd458]957 return -1;
958
959 return 0;
960}
961
[8b243f2]962/** Wait for an incoming IPC call or an answer.
[fbcfd458]963 *
[51ec40f]964 * @param calldata Pointer to buffer where the call/answer data is stored.
965 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
966 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
967 * for explanation.
[bd5a663]968 *
[8b243f2]969 * @return Hash of the call.
970 * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
971 * call is a notification. IPC_CALLID_ANSWERED denotes an
972 * answer.
[2d5a54f3]973 */
[7f1c620]974unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
[2d5a54f3]975{
976 call_t *call;
977
[741fd16]978restart:
979
980#ifdef CONFIG_UDEBUG
981 udebug_stoppable_begin();
982#endif
[51ec40f]983 call = ipc_wait_for_call(&TASK->answerbox, usec,
984 flags | SYNCH_FLAGS_INTERRUPTIBLE);
[741fd16]985
986#ifdef CONFIG_UDEBUG
987 udebug_stoppable_end();
988#endif
[9f22213]989 if (!call)
990 return 0;
[2d5a54f3]991
[5626277]992 if (call->flags & IPC_CALL_NOTIF) {
993 ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
[43752b6]994
995 /* Set in_phone_hash to the interrupt counter */
[0c1a5d8a]996 call->data.phone = (void *) call->priv;
[43752b6]997
998 STRUCT_TO_USPACE(calldata, &call->data);
999
[5626277]1000 ipc_call_free(call);
1001
[8b243f2]1002 return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
[5626277]1003 }
1004
[2d5a54f3]1005 if (call->flags & IPC_CALL_ANSWERED) {
[7c7aae16]1006 process_answer(call);
[2d5a54f3]1007
1008 ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
[fbcfd458]1009
1010 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
1011 ipc_call_free(call);
1012 goto restart;
[119c335]1013 } else {
1014 /*
1015 * Decrement the counter of active calls only if the
1016 * call is not an answer to IPC_M_PHONE_HUNGUP,
1017 * which doesn't contribute to the counter.
1018 */
1019 atomic_dec(&TASK->active_calls);
[fbcfd458]1020 }
1021
1022 STRUCT_TO_USPACE(&calldata->args, &call->data.args);
[2d5a54f3]1023 ipc_call_free(call);
1024
[8b243f2]1025 return ((unative_t) call) | IPC_CALLID_ANSWERED;
[2d5a54f3]1026 }
[fbcfd458]1027
[2d5a54f3]1028 if (process_request(&TASK->answerbox, call))
1029 goto restart;
[fbcfd458]1030
[7c7aae16]1031 /* Include phone address('id') of the caller in the request,
1032 * copy whole call->data, not only call->data.args */
[bd55bbb]1033 if (STRUCT_TO_USPACE(calldata, &call->data)) {
[e06da7e]1034 /*
1035 * The callee will not receive this call and no one else has
1036 * a chance to answer it. Reply with the EPARTY error code.
[b11ee88]1037 */
[e06da7e]1038 ipc_data_t saved_data;
1039 int saveddata = 0;
1040
1041 if (answer_need_old(call)) {
1042 memcpy(&saved_data, &call->data, sizeof(call->data));
1043 saveddata = 1;
1044 }
1045
1046 IPC_SET_RETVAL(call->data, EPARTY);
1047 (void) answer_preprocess(call, saveddata ? &saved_data : NULL);
1048 ipc_answer(&TASK->answerbox, call);
[bd55bbb]1049 return 0;
1050 }
[7f1c620]1051 return (unative_t)call;
[2d5a54f3]1052}
[5626277]1053
[057d21a]1054/** Interrupt one thread from sys_ipc_wait_for_call(). */
1055unative_t sys_ipc_poke(void)
1056{
1057 waitq_unsleep(&TASK->answerbox.wq);
1058 return EOK;
1059}
1060
[8b243f2]1061/** Connect an IRQ handler to a task.
[2b017ba]1062 *
[8b243f2]1063 * @param inr IRQ number.
1064 * @param devno Device number.
1065 * @param method Method to be associated with the notification.
1066 * @param ucode Uspace pointer to the top-half pseudocode.
[2b017ba]1067 *
[8b243f2]1068 * @return EPERM or a return code returned by ipc_irq_register().
[2b017ba]1069 */
[51ec40f]1070unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
1071 irq_code_t *ucode)
[5626277]1072{
[2bb8648]1073 if (!(cap_get(TASK) & CAP_IRQ_REG))
1074 return EPERM;
1075
[2b017ba]1076 return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
[5626277]1077}
1078
[8b243f2]1079/** Disconnect an IRQ handler from a task.
1080 *
1081 * @param inr IRQ number.
1082 * @param devno Device number.
[2b017ba]1083 *
[8b243f2]1084 * @return Zero on success or EPERM on error..
[2b017ba]1085 */
1086unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
[5626277]1087{
[2bb8648]1088 if (!(cap_get(TASK) & CAP_IRQ_REG))
1089 return EPERM;
1090
[2b017ba]1091 ipc_irq_unregister(&TASK->answerbox, inr, devno);
[5626277]1092
1093 return 0;
1094}
[b45c443]1095
[9a1b20c]1096#include <console/console.h>
1097
1098/**
1099 * Syscall connect to a task by id.
1100 *
1101 * @return Phone id on success, or negative error code.
1102 */
1103unative_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid_arg)
1104{
1105#ifdef CONFIG_UDEBUG
1106 sysarg64_t taskid_arg;
1107 int rc;
1108
1109 rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
1110 if (rc != 0)
1111 return (unative_t) rc;
1112
[0108984a]1113 LOG("sys_ipc_connect_kbox(%" PRIu64 ")\n", taskid_arg.value);
[9a1b20c]1114
1115 return ipc_connect_kbox(taskid_arg.value);
1116#else
1117 return (unative_t) ENOTSUP;
1118#endif
1119}
1120
[cc73a8a1]1121/** @}
[b45c443]1122 */
Note: See TracBrowser for help on using the repository browser.