Subversion Repositories HelenOS

Rev

Rev 2471 | Rev 2556 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1027 palkovsky 1
/*
2071 jermar 2
 * Copyright (c) 2006 Ondrej Palkovsky
1027 palkovsky 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1757 jermar 29
/** @addtogroup genericipc
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
1027 palkovsky 35
#include <arch.h>
36
#include <proc/task.h>
1288 jermar 37
#include <proc/thread.h>
1027 palkovsky 38
#include <errno.h>
39
#include <memstr.h>
40
#include <debug.h>
41
#include <ipc/ipc.h>
42
#include <ipc/sysipc.h>
1281 palkovsky 43
#include <ipc/irq.h>
1072 palkovsky 44
#include <ipc/ipcrsc.h>
1258 palkovsky 45
#include <arch/interrupt.h>
1027 palkovsky 46
#include <print.h>
1288 jermar 47
#include <syscall/copy.h>
1297 jermar 48
#include <security/cap.h>
1329 palkovsky 49
#include <mm/as.h>
1875 jermar 50
#include <print.h>
1027 palkovsky 51
 
2494 jermar 52
/** Maximum buffer size allowed for IPC_M_DATA_SEND requests. */
53
#define DATA_SEND_LIMIT     (64 * 1024)
54
 
2471 jermar 55
#define GET_CHECK_PHONE(phone, phoneid, err) \
56
{ \
57
    if (phoneid > IPC_MAX_PHONES) { \
58
        err; \
59
    } \
60
    phone = &TASK->phones[phoneid]; \
1084 palkovsky 61
}
62
 
2471 jermar 63
#define STRUCT_TO_USPACE(dst, src)  copy_to_uspace(dst, src, sizeof(*(src)))
1084 palkovsky 64
 
2471 jermar 65
/** Decide if the method is a system method.
66
 *
67
 * @param method    Method to be decided.
68
 *
69
 * @return      Return 1 if the method is a system method.
70
 *          Otherwise return 0.
71
 */
1780 jermar 72
static inline int is_system_method(unative_t method)
1040 palkovsky 73
{
74
    if (method <= IPC_M_LAST_SYSTEM)
75
        return 1;
76
    return 0;
77
}
78
 
2471 jermar 79
/** Decide if the message with this method is forwardable.
1040 palkovsky 80
 *
81
 * - some system messages may be forwarded, for some of them
82
 *   it is useless
2471 jermar 83
 *
84
 * @param method    Method to be decided.
85
 *
86
 * @return      Return 1 if the method is forwardable.
87
 *          Otherwise return 0.
1040 palkovsky 88
 */
1780 jermar 89
static inline int is_forwardable(unative_t method)
1040 palkovsky 90
{
2494 jermar 91
    switch (method) {
92
    case IPC_M_PHONE_HUNGUP:
93
    case IPC_M_AS_AREA_SEND:
94
    case IPC_M_AS_AREA_RECV:
95
    case IPC_M_DATA_SEND:
96
        /* This message is meant only for the original recipient. */
97
        return 0;
98
    default:
99
        return 1;
100
    }
1040 palkovsky 101
}
102
 
1027 palkovsky 103
 
2471 jermar 104
/***********************************************************************
105
 * Functions that preprocess answer before sending it to the recepient.
106
 ***********************************************************************/
107
 
108
/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
109
 * for answer_preprocess().
110
 *
111
 * @param call      Call structure to be decided.
112
 *
113
 * @return      Return 1 if the old call contents should be saved.
114
 *          Return 0 otherwise.
1027 palkovsky 115
 */
1088 palkovsky 116
static inline int answer_need_old(call_t *call)
1027 palkovsky 117
{
2494 jermar 118
    switch (IPC_GET_METHOD(call->data)) {
119
    case IPC_M_CONNECT_TO_ME:
120
    case IPC_M_CONNECT_ME_TO:
121
    case IPC_M_AS_AREA_SEND:
122
    case IPC_M_AS_AREA_RECV:
123
    case IPC_M_DATA_SEND:
1027 palkovsky 124
        return 1;
2494 jermar 125
    default:
126
        return 0;
127
    }
1027 palkovsky 128
}
129
 
2471 jermar 130
/** Interpret process answer as control information.
1428 palkovsky 131
 *
2471 jermar 132
 * This function is called directly after sys_ipc_answer().
133
 *
134
 * @param answer    Call structure with the answer.
135
 * @param olddata   Saved data of the request.
136
 *
137
 * @return      Return 0 on success or an error code.
1428 palkovsky 138
 */
1329 palkovsky 139
static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
1027 palkovsky 140
{
141
    int phoneid;
142
 
1088 palkovsky 143
    if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
1141 palkovsky 144
        /* In case of forward, hangup the forwared phone,
145
         * not the originator
146
         */
147
        spinlock_lock(&answer->data.phone->lock);
148
        spinlock_lock(&TASK->answerbox.lock);
1568 palkovsky 149
        if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
1573 palkovsky 150
            list_remove(&answer->data.phone->link);
1568 palkovsky 151
            answer->data.phone->state = IPC_PHONE_SLAMMED;
1141 palkovsky 152
        }
153
        spinlock_unlock(&TASK->answerbox.lock);
154
        spinlock_unlock(&answer->data.phone->lock);
1088 palkovsky 155
    }
156
 
157
    if (!olddata)
1329 palkovsky 158
        return 0;
1088 palkovsky 159
 
1086 palkovsky 160
    if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
1040 palkovsky 161
        phoneid = IPC_GET_ARG3(*olddata);
1027 palkovsky 162
        if (IPC_GET_RETVAL(answer->data)) {
163
            /* The connection was not accepted */
164
            phone_dealloc(phoneid);
1040 palkovsky 165
        } else {
1090 palkovsky 166
            /* The connection was accepted */
2359 jermar 167
            phone_connect(phoneid, &answer->sender->answerbox);
2471 jermar 168
            /* Set 'phone hash' as arg3 of response */
2359 jermar 169
            IPC_SET_ARG3(answer->data,
170
                (unative_t) &TASK->phones[phoneid]);
1027 palkovsky 171
        }
1086 palkovsky 172
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
1060 palkovsky 173
        /* If the users accepted call, connect */
174
        if (!IPC_GET_RETVAL(answer->data)) {
2359 jermar 175
            ipc_phone_connect((phone_t *) IPC_GET_ARG3(*olddata),
176
                &TASK->answerbox);
1060 palkovsky 177
        }
1359 jermar 178
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_SEND) {
2359 jermar 179
        if (!IPC_GET_RETVAL(answer->data)) {
180
            /* Accepted, handle as_area receipt */
1413 jermar 181
            ipl_t ipl;
1461 palkovsky 182
            int rc;
1413 jermar 183
            as_t *as;
184
 
185
            ipl = interrupts_disable();
186
            spinlock_lock(&answer->sender->lock);
187
            as = answer->sender->as;
188
            spinlock_unlock(&answer->sender->lock);
189
            interrupts_restore(ipl);
190
 
2359 jermar 191
            rc = as_area_share(as, IPC_GET_ARG1(*olddata),
192
                IPC_GET_ARG2(*olddata), AS,
193
                IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
1461 palkovsky 194
            IPC_SET_RETVAL(answer->data, rc);
195
            return rc;
1329 palkovsky 196
        }
1428 palkovsky 197
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_RECV) {
198
        if (!IPC_GET_RETVAL(answer->data)) {
199
            ipl_t ipl;
200
            as_t *as;
1434 palkovsky 201
            int rc;
1428 palkovsky 202
 
203
            ipl = interrupts_disable();
204
            spinlock_lock(&answer->sender->lock);
205
            as = answer->sender->as;
206
            spinlock_unlock(&answer->sender->lock);
207
            interrupts_restore(ipl);
208
 
2359 jermar 209
            rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
210
                IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
211
                IPC_GET_ARG2(answer->data));
1434 palkovsky 212
            IPC_SET_RETVAL(answer->data, rc);
1428 palkovsky 213
        }
2494 jermar 214
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_SEND) {
215
        if (!IPC_GET_RETVAL(answer->data)) {
216
            int rc;
217
            uintptr_t dst;
218
            uintptr_t size;
219
 
220
            ASSERT(answer->buffer);
221
 
222
            dst = IPC_GET_ARG1(answer->data);
223
            size = IPC_GET_ARG3(answer->data);
224
 
225
            rc = copy_to_uspace((void *) dst, answer->buffer, size);
226
            if (rc != 0)
227
                IPC_SET_RETVAL(answer->data, rc);
228
            free(answer->buffer);
229
            answer->buffer = NULL;
230
        }
1027 palkovsky 231
    }
1329 palkovsky 232
    return 0;
1027 palkovsky 233
}
234
 
2471 jermar 235
/** Called before the request is sent.
1090 palkovsky 236
 *
2471 jermar 237
 * @param call      Call structure with the request.
238
 *
239
 * @return      Return 0 on success, ELIMIT or EPERM on error.
1090 palkovsky 240
 */
241
static int request_preprocess(call_t *call)
242
{
243
    int newphid;
1329 palkovsky 244
    size_t size;
2494 jermar 245
    uintptr_t src;
246
    int rc;
1090 palkovsky 247
 
248
    switch (IPC_GET_METHOD(call->data)) {
249
    case IPC_M_CONNECT_ME_TO:
250
        newphid = phone_alloc();
251
        if (newphid < 0)
252
            return ELIMIT;
253
        /* Set arg3 for server */
2359 jermar 254
        IPC_SET_ARG3(call->data, (unative_t) &TASK->phones[newphid]);
1090 palkovsky 255
        call->flags |= IPC_CALL_CONN_ME_TO;
2098 decky 256
        call->priv = newphid;
1090 palkovsky 257
        break;
1359 jermar 258
    case IPC_M_AS_AREA_SEND:
1417 jermar 259
        size = as_get_size(IPC_GET_ARG1(call->data));
2471 jermar 260
        if (!size)
1329 palkovsky 261
            return EPERM;
1417 jermar 262
        IPC_SET_ARG2(call->data, size);
1329 palkovsky 263
        break;
2494 jermar 264
    case IPC_M_DATA_SEND:
265
        src = IPC_GET_ARG2(call->data);
266
        size = IPC_GET_ARG3(call->data);
267
 
268
        if ((size <= 0) || (size > DATA_SEND_LIMIT))
269
            return ELIMIT;
270
 
271
        call->buffer = (uint8_t *) malloc(size, 0);
272
        rc = copy_from_uspace(call->buffer, (void *) src, size);
273
        if (rc != 0) {
274
            free(call->buffer);
275
            return rc;
276
        }
277
        break;
1090 palkovsky 278
    default:
279
        break;
280
    }
281
    return 0;
282
}
283
 
2471 jermar 284
/*******************************************************************************
285
 * Functions called to process received call/answer before passing it to uspace.
286
 *******************************************************************************/
287
 
288
/** Do basic kernel processing of received call answer.
289
 *
290
 * @param call      Call structure with the answer.
1027 palkovsky 291
 */
1090 palkovsky 292
static void process_answer(call_t *call)
1027 palkovsky 293
{
2359 jermar 294
    if (IPC_GET_RETVAL(call->data) == EHANGUP &&
295
        (call->flags & IPC_CALL_FORWARDED))
1088 palkovsky 296
        IPC_SET_RETVAL(call->data, EFORWARD);
1090 palkovsky 297
 
298
    if (call->flags & IPC_CALL_CONN_ME_TO) {
299
        if (IPC_GET_RETVAL(call->data))
2098 decky 300
            phone_dealloc(call->priv);
1090 palkovsky 301
        else
2098 decky 302
            IPC_SET_ARG3(call->data, call->priv);
1090 palkovsky 303
    }
1027 palkovsky 304
}
305
 
2471 jermar 306
/** Do basic kernel processing of received call request.
1027 palkovsky 307
 *
2471 jermar 308
 * @param box       Destination answerbox structure.
309
 * @param call      Call structure with the request.
310
 *
311
 * @return      Return 0 if the call should be passed to userspace.
312
 *          Return -1 if the call should be ignored.
1027 palkovsky 313
 */
2359 jermar 314
static int process_request(answerbox_t *box, call_t *call)
1027 palkovsky 315
{
316
    int phoneid;
317
 
1086 palkovsky 318
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
1040 palkovsky 319
        phoneid = phone_alloc();
1027 palkovsky 320
        if (phoneid < 0) { /* Failed to allocate phone */
321
            IPC_SET_RETVAL(call->data, ELIMIT);
2471 jermar 322
            ipc_answer(box, call);
1027 palkovsky 323
            return -1;
324
        }
325
        IPC_SET_ARG3(call->data, phoneid);
326
    }
327
    return 0;
328
}
329
 
2471 jermar 330
/** Make a fast call over IPC, wait for reply and return to user.
1027 palkovsky 331
 *
2471 jermar 332
 * This function can handle only one argument of payload, but is faster than
333
 * the generic function (i.e. sys_ipc_call_sync()).
334
 *
335
 * @param phoneid   Phone handle for the call.
336
 * @param method    Method of the call.
337
 * @param arg1      Service-defined payload argument.
338
 * @param data      Address of userspace structure where the reply call will
339
 *          be stored.
340
 *
341
 * @return      Returns 0 on success.
342
 *          Return ENOENT if there is no such phone handle.
1027 palkovsky 343
 */
2359 jermar 344
unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
345
    unative_t arg1, ipc_data_t *data)
1027 palkovsky 346
{
347
    call_t call;
348
    phone_t *phone;
1090 palkovsky 349
    int res;
1027 palkovsky 350
 
1084 palkovsky 351
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
1027 palkovsky 352
 
1084 palkovsky 353
    ipc_call_static_init(&call);
1027 palkovsky 354
    IPC_SET_METHOD(call.data, method);
355
    IPC_SET_ARG1(call.data, arg1);
356
 
2471 jermar 357
    if (!(res = request_preprocess(&call))) {
1090 palkovsky 358
        ipc_call_sync(phone, &call);
359
        process_answer(&call);
2471 jermar 360
    } else {
1090 palkovsky 361
        IPC_SET_RETVAL(call.data, res);
2471 jermar 362
    }
1086 palkovsky 363
    STRUCT_TO_USPACE(&data->args, &call.data.args);
1027 palkovsky 364
 
365
    return 0;
366
}
367
 
2471 jermar 368
/** Make a synchronous IPC call allowing to transmit the entire payload.
369
 *
370
 * @param phoneid   Phone handle for the call.
371
 * @param question  Userspace address of call data with the request.
2494 jermar 372
 * @param reply     Userspace address of call data where to store the
373
 *          answer.
2471 jermar 374
 *
375
 * @return      Zero on success or an error code.
376
 */
2359 jermar 377
unative_t sys_ipc_call_sync(unative_t phoneid, ipc_data_t *question,
378
    ipc_data_t *reply)
1027 palkovsky 379
{
380
    call_t call;
381
    phone_t *phone;
1090 palkovsky 382
    int res;
1288 jermar 383
    int rc;
1027 palkovsky 384
 
1084 palkovsky 385
    ipc_call_static_init(&call);
2359 jermar 386
    rc = copy_from_uspace(&call.data.args, &question->args,
387
        sizeof(call.data.args));
1288 jermar 388
    if (rc != 0)
1780 jermar 389
        return (unative_t) rc;
1040 palkovsky 390
 
1084 palkovsky 391
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
1072 palkovsky 392
 
2471 jermar 393
    if (!(res = request_preprocess(&call))) {
1090 palkovsky 394
        ipc_call_sync(phone, &call);
395
        process_answer(&call);
396
    } else
397
        IPC_SET_RETVAL(call.data, res);
1027 palkovsky 398
 
1288 jermar 399
    rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
400
    if (rc != 0)
401
        return rc;
1027 palkovsky 402
 
403
    return 0;
404
}
405
 
2471 jermar 406
/** Check that the task did not exceed the allowed limit of asynchronous calls.
1027 palkovsky 407
 *
2471 jermar 408
 * @return      Return 0 if limit not reached or -1 if limit exceeded.
1027 palkovsky 409
 */
410
static int check_call_limit(void)
411
{
412
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
413
        atomic_dec(&TASK->active_calls);
414
        return -1;
415
    }
416
    return 0;
417
}
418
 
2471 jermar 419
/** Make a fast asynchronous call over IPC.
1027 palkovsky 420
 *
2471 jermar 421
 * This function can only handle two arguments of payload, but is faster than
422
 * the generic function sys_ipc_call_async().
423
 *
424
 * @param phoneid   Phone handle for the call.
425
 * @param method    Method of the call.
426
 * @param arg1      Service-defined payload argument.
427
 * @param arg2      Service-defined payload argument.
428
 *
429
 * @return      Return call hash on success.
430
 *          Return IPC_CALLRET_FATAL in case of a fatal error and
431
 *          IPC_CALLRET_TEMPORARY if there are too many pending
432
 *          asynchronous requests; answers should be handled first.
1027 palkovsky 433
 */
2359 jermar 434
unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
435
    unative_t arg1, unative_t arg2)
1027 palkovsky 436
{
437
    call_t *call;
438
    phone_t *phone;
1090 palkovsky 439
    int res;
1027 palkovsky 440
 
441
    if (check_call_limit())
442
        return IPC_CALLRET_TEMPORARY;
443
 
1090 palkovsky 444
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
1072 palkovsky 445
 
1258 palkovsky 446
    call = ipc_call_alloc(0);
1027 palkovsky 447
    IPC_SET_METHOD(call->data, method);
448
    IPC_SET_ARG1(call->data, arg1);
449
    IPC_SET_ARG2(call->data, arg2);
1669 palkovsky 450
    IPC_SET_ARG3(call->data, 0);
1027 palkovsky 451
 
2359 jermar 452
    if (!(res = request_preprocess(call)))
1090 palkovsky 453
        ipc_call(phone, call);
454
    else
455
        ipc_backsend_err(phone, call, res);
1027 palkovsky 456
 
1780 jermar 457
    return (unative_t) call;
1027 palkovsky 458
}
459
 
2471 jermar 460
/** Make an asynchronous IPC call allowing to transmit the entire payload.
1027 palkovsky 461
 *
2471 jermar 462
 * @param phoneid   Phone handle for the call.
463
 * @param data      Userspace address of call data with the request.
464
 *
465
 * @return      See sys_ipc_call_async_fast().
1027 palkovsky 466
 */
1780 jermar 467
unative_t sys_ipc_call_async(unative_t phoneid, ipc_data_t *data)
1027 palkovsky 468
{
469
    call_t *call;
470
    phone_t *phone;
1090 palkovsky 471
    int res;
1288 jermar 472
    int rc;
1027 palkovsky 473
 
1072 palkovsky 474
    if (check_call_limit())
475
        return IPC_CALLRET_TEMPORARY;
476
 
1090 palkovsky 477
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
1027 palkovsky 478
 
1258 palkovsky 479
    call = ipc_call_alloc(0);
2359 jermar 480
    rc = copy_from_uspace(&call->data.args, &data->args,
481
        sizeof(call->data.args));
1293 palkovsky 482
    if (rc != 0) {
483
        ipc_call_free(call);
1780 jermar 484
        return (unative_t) rc;
1293 palkovsky 485
    }
2359 jermar 486
    if (!(res = request_preprocess(call)))
1090 palkovsky 487
        ipc_call(phone, call);
488
    else
489
        ipc_backsend_err(phone, call, res);
1040 palkovsky 490
 
1780 jermar 491
    return (unative_t) call;
1027 palkovsky 492
}
493
 
2471 jermar 494
/** Forward a received call to another destination.
1040 palkovsky 495
 *
2471 jermar 496
 * @param callid    Hash of the call to forward.
497
 * @param phoneid   Phone handle to use for forwarding.
498
 * @param method    New method to use for the forwarded call.
499
 * @param arg1      New value of the first argument for the forwarded call.
1060 palkovsky 500
 *
2471 jermar 501
 * @return      Return 0 on succes, otherwise return an error code.
502
 *
503
 * In case the original method is a system method, ARG1 and ARG2 are overwritten
504
 * in the forwarded message with the new method and the new arg1, respectively.
505
 * Otherwise the METHOD and ARG1 are rewritten with the new method and arg1,
506
 * respectively.
507
 *
1060 palkovsky 508
 * Warning: If implementing non-fast version, make sure that
2471 jermar 509
 *          ARG3 is not rewritten for certain system IPC
1040 palkovsky 510
 */
1780 jermar 511
unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
2359 jermar 512
    unative_t method, unative_t arg1)
1040 palkovsky 513
{
514
    call_t *call;
515
    phone_t *phone;
516
 
517
    call = get_call(callid);
518
    if (!call)
519
        return ENOENT;
520
 
1088 palkovsky 521
    call->flags |= IPC_CALL_FORWARDED;
522
 
1084 palkovsky 523
    GET_CHECK_PHONE(phone, phoneid, {
1040 palkovsky 524
        IPC_SET_RETVAL(call->data, EFORWARD);
525
        ipc_answer(&TASK->answerbox, call);
526
        return ENOENT;
1084 palkovsky 527
    });    
1040 palkovsky 528
 
529
    if (!is_forwardable(IPC_GET_METHOD(call->data))) {
530
        IPC_SET_RETVAL(call->data, EFORWARD);
531
        ipc_answer(&TASK->answerbox, call);
532
        return EPERM;
533
    }
534
 
535
    /* Userspace is not allowed to change method of system methods
536
     * on forward, allow changing ARG1 and ARG2 by means of method and arg1
537
     */
538
    if (is_system_method(IPC_GET_METHOD(call->data))) {
1090 palkovsky 539
        if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
540
            phone_dealloc(IPC_GET_ARG3(call->data));
541
 
1040 palkovsky 542
        IPC_SET_ARG1(call->data, method);
543
        IPC_SET_ARG2(call->data, arg1);
544
    } else {
545
        IPC_SET_METHOD(call->data, method);
546
        IPC_SET_ARG1(call->data, arg1);
547
    }
548
 
1088 palkovsky 549
    return ipc_forward(call, phone, &TASK->answerbox);
1040 palkovsky 550
}
551
 
2471 jermar 552
/** Answer an IPC call - fast version.
553
 *
554
 * This function can handle only two return arguments of payload, but is faster
555
 * than the generic sys_ipc_answer().
556
 *
557
 * @param callid    Hash of the call to be answered.
558
 * @param retval    Return value of the answer.
559
 * @param arg1      Service-defined return value.
560
 * @param arg2      Service-defined return value.
561
 *
562
 * @return      Return 0 on success, otherwise return an error code.   
563
 */
2359 jermar 564
unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
565
    unative_t arg1, unative_t arg2)
1027 palkovsky 566
{
567
    call_t *call;
1040 palkovsky 568
    ipc_data_t saved_data;
1088 palkovsky 569
    int saveddata = 0;
1329 palkovsky 570
    int rc;
1027 palkovsky 571
 
1346 palkovsky 572
    /* Do not answer notification callids */
573
    if (callid & IPC_CALLID_NOTIFICATION)
574
        return 0;
575
 
1040 palkovsky 576
    call = get_call(callid);
577
    if (!call)
578
        return ENOENT;
1027 palkovsky 579
 
1088 palkovsky 580
    if (answer_need_old(call)) {
1040 palkovsky 581
        memcpy(&saved_data, &call->data, sizeof(call->data));
1088 palkovsky 582
        saveddata = 1;
1027 palkovsky 583
    }
584
 
585
    IPC_SET_RETVAL(call->data, retval);
586
    IPC_SET_ARG1(call->data, arg1);
587
    IPC_SET_ARG2(call->data, arg2);
1329 palkovsky 588
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
1040 palkovsky 589
 
1027 palkovsky 590
    ipc_answer(&TASK->answerbox, call);
1329 palkovsky 591
    return rc;
1027 palkovsky 592
}
593
 
2471 jermar 594
/** Answer an IPC call.
595
 *
596
 * @param callid    Hash of the call to be answered.
597
 * @param data      Userspace address of call data with the answer.
598
 *
599
 * @return      Return 0 on success, otherwise return an error code.
600
 */
1780 jermar 601
unative_t sys_ipc_answer(unative_t callid, ipc_data_t *data)
1027 palkovsky 602
{
603
    call_t *call;
1040 palkovsky 604
    ipc_data_t saved_data;
1088 palkovsky 605
    int saveddata = 0;
1288 jermar 606
    int rc;
1027 palkovsky 607
 
1346 palkovsky 608
    /* Do not answer notification callids */
609
    if (callid & IPC_CALLID_NOTIFICATION)
610
        return 0;
611
 
1040 palkovsky 612
    call = get_call(callid);
613
    if (!call)
614
        return ENOENT;
1027 palkovsky 615
 
1088 palkovsky 616
    if (answer_need_old(call)) {
1040 palkovsky 617
        memcpy(&saved_data, &call->data, sizeof(call->data));
1088 palkovsky 618
        saveddata = 1;
1027 palkovsky 619
    }
1288 jermar 620
    rc = copy_from_uspace(&call->data.args, &data->args,
2359 jermar 621
        sizeof(call->data.args));
1288 jermar 622
    if (rc != 0)
623
        return rc;
1027 palkovsky 624
 
1329 palkovsky 625
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
1027 palkovsky 626
 
627
    ipc_answer(&TASK->answerbox, call);
628
 
1329 palkovsky 629
    return rc;
1027 palkovsky 630
}
631
 
2471 jermar 632
/** Hang up a phone.
1086 palkovsky 633
 *
2471 jermar 634
 * @param       Phone handle of the phone to be hung up.
635
 *
636
 * @return      Return 0 on success or an error code.
1086 palkovsky 637
 */
1780 jermar 638
unative_t sys_ipc_hangup(int phoneid)
1086 palkovsky 639
{
640
    phone_t *phone;
641
 
642
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
643
 
1591 palkovsky 644
    if (ipc_phone_hangup(phone))
1086 palkovsky 645
        return -1;
646
 
647
    return 0;
648
}
649
 
2471 jermar 650
/** Wait for an incoming IPC call or an answer.
1027 palkovsky 651
 *
2359 jermar 652
 * @param calldata  Pointer to buffer where the call/answer data is stored.
653
 * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
654
 * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
655
 *          for explanation.
1364 jermar 656
 *
2471 jermar 657
 * @return      Hash of the call.
658
 *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
659
 *          call is a notification. IPC_CALLID_ANSWERED denotes an
660
 *          answer.
1027 palkovsky 661
 */
1780 jermar 662
unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
1027 palkovsky 663
{
664
    call_t *call;
665
 
666
restart:   
2359 jermar 667
    call = ipc_wait_for_call(&TASK->answerbox, usec,
668
        flags | SYNCH_FLAGS_INTERRUPTIBLE);
1088 palkovsky 669
    if (!call)
670
        return 0;
1027 palkovsky 671
 
1258 palkovsky 672
    if (call->flags & IPC_CALL_NOTIF) {
673
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
1693 palkovsky 674
 
675
        /* Set in_phone_hash to the interrupt counter */
2098 decky 676
        call->data.phone = (void *) call->priv;
1693 palkovsky 677
 
678
        STRUCT_TO_USPACE(calldata, &call->data);
679
 
1258 palkovsky 680
        ipc_call_free(call);
681
 
2471 jermar 682
        return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
1258 palkovsky 683
    }
684
 
1027 palkovsky 685
    if (call->flags & IPC_CALL_ANSWERED) {
1090 palkovsky 686
        process_answer(call);
1027 palkovsky 687
 
1086 palkovsky 688
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
689
 
1027 palkovsky 690
        atomic_dec(&TASK->active_calls);
1086 palkovsky 691
 
692
        if (call->flags & IPC_CALL_DISCARD_ANSWER) {
693
            ipc_call_free(call);
694
            goto restart;
695
        }
696
 
697
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
1027 palkovsky 698
        ipc_call_free(call);
699
 
2471 jermar 700
        return ((unative_t) call) | IPC_CALLID_ANSWERED;
1027 palkovsky 701
    }
1086 palkovsky 702
 
1027 palkovsky 703
    if (process_request(&TASK->answerbox, call))
704
        goto restart;
1086 palkovsky 705
 
1090 palkovsky 706
    /* Include phone address('id') of the caller in the request,
707
     * copy whole call->data, not only call->data.args */
1396 palkovsky 708
    if (STRUCT_TO_USPACE(calldata, &call->data)) {
709
        return 0;
710
    }
1780 jermar 711
    return (unative_t)call;
1027 palkovsky 712
}
1258 palkovsky 713
 
2471 jermar 714
/** Connect an IRQ handler to a task.
1923 jermar 715
 *
2471 jermar 716
 * @param inr       IRQ number.
717
 * @param devno     Device number.
718
 * @param method    Method to be associated with the notification.
719
 * @param ucode     Uspace pointer to the top-half pseudocode.
1923 jermar 720
 *
2471 jermar 721
 * @return      EPERM or a return code returned by ipc_irq_register().
1923 jermar 722
 */
2359 jermar 723
unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
724
    irq_code_t *ucode)
1258 palkovsky 725
{
1297 jermar 726
    if (!(cap_get(TASK) & CAP_IRQ_REG))
727
        return EPERM;
728
 
1923 jermar 729
    return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
1258 palkovsky 730
}
731
 
2471 jermar 732
/** Disconnect an IRQ handler from a task.
1923 jermar 733
 *
2471 jermar 734
 * @param inr       IRQ number.
735
 * @param devno     Device number.
736
 *
737
 * @return      Zero on success or EPERM on error..
1923 jermar 738
 */
739
unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
1258 palkovsky 740
{
1297 jermar 741
    if (!(cap_get(TASK) & CAP_IRQ_REG))
742
        return EPERM;
743
 
1923 jermar 744
    ipc_irq_unregister(&TASK->answerbox, inr, devno);
1258 palkovsky 745
 
746
    return 0;
747
}
1702 cejka 748
 
1757 jermar 749
/** @}
1702 cejka 750
 */