vmm/cpu_config/x86_64/cpuid/intel/
mod.rs

1// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3#![allow(
4    clippy::similar_names,
5    clippy::module_name_repetitions,
6    clippy::unreadable_literal,
7    clippy::unsafe_derive_deserialize
8)]
9
10/// CPUID normalize implementation.
11mod normalize;
12
13pub use normalize::{DeterministicCacheError, NormalizeCpuidError};
14
15use super::{CpuidEntry, CpuidKey, CpuidRegisters, CpuidTrait, KvmCpuidFlags};
16
17/// A structure matching the Intel CPUID specification as described in
18/// [IntelĀ® 64 and IA-32 Architectures Software Developer's Manual Combined Volumes 2A, 2B, 2C, and 2D: Instruction Set Reference, A-Z](https://cdrdv2.intel.com/v1/dl/getContent/671110)
19/// .
20#[derive(Debug, Clone, Eq, PartialEq)]
21pub struct IntelCpuid(pub std::collections::BTreeMap<CpuidKey, CpuidEntry>);
22
23impl CpuidTrait for IntelCpuid {
24    /// Gets a given sub-leaf.
25    #[inline]
26    fn get(&self, key: &CpuidKey) -> Option<&CpuidEntry> {
27        self.0.get(key)
28    }
29
30    /// Gets a given sub-leaf.
31    #[inline]
32    fn get_mut(&mut self, key: &CpuidKey) -> Option<&mut CpuidEntry> {
33        self.0.get_mut(key)
34    }
35}
36
37impl From<kvm_bindings::CpuId> for IntelCpuid {
38    #[inline]
39    fn from(kvm_cpuid: kvm_bindings::CpuId) -> Self {
40        let map = kvm_cpuid
41            .as_slice()
42            .iter()
43            .map(|entry| {
44                (
45                    CpuidKey {
46                        leaf: entry.function,
47                        subleaf: entry.index,
48                    },
49                    CpuidEntry {
50                        flags: KvmCpuidFlags(entry.flags),
51                        result: CpuidRegisters {
52                            eax: entry.eax,
53                            ebx: entry.ebx,
54                            ecx: entry.ecx,
55                            edx: entry.edx,
56                        },
57                    },
58                )
59            })
60            .collect();
61        Self(map)
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn get() {
71        let cpuid = IntelCpuid(std::collections::BTreeMap::new());
72        assert_eq!(
73            cpuid.get(&CpuidKey {
74                leaf: 0,
75                subleaf: 0
76            }),
77            None
78        );
79    }
80
81    #[test]
82    fn get_mut() {
83        let mut cpuid = IntelCpuid(std::collections::BTreeMap::new());
84        assert_eq!(
85            cpuid.get_mut(&CpuidKey {
86                leaf: 0,
87                subleaf: 0
88            }),
89            None
90        );
91    }
92}