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 PRTL_USER_PROCESS_PARAMETERS ProcessParams
;
448 Process
= Thread
->ThreadsProcess
;
450 pTeb
= NtCurrentTeb();
453 ProcessParams
= pTeb
->ProcessEnvironmentBlock
->ProcessParameters
;
455 /* Allocate a new Win32 thread info */
456 Status
= AllocW32Thread(Thread
, &ptiCurrent
);
457 if (!NT_SUCCESS(Status
))
459 ERR_CH(UserThread
, "Failed to allocate pti for TID:0x%lx\n",
460 HandleToUlong(Thread
->Cid
.UniqueThread
));
464 /* Initialize the THREADINFO */
465 ptiCurrent
->pEThread
= Thread
;
466 ptiCurrent
->ppi
= PsGetProcessWin32Process(Process
);
467 IntReferenceProcessInfo(ptiCurrent
->ppi
);
468 pTeb
->Win32ThreadInfo
= ptiCurrent
;
469 ptiCurrent
->pClientInfo
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
471 /* Mark the process as having threads */
472 ptiCurrent
->ppi
->W32PF_flags
|= W32PF_THREADCONNECTED
;
474 InitializeListHead(&ptiCurrent
->WindowListHead
);
475 InitializeListHead(&ptiCurrent
->W32CallbackListHead
);
476 InitializeListHead(&ptiCurrent
->PostedMessagesListHead
);
477 InitializeListHead(&ptiCurrent
->SentMessagesListHead
);
478 InitializeListHead(&ptiCurrent
->PtiLink
);
479 for (i
= 0; i
< NB_HOOKS
; i
++)
481 InitializeListHead(&ptiCurrent
->aphkStart
[i
]);
483 ptiCurrent
->ptiSibling
= ptiCurrent
->ppi
->ptiList
;
484 ptiCurrent
->ppi
->ptiList
= ptiCurrent
;
485 ptiCurrent
->ppi
->cThreads
++;
487 ptiCurrent
->hEventQueueClient
= NULL
;
488 Status
= ZwCreateEvent(&ptiCurrent
->hEventQueueClient
, EVENT_ALL_ACCESS
,
489 NULL
, SynchronizationEvent
, FALSE
);
490 if (!NT_SUCCESS(Status
))
492 ERR_CH(UserThread
, "Event creation failed, Status 0x%08x.\n", Status
);
495 Status
= ObReferenceObjectByHandle(ptiCurrent
->hEventQueueClient
, 0,
496 *ExEventObjectType
, UserMode
,
497 (PVOID
*)&ptiCurrent
->pEventQueueServer
, NULL
);
498 if (!NT_SUCCESS(Status
))
500 ERR_CH(UserThread
, "Failed referencing the event object, Status 0x%08x.\n", Status
);
501 ObCloseHandle(ptiCurrent
->hEventQueueClient
, UserMode
);
502 ptiCurrent
->hEventQueueClient
= NULL
;
506 ptiCurrent
->timeLast
= EngGetTickCount32();
507 ptiCurrent
->MessageQueue
= MsqCreateMessageQueue(ptiCurrent
);
508 if (ptiCurrent
->MessageQueue
== NULL
)
510 ERR_CH(UserThread
, "Failed to allocate message loop\n");
511 Status
= STATUS_NO_MEMORY
;
515 ptiCurrent
->KeyboardLayout
= W32kGetDefaultKeyLayout();
516 if (ptiCurrent
->KeyboardLayout
)
517 UserReferenceObject(ptiCurrent
->KeyboardLayout
);
519 ptiCurrent
->TIF_flags
&= ~TIF_INCLEANUP
;
521 // FIXME: Flag SYSTEM threads with... TIF_SYSTEMTHREAD !!
523 /* CSRSS threads have some special features */
524 if (Process
== gpepCSRSS
|| !gpepCSRSS
)
525 ptiCurrent
->TIF_flags
= TIF_CSRSSTHREAD
| TIF_DONTATTACHQUEUE
;
527 ptiCurrent
->pcti
= &ptiCurrent
->cti
;
529 /* Initialize the CLIENTINFO */
530 pci
= (PCLIENTINFO
)pTeb
->Win32ClientInfo
;
531 RtlZeroMemory(pci
, sizeof(*pci
));
532 pci
->ppi
= ptiCurrent
->ppi
;
533 pci
->fsHooks
= ptiCurrent
->fsHooks
;
534 pci
->dwTIFlags
= ptiCurrent
->TIF_flags
;
535 if (ptiCurrent
->KeyboardLayout
)
537 pci
->hKL
= ptiCurrent
->KeyboardLayout
->hkl
;
538 pci
->CodePage
= ptiCurrent
->KeyboardLayout
->CodePage
;
541 /* Need to pass the user Startup Information to the current process. */
544 if ( ptiCurrent
->ppi
->usi
.cb
== 0 ) // Not initialized yet.
546 if ( ProcessParams
->WindowFlags
!= 0 ) // Need window flags set.
548 ptiCurrent
->ppi
->usi
.cb
= sizeof(USERSTARTUPINFO
);
549 ptiCurrent
->ppi
->usi
.dwX
= ProcessParams
->StartingX
;
550 ptiCurrent
->ppi
->usi
.dwY
= ProcessParams
->StartingY
;
551 ptiCurrent
->ppi
->usi
.dwXSize
= ProcessParams
->CountX
;
552 ptiCurrent
->ppi
->usi
.dwYSize
= ProcessParams
->CountY
;
553 ptiCurrent
->ppi
->usi
.dwFlags
= ProcessParams
->WindowFlags
;
554 ptiCurrent
->ppi
->usi
.wShowWindow
= (WORD
)ProcessParams
->ShowWindowFlags
;
560 * Assign a default window station and desktop to the process.
561 * Do not try to open a desktop or window station before the very first
562 * (interactive) window station has been created by Winlogon.
564 if (!(ptiCurrent
->TIF_flags
& (TIF_SYSTEMTHREAD
| TIF_CSRSSTHREAD
)) &&
565 ptiCurrent
->ppi
->hdeskStartup
== NULL
&&
566 InputWindowStation
!= NULL
)
568 HWINSTA hWinSta
= NULL
;
570 UNICODE_STRING DesktopPath
;
574 * Inherit the thread desktop and process window station (if not yet inherited)
575 * from the process startup info structure. See documentation of CreateProcess().
577 Status
= STATUS_UNSUCCESSFUL
;
578 if (ProcessParams
&& ProcessParams
->DesktopInfo
.Length
> 0)
580 Status
= IntSafeCopyUnicodeStringTerminateNULL(&DesktopPath
, &ProcessParams
->DesktopInfo
);
582 if (!NT_SUCCESS(Status
))
584 RtlInitUnicodeString(&DesktopPath
, NULL
);
587 Status
= IntResolveDesktop(Process
,
589 !!(ProcessParams
->WindowFlags
& STARTF_INHERITDESKTOP
),
593 if (DesktopPath
.Buffer
)
594 ExFreePoolWithTag(DesktopPath
.Buffer
, TAG_STRING
);
596 if (!NT_SUCCESS(Status
))
598 ERR_CH(UserThread
, "Failed to assign default desktop and winsta to process\n");
602 if (!UserSetProcessWindowStation(hWinSta
))
604 Status
= STATUS_UNSUCCESSFUL
;
605 ERR_CH(UserThread
, "Failed to set initial process winsta\n");
609 /* Validate the new desktop */
610 Status
= IntValidateDesktopHandle(hDesk
, UserMode
, 0, &pdesk
);
611 if (!NT_SUCCESS(Status
))
613 ERR_CH(UserThread
, "Failed to validate initial desktop handle\n");
617 /* Store the parsed desktop as the initial desktop */
618 ASSERT(ptiCurrent
->ppi
->hdeskStartup
== NULL
);
619 ASSERT(Process
->UniqueProcessId
!= gpidLogon
);
620 ptiCurrent
->ppi
->hdeskStartup
= hDesk
;
621 ptiCurrent
->ppi
->rpdeskStartup
= pdesk
;
624 if (ptiCurrent
->ppi
->hdeskStartup
!= NULL
)
626 if (!IntSetThreadDesktop(ptiCurrent
->ppi
->hdeskStartup
, FALSE
))
628 ERR_CH(UserThread
, "Failed to set thread desktop\n");
629 Status
= STATUS_UNSUCCESSFUL
;
634 /* Mark the thread as fully initialized */
635 ptiCurrent
->TIF_flags
|= TIF_GUITHREADINITIALIZED
;
637 if (!(ptiCurrent
->ppi
->W32PF_flags
& (W32PF_ALLOWFOREGROUNDACTIVATE
| W32PF_APPSTARTING
)) &&
638 (gptiForeground
&& gptiForeground
->ppi
== ptiCurrent
->ppi
))
640 ptiCurrent
->TIF_flags
|= TIF_ALLOWFOREGROUNDACTIVATE
;
642 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
644 /* Last things to do only if we are not a SYSTEM or CSRSS thread */
645 if (!(ptiCurrent
->TIF_flags
& (TIF_SYSTEMTHREAD
| TIF_CSRSSTHREAD
)))
647 /* Callback to User32 Client Thread Setup */
648 TRACE_CH(UserThread
, "Call co_IntClientThreadSetup...\n");
649 Status
= co_IntClientThreadSetup();
650 if (!NT_SUCCESS(Status
))
652 ERR_CH(UserThread
, "ClientThreadSetup failed with Status 0x%08lx\n", Status
);
655 TRACE_CH(UserThread
, "co_IntClientThreadSetup succeeded!\n");
659 TRACE_CH(UserThread
, "co_IntClientThreadSetup cannot be called...\n");
662 TRACE_CH(UserThread
, "UserCreateW32Thread pti 0x%p\n", ptiCurrent
);
663 return STATUS_SUCCESS
;
666 ERR_CH(UserThread
, "InitThreadCallback failed! Freeing pti 0x%p for TID:0x%lx\n",
667 ptiCurrent
, HandleToUlong(Thread
->Cid
.UniqueThread
));
668 ExitThreadCallback(Thread
);
673 UserDisplayNotifyShutdown(PPROCESSINFO ppiCurrent
);
677 ExitThreadCallback(PETHREAD Thread
)
680 PSINGLE_LIST_ENTRY psle
;
681 PPROCESSINFO ppiCurrent
;
683 PTHREADINFO ptiCurrent
;
685 Process
= Thread
->ThreadsProcess
;
687 /* Get the Win32 Thread */
688 ptiCurrent
= PsGetThreadWin32Thread(Thread
);
691 TRACE_CH(UserThread
, "Destroying pti 0x%p eThread 0x%p\n", ptiCurrent
, Thread
);
693 ptiCurrent
->TIF_flags
|= TIF_INCLEANUP
;
694 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
696 ppiCurrent
= ptiCurrent
->ppi
;
699 IsRemoveAttachThread(ptiCurrent
);
701 ptiCurrent
->TIF_flags
|= TIF_DONTATTACHQUEUE
;
702 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
704 UserCloseClipboard();
706 /* Decrement thread count and check if its 0 */
707 ppiCurrent
->cThreads
--;
709 if (ptiCurrent
->TIF_flags
& TIF_GUITHREADINITIALIZED
)
711 /* Do now some process cleanup that requires a valid win32 thread */
712 if (ptiCurrent
->ppi
->cThreads
== 0)
714 /* Check if we have registered the user api hook */
715 if (ptiCurrent
->ppi
== ppiUahServer
)
717 /* Unregister the api hook */
718 UserUnregisterUserApiHook();
721 /* Notify logon application to restart shell if needed */
722 if (ptiCurrent
->pDeskInfo
)
724 if (ptiCurrent
->pDeskInfo
->ppiShellProcess
== ppiCurrent
)
726 DWORD ExitCode
= PsGetProcessExitStatus(Process
);
728 TRACE_CH(UserProcess
, "Shell process is exiting (%lu)\n", ExitCode
);
730 UserPostMessage(hwndSAS
,
735 ptiCurrent
->pDeskInfo
->ppiShellProcess
= NULL
;
740 DceFreeThreadDCE(ptiCurrent
);
741 DestroyTimersForThread(ptiCurrent
);
742 KeSetEvent(ptiCurrent
->pEventQueueServer
, IO_NO_INCREMENT
, FALSE
);
743 UnregisterThreadHotKeys(ptiCurrent
);
745 if (!UserDestroyObjectsForOwner(gHandleTable
, ptiCurrent
))
747 DPRINT1("Failed to delete objects belonging to thread %p. This is VERY BAD!.\n", ptiCurrent
);
749 return STATUS_UNSUCCESSFUL
;
752 if (ppiCurrent
&& ppiCurrent
->ptiList
== ptiCurrent
&& !ptiCurrent
->ptiSibling
&&
753 ppiCurrent
->W32PF_flags
& W32PF_CLASSESREGISTERED
)
755 TRACE_CH(UserThread
, "DestroyProcessClasses\n");
756 /* no process windows should exist at this point, or the function will assert! */
757 DestroyProcessClasses(ppiCurrent
);
758 ppiCurrent
->W32PF_flags
&= ~W32PF_CLASSESREGISTERED
;
761 IntBlockInput(ptiCurrent
, FALSE
);
762 IntCleanupThreadCallbacks(ptiCurrent
);
764 /* cleanup user object references stack */
765 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
768 PUSER_REFERENCE_ENTRY ref
= CONTAINING_RECORD(psle
, USER_REFERENCE_ENTRY
, Entry
);
769 TRACE_CH(UserThread
, "thread clean: remove reference obj 0x%p\n",ref
->obj
);
770 UserDereferenceObject(ref
->obj
);
772 psle
= PopEntryList(&ptiCurrent
->ReferencesList
);
776 if (ptiCurrent
->cEnterCount
)
778 KeSetKernelStackSwapEnable(TRUE
);
779 ptiCurrent
->cEnterCount
= 0;
782 /* Find the THREADINFO in the PROCESSINFO's list */
783 ppti
= &ppiCurrent
->ptiList
;
784 while (*ppti
!= NULL
&& *ppti
!= ptiCurrent
)
786 ppti
= &((*ppti
)->ptiSibling
);
789 /* we must have found it */
790 ASSERT(*ppti
== ptiCurrent
);
792 /* Remove it from the list */
793 *ppti
= ptiCurrent
->ptiSibling
;
795 if (ptiCurrent
->KeyboardLayout
)
796 UserDereferenceObject(ptiCurrent
->KeyboardLayout
);
798 if (gptiForeground
== ptiCurrent
)
800 // IntNotifyWinEvent(EVENT_OBJECT_FOCUS, NULL, OBJID_CLIENT, CHILDID_SELF, 0);
801 // IntNotifyWinEvent(EVENT_SYSTEM_FOREGROUND, NULL, OBJID_WINDOW, CHILDID_SELF, 0);
803 gptiForeground
= NULL
;
806 /* Restore display mode when we are the last thread, and we changed the display mode */
807 if (ppiCurrent
->cThreads
== 0)
808 UserDisplayNotifyShutdown(ppiCurrent
);
811 // Fixes CORE-6384 & CORE-7030.
812 /* if (ptiLastInput == ptiCurrent)
814 if (!ppiCurrent->ptiList)
815 ptiLastInput = gptiForeground;
817 ptiLastInput = ppiCurrent->ptiList;
818 ERR_CH(UserThread, "DTI: ptiLastInput is Cleared!!\n");
821 TRACE_CH(UserThread
, "Freeing pti 0x%p\n", ptiCurrent
);
823 IntSetThreadDesktop(NULL
, TRUE
);
825 if (ptiCurrent
->hEventQueueClient
!= NULL
)
827 ObCloseHandle(ptiCurrent
->hEventQueueClient
, UserMode
);
828 ObDereferenceObject(ptiCurrent
->pEventQueueServer
);
830 ptiCurrent
->hEventQueueClient
= NULL
;
832 /* The thread is dying */
833 PsSetThreadWin32Thread(Thread
/*ptiCurrent->pEThread*/, NULL
, ptiCurrent
);
835 /* Dereference the THREADINFO */
836 IntDereferenceThreadInfo(ptiCurrent
);
838 return STATUS_SUCCESS
;
843 Win32kThreadCallback(PETHREAD Thread
,
844 PSW32THREADCALLOUTTYPE Type
)
848 ASSERT(NtCurrentTeb());
850 UserEnterExclusive();
852 if (Type
== PsW32ThreadCalloutInitialize
)
854 ASSERT(PsGetThreadWin32Thread(Thread
) == NULL
);
855 Status
= InitThreadCallback(Thread
);
857 else // if (Type == PsW32ThreadCalloutExit)
859 ASSERT(PsGetThreadWin32Thread(Thread
) != NULL
);
860 Status
= ExitThreadCallback(Thread
);
868 _Function_class_(DRIVER_UNLOAD
)
870 DriverUnload(IN PDRIVER_OBJECT DriverObject
)
872 // TODO: Do more cleanup!
882 if (!NT_SUCCESS(Status)) \
884 DPRINT1("Failed '%s' (0x%lx)\n", #x, Status); \
890 * This definition doesn't work
896 IN PDRIVER_OBJECT DriverObject
,
897 IN PUNICODE_STRING RegistryPath
)
901 WIN32_CALLOUTS_FPNS CalloutData
= {0};
902 PVOID GlobalUserHeapBase
= NULL
;
905 * Register user mode call interface
906 * (system service table index = 1)
908 Result
= KeAddSystemServiceTable(Win32kSSDT
,
910 Win32kNumberOfSysCalls
,
915 DPRINT1("Adding system services failed!\n");
916 return STATUS_UNSUCCESSFUL
;
919 hModuleWin
= MmPageEntireDriver(DriverEntry
);
920 DPRINT("Win32k hInstance 0x%p!\n", hModuleWin
);
922 DriverObject
->DriverUnload
= DriverUnload
;
924 /* Register Object Manager Callbacks */
925 CalloutData
.ProcessCallout
= Win32kProcessCallback
;
926 CalloutData
.ThreadCallout
= Win32kThreadCallback
;
927 // CalloutData.GlobalAtomTableCallout = NULL;
928 // CalloutData.PowerEventCallout = NULL;
929 // CalloutData.PowerStateCallout = NULL;
930 // CalloutData.JobCallout = NULL;
931 CalloutData
.BatchFlushRoutine
= NtGdiFlushUserBatch
;
932 CalloutData
.DesktopOpenProcedure
= IntDesktopObjectOpen
;
933 CalloutData
.DesktopOkToCloseProcedure
= IntDesktopOkToClose
;
934 CalloutData
.DesktopCloseProcedure
= IntDesktopObjectClose
;
935 CalloutData
.DesktopDeleteProcedure
= IntDesktopObjectDelete
;
936 CalloutData
.WindowStationOkToCloseProcedure
= IntWinStaOkToClose
;
937 // CalloutData.WindowStationCloseProcedure = NULL;
938 CalloutData
.WindowStationDeleteProcedure
= IntWinStaObjectDelete
;
939 CalloutData
.WindowStationParseProcedure
= IntWinStaObjectParse
;
940 // CalloutData.WindowStationOpenProcedure = NULL;
942 /* Register our per-process and per-thread structures. */
943 PsEstablishWin32Callouts(&CalloutData
);
945 /* Register service hook callbacks */
946 #if DBG && defined(KDBG)
947 KdSystemDebugControl('CsoR', DbgPreServiceHook
, ID_Win32PreServiceHook
, 0, 0, 0, 0);
948 KdSystemDebugControl('CsoR', DbgPostServiceHook
, ID_Win32PostServiceHook
, 0, 0, 0, 0);
951 /* Create the global USER heap */
952 GlobalUserHeap
= UserCreateHeap(&GlobalUserHeapSection
,
954 1 * 1024 * 1024); /* FIXME: 1 MB for now... */
955 if (GlobalUserHeap
== NULL
)
957 DPRINT1("Failed to initialize the global heap!\n");
958 return STATUS_UNSUCCESSFUL
;
961 /* Allocate global server info structure */
962 gpsi
= UserHeapAlloc(sizeof(*gpsi
));
965 DPRINT1("Failed allocate server info structure!\n");
966 return STATUS_UNSUCCESSFUL
;
969 RtlZeroMemory(gpsi
, sizeof(*gpsi
));
970 DPRINT("Global Server Data -> %p\n", gpsi
);
972 NT_ROF(InitGdiHandleTable());
973 NT_ROF(InitPaletteImpl());
975 /* Create stock objects, ie. precreated objects commonly
976 used by win32 applications */
977 CreateStockObjects();
978 CreateSysColorObjects();
980 NT_ROF(InitBrushImpl());
981 NT_ROF(InitPDEVImpl());
982 NT_ROF(InitLDEVImpl());
983 NT_ROF(InitDeviceImpl());
984 NT_ROF(InitDcImpl());
985 NT_ROF(InitUserImpl());
986 NT_ROF(InitWindowStationImpl());
987 NT_ROF(InitDesktopImpl());
988 NT_ROF(InitInputImpl());
989 NT_ROF(InitKeyboardImpl());
990 NT_ROF(MsqInitializeImpl());
991 NT_ROF(InitTimerImpl());
992 NT_ROF(InitDCEImpl());
994 gusLanguageID
= UserGetLanguageID();
996 /* Initialize FreeType library */
997 if (!InitFontSupport())
999 DPRINT1("Unable to initialize font support\n");
1003 return STATUS_SUCCESS
;