2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ke/amd64/kiinit.c
5 * PURPOSE: Kernel Initialization for x86 CPUs
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Timo Kreuzer (timo.kreuzer@reactos.org)
10 /* INCLUDES *****************************************************************/
16 #define REQUIRED_FEATURE_BITS (KF_RDTSC|KF_CR4|KF_CMPXCHG8B|KF_XMMI|KF_XMMI64| \
17 KF_LARGE_PAGE|KF_FAST_SYSCALL|KF_GLOBAL_PAGE| \
18 KF_CMOV|KF_PAT|KF_MMX|KF_FXSR|KF_NX_BIT|KF_MTRR)
20 /* GLOBALS *******************************************************************/
22 /* Function pointer for early debug prints */
23 ULONG (*FrLdrDbgPrint
)(const char *Format
, ...);
25 /* Spinlocks used only on X86 */
26 KSPIN_LOCK KiFreezeExecutionLock
;
31 /* Boot and double-fault/NMI/DPC stack */
32 UCHAR
DECLSPEC_ALIGN(16) P0BootStackData
[KERNEL_STACK_SIZE
] = {0};
33 UCHAR
DECLSPEC_ALIGN(16) KiDoubleFaultStackData
[KERNEL_STACK_SIZE
] = {0};
34 ULONG_PTR P0BootStack
= (ULONG_PTR
)&P0BootStackData
[KERNEL_STACK_SIZE
];
35 ULONG_PTR KiDoubleFaultStack
= (ULONG_PTR
)&KiDoubleFaultStackData
[KERNEL_STACK_SIZE
];
37 void KiInitializeSegments();
38 void KiSystemCallEntry64();
39 void KiSystemCallEntry32();
41 /* FUNCTIONS *****************************************************************/
46 KiInitMachineDependent(VOID
)
48 /* Check for large page support */
49 if (KeFeatureBits
& KF_LARGE_PAGE
)
51 /* FIXME: Support this */
52 DPRINT("Large Page support detected but not yet taken advantage of!\n");
55 /* Check for global page support */
56 if (KeFeatureBits
& KF_GLOBAL_PAGE
)
58 /* FIXME: Support this */
59 DPRINT("Global Page support detected but not yet taken advantage of!\n");
62 /* Check if we have MTRR */
63 if (KeFeatureBits
& KF_MTRR
)
65 /* FIXME: Support this */
66 DPRINT("MTRR support detected but not yet taken advantage of!\n");
69 /* Check for PAT and/or MTRR support */
70 if (KeFeatureBits
& KF_PAT
)
72 /* FIXME: Support this */
73 DPRINT("PAT support detected but not yet taken advantage of!\n");
76 // /* Allocate the IOPM save area */
77 // Ki386IopmSaveArea = ExAllocatePoolWithTag(PagedPool,
80 // if (!Ki386IopmSaveArea)
82 // /* Bugcheck. We need this for V86/VDM support. */
83 // KeBugCheckEx(NO_PAGES_AVAILABLE, 2, IOPM_SIZE, 0, 0);
90 KiInitializePcr(IN PKIPCR Pcr
,
91 IN ULONG ProcessorNumber
,
92 IN PKTHREAD IdleThread
,
95 KDESCRIPTOR GdtDescriptor
= {{0},0,0}, IdtDescriptor
= {{0},0,0};
96 PKGDTENTRY64 TssEntry
;
99 /* Zero out the PCR */
100 RtlZeroMemory(Pcr
, sizeof(KIPCR
));
102 /* Set pointers to ourselves */
103 Pcr
->Self
= (PKPCR
)Pcr
;
104 Pcr
->CurrentPrcb
= &Pcr
->Prcb
;
106 /* Set the PCR Version */
107 Pcr
->MajorVersion
= PCR_MAJOR_VERSION
;
108 Pcr
->MinorVersion
= PCR_MINOR_VERSION
;
110 /* Set the PRCB Version */
111 Pcr
->Prcb
.MajorVersion
= 1;
112 Pcr
->Prcb
.MinorVersion
= 1;
114 /* Set the Build Type */
115 Pcr
->Prcb
.BuildType
= 0;
117 Pcr
->Prcb
.BuildType
|= PRCB_BUILD_UNIPROCESSOR
;
120 Pcr
->Prcb
.BuildType
|= PRCB_BUILD_DEBUG
;
123 /* Set the Processor Number and current Processor Mask */
124 Pcr
->Prcb
.Number
= (UCHAR
)ProcessorNumber
;
125 Pcr
->Prcb
.SetMember
= 1ULL << ProcessorNumber
;
127 /* Get GDT and IDT descriptors */
128 __sgdt(&GdtDescriptor
.Limit
);
129 __sidt(&IdtDescriptor
.Limit
);
130 Pcr
->GdtBase
= (PVOID
)GdtDescriptor
.Base
;
131 Pcr
->IdtBase
= (PKIDTENTRY
)IdtDescriptor
.Base
;
133 /* Get TSS Selector */
135 ASSERT(Tr
== KGDT64_SYS_TSS
);
138 TssEntry
= KiGetGdtEntry(Pcr
->GdtBase
, Tr
);
140 /* Get the KTSS itself */
141 Pcr
->TssBase
= KiGetGdtDescriptorBase(TssEntry
);
143 Pcr
->Prcb
.RspBase
= Pcr
->TssBase
->Rsp0
; // FIXME
146 Pcr
->Prcb
.DpcStack
= DpcStack
;
148 /* Setup the processor set */
149 Pcr
->Prcb
.MultiThreadProcessorSet
= Pcr
->Prcb
.SetMember
;
151 /* Clear DR6/7 to cleanup bootloader debugging */
152 Pcr
->Prcb
.ProcessorState
.SpecialRegisters
.KernelDr6
= 0;
153 Pcr
->Prcb
.ProcessorState
.SpecialRegisters
.KernelDr7
= 0;
155 /* Set the Current Thread */
156 Pcr
->Prcb
.CurrentThread
= IdleThread
;
158 /* Start us out at PASSIVE_LEVEL */
159 Pcr
->Irql
= PASSIVE_LEVEL
;
160 KeSetCurrentIrql(PASSIVE_LEVEL
);
166 KiInitializeCpu(PKIPCR Pcr
)
172 KiInitializeSegments();
175 __writemsr(MSR_GS_BASE
, (ULONG64
)Pcr
);
176 __writemsr(MSR_GS_SWAP
, (ULONG64
)Pcr
);
178 /* Detect and set the CPU Type */
179 KiSetProcessorType();
181 /* Get the processor features for this CPU */
182 FeatureBits
= KiGetFeatureBits();
184 /* Check if we support all needed features */
185 if ((FeatureBits
& REQUIRED_FEATURE_BITS
) != REQUIRED_FEATURE_BITS
)
187 /* If not, bugcheck system */
188 FrLdrDbgPrint("CPU doesn't have needed features! Has: 0x%x, required: 0x%x\n",
189 FeatureBits
, REQUIRED_FEATURE_BITS
);
193 /* Set DEP to always on */
194 SharedUserData
->NXSupportPolicy
= NX_SUPPORT_POLICY_ALWAYSON
;
195 FeatureBits
|= KF_NX_ENABLED
;
197 /* Save feature bits */
198 Pcr
->Prcb
.FeatureBits
= FeatureBits
;
200 /* Enable fx save restore support */
201 __writecr4(__readcr4() | CR4_FXSR
);
203 /* Enable XMMI exceptions */
204 __writecr4(__readcr4() | CR4_XMMEXCPT
);
206 /* Enable Write-Protection */
207 __writecr0(__readcr0() | CR0_WP
);
209 /* Disable fpu monitoring */
210 __writecr0(__readcr0() & ~CR0_MP
);
212 /* Disable x87 fpu exceptions */
213 __writecr0(__readcr0() & ~CR0_NE
);
218 /* Set the systemcall entry points */
219 __writemsr(MSR_LSTAR
, (ULONG64
)KiSystemCallEntry64
);
220 __writemsr(MSR_CSTAR
, (ULONG64
)KiSystemCallEntry32
);
222 __writemsr(MSR_STAR
, ((ULONG64
)KGDT64_R0_CODE
<< 32) |
223 ((ULONG64
)(KGDT64_R3_CMCODE
|RPL_MASK
) << 48));
225 /* Set the flags to be cleared when doing a syscall */
226 __writemsr(MSR_SYSCALL_MASK
, EFLAGS_IF_MASK
| EFLAGS_TF
| EFLAGS_DF
);
228 /* Enable syscall instruction and no-execute support */
229 __writemsr(MSR_EFER
, __readmsr(MSR_EFER
) | MSR_SCE
| MSR_NXE
);
231 /* Initialize the PAT */
232 Pat
= (PAT_WB
<< 0) | (PAT_WC
<< 8) | (PAT_UCM
<< 16) | (PAT_UC
<< 24) |
233 (PAT_WB
<< 32) | (PAT_WC
<< 40) | (PAT_UCM
<< 48) | (PAT_UC
<< 56);
234 __writemsr(MSR_PAT
, Pat
);
239 KiInitializeTss(IN PKTSS64 Tss
,
242 PKGDTENTRY64 TssEntry
;
244 /* Get pointer to the GDT entry */
245 TssEntry
= KiGetGdtEntry(KeGetPcr()->GdtBase
, KGDT64_SYS_TSS
);
247 /* Initialize the GDT entry */
248 KiInitGdtEntry(TssEntry
, (ULONG64
)Tss
, sizeof(KTSS64
), AMD64_TSS
, 0);
250 /* Zero out the TSS */
251 RtlZeroMemory(Tss
, sizeof(KTSS64
));
253 /* FIXME: I/O Map? */
254 Tss
->IoMapBase
= 0x68;
256 /* Setup ring 0 stack pointer */
259 /* Setup a stack for Double Fault Traps */
260 Tss
->Ist
[1] = (ULONG64
)KiDoubleFaultStack
;
262 /* Setup a stack for CheckAbort Traps */
263 Tss
->Ist
[2] = (ULONG64
)KiDoubleFaultStack
;
265 /* Setup a stack for NMI Traps */
266 Tss
->Ist
[3] = (ULONG64
)KiDoubleFaultStack
;
268 /* Load the task register */
269 __ltr(KGDT64_SYS_TSS
);
275 KiInitializeKernelMachineDependent(
277 IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
279 /* Set boot-level flags */
280 KeI386CpuType
= Prcb
->CpuType
;
281 KeI386CpuStep
= Prcb
->CpuStep
;
282 KeProcessorArchitecture
= PROCESSOR_ARCHITECTURE_AMD64
;
283 KeProcessorLevel
= (USHORT
)Prcb
->CpuType
;
285 KeProcessorRevision
= Prcb
->CpuStep
;
287 /* Set basic CPU Features that user mode can read */
288 SharedUserData
->ProcessorFeatures
[PF_COMPARE_EXCHANGE_DOUBLE
] = TRUE
;
289 SharedUserData
->ProcessorFeatures
[PF_RDTSC_INSTRUCTION_AVAILABLE
] = TRUE
;
290 SharedUserData
->ProcessorFeatures
[PF_PPC_MOVEMEM_64BIT_OK
] = TRUE
;
291 SharedUserData
->ProcessorFeatures
[PF_PAE_ENABLED
] = TRUE
; // ???
292 SharedUserData
->ProcessorFeatures
[PF_NX_ENABLED
] = TRUE
;
293 SharedUserData
->ProcessorFeatures
[PF_FASTFAIL_AVAILABLE
] = TRUE
;
294 SharedUserData
->ProcessorFeatures
[PF_XSAVE_ENABLED
] = TRUE
;
295 SharedUserData
->ProcessorFeatures
[PF_MMX_INSTRUCTIONS_AVAILABLE
] =
296 (Prcb
->FeatureBits
& KF_MMX
) ? TRUE
: FALSE
;
297 SharedUserData
->ProcessorFeatures
[PF_XMMI_INSTRUCTIONS_AVAILABLE
] =
298 ((Prcb
->FeatureBits
& KF_FXSR
) && (Prcb
->FeatureBits
& KF_XMMI
)) ? TRUE
: FALSE
;
299 SharedUserData
->ProcessorFeatures
[PF_XMMI64_INSTRUCTIONS_AVAILABLE
] =
300 ((Prcb
->FeatureBits
& KF_FXSR
) && (Prcb
->FeatureBits
& KF_XMMI64
)) ? TRUE
: FALSE
;
301 SharedUserData
->ProcessorFeatures
[PF_3DNOW_INSTRUCTIONS_AVAILABLE
] =
302 (Prcb
->FeatureBits
& KF_3DNOW
) ? TRUE
: FALSE
;
303 SharedUserData
->ProcessorFeatures
[PF_SSE3_INSTRUCTIONS_AVAILABLE
] =
304 (Prcb
->FeatureBits
& KF_SSE3
) ? TRUE
: FALSE
;
305 SharedUserData
->ProcessorFeatures
[PF_COMPARE_EXCHANGE128
] =
306 (Prcb
->FeatureBits
& KF_CMPXCHG16B
) ? TRUE
: FALSE
;
308 /* Set the default NX policy (opt-in) */
309 SharedUserData
->NXSupportPolicy
= NX_SUPPORT_POLICY_OPTIN
;
311 /* Check if NPX is always on */
312 if (strstr(KeLoaderBlock
->LoadOptions
, "NOEXECUTE=ALWAYSON"))
314 /* Set it always on */
315 SharedUserData
->NXSupportPolicy
= NX_SUPPORT_POLICY_ALWAYSON
;
316 Prcb
->FeatureBits
|= KF_NX_ENABLED
;
318 else if (strstr(KeLoaderBlock
->LoadOptions
, "NOEXECUTE=OPTOUT"))
320 /* Set it in opt-out mode */
321 SharedUserData
->NXSupportPolicy
= NX_SUPPORT_POLICY_OPTOUT
;
322 Prcb
->FeatureBits
|= KF_NX_ENABLED
;
324 else if ((strstr(KeLoaderBlock
->LoadOptions
, "NOEXECUTE=OPTIN")) ||
325 (strstr(KeLoaderBlock
->LoadOptions
, "NOEXECUTE")))
327 /* Set the feature bits */
328 Prcb
->FeatureBits
|= KF_NX_ENABLED
;
330 else if ((strstr(KeLoaderBlock
->LoadOptions
, "NOEXECUTE=ALWAYSOFF")) ||
331 (strstr(KeLoaderBlock
->LoadOptions
, "EXECUTE")))
333 /* Set disabled mode */
334 SharedUserData
->NXSupportPolicy
= NX_SUPPORT_POLICY_ALWAYSOFF
;
335 Prcb
->FeatureBits
|= KF_NX_DISABLED
;
339 static LDR_DATA_TABLE_ENTRY LdrCoreEntries
[3];
342 KiInitModuleList(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
344 PLDR_DATA_TABLE_ENTRY LdrEntry
;
348 /* Initialize the list head */
349 InitializeListHead(&PsLoadedModuleList
);
351 /* Loop the first 3 entries */
352 for (Entry
= LoaderBlock
->LoadOrderListHead
.Flink
, i
= 0;
353 Entry
!= &LoaderBlock
->LoadOrderListHead
&& i
< 3;
354 Entry
= Entry
->Flink
, i
++)
356 /* Get the data table entry */
357 LdrEntry
= CONTAINING_RECORD(Entry
,
358 LDR_DATA_TABLE_ENTRY
,
362 LdrCoreEntries
[i
] = *LdrEntry
;
364 /* Insert the copy into the list */
365 InsertTailList(&PsLoadedModuleList
, &LdrCoreEntries
[i
].InLoadOrderLinks
);
372 KiSystemStartup(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
375 PKTHREAD InitialThread
;
376 ULONG64 InitialStack
;
380 FrLdrDbgPrint
= LoaderBlock
->u
.I386
.CommonDataArea
;
381 //FrLdrDbgPrint("Hello from KiSystemStartup!!!\n");
383 /* Save the loader block */
384 KeLoaderBlock
= LoaderBlock
;
386 /* Get the current CPU number */
387 Cpu
= KeNumberProcessors
++; // FIXME
389 /* LoaderBlock initialization for Cpu 0 */
392 /* Set the initial stack, idle thread and process */
393 LoaderBlock
->KernelStack
= (ULONG_PTR
)P0BootStack
;
394 LoaderBlock
->Thread
= (ULONG_PTR
)&KiInitialThread
;
395 LoaderBlock
->Process
= (ULONG_PTR
)&KiInitialProcess
.Pcb
;
396 LoaderBlock
->Prcb
= (ULONG_PTR
)&KiInitialPcr
.Prcb
;
399 /* Get Pcr from loader block */
400 Pcr
= CONTAINING_RECORD(LoaderBlock
->Prcb
, KIPCR
, Prcb
);
402 /* Set the PRCB for this Processor */
403 KiProcessorBlock
[Cpu
] = &Pcr
->Prcb
;
405 /* Align stack to 16 bytes */
406 LoaderBlock
->KernelStack
&= ~(16 - 1);
408 /* Save the initial thread and stack */
409 InitialStack
= LoaderBlock
->KernelStack
; // Checkme
410 InitialThread
= (PKTHREAD
)LoaderBlock
->Thread
;
412 /* Set us as the current process */
413 InitialThread
->ApcState
.Process
= (PVOID
)LoaderBlock
->Process
;
415 /* Initialize the PCR */
416 KiInitializePcr(Pcr
, Cpu
, InitialThread
, (PVOID
)KiDoubleFaultStack
);
418 /* Initialize the CPU features */
419 KiInitializeCpu(Pcr
);
421 /* Initial setup for the boot CPU */
424 /* Initialize the module list (ntos, hal, kdcom) */
425 KiInitModuleList(LoaderBlock
);
427 /* Setup the TSS descriptors and entries */
428 KiInitializeTss(Pcr
->TssBase
, InitialStack
);
433 /* Initialize debugging system */
434 KdInitSystem(0, KeLoaderBlock
);
436 /* Check for break-in */
437 if (KdPollBreakIn()) DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
440 DPRINT1("Pcr = %p, Gdt = %p, Idt = %p, Tss = %p\n",
441 Pcr
, Pcr
->GdtBase
, Pcr
->IdtBase
, Pcr
->TssBase
);
444 while (InterlockedBitTestAndSet64((PLONG64
)&KiFreezeExecutionLock
, 0))
446 /* Loop until lock is free */
447 while ((*(volatile KSPIN_LOCK
*)&KiFreezeExecutionLock
) & 1);
450 /* Initialize the Processor with HAL */
451 HalInitializeProcessor(Cpu
, KeLoaderBlock
);
453 /* Set processor as active */
454 KeActiveProcessors
|= 1ULL << Cpu
;
457 InterlockedAnd64((PLONG64
)&KiFreezeExecutionLock
, 0);
459 /* Raise to HIGH_LEVEL */
460 KfRaiseIrql(HIGH_LEVEL
);
462 /* Machine specific kernel initialization */
463 if (Cpu
== 0) KiInitializeKernelMachineDependent(&Pcr
->Prcb
, LoaderBlock
);
465 /* Switch to new kernel stack and start kernel bootstrapping */
466 KiSwitchToBootStack(InitialStack
& ~3);