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

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

Make sure that both dispatched and non-dispatched calls are properly
answered in ipc_cleanup_call_list().

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