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.174 2003/10/12 17:05:45 hbirr 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 #define NTOS_MODE_KERNEL
33 #include <internal/ntoskrnl.h>
34 #include <reactos/resource.h>
35 #include <internal/mm.h>
36 #include <internal/ifs.h>
37 #include <internal/module.h>
38 #include <internal/ldr.h>
39 #include <internal/ex.h>
40 #include <internal/ps.h>
41 #include <internal/ke.h>
42 #include <internal/io.h>
43 #include <internal/po.h>
44 #include <internal/cc.h>
45 #include <internal/se.h>
46 #include <internal/v86m.h>
47 #include <internal/kd.h>
48 #include <internal/trap.h>
49 #include "../dbg/kdb.h"
50 #include <internal/registry.h>
51 #include <internal/nls.h>
52 #include <reactos/bugcodes.h>
53 #include <ntos/bootvid.h>
54 #include <napi/core.h>
57 #include <internal/ntosdbg.h>
63 #include <internal/debug.h>
65 /* GLOBALS *******************************************************************/
67 ULONG EXPORTED NtBuildNumber
= KERNEL_VERSION_BUILD
;
68 ULONG EXPORTED NtGlobalFlag
= 0;
69 CHAR EXPORTED KeNumberProcessors
;
70 LOADER_PARAMETER_BLOCK EXPORTED KeLoaderBlock
;
71 ULONG EXPORTED KeDcacheFlushCount
= 0;
72 ULONG EXPORTED KeIcacheFlushCount
= 0;
73 static LOADER_MODULE KeLoaderModules
[64];
74 static UCHAR KeLoaderModuleStrings
[64][256];
75 static UCHAR KeLoaderCommandLine
[256];
76 static ADDRESS_RANGE KeMemoryMap
[64];
77 static ULONG KeMemoryMapRangeCount
;
78 static ULONG FirstKrnlPhysAddr
;
79 static ULONG LastKrnlPhysAddr
;
80 static ULONG LastKernelAddress
;
81 volatile BOOLEAN Initialized
= FALSE
;
82 extern ULONG MmCoreDumpType
;
84 extern PVOID Ki386InitialStackArray
[MAXIMUM_PROCESSORS
];
87 /* FUNCTIONS ****************************************************************/
89 static BOOLEAN INIT_FUNCTION
90 RtlpCheckFileNameExtension(PCHAR FileName
,
95 Ext
= strrchr(FileName
, '.');
98 if ((Extension
== NULL
) || (*Extension
== 0))
104 if (*Extension
!= '.')
107 if (_stricmp(Ext
, Extension
) == 0)
114 static VOID INIT_FUNCTION
115 InitSystemSharedUserPage (PCSZ ParameterLine
)
117 UNICODE_STRING ArcDeviceName
;
118 UNICODE_STRING ArcName
;
119 UNICODE_STRING BootPath
;
120 UNICODE_STRING DriveDeviceName
;
121 UNICODE_STRING DriveName
;
122 WCHAR DriveNameBuffer
[20];
124 PWCHAR ArcNameBuffer
;
128 OBJECT_ATTRIBUTES ObjectAttributes
;
131 BOOLEAN BootDriveFound
;
135 * The shared user page has been zeroed-out right after creation.
136 * There is NO need to do this again.
139 SharedUserData
->NtProductType
= NtProductWinNt
;
141 BootDriveFound
= FALSE
;
144 * Retrieve the current dos system path
145 * (e.g.: C:\reactos) from the given arc path
146 * (e.g.: multi(0)disk(0)rdisk(0)partititon(1)\reactos)
147 * Format: "<arc_name>\<path> [options...]"
150 /* create local parameter line copy */
151 ParamBuffer
= ExAllocatePool (PagedPool
, 256);
152 strcpy (ParamBuffer
, (char *)ParameterLine
);
153 DPRINT("%s\n", ParamBuffer
);
155 /* cut options off */
156 p
= strchr (ParamBuffer
, ' ');
161 DPRINT("%s\n", ParamBuffer
);
164 p
= strchr (ParamBuffer
, '\\');
167 DPRINT("Boot path: %s\n", p
);
168 RtlCreateUnicodeStringFromAsciiz (&BootPath
, p
);
173 DPRINT("Boot path: %s\n", "\\");
174 RtlCreateUnicodeStringFromAsciiz (&BootPath
, "\\");
176 DPRINT("Arc name: %s\n", ParamBuffer
);
178 /* Only arc name left - build full arc name */
179 ArcNameBuffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
180 swprintf (ArcNameBuffer
, L
"\\ArcName\\%S", ParamBuffer
);
181 RtlInitUnicodeString (&ArcName
, ArcNameBuffer
);
182 DPRINT("Arc name: %wZ\n", &ArcName
);
184 /* free ParamBuffer */
185 ExFreePool (ParamBuffer
);
187 /* allocate arc device name string */
188 ArcDeviceName
.Length
= 0;
189 ArcDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
190 ArcDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
192 InitializeObjectAttributes (&ObjectAttributes
,
198 Status
= NtOpenSymbolicLinkObject (&Handle
,
199 SYMBOLIC_LINK_ALL_ACCESS
,
201 RtlFreeUnicodeString (&ArcName
);
202 if (!NT_SUCCESS(Status
))
204 RtlFreeUnicodeString (&BootPath
);
205 RtlFreeUnicodeString (&ArcDeviceName
);
206 CPRINT("NtOpenSymbolicLinkObject() failed (Status %x)\n",
212 Status
= NtQuerySymbolicLinkObject (Handle
,
216 if (!NT_SUCCESS(Status
))
218 RtlFreeUnicodeString (&BootPath
);
219 RtlFreeUnicodeString (&ArcDeviceName
);
220 CPRINT("NtQuerySymbolicObject() failed (Status %x)\n",
225 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length
, &ArcDeviceName
);
228 /* allocate device name string */
229 DriveDeviceName
.Length
= 0;
230 DriveDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
231 DriveDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
233 for (i
= 0; i
< 26; i
++)
235 swprintf (DriveNameBuffer
, L
"\\??\\%C:", 'A' + i
);
236 RtlInitUnicodeString (&DriveName
,
239 InitializeObjectAttributes (&ObjectAttributes
,
245 Status
= NtOpenSymbolicLinkObject (&Handle
,
246 SYMBOLIC_LINK_ALL_ACCESS
,
248 if (!NT_SUCCESS(Status
))
250 DPRINT("Failed to open link %wZ\n",
255 Status
= NtQuerySymbolicLinkObject (Handle
,
258 if (!NT_SUCCESS(Status
))
260 DPRINT("Failed query open link %wZ\n",
264 DPRINT("Opened link: %wZ ==> %wZ\n",
265 &DriveName
, &DriveDeviceName
);
267 if (!RtlCompareUnicodeString (&ArcDeviceName
, &DriveDeviceName
, FALSE
))
269 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i
, &BootPath
);
270 swprintf(SharedUserData
->NtSystemRoot
,
271 L
"%C:%wZ", 'A' + i
, &BootPath
);
273 BootDriveFound
= TRUE
;
279 RtlFreeUnicodeString (&BootPath
);
280 RtlFreeUnicodeString (&DriveDeviceName
);
281 RtlFreeUnicodeString (&ArcDeviceName
);
283 DPRINT("DosDeviceMap: 0x%x\n", SharedUserData
->DosDeviceMap
);
285 if (BootDriveFound
== FALSE
)
287 DbgPrint("No system drive found!\n");
292 VOID STATIC INIT_FUNCTION
293 MiFreeBootDriverMemory(PVOID StartAddress
, ULONG Length
)
297 for (i
= 0; i
< PAGE_ROUND_UP(Length
)/PAGE_SIZE
; i
++)
299 MmDeleteVirtualMapping(NULL
, StartAddress
+ i
* PAGE_SIZE
, TRUE
, NULL
, NULL
);
305 ExpInitializeExecutive(VOID
)
307 LARGE_INTEGER Timeout
;
308 HANDLE ProcessHandle
;
310 ULONG BootDriverCount
;
320 BOOLEAN NoBootScreen
= FALSE
;
322 HANDLE InitDoneEventHandle
;
323 OBJECT_ATTRIBUTES ObjectAttributes
;
326 * Fail at runtime if someone has changed various structures without
327 * updating the offsets used for the assembler code.
329 assert(FIELD_OFFSET(KTHREAD
, InitialStack
) == KTHREAD_INITIAL_STACK
);
330 assert(FIELD_OFFSET(KTHREAD
, Teb
) == KTHREAD_TEB
);
331 assert(FIELD_OFFSET(KTHREAD
, KernelStack
) == KTHREAD_KERNEL_STACK
);
332 assert(FIELD_OFFSET(KTHREAD
, ServiceTable
) == KTHREAD_SERVICE_TABLE
);
333 assert(FIELD_OFFSET(KTHREAD
, PreviousMode
) == KTHREAD_PREVIOUS_MODE
);
334 assert(FIELD_OFFSET(KTHREAD
, TrapFrame
) == KTHREAD_TRAP_FRAME
);
335 assert(FIELD_OFFSET(KTHREAD
, CallbackStack
) == KTHREAD_CALLBACK_STACK
);
336 assert(FIELD_OFFSET(ETHREAD
, ThreadsProcess
) == ETHREAD_THREADS_PROCESS
);
337 assert(FIELD_OFFSET(KPROCESS
, DirectoryTableBase
) ==
338 KPROCESS_DIRECTORY_TABLE_BASE
);
339 assert(FIELD_OFFSET(KPROCESS
, IopmOffset
) == KPROCESS_IOPM_OFFSET
);
340 assert(FIELD_OFFSET(KPROCESS
, LdtDescriptor
) == KPROCESS_LDT_DESCRIPTOR0
);
341 assert(FIELD_OFFSET(KTRAP_FRAME
, Reserved9
) == KTRAP_FRAME_RESERVED9
);
342 assert(FIELD_OFFSET(KV86M_TRAP_FRAME
, regs
) == TF_REGS
);
343 assert(FIELD_OFFSET(KV86M_TRAP_FRAME
, orig_ebp
) == TF_ORIG_EBP
);
345 assert(FIELD_OFFSET(KPCR
, Tib
.ExceptionList
) == KPCR_EXCEPTION_LIST
);
346 assert(FIELD_OFFSET(KPCR
, Self
) == KPCR_SELF
);
347 assert(FIELD_OFFSET(IKPCR
, Tib
.ExceptionList
) == KPCR_EXCEPTION_LIST
);
348 assert(FIELD_OFFSET(IKPCR
, Self
) == KPCR_SELF
);
349 assert(FIELD_OFFSET(IKPCR
, CurrentThread
) == KPCR_CURRENT_THREAD
);
353 KeLowerIrql(DISPATCH_LEVEL
);
357 p1
= (PCHAR
)KeLoaderBlock
.CommandLine
;
360 while(*p1
&& (p2
= strchr(p1
, '/')))
363 if (!_strnicmp(p2
, "MAXMEM", 6))
366 while (isspace(*p2
)) p2
++;
370 while(isspace(*p2
)) p2
++;
375 MaxMem
= MaxMem
* 10 + *p2
- '0';
382 else if (!_strnicmp(p2
, "NOBOOTSCREEN", 12))
387 else if (!_strnicmp(p2
, "CRASHDUMP", 9))
393 if (!_strnicmp(p2
, "FULL", 4))
395 MmCoreDumpType
= MM_CORE_DUMP_TYPE_FULL
;
399 MmCoreDumpType
= MM_CORE_DUMP_TYPE_NONE
;
406 MmInit1(FirstKrnlPhysAddr
,
409 (PADDRESS_RANGE
)&KeMemoryMap
,
410 KeMemoryMapRangeCount
,
411 MaxMem
> 8 ? MaxMem
: 4096);
413 /* Import ANSI code page table */
414 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
416 start
= KeLoaderModules
[i
].ModStart
;
417 length
= KeLoaderModules
[i
].ModEnd
- start
;
419 name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\');
422 name
= (PCHAR
)KeLoaderModules
[i
].String
;
429 if (!_stricmp (name
, "ansi.nls"))
431 RtlpImportAnsiCodePage((PUSHORT
)start
, length
);
435 /* Import OEM code page table */
436 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
438 start
= KeLoaderModules
[i
].ModStart
;
439 length
= KeLoaderModules
[i
].ModEnd
- start
;
441 name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\');
444 name
= (PCHAR
)KeLoaderModules
[i
].String
;
451 if (!_stricmp (name
, "oem.nls"))
453 RtlpImportOemCodePage((PUSHORT
)start
, length
);
457 /* Import Unicode casemap table */
458 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
460 start
= KeLoaderModules
[i
].ModStart
;
461 length
= KeLoaderModules
[i
].ModEnd
- start
;
463 name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\');
466 name
= (PCHAR
)KeLoaderModules
[i
].String
;
473 if (!_stricmp (name
, "casemap.nls"))
475 RtlpImportUnicodeCasemap((PUSHORT
)start
, length
);
479 /* Create initial NLS tables */
480 RtlpCreateInitialNlsTables();
483 * Initialize the kernel debugger
485 KdInitSystem (0, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
490 KeLowerIrql(PASSIVE_LEVEL
);
493 KEBUGCHECK(SECURITY_INITIALIZATION_FAILED
);
498 KEBUGCHECK(SECURITY1_INITIALIZATION_FAILED
);
500 PiInitProcessManager();
502 if (KdPollBreakIn ())
504 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
507 /* Initialize all processors */
508 KeNumberProcessors
= 0;
510 while (!HalAllProcessorsStarted())
512 PVOID ProcessorStack
;
514 if (KeNumberProcessors
!= 0)
516 KePrepareForApplicationProcessorInit(KeNumberProcessors
);
517 PsPrepareForApplicationProcessorInit(KeNumberProcessors
);
519 /* Allocate a stack for use when booting the processor */
520 /* FIXME: The nonpaged memory for the stack is not released after use */
522 ExAllocatePool(NonPagedPool
, MM_STACK_SIZE
) + MM_STACK_SIZE
;
523 Ki386InitialStackArray
[((int)KeNumberProcessors
)] =
524 (PVOID
)(ProcessorStack
- MM_STACK_SIZE
);
525 HalInitializeProcessor(KeNumberProcessors
, ProcessorStack
);
526 KeNumberProcessors
++;
530 * Initialize various critical subsystems
532 HalInitSystem(1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
538 LdrInitModuleManagement();
539 CmInitializeRegistry();
544 FsRtlpInitFileLockingImplementation();
546 /* Report all resources used by hal */
547 HalReportResourceUsage();
550 * Clear the screen to blue
552 HalInitSystem(2, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
555 * Display version number and copyright/warranty message
557 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR
" (Build "
558 KERNEL_VERSION_BUILD_STR
")\n");
559 HalDisplayString(RES_STR_LEGAL_COPYRIGHT
);
560 HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
561 "Public License, and you\n");
562 HalDisplayString("are welcome to change it and/or distribute copies of it "
564 HalDisplayString("conditions. There is absolutely no warranty for "
567 if (KeNumberProcessors
> 1)
570 "Found %d system processors. [%lu MB Memory]\n",
572 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
577 "Found 1 system processor. [%lu MB Memory]\n",
578 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
580 HalDisplayString(str
);
585 /* Create the NLS section */
586 RtlpCreateNlsSection();
589 * Initalize services loaded at boot time
591 DPRINT("%d files loaded\n",KeLoaderBlock
.ModsCount
);
592 for (i
=0; i
< KeLoaderBlock
.ModsCount
; i
++)
594 CPRINT("Module: '%s' at %08lx, length 0x%08lx\n",
595 KeLoaderModules
[i
].String
,
596 KeLoaderModules
[i
].ModStart
,
597 KeLoaderModules
[i
].ModEnd
- KeLoaderModules
[i
].ModStart
);
600 /* Pass 1: import system hive registry chunk */
602 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
604 start
= KeLoaderModules
[i
].ModStart
;
605 length
= KeLoaderModules
[i
].ModEnd
- start
;
607 DPRINT("Module: '%s'\n", (PCHAR
)KeLoaderModules
[i
].String
);
608 name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\');
611 name
= (PCHAR
)KeLoaderModules
[i
].String
;
618 if (!_stricmp (name
, "system") ||
619 !_stricmp (name
, "system.hiv"))
621 CPRINT("Process system hive registry chunk at %08lx\n", start
);
623 CmImportSystemHive((PCHAR
)start
, length
);
627 /* Pass 2: import hardware hive registry chunk */
628 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
630 start
= KeLoaderModules
[i
].ModStart
;
631 length
= KeLoaderModules
[i
].ModEnd
- start
;
632 name
= (PCHAR
)KeLoaderModules
[i
].String
;
633 if (!_stricmp (name
, "hardware") ||
634 !_stricmp (name
, "hardware.hiv"))
636 CPRINT("Process hardware hive registry chunk at %08lx\n", start
);
637 CmImportHardwareHive((PCHAR
)start
, length
);
641 /* Create dummy keys if no hardware hive was found */
642 CmImportHardwareHive (NULL
, 0);
644 /* Initialize volatile registry settings */
645 if (SetupBoot
== FALSE
)
647 CmInit2((PCHAR
)KeLoaderBlock
.CommandLine
);
651 * Enter the kernel debugger before starting up the boot drivers
657 IoCreateDriverList();
661 /* Pass 3: process boot loaded drivers */
663 for (i
=1; i
< KeLoaderBlock
.ModsCount
; i
++)
665 start
= KeLoaderModules
[i
].ModStart
;
666 length
= KeLoaderModules
[i
].ModEnd
- start
;
667 name
= (PCHAR
)KeLoaderModules
[i
].String
;
668 if (RtlpCheckFileNameExtension(name
, ".sys") ||
669 RtlpCheckFileNameExtension(name
, ".sym"))
671 CPRINT("Initializing driver '%s' at %08lx, length 0x%08lx\n",
672 name
, start
, length
);
673 LdrInitializeBootStartDriver((PVOID
)start
, name
, length
);
675 if (RtlpCheckFileNameExtension(name
, ".sys"))
679 /* Pass 4: free memory for all boot files, except ntoskrnl.exe and hal.dll */
680 for (i
= 2; i
< KeLoaderBlock
.ModsCount
; i
++)
683 /* Do not free the memory from symbol files, if the kernel debugger is active */
684 if (!RtlpCheckFileNameExtension(name
, ".sym"))
687 MiFreeBootDriverMemory((PVOID
)KeLoaderModules
[i
].ModStart
,
688 KeLoaderModules
[i
].ModEnd
- KeLoaderModules
[i
].ModStart
);
692 if (BootDriverCount
== 0)
694 DbgPrint("No boot drivers available.\n");
698 /* Display the boot screen image if not disabled */
701 InbvEnableBootDriver(TRUE
);
704 /* Create ARC names for boot devices */
707 /* Create the SystemRoot symbolic link */
708 CPRINT("CommandLine: %s\n", (PUCHAR
)KeLoaderBlock
.CommandLine
);
709 Status
= IoCreateSystemRootLink((PUCHAR
)KeLoaderBlock
.CommandLine
);
710 if (!NT_SUCCESS(Status
))
711 KEBUGCHECK(INACCESSIBLE_BOOT_DEVICE
);
713 #ifdef DBGPRINT_FILE_LOG
714 /* On the assumption that we can now access disks start up the debug
717 #endif /* DBGPRINT_FILE_LOG */
723 PiInitDefaultLocale();
726 * Load boot start drivers
728 IopLoadBootStartDrivers();
731 * Load Auto configured drivers
733 LdrLoadAutoConfigDrivers();
735 IoDestroyDriverList();
738 * Assign drive letters
740 IoAssignDriveLetters ((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
,
746 * Initialize shared user page:
747 * - set dos system path, dos device map, etc.
749 InitSystemSharedUserPage ((PUCHAR
)KeLoaderBlock
.CommandLine
);
751 /* Create 'ReactOSInitDone' event */
752 RtlInitUnicodeString(&Name
, L
"\\ReactOSInitDone");
753 InitializeObjectAttributes(&ObjectAttributes
,
758 Status
= NtCreateEvent(&InitDoneEventHandle
,
761 FALSE
, /* Synchronization event */
762 FALSE
); /* Not signalled */
763 if (!NT_SUCCESS(Status
))
765 DPRINT1("Failed to create 'ReactOSInitDone' event (Status 0x%x)\n", Status
);
766 InitDoneEventHandle
= INVALID_HANDLE_VALUE
;
770 * Launch initial process
772 Status
= LdrLoadInitialProcess(&ProcessHandle
,
774 if (!NT_SUCCESS(Status
))
776 KEBUGCHECKEX(SESSION4_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
779 if (InitDoneEventHandle
!= INVALID_HANDLE_VALUE
)
781 HANDLE Handles
[2]; /* Init event, Initial process */
783 Handles
[0] = InitDoneEventHandle
;
784 Handles
[1] = ProcessHandle
;
786 /* Wait for the system to be initialized */
787 Timeout
.QuadPart
= -1200000000LL; /* 120 second timeout */
788 Status
= NtWaitForMultipleObjects(((LONG
) sizeof(Handles
) / sizeof(HANDLE
)),
791 FALSE
, /* Non-alertable */
793 if (!NT_SUCCESS(Status
))
795 DPRINT1("NtWaitForMultipleObjects failed with status 0x%x!\n", Status
);
797 else if (Status
== STATUS_TIMEOUT
)
799 DPRINT1("WARNING: System not initialized after 120 seconds.\n");
801 else if (Status
== STATUS_WAIT_0
+ 1)
804 * Crash the system if the initial process was terminated.
806 KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
811 InbvEnableBootDriver(FALSE
);
814 NtSetEvent(InitDoneEventHandle
, NULL
);
816 NtClose(InitDoneEventHandle
);
820 /* On failure to create 'ReactOSInitDone' event, go to text mode ASAP */
823 InbvEnableBootDriver(FALSE
);
827 * Crash the system if the initial process terminates within 5 seconds.
829 Timeout
.QuadPart
= -50000000LL;
830 Status
= NtWaitForSingleObject(ProcessHandle
,
833 if (Status
!= STATUS_TIMEOUT
)
835 KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
839 NtClose(ThreadHandle
);
840 NtClose(ProcessHandle
);
845 KiSystemStartup(BOOLEAN BootProcessor
)
847 HalInitSystem (0, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
851 ExpInitializeExecutive();
854 PsTerminateSystemThread(STATUS_SUCCESS
);
857 /* Do application processor initialization */
858 KeApplicationProcessorInit();
859 PsApplicationProcessorInit();
860 KeLowerIrql(PASSIVE_LEVEL
);
861 PsIdleThreadMain(NULL
);
868 _main (ULONG MultiBootMagic
, PLOADER_PARAMETER_BLOCK _LoaderBlock
)
870 * FUNCTION: Called by the boot loader to start the kernel
872 * LoaderBlock = Pointer to boot parameters initialized by the boot
874 * NOTE: The boot parameters are stored in low memory which will become
875 * invalid after the memory managment is initialized so we make a local copy.
880 ULONG last_kernel_address
;
881 extern ULONG _bss_end__
;
886 /* Low level architecture specific initialization */
890 * Copy the parameters to a local buffer because lowmem will go away
892 memcpy(&KeLoaderBlock
, _LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
893 memcpy(&KeLoaderModules
[1], (PVOID
)KeLoaderBlock
.ModsAddr
,
894 sizeof(LOADER_MODULE
) * KeLoaderBlock
.ModsCount
);
895 KeLoaderBlock
.ModsCount
++;
896 KeLoaderBlock
.ModsAddr
= (ULONG
)&KeLoaderModules
;
899 * Convert a path specification in the grub format to one understood by the
900 * rest of the kernel.
902 if (((PUCHAR
)_LoaderBlock
->CommandLine
)[0] == '(')
904 ULONG DiskNumber
= 0, PartNumber
= 0;
910 if (((PUCHAR
)_LoaderBlock
->CommandLine
)[1] == 'h' &&
911 ((PUCHAR
)_LoaderBlock
->CommandLine
)[2] == 'd')
913 DiskNumber
= ((PUCHAR
)_LoaderBlock
->CommandLine
)[3] - '0';
914 PartNumber
= ((PUCHAR
)_LoaderBlock
->CommandLine
)[5] - '0';
916 strcpy(Temp
, &((PUCHAR
)_LoaderBlock
->CommandLine
)[7]);
917 if ((options
= strchr(Temp
, ' ')) != NULL
)
926 if ((s1
= strrchr(Temp
, '/')) != NULL
)
929 if ((s1
= strrchr(Temp
, '/')) != NULL
)
934 sprintf(KeLoaderCommandLine
,
935 "multi(0)disk(0)rdisk(%lu)partition(%lu)%s %s",
936 DiskNumber
, PartNumber
+ 1, Temp
, options
);
938 p
= KeLoaderCommandLine
;
939 while (*p
!= 0 && *p
!= ' ')
947 DPRINT1("Command Line: %s\n", KeLoaderCommandLine
);
951 strcpy(KeLoaderCommandLine
, (PUCHAR
)_LoaderBlock
->CommandLine
);
953 KeLoaderBlock
.CommandLine
= (ULONG
)KeLoaderCommandLine
;
955 strcpy(KeLoaderModuleStrings
[0], "ntoskrnl.exe");
956 KeLoaderModules
[0].String
= (ULONG
)KeLoaderModuleStrings
[0];
957 KeLoaderModules
[0].ModStart
= 0xC0000000;
958 KeLoaderModules
[0].ModEnd
= PAGE_ROUND_UP((ULONG
)&_bss_end__
);
959 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
962 if ((s
= strrchr((PUCHAR
)KeLoaderModules
[i
].String
, '/')) != 0)
964 strcpy(KeLoaderModuleStrings
[i
], s
+ 1);
968 strcpy(KeLoaderModuleStrings
[i
], (PUCHAR
)KeLoaderModules
[i
].String
);
970 KeLoaderModules
[i
].ModStart
-= 0x200000;
971 KeLoaderModules
[i
].ModStart
+= 0xc0000000;
972 KeLoaderModules
[i
].ModEnd
-= 0x200000;
973 KeLoaderModules
[i
].ModEnd
+= 0xc0000000;
974 KeLoaderModules
[i
].String
= (ULONG
)KeLoaderModuleStrings
[i
];
978 HalnInitializeDisplay((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
981 HalBase
= KeLoaderModules
[1].ModStart
;
983 PAGE_ROUND_UP(KeLoaderModules
[KeLoaderBlock
.ModsCount
- 1].ModEnd
);
988 LdrSafePEProcessModule((PVOID
)HalBase
, (PVOID
)DriverBase
, (PVOID
)0xC0000000, &DriverSize
);
990 LdrHalBase
= (ULONG_PTR
)DriverBase
;
991 last_kernel_address
= DriverBase
+ DriverSize
;
994 * Process ntoskrnl.exe
996 LdrSafePEProcessModule((PVOID
)0xC0000000, (PVOID
)0xC0000000, (PVOID
)DriverBase
, &DriverSize
);
998 FirstKrnlPhysAddr
= KeLoaderModules
[0].ModStart
- 0xc0000000 + 0x200000;
999 LastKrnlPhysAddr
= last_kernel_address
- 0xc0000000 + 0x200000;
1000 LastKernelAddress
= last_kernel_address
;
1003 /* FIXME: VMware does not like it when ReactOS is using the BIOS memory map */
1004 KeLoaderBlock
.Flags
&= ~MB_FLAGS_MMAP_INFO
;
1007 KeMemoryMapRangeCount
= 0;
1008 if (KeLoaderBlock
.Flags
& MB_FLAGS_MMAP_INFO
)
1010 /* We have a memory map from the nice BIOS */
1011 size
= *((PULONG
)(KeLoaderBlock
.MmapAddr
- sizeof(ULONG
)));
1013 while (i
< KeLoaderBlock
.MmapLength
)
1015 memcpy (&KeMemoryMap
[KeMemoryMapRangeCount
],
1016 (PVOID
)(KeLoaderBlock
.MmapAddr
+ i
),
1017 sizeof(ADDRESS_RANGE
));
1018 KeMemoryMapRangeCount
++;