3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
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.
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.
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.
19 /* $Id: main.c,v 1.118 2002/04/26 13:11:28 ekohl Exp $
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/main.c
23 * PURPOSE: Initalizes the kernel
24 * PROGRAMMER: David Welch (welch@cwcom.net)
29 /* INCLUDES *****************************************************************/
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>
52 #include <internal/ntosdbg.h>
58 #include <internal/debug.h>
60 /* GLOBALS *******************************************************************/
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
;
76 extern PVOID Ki386InitialStackArray
[MAXIMUM_PROCESSORS
];
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},
104 /* FUNCTIONS ****************************************************************/
108 VOID
CreateDefaultRegistryForLegacyDriver(
112 WCHAR LegacyDriver
[] = L
"LegacyDriver";
114 WCHAR InstancePath
[MAX_PATH
];
115 WCHAR KeyNameBuffer
[MAX_PATH
];
116 WCHAR Name
[MAX_PATH
];
117 UNICODE_STRING KeyName
;
124 WCHAR ImagePath
[MAX_PATH
];
127 wcscpy(Name
, Service
->ServiceName
);
129 wcscpy(InstancePath
, L
"Root\\LEGACY_");
130 wcscat(InstancePath
, Name
);
131 wcscat(InstancePath
, L
"\\0000");
133 wcscpy(KeyNameBuffer
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
134 wcscat(KeyNameBuffer
, InstancePath
);
136 RtlInitUnicodeString(&KeyName
, KeyNameBuffer
);
138 DPRINT("Key name is %S\n", KeyName
.Buffer
);
140 Status
= RtlpCreateRegistryKeyPath(KeyName
.Buffer
);
141 if (!NT_SUCCESS(Status
))
143 DPRINT1("RtlpCreateRegistryKeyPath() failed with status %x\n", Status
);
147 Status
= RtlpGetRegistryHandle(
152 if (!NT_SUCCESS(Status
))
154 DPRINT1("RtlpGetRegistryHandle() failed (Status %x)\n", Status
);
159 Length
= sizeof(DWORD
);
160 Status
= RtlWriteRegistryValue(
167 if (!NT_SUCCESS(Status
))
169 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
174 Length
= (wcslen(LegacyDriver
) + 1) * sizeof(WCHAR
);
175 Status
= RtlWriteRegistryValue(
182 if (!NT_SUCCESS(Status
))
184 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
189 Length
= (wcslen(Service
->DeviceDesc
) + 1) * sizeof(WCHAR
);
190 Status
= RtlWriteRegistryValue(
197 if (!NT_SUCCESS(Status
))
199 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
205 Length
= Length
= sizeof(DWORD
);
206 Status
= RtlWriteRegistryValue(
213 if (!NT_SUCCESS(Status
))
215 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
220 Length
= (wcslen(Service
->ServiceName
) + 1) * sizeof(WCHAR
);
221 Status
= RtlWriteRegistryValue(
226 Service
->ServiceName
,
228 if (!NT_SUCCESS(Status
))
230 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
238 /* Services section */
240 Status
= RtlpGetRegistryHandle(
241 RTL_REGISTRY_SERVICES
,
242 Service
->ServiceName
,
245 if (!NT_SUCCESS(Status
))
247 DPRINT1("RtlpGetRegistryHandle() failed (Status %x)\n", Status
);
251 Length
= (wcslen(Service
->DeviceDesc
) + 1) * sizeof(WCHAR
);
252 Status
= RtlWriteRegistryValue(
259 if (!NT_SUCCESS(Status
))
261 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
267 Length
= sizeof(DWORD
);
268 Status
= RtlWriteRegistryValue(
275 if (!NT_SUCCESS(Status
))
277 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
282 Length
= (wcslen(Service
->Group
) + 1) * sizeof(WCHAR
);
283 Status
= RtlWriteRegistryValue(
290 if (!NT_SUCCESS(Status
))
292 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
297 wcscpy(ImagePath
, L
"\\SystemRoot\\System32\\drivers\\");
298 wcscat(ImagePath
, Service
->ServiceName
);
299 wcscat(ImagePath
, L
".sys");
301 Length
= (wcslen(ImagePath
) + 1) * sizeof(WCHAR
);
302 Status
= RtlWriteRegistryValue(
309 if (!NT_SUCCESS(Status
))
311 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
316 DwordData
= Service
->Start
;
317 Length
= sizeof(DWORD
);
318 Status
= RtlWriteRegistryValue(
325 if (!NT_SUCCESS(Status
))
327 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
332 DwordData
= Service
->Type
;
333 Length
= sizeof(DWORD
);
334 Status
= RtlWriteRegistryValue(
341 if (!NT_SUCCESS(Status
))
343 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
351 VOID
CreateDefaultRegistry()
356 Status
= RtlpCreateRegistryKeyPath(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
357 if (!NT_SUCCESS(Status
))
359 CPRINT("RtlpCreateRegistryKeyPath() (Status %x)\n", Status
);
363 Status
= RtlpCreateRegistryKeyPath(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");
364 if (!NT_SUCCESS(Status
))
366 CPRINT("RtlpCreateRegistryKeyPath() (Status %x)\n", Status
);
370 for (i
= 0; Services
[i
].ServiceName
!= NULL
; i
++)
372 CreateDefaultRegistryForLegacyDriver(&Services
[i
]);
378 RtlpCheckFileNameExtension(PCHAR FileName
,
383 Ext
= strrchr(FileName
, '.');
384 if ((Extension
== NULL
) || (*Extension
== 0))
391 if (*Extension
!= '.')
394 if (_stricmp(Ext
, Extension
) == 0)
402 InitSystemSharedUserPage (PCSZ ParameterLine
)
404 UNICODE_STRING ArcDeviceName
;
405 UNICODE_STRING ArcName
;
406 UNICODE_STRING BootPath
;
407 UNICODE_STRING DriveDeviceName
;
408 UNICODE_STRING DriveName
;
409 WCHAR DriveNameBuffer
[20];
411 PWCHAR ArcNameBuffer
;
415 OBJECT_ATTRIBUTES ObjectAttributes
;
418 BOOLEAN BootDriveFound
;
422 * The shared user page has been zeroed-out right after creation.
423 * There is NO need to do this again.
426 SharedUserData
->NtProductType
= NtProductWinNt
;
428 BootDriveFound
= FALSE
;
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...]"
437 /* create local parameter line copy */
438 ParamBuffer
= ExAllocatePool (PagedPool
, 256);
439 strcpy (ParamBuffer
, (char *)ParameterLine
);
440 DPRINT("%s\n", ParamBuffer
);
442 /* cut options off */
443 p
= strchr (ParamBuffer
, ' ');
448 DPRINT("%s\n", ParamBuffer
);
451 p
= strchr (ParamBuffer
, '\\');
454 DPRINT("Boot path: %s\n", p
);
455 RtlCreateUnicodeStringFromAsciiz (&BootPath
, p
);
460 DPRINT("Boot path: %s\n", "\\");
461 RtlCreateUnicodeStringFromAsciiz (&BootPath
, "\\");
463 DPRINT("Arc name: %s\n", ParamBuffer
);
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
);
471 /* free ParamBuffer */
472 ExFreePool (ParamBuffer
);
474 /* allocate arc device name string */
475 ArcDeviceName
.Length
= 0;
476 ArcDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
477 ArcDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
479 InitializeObjectAttributes (&ObjectAttributes
,
485 Status
= NtOpenSymbolicLinkObject (&Handle
,
486 SYMBOLIC_LINK_ALL_ACCESS
,
488 RtlFreeUnicodeString (&ArcName
);
489 if (!NT_SUCCESS(Status
))
491 RtlFreeUnicodeString (&BootPath
);
492 RtlFreeUnicodeString (&ArcDeviceName
);
493 CPRINT("NtOpenSymbolicLinkObject() failed (Status %x)\n",
499 Status
= NtQuerySymbolicLinkObject (Handle
,
503 if (!NT_SUCCESS(Status
))
505 RtlFreeUnicodeString (&BootPath
);
506 RtlFreeUnicodeString (&ArcDeviceName
);
507 CPRINT("NtQuerySymbolicObject() failed (Status %x)\n",
512 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length
, &ArcDeviceName
);
515 /* allocate device name string */
516 DriveDeviceName
.Length
= 0;
517 DriveDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
518 DriveDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
520 for (i
= 0; i
< 26; i
++)
522 swprintf (DriveNameBuffer
, L
"\\??\\%C:", 'A' + i
);
523 RtlInitUnicodeString (&DriveName
,
526 InitializeObjectAttributes (&ObjectAttributes
,
532 Status
= NtOpenSymbolicLinkObject (&Handle
,
533 SYMBOLIC_LINK_ALL_ACCESS
,
535 if (!NT_SUCCESS(Status
))
537 DPRINT("Failed to open link %wZ\n",
542 Status
= NtQuerySymbolicLinkObject (Handle
,
545 if (!NT_SUCCESS(Status
))
547 DPRINT("Failed query open link %wZ\n",
551 DPRINT("Opened link: %wZ ==> %wZ\n",
552 &DriveName
, &DriveDeviceName
);
554 if (!RtlCompareUnicodeString (&ArcDeviceName
, &DriveDeviceName
, FALSE
))
556 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i
, &BootPath
);
557 swprintf(SharedUserData
->NtSystemRoot
,
558 L
"%C:%wZ", 'A' + i
, &BootPath
);
560 BootDriveFound
= TRUE
;
565 /* set bit in dos drives bitmap (drive available) */
566 SharedUserData
->DosDeviceMap
|= (1<<i
);
569 RtlFreeUnicodeString (&BootPath
);
570 RtlFreeUnicodeString (&DriveDeviceName
);
571 RtlFreeUnicodeString (&ArcDeviceName
);
573 DPRINT("DosDeviceMap: 0x%x\n", SharedPage
->DosDeviceMap
);
575 if (BootDriveFound
== FALSE
)
577 DbgPrint("No system drive found!\n");
584 VOID
DumpBIOSMemoryMap(VOID
)
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
++)
594 DbgPrint("Range: Base (%08X) Length (%08X) Type (%02X)\n",
595 KeMemoryMap
[i
].BaseAddrLow
,
596 KeMemoryMap
[i
].LengthLow
,
597 KeMemoryMap
[i
].Type
);
607 static ULONG Scratch
;
609 EXCEPTION_DISPOSITION
610 ExpUnhandledException1(
611 PEXCEPTION_RECORD ExceptionRecord
,
612 PEXCEPTION_REGISTRATION ExceptionRegistration
,
614 PVOID DispatcherContext
)
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
);
623 Context
->Eax
= (ULONG
)&Scratch
;
625 return ExceptionContinueExecution
;
629 EXCEPTION_DISPOSITION
630 ExpUnhandledException2(
631 PEXCEPTION_RECORD ExceptionRecord
,
632 PEXCEPTION_REGISTRATION ExceptionRegistration
,
634 PVOID DispatcherContext
)
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
);
644 Context
->Eax
= (ULONG
)&Scratch
;
646 return ExceptionContinueExecution
;
650 return ExceptionContinueSearch
;
657 // Put in mingw headers
661 PEXCEPTION_REGISTRATION RegistrationFrame
,
667 PVOID RegistrationFrame
);
669 extern EXCEPTION_DISPOSITION
672 PEXCEPTION_RECORD ExceptionRecord
,
673 PEXCEPTION_REGISTRATION RegistrationFrame
,
675 PVOID DispatcherContext
);
677 extern EXCEPTION_DISPOSITION
680 PEXCEPTION_RECORD ExceptionRecord
,
681 PEXCEPTION_REGISTRATION RegistrationFrame
,
683 PVOID DispatcherContext
);
687 PRTL_EXCEPTION_REGISTRATION
692 __asm__("movl %%ebp, %0\n\t" : "=a" (Value
));
694 return((PRTL_EXCEPTION_REGISTRATION
)Value
) - 1;
698 PRTL_EXCEPTION_REGISTRATION TestER
;
699 SCOPETABLE_ENTRY ScopeTable
;
700 PEXCEPTION_REGISTRATION OSPtr
;
703 DWORD CDECL
SEHFilterRoutine(VOID
)
705 DbgPrint("Within filter routine.\n");
706 return EXCEPTION_EXECUTE_HANDLER
;
707 //return EXCEPTION_CONTINUE_EXECUTION;
710 VOID CDECL
SEHHandlerRoutine(VOID
)
712 DbgPrint("Within exception handler.\n");
713 DbgPrint("System halted.\n");
720 RTL_EXCEPTION_REGISTRATION ER
;
721 LPEXCEPTION_POINTERS ExceptionPointers
;
722 PVOID StandardESPInFrame
;
724 __asm__ ("movl %%esp,%%eax;" : "=a" (StandardESPInFrame
));
725 DbgPrint("StandardESPInFrame: 0x%X\n", StandardESPInFrame
);
727 ExceptionPointers
= NULL
;
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
);
733 ER
.ScopeTable
= &ScopeTable
;
734 DbgPrint("ER.ScopeTable: 0x%X\n", ER
.ScopeTable
);
736 __asm__ ("movl %%ebp,%%eax;" : "=a" (ER
.Ebp
));
737 DbgPrint("ER.Ebp: 0x%X\n", ER
.Ebp
);
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
);
747 DbgPrint("OSPtr: 0x%X\n", OSPtr
);
749 __asm__ ("movl %0,%%eax;movl %%eax,%%fs:0;" : : "m" (OSPtr
));
751 /*__try1(__except_handler3)*/ if(1) {
752 ER
.TryLevel
= 0; // Entered first try... block
754 DbgPrint("Within guarded section.\n");
755 x
= (PULONG
)0xf2000000; *x
= 0;
756 DbgPrint("After exception.\n");
757 } /* __except1 */ if(0) {
760 DbgPrint("After exception2.\n");
762 __asm__ ("movl %0,%%eax;movl %%eax,%%fs:0;" : : "m" (ER
.OS
.prev
));
763 //KeGetCurrentKPCR()->ExceptionList = ER.OS.prev;
765 DbgPrint("Exiting.\n");
771 ExpInitializeExecutive(VOID
)
780 * Fail at runtime if someone has changed various structures without
781 * updating the offsets used for the assembler code.
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
);
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
);
802 KeLowerIrql(DISPATCH_LEVEL
);
806 MmInit1(FirstKrnlPhysAddr
,
809 (PADDRESS_RANGE
)&KeMemoryMap
,
810 KeMemoryMapRangeCount
);
812 /* create default nls tables */
816 * Initialize the kernel debugger
818 KdInitSystem (0, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
823 KeLowerIrql(PASSIVE_LEVEL
);
826 KeBugCheck(SECURITY_INITIALIZATION_FAILED
);
829 PiInitProcessManager();
833 if (KdPollBreakIn ())
835 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
839 * Display version number and copyright/warranty message
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 "
848 HalDisplayString("conditions. There is absolutely no warranty for "
851 /* Initialize all processors */
852 KeNumberProcessors
= 0;
854 while (!HalAllProcessorsStarted())
856 PVOID ProcessorStack
;
858 if (KeNumberProcessors
!= 0)
860 KePrepareForApplicationProcessorInit(KeNumberProcessors
);
861 PsPrepareForApplicationProcessorInit(KeNumberProcessors
);
863 /* Allocate a stack for use when booting the processor */
864 /* FIXME: The nonpaged memory for the stack is not released after use */
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
++;
873 if (KeNumberProcessors
> 1)
876 "Found %d system processors. [%lu MB Memory]\n",
878 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
883 "Found 1 system processor. [%lu MB Memory]\n",
884 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
886 HalDisplayString(str
);
889 * Initialize various critical subsystems
891 HalInitSystem(1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
896 LdrInitModuleManagement();
897 CmInitializeRegistry();
903 /* Report all resources used by hal */
904 HalReportResourceUsage();
907 * Initalize services loaded at boot time
909 DPRINT("%d files loaded\n",KeLoaderBlock
.ModsCount
);
910 for (i
=0; i
< KeLoaderBlock
.ModsCount
; i
++)
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
);
918 /* Pass 1: load nls files */
919 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
921 name
= (PCHAR
)KeLoaderModules
[i
].String
;
922 if (RtlpCheckFileNameExtension(name
, ".nls"))
929 name
= (PCHAR
)KeLoaderModules
[i
+1].String
;
930 if (RtlpCheckFileNameExtension(name
, ".nls"))
932 Mod2Start
= (ULONG
)KeLoaderModules
[i
+1].ModStart
;
933 Mod2End
= (ULONG
)KeLoaderModules
[i
+1].ModEnd
;
935 name
= (PCHAR
)KeLoaderModules
[i
+2].String
;
936 if (RtlpCheckFileNameExtension(name
, ".nls"))
938 Mod3Start
= (ULONG
)KeLoaderModules
[i
+2].ModStart
;
939 Mod3End
= (ULONG
)KeLoaderModules
[i
+2].ModEnd
;
943 /* Initialize nls sections */
944 RtlpInitNlsSections((ULONG
)KeLoaderModules
[i
].ModStart
,
945 (ULONG
)KeLoaderModules
[i
].ModEnd
,
954 /* Pass 2: load registry chunks passed in */
955 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
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"))
963 CPRINT("Process registry chunk at %08lx\n", start
);
964 CmImportHive((PCHAR
)start
, length
);
969 * Enter the kernel debugger before starting up the boot drivers
975 /* Pass 3: process boot loaded drivers */
976 for (i
=1; i
< KeLoaderBlock
.ModsCount
; i
++)
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"))
984 CPRINT("Processing module '%s' at %08lx, length 0x%08lx\n",
985 name
, start
, length
);
986 LdrProcessDriver((PVOID
)start
, name
, length
);
990 /* Create ARC names for boot devices */
993 /* Create the SystemRoot symbolic link */
994 CPRINT("CommandLine: %s\n", (PUCHAR
)KeLoaderBlock
.CommandLine
);
995 IoCreateSystemRootLink((PUCHAR
)KeLoaderBlock
.CommandLine
);
997 #ifdef DBGPRINT_FILE_LOG
998 /* On the assumption that we can now access disks start up the debug
1001 #endif /* DBGPRINT_FILE_LOG */
1005 CreateDefaultRegistry();
1008 PiInitDefaultLocale();
1011 * Start the motherboard enumerator (the HAL)
1013 HalInitSystem(2, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
1016 * Load boot start drivers
1018 IopLoadBootStartDrivers();
1021 * Load Auto configured drivers
1023 LdrLoadAutoConfigDrivers();
1026 * Assign drive letters
1028 IoAssignDriveLetters ((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
,
1034 * Initialize shared user page:
1035 * - set dos system path, dos device map, etc.
1037 InitSystemSharedUserPage ((PUCHAR
)KeLoaderBlock
.CommandLine
);
1040 KeBugCheck(SECURITY1_INITIALIZATION_FAILED
);
1043 * Launch initial process
1045 LdrLoadInitialProcess();
1047 PsTerminateSystemThread(STATUS_SUCCESS
);
1052 KiSystemStartup(BOOLEAN BootProcessor
)
1054 HalInitSystem (0, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
1059 ExpInitializeExecutive();
1062 /* Do application processor initialization */
1063 KeApplicationProcessorInit();
1064 PsApplicationProcessorInit();
1065 KeLowerIrql(PASSIVE_LEVEL
);
1066 PsIdleThreadMain(NULL
);
1072 _main (ULONG MultiBootMagic
, PLOADER_PARAMETER_BLOCK _LoaderBlock
)
1074 * FUNCTION: Called by the boot loader to start the kernel
1076 * LoaderBlock = Pointer to boot parameters initialized by the boot
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.
1084 ULONG last_kernel_address
;
1085 extern ULONG _bss_end__
;
1090 /* Low level architecture specific initialization */
1094 * Copy the parameters to a local buffer because lowmem will go away
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
;
1102 strcpy(KeLoaderCommandLine
, (PUCHAR
)_LoaderBlock
->CommandLine
);
1103 KeLoaderBlock
.CommandLine
= (ULONG
)KeLoaderCommandLine
;
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
++)
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
];
1120 HalnInitializeDisplay((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
1123 HalBase
= KeLoaderModules
[1].ModStart
;
1124 DriverBase
= KeLoaderModules
[KeLoaderBlock
.ModsCount
- 1].ModEnd
;
1129 LdrSafePEProcessModule((PVOID
)HalBase
, (PVOID
)DriverBase
, (PVOID
)0xC0000000, &DriverSize
);
1131 LdrHalBase
= (ULONG_PTR
)DriverBase
;
1132 last_kernel_address
= DriverBase
+ DriverSize
;
1135 * Process ntoskrnl.exe
1137 LdrSafePEProcessModule((PVOID
)0xC0000000, (PVOID
)0xC0000000, (PVOID
)DriverBase
, &DriverSize
);
1139 FirstKrnlPhysAddr
= KeLoaderModules
[0].ModStart
- 0xc0000000 + 0x200000;
1140 LastKrnlPhysAddr
= last_kernel_address
- 0xc0000000 + 0x200000;
1141 LastKernelAddress
= last_kernel_address
;
1144 /* FIXME: VMware does not like it when ReactOS is using the BIOS memory map */
1145 KeLoaderBlock
.Flags
&= ~MB_FLAGS_MMAP_INFO
;
1148 KeMemoryMapRangeCount
= 0;
1149 if (KeLoaderBlock
.Flags
& MB_FLAGS_MMAP_INFO
)
1151 /* We have a memory map from the nice BIOS */
1152 size
= *((PULONG
)(KeLoaderBlock
.MmapAddr
- sizeof(ULONG
)));
1154 while (i
< KeLoaderBlock
.MmapLength
)
1156 memcpy (&KeMemoryMap
[KeMemoryMapRangeCount
],
1157 (PVOID
)(KeLoaderBlock
.MmapAddr
+ i
),
1158 sizeof(ADDRESS_RANGE
));
1159 KeMemoryMapRangeCount
++;