| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup genericipc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <arch.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <memstr.h>
|
|---|
| 38 | #include <ipc/ipc.h>
|
|---|
| 39 | #include <abi/ipc/methods.h>
|
|---|
| 40 | #include <ipc/sysipc.h>
|
|---|
| 41 | #include <ipc/sysipc_ops.h>
|
|---|
| 42 | #include <ipc/sysipc_priv.h>
|
|---|
| 43 | #include <ipc/irq.h>
|
|---|
| 44 | #include <ipc/ipcrsc.h>
|
|---|
| 45 | #include <ipc/event.h>
|
|---|
| 46 | #include <ipc/kbox.h>
|
|---|
| 47 | #include <synch/waitq.h>
|
|---|
| 48 | #include <arch/interrupt.h>
|
|---|
| 49 | #include <syscall/copy.h>
|
|---|
| 50 | #include <security/cap.h>
|
|---|
| 51 | #include <console/console.h>
|
|---|
| 52 | #include <print.h>
|
|---|
| 53 | #include <macros.h>
|
|---|
| 54 |
|
|---|
| 55 | #define STRUCT_TO_USPACE(dst, src) copy_to_uspace((dst), (src), sizeof(*(src)))
|
|---|
| 56 |
|
|---|
| 57 | /** Decide if the interface and method is a system method.
|
|---|
| 58 | *
|
|---|
| 59 | * @param imethod Interface and method to be decided.
|
|---|
| 60 | *
|
|---|
| 61 | * @return True if the interface and method is a system
|
|---|
| 62 | * interface and method.
|
|---|
| 63 | *
|
|---|
| 64 | */
|
|---|
| 65 | static inline bool method_is_system(sysarg_t imethod)
|
|---|
| 66 | {
|
|---|
| 67 | if (imethod <= IPC_M_LAST_SYSTEM)
|
|---|
| 68 | return true;
|
|---|
| 69 |
|
|---|
| 70 | return false;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Decide if the message with this interface and method is forwardable.
|
|---|
| 74 | *
|
|---|
| 75 | * Some system messages may be forwarded, for some of them
|
|---|
| 76 | * it is useless.
|
|---|
| 77 | *
|
|---|
| 78 | * @param imethod Interface and method to be decided.
|
|---|
| 79 | *
|
|---|
| 80 | * @return True if the interface and method is forwardable.
|
|---|
| 81 | *
|
|---|
| 82 | */
|
|---|
| 83 | static inline bool method_is_forwardable(sysarg_t imethod)
|
|---|
| 84 | {
|
|---|
| 85 | switch (imethod) {
|
|---|
| 86 | case IPC_M_CONNECTION_CLONE:
|
|---|
| 87 | case IPC_M_CLONE_ESTABLISH:
|
|---|
| 88 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 89 | /* This message is meant only for the original recipient. */
|
|---|
| 90 | return false;
|
|---|
| 91 | default:
|
|---|
| 92 | return true;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** Decide if the message with this interface and method is immutable on forward.
|
|---|
| 97 | *
|
|---|
| 98 | * Some system messages may be forwarded but their content cannot be altered.
|
|---|
| 99 | *
|
|---|
| 100 | * @param imethod Interface and method to be decided.
|
|---|
| 101 | *
|
|---|
| 102 | * @return True if the interface and method is immutable on forward.
|
|---|
| 103 | *
|
|---|
| 104 | */
|
|---|
| 105 | static inline bool method_is_immutable(sysarg_t imethod)
|
|---|
| 106 | {
|
|---|
| 107 | switch (imethod) {
|
|---|
| 108 | case IPC_M_SHARE_OUT:
|
|---|
| 109 | case IPC_M_SHARE_IN:
|
|---|
| 110 | case IPC_M_DATA_WRITE:
|
|---|
| 111 | case IPC_M_DATA_READ:
|
|---|
| 112 | case IPC_M_STATE_CHANGE_AUTHORIZE:
|
|---|
| 113 | return true;
|
|---|
| 114 | default:
|
|---|
| 115 | return false;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 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 | *
|
|---|
| 127 | * @param call Call structure to be decided.
|
|---|
| 128 | *
|
|---|
| 129 | * @return true if the old call contents should be saved.
|
|---|
| 130 | *
|
|---|
| 131 | */
|
|---|
| 132 | static inline bool answer_need_old(call_t *call)
|
|---|
| 133 | {
|
|---|
| 134 | switch (IPC_GET_IMETHOD(call->data)) {
|
|---|
| 135 | case IPC_M_CONNECTION_CLONE:
|
|---|
| 136 | case IPC_M_CLONE_ESTABLISH:
|
|---|
| 137 | case IPC_M_CONNECT_TO_ME:
|
|---|
| 138 | case IPC_M_CONNECT_ME_TO:
|
|---|
| 139 | case IPC_M_SHARE_OUT:
|
|---|
| 140 | case IPC_M_SHARE_IN:
|
|---|
| 141 | case IPC_M_DATA_WRITE:
|
|---|
| 142 | case IPC_M_DATA_READ:
|
|---|
| 143 | case IPC_M_STATE_CHANGE_AUTHORIZE:
|
|---|
| 144 | return true;
|
|---|
| 145 | default:
|
|---|
| 146 | return false;
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /** Interpret process answer as control information.
|
|---|
| 151 | *
|
|---|
| 152 | * This function is called directly after sys_ipc_answer().
|
|---|
| 153 | *
|
|---|
| 154 | * @param answer Call structure with the answer.
|
|---|
| 155 | * @param olddata Saved data of the request.
|
|---|
| 156 | *
|
|---|
| 157 | * @return Return EOK on success or a negative error code.
|
|---|
| 158 | *
|
|---|
| 159 | */
|
|---|
| 160 | int answer_preprocess(call_t *answer, ipc_data_t *olddata)
|
|---|
| 161 | {
|
|---|
| 162 | int rc = EOK;
|
|---|
| 163 |
|
|---|
| 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);
|
|---|
| 170 |
|
|---|
| 171 | SYSIPC_OP(answer_cleanup, answer, olddata);
|
|---|
| 172 | return rc;
|
|---|
| 173 | } else {
|
|---|
| 174 | ASSERT(answer->active);
|
|---|
| 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
|
|---|
| 188 | * forget this soon to be answered call.
|
|---|
| 189 | */
|
|---|
| 190 | spinlock_lock(&answer->sender->active_calls_lock);
|
|---|
| 191 | list_remove(&answer->ta_link);
|
|---|
| 192 | spinlock_unlock(&answer->sender->active_calls_lock);
|
|---|
| 193 | }
|
|---|
| 194 | spinlock_unlock(&answer->forget_lock);
|
|---|
| 195 |
|
|---|
| 196 | if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
|
|---|
| 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);
|
|---|
| 202 | phone->state = IPC_PHONE_SLAMMED;
|
|---|
| 203 | irq_spinlock_unlock(&phone->callee->lock, true);
|
|---|
| 204 | }
|
|---|
| 205 | mutex_unlock(&phone->lock);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | if (!olddata)
|
|---|
| 209 | return rc;
|
|---|
| 210 |
|
|---|
| 211 | return SYSIPC_OP(answer_preprocess, answer, olddata);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /** Called before the request is sent.
|
|---|
| 215 | *
|
|---|
| 216 | * @param call Call structure with the request.
|
|---|
| 217 | * @param phone Phone that the call will be sent through.
|
|---|
| 218 | *
|
|---|
| 219 | * @return Return 0 on success, ELIMIT or EPERM on error.
|
|---|
| 220 | *
|
|---|
| 221 | */
|
|---|
| 222 | static int request_preprocess(call_t *call, phone_t *phone)
|
|---|
| 223 | {
|
|---|
| 224 | call->request_method = IPC_GET_IMETHOD(call->data);
|
|---|
| 225 | return SYSIPC_OP(request_preprocess, call, phone);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /*******************************************************************************
|
|---|
| 229 | * Functions called to process received call/answer before passing it to uspace.
|
|---|
| 230 | *******************************************************************************/
|
|---|
| 231 |
|
|---|
| 232 | /** Do basic kernel processing of received call answer.
|
|---|
| 233 | *
|
|---|
| 234 | * @param call Call structure with the answer.
|
|---|
| 235 | *
|
|---|
| 236 | */
|
|---|
| 237 | static void process_answer(call_t *call)
|
|---|
| 238 | {
|
|---|
| 239 | if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
|
|---|
| 240 | (call->flags & IPC_CALL_FORWARDED))
|
|---|
| 241 | IPC_SET_RETVAL(call->data, EFORWARD);
|
|---|
| 242 |
|
|---|
| 243 | SYSIPC_OP(answer_process, call);
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 | /** Do basic kernel processing of received call request.
|
|---|
| 248 | *
|
|---|
| 249 | * @param box Destination answerbox structure.
|
|---|
| 250 | * @param call Call structure with the request.
|
|---|
| 251 | *
|
|---|
| 252 | * @return 0 if the call should be passed to userspace.
|
|---|
| 253 | * @return -1 if the call should be ignored.
|
|---|
| 254 | *
|
|---|
| 255 | */
|
|---|
| 256 | static int process_request(answerbox_t *box, call_t *call)
|
|---|
| 257 | {
|
|---|
| 258 | return SYSIPC_OP(request_process, call, box);
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | /** Check that the task did not exceed the allowed limit of asynchronous calls
|
|---|
| 262 | * made over a phone.
|
|---|
| 263 | *
|
|---|
| 264 | * @param phone Phone to check the limit against.
|
|---|
| 265 | *
|
|---|
| 266 | * @return 0 if limit not reached or -1 if limit exceeded.
|
|---|
| 267 | *
|
|---|
| 268 | */
|
|---|
| 269 | static int check_call_limit(phone_t *phone)
|
|---|
| 270 | {
|
|---|
| 271 | if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
|
|---|
| 272 | return -1;
|
|---|
| 273 |
|
|---|
| 274 | return 0;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /** Make a fast asynchronous call over IPC.
|
|---|
| 278 | *
|
|---|
| 279 | * This function can only handle four arguments of payload, but is faster than
|
|---|
| 280 | * the generic function sys_ipc_call_async_slow().
|
|---|
| 281 | *
|
|---|
| 282 | * @param phoneid Phone handle for the call.
|
|---|
| 283 | * @param imethod Interface and method of the call.
|
|---|
| 284 | * @param arg1 Service-defined payload argument.
|
|---|
| 285 | * @param arg2 Service-defined payload argument.
|
|---|
| 286 | * @param arg3 Service-defined payload argument.
|
|---|
| 287 | * @param arg4 Service-defined payload argument.
|
|---|
| 288 | *
|
|---|
| 289 | * @return Call hash on success.
|
|---|
| 290 | * @return IPC_CALLRET_FATAL in case of a fatal error.
|
|---|
| 291 | * @return IPC_CALLRET_TEMPORARY if there are too many pending
|
|---|
| 292 | * asynchronous requests; answers should be handled first.
|
|---|
| 293 | *
|
|---|
| 294 | */
|
|---|
| 295 | sysarg_t sys_ipc_call_async_fast(sysarg_t phoneid, sysarg_t imethod,
|
|---|
| 296 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
|---|
| 297 | {
|
|---|
| 298 | phone_t *phone;
|
|---|
| 299 | if (phone_get(phoneid, &phone) != EOK)
|
|---|
| 300 | return IPC_CALLRET_FATAL;
|
|---|
| 301 |
|
|---|
| 302 | if (check_call_limit(phone))
|
|---|
| 303 | return IPC_CALLRET_TEMPORARY;
|
|---|
| 304 |
|
|---|
| 305 | call_t *call = ipc_call_alloc(0);
|
|---|
| 306 | IPC_SET_IMETHOD(call->data, imethod);
|
|---|
| 307 | IPC_SET_ARG1(call->data, arg1);
|
|---|
| 308 | IPC_SET_ARG2(call->data, arg2);
|
|---|
| 309 | IPC_SET_ARG3(call->data, arg3);
|
|---|
| 310 | IPC_SET_ARG4(call->data, arg4);
|
|---|
| 311 |
|
|---|
| 312 | /*
|
|---|
| 313 | * To achieve deterministic behavior, zero out arguments that are beyond
|
|---|
| 314 | * the limits of the fast version.
|
|---|
| 315 | */
|
|---|
| 316 | IPC_SET_ARG5(call->data, 0);
|
|---|
| 317 |
|
|---|
| 318 | int res = request_preprocess(call, phone);
|
|---|
| 319 |
|
|---|
| 320 | if (!res)
|
|---|
| 321 | ipc_call(phone, call);
|
|---|
| 322 | else
|
|---|
| 323 | ipc_backsend_err(phone, call, res);
|
|---|
| 324 |
|
|---|
| 325 | return (sysarg_t) call;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | /** Make an asynchronous IPC call allowing to transmit the entire payload.
|
|---|
| 329 | *
|
|---|
| 330 | * @param phoneid Phone handle for the call.
|
|---|
| 331 | * @param data Userspace address of call data with the request.
|
|---|
| 332 | *
|
|---|
| 333 | * @return See sys_ipc_call_async_fast().
|
|---|
| 334 | *
|
|---|
| 335 | */
|
|---|
| 336 | sysarg_t sys_ipc_call_async_slow(sysarg_t phoneid, ipc_data_t *data)
|
|---|
| 337 | {
|
|---|
| 338 | phone_t *phone;
|
|---|
| 339 | if (phone_get(phoneid, &phone) != EOK)
|
|---|
| 340 | return IPC_CALLRET_FATAL;
|
|---|
| 341 |
|
|---|
| 342 | if (check_call_limit(phone))
|
|---|
| 343 | return IPC_CALLRET_TEMPORARY;
|
|---|
| 344 |
|
|---|
| 345 | call_t *call = ipc_call_alloc(0);
|
|---|
| 346 | int rc = copy_from_uspace(&call->data.args, &data->args,
|
|---|
| 347 | sizeof(call->data.args));
|
|---|
| 348 | if (rc != 0) {
|
|---|
| 349 | ipc_call_free(call);
|
|---|
| 350 | return (sysarg_t) rc;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | int res = request_preprocess(call, phone);
|
|---|
| 354 |
|
|---|
| 355 | if (!res)
|
|---|
| 356 | ipc_call(phone, call);
|
|---|
| 357 | else
|
|---|
| 358 | ipc_backsend_err(phone, call, res);
|
|---|
| 359 |
|
|---|
| 360 | return (sysarg_t) call;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | /** Forward a received call to another destination
|
|---|
| 364 | *
|
|---|
| 365 | * Common code for both the fast and the slow version.
|
|---|
| 366 | *
|
|---|
| 367 | * @param callid Hash of the call to forward.
|
|---|
| 368 | * @param phoneid Phone handle to use for forwarding.
|
|---|
| 369 | * @param imethod New interface and method to use for the forwarded call.
|
|---|
| 370 | * @param arg1 New value of the first argument for the forwarded call.
|
|---|
| 371 | * @param arg2 New value of the second argument for the forwarded call.
|
|---|
| 372 | * @param arg3 New value of the third argument for the forwarded call.
|
|---|
| 373 | * @param arg4 New value of the fourth argument for the forwarded call.
|
|---|
| 374 | * @param arg5 New value of the fifth argument for the forwarded call.
|
|---|
| 375 | * @param mode Flags that specify mode of the forward operation.
|
|---|
| 376 | * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
|
|---|
| 377 | * the function considers only the fast version arguments:
|
|---|
| 378 | * i.e. arg1 and arg2.
|
|---|
| 379 | *
|
|---|
| 380 | * @return 0 on succes, otherwise an error code.
|
|---|
| 381 | *
|
|---|
| 382 | * Warning: Make sure that ARG5 is not rewritten for certain system IPC
|
|---|
| 383 | *
|
|---|
| 384 | */
|
|---|
| 385 | static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t phoneid,
|
|---|
| 386 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
|---|
| 387 | sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
|
|---|
| 388 | {
|
|---|
| 389 | call_t *call = get_call(callid);
|
|---|
| 390 | if (!call)
|
|---|
| 391 | return ENOENT;
|
|---|
| 392 |
|
|---|
| 393 | ipc_data_t old;
|
|---|
| 394 | bool need_old = answer_need_old(call);
|
|---|
| 395 | if (need_old)
|
|---|
| 396 | old = call->data;
|
|---|
| 397 |
|
|---|
| 398 | bool after_forward = false;
|
|---|
| 399 | int rc;
|
|---|
| 400 | phone_t *phone;
|
|---|
| 401 |
|
|---|
| 402 | if (phone_get(phoneid, &phone) != EOK) {
|
|---|
| 403 | rc = ENOENT;
|
|---|
| 404 | goto error;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
|
|---|
| 408 | rc = EPERM;
|
|---|
| 409 | goto error;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | call->flags |= IPC_CALL_FORWARDED;
|
|---|
| 413 |
|
|---|
| 414 | /*
|
|---|
| 415 | * User space is not allowed to change interface and method of system
|
|---|
| 416 | * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
|
|---|
| 417 | * means of imethod, arg1, arg2 and arg3.
|
|---|
| 418 | * If the interface and method is immutable, don't change anything.
|
|---|
| 419 | */
|
|---|
| 420 | if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
|
|---|
| 421 | if (method_is_system(IPC_GET_IMETHOD(call->data))) {
|
|---|
| 422 | if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
|
|---|
| 423 | phone_dealloc(IPC_GET_ARG5(call->data));
|
|---|
| 424 |
|
|---|
| 425 | IPC_SET_ARG1(call->data, imethod);
|
|---|
| 426 | IPC_SET_ARG2(call->data, arg1);
|
|---|
| 427 | IPC_SET_ARG3(call->data, arg2);
|
|---|
| 428 |
|
|---|
| 429 | if (slow)
|
|---|
| 430 | IPC_SET_ARG4(call->data, arg3);
|
|---|
| 431 |
|
|---|
| 432 | /*
|
|---|
| 433 | * For system methods we deliberately don't
|
|---|
| 434 | * overwrite ARG5.
|
|---|
| 435 | */
|
|---|
| 436 | } else {
|
|---|
| 437 | IPC_SET_IMETHOD(call->data, imethod);
|
|---|
| 438 | IPC_SET_ARG1(call->data, arg1);
|
|---|
| 439 | IPC_SET_ARG2(call->data, arg2);
|
|---|
| 440 | if (slow) {
|
|---|
| 441 | IPC_SET_ARG3(call->data, arg3);
|
|---|
| 442 | IPC_SET_ARG4(call->data, arg4);
|
|---|
| 443 | IPC_SET_ARG5(call->data, arg5);
|
|---|
| 444 | }
|
|---|
| 445 | }
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | rc = ipc_forward(call, phone, &TASK->answerbox, mode);
|
|---|
| 449 | if (rc != EOK) {
|
|---|
| 450 | after_forward = true;
|
|---|
| 451 | goto error;
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | return EOK;
|
|---|
| 455 |
|
|---|
| 456 | error:
|
|---|
| 457 | IPC_SET_RETVAL(call->data, EFORWARD);
|
|---|
| 458 | (void) answer_preprocess(call, need_old ? &old : NULL);
|
|---|
| 459 | if (after_forward)
|
|---|
| 460 | _ipc_answer_free_call(call, false);
|
|---|
| 461 | else
|
|---|
| 462 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 463 |
|
|---|
| 464 | return rc;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | /** Forward a received call to another destination - fast version.
|
|---|
| 468 | *
|
|---|
| 469 | * In case the original interface and method is a system method, ARG1, ARG2
|
|---|
| 470 | * and ARG3 are overwritten in the forwarded message with the new method and
|
|---|
| 471 | * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
|
|---|
| 472 | * are rewritten with the new interface and method, arg1 and arg2, respectively.
|
|---|
| 473 | * Also note there is a set of immutable methods, for which the new method and
|
|---|
| 474 | * arguments are not set and these values are ignored.
|
|---|
| 475 | *
|
|---|
| 476 | * @param callid Hash of the call to forward.
|
|---|
| 477 | * @param phoneid Phone handle to use for forwarding.
|
|---|
| 478 | * @param imethod New interface and method to use for the forwarded call.
|
|---|
| 479 | * @param arg1 New value of the first argument for the forwarded call.
|
|---|
| 480 | * @param arg2 New value of the second argument for the forwarded call.
|
|---|
| 481 | * @param mode Flags that specify mode of the forward operation.
|
|---|
| 482 | *
|
|---|
| 483 | * @return 0 on succes, otherwise an error code.
|
|---|
| 484 | *
|
|---|
| 485 | */
|
|---|
| 486 | sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t phoneid,
|
|---|
| 487 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
|---|
| 488 | {
|
|---|
| 489 | return sys_ipc_forward_common(callid, phoneid, imethod, arg1, arg2, 0, 0,
|
|---|
| 490 | 0, mode, false);
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | /** Forward a received call to another destination - slow version.
|
|---|
| 494 | *
|
|---|
| 495 | * This function is the slow verision of the sys_ipc_forward_fast interface.
|
|---|
| 496 | * It can copy all five new arguments and the new interface and method from
|
|---|
| 497 | * the userspace. It naturally extends the functionality of the fast version.
|
|---|
| 498 | * For system methods, it additionally stores the new value of arg3 to ARG4.
|
|---|
| 499 | * For non-system methods, it additionally stores the new value of arg3, arg4
|
|---|
| 500 | * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
|
|---|
| 501 | *
|
|---|
| 502 | * @param callid Hash of the call to forward.
|
|---|
| 503 | * @param phoneid Phone handle to use for forwarding.
|
|---|
| 504 | * @param data Userspace address of the new IPC data.
|
|---|
| 505 | * @param mode Flags that specify mode of the forward operation.
|
|---|
| 506 | *
|
|---|
| 507 | * @return 0 on succes, otherwise an error code.
|
|---|
| 508 | *
|
|---|
| 509 | */
|
|---|
| 510 | sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t phoneid,
|
|---|
| 511 | ipc_data_t *data, unsigned int mode)
|
|---|
| 512 | {
|
|---|
| 513 | ipc_data_t newdata;
|
|---|
| 514 | int rc = copy_from_uspace(&newdata.args, &data->args,
|
|---|
| 515 | sizeof(newdata.args));
|
|---|
| 516 | if (rc != 0)
|
|---|
| 517 | return (sysarg_t) rc;
|
|---|
| 518 |
|
|---|
| 519 | return sys_ipc_forward_common(callid, phoneid,
|
|---|
| 520 | IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
|
|---|
| 521 | IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
|
|---|
| 522 | IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | /** Answer an IPC call - fast version.
|
|---|
| 526 | *
|
|---|
| 527 | * This function can handle only two return arguments of payload, but is faster
|
|---|
| 528 | * than the generic sys_ipc_answer().
|
|---|
| 529 | *
|
|---|
| 530 | * @param callid Hash of the call to be answered.
|
|---|
| 531 | * @param retval Return value of the answer.
|
|---|
| 532 | * @param arg1 Service-defined return value.
|
|---|
| 533 | * @param arg2 Service-defined return value.
|
|---|
| 534 | * @param arg3 Service-defined return value.
|
|---|
| 535 | * @param arg4 Service-defined return value.
|
|---|
| 536 | *
|
|---|
| 537 | * @return 0 on success, otherwise an error code.
|
|---|
| 538 | *
|
|---|
| 539 | */
|
|---|
| 540 | sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
|
|---|
| 541 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
|---|
| 542 | {
|
|---|
| 543 | /* Do not answer notification callids */
|
|---|
| 544 | if (callid & IPC_CALLID_NOTIFICATION)
|
|---|
| 545 | return 0;
|
|---|
| 546 |
|
|---|
| 547 | call_t *call = get_call(callid);
|
|---|
| 548 | if (!call)
|
|---|
| 549 | return ENOENT;
|
|---|
| 550 |
|
|---|
| 551 | ipc_data_t saved_data;
|
|---|
| 552 | bool saved;
|
|---|
| 553 |
|
|---|
| 554 | if (answer_need_old(call)) {
|
|---|
| 555 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
|---|
| 556 | saved = true;
|
|---|
| 557 | } else
|
|---|
| 558 | saved = false;
|
|---|
| 559 |
|
|---|
| 560 | IPC_SET_RETVAL(call->data, retval);
|
|---|
| 561 | IPC_SET_ARG1(call->data, arg1);
|
|---|
| 562 | IPC_SET_ARG2(call->data, arg2);
|
|---|
| 563 | IPC_SET_ARG3(call->data, arg3);
|
|---|
| 564 | IPC_SET_ARG4(call->data, arg4);
|
|---|
| 565 |
|
|---|
| 566 | /*
|
|---|
| 567 | * To achieve deterministic behavior, zero out arguments that are beyond
|
|---|
| 568 | * the limits of the fast version.
|
|---|
| 569 | */
|
|---|
| 570 | IPC_SET_ARG5(call->data, 0);
|
|---|
| 571 | int rc = answer_preprocess(call, saved ? &saved_data : NULL);
|
|---|
| 572 |
|
|---|
| 573 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 574 | return rc;
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | /** Answer an IPC call.
|
|---|
| 578 | *
|
|---|
| 579 | * @param callid Hash of the call to be answered.
|
|---|
| 580 | * @param data Userspace address of call data with the answer.
|
|---|
| 581 | *
|
|---|
| 582 | * @return 0 on success, otherwise an error code.
|
|---|
| 583 | *
|
|---|
| 584 | */
|
|---|
| 585 | sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
|
|---|
| 586 | {
|
|---|
| 587 | /* Do not answer notification callids */
|
|---|
| 588 | if (callid & IPC_CALLID_NOTIFICATION)
|
|---|
| 589 | return 0;
|
|---|
| 590 |
|
|---|
| 591 | call_t *call = get_call(callid);
|
|---|
| 592 | if (!call)
|
|---|
| 593 | return ENOENT;
|
|---|
| 594 |
|
|---|
| 595 | ipc_data_t saved_data;
|
|---|
| 596 | bool saved;
|
|---|
| 597 |
|
|---|
| 598 | if (answer_need_old(call)) {
|
|---|
| 599 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
|---|
| 600 | saved = true;
|
|---|
| 601 | } else
|
|---|
| 602 | saved = false;
|
|---|
| 603 |
|
|---|
| 604 | int rc = copy_from_uspace(&call->data.args, &data->args,
|
|---|
| 605 | sizeof(call->data.args));
|
|---|
| 606 | if (rc != 0)
|
|---|
| 607 | return rc;
|
|---|
| 608 |
|
|---|
| 609 | rc = answer_preprocess(call, saved ? &saved_data : NULL);
|
|---|
| 610 |
|
|---|
| 611 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 612 | return rc;
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | /** Hang up a phone.
|
|---|
| 616 | *
|
|---|
| 617 | * @param Phone handle of the phone to be hung up.
|
|---|
| 618 | *
|
|---|
| 619 | * @return 0 on success or an error code.
|
|---|
| 620 | *
|
|---|
| 621 | */
|
|---|
| 622 | sysarg_t sys_ipc_hangup(sysarg_t phoneid)
|
|---|
| 623 | {
|
|---|
| 624 | phone_t *phone;
|
|---|
| 625 |
|
|---|
| 626 | if (phone_get(phoneid, &phone) != EOK)
|
|---|
| 627 | return ENOENT;
|
|---|
| 628 |
|
|---|
| 629 | if (ipc_phone_hangup(phone))
|
|---|
| 630 | return -1;
|
|---|
| 631 |
|
|---|
| 632 | return 0;
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | /** Wait for an incoming IPC call or an answer.
|
|---|
| 636 | *
|
|---|
| 637 | * @param calldata Pointer to buffer where the call/answer data is stored.
|
|---|
| 638 | * @param usec Timeout. See waitq_sleep_timeout() for explanation.
|
|---|
| 639 | * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
|
|---|
| 640 | * for explanation.
|
|---|
| 641 | *
|
|---|
| 642 | * @return Hash of the call.
|
|---|
| 643 | * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
|
|---|
| 644 | * call is a notification. IPC_CALLID_ANSWERED denotes an
|
|---|
| 645 | * answer.
|
|---|
| 646 | *
|
|---|
| 647 | */
|
|---|
| 648 | sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
|
|---|
| 649 | unsigned int flags)
|
|---|
| 650 | {
|
|---|
| 651 | call_t *call;
|
|---|
| 652 |
|
|---|
| 653 | restart:
|
|---|
| 654 |
|
|---|
| 655 | #ifdef CONFIG_UDEBUG
|
|---|
| 656 | udebug_stoppable_begin();
|
|---|
| 657 | #endif
|
|---|
| 658 |
|
|---|
| 659 | call = ipc_wait_for_call(&TASK->answerbox, usec,
|
|---|
| 660 | flags | SYNCH_FLAGS_INTERRUPTIBLE);
|
|---|
| 661 |
|
|---|
| 662 | #ifdef CONFIG_UDEBUG
|
|---|
| 663 | udebug_stoppable_end();
|
|---|
| 664 | #endif
|
|---|
| 665 |
|
|---|
| 666 | if (!call)
|
|---|
| 667 | return 0;
|
|---|
| 668 |
|
|---|
| 669 | if (call->flags & IPC_CALL_NOTIF) {
|
|---|
| 670 | /* Set in_phone_hash to the interrupt counter */
|
|---|
| 671 | call->data.phone = (void *) call->priv;
|
|---|
| 672 |
|
|---|
| 673 | STRUCT_TO_USPACE(calldata, &call->data);
|
|---|
| 674 |
|
|---|
| 675 | ipc_call_free(call);
|
|---|
| 676 |
|
|---|
| 677 | return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
|
|---|
| 678 | }
|
|---|
| 679 |
|
|---|
| 680 | if (call->flags & IPC_CALL_ANSWERED) {
|
|---|
| 681 | process_answer(call);
|
|---|
| 682 |
|
|---|
| 683 | if (call->flags & IPC_CALL_DISCARD_ANSWER) {
|
|---|
| 684 | ipc_call_free(call);
|
|---|
| 685 | goto restart;
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | STRUCT_TO_USPACE(&calldata->args, &call->data.args);
|
|---|
| 689 | ipc_call_free(call);
|
|---|
| 690 |
|
|---|
| 691 | return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
|
|---|
| 692 | }
|
|---|
| 693 |
|
|---|
| 694 | if (process_request(&TASK->answerbox, call))
|
|---|
| 695 | goto restart;
|
|---|
| 696 |
|
|---|
| 697 | /* Include phone address('id') of the caller in the request,
|
|---|
| 698 | * copy whole call->data, not only call->data.args */
|
|---|
| 699 | if (STRUCT_TO_USPACE(calldata, &call->data)) {
|
|---|
| 700 | /*
|
|---|
| 701 | * The callee will not receive this call and no one else has
|
|---|
| 702 | * a chance to answer it. Reply with the EPARTY error code.
|
|---|
| 703 | */
|
|---|
| 704 | ipc_data_t saved_data;
|
|---|
| 705 | bool saved;
|
|---|
| 706 |
|
|---|
| 707 | if (answer_need_old(call)) {
|
|---|
| 708 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
|---|
| 709 | saved = true;
|
|---|
| 710 | } else
|
|---|
| 711 | saved = false;
|
|---|
| 712 |
|
|---|
| 713 | IPC_SET_RETVAL(call->data, EPARTY);
|
|---|
| 714 | (void) answer_preprocess(call, saved ? &saved_data : NULL);
|
|---|
| 715 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 716 | return 0;
|
|---|
| 717 | }
|
|---|
| 718 |
|
|---|
| 719 | return (sysarg_t) call;
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | /** Interrupt one thread from sys_ipc_wait_for_call().
|
|---|
| 723 | *
|
|---|
| 724 | */
|
|---|
| 725 | sysarg_t sys_ipc_poke(void)
|
|---|
| 726 | {
|
|---|
| 727 | waitq_unsleep(&TASK->answerbox.wq);
|
|---|
| 728 | return EOK;
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | /** Connect an IRQ handler to a task.
|
|---|
| 732 | *
|
|---|
| 733 | * @param inr IRQ number.
|
|---|
| 734 | * @param devno Device number.
|
|---|
| 735 | * @param imethod Interface and method to be associated with the notification.
|
|---|
| 736 | * @param ucode Uspace pointer to the top-half pseudocode.
|
|---|
| 737 | *
|
|---|
| 738 | * @return EPERM or a return code returned by ipc_irq_subscribe().
|
|---|
| 739 | *
|
|---|
| 740 | */
|
|---|
| 741 | sysarg_t sys_ipc_irq_subscribe(inr_t inr, devno_t devno, sysarg_t imethod,
|
|---|
| 742 | irq_code_t *ucode)
|
|---|
| 743 | {
|
|---|
| 744 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
|---|
| 745 | return EPERM;
|
|---|
| 746 |
|
|---|
| 747 | return ipc_irq_subscribe(&TASK->answerbox, inr, devno, imethod, ucode);
|
|---|
| 748 | }
|
|---|
| 749 |
|
|---|
| 750 | /** Disconnect an IRQ handler from a task.
|
|---|
| 751 | *
|
|---|
| 752 | * @param inr IRQ number.
|
|---|
| 753 | * @param devno Device number.
|
|---|
| 754 | *
|
|---|
| 755 | * @return Zero on success or EPERM on error.
|
|---|
| 756 | *
|
|---|
| 757 | */
|
|---|
| 758 | sysarg_t sys_ipc_irq_unsubscribe(inr_t inr, devno_t devno)
|
|---|
| 759 | {
|
|---|
| 760 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
|---|
| 761 | return EPERM;
|
|---|
| 762 |
|
|---|
| 763 | ipc_irq_unsubscribe(&TASK->answerbox, inr, devno);
|
|---|
| 764 |
|
|---|
| 765 | return 0;
|
|---|
| 766 | }
|
|---|
| 767 |
|
|---|
| 768 | #ifdef __32_BITS__
|
|---|
| 769 |
|
|---|
| 770 | /** Syscall connect to a task by ID (32 bits)
|
|---|
| 771 | *
|
|---|
| 772 | * @return Phone id on success, or negative error code.
|
|---|
| 773 | *
|
|---|
| 774 | */
|
|---|
| 775 | sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
|
|---|
| 776 | {
|
|---|
| 777 | #ifdef CONFIG_UDEBUG
|
|---|
| 778 | sysarg64_t taskid;
|
|---|
| 779 | int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
|
|---|
| 780 | if (rc != 0)
|
|---|
| 781 | return (sysarg_t) rc;
|
|---|
| 782 |
|
|---|
| 783 | return ipc_connect_kbox((task_id_t) taskid);
|
|---|
| 784 | #else
|
|---|
| 785 | return (sysarg_t) ENOTSUP;
|
|---|
| 786 | #endif
|
|---|
| 787 | }
|
|---|
| 788 |
|
|---|
| 789 | #endif /* __32_BITS__ */
|
|---|
| 790 |
|
|---|
| 791 | #ifdef __64_BITS__
|
|---|
| 792 |
|
|---|
| 793 | /** Syscall connect to a task by ID (64 bits)
|
|---|
| 794 | *
|
|---|
| 795 | * @return Phone id on success, or negative error code.
|
|---|
| 796 | *
|
|---|
| 797 | */
|
|---|
| 798 | sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
|
|---|
| 799 | {
|
|---|
| 800 | #ifdef CONFIG_UDEBUG
|
|---|
| 801 | return ipc_connect_kbox((task_id_t) taskid);
|
|---|
| 802 | #else
|
|---|
| 803 | return (sysarg_t) ENOTSUP;
|
|---|
| 804 | #endif
|
|---|
| 805 | }
|
|---|
| 806 |
|
|---|
| 807 | #endif /* __64_BITS__ */
|
|---|
| 808 |
|
|---|
| 809 | /** @}
|
|---|
| 810 | */
|
|---|