Subversion Repositories HelenOS-historic

Rev

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

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