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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f7cbc6f was fbcdeb8, checked in by Martin Decky <martin@…>, 14 years ago

Remove the two-phase way of creating virtual memory areas (first asking for a mappable address and then mapping it) which was prone to race conditions when two or more calls to as_get_mappable_page() and as_area_create() were interleaved. This for example caused the e1k driver to randomly fail.

The memory area related syscalls and IPC calls have all been altered to accept a special value (void *) -1, representing a demand to atomically search for a mappable address space "hole" and map to it.

Individual changes:

  • IPC_M_SHARE_OUT: the destination address space area is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the callee via a pointer in an IPC reply argument)

  • IPC_M_SHARE_IN: the destination address space ares is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the caller as usual via an IPC argument)

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