source: mainline/generic/src/syscall/syscall.c@ d3e7ff4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d3e7ff4 was d3e7ff4, checked in by Jakub Jermar <jakub@…>, 19 years ago

Add sys_mremap() syscall.

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2 * Copyright (C) 2005 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <syscall/syscall.h>
30#include <proc/thread.h>
31#include <mm/as.h>
32#include <print.h>
33#include <putchar.h>
34#include <ipc/ipc.h>
35#include <errno.h>
36#include <proc/task.h>
37#include <arch.h>
38#include <debug.h>
39
40static __native sys_ctl(void) {
41 printf("Thread finished\n");
42 thread_exit();
43 /* Unreachable */
44 return 0;
45}
46
47static __native sys_io(int fd, const void * buf, size_t count) {
48
49 // TODO: buf sanity checks and a lot of other stuff ...
50
51 size_t i;
52
53 for (i = 0; i < count; i++)
54 putchar(((char *) buf)[i]);
55
56 return count;
57}
58
59/** Send a call over IPC, wait for reply, return to user
60 *
61 * @return Call identification, returns -1 on fatal error,
62 -2 on 'Too many async request, handle answers first
63 */
64static __native sys_ipc_call_sync(__native phoneid, __native method,
65 __native arg1, __native *data)
66{
67 call_t call;
68 phone_t *phone;
69 /* Special answerbox for synchronous messages */
70
71 if (phoneid >= IPC_MAX_PHONES)
72 return IPC_CALLRET_FATAL;
73
74 phone = &TASK->phones[phoneid];
75 if (!phone->callee)
76 return IPC_CALLRET_FATAL;
77
78 ipc_call_init(&call);
79 IPC_SET_METHOD(call.data, method);
80 IPC_SET_ARG1(call.data, arg1);
81
82 ipc_call_sync(phone, &call);
83
84 copy_to_uspace(data, &call.data, sizeof(call.data));
85
86 return 0;
87}
88
89static __native sys_ipc_call_sync_medium(__native phoneid, __native *data)
90{
91 call_t call;
92 phone_t *phone;
93 /* Special answerbox for synchronous messages */
94
95 if (phoneid >= IPC_MAX_PHONES)
96 return IPC_CALLRET_FATAL;
97
98 phone = &TASK->phones[phoneid];
99 if (!phone->callee)
100 return IPC_CALLRET_FATAL;
101
102 ipc_call_init(&call);
103 copy_from_uspace(&call.data, data, sizeof(call.data));
104
105 ipc_call_sync(phone, &call);
106
107 copy_to_uspace(data, &call.data, sizeof(call.data));
108
109 return 0;
110}
111
112
113/** Send an asynchronous call over ipc
114 *
115 * @return Call identification, returns -1 on fatal error,
116 -2 on 'Too many async request, handle answers first
117 */
118static __native sys_ipc_call_async(__native phoneid, __native method,
119 __native arg1, __native arg2)
120{
121 call_t *call;
122 phone_t *phone;
123
124 if (phoneid >= IPC_MAX_PHONES)
125 return IPC_CALLRET_FATAL;
126
127 phone = &TASK->phones[phoneid];
128 if (!phone->callee)
129 return IPC_CALLRET_FATAL;
130
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();
136 IPC_SET_METHOD(call->data, method);
137 IPC_SET_ARG1(call->data, arg1);
138 IPC_SET_ARG2(call->data, arg2);
139
140 ipc_call(phone, call);
141
142 return (__native) call;
143}
144
145/** Send IPC answer */
146static __native sys_ipc_answer(__native callid, __native retval, __native arg1,
147 __native arg2)
148{
149 call_t *call;
150
151 /* Check that the user is not sending us answer callid */
152 ASSERT(! (callid & 1));
153 /* TODO: Check that the callid is in the dispatch table */
154 call = (call_t *) callid;
155
156 IPC_SET_RETVAL(call->data, retval);
157 IPC_SET_ARG1(call->data, arg1);
158 IPC_SET_ARG2(call->data, arg2);
159
160 ipc_answer(&TASK->answerbox, call);
161 return 0;
162}
163
164/** Wait for incoming ipc call or answer
165 *
166 * @param result
167 * @param flags
168 * @return Callid, if callid & 1, then the call is answer
169 */
170static __native sys_ipc_wait_for_call(__native *calldata, __native flags)
171{
172 call_t *call;
173
174 call = ipc_wait_for_call(&TASK->answerbox, flags);
175
176 copy_to_uspace(calldata, &call->data, sizeof(call->data));
177 if (call->flags & IPC_CALL_ANSWERED) {
178 ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
179 ipc_call_free(call);
180 return ((__native)call) | IPC_CALLID_ANSWERED;
181 }
182 return (__native)call;
183}
184
185static __native sys_mremap(void *address, size_t size, unsigned long flags)
186{
187 return as_remap(AS, (__address) address, size, 0);
188}
189
190syshandler_t syscall_table[SYSCALL_END] = {
191 sys_ctl,
192 sys_io,
193 sys_mremap,
194 sys_ipc_call_sync,
195 sys_ipc_call_sync_medium,
196 sys_ipc_call_async,
197 sys_ipc_answer,
198 sys_ipc_wait_for_call
199};
Note: See TracBrowser for help on using the repository browser.