Skip to contentSkip to conversation

KVM: x86: stop IO emulation cycle if instruction pointer is modified

PATCH Marcelo Tosatti 2 participants 3 messages Jul 5, 2013 – Jul 8, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

MMIO/PIO emulation should be interrupted if the system is restarted. Otherwise in progress IO emulation continues at the instruction pointer, even after vcpus' IP has been modified by KVM_SET_REGS.

Use IP change as an indicator to reset MMIO/PIO emulation state.

arch/x86/kvm/x86.c +4 -0
index e8ba99c..9f7ce41 100644
@@ -6144,6 +6144,10 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
 	kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
 #endif
 
+	/* stop IO emulation cycle if RIP changes */
+	if (kvm_rip_read(vcpu) != regs->rip)
+		vcpu->arch.complete_userspace_io = NULL;
+
 	kvm_rip_write(vcpu, regs->rip);
 	kvm_set_rflags(vcpu, regs->rflags);
 
#1 lore raw
Quoted 7 lines · Marcelo Tosatti

MMIO/PIO emulation should be interrupted if the system is restarted. Otherwise in progress IO emulation continues at the instruction pointer, even after vcpus' IP has been modified by KVM_SET_REGS.

Use IP change as an indicator to reset MMIO/PIO emulation state.

Userspace has to return to the kernel to complete pending IO operation. This is documented in Documentation/virtual/kvm/api.txt. If this is not what program does it is a bug. What userspace you see the problem with?

Quoted 7 lines
arch/x86/kvm/x86.c +0 -0
index e8ba99c..9f7ce41 100644

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) · line 6153

Quoted 10 lines
arch/x86/kvm/x86.c +4 -0
@@ -6144,6 +6144,10 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
 	kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
 #endif
 
+	/* stop IO emulation cycle if RIP changes */
+	if (kvm_rip_read(vcpu) != regs->rip)
+		vcpu->arch.complete_userspace_io = NULL;
+
 	kvm_rip_write(vcpu, regs->rip);
 	kvm_set_rflags(vcpu, regs->rflags);
 

--
Gleb.

#2 lore raw
Quoted 11 lines · Gleb Natapov
Quoted 7 lines · Marcelo Tosatti

MMIO/PIO emulation should be interrupted if the system is restarted. Otherwise in progress IO emulation continues at the instruction pointer, even after vcpus' IP has been modified by KVM_SET_REGS.

Use IP change as an indicator to reset MMIO/PIO emulation state.

Userspace has to return to the kernel to complete pending IO operation. This is documented in Documentation/virtual/kvm/api.txt. If this is not what program does it is a bug. What userspace you see the problem with?

You're right, this patch should not be necessary.