KVM: x86: Improve the handling of debug exceptions during instruction emulation
v2 cover KVM: x86: Improve the handling of debug exceptions during instruction emulation
During my testing, I found that guest debugging with 'DR6.BD' does not work in instruction emulation, as the current code only considers the guest's DR7. Upon reviewing the code, I also observed that the checks for the userspace guest debugging feature and the guest's own debugging feature are repeated in different places during instruction emulation, but the overall logic is the same. If guest debug is enabled, it needs to exit to userspace; otherwise, a #DB exception needs to be injected into the guest. Therefore, as suggested by Jiangshan Lai, some cleanup has been done for #DB handling in instruction emulation in this patchset. A new function named 'kvm_inject_emulated_db()' is introduced to consolidate all the checking logic. Moreover, I hope we can make the #DB interception path use the same function as well.
Additionally, when I looked into the single-step #DB handling in instruction emulation, I noticed that the interrupt shadow is toggled, but it is not considered in the single-step #DB injection. This oversight causes VM entry to fail on VMX (due to pending debug exceptions state checking).
As pointed out by Sean, fault-like code #DBs can be coincident with trap-like single-step #DBs at the instruction boundary on the hardware. However it is difficult to emulate this in the emulator, as kvm_vcpu_check_code_breakpoint() is called at the start of the next instruction while the single-step #DB for the previous instruction has already been injected.
v1->v2:
- cleanup in inject_emulated_exception().
- rename 'set_pending_dbg' callback as 'refresh_pending_dbg_exceptions'.
- fold refresh_pending_dbg_exceptions() call into kvm_vcpu_do_singlestep().
- Split the change to move up kvm_set_rflags() into a single patch.
- Move the #DB and IRQ handler registration after guest debug testcases.
Hou Wenlong (9):
KVM: x86: Capture "struct x86_exception" in
inject_emulated_exception()
KVM: x86: Set guest DR6 by kvm_queue_exception_p() in instruction
emulation
KVM: x86: Check guest debug in DR access instruction emulation
KVM: x86: Only check effective code breakpoint in emulation
KVM: x86: Consolidate KVM_GUESTDBG_SINGLESTEP check into the
kvm_inject_emulated_db()
KVM: x86: Move kvm_set_rflags() up before kvm_vcpu_do_singlestep()
KVM: VMX: Refresh 'PENDING_DBG_EXCEPTIONS.BS' bit during instruction
emulation
KVM: selftests: Verify guest debug DR7.GD checking during instruction
emulation
KVM: selftests: Verify 'BS' bit checking in pending debug exception
during VM entry
arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/emulate.c | 14 +-- arch/x86/kvm/kvm_emulate.h | 7 +- arch/x86/kvm/vmx/main.c | 9 ++ arch/x86/kvm/vmx/vmx.c | 15 ++- arch/x86/kvm/vmx/x86_ops.h | 1 + arch/x86/kvm/x86.c | 116 ++++++++++-------- arch/x86/kvm/x86.h | 7 ++ .../selftests/kvm/include/x86/processor.h | 3 +- tools/testing/selftests/kvm/x86/debug_regs.c | 72 ++++++++++- 11 files changed, 178 insertions(+), 68 deletions(-)
base-commit: 5d3e2d9ba9ed68576c70c127e4f7446d896f2af2 --
2.31.1
PATCH v2 1/9 KVM: x86: Capture "struct x86_exception" in inject_emulated_exception()
As all callers in inject_emulated_exception() use "struct x86_exception" directly, capture it locally instead of using the context.
No functional change intended.
--- arch/x86/kvm/x86.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
arch/x86/kvm/x86.c +6 -7
@@ -8923,15 +8923,14 @@ static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)static void inject_emulated_exception (struct kvm_vcpu *vcpu) { -struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; +struct x86_exception *ex = &vcpu->arch.emulate_ctxt->exception; -if (ctxt->exception.vector ==PF_VECTOR ) -kvm_inject_emulated_page_fault (vcpu, &ctxt->exception); -else if (ctxt->exception.error_code_valid) -kvm_queue_exception_e (vcpu, ctxt->exception.vector, - ctxt->exception.error_code); +if (ex->vector ==PF_VECTOR ) +kvm_inject_emulated_page_fault (vcpu, ex); +else if (ex->error_code_valid) +kvm_queue_exception_e (vcpu, ex->vector, ex->error_code);else -kvm_queue_exception (vcpu, ctxt->exception.vector); +kvm_queue_exception (vcpu, ex->vector); }static struct x86_emulate_ctxt *alloc_emulate_ctxt (struct kvm_vcpu *vcpu)
Signature · 1 line
2.31.1
PATCH v2 2/9 KVM: x86: Set guest DR6 by kvm_queue_exception_p() in instruction emulation
In reply to Hou WenlongRecord DR6 in emulate_db() and use kvm_queue_exception_p() to set DR6 instead of directly using kvm_set_dr6() in emulation, which keeps the handling of DR6 during #DB injection consistent with other code paths.
No functional change intended.
--- arch/x86/kvm/emulate.c | 14 ++++---------- arch/x86/kvm/kvm_emulate.h | 6 +++++- arch/x86/kvm/x86.c | 5 ++++- 3 files changed, 13 insertions(+), 12 deletions(-)
arch/x86/kvm/emulate.c +4 -10
@@ -540,8 +540,9 @@ static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,return X86EMUL_PROPAGATE_FAULT ; } -static int emulate_db (struct x86_emulate_ctxt *ctxt) +static int emulate_db (struct x86_emulate_ctxt *ctxt,unsigned long dr6) { + ctxt->exception.dr6 = dr6;return emulate_exception (ctxt,DB_VECTOR ,0 , false); }
@@ -3834,15 +3835,8 @@ static int check_dr_read(struct x86_emulate_ctxt *ctxt)if ((cr4 &X86_CR4_DE ) && (dr ==4 || dr ==5 ))return emulate_ud (ctxt); -if (ctxt->ops->get_dr (ctxt,7 ) &DR7_GD ) { -ulong dr6; - - dr6 = ctxt->ops->get_dr (ctxt,6 ); - dr6 &= ~DR_TRAP_BITS ; - dr6 |=DR6_BD |DR6_ACTIVE_LOW ; - ctxt->ops->set_dr (ctxt,6 , dr6); -return emulate_db (ctxt); - } +if (ctxt->ops->get_dr (ctxt,7 ) &DR7_GD ) +return emulate_db (ctxt,DR6_BD );return X86EMUL_CONTINUE ; }
arch/x86/kvm/kvm_emulate.h +5 -1
@@ -24,7 +24,11 @@ struct x86_exception {bool error_code_valid;u16 error_code;bool nested_page_fault; -u64 address;/* cr2 or nested page fault gpa */ +union { +u64 address;/* cr2 or nested page fault gpa */ +unsigned long dr6; +u64 payload; + };u8 async_page_fault;unsigned long exit_qualification; };
arch/x86/kvm/x86.c +4 -1
@@ -8925,7 +8925,9 @@ static void inject_emulated_exception(struct kvm_vcpu *vcpu) {struct x86_exception *ex = &vcpu->arch.emulate_ctxt->exception; -if (ex->vector ==PF_VECTOR ) +if (ex->vector ==DB_VECTOR ) +kvm_queue_exception_e (vcpu,DB_VECTOR , ex->dr6); +else if (ex->vector ==PF_VECTOR )kvm_inject_emulated_page_fault (vcpu, ex);else if (ex->error_code_valid)kvm_queue_exception_e (vcpu, ex->vector, ex->error_code);
@@ -8970,6 +8972,7 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) ctxt->interruptibility =0 ; ctxt->have_exception = false; ctxt->exception.vector =-1 ; + ctxt->exception.payload =0 ; ctxt->perm_ok = false;init_decode_cache (ctxt);
Signature · 1 line
2.31.1
Sean Christopherson
maintainerRe: [PATCH v2 2/9] KVM: x86: Set guest DR6 by kvm_queue_exception_p() in instruction emulation
Quoted 51 lines · Hou Wenlong
Record DR6 in
emulate_db()and usekvm_queue_exception_p()to set DR6 instead of directly usingkvm_set_dr6()in emulation, which keeps the handling of DR6 during #DB injection consistent with other code paths.No functional change intended.
Signed-off-by Hou Wenlong <[email protected]>--- arch/x86/kvm/emulate.c | 14 ++++---------- arch/x86/kvm/kvm_emulate.h | 6 +++++- arch/x86/kvm/x86.c | 5 ++++- 3 files changed, 13 insertions(+), 12 deletions(-)arch/x86/kvm/emulate.c +4
-10@@ -540,8 +540,9 @@ static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,return X86EMUL_PROPAGATE_FAULT ; } -static int emulate_db (struct x86_emulate_ctxt *ctxt) +static int emulate_db (struct x86_emulate_ctxt *ctxt,unsigned long dr6) { + ctxt->exception.dr6 = dr6;return emulate_exception (ctxt,DB_VECTOR ,0 , false); }@@ -3834,15 +3835,8 @@ static int check_dr_read(struct x86_emulate_ctxt *ctxt)if ((cr4 &X86_CR4_DE ) && (dr ==4 || dr ==5 ))return emulate_ud (ctxt); -if (ctxt->ops->get_dr (ctxt,7 ) &DR7_GD ) { -ulong dr6; - - dr6 = ctxt->ops->get_dr (ctxt,6 ); - dr6 &= ~DR_TRAP_BITS ; - dr6 |=DR6_BD |DR6_ACTIVE_LOW ; - ctxt->ops->set_dr (ctxt,6 , dr6); -return emulate_db (ctxt); - } +if (ctxt->ops->get_dr (ctxt,7 ) &DR7_GD ) +return emulate_db (ctxt,DR6_BD );return X86EMUL_CONTINUE ; }arch/x86/kvm/kvm_emulate.h +0
-0
arch/x86/kvm/kvm_emulate.h struct x86_exception { · line 30
Quoted 8 lines
arch/x86/kvm/kvm_emulate.h +4
-1@@ -24,7 +24,11 @@ struct x86_exception {bool error_code_valid;u16 error_code;bool nested_page_fault; -u64 address;/* cr2 or nested page fault gpa */ +union { +u64 address;/* cr2 or nested page fault gpa */ +unsigned long dr6; +u64 payload;
Please split the introduction of the union to a separate patch, mainly so that the effectively zeroing of ctxt.exception.address in init_emulate_ctxt() is isolated, e.g. in case it somehow causes problems. But that will also allow introducing the inject_emulated_exception() change separately from the check_dr_read() change.
Quoted 9 lines
arch/x86/kvm/kvm_emulate.h +1
-0+ };u8 async_page_fault;unsigned long exit_qualification; };arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static void inject_emulated_exception(struct kvm_vcpu *vcpu) · line 8929
Quoted 6 lines
arch/x86/kvm/x86.c +2
-1@@ -8925,7 +8925,9 @@ static void inject_emulated_exception(struct kvm_vcpu *vcpu) {struct x86_exception *ex = &vcpu->arch.emulate_ctxt->exception; -if (ex->vector ==PF_VECTOR ) +if (ex->vector ==DB_VECTOR ) +kvm_queue_exception_e (vcpu,DB_VECTOR , ex->dr6);
This should be kvm_queue_exception_p(). I also think pivoting on DB_VECTOR is the wrong approach. Rather than key off the vector, add payload_valid (to match error_code_valid), and then do:
struct x86_exception *ex = &vcpu->arch.emulate_ctxt->exception; WARN_ON_ONCE(ex->vector != PF_VECTOR && ex->payload_valid && ex->error_code_valid); if (ex->vector == PF_VECTOR) kvm_inject_emulated_page_fault(vcpu, ex); else if (ex->payload_valid) kvm_queue_exception_p(vcpu, DB_VECTOR, ex->payload); else if (ex->error_code_valid) kvm_queue_exception_e(vcpu, ex->vector, ex->error_code); else kvm_queue_exception(vcpu, ex->vector);
PF_VECTOR is special because it has both an error code and a payload, and because it needs additional handling on multiple fronts.
Quoted 15 lines
arch/x86/kvm/x86.c +2
-0+else if (ex->vector ==PF_VECTOR )kvm_inject_emulated_page_fault (vcpu, ex);else if (ex->error_code_valid)kvm_queue_exception_e (vcpu, ex->vector, ex->error_code);@@ -8970,6 +8972,7 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) ctxt->interruptibility =0 ; ctxt->have_exception = false; ctxt->exception.vector =-1 ; + ctxt->exception.payload =0 ; ctxt->perm_ok = false;init_decode_cache (ctxt);Signature · 1 line
2.31.1
Sean Christopherson
maintainerRe: [PATCH v2 2/9] KVM: x86: Set guest DR6 by kvm_queue_exception_p() in instruction emulation
Quoted 9 lines · Sean Christopherson
Quoted 8 lines · Hou Wenlong
arch/x86/kvm/x86.c +0
-0@@ -8925,7 +8925,9 @@ static void inject_emulated_exception(struct kvm_vcpu *vcpu) {struct x86_exception *ex = &vcpu->arch.emulate_ctxt->exception;
Quoted 6 lines
arch/x86/kvm/x86.c static void inject_emulated_exception(struct kvm_vcpu *vcpu) · line 8929
Quoted 3 lines
arch/x86/kvm/x86.c +2
-1-if (ex->vector ==PF_VECTOR ) +if (ex->vector ==DB_VECTOR ) +kvm_queue_exception_e (vcpu,DB_VECTOR , ex->dr6);This should be
kvm_queue_exception_p(). I also think pivoting onDB_VECTORis the wrong approach.
Gah, never mind, didn't look at the next patch.
Sean Christopherson
maintainerRe: [PATCH v2 2/9] KVM: x86: Set guest DR6 by kvm_queue_exception_p() in instruction emulation
Quoted 10 lines · Sean Christopherson
Quoted 9 lines · Sean Christopherson
Quoted 8 lines · Hou Wenlong
arch/x86/kvm/x86.c +0
-0@@ -8925,7 +8925,9 @@ static void inject_emulated_exception(struct kvm_vcpu *vcpu) {struct x86_exception *ex = &vcpu->arch.emulate_ctxt->exception;
Quoted 8 lines
Quoted 6 lines
arch/x86/kvm/x86.c static void inject_emulated_exception(struct kvm_vcpu *vcpu) · line 8929
Quoted 3 lines
arch/x86/kvm/x86.c +2
-1-if (ex->vector ==PF_VECTOR ) +if (ex->vector ==DB_VECTOR ) +kvm_queue_exception_e (vcpu,DB_VECTOR , ex->dr6);This should be
kvm_queue_exception_p(). I also think pivoting onDB_VECTORis the wrong approach.Gah, never mind, didn't look at the next patch.
Actually, that's a good excuse to provide kvm_inject_emulated_db() in this patch, even though it doesn't become truly necessary until the next patch. Eliminating some of the code movement in the next patch will yield a smaller diff, and make it easier to see that there's change in the !KVM_GUESTDBG_USE_HW_BP case.
@@ -8976,23 +8998,36 @@ static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
}
}
-static void kvm_inject_emulated_db(struct kvm_vcpu *vcpu, unsigned long dr6) +static int kvm_inject_emulated_db(struct kvm_vcpu *vcpu, unsigned long dr6)
{
+ struct kvm_run *kvm_run = vcpu->run;
+
+ if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
+ kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW;
+ kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu);
+ kvm_run->debug.arch.exception = DB_VECTOR;
+ kvm_run->exit_reason = KVM_EXIT_DEBUG;
+ return 0;
+ }
+
kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);+ return 1;
}
-static void inject_emulated_exception(struct kvm_vcpu *vcpu) +static int inject_emulated_exception(struct kvm_vcpu *vcpu)
{
struct x86_exception *ex = &vcpu->arch.emulate_ctxt->exception;
if (ex->vector == DB_VECTOR)- kvm_inject_emulated_db(vcpu, ex->dr6);
- else if (ex->vector == PF_VECTOR)
+ return kvm_inject_emulated_db(vcpu, ex->dr6);
+
+ if (ex->vector == PF_VECTOR)
kvm_inject_emulated_page_fault(vcpu, ex);
else if (ex->error_code_valid)
kvm_queue_exception_e(vcpu, ex->vector, ex->error_code);
else
kvm_queue_exception(vcpu, ex->vector);+ return 1;
}
static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu)
PATCH v2 3/9 KVM: x86: Check guest debug in DR access instruction emulation
In reply to Hou WenlongWhen a DR access instruction is emulated by the x86 instruction emulator, only the guest DR7.GD is checked. Since the instruction emulation path has already performed some guest debug checks, add a guest debug check in the DR access instruction emulation to improve the guest debug logic in the instruction emulator.
--- arch/x86/kvm/emulate.c | 2 +- arch/x86/kvm/kvm_emulate.h | 1 + arch/x86/kvm/x86.c | 52 +++++++++++++++++++++++++++++++++----- arch/x86/kvm/x86.h | 7 +++++ 4 files changed, 54 insertions(+), 8 deletions(-)
arch/x86/kvm/emulate.c +1 -1
@@ -3835,7 +3835,7 @@ static int check_dr_read(struct x86_emulate_ctxt *ctxt)if ((cr4 &X86_CR4_DE ) && (dr ==4 || dr ==5 ))return emulate_ud (ctxt); -if (ctxt->ops->get_dr (ctxt,7 ) &DR7_GD ) +if (ctxt->ops->get_eff_dr (ctxt,7 ) &DR7_GD )return emulate_db (ctxt,DR6_BD );return X86EMUL_CONTINUE ;
arch/x86/kvm/kvm_emulate.h +1 -0
@@ -216,6 +216,7 @@ struct x86_emulate_ops {int (*set_cr)(struct x86_emulate_ctxt *ctxt,int cr,ulong val);int (*cpl)(struct x86_emulate_ctxt *ctxt);ulong (*get_dr)(struct x86_emulate_ctxt *ctxt,int dr); +ulong (*get_eff_dr)(struct x86_emulate_ctxt *ctxt,int dr);int (*set_dr)(struct x86_emulate_ctxt *ctxt,int dr,ulong value);int (*set_msr_with_filter)(struct x86_emulate_ctxt *ctxt,u32 msr_index,u64 data);int (*get_msr_with_filter)(struct x86_emulate_ctxt *ctxt,u32 msr_index,u64 *pdata);
arch/x86/kvm/x86.c +45 -7
97 lines not on this page · raw message
arch/x86/kvm/x86.h +7 -0
@@ -617,6 +617,13 @@ static inline bool kvm_dr6_valid(u64 data)return !(data >>32 ); } +static inline unsigned long kvm_get_eff_dr7 (struct kvm_vcpu *vcpu) +{ +if (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ) +return vcpu->arch.guest_debug_dr7; +return vcpu->arch.dr7; +} +/* * Trigger machine check on the host. We assume all the MSRs are already set up * by the CPU and that we still run on the same CPU as the MCE occurred on.
Signature · 1 line
2.31.1
PATCH v2 4/9 KVM: x86: Only check effective code breakpoint in emulation
In reply to Hou WenlongWhen guest debug is enabled, the effective breakpoints are controlled by guest debug rather than by the guest itself. Therefore, only check the code breakpoints of guest debug in emulation if guest debug is enabled, in order to maintain consistency with hardware behavior.
--- arch/x86/kvm/x86.c | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-)
arch/x86/kvm/x86.c +10 -25
@@ -9272,6 +9272,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_skip_emulated_instruction);static bool kvm_is_code_breakpoint_inhibited (struct kvm_vcpu *vcpu) { +if (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ) +return false; +if (kvm_get_rflags (vcpu) &X86_EFLAGS_RF )return true;
@@ -9288,6 +9291,8 @@ static bool kvm_is_code_breakpoint_inhibited(struct kvm_vcpu *vcpu)static bool kvm_vcpu_check_code_breakpoint (struct kvm_vcpu *vcpu,int emulation_type,int *r) { +unsigned long dr7 =kvm_get_eff_dr7 (vcpu); +WARN_ON_ONCE (emulation_type &EMULTYPE_NO_DECODE );/*
@@ -9308,34 +9313,14 @@ static bool kvm_vcpu_check_code_breakpoint(struct kvm_vcpu *vcpu, EMULTYPE_TRAP_UD | EMULTYPE_VMWARE_GP | EMULTYPE_PF))return false; -if (unlikely (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ) && - (vcpu->arch.guest_debug_dr7 &DR7_BP_EN_MASK )) { -struct kvm_run *kvm_run = vcpu->run; -unsigned long eip =kvm_get_linear_rip (vcpu); -u32 dr6 =kvm_vcpu_check_hw_bp (eip,0 , - vcpu->arch.guest_debug_dr7, - vcpu->arch.eff_db); - -if (dr6 !=0 ) { - kvm_run->debug.arch.dr6 = dr6 |DR6_ACTIVE_LOW ; - kvm_run->debug.arch.pc = eip; - kvm_run->debug.arch.exception =DB_VECTOR ; - kvm_run->exit_reason =KVM_EXIT_DEBUG ; - *r =0 ; -return true; - } - } - -if (unlikely (vcpu->arch.dr7 &DR7_BP_EN_MASK ) && +if (unlikely (dr7 &DR7_BP_EN_MASK ) && !kvm_is_code_breakpoint_inhibited (vcpu)) {unsigned long eip =kvm_get_linear_rip (vcpu); -u32 dr6 =kvm_vcpu_check_hw_bp (eip,0 , - vcpu->arch.dr7, - vcpu->arch.db); +u32 dr6 =kvm_vcpu_check_hw_bp (eip,0 , dr7, + vcpu->arch.eff_db); -if (dr6 !=0 ) { -kvm_queue_exception_p (vcpu,DB_VECTOR , dr6); - *r =1 ; +if (dr6) { + *r =kvm_inject_emulated_db (vcpu, dr6);return true; } }
Signature · 1 line
2.31.1
PATCH v2 5/9 KVM: x86: Consolidate KVM_GUESTDBG_SINGLESTEP check into the kvm_inject_emulated_db()
In reply to Hou WenlongUse kvm_inject_emulated_db() in kvm_vcpu_do_singlestep() to consolidate 'KVM_GUESTDBG_SINGLESTEP' check into kvm_inject_emulated_db() during emulation.
No functional change intended.
--- arch/x86/kvm/x86.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
arch/x86/kvm/x86.c +2 -12
@@ -8947,7 +8947,7 @@ static int kvm_inject_emulated_db(struct kvm_vcpu *vcpu, unsigned long dr6) {struct kvm_run *kvm_run = vcpu->run; -if (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ) { +if (vcpu->guest_debug & (KVM_GUESTDBG_USE_HW_BP |KVM_GUESTDBG_SINGLESTEP )) { kvm_run->debug.arch.dr6 = dr6 |DR6_ACTIVE_LOW ; kvm_run->debug.arch.pc =kvm_get_linear_rip (vcpu); kvm_run->debug.arch.exception =DB_VECTOR ;
@@ -9232,17 +9232,7 @@ static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,static int kvm_vcpu_do_singlestep (struct kvm_vcpu *vcpu) { -struct kvm_run *kvm_run = vcpu->run; - -if (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP ) { - kvm_run->debug.arch.dr6 =DR6_BS |DR6_ACTIVE_LOW ; - kvm_run->debug.arch.pc =kvm_get_linear_rip (vcpu); - kvm_run->debug.arch.exception =DB_VECTOR ; - kvm_run->exit_reason =KVM_EXIT_DEBUG ; -return 0 ; - } -kvm_queue_exception_p (vcpu,DB_VECTOR ,DR6_BS ); -return 1 ; +return kvm_inject_emulated_db (vcpu,DR6_BS ); }int kvm_skip_emulated_instruction (struct kvm_vcpu *vcpu)
Signature · 1 line
2.31.1
PATCH v2 6/9 KVM: x86: Move kvm_set_rflags() up before kvm_vcpu_do_singlestep()
In reply to Hou WenlongThe single-step trap #DB should be injected after the instruction completes, as 'ctxt->tf' already records the old value of 'X86_EFLAGS_TF'. Therefore, it's okay to move kvm_set_rflags() up before kvm_vcpu_do_single_step() to align it more closely with hardware behavior.
No functional change intended.
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -9593,10 +9593,10 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,if (ctxt->is_branch)kvm_pmu_branch_retired (vcpu);kvm_rip_write (vcpu, ctxt->eip); +__kvm_set_rflags (vcpu, ctxt->eflags);if (r && (ctxt->tf || (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP ))) r =kvm_vcpu_do_singlestep (vcpu);kvm_x86_call (update_emulated_instruction)(vcpu); -__kvm_set_rflags (vcpu, ctxt->eflags); }/*
Signature · 1 line
2.31.1
PATCH v2 7/9 KVM: VMX: Refresh 'PENDING_DBG_EXCEPTIONS.BS' bit during instruction emulation
In reply to Hou WenlongThe VM-Entry has a consistency check that when the 'STI' or 'MOVSS' blocking is active, the 'PENDING_DBG_EXCEPTIONS.BS' bit must be set if 'RFLAGS.TF' is set; otherwise, a VM-Fail is triggered during VM-entry. However, when 'STI' or 'MOV SS' is emulated (e.g., using the 'force emulation' prefix), the emulator only refreshes interruptibility state but pending debug exception state is not refreshed. Since the force emulation prefix causes a VM-Exit due to #UD interception, which clears the 'PENDING_DBG_EXCEPTIONS' bits, the emulator should refresh the 'PENDING_DBG_EXCEPTIONS.BS' bit when the 'RFLAGS.TF' bit is set to ensure the success of VM-Entry.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/main.c | 9 +++++++++ arch/x86/kvm/vmx/vmx.c | 15 ++++++++++----- arch/x86/kvm/vmx/x86_ops.h | 1 + arch/x86/kvm/x86.c | 7 ++++++- 6 files changed, 28 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -50,6 +50,7 @@ KVM_X86_OP(get_gdt) KVM_X86_OP(set_gdt) KVM_X86_OP(sync_dirty_debug_regs) KVM_X86_OP(set_dr7) +KVM_X86_OP_OPTIONAL(refresh_pending_dbg_exceptions) KVM_X86_OP(cache_reg) KVM_X86_OP(get_rflags) KVM_X86_OP(set_rflags)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1763,6 +1763,7 @@ struct kvm_x86_ops {void (*set_gdt)(struct kvm_vcpu *vcpu,struct desc_ptr *dt);void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu);void (*set_dr7)(struct kvm_vcpu *vcpu,unsigned long value); +void (*refresh_pending_dbg_exceptions)(struct kvm_vcpu *vcpu);void (*cache_reg)(struct kvm_vcpu *vcpu,enum kvm_reg reg);unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);void (*set_rflags)(struct kvm_vcpu *vcpu,unsigned long rflags);
arch/x86/kvm/vmx/main.c +9 -0
@@ -465,6 +465,14 @@ static void vt_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)vmx_set_dr7 (vcpu, val); } +static void vt_refresh_pending_dbg_exceptions (struct kvm_vcpu *vcpu) +{ +if (WARN_ON_ONCE (is_td_vcpu (vcpu))) +return ; + +vmx_refresh_pending_dbg_exceptions (vcpu); +} +static void vt_sync_dirty_debug_regs (struct kvm_vcpu *vcpu) {/*
@@ -915,6 +923,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .get_gdt = vt_op(get_gdt), .set_gdt = vt_op(set_gdt), .set_dr7 = vt_op(set_dr7), + .refresh_pending_dbg_exceptions = vt_op(refresh_pending_dbg_exceptions), .sync_dirty_debug_regs = vt_op(sync_dirty_debug_regs), .cache_reg = vt_op(cache_reg), .get_rflags = vt_op(get_rflags),
arch/x86/kvm/vmx/vmx.c +10 -5
@@ -5431,11 +5431,8 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)*/ if (is_icebp (intr_info))WARN_ON (!skip_emulated_instruction (vcpu)); -else if ((vmx_get_rflags (vcpu) &X86_EFLAGS_TF ) && - (vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO ) & - (GUEST_INTR_STATE_STI |GUEST_INTR_STATE_MOV_SS ))) -vmcs_writel (GUEST_PENDING_DBG_EXCEPTIONS , -vmcs_readl (GUEST_PENDING_DBG_EXCEPTIONS ) |DR6_BS ); +else +vmx_refresh_pending_dbg_exceptions (vcpu);kvm_queue_exception_p (vcpu,DB_VECTOR , dr6);return 1 ;
@@ -5742,6 +5739,14 @@ void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)vmcs_writel (GUEST_DR7 , val); } +void vmx_refresh_pending_dbg_exceptions (struct kvm_vcpu *vcpu) +{ +if ((vmx_get_rflags (vcpu) &X86_EFLAGS_TF ) && +vmx_get_interrupt_shadow (vcpu)) +vmcs_writel (GUEST_PENDING_DBG_EXCEPTIONS , +vmcs_readl (GUEST_PENDING_DBG_EXCEPTIONS ) |DR6_BS ); +} +static int handle_tpr_below_threshold (struct kvm_vcpu *vcpu) {kvm_apic_update_ppr (vcpu);
arch/x86/kvm/vmx/x86_ops.h +1 -0
@@ -74,6 +74,7 @@ void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt);void vmx_get_gdt (struct kvm_vcpu *vcpu,struct desc_ptr *dt);void vmx_set_gdt (struct kvm_vcpu *vcpu,struct desc_ptr *dt);void vmx_set_dr7 (struct kvm_vcpu *vcpu,unsigned long val); +void vmx_refresh_pending_dbg_exceptions (struct kvm_vcpu *vcpu);void vmx_sync_dirty_debug_regs (struct kvm_vcpu *vcpu);void vmx_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg);unsigned long vmx_get_rflags (struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +6 -1
@@ -9232,7 +9232,12 @@ static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,static int kvm_vcpu_do_singlestep (struct kvm_vcpu *vcpu) { -return kvm_inject_emulated_db (vcpu,DR6_BS ); +int r; + + r =kvm_inject_emulated_db (vcpu,DR6_BS ); +if (r) +kvm_x86_call (refresh_pending_dbg_exceptions)(vcpu); +return r; }int kvm_skip_emulated_instruction (struct kvm_vcpu *vcpu)
Signature · 1 line
2.31.1
Sean Christopherson
maintainerRe: [PATCH v2 7/9] KVM: VMX: Refresh 'PENDING_DBG_EXCEPTIONS.BS' bit during instruction emulation
Quoted 10 lines · Hou Wenlong
The VM-Entry has a consistency check that when the 'STI' or 'MOVSS' blocking is active, the 'PENDING_DBG_EXCEPTIONS.BS' bit must be set if 'RFLAGS.TF' is set; otherwise, a VM-Fail is triggered during VM-entry. However, when 'STI' or 'MOV SS' is emulated (e.g., using the 'force emulation' prefix), the emulator only refreshes interruptibility state but pending debug exception state is not refreshed. Since the force emulation prefix causes a VM-Exit due to #UD interception, which clears the '
PENDING_DBG_EXCEPTIONS' bits, the emulator should refresh the 'PENDING_DBG_EXCEPTIONS.BS' bit when the 'RFLAGS.TF' bit is set to ensure the success of VM-Entry.
After (way too) much thought, it's not just (forced) emulation that's flawed; save/restore also needs similar treatment. E.g. if userspace gains control of the vCPU on a single-step #DB exit with STI-blocking, then KVM will save/restore the pending #DB, RFLAGS.TF, and STI-blocking, but not PENDING_DBG_EXCEPTIONS.BS. When the target resumes, VM-Entry will fail due to the "bad" state.
So AFAICT, we can handle both by moving the fixup logic to vmx_inject_exception() (because it's just fixup for a consistency check, e.g. the state doesn't need to be saved/restored). I'm not entirely convinced this fixes *all* the flows that can run afoul of the consistency check, but I think it gets the legimiate ones? And if not, we can always continue playing whack-a-mole...
I'll send a v3 of the whole series, because with this approach there's no need to commit RFLAGS before queuing the #DB in the emulator writeback path, and we can drop kvm_vcpu_do_singlestep() entirely.
--- arch/x86/kvm/vmx/vmx.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)
arch/x86/kvm/vmx/vmx.c +18 -17
@@ -1909,6 +1909,24 @@ void vmx_inject_exception(struct kvm_vcpu *vcpu)u32 intr_info = ex->vector |INTR_INFO_VALID_MASK ;struct vcpu_vmx *vmx =to_vmx (vcpu); +/* +* When injecting a #DB, single-stepping is enabled in RFLAGS, and STI +* or MOV-SS blocking is active, set vmcs.PENDING_DBG_EXCEPTIONS.BS to +* prevent a false positive from VM-Entry consistency check. VM-Entry +* asserts that a single-step #DB _must_ be pending in this scenario, +* as the previous instruction cannot have toggled RFLAGS.TF 0=>1 +* (because STI and POP/MOV don't modify RFLAGS), therefore the one +* instruction delay when activating single-step breakpoints must have +* already expired. However, the CPU isn't smart enough to peek at +* vmcs.VM_ENTRY_INTR_INFO_FIELD and so doesn't realize that yes, there +* is indeed a #DB pending/imminent. +*/ +if (ex->vector ==DB_VECTOR && + (vmx_get_rflags (vcpu) &X86_EFLAGS_TF ) && +vmx_get_interrupt_shadow (vcpu)) +vmcs_writel (GUEST_PENDING_DBG_EXCEPTIONS , +vmcs_readl (GUEST_PENDING_DBG_EXCEPTIONS ) |DR6_BS ); +kvm_deliver_exception_payload (vcpu, ex);if (ex->has_error_code) {
@@ -5485,26 +5503,9 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)* avoid single-step #DB and MTF updates, as ICEBP is * higher priority. Note, skipping ICEBP still clears * STI and MOVSS blocking. -* -* For all other #DBs, set vmcs.PENDING_DBG_EXCEPTIONS.BS -* if single-step is enabled in RFLAGS and STI or MOVSS -* blocking is active, as the CPU doesn't set the bit -* on VM-Exit due to #DB interception. VM-Entry has a -* consistency check that a single-step #DB is pending -* in this scenario as the previous instruction cannot -* have toggled RFLAGS.TF 0=>1 (because STI and POP/MOV -* don't modify RFLAGS), therefore the one instruction -* delay when activating single-step breakpoints must -* have already expired. Note, the CPU sets/clears BS -* as appropriate for all other VM-Exits types. */ if (is_icebp (intr_info))WARN_ON (!skip_emulated_instruction (vcpu)); -else if ((vmx_get_rflags (vcpu) &X86_EFLAGS_TF ) && - (vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO ) & - (GUEST_INTR_STATE_STI |GUEST_INTR_STATE_MOV_SS ))) -vmcs_writel (GUEST_PENDING_DBG_EXCEPTIONS , -vmcs_readl (GUEST_PENDING_DBG_EXCEPTIONS ) |DR6_BS );kvm_queue_exception_p (vcpu,DB_VECTOR , dr6);return 1 ;
base-commit: b7fbe9a1bf9ee6c967ef77d366ca58c35fcf1887
Quoted 44 lines
(quoted diff) +8
-0+void vmx_refresh_pending_dbg_exceptions(struct kvm_vcpu *vcpu) +{ + if ((vmx_get_rflags(vcpu) & X86_EFLAGS_TF) && + vmx_get_interrupt_shadow(vcpu)) + vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, + vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS) | DR6_BS); +} + static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) { kvm_apic_update_ppr(vcpu);arch/x86/kvm/vmx/x86_ops.h +1
-0@@ -74,6 +74,7 @@ void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt);void vmx_get_gdt (struct kvm_vcpu *vcpu,struct desc_ptr *dt);void vmx_set_gdt (struct kvm_vcpu *vcpu,struct desc_ptr *dt);void vmx_set_dr7 (struct kvm_vcpu *vcpu,unsigned long val); +void vmx_refresh_pending_dbg_exceptions (struct kvm_vcpu *vcpu);void vmx_sync_dirty_debug_regs (struct kvm_vcpu *vcpu);void vmx_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg);unsigned long vmx_get_rflags (struct kvm_vcpu *vcpu);arch/x86/kvm/x86.c +6
-1@@ -9232,7 +9232,12 @@ static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,static int kvm_vcpu_do_singlestep (struct kvm_vcpu *vcpu) { -return kvm_inject_emulated_db (vcpu,DR6_BS ); +int r; + + r =kvm_inject_emulated_db (vcpu,DR6_BS ); +if (r) +kvm_x86_call (refresh_pending_dbg_exceptions)(vcpu); +return r; }int kvm_skip_emulated_instruction (struct kvm_vcpu *vcpu)Signature · 1 line
2.31.1
PATCH v2 8/9 KVM: selftests: Verify guest debug DR7.GD checking during instruction emulation
In reply to Hou WenlongSimilar to the global disable test case in x86's debug_regs test, use 'KVM_FEP' to trigger instruction emulation in order to verify the guest debug DR7.GD checking during instruction emulation.
--- tools/testing/selftests/kvm/x86/debug_regs.c | 23 +++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/x86/debug_regs.c +22 -1
@@ -19,6 +19,7 @@uint32_t guest_value;extern unsigned char sw_bp, hw_bp, write_data, ss_start, bd_start; +extern unsigned char fep_bd_start;static void guest_code (void ) {
@@ -64,6 +65,10 @@ static void guest_code(void)/* DR6.BD test */ asmvolatile ("bd_start: mov %%dr0, %%rax" : : :"rax" ); + +if (is_forced_emulation_enabled) + asmvolatile (KVM_FEP "fep_bd_start: mov %%dr0, %%rax" : : :"rax" ); +GUEST_DONE (); }
@@ -185,7 +190,7 @@ int main(void) target_dr6); } -/* Finally test global disable */ +/* test global disable */ memset (&debug,0 ,sizeof (debug)); debug.control =KVM_GUESTDBG_ENABLE |KVM_GUESTDBG_USE_HW_BP ; debug.arch.debugreg[7 ] =0x400 |DR7_GD ;
@@ -202,6 +207,22 @@ int main(void) run->debug.arch.pc, target_rip, run->debug.arch.dr6, target_dr6); +/* test global disable in emulation */ +if (is_forced_emulation_enabled) { +/* Skip the 3-bytes "mov dr0" */ +vcpu_skip_insn (vcpu,3 ); +vcpu_run (vcpu); +TEST_ASSERT (run->exit_reason ==KVM_EXIT_DEBUG && + run->debug.arch.exception ==DB_VECTOR && + run->debug.arch.pc ==CAST_TO_RIP (fep_bd_start) && + run->debug.arch.dr6 == target_dr6, +"DR7.GD: exit %d exception %d rip 0x%llx " +"(should be 0x%llx) dr6 0x%llx (should be 0x%llx)" , + run->exit_reason, run->debug.arch.exception, + run->debug.arch.pc, target_rip, run->debug.arch.dr6, + target_dr6); + } +/* Disable all debug controls, run to the end */ memset (&debug,0 ,sizeof (debug));vcpu_guest_debug_set (vcpu, &debug);
Signature · 1 line
2.31.1
PATCH v2 9/9 KVM: selftests: Verify 'BS' bit checking in pending debug exception state during VM-Entry
In reply to Hou WenlongIn the x86's debug_regs test, add a test case to cover the scenario where single-step with STI in VMX sets the 'BS' bit in pending debug exceptions state for #DB interception and instruction emulation in both cases.
--- .../selftests/kvm/include/x86/processor.h | 3 +- tools/testing/selftests/kvm/x86/debug_regs.c | 53 +++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +2 -1
@@ -36,7 +36,8 @@ extern uint64_t guest_tsc_khz;const char *ex_str (int vector); -#define X86_EFLAGS_FIXED (1u << 1) +#define X86_EFLAGS_FIXED (1u << 1) +#define X86_EFLAGS_TF (1u << 8)#define X86_CR4_VME (1ul << 0)#define X86_CR4_PVI (1ul << 1)
tools/testing/selftests/kvm/x86/debug_regs.c +49 -4
80 lines not on this page · raw message
--
2.31.1
Sean Christopherson
maintainerRe: [PATCH v2 9/9] KVM: selftests: Verify 'BS' bit checking in pending debug exception state during VM-Entry
Quoted 4 lines · Hou Wenlong
In the x86's
debug_regstest, add a test case to cover the scenario where single-step with STI in VMX sets the 'BS' bit in pending debug exceptions state for #DB interception and instruction emulation in both cases.
...
Quoted 10 lines
tools/testing/selftests/kvm/x86/debug_regs.c +10
-0+static void guest_db_handler (struct ex_regs *regs) +{ +static int count; +unsigned long target_rips[2 ] = { +CAST_TO_RIP (fep_sti_start), +CAST_TO_RIP (fep_sti_end), + }; + +__GUEST_ASSERT (regs->rip == target_rips[count],"STI: unexpected rip 0x%lx (should be 0x%lx)" , + regs->rip, target_rips[count]);
tools/testing/selftests/kvm/x86/debug_regs.c line 45
Quoted 10 lines
tools/testing/selftests/kvm/x86/debug_regs.c +10
-0+ regs->rflags &= ~X86_EFLAGS_TF ; + count++; +} + +static void guest_irq_handler (struct ex_regs *regs) +{ +unsigned long target_rip =CAST_TO_RIP (fep_bd_start); + +__GUEST_ASSERT (regs->rip == target_rip,"IRQ: unexpected rip 0x%lx (should be 0x%lx)" , + regs->rip, target_rip);
This is wrong, and the test fails with your series applied verbatim. The IRQ will arrive at fep_sti_start, because RFLAGS.IF=0 from the CLI at the end of thess_start block, and remains that way across fep_bd_start. And to make things
even more confusing, the IRQ arrives on the CLI even though it's in an STI shadow due to #DBs not being subjected to _STI_ shadows on Intel, only to MOV-SS/POP-SS shadows. I.e. the #DB lands on CLI, pushes RFLAGS.IF=1 on the stack, and then the subsequent IRET from guest_db_handler() fully unmasks IRQs, and voila.
Re: [PATCH v2 9/9] KVM: selftests: Verify 'BS' bit checking in pending debug exception state during VM-Entry
Quoted 29 lines · Sean Christopherson
Quoted 4 lines · Hou Wenlong
In the x86's
debug_regstest, add a test case to cover the scenario where single-step with STI in VMX sets the 'BS' bit in pending debug exceptions state for #DB interception and instruction emulation in both cases....
Quoted 10 lines
tools/testing/selftests/kvm/x86/debug_regs.c +10
-0+static void guest_db_handler (struct ex_regs *regs) +{ +static int count; +unsigned long target_rips[2 ] = { +CAST_TO_RIP (fep_sti_start), +CAST_TO_RIP (fep_sti_end), + }; + +__GUEST_ASSERT (regs->rip == target_rips[count],"STI: unexpected rip 0x%lx (should be 0x%lx)" , + regs->rip, target_rips[count]);tools/testing/selftests/kvm/x86/debug_regs.c line 45
Quoted 10 lines
tools/testing/selftests/kvm/x86/debug_regs.c +10
-0+ regs->rflags &= ~X86_EFLAGS_TF ; + count++; +} + +static void guest_irq_handler (struct ex_regs *regs) +{ +unsigned long target_rip =CAST_TO_RIP (fep_bd_start); + +__GUEST_ASSERT (regs->rip == target_rip,"IRQ: unexpected rip 0x%lx (should be 0x%lx)" , + regs->rip, target_rip);
Quoted 7 lines
This is wrong, and the test fails with your series applied verbatim. The IRQ will arrive at
fep_sti_start, because RFLAGS.IF=0 from the CLI at the end of thess_startblock, and remains that way acrossfep_bd_start. And to make things
even more confusing, the IRQ arrives on the CLI even though it's in an STI shadow due to #DBs not being subjected to _STI_ shadows on Intel, only to MOV-SS/POP-SS shadows. I.e. the #DB lands on CLI, pushes RFLAGS.IF=1 on the stack, and then the subsequent IRET fromguest_db_handler()fully unmasks IRQs, and voila.
Yes, the RIP here should be 'fep_sti_start'. This was a stupid mistake, and I’m very sorry about that. In my reply to v1[0] I explained why an IRQ handler is needed, so in v2 I added comments and an additional check, and I tested it before sending. However, it looks like the patch differs from what I had locally—my code management was a bit messy.
As you pointed out, this pending interrupt can make the test somewhat confusing. So it might be better to consume the IRQ before running the test, e.g.:
```
diff --git a/tools/testing/selftests/kvm/x86/debug_regs.c
b/tools/testing/selftests/kvm/x86/debug_regs.c
index 3ec2724697de..030d5cb58cc2 100644
--- a/tools/testing/selftests/kvm/x86/debug_regs.c
+++ b/tools/testing/selftests/kvm/x86/debug_regs.c
@@ -21,7 +21,7 @@
u32 guest_value;
extern unsigned char sw_bp, hw_bp, write_data, ss_start, bd_start;
-extern unsigned char fep_bd_start, fep_sti_start, fep_sti_end;
+extern unsigned char fep_irq, fep_bd_start, fep_sti_start, fep_sti_end;
static void guest_db_handler(struct ex_regs *regs)
{
@@ -39,7 +39,7 @@ static void guest_db_handler(struct ex_regs *regs)
static void guest_irq_handler(struct ex_regs *regs)
{
- unsigned long target_rip = CAST_TO_RIP(fep_bd_start);
+ unsigned long target_rip = CAST_TO_RIP(fep_irq);
__GUEST_ASSERT(regs->rip == target_rip, "IRQ: unexpected rip
0x%lx (should be 0x%lx)",
regs->rip, target_rip);
@@ -93,6 +93,14 @@ static void guest_code(void)
if (is_forced_emulation_enabled) {
asm volatile(KVM_FEP "fep_bd_start: mov %%dr0, %%rax" :
: : "rax");
+ /*
+ * Consume the pending interrupt, as the single-step #DB
delivery after
+ * STI removes the interrupt shadow, so the pending
interrupt will be
+ * delivered after guest #DB handling.
+ */
+ asm volatile("sti; nop\n\t"
+ "fep_irq:");
+
/* pending debug exceptions for emulation */
asm volatile("pushf\n\t"
"orq $" __stringify(X86_EFLAGS_TF) ",
(%rsp)\n\t"
@@ -264,13 +272,10 @@ int main(void)
memset(&debug, 0, sizeof(debug));
vcpu_guest_debug_set(vcpu, &debug);
- vm_install_exception_handler(vm, DB_VECTOR, guest_db_handler);
- /*
- * Install a dummy IRQ handler, as the single-step #DB delivery
after
- * STI removes the interrupt shadow, so the pending interrupt
will be
- * delivered after #DB handling.
- */
- vm_install_exception_handler(vm, IRQ_VECTOR, guest_irq_handler);
+ if (is_forced_emulation_enabled) {
+ vm_install_exception_handler(vm, DB_VECTOR,
guest_db_handler);
+ vm_install_exception_handler(vm, IRQ_VECTOR,
guest_irq_handler);
+ }
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
```I have tested it on both Intel and AMD. Please let me know if I should resend a v3 of this patch.
[0]: https://lore.kernel.org/kvm/[email protected]/
Thanks!
Sean Christopherson
maintainerRe: [PATCH v2 9/9] KVM: selftests: Verify 'BS' bit checking in pending debug exception state during VM-Entry
Quoted 22 lines · Hou Wenlong
Quoted 15 lines · Sean Christopherson
tools/testing/selftests/kvm/x86/debug_regs.c line 45
Quoted 6 lines · Hou Wenlong
tools/testing/selftests/kvm/x86/debug_regs.c +6
-0+static void guest_irq_handler (struct ex_regs *regs) +{ +unsigned long target_rip =CAST_TO_RIP (fep_bd_start); + +__GUEST_ASSERT (regs->rip == target_rip,"IRQ: unexpected rip 0x%lx (should be 0x%lx)" , + regs->rip, target_rip);This is wrong, and the test fails with your series applied verbatim. The IRQ will arrive at
fep_sti_start, because RFLAGS.IF=0 from the CLI at the end of thess_startblock, and remains that way acrossfep_bd_start. And to make things
even more confusing, the IRQ arrives on the CLI even though it's in an STI shadow due to #DBs not being subjected to _STI_ shadows on Intel, only to MOV-SS/POP-SS shadows. I.e. the #DB lands on CLI, pushes RFLAGS.IF=1 on the stack, and then the subsequent IRET fromguest_db_handler()fully unmasks IRQs, and voila.Yes, the RIP here should be '
fep_sti_start'. This was a stupid mistake, and I’m very sorry about that. In my reply to v1[0] I explained why an IRQ handler is needed, so in v2 I added comments and an additional check, and I tested it before sending. However, it looks like the patch differs from what I had locally—my code management was a bit messy.
...
Quoted 2 lines
I have tested it on both Intel and AMD. Please let me know if I should resend a v3 of this patch.
No need, I'm actually going to send a v3 of the entire series (beleated feedback on patch 6 incoming...).
Sean Christopherson
maintainerQuoted 17 lines · Hou Wenlong
Hou Wenlong (9):
KVM: x86: Capture "structx86_exception" in
inject_emulated_exception()
KVM: x86: Set guest DR6 bykvm_queue_exception_p()in instruction
emulation
KVM: x86: Check guest debug in DR access instruction emulation
KVM: x86: Only check effective code breakpoint in emulation
KVM: x86: ConsolidateKVM_GUESTDBG_SINGLESTEPcheck into the
kvm_inject_emulated_db()
KVM: x86: Movekvm_set_rflags()up beforekvm_vcpu_do_singlestep()
KVM: VMX: Refresh 'PENDING_DBG_EXCEPTIONS.BS' bit during instruction
emulation
KVM: selftests: Verify guest debug DR7.GD checking during instruction
emulation
KVM: selftests: Verify 'BS' bit checking in pending debug exception
during VM entry
Quoted 12 lines
arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/emulate.c | 14 +-- arch/x86/kvm/kvm_emulate.h | 7 +- arch/x86/kvm/vmx/main.c | 9 ++ arch/x86/kvm/vmx/vmx.c | 15 ++- arch/x86/kvm/vmx/x86_ops.h | 1 + arch/x86/kvm/x86.c | 116 ++++++++++-------- arch/x86/kvm/x86.h | 7 ++ .../selftests/kvm/include/x86/processor.h | 3 +- tools/testing/selftests/kvm/x86/debug_regs.c | 72 ++++++++++- 11 files changed, 178 insertions(+), 68 deletions(-)
One goof and some nits on patch 3, but I'll fix them up when applying, i.e. no need for a v3. I'll probably also add some comments and elaborate on some of the changelogs.