Subversion Repositories HelenOS-historic

Rev

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

Rev Author Line No. Line
1027 palkovsky 1
/*
2
 * Copyright (C) 2006 Ondrej 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
 
29
#include <arch.h>
30
#include <proc/task.h>
1288 jermar 31
#include <proc/thread.h>
1027 palkovsky 32
#include <errno.h>
33
#include <memstr.h>
34
#include <debug.h>
35
#include <ipc/ipc.h>
36
#include <ipc/sysipc.h>
1281 palkovsky 37
#include <ipc/irq.h>
1072 palkovsky 38
#include <ipc/ipcrsc.h>
1258 palkovsky 39
#include <arch/interrupt.h>
1027 palkovsky 40
#include <print.h>
1288 jermar 41
#include <syscall/copy.h>
1297 jermar 42
#include <security/cap.h>
1027 palkovsky 43
 
1084 palkovsky 44
#define GET_CHECK_PHONE(phone,phoneid,err) { \
45
      if (phoneid > IPC_MAX_PHONES) { err; } \
46
      phone = &TASK->phones[phoneid]; \
47
}
48
 
1090 palkovsky 49
#define STRUCT_TO_USPACE(dst,src) copy_to_uspace(dst,src,sizeof(*(src)))
1084 palkovsky 50
 
1040 palkovsky 51
/** Return true if the method is a system method */
52
static inline int is_system_method(__native method)
53
{
54
    if (method <= IPC_M_LAST_SYSTEM)
55
        return 1;
56
    return 0;
57
}
58
 
59
/** Return true if the message with this method is forwardable
60
 *
61
 * - some system messages may be forwarded, for some of them
62
 *   it is useless
63
 */
64
static inline int is_forwardable(__native method)
65
{
1086 palkovsky 66
    if (method == IPC_M_PHONE_HUNGUP)
67
        return 0; /* This message is meant only for the receiver */
1040 palkovsky 68
    return 1;
69
}
70
 
1027 palkovsky 71
/****************************************************/
72
/* Functions that preprocess answer before sending
73
 * it to the recepient
74
 */
75
 
76
/** Return true if the caller (ipc_answer) should save
1088 palkovsky 77
 * the old call contents for answer_preprocess
1027 palkovsky 78
 */
1088 palkovsky 79
static inline int answer_need_old(call_t *call)
1027 palkovsky 80
{
1086 palkovsky 81
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
1027 palkovsky 82
        return 1;
1086 palkovsky 83
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_ME_TO)
1060 palkovsky 84
        return 1;
1027 palkovsky 85
    return 0;
86
}
87
 
88
/** Interpret process answer as control information */
1040 palkovsky 89
static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata)
1027 palkovsky 90
{
91
    int phoneid;
92
 
1088 palkovsky 93
    if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
1141 palkovsky 94
        /* In case of forward, hangup the forwared phone,
95
         * not the originator
96
         */
97
        spinlock_lock(&answer->data.phone->lock);
98
        spinlock_lock(&TASK->answerbox.lock);
99
        if (answer->data.phone->callee) {
100
            list_remove(&answer->data.phone->list);
101
            answer->data.phone->callee = 0;
102
        }
103
        spinlock_unlock(&TASK->answerbox.lock);
104
        spinlock_unlock(&answer->data.phone->lock);
1088 palkovsky 105
    }
106
 
107
    if (!olddata)
108
        return;
109
 
1086 palkovsky 110
    if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
1040 palkovsky 111
        phoneid = IPC_GET_ARG3(*olddata);
1027 palkovsky 112
        if (IPC_GET_RETVAL(answer->data)) {
113
            /* The connection was not accepted */
114
            phone_dealloc(phoneid);
1040 palkovsky 115
        } else {
1090 palkovsky 116
            /* The connection was accepted */
1040 palkovsky 117
            phone_connect(phoneid,&answer->sender->answerbox);
1090 palkovsky 118
            /* Set 'phone identification' as arg3 of response */
119
            IPC_SET_ARG3(answer->data, (__native)&TASK->phones[phoneid]);
1027 palkovsky 120
        }
1086 palkovsky 121
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
1060 palkovsky 122
        /* If the users accepted call, connect */
123
        if (!IPC_GET_RETVAL(answer->data)) {
124
            ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata),
125
                      &TASK->answerbox);
126
        }
1027 palkovsky 127
    }
128
}
129
 
1090 palkovsky 130
/** Called before the request is sent
131
 *
132
 * @return 0 - no error, -1 - report error to user
133
 */
134
static int request_preprocess(call_t *call)
135
{
136
    int newphid;
137
 
138
    switch (IPC_GET_METHOD(call->data)) {
139
    case IPC_M_CONNECT_ME_TO:
140
        newphid = phone_alloc();
141
        if (newphid < 0)
142
            return ELIMIT;
143
        /* Set arg3 for server */
144
        IPC_SET_ARG3(call->data, (__native)&TASK->phones[newphid]);
145
        call->flags |= IPC_CALL_CONN_ME_TO;
146
        call->private = newphid;
147
        break;
148
    default:
149
        break;
150
    }
151
    return 0;
152
}
153
 
1027 palkovsky 154
/****************************************************/
155
/* Functions called to process received call/answer
156
 * before passing to uspace
157
 */
158
 
159
/** Do basic kernel processing of received call answer */
1090 palkovsky 160
static void process_answer(call_t *call)
1027 palkovsky 161
{
1088 palkovsky 162
    if (IPC_GET_RETVAL(call->data) == EHANGUP && \
163
        call->flags & IPC_CALL_FORWARDED)
164
        IPC_SET_RETVAL(call->data, EFORWARD);
1090 palkovsky 165
 
166
    if (call->flags & IPC_CALL_CONN_ME_TO) {
167
        if (IPC_GET_RETVAL(call->data))
168
            phone_dealloc(call->private);
169
        else
170
            IPC_SET_ARG3(call->data, call->private);
171
    }
1027 palkovsky 172
}
173
 
174
/** Do basic kernel processing of received call request
175
 *
176
 * @return 0 - the call should be passed to userspace, 1 - ignore call
177
 */
178
static int process_request(answerbox_t *box,call_t *call)
179
{
180
    int phoneid;
181
 
1086 palkovsky 182
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
1040 palkovsky 183
        phoneid = phone_alloc();
1027 palkovsky 184
        if (phoneid < 0) { /* Failed to allocate phone */
185
            IPC_SET_RETVAL(call->data, ELIMIT);
186
            ipc_answer(box,call);
187
            return -1;
188
        }
189
        IPC_SET_ARG3(call->data, phoneid);
190
    }
191
    return 0;
192
}
193
 
194
/** Send a call over IPC, wait for reply, return to user
195
 *
196
 * @return Call identification, returns -1 on fatal error,
197
           -2 on 'Too many async request, handle answers first
198
 */
199
__native sys_ipc_call_sync_fast(__native phoneid, __native method,
1086 palkovsky 200
                __native arg1, ipc_data_t *data)
1027 palkovsky 201
{
202
    call_t call;
203
    phone_t *phone;
1090 palkovsky 204
    int res;
1027 palkovsky 205
 
1084 palkovsky 206
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
1027 palkovsky 207
 
1084 palkovsky 208
    ipc_call_static_init(&call);
1027 palkovsky 209
    IPC_SET_METHOD(call.data, method);
210
    IPC_SET_ARG1(call.data, arg1);
211
 
1090 palkovsky 212
    if (!(res=request_preprocess(&call))) {
213
        ipc_call_sync(phone, &call);
214
        process_answer(&call);
215
    } else
216
        IPC_SET_RETVAL(call.data, res);
1086 palkovsky 217
    STRUCT_TO_USPACE(&data->args, &call.data.args);
1027 palkovsky 218
 
219
    return 0;
220
}
221
 
222
/** Synchronous IPC call allowing to send whole message */
1086 palkovsky 223
__native sys_ipc_call_sync(__native phoneid, ipc_data_t *question,
224
               ipc_data_t *reply)
1027 palkovsky 225
{
226
    call_t call;
227
    phone_t *phone;
1090 palkovsky 228
    int res;
1288 jermar 229
    int rc;
1027 palkovsky 230
 
1084 palkovsky 231
    ipc_call_static_init(&call);
1288 jermar 232
    rc = copy_from_uspace(&call.data.args, &question->args, sizeof(call.data.args));
233
    if (rc != 0)
234
        return (__native) rc;
1040 palkovsky 235
 
1084 palkovsky 236
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
1072 palkovsky 237
 
1090 palkovsky 238
    if (!(res=request_preprocess(&call))) {
239
        ipc_call_sync(phone, &call);
240
        process_answer(&call);
241
    } else
242
        IPC_SET_RETVAL(call.data, res);
1027 palkovsky 243
 
1288 jermar 244
    rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
245
    if (rc != 0)
246
        return rc;
1027 palkovsky 247
 
248
    return 0;
249
}
250
 
251
/** Check that the task did not exceed allowed limit
252
 *
253
 * @return 0 - Limit OK,   -1 - limit exceeded
254
 */
255
static int check_call_limit(void)
256
{
257
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
258
        atomic_dec(&TASK->active_calls);
259
        return -1;
260
    }
261
    return 0;
262
}
263
 
264
/** Send an asynchronous call over ipc
265
 *
266
 * @return Call identification, returns -1 on fatal error,
267
           -2 on 'Too many async request, handle answers first
268
 */
269
__native sys_ipc_call_async_fast(__native phoneid, __native method,
270
                 __native arg1, __native arg2)
271
{
272
    call_t *call;
273
    phone_t *phone;
1090 palkovsky 274
    int res;
1027 palkovsky 275
 
276
    if (check_call_limit())
277
        return IPC_CALLRET_TEMPORARY;
278
 
1090 palkovsky 279
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
1072 palkovsky 280
 
1258 palkovsky 281
    call = ipc_call_alloc(0);
1027 palkovsky 282
    IPC_SET_METHOD(call->data, method);
283
    IPC_SET_ARG1(call->data, arg1);
284
    IPC_SET_ARG2(call->data, arg2);
285
 
1090 palkovsky 286
    if (!(res=request_preprocess(call)))
287
        ipc_call(phone, call);
288
    else
289
        ipc_backsend_err(phone, call, res);
1027 palkovsky 290
 
291
    return (__native) call;
292
}
293
 
294
/** Synchronous IPC call allowing to send whole message
295
 *
296
 * @return The same as sys_ipc_call_async
297
 */
1086 palkovsky 298
__native sys_ipc_call_async(__native phoneid, ipc_data_t *data)
1027 palkovsky 299
{
300
    call_t *call;
301
    phone_t *phone;
1090 palkovsky 302
    int res;
1288 jermar 303
    int rc;
1027 palkovsky 304
 
1072 palkovsky 305
    if (check_call_limit())
306
        return IPC_CALLRET_TEMPORARY;
307
 
1090 palkovsky 308
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
1027 palkovsky 309
 
1258 palkovsky 310
    call = ipc_call_alloc(0);
1288 jermar 311
    rc = copy_from_uspace(&call->data.args, &data->args, sizeof(call->data.args));
1293 palkovsky 312
    if (rc != 0) {
313
        ipc_call_free(call);
1288 jermar 314
        return (__native) rc;
1293 palkovsky 315
    }
1090 palkovsky 316
    if (!(res=request_preprocess(call)))
317
        ipc_call(phone, call);
318
    else
319
        ipc_backsend_err(phone, call, res);
1040 palkovsky 320
 
1027 palkovsky 321
    return (__native) call;
322
}
323
 
1040 palkovsky 324
/** Forward received call to another destination
325
 *
326
 * The arg1 and arg2 are changed in the forwarded message
1060 palkovsky 327
 *
328
 * Warning: If implementing non-fast version, make sure that
329
 *          arg3 is not rewritten for certain system IPC
1040 palkovsky 330
 */
331
__native sys_ipc_forward_fast(__native callid, __native phoneid,
332
                  __native method, __native arg1)
333
{
334
    call_t *call;
335
    phone_t *phone;
336
 
337
    call = get_call(callid);
338
    if (!call)
339
        return ENOENT;
340
 
1088 palkovsky 341
    call->flags |= IPC_CALL_FORWARDED;
342
 
1084 palkovsky 343
    GET_CHECK_PHONE(phone, phoneid, {
1040 palkovsky 344
        IPC_SET_RETVAL(call->data, EFORWARD);
345
        ipc_answer(&TASK->answerbox, call);
346
        return ENOENT;
1084 palkovsky 347
    });    
1040 palkovsky 348
 
349
    if (!is_forwardable(IPC_GET_METHOD(call->data))) {
350
        IPC_SET_RETVAL(call->data, EFORWARD);
351
        ipc_answer(&TASK->answerbox, call);
352
        return EPERM;
353
    }
354
 
355
    /* Userspace is not allowed to change method of system methods
356
     * on forward, allow changing ARG1 and ARG2 by means of method and arg1
357
     */
358
    if (is_system_method(IPC_GET_METHOD(call->data))) {
1090 palkovsky 359
        if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
360
            phone_dealloc(IPC_GET_ARG3(call->data));
361
 
1040 palkovsky 362
        IPC_SET_ARG1(call->data, method);
363
        IPC_SET_ARG2(call->data, arg1);
364
    } else {
365
        IPC_SET_METHOD(call->data, method);
366
        IPC_SET_ARG1(call->data, arg1);
367
    }
368
 
1088 palkovsky 369
    return ipc_forward(call, phone, &TASK->answerbox);
1040 palkovsky 370
}
371
 
1027 palkovsky 372
/** Send IPC answer */
373
__native sys_ipc_answer_fast(__native callid, __native retval,
374
                 __native arg1, __native arg2)
375
{
376
    call_t *call;
1040 palkovsky 377
    ipc_data_t saved_data;
1088 palkovsky 378
    int saveddata = 0;
1027 palkovsky 379
 
1040 palkovsky 380
    call = get_call(callid);
381
    if (!call)
382
        return ENOENT;
1027 palkovsky 383
 
1088 palkovsky 384
    if (answer_need_old(call)) {
1040 palkovsky 385
        memcpy(&saved_data, &call->data, sizeof(call->data));
1088 palkovsky 386
        saveddata = 1;
1027 palkovsky 387
    }
388
 
389
    IPC_SET_RETVAL(call->data, retval);
390
    IPC_SET_ARG1(call->data, arg1);
391
    IPC_SET_ARG2(call->data, arg2);
1088 palkovsky 392
    answer_preprocess(call, saveddata ? &saved_data : NULL);
1040 palkovsky 393
 
1027 palkovsky 394
    ipc_answer(&TASK->answerbox, call);
395
    return 0;
396
}
397
 
398
/** Send IPC answer */
1086 palkovsky 399
__native sys_ipc_answer(__native callid, ipc_data_t *data)
1027 palkovsky 400
{
401
    call_t *call;
1040 palkovsky 402
    ipc_data_t saved_data;
1088 palkovsky 403
    int saveddata = 0;
1288 jermar 404
    int rc;
1027 palkovsky 405
 
1040 palkovsky 406
    call = get_call(callid);
407
    if (!call)
408
        return ENOENT;
1027 palkovsky 409
 
1088 palkovsky 410
    if (answer_need_old(call)) {
1040 palkovsky 411
        memcpy(&saved_data, &call->data, sizeof(call->data));
1088 palkovsky 412
        saveddata = 1;
1027 palkovsky 413
    }
1288 jermar 414
    rc = copy_from_uspace(&call->data.args, &data->args,
1086 palkovsky 415
             sizeof(call->data.args));
1288 jermar 416
    if (rc != 0)
417
        return rc;
1027 palkovsky 418
 
1088 palkovsky 419
    answer_preprocess(call, saveddata ? &saved_data : NULL);
1027 palkovsky 420
 
421
    ipc_answer(&TASK->answerbox, call);
422
 
423
    return 0;
424
}
425
 
1086 palkovsky 426
/** Hang up the phone
427
 *
428
 */
429
__native sys_ipc_hangup(int phoneid)
430
{
431
    phone_t *phone;
432
 
433
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
434
 
435
    if (ipc_phone_hangup(phone))
436
        return -1;
437
 
438
    return 0;
439
}
440
 
1027 palkovsky 441
/** Wait for incoming ipc call or answer
442
 *
1086 palkovsky 443
 * @param calldata Pointer to buffer where the call/answer data is stored
1027 palkovsky 444
 * @param flags
445
 * @return Callid, if callid & 1, then the call is answer
446
 */
1090 palkovsky 447
__native sys_ipc_wait_for_call(ipc_data_t *calldata, __native flags)
1027 palkovsky 448
{
449
    call_t *call;
450
 
451
restart:   
452
    call = ipc_wait_for_call(&TASK->answerbox, flags);
1088 palkovsky 453
    if (!call)
454
        return 0;
1027 palkovsky 455
 
1258 palkovsky 456
    if (call->flags & IPC_CALL_NOTIF) {
457
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
458
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
459
        ipc_call_free(call);
460
 
461
        return ((__native)call) | IPC_CALLID_NOTIFICATION;
462
    }
463
 
1027 palkovsky 464
    if (call->flags & IPC_CALL_ANSWERED) {
1090 palkovsky 465
        process_answer(call);
1027 palkovsky 466
 
1086 palkovsky 467
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
468
 
1027 palkovsky 469
        atomic_dec(&TASK->active_calls);
1086 palkovsky 470
 
471
        if (call->flags & IPC_CALL_DISCARD_ANSWER) {
472
            ipc_call_free(call);
473
            goto restart;
474
        }
475
 
476
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
1027 palkovsky 477
        ipc_call_free(call);
478
 
479
        return ((__native)call) | IPC_CALLID_ANSWERED;
480
    }
1086 palkovsky 481
 
1027 palkovsky 482
    if (process_request(&TASK->answerbox, call))
483
        goto restart;
1086 palkovsky 484
 
1090 palkovsky 485
    /* Include phone address('id') of the caller in the request,
486
     * copy whole call->data, not only call->data.args */
1086 palkovsky 487
    STRUCT_TO_USPACE(calldata, &call->data);
1027 palkovsky 488
    return (__native)call;
489
}
1258 palkovsky 490
 
491
/** Connect irq handler to task */
1281 palkovsky 492
__native sys_ipc_register_irq(__native irq, irq_code_t *ucode)
1258 palkovsky 493
{
1297 jermar 494
    if (!(cap_get(TASK) & CAP_IRQ_REG))
495
        return EPERM;
496
 
1258 palkovsky 497
    if (irq >= IRQ_COUNT)
1297 jermar 498
        return (__native) ELIMIT;
1258 palkovsky 499
 
500
    irq_ipc_bind_arch(irq);
1281 palkovsky 501
 
502
    return ipc_irq_register(&TASK->answerbox, irq, ucode);
1258 palkovsky 503
}
504
 
505
/* Disconnect irq handler from task */
506
__native sys_ipc_unregister_irq(__native irq)
507
{
1297 jermar 508
    if (!(cap_get(TASK) & CAP_IRQ_REG))
509
        return EPERM;
510
 
1258 palkovsky 511
    if (irq >= IRQ_COUNT)
1297 jermar 512
        return (__native) ELIMIT;
1258 palkovsky 513
 
514
    ipc_irq_unregister(&TASK->answerbox, irq);
515
 
516
    return 0;
517
}