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
);
140 /* Allocate memory for the event structure */
141 Event
= ExAllocatePoolWithTag(NonPagedPool
,
146 /* Initialize the kernel event */
147 KeInitializeEvent(Event
,
148 SynchronizationEvent
,
154 DPRINT("CreateEvent() failed\n");
159 ppiCurrent
->InputIdleEvent
= Event
;
160 KeInitializeEvent(ppiCurrent
->InputIdleEvent
, NotificationEvent
, FALSE
);
163 ppiCurrent
->peProcess
= Process
;
164 ppiCurrent
->W32Pid
= HandleToUlong(PsGetProcessId(Process
));
166 /* Setup process flags */
167 ppiCurrent
->W32PF_flags
|= W32PF_PROCESSCONNECTED
;
168 if ( Process
->Peb
->ProcessParameters
&&
169 Process
->Peb
->ProcessParameters
->WindowFlags
& STARTF_SCRNSAVER
)
171 ppiScrnSaver
= ppiCurrent
;
172 ppiCurrent
->W32PF_flags
|= W32PF_SCREENSAVER
;
175 // FIXME: check if this process is allowed.
176 ppiCurrent
->W32PF_flags
|= W32PF_ALLOWFOREGROUNDACTIVATE
; // Starting application will get it toggled off.
178 return STATUS_SUCCESS
;
182 UserProcessDestroy(PEPROCESS Process
)
184 PPROCESSINFO ppiCurrent
= PsGetProcessWin32Process(Process
);
187 if (ppiScrnSaver
== ppiCurrent
)
190 /* Destroy user objects */
191 UserDestroyObjectsForOwner(gHandleTable
, ppiCurrent
);
193 TRACE_CH(UserProcess
, "Freeing ppi 0x%p\n", ppiCurrent
);
195 if (DBG_IS_CHANNEL_ENABLED(ppiCurrent
, DbgChUserObj
, WARN_LEVEL
))
197 TRACE_CH(UserObj
, "Dumping user handles at the end of the process %s (Info %p).\n",
198 ppiCurrent
->peProcess
->ImageFileName
, ppiCurrent
);
199 DbgUserDumpHandleTable();
203 /* Remove it from the list of GUI apps */
204 co_IntGraphicsCheck(FALSE
);
207 * Deregister logon application automatically
209 if (gpidLogon
== ppiCurrent
->peProcess
->UniqueProcessId
)
212 /* Close the current window station */
213 UserSetProcessWindowStation(NULL
);
215 if (gppiInputProvider
== ppiCurrent
) gppiInputProvider
= NULL
;
217 if (ppiCurrent
->hdeskStartup
)
219 ZwClose(ppiCurrent
->hdeskStartup
);
220 ppiCurrent
->hdeskStartup
= NULL
;
223 /* Clean up the process icon cache */
224 IntCleanupCurIconCache(ppiCurrent
);
226 return STATUS_SUCCESS
;
230 InitProcessCallback(PEPROCESS Process
)
233 PPROCESSINFO ppiCurrent
;
234 PVOID KernelMapping
= NULL
, UserMapping
= NULL
;
236 /* We might be called with an already allocated win32 process */
237 ppiCurrent
= PsGetProcessWin32Process(Process
);
238 if (ppiCurrent
!= NULL
)
240 /* There is no more to do for us (this is a success code!) */
241 return STATUS_ALREADY_WIN32
;
243 // if (ppiCurrent->W32PF_flags & W32PF_PROCESSCONNECTED)
244 // return STATUS_ALREADY_WIN32;
246 /* Allocate a new Win32 process info */
247 Status
= AllocW32Process(Process
, &ppiCurrent
);
248 if (!NT_SUCCESS(Status
))
250 ERR_CH(UserProcess
, "Failed to allocate ppi for PID:0x%lx\n",
251 HandleToUlong(Process
->UniqueProcessId
));
256 DbgInitDebugChannels();
258 KdRosRegisterCliCallback(DbgGdiKdbgCliCallback
);
262 /* Map the global user heap into the process */
263 Status
= MapGlobalUserHeap(Process
, &KernelMapping
, &UserMapping
);
264 if (!NT_SUCCESS(Status
))
266 TRACE_CH(UserProcess
, "Failed to map the global heap! 0x%x\n", Status
);
270 TRACE_CH(UserProcess
, "InitProcessCallback -- We have KernelMapping 0x%p and UserMapping 0x%p with delta = 0x%x\n",
271 KernelMapping
, UserMapping
, (ULONG_PTR
)KernelMapping
- (ULONG_PTR
)UserMapping
);
273 /* Initialize USER process info */
274 Status
= UserProcessCreate(Process
);
275 if (!NT_SUCCESS(Status
))
277 ERR_CH(UserProcess
, "UserProcessCreate failed, Status 0x%08lx\n", Status
);
281 /* Initialize GDI process info */
282 Status
= GdiProcessCreate(Process
);
283 if (!NT_SUCCESS(Status
))
285 ERR_CH(UserProcess
, "GdiProcessCreate failed, Status 0x%08lx\n", Status
);
289 /* Add the process to the global list */
290 ppiCurrent
->ppiNext
= gppiList
;
291 gppiList
= ppiCurrent
;
293 return STATUS_SUCCESS
;
296 ERR_CH(UserProcess
, "InitProcessCallback failed! Freeing ppi 0x%p for PID:0x%lx\n",
297 ppiCurrent
, HandleToUlong(Process
->UniqueProcessId
));
298 ExitProcessCallback(Process
);
303 ExitProcessCallback(PEPROCESS Process
)
305 PPROCESSINFO ppiCurrent
, *pppi
;
307 /* Get the Win32 Process */
308 ppiCurrent
= PsGetProcessWin32Process(Process
);
310 ASSERT(ppiCurrent
->peProcess
== Process
);
312 TRACE_CH(UserProcess
, "Destroying ppi 0x%p\n", ppiCurrent
);
313 ppiCurrent
->W32PF_flags
|= W32PF_TERMINATED
;
315 /* Remove it from the list */
317 while (*pppi
!= NULL
&& *pppi
!= ppiCurrent
)
319 pppi
= &(*pppi
)->ppiNext
;
321 ASSERT(*pppi
== ppiCurrent
);
322 *pppi
= ppiCurrent
->ppiNext
;
324 /* Cleanup GDI info */
325 GdiProcessDestroy(Process
);
327 /* Cleanup USER info */
328 UserProcessDestroy(Process
);
330 /* The process is dying */
331 PsSetProcessWin32Process(Process
, NULL
, ppiCurrent
);
332 ppiCurrent
->peProcess
= NULL
;
334 /* Finally, dereference */
335 FreeW32Process(/*Process*/ ppiCurrent
); // IntDereferenceProcessInfo(ppiCurrent);
337 return STATUS_SUCCESS
;
342 Win32kProcessCallback(PEPROCESS Process
,
347 ASSERT(Process
->Peb
);
349 TRACE_CH(UserProcess
, "Win32kProcessCallback -->\n");
351 UserEnterExclusive();
355 Status
= InitProcessCallback(Process
);
359 Status
= ExitProcessCallback(Process
);
364 TRACE_CH(UserProcess
, "<-- Win32kProcessCallback\n");
372 AllocW32Thread(IN PETHREAD Thread
,
373 OUT PTHREADINFO
* W32Thread
)
375 PTHREADINFO ptiCurrent
;
377 TRACE_CH(UserThread
, "In AllocW32Thread(0x%p)\n", Thread
);
379 /* Check that we were not called with an already existing Win32 thread info */
380 ptiCurrent
= PsGetThreadWin32Thread(Thread
);
381 NT_ASSERT(ptiCurrent
== NULL
);
383 /* Allocate a new Win32 thread info */
384 ptiCurrent
= ExAllocatePoolWithTag(NonPagedPool
,
387 if (ptiCurrent
== NULL
)
389 ERR_CH(UserThread
, "Failed to allocate pti for TID:0x%lx\n",
390 HandleToUlong(Thread
->Cid
.UniqueThread
));
391 return STATUS_NO_MEMORY
;
394 TRACE_CH(UserThread
, "Allocated pti 0x%p for TID:0x%lx\n",
395 ptiCurrent
, HandleToUlong(Thread
->Cid
.UniqueThread
));
397 RtlZeroMemory(ptiCurrent
, sizeof(*ptiCurrent
));
399 PsSetThreadWin32Thread(Thread
, ptiCurrent
, NULL
);
400 IntReferenceThreadInfo(ptiCurrent
);
402 *W32Thread
= ptiCurrent
;
403 return STATUS_SUCCESS
;
406 #define FreeW32Thread(/*Thread*/ W32Thread) \
408 /*PTHREADINFO W32Thread = PsGetThreadWin32Thread(Thread);*/ \
409 /*ASSERT(W32Thread);*/ \
410 IntDereferenceThreadInfo(W32Thread); \
414 * Called from IntDereferenceThreadInfo
417 UserDeleteW32Thread(PTHREADINFO pti
)
419 PPROCESSINFO ppi
= pti
->ppi
;
421 TRACE_CH(UserThread
, "UserDeleteW32Thread pti 0x%p\n",pti
);
423 /* Free the message queue */
424 if (pti
->MessageQueue
)
426 MsqDestroyMessageQueue(pti
);
429 MsqCleanupThreadMsgs(pti
);
431 ExFreePoolWithTag(pti
, USERTAG_THREADINFO
);
433 IntDereferenceProcessInfo(ppi
);
437 UserThreadCreate(PETHREAD Thread
)
439 return STATUS_SUCCESS
;
443 UserThreadDestroy(PETHREAD Thread
)
445 return STATUS_SUCCESS
;
449 InitThreadCallback(PETHREAD Thread
)
453 PTHREADINFO ptiCurrent
;
455 NTSTATUS Status
= STATUS_SUCCESS
;
457 LARGE_INTEGER LargeTickCount
;
458 PRTL_USER_PROCESS_PARAMETERS ProcessParams
;
460 Process
= Thread
->ThreadsProcess
;
462 pTeb
= NtCurrentTeb();
465 ProcessParams
= pTeb
->ProcessEnvironmentBlock
->ProcessParameters
;
467 /* Allocate a new Win32 thread info */
468 Status
= AllocW32Thread(Thread
, &ptiCurrent
);
469 if (!NT_SUCCESS(Status
))
471 ERR_CH(UserThread
, "Failed to allocate pti for TID:0x%lx\n",
472 HandleToUlong(Thread
->Cid
.UniqueThread
));
476 /* Initialize the THREADINFO */
477 ptiCurrent
->pEThread
= Thread
;
478 ptiCurrent
->ppi
= PsGetProcessWin32Process(Process
);
479 IntReferenceProcessInfo(ptiCurrent
->ppi
);
480 pTeb
->Win32ThreadInfo
= ptiCurrent
;
481 ptiCurrent
->pClientInfo
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
483 /* Mark the process as having threads */
484 ptiCurrent
->ppi
->W32PF_flags
|= W32PF_THREADCONNECTED
;
486 InitializeListHead(&ptiCurrent
->WindowListHead
);
487 InitializeListHead(&ptiCurrent
->W32CallbackListHead
);
488 InitializeListHead(&ptiCurrent
->PostedMessagesListHead
);
489 InitializeListHead(&ptiCurrent
->SentMessagesListHead
);
490 InitializeListHead(&ptiCurrent
->PtiLink
);
491 for (i
= 0; i
< NB_HOOKS
; i
++)
493 InitializeListHead(&ptiCurrent
->aphkStart
[i
]);
495 ptiCurrent
->ptiSibling
= ptiCurrent
->ppi
->ptiList
;
496 ptiCurrent
->ppi
->ptiList
= ptiCurrent
;
497 ptiCurrent
->ppi
->cThreads
++;
499 ptiCurrent
->hEventQueueClient
= NULL
;
500 Status
= ZwCreateEvent(&ptiCurrent
->hEventQueueClient
, EVENT_ALL_ACCESS
,
501 NULL
, SynchronizationEvent
, FALSE
);
502 if (!NT_SUCCESS(Status
))
504 ERR_CH(UserThread
, "Event creation failed, Status 0x%08x.\n", Status
);
507 Status
= ObReferenceObjectByHandle(ptiCurrent
->hEventQueueClient
, 0,
508 *ExEventObjectType
, UserMode
,
509 (PVOID
*)&ptiCurrent
->pEventQueueServer
, NULL
);
510 if (!NT_SUCCESS(Status
))
512 ERR_CH(UserThread
, "Failed referencing the event object, Status 0x%08x.\n", Status
);
513 ObCloseHandle(ptiCurrent
->hEventQueueClient
, UserMode
);
514 ptiCurrent
->hEventQueueClient
= NULL
;
518 KeQueryTickCount(&LargeTickCount
);
519 ptiCurrent
->timeLast
= LargeTickCount
.u
.LowPart
;
521 ptiCurrent
->MessageQueue
= MsqCreateMessageQueue(ptiCurrent
);
522 if (ptiCurrent
->MessageQueue
== NULL
)
524 ERR_CH(UserThread
, "Failed to allocate message loop\n");
525 Status
= STATUS_NO_MEMORY
;
529 ptiCurrent
->KeyboardLayout
= W32kGetDefaultKeyLayout();
530 if (ptiCurrent
->KeyboardLayout
)
531 UserReferenceObject(ptiCurrent
->KeyboardLayout
);
533 ptiCurrent
->TIF_flags
&= ~TIF_INCLEANUP
;
535 /* CSRSS threads have some special features */
536 if (Process
== gpepCSRSS
)
537 ptiCurrent
->TIF_flags
= TIF_CSRSSTHREAD
| TIF_DONTATTACHQUEUE
;
539 // FIXME: Flag SYSTEM threads with... TIF_SYSTEMTHREAD !!
541 ptiCurrent
->pcti
= &ptiCurrent
->cti
;
543 /* Initialize the CLIENTINFO */
544 pci
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
545 RtlZeroMemory(pci
, sizeof(*pci
));
546 pci
->ppi
= ptiCurrent
->ppi
;
547 pci
->fsHooks
= ptiCurrent
->fsHooks
;
548 pci
->dwTIFlags
= ptiCurrent
->TIF_flags
;
549 if (ptiCurrent
->KeyboardLayout
)
551 pci
->hKL
= ptiCurrent
->KeyboardLayout
->hkl
;
552 pci
->CodePage
= ptiCurrent
->KeyboardLayout
->CodePage
;
555 /* Need to pass the user Startup Information to the current process. */
558 if ( ptiCurrent
->ppi
->usi
.cb
== 0 ) // Not initialized yet.
560 if ( ProcessParams
->WindowFlags
!= 0 ) // Need window flags set.
562 ptiCurrent
->ppi
->usi
.cb
= sizeof(USERSTARTUPINFO
);
563 ptiCurrent
->ppi
->usi
.dwX
= ProcessParams
->StartingX
;
564 ptiCurrent
->ppi
->usi
.dwY
= ProcessParams
->StartingY
;
565 ptiCurrent
->ppi
->usi
.dwXSize
= ProcessParams
->CountX
;
566 ptiCurrent
->ppi
->usi
.dwYSize
= ProcessParams
->CountY
;
567 ptiCurrent
->ppi
->usi
.dwFlags
= ProcessParams
->WindowFlags
;
568 ptiCurrent
->ppi
->usi
.wShowWindow
= (WORD
)ProcessParams
->ShowWindowFlags
;
573 /* Assign a default window station and desktop to the process */
574 /* Do not try to open a desktop or window station before winlogon initializes */
575 if (ptiCurrent
->ppi
->hdeskStartup
== NULL
&& gpidLogon
!= 0)
577 HWINSTA hWinSta
= NULL
;
579 UNICODE_STRING DesktopPath
;
583 * inherit the thread desktop and process window station (if not yet inherited) from the process startup
584 * info structure. See documentation of CreateProcess()
587 Status
= STATUS_UNSUCCESSFUL
;
588 if (ProcessParams
&& ProcessParams
->DesktopInfo
.Length
> 0)
590 Status
= IntSafeCopyUnicodeStringTerminateNULL(&DesktopPath
, &ProcessParams
->DesktopInfo
);
592 if (!NT_SUCCESS(Status
))
594 RtlInitUnicodeString(&DesktopPath
, NULL
);
597 Status
= IntParseDesktopPath(Process
,
602 if (DesktopPath
.Buffer
)
603 ExFreePoolWithTag(DesktopPath
.Buffer
, TAG_STRING
);
605 if (!NT_SUCCESS(Status
))
607 ERR_CH(UserThread
, "Failed to assign default dekstop and winsta to process\n");
611 if (!UserSetProcessWindowStation(hWinSta
))
613 Status
= STATUS_UNSUCCESSFUL
;
614 ERR_CH(UserThread
, "Failed to set initial process winsta\n");
618 /* Validate the new desktop. */
619 Status
= IntValidateDesktopHandle(hDesk
, UserMode
, 0, &pdesk
);
620 if (!NT_SUCCESS(Status
))
622 ERR_CH(UserThread
, "Failed to validate initial desktop handle\n");
626 /* Store the parsed desktop as the initial desktop */
627 ptiCurrent
->ppi
->hdeskStartup
= hDesk
;
628 ptiCurrent
->ppi
->rpdeskStartup
= pdesk
;
631 if (ptiCurrent
->ppi
->hdeskStartup
!= NULL
)
633 if (!IntSetThreadDesktop(ptiCurrent
->ppi
->hdeskStartup
, FALSE
))
635 ERR_CH(UserThread
, "Failed to set thread desktop\n");
636 Status
= STATUS_UNSUCCESSFUL
;
641 /* Mark the thread as fully initialized */
642 ptiCurrent
->TIF_flags
|= TIF_GUITHREADINITIALIZED
;
644 if (!(ptiCurrent
->ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_APPSTARTING
)) &&
645 (gptiForeground
&& gptiForeground
->ppi
== ptiCurrent
->ppi
))
647 ptiCurrent
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
649 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
651 /* Last things to do only if we are not a SYSTEM or CSRSS thread */
652 if (!(ptiCurrent
->TIF_flags
& (TIF_SYSTEMTHREAD
| TIF_CSRSSTHREAD
)))
654 /* Callback to User32 Client Thread Setup */
655 TRACE_CH(UserThread
, "Call co_IntClientThreadSetup...\n");
656 Status
= co_IntClientThreadSetup();
657 if (!NT_SUCCESS(Status
))
659 ERR_CH(UserThread
, "ClientThreadSetup failed with Status 0x%08lx\n", Status
);
662 TRACE_CH(UserThread
, "co_IntClientThreadSetup succeeded!\n");
666 TRACE_CH(UserThread
, "co_IntClientThreadSetup cannot be called...\n");
669 TRACE_CH(UserThread
, "UserCreateW32Thread pti 0x%p\n", ptiCurrent
);
670 return STATUS_SUCCESS
;
673 ERR_CH(UserThread
, "InitThreadCallback failed! Freeing pti 0x%p for TID:0x%lx\n",
674 ptiCurrent
, HandleToUlong(Thread
->Cid
.UniqueThread
));
675 ExitThreadCallback(Thread
);
680 UserDisplayNotifyShutdown(PPROCESSINFO ppiCurrent
);
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 UserCloseClipboard();
713 /* Decrement thread count and check if its 0 */
714 ppiCurrent
->cThreads
--;
716 if (ptiCurrent
->TIF_flags
& TIF_GUITHREADINITIALIZED
)
718 /* Do now some process cleanup that requires a valid win32 thread */
719 if (ptiCurrent
->ppi
->cThreads
== 0)
721 /* Check if we have registered the user api hook */
722 if (ptiCurrent
->ppi
== ppiUahServer
)
724 /* Unregister the api hook */
725 UserUnregisterUserApiHook();
728 /* Notify logon application to restart shell if needed */
729 if (ptiCurrent
->pDeskInfo
)
731 if (ptiCurrent
->pDeskInfo
->ppiShellProcess
== ppiCurrent
)
733 DWORD ExitCode
= PsGetProcessExitStatus(Process
);
735 TRACE_CH(UserProcess
, "Shell process is exiting (%lu)\n", ExitCode
);
737 UserPostMessage(hwndSAS
,
742 ptiCurrent
->pDeskInfo
->ppiShellProcess
= NULL
;
747 DceFreeThreadDCE(ptiCurrent
);
748 DestroyTimersForThread(ptiCurrent
);
749 KeSetEvent(ptiCurrent
->pEventQueueServer
, IO_NO_INCREMENT
, FALSE
);
750 UnregisterThreadHotKeys(ptiCurrent
);
752 if (!UserDestroyObjectsForOwner(gHandleTable
, ptiCurrent
))
754 DPRINT1("Failed to delete objects belonging to thread %p. This is VERY BAD!.\n", ptiCurrent
);
756 return STATUS_UNSUCCESSFUL
;
759 if (ppiCurrent
&& ppiCurrent
->ptiList
== ptiCurrent
&& !ptiCurrent
->ptiSibling
&&
760 ppiCurrent
->W32PF_flags
& W32PF_CLASSESREGISTERED
)
762 TRACE_CH(UserThread
, "DestroyProcessClasses\n");
763 /* no process windows should exist at this point, or the function will assert! */
764 DestroyProcessClasses(ppiCurrent
);
765 ppiCurrent
->W32PF_flags
&= ~W32PF_CLASSESREGISTERED
;
768 IntBlockInput(ptiCurrent
, FALSE
);
769 IntCleanupThreadCallbacks(ptiCurrent
);
771 /* cleanup user object references stack */
772 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
775 PUSER_REFERENCE_ENTRY ref
= CONTAINING_RECORD(psle
, USER_REFERENCE_ENTRY
, Entry
);
776 TRACE_CH(UserThread
, "thread clean: remove reference obj 0x%p\n",ref
->obj
);
777 UserDereferenceObject(ref
->obj
);
779 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
783 if (ptiCurrent
->cEnterCount
)
785 KeSetKernelStackSwapEnable(TRUE
);
786 ptiCurrent
->cEnterCount
= 0;
789 /* Find the THREADINFO in the PROCESSINFO's list */
790 ppti
= &ppiCurrent
->ptiList
;
791 while (*ppti
!= NULL
&& *ppti
!= ptiCurrent
)
793 ppti
= &((*ppti
)->ptiSibling
);
796 /* we must have found it */
797 ASSERT(*ppti
== ptiCurrent
);
799 /* Remove it from the list */
800 *ppti
= ptiCurrent
->ptiSibling
;
802 if (ptiCurrent
->KeyboardLayout
)
803 UserDereferenceObject(ptiCurrent
->KeyboardLayout
);
805 if (gptiForeground
== ptiCurrent
)
807 // IntNotifyWinEvent(EVENT_OBJECT_FOCUS, NULL, OBJID_CLIENT, CHILDID_SELF, 0);
808 // IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND, NULL, OBJID_WINDOW, CHILDID_SELF, 0);
810 gptiForeground
= NULL
;
813 /* Restore display mode when we are the last thread, and we changed the display mode */
814 if (ppiCurrent
->cThreads
== 0)
815 UserDisplayNotifyShutdown(ppiCurrent
);
818 // Fixes CORE-6384 & CORE-7030.
819 /* if (ptiLastInput == ptiCurrent)
821 if (!ppiCurrent->ptiList)
822 ptiLastInput = gptiForeground;
824 ptiLastInput = ppiCurrent->ptiList;
825 ERR_CH(UserThread, "DTI: ptiLastInput is Cleared!!\n");
828 TRACE_CH(UserThread
, "Freeing pti 0x%p\n", ptiCurrent
);
830 IntSetThreadDesktop(NULL
, TRUE
);
832 if (ptiCurrent
->hEventQueueClient
!= NULL
)
834 ObCloseHandle(ptiCurrent
->hEventQueueClient
, UserMode
);
835 ObDereferenceObject(ptiCurrent
->pEventQueueServer
);
837 ptiCurrent
->hEventQueueClient
= NULL
;
839 /* The thread is dying */
840 PsSetThreadWin32Thread(Thread
/*ptiCurrent->pEThread*/, NULL
, ptiCurrent
);
841 ptiCurrent
->pEThread
= NULL
;
843 /* Free the THREADINFO */
844 FreeW32Thread(/*Thread*/ ptiCurrent
); // IntDereferenceThreadInfo(ptiCurrent);
846 return STATUS_SUCCESS
;
851 Win32kThreadCallback(PETHREAD Thread
,
852 PSW32THREADCALLOUTTYPE Type
)
856 ASSERT(NtCurrentTeb());
858 UserEnterExclusive();
860 if (Type
== PsW32ThreadCalloutInitialize
)
862 ASSERT(PsGetThreadWin32Thread(Thread
) == NULL
);
863 Status
= InitThreadCallback(Thread
);
865 else // if (Type == PsW32ThreadCalloutExit)
867 ASSERT(PsGetThreadWin32Thread(Thread
) != NULL
);
868 Status
= ExitThreadCallback(Thread
);
876 _Function_class_(DRIVER_UNLOAD
)
878 DriverUnload(IN PDRIVER_OBJECT DriverObject
)
880 // TODO: Do more cleanup!
890 if (!NT_SUCCESS(Status)) \
892 DPRINT1("Failed '%s' (0x%lx)\n", #x, Status); \
898 * This definition doesn't work
904 IN PDRIVER_OBJECT DriverObject
,
905 IN PUNICODE_STRING RegistryPath
)
909 WIN32_CALLOUTS_FPNS CalloutData
= {0};
910 PVOID GlobalUserHeapBase
= NULL
;
913 * Register user mode call interface
914 * (system service table index = 1)
916 Result
= KeAddSystemServiceTable(Win32kSSDT
,
918 Win32kNumberOfSysCalls
,
923 DPRINT1("Adding system services failed!\n");
924 return STATUS_UNSUCCESSFUL
;
927 hModuleWin
= MmPageEntireDriver(DriverEntry
);
928 DPRINT("Win32k hInstance 0x%p!\n", hModuleWin
);
930 DriverObject
->DriverUnload
= DriverUnload
;
932 /* Register Object Manager Callbacks */
933 CalloutData
.ProcessCallout
= Win32kProcessCallback
;
934 CalloutData
.ThreadCallout
= Win32kThreadCallback
;
935 // CalloutData.GlobalAtomTableCallout = NULL;
936 // CalloutData.PowerEventCallout = NULL;
937 // CalloutData.PowerStateCallout = NULL;
938 // CalloutData.JobCallout = NULL;
939 CalloutData
.BatchFlushRoutine
= NtGdiFlushUserBatch
;
940 CalloutData
.DesktopOpenProcedure
= IntDesktopObjectOpen
;
941 CalloutData
.DesktopOkToCloseProcedure
= IntDesktopOkToClose
;
942 CalloutData
.DesktopCloseProcedure
= IntDesktopObjectClose
;
943 CalloutData
.DesktopDeleteProcedure
= IntDesktopObjectDelete
;
944 CalloutData
.WindowStationOkToCloseProcedure
= IntWinStaOkToClose
;
945 // CalloutData.WindowStationCloseProcedure = NULL;
946 CalloutData
.WindowStationDeleteProcedure
= IntWinStaObjectDelete
;
947 CalloutData
.WindowStationParseProcedure
= IntWinStaObjectParse
;
948 // CalloutData.WindowStationOpenProcedure = NULL;
950 /* Register our per-process and per-thread structures. */
951 PsEstablishWin32Callouts(&CalloutData
);
953 /* Register service hook callbacks */
954 #if DBG && defined(KDBG)
955 KdSystemDebugControl('CsoR', DbgPreServiceHook
, ID_Win32PreServiceHook
, 0, 0, 0, 0);
956 KdSystemDebugControl('CsoR', DbgPostServiceHook
, ID_Win32PostServiceHook
, 0, 0, 0, 0);
959 /* Create the global USER heap */
960 GlobalUserHeap
= UserCreateHeap(&GlobalUserHeapSection
,
962 1 * 1024 * 1024); /* FIXME: 1 MB for now... */
963 if (GlobalUserHeap
== NULL
)
965 DPRINT1("Failed to initialize the global heap!\n");
966 return STATUS_UNSUCCESSFUL
;
969 /* Allocate global server info structure */
970 gpsi
= UserHeapAlloc(sizeof(*gpsi
));
973 DPRINT1("Failed allocate server info structure!\n");
974 return STATUS_UNSUCCESSFUL
;
977 RtlZeroMemory(gpsi
, sizeof(*gpsi
));
978 DPRINT("Global Server Data -> %p\n", gpsi
);
980 NT_ROF(InitGdiHandleTable());
981 NT_ROF(InitPaletteImpl());
983 /* Create stock objects, ie. precreated objects commonly
984 used by win32 applications */
985 CreateStockObjects();
986 CreateSysColorObjects();
988 NT_ROF(InitBrushImpl());
989 NT_ROF(InitPDEVImpl());
990 NT_ROF(InitLDEVImpl());
991 NT_ROF(InitDeviceImpl());
992 NT_ROF(InitDcImpl());
993 NT_ROF(InitUserImpl());
994 NT_ROF(InitWindowStationImpl());
995 NT_ROF(InitDesktopImpl());
996 NT_ROF(InitInputImpl());
997 NT_ROF(InitKeyboardImpl());
998 NT_ROF(MsqInitializeImpl());
999 NT_ROF(InitTimerImpl());
1000 NT_ROF(InitDCEImpl());
1002 gusLanguageID
= UserGetLanguageID();
1004 /* Initialize FreeType library */
1005 if (!InitFontSupport())
1007 DPRINT1("Unable to initialize font support\n");
1011 return STATUS_SUCCESS
;