syscall.c
1 |
#include <syscall.h> |
---|---|
2 |
#include "../syscall-nr.h" |
3 |
|
4 |
/* Invokes syscall NUMBER, passing no arguments, and returns the
|
5 |
return value as an `int'. */
|
6 |
#define syscall0(NUMBER) \
|
7 |
({ \ |
8 |
int retval; \
|
9 |
asm volatile \ |
10 |
("pushl %[number]; int $0x30; addl $4, %%esp" \
|
11 |
: "=a" (retval) \
|
12 |
: [number] "i" (NUMBER) \
|
13 |
: "memory"); \
|
14 |
retval; \ |
15 |
}) |
16 |
|
17 |
/* Invokes syscall NUMBER, passing argument ARG0, and returns the
|
18 |
return value as an `int'. */
|
19 |
#define syscall1(NUMBER, ARG0) \
|
20 |
({ \ |
21 |
int retval; \
|
22 |
asm volatile \ |
23 |
("pushl %[arg0]; pushl %[number]; int $0x30; addl $8, %%esp" \
|
24 |
: "=a" (retval) \
|
25 |
: [number] "i" (NUMBER), \
|
26 |
[arg0] "g" (ARG0) \
|
27 |
: "memory"); \
|
28 |
retval; \ |
29 |
}) |
30 |
|
31 |
/* Invokes syscall NUMBER, passing arguments ARG0 and ARG1, and
|
32 |
returns the return value as an `int'. */
|
33 |
#define syscall2(NUMBER, ARG0, ARG1) \
|
34 |
({ \ |
35 |
int retval; \
|
36 |
asm volatile \ |
37 |
("pushl %[arg1]; pushl %[arg0]; " \
|
38 |
"pushl %[number]; int $0x30; addl $12, %%esp" \
|
39 |
: "=a" (retval) \
|
40 |
: [number] "i" (NUMBER), \
|
41 |
[arg0] "g" (ARG0), \
|
42 |
[arg1] "g" (ARG1) \
|
43 |
: "memory"); \
|
44 |
retval; \ |
45 |
}) |
46 |
|
47 |
/* Invokes syscall NUMBER, passing arguments ARG0, ARG1, and
|
48 |
ARG2, and returns the return value as an `int'. */
|
49 |
#define syscall3(NUMBER, ARG0, ARG1, ARG2) \
|
50 |
({ \ |
51 |
int retval; \
|
52 |
asm volatile \ |
53 |
("pushl %[arg2]; pushl %[arg1]; pushl %[arg0]; " \
|
54 |
"pushl %[number]; int $0x30; addl $16, %%esp" \
|
55 |
: "=a" (retval) \
|
56 |
: [number] "i" (NUMBER), \
|
57 |
[arg0] "g" (ARG0), \
|
58 |
[arg1] "g" (ARG1), \
|
59 |
[arg2] "g" (ARG2) \
|
60 |
: "memory"); \
|
61 |
retval; \ |
62 |
}) |
63 |
|
64 |
struct syscall_args
|
65 |
{ |
66 |
int a0;
|
67 |
int a1;
|
68 |
int a2;
|
69 |
}; |
70 |
|
71 |
void
|
72 |
halt (void)
|
73 |
{ |
74 |
syscall0 (SYS_HALT); |
75 |
NOT_REACHED (); |
76 |
} |
77 |
|
78 |
void
|
79 |
exit (int status)
|
80 |
{ |
81 |
static struct syscall_args ag; |
82 |
ag.a0 = (int) status;
|
83 |
syscall1 (SYS_EXIT, ag.a0); |
84 |
//syscall1 (SYS_EXIT, status);
|
85 |
NOT_REACHED (); |
86 |
} |
87 |
|
88 |
pid_t |
89 |
exec (const char *file) |
90 |
{ |
91 |
static struct syscall_args ag; |
92 |
ag.a0 = (int) file;
|
93 |
return syscall1 (SYS_EXEC, ag.a0);
|
94 |
//return (pid_t) syscall1 (SYS_EXEC, file);
|
95 |
} |
96 |
|
97 |
int
|
98 |
wait (pid_t pid) |
99 |
{ |
100 |
static struct syscall_args ag; |
101 |
ag.a0 = (int) pid;
|
102 |
return syscall1 (SYS_WAIT, ag.a0);
|
103 |
//return syscall1 (SYS_WAIT, pid);
|
104 |
} |
105 |
|
106 |
bool
|
107 |
create (const char *file, unsigned initial_size) |
108 |
{ |
109 |
static struct syscall_args ag; |
110 |
ag.a0 = (int) file;
|
111 |
ag.a1 = (int) initial_size;
|
112 |
return syscall2 (SYS_CREATE, ag.a0, ag.a1);
|
113 |
//return syscall2 (SYS_CREATE, file, initial_size);
|
114 |
} |
115 |
|
116 |
bool
|
117 |
remove (const char *file) |
118 |
{ |
119 |
static struct syscall_args ag; |
120 |
ag.a0 = (int) file;
|
121 |
return syscall1 (SYS_REMOVE, ag.a0);
|
122 |
//return syscall1 (SYS_REMOVE, file);
|
123 |
} |
124 |
|
125 |
int
|
126 |
open (const char *file) |
127 |
{ |
128 |
static struct syscall_args ag; |
129 |
ag.a0 = (int) file;
|
130 |
return syscall1 (SYS_OPEN, ag.a0);
|
131 |
//return syscall1 (SYS_OPEN, file);
|
132 |
} |
133 |
|
134 |
int
|
135 |
filesize (int fd)
|
136 |
{ |
137 |
static struct syscall_args ag; |
138 |
ag.a0 = (int) fd;
|
139 |
return syscall1 (SYS_FILESIZE, ag.a0);
|
140 |
//return syscall1 (SYS_FILESIZE, fd);
|
141 |
} |
142 |
|
143 |
int
|
144 |
read (int fd, void *buffer, unsigned size) |
145 |
{ |
146 |
static struct syscall_args ag; |
147 |
ag.a0 = (int) fd;
|
148 |
ag.a1 = (int) buffer;
|
149 |
ag.a2 = (int) size;
|
150 |
return syscall3 (SYS_READ, ag.a0, ag.a1, ag.a2);
|
151 |
//return syscall3 (SYS_READ, fd, buffer, size);
|
152 |
} |
153 |
|
154 |
int
|
155 |
write (int fd, const void *buffer, unsigned size) |
156 |
{ |
157 |
static struct syscall_args ag; |
158 |
ag.a0 = (int) fd;
|
159 |
ag.a1 = (int) buffer;
|
160 |
ag.a2 = (int) size;
|
161 |
return syscall3 (SYS_WRITE, ag.a0, ag.a1, ag.a2);
|
162 |
//return syscall3 (SYS_WRITE, fd, buffer, size);
|
163 |
} |
164 |
|
165 |
void
|
166 |
seek (int fd, unsigned position) |
167 |
{ |
168 |
static struct syscall_args ag; |
169 |
ag.a0 = (int) fd;
|
170 |
ag.a1 = (int) position;
|
171 |
return syscall2 (SYS_SEEK, ag.a0, ag.a1);
|
172 |
//syscall2 (SYS_SEEK, fd, position);
|
173 |
} |
174 |
|
175 |
unsigned
|
176 |
tell (int fd)
|
177 |
{ |
178 |
static struct syscall_args ag; |
179 |
ag.a0 = (int) fd;
|
180 |
return syscall1 (SYS_TELL, ag.a0);
|
181 |
//return syscall1 (SYS_TELL, fd);
|
182 |
} |
183 |
|
184 |
void
|
185 |
close (int fd)
|
186 |
{ |
187 |
static struct syscall_args ag; |
188 |
ag.a0 = (int) fd;
|
189 |
syscall1 (SYS_CLOSE, ag.a0); |
190 |
//syscall1 (SYS_CLOSE, fd);
|
191 |
} |
192 |
|
193 |
mapid_t |
194 |
mmap (int fd, void *addr) |
195 |
{ |
196 |
static struct syscall_args ag; |
197 |
ag.a0 = (int) fd;
|
198 |
ag.a1 = (int) addr;
|
199 |
return syscall2 (SYS_MMAP, ag.a0, ag.a1);
|
200 |
//return syscall2 (SYS_MMAP, fd, addr);
|
201 |
} |
202 |
|
203 |
void
|
204 |
munmap (mapid_t mapid) |
205 |
{ |
206 |
static struct syscall_args ag; |
207 |
ag.a0 = (int) mapid;
|
208 |
syscall1 (SYS_MUNMAP, ag.a0); |
209 |
//syscall1 (SYS_MUNMAP, mapid);
|
210 |
} |
211 |
|
212 |
bool
|
213 |
chdir (const char *dir) |
214 |
{ |
215 |
static struct syscall_args ag; |
216 |
ag.a0 = (int) dir;
|
217 |
return syscall1 (SYS_CHDIR, ag.a0);
|
218 |
//return syscall1 (SYS_CHDIR, dir);
|
219 |
} |
220 |
|
221 |
bool
|
222 |
mkdir (const char *dir) |
223 |
{ |
224 |
static struct syscall_args ag; |
225 |
ag.a0 = (int) dir;
|
226 |
return syscall1 (SYS_MKDIR, ag.a0);
|
227 |
//return syscall1 (SYS_MKDIR, dir);
|
228 |
} |
229 |
|
230 |
bool
|
231 |
readdir (int fd, char name[READDIR_MAX_LEN + 1]) |
232 |
{ |
233 |
static struct syscall_args ag; |
234 |
ag.a0 = (int) fd;
|
235 |
ag.a1 = (int) name;
|
236 |
return syscall2 (SYS_READDIR, ag.a0, ag.a1);
|
237 |
//return syscall2 (SYS_READDIR, fd, name);
|
238 |
} |
239 |
|
240 |
bool
|
241 |
isdir (int fd)
|
242 |
{ |
243 |
static struct syscall_args ag; |
244 |
ag.a0 = (int) fd;
|
245 |
return syscall1 (SYS_ISDIR, ag.a0);
|
246 |
//return syscall1 (SYS_ISDIR, fd);
|
247 |
} |
248 |
|
249 |
int
|
250 |
inumber (int fd)
|
251 |
{ |
252 |
static struct syscall_args ag; |
253 |
ag.a0 = (int) fd;
|
254 |
return syscall1 (SYS_INUMBER, ag.a0);
|
255 |
//return syscall1 (SYS_INUMBER, fd);
|
256 |
} |