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