From 848ae7640fa5e49e8fdb15d01a820cddd58d9ee9 Mon Sep 17 00:00:00 2001 From: torgeiru Date: Wed, 29 Oct 2025 13:18:19 +0100 Subject: [PATCH] Removing arch_prctl from syscall and creating architecture specific musl calls Set_thread_area and arch_prctl are functions used for setting the thread pointer. This is used for thread local storage. Currently arch_prctl goes through a system call trap. This patches musl so that it goes through a typical function call instead. Also, this moves the implementation of these function calls to architecture, removing the need for macros. --- deps/musl/default.nix | 3 ++ deps/musl/patches/__set_thread_area.c | 8 +++++ src/arch/aarch64/CMakeLists.txt | 2 +- src/arch/aarch64/arch_musl.cpp | 15 ++++++++ src/arch/aarch64/syscall_entry.cpp | 18 ---------- src/arch/i686/CMakeLists.txt | 1 + src/arch/i686/arch_musl.cpp | 15 ++++++++ src/arch/x86_64/CMakeLists.txt | 1 + src/arch/x86_64/arch_musl.cpp | 34 ++++++++++++++++++ src/arch/x86_64/syscall_entry.cpp | 51 ++------------------------- 10 files changed, 80 insertions(+), 68 deletions(-) create mode 100644 deps/musl/patches/__set_thread_area.c create mode 100644 src/arch/aarch64/arch_musl.cpp delete mode 100644 src/arch/aarch64/syscall_entry.cpp create mode 100644 src/arch/i686/arch_musl.cpp create mode 100644 src/arch/x86_64/arch_musl.cpp diff --git a/deps/musl/default.nix b/deps/musl/default.nix index c3070d3340..3921abdcdb 100644 --- a/deps/musl/default.nix +++ b/deps/musl/default.nix @@ -27,6 +27,9 @@ stdenv.mkDerivation rec { cp ${./patches/includeos_syscalls.h} $sourceRoot/src/internal/includeos_syscalls.h cp ${./patches/syscall.h} $sourceRoot/src/internal/syscall.h + rm $sourceRoot/src/thread/x86_64/__set_thread_area.s + cp ${./patches/__set_thread_area.c} $sourceRoot/src/thread/x86_64/__set_thread_area.c + rm $sourceRoot/arch/x86_64/syscall_arch.h rm $sourceRoot/arch/i386/syscall_arch.h ''; diff --git a/deps/musl/patches/__set_thread_area.c b/deps/musl/patches/__set_thread_area.c new file mode 100644 index 0000000000..ab248a37b6 --- /dev/null +++ b/deps/musl/patches/__set_thread_area.c @@ -0,0 +1,8 @@ +#include "syscall.h" + +#define ARCH_SET_FS 0x1002 + +int __set_thread_area(void *p) +{ + return __syscall(SYS_arch_prctl, ARCH_SET_FS, p); +} diff --git a/src/arch/aarch64/CMakeLists.txt b/src/arch/aarch64/CMakeLists.txt index 4df69bf8b2..d032ae89ea 100644 --- a/src/arch/aarch64/CMakeLists.txt +++ b/src/arch/aarch64/CMakeLists.txt @@ -7,6 +7,7 @@ set(ARCH_OBJECTS # profile_intr.asm # apic_asm.asm arch_start.asm + arch_musl.cpp exceptions.asm # interrupts.asm # fiber.asm @@ -15,7 +16,6 @@ set(ARCH_SOURCES paging.cpp cpu.cpp timer.cpp - syscall_entry.cpp ) enable_language(ASM) diff --git a/src/arch/aarch64/arch_musl.cpp b/src/arch/aarch64/arch_musl.cpp new file mode 100644 index 0000000000..4a53464b42 --- /dev/null +++ b/src/arch/aarch64/arch_musl.cpp @@ -0,0 +1,15 @@ +#include +#include +#include "cpu.hpp" + +extern "C" +long syscall_SYS_set_thread_area(struct user_desc *u_desc) { + set_tpidr(u_info); // This probably still does not work + return 0; +} + +extern "C" +long syscall_SYS_arch_prctl(int code, uintptr_t ptr) { + os::panic("Arch_prctl is specific to x86!"); + return -ENOSYS; +} diff --git a/src/arch/aarch64/syscall_entry.cpp b/src/arch/aarch64/syscall_entry.cpp deleted file mode 100644 index ecc773ca55..0000000000 --- a/src/arch/aarch64/syscall_entry.cpp +++ /dev/null @@ -1,18 +0,0 @@ - - -#include "cpu.h" - - -extern "C" -long syscall_SYS_set_thread_area(struct user_desc *u_info) -{ - set_tpidr(u_info); - /* - if (UNLIKELY(!u_info)) return -EINVAL; -#ifdef __x86_64__ - x86::CPU::set_fs(u_info); -#else - x86::CPU::set_gs(u_info); -#endif*/ - return 0; -} diff --git a/src/arch/i686/CMakeLists.txt b/src/arch/i686/CMakeLists.txt index 357bf539e7..0a689c94e0 100644 --- a/src/arch/i686/CMakeLists.txt +++ b/src/arch/i686/CMakeLists.txt @@ -6,6 +6,7 @@ set(ARCH_OBJECTS profile_intr.asm apic_asm.asm arch_start.asm + arch_musl.cpp exceptions.asm interrupts.asm fiber.asm diff --git a/src/arch/i686/arch_musl.cpp b/src/arch/i686/arch_musl.cpp new file mode 100644 index 0000000000..c61a5ebfae --- /dev/null +++ b/src/arch/i686/arch_musl.cpp @@ -0,0 +1,15 @@ +#include +#include + +extern "C" +long syscall_SYS_set_thread_area(struct user_desc *u_info) +{ + os::panic("Libc not supported for 32 bit currently!"); + return -ENOSYS; +} + +extern "C" +long syscall_SYS_arch_prctl(int code, uintptr_t ptr) { + os::panic("Libc not supported for 32 bit currently!"); + return -ENOSYS; +} diff --git a/src/arch/x86_64/CMakeLists.txt b/src/arch/x86_64/CMakeLists.txt index de8d27d461..ce9be1e0d7 100644 --- a/src/arch/x86_64/CMakeLists.txt +++ b/src/arch/x86_64/CMakeLists.txt @@ -5,6 +5,7 @@ set(ARCH_OBJECTS apic_asm.asm apic_longmode.asm arch_start.asm + arch_musl.cpp exceptions.asm interrupts.asm fiber_asm.asm diff --git a/src/arch/x86_64/arch_musl.cpp b/src/arch/x86_64/arch_musl.cpp new file mode 100644 index 0000000000..f2f1f4526a --- /dev/null +++ b/src/arch/x86_64/arch_musl.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +extern "C" +long syscall_SYS_set_thread_area(struct user_desc *u_desc) { + os::panic("Setting thread pointer on 64 bit Intel/AMD should go via arch_prctl!"); + return -ENOSYS; +} + +#define ARCH_SET_GS 0x1001 +#define ARCH_SET_FS 0x1002 +#define ARCH_GET_FS 0x1003 +#define ARCH_GET_GS 0x1004 + +extern "C" +long syscall_SYS_arch_prctl(int code, uintptr_t ptr) { + switch(code){ + case ARCH_SET_GS: + if (UNLIKELY(!ptr)) return -EINVAL; + x86::CPU::set_gs((void*)ptr); + break; + case ARCH_SET_FS: + if (UNLIKELY(!ptr)) return -EINVAL; + x86::CPU::set_fs((void*)ptr); + break; + case ARCH_GET_GS: + os::panic(" GET_GS called!\n"); + case ARCH_GET_FS: + os::panic(" GET_FS called!\n"); + } + return 0; +} diff --git a/src/arch/x86_64/syscall_entry.cpp b/src/arch/x86_64/syscall_entry.cpp index 21f461e232..507372b15b 100644 --- a/src/arch/x86_64/syscall_entry.cpp +++ b/src/arch/x86_64/syscall_entry.cpp @@ -14,63 +14,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include -#include #include -#include - -#define ARCH_SET_GS 0x1001 -#define ARCH_SET_FS 0x1002 -#define ARCH_GET_FS 0x1003 -#define ARCH_GET_GS 0x1004 - -#ifdef __x86_64__ -static long sys_prctl(int code, uintptr_t ptr) -{ - switch(code){ - case ARCH_SET_GS: - //kprintf(" set_gs to %#lx\n", ptr); - if (UNLIKELY(!ptr)) return -EINVAL; - x86::CPU::set_gs((void*)ptr); - break; - case ARCH_SET_FS: - //kprintf(" set_fs to %#lx\n", ptr); - if (UNLIKELY(!ptr)) return -EINVAL; - x86::CPU::set_fs((void*)ptr); - break; - case ARCH_GET_GS: - os::panic(" GET_GS called!\n"); - case ARCH_GET_FS: - os::panic(" GET_FS called!\n"); - } - return -EINVAL; -} -#endif extern "C" uintptr_t syscall_entry(uint64_t n, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5) { - switch(n) { - case 158: // arch_prctl - sys_prctl(a1, a2); - break; - default: - kprintf(" no %lu (a1=%#lx a2=%#lx a3=%#lx a4=%#lx a5=%#lx) \n", + kprintf(" no %lu (a1=%#lx a2=%#lx a3=%#lx a4=%#lx a5=%#lx) \n", n, a1, a2, a3, a4, a5); - } + os::panic("Syscalls are not implemented in IncludeOS!"); return 0; } -extern "C" -long syscall_SYS_set_thread_area(struct user_desc *u_info) -{ - if (UNLIKELY(!u_info)) return -EINVAL; -#ifdef __x86_64__ - x86::CPU::set_fs(u_info); -#else - x86::CPU::set_gs(u_info); -#endif - return 0; -}