Subversion Repositories HelenOS-historic

Rev

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

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