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

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

Call answer_preprocess() also when ipc_forward() returns error.

  • Property mode set to 100644
File size: 22.1 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;
[b1e6269]162 sysipc_ops_t *ops;
[9956fad9]163
[2405bb5]164 spinlock_lock(&answer->forget_lock);
165 if (answer->forget) {
166 /*
167 * This is a forgotten call and answer->sender is not valid.
168 */
169 spinlock_unlock(&answer->forget_lock);
[b1e6269]170
171 ops = sysipc_ops_get(answer->request_method);
172 if (ops->answer_cleanup)
173 ops->answer_cleanup(answer, olddata);
174
[2405bb5]175 return rc;
176 } else {
[20282ef3]177 ASSERT(answer->active);
178
179 /*
180 * Mark the call as inactive to prevent _ipc_answer_free_call()
181 * from attempting to remove the call from the active list
182 * itself.
183 */
184 answer->active = false;
185
186 /*
187 * Remove the call from the sender's active call list.
188 * We enforce this locking order so that any potential
189 * concurrently executing forget operation is forced to
190 * release its active_calls_lock and lose the race to
191 * forget this soon to be answered call.
192 */
193 spinlock_lock(&answer->sender->active_calls_lock);
194 list_remove(&answer->ta_link);
195 spinlock_unlock(&answer->sender->active_calls_lock);
[2405bb5]196 }
197 spinlock_unlock(&answer->forget_lock);
198
[6c441cf8]199 if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
[ca687ad]200 /* In case of forward, hangup the forwared phone,
201 * not the originator
202 */
[ff48a15]203 mutex_lock(&answer->data.phone->lock);
[da1bafb]204 irq_spinlock_lock(&TASK->answerbox.lock, true);
[eb3d379]205 if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
[c4e4507]206 list_remove(&answer->data.phone->link);
[eb3d379]207 answer->data.phone->state = IPC_PHONE_SLAMMED;
[ca687ad]208 }
[da1bafb]209 irq_spinlock_unlock(&TASK->answerbox.lock, true);
[ff48a15]210 mutex_unlock(&answer->data.phone->lock);
[9f22213]211 }
[da1bafb]212
[53af6e8c]213 if (!olddata)
[9956fad9]214 return rc;
[e8039a86]215
[b1e6269]216 ops = sysipc_ops_get(answer->request_method);
[e8039a86]217 if (ops->answer_preprocess)
218 rc = ops->answer_preprocess(answer, olddata);
[da1bafb]219
[9956fad9]220 return rc;
[2d5a54f3]221}
222
[8b243f2]223/** Called before the request is sent.
224 *
[da1bafb]225 * @param call Call structure with the request.
226 * @param phone Phone that the call will be sent through.
227 *
228 * @return Return 0 on success, ELIMIT or EPERM on error.
[7c7aae16]229 *
230 */
[9a1b20c]231static int request_preprocess(call_t *call, phone_t *phone)
[7c7aae16]232{
[924c2530]233 int rc = EOK;
234
[32e4643]235 call->request_method = IPC_GET_IMETHOD(call->data);
236
237 sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
[e8039a86]238 if (ops->request_preprocess)
239 rc = ops->request_preprocess(call, phone);
[da1bafb]240
[924c2530]241 return rc;
[7c7aae16]242}
243
[8b243f2]244/*******************************************************************************
245 * Functions called to process received call/answer before passing it to uspace.
246 *******************************************************************************/
[2d5a54f3]247
[8b243f2]248/** Do basic kernel processing of received call answer.
249 *
[da1bafb]250 * @param call Call structure with the answer.
251 *
[8b243f2]252 */
[7c7aae16]253static void process_answer(call_t *call)
[2d5a54f3]254{
[6c441cf8]255 if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
[51ec40f]256 (call->flags & IPC_CALL_FORWARDED))
[9f22213]257 IPC_SET_RETVAL(call->data, EFORWARD);
[da1bafb]258
[32e4643]259 sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
260 if (ops->answer_process)
261 (void) ops->answer_process(call);
[2d5a54f3]262}
263
[691d8d8]264
[8b243f2]265/** Do basic kernel processing of received call request.
266 *
[da1bafb]267 * @param box Destination answerbox structure.
268 * @param call Call structure with the request.
269 *
270 * @return 0 if the call should be passed to userspace.
271 * @return -1 if the call should be ignored.
[2d5a54f3]272 *
273 */
[51ec40f]274static int process_request(answerbox_t *box, call_t *call)
[2d5a54f3]275{
[691d8d8]276 int rc = EOK;
277
[32e4643]278 sysipc_ops_t *ops = sysipc_ops_get(call->request_method);
[e8039a86]279 if (ops->request_process)
280 rc = ops->request_process(call, box);
[da1bafb]281
[691d8d8]282 return rc;
[2d5a54f3]283}
284
[97d17fe]285/** Check that the task did not exceed the allowed limit of asynchronous calls
286 * made over a phone.
[2d5a54f3]287 *
[228e490]288 * @param phone Phone to check the limit against.
289 *
[da1bafb]290 * @return 0 if limit not reached or -1 if limit exceeded.
291 *
[2d5a54f3]292 */
[97d17fe]293static int check_call_limit(phone_t *phone)
[2d5a54f3]294{
[97d17fe]295 if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
[2d5a54f3]296 return -1;
[da1bafb]297
[2d5a54f3]298 return 0;
299}
300
[8b243f2]301/** Make a fast asynchronous call over IPC.
[2d5a54f3]302 *
[3209923]303 * This function can only handle four arguments of payload, but is faster than
304 * the generic function sys_ipc_call_async_slow().
[8b243f2]305 *
[da1bafb]306 * @param phoneid Phone handle for the call.
[228e490]307 * @param imethod Interface and method of the call.
[da1bafb]308 * @param arg1 Service-defined payload argument.
309 * @param arg2 Service-defined payload argument.
310 * @param arg3 Service-defined payload argument.
311 * @param arg4 Service-defined payload argument.
312 *
313 * @return Call hash on success.
314 * @return IPC_CALLRET_FATAL in case of a fatal error.
315 * @return IPC_CALLRET_TEMPORARY if there are too many pending
316 * asynchronous requests; answers should be handled first.
317 *
[2d5a54f3]318 */
[228e490]319sysarg_t sys_ipc_call_async_fast(sysarg_t phoneid, sysarg_t imethod,
[96b02eb9]320 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]321{
[da1bafb]322 phone_t *phone;
[c9fff17]323 if (phone_get(phoneid, &phone) != EOK)
324 return IPC_CALLRET_FATAL;
[228e490]325
[97d17fe]326 if (check_call_limit(phone))
327 return IPC_CALLRET_TEMPORARY;
[da1bafb]328
329 call_t *call = ipc_call_alloc(0);
[228e490]330 IPC_SET_IMETHOD(call->data, imethod);
[2d5a54f3]331 IPC_SET_ARG1(call->data, arg1);
332 IPC_SET_ARG2(call->data, arg2);
[3209923]333 IPC_SET_ARG3(call->data, arg3);
334 IPC_SET_ARG4(call->data, arg4);
[da1bafb]335
[8498915]336 /*
337 * To achieve deterministic behavior, zero out arguments that are beyond
338 * the limits of the fast version.
339 */
340 IPC_SET_ARG5(call->data, 0);
[da1bafb]341
342 int res = request_preprocess(call, phone);
343
344 if (!res)
[7c7aae16]345 ipc_call(phone, call);
346 else
347 ipc_backsend_err(phone, call, res);
[da1bafb]348
[96b02eb9]349 return (sysarg_t) call;
[2d5a54f3]350}
351
[8b243f2]352/** Make an asynchronous IPC call allowing to transmit the entire payload.
353 *
[da1bafb]354 * @param phoneid Phone handle for the call.
355 * @param data Userspace address of call data with the request.
356 *
357 * @return See sys_ipc_call_async_fast().
[2d5a54f3]358 *
359 */
[96b02eb9]360sysarg_t sys_ipc_call_async_slow(sysarg_t phoneid, ipc_data_t *data)
[2d5a54f3]361{
[da1bafb]362 phone_t *phone;
[c9fff17]363 if (phone_get(phoneid, &phone) != EOK)
364 return IPC_CALLRET_FATAL;
[4e49572]365
[97d17fe]366 if (check_call_limit(phone))
367 return IPC_CALLRET_TEMPORARY;
368
[da1bafb]369 call_t *call = ipc_call_alloc(0);
370 int rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]371 sizeof(call->data.args));
[f58af46]372 if (rc != 0) {
373 ipc_call_free(call);
[96b02eb9]374 return (sysarg_t) rc;
[f58af46]375 }
[da1bafb]376
377 int res = request_preprocess(call, phone);
378
379 if (!res)
[7c7aae16]380 ipc_call(phone, call);
381 else
382 ipc_backsend_err(phone, call, res);
[da1bafb]383
[96b02eb9]384 return (sysarg_t) call;
[2d5a54f3]385}
386
[da1bafb]387/** Forward a received call to another destination
388 *
389 * Common code for both the fast and the slow version.
390 *
391 * @param callid Hash of the call to forward.
392 * @param phoneid Phone handle to use for forwarding.
[228e490]393 * @param imethod New interface and method to use for the forwarded call.
[da1bafb]394 * @param arg1 New value of the first argument for the forwarded call.
395 * @param arg2 New value of the second argument for the forwarded call.
396 * @param arg3 New value of the third argument for the forwarded call.
397 * @param arg4 New value of the fourth argument for the forwarded call.
398 * @param arg5 New value of the fifth argument for the forwarded call.
399 * @param mode Flags that specify mode of the forward operation.
400 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
401 * the function considers only the fast version arguments:
402 * i.e. arg1 and arg2.
403 *
404 * @return 0 on succes, otherwise an error code.
405 *
406 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
407 *
[2ba7810]408 */
[96b02eb9]409static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t phoneid,
[228e490]410 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
[96b02eb9]411 sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
[2ba7810]412{
[da1bafb]413 call_t *call = get_call(callid);
[f9841e69]414 phone_t *phone;
415 bool need_old = answer_need_old(call);
416 bool after_forward = false;
417 ipc_data_t old;
[95a3082]418 int rc;
419
[2ba7810]420 if (!call)
421 return ENOENT;
[f9841e69]422
423 if (need_old)
424 old = call->data;
[48daf64]425
[c9fff17]426 if (phone_get(phoneid, &phone) != EOK) {
[95a3082]427 rc = ENOENT;
428 goto error;
[c9fff17]429 }
[da1bafb]430
[228e490]431 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
[95a3082]432 rc = EPERM;
433 goto error;
[2ba7810]434 }
[95a3082]435
436 call->flags |= IPC_CALL_FORWARDED;
[da1bafb]437
[0dc4258]438 /*
[4e5dabf]439 * User space is not allowed to change interface and method of system
[228e490]440 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
[4e5dabf]441 * means of imethod, arg1, arg2 and arg3.
[228e490]442 * If the interface and method is immutable, don't change anything.
[2ba7810]443 */
[228e490]444 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
445 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
446 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
[b61d47d]447 phone_dealloc(IPC_GET_ARG5(call->data));
[da1bafb]448
[228e490]449 IPC_SET_ARG1(call->data, imethod);
[0dc4258]450 IPC_SET_ARG2(call->data, arg1);
[b61d47d]451 IPC_SET_ARG3(call->data, arg2);
[da1bafb]452
[4e5dabf]453 if (slow)
[48daf64]454 IPC_SET_ARG4(call->data, arg3);
[4e5dabf]455
456 /*
457 * For system methods we deliberately don't
458 * overwrite ARG5.
459 */
[0dc4258]460 } else {
[228e490]461 IPC_SET_IMETHOD(call->data, imethod);
[0dc4258]462 IPC_SET_ARG1(call->data, arg1);
[b61d47d]463 IPC_SET_ARG2(call->data, arg2);
[48daf64]464 if (slow) {
465 IPC_SET_ARG3(call->data, arg3);
466 IPC_SET_ARG4(call->data, arg4);
467 IPC_SET_ARG5(call->data, arg5);
468 }
[0dc4258]469 }
[2ba7810]470 }
[da1bafb]471
[f9841e69]472 rc = ipc_forward(call, phone, &TASK->answerbox, mode);
473 if (rc != EOK) {
474 after_forward = true;
475 goto error;
476 }
[95a3082]477
[f9841e69]478 return EOK;
[95a3082]479
[f9841e69]480error:
[fcfa926b]481 IPC_SET_RETVAL(call->data, EFORWARD);
[95a3082]482 answer_preprocess(call, need_old ? &old : NULL);
[f9841e69]483 if (after_forward)
484 _ipc_answer_free_call(call, false);
485 else
486 ipc_answer(&TASK->answerbox, call);
487
[95a3082]488 return rc;
[2ba7810]489}
490
[48daf64]491/** Forward a received call to another destination - fast version.
492 *
[228e490]493 * In case the original interface and method is a system method, ARG1, ARG2
494 * and ARG3 are overwritten in the forwarded message with the new method and
495 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
496 * are rewritten with the new interface and method, arg1 and arg2, respectively.
497 * Also note there is a set of immutable methods, for which the new method and
498 * arguments are not set and these values are ignored.
[da1bafb]499 *
500 * @param callid Hash of the call to forward.
501 * @param phoneid Phone handle to use for forwarding.
[228e490]502 * @param imethod New interface and method to use for the forwarded call.
[da1bafb]503 * @param arg1 New value of the first argument for the forwarded call.
504 * @param arg2 New value of the second argument for the forwarded call.
505 * @param mode Flags that specify mode of the forward operation.
506 *
507 * @return 0 on succes, otherwise an error code.
508 *
[48daf64]509 */
[96b02eb9]510sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t phoneid,
[228e490]511 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[48daf64]512{
[228e490]513 return sys_ipc_forward_common(callid, phoneid, imethod, arg1, arg2, 0, 0,
[48daf64]514 0, mode, false);
515}
516
517/** Forward a received call to another destination - slow version.
518 *
519 * This function is the slow verision of the sys_ipc_forward_fast interface.
[228e490]520 * It can copy all five new arguments and the new interface and method from
521 * the userspace. It naturally extends the functionality of the fast version.
522 * For system methods, it additionally stores the new value of arg3 to ARG4.
523 * For non-system methods, it additionally stores the new value of arg3, arg4
524 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
[da1bafb]525 *
526 * @param callid Hash of the call to forward.
527 * @param phoneid Phone handle to use for forwarding.
528 * @param data Userspace address of the new IPC data.
529 * @param mode Flags that specify mode of the forward operation.
530 *
531 * @return 0 on succes, otherwise an error code.
532 *
[48daf64]533 */
[96b02eb9]534sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t phoneid,
[da1bafb]535 ipc_data_t *data, unsigned int mode)
[48daf64]536{
537 ipc_data_t newdata;
[da1bafb]538 int rc = copy_from_uspace(&newdata.args, &data->args,
[48daf64]539 sizeof(newdata.args));
[da1bafb]540 if (rc != 0)
[96b02eb9]541 return (sysarg_t) rc;
[da1bafb]542
[48daf64]543 return sys_ipc_forward_common(callid, phoneid,
[228e490]544 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
[48daf64]545 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
546 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
547}
548
[8b243f2]549/** Answer an IPC call - fast version.
550 *
551 * This function can handle only two return arguments of payload, but is faster
552 * than the generic sys_ipc_answer().
553 *
[da1bafb]554 * @param callid Hash of the call to be answered.
555 * @param retval Return value of the answer.
556 * @param arg1 Service-defined return value.
557 * @param arg2 Service-defined return value.
558 * @param arg3 Service-defined return value.
559 * @param arg4 Service-defined return value.
560 *
561 * @return 0 on success, otherwise an error code.
[8b243f2]562 *
563 */
[96b02eb9]564sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
565 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]566{
[897f2e76]567 /* Do not answer notification callids */
568 if (callid & IPC_CALLID_NOTIFICATION)
569 return 0;
[da1bafb]570
571 call_t *call = get_call(callid);
[2ba7810]572 if (!call)
573 return ENOENT;
[da1bafb]574
575 ipc_data_t saved_data;
576 bool saved;
577
[9f22213]578 if (answer_need_old(call)) {
[2ba7810]579 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]580 saved = true;
581 } else
582 saved = false;
583
[2d5a54f3]584 IPC_SET_RETVAL(call->data, retval);
585 IPC_SET_ARG1(call->data, arg1);
586 IPC_SET_ARG2(call->data, arg2);
[b74959bd]587 IPC_SET_ARG3(call->data, arg3);
588 IPC_SET_ARG4(call->data, arg4);
[da1bafb]589
[8498915]590 /*
591 * To achieve deterministic behavior, zero out arguments that are beyond
592 * the limits of the fast version.
593 */
594 IPC_SET_ARG5(call->data, 0);
[da1bafb]595 int rc = answer_preprocess(call, saved ? &saved_data : NULL);
596
[2d5a54f3]597 ipc_answer(&TASK->answerbox, call);
[7c23af9]598 return rc;
[2d5a54f3]599}
600
[8b243f2]601/** Answer an IPC call.
602 *
[da1bafb]603 * @param callid Hash of the call to be answered.
604 * @param data Userspace address of call data with the answer.
605 *
606 * @return 0 on success, otherwise an error code.
[8b243f2]607 *
608 */
[96b02eb9]609sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
[2d5a54f3]610{
[897f2e76]611 /* Do not answer notification callids */
612 if (callid & IPC_CALLID_NOTIFICATION)
613 return 0;
[da1bafb]614
615 call_t *call = get_call(callid);
[2ba7810]616 if (!call)
617 return ENOENT;
[da1bafb]618
619 ipc_data_t saved_data;
620 bool saved;
621
[9f22213]622 if (answer_need_old(call)) {
[2ba7810]623 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]624 saved = true;
625 } else
626 saved = false;
627
628 int rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]629 sizeof(call->data.args));
[e3c762cd]630 if (rc != 0)
631 return rc;
[da1bafb]632
633 rc = answer_preprocess(call, saved ? &saved_data : NULL);
[2d5a54f3]634
635 ipc_answer(&TASK->answerbox, call);
[7c23af9]636 return rc;
[2d5a54f3]637}
638
[8b243f2]639/** Hang up a phone.
[2d5a54f3]640 *
[da1bafb]641 * @param Phone handle of the phone to be hung up.
642 *
643 * @return 0 on success or an error code.
[8b243f2]644 *
[fbcfd458]645 */
[96b02eb9]646sysarg_t sys_ipc_hangup(sysarg_t phoneid)
[fbcfd458]647{
648 phone_t *phone;
[da1bafb]649
[c9fff17]650 if (phone_get(phoneid, &phone) != EOK)
651 return ENOENT;
[da1bafb]652
[d8f7362]653 if (ipc_phone_hangup(phone))
[fbcfd458]654 return -1;
[da1bafb]655
[fbcfd458]656 return 0;
657}
658
[8b243f2]659/** Wait for an incoming IPC call or an answer.
[fbcfd458]660 *
[da1bafb]661 * @param calldata Pointer to buffer where the call/answer data is stored.
662 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
663 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
664 * for explanation.
665 *
666 * @return Hash of the call.
667 * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
668 * call is a notification. IPC_CALLID_ANSWERED denotes an
669 * answer.
[bd5a663]670 *
[2d5a54f3]671 */
[96b02eb9]672sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
[da1bafb]673 unsigned int flags)
[2d5a54f3]674{
675 call_t *call;
[da1bafb]676
[741fd16]677restart:
[da1bafb]678
[741fd16]679#ifdef CONFIG_UDEBUG
680 udebug_stoppable_begin();
[da1bafb]681#endif
682
[51ec40f]683 call = ipc_wait_for_call(&TASK->answerbox, usec,
684 flags | SYNCH_FLAGS_INTERRUPTIBLE);
[da1bafb]685
[741fd16]686#ifdef CONFIG_UDEBUG
687 udebug_stoppable_end();
688#endif
[da1bafb]689
[9f22213]690 if (!call)
691 return 0;
[da1bafb]692
[5626277]693 if (call->flags & IPC_CALL_NOTIF) {
[43752b6]694 /* Set in_phone_hash to the interrupt counter */
[0c1a5d8a]695 call->data.phone = (void *) call->priv;
[43752b6]696
697 STRUCT_TO_USPACE(calldata, &call->data);
[da1bafb]698
[5626277]699 ipc_call_free(call);
700
[96b02eb9]701 return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
[5626277]702 }
[da1bafb]703
[2d5a54f3]704 if (call->flags & IPC_CALL_ANSWERED) {
[7c7aae16]705 process_answer(call);
[da1bafb]706
[fbcfd458]707 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
708 ipc_call_free(call);
709 goto restart;
710 }
[da1bafb]711
[fbcfd458]712 STRUCT_TO_USPACE(&calldata->args, &call->data.args);
[2d5a54f3]713 ipc_call_free(call);
[da1bafb]714
[96b02eb9]715 return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
[2d5a54f3]716 }
[da1bafb]717
[2d5a54f3]718 if (process_request(&TASK->answerbox, call))
719 goto restart;
[da1bafb]720
[7c7aae16]721 /* Include phone address('id') of the caller in the request,
722 * copy whole call->data, not only call->data.args */
[bd55bbb]723 if (STRUCT_TO_USPACE(calldata, &call->data)) {
[e06da7e]724 /*
725 * The callee will not receive this call and no one else has
726 * a chance to answer it. Reply with the EPARTY error code.
[b11ee88]727 */
[e06da7e]728 ipc_data_t saved_data;
[da1bafb]729 bool saved;
730
[e06da7e]731 if (answer_need_old(call)) {
732 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]733 saved = true;
734 } else
735 saved = false;
[e06da7e]736
737 IPC_SET_RETVAL(call->data, EPARTY);
[da1bafb]738 (void) answer_preprocess(call, saved ? &saved_data : NULL);
[e06da7e]739 ipc_answer(&TASK->answerbox, call);
[bd55bbb]740 return 0;
741 }
[da1bafb]742
[96b02eb9]743 return (sysarg_t) call;
[2d5a54f3]744}
[5626277]745
[da1bafb]746/** Interrupt one thread from sys_ipc_wait_for_call().
747 *
748 */
[96b02eb9]749sysarg_t sys_ipc_poke(void)
[057d21a]750{
[da1bafb]751 waitq_unsleep(&TASK->answerbox.wq);
[057d21a]752 return EOK;
753}
754
[8b243f2]755/** Connect an IRQ handler to a task.
[2b017ba]756 *
[228e490]757 * @param inr IRQ number.
758 * @param devno Device number.
759 * @param imethod Interface and method to be associated with the notification.
760 * @param ucode Uspace pointer to the top-half pseudocode.
[da1bafb]761 *
762 * @return EPERM or a return code returned by ipc_irq_register().
[2b017ba]763 *
764 */
[f044e96]765sysarg_t sys_irq_register(inr_t inr, devno_t devno, sysarg_t imethod,
[51ec40f]766 irq_code_t *ucode)
[5626277]767{
[2bb8648]768 if (!(cap_get(TASK) & CAP_IRQ_REG))
769 return EPERM;
[da1bafb]770
[228e490]771 return ipc_irq_register(&TASK->answerbox, inr, devno, imethod, ucode);
[5626277]772}
773
[8b243f2]774/** Disconnect an IRQ handler from a task.
775 *
[da1bafb]776 * @param inr IRQ number.
777 * @param devno Device number.
778 *
779 * @return Zero on success or EPERM on error.
[2b017ba]780 *
781 */
[f044e96]782sysarg_t sys_irq_unregister(inr_t inr, devno_t devno)
[5626277]783{
[2bb8648]784 if (!(cap_get(TASK) & CAP_IRQ_REG))
785 return EPERM;
[da1bafb]786
[2b017ba]787 ipc_irq_unregister(&TASK->answerbox, inr, devno);
[da1bafb]788
[5626277]789 return 0;
790}
[b45c443]791
[6b10dab]792#ifdef __32_BITS__
[9a1b20c]793
[6b10dab]794/** Syscall connect to a task by ID (32 bits)
[da1bafb]795 *
796 * @return Phone id on success, or negative error code.
[9a1b20c]797 *
798 */
[6b10dab]799sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
[9a1b20c]800{
801#ifdef CONFIG_UDEBUG
[6b10dab]802 sysarg64_t taskid;
803 int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
[9a1b20c]804 if (rc != 0)
[96b02eb9]805 return (sysarg_t) rc;
[da1bafb]806
[6b10dab]807 return ipc_connect_kbox((task_id_t) taskid);
[9a1b20c]808#else
[96b02eb9]809 return (sysarg_t) ENOTSUP;
[9a1b20c]810#endif
811}
812
[6b10dab]813#endif /* __32_BITS__ */
814
815#ifdef __64_BITS__
816
817/** Syscall connect to a task by ID (64 bits)
818 *
819 * @return Phone id on success, or negative error code.
820 *
821 */
822sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
823{
824#ifdef CONFIG_UDEBUG
825 return ipc_connect_kbox((task_id_t) taskid);
826#else
827 return (sysarg_t) ENOTSUP;
828#endif
829}
830
831#endif /* __64_BITS__ */
832
[cc73a8a1]833/** @}
[b45c443]834 */
Note: See TracBrowser for help on using the repository browser.