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

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

Always remember the original request method so that it is actually
possible to call ops→answer_process().

  • Property mode set to 100644
File size: 21.9 KB
RevLine 
[2d5a54f3]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[2d5a54f3]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[cc73a8a1]29/** @addtogroup genericipc
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[2d5a54f3]35#include <arch.h>
36#include <errno.h>
37#include <memstr.h>
38#include <ipc/ipc.h>
[c0699467]39#include <abi/ipc/methods.h>
[2d5a54f3]40#include <ipc/sysipc.h>
[e8039a86]41#include <ipc/sysipc_ops.h>
[162f919]42#include <ipc/irq.h>
[4e49572]43#include <ipc/ipcrsc.h>
[fdd4898]44#include <ipc/event.h>
[e028660]45#include <ipc/kbox.h>
[057d21a]46#include <synch/waitq.h>
[5626277]47#include <arch/interrupt.h>
[e3c762cd]48#include <syscall/copy.h>
[2bb8648]49#include <security/cap.h>
[6b10dab]50#include <console/console.h>
[253f35a1]51#include <print.h>
[e2ab36f1]52#include <macros.h>
[2d5a54f3]53
[da1bafb]54#define STRUCT_TO_USPACE(dst, src) copy_to_uspace((dst), (src), sizeof(*(src)))
[7918fce]55
[228e490]56/** Decide if the interface and method is a system method.
[8b243f2]57 *
[228e490]58 * @param imethod Interface and method to be decided.
[da1bafb]59 *
[228e490]60 * @return True if the interface and method is a system
61 * interface and method.
[8b243f2]62 *
63 */
[228e490]64static inline bool method_is_system(sysarg_t imethod)
[2ba7810]65{
[228e490]66 if (imethod <= IPC_M_LAST_SYSTEM)
[da1bafb]67 return true;
68
69 return false;
[2ba7810]70}
71
[228e490]72/** Decide if the message with this interface and method is forwardable.
[2ba7810]73 *
[228e490]74 * Some system messages may be forwarded, for some of them
75 * it is useless.
[8b243f2]76 *
[228e490]77 * @param imethod Interface and method to be decided.
[da1bafb]78 *
[228e490]79 * @return True if the interface and method is forwardable.
[8b243f2]80 *
[2ba7810]81 */
[228e490]82static inline bool method_is_forwardable(sysarg_t imethod)
[2ba7810]83{
[228e490]84 switch (imethod) {
[2c0e5d2]85 case IPC_M_CONNECTION_CLONE:
[6aae539d]86 case IPC_M_CLONE_ESTABLISH:
[7918fce]87 case IPC_M_PHONE_HUNGUP:
88 /* This message is meant only for the original recipient. */
[da1bafb]89 return false;
[7918fce]90 default:
[da1bafb]91 return true;
[7918fce]92 }
[2ba7810]93}
94
[228e490]95/** Decide if the message with this interface and method is immutable on forward.
[0dc4258]96 *
[228e490]97 * Some system messages may be forwarded but their content cannot be altered.
[0dc4258]98 *
[228e490]99 * @param imethod Interface and method to be decided.
[da1bafb]100 *
[228e490]101 * @return True if the interface and method is immutable on forward.
[0dc4258]102 *
103 */
[228e490]104static inline bool method_is_immutable(sysarg_t imethod)
[0dc4258]105{
[228e490]106 switch (imethod) {
[27d293a]107 case IPC_M_SHARE_OUT:
108 case IPC_M_SHARE_IN:
[36d852c]109 case IPC_M_DATA_WRITE:
110 case IPC_M_DATA_READ:
[fdd4898]111 case IPC_M_STATE_CHANGE_AUTHORIZE:
[da1bafb]112 return true;
[0dc4258]113 default:
[da1bafb]114 return false;
[0dc4258]115 }
116}
117
[2d5a54f3]118
[8b243f2]119/***********************************************************************
120 * Functions that preprocess answer before sending it to the recepient.
121 ***********************************************************************/
122
123/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
124 * for answer_preprocess().
125 *
[da1bafb]126 * @param call Call structure to be decided.
127 *
128 * @return true if the old call contents should be saved.
[8b243f2]129 *
[2d5a54f3]130 */
[da1bafb]131static inline bool answer_need_old(call_t *call)
[2d5a54f3]132{
[228e490]133 switch (IPC_GET_IMETHOD(call->data)) {
[2c0e5d2]134 case IPC_M_CONNECTION_CLONE:
[6aae539d]135 case IPC_M_CLONE_ESTABLISH:
[7918fce]136 case IPC_M_CONNECT_TO_ME:
137 case IPC_M_CONNECT_ME_TO:
[27d293a]138 case IPC_M_SHARE_OUT:
139 case IPC_M_SHARE_IN:
[36d852c]140 case IPC_M_DATA_WRITE:
141 case IPC_M_DATA_READ:
[fdd4898]142 case IPC_M_STATE_CHANGE_AUTHORIZE:
[da1bafb]143 return true;
[7918fce]144 default:
[da1bafb]145 return false;
[7918fce]146 }
[2d5a54f3]147}
148
[8b243f2]149/** Interpret process answer as control information.
150 *
151 * This function is called directly after sys_ipc_answer().
[46fc2f9]152 *
[da1bafb]153 * @param answer Call structure with the answer.
154 * @param olddata Saved data of the request.
155 *
[9956fad9]156 * @return Return EOK on success or a negative error code.
[8b243f2]157 *
[46fc2f9]158 */
[9956fad9]159static int answer_preprocess(call_t *answer, ipc_data_t *olddata)
[2d5a54f3]160{
[9956fad9]161 int rc = EOK;
162
[2405bb5]163 spinlock_lock(&answer->forget_lock);
164 if (answer->forget) {
165 /*
166 * This is a forgotten call and answer->sender is not valid.
167 */
168 spinlock_unlock(&answer->forget_lock);
[e8039a86]169 /* TODO: cleanup? */
[2405bb5]170 return rc;
171 } else {
172 /*
173 * Hold the sender task so that it cannot suddenly disappear
174 * while we are working with it.
175 */
176 task_hold(answer->sender);
177 }
178 spinlock_unlock(&answer->forget_lock);
179
[6c441cf8]180 if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
[ca687ad]181 /* In case of forward, hangup the forwared phone,
182 * not the originator
183 */
[ff48a15]184 mutex_lock(&answer->data.phone->lock);
[da1bafb]185 irq_spinlock_lock(&TASK->answerbox.lock, true);
[eb3d379]186 if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
[c4e4507]187 list_remove(&answer->data.phone->link);
[eb3d379]188 answer->data.phone->state = IPC_PHONE_SLAMMED;
[ca687ad]189 }
[da1bafb]190 irq_spinlock_unlock(&TASK->answerbox.lock, true);
[ff48a15]191 mutex_unlock(&answer->data.phone->lock);
[9f22213]192 }
[da1bafb]193
[2405bb5]194 if (!olddata) {
195 task_release(answer->sender);
[9956fad9]196 return rc;
[2405bb5]197 }
[da1bafb]198
[e8039a86]199
[32e4643]200 sysipc_ops_t *ops = sysipc_ops_get(answer->request_method);
[e8039a86]201 if (ops->answer_preprocess)
202 rc = ops->answer_preprocess(answer, olddata);
[da1bafb]203
[2405bb5]204 task_release(answer->sender);
205
[9956fad9]206 return rc;
[2d5a54f3]207}
208
[8b243f2]209/** Called before the request is sent.
210 *
[da1bafb]211 * @param call Call structure with the request.
212 * @param phone Phone that the call will be sent through.
213 *
214 * @return Return 0 on success, ELIMIT or EPERM on error.
[7c7aae16]215 *
216 */
[9a1b20c]217static int request_preprocess(call_t *call, phone_t *phone)
[7c7aae16]218{
[924c2530]219 int rc = EOK;
220
[32e4643]221 call->request_method = IPC_GET_IMETHOD(call->data);
222
223 sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
[e8039a86]224 if (ops->request_preprocess)
225 rc = ops->request_preprocess(call, phone);
[da1bafb]226
[924c2530]227 return rc;
[7c7aae16]228}
229
[8b243f2]230/*******************************************************************************
231 * Functions called to process received call/answer before passing it to uspace.
232 *******************************************************************************/
[2d5a54f3]233
[8b243f2]234/** Do basic kernel processing of received call answer.
235 *
[da1bafb]236 * @param call Call structure with the answer.
237 *
[8b243f2]238 */
[7c7aae16]239static void process_answer(call_t *call)
[2d5a54f3]240{
[6c441cf8]241 if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
[51ec40f]242 (call->flags & IPC_CALL_FORWARDED))
[9f22213]243 IPC_SET_RETVAL(call->data, EFORWARD);
[da1bafb]244
[7c7aae16]245 if (call->flags & IPC_CALL_CONN_ME_TO) {
246 if (IPC_GET_RETVAL(call->data))
[0c1a5d8a]247 phone_dealloc(call->priv);
[7c7aae16]248 else
[b61d47d]249 IPC_SET_ARG5(call->data, call->priv);
[7c7aae16]250 }
[da1bafb]251
[a55d5f9f]252 if (call->buffer) {
[da1bafb]253 /*
254 * This must be an affirmative answer to IPC_M_DATA_READ
[79ae36dd]255 * or IPC_M_DEBUG/UDEBUG_M_MEM_READ...
[da1bafb]256 *
257 */
[a55d5f9f]258 uintptr_t dst = IPC_GET_ARG1(call->data);
259 size_t size = IPC_GET_ARG2(call->data);
260 int rc = copy_to_uspace((void *) dst, call->buffer, size);
261 if (rc)
262 IPC_SET_RETVAL(call->data, rc);
263 free(call->buffer);
264 call->buffer = NULL;
265 }
[32e4643]266
267 sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
268 if (ops->answer_process)
269 (void) ops->answer_process(call);
[2d5a54f3]270}
271
[691d8d8]272
[8b243f2]273/** Do basic kernel processing of received call request.
274 *
[da1bafb]275 * @param box Destination answerbox structure.
276 * @param call Call structure with the request.
277 *
278 * @return 0 if the call should be passed to userspace.
279 * @return -1 if the call should be ignored.
[2d5a54f3]280 *
281 */
[51ec40f]282static int process_request(answerbox_t *box, call_t *call)
[2d5a54f3]283{
[691d8d8]284 int rc = EOK;
285
[32e4643]286 sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
[e8039a86]287 if (ops->request_process)
288 rc = ops->request_process(call, box);
[da1bafb]289
[691d8d8]290 return rc;
[2d5a54f3]291}
292
[97d17fe]293/** Check that the task did not exceed the allowed limit of asynchronous calls
294 * made over a phone.
[2d5a54f3]295 *
[228e490]296 * @param phone Phone to check the limit against.
297 *
[da1bafb]298 * @return 0 if limit not reached or -1 if limit exceeded.
299 *
[2d5a54f3]300 */
[97d17fe]301static int check_call_limit(phone_t *phone)
[2d5a54f3]302{
[97d17fe]303 if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
[2d5a54f3]304 return -1;
[da1bafb]305
[2d5a54f3]306 return 0;
307}
308
[8b243f2]309/** Make a fast asynchronous call over IPC.
[2d5a54f3]310 *
[3209923]311 * This function can only handle four arguments of payload, but is faster than
312 * the generic function sys_ipc_call_async_slow().
[8b243f2]313 *
[da1bafb]314 * @param phoneid Phone handle for the call.
[228e490]315 * @param imethod Interface and method of the call.
[da1bafb]316 * @param arg1 Service-defined payload argument.
317 * @param arg2 Service-defined payload argument.
318 * @param arg3 Service-defined payload argument.
319 * @param arg4 Service-defined payload argument.
320 *
321 * @return Call hash on success.
322 * @return IPC_CALLRET_FATAL in case of a fatal error.
323 * @return IPC_CALLRET_TEMPORARY if there are too many pending
324 * asynchronous requests; answers should be handled first.
325 *
[2d5a54f3]326 */
[228e490]327sysarg_t sys_ipc_call_async_fast(sysarg_t phoneid, sysarg_t imethod,
[96b02eb9]328 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]329{
[da1bafb]330 phone_t *phone;
[c9fff17]331 if (phone_get(phoneid, &phone) != EOK)
332 return IPC_CALLRET_FATAL;
[228e490]333
[97d17fe]334 if (check_call_limit(phone))
335 return IPC_CALLRET_TEMPORARY;
[da1bafb]336
337 call_t *call = ipc_call_alloc(0);
[228e490]338 IPC_SET_IMETHOD(call->data, imethod);
[2d5a54f3]339 IPC_SET_ARG1(call->data, arg1);
340 IPC_SET_ARG2(call->data, arg2);
[3209923]341 IPC_SET_ARG3(call->data, arg3);
342 IPC_SET_ARG4(call->data, arg4);
[da1bafb]343
[8498915]344 /*
345 * To achieve deterministic behavior, zero out arguments that are beyond
346 * the limits of the fast version.
347 */
348 IPC_SET_ARG5(call->data, 0);
[da1bafb]349
350 int res = request_preprocess(call, phone);
351
352 if (!res)
[7c7aae16]353 ipc_call(phone, call);
354 else
355 ipc_backsend_err(phone, call, res);
[da1bafb]356
[96b02eb9]357 return (sysarg_t) call;
[2d5a54f3]358}
359
[8b243f2]360/** Make an asynchronous IPC call allowing to transmit the entire payload.
361 *
[da1bafb]362 * @param phoneid Phone handle for the call.
363 * @param data Userspace address of call data with the request.
364 *
365 * @return See sys_ipc_call_async_fast().
[2d5a54f3]366 *
367 */
[96b02eb9]368sysarg_t sys_ipc_call_async_slow(sysarg_t phoneid, ipc_data_t *data)
[2d5a54f3]369{
[da1bafb]370 phone_t *phone;
[c9fff17]371 if (phone_get(phoneid, &phone) != EOK)
372 return IPC_CALLRET_FATAL;
[4e49572]373
[97d17fe]374 if (check_call_limit(phone))
375 return IPC_CALLRET_TEMPORARY;
376
[da1bafb]377 call_t *call = ipc_call_alloc(0);
378 int rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]379 sizeof(call->data.args));
[f58af46]380 if (rc != 0) {
381 ipc_call_free(call);
[96b02eb9]382 return (sysarg_t) rc;
[f58af46]383 }
[da1bafb]384
385 int res = request_preprocess(call, phone);
386
387 if (!res)
[7c7aae16]388 ipc_call(phone, call);
389 else
390 ipc_backsend_err(phone, call, res);
[da1bafb]391
[96b02eb9]392 return (sysarg_t) call;
[2d5a54f3]393}
394
[da1bafb]395/** Forward a received call to another destination
396 *
397 * Common code for both the fast and the slow version.
398 *
399 * @param callid Hash of the call to forward.
400 * @param phoneid Phone handle to use for forwarding.
[228e490]401 * @param imethod New interface and method to use for the forwarded call.
[da1bafb]402 * @param arg1 New value of the first argument for the forwarded call.
403 * @param arg2 New value of the second argument for the forwarded call.
404 * @param arg3 New value of the third argument for the forwarded call.
405 * @param arg4 New value of the fourth argument for the forwarded call.
406 * @param arg5 New value of the fifth argument for the forwarded call.
407 * @param mode Flags that specify mode of the forward operation.
408 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
409 * the function considers only the fast version arguments:
410 * i.e. arg1 and arg2.
411 *
412 * @return 0 on succes, otherwise an error code.
413 *
414 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
415 *
[2ba7810]416 */
[96b02eb9]417static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t phoneid,
[228e490]418 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
[96b02eb9]419 sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
[2ba7810]420{
[da1bafb]421 call_t *call = get_call(callid);
[2ba7810]422 if (!call)
423 return ENOENT;
[48daf64]424
[9f22213]425 call->flags |= IPC_CALL_FORWARDED;
[da1bafb]426
427 phone_t *phone;
[c9fff17]428 if (phone_get(phoneid, &phone) != EOK) {
[2ba7810]429 IPC_SET_RETVAL(call->data, EFORWARD);
430 ipc_answer(&TASK->answerbox, call);
431 return ENOENT;
[c9fff17]432 }
[da1bafb]433
[228e490]434 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
[2ba7810]435 IPC_SET_RETVAL(call->data, EFORWARD);
436 ipc_answer(&TASK->answerbox, call);
437 return EPERM;
438 }
[da1bafb]439
[0dc4258]440 /*
[4e5dabf]441 * User space is not allowed to change interface and method of system
[228e490]442 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
[4e5dabf]443 * means of imethod, arg1, arg2 and arg3.
[228e490]444 * If the interface and method is immutable, don't change anything.
[2ba7810]445 */
[228e490]446 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
447 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
448 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
[b61d47d]449 phone_dealloc(IPC_GET_ARG5(call->data));
[da1bafb]450
[228e490]451 IPC_SET_ARG1(call->data, imethod);
[0dc4258]452 IPC_SET_ARG2(call->data, arg1);
[b61d47d]453 IPC_SET_ARG3(call->data, arg2);
[da1bafb]454
[4e5dabf]455 if (slow)
[48daf64]456 IPC_SET_ARG4(call->data, arg3);
[4e5dabf]457
458 /*
459 * For system methods we deliberately don't
460 * overwrite ARG5.
461 */
[0dc4258]462 } else {
[228e490]463 IPC_SET_IMETHOD(call->data, imethod);
[0dc4258]464 IPC_SET_ARG1(call->data, arg1);
[b61d47d]465 IPC_SET_ARG2(call->data, arg2);
[48daf64]466 if (slow) {
467 IPC_SET_ARG3(call->data, arg3);
468 IPC_SET_ARG4(call->data, arg4);
469 IPC_SET_ARG5(call->data, arg5);
470 }
[0dc4258]471 }
[2ba7810]472 }
[da1bafb]473
[d40a8ff]474 return ipc_forward(call, phone, &TASK->answerbox, mode);
[2ba7810]475}
476
[48daf64]477/** Forward a received call to another destination - fast version.
478 *
[228e490]479 * In case the original interface and method is a system method, ARG1, ARG2
480 * and ARG3 are overwritten in the forwarded message with the new method and
481 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
482 * are rewritten with the new interface and method, arg1 and arg2, respectively.
483 * Also note there is a set of immutable methods, for which the new method and
484 * arguments are not set and these values are ignored.
[da1bafb]485 *
486 * @param callid Hash of the call to forward.
487 * @param phoneid Phone handle to use for forwarding.
[228e490]488 * @param imethod New interface and method to use for the forwarded call.
[da1bafb]489 * @param arg1 New value of the first argument for the forwarded call.
490 * @param arg2 New value of the second argument for the forwarded call.
491 * @param mode Flags that specify mode of the forward operation.
492 *
493 * @return 0 on succes, otherwise an error code.
494 *
[48daf64]495 */
[96b02eb9]496sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t phoneid,
[228e490]497 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[48daf64]498{
[228e490]499 return sys_ipc_forward_common(callid, phoneid, imethod, arg1, arg2, 0, 0,
[48daf64]500 0, mode, false);
501}
502
503/** Forward a received call to another destination - slow version.
504 *
505 * This function is the slow verision of the sys_ipc_forward_fast interface.
[228e490]506 * It can copy all five new arguments and the new interface and method from
507 * the userspace. It naturally extends the functionality of the fast version.
508 * For system methods, it additionally stores the new value of arg3 to ARG4.
509 * For non-system methods, it additionally stores the new value of arg3, arg4
510 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
[da1bafb]511 *
512 * @param callid Hash of the call to forward.
513 * @param phoneid Phone handle to use for forwarding.
514 * @param data Userspace address of the new IPC data.
515 * @param mode Flags that specify mode of the forward operation.
516 *
517 * @return 0 on succes, otherwise an error code.
518 *
[48daf64]519 */
[96b02eb9]520sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t phoneid,
[da1bafb]521 ipc_data_t *data, unsigned int mode)
[48daf64]522{
523 ipc_data_t newdata;
[da1bafb]524 int rc = copy_from_uspace(&newdata.args, &data->args,
[48daf64]525 sizeof(newdata.args));
[da1bafb]526 if (rc != 0)
[96b02eb9]527 return (sysarg_t) rc;
[da1bafb]528
[48daf64]529 return sys_ipc_forward_common(callid, phoneid,
[228e490]530 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
[48daf64]531 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
532 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
533}
534
[8b243f2]535/** Answer an IPC call - fast version.
536 *
537 * This function can handle only two return arguments of payload, but is faster
538 * than the generic sys_ipc_answer().
539 *
[da1bafb]540 * @param callid Hash of the call to be answered.
541 * @param retval Return value of the answer.
542 * @param arg1 Service-defined return value.
543 * @param arg2 Service-defined return value.
544 * @param arg3 Service-defined return value.
545 * @param arg4 Service-defined return value.
546 *
547 * @return 0 on success, otherwise an error code.
[8b243f2]548 *
549 */
[96b02eb9]550sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
551 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]552{
[897f2e76]553 /* Do not answer notification callids */
554 if (callid & IPC_CALLID_NOTIFICATION)
555 return 0;
[da1bafb]556
557 call_t *call = get_call(callid);
[2ba7810]558 if (!call)
559 return ENOENT;
[da1bafb]560
561 ipc_data_t saved_data;
562 bool saved;
563
[9f22213]564 if (answer_need_old(call)) {
[2ba7810]565 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]566 saved = true;
567 } else
568 saved = false;
569
[2d5a54f3]570 IPC_SET_RETVAL(call->data, retval);
571 IPC_SET_ARG1(call->data, arg1);
572 IPC_SET_ARG2(call->data, arg2);
[b74959bd]573 IPC_SET_ARG3(call->data, arg3);
574 IPC_SET_ARG4(call->data, arg4);
[da1bafb]575
[8498915]576 /*
577 * To achieve deterministic behavior, zero out arguments that are beyond
578 * the limits of the fast version.
579 */
580 IPC_SET_ARG5(call->data, 0);
[da1bafb]581 int rc = answer_preprocess(call, saved ? &saved_data : NULL);
582
[2d5a54f3]583 ipc_answer(&TASK->answerbox, call);
[7c23af9]584 return rc;
[2d5a54f3]585}
586
[8b243f2]587/** Answer an IPC call.
588 *
[da1bafb]589 * @param callid Hash of the call to be answered.
590 * @param data Userspace address of call data with the answer.
591 *
592 * @return 0 on success, otherwise an error code.
[8b243f2]593 *
594 */
[96b02eb9]595sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
[2d5a54f3]596{
[897f2e76]597 /* Do not answer notification callids */
598 if (callid & IPC_CALLID_NOTIFICATION)
599 return 0;
[da1bafb]600
601 call_t *call = get_call(callid);
[2ba7810]602 if (!call)
603 return ENOENT;
[da1bafb]604
605 ipc_data_t saved_data;
606 bool saved;
607
[9f22213]608 if (answer_need_old(call)) {
[2ba7810]609 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]610 saved = true;
611 } else
612 saved = false;
613
614 int rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]615 sizeof(call->data.args));
[e3c762cd]616 if (rc != 0)
617 return rc;
[da1bafb]618
619 rc = answer_preprocess(call, saved ? &saved_data : NULL);
[2d5a54f3]620
621 ipc_answer(&TASK->answerbox, call);
[7c23af9]622 return rc;
[2d5a54f3]623}
624
[8b243f2]625/** Hang up a phone.
[2d5a54f3]626 *
[da1bafb]627 * @param Phone handle of the phone to be hung up.
628 *
629 * @return 0 on success or an error code.
[8b243f2]630 *
[fbcfd458]631 */
[96b02eb9]632sysarg_t sys_ipc_hangup(sysarg_t phoneid)
[fbcfd458]633{
634 phone_t *phone;
[da1bafb]635
[c9fff17]636 if (phone_get(phoneid, &phone) != EOK)
637 return ENOENT;
[da1bafb]638
[d8f7362]639 if (ipc_phone_hangup(phone))
[fbcfd458]640 return -1;
[da1bafb]641
[fbcfd458]642 return 0;
643}
644
[8b243f2]645/** Wait for an incoming IPC call or an answer.
[fbcfd458]646 *
[da1bafb]647 * @param calldata Pointer to buffer where the call/answer data is stored.
648 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
649 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
650 * for explanation.
651 *
652 * @return Hash of the call.
653 * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
654 * call is a notification. IPC_CALLID_ANSWERED denotes an
655 * answer.
[bd5a663]656 *
[2d5a54f3]657 */
[96b02eb9]658sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
[da1bafb]659 unsigned int flags)
[2d5a54f3]660{
661 call_t *call;
[da1bafb]662
[741fd16]663restart:
[da1bafb]664
[741fd16]665#ifdef CONFIG_UDEBUG
666 udebug_stoppable_begin();
[da1bafb]667#endif
668
[51ec40f]669 call = ipc_wait_for_call(&TASK->answerbox, usec,
670 flags | SYNCH_FLAGS_INTERRUPTIBLE);
[da1bafb]671
[741fd16]672#ifdef CONFIG_UDEBUG
673 udebug_stoppable_end();
674#endif
[da1bafb]675
[9f22213]676 if (!call)
677 return 0;
[da1bafb]678
[5626277]679 if (call->flags & IPC_CALL_NOTIF) {
[43752b6]680 /* Set in_phone_hash to the interrupt counter */
[0c1a5d8a]681 call->data.phone = (void *) call->priv;
[43752b6]682
683 STRUCT_TO_USPACE(calldata, &call->data);
[da1bafb]684
[5626277]685 ipc_call_free(call);
686
[96b02eb9]687 return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
[5626277]688 }
[da1bafb]689
[2d5a54f3]690 if (call->flags & IPC_CALL_ANSWERED) {
[7c7aae16]691 process_answer(call);
[da1bafb]692
[fbcfd458]693 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
694 ipc_call_free(call);
695 goto restart;
696 }
[da1bafb]697
[fbcfd458]698 STRUCT_TO_USPACE(&calldata->args, &call->data.args);
[2d5a54f3]699 ipc_call_free(call);
[da1bafb]700
[96b02eb9]701 return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
[2d5a54f3]702 }
[da1bafb]703
[2d5a54f3]704 if (process_request(&TASK->answerbox, call))
705 goto restart;
[da1bafb]706
[7c7aae16]707 /* Include phone address('id') of the caller in the request,
708 * copy whole call->data, not only call->data.args */
[bd55bbb]709 if (STRUCT_TO_USPACE(calldata, &call->data)) {
[e06da7e]710 /*
711 * The callee will not receive this call and no one else has
712 * a chance to answer it. Reply with the EPARTY error code.
[b11ee88]713 */
[e06da7e]714 ipc_data_t saved_data;
[da1bafb]715 bool saved;
716
[e06da7e]717 if (answer_need_old(call)) {
718 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]719 saved = true;
720 } else
721 saved = false;
[e06da7e]722
723 IPC_SET_RETVAL(call->data, EPARTY);
[da1bafb]724 (void) answer_preprocess(call, saved ? &saved_data : NULL);
[e06da7e]725 ipc_answer(&TASK->answerbox, call);
[bd55bbb]726 return 0;
727 }
[da1bafb]728
[96b02eb9]729 return (sysarg_t) call;
[2d5a54f3]730}
[5626277]731
[da1bafb]732/** Interrupt one thread from sys_ipc_wait_for_call().
733 *
734 */
[96b02eb9]735sysarg_t sys_ipc_poke(void)
[057d21a]736{
[da1bafb]737 waitq_unsleep(&TASK->answerbox.wq);
[057d21a]738 return EOK;
739}
740
[8b243f2]741/** Connect an IRQ handler to a task.
[2b017ba]742 *
[228e490]743 * @param inr IRQ number.
744 * @param devno Device number.
745 * @param imethod Interface and method to be associated with the notification.
746 * @param ucode Uspace pointer to the top-half pseudocode.
[da1bafb]747 *
748 * @return EPERM or a return code returned by ipc_irq_register().
[2b017ba]749 *
750 */
[f044e96]751sysarg_t sys_irq_register(inr_t inr, devno_t devno, sysarg_t imethod,
[51ec40f]752 irq_code_t *ucode)
[5626277]753{
[2bb8648]754 if (!(cap_get(TASK) & CAP_IRQ_REG))
755 return EPERM;
[da1bafb]756
[228e490]757 return ipc_irq_register(&TASK->answerbox, inr, devno, imethod, ucode);
[5626277]758}
759
[8b243f2]760/** Disconnect an IRQ handler from a task.
761 *
[da1bafb]762 * @param inr IRQ number.
763 * @param devno Device number.
764 *
765 * @return Zero on success or EPERM on error.
[2b017ba]766 *
767 */
[f044e96]768sysarg_t sys_irq_unregister(inr_t inr, devno_t devno)
[5626277]769{
[2bb8648]770 if (!(cap_get(TASK) & CAP_IRQ_REG))
771 return EPERM;
[da1bafb]772
[2b017ba]773 ipc_irq_unregister(&TASK->answerbox, inr, devno);
[da1bafb]774
[5626277]775 return 0;
776}
[b45c443]777
[6b10dab]778#ifdef __32_BITS__
[9a1b20c]779
[6b10dab]780/** Syscall connect to a task by ID (32 bits)
[da1bafb]781 *
782 * @return Phone id on success, or negative error code.
[9a1b20c]783 *
784 */
[6b10dab]785sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
[9a1b20c]786{
787#ifdef CONFIG_UDEBUG
[6b10dab]788 sysarg64_t taskid;
789 int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
[9a1b20c]790 if (rc != 0)
[96b02eb9]791 return (sysarg_t) rc;
[da1bafb]792
[6b10dab]793 return ipc_connect_kbox((task_id_t) taskid);
[9a1b20c]794#else
[96b02eb9]795 return (sysarg_t) ENOTSUP;
[9a1b20c]796#endif
797}
798
[6b10dab]799#endif /* __32_BITS__ */
800
801#ifdef __64_BITS__
802
803/** Syscall connect to a task by ID (64 bits)
804 *
805 * @return Phone id on success, or negative error code.
806 *
807 */
808sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
809{
810#ifdef CONFIG_UDEBUG
811 return ipc_connect_kbox((task_id_t) taskid);
812#else
813 return (sysarg_t) ENOTSUP;
814#endif
815}
816
817#endif /* __64_BITS__ */
818
[cc73a8a1]819/** @}
[b45c443]820 */
Note: See TracBrowser for help on using the repository browser.