Subversion Repositories HelenOS-historic

Rev

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

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