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

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

Add SYSIPC_OP macro to avoid repeating the same boilerplate code.

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