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.93 2001/04/17 23:39:25 dwelch 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/config.h>
33 #include <internal/ntoskrnl.h>
34 #include <reactos/resource.h>
35 #include <internal/mm.h>
36 #include <internal/module.h>
37 #include <internal/ldr.h>
38 #include <internal/ex.h>
39 #include <internal/ps.h>
40 #include <internal/ke.h>
41 #include <internal/io.h>
42 #include <napi/shared_data.h>
43 #include <internal/v86m.h>
44 #include <internal/kd.h>
45 #include <internal/trap.h>
46 #include <internal/config.h>
47 #include "../dbg/kdb.h"
50 #include <internal/debug.h>
52 /* GLOBALS *******************************************************************/
54 ULONG EXPORTED NtBuildNumber
= KERNEL_VERSION_BUILD
;
55 ULONG EXPORTED NtGlobalFlag
= 0;
56 CHAR EXPORTED KeNumberProcessors
;
57 LOADER_PARAMETER_BLOCK EXPORTED KeLoaderBlock
;
58 static LOADER_MODULE KeLoaderModules
[64];
59 static UCHAR KeLoaderModuleStrings
[64][256];
60 static UCHAR KeLoaderCommandLine
[256];
61 static ULONG FirstKrnlPhysAddr
;
62 static ULONG LastKrnlPhysAddr
;
63 static ULONG LastKernelAddress
;
64 volatile BOOLEAN Initialized
= FALSE
;
66 /* FUNCTIONS ****************************************************************/
69 CreateSystemRootLink (PCSZ ParameterLine
)
71 UNICODE_STRING LinkName
;
72 UNICODE_STRING DeviceName
;
73 UNICODE_STRING ArcName
;
74 UNICODE_STRING BootPath
;
80 OBJECT_ATTRIBUTES ObjectAttributes
;
83 /* create local parameter line copy */
84 ParamBuffer
= ExAllocatePool (PagedPool
, 256);
85 strcpy (ParamBuffer
, (char *)ParameterLine
);
87 DPRINT("%s\n", ParamBuffer
);
88 /* Format: <arc_name>\<path> [options...] */
91 p
= strchr (ParamBuffer
, ' ');
94 DPRINT("%s\n", ParamBuffer
);
97 p
= strchr (ParamBuffer
, '\\');
100 DPRINT("Boot path: %s\n", p
);
101 RtlCreateUnicodeStringFromAsciiz (&BootPath
, p
);
106 DPRINT("Boot path: %s\n", "\\");
107 RtlCreateUnicodeStringFromAsciiz (&BootPath
, "\\");
109 DPRINT("Arc name: %s\n", ParamBuffer
);
111 /* Only arc name left - build full arc name */
112 ArcNameBuffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
113 swprintf (ArcNameBuffer
,
114 L
"\\ArcName\\%S", ParamBuffer
);
115 RtlInitUnicodeString (&ArcName
, ArcNameBuffer
);
116 DPRINT1("Arc name: %wZ\n", &ArcName
);
118 /* free ParamBuffer */
119 ExFreePool (ParamBuffer
);
121 /* allocate device name string */
122 DeviceName
.Length
= 0;
123 DeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
124 DeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
126 InitializeObjectAttributes (&ObjectAttributes
,
132 Status
= NtOpenSymbolicLinkObject (&Handle
,
133 SYMBOLIC_LINK_ALL_ACCESS
,
135 if (!NT_SUCCESS(Status
))
137 RtlFreeUnicodeString (&BootPath
);
138 RtlFreeUnicodeString (&DeviceName
);
139 DbgPrint("NtOpenSymbolicLinkObject() '%wZ' failed (Status %x)\n",
142 RtlFreeUnicodeString (&ArcName
);
146 RtlFreeUnicodeString (&ArcName
);
148 Status
= NtQuerySymbolicLinkObject (Handle
,
152 if (!NT_SUCCESS(Status
))
154 RtlFreeUnicodeString (&BootPath
);
155 RtlFreeUnicodeString (&DeviceName
);
156 DbgPrint("NtQuerySymbolicObject() failed (Status %x)\n",
161 DPRINT("Length: %lu DeviceName: %wZ\n", Length
, &DeviceName
);
163 RtlAppendUnicodeStringToString (&DeviceName
,
166 RtlFreeUnicodeString (&BootPath
);
167 DPRINT("DeviceName: %wZ\n", &DeviceName
);
169 /* create the '\SystemRoot' link */
170 RtlInitUnicodeString (&LinkName
,
173 Status
= IoCreateSymbolicLink (&LinkName
,
175 RtlFreeUnicodeString (&DeviceName
);
176 if (!NT_SUCCESS(Status
))
178 DbgPrint("IoCreateSymbolicLink() failed (Status %x)\n",
184 /* Check if '\SystemRoot'(LinkName) can be opened, otherwise crash it! */
185 InitializeObjectAttributes (&ObjectAttributes
,
191 Status
= NtOpenSymbolicLinkObject (&Handle
,
192 SYMBOLIC_LINK_ALL_ACCESS
,
194 if (!NT_SUCCESS(Status
))
196 DbgPrint("NtOpenSymbolicLinkObject() failed to open '\\SystemRoot' (Status %x)\n",
205 InitSystemSharedUserPage (PCSZ ParameterLine
)
207 PKUSER_SHARED_DATA SharedPage
;
209 UNICODE_STRING ArcDeviceName
;
210 UNICODE_STRING ArcName
;
211 UNICODE_STRING BootPath
;
212 UNICODE_STRING DriveDeviceName
;
213 UNICODE_STRING DriveName
;
214 WCHAR DriveNameBuffer
[20];
216 PWCHAR ArcNameBuffer
;
220 OBJECT_ATTRIBUTES ObjectAttributes
;
223 BOOLEAN BootDriveFound
;
225 SharedPage
= (PKUSER_SHARED_DATA
)KERNEL_SHARED_DATA_BASE
;
226 SharedPage
->DosDeviceMap
= 0;
227 SharedPage
->NtProductType
= NtProductWinNt
;
228 for (i
= 0; i
< 32; i
++)
230 SharedPage
->DosDeviceDriveType
[i
] = 0;
233 BootDriveFound
= FALSE
;
236 * Retrieve the current dos system path
237 * (e.g.: C:\reactos) from the given arc path
238 * (e.g.: multi(0)disk(0)rdisk(0)partititon(1)\reactos)
239 * Format: "<arc_name>\<path> [options...]"
242 /* create local parameter line copy */
243 ParamBuffer
= ExAllocatePool (PagedPool
, 256);
244 strcpy (ParamBuffer
, (char *)ParameterLine
);
245 DPRINT("%s\n", ParamBuffer
);
247 /* cut options off */
248 p
= strchr (ParamBuffer
, ' ');
253 DPRINT("%s\n", ParamBuffer
);
256 p
= strchr (ParamBuffer
, '\\');
259 DPRINT("Boot path: %s\n", p
);
260 RtlCreateUnicodeStringFromAsciiz (&BootPath
, p
);
265 DPRINT("Boot path: %s\n", "\\");
266 RtlCreateUnicodeStringFromAsciiz (&BootPath
, "\\");
268 DPRINT("Arc name: %s\n", ParamBuffer
);
270 /* Only arc name left - build full arc name */
271 ArcNameBuffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
272 swprintf (ArcNameBuffer
, L
"\\ArcName\\%S", ParamBuffer
);
273 RtlInitUnicodeString (&ArcName
, ArcNameBuffer
);
274 DPRINT("Arc name: %wZ\n", &ArcName
);
276 /* free ParamBuffer */
277 ExFreePool (ParamBuffer
);
279 /* allocate arc device name string */
280 ArcDeviceName
.Length
= 0;
281 ArcDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
282 ArcDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
284 InitializeObjectAttributes (&ObjectAttributes
,
290 Status
= NtOpenSymbolicLinkObject (&Handle
,
291 SYMBOLIC_LINK_ALL_ACCESS
,
293 RtlFreeUnicodeString (&ArcName
);
294 if (!NT_SUCCESS(Status
))
296 RtlFreeUnicodeString (&BootPath
);
297 RtlFreeUnicodeString (&ArcDeviceName
);
298 DbgPrint("NtOpenSymbolicLinkObject() failed (Status %x)\n",
304 Status
= NtQuerySymbolicLinkObject (Handle
,
308 if (!NT_SUCCESS(Status
))
310 RtlFreeUnicodeString (&BootPath
);
311 RtlFreeUnicodeString (&ArcDeviceName
);
312 DbgPrint("NtQuerySymbolicObject() failed (Status %x)\n",
317 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length
, &ArcDeviceName
);
320 /* allocate device name string */
321 DriveDeviceName
.Length
= 0;
322 DriveDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
323 DriveDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
325 for (i
= 0; i
< 26; i
++)
327 swprintf (DriveNameBuffer
, L
"\\??\\%C:", 'A' + i
);
328 RtlInitUnicodeString (&DriveName
,
331 InitializeObjectAttributes (&ObjectAttributes
,
337 Status
= NtOpenSymbolicLinkObject (&Handle
,
338 SYMBOLIC_LINK_ALL_ACCESS
,
340 if (!NT_SUCCESS(Status
))
342 DPRINT("Failed to open link %wZ\n",
347 Status
= NtQuerySymbolicLinkObject (Handle
,
350 if (!NT_SUCCESS(Status
))
352 DPRINT("Failed query open link %wZ\n",
356 DPRINT("Opened link: %wZ ==> %wZ\n",
357 &DriveName
, &DriveDeviceName
);
359 if (!RtlCompareUnicodeString (&ArcDeviceName
, &DriveDeviceName
, FALSE
))
361 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i
, &BootPath
);
362 swprintf (SharedPage
->NtSystemRoot
,
363 L
"%C:%wZ", 'A' + i
, &BootPath
);
365 BootDriveFound
= TRUE
;
370 /* set bit in dos drives bitmap (drive available) */
371 SharedPage
->DosDeviceMap
|= (1<<i
);
374 RtlFreeUnicodeString (&BootPath
);
375 RtlFreeUnicodeString (&DriveDeviceName
);
376 RtlFreeUnicodeString (&ArcDeviceName
);
378 DPRINT("DosDeviceMap: 0x%x\n", SharedPage
->DosDeviceMap
);
380 if (BootDriveFound
== FALSE
)
382 DbgPrint("No system drive found!\n");
388 ExpInitializeExecutive(VOID
)
396 * Fail at runtime if someone has changed various structures without
397 * updating the offsets used for the assembler code.
399 assert(FIELD_OFFSET(KTHREAD
, InitialStack
) == KTHREAD_INITIAL_STACK
);
400 assert(FIELD_OFFSET(KTHREAD
, Teb
) == KTHREAD_TEB
);
401 assert(FIELD_OFFSET(KTHREAD
, KernelStack
) == KTHREAD_KERNEL_STACK
);
402 assert(FIELD_OFFSET(KTHREAD
, PreviousMode
) == KTHREAD_PREVIOUS_MODE
);
403 assert(FIELD_OFFSET(KTHREAD
, TrapFrame
) == KTHREAD_TRAP_FRAME
);
404 assert(FIELD_OFFSET(ETHREAD
, ThreadsProcess
) == ETHREAD_THREADS_PROCESS
);
405 assert(FIELD_OFFSET(KPROCESS
, DirectoryTableBase
) ==
406 KPROCESS_DIRECTORY_TABLE_BASE
);
407 assert(FIELD_OFFSET(KTRAP_FRAME
, Reserved9
) == KTRAP_FRAME_RESERVED9
);
408 assert(FIELD_OFFSET(KV86M_TRAP_FRAME
, regs
) == TF_REGS
);
409 assert(FIELD_OFFSET(KV86M_TRAP_FRAME
, orig_ebp
) == TF_ORIG_EBP
);
411 assert(FIELD_OFFSET(KPCR
, ExceptionList
) == KPCR_EXCEPTION_LIST
);
412 assert(FIELD_OFFSET(KPCR
, Self
) == KPCR_SELF
);
413 assert(FIELD_OFFSET(KPCR
, CurrentThread
) == KPCR_CURRENT_THREAD
);
417 KeLowerIrql(DISPATCH_LEVEL
);
421 MmInit1(FirstKrnlPhysAddr
, LastKrnlPhysAddr
, LastKernelAddress
);
424 * Initialize the kernel debugger
426 KdInitSystem (0, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
427 if (KdPollBreakIn ())
429 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
435 KeLowerIrql(PASSIVE_LEVEL
);
438 PiInitProcessManager();
441 * Display version number and copyright/warranty message
443 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR
" (Build "
444 KERNEL_VERSION_BUILD_STR
")\n");
445 HalDisplayString(RES_STR_LEGAL_COPYRIGHT
);
446 HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
447 "Public License, and you\n");
448 HalDisplayString("are welcome to change it and/or distribute copies of it "
450 HalDisplayString("conditions. There is absolutely no warranty for "
453 /* Initialize all processors */
454 KeNumberProcessors
= 0;
456 while (!HalAllProcessorsStarted())
458 if (KeNumberProcessors
!= 0)
460 KePrepareForApplicationProcessorInit(KeNumberProcessors
);
461 PsPrepareForApplicationProcessorInit(KeNumberProcessors
);
463 HalInitializeProcessor(KeNumberProcessors
);
464 KeNumberProcessors
++;
467 if (KeNumberProcessors
> 1)
469 sprintf(str
, "Found %d system processors.\n",
474 strcpy(str
, "Found 1 system processor.\n");
476 HalDisplayString(str
);
479 * Initialize various critical subsystems
481 HalInitSystem (1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
485 LdrInitModuleManagement();
486 CmInitializeRegistry();
490 /* Report all resources used by hal */
491 HalReportResourceUsage ();
494 * Enter the kernel debugger before starting up the boot drivers
501 * Initalize services loaded at boot time
503 DPRINT1("%d files loaded\n",KeLoaderBlock
.ModsCount
);
504 for (i
=0; i
< KeLoaderBlock
.ModsCount
; i
++)
506 DPRINT1("module: %s\n", KeLoaderModules
[i
].String
);
509 /* Pass 1: load registry chunks passed in */
510 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
512 start
= KeLoaderModules
[i
].ModStart
;
513 if (strcmp ((PCHAR
) start
, "REGEDIT4") == 0)
515 DPRINT1("process registry chunk at %08lx\n", start
);
516 CmImportHive((PCHAR
) start
);
520 /* Pass 2: process boot loaded drivers */
521 for (i
=1; i
< KeLoaderBlock
.ModsCount
; i
++)
523 start
= KeLoaderModules
[i
].ModStart
;
524 name
= (PCHAR
)KeLoaderModules
[i
].String
;
525 if (strcmp ((PCHAR
) start
, "REGEDIT4") != 0)
527 DPRINT1("process module '%s' at %08lx\n", name
, start
);
528 LdrProcessDriver((PVOID
)start
, name
);
532 /* Create the SystemRoot symbolic link */
533 DbgPrint("CommandLine: %s\n", (PUCHAR
)KeLoaderBlock
.CommandLine
);
535 CreateSystemRootLink ((PUCHAR
)KeLoaderBlock
.CommandLine
);
537 #ifdef DBGPRINT_FILE_LOG
538 /* On the assumption that we can now access disks start up the debug
541 #endif /* DBGPRINT_FILE_LOG */
544 CmInitializeRegistry2();
547 * Load Auto configured drivers
549 LdrLoadAutoConfigDrivers();
552 * Assign drive letters
554 IoAssignDriveLetters ((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
,
560 * Initialize shared user page:
561 * - set dos system path, dos device map, etc.
563 InitSystemSharedUserPage ((PUCHAR
)KeLoaderBlock
.CommandLine
);
566 * Launch initial process
568 LdrLoadInitialProcess();
570 PsTerminateSystemThread(STATUS_SUCCESS
);
574 KiSystemStartup(BOOLEAN BootProcessor
)
576 HalInitSystem (0, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
580 ExpInitializeExecutive();
583 /* Do application processor initialization */
584 KeApplicationProcessorInit();
585 PsApplicationProcessorInit();
586 KeLowerIrql(PASSIVE_LEVEL
);
587 PsIdleThreadMain(NULL
);
593 _main (ULONG MultiBootMagic
, PLOADER_PARAMETER_BLOCK _LoaderBlock
)
595 * FUNCTION: Called by the boot loader to start the kernel
597 * LoaderBlock = Pointer to boot parameters initialized by the boot
599 * NOTE: The boot parameters are stored in low memory which will become
600 * invalid after the memory managment is initialized so we make a local copy.
604 ULONG last_kernel_address
;
605 extern ULONG _bss_end__
;
608 * Copy the parameters to a local buffer because lowmem will go away
610 memcpy (&KeLoaderBlock
, _LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
611 memcpy (&KeLoaderModules
[1], (PVOID
)KeLoaderBlock
.ModsAddr
,
612 sizeof(LOADER_MODULE
) * KeLoaderBlock
.ModsCount
);
613 KeLoaderBlock
.ModsCount
++;
614 KeLoaderBlock
.ModsAddr
= (ULONG
)&KeLoaderModules
;
617 * FIXME: Preliminary hack!!!! Add boot device to beginning of command line.
618 * This should be done by the boot loader.
620 strcpy (KeLoaderCommandLine
,
621 "multi(0)disk(0)rdisk(0)partition(1)\\reactos /DEBUGPORT=SCREEN");
622 strcat (KeLoaderCommandLine
, (PUCHAR
)KeLoaderBlock
.CommandLine
);
624 KeLoaderBlock
.CommandLine
= (ULONG
)KeLoaderCommandLine
;
625 strcpy(KeLoaderModuleStrings
[0], "ntoskrnl.exe");
626 KeLoaderModules
[0].String
= (ULONG
)KeLoaderModuleStrings
[0];
627 KeLoaderModules
[0].ModStart
= 0xC0000000;
628 KeLoaderModules
[0].ModEnd
= PAGE_ROUND_UP((ULONG
)&_bss_end__
);
629 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
631 strcpy(KeLoaderModuleStrings
[i
], (PUCHAR
)KeLoaderModules
[i
].String
);
632 KeLoaderModules
[i
].ModStart
-= 0x200000;
633 KeLoaderModules
[i
].ModStart
+= 0xc0000000;
634 KeLoaderModules
[i
].ModEnd
-= 0x200000;
635 KeLoaderModules
[i
].ModEnd
+= 0xc0000000;
636 KeLoaderModules
[i
].String
= (ULONG
)KeLoaderModuleStrings
[i
];
639 last_kernel_address
= KeLoaderModules
[KeLoaderBlock
.ModsCount
- 1].ModEnd
;
641 FirstKrnlPhysAddr
= KeLoaderModules
[0].ModStart
- 0xc0000000 + 0x200000;
642 LastKrnlPhysAddr
= last_kernel_address
- 0xc0000000 + 0x200000;
643 LastKernelAddress
= last_kernel_address
;