SMP scheduling fixes
[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.93 2001/04/17 23:39:25 dwelch 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 <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"
48
49 #define NDEBUG
50 #include <internal/debug.h>
51
52 /* GLOBALS *******************************************************************/
53
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;
65
66 /* FUNCTIONS ****************************************************************/
67
68 static VOID
69 CreateSystemRootLink (PCSZ ParameterLine)
70 {
71 UNICODE_STRING LinkName;
72 UNICODE_STRING DeviceName;
73 UNICODE_STRING ArcName;
74 UNICODE_STRING BootPath;
75 PCHAR ParamBuffer;
76 PWCHAR ArcNameBuffer;
77 PCHAR p;
78 NTSTATUS Status;
79 ULONG Length;
80 OBJECT_ATTRIBUTES ObjectAttributes;
81 HANDLE Handle;
82
83 /* create local parameter line copy */
84 ParamBuffer = ExAllocatePool (PagedPool, 256);
85 strcpy (ParamBuffer, (char *)ParameterLine);
86
87 DPRINT("%s\n", ParamBuffer);
88 /* Format: <arc_name>\<path> [options...] */
89
90 /* cut options off */
91 p = strchr (ParamBuffer, ' ');
92 if (p)
93 *p = 0;
94 DPRINT("%s\n", ParamBuffer);
95
96 /* extract path */
97 p = strchr (ParamBuffer, '\\');
98 if (p)
99 {
100 DPRINT("Boot path: %s\n", p);
101 RtlCreateUnicodeStringFromAsciiz (&BootPath, p);
102 *p = 0;
103 }
104 else
105 {
106 DPRINT("Boot path: %s\n", "\\");
107 RtlCreateUnicodeStringFromAsciiz (&BootPath, "\\");
108 }
109 DPRINT("Arc name: %s\n", ParamBuffer);
110
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);
117
118 /* free ParamBuffer */
119 ExFreePool (ParamBuffer);
120
121 /* allocate device name string */
122 DeviceName.Length = 0;
123 DeviceName.MaximumLength = 256 * sizeof(WCHAR);
124 DeviceName.Buffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
125
126 InitializeObjectAttributes (&ObjectAttributes,
127 &ArcName,
128 0,
129 NULL,
130 NULL);
131
132 Status = NtOpenSymbolicLinkObject (&Handle,
133 SYMBOLIC_LINK_ALL_ACCESS,
134 &ObjectAttributes);
135 if (!NT_SUCCESS(Status))
136 {
137 RtlFreeUnicodeString (&BootPath);
138 RtlFreeUnicodeString (&DeviceName);
139 DbgPrint("NtOpenSymbolicLinkObject() '%wZ' failed (Status %x)\n",
140 &ArcName,
141 Status);
142 RtlFreeUnicodeString (&ArcName);
143
144 KeBugCheck (0x0);
145 }
146 RtlFreeUnicodeString (&ArcName);
147
148 Status = NtQuerySymbolicLinkObject (Handle,
149 &DeviceName,
150 &Length);
151 NtClose (Handle);
152 if (!NT_SUCCESS(Status))
153 {
154 RtlFreeUnicodeString (&BootPath);
155 RtlFreeUnicodeString (&DeviceName);
156 DbgPrint("NtQuerySymbolicObject() failed (Status %x)\n",
157 Status);
158
159 KeBugCheck (0x0);
160 }
161 DPRINT("Length: %lu DeviceName: %wZ\n", Length, &DeviceName);
162
163 RtlAppendUnicodeStringToString (&DeviceName,
164 &BootPath);
165
166 RtlFreeUnicodeString (&BootPath);
167 DPRINT("DeviceName: %wZ\n", &DeviceName);
168
169 /* create the '\SystemRoot' link */
170 RtlInitUnicodeString (&LinkName,
171 L"\\SystemRoot");
172
173 Status = IoCreateSymbolicLink (&LinkName,
174 &DeviceName);
175 RtlFreeUnicodeString (&DeviceName);
176 if (!NT_SUCCESS(Status))
177 {
178 DbgPrint("IoCreateSymbolicLink() failed (Status %x)\n",
179 Status);
180
181 KeBugCheck (0x0);
182 }
183
184 /* Check if '\SystemRoot'(LinkName) can be opened, otherwise crash it! */
185 InitializeObjectAttributes (&ObjectAttributes,
186 &LinkName,
187 0,
188 NULL,
189 NULL);
190
191 Status = NtOpenSymbolicLinkObject (&Handle,
192 SYMBOLIC_LINK_ALL_ACCESS,
193 &ObjectAttributes);
194 if (!NT_SUCCESS(Status))
195 {
196 DbgPrint("NtOpenSymbolicLinkObject() failed to open '\\SystemRoot' (Status %x)\n",
197 Status);
198 KeBugCheck (0x0);
199 }
200 NtClose(Handle);
201 }
202
203
204 static VOID
205 InitSystemSharedUserPage (PCSZ ParameterLine)
206 {
207 PKUSER_SHARED_DATA SharedPage;
208
209 UNICODE_STRING ArcDeviceName;
210 UNICODE_STRING ArcName;
211 UNICODE_STRING BootPath;
212 UNICODE_STRING DriveDeviceName;
213 UNICODE_STRING DriveName;
214 WCHAR DriveNameBuffer[20];
215 PCHAR ParamBuffer;
216 PWCHAR ArcNameBuffer;
217 PCHAR p;
218 NTSTATUS Status;
219 ULONG Length;
220 OBJECT_ATTRIBUTES ObjectAttributes;
221 HANDLE Handle;
222 ULONG i;
223 BOOLEAN BootDriveFound;
224
225 SharedPage = (PKUSER_SHARED_DATA)KERNEL_SHARED_DATA_BASE;
226 SharedPage->DosDeviceMap = 0;
227 SharedPage->NtProductType = NtProductWinNt;
228 for (i = 0; i < 32; i++)
229 {
230 SharedPage->DosDeviceDriveType[i] = 0;
231 }
232
233 BootDriveFound = FALSE;
234
235 /*
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...]"
240 */
241
242 /* create local parameter line copy */
243 ParamBuffer = ExAllocatePool (PagedPool, 256);
244 strcpy (ParamBuffer, (char *)ParameterLine);
245 DPRINT("%s\n", ParamBuffer);
246
247 /* cut options off */
248 p = strchr (ParamBuffer, ' ');
249 if (p)
250 {
251 *p = 0;
252 }
253 DPRINT("%s\n", ParamBuffer);
254
255 /* extract path */
256 p = strchr (ParamBuffer, '\\');
257 if (p)
258 {
259 DPRINT("Boot path: %s\n", p);
260 RtlCreateUnicodeStringFromAsciiz (&BootPath, p);
261 *p = 0;
262 }
263 else
264 {
265 DPRINT("Boot path: %s\n", "\\");
266 RtlCreateUnicodeStringFromAsciiz (&BootPath, "\\");
267 }
268 DPRINT("Arc name: %s\n", ParamBuffer);
269
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);
275
276 /* free ParamBuffer */
277 ExFreePool (ParamBuffer);
278
279 /* allocate arc device name string */
280 ArcDeviceName.Length = 0;
281 ArcDeviceName.MaximumLength = 256 * sizeof(WCHAR);
282 ArcDeviceName.Buffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
283
284 InitializeObjectAttributes (&ObjectAttributes,
285 &ArcName,
286 0,
287 NULL,
288 NULL);
289
290 Status = NtOpenSymbolicLinkObject (&Handle,
291 SYMBOLIC_LINK_ALL_ACCESS,
292 &ObjectAttributes);
293 RtlFreeUnicodeString (&ArcName);
294 if (!NT_SUCCESS(Status))
295 {
296 RtlFreeUnicodeString (&BootPath);
297 RtlFreeUnicodeString (&ArcDeviceName);
298 DbgPrint("NtOpenSymbolicLinkObject() failed (Status %x)\n",
299 Status);
300
301 KeBugCheck (0x0);
302 }
303
304 Status = NtQuerySymbolicLinkObject (Handle,
305 &ArcDeviceName,
306 &Length);
307 NtClose (Handle);
308 if (!NT_SUCCESS(Status))
309 {
310 RtlFreeUnicodeString (&BootPath);
311 RtlFreeUnicodeString (&ArcDeviceName);
312 DbgPrint("NtQuerySymbolicObject() failed (Status %x)\n",
313 Status);
314
315 KeBugCheck (0x0);
316 }
317 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length, &ArcDeviceName);
318
319
320 /* allocate device name string */
321 DriveDeviceName.Length = 0;
322 DriveDeviceName.MaximumLength = 256 * sizeof(WCHAR);
323 DriveDeviceName.Buffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
324
325 for (i = 0; i < 26; i++)
326 {
327 swprintf (DriveNameBuffer, L"\\??\\%C:", 'A' + i);
328 RtlInitUnicodeString (&DriveName,
329 DriveNameBuffer);
330
331 InitializeObjectAttributes (&ObjectAttributes,
332 &DriveName,
333 0,
334 NULL,
335 NULL);
336
337 Status = NtOpenSymbolicLinkObject (&Handle,
338 SYMBOLIC_LINK_ALL_ACCESS,
339 &ObjectAttributes);
340 if (!NT_SUCCESS(Status))
341 {
342 DPRINT("Failed to open link %wZ\n",
343 &DriveName);
344 continue;
345 }
346
347 Status = NtQuerySymbolicLinkObject (Handle,
348 &DriveDeviceName,
349 &Length);
350 if (!NT_SUCCESS(Status))
351 {
352 DPRINT("Failed query open link %wZ\n",
353 &DriveName);
354 continue;
355 }
356 DPRINT("Opened link: %wZ ==> %wZ\n",
357 &DriveName, &DriveDeviceName);
358
359 if (!RtlCompareUnicodeString (&ArcDeviceName, &DriveDeviceName, FALSE))
360 {
361 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i, &BootPath);
362 swprintf (SharedPage->NtSystemRoot,
363 L"%C:%wZ", 'A' + i, &BootPath);
364
365 BootDriveFound = TRUE;
366 }
367
368 NtClose (Handle);
369
370 /* set bit in dos drives bitmap (drive available) */
371 SharedPage->DosDeviceMap |= (1<<i);
372 }
373
374 RtlFreeUnicodeString (&BootPath);
375 RtlFreeUnicodeString (&DriveDeviceName);
376 RtlFreeUnicodeString (&ArcDeviceName);
377
378 DPRINT("DosDeviceMap: 0x%x\n", SharedPage->DosDeviceMap);
379
380 if (BootDriveFound == FALSE)
381 {
382 DbgPrint("No system drive found!\n");
383 KeBugCheck (0x0);
384 }
385 }
386
387 VOID
388 ExpInitializeExecutive(VOID)
389 {
390 ULONG i;
391 ULONG start;
392 PCHAR name;
393 CHAR str[50];
394
395 /*
396 * Fail at runtime if someone has changed various structures without
397 * updating the offsets used for the assembler code.
398 */
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);
410
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);
414
415 LdrInit1();
416
417 KeLowerIrql(DISPATCH_LEVEL);
418
419 NtEarlyInitVdm();
420
421 MmInit1(FirstKrnlPhysAddr, LastKrnlPhysAddr, LastKernelAddress);
422
423 /*
424 * Initialize the kernel debugger
425 */
426 KdInitSystem (0, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
427 if (KdPollBreakIn ())
428 {
429 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C);
430 }
431
432 MmInit2();
433 KeInit2();
434
435 KeLowerIrql(PASSIVE_LEVEL);
436
437 ObInit();
438 PiInitProcessManager();
439
440 /*
441 * Display version number and copyright/warranty message
442 */
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 "
449 "under certain\n");
450 HalDisplayString("conditions. There is absolutely no warranty for "
451 "ReactOS.\n");
452
453 /* Initialize all processors */
454 KeNumberProcessors = 0;
455
456 while (!HalAllProcessorsStarted())
457 {
458 if (KeNumberProcessors != 0)
459 {
460 KePrepareForApplicationProcessorInit(KeNumberProcessors);
461 PsPrepareForApplicationProcessorInit(KeNumberProcessors);
462 }
463 HalInitializeProcessor(KeNumberProcessors);
464 KeNumberProcessors++;
465 }
466
467 if (KeNumberProcessors > 1)
468 {
469 sprintf(str, "Found %d system processors.\n",
470 KeNumberProcessors);
471 }
472 else
473 {
474 strcpy(str, "Found 1 system processor.\n");
475 }
476 HalDisplayString(str);
477
478 /*
479 * Initialize various critical subsystems
480 */
481 HalInitSystem (1, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
482
483 ExInit();
484 IoInit();
485 LdrInitModuleManagement();
486 CmInitializeRegistry();
487 NtInit();
488 MmInit3();
489
490 /* Report all resources used by hal */
491 HalReportResourceUsage ();
492
493 /*
494 * Enter the kernel debugger before starting up the boot drivers
495 */
496 #ifdef KDBG
497 KdbEnter();
498 #endif /* KDBG */
499
500 /*
501 * Initalize services loaded at boot time
502 */
503 DPRINT1("%d files loaded\n",KeLoaderBlock.ModsCount);
504 for (i=0; i < KeLoaderBlock.ModsCount; i++)
505 {
506 DPRINT1("module: %s\n", KeLoaderModules[i].String);
507 }
508
509 /* Pass 1: load registry chunks passed in */
510 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
511 {
512 start = KeLoaderModules[i].ModStart;
513 if (strcmp ((PCHAR) start, "REGEDIT4") == 0)
514 {
515 DPRINT1("process registry chunk at %08lx\n", start);
516 CmImportHive((PCHAR) start);
517 }
518 }
519
520 /* Pass 2: process boot loaded drivers */
521 for (i=1; i < KeLoaderBlock.ModsCount; i++)
522 {
523 start = KeLoaderModules[i].ModStart;
524 name = (PCHAR)KeLoaderModules[i].String;
525 if (strcmp ((PCHAR) start, "REGEDIT4") != 0)
526 {
527 DPRINT1("process module '%s' at %08lx\n", name, start);
528 LdrProcessDriver((PVOID)start, name);
529 }
530 }
531
532 /* Create the SystemRoot symbolic link */
533 DbgPrint("CommandLine: %s\n", (PUCHAR)KeLoaderBlock.CommandLine);
534
535 CreateSystemRootLink ((PUCHAR)KeLoaderBlock.CommandLine);
536
537 #ifdef DBGPRINT_FILE_LOG
538 /* On the assumption that we can now access disks start up the debug
539 logger thread */
540 DebugLogInit2();
541 #endif /* DBGPRINT_FILE_LOG */
542
543
544 CmInitializeRegistry2();
545
546 /*
547 * Load Auto configured drivers
548 */
549 LdrLoadAutoConfigDrivers();
550
551 /*
552 * Assign drive letters
553 */
554 IoAssignDriveLetters ((PLOADER_PARAMETER_BLOCK)&KeLoaderBlock,
555 NULL,
556 NULL,
557 NULL);
558
559 /*
560 * Initialize shared user page:
561 * - set dos system path, dos device map, etc.
562 */
563 InitSystemSharedUserPage ((PUCHAR)KeLoaderBlock.CommandLine);
564
565 /*
566 * Launch initial process
567 */
568 LdrLoadInitialProcess();
569
570 PsTerminateSystemThread(STATUS_SUCCESS);
571 }
572
573 VOID
574 KiSystemStartup(BOOLEAN BootProcessor)
575 {
576 HalInitSystem (0, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
577 if (BootProcessor)
578 {
579 /* Never returns */
580 ExpInitializeExecutive();
581 KeBugCheck(0);
582 }
583 /* Do application processor initialization */
584 KeApplicationProcessorInit();
585 PsApplicationProcessorInit();
586 KeLowerIrql(PASSIVE_LEVEL);
587 PsIdleThreadMain(NULL);
588 KeBugCheck(0);
589 for(;;);
590 }
591
592 VOID
593 _main (ULONG MultiBootMagic, PLOADER_PARAMETER_BLOCK _LoaderBlock)
594 /*
595 * FUNCTION: Called by the boot loader to start the kernel
596 * ARGUMENTS:
597 * LoaderBlock = Pointer to boot parameters initialized by the boot
598 * loader
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.
601 */
602 {
603 ULONG i;
604 ULONG last_kernel_address;
605 extern ULONG _bss_end__;
606
607 /*
608 * Copy the parameters to a local buffer because lowmem will go away
609 */
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;
615
616 /*
617 * FIXME: Preliminary hack!!!! Add boot device to beginning of command line.
618 * This should be done by the boot loader.
619 */
620 strcpy (KeLoaderCommandLine,
621 "multi(0)disk(0)rdisk(0)partition(1)\\reactos /DEBUGPORT=SCREEN");
622 strcat (KeLoaderCommandLine, (PUCHAR)KeLoaderBlock.CommandLine);
623
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++)
630 {
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];
637 }
638
639 last_kernel_address = KeLoaderModules[KeLoaderBlock.ModsCount - 1].ModEnd;
640
641 FirstKrnlPhysAddr = KeLoaderModules[0].ModStart - 0xc0000000 + 0x200000;
642 LastKrnlPhysAddr = last_kernel_address - 0xc0000000 + 0x200000;
643 LastKernelAddress = last_kernel_address;
644
645 KeInit1();
646
647 KiSystemStartup(1);
648 }
649
650 /* EOF */
651