Index: /trunk/include/VBox/em.h
===================================================================
--- /trunk/include/VBox/em.h	(revision 20070)
+++ /trunk/include/VBox/em.h	(revision 20071)
@@ -183,4 +183,6 @@
 
 VMMR3DECL(void)     EMR3ReleaseOwnedLocks(PVM pVM);
+VMMR3DECL(void)     EMR3RemUnlock(PVM pVM);
+VMMR3DECL(void)     EMR3RemLock(PVM pVM);
 
 /**
Index: /trunk/src/VBox/VMM/EM.cpp
===================================================================
--- /trunk/src/VBox/VMM/EM.cpp	(revision 20070)
+++ /trunk/src/VBox/VMM/EM.cpp	(revision 20071)
@@ -109,6 +109,4 @@
 static int emR3SingleStepExecRem(PVM pVM, uint32_t cIterations);
 static EMSTATE emR3Reschedule(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
-static void emR3RemLock(PVM pVM);
-static void emR3RemUnlock(PVM pVM);
 
 /**
@@ -872,5 +870,5 @@
  * @param   pVM         VM handle.
  */
-static void emR3RemLock(PVM pVM)
+VMMR3DECL(void) EMR3RemLock(PVM pVM)
 {
     int rc = PDMCritSectEnter(&pVM->em.s.CritSectREM, VERR_SEM_BUSY);
@@ -883,5 +881,5 @@
  * @param   pVM         VM handle.
  */
-static void emR3RemUnlock(PVM pVM)
+VMMR3DECL(void) EMR3RemUnlock(PVM pVM)
 {
     PDMCritSectLeave(&pVM->em.s.CritSectREM);
@@ -901,5 +899,5 @@
     LogFlow(("emR3RemStep: cs:eip=%04x:%08x\n", CPUMGetGuestCS(pVCpu),  CPUMGetGuestEIP(pVCpu)));
 
-    emR3RemLock(pVM);
+    EMR3RemLock(pVM);
 
     /*
@@ -912,5 +910,5 @@
         REMR3StateBack(pVM, pVCpu);
     }
-    emR3RemUnlock(pVM);
+    EMR3RemUnlock(pVM);
 
     LogFlow(("emR3RemStep: returns %Rrc cs:eip=%04x:%08x\n", rc, CPUMGetGuestCS(pVCpu),  CPUMGetGuestEIP(pVCpu)));
@@ -954,5 +952,5 @@
 
     /* Big lock, but you are not supposed to own any lock when coming in here. */
-    emR3RemLock(pVM);
+    EMR3RemLock(pVM);
 
     /*
@@ -1077,5 +1075,5 @@
         STAM_PROFILE_STOP(&pVCpu->em.s.StatREMSync, e);
     }
-    emR3RemUnlock(pVM);
+    EMR3RemUnlock(pVM);
 
     STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatREMTotal, a);
@@ -1482,5 +1480,5 @@
     STAM_PROFILE_START(&pVCpu->em.s.StatREMEmu, a);
     Log(("EMINS: %04x:%RGv RSP=%RGv\n", pCtx->cs, (RTGCPTR)pCtx->rip, (RTGCPTR)pCtx->rsp));
-    emR3RemLock(pVM);
+    EMR3RemLock(pVM);
     /* Flush the recompiler TLB if the VCPU has changed. */
     if (pVM->em.s.idLastRemCpu != pVCpu->idCpu)
@@ -1489,5 +1487,5 @@
 
     rc = REMR3EmulateInstruction(pVM, pVCpu);
-    emR3RemUnlock(pVM);
+    EMR3RemUnlock(pVM);
     STAM_PROFILE_STOP(&pVCpu->em.s.StatREMEmu, a);
 
@@ -2497,7 +2495,7 @@
         case VERR_REM_FLUSHED_PAGES_OVERFLOW:
             Assert((pCtx->ss & X86_SEL_RPL) != 1);
-            emR3RemLock(pVM);
+            EMR3RemLock(pVM);
             REMR3ReplayInvalidatedPages(pVM, pVCpu);
-            emR3RemUnlock(pVM);
+            EMR3RemUnlock(pVM);
             rc = VINF_SUCCESS;
             break;
@@ -3450,7 +3448,7 @@
         if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_REM_HANDLER_NOTIFY, VM_FF_PGM_NO_MEMORY))
         {
-            emR3RemLock(pVM);
+            EMR3RemLock(pVM);
             REMR3ReplayHandlerNotifications(pVM);
-            emR3RemUnlock(pVM);
+            EMR3RemUnlock(pVM);
         }
 
Index: /trunk/src/VBox/VMM/PGMPhys.cpp
===================================================================
--- /trunk/src/VBox/VMM/PGMPhys.cpp	(revision 20070)
+++ /trunk/src/VBox/VMM/PGMPhys.cpp	(revision 20071)
@@ -1841,4 +1841,5 @@
         }
         GMMR3FreePagesCleanup(pReq);
+        pgmUnlock(pVM);
     }
     else
@@ -1846,8 +1847,8 @@
         /* link in the ram range */
         pgmR3PhysLinkRamRange(pVM, &pCur->RamRange, pRamPrev);
+        pgmUnlock(pVM);
         REMR3NotifyPhysRamRegister(pVM, GCPhys, pCur->RamRange.cb, REM_NOTIFY_PHYS_RAM_FLAGS_MMIO2);
     }
 
-    pgmUnlock(pVM);
 
     return VINF_SUCCESS;
Index: /trunk/src/recompiler/VBoxREMWrapper.cpp
===================================================================
--- /trunk/src/recompiler/VBoxREMWrapper.cpp	(revision 20070)
+++ /trunk/src/recompiler/VBoxREMWrapper.cpp	(revision 20071)
@@ -1107,4 +1107,6 @@
     { "DISInstr",                               (void *)(uintptr_t)&DISInstr,                       &g_aArgsDISInstr[0],                        RT_ELEMENTS(g_aArgsDISInstr),                          REMFNDESC_FLAGS_RET_INT,    sizeof(bool),       NULL },
     { "EMR3FatalError",                         (void *)(uintptr_t)&EMR3FatalError,                 &g_aArgsEMR3FatalError[0],                  RT_ELEMENTS(g_aArgsEMR3FatalError),                    REMFNDESC_FLAGS_RET_VOID,   0,                  NULL },
+    { "EMR3RemLock",                            (void *)(uintptr_t)&EMR3RemLock,                    &g_aArgsVM[0],                              RT_ELEMENTS(g_aArgsVM),                                REMFNDESC_FLAGS_RET_VOID,   0,                  NULL },
+    { "EMR3RemUnlock",                          (void *)(uintptr_t)&EMR3RemUnlock,                  &g_aArgsVM[0],                              RT_ELEMENTS(g_aArgsVM),                                REMFNDESC_FLAGS_RET_VOID,   0,                  NULL },
     { "HWACCMR3CanExecuteGuest",                (void *)(uintptr_t)&HWACCMR3CanExecuteGuest,        &g_aArgsHWACCMR3CanExecuteGuest[0],         RT_ELEMENTS(g_aArgsHWACCMR3CanExecuteGuest),           REMFNDESC_FLAGS_RET_INT,    sizeof(int),        NULL },
     { "IOMIOPortRead",                          (void *)(uintptr_t)&IOMIOPortRead,                  &g_aArgsIOMIOPortRead[0],                   RT_ELEMENTS(g_aArgsIOMIOPortRead),                     REMFNDESC_FLAGS_RET_INT,    sizeof(int),        NULL },
@@ -1150,4 +1152,5 @@
     { "PGMR3PhysWriteU64",                      (void *)(uintptr_t)&PGMR3PhysWriteU64,              &g_aArgsPGMR3PhysWriteU64[0],               RT_ELEMENTS(g_aArgsPGMR3PhysWriteU32),                 REMFNDESC_FLAGS_RET_VOID,   0,                  NULL },
     { "PGMR3PhysTlbGCPhys2Ptr",                 (void *)(uintptr_t)&PGMR3PhysTlbGCPhys2Ptr,         &g_aArgsPGMR3PhysTlbGCPhys2Ptr[0],          RT_ELEMENTS(g_aArgsPGMR3PhysTlbGCPhys2Ptr),            REMFNDESC_FLAGS_RET_INT,    sizeof(int),        NULL },
+    { "PGMIsLockOwner",                         (void *)(uintptr_t)&PGMIsLockOwner,                 &g_aArgsVM[0],                              RT_ELEMENTS(g_aArgsVM),                                REMFNDESC_FLAGS_RET_INT,    sizeof(bool),       NULL },
     { "SSMR3GetGCPtr",                          (void *)(uintptr_t)&SSMR3GetGCPtr,                  &g_aArgsSSMR3GetGCPtr[0],                   RT_ELEMENTS(g_aArgsSSMR3GetGCPtr),                     REMFNDESC_FLAGS_RET_INT,    sizeof(int),        NULL },
     { "SSMR3GetMem",                            (void *)(uintptr_t)&SSMR3GetMem,                    &g_aArgsSSMR3GetMem[0],                     RT_ELEMENTS(g_aArgsSSMR3GetMem),                       REMFNDESC_FLAGS_RET_INT,    sizeof(int),        NULL },
Index: /trunk/src/recompiler/VBoxRecompiler.c
===================================================================
--- /trunk/src/recompiler/VBoxRecompiler.c	(revision 20070)
+++ /trunk/src/recompiler/VBoxRecompiler.c	(revision 20071)
@@ -2839,4 +2839,7 @@
     Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
     AssertMsg(fFlags == REM_NOTIFY_PHYS_RAM_FLAGS_RAM || fFlags == REM_NOTIFY_PHYS_RAM_FLAGS_MMIO2, ("#x\n", fFlags));
+    Assert(!PGMIsLockOwner(pVM));
+
+    EMR3RemLock(pVM);
 
     /*
@@ -2861,4 +2864,5 @@
     Assert(pVM->rem.s.fIgnoreAll);
     pVM->rem.s.fIgnoreAll = false;
+    EMR3RemUnlock(pVM);
 }
 
@@ -2886,5 +2890,7 @@
     Assert(cb);
     Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
-
+    Assert(!PGMIsLockOwner(pVM));
+
+    EMR3RemLock(pVM);
     /*
      * Register the rom.
@@ -2897,4 +2903,5 @@
     Assert(pVM->rem.s.fIgnoreAll);
     pVM->rem.s.fIgnoreAll = false;
+    EMR3RemUnlock(pVM);
 }
 
@@ -2918,4 +2925,7 @@
     Assert(cb);
     Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
+    Assert(!PGMIsLockOwner(pVM));
+
+    EMR3RemLock(pVM);
 
     /*
@@ -2929,4 +2939,5 @@
     Assert(pVM->rem.s.fIgnoreAll);
     pVM->rem.s.fIgnoreAll = false;
+    EMR3RemUnlock(pVM);
 }
 
@@ -2951,5 +2962,7 @@
     Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
     Assert(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb);
-
+    Assert(!PGMIsLockOwner(pVM));
+
+    EMR3RemLock(pVM);
     if (pVM->rem.s.cHandlerNotifications)
         REMR3ReplayHandlerNotifications(pVM);
@@ -2965,4 +2978,5 @@
     Assert(pVM->rem.s.fIgnoreAll);
     pVM->rem.s.fIgnoreAll = false;
+    EMR3RemUnlock(pVM);
 }
 
@@ -2983,4 +2997,7 @@
           enmType, GCPhys, cb, fHasHCHandler, fRestoreAsRAM, MMR3PhysGetRamSize(pVM)));
     VM_ASSERT_EMT(pVM);
+    Assert(!PGMIsLockOwner(pVM));
+
+    EMR3RemLock(pVM);
 
     if (pVM->rem.s.cHandlerNotifications)
@@ -3010,4 +3027,5 @@
     Assert(pVM->rem.s.fIgnoreAll);
     pVM->rem.s.fIgnoreAll = false;
+    EMR3RemUnlock(pVM);
 }
 
@@ -3030,4 +3048,7 @@
     VM_ASSERT_EMT(pVM);
     AssertReleaseMsg(enmType != PGMPHYSHANDLERTYPE_MMIO, ("enmType=%d\n", enmType));
+    Assert(!PGMIsLockOwner(pVM));
+
+    EMR3RemLock(pVM);
 
     if (pVM->rem.s.cHandlerNotifications)
@@ -3062,4 +3083,5 @@
         pVM->rem.s.fIgnoreAll = false;
     }
+    EMR3RemUnlock(pVM);
 }
 
