@@ -160,6 +160,9 @@ NTSTATUS VioGpuAdapterLite::SetCurrentModeExt(CURRENT_MODE* pCurrentMode)
160160 return status;
161161 }
162162
163+ // Adding a lock here to prevent potential memory dereferencing issues,
164+ // as m_screen is utilized across multiple threads
165+ KeWaitForMutexObject (&m_screen_mutex, Executive, KernelMode, FALSE , NULL );
163166 DBGPRINT (" ScreenNum = %d, Mode = %dx%d\n " , pCurrentMode->DispInfo .TargetId , pCurrentMode->DispInfo .Width , pCurrentMode->DispInfo .Height );
164167
165168 for (ULONG idx = 0 ; idx < m_screen[pCurrentMode->DispInfo .TargetId ].GetModeCount (); idx++)
@@ -183,6 +186,7 @@ NTSTATUS VioGpuAdapterLite::SetCurrentModeExt(CURRENT_MODE* pCurrentMode)
183186 break ;
184187 }
185188
189+ KeReleaseMutex (&m_screen_mutex, FALSE );
186190 return status;
187191}
188192
@@ -198,6 +202,9 @@ NTSTATUS VioGpuAdapterLite::VioGpuAdapterLiteInit()
198202 VioGpuDbgBreak ();
199203 return status;
200204 }
205+
206+ KeInitializeMutex (&m_screen_mutex, 0 );
207+
201208 status = VirtIoDeviceInit ();
202209 if (!NT_SUCCESS (status)) {
203210 DBGPRINT (" Failed to initialize virtio device, error %x\n " , status);
@@ -380,7 +387,20 @@ PBYTE VioGpuAdapterLite::GetEdidData(UINT Id)
380387{
381388 PAGED_CODE ();
382389 TRACING ();
383- return m_bEDID ? m_screen[Id].m_EDIDs : (PBYTE)(&g_gpu_edid);// .data;
390+ PBYTE ret;
391+
392+ if (m_bEDID) {
393+ // Adding a lock here to prevent potential memory dereferencing issues,
394+ // as m_screen is utilized across multiple threads
395+ KeWaitForMutexObject (&m_screen_mutex, Executive, KernelMode, FALSE , NULL );
396+ ret = m_screen[Id].m_EDIDs ;
397+ KeReleaseMutex (&m_screen_mutex, FALSE );
398+ }
399+ else {
400+ ret = (PBYTE)(&g_gpu_edid);
401+ }
402+
403+ return ret;
384404}
385405
386406NTSTATUS VioGpuAdapterLite::HWInit (WDFCMRESLIST pResList, DXGK_DISPLAY_INFORMATION* pDispInfo)
@@ -787,10 +807,14 @@ BOOLEAN VioGpuAdapterLite::GetEdids(UINT32 screen_num)
787807
788808 PGPU_VBUFFER vbuf = NULL ;
789809
810+ // Adding a lock here to prevent potential memory dereferencing issues,
811+ // as m_screen is utilized across multiple threads
812+ KeWaitForMutexObject (&m_screen_mutex, Executive, KernelMode, FALSE , NULL );
790813 if (m_CtrlQueue.AskEdidInfo (&vbuf, screen_num, &m_screen[screen_num].m_EdidEvent ) &&
791814 m_CtrlQueue.GetEdidInfo (vbuf, screen_num, m_screen[screen_num].m_EDIDs )) {
792815 m_bEDID = TRUE ;
793816 }
817+ KeReleaseMutex (&m_screen_mutex, FALSE );
794818 m_CtrlQueue.ReleaseBuffer (vbuf);
795819
796820 return TRUE ;
@@ -881,6 +905,9 @@ NTSTATUS VioGpuAdapterLite::GetModeList(DXGK_DISPLAY_INFORMATION* pDispInfo)
881905
882906 TRACING ();
883907
908+ // Adding a lock here to prevent potential memory dereferencing issues,
909+ // as m_screen is utilized across multiple threads
910+ KeWaitForMutexObject (&m_screen_mutex, Executive, KernelMode, FALSE , NULL );
884911 for (UINT32 i = 0 ; i < m_u32NumScanouts; i++) {
885912
886913 UINT ModeCount = 0 ;
@@ -971,6 +998,7 @@ NTSTATUS VioGpuAdapterLite::GetModeList(DXGK_DISPLAY_INFORMATION* pDispInfo)
971998 m_screen[i].m_ModeInfo [idx].VisScreenHeight );
972999 }
9731000 }
1001+ KeReleaseMutex (&m_screen_mutex, FALSE );
9741002 return Status;
9751003}
9761004PAGED_CODE_SEG_END
@@ -1060,12 +1088,15 @@ void VioGpuAdapterLite::ThreadWorkRoutine(void)
10601088void VioGpuAdapterLite::ConfigChanged (void )
10611089{
10621090 TRACING ();
1091+ NTSTATUS status = STATUS_SUCCESS;
10631092 UINT32 events_read, events_clear = 0 ;
10641093 virtio_get_config (&m_VioDev, FIELD_OFFSET (GPU_CONFIG, events_read),
10651094 &events_read, sizeof (m_u32NumScanouts));
10661095 if (events_read & VIRTIO_GPU_EVENT_DISPLAY) {
1067- for (UINT32 i = 0 ; i < m_u32NumScanouts; i++) {
1068- GetDisplayInfo (i);
1096+ status = GetModeList (&DisplayInfo);
1097+ if (!NT_SUCCESS (status)) {
1098+ ERR (" GetModeList failed with %x\n " , status);
1099+ VioGpuDbgBreak ();
10691100 }
10701101 if (hpd_event) {
10711102 DBGPRINT (" Sending Hot Plug event to UMD\n " );
@@ -1074,10 +1105,6 @@ void VioGpuAdapterLite::ConfigChanged(void)
10741105 events_clear |= VIRTIO_GPU_EVENT_DISPLAY;
10751106 virtio_set_config (&m_VioDev, FIELD_OFFSET (GPU_CONFIG, events_clear),
10761107 &events_clear, sizeof (m_u32NumScanouts));
1077- // UpdateChildStatus(FALSE);
1078- // ProcessEdid();
1079- // TODO: Enable following if needed
1080- // UpdateChildStatus(TRUE);
10811108 }
10821109}
10831110
0 commit comments