Rev 2636 | Rev 2638 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1027 | palkovsky | 1 | /* |
| 2071 | jermar | 2 | * Copyright (c) 2006 Ondrej Palkovsky |
| 1027 | palkovsky | 3 | * All rights reserved. |
| 4 | * |
||
| 5 | * Redistribution and use in source and binary forms, with or without |
||
| 6 | * modification, are permitted provided that the following conditions |
||
| 7 | * are met: |
||
| 8 | * |
||
| 9 | * - Redistributions of source code must retain the above copyright |
||
| 10 | * notice, this list of conditions and the following disclaimer. |
||
| 11 | * - Redistributions in binary form must reproduce the above copyright |
||
| 12 | * notice, this list of conditions and the following disclaimer in the |
||
| 13 | * documentation and/or other materials provided with the distribution. |
||
| 14 | * - The name of the author may not be used to endorse or promote products |
||
| 15 | * derived from this software without specific prior written permission. |
||
| 16 | * |
||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 27 | */ |
||
| 28 | |||
| 1757 | jermar | 29 | /** @addtogroup genericipc |
| 1702 | cejka | 30 | * @{ |
| 31 | */ |
||
| 32 | /** @file |
||
| 33 | */ |
||
| 34 | |||
| 1027 | palkovsky | 35 | #include <arch.h> |
| 36 | #include <proc/task.h> |
||
| 1288 | jermar | 37 | #include <proc/thread.h> |
| 1027 | palkovsky | 38 | #include <errno.h> |
| 39 | #include <memstr.h> |
||
| 40 | #include <debug.h> |
||
| 41 | #include <ipc/ipc.h> |
||
| 42 | #include <ipc/sysipc.h> |
||
| 1281 | palkovsky | 43 | #include <ipc/irq.h> |
| 1072 | palkovsky | 44 | #include <ipc/ipcrsc.h> |
| 1258 | palkovsky | 45 | #include <arch/interrupt.h> |
| 1027 | palkovsky | 46 | #include <print.h> |
| 1288 | jermar | 47 | #include <syscall/copy.h> |
| 1297 | jermar | 48 | #include <security/cap.h> |
| 1329 | palkovsky | 49 | #include <mm/as.h> |
| 1875 | jermar | 50 | #include <print.h> |
| 1027 | palkovsky | 51 | |
| 2494 | jermar | 52 | /** Maximum buffer size allowed for IPC_M_DATA_SEND requests. */ |
| 53 | #define DATA_SEND_LIMIT (64 * 1024) |
||
| 54 | |||
| 2471 | jermar | 55 | #define GET_CHECK_PHONE(phone, phoneid, err) \ |
| 56 | { \ |
||
| 57 | if (phoneid > IPC_MAX_PHONES) { \ |
||
| 58 | err; \ |
||
| 59 | } \ |
||
| 60 | phone = &TASK->phones[phoneid]; \ |
||
| 1084 | palkovsky | 61 | } |
| 62 | |||
| 2471 | jermar | 63 | #define STRUCT_TO_USPACE(dst, src) copy_to_uspace(dst, src, sizeof(*(src))) |
| 1084 | palkovsky | 64 | |
| 2471 | jermar | 65 | /** Decide if the method is a system method. |
| 66 | * |
||
| 67 | * @param method Method to be decided. |
||
| 68 | * |
||
| 69 | * @return Return 1 if the method is a system method. |
||
| 70 | * Otherwise return 0. |
||
| 71 | */ |
||
| 2557 | jermar | 72 | static inline int method_is_system(unative_t method) |
| 1040 | palkovsky | 73 | { |
| 74 | if (method <= IPC_M_LAST_SYSTEM) |
||
| 75 | return 1; |
||
| 76 | return 0; |
||
| 77 | } |
||
| 78 | |||
| 2471 | jermar | 79 | /** Decide if the message with this method is forwardable. |
| 1040 | palkovsky | 80 | * |
| 81 | * - some system messages may be forwarded, for some of them |
||
| 82 | * it is useless |
||
| 2471 | jermar | 83 | * |
| 84 | * @param method Method to be decided. |
||
| 85 | * |
||
| 86 | * @return Return 1 if the method is forwardable. |
||
| 87 | * Otherwise return 0. |
||
| 1040 | palkovsky | 88 | */ |
| 2557 | jermar | 89 | static inline int method_is_forwardable(unative_t method) |
| 1040 | palkovsky | 90 | { |
| 2494 | jermar | 91 | switch (method) { |
| 92 | case IPC_M_PHONE_HUNGUP: |
||
| 93 | /* This message is meant only for the original recipient. */ |
||
| 94 | return 0; |
||
| 95 | default: |
||
| 96 | return 1; |
||
| 97 | } |
||
| 1040 | palkovsky | 98 | } |
| 99 | |||
| 2557 | jermar | 100 | /** Decide if the message with this method is immutable on forward. |
| 101 | * |
||
| 2601 | jermar | 102 | * - some system messages may be forwarded but their content cannot be altered |
| 2557 | jermar | 103 | * |
| 104 | * @param method Method to be decided. |
||
| 105 | * |
||
| 106 | * @return Return 1 if the method is immutable on forward. |
||
| 107 | * Otherwise return 0. |
||
| 108 | */ |
||
| 109 | static inline int method_is_immutable(unative_t method) |
||
| 110 | { |
||
| 111 | switch (method) { |
||
| 112 | case IPC_M_AS_AREA_SEND: |
||
| 113 | case IPC_M_AS_AREA_RECV: |
||
| 114 | case IPC_M_DATA_SEND: |
||
| 115 | return 1; |
||
| 116 | break; |
||
| 117 | default: |
||
| 118 | return 0; |
||
| 119 | } |
||
| 120 | } |
||
| 1027 | palkovsky | 121 | |
| 2557 | jermar | 122 | |
| 2471 | jermar | 123 | /*********************************************************************** |
| 124 | * Functions that preprocess answer before sending it to the recepient. |
||
| 125 | ***********************************************************************/ |
||
| 126 | |||
| 127 | /** Decide if the caller (e.g. ipc_answer()) should save the old call contents |
||
| 128 | * for answer_preprocess(). |
||
| 129 | * |
||
| 130 | * @param call Call structure to be decided. |
||
| 131 | * |
||
| 132 | * @return Return 1 if the old call contents should be saved. |
||
| 133 | * Return 0 otherwise. |
||
| 1027 | palkovsky | 134 | */ |
| 1088 | palkovsky | 135 | static inline int answer_need_old(call_t *call) |
| 1027 | palkovsky | 136 | { |
| 2494 | jermar | 137 | switch (IPC_GET_METHOD(call->data)) { |
| 138 | case IPC_M_CONNECT_TO_ME: |
||
| 139 | case IPC_M_CONNECT_ME_TO: |
||
| 140 | case IPC_M_AS_AREA_SEND: |
||
| 141 | case IPC_M_AS_AREA_RECV: |
||
| 142 | case IPC_M_DATA_SEND: |
||
| 1027 | palkovsky | 143 | return 1; |
| 2494 | jermar | 144 | default: |
| 145 | return 0; |
||
| 146 | } |
||
| 1027 | palkovsky | 147 | } |
| 148 | |||
| 2471 | jermar | 149 | /** Interpret process answer as control information. |
| 1428 | palkovsky | 150 | * |
| 2471 | jermar | 151 | * This function is called directly after sys_ipc_answer(). |
| 152 | * |
||
| 153 | * @param answer Call structure with the answer. |
||
| 154 | * @param olddata Saved data of the request. |
||
| 155 | * |
||
| 156 | * @return Return 0 on success or an error code. |
||
| 1428 | palkovsky | 157 | */ |
| 1329 | palkovsky | 158 | static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata) |
| 1027 | palkovsky | 159 | { |
| 160 | int phoneid; |
||
| 161 | |||
| 1088 | palkovsky | 162 | if (IPC_GET_RETVAL(answer->data) == EHANGUP) { |
| 1141 | palkovsky | 163 | /* In case of forward, hangup the forwared phone, |
| 164 | * not the originator |
||
| 165 | */ |
||
| 166 | spinlock_lock(&answer->data.phone->lock); |
||
| 167 | spinlock_lock(&TASK->answerbox.lock); |
||
| 1568 | palkovsky | 168 | if (answer->data.phone->state == IPC_PHONE_CONNECTED) { |
| 1573 | palkovsky | 169 | list_remove(&answer->data.phone->link); |
| 1568 | palkovsky | 170 | answer->data.phone->state = IPC_PHONE_SLAMMED; |
| 1141 | palkovsky | 171 | } |
| 172 | spinlock_unlock(&TASK->answerbox.lock); |
||
| 173 | spinlock_unlock(&answer->data.phone->lock); |
||
| 1088 | palkovsky | 174 | } |
| 175 | |||
| 176 | if (!olddata) |
||
| 1329 | palkovsky | 177 | return 0; |
| 1088 | palkovsky | 178 | |
| 1086 | palkovsky | 179 | if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) { |
| 2637 | cejka | 180 | phoneid = IPC_GET_ARG5(*olddata); |
| 1027 | palkovsky | 181 | if (IPC_GET_RETVAL(answer->data)) { |
| 182 | /* The connection was not accepted */ |
||
| 183 | phone_dealloc(phoneid); |
||
| 1040 | palkovsky | 184 | } else { |
| 1090 | palkovsky | 185 | /* The connection was accepted */ |
| 2359 | jermar | 186 | phone_connect(phoneid, &answer->sender->answerbox); |
| 2471 | jermar | 187 | /* Set 'phone hash' as arg3 of response */ |
| 2637 | cejka | 188 | IPC_SET_ARG5(answer->data, |
| 2359 | jermar | 189 | (unative_t) &TASK->phones[phoneid]); |
| 1027 | palkovsky | 190 | } |
| 1086 | palkovsky | 191 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) { |
| 1060 | palkovsky | 192 | /* If the users accepted call, connect */ |
| 193 | if (!IPC_GET_RETVAL(answer->data)) { |
||
| 2635 | cejka | 194 | ipc_phone_connect((phone_t *) IPC_GET_ARG5(*olddata), |
| 2359 | jermar | 195 | &TASK->answerbox); |
| 1060 | palkovsky | 196 | } |
| 1359 | jermar | 197 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_SEND) { |
| 2359 | jermar | 198 | if (!IPC_GET_RETVAL(answer->data)) { |
| 199 | /* Accepted, handle as_area receipt */ |
||
| 1413 | jermar | 200 | ipl_t ipl; |
| 1461 | palkovsky | 201 | int rc; |
| 1413 | jermar | 202 | as_t *as; |
| 203 | |||
| 204 | ipl = interrupts_disable(); |
||
| 205 | spinlock_lock(&answer->sender->lock); |
||
| 206 | as = answer->sender->as; |
||
| 207 | spinlock_unlock(&answer->sender->lock); |
||
| 208 | interrupts_restore(ipl); |
||
| 209 | |||
| 2359 | jermar | 210 | rc = as_area_share(as, IPC_GET_ARG1(*olddata), |
| 211 | IPC_GET_ARG2(*olddata), AS, |
||
| 212 | IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata)); |
||
| 1461 | palkovsky | 213 | IPC_SET_RETVAL(answer->data, rc); |
| 214 | return rc; |
||
| 1329 | palkovsky | 215 | } |
| 1428 | palkovsky | 216 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_RECV) { |
| 217 | if (!IPC_GET_RETVAL(answer->data)) { |
||
| 218 | ipl_t ipl; |
||
| 219 | as_t *as; |
||
| 1434 | palkovsky | 220 | int rc; |
| 1428 | palkovsky | 221 | |
| 222 | ipl = interrupts_disable(); |
||
| 223 | spinlock_lock(&answer->sender->lock); |
||
| 224 | as = answer->sender->as; |
||
| 225 | spinlock_unlock(&answer->sender->lock); |
||
| 226 | interrupts_restore(ipl); |
||
| 227 | |||
| 2359 | jermar | 228 | rc = as_area_share(AS, IPC_GET_ARG1(answer->data), |
| 229 | IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata), |
||
| 230 | IPC_GET_ARG2(answer->data)); |
||
| 1434 | palkovsky | 231 | IPC_SET_RETVAL(answer->data, rc); |
| 1428 | palkovsky | 232 | } |
| 2494 | jermar | 233 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_SEND) { |
| 234 | if (!IPC_GET_RETVAL(answer->data)) { |
||
| 235 | int rc; |
||
| 236 | uintptr_t dst; |
||
| 237 | uintptr_t size; |
||
| 238 | |||
| 239 | ASSERT(answer->buffer); |
||
| 240 | |||
| 241 | dst = IPC_GET_ARG1(answer->data); |
||
| 242 | size = IPC_GET_ARG3(answer->data); |
||
| 243 | |||
| 244 | rc = copy_to_uspace((void *) dst, answer->buffer, size); |
||
| 245 | if (rc != 0) |
||
| 246 | IPC_SET_RETVAL(answer->data, rc); |
||
| 247 | free(answer->buffer); |
||
| 248 | answer->buffer = NULL; |
||
| 249 | } |
||
| 1027 | palkovsky | 250 | } |
| 1329 | palkovsky | 251 | return 0; |
| 1027 | palkovsky | 252 | } |
| 253 | |||
| 2471 | jermar | 254 | /** Called before the request is sent. |
| 1090 | palkovsky | 255 | * |
| 2471 | jermar | 256 | * @param call Call structure with the request. |
| 257 | * |
||
| 258 | * @return Return 0 on success, ELIMIT or EPERM on error. |
||
| 1090 | palkovsky | 259 | */ |
| 260 | static int request_preprocess(call_t *call) |
||
| 261 | { |
||
| 262 | int newphid; |
||
| 1329 | palkovsky | 263 | size_t size; |
| 2494 | jermar | 264 | uintptr_t src; |
| 265 | int rc; |
||
| 1090 | palkovsky | 266 | |
| 267 | switch (IPC_GET_METHOD(call->data)) { |
||
| 268 | case IPC_M_CONNECT_ME_TO: |
||
| 269 | newphid = phone_alloc(); |
||
| 270 | if (newphid < 0) |
||
| 271 | return ELIMIT; |
||
| 272 | /* Set arg3 for server */ |
||
| 2635 | cejka | 273 | IPC_SET_ARG5(call->data, (unative_t) &TASK->phones[newphid]); |
| 1090 | palkovsky | 274 | call->flags |= IPC_CALL_CONN_ME_TO; |
| 2098 | decky | 275 | call->priv = newphid; |
| 1090 | palkovsky | 276 | break; |
| 1359 | jermar | 277 | case IPC_M_AS_AREA_SEND: |
| 2556 | jermar | 278 | size = as_area_get_size(IPC_GET_ARG1(call->data)); |
| 2471 | jermar | 279 | if (!size) |
| 1329 | palkovsky | 280 | return EPERM; |
| 1417 | jermar | 281 | IPC_SET_ARG2(call->data, size); |
| 1329 | palkovsky | 282 | break; |
| 2494 | jermar | 283 | case IPC_M_DATA_SEND: |
| 284 | src = IPC_GET_ARG2(call->data); |
||
| 285 | size = IPC_GET_ARG3(call->data); |
||
| 286 | |||
| 287 | if ((size <= 0) || (size > DATA_SEND_LIMIT)) |
||
| 288 | return ELIMIT; |
||
| 289 | |||
| 290 | call->buffer = (uint8_t *) malloc(size, 0); |
||
| 291 | rc = copy_from_uspace(call->buffer, (void *) src, size); |
||
| 292 | if (rc != 0) { |
||
| 293 | free(call->buffer); |
||
| 294 | return rc; |
||
| 295 | } |
||
| 296 | break; |
||
| 1090 | palkovsky | 297 | default: |
| 298 | break; |
||
| 299 | } |
||
| 300 | return 0; |
||
| 301 | } |
||
| 302 | |||
| 2471 | jermar | 303 | /******************************************************************************* |
| 304 | * Functions called to process received call/answer before passing it to uspace. |
||
| 305 | *******************************************************************************/ |
||
| 306 | |||
| 307 | /** Do basic kernel processing of received call answer. |
||
| 308 | * |
||
| 309 | * @param call Call structure with the answer. |
||
| 1027 | palkovsky | 310 | */ |
| 1090 | palkovsky | 311 | static void process_answer(call_t *call) |
| 1027 | palkovsky | 312 | { |
| 2359 | jermar | 313 | if (IPC_GET_RETVAL(call->data) == EHANGUP && |
| 314 | (call->flags & IPC_CALL_FORWARDED)) |
||
| 1088 | palkovsky | 315 | IPC_SET_RETVAL(call->data, EFORWARD); |
| 1090 | palkovsky | 316 | |
| 317 | if (call->flags & IPC_CALL_CONN_ME_TO) { |
||
| 318 | if (IPC_GET_RETVAL(call->data)) |
||
| 2098 | decky | 319 | phone_dealloc(call->priv); |
| 1090 | palkovsky | 320 | else |
| 2635 | cejka | 321 | IPC_SET_ARG5(call->data, call->priv); |
| 1090 | palkovsky | 322 | } |
| 1027 | palkovsky | 323 | } |
| 324 | |||
| 2471 | jermar | 325 | /** Do basic kernel processing of received call request. |
| 1027 | palkovsky | 326 | * |
| 2471 | jermar | 327 | * @param box Destination answerbox structure. |
| 328 | * @param call Call structure with the request. |
||
| 329 | * |
||
| 330 | * @return Return 0 if the call should be passed to userspace. |
||
| 331 | * Return -1 if the call should be ignored. |
||
| 1027 | palkovsky | 332 | */ |
| 2359 | jermar | 333 | static int process_request(answerbox_t *box, call_t *call) |
| 1027 | palkovsky | 334 | { |
| 335 | int phoneid; |
||
| 336 | |||
| 1086 | palkovsky | 337 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) { |
| 1040 | palkovsky | 338 | phoneid = phone_alloc(); |
| 1027 | palkovsky | 339 | if (phoneid < 0) { /* Failed to allocate phone */ |
| 340 | IPC_SET_RETVAL(call->data, ELIMIT); |
||
| 2471 | jermar | 341 | ipc_answer(box, call); |
| 1027 | palkovsky | 342 | return -1; |
| 343 | } |
||
| 2637 | cejka | 344 | IPC_SET_ARG5(call->data, phoneid); |
| 1027 | palkovsky | 345 | } |
| 346 | return 0; |
||
| 347 | } |
||
| 348 | |||
| 2471 | jermar | 349 | /** Make a fast call over IPC, wait for reply and return to user. |
| 1027 | palkovsky | 350 | * |
| 2615 | jermar | 351 | * This function can handle only three arguments of payload, but is faster than |
| 352 | * the generic function (i.e. sys_ipc_call_sync_slow()). |
||
| 2471 | jermar | 353 | * |
| 354 | * @param phoneid Phone handle for the call. |
||
| 355 | * @param method Method of the call. |
||
| 356 | * @param arg1 Service-defined payload argument. |
||
| 2615 | jermar | 357 | * @param arg2 Service-defined payload argument. |
| 358 | * @param arg3 Service-defined payload argument. |
||
| 2471 | jermar | 359 | * @param data Address of userspace structure where the reply call will |
| 360 | * be stored. |
||
| 361 | * |
||
| 362 | * @return Returns 0 on success. |
||
| 363 | * Return ENOENT if there is no such phone handle. |
||
| 1027 | palkovsky | 364 | */ |
| 2359 | jermar | 365 | unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method, |
| 2615 | jermar | 366 | unative_t arg1, unative_t arg2, unative_t arg3, ipc_data_t *data) |
| 1027 | palkovsky | 367 | { |
| 368 | call_t call; |
||
| 369 | phone_t *phone; |
||
| 1090 | palkovsky | 370 | int res; |
| 2617 | jermar | 371 | int rc; |
| 1027 | palkovsky | 372 | |
| 1084 | palkovsky | 373 | GET_CHECK_PHONE(phone, phoneid, return ENOENT); |
| 1027 | palkovsky | 374 | |
| 1084 | palkovsky | 375 | ipc_call_static_init(&call); |
| 1027 | palkovsky | 376 | IPC_SET_METHOD(call.data, method); |
| 377 | IPC_SET_ARG1(call.data, arg1); |
||
| 2615 | jermar | 378 | IPC_SET_ARG2(call.data, arg2); |
| 379 | IPC_SET_ARG3(call.data, arg3); |
||
| 2620 | jermar | 380 | /* |
| 381 | * To achieve deterministic behavior, zero out arguments that are beyond |
||
| 382 | * the limits of the fast version. |
||
| 383 | */ |
||
| 384 | IPC_SET_ARG4(call.data, 0); |
||
| 385 | IPC_SET_ARG5(call.data, 0); |
||
| 1027 | palkovsky | 386 | |
| 2471 | jermar | 387 | if (!(res = request_preprocess(&call))) { |
| 1090 | palkovsky | 388 | ipc_call_sync(phone, &call); |
| 389 | process_answer(&call); |
||
| 2471 | jermar | 390 | } else { |
| 1090 | palkovsky | 391 | IPC_SET_RETVAL(call.data, res); |
| 2471 | jermar | 392 | } |
| 2617 | jermar | 393 | rc = STRUCT_TO_USPACE(&data->args, &call.data.args); |
| 394 | if (rc != 0) |
||
| 395 | return rc; |
||
| 1027 | palkovsky | 396 | |
| 397 | return 0; |
||
| 398 | } |
||
| 399 | |||
| 2471 | jermar | 400 | /** Make a synchronous IPC call allowing to transmit the entire payload. |
| 401 | * |
||
| 402 | * @param phoneid Phone handle for the call. |
||
| 403 | * @param question Userspace address of call data with the request. |
||
| 2494 | jermar | 404 | * @param reply Userspace address of call data where to store the |
| 405 | * answer. |
||
| 2471 | jermar | 406 | * |
| 407 | * @return Zero on success or an error code. |
||
| 408 | */ |
||
| 2615 | jermar | 409 | unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question, |
| 2359 | jermar | 410 | ipc_data_t *reply) |
| 1027 | palkovsky | 411 | { |
| 412 | call_t call; |
||
| 413 | phone_t *phone; |
||
| 1090 | palkovsky | 414 | int res; |
| 1288 | jermar | 415 | int rc; |
| 1027 | palkovsky | 416 | |
| 1084 | palkovsky | 417 | ipc_call_static_init(&call); |
| 2359 | jermar | 418 | rc = copy_from_uspace(&call.data.args, &question->args, |
| 419 | sizeof(call.data.args)); |
||
| 1288 | jermar | 420 | if (rc != 0) |
| 1780 | jermar | 421 | return (unative_t) rc; |
| 1040 | palkovsky | 422 | |
| 1084 | palkovsky | 423 | GET_CHECK_PHONE(phone, phoneid, return ENOENT); |
| 1072 | palkovsky | 424 | |
| 2471 | jermar | 425 | if (!(res = request_preprocess(&call))) { |
| 1090 | palkovsky | 426 | ipc_call_sync(phone, &call); |
| 427 | process_answer(&call); |
||
| 428 | } else |
||
| 429 | IPC_SET_RETVAL(call.data, res); |
||
| 1027 | palkovsky | 430 | |
| 1288 | jermar | 431 | rc = STRUCT_TO_USPACE(&reply->args, &call.data.args); |
| 432 | if (rc != 0) |
||
| 433 | return rc; |
||
| 1027 | palkovsky | 434 | |
| 435 | return 0; |
||
| 436 | } |
||
| 437 | |||
| 2471 | jermar | 438 | /** Check that the task did not exceed the allowed limit of asynchronous calls. |
| 1027 | palkovsky | 439 | * |
| 2471 | jermar | 440 | * @return Return 0 if limit not reached or -1 if limit exceeded. |
| 1027 | palkovsky | 441 | */ |
| 442 | static int check_call_limit(void) |
||
| 443 | { |
||
| 444 | if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) { |
||
| 445 | atomic_dec(&TASK->active_calls); |
||
| 446 | return -1; |
||
| 447 | } |
||
| 448 | return 0; |
||
| 449 | } |
||
| 450 | |||
| 2471 | jermar | 451 | /** Make a fast asynchronous call over IPC. |
| 1027 | palkovsky | 452 | * |
| 2618 | jermar | 453 | * This function can only handle four arguments of payload, but is faster than |
| 454 | * the generic function sys_ipc_call_async_slow(). |
||
| 2471 | jermar | 455 | * |
| 456 | * @param phoneid Phone handle for the call. |
||
| 457 | * @param method Method of the call. |
||
| 458 | * @param arg1 Service-defined payload argument. |
||
| 459 | * @param arg2 Service-defined payload argument. |
||
| 2618 | jermar | 460 | * @param arg3 Service-defined payload argument. |
| 461 | * @param arg4 Service-defined payload argument. |
||
| 2471 | jermar | 462 | * |
| 463 | * @return Return call hash on success. |
||
| 464 | * Return IPC_CALLRET_FATAL in case of a fatal error and |
||
| 465 | * IPC_CALLRET_TEMPORARY if there are too many pending |
||
| 466 | * asynchronous requests; answers should be handled first. |
||
| 1027 | palkovsky | 467 | */ |
| 2359 | jermar | 468 | unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method, |
| 2618 | jermar | 469 | unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4) |
| 1027 | palkovsky | 470 | { |
| 471 | call_t *call; |
||
| 472 | phone_t *phone; |
||
| 1090 | palkovsky | 473 | int res; |
| 1027 | palkovsky | 474 | |
| 475 | if (check_call_limit()) |
||
| 476 | return IPC_CALLRET_TEMPORARY; |
||
| 477 | |||
| 1090 | palkovsky | 478 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL); |
| 1072 | palkovsky | 479 | |
| 1258 | palkovsky | 480 | call = ipc_call_alloc(0); |
| 1027 | palkovsky | 481 | IPC_SET_METHOD(call->data, method); |
| 482 | IPC_SET_ARG1(call->data, arg1); |
||
| 483 | IPC_SET_ARG2(call->data, arg2); |
||
| 2618 | jermar | 484 | IPC_SET_ARG3(call->data, arg3); |
| 485 | IPC_SET_ARG4(call->data, arg4); |
||
| 2620 | jermar | 486 | /* |
| 487 | * To achieve deterministic behavior, zero out arguments that are beyond |
||
| 488 | * the limits of the fast version. |
||
| 489 | */ |
||
| 490 | IPC_SET_ARG5(call->data, 0); |
||
| 1027 | palkovsky | 491 | |
| 2359 | jermar | 492 | if (!(res = request_preprocess(call))) |
| 1090 | palkovsky | 493 | ipc_call(phone, call); |
| 494 | else |
||
| 495 | ipc_backsend_err(phone, call, res); |
||
| 1027 | palkovsky | 496 | |
| 1780 | jermar | 497 | return (unative_t) call; |
| 1027 | palkovsky | 498 | } |
| 499 | |||
| 2471 | jermar | 500 | /** Make an asynchronous IPC call allowing to transmit the entire payload. |
| 1027 | palkovsky | 501 | * |
| 2471 | jermar | 502 | * @param phoneid Phone handle for the call. |
| 503 | * @param data Userspace address of call data with the request. |
||
| 504 | * |
||
| 505 | * @return See sys_ipc_call_async_fast(). |
||
| 1027 | palkovsky | 506 | */ |
| 2618 | jermar | 507 | unative_t sys_ipc_call_async_slow(unative_t phoneid, ipc_data_t *data) |
| 1027 | palkovsky | 508 | { |
| 509 | call_t *call; |
||
| 510 | phone_t *phone; |
||
| 1090 | palkovsky | 511 | int res; |
| 1288 | jermar | 512 | int rc; |
| 1027 | palkovsky | 513 | |
| 1072 | palkovsky | 514 | if (check_call_limit()) |
| 515 | return IPC_CALLRET_TEMPORARY; |
||
| 516 | |||
| 1090 | palkovsky | 517 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL); |
| 1027 | palkovsky | 518 | |
| 1258 | palkovsky | 519 | call = ipc_call_alloc(0); |
| 2359 | jermar | 520 | rc = copy_from_uspace(&call->data.args, &data->args, |
| 521 | sizeof(call->data.args)); |
||
| 1293 | palkovsky | 522 | if (rc != 0) { |
| 523 | ipc_call_free(call); |
||
| 1780 | jermar | 524 | return (unative_t) rc; |
| 1293 | palkovsky | 525 | } |
| 2359 | jermar | 526 | if (!(res = request_preprocess(call))) |
| 1090 | palkovsky | 527 | ipc_call(phone, call); |
| 528 | else |
||
| 529 | ipc_backsend_err(phone, call, res); |
||
| 1040 | palkovsky | 530 | |
| 1780 | jermar | 531 | return (unative_t) call; |
| 1027 | palkovsky | 532 | } |
| 533 | |||
| 2471 | jermar | 534 | /** Forward a received call to another destination. |
| 1040 | palkovsky | 535 | * |
| 2471 | jermar | 536 | * @param callid Hash of the call to forward. |
| 537 | * @param phoneid Phone handle to use for forwarding. |
||
| 538 | * @param method New method to use for the forwarded call. |
||
| 539 | * @param arg1 New value of the first argument for the forwarded call. |
||
| 2636 | jermar | 540 | * @param arg2 New value of the second argument for the forwarded call. |
| 2622 | jermar | 541 | * @param mode Flags that specify mode of the forward operation. |
| 1060 | palkovsky | 542 | * |
| 2471 | jermar | 543 | * @return Return 0 on succes, otherwise return an error code. |
| 544 | * |
||
| 2636 | jermar | 545 | * In case the original method is a system method, ARG1, ARG2 and ARG3 are |
| 546 | * overwritten in the forwarded message with the new method and the new arg1 and |
||
| 547 | * arg2, respectively. Otherwise the METHOD, ARG1 and ARG2 are rewritten with |
||
| 548 | * the new method, arg1 and arg2, respectively. Also note there is a set of |
||
| 549 | * immutable methods, for which the new method and argument is not set and |
||
| 550 | * these values are ignored. |
||
| 2471 | jermar | 551 | * |
| 2622 | jermar | 552 | * Warning: When implementing support for changing additional payload |
| 2636 | jermar | 553 | * arguments, make sure that ARG5 is not rewritten for certain |
| 2622 | jermar | 554 | * system IPC |
| 1040 | palkovsky | 555 | */ |
| 1780 | jermar | 556 | unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid, |
| 2635 | cejka | 557 | unative_t method, unative_t arg1, unative_t arg2, int mode) |
| 1040 | palkovsky | 558 | { |
| 559 | call_t *call; |
||
| 560 | phone_t *phone; |
||
| 561 | |||
| 562 | call = get_call(callid); |
||
| 563 | if (!call) |
||
| 564 | return ENOENT; |
||
| 565 | |||
| 1088 | palkovsky | 566 | call->flags |= IPC_CALL_FORWARDED; |
| 567 | |||
| 1084 | palkovsky | 568 | GET_CHECK_PHONE(phone, phoneid, { |
| 1040 | palkovsky | 569 | IPC_SET_RETVAL(call->data, EFORWARD); |
| 570 | ipc_answer(&TASK->answerbox, call); |
||
| 571 | return ENOENT; |
||
| 1084 | palkovsky | 572 | }); |
| 1040 | palkovsky | 573 | |
| 2557 | jermar | 574 | if (!method_is_forwardable(IPC_GET_METHOD(call->data))) { |
| 1040 | palkovsky | 575 | IPC_SET_RETVAL(call->data, EFORWARD); |
| 576 | ipc_answer(&TASK->answerbox, call); |
||
| 577 | return EPERM; |
||
| 578 | } |
||
| 579 | |||
| 2557 | jermar | 580 | /* |
| 581 | * Userspace is not allowed to change method of system methods on |
||
| 2635 | cejka | 582 | * forward, allow changing ARG1, ARG2 and ARG3 by means of method, |
| 583 | * arg1 and arg2. |
||
| 2557 | jermar | 584 | * If the method is immutable, don't change anything. |
| 1040 | palkovsky | 585 | */ |
| 2557 | jermar | 586 | if (!method_is_immutable(IPC_GET_METHOD(call->data))) { |
| 587 | if (method_is_system(IPC_GET_METHOD(call->data))) { |
||
| 588 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) |
||
| 2635 | cejka | 589 | phone_dealloc(IPC_GET_ARG5(call->data)); |
| 1090 | palkovsky | 590 | |
| 2557 | jermar | 591 | IPC_SET_ARG1(call->data, method); |
| 592 | IPC_SET_ARG2(call->data, arg1); |
||
| 2635 | cejka | 593 | IPC_SET_ARG3(call->data, arg2); |
| 2557 | jermar | 594 | } else { |
| 595 | IPC_SET_METHOD(call->data, method); |
||
| 596 | IPC_SET_ARG1(call->data, arg1); |
||
| 2635 | cejka | 597 | IPC_SET_ARG2(call->data, arg2); |
| 2557 | jermar | 598 | } |
| 1040 | palkovsky | 599 | } |
| 600 | |||
| 2622 | jermar | 601 | return ipc_forward(call, phone, &TASK->answerbox, mode); |
| 1040 | palkovsky | 602 | } |
| 603 | |||
| 2471 | jermar | 604 | /** Answer an IPC call - fast version. |
| 605 | * |
||
| 606 | * This function can handle only two return arguments of payload, but is faster |
||
| 607 | * than the generic sys_ipc_answer(). |
||
| 608 | * |
||
| 609 | * @param callid Hash of the call to be answered. |
||
| 610 | * @param retval Return value of the answer. |
||
| 611 | * @param arg1 Service-defined return value. |
||
| 612 | * @param arg2 Service-defined return value. |
||
| 2619 | jermar | 613 | * @param arg3 Service-defined return value. |
| 614 | * @param arg4 Service-defined return value. |
||
| 2471 | jermar | 615 | * |
| 616 | * @return Return 0 on success, otherwise return an error code. |
||
| 617 | */ |
||
| 2359 | jermar | 618 | unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval, |
| 2619 | jermar | 619 | unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4) |
| 1027 | palkovsky | 620 | { |
| 621 | call_t *call; |
||
| 1040 | palkovsky | 622 | ipc_data_t saved_data; |
| 1088 | palkovsky | 623 | int saveddata = 0; |
| 1329 | palkovsky | 624 | int rc; |
| 1027 | palkovsky | 625 | |
| 1346 | palkovsky | 626 | /* Do not answer notification callids */ |
| 627 | if (callid & IPC_CALLID_NOTIFICATION) |
||
| 628 | return 0; |
||
| 629 | |||
| 1040 | palkovsky | 630 | call = get_call(callid); |
| 631 | if (!call) |
||
| 632 | return ENOENT; |
||
| 1027 | palkovsky | 633 | |
| 1088 | palkovsky | 634 | if (answer_need_old(call)) { |
| 1040 | palkovsky | 635 | memcpy(&saved_data, &call->data, sizeof(call->data)); |
| 1088 | palkovsky | 636 | saveddata = 1; |
| 1027 | palkovsky | 637 | } |
| 638 | |||
| 639 | IPC_SET_RETVAL(call->data, retval); |
||
| 640 | IPC_SET_ARG1(call->data, arg1); |
||
| 641 | IPC_SET_ARG2(call->data, arg2); |
||
| 2619 | jermar | 642 | IPC_SET_ARG3(call->data, arg3); |
| 643 | IPC_SET_ARG4(call->data, arg4); |
||
| 2620 | jermar | 644 | /* |
| 645 | * To achieve deterministic behavior, zero out arguments that are beyond |
||
| 646 | * the limits of the fast version. |
||
| 647 | */ |
||
| 648 | IPC_SET_ARG5(call->data, 0); |
||
| 1329 | palkovsky | 649 | rc = answer_preprocess(call, saveddata ? &saved_data : NULL); |
| 1040 | palkovsky | 650 | |
| 1027 | palkovsky | 651 | ipc_answer(&TASK->answerbox, call); |
| 1329 | palkovsky | 652 | return rc; |
| 1027 | palkovsky | 653 | } |
| 654 | |||
| 2471 | jermar | 655 | /** Answer an IPC call. |
| 656 | * |
||
| 657 | * @param callid Hash of the call to be answered. |
||
| 658 | * @param data Userspace address of call data with the answer. |
||
| 659 | * |
||
| 660 | * @return Return 0 on success, otherwise return an error code. |
||
| 661 | */ |
||
| 2619 | jermar | 662 | unative_t sys_ipc_answer_slow(unative_t callid, ipc_data_t *data) |
| 1027 | palkovsky | 663 | { |
| 664 | call_t *call; |
||
| 1040 | palkovsky | 665 | ipc_data_t saved_data; |
| 1088 | palkovsky | 666 | int saveddata = 0; |
| 1288 | jermar | 667 | int rc; |
| 1027 | palkovsky | 668 | |
| 1346 | palkovsky | 669 | /* Do not answer notification callids */ |
| 670 | if (callid & IPC_CALLID_NOTIFICATION) |
||
| 671 | return 0; |
||
| 672 | |||
| 1040 | palkovsky | 673 | call = get_call(callid); |
| 674 | if (!call) |
||
| 675 | return ENOENT; |
||
| 1027 | palkovsky | 676 | |
| 1088 | palkovsky | 677 | if (answer_need_old(call)) { |
| 1040 | palkovsky | 678 | memcpy(&saved_data, &call->data, sizeof(call->data)); |
| 1088 | palkovsky | 679 | saveddata = 1; |
| 1027 | palkovsky | 680 | } |
| 1288 | jermar | 681 | rc = copy_from_uspace(&call->data.args, &data->args, |
| 2359 | jermar | 682 | sizeof(call->data.args)); |
| 1288 | jermar | 683 | if (rc != 0) |
| 684 | return rc; |
||
| 1027 | palkovsky | 685 | |
| 1329 | palkovsky | 686 | rc = answer_preprocess(call, saveddata ? &saved_data : NULL); |
| 1027 | palkovsky | 687 | |
| 688 | ipc_answer(&TASK->answerbox, call); |
||
| 689 | |||
| 1329 | palkovsky | 690 | return rc; |
| 1027 | palkovsky | 691 | } |
| 692 | |||
| 2471 | jermar | 693 | /** Hang up a phone. |
| 1086 | palkovsky | 694 | * |
| 2471 | jermar | 695 | * @param Phone handle of the phone to be hung up. |
| 696 | * |
||
| 697 | * @return Return 0 on success or an error code. |
||
| 1086 | palkovsky | 698 | */ |
| 1780 | jermar | 699 | unative_t sys_ipc_hangup(int phoneid) |
| 1086 | palkovsky | 700 | { |
| 701 | phone_t *phone; |
||
| 702 | |||
| 703 | GET_CHECK_PHONE(phone, phoneid, return ENOENT); |
||
| 704 | |||
| 1591 | palkovsky | 705 | if (ipc_phone_hangup(phone)) |
| 1086 | palkovsky | 706 | return -1; |
| 707 | |||
| 708 | return 0; |
||
| 709 | } |
||
| 710 | |||
| 2471 | jermar | 711 | /** Wait for an incoming IPC call or an answer. |
| 1027 | palkovsky | 712 | * |
| 2359 | jermar | 713 | * @param calldata Pointer to buffer where the call/answer data is stored. |
| 714 | * @param usec Timeout. See waitq_sleep_timeout() for explanation. |
||
| 715 | * @param flags Select mode of sleep operation. See waitq_sleep_timeout() |
||
| 716 | * for explanation. |
||
| 1364 | jermar | 717 | * |
| 2471 | jermar | 718 | * @return Hash of the call. |
| 719 | * If IPC_CALLID_NOTIFICATION bit is set in the hash, the |
||
| 720 | * call is a notification. IPC_CALLID_ANSWERED denotes an |
||
| 721 | * answer. |
||
| 1027 | palkovsky | 722 | */ |
| 1780 | jermar | 723 | unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags) |
| 1027 | palkovsky | 724 | { |
| 725 | call_t *call; |
||
| 726 | |||
| 727 | restart: |
||
| 2359 | jermar | 728 | call = ipc_wait_for_call(&TASK->answerbox, usec, |
| 729 | flags | SYNCH_FLAGS_INTERRUPTIBLE); |
||
| 1088 | palkovsky | 730 | if (!call) |
| 731 | return 0; |
||
| 1027 | palkovsky | 732 | |
| 1258 | palkovsky | 733 | if (call->flags & IPC_CALL_NOTIF) { |
| 734 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC)); |
||
| 1693 | palkovsky | 735 | |
| 736 | /* Set in_phone_hash to the interrupt counter */ |
||
| 2098 | decky | 737 | call->data.phone = (void *) call->priv; |
| 1693 | palkovsky | 738 | |
| 739 | STRUCT_TO_USPACE(calldata, &call->data); |
||
| 740 | |||
| 1258 | palkovsky | 741 | ipc_call_free(call); |
| 742 | |||
| 2471 | jermar | 743 | return ((unative_t) call) | IPC_CALLID_NOTIFICATION; |
| 1258 | palkovsky | 744 | } |
| 745 | |||
| 1027 | palkovsky | 746 | if (call->flags & IPC_CALL_ANSWERED) { |
| 1090 | palkovsky | 747 | process_answer(call); |
| 1027 | palkovsky | 748 | |
| 1086 | palkovsky | 749 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC)); |
| 750 | |||
| 1027 | palkovsky | 751 | atomic_dec(&TASK->active_calls); |
| 1086 | palkovsky | 752 | |
| 753 | if (call->flags & IPC_CALL_DISCARD_ANSWER) { |
||
| 754 | ipc_call_free(call); |
||
| 755 | goto restart; |
||
| 756 | } |
||
| 757 | |||
| 758 | STRUCT_TO_USPACE(&calldata->args, &call->data.args); |
||
| 1027 | palkovsky | 759 | ipc_call_free(call); |
| 760 | |||
| 2471 | jermar | 761 | return ((unative_t) call) | IPC_CALLID_ANSWERED; |
| 1027 | palkovsky | 762 | } |
| 1086 | palkovsky | 763 | |
| 1027 | palkovsky | 764 | if (process_request(&TASK->answerbox, call)) |
| 765 | goto restart; |
||
| 1086 | palkovsky | 766 | |
| 1090 | palkovsky | 767 | /* Include phone address('id') of the caller in the request, |
| 768 | * copy whole call->data, not only call->data.args */ |
||
| 1396 | palkovsky | 769 | if (STRUCT_TO_USPACE(calldata, &call->data)) { |
| 770 | return 0; |
||
| 771 | } |
||
| 1780 | jermar | 772 | return (unative_t)call; |
| 1027 | palkovsky | 773 | } |
| 1258 | palkovsky | 774 | |
| 2471 | jermar | 775 | /** Connect an IRQ handler to a task. |
| 1923 | jermar | 776 | * |
| 2471 | jermar | 777 | * @param inr IRQ number. |
| 778 | * @param devno Device number. |
||
| 779 | * @param method Method to be associated with the notification. |
||
| 780 | * @param ucode Uspace pointer to the top-half pseudocode. |
||
| 1923 | jermar | 781 | * |
| 2471 | jermar | 782 | * @return EPERM or a return code returned by ipc_irq_register(). |
| 1923 | jermar | 783 | */ |
| 2359 | jermar | 784 | unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method, |
| 785 | irq_code_t *ucode) |
||
| 1258 | palkovsky | 786 | { |
| 1297 | jermar | 787 | if (!(cap_get(TASK) & CAP_IRQ_REG)) |
| 788 | return EPERM; |
||
| 789 | |||
| 1923 | jermar | 790 | return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode); |
| 1258 | palkovsky | 791 | } |
| 792 | |||
| 2471 | jermar | 793 | /** Disconnect an IRQ handler from a task. |
| 1923 | jermar | 794 | * |
| 2471 | jermar | 795 | * @param inr IRQ number. |
| 796 | * @param devno Device number. |
||
| 797 | * |
||
| 798 | * @return Zero on success or EPERM on error.. |
||
| 1923 | jermar | 799 | */ |
| 800 | unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno) |
||
| 1258 | palkovsky | 801 | { |
| 1297 | jermar | 802 | if (!(cap_get(TASK) & CAP_IRQ_REG)) |
| 803 | return EPERM; |
||
| 804 | |||
| 1923 | jermar | 805 | ipc_irq_unregister(&TASK->answerbox, inr, devno); |
| 1258 | palkovsky | 806 | |
| 807 | return 0; |
||
| 808 | } |
||
| 1702 | cejka | 809 | |
| 1757 | jermar | 810 | /** @} |
| 1702 | cejka | 811 | */ |