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

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

Remove unused connection cloning

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