| 1 | //! `cpuid` intrinsics |
| 2 | #![allow (clippy::module_name_repetitions)] |
| 3 | |
| 4 | use crate::arch::asm; |
| 5 | #[cfg (test)] |
| 6 | use stdarch_test::assert_instr; |
| 7 | |
| 8 | /// Result of the `cpuid` instruction. |
| 9 | #[allow (clippy::missing_inline_in_public_items)] |
| 10 | // ^^ the derived impl of Debug for CpuidResult is not #[inline] and that's OK. |
| 11 | #[derive (Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] |
| 12 | #[stable (feature = "simd_x86" , since = "1.27.0" )] |
| 13 | pub struct CpuidResult { |
| 14 | /// EAX register. |
| 15 | #[stable (feature = "simd_x86" , since = "1.27.0" )] |
| 16 | pub eax: u32, |
| 17 | /// EBX register. |
| 18 | #[stable (feature = "simd_x86" , since = "1.27.0" )] |
| 19 | pub ebx: u32, |
| 20 | /// ECX register. |
| 21 | #[stable (feature = "simd_x86" , since = "1.27.0" )] |
| 22 | pub ecx: u32, |
| 23 | /// EDX register. |
| 24 | #[stable (feature = "simd_x86" , since = "1.27.0" )] |
| 25 | pub edx: u32, |
| 26 | } |
| 27 | |
| 28 | /// Returns the result of the `cpuid` instruction for a given `leaf` (`EAX`) |
| 29 | /// and `sub_leaf` (`ECX`). |
| 30 | /// |
| 31 | /// There are two types of information leaves - basic leaves (with `leaf < 0x8000000`) |
| 32 | /// and extended leaves (with `leaf >= 0x80000000`). The highest supported basic and |
| 33 | /// extended leaves can be obtained by calling CPUID with `0` and `0x80000000`, |
| 34 | /// respectively, and reading the value in the `EAX` register. If the leaf supports |
| 35 | /// more than one sub-leaf, then the procedure of obtaining the highest supported |
| 36 | /// sub-leaf, as well as the behavior if a invalid sub-leaf value is passed, depends |
| 37 | /// on the specific leaf. |
| 38 | /// |
| 39 | /// If the `leaf` value is higher than the maximum supported basic or extended leaf |
| 40 | /// for the processor, this returns the information for the highest supported basic |
| 41 | /// information leaf (with the passed `sub_leaf` value). If the `leaf` value is less |
| 42 | /// than or equal to the highest basic or extended leaf value, but the leaf is not |
| 43 | /// supported on the processor, all zeros are returned. |
| 44 | /// |
| 45 | /// The [CPUID Wikipedia page][wiki_cpuid] contains information on how to query which |
| 46 | /// information using the `EAX` and `ECX` registers, and the interpretation of |
| 47 | /// the results returned in `EAX`, `EBX`, `ECX`, and `EDX`. |
| 48 | /// |
| 49 | /// The references are: |
| 50 | /// - [Intel 64 and IA-32 Architectures Software Developer's Manual Volume 2: |
| 51 | /// Instruction Set Reference, A-Z][intel64_ref]. |
| 52 | /// - [AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and |
| 53 | /// System Instructions][amd64_ref]. |
| 54 | /// |
| 55 | /// [wiki_cpuid]: https://en.wikipedia.org/wiki/CPUID |
| 56 | /// [intel64_ref]: https://cdrdv2-public.intel.com/671110/325383-sdm-vol-2abcd.pdf |
| 57 | /// [amd64_ref]: https://docs.amd.com/v/u/en-US/24594_3.37 |
| 58 | #[inline ] |
| 59 | #[cfg_attr (test, assert_instr(cpuid))] |
| 60 | #[stable (feature = "simd_x86" , since = "1.27.0" )] |
| 61 | pub fn __cpuid_count(leaf: u32, sub_leaf: u32) -> CpuidResult { |
| 62 | if cfg!(target_env = "sgx" ) { |
| 63 | panic!("`__cpuid` cannot be used in SGX" ); |
| 64 | } |
| 65 | |
| 66 | let eax; |
| 67 | let ebx; |
| 68 | let ecx; |
| 69 | let edx; |
| 70 | |
| 71 | // LLVM sometimes reserves `ebx` for its internal use, we so we need to use |
| 72 | // a scratch register for it instead. |
| 73 | #[cfg (target_arch = "x86" )] |
| 74 | unsafe { |
| 75 | asm!( |
| 76 | "mov {0}, ebx" , |
| 77 | "cpuid" , |
| 78 | "xchg {0}, ebx" , |
| 79 | out(reg) ebx, |
| 80 | inout("eax" ) leaf => eax, |
| 81 | inout("ecx" ) sub_leaf => ecx, |
| 82 | out("edx" ) edx, |
| 83 | options(nostack, preserves_flags), |
| 84 | ); |
| 85 | } |
| 86 | #[cfg (target_arch = "x86_64" )] |
| 87 | unsafe { |
| 88 | asm!( |
| 89 | "mov { 0:r}, rbx" , |
| 90 | "cpuid" , |
| 91 | "xchg { 0:r}, rbx" , |
| 92 | out(reg) ebx, |
| 93 | inout("eax" ) leaf => eax, |
| 94 | inout("ecx" ) sub_leaf => ecx, |
| 95 | out("edx" ) edx, |
| 96 | options(nostack, preserves_flags), |
| 97 | ); |
| 98 | } |
| 99 | CpuidResult { eax, ebx, ecx, edx } |
| 100 | } |
| 101 | |
| 102 | /// Calls CPUID with the provided `leaf` value, with `sub_leaf` set to 0. |
| 103 | /// See [`__cpuid_count`](fn.__cpuid_count.html). |
| 104 | #[inline ] |
| 105 | #[cfg_attr (test, assert_instr(cpuid))] |
| 106 | #[stable (feature = "simd_x86" , since = "1.27.0" )] |
| 107 | pub fn __cpuid(leaf: u32) -> CpuidResult { |
| 108 | __cpuid_count(leaf, sub_leaf:0) |
| 109 | } |
| 110 | |
| 111 | /// Returns the EAX and EBX register after calling CPUID with the provided `leaf`, |
| 112 | /// with `sub_leaf` set to 0. |
| 113 | /// |
| 114 | /// If `leaf` if 0 or `0x80000000`, the first tuple argument contains the maximum |
| 115 | /// supported basic or extended leaf, respectively. |
| 116 | /// |
| 117 | /// See also [`__cpuid`](fn.__cpuid.html) and |
| 118 | /// [`__cpuid_count`](fn.__cpuid_count.html). |
| 119 | #[inline ] |
| 120 | #[stable (feature = "simd_x86" , since = "1.27.0" )] |
| 121 | pub fn __get_cpuid_max(leaf: u32) -> (u32, u32) { |
| 122 | let CpuidResult { eax: u32, ebx: u32, .. } = __cpuid(leaf); |
| 123 | (eax, ebx) |
| 124 | } |
| 125 | |