Subversion Repositories HelenOS-historic

Rev

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

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