Subversion Repositories HelenOS

Rev

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

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