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