Improve debuggability by eliminating system call stub in separate
[pintos-anon] / src / lib / user / syscall.c
1 #include <syscall.h>
2 #include "syscall-stub.h"
3 #include "../syscall-nr.h"
4
5 /* Invokes syscall NUMBER, passing no arguments, and returns the
6    return value as an `int'. */
7 #define syscall0(NUMBER)                                \
8         ({                                              \
9           int retval;                                   \
10           asm volatile                                  \
11             ("push %[number]; int 0x30; add %%esp, 4"   \
12                : "=a" (retval)                          \
13                : [number] "i" (NUMBER)                  \
14                : "memory");                             \
15           retval;                                       \
16         })
17
18 /* Invokes syscall NUMBER, passing argument ARG0, and returns the
19    return value as an `int'. */
20 #define syscall1(NUMBER, ARG0)                                          \
21         ({                                                              \
22           int retval;                                                   \
23           asm volatile                                                  \
24             ("push %[arg0]; push %[number]; int 0x30; add %%esp, 8"     \
25                : "=a" (retval)                                          \
26                : [number] "i" (NUMBER),                                 \
27                  [arg0] "g" (ARG0)                                      \
28                : "memory");                                             \
29           retval;                                                       \
30         })
31
32 /* Invokes syscall NUMBER, passing arguments ARG0 and ARG1, and
33    returns the return value as an `int'. */
34 #define syscall2(NUMBER, ARG0, ARG1)                    \
35         ({                                              \
36           int retval;                                   \
37           asm volatile                                  \
38             ("push %[arg1]; push %[arg0]; "             \
39              "push %[number]; int 0x30; add %%esp, 12"  \
40                : "=a" (retval)                          \
41                : [number] "i" (NUMBER),                 \
42                  [arg0] "g" (ARG0),                     \
43                  [arg1] "g" (ARG1)                      \
44                : "memory");                             \
45           retval;                                       \
46         })
47
48 /* Invokes syscall NUMBER, passing arguments ARG0, ARG1, and
49    ARG2, and returns the return value as an `int'. */
50 #define syscall3(NUMBER, ARG0, ARG1, ARG2)                      \
51         ({                                                      \
52           int retval;                                           \
53           asm volatile                                          \
54             ("push %[arg2]; push %[arg1]; push %[arg0]; "       \
55              "push %[number]; int 0x30; add %%esp, 16"          \
56                : "=a" (retval)                                  \
57                : [number] "i" (NUMBER),                         \
58                  [arg0] "g" (ARG0),                             \
59                  [arg1] "g" (ARG1),                             \
60                  [arg2] "g" (ARG2)                              \
61                : "memory");                                     \
62           retval;                                               \
63         })
64
65 void
66 halt (void) 
67 {
68   syscall0 (SYS_halt);
69   NOT_REACHED ();
70 }
71
72 void
73 exit (int status)
74 {
75   syscall1 (SYS_exit, status);
76   NOT_REACHED ();
77 }
78
79 pid_t
80 exec (const char *file)
81 {
82   return (pid_t) syscall1 (SYS_exec, file);
83 }
84
85 int
86 join (pid_t pid)
87 {
88   return syscall1 (SYS_join, pid);
89 }
90
91 bool
92 create (const char *file, unsigned initial_size)
93 {
94   return syscall2 (SYS_create, file, initial_size);
95 }
96
97 bool
98 remove (const char *file)
99 {
100   return syscall1 (SYS_remove, file);
101 }
102
103 int
104 open (const char *file)
105 {
106   return syscall1 (SYS_open, file);
107 }
108
109 int
110 filesize (int fd) 
111 {
112   return syscall1 (SYS_filesize, fd);
113 }
114
115 int
116 read (int fd, void *buffer, unsigned size)
117 {
118   return syscall3 (SYS_read, fd, buffer, size);
119 }
120
121 int
122 write (int fd, const void *buffer, unsigned size)
123 {
124   return syscall3 (SYS_write, fd, buffer, size);
125 }
126
127 void
128 seek (int fd, unsigned position) 
129 {
130   syscall2 (SYS_seek, fd, position);
131 }
132
133 unsigned
134 tell (int fd) 
135 {
136   return syscall1 (SYS_tell, fd);
137 }
138
139 void
140 close (int fd)
141 {
142   syscall1 (SYS_close, fd);
143 }
144
145 bool
146 mmap (int fd, void *addr, unsigned length)
147 {
148   return syscall3 (SYS_mmap, fd, addr, length);
149 }
150
151 bool
152 munmap (void *addr, unsigned length)
153 {
154   return syscall2 (SYS_munmap, addr, length);
155 }
156
157 bool
158 chdir (const char *dir)
159 {
160   return syscall1 (SYS_chdir, dir);
161 }
162
163 bool
164 mkdir (const char *dir)
165 {
166   return syscall1 (SYS_mkdir, dir);
167 }
168
169 void
170 lsdir (void)
171 {
172   syscall0 (SYS_lsdir);
173 }
174