Subversion Repositories HelenOS-historic

Rev

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

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