2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ke/i386/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| \
19 /* GLOBALS *******************************************************************/
21 /* Function pointer for early debug prints */
22 ULONG (*FrLdrDbgPrint
)(const char *Format
, ...);
24 /* Spinlocks used only on X86 */
25 KSPIN_LOCK KiFreezeExecutionLock
;
30 /* Boot and double-fault/NMI/DPC stack */
31 UCHAR
DECLSPEC_ALIGN(16) P0BootStackData
[KERNEL_STACK_SIZE
] = {0};
32 UCHAR
DECLSPEC_ALIGN(16) KiDoubleFaultStackData
[KERNEL_STACK_SIZE
] = {0};
33 ULONG_PTR P0BootStack
= (ULONG_PTR
)&P0BootStackData
[KERNEL_STACK_SIZE
];
34 ULONG_PTR KiDoubleFaultStack
= (ULONG_PTR
)&KiDoubleFaultStackData
[KERNEL_STACK_SIZE
];
36 void KiInitializeSegments();
37 void KiSystemCallEntry64();
38 void KiSystemCallEntry32();
40 /* FUNCTIONS *****************************************************************/
44 KiInitMachineDependent(VOID
)
46 /* Check for large page support */
47 if (KeFeatureBits
& KF_LARGE_PAGE
)
49 /* FIXME: Support this */
50 DPRINT("Large Page support detected but not yet taken advantage of!\n");
53 /* Check for global page support */
54 if (KeFeatureBits
& KF_GLOBAL_PAGE
)
56 /* FIXME: Support this */
57 DPRINT("Global Page support detected but not yet taken advantage of!\n");
60 /* Check if we have MTRR */
61 if (KeFeatureBits
& KF_MTRR
)
63 /* FIXME: Support this */
64 DPRINT("MTRR support detected but not yet taken advantage of!\n");
67 /* Check for PAT and/or MTRR support */
68 if (KeFeatureBits
& KF_PAT
)
70 /* FIXME: Support this */
71 DPRINT("PAT support detected but not yet taken advantage of!\n");
74 /* Allocate the IOPM save area. */
75 // Ki386IopmSaveArea = ExAllocatePoolWithTag(PagedPool,
77 // TAG('K', 'e', ' ', ' '));
78 // if (!Ki386IopmSaveArea)
80 // /* Bugcheck. We need this for V86/VDM support. */
81 // KeBugCheckEx(NO_PAGES_AVAILABLE, 2, PAGE_SIZE * 2, 0, 0);
88 KiInitializePcr(IN PKIPCR Pcr
,
89 IN ULONG ProcessorNumber
,
90 IN PKTHREAD IdleThread
,
93 KDESCRIPTOR GdtDescriptor
= {{0},0,0}, IdtDescriptor
= {{0},0,0};
94 PKGDTENTRY64 TssEntry
;
97 /* Zero out the PCR */
98 RtlZeroMemory(Pcr
, sizeof(KIPCR
));
100 /* Set pointers to ourselves */
101 Pcr
->Self
= (PKPCR
)Pcr
;
102 Pcr
->CurrentPrcb
= &Pcr
->Prcb
;
104 /* Set the PCR Version */
105 Pcr
->MajorVersion
= PCR_MAJOR_VERSION
;
106 Pcr
->MinorVersion
= PCR_MINOR_VERSION
;
108 /* Set the PRCB Version */
109 Pcr
->Prcb
.MajorVersion
= 1;
110 Pcr
->Prcb
.MinorVersion
= 1;
112 /* Set the Build Type */
113 Pcr
->Prcb
.BuildType
= 0;
115 Pcr
->Prcb
.BuildType
|= PRCB_BUILD_UNIPROCESSOR
;
118 Pcr
->Prcb
.BuildType
|= PRCB_BUILD_DEBUG
;
121 /* Set the Processor Number and current Processor Mask */
122 Pcr
->Prcb
.Number
= (UCHAR
)ProcessorNumber
;
123 Pcr
->Prcb
.SetMember
= 1ULL << ProcessorNumber
;
125 /* Get GDT and IDT descriptors */
126 __sgdt(&GdtDescriptor
.Limit
);
127 __sidt(&IdtDescriptor
.Limit
);
128 Pcr
->GdtBase
= (PVOID
)GdtDescriptor
.Base
;
129 Pcr
->IdtBase
= (PKIDTENTRY
)IdtDescriptor
.Base
;
131 /* Get TSS Selector */
133 ASSERT(Tr
== KGDT64_SYS_TSS
);
136 TssEntry
= KiGetGdtEntry(Pcr
->GdtBase
, Tr
);
138 /* Get the KTSS itself */
139 Pcr
->TssBase
= KiGetGdtDescriptorBase(TssEntry
);
141 Pcr
->Prcb
.RspBase
= Pcr
->TssBase
->Rsp0
; // FIXME
144 Pcr
->Prcb
.DpcStack
= DpcStack
;
146 /* Setup the processor set */
147 Pcr
->Prcb
.MultiThreadProcessorSet
= Pcr
->Prcb
.SetMember
;
149 /* Clear DR6/7 to cleanup bootloader debugging */
150 Pcr
->Prcb
.ProcessorState
.SpecialRegisters
.KernelDr6
= 0;
151 Pcr
->Prcb
.ProcessorState
.SpecialRegisters
.KernelDr7
= 0;
153 /* Set the Current Thread */
154 Pcr
->Prcb
.CurrentThread
= IdleThread
;
156 /* Start us out at PASSIVE_LEVEL */
157 Pcr
->Irql
= PASSIVE_LEVEL
;
158 KeSetCurrentIrql(PASSIVE_LEVEL
);
163 KiInitializeCpu(PKIPCR Pcr
)
168 KiInitializeSegments();
171 __writemsr(MSR_GS_BASE
, (ULONG64
)Pcr
);
172 __writemsr(MSR_GS_SWAP
, (ULONG64
)Pcr
);
174 /* Detect and set the CPU Type */
175 KiSetProcessorType();
177 /* Get the processor features for this CPU */
178 FeatureBits
= KiGetFeatureBits();
180 /* Check if we support all needed features */
181 if ((FeatureBits
& REQUIRED_FEATURE_BITS
) != REQUIRED_FEATURE_BITS
)
183 /* If not, bugcheck system */
184 FrLdrDbgPrint("CPU doesn't have needed features! Has: 0x%x, required: 0x%x\n",
185 FeatureBits
, REQUIRED_FEATURE_BITS
);
189 /* Set DEP to always on */
190 SharedUserData
->NXSupportPolicy
= NX_SUPPORT_POLICY_ALWAYSON
;
191 FeatureBits
|= KF_NX_ENABLED
;
193 /* Save feature bits */
194 Pcr
->Prcb
.FeatureBits
= FeatureBits
;
196 /* Enable fx save restore support */
197 __writecr4(__readcr4() | CR4_FXSR
);
199 /* Enable XMMI exceptions */
200 __writecr4(__readcr4() | CR4_XMMEXCPT
);
202 /* Enable Write-Protection */
203 __writecr0(__readcr0() | CR0_WP
);
205 /* Disable fpu monitoring */
206 __writecr0(__readcr0() & ~CR0_MP
);
208 /* Disable x87 fpu exceptions */
209 __writecr0(__readcr0() & ~CR0_NE
);
214 /* Set the systemcall entry points */
215 __writemsr(MSR_LSTAR
, (ULONG64
)KiSystemCallEntry64
);
216 __writemsr(MSR_CSTAR
, (ULONG64
)KiSystemCallEntry32
);
218 __writemsr(MSR_STAR
, ((ULONG64
)KGDT64_R0_CODE
<< 32) |
219 ((ULONG64
)(KGDT64_R3_CMCODE
|RPL_MASK
) << 48));
221 /* Set the flags to be cleared when doing a syscall */
222 __writemsr(MSR_SYSCALL_MASK
, EFLAGS_IF_MASK
| EFLAGS_TF
| EFLAGS_DF
);
224 /* Enable syscall instruction */
225 __writemsr(MSR_EFER
, __readmsr(MSR_EFER
) | MSR_SCE
);
230 KiInitializeTss(IN PKTSS64 Tss
,
233 PKGDTENTRY64 TssEntry
;
235 /* Get pointer to the GDT entry */
236 TssEntry
= KiGetGdtEntry(KeGetPcr()->GdtBase
, KGDT64_SYS_TSS
);
238 /* Initialize the GDT entry */
239 KiInitGdtEntry(TssEntry
, (ULONG64
)Tss
, sizeof(KTSS64
), AMD64_TSS
, 0);
241 /* Zero out the TSS */
242 RtlZeroMemory(Tss
, sizeof(KTSS64
));
244 /* FIXME: I/O Map? */
245 Tss
->IoMapBase
= 0x68;
247 /* Setup ring 0 stack pointer */
250 /* Setup a stack for Double Fault Traps */
251 Tss
->Ist
[1] = (ULONG64
)KiDoubleFaultStack
;
253 /* Setup a stack for CheckAbort Traps */
254 Tss
->Ist
[2] = (ULONG64
)KiDoubleFaultStack
;
256 /* Setup a stack for NMI Traps */
257 Tss
->Ist
[3] = (ULONG64
)KiDoubleFaultStack
;
259 /* Load the task register */
260 __ltr(KGDT64_SYS_TSS
);
266 KiInitializeKernelMachineDependent(
268 IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
270 /* Set boot-level flags */
271 KeI386CpuType
= Prcb
->CpuType
;
272 KeI386CpuStep
= Prcb
->CpuStep
;
273 KeProcessorArchitecture
= PROCESSOR_ARCHITECTURE_AMD64
;
274 KeProcessorLevel
= (USHORT
)Prcb
->CpuType
;
275 if (Prcb
->CpuID
) KeProcessorRevision
= Prcb
->CpuStep
;
277 /* Set basic CPU Features that user mode can read */
278 SharedUserData
->ProcessorFeatures
[PF_MMX_INSTRUCTIONS_AVAILABLE
] =
279 (Prcb
->FeatureBits
& KF_MMX
) ? TRUE
: FALSE
;
280 SharedUserData
->ProcessorFeatures
[PF_COMPARE_EXCHANGE_DOUBLE
] = TRUE
;
281 SharedUserData
->ProcessorFeatures
[PF_XMMI_INSTRUCTIONS_AVAILABLE
] =
282 ((Prcb
->FeatureBits
& KF_FXSR
) && (Prcb
->FeatureBits
& KF_XMMI
)) ? TRUE
: FALSE
;
283 SharedUserData
->ProcessorFeatures
[PF_XMMI64_INSTRUCTIONS_AVAILABLE
] =
284 ((Prcb
->FeatureBits
& KF_FXSR
) && (Prcb
->FeatureBits
& KF_XMMI64
)) ? TRUE
: FALSE
;
285 SharedUserData
->ProcessorFeatures
[PF_3DNOW_INSTRUCTIONS_AVAILABLE
] =
286 (Prcb
->FeatureBits
& KF_3DNOW
) ? TRUE
: FALSE
;
287 SharedUserData
->ProcessorFeatures
[PF_RDTSC_INSTRUCTION_AVAILABLE
] = TRUE
;
289 /* Set the default NX policy (opt-in) */
290 SharedUserData
->NXSupportPolicy
= NX_SUPPORT_POLICY_OPTIN
;
292 /* Check if NPX is always on */
293 if (strstr(KeLoaderBlock
->LoadOptions
, "NOEXECUTE=ALWAYSON"))
295 /* Set it always on */
296 SharedUserData
->NXSupportPolicy
= NX_SUPPORT_POLICY_ALWAYSON
;
297 Prcb
->FeatureBits
|= KF_NX_ENABLED
;
299 else if (strstr(KeLoaderBlock
->LoadOptions
, "NOEXECUTE=OPTOUT"))
301 /* Set it in opt-out mode */
302 SharedUserData
->NXSupportPolicy
= NX_SUPPORT_POLICY_OPTOUT
;
303 Prcb
->FeatureBits
|= KF_NX_ENABLED
;
305 else if ((strstr(KeLoaderBlock
->LoadOptions
, "NOEXECUTE=OPTIN")) ||
306 (strstr(KeLoaderBlock
->LoadOptions
, "NOEXECUTE")))
308 /* Set the feature bits */
309 Prcb
->FeatureBits
|= KF_NX_ENABLED
;
311 else if ((strstr(KeLoaderBlock
->LoadOptions
, "NOEXECUTE=ALWAYSOFF")) ||
312 (strstr(KeLoaderBlock
->LoadOptions
, "EXECUTE")))
314 /* Set disabled mode */
315 SharedUserData
->NXSupportPolicy
= NX_SUPPORT_POLICY_ALWAYSOFF
;
316 Prcb
->FeatureBits
|= KF_NX_DISABLED
;
321 static LDR_DATA_TABLE_ENTRY LdrCoreEntries
[3];
324 KiInitModuleList(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
326 PLDR_DATA_TABLE_ENTRY LdrEntry
;
330 /* Initialize the list head */
331 InitializeListHead(&PsLoadedModuleList
);
333 /* Loop the first 3 entries */
334 for (Entry
= LoaderBlock
->LoadOrderListHead
.Flink
, i
= 0;
335 Entry
!= &LoaderBlock
->LoadOrderListHead
&& i
< 3;
336 Entry
= Entry
->Flink
, i
++)
338 /* Get the data table entry */
339 LdrEntry
= CONTAINING_RECORD(Entry
,
340 LDR_DATA_TABLE_ENTRY
,
344 LdrCoreEntries
[i
] = *LdrEntry
;
346 /* Insert the copy into the list */
347 InsertTailList(&PsLoadedModuleList
, &LdrCoreEntries
[i
].InLoadOrderLinks
);
353 KiSystemStartup(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
356 PKTHREAD InitialThread
;
357 ULONG64 InitialStack
;
361 FrLdrDbgPrint
= LoaderBlock
->u
.I386
.CommonDataArea
;
362 //FrLdrDbgPrint("Hello from KiSystemStartup!!!\n");
364 /* Save the loader block */
365 KeLoaderBlock
= LoaderBlock
;
367 /* Get the current CPU number */
368 Cpu
= (CCHAR
)KeNumberProcessors
++; // FIXME
370 /* LoaderBlock initialization for Cpu 0 */
373 /* Set the initial stack, idle thread and process */
374 LoaderBlock
->KernelStack
= (ULONG_PTR
)P0BootStack
;
375 LoaderBlock
->Thread
= (ULONG_PTR
)&KiInitialThread
;
376 LoaderBlock
->Process
= (ULONG_PTR
)&KiInitialProcess
.Pcb
;
377 LoaderBlock
->Prcb
= (ULONG_PTR
)&KiInitialPcr
.Prcb
;
380 /* Get Pcr from loader block */
381 Pcr
= CONTAINING_RECORD(LoaderBlock
->Prcb
, KIPCR
, Prcb
);
383 /* Set the PRCB for this Processor */
384 KiProcessorBlock
[Cpu
] = &Pcr
->Prcb
;
386 /* Align stack to 16 bytes */
387 LoaderBlock
->KernelStack
&= ~(16 - 1);
389 /* Save the initial thread and stack */
390 InitialStack
= LoaderBlock
->KernelStack
; // Checkme
391 InitialThread
= (PKTHREAD
)LoaderBlock
->Thread
;
393 /* Set us as the current process */
394 InitialThread
->ApcState
.Process
= (PVOID
)LoaderBlock
->Process
;
396 /* Initialize the PCR */
397 KiInitializePcr(Pcr
, Cpu
, InitialThread
, (PVOID
)KiDoubleFaultStack
);
399 /* Initialize the CPU features */
400 KiInitializeCpu(Pcr
);
402 /* Initial setup for the boot CPU */
405 /* Initialize the module list (ntos, hal, kdcom) */
406 KiInitModuleList(LoaderBlock
);
408 /* Setup the TSS descriptors and entries */
409 KiInitializeTss(Pcr
->TssBase
, InitialStack
);
414 /* Initialize debugging system */
415 KdInitSystem(0, KeLoaderBlock
);
417 /* Check for break-in */
418 if (KdPollBreakIn()) DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
421 DPRINT1("Pcr = %p, Gdt = %p, Idt = %p, Tss = %p\n",
422 Pcr
, Pcr
->GdtBase
, Pcr
->IdtBase
, Pcr
->TssBase
);
425 while (InterlockedBitTestAndSet64((PLONG64
)&KiFreezeExecutionLock
, 0))
427 /* Loop until lock is free */
428 while ((*(volatile KSPIN_LOCK
*)&KiFreezeExecutionLock
) & 1);
431 /* Initialize the Processor with HAL */
432 HalInitializeProcessor(Cpu
, KeLoaderBlock
);
434 /* Set processor as active */
435 KeActiveProcessors
|= 1ULL << Cpu
;
438 InterlockedAnd64((PLONG64
)&KiFreezeExecutionLock
, 0);
440 /* Raise to HIGH_LEVEL */
441 KfRaiseIrql(HIGH_LEVEL
);
443 /* Machine specific kernel initialization */
444 if (Cpu
== 0) KiInitializeKernelMachineDependent(&Pcr
->Prcb
, LoaderBlock
);
446 /* Switch to new kernel stack and start kernel bootstrapping */
447 KiSwitchToBootStack(InitialStack
& ~3);