Index: /trunk/src/VBox/VMM/VMMRC/CPUMRC.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMRC/CPUMRC.cpp	(revision 42770)
+++ /trunk/src/VBox/VMM/VMMRC/CPUMRC.cpp	(revision 42771)
@@ -23,4 +23,5 @@
 #include <VBox/vmm/cpum.h>
 #include <VBox/vmm/vmm.h>
+#include <VBox/vmm/patm.h>
 #include <VBox/vmm/trpm.h>
 #include "CPUMInternal.h"
@@ -76,2 +77,31 @@
 }
 
+
+/**
+ * Called by TRPM and CPUM assembly code to make sure the guest state is
+ * ready for execution.
+ *
+ * @param   pVM                 The VM handle.
+ */
+DECLASM(void) CPUMRCAssertPreExecutionSanity(PVM pVM)
+{
+    /*
+     * Check some important assumptions before resuming guest execution.
+     */
+    PVMCPU         pVCpu     = VMMGetCpu0(pVM);
+    PCCPUMCTX      pCtx      = &pVCpu->cpum.s.Guest;
+    uint8_t  const uRawCpl   = CPUMGetGuestCPL(pVCpu);
+    uint32_t const u32EFlags = CPUMRawGetEFlags(pVCpu);
+    bool     const fPatch    = PATMIsPatchGCAddr(pVM, pCtx->eip);
+    AssertMsg(pCtx->eflags.Bits.u1IF,                ("cs:eip=%04x:%08x ss:esp=%04x:%08x cpl=%u raw/efl=%#x/%#x%s\n", pCtx->cs.Sel, pCtx->eip, pCtx->ss.Sel, pCtx->esp, uRawCpl, u32EFlags, pCtx->eflags.u, fPatch ? " patch" : ""));
+    AssertMsg(pCtx->eflags.Bits.u2IOPL < RT_MAX(uRawCpl, 1U),
+                                                     ("cs:eip=%04x:%08x ss:esp=%04x:%08x cpl=%u raw/efl=%#x/%#x%s\n", pCtx->cs.Sel, pCtx->eip, pCtx->ss.Sel, pCtx->esp, uRawCpl, u32EFlags, pCtx->eflags.u, fPatch ? " patch" : ""));
+    if (!(u32EFlags & X86_EFL_VM))
+    {
+        AssertMsg((u32EFlags & X86_EFL_IF) || fPatch,("cs:eip=%04x:%08x ss:esp=%04x:%08x cpl=%u raw/efl=%#x/%#x%s\n", pCtx->cs.Sel, pCtx->eip, pCtx->ss.Sel, pCtx->esp, uRawCpl, u32EFlags, pCtx->eflags.u, fPatch ? " patch" : ""));
+        AssertMsg((pCtx->cs.Sel & X86_SEL_RPL) > 0,  ("cs:eip=%04x:%08x ss:esp=%04x:%08x cpl=%u raw/efl=%#x/%#x%s\n", pCtx->cs.Sel, pCtx->eip, pCtx->ss.Sel, pCtx->esp, uRawCpl, u32EFlags, pCtx->eflags.u, fPatch ? " patch" : ""));
+        AssertMsg((pCtx->ss.Sel & X86_SEL_RPL) > 0,  ("cs:eip=%04x:%08x ss:esp=%04x:%08x cpl=%u raw/efl=%#x/%#x%s\n", pCtx->cs.Sel, pCtx->eip, pCtx->ss.Sel, pCtx->esp, uRawCpl, u32EFlags, pCtx->eflags.u, fPatch ? " patch" : ""));
+    }
+    AssertMsg(CPUMIsGuestInRawMode(pVCpu),           ("cs:eip=%04x:%08x ss:esp=%04x:%08x cpl=%u raw/efl=%#x/%#x%s\n", pCtx->cs.Sel, pCtx->eip, pCtx->ss.Sel, pCtx->esp, uRawCpl, u32EFlags, pCtx->eflags.u, fPatch ? " patch" : ""));
+Log2(("cs:eip=%04x:%08x ss:esp=%04x:%08x cpl=%u raw/efl=%#x/%#x%s\n", pCtx->cs.Sel, pCtx->eip, pCtx->ss.Sel, pCtx->esp, uRawCpl, u32EFlags, pCtx->eflags.u, fPatch ? " patch" : ""));
+}
Index: /trunk/src/VBox/VMM/VMMRC/CPUMRCA.asm
===================================================================
--- /trunk/src/VBox/VMM/VMMRC/CPUMRCA.asm	(revision 42770)
+++ /trunk/src/VBox/VMM/VMMRC/CPUMRCA.asm	(revision 42771)
@@ -33,4 +33,6 @@
 extern IMPNAME(g_VM)                   ; VMM GC Builtin import
 extern NAME(cpumRCHandleNPAndGP)       ; CPUMGC.cpp
+extern NAME(CPUMRCAssertPreExecutionSanity)
+
 
 ;
@@ -163,4 +165,14 @@
 align 16
 BEGINPROC_EXPORTED CPUMGCResumeGuest
+%ifdef VBOX_STRICT
+    ; Call CPUM to check sanity.
+    push    edx
+    mov     edx, IMP(g_VM)
+    push    edx
+    call    NAME(CPUMRCAssertPreExecutionSanity)
+    add     esp, 4
+    pop     edx
+%endif
+
     ; Convert to CPUMCPU pointer
     add     edx, [edx + CPUM.offCPUMCPU0]
@@ -241,4 +253,14 @@
 align 16
 BEGINPROC_EXPORTED CPUMGCResumeGuestV86
+%ifdef VBOX_STRICT
+    ; Call CPUM to check sanity.
+    push    edx
+    mov     edx, IMP(g_VM)
+    push    edx
+    call    NAME(CPUMRCAssertPreExecutionSanity)
+    add     esp, 4
+    pop     edx
+%endif
+
     ; Convert to CPUMCPU pointer
     add     edx, [edx + CPUM.offCPUMCPU0]
Index: /trunk/src/VBox/VMM/VMMRC/TRPMRCHandlersA.asm
===================================================================
--- /trunk/src/VBox/VMM/VMMRC/TRPMRCHandlersA.asm	(revision 42770)
+++ /trunk/src/VBox/VMM/VMMRC/TRPMRCHandlersA.asm	(revision 42771)
@@ -54,4 +54,5 @@
 extern NAME(TRPMGCTrap0eHandler)
 extern NAME(TRPMGCHyperTrap0eHandler)
+extern NAME(CPUMRCAssertPreExecutionSanity)
 
 
@@ -474,4 +475,12 @@
 %endif
 
+%ifdef VBOX_STRICT
+    ; Call CPUM to check sanity.
+    mov     edx, IMP(g_VM)
+    push    edx
+    call    NAME(CPUMRCAssertPreExecutionSanity)
+    add     esp, 4
+%endif
+
     ; enable WP
     mov     eax, cr0                    ;; @todo try elimiate this read.
@@ -924,4 +933,12 @@
     mov     [esi + TRPMCPU.uPrevVector], edx
 
+%ifdef VBOX_STRICT
+    ; Call CPUM to check sanity.
+    mov     edx, IMP(g_VM)
+    push    edx
+    call    NAME(CPUMRCAssertPreExecutionSanity)
+    add     esp, 4
+%endif
+
     ; enable WP
     mov     eax, cr0                    ;; @todo try elimiate this read.
