2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Driver entry and initialization of win32k
5 * FILE: win32ss/user/ntuser/main.c
18 NTSTATUS
ExitProcessCallback(PEPROCESS Process
);
19 NTSTATUS NTAPI
ExitThreadCallback(PETHREAD Thread
);
21 // TODO: Should be moved to some GDI header
22 NTSTATUS
GdiProcessCreate(PEPROCESS Process
);
23 NTSTATUS
GdiProcessDestroy(PEPROCESS Process
);
24 NTSTATUS
GdiThreadCreate(PETHREAD Thread
);
25 NTSTATUS
GdiThreadDestroy(PETHREAD Thread
);
27 PSERVERINFO gpsi
= NULL
; // Global User Server Information.
30 PPROCESSINFO ppiScrnSaver
;
31 PPROCESSINFO gppiList
= NULL
;
33 extern ULONG_PTR Win32kSSDT
[];
34 extern UCHAR Win32kSSPT
[];
35 extern ULONG Win32kNumberOfSysCalls
;
40 DbgPreServiceHook(ULONG ulSyscallId
, PULONG_PTR pulArguments
)
42 GdiDbgPreServiceHook(ulSyscallId
, pulArguments
);
43 UserDbgPreServiceHook(ulSyscallId
, pulArguments
);
48 DbgPostServiceHook(ULONG ulSyscallId
, ULONG_PTR ulResult
)
50 ulResult
= GdiDbgPostServiceHook(ulSyscallId
, ulResult
);
51 ulResult
= UserDbgPostServiceHook(ulSyscallId
, ulResult
);
58 AllocW32Process(IN PEPROCESS Process
,
59 OUT PPROCESSINFO
* W32Process
)
61 PPROCESSINFO ppiCurrent
;
63 TRACE_CH(UserProcess
, "In AllocW32Process(0x%p)\n", Process
);
65 /* Check that we were not called with an already existing Win32 process info */
66 ppiCurrent
= PsGetProcessWin32Process(Process
);
67 if (ppiCurrent
) return STATUS_SUCCESS
;
69 /* Allocate a new Win32 process info */
70 ppiCurrent
= ExAllocatePoolWithTag(NonPagedPool
,
73 if (ppiCurrent
== NULL
)
75 ERR_CH(UserProcess
, "Failed to allocate ppi for PID:0x%lx\n",
76 HandleToUlong(Process
->UniqueProcessId
));
77 return STATUS_NO_MEMORY
;
80 TRACE_CH(UserProcess
, "Allocated ppi 0x%p for PID:0x%lx\n",
81 ppiCurrent
, HandleToUlong(Process
->UniqueProcessId
));
83 RtlZeroMemory(ppiCurrent
, sizeof(*ppiCurrent
));
85 PsSetProcessWin32Process(Process
, ppiCurrent
, NULL
);
86 IntReferenceProcessInfo(ppiCurrent
);
88 *W32Process
= ppiCurrent
;
89 return STATUS_SUCCESS
;
92 #define FreeW32Process(/*Process*/ W32Process) \
94 /*PPROCESSINFO W32Process = PsGetProcessWin32Process(Process);*/ \
95 /*ASSERT(W32Process);*/ \
96 IntDereferenceProcessInfo(W32Process); \
100 * Called from IntDereferenceProcessInfo
103 UserDeleteW32Process(
104 _Pre_notnull_
__drv_freesMem(Mem
) PPROCESSINFO ppiCurrent
)
106 if (ppiCurrent
->InputIdleEvent
)
108 /* Free the allocated memory */
109 ExFreePoolWithTag(ppiCurrent
->InputIdleEvent
, USERTAG_EVENT
);
112 /* Close the startup desktop */
113 if (ppiCurrent
->rpdeskStartup
)
114 ObDereferenceObject(ppiCurrent
->rpdeskStartup
);
117 if (DBG_IS_CHANNEL_ENABLED(ppiCurrent
, DbgChUserObj
, WARN_LEVEL
))
119 TRACE_PPI(ppiCurrent
, UserObj
, "Dumping user handles now that process info %p is gets freed.\n", ppiCurrent
);
120 DbgUserDumpHandleTable();
124 /* Free the PROCESSINFO */
125 ExFreePoolWithTag(ppiCurrent
, USERTAG_PROCESSINFO
);
129 UserProcessCreate(PEPROCESS Process
)
131 PPROCESSINFO ppiCurrent
= PsGetProcessWin32Process(Process
);
134 InitializeListHead(&ppiCurrent
->DriverObjListHead
);
135 ExInitializeFastMutex(&ppiCurrent
->DriverObjListLock
);
137 ppiCurrent
->KeyboardLayout
= W32kGetDefaultKeyLayout();
141 /* Allocate memory for the event structure */
142 Event
= ExAllocatePoolWithTag(NonPagedPool
,
147 /* Initialize the kernel event */
148 KeInitializeEvent(Event
,
149 SynchronizationEvent
,
155 DPRINT("CreateEvent() failed\n");
160 ppiCurrent
->InputIdleEvent
= Event
;
161 KeInitializeEvent(ppiCurrent
->InputIdleEvent
, NotificationEvent
, FALSE
);
164 ppiCurrent
->peProcess
= Process
;
165 ppiCurrent
->W32Pid
= HandleToUlong(PsGetProcessId(Process
));
167 /* Setup process flags */
168 ppiCurrent
->W32PF_flags
|= W32PF_PROCESSCONNECTED
;
169 if ( Process
->Peb
->ProcessParameters
&&
170 Process
->Peb
->ProcessParameters
->WindowFlags
& STARTF_SCRNSAVER
)
172 ppiScrnSaver
= ppiCurrent
;
173 ppiCurrent
->W32PF_flags
|= W32PF_SCREENSAVER
;
176 // FIXME: check if this process is allowed.
177 ppiCurrent
->W32PF_flags
|= W32PF_ALLOWFOREGROUNDACTIVATE
; // Starting application will get it toggled off.
179 return STATUS_SUCCESS
;
183 UserProcessDestroy(PEPROCESS Process
)
185 PPROCESSINFO ppiCurrent
= PsGetProcessWin32Process(Process
);
188 if (ppiScrnSaver
== ppiCurrent
)
191 /* Destroy user objects */
192 UserDestroyObjectsForOwner(gHandleTable
, ppiCurrent
);
194 TRACE_CH(UserProcess
, "Freeing ppi 0x%p\n", ppiCurrent
);
196 if (DBG_IS_CHANNEL_ENABLED(ppiCurrent
, DbgChUserObj
, WARN_LEVEL
))
198 TRACE_CH(UserObj
, "Dumping user handles at the end of the process %s (Info %p).\n",
199 ppiCurrent
->peProcess
->ImageFileName
, ppiCurrent
);
200 DbgUserDumpHandleTable();
204 /* Remove it from the list of GUI apps */
205 co_IntGraphicsCheck(FALSE
);
208 * Deregister logon application automatically
210 if (gpidLogon
== ppiCurrent
->peProcess
->UniqueProcessId
)
213 /* Close the current window station */
214 UserSetProcessWindowStation(NULL
);
216 if (gppiInputProvider
== ppiCurrent
) gppiInputProvider
= NULL
;
218 if (ppiCurrent
->hdeskStartup
)
220 ZwClose(ppiCurrent
->hdeskStartup
);
221 ppiCurrent
->hdeskStartup
= NULL
;
224 /* Clean up the process icon cache */
225 IntCleanupCurIconCache(ppiCurrent
);
227 return STATUS_SUCCESS
;
231 InitProcessCallback(PEPROCESS Process
)
234 PPROCESSINFO ppiCurrent
;
235 PVOID KernelMapping
= NULL
, UserMapping
= NULL
;
237 /* We might be called with an already allocated win32 process */
238 ppiCurrent
= PsGetProcessWin32Process(Process
);
239 if (ppiCurrent
!= NULL
)
241 /* There is no more to do for us (this is a success code!) */
242 return STATUS_ALREADY_WIN32
;
244 // if (ppiCurrent->W32PF_flags & W32PF_PROCESSCONNECTED)
245 // return STATUS_ALREADY_WIN32;
247 /* Allocate a new Win32 process info */
248 Status
= AllocW32Process(Process
, &ppiCurrent
);
249 if (!NT_SUCCESS(Status
))
251 ERR_CH(UserProcess
, "Failed to allocate ppi for PID:0x%lx\n",
252 HandleToUlong(Process
->UniqueProcessId
));
257 DbgInitDebugChannels();
259 KdRosRegisterCliCallback(DbgGdiKdbgCliCallback
);
263 /* Map the global user heap into the process */
264 Status
= MapGlobalUserHeap(Process
, &KernelMapping
, &UserMapping
);
265 if (!NT_SUCCESS(Status
))
267 TRACE_CH(UserProcess
, "Failed to map the global heap! 0x%x\n", Status
);
271 TRACE_CH(UserProcess
, "InitProcessCallback -- We have KernelMapping 0x%p and UserMapping 0x%p with delta = 0x%x\n",
272 KernelMapping
, UserMapping
, (ULONG_PTR
)KernelMapping
- (ULONG_PTR
)UserMapping
);
274 /* Initialize USER process info */
275 Status
= UserProcessCreate(Process
);
276 if (!NT_SUCCESS(Status
))
278 ERR_CH(UserProcess
, "UserProcessCreate failed, Status 0x%08lx\n", Status
);
282 /* Initialize GDI process info */
283 Status
= GdiProcessCreate(Process
);
284 if (!NT_SUCCESS(Status
))
286 ERR_CH(UserProcess
, "GdiProcessCreate failed, Status 0x%08lx\n", Status
);
290 /* Add the process to the global list */
291 ppiCurrent
->ppiNext
= gppiList
;
292 gppiList
= ppiCurrent
;
294 return STATUS_SUCCESS
;
297 ERR_CH(UserProcess
, "InitProcessCallback failed! Freeing ppi 0x%p for PID:0x%lx\n",
298 ppiCurrent
, HandleToUlong(Process
->UniqueProcessId
));
299 ExitProcessCallback(Process
);
304 ExitProcessCallback(PEPROCESS Process
)
306 PPROCESSINFO ppiCurrent
, *pppi
;
308 /* Get the Win32 Process */
309 ppiCurrent
= PsGetProcessWin32Process(Process
);
311 ASSERT(ppiCurrent
->peProcess
== Process
);
313 TRACE_CH(UserProcess
, "Destroying ppi 0x%p\n", ppiCurrent
);
314 ppiCurrent
->W32PF_flags
|= W32PF_TERMINATED
;
316 /* Remove it from the list */
318 while (*pppi
!= NULL
&& *pppi
!= ppiCurrent
)
320 pppi
= &(*pppi
)->ppiNext
;
322 ASSERT(*pppi
== ppiCurrent
);
323 *pppi
= ppiCurrent
->ppiNext
;
325 /* Cleanup GDI info */
326 GdiProcessDestroy(Process
);
328 /* Cleanup USER info */
329 UserProcessDestroy(Process
);
331 /* The process is dying */
332 PsSetProcessWin32Process(Process
, NULL
, ppiCurrent
);
333 ppiCurrent
->peProcess
= NULL
;
335 /* Finally, dereference */
336 FreeW32Process(/*Process*/ ppiCurrent
); // IntDereferenceProcessInfo(ppiCurrent);
338 return STATUS_SUCCESS
;
343 Win32kProcessCallback(PEPROCESS Process
,
348 ASSERT(Process
->Peb
);
350 TRACE_CH(UserProcess
, "Win32kProcessCallback -->\n");
352 UserEnterExclusive();
356 Status
= InitProcessCallback(Process
);
360 Status
= ExitProcessCallback(Process
);
365 TRACE_CH(UserProcess
, "<-- Win32kProcessCallback\n");
373 AllocW32Thread(IN PETHREAD Thread
,
374 OUT PTHREADINFO
* W32Thread
)
376 PTHREADINFO ptiCurrent
;
378 TRACE_CH(UserThread
, "In AllocW32Thread(0x%p)\n", Thread
);
380 /* Check that we were not called with an already existing Win32 thread info */
381 ptiCurrent
= PsGetThreadWin32Thread(Thread
);
382 NT_ASSERT(ptiCurrent
== NULL
);
384 /* Allocate a new Win32 thread info */
385 ptiCurrent
= ExAllocatePoolWithTag(NonPagedPool
,
388 if (ptiCurrent
== NULL
)
390 ERR_CH(UserThread
, "Failed to allocate pti for TID:0x%lx\n",
391 HandleToUlong(Thread
->Cid
.UniqueThread
));
392 return STATUS_NO_MEMORY
;
395 TRACE_CH(UserThread
, "Allocated pti 0x%p for TID:0x%lx\n",
396 ptiCurrent
, HandleToUlong(Thread
->Cid
.UniqueThread
));
398 RtlZeroMemory(ptiCurrent
, sizeof(*ptiCurrent
));
400 PsSetThreadWin32Thread(Thread
, ptiCurrent
, NULL
);
401 IntReferenceThreadInfo(ptiCurrent
);
403 *W32Thread
= ptiCurrent
;
404 return STATUS_SUCCESS
;
407 #define FreeW32Thread(/*Thread*/ W32Thread) \
409 /*PTHREADINFO W32Thread = PsGetThreadWin32Thread(Thread);*/ \
410 /*ASSERT(W32Thread);*/ \
411 IntDereferenceThreadInfo(W32Thread); \
415 * Called from IntDereferenceThreadInfo
418 UserDeleteW32Thread(PTHREADINFO pti
)
420 PPROCESSINFO ppi
= pti
->ppi
;
422 TRACE_CH(UserThread
, "UserDeleteW32Thread pti 0x%p\n",pti
);
424 /* Free the message queue */
425 if (pti
->MessageQueue
)
427 MsqDestroyMessageQueue(pti
);
430 MsqCleanupThreadMsgs(pti
);
432 ExFreePoolWithTag(pti
, USERTAG_THREADINFO
);
434 IntDereferenceProcessInfo(ppi
);
438 UserThreadCreate(PETHREAD Thread
)
440 return STATUS_SUCCESS
;
444 UserThreadDestroy(PETHREAD Thread
)
446 return STATUS_SUCCESS
;
450 InitThreadCallback(PETHREAD Thread
)
454 PTHREADINFO ptiCurrent
;
456 NTSTATUS Status
= STATUS_SUCCESS
;
458 LARGE_INTEGER LargeTickCount
;
459 PRTL_USER_PROCESS_PARAMETERS ProcessParams
;
461 Process
= Thread
->ThreadsProcess
;
463 pTeb
= NtCurrentTeb();
466 ProcessParams
= pTeb
->ProcessEnvironmentBlock
->ProcessParameters
;
468 /* Allocate a new Win32 thread info */
469 Status
= AllocW32Thread(Thread
, &ptiCurrent
);
470 if (!NT_SUCCESS(Status
))
472 ERR_CH(UserThread
, "Failed to allocate pti for TID:0x%lx\n",
473 HandleToUlong(Thread
->Cid
.UniqueThread
));
477 /* Initialize the THREADINFO */
478 ptiCurrent
->pEThread
= Thread
;
479 ptiCurrent
->ppi
= PsGetProcessWin32Process(Process
);
480 IntReferenceProcessInfo(ptiCurrent
->ppi
);
481 pTeb
->Win32ThreadInfo
= ptiCurrent
;
482 ptiCurrent
->pClientInfo
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
484 /* Mark the process as having threads */
485 ptiCurrent
->ppi
->W32PF_flags
|= W32PF_THREADCONNECTED
;
487 InitializeListHead(&ptiCurrent
->WindowListHead
);
488 InitializeListHead(&ptiCurrent
->W32CallbackListHead
);
489 InitializeListHead(&ptiCurrent
->PostedMessagesListHead
);
490 InitializeListHead(&ptiCurrent
->SentMessagesListHead
);
491 InitializeListHead(&ptiCurrent
->DispatchingMessagesHead
);
492 InitializeListHead(&ptiCurrent
->LocalDispatchingMessagesHead
);
493 InitializeListHead(&ptiCurrent
->PtiLink
);
494 for (i
= 0; i
< NB_HOOKS
; i
++)
496 InitializeListHead(&ptiCurrent
->aphkStart
[i
]);
498 ptiCurrent
->ptiSibling
= ptiCurrent
->ppi
->ptiList
;
499 ptiCurrent
->ppi
->ptiList
= ptiCurrent
;
500 ptiCurrent
->ppi
->cThreads
++;
502 ptiCurrent
->hEventQueueClient
= NULL
;
503 Status
= ZwCreateEvent(&ptiCurrent
->hEventQueueClient
, EVENT_ALL_ACCESS
,
504 NULL
, SynchronizationEvent
, FALSE
);
505 if (!NT_SUCCESS(Status
))
507 ERR_CH(UserThread
, "Event creation failed, Status 0x%08x.\n", Status
);
510 Status
= ObReferenceObjectByHandle(ptiCurrent
->hEventQueueClient
, 0,
511 *ExEventObjectType
, UserMode
,
512 (PVOID
*)&ptiCurrent
->pEventQueueServer
, NULL
);
513 if (!NT_SUCCESS(Status
))
515 ERR_CH(UserThread
, "Failed referencing the event object, Status 0x%08x.\n", Status
);
516 ObCloseHandle(ptiCurrent
->hEventQueueClient
, UserMode
);
517 ptiCurrent
->hEventQueueClient
= NULL
;
521 KeQueryTickCount(&LargeTickCount
);
522 ptiCurrent
->timeLast
= LargeTickCount
.u
.LowPart
;
524 ptiCurrent
->MessageQueue
= MsqCreateMessageQueue(ptiCurrent
);
525 if (ptiCurrent
->MessageQueue
== NULL
)
527 ERR_CH(UserThread
, "Failed to allocate message loop\n");
528 Status
= STATUS_NO_MEMORY
;
532 ptiCurrent
->KeyboardLayout
= W32kGetDefaultKeyLayout();
533 if (ptiCurrent
->KeyboardLayout
)
534 UserReferenceObject(ptiCurrent
->KeyboardLayout
);
536 ptiCurrent
->TIF_flags
&= ~TIF_INCLEANUP
;
538 /* CSRSS threads have some special features */
539 if (Process
== gpepCSRSS
)
540 ptiCurrent
->TIF_flags
= TIF_CSRSSTHREAD
| TIF_DONTATTACHQUEUE
;
542 // FIXME: Flag SYSTEM threads with... TIF_SYSTEMTHREAD !!
544 ptiCurrent
->pcti
= &ptiCurrent
->cti
;
546 /* Initialize the CLIENTINFO */
547 pci
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
548 RtlZeroMemory(pci
, sizeof(*pci
));
549 pci
->ppi
= ptiCurrent
->ppi
;
550 pci
->fsHooks
= ptiCurrent
->fsHooks
;
551 pci
->dwTIFlags
= ptiCurrent
->TIF_flags
;
552 if (ptiCurrent
->KeyboardLayout
)
554 pci
->hKL
= ptiCurrent
->KeyboardLayout
->hkl
;
555 pci
->CodePage
= ptiCurrent
->KeyboardLayout
->CodePage
;
558 /* Need to pass the user Startup Information to the current process. */
561 if ( ptiCurrent
->ppi
->usi
.cb
== 0 ) // Not initialized yet.
563 if ( ProcessParams
->WindowFlags
!= 0 ) // Need window flags set.
565 ptiCurrent
->ppi
->usi
.cb
= sizeof(USERSTARTUPINFO
);
566 ptiCurrent
->ppi
->usi
.dwX
= ProcessParams
->StartingX
;
567 ptiCurrent
->ppi
->usi
.dwY
= ProcessParams
->StartingY
;
568 ptiCurrent
->ppi
->usi
.dwXSize
= ProcessParams
->CountX
;
569 ptiCurrent
->ppi
->usi
.dwYSize
= ProcessParams
->CountY
;
570 ptiCurrent
->ppi
->usi
.dwFlags
= ProcessParams
->WindowFlags
;
571 ptiCurrent
->ppi
->usi
.wShowWindow
= (WORD
)ProcessParams
->ShowWindowFlags
;
576 /* Assign a default window station and desktop to the process */
577 /* Do not try to open a desktop or window station before winlogon initializes */
578 if (ptiCurrent
->ppi
->hdeskStartup
== NULL
&& gpidLogon
!= 0)
580 HWINSTA hWinSta
= NULL
;
582 UNICODE_STRING DesktopPath
;
586 * inherit the thread desktop and process window station (if not yet inherited) from the process startup
587 * info structure. See documentation of CreateProcess()
590 Status
= STATUS_UNSUCCESSFUL
;
591 if (ProcessParams
&& ProcessParams
->DesktopInfo
.Length
> 0)
593 Status
= IntSafeCopyUnicodeStringTerminateNULL(&DesktopPath
, &ProcessParams
->DesktopInfo
);
595 if (!NT_SUCCESS(Status
))
597 RtlInitUnicodeString(&DesktopPath
, NULL
);
600 Status
= IntParseDesktopPath(Process
,
605 if (DesktopPath
.Buffer
)
606 ExFreePoolWithTag(DesktopPath
.Buffer
, TAG_STRING
);
608 if (!NT_SUCCESS(Status
))
610 ERR_CH(UserThread
, "Failed to assign default dekstop and winsta to process\n");
614 if (!UserSetProcessWindowStation(hWinSta
))
616 Status
= STATUS_UNSUCCESSFUL
;
617 ERR_CH(UserThread
, "Failed to set initial process winsta\n");
621 /* Validate the new desktop. */
622 Status
= IntValidateDesktopHandle(hDesk
, UserMode
, 0, &pdesk
);
623 if (!NT_SUCCESS(Status
))
625 ERR_CH(UserThread
, "Failed to validate initial desktop handle\n");
629 /* Store the parsed desktop as the initial desktop */
630 ptiCurrent
->ppi
->hdeskStartup
= hDesk
;
631 ptiCurrent
->ppi
->rpdeskStartup
= pdesk
;
634 if (ptiCurrent
->ppi
->hdeskStartup
!= NULL
)
636 if (!IntSetThreadDesktop(ptiCurrent
->ppi
->hdeskStartup
, FALSE
))
638 ERR_CH(UserThread
, "Failed to set thread desktop\n");
639 Status
= STATUS_UNSUCCESSFUL
;
644 /* Mark the thread as fully initialized */
645 ptiCurrent
->TIF_flags
|= TIF_GUITHREADINITIALIZED
;
647 if (!(ptiCurrent
->ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_APPSTARTING
)) &&
648 (gptiForeground
&& gptiForeground
->ppi
== ptiCurrent
->ppi
))
650 ptiCurrent
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
652 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
654 /* Last things to do only if we are not a SYSTEM or CSRSS thread */
655 if (!(ptiCurrent
->TIF_flags
& (TIF_SYSTEMTHREAD
| TIF_CSRSSTHREAD
)))
657 /* Callback to User32 Client Thread Setup */
658 TRACE_CH(UserThread
, "Call co_IntClientThreadSetup...\n");
659 Status
= co_IntClientThreadSetup();
660 if (!NT_SUCCESS(Status
))
662 ERR_CH(UserThread
, "ClientThreadSetup failed with Status 0x%08lx\n", Status
);
665 TRACE_CH(UserThread
, "co_IntClientThreadSetup succeeded!\n");
669 TRACE_CH(UserThread
, "co_IntClientThreadSetup cannot be called...\n");
672 TRACE_CH(UserThread
, "UserCreateW32Thread pti 0x%p\n", ptiCurrent
);
673 return STATUS_SUCCESS
;
676 ERR_CH(UserThread
, "InitThreadCallback failed! Freeing pti 0x%p for TID:0x%lx\n",
677 ptiCurrent
, HandleToUlong(Thread
->Cid
.UniqueThread
));
678 ExitThreadCallback(Thread
);
684 ExitThreadCallback(PETHREAD Thread
)
687 PSINGLE_LIST_ENTRY psle
;
688 PPROCESSINFO ppiCurrent
;
690 PTHREADINFO ptiCurrent
;
692 Process
= Thread
->ThreadsProcess
;
694 /* Get the Win32 Thread */
695 ptiCurrent
= PsGetThreadWin32Thread(Thread
);
698 TRACE_CH(UserThread
, "Destroying pti 0x%p eThread 0x%p\n", ptiCurrent
, Thread
);
700 ptiCurrent
->TIF_flags
|= TIF_INCLEANUP
;
701 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
703 ppiCurrent
= ptiCurrent
->ppi
;
706 IsRemoveAttachThread(ptiCurrent
);
708 ptiCurrent
->TIF_flags
|= TIF_DONTATTACHQUEUE
;
709 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
711 /* Decrement thread count and check if its 0 */
712 ppiCurrent
->cThreads
--;
714 if (ptiCurrent
->TIF_flags
& TIF_GUITHREADINITIALIZED
)
716 /* Do now some process cleanup that requires a valid win32 thread */
717 if (ptiCurrent
->ppi
->cThreads
== 0)
719 /* Check if we have registered the user api hook */
720 if (ptiCurrent
->ppi
== ppiUahServer
)
722 /* Unregister the api hook */
723 UserUnregisterUserApiHook();
726 /* Notify logon application to restart shell if needed */
727 if (ptiCurrent
->pDeskInfo
)
729 if (ptiCurrent
->pDeskInfo
->ppiShellProcess
== ppiCurrent
)
731 DWORD ExitCode
= PsGetProcessExitStatus(Process
);
733 TRACE_CH(UserProcess
, "Shell process is exiting (%lu)\n", ExitCode
);
735 UserPostMessage(hwndSAS
,
740 ptiCurrent
->pDeskInfo
->ppiShellProcess
= NULL
;
745 DceFreeThreadDCE(ptiCurrent
);
746 DestroyTimersForThread(ptiCurrent
);
747 KeSetEvent(ptiCurrent
->pEventQueueServer
, IO_NO_INCREMENT
, FALSE
);
748 UnregisterThreadHotKeys(ptiCurrent
);
750 if (!UserDestroyObjectsForOwner(gHandleTable
, ptiCurrent
))
752 DPRINT1("Failed to delete objects belonging to thread %p. This is VERY BAD!.\n", ptiCurrent
);
754 return STATUS_UNSUCCESSFUL
;
757 if (ppiCurrent
&& ppiCurrent
->ptiList
== ptiCurrent
&& !ptiCurrent
->ptiSibling
&&
758 ppiCurrent
->W32PF_flags
& W32PF_CLASSESREGISTERED
)
760 TRACE_CH(UserThread
, "DestroyProcessClasses\n");
761 /* no process windows should exist at this point, or the function will assert! */
762 DestroyProcessClasses(ppiCurrent
);
763 ppiCurrent
->W32PF_flags
&= ~W32PF_CLASSESREGISTERED
;
766 IntBlockInput(ptiCurrent
, FALSE
);
767 IntCleanupThreadCallbacks(ptiCurrent
);
769 /* cleanup user object references stack */
770 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
773 PUSER_REFERENCE_ENTRY ref
= CONTAINING_RECORD(psle
, USER_REFERENCE_ENTRY
, Entry
);
774 TRACE_CH(UserThread
, "thread clean: remove reference obj 0x%p\n",ref
->obj
);
775 UserDereferenceObject(ref
->obj
);
777 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
781 /* Find the THREADINFO in the PROCESSINFO's list */
782 ppti
= &ppiCurrent
->ptiList
;
783 while (*ppti
!= NULL
&& *ppti
!= ptiCurrent
)
785 ppti
= &((*ppti
)->ptiSibling
);
788 /* we must have found it */
789 ASSERT(*ppti
== ptiCurrent
);
791 /* Remove it from the list */
792 *ppti
= ptiCurrent
->ptiSibling
;
794 if (ptiCurrent
->KeyboardLayout
)
795 UserDereferenceObject(ptiCurrent
->KeyboardLayout
);
797 if (gptiForeground
== ptiCurrent
)
799 // IntNotifyWinEvent(EVENT_OBJECT_FOCUS, NULL, OBJID_CLIENT, CHILDID_SELF, 0);
800 // IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND, NULL, OBJID_WINDOW, CHILDID_SELF, 0);
802 gptiForeground
= NULL
;
805 // Fixes CORE-6384 & CORE-7030.
806 /* if (ptiLastInput == ptiCurrent)
808 if (!ppiCurrent->ptiList)
809 ptiLastInput = gptiForeground;
811 ptiLastInput = ppiCurrent->ptiList;
812 ERR_CH(UserThread, "DTI: ptiLastInput is Cleared!!\n");
815 TRACE_CH(UserThread
, "Freeing pti 0x%p\n", ptiCurrent
);
817 IntSetThreadDesktop(NULL
, TRUE
);
819 if (ptiCurrent
->hEventQueueClient
!= NULL
)
821 ObCloseHandle(ptiCurrent
->hEventQueueClient
, UserMode
);
822 ObDereferenceObject(ptiCurrent
->pEventQueueServer
);
824 ptiCurrent
->hEventQueueClient
= NULL
;
826 /* The thread is dying */
827 PsSetThreadWin32Thread(Thread
/*ptiCurrent->pEThread*/, NULL
, ptiCurrent
);
828 ptiCurrent
->pEThread
= NULL
;
830 /* Free the THREADINFO */
831 FreeW32Thread(/*Thread*/ ptiCurrent
); // IntDereferenceThreadInfo(ptiCurrent);
833 return STATUS_SUCCESS
;
838 Win32kThreadCallback(PETHREAD Thread
,
839 PSW32THREADCALLOUTTYPE Type
)
843 ASSERT(NtCurrentTeb());
845 UserEnterExclusive();
847 if (Type
== PsW32ThreadCalloutInitialize
)
849 ASSERT(PsGetThreadWin32Thread(Thread
) == NULL
);
850 Status
= InitThreadCallback(Thread
);
852 else // if (Type == PsW32ThreadCalloutExit)
854 ASSERT(PsGetThreadWin32Thread(Thread
) != NULL
);
855 Status
= ExitThreadCallback(Thread
);
863 _Function_class_(DRIVER_UNLOAD
)
865 DriverUnload(IN PDRIVER_OBJECT DriverObject
)
867 // TODO: Do more cleanup!
877 if (!NT_SUCCESS(Status)) \
879 DPRINT1("Failed '%s' (0x%lx)\n", #x, Status); \
885 * This definition doesn't work
891 IN PDRIVER_OBJECT DriverObject
,
892 IN PUNICODE_STRING RegistryPath
)
896 WIN32_CALLOUTS_FPNS CalloutData
= {0};
897 PVOID GlobalUserHeapBase
= NULL
;
900 * Register user mode call interface
901 * (system service table index = 1)
903 Result
= KeAddSystemServiceTable(Win32kSSDT
,
905 Win32kNumberOfSysCalls
,
910 DPRINT1("Adding system services failed!\n");
911 return STATUS_UNSUCCESSFUL
;
914 hModuleWin
= MmPageEntireDriver(DriverEntry
);
915 DPRINT("Win32k hInstance 0x%p!\n", hModuleWin
);
917 DriverObject
->DriverUnload
= DriverUnload
;
919 /* Register Object Manager Callbacks */
920 CalloutData
.ProcessCallout
= Win32kProcessCallback
;
921 CalloutData
.ThreadCallout
= Win32kThreadCallback
;
922 // CalloutData.GlobalAtomTableCallout = NULL;
923 // CalloutData.PowerEventCallout = NULL;
924 // CalloutData.PowerStateCallout = NULL;
925 // CalloutData.JobCallout = NULL;
926 CalloutData
.BatchFlushRoutine
= NtGdiFlushUserBatch
;
927 CalloutData
.DesktopOpenProcedure
= IntDesktopObjectOpen
;
928 CalloutData
.DesktopOkToCloseProcedure
= IntDesktopOkToClose
;
929 CalloutData
.DesktopCloseProcedure
= IntDesktopObjectClose
;
930 CalloutData
.DesktopDeleteProcedure
= IntDesktopObjectDelete
;
931 CalloutData
.WindowStationOkToCloseProcedure
= IntWinstaOkToClose
;
932 // CalloutData.WindowStationCloseProcedure = NULL;
933 CalloutData
.WindowStationDeleteProcedure
= IntWinStaObjectDelete
;
934 CalloutData
.WindowStationParseProcedure
= IntWinStaObjectParse
;
935 // CalloutData.WindowStationOpenProcedure = NULL;
937 /* Register our per-process and per-thread structures. */
938 PsEstablishWin32Callouts(&CalloutData
);
940 /* Register service hook callbacks */
941 #if DBG && defined(KDBG)
942 KdSystemDebugControl('CsoR', DbgPreServiceHook
, ID_Win32PreServiceHook
, 0, 0, 0, 0);
943 KdSystemDebugControl('CsoR', DbgPostServiceHook
, ID_Win32PostServiceHook
, 0, 0, 0, 0);
946 /* Create the global USER heap */
947 GlobalUserHeap
= UserCreateHeap(&GlobalUserHeapSection
,
949 1 * 1024 * 1024); /* FIXME: 1 MB for now... */
950 if (GlobalUserHeap
== NULL
)
952 DPRINT1("Failed to initialize the global heap!\n");
953 return STATUS_UNSUCCESSFUL
;
956 /* Allocate global server info structure */
957 gpsi
= UserHeapAlloc(sizeof(*gpsi
));
960 DPRINT1("Failed allocate server info structure!\n");
961 return STATUS_UNSUCCESSFUL
;
964 RtlZeroMemory(gpsi
, sizeof(*gpsi
));
965 DPRINT("Global Server Data -> %p\n", gpsi
);
967 NT_ROF(InitGdiHandleTable());
968 NT_ROF(InitPaletteImpl());
970 /* Create stock objects, ie. precreated objects commonly
971 used by win32 applications */
972 CreateStockObjects();
973 CreateSysColorObjects();
975 NT_ROF(InitBrushImpl());
976 NT_ROF(InitPDEVImpl());
977 NT_ROF(InitLDEVImpl());
978 NT_ROF(InitDeviceImpl());
979 NT_ROF(InitDcImpl());
980 NT_ROF(InitUserImpl());
981 NT_ROF(InitWindowStationImpl());
982 NT_ROF(InitDesktopImpl());
983 NT_ROF(InitInputImpl());
984 NT_ROF(InitKeyboardImpl());
985 NT_ROF(MsqInitializeImpl());
986 NT_ROF(InitTimerImpl());
987 NT_ROF(InitDCEImpl());
989 /* Initialize FreeType library */
990 if (!InitFontSupport())
992 DPRINT1("Unable to initialize font support\n");
996 gusLanguageID
= UserGetLanguageID();
998 return STATUS_SUCCESS
;