Index: trunk/src/VBox/VMM/VMMR0/HMVMXR0.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMR0/HMVMXR0.cpp	(revision 45780)
+++ trunk/src/VBox/VMM/VMMR0/HMVMXR0.cpp	(revision 45781)
@@ -1910,12 +1910,4 @@
     }
 
-    for (VMCPUID i = 0; i < pVM->cCpus; i++)
-    {
-        PVMCPU pVCpu = &pVM->aCpus[i];
-
-        /* Current guest paging mode. */
-        pVCpu->hm.s.vmx.enmLastSeenGuestMode = PGMMODE_REAL;
-    }
-
     return VINF_SUCCESS;
 }
@@ -3395,17 +3387,12 @@
         {
             Assert(pVM->hm.s.vmx.pRealModeTSS);
-            PGMMODE enmGuestMode = PGMGetGuestMode(pVCpu);
-            if (pVCpu->hm.s.vmx.enmLastSeenGuestMode != enmGuestMode)
+            if (   pVCpu->hm.s.vmx.fWasInRealMode 
+                && PGMGetGuestMode(pVCpu) >= PGMMODE_PROTECTED)
             {
-                AssertCompile(PGMMODE_REAL < PGMMODE_PROTECTED);
-                if (   pVCpu->hm.s.vmx.enmLastSeenGuestMode == PGMMODE_REAL
-                    && enmGuestMode >= PGMMODE_PROTECTED)
-                {
-                    /* Signal that recompiler must flush its code-cache as the guest -may- rewrite code it will later execute
-                       in real-mode (e.g. OpenBSD 4.0) */
-                    REMFlushTBs(pVM);
-                    Log(("Load: Switch to protected mode detected!\n"));
-                }
-                pVCpu->hm.s.vmx.enmLastSeenGuestMode = enmGuestMode;
+                /* Signal that recompiler must flush its code-cache as the guest -may- rewrite code it will later execute
+                   in real-mode (e.g. OpenBSD 4.0) */
+                REMFlushTBs(pVM);
+                Log(("Load: Switch to protected mode detected!\n"));
+                pVCpu->hm.s.vmx.fWasInRealMode = false;
             }
         }
Index: trunk/src/VBox/VMM/VMMR3/HM.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMR3/HM.cpp	(revision 45780)
+++ trunk/src/VBox/VMM/VMMR3/HM.cpp	(revision 45781)
@@ -1487,6 +1487,8 @@
 
             pVCpu->hm.s.enmShadowMode            = PGMGetShadowMode(pVCpu);
+#ifdef VBOX_WITH_OLD_VTX_CODE
             Assert(pVCpu->hm.s.vmx.enmCurrGuestMode == PGMGetGuestMode(pVCpu));
             pVCpu->hm.s.vmx.enmCurrGuestMode     = PGMGetGuestMode(pVCpu);
+#endif
         }
     }
@@ -1567,5 +1569,4 @@
         }
     }
-#endif
 
     if (pVCpu->hm.s.vmx.enmCurrGuestMode != enmGuestMode)
@@ -1583,4 +1584,14 @@
         }
     }
+#else
+    /* If the guest left protected mode VMX execution, we'll have to be extra
+     * careful if/when the guest switches back to protected mode.
+     */
+    if (enmGuestMode == PGMMODE_REAL)
+    {
+        Log(("HMR3PagingModeChanged indicates real mode execution\n"));
+        pVCpu->hm.s.vmx.fWasInRealMode = true;
+    }
+#endif
 
     /** @todo r=ramshankar: Why do we need to do this? Most likely
@@ -1669,8 +1680,12 @@
     pVCpu->hm.s.Event.fPending = false;
 
+#ifdef VBOX_WITH_OLD_VTX_CODE
     /* Reset state information for real-mode emulation in VT-x. */
     pVCpu->hm.s.vmx.enmLastSeenGuestMode = PGMMODE_REAL;
     pVCpu->hm.s.vmx.enmPrevGuestMode     = PGMMODE_REAL;
     pVCpu->hm.s.vmx.enmCurrGuestMode     = PGMMODE_REAL;
+#else
+    pVCpu->hm.s.vmx.fWasInRealMode = true;
+#endif
 
     /* Reset the contents of the read cache. */
@@ -2383,6 +2398,10 @@
                    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 VBOX_WITH_OLD_VTX_CODE
                 if (    pVCpu->hm.s.vmx.enmLastSeenGuestMode == PGMMODE_REAL
                     &&  enmGuestMode >= PGMMODE_PROTECTED)
+#else
+                if (pVCpu->hm.s.vmx.fWasInRealMode)
+#endif
                 {
                     if (   (pCtx->cs.Sel & X86_SEL_RPL)
@@ -2784,4 +2803,5 @@
         AssertRCReturn(rc, rc);
 
+#if VBOX_WITH_OLD_VTX_CODE
         rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hm.s.vmx.enmLastSeenGuestMode);
         AssertRCReturn(rc, rc);
@@ -2790,4 +2810,15 @@
         rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hm.s.vmx.enmPrevGuestMode);
         AssertRCReturn(rc, rc);
+#else
+        //@todo: We only need to save pVM->aCpus[i].hm.s.vmx.fWasInRealMode and
+        // perhaps not even that (the initial value of 'true' is safe).
+        uint32_t u32Dummy = PGMMODE_REAL;
+        rc = SSMR3PutU32(pSSM, u32Dummy);
+        AssertRCReturn(rc, rc);
+        rc = SSMR3PutU32(pSSM, u32Dummy);
+        AssertRCReturn(rc, rc);
+        rc = SSMR3PutU32(pSSM, u32Dummy);
+        AssertRCReturn(rc, rc);
+#endif
     }
 #ifdef VBOX_HM_WITH_GUEST_PATCHING
@@ -2882,4 +2913,5 @@
             uint32_t val;
 
+#ifdef VBOX_WITH_OLD_VTX_CODE
             rc = SSMR3GetU32(pSSM, &val);
             AssertRCReturn(rc, rc);
@@ -2893,4 +2925,13 @@
             AssertRCReturn(rc, rc);
             pVM->aCpus[i].hm.s.vmx.enmPrevGuestMode = (PGMMODE)val;
+#else
+            //@todo: See note above re saving enmLastSeenGuestMode
+            rc = SSMR3GetU32(pSSM, &val);
+            AssertRCReturn(rc, rc);
+            rc = SSMR3GetU32(pSSM, &val);
+            AssertRCReturn(rc, rc);
+            rc = SSMR3GetU32(pSSM, &val);
+            AssertRCReturn(rc, rc);
+#endif
         }
     }
Index: trunk/src/VBox/VMM/include/HMInternal.h
===================================================================
--- trunk/src/VBox/VMM/include/HMInternal.h	(revision 45780)
+++ trunk/src/VBox/VMM/include/HMInternal.h	(revision 45781)
@@ -688,4 +688,5 @@
         } lasterror;
 
+#ifdef VBOX_WITH_OLD_VTX_CODE
         /** The last seen guest paging mode (by VT-x). */
         PGMMODE                     enmLastSeenGuestMode;
@@ -694,4 +695,8 @@
         /** Previous guest paging mode (as seen by HMR3PagingModeChanged). */
         PGMMODE                     enmPrevGuestMode;
+#else
+        /** Set if guest was executing in real mode (extra checks). */
+        bool                        fWasInRealMode;
+#endif
     } vmx;
 
