Subversion Repositories HelenOS

Rev

Rev 3431 | Rev 3471 | 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>
3433 svoboda 45
#include <ipc/ipc_kbox.h>
2813 svoboda 46
#include <udebug/udebug_ipc.h>
1258 palkovsky 47
#include <arch/interrupt.h>
1027 palkovsky 48
#include <print.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
         */
3042 svoboda 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);
3042 svoboda 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);
3011 svoboda 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
                }
3011 svoboda 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;
3425 svoboda 275
            size_t size;
276
            size_t max_size;
2494 jermar 277
 
3425 svoboda 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.
2801 svoboda 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
 */
2801 svoboda 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;
3431 svoboda 346
#ifdef CONFIG_UDEBUG
2812 svoboda 347
    case IPC_M_DEBUG_ALL:
2892 svoboda 348
        return udebug_request_preprocess(call, phone);
3431 svoboda 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. */
2812 svoboda 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);
2801 svoboda 410
    }
411
    switch (IPC_GET_METHOD(call->data)) {
2812 svoboda 412
    case IPC_M_DEBUG_ALL:
2801 svoboda 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;
3424 svoboda 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
 
2801 svoboda 458
    if (!(res = request_preprocess(&call, phone))) {
3425 svoboda 459
        rc = ipc_call_sync(phone, &call);
460
        if (rc != EOK)
461
            return rc;
1090 palkovsky 462
        process_answer(&call);
2471 jermar 463
    } else {
1090 palkovsky 464
        IPC_SET_RETVAL(call.data, res);
2471 jermar 465
    }
2617 jermar 466
    rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
467
    if (rc != 0)
468
        return rc;
1027 palkovsky 469
 
470
    return 0;
471
}
472
 
2471 jermar 473
/** Make a synchronous IPC call allowing to transmit the entire payload.
474
 *
475
 * @param phoneid   Phone handle for the call.
476
 * @param question  Userspace address of call data with the request.
2494 jermar 477
 * @param reply     Userspace address of call data where to store the
478
 *          answer.
2471 jermar 479
 *
480
 * @return      Zero on success or an error code.
481
 */
2615 jermar 482
unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question,
2359 jermar 483
    ipc_data_t *reply)
1027 palkovsky 484
{
485
    call_t call;
486
    phone_t *phone;
1090 palkovsky 487
    int res;
1288 jermar 488
    int rc;
1027 palkovsky 489
 
1084 palkovsky 490
    ipc_call_static_init(&call);
2359 jermar 491
    rc = copy_from_uspace(&call.data.args, &question->args,
492
        sizeof(call.data.args));
1288 jermar 493
    if (rc != 0)
1780 jermar 494
        return (unative_t) rc;
1040 palkovsky 495
 
1084 palkovsky 496
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
1072 palkovsky 497
 
2801 svoboda 498
    if (!(res = request_preprocess(&call, phone))) {
3425 svoboda 499
        rc = ipc_call_sync(phone, &call);
500
        if (rc != EOK)
501
            return rc;
1090 palkovsky 502
        process_answer(&call);
503
    } else
504
        IPC_SET_RETVAL(call.data, res);
1027 palkovsky 505
 
1288 jermar 506
    rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
507
    if (rc != 0)
508
        return rc;
1027 palkovsky 509
 
510
    return 0;
511
}
512
 
2471 jermar 513
/** Check that the task did not exceed the allowed limit of asynchronous calls.
1027 palkovsky 514
 *
2471 jermar 515
 * @return      Return 0 if limit not reached or -1 if limit exceeded.
1027 palkovsky 516
 */
517
static int check_call_limit(void)
518
{
519
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
520
        atomic_dec(&TASK->active_calls);
521
        return -1;
522
    }
523
    return 0;
524
}
525
 
2471 jermar 526
/** Make a fast asynchronous call over IPC.
1027 palkovsky 527
 *
2618 jermar 528
 * This function can only handle four arguments of payload, but is faster than
529
 * the generic function sys_ipc_call_async_slow().
2471 jermar 530
 *
531
 * @param phoneid   Phone handle for the call.
532
 * @param method    Method of the call.
533
 * @param arg1      Service-defined payload argument.
534
 * @param arg2      Service-defined payload argument.
2618 jermar 535
 * @param arg3      Service-defined payload argument.
536
 * @param arg4      Service-defined payload argument.
2471 jermar 537
 *
538
 * @return      Return call hash on success.
539
 *          Return IPC_CALLRET_FATAL in case of a fatal error and
540
 *          IPC_CALLRET_TEMPORARY if there are too many pending
541
 *          asynchronous requests; answers should be handled first.
1027 palkovsky 542
 */
2359 jermar 543
unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
2618 jermar 544
    unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
1027 palkovsky 545
{
546
    call_t *call;
547
    phone_t *phone;
1090 palkovsky 548
    int res;
1027 palkovsky 549
 
550
    if (check_call_limit())
551
        return IPC_CALLRET_TEMPORARY;
552
 
1090 palkovsky 553
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
1072 palkovsky 554
 
1258 palkovsky 555
    call = ipc_call_alloc(0);
1027 palkovsky 556
    IPC_SET_METHOD(call->data, method);
557
    IPC_SET_ARG1(call->data, arg1);
558
    IPC_SET_ARG2(call->data, arg2);
2618 jermar 559
    IPC_SET_ARG3(call->data, arg3);
560
    IPC_SET_ARG4(call->data, arg4);
2620 jermar 561
    /*
562
     * To achieve deterministic behavior, zero out arguments that are beyond
563
     * the limits of the fast version.
564
     */
565
    IPC_SET_ARG5(call->data, 0);
1027 palkovsky 566
 
2801 svoboda 567
    if (!(res = request_preprocess(call, phone)))
1090 palkovsky 568
        ipc_call(phone, call);
569
    else
570
        ipc_backsend_err(phone, call, res);
1027 palkovsky 571
 
1780 jermar 572
    return (unative_t) call;
1027 palkovsky 573
}
574
 
2471 jermar 575
/** Make an asynchronous IPC call allowing to transmit the entire payload.
1027 palkovsky 576
 *
2471 jermar 577
 * @param phoneid   Phone handle for the call.
578
 * @param data      Userspace address of call data with the request.
579
 *
580
 * @return      See sys_ipc_call_async_fast().
1027 palkovsky 581
 */
2618 jermar 582
unative_t sys_ipc_call_async_slow(unative_t phoneid, ipc_data_t *data)
1027 palkovsky 583
{
584
    call_t *call;
585
    phone_t *phone;
1090 palkovsky 586
    int res;
1288 jermar 587
    int rc;
1027 palkovsky 588
 
1072 palkovsky 589
    if (check_call_limit())
590
        return IPC_CALLRET_TEMPORARY;
591
 
1090 palkovsky 592
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
1027 palkovsky 593
 
1258 palkovsky 594
    call = ipc_call_alloc(0);
2359 jermar 595
    rc = copy_from_uspace(&call->data.args, &data->args,
596
        sizeof(call->data.args));
1293 palkovsky 597
    if (rc != 0) {
598
        ipc_call_free(call);
1780 jermar 599
        return (unative_t) rc;
1293 palkovsky 600
    }
2801 svoboda 601
    if (!(res = request_preprocess(call, phone)))
1090 palkovsky 602
        ipc_call(phone, call);
603
    else
604
        ipc_backsend_err(phone, call, res);
1040 palkovsky 605
 
1780 jermar 606
    return (unative_t) call;
1027 palkovsky 607
}
608
 
2471 jermar 609
/** Forward a received call to another destination.
1040 palkovsky 610
 *
2471 jermar 611
 * @param callid    Hash of the call to forward.
612
 * @param phoneid   Phone handle to use for forwarding.
613
 * @param method    New method to use for the forwarded call.
614
 * @param arg1      New value of the first argument for the forwarded call.
2636 jermar 615
 * @param arg2      New value of the second argument for the forwarded call.
2622 jermar 616
 * @param mode      Flags that specify mode of the forward operation.
1060 palkovsky 617
 *
2471 jermar 618
 * @return      Return 0 on succes, otherwise return an error code.
619
 *
2636 jermar 620
 * In case the original method is a system method, ARG1, ARG2 and ARG3 are
621
 * overwritten in the forwarded message with the new method and the new arg1 and
622
 * arg2, respectively. Otherwise the METHOD, ARG1 and ARG2 are rewritten with
623
 * the new method, arg1 and arg2, respectively. Also note there is a set of
624
 * immutable methods, for which the new method and argument is not set and
625
 * these values are ignored.
2471 jermar 626
 *
2622 jermar 627
 * Warning: When implementing support for changing additional payload
2636 jermar 628
 *      arguments, make sure that ARG5 is not rewritten for certain
2622 jermar 629
 *      system IPC
1040 palkovsky 630
 */
1780 jermar 631
unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
2635 cejka 632
    unative_t method, unative_t arg1, unative_t arg2, int mode)
1040 palkovsky 633
{
634
    call_t *call;
635
    phone_t *phone;
636
 
637
    call = get_call(callid);
638
    if (!call)
639
        return ENOENT;
640
 
1088 palkovsky 641
    call->flags |= IPC_CALL_FORWARDED;
642
 
1084 palkovsky 643
    GET_CHECK_PHONE(phone, phoneid, {
1040 palkovsky 644
        IPC_SET_RETVAL(call->data, EFORWARD);
645
        ipc_answer(&TASK->answerbox, call);
646
        return ENOENT;
3424 svoboda 647
    });
1040 palkovsky 648
 
2557 jermar 649
    if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
1040 palkovsky 650
        IPC_SET_RETVAL(call->data, EFORWARD);
651
        ipc_answer(&TASK->answerbox, call);
652
        return EPERM;
653
    }
654
 
2557 jermar 655
    /*
656
     * Userspace is not allowed to change method of system methods on
2635 cejka 657
     * forward, allow changing ARG1, ARG2 and ARG3 by means of method,
658
     * arg1 and arg2.
2557 jermar 659
     * If the method is immutable, don't change anything.
1040 palkovsky 660
     */
2557 jermar 661
    if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
662
        if (method_is_system(IPC_GET_METHOD(call->data))) {
663
            if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
2635 cejka 664
                phone_dealloc(IPC_GET_ARG5(call->data));
1090 palkovsky 665
 
2557 jermar 666
            IPC_SET_ARG1(call->data, method);
667
            IPC_SET_ARG2(call->data, arg1);
2635 cejka 668
            IPC_SET_ARG3(call->data, arg2);
2557 jermar 669
        } else {
670
            IPC_SET_METHOD(call->data, method);
671
            IPC_SET_ARG1(call->data, arg1);
2635 cejka 672
            IPC_SET_ARG2(call->data, arg2);
2557 jermar 673
        }
1040 palkovsky 674
    }
675
 
2622 jermar 676
    return ipc_forward(call, phone, &TASK->answerbox, mode);
1040 palkovsky 677
}
678
 
2471 jermar 679
/** Answer an IPC call - fast version.
680
 *
681
 * This function can handle only two return arguments of payload, but is faster
682
 * than the generic sys_ipc_answer().
683
 *
684
 * @param callid    Hash of the call to be answered.
685
 * @param retval    Return value of the answer.
686
 * @param arg1      Service-defined return value.
687
 * @param arg2      Service-defined return value.
2619 jermar 688
 * @param arg3      Service-defined return value.
689
 * @param arg4      Service-defined return value.
2471 jermar 690
 *
691
 * @return      Return 0 on success, otherwise return an error code.   
692
 */
2359 jermar 693
unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
2619 jermar 694
    unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
1027 palkovsky 695
{
696
    call_t *call;
1040 palkovsky 697
    ipc_data_t saved_data;
1088 palkovsky 698
    int saveddata = 0;
1329 palkovsky 699
    int rc;
1027 palkovsky 700
 
1346 palkovsky 701
    /* Do not answer notification callids */
702
    if (callid & IPC_CALLID_NOTIFICATION)
703
        return 0;
704
 
1040 palkovsky 705
    call = get_call(callid);
706
    if (!call)
707
        return ENOENT;
1027 palkovsky 708
 
1088 palkovsky 709
    if (answer_need_old(call)) {
1040 palkovsky 710
        memcpy(&saved_data, &call->data, sizeof(call->data));
1088 palkovsky 711
        saveddata = 1;
1027 palkovsky 712
    }
713
 
714
    IPC_SET_RETVAL(call->data, retval);
715
    IPC_SET_ARG1(call->data, arg1);
716
    IPC_SET_ARG2(call->data, arg2);
2619 jermar 717
    IPC_SET_ARG3(call->data, arg3);
718
    IPC_SET_ARG4(call->data, arg4);
2620 jermar 719
    /*
720
     * To achieve deterministic behavior, zero out arguments that are beyond
721
     * the limits of the fast version.
722
     */
723
    IPC_SET_ARG5(call->data, 0);
1329 palkovsky 724
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
1040 palkovsky 725
 
1027 palkovsky 726
    ipc_answer(&TASK->answerbox, call);
1329 palkovsky 727
    return rc;
1027 palkovsky 728
}
729
 
2471 jermar 730
/** Answer an IPC call.
731
 *
732
 * @param callid    Hash of the call to be answered.
733
 * @param data      Userspace address of call data with the answer.
734
 *
735
 * @return      Return 0 on success, otherwise return an error code.
736
 */
2619 jermar 737
unative_t sys_ipc_answer_slow(unative_t callid, ipc_data_t *data)
1027 palkovsky 738
{
739
    call_t *call;
1040 palkovsky 740
    ipc_data_t saved_data;
1088 palkovsky 741
    int saveddata = 0;
1288 jermar 742
    int rc;
1027 palkovsky 743
 
1346 palkovsky 744
    /* Do not answer notification callids */
745
    if (callid & IPC_CALLID_NOTIFICATION)
746
        return 0;
747
 
1040 palkovsky 748
    call = get_call(callid);
749
    if (!call)
750
        return ENOENT;
1027 palkovsky 751
 
1088 palkovsky 752
    if (answer_need_old(call)) {
1040 palkovsky 753
        memcpy(&saved_data, &call->data, sizeof(call->data));
1088 palkovsky 754
        saveddata = 1;
1027 palkovsky 755
    }
1288 jermar 756
    rc = copy_from_uspace(&call->data.args, &data->args,
2359 jermar 757
        sizeof(call->data.args));
1288 jermar 758
    if (rc != 0)
759
        return rc;
1027 palkovsky 760
 
1329 palkovsky 761
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
1027 palkovsky 762
 
763
    ipc_answer(&TASK->answerbox, call);
764
 
1329 palkovsky 765
    return rc;
1027 palkovsky 766
}
767
 
2471 jermar 768
/** Hang up a phone.
1086 palkovsky 769
 *
2471 jermar 770
 * @param       Phone handle of the phone to be hung up.
771
 *
772
 * @return      Return 0 on success or an error code.
1086 palkovsky 773
 */
1780 jermar 774
unative_t sys_ipc_hangup(int phoneid)
1086 palkovsky 775
{
776
    phone_t *phone;
777
 
778
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
779
 
1591 palkovsky 780
    if (ipc_phone_hangup(phone))
1086 palkovsky 781
        return -1;
782
 
783
    return 0;
784
}
785
 
2471 jermar 786
/** Wait for an incoming IPC call or an answer.
1027 palkovsky 787
 *
2359 jermar 788
 * @param calldata  Pointer to buffer where the call/answer data is stored.
789
 * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
790
 * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
791
 *          for explanation.
1364 jermar 792
 *
2471 jermar 793
 * @return      Hash of the call.
794
 *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
795
 *          call is a notification. IPC_CALLID_ANSWERED denotes an
796
 *          answer.
1027 palkovsky 797
 */
1780 jermar 798
unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
1027 palkovsky 799
{
800
    call_t *call;
801
 
802
restart:   
2359 jermar 803
    call = ipc_wait_for_call(&TASK->answerbox, usec,
804
        flags | SYNCH_FLAGS_INTERRUPTIBLE);
1088 palkovsky 805
    if (!call)
806
        return 0;
1027 palkovsky 807
 
1258 palkovsky 808
    if (call->flags & IPC_CALL_NOTIF) {
809
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
1693 palkovsky 810
 
811
        /* Set in_phone_hash to the interrupt counter */
2098 decky 812
        call->data.phone = (void *) call->priv;
1693 palkovsky 813
 
814
        STRUCT_TO_USPACE(calldata, &call->data);
815
 
1258 palkovsky 816
        ipc_call_free(call);
817
 
2471 jermar 818
        return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
1258 palkovsky 819
    }
820
 
1027 palkovsky 821
    if (call->flags & IPC_CALL_ANSWERED) {
1090 palkovsky 822
        process_answer(call);
1027 palkovsky 823
 
1086 palkovsky 824
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
825
 
1027 palkovsky 826
        atomic_dec(&TASK->active_calls);
1086 palkovsky 827
 
828
        if (call->flags & IPC_CALL_DISCARD_ANSWER) {
829
            ipc_call_free(call);
830
            goto restart;
831
        }
832
 
833
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
1027 palkovsky 834
        ipc_call_free(call);
835
 
2471 jermar 836
        return ((unative_t) call) | IPC_CALLID_ANSWERED;
1027 palkovsky 837
    }
1086 palkovsky 838
 
1027 palkovsky 839
    if (process_request(&TASK->answerbox, call))
840
        goto restart;
1086 palkovsky 841
 
1090 palkovsky 842
    /* Include phone address('id') of the caller in the request,
843
     * copy whole call->data, not only call->data.args */
1396 palkovsky 844
    if (STRUCT_TO_USPACE(calldata, &call->data)) {
845
        return 0;
846
    }
1780 jermar 847
    return (unative_t)call;
1027 palkovsky 848
}
1258 palkovsky 849
 
2471 jermar 850
/** Connect an IRQ handler to a task.
1923 jermar 851
 *
2471 jermar 852
 * @param inr       IRQ number.
853
 * @param devno     Device number.
854
 * @param method    Method to be associated with the notification.
855
 * @param ucode     Uspace pointer to the top-half pseudocode.
1923 jermar 856
 *
2471 jermar 857
 * @return      EPERM or a return code returned by ipc_irq_register().
1923 jermar 858
 */
2359 jermar 859
unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
860
    irq_code_t *ucode)
1258 palkovsky 861
{
1297 jermar 862
    if (!(cap_get(TASK) & CAP_IRQ_REG))
863
        return EPERM;
864
 
1923 jermar 865
    return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
1258 palkovsky 866
}
867
 
2471 jermar 868
/** Disconnect an IRQ handler from a task.
1923 jermar 869
 *
2471 jermar 870
 * @param inr       IRQ number.
871
 * @param devno     Device number.
872
 *
873
 * @return      Zero on success or EPERM on error..
1923 jermar 874
 */
875
unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
1258 palkovsky 876
{
1297 jermar 877
    if (!(cap_get(TASK) & CAP_IRQ_REG))
878
        return EPERM;
879
 
1923 jermar 880
    ipc_irq_unregister(&TASK->answerbox, inr, devno);
1258 palkovsky 881
 
882
    return 0;
883
}
1702 cejka 884
 
3424 svoboda 885
#include <console/console.h>
2801 svoboda 886
 
887
/**
888
 * Syscall connect to a task by id.
889
 *
890
 * @return      Phone id on success, or negative error code.
891
 */
2808 svoboda 892
unative_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid_arg)
2801 svoboda 893
{
3431 svoboda 894
#ifdef CONFIG_UDEBUG
2801 svoboda 895
    sysarg64_t taskid_arg;
896
    int rc;
897
 
898
    rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
899
    if (rc != 0)
900
        return (unative_t) rc;
901
 
3424 svoboda 902
    printf("sys_ipc_connect_kbox(%lld, %d)\n", taskid_arg.value);
2801 svoboda 903
 
2808 svoboda 904
    return ipc_connect_kbox(taskid_arg.value);
3431 svoboda 905
#else
906
    return (unative_t) ENOTSUP;
907
#endif
2801 svoboda 908
}
909
 
1757 jermar 910
/** @}
1702 cejka 911
 */