[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);
|
---|
| 108 | if (answer->data.phone->callee) {
|
---|
| 109 | list_remove(&answer->data.phone->list);
|
---|
| 110 | answer->data.phone->callee = 0;
|
---|
| 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;
|
---|
| 139 | as_t *as;
|
---|
| 140 |
|
---|
| 141 | ipl = interrupts_disable();
|
---|
| 142 | spinlock_lock(&answer->sender->lock);
|
---|
| 143 | as = answer->sender->as;
|
---|
| 144 | spinlock_unlock(&answer->sender->lock);
|
---|
| 145 | interrupts_restore(ipl);
|
---|
| 146 |
|
---|
[fd4d8c0] | 147 | return as_area_share(as, IPC_GET_ARG1(*olddata), IPC_GET_ARG2(*olddata),
|
---|
[46fc2f9] | 148 | AS, IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
|
---|
| 149 | }
|
---|
| 150 | } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_RECV) {
|
---|
| 151 | if (!IPC_GET_RETVAL(answer->data)) {
|
---|
| 152 | ipl_t ipl;
|
---|
| 153 | as_t *as;
|
---|
[d6e5cbc] | 154 | int rc;
|
---|
[46fc2f9] | 155 |
|
---|
| 156 | ipl = interrupts_disable();
|
---|
| 157 | spinlock_lock(&answer->sender->lock);
|
---|
| 158 | as = answer->sender->as;
|
---|
| 159 | spinlock_unlock(&answer->sender->lock);
|
---|
| 160 | interrupts_restore(ipl);
|
---|
| 161 |
|
---|
[d6e5cbc] | 162 | rc = as_area_share(AS, IPC_GET_ARG1(answer->data), IPC_GET_ARG2(*olddata),
|
---|
| 163 | as, IPC_GET_ARG1(*olddata), IPC_GET_ARG3(*olddata));
|
---|
| 164 | IPC_SET_RETVAL(answer->data, rc);
|
---|
[7c23af9] | 165 | }
|
---|
[2d5a54f3] | 166 | }
|
---|
[7c23af9] | 167 | return 0;
|
---|
[2d5a54f3] | 168 | }
|
---|
| 169 |
|
---|
[7c7aae16] | 170 | /** Called before the request is sent
|
---|
| 171 | *
|
---|
| 172 | * @return 0 - no error, -1 - report error to user
|
---|
| 173 | */
|
---|
| 174 | static int request_preprocess(call_t *call)
|
---|
| 175 | {
|
---|
| 176 | int newphid;
|
---|
[7c23af9] | 177 | size_t size;
|
---|
[7c7aae16] | 178 |
|
---|
| 179 | switch (IPC_GET_METHOD(call->data)) {
|
---|
| 180 | case IPC_M_CONNECT_ME_TO:
|
---|
| 181 | newphid = phone_alloc();
|
---|
| 182 | if (newphid < 0)
|
---|
| 183 | return ELIMIT;
|
---|
| 184 | /* Set arg3 for server */
|
---|
| 185 | IPC_SET_ARG3(call->data, (__native)&TASK->phones[newphid]);
|
---|
| 186 | call->flags |= IPC_CALL_CONN_ME_TO;
|
---|
| 187 | call->private = newphid;
|
---|
| 188 | break;
|
---|
[8497711] | 189 | case IPC_M_AS_AREA_SEND:
|
---|
[fd4d8c0] | 190 | size = as_get_size(IPC_GET_ARG1(call->data));
|
---|
[7c23af9] | 191 | if (!size) {
|
---|
| 192 | return EPERM;
|
---|
| 193 | }
|
---|
[fd4d8c0] | 194 | IPC_SET_ARG2(call->data, size);
|
---|
[7c23af9] | 195 | break;
|
---|
[7c7aae16] | 196 | default:
|
---|
| 197 | break;
|
---|
| 198 | }
|
---|
| 199 | return 0;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[2d5a54f3] | 202 | /****************************************************/
|
---|
| 203 | /* Functions called to process received call/answer
|
---|
| 204 | * before passing to uspace
|
---|
| 205 | */
|
---|
| 206 |
|
---|
| 207 | /** Do basic kernel processing of received call answer */
|
---|
[7c7aae16] | 208 | static void process_answer(call_t *call)
|
---|
[2d5a54f3] | 209 | {
|
---|
[9f22213] | 210 | if (IPC_GET_RETVAL(call->data) == EHANGUP && \
|
---|
| 211 | call->flags & IPC_CALL_FORWARDED)
|
---|
| 212 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
[7c7aae16] | 213 |
|
---|
| 214 | if (call->flags & IPC_CALL_CONN_ME_TO) {
|
---|
| 215 | if (IPC_GET_RETVAL(call->data))
|
---|
| 216 | phone_dealloc(call->private);
|
---|
| 217 | else
|
---|
| 218 | IPC_SET_ARG3(call->data, call->private);
|
---|
| 219 | }
|
---|
[2d5a54f3] | 220 | }
|
---|
| 221 |
|
---|
| 222 | /** Do basic kernel processing of received call request
|
---|
| 223 | *
|
---|
| 224 | * @return 0 - the call should be passed to userspace, 1 - ignore call
|
---|
| 225 | */
|
---|
| 226 | static int process_request(answerbox_t *box,call_t *call)
|
---|
| 227 | {
|
---|
| 228 | int phoneid;
|
---|
| 229 |
|
---|
[fbcfd458] | 230 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
|
---|
[2ba7810] | 231 | phoneid = phone_alloc();
|
---|
[2d5a54f3] | 232 | if (phoneid < 0) { /* Failed to allocate phone */
|
---|
| 233 | IPC_SET_RETVAL(call->data, ELIMIT);
|
---|
| 234 | ipc_answer(box,call);
|
---|
| 235 | return -1;
|
---|
| 236 | }
|
---|
| 237 | IPC_SET_ARG3(call->data, phoneid);
|
---|
| 238 | }
|
---|
| 239 | return 0;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /** Send a call over IPC, wait for reply, return to user
|
---|
| 243 | *
|
---|
| 244 | * @return Call identification, returns -1 on fatal error,
|
---|
| 245 | -2 on 'Too many async request, handle answers first
|
---|
| 246 | */
|
---|
| 247 | __native sys_ipc_call_sync_fast(__native phoneid, __native method,
|
---|
[fbcfd458] | 248 | __native arg1, ipc_data_t *data)
|
---|
[2d5a54f3] | 249 | {
|
---|
| 250 | call_t call;
|
---|
| 251 | phone_t *phone;
|
---|
[7c7aae16] | 252 | int res;
|
---|
[2d5a54f3] | 253 |
|
---|
[ba81cab] | 254 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
[4e49572] | 255 |
|
---|
[ba81cab] | 256 | ipc_call_static_init(&call);
|
---|
[2d5a54f3] | 257 | IPC_SET_METHOD(call.data, method);
|
---|
| 258 | IPC_SET_ARG1(call.data, arg1);
|
---|
| 259 |
|
---|
[7c7aae16] | 260 | if (!(res=request_preprocess(&call))) {
|
---|
| 261 | ipc_call_sync(phone, &call);
|
---|
| 262 | process_answer(&call);
|
---|
| 263 | } else
|
---|
| 264 | IPC_SET_RETVAL(call.data, res);
|
---|
[fbcfd458] | 265 | STRUCT_TO_USPACE(&data->args, &call.data.args);
|
---|
[2d5a54f3] | 266 |
|
---|
| 267 | return 0;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /** Synchronous IPC call allowing to send whole message */
|
---|
[fbcfd458] | 271 | __native sys_ipc_call_sync(__native phoneid, ipc_data_t *question,
|
---|
| 272 | ipc_data_t *reply)
|
---|
[2d5a54f3] | 273 | {
|
---|
| 274 | call_t call;
|
---|
| 275 | phone_t *phone;
|
---|
[7c7aae16] | 276 | int res;
|
---|
[e3c762cd] | 277 | int rc;
|
---|
[2d5a54f3] | 278 |
|
---|
[ba81cab] | 279 | ipc_call_static_init(&call);
|
---|
[e3c762cd] | 280 | rc = copy_from_uspace(&call.data.args, &question->args, sizeof(call.data.args));
|
---|
| 281 | if (rc != 0)
|
---|
| 282 | return (__native) rc;
|
---|
[2ba7810] | 283 |
|
---|
[ba81cab] | 284 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
[4e49572] | 285 |
|
---|
[7c7aae16] | 286 | if (!(res=request_preprocess(&call))) {
|
---|
| 287 | ipc_call_sync(phone, &call);
|
---|
| 288 | process_answer(&call);
|
---|
| 289 | } else
|
---|
| 290 | IPC_SET_RETVAL(call.data, res);
|
---|
[2d5a54f3] | 291 |
|
---|
[e3c762cd] | 292 | rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
|
---|
| 293 | if (rc != 0)
|
---|
| 294 | return rc;
|
---|
[2d5a54f3] | 295 |
|
---|
| 296 | return 0;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /** Check that the task did not exceed allowed limit
|
---|
| 300 | *
|
---|
| 301 | * @return 0 - Limit OK, -1 - limit exceeded
|
---|
| 302 | */
|
---|
| 303 | static int check_call_limit(void)
|
---|
| 304 | {
|
---|
| 305 | if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
|
---|
| 306 | atomic_dec(&TASK->active_calls);
|
---|
| 307 | return -1;
|
---|
| 308 | }
|
---|
| 309 | return 0;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | /** Send an asynchronous call over ipc
|
---|
| 313 | *
|
---|
| 314 | * @return Call identification, returns -1 on fatal error,
|
---|
| 315 | -2 on 'Too many async request, handle answers first
|
---|
| 316 | */
|
---|
| 317 | __native sys_ipc_call_async_fast(__native phoneid, __native method,
|
---|
| 318 | __native arg1, __native arg2)
|
---|
| 319 | {
|
---|
| 320 | call_t *call;
|
---|
| 321 | phone_t *phone;
|
---|
[7c7aae16] | 322 | int res;
|
---|
[2ba7810] | 323 |
|
---|
[2d5a54f3] | 324 | if (check_call_limit())
|
---|
| 325 | return IPC_CALLRET_TEMPORARY;
|
---|
| 326 |
|
---|
[7c7aae16] | 327 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
|
---|
[4e49572] | 328 |
|
---|
[5626277] | 329 | call = ipc_call_alloc(0);
|
---|
[2d5a54f3] | 330 | IPC_SET_METHOD(call->data, method);
|
---|
| 331 | IPC_SET_ARG1(call->data, arg1);
|
---|
| 332 | IPC_SET_ARG2(call->data, arg2);
|
---|
| 333 |
|
---|
[7c7aae16] | 334 | if (!(res=request_preprocess(call)))
|
---|
| 335 | ipc_call(phone, call);
|
---|
| 336 | else
|
---|
| 337 | ipc_backsend_err(phone, call, res);
|
---|
[2d5a54f3] | 338 |
|
---|
| 339 | return (__native) call;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | /** Synchronous IPC call allowing to send whole message
|
---|
| 343 | *
|
---|
| 344 | * @return The same as sys_ipc_call_async
|
---|
| 345 | */
|
---|
[fbcfd458] | 346 | __native sys_ipc_call_async(__native phoneid, ipc_data_t *data)
|
---|
[2d5a54f3] | 347 | {
|
---|
| 348 | call_t *call;
|
---|
| 349 | phone_t *phone;
|
---|
[7c7aae16] | 350 | int res;
|
---|
[e3c762cd] | 351 | int rc;
|
---|
[2d5a54f3] | 352 |
|
---|
| 353 | if (check_call_limit())
|
---|
| 354 | return IPC_CALLRET_TEMPORARY;
|
---|
| 355 |
|
---|
[7c7aae16] | 356 | GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
|
---|
[4e49572] | 357 |
|
---|
[5626277] | 358 | call = ipc_call_alloc(0);
|
---|
[e3c762cd] | 359 | rc = copy_from_uspace(&call->data.args, &data->args, sizeof(call->data.args));
|
---|
[f58af46] | 360 | if (rc != 0) {
|
---|
| 361 | ipc_call_free(call);
|
---|
[e3c762cd] | 362 | return (__native) rc;
|
---|
[f58af46] | 363 | }
|
---|
[7c7aae16] | 364 | if (!(res=request_preprocess(call)))
|
---|
| 365 | ipc_call(phone, call);
|
---|
| 366 | else
|
---|
| 367 | ipc_backsend_err(phone, call, res);
|
---|
[2d5a54f3] | 368 |
|
---|
| 369 | return (__native) call;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[2ba7810] | 372 | /** Forward received call to another destination
|
---|
| 373 | *
|
---|
| 374 | * The arg1 and arg2 are changed in the forwarded message
|
---|
[37c57f2] | 375 | *
|
---|
| 376 | * Warning: If implementing non-fast version, make sure that
|
---|
| 377 | * arg3 is not rewritten for certain system IPC
|
---|
[2ba7810] | 378 | */
|
---|
| 379 | __native sys_ipc_forward_fast(__native callid, __native phoneid,
|
---|
| 380 | __native method, __native arg1)
|
---|
| 381 | {
|
---|
| 382 | call_t *call;
|
---|
| 383 | phone_t *phone;
|
---|
| 384 |
|
---|
| 385 | call = get_call(callid);
|
---|
| 386 | if (!call)
|
---|
| 387 | return ENOENT;
|
---|
| 388 |
|
---|
[9f22213] | 389 | call->flags |= IPC_CALL_FORWARDED;
|
---|
| 390 |
|
---|
[ba81cab] | 391 | GET_CHECK_PHONE(phone, phoneid, {
|
---|
[2ba7810] | 392 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
| 393 | ipc_answer(&TASK->answerbox, call);
|
---|
| 394 | return ENOENT;
|
---|
[ba81cab] | 395 | });
|
---|
[2ba7810] | 396 |
|
---|
| 397 | if (!is_forwardable(IPC_GET_METHOD(call->data))) {
|
---|
| 398 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
| 399 | ipc_answer(&TASK->answerbox, call);
|
---|
| 400 | return EPERM;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | /* Userspace is not allowed to change method of system methods
|
---|
| 404 | * on forward, allow changing ARG1 and ARG2 by means of method and arg1
|
---|
| 405 | */
|
---|
| 406 | if (is_system_method(IPC_GET_METHOD(call->data))) {
|
---|
[7c7aae16] | 407 | if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
|
---|
| 408 | phone_dealloc(IPC_GET_ARG3(call->data));
|
---|
| 409 |
|
---|
[2ba7810] | 410 | IPC_SET_ARG1(call->data, method);
|
---|
| 411 | IPC_SET_ARG2(call->data, arg1);
|
---|
| 412 | } else {
|
---|
| 413 | IPC_SET_METHOD(call->data, method);
|
---|
| 414 | IPC_SET_ARG1(call->data, arg1);
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[9f22213] | 417 | return ipc_forward(call, phone, &TASK->answerbox);
|
---|
[2ba7810] | 418 | }
|
---|
| 419 |
|
---|
[2d5a54f3] | 420 | /** Send IPC answer */
|
---|
| 421 | __native sys_ipc_answer_fast(__native callid, __native retval,
|
---|
| 422 | __native arg1, __native arg2)
|
---|
| 423 | {
|
---|
| 424 | call_t *call;
|
---|
[2ba7810] | 425 | ipc_data_t saved_data;
|
---|
[9f22213] | 426 | int saveddata = 0;
|
---|
[7c23af9] | 427 | int rc;
|
---|
[2d5a54f3] | 428 |
|
---|
[897f2e76] | 429 | /* Do not answer notification callids */
|
---|
| 430 | if (callid & IPC_CALLID_NOTIFICATION)
|
---|
| 431 | return 0;
|
---|
| 432 |
|
---|
[2ba7810] | 433 | call = get_call(callid);
|
---|
| 434 | if (!call)
|
---|
| 435 | return ENOENT;
|
---|
[2d5a54f3] | 436 |
|
---|
[9f22213] | 437 | if (answer_need_old(call)) {
|
---|
[2ba7810] | 438 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
[9f22213] | 439 | saveddata = 1;
|
---|
[2d5a54f3] | 440 | }
|
---|
| 441 |
|
---|
| 442 | IPC_SET_RETVAL(call->data, retval);
|
---|
| 443 | IPC_SET_ARG1(call->data, arg1);
|
---|
| 444 | IPC_SET_ARG2(call->data, arg2);
|
---|
[7c23af9] | 445 | rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
|
---|
[2d5a54f3] | 446 |
|
---|
| 447 | ipc_answer(&TASK->answerbox, call);
|
---|
[7c23af9] | 448 | return rc;
|
---|
[2d5a54f3] | 449 | }
|
---|
| 450 |
|
---|
| 451 | /** Send IPC answer */
|
---|
[fbcfd458] | 452 | __native sys_ipc_answer(__native callid, ipc_data_t *data)
|
---|
[2d5a54f3] | 453 | {
|
---|
| 454 | call_t *call;
|
---|
[2ba7810] | 455 | ipc_data_t saved_data;
|
---|
[9f22213] | 456 | int saveddata = 0;
|
---|
[e3c762cd] | 457 | int rc;
|
---|
[2d5a54f3] | 458 |
|
---|
[897f2e76] | 459 | /* Do not answer notification callids */
|
---|
| 460 | if (callid & IPC_CALLID_NOTIFICATION)
|
---|
| 461 | return 0;
|
---|
| 462 |
|
---|
[2ba7810] | 463 | call = get_call(callid);
|
---|
| 464 | if (!call)
|
---|
| 465 | return ENOENT;
|
---|
[2d5a54f3] | 466 |
|
---|
[9f22213] | 467 | if (answer_need_old(call)) {
|
---|
[2ba7810] | 468 | memcpy(&saved_data, &call->data, sizeof(call->data));
|
---|
[9f22213] | 469 | saveddata = 1;
|
---|
[2d5a54f3] | 470 | }
|
---|
[e3c762cd] | 471 | rc = copy_from_uspace(&call->data.args, &data->args,
|
---|
[fbcfd458] | 472 | sizeof(call->data.args));
|
---|
[e3c762cd] | 473 | if (rc != 0)
|
---|
| 474 | return rc;
|
---|
[2d5a54f3] | 475 |
|
---|
[7c23af9] | 476 | rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
|
---|
[2d5a54f3] | 477 |
|
---|
| 478 | ipc_answer(&TASK->answerbox, call);
|
---|
| 479 |
|
---|
[7c23af9] | 480 | return rc;
|
---|
[2d5a54f3] | 481 | }
|
---|
| 482 |
|
---|
[fbcfd458] | 483 | /** Hang up the phone
|
---|
[2d5a54f3] | 484 | *
|
---|
[fbcfd458] | 485 | */
|
---|
| 486 | __native sys_ipc_hangup(int phoneid)
|
---|
| 487 | {
|
---|
| 488 | phone_t *phone;
|
---|
| 489 |
|
---|
| 490 | GET_CHECK_PHONE(phone, phoneid, return ENOENT);
|
---|
| 491 |
|
---|
| 492 | if (ipc_phone_hangup(phone))
|
---|
| 493 | return -1;
|
---|
| 494 |
|
---|
| 495 | return 0;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | /** Wait for incoming ipc call or answer
|
---|
| 499 | *
|
---|
| 500 | * @param calldata Pointer to buffer where the call/answer data is stored
|
---|
[bd5a663] | 501 | * @param usec Timeout. See waitq_sleep_timeout() for explanation.
|
---|
| 502 | * @param nonblocking See waitq_sleep_timeout() for explanation.
|
---|
| 503 | *
|
---|
[2d5a54f3] | 504 | * @return Callid, if callid & 1, then the call is answer
|
---|
| 505 | */
|
---|
[bd5a663] | 506 | __native sys_ipc_wait_for_call(ipc_data_t *calldata, __u32 usec, int nonblocking)
|
---|
[2d5a54f3] | 507 | {
|
---|
| 508 | call_t *call;
|
---|
| 509 |
|
---|
| 510 | restart:
|
---|
[bd5a663] | 511 | call = ipc_wait_for_call(&TASK->answerbox, usec, nonblocking);
|
---|
[9f22213] | 512 | if (!call)
|
---|
| 513 | return 0;
|
---|
[2d5a54f3] | 514 |
|
---|
[5626277] | 515 | if (call->flags & IPC_CALL_NOTIF) {
|
---|
| 516 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
| 517 | STRUCT_TO_USPACE(&calldata->args, &call->data.args);
|
---|
| 518 | ipc_call_free(call);
|
---|
| 519 |
|
---|
| 520 | return ((__native)call) | IPC_CALLID_NOTIFICATION;
|
---|
| 521 | }
|
---|
| 522 |
|
---|
[2d5a54f3] | 523 | if (call->flags & IPC_CALL_ANSWERED) {
|
---|
[7c7aae16] | 524 | process_answer(call);
|
---|
[2d5a54f3] | 525 |
|
---|
| 526 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
[fbcfd458] | 527 |
|
---|
| 528 | atomic_dec(&TASK->active_calls);
|
---|
| 529 |
|
---|
| 530 | if (call->flags & IPC_CALL_DISCARD_ANSWER) {
|
---|
| 531 | ipc_call_free(call);
|
---|
| 532 | goto restart;
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | STRUCT_TO_USPACE(&calldata->args, &call->data.args);
|
---|
[2d5a54f3] | 536 | ipc_call_free(call);
|
---|
| 537 |
|
---|
| 538 | return ((__native)call) | IPC_CALLID_ANSWERED;
|
---|
| 539 | }
|
---|
[fbcfd458] | 540 |
|
---|
[2d5a54f3] | 541 | if (process_request(&TASK->answerbox, call))
|
---|
| 542 | goto restart;
|
---|
[fbcfd458] | 543 |
|
---|
[7c7aae16] | 544 | /* Include phone address('id') of the caller in the request,
|
---|
| 545 | * copy whole call->data, not only call->data.args */
|
---|
[bd55bbb] | 546 | if (STRUCT_TO_USPACE(calldata, &call->data)) {
|
---|
| 547 | return 0;
|
---|
| 548 | }
|
---|
[2d5a54f3] | 549 | return (__native)call;
|
---|
| 550 | }
|
---|
[5626277] | 551 |
|
---|
| 552 | /** Connect irq handler to task */
|
---|
[162f919] | 553 | __native sys_ipc_register_irq(__native irq, irq_code_t *ucode)
|
---|
[5626277] | 554 | {
|
---|
[2bb8648] | 555 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
---|
| 556 | return EPERM;
|
---|
| 557 |
|
---|
[5626277] | 558 | if (irq >= IRQ_COUNT)
|
---|
[2bb8648] | 559 | return (__native) ELIMIT;
|
---|
[5626277] | 560 |
|
---|
| 561 | irq_ipc_bind_arch(irq);
|
---|
[162f919] | 562 |
|
---|
| 563 | return ipc_irq_register(&TASK->answerbox, irq, ucode);
|
---|
[5626277] | 564 | }
|
---|
| 565 |
|
---|
| 566 | /* Disconnect irq handler from task */
|
---|
| 567 | __native sys_ipc_unregister_irq(__native irq)
|
---|
| 568 | {
|
---|
[2bb8648] | 569 | if (!(cap_get(TASK) & CAP_IRQ_REG))
|
---|
| 570 | return EPERM;
|
---|
| 571 |
|
---|
[5626277] | 572 | if (irq >= IRQ_COUNT)
|
---|
[2bb8648] | 573 | return (__native) ELIMIT;
|
---|
[5626277] | 574 |
|
---|
| 575 | ipc_irq_unregister(&TASK->answerbox, irq);
|
---|
| 576 |
|
---|
| 577 | return 0;
|
---|
| 578 | }
|
---|