Subversion Repositories HelenOS-historic

Rev

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

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