3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ke/i386/kernel.c
6 * PURPOSE: Initializes the kernel
8 * PROGRAMMERS: David Welch (welch@mcmail.com)
11 /* INCLUDES *****************************************************************/
15 #include <internal/debug.h>
17 /* GLOBALS *******************************************************************/
19 ULONG KiPcrInitDone
= 0;
20 static ULONG PcrsAllocated
= 0;
21 static ULONG Ke386CpuidFlags2
, Ke386CpuidExFlags
;
22 ULONG Ke386CacheAlignment
;
23 CHAR Ke386CpuidModel
[49] = {0,};
24 ULONG Ke386L1CacheSize
;
25 BOOLEAN Ke386NoExecute
= FALSE
;
26 BOOLEAN Ke386Pae
= FALSE
;
27 BOOLEAN Ke386GlobalPagesEnabled
= FALSE
;
28 ULONG KiFastSystemCallDisable
= 1;
30 /* FUNCTIONS *****************************************************************/
32 VOID INIT_FUNCTION STATIC
35 ULONG OrigFlags
, Flags
, FinalFlags
;
37 ULONG Dummy
, Eax
, Ebx
, Ecx
, Edx
;
38 PKPCR Pcr
= KeGetCurrentKPCR();
40 Ke386CpuidFlags2
= Ke386CpuidExFlags
= 0;
41 Ke386CacheAlignment
= 32;
43 /* Try to toggle the id bit in eflags. */
44 Ke386SaveFlags(OrigFlags
);
45 Flags
= OrigFlags
^ X86_EFLAGS_ID
;
46 Ke386RestoreFlags(Flags
);
47 Ke386SaveFlags(FinalFlags
);
48 if ((OrigFlags
& X86_EFLAGS_ID
) == (FinalFlags
& X86_EFLAGS_ID
))
50 /* No cpuid supported. */
51 Pcr
->PrcbData
.CpuID
= FALSE
;
52 Pcr
->PrcbData
.CpuType
= 3;
55 Pcr
->PrcbData
.CpuID
= TRUE
;
57 /* Get the vendor name and the maximum cpuid level supported. */
58 Ki386Cpuid(0, &MaxCpuidLevel
, (PULONG
)&Pcr
->PrcbData
.VendorString
[0], (PULONG
)&Pcr
->PrcbData
.VendorString
[8], (PULONG
)&Pcr
->PrcbData
.VendorString
[4]);
59 if (MaxCpuidLevel
> 0)
61 /* Get the feature flags. */
62 Ki386Cpuid(1, &Eax
, &Ebx
, &Ke386CpuidFlags2
, &Pcr
->PrcbData
.FeatureBits
);
63 /* Get the cache alignment, if it is available */
64 if (Pcr
->PrcbData
.FeatureBits
& (1<<19))
66 Ke386CacheAlignment
= ((Ebx
>> 8) & 0xff) * 8;
68 Pcr
->PrcbData
.CpuType
= (Eax
>> 8) & 0xf;
69 Pcr
->PrcbData
.CpuStep
= (Eax
& 0xf) | ((Eax
<< 4) & 0xf00);
73 Pcr
->PrcbData
.CpuType
= 4;
76 /* Get the maximum extended cpuid level supported. */
77 Ki386Cpuid(0x80000000, &MaxCpuidLevel
, &Dummy
, &Dummy
, &Dummy
);
78 if (MaxCpuidLevel
> 0)
80 /* Get the extended feature flags. */
81 Ki386Cpuid(0x80000001, &Dummy
, &Dummy
, &Dummy
, &Ke386CpuidExFlags
);
84 /* Get the model name. */
85 if (MaxCpuidLevel
>= 0x80000004)
87 PULONG v
= (PULONG
)Ke386CpuidModel
;
88 Ki386Cpuid(0x80000002, v
, v
+ 1, v
+ 2, v
+ 3);
89 Ki386Cpuid(0x80000003, v
+ 4, v
+ 5, v
+ 6, v
+ 7);
90 Ki386Cpuid(0x80000004, v
+ 8, v
+ 9, v
+ 10, v
+ 11);
93 /* Get the L1 cache size */
94 if (MaxCpuidLevel
>= 0x80000005)
96 Ki386Cpuid(0x80000005, &Dummy
, &Dummy
, &Ecx
, &Edx
);
97 Ke386L1CacheSize
= (Ecx
>> 24)+(Edx
>> 24);
100 Ke386CacheAlignment
= Ecx
& 0xff;
104 /* Get the L2 cache size */
105 if (MaxCpuidLevel
>= 0x80000006)
107 Ki386Cpuid(0x80000006, &Dummy
, &Dummy
, &Ecx
, &Dummy
);
108 Pcr
->L2CacheSize
= Ecx
>> 16;
113 KePrepareForApplicationProcessorInit(ULONG Id
)
115 DPRINT("KePrepareForApplicationProcessorInit(Id %d)\n", Id
);
120 BootPcr
= (PKPCR
)KPCR_BASE
;
121 Pcr
= (PKPCR
)((ULONG_PTR
)KPCR_BASE
+ Id
* PAGE_SIZE
);
123 MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &PrcPfn
);
124 MmCreateVirtualMappingForKernel((PVOID
)Pcr
,
129 * Create a PCR for this processor
131 memset(Pcr
, 0, PAGE_SIZE
);
132 Pcr
->ProcessorNumber
= Id
;
133 Pcr
->Tib
.Self
= &Pcr
->Tib
;
135 Pcr
->Irql
= SYNCH_LEVEL
;
137 Pcr
->PrcbData
.MHz
= BootPcr
->PrcbData
.MHz
;
138 Pcr
->StallScaleFactor
= BootPcr
->StallScaleFactor
;
140 /* Mark the end of the exception handler list */
141 Pcr
->Tib
.ExceptionList
= (PVOID
)-1;
143 KiGdtPrepareForApplicationProcessorInit(Id
);
147 KeApplicationProcessorInit(VOID
)
152 DPRINT("KeApplicationProcessorInit()\n");
154 if (Ke386GlobalPagesEnabled
)
156 /* Enable global pages */
157 Ke386SetCr4(Ke386GetCr4() | X86_CR4_PGE
);
161 Offset
= InterlockedIncrementUL(&PcrsAllocated
) - 1;
162 Pcr
= (PKPCR
)((ULONG_PTR
)KPCR_BASE
+ Offset
* PAGE_SIZE
);
167 KiInitializeGdt(Pcr
);
169 /* Get processor information. */
172 /* Check FPU/MMX/SSE support. */
177 if (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SYSCALL
)
179 extern void KiFastCallEntry(void);
181 /* CS Selector of the target segment. */
182 Ke386Wrmsr(0x174, KERNEL_CS
, 0);
184 Ke386Wrmsr(0x175, 0, 0);
186 Ke386Wrmsr(0x176, (ULONG_PTR
)KiFastCallEntry
, 0);
190 * It is now safe to process interrupts
192 KeLowerIrql(DISPATCH_LEVEL
);
197 Ki386ApplicationProcessorInitializeTSS();
200 * Initialize a default LDT
202 Ki386InitializeLdt();
204 /* Now we can enable interrupts. */
205 Ke386EnableInterrupts();
209 KeInit1(PCHAR CommandLine
, PULONG LastKernelAddress
)
213 BOOLEAN NoExecute
= FALSE
;
215 extern USHORT KiBootGdt
[];
216 extern KTSS KiBootTss
;
219 * Initialize the initial PCR region. We can't allocate a page
220 * with MmAllocPage() here because MmInit1() has not yet been
221 * called, so we use a predefined page in low memory
224 KPCR
= (PKPCR
)KPCR_BASE
;
225 memset(KPCR
, 0, PAGE_SIZE
);
227 KPCR
->Irql
= SYNCH_LEVEL
;
228 KPCR
->Tib
.Self
= &KPCR
->Tib
;
229 KPCR
->GDT
= KiBootGdt
;
230 KPCR
->IDT
= (PUSHORT
)KiIdt
;
231 KPCR
->TSS
= &KiBootTss
;
232 KPCR
->ProcessorNumber
= 0;
236 KiInitializeGdt (NULL
);
237 Ki386BootInitializeTSS();
238 Ki386InitializeLdt();
240 /* Get processor information. */
243 /* Check FPU/MMX/SSE support. */
246 /* Mark the end of the exception handler list */
247 KPCR
->Tib
.ExceptionList
= (PVOID
)-1;
254 if (KPCR
->PrcbData
.FeatureBits
& X86_FEATURE_PGE
)
257 /* Enable global pages */
258 Ke386GlobalPagesEnabled
= TRUE
;
259 Ke386SaveFlags(Flags
);
260 Ke386DisableInterrupts();
261 Ke386SetCr4(Ke386GetCr4() | X86_CR4_PGE
);
262 Ke386RestoreFlags(Flags
);
265 /* Search for pae and noexecute */
266 p1
= (PCHAR
)KeLoaderBlock
.CommandLine
;
267 while(*p1
&& (p2
= strchr(p1
, '/')))
270 if (!_strnicmp(p2
, "PAE", 3))
272 if (p2
[3] == ' ' || p2
[3] == 0)
278 else if (!_strnicmp(p2
, "NOEXECUTE", 9))
280 if (p2
[9] == ' ' || p2
[9] == '=' || p2
[9] == 0)
291 * Make the detection of the noexecute feature more portable.
293 if(KPCR
->PrcbData
.CpuType
== 0xf &&
294 RtlCompareMemory("AuthenticAMD", KPCR
->PrcbData
.VendorString
, 12) == 12)
299 Ke386SaveFlags(Flags
);
300 Ke386DisableInterrupts();
302 Ke386Rdmsr(0xc0000080, l
, h
);
304 Ke386Wrmsr(0xc0000080, l
, h
);
305 Ke386NoExecute
= TRUE
;
306 Ke386RestoreFlags(Flags
);
315 Ke386Pae
= Ke386GetCr4() & X86_CR4_PAE
? TRUE
: FALSE
;
317 /* Enable PAE mode */
318 if ((Pae
&& (KPCR
->PrcbData
.FeatureBits
& X86_FEATURE_PAE
)) || NoExecute
)
320 MiEnablePAE((PVOID
*)LastKernelAddress
);
321 Ke386PaeEnabled
= TRUE
;
324 if (KPCR
->PrcbData
.FeatureBits
& X86_FEATURE_SYSCALL
)
326 extern void KiFastCallEntry(void);
328 /* CS Selector of the target segment. */
329 Ke386Wrmsr(0x174, KERNEL_CS
, 0);
331 Ke386Wrmsr(0x175, 0, 0);
333 Ke386Wrmsr(0x176, (ULONG_PTR
)KiFastCallEntry
, 0);
340 PKPCR Pcr
= KeGetCurrentKPCR();
342 KeInitializeBugCheck();
343 KeInitializeDispatcher();
344 KiInitializeSystemClock();
346 if (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_PAE
)
348 DPRINT("CPU supports PAE mode\n");
351 DPRINT("CPU runs in PAE mode\n");
354 DPRINT("NoExecute is enabled\n");
359 DPRINT("CPU doesn't run in PAE mode\n");
362 if ((Pcr
->PrcbData
.FeatureBits
& (X86_FEATURE_FXSR
| X86_FEATURE_MMX
| X86_FEATURE_SSE
| X86_FEATURE_SSE2
)) ||
363 (Ke386CpuidFlags2
& X86_EXT_FEATURE_SSE3
))
365 DPRINT("CPU supports" "%s%s%s%s%s" ".\n",
366 ((Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_FXSR
) ? " FXSR" : ""),
367 ((Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_MMX
) ? " MMX" : ""),
368 ((Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SSE
) ? " SSE" : ""),
369 ((Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SSE2
) ? " SSE2" : ""),
370 ((Ke386CpuidFlags2
& X86_EXT_FEATURE_SSE3
) ? " SSE3" : ""));
372 if (Ke386GetCr4() & X86_CR4_OSFXSR
)
374 DPRINT("SSE enabled.\n");
376 if (Ke386GetCr4() & X86_CR4_OSXMMEXCPT
)
378 DPRINT("Unmasked SIMD exceptions enabled.\n");
380 if (Pcr
->PrcbData
.VendorString
[0])
382 DPRINT("CPU Vendor: %s\n", Pcr
->PrcbData
.VendorString
);
384 if (Ke386CpuidModel
[0])
386 DPRINT("CPU Model: %s\n", Ke386CpuidModel
);
389 DPRINT("Ke386CacheAlignment: %d\n", Ke386CacheAlignment
);
390 if (Ke386L1CacheSize
)
392 DPRINT("Ke386L1CacheSize: %dkB\n", Ke386L1CacheSize
);
394 if (Pcr
->L2CacheSize
)
396 DPRINT("Ke386L2CacheSize: %dkB\n", Pcr
->L2CacheSize
);
401 Ki386SetProcessorFeatures(VOID
)
403 PKPCR Pcr
= KeGetCurrentKPCR();
404 OBJECT_ATTRIBUTES ObjectAttributes
;
405 UNICODE_STRING KeyName
;
406 UNICODE_STRING ValueName
;
409 KEY_VALUE_PARTIAL_INFORMATION ValueData
;
411 ULONG FastSystemCallDisable
= 0;
413 SharedUserData
->ProcessorFeatures
[PF_FLOATING_POINT_PRECISION_ERRATA
] = FALSE
;
414 SharedUserData
->ProcessorFeatures
[PF_FLOATING_POINT_EMULATED
] = FALSE
;
415 SharedUserData
->ProcessorFeatures
[PF_COMPARE_EXCHANGE_DOUBLE
] =
416 (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_CX8
);
417 SharedUserData
->ProcessorFeatures
[PF_MMX_INSTRUCTIONS_AVAILABLE
] =
418 (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_MMX
);
419 SharedUserData
->ProcessorFeatures
[PF_PPC_MOVEMEM_64BIT_OK
] = FALSE
;
420 SharedUserData
->ProcessorFeatures
[PF_ALPHA_BYTE_INSTRUCTIONS
] = FALSE
;
421 SharedUserData
->ProcessorFeatures
[PF_XMMI_INSTRUCTIONS_AVAILABLE
] =
422 (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SSE
);
423 SharedUserData
->ProcessorFeatures
[PF_3DNOW_INSTRUCTIONS_AVAILABLE
] =
424 (Ke386CpuidExFlags
& X86_EXT_FEATURE_3DNOW
);
425 SharedUserData
->ProcessorFeatures
[PF_RDTSC_INSTRUCTION_AVAILABLE
] =
426 (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_TSC
);
427 SharedUserData
->ProcessorFeatures
[PF_PAE_ENABLED
] = Ke386Pae
;
428 SharedUserData
->ProcessorFeatures
[PF_XMMI64_INSTRUCTIONS_AVAILABLE
] =
429 (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SSE2
);
431 /* Does the CPU Support Fast System Call? */
432 if (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SYSCALL
) {
434 /* FIXME: Check for Family == 6, Model < 3 and Stepping < 3 and disable */
436 /* Make sure it's not disabled in registry */
437 RtlRosInitUnicodeStringFromLiteral(&KeyName
,
438 L
"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Session Manager\\Kernel");
439 RtlRosInitUnicodeStringFromLiteral(&ValueName
,
440 L
"FastSystemCallDisable");
441 InitializeObjectAttributes(&ObjectAttributes
,
443 OBJ_CASE_INSENSITIVE
,
446 Status
= NtOpenKey(&KeyHandle
, KEY_ALL_ACCESS
, &ObjectAttributes
);
448 if (NT_SUCCESS(Status
)) {
450 /* Read the Value then Close the Key */
451 Status
= NtQueryValueKey(KeyHandle
,
453 KeyValuePartialInformation
,
457 RtlMoveMemory(&FastSystemCallDisable
, ValueData
.Data
, sizeof(ULONG
));
464 /* Disable SYSENTER/SYSEXIT, because the CPU doesn't support it */
465 FastSystemCallDisable
= 1;
469 if (FastSystemCallDisable
) {
472 SharedUserData
->SystemCall
[0] = 0x8D;
473 SharedUserData
->SystemCall
[1] = 0x54;
474 SharedUserData
->SystemCall
[2] = 0x24;
475 SharedUserData
->SystemCall
[3] = 0x08;
476 SharedUserData
->SystemCall
[4] = 0xCD;
477 SharedUserData
->SystemCall
[5] = 0x2E;
478 SharedUserData
->SystemCall
[6] = 0xC3;
483 SharedUserData
->SystemCall
[0] = 0x8B;
484 SharedUserData
->SystemCall
[1] = 0xD4;
485 SharedUserData
->SystemCall
[2] = 0x0F;
486 SharedUserData
->SystemCall
[3] = 0x34;
487 SharedUserData
->SystemCall
[4] = 0xC3;
489 /* Enable SYSENTER/SYSEXIT */
490 KiFastSystemCallDisable
= 0;