Skip to contentSkip to conversation

x86/vmx: fix dr6 handling issue

PATCH In review Yang Zhang 2 participants 3 messages May 13, 2014 – May 15, 2014

lore
Share & tools
lore

Yang Zhang

author
#0 lore raw

PATCH 1/1 x86/vmx: fix dr6 handling issue

From: Yang Zhang <[email protected]>

Guest's dr6 may lost in some corner case. For example:
1. Guest set TF bit to enable single step.
2. Debug trap occurs and delivered to guest direclty.
3. VCPU is scheduled out and dr6 is not saved due to flag_debug_dirty is not set and db7 has no debug event enabled.
4. After VCPU is scheduled in agagin, db6 may lost.

With this patch, hypervisor will intercept the first debug trap after vcpu is scheduled in and restore dr6 in debug_trap handler.

---

I don't have the environment to test this patch. So Juergen can you help to verify it? Thanks.

 xen/arch/x86/hvm/vmx/vmx.c     |   37 +++++++++++++++++++++++++------------
 xen/arch/x86/traps.c           |    4 ++--
 xen/include/asm-x86/debugreg.h |    3 +++
 xen/include/asm-x86/hvm/hvm.h  |    3 ++-
 4 files changed, 32 insertions(+), 15 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +25 -12
index b6c022b..520098f 100644
@@ -376,6 +376,10 @@ static void vmx_save_dr(struct vcpu *v)
     v->arch.hvm_vmx.exec_control |= CPU_BASED_MOV_DR_EXITING;
     vmx_update_cpu_exec_control(v);
 
+    /* Trap debug for signle stepping. */
+    v->arch.hvm_vmx.exception_bitmap |= 1 << TRAP_debug;
+    vmx_update_exception_bitmap(v);
+
     v->arch.debugreg[0] = read_debugreg(0);
     v->arch.debugreg[1] = read_debugreg(1);
     v->arch.debugreg[2] = read_debugreg(2);
@@ -392,6 +396,13 @@ static void __restore_debug_registers(struct vcpu *v)
 
     v->arch.hvm_vcpu.flag_dr_dirty = 1;
 
+    /* 
+     * After restore, hardware has the right context.
+     * So no need to trap debug anymore.
+     */
+    v->arch.hvm_vmx.exception_bitmap &= ~(1 << TRAP_debug);
+    vmx_update_exception_bitmap(v);
+
     write_debugreg(0, v->arch.debugreg[0]);
     write_debugreg(1, v->arch.debugreg[1]);
     write_debugreg(2, v->arch.debugreg[2]);
@@ -1178,16 +1189,10 @@ static void vmx_update_host_cr3(struct vcpu *v)
 
 void vmx_update_debug_state(struct vcpu *v)
 {
-    unsigned long mask;
-
-    mask = 1u << TRAP_int3;
-    if ( !cpu_has_monitor_trap_flag )
-        mask |= 1u << TRAP_debug;
-
     if ( v->arch.hvm_vcpu.debug_state_latch )
-        v->arch.hvm_vmx.exception_bitmap |= mask;
+        v->arch.hvm_vmx.exception_bitmap |= 1u << TRAP_int3;
     else
-        v->arch.hvm_vmx.exception_bitmap &= ~mask;
+        v->arch.hvm_vmx.exception_bitmap &= ~(1u << TRAP_int3);
 
     vmx_vmcs_enter(v);
     vmx_update_exception_bitmap(v);
@@ -2717,10 +2722,18 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
              */
             __vmread(EXIT_QUALIFICATION, &exit_qualification);
             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;
-            domain_pause_for_debugger();
+            exit_qualification |= DR_STATUS_RESERVED_ONE;
+            if ( v->domain->debugger_attached )
+            {
+                write_debugreg(6, exit_qualification);
+                domain_pause_for_debugger();
+            }
+            else 
+            {
+                v->arch.debugreg[6] |= exit_qualification;
+                __restore_debug_registers(v);
+                hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
+            }
             break;
         case TRAP_int3: 
         {
xen/arch/x86/traps.c +2 -2
index 40366f1..e86ca2c 100644
@@ -3763,8 +3763,8 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)
          * DR6: Bits 4-11,16-31 reserved (set to 1).
          *      Bit 12 reserved (set to 0).
          */
-        value &= 0xffffefff; /* reserved bits => 0 */
-        value |= 0xffff0ff0; /* reserved bits => 1 */
+        value &= ~DR_STATUS_RESERVED_ZERO;  /* reserved bits => 0 */
+        value |= DR_STATUS_RESERVED_ONE;    /* reserved bits => 1 */
         if ( v == curr ) 
             write_debugreg(6, value);
         break;
xen/include/asm-x86/debugreg.h +3 -0
index 62007bb..4a9a7d6 100644
@@ -76,4 +76,7 @@
 long set_debugreg(struct vcpu *, unsigned int reg, unsigned long value);
 void activate_debugregs(const struct vcpu *);
 
+#define DR_STATUS_RESERVED_ZERO  (0x00001000ul) /* Reserved, read as zero */
+#define DR_STATUS_RESERVED_ONE   (0xffff0ff0ul) /* Reserved, read as one */
+
 #endif /* _X86_DEBUGREG_H */
xen/include/asm-x86/hvm/hvm.h +2 -1
index b5a56e0..eb73da7 100644
@@ -420,7 +420,8 @@ static inline bool_t hvm_vcpu_has_smap(void)
         (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) |\
+                       (1U << TRAP_debug))
 
 /*
  * x86 event types. This enumeration is valid for:
Signature · 1 line
1.7.1
#1 lore raw

xen/arch/x86/hvm/vmx/vmx.c static void vmx_update_host_cr3(struct vcpu *v) · line 1185

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 13.05.14 at 10:00, <[email protected]> wrote:
xen/arch/x86/hvm/vmx/vmx.c +0 -5
@@ -1178,16 +1189,10 @@ static void vmx_update_host_cr3(struct vcpu *v)
 
 void vmx_update_debug_state(struct vcpu *v)
 {
-    unsigned long mask;
-
-    mask = 1u << TRAP_int3;
-    if ( !cpu_has_monitor_trap_flag )
-        mask |= 1u << TRAP_debug;

So how are things going to work now when !cpu_has_monitor_trap_flag?

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -4
@@ -2717,10 +2722,18 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
              */
             __vmread(EXIT_QUALIFICATION, &exit_qualification);
             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;
-            domain_pause_for_debugger();
+            exit_qualification |= DR_STATUS_RESERVED_ONE;
+            if ( v->domain->debugger_attached )

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2736

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10 -0
+            {
+                write_debugreg(6, exit_qualification);
+                domain_pause_for_debugger();
+            }
+            else 
+            {
+                v->arch.debugreg[6] |= exit_qualification;
+                __restore_debug_registers(v);
+                hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
+            }

I.e. is that really the only thing that needs doing when there' no MTF?

xen/arch/x86/traps.c long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value) · line 3766

Quoted 9 lines
xen/arch/x86/traps.c +1 -2
@@ -3763,8 +3763,8 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)
          * DR6: Bits 4-11,16-31 reserved (set to 1).
          *      Bit 12 reserved (set to 0).
          */
-        value &= 0xffffefff; /* reserved bits => 0 */
-        value |= 0xffff0ff0; /* reserved bits => 1 */
+        value &= ~DR_STATUS_RESERVED_ZERO;  /* reserved bits => 0 */

This is not equivalent to the old code, ...

xen/include/asm-x86/debugreg.h line 79

Quoted 7 lines
xen/include/asm-x86/debugreg.h +1 -0
@@ -76,4 +76,7 @@
 long set_debugreg(struct vcpu *, unsigned int reg, unsigned long value);
 void activate_debugregs(const struct vcpu *);
 
+#define DR_STATUS_RESERVED_ZERO  (0x00001000ul) /* Reserved, read as zero */

... because here you're introducing a problem similar to the one which was the subject of XSA-12. This needs to be

#define DR_STATUS_RESERVED_ZERO (~0xffffeffful) /* Reserved, read as zero */

matching DR_CONTROL_RESERVED_ZERO.

Jan

#2 lore raw

Jan Beulich wrote on 2014-05-13:

Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 13.05.14 at 10:00, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -1178,16 +1189,10 @@ static void vmx_update_host_cr3(struct vcpu
 *v)
 
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_update_host_cr3(struct vcpu *v) · line 1185

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -5
 void vmx_update_debug_state(struct vcpu *v)  {
-    unsigned long mask;
-
-    mask = 1u << TRAP_int3;
-    if ( !cpu_has_monitor_trap_flag )
-        mask |= 1u << TRAP_debug;

So how are things going to work now when !cpu_has_monitor_trap_flag?

Umm.. it seems wrong in such case.

Quoted 19 lines

Review void vmx_vmexit_handler(struct cpu_user_regs

Quoted 2 lines
(quoted diff) +0 -0
@@ -2717,10 +2722,18 @@ void vmx_vmexit_handler(struct cpu_user_regs

*regs)

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +0 -2
              */
             __vmread(EXIT_QUALIFICATION, &exit_qualification);
             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; -           

domain_pause_for_debugger(); + exit_qualification |=
DR_STATUS_RESERVED_ONE; + if ( v->domain->debugger_attached
) + { + write_debugreg(6,
exit_qualification); + domain_pause_for_debugger(); +

Quoted 5 lines
        } +            else +            { +               
v->arch.debugreg[6] |= exit_qualification; +               
__restore_debug_registers(v); +               
hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE); +      
     }
Quoted 1 line

I.e. is that really the only thing that needs doing when there' no MTF?

Previously, I think it is enough. But after more thoughts, I am not sure whether it will cause any problem if guest is also using debugging mechanism while host is debugging the guest itself. Need more investigations.

Quoted 28 lines

xen/arch/x86/traps.c long set_debugreg(struct vcpu *v, unsigned int · line 3763

Quoted 5 lines
xen/arch/x86/traps.c +0 -0
@@ -3763,8 +3763,8 @@ long set_debugreg(struct vcpu *v, unsigned int
 reg,

unsigned long value)

xen/arch/x86/traps.c long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)

Quoted 6 lines
xen/arch/x86/traps.c +1 -2
          * DR6: Bits 4-11,16-31 reserved (set to 1).
          *      Bit 12 reserved (set to 0).
          */
-        value &= 0xffffefff; /* reserved bits => 0 */
-        value |= 0xffff0ff0; /* reserved bits => 1 */
+        value &= ~DR_STATUS_RESERVED_ZERO;  /* reserved bits => 0

*/

This is not equivalent to the old code, ...

xen/include/asm-x86/debugreg.h line 80

Quoted 8 lines
xen/include/asm-x86/debugreg.h +2 -0
@@ -76,4 +76,7 @@
 long set_debugreg(struct vcpu *, unsigned int reg, unsigned long
 value);  void activate_debugregs(const struct vcpu *);
 
+#define DR_STATUS_RESERVED_ZERO  (0x00001000ul) /* Reserved, read as
+zero */

... because here you're introducing a problem similar to the one which was the subject of XSA-12. This needs to be

Quoted 6 lines

#define DR_STATUS_RESERVED_ZERO (~0xffffeffful) /* Reserved, read as zero */

matching DR_CONTROL_RESERVED_ZERO.

Jan

Good point.

Best regards,
Yang