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.113 2002/02/08 02:57:06 chorns 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 #include <ddk/ntddk.h>
32 #include <internal/ntoskrnl.h>
33 #include <reactos/resource.h>
34 #include <internal/mm.h>
35 #include <internal/module.h>
36 #include <internal/ldr.h>
37 #include <internal/ex.h>
38 #include <internal/ps.h>
39 #include <internal/ke.h>
40 #include <internal/io.h>
41 #include <internal/po.h>
42 #include <internal/cc.h>
43 //#include <internal/se.h>
44 #include <napi/shared_data.h>
45 #include <internal/v86m.h>
46 #include <internal/kd.h>
47 #include <internal/trap.h>
48 #include "../dbg/kdb.h"
49 #include <internal/registry.h>
52 #include <internal/ntosdbg.h>
58 #include <internal/debug.h>
60 /* GLOBALS *******************************************************************/
62 ULONG EXPORTED NtBuildNumber
= KERNEL_VERSION_BUILD
;
63 ULONG EXPORTED NtGlobalFlag
= 0;
64 CHAR EXPORTED KeNumberProcessors
;
65 LOADER_PARAMETER_BLOCK EXPORTED KeLoaderBlock
;
66 static LOADER_MODULE KeLoaderModules
[64];
67 static UCHAR KeLoaderModuleStrings
[64][256];
68 static UCHAR KeLoaderCommandLine
[256];
69 static ADDRESS_RANGE KeMemoryMap
[64];
70 static ULONG KeMemoryMapRangeCount
;
71 static ULONG FirstKrnlPhysAddr
;
72 static ULONG LastKrnlPhysAddr
;
73 static ULONG LastKernelAddress
;
74 volatile BOOLEAN Initialized
= FALSE
;
76 extern PVOID Ki386InitialStackArray
[MAXIMUM_PROCESSORS
];
87 SERVICE Services
[] = {
88 {L
"pci", L
"PCI Bus Driver", L
"Boot Bus Extender", 0, 1},
89 {L
"keyboard", L
"Standard Keyboard Driver", L
"Base", 0, 1},
90 {L
"blue", L
"Bluescreen Driver", L
"Base", 0, 1},
91 {L
"vidport", L
"Video Port Driver", L
"Base", 0, 1},
92 {L
"vgamp", L
"VGA Miniport", L
"Base", 0, 1},
93 {L
"minixfs", L
"Minix File System", L
"File system", 0, 1},
94 {L
"msfs", L
"Mail Slot File System", L
"File system", 0, 1},
95 {L
"npfs", L
"Named Pipe File System", L
"File system", 0, 1},
96 {L
"psaux", L
"PS/2 Auxillary Port Driver", L
"", 0, 1},
97 {L
"mouclass", L
"Mouse Class Driver", L
"Pointer Class", 0, 1},
98 {L
"ndis", L
"NDIS System Driver", L
"NDIS Wrapper", 0, 1},
99 {L
"ne2000", L
"Novell Eagle 2000 Driver", L
"NDIS", 0, 1},
100 {L
"afd", L
"AFD Networking Support Environment", L
"TDI", 0, 1},
104 /* FUNCTIONS ****************************************************************/
108 VOID
CreateDefaultRegistryForLegacyDriver(
112 WCHAR LegacyDriver
[] = L
"LegacyDriver";
114 WCHAR InstancePath
[MAX_PATH
];
115 WCHAR KeyNameBuffer
[MAX_PATH
];
116 WCHAR Name
[MAX_PATH
];
117 UNICODE_STRING KeyName
;
124 WCHAR ImagePath
[MAX_PATH
];
127 wcscpy(Name
, Service
->ServiceName
);
129 wcscpy(InstancePath
, L
"Root\\LEGACY_");
130 wcscat(InstancePath
, Name
);
131 wcscat(InstancePath
, L
"\\0000");
133 wcscpy(KeyNameBuffer
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
134 wcscat(KeyNameBuffer
, InstancePath
);
136 RtlInitUnicodeString(&KeyName
, KeyNameBuffer
);
138 DPRINT("Key name is %S\n", KeyName
.Buffer
);
140 Status
= RtlpCreateRegistryKeyPath(KeyName
.Buffer
);
141 if (!NT_SUCCESS(Status
))
143 DPRINT1("RtlpCreateRegistryKeyPath() failed with status %x\n", Status
);
147 Status
= RtlpGetRegistryHandle(
152 if (!NT_SUCCESS(Status
))
154 DPRINT1("RtlpGetRegistryHandle() failed (Status %x)\n", Status
);
159 Length
= sizeof(DWORD
);
160 Status
= RtlWriteRegistryValue(
167 if (!NT_SUCCESS(Status
))
169 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
174 Length
= (wcslen(LegacyDriver
) + 1) * sizeof(WCHAR
);
175 Status
= RtlWriteRegistryValue(
182 if (!NT_SUCCESS(Status
))
184 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
189 Length
= (wcslen(Service
->DeviceDesc
) + 1) * sizeof(WCHAR
);
190 Status
= RtlWriteRegistryValue(
197 if (!NT_SUCCESS(Status
))
199 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
205 Length
= Length
= sizeof(DWORD
);
206 Status
= RtlWriteRegistryValue(
213 if (!NT_SUCCESS(Status
))
215 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
220 Length
= (wcslen(Service
->ServiceName
) + 1) * sizeof(WCHAR
);
221 Status
= RtlWriteRegistryValue(
226 Service
->ServiceName
,
228 if (!NT_SUCCESS(Status
))
230 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
238 /* Services section */
240 Status
= RtlpGetRegistryHandle(
241 RTL_REGISTRY_SERVICES
,
242 Service
->ServiceName
,
245 if (!NT_SUCCESS(Status
))
247 DPRINT1("RtlpGetRegistryHandle() failed (Status %x)\n", Status
);
251 Length
= (wcslen(Service
->DeviceDesc
) + 1) * sizeof(WCHAR
);
252 Status
= RtlWriteRegistryValue(
259 if (!NT_SUCCESS(Status
))
261 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
267 Length
= sizeof(DWORD
);
268 Status
= RtlWriteRegistryValue(
275 if (!NT_SUCCESS(Status
))
277 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
282 Length
= (wcslen(Service
->Group
) + 1) * sizeof(WCHAR
);
283 Status
= RtlWriteRegistryValue(
290 if (!NT_SUCCESS(Status
))
292 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
297 wcscpy(ImagePath
, L
"\\SystemRoot\\System32\\drivers\\");
298 wcscat(ImagePath
, Service
->ServiceName
);
299 wcscat(ImagePath
, L
".sys");
301 Length
= (wcslen(ImagePath
) + 1) * sizeof(WCHAR
);
302 Status
= RtlWriteRegistryValue(
309 if (!NT_SUCCESS(Status
))
311 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
316 DwordData
= Service
->Start
;
317 Length
= sizeof(DWORD
);
318 Status
= RtlWriteRegistryValue(
325 if (!NT_SUCCESS(Status
))
327 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
332 DwordData
= Service
->Type
;
333 Length
= sizeof(DWORD
);
334 Status
= RtlWriteRegistryValue(
341 if (!NT_SUCCESS(Status
))
343 DPRINT1("RtlWriteRegistryValue() failed (Status %x)\n", Status
);
351 VOID
CreateDefaultRegistry()
356 Status
= RtlpCreateRegistryKeyPath(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
357 if (!NT_SUCCESS(Status
))
359 CPRINT("RtlpCreateRegistryKeyPath() (Status %x)\n", Status
);
363 Status
= RtlpCreateRegistryKeyPath(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");
364 if (!NT_SUCCESS(Status
))
366 CPRINT("RtlpCreateRegistryKeyPath() (Status %x)\n", Status
);
370 for (i
= 0; Services
[i
].ServiceName
!= NULL
; i
++)
372 CreateDefaultRegistryForLegacyDriver(&Services
[i
]);
378 RtlpCheckFileNameExtension(PCHAR FileName
,
383 Ext
= strrchr(FileName
, '.');
384 if ((Extension
== NULL
) || (*Extension
== 0))
391 if (*Extension
!= '.')
394 if (_stricmp(Ext
, Extension
) == 0)
401 CreateSystemRootLink (PCSZ ParameterLine
)
403 UNICODE_STRING LinkName
;
404 UNICODE_STRING DeviceName
;
405 UNICODE_STRING ArcName
;
406 UNICODE_STRING BootPath
;
408 PWCHAR ArcNameBuffer
;
412 OBJECT_ATTRIBUTES ObjectAttributes
;
415 /* create local parameter line copy */
416 ParamBuffer
= ExAllocatePool (PagedPool
, 256);
417 strcpy (ParamBuffer
, (char *)ParameterLine
);
419 DPRINT("%s\n", ParamBuffer
);
420 /* Format: <arc_name>\<path> [options...] */
422 /* cut options off */
423 p
= strchr (ParamBuffer
, ' ');
426 DPRINT("%s\n", ParamBuffer
);
429 p
= strchr (ParamBuffer
, '\\');
432 DPRINT("Boot path: %s\n", p
);
433 RtlCreateUnicodeStringFromAsciiz (&BootPath
, p
);
438 DPRINT("Boot path: %s\n", "\\");
439 RtlCreateUnicodeStringFromAsciiz (&BootPath
, "\\");
441 DPRINT("Arc name: %s\n", ParamBuffer
);
443 /* Only arc name left - build full arc name */
444 ArcNameBuffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
445 swprintf (ArcNameBuffer
,
446 L
"\\ArcName\\%S", ParamBuffer
);
447 RtlInitUnicodeString (&ArcName
, ArcNameBuffer
);
448 DPRINT("Arc name: %wZ\n", &ArcName
);
450 /* free ParamBuffer */
451 ExFreePool (ParamBuffer
);
453 /* allocate device name string */
454 DeviceName
.Length
= 0;
455 DeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
456 DeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
458 InitializeObjectAttributes (&ObjectAttributes
,
464 Status
= NtOpenSymbolicLinkObject (&Handle
,
465 SYMBOLIC_LINK_ALL_ACCESS
,
467 if (!NT_SUCCESS(Status
))
469 RtlFreeUnicodeString (&BootPath
);
470 RtlFreeUnicodeString (&DeviceName
);
471 DPRINT1("NtOpenSymbolicLinkObject() '%wZ' failed (Status %x)\n",
474 RtlFreeUnicodeString (&ArcName
);
478 RtlFreeUnicodeString (&ArcName
);
480 Status
= NtQuerySymbolicLinkObject (Handle
,
484 if (!NT_SUCCESS(Status
))
486 RtlFreeUnicodeString (&BootPath
);
487 RtlFreeUnicodeString (&DeviceName
);
488 CPRINT("NtQuerySymbolicObject() failed (Status %x)\n",
493 DPRINT("Length: %lu DeviceName: %wZ\n", Length
, &DeviceName
);
495 RtlAppendUnicodeStringToString (&DeviceName
,
498 RtlFreeUnicodeString (&BootPath
);
499 DPRINT("DeviceName: %wZ\n", &DeviceName
);
501 /* create the '\SystemRoot' link */
502 RtlInitUnicodeString (&LinkName
,
505 Status
= IoCreateSymbolicLink (&LinkName
,
507 RtlFreeUnicodeString (&DeviceName
);
508 if (!NT_SUCCESS(Status
))
510 CPRINT("IoCreateSymbolicLink() failed (Status %x)\n",
516 /* Check if '\SystemRoot'(LinkName) can be opened, otherwise crash it! */
517 InitializeObjectAttributes (&ObjectAttributes
,
523 Status
= NtOpenSymbolicLinkObject (&Handle
,
524 SYMBOLIC_LINK_ALL_ACCESS
,
526 if (!NT_SUCCESS(Status
))
528 CPRINT("NtOpenSymbolicLinkObject() failed to open '\\SystemRoot' (Status %x)\n",
537 InitSystemSharedUserPage (PCSZ ParameterLine
)
539 PKUSER_SHARED_DATA SharedPage
;
541 UNICODE_STRING ArcDeviceName
;
542 UNICODE_STRING ArcName
;
543 UNICODE_STRING BootPath
;
544 UNICODE_STRING DriveDeviceName
;
545 UNICODE_STRING DriveName
;
546 WCHAR DriveNameBuffer
[20];
548 PWCHAR ArcNameBuffer
;
552 OBJECT_ATTRIBUTES ObjectAttributes
;
555 BOOLEAN BootDriveFound
;
557 SharedPage
= (PKUSER_SHARED_DATA
)KERNEL_SHARED_DATA_BASE
;
558 SharedPage
->DosDeviceMap
= 0;
559 SharedPage
->NtProductType
= NtProductWinNt
;
560 for (i
= 0; i
< 32; i
++)
562 SharedPage
->DosDeviceDriveType
[i
] = 0;
565 BootDriveFound
= FALSE
;
568 * Retrieve the current dos system path
569 * (e.g.: C:\reactos) from the given arc path
570 * (e.g.: multi(0)disk(0)rdisk(0)partititon(1)\reactos)
571 * Format: "<arc_name>\<path> [options...]"
574 /* create local parameter line copy */
575 ParamBuffer
= ExAllocatePool (PagedPool
, 256);
576 strcpy (ParamBuffer
, (char *)ParameterLine
);
577 DPRINT("%s\n", ParamBuffer
);
579 /* cut options off */
580 p
= strchr (ParamBuffer
, ' ');
585 DPRINT("%s\n", ParamBuffer
);
588 p
= strchr (ParamBuffer
, '\\');
591 DPRINT("Boot path: %s\n", p
);
592 RtlCreateUnicodeStringFromAsciiz (&BootPath
, p
);
597 DPRINT("Boot path: %s\n", "\\");
598 RtlCreateUnicodeStringFromAsciiz (&BootPath
, "\\");
600 DPRINT("Arc name: %s\n", ParamBuffer
);
602 /* Only arc name left - build full arc name */
603 ArcNameBuffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
604 swprintf (ArcNameBuffer
, L
"\\ArcName\\%S", ParamBuffer
);
605 RtlInitUnicodeString (&ArcName
, ArcNameBuffer
);
606 DPRINT("Arc name: %wZ\n", &ArcName
);
608 /* free ParamBuffer */
609 ExFreePool (ParamBuffer
);
611 /* allocate arc device name string */
612 ArcDeviceName
.Length
= 0;
613 ArcDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
614 ArcDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
616 InitializeObjectAttributes (&ObjectAttributes
,
622 Status
= NtOpenSymbolicLinkObject (&Handle
,
623 SYMBOLIC_LINK_ALL_ACCESS
,
625 RtlFreeUnicodeString (&ArcName
);
626 if (!NT_SUCCESS(Status
))
628 RtlFreeUnicodeString (&BootPath
);
629 RtlFreeUnicodeString (&ArcDeviceName
);
630 CPRINT("NtOpenSymbolicLinkObject() failed (Status %x)\n",
636 Status
= NtQuerySymbolicLinkObject (Handle
,
640 if (!NT_SUCCESS(Status
))
642 RtlFreeUnicodeString (&BootPath
);
643 RtlFreeUnicodeString (&ArcDeviceName
);
644 CPRINT("NtQuerySymbolicObject() failed (Status %x)\n",
649 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length
, &ArcDeviceName
);
652 /* allocate device name string */
653 DriveDeviceName
.Length
= 0;
654 DriveDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
655 DriveDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
657 for (i
= 0; i
< 26; i
++)
659 swprintf (DriveNameBuffer
, L
"\\??\\%C:", 'A' + i
);
660 RtlInitUnicodeString (&DriveName
,
663 InitializeObjectAttributes (&ObjectAttributes
,
669 Status
= NtOpenSymbolicLinkObject (&Handle
,
670 SYMBOLIC_LINK_ALL_ACCESS
,
672 if (!NT_SUCCESS(Status
))
674 DPRINT("Failed to open link %wZ\n",
679 Status
= NtQuerySymbolicLinkObject (Handle
,
682 if (!NT_SUCCESS(Status
))
684 DPRINT("Failed query open link %wZ\n",
688 DPRINT("Opened link: %wZ ==> %wZ\n",
689 &DriveName
, &DriveDeviceName
);
691 if (!RtlCompareUnicodeString (&ArcDeviceName
, &DriveDeviceName
, FALSE
))
693 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i
, &BootPath
);
694 swprintf (SharedPage
->NtSystemRoot
,
695 L
"%C:%wZ", 'A' + i
, &BootPath
);
697 BootDriveFound
= TRUE
;
702 /* set bit in dos drives bitmap (drive available) */
703 SharedPage
->DosDeviceMap
|= (1<<i
);
706 RtlFreeUnicodeString (&BootPath
);
707 RtlFreeUnicodeString (&DriveDeviceName
);
708 RtlFreeUnicodeString (&ArcDeviceName
);
710 DPRINT("DosDeviceMap: 0x%x\n", SharedPage
->DosDeviceMap
);
712 if (BootDriveFound
== FALSE
)
714 DbgPrint("No system drive found!\n");
721 VOID
DumpBIOSMemoryMap(VOID
)
725 DbgPrint("Dumping BIOS memory map:\n");
726 DbgPrint("Memory map base: %d\n", KeLoaderBlock
.MmapAddr
);
727 DbgPrint("Memory map size: %d\n", KeLoaderBlock
.MmapLength
);
728 DbgPrint("Address range count: %d\n", KeMemoryMapRangeCount
);
729 for (i
= 0; i
< KeMemoryMapRangeCount
; i
++)
731 DbgPrint("Range: Base (%08X) Length (%08X) Type (%02X)\n",
732 KeMemoryMap
[i
].BaseAddrLow
,
733 KeMemoryMap
[i
].LengthLow
,
734 KeMemoryMap
[i
].Type
);
744 static ULONG Scratch
;
746 EXCEPTION_DISPOSITION
747 ExpUnhandledException1(
748 PEXCEPTION_RECORD ExceptionRecord
,
749 PEXCEPTION_REGISTRATION ExceptionRegistration
,
751 PVOID DispatcherContext
)
753 DbgPrint("ExpUnhandledException1() called\n");
754 DbgPrint("ExceptionRecord 0x%X\n", ExceptionRecord
);
755 DbgPrint(" Flags 0x%X\n", ExceptionRecord
->ExceptionFlags
);
756 DbgPrint("ExceptionRegistration 0x%X\n", ExceptionRegistration
);
757 DbgPrint("Context 0x%X\n", Context
);
758 DbgPrint("DispatcherContext 0x%X\n", DispatcherContext
);
760 Context
->Eax
= (ULONG
)&Scratch
;
762 return ExceptionContinueExecution
;
766 EXCEPTION_DISPOSITION
767 ExpUnhandledException2(
768 PEXCEPTION_RECORD ExceptionRecord
,
769 PEXCEPTION_REGISTRATION ExceptionRegistration
,
771 PVOID DispatcherContext
)
773 DbgPrint("ExpUnhandledException2() called\n");
774 DbgPrint("ExceptionRecord 0x%X\n", ExceptionRecord
);
775 DbgPrint(" Flags 0x%X\n", ExceptionRecord
->ExceptionFlags
);
776 DbgPrint("ExceptionRegistration 0x%X\n", ExceptionRegistration
);
777 DbgPrint("Context 0x%X\n", Context
);
778 DbgPrint("DispatcherContext 0x%X\n", DispatcherContext
);
781 Context
->Eax
= (ULONG
)&Scratch
;
783 return ExceptionContinueExecution
;
787 return ExceptionContinueSearch
;
794 // Put in mingw headers
798 PEXCEPTION_REGISTRATION RegistrationFrame
,
804 PVOID RegistrationFrame
);
806 extern EXCEPTION_DISPOSITION
809 PEXCEPTION_RECORD ExceptionRecord
,
810 PEXCEPTION_REGISTRATION RegistrationFrame
,
812 PVOID DispatcherContext
);
814 extern EXCEPTION_DISPOSITION
817 PEXCEPTION_RECORD ExceptionRecord
,
818 PEXCEPTION_REGISTRATION RegistrationFrame
,
820 PVOID DispatcherContext
);
824 PRTL_EXCEPTION_REGISTRATION
829 __asm__("movl %%ebp, %0\n\t" : "=a" (Value
));
831 return((PRTL_EXCEPTION_REGISTRATION
)Value
) - 1;
835 PRTL_EXCEPTION_REGISTRATION TestER
;
836 SCOPETABLE_ENTRY ScopeTable
;
837 PEXCEPTION_REGISTRATION OSPtr
;
840 DWORD CDECL
SEHFilterRoutine(VOID
)
842 DbgPrint("Within filter routine.\n");
843 return EXCEPTION_EXECUTE_HANDLER
;
844 //return EXCEPTION_CONTINUE_EXECUTION;
847 VOID CDECL
SEHHandlerRoutine(VOID
)
849 DbgPrint("Within exception handler.\n");
850 DbgPrint("System halted.\n");
857 RTL_EXCEPTION_REGISTRATION ER
;
858 LPEXCEPTION_POINTERS ExceptionPointers
;
859 PVOID StandardESPInFrame
;
861 __asm__ ("movl %%esp,%%eax;" : "=a" (StandardESPInFrame
));
862 DbgPrint("StandardESPInFrame: 0x%X\n", StandardESPInFrame
);
864 ExceptionPointers
= NULL
;
866 ER
.OS
.handler
= _except_handler3
;
867 __asm__ ("movl %%fs:0,%%eax;" : "=a" (ER
.OS
.prev
));
868 DbgPrint("ER.OS.prev: 0x%X\n", ER
.OS
.prev
);
870 ER
.ScopeTable
= &ScopeTable
;
871 DbgPrint("ER.ScopeTable: 0x%X\n", ER
.ScopeTable
);
873 __asm__ ("movl %%ebp,%%eax;" : "=a" (ER
.Ebp
));
874 DbgPrint("ER.Ebp: 0x%X\n", ER
.Ebp
);
876 ScopeTable
.PreviousTryLevel
= -1;
877 ScopeTable
.FilterRoutine
= SEHFilterRoutine
;
878 DbgPrint("ScopeTable.FilterRoutine: 0x%X\n", ScopeTable
.FilterRoutine
);
879 ScopeTable
.HandlerRoutine
= SEHHandlerRoutine
;
880 DbgPrint("ScopeTable.HandlerRoutine: 0x%X\n", ScopeTable
.HandlerRoutine
);
884 DbgPrint("OSPtr: 0x%X\n", OSPtr
);
886 __asm__ ("movl %0,%%eax;movl %%eax,%%fs:0;" : : "m" (OSPtr
));
888 /*__try1(__except_handler3)*/ if(1) {
889 ER
.TryLevel
= 0; // Entered first try... block
891 DbgPrint("Within guarded section.\n");
892 x
= (PULONG
)0xf2000000; *x
= 0;
893 DbgPrint("After exception.\n");
894 } /* __except1 */ if(0) {
897 DbgPrint("After exception2.\n");
899 __asm__ ("movl %0,%%eax;movl %%eax,%%fs:0;" : : "m" (ER
.OS
.prev
));
900 //KeGetCurrentKPCR()->ExceptionList = ER.OS.prev;
902 DbgPrint("Exiting.\n");
908 ExpInitializeExecutive(VOID
)
917 * Fail at runtime if someone has changed various structures without
918 * updating the offsets used for the assembler code.
920 assert(FIELD_OFFSET(KTHREAD
, InitialStack
) == KTHREAD_INITIAL_STACK
);
921 assert(FIELD_OFFSET(KTHREAD
, Teb
) == KTHREAD_TEB
);
922 assert(FIELD_OFFSET(KTHREAD
, KernelStack
) == KTHREAD_KERNEL_STACK
);
923 assert(FIELD_OFFSET(KTHREAD
, PreviousMode
) == KTHREAD_PREVIOUS_MODE
);
924 assert(FIELD_OFFSET(KTHREAD
, TrapFrame
) == KTHREAD_TRAP_FRAME
);
925 assert(FIELD_OFFSET(KTHREAD
, CallbackStack
) == KTHREAD_CALLBACK_STACK
);
926 assert(FIELD_OFFSET(ETHREAD
, ThreadsProcess
) == ETHREAD_THREADS_PROCESS
);
927 assert(FIELD_OFFSET(KPROCESS
, DirectoryTableBase
) ==
928 KPROCESS_DIRECTORY_TABLE_BASE
);
929 assert(FIELD_OFFSET(KTRAP_FRAME
, Reserved9
) == KTRAP_FRAME_RESERVED9
);
930 assert(FIELD_OFFSET(KV86M_TRAP_FRAME
, regs
) == TF_REGS
);
931 assert(FIELD_OFFSET(KV86M_TRAP_FRAME
, orig_ebp
) == TF_ORIG_EBP
);
933 assert(FIELD_OFFSET(KPCR
, ExceptionList
) == KPCR_EXCEPTION_LIST
);
934 assert(FIELD_OFFSET(KPCR
, Self
) == KPCR_SELF
);
935 assert(FIELD_OFFSET(KPCR
, CurrentThread
) == KPCR_CURRENT_THREAD
);
939 KeLowerIrql(DISPATCH_LEVEL
);
943 MmInit1(FirstKrnlPhysAddr
,
946 (PADDRESS_RANGE
)&KeMemoryMap
,
947 KeMemoryMapRangeCount
);
949 /* create default nls tables */
953 * Initialize the kernel debugger
955 KdInitSystem (0, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
960 KeLowerIrql(PASSIVE_LEVEL
);
963 PiInitProcessManager();
967 if (KdPollBreakIn ())
969 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
973 * Display version number and copyright/warranty message
975 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR
" (Build "
976 KERNEL_VERSION_BUILD_STR
")\n");
977 HalDisplayString(RES_STR_LEGAL_COPYRIGHT
);
978 HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
979 "Public License, and you\n");
980 HalDisplayString("are welcome to change it and/or distribute copies of it "
982 HalDisplayString("conditions. There is absolutely no warranty for "
985 /* Initialize all processors */
986 KeNumberProcessors
= 0;
988 while (!HalAllProcessorsStarted())
990 PVOID ProcessorStack
;
992 if (KeNumberProcessors
!= 0)
994 KePrepareForApplicationProcessorInit(KeNumberProcessors
);
995 PsPrepareForApplicationProcessorInit(KeNumberProcessors
);
997 /* Allocate a stack for use when booting the processor */
998 /* FIXME: The nonpaged memory for the stack is not released after use */
1000 ExAllocatePool(NonPagedPool
, MM_STACK_SIZE
) + MM_STACK_SIZE
;
1001 Ki386InitialStackArray
[((int)KeNumberProcessors
)] =
1002 (PVOID
)(ProcessorStack
- MM_STACK_SIZE
);
1003 HalInitializeProcessor(KeNumberProcessors
, ProcessorStack
);
1004 KeNumberProcessors
++;
1007 if (KeNumberProcessors
> 1)
1010 "Found %d system processors. [%lu MB Memory]\n",
1012 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
1017 "Found 1 system processor. [%lu MB Memory]\n",
1018 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
1020 HalDisplayString(str
);
1023 * Initialize various critical subsystems
1025 HalInitSystem(1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
1030 LdrInitModuleManagement();
1031 CmInitializeRegistry();
1037 /* Report all resources used by hal */
1038 HalReportResourceUsage();
1041 * Initalize services loaded at boot time
1043 DPRINT("%d files loaded\n",KeLoaderBlock
.ModsCount
);
1044 for (i
=0; i
< KeLoaderBlock
.ModsCount
; i
++)
1046 CPRINT("Module: '%s' at %08lx, length 0x%08lx\n",
1047 KeLoaderModules
[i
].String
,
1048 KeLoaderModules
[i
].ModStart
,
1049 KeLoaderModules
[i
].ModEnd
- KeLoaderModules
[i
].ModStart
);
1052 /* Pass 1: load nls files */
1053 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
1055 name
= (PCHAR
)KeLoaderModules
[i
].String
;
1056 if (RtlpCheckFileNameExtension(name
, ".nls"))
1058 ULONG Mod2Start
= 0;
1060 ULONG Mod3Start
= 0;
1063 name
= (PCHAR
)KeLoaderModules
[i
+1].String
;
1064 if (RtlpCheckFileNameExtension(name
, ".nls"))
1066 Mod2Start
= (ULONG
)KeLoaderModules
[i
+1].ModStart
;
1067 Mod2End
= (ULONG
)KeLoaderModules
[i
+1].ModEnd
;
1069 name
= (PCHAR
)KeLoaderModules
[i
+2].String
;
1070 if (RtlpCheckFileNameExtension(name
, ".nls"))
1072 Mod3Start
= (ULONG
)KeLoaderModules
[i
+2].ModStart
;
1073 Mod3End
= (ULONG
)KeLoaderModules
[i
+2].ModEnd
;
1077 /* Initialize nls sections */
1078 RtlpInitNlsSections((ULONG
)KeLoaderModules
[i
].ModStart
,
1079 (ULONG
)KeLoaderModules
[i
].ModEnd
,
1088 /* Pass 2: load registry chunks passed in */
1089 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
1091 start
= KeLoaderModules
[i
].ModStart
;
1092 length
= KeLoaderModules
[i
].ModEnd
- start
;
1093 name
= (PCHAR
)KeLoaderModules
[i
].String
;
1094 if (RtlpCheckFileNameExtension(name
, "") ||
1095 RtlpCheckFileNameExtension(name
, ".hiv"))
1097 CPRINT("Process registry chunk at %08lx\n", start
);
1098 CmImportHive((PCHAR
)start
, length
);
1103 * Enter the kernel debugger before starting up the boot drivers
1109 /* Pass 3: process boot loaded drivers */
1110 for (i
=1; i
< KeLoaderBlock
.ModsCount
; i
++)
1112 start
= KeLoaderModules
[i
].ModStart
;
1113 length
= KeLoaderModules
[i
].ModEnd
- start
;
1114 name
= (PCHAR
)KeLoaderModules
[i
].String
;
1115 if (RtlpCheckFileNameExtension(name
, ".sys") ||
1116 RtlpCheckFileNameExtension(name
, ".sym"))
1118 CPRINT("Processing module '%s' at %08lx, length 0x%08lx\n",
1119 name
, start
, length
);
1120 LdrProcessDriver((PVOID
)start
, name
, length
);
1124 /* Create the SystemRoot symbolic link */
1125 CPRINT("CommandLine: %s\n", (PUCHAR
)KeLoaderBlock
.CommandLine
);
1127 CreateSystemRootLink ((PUCHAR
)KeLoaderBlock
.CommandLine
);
1129 #ifdef DBGPRINT_FILE_LOG
1130 /* On the assumption that we can now access disks start up the debug
1133 #endif /* DBGPRINT_FILE_LOG */
1136 CmInitializeRegistry2();
1139 CreateDefaultRegistry();
1142 PiInitDefaultLocale();
1145 * Start the motherboard enumerator (the HAL)
1147 HalInitSystem(2, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
1150 * Load boot start drivers
1152 IopLoadBootStartDrivers();
1155 * Load Auto configured drivers
1157 LdrLoadAutoConfigDrivers();
1160 * Assign drive letters
1162 IoAssignDriveLetters ((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
,
1168 * Initialize shared user page:
1169 * - set dos system path, dos device map, etc.
1171 InitSystemSharedUserPage ((PUCHAR
)KeLoaderBlock
.CommandLine
);
1174 * Launch initial process
1176 LdrLoadInitialProcess();
1178 PsTerminateSystemThread(STATUS_SUCCESS
);
1183 KiSystemStartup(BOOLEAN BootProcessor
)
1185 HalInitSystem (0, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
1190 ExpInitializeExecutive();
1193 /* Do application processor initialization */
1194 KeApplicationProcessorInit();
1195 PsApplicationProcessorInit();
1196 KeLowerIrql(PASSIVE_LEVEL
);
1197 PsIdleThreadMain(NULL
);
1203 _main (ULONG MultiBootMagic
, PLOADER_PARAMETER_BLOCK _LoaderBlock
)
1205 * FUNCTION: Called by the boot loader to start the kernel
1207 * LoaderBlock = Pointer to boot parameters initialized by the boot
1209 * NOTE: The boot parameters are stored in low memory which will become
1210 * invalid after the memory managment is initialized so we make a local copy.
1215 ULONG last_kernel_address
;
1216 extern ULONG _bss_end__
;
1221 /* Low level architecture specific initialization */
1225 * Copy the parameters to a local buffer because lowmem will go away
1227 memcpy (&KeLoaderBlock
, _LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
1228 memcpy (&KeLoaderModules
[1], (PVOID
)KeLoaderBlock
.ModsAddr
,
1229 sizeof(LOADER_MODULE
) * KeLoaderBlock
.ModsCount
);
1230 KeLoaderBlock
.ModsCount
++;
1231 KeLoaderBlock
.ModsAddr
= (ULONG
)&KeLoaderModules
;
1234 * FIXME: Preliminary hack!!!! Add boot device to beginning of command line.
1235 * This should be done by the boot loader.
1237 strcpy (KeLoaderCommandLine
,
1238 "multi(0)disk(0)rdisk(0)partition(1)\\reactos /DEBUGPORT=SCREEN");
1239 strcat (KeLoaderCommandLine
, (PUCHAR
)KeLoaderBlock
.CommandLine
);
1241 KeLoaderBlock
.CommandLine
= (ULONG
)KeLoaderCommandLine
;
1242 strcpy(KeLoaderModuleStrings
[0], "ntoskrnl.exe");
1243 KeLoaderModules
[0].String
= (ULONG
)KeLoaderModuleStrings
[0];
1244 KeLoaderModules
[0].ModStart
= 0xC0000000;
1245 KeLoaderModules
[0].ModEnd
= PAGE_ROUND_UP((ULONG
)&_bss_end__
);
1246 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
1248 strcpy(KeLoaderModuleStrings
[i
], (PUCHAR
)KeLoaderModules
[i
].String
);
1249 KeLoaderModules
[i
].ModStart
-= 0x200000;
1250 KeLoaderModules
[i
].ModStart
+= 0xc0000000;
1251 KeLoaderModules
[i
].ModEnd
-= 0x200000;
1252 KeLoaderModules
[i
].ModEnd
+= 0xc0000000;
1253 KeLoaderModules
[i
].String
= (ULONG
)KeLoaderModuleStrings
[i
];
1257 HalnInitializeDisplay((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
1260 HalBase
= KeLoaderModules
[1].ModStart
;
1261 DriverBase
= KeLoaderModules
[KeLoaderBlock
.ModsCount
- 1].ModEnd
;
1266 LdrSafePEProcessModule((PVOID
)HalBase
, (PVOID
)DriverBase
, (PVOID
)0xC0000000, &DriverSize
);
1268 LdrHalBase
= (ULONG_PTR
)DriverBase
;
1269 last_kernel_address
= DriverBase
+ DriverSize
;
1272 * Process ntoskrnl.exe
1274 LdrSafePEProcessModule((PVOID
)0xC0000000, (PVOID
)0xC0000000, (PVOID
)DriverBase
, &DriverSize
);
1276 FirstKrnlPhysAddr
= KeLoaderModules
[0].ModStart
- 0xc0000000 + 0x200000;
1277 LastKrnlPhysAddr
= last_kernel_address
- 0xc0000000 + 0x200000;
1278 LastKernelAddress
= last_kernel_address
;
1281 /* FIXME: VMware does not like it when ReactOS is using the BIOS memory map */
1282 KeLoaderBlock
.Flags
&= ~MB_FLAGS_MMAP_INFO
;
1285 KeMemoryMapRangeCount
= 0;
1286 if (KeLoaderBlock
.Flags
& MB_FLAGS_MMAP_INFO
)
1288 /* We have a memory map from the nice BIOS */
1289 size
= *((PULONG
)(KeLoaderBlock
.MmapAddr
- sizeof(ULONG
)));
1291 while (i
< KeLoaderBlock
.MmapLength
)
1293 memcpy (&KeMemoryMap
[KeMemoryMapRangeCount
],
1294 (PVOID
)(KeLoaderBlock
.MmapAddr
+ i
),
1295 sizeof(ADDRESS_RANGE
));
1296 KeMemoryMapRangeCount
++;