Index: /trunk/src/VBox/VMM/VMMR0/HMR0.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMR0/HMR0.cpp	(revision 47802)
+++ /trunk/src/VBox/VMM/VMMR0/HMR0.cpp	(revision 47803)
@@ -1482,5 +1482,4 @@
  * @returns VBox status code.
  * @param   pVCpu       Pointer to the VMCPU.
- * @param   idCpu       The identifier for the CPU the function is called on.
  */
 VMMR0_INT_DECL(int) HMR0LeaveEx(PVMCPU pVCpu)
Index: /trunk/src/VBox/VMM/VMMR0/HMSVMR0.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMR0/HMSVMR0.cpp	(revision 47802)
+++ /trunk/src/VBox/VMM/VMMR0/HMSVMR0.cpp	(revision 47803)
@@ -1620,4 +1620,5 @@
             Assert(pVCpu->hm.s.fContextUseFlags & (HM_CHANGED_HOST_CONTEXT | HM_CHANGED_GUEST_CR0));
 
+            pVCpu->hm.s.fLeaveDone = false;
             RTThreadPreemptRestore(&PreemptState);
             break;
@@ -1895,50 +1896,47 @@
         RTThreadPreemptDisable(&PreemptState);
         fPreemptDisabled = true;
-        if (pVCpu->hm.s.fLeaveDone)
-        {
-            RTThreadPreemptRestore(&PreemptState);
-            return;
-        }
-    }
-
-    /* Restore host FPU state if necessary and resync on next R0 reentry .*/
-    if (CPUMIsGuestFPUStateActive(pVCpu))
-    {
-        CPUMR0SaveGuestFPU(pVM, pVCpu, pCtx);
-        Assert(!CPUMIsGuestFPUStateActive(pVCpu));
-        pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_GUEST_CR0;
-    }
-
-    /*
-     * Restore host debug registers if necessary and resync on next R0 reentry.
-     */
-#ifdef VBOX_STRICT
-    if (CPUMIsHyperDebugStateActive(pVCpu))
-    {
-        PSVMVMCB pVmcb = (PSVMVMCB)pVCpu->hm.s.svm.pvVmcb;
-        Assert(pVmcb->ctrl.u16InterceptRdDRx == 0xffff);
-        Assert(pVmcb->ctrl.u16InterceptWrDRx == 0xffff);
-    }
-#endif
-    if (CPUMR0DebugStateMaybeSaveGuestAndRestoreHost(pVCpu, false /* save DR6 */))
-        pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_GUEST_DEBUG;
-
-    Assert(!CPUMIsHyperDebugStateActive(pVCpu));
-    Assert(!CPUMIsGuestDebugStateActive(pVCpu));
-
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatEntry);
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatLoadGuestState);
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExit1);
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExit2);
-    STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchLongJmpToR3);
-
-    VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_HM, VMCPUSTATE_STARTED_EXEC);
+    }
+
+    if (!pVCpu->hm.s.fLeaveDone)
+    {
+        /* Restore host FPU state if necessary and resync on next R0 reentry .*/
+        if (CPUMIsGuestFPUStateActive(pVCpu))
+        {
+            CPUMR0SaveGuestFPU(pVM, pVCpu, pCtx);
+            Assert(!CPUMIsGuestFPUStateActive(pVCpu));
+            pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_GUEST_CR0;
+        }
+
+        /*
+         * Restore host debug registers if necessary and resync on next R0 reentry.
+         */
+    #ifdef VBOX_STRICT
+        if (CPUMIsHyperDebugStateActive(pVCpu))
+        {
+            PSVMVMCB pVmcb = (PSVMVMCB)pVCpu->hm.s.svm.pvVmcb;
+            Assert(pVmcb->ctrl.u16InterceptRdDRx == 0xffff);
+            Assert(pVmcb->ctrl.u16InterceptWrDRx == 0xffff);
+        }
+    #endif
+        if (CPUMR0DebugStateMaybeSaveGuestAndRestoreHost(pVCpu, false /* save DR6 */))
+            pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_GUEST_DEBUG;
+
+        Assert(!CPUMIsHyperDebugStateActive(pVCpu));
+        Assert(!CPUMIsGuestDebugStateActive(pVCpu));
+
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatEntry);
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatLoadGuestState);
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExit1);
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExit2);
+        STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchLongJmpToR3);
+
+        VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_HM, VMCPUSTATE_STARTED_EXEC);
+
+        pVCpu->hm.s.fLeaveDone = true;
+    }
 
     /* Restore preemption if we previous disabled it ourselves. */
     if (fPreemptDisabled)
-    {
-        pVCpu->hm.s.fLeaveDone = true;
         RTThreadPreemptRestore(&PreemptState);
-    }
 }
 
Index: /trunk/src/VBox/VMM/VMMR0/HMVMXR0.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMR0/HMVMXR0.cpp	(revision 47802)
+++ /trunk/src/VBox/VMM/VMMR0/HMVMXR0.cpp	(revision 47803)
@@ -6041,61 +6041,58 @@
         RTThreadPreemptDisable(&PreemptState);
         fPreemptDisabled = true;
-        if (pVCpu->hm.s.fLeaveDone)
-        {
-            RTThreadPreemptRestore(&PreemptState);
-            return;
-        }
-    }
-
-    /* Save the guest state if necessary. */
-    if (pVCpu->hm.s.vmx.fUpdatedGuestState != HMVMX_UPDATED_GUEST_ALL)
-    {
-        int rc = hmR0VmxSaveGuestState(pVCpu, pMixedCtx);
-        AssertRC(rc);
-        Assert(pVCpu->hm.s.vmx.fUpdatedGuestState == HMVMX_UPDATED_GUEST_ALL);
-    }
-
-    /* Restore host FPU state if necessary and resync on next R0 reentry .*/
-    if (CPUMIsGuestFPUStateActive(pVCpu))
-    {
-        CPUMR0SaveGuestFPU(pVM, pVCpu, pMixedCtx);
-        Assert(!CPUMIsGuestFPUStateActive(pVCpu));
-        pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_GUEST_CR0;
-    }
-
-    /* Restore host debug registers if necessary and resync on next R0 reentry. */
-#ifdef VBOX_STRICT
-    if (CPUMIsHyperDebugStateActive(pVCpu))
-        Assert(pVCpu->hm.s.vmx.u32ProcCtls & VMX_VMCS_CTRL_PROC_EXEC_MOV_DR_EXIT);
-#endif
-    if (CPUMR0DebugStateMaybeSaveGuestAndRestoreHost(pVCpu, true /* save DR6 */))
-        pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_GUEST_DEBUG;
-    Assert(!CPUMIsGuestDebugStateActive(pVCpu));
-    Assert(!CPUMIsHyperDebugStateActive(pVCpu));
-
-    /* Restore host-state bits that VT-x only restores partially. */
-    if (pVCpu->hm.s.vmx.fRestoreHostFlags)
-    {
-        VMXRestoreHostState(pVCpu->hm.s.vmx.fRestoreHostFlags, &pVCpu->hm.s.vmx.RestoreHost);
-        pVCpu->hm.s.vmx.fRestoreHostFlags = 0;
-    }
-
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatEntry);
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatLoadGuestState);
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExit1);
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExit2);
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExitIO);
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExitMovCRx);
-    STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExitXcptNmi);
-    STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchLongJmpToR3);
-
-    VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_HM, VMCPUSTATE_STARTED_EXEC);
+    }
+
+    if (!pVCpu->hm.s.fLeaveDone)
+    {
+        /* Save the guest state if necessary. */
+        if (pVCpu->hm.s.vmx.fUpdatedGuestState != HMVMX_UPDATED_GUEST_ALL)
+        {
+            int rc = hmR0VmxSaveGuestState(pVCpu, pMixedCtx);
+            AssertRC(rc);
+            Assert(pVCpu->hm.s.vmx.fUpdatedGuestState == HMVMX_UPDATED_GUEST_ALL);
+        }
+
+        /* Restore host FPU state if necessary and resync on next R0 reentry .*/
+        if (CPUMIsGuestFPUStateActive(pVCpu))
+        {
+            CPUMR0SaveGuestFPU(pVM, pVCpu, pMixedCtx);
+            Assert(!CPUMIsGuestFPUStateActive(pVCpu));
+            pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_GUEST_CR0;
+        }
+
+        /* Restore host debug registers if necessary and resync on next R0 reentry. */
+    #ifdef VBOX_STRICT
+        if (CPUMIsHyperDebugStateActive(pVCpu))
+            Assert(pVCpu->hm.s.vmx.u32ProcCtls & VMX_VMCS_CTRL_PROC_EXEC_MOV_DR_EXIT);
+    #endif
+        if (CPUMR0DebugStateMaybeSaveGuestAndRestoreHost(pVCpu, true /* save DR6 */))
+            pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_GUEST_DEBUG;
+        Assert(!CPUMIsGuestDebugStateActive(pVCpu));
+        Assert(!CPUMIsHyperDebugStateActive(pVCpu));
+
+        /* Restore host-state bits that VT-x only restores partially. */
+        if (pVCpu->hm.s.vmx.fRestoreHostFlags)
+        {
+            VMXRestoreHostState(pVCpu->hm.s.vmx.fRestoreHostFlags, &pVCpu->hm.s.vmx.RestoreHost);
+            pVCpu->hm.s.vmx.fRestoreHostFlags = 0;
+        }
+
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatEntry);
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatLoadGuestState);
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExit1);
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExit2);
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExitIO);
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExitMovCRx);
+        STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExitXcptNmi);
+        STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchLongJmpToR3);
+
+        VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_HM, VMCPUSTATE_STARTED_EXEC);
+
+        pVCpu->hm.s.fLeaveDone = true;
+    }
 
     /* Restore preemption if we previous disabled it ourselves. */
     if (fPreemptDisabled)
-    {
-        pVCpu->hm.s.fLeaveDone = true;
         RTThreadPreemptRestore(&PreemptState);
-    }
 }
 
