Re: [PATCH v2 8/9] KVM: x86, SVM: isolate vcpu->arch.dr6 from vmcb->save.dr6
From: Peter Xu
Date: Fri May 08 2020 - 11:32:21 EST
On Fri, May 08, 2020 at 12:33:57AM +0200, Paolo Bonzini wrote:
> On 07/05/20 21:28, Peter Xu wrote:
> >> - svm->vcpu.arch.dr6 = dr6;
> >> + WARN_ON(svm->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT);
> >> + svm->vcpu.arch.dr6 &= ~(DR_TRAP_BITS | DR6_RTM);
> >> + svm->vcpu.arch.dr6 |= dr6 & ~DR6_FIXED_1;
> > I failed to figure out what the above calculation is going to do...
>
> The calculation is merging the cause of the #DB with the guest DR6.
> It's basically the same effect as kvm_deliver_exception_payload. The
> payload has DR6_RTM flipped compared to DR6, so you have the following
> simplfications:
>
> payload = (dr6 ^ DR6_RTM) & ~DR6_FIXED_1;
> /* This is kvm_deliver_exception_payload: */
> vcpu->arch.dr6 &= ~DR_TRAP_BITS;
> vcpu->arch.dr6 |= DR6_RTM;
> /* copy dr6 bits other than RTM */
> vcpu->arch.dr6 |= payload;
> /* copy flipped RTM bit */
> vcpu->arch.dr6 ^= payload & DR6_RTM;
>
> ->
>
> payload = (dr6 ^ DR6_RTM) & ~DR6_FIXED_1;
> /* clear RTM here, so that we can OR it below */
> vcpu->arch.dr6 &= ~(DR_TRAP_BITS | DR6_RTM);
> /* copy dr6 bits other than RTM */
> vcpu->arch.dr6 |= payload & ~DR6_RTM;
> /* copy flipped RTM bit */
> vcpu->arch.dr6 |= (payload ^ DR6_RTM) & DR6_RTM;
>
> ->
>
> /* we can drop the double XOR of DR6_RTM */
> dr6 &= ~DR6_FIXED_1;
> vcpu->arch.dr6 &= ~(DR_TRAP_BITS | DR6_RTM);
> vcpu->arch.dr6 |= dr6 & ~DR6_RTM;
> vcpu->arch.dr6 |= dr6 & DR6_RTM;
>
> ->
>
> /* we can do the two ORs with a single operation */
> vcpu->arch.dr6 &= ~(DR_TRAP_BITS | DR6_RTM);
> vcpu->arch.dr6 |= dr6 & ~DR6_FIXED_1;
Oh that's quite some math. :) Thanks Paolo!
Shall we introduce a helper for both kvm_deliver_exception_payload and here
(e.g. kvm_merge_dr6)? Also, wondering whether this could be a bit easier to
follow by defining:
/*
* These bits could be kept being set until the next #DB if not
* explicitly cleared.
*/
#define DR6_CARRY_OVER_BITS (DR6_BT | DR6_BS | DR6_BD)
Then the imho above calculation could also become:
vcpu->arch.dr6 = (vcpu->arch.dr6 & DR6_CARRY_OVER_BITS) | save.dr6;
What do you think?
>
> > E.g., I
> > think the old "BT|BS|BD" bits in the old arch.dr6 cache will be leftover even
> > if none of them is set in save.dr6, while we shouldn't?
>
> Those bits should be kept; this is covered for example by the "hw
> breakpoint (test that dr6.BS is not cleared)" testcase in kvm-unit-tests
> x86/debug.c.
Right. Thanks!
--
Peter Xu