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

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

Partially revert jakub@…

This changeset reintroduces portions of the synchronous IPC mechanism for
internal use by the kernel / user_backend. The synchronous IPC is not
exported to user space and is simpler than in its original form.

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