xref: /reactos/ntoskrnl/ke/amd64/kiinit.c (revision 83e13630)
1 /*
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)
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15 
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)
19 
20 /* GLOBALS *******************************************************************/
21 
22 /* Function pointer for early debug prints */
23 ULONG (*FrLdrDbgPrint)(const char *Format, ...);
24 
25 /* Spinlocks used only on X86 */
26 KSPIN_LOCK KiFreezeExecutionLock;
27 
28 
29 KIPCR KiInitialPcr;
30 
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];
36 
37 ULONGLONG BootCycles, BootCyclesEnd;
38 
39 void KiInitializeSegments();
40 void KiSystemCallEntry64();
41 void KiSystemCallEntry32();
42 
43 /* FUNCTIONS *****************************************************************/
44 
45 CODE_SEG("INIT")
46 VOID
47 NTAPI
48 KiInitMachineDependent(VOID)
49 {
50     /* Check for large page support */
51     if (KeFeatureBits & KF_LARGE_PAGE)
52     {
53         /* FIXME: Support this */
54         DPRINT("Large Page support detected but not yet taken advantage of!\n");
55     }
56 
57     /* Check for global page support */
58     if (KeFeatureBits & KF_GLOBAL_PAGE)
59     {
60         /* FIXME: Support this */
61         DPRINT("Global Page support detected but not yet taken advantage of!\n");
62     }
63 
64     /* Check if we have MTRR */
65     if (KeFeatureBits & KF_MTRR)
66     {
67         /* FIXME: Support this */
68         DPRINT("MTRR support detected but not yet taken advantage of!\n");
69     }
70 
71     /* Check for PAT and/or MTRR support */
72     if (KeFeatureBits & KF_PAT)
73     {
74         /* FIXME: Support this */
75         DPRINT("PAT support detected but not yet taken advantage of!\n");
76     }
77 
78 //        /* Allocate the IOPM save area */
79 //        Ki386IopmSaveArea = ExAllocatePoolWithTag(PagedPool,
80 //                                                  IOPM_SIZE,
81 //                                                  '  eK');
82 //        if (!Ki386IopmSaveArea)
83 //        {
84 //            /* Bugcheck. We need this for V86/VDM support. */
85 //            KeBugCheckEx(NO_PAGES_AVAILABLE, 2, IOPM_SIZE, 0, 0);
86 //        }
87 
88 }
89 
90 VOID
91 NTAPI
92 KiInitializePcr(IN PKIPCR Pcr,
93                 IN ULONG ProcessorNumber,
94                 IN PKTHREAD IdleThread,
95                 IN PVOID DpcStack)
96 {
97     KDESCRIPTOR GdtDescriptor = {{0},0,0}, IdtDescriptor = {{0},0,0};
98     PKGDTENTRY64 TssEntry;
99     USHORT Tr = 0;
100 
101     /* Zero out the PCR */
102     RtlZeroMemory(Pcr, sizeof(KIPCR));
103 
104     /* Set pointers to ourselves */
105     Pcr->Self = (PKPCR)Pcr;
106     Pcr->CurrentPrcb = &Pcr->Prcb;
107 
108     /* Set the PCR Version */
109     Pcr->MajorVersion = PCR_MAJOR_VERSION;
110     Pcr->MinorVersion = PCR_MINOR_VERSION;
111 
112     /* Set the PRCB Version */
113     Pcr->Prcb.MajorVersion = PRCB_MAJOR_VERSION;
114     Pcr->Prcb.MinorVersion = PRCB_MINOR_VERSION;
115 
116     /* Set the Build Type */
117     Pcr->Prcb.BuildType = 0;
118 #ifndef CONFIG_SMP
119     Pcr->Prcb.BuildType |= PRCB_BUILD_UNIPROCESSOR;
120 #endif
121 #if DBG
122     Pcr->Prcb.BuildType |= PRCB_BUILD_DEBUG;
123 #endif
124 
125     /* Set the Processor Number and current Processor Mask */
126     Pcr->Prcb.Number = (UCHAR)ProcessorNumber;
127     Pcr->Prcb.SetMember = 1ULL << ProcessorNumber;
128 
129     /* Get GDT and IDT descriptors */
130     __sgdt(&GdtDescriptor.Limit);
131     __sidt(&IdtDescriptor.Limit);
132     Pcr->GdtBase = (PVOID)GdtDescriptor.Base;
133     Pcr->IdtBase = (PKIDTENTRY)IdtDescriptor.Base;
134 
135     /* Get TSS Selector */
136     __str(&Tr);
137     ASSERT(Tr == KGDT64_SYS_TSS);
138 
139     /* Get TSS Entry */
140     TssEntry = KiGetGdtEntry(Pcr->GdtBase, Tr);
141 
142     /* Get the KTSS itself */
143     Pcr->TssBase = KiGetGdtDescriptorBase(TssEntry);
144 
145     Pcr->Prcb.RspBase = Pcr->TssBase->Rsp0; // FIXME
146 
147     /* Set DPC Stack */
148     Pcr->Prcb.DpcStack = DpcStack;
149 
150     /* Setup the processor set */
151     Pcr->Prcb.MultiThreadProcessorSet = Pcr->Prcb.SetMember;
152 
153     /* Clear DR6/7 to cleanup bootloader debugging */
154     Pcr->Prcb.ProcessorState.SpecialRegisters.KernelDr6 = 0;
155     Pcr->Prcb.ProcessorState.SpecialRegisters.KernelDr7 = 0;
156 
157     /* Initialize MXCSR (all exceptions masked) */
158     Pcr->Prcb.MxCsr = INITIAL_MXCSR;
159 
160     /* Set the Current Thread */
161     Pcr->Prcb.CurrentThread = IdleThread;
162 
163     /* Start us out at PASSIVE_LEVEL */
164     Pcr->Irql = PASSIVE_LEVEL;
165     KeSetCurrentIrql(PASSIVE_LEVEL);
166 }
167 
168 VOID
169 NTAPI
170 KiInitializeCpu(PKIPCR Pcr)
171 {
172     ULONG64 Pat;
173     ULONG FeatureBits;
174 
175     /* Initialize gs */
176     KiInitializeSegments();
177 
178     /* Set GS base */
179     __writemsr(MSR_GS_BASE, (ULONG64)Pcr);
180     __writemsr(MSR_GS_SWAP, (ULONG64)Pcr);
181 
182     /* Detect and set the CPU Type */
183     KiSetProcessorType();
184 
185     /* Get the processor features for this CPU */
186     FeatureBits = KiGetFeatureBits();
187 
188     /* Check if we support all needed features */
189     if ((FeatureBits & REQUIRED_FEATURE_BITS) != REQUIRED_FEATURE_BITS)
190     {
191         /* If not, bugcheck system */
192         FrLdrDbgPrint("CPU doesn't have needed features! Has: 0x%x, required: 0x%x\n",
193                 FeatureBits, REQUIRED_FEATURE_BITS);
194         KeBugCheck(0);
195     }
196 
197     /* Set DEP to always on */
198     SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_ALWAYSON;
199     FeatureBits |= KF_NX_ENABLED;
200 
201     /* Save feature bits */
202     Pcr->Prcb.FeatureBits = FeatureBits;
203 
204     /* Enable fx save restore support */
205     __writecr4(__readcr4() | CR4_FXSR);
206 
207     /* Enable XMMI exceptions */
208     __writecr4(__readcr4() | CR4_XMMEXCPT);
209 
210     /* Enable Write-Protection */
211     __writecr0(__readcr0() | CR0_WP);
212 
213     /* Disable fpu monitoring */
214     __writecr0(__readcr0() & ~CR0_MP);
215 
216     /* Disable x87 fpu exceptions */
217     __writecr0(__readcr0() & ~CR0_NE);
218 
219     /* LDT is unused */
220     __lldt(0);
221 
222     /* Set the systemcall entry points */
223     __writemsr(MSR_LSTAR, (ULONG64)KiSystemCallEntry64);
224     __writemsr(MSR_CSTAR, (ULONG64)KiSystemCallEntry32);
225 
226     __writemsr(MSR_STAR, ((ULONG64)KGDT64_R0_CODE << 32) |
227                          ((ULONG64)(KGDT64_R3_CMCODE|RPL_MASK) << 48));
228 
229     /* Set the flags to be cleared when doing a syscall */
230     __writemsr(MSR_SYSCALL_MASK, EFLAGS_IF_MASK | EFLAGS_TF | EFLAGS_DF);
231 
232     /* Enable syscall instruction and no-execute support */
233     __writemsr(MSR_EFER, __readmsr(MSR_EFER) | MSR_SCE | MSR_NXE);
234 
235     /* Initialize the PAT */
236     Pat = (PAT_WB << 0)  | (PAT_WC << 8) | (PAT_UCM << 16) | (PAT_UC << 24) |
237           (PAT_WB << 32) | (PAT_WC << 40) | (PAT_UCM << 48) | (PAT_UC << 56);
238     __writemsr(MSR_PAT, Pat);
239 
240     /* Initialize MXCSR */
241     _mm_setcsr(INITIAL_MXCSR);
242 }
243 
244 VOID
245 FASTCALL
246 KiInitializeTss(IN PKTSS64 Tss,
247                 IN UINT64 Stack)
248 {
249     PKGDTENTRY64 TssEntry;
250 
251     /* Get pointer to the GDT entry */
252     TssEntry = KiGetGdtEntry(KeGetPcr()->GdtBase, KGDT64_SYS_TSS);
253 
254     /* Initialize the GDT entry */
255     KiInitGdtEntry(TssEntry, (ULONG64)Tss, sizeof(KTSS64), AMD64_TSS, 0);
256 
257     /* Zero out the TSS */
258     RtlZeroMemory(Tss, sizeof(KTSS64));
259 
260     /* FIXME: I/O Map? */
261     Tss->IoMapBase = 0x68;
262 
263     /* Setup ring 0 stack pointer */
264     Tss->Rsp0 = Stack;
265 
266     /* Setup a stack for Double Fault Traps */
267     Tss->Ist[1] = (ULONG64)KiDoubleFaultStack;
268 
269     /* Setup a stack for CheckAbort Traps */
270     Tss->Ist[2] = (ULONG64)KiDoubleFaultStack;
271 
272     /* Setup a stack for NMI Traps */
273     Tss->Ist[3] = (ULONG64)KiDoubleFaultStack;
274 
275     /* Load the task register */
276     __ltr(KGDT64_SYS_TSS);
277 }
278 
279 CODE_SEG("INIT")
280 VOID
281 NTAPI
282 KiInitializeKernelMachineDependent(
283     IN PKPRCB Prcb,
284     IN PLOADER_PARAMETER_BLOCK LoaderBlock)
285 {
286     /* Set boot-level flags */
287     KeI386CpuType = Prcb->CpuType;
288     KeI386CpuStep = Prcb->CpuStep;
289     KeProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64;
290     KeProcessorLevel = (USHORT)Prcb->CpuType;
291     if (Prcb->CpuID)
292         KeProcessorRevision = Prcb->CpuStep;
293 
294     /* Set basic CPU Features that user mode can read */
295     SharedUserData->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
296     SharedUserData->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
297     SharedUserData->ProcessorFeatures[PF_PPC_MOVEMEM_64BIT_OK] = TRUE;
298     SharedUserData->ProcessorFeatures[PF_PAE_ENABLED] = TRUE; // ???
299     SharedUserData->ProcessorFeatures[PF_NX_ENABLED] = TRUE;
300     SharedUserData->ProcessorFeatures[PF_FASTFAIL_AVAILABLE] = TRUE;
301     SharedUserData->ProcessorFeatures[PF_XSAVE_ENABLED] = TRUE;
302     SharedUserData->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] =
303         (Prcb->FeatureBits & KF_MMX) ? TRUE: FALSE;
304     SharedUserData->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] =
305         ((Prcb->FeatureBits & KF_FXSR) && (Prcb->FeatureBits & KF_XMMI)) ? TRUE: FALSE;
306     SharedUserData->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] =
307         ((Prcb->FeatureBits & KF_FXSR) && (Prcb->FeatureBits & KF_XMMI64)) ? TRUE: FALSE;
308     SharedUserData->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] =
309         (Prcb->FeatureBits & KF_3DNOW) ? TRUE: FALSE;
310     SharedUserData->ProcessorFeatures[PF_SSE3_INSTRUCTIONS_AVAILABLE] =
311         (Prcb->FeatureBits & KF_SSE3) ? TRUE: FALSE;
312     SharedUserData->ProcessorFeatures[PF_COMPARE_EXCHANGE128] =
313         (Prcb->FeatureBits & KF_CMPXCHG16B) ? TRUE: FALSE;
314 
315     /* Set the default NX policy (opt-in) */
316     SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_OPTIN;
317 
318     /* Check if NPX is always on */
319     if (strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=ALWAYSON"))
320     {
321         /* Set it always on */
322         SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_ALWAYSON;
323         Prcb->FeatureBits |= KF_NX_ENABLED;
324     }
325     else if (strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=OPTOUT"))
326     {
327         /* Set it in opt-out mode */
328         SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_OPTOUT;
329         Prcb->FeatureBits |= KF_NX_ENABLED;
330     }
331     else if ((strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=OPTIN")) ||
332              (strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE")))
333     {
334         /* Set the feature bits */
335         Prcb->FeatureBits |= KF_NX_ENABLED;
336     }
337     else if ((strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=ALWAYSOFF")) ||
338              (strstr(KeLoaderBlock->LoadOptions, "EXECUTE")))
339     {
340         /* Set disabled mode */
341         SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_ALWAYSOFF;
342         Prcb->FeatureBits |= KF_NX_DISABLED;
343     }
344 }
345 
346 static LDR_DATA_TABLE_ENTRY LdrCoreEntries[3];
347 
348 void
349 KiInitModuleList(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
350 {
351     PLDR_DATA_TABLE_ENTRY LdrEntry;
352     PLIST_ENTRY Entry;
353     ULONG i;
354 
355     /* Initialize the list head */
356     InitializeListHead(&PsLoadedModuleList);
357 
358     /* Loop the first 3 entries */
359     for (Entry = LoaderBlock->LoadOrderListHead.Flink, i = 0;
360          Entry != &LoaderBlock->LoadOrderListHead && i < 3;
361          Entry = Entry->Flink, i++)
362     {
363         /* Get the data table entry */
364         LdrEntry = CONTAINING_RECORD(Entry,
365                                      LDR_DATA_TABLE_ENTRY,
366                                      InLoadOrderLinks);
367 
368         /* Copy the entry */
369         LdrCoreEntries[i] = *LdrEntry;
370 
371         /* Insert the copy into the list */
372         InsertTailList(&PsLoadedModuleList, &LdrCoreEntries[i].InLoadOrderLinks);
373     }
374 }
375 
376 CODE_SEG("INIT")
377 DECLSPEC_NORETURN
378 VOID
379 NTAPI
380 KiSystemStartup(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
381 {
382     CCHAR Cpu;
383     PKTHREAD InitialThread;
384     ULONG64 InitialStack;
385     PKIPCR Pcr;
386 
387     /* Boot cycles timestamp */
388     BootCycles = __rdtsc();
389 
390     /* HACK */
391     FrLdrDbgPrint = LoaderBlock->u.I386.CommonDataArea;
392     //FrLdrDbgPrint("Hello from KiSystemStartup!!!\n");
393 
394     /* Save the loader block */
395     KeLoaderBlock = LoaderBlock;
396 
397     /* Get the current CPU number */
398     Cpu = KeNumberProcessors++; // FIXME
399 
400     /* LoaderBlock initialization for Cpu 0 */
401     if (Cpu == 0)
402     {
403         /* Set the initial stack, idle thread and process */
404         LoaderBlock->KernelStack = (ULONG_PTR)P0BootStack;
405         LoaderBlock->Thread = (ULONG_PTR)&KiInitialThread;
406         LoaderBlock->Process = (ULONG_PTR)&KiInitialProcess.Pcb;
407         LoaderBlock->Prcb = (ULONG_PTR)&KiInitialPcr.Prcb;
408     }
409 
410     /* Get Pcr from loader block */
411     Pcr = CONTAINING_RECORD(LoaderBlock->Prcb, KIPCR, Prcb);
412 
413     /* Set the PRCB for this Processor */
414     KiProcessorBlock[Cpu] = &Pcr->Prcb;
415 
416     /* Align stack to 16 bytes */
417     LoaderBlock->KernelStack &= ~(16 - 1);
418 
419     /* Save the initial thread and stack */
420     InitialStack = LoaderBlock->KernelStack; // Checkme
421     InitialThread = (PKTHREAD)LoaderBlock->Thread;
422 
423     /* Set us as the current process */
424     InitialThread->ApcState.Process = (PVOID)LoaderBlock->Process;
425 
426     /* Initialize the PCR */
427     KiInitializePcr(Pcr, Cpu, InitialThread, (PVOID)KiDoubleFaultStack);
428 
429     /* Initialize the CPU features */
430     KiInitializeCpu(Pcr);
431 
432     /* Initial setup for the boot CPU */
433     if (Cpu == 0)
434     {
435         /* Initialize the module list (ntos, hal, kdcom) */
436         KiInitModuleList(LoaderBlock);
437 
438         /* Setup the TSS descriptors and entries */
439         KiInitializeTss(Pcr->TssBase, InitialStack);
440 
441         /* Setup the IDT */
442         KeInitExceptions();
443 
444          /* Initialize debugging system */
445         KdInitSystem(0, KeLoaderBlock);
446 
447         /* Check for break-in */
448         if (KdPollBreakIn()) DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C);
449     }
450 
451     DPRINT1("Pcr = %p, Gdt = %p, Idt = %p, Tss = %p\n",
452            Pcr, Pcr->GdtBase, Pcr->IdtBase, Pcr->TssBase);
453 
454     /* Acquire lock */
455     while (InterlockedBitTestAndSet64((PLONG64)&KiFreezeExecutionLock, 0))
456     {
457         /* Loop until lock is free */
458         while ((*(volatile KSPIN_LOCK*)&KiFreezeExecutionLock) & 1);
459     }
460 
461     /* Initialize the Processor with HAL */
462     HalInitializeProcessor(Cpu, KeLoaderBlock);
463 
464     /* Set processor as active */
465     KeActiveProcessors |= 1ULL << Cpu;
466 
467     /* Release lock */
468     InterlockedAnd64((PLONG64)&KiFreezeExecutionLock, 0);
469 
470     /* Raise to HIGH_LEVEL */
471     KfRaiseIrql(HIGH_LEVEL);
472 
473     /* Machine specific kernel initialization */
474     if (Cpu == 0) KiInitializeKernelMachineDependent(&Pcr->Prcb, LoaderBlock);
475 
476     /* Switch to new kernel stack and start kernel bootstrapping */
477     KiSwitchToBootStack(InitialStack & ~3);
478 }
479 
480