Subversion Repositories HelenOS-historic

Rev

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

Rev 977 Rev 998
1
/*
1
/*
2
 * Copyright (C) 2005 Martin Decky
2
 * Copyright (C) 2005 Martin Decky
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 <syscall/syscall.h>
29
#include <syscall/syscall.h>
30
#include <proc/thread.h>
30
#include <proc/thread.h>
31
#include <mm/as.h>
31
#include <mm/as.h>
32
#include <print.h>
32
#include <print.h>
33
#include <putchar.h>
33
#include <putchar.h>
34
#include <ipc/ipc.h>
34
#include <ipc/ipc.h>
35
#include <errno.h>
35
#include <errno.h>
36
#include <proc/task.h>
36
#include <proc/task.h>
37
#include <arch.h>
37
#include <arch.h>
38
#include <debug.h>
38
#include <debug.h>
39
 
39
 
40
static __native sys_ctl(void) {
40
static __native sys_ctl(void) {
41
    printf("Thread finished\n");
41
    printf("Thread finished\n");
42
    thread_exit();
42
    thread_exit();
43
    /* Unreachable */
43
    /* Unreachable */
44
    return 0;
44
    return 0;
45
}
45
}
46
 
46
 
47
static __native sys_io(int fd, const void * buf, size_t count) {
47
static __native sys_io(int fd, const void * buf, size_t count) {
48
   
48
   
49
    // TODO: buf sanity checks and a lot of other stuff ...
49
    // TODO: buf sanity checks and a lot of other stuff ...
50
 
50
 
51
    size_t i;
51
    size_t i;
52
   
52
   
53
    for (i = 0; i < count; i++)
53
    for (i = 0; i < count; i++)
54
        putchar(((char *) buf)[i]);
54
        putchar(((char *) buf)[i]);
55
   
55
   
56
    return count;
56
    return count;
57
}
57
}
58
 
58
 
-
 
59
static phone_t * get_phone(__native phoneid)
-
 
60
{
-
 
61
    phone_t *phone;
-
 
62
 
-
 
63
    if (phoneid >= IPC_MAX_PHONES)
-
 
64
        return NULL;
-
 
65
 
-
 
66
    phone = &TASK->phones[phoneid];
-
 
67
    if (!phone->callee)
-
 
68
        return NULL;
-
 
69
    return phone;
-
 
70
}
-
 
71
 
59
/** Send a call over IPC, wait for reply, return to user
72
/** Send a call over IPC, wait for reply, return to user
60
 *
73
 *
61
 * @return Call identification, returns -1 on fatal error,
74
 * @return Call identification, returns -1 on fatal error,
62
           -2 on 'Too many async request, handle answers first
75
           -2 on 'Too many async request, handle answers first
63
 */
76
 */
64
static __native sys_ipc_call_sync(__native phoneid, __native method,
77
static __native sys_ipc_call_sync_fast(__native phoneid, __native method,
65
                   __native arg1, __native *data)
78
                       __native arg1, __native *data)
66
{
79
{
67
    call_t call;
80
    call_t call;
68
    phone_t *phone;
81
    phone_t *phone;
69
    /* Special answerbox for synchronous messages */
82
    /* Special answerbox for synchronous messages */
70
 
83
 
71
    if (phoneid >= IPC_MAX_PHONES)
-
 
72
        return IPC_CALLRET_FATAL;
-
 
73
 
-
 
74
    phone = &TASK->phones[phoneid];
84
    phone = get_phone(phoneid);
75
    if (!phone->callee)
85
    if (!phone)
76
        return IPC_CALLRET_FATAL;
86
        return IPC_CALLRET_FATAL;
77
 
87
 
78
    ipc_call_init(&call);
88
    ipc_call_init(&call);
79
    IPC_SET_METHOD(call.data, method);
89
    IPC_SET_METHOD(call.data, method);
80
    IPC_SET_ARG1(call.data, arg1);
90
    IPC_SET_ARG1(call.data, arg1);
81
   
91
   
82
    ipc_call_sync(phone, &call);
92
    ipc_call_sync(phone, &call);
83
 
93
 
84
    copy_to_uspace(data, &call.data, sizeof(call.data));
94
    copy_to_uspace(data, &call.data, sizeof(call.data));
85
 
95
 
86
    return 0;
96
    return 0;
87
}
97
}
88
 
98
 
-
 
99
/** Synchronous IPC call allowing to send whole message */
89
static __native sys_ipc_call_sync_medium(__native phoneid, __native *data)
100
static __native sys_ipc_call_sync(__native phoneid, __native *data)
90
{
101
{
91
    call_t call;
102
    call_t call;
92
    phone_t *phone;
103
    phone_t *phone;
93
    /* Special answerbox for synchronous messages */
104
    /* Special answerbox for synchronous messages */
94
 
105
 
95
    if (phoneid >= IPC_MAX_PHONES)
-
 
96
        return IPC_CALLRET_FATAL;
-
 
97
 
-
 
98
    phone = &TASK->phones[phoneid];
106
    phone = get_phone(phoneid);
99
    if (!phone->callee)
107
    if (!phone)
100
        return IPC_CALLRET_FATAL;
108
        return IPC_CALLRET_FATAL;
101
 
109
 
102
    ipc_call_init(&call);
110
    ipc_call_init(&call);
103
    copy_from_uspace(&call.data, data, sizeof(call.data));
111
    copy_from_uspace(&call.data, data, sizeof(call.data));
104
   
112
   
105
    ipc_call_sync(phone, &call);
113
    ipc_call_sync(phone, &call);
106
 
114
 
107
    copy_to_uspace(data, &call.data, sizeof(call.data));
115
    copy_to_uspace(data, &call.data, sizeof(call.data));
108
 
116
 
109
    return 0;
117
    return 0;
110
}
118
}
111
 
119
 
-
 
120
/** Check that the task did not exceed allowed limit
-
 
121
 *
-
 
122
 * @return 0 - Limit OK,   -1 - limit exceeded
-
 
123
 */
-
 
124
static int check_call_limit(void)
-
 
125
{
-
 
126
    if (atomic_inc_post(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
-
 
127
        atomic_dec(&TASK->active_calls);
-
 
128
        return -1;
-
 
129
    }
-
 
130
    return 0;
-
 
131
}
112
 
132
 
113
/** Send an asynchronous call over ipc
133
/** Send an asynchronous call over ipc
114
 *
134
 *
115
 * @return Call identification, returns -1 on fatal error,
135
 * @return Call identification, returns -1 on fatal error,
116
           -2 on 'Too many async request, handle answers first
136
           -2 on 'Too many async request, handle answers first
117
 */
137
 */
118
static __native sys_ipc_call_async(__native phoneid, __native method,
138
static __native sys_ipc_call_async_fast(__native phoneid, __native method,
119
                   __native arg1, __native arg2)
139
                    __native arg1, __native arg2)
120
{
140
{
121
    call_t *call;
141
    call_t *call;
122
    phone_t *phone;
142
    phone_t *phone;
123
 
143
 
124
    if (phoneid >= IPC_MAX_PHONES)
144
    phone = get_phone(phoneid);
-
 
145
    if (!phone)
125
        return IPC_CALLRET_FATAL;
146
        return IPC_CALLRET_FATAL;
126
 
147
 
127
    phone = &TASK->phones[phoneid];
-
 
128
    if (!phone->callee)
148
    if (check_call_limit())
129
        return IPC_CALLRET_FATAL;
149
        return IPC_CALLRET_TEMPORARY;
130
 
150
 
131
    /* TODO: Check that we did not exceed system imposed maximum
-
 
132
     * of asynchrnously sent messages
-
 
133
     * - the userspace should be able to handle it correctly
-
 
134
     */
-
 
135
    call = ipc_call_alloc();
151
    call = ipc_call_alloc();
136
    IPC_SET_METHOD(call->data, method);
152
    IPC_SET_METHOD(call->data, method);
137
    IPC_SET_ARG1(call->data, arg1);
153
    IPC_SET_ARG1(call->data, arg1);
138
    IPC_SET_ARG2(call->data, arg2);
154
    IPC_SET_ARG2(call->data, arg2);
139
 
155
 
140
    ipc_call(phone, call);
156
    ipc_call(phone, call);
141
 
157
 
142
    return (__native) call;
158
    return (__native) call;
143
}
159
}
144
 
160
 
-
 
161
/** Synchronous IPC call allowing to send whole message
-
 
162
 *
-
 
163
 * @return The same as sys_ipc_call_async
-
 
164
 */
-
 
165
static __native sys_ipc_call_async(__native phoneid, __native *data)
-
 
166
{
-
 
167
    call_t *call;
-
 
168
    phone_t *phone;
-
 
169
 
-
 
170
    phone = get_phone(phoneid);
-
 
171
    if (!phone)
-
 
172
        return IPC_CALLRET_FATAL;
-
 
173
 
-
 
174
    if (check_call_limit())
-
 
175
        return IPC_CALLRET_TEMPORARY;
-
 
176
 
-
 
177
    call = ipc_call_alloc();
-
 
178
    copy_from_uspace(&call->data, data, sizeof(call->data));
-
 
179
   
-
 
180
    ipc_call(phone, call);
-
 
181
 
-
 
182
    return (__native) call;
-
 
183
}
-
 
184
 
-
 
185
 
145
/** Send IPC answer */
186
/** Send IPC answer */
146
static __native sys_ipc_answer(__native callid, __native retval, __native arg1,
187
static __native sys_ipc_answer(__native callid, __native retval, __native arg1,
147
                   __native arg2)
188
                   __native arg2)
148
{
189
{
149
    call_t *call;
190
    call_t *call;
150
 
191
 
151
    /* Check that the user is not sending us answer callid */
192
    /* Check that the user is not sending us answer callid */
152
    ASSERT(! (callid & 1));
193
    ASSERT(! (callid & 1));
153
    /* TODO: Check that the callid is in the dispatch table */
194
    /* TODO: Check that the callid is in the dispatch table */
154
    call = (call_t *) callid;
195
    call = (call_t *) callid;
155
 
196
 
156
    IPC_SET_RETVAL(call->data, retval);
197
    IPC_SET_RETVAL(call->data, retval);
157
    IPC_SET_ARG1(call->data, arg1);
198
    IPC_SET_ARG1(call->data, arg1);
158
    IPC_SET_ARG2(call->data, arg2);
199
    IPC_SET_ARG2(call->data, arg2);
159
 
200
 
160
    ipc_answer(&TASK->answerbox, call);
201
    ipc_answer(&TASK->answerbox, call);
161
    return 0;
202
    return 0;
162
}
203
}
163
 
204
 
164
/** Wait for incoming ipc call or answer
205
/** Wait for incoming ipc call or answer
165
 *
206
 *
166
 * @param result
207
 * @param result
167
 * @param flags
208
 * @param flags
168
 * @return Callid, if callid & 1, then the call is answer
209
 * @return Callid, if callid & 1, then the call is answer
169
 */
210
 */
170
static __native sys_ipc_wait_for_call(__native *calldata, __native flags)
211
static __native sys_ipc_wait_for_call(__native *calldata, __native flags)
171
{
212
{
172
    call_t *call;
213
    call_t *call;
173
   
214
   
174
    call = ipc_wait_for_call(&TASK->answerbox, flags);
215
    call = ipc_wait_for_call(&TASK->answerbox, flags);
175
 
216
 
176
    copy_to_uspace(calldata, &call->data, sizeof(call->data));
217
    copy_to_uspace(calldata, &call->data, sizeof(call->data));
-
 
218
 
177
    if (call->flags & IPC_CALL_ANSWERED) {
219
    if (call->flags & IPC_CALL_ANSWERED) {
178
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
220
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
179
        ipc_call_free(call);
221
        ipc_call_free(call);
-
 
222
        atomic_dec(&TASK->active_calls);
180
        return ((__native)call) | IPC_CALLID_ANSWERED;
223
        return ((__native)call) | IPC_CALLID_ANSWERED;
181
    }
224
    }
182
    return (__native)call;
225
    return (__native)call;
183
}
226
}
184
 
227
 
185
static __native sys_mremap(void *address, size_t size, unsigned long flags)
228
static __native sys_mremap(void *address, size_t size, unsigned long flags)
186
{
229
{
187
    return as_remap(AS, (__address) address, size, 0);
230
    return as_remap(AS, (__address) address, size, 0);
188
}
231
}
189
 
232
 
190
syshandler_t syscall_table[SYSCALL_END] = {
233
syshandler_t syscall_table[SYSCALL_END] = {
191
    sys_ctl,
234
    sys_ctl,
192
    sys_io,
235
    sys_io,
193
    sys_mremap,
236
    sys_mremap,
-
 
237
    sys_ipc_call_sync_fast,
194
    sys_ipc_call_sync,
238
    sys_ipc_call_sync,
195
    sys_ipc_call_sync_medium,
239
    sys_ipc_call_async_fast,
196
    sys_ipc_call_async,
240
    sys_ipc_call_async,
197
    sys_ipc_answer,
241
    sys_ipc_answer,
198
    sys_ipc_wait_for_call
242
    sys_ipc_wait_for_call
199
};
243
};
200
 
244