source: mainline/kernel/generic/src/ipc/sysipc.c@ 7c0e1f5

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

Rework userspace call tracking

Setting the address of the userspace call structure in the kernel
call_t structure on send allows us to remove lots of userspace
scaffolding. More importantly, it also opens the door for not needing
the callid (later capability) on answer receive.

  • Property mode set to 100644
File size: 23.6 KB
RevLine 
[2d5a54f3]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[2d5a54f3]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[cc73a8a1]29/** @addtogroup genericipc
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[2d5a54f3]35#include <arch.h>
[63e27ef]36#include <assert.h>
[2d5a54f3]37#include <errno.h>
[44a7ee5]38#include <mem.h>
[2d5a54f3]39#include <ipc/ipc.h>
[c0699467]40#include <abi/ipc/methods.h>
[2d5a54f3]41#include <ipc/sysipc.h>
[e8039a86]42#include <ipc/sysipc_ops.h>
[5d3ed34]43#include <ipc/sysipc_priv.h>
[162f919]44#include <ipc/irq.h>
[4e49572]45#include <ipc/ipcrsc.h>
[fdd4898]46#include <ipc/event.h>
[e028660]47#include <ipc/kbox.h>
[057d21a]48#include <synch/waitq.h>
[5626277]49#include <arch/interrupt.h>
[e3c762cd]50#include <syscall/copy.h>
[719a208]51#include <security/perm.h>
[6b10dab]52#include <console/console.h>
[253f35a1]53#include <print.h>
[e2ab36f1]54#include <macros.h>
[2d5a54f3]55
[da1bafb]56#define STRUCT_TO_USPACE(dst, src) copy_to_uspace((dst), (src), sizeof(*(src)))
[7918fce]57
[228e490]58/** Decide if the interface and method is a system method.
[8b243f2]59 *
[228e490]60 * @param imethod Interface and method to be decided.
[da1bafb]61 *
[228e490]62 * @return True if the interface and method is a system
63 * interface and method.
[8b243f2]64 *
65 */
[228e490]66static inline bool method_is_system(sysarg_t imethod)
[2ba7810]67{
[228e490]68 if (imethod <= IPC_M_LAST_SYSTEM)
[da1bafb]69 return true;
70
71 return false;
[2ba7810]72}
73
[228e490]74/** Decide if the message with this interface and method is forwardable.
[2ba7810]75 *
[228e490]76 * Some system messages may be forwarded, for some of them
77 * it is useless.
[8b243f2]78 *
[228e490]79 * @param imethod Interface and method to be decided.
[da1bafb]80 *
[228e490]81 * @return True if the interface and method is forwardable.
[8b243f2]82 *
[2ba7810]83 */
[228e490]84static inline bool method_is_forwardable(sysarg_t imethod)
[2ba7810]85{
[228e490]86 switch (imethod) {
[7918fce]87 case IPC_M_PHONE_HUNGUP:
88 /* This message is meant only for the original recipient. */
[da1bafb]89 return false;
[7918fce]90 default:
[da1bafb]91 return true;
[7918fce]92 }
[2ba7810]93}
94
[228e490]95/** Decide if the message with this interface and method is immutable on forward.
[0dc4258]96 *
[228e490]97 * Some system messages may be forwarded but their content cannot be altered.
[0dc4258]98 *
[228e490]99 * @param imethod Interface and method to be decided.
[da1bafb]100 *
[228e490]101 * @return True if the interface and method is immutable on forward.
[0dc4258]102 *
103 */
[228e490]104static inline bool method_is_immutable(sysarg_t imethod)
[0dc4258]105{
[228e490]106 switch (imethod) {
[072607b]107 case IPC_M_PAGE_IN:
[27d293a]108 case IPC_M_SHARE_OUT:
109 case IPC_M_SHARE_IN:
[36d852c]110 case IPC_M_DATA_WRITE:
111 case IPC_M_DATA_READ:
[fdd4898]112 case IPC_M_STATE_CHANGE_AUTHORIZE:
[da1bafb]113 return true;
[0dc4258]114 default:
[da1bafb]115 return false;
[0dc4258]116 }
117}
118
[2d5a54f3]119
[8b243f2]120/***********************************************************************
121 * Functions that preprocess answer before sending it to the recepient.
122 ***********************************************************************/
123
124/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
125 * for answer_preprocess().
126 *
[da1bafb]127 * @param call Call structure to be decided.
128 *
129 * @return true if the old call contents should be saved.
[8b243f2]130 *
[2d5a54f3]131 */
[da1bafb]132static inline bool answer_need_old(call_t *call)
[2d5a54f3]133{
[228e490]134 switch (IPC_GET_IMETHOD(call->data)) {
[7918fce]135 case IPC_M_CONNECT_TO_ME:
136 case IPC_M_CONNECT_ME_TO:
[072607b]137 case IPC_M_PAGE_IN:
[27d293a]138 case IPC_M_SHARE_OUT:
139 case IPC_M_SHARE_IN:
[36d852c]140 case IPC_M_DATA_WRITE:
141 case IPC_M_DATA_READ:
[fdd4898]142 case IPC_M_STATE_CHANGE_AUTHORIZE:
[da1bafb]143 return true;
[7918fce]144 default:
[da1bafb]145 return false;
[7918fce]146 }
[2d5a54f3]147}
148
[8b243f2]149/** Interpret process answer as control information.
150 *
151 * This function is called directly after sys_ipc_answer().
[46fc2f9]152 *
[da1bafb]153 * @param answer Call structure with the answer.
154 * @param olddata Saved data of the request.
155 *
[9956fad9]156 * @return Return EOK on success or a negative error code.
[8b243f2]157 *
[46fc2f9]158 */
[5d3ed34]159int answer_preprocess(call_t *answer, ipc_data_t *olddata)
[2d5a54f3]160{
[9956fad9]161 int rc = EOK;
162
[2405bb5]163 spinlock_lock(&answer->forget_lock);
164 if (answer->forget) {
165 /*
166 * This is a forgotten call and answer->sender is not valid.
167 */
168 spinlock_unlock(&answer->forget_lock);
[b1e6269]169
[466e95f7]170 SYSIPC_OP(answer_cleanup, answer, olddata);
[2405bb5]171 return rc;
172 } else {
[63e27ef]173 assert(answer->active);
[20282ef3]174
175 /*
176 * Mark the call as inactive to prevent _ipc_answer_free_call()
177 * from attempting to remove the call from the active list
178 * itself.
179 */
180 answer->active = false;
181
182 /*
183 * Remove the call from the sender's active call list.
184 * We enforce this locking order so that any potential
185 * concurrently executing forget operation is forced to
186 * release its active_calls_lock and lose the race to
187 * forget this soon to be answered call.
188 */
189 spinlock_lock(&answer->sender->active_calls_lock);
190 list_remove(&answer->ta_link);
191 spinlock_unlock(&answer->sender->active_calls_lock);
[2405bb5]192 }
193 spinlock_unlock(&answer->forget_lock);
194
[6c441cf8]195 if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
[2541646]196 phone_t *phone = answer->caller_phone;
197 mutex_lock(&phone->lock);
198 if (phone->state == IPC_PHONE_CONNECTED) {
199 irq_spinlock_lock(&phone->callee->lock, true);
200 list_remove(&phone->link);
[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 }
[da1bafb]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 */
[9a1b20c]223static int 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
[8b243f2]229/*******************************************************************************
230 * Functions called to process received call/answer before passing it to uspace.
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{
[6c441cf8]240 if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
[51ec40f]241 (call->flags & IPC_CALL_FORWARDED))
[9f22213]242 IPC_SET_RETVAL(call->data, EFORWARD);
[da1bafb]243
[466e95f7]244 SYSIPC_OP(answer_process, call);
[2d5a54f3]245}
246
[691d8d8]247
[8b243f2]248/** Do basic kernel processing of received call request.
249 *
[da1bafb]250 * @param box Destination answerbox structure.
251 * @param call Call structure with the request.
252 *
253 * @return 0 if the call should be passed to userspace.
254 * @return -1 if the call should be ignored.
[2d5a54f3]255 *
256 */
[51ec40f]257static int process_request(answerbox_t *box, call_t *call)
[2d5a54f3]258{
[466e95f7]259 return SYSIPC_OP(request_process, call, box);
[2d5a54f3]260}
261
[bc117a5]262/** Make a call over IPC and wait for reply.
263 *
[48bcf49]264 * @param handle Phone capability handle for the call.
[05ffb41]265 * @param data[inout] Structure with request/reply data.
266 * @param priv Value to be stored in call->priv.
[bc117a5]267 *
268 * @return EOK on success.
269 * @return ENOENT if there is no such phone handle.
270 *
271 */
[48bcf49]272int ipc_req_internal(cap_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;
277
278 call_t *call = ipc_call_alloc(0);
[ae66564]279 call->priv = priv;
[bc117a5]280 memcpy(call->data.args, data->args, sizeof(data->args));
281
[48bcf49]282 int 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);
323
324 memcpy(data->args, call->data.args, sizeof(data->args));
[d51a0d6]325 kobject_put(call->kobject);
[48bcf49]326 kobject_put(kobj);
[bc117a5]327
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{
[97d17fe]341 if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
[2d5a54f3]342 return -1;
[da1bafb]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 *
359 * @return Call hash on success.
360 * @return IPC_CALLRET_FATAL in case of a fatal error.
361 *
[2d5a54f3]362 */
[48bcf49]363sysarg_t sys_ipc_call_async_fast(sysarg_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)
[c9fff17]368 return IPC_CALLRET_FATAL;
[228e490]369
[48bcf49]370 if (check_call_limit(kobj->phone)) {
371 kobject_put(kobj);
[d432c02]372 return IPC_CALLRET_FATAL;
[48bcf49]373 }
[da1bafb]374
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);
[da1bafb]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;
[da1bafb]389
[48bcf49]390 int res = request_preprocess(call, kobj->phone);
[da1bafb]391
392 if (!res)
[48bcf49]393 ipc_call(kobj->phone, call);
[7c7aae16]394 else
[48bcf49]395 ipc_backsend_err(kobj->phone, call, res);
[da1bafb]396
[48bcf49]397 kobject_put(kobj);
[96b02eb9]398 return (sysarg_t) call;
[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 */
[7c0e1f5]410sysarg_t sys_ipc_call_async_slow(sysarg_t handle, ipc_data_t *data,
411 sysarg_t label)
[2d5a54f3]412{
[48bcf49]413 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
414 if (!kobj)
[c9fff17]415 return IPC_CALLRET_FATAL;
[4e49572]416
[48bcf49]417 if (check_call_limit(kobj->phone)) {
418 kobject_put(kobj);
[d432c02]419 return IPC_CALLRET_FATAL;
[48bcf49]420 }
[97d17fe]421
[da1bafb]422 call_t *call = ipc_call_alloc(0);
423 int rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]424 sizeof(call->data.args));
[f58af46]425 if (rc != 0) {
[d51a0d6]426 kobject_put(call->kobject);
[48bcf49]427 kobject_put(kobj);
[96b02eb9]428 return (sysarg_t) rc;
[f58af46]429 }
[7c0e1f5]430
431 /* Set the user-defined label */
432 call->data.label = label;
[da1bafb]433
[48bcf49]434 int res = request_preprocess(call, kobj->phone);
[da1bafb]435
436 if (!res)
[48bcf49]437 ipc_call(kobj->phone, call);
[7c7aae16]438 else
[48bcf49]439 ipc_backsend_err(kobj->phone, call, res);
[da1bafb]440
[48bcf49]441 kobject_put(kobj);
[96b02eb9]442 return (sysarg_t) call;
[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 *
[48bcf49]449 * @param callid Hash of the call to forward.
450 * @param handle Phone capability to use for forwarding.
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 */
[48bcf49]467static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t handle,
[228e490]468 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
[96b02eb9]469 sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
[2ba7810]470{
[da1bafb]471 call_t *call = get_call(callid);
[2ba7810]472 if (!call)
473 return ENOENT;
[09024119]474
475 ipc_data_t old;
476 bool need_old = answer_need_old(call);
[f9841e69]477 if (need_old)
478 old = call->data;
[48daf64]479
[09024119]480 bool after_forward = false;
481 int rc;
[05ffb41]482
[48bcf49]483 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
484 if (!kobj) {
[95a3082]485 rc = ENOENT;
486 goto error;
[c9fff17]487 }
[da1bafb]488
[228e490]489 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
[95a3082]490 rc = EPERM;
491 goto error;
[2ba7810]492 }
[09024119]493
[95a3082]494 call->flags |= IPC_CALL_FORWARDED;
[da1bafb]495
[0dc4258]496 /*
[4e5dabf]497 * User space is not allowed to change interface and method of system
[228e490]498 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
[4e5dabf]499 * means of imethod, arg1, arg2 and arg3.
[228e490]500 * If the interface and method is immutable, don't change anything.
[2ba7810]501 */
[228e490]502 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
503 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
504 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
[b61d47d]505 phone_dealloc(IPC_GET_ARG5(call->data));
[da1bafb]506
[228e490]507 IPC_SET_ARG1(call->data, imethod);
[0dc4258]508 IPC_SET_ARG2(call->data, arg1);
[b61d47d]509 IPC_SET_ARG3(call->data, arg2);
[da1bafb]510
[4e5dabf]511 if (slow)
[48daf64]512 IPC_SET_ARG4(call->data, arg3);
[4e5dabf]513
514 /*
515 * For system methods we deliberately don't
516 * overwrite ARG5.
517 */
[0dc4258]518 } else {
[228e490]519 IPC_SET_IMETHOD(call->data, imethod);
[0dc4258]520 IPC_SET_ARG1(call->data, arg1);
[b61d47d]521 IPC_SET_ARG2(call->data, arg2);
[48daf64]522 if (slow) {
523 IPC_SET_ARG3(call->data, arg3);
524 IPC_SET_ARG4(call->data, arg4);
525 IPC_SET_ARG5(call->data, arg5);
526 }
[0dc4258]527 }
[2ba7810]528 }
[da1bafb]529
[48bcf49]530 rc = ipc_forward(call, kobj->phone, &TASK->answerbox, mode);
[f9841e69]531 if (rc != EOK) {
532 after_forward = true;
533 goto error;
534 }
[95a3082]535
[48bcf49]536 kobject_put(kobj);
[f9841e69]537 return EOK;
[95a3082]538
[f9841e69]539error:
[fcfa926b]540 IPC_SET_RETVAL(call->data, EFORWARD);
[9ef1b79b]541 (void) answer_preprocess(call, need_old ? &old : NULL);
[f9841e69]542 if (after_forward)
543 _ipc_answer_free_call(call, false);
544 else
545 ipc_answer(&TASK->answerbox, call);
546
[48bcf49]547 if (kobj)
548 kobject_put(kobj);
[95a3082]549 return rc;
[2ba7810]550}
551
[48daf64]552/** Forward a received call to another destination - fast version.
553 *
[228e490]554 * In case the original interface and method is a system method, ARG1, ARG2
555 * and ARG3 are overwritten in the forwarded message with the new method and
556 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
557 * are rewritten with the new interface and method, arg1 and arg2, respectively.
558 * Also note there is a set of immutable methods, for which the new method and
559 * arguments are not set and these values are ignored.
[da1bafb]560 *
[48bcf49]561 * @param callid Hash of the call to forward.
562 * @param handle Phone handle to use for forwarding.
563 * @param imethod New interface and method to use for the forwarded call.
564 * @param arg1 New value of the first argument for the forwarded call.
565 * @param arg2 New value of the second argument for the forwarded call.
566 * @param mode Flags that specify mode of the forward operation.
[da1bafb]567 *
568 * @return 0 on succes, otherwise an error code.
569 *
[48daf64]570 */
[48bcf49]571sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t handle,
[228e490]572 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[48daf64]573{
[48bcf49]574 return sys_ipc_forward_common(callid, handle, imethod, arg1, arg2, 0, 0,
[48daf64]575 0, mode, false);
576}
577
578/** Forward a received call to another destination - slow version.
579 *
580 * This function is the slow verision of the sys_ipc_forward_fast interface.
[228e490]581 * It can copy all five new arguments and the new interface and method from
582 * the userspace. It naturally extends the functionality of the fast version.
583 * For system methods, it additionally stores the new value of arg3 to ARG4.
584 * For non-system methods, it additionally stores the new value of arg3, arg4
585 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
[da1bafb]586 *
587 * @param callid Hash of the call to forward.
[48bcf49]588 * @param handle Phone handle to use for forwarding.
[da1bafb]589 * @param data Userspace address of the new IPC data.
590 * @param mode Flags that specify mode of the forward operation.
591 *
592 * @return 0 on succes, otherwise an error code.
593 *
[48daf64]594 */
[48bcf49]595sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t handle,
[da1bafb]596 ipc_data_t *data, unsigned int mode)
[48daf64]597{
598 ipc_data_t newdata;
[da1bafb]599 int rc = copy_from_uspace(&newdata.args, &data->args,
[48daf64]600 sizeof(newdata.args));
[da1bafb]601 if (rc != 0)
[96b02eb9]602 return (sysarg_t) rc;
[da1bafb]603
[48bcf49]604 return sys_ipc_forward_common(callid, handle,
[228e490]605 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
[48daf64]606 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
607 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
608}
609
[8b243f2]610/** Answer an IPC call - fast version.
611 *
612 * This function can handle only two return arguments of payload, but is faster
613 * than the generic sys_ipc_answer().
614 *
[da1bafb]615 * @param callid Hash of the call to be answered.
616 * @param retval Return value of the answer.
617 * @param arg1 Service-defined return value.
618 * @param arg2 Service-defined return value.
619 * @param arg3 Service-defined return value.
620 * @param arg4 Service-defined return value.
621 *
622 * @return 0 on success, otherwise an error code.
[8b243f2]623 *
624 */
[96b02eb9]625sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
626 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]627{
[897f2e76]628 /* Do not answer notification callids */
629 if (callid & IPC_CALLID_NOTIFICATION)
630 return 0;
[da1bafb]631
632 call_t *call = get_call(callid);
[2ba7810]633 if (!call)
634 return ENOENT;
[da1bafb]635
636 ipc_data_t saved_data;
637 bool saved;
638
[9f22213]639 if (answer_need_old(call)) {
[2ba7810]640 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]641 saved = true;
642 } else
643 saved = false;
644
[2d5a54f3]645 IPC_SET_RETVAL(call->data, retval);
646 IPC_SET_ARG1(call->data, arg1);
647 IPC_SET_ARG2(call->data, arg2);
[b74959bd]648 IPC_SET_ARG3(call->data, arg3);
649 IPC_SET_ARG4(call->data, arg4);
[da1bafb]650
[8498915]651 /*
652 * To achieve deterministic behavior, zero out arguments that are beyond
653 * the limits of the fast version.
654 */
655 IPC_SET_ARG5(call->data, 0);
[da1bafb]656 int rc = answer_preprocess(call, saved ? &saved_data : NULL);
657
[2d5a54f3]658 ipc_answer(&TASK->answerbox, call);
[7c23af9]659 return rc;
[2d5a54f3]660}
661
[8b243f2]662/** Answer an IPC call.
663 *
[da1bafb]664 * @param callid Hash of the call to be answered.
665 * @param data Userspace address of call data with the answer.
666 *
667 * @return 0 on success, otherwise an error code.
[8b243f2]668 *
669 */
[96b02eb9]670sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
[2d5a54f3]671{
[897f2e76]672 /* Do not answer notification callids */
673 if (callid & IPC_CALLID_NOTIFICATION)
674 return 0;
[da1bafb]675
676 call_t *call = get_call(callid);
[2ba7810]677 if (!call)
678 return ENOENT;
[da1bafb]679
680 ipc_data_t saved_data;
681 bool saved;
682
[9f22213]683 if (answer_need_old(call)) {
[2ba7810]684 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]685 saved = true;
686 } else
687 saved = false;
688
689 int rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]690 sizeof(call->data.args));
[e3c762cd]691 if (rc != 0)
692 return rc;
[da1bafb]693
694 rc = answer_preprocess(call, saved ? &saved_data : NULL);
[2d5a54f3]695
696 ipc_answer(&TASK->answerbox, call);
[7c23af9]697 return rc;
[2d5a54f3]698}
699
[8b243f2]700/** Hang up a phone.
[2d5a54f3]701 *
[48bcf49]702 * @param handle Phone capability handle of the phone to be hung up.
[da1bafb]703 *
704 * @return 0 on success or an error code.
[8b243f2]705 *
[fbcfd458]706 */
[48bcf49]707sysarg_t sys_ipc_hangup(sysarg_t handle)
[fbcfd458]708{
[48bcf49]709 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
710 if (!kobj)
[c9fff17]711 return ENOENT;
[da1bafb]712
[48bcf49]713 if (ipc_phone_hangup(kobj->phone)) {
714 kobject_put(kobj);
[fbcfd458]715 return -1;
[48bcf49]716 }
[da1bafb]717
[48bcf49]718 kobject_put(kobj);
[fbcfd458]719 return 0;
720}
721
[8b243f2]722/** Wait for an incoming IPC call or an answer.
[fbcfd458]723 *
[da1bafb]724 * @param calldata Pointer to buffer where the call/answer data is stored.
725 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
726 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
727 * for explanation.
728 *
729 * @return Hash of the call.
730 * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
731 * call is a notification. IPC_CALLID_ANSWERED denotes an
732 * answer.
[bd5a663]733 *
[2d5a54f3]734 */
[96b02eb9]735sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
[da1bafb]736 unsigned int flags)
[2d5a54f3]737{
738 call_t *call;
[da1bafb]739
[741fd16]740restart:
[da1bafb]741
[741fd16]742#ifdef CONFIG_UDEBUG
743 udebug_stoppable_begin();
[da1bafb]744#endif
745
[51ec40f]746 call = ipc_wait_for_call(&TASK->answerbox, usec,
747 flags | SYNCH_FLAGS_INTERRUPTIBLE);
[da1bafb]748
[741fd16]749#ifdef CONFIG_UDEBUG
750 udebug_stoppable_end();
751#endif
[da1bafb]752
[9f22213]753 if (!call)
754 return 0;
[da1bafb]755
[5626277]756 if (call->flags & IPC_CALL_NOTIF) {
[43752b6]757 /* Set in_phone_hash to the interrupt counter */
[0c1a5d8a]758 call->data.phone = (void *) call->priv;
[43752b6]759
760 STRUCT_TO_USPACE(calldata, &call->data);
[da1bafb]761
[d51a0d6]762 kobject_put(call->kobject);
[5626277]763
[96b02eb9]764 return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
[5626277]765 }
[da1bafb]766
[2d5a54f3]767 if (call->flags & IPC_CALL_ANSWERED) {
[7c7aae16]768 process_answer(call);
[da1bafb]769
[fbcfd458]770 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
[d51a0d6]771 kobject_put(call->kobject);
[fbcfd458]772 goto restart;
773 }
[da1bafb]774
[1d81eb6]775 STRUCT_TO_USPACE(calldata, &call->data);
[d51a0d6]776 kobject_put(call->kobject);
[da1bafb]777
[96b02eb9]778 return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
[2d5a54f3]779 }
[da1bafb]780
[2d5a54f3]781 if (process_request(&TASK->answerbox, call))
782 goto restart;
[da1bafb]783
[7c7aae16]784 /* Include phone address('id') of the caller in the request,
785 * copy whole call->data, not only call->data.args */
[bd55bbb]786 if (STRUCT_TO_USPACE(calldata, &call->data)) {
[e06da7e]787 /*
788 * The callee will not receive this call and no one else has
789 * a chance to answer it. Reply with the EPARTY error code.
[b11ee88]790 */
[e06da7e]791 ipc_data_t saved_data;
[da1bafb]792 bool saved;
793
[e06da7e]794 if (answer_need_old(call)) {
795 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]796 saved = true;
797 } else
798 saved = false;
[e06da7e]799
800 IPC_SET_RETVAL(call->data, EPARTY);
[da1bafb]801 (void) answer_preprocess(call, saved ? &saved_data : NULL);
[e06da7e]802 ipc_answer(&TASK->answerbox, call);
[bd55bbb]803 return 0;
804 }
[da1bafb]805
[96b02eb9]806 return (sysarg_t) call;
[2d5a54f3]807}
[5626277]808
[da1bafb]809/** Interrupt one thread from sys_ipc_wait_for_call().
810 *
811 */
[96b02eb9]812sysarg_t sys_ipc_poke(void)
[057d21a]813{
[da1bafb]814 waitq_unsleep(&TASK->answerbox.wq);
[057d21a]815 return EOK;
816}
817
[8b243f2]818/** Connect an IRQ handler to a task.
[2b017ba]819 *
[228e490]820 * @param inr IRQ number.
821 * @param imethod Interface and method to be associated with the notification.
822 * @param ucode Uspace pointer to the top-half pseudocode.
[da1bafb]823 *
[e9d15d9]824 * @return IRQ kernel object capability
825 * @return EPERM
826 * @return Error code returned by ipc_irq_subscribe().
[2b017ba]827 *
828 */
[24abb85d]829sysarg_t sys_ipc_irq_subscribe(inr_t inr, sysarg_t imethod, irq_code_t *ucode)
[5626277]830{
[719a208]831 if (!(perm_get(TASK) & PERM_IRQ_REG))
[2bb8648]832 return EPERM;
[da1bafb]833
[24abb85d]834 return ipc_irq_subscribe(&TASK->answerbox, inr, imethod, ucode);
[5626277]835}
836
[8b243f2]837/** Disconnect an IRQ handler from a task.
838 *
[da1bafb]839 * @param inr IRQ number.
840 * @param devno Device number.
841 *
842 * @return Zero on success or EPERM on error.
[2b017ba]843 *
844 */
[e9d15d9]845sysarg_t sys_ipc_irq_unsubscribe(sysarg_t cap)
[5626277]846{
[719a208]847 if (!(perm_get(TASK) & PERM_IRQ_REG))
[2bb8648]848 return EPERM;
[da1bafb]849
[e9d15d9]850 ipc_irq_unsubscribe(&TASK->answerbox, cap);
[da1bafb]851
[5626277]852 return 0;
853}
[b45c443]854
[6b10dab]855#ifdef __32_BITS__
[9a1b20c]856
[6b10dab]857/** Syscall connect to a task by ID (32 bits)
[da1bafb]858 *
859 * @return Phone id on success, or negative error code.
[9a1b20c]860 *
861 */
[6b10dab]862sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
[9a1b20c]863{
864#ifdef CONFIG_UDEBUG
[6b10dab]865 sysarg64_t taskid;
866 int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
[9a1b20c]867 if (rc != 0)
[96b02eb9]868 return (sysarg_t) rc;
[da1bafb]869
[6b10dab]870 return ipc_connect_kbox((task_id_t) taskid);
[9a1b20c]871#else
[96b02eb9]872 return (sysarg_t) ENOTSUP;
[9a1b20c]873#endif
874}
875
[6b10dab]876#endif /* __32_BITS__ */
877
878#ifdef __64_BITS__
879
880/** Syscall connect to a task by ID (64 bits)
881 *
882 * @return Phone id on success, or negative error code.
883 *
884 */
885sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
886{
887#ifdef CONFIG_UDEBUG
888 return ipc_connect_kbox((task_id_t) taskid);
889#else
890 return (sysarg_t) ENOTSUP;
891#endif
892}
893
894#endif /* __64_BITS__ */
895
[cc73a8a1]896/** @}
[b45c443]897 */
Note: See TracBrowser for help on using the repository browser.