source: mainline/kernel/generic/src/ipc/sysipc.c@ 36b16bc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 36b16bc was 36b16bc, checked in by Jakub Jermar <jakub@…>, 14 years ago

Fix leftover use of task hash in async_connect_to_me().

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