Skip to content

Commit 4d5523c

Browse files
committed
KVM: x86: fix DR6 delivery for various cases of #DB injection
Go through kvm_queue_exception_p so that the payload is correctly delivered through the exit qualification, and add a kvm_update_dr6 call to kvm_deliver_exception_payload that is needed on AMD. Reported-by: Peter Xu <[email protected]> Reviewed-by: Peter Xu <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent b9b2782 commit 4d5523c

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ bool kvm_rdpmc(struct kvm_vcpu *vcpu);
14491449

14501450
void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr);
14511451
void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
1452+
void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, unsigned long payload);
14521453
void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr);
14531454
void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
14541455
void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);

arch/x86/kvm/vmx/vmx.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4677,12 +4677,10 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
46774677
dr6 = vmcs_readl(EXIT_QUALIFICATION);
46784678
if (!(vcpu->guest_debug &
46794679
(KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
4680-
vcpu->arch.dr6 &= ~DR_TRAP_BITS;
4681-
vcpu->arch.dr6 |= dr6 | DR6_RTM;
46824680
if (is_icebp(intr_info))
46834681
WARN_ON(!skip_emulated_instruction(vcpu));
46844682

4685-
kvm_queue_exception(vcpu, DB_VECTOR);
4683+
kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
46864684
return 1;
46874685
}
46884686
kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
@@ -4936,9 +4934,7 @@ static int handle_dr(struct kvm_vcpu *vcpu)
49364934
vcpu->run->exit_reason = KVM_EXIT_DEBUG;
49374935
return 0;
49384936
} else {
4939-
vcpu->arch.dr6 &= ~DR_TRAP_BITS;
4940-
vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
4941-
kvm_queue_exception(vcpu, DB_VECTOR);
4937+
kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD);
49424938
return 1;
49434939
}
49444940
}

arch/x86/kvm/x86.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS;
104104
KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
105105

106106
static void update_cr8_intercept(struct kvm_vcpu *vcpu);
107+
static void kvm_update_dr6(struct kvm_vcpu *vcpu);
107108
static void process_nmi(struct kvm_vcpu *vcpu);
108109
static void enter_smm(struct kvm_vcpu *vcpu);
109110
static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
@@ -473,6 +474,7 @@ void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
473474
* breakpoint), it is reserved and must be zero in DR6.
474475
*/
475476
vcpu->arch.dr6 &= ~BIT(12);
477+
kvm_update_dr6(vcpu);
476478
break;
477479
case PF_VECTOR:
478480
vcpu->arch.cr2 = payload;
@@ -572,11 +574,12 @@ void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
572574
}
573575
EXPORT_SYMBOL_GPL(kvm_requeue_exception);
574576

575-
static void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
576-
unsigned long payload)
577+
void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
578+
unsigned long payload)
577579
{
578580
kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
579581
}
582+
EXPORT_SYMBOL_GPL(kvm_queue_exception_p);
580583

581584
static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
582585
u32 error_code, unsigned long payload)
@@ -6719,9 +6722,7 @@ static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
67196722
vcpu->arch.db);
67206723

67216724
if (dr6 != 0) {
6722-
vcpu->arch.dr6 &= ~DR_TRAP_BITS;
6723-
vcpu->arch.dr6 |= dr6 | DR6_RTM;
6724-
kvm_queue_exception(vcpu, DB_VECTOR);
6725+
kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
67256726
*r = 1;
67266727
return true;
67276728
}

0 commit comments

Comments
 (0)