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

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

Fix vertical spacing with new Ccheck revision.

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