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
;
93 * Called from IntDereferenceProcessInfo
97 _Pre_notnull_
__drv_freesMem(Mem
) PPROCESSINFO ppiCurrent
)
99 if (ppiCurrent
->InputIdleEvent
)
101 /* Free the allocated memory */
102 ExFreePoolWithTag(ppiCurrent
->InputIdleEvent
, USERTAG_EVENT
);
105 /* Close the startup desktop */
106 if (ppiCurrent
->rpdeskStartup
)
107 ObDereferenceObject(ppiCurrent
->rpdeskStartup
);
110 if (DBG_IS_CHANNEL_ENABLED(ppiCurrent
, DbgChUserObj
, WARN_LEVEL
))
112 TRACE_PPI(ppiCurrent
, UserObj
, "Dumping user handles now that process info %p is gets freed.\n", ppiCurrent
);
113 DbgUserDumpHandleTable();
117 /* Free the PROCESSINFO */
118 ExFreePoolWithTag(ppiCurrent
, USERTAG_PROCESSINFO
);
122 UserProcessCreate(PEPROCESS Process
)
124 PPROCESSINFO ppiCurrent
= PsGetProcessWin32Process(Process
);
127 InitializeListHead(&ppiCurrent
->DriverObjListHead
);
128 ExInitializeFastMutex(&ppiCurrent
->DriverObjListLock
);
133 /* Allocate memory for the event structure */
134 Event
= ExAllocatePoolWithTag(NonPagedPool
,
139 /* Initialize the kernel event */
140 KeInitializeEvent(Event
,
141 SynchronizationEvent
,
147 DPRINT("CreateEvent() failed\n");
152 ppiCurrent
->InputIdleEvent
= Event
;
153 KeInitializeEvent(ppiCurrent
->InputIdleEvent
, NotificationEvent
, FALSE
);
156 ppiCurrent
->peProcess
= Process
;
157 ppiCurrent
->W32Pid
= HandleToUlong(PsGetProcessId(Process
));
159 /* Setup process flags */
160 ppiCurrent
->W32PF_flags
|= W32PF_PROCESSCONNECTED
;
161 if (Process
->Peb
->ProcessParameters
&&
162 (Process
->Peb
->ProcessParameters
->WindowFlags
& STARTF_SCREENSAVER
))
164 ppiScrnSaver
= ppiCurrent
;
165 ppiCurrent
->W32PF_flags
|= W32PF_SCREENSAVER
;
168 // FIXME: check if this process is allowed.
169 ppiCurrent
->W32PF_flags
|= W32PF_ALLOWFOREGROUNDACTIVATE
; // Starting application will get it toggled off.
171 return STATUS_SUCCESS
;
175 UserProcessDestroy(PEPROCESS Process
)
177 PPROCESSINFO ppiCurrent
= PsGetProcessWin32Process(Process
);
180 if (ppiScrnSaver
== ppiCurrent
)
183 /* Destroy user objects */
184 UserDestroyObjectsForOwner(gHandleTable
, ppiCurrent
);
186 TRACE_CH(UserProcess
, "Freeing ppi 0x%p\n", ppiCurrent
);
188 if (DBG_IS_CHANNEL_ENABLED(ppiCurrent
, DbgChUserObj
, WARN_LEVEL
))
190 TRACE_CH(UserObj
, "Dumping user handles at the end of the process %s (Info %p).\n",
191 ppiCurrent
->peProcess
->ImageFileName
, ppiCurrent
);
192 DbgUserDumpHandleTable();
196 /* Remove it from the list of GUI apps */
197 co_IntGraphicsCheck(FALSE
);
200 * Deregister logon application automatically
202 if (gpidLogon
== ppiCurrent
->peProcess
->UniqueProcessId
)
205 /* Close the current window station */
206 UserSetProcessWindowStation(NULL
);
208 if (gppiInputProvider
== ppiCurrent
) gppiInputProvider
= NULL
;
210 if (ppiCurrent
->hdeskStartup
)
212 ZwClose(ppiCurrent
->hdeskStartup
);
213 ppiCurrent
->hdeskStartup
= NULL
;
216 /* Clean up the process icon cache */
217 IntCleanupCurIconCache(ppiCurrent
);
219 return STATUS_SUCCESS
;
223 InitProcessCallback(PEPROCESS Process
)
226 PPROCESSINFO ppiCurrent
;
227 PVOID KernelMapping
= NULL
, UserMapping
= NULL
;
229 /* We might be called with an already allocated win32 process */
230 ppiCurrent
= PsGetProcessWin32Process(Process
);
231 if (ppiCurrent
!= NULL
)
233 /* There is no more to do for us (this is a success code!) */
234 return STATUS_ALREADY_WIN32
;
236 // if (ppiCurrent->W32PF_flags & W32PF_PROCESSCONNECTED)
237 // return STATUS_ALREADY_WIN32;
239 /* Allocate a new Win32 process info */
240 Status
= AllocW32Process(Process
, &ppiCurrent
);
241 if (!NT_SUCCESS(Status
))
243 ERR_CH(UserProcess
, "Failed to allocate ppi for PID:0x%lx\n",
244 HandleToUlong(Process
->UniqueProcessId
));
249 DbgInitDebugChannels();
251 KdRosRegisterCliCallback(DbgGdiKdbgCliCallback
);
255 /* Map the global user heap into the process */
256 Status
= MapGlobalUserHeap(Process
, &KernelMapping
, &UserMapping
);
257 if (!NT_SUCCESS(Status
))
259 TRACE_CH(UserProcess
, "Failed to map the global heap! 0x%x\n", Status
);
263 TRACE_CH(UserProcess
, "InitProcessCallback -- We have KernelMapping 0x%p and UserMapping 0x%p with delta = 0x%x\n",
264 KernelMapping
, UserMapping
, (ULONG_PTR
)KernelMapping
- (ULONG_PTR
)UserMapping
);
266 /* Initialize USER process info */
267 Status
= UserProcessCreate(Process
);
268 if (!NT_SUCCESS(Status
))
270 ERR_CH(UserProcess
, "UserProcessCreate failed, Status 0x%08lx\n", Status
);
274 /* Initialize GDI process info */
275 Status
= GdiProcessCreate(Process
);
276 if (!NT_SUCCESS(Status
))
278 ERR_CH(UserProcess
, "GdiProcessCreate failed, Status 0x%08lx\n", Status
);
282 /* Add the process to the global list */
283 ppiCurrent
->ppiNext
= gppiList
;
284 gppiList
= ppiCurrent
;
286 return STATUS_SUCCESS
;
289 ERR_CH(UserProcess
, "InitProcessCallback failed! Freeing ppi 0x%p for PID:0x%lx\n",
290 ppiCurrent
, HandleToUlong(Process
->UniqueProcessId
));
291 ExitProcessCallback(Process
);
296 ExitProcessCallback(PEPROCESS Process
)
298 PPROCESSINFO ppiCurrent
, *pppi
;
300 /* Get the Win32 Process */
301 ppiCurrent
= PsGetProcessWin32Process(Process
);
303 ASSERT(ppiCurrent
->peProcess
== Process
);
305 TRACE_CH(UserProcess
, "Destroying ppi 0x%p\n", ppiCurrent
);
306 ppiCurrent
->W32PF_flags
|= W32PF_TERMINATED
;
308 /* Remove it from the list */
310 while (*pppi
!= NULL
&& *pppi
!= ppiCurrent
)
312 pppi
= &(*pppi
)->ppiNext
;
314 ASSERT(*pppi
== ppiCurrent
);
315 *pppi
= ppiCurrent
->ppiNext
;
317 /* Cleanup GDI info */
318 GdiProcessDestroy(Process
);
320 /* Cleanup USER info */
321 UserProcessDestroy(Process
);
323 /* The process is dying */
324 PsSetProcessWin32Process(Process
, NULL
, ppiCurrent
);
325 ppiCurrent
->peProcess
= NULL
;
327 /* Finally, dereference */
328 IntDereferenceProcessInfo(ppiCurrent
);
330 return STATUS_SUCCESS
;
335 Win32kProcessCallback(PEPROCESS Process
,
340 ASSERT(Process
->Peb
);
342 TRACE_CH(UserProcess
, "Win32kProcessCallback -->\n");
344 UserEnterExclusive();
348 Status
= InitProcessCallback(Process
);
352 Status
= ExitProcessCallback(Process
);
357 TRACE_CH(UserProcess
, "<-- Win32kProcessCallback\n");
365 AllocW32Thread(IN PETHREAD Thread
,
366 OUT PTHREADINFO
* W32Thread
)
368 PTHREADINFO ptiCurrent
;
370 TRACE_CH(UserThread
, "In AllocW32Thread(0x%p)\n", Thread
);
372 /* Check that we were not called with an already existing Win32 thread info */
373 ptiCurrent
= PsGetThreadWin32Thread(Thread
);
374 NT_ASSERT(ptiCurrent
== NULL
);
376 /* Allocate a new Win32 thread info */
377 ptiCurrent
= ExAllocatePoolWithTag(NonPagedPool
,
380 if (ptiCurrent
== NULL
)
382 ERR_CH(UserThread
, "Failed to allocate pti for TID:0x%lx\n",
383 HandleToUlong(Thread
->Cid
.UniqueThread
));
384 return STATUS_NO_MEMORY
;
387 TRACE_CH(UserThread
, "Allocated pti 0x%p for TID:0x%lx\n",
388 ptiCurrent
, HandleToUlong(Thread
->Cid
.UniqueThread
));
390 RtlZeroMemory(ptiCurrent
, sizeof(*ptiCurrent
));
392 PsSetThreadWin32Thread(Thread
, ptiCurrent
, NULL
);
393 ObReferenceObject(Thread
);
394 IntReferenceThreadInfo(ptiCurrent
);
396 *W32Thread
= ptiCurrent
;
397 return STATUS_SUCCESS
;
401 * Called from IntDereferenceThreadInfo
404 UserDeleteW32Thread(PTHREADINFO pti
)
406 PPROCESSINFO ppi
= pti
->ppi
;
408 TRACE_CH(UserThread
, "UserDeleteW32Thread pti 0x%p\n",pti
);
410 /* Free the message queue */
411 if (pti
->MessageQueue
)
413 MsqDestroyMessageQueue(pti
);
416 MsqCleanupThreadMsgs(pti
);
418 ObDereferenceObject(pti
->pEThread
);
420 ExFreePoolWithTag(pti
, USERTAG_THREADINFO
);
422 IntDereferenceProcessInfo(ppi
);
426 UserThreadCreate(PETHREAD Thread
)
428 return STATUS_SUCCESS
;
432 UserThreadDestroy(PETHREAD Thread
)
434 return STATUS_SUCCESS
;
438 InitThreadCallback(PETHREAD Thread
)
442 PTHREADINFO ptiCurrent
;
444 NTSTATUS Status
= STATUS_SUCCESS
;
446 LARGE_INTEGER LargeTickCount
;
447 PRTL_USER_PROCESS_PARAMETERS ProcessParams
;
449 Process
= Thread
->ThreadsProcess
;
451 pTeb
= NtCurrentTeb();
454 ProcessParams
= pTeb
->ProcessEnvironmentBlock
->ProcessParameters
;
456 /* Allocate a new Win32 thread info */
457 Status
= AllocW32Thread(Thread
, &ptiCurrent
);
458 if (!NT_SUCCESS(Status
))
460 ERR_CH(UserThread
, "Failed to allocate pti for TID:0x%lx\n",
461 HandleToUlong(Thread
->Cid
.UniqueThread
));
465 /* Initialize the THREADINFO */
466 ptiCurrent
->pEThread
= Thread
;
467 ptiCurrent
->ppi
= PsGetProcessWin32Process(Process
);
468 IntReferenceProcessInfo(ptiCurrent
->ppi
);
469 pTeb
->Win32ThreadInfo
= ptiCurrent
;
470 ptiCurrent
->pClientInfo
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
472 /* Mark the process as having threads */
473 ptiCurrent
->ppi
->W32PF_flags
|= W32PF_THREADCONNECTED
;
475 InitializeListHead(&ptiCurrent
->WindowListHead
);
476 InitializeListHead(&ptiCurrent
->W32CallbackListHead
);
477 InitializeListHead(&ptiCurrent
->PostedMessagesListHead
);
478 InitializeListHead(&ptiCurrent
->SentMessagesListHead
);
479 InitializeListHead(&ptiCurrent
->PtiLink
);
480 for (i
= 0; i
< NB_HOOKS
; i
++)
482 InitializeListHead(&ptiCurrent
->aphkStart
[i
]);
484 ptiCurrent
->ptiSibling
= ptiCurrent
->ppi
->ptiList
;
485 ptiCurrent
->ppi
->ptiList
= ptiCurrent
;
486 ptiCurrent
->ppi
->cThreads
++;
488 ptiCurrent
->hEventQueueClient
= NULL
;
489 Status
= ZwCreateEvent(&ptiCurrent
->hEventQueueClient
, EVENT_ALL_ACCESS
,
490 NULL
, SynchronizationEvent
, FALSE
);
491 if (!NT_SUCCESS(Status
))
493 ERR_CH(UserThread
, "Event creation failed, Status 0x%08x.\n", Status
);
496 Status
= ObReferenceObjectByHandle(ptiCurrent
->hEventQueueClient
, 0,
497 *ExEventObjectType
, UserMode
,
498 (PVOID
*)&ptiCurrent
->pEventQueueServer
, NULL
);
499 if (!NT_SUCCESS(Status
))
501 ERR_CH(UserThread
, "Failed referencing the event object, Status 0x%08x.\n", Status
);
502 ObCloseHandle(ptiCurrent
->hEventQueueClient
, UserMode
);
503 ptiCurrent
->hEventQueueClient
= NULL
;
507 KeQueryTickCount(&LargeTickCount
);
508 ptiCurrent
->timeLast
= LargeTickCount
.u
.LowPart
;
510 ptiCurrent
->MessageQueue
= MsqCreateMessageQueue(ptiCurrent
);
511 if (ptiCurrent
->MessageQueue
== NULL
)
513 ERR_CH(UserThread
, "Failed to allocate message loop\n");
514 Status
= STATUS_NO_MEMORY
;
518 ptiCurrent
->KeyboardLayout
= W32kGetDefaultKeyLayout();
519 if (ptiCurrent
->KeyboardLayout
)
520 UserReferenceObject(ptiCurrent
->KeyboardLayout
);
522 ptiCurrent
->TIF_flags
&= ~TIF_INCLEANUP
;
524 // FIXME: Flag SYSTEM threads with... TIF_SYSTEMTHREAD !!
526 /* CSRSS threads have some special features */
527 if (Process
== gpepCSRSS
|| !gpepCSRSS
)
528 ptiCurrent
->TIF_flags
= TIF_CSRSSTHREAD
| TIF_DONTATTACHQUEUE
;
530 ptiCurrent
->pcti
= &ptiCurrent
->cti
;
532 /* Initialize the CLIENTINFO */
533 pci
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
534 RtlZeroMemory(pci
, sizeof(*pci
));
535 pci
->ppi
= ptiCurrent
->ppi
;
536 pci
->fsHooks
= ptiCurrent
->fsHooks
;
537 pci
->dwTIFlags
= ptiCurrent
->TIF_flags
;
538 if (ptiCurrent
->KeyboardLayout
)
540 pci
->hKL
= ptiCurrent
->KeyboardLayout
->hkl
;
541 pci
->CodePage
= ptiCurrent
->KeyboardLayout
->CodePage
;
544 /* Need to pass the user Startup Information to the current process. */
547 if ( ptiCurrent
->ppi
->usi
.cb
== 0 ) // Not initialized yet.
549 if ( ProcessParams
->WindowFlags
!= 0 ) // Need window flags set.
551 ptiCurrent
->ppi
->usi
.cb
= sizeof(USERSTARTUPINFO
);
552 ptiCurrent
->ppi
->usi
.dwX
= ProcessParams
->StartingX
;
553 ptiCurrent
->ppi
->usi
.dwY
= ProcessParams
->StartingY
;
554 ptiCurrent
->ppi
->usi
.dwXSize
= ProcessParams
->CountX
;
555 ptiCurrent
->ppi
->usi
.dwYSize
= ProcessParams
->CountY
;
556 ptiCurrent
->ppi
->usi
.dwFlags
= ProcessParams
->WindowFlags
;
557 ptiCurrent
->ppi
->usi
.wShowWindow
= (WORD
)ProcessParams
->ShowWindowFlags
;
563 * Assign a default window station and desktop to the process.
564 * Do not try to open a desktop or window station before the very first
565 * (interactive) window station has been created by Winlogon.
567 if (!(ptiCurrent
->TIF_flags
& (TIF_SYSTEMTHREAD
| TIF_CSRSSTHREAD
)) &&
568 ptiCurrent
->ppi
->hdeskStartup
== NULL
&&
569 InputWindowStation
!= NULL
)
571 HWINSTA hWinSta
= NULL
;
573 UNICODE_STRING DesktopPath
;
577 * Inherit the thread desktop and process window station (if not yet inherited)
578 * from the process startup info structure. See documentation of CreateProcess().
580 Status
= STATUS_UNSUCCESSFUL
;
581 if (ProcessParams
&& ProcessParams
->DesktopInfo
.Length
> 0)
583 Status
= IntSafeCopyUnicodeStringTerminateNULL(&DesktopPath
, &ProcessParams
->DesktopInfo
);
585 if (!NT_SUCCESS(Status
))
587 RtlInitUnicodeString(&DesktopPath
, NULL
);
590 Status
= IntResolveDesktop(Process
,
592 !!(ProcessParams
->WindowFlags
& STARTF_INHERITDESKTOP
),
596 if (DesktopPath
.Buffer
)
597 ExFreePoolWithTag(DesktopPath
.Buffer
, TAG_STRING
);
599 if (!NT_SUCCESS(Status
))
601 ERR_CH(UserThread
, "Failed to assign default desktop and winsta to process\n");
605 if (!UserSetProcessWindowStation(hWinSta
))
607 Status
= STATUS_UNSUCCESSFUL
;
608 ERR_CH(UserThread
, "Failed to set initial process winsta\n");
612 /* Validate the new desktop */
613 Status
= IntValidateDesktopHandle(hDesk
, UserMode
, 0, &pdesk
);
614 if (!NT_SUCCESS(Status
))
616 ERR_CH(UserThread
, "Failed to validate initial desktop handle\n");
620 /* Store the parsed desktop as the initial desktop */
621 ASSERT(ptiCurrent
->ppi
->hdeskStartup
== NULL
);
622 ASSERT(Process
->UniqueProcessId
!= gpidLogon
);
623 ptiCurrent
->ppi
->hdeskStartup
= hDesk
;
624 ptiCurrent
->ppi
->rpdeskStartup
= pdesk
;
627 if (ptiCurrent
->ppi
->hdeskStartup
!= NULL
)
629 if (!IntSetThreadDesktop(ptiCurrent
->ppi
->hdeskStartup
, FALSE
))
631 ERR_CH(UserThread
, "Failed to set thread desktop\n");
632 Status
= STATUS_UNSUCCESSFUL
;
637 /* Mark the thread as fully initialized */
638 ptiCurrent
->TIF_flags
|= TIF_GUITHREADINITIALIZED
;
640 if (!(ptiCurrent
->ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_APPSTARTING
)) &&
641 (gptiForeground
&& gptiForeground
->ppi
== ptiCurrent
->ppi
))
643 ptiCurrent
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
645 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
647 /* Last things to do only if we are not a SYSTEM or CSRSS thread */
648 if (!(ptiCurrent
->TIF_flags
& (TIF_SYSTEMTHREAD
| TIF_CSRSSTHREAD
)))
650 /* Callback to User32 Client Thread Setup */
651 TRACE_CH(UserThread
, "Call co_IntClientThreadSetup...\n");
652 Status
= co_IntClientThreadSetup();
653 if (!NT_SUCCESS(Status
))
655 ERR_CH(UserThread
, "ClientThreadSetup failed with Status 0x%08lx\n", Status
);
658 TRACE_CH(UserThread
, "co_IntClientThreadSetup succeeded!\n");
662 TRACE_CH(UserThread
, "co_IntClientThreadSetup cannot be called...\n");
665 TRACE_CH(UserThread
, "UserCreateW32Thread pti 0x%p\n", ptiCurrent
);
666 return STATUS_SUCCESS
;
669 ERR_CH(UserThread
, "InitThreadCallback failed! Freeing pti 0x%p for TID:0x%lx\n",
670 ptiCurrent
, HandleToUlong(Thread
->Cid
.UniqueThread
));
671 ExitThreadCallback(Thread
);
676 UserDisplayNotifyShutdown(PPROCESSINFO ppiCurrent
);
680 ExitThreadCallback(PETHREAD Thread
)
683 PSINGLE_LIST_ENTRY psle
;
684 PPROCESSINFO ppiCurrent
;
686 PTHREADINFO ptiCurrent
;
688 Process
= Thread
->ThreadsProcess
;
690 /* Get the Win32 Thread */
691 ptiCurrent
= PsGetThreadWin32Thread(Thread
);
694 TRACE_CH(UserThread
, "Destroying pti 0x%p eThread 0x%p\n", ptiCurrent
, Thread
);
696 ptiCurrent
->TIF_flags
|= TIF_INCLEANUP
;
697 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
699 ppiCurrent
= ptiCurrent
->ppi
;
702 IsRemoveAttachThread(ptiCurrent
);
704 ptiCurrent
->TIF_flags
|= TIF_DONTATTACHQUEUE
;
705 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
707 UserCloseClipboard();
709 /* Decrement thread count and check if its 0 */
710 ppiCurrent
->cThreads
--;
712 if (ptiCurrent
->TIF_flags
& TIF_GUITHREADINITIALIZED
)
714 /* Do now some process cleanup that requires a valid win32 thread */
715 if (ptiCurrent
->ppi
->cThreads
== 0)
717 /* Check if we have registered the user api hook */
718 if (ptiCurrent
->ppi
== ppiUahServer
)
720 /* Unregister the api hook */
721 UserUnregisterUserApiHook();
724 /* Notify logon application to restart shell if needed */
725 if (ptiCurrent
->pDeskInfo
)
727 if (ptiCurrent
->pDeskInfo
->ppiShellProcess
== ppiCurrent
)
729 DWORD ExitCode
= PsGetProcessExitStatus(Process
);
731 TRACE_CH(UserProcess
, "Shell process is exiting (%lu)\n", ExitCode
);
733 UserPostMessage(hwndSAS
,
738 ptiCurrent
->pDeskInfo
->ppiShellProcess
= NULL
;
743 DceFreeThreadDCE(ptiCurrent
);
744 DestroyTimersForThread(ptiCurrent
);
745 KeSetEvent(ptiCurrent
->pEventQueueServer
, IO_NO_INCREMENT
, FALSE
);
746 UnregisterThreadHotKeys(ptiCurrent
);
748 if (!UserDestroyObjectsForOwner(gHandleTable
, ptiCurrent
))
750 DPRINT1("Failed to delete objects belonging to thread %p. This is VERY BAD!.\n", ptiCurrent
);
752 return STATUS_UNSUCCESSFUL
;
755 if (ppiCurrent
&& ppiCurrent
->ptiList
== ptiCurrent
&& !ptiCurrent
->ptiSibling
&&
756 ppiCurrent
->W32PF_flags
& W32PF_CLASSESREGISTERED
)
758 TRACE_CH(UserThread
, "DestroyProcessClasses\n");
759 /* no process windows should exist at this point, or the function will assert! */
760 DestroyProcessClasses(ppiCurrent
);
761 ppiCurrent
->W32PF_flags
&= ~W32PF_CLASSESREGISTERED
;
764 IntBlockInput(ptiCurrent
, FALSE
);
765 IntCleanupThreadCallbacks(ptiCurrent
);
767 /* cleanup user object references stack */
768 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
771 PUSER_REFERENCE_ENTRY ref
= CONTAINING_RECORD(psle
, USER_REFERENCE_ENTRY
, Entry
);
772 TRACE_CH(UserThread
, "thread clean: remove reference obj 0x%p\n",ref
->obj
);
773 UserDereferenceObject(ref
->obj
);
775 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
779 if (ptiCurrent
->cEnterCount
)
781 KeSetKernelStackSwapEnable(TRUE
);
782 ptiCurrent
->cEnterCount
= 0;
785 /* Find the THREADINFO in the PROCESSINFO's list */
786 ppti
= &ppiCurrent
->ptiList
;
787 while (*ppti
!= NULL
&& *ppti
!= ptiCurrent
)
789 ppti
= &((*ppti
)->ptiSibling
);
792 /* we must have found it */
793 ASSERT(*ppti
== ptiCurrent
);
795 /* Remove it from the list */
796 *ppti
= ptiCurrent
->ptiSibling
;
798 if (ptiCurrent
->KeyboardLayout
)
799 UserDereferenceObject(ptiCurrent
->KeyboardLayout
);
801 if (gptiForeground
== ptiCurrent
)
803 // IntNotifyWinEvent(EVENT_OBJECT_FOCUS, NULL, OBJID_CLIENT, CHILDID_SELF, 0);
804 // IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND, NULL, OBJID_WINDOW, CHILDID_SELF, 0);
806 gptiForeground
= NULL
;
809 /* Restore display mode when we are the last thread, and we changed the display mode */
810 if (ppiCurrent
->cThreads
== 0)
811 UserDisplayNotifyShutdown(ppiCurrent
);
814 // Fixes CORE-6384 & CORE-7030.
815 /* if (ptiLastInput == ptiCurrent)
817 if (!ppiCurrent->ptiList)
818 ptiLastInput = gptiForeground;
820 ptiLastInput = ppiCurrent->ptiList;
821 ERR_CH(UserThread, "DTI: ptiLastInput is Cleared!!\n");
824 TRACE_CH(UserThread
, "Freeing pti 0x%p\n", ptiCurrent
);
826 IntSetThreadDesktop(NULL
, TRUE
);
828 if (ptiCurrent
->hEventQueueClient
!= NULL
)
830 ObCloseHandle(ptiCurrent
->hEventQueueClient
, UserMode
);
831 ObDereferenceObject(ptiCurrent
->pEventQueueServer
);
833 ptiCurrent
->hEventQueueClient
= NULL
;
835 /* The thread is dying */
836 PsSetThreadWin32Thread(Thread
/*ptiCurrent->pEThread*/, NULL
, ptiCurrent
);
838 /* Dereference the THREADINFO */
839 IntDereferenceThreadInfo(ptiCurrent
);
841 return STATUS_SUCCESS
;
846 Win32kThreadCallback(PETHREAD Thread
,
847 PSW32THREADCALLOUTTYPE Type
)
851 ASSERT(NtCurrentTeb());
853 UserEnterExclusive();
855 if (Type
== PsW32ThreadCalloutInitialize
)
857 ASSERT(PsGetThreadWin32Thread(Thread
) == NULL
);
858 Status
= InitThreadCallback(Thread
);
860 else // if (Type == PsW32ThreadCalloutExit)
862 ASSERT(PsGetThreadWin32Thread(Thread
) != NULL
);
863 Status
= ExitThreadCallback(Thread
);
871 _Function_class_(DRIVER_UNLOAD
)
873 DriverUnload(IN PDRIVER_OBJECT DriverObject
)
875 // TODO: Do more cleanup!
885 if (!NT_SUCCESS(Status)) \
887 DPRINT1("Failed '%s' (0x%lx)\n", #x, Status); \
893 * This definition doesn't work
899 IN PDRIVER_OBJECT DriverObject
,
900 IN PUNICODE_STRING RegistryPath
)
904 WIN32_CALLOUTS_FPNS CalloutData
= {0};
905 PVOID GlobalUserHeapBase
= NULL
;
908 * Register user mode call interface
909 * (system service table index = 1)
911 Result
= KeAddSystemServiceTable(Win32kSSDT
,
913 Win32kNumberOfSysCalls
,
918 DPRINT1("Adding system services failed!\n");
919 return STATUS_UNSUCCESSFUL
;
922 hModuleWin
= MmPageEntireDriver(DriverEntry
);
923 DPRINT("Win32k hInstance 0x%p!\n", hModuleWin
);
925 DriverObject
->DriverUnload
= DriverUnload
;
927 /* Register Object Manager Callbacks */
928 CalloutData
.ProcessCallout
= Win32kProcessCallback
;
929 CalloutData
.ThreadCallout
= Win32kThreadCallback
;
930 // CalloutData.GlobalAtomTableCallout = NULL;
931 // CalloutData.PowerEventCallout = NULL;
932 // CalloutData.PowerStateCallout = NULL;
933 // CalloutData.JobCallout = NULL;
934 CalloutData
.BatchFlushRoutine
= NtGdiFlushUserBatch
;
935 CalloutData
.DesktopOpenProcedure
= IntDesktopObjectOpen
;
936 CalloutData
.DesktopOkToCloseProcedure
= IntDesktopOkToClose
;
937 CalloutData
.DesktopCloseProcedure
= IntDesktopObjectClose
;
938 CalloutData
.DesktopDeleteProcedure
= IntDesktopObjectDelete
;
939 CalloutData
.WindowStationOkToCloseProcedure
= IntWinStaOkToClose
;
940 // CalloutData.WindowStationCloseProcedure = NULL;
941 CalloutData
.WindowStationDeleteProcedure
= IntWinStaObjectDelete
;
942 CalloutData
.WindowStationParseProcedure
= IntWinStaObjectParse
;
943 // CalloutData.WindowStationOpenProcedure = NULL;
945 /* Register our per-process and per-thread structures. */
946 PsEstablishWin32Callouts(&CalloutData
);
948 /* Register service hook callbacks */
949 #if DBG && defined(KDBG)
950 KdSystemDebugControl('CsoR', DbgPreServiceHook
, ID_Win32PreServiceHook
, 0, 0, 0, 0);
951 KdSystemDebugControl('CsoR', DbgPostServiceHook
, ID_Win32PostServiceHook
, 0, 0, 0, 0);
954 /* Create the global USER heap */
955 GlobalUserHeap
= UserCreateHeap(&GlobalUserHeapSection
,
957 1 * 1024 * 1024); /* FIXME: 1 MB for now... */
958 if (GlobalUserHeap
== NULL
)
960 DPRINT1("Failed to initialize the global heap!\n");
961 return STATUS_UNSUCCESSFUL
;
964 /* Allocate global server info structure */
965 gpsi
= UserHeapAlloc(sizeof(*gpsi
));
968 DPRINT1("Failed allocate server info structure!\n");
969 return STATUS_UNSUCCESSFUL
;
972 RtlZeroMemory(gpsi
, sizeof(*gpsi
));
973 DPRINT("Global Server Data -> %p\n", gpsi
);
975 NT_ROF(InitGdiHandleTable());
976 NT_ROF(InitPaletteImpl());
978 /* Create stock objects, ie. precreated objects commonly
979 used by win32 applications */
980 CreateStockObjects();
981 CreateSysColorObjects();
983 NT_ROF(InitBrushImpl());
984 NT_ROF(InitPDEVImpl());
985 NT_ROF(InitLDEVImpl());
986 NT_ROF(InitDeviceImpl());
987 NT_ROF(InitDcImpl());
988 NT_ROF(InitUserImpl());
989 NT_ROF(InitWindowStationImpl());
990 NT_ROF(InitDesktopImpl());
991 NT_ROF(InitInputImpl());
992 NT_ROF(InitKeyboardImpl());
993 NT_ROF(MsqInitializeImpl());
994 NT_ROF(InitTimerImpl());
995 NT_ROF(InitDCEImpl());
997 gusLanguageID
= UserGetLanguageID();
999 /* Initialize FreeType library */
1000 if (!InitFontSupport())
1002 DPRINT1("Unable to initialize font support\n");
1006 return STATUS_SUCCESS
;