x86/debug: fix guest dr6 value for single stepping and HW breakpoints
cover x86/debug: fix guest dr6 value for single stepping and HW breakpoints
Xen has a bug where hardware breakpoint exceptions (DR_TRAP<n>) are erroneously recognized as single-stepping exceptions (DR_STEP). This interferes with userland debugging and allows (otherwise restricted) usermode programs to detect Xen HVM (or PVH). This patch series aim to fix this.
The last patch is optional and finishes incomplete plumbing work initiated by commit 21867648033d (x86/debug: Plumb pending_dbg through the monitor and devicemodel interfaces, 2018-05-31).
The following Linux x86-64 program demonstrates the bug:
--- (snip) ---
#include <stddef.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <stdio.h>
#define ABORT_ON_ERR(x) if ((x) == -1) abort();
int main(void)
{
unsigned long cur_rip, cur_eflags, cur_dr6;
int wstatus, exit_code;
pid_t pid;
ABORT_ON_ERR(pid = fork());
if (pid == 0) {
ABORT_ON_ERR(ptrace(PTRACE_TRACEME, 0, NULL, NULL));
ABORT_ON_ERR(raise(SIGSTOP));
_exit(0);
}
/* Wait for first ptrace event */
if (waitpid(pid, &wstatus, 0) != pid) abort();
if (!WIFSTOPPED(wstatus)) abort();
/* Obtain current RIP value and perform sanity check */
cur_rip = ptrace(PTRACE_PEEKUSER, pid, (void *)offsetof(struct user, regs.rip), &cur_rip);
cur_dr6 = ptrace(PTRACE_PEEKUSER, pid, (void *)offsetof(struct user, u_debugreg[6]), &cur_dr6);
assert(cur_dr6 == 0xffff0ff0UL);
/* Set up debug registers and set EFLAGS.TF */
cur_eflags = ptrace(PTRACE_PEEKUSER, pid, (void *)offsetof(struct user, regs.eflags), &cur_eflags);
ABORT_ON_ERR(ptrace(PTRACE_POKEUSER, pid, (void *)offsetof(struct user, regs.eflags), (void *)(cur_eflags | 0x100UL)));
ABORT_ON_ERR(ptrace(PTRACE_POKEUSER, pid, (void *)offsetof(struct user, u_debugreg[0]), (void *)cur_rip));
ABORT_ON_ERR(ptrace(PTRACE_POKEUSER, pid, (void *)offsetof(struct user, u_debugreg[7]), (void *)1L));
/* Continue execution to trigger hardware breakpoint */
ABORT_ON_ERR(ptrace(PTRACE_CONT, pid, NULL, (unsigned long)0));
if (waitpid(pid, &wstatus, 0) != pid) abort();
if (!(WIFSTOPPED(wstatus) && WSTOPSIG(wstatus) == SIGTRAP)) abort();
/* Detect if Xen has tampered with DR6 */
cur_dr6 = ptrace(PTRACE_PEEKUSER, pid, (void *)offsetof(struct user, u_debugreg[6]), &cur_dr6);
fprintf(stderr, "DR6 = 0x%08lx\n", cur_dr6);
if (cur_dr6 == 0xffff0ff1UL)
{
fputs("Running on bare-metal, Xen PV, or non-Xen VMM\n", stdout);
exit_code = EXIT_FAILURE;
}
else
{
fputs("Running on Xen HVM\n", stdout);
exit_code = EXIT_SUCCESS;
}
/* Tear down debug registers and unset EFLAGS.TF */
cur_eflags = ptrace(PTRACE_PEEKUSER, pid, (void *)offsetof(struct user, regs.eflags), &cur_eflags);
ABORT_ON_ERR(ptrace(PTRACE_POKEUSER, pid, (void *)offsetof(struct user, regs.eflags), (void *)(cur_eflags & ~0x100UL)));
ABORT_ON_ERR(ptrace(PTRACE_POKEUSER, pid, (void *)offsetof(struct user, u_debugreg[7]), (void *)0L));
/* Continue execution to let child process exit */
ABORT_ON_ERR(ptrace(PTRACE_CONT, pid, NULL, (unsigned long)0));
if (waitpid(pid, &wstatus, 0) != pid) abort();
if (!(WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 0)) abort();
return exit_code;
}--- (snip) ---
Jinoh Kang (6):
x86/hvm: only populate info->cr2 for #PF in hvm_get_pending_event()
x86emul: rename field 'cr2' of struct x86_event to 'extra'
x86: don't assume #DB is always caused by singlestep if EFLAGS.TF is
set
x86/pv: set DR_STEP if single-stepping after ro page fault emulation x86/pv: factor out single-step debug trap injection x86/debug: actually plumb pending_dbg through the monitor and
devicemodel interfaces
xen/arch/x86/hvm/dm.c | 2 +- xen/arch/x86/hvm/emulate.c | 3 +- xen/arch/x86/hvm/hvm.c | 11 ++++-- xen/arch/x86/hvm/svm/nestedsvm.c | 2 +- xen/arch/x86/hvm/svm/svm.c | 48 ++++++++++++++++++++------ xen/arch/x86/hvm/vmx/vmx.c | 40 ++++++++++++++------- xen/arch/x86/include/asm/domain.h | 14 +++++++- xen/arch/x86/include/asm/hvm/hvm.h | 14 +++++++- xen/arch/x86/mm/shadow/multi.c | 5 +-- xen/arch/x86/pv/emulate.c | 5 +-- xen/arch/x86/pv/ro-page-fault.c | 3 +- xen/arch/x86/pv/traps.c | 18 +++++++--- xen/arch/x86/x86_emulate/x86_emulate.h | 6 ++-- 13 files changed, 128 insertions(+), 43 deletions(-)
Signature · 1 line
2.41.0
PATCH 1/6 x86/hvm: only populate info->cr2 for #PF in hvm_get_pending_event()
Prepare for an upcoming patch that overloads the 'cr2' field for #DB.
--- xen/arch/x86/hvm/hvm.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/hvm.c +7 -2
@@ -502,9 +502,14 @@ void hvm_migrate_pirqs(struct vcpu *v)static bool hvm_get_pending_event (struct vcpu *v,struct x86_event *info) { - info->cr2 = v->arch.hvm.guest_cr[2 ]; +if ( !alternative_call (hvm_funcs.get_pending_event, v, info) ) +return false; + +if ( info->type ==X86_EVENTTYPE_HW_EXCEPTION && + info->vector ==X86_EXC_PF ) + info->cr2 = v->arch.hvm.guest_cr[2 ]; -return alternative_call (hvm_funcs.get_pending_event, v, info); +return true; }void hvm_do_resume (struct vcpu *v)
Signature · 1 line
2.41.0
PATCH 2/6 x86emul: rename field 'cr2' of struct x86_event to 'extra'
In reply to Jinoh KangXEN_DMOP_inject_event() copies the 'cr2' argument to struct x86_event. 'cr2' is overladed to mean pending_dbg for a debug trap, but consumers of struct x86_event always interpret it as CR2.
Clarify the role of the 'cr2' field by renaming it to 'extra', in preparation for an upcoming patch that uses it to actually populate dr6.
--- xen/arch/x86/hvm/dm.c | 2 +- xen/arch/x86/hvm/hvm.c | 4 ++-- xen/arch/x86/hvm/svm/nestedsvm.c | 2 +- xen/arch/x86/hvm/svm/svm.c | 8 ++++---- xen/arch/x86/hvm/vmx/vmx.c | 2 +- xen/arch/x86/include/asm/domain.h | 2 +- xen/arch/x86/include/asm/hvm/hvm.h | 2 +- xen/arch/x86/pv/traps.c | 8 ++++---- xen/arch/x86/x86_emulate/x86_emulate.h | 4 ++-- 9 files changed, 17 insertions(+), 17 deletions(-)
xen/arch/x86/hvm/dm.c +1 -1
@@ -314,7 +314,7 @@ static int inject_event(struct domain *d, v->arch.hvm.inject_event.type = data->type; v->arch.hvm.inject_event.insn_len = data->insn_len; v->arch.hvm.inject_event.error_code = data->error_code; - v->arch.hvm.inject_event.cr2 = data->cr2; + v->arch.hvm.inject_event.extra = data->cr2;smp_wmb (); v->arch.hvm.inject_event.vector = data->vector;
xen/arch/x86/hvm/hvm.c +2 -2
@@ -507,7 +507,7 @@ static bool hvm_get_pending_event(struct vcpu *v, struct x86_event *info)if ( info->type ==X86_EVENTTYPE_HW_EXCEPTION && info->vector ==X86_EXC_PF ) - info->cr2 = v->arch.hvm.guest_cr[2 ]; + info->extra = v->arch.hvm.guest_cr[2 ];return true; }
@@ -548,7 +548,7 @@ void hvm_do_resume(struct vcpu *v)if (hvm_get_pending_event (v, &info) ) {hvm_monitor_interrupt (info.vector, info.type, info.error_code, - info.cr2); + info.extra); v->arch.monitor.next_interrupt_enabled = false; } }
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
@@ -842,7 +842,7 @@ int cf_check nsvm_vcpu_vmexit_event(ASSERT (vcpu_nestedhvm (v).nv_vvmcx !=NULL );nestedsvm_vmexit_defer (v,VMEXIT_EXCEPTION_DE + event->vector, - event->error_code, event->cr2); + event->error_code, event->extra);return NESTEDHVM_VMEXIT_DONE ; }
xen/arch/x86/hvm/svm/svm.c +4 -4
@@ -1252,7 +1252,7 @@ static void svm_emul_swint_injection(struct x86_event *event) { fault =X86_EXC_PF ; ec = pfinfo.ec; - event->cr2 = pfinfo.linear; + event->extra = pfinfo.linear; } goto raise_exception;
@@ -1345,8 +1345,8 @@ static void cf_check svm_inject_event(const struct x86_event *event)case X86_EXC_PF :ASSERT (_event.type ==X86_EVENTTYPE_HW_EXCEPTION ); - curr->arch.hvm.guest_cr[2 ] = _event.cr2; -vmcb_set_cr2 (vmcb, _event.cr2); + curr->arch.hvm.guest_cr[2 ] = _event.extra; +vmcb_set_cr2 (vmcb, _event.extra);break ; }
@@ -1430,7 +1430,7 @@ static void cf_check svm_inject_event(const struct x86_event *event)if ( _event.vector ==X86_EXC_PF && _event.type ==X86_EVENTTYPE_HW_EXCEPTION )HVMTRACE_LONG_2D (PF_INJECT , _event.error_code, -TRC_PAR_LONG (_event.cr2)); +TRC_PAR_LONG (_event.extra));else HVMTRACE_2D (INJ_EXC , _event.vector, _event.error_code); }
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -2051,7 +2051,7 @@ static void cf_check vmx_inject_event(const struct x86_event *event)case X86_EXC_PF :ASSERT (_event.type ==X86_EVENTTYPE_HW_EXCEPTION ); - curr->arch.hvm.guest_cr[2 ] = _event.cr2; + curr->arch.hvm.guest_cr[2 ] = _event.extra;break ; }
xen/arch/x86/include/asm/domain.h +1 -1
@@ -735,7 +735,7 @@ static inline void pv_inject_page_fault(int errcode, unsigned long cr2) .vector = X86_EXC_PF, .type = X86_EVENTTYPE_HW_EXCEPTION, .error_code = errcode, - .cr2 = cr2, + .extra = cr2, };pv_inject_event (&event);
xen/arch/x86/include/asm/hvm/hvm.h +1 -1
@@ -532,7 +532,7 @@ static inline void hvm_inject_page_fault(int errcode, unsigned long cr2) .vector = X86_EXC_PF, .type = X86_EVENTTYPE_HW_EXCEPTION, .error_code = errcode, - .cr2 = cr2, + .extra = cr2, };hvm_inject_event (&event);
xen/arch/x86/pv/traps.c +4 -4
@@ -53,15 +53,15 @@ void pv_inject_event(const struct x86_event *event)if ( event->type ==X86_EVENTTYPE_HW_EXCEPTION && vector ==X86_EXC_PF ) { - curr->arch.pv.ctrlreg[2 ] = event->cr2; -arch_set_cr2 (curr, event->cr2); + curr->arch.pv.ctrlreg[2 ] = event->extra; +arch_set_cr2 (curr, event->extra);/* Re-set error_code.user flag appropriately for the guest. */ error_code &= ~PFEC_user_mode;if ( !guest_kernel_mode (curr, regs) ) error_code |= PFEC_user_mode; -trace_pv_page_fault (event->cr2, error_code); +trace_pv_page_fault (event->extra, error_code); }else trace_pv_trap (vector, regs->rip, use_error_code, error_code);
@@ -82,7 +82,7 @@ void pv_inject_event(const struct x86_event *event) vector, vector_name(vector), error_code);if ( vector ==X86_EXC_PF ) -show_page_walk (event->cr2); +show_page_walk (event->extra); } }
xen/arch/x86/x86_emulate/x86_emulate.h +2 -2
@@ -78,7 +78,7 @@ struct x86_event {uint8_t type;/* X86_EVENTTYPE_* */ uint8_t insn_len;/* Instruction length */ int32_t error_code;/* X86_EVENT_NO_EC if n/a */ -unsigned long cr2;/* Only for X86_EXC_PF h/w exception */ +unsigned long extra;/* CR2 if X86_EXC_PF h/w exception */ };/*
@@ -831,7 +831,7 @@ static inline void x86_emul_pagefault( ctxt->event.vector =X86_EXC_PF ; ctxt->event.type =X86_EVENTTYPE_HW_EXCEPTION ; ctxt->event.error_code = error_code; - ctxt->event.cr2 = cr2; + ctxt->event.extra = cr2; ctxt->event_pending = true; }
Signature · 1 line
2.41.0
PATCH 3/6 x86: don't assume #DB is always caused by singlestep if EFLAGS.TF is set
In reply to Jinoh KangToday, when a HVM (or PVH) guest triggers a hardware breakpoint while EFLAGS.TF is set, Xen incorrectly assumes that this is a single stepping exception and sets DR_STEP in dr6 in addition to DR_TRAP<n>.
This causes problems with Linux HW breakpoint handler, which ignores DR_TRAP<n> bits when DR_STEP is set. This prevents user-mode debuggers from recognizing hardware breakpoints if EFLAGS.TF is set.
Fix this by not setting DR_STEP in {vmx,svm}_inject_event, unless the emulator explicitly signals the single-stepping mode via the newly added "singlestep" boolean field of struct x86_event.
--- xen/arch/x86/hvm/emulate.c | 3 ++- xen/arch/x86/hvm/svm/svm.c | 6 +++--- xen/arch/x86/hvm/vmx/vmx.c | 6 +++--- xen/arch/x86/include/asm/hvm/hvm.h | 12 ++++++++++++ xen/arch/x86/mm/shadow/multi.c | 5 +++-- xen/arch/x86/x86_emulate/x86_emulate.h | 4 +++- 6 files changed, 26 insertions(+), 10 deletions(-)
xen/arch/x86/hvm/emulate.c +2 -1
@@ -26,6 +26,7 @@#include <asm/hvm/support.h> #include <asm/iocap.h> #include <asm/vm_event.h> +#include <asm/debugreg.h> struct hvmemul_cache {
@@ -2673,7 +2674,7 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt, }if ( hvmemul_ctxt->ctxt.retire.singlestep ) -hvm_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +hvm_inject_debug_exception (DR_STEP ); new_intr_shadow = hvmemul_ctxt->intr_shadow;
xen/arch/x86/hvm/svm/svm.c +3 -3
@@ -96,7 +96,7 @@ void __update_guest_eip(struct cpu_user_regs *regs, unsigned int inst_len) curr->arch.hvm.svm.vmcb->int_stat.intr_shadow =0 ;if ( regs->eflags &X86_EFLAGS_TF ) -hvm_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +hvm_inject_debug_exception (DR_STEP ); } static void cf_check svm_cpu_down(void)
@@ -1328,10 +1328,10 @@ static void cf_check svm_inject_event(const struct x86_event *event)switch ( _event.vector | -(_event.type ==X86_EVENTTYPE_SW_INTERRUPT ) ) {case X86_EXC_DB : -if ( regs->eflags &X86_EFLAGS_TF ) +if ( event->extra ) {__restore_debug_registers (vmcb, curr); -vmcb_set_dr6 (vmcb,vmcb_get_dr6 (vmcb) |DR_STEP ); +vmcb_set_dr6 (vmcb,vmcb_get_dr6 (vmcb) | event->extra); }/* fall through */ case X86_EXC_BP :
xen/arch/x86/hvm/vmx/vmx.c +3 -3
@@ -2022,10 +2022,10 @@ static void cf_check vmx_inject_event(const struct x86_event *event)switch ( _event.vector | -(_event.type ==X86_EVENTTYPE_SW_INTERRUPT ) ) {case X86_EXC_DB : -if (guest_cpu_user_regs ()->eflags &X86_EFLAGS_TF ) +if ( event->extra ) {__restore_debug_registers (curr); -write_debugreg (6 ,read_debugreg (6 ) |DR_STEP ); +write_debugreg (6 ,read_debugreg (6 ) | event->extra); }if ( !nestedhvm_vcpu_in_guestmode (curr) || !nvmx_intercepts_exception (curr,X86_EXC_DB , _event.error_code) )
@@ -3068,7 +3068,7 @@ void update_guest_eip(void) }if ( regs->eflags &X86_EFLAGS_TF ) -hvm_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +hvm_inject_debug_exception (DR_STEP ); } static void cf_check vmx_fpu_dirty_intercept(void)
xen/arch/x86/include/asm/hvm/hvm.h +12 -0
@@ -538,6 +538,18 @@ static inline void hvm_inject_page_fault(int errcode, unsigned long cr2)hvm_inject_event (&event); } +static inline void hvm_inject_debug_exception (unsigned long pending_dbg) +{ +struct x86_event event = { + .vector =X86_EXC_DB , + .type =X86_EVENTTYPE_HW_EXCEPTION , + .error_code =X86_EVENT_NO_EC , + .extra = pending_dbg, + }; + +hvm_inject_event (&event); +} +static inline bool hvm_event_pending (const struct vcpu *v) {return alternative_call (hvm_funcs.event_pending, v);
xen/arch/x86/mm/shadow/multi.c +3 -2
@@ -24,6 +24,7 @@#include <asm/hvm/cacheattr.h> #include <asm/mtrr.h> #include <asm/guest_pt.h> +#include <asm/debugreg.h> #include <public/sched.h> #include "private.h" #include "types.h"
@@ -2788,7 +2789,7 @@ static int cf_check sh_page_fault(#endif if ( emul_ctxt.ctxt.retire.singlestep ) -hvm_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +hvm_inject_debug_exception (DR_STEP );#if GUEST_PAGING_LEVELS ==3 /* PAE guest */ /*
@@ -2829,7 +2830,7 @@ static int cf_check sh_page_fault(TRACE_SHADOW_PATH_FLAG (TRCE_SFLAG_EMULATION_LAST_FAILED );if ( emul_ctxt.ctxt.retire.singlestep ) -hvm_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +hvm_inject_debug_exception (DR_STEP );break ;/* Don't emulate again if we failed! */ }
xen/arch/x86/x86_emulate/x86_emulate.h +3 -1
@@ -78,7 +78,9 @@ struct x86_event {uint8_t type;/* X86_EVENTTYPE_* */ uint8_t insn_len;/* Instruction length */ int32_t error_code;/* X86_EVENT_NO_EC if n/a */ -unsigned long extra;/* CR2 if X86_EXC_PF h/w exception */ + +/* Type-specific extra data (%cr2 for #PF, pending_dbg for #DB) */ +unsigned long extra; };/*
Signature · 1 line
2.41.0
Re: [PATCH 3/6] x86: don't assume #DB is always caused by singlestep if EFLAGS.TF is set
Quoted 11 lines · Jinoh Kang
Today, when a HVM (or PVH) guest triggers a hardware breakpoint while EFLAGS.TF is set, Xen incorrectly assumes that this is a single stepping exception and sets
DR_STEPin dr6 in addition to DR_TRAP<n>.This causes problems with Linux HW breakpoint handler, which ignores DR_TRAP<n> bits when
DR_STEPis set. This prevents user-mode debuggers from recognizing hardware breakpoints if EFLAGS.TF is set.Fix this by not setting
DR_STEPin {vmx,svm}_inject_event, unless the emulator explicitly signals the single-stepping mode via the newly added "singlestep" boolean field of structx86_event.
s/the newly added "singlestep" boolean field/the 'extra' field/. oops.
Quoted 153 lines
Fixes 8b831f4189 ("x86: single step after instruction emulation")Signed-off-by Jinoh Kang <[email protected]>--- xen/arch/x86/hvm/emulate.c | 3 ++- xen/arch/x86/hvm/svm/svm.c | 6 +++--- xen/arch/x86/hvm/vmx/vmx.c | 6 +++--- xen/arch/x86/include/asm/hvm/hvm.h | 12 ++++++++++++ xen/arch/x86/mm/shadow/multi.c | 5 +++-- xen/arch/x86/x86_emulate/x86_emulate.h | 4 +++- 6 files changed, 26 insertions(+), 10 deletions(-)xen/arch/x86/hvm/emulate.c +2
-1@@ -26,6 +26,7 @@#include <asm/hvm/support.h> #include <asm/iocap.h> #include <asm/vm_event.h> +#include <asm/debugreg.h> struct hvmemul_cache {@@ -2673,7 +2674,7 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt, }if ( hvmemul_ctxt->ctxt.retire.singlestep ) -hvm_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +hvm_inject_debug_exception (DR_STEP ); new_intr_shadow = hvmemul_ctxt->intr_shadow;xen/arch/x86/hvm/svm/svm.c +3
-3@@ -96,7 +96,7 @@ void __update_guest_eip(struct cpu_user_regs *regs, unsigned int inst_len) curr->arch.hvm.svm.vmcb->int_stat.intr_shadow =0 ;if ( regs->eflags &X86_EFLAGS_TF ) -hvm_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +hvm_inject_debug_exception (DR_STEP ); } static void cf_check svm_cpu_down(void)@@ -1328,10 +1328,10 @@ static void cf_check svm_inject_event(const struct x86_event *event)switch ( _event.vector | -(_event.type ==X86_EVENTTYPE_SW_INTERRUPT ) ) {case X86_EXC_DB : -if ( regs->eflags &X86_EFLAGS_TF ) +if ( event->extra ) {__restore_debug_registers (vmcb, curr); -vmcb_set_dr6 (vmcb,vmcb_get_dr6 (vmcb) |DR_STEP ); +vmcb_set_dr6 (vmcb,vmcb_get_dr6 (vmcb) | event->extra); }/* fall through */ case X86_EXC_BP :xen/arch/x86/hvm/vmx/vmx.c +3
-3@@ -2022,10 +2022,10 @@ static void cf_check vmx_inject_event(const struct x86_event *event)switch ( _event.vector | -(_event.type ==X86_EVENTTYPE_SW_INTERRUPT ) ) {case X86_EXC_DB : -if (guest_cpu_user_regs ()->eflags &X86_EFLAGS_TF ) +if ( event->extra ) {__restore_debug_registers (curr); -write_debugreg (6 ,read_debugreg (6 ) |DR_STEP ); +write_debugreg (6 ,read_debugreg (6 ) | event->extra); }if ( !nestedhvm_vcpu_in_guestmode (curr) || !nvmx_intercepts_exception (curr,X86_EXC_DB , _event.error_code) )@@ -3068,7 +3068,7 @@ void update_guest_eip(void) }if ( regs->eflags &X86_EFLAGS_TF ) -hvm_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +hvm_inject_debug_exception (DR_STEP ); } static void cf_check vmx_fpu_dirty_intercept(void)xen/arch/x86/include/asm/hvm/hvm.h +12
-0@@ -538,6 +538,18 @@ static inline void hvm_inject_page_fault(int errcode, unsigned long cr2)hvm_inject_event (&event); } +static inline void hvm_inject_debug_exception (unsigned long pending_dbg) +{ +struct x86_event event = { + .vector =X86_EXC_DB , + .type =X86_EVENTTYPE_HW_EXCEPTION , + .error_code =X86_EVENT_NO_EC , + .extra = pending_dbg, + }; + +hvm_inject_event (&event); +} +static inline bool hvm_event_pending (const struct vcpu *v) {return alternative_call (hvm_funcs.event_pending, v);xen/arch/x86/mm/shadow/multi.c +3
-2@@ -24,6 +24,7 @@#include <asm/hvm/cacheattr.h> #include <asm/mtrr.h> #include <asm/guest_pt.h> +#include <asm/debugreg.h> #include <public/sched.h> #include "private.h" #include "types.h" @@ -2788,7 +2789,7 @@ static int cf_check sh_page_fault(#endif if ( emul_ctxt.ctxt.retire.singlestep ) -hvm_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +hvm_inject_debug_exception (DR_STEP );#if GUEST_PAGING_LEVELS ==3 /* PAE guest */ /* @@ -2829,7 +2830,7 @@ static int cf_check sh_page_fault(TRACE_SHADOW_PATH_FLAG (TRCE_SFLAG_EMULATION_LAST_FAILED );if ( emul_ctxt.ctxt.retire.singlestep ) -hvm_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +hvm_inject_debug_exception (DR_STEP );break ;/* Don't emulate again if we failed! */ }xen/arch/x86/x86_emulate/x86_emulate.h +3
-1@@ -78,7 +78,9 @@ struct x86_event {uint8_t type;/* X86_EVENTTYPE_* */ uint8_t insn_len;/* Instruction length */ int32_t error_code;/* X86_EVENT_NO_EC if n/a */ -unsigned long extra;/* CR2 if X86_EXC_PF h/w exception */ + +/* Type-specific extra data (%cr2 for #PF, pending_dbg for #DB) */ +unsigned long extra; };/*
Signature · 2 lines
Sincerely, Jinoh Kang
PATCH 4/6 x86/pv: set DR_STEP if single-stepping after ro page fault emulation
In reply to Jinoh Kang--- xen/arch/x86/pv/ro-page-fault.c | 4 ++++ 1 file changed, 4 insertions(+)
xen/arch/x86/pv/ro-page-fault.c +4 -0
@@ -10,6 +10,7 @@#include <asm/pv/trace.h> #include <asm/shadow.h> +#include <asm/debugreg.h> #include "emulate.h" #include "mm.h"
@@ -390,7 +391,10 @@ int pv_ro_page_fault(unsigned long addr, struct cpu_user_regs *regs)/* Fallthrough */ case X86EMUL_OKAY :if ( ctxt.retire.singlestep ) + { + current->arch.dr6 |=DR_STEP |DR_STATUS_RESERVED_ONE ;pv_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); + }/* Fallthrough */ case X86EMUL_RETRY :
Signature · 1 line
2.41.0
PATCH 5/6 x86/pv: factor out single-step debug trap injection
In reply to Jinoh KangAdd pv_inject_debug_exception() helper and use it wherever applicable.
This helper corresponds to hvm_inject_debug_exception() in HVM.
--- xen/arch/x86/include/asm/domain.h | 12 ++++++++++++ xen/arch/x86/pv/emulate.c | 5 +---- xen/arch/x86/pv/ro-page-fault.c | 5 +---- xen/arch/x86/pv/traps.c | 10 ++++++++++ 4 files changed, 24 insertions(+), 8 deletions(-)
xen/arch/x86/include/asm/domain.h +12 -0
@@ -741,6 +741,18 @@ static inline void pv_inject_page_fault(int errcode, unsigned long cr2)pv_inject_event (&event); } +static inline void pv_inject_debug_exception (unsigned long pending_dbg) +{ +const struct x86_event event = { + .vector =X86_EXC_DB , + .type =X86_EVENTTYPE_HW_EXCEPTION , + .error_code =X86_EVENT_NO_EC , + .extra = pending_dbg, + }; + +pv_inject_event (&event); +} +static inline void pv_inject_sw_interrupt (unsigned int vector) {const struct x86_event event = {
xen/arch/x86/pv/emulate.c +1 -4
@@ -72,10 +72,7 @@ void pv_emul_instruction_done(struct cpu_user_regs *regs, unsigned long rip) regs->rip = rip; regs->eflags &= ~X86_EFLAGS_RF ;if ( regs->eflags &X86_EFLAGS_TF ) - { - current->arch.dr6 |=DR_STEP |DR_STATUS_RESERVED_ONE ; -pv_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); - } +pv_inject_debug_exception (DR_STEP ); }uint64_t pv_get_reg (struct vcpu *v,unsigned int reg)
xen/arch/x86/pv/ro-page-fault.c +1 -4
@@ -391,10 +391,7 @@ int pv_ro_page_fault(unsigned long addr, struct cpu_user_regs *regs)/* Fallthrough */ case X86EMUL_OKAY :if ( ctxt.retire.singlestep ) - { - current->arch.dr6 |=DR_STEP |DR_STATUS_RESERVED_ONE ; -pv_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); - } +pv_inject_debug_exception (DR_STEP );/* Fallthrough */ case X86EMUL_RETRY :
xen/arch/x86/pv/traps.c +10 -0
@@ -15,6 +15,7 @@#include <asm/pv/trace.h> #include <asm/shared.h> #include <asm/traps.h> +#include <asm/debugreg.h> #include <irq_vectors.h> void pv_inject_event (const struct x86_event *event)
@@ -64,7 +65,16 @@ void pv_inject_event(const struct x86_event *event)trace_pv_page_fault (event->extra, error_code); }else + { +if ( event->type ==X86_EVENTTYPE_HW_EXCEPTION && + vector ==X86_EXC_DB ) + { +if ( event->extra ) + curr->arch.dr6 |= event->extra |DR_STATUS_RESERVED_ONE ; + } +trace_pv_trap (vector, regs->rip, use_error_code, error_code); + }if ( use_error_code ) {
Signature · 1 line
2.41.0
PATCH 6/6 x86/debug: actually plumb pending_dbg through the monitor and devicemodel interfaces
In reply to Jinoh KangCommit 21867648033d ("x86/debug: Plumb pending_dbg through the monitor and devicemodel interfaces") introduced pending_dbg, but did not actually populate or use the field.
--- xen/arch/x86/hvm/svm/svm.c | 34 +++++++++++++++++++++++++++++++--- xen/arch/x86/hvm/vmx/vmx.c | 32 ++++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 11 deletions(-)
xen/arch/x86/hvm/svm/svm.c +31 -3
@@ -2422,6 +2422,14 @@ static bool cf_check svm_get_pending_event( info->type = vmcb->event_inj.type; info->error_code = vmcb->event_inj.ec; +if ( info->type ==X86_EVENTTYPE_HW_EXCEPTION && + info->vector ==X86_EXC_DB ) + { +unsigned long dr6 = v->arch.hvm.flag_dr_dirty ? +vmcb_get_dr6 (vmcb) : v->arch.dr6; + info->extra = dr6 & ~DR_STATUS_RESERVED_ONE ; + } +return true; }
@@ -2733,16 +2741,28 @@ void svm_vmexit_handler(void)if ( !v->domain->debugger_attached ) {unsigned int trap_type; +unsigned long exit_pending_dbg;if (likely (exit_reason !=VMEXIT_ICEBP ) ) { trap_type =X86_EVENTTYPE_HW_EXCEPTION ; insn_len =0 ; + +__restore_debug_registers (vmcb, v); + +/* +* NOTE: This is slightly wrong; old bits in dr6 are not +* automatically cleared by CPU on #DB, so it's not exactly +* equivalent to PENDING_DBG_EXCEPTIONS in semantics. +*/ + exit_pending_dbg =vmcb_get_dr6 (vmcb) & ~DR_STATUS_RESERVED_ONE ; +vmcb_set_dr6 (vmcb,DR_STATUS_RESERVED_ONE ); }else { trap_type =X86_EVENTTYPE_PRI_SW_EXCEPTION ; insn_len =svm_get_insn_len (v,INSTR_ICEBP ); + exit_pending_dbg =0 ;if ( !insn_len )break ;
@@ -2750,12 +2770,20 @@ void svm_vmexit_handler(void) rc =hvm_monitor_debug (regs->rip,HVM_MONITOR_DEBUG_EXCEPTION , - trap_type, insn_len,0 ); + trap_type, insn_len, exit_pending_dbg);if ( rc <0 ) goto unexpected_exit_type;if ( !rc ) -hvm_inject_exception (X86_EXC_DB , - trap_type, insn_len,X86_EVENT_NO_EC ); + { +if (trap_type ==X86_EVENTTYPE_HW_EXCEPTION ) + { +/* Updates DR6 where debugger can peek. */ +hvm_inject_debug_exception (exit_pending_dbg); + } +else +hvm_inject_exception (X86_EXC_DB , + trap_type, insn_len,X86_EVENT_NO_EC ); + } }else domain_pause_for_debugger ();
xen/arch/x86/hvm/vmx/vmx.c +24 -8
@@ -2469,6 +2469,14 @@ static bool cf_check vmx_get_pending_event( info->type =MASK_EXTR (intr_info,INTR_INFO_INTR_TYPE_MASK ); info->error_code = error_code; +if ( info->type ==X86_EVENTTYPE_HW_EXCEPTION && + info->vector ==X86_EXC_DB ) + { +unsigned long dr6 = v->arch.hvm.flag_dr_dirty ? +read_debugreg (6 ) : v->arch.dr6; + info->extra = dr6 & ~DR_STATUS_RESERVED_ONE ; + } +return true; }
@@ -4240,13 +4248,11 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) {case X86_EXC_DB :/* -* Updates DR6 where debugger can peek (See 3B 23.2.1, -* Table 23-1, "Exit Qualification for Debug Exceptions"). +* See 3B 23.2.1, Table 23-1, "Exit Qualification for Debug +* Exceptions". */ __vmread (EXIT_QUALIFICATION , &exit_qualification);HVMTRACE_1D (TRAP_DEBUG , exit_qualification); -__restore_debug_registers (v); -write_debugreg (6 , exit_qualification |DR_STATUS_RESERVED_ONE );/* * Work around SingleStep + STI/MovSS VMEntry failures.
@@ -4285,22 +4291,32 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)if ( !v->domain->debugger_attached ) { -unsigned long insn_len =0 ; +unsigned long exit_pending_dbg =0 , insn_len =0 ;int rc;unsigned long trap_type =MASK_EXTR (intr_info,INTR_INFO_INTR_TYPE_MASK ); -if ( trap_type >=X86_EVENTTYPE_SW_INTERRUPT ) +if ( trap_type ==X86_EVENTTYPE_HW_EXCEPTION ) + exit_pending_dbg = exit_qualification; +else if ( trap_type >=X86_EVENTTYPE_SW_INTERRUPT )__vmread (VM_EXIT_INSTRUCTION_LEN , &insn_len); rc =hvm_monitor_debug (regs->rip,HVM_MONITOR_DEBUG_EXCEPTION , - trap_type, insn_len,0 ); + trap_type, insn_len, exit_pending_dbg);if ( rc <0 ) goto exit_and_crash;if ( !rc ) -vmx_propagate_intr (intr_info); + { +if ( trap_type ==X86_EVENTTYPE_HW_EXCEPTION ) + { +/* Updates DR6 where debugger can peek. */ +hvm_inject_debug_exception (exit_pending_dbg); + } +else +vmx_propagate_intr (intr_info); + } }else domain_pause_for_debugger ();
Signature · 1 line
2.41.0
Quoted 2 lines · Jinoh Kang
Xen has a bug where hardware breakpoint exceptions (DR_TRAP<n>) are erroneously recognized as single-stepping exceptions (
DR_STEP).
I expected this to come back and bite.
https://lore.kernel.org/xen-devel/[email protected]/
Xen's %dr6 handling is very broken, and then Spectre/Meltdown happened and some how my bugfixes are now 5 years old and still incomplete. I've got a form of this series rebased onto staging, which I'll need to dust off.
That said, I was not aware of this specific case going wrong. (But I can't say I'm surprised.)
Thankyou for the test case. If I'm reading it right, the problem is that when %dr0 genuinely triggers, we VMExit (to break #DB infinite loops), and on re-injecting the #DB back to the guest, we blindly set singlestep?
This is wrong for #DB faults, and you're using an instruction breakpoint so that matches.
However, it is far more complicated for #DB traps, where hardware leaves it up to the VMM to merge status bits, and it's different between PV, VT-x and SVM.
Worse than that, there is an bug on Intel hardware where if a singlestep is genuinely pending, then data breakpoint information isn't passed to the VMM on VMExit. I have yet to persuade Intel to fix this, despite quite a lot of trying.
Looking at patch 3, I think I can see how it fixes your bug, but I don't think the logic is correct in all cases. In particular, look at my series for the cases where X86_DR6_DEFAULT is used to flip polarity. Notably, PV and SVM have different dr6 polarity to VT-x's pending_dbg field.
Also, on Intel you're supposed to leave pending bits in pending_dbg and not inject #DB directly, in order for the pipeline to get the exception priority in the right order. This I didn't get around to fixing at the time.
I suspect what we'll need to do is combine parts of your series and parts of mine. I never got around to fixing the introspection bugs in mine (that's a far larger task to do nicely), and yours is more targetted.
~Andrew
Quoted 3 lines · Andrew Cooper
I suspect what we'll need to do is combine parts of your series and parts of mine. I never got around to fixing the introspection bugs in mine (that's a far larger task to do nicely), and yours is more targetted.
Unless I've done something wrong, your debug-fixes-v1 when rebased to master seems to fix my problem, so I guess your patchset is the superset.
Signature · 2 lines
Sincerely, Jinoh Kang