From a1d0b8fc3596a07b2d99f80aad4903556e81b432 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 20 Aug 2022 20:51:01 -0600 Subject: [PATCH] More x86 fixes --- src/crti/src/lib.rs | 22 ++++++++++++++++++++++ src/crtn/src/lib.rs | 18 ++++++++++++++++++ src/ld_so/tcb.rs | 22 ++++++++++++++++++---- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/crti/src/lib.rs b/src/crti/src/lib.rs index 8e50d1d0bf..f9c303d069 100644 --- a/src/crti/src/lib.rs +++ b/src/crti/src/lib.rs @@ -5,6 +5,27 @@ use core::arch::global_asm; +#[cfg(target_arch = "x86_64")] +global_asm!( + r#" + .section .init + .global _init + _init: + push ebp + mov ebp, esp + // Created a new stack frame and updated the stack pointer + // Body will be filled in by gcc and ended by crtn.o + + .section .fini + .global _fini + _fini: + push ebp + mov ebp, esp + // Created a new stack frame and updated the stack pointer + // Body will be filled in by gcc and ended by crtn.o +"# +); + // https://wiki.osdev.org/Creating_a_C_Library#crtbegin.o.2C_crtend.o.2C_crti.o.2C_and_crtn.o #[cfg(target_arch = "x86_64")] global_asm!( @@ -26,6 +47,7 @@ global_asm!( // Body will be filled in by gcc and ended by crtn.o "# ); + // https://git.musl-libc.org/cgit/musl/tree/crt/aarch64/crti.s #[cfg(target_arch = "aarch64")] global_asm!( diff --git a/src/crtn/src/lib.rs b/src/crtn/src/lib.rs index e2af095320..e27a5786b0 100644 --- a/src/crtn/src/lib.rs +++ b/src/crtn/src/lib.rs @@ -5,6 +5,23 @@ use core::arch::global_asm; +#[cfg(target_arch = "x86")] +global_asm!( + r#" + .section .init + // This happens after crti.o and gcc has inserted code + // Pop the stack frame + pop ebp + ret + + .section .fini + // This happens after crti.o and gcc has inserted code + // Pop the stack frame + pop ebp + ret +"# +); + // https://wiki.osdev.org/Creating_a_C_Library#crtbegin.o.2C_crtend.o.2C_crti.o.2C_and_crtn.o #[cfg(target_arch = "x86_64")] global_asm!( @@ -22,6 +39,7 @@ global_asm!( ret "# ); + // https://git.musl-libc.org/cgit/musl/tree/crt/aarch64/crtn.s #[cfg(target_arch = "aarch64")] global_asm!( diff --git a/src/ld_so/tcb.rs b/src/ld_so/tcb.rs index 3826322fa7..6dfb9b016c 100644 --- a/src/ld_so/tcb.rs +++ b/src/ld_so/tcb.rs @@ -195,11 +195,10 @@ impl Tcb { Ok(tls_tcb.split_at_mut(size)) } - /// Architecture specific code to read a usize from the TCB - x86_64 + /// Architecture specific code to read a usize from the TCB - aarch64 #[inline(always)] #[cfg(target_arch = "aarch64")] unsafe fn arch_read(offset: usize) -> usize { - // TODO: s/llvm_asm/asm/g let tp: usize; asm!( "mrs {}, tpidr_el0", @@ -209,9 +208,24 @@ impl Tcb { *((tp + offset) as *const usize) } + /// Architecture specific code to read a usize from the TCB - x86 + #[inline(always)] + #[cfg(any(target_arch = "x86")] + unsafe fn arch_read(offset: usize) -> usize { + let value; + asm!( + " + mov {}, gs:[{}] + ", + out(reg) value, + in(reg) offset, + ); + value + } + /// Architecture specific code to read a usize from the TCB - x86_64 #[inline(always)] - #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + #[cfg(any(target_arch = "x86_64")] unsafe fn arch_read(offset: usize) -> usize { let value; asm!( @@ -248,7 +262,7 @@ impl Tcb { let _ = syscall::read(file, &mut env) .expect_notls("failed to read fsbase"); - env.fsbase = tp as u32; + env.gsbase = tp as u32; let _ = syscall::write(file, &env) .expect_notls("failed to write fsbase");