Subversion Repositories HelenOS

Rev

Rev 1084 | Rev 1088 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1084 Rev 1086
Line 40... Line 40...
40
 
40
 
41
#include <print.h>
41
#include <print.h>
42
#include <arch.h>
42
#include <arch.h>
43
#include <proc/thread.h>
43
#include <proc/thread.h>
44
 
44
 
45
/* TODO: multi-threaded connect-to-me can cause race condition
-
 
46
 * on phone, add counter + thread_kill??
-
 
47
 *
-
 
48
 */
-
 
49
 
-
 
50
#define GET_CHECK_PHONE(phone,phoneid,err) { \
45
#define GET_CHECK_PHONE(phone,phoneid,err) { \
51
      if (phoneid > IPC_MAX_PHONES) { err; } \
46
      if (phoneid > IPC_MAX_PHONES) { err; } \
52
      phone = &TASK->phones[phoneid]; \
47
      phone = &TASK->phones[phoneid]; \
53
}
48
}
54
 
49
 
-
 
50
#define STRUCT_TO_USPACE(dst,src) copy_to_uspace(dst,src,sizeof(*src))
55
 
51
 
56
/** Return true if the method is a system method */
52
/** Return true if the method is a system method */
57
static inline int is_system_method(__native method)
53
static inline int is_system_method(__native method)
58
{
54
{
59
    if (method <= IPC_M_LAST_SYSTEM)
55
    if (method <= IPC_M_LAST_SYSTEM)
Line 66... Line 62...
66
 * - some system messages may be forwarded, for some of them
62
 * - some system messages may be forwarded, for some of them
67
 *   it is useless
63
 *   it is useless
68
 */
64
 */
69
static inline int is_forwardable(__native method)
65
static inline int is_forwardable(__native method)
70
{
66
{
-
 
67
    if (method == IPC_M_PHONE_HUNGUP)
-
 
68
        return 0; /* This message is meant only for the receiver */
71
    return 1;
69
    return 1;
72
}
70
}
73
 
71
 
74
/****************************************************/
72
/****************************************************/
75
/* Functions that preprocess answer before sending
73
/* Functions that preprocess answer before sending
Line 79... Line 77...
79
/** Return true if the caller (ipc_answer) should save
77
/** Return true if the caller (ipc_answer) should save
80
 * the old call contents and call answer_preprocess
78
 * the old call contents and call answer_preprocess
81
 */
79
 */
82
static inline int answer_will_preprocess(call_t *call)
80
static inline int answer_will_preprocess(call_t *call)
83
{
81
{
84
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME)
82
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
85
        return 1;
83
        return 1;
86
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTMETO)
84
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_ME_TO)
87
        return 1;
85
        return 1;
88
    return 0;
86
    return 0;
89
}
87
}
90
 
88
 
91
/** Interpret process answer as control information */
89
/** Interpret process answer as control information */
92
static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata)
90
static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata)
93
{
91
{
94
    int phoneid;
92
    int phoneid;
95
 
93
 
96
    if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTTOME) {
94
    if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
97
        phoneid = IPC_GET_ARG3(*olddata);
95
        phoneid = IPC_GET_ARG3(*olddata);
98
        if (IPC_GET_RETVAL(answer->data)) {
96
        if (IPC_GET_RETVAL(answer->data)) {
99
            /* The connection was not accepted */
97
            /* The connection was not accepted */
100
            phone_dealloc(phoneid);
98
            phone_dealloc(phoneid);
101
        } else {
99
        } else {
102
            /* The connection was accepted */
100
            /* The connection was accepted */
103
            phone_connect(phoneid,&answer->sender->answerbox);
101
            phone_connect(phoneid,&answer->sender->answerbox);
104
        }
102
        }
105
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTMETO) {
103
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
106
        /* If the users accepted call, connect */
104
        /* If the users accepted call, connect */
107
        if (!IPC_GET_RETVAL(answer->data)) {
105
        if (!IPC_GET_RETVAL(answer->data)) {
108
            ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata),
106
            ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata),
109
                      &TASK->answerbox);
107
                      &TASK->answerbox);
110
        }
108
        }
Line 128... Line 126...
128
 */
126
 */
129
static int process_request(answerbox_t *box,call_t *call)
127
static int process_request(answerbox_t *box,call_t *call)
130
{
128
{
131
    int phoneid;
129
    int phoneid;
132
 
130
 
133
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) {
131
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
134
        phoneid = phone_alloc();
132
        phoneid = phone_alloc();
135
        if (phoneid < 0) { /* Failed to allocate phone */
133
        if (phoneid < 0) { /* Failed to allocate phone */
136
            IPC_SET_RETVAL(call->data, ELIMIT);
134
            IPC_SET_RETVAL(call->data, ELIMIT);
137
            ipc_answer(box,call);
135
            ipc_answer(box,call);
138
            return -1;
136
            return -1;
Line 146... Line 144...
146
 *
144
 *
147
 * @return Call identification, returns -1 on fatal error,
145
 * @return Call identification, returns -1 on fatal error,
148
           -2 on 'Too many async request, handle answers first
146
           -2 on 'Too many async request, handle answers first
149
 */
147
 */
150
__native sys_ipc_call_sync_fast(__native phoneid, __native method,
148
__native sys_ipc_call_sync_fast(__native phoneid, __native method,
151
                __native arg1, __native *data)
149
                __native arg1, ipc_data_t *data)
152
{
150
{
153
    call_t call;
151
    call_t call;
154
    phone_t *phone;
152
    phone_t *phone;
155
 
153
 
156
    if (is_system_method(method))
154
    if (is_system_method(method))
Line 162... Line 160...
162
    IPC_SET_METHOD(call.data, method);
160
    IPC_SET_METHOD(call.data, method);
163
    IPC_SET_ARG1(call.data, arg1);
161
    IPC_SET_ARG1(call.data, arg1);
164
   
162
   
165
    ipc_call_sync(phone, &call);
163
    ipc_call_sync(phone, &call);
166
 
164
 
167
    copy_to_uspace(data, &call.data, sizeof(call.data));
165
    STRUCT_TO_USPACE(&data->args, &call.data.args);
168
 
166
 
169
    return 0;
167
    return 0;
170
}
168
}
171
 
169
 
172
/** Synchronous IPC call allowing to send whole message */
170
/** Synchronous IPC call allowing to send whole message */
173
__native sys_ipc_call_sync(__native phoneid, __native *question,
171
__native sys_ipc_call_sync(__native phoneid, ipc_data_t *question,
174
               __native *reply)
172
               ipc_data_t *reply)
175
{
173
{
176
    call_t call;
174
    call_t call;
177
    phone_t *phone;
175
    phone_t *phone;
178
 
176
 
179
    ipc_call_static_init(&call);
177
    ipc_call_static_init(&call);
180
    copy_from_uspace(&call.data, question, sizeof(call.data));
178
    copy_from_uspace(&call.data.args, &question->args, sizeof(call.data.args));
181
 
179
 
182
    if (is_system_method(IPC_GET_METHOD(call.data)))
180
    if (is_system_method(IPC_GET_METHOD(call.data)))
183
        return EPERM;
181
        return EPERM;
184
   
182
   
185
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
183
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
186
 
184
 
187
    ipc_call_sync(phone, &call);
185
    ipc_call_sync(phone, &call);
188
 
186
 
189
    copy_to_uspace(reply, &call.data, sizeof(call.data));
187
    STRUCT_TO_USPACE(&reply->args, &call.data.args);
190
 
188
 
191
    return 0;
189
    return 0;
192
}
190
}
193
 
191
 
194
/** Check that the task did not exceed allowed limit
192
/** Check that the task did not exceed allowed limit
Line 235... Line 233...
235
 
233
 
236
/** Synchronous IPC call allowing to send whole message
234
/** Synchronous IPC call allowing to send whole message
237
 *
235
 *
238
 * @return The same as sys_ipc_call_async
236
 * @return The same as sys_ipc_call_async
239
 */
237
 */
240
__native sys_ipc_call_async(__native phoneid, __native *data)
238
__native sys_ipc_call_async(__native phoneid, ipc_data_t *data)
241
{
239
{
242
    call_t *call;
240
    call_t *call;
243
    phone_t *phone;
241
    phone_t *phone;
244
 
242
 
245
    if (check_call_limit())
243
    if (check_call_limit())
246
        return IPC_CALLRET_TEMPORARY;
244
        return IPC_CALLRET_TEMPORARY;
247
 
245
 
248
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
246
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
249
 
247
 
250
    call = ipc_call_alloc();
248
    call = ipc_call_alloc();
251
    copy_from_uspace(&call->data, data, sizeof(call->data));
249
    copy_from_uspace(&call->data.args, &data->args, sizeof(call->data.args));
252
 
250
 
253
    if (is_system_method(IPC_GET_METHOD(call->data))) {
251
    if (is_system_method(IPC_GET_METHOD(call->data))) {
254
        ipc_call_free(call);
252
        ipc_call_free(call);
255
        return EPERM;
253
        return EPERM;
256
    }
254
    }
Line 332... Line 330...
332
    ipc_answer(&TASK->answerbox, call);
330
    ipc_answer(&TASK->answerbox, call);
333
    return 0;
331
    return 0;
334
}
332
}
335
 
333
 
336
/** Send IPC answer */
334
/** Send IPC answer */
337
inline __native sys_ipc_answer(__native callid, __native *data)
335
__native sys_ipc_answer(__native callid, ipc_data_t *data)
338
{
336
{
339
    call_t *call;
337
    call_t *call;
340
    ipc_data_t saved_data;
338
    ipc_data_t saved_data;
341
    int preprocess = 0;
339
    int preprocess = 0;
342
 
340
 
Line 346... Line 344...
346
 
344
 
347
    if (answer_will_preprocess(call)) {
345
    if (answer_will_preprocess(call)) {
348
        memcpy(&saved_data, &call->data, sizeof(call->data));
346
        memcpy(&saved_data, &call->data, sizeof(call->data));
349
        preprocess = 1;
347
        preprocess = 1;
350
    }
348
    }
351
    copy_from_uspace(&call->data, data, sizeof(call->data));
349
    copy_from_uspace(&call->data.args, &data->args,
-
 
350
             sizeof(call->data.args));
352
 
351
 
353
    if (preprocess)
352
    if (preprocess)
354
        answer_preprocess(call, &saved_data);
353
        answer_preprocess(call, &saved_data);
355
   
354
   
356
    ipc_answer(&TASK->answerbox, call);
355
    ipc_answer(&TASK->answerbox, call);
Line 367... Line 366...
367
{
366
{
368
    call_t call;
367
    call_t call;
369
    phone_t *phone;
368
    phone_t *phone;
370
 
369
 
371
    ipc_call_static_init(&call);
370
    ipc_call_static_init(&call);
372
    IPC_SET_METHOD(call.data, IPC_M_CONNECTTOME);
371
    IPC_SET_METHOD(call.data, IPC_M_CONNECT_TO_ME);
373
    IPC_SET_ARG1(call.data, arg1);
372
    IPC_SET_ARG1(call.data, arg1);
374
    IPC_SET_ARG2(call.data, arg2);
373
    IPC_SET_ARG2(call.data, arg2);
375
   
374
   
376
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
375
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
377
 
376
 
378
    ipc_call_sync(phone, &call);
377
    ipc_call_sync(phone, &call);
379
 
378
 
380
    if (!IPC_GET_RETVAL(call.data) && taskid)
379
    if (!IPC_GET_RETVAL(call.data) && taskid)
381
        copy_to_uspace(taskid,
-
 
382
                   &phone->callee->task->taskid,
380
        STRUCT_TO_USPACE(taskid, &phone->callee->task->taskid);
383
                   sizeof(TASK->taskid));
-
 
384
 
381
 
385
    return IPC_GET_RETVAL(call.data);
382
    return IPC_GET_RETVAL(call.data);
386
}
383
}
387
 
384
 
388
/** Ask target process to connect me somewhere
385
/** Ask target process to connect me somewhere
Line 401... Line 398...
401
    newphid = phone_alloc();
398
    newphid = phone_alloc();
402
    if (newphid < 0)
399
    if (newphid < 0)
403
        return ELIMIT;
400
        return ELIMIT;
404
 
401
 
405
    ipc_call_static_init(&call);
402
    ipc_call_static_init(&call);
406
    IPC_SET_METHOD(call.data, IPC_M_CONNECTMETO);
403
    IPC_SET_METHOD(call.data, IPC_M_CONNECT_ME_TO);
407
    IPC_SET_ARG1(call.data, arg1);
404
    IPC_SET_ARG1(call.data, arg1);
408
    IPC_SET_ARG2(call.data, arg2);
405
    IPC_SET_ARG2(call.data, arg2);
409
    IPC_SET_ARG3(call.data, (__native)&TASK->phones[newphid]);
406
    IPC_SET_ARG3(call.data, (__native)&TASK->phones[newphid]);
410
 
407
 
411
    ipc_call_sync(phone, &call);
408
    ipc_call_sync(phone, &call);
Line 416... Line 413...
416
    }
413
    }
417
 
414
 
418
    return newphid;
415
    return newphid;
419
}
416
}
420
 
417
 
421
/** Wait for incoming ipc call or answer
418
/** Hang up the phone
422
 *
419
 *
423
 * Generic function - can serve either as inkernel or userspace call
-
 
424
 * - inside kernel does probably unnecessary copying of data (TODO)
-
 
425
 *
420
 *
-
 
421
 *
-
 
422
 */
-
 
423
__native sys_ipc_hangup(int phoneid)
-
 
424
{
426
 * @param result
425
    phone_t *phone;
-
 
426
 
-
 
427
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
-
 
428
 
-
 
429
    if (ipc_phone_hangup(phone))
-
 
430
        return -1;
-
 
431
 
-
 
432
    return 0;
-
 
433
}
-
 
434
 
-
 
435
/** Wait for incoming ipc call or answer
-
 
436
 *
-
 
437
 * @param calldata Pointer to buffer where the call/answer data is stored
-
 
438
 * @param taskid On 'CONNECT_ME_TO' call it is filled with 'taskid' of
427
 * @param taskid
439
 *               the caller.
428
 * @param flags
440
 * @param flags
429
 * @return Callid, if callid & 1, then the call is answer
441
 * @return Callid, if callid & 1, then the call is answer
430
 */
442
 */
431
inline __native sys_ipc_wait_for_call(ipc_data_t *calldata,
443
__native sys_ipc_wait_for_call(ipc_data_t *calldata, task_id_t *taskid,
432
                      task_id_t *taskid,
-
 
433
                      __native flags)
444
                   __native flags)
434
                         
445
                         
435
{
446
{
436
    call_t *call;
447
    call_t *call;
437
 
448
 
Line 440... Line 451...
440
 
451
 
441
    if (call->flags & IPC_CALL_ANSWERED) {
452
    if (call->flags & IPC_CALL_ANSWERED) {
442
        if (process_answer(&TASK->answerbox, call))
453
        if (process_answer(&TASK->answerbox, call))
443
            goto restart;
454
            goto restart;
444
 
455
 
445
        copy_to_uspace(calldata, &call->data, sizeof(call->data));
-
 
446
        atomic_dec(&TASK->active_calls);
-
 
447
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
456
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
-
 
457
 
-
 
458
        atomic_dec(&TASK->active_calls);
-
 
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);
448
        ipc_call_free(call);
466
        ipc_call_free(call);
449
 
467
 
450
        return ((__native)call) | IPC_CALLID_ANSWERED;
468
        return ((__native)call) | IPC_CALLID_ANSWERED;
451
    }
469
    }
-
 
470
 
452
    if (process_request(&TASK->answerbox, call))
471
    if (process_request(&TASK->answerbox, call))
453
        goto restart;
472
        goto restart;
-
 
473
 
-
 
474
    /* Include phone address('id') of the caller in the request */
454
    copy_to_uspace(calldata, &call->data, sizeof(call->data));
475
    STRUCT_TO_USPACE(calldata, &call->data);
-
 
476
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_ME_TO)
455
    copy_to_uspace(taskid, (void *)&TASK->taskid, sizeof(TASK->taskid));
477
        STRUCT_TO_USPACE(taskid, &TASK->taskid);
456
    return (__native)call;
478
    return (__native)call;
457
}
479
}
458
 
-