Skip to main content

vm_topology/
processor.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Processor topology types.
5
6pub mod aarch64;
7pub mod x86;
8
9cfg_if::cfg_if! {
10    if #[cfg(guest_arch = "aarch64")] {
11        pub use aarch64 as arch;
12        pub use aarch64::Aarch64Topology as TargetTopology;
13        pub use aarch64::Aarch64VpInfo as TargetVpInfo;
14    } else if #[cfg(guest_arch = "x86_64")] {
15        pub use x86 as arch;
16        pub use x86::X86Topology as TargetTopology;
17        pub use x86::X86VpInfo as TargetVpInfo;
18    } else {
19        compile_error!("Unsupported architecture");
20    }
21}
22use thiserror::Error;
23
24/// A description of the VM's processor topology.
25///
26/// Currently this just tracks the APIC IDs for the processors.
27///
28/// Build one with [`TopologyBuilder`].
29#[cfg_attr(
30    feature = "inspect",
31    derive(inspect::Inspect),
32    inspect(bound = "T: inspect::Inspect, T::ArchVpInfo: inspect::Inspect")
33)]
34#[derive(Debug, Clone)]
35pub struct ProcessorTopology<T: ArchTopology = TargetTopology> {
36    #[cfg_attr(feature = "inspect", inspect(iter_by_index))]
37    vps: Vec<T::ArchVpInfo>,
38    smt_enabled: bool,
39    vps_per_socket: u32,
40    arch: T,
41}
42
43/// Architecture-specific topology types.
44pub trait ArchTopology: Sized {
45    /// The architecture-specific VP info type.
46    type ArchVpInfo: Copy + AsRef<VpInfo>;
47
48    /// The architecture-specific [`TopologyBuilder`] generic.
49    type BuilderState;
50
51    /// Compute VP topology from a VP.
52    fn vp_topology(topology: &ProcessorTopology<Self>, info: &Self::ArchVpInfo) -> VpTopologyInfo;
53}
54
55/// A builder for [`ProcessorTopology`].
56#[derive(Debug)]
57pub struct TopologyBuilder<T: ArchTopology> {
58    vps_per_socket: u32,
59    smt_enabled: bool,
60    arch: T::BuilderState,
61}
62
63/// Error returned by [`TopologyBuilder::from_host_topology`].
64#[derive(Debug, Error)]
65pub enum HostTopologyError {
66    /// Could not find the host topology.
67    #[error("could not compute host topology via cpuid")]
68    NotFound,
69    /// The host topology has more than 2 threads per core.
70    #[error("unsupported thread-per-core count {0}")]
71    UnsupportedThreadsPerCore(u32),
72}
73
74/// Error when building a [`ProcessorTopology`].
75#[derive(Debug, Error)]
76pub enum InvalidTopology {
77    /// Failed to configure at least one VP.
78    #[error("must have at least one processor")]
79    NoVps,
80    /// Too many virtual processors.
81    #[error("too many processors requested: {requested}, max {max}")]
82    TooManyVps {
83        /// The number of processors requested.
84        requested: u32,
85        /// The maximum number of processors.
86        max: u32,
87    },
88    /// Not all processors will be addressable in XAPIC mode.
89    #[error("too many processors or too high an APIC ID {0} for xapic mode")]
90    ApicIdLimitExceeded(u32),
91    /// VpInfo indices must be linear and start at 0
92    #[error("vp indices don't start at 0 or don't count up")]
93    InvalidVpIndices,
94    /// A PPI INTID is not in the valid range (16..32).
95    #[error("PPI INTID {0} is not in the valid range 16..32")]
96    InvalidPpiIntid(u32),
97    /// The GIC interrupt count is invalid.
98    #[error("gic_nr_irqs {0} must be 64..=992 and a multiple of 32")]
99    InvalidGicNrIrqs(u32),
100    /// GICv2 supports at most 8 CPUs.
101    #[error("GICv2 supports at most 8 CPUs, but {0} were requested")]
102    TooManyCpusForGicV2(u32),
103    /// Failed to query the topology information from Device Tree.
104    #[error("failed to query memory topology from device tree")]
105    StdIoError(#[source] std::io::Error),
106}
107
108impl<T: ArchTopology> TopologyBuilder<T> {
109    /// Sets the number of VPs per socket.
110    ///
111    /// This does not need to be a power of 2, but it should be a multiple of 2
112    /// if SMT is enabled.
113    ///
114    /// The number of VPs per socket will be rounded up to a power of 2 for
115    /// purposes of defining the x2APIC ID.
116    pub fn vps_per_socket(&mut self, count: u32) -> &mut Self {
117        self.vps_per_socket = count.clamp(1, 32768);
118        self
119    }
120
121    /// Sets whether SMT (hyperthreading) is enabled.
122    ///
123    /// This is ignored if `vps_per_socket` is 1.
124    pub fn smt_enabled(&mut self, enabled: bool) -> &mut Self {
125        self.smt_enabled = enabled;
126        self
127    }
128}
129
130impl<
131    #[cfg(feature = "inspect")] T: ArchTopology + inspect::Inspect,
132    #[cfg(not(feature = "inspect"))] T: ArchTopology,
133> ProcessorTopology<T>
134{
135    /// Returns the number of VPs.
136    pub fn vp_count(&self) -> u32 {
137        self.vps.len() as u32
138    }
139
140    /// Returns information for the given processor by VP index.
141    ///
142    /// Panics if the VP index is out of range.
143    pub fn vp(&self, vp_index: VpIndex) -> VpInfo {
144        *self.vps[vp_index.index() as usize].as_ref()
145    }
146
147    /// Returns information for the given processor by VP index, including
148    /// architecture-specific information.
149    ///
150    /// Panics if the VP index is out of range.
151    pub fn vp_arch(&self, vp_index: VpIndex) -> T::ArchVpInfo {
152        self.vps[vp_index.index() as usize]
153    }
154
155    /// Returns an iterator over all VPs.
156    pub fn vps(&self) -> impl '_ + ExactSizeIterator<Item = VpInfo> + Clone {
157        self.vps.iter().map(|vp| *vp.as_ref())
158    }
159
160    /// Returns an iterator over all VPs, including architecture-specific information.
161    pub fn vps_arch(&self) -> impl '_ + ExactSizeIterator<Item = T::ArchVpInfo> + Clone {
162        self.vps.iter().copied()
163    }
164
165    /// Returns whether SMT (hyperthreading) is enabled.
166    pub fn smt_enabled(&self) -> bool {
167        self.smt_enabled
168    }
169
170    /// Returns the number of VPs per socket.
171    ///
172    /// This will always be a power of 2. The number of VPs actually populated
173    /// in a socket may be smaller than this.
174    pub fn reserved_vps_per_socket(&self) -> u32 {
175        self.vps_per_socket.next_power_of_two()
176    }
177
178    /// Computes the processor topology information for a VP.
179    pub fn vp_topology(&self, vp_index: VpIndex) -> VpTopologyInfo {
180        T::vp_topology(self, &self.vp_arch(vp_index))
181    }
182}
183
184/// Per-processor topology information.
185#[cfg_attr(feature = "inspect", derive(inspect::Inspect))]
186#[derive(Debug, Copy, Clone)]
187pub struct VpInfo {
188    /// The VP index of the processor.
189    pub vp_index: VpIndex,
190    /// The virtual NUMA node of the processor.
191    pub vnode: u32,
192}
193
194impl AsRef<VpInfo> for VpInfo {
195    fn as_ref(&self) -> &VpInfo {
196        self
197    }
198}
199
200impl VpInfo {
201    /// Returns true if this is the BSP.
202    pub fn is_bsp(&self) -> bool {
203        self.vp_index.is_bsp()
204    }
205}
206
207/// Topology information about a virtual processor.
208pub struct VpTopologyInfo {
209    /// The socket index.
210    pub socket: u32,
211    /// The core index within the socket.
212    pub core: u32,
213    /// The thread index within the core.
214    pub thread: u32,
215}
216
217/// The virtual processor index.
218///
219/// This value is used inside the VMM to identify the processor. It is expected
220/// to be used as an index into processor arrays, so it starts at zero and has
221/// no gaps.
222///
223/// VP index zero is special in that it is always present and is always the BSP.
224///
225/// The same value is exposed to the guest operating system as the HV VP index,
226/// via the Microsoft hypervisor guest interface. This constrains the HV VP
227/// index to start at zero and have no gaps, which is not required by the
228/// hypervisor interface, but it matches the behavior of Hyper-V and is not a
229/// practical limitation.
230///
231/// This value is distinct from the APIC ID, although they are often the same
232/// for all processors in small VMs and some in large VMs. Be careful not to use
233/// them interchangeably.
234#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
235#[cfg_attr(feature = "inspect", derive(inspect::Inspect), inspect(transparent))]
236pub struct VpIndex(u32);
237
238impl VpIndex {
239    /// Returns `index` as a VP index.
240    pub const fn new(index: u32) -> Self {
241        Self(index)
242    }
243
244    /// VP index zero, corresponding to the boot processor (BSP).
245    ///
246    /// Note that this being a constant means that the BSP's HV VP index
247    /// observed by the guest will always be zero. This is consistent with
248    /// Hyper-V and is not a practical limitation.
249    ///
250    /// Note that the APIC ID of the BSP might not be zero.
251    pub const BSP: Self = Self::new(0);
252
253    /// Returns the VP index value.
254    pub fn index(&self) -> u32 {
255        self.0
256    }
257
258    /// Returns true if this is the index of the BSP (0).
259    pub fn is_bsp(&self) -> bool {
260        *self == Self::BSP
261    }
262}