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)
14 #include <internal/debug.h>
16 /* DATA **********************************************************************/
18 extern ULONG MmCoreDumpType
;
19 extern CHAR KiTimerSystemAuditing
;
20 extern PVOID Ki386InitialStackArray
[MAXIMUM_PROCESSORS
];
21 extern ADDRESS_RANGE KeMemoryMap
[64];
22 extern ULONG KeMemoryMapRangeCount
;
23 extern ULONG_PTR FirstKrnlPhysAddr
;
24 extern ULONG_PTR LastKrnlPhysAddr
;
25 extern ULONG_PTR LastKernelAddress
;
26 extern LOADER_MODULE KeLoaderModules
[64];
27 extern PRTL_MESSAGE_RESOURCE_DATA KiBugCodeMessages
;
28 extern LIST_ENTRY KiProfileListHead
;
29 extern LIST_ENTRY KiProfileSourceListHead
;
30 extern KSPIN_LOCK KiProfileLock
;
31 BOOLEAN SetupMode
= TRUE
;
33 VOID
PspPostInitSystemProcess(VOID
);
35 /* FUNCTIONS ****************************************************************/
40 InitSystemSharedUserPage (PCSZ ParameterLine
)
42 UNICODE_STRING ArcDeviceName
;
43 UNICODE_STRING ArcName
;
44 UNICODE_STRING BootPath
;
45 UNICODE_STRING DriveDeviceName
;
46 UNICODE_STRING DriveName
;
47 WCHAR DriveNameBuffer
[20];
53 OBJECT_ATTRIBUTES ObjectAttributes
;
56 BOOLEAN BootDriveFound
= FALSE
;
60 * The shared user page has been zeroed-out right after creation.
61 * There is NO need to do this again.
63 Ki386SetProcessorFeatures();
65 /* Set the Version Data */
66 SharedUserData
->NtProductType
= NtProductWinNt
;
67 SharedUserData
->ProductTypeIsValid
= TRUE
;
68 SharedUserData
->NtMajorVersion
= 5;
69 SharedUserData
->NtMinorVersion
= 0;
72 * Retrieve the current dos system path
73 * (e.g.: C:\reactos) from the given arc path
74 * (e.g.: multi(0)disk(0)rdisk(0)partititon(1)\reactos)
75 * Format: "<arc_name>\<path> [options...]"
78 /* Create local parameter line copy */
79 ParamBuffer
= ExAllocatePool(PagedPool
, 256);
80 strcpy (ParamBuffer
, (char *)ParameterLine
);
81 DPRINT("%s\n", ParamBuffer
);
84 p
= strchr (ParamBuffer
, ' ');
86 DPRINT("%s\n", ParamBuffer
);
89 p
= strchr (ParamBuffer
, '\\');
92 DPRINT("Boot path: %s\n", p
);
93 RtlCreateUnicodeStringFromAsciiz (&BootPath
, p
);
98 DPRINT("Boot path: %s\n", "\\");
99 RtlCreateUnicodeStringFromAsciiz (&BootPath
, "\\");
101 DPRINT("Arc name: %s\n", ParamBuffer
);
103 /* Only ARC Name left - Build full ARC Name */
104 ArcNameBuffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
105 swprintf (ArcNameBuffer
, L
"\\ArcName\\%S", ParamBuffer
);
106 RtlInitUnicodeString (&ArcName
, ArcNameBuffer
);
107 DPRINT("Arc name: %wZ\n", &ArcName
);
109 /* Free ParamBuffer */
110 ExFreePool (ParamBuffer
);
112 /* Allocate ARC Device Name string */
113 ArcDeviceName
.Length
= 0;
114 ArcDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
115 ArcDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
117 /* Open the Symbolic Link */
118 InitializeObjectAttributes(&ObjectAttributes
,
123 Status
= NtOpenSymbolicLinkObject(&Handle
,
124 SYMBOLIC_LINK_ALL_ACCESS
,
127 /* Free the String */
128 ExFreePool(ArcName
.Buffer
);
130 /* Check for Success */
131 if (!NT_SUCCESS(Status
)) {
133 /* Free the Strings */
134 RtlFreeUnicodeString(&BootPath
);
135 ExFreePool(ArcDeviceName
.Buffer
);
136 CPRINT("NtOpenSymbolicLinkObject() failed (Status %x)\n", Status
);
141 Status
= NtQuerySymbolicLinkObject(Handle
,
146 /* Check for Success */
147 if (!NT_SUCCESS(Status
)) {
149 /* Free the Strings */
150 RtlFreeUnicodeString(&BootPath
);
151 ExFreePool(ArcDeviceName
.Buffer
);
152 CPRINT("NtQuerySymbolicLinkObject() failed (Status %x)\n", Status
);
155 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length
, &ArcDeviceName
);
157 /* Allocate Device Name string */
158 DriveDeviceName
.Length
= 0;
159 DriveDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
160 DriveDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
163 for (i
= 0; i
< 26; i
++) {
165 /* Setup the String */
166 swprintf (DriveNameBuffer
, L
"\\??\\%C:", 'A' + i
);
167 RtlInitUnicodeString(&DriveName
,
170 /* Open the Symbolic Link */
171 InitializeObjectAttributes(&ObjectAttributes
,
176 Status
= NtOpenSymbolicLinkObject(&Handle
,
177 SYMBOLIC_LINK_ALL_ACCESS
,
180 /* If it failed, skip to the next drive */
181 if (!NT_SUCCESS(Status
)) {
182 DPRINT("Failed to open link %wZ\n", &DriveName
);
187 Status
= NtQuerySymbolicLinkObject(Handle
,
191 /* If it failed, skip to the next drive */
192 if (!NT_SUCCESS(Status
)) {
193 DPRINT("Failed to query link %wZ\n", &DriveName
);
196 DPRINT("Opened link: %wZ ==> %wZ\n", &DriveName
, &DriveDeviceName
);
198 /* See if we've found the boot drive */
199 if (!RtlCompareUnicodeString (&ArcDeviceName
, &DriveDeviceName
, FALSE
)) {
201 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i
, &BootPath
);
202 swprintf(SharedUserData
->NtSystemRoot
, L
"%C:%wZ", 'A' + i
, &BootPath
);
203 BootDriveFound
= TRUE
;
206 /* Close this Link */
210 /* Free all the Strings we have in memory */
211 RtlFreeUnicodeString (&BootPath
);
212 ExFreePool(DriveDeviceName
.Buffer
);
213 ExFreePool(ArcDeviceName
.Buffer
);
215 /* Make sure we found the Boot Drive */
216 if (BootDriveFound
== FALSE
) {
218 DbgPrint("No system drive found!\n");
219 KEBUGCHECK (NO_BOOT_DEVICE
);
226 ExecuteRuntimeAsserts(VOID
)
229 * Fail at runtime if someone has changed various structures without
230 * updating the offsets used for the assembler code.
232 ASSERT(FIELD_OFFSET(KTHREAD
, InitialStack
) == KTHREAD_INITIAL_STACK
);
233 ASSERT(FIELD_OFFSET(KTHREAD
, Teb
) == KTHREAD_TEB
);
234 ASSERT(FIELD_OFFSET(KTHREAD
, KernelStack
) == KTHREAD_KERNEL_STACK
);
235 ASSERT(FIELD_OFFSET(KTHREAD
, NpxState
) == KTHREAD_NPX_STATE
);
236 ASSERT(FIELD_OFFSET(KTHREAD
, ServiceTable
) == KTHREAD_SERVICE_TABLE
);
237 ASSERT(FIELD_OFFSET(KTHREAD
, PreviousMode
) == KTHREAD_PREVIOUS_MODE
);
238 ASSERT(FIELD_OFFSET(KTHREAD
, TrapFrame
) == KTHREAD_TRAP_FRAME
);
239 ASSERT(FIELD_OFFSET(KTHREAD
, CallbackStack
) == KTHREAD_CALLBACK_STACK
);
240 ASSERT(FIELD_OFFSET(KTHREAD
, ApcState
.Process
) == KTHREAD_APCSTATE_PROCESS
);
241 ASSERT(FIELD_OFFSET(KPROCESS
, DirectoryTableBase
) == KPROCESS_DIRECTORY_TABLE_BASE
);
242 ASSERT(FIELD_OFFSET(KPROCESS
, IopmOffset
) == KPROCESS_IOPM_OFFSET
);
243 ASSERT(FIELD_OFFSET(KPROCESS
, LdtDescriptor
) == KPROCESS_LDT_DESCRIPTOR0
);
244 ASSERT(FIELD_OFFSET(KTRAP_FRAME
, Reserved9
) == KTRAP_FRAME_RESERVED9
);
245 ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME
, SavedExceptionStack
) == TF_SAVED_EXCEPTION_STACK
);
246 ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME
, regs
) == TF_REGS
);
247 ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME
, orig_ebp
) == TF_ORIG_EBP
);
248 ASSERT(FIELD_OFFSET(KPCR
, Tib
.ExceptionList
) == KPCR_EXCEPTION_LIST
);
249 ASSERT(FIELD_OFFSET(KPCR
, Self
) == KPCR_SELF
);
250 ASSERT(FIELD_OFFSET(KIPCR
, PrcbData
) + FIELD_OFFSET(KPRCB
, CurrentThread
) == KPCR_CURRENT_THREAD
);
251 ASSERT(FIELD_OFFSET(KIPCR
, PrcbData
) + FIELD_OFFSET(KPRCB
, NpxThread
) == KPCR_NPX_THREAD
);
252 ASSERT(FIELD_OFFSET(KTSS
, Esp0
) == KTSS_ESP0
);
253 ASSERT(FIELD_OFFSET(KTSS
, Eflags
) == KTSS_EFLAGS
);
254 ASSERT(FIELD_OFFSET(KTSS
, IoMapBase
) == KTSS_IOMAPBASE
);
255 ASSERT(sizeof(FX_SAVE_AREA
) == SIZEOF_FX_SAVE_AREA
);
261 ParseAndCacheLoadedModules(VOID
)
266 /* Loop the Module List and get the modules we want */
267 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++) {
269 /* Get the Name of this Module */
270 if (!(Name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\'))) {
273 Name
= (PCHAR
)KeLoaderModules
[i
].String
;
281 /* Now check for any of the modules we will need later */
282 if (!_stricmp(Name
, "ansi.nls")) {
284 CachedModules
[AnsiCodepage
] = &KeLoaderModules
[i
];
286 } else if (!_stricmp(Name
, "oem.nls")) {
288 CachedModules
[OemCodepage
] = &KeLoaderModules
[i
];
290 } else if (!_stricmp(Name
, "casemap.nls")) {
292 CachedModules
[UnicodeCasemap
] = &KeLoaderModules
[i
];
294 } else if (!_stricmp(Name
, "system") || !_stricmp(Name
, "system.hiv")) {
296 CachedModules
[SystemRegistry
] = &KeLoaderModules
[i
];
299 } else if (!_stricmp(Name
, "hardware") || !_stricmp(Name
, "hardware.hiv")) {
301 CachedModules
[HardwareRegistry
] = &KeLoaderModules
[i
];
309 ParseCommandLine(PULONG MaxMem
,
312 PBOOLEAN ForceAcpiDisable
)
316 p1
= (PCHAR
)KeLoaderBlock
.CommandLine
;
317 while(*p1
&& (p2
= strchr(p1
, '/'))) {
320 if (!_strnicmp(p2
, "MAXMEM", 6)) {
323 while (isspace(*p2
)) p2
++;
329 while(isspace(*p2
)) p2
++;
332 while (isdigit(*p2
)) {
333 *MaxMem
= *MaxMem
* 10 + *p2
- '0';
339 } else if (!_strnicmp(p2
, "NOGUIBOOT", 9)) {
344 } else if (!_strnicmp(p2
, "CRASHDUMP", 9)) {
350 if (!_strnicmp(p2
, "FULL", 4)) {
352 MmCoreDumpType
= MM_CORE_DUMP_TYPE_FULL
;
356 MmCoreDumpType
= MM_CORE_DUMP_TYPE_NONE
;
359 } else if (!_strnicmp(p2
, "BOOTLOG", 7)) {
363 } else if (!_strnicmp(p2
, "NOACPI", 6)) {
366 *ForceAcpiDisable
= TRUE
;
375 ExpDisplayNotice(VOID
)
382 "\n\n\n ReactOS " KERNEL_VERSION_STR
" Setup \n");
384 " \xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD");
390 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR
" (Build "
391 KERNEL_VERSION_BUILD_STR
")\n");
392 HalDisplayString(RES_STR_LEGAL_COPYRIGHT
);
393 HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
394 "Public License, and you\n");
395 HalDisplayString("are welcome to change it and/or distribute copies of it "
397 HalDisplayString("conditions. There is absolutely no warranty for "
400 /* Display number of Processors */
402 "Found %x system processor(s). [%lu MB Memory]\n",
403 (int)KeNumberProcessors
,
404 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
405 HalDisplayString(str
);
412 ExpInitializeExecutive(VOID
)
414 UNICODE_STRING EventName
;
415 HANDLE InitDoneEventHandle
;
416 OBJECT_ATTRIBUTES ObjectAttributes
;
417 BOOLEAN NoGuiBoot
= FALSE
;
418 BOOLEAN BootLog
= FALSE
;
420 BOOLEAN ForceAcpiDisable
= FALSE
;
421 LARGE_INTEGER Timeout
;
422 HANDLE ProcessHandle
;
426 /* Check if the structures match the ASM offset constants */
427 ExecuteRuntimeAsserts();
429 /* Sets up the Text Sections of the Kernel and HAL for debugging */
432 /* Lower the IRQL to Dispatch Level */
433 KeLowerIrql(DISPATCH_LEVEL
);
435 /* Sets up the VDM Data */
438 /* Parse Command Line Settings */
439 ParseCommandLine(&MaxMem
, &NoGuiBoot
, &BootLog
, &ForceAcpiDisable
);
441 /* Initialize Kernel Memory Address Space */
442 MmInit1(FirstKrnlPhysAddr
,
445 (PADDRESS_RANGE
)&KeMemoryMap
,
446 KeMemoryMapRangeCount
,
447 MaxMem
> 8 ? MaxMem
: 4096);
449 /* Parse the Loaded Modules (by FreeLoader) and cache the ones we'll need */
450 ParseAndCacheLoadedModules();
453 /* Initialize the Dispatcher, Clock and Bug Check Mechanisms. */
456 /* Bring back the IRQL to Passive */
457 KeLowerIrql(PASSIVE_LEVEL
);
459 /* Initialize Profiling */
460 InitializeListHead(&KiProfileListHead
);
461 InitializeListHead(&KiProfileSourceListHead
);
462 KeInitializeSpinLock(&KiProfileLock
);
464 /* Load basic Security for other Managers */
465 if (!SeInit1()) KEBUGCHECK(SECURITY_INITIALIZATION_FAILED
);
467 /* Create the Basic Object Manager Types to allow new Object Types */
470 /* Initialize Lookaside Lists */
473 /* Set up Region Maps, Sections and the Paging File */
476 /* Initialize Tokens now that the Object Manager is ready */
477 if (!SeInit2()) KEBUGCHECK(SECURITY1_INITIALIZATION_FAILED
);
479 /* Set 1 CPU for now, we'll increment this later */
480 KeNumberProcessors
= 1;
482 /* Initalize the Process Manager */
483 PiInitProcessManager();
485 /* Break into the Debugger if requested */
486 if (KdPollBreakIn()) DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
488 /* Initialize all processors */
489 while (!HalAllProcessorsStarted()) {
491 PVOID ProcessorStack
;
493 /* Set up the Kernel and Process Manager for this CPU */
494 KePrepareForApplicationProcessorInit(KeNumberProcessors
);
495 KeCreateApplicationProcessorIdleThread(KeNumberProcessors
);
497 /* Allocate a stack for use when booting the processor */
498 ProcessorStack
= RVA(Ki386InitialStackArray
[((int)KeNumberProcessors
)], MM_STACK_SIZE
);
500 /* Tell HAL a new CPU is being started */
501 HalStartNextProcessor(0, (ULONG
)ProcessorStack
- 2*sizeof(FX_SAVE_AREA
));
502 KeNumberProcessors
++;
505 /* Do Phase 1 HAL Initalization */
506 HalInitSystem(1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
508 /* Initialize Basic System Objects and Worker Threads */
511 /* Create the system handle table, assign it to the system process, create
512 the client id table and assign a PID for the system process. This needs
513 to be done before the worker threads are initialized so the system
514 process gets the first PID (4) */
515 PspPostInitSystemProcess();
517 /* initialize the worker threads */
518 ExpInitializeWorkerThreads();
520 /* initialize callbacks */
521 ExpInitializeCallbacks();
523 /* Call KD Providers at Phase 1 */
524 KdInitSystem(1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
526 /* Initialize I/O Objects, Filesystems, Error Logging and Shutdown */
530 PoInit((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
, ForceAcpiDisable
);
532 /* Initialize the Registry (Hives are NOT yet loaded!) */
533 CmInitializeRegistry();
535 /* Unmap Low memory, initialize the Page Zeroing and the Balancer Thread */
538 /* Initialize Cache Views */
541 /* Initialize File Locking */
542 FsRtlpInitFileLockingImplementation();
544 /* Report all resources used by hal */
545 HalReportResourceUsage();
547 /* Clear the screen to blue */
548 HalInitSystem(2, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
550 /* Display version number and copyright/warranty message */
553 /* Call KD Providers at Phase 2 */
554 KdInitSystem(2, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
556 /* Import and create NLS Data and Sections */
559 /* Import and Load Registry Hives */
560 CmInitHives(SetupMode
);
562 /* Initialize the time zone information from the registry */
563 ExpInitTimeZoneInfo();
565 /* Enter the kernel debugger before starting up the boot drivers */
566 if (KdDebuggerEnabled
) KdbEnter();
568 /* Setup Drivers and Root Device Node */
571 /* Display the boot screen image if not disabled */
572 if (!NoGuiBoot
) InbvEnableBootDriver(TRUE
);
574 /* Create ARC Names, SystemRoot SymLink, Load Drivers and Assign Letters */
577 /* Load the System DLL and its Entrypoints */
578 LdrpInitializeSystemDll();
580 /* Initialize the Default Locale */
581 PiInitDefaultLocale();
583 /* Initialize shared user page. Set dos system path, dos device map, etc. */
584 InitSystemSharedUserPage ((PCHAR
)KeLoaderBlock
.CommandLine
);
586 /* Create 'ReactOSInitDone' event */
587 RtlInitUnicodeString(&EventName
, L
"\\ReactOSInitDone");
588 InitializeObjectAttributes(&ObjectAttributes
,
593 Status
= ZwCreateEvent(&InitDoneEventHandle
,
596 SynchronizationEvent
,
599 /* Check for Success */
600 if (!NT_SUCCESS(Status
)) {
602 DPRINT1("Failed to create 'ReactOSInitDone' event (Status 0x%x)\n", Status
);
603 InitDoneEventHandle
= INVALID_HANDLE_VALUE
;
606 /* Launch initial process */
607 Status
= LdrLoadInitialProcess(&ProcessHandle
,
610 /* Check for success, Bugcheck if we failed */
611 if (!NT_SUCCESS(Status
)) {
613 KEBUGCHECKEX(SESSION4_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
616 /* Wait on the Completion Event */
617 if (InitDoneEventHandle
!= INVALID_HANDLE_VALUE
) {
619 HANDLE Handles
[2]; /* Init event, Initial process */
621 /* Setup the Handles to wait on */
622 Handles
[0] = InitDoneEventHandle
;
623 Handles
[1] = ProcessHandle
;
625 /* Wait for the system to be initialized */
626 Timeout
.QuadPart
= (LONGLONG
)-1200000000; /* 120 second timeout */
627 Status
= ZwWaitForMultipleObjects(2,
632 if (!NT_SUCCESS(Status
)) {
634 DPRINT1("NtWaitForMultipleObjects failed with status 0x%x!\n", Status
);
636 } else if (Status
== STATUS_TIMEOUT
) {
638 DPRINT1("WARNING: System not initialized after 120 seconds.\n");
640 } else if (Status
== STATUS_WAIT_0
+ 1) {
642 /* Crash the system if the initial process was terminated. */
643 KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
646 /* Disable the Boot Logo */
647 if (!NoGuiBoot
) InbvEnableBootDriver(FALSE
);
649 /* Signal the Event and close the handle */
650 ZwSetEvent(InitDoneEventHandle
, NULL
);
651 ZwClose(InitDoneEventHandle
);
655 /* On failure to create 'ReactOSInitDone' event, go to text mode ASAP */
656 if (!NoGuiBoot
) InbvEnableBootDriver(FALSE
);
658 /* Crash the system if the initial process terminates within 5 seconds. */
659 Timeout
.QuadPart
= (LONGLONG
)-50000000; /* 5 second timeout */
660 Status
= ZwWaitForSingleObject(ProcessHandle
,
664 /* Check for timeout, crash if the initial process didn't initalize */
665 if (Status
!= STATUS_TIMEOUT
) KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED
, Status
, 1, 0, 0);
668 /* Enable the Clock, close remaining handles */
669 KiTimerSystemAuditing
= 1;
670 ZwClose(ThreadHandle
);
671 ZwClose(ProcessHandle
);
677 ExpInitLookasideLists();
683 ExpInitializeEventImplementation();
684 ExpInitializeEventPairImplementation();
685 ExpInitializeMutantImplementation();
686 ExpInitializeSemaphoreImplementation();
687 ExpInitializeTimerImplementation();
689 ExpInitializeProfileImplementation();
692 ExpInitializeHandleTables();