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>
14 #include "../dbg/kdb.h"
16 #include <internal/debug.h>
18 /* DATA **********************************************************************/
20 extern ULONG MmCoreDumpType
;
21 extern CHAR KiTimerSystemAuditing
;
22 extern PVOID Ki386InitialStackArray
[MAXIMUM_PROCESSORS
];
23 extern ADDRESS_RANGE KeMemoryMap
[64];
24 extern ULONG KeMemoryMapRangeCount
;
25 extern ULONG_PTR FirstKrnlPhysAddr
;
26 extern ULONG_PTR LastKrnlPhysAddr
;
27 extern ULONG_PTR LastKernelAddress
;
28 extern LOADER_MODULE KeLoaderModules
[64];
29 extern PRTL_MESSAGE_RESOURCE_DATA KiBugCodeMessages
;
30 extern LIST_ENTRY KiProfileListHead
;
31 extern LIST_ENTRY KiProfileSourceListHead
;
32 extern KSPIN_LOCK KiProfileLock
;
34 VOID
PspPostInitSystemProcess(VOID
);
36 /* FUNCTIONS ****************************************************************/
41 InitSystemSharedUserPage (PCSZ ParameterLine
)
43 UNICODE_STRING ArcDeviceName
;
44 UNICODE_STRING ArcName
;
45 UNICODE_STRING BootPath
;
46 UNICODE_STRING DriveDeviceName
;
47 UNICODE_STRING DriveName
;
48 WCHAR DriveNameBuffer
[20];
54 OBJECT_ATTRIBUTES ObjectAttributes
;
57 BOOLEAN BootDriveFound
= FALSE
;
61 * The shared user page has been zeroed-out right after creation.
62 * There is NO need to do this again.
64 Ki386SetProcessorFeatures();
66 /* Set the Version Data */
67 SharedUserData
->NtProductType
= NtProductWinNt
;
68 SharedUserData
->ProductTypeIsValid
= TRUE
;
69 SharedUserData
->NtMajorVersion
= 5;
70 SharedUserData
->NtMinorVersion
= 0;
73 * Retrieve the current dos system path
74 * (e.g.: C:\reactos) from the given arc path
75 * (e.g.: multi(0)disk(0)rdisk(0)partititon(1)\reactos)
76 * Format: "<arc_name>\<path> [options...]"
79 /* Create local parameter line copy */
80 ParamBuffer
= ExAllocatePool(PagedPool
, 256);
81 strcpy (ParamBuffer
, (char *)ParameterLine
);
82 DPRINT("%s\n", ParamBuffer
);
85 p
= strchr (ParamBuffer
, ' ');
87 DPRINT("%s\n", ParamBuffer
);
90 p
= strchr (ParamBuffer
, '\\');
93 DPRINT("Boot path: %s\n", p
);
94 RtlCreateUnicodeStringFromAsciiz (&BootPath
, p
);
99 DPRINT("Boot path: %s\n", "\\");
100 RtlCreateUnicodeStringFromAsciiz (&BootPath
, "\\");
102 DPRINT("Arc name: %s\n", ParamBuffer
);
104 /* Only ARC Name left - Build full ARC Name */
105 ArcNameBuffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
106 swprintf (ArcNameBuffer
, L
"\\ArcName\\%S", ParamBuffer
);
107 RtlInitUnicodeString (&ArcName
, ArcNameBuffer
);
108 DPRINT("Arc name: %wZ\n", &ArcName
);
110 /* Free ParamBuffer */
111 ExFreePool (ParamBuffer
);
113 /* Allocate ARC Device Name string */
114 ArcDeviceName
.Length
= 0;
115 ArcDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
116 ArcDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
118 /* Open the Symbolic Link */
119 InitializeObjectAttributes(&ObjectAttributes
,
124 Status
= NtOpenSymbolicLinkObject(&Handle
,
125 SYMBOLIC_LINK_ALL_ACCESS
,
128 /* Free the String */
129 RtlFreeUnicodeString(&ArcName
);
131 /* Check for Success */
132 if (!NT_SUCCESS(Status
)) {
134 /* Free the Strings */
135 RtlFreeUnicodeString(&BootPath
);
136 RtlFreeUnicodeString(&ArcDeviceName
);
137 CPRINT("NtOpenSymbolicLinkObject() failed (Status %x)\n", Status
);
142 Status
= NtQuerySymbolicLinkObject(Handle
,
147 /* Check for Success */
148 if (!NT_SUCCESS(Status
)) {
150 /* Free the Strings */
151 RtlFreeUnicodeString(&BootPath
);
152 RtlFreeUnicodeString(&ArcDeviceName
);
153 CPRINT("NtQuerySymbolicLinkObject() failed (Status %x)\n", Status
);
156 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length
, &ArcDeviceName
);
158 /* Allocate Device Name string */
159 DriveDeviceName
.Length
= 0;
160 DriveDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
161 DriveDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
164 for (i
= 0; i
< 26; i
++) {
166 /* Setup the String */
167 swprintf (DriveNameBuffer
, L
"\\??\\%C:", 'A' + i
);
168 RtlInitUnicodeString(&DriveName
,
171 /* Open the Symbolic Link */
172 InitializeObjectAttributes(&ObjectAttributes
,
177 Status
= NtOpenSymbolicLinkObject(&Handle
,
178 SYMBOLIC_LINK_ALL_ACCESS
,
181 /* If it failed, skip to the next drive */
182 if (!NT_SUCCESS(Status
)) {
183 DPRINT("Failed to open link %wZ\n", &DriveName
);
188 Status
= NtQuerySymbolicLinkObject(Handle
,
192 /* If it failed, skip to the next drive */
193 if (!NT_SUCCESS(Status
)) {
194 DPRINT("Failed to query link %wZ\n", &DriveName
);
197 DPRINT("Opened link: %wZ ==> %wZ\n", &DriveName
, &DriveDeviceName
);
199 /* See if we've found the boot drive */
200 if (!RtlCompareUnicodeString (&ArcDeviceName
, &DriveDeviceName
, FALSE
)) {
202 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i
, &BootPath
);
203 swprintf(SharedUserData
->NtSystemRoot
, L
"%C:%wZ", 'A' + i
, &BootPath
);
204 BootDriveFound
= TRUE
;
207 /* Close this Link */
211 /* Free all the Strings we have in memory */
212 RtlFreeUnicodeString (&BootPath
);
213 RtlFreeUnicodeString (&DriveDeviceName
);
214 RtlFreeUnicodeString (&ArcDeviceName
);
216 /* Make sure we found the Boot Drive */
217 if (BootDriveFound
== FALSE
) {
219 DbgPrint("No system drive found!\n");
220 KEBUGCHECK (NO_BOOT_DEVICE
);
227 ExecuteRuntimeAsserts(VOID
)
230 * Fail at runtime if someone has changed various structures without
231 * updating the offsets used for the assembler code.
233 ASSERT(FIELD_OFFSET(KTHREAD
, InitialStack
) == KTHREAD_INITIAL_STACK
);
234 ASSERT(FIELD_OFFSET(KTHREAD
, Teb
) == KTHREAD_TEB
);
235 ASSERT(FIELD_OFFSET(KTHREAD
, KernelStack
) == KTHREAD_KERNEL_STACK
);
236 ASSERT(FIELD_OFFSET(KTHREAD
, NpxState
) == KTHREAD_NPX_STATE
);
237 ASSERT(FIELD_OFFSET(KTHREAD
, ServiceTable
) == KTHREAD_SERVICE_TABLE
);
238 ASSERT(FIELD_OFFSET(KTHREAD
, PreviousMode
) == KTHREAD_PREVIOUS_MODE
);
239 ASSERT(FIELD_OFFSET(KTHREAD
, TrapFrame
) == KTHREAD_TRAP_FRAME
);
240 ASSERT(FIELD_OFFSET(KTHREAD
, CallbackStack
) == KTHREAD_CALLBACK_STACK
);
241 ASSERT(FIELD_OFFSET(KTHREAD
, ApcState
.Process
) == KTHREAD_APCSTATE_PROCESS
);
242 ASSERT(FIELD_OFFSET(KPROCESS
, DirectoryTableBase
) == KPROCESS_DIRECTORY_TABLE_BASE
);
243 ASSERT(FIELD_OFFSET(KPROCESS
, IopmOffset
) == KPROCESS_IOPM_OFFSET
);
244 ASSERT(FIELD_OFFSET(KPROCESS
, LdtDescriptor
) == KPROCESS_LDT_DESCRIPTOR0
);
245 ASSERT(FIELD_OFFSET(KTRAP_FRAME
, Reserved9
) == KTRAP_FRAME_RESERVED9
);
246 ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME
, SavedExceptionStack
) == TF_SAVED_EXCEPTION_STACK
);
247 ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME
, regs
) == TF_REGS
);
248 ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME
, orig_ebp
) == TF_ORIG_EBP
);
249 ASSERT(FIELD_OFFSET(KPCR
, Tib
.ExceptionList
) == KPCR_EXCEPTION_LIST
);
250 ASSERT(FIELD_OFFSET(KPCR
, Self
) == KPCR_SELF
);
251 ASSERT(FIELD_OFFSET(KPCR
, PrcbData
) + FIELD_OFFSET(KPRCB
, CurrentThread
) == KPCR_CURRENT_THREAD
);
252 ASSERT(FIELD_OFFSET(KPCR
, PrcbData
) + FIELD_OFFSET(KPRCB
, NpxThread
) == KPCR_NPX_THREAD
);
253 ASSERT(FIELD_OFFSET(KTSS
, Esp0
) == KTSS_ESP0
);
254 ASSERT(FIELD_OFFSET(KTSS
, Eflags
) == KTSS_EFLAGS
);
255 ASSERT(FIELD_OFFSET(KTSS
, IoMapBase
) == KTSS_IOMAPBASE
);
256 ASSERT(sizeof(FX_SAVE_AREA
) == SIZEOF_FX_SAVE_AREA
);
262 ParseAndCacheLoadedModules(PBOOLEAN SetupBoot
)
267 /* Loop the Module List and get the modules we want */
268 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++) {
270 /* Get the Name of this Module */
271 if (!(Name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\'))) {
274 Name
= (PCHAR
)KeLoaderModules
[i
].String
;
282 /* Now check for any of the modules we will need later */
283 if (!_stricmp(Name
, "ansi.nls")) {
285 CachedModules
[AnsiCodepage
] = &KeLoaderModules
[i
];
287 } else if (!_stricmp(Name
, "oem.nls")) {
289 CachedModules
[OemCodepage
] = &KeLoaderModules
[i
];
291 } else if (!_stricmp(Name
, "casemap.nls")) {
293 CachedModules
[UnicodeCasemap
] = &KeLoaderModules
[i
];
295 } else if (!_stricmp(Name
, "system") || !_stricmp(Name
, "system.hiv")) {
297 CachedModules
[SystemRegistry
] = &KeLoaderModules
[i
];
300 } else if (!_stricmp(Name
, "hardware") || !_stricmp(Name
, "hardware.hiv")) {
302 CachedModules
[HardwareRegistry
] = &KeLoaderModules
[i
];
310 ParseCommandLine(PULONG MaxMem
,
313 PBOOLEAN ForceAcpiDisable
)
317 p1
= (PCHAR
)KeLoaderBlock
.CommandLine
;
318 while(*p1
&& (p2
= strchr(p1
, '/'))) {
321 if (!_strnicmp(p2
, "MAXMEM", 6)) {
324 while (isspace(*p2
)) p2
++;
330 while(isspace(*p2
)) p2
++;
333 while (isdigit(*p2
)) {
334 *MaxMem
= *MaxMem
* 10 + *p2
- '0';
340 } else if (!_strnicmp(p2
, "NOGUIBOOT", 9)) {
345 } else if (!_strnicmp(p2
, "CRASHDUMP", 9)) {
351 if (!_strnicmp(p2
, "FULL", 4)) {
353 MmCoreDumpType
= MM_CORE_DUMP_TYPE_FULL
;
357 MmCoreDumpType
= MM_CORE_DUMP_TYPE_NONE
;
360 } else if (!_strnicmp(p2
, "BOOTLOG", 7)) {
364 } else if (!_strnicmp(p2
, "NOACPI", 6)) {
367 *ForceAcpiDisable
= TRUE
;
377 ExpInitializeExecutive(VOID
)
380 UNICODE_STRING EventName
;
381 HANDLE InitDoneEventHandle
;
382 OBJECT_ATTRIBUTES ObjectAttributes
;
383 BOOLEAN NoGuiBoot
= FALSE
;
384 BOOLEAN BootLog
= FALSE
;
386 BOOLEAN SetupBoot
= TRUE
;
387 BOOLEAN ForceAcpiDisable
= FALSE
;
388 LARGE_INTEGER Timeout
;
389 HANDLE ProcessHandle
;
393 /* Check if the structures match the ASM offset constants */
394 ExecuteRuntimeAsserts();
396 /* Sets up the Text Sections of the Kernel and HAL for debugging */
399 /* Lower the IRQL to Dispatch Level */
400 KeLowerIrql(DISPATCH_LEVEL
);
402 /* Sets up the VDM Data */
405 /* Parse Command Line Settings */
406 ParseCommandLine(&MaxMem
, &NoGuiBoot
, &BootLog
, &ForceAcpiDisable
);
408 /* Initialize Kernel Memory Address Space */
409 MmInit1(FirstKrnlPhysAddr
,
412 (PADDRESS_RANGE
)&KeMemoryMap
,
413 KeMemoryMapRangeCount
,
414 MaxMem
> 8 ? MaxMem
: 4096);
416 /* Parse the Loaded Modules (by FreeLoader) and cache the ones we'll need */
417 ParseAndCacheLoadedModules(&SetupBoot
);
419 /* Initialize the kernel debugger */
420 KdInitSystem (1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
422 /* Initialize the Dispatcher, Clock and Bug Check Mechanisms. */
425 /* Bring back the IRQL to Passive */
426 KeLowerIrql(PASSIVE_LEVEL
);
428 /* Initialize Profiling */
429 InitializeListHead(&KiProfileListHead
);
430 InitializeListHead(&KiProfileSourceListHead
);
431 KeInitializeSpinLock(&KiProfileLock
);
433 /* Load basic Security for other Managers */
434 if (!SeInit1()) KEBUGCHECK(SECURITY_INITIALIZATION_FAILED
);
436 /* Create the Basic Object Manager Types to allow new Object Types */
439 /* Initialize Lookaside Lists */
442 /* Set up Region Maps, Sections and the Paging File */
445 /* Initialize Tokens now that the Object Manager is ready */
446 if (!SeInit2()) KEBUGCHECK(SECURITY1_INITIALIZATION_FAILED
);
448 /* Set 1 CPU for now, we'll increment this later */
449 KeNumberProcessors
= 1;
451 /* Initalize the Process Manager */
452 PiInitProcessManager();
454 /* Break into the Debugger if requested */
455 if (KdPollBreakIn()) DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
457 /* Initialize all processors */
458 while (!HalAllProcessorsStarted()) {
460 PVOID ProcessorStack
;
462 /* Set up the Kernel and Process Manager for this CPU */
463 KePrepareForApplicationProcessorInit(KeNumberProcessors
);
464 PsPrepareForApplicationProcessorInit(KeNumberProcessors
);
466 /* Allocate a stack for use when booting the processor */
467 ProcessorStack
= Ki386InitialStackArray
[((int)KeNumberProcessors
)] + MM_STACK_SIZE
;
469 /* Tell HAL a new CPU is being started */
470 HalStartNextProcessor(0, (ULONG
)ProcessorStack
- 2*sizeof(FX_SAVE_AREA
));
471 KeNumberProcessors
++;
474 /* Do Phase 1 HAL Initalization */
475 HalInitSystem(1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
477 /* Initialize Basic System Objects and Worker Threads */
480 /* Create the system handle table, assign it to the system process, create
481 the client id table and assign a PID for the system process. This needs
482 to be done before the worker threads are initialized so the system
483 process gets the first PID (4) */
484 PspPostInitSystemProcess();
486 /* initialize the worker threads */
487 ExpInitializeWorkerThreads();
489 /* initialize callbacks */
490 ExpInitializeCallbacks();
492 /* Initialize the GDB Stub and break */
495 /* Initialize I/O Objects, Filesystems, Error Logging and Shutdown */
499 PoInit((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
, ForceAcpiDisable
);
501 /* Initialize the Registry (Hives are NOT yet loaded!) */
502 CmInitializeRegistry();
504 /* Unmap Low memory, initialize the Page Zeroing and the Balancer Thread */
507 /* Initialize Cache Views */
510 /* Hook System Interrupt for the Debugger */
513 /* Initialize File Locking */
514 FsRtlpInitFileLockingImplementation();
516 /* Report all resources used by hal */
517 HalReportResourceUsage();
519 /* Clear the screen to blue */
520 HalInitSystem(2, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
522 /* Display version number and copyright/warranty message */
523 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR
" (Build "
524 KERNEL_VERSION_BUILD_STR
")\n");
525 HalDisplayString(RES_STR_LEGAL_COPYRIGHT
);
526 HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
527 "Public License, and you\n");
528 HalDisplayString("are welcome to change it and/or distribute copies of it "
530 HalDisplayString("conditions. There is absolutely no warranty for "
533 /* Display number of Processors */
535 "Found %d system processor(s). [%lu MB Memory]\n",
537 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
538 HalDisplayString(str
);
540 /* Print which Debugger is being used */
543 /* Import and create NLS Data and Sections */
546 /* Import and Load Registry Hives */
547 CmInitHives(SetupBoot
);
549 /* Initialize the time zone information from the registry */
550 ExpInitTimeZoneInfo();
552 /* Enter the kernel debugger before starting up the boot drivers */
557 /* Setup Drivers and Root Device Node */
560 /* Display the boot screen image if not disabled */
561 if (!NoGuiBoot
) InbvEnableBootDriver(TRUE
);
563 /* Create ARC Names, SystemRoot SymLink, Load Drivers and Assign Letters */
566 /* Load the System DLL and its Entrypoints */
567 LdrpInitializeSystemDll();
569 /* Initialize the Default Locale */
570 PiInitDefaultLocale();
572 /* Initialize shared user page. Set dos system path, dos device map, etc. */
573 InitSystemSharedUserPage ((PCHAR
)KeLoaderBlock
.CommandLine
);
575 /* Create 'ReactOSInitDone' event */
576 RtlInitUnicodeString(&EventName
, L
"\\ReactOSInitDone");
577 InitializeObjectAttributes(&ObjectAttributes
,
582 Status
= ZwCreateEvent(&InitDoneEventHandle
,
585 SynchronizationEvent
,
588 /* Check for Success */
589 if (!NT_SUCCESS(Status
)) {
591 DPRINT1("Failed to create 'ReactOSInitDone' event (Status 0x%x)\n", Status
);
592 InitDoneEventHandle
= INVALID_HANDLE_VALUE
;
595 /* Launch initial process */
596 Status
= LdrLoadInitialProcess(&ProcessHandle
,
599 /* Check for success, Bugcheck if we failed */
600 if (!NT_SUCCESS(Status
)) {
602 KEBUGCHECKEX(SESSION4_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
605 /* Wait on the Completion Event */
606 if (InitDoneEventHandle
!= INVALID_HANDLE_VALUE
) {
608 HANDLE Handles
[2]; /* Init event, Initial process */
610 /* Setup the Handles to wait on */
611 Handles
[0] = InitDoneEventHandle
;
612 Handles
[1] = ProcessHandle
;
614 /* Wait for the system to be initialized */
615 Timeout
.QuadPart
= (LONGLONG
)-1200000000; /* 120 second timeout */
616 Status
= ZwWaitForMultipleObjects(2,
621 if (!NT_SUCCESS(Status
)) {
623 DPRINT1("NtWaitForMultipleObjects failed with status 0x%x!\n", Status
);
625 } else if (Status
== STATUS_TIMEOUT
) {
627 DPRINT1("WARNING: System not initialized after 120 seconds.\n");
629 } else if (Status
== STATUS_WAIT_0
+ 1) {
631 /* Crash the system if the initial process was terminated. */
632 KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
635 /* Disable the Boot Logo */
636 if (!NoGuiBoot
) InbvEnableBootDriver(FALSE
);
638 /* Signal the Event and close the handle */
639 ZwSetEvent(InitDoneEventHandle
, NULL
);
640 ZwClose(InitDoneEventHandle
);
644 /* On failure to create 'ReactOSInitDone' event, go to text mode ASAP */
645 if (!NoGuiBoot
) InbvEnableBootDriver(FALSE
);
647 /* Crash the system if the initial process terminates within 5 seconds. */
648 Timeout
.QuadPart
= (LONGLONG
)-50000000; /* 5 second timeout */
649 Status
= ZwWaitForSingleObject(ProcessHandle
,
653 /* Check for timeout, crash if the initial process didn't initalize */
654 if (Status
!= STATUS_TIMEOUT
) KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED
, Status
, 1, 0, 0);
657 /* Enable the Clock, close remaining handles */
658 KiTimerSystemAuditing
= 1;
659 ZwClose(ThreadHandle
);
660 ZwClose(ProcessHandle
);
666 ExpInitLookasideLists();
672 ExpInitializeEventImplementation();
673 ExpInitializeEventPairImplementation();
674 ExpInitializeMutantImplementation();
675 ExpInitializeSemaphoreImplementation();
676 ExpInitializeTimerImplementation();
678 ExpInitializeProfileImplementation();
681 ExpInitializeHandleTables();