2006-09-03 21:06:30 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: ntoskrnl/ke/i386/kiinit.c
|
|
|
|
* PURPOSE: Kernel Initialization for x86 CPUs
|
|
|
|
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
|
|
|
#include <ntoskrnl.h>
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
2011-09-13 12:53:50 +00:00
|
|
|
#include "internal/i386/trap_x.h"
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* GLOBALS *******************************************************************/
|
|
|
|
|
2010-01-19 18:27:24 +00:00
|
|
|
/* Boot and double-fault/NMI/DPC stack */
|
2014-09-13 08:24:03 +00:00
|
|
|
UCHAR DECLSPEC_ALIGN(PAGE_SIZE) P0BootStackData[KERNEL_STACK_SIZE] = {0};
|
|
|
|
UCHAR DECLSPEC_ALIGN(PAGE_SIZE) KiDoubleFaultStackData[KERNEL_STACK_SIZE] = {0};
|
2010-01-19 18:27:24 +00:00
|
|
|
ULONG_PTR P0BootStack = (ULONG_PTR)&P0BootStackData[KERNEL_STACK_SIZE];
|
|
|
|
ULONG_PTR KiDoubleFaultStack = (ULONG_PTR)&KiDoubleFaultStackData[KERNEL_STACK_SIZE];
|
|
|
|
|
2006-09-03 21:06:30 +00:00
|
|
|
/* Spinlocks used only on X86 */
|
|
|
|
KSPIN_LOCK KiFreezeExecutionLock;
|
|
|
|
KSPIN_LOCK Ki486CompatibilityLock;
|
|
|
|
|
2010-01-28 15:51:18 +00:00
|
|
|
/* Perf */
|
|
|
|
ULONG ProcessCount;
|
|
|
|
ULONGLONG BootCycles, BootCyclesEnd;
|
|
|
|
|
2006-09-03 21:06:30 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
2018-12-30 11:19:11 +00:00
|
|
|
INIT_FUNCTION
|
2006-10-04 03:37:11 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
KiInitMachineDependent(VOID)
|
|
|
|
{
|
|
|
|
ULONG CpuCount;
|
|
|
|
BOOLEAN FbCaching = FALSE;
|
|
|
|
NTSTATUS Status;
|
2006-11-27 19:26:31 +00:00
|
|
|
ULONG ReturnLength;
|
2007-01-18 06:23:14 +00:00
|
|
|
ULONG i, Affinity, Sample = 0;
|
2006-10-04 05:29:30 +00:00
|
|
|
PFX_SAVE_AREA FxSaveArea;
|
2007-01-18 06:23:14 +00:00
|
|
|
ULONG MXCsrMask = 0xFFBF;
|
2015-05-02 23:11:50 +00:00
|
|
|
CPU_INFO CpuInfo;
|
2018-03-01 09:36:27 +00:00
|
|
|
KI_SAMPLE_MAP Samples[10];
|
2007-01-18 06:23:14 +00:00
|
|
|
PKI_SAMPLE_MAP CurrentSample = Samples;
|
2013-07-16 13:49:03 +00:00
|
|
|
LARGE_IDENTITY_MAP IdentityMap;
|
2006-10-04 03:37:11 +00:00
|
|
|
|
|
|
|
/* Check for large page support */
|
|
|
|
if (KeFeatureBits & KF_LARGE_PAGE)
|
|
|
|
{
|
2013-07-16 13:49:03 +00:00
|
|
|
/* Do an IPI to enable it on all CPUs */
|
|
|
|
if (Ki386CreateIdentityMap(&IdentityMap, Ki386EnableCurrentLargePage, 2))
|
|
|
|
KeIpiGenericCall(Ki386EnableTargetLargePage, (ULONG_PTR)&IdentityMap);
|
|
|
|
|
|
|
|
/* Free the pages allocated for identity map */
|
|
|
|
Ki386FreeIdentityMap(&IdentityMap);
|
2006-10-04 03:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for global page support */
|
|
|
|
if (KeFeatureBits & KF_GLOBAL_PAGE)
|
|
|
|
{
|
|
|
|
/* Do an IPI to enable it on all CPUs */
|
|
|
|
CpuCount = KeNumberProcessors;
|
|
|
|
KeIpiGenericCall(Ki386EnableGlobalPage, (ULONG_PTR)&CpuCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for PAT and/or MTRR support */
|
|
|
|
if (KeFeatureBits & (KF_PAT | KF_MTRR))
|
|
|
|
{
|
|
|
|
/* Query the HAL to make sure we can use it */
|
|
|
|
Status = HalQuerySystemInformation(HalFrameBufferCachingInformation,
|
|
|
|
sizeof(BOOLEAN),
|
|
|
|
&FbCaching,
|
|
|
|
&ReturnLength);
|
|
|
|
if ((NT_SUCCESS(Status)) && (FbCaching))
|
|
|
|
{
|
|
|
|
/* We can't, disable it */
|
|
|
|
KeFeatureBits &= ~(KF_PAT | KF_MTRR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for PAT support and enable it */
|
|
|
|
if (KeFeatureBits & KF_PAT) KiInitializePAT();
|
|
|
|
|
2006-10-04 05:29:30 +00:00
|
|
|
/* Assume no errata for now */
|
|
|
|
SharedUserData->ProcessorFeatures[PF_FLOATING_POINT_PRECISION_ERRATA] = 0;
|
|
|
|
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Check if we have an NPX */
|
|
|
|
if (KeI386NpxPresent)
|
|
|
|
{
|
|
|
|
/* Loop every CPU */
|
|
|
|
i = KeActiveProcessors;
|
|
|
|
for (Affinity = 1; i; Affinity <<= 1)
|
|
|
|
{
|
|
|
|
/* Check if this is part of the set */
|
|
|
|
if (i & Affinity)
|
|
|
|
{
|
|
|
|
/* Run on this CPU */
|
|
|
|
i &= ~Affinity;
|
|
|
|
KeSetSystemAffinityThread(Affinity);
|
|
|
|
|
|
|
|
/* Detect FPU errata */
|
|
|
|
if (KiIsNpxErrataPresent())
|
|
|
|
{
|
|
|
|
/* Disable NPX support */
|
|
|
|
KeI386NpxPresent = FALSE;
|
|
|
|
SharedUserData->
|
|
|
|
ProcessorFeatures[PF_FLOATING_POINT_PRECISION_ERRATA] =
|
|
|
|
TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-04 05:29:30 +00:00
|
|
|
/* If there's no NPX, then we're emulating the FPU */
|
|
|
|
SharedUserData->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] =
|
|
|
|
!KeI386NpxPresent;
|
|
|
|
|
|
|
|
/* Check if there's no NPX, so that we can disable associated features */
|
|
|
|
if (!KeI386NpxPresent)
|
|
|
|
{
|
|
|
|
/* Remove NPX-related bits */
|
|
|
|
KeFeatureBits &= ~(KF_XMMI64 | KF_XMMI | KF_FXSR | KF_MMX);
|
|
|
|
|
|
|
|
/* Disable kernel flags */
|
|
|
|
KeI386FxsrPresent = KeI386XMMIPresent = FALSE;
|
|
|
|
|
|
|
|
/* Disable processor features that might've been set until now */
|
|
|
|
SharedUserData->ProcessorFeatures[PF_FLOATING_POINT_PRECISION_ERRATA] =
|
|
|
|
SharedUserData->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] =
|
|
|
|
SharedUserData->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] =
|
|
|
|
SharedUserData->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] =
|
|
|
|
SharedUserData->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = 0;
|
|
|
|
}
|
|
|
|
|
2006-10-04 03:37:11 +00:00
|
|
|
/* Check for CR4 support */
|
|
|
|
if (KeFeatureBits & KF_CR4)
|
|
|
|
{
|
|
|
|
/* Do an IPI call to enable the Debug Exceptions */
|
|
|
|
CpuCount = KeNumberProcessors;
|
|
|
|
KeIpiGenericCall(Ki386EnableDE, (ULONG_PTR)&CpuCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if FXSR was found */
|
|
|
|
if (KeFeatureBits & KF_FXSR)
|
|
|
|
{
|
|
|
|
/* Do an IPI call to enable the FXSR */
|
|
|
|
CpuCount = KeNumberProcessors;
|
|
|
|
KeIpiGenericCall(Ki386EnableFxsr, (ULONG_PTR)&CpuCount);
|
|
|
|
|
|
|
|
/* Check if XMM was found too */
|
|
|
|
if (KeFeatureBits & KF_XMMI)
|
|
|
|
{
|
|
|
|
/* Do an IPI call to enable XMMI exceptions */
|
|
|
|
CpuCount = KeNumberProcessors;
|
|
|
|
KeIpiGenericCall(Ki386EnableXMMIExceptions, (ULONG_PTR)&CpuCount);
|
|
|
|
|
|
|
|
/* FIXME: Implement and enable XMM Page Zeroing for Mm */
|
|
|
|
|
|
|
|
/* Patch the RtlPrefetchMemoryNonTemporal routine to enable it */
|
2015-09-01 19:51:52 +00:00
|
|
|
*(PCHAR)RtlPrefetchMemoryNonTemporal = 0x90; // NOP
|
2006-10-04 03:37:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for, and enable SYSENTER support */
|
|
|
|
KiRestoreFastSyscallReturnState();
|
|
|
|
|
|
|
|
/* Loop every CPU */
|
|
|
|
i = KeActiveProcessors;
|
|
|
|
for (Affinity = 1; i; Affinity <<= 1)
|
|
|
|
{
|
|
|
|
/* Check if this is part of the set */
|
|
|
|
if (i & Affinity)
|
|
|
|
{
|
|
|
|
/* Run on this CPU */
|
|
|
|
i &= ~Affinity;
|
|
|
|
KeSetSystemAffinityThread(Affinity);
|
|
|
|
|
|
|
|
/* Reset MHz to 0 for this CPU */
|
|
|
|
KeGetCurrentPrcb()->MHz = 0;
|
|
|
|
|
|
|
|
/* Check if we can use RDTSC */
|
|
|
|
if (KeFeatureBits & KF_RDTSC)
|
|
|
|
{
|
|
|
|
/* Start sampling loop */
|
|
|
|
for (;;)
|
|
|
|
{
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Do a dummy CPUID to start the sample */
|
2015-05-02 23:11:50 +00:00
|
|
|
KiCpuId(&CpuInfo, 0);
|
2007-01-18 06:23:14 +00:00
|
|
|
|
|
|
|
/* Fill out the starting data */
|
|
|
|
CurrentSample->PerfStart = KeQueryPerformanceCounter(NULL);
|
|
|
|
CurrentSample->TSCStart = __rdtsc();
|
|
|
|
CurrentSample->PerfFreq.QuadPart = -50000;
|
|
|
|
|
|
|
|
/* Sleep for this sample */
|
|
|
|
KeDelayExecutionThread(KernelMode,
|
|
|
|
FALSE,
|
|
|
|
&CurrentSample->PerfFreq);
|
|
|
|
|
|
|
|
/* Do another dummy CPUID */
|
2015-05-02 23:11:50 +00:00
|
|
|
KiCpuId(&CpuInfo, 0);
|
2007-01-18 06:23:14 +00:00
|
|
|
|
|
|
|
/* Fill out the ending data */
|
|
|
|
CurrentSample->PerfEnd =
|
|
|
|
KeQueryPerformanceCounter(&CurrentSample->PerfFreq);
|
|
|
|
CurrentSample->TSCEnd = __rdtsc();
|
|
|
|
|
|
|
|
/* Calculate the differences */
|
|
|
|
CurrentSample->PerfDelta = CurrentSample->PerfEnd.QuadPart -
|
|
|
|
CurrentSample->PerfStart.QuadPart;
|
|
|
|
CurrentSample->TSCDelta = CurrentSample->TSCEnd -
|
|
|
|
CurrentSample->TSCStart;
|
|
|
|
|
|
|
|
/* Compute CPU Speed */
|
2007-08-04 08:49:47 +00:00
|
|
|
CurrentSample->MHz = (ULONG)((CurrentSample->TSCDelta *
|
|
|
|
CurrentSample->
|
|
|
|
PerfFreq.QuadPart + 500000) /
|
|
|
|
(CurrentSample->PerfDelta *
|
|
|
|
1000000));
|
2007-01-18 06:23:14 +00:00
|
|
|
|
|
|
|
/* Check if this isn't the first sample */
|
|
|
|
if (Sample)
|
|
|
|
{
|
|
|
|
/* Check if we got a good precision within 1MHz */
|
|
|
|
if ((CurrentSample->MHz == CurrentSample[-1].MHz) ||
|
|
|
|
(CurrentSample->MHz == CurrentSample[-1].MHz + 1) ||
|
|
|
|
(CurrentSample->MHz == CurrentSample[-1].MHz - 1))
|
|
|
|
{
|
|
|
|
/* We did, stop sampling */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move on */
|
|
|
|
CurrentSample++;
|
|
|
|
Sample++;
|
2007-07-10 13:30:43 +00:00
|
|
|
|
2018-03-01 09:36:27 +00:00
|
|
|
if (Sample == RTL_NUMBER_OF(Samples))
|
2007-07-10 13:30:43 +00:00
|
|
|
{
|
2018-03-01 09:36:27 +00:00
|
|
|
/* No luck. Average the samples and be done */
|
|
|
|
ULONG TotalMHz = 0;
|
|
|
|
while (Sample--)
|
|
|
|
{
|
|
|
|
TotalMHz += Samples[Sample].MHz;
|
|
|
|
}
|
|
|
|
CurrentSample[-1].MHz = TotalMHz / RTL_NUMBER_OF(Samples);
|
|
|
|
DPRINT1("Sampling CPU frequency failed. Using average of %lu MHz\n", CurrentSample[-1].MHz);
|
|
|
|
break;
|
2007-07-10 13:30:43 +00:00
|
|
|
}
|
2006-10-04 03:37:11 +00:00
|
|
|
}
|
2007-01-18 06:23:14 +00:00
|
|
|
|
|
|
|
/* Save the CPU Speed */
|
|
|
|
KeGetCurrentPrcb()->MHz = CurrentSample[-1].MHz;
|
2006-10-04 03:37:11 +00:00
|
|
|
}
|
|
|
|
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Check if we have MTRR */
|
|
|
|
if (KeFeatureBits & KF_MTRR)
|
2006-10-04 03:37:11 +00:00
|
|
|
{
|
|
|
|
/* Then manually initialize MTRR for the CPU */
|
2007-03-04 19:06:34 +00:00
|
|
|
KiInitializeMTRR(i ? FALSE : TRUE);
|
2006-10-04 03:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have AMD MTRR and initialize it for the CPU */
|
|
|
|
if (KeFeatureBits & KF_AMDK6MTRR) KiAmdK6InitializeMTRR();
|
|
|
|
|
2006-10-04 05:29:30 +00:00
|
|
|
/* Check if this is a buggy Pentium and apply the fixup if so */
|
|
|
|
if (KiI386PentiumLockErrataPresent) KiI386PentiumLockErrataFixup();
|
|
|
|
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Check if the CPU supports FXSR */
|
|
|
|
if (KeFeatureBits & KF_FXSR)
|
|
|
|
{
|
|
|
|
/* Get the current thread NPX state */
|
2010-11-19 06:34:14 +00:00
|
|
|
FxSaveArea = KiGetThreadNpxArea(KeGetCurrentThread());
|
2006-10-04 05:29:30 +00:00
|
|
|
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Clear initial MXCsr mask */
|
|
|
|
FxSaveArea->U.FxArea.MXCsrMask = 0;
|
2006-10-04 05:29:30 +00:00
|
|
|
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Save the current NPX State */
|
2010-11-19 06:34:14 +00:00
|
|
|
Ke386SaveFpuState(FxSaveArea);
|
|
|
|
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Check if the current mask doesn't match the reserved bits */
|
|
|
|
if (FxSaveArea->U.FxArea.MXCsrMask != 0)
|
|
|
|
{
|
|
|
|
/* Then use whatever it's holding */
|
|
|
|
MXCsrMask = FxSaveArea->U.FxArea.MXCsrMask;
|
|
|
|
}
|
2006-10-04 05:29:30 +00:00
|
|
|
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Check if nobody set the kernel-wide mask */
|
|
|
|
if (!KiMXCsrMask)
|
2006-10-04 05:29:30 +00:00
|
|
|
{
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Then use the one we calculated above */
|
|
|
|
KiMXCsrMask = MXCsrMask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Was it set to the same value we found now? */
|
|
|
|
if (KiMXCsrMask != MXCsrMask)
|
|
|
|
{
|
|
|
|
/* No, something is definitely wrong */
|
2008-08-24 15:48:05 +00:00
|
|
|
KeBugCheckEx(MULTIPROCESSOR_CONFIGURATION_NOT_SUPPORTED,
|
2007-01-18 06:23:14 +00:00
|
|
|
KF_FXSR,
|
|
|
|
KiMXCsrMask,
|
|
|
|
MXCsrMask,
|
|
|
|
0);
|
|
|
|
}
|
2006-10-04 05:29:30 +00:00
|
|
|
}
|
|
|
|
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Now set the kernel mask */
|
|
|
|
KiMXCsrMask &= MXCsrMask;
|
|
|
|
}
|
2006-10-04 03:37:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return affinity back to where it was */
|
|
|
|
KeRevertToUserAffinityThread();
|
2006-10-04 05:29:30 +00:00
|
|
|
|
|
|
|
/* NT allows limiting the duration of an ISR with a registry key */
|
|
|
|
if (KiTimeLimitIsrMicroseconds)
|
|
|
|
{
|
|
|
|
/* FIXME: TODO */
|
|
|
|
DPRINT1("ISR Time Limit not yet supported\n");
|
|
|
|
}
|
2011-09-25 09:12:26 +00:00
|
|
|
|
2010-01-27 03:05:10 +00:00
|
|
|
/* Set CR0 features based on detected CPU */
|
|
|
|
KiSetCR0Bits();
|
2006-10-04 03:37:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 11:19:11 +00:00
|
|
|
INIT_FUNCTION
|
2006-09-03 21:06:30 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
KiInitializePcr(IN ULONG ProcessorNumber,
|
|
|
|
IN PKIPCR Pcr,
|
|
|
|
IN PKIDTENTRY Idt,
|
|
|
|
IN PKGDTENTRY Gdt,
|
|
|
|
IN PKTSS Tss,
|
|
|
|
IN PKTHREAD IdleThread,
|
|
|
|
IN PVOID DpcStack)
|
|
|
|
{
|
|
|
|
/* Setup the TIB */
|
|
|
|
Pcr->NtTib.ExceptionList = EXCEPTION_CHAIN_END;
|
|
|
|
Pcr->NtTib.StackBase = 0;
|
|
|
|
Pcr->NtTib.StackLimit = 0;
|
2007-01-18 06:23:14 +00:00
|
|
|
Pcr->NtTib.Self = NULL;
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Set the Current Thread */
|
2006-09-17 07:06:35 +00:00
|
|
|
Pcr->PrcbData.CurrentThread = IdleThread;
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Set pointers to ourselves */
|
2017-12-12 11:50:58 +00:00
|
|
|
Pcr->SelfPcr = (PKPCR)Pcr;
|
2006-09-03 21:06:30 +00:00
|
|
|
Pcr->Prcb = &Pcr->PrcbData;
|
|
|
|
|
|
|
|
/* Set the PCR Version */
|
|
|
|
Pcr->MajorVersion = PCR_MAJOR_VERSION;
|
|
|
|
Pcr->MinorVersion = PCR_MINOR_VERSION;
|
|
|
|
|
|
|
|
/* Set the PCRB Version */
|
|
|
|
Pcr->PrcbData.MajorVersion = 1;
|
|
|
|
Pcr->PrcbData.MinorVersion = 1;
|
|
|
|
|
|
|
|
/* Set the Build Type */
|
|
|
|
Pcr->PrcbData.BuildType = 0;
|
2006-11-27 19:26:31 +00:00
|
|
|
#ifndef CONFIG_SMP
|
|
|
|
Pcr->PrcbData.BuildType |= PRCB_BUILD_UNIPROCESSOR;
|
|
|
|
#endif
|
2009-06-17 12:44:05 +00:00
|
|
|
#if DBG
|
2006-11-27 19:26:31 +00:00
|
|
|
Pcr->PrcbData.BuildType |= PRCB_BUILD_DEBUG;
|
|
|
|
#endif
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Set the Processor Number and current Processor Mask */
|
|
|
|
Pcr->PrcbData.Number = (UCHAR)ProcessorNumber;
|
|
|
|
Pcr->PrcbData.SetMember = 1 << ProcessorNumber;
|
|
|
|
|
|
|
|
/* Set the PRCB for this Processor */
|
|
|
|
KiProcessorBlock[ProcessorNumber] = Pcr->Prcb;
|
|
|
|
|
|
|
|
/* Start us out at PASSIVE_LEVEL */
|
|
|
|
Pcr->Irql = PASSIVE_LEVEL;
|
|
|
|
|
|
|
|
/* Set the GDI, IDT, TSS and DPC Stack */
|
|
|
|
Pcr->GDT = (PVOID)Gdt;
|
|
|
|
Pcr->IDT = Idt;
|
|
|
|
Pcr->TSS = Tss;
|
2007-01-18 06:23:14 +00:00
|
|
|
Pcr->TssCopy = Tss;
|
2006-09-03 21:06:30 +00:00
|
|
|
Pcr->PrcbData.DpcStack = DpcStack;
|
2007-01-18 06:23:14 +00:00
|
|
|
|
|
|
|
/* Setup the processor set */
|
|
|
|
Pcr->PrcbData.MultiThreadProcessorSet = Pcr->PrcbData.SetMember;
|
2006-09-03 21:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 11:19:11 +00:00
|
|
|
INIT_FUNCTION
|
2006-09-03 21:06:30 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
KiInitializeKernel(IN PKPROCESS InitProcess,
|
|
|
|
IN PKTHREAD InitThread,
|
|
|
|
IN PVOID IdleStack,
|
|
|
|
IN PKPRCB Prcb,
|
|
|
|
IN CCHAR Number,
|
2006-09-30 03:33:50 +00:00
|
|
|
IN PLOADER_PARAMETER_BLOCK LoaderBlock)
|
2006-09-03 21:06:30 +00:00
|
|
|
{
|
|
|
|
BOOLEAN NpxPresent;
|
|
|
|
ULONG FeatureBits;
|
2008-08-11 08:40:52 +00:00
|
|
|
ULONG PageDirectory[2];
|
2006-09-03 21:06:30 +00:00
|
|
|
PVOID DpcStack;
|
2007-01-18 06:23:14 +00:00
|
|
|
ULONG Vendor[3];
|
2015-09-26 10:12:53 +00:00
|
|
|
KIRQL DummyIrql;
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Detect and set the CPU Type */
|
|
|
|
KiSetProcessorType();
|
|
|
|
|
|
|
|
/* Check if an FPU is present */
|
|
|
|
NpxPresent = KiIsNpxPresent();
|
|
|
|
|
|
|
|
/* Initialize the Power Management Support for this PRCB */
|
|
|
|
PoInitializePrcb(Prcb);
|
|
|
|
|
|
|
|
/* Bugcheck if this is a 386 CPU */
|
2011-09-24 18:13:57 +00:00
|
|
|
if (Prcb->CpuType == 3) KeBugCheckEx(UNSUPPORTED_PROCESSOR, 0x386, 0, 0, 0);
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Get the processor features for the CPU */
|
|
|
|
FeatureBits = KiGetFeatureBits();
|
|
|
|
|
2006-10-04 05:48:46 +00:00
|
|
|
/* Set the default NX policy (opt-in) */
|
2007-03-02 06:01:07 +00:00
|
|
|
SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_OPTIN;
|
2006-10-04 05:48:46 +00:00
|
|
|
|
|
|
|
/* Check if NPX is always on */
|
|
|
|
if (strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=ALWAYSON"))
|
|
|
|
{
|
|
|
|
/* Set it always on */
|
2007-03-02 06:01:07 +00:00
|
|
|
SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_ALWAYSON;
|
2006-10-04 05:59:56 +00:00
|
|
|
FeatureBits |= KF_NX_ENABLED;
|
2006-10-04 05:48:46 +00:00
|
|
|
}
|
|
|
|
else if (strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=OPTOUT"))
|
|
|
|
{
|
|
|
|
/* Set it in opt-out mode */
|
2007-03-02 06:01:07 +00:00
|
|
|
SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_OPTOUT;
|
2006-10-04 05:59:56 +00:00
|
|
|
FeatureBits |= KF_NX_ENABLED;
|
2006-10-04 05:48:46 +00:00
|
|
|
}
|
|
|
|
else if ((strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=OPTIN")) ||
|
|
|
|
(strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE")))
|
|
|
|
{
|
|
|
|
/* Set the feature bits */
|
2006-10-04 05:59:56 +00:00
|
|
|
FeatureBits |= KF_NX_ENABLED;
|
2006-10-04 05:48:46 +00:00
|
|
|
}
|
|
|
|
else if ((strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=ALWAYSOFF")) ||
|
|
|
|
(strstr(KeLoaderBlock->LoadOptions, "EXECUTE")))
|
|
|
|
{
|
|
|
|
/* Set disabled mode */
|
2007-03-02 06:01:07 +00:00
|
|
|
SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_ALWAYSOFF;
|
2006-10-04 05:59:56 +00:00
|
|
|
FeatureBits |= KF_NX_DISABLED;
|
2006-10-04 05:48:46 +00:00
|
|
|
}
|
|
|
|
|
2006-10-04 05:59:56 +00:00
|
|
|
/* Save feature bits */
|
|
|
|
Prcb->FeatureBits = FeatureBits;
|
|
|
|
|
2006-09-30 07:04:49 +00:00
|
|
|
/* Save CPU state */
|
|
|
|
KiSaveProcessorControlState(&Prcb->ProcessorState);
|
|
|
|
|
2006-09-03 21:06:30 +00:00
|
|
|
/* Get cache line information for this CPU */
|
|
|
|
KiGetCacheInformation();
|
|
|
|
|
|
|
|
/* Initialize spinlocks and DPC data */
|
|
|
|
KiInitSpinLocks(Prcb, Number);
|
|
|
|
|
|
|
|
/* Check if this is the Boot CPU */
|
|
|
|
if (!Number)
|
|
|
|
{
|
|
|
|
/* Set Node Data */
|
|
|
|
KeNodeBlock[0] = &KiNode0;
|
|
|
|
Prcb->ParentNode = KeNodeBlock[0];
|
|
|
|
KeNodeBlock[0]->ProcessorMask = Prcb->SetMember;
|
|
|
|
|
|
|
|
/* Set boot-level flags */
|
|
|
|
KeI386NpxPresent = NpxPresent;
|
|
|
|
KeI386CpuType = Prcb->CpuType;
|
|
|
|
KeI386CpuStep = Prcb->CpuStep;
|
2007-01-18 06:23:14 +00:00
|
|
|
KeProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
|
2006-09-03 21:06:30 +00:00
|
|
|
KeProcessorLevel = (USHORT)Prcb->CpuType;
|
|
|
|
if (Prcb->CpuID) KeProcessorRevision = Prcb->CpuStep;
|
|
|
|
KeFeatureBits = FeatureBits;
|
|
|
|
KeI386FxsrPresent = (KeFeatureBits & KF_FXSR) ? TRUE : FALSE;
|
|
|
|
KeI386XMMIPresent = (KeFeatureBits & KF_XMMI) ? TRUE : FALSE;
|
|
|
|
|
2007-01-18 06:23:14 +00:00
|
|
|
/* Detect 8-byte compare exchange support */
|
|
|
|
if (!(KeFeatureBits & KF_CMPXCHG8B))
|
|
|
|
{
|
|
|
|
/* Copy the vendor string */
|
|
|
|
RtlCopyMemory(Vendor, Prcb->VendorString, sizeof(Vendor));
|
|
|
|
|
|
|
|
/* Bugcheck the system. Windows *requires* this */
|
2011-09-24 18:13:57 +00:00
|
|
|
KeBugCheckEx(UNSUPPORTED_PROCESSOR,
|
2007-01-18 06:23:14 +00:00
|
|
|
(1 << 24 ) | (Prcb->CpuType << 16) | Prcb->CpuStep,
|
|
|
|
Vendor[0],
|
|
|
|
Vendor[1],
|
|
|
|
Vendor[2]);
|
|
|
|
}
|
|
|
|
|
2006-09-03 21:06:30 +00:00
|
|
|
/* Set the current MP Master KPRCB to the Boot PRCB */
|
|
|
|
Prcb->MultiThreadSetMaster = Prcb;
|
|
|
|
|
2006-09-16 20:37:49 +00:00
|
|
|
/* Lower to APC_LEVEL */
|
2007-01-18 06:23:14 +00:00
|
|
|
KeLowerIrql(APC_LEVEL);
|
2006-09-16 20:37:49 +00:00
|
|
|
|
2006-09-03 21:06:30 +00:00
|
|
|
/* Initialize some spinlocks */
|
|
|
|
KeInitializeSpinLock(&KiFreezeExecutionLock);
|
|
|
|
KeInitializeSpinLock(&Ki486CompatibilityLock);
|
|
|
|
|
|
|
|
/* Initialize portable parts of the OS */
|
|
|
|
KiInitSystem();
|
|
|
|
|
|
|
|
/* Initialize the Idle Process and the Process Listhead */
|
|
|
|
InitializeListHead(&KiProcessListHead);
|
2008-08-11 08:40:52 +00:00
|
|
|
PageDirectory[0] = 0;
|
|
|
|
PageDirectory[1] = 0;
|
2006-09-03 21:06:30 +00:00
|
|
|
KeInitializeProcess(InitProcess,
|
|
|
|
0,
|
|
|
|
0xFFFFFFFF,
|
2008-08-11 08:40:52 +00:00
|
|
|
PageDirectory,
|
2006-09-10 23:17:22 +00:00
|
|
|
FALSE);
|
2006-09-03 21:06:30 +00:00
|
|
|
InitProcess->QuantumReset = MAXCHAR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* FIXME */
|
|
|
|
DPRINT1("SMP Boot support not yet present\n");
|
|
|
|
}
|
|
|
|
|
2006-10-05 01:44:54 +00:00
|
|
|
/* Setup the Idle Thread */
|
|
|
|
KeInitializeThread(InitProcess,
|
|
|
|
InitThread,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
IdleStack);
|
|
|
|
InitThread->NextProcessor = Number;
|
|
|
|
InitThread->Priority = HIGH_PRIORITY;
|
|
|
|
InitThread->State = Running;
|
|
|
|
InitThread->Affinity = 1 << Number;
|
|
|
|
InitThread->WaitIrql = DISPATCH_LEVEL;
|
|
|
|
InitProcess->ActiveProcessors = 1 << Number;
|
|
|
|
|
|
|
|
/* HACK for MmUpdatePageDir */
|
|
|
|
((PETHREAD)InitThread)->ThreadsProcess = (PEPROCESS)InitProcess;
|
|
|
|
|
2006-10-04 16:00:36 +00:00
|
|
|
/* Set basic CPU Features that user mode can read */
|
|
|
|
SharedUserData->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] =
|
2006-10-26 01:49:51 +00:00
|
|
|
(KeFeatureBits & KF_MMX) ? TRUE: FALSE;
|
2006-10-04 16:00:36 +00:00
|
|
|
SharedUserData->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE] =
|
2006-10-26 01:49:51 +00:00
|
|
|
(KeFeatureBits & KF_CMPXCHG8B) ? TRUE: FALSE;
|
2006-10-04 16:00:36 +00:00
|
|
|
SharedUserData->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] =
|
2006-10-26 01:49:51 +00:00
|
|
|
((KeFeatureBits & KF_FXSR) && (KeFeatureBits & KF_XMMI)) ? TRUE: FALSE;
|
2006-10-04 16:00:36 +00:00
|
|
|
SharedUserData->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] =
|
2006-10-26 01:49:51 +00:00
|
|
|
((KeFeatureBits & KF_FXSR) && (KeFeatureBits & KF_XMMI64)) ? TRUE: FALSE;
|
2006-10-04 16:00:36 +00:00
|
|
|
SharedUserData->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] =
|
2006-10-26 01:49:51 +00:00
|
|
|
(KeFeatureBits & KF_3DNOW) ? TRUE: FALSE;
|
2006-10-04 16:00:36 +00:00
|
|
|
SharedUserData->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE] =
|
2006-10-26 01:49:51 +00:00
|
|
|
(KeFeatureBits & KF_RDTSC) ? TRUE: FALSE;
|
2006-10-04 16:00:36 +00:00
|
|
|
|
2006-09-03 21:06:30 +00:00
|
|
|
/* Set up the thread-related fields in the PRCB */
|
2006-09-16 20:37:49 +00:00
|
|
|
Prcb->CurrentThread = InitThread;
|
2006-09-03 21:06:30 +00:00
|
|
|
Prcb->NextThread = NULL;
|
2006-09-16 20:37:49 +00:00
|
|
|
Prcb->IdleThread = InitThread;
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Initialize the Kernel Executive */
|
2006-10-01 18:01:38 +00:00
|
|
|
ExpInitializeExecutive(Number, LoaderBlock);
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Only do this on the boot CPU */
|
|
|
|
if (!Number)
|
|
|
|
{
|
|
|
|
/* Calculate the time reciprocal */
|
|
|
|
KiTimeIncrementReciprocal =
|
|
|
|
KiComputeReciprocal(KeMaximumIncrement,
|
|
|
|
&KiTimeIncrementShiftCount);
|
|
|
|
|
|
|
|
/* Update DPC Values in case they got updated by the executive */
|
|
|
|
Prcb->MaximumDpcQueueDepth = KiMaximumDpcQueueDepth;
|
|
|
|
Prcb->MinimumDpcRate = KiMinimumDpcRate;
|
|
|
|
Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
|
|
|
|
|
|
|
|
/* Allocate the DPC Stack */
|
2007-09-24 14:01:54 +00:00
|
|
|
DpcStack = MmCreateKernelStack(FALSE, 0);
|
2006-09-03 21:06:30 +00:00
|
|
|
if (!DpcStack) KeBugCheckEx(NO_PAGES_AVAILABLE, 1, 0, 0, 0);
|
2007-09-27 13:07:43 +00:00
|
|
|
Prcb->DpcStack = DpcStack;
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Allocate the IOPM save area. */
|
|
|
|
Ki386IopmSaveArea = ExAllocatePoolWithTag(PagedPool,
|
|
|
|
PAGE_SIZE * 2,
|
2009-08-24 18:19:53 +00:00
|
|
|
' eK');
|
2006-09-03 21:06:30 +00:00
|
|
|
if (!Ki386IopmSaveArea)
|
|
|
|
{
|
|
|
|
/* Bugcheck. We need this for V86/VDM support. */
|
|
|
|
KeBugCheckEx(NO_PAGES_AVAILABLE, 2, PAGE_SIZE * 2, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-16 20:37:49 +00:00
|
|
|
/* Raise to Dispatch */
|
2015-09-26 10:12:53 +00:00
|
|
|
KeRaiseIrql(DISPATCH_LEVEL,
|
|
|
|
&DummyIrql);
|
2006-09-03 21:06:30 +00:00
|
|
|
|
2006-09-16 20:37:49 +00:00
|
|
|
/* Set the Idle Priority to 0. This will jump into Phase 1 */
|
|
|
|
KeSetPriorityThread(InitThread, 0);
|
2006-09-30 07:04:49 +00:00
|
|
|
|
|
|
|
/* If there's no thread scheduled, put this CPU in the Idle summary */
|
2007-01-18 06:23:14 +00:00
|
|
|
KiAcquirePrcbLock(Prcb);
|
2006-09-30 07:04:49 +00:00
|
|
|
if (!Prcb->NextThread) KiIdleSummary |= 1 << Number;
|
2007-01-18 06:23:14 +00:00
|
|
|
KiReleasePrcbLock(Prcb);
|
2006-09-30 07:04:49 +00:00
|
|
|
|
|
|
|
/* Raise back to HIGH_LEVEL and clear the PRCB for the loader block */
|
2015-09-26 10:12:53 +00:00
|
|
|
KeRaiseIrql(HIGH_LEVEL,
|
|
|
|
&DummyIrql);
|
2006-09-30 07:04:49 +00:00
|
|
|
LoaderBlock->Prcb = 0;
|
2006-09-03 21:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 11:19:11 +00:00
|
|
|
INIT_FUNCTION
|
2006-09-30 05:42:22 +00:00
|
|
|
VOID
|
2006-09-30 07:04:49 +00:00
|
|
|
FASTCALL
|
2006-09-30 05:42:22 +00:00
|
|
|
KiGetMachineBootPointers(IN PKGDTENTRY *Gdt,
|
|
|
|
IN PKIDTENTRY *Idt,
|
|
|
|
IN PKIPCR *Pcr,
|
|
|
|
IN PKTSS *Tss)
|
|
|
|
{
|
2009-09-21 15:20:18 +00:00
|
|
|
KDESCRIPTOR GdtDescriptor, IdtDescriptor;
|
2006-09-30 05:42:22 +00:00
|
|
|
KGDTENTRY TssSelector, PcrSelector;
|
2009-09-21 15:20:18 +00:00
|
|
|
USHORT Tr, Fs;
|
2006-09-30 05:42:22 +00:00
|
|
|
|
|
|
|
/* Get GDT and IDT descriptors */
|
2009-09-21 15:20:18 +00:00
|
|
|
Ke386GetGlobalDescriptorTable(&GdtDescriptor.Limit);
|
|
|
|
__sidt(&IdtDescriptor.Limit);
|
2006-09-30 05:42:22 +00:00
|
|
|
|
|
|
|
/* Save IDT and GDT */
|
|
|
|
*Gdt = (PKGDTENTRY)GdtDescriptor.Base;
|
|
|
|
*Idt = (PKIDTENTRY)IdtDescriptor.Base;
|
|
|
|
|
|
|
|
/* Get TSS and FS Selectors */
|
2009-09-27 10:09:38 +00:00
|
|
|
Tr = Ke386GetTr();
|
2006-09-30 05:42:22 +00:00
|
|
|
Fs = Ke386GetFs();
|
|
|
|
|
|
|
|
/* Get PCR Selector, mask it and get its GDT Entry */
|
|
|
|
PcrSelector = *(PKGDTENTRY)((ULONG_PTR)*Gdt + (Fs & ~RPL_MASK));
|
|
|
|
|
|
|
|
/* Get the KPCR itself */
|
|
|
|
*Pcr = (PKIPCR)(ULONG_PTR)(PcrSelector.BaseLow |
|
|
|
|
PcrSelector.HighWord.Bytes.BaseMid << 16 |
|
|
|
|
PcrSelector.HighWord.Bytes.BaseHi << 24);
|
|
|
|
|
|
|
|
/* Get TSS Selector, mask it and get its GDT Entry */
|
|
|
|
TssSelector = *(PKGDTENTRY)((ULONG_PTR)*Gdt + (Tr & ~RPL_MASK));
|
|
|
|
|
|
|
|
/* Get the KTSS itself */
|
|
|
|
*Tss = (PKTSS)(ULONG_PTR)(TssSelector.BaseLow |
|
|
|
|
TssSelector.HighWord.Bytes.BaseMid << 16 |
|
|
|
|
TssSelector.HighWord.Bytes.BaseHi << 24);
|
|
|
|
}
|
|
|
|
|
2018-12-30 11:19:11 +00:00
|
|
|
INIT_FUNCTION
|
2006-09-03 21:06:30 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2010-01-19 18:27:24 +00:00
|
|
|
KiSystemStartupBootStack(VOID)
|
|
|
|
{
|
|
|
|
PKTHREAD Thread;
|
2011-09-25 09:12:26 +00:00
|
|
|
|
2010-01-19 18:27:24 +00:00
|
|
|
/* Initialize the kernel for the current CPU */
|
|
|
|
KiInitializeKernel(&KiInitialProcess.Pcb,
|
|
|
|
(PKTHREAD)KeLoaderBlock->Thread,
|
|
|
|
(PVOID)(KeLoaderBlock->KernelStack & ~3),
|
|
|
|
(PKPRCB)__readfsdword(KPCR_PRCB),
|
|
|
|
KeNumberProcessors - 1,
|
|
|
|
KeLoaderBlock);
|
2011-09-25 09:12:26 +00:00
|
|
|
|
2010-01-19 18:27:24 +00:00
|
|
|
/* Set the priority of this thread to 0 */
|
|
|
|
Thread = KeGetCurrentThread();
|
|
|
|
Thread->Priority = 0;
|
2011-09-25 09:12:26 +00:00
|
|
|
|
2010-01-19 18:27:24 +00:00
|
|
|
/* Force interrupts enabled and lower IRQL back to DISPATCH_LEVEL */
|
|
|
|
_enable();
|
2015-09-26 10:12:53 +00:00
|
|
|
KeLowerIrql(DISPATCH_LEVEL);
|
2011-09-25 09:12:26 +00:00
|
|
|
|
2010-01-19 18:27:24 +00:00
|
|
|
/* Set the right wait IRQL */
|
|
|
|
Thread->WaitIrql = DISPATCH_LEVEL;
|
|
|
|
|
|
|
|
/* Jump into the idle loop */
|
|
|
|
KiIdleLoop();
|
|
|
|
}
|
|
|
|
|
2014-09-13 08:24:03 +00:00
|
|
|
static
|
|
|
|
VOID
|
|
|
|
KiMarkPageAsReadOnly(
|
|
|
|
PVOID Address)
|
|
|
|
{
|
|
|
|
PHARDWARE_PTE PointerPte;
|
|
|
|
|
|
|
|
/* Make sure the address is page aligned */
|
|
|
|
ASSERT(ALIGN_DOWN_POINTER_BY(Address, PAGE_SIZE) == Address);
|
|
|
|
|
|
|
|
/* Get the PTE address */
|
|
|
|
PointerPte = ((PHARDWARE_PTE)PTE_BASE) + ((ULONG_PTR)Address / PAGE_SIZE);
|
|
|
|
ASSERT(PointerPte->Valid);
|
|
|
|
ASSERT(PointerPte->Write);
|
|
|
|
|
|
|
|
/* Set as read-only */
|
|
|
|
PointerPte->Write = 0;
|
|
|
|
|
|
|
|
/* Flush the TLB entry */
|
|
|
|
__invlpg(Address);
|
|
|
|
}
|
|
|
|
|
2018-12-30 11:19:11 +00:00
|
|
|
INIT_FUNCTION
|
2010-01-19 18:27:24 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
KiSystemStartup(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
|
2006-09-03 21:06:30 +00:00
|
|
|
{
|
|
|
|
ULONG Cpu;
|
2006-09-30 05:42:22 +00:00
|
|
|
PKTHREAD InitialThread;
|
2006-10-01 06:08:05 +00:00
|
|
|
ULONG InitialStack;
|
2006-09-30 05:42:22 +00:00
|
|
|
PKGDTENTRY Gdt;
|
|
|
|
PKIDTENTRY Idt;
|
2007-10-10 12:39:54 +00:00
|
|
|
KIDTENTRY NmiEntry, DoubleFaultEntry;
|
2006-09-30 05:42:22 +00:00
|
|
|
PKTSS Tss;
|
|
|
|
PKIPCR Pcr;
|
2015-09-26 10:12:53 +00:00
|
|
|
KIRQL DummyIrql;
|
2011-09-25 09:12:26 +00:00
|
|
|
|
2010-01-28 15:51:18 +00:00
|
|
|
/* Boot cycles timestamp */
|
|
|
|
BootCycles = __rdtsc();
|
2011-06-29 20:27:07 +00:00
|
|
|
|
2006-09-03 21:06:30 +00:00
|
|
|
/* Save the loader block and get the current CPU */
|
2006-09-30 03:33:50 +00:00
|
|
|
KeLoaderBlock = LoaderBlock;
|
2006-09-03 21:06:30 +00:00
|
|
|
Cpu = KeNumberProcessors;
|
|
|
|
if (!Cpu)
|
|
|
|
{
|
|
|
|
/* If this is the boot CPU, set FS and the CPU Number*/
|
|
|
|
Ke386SetFs(KGDT_R0_PCR);
|
2006-10-01 06:46:08 +00:00
|
|
|
__writefsdword(KPCR_PROCESSOR_NUMBER, Cpu);
|
2006-09-30 05:42:22 +00:00
|
|
|
|
|
|
|
/* Set the initial stack and idle thread as well */
|
|
|
|
LoaderBlock->KernelStack = (ULONG_PTR)P0BootStack;
|
|
|
|
LoaderBlock->Thread = (ULONG_PTR)&KiInitialThread;
|
2006-09-03 21:06:30 +00:00
|
|
|
}
|
|
|
|
|
2006-09-30 05:42:22 +00:00
|
|
|
/* Save the initial thread and stack */
|
2006-10-01 06:08:05 +00:00
|
|
|
InitialStack = LoaderBlock->KernelStack;
|
2006-09-30 05:42:22 +00:00
|
|
|
InitialThread = (PKTHREAD)LoaderBlock->Thread;
|
|
|
|
|
|
|
|
/* Clean the APC List Head */
|
|
|
|
InitializeListHead(&InitialThread->ApcState.ApcListHead[KernelMode]);
|
|
|
|
|
|
|
|
/* Initialize the machine type */
|
|
|
|
KiInitializeMachineType();
|
|
|
|
|
2006-10-01 15:22:42 +00:00
|
|
|
/* Skip initial setup if this isn't the Boot CPU */
|
|
|
|
if (Cpu) goto AppCpuInit;
|
|
|
|
|
2006-09-30 05:42:22 +00:00
|
|
|
/* Get GDT, IDT, PCR and TSS pointers */
|
|
|
|
KiGetMachineBootPointers(&Gdt, &Idt, &Pcr, &Tss);
|
|
|
|
|
|
|
|
/* Setup the TSS descriptors and entries */
|
2006-10-01 07:31:33 +00:00
|
|
|
Ki386InitializeTss(Tss, Idt, Gdt);
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Initialize the PCR */
|
|
|
|
RtlZeroMemory(Pcr, PAGE_SIZE);
|
|
|
|
KiInitializePcr(Cpu,
|
|
|
|
Pcr,
|
2006-09-30 05:42:22 +00:00
|
|
|
Idt,
|
|
|
|
Gdt,
|
|
|
|
Tss,
|
|
|
|
InitialThread,
|
2010-01-19 18:27:24 +00:00
|
|
|
(PVOID)KiDoubleFaultStack);
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Set us as the current process */
|
2006-09-30 05:42:22 +00:00
|
|
|
InitialThread->ApcState.Process = &KiInitialProcess.Pcb;
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Clear DR6/7 to cleanup bootloader debugging */
|
2006-10-01 06:46:08 +00:00
|
|
|
__writefsdword(KPCR_TEB, 0);
|
2006-09-30 05:42:22 +00:00
|
|
|
__writefsdword(KPCR_DR6, 0);
|
|
|
|
__writefsdword(KPCR_DR7, 0);
|
2006-09-03 21:06:30 +00:00
|
|
|
|
2006-11-29 19:20:17 +00:00
|
|
|
/* Setup the IDT */
|
|
|
|
KeInitExceptions();
|
|
|
|
|
2006-09-03 21:06:30 +00:00
|
|
|
/* Load Ring 3 selectors for DS/ES */
|
|
|
|
Ke386SetDs(KGDT_R3_DATA | RPL_MASK);
|
|
|
|
Ke386SetEs(KGDT_R3_DATA | RPL_MASK);
|
|
|
|
|
2009-08-02 21:27:26 +00:00
|
|
|
/* Save NMI and double fault traps */
|
|
|
|
RtlCopyMemory(&NmiEntry, &Idt[2], sizeof(KIDTENTRY));
|
|
|
|
RtlCopyMemory(&DoubleFaultEntry, &Idt[8], sizeof(KIDTENTRY));
|
|
|
|
|
|
|
|
/* Copy kernel's trap handlers */
|
|
|
|
RtlCopyMemory(Idt,
|
|
|
|
(PVOID)KiIdtDescriptor.Base,
|
|
|
|
KiIdtDescriptor.Limit + 1);
|
|
|
|
|
|
|
|
/* Restore NMI and double fault */
|
|
|
|
RtlCopyMemory(&Idt[2], &NmiEntry, sizeof(KIDTENTRY));
|
|
|
|
RtlCopyMemory(&Idt[8], &DoubleFaultEntry, sizeof(KIDTENTRY));
|
2007-10-10 12:39:54 +00:00
|
|
|
|
2006-09-03 21:06:30 +00:00
|
|
|
AppCpuInit:
|
2006-09-30 06:18:45 +00:00
|
|
|
/* Loop until we can release the freeze lock */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Loop until execution can continue */
|
2007-03-19 21:53:38 +00:00
|
|
|
while (*(volatile PKSPIN_LOCK*)&KiFreezeExecutionLock == (PVOID)1);
|
2006-10-01 14:02:08 +00:00
|
|
|
} while(InterlockedBitTestAndSet((PLONG)&KiFreezeExecutionLock, 0));
|
2006-09-30 06:18:45 +00:00
|
|
|
|
|
|
|
/* Setup CPU-related fields */
|
2006-09-30 05:42:22 +00:00
|
|
|
__writefsdword(KPCR_NUMBER, Cpu);
|
|
|
|
__writefsdword(KPCR_SET_MEMBER, 1 << Cpu);
|
|
|
|
__writefsdword(KPCR_SET_MEMBER_COPY, 1 << Cpu);
|
|
|
|
__writefsdword(KPCR_PRCB_SET_MEMBER, 1 << Cpu);
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Initialize the Processor with HAL */
|
2006-09-30 03:33:50 +00:00
|
|
|
HalInitializeProcessor(Cpu, KeLoaderBlock);
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Set active processors */
|
2006-10-01 15:22:42 +00:00
|
|
|
KeActiveProcessors |= __readfsdword(KPCR_SET_MEMBER);
|
2006-09-03 21:06:30 +00:00
|
|
|
KeNumberProcessors++;
|
|
|
|
|
2006-09-30 06:18:45 +00:00
|
|
|
/* Check if this is the boot CPU */
|
|
|
|
if (!Cpu)
|
|
|
|
{
|
|
|
|
/* Initialize debugging system */
|
2006-10-01 07:24:24 +00:00
|
|
|
KdInitSystem(0, KeLoaderBlock);
|
2006-09-03 21:06:30 +00:00
|
|
|
|
2006-09-30 06:18:45 +00:00
|
|
|
/* Check for break-in */
|
2009-10-18 11:50:34 +00:00
|
|
|
if (KdPollBreakIn()) DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C);
|
2014-09-13 08:24:03 +00:00
|
|
|
|
|
|
|
/* Make the lowest page of the boot and double fault stack read-only */
|
|
|
|
KiMarkPageAsReadOnly(P0BootStackData);
|
|
|
|
KiMarkPageAsReadOnly(KiDoubleFaultStackData);
|
2006-09-30 06:18:45 +00:00
|
|
|
}
|
2006-09-03 21:06:30 +00:00
|
|
|
|
|
|
|
/* Raise to HIGH_LEVEL */
|
2015-09-26 10:12:53 +00:00
|
|
|
KeRaiseIrql(HIGH_LEVEL,
|
|
|
|
&DummyIrql);
|
2006-09-03 21:06:30 +00:00
|
|
|
|
2007-08-06 23:55:35 +00:00
|
|
|
/* Switch to new kernel stack and start kernel bootstrapping */
|
2010-01-19 18:27:24 +00:00
|
|
|
KiSwitchToBootStack(InitialStack & ~3);
|
2010-01-19 21:19:56 +00:00
|
|
|
}
|