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