2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Driver entry and initialization of win32k
5 * FILE: subsystems/win32/win32k/main/main.c
17 PGDI_HANDLE_TABLE NTAPI
GDIOBJ_iAllocHandleTable(OUT PSECTION_OBJECT
*SectionObject
);
18 BOOL NTAPI
GDI_CleanupForProcess (struct _EPROCESS
*Process
);
19 NTSTATUS NTAPI
UserDestroyThreadInfo(struct _ETHREAD
*Thread
);
21 HANDLE GlobalUserHeap
= NULL
;
22 PSECTION_OBJECT GlobalUserHeapSection
= NULL
;
24 PSERVERINFO gpsi
= NULL
; // Global User Server Information.
27 PPROCESSINFO ppiScrnSaver
;
29 extern ULONG_PTR Win32kSSDT
[];
30 extern UCHAR Win32kSSPT
[];
31 extern ULONG Win32kNumberOfSysCalls
;
36 DbgPreServiceHook(ULONG ulSyscallId
, PULONG_PTR pulArguments
)
38 GdiDbgPreServiceHook(ulSyscallId
, pulArguments
);
39 UserDbgPreServiceHook(ulSyscallId
, pulArguments
);
44 DbgPostServiceHook(ULONG ulSyscallId
, ULONG_PTR ulResult
)
46 ulResult
= GdiDbgPostServiceHook(ulSyscallId
, ulResult
);
47 ulResult
= UserDbgPostServiceHook(ulSyscallId
, ulResult
);
54 Win32kProcessCallback(struct _EPROCESS
*Process
,
57 PPROCESSINFO ppiCurrent
;
58 DECLARE_RETURN(NTSTATUS
);
68 PVOID UserBase
= NULL
;
69 PRTL_USER_PROCESS_PARAMETERS pParams
= Process
->Peb
->ProcessParameters
;
72 ASSERT(PsGetProcessWin32Process(Process
) == NULL
);
74 ppiCurrent
= ExAllocatePoolWithTag(NonPagedPool
,
78 if (ppiCurrent
== NULL
)
80 ERR_CH(UserProcess
, "Failed to allocate ppi for PID:0x%lx\n", HandleToUlong(Process
->UniqueProcessId
));
81 RETURN( STATUS_NO_MEMORY
);
84 RtlZeroMemory(ppiCurrent
, sizeof(PROCESSINFO
));
86 PsSetProcessWin32Process(Process
, ppiCurrent
);
89 DbgInitDebugChannels();
92 TRACE_CH(UserProcess
,"Allocated ppi 0x%p for PID:0x%lx\n", ppiCurrent
, HandleToUlong(Process
->UniqueProcessId
));
94 /* map the global heap into the process */
96 Status
= MmMapViewOfSection(GlobalUserHeapSection
,
97 PsGetCurrentProcess(),
105 PAGE_EXECUTE_READ
); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
106 if (!NT_SUCCESS(Status
))
108 TRACE_CH(UserProcess
,"Failed to map the global heap! 0x%x\n", Status
);
111 ppiCurrent
->HeapMappings
.Next
= NULL
;
112 ppiCurrent
->HeapMappings
.KernelMapping
= (PVOID
)GlobalUserHeap
;
113 ppiCurrent
->HeapMappings
.UserMapping
= UserBase
;
114 ppiCurrent
->HeapMappings
.Count
= 1;
116 InitializeListHead(&ppiCurrent
->MenuListHead
);
118 InitializeListHead(&ppiCurrent
->GDIBrushAttrFreeList
);
119 InitializeListHead(&ppiCurrent
->GDIDcAttrFreeList
);
121 InitializeListHead(&ppiCurrent
->PrivateFontListHead
);
122 ExInitializeFastMutex(&ppiCurrent
->PrivateFontListLock
);
124 InitializeListHead(&ppiCurrent
->DriverObjListHead
);
125 ExInitializeFastMutex(&ppiCurrent
->DriverObjListLock
);
127 ppiCurrent
->KeyboardLayout
= W32kGetDefaultKeyLayout();
128 if (!EngCreateEvent((PEVENT
*)&ppiCurrent
->InputIdleEvent
))
133 KeInitializeEvent(ppiCurrent
->InputIdleEvent
, NotificationEvent
, FALSE
);
136 /* map the gdi handle table to user land */
137 Process
->Peb
->GdiSharedHandleTable
= GDI_MapHandleTable(Process
);
138 Process
->Peb
->GdiDCAttributeList
= GDI_BATCH_LIMIT
;
139 pParams
= Process
->Peb
->ProcessParameters
;
141 ppiCurrent
->peProcess
= Process
;
142 /* setup process flags */
143 ppiCurrent
->W32PF_flags
= W32PF_THREADCONNECTED
;
146 pParams
->WindowFlags
& STARTF_SCRNSAVER
)
148 ppiScrnSaver
= ppiCurrent
;
149 ppiCurrent
->W32PF_flags
|= W32PF_SCREENSAVER
;
152 /* Create pools for GDI object attributes */
153 ppiCurrent
->pPoolDcAttr
= GdiPoolCreate(sizeof(DC_ATTR
), 'acdG');
154 ppiCurrent
->pPoolBrushAttr
= GdiPoolCreate(sizeof(BRUSH_ATTR
), 'arbG');
155 ppiCurrent
->pPoolRgnAttr
= GdiPoolCreate(sizeof(RGN_ATTR
), 'agrG');
156 ASSERT(ppiCurrent
->pPoolDcAttr
);
157 ASSERT(ppiCurrent
->pPoolBrushAttr
);
158 ASSERT(ppiCurrent
->pPoolRgnAttr
);
162 /* Get the Win32 Process */
163 ppiCurrent
= PsGetProcessWin32Process(Process
);
167 TRACE_CH(UserProcess
, "Destroying ppi 0x%p\n", ppiCurrent
);
168 ppiCurrent
->W32PF_flags
|= W32PF_TERMINATED
;
170 if (ppiScrnSaver
== ppiCurrent
)
173 if (ppiCurrent
->InputIdleEvent
)
175 EngFreeMem(ppiCurrent
->InputIdleEvent
);
176 ppiCurrent
->InputIdleEvent
= NULL
;
179 IntCleanupMenus(Process
, ppiCurrent
);
180 IntCleanupCurIcons(Process
, ppiCurrent
);
183 GDI_CleanupForProcess(Process
);
185 co_IntGraphicsCheck(FALSE
);
188 * Deregister logon application automatically
190 if(LogonProcess
== ppiCurrent
)
195 /* Close the startup desktop */
196 if(ppiCurrent
->rpdeskStartup
)
197 ObDereferenceObject(ppiCurrent
->rpdeskStartup
);
198 if(ppiCurrent
->hdeskStartup
)
199 ZwClose(ppiCurrent
->hdeskStartup
);
201 /* Close the current window station */
202 UserSetProcessWindowStation(NULL
);
204 /* Destroy GDI pools */
205 GdiPoolDestroy(ppiCurrent
->pPoolDcAttr
);
206 GdiPoolDestroy(ppiCurrent
->pPoolBrushAttr
);
207 GdiPoolDestroy(ppiCurrent
->pPoolRgnAttr
);
209 if (gppiInputProvider
== ppiCurrent
) gppiInputProvider
= NULL
;
211 TRACE_CH(UserProcess
,"Freeing ppi 0x%p\n", ppiCurrent
);
213 /* Ftee the PROCESSINFO */
214 PsSetProcessWin32Process(Process
, NULL
);
215 ExFreePoolWithTag(ppiCurrent
, USERTAG_PROCESSINFO
);
218 RETURN( STATUS_SUCCESS
);
226 UserCreateThreadInfo(struct _ETHREAD
*Thread
)
228 struct _EPROCESS
*Process
;
230 PTHREADINFO ptiCurrent
;
232 NTSTATUS Status
= STATUS_SUCCESS
;
235 Process
= Thread
->ThreadsProcess
;
237 pTeb
= NtCurrentTeb();
241 ptiCurrent
= ExAllocatePoolWithTag(NonPagedPool
,
244 if (ptiCurrent
== NULL
)
246 ERR_CH(UserThread
, "Failed to allocate pti for TID %p\n", Thread
->Cid
.UniqueThread
);
247 return STATUS_NO_MEMORY
;
250 TRACE_CH(UserThread
,"Create pti 0x%p eThread 0x%p\n", ptiCurrent
, Thread
);
252 RtlZeroMemory(ptiCurrent
, sizeof(THREADINFO
));
254 PsSetThreadWin32Thread(Thread
, ptiCurrent
);
255 pTeb
->Win32ThreadInfo
= ptiCurrent
;
256 ptiCurrent
->pClientInfo
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
258 TRACE_CH(UserThread
, "Allocated pti 0x%p for TID %p\n", ptiCurrent
, Thread
->Cid
.UniqueThread
);
260 /* Initialize the THREADINFO */
261 InitializeListHead(&ptiCurrent
->WindowListHead
);
262 InitializeListHead(&ptiCurrent
->W32CallbackListHead
);
263 InitializeListHead(&ptiCurrent
->PtiLink
);
264 for (i
= 0; i
< NB_HOOKS
; i
++)
266 InitializeListHead(&ptiCurrent
->aphkStart
[i
]);
268 ptiCurrent
->pEThread
= Thread
;
269 ptiCurrent
->ppi
= PsGetCurrentProcessWin32Process();
270 ptiCurrent
->ptiSibling
= ptiCurrent
->ppi
->ptiList
;
271 ptiCurrent
->ppi
->ptiList
= ptiCurrent
;
272 ptiCurrent
->ppi
->cThreads
++;
273 ptiCurrent
->MessageQueue
= MsqCreateMessageQueue(Thread
);
274 if(ptiCurrent
->MessageQueue
== NULL
)
276 ERR_CH(UserThread
,"Failed to allocate message loop\n");
277 Status
= STATUS_NO_MEMORY
;
280 ptiCurrent
->KeyboardLayout
= W32kGetDefaultKeyLayout();
281 if (ptiCurrent
->KeyboardLayout
)
282 UserReferenceObject(ptiCurrent
->KeyboardLayout
);
283 ptiCurrent
->TIF_flags
&= ~TIF_INCLEANUP
;
285 /* Initialize the CLIENTINFO */
286 pci
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
287 RtlZeroMemory(pci
, sizeof(CLIENTINFO
));
288 pci
->ppi
= ptiCurrent
->ppi
;
289 pci
->fsHooks
= ptiCurrent
->fsHooks
;
290 pci
->dwTIFlags
= ptiCurrent
->TIF_flags
;
291 if (ptiCurrent
->KeyboardLayout
)
293 pci
->hKL
= ptiCurrent
->KeyboardLayout
->hkl
;
294 pci
->CodePage
= ptiCurrent
->KeyboardLayout
->CodePage
;
297 /* Assign a default window station and desktop to the process */
298 /* Do not try to open a desktop or window station before winlogon initializes */
299 if(ptiCurrent
->ppi
->hdeskStartup
== NULL
&& LogonProcess
!= NULL
)
301 HWINSTA hWinSta
= NULL
;
303 UNICODE_STRING DesktopPath
;
305 PRTL_USER_PROCESS_PARAMETERS ProcessParams
;
308 * inherit the thread desktop and process window station (if not yet inherited) from the process startup
309 * info structure. See documentation of CreateProcess()
311 ProcessParams
= pTeb
->ProcessEnvironmentBlock
->ProcessParameters
;
313 Status
= STATUS_UNSUCCESSFUL
;
314 if(ProcessParams
&& ProcessParams
->DesktopInfo
.Length
> 0)
316 Status
= IntSafeCopyUnicodeStringTerminateNULL(&DesktopPath
, &ProcessParams
->DesktopInfo
);
318 if(!NT_SUCCESS(Status
))
320 RtlInitUnicodeString(&DesktopPath
, NULL
);
323 Status
= IntParseDesktopPath(Process
,
328 if (DesktopPath
.Buffer
)
329 ExFreePoolWithTag(DesktopPath
.Buffer
, TAG_STRING
);
331 if(!NT_SUCCESS(Status
))
333 ERR_CH(UserThread
, "Failed to assign default dekstop and winsta to process\n");
337 if(!UserSetProcessWindowStation(hWinSta
))
339 Status
= STATUS_UNSUCCESSFUL
;
340 ERR_CH(UserThread
,"Failed to set initial process winsta\n");
344 /* Validate the new desktop. */
345 Status
= IntValidateDesktopHandle(hDesk
, UserMode
, 0, &pdesk
);
346 if(!NT_SUCCESS(Status
))
348 ERR_CH(UserThread
,"Failed to validate initial desktop handle\n");
352 /* Store the parsed desktop as the initial desktop */
353 ptiCurrent
->ppi
->hdeskStartup
= hDesk
;
354 ptiCurrent
->ppi
->rpdeskStartup
= pdesk
;
357 if (ptiCurrent
->ppi
->hdeskStartup
!= NULL
)
359 if (!IntSetThreadDesktop(ptiCurrent
->ppi
->hdeskStartup
, FALSE
))
361 ERR_CH(UserThread
,"Failed to set thread desktop\n");
362 Status
= STATUS_UNSUCCESSFUL
;
367 /* mark the thread as fully initialized */
368 ptiCurrent
->TIF_flags
|= TIF_GUITHREADINITIALIZED
;
369 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
371 return STATUS_SUCCESS
;
374 ERR_CH(UserThread
,"UserCreateThreadInfo failed! Freeing pti 0x%p for TID %p\n", ptiCurrent
, Thread
->Cid
.UniqueThread
);
375 UserDestroyThreadInfo(Thread
);
381 UserDestroyThreadInfo(struct _ETHREAD
*Thread
)
384 PSINGLE_LIST_ENTRY psle
;
385 PPROCESSINFO ppiCurrent
;
386 struct _EPROCESS
*Process
;
387 PTHREADINFO ptiCurrent
;
389 Process
= Thread
->ThreadsProcess
;
391 /* Get the Win32 Thread */
392 ptiCurrent
= PsGetThreadWin32Thread(Thread
);
396 TRACE_CH(UserThread
,"Destroying pti 0x%p eThread 0x%p\n", ptiCurrent
, Thread
);
398 ptiCurrent
->TIF_flags
|= TIF_INCLEANUP
;
399 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
401 ppiCurrent
= ptiCurrent
->ppi
;
404 /* Decrement thread count and check if its 0 */
405 ppiCurrent
->cThreads
--;
407 if(ptiCurrent
->TIF_flags
& TIF_GUITHREADINITIALIZED
)
409 /* Do now some process cleanup that requires a valid win32 thread */
410 if(ptiCurrent
->ppi
->cThreads
== 0)
412 /* Check if we have registered the user api hook */
413 if(ptiCurrent
->ppi
== ppiUahServer
)
415 /* Unregister the api hook */
416 UserUnregisterUserApiHook();
419 /* Notify logon application to restart shell if needed */
420 if(ptiCurrent
->pDeskInfo
)
422 if(ptiCurrent
->pDeskInfo
->ppiShellProcess
== ppiCurrent
)
424 DWORD ExitCode
= PsGetProcessExitStatus(Process
);
426 TRACE_CH(UserProcess
, "Shell process is exiting (%lu)\n", ExitCode
);
428 UserPostMessage(hwndSAS
,
433 ptiCurrent
->pDeskInfo
->ppiShellProcess
= NULL
;
438 DceFreeThreadDCE(ptiCurrent
);
439 HOOK_DestroyThreadHooks(Thread
);
440 EVENT_DestroyThreadEvents(Thread
);
441 DestroyTimersForThread(ptiCurrent
);
442 KeSetEvent(ptiCurrent
->MessageQueue
->NewMessages
, IO_NO_INCREMENT
, FALSE
);
443 UnregisterThreadHotKeys(Thread
);
445 if (IsListEmpty(&ptiCurrent->WindowListHead))
447 ERR_CH(UserThread,"Thread Window List is Empty!\n");
450 co_DestroyThreadWindows(Thread
);
452 if (ppiCurrent
&& ppiCurrent
->ptiList
== ptiCurrent
&& !ptiCurrent
->ptiSibling
)
454 //ERR_CH(UserThread,"DestroyProcessClasses\n");
455 /* no process windows should exist at this point, or the function will assert! */
456 DestroyProcessClasses(ppiCurrent
);
457 ppiCurrent
->W32PF_flags
&= ~W32PF_CLASSESREGISTERED
;
460 IntBlockInput(ptiCurrent
, FALSE
);
461 IntCleanupThreadCallbacks(ptiCurrent
);
463 /* cleanup user object references stack */
464 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
467 PUSER_REFERENCE_ENTRY ref
= CONTAINING_RECORD(psle
, USER_REFERENCE_ENTRY
, Entry
);
468 TRACE_CH(UserThread
,"thread clean: remove reference obj 0x%p\n",ref
->obj
);
469 UserDereferenceObject(ref
->obj
);
471 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
476 if (IsThreadAttach(ptiCurrent
))
478 PTHREADINFO ptiFrom
= IsThreadAttach(ptiCurrent
);
479 TRACE_CH(UserThread
,"Attached Thread ptiTo is getting switched!\n");
480 UserAttachThreadInput(ptiFrom
, ptiCurrent
, FALSE
);
484 if (ptiCurrent
->pqAttach
&& ptiCurrent
->MessageQueue
)
487 ptiTo
= PsGetThreadWin32Thread(ptiCurrent
->MessageQueue
->Thread
);
488 TRACE_CH(UserThread
,"Attached Thread ptiFrom is getting switched!\n");
489 UserAttachThreadInput( ptiCurrent
, ptiTo
, FALSE
);
492 /* Free the message queue */
493 if(ptiCurrent
->MessageQueue
)
495 MsqDestroyMessageQueue(ptiCurrent
->MessageQueue
);
498 /* Find the THREADINFO in the PROCESSINFO's list */
499 ppti
= &ppiCurrent
->ptiList
;
500 while (*ppti
!= NULL
&& *ppti
!= ptiCurrent
)
502 ppti
= &((*ppti
)->ptiSibling
);
505 /* we must have found it */
506 ASSERT(*ppti
== ptiCurrent
);
508 /* Remove it from the list */
509 *ppti
= ptiCurrent
->ptiSibling
;
511 if (ptiCurrent
->KeyboardLayout
)
512 UserDereferenceObject(ptiCurrent
->KeyboardLayout
);
514 IntSetThreadDesktop(NULL
, TRUE
);
516 TRACE_CH(UserThread
,"Freeing pti 0x%p\n", ptiCurrent
);
518 /* Free the THREADINFO */
519 PsSetThreadWin32Thread(Thread
, NULL
);
520 ExFreePoolWithTag(ptiCurrent
, USERTAG_THREADINFO
);
522 return STATUS_SUCCESS
;
527 Win32kThreadCallback(struct _ETHREAD
*Thread
,
528 PSW32THREADCALLOUTTYPE Type
)
532 UserEnterExclusive();
534 ASSERT(NtCurrentTeb());
536 if (Type
== PsW32ThreadCalloutInitialize
)
538 ASSERT(PsGetThreadWin32Thread(Thread
) == NULL
);
539 Status
= UserCreateThreadInfo(Thread
);
543 ASSERT(PsGetThreadWin32Thread(Thread
) != NULL
);
544 Status
= UserDestroyThreadInfo(Thread
);
553 C_ASSERT(sizeof(SERVERINFO
) <= PAGE_SIZE
);
559 if (!NT_SUCCESS(Status)) \
561 DPRINT1("Failed '%s' (0x%lx)\n", #x, Status); \
566 * This definition doesn't work
572 IN PDRIVER_OBJECT DriverObject
,
573 IN PUNICODE_STRING RegistryPath
)
577 WIN32_CALLOUTS_FPNS CalloutData
= {0};
578 PVOID GlobalUserHeapBase
= NULL
;
581 * Register user mode call interface
582 * (system service table index = 1)
584 Result
= KeAddSystemServiceTable(Win32kSSDT
,
586 Win32kNumberOfSysCalls
,
591 DPRINT1("Adding system services failed!\n");
592 return STATUS_UNSUCCESSFUL
;
595 hModuleWin
= MmPageEntireDriver(DriverEntry
);
596 DPRINT("Win32k hInstance 0x%p!\n",hModuleWin
);
598 /* Register Object Manager Callbacks */
599 CalloutData
.WindowStationParseProcedure
= IntWinStaObjectParse
;
600 CalloutData
.WindowStationDeleteProcedure
= IntWinStaObjectDelete
;
601 CalloutData
.DesktopDeleteProcedure
= IntDesktopObjectDelete
;
602 CalloutData
.ProcessCallout
= Win32kProcessCallback
;
603 CalloutData
.ThreadCallout
= Win32kThreadCallback
;
604 CalloutData
.BatchFlushRoutine
= NtGdiFlushUserBatch
;
605 CalloutData
.DesktopOkToCloseProcedure
= IntDesktopOkToClose
;
606 CalloutData
.WindowStationOkToCloseProcedure
= IntWinstaOkToClose
;
608 /* Register our per-process and per-thread structures. */
609 PsEstablishWin32Callouts((PWIN32_CALLOUTS_FPNS
)&CalloutData
);
611 /* Register service hook callbacks */
613 KdSystemDebugControl('CsoR', DbgPreServiceHook
, ID_Win32PreServiceHook
, 0, 0, 0, 0);
614 KdSystemDebugControl('CsoR', DbgPostServiceHook
, ID_Win32PostServiceHook
, 0, 0, 0, 0);
617 /* Create the global USER heap */
618 GlobalUserHeap
= UserCreateHeap(&GlobalUserHeapSection
,
620 1 * 1024 * 1024); /* FIXME: 1 MB for now... */
621 if (GlobalUserHeap
== NULL
)
623 DPRINT1("Failed to initialize the global heap!\n");
624 return STATUS_UNSUCCESSFUL
;
627 /* Allocate global server info structure */
628 gpsi
= UserHeapAlloc(sizeof(SERVERINFO
));
631 DPRINT1("Failed allocate server info structure!\n");
632 return STATUS_UNSUCCESSFUL
;
635 RtlZeroMemory(gpsi
, sizeof(SERVERINFO
));
636 DPRINT("Global Server Data -> %p\n", gpsi
);
638 NT_ROF(InitGdiHandleTable());
639 NT_ROF(InitPaletteImpl());
641 /* Create stock objects, ie. precreated objects commonly
642 used by win32 applications */
643 CreateStockObjects();
644 CreateSysColorObjects();
646 NT_ROF(InitBrushImpl());
647 NT_ROF(InitPDEVImpl());
648 NT_ROF(InitLDEVImpl());
649 NT_ROF(InitDeviceImpl());
650 NT_ROF(InitDcImpl());
651 NT_ROF(InitUserImpl());
652 NT_ROF(InitWindowStationImpl());
653 NT_ROF(InitDesktopImpl());
654 NT_ROF(InitInputImpl());
655 NT_ROF(InitKeyboardImpl());
656 NT_ROF(MsqInitializeImpl());
657 NT_ROF(InitTimerImpl());
658 NT_ROF(InitDCEImpl());
660 /* Initialize FreeType library */
661 if (!InitFontSupport())
663 DPRINT1("Unable to initialize font support\n");
667 gusLanguageID
= UserGetLanguageID();
669 return STATUS_SUCCESS
;