Skip to contentSkip to conversation

KVM: nVMX: Reset RFLAGS on VM-exit

PATCH Superseded Jan Kiszka 2 participants 3 messages Mar 3, 2013

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 KVM: nVMX: Reset RFLAGS on VM-exit

From: Jan Kiszka <[email protected]>

Ouch, how could this work so well that far? We need to clear RFLAGS to the reset value as specified by the SDM. Particularly, IF must be off after VM-exit!

---
 arch/x86/kvm/vmx.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/kvm/vmx.c +1 -0
index 486078d..252f9c4 100644
@@ -7454,6 +7454,7 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
 
 	kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
 	kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
+	vmx_set_rflags(vcpu, 0x02);
 	/*
 	 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
 	 * actually changed, because it depends on the current state of
Signature · 1 line
1.7.3.4
#1 lore raw

On Sun, Mar 03, 2013, Jan Kiszka wrote about "[PATCH] KVM: nVMX: Reset RFLAGS on VM-exit":

Quoted 5 lines

From: Jan Kiszka <[email protected]>

Ouch, how could this work so well that far? We need to clear RFLAGS to the reset value as specified by the SDM. Particularly, IF must be off after VM-exit!

nested_vmx_succeed() or nested_vmx_fail*() were already clearing some of the fields that I understood was necessary to clear. But they did not clear the IF - I never realised (and didn't verify now) that this is part of the spec. And since L1 KVM anyways enters L2 with interrupts disabled, nested KVM would not see a difference.

arch/x86/kvm/vmx.c static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, · line 7457

Quoted 1 line
arch/x86/kvm/vmx.c +1 -0
+	vmx_set_rflags(vcpu, 0x02);

There's a macro X86_EFLAGS_BIT1 which you can use for this 0x02.

Signature · 4 lines
Nadav Har'El                        |         Sunday, Mar 3 2013, 22 Adar 5773
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |"I don't use drugs, my dreams are
http://nadav.harel.org.il           |frightening enough." -- M. C. Escher
author
#2 lore raw
Quoted 7 lines · Nadav Har'El

On Sun, Mar 03, 2013, Jan Kiszka wrote about "[PATCH] KVM: nVMX: Reset RFLAGS on VM-exit":

Quoted 5 lines

From: Jan Kiszka <[email protected]>

Ouch, how could this work so well that far? We need to clear RFLAGS to the reset value as specified by the SDM. Particularly, IF must be off after VM-exit!

Quoted 5 lines

nested_vmx_succeed() or nested_vmx_fail*() were already clearing some of the fields that I understood was necessary to clear. But they did not clear the IF - I never realised (and didn't verify now) that this is part of the spec. And since L1 KVM anyways enters L2 with interrupts disabled, nested KVM would not see a difference.

But we didn't restore rflags on vmexit either, we reused the guest value for L1. It was pure luck (and due to KVM's short IRQ-off phase after vmexit) when things worked. But I bet this should fix some spurious issues.

Quoted 4 lines

arch/x86/kvm/vmx.c static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, · line 7457

Quoted 2 lines
arch/x86/kvm/vmx.c +1 -0
+	vmx_set_rflags(vcpu, 0x02);

There's a macro X86_EFLAGS_BIT1 which you can use for this 0x02.

Ah, good to know. Checked vmx_vcpu_reset and found this value.

Jan