cpuid is safe since the stdarch sync, so remove unsafe from usages

This commit is contained in:
Manuel Drehwald 2025-12-26 08:46:49 +01:00
parent 6adebf5685
commit 6f06d386b8
7 changed files with 32 additions and 33 deletions

View file

@ -57,7 +57,7 @@ fn load_x86_features() -> Flags {
// (in that order)
let mut vendor_id = [0u8; 12];
let max_basic_leaf;
unsafe {
{
let CpuidResult { eax, ebx, ecx, edx } = __cpuid(0);
max_basic_leaf = eax;
vendor_id[0..4].copy_from_slice(&ebx.to_ne_bytes());
@ -72,7 +72,7 @@ fn load_x86_features() -> Flags {
// EAX = 1, ECX = 0: Queries "Processor Info and Feature Bits";
// Contains information about most x86 features.
let CpuidResult { ecx, edx, .. } = unsafe { __cpuid(0x0000_0001_u32) };
let CpuidResult { ecx, edx, .. } = __cpuid(0x0000_0001_u32);
let proc_info_ecx = Flags::from_bits(ecx);
let proc_info_edx = Flags::from_bits(edx);
@ -82,23 +82,23 @@ fn load_x86_features() -> Flags {
let mut extended_features_edx = Flags::empty();
let mut extended_features_eax_leaf_1 = Flags::empty();
if max_basic_leaf >= 7 {
let CpuidResult { ebx, edx, .. } = unsafe { __cpuid(0x0000_0007_u32) };
let CpuidResult { ebx, edx, .. } = __cpuid(0x0000_0007_u32);
extended_features_ebx = Flags::from_bits(ebx);
extended_features_edx = Flags::from_bits(edx);
let CpuidResult { eax, .. } = unsafe { __cpuid_count(0x0000_0007_u32, 0x0000_0001_u32) };
let CpuidResult { eax, .. } = __cpuid_count(0x0000_0007_u32, 0x0000_0001_u32);
extended_features_eax_leaf_1 = Flags::from_bits(eax)
}
// EAX = 0x8000_0000, ECX = 0: Get Highest Extended Function Supported
// - EAX returns the max leaf value for extended information, that is,
// `cpuid` calls in range [0x8000_0000; u32::MAX]:
let extended_max_basic_leaf = unsafe { __cpuid(0x8000_0000_u32) }.eax;
let extended_max_basic_leaf = __cpuid(0x8000_0000_u32).eax;
// EAX = 0x8000_0001, ECX=0: Queries "Extended Processor Info and Feature Bits"
let mut extended_proc_info_ecx = Flags::empty();
if extended_max_basic_leaf >= 1 {
let CpuidResult { ecx, .. } = unsafe { __cpuid(0x8000_0001_u32) };
let CpuidResult { ecx, .. } = __cpuid(0x8000_0001_u32);
extended_proc_info_ecx = Flags::from_bits(ecx);
}

View file

@ -297,7 +297,7 @@ pub(crate) mod instant_internal {
// Inspired by [`edk2/UefiCpuPkg/Library/CpuTimerLib/CpuTimerLib.c`](https://github.com/tianocore/edk2/blob/master/UefiCpuPkg/Library/CpuTimerLib/CpuTimerLib.c)
let freq = FREQUENCY
.get_or_try_init(|| {
let cpuid = unsafe { crate::arch::x86_64::__cpuid(0x15) };
let cpuid = crate::arch::x86_64::__cpuid(0x15);
if cpuid.eax == 0 || cpuid.ebx == 0 || cpuid.ecx == 0 {
return Err(());
}
@ -316,7 +316,7 @@ pub(crate) mod instant_internal {
let freq = FREQUENCY
.get_or_try_init(|| {
let cpuid = unsafe { crate::arch::x86::__cpuid(0x15) };
let cpuid = crate::arch::x86::__cpuid(0x15);
if cpuid.eax == 0 || cpuid.ebx == 0 || cpuid.ecx == 0 {
return Err(());
}

View file

@ -107,11 +107,11 @@ mod rdrand {
{
// SAFETY: All Rust x86 targets are new enough to have CPUID, and we
// check that leaf 1 is supported before using it.
let cpuid0 = unsafe { arch::__cpuid(0) };
let cpuid0 = arch::__cpuid(0);
if cpuid0.eax < 1 {
return false;
}
let cpuid1 = unsafe { arch::__cpuid(1) };
let cpuid1 = arch::__cpuid(1);
let vendor_id =
[cpuid0.ebx.to_le_bytes(), cpuid0.edx.to_le_bytes(), cpuid0.ecx.to_le_bytes()];

View file

@ -40,10 +40,10 @@ pub(crate) fn detect_features() -> cache::Initializer {
// leaf value for subsequent calls of `cpuinfo` in range [0,
// 0x8000_0000]. - The vendor ID is stored in 12 u8 ascii chars,
// returned in EBX, EDX, and ECX (in that order):
let (max_basic_leaf, vendor_id) = unsafe {
let (max_basic_leaf, vendor_id) = {
let CpuidResult { eax: max_basic_leaf, ebx, ecx, edx } = __cpuid(0);
let vendor_id: [[u8; 4]; 3] = [ebx.to_ne_bytes(), edx.to_ne_bytes(), ecx.to_ne_bytes()];
let vendor_id: [u8; 12] = mem::transmute(vendor_id);
let vendor_id: [u8; 12] = unsafe { mem::transmute(vendor_id) };
(max_basic_leaf, vendor_id)
};
@ -54,8 +54,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
// EAX = 1, ECX = 0: Queries "Processor Info and Feature Bits";
// Contains information about most x86 features.
let CpuidResult { ecx: proc_info_ecx, edx: proc_info_edx, .. } =
unsafe { __cpuid(0x0000_0001_u32) };
let CpuidResult { ecx: proc_info_ecx, edx: proc_info_edx, .. } = __cpuid(0x0000_0001_u32);
// EAX = 7: Queries "Extended Features";
// Contains information about bmi,bmi2, and avx2 support.
@ -66,9 +65,9 @@ pub(crate) fn detect_features() -> cache::Initializer {
extended_features_eax_leaf_1,
extended_features_edx_leaf_1,
) = if max_basic_leaf >= 7 {
let CpuidResult { ebx, ecx, edx, .. } = unsafe { __cpuid(0x0000_0007_u32) };
let CpuidResult { ebx, ecx, edx, .. } = __cpuid(0x0000_0007_u32);
let CpuidResult { eax: eax_1, edx: edx_1, .. } =
unsafe { __cpuid_count(0x0000_0007_u32, 0x0000_0001_u32) };
__cpuid_count(0x0000_0007_u32, 0x0000_0001_u32);
(ebx, ecx, edx, eax_1, edx_1)
} else {
(0, 0, 0, 0, 0) // CPUID does not support "Extended Features"
@ -77,12 +76,12 @@ pub(crate) fn detect_features() -> cache::Initializer {
// EAX = 0x8000_0000, ECX = 0: Get Highest Extended Function Supported
// - EAX returns the max leaf value for extended information, that is,
// `cpuid` calls in range [0x8000_0000; u32::MAX]:
let CpuidResult { eax: extended_max_basic_leaf, .. } = unsafe { __cpuid(0x8000_0000_u32) };
let CpuidResult { eax: extended_max_basic_leaf, .. } = __cpuid(0x8000_0000_u32);
// EAX = 0x8000_0001, ECX=0: Queries "Extended Processor Info and Feature
// Bits"
let extended_proc_info_ecx = if extended_max_basic_leaf >= 1 {
let CpuidResult { ecx, .. } = unsafe { __cpuid(0x8000_0001_u32) };
let CpuidResult { ecx, .. } = __cpuid(0x8000_0001_u32);
ecx
} else {
0
@ -132,7 +131,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
// Detect if CPUID.19h available
if bit::test(extended_features_ecx as usize, 23) {
let CpuidResult { ebx, .. } = unsafe { __cpuid(0x19) };
let CpuidResult { ebx, .. } = __cpuid(0x19);
enable(ebx, 0, Feature::kl);
enable(ebx, 2, Feature::widekl);
}
@ -223,7 +222,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
// ECX = 1):
if max_basic_leaf >= 0xd {
let CpuidResult { eax: proc_extended_state1_eax, .. } =
unsafe { __cpuid_count(0xd_u32, 1) };
__cpuid_count(0xd_u32, 1);
enable(proc_extended_state1_eax, 0, Feature::xsaveopt);
enable(proc_extended_state1_eax, 1, Feature::xsavec);
enable(proc_extended_state1_eax, 3, Feature::xsaves);
@ -282,7 +281,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
if max_basic_leaf >= 0x1e {
let CpuidResult { eax: amx_feature_flags_eax, .. } =
unsafe { __cpuid_count(0x1e_u32, 1) };
__cpuid_count(0x1e_u32, 1);
enable(amx_feature_flags_eax, 4, Feature::amx_fp8);
enable(amx_feature_flags_eax, 6, Feature::amx_tf32);
@ -297,7 +296,7 @@ pub(crate) fn detect_features() -> cache::Initializer {
let avx10_1 = enable(extended_features_edx_leaf_1, 19, Feature::avx10_1);
if avx10_1 {
let CpuidResult { ebx, .. } = unsafe { __cpuid(0x24) };
let CpuidResult { ebx, .. } = __cpuid(0x24);
let avx10_version = ebx & 0xff;
if avx10_version >= 2 {
value.set(Feature::avx10_2 as u32);

View file

@ -151,7 +151,7 @@ mod tsc {
// implemented like https://docs.rs/raw-cpuid/latest/src/raw_cpuid/extended.rs.html#965-967
const LEAF: u32 = 0x80000007; // this is the leaf for "advanced power management info"
let cpuid = unsafe { __cpuid(LEAF) };
let cpuid = __cpuid(LEAF);
(cpuid.edx & (1 << 8)) != 0 // EDX bit 8 indicates invariant TSC
}

View file

@ -66,13 +66,13 @@ fn detect_features() -> cache::Initializer {
ecx: proc_info_ecx,
edx: proc_info_edx,
..
} = unsafe { __cpuid(0x0000_0001_u32) };
} = __cpuid(0x0000_0001_u32);
// EAX = 7, ECX = 0: Queries "Extended Features";
// Contains information about bmi,bmi2, and avx2 support.
let (extended_features_ebx, extended_features_ecx) = if max_basic_leaf >= 7
{
let CpuidResult { ebx, ecx, .. } = unsafe { __cpuid(0x0000_0007_u32) };
let CpuidResult { ebx, ecx, .. } = __cpuid(0x0000_0007_u32);
(ebx, ecx)
} else {
(0, 0) // CPUID does not support "Extended Features"
@ -84,12 +84,12 @@ fn detect_features() -> cache::Initializer {
let CpuidResult {
eax: extended_max_basic_leaf,
..
} = unsafe { __cpuid(0x8000_0000_u32) };
} = __cpuid(0x8000_0000_u32);
// EAX = 0x8000_0001, ECX=0: Queries "Extended Processor Info and Feature
// Bits"
let extended_proc_info_ecx = if extended_max_basic_leaf >= 1 {
let CpuidResult { ecx, .. } = unsafe { __cpuid(0x8000_0001_u32) };
let CpuidResult { ecx, .. } = __cpuid(0x8000_0001_u32);
ecx
} else {
0
@ -182,7 +182,7 @@ fn detect_features() -> cache::Initializer {
let CpuidResult {
eax: proc_extended_state1_eax,
..
} = unsafe { __cpuid_count(0xd_u32, 1) };
} = __cpuid_count(0xd_u32, 1);
enable(proc_extended_state1_eax, 0, Feature::xsaveopt);
enable(proc_extended_state1_eax, 1, Feature::xsavec);
enable(proc_extended_state1_eax, 3, Feature::xsaves);

View file

@ -66,12 +66,12 @@ fn detect_features() -> cache::Initializer {
ecx: proc_info_ecx,
edx: proc_info_edx,
..
} = unsafe { __cpuid(0x0000_0001_u32) };
} = __cpuid(0x0000_0001_u32);
// EAX = 7, ECX = 0: Queries "Extended Features";
// Contains information about bmi,bmi2, and avx2 support.
let (extended_features_ebx, extended_features_ecx) = if max_basic_leaf >= 7 {
let CpuidResult { ebx, ecx, .. } = unsafe { __cpuid(0x0000_0007_u32) };
let CpuidResult { ebx, ecx, .. } = __cpuid(0x0000_0007_u32);
(ebx, ecx)
} else {
(0, 0) // CPUID does not support "Extended Features"
@ -83,12 +83,12 @@ fn detect_features() -> cache::Initializer {
let CpuidResult {
eax: extended_max_basic_leaf,
..
} = unsafe { __cpuid(0x8000_0000_u32) };
} = __cpuid(0x8000_0000_u32);
// EAX = 0x8000_0001, ECX=0: Queries "Extended Processor Info and Feature
// Bits"
let extended_proc_info_ecx = if extended_max_basic_leaf >= 1 {
let CpuidResult { ecx, .. } = unsafe { __cpuid(0x8000_0001_u32) };
let CpuidResult { ecx, .. } = __cpuid(0x8000_0001_u32);
ecx
} else {
0
@ -181,7 +181,7 @@ fn detect_features() -> cache::Initializer {
let CpuidResult {
eax: proc_extended_state1_eax,
..
} = unsafe { __cpuid_count(0xd_u32, 1) };
} = __cpuid_count(0xd_u32, 1);
enable(proc_extended_state1_eax, 0, Feature::xsaveopt);
enable(proc_extended_state1_eax, 1, Feature::xsavec);
enable(proc_extended_state1_eax, 3, Feature::xsaves);