Subversion Repositories HelenOS-historic

Rev

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

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