7749d7424ec811061fd5b13b54b57740e690b1ce
[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 PCHAR UserServerApiNameTable[UserpMaxApiNumber - USERSRV_FIRST_API_NUMBER] =
61 {
62 "SrvExitWindowsEx",
63 "SrvEndTask",
64 "SrvLogon",
65 "SrvRegisterServicesProcess",
66 "SrvActivateDebugger",
67 "SrvGetThreadConsoleDesktop",
68 "SrvDeviceEvent",
69 "SrvRegisterLogonProcess",
70 "SrvCreateSystemThreads",
71 "SrvRecordShutdownReason",
72 // "SrvCancelShutdown",
73 // "SrvConsoleHandleOperation",
74 // "SrvGetSetShutdownBlockReason",
75 };
76
77
78 /* FUNCTIONS ******************************************************************/
79
80 // PUSER_SOUND_SENTRY. Used in basesrv.dll
81 BOOL WINAPI _UserSoundSentry(VOID)
82 {
83 // TODO: Do something.
84 return TRUE;
85 }
86
87 ULONG
88 InitializeVideoAddressSpace(VOID)
89 {
90 OBJECT_ATTRIBUTES ObjectAttributes;
91 UNICODE_STRING PhysMemName = RTL_CONSTANT_STRING(L"\\Device\\PhysicalMemory");
92 NTSTATUS Status;
93 HANDLE PhysMemHandle;
94 PVOID BaseAddress;
95 LARGE_INTEGER Offset;
96 SIZE_T ViewSize;
97 CHAR IVTAndBda[1024+256];
98
99 /* Free the 1MB pre-reserved region. In reality, ReactOS should simply support us mapping the view into the reserved area, but it doesn't. */
100 BaseAddress = 0;
101 ViewSize = 1024 * 1024;
102 Status = ZwFreeVirtualMemory(NtCurrentProcess(),
103 &BaseAddress,
104 &ViewSize,
105 MEM_RELEASE);
106 if (!NT_SUCCESS(Status))
107 {
108 DPRINT1("Couldn't unmap reserved memory (%x)\n", Status);
109 return 0;
110 }
111
112 /* Open the physical memory section */
113 InitializeObjectAttributes(&ObjectAttributes,
114 &PhysMemName,
115 0,
116 NULL,
117 NULL);
118 Status = ZwOpenSection(&PhysMemHandle,
119 SECTION_ALL_ACCESS,
120 &ObjectAttributes);
121 if (!NT_SUCCESS(Status))
122 {
123 DPRINT1("Couldn't open \\Device\\PhysicalMemory\n");
124 return 0;
125 }
126
127 /* Map the BIOS and device registers into the address space */
128 Offset.QuadPart = 0xa0000;
129 ViewSize = 0x100000 - 0xa0000;
130 BaseAddress = (PVOID)0xa0000;
131 Status = ZwMapViewOfSection(PhysMemHandle,
132 NtCurrentProcess(),
133 &BaseAddress,
134 0,
135 ViewSize,
136 &Offset,
137 &ViewSize,
138 ViewUnmap,
139 0,
140 PAGE_EXECUTE_READWRITE);
141 if (!NT_SUCCESS(Status))
142 {
143 DPRINT1("Couldn't map physical memory (%x)\n", Status);
144 ZwClose(PhysMemHandle);
145 return 0;
146 }
147
148 /* Close physical memory section handle */
149 ZwClose(PhysMemHandle);
150
151 if (BaseAddress != (PVOID)0xa0000)
152 {
153 DPRINT1("Couldn't map physical memory at the right address (was %x)\n",
154 BaseAddress);
155 return 0;
156 }
157
158 /* Allocate some low memory to use for the non-BIOS
159 * parts of the v86 mode address space
160 */
161 BaseAddress = (PVOID)0x1;
162 ViewSize = 0xa0000 - 0x1000;
163 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
164 &BaseAddress,
165 0,
166 &ViewSize,
167 MEM_RESERVE | MEM_COMMIT,
168 PAGE_EXECUTE_READWRITE);
169 if (!NT_SUCCESS(Status))
170 {
171 DPRINT1("Failed to allocate virtual memory (Status %x)\n", Status);
172 return 0;
173 }
174 if (BaseAddress != (PVOID)0x0)
175 {
176 DPRINT1("Failed to allocate virtual memory at right address (was %x)\n",
177 BaseAddress);
178 return 0;
179 }
180
181 /* Get the real mode IVT and BDA from the kernel */
182 Status = NtVdmControl(VdmInitialize, IVTAndBda);
183 if (!NT_SUCCESS(Status))
184 {
185 DPRINT1("NtVdmControl failed (status %x)\n", Status);
186 return 0;
187 }
188
189 /* Return success */
190 return 1;
191 }
192
193 /**********************************************************************
194 * UserpInitVideo
195 *
196 * TODO: we need a virtual device for sessions other than
197 * TODO: the console one
198 */
199 NTSTATUS
200 UserpInitVideo(VOID)
201 {
202 OBJECT_ATTRIBUTES ObjectAttributes;
203 UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\??\\DISPLAY1");
204 IO_STATUS_BLOCK Iosb;
205 HANDLE VideoHandle = (HANDLE) 0;
206 NTSTATUS Status = STATUS_SUCCESS;
207
208 DPRINT("CSR: %s called\n", __FUNCTION__);
209
210 InitializeVideoAddressSpace();
211
212 InitializeObjectAttributes(&ObjectAttributes,
213 &DeviceName,
214 0,
215 NULL,
216 NULL);
217 Status = NtOpenFile(&VideoHandle,
218 FILE_ALL_ACCESS,
219 &ObjectAttributes,
220 &Iosb,
221 0,
222 0);
223 if (NT_SUCCESS(Status))
224 {
225 NtClose(VideoHandle);
226 }
227
228 return Status;
229 }
230
231 // From win32ss/user/win32csr/dllmain.c
232 VOID
233 WINAPI
234 PrivateCsrssManualGuiCheck(LONG Check)
235 {
236 NtUserCallOneParam(Check, ONEPARAM_ROUTINE_CSRSS_GUICHECK);
237 }
238
239 ULONG
240 NTAPI
241 CreateSystemThreads(PVOID pParam)
242 {
243 NtUserCallOneParam((DWORD)pParam, ONEPARAM_ROUTINE_CREATESYSTEMTHREADS);
244 DPRINT1("This thread should not terminate!\n");
245 return 0;
246 }
247
248 CSR_API(SrvCreateSystemThreads)
249 {
250 DPRINT1("%s not yet implemented\n", __FUNCTION__);
251 return STATUS_NOT_IMPLEMENTED;
252 }
253
254 CSR_API(SrvActivateDebugger)
255 {
256 DPRINT1("%s not yet implemented\n", __FUNCTION__);
257 return STATUS_NOT_IMPLEMENTED;
258 }
259
260 CSR_API(SrvGetThreadConsoleDesktop)
261 {
262 DPRINT1("%s not yet implemented\n", __FUNCTION__);
263 return STATUS_NOT_IMPLEMENTED;
264 }
265
266 CSR_API(SrvDeviceEvent)
267 {
268 DPRINT1("%s not yet implemented\n", __FUNCTION__);
269 return STATUS_NOT_IMPLEMENTED;
270 }
271
272 CSR_SERVER_DLL_INIT(UserServerDllInitialization)
273 {
274 /*** From win32csr... ***/
275 HANDLE ServerThread;
276 CLIENT_ID ClientId;
277 NTSTATUS Status;
278 UINT i;
279 /*** END - From win32csr... ***/
280
281 /* Initialize the memory */
282 UserServerHeap = RtlGetProcessHeap();
283
284 /* Initialize the video */
285 UserpInitVideo();
286 NtUserInitialize(0, NULL, NULL);
287 PrivateCsrssManualGuiCheck(0);
288
289 /* Setup the DLL Object */
290 LoadedServerDll->ApiBase = USERSRV_FIRST_API_NUMBER;
291 LoadedServerDll->HighestApiSupported = UserpMaxApiNumber;
292 LoadedServerDll->DispatchTable = UserServerApiDispatchTable;
293 LoadedServerDll->ValidTable = UserServerApiServerValidTable;
294 LoadedServerDll->NameTable = UserServerApiNameTable;
295 LoadedServerDll->SizeOfProcessData = 0;
296 LoadedServerDll->ConnectCallback = NULL;
297 LoadedServerDll->DisconnectCallback = NULL;
298 LoadedServerDll->HardErrorCallback = UserServerHardError;
299 LoadedServerDll->ShutdownProcessCallback = NULL;
300
301 UserServerDllInstance = LoadedServerDll->ServerHandle;
302
303 /*** From win32csr... See r54125 ***/
304 /* Start the Raw Input Thread and the Desktop Thread */
305 for (i = 0; i < 2; ++i)
306 {
307 Status = RtlCreateUserThread(NtCurrentProcess(),
308 NULL, TRUE, 0, 0, 0,
309 CreateSystemThreads,
310 (PVOID)i, &ServerThread, &ClientId);
311 if (NT_SUCCESS(Status))
312 {
313 NtResumeThread(ServerThread, NULL);
314 NtClose(ServerThread);
315 }
316 else
317 DPRINT1("Cannot start Raw Input Thread!\n");
318 }
319 /*** END - From win32csr... ***/
320
321 /* All done */
322 return STATUS_SUCCESS;
323 }
324
325 /* EOF */