Skip to content

Commit f915b23

Browse files
committed
refactor(vmm): Better error messages for CPUID normalization
Show which combination of subleaf, register and bit(s) failed to set, remove unused error variant and reorder error variants in alphabetical order. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent 6220711 commit f915b23

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,35 @@ pub enum PassthroughCacheTopologyError {
4848
pub enum FeatureEntryError {
4949
/// Missing leaf 0x80000008.
5050
MissingLeaf0x80000008,
51-
/// Failed to set `nt` (number of physical threads) due to overflow.
52-
NumberOfPhysicalThreadsOverflow,
53-
/// Failed to set `nt` (number of physical threads).
51+
/// Failed to set number of physical threads (CPUID.80000008H:ECX[7:0]): {0}
5452
NumberOfPhysicalThreads(CheckedAssignError),
53+
/// Failed to set number of physical threads (CPUID.80000008H:ECX[7:0]) due to overflow.
54+
NumberOfPhysicalThreadsOverflow,
5555
}
5656

5757
/// Error type for setting leaf 0x8000001d section of [`super::AmdCpuid::normalize`].
5858
#[derive(Debug, thiserror::Error, displaydoc::Display, Eq, PartialEq)]
5959
pub enum ExtendedCacheTopologyError {
6060
/// Missing leaf 0x8000001d.
6161
MissingLeaf0x8000001d,
62-
/// Failed to set `num_sharing_cache` due to overflow.
63-
NumSharingCacheOverflow,
64-
/// Failed to set `num_sharing_cache`: {0}
65-
NumSharingCache(CheckedAssignError),
62+
#[rustfmt::skip]
63+
/// Failed to set number of logical processors sharing cache(CPUID.(EAX=8000001DH,ECX={0}):EAX[25:14]): {1}
64+
NumSharingCache(u32, CheckedAssignError),
65+
#[rustfmt::skip]
66+
/// Failed to set number of logical processors sharing cache (CPUID.(EAX=8000001DH,ECX={0}):EAX[25:14]) due to overflow.
67+
NumSharingCacheOverflow(u32),
6668
}
6769

6870
/// Error type for setting leaf 0x8000001e section of [`super::AmdCpuid::normalize`].
6971
#[derive(Debug, thiserror::Error, displaydoc::Display, Eq, PartialEq)]
7072
pub enum ExtendedApicIdError {
73+
/// Failed to set compute unit ID (CPUID.8000001EH:EBX[7:0]): {0}
74+
ComputeUnitId(CheckedAssignError),
75+
/// Failed to set extended APIC ID (CPUID.8000001EH:EAX[31:0]): {0}
76+
ExtendedApicId(CheckedAssignError),
7177
/// Missing leaf 0x8000001e.
7278
MissingLeaf0x8000001e,
73-
/// Failed to set `extended_apic_id`: {0}
74-
ExtendedApicId(CheckedAssignError),
75-
/// Failed to set `compute_unit_id`: {0}
76-
ComputeUnitId(CheckedAssignError),
77-
/// Failed to set `threads_per_compute_unit`: {0}
79+
/// Failed to set threads per core unit (CPUID:8000001EH:EBX[15:8]): {0}
7880
ThreadPerComputeUnit(CheckedAssignError),
7981
}
8082

@@ -282,16 +284,16 @@ impl super::AmdCpuid {
282284
// SAFETY: We know `cpus_per_core > 0` therefore this is always safe.
283285
let sub = u32::from(cpus_per_core.checked_sub(1).unwrap());
284286
set_range(&mut subleaf.result.eax, 14..26, sub)
285-
.map_err(ExtendedCacheTopologyError::NumSharingCache)?;
287+
.map_err(|err| ExtendedCacheTopologyError::NumSharingCache(i, err))?;
286288
}
287289
// L3 Cache
288290
// The L3 cache is shared among all the logical threads
289291
3 => {
290292
let sub = cpu_count
291293
.checked_sub(1)
292-
.ok_or(ExtendedCacheTopologyError::NumSharingCacheOverflow)?;
294+
.ok_or(ExtendedCacheTopologyError::NumSharingCacheOverflow(i))?;
293295
set_range(&mut subleaf.result.eax, 14..26, u32::from(sub))
294-
.map_err(ExtendedCacheTopologyError::NumSharingCache)?;
296+
.map_err(|err| ExtendedCacheTopologyError::NumSharingCache(i, err))?;
295297
}
296298
_ => (),
297299
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ pub enum NormalizeCpuidError {
3434
#[allow(clippy::enum_variant_names)]
3535
#[derive(Debug, thiserror::Error, displaydoc::Display, Eq, PartialEq)]
3636
pub enum DeterministicCacheError {
37-
/// Failed to set `Maximum number of addressable IDs for logical processors sharing this cache` due to underflow in cpu count.
38-
MaxCpusPerCoreUnderflow,
39-
/// Failed to set `Maximum number of addressable IDs for logical processors sharing this cache`: {0}.
40-
MaxCpusPerCore(CheckedAssignError),
41-
/// Failed to set `Maximum number of addressable IDs for processor cores in the physical package` due to underflow in cores.
42-
MaxCorePerPackageUnderflow,
43-
/// Failed to set `Maximum number of addressable IDs for processor cores in the physical package`: {0}.
37+
/// Failed to set max addressable core ID in physical package (CPUID.04H:EAX[31:26]): {0}.
4438
MaxCorePerPackage(CheckedAssignError),
39+
/// Failed to set max addressable core ID in physical package (CPUID.04H:EAX[31:26]) due to underflow in cores.
40+
MaxCorePerPackageUnderflow,
41+
/// Failed to set max addressable processor ID sharing cache (CPUID.04H:EAX[25:14]): {0}.
42+
MaxCpusPerCore(CheckedAssignError),
43+
/// Failed to set max addressable processor ID sharing cache (CPUID.04H:EAX[25:14]) due to underflow in cpu count.
44+
MaxCpusPerCoreUnderflow,
4545
}
4646

4747
/// We always use this brand string.

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,14 @@ pub enum GetMaxCpusPerPackageError {
6161
#[rustfmt::skip]
6262
#[derive(Debug, thiserror::Error, displaydoc::Display, Eq, PartialEq)]
6363
pub enum ExtendedTopologyError {
64-
/// Failed to set `Number of bits to shift right on x2APIC ID to get a unique topology ID of the next level type`: {0}
65-
ApicId(CheckedAssignError),
66-
/// Failed to set `Number of logical processors at this level type`: {0}
67-
LogicalProcessors(CheckedAssignError),
68-
/// Failed to set `Domain Type`: {0}
69-
DomainType(CheckedAssignError),
70-
/// Failed to set `Input ECX`: {0}
71-
InputEcx(CheckedAssignError),
72-
/// Failed to set all leaves, as more than `u32::MAX` sub-leaves are present: {0}
73-
Overflow(<u32 as TryFrom<usize>>::Error),
64+
/// Failed to set domain type (CPUID.(EAX=0xB,ECX={0}):ECX[15:8]): {1}
65+
DomainType(u32, CheckedAssignError),
66+
/// Failed to set input ECX (CPUID.(EAX=0xB,ECX={0}):ECX[7:0]): {1}
67+
InputEcx(u32, CheckedAssignError),
68+
/// Failed to set number of logical processors (CPUID.(EAX=0xB,ECX={0}):EBX[15:0]): {1}
69+
NumLogicalProcs(u32, CheckedAssignError),
70+
/// Failed to set right-shift bits (CPUID.(EAX=0xB,ECX={0}):EAX[4:0]): {1}
71+
RightShiftBits(u32, CheckedAssignError),
7472
/// Unexpected subleaf: {0}
7573
UnexpectedSubleaf(u32)
7674
}
@@ -348,18 +346,18 @@ impl super::Cpuid {
348346
// To get the next level APIC ID, shift right with at most 1 because we have
349347
// maximum 2 logical procerssors per core that can be represented by 1 bit.
350348
set_range(&mut subleaf.result.eax, 0..5, u32::from(cpu_bits))
351-
.map_err(ExtendedTopologyError::ApicId)?;
349+
.map_err(|err| ExtendedTopologyError::RightShiftBits(index, err))?;
352350

353351
// When cpu_count == 1 or HT is disabled, there is 1 logical core at this
354352
// domain; otherwise there are 2
355353
set_range(&mut subleaf.result.ebx, 0..16, u32::from(cpus_per_core))
356-
.map_err(ExtendedTopologyError::LogicalProcessors)?;
354+
.map_err(|err| ExtendedTopologyError::NumLogicalProcs(index, err))?;
357355

358356
// Skip setting 0 to ECX[7:0] since it's already reset to 0.
359357

360358
// Set the domain type identification value for logical processor,
361359
set_range(&mut subleaf.result.ecx, 8..16, 1)
362-
.map_err(ExtendedTopologyError::DomainType)?;
360+
.map_err(|err| ExtendedTopologyError::DomainType(index, err))?;
363361
}
364362
// Core domain
365363
1 => {
@@ -373,17 +371,17 @@ impl super::Cpuid {
373371
0..5,
374372
MAX_SUPPORTED_VCPUS.next_power_of_two().ilog2(),
375373
)
376-
.map_err(ExtendedTopologyError::ApicId)?;
374+
.map_err(|err| ExtendedTopologyError::RightShiftBits(index, err))?;
377375
set_range(&mut subleaf.result.ebx, 0..16, u32::from(cpu_count))
378-
.map_err(ExtendedTopologyError::LogicalProcessors)?;
376+
.map_err(|err| ExtendedTopologyError::NumLogicalProcs(index, err))?;
379377

380378
// Setting the input ECX value (i.e. `index`)
381379
set_range(&mut subleaf.result.ecx, 0..8, index)
382-
.map_err(ExtendedTopologyError::InputEcx)?;
380+
.map_err(|err| ExtendedTopologyError::InputEcx(index, err))?;
383381

384382
// Set the domain type identification value for core.
385383
set_range(&mut subleaf.result.ecx, 8..16, 2)
386-
.map_err(ExtendedTopologyError::DomainType)?;
384+
.map_err(|err| ExtendedTopologyError::DomainType(index, err))?;
387385
}
388386
_ => {
389387
// KVM no longer returns any subleaf numbers greater than 0. The patch was

0 commit comments

Comments
 (0)