acffbc43a9ccb278c830b60ca94e196f4e4191e1
[reactos.git] / reactos / win32ss / user / winsrv / usersrv / init.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS User API Server DLL
4 * FILE: win32ss/user/winsrv/usersrv/init.c
5 * PURPOSE: Initialization
6 * PROGRAMMERS: Dmitry Philippov (shedon@mail.ru)
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8 */
9
10 /* INCLUDES *******************************************************************/
11
12 #include "usersrv.h"
13 #include "api.h"
14
15 #define NDEBUG
16 #include <debug.h>
17
18 /* GLOBALS ********************************************************************/
19
20 HINSTANCE UserServerDllInstance = NULL;
21
22 /* Memory */
23 HANDLE UserServerHeap = NULL; // Our own heap.
24
25 // Windows Server 2003 table from http://j00ru.vexillium.org/csrss_list/api_list.html#Windows_2k3
26 PCSR_API_ROUTINE UserServerApiDispatchTable[UserpMaxApiNumber - USERSRV_FIRST_API_NUMBER] =
27 {
28 SrvExitWindowsEx,
29 SrvEndTask,
30 SrvLogon,
31 SrvRegisterServicesProcess, // Not present in Win7
32 SrvActivateDebugger,
33 SrvGetThreadConsoleDesktop, // Not present in Win7
34 SrvDeviceEvent,
35 SrvRegisterLogonProcess, // Not present in Win7
36 SrvCreateSystemThreads,
37 SrvRecordShutdownReason,
38 // SrvCancelShutdown, // Added in Vista
39 // SrvConsoleHandleOperation, // Added in Win7
40 // SrvGetSetShutdownBlockReason, // Added in Vista
41 };
42
43 BOOLEAN UserServerApiServerValidTable[UserpMaxApiNumber - USERSRV_FIRST_API_NUMBER] =
44 {
45 FALSE, // SrvExitWindowsEx
46 FALSE, // SrvEndTask
47 FALSE, // SrvLogon
48 FALSE, // SrvRegisterServicesProcess
49 FALSE, // SrvActivateDebugger
50 TRUE, // SrvGetThreadConsoleDesktop
51 FALSE, // SrvDeviceEvent
52 FALSE, // SrvRegisterLogonProcess
53 FALSE, // SrvCreateSystemThreads
54 FALSE, // SrvRecordShutdownReason
55 // FALSE, // SrvCancelShutdown
56 // FALSE, // SrvConsoleHandleOperation
57 // FALSE, // SrvGetSetShutdownBlockReason
58 };
59
60 /*
61 * On Windows Server 2003, CSR Servers contain
62 * the API Names Table only in Debug Builds.
63 */
64 #ifdef CSR_DBG
65 PCHAR UserServerApiNameTable[UserpMaxApiNumber - USERSRV_FIRST_API_NUMBER] =
66 {
67 "SrvExitWindowsEx",
68 "SrvEndTask",
69 "SrvLogon",
70 "SrvRegisterServicesProcess",
71 "SrvActivateDebugger",
72 "SrvGetThreadConsoleDesktop",
73 "SrvDeviceEvent",
74 "SrvRegisterLogonProcess",
75 "SrvCreateSystemThreads",
76 "SrvRecordShutdownReason",
77 // "SrvCancelShutdown",
78 // "SrvConsoleHandleOperation",
79 // "SrvGetSetShutdownBlockReason",
80 };
81 #endif
82
83 /* FUNCTIONS ******************************************************************/
84
85 // PUSER_SOUND_SENTRY. Used in basesrv.dll
86 BOOL WINAPI _UserSoundSentry(VOID)
87 {
88 // TODO: Do something.
89 return TRUE;
90 }
91
92 ULONG
93 InitializeVideoAddressSpace(VOID)
94 {
95 OBJECT_ATTRIBUTES ObjectAttributes;
96 UNICODE_STRING PhysMemName = RTL_CONSTANT_STRING(L"\\Device\\PhysicalMemory");
97 NTSTATUS Status;
98 HANDLE PhysMemHandle;
99 PVOID BaseAddress;
100 LARGE_INTEGER Offset;
101 SIZE_T ViewSize;
102 CHAR IVTAndBda[1024+256];
103
104 /* Free the 1MB pre-reserved region. In reality, ReactOS should simply support us mapping the view into the reserved area, but it doesn't. */
105 BaseAddress = 0;
106 ViewSize = 1024 * 1024;
107 Status = ZwFreeVirtualMemory(NtCurrentProcess(),
108 &BaseAddress,
109 &ViewSize,
110 MEM_RELEASE);
111 if (!NT_SUCCESS(Status))
112 {
113 DPRINT1("Couldn't unmap reserved memory (%x)\n", Status);
114 return 0;
115 }
116
117 /* Open the physical memory section */
118 InitializeObjectAttributes(&ObjectAttributes,
119 &PhysMemName,
120 0,
121 NULL,
122 NULL);
123 Status = ZwOpenSection(&PhysMemHandle,
124 SECTION_ALL_ACCESS,
125 &ObjectAttributes);
126 if (!NT_SUCCESS(Status))
127 {
128 DPRINT1("Couldn't open \\Device\\PhysicalMemory\n");
129 return 0;
130 }
131
132 /* Map the BIOS and device registers into the address space */
133 Offset.QuadPart = 0xa0000;
134 ViewSize = 0x100000 - 0xa0000;
135 BaseAddress = (PVOID)0xa0000;
136 Status = ZwMapViewOfSection(PhysMemHandle,
137 NtCurrentProcess(),
138 &BaseAddress,
139 0,
140 ViewSize,
141 &Offset,
142 &ViewSize,
143 ViewUnmap,
144 0,
145 PAGE_EXECUTE_READWRITE);
146 if (!NT_SUCCESS(Status))
147 {
148 DPRINT1("Couldn't map physical memory (%x)\n", Status);
149 ZwClose(PhysMemHandle);
150 return 0;
151 }
152
153 /* Close physical memory section handle */
154 ZwClose(PhysMemHandle);
155
156 if (BaseAddress != (PVOID)0xa0000)
157 {
158 DPRINT1("Couldn't map physical memory at the right address (was %x)\n",
159 BaseAddress);
160 return 0;
161 }
162
163 /* Allocate some low memory to use for the non-BIOS
164 * parts of the v86 mode address space
165 */
166 BaseAddress = (PVOID)0x1;
167 ViewSize = 0xa0000 - 0x1000;
168 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
169 &BaseAddress,
170 0,
171 &ViewSize,
172 MEM_RESERVE | MEM_COMMIT,
173 PAGE_EXECUTE_READWRITE);
174 if (!NT_SUCCESS(Status))
175 {
176 DPRINT1("Failed to allocate virtual memory (Status %x)\n", Status);
177 return 0;
178 }
179 if (BaseAddress != (PVOID)0x0)
180 {
181 DPRINT1("Failed to allocate virtual memory at right address (was %x)\n",
182 BaseAddress);
183 return 0;
184 }
185
186 /* Get the real mode IVT and BDA from the kernel */
187 Status = NtVdmControl(VdmInitialize, IVTAndBda);
188 if (!NT_SUCCESS(Status))
189 {
190 DPRINT1("NtVdmControl failed (status %x)\n", Status);
191 return 0;
192 }
193
194 /* Return success */
195 return 1;
196 }
197
198 /**********************************************************************
199 * UserpInitVideo
200 *
201 * TODO: we need a virtual device for sessions other than
202 * TODO: the console one
203 */
204 NTSTATUS
205 UserpInitVideo(VOID)
206 {
207 OBJECT_ATTRIBUTES ObjectAttributes;
208 UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\??\\DISPLAY1");
209 IO_STATUS_BLOCK Iosb;
210 HANDLE VideoHandle = (HANDLE) 0;
211 NTSTATUS Status = STATUS_SUCCESS;
212
213 DPRINT("CSR: %s called\n", __FUNCTION__);
214
215 InitializeVideoAddressSpace();
216
217 InitializeObjectAttributes(&ObjectAttributes,
218 &DeviceName,
219 0,
220 NULL,
221 NULL);
222 Status = NtOpenFile(&VideoHandle,
223 FILE_ALL_ACCESS,
224 &ObjectAttributes,
225 &Iosb,
226 0,
227 0);
228 if (NT_SUCCESS(Status))
229 {
230 NtClose(VideoHandle);
231 }
232
233 return Status;
234 }
235
236 // From win32ss/user/win32csr/dllmain.c
237 VOID
238 WINAPI
239 PrivateCsrssManualGuiCheck(LONG Check)
240 {
241 NtUserCallOneParam(Check, ONEPARAM_ROUTINE_CSRSS_GUICHECK);
242 }
243
244 ULONG
245 NTAPI
246 CreateSystemThreads(PVOID pParam)
247 {
248 NtUserCallOneParam((DWORD)pParam, ONEPARAM_ROUTINE_CREATESYSTEMTHREADS);
249 DPRINT1("This thread should not terminate!\n");
250 return 0;
251 }
252
253 CSR_API(SrvCreateSystemThreads)
254 {
255 DPRINT1("%s not yet implemented\n", __FUNCTION__);
256 return STATUS_NOT_IMPLEMENTED;
257 }
258
259 CSR_API(SrvActivateDebugger)
260 {
261 DPRINT1("%s not yet implemented\n", __FUNCTION__);
262 return STATUS_NOT_IMPLEMENTED;
263 }
264
265 CSR_API(SrvGetThreadConsoleDesktop)
266 {
267 DPRINT1("%s not yet implemented\n", __FUNCTION__);
268 return STATUS_NOT_IMPLEMENTED;
269 }
270
271 CSR_API(SrvDeviceEvent)
272 {
273 DPRINT1("%s not yet implemented\n", __FUNCTION__);
274 return STATUS_NOT_IMPLEMENTED;
275 }
276
277 CSR_SERVER_DLL_INIT(UserServerDllInitialization)
278 {
279 /*** From win32csr... ***/
280 HANDLE ServerThread;
281 CLIENT_ID ClientId;
282 NTSTATUS Status;
283 UINT i;
284 /*** END - From win32csr... ***/
285
286 /* Initialize the memory */
287 UserServerHeap = RtlGetProcessHeap();
288
289 /* Initialize the video */
290 UserpInitVideo();
291 NtUserInitialize(0, NULL, NULL);
292 PrivateCsrssManualGuiCheck(0);
293
294 /* Setup the DLL Object */
295 LoadedServerDll->ApiBase = USERSRV_FIRST_API_NUMBER;
296 LoadedServerDll->HighestApiSupported = UserpMaxApiNumber;
297 LoadedServerDll->DispatchTable = UserServerApiDispatchTable;
298 LoadedServerDll->ValidTable = UserServerApiServerValidTable;
299 #ifdef CSR_DBG
300 LoadedServerDll->NameTable = UserServerApiNameTable;
301 #endif
302 LoadedServerDll->SizeOfProcessData = 0;
303 LoadedServerDll->ConnectCallback = NULL;
304 LoadedServerDll->DisconnectCallback = NULL;
305 LoadedServerDll->HardErrorCallback = UserServerHardError;
306 LoadedServerDll->ShutdownProcessCallback = NULL;
307
308 UserServerDllInstance = LoadedServerDll->ServerHandle;
309
310 /*** From win32csr... See r54125 ***/
311 /* Start the Raw Input Thread and the Desktop Thread */
312 for (i = 0; i < 2; ++i)
313 {
314 Status = RtlCreateUserThread(NtCurrentProcess(),
315 NULL, TRUE, 0, 0, 0,
316 CreateSystemThreads,
317 (PVOID)i, &ServerThread, &ClientId);
318 if (NT_SUCCESS(Status))
319 {
320 NtResumeThread(ServerThread, NULL);
321 NtClose(ServerThread);
322 }
323 else
324 DPRINT1("Cannot start Raw Input Thread!\n");
325 }
326 /*** END - From win32csr... ***/
327
328 /* All done */
329 return STATUS_SUCCESS;
330 }
331
332 /* EOF */