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

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

Allow special flags that control processing of IPC_M_DATA_READ/WRITE in
the kernel:

  • IPC_XF_NONE: default behavior
  • IPC_XF_RESTRICT: restrict the transfer size if necessary

Make read() and write() use IPC_XF_RESTRICT.

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