Subversion Repositories HelenOS-historic

Rev

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

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