source: mainline/kernel/generic/src/ipc/sysipc.c@ 3dab10ae

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

Drop phone kobject reference when slamming the phone in answer_preprocess

  • 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
[43e2cbc]288 ipc_call_hold(call);
[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);
293 ipc_call_release(call);
[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 */
308 ipc_call_free(call);
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));
325 ipc_call_free(call);
[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 *
[3209923]349 * This function can only handle four arguments of payload, but is faster than
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.
357 * @param arg4 Service-defined payload argument.
[da1bafb]358 *
359 * @return Call hash on success.
360 * @return IPC_CALLRET_FATAL in case of a fatal error.
361 * @return IPC_CALLRET_TEMPORARY if there are too many pending
362 * asynchronous requests; answers should be handled first.
363 *
[2d5a54f3]364 */
[48bcf49]365sysarg_t sys_ipc_call_async_fast(sysarg_t handle, sysarg_t imethod,
[96b02eb9]366 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]367{
[48bcf49]368 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
369 if (!kobj)
[c9fff17]370 return IPC_CALLRET_FATAL;
[228e490]371
[48bcf49]372 if (check_call_limit(kobj->phone)) {
373 kobject_put(kobj);
[97d17fe]374 return IPC_CALLRET_TEMPORARY;
[48bcf49]375 }
[da1bafb]376
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);
382 IPC_SET_ARG4(call->data, arg4);
[da1bafb]383
[8498915]384 /*
385 * To achieve deterministic behavior, zero out arguments that are beyond
386 * the limits of the fast version.
387 */
388 IPC_SET_ARG5(call->data, 0);
[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.
405 *
406 * @return See sys_ipc_call_async_fast().
[2d5a54f3]407 *
408 */
[48bcf49]409sysarg_t sys_ipc_call_async_slow(sysarg_t handle, ipc_data_t *data)
[2d5a54f3]410{
[48bcf49]411 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
412 if (!kobj)
[c9fff17]413 return IPC_CALLRET_FATAL;
[4e49572]414
[48bcf49]415 if (check_call_limit(kobj->phone)) {
416 kobject_put(kobj);
[97d17fe]417 return IPC_CALLRET_TEMPORARY;
[48bcf49]418 }
[97d17fe]419
[da1bafb]420 call_t *call = ipc_call_alloc(0);
421 int rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]422 sizeof(call->data.args));
[f58af46]423 if (rc != 0) {
424 ipc_call_free(call);
[48bcf49]425 kobject_put(kobj);
[96b02eb9]426 return (sysarg_t) rc;
[f58af46]427 }
[da1bafb]428
[48bcf49]429 int res = request_preprocess(call, kobj->phone);
[da1bafb]430
431 if (!res)
[48bcf49]432 ipc_call(kobj->phone, call);
[7c7aae16]433 else
[48bcf49]434 ipc_backsend_err(kobj->phone, call, res);
[da1bafb]435
[48bcf49]436 kobject_put(kobj);
[96b02eb9]437 return (sysarg_t) call;
[2d5a54f3]438}
439
[da1bafb]440/** Forward a received call to another destination
441 *
442 * Common code for both the fast and the slow version.
443 *
[48bcf49]444 * @param callid Hash of the call to forward.
445 * @param handle Phone capability to use for forwarding.
446 * @param imethod New interface and method to use for the forwarded call.
447 * @param arg1 New value of the first argument for the forwarded call.
448 * @param arg2 New value of the second argument for the forwarded call.
449 * @param arg3 New value of the third argument for the forwarded call.
450 * @param arg4 New value of the fourth argument for the forwarded call.
451 * @param arg5 New value of the fifth argument for the forwarded call.
452 * @param mode Flags that specify mode of the forward operation.
453 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
454 * the function considers only the fast version arguments:
455 * i.e. arg1 and arg2.
[da1bafb]456 *
457 * @return 0 on succes, otherwise an error code.
458 *
459 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
460 *
[2ba7810]461 */
[48bcf49]462static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t handle,
[228e490]463 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
[96b02eb9]464 sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
[2ba7810]465{
[da1bafb]466 call_t *call = get_call(callid);
[2ba7810]467 if (!call)
468 return ENOENT;
[09024119]469
470 ipc_data_t old;
471 bool need_old = answer_need_old(call);
[f9841e69]472 if (need_old)
473 old = call->data;
[48daf64]474
[09024119]475 bool after_forward = false;
476 int rc;
[05ffb41]477
[48bcf49]478 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
479 if (!kobj) {
[95a3082]480 rc = ENOENT;
481 goto error;
[c9fff17]482 }
[da1bafb]483
[228e490]484 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
[95a3082]485 rc = EPERM;
486 goto error;
[2ba7810]487 }
[09024119]488
[95a3082]489 call->flags |= IPC_CALL_FORWARDED;
[da1bafb]490
[0dc4258]491 /*
[4e5dabf]492 * User space is not allowed to change interface and method of system
[228e490]493 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
[4e5dabf]494 * means of imethod, arg1, arg2 and arg3.
[228e490]495 * If the interface and method is immutable, don't change anything.
[2ba7810]496 */
[228e490]497 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
498 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
499 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
[b61d47d]500 phone_dealloc(IPC_GET_ARG5(call->data));
[da1bafb]501
[228e490]502 IPC_SET_ARG1(call->data, imethod);
[0dc4258]503 IPC_SET_ARG2(call->data, arg1);
[b61d47d]504 IPC_SET_ARG3(call->data, arg2);
[da1bafb]505
[4e5dabf]506 if (slow)
[48daf64]507 IPC_SET_ARG4(call->data, arg3);
[4e5dabf]508
509 /*
510 * For system methods we deliberately don't
511 * overwrite ARG5.
512 */
[0dc4258]513 } else {
[228e490]514 IPC_SET_IMETHOD(call->data, imethod);
[0dc4258]515 IPC_SET_ARG1(call->data, arg1);
[b61d47d]516 IPC_SET_ARG2(call->data, arg2);
[48daf64]517 if (slow) {
518 IPC_SET_ARG3(call->data, arg3);
519 IPC_SET_ARG4(call->data, arg4);
520 IPC_SET_ARG5(call->data, arg5);
521 }
[0dc4258]522 }
[2ba7810]523 }
[da1bafb]524
[48bcf49]525 rc = ipc_forward(call, kobj->phone, &TASK->answerbox, mode);
[f9841e69]526 if (rc != EOK) {
527 after_forward = true;
528 goto error;
529 }
[95a3082]530
[48bcf49]531 kobject_put(kobj);
[f9841e69]532 return EOK;
[95a3082]533
[f9841e69]534error:
[fcfa926b]535 IPC_SET_RETVAL(call->data, EFORWARD);
[9ef1b79b]536 (void) answer_preprocess(call, need_old ? &old : NULL);
[f9841e69]537 if (after_forward)
538 _ipc_answer_free_call(call, false);
539 else
540 ipc_answer(&TASK->answerbox, call);
541
[48bcf49]542 if (kobj)
543 kobject_put(kobj);
[95a3082]544 return rc;
[2ba7810]545}
546
[48daf64]547/** Forward a received call to another destination - fast version.
548 *
[228e490]549 * In case the original interface and method is a system method, ARG1, ARG2
550 * and ARG3 are overwritten in the forwarded message with the new method and
551 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
552 * are rewritten with the new interface and method, arg1 and arg2, respectively.
553 * Also note there is a set of immutable methods, for which the new method and
554 * arguments are not set and these values are ignored.
[da1bafb]555 *
[48bcf49]556 * @param callid Hash of the call to forward.
557 * @param handle Phone handle to use for forwarding.
558 * @param imethod New interface and method to use for the forwarded call.
559 * @param arg1 New value of the first argument for the forwarded call.
560 * @param arg2 New value of the second argument for the forwarded call.
561 * @param mode Flags that specify mode of the forward operation.
[da1bafb]562 *
563 * @return 0 on succes, otherwise an error code.
564 *
[48daf64]565 */
[48bcf49]566sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t handle,
[228e490]567 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[48daf64]568{
[48bcf49]569 return sys_ipc_forward_common(callid, handle, imethod, arg1, arg2, 0, 0,
[48daf64]570 0, mode, false);
571}
572
573/** Forward a received call to another destination - slow version.
574 *
575 * This function is the slow verision of the sys_ipc_forward_fast interface.
[228e490]576 * It can copy all five new arguments and the new interface and method from
577 * the userspace. It naturally extends the functionality of the fast version.
578 * For system methods, it additionally stores the new value of arg3 to ARG4.
579 * For non-system methods, it additionally stores the new value of arg3, arg4
580 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
[da1bafb]581 *
582 * @param callid Hash of the call to forward.
[48bcf49]583 * @param handle Phone handle to use for forwarding.
[da1bafb]584 * @param data Userspace address of the new IPC data.
585 * @param mode Flags that specify mode of the forward operation.
586 *
587 * @return 0 on succes, otherwise an error code.
588 *
[48daf64]589 */
[48bcf49]590sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t handle,
[da1bafb]591 ipc_data_t *data, unsigned int mode)
[48daf64]592{
593 ipc_data_t newdata;
[da1bafb]594 int rc = copy_from_uspace(&newdata.args, &data->args,
[48daf64]595 sizeof(newdata.args));
[da1bafb]596 if (rc != 0)
[96b02eb9]597 return (sysarg_t) rc;
[da1bafb]598
[48bcf49]599 return sys_ipc_forward_common(callid, handle,
[228e490]600 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
[48daf64]601 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
602 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
603}
604
[8b243f2]605/** Answer an IPC call - fast version.
606 *
607 * This function can handle only two return arguments of payload, but is faster
608 * than the generic sys_ipc_answer().
609 *
[da1bafb]610 * @param callid Hash of the call to be answered.
611 * @param retval Return value of the answer.
612 * @param arg1 Service-defined return value.
613 * @param arg2 Service-defined return value.
614 * @param arg3 Service-defined return value.
615 * @param arg4 Service-defined return value.
616 *
617 * @return 0 on success, otherwise an error code.
[8b243f2]618 *
619 */
[96b02eb9]620sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
621 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]622{
[897f2e76]623 /* Do not answer notification callids */
624 if (callid & IPC_CALLID_NOTIFICATION)
625 return 0;
[da1bafb]626
627 call_t *call = get_call(callid);
[2ba7810]628 if (!call)
629 return ENOENT;
[da1bafb]630
631 ipc_data_t saved_data;
632 bool saved;
633
[9f22213]634 if (answer_need_old(call)) {
[2ba7810]635 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]636 saved = true;
637 } else
638 saved = false;
639
[2d5a54f3]640 IPC_SET_RETVAL(call->data, retval);
641 IPC_SET_ARG1(call->data, arg1);
642 IPC_SET_ARG2(call->data, arg2);
[b74959bd]643 IPC_SET_ARG3(call->data, arg3);
644 IPC_SET_ARG4(call->data, arg4);
[da1bafb]645
[8498915]646 /*
647 * To achieve deterministic behavior, zero out arguments that are beyond
648 * the limits of the fast version.
649 */
650 IPC_SET_ARG5(call->data, 0);
[da1bafb]651 int rc = answer_preprocess(call, saved ? &saved_data : NULL);
652
[2d5a54f3]653 ipc_answer(&TASK->answerbox, call);
[7c23af9]654 return rc;
[2d5a54f3]655}
656
[8b243f2]657/** Answer an IPC call.
658 *
[da1bafb]659 * @param callid Hash of the call to be answered.
660 * @param data Userspace address of call data with the answer.
661 *
662 * @return 0 on success, otherwise an error code.
[8b243f2]663 *
664 */
[96b02eb9]665sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
[2d5a54f3]666{
[897f2e76]667 /* Do not answer notification callids */
668 if (callid & IPC_CALLID_NOTIFICATION)
669 return 0;
[da1bafb]670
671 call_t *call = get_call(callid);
[2ba7810]672 if (!call)
673 return ENOENT;
[da1bafb]674
675 ipc_data_t saved_data;
676 bool saved;
677
[9f22213]678 if (answer_need_old(call)) {
[2ba7810]679 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]680 saved = true;
681 } else
682 saved = false;
683
684 int rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]685 sizeof(call->data.args));
[e3c762cd]686 if (rc != 0)
687 return rc;
[da1bafb]688
689 rc = answer_preprocess(call, saved ? &saved_data : NULL);
[2d5a54f3]690
691 ipc_answer(&TASK->answerbox, call);
[7c23af9]692 return rc;
[2d5a54f3]693}
694
[8b243f2]695/** Hang up a phone.
[2d5a54f3]696 *
[48bcf49]697 * @param handle Phone capability handle of the phone to be hung up.
[da1bafb]698 *
699 * @return 0 on success or an error code.
[8b243f2]700 *
[fbcfd458]701 */
[48bcf49]702sysarg_t sys_ipc_hangup(sysarg_t handle)
[fbcfd458]703{
[48bcf49]704 kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
705 if (!kobj)
[c9fff17]706 return ENOENT;
[da1bafb]707
[48bcf49]708 if (ipc_phone_hangup(kobj->phone)) {
709 kobject_put(kobj);
[fbcfd458]710 return -1;
[48bcf49]711 }
[da1bafb]712
[48bcf49]713 kobject_put(kobj);
[fbcfd458]714 return 0;
715}
716
[8b243f2]717/** Wait for an incoming IPC call or an answer.
[fbcfd458]718 *
[da1bafb]719 * @param calldata Pointer to buffer where the call/answer data is stored.
720 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
721 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
722 * for explanation.
723 *
724 * @return Hash of the call.
725 * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
726 * call is a notification. IPC_CALLID_ANSWERED denotes an
727 * answer.
[bd5a663]728 *
[2d5a54f3]729 */
[96b02eb9]730sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
[da1bafb]731 unsigned int flags)
[2d5a54f3]732{
733 call_t *call;
[da1bafb]734
[741fd16]735restart:
[da1bafb]736
[741fd16]737#ifdef CONFIG_UDEBUG
738 udebug_stoppable_begin();
[da1bafb]739#endif
740
[51ec40f]741 call = ipc_wait_for_call(&TASK->answerbox, usec,
742 flags | SYNCH_FLAGS_INTERRUPTIBLE);
[da1bafb]743
[741fd16]744#ifdef CONFIG_UDEBUG
745 udebug_stoppable_end();
746#endif
[da1bafb]747
[9f22213]748 if (!call)
749 return 0;
[da1bafb]750
[5626277]751 if (call->flags & IPC_CALL_NOTIF) {
[43752b6]752 /* Set in_phone_hash to the interrupt counter */
[0c1a5d8a]753 call->data.phone = (void *) call->priv;
[43752b6]754
755 STRUCT_TO_USPACE(calldata, &call->data);
[da1bafb]756
[5626277]757 ipc_call_free(call);
758
[96b02eb9]759 return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
[5626277]760 }
[da1bafb]761
[2d5a54f3]762 if (call->flags & IPC_CALL_ANSWERED) {
[7c7aae16]763 process_answer(call);
[da1bafb]764
[fbcfd458]765 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
766 ipc_call_free(call);
767 goto restart;
768 }
[da1bafb]769
[1d81eb6]770 STRUCT_TO_USPACE(calldata, &call->data);
[2d5a54f3]771 ipc_call_free(call);
[da1bafb]772
[96b02eb9]773 return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
[2d5a54f3]774 }
[da1bafb]775
[2d5a54f3]776 if (process_request(&TASK->answerbox, call))
777 goto restart;
[da1bafb]778
[7c7aae16]779 /* Include phone address('id') of the caller in the request,
780 * copy whole call->data, not only call->data.args */
[bd55bbb]781 if (STRUCT_TO_USPACE(calldata, &call->data)) {
[e06da7e]782 /*
783 * The callee will not receive this call and no one else has
784 * a chance to answer it. Reply with the EPARTY error code.
[b11ee88]785 */
[e06da7e]786 ipc_data_t saved_data;
[da1bafb]787 bool saved;
788
[e06da7e]789 if (answer_need_old(call)) {
790 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]791 saved = true;
792 } else
793 saved = false;
[e06da7e]794
795 IPC_SET_RETVAL(call->data, EPARTY);
[da1bafb]796 (void) answer_preprocess(call, saved ? &saved_data : NULL);
[e06da7e]797 ipc_answer(&TASK->answerbox, call);
[bd55bbb]798 return 0;
799 }
[da1bafb]800
[96b02eb9]801 return (sysarg_t) call;
[2d5a54f3]802}
[5626277]803
[da1bafb]804/** Interrupt one thread from sys_ipc_wait_for_call().
805 *
806 */
[96b02eb9]807sysarg_t sys_ipc_poke(void)
[057d21a]808{
[da1bafb]809 waitq_unsleep(&TASK->answerbox.wq);
[057d21a]810 return EOK;
811}
812
[8b243f2]813/** Connect an IRQ handler to a task.
[2b017ba]814 *
[228e490]815 * @param inr IRQ number.
816 * @param imethod Interface and method to be associated with the notification.
817 * @param ucode Uspace pointer to the top-half pseudocode.
[da1bafb]818 *
[e9d15d9]819 * @return IRQ kernel object capability
820 * @return EPERM
821 * @return Error code returned by ipc_irq_subscribe().
[2b017ba]822 *
823 */
[24abb85d]824sysarg_t sys_ipc_irq_subscribe(inr_t inr, sysarg_t imethod, irq_code_t *ucode)
[5626277]825{
[719a208]826 if (!(perm_get(TASK) & PERM_IRQ_REG))
[2bb8648]827 return EPERM;
[da1bafb]828
[24abb85d]829 return ipc_irq_subscribe(&TASK->answerbox, inr, imethod, ucode);
[5626277]830}
831
[8b243f2]832/** Disconnect an IRQ handler from a task.
833 *
[da1bafb]834 * @param inr IRQ number.
835 * @param devno Device number.
836 *
837 * @return Zero on success or EPERM on error.
[2b017ba]838 *
839 */
[e9d15d9]840sysarg_t sys_ipc_irq_unsubscribe(sysarg_t cap)
[5626277]841{
[719a208]842 if (!(perm_get(TASK) & PERM_IRQ_REG))
[2bb8648]843 return EPERM;
[da1bafb]844
[e9d15d9]845 ipc_irq_unsubscribe(&TASK->answerbox, cap);
[da1bafb]846
[5626277]847 return 0;
848}
[b45c443]849
[6b10dab]850#ifdef __32_BITS__
[9a1b20c]851
[6b10dab]852/** Syscall connect to a task by ID (32 bits)
[da1bafb]853 *
854 * @return Phone id on success, or negative error code.
[9a1b20c]855 *
856 */
[6b10dab]857sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
[9a1b20c]858{
859#ifdef CONFIG_UDEBUG
[6b10dab]860 sysarg64_t taskid;
861 int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
[9a1b20c]862 if (rc != 0)
[96b02eb9]863 return (sysarg_t) rc;
[da1bafb]864
[6b10dab]865 return ipc_connect_kbox((task_id_t) taskid);
[9a1b20c]866#else
[96b02eb9]867 return (sysarg_t) ENOTSUP;
[9a1b20c]868#endif
869}
870
[6b10dab]871#endif /* __32_BITS__ */
872
873#ifdef __64_BITS__
874
875/** Syscall connect to a task by ID (64 bits)
876 *
877 * @return Phone id on success, or negative error code.
878 *
879 */
880sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
881{
882#ifdef CONFIG_UDEBUG
883 return ipc_connect_kbox((task_id_t) taskid);
884#else
885 return (sysarg_t) ENOTSUP;
886#endif
887}
888
889#endif /* __64_BITS__ */
890
[cc73a8a1]891/** @}
[b45c443]892 */
Note: See TracBrowser for help on using the repository browser.