[BASESRV]
[reactos.git] / win32ss / user / winsrv / init.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS User API Server DLL
4 * FILE: win32ss/user/winsrv/init.c
5 * PURPOSE: Initialization
6 * PROGRAMMERS: Dmitry Philippov (shedon@mail.ru)
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8 */
9
10 #include "winsrv.h"
11
12 /* Public Win32K Headers */
13 // For calling NtUser...()
14 #include <ntuser.h>
15
16 #define NDEBUG
17 #include <debug.h>
18
19 HINSTANCE UserSrvDllInstance = NULL;
20 // HANDLE WinSrvApiPort = NULL;
21
22 /* Memory */
23 HANDLE UserSrvHeap = NULL; // Our own heap.
24 // HANDLE BaseSrvSharedHeap = NULL; // Shared heap with CSR. (CsrSrvSharedSectionHeap)
25 // PBASE_STATIC_SERVER_DATA BaseStaticServerData = NULL; // Data that we can share amongst processes. Initialized inside BaseSrvSharedHeap.
26
27
28 PCSR_API_ROUTINE UserServerApiDispatchTable[UserpMaxApiNumber] =
29 {
30 SrvExitWindowsEx,
31 // SrvEndTask,
32 // SrvLogon,
33 SrvRegisterServicesProcess, // Not present in Win7
34 // SrvActivateDebugger,
35 // SrvGetThreadConsoleDesktop, // Not present in Win7
36 // SrvDeviceEvent,
37 SrvRegisterLogonProcess, // Not present in Win7
38 // SrvCreateSystemThreads,
39 // SrvRecordShutdownReason,
40 // SrvCancelShutdown, // Added in Vista
41 // SrvConsoleHandleOperation, // Added in Win7
42 // SrvGetSetShutdownBlockReason, // Added in Vista
43
44 /// HACK: ReactOS-specific
45 RosSetLogonNotifyWindow
46 };
47
48 BOOLEAN UserServerApiServerValidTable[UserpMaxApiNumber] =
49 {
50 FALSE, // SrvExitWindowsEx
51 // FALSE, // SrvEndTask
52 // FALSE, // SrvLogon
53 FALSE, // SrvRegisterServicesProcess
54 // FALSE, // SrvActivateDebugger
55 // TRUE, // SrvGetThreadConsoleDesktop
56 // FALSE, // SrvDeviceEvent
57 FALSE, // SrvRegisterLogonProcess
58 // FALSE, // SrvCreateSystemThreads
59 // FALSE, // SrvRecordShutdownReason
60 // FALSE, // SrvCancelShutdown
61 // FALSE, // SrvConsoleHandleOperation
62 // FALSE, // SrvGetSetShutdownBlockReason
63
64 /// HACK: ReactOS-specific
65 FALSE, // RosSetLogonNotifyWindow
66
67 // FALSE
68 };
69
70 PCHAR UserServerApiNameTable[UserpMaxApiNumber] =
71 {
72 "SrvExitWindowsEx",
73 // "SrvEndTask",
74 // "SrvLogon",
75 "SrvRegisterServicesProcess",
76 // "SrvActivateDebugger",
77 // "SrvGetThreadConsoleDesktop",
78 // "SrvDeviceEvent",
79 "SrvRegisterLogonProcess",
80 // "SrvCreateSystemThreads",
81 // "SrvRecordShutdownReason",
82 // "SrvCancelShutdown",
83 // "SrvConsoleHandleOperation",
84 // "SrvGetSetShutdownBlockReason",
85
86 /// HACK: ReactOS-specific
87 "RosSetLogonNotifyWindow",
88
89 // NULL
90 };
91
92
93 /* FUNCTIONS ******************************************************************/
94
95 /*
96 VOID WINAPI UserStaticServerThread(PVOID x)
97 {
98 // NTSTATUS Status = STATUS_SUCCESS;
99 PPORT_MESSAGE Request = (PPORT_MESSAGE)x;
100 PPORT_MESSAGE Reply = NULL;
101 ULONG MessageType = 0;
102
103 DPRINT("WINSRV: %s(%08lx) called\n", __FUNCTION__, x);
104
105 MessageType = Request->u2.s2.Type;
106 DPRINT("WINSRV: %s(%08lx) received a message (Type=%d)\n",
107 __FUNCTION__, x, MessageType);
108 switch (MessageType)
109 {
110 default:
111 Reply = Request;
112 /\* Status = *\/ NtReplyPort(WinSrvApiPort, Reply);
113 break;
114 }
115 }
116 */
117
118 ULONG
119 InitializeVideoAddressSpace(VOID)
120 {
121 OBJECT_ATTRIBUTES ObjectAttributes;
122 UNICODE_STRING PhysMemName = RTL_CONSTANT_STRING(L"\\Device\\PhysicalMemory");
123 NTSTATUS Status;
124 HANDLE PhysMemHandle;
125 PVOID BaseAddress;
126 LARGE_INTEGER Offset;
127 SIZE_T ViewSize;
128 CHAR IVTAndBda[1024+256];
129
130 /* Free the 1MB pre-reserved region. In reality, ReactOS should simply support us mapping the view into the reserved area, but it doesn't. */
131 BaseAddress = 0;
132 ViewSize = 1024 * 1024;
133 Status = ZwFreeVirtualMemory(NtCurrentProcess(),
134 &BaseAddress,
135 &ViewSize,
136 MEM_RELEASE);
137 if (!NT_SUCCESS(Status))
138 {
139 DPRINT1("Couldn't unmap reserved memory (%x)\n", Status);
140 return 0;
141 }
142
143 /* Open the physical memory section */
144 InitializeObjectAttributes(&ObjectAttributes,
145 &PhysMemName,
146 0,
147 NULL,
148 NULL);
149 Status = ZwOpenSection(&PhysMemHandle,
150 SECTION_ALL_ACCESS,
151 &ObjectAttributes);
152 if (!NT_SUCCESS(Status))
153 {
154 DPRINT1("Couldn't open \\Device\\PhysicalMemory\n");
155 return 0;
156 }
157
158 /* Map the BIOS and device registers into the address space */
159 Offset.QuadPart = 0xa0000;
160 ViewSize = 0x100000 - 0xa0000;
161 BaseAddress = (PVOID)0xa0000;
162 Status = ZwMapViewOfSection(PhysMemHandle,
163 NtCurrentProcess(),
164 &BaseAddress,
165 0,
166 ViewSize,
167 &Offset,
168 &ViewSize,
169 ViewUnmap,
170 0,
171 PAGE_EXECUTE_READWRITE);
172 if (!NT_SUCCESS(Status))
173 {
174 DPRINT1("Couldn't map physical memory (%x)\n", Status);
175 ZwClose(PhysMemHandle);
176 return 0;
177 }
178
179 /* Close physical memory section handle */
180 ZwClose(PhysMemHandle);
181
182 if (BaseAddress != (PVOID)0xa0000)
183 {
184 DPRINT1("Couldn't map physical memory at the right address (was %x)\n",
185 BaseAddress);
186 return 0;
187 }
188
189 /* Allocate some low memory to use for the non-BIOS
190 * parts of the v86 mode address space
191 */
192 BaseAddress = (PVOID)0x1;
193 ViewSize = 0xa0000 - 0x1000;
194 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
195 &BaseAddress,
196 0,
197 &ViewSize,
198 MEM_RESERVE | MEM_COMMIT,
199 PAGE_EXECUTE_READWRITE);
200 if (!NT_SUCCESS(Status))
201 {
202 DPRINT1("Failed to allocate virtual memory (Status %x)\n", Status);
203 return 0;
204 }
205 if (BaseAddress != (PVOID)0x0)
206 {
207 DPRINT1("Failed to allocate virtual memory at right address (was %x)\n",
208 BaseAddress);
209 return 0;
210 }
211
212 /* Get the real mode IVT and BDA from the kernel */
213 Status = NtVdmControl(VdmInitialize, IVTAndBda);
214 if (!NT_SUCCESS(Status))
215 {
216 DPRINT1("NtVdmControl failed (status %x)\n", Status);
217 return 0;
218 }
219
220 /* Return success */
221 return 1;
222 }
223
224 /**********************************************************************
225 * CsrpInitVideo/3
226 *
227 * TODO: we need a virtual device for sessions other than
228 * TODO: the console one
229 */
230 NTSTATUS
231 CsrpInitVideo(VOID)
232 {
233 OBJECT_ATTRIBUTES ObjectAttributes;
234 UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\??\\DISPLAY1");
235 IO_STATUS_BLOCK Iosb;
236 HANDLE VideoHandle = (HANDLE) 0;
237 NTSTATUS Status = STATUS_SUCCESS;
238
239 DPRINT("CSR: %s called\n", __FUNCTION__);
240
241 InitializeVideoAddressSpace();
242
243 InitializeObjectAttributes(&ObjectAttributes,
244 &DeviceName,
245 0,
246 NULL,
247 NULL);
248 Status = NtOpenFile(&VideoHandle,
249 FILE_ALL_ACCESS,
250 &ObjectAttributes,
251 &Iosb,
252 0,
253 0);
254 if (NT_SUCCESS(Status))
255 {
256 NtClose(VideoHandle);
257 }
258
259 return Status;
260 }
261
262 VOID
263 WINAPI
264 PrivateCsrssManualGuiCheck(LONG Check)
265 {
266 NtUserCallOneParam(Check, ONEPARAM_ROUTINE_CSRSS_GUICHECK);
267 }
268
269 /*** HACK from win32csr... ***/
270 static HHOOK hhk = NULL;
271
272 LRESULT
273 CALLBACK
274 KeyboardHookProc(int nCode,
275 WPARAM wParam,
276 LPARAM lParam)
277 {
278 return CallNextHookEx(hhk, nCode, wParam, lParam);
279 }
280 /*** END - HACK from win32csr... ***/
281
282 DWORD
283 WINAPI
284 CreateSystemThreads(PVOID pParam)
285 {
286 NtUserCallOneParam((DWORD)pParam, ONEPARAM_ROUTINE_CREATESYSTEMTHREADS);
287 DPRINT1("This thread should not terminate!\n");
288 return 0;
289 }
290
291 CSR_SERVER_DLL_INIT(UserServerDllInitialization)
292 {
293 /*
294 NTSTATUS Status = STATUS_SUCCESS;
295
296 DPRINT("WINSRV: %s called\n", __FUNCTION__);
297
298 // Get the listening port from csrsrv.dll
299 WinSrvApiPort = CsrQueryApiPort ();
300 if (NULL == WinSrvApiPort)
301 {
302 return STATUS_UNSUCCESSFUL;
303 }
304 // Register our message dispatcher
305 Status = CsrAddStaticServerThread (UserStaticServerThread);
306 if (NT_SUCCESS(Status))
307 {
308 //TODO: perform the real user server internal initialization here
309 }
310 return Status;
311 */
312
313 /*** From win32csr... ***/
314 HANDLE ServerThread;
315 CLIENT_ID ClientId;
316 NTSTATUS Status;
317 UINT i;
318 /*** END - From win32csr... ***/
319
320 /* Initialize memory */
321 UserSrvHeap = RtlGetProcessHeap(); // Initialize our own heap.
322 // BaseSrvSharedHeap = LoadedServerDll->SharedSection; // Get the CSR shared heap.
323 // LoadedServerDll->SharedSection = BaseStaticServerData;
324
325 CsrpInitVideo();
326 NtUserInitialize(0, NULL, NULL);
327 PrivateCsrssManualGuiCheck(0);
328
329 /* Setup the DLL Object */
330 LoadedServerDll->ApiBase = USERSRV_FIRST_API_NUMBER;
331 LoadedServerDll->HighestApiSupported = UserpMaxApiNumber;
332 LoadedServerDll->DispatchTable = UserServerApiDispatchTable;
333 LoadedServerDll->ValidTable = UserServerApiServerValidTable;
334 LoadedServerDll->NameTable = UserServerApiNameTable;
335 LoadedServerDll->SizeOfProcessData = 0;
336 LoadedServerDll->ConnectCallback = NULL;
337 // LoadedServerDll->DisconnectCallback = Win32CsrReleaseConsole;
338 // LoadedServerDll->NewProcessCallback = Win32CsrDuplicateHandleTable;
339 LoadedServerDll->HardErrorCallback = Win32CsrHardError;
340
341 /*** From win32csr... See r54125 ***/
342 /* Start the Raw Input Thread and the Desktop Thread */
343 for (i = 0; i < 2; ++i)
344 {
345 Status = RtlCreateUserThread(NtCurrentProcess(), NULL, TRUE, 0, 0, 0, (PTHREAD_START_ROUTINE)CreateSystemThreads, (PVOID)i, &ServerThread, &ClientId);
346 if (NT_SUCCESS(Status))
347 {
348 NtResumeThread(ServerThread, NULL);
349 NtClose(ServerThread);
350 }
351 else
352 DPRINT1("Cannot start Raw Input Thread!\n");
353 }
354 /*** END - From win32csr... ***/
355
356 /* All done */
357 return STATUS_SUCCESS;
358 }
359
360 // PUSER_SOUND_SENTRY. Used in basesrv.dll
361 BOOL WINAPI _UserSoundSentry(VOID)
362 {
363 // Do something.
364 return TRUE;
365 }
366
367 BOOL
368 WINAPI
369 DllMain(IN HINSTANCE hInstanceDll,
370 IN DWORD dwReason,
371 IN LPVOID lpReserved)
372 {
373 UNREFERENCED_PARAMETER(dwReason);
374 UNREFERENCED_PARAMETER(lpReserved);
375
376 if (DLL_PROCESS_ATTACH == dwReason)
377 {
378 UserSrvDllInstance = hInstanceDll;
379
380 /*** HACK from win32csr... ***/
381
382 //
383 // HACK HACK HACK ReactOS to BOOT! Initialization BUG ALERT! See bug 5655.
384 //
385 hhk = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0);
386 // BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT!
387 // BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT!
388 // BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT! BUG ALERT!
389
390 /*** END - HACK from win32csr... ***/
391 }
392
393 return TRUE;
394 }
395
396 /* EOF */