Index: /trunk/include/VBox/vmm/gvmm.h
===================================================================
--- /trunk/include/VBox/vmm/gvmm.h	(revision 68012)
+++ /trunk/include/VBox/vmm/gvmm.h	(revision 68013)
@@ -167,5 +167,4 @@
 GVMMR0DECL(int)     GVMMR0DeregisterVCpu(PGVM pGVM, PVM pVM, VMCPUID idCpu);
 GVMMR0DECL(PGVM)    GVMMR0ByHandle(uint32_t hGVM);
-GVMMR0DECL(int)     GVMMR0ByVM(PVM pVM, PGVM *ppGVM);
 GVMMR0DECL(int)     GVMMR0ValidateGVMandVM(PGVM pGVM, PVM pVM);
 GVMMR0DECL(int)     GVMMR0ValidateGVMandVMandEMT(PGVM pGVM, PVM pVM, VMCPUID idCpu);
Index: /trunk/include/VBox/vmm/pdmpci.h
===================================================================
--- /trunk/include/VBox/vmm/pdmpci.h	(revision 68012)
+++ /trunk/include/VBox/vmm/pdmpci.h	(revision 68013)
@@ -349,5 +349,5 @@
  * Process request (in R0).
  */
-PCIRAWR0DECL(int)  PciRawR0ProcessReq(PSUPDRVSESSION pSession, PVM pVM, PPCIRAWSENDREQ pReq);
+PCIRAWR0DECL(int)  PciRawR0ProcessReq(PGVM pGVM, PVM pVM, PSUPDRVSESSION pSession, PPCIRAWSENDREQ pReq);
 /**
  * Terminate R0 PCI module.
@@ -358,10 +358,10 @@
  * Per-VM R0 module init.
  */
-PCIRAWR0DECL(int)  PciRawR0InitVM(PVM pVM);
+PCIRAWR0DECL(int)  PciRawR0InitVM(PGVM pGVM, PVM pVM);
 
 /**
  * Per-VM R0 module termination routine.
  */
-PCIRAWR0DECL(void)  PciRawR0TermVM(PVM pVM);
+PCIRAWR0DECL(void)  PciRawR0TermVM(PGVM pGVM, PVM pVM);
 
 /**
Index: /trunk/src/VBox/Devices/Bus/SrvPciRawR0.cpp
===================================================================
--- /trunk/src/VBox/Devices/Bus/SrvPciRawR0.cpp	(revision 68012)
+++ /trunk/src/VBox/Devices/Bus/SrvPciRawR0.cpp	(revision 68013)
@@ -170,23 +170,16 @@
  * Per-VM R0 module init.
  */
-PCIRAWR0DECL(int)  PciRawR0InitVM(PVM pVM)
+PCIRAWR0DECL(int)  PciRawR0InitVM(PGVM pGVM, PVM pVM)
 {
     PRAWPCIFACTORY pFactory = NULL;
-    int            rc;
-
-    rc = SUPR0ComponentQueryFactory(pVM->pSession, "VBoxRawPci", RAWPCIFACTORY_UUID_STR, (void **)&pFactory);
-
+    int rc = SUPR0ComponentQueryFactory(pGVM->pSession, "VBoxRawPci", RAWPCIFACTORY_UUID_STR, (void **)&pFactory);
     if (RT_SUCCESS(rc))
     {
         if (pFactory)
         {
-            PGVM pGVM = NULL;
-            rc = GVMMR0ByVM(pVM, &pGVM);
-            if (RT_SUCCESS(rc))
-                rc = pFactory->pfnInitVm(pFactory, pVM, &pGVM->rawpci.s);
+            rc = pFactory->pfnInitVm(pFactory, pVM, &pGVM->rawpci.s);
             pFactory->pfnRelease(pFactory);
         }
     }
-
     return VINF_SUCCESS;
 }
@@ -195,19 +188,13 @@
  * Per-VM R0 module termination routine.
  */
-PCIRAWR0DECL(void)  PciRawR0TermVM(PVM pVM)
+PCIRAWR0DECL(void)  PciRawR0TermVM(PGVM pGVM, PVM pVM)
 {
     PRAWPCIFACTORY pFactory = NULL;
-    int            rc;
-
-    rc = SUPR0ComponentQueryFactory(pVM->pSession, "VBoxRawPci", RAWPCIFACTORY_UUID_STR, (void **)&pFactory);
-
+    int rc = SUPR0ComponentQueryFactory(pGVM->pSession, "VBoxRawPci", RAWPCIFACTORY_UUID_STR, (void **)&pFactory);
     if (RT_SUCCESS(rc))
     {
         if (pFactory)
         {
-            PGVM pGVM = NULL;
-            rc = GVMMR0ByVM(pVM, &pGVM);
-            if (RT_SUCCESS(rc))
-                pFactory->pfnDeinitVm(pFactory, pVM, &pGVM->rawpci.s);
+            pFactory->pfnDeinitVm(pFactory, pVM, &pGVM->rawpci.s);
             pFactory->pfnRelease(pFactory);
         }
@@ -501,5 +488,6 @@
     return &pNew->DevPort;
 }
-#endif
+
+#endif /* DEBUG_nike */
 
 static DECLCALLBACK(void) pcirawr0DevObjDestructor(void *pvObj, void *pvIns, void *pvUnused)
@@ -518,6 +506,5 @@
 
 
-static int pcirawr0OpenDevice(PSUPDRVSESSION   pSession,
-                              PVM              pVM,
+static int pcirawr0OpenDevice(PGVM pGVM, PVM pVM, PSUPDRVSESSION pSession,
                               uint32_t         HostDevice,
                               uint32_t         fFlags,
@@ -525,20 +512,21 @@
                               uint32_t        *pfDevFlags)
 {
+
+    int rc = GVMMR0ValidateGVMandVMandEMT(pGVM, pVM, 0 /*idCpu*/);
+    if (RT_FAILURE(rc))
+        return rc;
+
     /*
      * Query the factory we want, then use it create and connect the host device.
      */
-    PRAWPCIFACTORY pFactory = NULL;
-    PRAWPCIDEVPORT pDevPort = NULL;
-    int rc;
-    PPCIRAWDEV     pNew;
-
-    pNew = (PPCIRAWDEV)RTMemAllocZ(sizeof(*pNew));
+    PPCIRAWDEV pNew = (PPCIRAWDEV)RTMemAllocZ(sizeof(*pNew));
     if (!pNew)
         return VERR_NO_MEMORY;
 
-
+    PRAWPCIFACTORY pFactory = NULL;
     rc = SUPR0ComponentQueryFactory(pSession, "VBoxRawPci", RAWPCIFACTORY_UUID_STR, (void **)&pFactory);
     /* No host driver registered, provide some fake implementation
        for debugging purposes. */
+    PRAWPCIDEVPORT pDevPort = NULL;
 #ifdef DEBUG_nike
     if (rc == VERR_SUPDRV_COMPONENT_NOT_FOUND)
@@ -559,14 +547,10 @@
         if (pFactory)
         {
-             PGVM pGVM = NULL;
-             rc = GVMMR0ByVM(pVM, &pGVM);
-
-            if (RT_SUCCESS(rc))
-                rc = pFactory->pfnCreateAndConnect(pFactory,
-                                                   HostDevice,
-                                                   fFlags,
-                                                   &pGVM->rawpci.s,
-                                                   &pDevPort,
-                                                   pfDevFlags);
+            rc = pFactory->pfnCreateAndConnect(pFactory,
+                                               HostDevice,
+                                               fFlags,
+                                               &pGVM->rawpci.s,
+                                               &pDevPort,
+                                               pfDevFlags);
             pFactory->pfnRelease(pFactory);
         }
@@ -576,24 +560,30 @@
             rc = RTSpinlockCreate(&pNew->hSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "PciRaw");
             AssertRC(rc);
-            rc = RTSemEventCreate(&pNew->hIrqEvent);
-            AssertRC(rc);
-
-            pNew->pSession = pSession;
-            pNew->pPort    = pDevPort;
-            pNew->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_RAW_PCI_DEVICE,
-                                           pcirawr0DevObjDestructor, pNew, NULL);
-
-            uint32_t hHandle = 0;
-            rc = RTHandleTableAllocWithCtx(g_State.hHtDevs, pNew, pSession, &hHandle);
             if (RT_SUCCESS(rc))
             {
-                pNew->hHandle = (PCIRAWDEVHANDLE)hHandle;
-                *pHandle = pNew->hHandle;
-            }
-            else
-            {
-                SUPR0ObjRelease(pNew->pvObj, pSession);
+                rc = RTSemEventCreate(&pNew->hIrqEvent);
+                AssertRC(rc);
+                if (RT_SUCCESS(rc))
+                {
+                    pNew->pSession = pSession;
+                    pNew->pPort    = pDevPort;
+                    pNew->pvObj    = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_RAW_PCI_DEVICE,
+                                                      pcirawr0DevObjDestructor, pNew, NULL);
+                    if (pNew->pvObj)
+                    {
+
+                        uint32_t hHandle = 0;
+                        rc = RTHandleTableAllocWithCtx(g_State.hHtDevs, pNew, pSession, &hHandle);
+                        if (RT_SUCCESS(rc))
+                        {
+                            pNew->hHandle = (PCIRAWDEVHANDLE)hHandle;
+                            *pHandle = pNew->hHandle;
+                            return rc;
+                        }
+                        SUPR0ObjRelease(pNew->pvObj, pSession);
+                    }
+                    RTSemEventDestroy(pNew->hIrqEvent);
+                }
                 RTSpinlockDestroy(pNew->hSpinlock);
-                RTSemEventDestroy(pNew->hIrqEvent);
             }
         }
@@ -926,5 +916,5 @@
  * @returns VBox status code.
  */
-PCIRAWR0DECL(int) PciRawR0ProcessReq(PSUPDRVSESSION pSession, PVM pVM, PPCIRAWSENDREQ pReq)
+PCIRAWR0DECL(int) PciRawR0ProcessReq(PGVM pGVM, PVM pVM, PSUPDRVSESSION pSession, PPCIRAWSENDREQ pReq)
 {
     LogFlow(("PciRawR0ProcessReq: %d for %x\n", pReq->iRequest, pReq->TargetDevice));
@@ -935,5 +925,5 @@
     {
         case PCIRAWR0_DO_OPEN_DEVICE:
-            rc = pcirawr0OpenDevice(pSession, pVM,
+            rc = pcirawr0OpenDevice(pGVM, pVM, pSession,
                                     pReq->u.aOpenDevice.PciAddress,
                                     pReq->u.aOpenDevice.fFlags,
@@ -1039,2 +1029,3 @@
     return rc;
 }
+
Index: /trunk/src/VBox/VMM/VMMR0/GVMMR0.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMR0/GVMMR0.cpp	(revision 68012)
+++ /trunk/src/VBox/VMM/VMMR0/GVMMR0.cpp	(revision 68013)
@@ -1724,23 +1724,4 @@
 
 /**
- * Lookup a GVM structure by the shared VM structure.
- *
- * @returns VBox status code.
- * @param   pVM         The cross context VM structure.
- * @param   ppGVM       Where to store the GVM pointer.
- *
- * @remark  This will not take the 'used'-lock because it doesn't do
- *          nesting and this function will be used from under the lock.
- *          Update: This is no longer true.  Consider taking the lock in shared
- *          mode!
- */
-GVMMR0DECL(int) GVMMR0ByVM(PVM pVM, PGVM *ppGVM)
-{
-    PGVMM pGVMM;
-    return gvmmR0ByVM(pVM, ppGVM, &pGVMM, false /* fTakeUsedLock */);
-}
-
-
-/**
  * Check that the given GVM and VM structures match up.
  *
Index: /trunk/src/VBox/VMM/VMMR0/VMMR0.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMR0/VMMR0.cpp	(revision 68012)
+++ /trunk/src/VBox/VMM/VMMR0/VMMR0.cpp	(revision 68013)
@@ -462,5 +462,5 @@
                     VMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
 #ifdef VBOX_WITH_PCI_PASSTHROUGH
-                    rc = PciRawR0InitVM(pVM);
+                    rc = PciRawR0InitVM(pGVM, pVM);
 #endif
                     if (RT_SUCCESS(rc))
@@ -482,5 +482,5 @@
                         }
 #ifdef VBOX_WITH_PCI_PASSTHROUGH
-                        PciRawR0TermVM(pVM);
+                        PciRawR0TermVM(pGVM, pVM);
 #endif
                     }
@@ -525,5 +525,5 @@
 
 #ifdef VBOX_WITH_PCI_PASSTHROUGH
-    PciRawR0TermVM(pVM);
+    PciRawR0TermVM(pGVM, pVM);
 #endif
 
@@ -1940,5 +1940,5 @@
             if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PPCIRAWSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
                 return VERR_INVALID_PARAMETER;
-            rc = PciRawR0ProcessReq(pSession, pVM, (PPCIRAWSENDREQ)pReqHdr);
+            rc = PciRawR0ProcessReq(pGVM, pVM, pSession, (PPCIRAWSENDREQ)pReqHdr);
             VMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
             break;
