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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 036e97c was 036e97c, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Convert atomic_t to atomic_size_t (3): Use atomic_load instead of atomic_get

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