x86/pv: More debugging fixes
v2 cover x86/pv: More debugging fixes
Patches 1 and 2 already reviewed, and split out of a larger series covering HVM too. However, as it's been 6 years (yes really), I've reposed them just in case.
Patch 3 fixes a Coverity complaint, highlighted by Matt's recent change to fix IO breakpoint recognition.
Andrew Cooper (3):
x86/pv: Introduce x86_merge_dr6() and fix do_debug() x86/pv: Fix merging of new status bits into %dr6 x86/pv: Address Coverity complaint in check_guest_io_breakpoint()
xen/arch/x86/debug.c | 20 +++++++++++++++ xen/arch/x86/include/asm/debugreg.h | 34 ++++++++++++++++++++++++++ xen/arch/x86/include/asm/domain.h | 12 +++++++++ xen/arch/x86/include/asm/x86-defns.h | 7 ++++++ xen/arch/x86/pv/emul-priv-op.c | 26 ++++++-------------- xen/arch/x86/pv/emulate.c | 6 ++--- xen/arch/x86/pv/ro-page-fault.c | 2 +- xen/arch/x86/pv/traps.c | 16 +++++++++--- xen/arch/x86/traps.c | 12 ++++++--- xen/arch/x86/x86_emulate/x86_emulate.h | 5 +++- 10 files changed, 107 insertions(+), 33 deletions(-)
Signature · 1 line
2.39.2
PATCH v2 1/3 x86/pv: Introduce x86_merge_dr6() and fix do_debug()
Pretty much everywhere in Xen the logic to update %dr6 when injecting #DB is buggy. The architectural behaviour is to overwrite B{0..3} (rather than accumulate) and accumulate all other bits.
Introduce a new x86_merge_dr6() helper, and start fixing the mess by adjusting the dr6 merge in do_debug(). Also correct the comment.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
v2:
* Rebase (~6y worth)
* Split PV changes out of joint HVM patch.
In some theoretical future where deubgging is implemented in terms of introspection even for PV guests, the TODO will complete itself.
--- xen/arch/x86/debug.c | 20 ++++++++++++++++++++ xen/arch/x86/include/asm/debugreg.h | 8 ++++++++ xen/arch/x86/include/asm/x86-defns.h | 7 +++++++ xen/arch/x86/traps.c | 10 +++++++--- 4 files changed, 42 insertions(+), 3 deletions(-)
xen/arch/x86/debug.c +20 -0
@@ -2,12 +2,32 @@/* * Copyright (C) 2023 XenServer. */ +#include <xen/bug.h> #include <xen/kernel.h> #include <xen/lib/x86/cpu-policy.h> #include <asm/debugreg.h> +unsigned int x86_merge_dr6 (const struct cpu_policy *p,unsigned int dr6, +unsigned int pending_dbg) +{ +/* Flip dr6 to have positive polarity. */ + dr6 ^=X86_DR6_DEFAULT ; + +/* Sanity check that only known values are passed in. */ +ASSERT (!(dr6 & ~X86_DR6_KNOWN_MASK )); +ASSERT (!(pending_dbg & ~X86_DR6_KNOWN_MASK )); + +/* Breakpoints 0-3 overridden. Others accumulate. */ + dr6 = (dr6 & ~X86_DR6_BP_MASK ) | pending_dbg; + +/* Flip dr6 back to having default polarity. */ + dr6 ^=X86_DR6_DEFAULT ; + +return x86_adj_dr6_rsvd (p, dr6); +} +unsigned int x86_adj_dr6_rsvd (const struct cpu_policy *p,unsigned int dr6) {unsigned int ones =X86_DR6_DEFAULT ;
xen/arch/x86/include/asm/debugreg.h +8 -0
@@ -108,4 +108,12 @@ struct cpu_policy;unsigned int x86_adj_dr6_rsvd (const struct cpu_policy *p,unsigned int dr6);unsigned int x86_adj_dr7_rsvd (const struct cpu_policy *p,unsigned int dr7); +/* +* Merging new status bits into dr6 is far from simple. Breakpoints override, +* while others accumulate. New bits to be merged are taken with positive +* polarity. +*/ +unsigned int x86_merge_dr6 (const struct cpu_policy *p,unsigned int dr6, +unsigned int pending_dbg); +#endif /* _X86_DEBUGREG_H */
xen/arch/x86/include/asm/x86-defns.h +7 -0
@@ -132,6 +132,13 @@#define X86_DR6_ZEROS _AC(0x00001000, UL)/* %dr6 bits forced to 0 */ #define X86_DR6_DEFAULT _AC(0xffff0ff0, UL)/* Default %dr6 value */ +#define X86_DR6_BP_MASK \ + (X86_DR6_B0 | X86_DR6_B1 | X86_DR6_B2 | X86_DR6_B3) + +#define X86_DR6_KNOWN_MASK \ + (X86_DR6_BP_MASK | X86_DR6_BLD | X86_DR6_BD | X86_DR6_BS | \ + X86_DR6_BT | X86_DR6_RTM) +/* * Debug control flags in DR7. */
xen/arch/x86/traps.c +7 -3
@@ -2016,9 +2016,13 @@ void asmlinkage do_debug(struct cpu_user_regs *regs)return ; } -/* Save debug status register where guest OS can peek at it */ - v->arch.dr6 |= (dr6 & ~X86_DR6_DEFAULT ); - v->arch.dr6 &= (dr6 | ~X86_DR6_DEFAULT ); +/* +* Update the guest's dr6 so the debugger can peek at it. +* TODO: This should be passed out-of-bad to the debugger, so guest state +* is not corrupted by debugging actions completed behind it's back. +*/ + v->arch.dr6 =x86_merge_dr6 (v->domain->arch.cpu_policy, + v->arch.dr6, dr6 ^X86_DR6_DEFAULT );if (guest_kernel_mode (v, regs) && v->domain->debugger_attached ) {
Signature · 1 line
2.39.2
Re: [PATCH v2 1/3] x86/pv: Introduce x86_merge_dr6() and fix do_debug()
Quoted 3 lines · Andrew Cooper
Pretty much everywhere in Xen the logic to update %dr6 when injecting #DB is buggy. The architectural behaviour is to overwrite B{0..3} (rather than accumulate) and accumulate all other bits.
Just to double check (after 6 years I clearly don't recall anything that isn't written down in the description): The SDM pretty vaguely says "Certain debug exceptions may clear bits 0-3." What other information sources are there to make this less uncertain? (Picking an old hardcopy from the shelf, that confirms that long ago DR6 was indeed documented as all sticky.)
Jan
Re: [PATCH v2 1/3] x86/pv: Introduce x86_merge_dr6() and fix do_debug()
Quoted 9 lines · Jan Beulich
Quoted 3 lines · Andrew Cooper
Pretty much everywhere in Xen the logic to update %dr6 when injecting #DB is buggy. The architectural behaviour is to overwrite B{0..3} (rather than accumulate) and accumulate all other bits.
Just to double check (after 6 years I clearly don't recall anything that isn't written down in the description): The SDM pretty vaguely says "Certain debug exceptions may clear bits 0-3." What other information sources are there to make this less uncertain? (Picking an old hardcopy from the shelf, that confirms that long ago DR6 was indeed documented as all sticky.)
Well, "talking with the architects", but as always "it's complicated".
The debug infrastructure has had several breaking changes in it over the years (e.g. the reserved bits didn't use to be f's), and this one probably changed with the introduction of superscalar pipelines. That said, I don't think I've ever found a Spec Update / Revision Guide that doesn't have one "oops we don't do breakpoints properly in this case" erratum listed.
I'm informed it's "most going on all exceptions" these days, and the behaviour here did come mostly from my discussions about XSA-308 (or rather, the pre-security angle where it was just a bugfix) with Intel.
A guest that does clear the status bits every #DB won't notice, and one that doesn't will have problems on real hardware.
But I can reword if if you'd prefer?
~Andrew
Re: [PATCH v2 1/3] x86/pv: Introduce x86_merge_dr6() and fix do_debug()
Quoted 20 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich
Quoted 3 lines · Andrew Cooper
Pretty much everywhere in Xen the logic to update %dr6 when injecting #DB is buggy. The architectural behaviour is to overwrite B{0..3} (rather than accumulate) and accumulate all other bits.
Just to double check (after 6 years I clearly don't recall anything that isn't written down in the description): The SDM pretty vaguely says "Certain debug exceptions may clear bits 0-3." What other information sources are there to make this less uncertain? (Picking an old hardcopy from the shelf, that confirms that long ago DR6 was indeed documented as all sticky.)
Well, "talking with the architects", but as always "it's complicated".
The debug infrastructure has had several breaking changes in it over the years (e.g. the reserved bits didn't use to be f's), and this one probably changed with the introduction of superscalar pipelines. That said, I don't think I've ever found a Spec Update / Revision Guide that doesn't have one "oops we don't do breakpoints properly in this case" erratum listed.
Quoted 8 lines
I'm informed it's "most going on all exceptions" these days, and the behaviour here did come mostly from my discussions about XSA-308 (or rather, the pre-security angle where it was just a bugfix) with Intel.
A guest that does clear the status bits every #DB won't notice, and one that doesn't will have problems on real hardware.
But I can reword if if you'd prefer?
If I may ask, I'd like it to at least be clarified that documentation isn't quite precise here. Kind of to soften "architectural behaviour" a little.
Jan
Re: [PATCH v2 1/3] x86/pv: Introduce x86_merge_dr6() and fix do_debug()
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich
Quoted 3 lines · Andrew Cooper
Pretty much everywhere in Xen the logic to update %dr6 when injecting #DB is buggy. The architectural behaviour is to overwrite B{0..3} (rather than accumulate) and accumulate all other bits.
Just to double check (after 6 years I clearly don't recall anything that isn't written down in the description): The SDM pretty vaguely says "Certain debug exceptions may clear bits 0-3." What other information sources are there to make this less uncertain? (Picking an old hardcopy from the shelf, that confirms that long ago DR6 was indeed documented as all sticky.)
Well, "talking with the architects", but as always "it's complicated".
The debug infrastructure has had several breaking changes in it over the years (e.g. the reserved bits didn't use to be f's), and this one probably changed with the introduction of superscalar pipelines. That said, I don't think I've ever found a Spec Update / Revision Guide that doesn't have one "oops we don't do breakpoints properly in this case" erratum listed.
I'm informed it's "most going on all exceptions" these days, and the behaviour here did come mostly from my discussions about XSA-308 (or rather, the pre-security angle where it was just a bugfix) with Intel.
Quoted 6 lines
Quoted 4 lines
A guest that does clear the status bits every #DB won't notice, and one that doesn't will have problems on real hardware.
But I can reword if if you'd prefer?
If I may ask, I'd like it to at least be clarified that documentation isn't quite precise here. Kind of to soften "architectural behaviour" a little.
I've done some experimenting by pre/reloading %dr6 with ~DR6_DEFAULT (i.e. all sticky bits asserted).
The only debug event I can find which does not clear breakpoints is ICEBP on AMD.
ICEBP on Intel, and all combinations of singlestep/breakpoint/general detect including SS-delayed on both Intel and AMD clear the breakpoint bits.
I'll note this, but given how broken ICEBP virt is on AMD (i.e. not virtualised at all), I'm not inclined to special case it.
Also, I've found
https://lore.kernel.org/xen-devel/[email protected]/
which I'd totally forgotten about, which is mostly better reviewed/acked, and contains some fixes that I'd missed when (re)doing this part.
I'm going to pick out what I can from both of these series, because there's still some breaking errors found in a Linux HVM VM which I was trying to fix for 4.18 that are still not addressed...
~Andrew
PATCH v2 2/3 x86/pv: Fix merging of new status bits into %dr6
In reply to Andrew CooperAll #DB exceptions result in an update of %dr6, but this isn't captured in Xen's handling, and is buggy everywhere.
To begin resolving this issue, add a new pending_dbg field to x86_event (unioned with cr2 to avoid taking any extra space), and introduce pv_inject_debug_exn() helpers to replace the current callers using pv_inject_hw_exception().
Push the adjustment of v->arch.dr6 into pv_inject_event(), and use the new x86_merge_dr6() rather than the current incorrect logic.
A key property is that pending_dbg is taken with positive polarity to deal with RTM/BLD sensibly. Most callers pass in a constant, but callers passing in a hardware %dr6 value need to XOR the value with X86_DR6_DEFAULT to flip to positive polarity.
This fixes the behaviour of the breakpoint status bits; specifically that any left pending are discarded when a new #DB is raised. In principle it would fix RTM/BLD too, except PV guests can't turn these capabilities on to start with.
---
v2:
* Rebase (~6y worth)
* Split PV changes out of joint HVM patch.
--- xen/arch/x86/include/asm/domain.h | 12 ++++++++++++ xen/arch/x86/pv/emul-priv-op.c | 5 +---- xen/arch/x86/pv/emulate.c | 6 ++---- xen/arch/x86/pv/ro-page-fault.c | 2 +- xen/arch/x86/pv/traps.c | 16 ++++++++++++---- xen/arch/x86/traps.c | 2 +- xen/arch/x86/x86_emulate/x86_emulate.h | 5 ++++- 7 files changed, 33 insertions(+), 15 deletions(-)
xen/arch/x86/include/asm/domain.h +12 -0
@@ -731,6 +731,18 @@ static inline void pv_inject_hw_exception(unsigned int vector, int errcode)pv_inject_event (&event); } +static inline void pv_inject_debug_exn (unsigned int pending_dbg) +{ +struct x86_event event = { + .vector =X86_EXC_DB , + .type =X86_EVENTTYPE_HW_EXCEPTION , + .error_code =X86_EVENT_NO_EC , + .pending_dbg = pending_dbg, + }; + +pv_inject_event (&event); +} +static inline void pv_inject_page_fault (int errcode,unsigned long cr2) {const struct x86_event event = {
xen/arch/x86/pv/emul-priv-op.c +1 -4
@@ -1366,10 +1366,7 @@ int pv_emulate_privileged_op(struct cpu_user_regs *regs) ctxt.bpmatch |=DR_STEP ;if ( ctxt.bpmatch ) - { - curr->arch.dr6 |= ctxt.bpmatch |DR_STATUS_RESERVED_ONE ; -pv_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); - } +pv_inject_debug_exn (ctxt.bpmatch);/* fall through */ case X86EMUL_RETRY :
xen/arch/x86/pv/emulate.c +2 -4
@@ -71,11 +71,9 @@ 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_exn (X86_DR6_BS ); }uint64_t pv_get_reg (struct vcpu *v,unsigned int reg)
xen/arch/x86/pv/ro-page-fault.c +1 -1
@@ -390,7 +390,7 @@ int pv_ro_page_fault(unsigned long addr, struct cpu_user_regs *regs)/* Fallthrough */ case X86EMUL_OKAY :if ( ctxt.retire.singlestep ) -pv_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +pv_inject_debug_exn (X86_DR6_BS );/* Fallthrough */ case X86EMUL_RETRY :
xen/arch/x86/pv/traps.c +12 -4
@@ -12,6 +12,7 @@#include <xen/lib.h> #include <xen/softirq.h> +#include <asm/debugreg.h> #include <asm/pv/trace.h> #include <asm/shared.h> #include <asm/traps.h>
@@ -50,9 +51,9 @@ void pv_inject_event(const struct x86_event *event) tb->cs = ti->cs; tb->eip = ti->address; -if ( event->type ==X86_EVENTTYPE_HW_EXCEPTION && - vector ==X86_EXC_PF ) +switch ( vector | -(event->type ==X86_EVENTTYPE_SW_INTERRUPT ) ) { +case X86_EXC_PF : curr->arch.pv.ctrlreg[2 ] = event->cr2;arch_set_cr2 (curr, event->cr2);
@@ -62,9 +63,16 @@ void pv_inject_event(const struct x86_event *event) error_code |= PFEC_user_mode;trace_pv_page_fault (event->cr2, error_code); - } -else +break ; + +case X86_EXC_DB : + curr->arch.dr6 =x86_merge_dr6 (curr->domain->arch.cpu_policy, + curr->arch.dr6, event->pending_dbg); + fallthrough; +default :trace_pv_trap (vector, regs->rip, use_error_code, error_code); +break ; + }if ( use_error_code ) {
xen/arch/x86/traps.c +1 -1
@@ -2030,7 +2030,7 @@ void asmlinkage do_debug(struct cpu_user_regs *regs)return ; } -pv_inject_hw_exception (X86_EXC_DB ,X86_EVENT_NO_EC ); +pv_inject_debug_exn (0 /* N/A, already merged */ ); } void asmlinkage do_entry_CP(struct cpu_user_regs *regs)
xen/arch/x86/x86_emulate/x86_emulate.h +4 -1
@@ -78,7 +78,10 @@ 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 */ +union { +unsigned long cr2;/* #PF */ +unsigned long pending_dbg;/* #DB (new DR6 bits, positive polarity) */ + }; };/*
Signature · 1 line
2.39.2
PATCH v2 3/3 x86/pv: Address Coverity complaint in check_guest_io_breakpoint()
In reply to Andrew CooperCommit 08aacc392d86 ("x86/emul: Fix misaligned IO breakpoint behaviour in PV guests") caused a Coverity INTEGER_OVERFLOW complaint based on the reasoning that width could be 0.
It can't, but digging into the code generation, GCC 8 and later (bisected on gotbolt) choose to emit a CSWITCH lookup table, and because the range (bottom 2 bits clear), it's a 16-entry lookup table.
So Coverity is correct, given that GCC did emit a (dead) logic path where width stayed 0.
Rewrite the logic. Introduce x86_bp_width() which compiles to a single basic block, which replaces the switch() statement. Take the opportunity to also make start and width be loop-scope variables.
No practical change, but it should compile better and placate Coverity.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Matthew Barnes <[email protected]>
--- xen/arch/x86/include/asm/debugreg.h | 26 ++++++++++++++++++++++++++ xen/arch/x86/pv/emul-priv-op.c | 21 ++++++--------------- 2 files changed, 32 insertions(+), 15 deletions(-)
xen/arch/x86/include/asm/debugreg.h +26 -0
@@ -116,4 +116,30 @@ unsigned int x86_adj_dr7_rsvd(const struct cpu_policy *p, unsigned int dr7);unsigned int x86_merge_dr6 (const struct cpu_policy *p,unsigned int dr6,unsigned int pending_dbg); +/* +* Calculate the width of a breakpoint from its dr7 encoding. +* +* The LEN encoding in dr7 is 2 bits wide per breakpoint and encoded a mask +* (0, 1 and 3) for widths of 1, 2 and 4 respectively in the 32bit days. +* +* In 64bit, the unused value (2) was specified to mean a width of 8, which is +* great for encoding efficiency but less great for nicely calculating the +* width. +*/ +static inline unsigned int x86_bp_width (unsigned int dr7,unsigned int bp) +{ +unsigned int raw = (dr7 >> (DR_CONTROL_SHIFT + +DR_CONTROL_SIZE * bp +2 )) &3 ; + +/* +* If the top bit is set (i.e. we've got a 4 or 8), flip the bottom to +* reverse their order, making them sorted properly. Then it's a simple +* shift to calculate the width. +*/ +if ( raw &2 ) + raw ^=1 ; + +return 1U << raw; +} +#endif /* _X86_DEBUGREG_H */
xen/arch/x86/pv/emul-priv-op.c +6 -15
@@ -323,30 +323,21 @@ static unsigned int check_guest_io_breakpoint(struct vcpu *v, unsigned int port, unsigned int len) { -unsigned int width, i, match =0 ; -unsigned long start; +unsigned int i, match =0 ;if ( !v->arch.pv.dr7_emul || !(v->arch.pv.ctrlreg[4 ] &X86_CR4_DE ) )return 0 ;for ( i =0 ; i <4 ; i++ ) { +unsigned long start; +unsigned int width; +if ( !(v->arch.pv.dr7_emul & (3 << (i *DR_ENABLE_SIZE ))) )continue ; - start = v->arch.dr[i]; - width =0 ; - -switch ( (v->arch.dr7 >> - (DR_CONTROL_SHIFT + i *DR_CONTROL_SIZE )) &0xc ) - { -case DR_LEN_1 : width =1 ;break ; -case DR_LEN_2 : width =2 ;break ; -case DR_LEN_4 : width =4 ;break ; -case DR_LEN_8 : width =8 ;break ; - } - - start &= ~(width -1UL ); + width =x86_bp_width (v->arch.dr7, i); + start = v->arch.dr[i] & ~(width -1UL );if ( (start < (port + len)) && ((start + width) > port) ) match |=1u << i;
Signature · 1 line
2.39.2
Re: [PATCH v2 3/3] x86/pv: Address Coverity complaint in check_guest_io_breakpoint()
Quoted 11 lines · Andrew Cooper
Commit
08aacc392d86("x86/emul: Fix misaligned IO breakpoint behaviour in PV guests") caused a CoverityINTEGER_OVERFLOWcomplaint based on the reasoning that width could be 0.It can't, but digging into the code generation, GCC 8 and later (bisected on gotbolt) choose to emit a CSWITCH lookup table, and because the range (bottom 2 bits clear), it's a 16-entry lookup table.
So Coverity is correct, given that GCC did emit a (dead) logic path where width stayed 0.
Quoted 7 lines
Rewrite the logic. Introduce
x86_bp_width()which compiles to a single basic block, which replaces theswitch()statement. Take the opportunity to also make start and width be loop-scope variables.No practical change, but it should compile better and placate Coverity.
Fixes 08aacc392d86 ("x86/emul: Fix misaligned IO breakpoint behaviour in PV guests")
I forgot Coverity-ID: 1616152, fixed locally.
Quoted 2 lines
Signed-off-by Andrew Cooper <[email protected]>
Re: [PATCH v2 3/3] x86/pv: Address Coverity complaint in check_guest_io_breakpoint()
In reply to Andrew CooperQuoted 11 lines · Andrew Cooper
Commit
08aacc392d86("x86/emul: Fix misaligned IO breakpoint behaviour in PV guests") caused a CoverityINTEGER_OVERFLOWcomplaint based on the reasoning that width could be 0.It can't, but digging into the code generation, GCC 8 and later (bisected on gotbolt) choose to emit a CSWITCH lookup table, and because the range (bottom 2 bits clear), it's a 16-entry lookup table.
So Coverity is correct, given that GCC did emit a (dead) logic path where width stayed 0.
Quoted 8 lines
Rewrite the logic. Introduce
x86_bp_width()which compiles to a single basic block, which replaces theswitch()statement. Take the opportunity to also make start and width be loop-scope variables.No practical change, but it should compile better and placate Coverity.
Fixes 08aacc392d86 ("x86/emul: Fix misaligned IO breakpoint behaviour in PV guests")Signed-off-by Andrew Cooper <[email protected]>