- Removed the member OldProcess from the ETHREAD structure.
[reactos.git] / reactos / ntoskrnl / ke / main.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /* $Id: main.c,v 1.193 2004/08/31 20:17:18 hbirr Exp $
20 *
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/main.c
23 * PURPOSE: Initalizes the kernel
24 * PROGRAMMER: David Welch (welch@cwcom.net)
25 * UPDATE HISTORY:
26 * 28/05/98: Created
27 */
28
29 /* INCLUDES *****************************************************************/
30
31 #include <ntoskrnl.h>
32 #include "../dbg/kdb.h"
33 #include <ntos/bootvid.h>
34 #include <napi/core.h>
35
36 #ifdef HALDBG
37 #include <internal/ntosdbg.h>
38 #else
39 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
40 #define ps
41 #else
42 #define ps(args...)
43 #endif /* HALDBG */
44
45 #endif
46
47 #define NDEBUG
48 #include <internal/debug.h>
49
50 /* GLOBALS *******************************************************************/
51
52 #ifdef __GNUC__
53 ULONG EXPORTED NtBuildNumber = KERNEL_VERSION_BUILD;
54 ULONG EXPORTED NtGlobalFlag = 0;
55 CHAR EXPORTED KeNumberProcessors;
56 LOADER_PARAMETER_BLOCK EXPORTED KeLoaderBlock;
57 ULONG EXPORTED KeDcacheFlushCount = 0;
58 ULONG EXPORTED KeIcacheFlushCount = 0;
59 #else
60 /* Microsoft-style declarations */
61 EXPORTED ULONG NtBuildNumber = KERNEL_VERSION_BUILD;
62 EXPORTED ULONG NtGlobalFlag = 0;
63 EXPORTED CHAR KeNumberProcessors;
64 EXPORTED LOADER_PARAMETER_BLOCK KeLoaderBlock;
65 EXPORTED ULONG KeDcacheFlushCount = 0;
66 EXPORTED ULONG KeIcacheFlushCount = 0;
67 #endif /* __GNUC__ */
68
69 static LOADER_MODULE KeLoaderModules[64];
70 static UCHAR KeLoaderModuleStrings[64][256];
71 static UCHAR KeLoaderCommandLine[256];
72 static ADDRESS_RANGE KeMemoryMap[64];
73 static ULONG KeMemoryMapRangeCount;
74 static ULONG FirstKrnlPhysAddr;
75 static ULONG LastKrnlPhysAddr;
76 static ULONG LastKernelAddress;
77 volatile BOOLEAN Initialized = FALSE;
78 extern ULONG MmCoreDumpType;
79 extern CHAR KiTimerSystemAuditing;
80
81 extern PVOID Ki386InitialStackArray[MAXIMUM_PROCESSORS];
82
83
84 /* FUNCTIONS ****************************************************************/
85
86 static VOID INIT_FUNCTION
87 InitSystemSharedUserPage (PCSZ ParameterLine)
88 {
89 UNICODE_STRING ArcDeviceName;
90 UNICODE_STRING ArcName;
91 UNICODE_STRING BootPath;
92 UNICODE_STRING DriveDeviceName;
93 UNICODE_STRING DriveName;
94 WCHAR DriveNameBuffer[20];
95 PCHAR ParamBuffer;
96 PWCHAR ArcNameBuffer;
97 PCHAR p;
98 NTSTATUS Status;
99 ULONG Length;
100 OBJECT_ATTRIBUTES ObjectAttributes;
101 HANDLE Handle;
102 ULONG i;
103 BOOLEAN BootDriveFound;
104
105 /*
106 * NOTE:
107 * The shared user page has been zeroed-out right after creation.
108 * There is NO need to do this again.
109 */
110
111 SharedUserData->NtProductType = NtProductWinNt;
112
113 BootDriveFound = FALSE;
114
115 /*
116 * Retrieve the current dos system path
117 * (e.g.: C:\reactos) from the given arc path
118 * (e.g.: multi(0)disk(0)rdisk(0)partititon(1)\reactos)
119 * Format: "<arc_name>\<path> [options...]"
120 */
121
122 /* create local parameter line copy */
123 ParamBuffer = ExAllocatePool (PagedPool, 256);
124 strcpy (ParamBuffer, (char *)ParameterLine);
125 DPRINT("%s\n", ParamBuffer);
126
127 /* cut options off */
128 p = strchr (ParamBuffer, ' ');
129 if (p)
130 {
131 *p = 0;
132 }
133 DPRINT("%s\n", ParamBuffer);
134
135 /* extract path */
136 p = strchr (ParamBuffer, '\\');
137 if (p)
138 {
139 DPRINT("Boot path: %s\n", p);
140 RtlCreateUnicodeStringFromAsciiz (&BootPath, p);
141 *p = 0;
142 }
143 else
144 {
145 DPRINT("Boot path: %s\n", "\\");
146 RtlCreateUnicodeStringFromAsciiz (&BootPath, "\\");
147 }
148 DPRINT("Arc name: %s\n", ParamBuffer);
149
150 /* Only arc name left - build full arc name */
151 ArcNameBuffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
152 swprintf (ArcNameBuffer, L"\\ArcName\\%S", ParamBuffer);
153 RtlInitUnicodeString (&ArcName, ArcNameBuffer);
154 DPRINT("Arc name: %wZ\n", &ArcName);
155
156 /* free ParamBuffer */
157 ExFreePool (ParamBuffer);
158
159 /* allocate arc device name string */
160 ArcDeviceName.Length = 0;
161 ArcDeviceName.MaximumLength = 256 * sizeof(WCHAR);
162 ArcDeviceName.Buffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
163
164 InitializeObjectAttributes (&ObjectAttributes,
165 &ArcName,
166 OBJ_OPENLINK,
167 NULL,
168 NULL);
169
170 Status = NtOpenSymbolicLinkObject (&Handle,
171 SYMBOLIC_LINK_ALL_ACCESS,
172 &ObjectAttributes);
173 RtlFreeUnicodeString (&ArcName);
174 if (!NT_SUCCESS(Status))
175 {
176 RtlFreeUnicodeString (&BootPath);
177 RtlFreeUnicodeString (&ArcDeviceName);
178 CPRINT("NtOpenSymbolicLinkObject() failed (Status %x)\n",
179 Status);
180
181 KEBUGCHECK (0x0);
182 }
183
184 Status = NtQuerySymbolicLinkObject (Handle,
185 &ArcDeviceName,
186 &Length);
187 NtClose (Handle);
188 if (!NT_SUCCESS(Status))
189 {
190 RtlFreeUnicodeString (&BootPath);
191 RtlFreeUnicodeString (&ArcDeviceName);
192 CPRINT("NtQuerySymbolicObject() failed (Status %x)\n",
193 Status);
194
195 KEBUGCHECK (0x0);
196 }
197 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length, &ArcDeviceName);
198
199
200 /* allocate device name string */
201 DriveDeviceName.Length = 0;
202 DriveDeviceName.MaximumLength = 256 * sizeof(WCHAR);
203 DriveDeviceName.Buffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
204
205 for (i = 0; i < 26; i++)
206 {
207 swprintf (DriveNameBuffer, L"\\??\\%C:", 'A' + i);
208 RtlInitUnicodeString (&DriveName,
209 DriveNameBuffer);
210
211 InitializeObjectAttributes (&ObjectAttributes,
212 &DriveName,
213 OBJ_OPENLINK,
214 NULL,
215 NULL);
216
217 Status = NtOpenSymbolicLinkObject (&Handle,
218 SYMBOLIC_LINK_ALL_ACCESS,
219 &ObjectAttributes);
220 if (!NT_SUCCESS(Status))
221 {
222 DPRINT("Failed to open link %wZ\n",
223 &DriveName);
224 continue;
225 }
226
227 Status = NtQuerySymbolicLinkObject (Handle,
228 &DriveDeviceName,
229 &Length);
230 if (!NT_SUCCESS(Status))
231 {
232 DPRINT("Failed query open link %wZ\n",
233 &DriveName);
234 continue;
235 }
236 DPRINT("Opened link: %wZ ==> %wZ\n",
237 &DriveName, &DriveDeviceName);
238
239 if (!RtlCompareUnicodeString (&ArcDeviceName, &DriveDeviceName, FALSE))
240 {
241 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i, &BootPath);
242 swprintf(SharedUserData->NtSystemRoot,
243 L"%C:%wZ", 'A' + i, &BootPath);
244
245 BootDriveFound = TRUE;
246 }
247
248 NtClose (Handle);
249 }
250
251 RtlFreeUnicodeString (&BootPath);
252 RtlFreeUnicodeString (&DriveDeviceName);
253 RtlFreeUnicodeString (&ArcDeviceName);
254
255 DPRINT("DosDeviceMap: 0x%x\n", SharedUserData->DosDeviceMap);
256
257 if (BootDriveFound == FALSE)
258 {
259 DbgPrint("No system drive found!\n");
260 KEBUGCHECK (NO_BOOT_DEVICE);
261 }
262 }
263
264 VOID INIT_FUNCTION
265 ExpInitializeExecutive(VOID)
266 {
267 LARGE_INTEGER Timeout;
268 HANDLE ProcessHandle;
269 HANDLE ThreadHandle;
270 ULONG i;
271 ULONG start;
272 ULONG length;
273 PCHAR name;
274 CHAR str[50];
275 NTSTATUS Status;
276 BOOLEAN SetupBoot;
277 PCHAR p1, p2;
278 ULONG MaxMem;
279 BOOLEAN NoGuiBoot = FALSE;
280 UNICODE_STRING Name;
281 HANDLE InitDoneEventHandle;
282 OBJECT_ATTRIBUTES ObjectAttributes;
283
284 /*
285 * Fail at runtime if someone has changed various structures without
286 * updating the offsets used for the assembler code.
287 */
288 assert(FIELD_OFFSET(KTHREAD, InitialStack) == KTHREAD_INITIAL_STACK);
289 assert(FIELD_OFFSET(KTHREAD, Teb) == KTHREAD_TEB);
290 assert(FIELD_OFFSET(KTHREAD, KernelStack) == KTHREAD_KERNEL_STACK);
291 assert(FIELD_OFFSET(KTHREAD, ServiceTable) == KTHREAD_SERVICE_TABLE);
292 assert(FIELD_OFFSET(KTHREAD, PreviousMode) == KTHREAD_PREVIOUS_MODE);
293 assert(FIELD_OFFSET(KTHREAD, TrapFrame) == KTHREAD_TRAP_FRAME);
294 assert(FIELD_OFFSET(KTHREAD, CallbackStack) == KTHREAD_CALLBACK_STACK);
295 assert(FIELD_OFFSET(KTHREAD, ApcState.Process) == KTHREAD_APCSTATE_PROCESS);
296 assert(FIELD_OFFSET(KPROCESS, DirectoryTableBase) ==
297 KPROCESS_DIRECTORY_TABLE_BASE);
298 assert(FIELD_OFFSET(KPROCESS, IopmOffset) == KPROCESS_IOPM_OFFSET);
299 assert(FIELD_OFFSET(KPROCESS, LdtDescriptor) == KPROCESS_LDT_DESCRIPTOR0);
300 assert(FIELD_OFFSET(KTRAP_FRAME, Reserved9) == KTRAP_FRAME_RESERVED9);
301 assert(FIELD_OFFSET(KV86M_TRAP_FRAME, regs) == TF_REGS);
302 assert(FIELD_OFFSET(KV86M_TRAP_FRAME, orig_ebp) == TF_ORIG_EBP);
303
304 assert(FIELD_OFFSET(KPCR, Tib.ExceptionList) == KPCR_EXCEPTION_LIST);
305 assert(FIELD_OFFSET(KPCR, Self) == KPCR_SELF);
306 assert(FIELD_OFFSET(IKPCR, Tib.ExceptionList) == KPCR_EXCEPTION_LIST);
307 assert(FIELD_OFFSET(IKPCR, Self) == KPCR_SELF);
308 assert(FIELD_OFFSET(IKPCR, CurrentThread) == KPCR_CURRENT_THREAD);
309
310 LdrInit1();
311
312 KeLowerIrql(DISPATCH_LEVEL);
313
314 NtEarlyInitVdm();
315
316 p1 = (PCHAR)KeLoaderBlock.CommandLine;
317
318 MaxMem = 0;
319 while(*p1 && (p2 = strchr(p1, '/')))
320 {
321 p2++;
322 if (!_strnicmp(p2, "MAXMEM", 6))
323 {
324 p2 += 6;
325 while (isspace(*p2)) p2++;
326 if (*p2 == '=')
327 {
328 p2++;
329 while(isspace(*p2)) p2++;
330 if (isdigit(*p2))
331 {
332 while (isdigit(*p2))
333 {
334 MaxMem = MaxMem * 10 + *p2 - '0';
335 p2++;
336 }
337 break;
338 }
339 }
340 }
341 else if (!_strnicmp(p2, "NOGUIBOOT", 12))
342 {
343 p2 += 12;
344 NoGuiBoot = TRUE;
345 }
346 else if (!_strnicmp(p2, "CRASHDUMP", 9))
347 {
348 p2 += 9;
349 if (*p2 == ':')
350 {
351 p2++;
352 if (!_strnicmp(p2, "FULL", 4))
353 {
354 MmCoreDumpType = MM_CORE_DUMP_TYPE_FULL;
355 }
356 else
357 {
358 MmCoreDumpType = MM_CORE_DUMP_TYPE_NONE;
359 }
360 }
361 }
362 p1 = p2;
363 }
364
365 MmInit1(FirstKrnlPhysAddr,
366 LastKrnlPhysAddr,
367 LastKernelAddress,
368 (PADDRESS_RANGE)&KeMemoryMap,
369 KeMemoryMapRangeCount,
370 MaxMem > 8 ? MaxMem : 4096);
371
372 /* Import ANSI code page table */
373 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
374 {
375 start = KeLoaderModules[i].ModStart;
376 length = KeLoaderModules[i].ModEnd - start;
377
378 name = strrchr((PCHAR)KeLoaderModules[i].String, '\\');
379 if (name == NULL)
380 {
381 name = (PCHAR)KeLoaderModules[i].String;
382 }
383 else
384 {
385 name++;
386 }
387
388 if (!_stricmp (name, "ansi.nls"))
389 {
390 RtlpImportAnsiCodePage((PUSHORT)start, length);
391 }
392 }
393
394 /* Import OEM code page table */
395 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
396 {
397 start = KeLoaderModules[i].ModStart;
398 length = KeLoaderModules[i].ModEnd - start;
399
400 name = strrchr((PCHAR)KeLoaderModules[i].String, '\\');
401 if (name == NULL)
402 {
403 name = (PCHAR)KeLoaderModules[i].String;
404 }
405 else
406 {
407 name++;
408 }
409
410 if (!_stricmp (name, "oem.nls"))
411 {
412 RtlpImportOemCodePage((PUSHORT)start, length);
413 }
414 }
415
416 /* Import Unicode casemap table */
417 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
418 {
419 start = KeLoaderModules[i].ModStart;
420 length = KeLoaderModules[i].ModEnd - start;
421
422 name = strrchr((PCHAR)KeLoaderModules[i].String, '\\');
423 if (name == NULL)
424 {
425 name = (PCHAR)KeLoaderModules[i].String;
426 }
427 else
428 {
429 name++;
430 }
431
432 if (!_stricmp (name, "casemap.nls"))
433 {
434 RtlpImportUnicodeCasemap((PUSHORT)start, length);
435 }
436 }
437
438 /* Create initial NLS tables */
439 RtlpCreateInitialNlsTables();
440
441 /*
442 * Initialize the kernel debugger
443 */
444 KdInitSystem (0, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
445
446 KeInit2();
447
448 #if 1
449 if (KeMemoryMapRangeCount > 0)
450 {
451 DPRINT1("MemoryMap:\n");
452 for (i = 0; i < KeMemoryMapRangeCount; i++)
453 {
454 switch(KeMemoryMap[i].Type)
455 {
456 case 1:
457 strcpy(str, "(usable)");
458 break;
459 case 2:
460 strcpy(str, "(reserved)");
461 break;
462 case 3:
463 strcpy(str, "(ACPI data)");
464 break;
465 case 4:
466 strcpy(str, "(ACPI NVS)");
467 break;
468 default:
469 sprintf(str, "type %lu", KeMemoryMap[i].Type);
470 }
471 DPRINT1("%08x - %08x %s\n", KeMemoryMap[i].BaseAddrLow, KeMemoryMap[i].BaseAddrLow + KeMemoryMap[i].LengthLow, str);
472 }
473 }
474 #endif
475
476 KeLowerIrql(PASSIVE_LEVEL);
477
478 if (!SeInit1())
479 KEBUGCHECK(SECURITY_INITIALIZATION_FAILED);
480
481 ObInit();
482 ExInit2();
483 MmInit2();
484
485 if (!SeInit2())
486 KEBUGCHECK(SECURITY1_INITIALIZATION_FAILED);
487
488 PiInitProcessManager();
489
490 if (KdPollBreakIn ())
491 {
492 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C);
493 }
494
495 /* Initialize all processors */
496 KeNumberProcessors = 0;
497
498 while (!HalAllProcessorsStarted())
499 {
500 PVOID ProcessorStack;
501
502 if (KeNumberProcessors != 0)
503 {
504 KePrepareForApplicationProcessorInit(KeNumberProcessors);
505 PsPrepareForApplicationProcessorInit(KeNumberProcessors);
506 }
507 /* Allocate a stack for use when booting the processor */
508 /* FIXME: The nonpaged memory for the stack is not released after use */
509 ProcessorStack =
510 (char*)ExAllocatePool(NonPagedPool, MM_STACK_SIZE) + MM_STACK_SIZE;
511 Ki386InitialStackArray[((int)KeNumberProcessors)] =
512 (PVOID)((char*)ProcessorStack - MM_STACK_SIZE);
513 HalInitializeProcessor(KeNumberProcessors, ProcessorStack);
514 KeNumberProcessors++;
515 }
516
517 /*
518 * Initialize various critical subsystems
519 */
520 HalInitSystem(1, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
521
522 ExInit3();
523 KdInit1();
524 IoInit();
525 PoInit();
526 LdrInitModuleManagement();
527 CmInitializeRegistry();
528 NtInit();
529 MmInit3();
530 CcInit();
531 KdInit2();
532 FsRtlpInitFileLockingImplementation();
533
534 /* Report all resources used by hal */
535 HalReportResourceUsage();
536
537 /*
538 * Clear the screen to blue
539 */
540 HalInitSystem(2, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
541
542 /*
543 * Display version number and copyright/warranty message
544 */
545 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR" (Build "
546 KERNEL_VERSION_BUILD_STR")\n");
547 HalDisplayString(RES_STR_LEGAL_COPYRIGHT);
548 HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
549 "Public License, and you\n");
550 HalDisplayString("are welcome to change it and/or distribute copies of it "
551 "under certain\n");
552 HalDisplayString("conditions. There is absolutely no warranty for "
553 "ReactOS.\n\n");
554
555 if (KeNumberProcessors > 1)
556 {
557 sprintf(str,
558 "Found %d system processors. [%lu MB Memory]\n",
559 KeNumberProcessors,
560 (KeLoaderBlock.MemHigher + 1088)/ 1024);
561 }
562 else
563 {
564 sprintf(str,
565 "Found 1 system processor. [%lu MB Memory]\n",
566 (KeLoaderBlock.MemHigher + 1088)/ 1024);
567 }
568 HalDisplayString(str);
569
570 KdInit3();
571
572
573 /* Create the NLS section */
574 RtlpCreateNlsSection();
575
576 /*
577 * Initalize services loaded at boot time
578 */
579 DPRINT("%d files loaded\n",KeLoaderBlock.ModsCount);
580 for (i=0; i < KeLoaderBlock.ModsCount; i++)
581 {
582 CPRINT("Module: '%s' at %08lx, length 0x%08lx\n",
583 KeLoaderModules[i].String,
584 KeLoaderModules[i].ModStart,
585 KeLoaderModules[i].ModEnd - KeLoaderModules[i].ModStart);
586 }
587
588 /* Pass 1: import system hive registry chunk */
589 SetupBoot = TRUE;
590 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
591 {
592 start = KeLoaderModules[i].ModStart;
593 length = KeLoaderModules[i].ModEnd - start;
594
595 DPRINT("Module: '%s'\n", (PCHAR)KeLoaderModules[i].String);
596 name = strrchr((PCHAR)KeLoaderModules[i].String, '\\');
597 if (name == NULL)
598 {
599 name = (PCHAR)KeLoaderModules[i].String;
600 }
601 else
602 {
603 name++;
604 }
605
606 if (!_stricmp (name, "system") ||
607 !_stricmp (name, "system.hiv"))
608 {
609 CPRINT("Process system hive registry chunk at %08lx\n", start);
610 SetupBoot = FALSE;
611 CmImportSystemHive((PCHAR)start, length);
612 }
613 }
614
615 /* Pass 2: import hardware hive registry chunk */
616 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
617 {
618 start = KeLoaderModules[i].ModStart;
619 length = KeLoaderModules[i].ModEnd - start;
620 name = (PCHAR)KeLoaderModules[i].String;
621 if (!_stricmp (name, "hardware") ||
622 !_stricmp (name, "hardware.hiv"))
623 {
624 CPRINT("Process hardware hive registry chunk at %08lx\n", start);
625 CmImportHardwareHive((PCHAR)start, length);
626 }
627 }
628
629 /* Create dummy keys if no hardware hive was found */
630 CmImportHardwareHive (NULL, 0);
631
632 /* Initialize volatile registry settings */
633 if (SetupBoot == FALSE)
634 {
635 CmInit2((PCHAR)KeLoaderBlock.CommandLine);
636 }
637
638 /*
639 * Enter the kernel debugger before starting up the boot drivers
640 */
641 #ifdef KDBG
642 KdbEnter();
643 #endif /* KDBG */
644
645 IoCreateDriverList();
646
647 IoInit2();
648
649 /* Initialize Callbacks before drivers */
650 ExpInitializeCallbacks();
651
652 /*
653 * Load boot start drivers
654 */
655 IopInitializeBootDrivers();
656
657 /* Display the boot screen image if not disabled */
658 if (!NoGuiBoot)
659 {
660 InbvEnableBootDriver(TRUE);
661 }
662
663 /* Create ARC names for boot devices */
664 IoCreateArcNames();
665
666 /* Create the SystemRoot symbolic link */
667 CPRINT("CommandLine: %s\n", (PUCHAR)KeLoaderBlock.CommandLine);
668 Status = IoCreateSystemRootLink((PUCHAR)KeLoaderBlock.CommandLine);
669 if (!NT_SUCCESS(Status))
670 KEBUGCHECK(INACCESSIBLE_BOOT_DEVICE);
671
672 #ifdef KDBG
673 KdbInitProfiling2();
674 #endif /* KDBG */
675
676 /* On the assumption that we can now access disks start up the debug
677 * logger thread */
678 if ((KdDebuggerEnabled == TRUE) && (KdDebugState & KD_DEBUG_BOOTLOG))
679 {
680 DebugLogInit2();
681 }
682
683 PiInitDefaultLocale();
684
685 /*
686 * Load services for devices found by PnP manager
687 */
688 IopInitializePnpServices(IopRootDeviceNode, FALSE);
689
690 /*
691 * Load system start drivers
692 */
693 IopInitializeSystemDrivers();
694
695 IoDestroyDriverList();
696
697 /*
698 * Assign drive letters
699 */
700 IoAssignDriveLetters ((PLOADER_PARAMETER_BLOCK)&KeLoaderBlock,
701 NULL,
702 NULL,
703 NULL);
704
705 /*
706 * Initialize shared user page:
707 * - set dos system path, dos device map, etc.
708 */
709 InitSystemSharedUserPage ((PUCHAR)KeLoaderBlock.CommandLine);
710
711 /* Create 'ReactOSInitDone' event */
712 RtlInitUnicodeString(&Name, L"\\ReactOSInitDone");
713 InitializeObjectAttributes(&ObjectAttributes,
714 &Name,
715 0,
716 NULL,
717 NULL);
718 Status = NtCreateEvent(&InitDoneEventHandle,
719 EVENT_ALL_ACCESS,
720 &ObjectAttributes,
721 FALSE, /* Synchronization event */
722 FALSE); /* Not signalled */
723 if (!NT_SUCCESS(Status))
724 {
725 DPRINT1("Failed to create 'ReactOSInitDone' event (Status 0x%x)\n", Status);
726 InitDoneEventHandle = INVALID_HANDLE_VALUE;
727 }
728
729 /*
730 * Launch initial process
731 */
732 Status = LdrLoadInitialProcess(&ProcessHandle,
733 &ThreadHandle);
734 if (!NT_SUCCESS(Status))
735 {
736 KEBUGCHECKEX(SESSION4_INITIALIZATION_FAILED, Status, 0, 0, 0);
737 }
738
739 if (InitDoneEventHandle != INVALID_HANDLE_VALUE)
740 {
741 HANDLE Handles[2]; /* Init event, Initial process */
742
743 Handles[0] = InitDoneEventHandle;
744 Handles[1] = ProcessHandle;
745
746 /* Wait for the system to be initialized */
747 #ifdef __GNUC__
748 Timeout.QuadPart = -1200000000LL; /* 120 second timeout */
749 #else
750 Timeout.QuadPart = -1200000000; /* 120 second timeout */
751 #endif
752 Status = NtWaitForMultipleObjects(((LONG) sizeof(Handles) / sizeof(HANDLE)),
753 Handles,
754 WaitAny,
755 FALSE, /* Non-alertable */
756 &Timeout);
757 if (!NT_SUCCESS(Status))
758 {
759 DPRINT1("NtWaitForMultipleObjects failed with status 0x%x!\n", Status);
760 }
761 else if (Status == STATUS_TIMEOUT)
762 {
763 DPRINT1("WARNING: System not initialized after 120 seconds.\n");
764 }
765 else if (Status == STATUS_WAIT_0 + 1)
766 {
767 /*
768 * Crash the system if the initial process was terminated.
769 */
770 KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED, Status, 0, 0, 0);
771 }
772
773 if (!NoGuiBoot)
774 {
775 InbvEnableBootDriver(FALSE);
776 }
777
778 NtSetEvent(InitDoneEventHandle, NULL);
779
780 NtClose(InitDoneEventHandle);
781 }
782 else
783 {
784 /* On failure to create 'ReactOSInitDone' event, go to text mode ASAP */
785 if (!NoGuiBoot)
786 {
787 InbvEnableBootDriver(FALSE);
788 }
789
790 /*
791 * Crash the system if the initial process terminates within 5 seconds.
792 */
793 #ifdef __GNUC__
794 Timeout.QuadPart = -50000000LL;
795 #else
796 Timeout.QuadPart = -50000000;
797 #endif
798 Status = NtWaitForSingleObject(ProcessHandle,
799 FALSE,
800 &Timeout);
801 if (Status != STATUS_TIMEOUT)
802 {
803 KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED, Status, 1, 0, 0);
804 }
805 }
806 /*
807 * Tell ke/timer.c it's okay to run.
808 */
809
810 KiTimerSystemAuditing = 1;
811
812 NtClose(ThreadHandle);
813 NtClose(ProcessHandle);
814 }
815
816 VOID __attribute((noinline))
817 KiSystemStartup(BOOLEAN BootProcessor)
818 {
819 HalInitSystem (0, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
820
821 if (BootProcessor)
822 {
823 ExpInitializeExecutive();
824 MiFreeInitMemory();
825 /* Never returns */
826 PsTerminateSystemThread(STATUS_SUCCESS);
827 KEBUGCHECK(0);
828 }
829 /* Do application processor initialization */
830 KeApplicationProcessorInit();
831 PsApplicationProcessorInit();
832 KeLowerIrql(PASSIVE_LEVEL);
833 PsIdleThreadMain(NULL);
834 KEBUGCHECK(0);
835 for(;;);
836 }
837
838 VOID INIT_FUNCTION
839 _main (ULONG MultiBootMagic, PLOADER_PARAMETER_BLOCK _LoaderBlock)
840 /*
841 * FUNCTION: Called by the boot loader to start the kernel
842 * ARGUMENTS:
843 * LoaderBlock = Pointer to boot parameters initialized by the boot
844 * loader
845 * NOTE: The boot parameters are stored in low memory which will become
846 * invalid after the memory managment is initialized so we make a local copy.
847 */
848 {
849 ULONG i;
850 ULONG size;
851 ULONG last_kernel_address;
852 extern ULONG _bss_end__;
853 ULONG HalBase;
854 ULONG DriverBase;
855 ULONG DriverSize;
856
857 /* Low level architecture specific initialization */
858 KeInit1();
859
860 /*
861 * Copy the parameters to a local buffer because lowmem will go away
862 */
863 memcpy(&KeLoaderBlock, _LoaderBlock, sizeof(LOADER_PARAMETER_BLOCK));
864 memcpy(&KeLoaderModules[1], (PVOID)KeLoaderBlock.ModsAddr,
865 sizeof(LOADER_MODULE) * KeLoaderBlock.ModsCount);
866 KeLoaderBlock.ModsCount++;
867 KeLoaderBlock.ModsAddr = (ULONG)&KeLoaderModules;
868
869 /*
870 * Convert a path specification in the grub format to one understood by the
871 * rest of the kernel.
872 */
873 if (((PUCHAR)_LoaderBlock->CommandLine)[0] == '(')
874 {
875 ULONG DiskNumber = 0, PartNumber = 0;
876 PCH p;
877 CHAR Temp[256];
878 PCH options;
879 PCH s1;
880
881 if (((PUCHAR)_LoaderBlock->CommandLine)[1] == 'h' &&
882 ((PUCHAR)_LoaderBlock->CommandLine)[2] == 'd')
883 {
884 DiskNumber = ((PUCHAR)_LoaderBlock->CommandLine)[3] - '0';
885 PartNumber = ((PUCHAR)_LoaderBlock->CommandLine)[5] - '0';
886 }
887 strcpy(Temp, &((PUCHAR)_LoaderBlock->CommandLine)[7]);
888 if ((options = strchr(Temp, ' ')) != NULL)
889 {
890 *options = 0;
891 options++;
892 }
893 else
894 {
895 options = "";
896 }
897 if ((s1 = strrchr(Temp, '/')) != NULL)
898 {
899 *s1 = 0;
900 if ((s1 = strrchr(Temp, '/')) != NULL)
901 {
902 *s1 = 0;
903 }
904 }
905 sprintf(KeLoaderCommandLine,
906 "multi(0)disk(0)rdisk(%lu)partition(%lu)%s %s",
907 DiskNumber, PartNumber + 1, Temp, options);
908
909 p = KeLoaderCommandLine;
910 while (*p != 0 && *p != ' ')
911 {
912 if ((*p) == '/')
913 {
914 (*p) = '\\';
915 }
916 p++;
917 }
918 DPRINT1("Command Line: %s\n", KeLoaderCommandLine);
919 }
920 else
921 {
922 strcpy(KeLoaderCommandLine, (PUCHAR)_LoaderBlock->CommandLine);
923 }
924 KeLoaderBlock.CommandLine = (ULONG)KeLoaderCommandLine;
925
926 strcpy(KeLoaderModuleStrings[0], "ntoskrnl.exe");
927 KeLoaderModules[0].String = (ULONG)KeLoaderModuleStrings[0];
928 KeLoaderModules[0].ModStart = KERNEL_BASE;
929 #ifdef __GNUC__
930 KeLoaderModules[0].ModEnd = PAGE_ROUND_UP((ULONG)&_bss_end__);
931 #else
932 /* Take this value from the PE... */
933 {
934 PIMAGE_NT_HEADERS NtHeader = RtlImageNtHeader((PVOID)KeLoaderModules[0].ModStart);
935 PIMAGE_OPTIONAL_HEADER OptHead = &NtHeader->OptionalHeader;
936 KeLoaderModules[0].ModEnd =
937 KeLoaderModules[0].ModStart + PAGE_ROUND_UP((ULONG)OptHead->SizeOfImage);
938 }
939 #endif
940 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
941 {
942 CHAR* s;
943 if ((s = strrchr((PUCHAR)KeLoaderModules[i].String, '/')) != 0)
944 {
945 strcpy(KeLoaderModuleStrings[i], s + 1);
946 }
947 else
948 {
949 strcpy(KeLoaderModuleStrings[i], (PUCHAR)KeLoaderModules[i].String);
950 }
951 /* TODO: Fix this hardcoded load address stuff... */
952 KeLoaderModules[i].ModStart -= 0x200000;
953 KeLoaderModules[i].ModStart += KERNEL_BASE;
954 KeLoaderModules[i].ModEnd -= 0x200000;
955 KeLoaderModules[i].ModEnd += KERNEL_BASE;
956 KeLoaderModules[i].String = (ULONG)KeLoaderModuleStrings[i];
957 }
958
959 #ifdef HAL_DBG
960 HalnInitializeDisplay((PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
961 #endif
962
963 HalBase = KeLoaderModules[1].ModStart;
964 DriverBase =
965 PAGE_ROUND_UP(KeLoaderModules[KeLoaderBlock.ModsCount - 1].ModEnd);
966
967 /*
968 * Process hal.dll
969 */
970 LdrSafePEProcessModule((PVOID)HalBase, (PVOID)DriverBase, (PVOID)KERNEL_BASE, &DriverSize);
971
972 LdrHalBase = (ULONG_PTR)DriverBase;
973 last_kernel_address = DriverBase + DriverSize;
974
975 /*
976 * Process ntoskrnl.exe
977 */
978 LdrSafePEProcessModule((PVOID)KERNEL_BASE, (PVOID)KERNEL_BASE, (PVOID)DriverBase, &DriverSize);
979
980 /* Now our imports from HAL is fixed. This is the first */
981 /* time in the boot process that we can use HAL */
982
983 FirstKrnlPhysAddr = KeLoaderModules[0].ModStart - KERNEL_BASE + 0x200000;
984 LastKrnlPhysAddr = last_kernel_address - KERNEL_BASE + 0x200000;
985 LastKernelAddress = last_kernel_address;
986
987 #ifndef ACPI
988 /* FIXME: VMware does not like it when ReactOS is using the BIOS memory map */
989 KeLoaderBlock.Flags &= ~MB_FLAGS_MMAP_INFO;
990 #endif
991
992 KeMemoryMapRangeCount = 0;
993 if (KeLoaderBlock.Flags & MB_FLAGS_MMAP_INFO)
994 {
995 /* We have a memory map from the nice BIOS */
996 size = *((PULONG)(KeLoaderBlock.MmapAddr - sizeof(ULONG)));
997 i = 0;
998 while (i < KeLoaderBlock.MmapLength)
999 {
1000 memcpy (&KeMemoryMap[KeMemoryMapRangeCount],
1001 (PVOID)(KeLoaderBlock.MmapAddr + i),
1002 sizeof(ADDRESS_RANGE));
1003 KeMemoryMapRangeCount++;
1004 i += size;
1005 }
1006 }
1007
1008 KiSystemStartup(1);
1009 }
1010
1011 /* EOF */
1012