Subversion Repositories HelenOS

Rev

Rev 1029 | Rev 1060 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1029 Rev 1040
1
/*
1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
2
 * Copyright (C) 2006 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <arch.h>
29
#include <arch.h>
30
#include <proc/task.h>
30
#include <proc/task.h>
31
 
31
 
32
#include <errno.h>
32
#include <errno.h>
33
#include <mm/page.h>
33
#include <mm/page.h>
34
#include <memstr.h>
34
#include <memstr.h>
35
#include <debug.h>
35
#include <debug.h>
36
#include <ipc/ipc.h>
36
#include <ipc/ipc.h>
37
#include <ipc/sysipc.h>
37
#include <ipc/sysipc.h>
38
#include <print.h>
38
#include <print.h>
39
 
39
 
40
/* TODO: multi-threaded connect-to-me can cause race condition
40
/* TODO: multi-threaded connect-to-me can cause race condition
41
 * on phone, add counter + thread_kill??
41
 * on phone, add counter + thread_kill??
42
 *
42
 *
43
 * - don't allow userspace app to send system messages
-
 
44
 */
43
 */
45
 
44
 
-
 
45
/** Return true if the method is a system method */
-
 
46
static inline int is_system_method(__native method)
-
 
47
{
-
 
48
    if (method <= IPC_M_LAST_SYSTEM)
-
 
49
        return 1;
-
 
50
    return 0;
-
 
51
}
-
 
52
 
-
 
53
/** Return true if the message with this method is forwardable
-
 
54
 *
-
 
55
 * - some system messages may be forwarded, for some of them
-
 
56
 *   it is useless
-
 
57
 */
-
 
58
static inline int is_forwardable(__native method)
-
 
59
{
-
 
60
    return 1;
-
 
61
}
-
 
62
 
-
 
63
/** Find call_t * in call table according to callid
-
 
64
 *
-
 
65
 * @return NULL on not found, otherwise pointer to call structure
-
 
66
 */
-
 
67
static inline call_t * get_call(__native callid)
-
 
68
{
-
 
69
    /* TODO: Traverse list of dispatched calls and find one */
-
 
70
    /* TODO: locking of call, ripping it from dispatched calls etc.  */
-
 
71
    return (call_t *) callid;
-
 
72
}
-
 
73
 
46
/** Return pointer to phone identified by phoneid or NULL if non-existent */
74
/** Return pointer to phone identified by phoneid or NULL if non-existent */
47
static phone_t * get_phone(__native phoneid)
75
static phone_t * get_phone(__native phoneid)
48
{
76
{
49
    phone_t *phone;
77
    phone_t *phone;
50
 
78
 
51
    if (phoneid >= IPC_MAX_PHONES)
79
    if (phoneid >= IPC_MAX_PHONES)
52
        return NULL;
80
        return NULL;
53
 
81
 
54
    phone = &TASK->phones[phoneid];
82
    phone = &TASK->phones[phoneid];
55
    if (!phone->callee)
83
    if (!phone->callee)
56
        return NULL;
84
        return NULL;
57
    return phone;
85
    return phone;
58
}
86
}
59
 
87
 
60
/** Allocate new phone slot in current TASK structure */
88
/** Allocate new phone slot in current TASK structure */
61
static int phone_alloc(answerbox_t *callee)
89
static int phone_alloc(void)
62
{
90
{
63
    int i;
91
    int i;
64
 
92
 
65
    spinlock_lock(&TASK->lock);
93
    spinlock_lock(&TASK->lock);
66
   
94
   
67
    for (i=0; i < IPC_MAX_PHONES; i++) {
95
    for (i=0; i < IPC_MAX_PHONES; i++) {
68
        if (!TASK->phones[i].callee) {
96
        if (!TASK->phones[i].busy) {
69
            TASK->phones[i].callee = callee;
97
            TASK->phones[i].busy = 1;
70
            break;
98
            break;
71
        }
99
        }
72
    }
100
    }
73
    spinlock_unlock(&TASK->lock);
101
    spinlock_unlock(&TASK->lock);
74
 
102
 
75
    if (i >= IPC_MAX_PHONES)
103
    if (i >= IPC_MAX_PHONES)
76
        return -1;
104
        return -1;
77
    return i;
105
    return i;
78
}
106
}
79
 
107
 
80
/** Disconnect phone */
108
/** Disconnect phone */
81
static void phone_dealloc(int phoneid)
109
static void phone_dealloc(int phoneid)
82
{
110
{
83
    spinlock_lock(&TASK->lock);
111
    spinlock_lock(&TASK->lock);
84
 
112
 
85
    ASSERT(TASK->phones[phoneid].callee);
113
    ASSERT(TASK->phones[phoneid].busy);
86
 
114
 
-
 
115
    if (TASK->phones[phoneid].callee)
-
 
116
        ipc_phone_destroy(&TASK->phones[phoneid]);
-
 
117
 
87
    TASK->phones[phoneid].callee = NULL;
118
    TASK->phones[phoneid].busy = 0;
88
    spinlock_unlock(&TASK->lock);
119
    spinlock_unlock(&TASK->lock);
89
}
120
}
90
 
121
 
-
 
122
static void phone_connect(int phoneid, answerbox_t *box)
-
 
123
{
-
 
124
    phone_t *phone = &TASK->phones[phoneid];
-
 
125
   
-
 
126
    ipc_phone_connect(phone, box);
-
 
127
}
-
 
128
 
91
/****************************************************/
129
/****************************************************/
92
/* Functions that preprocess answer before sending
130
/* Functions that preprocess answer before sending
93
 * it to the recepient
131
 * it to the recepient
94
 */
132
 */
95
 
133
 
96
/** Return true if the caller (ipc_answer) should save
134
/** Return true if the caller (ipc_answer) should save
97
 * the old call contents and call answer_preprocess
135
 * the old call contents and call answer_preprocess
98
 */
136
 */
99
static inline int answer_will_preprocess(call_t *call)
137
static inline int answer_will_preprocess(call_t *call)
100
{
138
{
101
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME)
139
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME)
102
        return 1;
140
        return 1;
103
    return 0;
141
    return 0;
104
}
142
}
105
 
143
 
106
/** Interpret process answer as control information */
144
/** Interpret process answer as control information */
107
static inline void answer_preprocess(call_t *answer, call_t *call)
145
static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata)
108
{
146
{
109
    int phoneid;
147
    int phoneid;
110
 
148
 
111
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) {
149
    if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTTOME) {
-
 
150
        phoneid = IPC_GET_ARG3(*olddata);
112
        if (IPC_GET_RETVAL(answer->data)) {
151
        if (IPC_GET_RETVAL(answer->data)) {
113
            /* The connection was not accepted */
152
            /* The connection was not accepted */
114
            /* TODO...race for multi-threaded process */
-
 
115
            phoneid = IPC_GET_ARG3(call->data);
-
 
116
            phone_dealloc(phoneid);
153
            phone_dealloc(phoneid);
-
 
154
        } else {
-
 
155
            /* The connection was accepted */
-
 
156
            phone_connect(phoneid,&answer->sender->answerbox);
117
        }
157
        }
118
    }
158
    }
119
}
159
}
120
 
160
 
121
/****************************************************/
161
/****************************************************/
122
/* Functions called to process received call/answer
162
/* Functions called to process received call/answer
123
 * before passing to uspace
163
 * before passing to uspace
124
 */
164
 */
125
 
165
 
126
/** Do basic kernel processing of received call answer */
166
/** Do basic kernel processing of received call answer */
127
static int process_answer(answerbox_t *box,call_t *call)
167
static int process_answer(answerbox_t *box,call_t *call)
128
{
168
{
129
    return 0;
169
    return 0;
130
}
170
}
131
 
171
 
132
/** Do basic kernel processing of received call request
172
/** Do basic kernel processing of received call request
133
 *
173
 *
134
 * @return 0 - the call should be passed to userspace, 1 - ignore call
174
 * @return 0 - the call should be passed to userspace, 1 - ignore call
135
 */
175
 */
136
static int process_request(answerbox_t *box,call_t *call)
176
static int process_request(answerbox_t *box,call_t *call)
137
{
177
{
138
    int phoneid;
178
    int phoneid;
139
 
179
 
140
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) {
180
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) {
141
        phoneid = phone_alloc(&call->sender->answerbox);
181
        phoneid = phone_alloc();
142
        if (phoneid < 0) { /* Failed to allocate phone */
182
        if (phoneid < 0) { /* Failed to allocate phone */
143
            IPC_SET_RETVAL(call->data, ELIMIT);
183
            IPC_SET_RETVAL(call->data, ELIMIT);
144
            ipc_answer(box,call);
184
            ipc_answer(box,call);
145
            return -1;
185
            return -1;
146
        }
186
        }
147
        IPC_SET_ARG3(call->data, phoneid);
187
        IPC_SET_ARG3(call->data, phoneid);
148
    }
188
    }
149
    return 0;
189
    return 0;
150
}
190
}
151
 
191
 
152
/** Send a call over IPC, wait for reply, return to user
192
/** Send a call over IPC, wait for reply, return to user
153
 *
193
 *
154
 * @return Call identification, returns -1 on fatal error,
194
 * @return Call identification, returns -1 on fatal error,
155
           -2 on 'Too many async request, handle answers first
195
           -2 on 'Too many async request, handle answers first
156
 */
196
 */
157
__native sys_ipc_call_sync_fast(__native phoneid, __native method,
197
__native sys_ipc_call_sync_fast(__native phoneid, __native method,
158
                __native arg1, __native *data)
198
                __native arg1, __native *data)
159
{
199
{
160
    call_t call;
200
    call_t call;
161
    phone_t *phone;
201
    phone_t *phone;
162
 
202
 
163
    phone = get_phone(phoneid);
203
    phone = get_phone(phoneid);
164
    if (!phone)
204
    if (!phone)
165
        return IPC_CALLRET_FATAL;
205
        return ENOENT;
-
 
206
 
-
 
207
    if (is_system_method(method))
-
 
208
        return EPERM;
166
 
209
 
167
    ipc_call_init(&call);
210
    ipc_call_init(&call);
168
    IPC_SET_METHOD(call.data, method);
211
    IPC_SET_METHOD(call.data, method);
169
    IPC_SET_ARG1(call.data, arg1);
212
    IPC_SET_ARG1(call.data, arg1);
170
   
213
   
171
    ipc_call_sync(phone, &call);
214
    ipc_call_sync(phone, &call);
172
 
215
 
173
    copy_to_uspace(data, &call.data, sizeof(call.data));
216
    copy_to_uspace(data, &call.data, sizeof(call.data));
174
 
217
 
175
    return 0;
218
    return 0;
176
}
219
}
177
 
220
 
178
/** Synchronous IPC call allowing to send whole message */
221
/** Synchronous IPC call allowing to send whole message */
179
__native sys_ipc_call_sync(__native phoneid, __native *question,
222
__native sys_ipc_call_sync(__native phoneid, __native *question,
180
               __native *reply)
223
               __native *reply)
181
{
224
{
182
    call_t call;
225
    call_t call;
183
    phone_t *phone;
226
    phone_t *phone;
184
 
227
 
185
    phone = get_phone(phoneid);
228
    phone = get_phone(phoneid);
186
    if (!phone)
229
    if (!phone)
187
        return IPC_CALLRET_FATAL;
230
        return ENOENT;
188
 
231
 
189
    ipc_call_init(&call);
232
    ipc_call_init(&call);
190
    copy_from_uspace(&call.data, question, sizeof(call.data));
233
    copy_from_uspace(&call.data, question, sizeof(call.data));
-
 
234
 
-
 
235
    if (is_system_method(IPC_GET_METHOD(call.data)))
-
 
236
        return EPERM;
191
   
237
   
192
    ipc_call_sync(phone, &call);
238
    ipc_call_sync(phone, &call);
193
 
239
 
194
    copy_to_uspace(reply, &call.data, sizeof(call.data));
240
    copy_to_uspace(reply, &call.data, sizeof(call.data));
195
 
241
 
196
    return 0;
242
    return 0;
197
}
243
}
198
 
244
 
199
/** Check that the task did not exceed allowed limit
245
/** Check that the task did not exceed allowed limit
200
 *
246
 *
201
 * @return 0 - Limit OK,   -1 - limit exceeded
247
 * @return 0 - Limit OK,   -1 - limit exceeded
202
 */
248
 */
203
static int check_call_limit(void)
249
static int check_call_limit(void)
204
{
250
{
205
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
251
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
206
        atomic_dec(&TASK->active_calls);
252
        atomic_dec(&TASK->active_calls);
207
        return -1;
253
        return -1;
208
    }
254
    }
209
    return 0;
255
    return 0;
210
}
256
}
211
 
257
 
212
/** Send an asynchronous call over ipc
258
/** Send an asynchronous call over ipc
213
 *
259
 *
214
 * @return Call identification, returns -1 on fatal error,
260
 * @return Call identification, returns -1 on fatal error,
215
           -2 on 'Too many async request, handle answers first
261
           -2 on 'Too many async request, handle answers first
216
 */
262
 */
217
__native sys_ipc_call_async_fast(__native phoneid, __native method,
263
__native sys_ipc_call_async_fast(__native phoneid, __native method,
218
                 __native arg1, __native arg2)
264
                 __native arg1, __native arg2)
219
{
265
{
220
    call_t *call;
266
    call_t *call;
221
    phone_t *phone;
267
    phone_t *phone;
222
 
268
 
223
    phone = get_phone(phoneid);
269
    phone = get_phone(phoneid);
224
    if (!phone)
270
    if (!phone)
225
        return IPC_CALLRET_FATAL;
271
        return IPC_CALLRET_FATAL;
226
 
272
 
-
 
273
    if (is_system_method(method))
-
 
274
        return IPC_CALLRET_FATAL;
-
 
275
 
227
    if (check_call_limit())
276
    if (check_call_limit())
228
        return IPC_CALLRET_TEMPORARY;
277
        return IPC_CALLRET_TEMPORARY;
229
 
278
 
230
    call = ipc_call_alloc();
279
    call = ipc_call_alloc();
231
    IPC_SET_METHOD(call->data, method);
280
    IPC_SET_METHOD(call->data, method);
232
    IPC_SET_ARG1(call->data, arg1);
281
    IPC_SET_ARG1(call->data, arg1);
233
    IPC_SET_ARG2(call->data, arg2);
282
    IPC_SET_ARG2(call->data, arg2);
234
 
283
 
235
    ipc_call(phone, call);
284
    ipc_call(phone, call);
236
 
285
 
237
    return (__native) call;
286
    return (__native) call;
238
}
287
}
239
 
288
 
240
/** Synchronous IPC call allowing to send whole message
289
/** Synchronous IPC call allowing to send whole message
241
 *
290
 *
242
 * @return The same as sys_ipc_call_async
291
 * @return The same as sys_ipc_call_async
243
 */
292
 */
244
__native sys_ipc_call_async(__native phoneid, __native *data)
293
__native sys_ipc_call_async(__native phoneid, __native *data)
245
{
294
{
246
    call_t *call;
295
    call_t *call;
247
    phone_t *phone;
296
    phone_t *phone;
248
 
297
 
249
    phone = get_phone(phoneid);
298
    phone = get_phone(phoneid);
250
    if (!phone)
299
    if (!phone)
251
        return IPC_CALLRET_FATAL;
300
        return IPC_CALLRET_FATAL;
252
 
301
 
253
    if (check_call_limit())
302
    if (check_call_limit())
254
        return IPC_CALLRET_TEMPORARY;
303
        return IPC_CALLRET_TEMPORARY;
255
 
304
 
256
    call = ipc_call_alloc();
305
    call = ipc_call_alloc();
257
    copy_from_uspace(&call->data, data, sizeof(call->data));
306
    copy_from_uspace(&call->data, data, sizeof(call->data));
-
 
307
 
-
 
308
    if (is_system_method(IPC_GET_METHOD(call->data))) {
-
 
309
        ipc_call_free(call);
-
 
310
        return EPERM;
-
 
311
    }
258
   
312
   
259
    ipc_call(phone, call);
313
    ipc_call(phone, call);
260
 
314
 
261
    return (__native) call;
315
    return (__native) call;
262
}
316
}
263
 
317
 
-
 
318
/** Forward received call to another destination
-
 
319
 *
-
 
320
 * The arg1 and arg2 are changed in the forwarded message
-
 
321
 */
-
 
322
__native sys_ipc_forward_fast(__native callid, __native phoneid,
-
 
323
                  __native method, __native arg1)
-
 
324
{
-
 
325
    call_t *call;
-
 
326
    phone_t *phone;
-
 
327
 
-
 
328
    call = get_call(callid);
-
 
329
    if (!call)
-
 
330
        return ENOENT;
-
 
331
 
-
 
332
    phone = get_phone(phoneid);
-
 
333
    if (!phone) {
-
 
334
        IPC_SET_RETVAL(call->data, EFORWARD);
-
 
335
        ipc_answer(&TASK->answerbox, call);
-
 
336
        return ENOENT;
-
 
337
    }
-
 
338
 
-
 
339
    if (!is_forwardable(IPC_GET_METHOD(call->data))) {
-
 
340
        IPC_SET_RETVAL(call->data, EFORWARD);
-
 
341
        ipc_answer(&TASK->answerbox, call);
-
 
342
        return EPERM;
-
 
343
    }
-
 
344
 
-
 
345
    /* Userspace is not allowed to change method of system methods
-
 
346
     * on forward, allow changing ARG1 and ARG2 by means of method and arg1
-
 
347
     */
-
 
348
    if (is_system_method(IPC_GET_METHOD(call->data))) {
-
 
349
        IPC_SET_ARG1(call->data, method);
-
 
350
        IPC_SET_ARG2(call->data, arg1);
-
 
351
    } else {
-
 
352
        IPC_SET_METHOD(call->data, method);
-
 
353
        IPC_SET_ARG1(call->data, arg1);
-
 
354
    }
-
 
355
 
-
 
356
    ipc_forward(call, phone->callee, &TASK->answerbox);
-
 
357
 
-
 
358
    return 0;
-
 
359
}
-
 
360
 
264
/** Send IPC answer */
361
/** Send IPC answer */
265
__native sys_ipc_answer_fast(__native callid, __native retval,
362
__native sys_ipc_answer_fast(__native callid, __native retval,
266
                 __native arg1, __native arg2)
363
                 __native arg1, __native arg2)
267
{
364
{
268
    call_t *call;
365
    call_t *call;
269
    call_t saved_call;
366
    ipc_data_t saved_data;
270
    int preprocess = 0;
367
    int preprocess = 0;
271
 
368
 
272
    /* Check that the user is not sending us answer callid */
369
    call = get_call(callid);
273
    ASSERT(! (callid & 1));
370
    if (!call)
274
    /* TODO: Check that the callid is in the dispatch table */
-
 
275
    call = (call_t *) callid;
371
        return ENOENT;
276
 
372
 
277
    if (answer_will_preprocess(call)) {
373
    if (answer_will_preprocess(call)) {
278
        memcpy(&saved_call.data, &call->data, sizeof(call->data));
374
        memcpy(&saved_data, &call->data, sizeof(call->data));
279
        preprocess = 1;
375
        preprocess = 1;
280
    }
376
    }
281
 
377
 
282
    IPC_SET_RETVAL(call->data, retval);
378
    IPC_SET_RETVAL(call->data, retval);
283
    IPC_SET_ARG1(call->data, arg1);
379
    IPC_SET_ARG1(call->data, arg1);
284
    IPC_SET_ARG2(call->data, arg2);
380
    IPC_SET_ARG2(call->data, arg2);
-
 
381
 
285
    if (preprocess)
382
    if (preprocess)
286
        answer_preprocess(call, &saved_call);
383
        answer_preprocess(call, &saved_data);
287
 
384
 
288
    ipc_answer(&TASK->answerbox, call);
385
    ipc_answer(&TASK->answerbox, call);
289
    return 0;
386
    return 0;
290
}
387
}
291
 
388
 
292
/** Send IPC answer */
389
/** Send IPC answer */
293
inline __native sys_ipc_answer(__native callid, __native *data)
390
inline __native sys_ipc_answer(__native callid, __native *data)
294
{
391
{
295
    call_t *call;
392
    call_t *call;
296
    call_t saved_call;
393
    ipc_data_t saved_data;
297
    int preprocess = 0;
394
    int preprocess = 0;
298
 
395
 
299
    /* Check that the user is not sending us answer callid */
396
    call = get_call(callid);
300
    ASSERT(! (callid & 1));
397
    if (!call)
301
    /* TODO: Check that the callid is in the dispatch table */
-
 
302
    call = (call_t *) callid;
398
        return ENOENT;
303
 
399
 
304
    if (answer_will_preprocess(call)) {
400
    if (answer_will_preprocess(call)) {
305
        memcpy(&saved_call.data, &call->data, sizeof(call->data));
401
        memcpy(&saved_data, &call->data, sizeof(call->data));
306
        preprocess = 1;
402
        preprocess = 1;
307
    }
403
    }
308
    copy_from_uspace(&call->data, data, sizeof(call->data));
404
    copy_from_uspace(&call->data, data, sizeof(call->data));
309
 
405
 
310
    if (preprocess)
406
    if (preprocess)
311
        answer_preprocess(call, &saved_call);
407
        answer_preprocess(call, &saved_data);
312
   
408
   
313
    ipc_answer(&TASK->answerbox, call);
409
    ipc_answer(&TASK->answerbox, call);
314
 
410
 
315
    return 0;
411
    return 0;
316
}
412
}
317
 
413
 
318
/** Ask the other side of connection to do 'callback' connection
414
/** Ask the other side of connection to do 'callback' connection
319
 *
415
 *
320
 * @return 0 if no error, error otherwise
416
 * @return 0 if no error, error otherwise
321
 */
417
 */
322
__native sys_ipc_connect_to_me(__native phoneid, __native arg1,
418
__native sys_ipc_connect_to_me(__native phoneid, __native arg1,
323
                   __native arg2, task_id_t *taskid)
419
                   __native arg2, task_id_t *taskid)
324
{
420
{
325
    call_t call;
421
    call_t call;
326
    phone_t *phone;
422
    phone_t *phone;
327
 
423
 
328
    phone = get_phone(phoneid);
424
    phone = get_phone(phoneid);
329
    if (!phone)
425
    if (!phone)
330
        return IPC_CALLRET_FATAL;
426
        return ENOENT;
331
 
427
 
332
    ipc_call_init(&call);
428
    ipc_call_init(&call);
333
    IPC_SET_METHOD(call.data, IPC_M_CONNECTTOME);
429
    IPC_SET_METHOD(call.data, IPC_M_CONNECTTOME);
334
    IPC_SET_ARG1(call.data, arg1);
430
    IPC_SET_ARG1(call.data, arg1);
335
    IPC_SET_ARG2(call.data, arg2);
431
    IPC_SET_ARG2(call.data, arg2);
336
   
432
   
337
    ipc_call_sync(phone, &call);
433
    ipc_call_sync(phone, &call);
338
 
434
 
339
    if (!IPC_GET_RETVAL(call.data) && taskid)
435
    if (!IPC_GET_RETVAL(call.data) && taskid)
340
        copy_to_uspace(taskid,
436
        copy_to_uspace(taskid,
341
                   &phone->callee->task->taskid,
437
                   &phone->callee->task->taskid,
342
                   sizeof(TASK->taskid));
438
                   sizeof(TASK->taskid));
343
    return IPC_GET_RETVAL(call.data);
439
    return IPC_GET_RETVAL(call.data);
344
}
440
}
345
 
441
 
-
 
442
/** Ask target process to connect me somewhere
-
 
443
 *
-
 
444
 * @return phoneid - on success, error otherwise
-
 
445
 */
-
 
446
__native sys_ipc_connect_me_to(__native phoneid, __native arg1,
-
 
447
                   __native arg2)
-
 
448
{
-
 
449
    call_t call;
-
 
450
    phone_t *phone;
-
 
451
 
-
 
452
    phone = get_phone(phoneid);
-
 
453
    if (!phone)
-
 
454
        return ENOENT;
-
 
455
 
-
 
456
    ipc_call_init(&call);
-
 
457
    IPC_SET_METHOD(call.data, IPC_M_CONNECTMETO);
-
 
458
    IPC_SET_ARG1(call.data, arg1);
-
 
459
    IPC_SET_ARG2(call.data, arg2);
-
 
460
 
-
 
461
    ipc_call_sync(phone, &call);
-
 
462
    if (!IPC_GET_RETVAL(call.data)) {
-
 
463
        /* Everybody accepted, we should be connected by now */
-
 
464
    }
-
 
465
 
-
 
466
    return 0;
-
 
467
}
-
 
468
 
346
/** Wait for incoming ipc call or answer
469
/** Wait for incoming ipc call or answer
347
 *
470
 *
348
 * Generic function - can serve either as inkernel or userspace call
471
 * Generic function - can serve either as inkernel or userspace call
349
 * - inside kernel does probably unnecessary copying of data (TODO)
472
 * - inside kernel does probably unnecessary copying of data (TODO)
350
 *
473
 *
351
 * @param result
474
 * @param result
352
 * @param taskid
475
 * @param taskid
353
 * @param flags
476
 * @param flags
354
 * @return Callid, if callid & 1, then the call is answer
477
 * @return Callid, if callid & 1, then the call is answer
355
 */
478
 */
356
inline __native sys_ipc_wait_for_call(ipc_data_t *calldata,
479
inline __native sys_ipc_wait_for_call(ipc_data_t *calldata,
357
                      task_id_t *taskid,
480
                      task_id_t *taskid,
358
                      __native flags)
481
                      __native flags)
359
                         
482
                         
360
{
483
{
361
    call_t *call;
484
    call_t *call;
362
 
485
 
363
restart:   
486
restart:   
364
    call = ipc_wait_for_call(&TASK->answerbox, flags);
487
    call = ipc_wait_for_call(&TASK->answerbox, flags);
-
 
488
    printf("Received call %P from sender: %P\n", call, call->sender);
365
 
489
 
366
    if (call->flags & IPC_CALL_ANSWERED) {
490
    if (call->flags & IPC_CALL_ANSWERED) {
367
        if (process_answer(&TASK->answerbox, call))
491
        if (process_answer(&TASK->answerbox, call))
368
            goto restart;
492
            goto restart;
369
 
493
 
370
        copy_to_uspace(calldata, &call->data, sizeof(call->data));
494
        copy_to_uspace(calldata, &call->data, sizeof(call->data));
371
        atomic_dec(&TASK->active_calls);
495
        atomic_dec(&TASK->active_calls);
372
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
496
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
373
        ipc_call_free(call);
497
        ipc_call_free(call);
374
 
498
 
375
        return ((__native)call) | IPC_CALLID_ANSWERED;
499
        return ((__native)call) | IPC_CALLID_ANSWERED;
376
    }
500
    }
377
    if (process_request(&TASK->answerbox, call))
501
    if (process_request(&TASK->answerbox, call))
378
        goto restart;
502
        goto restart;
379
    copy_to_uspace(calldata, &call->data, sizeof(call->data));
503
    copy_to_uspace(calldata, &call->data, sizeof(call->data));
380
    copy_to_uspace(taskid, (void *)&TASK->taskid, sizeof(TASK->taskid));
504
    copy_to_uspace(taskid, (void *)&TASK->taskid, sizeof(TASK->taskid));
381
    return (__native)call;
505
    return (__native)call;
382
}
506
}
383
 
507
 
384
 
508