From b992e37559221faba58abef5e3662ef2a1b4b4ea Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sat, 9 Sep 2023 12:22:17 -0700 Subject: [PATCH] Add more comments and make the src/arch/* files more consistent. --- src/arch/aarch64.rs | 14 ++++++++++---- src/arch/arm.rs | 12 +++++++++--- src/arch/riscv64.rs | 14 ++++++++++---- src/arch/x86.rs | 12 +++++++++--- src/lib.rs | 6 +++--- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/arch/aarch64.rs b/src/arch/aarch64.rs index c9f38a5..97d3b95 100644 --- a/src/arch/aarch64.rs +++ b/src/arch/aarch64.rs @@ -50,20 +50,26 @@ pub(super) unsafe fn clone( } /// Write a value to the platform thread-pointer register. -#[cfg(feature = "origin-thread")] +#[cfg(all( + feature = "origin-thread", + any(feature = "origin-start", feature = "external-start") +))] #[inline] pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) { - asm!("msr tpidr_el0,{0}", in(reg) ptr); + asm!("msr tpidr_el0,{}", in(reg) ptr); debug_assert_eq!(get_thread_pointer(), ptr); } /// Read the value of the platform thread-pointer register. -#[cfg(feature = "origin-thread")] +#[cfg(all( + feature = "origin-thread", + any(feature = "origin-start", feature = "external-start") +))] #[inline] pub(super) fn get_thread_pointer() -> *mut c_void { let ptr; unsafe { - asm!("mrs {0},tpidr_el0", out(reg) ptr, options(nostack, preserves_flags, readonly)); + asm!("mrs {},tpidr_el0", out(reg) ptr, options(nostack, preserves_flags, readonly)); } ptr } diff --git a/src/arch/arm.rs b/src/arch/arm.rs index e41963b..3f972e4 100644 --- a/src/arch/arm.rs +++ b/src/arch/arm.rs @@ -51,7 +51,10 @@ pub(super) unsafe fn clone( } /// Write a value to the platform thread-pointer register. -#[cfg(feature = "origin-thread")] +#[cfg(all( + feature = "origin-thread", + any(feature = "origin-start", feature = "external-start") +))] #[inline] pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) { rustix::runtime::arm_set_tls(ptr).expect("arm_set_tls"); @@ -59,12 +62,15 @@ pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) { } /// Read the value of the platform thread-pointer register. -#[cfg(feature = "origin-thread")] +#[cfg(all( + feature = "origin-thread", + any(feature = "origin-start", feature = "external-start") +))] #[inline] pub(super) fn get_thread_pointer() -> *mut c_void { let ptr; unsafe { - asm!("mrc p15,0,{0},c13,c0,3", out(reg) ptr); + asm!("mrc p15,0,{},c13,c0,3", out(reg) ptr); } ptr } diff --git a/src/arch/riscv64.rs b/src/arch/riscv64.rs index d1118f3..8dbbd0a 100644 --- a/src/arch/riscv64.rs +++ b/src/arch/riscv64.rs @@ -47,20 +47,26 @@ pub(super) unsafe fn clone( } /// Write a value to the platform thread-pointer register. -#[cfg(feature = "origin-thread")] +#[cfg(all( + feature = "origin-thread", + any(feature = "origin-start", feature = "external-start") +))] #[inline] pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) { - asm!("mv tp,{0}", in(reg) ptr); + asm!("mv tp,{}", in(reg) ptr); debug_assert_eq!(get_thread_pointer(), ptr); } /// Read the value of the platform thread-pointer register. -#[cfg(feature = "origin-thread")] +#[cfg(all( + feature = "origin-thread", + any(feature = "origin-start", feature = "external-start") +))] #[inline] pub(super) fn get_thread_pointer() -> *mut c_void { let ptr; unsafe { - asm!("mv {0},tp", out(reg) ptr, options(nostack, preserves_flags, readonly)); + asm!("mv {},tp", out(reg) ptr, options(nostack, preserves_flags, readonly)); } ptr } diff --git a/src/arch/x86.rs b/src/arch/x86.rs index 99acec8..03876bf 100644 --- a/src/arch/x86.rs +++ b/src/arch/x86.rs @@ -89,7 +89,10 @@ pub(super) unsafe fn clone( } /// Write a value to the platform thread-pointer register. -#[cfg(feature = "origin-thread")] +#[cfg(all( + feature = "origin-thread", + any(feature = "origin-start", feature = "external-start") +))] #[inline] pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) { let mut user_desc = rustix::runtime::UserDesc { @@ -113,12 +116,15 @@ pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) { } /// Read the value of the platform thread-pointer register. -#[cfg(feature = "origin-thread")] +#[cfg(all( + feature = "origin-thread", + any(feature = "origin-start", feature = "external-start") +))] #[inline] pub(super) fn get_thread_pointer() -> *mut c_void { let ptr; unsafe { - asm!("mov {0},DWORD PTR gs:0", out(reg) ptr, options(nostack, preserves_flags, readonly)); + asm!("mov {},DWORD PTR gs:0", out(reg) ptr, options(nostack, preserves_flags, readonly)); } ptr } diff --git a/src/lib.rs b/src/lib.rs index c465394..62f1b5a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,9 +100,9 @@ unsafe extern "C" fn _start() -> ! { #[cfg(target_arch = "x86")] asm!( "mov eax, esp", // Save the incoming `esp` value. - "push ebp", // Pad for alignment. - "push ebp", // Pad for alignment. - "push ebp", // Pad for alignment. + "push ebp", // Pad for stack pointer alignment. + "push ebp", // Pad for stack pointer alignment. + "push ebp", // Pad for stack pointer alignment. "push eax", // Pass saved the incoming `esp` as the arg to `entry`. "push ebp", // Set the return address to zero. "jmp {entry}", // Jump to `entry`.