Skip to content

Commit 3dc47d2

Browse files
committed
vmm: fix immediate_exit handling for PIO/MMIO code paths
To finish the emulation of PIO or MMIO code paths [0], we need to get back into KVM_RUN. In order to not lose any signal that we received, we must preserve the original `immediate_exit` flag in case it was already set by the signal handler. [0] https://elixir.bootlin.com/linux/v6.12/source/arch/x86/kvm/x86.c#L11555
1 parent c5c20b5 commit 3dc47d2

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

vmm/src/cpu.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,16 +1121,9 @@ impl CpuManager {
11211121
}
11221122

11231123
let kvm_run = KVM_RUN.get();
1124-
assert!(!kvm_run.is_null(), "kvm_run should have been mapped as part of vCPU setup");
1125-
// SAFETY: We mapped the whole structure.
1126-
let ptr_kvm_run_immediate_exit = unsafe { kvm_run.cast::<u8>().add(1) };
1127-
// SAFETY: We know the mapping is valid.
1128-
unsafe {
1129-
core::ptr::write_volatile(ptr_kvm_run_immediate_exit, 1);
1130-
}
1131-
1132-
// TODO ::Release?
1133-
std::sync::atomic::fence(Ordering::SeqCst);
1124+
let kvm_run = unsafe {
1125+
kvm_run.as_mut().expect("kvm_run should have been mapped as part of vCPU setup") };
1126+
kvm_run.immediate_exit = 1;
11341127
}
11351128
// This uses an async signal safe handler to kill the vcpu handles.
11361129
register_signal_handler(SIGRTMIN(), handle_signal)
@@ -1170,12 +1163,17 @@ impl CpuManager {
11701163

11711164
#[cfg(feature = "kvm")]
11721165
if matches!(hypervisor_type, HypervisorType::Kvm) {
1166+
let old_immediate_exit_flag = {
1167+
let kvm_run: *mut kvm_run = KVM_RUN.get();
1168+
let kvm_run = unsafe { kvm_run.as_mut().unwrap() };
1169+
kvm_run.immediate_exit != 0
1170+
};
11731171
vcpu.lock().as_ref().unwrap().vcpu.set_immediate_exit(true);
11741172
if !matches!(vcpu.lock().unwrap().run(), Ok(VmExit::Ignore)) {
11751173
error!("Unexpected VM exit on \"immediate_exit\" run");
11761174
break;
11771175
}
1178-
vcpu.lock().as_ref().unwrap().vcpu.set_immediate_exit(false);
1176+
vcpu.lock().as_ref().unwrap().vcpu.set_immediate_exit(old_immediate_exit_flag);
11791177
}
11801178

11811179
vcpu_run_interrupted.store(true, Ordering::SeqCst);

0 commit comments

Comments
 (0)