Subversion Repositories HelenOS

Rev

Rev 3665 | 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>
3492 rimsky 45
#include <ipc/kbox.h>
46
#include <udebug/udebug_ipc.h>
3862 rimsky 47
#include <arch/asm.h>
1258 palkovsky 48
#include <arch/interrupt.h>
1288 jermar 49
#include <syscall/copy.h>
1297 jermar 50
#include <security/cap.h>
1329 palkovsky 51
#include <mm/as.h>
1875 jermar 52
#include <print.h>
1027 palkovsky 53
 
2660 jermar 54
/**
55
 * Maximum buffer size allowed for IPC_M_DATA_WRITE and IPC_M_DATA_READ
56
 * requests.
57
 */
58
#define DATA_XFER_LIMIT     (64 * 1024)
2494 jermar 59
 
2471 jermar 60
#define GET_CHECK_PHONE(phone, phoneid, err) \
61
{ \
62
    if (phoneid > IPC_MAX_PHONES) { \
63
        err; \
64
    } \
65
    phone = &TASK->phones[phoneid]; \
1084 palkovsky 66
}
67
 
2471 jermar 68
#define STRUCT_TO_USPACE(dst, src)  copy_to_uspace(dst, src, sizeof(*(src)))
1084 palkovsky 69
 
2471 jermar 70
/** Decide if the method is a system method.
71
 *
72
 * @param method    Method to be decided.
73
 *
74
 * @return      Return 1 if the method is a system method.
75
 *          Otherwise return 0.
76
 */
2557 jermar 77
static inline int method_is_system(unative_t method)
1040 palkovsky 78
{
79
    if (method <= IPC_M_LAST_SYSTEM)
80
        return 1;
81
    return 0;
82
}
83
 
2471 jermar 84
/** Decide if the message with this method is forwardable.
1040 palkovsky 85
 *
86
 * - some system messages may be forwarded, for some of them
87
 *   it is useless
2471 jermar 88
 *
89
 * @param method    Method to be decided.
90
 *
91
 * @return      Return 1 if the method is forwardable.
92
 *          Otherwise return 0.
1040 palkovsky 93
 */
2557 jermar 94
static inline int method_is_forwardable(unative_t method)
1040 palkovsky 95
{
2494 jermar 96
    switch (method) {
97
    case IPC_M_PHONE_HUNGUP:
98
        /* This message is meant only for the original recipient. */
99
        return 0;
100
    default:
101
        return 1;
102
    }
1040 palkovsky 103
}
104
 
2557 jermar 105
/** Decide if the message with this method is immutable on forward.
106
 *
2601 jermar 107
 * - some system messages may be forwarded but their content cannot be altered
2557 jermar 108
 *
109
 * @param method    Method to be decided.
110
 *
111
 * @return      Return 1 if the method is immutable on forward.
112
 *          Otherwise return 0.
113
 */
114
static inline int method_is_immutable(unative_t method)
115
{
116
    switch (method) {
2677 jermar 117
    case IPC_M_SHARE_OUT:
118
    case IPC_M_SHARE_IN:
2660 jermar 119
    case IPC_M_DATA_WRITE:
120
    case IPC_M_DATA_READ:
2557 jermar 121
        return 1;
122
        break;
123
    default:
124
        return 0;
125
    }
126
}
1027 palkovsky 127
 
2557 jermar 128
 
2471 jermar 129
/***********************************************************************
130
 * Functions that preprocess answer before sending it to the recepient.
131
 ***********************************************************************/
132
 
133
/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
134
 * for answer_preprocess().
135
 *
136
 * @param call      Call structure to be decided.
137
 *
138
 * @return      Return 1 if the old call contents should be saved.
139
 *          Return 0 otherwise.
1027 palkovsky 140
 */
1088 palkovsky 141
static inline int answer_need_old(call_t *call)
1027 palkovsky 142
{
2494 jermar 143
    switch (IPC_GET_METHOD(call->data)) {
144
    case IPC_M_CONNECT_TO_ME:
145
    case IPC_M_CONNECT_ME_TO:
2677 jermar 146
    case IPC_M_SHARE_OUT:
147
    case IPC_M_SHARE_IN:
2660 jermar 148
    case IPC_M_DATA_WRITE:
149
    case IPC_M_DATA_READ:
1027 palkovsky 150
        return 1;
2494 jermar 151
    default:
152
        return 0;
153
    }
1027 palkovsky 154
}
155
 
2471 jermar 156
/** Interpret process answer as control information.
1428 palkovsky 157
 *
2471 jermar 158
 * This function is called directly after sys_ipc_answer().
159
 *
160
 * @param answer    Call structure with the answer.
161
 * @param olddata   Saved data of the request.
162
 *
163
 * @return      Return 0 on success or an error code.
1428 palkovsky 164
 */
1329 palkovsky 165
static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
1027 palkovsky 166
{
167
    int phoneid;
168
 
2745 decky 169
    if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
1141 palkovsky 170
        /* In case of forward, hangup the forwared phone,
171
         * not the originator
172
         */
3020 jermar 173
        mutex_lock(&answer->data.phone->lock);
1141 palkovsky 174
        spinlock_lock(&TASK->answerbox.lock);
1568 palkovsky 175
        if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
1573 palkovsky 176
            list_remove(&answer->data.phone->link);
1568 palkovsky 177
            answer->data.phone->state = IPC_PHONE_SLAMMED;
1141 palkovsky 178
        }
179
        spinlock_unlock(&TASK->answerbox.lock);
3020 jermar 180
        mutex_unlock(&answer->data.phone->lock);
1088 palkovsky 181
    }
182
 
183
    if (!olddata)
1329 palkovsky 184
        return 0;
1088 palkovsky 185
 
1086 palkovsky 186
    if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
2637 cejka 187
        phoneid = IPC_GET_ARG5(*olddata);
1027 palkovsky 188
        if (IPC_GET_RETVAL(answer->data)) {
189
            /* The connection was not accepted */
190
            phone_dealloc(phoneid);
1040 palkovsky 191
        } else {
1090 palkovsky 192
            /* The connection was accepted */
2359 jermar 193
            phone_connect(phoneid, &answer->sender->answerbox);
2638 jermar 194
            /* Set 'phone hash' as arg5 of response */
2637 cejka 195
            IPC_SET_ARG5(answer->data,
2359 jermar 196
                (unative_t) &TASK->phones[phoneid]);
1027 palkovsky 197
        }
1086 palkovsky 198
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
1060 palkovsky 199
        /* If the users accepted call, connect */
200
        if (!IPC_GET_RETVAL(answer->data)) {
2635 cejka 201
            ipc_phone_connect((phone_t *) IPC_GET_ARG5(*olddata),
2359 jermar 202
                &TASK->answerbox);
1060 palkovsky 203
        }
2677 jermar 204
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_SHARE_OUT) {
2359 jermar 205
        if (!IPC_GET_RETVAL(answer->data)) {
206
            /* Accepted, handle as_area receipt */
1413 jermar 207
            ipl_t ipl;
1461 palkovsky 208
            int rc;
1413 jermar 209
            as_t *as;
210
 
211
            ipl = interrupts_disable();
212
            spinlock_lock(&answer->sender->lock);
213
            as = answer->sender->as;
214
            spinlock_unlock(&answer->sender->lock);
215
            interrupts_restore(ipl);
216
 
2359 jermar 217
            rc = as_area_share(as, IPC_GET_ARG1(*olddata),
218
                IPC_GET_ARG2(*olddata), AS,
219
                IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
1461 palkovsky 220
            IPC_SET_RETVAL(answer->data, rc);
221
            return rc;
1329 palkovsky 222
        }
2677 jermar 223
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_SHARE_IN) {
1428 palkovsky 224
        if (!IPC_GET_RETVAL(answer->data)) {
225
            ipl_t ipl;
226
            as_t *as;
1434 palkovsky 227
            int rc;
1428 palkovsky 228
 
229
            ipl = interrupts_disable();
230
            spinlock_lock(&answer->sender->lock);
231
            as = answer->sender->as;
232
            spinlock_unlock(&answer->sender->lock);
233
            interrupts_restore(ipl);
234
 
2359 jermar 235
            rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
236
                IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
237
                IPC_GET_ARG2(answer->data));
1434 palkovsky 238
            IPC_SET_RETVAL(answer->data, rc);
1428 palkovsky 239
        }
2662 jermar 240
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_READ) {
241
        ASSERT(!answer->buffer);
242
        if (!IPC_GET_RETVAL(answer->data)) {
243
            /* The recipient agreed to send data. */
244
            uintptr_t src = IPC_GET_ARG1(answer->data);
245
            uintptr_t dst = IPC_GET_ARG1(*olddata);
246
            size_t max_size = IPC_GET_ARG2(*olddata);
247
            size_t size = IPC_GET_ARG2(answer->data);
2934 jermar 248
            if (size && size <= max_size) {
2662 jermar 249
                /*
250
                 * Copy the destination VA so that this piece of
251
                 * information is not lost.
252
                 */
253
                IPC_SET_ARG1(answer->data, dst);
254
 
255
                answer->buffer = malloc(size, 0);
256
                int rc = copy_from_uspace(answer->buffer,
257
                    (void *) src, size);
258
                if (rc) {
259
                    IPC_SET_RETVAL(answer->data, rc);
260
                    free(answer->buffer);
261
                    answer->buffer = NULL;
262
                }
2934 jermar 263
            } else if (!size) {
264
                IPC_SET_RETVAL(answer->data, EOK);
2662 jermar 265
            } else {
266
                IPC_SET_RETVAL(answer->data, ELIMIT);
267
            }
268
        }
2660 jermar 269
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_WRITE) {
2661 jermar 270
        ASSERT(answer->buffer);
2494 jermar 271
        if (!IPC_GET_RETVAL(answer->data)) {
2662 jermar 272
            /* The recipient agreed to receive data. */
2494 jermar 273
            int rc;
274
            uintptr_t dst;
3332 jermar 275
            size_t size;
276
            size_t max_size;
2494 jermar 277
 
3332 jermar 278
            dst = (uintptr_t)IPC_GET_ARG1(answer->data);
279
            size = (size_t)IPC_GET_ARG2(answer->data);
280
            max_size = (size_t)IPC_GET_ARG2(*olddata);
2494 jermar 281
 
2662 jermar 282
            if (size <= max_size) {
283
                rc = copy_to_uspace((void *) dst,
284
                    answer->buffer, size);
285
                if (rc)
286
                    IPC_SET_RETVAL(answer->data, rc);
287
            } else {
288
                IPC_SET_RETVAL(answer->data, ELIMIT);
289
            }
2494 jermar 290
        }
2661 jermar 291
        free(answer->buffer);
292
        answer->buffer = NULL;
1027 palkovsky 293
    }
1329 palkovsky 294
    return 0;
1027 palkovsky 295
}
296
 
2471 jermar 297
/** Called before the request is sent.
1090 palkovsky 298
 *
2471 jermar 299
 * @param call      Call structure with the request.
3492 rimsky 300
 * @param phone     Phone that the call will be sent through.
2471 jermar 301
 *
302
 * @return      Return 0 on success, ELIMIT or EPERM on error.
1090 palkovsky 303
 */
3492 rimsky 304
static int request_preprocess(call_t *call, phone_t *phone)
1090 palkovsky 305
{
306
    int newphid;
1329 palkovsky 307
    size_t size;
2494 jermar 308
    uintptr_t src;
309
    int rc;
1090 palkovsky 310
 
311
    switch (IPC_GET_METHOD(call->data)) {
312
    case IPC_M_CONNECT_ME_TO:
313
        newphid = phone_alloc();
314
        if (newphid < 0)
315
            return ELIMIT;
2638 jermar 316
        /* Set arg5 for server */
2635 cejka 317
        IPC_SET_ARG5(call->data, (unative_t) &TASK->phones[newphid]);
1090 palkovsky 318
        call->flags |= IPC_CALL_CONN_ME_TO;
2098 decky 319
        call->priv = newphid;
1090 palkovsky 320
        break;
2677 jermar 321
    case IPC_M_SHARE_OUT:
2556 jermar 322
        size = as_area_get_size(IPC_GET_ARG1(call->data));
2471 jermar 323
        if (!size)
1329 palkovsky 324
            return EPERM;
1417 jermar 325
        IPC_SET_ARG2(call->data, size);
1329 palkovsky 326
        break;
2662 jermar 327
    case IPC_M_DATA_READ:
328
        size = IPC_GET_ARG2(call->data);
329
        if ((size <= 0 || (size > DATA_XFER_LIMIT)))
330
            return ELIMIT;
331
        break;
2660 jermar 332
    case IPC_M_DATA_WRITE:
2676 jermar 333
        src = IPC_GET_ARG1(call->data);
334
        size = IPC_GET_ARG2(call->data);
2494 jermar 335
 
2660 jermar 336
        if ((size <= 0) || (size > DATA_XFER_LIMIT))
2494 jermar 337
            return ELIMIT;
338
 
339
        call->buffer = (uint8_t *) malloc(size, 0);
340
        rc = copy_from_uspace(call->buffer, (void *) src, size);
341
        if (rc != 0) {
342
            free(call->buffer);
343
            return rc;
344
        }
345
        break;
3492 rimsky 346
#ifdef CONFIG_UDEBUG
347
    case IPC_M_DEBUG_ALL:
348
        return udebug_request_preprocess(call, phone);
349
#endif
1090 palkovsky 350
    default:
351
        break;
352
    }
353
    return 0;
354
}
355
 
2471 jermar 356
/*******************************************************************************
357
 * Functions called to process received call/answer before passing it to uspace.
358
 *******************************************************************************/
359
 
360
/** Do basic kernel processing of received call answer.
361
 *
362
 * @param call      Call structure with the answer.
1027 palkovsky 363
 */
1090 palkovsky 364
static void process_answer(call_t *call)
1027 palkovsky 365
{
2745 decky 366
    if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
2359 jermar 367
        (call->flags & IPC_CALL_FORWARDED))
1088 palkovsky 368
        IPC_SET_RETVAL(call->data, EFORWARD);
1090 palkovsky 369
 
370
    if (call->flags & IPC_CALL_CONN_ME_TO) {
371
        if (IPC_GET_RETVAL(call->data))
2098 decky 372
            phone_dealloc(call->priv);
1090 palkovsky 373
        else
2635 cejka 374
            IPC_SET_ARG5(call->data, call->priv);
1090 palkovsky 375
    }
2662 jermar 376
 
377
    if (call->buffer) {
378
        /* This must be an affirmative answer to IPC_M_DATA_READ. */
3492 rimsky 379
        /* or IPC_M_DEBUG_ALL/UDEBUG_M_MEM_READ... */
2662 jermar 380
        uintptr_t dst = IPC_GET_ARG1(call->data);
381
        size_t size = IPC_GET_ARG2(call->data);
382
        int rc = copy_to_uspace((void *) dst, call->buffer, size);
383
        if (rc)
384
            IPC_SET_RETVAL(call->data, rc);
385
        free(call->buffer);
386
        call->buffer = NULL;
387
    }
1027 palkovsky 388
}
389
 
2471 jermar 390
/** Do basic kernel processing of received call request.
1027 palkovsky 391
 *
2471 jermar 392
 * @param box       Destination answerbox structure.
393
 * @param call      Call structure with the request.
394
 *
395
 * @return      Return 0 if the call should be passed to userspace.
396
 *          Return -1 if the call should be ignored.
1027 palkovsky 397
 */
2359 jermar 398
static int process_request(answerbox_t *box, call_t *call)
1027 palkovsky 399
{
400
    int phoneid;
401
 
1086 palkovsky 402
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
1040 palkovsky 403
        phoneid = phone_alloc();
1027 palkovsky 404
        if (phoneid < 0) { /* Failed to allocate phone */
405
            IPC_SET_RETVAL(call->data, ELIMIT);
2471 jermar 406
            ipc_answer(box, call);
1027 palkovsky 407
            return -1;
408
        }
2637 cejka 409
        IPC_SET_ARG5(call->data, phoneid);
3492 rimsky 410
    }
411
    switch (IPC_GET_METHOD(call->data)) {
412
    case IPC_M_DEBUG_ALL:
413
        return -1;
414
    default:
415
        break;
416
    }
1027 palkovsky 417
    return 0;
418
}
419
 
2471 jermar 420
/** Make a fast call over IPC, wait for reply and return to user.
1027 palkovsky 421
 *
2615 jermar 422
 * This function can handle only three arguments of payload, but is faster than
423
 * the generic function (i.e. sys_ipc_call_sync_slow()).
2471 jermar 424
 *
425
 * @param phoneid   Phone handle for the call.
426
 * @param method    Method of the call.
427
 * @param arg1      Service-defined payload argument.
2615 jermar 428
 * @param arg2      Service-defined payload argument.
429
 * @param arg3      Service-defined payload argument.
2471 jermar 430
 * @param data      Address of userspace structure where the reply call will
431
 *          be stored.
432
 *
433
 * @return      Returns 0 on success.
434
 *          Return ENOENT if there is no such phone handle.
1027 palkovsky 435
 */
2359 jermar 436
unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
2615 jermar 437
    unative_t arg1, unative_t arg2, unative_t arg3, ipc_data_t *data)
1027 palkovsky 438
{
439
    call_t call;
440
    phone_t *phone;
1090 palkovsky 441
    int res;
2617 jermar 442
    int rc;
3054 decky 443
 
1084 palkovsky 444
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
1027 palkovsky 445
 
1084 palkovsky 446
    ipc_call_static_init(&call);
1027 palkovsky 447
    IPC_SET_METHOD(call.data, method);
448
    IPC_SET_ARG1(call.data, arg1);
2615 jermar 449
    IPC_SET_ARG2(call.data, arg2);
450
    IPC_SET_ARG3(call.data, arg3);
2620 jermar 451
    /*
452
     * To achieve deterministic behavior, zero out arguments that are beyond
453
     * the limits of the fast version.
454
     */
455
    IPC_SET_ARG4(call.data, 0);
456
    IPC_SET_ARG5(call.data, 0);
1027 palkovsky 457
 
3492 rimsky 458
    if (!(res = request_preprocess(&call, phone))) {
3665 rimsky 459
#ifdef CONFIG_UDEBUG
460
        udebug_stoppable_begin();
461
#endif
3397 rimsky 462
        rc = ipc_call_sync(phone, &call);
3665 rimsky 463
#ifdef CONFIG_UDEBUG
464
        udebug_stoppable_end();
465
#endif
3397 rimsky 466
        if (rc != EOK)
467
            return rc;
1090 palkovsky 468
        process_answer(&call);
3665 rimsky 469
 
2471 jermar 470
    } else {
1090 palkovsky 471
        IPC_SET_RETVAL(call.data, res);
2471 jermar 472
    }
2617 jermar 473
    rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
474
    if (rc != 0)
475
        return rc;
1027 palkovsky 476
 
477
    return 0;
478
}
479
 
2471 jermar 480
/** Make a synchronous IPC call allowing to transmit the entire payload.
481
 *
482
 * @param phoneid   Phone handle for the call.
483
 * @param question  Userspace address of call data with the request.
2494 jermar 484
 * @param reply     Userspace address of call data where to store the
485
 *          answer.
2471 jermar 486
 *
487
 * @return      Zero on success or an error code.
488
 */
2615 jermar 489
unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question,
2359 jermar 490
    ipc_data_t *reply)
1027 palkovsky 491
{
492
    call_t call;
493
    phone_t *phone;
1090 palkovsky 494
    int res;
1288 jermar 495
    int rc;
1027 palkovsky 496
 
1084 palkovsky 497
    ipc_call_static_init(&call);
2359 jermar 498
    rc = copy_from_uspace(&call.data.args, &question->args,
499
        sizeof(call.data.args));
1288 jermar 500
    if (rc != 0)
1780 jermar 501
        return (unative_t) rc;
1040 palkovsky 502
 
1084 palkovsky 503
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
1072 palkovsky 504
 
3492 rimsky 505
    if (!(res = request_preprocess(&call, phone))) {
3665 rimsky 506
#ifdef CONFIG_UDEBUG
507
        udebug_stoppable_begin();
508
#endif
3397 rimsky 509
        rc = ipc_call_sync(phone, &call);
3665 rimsky 510
#ifdef CONFIG_UDEBUG
511
        udebug_stoppable_end();
512
#endif
3397 rimsky 513
        if (rc != EOK)
514
            return rc;
1090 palkovsky 515
        process_answer(&call);
516
    } else
517
        IPC_SET_RETVAL(call.data, res);
1027 palkovsky 518
 
1288 jermar 519
    rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
520
    if (rc != 0)
521
        return rc;
1027 palkovsky 522
 
523
    return 0;
524
}
525
 
2471 jermar 526
/** Check that the task did not exceed the allowed limit of asynchronous calls.
1027 palkovsky 527
 *
2471 jermar 528
 * @return      Return 0 if limit not reached or -1 if limit exceeded.
1027 palkovsky 529
 */
530
static int check_call_limit(void)
531
{
532
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
533
        atomic_dec(&TASK->active_calls);
534
        return -1;
535
    }
536
    return 0;
537
}
538
 
2471 jermar 539
/** Make a fast asynchronous call over IPC.
1027 palkovsky 540
 *
2618 jermar 541
 * This function can only handle four arguments of payload, but is faster than
542
 * the generic function sys_ipc_call_async_slow().
2471 jermar 543
 *
544
 * @param phoneid   Phone handle for the call.
545
 * @param method    Method of the call.
546
 * @param arg1      Service-defined payload argument.
547
 * @param arg2      Service-defined payload argument.
2618 jermar 548
 * @param arg3      Service-defined payload argument.
549
 * @param arg4      Service-defined payload argument.
2471 jermar 550
 *
551
 * @return      Return call hash on success.
552
 *          Return IPC_CALLRET_FATAL in case of a fatal error and
553
 *          IPC_CALLRET_TEMPORARY if there are too many pending
554
 *          asynchronous requests; answers should be handled first.
1027 palkovsky 555
 */
2359 jermar 556
unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
2618 jermar 557
    unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
1027 palkovsky 558
{
559
    call_t *call;
560
    phone_t *phone;
1090 palkovsky 561
    int res;
1027 palkovsky 562
 
563
    if (check_call_limit())
564
        return IPC_CALLRET_TEMPORARY;
565
 
1090 palkovsky 566
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
1072 palkovsky 567
 
1258 palkovsky 568
    call = ipc_call_alloc(0);
1027 palkovsky 569
    IPC_SET_METHOD(call->data, method);
570
    IPC_SET_ARG1(call->data, arg1);
571
    IPC_SET_ARG2(call->data, arg2);
2618 jermar 572
    IPC_SET_ARG3(call->data, arg3);
573
    IPC_SET_ARG4(call->data, arg4);
2620 jermar 574
    /*
575
     * To achieve deterministic behavior, zero out arguments that are beyond
576
     * the limits of the fast version.
577
     */
578
    IPC_SET_ARG5(call->data, 0);
1027 palkovsky 579
 
3492 rimsky 580
    if (!(res = request_preprocess(call, phone)))
1090 palkovsky 581
        ipc_call(phone, call);
582
    else
583
        ipc_backsend_err(phone, call, res);
1027 palkovsky 584
 
1780 jermar 585
    return (unative_t) call;
1027 palkovsky 586
}
587
 
2471 jermar 588
/** Make an asynchronous IPC call allowing to transmit the entire payload.
1027 palkovsky 589
 *
2471 jermar 590
 * @param phoneid   Phone handle for the call.
591
 * @param data      Userspace address of call data with the request.
592
 *
593
 * @return      See sys_ipc_call_async_fast().
1027 palkovsky 594
 */
2618 jermar 595
unative_t sys_ipc_call_async_slow(unative_t phoneid, ipc_data_t *data)
1027 palkovsky 596
{
597
    call_t *call;
598
    phone_t *phone;
1090 palkovsky 599
    int res;
1288 jermar 600
    int rc;
1027 palkovsky 601
 
1072 palkovsky 602
    if (check_call_limit())
603
        return IPC_CALLRET_TEMPORARY;
604
 
1090 palkovsky 605
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
1027 palkovsky 606
 
1258 palkovsky 607
    call = ipc_call_alloc(0);
2359 jermar 608
    rc = copy_from_uspace(&call->data.args, &data->args,
609
        sizeof(call->data.args));
1293 palkovsky 610
    if (rc != 0) {
611
        ipc_call_free(call);
1780 jermar 612
        return (unative_t) rc;
1293 palkovsky 613
    }
3492 rimsky 614
    if (!(res = request_preprocess(call, phone)))
1090 palkovsky 615
        ipc_call(phone, call);
616
    else
617
        ipc_backsend_err(phone, call, res);
1040 palkovsky 618
 
1780 jermar 619
    return (unative_t) call;
1027 palkovsky 620
}
621
 
2471 jermar 622
/** Forward a received call to another destination.
1040 palkovsky 623
 *
2471 jermar 624
 * @param callid    Hash of the call to forward.
625
 * @param phoneid   Phone handle to use for forwarding.
626
 * @param method    New method to use for the forwarded call.
627
 * @param arg1      New value of the first argument for the forwarded call.
2636 jermar 628
 * @param arg2      New value of the second argument for the forwarded call.
2622 jermar 629
 * @param mode      Flags that specify mode of the forward operation.
1060 palkovsky 630
 *
2471 jermar 631
 * @return      Return 0 on succes, otherwise return an error code.
632
 *
2636 jermar 633
 * In case the original method is a system method, ARG1, ARG2 and ARG3 are
634
 * overwritten in the forwarded message with the new method and the new arg1 and
635
 * arg2, respectively. Otherwise the METHOD, ARG1 and ARG2 are rewritten with
636
 * the new method, arg1 and arg2, respectively. Also note there is a set of
637
 * immutable methods, for which the new method and argument is not set and
638
 * these values are ignored.
2471 jermar 639
 *
2622 jermar 640
 * Warning: When implementing support for changing additional payload
2636 jermar 641
 *      arguments, make sure that ARG5 is not rewritten for certain
2622 jermar 642
 *      system IPC
1040 palkovsky 643
 */
1780 jermar 644
unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
2635 cejka 645
    unative_t method, unative_t arg1, unative_t arg2, int mode)
1040 palkovsky 646
{
647
    call_t *call;
648
    phone_t *phone;
649
 
650
    call = get_call(callid);
651
    if (!call)
652
        return ENOENT;
653
 
1088 palkovsky 654
    call->flags |= IPC_CALL_FORWARDED;
655
 
1084 palkovsky 656
    GET_CHECK_PHONE(phone, phoneid, {
1040 palkovsky 657
        IPC_SET_RETVAL(call->data, EFORWARD);
658
        ipc_answer(&TASK->answerbox, call);
659
        return ENOENT;
3054 decky 660
    });
1040 palkovsky 661
 
2557 jermar 662
    if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
1040 palkovsky 663
        IPC_SET_RETVAL(call->data, EFORWARD);
664
        ipc_answer(&TASK->answerbox, call);
665
        return EPERM;
666
    }
667
 
2557 jermar 668
    /*
669
     * Userspace is not allowed to change method of system methods on
2635 cejka 670
     * forward, allow changing ARG1, ARG2 and ARG3 by means of method,
671
     * arg1 and arg2.
2557 jermar 672
     * If the method is immutable, don't change anything.
1040 palkovsky 673
     */
2557 jermar 674
    if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
675
        if (method_is_system(IPC_GET_METHOD(call->data))) {
676
            if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
2635 cejka 677
                phone_dealloc(IPC_GET_ARG5(call->data));
1090 palkovsky 678
 
2557 jermar 679
            IPC_SET_ARG1(call->data, method);
680
            IPC_SET_ARG2(call->data, arg1);
2635 cejka 681
            IPC_SET_ARG3(call->data, arg2);
2557 jermar 682
        } else {
683
            IPC_SET_METHOD(call->data, method);
684
            IPC_SET_ARG1(call->data, arg1);
2635 cejka 685
            IPC_SET_ARG2(call->data, arg2);
2557 jermar 686
        }
1040 palkovsky 687
    }
688
 
2622 jermar 689
    return ipc_forward(call, phone, &TASK->answerbox, mode);
1040 palkovsky 690
}
691
 
2471 jermar 692
/** Answer an IPC call - fast version.
693
 *
694
 * This function can handle only two return arguments of payload, but is faster
695
 * than the generic sys_ipc_answer().
696
 *
697
 * @param callid    Hash of the call to be answered.
698
 * @param retval    Return value of the answer.
699
 * @param arg1      Service-defined return value.
700
 * @param arg2      Service-defined return value.
2619 jermar 701
 * @param arg3      Service-defined return value.
702
 * @param arg4      Service-defined return value.
2471 jermar 703
 *
704
 * @return      Return 0 on success, otherwise return an error code.   
705
 */
2359 jermar 706
unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
2619 jermar 707
    unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
1027 palkovsky 708
{
709
    call_t *call;
1040 palkovsky 710
    ipc_data_t saved_data;
1088 palkovsky 711
    int saveddata = 0;
1329 palkovsky 712
    int rc;
1027 palkovsky 713
 
1346 palkovsky 714
    /* Do not answer notification callids */
715
    if (callid & IPC_CALLID_NOTIFICATION)
716
        return 0;
717
 
1040 palkovsky 718
    call = get_call(callid);
719
    if (!call)
720
        return ENOENT;
1027 palkovsky 721
 
1088 palkovsky 722
    if (answer_need_old(call)) {
1040 palkovsky 723
        memcpy(&saved_data, &call->data, sizeof(call->data));
1088 palkovsky 724
        saveddata = 1;
1027 palkovsky 725
    }
726
 
727
    IPC_SET_RETVAL(call->data, retval);
728
    IPC_SET_ARG1(call->data, arg1);
729
    IPC_SET_ARG2(call->data, arg2);
2619 jermar 730
    IPC_SET_ARG3(call->data, arg3);
731
    IPC_SET_ARG4(call->data, arg4);
2620 jermar 732
    /*
733
     * To achieve deterministic behavior, zero out arguments that are beyond
734
     * the limits of the fast version.
735
     */
736
    IPC_SET_ARG5(call->data, 0);
1329 palkovsky 737
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
1040 palkovsky 738
 
1027 palkovsky 739
    ipc_answer(&TASK->answerbox, call);
1329 palkovsky 740
    return rc;
1027 palkovsky 741
}
742
 
2471 jermar 743
/** Answer an IPC call.
744
 *
745
 * @param callid    Hash of the call to be answered.
746
 * @param data      Userspace address of call data with the answer.
747
 *
748
 * @return      Return 0 on success, otherwise return an error code.
749
 */
2619 jermar 750
unative_t sys_ipc_answer_slow(unative_t callid, ipc_data_t *data)
1027 palkovsky 751
{
752
    call_t *call;
1040 palkovsky 753
    ipc_data_t saved_data;
1088 palkovsky 754
    int saveddata = 0;
1288 jermar 755
    int rc;
1027 palkovsky 756
 
1346 palkovsky 757
    /* Do not answer notification callids */
758
    if (callid & IPC_CALLID_NOTIFICATION)
759
        return 0;
760
 
1040 palkovsky 761
    call = get_call(callid);
762
    if (!call)
763
        return ENOENT;
1027 palkovsky 764
 
1088 palkovsky 765
    if (answer_need_old(call)) {
1040 palkovsky 766
        memcpy(&saved_data, &call->data, sizeof(call->data));
1088 palkovsky 767
        saveddata = 1;
1027 palkovsky 768
    }
1288 jermar 769
    rc = copy_from_uspace(&call->data.args, &data->args,
2359 jermar 770
        sizeof(call->data.args));
1288 jermar 771
    if (rc != 0)
772
        return rc;
1027 palkovsky 773
 
1329 palkovsky 774
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
1027 palkovsky 775
 
776
    ipc_answer(&TASK->answerbox, call);
777
 
1329 palkovsky 778
    return rc;
1027 palkovsky 779
}
780
 
2471 jermar 781
/** Hang up a phone.
1086 palkovsky 782
 *
2471 jermar 783
 * @param       Phone handle of the phone to be hung up.
784
 *
785
 * @return      Return 0 on success or an error code.
1086 palkovsky 786
 */
1780 jermar 787
unative_t sys_ipc_hangup(int phoneid)
1086 palkovsky 788
{
789
    phone_t *phone;
790
 
791
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
792
 
1591 palkovsky 793
    if (ipc_phone_hangup(phone))
1086 palkovsky 794
        return -1;
795
 
796
    return 0;
797
}
798
 
2471 jermar 799
/** Wait for an incoming IPC call or an answer.
1027 palkovsky 800
 *
2359 jermar 801
 * @param calldata  Pointer to buffer where the call/answer data is stored.
802
 * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
803
 * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
804
 *          for explanation.
1364 jermar 805
 *
2471 jermar 806
 * @return      Hash of the call.
807
 *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
808
 *          call is a notification. IPC_CALLID_ANSWERED denotes an
809
 *          answer.
1027 palkovsky 810
 */
1780 jermar 811
unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
1027 palkovsky 812
{
813
    call_t *call;
814
 
3665 rimsky 815
restart:
816
 
817
#ifdef CONFIG_UDEBUG
818
    udebug_stoppable_begin();
819
#endif  
2359 jermar 820
    call = ipc_wait_for_call(&TASK->answerbox, usec,
821
        flags | SYNCH_FLAGS_INTERRUPTIBLE);
3665 rimsky 822
 
823
#ifdef CONFIG_UDEBUG
824
    udebug_stoppable_end();
825
#endif
1088 palkovsky 826
    if (!call)
827
        return 0;
1027 palkovsky 828
 
1258 palkovsky 829
    if (call->flags & IPC_CALL_NOTIF) {
830
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
1693 palkovsky 831
 
832
        /* Set in_phone_hash to the interrupt counter */
2098 decky 833
        call->data.phone = (void *) call->priv;
1693 palkovsky 834
 
835
        STRUCT_TO_USPACE(calldata, &call->data);
836
 
1258 palkovsky 837
        ipc_call_free(call);
838
 
2471 jermar 839
        return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
1258 palkovsky 840
    }
841
 
1027 palkovsky 842
    if (call->flags & IPC_CALL_ANSWERED) {
1090 palkovsky 843
        process_answer(call);
1027 palkovsky 844
 
1086 palkovsky 845
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
846
 
847
        if (call->flags & IPC_CALL_DISCARD_ANSWER) {
848
            ipc_call_free(call);
849
            goto restart;
3492 rimsky 850
        } else {
851
            /*
852
             * Decrement the counter of active calls only if the
853
             * call is not an answer to IPC_M_PHONE_HUNGUP,
854
             * which doesn't contribute to the counter.
855
             */
856
            atomic_dec(&TASK->active_calls);
1086 palkovsky 857
        }
858
 
859
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
1027 palkovsky 860
        ipc_call_free(call);
861
 
2471 jermar 862
        return ((unative_t) call) | IPC_CALLID_ANSWERED;
1027 palkovsky 863
    }
1086 palkovsky 864
 
1027 palkovsky 865
    if (process_request(&TASK->answerbox, call))
866
        goto restart;
1086 palkovsky 867
 
1090 palkovsky 868
    /* Include phone address('id') of the caller in the request,
869
     * copy whole call->data, not only call->data.args */
1396 palkovsky 870
    if (STRUCT_TO_USPACE(calldata, &call->data)) {
871
        return 0;
872
    }
1780 jermar 873
    return (unative_t)call;
1027 palkovsky 874
}
1258 palkovsky 875
 
2471 jermar 876
/** Connect an IRQ handler to a task.
1923 jermar 877
 *
2471 jermar 878
 * @param inr       IRQ number.
879
 * @param devno     Device number.
880
 * @param method    Method to be associated with the notification.
881
 * @param ucode     Uspace pointer to the top-half pseudocode.
1923 jermar 882
 *
2471 jermar 883
 * @return      EPERM or a return code returned by ipc_irq_register().
1923 jermar 884
 */
2359 jermar 885
unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
886
    irq_code_t *ucode)
1258 palkovsky 887
{
1297 jermar 888
    if (!(cap_get(TASK) & CAP_IRQ_REG))
889
        return EPERM;
890
 
1923 jermar 891
    return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
1258 palkovsky 892
}
893
 
2471 jermar 894
/** Disconnect an IRQ handler from a task.
1923 jermar 895
 *
2471 jermar 896
 * @param inr       IRQ number.
897
 * @param devno     Device number.
898
 *
899
 * @return      Zero on success or EPERM on error..
1923 jermar 900
 */
901
unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
1258 palkovsky 902
{
1297 jermar 903
    if (!(cap_get(TASK) & CAP_IRQ_REG))
904
        return EPERM;
905
 
1923 jermar 906
    ipc_irq_unregister(&TASK->answerbox, inr, devno);
1258 palkovsky 907
 
908
    return 0;
909
}
1702 cejka 910
 
3492 rimsky 911
#include <console/console.h>
912
 
913
/**
914
 * Syscall connect to a task by id.
915
 *
916
 * @return      Phone id on success, or negative error code.
917
 */
918
unative_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid_arg)
919
{
920
#ifdef CONFIG_UDEBUG
921
    sysarg64_t taskid_arg;
922
    int rc;
923
 
924
    rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
925
    if (rc != 0)
926
        return (unative_t) rc;
927
 
928
    LOG("sys_ipc_connect_kbox(%" PRIu64 ")\n", taskid_arg.value);
929
 
930
    return ipc_connect_kbox(taskid_arg.value);
931
#else
932
    return (unative_t) ENOTSUP;
933
#endif
934
}
935
 
1757 jermar 936
/** @}
1702 cejka 937
 */