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:%d\n", 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%x for PID:%d\n", ppiCurrent
, 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 EngCreateEvent((PEVENT
*)&ppiCurrent
->InputIdleEvent
);
129 KeInitializeEvent(ppiCurrent
->InputIdleEvent
, NotificationEvent
, FALSE
);
132 /* map the gdi handle table to user land */
133 Process
->Peb
->GdiSharedHandleTable
= GDI_MapHandleTable(Process
);
134 Process
->Peb
->GdiDCAttributeList
= GDI_BATCH_LIMIT
;
135 pParams
= Process
->Peb
->ProcessParameters
;
137 ppiCurrent
->peProcess
= Process
;
138 /* setup process flags */
139 ppiCurrent
->W32PF_flags
= W32PF_THREADCONNECTED
;
142 pParams
->WindowFlags
& STARTF_SCRNSAVER
)
144 ppiScrnSaver
= ppiCurrent
;
145 ppiCurrent
->W32PF_flags
|= W32PF_SCREENSAVER
;
148 /* Create pools for GDI object attributes */
149 ppiCurrent
->pPoolDcAttr
= GdiPoolCreate(sizeof(DC_ATTR
), 'acdG');
150 ppiCurrent
->pPoolBrushAttr
= GdiPoolCreate(sizeof(BRUSH_ATTR
), 'arbG');
151 ppiCurrent
->pPoolRgnAttr
= GdiPoolCreate(sizeof(RGN_ATTR
), 'agrG');
152 ASSERT(ppiCurrent
->pPoolDcAttr
);
153 ASSERT(ppiCurrent
->pPoolBrushAttr
);
154 ASSERT(ppiCurrent
->pPoolRgnAttr
);
158 /* Get the Win32 Process */
159 ppiCurrent
= PsGetProcessWin32Process(Process
);
163 TRACE_CH(UserProcess
, "Destroying ppi 0x%x\n", ppiCurrent
);
164 ppiCurrent
->W32PF_flags
|= W32PF_TERMINATED
;
166 if (ppiScrnSaver
== ppiCurrent
)
169 if (ppiCurrent
->InputIdleEvent
)
171 EngFreeMem(ppiCurrent
->InputIdleEvent
);
172 ppiCurrent
->InputIdleEvent
= NULL
;
175 IntCleanupMenus(Process
, ppiCurrent
);
176 IntCleanupCurIcons(Process
, ppiCurrent
);
179 GDI_CleanupForProcess(Process
);
181 co_IntGraphicsCheck(FALSE
);
184 * Deregister logon application automatically
186 if(LogonProcess
== ppiCurrent
)
191 /* Close the startup desktop */
192 if(ppiCurrent
->rpdeskStartup
)
193 ObDereferenceObject(ppiCurrent
->rpdeskStartup
);
194 if(ppiCurrent
->hdeskStartup
)
195 ZwClose(ppiCurrent
->hdeskStartup
);
197 /* Close the current window station */
198 UserSetProcessWindowStation(NULL
);
200 /* Destroy GDI pools */
201 GdiPoolDestroy(ppiCurrent
->pPoolDcAttr
);
202 GdiPoolDestroy(ppiCurrent
->pPoolBrushAttr
);
203 GdiPoolDestroy(ppiCurrent
->pPoolRgnAttr
);
205 if (gppiInputProvider
== ppiCurrent
) gppiInputProvider
= NULL
;
207 TRACE_CH(UserProcess
,"Freeing ppi 0x%x\n", ppiCurrent
);
209 /* Ftee the PROCESSINFO */
210 PsSetProcessWin32Process(Process
, NULL
);
211 ExFreePoolWithTag(ppiCurrent
, USERTAG_PROCESSINFO
);
214 RETURN( STATUS_SUCCESS
);
222 UserCreateThreadInfo(struct _ETHREAD
*Thread
)
224 struct _EPROCESS
*Process
;
226 PTHREADINFO ptiCurrent
;
228 NTSTATUS Status
= STATUS_SUCCESS
;
231 Process
= Thread
->ThreadsProcess
;
233 pTeb
= NtCurrentTeb();
237 ptiCurrent
= ExAllocatePoolWithTag(NonPagedPool
,
240 if (ptiCurrent
== NULL
)
242 ERR_CH(UserThread
, "Failed to allocate pti for TID %d\n", Thread
->Cid
.UniqueThread
);
243 return STATUS_NO_MEMORY
;
246 RtlZeroMemory(ptiCurrent
, sizeof(THREADINFO
));
248 PsSetThreadWin32Thread(Thread
, ptiCurrent
);
249 pTeb
->Win32ThreadInfo
= ptiCurrent
;
250 ptiCurrent
->pClientInfo
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
252 TRACE_CH(UserThread
, "Allocated pti 0x%x for TID %d\n", ptiCurrent
, Thread
->Cid
.UniqueThread
);
254 /* Initialize the THREADINFO */
255 InitializeListHead(&ptiCurrent
->WindowListHead
);
256 InitializeListHead(&ptiCurrent
->W32CallbackListHead
);
257 InitializeListHead(&ptiCurrent
->PtiLink
);
258 for (i
= 0; i
< NB_HOOKS
; i
++)
260 InitializeListHead(&ptiCurrent
->aphkStart
[i
]);
262 ptiCurrent
->pEThread
= Thread
;
263 ptiCurrent
->ppi
= PsGetCurrentProcessWin32Process();
264 ptiCurrent
->ptiSibling
= ptiCurrent
->ppi
->ptiList
;
265 ptiCurrent
->ppi
->ptiList
= ptiCurrent
;
266 ptiCurrent
->ppi
->cThreads
++;
267 ptiCurrent
->MessageQueue
= MsqCreateMessageQueue(Thread
);
268 if(ptiCurrent
->MessageQueue
== NULL
)
270 ERR_CH(UserThread
,"Failed to allocate message loop\n");
271 Status
= STATUS_NO_MEMORY
;
274 ptiCurrent
->KeyboardLayout
= W32kGetDefaultKeyLayout();
275 if (ptiCurrent
->KeyboardLayout
)
276 UserReferenceObject(ptiCurrent
->KeyboardLayout
);
277 ptiCurrent
->TIF_flags
&= ~TIF_INCLEANUP
;
279 /* Initialize the CLIENTINFO */
280 pci
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
281 RtlZeroMemory(pci
, sizeof(CLIENTINFO
));
282 pci
->ppi
= ptiCurrent
->ppi
;
283 pci
->fsHooks
= ptiCurrent
->fsHooks
;
284 pci
->dwTIFlags
= ptiCurrent
->TIF_flags
;
285 if (ptiCurrent
->KeyboardLayout
)
286 pci
->hKL
= ptiCurrent
->KeyboardLayout
->hkl
;
288 /* Assign a default window station and desktop to the process */
289 /* Do not try to open a desktop or window station before winlogon initializes */
290 if(ptiCurrent
->ppi
->hdeskStartup
== NULL
&& LogonProcess
!= NULL
)
292 HWINSTA hWinSta
= NULL
;
294 UNICODE_STRING DesktopPath
;
296 PRTL_USER_PROCESS_PARAMETERS ProcessParams
;
299 * inherit the thread desktop and process window station (if not yet inherited) from the process startup
300 * info structure. See documentation of CreateProcess()
302 ProcessParams
= pTeb
->ProcessEnvironmentBlock
->ProcessParameters
;
304 Status
= STATUS_UNSUCCESSFUL
;
305 if(ProcessParams
&& ProcessParams
->DesktopInfo
.Length
> 0)
307 Status
= IntSafeCopyUnicodeStringTerminateNULL(&DesktopPath
, &ProcessParams
->DesktopInfo
);
309 if(!NT_SUCCESS(Status
))
311 RtlInitUnicodeString(&DesktopPath
, NULL
);
314 Status
= IntParseDesktopPath(Process
,
319 if (DesktopPath
.Buffer
)
320 ExFreePoolWithTag(DesktopPath
.Buffer
, TAG_STRING
);
322 if(!NT_SUCCESS(Status
))
324 ERR_CH(UserThread
, "Failed to assign default dekstop and winsta to process\n");
328 if(!UserSetProcessWindowStation(hWinSta
))
330 Status
= STATUS_UNSUCCESSFUL
;
331 ERR_CH(UserThread
,"Failed to set initial process winsta\n");
335 /* Validate the new desktop. */
336 Status
= IntValidateDesktopHandle(hDesk
, UserMode
, 0, &pdesk
);
337 if(!NT_SUCCESS(Status
))
339 ERR_CH(UserThread
,"Failed to validate initial desktop handle\n");
343 /* Store the parsed desktop as the initial desktop */
344 ptiCurrent
->ppi
->hdeskStartup
= hDesk
;
345 ptiCurrent
->ppi
->rpdeskStartup
= pdesk
;
348 if (ptiCurrent
->ppi
->hdeskStartup
!= NULL
)
350 if (!IntSetThreadDesktop(ptiCurrent
->ppi
->hdeskStartup
, FALSE
))
352 ERR_CH(UserThread
,"Failed to set thread desktop\n");
353 Status
= STATUS_UNSUCCESSFUL
;
358 /* mark the thread as fully initialized */
359 ptiCurrent
->TIF_flags
|= TIF_GUITHREADINITIALIZED
;
360 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
362 return STATUS_SUCCESS
;
365 ERR_CH(UserThread
,"UserCreateThreadInfo failed! Freeing pti 0x%x for TID %d\n", ptiCurrent
, Thread
->Cid
.UniqueThread
);
366 UserDestroyThreadInfo(Thread
);
372 UserDestroyThreadInfo(struct _ETHREAD
*Thread
)
375 PSINGLE_LIST_ENTRY psle
;
376 PPROCESSINFO ppiCurrent
;
377 struct _EPROCESS
*Process
;
378 PTHREADINFO ptiCurrent
;
380 Process
= Thread
->ThreadsProcess
;
382 /* Get the Win32 Thread */
383 ptiCurrent
= PsGetThreadWin32Thread(Thread
);
387 TRACE_CH(UserThread
,"Destroying pti 0x%x\n", ptiCurrent
);
389 ppiCurrent
= ptiCurrent
->ppi
;
390 ptiCurrent
->TIF_flags
|= TIF_INCLEANUP
;
391 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
394 /* Decrement thread count and check if its 0 */
395 ppiCurrent
->cThreads
--;
397 if(ptiCurrent
->TIF_flags
& TIF_GUITHREADINITIALIZED
)
399 /* Do now some process cleanup that requires a valid win32 thread */
400 if(ptiCurrent
->ppi
->cThreads
== 0)
402 /* Check if we have registered the user api hook */
403 if(ptiCurrent
->ppi
== ppiUahServer
)
405 /* Unregister the api hook */
406 UserUnregisterUserApiHook();
409 /* Notify logon application to restart shell if needed */
410 if(ptiCurrent
->pDeskInfo
)
412 if(ptiCurrent
->pDeskInfo
->ppiShellProcess
== ppiCurrent
)
414 DWORD ExitCode
= PsGetProcessExitStatus(Process
);
416 TRACE_CH(UserProcess
, "Shell process is exiting (%d)\n", ExitCode
);
418 UserPostMessage(hwndSAS
,
423 ptiCurrent
->pDeskInfo
->ppiShellProcess
= NULL
;
428 DceFreeThreadDCE(ptiCurrent
);
429 HOOK_DestroyThreadHooks(Thread
);
430 EVENT_DestroyThreadEvents(Thread
);
431 DestroyTimersForThread(ptiCurrent
);
432 KeSetEvent(ptiCurrent
->MessageQueue
->NewMessages
, IO_NO_INCREMENT
, FALSE
);
433 UnregisterThreadHotKeys(Thread
);
435 if (IsListEmpty(&ptiCurrent->WindowListHead))
437 ERR_CH(UserThread,"Thread Window List is Empty!\n");
440 co_DestroyThreadWindows(Thread
);
442 if (ppiCurrent
&& ppiCurrent
->ptiList
== ptiCurrent
&& !ptiCurrent
->ptiSibling
)
444 //ERR_CH(UserThread,"DestroyProcessClasses\n");
445 /* no process windows should exist at this point, or the function will assert! */
446 DestroyProcessClasses(ppiCurrent
);
447 ppiCurrent
->W32PF_flags
&= ~W32PF_CLASSESREGISTERED
;
450 IntBlockInput(ptiCurrent
, FALSE
);
451 IntCleanupThreadCallbacks(ptiCurrent
);
453 /* cleanup user object references stack */
454 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
457 PUSER_REFERENCE_ENTRY ref
= CONTAINING_RECORD(psle
, USER_REFERENCE_ENTRY
, Entry
);
458 TRACE_CH(UserThread
,"thread clean: remove reference obj 0x%x\n",ref
->obj
);
459 UserDereferenceObject(ref
->obj
);
461 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
465 /* Free the message queue */
466 if(ptiCurrent
->MessageQueue
)
467 MsqDestroyMessageQueue(ptiCurrent
->MessageQueue
);
469 /* Find the THREADINFO in the PROCESSINFO's list */
470 ppti
= &ppiCurrent
->ptiList
;
471 while (*ppti
!= NULL
&& *ppti
!= ptiCurrent
)
473 ppti
= &((*ppti
)->ptiSibling
);
476 /* we must have found it */
477 ASSERT(*ppti
== ptiCurrent
);
479 /* Remove it from the list */
480 *ppti
= ptiCurrent
->ptiSibling
;
482 if (ptiCurrent
->KeyboardLayout
)
483 UserDereferenceObject(ptiCurrent
->KeyboardLayout
);
485 IntSetThreadDesktop(NULL
, TRUE
);
487 TRACE_CH(UserThread
,"Freeing pti 0x%x\n", ptiCurrent
);
489 /* Free the THREADINFO */
490 PsSetThreadWin32Thread(Thread
, NULL
);
491 ExFreePoolWithTag(ptiCurrent
, USERTAG_THREADINFO
);
493 return STATUS_SUCCESS
;
498 Win32kThreadCallback(struct _ETHREAD
*Thread
,
499 PSW32THREADCALLOUTTYPE Type
)
503 UserEnterExclusive();
505 ASSERT(NtCurrentTeb());
507 if (Type
== PsW32ThreadCalloutInitialize
)
509 ASSERT(PsGetThreadWin32Thread(Thread
) == NULL
);
510 Status
= UserCreateThreadInfo(Thread
);
514 ASSERT(PsGetThreadWin32Thread(Thread
) != NULL
);
515 Status
= UserDestroyThreadInfo(Thread
);
524 C_ASSERT(sizeof(SERVERINFO
) <= PAGE_SIZE
);
530 if (!NT_SUCCESS(Status)) \
532 DPRINT1("Failed '%s' (0x%lx)\n", #x, Status); \
537 * This definition doesn't work
543 IN PDRIVER_OBJECT DriverObject
,
544 IN PUNICODE_STRING RegistryPath
)
548 WIN32_CALLOUTS_FPNS CalloutData
= {0};
549 PVOID GlobalUserHeapBase
= NULL
;
552 * Register user mode call interface
553 * (system service table index = 1)
555 Result
= KeAddSystemServiceTable(Win32kSSDT
,
557 Win32kNumberOfSysCalls
,
562 DPRINT1("Adding system services failed!\n");
563 return STATUS_UNSUCCESSFUL
;
566 hModuleWin
= MmPageEntireDriver(DriverEntry
);
567 DPRINT("Win32k hInstance 0x%x!\n",hModuleWin
);
569 /* Register Object Manager Callbacks */
570 CalloutData
.WindowStationParseProcedure
= IntWinStaObjectParse
;
571 CalloutData
.WindowStationDeleteProcedure
= IntWinStaObjectDelete
;
572 CalloutData
.DesktopDeleteProcedure
= IntDesktopObjectDelete
;
573 CalloutData
.ProcessCallout
= Win32kProcessCallback
;
574 CalloutData
.ThreadCallout
= Win32kThreadCallback
;
575 CalloutData
.BatchFlushRoutine
= NtGdiFlushUserBatch
;
576 CalloutData
.DesktopOkToCloseProcedure
= IntDesktopOkToClose
;
577 CalloutData
.WindowStationOkToCloseProcedure
= IntWinstaOkToClose
;
579 /* Register our per-process and per-thread structures. */
580 PsEstablishWin32Callouts((PWIN32_CALLOUTS_FPNS
)&CalloutData
);
582 /* Register service hook callbacks */
584 KdSystemDebugControl('CsoR', DbgPreServiceHook
, ID_Win32PreServiceHook
, 0, 0, 0, 0);
585 KdSystemDebugControl('CsoR', DbgPostServiceHook
, ID_Win32PostServiceHook
, 0, 0, 0, 0);
588 /* Create the global USER heap */
589 GlobalUserHeap
= UserCreateHeap(&GlobalUserHeapSection
,
591 1 * 1024 * 1024); /* FIXME: 1 MB for now... */
592 if (GlobalUserHeap
== NULL
)
594 DPRINT1("Failed to initialize the global heap!\n");
595 return STATUS_UNSUCCESSFUL
;
598 /* Allocate global server info structure */
599 gpsi
= UserHeapAlloc(sizeof(SERVERINFO
));
602 DPRINT1("Failed allocate server info structure!\n");
603 return STATUS_UNSUCCESSFUL
;
606 RtlZeroMemory(gpsi
, sizeof(SERVERINFO
));
607 DPRINT("Global Server Data -> %x\n", gpsi
);
609 NT_ROF(InitGdiHandleTable());
610 NT_ROF(InitPaletteImpl());
612 /* Create stock objects, ie. precreated objects commonly
613 used by win32 applications */
614 CreateStockObjects();
615 CreateSysColorObjects();
617 NT_ROF(InitPDEVImpl());
618 NT_ROF(InitLDEVImpl());
619 NT_ROF(InitDeviceImpl());
620 NT_ROF(InitDcImpl());
621 NT_ROF(InitUserImpl());
622 NT_ROF(InitWindowStationImpl());
623 NT_ROF(InitDesktopImpl());
624 NT_ROF(InitInputImpl());
625 NT_ROF(InitKeyboardImpl());
626 NT_ROF(MsqInitializeImpl());
627 NT_ROF(InitTimerImpl());
628 NT_ROF(InitDCEImpl());
630 /* Initialize FreeType library */
631 if (!InitFontSupport())
633 DPRINT1("Unable to initialize font support\n");
637 gusLanguageID
= UserGetLanguageID();
639 return STATUS_SUCCESS
;