Debug-Registers in HVM domain destroyed
Hi,
we've found a problem with debug registers in HVM domains with Xen (we are running 4.2, but the code in the hypervisor seems to be unchanged in unstable) on INTEL processors:
Debug registers are restored on vcpu switch only if db7 has any debug events activated. This leads to problems in the following cases:
- db0-3 are changed by the guest before events are set "active" in db7. In case of a vcpu switch between setting db0-3 and db7, db0-3 are lost. BTW: setting db7 before db0-3 is no option, as this could trigger debug interrupts due to stale db0-3 contents.
- single stepping is used and vcpu switch occurs between the single step trap and reading of db6 in the guest. db6 contents (single step indicator) are lost in this case.
Any thoughts?
Juergen
Signature · 5 lines
Juergen Gross Principal Developer Operating Systems PBG PDG ES&S SWE OS6 Telephone: +49 (0) 89 62060 2932 Fujitsu e-mail: [email protected] Mies-van-der-Rohe-Str. 8 Internet: ts.fujitsu.com D-80807 Muenchen Company details: ts.fujitsu.com/imprint.html
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Debug registers are restored on vcpu switch only if db7 has any debug events activated. This leads to problems in the following cases:
Quoted 8 lines
- db0-3 are changed by the guest before events are set "active" in db7. In case of a vcpu switch between setting db0-3 and db7, db0-3 are lost. BTW: setting db7 before db0-3 is no option, as this could trigger debug interrupts due to stale db0-3 contents.
- single stepping is used and vcpu switch occurs between the single step trap and reading of db6 in the guest. db6 contents (single step indicator) are lost in this case.
Not exactly, at least not looking at how things are supposed to work:__restore_debug_registers() gets called when
- context switching in (vmx_restore_dr())
- injecting TRAP_debug
- any DRn is being accessed
So when your guest writes DR[0-3], debug registers should get restored (from their original zero values) and the guest would be permitted direct access to the hardware registers. Once context switched out, vmx_save_dr() ought to be saving the values (irrespective of DR7 contents, only depending upon v->arch.hvm_vcpu.flag_dr_dirty). During the next context switch in, they would get restored immediately if DR7 already has some breakpoint enabled, or again during first DR access if not.
Hence I think that in general this ought to work. Question is whether one of the more modern feature additions broke any of this. Assuming that your guest isn't doing heavy accesses to the debug registers, instrumenting the hypervisor side code to track the saving/restoring shouldn't be causing too much log output (as long as you suppress output from the context switch path when no relevant state changed).
Jan
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Debug registers are restored on vcpu switch only if db7 has any debug events activated. This leads to problems in the following cases:
- db0-3 are changed by the guest before events are set "active" in db7. In case of a vcpu switch between setting db0-3 and db7, db0-3 are lost. BTW: setting db7 before db0-3 is no option, as this could trigger debug interrupts due to stale db0-3 contents.
Quoted 8 lines
Quoted 3 lines
- single stepping is used and vcpu switch occurs between the single step trap and reading of db6 in the guest. db6 contents (single step indicator) are lost in this case.
Not exactly, at least not looking at how things are supposed to work:
__restore_debug_registers()gets called when
- context switching in (vmx_restore_dr())
- injectingTRAP_debug
Is this the case when the guest itself uses single stepping? Initially the debug trap shouldn't cause a VMEXIT, I think. And I'm not sure the hypervisor will see a guest setting TF via an IRET.
I _have_ seen a debug trap in the guest after single stepping without db6 having the single step indicator set...
Quoted 10 lines
- any DRn is being accessed
So when your guest writes DR[0-3], debug registers should get restored (from their original zero values) and the guest would be permitted direct access to the hardware registers. Once context switched out,
vmx_save_dr()ought to be saving the values (irrespective of DR7 contents, only depending uponv->arch.hvm_vcpu.flag_dr_dirty). During the next context switch in, they would get restored immediately if DR7 already has some breakpoint enabled, or again during first DR access if not.
Okay, I'll check that. A little test routine in my domU should be able to verify that debug registers won't change under it's feet in case of no activated events in db7.
Juergen
Signature · 5 lines
Juergen Gross Principal Developer Operating Systems PBG PDG ES&S SWE OS6 Telephone: +49 (0) 89 62060 2932 Fujitsu e-mail: [email protected] Mies-van-der-Rohe-Str. 8 Internet: ts.fujitsu.com D-80807 Muenchen Company details: ts.fujitsu.com/imprint.html
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Debug registers are restored on vcpu switch only if db7 has any debug events activated. This leads to problems in the following cases:
- db0-3 are changed by the guest before events are set "active" in db7. In
case
Quoted 1 line
Quoted 1 line
of a vcpu switch between setting db0-3 and db7, db0-3 are lost. BTW:
setting
Quoted 1 line
Quoted 1 line
db7 before db0-3 is no option, as this could trigger debug interrupts due
to
Quoted 4 lines
Quoted 4 lines
stale db0-3 contents.
- single stepping is used and vcpu switch occurs between the single step trap and reading of db6 in the guest. db6 contents (single step indicator)
are
Quoted 2 lines
Quoted 1 line
lost in this case.
Quoted 7 lines
Quoted 4 lines
Not exactly, at least not looking at how things are supposed to work:
__restore_debug_registers()gets called when
- context switching in (vmx_restore_dr())
- injectingTRAP_debugIs this the case when the guest itself uses single stepping? Initially the debug trap shouldn't cause a VMEXIT, I think.
That looks like a bug, indeed - it's missing from the initially set exception_bitmap. Could you check whether adding this in construct_vmcs() addresses that part of the issue? (A proper fix would likely include further adjustments to the setting of this flag, e.g. clearing it alongside clearing the DR intercept.) But then again all of this already depends on cpu_has_monitor_trap_flag - if that's set on your system, maybe you could try suppressing its detection (by removing CPU_BASED_MONITOR_TRAP_FLAG from the optional feature set in vmx_init_vmcs_config())?
Quoted 2 lines
And I'm not sure the
hypervisor will see a guest setting TF via an IRET.
It shouldn't need to know of this.
Jan
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Debug registers are restored on vcpu switch only if db7 has any debug events activated. This leads to problems in the following cases:
- db0-3 are changed by the guest before events are set "active" in db7. In
case
Quoted 1 line
Quoted 1 line
of a vcpu switch between setting db0-3 and db7, db0-3 are lost. BTW:
setting
Quoted 1 line
Quoted 1 line
db7 before db0-3 is no option, as this could trigger debug interrupts due
to
Quoted 4 lines
Quoted 4 lines
stale db0-3 contents.
- single stepping is used and vcpu switch occurs between the single step trap and reading of db6 in the guest. db6 contents (single step indicator)
Quoted 7 lines
Quoted 7 lines
are
Quoted 6 lines
Quoted 1 line
lost in this case.
Not exactly, at least not looking at how things are supposed to work:
__restore_debug_registers()gets called when
- context switching in (vmx_restore_dr())
- injectingTRAP_debug
Okay, db0-3 seem to be preserved. I did a test modifying the registers without activating any debug traps. Even under heavy vcpu scheduling load everything was fine.
Quoted 4 lines
Quoted 3 lines
Is this the case when the guest itself uses single stepping? Initially the debug trap shouldn't cause a VMEXIT, I think.
Quoted 9 lines
That looks like a bug, indeed - it's missing from the initially set
exception_bitmap. Could you check whether adding this inconstruct_vmcs()addresses that part of the issue? (A proper fix would likely include further adjustments to the setting of this flag, e.g. clearing it alongside clearing the DR intercept.) But then again all of this already depends oncpu_has_monitor_trap_flag- if that's set on your system, maybe you could try suppressing its detection (by removingCPU_BASED_MONITOR_TRAP_FLAGfrom the optional feature set invmx_init_vmcs_config())?
I've currently a test running with the attached patch (the bug was hit about once every 3 hours, test is running now for about 4 hours without problem). Test machine is running with Xen 4.2.3 hypervisor from SLES11 SP3.
Juergen
Signature · 5 lines
Juergen Gross Principal Developer Operating Systems PBG PDG ES&S SWE OS6 Telephone: +49 (0) 89 62060 2932 Fujitsu e-mail: [email protected] Mies-van-der-Rohe-Str. 8 Internet: ts.fujitsu.com D-80807 Muenchen Company details: ts.fujitsu.com/imprint.html
xen/include/asm-x86/hvm/hvm.h +2 -1
@@ -374,7 +374,8 @@ static inline int hvm_do_pmu_interrupt(s (cpu_has_xsave ? X86_CR4_OSXSAVE : 0))))/* These exceptions must always be intercepted. */ -#define HVM_TRAP_MASK ((1U << TRAP_machine_check) | (1U << TRAP_invalid_op)) +#define HVM_TRAP_MASK ((1U << TRAP_machine_check) | (1U << TRAP_invalid_op) |\ + (1 << TRAP_debug))/* * x86 event types. This enumeration is valid for:
xen/arch/x86/hvm/vmx/vmcs.c +1 -1
@@ -168,7 +168,7 @@ static int vmx_init_vmcs_config(void) CPU_BASED_RDTSC_EXITING); opt = (CPU_BASED_ACTIVATE_MSR_BITMAP |CPU_BASED_TPR_SHADOW | -CPU_BASED_MONITOR_TRAP_FLAG | +/* CPU_BASED_MONITOR_TRAP_FLAG | */ CPU_BASED_ACTIVATE_SECONDARY_CONTROLS ); _vmx_cpu_based_exec_control = adjust_vmx_controls( "CPU-Based Exec Control", min, opt,
xen/arch/x86/hvm/vmx/vmx.c +5 -1
@@ -2646,7 +2646,11 @@ void vmx_vmexit_handler(struct cpu_user_HVMTRACE_1D (TRAP_DEBUG , exit_qualification);write_debugreg (6 , exit_qualification |0xffff0ff0 );if ( !v->domain->debugger_attached || cpu_has_monitor_trap_flag ) - goto exit_and_crash; + { +__restore_debug_registers (v); +hvm_inject_hw_exception (TRAP_debug,HVM_DELIVER_NO_ERROR_CODE ); +break ; + }domain_pause_for_debugger ();break ;case TRAP_int3:
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Quoted 4 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Is this the case when the guest itself uses single stepping? Initially the debug trap shouldn't cause a VMEXIT, I think.
Quoted 9 lines
That looks like a bug, indeed - it's missing from the initially set
exception_bitmap. Could you check whether adding this inconstruct_vmcs()addresses that part of the issue? (A proper fix would likely include further adjustments to the setting of this flag, e.g. clearing it alongside clearing the DR intercept.) But then again all of this already depends oncpu_has_monitor_trap_flag- if that's set on your system, maybe you could try suppressing its detection (by removingCPU_BASED_MONITOR_TRAP_FLAGfrom the optional feature set invmx_init_vmcs_config())?
Quoted 3 lines
I've currently a test running with the attached patch (the bug was hit about once every 3 hours, test is running now for about 4 hours without problem). Test machine is running with Xen 4.2.3 hypervisor from SLES11 SP3.
Which, if it continues running fine, would confirm the theory. I'd like to defer to the VMX folks though for putting together a proper fix then - I'd likely overlook some corner case.
Jan
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Quoted 13 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Is this the case when the guest itself uses single stepping? Initially the debug trap shouldn't cause a VMEXIT, I think.
That looks like a bug, indeed - it's missing from the initially set
exception_bitmap. Could you check whether adding this inconstruct_vmcs()addresses that part of the issue? (A proper fix would likely include further adjustments to the setting of this flag, e.g. clearing it alongside clearing the DR intercept.) But then again all of this already depends oncpu_has_monitor_trap_flag- if that's set on your system, maybe you could try suppressing its detection (by removingCPU_BASED_MONITOR_TRAP_FLAGfrom the optional feature set invmx_init_vmcs_config())?
Quoted 7 lines
Quoted 3 lines
I've currently a test running with the attached patch (the bug was hit about once every 3 hours, test is running now for about 4 hours without problem). Test machine is running with Xen 4.2.3 hypervisor from SLES11 SP3.
Which, if it continues running fine, would confirm the theory. I'd like to defer to the VMX folks though for putting together a proper fix then - I'd likely overlook some corner case.
Okay, theory confirmed.
Unless you want to do it, I'll start another thread with the info found so far and include Jun Nakajima and Eddie Dong.
Juergen
Signature · 5 lines
Juergen Gross Principal Developer Operating Systems PBG PDG ES&S SWE OS6 Telephone: +49 (0) 89 62060 2932 Fujitsu e-mail: [email protected] Mies-van-der-Rohe-Str. 8 Internet: ts.fujitsu.com D-80807 Muenchen Company details: ts.fujitsu.com/imprint.html
Quoted 22 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Quoted 20 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Quoted 4 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · Juergen Gross
Is this the case when the guest itself uses single stepping? Initially the debug trap shouldn't cause a VMEXIT, I think.
Quoted 9 lines
That looks like a bug, indeed - it's missing from the initially set
exception_bitmap. Could you check whether adding this inconstruct_vmcs()addresses that part of the issue? (A proper fix would likely include further adjustments to the setting of this flag, e.g. clearing it alongside clearing the DR intercept.) But then again all of this already depends oncpu_has_monitor_trap_flag- if that's set on your system, maybe you could try suppressing its detection (by removingCPU_BASED_MONITOR_TRAP_FLAGfrom the optional feature set invmx_init_vmcs_config())?I've currently a test running with the attached patch (the bug was hit about once every 3 hours, test is running now for about 4 hours without problem). Test machine is running with Xen 4.2.3 hypervisor from SLES11 SP3.
Quoted 8 lines
Quoted 3 lines
Which, if it continues running fine, would confirm the theory. I'd like to defer to the VMX folks though for putting together a proper fix then - I'd likely overlook some corner case.
Okay, theory confirmed.
Unless you want to do it, I'll start another thread with the info found so far and include Jun Nakajima and Eddie Dong.
Please do. And perhaps also include Yang Z Zhang.
Jan