Skip to contentSkip to conversation

KVM: x86: fix DR6 delivery for emulated hardware breakpoint

PATCH In review Paolo Bonzini 3 participants 3 messages May 5, 2020 – May 6, 2020

Subsystems (3)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86: fix DR6 delivery for emulated hardware breakpoint

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.

---
 arch/x86/kvm/x86.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +2 -3
index c5835f9cb9ad..8f61cb15f147 100644
@@ -473,6 +473,7 @@ void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
 		 * breakpoint), it is reserved and must be zero in DR6.
 		 */
 		vcpu->arch.dr6 &= ~BIT(12);
+		kvm_update_dr6(vcpu);
 		break;
 	case PF_VECTOR:
 		vcpu->arch.cr2 = payload;
@@ -6731,9 +6732,7 @@ static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
 					   vcpu->arch.db);
 
 		if (dr6 != 0) {
-			vcpu->arch.dr6 &= ~DR_TRAP_BITS;
-			vcpu->arch.dr6 |= dr6 | DR6_RTM;
-			kvm_queue_exception(vcpu, DB_VECTOR);
+			kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
 			*r = 1;
 			return true;
 		}
Signature · 1 line
2.18.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Paolo Bonzini

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.

I still think we could also change this in handle_exception_nmi() and handle_dr() as mentioned in the other thread, but no strong opinion:

Thanks,

Signature · 1 line
Peter Xu
#2 lore raw
In reply to Paolo Bonzini
I love your patch! Yet something to improve:

Hi Paolo,

I love your patch! Yet something to improve:

[auto build test ERROR on kvm/linux-next]
[also build test ERROR on tip/auto-latest linus/master v5.7-rc4 next-20200505] [cannot apply to linux/master]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Paolo-Bonzini/KVM-x86-fix-DR6-delivery-for-emulated-hardware-breakpoint/20200506-043817
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git linux-next
config: i386-allyesconfig (attached as .config)
compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

All error/warnings (new ones prefixed by >>):

arch/x86/kvm/x86.c: In function 'kvm_deliver_exception_payload':

>> arch/x86/kvm/x86.c:472:3: error: implicit declaration of function 'kvm_update_dr6'; did you mean 'kvm_update_cpuid'? [-Werror=implicit-function-declaration]
      kvm_update_dr6(vcpu);
      ^~~~~~~~~~~~~~
      kvm_update_cpuid
   arch/x86/kvm/x86.c: At top level:
>> arch/x86/kvm/x86.c:1071:13: warning: conflicting types for 'kvm_update_dr6'
    static void kvm_update_dr6(struct kvm_vcpu *vcpu)
                ^~~~~~~~~~~~~~
>> arch/x86/kvm/x86.c:1071:13: error: static declaration of 'kvm_update_dr6' follows non-static declaration
   arch/x86/kvm/x86.c:472:3: note: previous implicit declaration of 'kvm_update_dr6' was here
      kvm_update_dr6(vcpu);
      ^~~~~~~~~~~~~~

cc1: some warnings being treated as errors

vim +472 arch/x86/kvm/x86.c

432
433 void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
434 {
435 unsigned nr = vcpu->arch.exception.nr;
436 bool has_payload = vcpu->arch.exception.has_payload;
437 unsigned long payload = vcpu->arch.exception.payload;
438
439 if (!has_payload)
440 return;
441
442 switch (nr) {
443 case DB_VECTOR:
444 /*
445 * "Certain debug exceptions may clear bit 0-3. The
446 * remaining contents of the DR6 register are never
447 * cleared by the processor".
448 */
449 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
450 /*
451 * DR6.RTM is set by all #DB exceptions that don't clear it.
452 */
453 vcpu->arch.dr6 |= DR6_RTM;
454 vcpu->arch.dr6 |= payload;
455 /*
456 * Bit 16 should be set in the payload whenever the #DB
457 * exception should clear DR6.RTM. This makes the payload
458 * compatible with the pending debug exceptions under VMX.
459 * Though not currently documented in the SDM, this also
460 * makes the payload compatible with the exit qualification
461 * for #DB exceptions under VMX.
462 */
463 vcpu->arch.dr6 ^= payload & DR6_RTM;
464
465 /*
466 * The #DB payload is defined as compatible with the 'pending
467 * debug exceptions' field under VMX, not DR6. While bit 12 is
468 * defined in the 'pending debug exceptions' field (enabled
469 * breakpoint), it is reserved and must be zero in DR6.
470 */
471 vcpu->arch.dr6 &= ~BIT(12);

 > 472			kvm_update_dr6(vcpu);

473 break;
474 case PF_VECTOR:
475 vcpu->arch.cr2 = payload;
476 break;
477 }
478
479 vcpu->arch.exception.has_payload = false;
480 vcpu->arch.exception.payload = 0;
481 }
482 EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
483

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org