5ed49f112e86f332bafa43d4a15c2be85b37ecbb
[reactos.git] / subsystems / win32 / win32k / main / dllmain.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Driver entry and initialization of win32k
5 * FILE: subsystems/win32/win32k/main/main.c
6 * PROGRAMER:
7 */
8
9 #include <win32k.h>
10 #include <include/napi.h>
11
12 #define NDEBUG
13 #include <debug.h>
14
15 HANDLE hModuleWin;
16
17 PGDI_HANDLE_TABLE INTERNAL_CALL GDIOBJ_iAllocHandleTable(OUT PSECTION_OBJECT *SectionObject);
18 BOOL INTERNAL_CALL GDI_CleanupForProcess (struct _EPROCESS *Process);
19
20 HANDLE GlobalUserHeap = NULL;
21 PSECTION_OBJECT GlobalUserHeapSection = NULL;
22
23 PSERVERINFO gpsi = NULL; // Global User Server Information.
24
25 SHORT gusLanguageID;
26
27 extern ULONG_PTR Win32kSSDT[];
28 extern UCHAR Win32kSSPT[];
29 extern ULONG Win32kNumberOfSysCalls;
30
31 NTSTATUS
32 APIENTRY
33 Win32kProcessCallback(struct _EPROCESS *Process,
34 BOOLEAN Create)
35 {
36 PPROCESSINFO Win32Process;
37 DECLARE_RETURN(NTSTATUS);
38
39 DPRINT("Enter Win32kProcessCallback\n");
40 UserEnterExclusive();
41
42 /* Get the Win32 Process */
43 Win32Process = PsGetProcessWin32Process(Process);
44
45 /* Allocate one if needed */
46 if (!Win32Process)
47 {
48 /* FIXME - lock the process */
49 Win32Process = ExAllocatePoolWithTag(NonPagedPool,
50 sizeof(PROCESSINFO),
51 'p23W');
52
53 if (Win32Process == NULL) RETURN( STATUS_NO_MEMORY);
54
55 RtlZeroMemory(Win32Process, sizeof(PROCESSINFO));
56
57 PsSetProcessWin32Process(Process, Win32Process);
58 /* FIXME - unlock the process */
59 }
60
61 if (Create)
62 {
63 SIZE_T ViewSize = 0;
64 LARGE_INTEGER Offset;
65 PVOID UserBase = NULL;
66 NTSTATUS Status;
67 extern PSECTION_OBJECT GlobalUserHeapSection;
68 DPRINT("Creating W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
69
70 /* map the global heap into the process */
71 Offset.QuadPart = 0;
72 Status = MmMapViewOfSection(GlobalUserHeapSection,
73 PsGetCurrentProcess(),
74 &UserBase,
75 0,
76 0,
77 &Offset,
78 &ViewSize,
79 ViewUnmap,
80 SEC_NO_CHANGE,
81 PAGE_EXECUTE_READ); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
82 if (!NT_SUCCESS(Status))
83 {
84 DPRINT1("Failed to map the global heap! 0x%x\n", Status);
85 RETURN(Status);
86 }
87 Win32Process->HeapMappings.Next = NULL;
88 Win32Process->HeapMappings.KernelMapping = (PVOID)GlobalUserHeap;
89 Win32Process->HeapMappings.UserMapping = UserBase;
90 Win32Process->HeapMappings.Count = 1;
91
92 InitializeListHead(&Win32Process->ClassList);
93
94 InitializeListHead(&Win32Process->MenuListHead);
95
96 InitializeListHead(&Win32Process->GDIBrushAttrFreeList);
97 InitializeListHead(&Win32Process->GDIDcAttrFreeList);
98
99 InitializeListHead(&Win32Process->PrivateFontListHead);
100 ExInitializeFastMutex(&Win32Process->PrivateFontListLock);
101
102 InitializeListHead(&Win32Process->DriverObjListHead);
103 ExInitializeFastMutex(&Win32Process->DriverObjListLock);
104
105 Win32Process->KeyboardLayout = W32kGetDefaultKeyLayout();
106 EngCreateEvent((PEVENT *)&Win32Process->InputIdleEvent);
107 KeInitializeEvent(Win32Process->InputIdleEvent, NotificationEvent, FALSE);
108
109 if(Process->Peb != NULL)
110 {
111 /* map the gdi handle table to user land */
112 Process->Peb->GdiSharedHandleTable = GDI_MapHandleTable(GdiTableSection, Process);
113 Process->Peb->GdiDCAttributeList = GDI_BATCH_LIMIT;
114 }
115
116 Win32Process->peProcess = Process;
117 /* setup process flags */
118 Win32Process->W32PF_flags = 0;
119 }
120 else
121 {
122 DPRINT("Destroying W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
123 Win32Process->W32PF_flags |= W32PF_TERMINATED;
124 if (Win32Process->InputIdleEvent)
125 {
126 EngFreeMem((PVOID)Win32Process->InputIdleEvent);
127 Win32Process->InputIdleEvent = NULL;
128 }
129
130 IntCleanupMenus(Process, Win32Process);
131 IntCleanupCurIcons(Process, Win32Process);
132 CleanupMonitorImpl();
133
134 /* no process windows should exist at this point, or the function will assert! */
135 DestroyProcessClasses(Win32Process);
136
137 GDI_CleanupForProcess(Process);
138
139 co_IntGraphicsCheck(FALSE);
140
141 /*
142 * Deregister logon application automatically
143 */
144 if(LogonProcess == Win32Process)
145 {
146 LogonProcess = NULL;
147 }
148 }
149
150 RETURN( STATUS_SUCCESS);
151
152 CLEANUP:
153 UserLeave();
154 DPRINT("Leave Win32kProcessCallback, ret=%i\n",_ret_);
155 END_CLEANUP;
156 }
157
158
159 NTSTATUS
160 APIENTRY
161 Win32kThreadCallback(struct _ETHREAD *Thread,
162 PSW32THREADCALLOUTTYPE Type)
163 {
164 struct _EPROCESS *Process;
165 PTHREADINFO Win32Thread;
166 int i;
167 DECLARE_RETURN(NTSTATUS);
168
169 DPRINT("Enter Win32kThreadCallback\n");
170 UserEnterExclusive();
171
172 Process = Thread->ThreadsProcess;
173
174 /* Get the Win32 Thread */
175 Win32Thread = PsGetThreadWin32Thread(Thread);
176
177 /* Allocate one if needed */
178 if (!Win32Thread)
179 {
180 /* FIXME - lock the process */
181 Win32Thread = ExAllocatePoolWithTag(NonPagedPool,
182 sizeof(THREADINFO),
183 't23W');
184
185 if (Win32Thread == NULL) RETURN( STATUS_NO_MEMORY);
186
187 RtlZeroMemory(Win32Thread, sizeof(THREADINFO));
188
189 PsSetThreadWin32Thread(Thread, Win32Thread);
190 /* FIXME - unlock the process */
191 }
192 if (Type == PsW32ThreadCalloutInitialize)
193 {
194 HWINSTA hWinSta = NULL;
195 PTEB pTeb;
196 HDESK hDesk = NULL;
197 NTSTATUS Status;
198 PUNICODE_STRING DesktopPath;
199 PRTL_USER_PROCESS_PARAMETERS ProcessParams = (Process->Peb ? Process->Peb->ProcessParameters : NULL);
200
201 DPRINT("Creating W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
202
203 InitializeListHead(&Win32Thread->WindowListHead);
204 InitializeListHead(&Win32Thread->W32CallbackListHead);
205 InitializeListHead(&Win32Thread->PtiLink);
206 for (i = 0; i < NB_HOOKS; i++)
207 {
208 InitializeListHead(&Win32Thread->aphkStart[i]);
209 }
210
211 /*
212 * inherit the thread desktop and process window station (if not yet inherited) from the process startup
213 * info structure. See documentation of CreateProcess()
214 */
215 DesktopPath = (ProcessParams ? ((ProcessParams->DesktopInfo.Length > 0) ? &ProcessParams->DesktopInfo : NULL) : NULL);
216 Status = IntParseDesktopPath(Process,
217 DesktopPath,
218 &hWinSta,
219 &hDesk);
220 if(NT_SUCCESS(Status))
221 {
222 if(hWinSta != NULL)
223 {
224 if(Process != CsrProcess)
225 {
226 HWINSTA hProcessWinSta = (HWINSTA)InterlockedCompareExchangePointer((PVOID)&Process->Win32WindowStation, (PVOID)hWinSta, NULL);
227 if(hProcessWinSta != NULL)
228 {
229 /* our process is already assigned to a different window station, we don't need the handle anymore */
230 NtClose(hWinSta);
231 }
232 }
233 else
234 {
235 NtClose(hWinSta);
236 }
237 }
238
239 if (hDesk != NULL)
240 {
241 PDESKTOP DesktopObject;
242 Win32Thread->rpdesk = NULL;
243 Status = ObReferenceObjectByHandle(hDesk,
244 0,
245 ExDesktopObjectType,
246 KernelMode,
247 (PVOID*)&DesktopObject,
248 NULL);
249 NtClose(hDesk);
250 if(NT_SUCCESS(Status))
251 {
252 if (!IntSetThreadDesktop(DesktopObject,
253 FALSE))
254 {
255 DPRINT1("Unable to set thread desktop\n");
256 }
257 }
258 else
259 {
260 DPRINT1("Unable to reference thread desktop handle 0x%x\n", hDesk);
261 }
262 }
263 }
264 else
265 {
266 DPRINT1("No Desktop handle for this Thread!\n");
267 }
268 Win32Thread->TIF_flags &= ~TIF_INCLEANUP;
269 co_IntDestroyCaret(Win32Thread);
270 Win32Thread->ppi = PsGetCurrentProcessWin32Process();
271 if (Win32Thread->rpdesk && !Win32Thread->pDeskInfo)
272 {
273 Win32Thread->pDeskInfo = Win32Thread->rpdesk->pDeskInfo;
274 }
275 Win32Thread->MessageQueue = MsqCreateMessageQueue(Thread);
276 Win32Thread->KeyboardLayout = W32kGetDefaultKeyLayout();
277 pTeb = NtCurrentTeb();
278 if (pTeb)
279 { /* Attempt to startup client support which should have been initialized in IntSetThreadDesktop. */
280 PCLIENTINFO pci = (PCLIENTINFO)pTeb->Win32ClientInfo;
281 Win32Thread->pClientInfo = pci;
282 pci->pClientThreadInfo = NULL;
283 pci->ppi = Win32Thread->ppi;
284 pci->fsHooks = Win32Thread->fsHooks;
285 if (Win32Thread->KeyboardLayout) pci->hKL = Win32Thread->KeyboardLayout->hkl;
286 pci->dwTIFlags = Win32Thread->TIF_flags;
287 /* CI may not have been initialized. */
288 if (!pci->pDeskInfo && Win32Thread->pDeskInfo)
289 {
290 if (!pci->ulClientDelta) pci->ulClientDelta = DesktopHeapGetUserDelta();
291
292 pci->pDeskInfo = (PVOID)((ULONG_PTR)Win32Thread->pDeskInfo - pci->ulClientDelta);
293 }
294 }
295 else
296 {
297 DPRINT1("No TEB for this Thread!\n");
298 }
299 Win32Thread->pEThread = Thread;
300 }
301 else
302 {
303 PSINGLE_LIST_ENTRY e;
304
305 DPRINT("Destroying W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
306
307 Win32Thread->TIF_flags |= TIF_INCLEANUP;
308 DceFreeThreadDCE(Win32Thread);
309 HOOK_DestroyThreadHooks(Thread);
310 EVENT_DestroyThreadEvents(Thread);
311 /* Cleanup timers */
312 DestroyTimersForThread(Win32Thread);
313 KeSetEvent(Win32Thread->MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
314 UnregisterThreadHotKeys(Thread);
315 /* what if this co_ func crash in umode? what will clean us up then? */
316 co_DestroyThreadWindows(Thread);
317 IntBlockInput(Win32Thread, FALSE);
318 MsqDestroyMessageQueue(Win32Thread->MessageQueue);
319 IntCleanupThreadCallbacks(Win32Thread);
320
321 /* cleanup user object references stack */
322 e = PopEntryList(&Win32Thread->ReferencesList);
323 while (e)
324 {
325 PUSER_REFERENCE_ENTRY ref = CONTAINING_RECORD(e, USER_REFERENCE_ENTRY, Entry);
326 DPRINT("thread clean: remove reference obj 0x%x\n",ref->obj);
327 UserDereferenceObject(ref->obj);
328
329 e = PopEntryList(&Win32Thread->ReferencesList);
330 }
331
332 IntSetThreadDesktop(NULL,
333 TRUE);
334
335 PsSetThreadWin32Thread(Thread, NULL);
336 }
337
338 RETURN( STATUS_SUCCESS);
339
340 CLEANUP:
341 UserLeave();
342 DPRINT("Leave Win32kThreadCallback, ret=%i\n",_ret_);
343 END_CLEANUP;
344 }
345
346 NTSTATUS
347 Win32kInitWin32Thread(PETHREAD Thread)
348 {
349 PEPROCESS Process;
350
351 Process = Thread->ThreadsProcess;
352
353 if (Process->Win32Process == NULL)
354 {
355 /* FIXME - lock the process */
356 Process->Win32Process = ExAllocatePoolWithTag(NonPagedPool, sizeof(PROCESSINFO), USERTAG_PROCESSINFO);
357
358 if (Process->Win32Process == NULL)
359 return STATUS_NO_MEMORY;
360
361 RtlZeroMemory(Process->Win32Process, sizeof(PROCESSINFO));
362 /* FIXME - unlock the process */
363
364 Win32kProcessCallback(Process, TRUE);
365 }
366
367 if (Thread->Tcb.Win32Thread == NULL)
368 {
369 Thread->Tcb.Win32Thread = ExAllocatePoolWithTag(NonPagedPool, sizeof(THREADINFO), USERTAG_THREADINFO);
370 if (Thread->Tcb.Win32Thread == NULL)
371 return STATUS_NO_MEMORY;
372
373 RtlZeroMemory(Thread->Tcb.Win32Thread, sizeof(THREADINFO));
374
375 Win32kThreadCallback(Thread, PsW32ThreadCalloutInitialize);
376 }
377
378 return(STATUS_SUCCESS);
379 }
380
381 C_ASSERT(sizeof(SERVERINFO) <= PAGE_SIZE);
382
383 // Return on failure
384 #define NT_ROF(x) \
385 Status = (x); \
386 if (!NT_SUCCESS(Status)) \
387 { \
388 DPRINT1("Failed '%s' (0x%lx)\n", #x, Status); \
389 return Status; \
390 }
391
392 /*
393 * This definition doesn't work
394 */
395 INIT_FUNCTION
396 NTSTATUS
397 APIENTRY
398 DriverEntry(
399 IN PDRIVER_OBJECT DriverObject,
400 IN PUNICODE_STRING RegistryPath)
401 {
402 NTSTATUS Status;
403 BOOLEAN Result;
404 WIN32_CALLOUTS_FPNS CalloutData = {0};
405 PVOID GlobalUserHeapBase = NULL;
406
407 /*
408 * Register user mode call interface
409 * (system service table index = 1)
410 */
411 Result = KeAddSystemServiceTable(Win32kSSDT,
412 NULL,
413 Win32kNumberOfSysCalls,
414 Win32kSSPT,
415 1);
416 if (Result == FALSE)
417 {
418 DPRINT1("Adding system services failed!\n");
419 return STATUS_UNSUCCESSFUL;
420 }
421
422 hModuleWin = MmPageEntireDriver(DriverEntry);
423 DPRINT("Win32k hInstance 0x%x!\n",hModuleWin);
424
425 /* Register Object Manager Callbacks */
426 CalloutData.WindowStationParseProcedure = IntWinStaObjectParse;
427 CalloutData.WindowStationDeleteProcedure = IntWinStaObjectDelete;
428 CalloutData.DesktopDeleteProcedure = IntDesktopObjectDelete;
429 CalloutData.ProcessCallout = Win32kProcessCallback;
430 CalloutData.ThreadCallout = Win32kThreadCallback;
431 CalloutData.BatchFlushRoutine = NtGdiFlushUserBatch;
432
433 /* Register our per-process and per-thread structures. */
434 PsEstablishWin32Callouts((PWIN32_CALLOUTS_FPNS)&CalloutData);
435
436 /* Create the global USER heap */
437 GlobalUserHeap = UserCreateHeap(&GlobalUserHeapSection,
438 &GlobalUserHeapBase,
439 1 * 1024 * 1024); /* FIXME - 1 MB for now... */
440 if (GlobalUserHeap == NULL)
441 {
442 DPRINT1("Failed to initialize the global heap!\n");
443 return STATUS_UNSUCCESSFUL;
444 }
445
446 /* Allocate global server info structure */
447 gpsi = UserHeapAlloc(sizeof(SERVERINFO));
448 if (!gpsi)
449 {
450 DPRINT1("Failed allocate server info structure!\n");
451 return STATUS_UNSUCCESSFUL;
452 }
453
454 RtlZeroMemory(gpsi, sizeof(SERVERINFO));
455 DPRINT("Global Server Data -> %x\n", gpsi);
456
457 NT_ROF(InitGdiHandleTable());
458 NT_ROF(InitPaletteImpl());
459
460 /* Create stock objects, ie. precreated objects commonly
461 used by win32 applications */
462 CreateStockObjects();
463 CreateSysColorObjects();
464
465 NT_ROF(InitXlateImpl());
466 NT_ROF(InitPDEVImpl());
467 NT_ROF(InitLDEVImpl());
468 NT_ROF(InitDeviceImpl());
469 NT_ROF(InitDcImpl());
470 NT_ROF(InitUserImpl());
471 NT_ROF(InitHotkeyImpl());
472 NT_ROF(InitWindowStationImpl());
473 NT_ROF(InitDesktopImpl());
474 NT_ROF(InitWindowImpl());
475 NT_ROF(InitMenuImpl());
476 NT_ROF(InitInputImpl());
477 NT_ROF(InitKeyboardImpl());
478 NT_ROF(InitMonitorImpl());
479 NT_ROF(MsqInitializeImpl());
480 NT_ROF(InitTimerImpl());
481 NT_ROF(InitAcceleratorImpl());
482 NT_ROF(InitGuiCheckImpl());
483
484 /* Initialize FreeType library */
485 if (!InitFontSupport())
486 {
487 DPRINT1("Unable to initialize font support\n");
488 return Status;
489 }
490
491 gusLanguageID = IntGdiGetLanguageID();
492
493 return STATUS_SUCCESS;
494 }
495
496 /* EOF */