Index: trunk/src/VBox/Devices/Audio/DevHDA.cpp
===================================================================
--- trunk/src/VBox/Devices/Audio/DevHDA.cpp	(revision 71708)
+++ trunk/src/VBox/Devices/Audio/DevHDA.cpp	(revision 71709)
@@ -30,4 +30,5 @@
 #include <VBox/vmm/pdmaudioifs.h>
 #include <VBox/version.h>
+#include <VBox/AssertGuest.h>
 
 #include <iprt/assert.h>
@@ -970,7 +971,7 @@
 static int hdaRegWriteU32(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
 {
+    uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
+
     DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
-
-    uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
 
     pThis->au32Regs[iRegMem]  = (u32Value & g_aHdaRegMap[iReg].writable)
@@ -983,20 +984,21 @@
 {
     RT_NOREF_PV(iReg);
+#ifdef IN_RING3
+    DEVHDA_LOCK(pThis);
+#else
+    if (!(u32Value & HDA_GCTL_CRST))
+        return VINF_IOM_R3_MMIO_WRITE;
+    DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
+#endif
 
     if (u32Value & HDA_GCTL_CRST)
     {
-        DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
-
         /* Set the CRST bit to indicate that we're leaving reset mode. */
         HDA_REG(pThis, GCTL) |= HDA_GCTL_CRST;
         LogFunc(("Guest leaving HDA reset\n"));
-
-        DEVHDA_UNLOCK(pThis);
     }
     else
     {
 #ifdef IN_RING3
-        DEVHDA_LOCK(pThis);
-
         /* Enter reset state. */
         LogFunc(("Guest entering HDA reset with DMA(RIRB:%s, CORB:%s)\n",
@@ -1008,8 +1010,6 @@
 
         hdaGCTLReset(pThis);
-
-        DEVHDA_UNLOCK(pThis);
 #else
-        return VINF_IOM_R3_MMIO_WRITE;
+        AssertFailedReturnStmt(DEVHDA_UNLOCK(pThis), VINF_IOM_R3_MMIO_WRITE);
 #endif
     }
@@ -1017,13 +1017,10 @@
     if (u32Value & HDA_GCTL_FCNTRL)
     {
-        DEVHDA_LOCK(pThis);
-
         /* Flush: GSTS:1 set, see 6.2.6. */
         HDA_REG(pThis, GSTS) |= HDA_GSTS_FSTS;  /* Set the flush status. */
         /* DPLBASE and DPUBASE should be initialized with initial value (see 6.2.6). */
-
-        DEVHDA_UNLOCK(pThis);
-    }
-
+    }
+
+    DEVHDA_UNLOCK(pThis);
     return VINF_SUCCESS;
 }
@@ -1039,5 +1036,4 @@
 
     DEVHDA_UNLOCK(pThis);
-
     return VINF_SUCCESS;
 }
@@ -1075,22 +1071,22 @@
     const uint64_t u64FrontAbsWalClk  = pThis->SinkFront.pStream
                                       ? hdaStreamPeriodGetAbsElapsedWalClk(&pThis->SinkFront.pStream->State.Period) : 0;
-#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
-# error "Implement me!"
-#endif
+# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
+#  error "Implement me!"
+# endif
     const uint64_t u64LineInAbsWalClk = pThis->SinkLineIn.pStream
                                       ? hdaStreamPeriodGetAbsElapsedWalClk(&pThis->SinkLineIn.pStream->State.Period) : 0;
-#ifdef VBOX_WITH_HDA_MIC_IN
+# ifdef VBOX_WITH_HDA_MIC_IN
     const uint64_t u64MicInAbsWalClk  = pThis->SinkMicIn.pStream
                                       ? hdaStreamPeriodGetAbsElapsedWalClk(&pThis->SinkMicIn.pStream->State.Period) : 0;
-#endif
+# endif
 
     uint64_t u64WalClkNew = RT_MAX(u64WalClkCur, u64FrontAbsWalClk);
-#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
-# error "Implement me!"
-#endif
-             u64WalClkNew = RT_MAX(u64WalClkNew, u64LineInAbsWalClk);
-#ifdef VBOX_WITH_HDA_MIC_IN
-             u64WalClkNew = RT_MAX(u64WalClkNew, u64MicInAbsWalClk);
-#endif
+# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
+#  error "Implement me!"
+# endif
+    u64WalClkNew          = RT_MAX(u64WalClkNew, u64LineInAbsWalClk);
+# ifdef VBOX_WITH_HDA_MIC_IN
+    u64WalClkNew          = RT_MAX(u64WalClkNew, u64MicInAbsWalClk);
+# endif
 
     Log3Func(("%RU64 -> Front=%RU64, LineIn=%RU64 -> %RU64\n",
@@ -1123,5 +1119,4 @@
 {
     RT_NOREF(iReg);
-
     DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
 
@@ -1173,5 +1168,4 @@
 #ifdef IN_RING3
     RT_NOREF(iReg);
-
     DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
 
@@ -1243,5 +1237,4 @@
 {
     RT_NOREF_PV(iReg);
-
     DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
 
@@ -1259,6 +1252,5 @@
 
     int rc = hdaRegWriteU16(pThis, iReg, u32Value);
-    if (RT_FAILURE(rc))
-        AssertRCReturn(rc, rc);
+    AssertRCSuccess(rc);
 
     rc = hdaCORBCmdProcess(pThis);
@@ -1278,22 +1270,17 @@
 
     PHDASTREAM pStream = hdaGetStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, CBL, iReg));
-    if (!pStream)
-    {
+    if (pStream)
+    {
+        pStream->u32CBL = u32Value;
+        LogFlowFunc(("[SD%RU8] CBL=%RU32\n", pStream->u8SD, u32Value));
+    }
+    else
         LogFunc(("[SD%RU8] Warning: Changing SDCBL on non-attached stream (0x%x)\n",
                  HDA_SD_NUM_FROM_REG(pThis, CTL, iReg), u32Value));
 
-        DEVHDA_UNLOCK(pThis);
-        return hdaRegWriteU32(pThis, iReg, u32Value);
-    }
-
-    pStream->u32CBL = u32Value;
-
-    LogFlowFunc(("[SD%RU8] CBL=%RU32\n", pStream->u8SD, u32Value));
-
-    DEVHDA_UNLOCK(pThis);
-
     int rc2 = hdaRegWriteU32(pThis, iReg, u32Value);
     AssertRC(rc2);
 
+    DEVHDA_UNLOCK(pThis);
     return VINF_SUCCESS; /* Always return success to the MMIO handler. */
 #else  /* !IN_RING3 */
@@ -1315,5 +1302,5 @@
      * instead of 24-bit required for SDCTL. So just mask this here to be safe.
      */
-    u32Value = (u32Value & 0x00ffffff);
+    u32Value &= 0x00ffffff;
 
     bool fRun      = RT_BOOL(u32Value & HDA_SDCTL_RUN);
@@ -1338,6 +1325,7 @@
         LogFunc(("[SD%RU8] Warning: Invalid stream tag %RU8 specified!\n", uSD, uTag));
 
+        int rc = hdaRegWriteU24(pThis, iReg, u32Value);
         DEVHDA_UNLOCK_BOTH(pThis, uSD);
-        return hdaRegWriteU24(pThis, iReg, u32Value);
+        return rc;
     }
 
@@ -1493,6 +1481,7 @@
                          HDA_SD_NUM_FROM_REG(pThis, STS, iReg), u32Value));
 
+        int rc = hdaRegWriteU16(pThis, iReg, u32Value);
         DEVHDA_UNLOCK_BOTH(pThis, uSD);
-        return hdaRegWriteU16(pThis, iReg, u32Value);
+        return rc;
     }
 
@@ -1535,9 +1524,9 @@
     }
 
-#ifndef DEBUG
+# ifndef DEBUG
     hdaProcessInterrupt(pThis);
-#else
+# else
     hdaProcessInterrupt(pThis, __FUNCTION__);
-#endif
+# endif
 
     const uint64_t tsNow = TMTimerGet(pThis->pTimer[uSD]);
@@ -1545,7 +1534,7 @@
 
     const uint64_t cTicksElapsed     = tsNow - pStream->State.tsTransferLast;
-#ifdef LOG_ENABLED
+# ifdef LOG_ENABLED
     const uint64_t cTicksTransferred = pStream->State.cbTransferProcessed * pStream->State.cTicksPerByte;
-#endif
+# endif
 
     uint64_t cTicksToNext = pStream->State.cTransferTicks;
@@ -1609,39 +1598,33 @@
 
     if (HDA_REG_IND(pThis, iReg) == u32Value) /* Value already set? */
-    {
-        DEVHDA_UNLOCK(pThis);
-        return VINF_SUCCESS;
-    }
-
-    uint8_t uSD = HDA_SD_NUM_FROM_REG(pThis, LVI, iReg);
-
-    PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
-    if (!pStream)
-    {
-        AssertMsgFailed(("[SD%RU8] Warning: Changing SDLVI on non-attached stream (0x%x)\n", uSD, u32Value));
-
-        DEVHDA_UNLOCK(pThis);
-        return hdaRegWriteU16(pThis, iReg, u32Value);
-    }
-
-    /** @todo Validate LVI. */
-    pStream->u16LVI = u32Value;
-    LogFunc(("[SD%RU8] Updating LVI to %RU16\n", uSD, pStream->u16LVI));
+    { /* nothing to do */ }
+    else
+    {
+        uint8_t    uSD     = HDA_SD_NUM_FROM_REG(pThis, LVI, iReg);
+        PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
+        if (pStream)
+        {
+            /** @todo Validate LVI. */
+            pStream->u16LVI = u32Value;
+            LogFunc(("[SD%RU8] Updating LVI to %RU16\n", uSD, pStream->u16LVI));
 
 # ifdef HDA_USE_DMA_ACCESS_HANDLER
-    if (hdaGetDirFromSD(uSD) == PDMAUDIODIR_OUT)
-    {
-        /* Try registering the DMA handlers.
-         * As we can't be sure in which order LVI + BDL base are set, try registering in both routines. */
-        if (hdaStreamRegisterDMAHandlers(pThis, pStream))
-            LogFunc(("[SD%RU8] DMA logging enabled\n", pStream->u8SD));
-    }
+            if (hdaGetDirFromSD(uSD) == PDMAUDIODIR_OUT)
+            {
+                /* Try registering the DMA handlers.
+                 * As we can't be sure in which order LVI + BDL base are set, try registering in both routines. */
+                if (hdaStreamRegisterDMAHandlers(pThis, pStream))
+                    LogFunc(("[SD%RU8] DMA logging enabled\n", pStream->u8SD));
+            }
 # endif
+        }
+        else
+            AssertMsgFailed(("[SD%RU8] Warning: Changing SDLVI on non-attached stream (0x%x)\n", uSD, u32Value));
+
+        int rc2 = hdaRegWriteU16(pThis, iReg, u32Value);
+        AssertRC(rc2);
+    }
 
     DEVHDA_UNLOCK(pThis);
-
-    int rc2 = hdaRegWriteU16(pThis, iReg, u32Value);
-    AssertRC(rc2);
-
     return VINF_SUCCESS; /* Always return success to the MMIO handler. */
 #else  /* !IN_RING3 */
@@ -1671,6 +1654,7 @@
         AssertMsgFailed(("[SD%RU8] Warning: Changing FIFOW on non-attached stream (0x%x)\n", uSD, u32Value));
 
+        int rc = hdaRegWriteU16(pThis, iReg, u32Value);
         DEVHDA_UNLOCK(pThis);
-        return hdaRegWriteU16(pThis, iReg, u32Value);
+        return rc;
     }
 
@@ -1685,7 +1669,6 @@
             break;
         default:
-            LogRel(("HDA: Warning: Guest tried write unsupported FIFOW (0x%x) to stream #%RU8, defaulting to 32 bytes\n",
-                    u32Value, uSD));
-            AssertFailed();
+            ASSERT_GUEST_LOGREL_MSG_FAILED(("Guest tried write unsupported FIFOW (0x%x) to stream #%RU8, defaulting to 32 bytes\n",
+                                            u32Value, uSD));
             u32FIFOW = HDA_SDFIFOW_32B;
             break;
@@ -1696,6 +1679,4 @@
         pStream->u16FIFOW = hdaSDFIFOWToBytes(u32FIFOW);
         LogFunc(("[SD%RU8] Updating FIFOW to %RU32 bytes\n", uSD, pStream->u16FIFOW));
-
-        DEVHDA_UNLOCK(pThis);
 
         int rc2 = hdaRegWriteU16(pThis, iReg, u32FIFOW);
@@ -1734,6 +1715,7 @@
         AssertMsgFailed(("[SD%RU8] Warning: Changing FIFOS on non-attached stream (0x%x)\n", uSD, u32Value));
 
+        int rc = hdaRegWriteU16(pThis, iReg, u32Value);
         DEVHDA_UNLOCK(pThis);
-        return hdaRegWriteU16(pThis, iReg, u32Value);
+        return rc;
     }
 
@@ -1752,7 +1734,6 @@
 
         default:
-            LogRel(("HDA: Warning: Guest tried write unsupported FIFOS (0x%x) to stream #%RU8, defaulting to 192 bytes\n",
-                    u32Value, uSD));
-            AssertFailed();
+            ASSERT_GUEST_LOGREL_MSG_FAILED(("Guest tried write unsupported FIFOS (0x%x) to stream #%RU8, defaulting to 192 bytes\n",
+                                            u32Value, uSD));
             u32FIFOS = HDA_SDOFIFO_192B;
             break;
@@ -1764,12 +1745,9 @@
         LogFunc(("[SD%RU8] Updating FIFOS to %RU32 bytes\n", uSD, pStream->u16FIFOS));
 
-        DEVHDA_UNLOCK(pThis);
-
         int rc2 = hdaRegWriteU16(pThis, iReg, u32FIFOS);
         AssertRC(rc2);
     }
-    else
-        DEVHDA_UNLOCK(pThis);
-
+
+    DEVHDA_UNLOCK(pThis);
     return VINF_SUCCESS; /* Always return success to the MMIO handler. */
 #else  /* !IN_RING3 */
@@ -1780,4 +1758,5 @@
 
 #ifdef IN_RING3
+
 /**
  * Adds an audio output stream to the device setup using the given configuration.
@@ -1799,5 +1778,5 @@
 
     bool fUseFront = true; /* Always use front out by default. */
-#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
+# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
     bool fUseRear;
     bool fUseCenter;
@@ -1846,5 +1825,5 @@
         }
     }
-#else /* !VBOX_WITH_AUDIO_HDA_51_SURROUND */
+# else  /* !VBOX_WITH_AUDIO_HDA_51_SURROUND */
     /* Only support mono or stereo channels. */
     if (   pCfg->Props.cChannels != 1 /* Mono */
@@ -1853,5 +1832,5 @@
         rc = VERR_NOT_SUPPORTED;
     }
-#endif
+# endif /* !VBOX_WITH_AUDIO_HDA_51_SURROUND */
 
     if (rc == VERR_NOT_SUPPORTED)
@@ -1881,5 +1860,5 @@
         }
 
-#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
+# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
         if (   RT_SUCCESS(rc)
             && (fUseCenter || fUseLFE))
@@ -1909,5 +1888,5 @@
             rc = hdaCodecAddStream(pThis->pCodec, PDMAUDIOMIXERCTL_REAR, pCfg);
         }
-#endif /* VBOX_WITH_AUDIO_HDA_51_SURROUND */
+# endif /* VBOX_WITH_AUDIO_HDA_51_SURROUND */
 
     } while (0);
@@ -1942,5 +1921,5 @@
             break;
         }
-#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
+# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
         case PDMAUDIORECSOURCE_MIC:
         {
@@ -1948,5 +1927,5 @@
             break;
         }
-#endif
+# endif
         default:
             rc = VERR_NOT_SUPPORTED;
@@ -2019,7 +1998,7 @@
             {
                 case PDMAUDIORECSOURCE_LINE: enmMixerCtl = PDMAUDIOMIXERCTL_LINE_IN; break;
-#ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
+# ifdef VBOX_WITH_AUDIO_HDA_MIC_IN
                 case PDMAUDIORECSOURCE_MIC:  enmMixerCtl = PDMAUDIOMIXERCTL_MIC_IN;  break;
-#endif
+# endif
                 default:
                     rc = VERR_NOT_SUPPORTED;
@@ -2037,8 +2016,8 @@
             {
                 case PDMAUDIOPLAYBACKDEST_FRONT:      enmMixerCtl = PDMAUDIOMIXERCTL_FRONT;      break;
-#ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
+# ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
                 case PDMAUDIOPLAYBACKDEST_CENTER_LFE: enmMixerCtl = PDMAUDIOMIXERCTL_CENTER_LFE; break;
                 case PDMAUDIOPLAYBACKDEST_REAR:       enmMixerCtl = PDMAUDIOMIXERCTL_REAR;       break;
-#endif
+# endif
                 default:
                     rc = VERR_NOT_SUPPORTED;
@@ -2059,18 +2038,18 @@
     return rc;
 }
+
 #endif /* IN_RING3 */
 
 static int hdaRegWriteSDFMT(PHDASTATE pThis, uint32_t iReg, uint32_t u32Value)
 {
-#ifdef IN_RING3
+#ifdef IN_RING3 /** @todo this can be done from R0 & RC, even the logging. */
     DEVHDA_LOCK(pThis);
 
-    PHDASTREAM pStream = hdaGetStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, FMT, iReg));
-    if (!pStream)
-    {
+# ifdef LOG_ENABLED
+    if (!hdaGetStreamFromSD(pThis, HDA_SD_NUM_FROM_REG(pThis, FMT, iReg)))
         LogFunc(("[SD%RU8] Warning: Changing SDFMT on non-attached stream (0x%x)\n",
                  HDA_SD_NUM_FROM_REG(pThis, FMT, iReg), u32Value));
-        return hdaRegWriteU16(pThis, iReg, u32Value);
-    }
+# endif
+
 
     /* Write the wanted stream format into the register in any case.
@@ -2096,8 +2075,8 @@
 {
 #ifdef IN_RING3
+    DEVHDA_LOCK(pThis);
+
     int rc2 = hdaRegWriteU32(pThis, iReg, u32Value);
     AssertRC(rc2);
-
-    DEVHDA_LOCK(pThis);
 
     PHDASTREAM pStream = hdaGetStreamFromSD(pThis, uSD);
@@ -2125,5 +2104,4 @@
 
     DEVHDA_UNLOCK(pThis);
-
     return VINF_SUCCESS; /* Always return success to the MMIO handler. */
 #else  /* !IN_RING3 */
@@ -2154,7 +2132,8 @@
     }
 
+    int rc = hdaRegReadU32(pThis, iReg, pu32Value);
     DEVHDA_UNLOCK(pThis);
 
-    return hdaRegReadU32(pThis, iReg, pu32Value);
+    return rc;
 }
 
@@ -2162,5 +2141,4 @@
 {
     RT_NOREF_PV(iReg);
-
     DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
 
@@ -2219,5 +2197,4 @@
 {
     RT_NOREF(iReg);
-
     DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
 
@@ -2244,7 +2221,7 @@
     }
 
+    /* The remaining bits are O, see 6.2.22. */
+
     DEVHDA_UNLOCK(pThis);
-
-    /* The remaining bits are O, see 6.2.22. */
     return VINF_SUCCESS;
 }
@@ -2262,6 +2239,4 @@
     }
 
-    RT_NOREF(iReg);
-
     int rc = hdaRegWriteU16(pThis, iReg, u32Value);
     AssertRC(rc);
@@ -2276,11 +2251,10 @@
 {
     uint32_t iRegMem = g_aHdaRegMap[iReg].mem_idx;
+    DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
+
     int rc = hdaRegWriteU32(pThis, iReg, u32Value);
-    if (RT_FAILURE(rc))
-        AssertRCReturn(rc, rc);
-
-    DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
-
-    switch(iReg)
+    Assert(rc == VINF_SUCCESS);
+
+    switch (iReg)
     {
         case HDA_REG_CORBLBASE:
@@ -2328,5 +2302,4 @@
 {
     RT_NOREF_PV(iReg);
-
     DEVHDA_LOCK_RETURN(pThis, VINF_IOM_R3_MMIO_WRITE);
 
@@ -2334,11 +2307,12 @@
     HDA_REG(pThis, RIRBSTS) &= ~(v & u32Value);
 
+#ifndef DEBUG
+    int rc = hdaProcessInterrupt(pThis);
+#else
+    int rc = hdaProcessInterrupt(pThis, __FUNCTION__);
+#endif
+
     DEVHDA_UNLOCK(pThis);
-
-#ifndef DEBUG
-    return hdaProcessInterrupt(pThis);
-#else
-    return hdaProcessInterrupt(pThis, __FUNCTION__);
-#endif
+    return rc;
 }
 
@@ -2392,5 +2366,5 @@
  * Adds a driver stream to a specific mixer sink.
  *
- * @returns IPRT status code.
+ * @returns IPRT status code (ignored by caller).
  * @param   pThis               HDA state.
  * @param   pMixSink            Audio mixer sink to add audio streams to.
@@ -2409,10 +2383,4 @@
     if (!pStreamCfg)
         return VERR_NO_MEMORY;
-
-    if (!RTStrPrintf(pStreamCfg->szName, sizeof(pStreamCfg->szName), "%s", pCfg->szName))
-    {
-        RTMemFree(pStreamCfg);
-        return VERR_BUFFER_OVERFLOW;
-    }
 
     LogFunc(("[LUN#%RU8] %s\n", pDrv->uLUN, pStreamCfg->szName));
@@ -2483,9 +2451,5 @@
     }
 
-    if (pStreamCfg)
-    {
-        RTMemFree(pStreamCfg);
-        pStreamCfg = NULL;
-    }
+    RTMemFree(pStreamCfg);
 
     LogFlowFuncLeaveRC(rc);
