source: mainline/kernel/generic/src/ipc/sysipc.c@ 6645a14

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

Make IPC_M_CONNECT_TO_ME more consistent with IPC_M_CONNECT_TO_ME.

  • Instead of passing the task ID of the connecting task in IPC argument 3 and 4, pass it in ipc_call_t::in_task_id.
  • Actually, all answers are signed by the answering task ID like this.
  • Property mode set to 100644
File size: 33.4 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);
[6364d3c]255 /* Set 'phone hash' as arg5 of response */
[38c706cc]256 IPC_SET_ARG5(answer->data,
[96b02eb9]257 (sysarg_t) &TASK->phones[phoneid]);
[2d5a54f3]258 }
[228e490]259 } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
[37c57f2]260 /* If the users accepted call, connect */
[2c0e5d2]261 if (IPC_GET_RETVAL(answer->data) == EOK) {
[b61d47d]262 ipc_phone_connect((phone_t *) IPC_GET_ARG5(*olddata),
[51ec40f]263 &TASK->answerbox);
[37c57f2]264 }
[228e490]265 } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_SHARE_OUT) {
[51ec40f]266 if (!IPC_GET_RETVAL(answer->data)) {
267 /* Accepted, handle as_area receipt */
[8d6bc2d5]268
[da1bafb]269 irq_spinlock_lock(&answer->sender->lock, true);
270 as_t *as = answer->sender->as;
271 irq_spinlock_unlock(&answer->sender->lock, true);
[8d6bc2d5]272
[da1bafb]273 int rc = as_area_share(as, IPC_GET_ARG1(*olddata),
[51ec40f]274 IPC_GET_ARG2(*olddata), AS,
275 IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
[76d7305]276 IPC_SET_RETVAL(answer->data, rc);
277 return rc;
[46fc2f9]278 }
[228e490]279 } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_SHARE_IN) {
[46fc2f9]280 if (!IPC_GET_RETVAL(answer->data)) {
[da1bafb]281 irq_spinlock_lock(&answer->sender->lock, true);
282 as_t *as = answer->sender->as;
283 irq_spinlock_unlock(&answer->sender->lock, true);
[46fc2f9]284
[da1bafb]285 int rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
[51ec40f]286 IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
287 IPC_GET_ARG2(answer->data));
[d6e5cbc]288 IPC_SET_RETVAL(answer->data, rc);
[7c23af9]289 }
[228e490]290 } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_DATA_READ) {
[a55d5f9f]291 ASSERT(!answer->buffer);
292 if (!IPC_GET_RETVAL(answer->data)) {
293 /* The recipient agreed to send data. */
294 uintptr_t src = IPC_GET_ARG1(answer->data);
295 uintptr_t dst = IPC_GET_ARG1(*olddata);
296 size_t max_size = IPC_GET_ARG2(*olddata);
297 size_t size = IPC_GET_ARG2(answer->data);
[1a60feeb]298 if (size && size <= max_size) {
[a55d5f9f]299 /*
300 * Copy the destination VA so that this piece of
301 * information is not lost.
302 */
303 IPC_SET_ARG1(answer->data, dst);
[da1bafb]304
[a55d5f9f]305 answer->buffer = malloc(size, 0);
306 int rc = copy_from_uspace(answer->buffer,
307 (void *) src, size);
308 if (rc) {
309 IPC_SET_RETVAL(answer->data, rc);
310 free(answer->buffer);
311 answer->buffer = NULL;
312 }
[1a60feeb]313 } else if (!size) {
314 IPC_SET_RETVAL(answer->data, EOK);
[a55d5f9f]315 } else {
316 IPC_SET_RETVAL(answer->data, ELIMIT);
317 }
318 }
[228e490]319 } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_DATA_WRITE) {
[654b7db]320 ASSERT(answer->buffer);
[7918fce]321 if (!IPC_GET_RETVAL(answer->data)) {
[a55d5f9f]322 /* The recipient agreed to receive data. */
[da1bafb]323 uintptr_t dst = (uintptr_t)IPC_GET_ARG1(answer->data);
324 size_t size = (size_t)IPC_GET_ARG2(answer->data);
325 size_t max_size = (size_t)IPC_GET_ARG2(*olddata);
326
[a55d5f9f]327 if (size <= max_size) {
[da1bafb]328 int rc = copy_to_uspace((void *) dst,
[a55d5f9f]329 answer->buffer, size);
330 if (rc)
331 IPC_SET_RETVAL(answer->data, rc);
332 } else {
333 IPC_SET_RETVAL(answer->data, ELIMIT);
334 }
[7918fce]335 }
[654b7db]336 free(answer->buffer);
337 answer->buffer = NULL;
[fdd4898]338 } else if (IPC_GET_IMETHOD(*olddata) == IPC_M_STATE_CHANGE_AUTHORIZE) {
339 if (!IPC_GET_RETVAL(answer->data)) {
340 /* The recipient authorized the change of state. */
341 phone_t *recipient_phone;
342 task_t *other_task_s;
343 task_t *other_task_r;
344 int rc;
345
346 rc = phone_get(IPC_GET_ARG1(answer->data),
347 &recipient_phone);
348 if (rc != EOK) {
349 IPC_SET_RETVAL(answer->data, ENOENT);
350 return ENOENT;
351 }
352
353 mutex_lock(&recipient_phone->lock);
354 if (recipient_phone->state != IPC_PHONE_CONNECTED) {
355 mutex_unlock(&recipient_phone->lock);
356 IPC_SET_RETVAL(answer->data, EINVAL);
357 return EINVAL;
358 }
359
360 other_task_r = recipient_phone->callee->task;
361 other_task_s = (task_t *) IPC_GET_ARG5(*olddata);
362
363 /*
364 * See if both the sender and the recipient meant the
365 * same third party task.
366 */
367 if (other_task_r != other_task_s) {
368 IPC_SET_RETVAL(answer->data, EINVAL);
369 rc = EINVAL;
370 } else {
371 rc = event_task_notify_5(other_task_r,
372 EVENT_TASK_STATE_CHANGE, false,
373 IPC_GET_ARG1(*olddata),
374 IPC_GET_ARG2(*olddata),
375 IPC_GET_ARG3(*olddata),
[e2ab36f1]376 LOWER32(olddata->task_id),
377 UPPER32(olddata->task_id));
[fdd4898]378 IPC_SET_RETVAL(answer->data, rc);
379 }
380
381 mutex_unlock(&recipient_phone->lock);
382 return rc;
383 }
[2d5a54f3]384 }
[da1bafb]385
[7c23af9]386 return 0;
[2d5a54f3]387}
388
[bf1fb9f]389static void phones_lock(phone_t *p1, phone_t *p2)
390{
391 if (p1 < p2) {
392 mutex_lock(&p1->lock);
393 mutex_lock(&p2->lock);
394 } else if (p1 > p2) {
395 mutex_lock(&p2->lock);
396 mutex_lock(&p1->lock);
[da1bafb]397 } else
[bf1fb9f]398 mutex_lock(&p1->lock);
399}
400
401static void phones_unlock(phone_t *p1, phone_t *p2)
402{
403 mutex_unlock(&p1->lock);
404 if (p1 != p2)
405 mutex_unlock(&p2->lock);
406}
407
[8b243f2]408/** Called before the request is sent.
409 *
[da1bafb]410 * @param call Call structure with the request.
411 * @param phone Phone that the call will be sent through.
412 *
413 * @return Return 0 on success, ELIMIT or EPERM on error.
[7c7aae16]414 *
415 */
[9a1b20c]416static int request_preprocess(call_t *call, phone_t *phone)
[7c7aae16]417{
[228e490]418 switch (IPC_GET_IMETHOD(call->data)) {
[2c0e5d2]419 case IPC_M_CONNECTION_CLONE: {
420 phone_t *cloned_phone;
[c9fff17]421 if (phone_get(IPC_GET_ARG1(call->data), &cloned_phone) != EOK)
422 return ENOENT;
[da1bafb]423
[bf1fb9f]424 phones_lock(cloned_phone, phone);
[da1bafb]425
[2c0e5d2]426 if ((cloned_phone->state != IPC_PHONE_CONNECTED) ||
427 phone->state != IPC_PHONE_CONNECTED) {
[bf1fb9f]428 phones_unlock(cloned_phone, phone);
[2c0e5d2]429 return EINVAL;
430 }
[da1bafb]431
[2c0e5d2]432 /*
433 * We can be pretty sure now that both tasks exist and we are
434 * connected to them. As we continue to hold the phone locks,
435 * we are effectively preventing them from finishing their
436 * potential cleanup.
[da1bafb]437 *
[2c0e5d2]438 */
[da1bafb]439 int newphid = phone_alloc(phone->callee->task);
[2c0e5d2]440 if (newphid < 0) {
[bf1fb9f]441 phones_unlock(cloned_phone, phone);
[2c0e5d2]442 return ELIMIT;
443 }
[da1bafb]444
[2c0e5d2]445 ipc_phone_connect(&phone->callee->task->phones[newphid],
446 cloned_phone->callee);
[bf1fb9f]447 phones_unlock(cloned_phone, phone);
[da1bafb]448
[2c0e5d2]449 /* Set the new phone for the callee. */
450 IPC_SET_ARG1(call->data, newphid);
451 break;
452 }
453 case IPC_M_CONNECT_ME:
[96b02eb9]454 IPC_SET_ARG5(call->data, (sysarg_t) phone);
[2c0e5d2]455 break;
[da1bafb]456 case IPC_M_CONNECT_ME_TO: {
457 int newphid = phone_alloc(TASK);
[7c7aae16]458 if (newphid < 0)
459 return ELIMIT;
[da1bafb]460
[6364d3c]461 /* Set arg5 for server */
[96b02eb9]462 IPC_SET_ARG5(call->data, (sysarg_t) &TASK->phones[newphid]);
[7c7aae16]463 call->flags |= IPC_CALL_CONN_ME_TO;
[0c1a5d8a]464 call->priv = newphid;
[7c7aae16]465 break;
[da1bafb]466 }
467 case IPC_M_SHARE_OUT: {
468 size_t size = as_area_get_size(IPC_GET_ARG1(call->data));
[8b243f2]469 if (!size)
[7c23af9]470 return EPERM;
[da1bafb]471
[fd4d8c0]472 IPC_SET_ARG2(call->data, size);
[7c23af9]473 break;
[da1bafb]474 }
475 case IPC_M_DATA_READ: {
476 size_t size = IPC_GET_ARG2(call->data);
[f6bffee]477 if (size > DATA_XFER_LIMIT) {
478 int flags = IPC_GET_ARG3(call->data);
479 if (flags & IPC_XF_RESTRICT)
480 IPC_SET_ARG2(call->data, DATA_XFER_LIMIT);
481 else
482 return ELIMIT;
483 }
[a55d5f9f]484 break;
[da1bafb]485 }
486 case IPC_M_DATA_WRITE: {
487 uintptr_t src = IPC_GET_ARG1(call->data);
488 size_t size = IPC_GET_ARG2(call->data);
[7918fce]489
[f6bffee]490 if (size > DATA_XFER_LIMIT) {
491 int flags = IPC_GET_ARG3(call->data);
492 if (flags & IPC_XF_RESTRICT) {
493 size = DATA_XFER_LIMIT;
494 IPC_SET_ARG2(call->data, size);
495 } else
496 return ELIMIT;
497 }
[7918fce]498
499 call->buffer = (uint8_t *) malloc(size, 0);
[da1bafb]500 int rc = copy_from_uspace(call->buffer, (void *) src, size);
[7918fce]501 if (rc != 0) {
502 free(call->buffer);
503 return rc;
504 }
[da1bafb]505
[7918fce]506 break;
[da1bafb]507 }
[fdd4898]508 case IPC_M_STATE_CHANGE_AUTHORIZE: {
509 phone_t *sender_phone;
510 task_t *other_task_s;
511
512 if (phone_get(IPC_GET_ARG5(call->data), &sender_phone) != EOK)
513 return ENOENT;
514
515 mutex_lock(&sender_phone->lock);
516 if (sender_phone->state != IPC_PHONE_CONNECTED) {
517 mutex_unlock(&sender_phone->lock);
518 return EINVAL;
519 }
520
521 other_task_s = sender_phone->callee->task;
522
523 mutex_unlock(&sender_phone->lock);
524
525 /* Remember the third party task hash. */
526 IPC_SET_ARG5(call->data, (sysarg_t) other_task_s);
527 break;
528 }
[9a1b20c]529#ifdef CONFIG_UDEBUG
[79ae36dd]530 case IPC_M_DEBUG:
[9a1b20c]531 return udebug_request_preprocess(call, phone);
532#endif
[7c7aae16]533 default:
534 break;
535 }
[da1bafb]536
[7c7aae16]537 return 0;
538}
539
[8b243f2]540/*******************************************************************************
541 * Functions called to process received call/answer before passing it to uspace.
542 *******************************************************************************/
[2d5a54f3]543
[8b243f2]544/** Do basic kernel processing of received call answer.
545 *
[da1bafb]546 * @param call Call structure with the answer.
547 *
[8b243f2]548 */
[7c7aae16]549static void process_answer(call_t *call)
[2d5a54f3]550{
[6c441cf8]551 if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
[51ec40f]552 (call->flags & IPC_CALL_FORWARDED))
[9f22213]553 IPC_SET_RETVAL(call->data, EFORWARD);
[da1bafb]554
[7c7aae16]555 if (call->flags & IPC_CALL_CONN_ME_TO) {
556 if (IPC_GET_RETVAL(call->data))
[0c1a5d8a]557 phone_dealloc(call->priv);
[7c7aae16]558 else
[b61d47d]559 IPC_SET_ARG5(call->data, call->priv);
[7c7aae16]560 }
[da1bafb]561
[a55d5f9f]562 if (call->buffer) {
[da1bafb]563 /*
564 * This must be an affirmative answer to IPC_M_DATA_READ
[79ae36dd]565 * or IPC_M_DEBUG/UDEBUG_M_MEM_READ...
[da1bafb]566 *
567 */
[a55d5f9f]568 uintptr_t dst = IPC_GET_ARG1(call->data);
569 size_t size = IPC_GET_ARG2(call->data);
570 int rc = copy_to_uspace((void *) dst, call->buffer, size);
571 if (rc)
572 IPC_SET_RETVAL(call->data, rc);
573 free(call->buffer);
574 call->buffer = NULL;
575 }
[2d5a54f3]576}
577
[8b243f2]578/** Do basic kernel processing of received call request.
579 *
[da1bafb]580 * @param box Destination answerbox structure.
581 * @param call Call structure with the request.
582 *
583 * @return 0 if the call should be passed to userspace.
584 * @return -1 if the call should be ignored.
[2d5a54f3]585 *
586 */
[51ec40f]587static int process_request(answerbox_t *box, call_t *call)
[2d5a54f3]588{
[228e490]589 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME) {
[da1bafb]590 int phoneid = phone_alloc(TASK);
[2d5a54f3]591 if (phoneid < 0) { /* Failed to allocate phone */
592 IPC_SET_RETVAL(call->data, ELIMIT);
[8b243f2]593 ipc_answer(box, call);
[2d5a54f3]594 return -1;
595 }
[da1bafb]596
[38c706cc]597 IPC_SET_ARG5(call->data, phoneid);
[9a1b20c]598 }
[da1bafb]599
[228e490]600 switch (IPC_GET_IMETHOD(call->data)) {
[79ae36dd]601 case IPC_M_DEBUG:
[9a1b20c]602 return -1;
603 default:
604 break;
605 }
[da1bafb]606
[2d5a54f3]607 return 0;
608}
609
[8b243f2]610/** Make a fast call over IPC, wait for reply and return to user.
611 *
[2e51969]612 * This function can handle only three arguments of payload, but is faster than
613 * the generic function (i.e. sys_ipc_call_sync_slow()).
[2d5a54f3]614 *
[da1bafb]615 * @param phoneid Phone handle for the call.
[228e490]616 * @param imethod Interface and method of the call.
[da1bafb]617 * @param arg1 Service-defined payload argument.
618 * @param arg2 Service-defined payload argument.
619 * @param arg3 Service-defined payload argument.
[228e490]620 * @param data Address of user-space structure where the reply call will
[da1bafb]621 * be stored.
622 *
623 * @return 0 on success.
624 * @return ENOENT if there is no such phone handle.
[8b243f2]625 *
[2d5a54f3]626 */
[228e490]627sysarg_t sys_ipc_call_sync_fast(sysarg_t phoneid, sysarg_t imethod,
[96b02eb9]628 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, ipc_data_t *data)
[2d5a54f3]629{
630 phone_t *phone;
[c9fff17]631 if (phone_get(phoneid, &phone) != EOK)
632 return ENOENT;
[228e490]633
[da1bafb]634 call_t *call = ipc_call_alloc(0);
[228e490]635 IPC_SET_IMETHOD(call->data, imethod);
[35bb2e7]636 IPC_SET_ARG1(call->data, arg1);
637 IPC_SET_ARG2(call->data, arg2);
638 IPC_SET_ARG3(call->data, arg3);
[da1bafb]639
[8498915]640 /*
641 * To achieve deterministic behavior, zero out arguments that are beyond
642 * the limits of the fast version.
643 */
[35bb2e7]644 IPC_SET_ARG4(call->data, 0);
645 IPC_SET_ARG5(call->data, 0);
[da1bafb]646
647 int res = request_preprocess(call, phone);
648 int rc;
649
650 if (!res) {
[741fd16]651#ifdef CONFIG_UDEBUG
652 udebug_stoppable_begin();
653#endif
[35bb2e7]654 rc = ipc_call_sync(phone, call);
[741fd16]655#ifdef CONFIG_UDEBUG
656 udebug_stoppable_end();
657#endif
[da1bafb]658
[35bb2e7]659 if (rc != EOK) {
[33adc6ce]660 /* The call will be freed by ipc_cleanup(). */
[79872cd]661 return rc;
[35bb2e7]662 }
[da1bafb]663
[35bb2e7]664 process_answer(call);
[da1bafb]665 } else
[35bb2e7]666 IPC_SET_RETVAL(call->data, res);
[da1bafb]667
[35bb2e7]668 rc = STRUCT_TO_USPACE(&data->args, &call->data.args);
669 ipc_call_free(call);
[bc50fc42]670 if (rc != 0)
671 return rc;
[da1bafb]672
[2d5a54f3]673 return 0;
674}
675
[8b243f2]676/** Make a synchronous IPC call allowing to transmit the entire payload.
677 *
[228e490]678 * @param phoneid Phone handle for the call.
679 * @param request User-space address of call data with the request.
680 * @param reply User-space address of call data where to store the
681 * answer.
[da1bafb]682 *
683 * @return Zero on success or an error code.
[8b243f2]684 *
685 */
[228e490]686sysarg_t sys_ipc_call_sync_slow(sysarg_t phoneid, ipc_data_t *request,
[51ec40f]687 ipc_data_t *reply)
[2d5a54f3]688{
689 phone_t *phone;
[c9fff17]690 if (phone_get(phoneid, &phone) != EOK)
691 return ENOENT;
[228e490]692
[da1bafb]693 call_t *call = ipc_call_alloc(0);
[228e490]694 int rc = copy_from_uspace(&call->data.args, &request->args,
[35bb2e7]695 sizeof(call->data.args));
696 if (rc != 0) {
697 ipc_call_free(call);
[96b02eb9]698 return (sysarg_t) rc;
[35bb2e7]699 }
[da1bafb]700
701 int res = request_preprocess(call, phone);
702
703 if (!res) {
[741fd16]704#ifdef CONFIG_UDEBUG
705 udebug_stoppable_begin();
706#endif
[35bb2e7]707 rc = ipc_call_sync(phone, call);
[741fd16]708#ifdef CONFIG_UDEBUG
709 udebug_stoppable_end();
710#endif
[da1bafb]711
[35bb2e7]712 if (rc != EOK) {
[33adc6ce]713 /* The call will be freed by ipc_cleanup(). */
[79872cd]714 return rc;
[35bb2e7]715 }
[da1bafb]716
[35bb2e7]717 process_answer(call);
[da1bafb]718 } else
[35bb2e7]719 IPC_SET_RETVAL(call->data, res);
[da1bafb]720
[35bb2e7]721 rc = STRUCT_TO_USPACE(&reply->args, &call->data.args);
722 ipc_call_free(call);
[e3c762cd]723 if (rc != 0)
724 return rc;
[da1bafb]725
[2d5a54f3]726 return 0;
727}
728
[97d17fe]729/** Check that the task did not exceed the allowed limit of asynchronous calls
730 * made over a phone.
[2d5a54f3]731 *
[228e490]732 * @param phone Phone to check the limit against.
733 *
[da1bafb]734 * @return 0 if limit not reached or -1 if limit exceeded.
735 *
[2d5a54f3]736 */
[97d17fe]737static int check_call_limit(phone_t *phone)
[2d5a54f3]738{
[97d17fe]739 if (atomic_get(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
[2d5a54f3]740 return -1;
[da1bafb]741
[2d5a54f3]742 return 0;
743}
744
[8b243f2]745/** Make a fast asynchronous call over IPC.
[2d5a54f3]746 *
[3209923]747 * This function can only handle four arguments of payload, but is faster than
748 * the generic function sys_ipc_call_async_slow().
[8b243f2]749 *
[da1bafb]750 * @param phoneid Phone handle for the call.
[228e490]751 * @param imethod Interface and method of the call.
[da1bafb]752 * @param arg1 Service-defined payload argument.
753 * @param arg2 Service-defined payload argument.
754 * @param arg3 Service-defined payload argument.
755 * @param arg4 Service-defined payload argument.
756 *
757 * @return Call hash on success.
758 * @return IPC_CALLRET_FATAL in case of a fatal error.
759 * @return IPC_CALLRET_TEMPORARY if there are too many pending
760 * asynchronous requests; answers should be handled first.
761 *
[2d5a54f3]762 */
[228e490]763sysarg_t sys_ipc_call_async_fast(sysarg_t phoneid, sysarg_t imethod,
[96b02eb9]764 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]765{
[da1bafb]766 phone_t *phone;
[c9fff17]767 if (phone_get(phoneid, &phone) != EOK)
768 return IPC_CALLRET_FATAL;
[228e490]769
[97d17fe]770 if (check_call_limit(phone))
771 return IPC_CALLRET_TEMPORARY;
[da1bafb]772
773 call_t *call = ipc_call_alloc(0);
[228e490]774 IPC_SET_IMETHOD(call->data, imethod);
[2d5a54f3]775 IPC_SET_ARG1(call->data, arg1);
776 IPC_SET_ARG2(call->data, arg2);
[3209923]777 IPC_SET_ARG3(call->data, arg3);
778 IPC_SET_ARG4(call->data, arg4);
[da1bafb]779
[8498915]780 /*
781 * To achieve deterministic behavior, zero out arguments that are beyond
782 * the limits of the fast version.
783 */
784 IPC_SET_ARG5(call->data, 0);
[da1bafb]785
786 int res = request_preprocess(call, phone);
787
788 if (!res)
[7c7aae16]789 ipc_call(phone, call);
790 else
791 ipc_backsend_err(phone, call, res);
[da1bafb]792
[96b02eb9]793 return (sysarg_t) call;
[2d5a54f3]794}
795
[8b243f2]796/** Make an asynchronous IPC call allowing to transmit the entire payload.
797 *
[da1bafb]798 * @param phoneid Phone handle for the call.
799 * @param data Userspace address of call data with the request.
800 *
801 * @return See sys_ipc_call_async_fast().
[2d5a54f3]802 *
803 */
[96b02eb9]804sysarg_t sys_ipc_call_async_slow(sysarg_t phoneid, ipc_data_t *data)
[2d5a54f3]805{
[da1bafb]806 phone_t *phone;
[c9fff17]807 if (phone_get(phoneid, &phone) != EOK)
808 return IPC_CALLRET_FATAL;
[4e49572]809
[97d17fe]810 if (check_call_limit(phone))
811 return IPC_CALLRET_TEMPORARY;
812
[da1bafb]813 call_t *call = ipc_call_alloc(0);
814 int rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]815 sizeof(call->data.args));
[f58af46]816 if (rc != 0) {
817 ipc_call_free(call);
[96b02eb9]818 return (sysarg_t) rc;
[f58af46]819 }
[da1bafb]820
821 int res = request_preprocess(call, phone);
822
823 if (!res)
[7c7aae16]824 ipc_call(phone, call);
825 else
826 ipc_backsend_err(phone, call, res);
[da1bafb]827
[96b02eb9]828 return (sysarg_t) call;
[2d5a54f3]829}
830
[da1bafb]831/** Forward a received call to another destination
832 *
833 * Common code for both the fast and the slow version.
834 *
835 * @param callid Hash of the call to forward.
836 * @param phoneid Phone handle to use for forwarding.
[228e490]837 * @param imethod New interface and method to use for the forwarded call.
[da1bafb]838 * @param arg1 New value of the first argument for the forwarded call.
839 * @param arg2 New value of the second argument for the forwarded call.
840 * @param arg3 New value of the third argument for the forwarded call.
841 * @param arg4 New value of the fourth argument for the forwarded call.
842 * @param arg5 New value of the fifth argument for the forwarded call.
843 * @param mode Flags that specify mode of the forward operation.
844 * @param slow If true, arg3, arg4 and arg5 are considered. Otherwise
845 * the function considers only the fast version arguments:
846 * i.e. arg1 and arg2.
847 *
848 * @return 0 on succes, otherwise an error code.
849 *
850 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
851 *
[2ba7810]852 */
[96b02eb9]853static sysarg_t sys_ipc_forward_common(sysarg_t callid, sysarg_t phoneid,
[228e490]854 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
[96b02eb9]855 sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
[2ba7810]856{
[da1bafb]857 call_t *call = get_call(callid);
[2ba7810]858 if (!call)
859 return ENOENT;
[48daf64]860
[9f22213]861 call->flags |= IPC_CALL_FORWARDED;
[da1bafb]862
863 phone_t *phone;
[c9fff17]864 if (phone_get(phoneid, &phone) != EOK) {
[2ba7810]865 IPC_SET_RETVAL(call->data, EFORWARD);
866 ipc_answer(&TASK->answerbox, call);
867 return ENOENT;
[c9fff17]868 }
[da1bafb]869
[228e490]870 if (!method_is_forwardable(IPC_GET_IMETHOD(call->data))) {
[2ba7810]871 IPC_SET_RETVAL(call->data, EFORWARD);
872 ipc_answer(&TASK->answerbox, call);
873 return EPERM;
874 }
[da1bafb]875
[0dc4258]876 /*
[228e490]877 * Userspace is not allowed to change interface and method of system
878 * methods on forward, allow changing ARG1, ARG2, ARG3 and ARG4 by
879 * means of method, arg1, arg2 and arg3.
880 * If the interface and method is immutable, don't change anything.
[2ba7810]881 */
[228e490]882 if (!method_is_immutable(IPC_GET_IMETHOD(call->data))) {
883 if (method_is_system(IPC_GET_IMETHOD(call->data))) {
884 if (IPC_GET_IMETHOD(call->data) == IPC_M_CONNECT_TO_ME)
[b61d47d]885 phone_dealloc(IPC_GET_ARG5(call->data));
[da1bafb]886
[228e490]887 IPC_SET_ARG1(call->data, imethod);
[0dc4258]888 IPC_SET_ARG2(call->data, arg1);
[b61d47d]889 IPC_SET_ARG3(call->data, arg2);
[da1bafb]890
[48daf64]891 if (slow) {
892 IPC_SET_ARG4(call->data, arg3);
893 /*
894 * For system methods we deliberately don't
895 * overwrite ARG5.
896 */
897 }
[0dc4258]898 } else {
[228e490]899 IPC_SET_IMETHOD(call->data, imethod);
[0dc4258]900 IPC_SET_ARG1(call->data, arg1);
[b61d47d]901 IPC_SET_ARG2(call->data, arg2);
[48daf64]902 if (slow) {
903 IPC_SET_ARG3(call->data, arg3);
904 IPC_SET_ARG4(call->data, arg4);
905 IPC_SET_ARG5(call->data, arg5);
906 }
[0dc4258]907 }
[2ba7810]908 }
[da1bafb]909
[d40a8ff]910 return ipc_forward(call, phone, &TASK->answerbox, mode);
[2ba7810]911}
912
[48daf64]913/** Forward a received call to another destination - fast version.
914 *
[228e490]915 * In case the original interface and method is a system method, ARG1, ARG2
916 * and ARG3 are overwritten in the forwarded message with the new method and
917 * the new arg1 and arg2, respectively. Otherwise the IMETHOD, ARG1 and ARG2
918 * are rewritten with the new interface and method, arg1 and arg2, respectively.
919 * Also note there is a set of immutable methods, for which the new method and
920 * arguments are not set and these values are ignored.
[da1bafb]921 *
922 * @param callid Hash of the call to forward.
923 * @param phoneid Phone handle to use for forwarding.
[228e490]924 * @param imethod New interface and method to use for the forwarded call.
[da1bafb]925 * @param arg1 New value of the first argument for the forwarded call.
926 * @param arg2 New value of the second argument for the forwarded call.
927 * @param mode Flags that specify mode of the forward operation.
928 *
929 * @return 0 on succes, otherwise an error code.
930 *
[48daf64]931 */
[96b02eb9]932sysarg_t sys_ipc_forward_fast(sysarg_t callid, sysarg_t phoneid,
[228e490]933 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[48daf64]934{
[228e490]935 return sys_ipc_forward_common(callid, phoneid, imethod, arg1, arg2, 0, 0,
[48daf64]936 0, mode, false);
937}
938
939/** Forward a received call to another destination - slow version.
940 *
941 * This function is the slow verision of the sys_ipc_forward_fast interface.
[228e490]942 * It can copy all five new arguments and the new interface and method from
943 * the userspace. It naturally extends the functionality of the fast version.
944 * For system methods, it additionally stores the new value of arg3 to ARG4.
945 * For non-system methods, it additionally stores the new value of arg3, arg4
946 * and arg5, respectively, to ARG3, ARG4 and ARG5, respectively.
[da1bafb]947 *
948 * @param callid Hash of the call to forward.
949 * @param phoneid Phone handle to use for forwarding.
950 * @param data Userspace address of the new IPC data.
951 * @param mode Flags that specify mode of the forward operation.
952 *
953 * @return 0 on succes, otherwise an error code.
954 *
[48daf64]955 */
[96b02eb9]956sysarg_t sys_ipc_forward_slow(sysarg_t callid, sysarg_t phoneid,
[da1bafb]957 ipc_data_t *data, unsigned int mode)
[48daf64]958{
959 ipc_data_t newdata;
[da1bafb]960 int rc = copy_from_uspace(&newdata.args, &data->args,
[48daf64]961 sizeof(newdata.args));
[da1bafb]962 if (rc != 0)
[96b02eb9]963 return (sysarg_t) rc;
[da1bafb]964
[48daf64]965 return sys_ipc_forward_common(callid, phoneid,
[228e490]966 IPC_GET_IMETHOD(newdata), IPC_GET_ARG1(newdata),
[48daf64]967 IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
968 IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
969}
970
[8b243f2]971/** Answer an IPC call - fast version.
972 *
973 * This function can handle only two return arguments of payload, but is faster
974 * than the generic sys_ipc_answer().
975 *
[da1bafb]976 * @param callid Hash of the call to be answered.
977 * @param retval Return value of the answer.
978 * @param arg1 Service-defined return value.
979 * @param arg2 Service-defined return value.
980 * @param arg3 Service-defined return value.
981 * @param arg4 Service-defined return value.
982 *
983 * @return 0 on success, otherwise an error code.
[8b243f2]984 *
985 */
[96b02eb9]986sysarg_t sys_ipc_answer_fast(sysarg_t callid, sysarg_t retval,
987 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[2d5a54f3]988{
[897f2e76]989 /* Do not answer notification callids */
990 if (callid & IPC_CALLID_NOTIFICATION)
991 return 0;
[da1bafb]992
993 call_t *call = get_call(callid);
[2ba7810]994 if (!call)
995 return ENOENT;
[da1bafb]996
997 ipc_data_t saved_data;
998 bool saved;
999
[9f22213]1000 if (answer_need_old(call)) {
[2ba7810]1001 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]1002 saved = true;
1003 } else
1004 saved = false;
1005
[2d5a54f3]1006 IPC_SET_RETVAL(call->data, retval);
1007 IPC_SET_ARG1(call->data, arg1);
1008 IPC_SET_ARG2(call->data, arg2);
[b74959bd]1009 IPC_SET_ARG3(call->data, arg3);
1010 IPC_SET_ARG4(call->data, arg4);
[da1bafb]1011
[8498915]1012 /*
1013 * To achieve deterministic behavior, zero out arguments that are beyond
1014 * the limits of the fast version.
1015 */
1016 IPC_SET_ARG5(call->data, 0);
[da1bafb]1017 int rc = answer_preprocess(call, saved ? &saved_data : NULL);
1018
[2d5a54f3]1019 ipc_answer(&TASK->answerbox, call);
[7c23af9]1020 return rc;
[2d5a54f3]1021}
1022
[8b243f2]1023/** Answer an IPC call.
1024 *
[da1bafb]1025 * @param callid Hash of the call to be answered.
1026 * @param data Userspace address of call data with the answer.
1027 *
1028 * @return 0 on success, otherwise an error code.
[8b243f2]1029 *
1030 */
[96b02eb9]1031sysarg_t sys_ipc_answer_slow(sysarg_t callid, ipc_data_t *data)
[2d5a54f3]1032{
[897f2e76]1033 /* Do not answer notification callids */
1034 if (callid & IPC_CALLID_NOTIFICATION)
1035 return 0;
[da1bafb]1036
1037 call_t *call = get_call(callid);
[2ba7810]1038 if (!call)
1039 return ENOENT;
[da1bafb]1040
1041 ipc_data_t saved_data;
1042 bool saved;
1043
[9f22213]1044 if (answer_need_old(call)) {
[2ba7810]1045 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]1046 saved = true;
1047 } else
1048 saved = false;
1049
1050 int rc = copy_from_uspace(&call->data.args, &data->args,
[51ec40f]1051 sizeof(call->data.args));
[e3c762cd]1052 if (rc != 0)
1053 return rc;
[da1bafb]1054
1055 rc = answer_preprocess(call, saved ? &saved_data : NULL);
[2d5a54f3]1056
1057 ipc_answer(&TASK->answerbox, call);
[7c23af9]1058 return rc;
[2d5a54f3]1059}
1060
[8b243f2]1061/** Hang up a phone.
[2d5a54f3]1062 *
[da1bafb]1063 * @param Phone handle of the phone to be hung up.
1064 *
1065 * @return 0 on success or an error code.
[8b243f2]1066 *
[fbcfd458]1067 */
[96b02eb9]1068sysarg_t sys_ipc_hangup(sysarg_t phoneid)
[fbcfd458]1069{
1070 phone_t *phone;
[da1bafb]1071
[c9fff17]1072 if (phone_get(phoneid, &phone) != EOK)
1073 return ENOENT;
[da1bafb]1074
[d8f7362]1075 if (ipc_phone_hangup(phone))
[fbcfd458]1076 return -1;
[da1bafb]1077
[fbcfd458]1078 return 0;
1079}
1080
[8b243f2]1081/** Wait for an incoming IPC call or an answer.
[fbcfd458]1082 *
[da1bafb]1083 * @param calldata Pointer to buffer where the call/answer data is stored.
1084 * @param usec Timeout. See waitq_sleep_timeout() for explanation.
1085 * @param flags Select mode of sleep operation. See waitq_sleep_timeout()
1086 * for explanation.
1087 *
1088 * @return Hash of the call.
1089 * If IPC_CALLID_NOTIFICATION bit is set in the hash, the
1090 * call is a notification. IPC_CALLID_ANSWERED denotes an
1091 * answer.
[bd5a663]1092 *
[2d5a54f3]1093 */
[96b02eb9]1094sysarg_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec,
[da1bafb]1095 unsigned int flags)
[2d5a54f3]1096{
1097 call_t *call;
[da1bafb]1098
[741fd16]1099restart:
[da1bafb]1100
[741fd16]1101#ifdef CONFIG_UDEBUG
1102 udebug_stoppable_begin();
[da1bafb]1103#endif
1104
[51ec40f]1105 call = ipc_wait_for_call(&TASK->answerbox, usec,
1106 flags | SYNCH_FLAGS_INTERRUPTIBLE);
[da1bafb]1107
[741fd16]1108#ifdef CONFIG_UDEBUG
1109 udebug_stoppable_end();
1110#endif
[da1bafb]1111
[9f22213]1112 if (!call)
1113 return 0;
[da1bafb]1114
[5626277]1115 if (call->flags & IPC_CALL_NOTIF) {
[43752b6]1116 /* Set in_phone_hash to the interrupt counter */
[0c1a5d8a]1117 call->data.phone = (void *) call->priv;
[43752b6]1118
1119 STRUCT_TO_USPACE(calldata, &call->data);
[da1bafb]1120
[5626277]1121 ipc_call_free(call);
1122
[96b02eb9]1123 return ((sysarg_t) call) | IPC_CALLID_NOTIFICATION;
[5626277]1124 }
[da1bafb]1125
[2d5a54f3]1126 if (call->flags & IPC_CALL_ANSWERED) {
[7c7aae16]1127 process_answer(call);
[da1bafb]1128
[fbcfd458]1129 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
1130 ipc_call_free(call);
1131 goto restart;
1132 }
[da1bafb]1133
[fbcfd458]1134 STRUCT_TO_USPACE(&calldata->args, &call->data.args);
[2d5a54f3]1135 ipc_call_free(call);
[da1bafb]1136
[96b02eb9]1137 return ((sysarg_t) call) | IPC_CALLID_ANSWERED;
[2d5a54f3]1138 }
[da1bafb]1139
[2d5a54f3]1140 if (process_request(&TASK->answerbox, call))
1141 goto restart;
[da1bafb]1142
[7c7aae16]1143 /* Include phone address('id') of the caller in the request,
1144 * copy whole call->data, not only call->data.args */
[bd55bbb]1145 if (STRUCT_TO_USPACE(calldata, &call->data)) {
[e06da7e]1146 /*
1147 * The callee will not receive this call and no one else has
1148 * a chance to answer it. Reply with the EPARTY error code.
[b11ee88]1149 */
[e06da7e]1150 ipc_data_t saved_data;
[da1bafb]1151 bool saved;
1152
[e06da7e]1153 if (answer_need_old(call)) {
1154 memcpy(&saved_data, &call->data, sizeof(call->data));
[da1bafb]1155 saved = true;
1156 } else
1157 saved = false;
[e06da7e]1158
1159 IPC_SET_RETVAL(call->data, EPARTY);
[da1bafb]1160 (void) answer_preprocess(call, saved ? &saved_data : NULL);
[e06da7e]1161 ipc_answer(&TASK->answerbox, call);
[bd55bbb]1162 return 0;
1163 }
[da1bafb]1164
[96b02eb9]1165 return (sysarg_t) call;
[2d5a54f3]1166}
[5626277]1167
[da1bafb]1168/** Interrupt one thread from sys_ipc_wait_for_call().
1169 *
1170 */
[96b02eb9]1171sysarg_t sys_ipc_poke(void)
[057d21a]1172{
[da1bafb]1173 waitq_unsleep(&TASK->answerbox.wq);
[057d21a]1174 return EOK;
1175}
1176
[8b243f2]1177/** Connect an IRQ handler to a task.
[2b017ba]1178 *
[228e490]1179 * @param inr IRQ number.
1180 * @param devno Device number.
1181 * @param imethod Interface and method to be associated with the notification.
1182 * @param ucode Uspace pointer to the top-half pseudocode.
[da1bafb]1183 *
1184 * @return EPERM or a return code returned by ipc_irq_register().
[2b017ba]1185 *
1186 */
[8add9ca5]1187sysarg_t sys_register_irq(inr_t inr, devno_t devno, sysarg_t imethod,
[51ec40f]1188 irq_code_t *ucode)
[5626277]1189{
[2bb8648]1190 if (!(cap_get(TASK) & CAP_IRQ_REG))
1191 return EPERM;
[da1bafb]1192
[228e490]1193 return ipc_irq_register(&TASK->answerbox, inr, devno, imethod, ucode);
[5626277]1194}
1195
[8b243f2]1196/** Disconnect an IRQ handler from a task.
1197 *
[da1bafb]1198 * @param inr IRQ number.
1199 * @param devno Device number.
1200 *
1201 * @return Zero on success or EPERM on error.
[2b017ba]1202 *
1203 */
[8add9ca5]1204sysarg_t sys_unregister_irq(inr_t inr, devno_t devno)
[5626277]1205{
[2bb8648]1206 if (!(cap_get(TASK) & CAP_IRQ_REG))
1207 return EPERM;
[da1bafb]1208
[2b017ba]1209 ipc_irq_unregister(&TASK->answerbox, inr, devno);
[da1bafb]1210
[5626277]1211 return 0;
1212}
[b45c443]1213
[6b10dab]1214#ifdef __32_BITS__
[9a1b20c]1215
[6b10dab]1216/** Syscall connect to a task by ID (32 bits)
[da1bafb]1217 *
1218 * @return Phone id on success, or negative error code.
[9a1b20c]1219 *
1220 */
[6b10dab]1221sysarg_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid)
[9a1b20c]1222{
1223#ifdef CONFIG_UDEBUG
[6b10dab]1224 sysarg64_t taskid;
1225 int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(sysarg64_t));
[9a1b20c]1226 if (rc != 0)
[96b02eb9]1227 return (sysarg_t) rc;
[da1bafb]1228
[6b10dab]1229 return ipc_connect_kbox((task_id_t) taskid);
[9a1b20c]1230#else
[96b02eb9]1231 return (sysarg_t) ENOTSUP;
[9a1b20c]1232#endif
1233}
1234
[6b10dab]1235#endif /* __32_BITS__ */
1236
1237#ifdef __64_BITS__
1238
1239/** Syscall connect to a task by ID (64 bits)
1240 *
1241 * @return Phone id on success, or negative error code.
1242 *
1243 */
1244sysarg_t sys_ipc_connect_kbox(sysarg_t taskid)
1245{
1246#ifdef CONFIG_UDEBUG
1247 return ipc_connect_kbox((task_id_t) taskid);
1248#else
1249 return (sysarg_t) ENOTSUP;
1250#endif
1251}
1252
1253#endif /* __64_BITS__ */
1254
[cc73a8a1]1255/** @}
[b45c443]1256 */
Note: See TracBrowser for help on using the repository browser.