Skip to contentSkip to conversation

vmx: Fix single step on debugger

PATCH In review Kouya Shimura 2 participants 5 messages Dec 19, 2008 – Dec 25, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vmx: Fix single step on debugger

The hvm domain which is being debugged sometimes crashes with the following message:

(XEN) Failed vm entry (exit reason 0x80000021) caused by invalid guest state (0). (XEN) ************* VMCS Area **************
(XEN) *** Guest State ***
(XEN) CR0: actual=0x000000008005003b, shadow=0x000000008005003b, gh_mask=ffffffffffffffff ...[snip]...
(XEN) DebugCtl=0000000000000000 DebugExceptions=0000000000000000
(XEN) Interruptibility=0001 ActivityState=0000
(XEN) *** Host State ***
(XEN) RSP = 0xffff828c8024ffa0 RIP = 0xffff828c801885b0
(XEN) CS=e008 DS=0000 ES=0000 FS=0000 GS=0000 SS=0000 TR=e040
(XEN) FSBase=0000000000000000 GSBase=0000000000000000 TRBase=ffff828c8028a200 (XEN) GDTBase=ffff828c800f3000 IDTBase=ffff828c8028db20
(XEN) CR0=000000008005003b CR3=00000001315b9000 CR4=00000000000026b0
(XEN) Sysenter RSP=ffff828c8024ffd0 CS:RIP=e008:ffff828c801af290
(XEN) *** Control State ***
(XEN) PinBased=0000003f CPUBased=b6a1e7fe SecondaryExec=00000041
(XEN) EntryControls=000013ff ExitControls=0003efff
(XEN) ExceptionBitmap=0004400a
(XEN) VMEntry: intr_info=00000031 errcode=00000004 ilen=00000000
(XEN) VMExit: intr_info=80000301 errcode=00000400 ilen=00000000
(XEN) reason=80000021 qualification=00000000
(XEN) IDTVectoring: info=00000000 errcode=00000000
(XEN) TPR Threshold = 0x00
(XEN) EPT pointer = 0x0000000000000000
(XEN) Virtual processor ID = 0x0000
(XEN) **************************************
(XEN) domain_crash called from vmx.c:2207
(XEN) Domain 14 (vcpu#0) crashed on cpu#0:

xen/arch/x86/hvm/vmx/vmx.c +33 -0
diff -r 8c35da364ab3 xen/arch/x86/hvm/vmx/vmx.c
@@ -1314,8 +1314,41 @@ static void vmx_set_uc_mode(struct vcpu 
 
 static void vmx_set_info_guest(struct vcpu *v)
 {
+    unsigned long intrblty;
+    unsigned long activity;
+    unsigned long debugctl;
+
     vmx_vmcs_enter(v);
+
     __vmwrite(GUEST_DR7, v->arch.guest_context.debugreg[7]);
+
+    /* 
+     * Setting X86_EFLAGS_TF may cause VM entry to fail.
+     * See SDM 3B 22.3.1.5.
+     */
+    if ( v->arch.guest_context.user_regs.eflags & X86_EFLAGS_TF )
+    {
+        intrblty = __vmread(GUEST_INTERRUPTIBILITY_INFO);
+        activity = __vmread(GUEST_ACTIVITY_STATE);
+
+        if ( (intrblty & (VMX_INTR_SHADOW_STI|VMX_INTR_SHADOW_MOV_SS)) ||
+             (activity == VMX_ACTIVITY_STATE_HLT) )
+        {
+            debugctl = __vmread(GUEST_IA32_DEBUGCTL);
+            debugctl |= VMX_IA32_DEBUGCTL_BTF;
+            __vmwrite(GUEST_IA32_DEBUGCTL, debugctl);
+        }
+    }
+    else
+    {
+        debugctl = __vmread(GUEST_IA32_DEBUGCTL);
+        if ( debugctl & VMX_IA32_DEBUGCTL_BTF )
+        {
+            debugctl &= ~VMX_IA32_DEBUGCTL_BTF;
+            __vmwrite(GUEST_IA32_DEBUGCTL, debugctl);
+        }
+    }
+
     vmx_vmcs_exit(v);
 }
 
xen/include/asm-x86/hvm/vmx/vmcs.h +10 -0
diff -r 8c35da364ab3 xen/include/asm-x86/hvm/vmx/vmcs.h
@@ -196,6 +196,16 @@ extern bool_t cpu_has_vmx_ins_outs_instr
 #define VMX_INTR_SHADOW_SMI             0x00000004
 #define VMX_INTR_SHADOW_NMI             0x00000008
 
+enum guest_activity_state {
+    VMX_ACTIVITY_STATE_ACTIVE        = 0,
+    VMX_ACTIVITY_STATE_HLT           = 1,
+    VMX_ACTIVITY_STATE_SHUTDOWN      = 2,
+    VMX_ACTIVITY_STATE_WAIT_FOR_SIPI = 3,
+};
+
+/* GUEST_IA32_DEBUGCTL flags */
+#define VMX_IA32_DEBUGCTL_BTF           0x00000002 // single-step on branches
+
 /* VMCS field encodings. */
 enum vmcs_field {
     VIRTUAL_PROCESSOR_ID            = 0x00000000,

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 2 lines · Kouya Shimura

The hvm domain which is being debugged sometimes crashes with the following message:

The fix looks like a dubious hack to me. Wouldn't it at least be a bit cleaner to mess with DR6.BS (the flag that's actually being checked by the CPU) rather than messing with the DEBUGCTL.BTF control flag?

-- Keir

#2 lore raw

Indeed, this fix is hacky but it works well for me.

At first, I tried setting GUEST_PENDING_DBG_EXCEPTIONS.BS according to the SDM. As a result, the guest doesn't be killed but never go progress after the STI instruction
(i.e. VM exit immediately after VM entry) as long as RFLAGS.TF=1. That is inconvenient for debugging.

Is there any other way?

Thanks,
Kouya

Quoted 11 lines · Keir Fraser
Quoted 2 lines · Kouya Shimura

The hvm domain which is being debugged sometimes crashes with the following message:

The fix looks like a dubious hack to me. Wouldn't it at least be a bit cleaner to mess with DR6.BS (the flag that's actually being checked by the CPU) rather than messing with the DEBUGCTL.BTF control flag?

-- Keir

#3 lore raw
Quoted 9 lines · Kouya Shimura

Indeed, this fix is hacky but it works well for me.

At first, I tried setting GUEST_PENDING_DBG_EXCEPTIONS.BS according to the SDM. As a result, the guest doesn't be killed but never go progress after the STI instruction
(i.e. VM exit immediately after VM entry) as long as RFLAGS.TF=1. That is inconvenient for debugging.

Is there any other way?

If you make it conditional on debugger_attached then I guess you can do what you like. I strongly dislike it though.

-- Keir

#4 lore raw
Quoted 10 lines · Keir Fraser
Quoted 8 lines · Kouya Shimura

Indeed, this fix is hacky but it works well for me.

At first, I tried setting GUEST_PENDING_DBG_EXCEPTIONS.BS according to the SDM. As a result, the guest doesn't be killed but never go progress after the STI instruction
(i.e. VM exit immediately after VM entry) as long as RFLAGS.TF=1. That is inconvenient for debugging.

Quoted 7 lines
Quoted 1 line

Is there any other way?

If you make it conditional on debugger_attached then I guess you can do what you like. I strongly dislike it though.

-- Keir

I read the SDM carefully, however, I can't find any solution for this. Although the monitor trap flag might help, my machine doesn't have such a feature.

At least, crashing the guest should be avoided.
Attached is the updated patch. I think it might become a little better.

Thanks,
Kouya

xen/arch/x86/hvm/vmx/vmx.c +21 -0
diff -r e2f36d066b7b xen/arch/x86/hvm/vmx/vmx.c
@@ -1314,8 +1314,29 @@
 
 static void vmx_set_info_guest(struct vcpu *v)
 {
+    unsigned long intr_shadow;
+
     vmx_vmcs_enter(v);
+
     __vmwrite(GUEST_DR7, v->arch.guest_context.debugreg[7]);
+
+    /* 
+     * If the interruptibility-state field indicates blocking by STI,
+     * setting the TF flag in the EFLAGS may cause VM entry to fail
+     * and crash the guest. See SDM 3B 22.3.1.5.
+     * Resetting the VMX_INTR_SHADOW_STI flag looks hackish but
+     * to set the GUEST_PENDING_DBG_EXCEPTIONS.BS here incurs
+     * immediately vmexit and hence make no progress.
+     */
+    intr_shadow = __vmread(GUEST_INTERRUPTIBILITY_INFO);
+    if ( v->domain->debugger_attached &&
+         (v->arch.guest_context.user_regs.eflags & X86_EFLAGS_TF) &&
+         (intr_shadow & VMX_INTR_SHADOW_STI) )
+    {
+        intr_shadow &= ~VMX_INTR_SHADOW_STI;
+        __vmwrite(GUEST_INTERRUPTIBILITY_INFO, intr_shadow);
+    }
+
     vmx_vmcs_exit(v);
 }
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel