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