syscall.h (6071B)
1 #ifndef __SYSCALL_H__ 2 #define __SYSCALL_H__ 3 4 #include <idt.h> 5 #include <common.h> 6 7 #define sys_exit(status) \ 8 ({ \ 9 int __num = __NR_exit, err_code = status; \ 10 asm volatile ("int $0x80" :: "a"(__num), "b"(err_code)); \ 11 }) 12 13 #define sys_fork() \ 14 ({ \ 15 int __num = __NR_fork; \ 16 pid_t __ret; \ 17 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \ 18 __ret; \ 19 }) 20 21 #define sys_waitpid(pid, status, options) \ 22 ({ \ 23 int __num = __NR_waitpid, *__status = status, __options = options; \ 24 pid_t __pid = pid, __ret; \ 25 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__pid), "c"(__status), "d"(__options)); \ 26 __ret; \ 27 }) 28 29 #define sys_read(fd, buf, count) \ 30 ({ \ 31 int __num = __NR_read, __fd = fd; \ 32 ssize_t __ret; \ 33 void *__buf = buf; \ 34 size_t __count = count; \ 35 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__fd), "c"(__buf), "d"(__count)); \ 36 __ret; \ 37 }) 38 39 #define sys_write(fd, buf, count) \ 40 ({ \ 41 int __num = __NR_write, __fd = fd; \ 42 ssize_t __ret; \ 43 void *__buf = buf; \ 44 size_t __count = count; \ 45 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__fd), "c"(__buf), "d"(__count)); \ 46 __ret; \ 47 }) 48 49 #define sys_reschedule() \ 50 ({ \ 51 int __num = __NR_reschedule; \ 52 asm volatile ("int $0x80" :: "a"(__num)); \ 53 }) 54 55 #define sys_open(pathname, flags, mode) \ 56 ({ \ 57 int __num = __NR_open, __flags = flags, __ret; \ 58 const char *__pathname = pathname; \ 59 mode_t __mode = mode; \ 60 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__pathname), "c"(__flags), "d"(__mode)); \ 61 __ret; \ 62 }) 63 64 #define sys_creat(pathname, mode) \ 65 ({ \ 66 int __num = __NR_creat, __ret; \ 67 mode_t __mode = mode; \ 68 const char *__pathname = pathname; \ 69 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__pathname), "c"(__mode)); \ 70 __ret; \ 71 }) 72 73 #define sys_close(fd) \ 74 ({ \ 75 int __num = __NR_close, __ret; \ 76 int __fd = fd; \ 77 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__fd)); \ 78 __ret; \ 79 }) 80 81 #define sys_isatty(fd) \ 82 ({ \ 83 int __num = __NR_isatty, __ret, __fd = fd; \ 84 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__fd)); \ 85 __ret; \ 86 }) 87 88 #define sys_getpid() \ 89 ({ \ 90 int __num = __NR_getpid; \ 91 pid_t __ret; \ 92 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \ 93 __ret; \ 94 }) 95 96 #define sys_getppid() \ 97 ({ \ 98 int __num = __NR_getppid; \ 99 pid_t __ret; \ 100 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \ 101 __ret; \ 102 }) 103 104 #define sys_readdir(dir) \ 105 ({ \ 106 int __num = __NR_readdir; \ 107 DIR *__dir = dir; \ 108 struct dirent_t *__ret; \ 109 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__dir)); \ 110 __ret; \ 111 }) 112 113 #define sys_dup(oldfd) \ 114 ({ \ 115 int __num = __NR_dup, __oldfd = oldfd, __ret; \ 116 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__oldfd)); \ 117 __ret; \ 118 }) 119 120 #define sys_dup2(oldfd, newfd) \ 121 ({ \ 122 int __num = __NR_dup, __oldfd = oldfd, __newfd = newfd, __ret; \ 123 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__oldfd), "c"(__newfd)); \ 124 __ret; \ 125 }) 126 127 #define sys_stat(path, stbuf) \ 128 ({ \ 129 int __num = __NR_stat, __ret; \ 130 const char *__path = path; \ 131 struct stat *__buf = stbuf; \ 132 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__path), "c"(__buf)); \ 133 __ret; \ 134 }) 135 136 #define sys_fstat(fd, stbuf) \ 137 ({ \ 138 int __num = __NR_fstat, __ret, __fd = fd; \ 139 struct stat *__buf = stbuf; \ 140 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__fd), "c"(__buf)); \ 141 __ret; \ 142 }) 143 144 #define sys_execve(filename, argv, envp) \ 145 ({ \ 146 int __num = __NR_execve, __ret; \ 147 const char *__filename = filename; \ 148 char **const __argv = argv; \ 149 char **const __envp = envp; \ 150 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__filename), "c"(__argv), "d"(__envp)); \ 151 __ret; \ 152 }) 153 154 #define sys_seeder() \ 155 ({ \ 156 int __num = __NR_seeder, __ret; \ 157 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \ 158 __ret; \ 159 }) 160 161 #define sys_sbrk(incr) \ 162 ({ \ 163 int __num = __NR_sbrk, __ret; \ 164 intptr_t __incr = incr; \ 165 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__incr)); \ 166 __ret; \ 167 }) 168 169 #define sys_getuid() \ 170 ({ \ 171 int __num = __NR_getuid; \ 172 uid_t __ret; \ 173 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \ 174 __ret; \ 175 }) 176 177 #define sys_setuid(uid) \ 178 ({ \ 179 int __num = __NR_setuid, __ret; \ 180 uid_t __uid = uid; \ 181 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__uid)); \ 182 __ret; \ 183 }) 184 185 #define sys_getgid() \ 186 ({ \ 187 int __num = __NR_getgid; \ 188 uid_t __ret; \ 189 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \ 190 __ret; \ 191 }) 192 193 #define sys_setgid(gid) \ 194 ({ \ 195 int __num = __NR_setgid, __ret; \ 196 uid_t __gid = gid; \ 197 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__gid)); \ 198 __ret; \ 199 }) 200 201 #define sys_suspend_task(channel) \ 202 ({ \ 203 int __num = __NR_suspend_task, __ret; \ 204 void * __channel = channel; \ 205 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__channel)); \ 206 __ret; \ 207 }) 208 209 #define sys_resume_task(channel) \ 210 ({ \ 211 int __num = __NR_resume_task; \ 212 void * __channel = channel; \ 213 asm volatile ("int $0x80" :: "a"(__num), "b"(__channel)); \ 214 }) 215 216 #define sys_pipe(pipefds) \ 217 ({ \ 218 int __num = __NR_pipe, __ret; \ 219 int *__pipefds = pipefds; \ 220 asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__pipefds)); \ 221 __ret; \ 222 }) 223 224 #define SYSCALL_NR 30 225 #define __NR_exit 1 226 #define __NR_fork 2 227 #define __NR_read 3 228 #define __NR_write 4 229 #define __NR_open 5 230 #define __NR_close 6 231 #define __NR_waitpid 7 232 #define __NR_creat 8 233 #define __NR_link 9 234 #define __NR_unlink 10 235 #define __NR_execve 11 236 #define __NR_reschedule 12 237 #define __NR_isatty 13 238 #define __NR_getpid 14 239 #define __NR_getppid 15 240 #define __NR_readdir 16 241 #define __NR_dup 17 242 #define __NR_dup2 18 243 #define __NR_stat 19 244 #define __NR_fstat 20 245 #define __NR_seeder 21 246 #define __NR_sbrk 22 247 #define __NR_getuid 23 248 #define __NR_setuid 24 249 #define __NR_getgid 25 250 #define __NR_setgid 26 251 #define __NR_suspend_task 27 252 #define __NR_resume_task 28 253 #define __NR_pipe 29 254 255 extern void syscall_dispatcher(struct trapframe_t *regs); 256 257 #endif 258