Skip to contentSkip to conversation

KVM: X86: Use kvm_get_linear_rip() in single-step and #DB/#BP interception

PATCH Applied Yuan Yao 3 participants 4 messages May 26, 2021 – May 27, 2021

Subsystems (3)
lore
Share & tools
lore

Yuan Yao

author
#0 lore raw

PATCH 1/1 KVM: X86: Use kvm_get_linear_rip() in single-step and #DB/#BP interception

From: Yuan Yao <[email protected]>

The kvm_get_linear_rip() handles x86/long mode cases well and has better readability, __kvm_set_rflags() also use the paired fucntion kvm_is_linear_rip() to check the vcpu->arch.singlestep_rip set in kvm_arch_vcpu_ioctl_set_guest_debug(), so change the "CS.BASE + RIP" code in kvm_arch_vcpu_ioctl_set_guest_debug() and handle_exception_nmi() to this one.

arch/x86/kvm/vmx/vmx.c +2 -3
index 4bceb5ca3a89..0db05cfbe434 100644
@@ -4843,7 +4843,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	struct kvm_run *kvm_run = vcpu->run;
 	u32 intr_info, ex_no, error_code;
-	unsigned long cr2, rip, dr6;
+	unsigned long cr2, dr6;
 	u32 vect_info;
 
 	vect_info = vmx->idt_vectoring_info;
@@ -4933,8 +4933,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 		vmx->vcpu.arch.event_exit_inst_len =
 			vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
 		kvm_run->exit_reason = KVM_EXIT_DEBUG;
-		rip = kvm_rip_read(vcpu);
-		kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
+		kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
 		kvm_run->debug.arch.exception = ex_no;
 		break;
 	case AC_VECTOR:
arch/x86/kvm/x86.c +1 -2
index 9b6bca616929..5cac5d883710 100644
@@ -10115,8 +10115,7 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
 	kvm_update_dr7(vcpu);
 
 	if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
-		vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
-			get_segment_base(vcpu, VCPU_SREG_CS);
+		vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu);
 
 	/*
 	 * Trigger an rflags update that will inject or remove the trace
Signature · 1 line
2.17.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Yuan Yao

From: Yuan Yao <[email protected]>

The kvm_get_linear_rip() handles x86/long mode cases well and has better readability, __kvm_set_rflags() also use the paired fucntion kvm_is_linear_rip() to check the vcpu->arch.singlestep_rip

^^^^^^^^
function

Please run checkpatch before submitting in the future, it will catch some of these misspellings.

Quoted 5 lines

set in kvm_arch_vcpu_ioctl_set_guest_debug(), so change the "CS.BASE + RIP" code in kvm_arch_vcpu_ioctl_set_guest_debug() and handle_exception_nmi() to this one.

author
#2 lore raw
Quoted 11 lines · Sean Christopherson
Quoted 5 lines · Yuan Yao

From: Yuan Yao <[email protected]>

The kvm_get_linear_rip() handles x86/long mode cases well and has better readability, __kvm_set_rflags() also use the paired fucntion kvm_is_linear_rip() to check the vcpu->arch.singlestep_rip

^^^^^^^^
function

Please run checkpatch before submitting in the future, it will catch some of these misspellings.

Sorry for this mistake, I will take care next time.

Quoted 8 lines
Quoted 6 lines

set in kvm_arch_vcpu_ioctl_set_guest_debug(), so change the "CS.BASE + RIP" code in kvm_arch_vcpu_ioctl_set_guest_debug() and handle_exception_nmi() to this one.

maintainer Applied
#3 lore raw
In reply to Yuan Yao
Quoted 40 lines · Yuan Yao

From: Yuan Yao <[email protected]>

The kvm_get_linear_rip() handles x86/long mode cases well and has better readability, __kvm_set_rflags() also use the paired fucntion kvm_is_linear_rip() to check the vcpu->arch.singlestep_rip set in kvm_arch_vcpu_ioctl_set_guest_debug(), so change the "CS.BASE + RIP" code in kvm_arch_vcpu_ioctl_set_guest_debug() and handle_exception_nmi() to this one.

arch/x86/kvm/vmx/vmx.c +2 -3
index 4bceb5ca3a89..0db05cfbe434 100644
@@ -4843,7 +4843,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
  	struct vcpu_vmx *vmx = to_vmx(vcpu);
  	struct kvm_run *kvm_run = vcpu->run;
  	u32 intr_info, ex_no, error_code;
-	unsigned long cr2, rip, dr6;
+	unsigned long cr2, dr6;
  	u32 vect_info;
 
  	vect_info = vmx->idt_vectoring_info;
@@ -4933,8 +4933,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
  		vmx->vcpu.arch.event_exit_inst_len =
  			vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  		kvm_run->exit_reason = KVM_EXIT_DEBUG;
-		rip = kvm_rip_read(vcpu);
-		kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
+		kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  		kvm_run->debug.arch.exception = ex_no;
  		break;
  	case AC_VECTOR:
arch/x86/kvm/x86.c +0 -0
index 9b6bca616929..5cac5d883710 100644

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, · line 10121

Quoted 9 lines
arch/x86/kvm/x86.c +1 -2
@@ -10115,8 +10115,7 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  	kvm_update_dr7(vcpu);
 
  	if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
-		vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
-			get_segment_base(vcpu, VCPU_SREG_CS);
+		vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu);
 
  	/*
  	 * Trigger an rflags update that will inject or remove the trace

Queued, thanks.

Paolo