From: Tulio A M Mendes Date: Mon, 9 Feb 2026 22:46:39 +0000 (-0300) Subject: user: add errno-based syscall wrappers X-Git-Url: https://projects.tadryanom.me/?a=commitdiff_plain;h=fbb19c99c3889c7a4abfca99f2e9c1d78aecf3a0;p=AdrOS.git user: add errno-based syscall wrappers --- diff --git a/Makefile b/Makefile index cad1ace9..829e873e 100644 --- a/Makefile +++ b/Makefile @@ -128,10 +128,10 @@ $(MKINITRD): tools/mkinitrd.c @gcc tools/mkinitrd.c -o $(MKINITRD) $(USER_ELF): user/init.c user/linker.ld - @i686-elf-gcc -m32 -ffreestanding -fno-pie -no-pie -nostdlib -Wl,-T,user/linker.ld -o $(USER_ELF) user/init.c + @i686-elf-gcc -m32 -ffreestanding -fno-pie -no-pie -nostdlib -Wl,-T,user/linker.ld -o $(USER_ELF) user/init.c user/errno.c $(ECHO_ELF): user/echo.c user/linker.ld - @i686-elf-gcc -m32 -ffreestanding -fno-pie -no-pie -nostdlib -Wl,-T,user/linker.ld -o $(ECHO_ELF) user/echo.c + @i686-elf-gcc -m32 -ffreestanding -fno-pie -no-pie -nostdlib -Wl,-T,user/linker.ld -o $(ECHO_ELF) user/echo.c user/errno.c $(INITRD_IMG): $(MKINITRD) $(USER_ELF) $(ECHO_ELF) @./$(MKINITRD) $(INITRD_IMG) $(USER_ELF):bin/init.elf $(ECHO_ELF):bin/echo.elf diff --git a/user/echo.c b/user/echo.c index 0d6c43b6..67f828f5 100644 --- a/user/echo.c +++ b/user/echo.c @@ -9,6 +9,8 @@ #include +#include "user_errno.h" + enum { SYSCALL_WRITE = 1, SYSCALL_EXIT = 2, @@ -22,7 +24,7 @@ static int sys_write(int fd, const void* buf, uint32_t len) { : "a"(SYSCALL_WRITE), "b"(fd), "c"(buf), "d"(len) : "memory" ); - return ret; + return __syscall_fix(ret); } static uint32_t ustrlen(const char* s) { diff --git a/user/errno.c b/user/errno.c new file mode 100644 index 00000000..74d9f803 --- /dev/null +++ b/user/errno.c @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2018, Tulio A M Mendes + * All rights reserved. + * See LICENSE for details. + * + * Source: https://github.com/tadryanom/AdrOS + */ + +int errno = 0; diff --git a/user/init.c b/user/init.c index 45cbf0f3..1b7a879a 100644 --- a/user/init.c +++ b/user/init.c @@ -38,6 +38,8 @@ #undef SEEK_END #endif +#include "user_errno.h" + enum { SYSCALL_WRITE = 1, SYSCALL_EXIT = 2, @@ -132,7 +134,7 @@ static int sys_write(int fd, const void* buf, uint32_t len) { : "a"(SYSCALL_WRITE), "b"(fd), "c"(buf), "d"(len) : "memory" ); - return ret; + return __syscall_fix(ret); } static void write_int_dec(int v) { @@ -188,7 +190,7 @@ static int sys_sigaction(int sig, void (*handler)(int), uintptr_t* old_out) { : "a"(SYSCALL_SIGACTION), "b"(sig), "c"(handler), "d"(old_out) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_select(uint32_t nfds, uint64_t* readfds, uint64_t* writefds, uint64_t* exceptfds, int32_t timeout) { @@ -199,7 +201,7 @@ static int sys_select(uint32_t nfds, uint64_t* readfds, uint64_t* writefds, uint : "a"(SYSCALL_SELECT), "b"(nfds), "c"(readfds), "d"(writefds), "S"(exceptfds), "D"(timeout) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_ioctl(int fd, uint32_t cmd, void* arg) { @@ -210,7 +212,7 @@ static int sys_ioctl(int fd, uint32_t cmd, void* arg) { : "a"(SYSCALL_IOCTL), "b"(fd), "c"(cmd), "d"(arg) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_kill(int pid, int sig) { @@ -221,7 +223,7 @@ static int sys_kill(int pid, int sig) { : "a"(SYSCALL_KILL), "b"(pid), "c"(sig) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_poll(struct pollfd* fds, uint32_t nfds, int32_t timeout) { @@ -232,7 +234,7 @@ static int sys_poll(struct pollfd* fds, uint32_t nfds, int32_t timeout) { : "a"(SYSCALL_POLL), "b"(fds), "c"(nfds), "d"(timeout) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_setsid(void) { @@ -243,7 +245,7 @@ static int sys_setsid(void) { : "a"(SYSCALL_SETSID) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_setpgid(int pid, int pgid) { @@ -254,7 +256,7 @@ static int sys_setpgid(int pid, int pgid) { : "a"(SYSCALL_SETPGID), "b"(pid), "c"(pgid) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_getpgrp(void) { @@ -265,7 +267,7 @@ static int sys_getpgrp(void) { : "a"(SYSCALL_GETPGRP) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_getpid(void) { @@ -276,7 +278,7 @@ static int sys_getpid(void) { : "a"(SYSCALL_GETPID) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_getppid(void) { @@ -287,7 +289,7 @@ static int sys_getppid(void) { : "a"(SYSCALL_GETPPID) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_fork(void) { @@ -298,7 +300,7 @@ static int sys_fork(void) { : "a"(SYSCALL_FORK) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_execve(const char* path, const char* const* argv, const char* const* envp) { @@ -309,7 +311,7 @@ static int sys_execve(const char* path, const char* const* argv, const char* con : "a"(SYSCALL_EXECVE), "b"(path), "c"(argv), "d"(envp) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_pipe(int fds[2]) { @@ -320,7 +322,7 @@ static int sys_pipe(int fds[2]) { : "a"(SYSCALL_PIPE), "b"(fds) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_dup(int oldfd) { @@ -331,7 +333,7 @@ static int sys_dup(int oldfd) { : "a"(SYSCALL_DUP), "b"(oldfd) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_dup2(int oldfd, int newfd) { @@ -342,7 +344,7 @@ static int sys_dup2(int oldfd, int newfd) { : "a"(SYSCALL_DUP2), "b"(oldfd), "c"(newfd) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_waitpid(int pid, int* status, uint32_t options) { @@ -353,7 +355,7 @@ static int sys_waitpid(int pid, int* status, uint32_t options) { : "a"(SYSCALL_WAITPID), "b"(pid), "c"(status), "d"(options) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_open(const char* path, uint32_t flags) { @@ -364,7 +366,7 @@ static int sys_open(const char* path, uint32_t flags) { : "a"(SYSCALL_OPEN), "b"(path), "c"(flags) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_read(int fd, void* buf, uint32_t len) { @@ -375,7 +377,7 @@ static int sys_read(int fd, void* buf, uint32_t len) { : "a"(SYSCALL_READ), "b"(fd), "c"(buf), "d"(len) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_close(int fd) { @@ -386,7 +388,7 @@ static int sys_close(int fd) { : "a"(SYSCALL_CLOSE), "b"(fd) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_lseek(int fd, int32_t offset, int whence) { @@ -397,7 +399,7 @@ static int sys_lseek(int fd, int32_t offset, int whence) { : "a"(SYSCALL_LSEEK), "b"(fd), "c"(offset), "d"(whence) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_fstat(int fd, struct stat* st) { @@ -408,7 +410,7 @@ static int sys_fstat(int fd, struct stat* st) { : "a"(SYSCALL_FSTAT), "b"(fd), "c"(st) : "memory" ); - return ret; + return __syscall_fix(ret); } static int sys_stat(const char* path, struct stat* st) { @@ -419,7 +421,7 @@ static int sys_stat(const char* path, struct stat* st) { : "a"(SYSCALL_STAT), "b"(path), "c"(st) : "memory" ); - return ret; + return __syscall_fix(ret); } __attribute__((noreturn)) static void sys_exit(int code) { diff --git a/user/user_errno.h b/user/user_errno.h new file mode 100644 index 00000000..288b9976 --- /dev/null +++ b/user/user_errno.h @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2018, Tulio A M Mendes + * All rights reserved. + * See LICENSE for details. + * + * Source: https://github.com/tadryanom/AdrOS + */ + +#ifndef USER_ERRNO_H +#define USER_ERRNO_H + +extern int errno; + +static inline int __syscall_fix(int ret) { + if (ret < 0) { + errno = -ret; + return -1; + } + return ret; +} + +#endif