| 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 <proc/task.h>
|
|---|
| 37 | #include <proc/thread.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <memstr.h>
|
|---|
| 40 | #include <debug.h>
|
|---|
| 41 | #include <ipc/ipc.h>
|
|---|
| 42 | #include <ipc/sysipc.h>
|
|---|
| 43 | #include <ipc/irq.h>
|
|---|
| 44 | #include <ipc/ipcrsc.h>
|
|---|
| 45 | #include <ipc/kbox.h>
|
|---|
| 46 | #include <synch/waitq.h>
|
|---|
| 47 | #include <udebug/udebug_ipc.h>
|
|---|
| 48 | #include <arch/interrupt.h>
|
|---|
| 49 | #include <syscall/copy.h>
|
|---|
| 50 | #include <security/cap.h>
|
|---|
| 51 | #include <console/console.h>
|
|---|
| 52 | #include <mm/as.h>
|
|---|
| 53 | #include <print.h>
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Maximum buffer size allowed for IPC_M_DATA_WRITE and IPC_M_DATA_READ
|
|---|
| 57 | * requests.
|
|---|
| 58 | */
|
|---|
| 59 | #define DATA_XFER_LIMIT (64 * 1024)
|
|---|
| 60 |
|
|---|
| 61 | #define STRUCT_TO_USPACE(dst, src) copy_to_uspace((dst), (src), sizeof(*(src)))
|
|---|
| 62 |
|
|---|
| 63 | /** Get phone from the current task by ID.
|
|---|
| 64 | *
|
|---|
| 65 | * @param phoneid Phone ID.
|
|---|
| 66 | * @param phone Place to store pointer to phone.
|
|---|
| 67 | *
|
|---|
| 68 | * @return EOK on success, EINVAL if ID is invalid.
|
|---|
| 69 | *
|
|---|
| 70 | */
|
|---|
| 71 | static int phone_get(sysarg_t phoneid, phone_t **phone)
|
|---|
| 72 | {
|
|---|
| 73 | if (phoneid >= IPC_MAX_PHONES)
|
|---|
| 74 | return EINVAL;
|
|---|
| 75 |
|
|---|
| 76 | *phone = &TASK->phones[phoneid];
|
|---|
| 77 | return EOK;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /** Decide if the interface and method is a system method.
|
|---|
| 81 | *
|
|---|
| 82 | * @param imethod Interface and method to be decided.
|
|---|
| 83 | *
|
|---|
| 84 | * @return True if the interface and method is a system
|
|---|
| 85 | * interface and method.
|
|---|
| 86 | *
|
|---|
| 87 | */
|
|---|
| 88 | static inline bool method_is_system(sysarg_t imethod)
|
|---|
| 89 | {
|
|---|
| 90 | if (imethod <= IPC_M_LAST_SYSTEM)
|
|---|
| 91 | return true;
|
|---|
| 92 |
|
|---|
| 93 | return false;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** Decide if the message with this interface and method is forwardable.
|
|---|
| 97 | *
|
|---|
| 98 | * Some system messages may be forwarded, for some of them
|
|---|
| 99 | * it is useless.
|
|---|
| 100 | *
|
|---|
| 101 | * @param imethod Interface and method to be decided.
|
|---|
| 102 | *
|
|---|
| 103 | * @return True if the interface and method is forwardable.
|
|---|
| 104 | *
|
|---|
| 105 | */
|
|---|
| 106 | static inline bool method_is_forwardable(sysarg_t imethod)
|
|---|
| 107 | {
|
|---|
| 108 | switch (imethod) {
|
|---|
| 109 | case IPC_M_CONNECTION_CLONE:
|
|---|
| 110 | case IPC_M_CONNECT_ME:
|
|---|
| 111 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 112 | /* This message is meant only for the original recipient. */
|
|---|
| 113 | return false;
|
|---|
| 114 | default:
|
|---|
| 115 | return true;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /** Decide if the message with this interface and method is immutable on forward.
|
|---|
| 120 | *
|
|---|
| 121 | * Some system messages may be forwarded but their content cannot be altered.
|
|---|
| 122 | *
|
|---|
| 123 | * @param imethod Interface and method to be decided.
|
|---|
| 124 | *
|
|---|
| 125 | * @return True if the interface and method is immutable on forward.
|
|---|
| 126 | *
|
|---|
| 127 | */
|
|---|
| 128 | static inline bool method_is_immutable(sysarg_t imethod)
|
|---|
| 129 | {
|
|---|
| 130 | switch (imethod) {
|
|---|
| 131 | case IPC_M_SHARE_OUT:
|
|---|
| 132 | case IPC_M_SHARE_IN:
|
|---|
| 133 | case IPC_M_DATA_WRITE:
|
|---|
| 134 | case IPC_M_DATA_READ:
|
|---|
| 135 | return true;
|
|---|
| 136 | default:
|
|---|
| 137 | return false;
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | /***********************************************************************
|
|---|
| 143 | * Functions that preprocess answer before sending it to the recepient.
|
|---|
| 144 | ***********************************************************************/
|
|---|
| 145 |
|
|---|
| 146 | /** Decide if the caller (e.g. ipc_answer()) should save the old call contents
|
|---|
| 147 | * for answer_preprocess().
|
|---|
| 148 | *
|
|---|
| 149 | * @param call Call structure to be decided.
|
|---|
| 150 | *
|
|---|
| 151 | * @return true if the old call contents should be saved.
|
|---|
| 152 | *
|
|---|
| 153 | */
|
|---|
| 154 | static inline bool answer_need_old(call_t *call)
|
|---|
| 155 | {
|
|---|
| 156 | switch (IPC_GET_IMETHOD(call->data)) {
|
|---|
| 157 | case IPC_M_CONNECTION_CLONE:
|
|---|
| 158 | case IPC_M_CONNECT_ME:
|
|---|
| 159 | case IPC_M_CONNECT_TO_ME:
|
|---|
| 160 | case IPC_M_CONNECT_ME_TO:
|
|---|
| 161 | case IPC_M_SHARE_OUT:
|
|---|
| 162 | case IPC_M_SHARE_IN:
|
|---|
| 163 | case IPC_M_DATA_WRITE:
|
|---|
| 164 | case IPC_M_DATA_READ:
|
|---|
| 165 | return true;
|
|---|
| 166 | default:
|
|---|
| 167 | return false;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /** Interpret process answer as control information.
|
|---|
| 172 | *
|
|---|
| 173 | * This function is called directly after sys_ipc_answer().
|
|---|
| 174 | *
|
|---|
| 175 | * @param answer Call structure with the answer.
|
|---|
| 176 | * @param olddata Saved data of the request.
|
|---|
| 177 | *
|
|---|
| 178 | * @return Return 0 on success or an error code.
|
|---|
| 179 | *
|
|---|
| 180 | */
|
|---|
| 181 | static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
|
|---|
| 182 | {
|
|---|
| 183 | if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
|
|---|
| 184 | /* In case of forward, hangup the forwared phone,
|
|---|
| 185 | * not the originator
|
|---|
| 186 | */
|
|---|
| 187 | mutex_lock(&answer->data.phone->lock);
|
|---|
| 188 | irq_spinlock_lock(&TASK->answerbox.lock, true);
|
|---|
| 189 | if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
|
|---|
| 190 | list_remove(&answer->data.phone->link);
|
|---|
| 191 | answer->data.phone->state = IPC_PHONE_SLAMMED;
|
|---|
| 192 | }
|
|---|
| 193 | irq_spinlock_unlock(&TASK->answerbox.lock, true);
|
|---|
| 194 | mutex_unlock(&answer->data.phone->lock);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | if (!olddata)
|
|---|
| 198 | return 0;
|
|---|
| 199 |
|
|---|
| 200 | if (IPC_GET_IMETHOD(*olddata) == IPC_M_CONNECTION_CLONE) {
|
|---|
| 201 | int phoneid = IPC_GET_ARG1(*olddata);
|
|---|
| 202 | phone_t *phone = &TASK->phones[phoneid];
|
|---|
| 203 |
|
|---|
| 204 | if (IPC_GET_RETVAL(answer->data) != EOK) {
|
|---|
| 205 | /*
|
|---|
| 206 | * The recipient of the cloned phone rejected the offer.
|
|---|
| 207 | * In this case, the connection was established at the
|
|---|
| 208 | * request time and therefore we need to slam the phone.
|
|---|
| 209 | * We don't merely hangup as that would result in
|
|---|
| 210 | * sending IPC_M_HUNGUP to the third party on the
|
|---|
| 211 | * other side of the cloned phone.
|
|---|
| 212 | */
|
|---|
| 213 | mutex_lock(&phone->lock);
|
|---|
| 214 | if (phone->state == IPC_PHONE_CONNECTED) {
|
|---|
| 215 | irq_spinlock_lock(&phone->callee->lock, true);
|
|---|
| 216 | list_remove(&phone->link);
|
|---|
| 217 | phone->state = IPC_PHONE_SLAMMED;
|
|---|
| 218 | irq_spinlock_unlock(&phone->callee->lock, true);
|
|---|
| 219 | }
|
|---|
| 220 | mutex_unlock(&phone->lock);
|
|---|
| 221 | }
|
|---|
| 222 | } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_CONNECT_ME) {
|
|---|
| 223 | phone_t *phone = (phone_t *) IPC_GET_ARG5(*olddata);
|
|---|
| 224 |
|
|---|
| 225 | if (IPC_GET_RETVAL(answer->data) != EOK) {
|
|---|
| 226 | /*
|
|---|
| 227 | * The other party on the cloned phoned rejected our
|
|---|
| 228 | * request for connection on the protocol level.
|
|---|
| 229 | * We need to break the connection without sending
|
|---|
| 230 | * IPC_M_HUNGUP back.
|
|---|
| 231 | */
|
|---|
| 232 | mutex_lock(&phone->lock);
|
|---|
| 233 | if (phone->state == IPC_PHONE_CONNECTED) {
|
|---|
| 234 | irq_spinlock_lock(&phone->callee->lock, true);
|
|---|
| 235 | list_remove(&phone->link);
|
|---|
| 236 | phone->state = IPC_PHONE_SLAMMED;
|
|---|
| 237 | irq_spinlock_unlock(&phone->callee->lock, true);
|
|---|
| 238 | }
|
|---|
| 239 | mutex_unlock(&phone->lock);
|
|---|
| 240 | }
|
|---|
| 241 | } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
|
|---|
| 242 | int phoneid = IPC_GET_ARG5(*olddata);
|
|---|
| 243 |
|
|---|
| 244 | if (IPC_GET_RETVAL(answer->data) != EOK) {
|
|---|
| 245 | /* The connection was not accepted */
|
|---|
| 246 | phone_dealloc(phoneid);
|
|---|
| 247 | } else {
|
|---|
| 248 | /* The connection was accepted */
|
|---|
| 249 | phone_connect(phoneid, &answer->sender->answerbox);
|
|---|
| 250 | /* Set 'phone hash' as arg5 of response */
|
|---|
| 251 | IPC_SET_ARG5(answer->data,
|
|---|
| 252 | (sysarg_t) &TASK->phones[phoneid]);
|
|---|
| 253 | }
|
|---|
| 254 | } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
|
|---|
| 255 | /* If the users accepted call, connect */
|
|---|
| 256 | if (IPC_GET_RETVAL(answer->data) == EOK) {
|
|---|
| 257 | ipc_phone_connect((phone_t *) IPC_GET_ARG5(*olddata),
|
|---|
| 258 | &TASK->answerbox);
|
|---|
| 259 | }
|
|---|
| 260 | } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_SHARE_OUT) {
|
|---|
| 261 | if (!IPC_GET_RETVAL(answer->data)) {
|
|---|
| 262 | /* Accepted, handle as_area receipt */
|
|---|
| 263 |
|
|---|
| 264 | irq_spinlock_lock(&answer->sender->lock, true);
|
|---|
| 265 | as_t *as = answer->sender->as;
|
|---|
| 266 | irq_spinlock_unlock(&answer->sender->lock, true);
|
|---|
| 267 |
|
|---|
| 268 | int rc = as_area_share(as, IPC_GET_ARG1(*olddata),
|
|---|
| 269 | IPC_GET_ARG2(*olddata), AS,
|
|---|
| 270 | IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
|
|---|
| 271 | IPC_SET_RETVAL(answer->data, rc);
|
|---|
| 272 | return rc;
|
|---|
| 273 | }
|
|---|
| 274 | } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_SHARE_IN) {
|
|---|
| 275 | if (!IPC_GET_RETVAL(answer->data)) {
|
|---|
| 276 | irq_spinlock_lock(&answer->sender->lock, true);
|
|---|
| 277 | as_t *as = answer->sender->as;
|
|---|
| 278 | irq_spinlock_unlock(&answer->sender->lock, true);
|
|---|
| 279 |
|
|---|
| 280 | int rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
|
|---|
| 281 | IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
|
|---|
| 282 | IPC_GET_ARG2(answer->data));
|
|---|
| 283 | IPC_SET_RETVAL(answer->data, rc);
|
|---|
| 284 | }
|
|---|
| 285 | } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_DATA_READ) {
|
|---|
| 286 | ASSERT(!answer->buffer);
|
|---|
| 287 | if (!IPC_GET_RETVAL(answer->data)) {
|
|---|
| 288 | /* The recipient agreed to send data. */
|
|---|
| 289 | uintptr_t src = IPC_GET_ARG1(answer->data);
|
|---|
| 290 | uintptr_t dst = IPC_GET_ARG1(*olddata);
|
|---|
| 291 | size_t max_size = IPC_GET_ARG2(*olddata);
|
|---|
| 292 | size_t size = IPC_GET_ARG2(answer->data);
|
|---|
| 293 | if (size && size <= max_size) {
|
|---|
| 294 | /*
|
|---|
| 295 | * Copy the destination VA so that this piece of
|
|---|
| 296 | * information is not lost.
|
|---|
| 297 | */
|
|---|
| 298 | IPC_SET_ARG1(answer->data, dst);
|
|---|
| 299 |
|
|---|
| 300 | answer->buffer = malloc(size, 0);
|
|---|
| 301 | int rc = copy_from_uspace(answer->buffer,
|
|---|
| 302 | (void *) src, size);
|
|---|
| 303 | if (rc) {
|
|---|
| 304 | IPC_SET_RETVAL(answer->data, rc);
|
|---|
| 305 | free(answer->buffer);
|
|---|
| 306 | answer->buffer = NULL;
|
|---|
| 307 | }
|
|---|
| 308 | } else if (!size) {
|
|---|
| 309 | IPC_SET_RETVAL(answer->data, EOK);
|
|---|
| 310 | } else {
|
|---|
| 311 | IPC_SET_RETVAL(answer->data, ELIMIT);
|
|---|
| 312 | }
|
|---|
| 313 | }
|
|---|
| 314 | } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_DATA_WRITE) {
|
|---|
| 315 | ASSERT(answer->buffer);
|
|---|
| 316 | if (!IPC_GET_RETVAL(answer->data)) {
|
|---|
| 317 | /* The recipient agreed to receive data. */
|
|---|
| 318 | uintptr_t dst = (uintptr_t)IPC_GET_ARG1(answer->data);
|
|---|
| 319 | size_t size = (size_t)IPC_GET_ARG2(answer->data);
|
|---|
| 320 | size_t max_size = (size_t)IPC_GET_ARG2(*olddata);
|
|---|
| 321 |
|
|---|
| 322 | if (size <= max_size) {
|
|---|
| 323 | int rc = copy_to_uspace((void *) dst,
|
|---|
| 324 | answer->buffer, size);
|
|---|
| 325 | if (rc)
|
|---|
| 326 | IPC_SET_RETVAL(answer->data, rc);
|
|---|
| 327 | } else {
|
|---|
| 328 | IPC_SET_RETVAL(answer->data, ELIMIT);
|
|---|
| 329 | }
|
|---|
| 330 | }
|
|---|
| 331 | free(answer->buffer);
|
|---|
| 332 | answer->buffer = NULL;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | return 0;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | static void phones_lock(phone_t *p1, phone_t *p2)
|
|---|
| 339 | {
|
|---|
| 340 | if (p1 < p2) {
|
|---|
| 341 | mutex_lock(&p1->lock);
|
|---|
| 342 | mutex_lock(&p2->lock);
|
|---|
| 343 | } else if (p1 > p2) {
|
|---|
| 344 | mutex_lock(&p2->lock);
|
|---|
| 345 | mutex_lock(&p1->lock);
|
|---|
| 346 | } else
|
|---|
| 347 | mutex_lock(&p1->lock);
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | static void phones_unlock(phone_t *p1, phone_t *p2)
|
|---|
| 351 | {
|
|---|
| 352 | mutex_unlock(&p1->lock);
|
|---|
| 353 | if (p1 != p2)
|
|---|
| 354 | mutex_unlock(&p2->lock);
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | /** Called before the request is sent.
|
|---|
| 358 | *
|
|---|
| 359 | * @param call Call structure with the request.
|
|---|
| 360 | * @param phone Phone that the call will be sent through.
|
|---|
| 361 | *
|
|---|
| 362 | * @return Return 0 on success, ELIMIT or EPERM on error.
|
|---|
| 363 | *
|
|---|
| 364 | */
|
|---|
| 365 | static int request_preprocess(call_t *call, phone_t *phone)
|
|---|
| 366 | {
|
|---|
| 367 | switch (IPC_GET_IMETHOD(call->data)) {
|
|---|
| 368 | case IPC_M_CONNECTION_CLONE: {
|
|---|
| 369 | phone_t *cloned_phone;
|
|---|
| 370 | if (phone_get(IPC_GET_ARG1(call->data), &cloned_phone) != EOK)
|
|---|
| 371 | return ENOENT;
|
|---|
| 372 |
|
|---|
| 373 | phones_lock(cloned_phone, phone);
|
|---|
| 374 |
|
|---|
| 375 | if ((cloned_phone->state != IPC_PHONE_CONNECTED) ||
|
|---|
| 376 | phone->state != IPC_PHONE_CONNECTED) {
|
|---|
| 377 | phones_unlock(cloned_phone, phone);
|
|---|
| 378 | return EINVAL;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | /*
|
|---|
| 382 | * We can be pretty sure now that both tasks exist and we are
|
|---|
| 383 | * connected to them. As we continue to hold the phone locks,
|
|---|
| 384 | * we are effectively preventing them from finishing their
|
|---|
| 385 | * potential cleanup.
|
|---|
| 386 | *
|
|---|
| 387 | */
|
|---|
| 388 | int newphid = phone_alloc(phone->callee->task);
|
|---|
| 389 | if (newphid < 0) {
|
|---|
| 390 | phones_unlock(cloned_phone, phone);
|
|---|
| 391 | return ELIMIT;
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | ipc_phone_connect(&phone->callee->task->phones[newphid],
|
|---|
| 395 | cloned_phone->callee);
|
|---|
| 396 | phones_unlock(cloned_phone, phone);
|
|---|
| 397 |
|
|---|
| 398 | /* Set the new phone for the callee. */
|
|---|
| 399 | IPC_SET_ARG1(call->data, newphid);
|
|---|
| 400 | break;
|
|---|
| 401 | }
|
|---|
| 402 | case IPC_M_CONNECT_ME:
|
|---|
| 403 | IPC_SET_ARG5(call->data, (sysarg_t) phone);
|
|---|
| 404 | break;
|
|---|
| 405 | case IPC_M_CONNECT_ME_TO: {
|
|---|
| 406 | int newphid = phone_alloc(TASK);
|
|---|
| 407 | if (newphid < 0)
|
|---|
| 408 | return ELIMIT;
|
|---|
| 409 |
|
|---|
| 410 | /* Set arg5 for server */
|
|---|
| 411 | IPC_SET_ARG5(call->data, (sysarg_t) &TASK->phones[newphid]);
|
|---|
| 412 | call->flags |= IPC_CALL_CONN_ME_TO;
|
|---|
| 413 | call->priv = newphid;
|
|---|
| 414 | break;
|
|---|
| 415 | }
|
|---|
| 416 | case IPC_M_SHARE_OUT: {
|
|---|
| 417 | size_t size = as_area_get_size(IPC_GET_ARG1(call->data));
|
|---|
| 418 | if (!size)
|
|---|
| 419 | return EPERM;
|
|---|
| 420 |
|
|---|
| 421 | IPC_SET_ARG2(call->data, size);
|
|---|
| 422 | break;
|
|---|
| 423 | }
|
|---|
| 424 | case IPC_M_DATA_READ: {
|
|---|
| 425 | size_t size = IPC_GET_ARG2(call->data);
|
|---|
| 426 | if ((size <= 0 || (size > DATA_XFER_LIMIT)))
|
|---|
| 427 | return ELIMIT;
|
|---|
| 428 |
|
|---|
| 429 | break;
|
|---|
| 430 | }
|
|---|
| 431 | case IPC_M_DATA_WRITE: {
|
|---|
| 432 | uintptr_t src = IPC_GET_ARG1(call->data);
|
|---|
| 433 | size_t size = IPC_GET_ARG2(call->data);
|
|---|
| 434 |
|
|---|
| 435 | if (size > DATA_XFER_LIMIT)
|
|---|
| 436 | return ELIMIT;
|
|---|
| 437 |
|
|---|
| 438 | call->buffer = (uint8_t *) malloc(size, 0);
|
|---|
| 439 | int rc = copy_from_uspace(call->buffer, (void *) src, size);
|
|---|
| 440 | if (rc != 0) {
|
|---|
| 441 | free(call->buffer);
|
|---|
| 442 | return rc;
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | break;
|
|---|
| 446 | }
|
|---|
| 447 | #ifdef CONFIG_UDEBUG
|
|---|
| 448 | case IPC_M_DEBUG_ALL:
|
|---|
| 449 | return udebug_request_preprocess(call, phone);
|
|---|
| 450 | #endif
|
|---|
| 451 | default:
|
|---|
| 452 | break;
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | return 0;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | /*******************************************************************************
|
|---|
| 459 | * Functions called to process received call/answer before passing it to uspace.
|
|---|
| 460 | *******************************************************************************/
|
|---|
| 461 |
|
|---|
| 462 | /** Do basic kernel processing of received call answer.
|
|---|
| 463 | *
|
|---|
| 464 | * @param call Call structure with the answer.
|
|---|
| 465 | *
|
|---|
| 466 | */
|
|---|
| 467 | static void process_answer(call_t *call)
|
|---|
| 468 | {
|
|---|
| 469 | if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
|
|---|
| 470 | (call->flags & IPC_CALL_FORWARDED))
|
|---|
| 471 | IPC_SET_RETVAL(call->data, EFORWARD);
|
|---|
| 472 |
|
|---|
| 473 | if (call->flags & IPC_CALL_CONN_ME_TO) {
|
|---|
| 474 | if (IPC_GET_RETVAL(call->data))
|
|---|
| 475 | phone_dealloc(call->priv);
|
|---|
| 476 | else
|
|---|
| 477 | IPC_SET_ARG5(call->data, call->priv);
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 | if (call->buffer) {
|
|---|
| 481 | /*
|
|---|
| 482 | * This must be an affirmative answer to IPC_M_DATA_READ
|
|---|
| 483 | * or IPC_M_DEBUG_ALL/UDEBUG_M_MEM_READ...
|
|---|
| 484 | *
|
|---|
| 485 | */
|
|---|
| 486 | uintptr_t dst = IPC_GET_ARG1(call->data);
|
|---|
| 487 | size_t size = IPC_GET_ARG2(call->data);
|
|---|
| 488 | int rc = copy_to_uspace((void *) dst, call->buffer, size);
|
|---|
| 489 | if (rc)
|
|---|
| 490 | IPC_SET_RETVAL(call->data, rc);
|
|---|
| 491 | free(call->buffer);
|
|---|
| 492 | call->buffer = NULL;
|
|---|
| 493 | }
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | /** Do basic kernel processing of received call request.
|
|---|
| 497 | *
|
|---|
| 498 | * @param box Destination answerbox structure.
|
|---|
| 499 | * @param call Call structure with the request.
|
|---|
| 500 | *
|
|---|
| 501 | * @return 0 if the call should be passed to userspace.
|
|---|
| 502 | * @return -1 if the call should be ignored.
|
|---|
| 503 | *
|
|---|
| 504 | */
|
|---|
| 505 | static int process_request(answerbox_t *box, call_t *call)
|
|---|
| 506 | {
|
|---|
| 507 | if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME) {
|
|---|
| 508 | int phoneid = phone_alloc(TASK);
|
|---|
| 509 | if (phoneid < 0) { /* Failed to allocate phone */
|
|---|
| 510 | IPC_SET_RETVAL(call->data, ELIMIT);
|
|---|
| 511 | ipc_answer(box, call);
|
|---|
| 512 | return -1;
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | IPC_SET_ARG5(call->data, phoneid);
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | switch (IPC_GET_IMETHOD(call->data)) {
|
|---|
| 519 | case IPC_M_DEBUG_ALL:
|
|---|
| 520 | return -1;
|
|---|
| 521 | default:
|
|---|
| 522 | break;
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | return 0;
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | /** Make a fast call over IPC, wait for reply and return to user.
|
|---|
| 529 | *
|
|---|
| 530 | * This function can handle only three arguments of payload, but is faster than
|
|---|
| 531 | * the generic function (i.e. sys_ipc_call_sync_slow()).
|
|---|
| 532 | *
|
|---|
| 533 | * @param phoneid Phone handle for the call.
|
|---|
| 534 | * @param imethod Interface and method of the call.
|
|---|
| 535 | * @param arg1 Service-defined payload argument.
|
|---|
| 536 | * @param arg2 Service-defined payload argument.
|
|---|
| 537 | * @param arg3 Service-defined payload argument.
|
|---|
| 538 | * @param data Address of user-space structure where the reply call will
|
|---|
| 539 | * be stored.
|
|---|
| 540 | *
|
|---|
| 541 | * @return 0 on success.
|
|---|
| 542 | * @return ENOENT if there is no such phone handle.
|
|---|
| 543 | *
|
|---|
| 544 | */
|
|---|
| 545 | sysarg_t sys_ipc_call_sync_fast(sysarg_t phoneid, sysarg_t imethod,
|
|---|
| 546 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, ipc_data_t *data)
|
|---|
| 547 | {
|
|---|
| 548 | phone_t *phone;
|
|---|
| 549 | if (phone_get(phoneid, &phone) != EOK)
|
|---|
| 550 | return ENOENT;
|
|---|
| 551 |
|
|---|
| 552 | call_t *call = ipc_call_alloc(0);
|
|---|
| 553 | IPC_SET_IMETHOD(call->data, imethod);
|
|---|
| 554 | IPC_SET_ARG1(call->data, arg1);
|
|---|
| 555 | IPC_SET_ARG2(call->data, arg2);
|
|---|
| 556 | IPC_SET_ARG3(call->data, arg3);
|
|---|
| 557 |
|
|---|
| 558 | /*
|
|---|
| 559 | * To achieve deterministic behavior, zero out arguments that are beyond
|
|---|
| 560 | * the limits of the fast version.
|
|---|
| 561 | */
|
|---|
| 562 | IPC_SET_ARG4(call->data, 0);
|
|---|
| 563 | IPC_SET_ARG5(call->data, 0);
|
|---|
| 564 |
|
|---|
| 565 | int res = request_preprocess(call, phone);
|
|---|
| 566 | int rc;
|
|---|
| 567 |
|
|---|
| 568 | if (!res) {
|
|---|
| 569 | #ifdef CONFIG_UDEBUG
|
|---|
| 570 | udebug_stoppable_begin();
|
|---|
| 571 | #endif
|
|---|
| 572 | rc = ipc_call_sync(phone, call);
|
|---|
| 573 | #ifdef CONFIG_UDEBUG
|
|---|
| 574 | udebug_stoppable_end();
|
|---|
| 575 | #endif
|
|---|
| 576 |
|
|---|
| 577 | if (rc != EOK) {
|
|---|
| 578 | /* The call will be freed by ipc_cleanup(). */
|
|---|
| 579 | return rc;
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | process_answer(call);
|
|---|
| 583 | } else
|
|---|
| 584 | IPC_SET_RETVAL(call->data, res);
|
|---|
| 585 |
|
|---|
| 586 | rc = STRUCT_TO_USPACE(&data->args, &call->data.args);
|
|---|
| 587 | ipc_call_free(call);
|
|---|
| 588 | if (rc != 0)
|
|---|
| 589 | return rc;
|
|---|
| 590 |
|
|---|
| 591 | return 0;
|
|---|
| 592 | }
|
|---|
| 593 |
|
|---|
| 594 | /** Make a synchronous IPC call allowing to transmit the entire payload.
|
|---|
| 595 | *
|
|---|
| 596 | * @param phoneid Phone handle for the call.
|
|---|
| 597 | * @param request User-space address of call data with the request.
|
|---|
| 598 | * @param reply User-space address of call data where to store the
|
|---|
| 599 | * answer.
|
|---|
| 600 | *
|
|---|
| 601 | * @return Zero on success or an error code.
|
|---|
| 602 | *
|
|---|
| 603 | */
|
|---|
| 604 | sysarg_t sys_ipc_call_sync_slow(sysarg_t phoneid, ipc_data_t *request,
|
|---|
| 605 | ipc_data_t *reply)
|
|---|
| 606 | {
|
|---|
| 607 | phone_t *phone;
|
|---|
| 608 | if (phone_get(phoneid, &phone) != EOK)
|
|---|
| 609 | return ENOENT;
|
|---|
| 610 |
|
|---|
| 611 | call_t *call = ipc_call_alloc(0);
|
|---|
| 612 | int rc = copy_from_uspace(&call->data.args, &request->args,
|
|---|
| 613 | sizeof(call->data.args));
|
|---|
| 614 | if (rc != 0) {
|
|---|
| 615 | ipc_call_free(call);
|
|---|
| 616 | return (sysarg_t) rc;
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | int res = request_preprocess(call, phone);
|
|---|
| 620 |
|
|---|
| 621 | if (!res) {
|
|---|
| 622 | #ifdef CONFIG_UDEBUG
|
|---|
| 623 | udebug_stoppable_begin();
|
|---|
| 624 | #endif
|
|---|
| 625 | rc = ipc_call_sync(phone, call);
|
|---|
| 626 | #ifdef CONFIG_UDEBUG
|
|---|
| 627 | udebug_stoppable_end();
|
|---|
| 628 | #endif
|
|---|
| 629 |
|
|---|
| 630 | if (rc != EOK) {
|
|---|
| 631 | /* The call will be freed by ipc_cleanup(). */
|
|---|
| 632 | return rc;
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | process_answer(call);
|
|---|
| 636 | } else
|
|---|
| 637 | IPC_SET_RETVAL(call->data, res);
|
|---|
| 638 |
|
|---|
| 639 | rc = STRUCT_TO_USPACE(&reply->args, &call->data.args);
|
|---|
| 640 | ipc_call_free(call);
|
|---|
| 641 | if (rc != 0)
|
|---|
| 642 | return rc;
|
|---|
| 643 |
|
|---|
| 644 | return 0;
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | /** Check that the task did not exceed the allowed limit of asynchronous calls
|
|---|
| 648 | * made over a phone.
|
|---|
| 649 | *
|
|---|
| 650 | * @param phone Phone to check the limit against.
|
|---|
| 651 | *
|
|---|
| 652 | * @return 0 if limit not reached or -1 if limit exceeded.
|
|---|
| 653 | *
|
|---|
| 654 | */
|
|---|
| 655 | static int check_call_limit(phone_t *phone)
|
|---|
| 656 | {
|
|---|
| 657 | if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
|
|---|
| 658 | return -1;
|
|---|
| 659 |
|
|---|
| 660 | return 0;
|
|---|
| 661 | }
|
|---|
| 662 |
|
|---|
| 663 | /** Make a fast asynchronous call over IPC.
|
|---|
| 664 | *
|
|---|
| 665 | * This function can only handle four arguments of payload, but is faster than
|
|---|
| 666 | * the generic function sys_ipc_call_async_slow().
|
|---|
| 667 | *
|
|---|
| 668 | * @param phoneid Phone handle for the call.
|
|---|
| 669 | * @param imethod Interface and method of the call.
|
|---|
| 670 | * @param arg1 Service-defined payload argument.
|
|---|
| 671 | * @param arg2 Service-defined payload argument.
|
|---|
| 672 | * @param arg3 Service-defined payload argument.
|
|---|
| 673 | * @param arg4 Service-defined payload argument.
|
|---|
| 674 | *
|
|---|
| 675 | * @return Call hash on success.
|
|---|
| 676 | * @return IPC_CALLRET_FATAL in case of a fatal error.
|
|---|
| 677 | * @return IPC_CALLRET_TEMPORARY if there are too many pending
|
|---|
| 678 | * asynchronous requests; answers should be handled first.
|
|---|
| 679 | *
|
|---|
| 680 | */
|
|---|
| 681 | sysarg_t sys_ipc_call_async_fast(sysarg_t phoneid, sysarg_t imethod,
|
|---|
| 682 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
|---|
| 683 | {
|
|---|
| 684 | phone_t *phone;
|
|---|
| 685 | if (phone_get(phoneid, &phone) != EOK)
|
|---|
| 686 | return IPC_CALLRET_FATAL;
|
|---|
| 687 |
|
|---|
| 688 | if (check_call_limit(phone))
|
|---|
| 689 | return IPC_CALLRET_TEMPORARY;
|
|---|
| 690 |
|
|---|
| 691 | call_t *call = ipc_call_alloc(0);
|
|---|
| 692 | IPC_SET_IMETHOD(call->data, imethod);
|
|---|
| 693 | IPC_SET_ARG1(call->data, arg1);
|
|---|
| 694 | IPC_SET_ARG2(call->data, arg2);
|
|---|
| 695 | IPC_SET_ARG3(call->data, arg3);
|
|---|
| 696 | IPC_SET_ARG4(call->data, arg4);
|
|---|
| 697 |
|
|---|
| 698 | /*
|
|---|
| 699 | * To achieve deterministic behavior, zero out arguments that are beyond
|
|---|
| 700 | * the limits of the fast version.
|
|---|
| 701 | */
|
|---|
| 702 | IPC_SET_ARG5(call->data, 0);
|
|---|
| 703 |
|
|---|
| 704 | int res = request_preprocess(call, phone);
|
|---|
| 705 |
|
|---|
| 706 | if (!res)
|
|---|
| 707 | ipc_call(phone, call);
|
|---|
| 708 | else
|
|---|
| 709 | ipc_backsend_err(phone, call, res);
|
|---|
| 710 |
|
|---|
| 711 | return (sysarg_t) call;
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | /** Make an asynchronous IPC call allowing to transmit the entire payload.
|
|---|
| 715 | *
|
|---|
| 716 | * @param phoneid Phone handle for the call.
|
|---|
| 717 | * @param data Userspace address of call data with the request.
|
|---|
| 718 | *
|
|---|
| 719 | * @return See sys_ipc_call_async_fast().
|
|---|
| 720 | *
|
|---|
| 721 | */
|
|---|
| 722 | sysarg_t sys_ipc_call_async_slow(sysarg_t phoneid, ipc_data_t *data)
|
|---|
| 723 | {
|
|---|
| 724 | phone_t *phone;
|
|---|
| 725 | if (phone_get(phoneid, &phone) != EOK)
|
|---|
| 726 | return IPC_CALLRET_FATAL;
|
|---|
| 727 |
|
|---|
| 728 | if (check_call_limit(phone))
|
|---|
| 729 | return IPC_CALLRET_TEMPORARY;
|
|---|
| 730 |
|
|---|
| 731 | call_t *call = ipc_call_alloc(0);
|
|---|
| 732 | int rc = copy_from_uspace(&call->data.args, &data->args,
|
|---|
| 733 | sizeof(call->data.args));
|
|---|
| 734 | if (rc != 0) {
|
|---|
| 735 | ipc_call_free(call);
|
|---|
| 736 | return (sysarg_t) rc;
|
|---|
| 737 | }
|
|---|
| 738 |
|
|---|
| 739 | int res = request_preprocess(call, phone);
|
|---|
| 740 |
|
|---|
| 741 | if (!res)
|
|---|
| 742 | ipc_call(phone, call);
|
|---|
| 743 | else
|
|---|
| 744 | ipc_backsend_err(phone, call, res);
|
|---|
| 745 |
|
|---|
| 746 | return (sysarg_t) call;
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | /** Forward a received call to another destination
|
|---|
| 750 | *
|
|---|
| 751 | * Common code for both the fast and the slow version.
|
|---|
| 752 | *
|
|---|
| 753 | * @param callid Hash of the call to forward.
|
|---|
| 754 | * @param phoneid Phone handle to use for forwarding.
|
|---|
| 755 | * @param imethod New interface and method to use for the forwarded call.
|
|---|
| 756 | * @param arg1 New value of the first argument for the forwarded call.
|
|---|
| 757 | * @param arg2 New value of the second argument for the forwarded call.
|
|---|
| 758 | * @param arg3 New value of the third argument for the forwarded call.
|
|---|
| 759 | * @param arg4 New value of the fourth argument for the forwarded call.
|
|---|
| 760 | * @param arg5 New value of the fifth argument for the forwarded call.
|
|---|
| 761 | * @param mode Flags that specify mode of the forward operation.
|
|---|
| 762 | * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
|
|---|
| 763 | * the function considers only the fast version arguments:
|
|---|
| 764 | * i.e. arg1 and arg2.
|
|---|
| 765 | *
|
|---|
| 766 | * @return 0 on succes, otherwise an error code.
|
|---|
| 767 | *
|
|---|
| 768 | * Warning: Make sure that ARG5 is not rewritten for certain system IPC
|
|---|
| 769 | *
|
|---|
| 770 | */
|
|---|
| 771 | static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t phoneid,
|
|---|
| 772 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
|---|
| 773 | sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
|
|---|
| 774 | {
|
|---|
| 775 | call_t *call = get_call(callid);
|
|---|
| 776 | if (!call)
|
|---|
| 777 | return ENOENT;
|
|---|
| 778 |
|
|---|
| 779 | call->flags |= IPC_CALL_FORWARDED;
|
|---|
| 780 |
|
|---|
| 781 | phone_t *phone;
|
|---|
| 782 | if (phone_get(phoneid, &phone) != EOK) {
|
|---|
| 783 | IPC_SET_RETVAL(call->data, EFORWARD);
|
|---|
| 784 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 785 | return ENOENT;
|
|---|
| 786 | }
|
|---|
| 787 |
|
|---|
| 788 | if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
|
|---|
| 789 | IPC_SET_RETVAL(call->data, EFORWARD);
|
|---|
| 790 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 791 | return EPERM;
|
|---|
| 792 | }
|
|---|
| 793 |
|
|---|
| 794 | /*
|
|---|
| 795 | * Userspace is not allowed to change interface and method of system
|
|---|
| 796 | * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
|
|---|
| 797 | * means of method, arg1, arg2 and arg3.
|
|---|
| 798 | * If the interface and method is immutable, don't change anything.
|
|---|
| 799 | */
|
|---|
| 800 | if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
|
|---|
| 801 | if (method_is_system(IPC_GET_IMETHOD(call->data))) {
|
|---|
| 802 | if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
|
|---|
| 803 | phone_dealloc(IPC_GET_ARG5(call->data));
|
|---|
| 804 |
|
|---|
| 805 | IPC_SET_ARG1(call->data, imethod);
|
|---|
| 806 | IPC_SET_ARG2(call->data, arg1);
|
|---|
| 807 | IPC_SET_ARG3(call->data, arg2);
|
|---|
| 808 |
|
|---|
| 809 | if (slow) {
|
|---|
| 810 | IPC_SET_ARG4(call->data, arg3);
|
|---|
| 811 | /*
|
|---|
| 812 | * For system methods we deliberately don't
|
|---|
| 813 | * overwrite ARG5.
|
|---|
| 814 | */
|
|---|
| 815 | }
|
|---|
| 816 | } else {
|
|---|
| 817 | IPC_SET_IMETHOD(call->data, imethod);
|
|---|
| 818 | IPC_SET_ARG1(call->data, arg1);
|
|---|
| 819 | IPC_SET_ARG2(call->data, arg2);
|
|---|
| 820 | if (slow) {
|
|---|
| 821 | IPC_SET_ARG3(call->data, arg3);
|
|---|
| 822 | IPC_SET_ARG4(call->data, arg4);
|
|---|
| 823 | IPC_SET_ARG5(call->data, arg5);
|
|---|
| 824 | }
|
|---|
| 825 | }
|
|---|
| 826 | }
|
|---|
| 827 |
|
|---|
| 828 | return ipc_forward(call, phone, &TASK->answerbox, mode);
|
|---|
| 829 | }
|
|---|
| 830 |
|
|---|
| 831 | /** Forward a received call to another destination - fast version.
|
|---|
| 832 | *
|
|---|
| 833 | * In case the original interface and method is a system method, ARG1, ARG2
|
|---|
| 834 | * and ARG3 are overwritten in the forwarded message with the new method and
|
|---|
| 835 | * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
|
|---|
| 836 | * are rewritten with the new interface and method, arg1 and arg2, respectively.
|
|---|
| 837 | * Also note there is a set of immutable methods, for which the new method and
|
|---|
| 838 | * arguments are not set and these values are ignored.
|
|---|
| 839 | *
|
|---|
| 840 | * @param callid Hash of the call to forward.
|
|---|
| 841 | * @param phoneid Phone handle to use for forwarding.
|
|---|
| 842 | * @param imethod New interface and method to use for the forwarded call.
|
|---|
| 843 | * @param arg1 New value of the first argument for the forwarded call.
|
|---|
| 844 | * @param arg2 New value of the second argument for the forwarded call.
|
|---|
| 845 | * @param mode Flags that specify mode of the forward operation.
|
|---|
| 846 | *
|
|---|
| 847 | * @return 0 on succes, otherwise an error code.
|
|---|
| 848 | *
|
|---|
| 849 | */
|
|---|
| 850 | sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t phoneid,
|
|---|
| 851 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
|---|
| 852 | {
|
|---|
| 853 | return sys_ipc_forward_common(callid, phoneid, imethod, arg1, arg2, 0, 0,
|
|---|
| 854 | 0, mode, false);
|
|---|
| 855 | }
|
|---|
| 856 |
|
|---|
| 857 | /** Forward a received call to another destination - slow version.
|
|---|
| 858 | *
|
|---|
| 859 | * This function is the slow verision of the sys_ipc_forward_fast interface.
|
|---|
| 860 | * It can copy all five new arguments and the new interface and method from
|
|---|
| 861 | * the userspace. It naturally extends the functionality of the fast version.
|
|---|
| 862 | * For system methods, it additionally stores the new value of arg3 to ARG4.
|
|---|
| 863 | * For non-system methods, it additionally stores the new value of arg3, arg4
|
|---|
| 864 | * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
|
|---|
| 865 | *
|
|---|
| 866 | * @param callid Hash of the call to forward.
|
|---|
| 867 | * @param phoneid Phone handle to use for forwarding.
|
|---|
| 868 | * @param data Userspace address of the new IPC data.
|
|---|
| 869 | * @param mode Flags that specify mode of the forward operation.
|
|---|
| 870 | *
|
|---|
| 871 | * @return 0 on succes, otherwise an error code.
|
|---|
| 872 | *
|
|---|
| 873 | */
|
|---|
| 874 | sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t phoneid,
|
|---|
| 875 | ipc_data_t *data, unsigned int mode)
|
|---|
| 876 | {
|
|---|
| 877 | ipc_data_t newdata;
|
|---|
| 878 | int rc = copy_from_uspace(&newdata.args, &data->args,
|
|---|
| 879 | sizeof(newdata.args));
|
|---|
| 880 | if (rc != 0)
|
|---|
| 881 | return (sysarg_t) rc;
|
|---|
| 882 |
|
|---|
| 883 | return sys_ipc_forward_common(callid, phoneid,
|
|---|
| 884 | IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
|
|---|
| 885 | IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
|
|---|
| 886 | IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
|
|---|
| 887 | }
|
|---|
| 888 |
|
|---|
| 889 | /** Answer an IPC call - fast version.
|
|---|
| 890 | *
|
|---|
| 891 | * This function can handle only two return arguments of payload, but is faster
|
|---|
| 892 | * than the generic sys_ipc_answer().
|
|---|
| 893 | *
|
|---|
| 894 | * @param callid Hash of the call to be answered.
|
|---|
| 895 | * @param retval Return value of the answer.
|
|---|
| 896 | * @param arg1 Service-defined return value.
|
|---|
| 897 | * @param arg2 Service-defined return value.
|
|---|
| 898 | * @param arg3 Service-defined return value.
|
|---|
| 899 | * @param arg4 Service-defined return value.
|
|---|
| 900 | *
|
|---|
| 901 | * @return 0 on success, otherwise an error code.
|
|---|
| 902 | *
|
|---|
| 903 | */
|
|---|
| 904 | sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
|
|---|
| 905 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
|---|
| 906 | {
|
|---|
| 907 | /* Do not answer notification callids */
|
|---|
| 908 | if (callid & IPC_CALLID_NOTIFICATION)
|
|---|
| 909 | return 0;
|
|---|
| 910 |
|
|---|
| 911 | call_t *call = get_call(callid);
|
|---|
| 912 | if (!call)
|
|---|
| 913 | return ENOENT;
|
|---|
| 914 |
|
|---|
| 915 | ipc_data_t saved_data;
|
|---|
| 916 | bool saved;
|
|---|
| 917 |
|
|---|
| 918 | if (answer_need_old(call)) {
|
|---|
| 919 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
|---|
| 920 | saved = true;
|
|---|
| 921 | } else
|
|---|
| 922 | saved = false;
|
|---|
| 923 |
|
|---|
| 924 | IPC_SET_RETVAL(call->data, retval);
|
|---|
| 925 | IPC_SET_ARG1(call->data, arg1);
|
|---|
| 926 | IPC_SET_ARG2(call->data, arg2);
|
|---|
| 927 | IPC_SET_ARG3(call->data, arg3);
|
|---|
| 928 | IPC_SET_ARG4(call->data, arg4);
|
|---|
| 929 |
|
|---|
| 930 | /*
|
|---|
| 931 | * To achieve deterministic behavior, zero out arguments that are beyond
|
|---|
| 932 | * the limits of the fast version.
|
|---|
| 933 | */
|
|---|
| 934 | IPC_SET_ARG5(call->data, 0);
|
|---|
| 935 | int rc = answer_preprocess(call, saved ? &saved_data : NULL);
|
|---|
| 936 |
|
|---|
| 937 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 938 | return rc;
|
|---|
| 939 | }
|
|---|
| 940 |
|
|---|
| 941 | /** Answer an IPC call.
|
|---|
| 942 | *
|
|---|
| 943 | * @param callid Hash of the call to be answered.
|
|---|
| 944 | * @param data Userspace address of call data with the answer.
|
|---|
| 945 | *
|
|---|
| 946 | * @return 0 on success, otherwise an error code.
|
|---|
| 947 | *
|
|---|
| 948 | */
|
|---|
| 949 | sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
|
|---|
| 950 | {
|
|---|
| 951 | /* Do not answer notification callids */
|
|---|
| 952 | if (callid & IPC_CALLID_NOTIFICATION)
|
|---|
| 953 | return 0;
|
|---|
| 954 |
|
|---|
| 955 | call_t *call = get_call(callid);
|
|---|
| 956 | if (!call)
|
|---|
| 957 | return ENOENT;
|
|---|
| 958 |
|
|---|
| 959 | ipc_data_t saved_data;
|
|---|
| 960 | bool saved;
|
|---|
| 961 |
|
|---|
| 962 | if (answer_need_old(call)) {
|
|---|
| 963 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
|---|
| 964 | saved = true;
|
|---|
| 965 | } else
|
|---|
| 966 | saved = false;
|
|---|
| 967 |
|
|---|
| 968 | int rc = copy_from_uspace(&call->data.args, &data->args,
|
|---|
| 969 | sizeof(call->data.args));
|
|---|
| 970 | if (rc != 0)
|
|---|
| 971 | return rc;
|
|---|
| 972 |
|
|---|
| 973 | rc = answer_preprocess(call, saved ? &saved_data : NULL);
|
|---|
| 974 |
|
|---|
| 975 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 976 | return rc;
|
|---|
| 977 | }
|
|---|
| 978 |
|
|---|
| 979 | /** Hang up a phone.
|
|---|
| 980 | *
|
|---|
| 981 | * @param Phone handle of the phone to be hung up.
|
|---|
| 982 | *
|
|---|
| 983 | * @return 0 on success or an error code.
|
|---|
| 984 | *
|
|---|
| 985 | */
|
|---|
| 986 | sysarg_t sys_ipc_hangup(sysarg_t phoneid)
|
|---|
| 987 | {
|
|---|
| 988 | phone_t *phone;
|
|---|
| 989 |
|
|---|
| 990 | if (phone_get(phoneid, &phone) != EOK)
|
|---|
| 991 | return ENOENT;
|
|---|
| 992 |
|
|---|
| 993 | if (ipc_phone_hangup(phone))
|
|---|
| 994 | return -1;
|
|---|
| 995 |
|
|---|
| 996 | return 0;
|
|---|
| 997 | }
|
|---|
| 998 |
|
|---|
| 999 | /** Wait for an incoming IPC call or an answer.
|
|---|
| 1000 | *
|
|---|
| 1001 | * @param calldata Pointer to buffer where the call/answer data is stored.
|
|---|
| 1002 | * @param usec Timeout. See waitq_sleep_timeout() for explanation.
|
|---|
| 1003 | * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
|
|---|
| 1004 | * for explanation.
|
|---|
| 1005 | *
|
|---|
| 1006 | * @return Hash of the call.
|
|---|
| 1007 | * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
|
|---|
| 1008 | * call is a notification. IPC_CALLID_ANSWERED denotes an
|
|---|
| 1009 | * answer.
|
|---|
| 1010 | *
|
|---|
| 1011 | */
|
|---|
| 1012 | sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
|
|---|
| 1013 | unsigned int flags)
|
|---|
| 1014 | {
|
|---|
| 1015 | call_t *call;
|
|---|
| 1016 |
|
|---|
| 1017 | restart:
|
|---|
| 1018 |
|
|---|
| 1019 | #ifdef CONFIG_UDEBUG
|
|---|
| 1020 | udebug_stoppable_begin();
|
|---|
| 1021 | #endif
|
|---|
| 1022 |
|
|---|
| 1023 | call = ipc_wait_for_call(&TASK->answerbox, usec,
|
|---|
| 1024 | flags | SYNCH_FLAGS_INTERRUPTIBLE);
|
|---|
| 1025 |
|
|---|
| 1026 | #ifdef CONFIG_UDEBUG
|
|---|
| 1027 | udebug_stoppable_end();
|
|---|
| 1028 | #endif
|
|---|
| 1029 |
|
|---|
| 1030 | if (!call)
|
|---|
| 1031 | return 0;
|
|---|
| 1032 |
|
|---|
| 1033 | if (call->flags & IPC_CALL_NOTIF) {
|
|---|
| 1034 | /* Set in_phone_hash to the interrupt counter */
|
|---|
| 1035 | call->data.phone = (void *) call->priv;
|
|---|
| 1036 |
|
|---|
| 1037 | STRUCT_TO_USPACE(calldata, &call->data);
|
|---|
| 1038 |
|
|---|
| 1039 | ipc_call_free(call);
|
|---|
| 1040 |
|
|---|
| 1041 | return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
|
|---|
| 1042 | }
|
|---|
| 1043 |
|
|---|
| 1044 | if (call->flags & IPC_CALL_ANSWERED) {
|
|---|
| 1045 | process_answer(call);
|
|---|
| 1046 |
|
|---|
| 1047 | if (call->flags & IPC_CALL_DISCARD_ANSWER) {
|
|---|
| 1048 | ipc_call_free(call);
|
|---|
| 1049 | goto restart;
|
|---|
| 1050 | }
|
|---|
| 1051 |
|
|---|
| 1052 | STRUCT_TO_USPACE(&calldata->args, &call->data.args);
|
|---|
| 1053 | ipc_call_free(call);
|
|---|
| 1054 |
|
|---|
| 1055 | return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
|
|---|
| 1056 | }
|
|---|
| 1057 |
|
|---|
| 1058 | if (process_request(&TASK->answerbox, call))
|
|---|
| 1059 | goto restart;
|
|---|
| 1060 |
|
|---|
| 1061 | /* Include phone address('id') of the caller in the request,
|
|---|
| 1062 | * copy whole call->data, not only call->data.args */
|
|---|
| 1063 | if (STRUCT_TO_USPACE(calldata, &call->data)) {
|
|---|
| 1064 | /*
|
|---|
| 1065 | * The callee will not receive this call and no one else has
|
|---|
| 1066 | * a chance to answer it. Reply with the EPARTY error code.
|
|---|
| 1067 | */
|
|---|
| 1068 | ipc_data_t saved_data;
|
|---|
| 1069 | bool saved;
|
|---|
| 1070 |
|
|---|
| 1071 | if (answer_need_old(call)) {
|
|---|
| 1072 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
|---|
| 1073 | saved = true;
|
|---|
| 1074 | } else
|
|---|
| 1075 | saved = false;
|
|---|
| 1076 |
|
|---|
| 1077 | IPC_SET_RETVAL(call->data, EPARTY);
|
|---|
| 1078 | (void) answer_preprocess(call, saved ? &saved_data : NULL);
|
|---|
| 1079 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 1080 | return 0;
|
|---|
| 1081 | }
|
|---|
| 1082 |
|
|---|
| 1083 | return (sysarg_t) call;
|
|---|
| 1084 | }
|
|---|
| 1085 |
|
|---|
| 1086 | /** Interrupt one thread from sys_ipc_wait_for_call().
|
|---|
| 1087 | *
|
|---|
| 1088 | */
|
|---|
| 1089 | sysarg_t sys_ipc_poke(void)
|
|---|
| 1090 | {
|
|---|
| 1091 | waitq_unsleep(&TASK->answerbox.wq);
|
|---|
| 1092 | return EOK;
|
|---|
| 1093 | }
|
|---|
| 1094 |
|
|---|
| 1095 | /** Connect an IRQ handler to a task.
|
|---|
| 1096 | *
|
|---|
| 1097 | * @param inr IRQ number.
|
|---|
| 1098 | * @param devno Device number.
|
|---|
| 1099 | * @param imethod Interface and method to be associated with the notification.
|
|---|
| 1100 | * @param ucode Uspace pointer to the top-half pseudocode.
|
|---|
| 1101 | *
|
|---|
| 1102 | * @return EPERM or a return code returned by ipc_irq_register().
|
|---|
| 1103 | *
|
|---|
| 1104 | */
|
|---|
| 1105 | sysarg_t sys_ipc_register_irq(inr_t inr, devno_t devno, sysarg_t imethod,
|
|---|
| 1106 | irq_code_t *ucode)
|
|---|
| 1107 | {
|
|---|
| 1108 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
|---|
| 1109 | return EPERM;
|
|---|
| 1110 |
|
|---|
| 1111 | return ipc_irq_register(&TASK->answerbox, inr, devno, imethod, ucode);
|
|---|
| 1112 | }
|
|---|
| 1113 |
|
|---|
| 1114 | /** Disconnect an IRQ handler from a task.
|
|---|
| 1115 | *
|
|---|
| 1116 | * @param inr IRQ number.
|
|---|
| 1117 | * @param devno Device number.
|
|---|
| 1118 | *
|
|---|
| 1119 | * @return Zero on success or EPERM on error.
|
|---|
| 1120 | *
|
|---|
| 1121 | */
|
|---|
| 1122 | sysarg_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
|
|---|
| 1123 | {
|
|---|
| 1124 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
|---|
| 1125 | return EPERM;
|
|---|
| 1126 |
|
|---|
| 1127 | ipc_irq_unregister(&TASK->answerbox, inr, devno);
|
|---|
| 1128 |
|
|---|
| 1129 | return 0;
|
|---|
| 1130 | }
|
|---|
| 1131 |
|
|---|
| 1132 | #ifdef __32_BITS__
|
|---|
| 1133 |
|
|---|
| 1134 | /** Syscall connect to a task by ID (32 bits)
|
|---|
| 1135 | *
|
|---|
| 1136 | * @return Phone id on success, or negative error code.
|
|---|
| 1137 | *
|
|---|
| 1138 | */
|
|---|
| 1139 | sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
|
|---|
| 1140 | {
|
|---|
| 1141 | #ifdef CONFIG_UDEBUG
|
|---|
| 1142 | sysarg64_t taskid;
|
|---|
| 1143 | int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
|
|---|
| 1144 | if (rc != 0)
|
|---|
| 1145 | return (sysarg_t) rc;
|
|---|
| 1146 |
|
|---|
| 1147 | return ipc_connect_kbox((task_id_t) taskid);
|
|---|
| 1148 | #else
|
|---|
| 1149 | return (sysarg_t) ENOTSUP;
|
|---|
| 1150 | #endif
|
|---|
| 1151 | }
|
|---|
| 1152 |
|
|---|
| 1153 | #endif /* __32_BITS__ */
|
|---|
| 1154 |
|
|---|
| 1155 | #ifdef __64_BITS__
|
|---|
| 1156 |
|
|---|
| 1157 | /** Syscall connect to a task by ID (64 bits)
|
|---|
| 1158 | *
|
|---|
| 1159 | * @return Phone id on success, or negative error code.
|
|---|
| 1160 | *
|
|---|
| 1161 | */
|
|---|
| 1162 | sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
|
|---|
| 1163 | {
|
|---|
| 1164 | #ifdef CONFIG_UDEBUG
|
|---|
| 1165 | return ipc_connect_kbox((task_id_t) taskid);
|
|---|
| 1166 | #else
|
|---|
| 1167 | return (sysarg_t) ENOTSUP;
|
|---|
| 1168 | #endif
|
|---|
| 1169 | }
|
|---|
| 1170 |
|
|---|
| 1171 | #endif /* __64_BITS__ */
|
|---|
| 1172 |
|
|---|
| 1173 | /** @}
|
|---|
| 1174 | */
|
|---|