ostd/arch/x86/cpu/cpuid.rs
1// SPDX-License-Identifier: MPL-2.0
2
3//! CPU information from the CPUID instruction.
4
5use core::arch::x86_64::CpuidResult;
6
7use spin::Once;
8
9static MAX_LEAF: Once<u32> = Once::new();
10static MAX_HYPERVISOR_LEAF: Once<u32> = Once::new();
11static MAX_EXTENDED_LEAF: Once<u32> = Once::new();
12
13#[repr(u32)]
14enum Leaf {
15 Base = 0x00,
16 Xstate = 0x0d,
17 Tsc = 0x15,
18
19 HypervisorBase = 0x40000000,
20 ExtBase = 0x80000000,
21}
22
23/// Executes the CPUID instruction for the given leaf and subleaf.
24///
25/// This method will return `None` if the leaf is not supported.
26pub fn cpuid(leaf: u32, subleaf: u32) -> Option<CpuidResult> {
27 fn raw_cpuid(leaf: u32, subleaf: u32) -> CpuidResult {
28 core::arch::x86_64::__cpuid_count(leaf, subleaf)
29 }
30
31 let max_leaf = if leaf < Leaf::HypervisorBase as u32 {
32 // Standard leaves (0x0000_0000 - 0x3FFF_FFFF)
33 *MAX_LEAF.call_once(|| raw_cpuid(Leaf::Base as u32, 0).eax)
34 } else if leaf < Leaf::ExtBase as u32 {
35 // Hypervisor leaves (0x4000_0000 - 0x7FFF_FFFF)
36 *MAX_HYPERVISOR_LEAF.call_once(|| raw_cpuid(Leaf::HypervisorBase as u32, 0).eax)
37 } else {
38 // Extended leaves (0x8000_0000 - 0xFFFF_FFFF)
39 *MAX_EXTENDED_LEAF.call_once(|| raw_cpuid(Leaf::ExtBase as u32, 0).eax)
40 };
41
42 if leaf > max_leaf {
43 None
44 } else {
45 Some(raw_cpuid(leaf, subleaf))
46 }
47}
48
49/// Queries the frequency in Hz of the Time Stamp Counter (TSC).
50///
51/// This is based on the information given by the CPUID instruction in the Time Stamp Counter and
52/// Nominal Core Crystal Clock Information Leaf.
53///
54/// Note that the CPUID leaf is currently only supported by new Intel CPUs. This method will return
55/// `None` if it is not supported.
56pub(in crate::arch) fn query_tsc_freq() -> Option<u64> {
57 let CpuidResult {
58 eax: denominator,
59 ebx: numerator,
60 ecx: crystal_freq,
61 ..
62 } = cpuid(Leaf::Tsc as u32, 0)?;
63
64 if denominator == 0 || numerator == 0 {
65 return None;
66 }
67
68 // If the nominal core crystal clock frequency is not enumerated, we can either obtain that
69 // information from a hardcoded table or rely on the processor base frequency. The Intel
70 // documentation recommends the first approach [1], but Linux uses the second approach because
71 // the first approach is difficult to implement correctly for all corner cases [2]. However,
72 // the second approach does not provide 100% accurate frequencies, so Linux must adjust them at
73 // runtime [2]. For now, we avoid these headaches by faithfully reporting that the TSC
74 // frequency is unavailable.
75 //
76 // [1]: Intel(R) 64 and IA-32 Architectures Software Developer’s Manual,
77 // Section 20.7.3, Determining the Processor Base Frequency
78 // [2]: https://github.com/torvalds/linux/commit/604dc9170f2435d27da5039a3efd757dceadc684
79 if crystal_freq == 0 {
80 return None;
81 }
82
83 Some((crystal_freq as u64) * (numerator as u64) / (denominator as u64))
84}
85
86/// Queries the supported XSTATE features, i.e., the supported bits of `XCR0` and `IA32_XSS`.
87pub(in crate::arch) fn query_xstate_max_features() -> Option<u64> {
88 let res0 = cpuid(Leaf::Xstate as u32, 0)?;
89 let res1 = cpuid(Leaf::Xstate as u32, 1)?;
90
91 // Supported bits in `XCR0`.
92 let xcr_bits = (res0.eax as u64) | ((res0.edx as u64) << 32);
93 // Supported bits in `IA32_XSS`.
94 let xss_bits = (res1.ecx as u64) | ((res1.edx as u64) << 32);
95
96 Some(xcr_bits | xss_bits)
97}
98
99/// Queries the size in bytes of the XSAVE area containing states enabled by `XCRO` and `IA32_XSS`.
100pub(in crate::arch) fn query_xsave_area_size() -> Option<u32> {
101 cpuid(Leaf::Xstate as u32, 1).map(|res| res.ebx)
102}
103
104/// Queries if the system is running in QEMU.
105///
106/// This function uses the CPUID instruction to detect the QEMU hypervisor signature.
107pub(in crate::arch) fn query_is_running_in_qemu() -> bool {
108 let Some(result) = cpuid(Leaf::HypervisorBase as u32, 0) else {
109 return false;
110 };
111
112 let mut signature = [0u8; 12];
113 signature[0..4].copy_from_slice(&result.ebx.to_ne_bytes());
114 signature[4..8].copy_from_slice(&result.ecx.to_ne_bytes());
115 signature[8..12].copy_from_slice(&result.edx.to_ne_bytes());
116
117 // Check for the QEMU hypervisor signature: "TCGTCGTCGTCG" or "KVMKVMKVM\0\0\0".
118 // Reference: <https://wiki.osdev.org/QEMU_fw_cfg#Detecting_QEMU>
119 matches!(&signature, b"TCGTCGTCGTCG" | b"KVMKVMKVM\0\0\0")
120}