Skip to content

Commit 8b47ce4

Browse files
committed
refactor: Calc right-shift bits for socket domain
The number of bits that the x2APIC ID must be shifted to the right to address instances of the socket domain, which is next higher-scoped domain to the core domain, was hardcoded as 7. That means up to 128 vcpus support. Currently the max vcpu count is hardcoded as 32, so it is enough but we might want to increase it in the future. To avoid unexpected issues when increasing the max vcpu count, calculate the value based on the max vCPU count. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent cdcf35a commit 8b47ce4

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/vmm/src/cpu_config/x86_64/cpuid/normalize.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,6 @@ impl super::Cpuid {
282282
cpu_bits: u8,
283283
cpus_per_core: u8,
284284
) -> Result<(), ExtendedTopologyError> {
285-
/// The APIC ID shift in leaf 0xBh specifies the number of bits to shit the x2APIC ID to
286-
/// get a unique topology of the next level. This allows 128 logical
287-
/// processors/package.
288-
const LEAFBH_INDEX1_APICID: u32 = 7;
289-
290285
// The following commit changed the behavior of KVM_GET_SUPPORTED_CPUID to no longer
291286
// include CPUID.(EAX=0BH,ECX=1).
292287
// https://lore.kernel.org/all/[email protected]/
@@ -365,11 +360,19 @@ impl super::Cpuid {
365360
}
366361
// Core domain
367362
1 => {
368-
set_range(&mut subleaf.result.eax, 0..5, LEAFBH_INDEX1_APICID)
369-
.map_err(ExtendedTopologyError::ApicId)?;
370-
371363
// Configure such that the next higher-scoped domain (i.e. socket) include
372364
// all logical processors.
365+
//
366+
// The CPUID.(EAX=0BH,ECX=1).EAX[4:0] value should be greater than or equal
367+
// to the base 2 logarithm of the number of logical processors, rounded up
368+
// to the decimal point. Since the maximum number of vCPUs is hardcoded and
369+
// is known a factorial of 2, use ilog2() of it.
370+
set_range(
371+
&mut subleaf.result.eax,
372+
0..5,
373+
crate::vmm_config::machine_config::MAX_SUPPORTED_VCPUS.ilog2(),
374+
)
375+
.map_err(ExtendedTopologyError::ApicId)?;
373376
set_range(&mut subleaf.result.ebx, 0..16, u32::from(cpu_count))
374377
.map_err(ExtendedTopologyError::LogicalProcessors)?;
375378

0 commit comments

Comments
 (0)