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 configured number of VPs per socket.
171    ///
172    /// This is the logical socket size before power-of-two rounding. Use
173    /// this for NUMA VP assignment and other logical topology queries.
174    pub fn vps_per_socket(&self) -> u32 {
175        self.vps_per_socket
176    }
177
178    /// Returns the number of VPs per socket, rounded up to a power of 2.
179    ///
180    /// This is the APIC-ID-space reservation per socket. The number of VPs
181    /// actually populated in a socket may be smaller than this.
182    pub fn reserved_vps_per_socket(&self) -> u32 {
183        self.vps_per_socket.next_power_of_two()
184    }
185
186    /// Computes the processor topology information for a VP.
187    pub fn vp_topology(&self, vp_index: VpIndex) -> VpTopologyInfo {
188        T::vp_topology(self, &self.vp_arch(vp_index))
189    }
190
191    /// Sets the virtual NUMA node for each VP.
192    ///
193    /// `vnodes` must have exactly `vp_count()` entries, where `vnodes[i]` is
194    /// the vnode for VP index `i`.
195    ///
196    /// # Panics
197    ///
198    /// Panics if `vnodes.len() != vp_count()`.
199    pub fn set_vnodes(&mut self, vnodes: &[u32])
200    where
201        T::ArchVpInfo: AsMut<VpInfo>,
202    {
203        assert_eq!(vnodes.len(), self.vps.len());
204        for (vp, &vnode) in self.vps.iter_mut().zip(vnodes) {
205            vp.as_mut().vnode = vnode;
206        }
207    }
208}
209
210/// Per-processor topology information.
211#[cfg_attr(feature = "inspect", derive(inspect::Inspect))]
212#[derive(Debug, Copy, Clone)]
213pub struct VpInfo {
214    /// The VP index of the processor.
215    pub vp_index: VpIndex,
216    /// The virtual NUMA node of the processor.
217    pub vnode: u32,
218}
219
220impl AsRef<VpInfo> for VpInfo {
221    fn as_ref(&self) -> &VpInfo {
222        self
223    }
224}
225
226impl VpInfo {
227    /// Returns true if this is the BSP.
228    pub fn is_bsp(&self) -> bool {
229        self.vp_index.is_bsp()
230    }
231}
232
233/// Topology information about a virtual processor.
234pub struct VpTopologyInfo {
235    /// The socket index.
236    pub socket: u32,
237    /// The core index within the socket.
238    pub core: u32,
239    /// The thread index within the core.
240    pub thread: u32,
241}
242
243/// The virtual processor index.
244///
245/// This value is used inside the VMM to identify the processor. It is expected
246/// to be used as an index into processor arrays, so it starts at zero and has
247/// no gaps.
248///
249/// VP index zero is special in that it is always present and is always the BSP.
250///
251/// The same value is exposed to the guest operating system as the HV VP index,
252/// via the Microsoft hypervisor guest interface. This constrains the HV VP
253/// index to start at zero and have no gaps, which is not required by the
254/// hypervisor interface, but it matches the behavior of Hyper-V and is not a
255/// practical limitation.
256///
257/// This value is distinct from the APIC ID, although they are often the same
258/// for all processors in small VMs and some in large VMs. Be careful not to use
259/// them interchangeably.
260#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
261#[cfg_attr(feature = "inspect", derive(inspect::Inspect), inspect(transparent))]
262pub struct VpIndex(u32);
263
264impl VpIndex {
265    /// Returns `index` as a VP index.
266    pub const fn new(index: u32) -> Self {
267        Self(index)
268    }
269
270    /// VP index zero, corresponding to the boot processor (BSP).
271    ///
272    /// Note that this being a constant means that the BSP's HV VP index
273    /// observed by the guest will always be zero. This is consistent with
274    /// Hyper-V and is not a practical limitation.
275    ///
276    /// Note that the APIC ID of the BSP might not be zero.
277    pub const BSP: Self = Self::new(0);
278
279    /// Returns the VP index value.
280    pub fn index(&self) -> u32 {
281        self.0
282    }
283
284    /// Returns true if this is the index of the BSP (0).
285    pub fn is_bsp(&self) -> bool {
286        *self == Self::BSP
287    }
288}