2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ex/init.c
5 * PURPOSE: Executive initalization
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net) - Added ExpInitializeExecutive
8 * and optimized/cleaned it.
9 * Eric Kohl (ekohl@abo.rhein-zeitung.de)
13 #include <ntos/bootvid.h>
15 #include <internal/debug.h>
17 /* DATA **********************************************************************/
19 extern ULONG MmCoreDumpType
;
20 extern CHAR KiTimerSystemAuditing
;
21 extern PVOID Ki386InitialStackArray
[MAXIMUM_PROCESSORS
];
22 extern ADDRESS_RANGE KeMemoryMap
[64];
23 extern ULONG KeMemoryMapRangeCount
;
24 extern ULONG_PTR FirstKrnlPhysAddr
;
25 extern ULONG_PTR LastKrnlPhysAddr
;
26 extern ULONG_PTR LastKernelAddress
;
27 extern LOADER_MODULE KeLoaderModules
[64];
29 /* FUNCTIONS ****************************************************************/
34 InitSystemSharedUserPage (PCSZ ParameterLine
)
36 UNICODE_STRING ArcDeviceName
;
37 UNICODE_STRING ArcName
;
38 UNICODE_STRING BootPath
;
39 UNICODE_STRING DriveDeviceName
;
40 UNICODE_STRING DriveName
;
41 WCHAR DriveNameBuffer
[20];
47 OBJECT_ATTRIBUTES ObjectAttributes
;
50 BOOLEAN BootDriveFound
= FALSE
;
54 * The shared user page has been zeroed-out right after creation.
55 * There is NO need to do this again.
57 Ki386SetProcessorFeatures();
59 /* Set the Version Data */
60 SharedUserData
->NtProductType
= NtProductWinNt
;
61 SharedUserData
->ProductTypeIsValid
= TRUE
;
62 SharedUserData
->NtMajorVersion
= 5;
63 SharedUserData
->NtMinorVersion
= 0;
66 * Retrieve the current dos system path
67 * (e.g.: C:\reactos) from the given arc path
68 * (e.g.: multi(0)disk(0)rdisk(0)partititon(1)\reactos)
69 * Format: "<arc_name>\<path> [options...]"
72 /* Create local parameter line copy */
73 ParamBuffer
= ExAllocatePool(PagedPool
, 256);
74 strcpy (ParamBuffer
, (char *)ParameterLine
);
75 DPRINT("%s\n", ParamBuffer
);
78 p
= strchr (ParamBuffer
, ' ');
80 DPRINT("%s\n", ParamBuffer
);
83 p
= strchr (ParamBuffer
, '\\');
86 DPRINT("Boot path: %s\n", p
);
87 RtlCreateUnicodeStringFromAsciiz (&BootPath
, p
);
92 DPRINT("Boot path: %s\n", "\\");
93 RtlCreateUnicodeStringFromAsciiz (&BootPath
, "\\");
95 DPRINT("Arc name: %s\n", ParamBuffer
);
97 /* Only ARC Name left - Build full ARC Name */
98 ArcNameBuffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
99 swprintf (ArcNameBuffer
, L
"\\ArcName\\%S", ParamBuffer
);
100 RtlInitUnicodeString (&ArcName
, ArcNameBuffer
);
101 DPRINT("Arc name: %wZ\n", &ArcName
);
103 /* Free ParamBuffer */
104 ExFreePool (ParamBuffer
);
106 /* Allocate ARC Device Name string */
107 ArcDeviceName
.Length
= 0;
108 ArcDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
109 ArcDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
111 /* Open the Symbolic Link */
112 InitializeObjectAttributes(&ObjectAttributes
,
117 Status
= NtOpenSymbolicLinkObject(&Handle
,
118 SYMBOLIC_LINK_ALL_ACCESS
,
121 /* Free the String */
122 RtlFreeUnicodeString (&ArcName
);
124 /* Check for Success */
125 if (!NT_SUCCESS(Status
)) {
127 /* Free the Strings */
128 RtlFreeUnicodeString(&BootPath
);
129 RtlFreeUnicodeString(&ArcDeviceName
);
130 CPRINT("NtOpenSymbolicLinkObject() failed (Status %x)\n", Status
);
135 Status
= NtQuerySymbolicLinkObject(Handle
,
140 /* Check for Success */
141 if (!NT_SUCCESS(Status
)) {
143 /* Free the Strings */
144 RtlFreeUnicodeString(&BootPath
);
145 RtlFreeUnicodeString(&ArcDeviceName
);
146 CPRINT("NtQuerySymbolicLinkObject() failed (Status %x)\n", Status
);
149 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length
, &ArcDeviceName
);
151 /* Allocate Device Name string */
152 DriveDeviceName
.Length
= 0;
153 DriveDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
154 DriveDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
157 for (i
= 0; i
< 26; i
++) {
159 /* Setup the String */
160 swprintf (DriveNameBuffer
, L
"\\??\\%C:", 'A' + i
);
161 RtlInitUnicodeString(&DriveName
,
164 /* Open the Symbolic Link */
165 InitializeObjectAttributes(&ObjectAttributes
,
170 Status
= NtOpenSymbolicLinkObject(&Handle
,
171 SYMBOLIC_LINK_ALL_ACCESS
,
174 /* If it failed, skip to the next drive */
175 if (!NT_SUCCESS(Status
)) {
176 DPRINT("Failed to open link %wZ\n", &DriveName
);
181 Status
= NtQuerySymbolicLinkObject(Handle
,
185 /* If it failed, skip to the next drive */
186 if (!NT_SUCCESS(Status
)) {
187 DPRINT("Failed to query link %wZ\n", &DriveName
);
190 DPRINT("Opened link: %wZ ==> %wZ\n", &DriveName
, &DriveDeviceName
);
192 /* See if we've found the boot drive */
193 if (!RtlCompareUnicodeString (&ArcDeviceName
, &DriveDeviceName
, FALSE
)) {
195 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i
, &BootPath
);
196 swprintf(SharedUserData
->NtSystemRoot
, L
"%C:%wZ", 'A' + i
, &BootPath
);
197 BootDriveFound
= TRUE
;
200 /* Close this Link */
204 /* Free all the Strings we have in memory */
205 RtlFreeUnicodeString (&BootPath
);
206 RtlFreeUnicodeString (&DriveDeviceName
);
207 RtlFreeUnicodeString (&ArcDeviceName
);
209 /* Make sure we found the Boot Drive */
210 if (BootDriveFound
== FALSE
) {
212 DbgPrint("No system drive found!\n");
213 KEBUGCHECK (NO_BOOT_DEVICE
);
220 ExecuteRuntimeAsserts(VOID
)
223 * Fail at runtime if someone has changed various structures without
224 * updating the offsets used for the assembler code.
226 ASSERT(FIELD_OFFSET(KTHREAD
, InitialStack
) == KTHREAD_INITIAL_STACK
);
227 ASSERT(FIELD_OFFSET(KTHREAD
, Teb
) == KTHREAD_TEB
);
228 ASSERT(FIELD_OFFSET(KTHREAD
, KernelStack
) == KTHREAD_KERNEL_STACK
);
229 ASSERT(FIELD_OFFSET(KTHREAD
, NpxState
) == KTHREAD_NPX_STATE
);
230 ASSERT(FIELD_OFFSET(KTHREAD
, ServiceTable
) == KTHREAD_SERVICE_TABLE
);
231 ASSERT(FIELD_OFFSET(KTHREAD
, PreviousMode
) == KTHREAD_PREVIOUS_MODE
);
232 ASSERT(FIELD_OFFSET(KTHREAD
, TrapFrame
) == KTHREAD_TRAP_FRAME
);
233 ASSERT(FIELD_OFFSET(KTHREAD
, CallbackStack
) == KTHREAD_CALLBACK_STACK
);
234 ASSERT(FIELD_OFFSET(KTHREAD
, ApcState
.Process
) == KTHREAD_APCSTATE_PROCESS
);
235 ASSERT(FIELD_OFFSET(KPROCESS
, DirectoryTableBase
) == KPROCESS_DIRECTORY_TABLE_BASE
);
236 ASSERT(FIELD_OFFSET(KPROCESS
, IopmOffset
) == KPROCESS_IOPM_OFFSET
);
237 ASSERT(FIELD_OFFSET(KPROCESS
, LdtDescriptor
) == KPROCESS_LDT_DESCRIPTOR0
);
238 ASSERT(FIELD_OFFSET(KTRAP_FRAME
, Reserved9
) == KTRAP_FRAME_RESERVED9
);
239 ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME
, SavedExceptionStack
) == TF_SAVED_EXCEPTION_STACK
);
240 ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME
, regs
) == TF_REGS
);
241 ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME
, orig_ebp
) == TF_ORIG_EBP
);
242 ASSERT(FIELD_OFFSET(KPCR
, Tib
.ExceptionList
) == KPCR_EXCEPTION_LIST
);
243 ASSERT(FIELD_OFFSET(KPCR
, Self
) == KPCR_SELF
);
244 ASSERT(FIELD_OFFSET(KPCR
, PrcbData
) + FIELD_OFFSET(KPRCB
, CurrentThread
) == KPCR_CURRENT_THREAD
);
245 ASSERT(FIELD_OFFSET(KPCR
, PrcbData
) + FIELD_OFFSET(KPRCB
, NpxThread
) == KPCR_NPX_THREAD
);
246 ASSERT(FIELD_OFFSET(KTSS
, Esp0
) == KTSS_ESP0
);
247 ASSERT(FIELD_OFFSET(KTSS
, Eflags
) == KTSS_EFLAGS
);
248 ASSERT(FIELD_OFFSET(KTSS
, IoMapBase
) == KTSS_IOMAPBASE
);
249 ASSERT(sizeof(FX_SAVE_AREA
) == SIZEOF_FX_SAVE_AREA
);
255 ParseAndCacheLoadedModules(PBOOLEAN SetupBoot
)
260 /* Loop the Module List and get the modules we want */
261 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++) {
263 /* Get the Name of this Module */
264 if (!(Name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\'))) {
267 Name
= (PCHAR
)KeLoaderModules
[i
].String
;
275 /* Now check for any of the modules we will need later */
276 if (!_stricmp(Name
, "ansi.nls")) {
278 CachedModules
[AnsiCodepage
] = &KeLoaderModules
[i
];
280 } else if (!_stricmp(Name
, "oem.nls")) {
282 CachedModules
[OemCodepage
] = &KeLoaderModules
[i
];
284 } else if (!_stricmp(Name
, "casemap.nls")) {
286 CachedModules
[UnicodeCasemap
] = &KeLoaderModules
[i
];
288 } else if (!_stricmp(Name
, "system") || !_stricmp(Name
, "system.hiv")) {
290 CachedModules
[SystemRegistry
] = &KeLoaderModules
[i
];
293 } else if (!_stricmp(Name
, "hardware") || !_stricmp(Name
, "hardware.hiv")) {
295 CachedModules
[HardwareRegistry
] = &KeLoaderModules
[i
];
303 ParseCommandLine(PULONG MaxMem
, PBOOLEAN NoGuiBoot
, PBOOLEAN BootLog
)
307 p1
= (PCHAR
)KeLoaderBlock
.CommandLine
;
308 while(*p1
&& (p2
= strchr(p1
, '/'))) {
311 if (!_strnicmp(p2
, "MAXMEM", 6)) {
314 while (isspace(*p2
)) p2
++;
320 while(isspace(*p2
)) p2
++;
323 while (isdigit(*p2
)) {
324 *MaxMem
= *MaxMem
* 10 + *p2
- '0';
330 } else if (!_strnicmp(p2
, "NOGUIBOOT", 9)) {
335 } else if (!_strnicmp(p2
, "CRASHDUMP", 9)) {
341 if (!_strnicmp(p2
, "FULL", 4)) {
343 MmCoreDumpType
= MM_CORE_DUMP_TYPE_FULL
;
347 MmCoreDumpType
= MM_CORE_DUMP_TYPE_NONE
;
350 } else if (!_strnicmp(p2
, "BOOTLOG", 7)) {
363 ExpInitializeExecutive(VOID
)
366 UNICODE_STRING EventName
;
367 HANDLE InitDoneEventHandle
;
368 OBJECT_ATTRIBUTES ObjectAttributes
;
369 BOOLEAN NoGuiBoot
= FALSE
;
370 BOOLEAN BootLog
= FALSE
;
372 BOOLEAN SetupBoot
= TRUE
;
373 LARGE_INTEGER Timeout
;
374 HANDLE ProcessHandle
;
378 /* Check if the structures match the ASM offset constants */
379 ExecuteRuntimeAsserts();
381 /* Sets up the Text Sections of the Kernel and HAL for debugging */
384 /* Lower the IRQL to Dispatch Level */
385 KeLowerIrql(DISPATCH_LEVEL
);
387 /* Sets up the VDM Data */
390 /* Parse Command Line Settings */
391 ParseCommandLine(&MaxMem
, &NoGuiBoot
, &BootLog
);
393 /* Initialize Kernel Memory Address Space */
394 MmInit1(FirstKrnlPhysAddr
,
397 (PADDRESS_RANGE
)&KeMemoryMap
,
398 KeMemoryMapRangeCount
,
399 MaxMem
> 8 ? MaxMem
: 4096);
401 /* Parse the Loaded Modules (by FreeLoader) and cache the ones we'll need */
402 ParseAndCacheLoadedModules(&SetupBoot
);
404 /* Initialize the kernel debugger */
405 KdInitSystem (1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
407 /* Initialize the Dispatcher, Clock and Bug Check Mechanisms. */
410 /* Bring back the IRQL to Passive */
411 KeLowerIrql(PASSIVE_LEVEL
);
413 /* Load basic Security for other Managers */
414 if (!SeInit1()) KEBUGCHECK(SECURITY_INITIALIZATION_FAILED
);
416 /* Create the Basic Object Manager Types to allow new Object Types */
419 /* Initialize Lookaside Lists */
422 /* Set up Region Maps, Sections and the Paging File */
425 /* Initialize Tokens now that the Object Manager is ready */
426 if (!SeInit2()) KEBUGCHECK(SECURITY1_INITIALIZATION_FAILED
);
428 /* Set 1 CPU for now, we'll increment this later */
429 KeNumberProcessors
= 1;
431 /* Initalize the Process Manager */
432 PiInitProcessManager();
434 /* Break into the Debugger if requested */
435 if (KdPollBreakIn()) DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
437 /* Initialize all processors */
438 while (!HalAllProcessorsStarted()) {
440 PVOID ProcessorStack
;
442 /* Set up the Kernel and Process Manager for this CPU */
443 KePrepareForApplicationProcessorInit(KeNumberProcessors
);
444 PsPrepareForApplicationProcessorInit(KeNumberProcessors
);
446 /* Allocate a stack for use when booting the processor */
447 ProcessorStack
= Ki386InitialStackArray
[((int)KeNumberProcessors
)] + MM_STACK_SIZE
;
449 /* Tell HAL a new CPU is being started */
450 HalStartNextProcessor(0, (ULONG
)ProcessorStack
- 2*sizeof(FX_SAVE_AREA
));
451 KeNumberProcessors
++;
454 /* Do Phase 1 HAL Initalization */
455 HalInitSystem(1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
457 /* Initialize Basic System Objects and Worker Threads */
460 /* Initialize the GDB Stub and break */
463 /* Initialize I/O Objects, Filesystems, Error Logging and Shutdown */
469 /* Initialize the Registry (Hives are NOT yet loaded!) */
470 CmInitializeRegistry();
472 /* Unmap Low memory, initialize the Page Zeroing and the Balancer Thread */
475 /* Initialize Cache Views */
478 /* Hook System Interrupt for the Debugger */
481 /* Initialize File Locking */
482 FsRtlpInitFileLockingImplementation();
484 /* Report all resources used by hal */
485 HalReportResourceUsage();
487 /* Clear the screen to blue */
488 HalInitSystem(2, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
490 /* Display version number and copyright/warranty message */
491 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR
" (Build "
492 KERNEL_VERSION_BUILD_STR
")\n");
493 HalDisplayString(RES_STR_LEGAL_COPYRIGHT
);
494 HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
495 "Public License, and you\n");
496 HalDisplayString("are welcome to change it and/or distribute copies of it "
498 HalDisplayString("conditions. There is absolutely no warranty for "
501 /* Display number of Processors */
503 "Found %d system processor(s). [%lu MB Memory]\n",
505 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
506 HalDisplayString(str
);
508 /* Print which Debugger is being used */
511 /* Import and create NLS Data and Sections */
514 /* Import and Load Registry Hives */
515 CmInitHives(SetupBoot
);
517 /* Initialize the time zone information from the registry */
518 ExpInitTimeZoneInfo();
520 /* Enter the kernel debugger before starting up the boot drivers */
525 /* Setup Drivers and Root Device Node */
528 /* Display the boot screen image if not disabled */
529 if (!NoGuiBoot
) InbvEnableBootDriver(TRUE
);
531 /* Create ARC Names, SystemRoot SymLink, Load Drivers and Assign Letters */
534 /* Initialize the Default Locale */
535 PiInitDefaultLocale();
537 /* Initialize shared user page. Set dos system path, dos device map, etc. */
538 InitSystemSharedUserPage ((PCHAR
)KeLoaderBlock
.CommandLine
);
540 /* Create 'ReactOSInitDone' event */
541 RtlInitUnicodeString(&EventName
, L
"\\ReactOSInitDone");
542 InitializeObjectAttributes(&ObjectAttributes
,
547 Status
= ZwCreateEvent(&InitDoneEventHandle
,
550 SynchronizationEvent
,
553 /* Check for Success */
554 if (!NT_SUCCESS(Status
)) {
556 DPRINT1("Failed to create 'ReactOSInitDone' event (Status 0x%x)\n", Status
);
557 InitDoneEventHandle
= INVALID_HANDLE_VALUE
;
560 /* Launch initial process */
561 Status
= LdrLoadInitialProcess(&ProcessHandle
,
564 /* Check for success, Bugcheck if we failed */
565 if (!NT_SUCCESS(Status
)) {
567 KEBUGCHECKEX(SESSION4_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
570 /* Wait on the Completion Event */
571 if (InitDoneEventHandle
!= INVALID_HANDLE_VALUE
) {
573 HANDLE Handles
[2]; /* Init event, Initial process */
575 /* Setup the Handles to wait on */
576 Handles
[0] = InitDoneEventHandle
;
577 Handles
[1] = ProcessHandle
;
579 /* Wait for the system to be initialized */
580 Timeout
.QuadPart
= (LONGLONG
)-1200000000; /* 120 second timeout */
581 Status
= ZwWaitForMultipleObjects(2,
586 if (!NT_SUCCESS(Status
)) {
588 DPRINT1("NtWaitForMultipleObjects failed with status 0x%x!\n", Status
);
590 } else if (Status
== STATUS_TIMEOUT
) {
592 DPRINT1("WARNING: System not initialized after 120 seconds.\n");
594 } else if (Status
== STATUS_WAIT_0
+ 1) {
596 /* Crash the system if the initial process was terminated. */
597 KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
600 /* Disable the Boot Logo */
601 if (!NoGuiBoot
) InbvEnableBootDriver(FALSE
);
603 /* Signal the Event and close the handle */
604 ZwSetEvent(InitDoneEventHandle
, NULL
);
605 ZwClose(InitDoneEventHandle
);
609 /* On failure to create 'ReactOSInitDone' event, go to text mode ASAP */
610 if (!NoGuiBoot
) InbvEnableBootDriver(FALSE
);
612 /* Crash the system if the initial process terminates within 5 seconds. */
613 Timeout
.QuadPart
= (LONGLONG
)-50000000; /* 5 second timeout */
614 Status
= ZwWaitForSingleObject(ProcessHandle
,
618 /* Check for timeout, crash if the initial process didn't initalize */
619 if (Status
!= STATUS_TIMEOUT
) KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED
, Status
, 1, 0, 0);
622 /* Enable the Clock, close remaining handles */
623 KiTimerSystemAuditing
= 1;
624 ZwClose(ThreadHandle
);
625 ZwClose(ProcessHandle
);
631 ExpInitLookasideLists();
637 ExpInitializeWorkerThreads();
638 ExpInitializeEventImplementation();
639 ExpInitializeEventPairImplementation();
640 ExpInitializeMutantImplementation();
641 ExpInitializeSemaphoreImplementation();
642 ExpInitializeTimerImplementation();
644 ExpInitializeProfileImplementation();
647 ExpInitializeCallbacks();