[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>
|
---|
[5626277] | 45 | #include <arch/interrupt.h>
|
---|
[2d5a54f3] | 46 | #include <print.h>
|
---|
[e3c762cd] | 47 | #include <syscall/copy.h>
|
---|
[2bb8648] | 48 | #include <security/cap.h>
|
---|
[7c23af9] | 49 | #include <mm/as.h>
|
---|
[253f35a1] | 50 | #include <print.h>
|
---|
[2d5a54f3] | 51 |
|
---|
[7918fce] | 52 | /** Maximum buffer size allowed for IPC_M_DATA_SEND requests. */
|
---|
| 53 | #define DATA_SEND_LIMIT (64 * 1024)
|
---|
| 54 |
|
---|
[8b243f2] | 55 | #define GET_CHECK_PHONE(phone, phoneid, err) \
|
---|
| 56 | { \
|
---|
| 57 | if (phoneid > IPC_MAX_PHONES) { \
|
---|
| 58 | err; \
|
---|
| 59 | } \
|
---|
| 60 | phone = &TASK->phones[phoneid]; \
|
---|
[ba81cab] | 61 | }
|
---|
| 62 |
|
---|
[8b243f2] | 63 | #define STRUCT_TO_USPACE(dst, src) copy_to_uspace(dst, src, sizeof(*(src)))
|
---|
[ba81cab] | 64 |
|
---|
[8b243f2] | 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 | */
|
---|
[0dc4258] | 72 | static inline int method_is_system(unative_t method)
|
---|
[2ba7810] | 73 | {
|
---|
| 74 | if (method <= IPC_M_LAST_SYSTEM)
|
---|
| 75 | return 1;
|
---|
| 76 | return 0;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[8b243f2] | 79 | /** Decide if the message with this method is forwardable.
|
---|
[2ba7810] | 80 | *
|
---|
| 81 | * - some system messages may be forwarded, for some of them
|
---|
| 82 | * it is useless
|
---|
[8b243f2] | 83 | *
|
---|
| 84 | * @param method Method to be decided.
|
---|
| 85 | *
|
---|
| 86 | * @return Return 1 if the method is forwardable.
|
---|
| 87 | * Otherwise return 0.
|
---|
[2ba7810] | 88 | */
|
---|
[0dc4258] | 89 | static inline int method_is_forwardable(unative_t method)
|
---|
[2ba7810] | 90 | {
|
---|
[7918fce] | 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 | }
|
---|
[2ba7810] | 98 | }
|
---|
| 99 |
|
---|
[0dc4258] | 100 | /** Decide if the message with this method is immutable on forward.
|
---|
| 101 | *
|
---|
[7c5bcc0] | 102 | * - some system messages may be forwarded but their content cannot be altered
|
---|
[0dc4258] | 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 | }
|
---|
| 121 |
|
---|
[2d5a54f3] | 122 |
|
---|
[8b243f2] | 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.
|
---|
[2d5a54f3] | 134 | */
|
---|
[9f22213] | 135 | static inline int answer_need_old(call_t *call)
|
---|
[2d5a54f3] | 136 | {
|
---|
[7918fce] | 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:
|
---|
[46fc2f9] | 143 | return 1;
|
---|
[7918fce] | 144 | default:
|
---|
| 145 | return 0;
|
---|
| 146 | }
|
---|
[2d5a54f3] | 147 | }
|
---|
| 148 |
|
---|
[8b243f2] | 149 | /** Interpret process answer as control information.
|
---|
| 150 | *
|
---|
| 151 | * This function is called directly after sys_ipc_answer().
|
---|
[46fc2f9] | 152 | *
|
---|
[8b243f2] | 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.
|
---|
[46fc2f9] | 157 | */
|
---|
[7c23af9] | 158 | static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
|
---|
[2d5a54f3] | 159 | {
|
---|
| 160 | int phoneid;
|
---|
| 161 |
|
---|
[9f22213] | 162 | if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
|
---|
[ca687ad] | 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);
|
---|
[eb3d379] | 168 | if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
|
---|
[c4e4507] | 169 | list_remove(&answer->data.phone->link);
|
---|
[eb3d379] | 170 | answer->data.phone->state = IPC_PHONE_SLAMMED;
|
---|
[ca687ad] | 171 | }
|
---|
| 172 | spinlock_unlock(&TASK->answerbox.lock);
|
---|
| 173 | spinlock_unlock(&answer->data.phone->lock);
|
---|
[9f22213] | 174 | }
|
---|
| 175 |
|
---|
| 176 | if (!olddata)
|
---|
[7c23af9] | 177 | return 0;
|
---|
[9f22213] | 178 |
|
---|
[fbcfd458] | 179 | if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
|
---|
[2ba7810] | 180 | phoneid = IPC_GET_ARG3(*olddata);
|
---|
[2d5a54f3] | 181 | if (IPC_GET_RETVAL(answer->data)) {
|
---|
| 182 | /* The connection was not accepted */
|
---|
| 183 | phone_dealloc(phoneid);
|
---|
[2ba7810] | 184 | } else {
|
---|
[7c7aae16] | 185 | /* The connection was accepted */
|
---|
[51ec40f] | 186 | phone_connect(phoneid, &answer->sender->answerbox);
|
---|
[8b243f2] | 187 | /* Set 'phone hash' as arg3 of response */
|
---|
[51ec40f] | 188 | IPC_SET_ARG3(answer->data,
|
---|
| 189 | (unative_t) &TASK->phones[phoneid]);
|
---|
[2d5a54f3] | 190 | }
|
---|
[fbcfd458] | 191 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
|
---|
[37c57f2] | 192 | /* If the users accepted call, connect */
|
---|
| 193 | if (!IPC_GET_RETVAL(answer->data)) {
|
---|
[51ec40f] | 194 | ipc_phone_connect((phone_t *) IPC_GET_ARG3(*olddata),
|
---|
| 195 | &TASK->answerbox);
|
---|
[37c57f2] | 196 | }
|
---|
[8497711] | 197 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_SEND) {
|
---|
[51ec40f] | 198 | if (!IPC_GET_RETVAL(answer->data)) {
|
---|
| 199 | /* Accepted, handle as_area receipt */
|
---|
[8d6bc2d5] | 200 | ipl_t ipl;
|
---|
[76d7305] | 201 | int rc;
|
---|
[8d6bc2d5] | 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 |
|
---|
[51ec40f] | 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));
|
---|
[76d7305] | 213 | IPC_SET_RETVAL(answer->data, rc);
|
---|
| 214 | return rc;
|
---|
[46fc2f9] | 215 | }
|
---|
| 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;
|
---|
[d6e5cbc] | 220 | int rc;
|
---|
[46fc2f9] | 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 |
|
---|
[51ec40f] | 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));
|
---|
[d6e5cbc] | 231 | IPC_SET_RETVAL(answer->data, rc);
|
---|
[7c23af9] | 232 | }
|
---|
[7918fce] | 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 | }
|
---|
[2d5a54f3] | 250 | }
|
---|
[7c23af9] | 251 | return 0;
|
---|
[2d5a54f3] | 252 | }
|
---|
| 253 |
|
---|
[8b243f2] | 254 | /** Called before the request is sent.
|
---|
| 255 | *
|
---|
| 256 | * @param call Call structure with the request.
|
---|
[7c7aae16] | 257 | *
|
---|
[8b243f2] | 258 | * @return Return 0 on success, ELIMIT or EPERM on error.
|
---|
[7c7aae16] | 259 | */
|
---|
| 260 | static int request_preprocess(call_t *call)
|
---|
| 261 | {
|
---|
| 262 | int newphid;
|
---|
[7c23af9] | 263 | size_t size;
|
---|
[7918fce] | 264 | uintptr_t src;
|
---|
| 265 | int rc;
|
---|
[7c7aae16] | 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 */
|
---|
[51ec40f] | 273 | IPC_SET_ARG3(call->data, (unative_t) &TASK->phones[newphid]);
|
---|
[7c7aae16] | 274 | call->flags |= IPC_CALL_CONN_ME_TO;
|
---|
[0c1a5d8a] | 275 | call->priv = newphid;
|
---|
[7c7aae16] | 276 | break;
|
---|
[8497711] | 277 | case IPC_M_AS_AREA_SEND:
|
---|
[b878df3] | 278 | size = as_area_get_size(IPC_GET_ARG1(call->data));
|
---|
[8b243f2] | 279 | if (!size)
|
---|
[7c23af9] | 280 | return EPERM;
|
---|
[fd4d8c0] | 281 | IPC_SET_ARG2(call->data, size);
|
---|
[7c23af9] | 282 | break;
|
---|
[7918fce] | 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;
|
---|
[7c7aae16] | 297 | default:
|
---|
| 298 | break;
|
---|
| 299 | }
|
---|
| 300 | return 0;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[8b243f2] | 303 | /*******************************************************************************
|
---|
| 304 | * Functions called to process received call/answer before passing it to uspace.
|
---|
| 305 | *******************************************************************************/
|
---|
[2d5a54f3] | 306 |
|
---|
[8b243f2] | 307 | /** Do basic kernel processing of received call answer.
|
---|
| 308 | *
|
---|
| 309 | * @param call Call structure with the answer.
|
---|
| 310 | */
|
---|
[7c7aae16] | 311 | static void process_answer(call_t *call)
|
---|
[2d5a54f3] | 312 | {
|
---|
[51ec40f] | 313 | if (IPC_GET_RETVAL(call->data) == EHANGUP &&
|
---|
| 314 | (call->flags & IPC_CALL_FORWARDED))
|
---|
[9f22213] | 315 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
[7c7aae16] | 316 |
|
---|
| 317 | if (call->flags & IPC_CALL_CONN_ME_TO) {
|
---|
| 318 | if (IPC_GET_RETVAL(call->data))
|
---|
[0c1a5d8a] | 319 | phone_dealloc(call->priv);
|
---|
[7c7aae16] | 320 | else
|
---|
[0c1a5d8a] | 321 | IPC_SET_ARG3(call->data, call->priv);
|
---|
[7c7aae16] | 322 | }
|
---|
[2d5a54f3] | 323 | }
|
---|
| 324 |
|
---|
[8b243f2] | 325 | /** Do basic kernel processing of received call request.
|
---|
| 326 | *
|
---|
| 327 | * @param box Destination answerbox structure.
|
---|
| 328 | * @param call Call structure with the request.
|
---|
[2d5a54f3] | 329 | *
|
---|
[8b243f2] | 330 | * @return Return 0 if the call should be passed to userspace.
|
---|
| 331 | * Return -1 if the call should be ignored.
|
---|
[2d5a54f3] | 332 | */
|
---|
[51ec40f] | 333 | static int process_request(answerbox_t *box, call_t *call)
|
---|
[2d5a54f3] | 334 | {
|
---|
| 335 | int phoneid;
|
---|
| 336 |
|
---|
[fbcfd458] | 337 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
|
---|
[2ba7810] | 338 | phoneid = phone_alloc();
|
---|
[2d5a54f3] | 339 | if (phoneid < 0) { /* Failed to allocate phone */
|
---|
| 340 | IPC_SET_RETVAL(call->data, ELIMIT);
|
---|
[8b243f2] | 341 | ipc_answer(box, call);
|
---|
[2d5a54f3] | 342 | return -1;
|
---|
| 343 | }
|
---|
| 344 | IPC_SET_ARG3(call->data, phoneid);
|
---|
| 345 | }
|
---|
| 346 | return 0;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
[8b243f2] | 349 | /** Make a fast call over IPC, wait for reply and return to user.
|
---|
| 350 | *
|
---|
[2e51969] | 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()).
|
---|
[2d5a54f3] | 353 | *
|
---|
[8b243f2] | 354 | * @param phoneid Phone handle for the call.
|
---|
| 355 | * @param method Method of the call.
|
---|
| 356 | * @param arg1 Service-defined payload argument.
|
---|
[2e51969] | 357 | * @param arg2 Service-defined payload argument.
|
---|
| 358 | * @param arg3 Service-defined payload argument.
|
---|
[8b243f2] | 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.
|
---|
[2d5a54f3] | 364 | */
|
---|
[51ec40f] | 365 | unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
|
---|
[2e51969] | 366 | unative_t arg1, unative_t arg2, unative_t arg3, ipc_data_t *data)
|
---|
[2d5a54f3] | 367 | {
|
---|
| 368 | call_t call;
|
---|
| 369 | phone_t *phone;
|
---|
[7c7aae16] | 370 | int res;
|
---|
[2d5a54f3] | 371 |
|
---|
[ba81cab] | 372 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
[4e49572] | 373 |
|
---|
[ba81cab] | 374 | ipc_call_static_init(&call);
|
---|
[2d5a54f3] | 375 | IPC_SET_METHOD(call.data, method);
|
---|
| 376 | IPC_SET_ARG1(call.data, arg1);
|
---|
[2e51969] | 377 | IPC_SET_ARG2(call.data, arg2);
|
---|
| 378 | IPC_SET_ARG3(call.data, arg3);
|
---|
[2d5a54f3] | 379 |
|
---|
[8b243f2] | 380 | if (!(res = request_preprocess(&call))) {
|
---|
[7c7aae16] | 381 | ipc_call_sync(phone, &call);
|
---|
| 382 | process_answer(&call);
|
---|
[8b243f2] | 383 | } else {
|
---|
[7c7aae16] | 384 | IPC_SET_RETVAL(call.data, res);
|
---|
[8b243f2] | 385 | }
|
---|
[fbcfd458] | 386 | STRUCT_TO_USPACE(&data->args, &call.data.args);
|
---|
[2d5a54f3] | 387 |
|
---|
| 388 | return 0;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[8b243f2] | 391 | /** Make a synchronous IPC call allowing to transmit the entire payload.
|
---|
| 392 | *
|
---|
| 393 | * @param phoneid Phone handle for the call.
|
---|
| 394 | * @param question Userspace address of call data with the request.
|
---|
[7918fce] | 395 | * @param reply Userspace address of call data where to store the
|
---|
| 396 | * answer.
|
---|
[8b243f2] | 397 | *
|
---|
| 398 | * @return Zero on success or an error code.
|
---|
| 399 | */
|
---|
[2e51969] | 400 | unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question,
|
---|
[51ec40f] | 401 | ipc_data_t *reply)
|
---|
[2d5a54f3] | 402 | {
|
---|
| 403 | call_t call;
|
---|
| 404 | phone_t *phone;
|
---|
[7c7aae16] | 405 | int res;
|
---|
[e3c762cd] | 406 | int rc;
|
---|
[2d5a54f3] | 407 |
|
---|
[ba81cab] | 408 | ipc_call_static_init(&call);
|
---|
[51ec40f] | 409 | rc = copy_from_uspace(&call.data.args, &question->args,
|
---|
| 410 | sizeof(call.data.args));
|
---|
[e3c762cd] | 411 | if (rc != 0)
|
---|
[7f1c620] | 412 | return (unative_t) rc;
|
---|
[2ba7810] | 413 |
|
---|
[ba81cab] | 414 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
[4e49572] | 415 |
|
---|
[8b243f2] | 416 | if (!(res = request_preprocess(&call))) {
|
---|
[7c7aae16] | 417 | ipc_call_sync(phone, &call);
|
---|
| 418 | process_answer(&call);
|
---|
| 419 | } else
|
---|
| 420 | IPC_SET_RETVAL(call.data, res);
|
---|
[2d5a54f3] | 421 |
|
---|
[e3c762cd] | 422 | rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
|
---|
| 423 | if (rc != 0)
|
---|
| 424 | return rc;
|
---|
[2d5a54f3] | 425 |
|
---|
| 426 | return 0;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
[8b243f2] | 429 | /** Check that the task did not exceed the allowed limit of asynchronous calls.
|
---|
[2d5a54f3] | 430 | *
|
---|
[8b243f2] | 431 | * @return Return 0 if limit not reached or -1 if limit exceeded.
|
---|
[2d5a54f3] | 432 | */
|
---|
| 433 | static int check_call_limit(void)
|
---|
| 434 | {
|
---|
| 435 | if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
|
---|
| 436 | atomic_dec(&TASK->active_calls);
|
---|
| 437 | return -1;
|
---|
| 438 | }
|
---|
| 439 | return 0;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
[8b243f2] | 442 | /** Make a fast asynchronous call over IPC.
|
---|
[2d5a54f3] | 443 | *
|
---|
[8b243f2] | 444 | * This function can only handle two arguments of payload, but is faster than
|
---|
| 445 | * the generic function sys_ipc_call_async().
|
---|
| 446 | *
|
---|
| 447 | * @param phoneid Phone handle for the call.
|
---|
| 448 | * @param method Method of the call.
|
---|
| 449 | * @param arg1 Service-defined payload argument.
|
---|
| 450 | * @param arg2 Service-defined payload argument.
|
---|
| 451 | *
|
---|
| 452 | * @return Return call hash on success.
|
---|
| 453 | * Return IPC_CALLRET_FATAL in case of a fatal error and
|
---|
| 454 | * IPC_CALLRET_TEMPORARY if there are too many pending
|
---|
| 455 | * asynchronous requests; answers should be handled first.
|
---|
[2d5a54f3] | 456 | */
|
---|
[51ec40f] | 457 | unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
|
---|
| 458 | unative_t arg1, unative_t arg2)
|
---|
[2d5a54f3] | 459 | {
|
---|
| 460 | call_t *call;
|
---|
| 461 | phone_t *phone;
|
---|
[7c7aae16] | 462 | int res;
|
---|
[2ba7810] | 463 |
|
---|
[2d5a54f3] | 464 | if (check_call_limit())
|
---|
| 465 | return IPC_CALLRET_TEMPORARY;
|
---|
| 466 |
|
---|
[7c7aae16] | 467 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
|
---|
[4e49572] | 468 |
|
---|
[5626277] | 469 | call = ipc_call_alloc(0);
|
---|
[2d5a54f3] | 470 | IPC_SET_METHOD(call->data, method);
|
---|
| 471 | IPC_SET_ARG1(call->data, arg1);
|
---|
| 472 | IPC_SET_ARG2(call->data, arg2);
|
---|
[85d24f61] | 473 | IPC_SET_ARG3(call->data, 0);
|
---|
[2d5a54f3] | 474 |
|
---|
[51ec40f] | 475 | if (!(res = request_preprocess(call)))
|
---|
[7c7aae16] | 476 | ipc_call(phone, call);
|
---|
| 477 | else
|
---|
| 478 | ipc_backsend_err(phone, call, res);
|
---|
[2d5a54f3] | 479 |
|
---|
[7f1c620] | 480 | return (unative_t) call;
|
---|
[2d5a54f3] | 481 | }
|
---|
| 482 |
|
---|
[8b243f2] | 483 | /** Make an asynchronous IPC call allowing to transmit the entire payload.
|
---|
| 484 | *
|
---|
| 485 | * @param phoneid Phone handle for the call.
|
---|
| 486 | * @param data Userspace address of call data with the request.
|
---|
[2d5a54f3] | 487 | *
|
---|
[8b243f2] | 488 | * @return See sys_ipc_call_async_fast().
|
---|
[2d5a54f3] | 489 | */
|
---|
[7f1c620] | 490 | unative_t sys_ipc_call_async(unative_t phoneid, ipc_data_t *data)
|
---|
[2d5a54f3] | 491 | {
|
---|
| 492 | call_t *call;
|
---|
| 493 | phone_t *phone;
|
---|
[7c7aae16] | 494 | int res;
|
---|
[e3c762cd] | 495 | int rc;
|
---|
[2d5a54f3] | 496 |
|
---|
| 497 | if (check_call_limit())
|
---|
| 498 | return IPC_CALLRET_TEMPORARY;
|
---|
| 499 |
|
---|
[7c7aae16] | 500 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
|
---|
[4e49572] | 501 |
|
---|
[5626277] | 502 | call = ipc_call_alloc(0);
|
---|
[51ec40f] | 503 | rc = copy_from_uspace(&call->data.args, &data->args,
|
---|
| 504 | sizeof(call->data.args));
|
---|
[f58af46] | 505 | if (rc != 0) {
|
---|
| 506 | ipc_call_free(call);
|
---|
[7f1c620] | 507 | return (unative_t) rc;
|
---|
[f58af46] | 508 | }
|
---|
[51ec40f] | 509 | if (!(res = request_preprocess(call)))
|
---|
[7c7aae16] | 510 | ipc_call(phone, call);
|
---|
| 511 | else
|
---|
| 512 | ipc_backsend_err(phone, call, res);
|
---|
[2d5a54f3] | 513 |
|
---|
[7f1c620] | 514 | return (unative_t) call;
|
---|
[2d5a54f3] | 515 | }
|
---|
| 516 |
|
---|
[8b243f2] | 517 | /** Forward a received call to another destination.
|
---|
| 518 | *
|
---|
| 519 | * @param callid Hash of the call to forward.
|
---|
| 520 | * @param phoneid Phone handle to use for forwarding.
|
---|
| 521 | * @param method New method to use for the forwarded call.
|
---|
| 522 | * @param arg1 New value of the first argument for the forwarded call.
|
---|
| 523 | *
|
---|
| 524 | * @return Return 0 on succes, otherwise return an error code.
|
---|
[2ba7810] | 525 | *
|
---|
[8b243f2] | 526 | * In case the original method is a system method, ARG1 and ARG2 are overwritten
|
---|
| 527 | * in the forwarded message with the new method and the new arg1, respectively.
|
---|
| 528 | * Otherwise the METHOD and ARG1 are rewritten with the new method and arg1,
|
---|
[0dc4258] | 529 | * respectively. Also note there is a set of immutable methods, for which the
|
---|
| 530 | * new method and argument is not set and these values are ignored.
|
---|
[37c57f2] | 531 | *
|
---|
| 532 | * Warning: If implementing non-fast version, make sure that
|
---|
[8b243f2] | 533 | * ARG3 is not rewritten for certain system IPC
|
---|
[2ba7810] | 534 | */
|
---|
[7f1c620] | 535 | unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
|
---|
[51ec40f] | 536 | unative_t method, unative_t arg1)
|
---|
[2ba7810] | 537 | {
|
---|
| 538 | call_t *call;
|
---|
| 539 | phone_t *phone;
|
---|
| 540 |
|
---|
| 541 | call = get_call(callid);
|
---|
| 542 | if (!call)
|
---|
| 543 | return ENOENT;
|
---|
| 544 |
|
---|
[9f22213] | 545 | call->flags |= IPC_CALL_FORWARDED;
|
---|
| 546 |
|
---|
[ba81cab] | 547 | GET_CHECK_PHONE(phone, phoneid, {
|
---|
[2ba7810] | 548 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
| 549 | ipc_answer(&TASK->answerbox, call);
|
---|
| 550 | return ENOENT;
|
---|
[ba81cab] | 551 | });
|
---|
[2ba7810] | 552 |
|
---|
[0dc4258] | 553 | if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
|
---|
[2ba7810] | 554 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
| 555 | ipc_answer(&TASK->answerbox, call);
|
---|
| 556 | return EPERM;
|
---|
| 557 | }
|
---|
| 558 |
|
---|
[0dc4258] | 559 | /*
|
---|
| 560 | * Userspace is not allowed to change method of system methods on
|
---|
| 561 | * forward, allow changing ARG1 and ARG2 by means of method and arg1.
|
---|
| 562 | * If the method is immutable, don't change anything.
|
---|
[2ba7810] | 563 | */
|
---|
[0dc4258] | 564 | if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
|
---|
| 565 | if (method_is_system(IPC_GET_METHOD(call->data))) {
|
---|
| 566 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
|
---|
| 567 | phone_dealloc(IPC_GET_ARG3(call->data));
|
---|
[7c7aae16] | 568 |
|
---|
[0dc4258] | 569 | IPC_SET_ARG1(call->data, method);
|
---|
| 570 | IPC_SET_ARG2(call->data, arg1);
|
---|
| 571 | } else {
|
---|
| 572 | IPC_SET_METHOD(call->data, method);
|
---|
| 573 | IPC_SET_ARG1(call->data, arg1);
|
---|
| 574 | }
|
---|
[2ba7810] | 575 | }
|
---|
| 576 |
|
---|
[9f22213] | 577 | return ipc_forward(call, phone, &TASK->answerbox);
|
---|
[2ba7810] | 578 | }
|
---|
| 579 |
|
---|
[8b243f2] | 580 | /** Answer an IPC call - fast version.
|
---|
| 581 | *
|
---|
| 582 | * This function can handle only two return arguments of payload, but is faster
|
---|
| 583 | * than the generic sys_ipc_answer().
|
---|
| 584 | *
|
---|
| 585 | * @param callid Hash of the call to be answered.
|
---|
| 586 | * @param retval Return value of the answer.
|
---|
| 587 | * @param arg1 Service-defined return value.
|
---|
| 588 | * @param arg2 Service-defined return value.
|
---|
| 589 | *
|
---|
| 590 | * @return Return 0 on success, otherwise return an error code.
|
---|
| 591 | */
|
---|
[51ec40f] | 592 | unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
|
---|
| 593 | unative_t arg1, unative_t arg2)
|
---|
[2d5a54f3] | 594 | {
|
---|
| 595 | call_t *call;
|
---|
[2ba7810] | 596 | ipc_data_t saved_data;
|
---|
[9f22213] | 597 | int saveddata = 0;
|
---|
[7c23af9] | 598 | int rc;
|
---|
[2d5a54f3] | 599 |
|
---|
[897f2e76] | 600 | /* Do not answer notification callids */
|
---|
| 601 | if (callid & IPC_CALLID_NOTIFICATION)
|
---|
| 602 | return 0;
|
---|
| 603 |
|
---|
[2ba7810] | 604 | call = get_call(callid);
|
---|
| 605 | if (!call)
|
---|
| 606 | return ENOENT;
|
---|
[2d5a54f3] | 607 |
|
---|
[9f22213] | 608 | if (answer_need_old(call)) {
|
---|
[2ba7810] | 609 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
[9f22213] | 610 | saveddata = 1;
|
---|
[2d5a54f3] | 611 | }
|
---|
| 612 |
|
---|
| 613 | IPC_SET_RETVAL(call->data, retval);
|
---|
| 614 | IPC_SET_ARG1(call->data, arg1);
|
---|
| 615 | IPC_SET_ARG2(call->data, arg2);
|
---|
[7c23af9] | 616 | rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
|
---|
[2d5a54f3] | 617 |
|
---|
| 618 | ipc_answer(&TASK->answerbox, call);
|
---|
[7c23af9] | 619 | return rc;
|
---|
[2d5a54f3] | 620 | }
|
---|
| 621 |
|
---|
[8b243f2] | 622 | /** Answer an IPC call.
|
---|
| 623 | *
|
---|
| 624 | * @param callid Hash of the call to be answered.
|
---|
| 625 | * @param data Userspace address of call data with the answer.
|
---|
| 626 | *
|
---|
| 627 | * @return Return 0 on success, otherwise return an error code.
|
---|
| 628 | */
|
---|
[7f1c620] | 629 | unative_t sys_ipc_answer(unative_t callid, ipc_data_t *data)
|
---|
[2d5a54f3] | 630 | {
|
---|
| 631 | call_t *call;
|
---|
[2ba7810] | 632 | ipc_data_t saved_data;
|
---|
[9f22213] | 633 | int saveddata = 0;
|
---|
[e3c762cd] | 634 | int rc;
|
---|
[2d5a54f3] | 635 |
|
---|
[897f2e76] | 636 | /* Do not answer notification callids */
|
---|
| 637 | if (callid & IPC_CALLID_NOTIFICATION)
|
---|
| 638 | return 0;
|
---|
| 639 |
|
---|
[2ba7810] | 640 | call = get_call(callid);
|
---|
| 641 | if (!call)
|
---|
| 642 | return ENOENT;
|
---|
[2d5a54f3] | 643 |
|
---|
[9f22213] | 644 | if (answer_need_old(call)) {
|
---|
[2ba7810] | 645 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
[9f22213] | 646 | saveddata = 1;
|
---|
[2d5a54f3] | 647 | }
|
---|
[e3c762cd] | 648 | rc = copy_from_uspace(&call->data.args, &data->args,
|
---|
[51ec40f] | 649 | sizeof(call->data.args));
|
---|
[e3c762cd] | 650 | if (rc != 0)
|
---|
| 651 | return rc;
|
---|
[2d5a54f3] | 652 |
|
---|
[7c23af9] | 653 | rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
|
---|
[2d5a54f3] | 654 |
|
---|
| 655 | ipc_answer(&TASK->answerbox, call);
|
---|
| 656 |
|
---|
[7c23af9] | 657 | return rc;
|
---|
[2d5a54f3] | 658 | }
|
---|
| 659 |
|
---|
[8b243f2] | 660 | /** Hang up a phone.
|
---|
[2d5a54f3] | 661 | *
|
---|
[8b243f2] | 662 | * @param Phone handle of the phone to be hung up.
|
---|
| 663 | *
|
---|
| 664 | * @return Return 0 on success or an error code.
|
---|
[fbcfd458] | 665 | */
|
---|
[7f1c620] | 666 | unative_t sys_ipc_hangup(int phoneid)
|
---|
[fbcfd458] | 667 | {
|
---|
| 668 | phone_t *phone;
|
---|
| 669 |
|
---|
| 670 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
| 671 |
|
---|
[d8f7362] | 672 | if (ipc_phone_hangup(phone))
|
---|
[fbcfd458] | 673 | return -1;
|
---|
| 674 |
|
---|
| 675 | return 0;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
[8b243f2] | 678 | /** Wait for an incoming IPC call or an answer.
|
---|
[fbcfd458] | 679 | *
|
---|
[51ec40f] | 680 | * @param calldata Pointer to buffer where the call/answer data is stored.
|
---|
| 681 | * @param usec Timeout. See waitq_sleep_timeout() for explanation.
|
---|
| 682 | * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
|
---|
| 683 | * for explanation.
|
---|
[bd5a663] | 684 | *
|
---|
[8b243f2] | 685 | * @return Hash of the call.
|
---|
| 686 | * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
|
---|
| 687 | * call is a notification. IPC_CALLID_ANSWERED denotes an
|
---|
| 688 | * answer.
|
---|
[2d5a54f3] | 689 | */
|
---|
[7f1c620] | 690 | unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
|
---|
[2d5a54f3] | 691 | {
|
---|
| 692 | call_t *call;
|
---|
| 693 |
|
---|
| 694 | restart:
|
---|
[51ec40f] | 695 | call = ipc_wait_for_call(&TASK->answerbox, usec,
|
---|
| 696 | flags | SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
[9f22213] | 697 | if (!call)
|
---|
| 698 | return 0;
|
---|
[2d5a54f3] | 699 |
|
---|
[5626277] | 700 | if (call->flags & IPC_CALL_NOTIF) {
|
---|
| 701 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
[43752b6] | 702 |
|
---|
| 703 | /* Set in_phone_hash to the interrupt counter */
|
---|
[0c1a5d8a] | 704 | call->data.phone = (void *) call->priv;
|
---|
[43752b6] | 705 |
|
---|
| 706 | STRUCT_TO_USPACE(calldata, &call->data);
|
---|
| 707 |
|
---|
[5626277] | 708 | ipc_call_free(call);
|
---|
| 709 |
|
---|
[8b243f2] | 710 | return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
|
---|
[5626277] | 711 | }
|
---|
| 712 |
|
---|
[2d5a54f3] | 713 | if (call->flags & IPC_CALL_ANSWERED) {
|
---|
[7c7aae16] | 714 | process_answer(call);
|
---|
[2d5a54f3] | 715 |
|
---|
| 716 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
[fbcfd458] | 717 |
|
---|
| 718 | atomic_dec(&TASK->active_calls);
|
---|
| 719 |
|
---|
| 720 | if (call->flags & IPC_CALL_DISCARD_ANSWER) {
|
---|
| 721 | ipc_call_free(call);
|
---|
| 722 | goto restart;
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | STRUCT_TO_USPACE(&calldata->args, &call->data.args);
|
---|
[2d5a54f3] | 726 | ipc_call_free(call);
|
---|
| 727 |
|
---|
[8b243f2] | 728 | return ((unative_t) call) | IPC_CALLID_ANSWERED;
|
---|
[2d5a54f3] | 729 | }
|
---|
[fbcfd458] | 730 |
|
---|
[2d5a54f3] | 731 | if (process_request(&TASK->answerbox, call))
|
---|
| 732 | goto restart;
|
---|
[fbcfd458] | 733 |
|
---|
[7c7aae16] | 734 | /* Include phone address('id') of the caller in the request,
|
---|
| 735 | * copy whole call->data, not only call->data.args */
|
---|
[bd55bbb] | 736 | if (STRUCT_TO_USPACE(calldata, &call->data)) {
|
---|
| 737 | return 0;
|
---|
| 738 | }
|
---|
[7f1c620] | 739 | return (unative_t)call;
|
---|
[2d5a54f3] | 740 | }
|
---|
[5626277] | 741 |
|
---|
[8b243f2] | 742 | /** Connect an IRQ handler to a task.
|
---|
[2b017ba] | 743 | *
|
---|
[8b243f2] | 744 | * @param inr IRQ number.
|
---|
| 745 | * @param devno Device number.
|
---|
| 746 | * @param method Method to be associated with the notification.
|
---|
| 747 | * @param ucode Uspace pointer to the top-half pseudocode.
|
---|
[2b017ba] | 748 | *
|
---|
[8b243f2] | 749 | * @return EPERM or a return code returned by ipc_irq_register().
|
---|
[2b017ba] | 750 | */
|
---|
[51ec40f] | 751 | unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
|
---|
| 752 | irq_code_t *ucode)
|
---|
[5626277] | 753 | {
|
---|
[2bb8648] | 754 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
---|
| 755 | return EPERM;
|
---|
| 756 |
|
---|
[2b017ba] | 757 | return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
|
---|
[5626277] | 758 | }
|
---|
| 759 |
|
---|
[8b243f2] | 760 | /** Disconnect an IRQ handler from a task.
|
---|
| 761 | *
|
---|
| 762 | * @param inr IRQ number.
|
---|
| 763 | * @param devno Device number.
|
---|
[2b017ba] | 764 | *
|
---|
[8b243f2] | 765 | * @return Zero on success or EPERM on error..
|
---|
[2b017ba] | 766 | */
|
---|
| 767 | unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
|
---|
[5626277] | 768 | {
|
---|
[2bb8648] | 769 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
---|
| 770 | return EPERM;
|
---|
| 771 |
|
---|
[2b017ba] | 772 | ipc_irq_unregister(&TASK->answerbox, inr, devno);
|
---|
[5626277] | 773 |
|
---|
| 774 | return 0;
|
---|
| 775 | }
|
---|
[b45c443] | 776 |
|
---|
[cc73a8a1] | 777 | /** @}
|
---|
[b45c443] | 778 | */
|
---|