Get hello.com working on metal again (#529)

* Fix deterministic startup stack setup, especially for bare metal
* Implement __enable_tls() on bare metal
* Get __get_tls_privileged() working on bare metal
This commit is contained in:
tkchia 2022-08-14 07:14:02 +08:00 committed by GitHub
parent ad775a75b8
commit 62ca1b0902
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 4 deletions

View file

@ -27,7 +27,7 @@ static noasan inline char *__get_tls(void) {
*/ */
static noasan inline char *__get_tls_privileged(void) { static noasan inline char *__get_tls_privileged(void) {
char *tib, *lin = (char *)0x30; char *tib, *lin = (char *)0x30;
if (IsLinux() || IsFreebsd() || IsNetbsd() || IsOpenbsd()) { if (IsLinux() || IsFreebsd() || IsNetbsd() || IsOpenbsd() || IsMetal()) {
asm("mov\t%%fs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory"); asm("mov\t%%fs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");
} else { } else {
asm("mov\t%%gs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory"); asm("mov\t%%gs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");

View file

@ -99,6 +99,8 @@ cosmo: push %rbp
.init.start 304,_init_stack .init.start 304,_init_stack
testb IsWindows() testb IsWindows()
jnz 9f jnz 9f
testb IsMetal()
jnz 9f
push %rdi push %rdi
push %rsi push %rsi
// allocate stack // allocate stack
@ -120,8 +122,9 @@ cosmo: push %rbp
leave leave
pop %rcx pop %rcx
lea (%rax,%r8),%rsp lea (%rax,%r8),%rsp
sub $ape_stack_align,%rsp # openbsd:stackbound mov $ape_stack_align,%eax # openbsd:stackbound
mov %rbp,(%rsp) neg %rax
and %rax,%rsp
push %rcx push %rcx
push %rbp push %rbp
mov %rsp,%rbp mov %rsp,%rbp

View file

@ -27,6 +27,7 @@
#include "libc/intrin/kprintf.h" #include "libc/intrin/kprintf.h"
#include "libc/log/libfatal.internal.h" #include "libc/log/libfatal.internal.h"
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
#include "libc/nexgen32e/msr.h"
#include "libc/nexgen32e/threaded.h" #include "libc/nexgen32e/threaded.h"
#include "libc/nt/thread.h" #include "libc/nt/thread.h"
#include "libc/runtime/internal.h" #include "libc/runtime/internal.h"
@ -158,11 +159,17 @@ privileged void __enable_tls(void) {
: "=a"(ax) : "=a"(ax)
: "0"(__NR___set_tcb), "D"(tib) : "0"(__NR___set_tcb), "D"(tib)
: "rcx", "r11", "memory", "cc"); : "rcx", "r11", "memory", "cc");
} else { } else if (IsLinux()) {
asm volatile("syscall" asm volatile("syscall"
: "=a"(ax) : "=a"(ax)
: "0"(__NR_linux_arch_prctl), "D"(ARCH_SET_FS), "S"(tib) : "0"(__NR_linux_arch_prctl), "D"(ARCH_SET_FS), "S"(tib)
: "rcx", "r11", "memory"); : "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 // We need to rewrite SysV _Thread_local code. You MUST use the