[2d5a54f3] | 1 | /*
|
---|
| 2 | * Copyright (C) 2006 Ondrej Palkovsky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <arch.h>
|
---|
| 30 | #include <proc/task.h>
|
---|
[e3c762cd] | 31 | #include <proc/thread.h>
|
---|
[2d5a54f3] | 32 | #include <errno.h>
|
---|
| 33 | #include <memstr.h>
|
---|
| 34 | #include <debug.h>
|
---|
| 35 | #include <ipc/ipc.h>
|
---|
| 36 | #include <ipc/sysipc.h>
|
---|
[162f919] | 37 | #include <ipc/irq.h>
|
---|
[4e49572] | 38 | #include <ipc/ipcrsc.h>
|
---|
[5626277] | 39 | #include <arch/interrupt.h>
|
---|
[2d5a54f3] | 40 | #include <print.h>
|
---|
[e3c762cd] | 41 | #include <syscall/copy.h>
|
---|
[2bb8648] | 42 | #include <security/cap.h>
|
---|
[7c23af9] | 43 | #include <mm/as.h>
|
---|
[2d5a54f3] | 44 |
|
---|
[ba81cab] | 45 | #define GET_CHECK_PHONE(phone,phoneid,err) { \
|
---|
| 46 | if (phoneid > IPC_MAX_PHONES) { err; } \
|
---|
| 47 | phone = &TASK->phones[phoneid]; \
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[7c7aae16] | 50 | #define STRUCT_TO_USPACE(dst,src) copy_to_uspace(dst,src,sizeof(*(src)))
|
---|
[ba81cab] | 51 |
|
---|
[2ba7810] | 52 | /** Return true if the method is a system method */
|
---|
| 53 | static inline int is_system_method(__native method)
|
---|
| 54 | {
|
---|
| 55 | if (method <= IPC_M_LAST_SYSTEM)
|
---|
| 56 | return 1;
|
---|
| 57 | return 0;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /** Return true if the message with this method is forwardable
|
---|
| 61 | *
|
---|
| 62 | * - some system messages may be forwarded, for some of them
|
---|
| 63 | * it is useless
|
---|
| 64 | */
|
---|
| 65 | static inline int is_forwardable(__native method)
|
---|
| 66 | {
|
---|
[46fc2f9] | 67 | if (method == IPC_M_PHONE_HUNGUP || method == IPC_M_AS_AREA_SEND \
|
---|
| 68 | || method == IPC_M_AS_AREA_RECV)
|
---|
[fbcfd458] | 69 | return 0; /* This message is meant only for the receiver */
|
---|
[2ba7810] | 70 | return 1;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[2d5a54f3] | 73 | /****************************************************/
|
---|
| 74 | /* Functions that preprocess answer before sending
|
---|
| 75 | * it to the recepient
|
---|
| 76 | */
|
---|
| 77 |
|
---|
| 78 | /** Return true if the caller (ipc_answer) should save
|
---|
[9f22213] | 79 | * the old call contents for answer_preprocess
|
---|
[2d5a54f3] | 80 | */
|
---|
[9f22213] | 81 | static inline int answer_need_old(call_t *call)
|
---|
[2d5a54f3] | 82 | {
|
---|
[fbcfd458] | 83 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
|
---|
[2d5a54f3] | 84 | return 1;
|
---|
[fbcfd458] | 85 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_ME_TO)
|
---|
[37c57f2] | 86 | return 1;
|
---|
[8497711] | 87 | if (IPC_GET_METHOD(call->data) == IPC_M_AS_AREA_SEND)
|
---|
[7c23af9] | 88 | return 1;
|
---|
[46fc2f9] | 89 | if (IPC_GET_METHOD(call->data) == IPC_M_AS_AREA_RECV)
|
---|
| 90 | return 1;
|
---|
[2d5a54f3] | 91 | return 0;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[46fc2f9] | 94 | /** Interpret process answer as control information
|
---|
| 95 | *
|
---|
| 96 | * This function is called directly after sys_ipc_answer
|
---|
| 97 | */
|
---|
[7c23af9] | 98 | static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
|
---|
[2d5a54f3] | 99 | {
|
---|
| 100 | int phoneid;
|
---|
| 101 |
|
---|
[9f22213] | 102 | if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
|
---|
[ca687ad] | 103 | /* In case of forward, hangup the forwared phone,
|
---|
| 104 | * not the originator
|
---|
| 105 | */
|
---|
| 106 | spinlock_lock(&answer->data.phone->lock);
|
---|
| 107 | spinlock_lock(&TASK->answerbox.lock);
|
---|
[eb3d379] | 108 | if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
|
---|
[c4e4507] | 109 | list_remove(&answer->data.phone->link);
|
---|
[eb3d379] | 110 | answer->data.phone->state = IPC_PHONE_SLAMMED;
|
---|
[ca687ad] | 111 | }
|
---|
| 112 | spinlock_unlock(&TASK->answerbox.lock);
|
---|
| 113 | spinlock_unlock(&answer->data.phone->lock);
|
---|
[9f22213] | 114 | }
|
---|
| 115 |
|
---|
| 116 | if (!olddata)
|
---|
[7c23af9] | 117 | return 0;
|
---|
[9f22213] | 118 |
|
---|
[fbcfd458] | 119 | if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
|
---|
[2ba7810] | 120 | phoneid = IPC_GET_ARG3(*olddata);
|
---|
[2d5a54f3] | 121 | if (IPC_GET_RETVAL(answer->data)) {
|
---|
| 122 | /* The connection was not accepted */
|
---|
| 123 | phone_dealloc(phoneid);
|
---|
[2ba7810] | 124 | } else {
|
---|
[7c7aae16] | 125 | /* The connection was accepted */
|
---|
[2ba7810] | 126 | phone_connect(phoneid,&answer->sender->answerbox);
|
---|
[7c7aae16] | 127 | /* Set 'phone identification' as arg3 of response */
|
---|
| 128 | IPC_SET_ARG3(answer->data, (__native)&TASK->phones[phoneid]);
|
---|
[2d5a54f3] | 129 | }
|
---|
[fbcfd458] | 130 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
|
---|
[37c57f2] | 131 | /* If the users accepted call, connect */
|
---|
| 132 | if (!IPC_GET_RETVAL(answer->data)) {
|
---|
| 133 | ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata),
|
---|
| 134 | &TASK->answerbox);
|
---|
| 135 | }
|
---|
[8497711] | 136 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_SEND) {
|
---|
| 137 | if (!IPC_GET_RETVAL(answer->data)) { /* Accepted, handle as_area receipt */
|
---|
[8d6bc2d5] | 138 | ipl_t ipl;
|
---|
[76d7305] | 139 | int rc;
|
---|
[8d6bc2d5] | 140 | as_t *as;
|
---|
| 141 |
|
---|
| 142 | ipl = interrupts_disable();
|
---|
| 143 | spinlock_lock(&answer->sender->lock);
|
---|
| 144 | as = answer->sender->as;
|
---|
| 145 | spinlock_unlock(&answer->sender->lock);
|
---|
| 146 | interrupts_restore(ipl);
|
---|
| 147 |
|
---|
[76d7305] | 148 | rc = as_area_share(as, IPC_GET_ARG1(*olddata), IPC_GET_ARG2(*olddata),
|
---|
| 149 | AS, IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
|
---|
| 150 | IPC_SET_RETVAL(answer->data, rc);
|
---|
| 151 | return rc;
|
---|
[46fc2f9] | 152 | }
|
---|
| 153 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_RECV) {
|
---|
| 154 | if (!IPC_GET_RETVAL(answer->data)) {
|
---|
| 155 | ipl_t ipl;
|
---|
| 156 | as_t *as;
|
---|
[d6e5cbc] | 157 | int rc;
|
---|
[46fc2f9] | 158 |
|
---|
| 159 | ipl = interrupts_disable();
|
---|
| 160 | spinlock_lock(&answer->sender->lock);
|
---|
| 161 | as = answer->sender->as;
|
---|
| 162 | spinlock_unlock(&answer->sender->lock);
|
---|
| 163 | interrupts_restore(ipl);
|
---|
| 164 |
|
---|
[d6e5cbc] | 165 | rc = as_area_share(AS, IPC_GET_ARG1(answer->data), IPC_GET_ARG2(*olddata),
|
---|
[76d7305] | 166 | as, IPC_GET_ARG1(*olddata), IPC_GET_ARG2(answer->data));
|
---|
[d6e5cbc] | 167 | IPC_SET_RETVAL(answer->data, rc);
|
---|
[7c23af9] | 168 | }
|
---|
[2d5a54f3] | 169 | }
|
---|
[7c23af9] | 170 | return 0;
|
---|
[2d5a54f3] | 171 | }
|
---|
| 172 |
|
---|
[7c7aae16] | 173 | /** Called before the request is sent
|
---|
| 174 | *
|
---|
| 175 | * @return 0 - no error, -1 - report error to user
|
---|
| 176 | */
|
---|
| 177 | static int request_preprocess(call_t *call)
|
---|
| 178 | {
|
---|
| 179 | int newphid;
|
---|
[7c23af9] | 180 | size_t size;
|
---|
[7c7aae16] | 181 |
|
---|
| 182 | switch (IPC_GET_METHOD(call->data)) {
|
---|
| 183 | case IPC_M_CONNECT_ME_TO:
|
---|
| 184 | newphid = phone_alloc();
|
---|
| 185 | if (newphid < 0)
|
---|
| 186 | return ELIMIT;
|
---|
| 187 | /* Set arg3 for server */
|
---|
| 188 | IPC_SET_ARG3(call->data, (__native)&TASK->phones[newphid]);
|
---|
| 189 | call->flags |= IPC_CALL_CONN_ME_TO;
|
---|
| 190 | call->private = newphid;
|
---|
| 191 | break;
|
---|
[8497711] | 192 | case IPC_M_AS_AREA_SEND:
|
---|
[fd4d8c0] | 193 | size = as_get_size(IPC_GET_ARG1(call->data));
|
---|
[7c23af9] | 194 | if (!size) {
|
---|
| 195 | return EPERM;
|
---|
| 196 | }
|
---|
[fd4d8c0] | 197 | IPC_SET_ARG2(call->data, size);
|
---|
[7c23af9] | 198 | break;
|
---|
[7c7aae16] | 199 | default:
|
---|
| 200 | break;
|
---|
| 201 | }
|
---|
| 202 | return 0;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[2d5a54f3] | 205 | /****************************************************/
|
---|
| 206 | /* Functions called to process received call/answer
|
---|
| 207 | * before passing to uspace
|
---|
| 208 | */
|
---|
| 209 |
|
---|
| 210 | /** Do basic kernel processing of received call answer */
|
---|
[7c7aae16] | 211 | static void process_answer(call_t *call)
|
---|
[2d5a54f3] | 212 | {
|
---|
[9f22213] | 213 | if (IPC_GET_RETVAL(call->data) == EHANGUP && \
|
---|
| 214 | call->flags & IPC_CALL_FORWARDED)
|
---|
| 215 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
[7c7aae16] | 216 |
|
---|
| 217 | if (call->flags & IPC_CALL_CONN_ME_TO) {
|
---|
| 218 | if (IPC_GET_RETVAL(call->data))
|
---|
| 219 | phone_dealloc(call->private);
|
---|
| 220 | else
|
---|
| 221 | IPC_SET_ARG3(call->data, call->private);
|
---|
| 222 | }
|
---|
[2d5a54f3] | 223 | }
|
---|
| 224 |
|
---|
| 225 | /** Do basic kernel processing of received call request
|
---|
| 226 | *
|
---|
| 227 | * @return 0 - the call should be passed to userspace, 1 - ignore call
|
---|
| 228 | */
|
---|
| 229 | static int process_request(answerbox_t *box,call_t *call)
|
---|
| 230 | {
|
---|
| 231 | int phoneid;
|
---|
| 232 |
|
---|
[fbcfd458] | 233 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
|
---|
[2ba7810] | 234 | phoneid = phone_alloc();
|
---|
[2d5a54f3] | 235 | if (phoneid < 0) { /* Failed to allocate phone */
|
---|
| 236 | IPC_SET_RETVAL(call->data, ELIMIT);
|
---|
| 237 | ipc_answer(box,call);
|
---|
| 238 | return -1;
|
---|
| 239 | }
|
---|
| 240 | IPC_SET_ARG3(call->data, phoneid);
|
---|
| 241 | }
|
---|
| 242 | return 0;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /** Send a call over IPC, wait for reply, return to user
|
---|
| 246 | *
|
---|
| 247 | * @return Call identification, returns -1 on fatal error,
|
---|
| 248 | -2 on 'Too many async request, handle answers first
|
---|
| 249 | */
|
---|
| 250 | __native sys_ipc_call_sync_fast(__native phoneid, __native method,
|
---|
[fbcfd458] | 251 | __native arg1, ipc_data_t *data)
|
---|
[2d5a54f3] | 252 | {
|
---|
| 253 | call_t call;
|
---|
| 254 | phone_t *phone;
|
---|
[7c7aae16] | 255 | int res;
|
---|
[2d5a54f3] | 256 |
|
---|
[ba81cab] | 257 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
[4e49572] | 258 |
|
---|
[ba81cab] | 259 | ipc_call_static_init(&call);
|
---|
[2d5a54f3] | 260 | IPC_SET_METHOD(call.data, method);
|
---|
| 261 | IPC_SET_ARG1(call.data, arg1);
|
---|
| 262 |
|
---|
[7c7aae16] | 263 | if (!(res=request_preprocess(&call))) {
|
---|
| 264 | ipc_call_sync(phone, &call);
|
---|
| 265 | process_answer(&call);
|
---|
| 266 | } else
|
---|
| 267 | IPC_SET_RETVAL(call.data, res);
|
---|
[fbcfd458] | 268 | STRUCT_TO_USPACE(&data->args, &call.data.args);
|
---|
[2d5a54f3] | 269 |
|
---|
| 270 | return 0;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /** Synchronous IPC call allowing to send whole message */
|
---|
[fbcfd458] | 274 | __native sys_ipc_call_sync(__native phoneid, ipc_data_t *question,
|
---|
| 275 | ipc_data_t *reply)
|
---|
[2d5a54f3] | 276 | {
|
---|
| 277 | call_t call;
|
---|
| 278 | phone_t *phone;
|
---|
[7c7aae16] | 279 | int res;
|
---|
[e3c762cd] | 280 | int rc;
|
---|
[2d5a54f3] | 281 |
|
---|
[ba81cab] | 282 | ipc_call_static_init(&call);
|
---|
[e3c762cd] | 283 | rc = copy_from_uspace(&call.data.args, &question->args, sizeof(call.data.args));
|
---|
| 284 | if (rc != 0)
|
---|
| 285 | return (__native) rc;
|
---|
[2ba7810] | 286 |
|
---|
[ba81cab] | 287 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
[4e49572] | 288 |
|
---|
[7c7aae16] | 289 | if (!(res=request_preprocess(&call))) {
|
---|
| 290 | ipc_call_sync(phone, &call);
|
---|
| 291 | process_answer(&call);
|
---|
| 292 | } else
|
---|
| 293 | IPC_SET_RETVAL(call.data, res);
|
---|
[2d5a54f3] | 294 |
|
---|
[e3c762cd] | 295 | rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
|
---|
| 296 | if (rc != 0)
|
---|
| 297 | return rc;
|
---|
[2d5a54f3] | 298 |
|
---|
| 299 | return 0;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | /** Check that the task did not exceed allowed limit
|
---|
| 303 | *
|
---|
| 304 | * @return 0 - Limit OK, -1 - limit exceeded
|
---|
| 305 | */
|
---|
| 306 | static int check_call_limit(void)
|
---|
| 307 | {
|
---|
| 308 | if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
|
---|
| 309 | atomic_dec(&TASK->active_calls);
|
---|
| 310 | return -1;
|
---|
| 311 | }
|
---|
| 312 | return 0;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | /** Send an asynchronous call over ipc
|
---|
| 316 | *
|
---|
| 317 | * @return Call identification, returns -1 on fatal error,
|
---|
| 318 | -2 on 'Too many async request, handle answers first
|
---|
| 319 | */
|
---|
| 320 | __native sys_ipc_call_async_fast(__native phoneid, __native method,
|
---|
| 321 | __native arg1, __native arg2)
|
---|
| 322 | {
|
---|
| 323 | call_t *call;
|
---|
| 324 | phone_t *phone;
|
---|
[7c7aae16] | 325 | int res;
|
---|
[2ba7810] | 326 |
|
---|
[2d5a54f3] | 327 | if (check_call_limit())
|
---|
| 328 | return IPC_CALLRET_TEMPORARY;
|
---|
| 329 |
|
---|
[7c7aae16] | 330 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
|
---|
[4e49572] | 331 |
|
---|
[5626277] | 332 | call = ipc_call_alloc(0);
|
---|
[2d5a54f3] | 333 | IPC_SET_METHOD(call->data, method);
|
---|
| 334 | IPC_SET_ARG1(call->data, arg1);
|
---|
| 335 | IPC_SET_ARG2(call->data, arg2);
|
---|
[85d24f61] | 336 | IPC_SET_ARG3(call->data, 0);
|
---|
[2d5a54f3] | 337 |
|
---|
[7c7aae16] | 338 | if (!(res=request_preprocess(call)))
|
---|
| 339 | ipc_call(phone, call);
|
---|
| 340 | else
|
---|
| 341 | ipc_backsend_err(phone, call, res);
|
---|
[2d5a54f3] | 342 |
|
---|
| 343 | return (__native) call;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | /** Synchronous IPC call allowing to send whole message
|
---|
| 347 | *
|
---|
| 348 | * @return The same as sys_ipc_call_async
|
---|
| 349 | */
|
---|
[fbcfd458] | 350 | __native sys_ipc_call_async(__native phoneid, ipc_data_t *data)
|
---|
[2d5a54f3] | 351 | {
|
---|
| 352 | call_t *call;
|
---|
| 353 | phone_t *phone;
|
---|
[7c7aae16] | 354 | int res;
|
---|
[e3c762cd] | 355 | int rc;
|
---|
[2d5a54f3] | 356 |
|
---|
| 357 | if (check_call_limit())
|
---|
| 358 | return IPC_CALLRET_TEMPORARY;
|
---|
| 359 |
|
---|
[7c7aae16] | 360 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
|
---|
[4e49572] | 361 |
|
---|
[5626277] | 362 | call = ipc_call_alloc(0);
|
---|
[e3c762cd] | 363 | rc = copy_from_uspace(&call->data.args, &data->args, sizeof(call->data.args));
|
---|
[f58af46] | 364 | if (rc != 0) {
|
---|
| 365 | ipc_call_free(call);
|
---|
[e3c762cd] | 366 | return (__native) rc;
|
---|
[f58af46] | 367 | }
|
---|
[7c7aae16] | 368 | if (!(res=request_preprocess(call)))
|
---|
| 369 | ipc_call(phone, call);
|
---|
| 370 | else
|
---|
| 371 | ipc_backsend_err(phone, call, res);
|
---|
[2d5a54f3] | 372 |
|
---|
| 373 | return (__native) call;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[2ba7810] | 376 | /** Forward received call to another destination
|
---|
| 377 | *
|
---|
| 378 | * The arg1 and arg2 are changed in the forwarded message
|
---|
[37c57f2] | 379 | *
|
---|
| 380 | * Warning: If implementing non-fast version, make sure that
|
---|
| 381 | * arg3 is not rewritten for certain system IPC
|
---|
[2ba7810] | 382 | */
|
---|
| 383 | __native sys_ipc_forward_fast(__native callid, __native phoneid,
|
---|
| 384 | __native method, __native arg1)
|
---|
| 385 | {
|
---|
| 386 | call_t *call;
|
---|
| 387 | phone_t *phone;
|
---|
| 388 |
|
---|
| 389 | call = get_call(callid);
|
---|
| 390 | if (!call)
|
---|
| 391 | return ENOENT;
|
---|
| 392 |
|
---|
[9f22213] | 393 | call->flags |= IPC_CALL_FORWARDED;
|
---|
| 394 |
|
---|
[ba81cab] | 395 | GET_CHECK_PHONE(phone, phoneid, {
|
---|
[2ba7810] | 396 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
| 397 | ipc_answer(&TASK->answerbox, call);
|
---|
| 398 | return ENOENT;
|
---|
[ba81cab] | 399 | });
|
---|
[2ba7810] | 400 |
|
---|
| 401 | if (!is_forwardable(IPC_GET_METHOD(call->data))) {
|
---|
| 402 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
| 403 | ipc_answer(&TASK->answerbox, call);
|
---|
| 404 | return EPERM;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | /* Userspace is not allowed to change method of system methods
|
---|
| 408 | * on forward, allow changing ARG1 and ARG2 by means of method and arg1
|
---|
| 409 | */
|
---|
| 410 | if (is_system_method(IPC_GET_METHOD(call->data))) {
|
---|
[7c7aae16] | 411 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
|
---|
| 412 | phone_dealloc(IPC_GET_ARG3(call->data));
|
---|
| 413 |
|
---|
[2ba7810] | 414 | IPC_SET_ARG1(call->data, method);
|
---|
| 415 | IPC_SET_ARG2(call->data, arg1);
|
---|
| 416 | } else {
|
---|
| 417 | IPC_SET_METHOD(call->data, method);
|
---|
| 418 | IPC_SET_ARG1(call->data, arg1);
|
---|
| 419 | }
|
---|
| 420 |
|
---|
[9f22213] | 421 | return ipc_forward(call, phone, &TASK->answerbox);
|
---|
[2ba7810] | 422 | }
|
---|
| 423 |
|
---|
[2d5a54f3] | 424 | /** Send IPC answer */
|
---|
| 425 | __native sys_ipc_answer_fast(__native callid, __native retval,
|
---|
| 426 | __native arg1, __native arg2)
|
---|
| 427 | {
|
---|
| 428 | call_t *call;
|
---|
[2ba7810] | 429 | ipc_data_t saved_data;
|
---|
[9f22213] | 430 | int saveddata = 0;
|
---|
[7c23af9] | 431 | int rc;
|
---|
[2d5a54f3] | 432 |
|
---|
[897f2e76] | 433 | /* Do not answer notification callids */
|
---|
| 434 | if (callid & IPC_CALLID_NOTIFICATION)
|
---|
| 435 | return 0;
|
---|
| 436 |
|
---|
[2ba7810] | 437 | call = get_call(callid);
|
---|
| 438 | if (!call)
|
---|
| 439 | return ENOENT;
|
---|
[2d5a54f3] | 440 |
|
---|
[9f22213] | 441 | if (answer_need_old(call)) {
|
---|
[2ba7810] | 442 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
[9f22213] | 443 | saveddata = 1;
|
---|
[2d5a54f3] | 444 | }
|
---|
| 445 |
|
---|
| 446 | IPC_SET_RETVAL(call->data, retval);
|
---|
| 447 | IPC_SET_ARG1(call->data, arg1);
|
---|
| 448 | IPC_SET_ARG2(call->data, arg2);
|
---|
[7c23af9] | 449 | rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
|
---|
[2d5a54f3] | 450 |
|
---|
| 451 | ipc_answer(&TASK->answerbox, call);
|
---|
[7c23af9] | 452 | return rc;
|
---|
[2d5a54f3] | 453 | }
|
---|
| 454 |
|
---|
| 455 | /** Send IPC answer */
|
---|
[fbcfd458] | 456 | __native sys_ipc_answer(__native callid, ipc_data_t *data)
|
---|
[2d5a54f3] | 457 | {
|
---|
| 458 | call_t *call;
|
---|
[2ba7810] | 459 | ipc_data_t saved_data;
|
---|
[9f22213] | 460 | int saveddata = 0;
|
---|
[e3c762cd] | 461 | int rc;
|
---|
[2d5a54f3] | 462 |
|
---|
[897f2e76] | 463 | /* Do not answer notification callids */
|
---|
| 464 | if (callid & IPC_CALLID_NOTIFICATION)
|
---|
| 465 | return 0;
|
---|
| 466 |
|
---|
[2ba7810] | 467 | call = get_call(callid);
|
---|
| 468 | if (!call)
|
---|
| 469 | return ENOENT;
|
---|
[2d5a54f3] | 470 |
|
---|
[9f22213] | 471 | if (answer_need_old(call)) {
|
---|
[2ba7810] | 472 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
[9f22213] | 473 | saveddata = 1;
|
---|
[2d5a54f3] | 474 | }
|
---|
[e3c762cd] | 475 | rc = copy_from_uspace(&call->data.args, &data->args,
|
---|
[fbcfd458] | 476 | sizeof(call->data.args));
|
---|
[e3c762cd] | 477 | if (rc != 0)
|
---|
| 478 | return rc;
|
---|
[2d5a54f3] | 479 |
|
---|
[7c23af9] | 480 | rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
|
---|
[2d5a54f3] | 481 |
|
---|
| 482 | ipc_answer(&TASK->answerbox, call);
|
---|
| 483 |
|
---|
[7c23af9] | 484 | return rc;
|
---|
[2d5a54f3] | 485 | }
|
---|
| 486 |
|
---|
[fbcfd458] | 487 | /** Hang up the phone
|
---|
[2d5a54f3] | 488 | *
|
---|
[fbcfd458] | 489 | */
|
---|
| 490 | __native sys_ipc_hangup(int phoneid)
|
---|
| 491 | {
|
---|
| 492 | phone_t *phone;
|
---|
| 493 |
|
---|
| 494 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
| 495 |
|
---|
[d8f7362] | 496 | if (ipc_phone_hangup(phone))
|
---|
[fbcfd458] | 497 | return -1;
|
---|
| 498 |
|
---|
| 499 | return 0;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | /** Wait for incoming ipc call or answer
|
---|
| 503 | *
|
---|
| 504 | * @param calldata Pointer to buffer where the call/answer data is stored
|
---|
[bd5a663] | 505 | * @param usec Timeout. See waitq_sleep_timeout() for explanation.
|
---|
[116d1ef4] | 506 | * @param flags Select mode of sleep operation. See waitq_sleep_timeout() for explanation.
|
---|
[bd5a663] | 507 | *
|
---|
[2d5a54f3] | 508 | * @return Callid, if callid & 1, then the call is answer
|
---|
| 509 | */
|
---|
[116d1ef4] | 510 | __native sys_ipc_wait_for_call(ipc_data_t *calldata, __u32 usec, int flags)
|
---|
[2d5a54f3] | 511 | {
|
---|
| 512 | call_t *call;
|
---|
| 513 |
|
---|
| 514 | restart:
|
---|
[116d1ef4] | 515 | call = ipc_wait_for_call(&TASK->answerbox, usec, flags | SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
[9f22213] | 516 | if (!call)
|
---|
| 517 | return 0;
|
---|
[2d5a54f3] | 518 |
|
---|
[5626277] | 519 | if (call->flags & IPC_CALL_NOTIF) {
|
---|
| 520 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
[43752b6] | 521 |
|
---|
| 522 | /* Set in_phone_hash to the interrupt counter */
|
---|
| 523 | call->data.phone = (void *)call->private;
|
---|
| 524 |
|
---|
| 525 | STRUCT_TO_USPACE(calldata, &call->data);
|
---|
| 526 |
|
---|
[5626277] | 527 | ipc_call_free(call);
|
---|
| 528 |
|
---|
| 529 | return ((__native)call) | IPC_CALLID_NOTIFICATION;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
[2d5a54f3] | 532 | if (call->flags & IPC_CALL_ANSWERED) {
|
---|
[7c7aae16] | 533 | process_answer(call);
|
---|
[2d5a54f3] | 534 |
|
---|
| 535 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
[fbcfd458] | 536 |
|
---|
| 537 | atomic_dec(&TASK->active_calls);
|
---|
| 538 |
|
---|
| 539 | if (call->flags & IPC_CALL_DISCARD_ANSWER) {
|
---|
| 540 | ipc_call_free(call);
|
---|
| 541 | goto restart;
|
---|
| 542 | }
|
---|
| 543 |
|
---|
| 544 | STRUCT_TO_USPACE(&calldata->args, &call->data.args);
|
---|
[2d5a54f3] | 545 | ipc_call_free(call);
|
---|
| 546 |
|
---|
| 547 | return ((__native)call) | IPC_CALLID_ANSWERED;
|
---|
| 548 | }
|
---|
[fbcfd458] | 549 |
|
---|
[2d5a54f3] | 550 | if (process_request(&TASK->answerbox, call))
|
---|
| 551 | goto restart;
|
---|
[fbcfd458] | 552 |
|
---|
[7c7aae16] | 553 | /* Include phone address('id') of the caller in the request,
|
---|
| 554 | * copy whole call->data, not only call->data.args */
|
---|
[bd55bbb] | 555 | if (STRUCT_TO_USPACE(calldata, &call->data)) {
|
---|
| 556 | return 0;
|
---|
| 557 | }
|
---|
[2d5a54f3] | 558 | return (__native)call;
|
---|
| 559 | }
|
---|
[5626277] | 560 |
|
---|
| 561 | /** Connect irq handler to task */
|
---|
[874621f] | 562 | __native sys_ipc_register_irq(int irq, irq_code_t *ucode)
|
---|
[5626277] | 563 | {
|
---|
[2bb8648] | 564 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
---|
| 565 | return EPERM;
|
---|
| 566 |
|
---|
[874621f] | 567 | if (irq >= IRQ_COUNT || irq <= -IPC_IRQ_RESERVED_VIRTUAL)
|
---|
[2bb8648] | 568 | return (__native) ELIMIT;
|
---|
[874621f] | 569 |
|
---|
[5626277] | 570 | irq_ipc_bind_arch(irq);
|
---|
[162f919] | 571 |
|
---|
| 572 | return ipc_irq_register(&TASK->answerbox, irq, ucode);
|
---|
[5626277] | 573 | }
|
---|
| 574 |
|
---|
| 575 | /* Disconnect irq handler from task */
|
---|
[874621f] | 576 | __native sys_ipc_unregister_irq(int irq)
|
---|
[5626277] | 577 | {
|
---|
[2bb8648] | 578 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
---|
| 579 | return EPERM;
|
---|
| 580 |
|
---|
[874621f] | 581 | if (irq >= IRQ_COUNT || irq <= -IPC_IRQ_RESERVED_VIRTUAL)
|
---|
[2bb8648] | 582 | return (__native) ELIMIT;
|
---|
[5626277] | 583 |
|
---|
| 584 | ipc_irq_unregister(&TASK->answerbox, irq);
|
---|
| 585 |
|
---|
| 586 | return 0;
|
---|
| 587 | }
|
---|