Make tests public. Rewrite most tests. Add tests.
[pintos-anon] / src / lib / user / 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             ("push %[number]; int 0x30; add %%esp, 4"   \
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             ("push %[arg0]; push %[number]; int 0x30; add %%esp, 8"     \
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             ("push %[arg1]; push %[arg0]; "             \
38              "push %[number]; int 0x30; add %%esp, 12"  \
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             ("push %[arg2]; push %[arg1]; push %[arg0]; "       \
54              "push %[number]; int 0x30; add %%esp, 16"          \
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 void
65 halt (void) 
66 {
67   syscall0 (SYS_halt);
68   NOT_REACHED ();
69 }
70
71 void
72 exit (int status)
73 {
74   syscall1 (SYS_exit, status);
75   NOT_REACHED ();
76 }
77
78 pid_t
79 exec (const char *file)
80 {
81   return (pid_t) syscall1 (SYS_exec, file);
82 }
83
84 int
85 wait (pid_t pid)
86 {
87   return syscall1 (SYS_wait, pid);
88 }
89
90 bool
91 create (const char *file, unsigned initial_size)
92 {
93   return syscall2 (SYS_create, file, initial_size);
94 }
95
96 bool
97 remove (const char *file)
98 {
99   return syscall1 (SYS_remove, file);
100 }
101
102 int
103 open (const char *file)
104 {
105   return syscall1 (SYS_open, file);
106 }
107
108 int
109 filesize (int fd) 
110 {
111   return syscall1 (SYS_filesize, fd);
112 }
113
114 int
115 read (int fd, void *buffer, unsigned size)
116 {
117   return syscall3 (SYS_read, fd, buffer, size);
118 }
119
120 int
121 write (int fd, const void *buffer, unsigned size)
122 {
123   return syscall3 (SYS_write, fd, buffer, size);
124 }
125
126 void
127 seek (int fd, unsigned position) 
128 {
129   syscall2 (SYS_seek, fd, position);
130 }
131
132 unsigned
133 tell (int fd) 
134 {
135   return syscall1 (SYS_tell, fd);
136 }
137
138 void
139 close (int fd)
140 {
141   syscall1 (SYS_close, fd);
142 }
143
144 mapid_t
145 mmap (int fd, void *addr)
146 {
147   return syscall2 (SYS_mmap, fd, addr);
148 }
149
150 void
151 munmap (mapid_t mapid)
152 {
153   syscall1 (SYS_munmap, mapid);
154 }
155
156 bool
157 chdir (const char *dir)
158 {
159   return syscall1 (SYS_chdir, dir);
160 }
161
162 bool
163 mkdir (const char *dir)
164 {
165   return syscall1 (SYS_mkdir, dir);
166 }
167
168 void
169 lsdir (void)
170 {
171   syscall0 (SYS_lsdir);
172 }
173