Add 'volatile' to cpuid inline asm (#994)

This commit is contained in:
Thom Chiovoloni 2021-02-05 17:57:55 -08:00 committed by GitHub
parent ff16186fcc
commit 59dfb05bd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 13 deletions

View file

@ -4,6 +4,7 @@
#![allow(unused_features)]
#![allow(incomplete_features)]
#![feature(
asm,
const_fn,
const_fn_union,
const_fn_transmute,

View file

@ -55,26 +55,33 @@ pub unsafe fn __cpuid_count(leaf: u32, sub_leaf: u32) -> CpuidResult {
let ebx;
let ecx;
let edx;
#[cfg(target_arch = "x86")]
{
llvm_asm!("cpuid"
: "={eax}"(eax), "={ebx}"(ebx), "={ecx}"(ecx), "={edx}"(edx)
: "{eax}"(leaf), "{ecx}"(sub_leaf)
: :);
asm!(
"cpuid",
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.
// 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
llvm_asm!(r#"
mov %rbx, %rsi
cpuid
xchg %rbx, %rsi
"#
: "={eax}"(eax), "={esi}"(ebx), "={ecx}"(ecx), "={edx}"(edx)
: "{eax}"(leaf), "{ecx}"(sub_leaf)
: :);
asm!(
"mov rsi, rbx",
"cpuid",
"xchg rsi, rbx",
inlateout("eax") leaf => eax,
lateout("esi") ebx,
inlateout("ecx") sub_leaf => ecx,
lateout("edx") edx,
options(nostack, preserves_flags),
);
}
CpuidResult { eax, ebx, ecx, edx }
}