Subversion Repositories HelenOS-historic

Rev

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

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