diff --git a/src/cpuid/src/brand_string.rs b/src/cpuid/src/brand_string.rs index 26852628d..825a11fd4 100644 --- a/src/cpuid/src/brand_string.rs +++ b/src/cpuid/src/brand_string.rs @@ -102,9 +102,10 @@ impl BrandString { /// Creates a brand string, initialized from the CPUID leaves 0x80000002 through 0x80000004 /// of the host CPU. + #[allow(unused_unsafe)] pub fn from_host_cpuid() -> Result { let mut this = Self::new(); - let mut cpuid_regs = host_cpuid(0x8000_0000); + let mut cpuid_regs = unsafe { host_cpuid(0x8000_0000) }; if cpuid_regs.eax < 0x8000_0004 { // Brand string not supported by the host CPU @@ -112,7 +113,7 @@ impl BrandString { } for leaf in 0x8000_0002..=0x8000_0004 { - cpuid_regs = host_cpuid(leaf); + cpuid_regs = unsafe { host_cpuid(leaf) }; this.set_reg_for_leaf(leaf, Reg::EAX, cpuid_regs.eax); this.set_reg_for_leaf(leaf, Reg::EBX, cpuid_regs.ebx); this.set_reg_for_leaf(leaf, Reg::ECX, cpuid_regs.ecx); @@ -388,7 +389,7 @@ mod tests { match BrandString::from_host_cpuid() { Ok(bstr) => { for leaf in 0x8000_0002..=0x8000_0004_u32 { - let host_regs = host_cpuid(leaf); + let host_regs = unsafe { host_cpuid(leaf) }; assert_eq!(bstr.get_reg_for_leaf(leaf, Reg::EAX), host_regs.eax); assert_eq!(bstr.get_reg_for_leaf(leaf, Reg::EBX), host_regs.ebx); assert_eq!(bstr.get_reg_for_leaf(leaf, Reg::ECX), host_regs.ecx); @@ -398,7 +399,7 @@ mod tests { Err(Error::NotSupported) => { // from_host_cpuid() should only fail if the host CPU doesn't support // CPUID leaves up to 0x80000004, so let's make sure that's what happened. - let host_regs = host_cpuid(0x8000_0000); + let host_regs = unsafe { host_cpuid(0x8000_0000) }; assert!(host_regs.eax < 0x8000_0004); } _ => panic!("This function should not return another type of error"), diff --git a/src/cpuid/src/common.rs b/src/cpuid/src/common.rs index 80fab58fe..950a8673d 100644 --- a/src/cpuid/src/common.rs +++ b/src/cpuid/src/common.rs @@ -18,6 +18,7 @@ pub enum Error { } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +#[allow(unused_unsafe)] pub fn get_cpuid(function: u32, count: u32) -> Result { // TODO: replace with validation based on `has_cpuid()` when it becomes stable: // https://doc.rust-lang.org/core/arch/x86/fn.has_cpuid.html @@ -34,14 +35,16 @@ pub fn get_cpuid(function: u32, count: u32) -> Result { } } - let max_function = __get_cpuid_max(function & leaf_0x80000000::LEAF_NUM).0; + // this is safe because the host supports the `cpuid` instruction + let max_function = unsafe { __get_cpuid_max(function & leaf_0x80000000::LEAF_NUM).0 }; if function > max_function { return Err(Error::InvalidParameters(format!( "Function not supported: 0x{function:x}" ))); } - let entry = __cpuid_count(function, count); + // this is safe because the host supports the `cpuid` instruction + let entry = unsafe { __cpuid_count(function, count) }; if entry.eax == 0 && entry.ebx == 0 && entry.ecx == 0 && entry.edx == 0 { return Err(Error::InvalidParameters(format!("Invalid count: {count}"))); }