[vbox-dev] vbox-dev at virtualbox.org

lin zuojian manjian2006 at gmail.com
Fri Dec 25 07:44:36 UTC 2015


Hi friends,
    I find my vbox will hanging after a short while playing Android
    games. Then I use debugger and figure out linux kernel driver of
    intel ac97 is dead looping. And this is result from the
    multithreading data inconsistency.
    So I use my patch:
diff --git a/include/iprt/AutoLock.h b/include/iprt/AutoLock.h
new file mode 100644
index 0000000..c15a19b
--- /dev/null
+++ b/include/iprt/AutoLock.h
@@ -0,0 +1,48 @@
+#ifndef __XX_AUTOLOCK__
+#define __XX_AUTOLOCK__
+#include <iprt/critsect.h>
+
+class CLock
+{
+public:
+	CLock(void){
+		Init();
+	}
+	~CLock(){
+		Close();
+	}
+	void Lock(){
+		RTCritSectEnter(&m_lock);
+	}
+	void UnLock(){
+		RTCritSectLeave(&m_lock);
+	}
+private:
+	RTCRITSECT m_lock;
+	void Init(){
+		RTCritSectInit(&m_lock);
+	}
+	void Close(){
+		RTCritSectDelete(&m_lock);
+	}
+};
+
+class CAutoLock{
+public:
+	CAutoLock(CLock *pLock){
+		m_pLock=pLock;
+		if (NULL!=m_pLock)
+		{
+			m_pLock->Lock();
+		}
+	}
+	~CAutoLock(){
+		if (NULL!=m_pLock)
+		{
+			m_pLock->UnLock();
+		}
+	}
+private:
+	CLock * m_pLock;
+};
+#endif
\ No newline at end of file
diff --git a/src/VBox/Devices/Audio/DevIchAc97.cpp b/src/VBox/Devices/Audio/DevIchAc97.cpp
index 84ac678..ddc199b 100644
--- a/src/VBox/Devices/Audio/DevIchAc97.cpp
+++ b/src/VBox/Devices/Audio/DevIchAc97.cpp
@@ -24,6 +24,8 @@
 #include <iprt/uuid.h>
 #include <iprt/string.h>
 
+#include <iprt/AutoLock.h>
+
 #include "VBoxDD.h"
 
 extern "C" {
@@ -186,7 +188,7 @@ typedef struct AC97BusMasterRegs
 } AC97BusMasterRegs;
 /** Pointer to a AC97 bus master register. */
 typedef AC97BusMasterRegs *PAC97BMREG;
-
+static CLock g_alock;
 typedef struct AC97STATE
 {
     /** The PCI device state. */
@@ -743,6 +745,7 @@ static int read_audio(PAC97STATE pThis, PAC97BMREG pReg, int max, int *stop)
 
 static void transfer_audio(PAC97STATE pThis, int index, int elapsed)
 {
+	CAutoLock alock(&g_alock);
     PAC97BMREG pReg = &pThis->bm_regs[index];
     int written = 0;
     int stop = 0;
@@ -853,6 +856,7 @@ static void po_callback(void *opaque, int free)
  */
 static DECLCALLBACK(int) ichac97IOPortNABMRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
 {
+	CAutoLock alock(&g_alock);
     PAC97STATE pThis = (PAC97STATE)pvUser;
 
     switch (cb)
@@ -1013,6 +1017,7 @@ static DECLCALLBACK(int) ichac97IOPortNABMRead(PPDMDEVINS pDevIns, void *pvUser,
  */
 static DECLCALLBACK(int) ichac97IOPortNABMWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
 {
+	CAutoLock alock(&g_alock);
     PAC97STATE pThis = (PAC97STATE)pvUser;
 
     switch (cb)
@@ -1151,6 +1156,7 @@ static DECLCALLBACK(int) ichac97IOPortNABMWrite(PPDMDEVINS pDevIns, void *pvUser
  */
 static DECLCALLBACK(int) ichac97IOPortNAMRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
 {
+	CAutoLock alock(&g_alock);
     PAC97STATE pThis = (PAC97STATE)pvUser;
 
     switch (cb)
@@ -1197,6 +1203,7 @@ static DECLCALLBACK(int) ichac97IOPortNAMRead(PPDMDEVINS pDevIns, void *pvUser,
  */
 static DECLCALLBACK(int) ichac97IOPortNAMWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
 {
+	CAutoLock alock(&g_alock);
     PAC97STATE pThis = (PAC97STATE)pvUser;
 
     switch (cb)
@@ -1457,6 +1464,7 @@ static DECLCALLBACK(void *) ichac97QueryInterface(struct PDMIBASE *pInterface, c
  */
 static DECLCALLBACK(void)  ac97Reset(PPDMDEVINS pDevIns)
 {
+	CAutoLock alock(&g_alock);
     PAC97STATE pThis = PDMINS_2_DATA(pDevIns, AC97STATE *);
 
     /*

     The patch is mean to protect the data accessing from different
     thread between CPU thread which is invoking timer and
     the CPU thread which is runing Linux intel ac97 drivers.
--
Lin Zuojian



More information about the vbox-dev mailing list