Skip to content

Commit 2971a0f

Browse files
ShadowCurseroypat
authored andcommitted
refactor: move CpuConfiguration creation into itself
Move manual creation of `CpuConfiguration` from `configure_system_for_boot` into `CpuConfiguration::new`. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 6fdb2b0 commit 2971a0f

File tree

4 files changed

+51
-37
lines changed

4 files changed

+51
-37
lines changed

src/vmm/src/arch/aarch64/mod.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ use linux_loader::loader::{Cmdline, KernelLoader};
2727
use vm_memory::GuestMemoryError;
2828

2929
use self::gic::GICDevice;
30-
use crate::arch::aarch64::regs::Aarch64RegisterVec;
31-
use crate::arch::aarch64::vcpu::{VcpuArchError, get_registers};
3230
use crate::arch::{BootProtocol, DeviceType, EntryPoint};
3331
use crate::cpu_config::aarch64::{CpuConfiguration, CpuConfigurationError};
3432
use crate::cpu_config::templates::CustomCpuTemplate;
@@ -51,14 +49,12 @@ pub enum ConfigurationError {
5149
KernelFile,
5250
/// Cannot load kernel due to invalid memory configuration or invalid kernel image: {0}
5351
KernelLoader(#[from] linux_loader::loader::Error),
54-
/// Error initializing the vcpu: {0}
55-
VcpuInit(KvmVcpuError),
56-
/// Error configuring the vcpu: {0}
57-
VcpuConfigure(KvmVcpuError),
58-
/// Error reading vcpu registers: {0}
59-
VcpuGetRegs(VcpuArchError),
52+
/// Error creating vcpu configuration: {0}
53+
VcpuConfig(CpuConfigurationError),
6054
/// Error applying vcpu template: {0}
6155
VcpuApplyTemplate(CpuConfigurationError),
56+
/// Error configuring the vcpu: {0}
57+
VcpuConfigure(KvmVcpuError),
6258
}
6359

6460
/// The start of the memory area reserved for MMIO devices.
@@ -84,18 +80,8 @@ pub fn configure_system_for_boot(
8480
boot_cmdline: Cmdline,
8581
) -> Result<(), ConfigurationError> {
8682
// Construct the base CpuConfiguration to apply CPU template onto.
87-
let cpu_config = {
88-
for vcpu in vcpus.iter_mut() {
89-
vcpu.kvm_vcpu
90-
.init(&cpu_template.vcpu_features)
91-
.map_err(ConfigurationError::VcpuInit)?;
92-
}
93-
94-
let mut regs = Aarch64RegisterVec::default();
95-
get_registers(&vcpus[0].kvm_vcpu.fd, &cpu_template.reg_list(), &mut regs)
96-
.map_err(ConfigurationError::VcpuGetRegs)?;
97-
CpuConfiguration { regs }
98-
};
83+
let cpu_config =
84+
CpuConfiguration::new(cpu_template, vcpus).map_err(ConfigurationError::VcpuConfig)?;
9985

10086
// Apply CPU template to the base CpuConfiguration.
10187
let cpu_config = CpuConfiguration::apply_template(cpu_config, cpu_template)

src/vmm/src/arch/x86_64/mod.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use super::EntryPoint;
4848
use crate::acpi::create_acpi_tables;
4949
use crate::arch::{BootProtocol, SYSTEM_MEM_SIZE, SYSTEM_MEM_START};
5050
use crate::cpu_config::templates::{CustomCpuTemplate, GuestConfigError};
51-
use crate::cpu_config::x86_64::{CpuConfiguration, cpuid};
51+
use crate::cpu_config::x86_64::CpuConfiguration;
5252
use crate::device_manager::resources::ResourceAllocator;
5353
use crate::initrd::InitrdConfig;
5454
use crate::utils::{mib_to_bytes, u64_to_usize};
@@ -153,16 +153,8 @@ pub fn configure_system_for_boot(
153153
boot_cmdline: Cmdline,
154154
) -> Result<(), ConfigurationError> {
155155
// Construct the base CpuConfiguration to apply CPU template onto.
156-
let cpu_config = {
157-
let cpuid = cpuid::Cpuid::try_from(vmm.kvm.supported_cpuid.clone())
158-
.map_err(GuestConfigError::CpuidFromKvmCpuid)?;
159-
let msrs = vcpus[0]
160-
.kvm_vcpu
161-
.get_msrs(cpu_template.msr_index_iter())
162-
.map_err(GuestConfigError::VcpuIoctl)?;
163-
CpuConfiguration { cpuid, msrs }
164-
};
165-
156+
let cpu_config =
157+
CpuConfiguration::new(vmm.kvm.supported_cpuid.clone(), cpu_template, &vcpus[0])?;
166158
// Apply CPU template to the base CpuConfiguration.
167159
let cpu_config = CpuConfiguration::apply_template(cpu_config, cpu_template)?;
168160

src/vmm/src/cpu_config/aarch64/mod.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ pub mod static_cpu_templates;
99
pub mod test_utils;
1010

1111
use super::templates::CustomCpuTemplate;
12+
use crate::Vcpu;
1213
use crate::arch::aarch64::regs::{Aarch64RegisterVec, RegSize};
13-
use crate::arch::aarch64::vcpu::VcpuArchError;
14+
use crate::arch::aarch64::vcpu::{VcpuArchError, get_registers};
15+
use crate::vstate::vcpu::KvmVcpuError;
1416

1517
/// Errors thrown while configuring templates.
16-
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
17-
#[error("Failed to create a guest cpu configuration: {0}")]
18-
pub struct CpuConfigurationError(#[from] pub VcpuArchError);
18+
#[derive(Debug, PartialEq, Eq, thiserror::Error, displaydoc::Display)]
19+
pub enum CpuConfigurationError {
20+
/// Error initializing the vcpu: {0}
21+
VcpuInit(#[from] KvmVcpuError),
22+
/// Error reading vcpu registers: {0}
23+
VcpuGetRegs(#[from] VcpuArchError),
24+
}
1925

2026
/// CPU configuration for aarch64
2127
#[derive(Debug, Default, Clone, PartialEq, Eq)]
@@ -25,6 +31,20 @@ pub struct CpuConfiguration {
2531
}
2632

2733
impl CpuConfiguration {
34+
/// Create new CpuConfiguration.
35+
pub fn new(
36+
cpu_template: &CustomCpuTemplate,
37+
vcpus: &mut [Vcpu],
38+
) -> Result<Self, CpuConfigurationError> {
39+
for vcpu in vcpus.iter_mut() {
40+
vcpu.kvm_vcpu.init(&cpu_template.vcpu_features)?;
41+
}
42+
43+
let mut regs = Aarch64RegisterVec::default();
44+
get_registers(&vcpus[0].kvm_vcpu.fd, &cpu_template.reg_list(), &mut regs)?;
45+
Ok(CpuConfiguration { regs })
46+
}
47+
2848
/// Creates new guest CPU config based on the provided template
2949
pub fn apply_template(
3050
mut self,

src/vmm/src/cpu_config/x86_64/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ pub mod test_utils;
1212

1313
use std::collections::BTreeMap;
1414

15+
use kvm_bindings::CpuId;
16+
1517
use self::custom_cpu_template::CpuidRegister;
1618
use super::templates::CustomCpuTemplate;
19+
use crate::Vcpu;
1720
use crate::cpu_config::x86_64::cpuid::{Cpuid, CpuidKey};
1821

1922
/// Errors thrown while configuring templates.
@@ -24,9 +27,9 @@ pub enum CpuConfigurationError {
2427
/// Template changes an MSR entry not supported by KVM: Register Address: {0:0x}
2528
MsrNotSupported(u32),
2629
/// Can create cpuid from raw: {0}
27-
CpuidFromKvmCpuid(crate::cpu_config::x86_64::cpuid::CpuidTryFromKvmCpuid),
30+
CpuidFromKvmCpuid(#[from] crate::cpu_config::x86_64::cpuid::CpuidTryFromKvmCpuid),
2831
/// KVM vcpu ioctl failed: {0}
29-
VcpuIoctl(crate::vstate::vcpu::KvmVcpuError),
32+
VcpuIoctl(#[from] crate::vstate::vcpu::KvmVcpuError),
3033
}
3134

3235
/// CPU configuration for x86_64 CPUs
@@ -41,6 +44,19 @@ pub struct CpuConfiguration {
4144
}
4245

4346
impl CpuConfiguration {
47+
/// Create new CpuConfiguration.
48+
pub fn new(
49+
supported_cpuid: CpuId,
50+
cpu_template: &CustomCpuTemplate,
51+
first_vcpu: &Vcpu,
52+
) -> Result<Self, CpuConfigurationError> {
53+
let cpuid = cpuid::Cpuid::try_from(supported_cpuid)?;
54+
let msrs = first_vcpu
55+
.kvm_vcpu
56+
.get_msrs(cpu_template.msr_index_iter())?;
57+
Ok(CpuConfiguration { cpuid, msrs })
58+
}
59+
4460
/// Modifies provided config with changes from template
4561
pub fn apply_template(
4662
self,

0 commit comments

Comments
 (0)