Index: trunk/src/VBox/HostDrivers/Support/SUPDrvGip.cpp
===================================================================
--- trunk/src/VBox/HostDrivers/Support/SUPDrvGip.cpp	(revision 54458)
+++ trunk/src/VBox/HostDrivers/Support/SUPDrvGip.cpp	(revision 54462)
@@ -2610,4 +2610,8 @@
     /** The time the master spent in the MP worker.  */
     uint64_t                    cElapsedMasterTscTicks;
+    /** The iTry value when stopped at. */
+    uint32_t                    iTry;
+    /** Set if the run timed out.   */
+    bool volatile               fTimedOut;
     /** Pointer to the master's synchronization struct (on stack). */
     PSUPTSCDELTASYNC2 volatile  pSyncMaster;
@@ -2660,8 +2664,23 @@
 # define TSCDELTA_DBG_CHECK_LOOP()      ((void)0)
 #endif
+#if 0
+# define TSCDELTA_DBG_SYNC_MSG(a_Args)  SUPR0Printf a_Args
+#else
+# define TSCDELTA_DBG_SYNC_MSG(a_Args)  ((void)0)
+#endif
+#if 0
+# define TSCDELTA_DBG_SYNC_MSG2(a_Args) SUPR0Printf a_Args
+#else
+# define TSCDELTA_DBG_SYNC_MSG2(a_Args) ((void)0)
+#endif
+#if 0
+# define TSCDELTA_DBG_SYNC_MSG9(a_Args) SUPR0Printf a_Args
+#else
+# define TSCDELTA_DBG_SYNC_MSG9(a_Args) ((void)0)
+#endif
 
 
 static bool supdrvTscDeltaSync2_Before(PSUPTSCDELTASYNC2 pMySync, PSUPTSCDELTASYNC2 pOtherSync,
-                                       bool fIsMaster, PRTCCUINTREG pfEFlags)
+                                       bool fIsMaster, PRTCCUINTREG pfEFlags, PSUPDRVGIPTSCDELTARGS pArgs)
 {
     uint32_t        iMySeq  = fIsMaster ? 0 : 256;
@@ -2682,5 +2701,8 @@
         { /* likely*/ }
         else
+        {
+            TSCDELTA_DBG_SYNC_MSG(("sync/before/%s: #1 uSyncVar=%#x\n", fIsMaster ? "master" : "worker", pOtherSync->uSyncVar));
             return false;
+        }
     }
 
@@ -2695,5 +2717,4 @@
         if (u32Tmp == GIP_TSC_DELTA_SYNC2_STEADY)
             break;
-
         ASMSetFlags(fEFlags);
         ASMNopPause();
@@ -2701,5 +2722,8 @@
         /* Abort? */
         if (u32Tmp != GIP_TSC_DELTA_SYNC2_READY)
-            break;
+        {
+            TSCDELTA_DBG_SYNC_MSG(("sync/before/%s: #2 u32Tmp=%#x\n", fIsMaster ? "master" : "worker", u32Tmp));
+            return false;
+        }
 
         /* Check for timeouts every so often (not every loop in case RDTSC is
@@ -2719,5 +2743,7 @@
             if (ASMAtomicCmpXchgU32(&pMySync->uSyncVar, GIP_TSC_DELTA_SYNC2_TIMEOUT, GIP_TSC_DELTA_SYNC2_READY))
             {
+                TSCDELTA_DBG_SYNC_MSG(("sync/before/%s: timeout\n", fIsMaster ? "master" : "worker"));
                 ASMAtomicCmpXchgU32(&pOtherSync->uSyncVar, GIP_TSC_DELTA_SYNC2_TIMEOUT, GIP_TSC_DELTA_SYNC2_STEADY);
+                ASMAtomicWriteBool(&pArgs->fTimedOut, true);
                 return false;
             }
@@ -2743,4 +2769,5 @@
         {
             ASMSetFlags(fEFlags);
+            TSCDELTA_DBG_SYNC_MSG(("sync/before/%s: #3 uSyncVar=%#x\n", fIsMaster ? "master" : "worker", pOtherSync->uSyncVar));
             return false;
         }
@@ -2757,4 +2784,5 @@
         {
             ASMSetFlags(fEFlags);
+            TSCDELTA_DBG_SYNC_MSG(("sync/before/%s: #4 uSyncVar=%#x\n", fIsMaster ? "master" : "worker", pOtherSync->uSyncVar));
             return false;
         }
@@ -2775,4 +2803,5 @@
         {
             ASMSetFlags(fEFlags);
+            TSCDELTA_DBG_SYNC_MSG(("sync/before/%s: #5 u32Tmp=%#x\n", fIsMaster ? "master" : "worker", u32Tmp));
             return false;
         }
@@ -2792,4 +2821,5 @@
         {
             ASMSetFlags(fEFlags);
+            TSCDELTA_DBG_SYNC_MSG(("sync/before/%s: #6 uSyncVar=%#x\n", fIsMaster ? "master" : "worker", pOtherSync->uSyncVar));
             return false;
         }
@@ -2828,15 +2858,24 @@
 }
 
-#define TSCDELTA_MASTER_SYNC_BEFORE(a_pMySync, a_pOtherSync, a_pfEFlags) \
-    if (RT_LIKELY(supdrvTscDeltaSync2_Before(a_pMySync, a_pOtherSync, true /*fMaster*/, a_pfEFlags))) \
+#define TSCDELTA_MASTER_SYNC_BEFORE(a_pMySync, a_pOtherSync, a_pfEFlags, a_pArgs) \
+    if (RT_LIKELY(supdrvTscDeltaSync2_Before(a_pMySync, a_pOtherSync, true /*fIsMaster*/, a_pfEFlags, a_pArgs))) \
     { /*likely*/ } \
-    else break
-#define TSCDELTA_OTHER_SYNC_BEFORE(a_pMySync, a_pOtherSync, a_pfEFlags) \
-    if (RT_LIKELY(supdrvTscDeltaSync2_Before(a_pMySync, a_pOtherSync, false /*fMaster*/, a_pfEFlags))) \
+    else if (true) \
+    { \
+        TSCDELTA_DBG_SYNC_MSG9(("sync/before/master: #89\n")); \
+        break; \
+    } else do {} while (0)
+#define TSCDELTA_OTHER_SYNC_BEFORE(a_pMySync, a_pOtherSync, a_pfEFlags, a_pArgs) \
+    if (RT_LIKELY(supdrvTscDeltaSync2_Before(a_pMySync, a_pOtherSync, false /*fIsMaster*/, a_pfEFlags, a_pArgs))) \
     { /*likely*/ } \
-    else break
-
-
-static bool supdrvTscDeltaSync2_After(PSUPTSCDELTASYNC2 pMySync, PSUPTSCDELTASYNC2 pOtherSync, RTCCUINTREG fEFlags)
+    else if (true) \
+    { \
+        TSCDELTA_DBG_SYNC_MSG9(("sync/before/other: #89\n")); \
+        break; \
+    } else do {} while (0)
+
+
+static bool supdrvTscDeltaSync2_After(PSUPTSCDELTASYNC2 pMySync, PSUPTSCDELTASYNC2 pOtherSync,
+                                      bool fIsMaster, RTCCUINTREG fEFlags)
 {
     TSCDELTA_DBG_VARS();
@@ -2853,9 +2892,15 @@
     {
         uint32_t u32Tmp = ASMAtomicReadU32(&pMySync->uSyncVar);
-        if (u32Tmp == GIP_TSC_DELTA_SYNC2_READY)
+        if (   u32Tmp == GIP_TSC_DELTA_SYNC2_READY
+            || (u32Tmp == GIP_TSC_DELTA_SYNC2_STEADY && !fIsMaster) /* kicked twice => race */ )
             return true;
         ASMNopPause();
-        if (u32Tmp != GIP_TSC_DELTA_SYNC2_GO)
+        if (RT_LIKELY(u32Tmp == GIP_TSC_DELTA_SYNC2_GO))
+        { /* likely */}
+        else
+        {
+            TSCDELTA_DBG_SYNC_MSG(("sync/after/other: #1 u32Tmp=%#x\n", u32Tmp));
             return false; /* shouldn't ever happen! */
+        }
         TSCDELTA_DBG_CHECK_LOOP();
         ASMNopPause();
@@ -2864,7 +2909,11 @@
 
 #define TSCDELTA_MASTER_SYNC_AFTER(a_pMySync, a_pOtherSync, a_fEFlags) \
-    if (RT_LIKELY(supdrvTscDeltaSync2_After(a_pMySync, a_pOtherSync, a_fEFlags))) \
+    if (RT_LIKELY(supdrvTscDeltaSync2_After(a_pMySync, a_pOtherSync, true /*fIsMaster*/, a_fEFlags))) \
     { /* likely */ } \
-    else break
+    else if (true) \
+    { \
+        TSCDELTA_DBG_SYNC_MSG9(("sync/after/master: #97\n")); \
+        break; \
+    } else do {} while (0)
 
 #define TSCDELTA_MASTER_KICK_OTHER_OUT_OF_AFTER(a_pMySync, a_pOtherSync) \
@@ -2874,5 +2923,9 @@
     if (RT_LIKELY(ASMAtomicCmpXchgU32(&(a_pOtherSync)->uSyncVar, GIP_TSC_DELTA_SYNC2_READY, GIP_TSC_DELTA_SYNC2_GO))) \
     { /* likely */ } \
-    else break
+    else if (true)\
+    { \
+        TSCDELTA_DBG_SYNC_MSG(("sync/after/master: #99 uSyncVar=%#x\n", (a_pOtherSync)->uSyncVar)); \
+        break; \
+    } else do {} while (0)
 
 #define TSCDELTA_OTHER_SYNC_AFTER(a_pMySync, a_pOtherSync, a_fEFlags) \
@@ -2886,10 +2939,15 @@
         { \
             ASMSetFlags(a_fEFlags); \
+            TSCDELTA_DBG_SYNC_MSG(("sync/after/other: #0 uSyncVar=%#x\n", (a_pOtherSync)->uSyncVar)); \
             break; \
         } \
-        if (RT_LIKELY(supdrvTscDeltaSync2_After(a_pMySync, a_pOtherSync, a_fEFlags))) \
+        if (RT_LIKELY(supdrvTscDeltaSync2_After(a_pMySync, a_pOtherSync, false /*fIsMaster*/, a_fEFlags))) \
         { /* likely */ } \
-        else break; \
-    }  else do {} while (0)
+        else \
+        { \
+            TSCDELTA_DBG_SYNC_MSG9(("sync/after/other: #98\n")); \
+            break;  \
+        } \
+    } else do {} while (0)
 /** @} */
 
@@ -2948,5 +3006,5 @@
                       ("%#llx idMaster=%#x idWorker=%#x (idGipMaster=%#x)\n",
                        pGipCpuMaster->u64TSCSample, pGipCpuMaster->idCpu, pGipCpuWorker->idCpu, pArgs->pDevExt->idGipMaster));
-            TSCDELTA_MASTER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags);
+            TSCDELTA_MASTER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags, pArgs);
 
             do
@@ -2986,5 +3044,5 @@
 
             ASMAtomicReadU64(&pGipCpuMaster->u64TSCSample);     /* Warm the cache line. */
-            TSCDELTA_OTHER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags);
+            TSCDELTA_OTHER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags, pArgs);
 
             /*
@@ -3025,4 +3083,7 @@
     }
 
+    TSCDELTA_DBG_SYNC_MSG9(("sync/method1loop/%s: #92 iLoop=%u MyState=%#x\n", fIsMaster ? "master" : "worker", iLoop,
+                           pMySync->uSyncVar));
+
     /*
      * We must reset the worker TSC sample value in case it gets picked as a
@@ -3065,5 +3126,5 @@
  */
 
-# define GIP_TSC_DELTA_M2_LOOPS             (12 + GIP_TSC_DELTA_M2_PRIMER_LOOPS)
+# define GIP_TSC_DELTA_M2_LOOPS             (8 + GIP_TSC_DELTA_M2_PRIMER_LOOPS)
 # define GIP_TSC_DELTA_M2_PRIMER_LOOPS      1
 
@@ -3109,7 +3170,7 @@
     if (cHits > 2)
         pArgs->pWorker->i64TSCDelta = iBestDelta;
-    pArgs->M2.cHits     += cHits;
-
-#if 1
+    pArgs->M2.cHits += cHits;
+
+#if 0 /* This is pointless now with supdrvTscDeltaVerify(0). */
     /*
      * Check and see if we can quit a little early.  If the result is already
@@ -3244,5 +3305,5 @@
              * Sync up with the worker and collect data.
              */
-            TSCDELTA_MASTER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags);
+            TSCDELTA_MASTER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags, pArgs);
             supdrvTscDeltaMethod2CollectData(pArgs->M2.pMasterData, &pArgs->M2.pWorkerData->iCurSeqNo, pArgs->M2.fLagMaster);
             TSCDELTA_MASTER_SYNC_AFTER(pMySync, pOtherSync, fEFlags);
@@ -3263,5 +3324,5 @@
              * The worker.
              */
-            TSCDELTA_OTHER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags);
+            TSCDELTA_OTHER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags, pArgs);
             supdrvTscDeltaMethod2CollectData(pArgs->M2.pWorkerData, &pArgs->M2.pMasterData->iCurSeqNo, pArgs->M2.fLagWorker);
             TSCDELTA_OTHER_SYNC_AFTER(pMySync, pOtherSync, fEFlags);
@@ -3316,5 +3377,5 @@
                                 PSUPTSCDELTASYNC2 pOtherSync, bool fIsMaster, int64_t iWorkerTscDelta)
 {
-    PSUPGIPCPU pGipCpuWorker = pArgs->pWorker;
+    /*PSUPGIPCPU pGipCpuWorker = pArgs->pWorker; - unused */
     PSUPGIPCPU pGipCpuMaster = pArgs->pMaster;
     uint32_t   i;
@@ -3330,5 +3391,5 @@
         {
             uint64_t uTscWorker;
-            TSCDELTA_MASTER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags);
+            TSCDELTA_MASTER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags, pArgs);
 
             /*
@@ -3425,5 +3486,5 @@
              * The worker, master leads.
              */
-            TSCDELTA_OTHER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags);
+            TSCDELTA_OTHER_SYNC_BEFORE(pMySync, pOtherSync, &fEFlags, pArgs);
 
             for (i = 0; i < RT_ELEMENTS(pArgs->auVerifyWorkerTscs); i += 2)
@@ -3492,4 +3553,6 @@
     ASMAtomicWriteNullPtr(ppMySync);
     ASMAtomicWriteBool(&pArgs->fAbortSetup, true);
+    if (fTimeout)
+        ASMAtomicWriteBool(&pArgs->fTimedOut, true);
 
     /*
@@ -3627,9 +3690,4 @@
         {
             ASMAtomicWriteS64(&pGipCpuWorker->i64TSCDelta, GIP_TSC_DELTA_INITIAL_MASTER_VALUE);
-            RTCpuSetDelByIndex(&pDevExt->TscDeltaCpuSet, pGipCpuMaster->iCpuSet);
-            RTCpuSetAddByIndex(&pDevExt->TscDeltaObtainedCpuSet, pGipCpuMaster->iCpuSet);
-        }
-        else
-        {
             RTCpuSetDelByIndex(&pDevExt->TscDeltaCpuSet, pGipCpuWorker->iCpuSet);
             RTCpuSetAddByIndex(&pDevExt->TscDeltaObtainedCpuSet, pGipCpuWorker->iCpuSet);
@@ -3645,6 +3703,14 @@
         for (iTry = 0; iTry < 12; iTry++)
         {
-            if (ASMAtomicReadU32(&MySync.uSyncVar) != GIP_TSC_DELTA_SYNC2_READY)
+            /*
+             * Check the state before we start.
+             */
+            uint32_t u32Tmp = ASMAtomicReadU32(&MySync.uSyncVar);
+            if (   u32Tmp != GIP_TSC_DELTA_SYNC2_READY
+                && (fIsMaster || u32Tmp != GIP_TSC_DELTA_SYNC2_STEADY) /* worker may be late prepping for the next round */ )
+            {
+                TSCDELTA_DBG_SYNC_MSG(("sync/loop/%s: #0 iTry=%u MyState=%#x\n", fIsMaster ? "master" : "worker", iTry, u32Tmp));
                 break;
+            }
 
             /*
@@ -3658,29 +3724,39 @@
 # error "huh??"
 #endif
-            if (ASMAtomicReadU32(&MySync.uSyncVar) != GIP_TSC_DELTA_SYNC2_READY)
-                break;
 
             /*
-             * Success? If so, stop trying.
+             * Check the state.
              */
-#if 1
-            if (pGipCpuWorker->i64TSCDelta != INT64_MAX)
-#else
-            if (pGipCpuWorker->i64TSCDelta != INT64_MAX && iTry >= 11)
-#endif
+            u32Tmp = ASMAtomicReadU32(&MySync.uSyncVar);
+            if (   u32Tmp != GIP_TSC_DELTA_SYNC2_READY
+                && (fIsMaster || u32Tmp != GIP_TSC_DELTA_SYNC2_STEADY) /* worker may be late prepping for the next round */ )
             {
                 if (fIsMaster)
-                {
-                    RTCpuSetDelByIndex(&pDevExt->TscDeltaCpuSet, pGipCpuMaster->iCpuSet);
-                    RTCpuSetAddByIndex(&pDevExt->TscDeltaObtainedCpuSet, pGipCpuMaster->iCpuSet);
-                }
+                    TSCDELTA_DBG_SYNC_MSG(("sync/loop/master: #1 iTry=%u MyState=%#x\n", iTry, u32Tmp));
                 else
+                    TSCDELTA_DBG_SYNC_MSG2(("sync/loop/worker: #1 iTry=%u MyState=%#x\n", iTry, u32Tmp));
+                break;
+            }
+
+            /*
+             * Success? If so, stop trying. Master decides.
+             */
+            if (fIsMaster)
+            {
+#if 0
+                if (pGipCpuWorker->i64TSCDelta != INT64_MAX)
+#else
+                if (pGipCpuWorker->i64TSCDelta != INT64_MAX && iTry >= 11)
+#endif
                 {
                     RTCpuSetDelByIndex(&pDevExt->TscDeltaCpuSet, pGipCpuWorker->iCpuSet);
                     RTCpuSetAddByIndex(&pDevExt->TscDeltaObtainedCpuSet, pGipCpuWorker->iCpuSet);
+                    TSCDELTA_DBG_SYNC_MSG2(("sync/loop/master: #9 iTry=%u MyState=%#x\n", iTry, MySync.uSyncVar));
+                    break;
                 }
-                break;
             }
         }
+        if (fIsMaster)
+            pArgs->iTry = iTry;
     }
 
@@ -3858,6 +3934,7 @@
                 {
 #if 0
-                    SUPR0Printf("mponpair ticks: %9llu %9llu  max: %9llu\n", pArgs->cElapsedMasterTscTicks,
-                                pArgs->cElapsedWorkerTscTicks, pArgs->cMaxTscTicks);
+                    SUPR0Printf("mponpair ticks: %9llu %9llu  max: %9llu  iTry: %u%s\n", pArgs->cElapsedMasterTscTicks,
+                                pArgs->cElapsedWorkerTscTicks, pArgs->cMaxTscTicks, pArgs->iTry,
+                                pArgs->fTimedOut ? " timed out" :"");
 #endif
 #if 0
