From 5551d6b5abaf9d5241904425e3991c256b30d22f Mon Sep 17 00:00:00 2001 From: frankyj915 Date: Sun, 15 Dec 2024 09:31:20 +0800 Subject: [PATCH 1/2] hypervisor/kvm: Fix setting core reg error when restoring VM vcpu_init() should be called before setting vcpu regs. Fix 5edbafc(migration: bugfix for mgiration) Signed-off-by: frankyj915 --- cpu/src/lib.rs | 20 +++++++++----------- hypervisor/src/kvm/aarch64/mod.rs | 6 ++++-- hypervisor/src/kvm/mod.rs | 4 ++-- hypervisor/src/kvm/x86_64/mod.rs | 8 +++++--- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/cpu/src/lib.rs b/cpu/src/lib.rs index 7698132..873cb49 100644 --- a/cpu/src/lib.rs +++ b/cpu/src/lib.rs @@ -160,7 +160,7 @@ pub trait CPUHypervisorOps: Send + Sync { fn set_boot_config( &self, arch_cpu: Arc>, - boot_config: &CPUBootConfig, + boot_config: &Option, #[cfg(target_arch = "aarch64")] vcpu_config: &CPUFeatures, ) -> Result<()>; @@ -323,16 +323,14 @@ impl CPUInterface for CPU { )))); } - if let Some(boot) = boot { - self.hypervisor_cpu - .set_boot_config( - self.arch_cpu.clone(), - boot, - #[cfg(target_arch = "aarch64")] - config, - ) - .with_context(|| "Failed to realize arch cpu")?; - } + self.hypervisor_cpu + .set_boot_config( + self.arch_cpu.clone(), + boot, + #[cfg(target_arch = "aarch64")] + config, + ) + .with_context(|| "Failed to realize arch cpu")?; self.arch_cpu .lock() diff --git a/hypervisor/src/kvm/aarch64/mod.rs b/hypervisor/src/kvm/aarch64/mod.rs index 0721236..5d2a938 100644 --- a/hypervisor/src/kvm/aarch64/mod.rs +++ b/hypervisor/src/kvm/aarch64/mod.rs @@ -135,7 +135,7 @@ impl KvmCpu { pub fn arch_set_boot_config( &self, arch_cpu: Arc>, - boot_config: &CPUBootConfig, + boot_config: &Option, vcpu_config: &CPUFeatures, ) -> Result<()> { let mut kvi = self.kvi.lock().unwrap(); @@ -169,7 +169,9 @@ impl KvmCpu { } drop(kvi); - arch_cpu.lock().unwrap().set_core_reg(boot_config); + if let Some(cfg) = boot_config { + arch_cpu.lock().unwrap().set_core_reg(cfg); + } self.arch_vcpu_init()?; diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index 671eb54..e20f102 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -544,7 +544,7 @@ impl CPUHypervisorOps for KvmCpu { fn set_boot_config( &self, arch_cpu: Arc>, - boot_config: &CPUBootConfig, + boot_config: &Option, #[cfg(target_arch = "aarch64")] vcpu_config: &CPUFeatures, ) -> Result<()> { #[cfg(target_arch = "aarch64")] @@ -1067,7 +1067,7 @@ mod test { let cpu = CPU::new(hypervisor_cpu.clone(), 0, x86_cpu, vm.clone()); // test `set_boot_config` function assert!(hypervisor_cpu - .set_boot_config(cpu.arch().clone(), &cpu_config) + .set_boot_config(cpu.arch().clone(), &Some(cpu_config)) .is_ok()); // test setup special registers diff --git a/hypervisor/src/kvm/x86_64/mod.rs b/hypervisor/src/kvm/x86_64/mod.rs index 7d7e7b5..e7d08ef 100644 --- a/hypervisor/src/kvm/x86_64/mod.rs +++ b/hypervisor/src/kvm/x86_64/mod.rs @@ -84,7 +84,7 @@ impl KvmCpu { pub fn arch_set_boot_config( &self, arch_cpu: Arc>, - boot_config: &CPUBootConfig, + boot_config: &Option, ) -> Result<()> { let mut locked_arch_cpu = arch_cpu.lock().unwrap(); let apic_id = locked_arch_cpu.apic_id; @@ -93,12 +93,14 @@ impl KvmCpu { .get_lapic() .with_context(|| format!("Failed to get lapic for CPU {}/KVM", apic_id))?; locked_arch_cpu.setup_lapic(lapic)?; - locked_arch_cpu.setup_regs(boot_config); let sregs = self .fd .get_sregs() .with_context(|| format!("Failed to get sregs for CPU {}/KVM", apic_id))?; - locked_arch_cpu.setup_sregs(sregs, boot_config)?; + if let Some(cfg) = boot_config { + locked_arch_cpu.setup_regs(cfg); + locked_arch_cpu.setup_sregs(sregs, cfg)?; + } locked_arch_cpu.setup_fpu(); locked_arch_cpu.setup_msrs(); -- 2.34.1