d983be882a4dbc8736d187908d77e13f182b7b2a
[reactos.git] / reactos / ntoskrnl / ke / main.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: main.c,v 1.118 2002/04/26 13:11:28 ekohl Exp $
20 *
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/main.c
23 * PURPOSE: Initalizes the kernel
24 * PROGRAMMER: David Welch (welch@cwcom.net)
25 * UPDATE HISTORY:
26 * 28/05/98: Created
27 */
28
29 /* INCLUDES *****************************************************************/
30
31 #include <ddk/ntddk.h>
32 #include <internal/ntoskrnl.h>
33 #include <reactos/resource.h>
34 #include <internal/mm.h>
35 #include <internal/module.h>
36 #include <internal/ldr.h>
37 #include <internal/ex.h>
38 #include <internal/ps.h>
39 #include <internal/ke.h>
40 #include <internal/io.h>
41 #include <internal/po.h>
42 #include <internal/cc.h>
43 #include <internal/se.h>
44 #include <internal/v86m.h>
45 #include <internal/kd.h>
46 #include <internal/trap.h>
47 #include "../dbg/kdb.h"
48 #include <internal/registry.h>
49 #include <reactos/bugcodes.h>
50
51 #ifdef HALDBG
52 #include <internal/ntosdbg.h>
53 #else
54 #define ps(args...)
55 #endif
56
57 #define NDEBUG
58 #include <internal/debug.h>
59
60 /* GLOBALS *******************************************************************/
61
62 ULONG EXPORTED NtBuildNumber = KERNEL_VERSION_BUILD;
63 ULONG EXPORTED NtGlobalFlag = 0;
64 CHAR EXPORTED KeNumberProcessors;
65 LOADER_PARAMETER_BLOCK EXPORTED KeLoaderBlock;
66 static LOADER_MODULE KeLoaderModules[64];
67 static UCHAR KeLoaderModuleStrings[64][256];
68 static UCHAR KeLoaderCommandLine[256];
69 static ADDRESS_RANGE KeMemoryMap[64];
70 static ULONG KeMemoryMapRangeCount;
71 static ULONG FirstKrnlPhysAddr;
72 static ULONG LastKrnlPhysAddr;
73 static ULONG LastKernelAddress;
74 volatile BOOLEAN Initialized = FALSE;
75
76 extern PVOID Ki386InitialStackArray[MAXIMUM_PROCESSORS];
77
78 typedef struct
79 {
80 LPWSTR ServiceName;
81 LPWSTR DeviceDesc;
82 LPWSTR Group;
83 DWORD Start;
84 DWORD Type;
85 } SERVICE, *PSERVICE;
86
87 SERVICE Services[] = {
88 {L"pci", L"PCI Bus Driver", L"Boot Bus Extender", 0, 1},
89 {L"keyboard", L"Standard Keyboard Driver", L"Base", 0, 1},
90 {L"blue", L"Bluescreen Driver", L"Base", 0, 1},
91 {L"vidport", L"Video Port Driver", L"Base", 0, 1},
92 {L"vgamp", L"VGA Miniport", L"Base", 0, 1},
93 {L"minixfs", L"Minix File System", L"File system", 0, 1},
94 {L"msfs", L"Mail Slot File System", L"File system", 0, 1},
95 {L"npfs", L"Named Pipe File System", L"File system", 0, 1},
96 {L"psaux", L"PS/2 Auxillary Port Driver", L"", 0, 1},
97 {L"mouclass", L"Mouse Class Driver", L"Pointer Class", 0, 1},
98 {L"ndis", L"NDIS System Driver", L"NDIS Wrapper", 0, 1},
99 {L"ne2000", L"Novell Eagle 2000 Driver", L"NDIS", 0, 1},
100 {L"afd", L"AFD Networking Support Environment", L"TDI", 0, 1},
101 {NULL,}
102 };
103
104 /* FUNCTIONS ****************************************************************/
105
106 #define FULLREG
107
108 VOID CreateDefaultRegistryForLegacyDriver(
109 PSERVICE Service)
110 {
111 #ifdef FULLREG
112 WCHAR LegacyDriver[] = L"LegacyDriver";
113 #endif
114 WCHAR InstancePath[MAX_PATH];
115 WCHAR KeyNameBuffer[MAX_PATH];
116 WCHAR Name[MAX_PATH];
117 UNICODE_STRING KeyName;
118 HANDLE KeyHandle;
119 #ifdef FULLREG
120 DWORD DwordData;
121 #endif
122 ULONG Length;
123 NTSTATUS Status;
124 WCHAR ImagePath[MAX_PATH];
125
126 /* Enum section */
127 wcscpy(Name, Service->ServiceName);
128 _wcsupr(Name);
129 wcscpy(InstancePath, L"Root\\LEGACY_");
130 wcscat(InstancePath, Name);
131 wcscat(InstancePath, L"\\0000");
132
133 wcscpy(KeyNameBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
134 wcscat(KeyNameBuffer, InstancePath);
135
136 RtlInitUnicodeString(&KeyName, KeyNameBuffer);
137
138 DPRINT("Key name is %S\n", KeyName.Buffer);
139
140 Status = RtlpCreateRegistryKeyPath(KeyName.Buffer);
141 if (!NT_SUCCESS(Status))
142 {
143 DPRINT1("RtlpCreateRegistryKeyPath() failed with status %x\n", Status);
144 return;
145 }
146
147 Status = RtlpGetRegistryHandle(
148 RTL_REGISTRY_ENUM,
149 InstancePath,
150 TRUE,
151 &KeyHandle);
152 if (!NT_SUCCESS(Status))
153 {
154 DPRINT1("RtlpGetRegistryHandle() failed (Status %x)\n", Status);
155 return;
156 }
157 #ifdef FULLREG
158 DwordData = 0;
159 Length = sizeof(DWORD);
160 Status = RtlWriteRegistryValue(
161 RTL_REGISTRY_HANDLE,
162 (PWSTR)KeyHandle,
163 L"Capabilities",
164 REG_DWORD,
165 (LPWSTR)&DwordData,
166 Length);
167 if (!NT_SUCCESS(Status))
168 {
169 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
170 NtClose(KeyHandle);
171 return;
172 }
173
174 Length = (wcslen(LegacyDriver) + 1) * sizeof(WCHAR);
175 Status = RtlWriteRegistryValue(
176 RTL_REGISTRY_HANDLE,
177 (PWSTR)KeyHandle,
178 L"Class",
179 REG_SZ,
180 LegacyDriver,
181 Length);
182 if (!NT_SUCCESS(Status))
183 {
184 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
185 NtClose(KeyHandle);
186 return;
187 }
188 #endif
189 Length = (wcslen(Service->DeviceDesc) + 1) * sizeof(WCHAR);
190 Status = RtlWriteRegistryValue(
191 RTL_REGISTRY_HANDLE,
192 (PWSTR)KeyHandle,
193 L"DeviceDesc",
194 REG_SZ,
195 Service->DeviceDesc,
196 Length);
197 if (!NT_SUCCESS(Status))
198 {
199 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
200 NtClose(KeyHandle);
201 return;
202 }
203 #ifdef FULLREG
204 DwordData = 0;
205 Length = Length = sizeof(DWORD);
206 Status = RtlWriteRegistryValue(
207 RTL_REGISTRY_HANDLE,
208 (PWSTR)KeyHandle,
209 L"Legacy",
210 REG_DWORD,
211 (LPWSTR)&DwordData,
212 sizeof(DWORD));
213 if (!NT_SUCCESS(Status))
214 {
215 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
216 NtClose(KeyHandle);
217 return;
218 }
219 #endif
220 Length = (wcslen(Service->ServiceName) + 1) * sizeof(WCHAR);
221 Status = RtlWriteRegistryValue(
222 RTL_REGISTRY_HANDLE,
223 (PWSTR)KeyHandle,
224 L"Service",
225 REG_SZ,
226 Service->ServiceName,
227 Length);
228 if (!NT_SUCCESS(Status))
229 {
230 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
231 NtClose(KeyHandle);
232 return;
233 }
234
235 NtClose(KeyHandle);
236
237
238 /* Services section */
239
240 Status = RtlpGetRegistryHandle(
241 RTL_REGISTRY_SERVICES,
242 Service->ServiceName,
243 TRUE,
244 &KeyHandle);
245 if (!NT_SUCCESS(Status))
246 {
247 DPRINT1("RtlpGetRegistryHandle() failed (Status %x)\n", Status);
248 return;
249 }
250 #ifdef FULLREG
251 Length = (wcslen(Service->DeviceDesc) + 1) * sizeof(WCHAR);
252 Status = RtlWriteRegistryValue(
253 RTL_REGISTRY_HANDLE,
254 (PWSTR)KeyHandle,
255 L"DisplayName",
256 REG_SZ,
257 Service->DeviceDesc,
258 Length);
259 if (!NT_SUCCESS(Status))
260 {
261 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
262 NtClose(KeyHandle);
263 return;
264 }
265
266 DwordData = 1;
267 Length = sizeof(DWORD);
268 Status = RtlWriteRegistryValue(
269 RTL_REGISTRY_HANDLE,
270 (PWSTR)KeyHandle,
271 L"ErrorControl",
272 REG_DWORD,
273 (LPWSTR)&DwordData,
274 Length);
275 if (!NT_SUCCESS(Status))
276 {
277 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
278 NtClose(KeyHandle);
279 return;
280 }
281
282 Length = (wcslen(Service->Group) + 1) * sizeof(WCHAR);
283 Status = RtlWriteRegistryValue(
284 RTL_REGISTRY_HANDLE,
285 (PWSTR)KeyHandle,
286 L"Group",
287 REG_SZ,
288 Service->Group,
289 Length);
290 if (!NT_SUCCESS(Status))
291 {
292 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
293 NtClose(KeyHandle);
294 return;
295 }
296 #endif
297 wcscpy(ImagePath, L"\\SystemRoot\\System32\\drivers\\");
298 wcscat(ImagePath, Service->ServiceName);
299 wcscat(ImagePath, L".sys");
300
301 Length = (wcslen(ImagePath) + 1) * sizeof(WCHAR);
302 Status = RtlWriteRegistryValue(
303 RTL_REGISTRY_HANDLE,
304 (PWSTR)KeyHandle,
305 L"ImagePath",
306 REG_SZ,
307 ImagePath,
308 Length);
309 if (!NT_SUCCESS(Status))
310 {
311 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
312 NtClose(KeyHandle);
313 return;
314 }
315 #ifdef FULLREG
316 DwordData = Service->Start;
317 Length = sizeof(DWORD);
318 Status = RtlWriteRegistryValue(
319 RTL_REGISTRY_HANDLE,
320 (PWSTR)KeyHandle,
321 L"Start",
322 REG_DWORD,
323 (LPWSTR)&DwordData,
324 Length);
325 if (!NT_SUCCESS(Status))
326 {
327 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
328 NtClose(KeyHandle);
329 return;
330 }
331
332 DwordData = Service->Type;
333 Length = sizeof(DWORD);
334 Status = RtlWriteRegistryValue(
335 RTL_REGISTRY_HANDLE,
336 (PWSTR)KeyHandle,
337 L"Type",
338 REG_DWORD,
339 (LPWSTR)&DwordData,
340 Length);
341 if (!NT_SUCCESS(Status))
342 {
343 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status);
344 NtClose(KeyHandle);
345 return;
346 }
347 #endif
348 NtClose(KeyHandle);
349 }
350
351 VOID CreateDefaultRegistry()
352 {
353 NTSTATUS Status;
354 ULONG i;
355
356 Status = RtlpCreateRegistryKeyPath(L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
357 if (!NT_SUCCESS(Status))
358 {
359 CPRINT("RtlpCreateRegistryKeyPath() (Status %x)\n", Status);
360 return;
361 }
362
363 Status = RtlpCreateRegistryKeyPath(L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");
364 if (!NT_SUCCESS(Status))
365 {
366 CPRINT("RtlpCreateRegistryKeyPath() (Status %x)\n", Status);
367 return;
368 }
369
370 for (i = 0; Services[i].ServiceName != NULL; i++)
371 {
372 CreateDefaultRegistryForLegacyDriver(&Services[i]);
373 }
374 }
375
376
377 static BOOLEAN
378 RtlpCheckFileNameExtension(PCHAR FileName,
379 PCHAR Extension)
380 {
381 PCHAR Ext;
382
383 Ext = strrchr(FileName, '.');
384 if ((Extension == NULL) || (*Extension == 0))
385 {
386 if (Ext == NULL)
387 return TRUE;
388 else
389 return FALSE;
390 }
391 if (*Extension != '.')
392 Ext++;
393
394 if (_stricmp(Ext, Extension) == 0)
395 return TRUE;
396 else
397 return FALSE;
398 }
399
400
401 static VOID
402 InitSystemSharedUserPage (PCSZ ParameterLine)
403 {
404 UNICODE_STRING ArcDeviceName;
405 UNICODE_STRING ArcName;
406 UNICODE_STRING BootPath;
407 UNICODE_STRING DriveDeviceName;
408 UNICODE_STRING DriveName;
409 WCHAR DriveNameBuffer[20];
410 PCHAR ParamBuffer;
411 PWCHAR ArcNameBuffer;
412 PCHAR p;
413 NTSTATUS Status;
414 ULONG Length;
415 OBJECT_ATTRIBUTES ObjectAttributes;
416 HANDLE Handle;
417 ULONG i;
418 BOOLEAN BootDriveFound;
419
420 /*
421 * NOTE:
422 * The shared user page has been zeroed-out right after creation.
423 * There is NO need to do this again.
424 */
425
426 SharedUserData->NtProductType = NtProductWinNt;
427
428 BootDriveFound = FALSE;
429
430 /*
431 * Retrieve the current dos system path
432 * (e.g.: C:\reactos) from the given arc path
433 * (e.g.: multi(0)disk(0)rdisk(0)partititon(1)\reactos)
434 * Format: "<arc_name>\<path> [options...]"
435 */
436
437 /* create local parameter line copy */
438 ParamBuffer = ExAllocatePool (PagedPool, 256);
439 strcpy (ParamBuffer, (char *)ParameterLine);
440 DPRINT("%s\n", ParamBuffer);
441
442 /* cut options off */
443 p = strchr (ParamBuffer, ' ');
444 if (p)
445 {
446 *p = 0;
447 }
448 DPRINT("%s\n", ParamBuffer);
449
450 /* extract path */
451 p = strchr (ParamBuffer, '\\');
452 if (p)
453 {
454 DPRINT("Boot path: %s\n", p);
455 RtlCreateUnicodeStringFromAsciiz (&BootPath, p);
456 *p = 0;
457 }
458 else
459 {
460 DPRINT("Boot path: %s\n", "\\");
461 RtlCreateUnicodeStringFromAsciiz (&BootPath, "\\");
462 }
463 DPRINT("Arc name: %s\n", ParamBuffer);
464
465 /* Only arc name left - build full arc name */
466 ArcNameBuffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
467 swprintf (ArcNameBuffer, L"\\ArcName\\%S", ParamBuffer);
468 RtlInitUnicodeString (&ArcName, ArcNameBuffer);
469 DPRINT("Arc name: %wZ\n", &ArcName);
470
471 /* free ParamBuffer */
472 ExFreePool (ParamBuffer);
473
474 /* allocate arc device name string */
475 ArcDeviceName.Length = 0;
476 ArcDeviceName.MaximumLength = 256 * sizeof(WCHAR);
477 ArcDeviceName.Buffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
478
479 InitializeObjectAttributes (&ObjectAttributes,
480 &ArcName,
481 0,
482 NULL,
483 NULL);
484
485 Status = NtOpenSymbolicLinkObject (&Handle,
486 SYMBOLIC_LINK_ALL_ACCESS,
487 &ObjectAttributes);
488 RtlFreeUnicodeString (&ArcName);
489 if (!NT_SUCCESS(Status))
490 {
491 RtlFreeUnicodeString (&BootPath);
492 RtlFreeUnicodeString (&ArcDeviceName);
493 CPRINT("NtOpenSymbolicLinkObject() failed (Status %x)\n",
494 Status);
495
496 KeBugCheck (0x0);
497 }
498
499 Status = NtQuerySymbolicLinkObject (Handle,
500 &ArcDeviceName,
501 &Length);
502 NtClose (Handle);
503 if (!NT_SUCCESS(Status))
504 {
505 RtlFreeUnicodeString (&BootPath);
506 RtlFreeUnicodeString (&ArcDeviceName);
507 CPRINT("NtQuerySymbolicObject() failed (Status %x)\n",
508 Status);
509
510 KeBugCheck (0x0);
511 }
512 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length, &ArcDeviceName);
513
514
515 /* allocate device name string */
516 DriveDeviceName.Length = 0;
517 DriveDeviceName.MaximumLength = 256 * sizeof(WCHAR);
518 DriveDeviceName.Buffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
519
520 for (i = 0; i < 26; i++)
521 {
522 swprintf (DriveNameBuffer, L"\\??\\%C:", 'A' + i);
523 RtlInitUnicodeString (&DriveName,
524 DriveNameBuffer);
525
526 InitializeObjectAttributes (&ObjectAttributes,
527 &DriveName,
528 0,
529 NULL,
530 NULL);
531
532 Status = NtOpenSymbolicLinkObject (&Handle,
533 SYMBOLIC_LINK_ALL_ACCESS,
534 &ObjectAttributes);
535 if (!NT_SUCCESS(Status))
536 {
537 DPRINT("Failed to open link %wZ\n",
538 &DriveName);
539 continue;
540 }
541
542 Status = NtQuerySymbolicLinkObject (Handle,
543 &DriveDeviceName,
544 &Length);
545 if (!NT_SUCCESS(Status))
546 {
547 DPRINT("Failed query open link %wZ\n",
548 &DriveName);
549 continue;
550 }
551 DPRINT("Opened link: %wZ ==> %wZ\n",
552 &DriveName, &DriveDeviceName);
553
554 if (!RtlCompareUnicodeString (&ArcDeviceName, &DriveDeviceName, FALSE))
555 {
556 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i, &BootPath);
557 swprintf(SharedUserData->NtSystemRoot,
558 L"%C:%wZ", 'A' + i, &BootPath);
559
560 BootDriveFound = TRUE;
561 }
562
563 NtClose (Handle);
564
565 /* set bit in dos drives bitmap (drive available) */
566 SharedUserData->DosDeviceMap |= (1<<i);
567 }
568
569 RtlFreeUnicodeString (&BootPath);
570 RtlFreeUnicodeString (&DriveDeviceName);
571 RtlFreeUnicodeString (&ArcDeviceName);
572
573 DPRINT("DosDeviceMap: 0x%x\n", SharedPage->DosDeviceMap);
574
575 if (BootDriveFound == FALSE)
576 {
577 DbgPrint("No system drive found!\n");
578 KeBugCheck (0x0);
579 }
580 }
581
582 #ifndef NDEBUG
583
584 VOID DumpBIOSMemoryMap(VOID)
585 {
586 ULONG i;
587
588 DbgPrint("Dumping BIOS memory map:\n");
589 DbgPrint("Memory map base: %d\n", KeLoaderBlock.MmapAddr);
590 DbgPrint("Memory map size: %d\n", KeLoaderBlock.MmapLength);
591 DbgPrint("Address range count: %d\n", KeMemoryMapRangeCount);
592 for (i = 0; i < KeMemoryMapRangeCount; i++)
593 {
594 DbgPrint("Range: Base (%08X) Length (%08X) Type (%02X)\n",
595 KeMemoryMap[i].BaseAddrLow,
596 KeMemoryMap[i].LengthLow,
597 KeMemoryMap[i].Type);
598 }
599 for (;;);
600 }
601
602 #endif /* !NDEBUG */
603
604 #if 1
605 // SEH Test
606
607 static ULONG Scratch;
608
609 EXCEPTION_DISPOSITION
610 ExpUnhandledException1(
611 PEXCEPTION_RECORD ExceptionRecord,
612 PEXCEPTION_REGISTRATION ExceptionRegistration,
613 PCONTEXT Context,
614 PVOID DispatcherContext)
615 {
616 DbgPrint("ExpUnhandledException1() called\n");
617 DbgPrint("ExceptionRecord 0x%X\n", ExceptionRecord);
618 DbgPrint(" Flags 0x%X\n", ExceptionRecord->ExceptionFlags);
619 DbgPrint("ExceptionRegistration 0x%X\n", ExceptionRegistration);
620 DbgPrint("Context 0x%X\n", Context);
621 DbgPrint("DispatcherContext 0x%X\n", DispatcherContext);
622
623 Context->Eax = (ULONG)&Scratch;
624
625 return ExceptionContinueExecution;
626 }
627
628
629 EXCEPTION_DISPOSITION
630 ExpUnhandledException2(
631 PEXCEPTION_RECORD ExceptionRecord,
632 PEXCEPTION_REGISTRATION ExceptionRegistration,
633 PCONTEXT Context,
634 PVOID DispatcherContext)
635 {
636 DbgPrint("ExpUnhandledException2() called\n");
637 DbgPrint("ExceptionRecord 0x%X\n", ExceptionRecord);
638 DbgPrint(" Flags 0x%X\n", ExceptionRecord->ExceptionFlags);
639 DbgPrint("ExceptionRegistration 0x%X\n", ExceptionRegistration);
640 DbgPrint("Context 0x%X\n", Context);
641 DbgPrint("DispatcherContext 0x%X\n", DispatcherContext);
642
643 #if 1
644 Context->Eax = (ULONG)&Scratch;
645
646 return ExceptionContinueExecution;
647
648 #else
649
650 return ExceptionContinueSearch;
651
652 #endif
653 }
654
655
656 #if 1
657 // Put in mingw headers
658 extern VOID
659 CDECL
660 _local_unwind2(
661 PEXCEPTION_REGISTRATION RegistrationFrame,
662 DWORD TryLevel);
663
664 extern VOID
665 CDECL
666 _global_unwind2(
667 PVOID RegistrationFrame);
668
669 extern EXCEPTION_DISPOSITION
670 CDECL
671 _except_handler2(
672 PEXCEPTION_RECORD ExceptionRecord,
673 PEXCEPTION_REGISTRATION RegistrationFrame,
674 PCONTEXT Context,
675 PVOID DispatcherContext);
676
677 extern EXCEPTION_DISPOSITION
678 CDECL
679 _except_handler3(
680 PEXCEPTION_RECORD ExceptionRecord,
681 PEXCEPTION_REGISTRATION RegistrationFrame,
682 PCONTEXT Context,
683 PVOID DispatcherContext);
684
685 #endif
686
687 PRTL_EXCEPTION_REGISTRATION
688 CurrentRER(VOID)
689 {
690 ULONG Value;
691
692 __asm__("movl %%ebp, %0\n\t" : "=a" (Value));
693
694 return((PRTL_EXCEPTION_REGISTRATION)Value) - 1;
695 }
696
697 PULONG x;
698 PRTL_EXCEPTION_REGISTRATION TestER;
699 SCOPETABLE_ENTRY ScopeTable;
700 PEXCEPTION_REGISTRATION OSPtr;
701
702
703 DWORD CDECL SEHFilterRoutine(VOID)
704 {
705 DbgPrint("Within filter routine.\n");
706 return EXCEPTION_EXECUTE_HANDLER;
707 //return EXCEPTION_CONTINUE_EXECUTION;
708 }
709
710 VOID CDECL SEHHandlerRoutine(VOID)
711 {
712 DbgPrint("Within exception handler.\n");
713 DbgPrint("System halted.\n");
714 for (;;);
715 }
716
717
718 VOID SEHTest()
719 {
720 RTL_EXCEPTION_REGISTRATION ER;
721 LPEXCEPTION_POINTERS ExceptionPointers;
722 PVOID StandardESPInFrame;
723
724 __asm__ ("movl %%esp,%%eax;" : "=a" (StandardESPInFrame));
725 DbgPrint("StandardESPInFrame: 0x%X\n", StandardESPInFrame);
726
727 ExceptionPointers = NULL;
728
729 ER.OS.handler = _except_handler3;
730 __asm__ ("movl %%fs:0,%%eax;" : "=a" (ER.OS.prev));
731 DbgPrint("ER.OS.prev: 0x%X\n", ER.OS.prev);
732
733 ER.ScopeTable = &ScopeTable;
734 DbgPrint("ER.ScopeTable: 0x%X\n", ER.ScopeTable);
735 ER.TryLevel = -1;
736 __asm__ ("movl %%ebp,%%eax;" : "=a" (ER.Ebp));
737 DbgPrint("ER.Ebp: 0x%X\n", ER.Ebp);
738
739 ScopeTable.PreviousTryLevel = -1;
740 ScopeTable.FilterRoutine = SEHFilterRoutine;
741 DbgPrint("ScopeTable.FilterRoutine: 0x%X\n", ScopeTable.FilterRoutine);
742 ScopeTable.HandlerRoutine = SEHHandlerRoutine;
743 DbgPrint("ScopeTable.HandlerRoutine: 0x%X\n", ScopeTable.HandlerRoutine);
744
745
746 OSPtr = &ER.OS;
747 DbgPrint("OSPtr: 0x%X\n", OSPtr);
748
749 __asm__ ("movl %0,%%eax;movl %%eax,%%fs:0;" : : "m" (OSPtr));
750
751 /*__try1(__except_handler3)*/ if(1) {
752 ER.TryLevel = 0; // Entered first try... block
753
754 DbgPrint("Within guarded section.\n");
755 x = (PULONG)0xf2000000; *x = 0;
756 DbgPrint("After exception.\n");
757 } /* __except1 */ if(0) {
758 }
759
760 DbgPrint("After exception2.\n");
761
762 __asm__ ("movl %0,%%eax;movl %%eax,%%fs:0;" : : "m" (ER.OS.prev));
763 //KeGetCurrentKPCR()->ExceptionList = ER.OS.prev;
764
765 DbgPrint("Exiting.\n");
766 }
767
768 #endif
769
770 VOID
771 ExpInitializeExecutive(VOID)
772 {
773 ULONG i;
774 ULONG start;
775 ULONG length;
776 PCHAR name;
777 CHAR str[50];
778
779 /*
780 * Fail at runtime if someone has changed various structures without
781 * updating the offsets used for the assembler code.
782 */
783 assert(FIELD_OFFSET(KTHREAD, InitialStack) == KTHREAD_INITIAL_STACK);
784 assert(FIELD_OFFSET(KTHREAD, Teb) == KTHREAD_TEB);
785 assert(FIELD_OFFSET(KTHREAD, KernelStack) == KTHREAD_KERNEL_STACK);
786 assert(FIELD_OFFSET(KTHREAD, PreviousMode) == KTHREAD_PREVIOUS_MODE);
787 assert(FIELD_OFFSET(KTHREAD, TrapFrame) == KTHREAD_TRAP_FRAME);
788 assert(FIELD_OFFSET(KTHREAD, CallbackStack) == KTHREAD_CALLBACK_STACK);
789 assert(FIELD_OFFSET(ETHREAD, ThreadsProcess) == ETHREAD_THREADS_PROCESS);
790 assert(FIELD_OFFSET(KPROCESS, DirectoryTableBase) ==
791 KPROCESS_DIRECTORY_TABLE_BASE);
792 assert(FIELD_OFFSET(KTRAP_FRAME, Reserved9) == KTRAP_FRAME_RESERVED9);
793 assert(FIELD_OFFSET(KV86M_TRAP_FRAME, regs) == TF_REGS);
794 assert(FIELD_OFFSET(KV86M_TRAP_FRAME, orig_ebp) == TF_ORIG_EBP);
795
796 assert(FIELD_OFFSET(KPCR, ExceptionList) == KPCR_EXCEPTION_LIST);
797 assert(FIELD_OFFSET(KPCR, Self) == KPCR_SELF);
798 assert(FIELD_OFFSET(KPCR, CurrentThread) == KPCR_CURRENT_THREAD);
799
800 LdrInit1();
801
802 KeLowerIrql(DISPATCH_LEVEL);
803
804 NtEarlyInitVdm();
805
806 MmInit1(FirstKrnlPhysAddr,
807 LastKrnlPhysAddr,
808 LastKernelAddress,
809 (PADDRESS_RANGE)&KeMemoryMap,
810 KeMemoryMapRangeCount);
811
812 /* create default nls tables */
813 RtlpInitNlsTables();
814
815 /*
816 * Initialize the kernel debugger
817 */
818 KdInitSystem (0, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
819
820 MmInit2();
821 KeInit2();
822
823 KeLowerIrql(PASSIVE_LEVEL);
824
825 if (!SeInit1())
826 KeBugCheck(SECURITY_INITIALIZATION_FAILED);
827
828 ObInit();
829 PiInitProcessManager();
830
831 KdInit1();
832
833 if (KdPollBreakIn ())
834 {
835 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C);
836 }
837
838 /*
839 * Display version number and copyright/warranty message
840 */
841 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR" (Build "
842 KERNEL_VERSION_BUILD_STR")\n");
843 HalDisplayString(RES_STR_LEGAL_COPYRIGHT);
844 HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
845 "Public License, and you\n");
846 HalDisplayString("are welcome to change it and/or distribute copies of it "
847 "under certain\n");
848 HalDisplayString("conditions. There is absolutely no warranty for "
849 "ReactOS.\n\n");
850
851 /* Initialize all processors */
852 KeNumberProcessors = 0;
853
854 while (!HalAllProcessorsStarted())
855 {
856 PVOID ProcessorStack;
857
858 if (KeNumberProcessors != 0)
859 {
860 KePrepareForApplicationProcessorInit(KeNumberProcessors);
861 PsPrepareForApplicationProcessorInit(KeNumberProcessors);
862 }
863 /* Allocate a stack for use when booting the processor */
864 /* FIXME: The nonpaged memory for the stack is not released after use */
865 ProcessorStack =
866 ExAllocatePool(NonPagedPool, MM_STACK_SIZE) + MM_STACK_SIZE;
867 Ki386InitialStackArray[((int)KeNumberProcessors)] =
868 (PVOID)(ProcessorStack - MM_STACK_SIZE);
869 HalInitializeProcessor(KeNumberProcessors, ProcessorStack);
870 KeNumberProcessors++;
871 }
872
873 if (KeNumberProcessors > 1)
874 {
875 sprintf(str,
876 "Found %d system processors. [%lu MB Memory]\n",
877 KeNumberProcessors,
878 (KeLoaderBlock.MemHigher + 1088)/ 1024);
879 }
880 else
881 {
882 sprintf(str,
883 "Found 1 system processor. [%lu MB Memory]\n",
884 (KeLoaderBlock.MemHigher + 1088)/ 1024);
885 }
886 HalDisplayString(str);
887
888 /*
889 * Initialize various critical subsystems
890 */
891 HalInitSystem(1, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
892
893 ExInit();
894 IoInit();
895 PoInit();
896 LdrInitModuleManagement();
897 CmInitializeRegistry();
898 NtInit();
899 MmInit3();
900 CcInit();
901 KdInit2();
902
903 /* Report all resources used by hal */
904 HalReportResourceUsage();
905
906 /*
907 * Initalize services loaded at boot time
908 */
909 DPRINT("%d files loaded\n",KeLoaderBlock.ModsCount);
910 for (i=0; i < KeLoaderBlock.ModsCount; i++)
911 {
912 CPRINT("Module: '%s' at %08lx, length 0x%08lx\n",
913 KeLoaderModules[i].String,
914 KeLoaderModules[i].ModStart,
915 KeLoaderModules[i].ModEnd - KeLoaderModules[i].ModStart);
916 }
917
918 /* Pass 1: load nls files */
919 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
920 {
921 name = (PCHAR)KeLoaderModules[i].String;
922 if (RtlpCheckFileNameExtension(name, ".nls"))
923 {
924 ULONG Mod2Start = 0;
925 ULONG Mod2End = 0;
926 ULONG Mod3Start = 0;
927 ULONG Mod3End = 0;
928
929 name = (PCHAR)KeLoaderModules[i+1].String;
930 if (RtlpCheckFileNameExtension(name, ".nls"))
931 {
932 Mod2Start = (ULONG)KeLoaderModules[i+1].ModStart;
933 Mod2End = (ULONG)KeLoaderModules[i+1].ModEnd;
934
935 name = (PCHAR)KeLoaderModules[i+2].String;
936 if (RtlpCheckFileNameExtension(name, ".nls"))
937 {
938 Mod3Start = (ULONG)KeLoaderModules[i+2].ModStart;
939 Mod3End = (ULONG)KeLoaderModules[i+2].ModEnd;
940 }
941 }
942
943 /* Initialize nls sections */
944 RtlpInitNlsSections((ULONG)KeLoaderModules[i].ModStart,
945 (ULONG)KeLoaderModules[i].ModEnd,
946 Mod2Start,
947 Mod2End,
948 Mod3Start,
949 Mod3End);
950 break;
951 }
952 }
953
954 /* Pass 2: load registry chunks passed in */
955 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
956 {
957 start = KeLoaderModules[i].ModStart;
958 length = KeLoaderModules[i].ModEnd - start;
959 name = (PCHAR)KeLoaderModules[i].String;
960 if (RtlpCheckFileNameExtension(name, "") ||
961 RtlpCheckFileNameExtension(name, ".hiv"))
962 {
963 CPRINT("Process registry chunk at %08lx\n", start);
964 CmImportHive((PCHAR)start, length);
965 }
966 }
967
968 /*
969 * Enter the kernel debugger before starting up the boot drivers
970 */
971 #ifdef KDBG
972 KdbEnter();
973 #endif /* KDBG */
974
975 /* Pass 3: process boot loaded drivers */
976 for (i=1; i < KeLoaderBlock.ModsCount; i++)
977 {
978 start = KeLoaderModules[i].ModStart;
979 length = KeLoaderModules[i].ModEnd - start;
980 name = (PCHAR)KeLoaderModules[i].String;
981 if (RtlpCheckFileNameExtension(name, ".sys") ||
982 RtlpCheckFileNameExtension(name, ".sym"))
983 {
984 CPRINT("Processing module '%s' at %08lx, length 0x%08lx\n",
985 name, start, length);
986 LdrProcessDriver((PVOID)start, name, length);
987 }
988 }
989
990 /* Create ARC names for boot devices */
991 IoCreateArcNames();
992
993 /* Create the SystemRoot symbolic link */
994 CPRINT("CommandLine: %s\n", (PUCHAR)KeLoaderBlock.CommandLine);
995 IoCreateSystemRootLink((PUCHAR)KeLoaderBlock.CommandLine);
996
997 #ifdef DBGPRINT_FILE_LOG
998 /* On the assumption that we can now access disks start up the debug
999 logger thread */
1000 DebugLogInit2();
1001 #endif /* DBGPRINT_FILE_LOG */
1002
1003
1004 #if 0
1005 CreateDefaultRegistry();
1006 #endif
1007
1008 PiInitDefaultLocale();
1009
1010 /*
1011 * Start the motherboard enumerator (the HAL)
1012 */
1013 HalInitSystem(2, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
1014 #if 0
1015 /*
1016 * Load boot start drivers
1017 */
1018 IopLoadBootStartDrivers();
1019 #else
1020 /*
1021 * Load Auto configured drivers
1022 */
1023 LdrLoadAutoConfigDrivers();
1024 #endif
1025 /*
1026 * Assign drive letters
1027 */
1028 IoAssignDriveLetters ((PLOADER_PARAMETER_BLOCK)&KeLoaderBlock,
1029 NULL,
1030 NULL,
1031 NULL);
1032
1033 /*
1034 * Initialize shared user page:
1035 * - set dos system path, dos device map, etc.
1036 */
1037 InitSystemSharedUserPage ((PUCHAR)KeLoaderBlock.CommandLine);
1038
1039 if (!SeInit2())
1040 KeBugCheck(SECURITY1_INITIALIZATION_FAILED);
1041
1042 /*
1043 * Launch initial process
1044 */
1045 LdrLoadInitialProcess();
1046
1047 PsTerminateSystemThread(STATUS_SUCCESS);
1048 }
1049
1050
1051 VOID
1052 KiSystemStartup(BOOLEAN BootProcessor)
1053 {
1054 HalInitSystem (0, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
1055
1056 if (BootProcessor)
1057 {
1058 /* Never returns */
1059 ExpInitializeExecutive();
1060 KeBugCheck(0);
1061 }
1062 /* Do application processor initialization */
1063 KeApplicationProcessorInit();
1064 PsApplicationProcessorInit();
1065 KeLowerIrql(PASSIVE_LEVEL);
1066 PsIdleThreadMain(NULL);
1067 KeBugCheck(0);
1068 for(;;);
1069 }
1070
1071 VOID
1072 _main (ULONG MultiBootMagic, PLOADER_PARAMETER_BLOCK _LoaderBlock)
1073 /*
1074 * FUNCTION: Called by the boot loader to start the kernel
1075 * ARGUMENTS:
1076 * LoaderBlock = Pointer to boot parameters initialized by the boot
1077 * loader
1078 * NOTE: The boot parameters are stored in low memory which will become
1079 * invalid after the memory managment is initialized so we make a local copy.
1080 */
1081 {
1082 ULONG i;
1083 ULONG size;
1084 ULONG last_kernel_address;
1085 extern ULONG _bss_end__;
1086 ULONG HalBase;
1087 ULONG DriverBase;
1088 ULONG DriverSize;
1089
1090 /* Low level architecture specific initialization */
1091 KeInit1();
1092
1093 /*
1094 * Copy the parameters to a local buffer because lowmem will go away
1095 */
1096 memcpy(&KeLoaderBlock, _LoaderBlock, sizeof(LOADER_PARAMETER_BLOCK));
1097 memcpy(&KeLoaderModules[1], (PVOID)KeLoaderBlock.ModsAddr,
1098 sizeof(LOADER_MODULE) * KeLoaderBlock.ModsCount);
1099 KeLoaderBlock.ModsCount++;
1100 KeLoaderBlock.ModsAddr = (ULONG)&KeLoaderModules;
1101
1102 strcpy(KeLoaderCommandLine, (PUCHAR)_LoaderBlock->CommandLine);
1103 KeLoaderBlock.CommandLine = (ULONG)KeLoaderCommandLine;
1104
1105 strcpy(KeLoaderModuleStrings[0], "ntoskrnl.exe");
1106 KeLoaderModules[0].String = (ULONG)KeLoaderModuleStrings[0];
1107 KeLoaderModules[0].ModStart = 0xC0000000;
1108 KeLoaderModules[0].ModEnd = PAGE_ROUND_UP((ULONG)&_bss_end__);
1109 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
1110 {
1111 strcpy(KeLoaderModuleStrings[i], (PUCHAR)KeLoaderModules[i].String);
1112 KeLoaderModules[i].ModStart -= 0x200000;
1113 KeLoaderModules[i].ModStart += 0xc0000000;
1114 KeLoaderModules[i].ModEnd -= 0x200000;
1115 KeLoaderModules[i].ModEnd += 0xc0000000;
1116 KeLoaderModules[i].String = (ULONG)KeLoaderModuleStrings[i];
1117 }
1118
1119 #ifdef HAL_DBG
1120 HalnInitializeDisplay((PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
1121 #endif
1122
1123 HalBase = KeLoaderModules[1].ModStart;
1124 DriverBase = KeLoaderModules[KeLoaderBlock.ModsCount - 1].ModEnd;
1125
1126 /*
1127 * Process hal.dll
1128 */
1129 LdrSafePEProcessModule((PVOID)HalBase, (PVOID)DriverBase, (PVOID)0xC0000000, &DriverSize);
1130
1131 LdrHalBase = (ULONG_PTR)DriverBase;
1132 last_kernel_address = DriverBase + DriverSize;
1133
1134 /*
1135 * Process ntoskrnl.exe
1136 */
1137 LdrSafePEProcessModule((PVOID)0xC0000000, (PVOID)0xC0000000, (PVOID)DriverBase, &DriverSize);
1138
1139 FirstKrnlPhysAddr = KeLoaderModules[0].ModStart - 0xc0000000 + 0x200000;
1140 LastKrnlPhysAddr = last_kernel_address - 0xc0000000 + 0x200000;
1141 LastKernelAddress = last_kernel_address;
1142
1143 #ifndef ACPI
1144 /* FIXME: VMware does not like it when ReactOS is using the BIOS memory map */
1145 KeLoaderBlock.Flags &= ~MB_FLAGS_MMAP_INFO;
1146 #endif
1147
1148 KeMemoryMapRangeCount = 0;
1149 if (KeLoaderBlock.Flags & MB_FLAGS_MMAP_INFO)
1150 {
1151 /* We have a memory map from the nice BIOS */
1152 size = *((PULONG)(KeLoaderBlock.MmapAddr - sizeof(ULONG)));
1153 i = 0;
1154 while (i < KeLoaderBlock.MmapLength)
1155 {
1156 memcpy (&KeMemoryMap[KeMemoryMapRangeCount],
1157 (PVOID)(KeLoaderBlock.MmapAddr + i),
1158 sizeof(ADDRESS_RANGE));
1159 KeMemoryMapRangeCount++;
1160 i += size;
1161 }
1162 }
1163
1164 KiSystemStartup(1);
1165 }
1166
1167 /* EOF */
1168