14269cf017eafd28844b9f5f666bb974152600cd
[reactos.git] / reactos / 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->MenuListHead);
93
94 InitializeListHead(&Win32Process->GDIBrushAttrFreeList);
95 InitializeListHead(&Win32Process->GDIDcAttrFreeList);
96
97 InitializeListHead(&Win32Process->PrivateFontListHead);
98 ExInitializeFastMutex(&Win32Process->PrivateFontListLock);
99
100 InitializeListHead(&Win32Process->DriverObjListHead);
101 ExInitializeFastMutex(&Win32Process->DriverObjListLock);
102
103 Win32Process->KeyboardLayout = W32kGetDefaultKeyLayout();
104 EngCreateEvent((PEVENT *)&Win32Process->InputIdleEvent);
105 KeInitializeEvent(Win32Process->InputIdleEvent, NotificationEvent, FALSE);
106
107 if(Process->Peb != NULL)
108 {
109 /* map the gdi handle table to user land */
110 Process->Peb->GdiSharedHandleTable = GDI_MapHandleTable(Process);
111 Process->Peb->GdiDCAttributeList = GDI_BATCH_LIMIT;
112 }
113
114 Win32Process->peProcess = Process;
115 /* setup process flags */
116 Win32Process->W32PF_flags = 0;
117
118 /* Create pools for GDI object attributes */
119 Win32Process->pPoolDcAttr = GdiPoolCreate(sizeof(DC_ATTR), 'acdG');
120 Win32Process->pPoolBrushAttr = GdiPoolCreate(sizeof(BRUSH_ATTR), 'arbG');
121 Win32Process->pPoolRgnAttr = GdiPoolCreate(sizeof(RGN_ATTR), 'agrG');
122 ASSERT(Win32Process->pPoolDcAttr);
123 ASSERT(Win32Process->pPoolBrushAttr);
124 ASSERT(Win32Process->pPoolRgnAttr);
125 }
126 else
127 {
128 DPRINT("Destroying W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
129 Win32Process->W32PF_flags |= W32PF_TERMINATED;
130
131 if (Win32Process->InputIdleEvent)
132 {
133 EngFreeMem((PVOID)Win32Process->InputIdleEvent);
134 Win32Process->InputIdleEvent = NULL;
135 }
136
137 IntCleanupMenus(Process, Win32Process);
138 IntCleanupCurIcons(Process, Win32Process);
139 CleanupMonitorImpl();
140
141 /* no process windows should exist at this point, or the function will assert! */
142 DestroyProcessClasses(Win32Process);
143 Win32Process->W32PF_flags &= ~W32PF_CLASSESREGISTERED;
144
145 GDI_CleanupForProcess(Process);
146
147 co_IntGraphicsCheck(FALSE);
148
149 /*
150 * Deregister logon application automatically
151 */
152 if(LogonProcess == Win32Process)
153 {
154 LogonProcess = NULL;
155 }
156
157 UserSetProcessWindowStation(NULL);
158
159 /* Destroy GDI pools */
160 GdiPoolDestroy(Win32Process->pPoolDcAttr);
161 GdiPoolDestroy(Win32Process->pPoolBrushAttr);
162 GdiPoolDestroy(Win32Process->pPoolRgnAttr);
163 }
164
165 RETURN( STATUS_SUCCESS);
166
167 CLEANUP:
168 UserLeave();
169 DPRINT("Leave Win32kProcessCallback, ret=%i\n",_ret_);
170 END_CLEANUP;
171 }
172
173
174 NTSTATUS
175 APIENTRY
176 Win32kThreadCallback(struct _ETHREAD *Thread,
177 PSW32THREADCALLOUTTYPE Type)
178 {
179 struct _EPROCESS *Process;
180 PTHREADINFO Win32Thread;
181 int i;
182 DECLARE_RETURN(NTSTATUS);
183
184 DPRINT("Enter Win32kThreadCallback\n");
185 UserEnterExclusive();
186
187 Process = Thread->ThreadsProcess;
188
189 /* Get the Win32 Thread */
190 Win32Thread = PsGetThreadWin32Thread(Thread);
191
192 /* Allocate one if needed */
193 if (!Win32Thread)
194 {
195 /* FIXME - lock the process */
196 Win32Thread = ExAllocatePoolWithTag(NonPagedPool,
197 sizeof(THREADINFO),
198 't23W');
199
200 if (Win32Thread == NULL) RETURN( STATUS_NO_MEMORY);
201
202 RtlZeroMemory(Win32Thread, sizeof(THREADINFO));
203
204 PsSetThreadWin32Thread(Thread, Win32Thread);
205 /* FIXME - unlock the process */
206 }
207 if (Type == PsW32ThreadCalloutInitialize)
208 {
209 HWINSTA hWinSta = NULL;
210 PTEB pTeb;
211 HDESK hDesk = NULL;
212 NTSTATUS Status;
213 PUNICODE_STRING DesktopPath;
214 PRTL_USER_PROCESS_PARAMETERS ProcessParams = (Process->Peb ? Process->Peb->ProcessParameters : NULL);
215
216 DPRINT("Creating W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
217
218 InitializeListHead(&Win32Thread->WindowListHead);
219 InitializeListHead(&Win32Thread->W32CallbackListHead);
220 InitializeListHead(&Win32Thread->PtiLink);
221 for (i = 0; i < NB_HOOKS; i++)
222 {
223 InitializeListHead(&Win32Thread->aphkStart[i]);
224 }
225
226 /*
227 * inherit the thread desktop and process window station (if not yet inherited) from the process startup
228 * info structure. See documentation of CreateProcess()
229 */
230 DesktopPath = (ProcessParams ? ((ProcessParams->DesktopInfo.Length > 0) ? &ProcessParams->DesktopInfo : NULL) : NULL);
231 Status = IntParseDesktopPath(Process,
232 DesktopPath,
233 &hWinSta,
234 &hDesk);
235 if(NT_SUCCESS(Status))
236 {
237 if(hWinSta != NULL)
238 {
239 if(!UserSetProcessWindowStation(hWinSta))
240 {
241 DPRINT1("Failed to set process window station\n");
242 }
243 }
244
245 if (hDesk != NULL)
246 {
247 if (!IntSetThreadDesktop(hDesk, FALSE))
248 {
249 DPRINT1("Unable to set thread desktop\n");
250 }
251 }
252 }
253 else
254 {
255 DPRINT1("No Desktop handle for this Thread!\n");
256 }
257 Win32Thread->TIF_flags &= ~TIF_INCLEANUP;
258 co_IntDestroyCaret(Win32Thread);
259 Win32Thread->ppi = PsGetCurrentProcessWin32Process();
260 Win32Thread->ptiSibling = Win32Thread->ppi->ptiList;
261 Win32Thread->ppi->ptiList = Win32Thread;
262 Win32Thread->ppi->cThreads++;
263 if (Win32Thread->rpdesk && !Win32Thread->pDeskInfo)
264 {
265 Win32Thread->pDeskInfo = Win32Thread->rpdesk->pDeskInfo;
266 }
267 Win32Thread->MessageQueue = MsqCreateMessageQueue(Thread);
268 Win32Thread->KeyboardLayout = W32kGetDefaultKeyLayout();
269 pTeb = NtCurrentTeb();
270 if (pTeb)
271 { /* Attempt to startup client support which should have been initialized in IntSetThreadDesktop. */
272 PCLIENTINFO pci = (PCLIENTINFO)pTeb->Win32ClientInfo;
273 Win32Thread->pClientInfo = pci;
274 pci->ppi = Win32Thread->ppi;
275 pci->fsHooks = Win32Thread->fsHooks;
276 if (Win32Thread->KeyboardLayout) pci->hKL = Win32Thread->KeyboardLayout->hkl;
277 pci->dwTIFlags = Win32Thread->TIF_flags;
278 /* CI may not have been initialized. */
279 if (!pci->pDeskInfo && Win32Thread->pDeskInfo)
280 {
281 if (!pci->ulClientDelta) pci->ulClientDelta = DesktopHeapGetUserDelta();
282
283 pci->pDeskInfo = (PVOID)((ULONG_PTR)Win32Thread->pDeskInfo - pci->ulClientDelta);
284 }
285 if (Win32Thread->pcti && pci->pDeskInfo)
286 pci->pClientThreadInfo = (PVOID)((ULONG_PTR)Win32Thread->pcti - pci->ulClientDelta);
287 else
288 pci->pClientThreadInfo = NULL;
289 }
290 else
291 {
292 DPRINT1("No TEB for this Thread!\n");
293 // System thread running! Now SendMessage should be okay.
294 Win32Thread->pcti = &Win32Thread->cti;
295 }
296 Win32Thread->pEThread = Thread;
297 }
298 else
299 {
300 PTHREADINFO pti;
301 PSINGLE_LIST_ENTRY e;
302
303 DPRINT("Destroying W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
304
305 Win32Thread->TIF_flags |= TIF_INCLEANUP;
306 pti = Win32Thread->ppi->ptiList;
307 if (pti == Win32Thread)
308 {
309 Win32Thread->ppi->ptiList = Win32Thread->ptiSibling;
310 Win32Thread->ppi->cThreads--;
311 }
312 else
313 {
314 do
315 {
316 if (pti->ptiSibling == Win32Thread)
317 {
318 pti->ptiSibling = Win32Thread->ptiSibling;
319 Win32Thread->ppi->cThreads--;
320 break;
321 }
322 pti = pti->ptiSibling;
323 }
324 while (pti);
325 }
326 DceFreeThreadDCE(Win32Thread);
327 HOOK_DestroyThreadHooks(Thread);
328 EVENT_DestroyThreadEvents(Thread);
329 /* Cleanup timers */
330 DestroyTimersForThread(Win32Thread);
331 KeSetEvent(Win32Thread->MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
332 UnregisterThreadHotKeys(Thread);
333 /* what if this co_ func crash in umode? what will clean us up then? */
334 co_DestroyThreadWindows(Thread);
335 IntBlockInput(Win32Thread, FALSE);
336 MsqDestroyMessageQueue(Win32Thread->MessageQueue);
337 IntCleanupThreadCallbacks(Win32Thread);
338
339 /* cleanup user object references stack */
340 e = PopEntryList(&Win32Thread->ReferencesList);
341 while (e)
342 {
343 PUSER_REFERENCE_ENTRY ref = CONTAINING_RECORD(e, USER_REFERENCE_ENTRY, Entry);
344 DPRINT("thread clean: remove reference obj 0x%x\n",ref->obj);
345 UserDereferenceObject(ref->obj);
346
347 e = PopEntryList(&Win32Thread->ReferencesList);
348 }
349
350 IntSetThreadDesktop(NULL, TRUE);
351
352 PsSetThreadWin32Thread(Thread, NULL);
353 }
354
355 RETURN( STATUS_SUCCESS);
356
357 CLEANUP:
358 UserLeave();
359 DPRINT("Leave Win32kThreadCallback, ret=%i\n",_ret_);
360 END_CLEANUP;
361 }
362
363 NTSTATUS
364 Win32kInitWin32Thread(PETHREAD Thread)
365 {
366 PEPROCESS Process;
367
368 Process = Thread->ThreadsProcess;
369
370 if (Process->Win32Process == NULL)
371 {
372 /* FIXME - lock the process */
373 Process->Win32Process = ExAllocatePoolWithTag(NonPagedPool, sizeof(PROCESSINFO), USERTAG_PROCESSINFO);
374
375 if (Process->Win32Process == NULL)
376 return STATUS_NO_MEMORY;
377
378 RtlZeroMemory(Process->Win32Process, sizeof(PROCESSINFO));
379 /* FIXME - unlock the process */
380
381 Win32kProcessCallback(Process, TRUE);
382 }
383
384 if (Thread->Tcb.Win32Thread == NULL)
385 {
386 Thread->Tcb.Win32Thread = ExAllocatePoolWithTag(NonPagedPool, sizeof(THREADINFO), USERTAG_THREADINFO);
387 if (Thread->Tcb.Win32Thread == NULL)
388 return STATUS_NO_MEMORY;
389
390 RtlZeroMemory(Thread->Tcb.Win32Thread, sizeof(THREADINFO));
391
392 Win32kThreadCallback(Thread, PsW32ThreadCalloutInitialize);
393 }
394
395 return(STATUS_SUCCESS);
396 }
397
398 C_ASSERT(sizeof(SERVERINFO) <= PAGE_SIZE);
399
400 // Return on failure
401 #define NT_ROF(x) \
402 Status = (x); \
403 if (!NT_SUCCESS(Status)) \
404 { \
405 DPRINT1("Failed '%s' (0x%lx)\n", #x, Status); \
406 return Status; \
407 }
408
409 /*
410 * This definition doesn't work
411 */
412 INIT_FUNCTION
413 NTSTATUS
414 APIENTRY
415 DriverEntry(
416 IN PDRIVER_OBJECT DriverObject,
417 IN PUNICODE_STRING RegistryPath)
418 {
419 NTSTATUS Status;
420 BOOLEAN Result;
421 WIN32_CALLOUTS_FPNS CalloutData = {0};
422 PVOID GlobalUserHeapBase = NULL;
423
424 /*
425 * Register user mode call interface
426 * (system service table index = 1)
427 */
428 Result = KeAddSystemServiceTable(Win32kSSDT,
429 NULL,
430 Win32kNumberOfSysCalls,
431 Win32kSSPT,
432 1);
433 if (Result == FALSE)
434 {
435 DPRINT1("Adding system services failed!\n");
436 return STATUS_UNSUCCESSFUL;
437 }
438
439 hModuleWin = MmPageEntireDriver(DriverEntry);
440 DPRINT("Win32k hInstance 0x%x!\n",hModuleWin);
441
442 /* Register Object Manager Callbacks */
443 CalloutData.WindowStationParseProcedure = IntWinStaObjectParse;
444 CalloutData.WindowStationDeleteProcedure = IntWinStaObjectDelete;
445 CalloutData.DesktopDeleteProcedure = IntDesktopObjectDelete;
446 CalloutData.ProcessCallout = Win32kProcessCallback;
447 CalloutData.ThreadCallout = Win32kThreadCallback;
448 CalloutData.BatchFlushRoutine = NtGdiFlushUserBatch;
449 CalloutData.DesktopOkToCloseProcedure = IntDesktopOkToClose;
450 CalloutData.WindowStationOkToCloseProcedure = IntWinstaOkToClose;
451
452 /* Register our per-process and per-thread structures. */
453 PsEstablishWin32Callouts((PWIN32_CALLOUTS_FPNS)&CalloutData);
454
455 #if DBG_ENABLE_SERVICE_HOOKS
456 /* Register service hook callbacks */
457 KdSystemDebugControl('CsoR', DbgPreServiceHook, ID_Win32PreServiceHook, 0, 0, 0, 0);
458 KdSystemDebugControl('CsoR', DbgPostServiceHook, ID_Win32PostServiceHook, 0, 0, 0, 0);
459 #endif
460
461 /* Create the global USER heap */
462 GlobalUserHeap = UserCreateHeap(&GlobalUserHeapSection,
463 &GlobalUserHeapBase,
464 1 * 1024 * 1024); /* FIXME - 1 MB for now... */
465 if (GlobalUserHeap == NULL)
466 {
467 DPRINT1("Failed to initialize the global heap!\n");
468 return STATUS_UNSUCCESSFUL;
469 }
470
471 /* Allocate global server info structure */
472 gpsi = UserHeapAlloc(sizeof(SERVERINFO));
473 if (!gpsi)
474 {
475 DPRINT1("Failed allocate server info structure!\n");
476 return STATUS_UNSUCCESSFUL;
477 }
478
479 RtlZeroMemory(gpsi, sizeof(SERVERINFO));
480 DPRINT("Global Server Data -> %x\n", gpsi);
481
482 NT_ROF(InitGdiHandleTable());
483 NT_ROF(InitPaletteImpl());
484
485 /* Create stock objects, ie. precreated objects commonly
486 used by win32 applications */
487 CreateStockObjects();
488 CreateSysColorObjects();
489
490 NT_ROF(InitXlateImpl());
491 NT_ROF(InitPDEVImpl());
492 NT_ROF(InitLDEVImpl());
493 NT_ROF(InitDeviceImpl());
494 NT_ROF(InitDcImpl());
495 NT_ROF(InitUserImpl());
496 NT_ROF(InitHotkeyImpl());
497 NT_ROF(InitWindowStationImpl());
498 NT_ROF(InitDesktopImpl());
499 NT_ROF(InitWindowImpl());
500 NT_ROF(InitMenuImpl());
501 NT_ROF(InitInputImpl());
502 NT_ROF(InitKeyboardImpl());
503 NT_ROF(InitMonitorImpl());
504 NT_ROF(MsqInitializeImpl());
505 NT_ROF(InitTimerImpl());
506 NT_ROF(InitAcceleratorImpl());
507 NT_ROF(InitGuiCheckImpl());
508
509 /* Initialize FreeType library */
510 if (!InitFontSupport())
511 {
512 DPRINT1("Unable to initialize font support\n");
513 return Status;
514 }
515
516 gusLanguageID = IntGdiGetLanguageID();
517
518 return STATUS_SUCCESS;
519 }
520
521 /* EOF */