]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
user: add errno-based syscall wrappers
authorTulio A M Mendes <[email protected]>
Mon, 9 Feb 2026 22:46:39 +0000 (19:46 -0300)
committerTulio A M Mendes <[email protected]>
Mon, 9 Feb 2026 22:46:39 +0000 (19:46 -0300)
Makefile
user/echo.c
user/errno.c [new file with mode: 0644]
user/init.c
user/user_errno.h [new file with mode: 0644]

index cad1ace9ebf0e08490376665a4cfe78aa8c87638..829e873ec71e60aa66c396f665563778fd518373 100644 (file)
--- 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
index 0d6c43b60f054e52ed8e1d1ea5b961d2a3216f5b..67f828f5ca0858415f700d3a102d8c4b16f6d459 100644 (file)
@@ -9,6 +9,8 @@
 
 #include <stdint.h>
 
+#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 (file)
index 0000000..74d9f80
--- /dev/null
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2018, Tulio A M Mendes <[email protected]>
+ * All rights reserved.
+ * See LICENSE for details.
+ *
+ * Source: https://github.com/tadryanom/AdrOS
+ */
+
+int errno = 0;
index 45cbf0f3b2479e2427b45a4ab16844d64381e0ae..1b7a879aae571fb63a7229a8805023f0ecbe6375 100644 (file)
@@ -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 (file)
index 0000000..288b997
--- /dev/null
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2018, Tulio A M Mendes <[email protected]>
+ * 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