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

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

Let kernel code get printf via the standard stdio header. Clean up unused includes.

  • Property mode set to 100644
File size: 24.2 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.
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 */
[6769005]388 call->data.answer_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 */
[6769005]432 call->data.answer_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))) {
[6769005]506 if (IPC_GET_IMETHOD(call->data) ==
507 IPC_M_CONNECT_TO_ME) {
508 kobject_put((kobject_t *) call->priv);
509 call->priv = 0;
510 cap_free(TASK,
511 (cap_handle_t) IPC_GET_ARG5(call->data));
512 }
[a35b458]513
[228e490]514 IPC_SET_ARG1(call->data, imethod);
[0dc4258]515 IPC_SET_ARG2(call->data, arg1);
[b61d47d]516 IPC_SET_ARG3(call->data, arg2);
[a35b458]517
[4e5dabf]518 if (slow)
[48daf64]519 IPC_SET_ARG4(call->data, arg3);
[a35b458]520
[4e5dabf]521 /*
522 * For system methods we deliberately don't
523 * overwrite ARG5.
524 */
[0dc4258]525 } else {
[228e490]526 IPC_SET_IMETHOD(call->data, imethod);
[0dc4258]527 IPC_SET_ARG1(call->data, arg1);
[b61d47d]528 IPC_SET_ARG2(call->data, arg2);
[48daf64]529 if (slow) {
530 IPC_SET_ARG3(call->data, arg3);
531 IPC_SET_ARG4(call->data, arg4);
532 IPC_SET_ARG5(call->data, arg5);
533 }
[0dc4258]534 }
[2ba7810]535 }
[a35b458]536
[01c3bb4]537 rc = ipc_forward(call, pkobj->phone, &TASK->answerbox, mode);
[f9841e69]538 if (rc != EOK) {
539 after_forward = true;
540 goto error;
541 }
[95a3082]542
[01c3bb4]543 cap_free(TASK, chandle);
544 kobject_put(ckobj);
545 kobject_put(pkobj);
[f9841e69]546 return EOK;
[95a3082]547
[f9841e69]548error:
[fcfa926b]549 IPC_SET_RETVAL(call->data, EFORWARD);
[9ef1b79b]550 (void) answer_preprocess(call, need_old ? &old : NULL);
[f9841e69]551 if (after_forward)
552 _ipc_answer_free_call(call, false);
553 else
554 ipc_answer(&TASK->answerbox, call);
555
[931afbc]556 cap_free(TASK, chandle);
557 kobject_put(ckobj);
[01c3bb4]558
559 if (pkobj)
560 kobject_put(pkobj);
[95a3082]561 return rc;
[2ba7810]562}
563
[48daf64]564/** Forward a received call to another destination - fast version.
565 *
[228e490]566 * In case the original interface and method is a system method, ARG1, ARG2
567 * and ARG3 are overwritten in the forwarded message with the new method and
568 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
569 * are rewritten with the new interface and method, arg1 and arg2, respectively.
570 * Also note there is a set of immutable methods, for which the new method and
571 * arguments are not set and these values are ignored.
[da1bafb]572 *
[01c3bb4]573 * @param chandle Call handle of the call to forward.
574 * @param phandle Phone handle to use for forwarding.
[48bcf49]575 * @param imethod New interface and method to use for the forwarded call.
576 * @param arg1 New value of the first argument for the forwarded call.
577 * @param arg2 New value of the second argument for the forwarded call.
578 * @param mode Flags that specify mode of the forward operation.
[da1bafb]579 *
580 * @return 0 on succes, otherwise an error code.
581 *
[48daf64]582 */
[eadaeae8]583sys_errno_t sys_ipc_forward_fast(cap_call_handle_t chandle,
584 cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
585 unsigned int mode)
[48daf64]586{
[01c3bb4]587 return sys_ipc_forward_common(chandle, phandle, imethod, arg1, arg2, 0,
588 0, 0, mode, false);
[48daf64]589}
590
591/** Forward a received call to another destination - slow version.
592 *
593 * This function is the slow verision of the sys_ipc_forward_fast interface.
[228e490]594 * It can copy all five new arguments and the new interface and method from
595 * the userspace. It naturally extends the functionality of the fast version.
596 * For system methods, it additionally stores the new value of arg3 to ARG4.
597 * For non-system methods, it additionally stores the new value of arg3, arg4
598 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
[da1bafb]599 *
[01c3bb4]600 * @param chandle Call handle of the call to forward.
601 * @param phandle Phone handle to use for forwarding.
602 * @param data Userspace address of the new IPC data.
603 * @param mode Flags that specify mode of the forward operation.
[da1bafb]604 *
605 * @return 0 on succes, otherwise an error code.
606 *
[48daf64]607 */
[eadaeae8]608sys_errno_t sys_ipc_forward_slow(cap_call_handle_t chandle,
609 cap_phone_handle_t phandle, ipc_data_t *data, unsigned int mode)
[48daf64]610{
611 ipc_data_t newdata;
[b7fd2a0]612 errno_t rc = copy_from_uspace(&newdata.args, &data->args,
[48daf64]613 sizeof(newdata.args));
[a53ed3a]614 if (rc != EOK)
[b7fd2a0]615 return (sys_errno_t) rc;
[a35b458]616
[01c3bb4]617 return sys_ipc_forward_common(chandle, phandle,
[228e490]618 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
[48daf64]619 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
[01c3bb4]620 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
[48daf64]621}
622
[8b243f2]623/** Answer an IPC call - fast version.
624 *
625 * This function can handle only two return arguments of payload, but is faster
626 * than the generic sys_ipc_answer().
627 *
[01c3bb4]628 * @param chandle Call handle to be answered.
629 * @param retval Return value of the answer.
630 * @param arg1 Service-defined return value.
631 * @param arg2 Service-defined return value.
632 * @param arg3 Service-defined return value.
633 * @param arg4 Service-defined return value.
[da1bafb]634 *
635 * @return 0 on success, otherwise an error code.
[8b243f2]636 *
637 */
[eadaeae8]638sys_errno_t sys_ipc_answer_fast(cap_call_handle_t chandle, sysarg_t retval,
639 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]640{
[01c3bb4]641 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
642 if (!kobj)
[2ba7810]643 return ENOENT;
[a35b458]644
[01c3bb4]645 call_t *call = kobj->call;
[931afbc]646 assert(!(call->flags & IPC_CALL_ANSWERED));
[01c3bb4]647
[da1bafb]648 ipc_data_t saved_data;
649 bool saved;
[a35b458]650
[9f22213]651 if (answer_need_old(call)) {
[2ba7810]652 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]653 saved = true;
654 } else
655 saved = false;
[a35b458]656
[2d5a54f3]657 IPC_SET_RETVAL(call->data, retval);
658 IPC_SET_ARG1(call->data, arg1);
659 IPC_SET_ARG2(call->data, arg2);
[b74959bd]660 IPC_SET_ARG3(call->data, arg3);
661 IPC_SET_ARG4(call->data, arg4);
[a35b458]662
[8498915]663 /*
664 * To achieve deterministic behavior, zero out arguments that are beyond
665 * the limits of the fast version.
666 */
667 IPC_SET_ARG5(call->data, 0);
[b7fd2a0]668 errno_t rc = answer_preprocess(call, saved ? &saved_data : NULL);
[a35b458]669
[2d5a54f3]670 ipc_answer(&TASK->answerbox, call);
[01c3bb4]671
672 kobject_put(kobj);
673 cap_free(TASK, chandle);
674
[7c23af9]675 return rc;
[2d5a54f3]676}
677
[8b243f2]678/** Answer an IPC call.
679 *
[01c3bb4]680 * @param chandle Call handle to be answered.
681 * @param data Userspace address of call data with the answer.
[da1bafb]682 *
683 * @return 0 on success, otherwise an error code.
[8b243f2]684 *
685 */
[eadaeae8]686sys_errno_t sys_ipc_answer_slow(cap_call_handle_t chandle, ipc_data_t *data)
[2d5a54f3]687{
[01c3bb4]688 kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
689 if (!kobj)
[2ba7810]690 return ENOENT;
[a35b458]691
[01c3bb4]692 call_t *call = kobj->call;
[931afbc]693 assert(!(call->flags & IPC_CALL_ANSWERED));
[01c3bb4]694
[da1bafb]695 ipc_data_t saved_data;
696 bool saved;
[a35b458]697
[9f22213]698 if (answer_need_old(call)) {
[2ba7810]699 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]700 saved = true;
701 } else
702 saved = false;
[a35b458]703
[1b20da0]704 errno_t rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]705 sizeof(call->data.args));
[a53ed3a]706 if (rc != EOK) {
[01c3bb4]707 /*
708 * Republish the capability so that the call does not get lost.
709 */
710 cap_publish(TASK, chandle, kobj);
[e3c762cd]711 return rc;
[01c3bb4]712 }
[a35b458]713
[da1bafb]714 rc = answer_preprocess(call, saved ? &saved_data : NULL);
[a35b458]715
[2d5a54f3]716 ipc_answer(&TASK->answerbox, call);
[01c3bb4]717
718 kobject_put(kobj);
719 cap_free(TASK, chandle);
720
[7c23af9]721 return rc;
[2d5a54f3]722}
723
[8b243f2]724/** Hang up a phone.
[2d5a54f3]725 *
[48bcf49]726 * @param handle Phone capability handle of the phone to be hung up.
[da1bafb]727 *
728 * @return 0 on success or an error code.
[8b243f2]729 *
[fbcfd458]730 */
[eadaeae8]731sys_errno_t sys_ipc_hangup(cap_phone_handle_t handle)
[fbcfd458]732{
[c25a39e]733 kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
[48bcf49]734 if (!kobj)
[c9fff17]735 return ENOENT;
[a35b458]736
[b7fd2a0]737 errno_t rc = ipc_phone_hangup(kobj->phone);
[48bcf49]738 kobject_put(kobj);
[c25a39e]739 cap_free(TASK, handle);
[7565a4b]740 return rc;
[fbcfd458]741}
742
[8b243f2]743/** Wait for an incoming IPC call or an answer.
[fbcfd458]744 *
[da1bafb]745 * @param calldata Pointer to buffer where the call/answer data is stored.
746 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
747 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
748 * for explanation.
749 *
[cde999a]750 * @return An error code on error.
[2d5a54f3]751 */
[b7fd2a0]752sys_errno_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
[da1bafb]753 unsigned int flags)
[2d5a54f3]754{
[acf6b55]755 call_t *call = NULL;
[a35b458]756
[741fd16]757restart:
[a35b458]758
[741fd16]759#ifdef CONFIG_UDEBUG
760 udebug_stoppable_begin();
[da1bafb]761#endif
[a35b458]762
[acf6b55]763 errno_t rc = ipc_wait_for_call(&TASK->answerbox, usec,
764 flags | SYNCH_FLAGS_INTERRUPTIBLE, &call);
[a35b458]765
[741fd16]766#ifdef CONFIG_UDEBUG
767 udebug_stoppable_end();
768#endif
[01c3bb4]769
[acf6b55]770 if (rc != EOK)
771 return rc;
772
773 assert(call);
[a35b458]774
[a1026da]775 call->data.flags = call->flags;
[5626277]776 if (call->flags & IPC_CALL_NOTIF) {
[6769005]777 /* Set the request_label to the interrupt counter */
778 call->data.request_label = (sysarg_t) call->priv;
[a35b458]779
[6deb2cd]780 call->data.cap_handle = CAP_NIL;
[503ffce]781
[43752b6]782 STRUCT_TO_USPACE(calldata, &call->data);
[d51a0d6]783 kobject_put(call->kobject);
[a35b458]784
[6deb2cd]785 return EOK;
[5626277]786 }
[a35b458]787
[2d5a54f3]788 if (call->flags & IPC_CALL_ANSWERED) {
[7c7aae16]789 process_answer(call);
[a35b458]790
[fbcfd458]791 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
[d51a0d6]792 kobject_put(call->kobject);
[fbcfd458]793 goto restart;
794 }
[503ffce]795
[6deb2cd]796 call->data.cap_handle = CAP_NIL;
[a35b458]797
[1d81eb6]798 STRUCT_TO_USPACE(calldata, &call->data);
[d51a0d6]799 kobject_put(call->kobject);
[a35b458]800
[6deb2cd]801 return EOK;
[2d5a54f3]802 }
[a35b458]803
[2d5a54f3]804 if (process_request(&TASK->answerbox, call))
805 goto restart;
[a35b458]806
[eadaeae8]807 cap_handle_t handle = CAP_NIL;
[acf6b55]808 rc = cap_alloc(TASK, &handle);
[09d01f2]809 if (rc != EOK) {
[01c3bb4]810 goto error;
[bd55bbb]811 }
[a35b458]812
[6deb2cd]813 call->data.cap_handle = handle;
814
[01c3bb4]815 /*
816 * Include phone hash of the caller in the request, copy the whole
817 * call->data, not only call->data.args.
818 */
819 rc = STRUCT_TO_USPACE(calldata, &call->data);
820 if (rc != EOK)
821 goto error;
822
823 kobject_add_ref(call->kobject);
824 cap_publish(TASK, handle, call->kobject);
[6deb2cd]825 return EOK;
[01c3bb4]826
827error:
[eadaeae8]828 if (CAP_HANDLE_VALID(handle))
[01c3bb4]829 cap_free(TASK, handle);
830
831 /*
832 * The callee will not receive this call and no one else has a chance to
[a1026da]833 * answer it. Set the IPC_CALL_AUTO_REPLY flag and return the EPARTY
834 * error code.
[01c3bb4]835 */
836 ipc_data_t saved_data;
837 bool saved;
838
839 if (answer_need_old(call)) {
840 memcpy(&saved_data, &call->data, sizeof(call->data));
841 saved = true;
842 } else
843 saved = false;
844
845 IPC_SET_RETVAL(call->data, EPARTY);
846 (void) answer_preprocess(call, saved ? &saved_data : NULL);
[a1026da]847 call->flags |= IPC_CALL_AUTO_REPLY;
[01c3bb4]848 ipc_answer(&TASK->answerbox, call);
849
850 return rc;
[2d5a54f3]851}
[5626277]852
[da1bafb]853/** Interrupt one thread from sys_ipc_wait_for_call().
854 *
855 */
[b7fd2a0]856sys_errno_t sys_ipc_poke(void)
[057d21a]857{
[96c30c8]858 waitq_wakeup(&TASK->answerbox.wq, WAKEUP_FIRST);
[057d21a]859 return EOK;
860}
861
[8b243f2]862/** Connect an IRQ handler to a task.
[2b017ba]863 *
[228e490]864 * @param inr IRQ number.
865 * @param imethod Interface and method to be associated with the notification.
866 * @param ucode Uspace pointer to the top-half pseudocode.
[da1bafb]867 *
[eadaeae8]868 * @param[out] uspace_handle Uspace pointer to IRQ capability handle
[9233e9d]869 *
[e9d15d9]870 * @return EPERM
871 * @return Error code returned by ipc_irq_subscribe().
[2b017ba]872 *
873 */
[eadaeae8]874sys_errno_t sys_ipc_irq_subscribe(inr_t inr, sysarg_t imethod,
875 irq_code_t *ucode, cap_irq_handle_t *uspace_handle)
[5626277]876{
[719a208]877 if (!(perm_get(TASK) & PERM_IRQ_REG))
[2bb8648]878 return EPERM;
[a35b458]879
[9233e9d]880 return ipc_irq_subscribe(&TASK->answerbox, inr, imethod, ucode, uspace_handle);
[5626277]881}
882
[8b243f2]883/** Disconnect an IRQ handler from a task.
884 *
[eadaeae8]885 * @param handle IRQ capability handle.
[da1bafb]886 *
887 * @return Zero on success or EPERM on error.
[2b017ba]888 *
889 */
[eadaeae8]890sys_errno_t sys_ipc_irq_unsubscribe(cap_irq_handle_t handle)
[5626277]891{
[719a208]892 if (!(perm_get(TASK) & PERM_IRQ_REG))
[2bb8648]893 return EPERM;
[a35b458]894
[eadaeae8]895 ipc_irq_unsubscribe(&TASK->answerbox, handle);
[a35b458]896
[5626277]897 return 0;
898}
[b45c443]899
[0016674]900/** Syscall connect to a task by ID
[da1bafb]901 *
[569a51a]902 * @return Error code.
[9a1b20c]903 *
904 */
[eadaeae8]905sys_errno_t sys_ipc_connect_kbox(task_id_t *uspace_taskid,
906 cap_phone_handle_t *uspace_phone)
[9a1b20c]907{
908#ifdef CONFIG_UDEBUG
[0016674]909 task_id_t taskid;
[eadaeae8]910 cap_phone_handle_t phone;
[a35b458]911
[b7fd2a0]912 errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(task_id_t));
[569a51a]913 if (rc == EOK) {
914 rc = ipc_connect_kbox((task_id_t) taskid, &phone);
915 }
[a35b458]916
[569a51a]917 if (rc == EOK) {
918 rc = copy_to_uspace(uspace_phone, &phone, sizeof(cap_handle_t));
[0016674]919 if (rc != EOK) {
920 // Clean up the phone on failure.
921 sys_ipc_hangup(phone);
922 }
[569a51a]923 }
[a35b458]924
[b7fd2a0]925 return (sys_errno_t) rc;
[6b10dab]926#else
[b7fd2a0]927 return (sys_errno_t) ENOTSUP;
[6b10dab]928#endif
929}
930
[cc73a8a1]931/** @}
[b45c443]932 */
Note: See TracBrowser for help on using the repository browser.