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
, Ke386CpuidExMisc
;
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
, 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
);
49 Pcr
->PrcbData
.LogicalProcessorsPerPhysicalProcessor
= 1;
50 Pcr
->PrcbData
.InitialApicId
= 0xff;
52 if ((OrigFlags
& X86_EFLAGS_ID
) == (FinalFlags
& X86_EFLAGS_ID
))
54 /* No cpuid supported. */
55 Pcr
->PrcbData
.CpuID
= FALSE
;
56 Pcr
->PrcbData
.CpuType
= 3;
59 Pcr
->PrcbData
.CpuID
= TRUE
;
61 /* Get the vendor name and the maximum cpuid level supported. */
62 Ki386Cpuid(0, &MaxCpuidLevel
, (PULONG
)&Pcr
->PrcbData
.VendorString
[0], (PULONG
)&Pcr
->PrcbData
.VendorString
[8], (PULONG
)&Pcr
->PrcbData
.VendorString
[4]);
63 if (MaxCpuidLevel
> 0)
65 /* Get the feature flags. */
66 Ki386Cpuid(1, &Eax
, &Ke386CpuidExMisc
, &Ke386CpuidFlags2
, &Pcr
->PrcbData
.FeatureBits
);
67 /* Get the cache alignment, if it is available */
68 if (Pcr
->PrcbData
.FeatureBits
& (1<<19))
70 Ke386CacheAlignment
= ((Ke386CpuidExMisc
>> 8) & 0xff) * 8;
72 Pcr
->PrcbData
.CpuType
= (Eax
>> 8) & 0xf;
73 Pcr
->PrcbData
.CpuStep
= (Eax
& 0xf) | ((Eax
<< 4) & 0xf00);
75 Pcr
->PrcbData
.InitialApicId
= (Ke386CpuidExMisc
>> 24) & 0xff;
77 /* detect Hyper-Threading on Pentium 4 CPUs or later */
78 if ((Pcr
->PrcbData
.CpuType
== 0xf || (Eax
& 0x0f00000)) &&
79 !strncmp(Pcr
->PrcbData
.VendorString
, "GenuineIntel", 12) &&
80 Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_HT
)
82 Pcr
->PrcbData
.LogicalProcessorsPerPhysicalProcessor
= (Ke386CpuidExMisc
>> 16) & 0xff;
87 Pcr
->PrcbData
.CpuType
= 4;
90 /* Get the maximum extended cpuid level supported. */
91 Ki386Cpuid(0x80000000, &MaxCpuidLevel
, &Dummy
, &Dummy
, &Dummy
);
92 if (MaxCpuidLevel
> 0)
94 /* Get the extended feature flags. */
95 Ki386Cpuid(0x80000001, &Dummy
, &Dummy
, &Dummy
, &Ke386CpuidExFlags
);
98 /* Get the model name. */
99 if (MaxCpuidLevel
>= 0x80000004)
101 PULONG v
= (PULONG
)Ke386CpuidModel
;
102 Ki386Cpuid(0x80000002, v
, v
+ 1, v
+ 2, v
+ 3);
103 Ki386Cpuid(0x80000003, v
+ 4, v
+ 5, v
+ 6, v
+ 7);
104 Ki386Cpuid(0x80000004, v
+ 8, v
+ 9, v
+ 10, v
+ 11);
107 /* Get the L1 cache size */
108 if (MaxCpuidLevel
>= 0x80000005)
110 Ki386Cpuid(0x80000005, &Dummy
, &Dummy
, &Ecx
, &Edx
);
111 Ke386L1CacheSize
= (Ecx
>> 24)+(Edx
>> 24);
112 if ((Ecx
& 0xff) > 0)
114 Ke386CacheAlignment
= Ecx
& 0xff;
118 /* Get the L2 cache size */
119 if (MaxCpuidLevel
>= 0x80000006)
121 Ki386Cpuid(0x80000006, &Dummy
, &Dummy
, &Ecx
, &Dummy
);
122 Pcr
->L2CacheSize
= Ecx
>> 16;
127 KePrepareForApplicationProcessorInit(ULONG Id
)
129 DPRINT("KePrepareForApplicationProcessorInit(Id %d)\n", Id
);
134 BootPcr
= (PKPCR
)KPCR_BASE
;
135 Pcr
= (PKPCR
)((ULONG_PTR
)KPCR_BASE
+ Id
* PAGE_SIZE
);
137 MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &PrcPfn
);
138 MmCreateVirtualMappingForKernel((PVOID
)Pcr
,
143 * Create a PCR for this processor
145 memset(Pcr
, 0, PAGE_SIZE
);
146 Pcr
->ProcessorNumber
= Id
;
147 Pcr
->Tib
.Self
= &Pcr
->Tib
;
149 Pcr
->Irql
= SYNCH_LEVEL
;
151 Pcr
->PrcbData
.MHz
= BootPcr
->PrcbData
.MHz
;
152 Pcr
->StallScaleFactor
= BootPcr
->StallScaleFactor
;
154 /* Mark the end of the exception handler list */
155 Pcr
->Tib
.ExceptionList
= (PVOID
)-1;
157 KiGdtPrepareForApplicationProcessorInit(Id
);
161 KeApplicationProcessorInit(VOID
)
166 DPRINT("KeApplicationProcessorInit()\n");
168 if (Ke386GlobalPagesEnabled
)
170 /* Enable global pages */
171 Ke386SetCr4(Ke386GetCr4() | X86_CR4_PGE
);
175 Offset
= InterlockedIncrementUL(&PcrsAllocated
) - 1;
176 Pcr
= (PKPCR
)((ULONG_PTR
)KPCR_BASE
+ Offset
* PAGE_SIZE
);
181 KiInitializeGdt(Pcr
);
183 /* Get processor information. */
186 /* Check FPU/MMX/SSE support. */
191 if (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SYSCALL
)
193 extern void KiFastCallEntry(void);
195 /* CS Selector of the target segment. */
196 Ke386Wrmsr(0x174, KERNEL_CS
, 0);
198 Ke386Wrmsr(0x175, 0, 0);
200 Ke386Wrmsr(0x176, (ULONG_PTR
)KiFastCallEntry
, 0);
204 * It is now safe to process interrupts
206 KeLowerIrql(DISPATCH_LEVEL
);
211 Ki386ApplicationProcessorInitializeTSS();
214 * Initialize a default LDT
216 Ki386InitializeLdt();
218 /* Now we can enable interrupts. */
219 Ke386EnableInterrupts();
223 KeInit1(PCHAR CommandLine
, PULONG LastKernelAddress
)
227 BOOLEAN NoExecute
= FALSE
;
229 extern USHORT KiBootGdt
[];
230 extern KTSS KiBootTss
;
233 * Initialize the initial PCR region. We can't allocate a page
234 * with MmAllocPage() here because MmInit1() has not yet been
235 * called, so we use a predefined page in low memory
238 KPCR
= (PKPCR
)KPCR_BASE
;
239 memset(KPCR
, 0, PAGE_SIZE
);
241 KPCR
->Irql
= SYNCH_LEVEL
;
242 KPCR
->Tib
.Self
= &KPCR
->Tib
;
243 KPCR
->GDT
= KiBootGdt
;
244 KPCR
->IDT
= (PUSHORT
)KiIdt
;
245 KPCR
->TSS
= &KiBootTss
;
246 KPCR
->ProcessorNumber
= 0;
250 KiInitializeGdt (NULL
);
251 Ki386BootInitializeTSS();
252 Ki386InitializeLdt();
254 /* Get processor information. */
257 /* Check FPU/MMX/SSE support. */
260 /* Mark the end of the exception handler list */
261 KPCR
->Tib
.ExceptionList
= (PVOID
)-1;
268 if (KPCR
->PrcbData
.FeatureBits
& X86_FEATURE_PGE
)
271 /* Enable global pages */
272 Ke386GlobalPagesEnabled
= TRUE
;
273 Ke386SaveFlags(Flags
);
274 Ke386DisableInterrupts();
275 Ke386SetCr4(Ke386GetCr4() | X86_CR4_PGE
);
276 Ke386RestoreFlags(Flags
);
279 /* Search for pae and noexecute */
280 p1
= (PCHAR
)KeLoaderBlock
.CommandLine
;
281 while(*p1
&& (p2
= strchr(p1
, '/')))
284 if (!_strnicmp(p2
, "PAE", 3))
286 if (p2
[3] == ' ' || p2
[3] == 0)
292 else if (!_strnicmp(p2
, "NOEXECUTE", 9))
294 if (p2
[9] == ' ' || p2
[9] == '=' || p2
[9] == 0)
305 * Make the detection of the noexecute feature more portable.
307 if(KPCR
->PrcbData
.CpuType
== 0xf &&
308 RtlCompareMemory("AuthenticAMD", KPCR
->PrcbData
.VendorString
, 12) == 12)
313 Ke386SaveFlags(Flags
);
314 Ke386DisableInterrupts();
316 Ke386Rdmsr(0xc0000080, l
, h
);
318 Ke386Wrmsr(0xc0000080, l
, h
);
319 Ke386NoExecute
= TRUE
;
320 Ke386RestoreFlags(Flags
);
329 Ke386Pae
= Ke386GetCr4() & X86_CR4_PAE
? TRUE
: FALSE
;
331 /* Enable PAE mode */
332 if ((Pae
&& (KPCR
->PrcbData
.FeatureBits
& X86_FEATURE_PAE
)) || NoExecute
)
334 MiEnablePAE((PVOID
*)LastKernelAddress
);
335 Ke386PaeEnabled
= TRUE
;
338 if (KPCR
->PrcbData
.FeatureBits
& X86_FEATURE_SYSCALL
)
340 extern void KiFastCallEntry(void);
342 /* CS Selector of the target segment. */
343 Ke386Wrmsr(0x174, KERNEL_CS
, 0);
345 Ke386Wrmsr(0x175, 0, 0);
347 Ke386Wrmsr(0x176, (ULONG_PTR
)KiFastCallEntry
, 0);
354 PKPCR Pcr
= KeGetCurrentKPCR();
356 KeInitializeBugCheck();
357 KeInitializeDispatcher();
358 KiInitializeSystemClock();
360 if (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_PAE
)
362 DPRINT("CPU supports PAE mode\n");
365 DPRINT("CPU runs in PAE mode\n");
368 DPRINT("NoExecute is enabled\n");
373 DPRINT("CPU doesn't run in PAE mode\n");
376 if ((Pcr
->PrcbData
.FeatureBits
& (X86_FEATURE_FXSR
| X86_FEATURE_MMX
| X86_FEATURE_SSE
| X86_FEATURE_SSE2
)) ||
377 (Ke386CpuidFlags2
& X86_EXT_FEATURE_SSE3
))
379 DPRINT("CPU supports" "%s%s%s%s%s" ".\n",
380 ((Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_FXSR
) ? " FXSR" : ""),
381 ((Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_MMX
) ? " MMX" : ""),
382 ((Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SSE
) ? " SSE" : ""),
383 ((Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SSE2
) ? " SSE2" : ""),
384 ((Ke386CpuidFlags2
& X86_EXT_FEATURE_SSE3
) ? " SSE3" : ""));
386 if (Ke386GetCr4() & X86_CR4_OSFXSR
)
388 DPRINT("SSE enabled.\n");
390 if (Ke386GetCr4() & X86_CR4_OSXMMEXCPT
)
392 DPRINT("Unmasked SIMD exceptions enabled.\n");
394 if (Pcr
->PrcbData
.VendorString
[0])
396 DPRINT("CPU Vendor: %s\n", Pcr
->PrcbData
.VendorString
);
398 if (Ke386CpuidModel
[0])
400 DPRINT("CPU Model: %s\n", Ke386CpuidModel
);
403 DPRINT("Ke386CacheAlignment: %d\n", Ke386CacheAlignment
);
404 if (Ke386L1CacheSize
)
406 DPRINT("Ke386L1CacheSize: %dkB\n", Ke386L1CacheSize
);
408 if (Pcr
->L2CacheSize
)
410 DPRINT("Ke386L2CacheSize: %dkB\n", Pcr
->L2CacheSize
);
415 Ki386SetProcessorFeatures(VOID
)
417 PKPCR Pcr
= KeGetCurrentKPCR();
418 OBJECT_ATTRIBUTES ObjectAttributes
;
419 UNICODE_STRING KeyName
;
420 UNICODE_STRING ValueName
;
423 KEY_VALUE_PARTIAL_INFORMATION ValueData
;
425 ULONG FastSystemCallDisable
= 0;
427 SharedUserData
->ProcessorFeatures
[PF_FLOATING_POINT_PRECISION_ERRATA
] = FALSE
;
428 SharedUserData
->ProcessorFeatures
[PF_FLOATING_POINT_EMULATED
] = FALSE
;
429 SharedUserData
->ProcessorFeatures
[PF_COMPARE_EXCHANGE_DOUBLE
] =
430 (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_CX8
);
431 SharedUserData
->ProcessorFeatures
[PF_MMX_INSTRUCTIONS_AVAILABLE
] =
432 (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_MMX
);
433 SharedUserData
->ProcessorFeatures
[PF_PPC_MOVEMEM_64BIT_OK
] = FALSE
;
434 SharedUserData
->ProcessorFeatures
[PF_ALPHA_BYTE_INSTRUCTIONS
] = FALSE
;
435 SharedUserData
->ProcessorFeatures
[PF_XMMI_INSTRUCTIONS_AVAILABLE
] =
436 (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SSE
);
437 SharedUserData
->ProcessorFeatures
[PF_3DNOW_INSTRUCTIONS_AVAILABLE
] =
438 (Ke386CpuidExFlags
& X86_EXT_FEATURE_3DNOW
);
439 SharedUserData
->ProcessorFeatures
[PF_RDTSC_INSTRUCTION_AVAILABLE
] =
440 (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_TSC
);
441 SharedUserData
->ProcessorFeatures
[PF_PAE_ENABLED
] = Ke386Pae
;
442 SharedUserData
->ProcessorFeatures
[PF_XMMI64_INSTRUCTIONS_AVAILABLE
] =
443 (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SSE2
);
445 /* Does the CPU Support Fast System Call? */
446 if (Pcr
->PrcbData
.FeatureBits
& X86_FEATURE_SYSCALL
) {
448 /* FIXME: Check for Family == 6, Model < 3 and Stepping < 3 and disable */
450 /* Make sure it's not disabled in registry */
451 RtlRosInitUnicodeStringFromLiteral(&KeyName
,
452 L
"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Session Manager\\Kernel");
453 RtlRosInitUnicodeStringFromLiteral(&ValueName
,
454 L
"FastSystemCallDisable");
455 InitializeObjectAttributes(&ObjectAttributes
,
457 OBJ_CASE_INSENSITIVE
,
460 Status
= NtOpenKey(&KeyHandle
, KEY_ALL_ACCESS
, &ObjectAttributes
);
462 if (NT_SUCCESS(Status
)) {
464 /* Read the Value then Close the Key */
465 Status
= NtQueryValueKey(KeyHandle
,
467 KeyValuePartialInformation
,
471 RtlMoveMemory(&FastSystemCallDisable
, ValueData
.Data
, sizeof(ULONG
));
478 /* Disable SYSENTER/SYSEXIT, because the CPU doesn't support it */
479 FastSystemCallDisable
= 1;
483 if (FastSystemCallDisable
) {
486 SharedUserData
->SystemCall
[0] = 0x8D;
487 SharedUserData
->SystemCall
[1] = 0x54;
488 SharedUserData
->SystemCall
[2] = 0x24;
489 SharedUserData
->SystemCall
[3] = 0x08;
490 SharedUserData
->SystemCall
[4] = 0xCD;
491 SharedUserData
->SystemCall
[5] = 0x2E;
492 SharedUserData
->SystemCall
[6] = 0xC3;
497 SharedUserData
->SystemCall
[0] = 0x8B;
498 SharedUserData
->SystemCall
[1] = 0xD4;
499 SharedUserData
->SystemCall
[2] = 0x0F;
500 SharedUserData
->SystemCall
[3] = 0x34;
501 SharedUserData
->SystemCall
[4] = 0xC3;
503 /* Enable SYSENTER/SYSEXIT */
504 KiFastSystemCallDisable
= 0;