Subversion Repositories HelenOS-historic

Rev

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

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