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