Subversion Repositories HelenOS-historic

Rev

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

Rev 1084 Rev 1086
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
-
 
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)
60
        return 1;
56
        return 1;
61
    return 0;
57
    return 0;
62
}
58
}
63
 
59
 
64
/** Return true if the message with this method is forwardable
60
/** Return true if the message with this method is forwardable
65
 *
61
 *
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
76
 * it to the recepient
74
 * it to the recepient
77
 */
75
 */
78
 
76
 
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
        }
111
    }
109
    }
112
}
110
}
113
 
111
 
114
/****************************************************/
112
/****************************************************/
115
/* Functions called to process received call/answer
113
/* Functions called to process received call/answer
116
 * before passing to uspace
114
 * before passing to uspace
117
 */
115
 */
118
 
116
 
119
/** Do basic kernel processing of received call answer */
117
/** Do basic kernel processing of received call answer */
120
static int process_answer(answerbox_t *box,call_t *call)
118
static int process_answer(answerbox_t *box,call_t *call)
121
{
119
{
122
    return 0;
120
    return 0;
123
}
121
}
124
 
122
 
125
/** Do basic kernel processing of received call request
123
/** Do basic kernel processing of received call request
126
 *
124
 *
127
 * @return 0 - the call should be passed to userspace, 1 - ignore call
125
 * @return 0 - the call should be passed to userspace, 1 - ignore call
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;
139
        }
137
        }
140
        IPC_SET_ARG3(call->data, phoneid);
138
        IPC_SET_ARG3(call->data, phoneid);
141
    }
139
    }
142
    return 0;
140
    return 0;
143
}
141
}
144
 
142
 
145
/** Send a call over IPC, wait for reply, return to user
143
/** Send a call over IPC, wait for reply, return to user
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))
157
        return EPERM;
155
        return EPERM;
158
 
156
 
159
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
157
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
160
 
158
 
161
    ipc_call_static_init(&call);
159
    ipc_call_static_init(&call);
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
195
 *
193
 *
196
 * @return 0 - Limit OK,   -1 - limit exceeded
194
 * @return 0 - Limit OK,   -1 - limit exceeded
197
 */
195
 */
198
static int check_call_limit(void)
196
static int check_call_limit(void)
199
{
197
{
200
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
198
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
201
        atomic_dec(&TASK->active_calls);
199
        atomic_dec(&TASK->active_calls);
202
        return -1;
200
        return -1;
203
    }
201
    }
204
    return 0;
202
    return 0;
205
}
203
}
206
 
204
 
207
/** Send an asynchronous call over ipc
205
/** Send an asynchronous call over ipc
208
 *
206
 *
209
 * @return Call identification, returns -1 on fatal error,
207
 * @return Call identification, returns -1 on fatal error,
210
           -2 on 'Too many async request, handle answers first
208
           -2 on 'Too many async request, handle answers first
211
 */
209
 */
212
__native sys_ipc_call_async_fast(__native phoneid, __native method,
210
__native sys_ipc_call_async_fast(__native phoneid, __native method,
213
                 __native arg1, __native arg2)
211
                 __native arg1, __native arg2)
214
{
212
{
215
    call_t *call;
213
    call_t *call;
216
    phone_t *phone;
214
    phone_t *phone;
217
 
215
 
218
    if (is_system_method(method))
216
    if (is_system_method(method))
219
        return IPC_CALLRET_FATAL;
217
        return IPC_CALLRET_FATAL;
220
 
218
 
221
    if (check_call_limit())
219
    if (check_call_limit())
222
        return IPC_CALLRET_TEMPORARY;
220
        return IPC_CALLRET_TEMPORARY;
223
 
221
 
224
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
222
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
225
 
223
 
226
    call = ipc_call_alloc();
224
    call = ipc_call_alloc();
227
    IPC_SET_METHOD(call->data, method);
225
    IPC_SET_METHOD(call->data, method);
228
    IPC_SET_ARG1(call->data, arg1);
226
    IPC_SET_ARG1(call->data, arg1);
229
    IPC_SET_ARG2(call->data, arg2);
227
    IPC_SET_ARG2(call->data, arg2);
230
 
228
 
231
    ipc_call(phone, call);
229
    ipc_call(phone, call);
232
 
230
 
233
    return (__native) call;
231
    return (__native) call;
234
}
232
}
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
    }
257
   
255
   
258
    ipc_call(phone, call);
256
    ipc_call(phone, call);
259
 
257
 
260
    return (__native) call;
258
    return (__native) call;
261
}
259
}
262
 
260
 
263
/** Forward received call to another destination
261
/** Forward received call to another destination
264
 *
262
 *
265
 * The arg1 and arg2 are changed in the forwarded message
263
 * The arg1 and arg2 are changed in the forwarded message
266
 *
264
 *
267
 * Warning: If implementing non-fast version, make sure that
265
 * Warning: If implementing non-fast version, make sure that
268
 *          arg3 is not rewritten for certain system IPC
266
 *          arg3 is not rewritten for certain system IPC
269
 */
267
 */
270
__native sys_ipc_forward_fast(__native callid, __native phoneid,
268
__native sys_ipc_forward_fast(__native callid, __native phoneid,
271
                  __native method, __native arg1)
269
                  __native method, __native arg1)
272
{
270
{
273
    call_t *call;
271
    call_t *call;
274
    phone_t *phone;
272
    phone_t *phone;
275
 
273
 
276
    call = get_call(callid);
274
    call = get_call(callid);
277
    if (!call)
275
    if (!call)
278
        return ENOENT;
276
        return ENOENT;
279
 
277
 
280
    GET_CHECK_PHONE(phone, phoneid, {
278
    GET_CHECK_PHONE(phone, phoneid, {
281
        IPC_SET_RETVAL(call->data, EFORWARD);
279
        IPC_SET_RETVAL(call->data, EFORWARD);
282
        ipc_answer(&TASK->answerbox, call);
280
        ipc_answer(&TASK->answerbox, call);
283
        return ENOENT;
281
        return ENOENT;
284
    });    
282
    });    
285
 
283
 
286
    if (!is_forwardable(IPC_GET_METHOD(call->data))) {
284
    if (!is_forwardable(IPC_GET_METHOD(call->data))) {
287
        IPC_SET_RETVAL(call->data, EFORWARD);
285
        IPC_SET_RETVAL(call->data, EFORWARD);
288
        ipc_answer(&TASK->answerbox, call);
286
        ipc_answer(&TASK->answerbox, call);
289
        return EPERM;
287
        return EPERM;
290
    }
288
    }
291
 
289
 
292
    /* Userspace is not allowed to change method of system methods
290
    /* Userspace is not allowed to change method of system methods
293
     * on forward, allow changing ARG1 and ARG2 by means of method and arg1
291
     * on forward, allow changing ARG1 and ARG2 by means of method and arg1
294
     */
292
     */
295
    if (is_system_method(IPC_GET_METHOD(call->data))) {
293
    if (is_system_method(IPC_GET_METHOD(call->data))) {
296
        IPC_SET_ARG1(call->data, method);
294
        IPC_SET_ARG1(call->data, method);
297
        IPC_SET_ARG2(call->data, arg1);
295
        IPC_SET_ARG2(call->data, arg1);
298
    } else {
296
    } else {
299
        IPC_SET_METHOD(call->data, method);
297
        IPC_SET_METHOD(call->data, method);
300
        IPC_SET_ARG1(call->data, arg1);
298
        IPC_SET_ARG1(call->data, arg1);
301
    }
299
    }
302
 
300
 
303
    ipc_forward(call, phone, &TASK->answerbox);
301
    ipc_forward(call, phone, &TASK->answerbox);
304
 
302
 
305
    return 0;
303
    return 0;
306
}
304
}
307
 
305
 
308
/** Send IPC answer */
306
/** Send IPC answer */
309
__native sys_ipc_answer_fast(__native callid, __native retval,
307
__native sys_ipc_answer_fast(__native callid, __native retval,
310
                 __native arg1, __native arg2)
308
                 __native arg1, __native arg2)
311
{
309
{
312
    call_t *call;
310
    call_t *call;
313
    ipc_data_t saved_data;
311
    ipc_data_t saved_data;
314
    int preprocess = 0;
312
    int preprocess = 0;
315
 
313
 
316
    call = get_call(callid);
314
    call = get_call(callid);
317
    if (!call)
315
    if (!call)
318
        return ENOENT;
316
        return ENOENT;
319
 
317
 
320
    if (answer_will_preprocess(call)) {
318
    if (answer_will_preprocess(call)) {
321
        memcpy(&saved_data, &call->data, sizeof(call->data));
319
        memcpy(&saved_data, &call->data, sizeof(call->data));
322
        preprocess = 1;
320
        preprocess = 1;
323
    }
321
    }
324
 
322
 
325
    IPC_SET_RETVAL(call->data, retval);
323
    IPC_SET_RETVAL(call->data, retval);
326
    IPC_SET_ARG1(call->data, arg1);
324
    IPC_SET_ARG1(call->data, arg1);
327
    IPC_SET_ARG2(call->data, arg2);
325
    IPC_SET_ARG2(call->data, arg2);
328
 
326
 
329
    if (preprocess)
327
    if (preprocess)
330
        answer_preprocess(call, &saved_data);
328
        answer_preprocess(call, &saved_data);
331
 
329
 
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
 
343
    call = get_call(callid);
341
    call = get_call(callid);
344
    if (!call)
342
    if (!call)
345
        return ENOENT;
343
        return ENOENT;
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);
357
 
356
 
358
    return 0;
357
    return 0;
359
}
358
}
360
 
359
 
361
/** Ask the other side of connection to do 'callback' connection
360
/** Ask the other side of connection to do 'callback' connection
362
 *
361
 *
363
 * @return 0 if no error, error otherwise
362
 * @return 0 if no error, error otherwise
364
 */
363
 */
365
__native sys_ipc_connect_to_me(__native phoneid, __native arg1,
364
__native sys_ipc_connect_to_me(__native phoneid, __native arg1,
366
                   __native arg2, task_id_t *taskid)
365
                   __native arg2, task_id_t *taskid)
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
389
 *
386
 *
390
 * @return phoneid - on success, error otherwise
387
 * @return phoneid - on success, error otherwise
391
 */
388
 */
392
__native sys_ipc_connect_me_to(__native phoneid, __native arg1,
389
__native sys_ipc_connect_me_to(__native phoneid, __native arg1,
393
                   __native arg2)
390
                   __native arg2)
394
{
391
{
395
    call_t call;
392
    call_t call;
396
    phone_t *phone;
393
    phone_t *phone;
397
    int newphid;
394
    int newphid;
398
 
395
 
399
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
396
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
400
 
397
 
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);
412
 
409
 
413
    if (IPC_GET_RETVAL(call.data)) { /* Connection failed */
410
    if (IPC_GET_RETVAL(call.data)) { /* Connection failed */
414
        phone_dealloc(newphid);
411
        phone_dealloc(newphid);
415
        return IPC_GET_RETVAL(call.data);
412
        return IPC_GET_RETVAL(call.data);
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)
-
 
-
 
420
 *
425
 *
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
 
438
restart:   
449
restart:   
439
    call = ipc_wait_for_call(&TASK->answerbox, flags);
450
    call = ipc_wait_for_call(&TASK->answerbox, flags);
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
 
-
 
459
 
480