vmm/cpu_config/x86_64/static_cpu_templates/
mod.rs

1// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use derive_more::Display;
5use serde::{Deserialize, Serialize};
6
7use crate::arch::x86_64::cpu_model::{
8    CASCADE_LAKE_FMS, CpuModel, ICE_LAKE_FMS, MILAN_FMS, SKYLAKE_FMS,
9};
10use crate::cpu_config::x86_64::cpuid::{VENDOR_ID_AMD, VENDOR_ID_INTEL};
11
12/// Module with C3 CPU template for x86_64
13pub mod c3;
14/// Module with T2 CPU template for x86_64
15pub mod t2;
16/// Module with T2A CPU template for x86_64
17pub mod t2a;
18/// Module with T2CL CPU template for x86_64
19pub mod t2cl;
20/// Module with T2S CPU template for x86_64
21pub mod t2s;
22
23/// Template types available for configuring the x86 CPU features that map
24/// to EC2 instances.
25#[derive(Debug, Default, Display, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
26pub enum StaticCpuTemplate {
27    /// C3 Template.
28    #[display("C3")]
29    C3,
30    /// T2 Template.
31    #[display("T2")]
32    T2,
33    /// T2S Template.
34    #[display("T2S")]
35    T2S,
36    /// No CPU template is used.
37    #[default]
38    #[display("None")]
39    None,
40    /// T2CL Template.
41    #[display("T2CL")]
42    T2CL,
43    /// T2A Template.
44    #[display("T2A")]
45    T2A,
46}
47
48impl StaticCpuTemplate {
49    /// Check if no template specified
50    pub fn is_none(&self) -> bool {
51        self == &StaticCpuTemplate::None
52    }
53
54    /// Return the supported vendor for the CPU template.
55    pub fn get_supported_vendor(&self) -> &'static [u8; 12] {
56        match self {
57            StaticCpuTemplate::C3 => VENDOR_ID_INTEL,
58            StaticCpuTemplate::T2 => VENDOR_ID_INTEL,
59            StaticCpuTemplate::T2S => VENDOR_ID_INTEL,
60            StaticCpuTemplate::T2CL => VENDOR_ID_INTEL,
61            StaticCpuTemplate::T2A => VENDOR_ID_AMD,
62            StaticCpuTemplate::None => unreachable!(), // Should be handled in advance
63        }
64    }
65
66    /// Return supported CPU models for the CPU template.
67    pub fn get_supported_cpu_models(&self) -> &'static [CpuModel] {
68        match self {
69            StaticCpuTemplate::C3 => &[SKYLAKE_FMS, CASCADE_LAKE_FMS, ICE_LAKE_FMS],
70            StaticCpuTemplate::T2 => &[SKYLAKE_FMS, CASCADE_LAKE_FMS, ICE_LAKE_FMS],
71            StaticCpuTemplate::T2S => &[SKYLAKE_FMS, CASCADE_LAKE_FMS],
72            StaticCpuTemplate::T2CL => &[CASCADE_LAKE_FMS, ICE_LAKE_FMS],
73            StaticCpuTemplate::T2A => &[MILAN_FMS],
74            StaticCpuTemplate::None => unreachable!(), // Should be handled in advance
75        }
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82    use crate::cpu_config::test_utils::get_json_template;
83
84    #[test]
85    fn verify_consistency_with_json_templates() {
86        let static_templates = [
87            (c3::c3(), "C3.json"),
88            (t2::t2(), "T2.json"),
89            (t2s::t2s(), "T2S.json"),
90            (t2cl::t2cl(), "T2CL.json"),
91            (t2a::t2a(), "T2A.json"),
92        ];
93
94        for (hardcoded_template, filename) in static_templates {
95            let json_template = get_json_template(filename);
96            assert_eq!(hardcoded_template, json_template);
97        }
98    }
99}