|
| 1 | +//! Miscelaneous x86 intrinsics available on all hosts. |
| 2 | +//! |
| 3 | +//! TODO: figure out a more discoverable name. |
| 4 | +
|
| 5 | +#[cfg(test)] |
| 6 | +use stdsimd_test::assert_instr; |
| 7 | + |
| 8 | +/// Result of the `cpuid` instruction. |
| 9 | +pub struct CpuidResult { |
| 10 | + /// EAX register. |
| 11 | + pub eax: u32, |
| 12 | + /// EBX register. |
| 13 | + pub ebx: u32, |
| 14 | + /// ECX register. |
| 15 | + pub ecx: u32, |
| 16 | + /// EDX register. |
| 17 | + pub edx: u32, |
| 18 | +} |
| 19 | + |
| 20 | +/// `cpuid` instruction. |
| 21 | +/// |
| 22 | +/// The [CPUID Wikipedia page][wiki_cpuid] contains how to query which |
| 23 | +/// information using the `eax` and `ecx` registers, and the format in |
| 24 | +/// which this information is returned in `eax...edx`. |
| 25 | +/// |
| 26 | +/// The `has_cpuid()` intrinsics can be used to query whether the `cpuid` |
| 27 | +/// instruction is available. |
| 28 | +/// |
| 29 | +/// The definitive references are: |
| 30 | +/// - [Intel 64 and IA-32 Architectures Software Developer's Manual Volume 2: |
| 31 | +/// Instruction Set Reference, A-Z][intel64_ref]. |
| 32 | +/// - [AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and |
| 33 | +/// System Instructions][amd64_ref]. |
| 34 | +/// |
| 35 | +/// [wiki_cpuid]: https://en.wikipedia.org/wiki/CPUID |
| 36 | +/// [intel64_ref]: http://www.intel.de/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf |
| 37 | +/// [amd64_ref]: http://support.amd.com/TechDocs/24594.pdf |
| 38 | +#[inline(always)] |
| 39 | +#[cfg_attr(test, assert_instr(cpuid))] |
| 40 | +pub unsafe fn cpuid(eax: u32, ecx: u32) -> CpuidResult { |
| 41 | + let mut r = ::std::mem::uninitialized::<CpuidResult>(); |
| 42 | + asm!("cpuid" |
| 43 | + : "={eax}"(r.eax), "={ebx}"(r.ebx), "={ecx}"(r.ecx), "={edx}"(r.edx) |
| 44 | + : "{eax}"(eax), "{ecx}"(ecx) |
| 45 | + : :); |
| 46 | + r |
| 47 | +} |
| 48 | + |
| 49 | +/// Reads EFLAGS. |
| 50 | +#[cfg(target_arch = "x86")] |
| 51 | +#[inline(always)] |
| 52 | +pub unsafe fn pushfd() -> u32 { |
| 53 | + let eflags: u32; |
| 54 | + asm!("pushfd; popl $0" : "=r"(eflags) : : : "volatile"); |
| 55 | + eflags |
| 56 | +} |
| 57 | + |
| 58 | +/// Write EFLAGS. |
| 59 | +#[cfg(target_arch = "x86")] |
| 60 | +#[inline(always)] |
| 61 | +pub unsafe fn popfd(eflags: u32) { |
| 62 | + asm!("pushl $0; popfd" : : "r"(eflags) : "cc", "flags" : "volatile"); |
| 63 | +} |
| 64 | + |
| 65 | +/// Does the host support the `cpuid` instruction? |
| 66 | +#[inline(always)] |
| 67 | +pub fn has_cpuid() -> bool { |
| 68 | + #[cfg(target_arch = "x86_64")] |
| 69 | + { |
| 70 | + true |
| 71 | + } |
| 72 | + #[cfg(target_arch = "x86")] |
| 73 | + { |
| 74 | + // On `x86` the `cpuid` instruction is not always available. |
| 75 | + // This follows the approach indicated in: |
| 76 | + // http://wiki.osdev.org/CPUID#Checking_CPUID_availability |
| 77 | + unsafe { |
| 78 | + // Read EFLAGS: |
| 79 | + let eflags: u32 = pushfd(); |
| 80 | + |
| 81 | + // Invert the ID bit in EFLAGS: |
| 82 | + let eflags_mod: u32 = eflags | 0x0020_0000; |
| 83 | + |
| 84 | + // Store the modified EFLAGS (ID bit may or may not be inverted) |
| 85 | + popfd(eflags_mod); |
| 86 | + |
| 87 | + // Read EFLAGS again: |
| 88 | + let eflags_after: u32 = pushfd(); |
| 89 | + |
| 90 | + // Check if the ID bit changed: |
| 91 | + eflags_after != eflags |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// Reads the contents of the extended control register `XCR` |
| 97 | +/// specified in `xcr_no`. |
| 98 | +#[inline(always)] |
| 99 | +// FIXME: see |
| 100 | +// https://github.com/rust-lang-nursery/stdsimd/issues/167 |
| 101 | +// #[target_feature = "+xsave"] |
| 102 | +#[cfg_attr(test, assert_instr(xgetbv))] |
| 103 | +pub unsafe fn xgetbv(xcr_no: u32) -> u64 { |
| 104 | + let eax: u32; |
| 105 | + let edx: u32; |
| 106 | + |
| 107 | + asm!("xgetbv" |
| 108 | + : "={eax}"(eax), "={edx}"(edx) |
| 109 | + : "{ecx}"(xcr_no) |
| 110 | + : :); |
| 111 | + |
| 112 | + ((edx as u64) << 32) | (eax as u64) |
| 113 | +} |
| 114 | + |
| 115 | +#[cfg(test)] |
| 116 | +mod tests { |
| 117 | + use super::*; |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_always_has_cpuid() { |
| 121 | + // all currently-tested targets have the instruction |
| 122 | + // FIXME: add targets without `cpuid` to CI |
| 123 | + assert!(has_cpuid()); |
| 124 | + } |
| 125 | + |
| 126 | + #[cfg(target_arch = "x86")] |
| 127 | + #[test] |
| 128 | + fn test_has_cpuid() { |
| 129 | + unsafe { |
| 130 | + let before = pushfd(); |
| 131 | + |
| 132 | + if has_cpuid() { |
| 133 | + assert!(before != pushfd()); |
| 134 | + } else { |
| 135 | + assert!(before == pushfd()); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + #[cfg(target_arch = "x86")] |
| 141 | + #[test] |
| 142 | + fn test_eflags() { |
| 143 | + unsafe { |
| 144 | + // reads eflags, writes them back, reads them again, |
| 145 | + // and compare for equality: |
| 146 | + let v = pushfd(); |
| 147 | + popfd(v); |
| 148 | + let u = pushfd(); |
| 149 | + assert_eq!(v, u); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments