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