From daad4f8d4178982810544fd370d606598def2a2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9=20van=20Geldorp?= Date: Fri, 7 Nov 2003 17:40:02 +0000 Subject: [PATCH] Simplify code by passing PDEVICE_OBJECT instead of HANDLE around for miniport device, fixing a reference-counting bug I introduced earlier along the way. svn path=/trunk/; revision=6560 --- reactos/include/win32k/dc.h | 2 +- reactos/include/win32k/driver.h | 2 +- reactos/include/win32k/misc.h | 3 -- reactos/subsys/win32k/main/dllmain.c | 7 +-- reactos/subsys/win32k/misc/driver.c | 33 ++++++++------ reactos/subsys/win32k/objects/dc.c | 67 +++++----------------------- 6 files changed, 32 insertions(+), 82 deletions(-) diff --git a/reactos/include/win32k/dc.h b/reactos/include/win32k/dc.h index 34593343198..eae4b3ea6ed 100644 --- a/reactos/include/win32k/dc.h +++ b/reactos/include/win32k/dc.h @@ -114,7 +114,7 @@ typedef struct GDIINFO GDIInfo; DEVINFO DevInfo; DRIVER_FUNCTIONS DriverFunctions; - HANDLE DisplayDevice; + PDEVICE_OBJECT VideoDeviceObject; } GDIDEVICE; /* Internal functions */ diff --git a/reactos/include/win32k/driver.h b/reactos/include/win32k/driver.h index c3a800c9504..2ef29788fb7 100644 --- a/reactos/include/win32k/driver.h +++ b/reactos/include/win32k/driver.h @@ -160,7 +160,7 @@ typedef struct _DRIVER_FUNCTIONS BOOL DRIVER_RegisterDriver(LPCWSTR Name, PGD_ENABLEDRIVER EnableDriver); PGD_ENABLEDRIVER DRIVER_FindDDIDriver(LPCWSTR Name); -HANDLE DRIVER_FindMPDriver(LPCWSTR Name); +PDEVICE_OBJECT DRIVER_FindMPDriver(LPCWSTR Name); BOOL DRIVER_BuildDDIFunctions(PDRVENABLEDATA DED, PDRIVER_FUNCTIONS DF); BOOL DRIVER_UnregisterDriver(LPCWSTR Name); diff --git a/reactos/include/win32k/misc.h b/reactos/include/win32k/misc.h index f6d49f1d732..c0b689370e1 100644 --- a/reactos/include/win32k/misc.h +++ b/reactos/include/win32k/misc.h @@ -1,9 +1,6 @@ #ifndef __WIN32K_MISC_H #define __WIN32K_MISC_H -/* Process context in which miniport driver is opened/used */ -extern PEPROCESS Win32kDeviceProcess; - BOOLEAN STDCALL Win32kInitialize (VOID); diff --git a/reactos/subsys/win32k/main/dllmain.c b/reactos/subsys/win32k/main/dllmain.c index 1136805f1d4..18d834a7390 100644 --- a/reactos/subsys/win32k/main/dllmain.c +++ b/reactos/subsys/win32k/main/dllmain.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: dllmain.c,v 1.47 2003/11/03 18:51:40 ekohl Exp $ +/* $Id: dllmain.c,v 1.48 2003/11/07 17:40:02 gvg Exp $ * * Entry Point for win32k.sys */ @@ -48,9 +48,6 @@ extern SSDT Win32kSSDT[]; extern SSPT Win32kSSPT[]; extern ULONG Win32kNumberOfSysCalls; -PEPROCESS Win32kDeviceProcess; - - NTSTATUS STDCALL Win32kProcessCallback (struct _EPROCESS *Process, BOOLEAN Create) @@ -270,8 +267,6 @@ Win32kInitialize (VOID) { DPRINT("in Win32kInitialize\n"); - Win32kDeviceProcess = PsGetCurrentProcess(); - InitGdiObjectHandleTable (); // Initialize FreeType library diff --git a/reactos/subsys/win32k/misc/driver.c b/reactos/subsys/win32k/misc/driver.c index 66dca6e96f6..5f2da037204 100644 --- a/reactos/subsys/win32k/misc/driver.c +++ b/reactos/subsys/win32k/misc/driver.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: driver.c,v 1.30 2003/10/25 18:45:13 gvg Exp $ +/* $Id: driver.c,v 1.31 2003/11/07 17:40:02 gvg Exp $ * * GDI Driver support routines * (mostly swiped from Wine) @@ -235,21 +235,15 @@ BOOL DRIVER_BuildDDIFunctions(PDRVENABLEDATA DED, typedef VP_STATUS (*PMP_DRIVERENTRY)(PVOID, PVOID); -HANDLE DRIVER_FindMPDriver(LPCWSTR Name) +PDEVICE_OBJECT DRIVER_FindMPDriver(LPCWSTR Name) { OBJECT_ATTRIBUTES ObjectAttributes; UNICODE_STRING DeviceName; IO_STATUS_BLOCK Iosb; HANDLE DisplayHandle; NTSTATUS Status; - PEPROCESS CurrentProcess; - - CurrentProcess = PsGetCurrentProcess(); - if (CurrentProcess != Win32kDeviceProcess) - { - /* Switch to process context in which handle is to be valid */ - KeAttachProcess(Win32kDeviceProcess); - } + PFILE_OBJECT VideoFileObject; + PDEVICE_OBJECT VideoDeviceObject; RtlInitUnicodeStringFromLiteral(&DeviceName, L"\\??\\DISPLAY1"); InitializeObjectAttributes(&ObjectAttributes, @@ -263,10 +257,21 @@ HANDLE DRIVER_FindMPDriver(LPCWSTR Name) &Iosb, 0, FILE_SYNCHRONOUS_IO_ALERT); - - if (CurrentProcess != Win32kDeviceProcess) + if (NT_SUCCESS(Status)) { - KeDetachProcess(); + Status = ObReferenceObjectByHandle(DisplayHandle, + FILE_READ_DATA | FILE_WRITE_DATA, + IoFileObjectType, + KernelMode, + (PVOID *)&VideoFileObject, + NULL); + if (NT_SUCCESS(Status)) + { + VideoDeviceObject = VideoFileObject->DeviceObject; + ObReferenceObject(VideoDeviceObject); + ObDereferenceObject(VideoFileObject); + } + ZwClose(DisplayHandle); } if (!NT_SUCCESS(Status)) @@ -276,7 +281,7 @@ HANDLE DRIVER_FindMPDriver(LPCWSTR Name) return(NULL); } - return(DisplayHandle); + return VideoDeviceObject; } diff --git a/reactos/subsys/win32k/objects/dc.c b/reactos/subsys/win32k/objects/dc.c index d8ab1cf6caa..cfd1a4429d9 100644 --- a/reactos/subsys/win32k/objects/dc.c +++ b/reactos/subsys/win32k/objects/dc.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: dc.c,v 1.95 2003/11/05 22:46:05 gvg Exp $ +/* $Id: dc.c,v 1.96 2003/11/07 17:40:02 gvg Exp $ * * DC.C - Device context functions * @@ -192,7 +192,7 @@ NtGdiCreateCompatableDC(HDC hDC) /* DriverName is copied in the AllocDC routine */ if(OrigDC == NULL) { - NewDC->DeviceDriver = PrimarySurface.DisplayDevice; + NewDC->DeviceDriver = (HANDLE) PrimarySurface.VideoDeviceObject; } else { @@ -312,23 +312,6 @@ FindDriverFileNames(PUNICODE_STRING DriverFileNames) return TRUE; } -static void FASTCALL -CloseMiniport() -{ - PEPROCESS CurrentProcess; - - CurrentProcess = PsGetCurrentProcess(); - if (CurrentProcess != Win32kDeviceProcess) - { - KeAttachProcess(Win32kDeviceProcess); - } - ZwClose(PrimarySurface.DisplayDevice); - if (CurrentProcess != Win32kDeviceProcess) - { - KeDetachProcess(); - } -} - static NTSTATUS STDCALL DevModeCallback(IN PWSTR ValueName, IN ULONG ValueType, @@ -501,13 +484,10 @@ NtGdiCreatePrimarySurface(LPCWSTR Driver, PWSTR CurrentName; BOOL GotDriver; BOOL DoDefault; - NTSTATUS Status; - PFILE_OBJECT FileObject; - PEPROCESS CurrentProcess; extern void FASTCALL IntInitDesktopWindow(ULONG Width, ULONG Height); /* Open the miniport driver */ - if ((PrimarySurface.DisplayDevice = DRIVER_FindMPDriver(Driver)) == NULL) + if ((PrimarySurface.VideoDeviceObject = DRIVER_FindMPDriver(Driver)) == NULL) { DPRINT1("FindMPDriver failed\n"); return(FALSE); @@ -565,7 +545,7 @@ NtGdiCreatePrimarySurface(LPCWSTR Driver, RtlFreeUnicodeString(&DriverFileNames); if (! GotDriver) { - CloseMiniport(); + ObDereferenceObject(PrimarySurface.VideoDeviceObject); DPRINT1("No suitable DDI driver found\n"); return FALSE; } @@ -580,36 +560,11 @@ NtGdiCreatePrimarySurface(LPCWSTR Driver, /* Construct DDI driver function dispatch table */ if (!DRIVER_BuildDDIFunctions(&DED, &PrimarySurface.DriverFunctions)) { - CloseMiniport(); + ObDereferenceObject(PrimarySurface.VideoDeviceObject); DPRINT1("BuildDDIFunctions failed\n"); return(FALSE); } - CurrentProcess = PsGetCurrentProcess(); - if (CurrentProcess != Win32kDeviceProcess) - { - /* Switch to process context in which device handle is valid */ - KeAttachProcess(Win32kDeviceProcess); - } - - Status = ObReferenceObjectByHandle(PrimarySurface.DisplayDevice, - FILE_READ_DATA | FILE_WRITE_DATA, - IoFileObjectType, - KernelMode, - (PVOID *)&FileObject, - NULL); - if (CurrentProcess != Win32kDeviceProcess) - { - KeDetachProcess(); - } - - if (!NT_SUCCESS(Status)) - { - CloseMiniport(); - DPRINT1("Referencing miniport device failed with status 0x%08x\n", Status); - return FALSE; - } - /* Allocate a phyical device handle from the driver */ if (SetupDevMode(&PrimarySurface.DMW)) { @@ -624,7 +579,7 @@ NtGdiCreatePrimarySurface(LPCWSTR Driver, &PrimarySurface.DevInfo, NULL, L"", - (HANDLE) (FileObject->DeviceObject)); + (HANDLE) (PrimarySurface.VideoDeviceObject)); DoDefault = (NULL == PrimarySurface.PDev); if (DoDefault) { @@ -650,12 +605,11 @@ NtGdiCreatePrimarySurface(LPCWSTR Driver, &PrimarySurface.DevInfo, NULL, L"", - (HANDLE) (FileObject->DeviceObject)); + (HANDLE) (PrimarySurface.VideoDeviceObject)); if (NULL == PrimarySurface.PDev) { - ObDereferenceObject(FileObject); - CloseMiniport(); + ObDereferenceObject(PrimarySurface.VideoDeviceObject); DPRINT1("DrvEnablePDEV with default parameters failed\n"); DPRINT1("Perhaps DDI driver doesn't match miniport driver?\n"); return FALSE; @@ -691,8 +645,7 @@ NtGdiCreatePrimarySurface(LPCWSTR Driver, PrimarySurface.DriverFunctions.EnableSurface(PrimarySurface.PDev); if (NULL == PrimarySurface.Handle) { - ObDereferenceObject(FileObject); - CloseMiniport(); + ObDereferenceObject(PrimarySurface.VideoDeviceObject); DPRINT1("DrvEnableSurface failed\n"); return FALSE; } @@ -824,7 +777,7 @@ NtGdiDeleteDC(HDC DCHandle) CHECKPOINT; DCToDelete->DriverFunctions.DisablePDev(DCToDelete->PDev); - CloseMiniport(); + ObDereferenceObject(PrimarySurface.VideoDeviceObject); PrimarySurfaceCreated = FALSE; }