From 3a49dbc4f9714c16ba81bc64f73d9295eb533912 Mon Sep 17 00:00:00 2001 From: tkchia Date: Wed, 10 Aug 2022 14:08:33 +0000 Subject: [PATCH] Implement __enable_tls() on bare metal --- libc/runtime/enable_tls.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libc/runtime/enable_tls.c b/libc/runtime/enable_tls.c index 7f154ba3d..8a327cddf 100644 --- a/libc/runtime/enable_tls.c +++ b/libc/runtime/enable_tls.c @@ -27,6 +27,7 @@ #include "libc/intrin/kprintf.h" #include "libc/log/libfatal.internal.h" #include "libc/macros.internal.h" +#include "libc/nexgen32e/msr.h" #include "libc/nexgen32e/threaded.h" #include "libc/nt/thread.h" #include "libc/runtime/internal.h" @@ -158,11 +159,17 @@ privileged void __enable_tls(void) { : "=a"(ax) : "0"(__NR___set_tcb), "D"(tib) : "rcx", "r11", "memory", "cc"); - } else { + } else if (IsLinux()) { asm volatile("syscall" : "=a"(ax) : "0"(__NR_linux_arch_prctl), "D"(ARCH_SET_FS), "S"(tib) : "rcx", "r11", "memory"); + } else { + uint64_t val = (uint64_t)tib; + asm volatile("wrmsr" + : /* no outputs */ + : "c"(MSR_IA32_FS_BASE), "a"((uint32_t)val), + "d"((uint32_t)(val >> 32))); } // We need to rewrite SysV _Thread_local code. You MUST use the