From a599c7f95e8916600ce092da1a5486bd62edef0f Mon Sep 17 00:00:00 2001 From: Matheus Date: Fri, 12 Sep 2025 23:03:30 -0300 Subject: [PATCH] syscalls: getcpu --- kernel-src/arch/x86-64/syscall.asm | 4 +++- kernel-src/arch/x86-64/syscalllog.c | 8 +++++--- kernel-src/sys/syscalls/getcpu.c | 14 +++++++++++++ patches/mlibc/jinx-working-patch.patch | 28 +++++++++++--------------- 4 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 kernel-src/sys/syscalls/getcpu.c diff --git a/kernel-src/arch/x86-64/syscall.asm b/kernel-src/arch/x86-64/syscall.asm index 97a93354..ff7c651b 100644 --- a/kernel-src/arch/x86-64/syscall.asm +++ b/kernel-src/arch/x86-64/syscall.asm @@ -94,6 +94,7 @@ extern syscall_killthread extern syscall_shutdown extern syscall_nice extern syscall_flock +extern syscall_getcpu syscalltab: dq syscall_print dq syscall_mmap @@ -190,7 +191,8 @@ dq syscall_killthread dq syscall_shutdown dq syscall_nice dq syscall_flock -syscallcount equ 95 +dq syscall_getcpu +syscallcount equ 96 section .text global arch_syscall_entry ; on entry: diff --git a/kernel-src/arch/x86-64/syscalllog.c b/kernel-src/arch/x86-64/syscalllog.c index 8855e3d1..453e95c5 100644 --- a/kernel-src/arch/x86-64/syscalllog.c +++ b/kernel-src/arch/x86-64/syscalllog.c @@ -5,7 +5,7 @@ #ifdef SYSCALL_LOGGING -#define SYSCALL_COUNT 95 +#define SYSCALL_COUNT 96 #define LOGSTR(x) arch_e9_puts(x) static char *name[] = { @@ -103,7 +103,8 @@ static char *name[] = { "killthread", "shutdown", "nice", - "flock" + "flock", + "getcpu" }; static char *args[] = { @@ -201,7 +202,8 @@ static char *args[] = { "pid %d tid %d signal %d", // killthread "how %d", // shutdown "off %d", // nice - "fd %d op %d" // flock + "fd %d op %d", // flock + "N/A" // getcpu }; #endif diff --git a/kernel-src/sys/syscalls/getcpu.c b/kernel-src/sys/syscalls/getcpu.c new file mode 100644 index 00000000..272e85a7 --- /dev/null +++ b/kernel-src/sys/syscalls/getcpu.c @@ -0,0 +1,14 @@ +#include +#include + +syscallret_t syscall_getcpu(context_t *) { + syscallret_t ret = { + .errno = 0 + }; + + + uint32_t cpu_id = current_cpu_internal_id(); + + ret.ret = cpu_id; + return ret; +} diff --git a/patches/mlibc/jinx-working-patch.patch b/patches/mlibc/jinx-working-patch.patch index ec650797..bb51cd07 100644 --- a/patches/mlibc/jinx-working-patch.patch +++ b/patches/mlibc/jinx-working-patch.patch @@ -84,26 +84,21 @@ index d5ba7b2..a86353f 100644 long error = syscall(SYSCALL_ARCHCTL, &ret, func, (uint64_t)arg); if(error) diff --git mlibc-clean/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp -index bb85dba..5e9e21a 100644 +index bb85dba..5f12e17 100644 --- mlibc-clean/sysdeps/astral/generic/generic.cpp +++ mlibc-workdir/sysdeps/astral/generic/generic.cpp -@@ -31,6 +31,12 @@ namespace mlibc { +@@ -40,6 +40,31 @@ namespace mlibc { return 0; } + int sys_getcpu(int *cpu) { -+ *cpu = 0; -+ // TODO unstub ++ long ret; ++ // can never fail ++ syscall(SYSCALL_GETCPU, &ret); ++ *cpu = ret; + return 0; + } + - int sys_getsockopt(int fd, int layer, int number, - void *__restrict buffer, socklen_t *__restrict size) { - (void)size; -@@ -40,6 +46,23 @@ namespace mlibc { - return 0; - } - + int sys_flock(int fd, int options) { + long r; + return syscall(SYSCALL_FLOCK, &r, fd, options); @@ -124,7 +119,7 @@ index bb85dba..5e9e21a 100644 int sys_tgkill(int pid, int tid, int sig) { long ret; return syscall(SYSCALL_KILLTHREAD, &ret, pid, tid, sig); -@@ -161,7 +184,18 @@ namespace mlibc { +@@ -161,7 +186,18 @@ namespace mlibc { *bytes_written = writec; return error; } @@ -144,7 +139,7 @@ index bb85dba..5e9e21a 100644 #ifndef MLIBC_BUILDING_RTLD typedef struct { -@@ -376,6 +410,12 @@ namespace mlibc { +@@ -376,6 +412,12 @@ namespace mlibc { return ret; } @@ -157,7 +152,7 @@ index bb85dba..5e9e21a 100644 #ifndef MLIBC_BUILDING_RTLD [[noreturn]] void sys_thread_exit() { -@@ -771,7 +811,7 @@ namespace mlibc { +@@ -771,7 +813,7 @@ namespace mlibc { int sys_tcb_set(void *pointer) { long r; @@ -184,16 +179,17 @@ index d738e02..7e4f2a1 100644 #ifdef __cplusplus extern "C" { diff --git mlibc-clean/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h -index 5f42d62..6fbb253 100644 +index 5f42d62..8414286 100644 --- mlibc-clean/sysdeps/astral/include/astral/syscall.h +++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h -@@ -96,6 +96,9 @@ +@@ -96,6 +96,10 @@ #define SYSCALL_SIGTIMEDWAIT 89 #define SYSCALL_SIGPENDING 90 #define SYSCALL_KILLTHREAD 91 +#define SYSCALL_SHUTDOWN 92 +#define SYSCALL_NICE 93 +#define SYSCALL_FLOCK 94 ++#define SYSCALL_GETCPU 95 #ifndef __MLIBC_ABI_ONLY