Subversion Repositories HelenOS

Rev

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

Rev 1027 Rev 1029
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
43
 * - don't allow userspace app to send system messages
44
 */
44
 */
45
 
45
 
46
/** Return pointer to phone identified by phoneid or NULL if non-existent */
46
/** Return pointer to phone identified by phoneid or NULL if non-existent */
47
static phone_t * get_phone(__native phoneid)
47
static phone_t * get_phone(__native phoneid)
48
{
48
{
49
    phone_t *phone;
49
    phone_t *phone;
50
 
50
 
51
    if (phoneid >= IPC_MAX_PHONES)
51
    if (phoneid >= IPC_MAX_PHONES)
52
        return NULL;
52
        return NULL;
53
 
53
 
54
    phone = &TASK->phones[phoneid];
54
    phone = &TASK->phones[phoneid];
55
    if (!phone->callee)
55
    if (!phone->callee)
56
        return NULL;
56
        return NULL;
57
    return phone;
57
    return phone;
58
}
58
}
59
 
59
 
60
/** Allocate new phone slot in current TASK structure */
60
/** Allocate new phone slot in current TASK structure */
61
static int phone_alloc(answerbox_t *callee)
61
static int phone_alloc(answerbox_t *callee)
62
{
62
{
63
    int i;
63
    int i;
64
 
64
 
65
    spinlock_lock(&TASK->lock);
65
    spinlock_lock(&TASK->lock);
66
   
66
   
67
    for (i=0; i < IPC_MAX_PHONES; i++) {
67
    for (i=0; i < IPC_MAX_PHONES; i++) {
68
        if (!TASK->phones[i].callee) {
68
        if (!TASK->phones[i].callee) {
69
            TASK->phones[i].callee = callee;
69
            TASK->phones[i].callee = callee;
70
            break;
70
            break;
71
        }
71
        }
72
    }
72
    }
73
    spinlock_unlock(&TASK->lock);
73
    spinlock_unlock(&TASK->lock);
74
 
74
 
75
    if (i >= IPC_MAX_PHONES)
75
    if (i >= IPC_MAX_PHONES)
76
        return -1;
76
        return -1;
77
    return i;
77
    return i;
78
}
78
}
79
 
79
 
80
/** Disconnect phone */
80
/** Disconnect phone */
81
static void phone_dealloc(int phoneid)
81
static void phone_dealloc(int phoneid)
82
{
82
{
83
    spinlock_lock(TASK->lock);
83
    spinlock_lock(&TASK->lock);
84
 
84
 
85
    ASSERT(TASK->phones[phoneid].callee);
85
    ASSERT(TASK->phones[phoneid].callee);
86
 
86
 
87
    TASK->phones[phoneid].callee = NULL;
87
    TASK->phones[phoneid].callee = NULL;
88
    spinlock_unlock(TASK->lock);
88
    spinlock_unlock(&TASK->lock);
89
}
89
}
90
 
90
 
91
/****************************************************/
91
/****************************************************/
92
/* Functions that preprocess answer before sending
92
/* Functions that preprocess answer before sending
93
 * it to the recepient
93
 * it to the recepient
94
 */
94
 */
95
 
95
 
96
/** Return true if the caller (ipc_answer) should save
96
/** Return true if the caller (ipc_answer) should save
97
 * the old call contents and call answer_preprocess
97
 * the old call contents and call answer_preprocess
98
 */
98
 */
99
static inline int answer_will_preprocess(call_t *call)
99
static inline int answer_will_preprocess(call_t *call)
100
{
100
{
101
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME)
101
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME)
102
        return 1;
102
        return 1;
103
    return 0;
103
    return 0;
104
}
104
}
105
 
105
 
106
/** Interpret process answer as control information */
106
/** Interpret process answer as control information */
107
static inline void answer_preprocess(call_t *answer, call_t *call)
107
static inline void answer_preprocess(call_t *answer, call_t *call)
108
{
108
{
109
    int phoneid;
109
    int phoneid;
110
 
110
 
111
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) {
111
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) {
112
        if (IPC_GET_RETVAL(answer->data)) {
112
        if (IPC_GET_RETVAL(answer->data)) {
113
            /* The connection was not accepted */
113
            /* The connection was not accepted */
114
            /* TODO...race for multi-threaded process */
114
            /* TODO...race for multi-threaded process */
115
            phoneid = IPC_GET_ARG3(call->data);
115
            phoneid = IPC_GET_ARG3(call->data);
116
            phone_dealloc(phoneid);
116
            phone_dealloc(phoneid);
117
        }
117
        }
118
    }
118
    }
119
}
119
}
120
 
120
 
121
/****************************************************/
121
/****************************************************/
122
/* Functions called to process received call/answer
122
/* Functions called to process received call/answer
123
 * before passing to uspace
123
 * before passing to uspace
124
 */
124
 */
125
 
125
 
126
/** Do basic kernel processing of received call answer */
126
/** Do basic kernel processing of received call answer */
127
static int process_answer(answerbox_t *box,call_t *call)
127
static int process_answer(answerbox_t *box,call_t *call)
128
{
128
{
129
    return 0;
129
    return 0;
130
}
130
}
131
 
131
 
132
/** Do basic kernel processing of received call request
132
/** Do basic kernel processing of received call request
133
 *
133
 *
134
 * @return 0 - the call should be passed to userspace, 1 - ignore call
134
 * @return 0 - the call should be passed to userspace, 1 - ignore call
135
 */
135
 */
136
static int process_request(answerbox_t *box,call_t *call)
136
static int process_request(answerbox_t *box,call_t *call)
137
{
137
{
138
    int phoneid;
138
    int phoneid;
139
 
139
 
140
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) {
140
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) {
141
        phoneid = phone_alloc(&call->sender->answerbox);
141
        phoneid = phone_alloc(&call->sender->answerbox);
142
        if (phoneid < 0) { /* Failed to allocate phone */
142
        if (phoneid < 0) { /* Failed to allocate phone */
143
            IPC_SET_RETVAL(call->data, ELIMIT);
143
            IPC_SET_RETVAL(call->data, ELIMIT);
144
            ipc_answer(box,call);
144
            ipc_answer(box,call);
145
            return -1;
145
            return -1;
146
        }
146
        }
147
        IPC_SET_ARG3(call->data, phoneid);
147
        IPC_SET_ARG3(call->data, phoneid);
148
    }
148
    }
149
    return 0;
149
    return 0;
150
}
150
}
151
 
151
 
152
/** Send a call over IPC, wait for reply, return to user
152
/** Send a call over IPC, wait for reply, return to user
153
 *
153
 *
154
 * @return Call identification, returns -1 on fatal error,
154
 * @return Call identification, returns -1 on fatal error,
155
           -2 on 'Too many async request, handle answers first
155
           -2 on 'Too many async request, handle answers first
156
 */
156
 */
157
__native sys_ipc_call_sync_fast(__native phoneid, __native method,
157
__native sys_ipc_call_sync_fast(__native phoneid, __native method,
158
                __native arg1, __native *data)
158
                __native arg1, __native *data)
159
{
159
{
160
    call_t call;
160
    call_t call;
161
    phone_t *phone;
161
    phone_t *phone;
162
 
162
 
163
    phone = get_phone(phoneid);
163
    phone = get_phone(phoneid);
164
    if (!phone)
164
    if (!phone)
165
        return IPC_CALLRET_FATAL;
165
        return IPC_CALLRET_FATAL;
166
 
166
 
167
    ipc_call_init(&call);
167
    ipc_call_init(&call);
168
    IPC_SET_METHOD(call.data, method);
168
    IPC_SET_METHOD(call.data, method);
169
    IPC_SET_ARG1(call.data, arg1);
169
    IPC_SET_ARG1(call.data, arg1);
170
   
170
   
171
    ipc_call_sync(phone, &call);
171
    ipc_call_sync(phone, &call);
172
 
172
 
173
    copy_to_uspace(data, &call.data, sizeof(call.data));
173
    copy_to_uspace(data, &call.data, sizeof(call.data));
174
 
174
 
175
    return 0;
175
    return 0;
176
}
176
}
177
 
177
 
178
/** Synchronous IPC call allowing to send whole message */
178
/** Synchronous IPC call allowing to send whole message */
179
__native sys_ipc_call_sync(__native phoneid, __native *question,
179
__native sys_ipc_call_sync(__native phoneid, __native *question,
180
               __native *reply)
180
               __native *reply)
181
{
181
{
182
    call_t call;
182
    call_t call;
183
    phone_t *phone;
183
    phone_t *phone;
184
 
184
 
185
    phone = get_phone(phoneid);
185
    phone = get_phone(phoneid);
186
    if (!phone)
186
    if (!phone)
187
        return IPC_CALLRET_FATAL;
187
        return IPC_CALLRET_FATAL;
188
 
188
 
189
    ipc_call_init(&call);
189
    ipc_call_init(&call);
190
    copy_from_uspace(&call.data, question, sizeof(call.data));
190
    copy_from_uspace(&call.data, question, sizeof(call.data));
191
   
191
   
192
    ipc_call_sync(phone, &call);
192
    ipc_call_sync(phone, &call);
193
 
193
 
194
    copy_to_uspace(reply, &call.data, sizeof(call.data));
194
    copy_to_uspace(reply, &call.data, sizeof(call.data));
195
 
195
 
196
    return 0;
196
    return 0;
197
}
197
}
198
 
198
 
199
/** Check that the task did not exceed allowed limit
199
/** Check that the task did not exceed allowed limit
200
 *
200
 *
201
 * @return 0 - Limit OK,   -1 - limit exceeded
201
 * @return 0 - Limit OK,   -1 - limit exceeded
202
 */
202
 */
203
static int check_call_limit(void)
203
static int check_call_limit(void)
204
{
204
{
205
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
205
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
206
        atomic_dec(&TASK->active_calls);
206
        atomic_dec(&TASK->active_calls);
207
        return -1;
207
        return -1;
208
    }
208
    }
209
    return 0;
209
    return 0;
210
}
210
}
211
 
211
 
212
/** Send an asynchronous call over ipc
212
/** Send an asynchronous call over ipc
213
 *
213
 *
214
 * @return Call identification, returns -1 on fatal error,
214
 * @return Call identification, returns -1 on fatal error,
215
           -2 on 'Too many async request, handle answers first
215
           -2 on 'Too many async request, handle answers first
216
 */
216
 */
217
__native sys_ipc_call_async_fast(__native phoneid, __native method,
217
__native sys_ipc_call_async_fast(__native phoneid, __native method,
218
                 __native arg1, __native arg2)
218
                 __native arg1, __native arg2)
219
{
219
{
220
    call_t *call;
220
    call_t *call;
221
    phone_t *phone;
221
    phone_t *phone;
222
 
222
 
223
    phone = get_phone(phoneid);
223
    phone = get_phone(phoneid);
224
    if (!phone)
224
    if (!phone)
225
        return IPC_CALLRET_FATAL;
225
        return IPC_CALLRET_FATAL;
226
 
226
 
227
    if (check_call_limit())
227
    if (check_call_limit())
228
        return IPC_CALLRET_TEMPORARY;
228
        return IPC_CALLRET_TEMPORARY;
229
 
229
 
230
    call = ipc_call_alloc();
230
    call = ipc_call_alloc();
231
    IPC_SET_METHOD(call->data, method);
231
    IPC_SET_METHOD(call->data, method);
232
    IPC_SET_ARG1(call->data, arg1);
232
    IPC_SET_ARG1(call->data, arg1);
233
    IPC_SET_ARG2(call->data, arg2);
233
    IPC_SET_ARG2(call->data, arg2);
234
 
234
 
235
    ipc_call(phone, call);
235
    ipc_call(phone, call);
236
 
236
 
237
    return (__native) call;
237
    return (__native) call;
238
}
238
}
239
 
239
 
240
/** Synchronous IPC call allowing to send whole message
240
/** Synchronous IPC call allowing to send whole message
241
 *
241
 *
242
 * @return The same as sys_ipc_call_async
242
 * @return The same as sys_ipc_call_async
243
 */
243
 */
244
__native sys_ipc_call_async(__native phoneid, __native *data)
244
__native sys_ipc_call_async(__native phoneid, __native *data)
245
{
245
{
246
    call_t *call;
246
    call_t *call;
247
    phone_t *phone;
247
    phone_t *phone;
248
 
248
 
249
    phone = get_phone(phoneid);
249
    phone = get_phone(phoneid);
250
    if (!phone)
250
    if (!phone)
251
        return IPC_CALLRET_FATAL;
251
        return IPC_CALLRET_FATAL;
252
 
252
 
253
    if (check_call_limit())
253
    if (check_call_limit())
254
        return IPC_CALLRET_TEMPORARY;
254
        return IPC_CALLRET_TEMPORARY;
255
 
255
 
256
    call = ipc_call_alloc();
256
    call = ipc_call_alloc();
257
    copy_from_uspace(&call->data, data, sizeof(call->data));
257
    copy_from_uspace(&call->data, data, sizeof(call->data));
258
   
258
   
259
    ipc_call(phone, call);
259
    ipc_call(phone, call);
260
 
260
 
261
    return (__native) call;
261
    return (__native) call;
262
}
262
}
263
 
263
 
264
/** Send IPC answer */
264
/** Send IPC answer */
265
__native sys_ipc_answer_fast(__native callid, __native retval,
265
__native sys_ipc_answer_fast(__native callid, __native retval,
266
                 __native arg1, __native arg2)
266
                 __native arg1, __native arg2)
267
{
267
{
268
    call_t *call;
268
    call_t *call;
269
    call_t saved_call;
269
    call_t saved_call;
270
    int preprocess = 0;
270
    int preprocess = 0;
271
 
271
 
272
    /* Check that the user is not sending us answer callid */
272
    /* Check that the user is not sending us answer callid */
273
    ASSERT(! (callid & 1));
273
    ASSERT(! (callid & 1));
274
    /* TODO: Check that the callid is in the dispatch table */
274
    /* TODO: Check that the callid is in the dispatch table */
275
    call = (call_t *) callid;
275
    call = (call_t *) callid;
276
 
276
 
277
    if (answer_will_preprocess(call)) {
277
    if (answer_will_preprocess(call)) {
278
        memcpy(&saved_call.data, &call->data, sizeof(call->data));
278
        memcpy(&saved_call.data, &call->data, sizeof(call->data));
279
        preprocess = 1;
279
        preprocess = 1;
280
    }
280
    }
281
 
281
 
282
    IPC_SET_RETVAL(call->data, retval);
282
    IPC_SET_RETVAL(call->data, retval);
283
    IPC_SET_ARG1(call->data, arg1);
283
    IPC_SET_ARG1(call->data, arg1);
284
    IPC_SET_ARG2(call->data, arg2);
284
    IPC_SET_ARG2(call->data, arg2);
285
    if (preprocess)
285
    if (preprocess)
286
        answer_preprocess(call, &saved_call);
286
        answer_preprocess(call, &saved_call);
287
 
287
 
288
    ipc_answer(&TASK->answerbox, call);
288
    ipc_answer(&TASK->answerbox, call);
289
    return 0;
289
    return 0;
290
}
290
}
291
 
291
 
292
/** Send IPC answer */
292
/** Send IPC answer */
293
inline __native sys_ipc_answer(__native callid, __native *data)
293
inline __native sys_ipc_answer(__native callid, __native *data)
294
{
294
{
295
    call_t *call;
295
    call_t *call;
296
    call_t saved_call;
296
    call_t saved_call;
297
    int preprocess = 0;
297
    int preprocess = 0;
298
 
298
 
299
    /* Check that the user is not sending us answer callid */
299
    /* Check that the user is not sending us answer callid */
300
    ASSERT(! (callid & 1));
300
    ASSERT(! (callid & 1));
301
    /* TODO: Check that the callid is in the dispatch table */
301
    /* TODO: Check that the callid is in the dispatch table */
302
    call = (call_t *) callid;
302
    call = (call_t *) callid;
303
 
303
 
304
    if (answer_will_preprocess(call)) {
304
    if (answer_will_preprocess(call)) {
305
        memcpy(&saved_call.data, &call->data, sizeof(call->data));
305
        memcpy(&saved_call.data, &call->data, sizeof(call->data));
306
        preprocess = 1;
306
        preprocess = 1;
307
    }
307
    }
308
    copy_from_uspace(&call->data, data, sizeof(call->data));
308
    copy_from_uspace(&call->data, data, sizeof(call->data));
309
 
309
 
310
    if (preprocess)
310
    if (preprocess)
311
        answer_preprocess(call, &saved_call);
311
        answer_preprocess(call, &saved_call);
312
   
312
   
313
    ipc_answer(&TASK->answerbox, call);
313
    ipc_answer(&TASK->answerbox, call);
314
 
314
 
315
    return 0;
315
    return 0;
316
}
316
}
317
 
317
 
318
/** Ask the other side of connection to do 'callback' connection
318
/** Ask the other side of connection to do 'callback' connection
319
 *
319
 *
320
 * @return 0 if no error, error otherwise
320
 * @return 0 if no error, error otherwise
321
 */
321
 */
322
__native sys_ipc_connect_to_me(__native phoneid, __native arg1,
322
__native sys_ipc_connect_to_me(__native phoneid, __native arg1,
323
                   __native arg2, task_id_t *taskid)
323
                   __native arg2, task_id_t *taskid)
324
{
324
{
325
    call_t call;
325
    call_t call;
326
    phone_t *phone;
326
    phone_t *phone;
327
 
327
 
328
    phone = get_phone(phoneid);
328
    phone = get_phone(phoneid);
329
    if (!phone)
329
    if (!phone)
330
        return IPC_CALLRET_FATAL;
330
        return IPC_CALLRET_FATAL;
331
 
331
 
332
    ipc_call_init(&call);
332
    ipc_call_init(&call);
333
    IPC_SET_METHOD(call.data, IPC_M_CONNECTTOME);
333
    IPC_SET_METHOD(call.data, IPC_M_CONNECTTOME);
334
    IPC_SET_ARG1(call.data, arg1);
334
    IPC_SET_ARG1(call.data, arg1);
335
    IPC_SET_ARG2(call.data, arg2);
335
    IPC_SET_ARG2(call.data, arg2);
336
   
336
   
337
    ipc_call_sync(phone, &call);
337
    ipc_call_sync(phone, &call);
338
 
338
 
339
    if (!IPC_GET_RETVAL(call.data) && taskid)
339
    if (!IPC_GET_RETVAL(call.data) && taskid)
340
        copy_to_uspace(taskid,
340
        copy_to_uspace(taskid,
341
                   &phone->callee->task->taskid,
341
                   &phone->callee->task->taskid,
342
                   sizeof(TASK->taskid));
342
                   sizeof(TASK->taskid));
343
    return IPC_GET_RETVAL(call.data);
343
    return IPC_GET_RETVAL(call.data);
344
}
344
}
345
 
345
 
346
/** Wait for incoming ipc call or answer
346
/** Wait for incoming ipc call or answer
347
 *
347
 *
348
 * Generic function - can serve either as inkernel or userspace call
348
 * Generic function - can serve either as inkernel or userspace call
349
 * - inside kernel does probably unnecessary copying of data (TODO)
349
 * - inside kernel does probably unnecessary copying of data (TODO)
350
 *
350
 *
351
 * @param result
351
 * @param result
352
 * @param taskid
352
 * @param taskid
353
 * @param flags
353
 * @param flags
354
 * @return Callid, if callid & 1, then the call is answer
354
 * @return Callid, if callid & 1, then the call is answer
355
 */
355
 */
356
inline __native sys_ipc_wait_for_call(ipc_data_t *calldata,
356
inline __native sys_ipc_wait_for_call(ipc_data_t *calldata,
357
                      task_id_t *taskid,
357
                      task_id_t *taskid,
358
                      __native flags)
358
                      __native flags)
359
                         
359
                         
360
{
360
{
361
    call_t *call;
361
    call_t *call;
362
 
362
 
363
restart:   
363
restart:   
364
    call = ipc_wait_for_call(&TASK->answerbox, flags);
364
    call = ipc_wait_for_call(&TASK->answerbox, flags);
365
 
365
 
366
    if (call->flags & IPC_CALL_ANSWERED) {
366
    if (call->flags & IPC_CALL_ANSWERED) {
367
        if (process_answer(&TASK->answerbox, call))
367
        if (process_answer(&TASK->answerbox, call))
368
            goto restart;
368
            goto restart;
369
 
369
 
370
        copy_to_uspace(calldata, &call->data, sizeof(call->data));
370
        copy_to_uspace(calldata, &call->data, sizeof(call->data));
371
        atomic_dec(&TASK->active_calls);
371
        atomic_dec(&TASK->active_calls);
372
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
372
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
373
        ipc_call_free(call);
373
        ipc_call_free(call);
374
 
374
 
375
        return ((__native)call) | IPC_CALLID_ANSWERED;
375
        return ((__native)call) | IPC_CALLID_ANSWERED;
376
    }
376
    }
377
    if (process_request(&TASK->answerbox, call))
377
    if (process_request(&TASK->answerbox, call))
378
        goto restart;
378
        goto restart;
379
    copy_to_uspace(calldata, &call->data, sizeof(call->data));
379
    copy_to_uspace(calldata, &call->data, sizeof(call->data));
380
    copy_to_uspace(taskid, (void *)&TASK->taskid, sizeof(TASK->taskid));
380
    copy_to_uspace(taskid, (void *)&TASK->taskid, sizeof(TASK->taskid));
381
    return (__native)call;
381
    return (__native)call;
382
}
382
}
383
 
383
 
384
 
384