Subversion Repositories HelenOS

Rev

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

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