cynix

x86 UNIX-like OS
git clone git://git.2f30.org/cynix
Log | Files | Refs | README | LICENSE

syscalls.c (8704B)


      1 /* syscalls glue for newlib */
      2 
      3 #include <sys/stat.h>
      4 #include <sys/types.h>
      5 #include <sys/fcntl.h>
      6 #include <sys/times.h>
      7 #include <sys/errno.h>
      8 #include <sys/time.h>
      9 #include <stdio.h>
     10 
     11 #define sys_exit(status) \
     12 	({ \
     13 		int __num = __NR_exit, err_code = status; \
     14 		asm volatile ("int $0x80" :: "a"(__num), "b"(err_code)); \
     15 	})
     16 
     17 #define sys_fork() \
     18 	({ \
     19 		int __num = __NR_fork; \
     20 		pid_t __ret; \
     21 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \
     22 		__ret; \
     23 	})
     24 
     25 #define sys_waitpid(pid, status, options) \
     26 	({ \
     27 		int __num = __NR_waitpid, *__status = status, __options = options; \
     28 		pid_t __pid = pid, __ret; \
     29 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__pid), "c"(__status), "d"(__options)); \
     30 		__ret; \
     31 	})
     32 
     33 #define sys_read(fd, buf, count) \
     34 	({ \
     35 		int __num = __NR_read, __fd = fd; \
     36 		ssize_t __ret; \
     37 		void *__buf = buf; \
     38 		size_t __count = count; \
     39 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__fd), "c"(__buf), "d"(__count)); \
     40 		__ret; \
     41 	})
     42 
     43 #define sys_write(fd, buf, count) \
     44 	({ \
     45 		int __num = __NR_write, __fd = fd; \
     46 		ssize_t __ret; \
     47 		void *__buf = buf; \
     48 		size_t __count = count; \
     49 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__fd), "c"(__buf), "d"(__count)); \
     50 		__ret; \
     51 	})
     52 
     53 #define sys_reschedule() \
     54 	({ \
     55 		int __num = __NR_reschedule; \
     56 		asm volatile ("int $0x80" :: "a"(__num)); \
     57 	})
     58 
     59 #define sys_open(pathname, flags, mode) \
     60 	({ \
     61 		int __num = __NR_open, __flags = flags, __ret; \
     62 		const char *__pathname = pathname; \
     63 		mode_t __mode = mode; \
     64 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__pathname), "c"(__flags), "d"(__mode)); \
     65 		__ret; \
     66 	})
     67 
     68 #define sys_creat(pathname, mode) \
     69 	({ \
     70 		int __num = __NR_creat, __ret; \
     71 		mode_t __mode = mode; \
     72 		const char *__pathname = pathname; \
     73 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__pathname), "c"(__mode)); \
     74 		__ret; \
     75 	})
     76 
     77 #define sys_close(fd) \
     78 	({ \
     79 		int __num = __NR_close, __ret; \
     80 		int __fd = fd; \
     81 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__fd)); \
     82 		__ret; \
     83 	})
     84 
     85 #define sys_isatty(fd) \
     86 	({ \
     87 		int __num = __NR_isatty, __ret, __fd = fd; \
     88 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__fd)); \
     89 		__ret; \
     90 	})
     91 
     92 #define sys_getpid() \
     93 	({ \
     94 		int __num = __NR_getpid; \
     95 		pid_t __ret; \
     96 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \
     97 		__ret; \
     98 	})
     99 
    100 #define sys_getppid() \
    101 	({ \
    102 		int __num = __NR_getppid; \
    103 		pid_t __ret; \
    104 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \
    105 		__ret; \
    106 	})
    107 
    108 #define sys_readdir() \
    109 	({ \
    110 		int __num = __NR_readdir; \
    111 		struct dirent_t *__ret; \
    112 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \
    113 		__ret; \
    114 	})
    115 
    116 #define sys_dup(oldfd) \
    117 	({ \
    118 		int __num = __NR_dup, __oldfd = oldfd, __ret; \
    119 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__oldfd)); \
    120 		__ret; \
    121 	})
    122 
    123 #define sys_dup2(oldfd, newfd) \
    124 	({ \
    125 		int __num = __NR_dup, __oldfd = oldfd, __newfd = newfd, __ret; \
    126 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__oldfd), "c"(__newfd)); \
    127 		__ret; \
    128 	})
    129 
    130 #define sys_stat(path, stbuf) \
    131 	({ \
    132 		int __num = __NR_stat, __ret; \
    133 		const char *__path = path; \
    134 		struct stat *__buf = stbuf; \
    135 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__path), "c"(__buf)); \
    136 		__ret; \
    137 	})
    138 
    139 #define sys_fstat(fd, stbuf) \
    140 	({ \
    141 		int __num = __NR_fstat, __ret, __fd = fd; \
    142 		struct stat *__buf = stbuf; \
    143 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__fd), "c"(__buf)); \
    144 		__ret; \
    145 	})
    146 
    147 #define sys_execve(filename, argv, envp) \
    148 	({ \
    149 		int __num = __NR_execve, __ret; \
    150 		const char *__filename = filename; \
    151 		char **const __argv = argv; \
    152 		char **const __envp = envp; \
    153 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__filename), "c"(__argv), "d"(__envp)); \
    154 		__ret; \
    155 	})
    156 
    157 #define sys_seeder() \
    158 	({ \
    159 		int __num = __NR_seeder, __ret; \
    160 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \
    161 		__ret; \
    162 	})
    163 
    164 #define sys_sbrk(incr) \
    165 	({ \
    166 		int __num = __NR_sbrk, __ret; \
    167 		long __incr = incr; \
    168 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__incr)); \
    169 		__ret; \
    170 	})
    171 
    172 #define sys_getuid() \
    173 	({ \
    174 		int __num = __NR_getuid; \
    175 		uid_t __ret; \
    176 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \
    177 		__ret; \
    178 	})
    179 
    180 #define sys_setuid(uid) \
    181 	({ \
    182 		int __num = __NR_setuid, __ret; \
    183 		uid_t __uid = uid; \
    184 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__uid)); \
    185 		__ret; \
    186 	})
    187 
    188 #define sys_getgid() \
    189 	({ \
    190 		int __num = __NR_getgid; \
    191 		uid_t __ret; \
    192 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num)); \
    193 		__ret; \
    194 	})
    195 
    196 #define sys_setgid(gid) \
    197 	({ \
    198 		int __num = __NR_setgid, __ret; \
    199 		uid_t __gid = gid; \
    200 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__gid)); \
    201 		__ret; \
    202 	})
    203 
    204 #define sys_suspend_task(channel) \
    205 	({ \
    206 		int __num = __NR_suspend_task, __ret; \
    207 		void * __channel = channel; \
    208 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__channel)); \
    209 		__ret; \
    210 	})
    211 
    212 #define sys_resume_task(channel) \
    213 	({ \
    214 		int __num = __NR_resume_task; \
    215 		void * __channel = channel; \
    216 		asm volatile ("int $0x80" :: "a"(__num), "b"(__channel)); \
    217 	})
    218 
    219 #define sys_pipe(pipefds) \
    220 	({ \
    221 		int __num = __NR_pipe, __ret; \
    222 		int *__pipefds = pipefds; \
    223 		asm volatile ("int $0x80" : "=a"(__ret) : "0"(__num), "b"(__pipefds)); \
    224 		__ret; \
    225 	})
    226 
    227 #define SYSCALL_NR 30
    228 #define __NR_exit 1
    229 #define __NR_fork 2
    230 #define __NR_read 3
    231 #define __NR_write 4
    232 #define __NR_open 5
    233 #define __NR_close 6
    234 #define __NR_waitpid 7
    235 #define __NR_creat 8
    236 #define __NR_link 9
    237 #define __NR_unlink 10
    238 #define __NR_execve 11
    239 #define __NR_reschedule 12
    240 #define __NR_isatty 13
    241 #define __NR_getpid 14
    242 #define __NR_getppid 15
    243 #define __NR_readdir 16
    244 #define __NR_dup 17
    245 #define __NR_dup2 18
    246 #define __NR_stat 19
    247 #define __NR_fstat 20
    248 #define __NR_seeder 21
    249 #define __NR_sbrk 22
    250 #define __NR_getuid 23
    251 #define __NR_setuid 24
    252 #define __NR_getgid 25
    253 #define __NR_setgid 26
    254 #define __NR_suspend_task 27
    255 #define __NR_resume_task 28
    256 #define __NR_pipe 29
    257 
    258 char **environ;
    259 
    260 int
    261 pipe(int pipefd[2])
    262 {
    263 	int ret;
    264 
    265 	ret = sys_pipe(pipefd);
    266 	if (ret < 0) {
    267 		errno = -ret;
    268 		return -1;
    269 	}
    270 	return ret;
    271 }
    272 
    273 uid_t
    274 getuid(void)
    275 {
    276 	return sys_getuid();
    277 }
    278 
    279 int
    280 setuid(uid_t uid)
    281 {
    282 	int ret;
    283 
    284 	ret = sys_setuid(uid);
    285 	if (ret < 0) {
    286 		errno = -ret;
    287 		return -1;
    288 	}
    289 	return ret;
    290 }
    291 
    292 gid_t
    293 getgid(void)
    294 {
    295 	return sys_getgid();
    296 }
    297 
    298 int
    299 setgid(gid_t gid)
    300 {
    301 	int ret;
    302 
    303 	ret = sys_setgid(gid);
    304 	if (ret < 0) {
    305 		errno = -ret;
    306 		return -1;
    307 	}
    308 	return ret;
    309 }
    310 
    311 void
    312 _exit(void)
    313 {
    314 	sys_exit(0);
    315 }
    316 
    317 int
    318 close(int file)
    319 {
    320 	int ret;
    321 
    322 	ret = sys_close(file);
    323 	if (ret < 0) {
    324 		errno = -ret;
    325 		return -1;
    326 	}
    327 	return ret;
    328 }
    329 
    330 int
    331 execve(char *name, char **argv, char **env)
    332 {
    333 	int ret;
    334 
    335 	ret = sys_execve(name, argv, env);
    336 	if (ret < 0) {
    337 		errno = -ret;
    338 		return -1;
    339 	}
    340 	return ret;
    341 }
    342 
    343 int
    344 fork(void)
    345 {
    346 	int ret;
    347 
    348 	ret = sys_fork();
    349 	if (ret < 0) {
    350 		errno = -ret;
    351 		return -1;
    352 	}
    353 	return ret;
    354 }
    355 
    356 int
    357 fstat(int file, struct stat *st)
    358 {
    359 	int ret;
    360 
    361 	ret = sys_fstat(file, st);
    362 	if (ret < 0) {
    363 		errno = -ret;
    364 		return -1;
    365 	}
    366 	return ret;
    367 }
    368 
    369 int
    370 dup(int fildes)
    371 {
    372 	int ret;
    373 
    374 	ret = sys_dup(fildes);
    375 	if (ret < 0) {
    376 		errno = -ret;
    377 		return -1;
    378 	}
    379 	return ret;
    380 }
    381 
    382 int
    383 dup2(int fildes, int fildes2)
    384 {
    385 	int ret;
    386 
    387 	ret = sys_dup2(fildes, fildes2);
    388 	if (ret < 0) {
    389 		errno = -ret;
    390 		return -1;
    391 	}
    392 	return ret;
    393 }
    394 
    395 int
    396 getpid(void)
    397 {
    398 	return sys_getpid();
    399 }
    400 
    401 int
    402 isatty(int file)
    403 {
    404 	return sys_isatty(file);
    405 }
    406 
    407 int
    408 kill(int pid, int sig)
    409 {
    410 	errno = ENOSYS;
    411 	return -1;
    412 }
    413 
    414 int
    415 link(char *old, char *new)
    416 {
    417 	errno = ENOSYS;
    418 	return -1;
    419 }
    420 
    421 int
    422 lseek(int file, int ptr, int dir)
    423 {
    424 	errno = ENOSYS;
    425 	return -1;
    426 }
    427 
    428 int
    429 open(const char *name, int flags, ...)
    430 {
    431 	int ret;
    432 
    433 	ret = sys_open(name, flags, 0);
    434 	if (ret < 0) {
    435 		errno = -ret;
    436 		return -1;
    437 	}
    438 	return ret;
    439 }
    440 
    441 int
    442 read(int file, char *ptr, int len)
    443 {
    444 	int ret;
    445 
    446 	ret = sys_read(file, ptr, len);
    447 	if (ret < 0) {
    448 		errno = -ret;
    449 		return -1;
    450 	}
    451 	return ret;
    452 }
    453 
    454 caddr_t
    455 sbrk(int incr)
    456 {
    457 	caddr_t ret;
    458 
    459 	ret = (caddr_t)sys_sbrk(incr);
    460 	return ret;
    461 }
    462 
    463 int
    464 stat(const char *file, struct stat *st)
    465 {
    466 	int ret;
    467 
    468 	ret = sys_stat(file, st);
    469 	if (ret < 0) {
    470 		errno = -ret;
    471 		return -1;
    472 	}
    473 	return ret;
    474 }
    475 
    476 clock_t
    477 times(struct tms *buf)
    478 {
    479 	errno = ENOSYS;
    480 	return -1;
    481 }
    482 
    483 int
    484 unlink(char *name)
    485 {
    486 	errno = ENOSYS;
    487 	return -1;
    488 }
    489 
    490 int
    491 wait(int *status)
    492 {
    493 	int ret;
    494 
    495 	ret = sys_waitpid(-1, status, 0);
    496 	if (ret < 0) {
    497 		errno = -ret;
    498 		return -1;
    499 	}
    500 	return ret;
    501 }
    502 
    503 int
    504 write(int file, char *ptr, int len)
    505 {
    506 	int ret;
    507 
    508 	ret = sys_write(file, ptr, len);
    509 	if (ret < 0) {
    510 		errno = -ret;
    511 		return -1;
    512 	}
    513 	return ret;
    514 }
    515 
    516 int
    517 gettimeofday(struct timeval *p, void *z)
    518 {
    519 	errno = ENOSYS;
    520 	return -1;
    521 }
    522