Skip to content

Commit 85917b7

Browse files
committed
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 0d6bca8 commit 85917b7

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

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

Lines changed: 4 additions & 18 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,12 +49,10 @@ pub enum ConfigurationError {
5149
KernelFile,
5250
/// Cannot load kernel due to invalid memory configuration or invalid kernel image: {0}
5351
KernelLoader(linux_loader::loader::Error),
54-
/// Error initializing the vcpu: {0}
55-
VcpuInit(KvmVcpuError),
52+
/// Error creating vcpu configuration: {0}
53+
VcpuConfig(CpuConfigurationError),
5654
/// Error configuring the vcpu: {0}
5755
VcpuConfigure(KvmVcpuError),
58-
/// Error reading vcpu registers: {0}
59-
VcpuGetRegs(VcpuArchError),
6056
/// Error applying vcpu template: {0}
6157
VcpuApplyTemplate(CpuConfigurationError),
6258
}
@@ -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: 27 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(KvmVcpuError),
22+
/// Error reading vcpu registers: {0}
23+
VcpuGetRegs(VcpuArchError),
24+
}
1925

2026
/// CPU configuration for aarch64
2127
#[derive(Debug, Default, Clone, PartialEq, Eq)]
@@ -25,6 +31,23 @@ 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
41+
.init(&cpu_template.vcpu_features)
42+
.map_err(CpuConfigurationError::VcpuInit)?;
43+
}
44+
45+
let mut regs = Aarch64RegisterVec::default();
46+
get_registers(&vcpus[0].kvm_vcpu.fd, &cpu_template.reg_list(), &mut regs)
47+
.map_err(CpuConfigurationError::VcpuGetRegs)?;
48+
Ok(CpuConfiguration { regs })
49+
}
50+
2851
/// Creates new guest CPU config based on the provided template
2952
pub fn apply_template(
3053
mut self,

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

Lines changed: 18 additions & 0 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.
@@ -41,6 +44,21 @@ 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+
.map_err(CpuConfigurationError::CpuidFromKvmCpuid)?;
55+
let msrs = first_vcpu
56+
.kvm_vcpu
57+
.get_msrs(cpu_template.msr_index_iter())
58+
.map_err(CpuConfigurationError::VcpuIoctl)?;
59+
Ok(CpuConfiguration { cpuid, msrs })
60+
}
61+
4462
/// Modifies provided config with changes from template
4563
pub fn apply_template(
4664
self,

0 commit comments

Comments
 (0)