Subversion Repositories HelenOS

Rev

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