Index: trunk/src/VBox/VMM/HWACCM.cpp
===================================================================
--- trunk/src/VBox/VMM/HWACCM.cpp	(revision 29736)
+++ trunk/src/VBox/VMM/HWACCM.cpp	(revision 29737)
@@ -2192,127 +2192,122 @@
 
     /* Note! The context supplied by REM is partial. If we add more checks here, be sure to verify that REM provides this info! */
-#ifdef HWACCM_VMX_EMULATE_REALMODE
-    bool fVMMDeviceHeapEnabled = PDMVMMDevHeapIsEnabled(pVM);
-
     Assert((pVM->hwaccm.s.vmx.fUnrestrictedGuest && !pVM->hwaccm.s.vmx.pRealModeTSS) || (!pVM->hwaccm.s.vmx.fUnrestrictedGuest && pVM->hwaccm.s.vmx.pRealModeTSS));
 
-    /** The VMM device heap is a requirement for emulating real mode or protected mode without paging when the unrestricted guest execution feature is missing. */
-    if (fVMMDeviceHeapEnabled)
-    {
-        if (CPUMIsGuestInRealModeEx(pCtx))
+    bool fSupportsRealMode = pVM->hwaccm.s.vmx.fUnrestrictedGuest || PDMVMMDevHeapIsEnabled(pVM);
+    if (!pVM->hwaccm.s.vmx.fUnrestrictedGuest)
+    {
+        /** The VMM device heap is a requirement for emulating real mode or protected mode without paging when the unrestricted guest execution feature is missing. */
+        if (fSupportsRealMode)
         {
-            /* VT-x will not allow high selector bases in v86 mode; fall back to the recompiler in that case.
-             * The base must also be equal to (sel << 4).
-             */
-            if (   (   pCtx->cs != (pCtx->csHid.u64Base >> 4)
-                    && pCtx->csHid.u64Base != 0xffff0000 /* we can deal with the BIOS code as it's also mapped into the lower region. */)
-                || pCtx->ds != (pCtx->dsHid.u64Base >> 4)
-                || pCtx->es != (pCtx->esHid.u64Base >> 4)
-                || pCtx->fs != (pCtx->fsHid.u64Base >> 4)
-                || pCtx->gs != (pCtx->gsHid.u64Base >> 4)
-                || pCtx->ss != (pCtx->ssHid.u64Base >> 4))
+            if (CPUMIsGuestInRealModeEx(pCtx))
             {
-                return false;
+                /* VT-x will not allow high selector bases in v86 mode; fall back to the recompiler in that case.
+                 * The base must also be equal to (sel << 4).
+                 */
+                if (   (   pCtx->cs != (pCtx->csHid.u64Base >> 4)
+                        && pCtx->csHid.u64Base != 0xffff0000 /* we can deal with the BIOS code as it's also mapped into the lower region. */)
+                    || pCtx->ds != (pCtx->dsHid.u64Base >> 4)
+                    || pCtx->es != (pCtx->esHid.u64Base >> 4)
+                    || pCtx->fs != (pCtx->fsHid.u64Base >> 4)
+                    || pCtx->gs != (pCtx->gsHid.u64Base >> 4)
+                    || pCtx->ss != (pCtx->ssHid.u64Base >> 4))
+                {
+                    return false;
+                }
+            }
+            else
+            {
+                PGMMODE enmGuestMode = PGMGetGuestMode(pVCpu);
+                /* Verify the requirements for executing code in protected mode. VT-x can't handle the CPU state right after a switch
+                 * from real to protected mode. (all sorts of RPL & DPL assumptions)
+                 */
+                if (    pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode == PGMMODE_REAL
+                    &&  enmGuestMode >= PGMMODE_PROTECTED)
+                {
+                    if (   (pCtx->cs & X86_SEL_RPL)
+                        || (pCtx->ds & X86_SEL_RPL)
+                        || (pCtx->es & X86_SEL_RPL)
+                        || (pCtx->fs & X86_SEL_RPL)
+                        || (pCtx->gs & X86_SEL_RPL)
+                        || (pCtx->ss & X86_SEL_RPL))
+                    {
+                        return false;
+                    }
+                }
             }
         }
         else
         {
-            PGMMODE enmGuestMode = PGMGetGuestMode(pVCpu);
-            /* Verify the requirements for executing code in protected mode. VT-x can't handle the CPU state right after a switch
-             * from real to protected mode. (all sorts of RPL & DPL assumptions)
-             */
-            if (    pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode == PGMMODE_REAL
-                &&  enmGuestMode >= PGMMODE_PROTECTED)
+            if (    !CPUMIsGuestInLongModeEx(pCtx)
+                &&  !pVM->hwaccm.s.vmx.fUnrestrictedGuest)
             {
-                if (   (pCtx->cs & X86_SEL_RPL)
-                    || (pCtx->ds & X86_SEL_RPL)
-                    || (pCtx->es & X86_SEL_RPL)
-                    || (pCtx->fs & X86_SEL_RPL)
-                    || (pCtx->gs & X86_SEL_RPL)
-                    || (pCtx->ss & X86_SEL_RPL))
-                {
+                /** @todo   This should (probably) be set on every excursion to the REM,
+                 *          however it's too risky right now. So, only apply it when we go
+                 *          back to REM for real mode execution. (The XP hack below doesn't
+                 *          work reliably without this.)
+                 *  Update: Implemented in EM.cpp, see #ifdef EM_NOTIFY_HWACCM.  */
+                pVM->aCpus[0].hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_ALL_GUEST;
+
+                if (    !pVM->hwaccm.s.fNestedPaging        /* requires a fake PD for real *and* protected mode without paging - stored in the VMM device heap*/
+                    ||  CPUMIsGuestInRealModeEx(pCtx))      /* requires a fake TSS for real mode - stored in the VMM device heap */
                     return false;
-                }
+
+                /* Too early for VT-x; Solaris guests will fail with a guru meditation otherwise; same for XP. */
+                if (pCtx->idtr.pIdt == 0 || pCtx->idtr.cbIdt == 0 || pCtx->tr == 0)
+                    return false;
+
+                /* The guest is about to complete the switch to protected mode. Wait a bit longer. */
+                /* Windows XP; switch to protected mode; all selectors are marked not present in the
+                 * hidden registers (possible recompiler bug; see load_seg_vm) */
+                if (pCtx->csHid.Attr.n.u1Present == 0)
+                    return false;
+                if (pCtx->ssHid.Attr.n.u1Present == 0)
+                    return false;
+
+                /* Windows XP: possible same as above, but new recompiler requires new heuristics?
+                   VT-x doesn't seem to like something about the guest state and this stuff avoids it. */
+                /** @todo This check is actually wrong, it doesn't take the direction of the
+                 *        stack segment into account. But, it does the job for now. */
+                if (pCtx->rsp >= pCtx->ssHid.u32Limit)
+                    return false;
+    #if 0
+                if (    pCtx->cs >= pCtx->gdtr.cbGdt
+                    ||  pCtx->ss >= pCtx->gdtr.cbGdt
+                    ||  pCtx->ds >= pCtx->gdtr.cbGdt
+                    ||  pCtx->es >= pCtx->gdtr.cbGdt
+                    ||  pCtx->fs >= pCtx->gdtr.cbGdt
+                    ||  pCtx->gs >= pCtx->gdtr.cbGdt)
+                    return false;
+    #endif
             }
         }
     }
-    else
-#endif /* HWACCM_VMX_EMULATE_REALMODE */
-    {
-        if (    !CPUMIsGuestInLongModeEx(pCtx)
-            &&  !pVM->hwaccm.s.vmx.fUnrestrictedGuest)
+
+    if (pVM->hwaccm.s.vmx.fEnabled)
+    {
+        uint32_t mask;
+
+        /* if bit N is set in cr0_fixed0, then it must be set in the guest's cr0. */
+        mask = (uint32_t)pVM->hwaccm.s.vmx.msr.vmx_cr0_fixed0;
+        /* Note: We ignore the NE bit here on purpose; see vmmr0\hwaccmr0.cpp for details. */
+        mask &= ~X86_CR0_NE;
+
+        if (fSupportsRealMode)
         {
-            /** @todo   This should (probably) be set on every excursion to the REM,
-             *          however it's too risky right now. So, only apply it when we go
-             *          back to REM for real mode execution. (The XP hack below doesn't
-             *          work reliably without this.)
-             *  Update: Implemented in EM.cpp, see #ifdef EM_NOTIFY_HWACCM.  */
-            pVM->aCpus[0].hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_ALL_GUEST;
-
-            if (    !pVM->hwaccm.s.fNestedPaging        /* requires a fake PD for real *and* protected mode without paging - stored in the VMM device heap*/
-                ||  CPUMIsGuestInRealModeEx(pCtx))      /* requires a fake TSS for real mode - stored in the VMM device heap */
-                return false;
-
-            /* Too early for VT-x; Solaris guests will fail with a guru meditation otherwise; same for XP. */
-            if (pCtx->idtr.pIdt == 0 || pCtx->idtr.cbIdt == 0 || pCtx->tr == 0)
-                return false;
-
-            /* The guest is about to complete the switch to protected mode. Wait a bit longer. */
-            /* Windows XP; switch to protected mode; all selectors are marked not present in the
-             * hidden registers (possible recompiler bug; see load_seg_vm) */
-            if (pCtx->csHid.Attr.n.u1Present == 0)
-                return false;
-            if (pCtx->ssHid.Attr.n.u1Present == 0)
-                return false;
-
-            /* Windows XP: possible same as above, but new recompiler requires new heuristics?
-               VT-x doesn't seem to like something about the guest state and this stuff avoids it. */
-            /** @todo This check is actually wrong, it doesn't take the direction of the
-             *        stack segment into account. But, it does the job for now. */
-            if (pCtx->rsp >= pCtx->ssHid.u32Limit)
-                return false;
-#if 0
-            if (    pCtx->cs >= pCtx->gdtr.cbGdt
-                ||  pCtx->ss >= pCtx->gdtr.cbGdt
-                ||  pCtx->ds >= pCtx->gdtr.cbGdt
-                ||  pCtx->es >= pCtx->gdtr.cbGdt
-                ||  pCtx->fs >= pCtx->gdtr.cbGdt
-                ||  pCtx->gs >= pCtx->gdtr.cbGdt)
-                return false;
-#endif
+            /* Note: We ignore the PE & PG bits here on purpose; we emulate real and protected mode without paging. */
+            mask &= ~(X86_CR0_PG|X86_CR0_PE);
         }
-    }
-
-    if (pVM->hwaccm.s.vmx.fEnabled)
-    {
-        uint32_t mask;
-
-        if (!pVM->hwaccm.s.vmx.fUnrestrictedGuest)
+        else
         {
-            /* if bit N is set in cr0_fixed0, then it must be set in the guest's cr0. */
-            mask = (uint32_t)pVM->hwaccm.s.vmx.msr.vmx_cr0_fixed0;
-            /* Note: We ignore the NE bit here on purpose; see vmmr0\hwaccmr0.cpp for details. */
-            mask &= ~X86_CR0_NE;
-
-#ifdef HWACCM_VMX_EMULATE_REALMODE
-            if (fVMMDeviceHeapEnabled)
-            {
-                /* Note: We ignore the PE & PG bits here on purpose; we emulate real and protected mode without paging. */
-                mask &= ~(X86_CR0_PG|X86_CR0_PE);
-            }
-            else
-#endif
-            {
-                /* We support protected mode without paging using identity mapping. */
-                mask &= ~X86_CR0_PG;
-            }
-            if ((pCtx->cr0 & mask) != mask)
-                return false;
-
-            /* if bit N is cleared in cr0_fixed1, then it must be zero in the guest's cr0. */
-            mask = (uint32_t)~pVM->hwaccm.s.vmx.msr.vmx_cr0_fixed1;
-            if ((pCtx->cr0 & mask) != 0)
-                return false;
+            /* We support protected mode without paging using identity mapping. */
+            mask &= ~X86_CR0_PG;
         }
+        if ((pCtx->cr0 & mask) != mask)
+            return false;
+
+        /* if bit N is cleared in cr0_fixed1, then it must be zero in the guest's cr0. */
+        mask = (uint32_t)~pVM->hwaccm.s.vmx.msr.vmx_cr0_fixed1;
+        if ((pCtx->cr0 & mask) != 0)
+            return false;
 
         /* if bit N is set in cr4_fixed0, then it must be set in the guest's cr4. */
Index: trunk/src/VBox/VMM/VMMR0/HWVMXR0.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMR0/HWVMXR0.cpp	(revision 29736)
+++ trunk/src/VBox/VMM/VMMR0/HWVMXR0.cpp	(revision 29737)
@@ -755,5 +755,4 @@
 #endif
 
-#ifdef HWACCM_VMX_EMULATE_REALMODE
     if (    CPUMIsGuestInRealModeEx(pCtx)
         &&  pVM->hwaccm.s.vmx.pRealModeTSS)
@@ -826,5 +825,4 @@
         return VINF_SUCCESS;
     }
-#endif /* HWACCM_VMX_EMULATE_REALMODE */
 
     /* Set event injection state. */
@@ -1282,10 +1280,8 @@
 #endif
 
-# ifdef HWACCM_VMX_EMULATE_REALMODE
     /* Intercept all exceptions in real mode as none of them can be injected directly (#GP otherwise). */
     if (    CPUMIsGuestInRealModeEx(pCtx)
         &&  pVM->hwaccm.s.vmx.pRealModeTSS)
         u32TrapMask |= HWACCM_VMX_TRAP_MASK_REALMODE;
-# endif /* HWACCM_VMX_EMULATE_REALMODE */
 
     int rc = VMXWriteVMCS(VMX_VMCS_CTRL_EXCEPTION_BITMAP, u32TrapMask);
@@ -1351,5 +1347,4 @@
     if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_SEGMENT_REGS)
     {
-#ifdef HWACCM_VMX_EMULATE_REALMODE
         if (pVM->hwaccm.s.vmx.pRealModeTSS)
         {
@@ -1421,5 +1416,4 @@
             }
         }
-#endif /* HWACCM_VMX_EMULATE_REALMODE */
 
         VMX_WRITE_SELREG(ES, es);
@@ -1465,5 +1459,4 @@
     if (pVCpu->hwaccm.s.fContextUseFlags & HWACCM_CHANGED_GUEST_TR)
     {
-#ifdef HWACCM_VMX_EMULATE_REALMODE
         /* Real mode emulation using v86 mode with CR4.VME (interrupt redirection using the int bitmap in the TSS) */
         if (    CPUMIsGuestInRealModeEx(pCtx)
@@ -1488,5 +1481,4 @@
         }
         else
-#endif /* HWACCM_VMX_EMULATE_REALMODE */
         {
             rc =  VMXWriteVMCS(VMX_VMCS16_GUEST_FIELD_TR,         pCtx->tr);
@@ -1646,10 +1638,8 @@
         }
 
-#ifdef HWACCM_VMX_EMULATE_REALMODE
         /* Turn off VME if we're in emulated real mode. */
         if (    CPUMIsGuestInRealModeEx(pCtx)
             &&  pVM->hwaccm.s.vmx.pRealModeTSS)
             val &= ~X86_CR4_VME;
-#endif /* HWACCM_VMX_EMULATE_REALMODE */
 
         rc |= VMXWriteVMCS64(VMX_VMCS64_GUEST_CR4,            val);
@@ -1787,5 +1777,4 @@
     eflags.u32 |= VMX_EFLAGS_RESERVED_1;
 
-#ifdef HWACCM_VMX_EMULATE_REALMODE
     /* Real mode emulation using v86 mode. */
     if (    CPUMIsGuestInRealModeEx(pCtx)
@@ -1797,5 +1786,4 @@
         eflags.Bits.u2IOPL = 0; /* must always be 0 or else certain instructions won't cause faults. */
     }
-#endif /* HWACCM_VMX_EMULATE_REALMODE */
     rc   = VMXWriteVMCS(VMX_VMCS_GUEST_RFLAGS,           eflags.u32);
     AssertRC(rc);
@@ -2018,5 +2006,4 @@
     pCtx->idtr.pIdt         = val;
 
-#ifdef HWACCM_VMX_EMULATE_REALMODE
     /* Real mode emulation using v86 mode. */
     if (    CPUMIsGuestInRealModeEx(pCtx)
@@ -2033,5 +2020,4 @@
     }
     else
-#endif /* HWACCM_VMX_EMULATE_REALMODE */
     {
         /* In real mode we have a fake TSS, so only sync it back when it's supposed to be valid. */
@@ -3320,5 +3306,4 @@
 #endif
             default:
-#ifdef HWACCM_VMX_EMULATE_REALMODE
                 if (    CPUMIsGuestInRealModeEx(pCtx)
                     &&  pVM->hwaccm.s.vmx.pRealModeTSS)
@@ -3336,5 +3321,4 @@
                     goto ResumeExecution;
                 }
-#endif
                 AssertMsgFailed(("Unexpected vm-exit caused by exception %x\n", vector));
                 rc = VERR_VMX_UNEXPECTED_EXCEPTION;
