x86/pv: Fixes to debug register handling
cover x86/pv: Fixes to debug register handling
At this point, I think these patches are plausible candidates for inclusion into 4.11. Whether they want backporting is a slightly harder matter, as these patches necesserily alter some error values for the get/set_debug_reg() hypercalls.
If however there is objection to these going into 4.11, they can sit in x86-next until the 4.12 window opens.
Andrew Cooper (3):
x86/pv: Introduce and use x86emul_read_dr()
x86/pv: Introduce and use x86emul_write_dr() x86/traps: Misc non-functional improvements to set_debugreg()
xen/arch/x86/pv/emul-priv-op.c | 24 +---------- xen/arch/x86/pv/misc-hypercalls.c | 18 ++------- xen/arch/x86/traps.c | 67 ++++++++++++++++++++----------- xen/arch/x86/x86_emulate.c | 73 ++++++++++++++++++++++++++++++++++ xen/arch/x86/x86_emulate/x86_emulate.h | 5 +++ 5 files changed, 127 insertions(+), 60 deletions(-)
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 1/3 x86/pv: Introduce and use x86emul_read_dr()
do_get_debugreg() has several bugs:
* The %cr4.de condition is inverted. %dr4/5 should be accessible only when %cr4.de is disabled.
* When %cr4.de is disabled, emulation should yield #UD rather than complete with zero.
* Using -EINVAL for errors is a broken ABI, as it overlaps with valid values near the top of the address space.
Introduce a common x86emul_read_dr() handler (as we will eventually want to add HVM support) which separates its success/failure indication from the data value, and have do_get_debugreg() call into the handler.
The ABI of do_get_debugreg() remains broken, but switches from -EINVAL to -ENODEV for compatibility with the changes in the following patch.
Take the opportunity to add a missing local variable block to x86_emulate.c
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Juergen Gross <[email protected]>
--- xen/arch/x86/pv/emul-priv-op.c | 15 +---------- xen/arch/x86/pv/misc-hypercalls.c | 18 +++---------- xen/arch/x86/x86_emulate.c | 49 ++++++++++++++++++++++++++++++++++ xen/arch/x86/x86_emulate/x86_emulate.h | 3 +++ 4 files changed, 56 insertions(+), 29 deletions(-)
xen/arch/x86/pv/emul-priv-op.c +1 -14
@@ -794,19 +794,6 @@ static int write_cr(unsigned int reg, unsigned long val,return X86EMUL_UNHANDLEABLE ; } -static int read_dr (unsigned int reg,unsigned long *val, -struct x86_emulate_ctxt *ctxt) -{ -unsigned long res =do_get_debugreg (reg); - -if (IS_ERR_VALUE (res) ) -return X86EMUL_UNHANDLEABLE ; - - *val = res; - -return X86EMUL_OKAY ; -} -static int write_dr (unsigned int reg,unsigned long val,struct x86_emulate_ctxt *ctxt) {
@@ -1305,7 +1292,7 @@ static const struct x86_emulate_ops priv_op_ops = { .read_segment = read_segment, .read_cr = read_cr, .write_cr = write_cr, - .read_dr = read_dr, + .read_dr = x86emul_read_dr, .write_dr = write_dr, .write_xcr = x86emul_write_xcr, .read_msr = read_msr,
xen/arch/x86/pv/misc-hypercalls.c +3 -15
@@ -30,22 +30,10 @@ long do_set_debugreg(int reg, unsigned long value)unsigned long do_get_debugreg (int reg) { -struct vcpu *curr = current; +unsigned long val; +int res =x86emul_read_dr (reg, &val,NULL ); -switch ( reg ) - { - case 0 ... 3: -case 6 : -return curr->arch.debugreg[reg]; -case 7 : -return (curr->arch.debugreg[7 ] | - curr->arch.debugreg[5 ]); - case 4 ... 5: -return ((curr->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) ? - curr->arch.debugreg[reg +2 ] :0 ); - } - -return -EINVAL ; +return res ==X86EMUL_OKAY ? val : -ENODEV ; }long do_fpu_taskswitch (int set)
xen/arch/x86/x86_emulate.c +49 -0
52 lines not on this page · raw message
xen/arch/x86/x86_emulate/x86_emulate.h +3 -0
@@ -722,6 +722,9 @@ int x86emul_read_xcr(unsigned int reg, uint64_t *val,int x86emul_write_xcr (unsigned int reg,uint64_t val,struct x86_emulate_ctxt *ctxt); +int x86emul_read_dr (unsigned int reg,unsigned long *val, +struct x86_emulate_ctxt *ctxt); +#endif int
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 1/3] x86/pv: Introduce and use x86emul_read_dr()
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
do_get_debugreg()has several bugs:* The %cr4.de condition is inverted. %dr4/5 should be accessible only when %cr4.de is disabled.
* When %cr4.de is disabled, emulation should yield #UD rather than complete with zero.
* Using-EINVALfor errors is a broken ABI, as it overlaps with valid values near the top of the address space.
Quoted 3 lines
Introduce a common
x86emul_read_dr()handler (as we will eventually want to add HVM support) which separates its success/failure indication from the data value, and havedo_get_debugreg()call into the handler.
The HVM part here is sort of questionable because of your use of curr->arch.pv_vcpu.ctrlreg[4]. This is appropriate for the NULL ctxt case, but it's already a layering violation for the use of the function in priv_op_ops, where the read_cr() hook should be used instead.
Quoted 4 lines
The ABI of
do_get_debugreg()remains broken, but switches from-EINVALto-ENODEVfor compatibility with the changes in the following patch.Take the opportunity to add a missing local variable block to
x86_emulate.c
I don't think such a block can ever be "missing" - it shouldn't really be a requirement for one to be there; note how ./CODING_STYLE says "is permitted". Of course I don't mind its addition here.
Quoted 15 lines
xen/arch/x86/x86_emulate.c +15
-0+int x86emul_read_dr (unsigned int reg,unsigned long *val, +struct x86_emulate_ctxt *ctxt) +{ +struct vcpu *curr = current; + +/* HVM support requires a bit more plumbing before it will work. */ +ASSERT (is_pv_vcpu (curr)); + +switch ( reg ) + { + case 0 ... 3: +case 6 : + *val = curr->arch.debugreg[reg]; +break ; +
xen/arch/x86/x86_emulate.c int x86emul_write_xcr(unsigned int reg, uint64_t val, · line 116
Quoted 10 lines
xen/arch/x86/x86_emulate.c +10
-0+case 7 : + *val = (curr->arch.debugreg[7 ] | + curr->arch.debugreg[5 ]); +break ; + + case 4 ... 5: +if ( !(curr->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) ) + { + *val = curr->arch.debugreg[reg +2 ]; +break ;
Once at it, wouldn't you better also fix the missing ORing of [5] into the DR7 (really DR5) value here?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 1/3] x86/pv: Introduce and use x86emul_read_dr()
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
do_get_debugreg()has several bugs:* The %cr4.de condition is inverted. %dr4/5 should be accessible only when %cr4.de is disabled.
* When %cr4.de is disabled, emulation should yield #UD rather than complete with zero.
* Using-EINVALfor errors is a broken ABI, as it overlaps with valid values near the top of the address space.
Quoted 5 lines
Quoted 3 lines
Introduce a common
x86emul_read_dr()handler (as we will eventually want to add HVM support) which separates its success/failure indication from the data value, and havedo_get_debugreg()call into the handler.The HVM part here is sort of questionable because of your use of curr->arch.pv_vcpu.ctrlreg[4].
That is what the "needs further plumbing" refers to, as well as needing hooks to get/modify %dr6/7 from the VMCB/VMCS.
However, we are gaining an increasing amount of common x86 code which needs to read control register values, and I've got a plan to refactor across the board to v->arch.cr4 (and similar). There is no point having identical information in different parts of sub-unions.
Quoted 3 lines
This is appropriate for the NULL ctxt case,
but it's already a layering violation for the use of the function inpriv_op_ops, where theread_cr()hook should be used instead.
Hmm - doing this, while probably the better long temr course of action, would require passing the ops structures down into the callbacks.
Quoted 20 lines
Quoted 20 lines
xen/arch/x86/x86_emulate.c +19
-0+int x86emul_read_dr (unsigned int reg,unsigned long *val, +struct x86_emulate_ctxt *ctxt) +{ +struct vcpu *curr = current; + +/* HVM support requires a bit more plumbing before it will work. */ +ASSERT (is_pv_vcpu (curr)); + +switch ( reg ) + { + case 0 ... 3: +case 6 : + *val = curr->arch.debugreg[reg]; +break ; + +case 7 : + *val = (curr->arch.debugreg[7 ] | + curr->arch.debugreg[5 ]); +break ;
Quoted 8 lines
xen/arch/x86/x86_emulate.c int x86emul_write_xcr(unsigned int reg, uint64_t val, · line 116
Quoted 6 lines
xen/arch/x86/x86_emulate.c +6
-0+ + case 4 ... 5: +if ( !(curr->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) ) + { + *val = curr->arch.debugreg[reg +2 ]; +break ;Once at it, wouldn't you better also fix the missing ORing of [5] into the DR7 (really DR5) value here?
[5] is zero when %cr4.de is clear (subject to a bugfix in the subsequent patch), as IO breakpoints are only valid to use when %cr4.de is enabled.
I haven't yet investigated the native behaviour of selecting an IO breakpoint, then disabling Debug Extensions.
That said, one of my TODO items is to allow PV guests to use General Detect (because its trivial to implement), at which point [5] will turn from an IO breakpoint shadow, into a more general %dr7 shadow, and ORing it in here will matter.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 1/3] x86/pv: Introduce and use x86emul_read_dr()
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 13.04.18 at 13:17, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
do_get_debugreg()has several bugs:* The %cr4.de condition is inverted. %dr4/5 should be accessible only when %cr4.de is disabled.
* When %cr4.de is disabled, emulation should yield #UD rather than complete with zero.
* Using-EINVALfor errors is a broken ABI, as it overlaps with valid values near the top of the address space.Quoted 5 lines
Quoted 3 lines
Introduce a common
x86emul_read_dr()handler (as we will eventually want to add HVM support) which separates its success/failure indication from the data value, and havedo_get_debugreg()call into the handler.The HVM part here is sort of questionable because of your use of curr->arch.pv_vcpu.ctrlreg[4].
Quoted 7 lines
That is what the "needs further plumbing" refers to, as well as needing hooks to get/modify %dr6/7 from the VMCB/VMCS.
However, we are gaining an increasing amount of common x86 code which needs to read control register values, and I've got a plan to refactor across the board to
v->arch.cr4(and similar). There is no point having identical information in different parts of sub-unions.
I agree.
Quoted 6 lines
Quoted 3 lines
This is appropriate for the NULL ctxt case,
but it's already a layering violation for the use of the function inpriv_op_ops, where theread_cr()hook should be used instead.Hmm - doing this, while probably the better long temr course of action, would require passing the ops structures down into the callbacks.
That doesn't sound like a problem, though - the hypercall path would pass NULL there as well.
Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
xen/arch/x86/x86_emulate.c +19
-0+int x86emul_read_dr (unsigned int reg,unsigned long *val, +struct x86_emulate_ctxt *ctxt) +{ +struct vcpu *curr = current; + +/* HVM support requires a bit more plumbing before it will work. */ +ASSERT (is_pv_vcpu (curr)); + +switch ( reg ) + { + case 0 ... 3: +case 6 : + *val = curr->arch.debugreg[reg]; +break ; + +case 7 : + *val = (curr->arch.debugreg[7 ] | + curr->arch.debugreg[5 ]); +break ;Quoted 8 lines
xen/arch/x86/x86_emulate.c int x86emul_write_xcr(unsigned int reg, uint64_t val, · line 116
Quoted 6 lines
xen/arch/x86/x86_emulate.c +6
-0+ + case 4 ... 5: +if ( !(curr->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) ) + { + *val = curr->arch.debugreg[reg +2 ]; +break ;Once at it, wouldn't you better also fix the missing ORing of [5] into the DR7 (really DR5) value here?
Quoted 2 lines
[5] is zero when %cr4.de is clear (subject to a bugfix in the subsequent patch), as IO breakpoints are only valid to use when %cr4.de is enabled.
Oh, right you are.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 1/3] x86/pv: Introduce and use x86emul_read_dr()
Quoted 61 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 13.04.18 at 13:17, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
do_get_debugreg()has several bugs:* The %cr4.de condition is inverted. %dr4/5 should be accessible only when %cr4.de is disabled.
* When %cr4.de is disabled, emulation should yield #UD rather than complete with zero.
* Using-EINVALfor errors is a broken ABI, as it overlaps with valid values near the top of the address space.Quoted 5 lines
Quoted 3 lines
Introduce a common
x86emul_read_dr()handler (as we will eventually want to add HVM support) which separates its success/failure indication from the data value, and havedo_get_debugreg()call into the handler.The HVM part here is sort of questionable because of your use of curr->arch.pv_vcpu.ctrlreg[4].
Quoted 7 lines
That is what the "needs further plumbing" refers to, as well as needing hooks to get/modify %dr6/7 from the VMCB/VMCS.
However, we are gaining an increasing amount of common x86 code which needs to read control register values, and I've got a plan to refactor across the board to
v->arch.cr4(and similar). There is no point having identical information in different parts of sub-unions.I agree.
Quoted 5 lines
Quoted 3 lines
This is appropriate for the NULL ctxt case,
but it's already a layering violation for the use of the function inpriv_op_ops, where theread_cr()hook should be used instead.Hmm - doing this, while probably the better long temr course of action, would require passing the ops structures down into the callbacks.
That doesn't sound like a problem, though - the hypercall path would pass NULL there as well.
Quoted 27 lines
Quoted 27 lines
Quoted 15 lines
xen/arch/x86/x86_emulate.c +15
-0+int x86emul_read_dr (unsigned int reg,unsigned long *val, +struct x86_emulate_ctxt *ctxt) +{ +struct vcpu *curr = current; + +/* HVM support requires a bit more plumbing before it will work. */ +ASSERT (is_pv_vcpu (curr)); + +switch ( reg ) + { + case 0 ... 3: +case 6 : + *val = curr->arch.debugreg[reg]; +break ; +xen/arch/x86/x86_emulate.c int x86emul_write_xcr(unsigned int reg, uint64_t val, · line 116
Quoted 10 lines
xen/arch/x86/x86_emulate.c +10
-0+case 7 : + *val = (curr->arch.debugreg[7 ] | + curr->arch.debugreg[5 ]); +break ; + + case 4 ... 5: +if ( !(curr->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) ) + { + *val = curr->arch.debugreg[reg +2 ]; +break ;Once at it, wouldn't you better also fix the missing ORing of [5] into the DR7 (really DR5) value here?
Quoted 3 lines
Quoted 2 lines
[5] is zero when %cr4.de is clear (subject to a bugfix in the subsequent patch), as IO breakpoints are only valid to use when %cr4.de is enabled.
Oh, right you are.
So, are your comments suitably addressed? It is unclear whether you want any changes to be made.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 1/3] x86/pv: Introduce and use x86emul_read_dr()
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 16.04.18 at 14:18, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 13.04.18 at 13:17, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
do_get_debugreg()has several bugs:* The %cr4.de condition is inverted. %dr4/5 should be accessible only when %cr4.de is disabled.
* When %cr4.de is disabled, emulation should yield #UD rather than complete with zero.
* Using-EINVALfor errors is a broken ABI, as it overlaps with valid values near the top of the address space.Quoted 5 lines
Quoted 3 lines
Introduce a common
x86emul_read_dr()handler (as we will eventually want to add HVM support) which separates its success/failure indication from the data value, and havedo_get_debugreg()call into the handler.The HVM part here is sort of questionable because of your use of curr->arch.pv_vcpu.ctrlreg[4].
Quoted 7 lines
That is what the "needs further plumbing" refers to, as well as needing hooks to get/modify %dr6/7 from the VMCB/VMCS.
However, we are gaining an increasing amount of common x86 code which needs to read control register values, and I've got a plan to refactor across the board to
v->arch.cr4(and similar). There is no point having identical information in different parts of sub-unions.I agree.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
This is appropriate for the NULL ctxt case,
but it's already a layering violation for the use of the function inpriv_op_ops, where theread_cr()hook should be used instead.Hmm - doing this, while probably the better long temr course of action, would require passing the ops structures down into the callbacks.
That doesn't sound like a problem, though - the hypercall path would pass NULL there as well.
This ...
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 15 lines
xen/arch/x86/x86_emulate.c +15
-0+int x86emul_read_dr (unsigned int reg,unsigned long *val, +struct x86_emulate_ctxt *ctxt) +{ +struct vcpu *curr = current; + +/* HVM support requires a bit more plumbing before it will work. */ +ASSERT (is_pv_vcpu (curr)); + +switch ( reg ) + { + case 0 ... 3: +case 6 : + *val = curr->arch.debugreg[reg]; +break ; +xen/arch/x86/x86_emulate.c int x86emul_write_xcr(unsigned int reg, uint64_t val, · line 116
Quoted 10 lines
xen/arch/x86/x86_emulate.c +10
-0+case 7 : + *val = (curr->arch.debugreg[7 ] | + curr->arch.debugreg[5 ]); +break ; + + case 4 ... 5: +if ( !(curr->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) ) + { + *val = curr->arch.debugreg[reg +2 ]; +break ;Once at it, wouldn't you better also fix the missing ORing of [5] into the DR7 (really DR5) value here?
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
[5] is zero when %cr4.de is clear (subject to a bugfix in the subsequent patch), as IO breakpoints are only valid to use when %cr4.de is enabled.
Oh, right you are.
So, are your comments suitably addressed? It is unclear whether you want any changes to be made.
... is what I'd prefer to be taken care of without delaying to the time when we make this work for HVM as well. Unless you feel really strongly about it being better the way you have it, in which case you may feel free to add my ack.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 1/3] x86/pv: Introduce and use x86emul_read_dr()
Quoted 66 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 16.04.18 at 14:18, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 13.04.18 at 13:17, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
do_get_debugreg()has several bugs:* The %cr4.de condition is inverted. %dr4/5 should be accessible only when %cr4.de is disabled.
* When %cr4.de is disabled, emulation should yield #UD rather than complete with zero.
* Using-EINVALfor errors is a broken ABI, as it overlaps with valid values near the top of the address space.Quoted 5 lines
Quoted 3 lines
Introduce a common
x86emul_read_dr()handler (as we will eventually want to add HVM support) which separates its success/failure indication from the data value, and havedo_get_debugreg()call into the handler.The HVM part here is sort of questionable because of your use of curr->arch.pv_vcpu.ctrlreg[4].
Quoted 7 lines
That is what the "needs further plumbing" refers to, as well as needing hooks to get/modify %dr6/7 from the VMCB/VMCS.
However, we are gaining an increasing amount of common x86 code which needs to read control register values, and I've got a plan to refactor across the board to
v->arch.cr4(and similar). There is no point having identical information in different parts of sub-unions.I agree.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
This is appropriate for the NULL ctxt case,
but it's already a layering violation for the use of the function inpriv_op_ops, where theread_cr()hook should be used instead.Hmm - doing this, while probably the better long temr course of action, would require passing the ops structures down into the callbacks.
That doesn't sound like a problem, though - the hypercall path would pass NULL there as well.
This ...
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 19 lines
Quoted 19 lines
xen/arch/x86/x86_emulate.c +19
-0+int x86emul_read_dr (unsigned int reg,unsigned long *val, +struct x86_emulate_ctxt *ctxt) +{ +struct vcpu *curr = current; + +/* HVM support requires a bit more plumbing before it will work. */ +ASSERT (is_pv_vcpu (curr)); + +switch ( reg ) + { + case 0 ... 3: +case 6 : + *val = curr->arch.debugreg[reg]; +break ; + +case 7 : + *val = (curr->arch.debugreg[7 ] | + curr->arch.debugreg[5 ]); +break ;Quoted 8 lines
xen/arch/x86/x86_emulate.c int x86emul_write_xcr(unsigned int reg, uint64_t val, · line 116
Quoted 6 lines
xen/arch/x86/x86_emulate.c +6
-0+ + case 4 ... 5: +if ( !(curr->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) ) + { + *val = curr->arch.debugreg[reg +2 ]; +break ;Once at it, wouldn't you better also fix the missing ORing of [5] into the DR7 (really DR5) value here?
[5] is zero when %cr4.de is clear (subject to a bugfix in the subsequent patch), as IO breakpoints are only valid to use when %cr4.de is enabled.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Oh, right you are.
So, are your comments suitably addressed? It is unclear whether you want any changes to be made.
... is what I'd prefer to be taken care of without delaying to the time when we make this work for HVM as well. Unless you feel really strongly about it being better the way you have it, in which case you may feel free to add my ack.
In all PV cases (hypercall and emulation), the current code functions correctly, because DE is active in context.
In principle, the emulation case would be better if it used the read_cr() hook, but that is invasive to arrange (which is why I chose not to at this point), and still needs special casing for the hypercall case anyway.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Jan Beulich
Acked-byRe: [PATCH 1/3] x86/pv: Introduce and use x86emul_read_dr()
Quoted 79 lines
Quoted 1 line
Quoted 1 line · On 16.04.18 at 18:44, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 16.04.18 at 14:18, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 13.04.18 at 13:17, <[email protected]> wrote:
On 13/04/18 09:31, Jan Beulich wrote:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
do_get_debugreg()has several bugs:* The %cr4.de condition is inverted. %dr4/5 should be accessible only when %cr4.de is disabled.
* When %cr4.de is disabled, emulation should yield #UD rather than complete with zero.
* Using-EINVALfor errors is a broken ABI, as it overlaps with validvalues
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
near the top of the address space.
Introduce a common
x86emul_read_dr()handler (as we will eventually want to add HVM support) which separates its success/failure indication from thedata
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 10 lines
Quoted 3 lines
Quoted 1 line
value, and have
do_get_debugreg()call into the handler.The HVM part here is sort of questionable because of your use of curr->arch.pv_vcpu.ctrlreg[4].
That is what the "needs further plumbing" refers to, as well as needing hooks to get/modify %dr6/7 from the VMCB/VMCS.
However, we are gaining an increasing amount of common x86 code which needs to read control register values, and I've got a plan to refactor across the board to
v->arch.cr4(and similar). There is no point having identical information in different parts of sub-unions.I agree.
Quoted 35 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
This is appropriate for the NULL ctxt case,
but it's already a layering violation for the use of the function inpriv_op_ops, where theread_cr()hook should be used instead.Hmm - doing this, while probably the better long temr course of action, would require passing the ops structures down into the callbacks.
That doesn't sound like a problem, though - the hypercall path would pass NULL there as well.
This ...
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 15 lines
(quoted diff) +15
-0+int x86emul_read_dr(unsigned int reg, unsigned long *val, + struct x86_emulate_ctxt *ctxt) +{ + struct vcpu *curr = current; + + /* HVM support requires a bit more plumbing before it will work. */ + ASSERT(is_pv_vcpu(curr)); + + switch ( reg ) + { + case 0 ... 3: + case 6: + *val = curr->arch.debugreg[reg]; + break; +Review
Quoted 10 lines
(quoted diff) +10
-0+ case 7: + *val = (curr->arch.debugreg[7] | + curr->arch.debugreg[5]); + break; + + case 4 ... 5: + if ( !(curr->arch.pv_vcpu.ctrlreg[4] & X86_CR4_DE) ) + { + *val = curr->arch.debugreg[reg + 2]; + break;Once at it, wouldn't you better also fix the missing ORing of [5] into the
DR7 (really
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
DR5) value here?
[5] is zero when %cr4.de is clear (subject to a bugfix in the subsequent patch), as IO breakpoints are only valid to use when %cr4.de is enabled.
Oh, right you are.
So, are your comments suitably addressed? It is unclear whether you want any changes to be made.
... is what I'd prefer to be taken care of without delaying to the time when we make this work for HVM as well. Unless you feel really strongly about it being better the way you have it, in which case you may feel free to add my ack.
Quoted 7 lines
In all PV cases (hypercall and emulation), the current code functions correctly, because DE is active in context.
In principle, the emulation case would be better if it used the
read_cr()hook, but that is invasive to arrange (which is why I chose not to at this point), and still needs special casing for the hypercall case anyway.
Well, okay then:
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 2/3 x86/pv: Introduce and use x86emul_write_dr()
In reply to Andrew Cooperset_debugreg() has several bugs:
* %dr4/5 should function correctly as aliases of %dr6/7 when CR4.DE is clear.
* Attempting to set the upper 32 bits of %dr6/7 should fail with #GP[0] rather than be silently corrected and complete.
* For emulation, the #UD and #GP[0] cases need properly distinguishing. Use -ENODEV for #UD cases, leaving -EINVAL (bad bits) and -EPERM (not allowed to use that valid bit) as before for hypercall callers.
* A write which clears %dr7.L/G leaves the IO shadow intact, meaning that subsequent reads of %dr7 will see stale IO watchpoint configuration.
Implement x86emul_write_dr() as a thin wrapper around set_debugreg().
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Juergen Gross <[email protected]>
--- xen/arch/x86/pv/emul-priv-op.c | 9 +-------- xen/arch/x86/traps.c | 32 +++++++++++++++++++++++++++++++- xen/arch/x86/x86_emulate.c | 24 ++++++++++++++++++++++++ xen/arch/x86/x86_emulate/x86_emulate.h | 2 ++ 4 files changed, 58 insertions(+), 9 deletions(-)
xen/arch/x86/pv/emul-priv-op.c +1 -8
@@ -794,13 +794,6 @@ static int write_cr(unsigned int reg, unsigned long val,return X86EMUL_UNHANDLEABLE ; } -static int write_dr (unsigned int reg,unsigned long val, -struct x86_emulate_ctxt *ctxt) -{ -return do_set_debugreg (reg, val) ==0 - ?X86EMUL_OKAY :X86EMUL_UNHANDLEABLE ; -} -static inline uint64_t guest_misc_enable (uint64_t val) { val &= ~(MSR_IA32_MISC_ENABLE_PERF_AVAIL |
@@ -1293,7 +1286,7 @@ static const struct x86_emulate_ops priv_op_ops = { .read_cr = read_cr, .write_cr = write_cr, .read_dr = x86emul_read_dr, - .write_dr = write_dr, + .write_dr = x86emul_write_dr, .write_xcr = x86emul_write_xcr, .read_msr = read_msr, .write_msr = write_msr,
xen/arch/x86/traps.c +31 -1
@@ -1998,6 +1998,12 @@ void activate_debugregs(const struct vcpu *curr) } } +/* +* Used by hypercalls and the emulator. +* -ENODEV => #UD +* -EINVAL => #GP Invalid bit +* -EPERM => #GP Valid bit, but not permitted to use +*/ long set_debugreg (struct vcpu *v,unsigned int reg,unsigned long value) {int i;
@@ -2029,7 +2035,17 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)if ( v == curr )write_debugreg (3 , value);break ; + +case 4 : +if ( v->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) +return -ENODEV ; + +/* Fallthrough */ case 6 : +/* The upper 32 bits are strictly reserved. */ +if ( value != (uint32_t )value ) +return -EINVAL ; +/* * DR6: Bits 4-11,16-31 reserved (set to 1). * Bit 12 reserved (set to 0).
@@ -2039,7 +2055,17 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)if ( v == curr )write_debugreg (6 , value);break ; + +case 5 : +if ( v->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) +return -ENODEV ; + +/* Fallthrough */ case 7 : +/* The upper 32 bits are strictly reserved. */ +if ( value != (uint32_t )value ) +return -EINVAL ; +/* * DR7: Bit 10 reserved (set to 1). * Bits 11-12,14-15 reserved (set to 0).
@@ -2052,6 +2078,10 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)*/ if ( value &DR_GENERAL_DETECT )return -EPERM ; + +/* Zero the IO shadow before recalculating the real %dr7 */ + v->arch.debugreg[5 ] =0 ; +/* DR7.{G,L}E = 0 => debugging disabled for this domain. */ if ( value &DR7_ACTIVE_MASK ) {
@@ -2084,7 +2114,7 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)write_debugreg (7 , value);break ;default : -return -EINVAL ; +return -ENODEV ; } v->arch.debugreg[reg] = value;
xen/arch/x86/x86_emulate.c +24 -0
@@ -15,6 +15,7 @@#include <asm/processor.h> /* current_cpu_info */ #include <asm/xstate.h> #include <asm/amd.h> /* cpu_has_amd_erratum() */ +#include <asm/debugreg.h> /* Avoid namespace pollution. */ #undef cmpxchg
@@ -127,6 +128,29 @@ int x86emul_read_dr(unsigned int reg, unsigned long *val,return X86EMUL_OKAY ; } +int x86emul_write_dr (unsigned int reg,unsigned long val, +struct x86_emulate_ctxt *ctxt) +{ +struct vcpu *curr = current; + +/* HVM support requires a bit more plumbing before it will work. */ +ASSERT (is_pv_vcpu (curr)); + +switch (set_debugreg (curr, reg, val) ) + { +case 0 : +return X86EMUL_OKAY ; + +case -ENODEV : +x86_emul_hw_exception (TRAP_invalid_op,X86_EVENT_NO_EC , ctxt); +return X86EMUL_EXCEPTION ; + +default : +x86_emul_hw_exception (TRAP_gp_fault,0 , ctxt); +return X86EMUL_EXCEPTION ; + } +} +/* * Local variables: * mode: C
xen/arch/x86/x86_emulate/x86_emulate.h +2 -0
@@ -724,6 +724,8 @@ int x86emul_write_xcr(unsigned int reg, uint64_t val,int x86emul_read_dr (unsigned int reg,unsigned long *val,struct x86_emulate_ctxt *ctxt); +int x86emul_write_dr (unsigned int reg,unsigned long val, +struct x86_emulate_ctxt *ctxt);#endif
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 2/3] x86/pv: Introduce and use x86emul_write_dr()
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
xen/arch/x86/traps.c +4
-0@@ -2029,7 +2035,17 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)if ( v == curr )write_debugreg (3 , value);break ; + +case 4 : +if ( v->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) +return -ENODEV ;
xen/arch/x86/traps.c long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value) · line 2051
Quoted 10 lines
xen/arch/x86/traps.c +6
-0+ +/* Fallthrough */ case 6 : +/* The upper 32 bits are strictly reserved. */ +if ( value != (uint32_t )value ) +return -EINVAL ; +/* * DR6: Bits 4-11,16-31 reserved (set to 1). * Bit 12 reserved (set to 0).
How are the upper 32 bits different from the other reserved bits (named in the comment visible here)?
Quoted 8 lines
xen/arch/x86/traps.c +4
-0@@ -2039,7 +2055,17 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)if ( v == curr )write_debugreg (6 , value);break ; + +case 5 : +if ( v->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) +return -ENODEV ;
xen/arch/x86/traps.c long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value) · line 2071
Quoted 10 lines
xen/arch/x86/traps.c +6
-0+ +/* Fallthrough */ case 7 : +/* The upper 32 bits are strictly reserved. */ +if ( value != (uint32_t )value ) +return -EINVAL ; +/* * DR7: Bit 10 reserved (set to 1). * Bits 11-12,14-15 reserved (set to 0).
Same here then.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 2/3] x86/pv: Introduce and use x86emul_write_dr()
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
xen/arch/x86/traps.c +7
-0@@ -2029,7 +2035,17 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)if ( v == curr )write_debugreg (3 , value);break ; + +case 4 : +if ( v->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) +return -ENODEV ; + +/* Fallthrough */ case 6 : +/* The upper 32 bits are strictly reserved. */
Quoted 8 lines
xen/arch/x86/traps.c long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value) · line 2051
Quoted 6 lines
xen/arch/x86/traps.c +3
-0+if ( value != (uint32_t )value ) +return -EINVAL ; +/* * DR6: Bits 4-11,16-31 reserved (set to 1). * Bit 12 reserved (set to 0). How are the upper 32 bits different from the other reserved bits (named in the comment visible here)?
The upper 32 bits are MBZ and will raise #GP if set. The lower reserved bits are write-ignored.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Jan Beulich
Reviewed-byRe: [PATCH 2/3] x86/pv: Introduce and use x86emul_write_dr()
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 13.04.18 at 13:37, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Review long set_debugreg(struct vcpu *v, unsigned int reg,
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
(quoted diff) +0
-0@@ -2029,7 +2035,17 @@ long set_debugreg(struct vcpu *v, unsigned int reg,unsigned long value)
Quoted 11 lines
Quoted 11 lines
xen/arch/x86/traps.c +7
-0if ( v == curr )write_debugreg (3 , value);break ; + +case 4 : +if ( v->arch.pv_vcpu.ctrlreg[4 ] &X86_CR4_DE ) +return -ENODEV ; + +/* Fallthrough */ case 6 : +/* The upper 32 bits are strictly reserved. */ Quoted 8 lines
xen/arch/x86/traps.c long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value) · line 2051
Quoted 6 lines
xen/arch/x86/traps.c +3
-0+if ( value != (uint32_t )value ) +return -EINVAL ; +/* * DR6: Bits 4-11,16-31 reserved (set to 1). * Bit 12 reserved (set to 0). How are the upper 32 bits different from the other reserved bits (named in the comment visible here)?
Quoted 2 lines
The upper 32 bits are MBZ and will raise #GP if set. The lower reserved bits are write-ignored.
Oh, indeed, I didn't recall that.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 3/3 x86/traps: Misc non-functional improvements to set_debugreg()
In reply to Andrew Cooper * Change 'int i' to being unsigned, and move it into its most narrow scope.
* Fold the access_ok() checks for %dr{0..3}. This halves the compiled size of the function.
* Additional newlines in appropriate places.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Juergen Gross <[email protected]>
--- xen/arch/x86/traps.c | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-)
xen/arch/x86/traps.c +13 -22
@@ -2006,34 +2006,24 @@ void activate_debugregs(const struct vcpu *curr)*/ long set_debugreg (struct vcpu *v,unsigned int reg,unsigned long value) { -int i;struct vcpu *curr = current;switch ( reg ) { -case 0 : -if ( !access_ok (value,sizeof (long )) ) -return -EPERM ; -if ( v == curr ) -write_debugreg (0 , value); -break ; -case 1 : -if ( !access_ok (value,sizeof (long )) ) -return -EPERM ; -if ( v == curr ) -write_debugreg (1 , value); -break ; -case 2 : -if ( !access_ok (value,sizeof (long )) ) -return -EPERM ; -if ( v == curr ) -write_debugreg (2 , value); -break ; -case 3 : + case 0 ... 3:if ( !access_ok (value,sizeof (long )) )return -EPERM ; +if ( v == curr ) -write_debugreg (3 , value); + { +switch ( reg ) + { +case 0 :write_debugreg (0 , value);break ; +case 1 :write_debugreg (1 , value);break ; +case 2 :write_debugreg (2 , value);break ; +case 3 :write_debugreg (3 , value);break ; + } + }break ;case 4 :
@@ -2085,7 +2075,7 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)/* DR7.{G,L}E = 0 => debugging disabled for this domain. */ if ( value &DR7_ACTIVE_MASK ) { -unsigned int io_enable =0 ; +unsigned int i, io_enable =0 ;for ( i =DR_CONTROL_SHIFT ; i <32 ; i +=DR_CONTROL_SIZE ) {
@@ -2113,6 +2103,7 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)if ( v == curr )write_debugreg (7 , value);break ; +default :return -ENODEV ; }
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 3/3] x86/traps: Misc non-functional improvements to set_debugreg()
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.04.18 at 18:55, <[email protected]> wrote:
* Change 'int i' to being unsigned, and move it into its most narrow scope.
* Fold theaccess_ok()checks for %dr{0..3}. This halves the compiled size of the function.
* Additional newlines in appropriate places.Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 5 lines · Andrew Cooper
At this point, I think these patches are plausible candidates for inclusion into 4.11. Whether they want backporting is a slightly harder matter, as these patches necesserily alter some error values for the get/set_debug_reg() hypercalls.
Quoted 7 lines
If however there is objection to these going into 4.11, they can sit in x86-next until the 4.12 window opens.
Andrew Cooper (3):
x86/pv: Introduce and usex86emul_read_dr()
x86/pv: Introduce and usex86emul_write_dr()x86/traps: Misc non-functional improvements toset_debugreg()
The complete series:
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel