Index: trunk/src/VBox/VMM/VMMAll/DBGFAll.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMAll/DBGFAll.cpp	(revision 61627)
+++ trunk/src/VBox/VMM/VMMAll/DBGFAll.cpp	(revision 61628)
@@ -299,5 +299,6 @@
  * @returns Strict VBox status code.
  * @retval  VINF_EM_DBG_EVENT if the event was raised and the caller should
- *          return ASAP to the debugger (via EM).
+ *          return ASAP to the debugger (via EM).  We set VMCPU_FF_DBGF so, it
+ *          is okay not to pass this along in some situations  .
  * @retval  VINF_SUCCESS if the event was disabled or ignored.
  *
@@ -370,4 +371,5 @@
         pVCpu->dbgf.s.cEvents = i + 1;
 
+        VMCPU_FF_SET(pVCpu, VMCPU_FF_DBGF);
         return VINF_EM_DBG_EVENT;
     }
Index: trunk/src/VBox/VMM/VMMAll/GIMAllHv.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMAll/GIMAllHv.cpp	(revision 61627)
+++ trunk/src/VBox/VMM/VMMAll/GIMAllHv.cpp	(revision 61628)
@@ -21,6 +21,16 @@
 *********************************************************************************************************************************/
 #define LOG_GROUP LOG_GROUP_GIM
+#include <VBox/vmm/em.h>
+#include <VBox/vmm/hm.h>
+#include <VBox/vmm/tm.h>
+#include <VBox/vmm/dbgf.h>
+#include <VBox/vmm/pgm.h>
 #include "GIMHvInternal.h"
 #include "GIMInternal.h"
+#include <VBox/vmm/vm.h>
+
+#include <VBox/vmm/pdmdev.h>
+#include <VBox/vmm/pdmapi.h>
+#include <VBox/err.h>
 
 #include <iprt/asm-amd64-x86.h>
@@ -28,13 +38,4 @@
 # include <iprt/mem.h>
 #endif
-
-#include <VBox/err.h>
-#include <VBox/vmm/em.h>
-#include <VBox/vmm/hm.h>
-#include <VBox/vmm/tm.h>
-#include <VBox/vmm/vm.h>
-#include <VBox/vmm/pgm.h>
-#include <VBox/vmm/pdmdev.h>
-#include <VBox/vmm/pdmapi.h>
 
 
@@ -726,4 +727,9 @@
                 LogRel(("GIM: HyperV: Guest indicates a fatal condition! P0=%#RX64 P1=%#RX64 P2=%#RX64 P3=%#RX64 P4=%#RX64\n",
                         pHv->uCrashP0Msr, pHv->uCrashP1Msr, pHv->uCrashP2Msr, pHv->uCrashP3Msr, pHv->uCrashP4Msr));
+
+                PVM pVM = pVCpu->CTX_SUFF(pVM);
+                if (DBGF_IS_EVENT_ENABLED(pVM, DBGFEVENT_BSOD_MSR))
+                    DBGFEventGenericWithArg(pVM, pVCpu, DBGFEVENT_BSOD_MSR, pHv->uCrashP0Msr, DBGFEVENTCTX_OTHER);
+                /* (Do not try pass VINF_EM_DBG_EVENT, doesn't work from here!) */
             }
             return VINF_SUCCESS;
Index: trunk/src/VBox/VMM/VMMAll/PGMAllBth.h
===================================================================
--- trunk/src/VBox/VMM/VMMAll/PGMAllBth.h	(revision 61627)
+++ trunk/src/VBox/VMM/VMMAll/PGMAllBth.h	(revision 61628)
@@ -1126,9 +1126,9 @@
             /* Get guest page flags. */
             uint64_t fPageGst;
-            rc = PGMGstGetPage(pVCpu, pvFault, &fPageGst, NULL);
-            if (RT_SUCCESS(rc))
+            int rc2 = PGMGstGetPage(pVCpu, pvFault, &fPageGst, NULL);
+            if (RT_SUCCESS(rc2))
             {
-                uint64_t fPageShw;
-                rc = PGMShwGetPage(pVCpu, pvFault, &fPageShw, NULL);
+                uint64_t fPageShw = 0;
+                rc2 = PGMShwGetPage(pVCpu, pvFault, &fPageShw, NULL);
 
                 /*
@@ -1143,6 +1143,6 @@
                               && (fPageShw & (X86_PTE_RW | X86_PTE_US)) == X86_PTE_RW
                               && (fPageGst & (X86_PTE_RW | X86_PTE_US)) == X86_PTE_US),
-                          ("Page flags mismatch! pvFault=%RGv uErr=%x GCPhys=%RGp fPageShw=%RX64 fPageGst=%RX64\n",
-                           pvFault, (uint32_t)uErr, GCPhys, fPageShw, fPageGst));
+                          ("Page flags mismatch! pvFault=%RGv uErr=%x GCPhys=%RGp fPageShw=%RX64 fPageGst=%RX64 rc=%d\n",
+                           pvFault, (uint32_t)uErr, GCPhys, fPageShw, fPageGst, rc));
             }
             else
Index: trunk/src/VBox/VMM/VMMAll/PGMAllPool.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMAll/PGMAllPool.cpp	(revision 61627)
+++ trunk/src/VBox/VMM/VMMAll/PGMAllPool.cpp	(revision 61628)
@@ -859,4 +859,5 @@
      * Must do this in raw mode (!); XP boot will fail otherwise.
      */
+RTLogPrintf("pgmPoolAccessPfHandlerFlush\n");
     VBOXSTRICTRC rc2 = EMInterpretInstructionDisasState(pVCpu, pDis, pRegFrame, pvFault, EMCODETYPE_ALL);
     if (rc2 == VINF_SUCCESS)
@@ -1025,4 +1026,5 @@
      * Interpret the instruction.
      */
+RTLogPrintf("pgmPoolAccessPfHandlerSimple\n");
     VBOXSTRICTRC rc = EMInterpretInstructionDisasState(pVCpu, pDis, pRegFrame, pvFault, EMCODETYPE_ALL);
     if (RT_SUCCESS(rc))
Index: trunk/src/VBox/VMM/VMMR3/DBGF.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMR3/DBGF.cpp	(revision 61627)
+++ trunk/src/VBox/VMM/VMMR3/DBGF.cpp	(revision 61628)
@@ -394,7 +394,10 @@
 /**
  * Forced action callback.
- * The VMM will call this from it's main loop when VM_FF_DBGF is set.
- *
- * The function checks and executes pending commands from the debugger.
+ *
+ * The VMM will call this from it's main loop when either VM_FF_DBGF or
+ * VMCPU_FF_DBGF are set.
+ *
+ * The function checks for and executes pending commands from the debugger.
+ * Then it checks for pending debug events and serves these.
  *
  * @returns VINF_SUCCESS normally.
@@ -402,12 +405,10 @@
  * @param   pVM         The cross context VM structure.
  */
-VMMR3_INT_DECL(int) DBGFR3VMMForcedAction(PVM pVM)
-{
-    int rc = VINF_SUCCESS;
+VMMR3_INT_DECL(int) DBGFR3VMMForcedAction(PVM pVM, PVMCPU pVCpu)
+{
+    VBOXSTRICTRC rcStrict = VINF_SUCCESS;
 
     if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_DBGF))
     {
-        PVMCPU pVCpu = VMMGetCpu(pVM);
-
         /*
          * Command pending? Process it.
@@ -418,10 +419,28 @@
             DBGFCMDDATA     CmdData = pVM->dbgf.s.VMMCmdData;
             DBGFCMD         enmCmd = dbgfR3SetCmd(pVM, DBGFCMD_NO_COMMAND);
-            rc = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
+            rcStrict = dbgfR3VMMCmd(pVM, enmCmd, &CmdData, &fResumeExecution);
             if (!fResumeExecution)
-                rc = dbgfR3VMMWait(pVM);
-        }
-    }
-    return rc;
+                rcStrict = dbgfR3VMMWait(pVM);
+        }
+    }
+
+    /*
+     * Dispatch pending events.
+     */
+    if (VMCPU_FF_TEST_AND_CLEAR(pVCpu, VMCPU_FF_DBGF))
+    {
+        if (   pVCpu->dbgf.s.cEvents > 0
+            && pVCpu->dbgf.s.aEvents[pVCpu->dbgf.s.cEvents - 1].enmState == DBGFEVENTSTATE_CURRENT)
+        {
+            VBOXSTRICTRC rcStrict2 = DBGFR3EventHandlePending(pVM, pVCpu);
+            if (   rcStrict2 != VINF_SUCCESS
+                && (   rcStrict == VINF_SUCCESS
+                    || RT_FAILURE(rcStrict2)
+                    || rcStrict2 < rcStrict) ) /** @todo oversimplified? */
+                rcStrict = rcStrict2;
+        }
+    }
+
+    return VBOXSTRICTRC_TODO(rcStrict);
 }
 
@@ -554,4 +573,5 @@
 {
     VMCPU_ASSERT_EMT(pVCpu);
+    VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_DBGF);
 
     /*
@@ -567,5 +587,8 @@
     int rc = dbgfR3EventPrologue(pVM, pEvent->enmType);
     if (RT_FAILURE(rc))
+    {
+        /** @todo drop them events?   */
         return rc;
+    }
 
 /** @todo SMP + debugger speaker logic  */
Index: trunk/src/VBox/VMM/VMMR3/EM.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMR3/EM.cpp	(revision 61627)
+++ trunk/src/VBox/VMM/VMMR3/EM.cpp	(revision 61628)
@@ -895,99 +895,96 @@
          * Process the result.
          */
-        do
-        {
-            switch (VBOXSTRICTRC_VAL(rc))
-            {
-                /*
-                 * Continue the debugging loop.
-                 */
-                case VINF_EM_DBG_STEP:
-                case VINF_EM_DBG_STOP:
-                case VINF_EM_DBG_EVENT:
-                case VINF_EM_DBG_STEPPED:
-                case VINF_EM_DBG_BREAKPOINT:
-                case VINF_EM_DBG_HYPER_STEPPED:
-                case VINF_EM_DBG_HYPER_BREAKPOINT:
-                case VINF_EM_DBG_HYPER_ASSERTION:
-                    break;
-
-                /*
-                 * Resuming execution (in some form) has to be done here if we got
-                 * a hypervisor debug event.
-                 */
-                case VINF_SUCCESS:
-                case VINF_EM_RESUME:
-                case VINF_EM_SUSPEND:
-                case VINF_EM_RESCHEDULE:
-                case VINF_EM_RESCHEDULE_RAW:
-                case VINF_EM_RESCHEDULE_REM:
-                case VINF_EM_HALT:
-                    if (pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER)
-                    {
+        switch (VBOXSTRICTRC_VAL(rc))
+        {
+            /*
+             * Continue the debugging loop.
+             */
+            case VINF_EM_DBG_STEP:
+            case VINF_EM_DBG_STOP:
+            case VINF_EM_DBG_EVENT:
+            case VINF_EM_DBG_STEPPED:
+            case VINF_EM_DBG_BREAKPOINT:
+            case VINF_EM_DBG_HYPER_STEPPED:
+            case VINF_EM_DBG_HYPER_BREAKPOINT:
+            case VINF_EM_DBG_HYPER_ASSERTION:
+                break;
+
+            /*
+             * Resuming execution (in some form) has to be done here if we got
+             * a hypervisor debug event.
+             */
+            case VINF_SUCCESS:
+            case VINF_EM_RESUME:
+            case VINF_EM_SUSPEND:
+            case VINF_EM_RESCHEDULE:
+            case VINF_EM_RESCHEDULE_RAW:
+            case VINF_EM_RESCHEDULE_REM:
+            case VINF_EM_HALT:
+                if (pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER)
+                {
 #ifdef VBOX_WITH_RAW_MODE
-                        rc = emR3RawResumeHyper(pVM, pVCpu);
-                        if (rc != VINF_SUCCESS && RT_SUCCESS(rc))
-                            continue;
+                    rc = emR3RawResumeHyper(pVM, pVCpu);
+                    if (rc != VINF_SUCCESS && RT_SUCCESS(rc))
+                        continue;
 #else
-                        AssertLogRelMsgFailedReturn(("Not implemented\n"), VERR_EM_INTERNAL_ERROR);
-#endif
-                    }
-                    if (rc == VINF_SUCCESS)
-                        rc = VINF_EM_RESCHEDULE;
-                    return rc;
-
-                /*
-                 * The debugger isn't attached.
-                 * We'll simply turn the thing off since that's the easiest thing to do.
-                 */
-                case VERR_DBGF_NOT_ATTACHED:
-                    switch (VBOXSTRICTRC_VAL(rcLast))
-                    {
-                        case VINF_EM_DBG_HYPER_STEPPED:
-                        case VINF_EM_DBG_HYPER_BREAKPOINT:
-                        case VINF_EM_DBG_HYPER_ASSERTION:
-                        case VERR_TRPM_PANIC:
-                        case VERR_TRPM_DONT_PANIC:
-                        case VERR_VMM_RING0_ASSERTION:
-                        case VERR_VMM_HYPER_CR3_MISMATCH:
-                        case VERR_VMM_RING3_CALL_DISABLED:
-                            return rcLast;
-                    }
-                    return VINF_EM_OFF;
-
-                /*
-                 * Status codes terminating the VM in one or another sense.
-                 */
-                case VINF_EM_TERMINATE:
-                case VINF_EM_OFF:
-                case VINF_EM_RESET:
-                case VINF_EM_NO_MEMORY:
-                case VINF_EM_RAW_STALE_SELECTOR:
-                case VINF_EM_RAW_IRET_TRAP:
-                case VERR_TRPM_PANIC:
-                case VERR_TRPM_DONT_PANIC:
-                case VERR_IEM_INSTR_NOT_IMPLEMENTED:
-                case VERR_IEM_ASPECT_NOT_IMPLEMENTED:
-                case VERR_VMM_RING0_ASSERTION:
-                case VERR_VMM_HYPER_CR3_MISMATCH:
-                case VERR_VMM_RING3_CALL_DISABLED:
-                case VERR_INTERNAL_ERROR:
-                case VERR_INTERNAL_ERROR_2:
-                case VERR_INTERNAL_ERROR_3:
-                case VERR_INTERNAL_ERROR_4:
-                case VERR_INTERNAL_ERROR_5:
-                case VERR_IPE_UNEXPECTED_STATUS:
-                case VERR_IPE_UNEXPECTED_INFO_STATUS:
-                case VERR_IPE_UNEXPECTED_ERROR_STATUS:
-                    return rc;
-
-                /*
-                 * The rest is unexpected, and will keep us here.
-                 */
-                default:
-                    AssertMsgFailed(("Unexpected rc %Rrc!\n", VBOXSTRICTRC_VAL(rc)));
-                    break;
-            }
-        } while (false);
+                    AssertLogRelMsgFailedReturn(("Not implemented\n"), VERR_EM_INTERNAL_ERROR);
+#endif
+                }
+                if (rc == VINF_SUCCESS)
+                    rc = VINF_EM_RESCHEDULE;
+                return rc;
+
+            /*
+             * The debugger isn't attached.
+             * We'll simply turn the thing off since that's the easiest thing to do.
+             */
+            case VERR_DBGF_NOT_ATTACHED:
+                switch (VBOXSTRICTRC_VAL(rcLast))
+                {
+                    case VINF_EM_DBG_HYPER_STEPPED:
+                    case VINF_EM_DBG_HYPER_BREAKPOINT:
+                    case VINF_EM_DBG_HYPER_ASSERTION:
+                    case VERR_TRPM_PANIC:
+                    case VERR_TRPM_DONT_PANIC:
+                    case VERR_VMM_RING0_ASSERTION:
+                    case VERR_VMM_HYPER_CR3_MISMATCH:
+                    case VERR_VMM_RING3_CALL_DISABLED:
+                        return rcLast;
+                }
+                return VINF_EM_OFF;
+
+            /*
+             * Status codes terminating the VM in one or another sense.
+             */
+            case VINF_EM_TERMINATE:
+            case VINF_EM_OFF:
+            case VINF_EM_RESET:
+            case VINF_EM_NO_MEMORY:
+            case VINF_EM_RAW_STALE_SELECTOR:
+            case VINF_EM_RAW_IRET_TRAP:
+            case VERR_TRPM_PANIC:
+            case VERR_TRPM_DONT_PANIC:
+            case VERR_IEM_INSTR_NOT_IMPLEMENTED:
+            case VERR_IEM_ASPECT_NOT_IMPLEMENTED:
+            case VERR_VMM_RING0_ASSERTION:
+            case VERR_VMM_HYPER_CR3_MISMATCH:
+            case VERR_VMM_RING3_CALL_DISABLED:
+            case VERR_INTERNAL_ERROR:
+            case VERR_INTERNAL_ERROR_2:
+            case VERR_INTERNAL_ERROR_3:
+            case VERR_INTERNAL_ERROR_4:
+            case VERR_INTERNAL_ERROR_5:
+            case VERR_IPE_UNEXPECTED_STATUS:
+            case VERR_IPE_UNEXPECTED_INFO_STATUS:
+            case VERR_IPE_UNEXPECTED_ERROR_STATUS:
+                return rc;
+
+            /*
+             * The rest is unexpected, and will keep us here.
+             */
+            default:
+                AssertMsgFailed(("Unexpected rc %Rrc!\n", VBOXSTRICTRC_VAL(rc)));
+                break;
+        }
     } /* debug for ever */
 }
@@ -1715,7 +1712,8 @@
          * Debugger Facility polling.
          */
-        if (VM_FF_IS_PENDING(pVM, VM_FF_DBGF))
-        {
-            rc2 = DBGFR3VMMForcedAction(pVM);
+        if (   VM_FF_IS_PENDING(pVM, VM_FF_DBGF)
+            || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_DBGF) )
+        {
+            rc2 = DBGFR3VMMForcedAction(pVM, pVCpu);
             UPDATE_RC();
         }
@@ -1760,5 +1758,5 @@
         /* check that we got them all  */
         AssertCompile(VM_FF_NORMAL_PRIORITY_POST_MASK == (VM_FF_CHECK_VM_STATE | VM_FF_DBGF | VM_FF_RESET | VM_FF_PGM_NO_MEMORY | VM_FF_EMT_RENDEZVOUS));
-        AssertCompile(VMCPU_FF_NORMAL_PRIORITY_POST_MASK == VM_WHEN_RAW_MODE(VMCPU_FF_CSAM_SCAN_PAGE, 0));
+        AssertCompile(VMCPU_FF_NORMAL_PRIORITY_POST_MASK == (VM_WHEN_RAW_MODE(VMCPU_FF_CSAM_SCAN_PAGE, 0) | VMCPU_FF_DBGF));
     }
 
@@ -1999,7 +1997,9 @@
          * Debugger Facility request.
          */
-        if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_DBGF, VM_FF_PGM_NO_MEMORY))
-        {
-            rc2 = DBGFR3VMMForcedAction(pVM);
+        if (   (   VM_FF_IS_PENDING(pVM, VM_FF_DBGF)
+                || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_DBGF) )
+            && !VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY) )
+        {
+            rc2 = DBGFR3VMMForcedAction(pVM, pVCpu);
             UPDATE_RC();
         }
@@ -2084,5 +2084,5 @@
         /* check that we got them all  */
         AssertCompile(VM_FF_HIGH_PRIORITY_PRE_MASK == (VM_FF_TM_VIRTUAL_SYNC | VM_FF_DBGF | VM_FF_CHECK_VM_STATE | VM_FF_DEBUG_SUSPEND | VM_FF_PGM_NEED_HANDY_PAGES | VM_FF_PGM_NO_MEMORY | VM_FF_EMT_RENDEZVOUS));
-        AssertCompile(VMCPU_FF_HIGH_PRIORITY_PRE_MASK == (VMCPU_FF_TIMER | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL | VMCPU_FF_INHIBIT_INTERRUPTS | VM_WHEN_RAW_MODE(VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_TRPM_SYNC_IDT | VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT, 0)));
+        AssertCompile(VMCPU_FF_HIGH_PRIORITY_PRE_MASK == (VMCPU_FF_TIMER | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL | VMCPU_FF_INHIBIT_INTERRUPTS | VM_WHEN_RAW_MODE(VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_TRPM_SYNC_IDT | VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT | VMCPU_FF_DBGF, 0)));
     }
 
Index: trunk/src/VBox/VMM/VMMR3/VMEmt.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMR3/VMEmt.cpp	(revision 61627)
+++ trunk/src/VBox/VMM/VMMR3/VMEmt.cpp	(revision 61628)
@@ -153,5 +153,6 @@
              * servicing requests. (Look after the ifs.)
              */
-            PVM pVM = pUVM->pVM;
+            PVM    pVM   = pUVM->pVM;
+            PVMCPU pVCpu = pUVCpu->pVCpu;
             enmBefore = pVM->enmVMState;
             if (pUVM->vm.s.fTerminateEMT)
@@ -182,10 +183,11 @@
                 Log(("vmR3EmulationThread: Req (cpu=%u) rc=%Rrc, VM state %s -> %s\n", pUVCpu->idCpu, rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
             }
-            else if (VM_FF_IS_SET(pVM, VM_FF_DBGF))
+            else if (   VM_FF_IS_SET(pVM, VM_FF_DBGF)
+                     || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_DBGF))
             {
                 /*
                  * Service the debugger request.
                  */
-                rc = DBGFR3VMMForcedAction(pVM);
+                rc = DBGFR3VMMForcedAction(pVM, pVCpu);
                 Log(("vmR3EmulationThread: Dbg rc=%Rrc, VM state %s -> %s\n", rc, VMR3GetStateName(enmBefore), VMR3GetStateName(pVM->enmVMState)));
             }
Index: trunk/src/VBox/VMM/VMMR3/VMM.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMR3/VMM.cpp	(revision 61627)
+++ trunk/src/VBox/VMM/VMMR3/VMM.cpp	(revision 61628)
@@ -2858,4 +2858,5 @@
         PRINT_FLAG(VMCPU_FF_,IEM);
         PRINT_FLAG(VMCPU_FF_,UPDATE_APIC);
+        PRINT_FLAG(VMCPU_FF_,DBGF);
         PRINT_FLAG(VMCPU_FF_,REQUEST);
         PRINT_FLAG(VMCPU_FF_,HM_UPDATE_CR3);
