Skip to content

Commit

Permalink
Avoid using ebx as an asm! operand (rust-lang#1121)
Browse files Browse the repository at this point in the history
It is sometimes reserved by LLVM.
  • Loading branch information
Amanieu committed Apr 16, 2021
1 parent bc1ce17 commit 5064ce0
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions crates/core_arch/src/x86/cpuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,29 @@ pub unsafe fn __cpuid_count(leaf: u32, sub_leaf: u32) -> CpuidResult {
let ecx;
let edx;

// LLVM sometimes reserves `ebx` for its internal use, we so we need to use
// a scratch register for it instead.
#[cfg(target_arch = "x86")]
{
asm!(
"mov {0}, ebx",
"cpuid",
"xchg {0}, ebx",
lateout(reg) ebx,
inlateout("eax") leaf => eax,
lateout("ebx") ebx,
inlateout("ecx") sub_leaf => ecx,
lateout("edx") edx,
options(nostack, preserves_flags),
);
}
#[cfg(target_arch = "x86_64")]
{
// x86-64 uses `rbx` as the base register, so preserve it.
// This works around a bug in LLVM with ASAN enabled:
// https://bugs.llvm.org/show_bug.cgi?id=17907
asm!(
"mov rsi, rbx",
"mov {0:r}, rbx",
"cpuid",
"xchg rsi, rbx",
"xchg {0:r}, rbx",
lateout(reg) ebx,
inlateout("eax") leaf => eax,
lateout("esi") ebx,
inlateout("ecx") sub_leaf => ecx,
lateout("edx") edx,
options(nostack, preserves_flags),
Expand Down

0 comments on commit 5064ce0

Please sign in to comment.