Subversion Repositories HelenOS-historic

Rev

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

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