Subversion Repositories HelenOS

Rev

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