2011-09-15 19:32:13 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2015-10-04 11:54:25 +00:00
|
|
|
* FILE: ntoskrnl/ke/amd64/krnlinit.c
|
2011-09-15 19:32:13 +00:00
|
|
|
* PURPOSE: Portable part of kernel initialization
|
|
|
|
* PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
|
|
|
|
* Alex Ionescu (alex.ionescu@reactos.org)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
|
|
|
#include <ntoskrnl.h>
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
extern ULONG_PTR MainSSDT[];
|
|
|
|
extern UCHAR MainSSPT[];
|
|
|
|
|
2017-11-09 22:23:55 +00:00
|
|
|
extern BOOLEAN RtlpUse16ByteSLists;
|
|
|
|
|
2011-09-15 19:32:13 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
2020-10-06 19:44:01 +00:00
|
|
|
CODE_SEG("INIT")
|
2011-09-15 19:32:13 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
KiInitializeKernel(IN PKPROCESS InitProcess,
|
|
|
|
IN PKTHREAD InitThread,
|
|
|
|
IN PVOID IdleStack,
|
|
|
|
IN PKPRCB Prcb,
|
|
|
|
IN PLOADER_PARAMETER_BLOCK LoaderBlock);
|
|
|
|
|
|
|
|
|
2023-07-12 12:28:48 +00:00
|
|
|
CODE_SEG("INIT")
|
|
|
|
VOID
|
|
|
|
KiCalculateCpuFrequency(
|
|
|
|
IN PKPRCB Prcb)
|
|
|
|
{
|
|
|
|
if (Prcb->FeatureBits & KF_RDTSC)
|
|
|
|
{
|
|
|
|
ULONG Sample = 0;
|
|
|
|
CPU_INFO CpuInfo;
|
|
|
|
KI_SAMPLE_MAP Samples[10];
|
|
|
|
PKI_SAMPLE_MAP CurrentSample = Samples;
|
|
|
|
|
|
|
|
/* Start sampling loop */
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Do a dummy CPUID to start the sample */
|
|
|
|
KiCpuId(&CpuInfo, 0);
|
|
|
|
|
|
|
|
/* Fill out the starting data */
|
|
|
|
CurrentSample->PerfStart = KeQueryPerformanceCounter(NULL);
|
|
|
|
CurrentSample->TSCStart = __rdtsc();
|
|
|
|
CurrentSample->PerfFreq.QuadPart = -50000;
|
|
|
|
|
|
|
|
/* Sleep for this sample */
|
|
|
|
KeStallExecutionProcessor(CurrentSample->PerfFreq.QuadPart * -1 / 10);
|
|
|
|
|
|
|
|
/* Do another dummy CPUID */
|
|
|
|
KiCpuId(&CpuInfo, 0);
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
CurrentSample->MHz = (ULONG)((CurrentSample->TSCDelta *
|
|
|
|
CurrentSample->
|
|
|
|
PerfFreq.QuadPart + 500000) /
|
|
|
|
(CurrentSample->PerfDelta *
|
|
|
|
1000000));
|
|
|
|
|
|
|
|
/* 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++;
|
|
|
|
|
|
|
|
if (Sample == RTL_NUMBER_OF(Samples))
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the CPU Speed */
|
|
|
|
Prcb->MHz = CurrentSample[-1].MHz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-15 19:32:13 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2013-04-04 20:11:17 +00:00
|
|
|
KiInitializeHandBuiltThread(
|
2011-09-15 19:32:13 +00:00
|
|
|
IN PKTHREAD Thread,
|
|
|
|
IN PKPROCESS Process,
|
|
|
|
IN PVOID Stack)
|
|
|
|
{
|
|
|
|
PKPRCB Prcb = KeGetCurrentPrcb();
|
|
|
|
|
|
|
|
/* Setup the Thread */
|
|
|
|
KeInitializeThread(Process, Thread, NULL, NULL, NULL, NULL, NULL, Stack);
|
|
|
|
|
|
|
|
Thread->NextProcessor = Prcb->Number;
|
|
|
|
Thread->Priority = HIGH_PRIORITY;
|
|
|
|
Thread->State = Running;
|
|
|
|
Thread->Affinity = (ULONG_PTR)1 << Prcb->Number;
|
|
|
|
Thread->WaitIrql = DISPATCH_LEVEL;
|
|
|
|
Process->ActiveProcessors |= (ULONG_PTR)1 << Prcb->Number;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-06 19:44:01 +00:00
|
|
|
CODE_SEG("INIT")
|
2020-09-09 01:46:56 +00:00
|
|
|
DECLSPEC_NORETURN
|
2011-09-15 19:32:13 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
KiSystemStartupBootStack(VOID)
|
|
|
|
{
|
|
|
|
PLOADER_PARAMETER_BLOCK LoaderBlock = KeLoaderBlock; // hack
|
|
|
|
PKPRCB Prcb = KeGetCurrentPrcb();
|
|
|
|
PKTHREAD Thread = (PKTHREAD)KeLoaderBlock->Thread;
|
|
|
|
PKPROCESS Process = Thread->ApcState.Process;
|
|
|
|
PVOID KernelStack = (PVOID)KeLoaderBlock->KernelStack;
|
|
|
|
|
|
|
|
/* Initialize the Power Management Support for this PRCB */
|
|
|
|
PoInitializePrcb(Prcb);
|
|
|
|
|
|
|
|
/* Save CPU state */
|
|
|
|
KiSaveProcessorControlState(&Prcb->ProcessorState);
|
|
|
|
|
|
|
|
/* Get cache line information for this CPU */
|
|
|
|
KiGetCacheInformation();
|
|
|
|
|
|
|
|
/* Initialize spinlocks and DPC data */
|
|
|
|
KiInitSpinLocks(Prcb, Prcb->Number);
|
|
|
|
|
|
|
|
/* Set up the thread-related fields in the PRCB */
|
|
|
|
Prcb->CurrentThread = Thread;
|
|
|
|
Prcb->NextThread = NULL;
|
|
|
|
Prcb->IdleThread = Thread;
|
|
|
|
|
|
|
|
/* Initialize PRCB pool lookaside pointers */
|
|
|
|
ExInitPoolLookasidePointers();
|
|
|
|
|
2011-09-16 09:46:08 +00:00
|
|
|
/* Lower to APC_LEVEL */
|
|
|
|
KeLowerIrql(APC_LEVEL);
|
|
|
|
|
2011-09-15 19:32:13 +00:00
|
|
|
/* Check if this is the boot cpu */
|
|
|
|
if (Prcb->Number == 0)
|
|
|
|
{
|
|
|
|
/* Initialize the kernel */
|
|
|
|
KiInitializeKernel(Process, Thread, KernelStack, Prcb, LoaderBlock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Initialize the startup thread */
|
2013-04-04 20:11:17 +00:00
|
|
|
KiInitializeHandBuiltThread(Thread, Process, KernelStack);
|
2011-09-15 19:32:13 +00:00
|
|
|
|
|
|
|
/* Initialize cpu with HAL */
|
|
|
|
if (!HalInitSystem(0, LoaderBlock))
|
|
|
|
{
|
|
|
|
/* Initialization failed */
|
|
|
|
KeBugCheck(HAL_INITIALIZATION_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 12:28:48 +00:00
|
|
|
/* Calculate the CPU frequency */
|
|
|
|
KiCalculateCpuFrequency(Prcb);
|
|
|
|
|
2011-09-15 19:32:13 +00:00
|
|
|
/* Raise to Dispatch */
|
|
|
|
KfRaiseIrql(DISPATCH_LEVEL);
|
|
|
|
|
|
|
|
/* Set the Idle Priority to 0. This will jump into Phase 1 */
|
|
|
|
KeSetPriorityThread(Thread, 0);
|
|
|
|
|
|
|
|
/* If there's no thread scheduled, put this CPU in the Idle summary */
|
|
|
|
KiAcquirePrcbLock(Prcb);
|
|
|
|
if (!Prcb->NextThread) KiIdleSummary |= (ULONG_PTR)1 << Prcb->Number;
|
|
|
|
KiReleasePrcbLock(Prcb);
|
|
|
|
|
|
|
|
/* Raise back to HIGH_LEVEL and clear the PRCB for the loader block */
|
|
|
|
KfRaiseIrql(HIGH_LEVEL);
|
|
|
|
LoaderBlock->Prcb = 0;
|
|
|
|
|
|
|
|
/* Set the priority of this thread to 0 */
|
|
|
|
Thread = KeGetCurrentThread();
|
|
|
|
Thread->Priority = 0;
|
|
|
|
|
|
|
|
/* Force interrupts enabled and lower IRQL back to DISPATCH_LEVEL */
|
|
|
|
_enable();
|
|
|
|
KeLowerIrql(DISPATCH_LEVEL);
|
|
|
|
|
|
|
|
/* Set the right wait IRQL */
|
|
|
|
Thread->WaitIrql = DISPATCH_LEVEL;
|
|
|
|
|
|
|
|
/* Jump into the idle loop */
|
|
|
|
KiIdleLoop();
|
|
|
|
}
|
|
|
|
|
2020-10-06 19:44:01 +00:00
|
|
|
CODE_SEG("INIT")
|
2011-09-15 19:32:13 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
KiInitializeKernel(IN PKPROCESS InitProcess,
|
|
|
|
IN PKTHREAD InitThread,
|
|
|
|
IN PVOID IdleStack,
|
|
|
|
IN PKPRCB Prcb,
|
|
|
|
IN PLOADER_PARAMETER_BLOCK LoaderBlock)
|
|
|
|
{
|
[HAL/NDK]
- Make Vector parameter in HalEnableSystemInterrupt, HalDisableSystemInterrupt and HalBeginSystemInterrupt an ULONG, not an UCHAR
[NDK]
- 64bit fixes for HANDLE_TABLE, KPROCESS, SECTION_IMAGE_INFORMATION, MMADDRESS_LIST, MMVAD_FLAGS, MMVAD, MMVAD_LONG, MMVAD_SHORT, MEMORY_DESCRIPTOR, MEMORY_ALLOCATION_DESCRIPTOR, LdrVerifyMappedImageMatchesChecksum
- KDPC_DATA::DpcQueueDepth is signed on amd64, unsigned on x86
[NTOSKRNL]
- Fix hundreds of MSVC and amd64 warnings
- add a pragma message to FstubFixupEfiPartition, since it looks broken
- Move portable Ke constants from <arch>/cpu.c to krnlinit.c
- Fixed a bug in amd64 KiGeneralProtectionFaultHandler
svn path=/trunk/; revision=53734
2011-09-18 13:11:45 +00:00
|
|
|
ULONG_PTR PageDirectory[2];
|
2011-09-15 19:32:13 +00:00
|
|
|
PVOID DpcStack;
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
/* Set Node Data */
|
|
|
|
KeNodeBlock[0] = &KiNode0;
|
|
|
|
Prcb->ParentNode = KeNodeBlock[0];
|
|
|
|
KeNodeBlock[0]->ProcessorMask = Prcb->SetMember;
|
|
|
|
|
|
|
|
/* Set boot-level flags */
|
|
|
|
KeFeatureBits = Prcb->FeatureBits;
|
|
|
|
|
2017-11-09 22:23:55 +00:00
|
|
|
/* Initialize 8/16 bit SList support */
|
|
|
|
RtlpUse16ByteSLists = (KeFeatureBits & KF_CMPXCHG16B) ? TRUE : FALSE;
|
|
|
|
|
2011-09-15 19:32:13 +00:00
|
|
|
/* Set the current MP Master KPRCB to the Boot PRCB */
|
|
|
|
Prcb->MultiThreadSetMaster = Prcb;
|
|
|
|
|
|
|
|
/* Initialize Bugcheck Callback data */
|
|
|
|
InitializeListHead(&KeBugcheckCallbackListHead);
|
|
|
|
InitializeListHead(&KeBugcheckReasonCallbackListHead);
|
|
|
|
KeInitializeSpinLock(&BugCheckCallbackLock);
|
|
|
|
|
|
|
|
/* Initialize the Timer Expiration DPC */
|
|
|
|
KeInitializeDpc(&KiTimerExpireDpc, KiTimerExpiration, NULL);
|
|
|
|
KeSetTargetProcessorDpc(&KiTimerExpireDpc, 0);
|
|
|
|
|
|
|
|
/* Initialize Profiling data */
|
|
|
|
KeInitializeSpinLock(&KiProfileLock);
|
|
|
|
InitializeListHead(&KiProfileListHead);
|
|
|
|
InitializeListHead(&KiProfileSourceListHead);
|
|
|
|
|
|
|
|
/* Loop the timer table */
|
|
|
|
for (i = 0; i < TIMER_TABLE_SIZE; i++)
|
|
|
|
{
|
|
|
|
/* Initialize the list and entries */
|
|
|
|
InitializeListHead(&KiTimerTableListHead[i].Entry);
|
|
|
|
KiTimerTableListHead[i].Time.HighPart = 0xFFFFFFFF;
|
|
|
|
KiTimerTableListHead[i].Time.LowPart = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the Swap event and all swap lists */
|
|
|
|
KeInitializeEvent(&KiSwapEvent, SynchronizationEvent, FALSE);
|
|
|
|
InitializeListHead(&KiProcessInSwapListHead);
|
|
|
|
InitializeListHead(&KiProcessOutSwapListHead);
|
|
|
|
InitializeListHead(&KiStackInSwapListHead);
|
|
|
|
|
|
|
|
/* Initialize the mutex for generic DPC calls */
|
|
|
|
ExInitializeFastMutex(&KiGenericCallDpcMutex);
|
|
|
|
|
|
|
|
/* Initialize the syscall table */
|
|
|
|
KeServiceDescriptorTable[0].Base = MainSSDT;
|
|
|
|
KeServiceDescriptorTable[0].Count = NULL;
|
|
|
|
KeServiceDescriptorTable[0].Limit = KiServiceLimit;
|
|
|
|
KeServiceDescriptorTable[1].Limit = 0;
|
|
|
|
KeServiceDescriptorTable[0].Number = MainSSPT;
|
|
|
|
|
|
|
|
/* Copy the the current table into the shadow table for win32k */
|
|
|
|
RtlCopyMemory(KeServiceDescriptorTableShadow,
|
|
|
|
KeServiceDescriptorTable,
|
|
|
|
sizeof(KeServiceDescriptorTable));
|
|
|
|
|
|
|
|
/* Initialize the Idle Process and the Process Listhead */
|
|
|
|
InitializeListHead(&KiProcessListHead);
|
|
|
|
PageDirectory[0] = 0;
|
|
|
|
PageDirectory[1] = 0;
|
|
|
|
KeInitializeProcess(InitProcess,
|
|
|
|
0,
|
|
|
|
0xFFFFFFFF,
|
|
|
|
PageDirectory,
|
|
|
|
FALSE);
|
|
|
|
InitProcess->QuantumReset = MAXCHAR;
|
|
|
|
|
|
|
|
/* Initialize the startup thread */
|
2013-04-04 20:11:17 +00:00
|
|
|
KiInitializeHandBuiltThread(InitThread, InitProcess, IdleStack);
|
2011-09-15 19:32:13 +00:00
|
|
|
|
|
|
|
/* Initialize the Kernel Executive */
|
|
|
|
ExpInitializeExecutive(0, LoaderBlock);
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
DpcStack = MmCreateKernelStack(FALSE, 0);
|
|
|
|
if (!DpcStack) KeBugCheckEx(NO_PAGES_AVAILABLE, 1, 0, 0, 0);
|
|
|
|
Prcb->DpcStack = DpcStack;
|
|
|
|
}
|
|
|
|
|