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