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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 15639ec was dd884cb, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix build with UDEBUG disabled.

  • Property mode set to 100644
File size: 24.4 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
[174156fd]29/** @addtogroup kernel_generic_ipc
[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>
[e2ab36f1]53#include <macros.h>
[01c3bb4]54#include <cap/cap.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;
[a35b458]70
[da1bafb]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
[6ff23ff]119/*
[8b243f2]120 * Functions that preprocess answer before sending it to the recepient.
[6ff23ff]121 */
[8b243f2]122
123/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
124 * for answer_preprocess().
125 *
[da1bafb]126 * @param call Call structure to be decided.
127 *
128 * @return true if the old call contents should be saved.
[8b243f2]129 *
[2d5a54f3]130 */
[da1bafb]131static inline bool answer_need_old(call_t *call)
[2d5a54f3]132{
[228e490]133 switch (IPC_GET_IMETHOD(call->data)) {
[7918fce]134 case IPC_M_CONNECT_TO_ME:
135 case IPC_M_CONNECT_ME_TO:
[072607b]136 case IPC_M_PAGE_IN:
[27d293a]137 case IPC_M_SHARE_OUT:
138 case IPC_M_SHARE_IN:
[36d852c]139 case IPC_M_DATA_WRITE:
140 case IPC_M_DATA_READ:
[fdd4898]141 case IPC_M_STATE_CHANGE_AUTHORIZE:
[da1bafb]142 return true;
[7918fce]143 default:
[da1bafb]144 return false;
[7918fce]145 }
[2d5a54f3]146}
147
[8b243f2]148/** Interpret process answer as control information.
149 *
150 * This function is called directly after sys_ipc_answer().
[46fc2f9]151 *
[da1bafb]152 * @param answer Call structure with the answer.
153 * @param olddata Saved data of the request.
154 *
[cde999a]155 * @return Return EOK on success or an error code.
[8b243f2]156 *
[46fc2f9]157 */
[b7fd2a0]158errno_t answer_preprocess(call_t *answer, ipc_data_t *olddata)
[2d5a54f3]159{
[b7fd2a0]160 errno_t rc = EOK;
[9956fad9]161
[2405bb5]162 spinlock_lock(&answer->forget_lock);
163 if (answer->forget) {
164 /*
165 * This is a forgotten call and answer->sender is not valid.
166 */
167 spinlock_unlock(&answer->forget_lock);
[b1e6269]168
[466e95f7]169 SYSIPC_OP(answer_cleanup, answer, olddata);
[2405bb5]170 return rc;
171 } else {
[63e27ef]172 assert(answer->active);
[20282ef3]173
174 /*
175 * Mark the call as inactive to prevent _ipc_answer_free_call()
176 * from attempting to remove the call from the active list
177 * itself.
178 */
179 answer->active = false;
180
181 /*
182 * Remove the call from the sender's active call list.
183 * We enforce this locking order so that any potential
184 * concurrently executing forget operation is forced to
185 * release its active_calls_lock and lose the race to
[1b20da0]186 * forget this soon to be answered call.
[20282ef3]187 */
188 spinlock_lock(&answer->sender->active_calls_lock);
189 list_remove(&answer->ta_link);
190 spinlock_unlock(&answer->sender->active_calls_lock);
[2405bb5]191 }
192 spinlock_unlock(&answer->forget_lock);
193
[b7fd2a0]194 if ((errno_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
[2541646]195 phone_t *phone = answer->caller_phone;
196 mutex_lock(&phone->lock);
197 if (phone->state == IPC_PHONE_CONNECTED) {
198 irq_spinlock_lock(&phone->callee->lock, true);
199 list_remove(&phone->link);
[3dab10ae]200 /* Drop callee->connected_phones reference */
201 kobject_put(phone->kobject);
[2541646]202 phone->state = IPC_PHONE_SLAMMED;
[6769005]203 phone->label = 0;
[2541646]204 irq_spinlock_unlock(&phone->callee->lock, true);
[ca687ad]205 }
[2541646]206 mutex_unlock(&phone->lock);
[9f22213]207 }
[a35b458]208
[53af6e8c]209 if (!olddata)
[9956fad9]210 return rc;
[e8039a86]211
[466e95f7]212 return SYSIPC_OP(answer_preprocess, answer, olddata);
[2d5a54f3]213}
214
[8b243f2]215/** Called before the request is sent.
216 *
[da1bafb]217 * @param call Call structure with the request.
218 * @param phone Phone that the call will be sent through.
219 *
220 * @return Return 0 on success, ELIMIT or EPERM on error.
[7c7aae16]221 *
222 */
[b7fd2a0]223static errno_t request_preprocess(call_t *call, phone_t *phone)
[7c7aae16]224{
[32e4643]225 call->request_method = IPC_GET_IMETHOD(call->data);
[466e95f7]226 return SYSIPC_OP(request_preprocess, call, phone);
[7c7aae16]227}
228
[6ff23ff]229/*
[8b243f2]230 * Functions called to process received call/answer before passing it to uspace.
[6ff23ff]231 */
[2d5a54f3]232
[8b243f2]233/** Do basic kernel processing of received call answer.
234 *
[da1bafb]235 * @param call Call structure with the answer.
236 *
[8b243f2]237 */
[7c7aae16]238static void process_answer(call_t *call)
[2d5a54f3]239{
[b7fd2a0]240 if (((errno_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
[51ec40f]241 (call->flags & IPC_CALL_FORWARDED))
[9f22213]242 IPC_SET_RETVAL(call->data, EFORWARD);
[a35b458]243
[466e95f7]244 SYSIPC_OP(answer_process, call);
[2d5a54f3]245}
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 *
[48bcf49]263 * @param handle Phone capability handle for the call.
[05ffb41]264 * @param data[inout] Structure with request/reply data.
265 * @param priv Value to be stored in call->priv.
[bc117a5]266 *
267 * @return EOK on success.
268 * @return ENOENT if there is no such phone handle.
[c222816]269 * @return ENOMEM if not enough memory to make the call
[bc117a5]270 *
271 */
[eadaeae8]272errno_t
273ipc_req_internal(cap_phone_handle_t handle, ipc_data_t *data, sysarg_t priv)
[bc117a5]274{
[48bcf49]275 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
276 if (!kobj->phone)
[bc117a5]277 return ENOENT;
[a35b458]278
[90efa3b]279 call_t *call = ipc_call_alloc();
[c222816]280 if (!call) {
281 kobject_put(kobj);
282 return ENOMEM;
283 }
284
[ae66564]285 call->priv = priv;
[bc117a5]286 memcpy(call->data.args, data->args, sizeof(data->args));
[a35b458]287
[b7fd2a0]288 errno_t rc = request_preprocess(call, kobj->phone);
[bc117a5]289 if (!rc) {
290#ifdef CONFIG_UDEBUG
291 udebug_stoppable_begin();
292#endif
293
[d51a0d6]294 kobject_add_ref(call->kobject);
[48bcf49]295 rc = ipc_call_sync(kobj->phone, call);
[43e2cbc]296 spinlock_lock(&call->forget_lock);
297 bool forgotten = call->forget;
298 spinlock_unlock(&call->forget_lock);
[d51a0d6]299 kobject_put(call->kobject);
[bc117a5]300
301#ifdef CONFIG_UDEBUG
302 udebug_stoppable_end();
303#endif
304
[43e2cbc]305 if (rc != EOK) {
306 if (!forgotten) {
307 /*
308 * There was an error, but it did not result
309 * in the call being forgotten. In fact, the
310 * call was not even sent. We are still
311 * its owners and are responsible for its
312 * deallocation.
313 */
[d51a0d6]314 kobject_put(call->kobject);
[43e2cbc]315 } else {
316 /*
317 * The call was forgotten and it changed hands.
318 * We are no longer expected to free it.
319 */
[63e27ef]320 assert(rc == EINTR);
[43e2cbc]321 }
[48bcf49]322 kobject_put(kobj);
323 return rc;
[43e2cbc]324 }
[bc117a5]325
326 process_answer(call);
327 } else
328 IPC_SET_RETVAL(call->data, rc);
[a35b458]329
[bc117a5]330 memcpy(data->args, call->data.args, sizeof(data->args));
[d51a0d6]331 kobject_put(call->kobject);
[48bcf49]332 kobject_put(kobj);
[a35b458]333
[bc117a5]334 return EOK;
335}
336
[97d17fe]337/** Check that the task did not exceed the allowed limit of asynchronous calls
338 * made over a phone.
[2d5a54f3]339 *
[228e490]340 * @param phone Phone to check the limit against.
341 *
[da1bafb]342 * @return 0 if limit not reached or -1 if limit exceeded.
343 *
[2d5a54f3]344 */
[97d17fe]345static int check_call_limit(phone_t *phone)
[2d5a54f3]346{
[036e97c]347 if (atomic_load(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
[2d5a54f3]348 return -1;
[a35b458]349
[2d5a54f3]350 return 0;
351}
352
[8b243f2]353/** Make a fast asynchronous call over IPC.
[2d5a54f3]354 *
[7c0e1f5]355 * This function can only handle three arguments of payload, but is faster than
[3209923]356 * the generic function sys_ipc_call_async_slow().
[8b243f2]357 *
[48bcf49]358 * @param handle Phone capability handle for the call.
359 * @param imethod Interface and method of the call.
360 * @param arg1 Service-defined payload argument.
361 * @param arg2 Service-defined payload argument.
362 * @param arg3 Service-defined payload argument.
[7c0e1f5]363 * @param label User-defined label.
[da1bafb]364 *
[d7e245a]365 * @return EOK on success.
[cde999a]366 * @return An error code on error.
[da1bafb]367 *
[2d5a54f3]368 */
[eadaeae8]369sys_errno_t sys_ipc_call_async_fast(cap_phone_handle_t handle, sysarg_t imethod,
[7c0e1f5]370 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t label)
[2d5a54f3]371{
[48bcf49]372 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
373 if (!kobj)
[d7e245a]374 return ENOENT;
[a35b458]375
[48bcf49]376 if (check_call_limit(kobj->phone)) {
377 kobject_put(kobj);
[d7e245a]378 return ELIMIT;
[48bcf49]379 }
[a35b458]380
[90efa3b]381 call_t *call = ipc_call_alloc();
[c222816]382 if (!call) {
383 kobject_put(kobj);
384 return ENOMEM;
385 }
386
[228e490]387 IPC_SET_IMETHOD(call->data, imethod);
[2d5a54f3]388 IPC_SET_ARG1(call->data, arg1);
389 IPC_SET_ARG2(call->data, arg2);
[3209923]390 IPC_SET_ARG3(call->data, arg3);
[a35b458]391
[8498915]392 /*
393 * To achieve deterministic behavior, zero out arguments that are beyond
394 * the limits of the fast version.
395 */
396 IPC_SET_ARG5(call->data, 0);
[7c0e1f5]397
398 /* Set the user-defined label */
[6769005]399 call->data.answer_label = label;
[a35b458]400
[b7fd2a0]401 errno_t res = request_preprocess(call, kobj->phone);
[a35b458]402
[da1bafb]403 if (!res)
[48bcf49]404 ipc_call(kobj->phone, call);
[7c7aae16]405 else
[48bcf49]406 ipc_backsend_err(kobj->phone, call, res);
[a35b458]407
[48bcf49]408 kobject_put(kobj);
[474c68b]409 return EOK;
[2d5a54f3]410}
411
[8b243f2]412/** Make an asynchronous IPC call allowing to transmit the entire payload.
413 *
[48bcf49]414 * @param handle Phone capability for the call.
[da1bafb]415 * @param data Userspace address of call data with the request.
[7c0e1f5]416 * @param label User-defined label.
[da1bafb]417 *
418 * @return See sys_ipc_call_async_fast().
[2d5a54f3]419 *
420 */
[eadaeae8]421sys_errno_t sys_ipc_call_async_slow(cap_phone_handle_t handle, ipc_data_t *data,
[7c0e1f5]422 sysarg_t label)
[2d5a54f3]423{
[48bcf49]424 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
425 if (!kobj)
[d7e245a]426 return ENOENT;
[4e49572]427
[48bcf49]428 if (check_call_limit(kobj->phone)) {
429 kobject_put(kobj);
[d7e245a]430 return ELIMIT;
[48bcf49]431 }
[97d17fe]432
[90efa3b]433 call_t *call = ipc_call_alloc();
[c222816]434 if (!call) {
435 kobject_put(kobj);
436 return ENOMEM;
437 }
438
[b7fd2a0]439 errno_t rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]440 sizeof(call->data.args));
[a53ed3a]441 if (rc != EOK) {
[d51a0d6]442 kobject_put(call->kobject);
[48bcf49]443 kobject_put(kobj);
[b7fd2a0]444 return (sys_errno_t) rc;
[f58af46]445 }
[7c0e1f5]446
447 /* Set the user-defined label */
[6769005]448 call->data.answer_label = label;
[a35b458]449
[b7fd2a0]450 errno_t res = request_preprocess(call, kobj->phone);
[a35b458]451
[da1bafb]452 if (!res)
[48bcf49]453 ipc_call(kobj->phone, call);
[7c7aae16]454 else
[48bcf49]455 ipc_backsend_err(kobj->phone, call, res);
[a35b458]456
[48bcf49]457 kobject_put(kobj);
[474c68b]458 return EOK;
[2d5a54f3]459}
460
[da1bafb]461/** Forward a received call to another destination
462 *
463 * Common code for both the fast and the slow version.
464 *
[01c3bb4]465 * @param chandle Call handle of the forwarded call.
466 * @param phandle Phone handle to use for forwarding.
[48bcf49]467 * @param imethod New interface and method to use for the forwarded call.
468 * @param arg1 New value of the first argument for the forwarded call.
469 * @param arg2 New value of the second argument for the forwarded call.
470 * @param arg3 New value of the third argument for the forwarded call.
471 * @param arg4 New value of the fourth argument for the forwarded call.
472 * @param arg5 New value of the fifth argument for the forwarded call.
473 * @param mode Flags that specify mode of the forward operation.
474 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
475 * the function considers only the fast version arguments:
476 * i.e. arg1 and arg2.
[da1bafb]477 *
478 * @return 0 on succes, otherwise an error code.
479 *
480 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
481 *
[2ba7810]482 */
[eadaeae8]483static sys_errno_t sys_ipc_forward_common(cap_call_handle_t chandle,
484 cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
485 sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
[2ba7810]486{
[01c3bb4]487 kobject_t *ckobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
488 if (!ckobj)
[2ba7810]489 return ENOENT;
[a35b458]490
[01c3bb4]491 call_t *call = ckobj->call;
492
[09024119]493 ipc_data_t old;
494 bool need_old = answer_need_old(call);
[f9841e69]495 if (need_old)
496 old = call->data;
[a35b458]497
[09024119]498 bool after_forward = false;
[b7fd2a0]499 errno_t rc;
[05ffb41]500
[01c3bb4]501 kobject_t *pkobj = kobject_get(TASK, phandle, KOBJECT_TYPE_PHONE);
502 if (!pkobj) {
[95a3082]503 rc = ENOENT;
504 goto error;
[c9fff17]505 }
[a35b458]506
[228e490]507 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
[95a3082]508 rc = EPERM;
509 goto error;
[2ba7810]510 }
[a35b458]511
[95a3082]512 call->flags |= IPC_CALL_FORWARDED;
[a35b458]513
[0dc4258]514 /*
[4e5dabf]515 * User space is not allowed to change interface and method of system
[228e490]516 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
[4e5dabf]517 * means of imethod, arg1, arg2 and arg3.
[228e490]518 * If the interface and method is immutable, don't change anything.
[2ba7810]519 */
[228e490]520 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
521 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
[6769005]522 if (IPC_GET_IMETHOD(call->data) ==
523 IPC_M_CONNECT_TO_ME) {
524 kobject_put((kobject_t *) call->priv);
525 call->priv = 0;
526 cap_free(TASK,
527 (cap_handle_t) IPC_GET_ARG5(call->data));
528 }
[a35b458]529
[228e490]530 IPC_SET_ARG1(call->data, imethod);
[0dc4258]531 IPC_SET_ARG2(call->data, arg1);
[b61d47d]532 IPC_SET_ARG3(call->data, arg2);
[a35b458]533
[4e5dabf]534 if (slow)
[48daf64]535 IPC_SET_ARG4(call->data, arg3);
[a35b458]536
[4e5dabf]537 /*
538 * For system methods we deliberately don't
539 * overwrite ARG5.
540 */
[0dc4258]541 } else {
[228e490]542 IPC_SET_IMETHOD(call->data, imethod);
[0dc4258]543 IPC_SET_ARG1(call->data, arg1);
[b61d47d]544 IPC_SET_ARG2(call->data, arg2);
[48daf64]545 if (slow) {
546 IPC_SET_ARG3(call->data, arg3);
547 IPC_SET_ARG4(call->data, arg4);
548 IPC_SET_ARG5(call->data, arg5);
549 }
[0dc4258]550 }
[2ba7810]551 }
[a35b458]552
[01c3bb4]553 rc = ipc_forward(call, pkobj->phone, &TASK->answerbox, mode);
[f9841e69]554 if (rc != EOK) {
555 after_forward = true;
556 goto error;
557 }
[95a3082]558
[01c3bb4]559 cap_free(TASK, chandle);
560 kobject_put(ckobj);
561 kobject_put(pkobj);
[f9841e69]562 return EOK;
[95a3082]563
[f9841e69]564error:
[fcfa926b]565 IPC_SET_RETVAL(call->data, EFORWARD);
[9ef1b79b]566 (void) answer_preprocess(call, need_old ? &old : NULL);
[f9841e69]567 if (after_forward)
568 _ipc_answer_free_call(call, false);
569 else
570 ipc_answer(&TASK->answerbox, call);
571
[931afbc]572 cap_free(TASK, chandle);
573 kobject_put(ckobj);
[01c3bb4]574
575 if (pkobj)
576 kobject_put(pkobj);
[95a3082]577 return rc;
[2ba7810]578}
579
[48daf64]580/** Forward a received call to another destination - fast version.
581 *
[228e490]582 * In case the original interface and method is a system method, ARG1, ARG2
583 * and ARG3 are overwritten in the forwarded message with the new method and
584 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
585 * are rewritten with the new interface and method, arg1 and arg2, respectively.
586 * Also note there is a set of immutable methods, for which the new method and
587 * arguments are not set and these values are ignored.
[da1bafb]588 *
[01c3bb4]589 * @param chandle Call handle of the call to forward.
590 * @param phandle Phone handle to use for forwarding.
[48bcf49]591 * @param imethod New interface and method to use for the forwarded call.
592 * @param arg1 New value of the first argument for the forwarded call.
593 * @param arg2 New value of the second argument for the forwarded call.
594 * @param mode Flags that specify mode of the forward operation.
[da1bafb]595 *
596 * @return 0 on succes, otherwise an error code.
597 *
[48daf64]598 */
[eadaeae8]599sys_errno_t sys_ipc_forward_fast(cap_call_handle_t chandle,
600 cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
601 unsigned int mode)
[48daf64]602{
[01c3bb4]603 return sys_ipc_forward_common(chandle, phandle, imethod, arg1, arg2, 0,
604 0, 0, mode, false);
[48daf64]605}
606
607/** Forward a received call to another destination - slow version.
608 *
609 * This function is the slow verision of the sys_ipc_forward_fast interface.
[228e490]610 * It can copy all five new arguments and the new interface and method from
611 * the userspace. It naturally extends the functionality of the fast version.
612 * For system methods, it additionally stores the new value of arg3 to ARG4.
613 * For non-system methods, it additionally stores the new value of arg3, arg4
614 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
[da1bafb]615 *
[01c3bb4]616 * @param chandle Call handle of the call to forward.
617 * @param phandle Phone handle to use for forwarding.
618 * @param data Userspace address of the new IPC data.
619 * @param mode Flags that specify mode of the forward operation.
[da1bafb]620 *
621 * @return 0 on succes, otherwise an error code.
622 *
[48daf64]623 */
[eadaeae8]624sys_errno_t sys_ipc_forward_slow(cap_call_handle_t chandle,
625 cap_phone_handle_t phandle, ipc_data_t *data, unsigned int mode)
[48daf64]626{
627 ipc_data_t newdata;
[b7fd2a0]628 errno_t rc = copy_from_uspace(&newdata.args, &data->args,
[48daf64]629 sizeof(newdata.args));
[a53ed3a]630 if (rc != EOK)
[b7fd2a0]631 return (sys_errno_t) rc;
[a35b458]632
[01c3bb4]633 return sys_ipc_forward_common(chandle, phandle,
[228e490]634 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
[48daf64]635 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
[01c3bb4]636 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
[48daf64]637}
638
[8b243f2]639/** Answer an IPC call - fast version.
640 *
641 * This function can handle only two return arguments of payload, but is faster
642 * than the generic sys_ipc_answer().
643 *
[01c3bb4]644 * @param chandle Call handle to be answered.
645 * @param retval Return value of the answer.
646 * @param arg1 Service-defined return value.
647 * @param arg2 Service-defined return value.
648 * @param arg3 Service-defined return value.
649 * @param arg4 Service-defined return value.
[da1bafb]650 *
651 * @return 0 on success, otherwise an error code.
[8b243f2]652 *
653 */
[eadaeae8]654sys_errno_t sys_ipc_answer_fast(cap_call_handle_t chandle, sysarg_t retval,
655 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]656{
[01c3bb4]657 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
658 if (!kobj)
[2ba7810]659 return ENOENT;
[a35b458]660
[01c3bb4]661 call_t *call = kobj->call;
[931afbc]662 assert(!(call->flags & IPC_CALL_ANSWERED));
[01c3bb4]663
[da1bafb]664 ipc_data_t saved_data;
665 bool saved;
[a35b458]666
[9f22213]667 if (answer_need_old(call)) {
[2ba7810]668 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]669 saved = true;
670 } else
671 saved = false;
[a35b458]672
[2d5a54f3]673 IPC_SET_RETVAL(call->data, retval);
674 IPC_SET_ARG1(call->data, arg1);
675 IPC_SET_ARG2(call->data, arg2);
[b74959bd]676 IPC_SET_ARG3(call->data, arg3);
677 IPC_SET_ARG4(call->data, arg4);
[a35b458]678
[8498915]679 /*
680 * To achieve deterministic behavior, zero out arguments that are beyond
681 * the limits of the fast version.
682 */
683 IPC_SET_ARG5(call->data, 0);
[b7fd2a0]684 errno_t rc = answer_preprocess(call, saved ? &saved_data : NULL);
[a35b458]685
[2d5a54f3]686 ipc_answer(&TASK->answerbox, call);
[01c3bb4]687
688 kobject_put(kobj);
689 cap_free(TASK, chandle);
690
[7c23af9]691 return rc;
[2d5a54f3]692}
693
[8b243f2]694/** Answer an IPC call.
695 *
[01c3bb4]696 * @param chandle Call handle to be answered.
697 * @param data Userspace address of call data with the answer.
[da1bafb]698 *
699 * @return 0 on success, otherwise an error code.
[8b243f2]700 *
701 */
[eadaeae8]702sys_errno_t sys_ipc_answer_slow(cap_call_handle_t chandle, ipc_data_t *data)
[2d5a54f3]703{
[01c3bb4]704 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
705 if (!kobj)
[2ba7810]706 return ENOENT;
[a35b458]707
[01c3bb4]708 call_t *call = kobj->call;
[931afbc]709 assert(!(call->flags & IPC_CALL_ANSWERED));
[01c3bb4]710
[da1bafb]711 ipc_data_t saved_data;
712 bool saved;
[a35b458]713
[9f22213]714 if (answer_need_old(call)) {
[2ba7810]715 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]716 saved = true;
717 } else
718 saved = false;
[a35b458]719
[1b20da0]720 errno_t rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]721 sizeof(call->data.args));
[a53ed3a]722 if (rc != EOK) {
[01c3bb4]723 /*
724 * Republish the capability so that the call does not get lost.
725 */
726 cap_publish(TASK, chandle, kobj);
[e3c762cd]727 return rc;
[01c3bb4]728 }
[a35b458]729
[da1bafb]730 rc = answer_preprocess(call, saved ? &saved_data : NULL);
[a35b458]731
[2d5a54f3]732 ipc_answer(&TASK->answerbox, call);
[01c3bb4]733
734 kobject_put(kobj);
735 cap_free(TASK, chandle);
736
[7c23af9]737 return rc;
[2d5a54f3]738}
739
[8b243f2]740/** Hang up a phone.
[2d5a54f3]741 *
[48bcf49]742 * @param handle Phone capability handle of the phone to be hung up.
[da1bafb]743 *
744 * @return 0 on success or an error code.
[8b243f2]745 *
[fbcfd458]746 */
[eadaeae8]747sys_errno_t sys_ipc_hangup(cap_phone_handle_t handle)
[fbcfd458]748{
[c25a39e]749 kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
[48bcf49]750 if (!kobj)
[c9fff17]751 return ENOENT;
[a35b458]752
[b7fd2a0]753 errno_t rc = ipc_phone_hangup(kobj->phone);
[48bcf49]754 kobject_put(kobj);
[c25a39e]755 cap_free(TASK, handle);
[7565a4b]756 return rc;
[fbcfd458]757}
758
[8b243f2]759/** Wait for an incoming IPC call or an answer.
[fbcfd458]760 *
[da1bafb]761 * @param calldata Pointer to buffer where the call/answer data is stored.
762 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
763 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
764 * for explanation.
765 *
[cde999a]766 * @return An error code on error.
[2d5a54f3]767 */
[b7fd2a0]768sys_errno_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
[da1bafb]769 unsigned int flags)
[2d5a54f3]770{
[acf6b55]771 call_t *call = NULL;
[dd884cb]772 errno_t rc;
[741fd16]773restart:
[a35b458]774
[741fd16]775#ifdef CONFIG_UDEBUG
776 udebug_stoppable_begin();
[da1bafb]777#endif
[a35b458]778
[dd884cb]779 rc = ipc_wait_for_call(&TASK->answerbox, usec,
[acf6b55]780 flags | SYNCH_FLAGS_INTERRUPTIBLE, &call);
[a35b458]781
[741fd16]782#ifdef CONFIG_UDEBUG
783 udebug_stoppable_end();
784#endif
[01c3bb4]785
[acf6b55]786 if (rc != EOK)
787 return rc;
788
789 assert(call);
[a35b458]790
[a1026da]791 call->data.flags = call->flags;
[5626277]792 if (call->flags & IPC_CALL_NOTIF) {
[6769005]793 /* Set the request_label to the interrupt counter */
794 call->data.request_label = (sysarg_t) call->priv;
[a35b458]795
[6deb2cd]796 call->data.cap_handle = CAP_NIL;
[503ffce]797
[43752b6]798 STRUCT_TO_USPACE(calldata, &call->data);
[d51a0d6]799 kobject_put(call->kobject);
[a35b458]800
[6deb2cd]801 return EOK;
[5626277]802 }
[a35b458]803
[2d5a54f3]804 if (call->flags & IPC_CALL_ANSWERED) {
[7c7aae16]805 process_answer(call);
[a35b458]806
[fbcfd458]807 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
[d51a0d6]808 kobject_put(call->kobject);
[fbcfd458]809 goto restart;
810 }
[503ffce]811
[6deb2cd]812 call->data.cap_handle = CAP_NIL;
[a35b458]813
[1d81eb6]814 STRUCT_TO_USPACE(calldata, &call->data);
[d51a0d6]815 kobject_put(call->kobject);
[a35b458]816
[6deb2cd]817 return EOK;
[2d5a54f3]818 }
[a35b458]819
[2d5a54f3]820 if (process_request(&TASK->answerbox, call))
821 goto restart;
[a35b458]822
[eadaeae8]823 cap_handle_t handle = CAP_NIL;
[acf6b55]824 rc = cap_alloc(TASK, &handle);
[09d01f2]825 if (rc != EOK) {
[01c3bb4]826 goto error;
[bd55bbb]827 }
[a35b458]828
[6deb2cd]829 call->data.cap_handle = handle;
830
[01c3bb4]831 /*
[5c03bd30]832 * Copy the whole call->data to include the request label.
[01c3bb4]833 */
834 rc = STRUCT_TO_USPACE(calldata, &call->data);
835 if (rc != EOK)
836 goto error;
837
838 kobject_add_ref(call->kobject);
839 cap_publish(TASK, handle, call->kobject);
[6deb2cd]840 return EOK;
[01c3bb4]841
842error:
[eadaeae8]843 if (CAP_HANDLE_VALID(handle))
[01c3bb4]844 cap_free(TASK, handle);
845
846 /*
847 * The callee will not receive this call and no one else has a chance to
[a1026da]848 * answer it. Set the IPC_CALL_AUTO_REPLY flag and return the EPARTY
849 * error code.
[01c3bb4]850 */
851 ipc_data_t saved_data;
852 bool saved;
853
854 if (answer_need_old(call)) {
855 memcpy(&saved_data, &call->data, sizeof(call->data));
856 saved = true;
857 } else
858 saved = false;
859
860 IPC_SET_RETVAL(call->data, EPARTY);
861 (void) answer_preprocess(call, saved ? &saved_data : NULL);
[a1026da]862 call->flags |= IPC_CALL_AUTO_REPLY;
[01c3bb4]863 ipc_answer(&TASK->answerbox, call);
864
865 return rc;
[2d5a54f3]866}
[5626277]867
[da1bafb]868/** Interrupt one thread from sys_ipc_wait_for_call().
869 *
870 */
[b7fd2a0]871sys_errno_t sys_ipc_poke(void)
[057d21a]872{
[96c30c8]873 waitq_wakeup(&TASK->answerbox.wq, WAKEUP_FIRST);
[057d21a]874 return EOK;
875}
876
[8b243f2]877/** Connect an IRQ handler to a task.
[2b017ba]878 *
[228e490]879 * @param inr IRQ number.
880 * @param imethod Interface and method to be associated with the notification.
881 * @param ucode Uspace pointer to the top-half pseudocode.
[da1bafb]882 *
[eadaeae8]883 * @param[out] uspace_handle Uspace pointer to IRQ capability handle
[9233e9d]884 *
[e9d15d9]885 * @return EPERM
886 * @return Error code returned by ipc_irq_subscribe().
[2b017ba]887 *
888 */
[eadaeae8]889sys_errno_t sys_ipc_irq_subscribe(inr_t inr, sysarg_t imethod,
890 irq_code_t *ucode, cap_irq_handle_t *uspace_handle)
[5626277]891{
[719a208]892 if (!(perm_get(TASK) & PERM_IRQ_REG))
[2bb8648]893 return EPERM;
[a35b458]894
[9233e9d]895 return ipc_irq_subscribe(&TASK->answerbox, inr, imethod, ucode, uspace_handle);
[5626277]896}
897
[8b243f2]898/** Disconnect an IRQ handler from a task.
899 *
[eadaeae8]900 * @param handle IRQ capability handle.
[da1bafb]901 *
902 * @return Zero on success or EPERM on error.
[2b017ba]903 *
904 */
[eadaeae8]905sys_errno_t sys_ipc_irq_unsubscribe(cap_irq_handle_t handle)
[5626277]906{
[719a208]907 if (!(perm_get(TASK) & PERM_IRQ_REG))
[2bb8648]908 return EPERM;
[a35b458]909
[eadaeae8]910 ipc_irq_unsubscribe(&TASK->answerbox, handle);
[a35b458]911
[5626277]912 return 0;
913}
[b45c443]914
[0016674]915/** Syscall connect to a task by ID
[da1bafb]916 *
[569a51a]917 * @return Error code.
[9a1b20c]918 *
919 */
[eadaeae8]920sys_errno_t sys_ipc_connect_kbox(task_id_t *uspace_taskid,
921 cap_phone_handle_t *uspace_phone)
[9a1b20c]922{
923#ifdef CONFIG_UDEBUG
[0016674]924 task_id_t taskid;
[eadaeae8]925 cap_phone_handle_t phone;
[a35b458]926
[b7fd2a0]927 errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(task_id_t));
[569a51a]928 if (rc == EOK) {
929 rc = ipc_connect_kbox((task_id_t) taskid, &phone);
930 }
[a35b458]931
[569a51a]932 if (rc == EOK) {
933 rc = copy_to_uspace(uspace_phone, &phone, sizeof(cap_handle_t));
[0016674]934 if (rc != EOK) {
935 // Clean up the phone on failure.
936 sys_ipc_hangup(phone);
937 }
[569a51a]938 }
[a35b458]939
[b7fd2a0]940 return (sys_errno_t) rc;
[6b10dab]941#else
[b7fd2a0]942 return (sys_errno_t) ENOTSUP;
[6b10dab]943#endif
944}
945
[cc73a8a1]946/** @}
[b45c443]947 */
Note: See TracBrowser for help on using the repository browser.