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

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