Subversion Repositories HelenOS-historic

Rev

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