Skip to content

Commit 4785ad0

Browse files
committed
refactor(vmm): move out arch-agnostic operations
Move out arch-agnostic operations from arch-specific operations. Thanks to the previous commits, now it is possible to do this. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent a86a87b commit 4785ad0

File tree

1 file changed

+32
-46
lines changed

1 file changed

+32
-46
lines changed

src/vmm/src/builder.rs

+32-46
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ use crate::construct_kvm_mpidrs;
4242
use crate::device_manager::legacy::PortIODeviceManager;
4343
use crate::device_manager::mmio::MMIODeviceManager;
4444
use crate::device_manager::persist::MMIODevManagerConstructorArgs;
45-
use crate::guest_config::templates::{GetCpuTemplate, GetCpuTemplateError, GuestConfigError};
45+
use crate::guest_config::templates::{
46+
CpuConfiguration, GetCpuTemplate, GetCpuTemplateError, GuestConfigError,
47+
};
4648
use crate::persist::{MicrovmState, MicrovmStateError};
4749
use crate::resources::VmResources;
4850
use crate::vmm_config::boot_source::BootConfig;
@@ -785,38 +787,48 @@ pub fn configure_system_for_boot(
785787

786788
let cpu_template = vm_config.cpu_template.get_cpu_template()?;
787789

790+
// Construct the base CpuConfiguration to apply CPU template onto.
788791
#[cfg(target_arch = "x86_64")]
789-
{
792+
let cpu_config = {
790793
use crate::guest_config::cpuid;
791-
// Construct the base CpuConfiguration to apply CPU template onto.
792794
let cpuid = cpuid::Cpuid::try_from(cpuid::RawCpuid::from(vmm.vm.supported_cpuid().clone()))
793795
.map_err(GuestConfigError::CpuidFromRaw)?;
794796
let msr_index_list = cpu_template.get_msr_index_list();
795797
let msrs = vcpus[0]
796798
.kvm_vcpu
797799
.get_msrs(msr_index_list)
798800
.map_err(GuestConfigError::VcpuIoctl)?;
799-
let cpu_config = crate::guest_config::x86_64::CpuConfiguration { cpuid, msrs };
801+
CpuConfiguration { cpuid, msrs }
802+
};
800803

801-
// Apply CPU template to the base CpuConfiguration.
802-
let cpu_config = crate::guest_config::x86_64::CpuConfiguration::apply_template(
803-
cpu_config,
804-
&cpu_template,
805-
)?;
804+
#[cfg(target_arch = "aarch64")]
805+
let cpu_config = {
806+
let regs = vcpus[0]
807+
.kvm_vcpu
808+
.get_regs(&cpu_template.reg_list())
809+
.map_err(GuestConfigError)?;
810+
CpuConfiguration { regs }
811+
};
806812

807-
let vcpu_config = VcpuConfig {
808-
vcpu_count: vm_config.vcpu_count,
809-
smt: vm_config.smt,
810-
cpu_config,
811-
};
813+
// Apply CPU template to the base CpuConfiguration.
814+
let cpu_config = CpuConfiguration::apply_template(cpu_config, &cpu_template)?;
812815

813-
for vcpu in vcpus.iter_mut() {
814-
vcpu.kvm_vcpu
815-
.configure(vmm.guest_memory(), entry_addr, &vcpu_config)
816-
.map_err(Error::VcpuConfigure)
817-
.map_err(Internal)?;
818-
}
816+
let vcpu_config = VcpuConfig {
817+
vcpu_count: vm_config.vcpu_count,
818+
smt: vm_config.smt,
819+
cpu_config,
820+
};
819821

822+
// Configure vCPUs with normalizing and setting the generated CPU configuration.
823+
for vcpu in vcpus.iter_mut() {
824+
vcpu.kvm_vcpu
825+
.configure(vmm.guest_memory(), entry_addr, &vcpu_config)
826+
.map_err(Error::VcpuConfigure)
827+
.map_err(Internal)?;
828+
}
829+
830+
#[cfg(target_arch = "x86_64")]
831+
{
820832
// Write the kernel command line to guest memory. This is x86_64 specific, since on
821833
// aarch64 the command line will be specified through the FDT.
822834
let cmdline_size = boot_cmdline
@@ -840,32 +852,6 @@ pub fn configure_system_for_boot(
840852
}
841853
#[cfg(target_arch = "aarch64")]
842854
{
843-
// Construct the base CpuConfiguration to apply CPU template onto.
844-
let regs = vcpus[0]
845-
.kvm_vcpu
846-
.get_regs(&cpu_template.reg_list())
847-
.map_err(GuestConfigError)?;
848-
let cpu_config = crate::guest_config::aarch64::CpuConfiguration { regs };
849-
850-
// Apply CPU template to the base CpuConfiguration.
851-
let cpu_config = crate::guest_config::aarch64::CpuConfiguration::apply_template(
852-
cpu_config,
853-
&cpu_template,
854-
)?;
855-
856-
let vcpu_config = VcpuConfig {
857-
vcpu_count: vm_config.vcpu_count,
858-
smt: vm_config.smt,
859-
cpu_config,
860-
};
861-
862-
for vcpu in vcpus.iter_mut() {
863-
vcpu.kvm_vcpu
864-
.configure(vmm.guest_memory(), entry_addr, &vcpu_config)
865-
.map_err(Error::VcpuConfigure)
866-
.map_err(Internal)?;
867-
}
868-
869855
let vcpu_mpidr = vcpus
870856
.iter_mut()
871857
.map(|cpu| cpu.kvm_vcpu.get_mpidr())

0 commit comments

Comments
 (0)