Author: ion Date: Wed Oct 4 07:37:11 2006 New Revision: 24385
URL: http://svn.reactos.org/svn/reactos?rev=24385&view=rev Log: - Make Kernel initialization (KeInit2) portable/more complete. KeInit2 becomes KeInitSystem, the portable part, with a call to KiInitMachineDependent, the arch-specific part. - Do all CPU configuration through IPIs and synchronize with potential multiple CPUs. - Add mtrr.c and patpge.c for future support of MTRR, AMD K6 MTRR, PGE and PAT support, with initilization calls in each of them (which only print out a "Sorry" message). - Add some stubbed code for extra initizliation to do later once some detection things are fixed.
Added: trunk/reactos/ntoskrnl/ke/i386/mtrr.c trunk/reactos/ntoskrnl/ke/i386/patpge.c Modified: trunk/reactos/ntoskrnl/ex/init.c trunk/reactos/ntoskrnl/include/internal/ke.h trunk/reactos/ntoskrnl/ke/dpc.c trunk/reactos/ntoskrnl/ke/i386/cpu.c trunk/reactos/ntoskrnl/ke/i386/kiinit.c trunk/reactos/ntoskrnl/ke/krnlinit.c trunk/reactos/ntoskrnl/ntoskrnl.rbuild
Modified: trunk/reactos/ntoskrnl/ex/init.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ex/init.c?rev=2438... ============================================================================== --- trunk/reactos/ntoskrnl/ex/init.c (original) +++ trunk/reactos/ntoskrnl/ex/init.c Wed Oct 4 07:37:11 2006 @@ -736,8 +736,8 @@ /* Set us at maximum priority */ KeSetPriorityThread(KeGetCurrentThread(), HIGH_PRIORITY);
- /* Initialize the second stage of the kernel */ - KeInit2(); + /* Initialize the later stages of the kernel */ + KeInitSystem();
/* Initialize all processors */ HalAllProcessorsStarted();
Modified: trunk/reactos/ntoskrnl/include/internal/ke.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/include/internal/k... ============================================================================== --- trunk/reactos/ntoskrnl/include/internal/ke.h (original) +++ trunk/reactos/ntoskrnl/include/internal/ke.h Wed Oct 4 07:37:11 2006 @@ -116,6 +116,7 @@ extern ULONG KiMinimumDpcRate; extern ULONG KiAdjustDpcThreshold; extern ULONG KiIdealDpcRate; +extern BOOLEAN KeThreadDpcEnable; extern LARGE_INTEGER KiTimeIncrementReciprocal; extern UCHAR KiTimeIncrementShiftCount; extern LIST_ENTRY BugcheckCallbackListHead, BugcheckReasonCallbackListHead; @@ -135,6 +136,7 @@ extern ULONG KiIdleSummary; extern VOID KiTrap8(VOID); extern VOID KiTrap2(VOID); +extern VOID KiFastCallEntry(VOID); extern PVOID KeUserApcDispatcher; extern PVOID KeUserCallbackDispatcher; extern PVOID KeUserExceptionDispatcher; @@ -550,10 +552,6 @@ KPRIORITY Increment );
-PULONG -NTAPI -KeGetStackTopThread(struct _ETHREAD* Thread); - VOID NTAPI KeContextToTrapFrame( @@ -638,6 +636,10 @@
VOID NTAPI +KeInitSystem(VOID); + +VOID +NTAPI KeInitExceptions(VOID);
VOID @@ -646,33 +648,17 @@
VOID NTAPI -KeInitTimer(VOID); - -VOID -NTAPI -KeInitDispatcher(VOID); +KiInitializeBugCheck(VOID);
VOID NTAPI KiInitializeSystemClock(VOID); - -VOID -NTAPI -KiInitializeBugCheck(VOID); - -VOID -NTAPI -Phase1Initialization(PVOID Context);
VOID NTAPI KiSystemStartup( IN PLOADER_PARAMETER_BLOCK LoaderBlock ); - -VOID -NTAPI -KeInit2(VOID);
BOOLEAN NTAPI @@ -709,33 +695,6 @@ IN PKEXCEPTION_FRAME ExceptionFrame, IN OUT PCONTEXT Context ); - -VOID -NTAPI -KeApplicationProcessorInit(VOID); - -VOID -NTAPI -KePrepareForApplicationProcessorInit(ULONG id); - -ULONG -NTAPI -KiUserTrapHandler( - PKTRAP_FRAME Tf, - ULONG ExceptionNr, - PVOID Cr2 -); - -VOID -NTAPI -KePushAndStackSwitchAndSysRet( - ULONG Push, - PVOID NewStack -); - -VOID -NTAPI -KeStackSwitchAndRet(PVOID NewStack);
VOID NTAPI @@ -813,7 +772,6 @@ VOID );
- VOID NTAPI KiInitializeMachineType( @@ -866,6 +824,48 @@ VOID );
+ULONG_PTR +NTAPI +Ki386EnableGlobalPage(IN volatile ULONG_PTR Context); + +VOID +NTAPI +KiInitializePAT(VOID); + +VOID +NTAPI +KiInitializeMTRR(IN BOOLEAN FinalCpu); + +VOID +NTAPI +KiAmdK6InitializeMTRR(VOID); + +VOID +NTAPI +KiRestoreFastSyscallReturnState(VOID); + +ULONG_PTR +NTAPI +Ki386EnableDE(IN ULONG_PTR Context); + +ULONG_PTR +NTAPI +Ki386EnableFxsr(IN ULONG_PTR Context); + +ULONG_PTR +NTAPI +Ki386EnableXMMIExceptions(IN ULONG_PTR Context); + +VOID +NTAPI +KiInitMachineDependent(VOID); + +VOID +WRMSR( + IN ULONG Register, + IN LONGLONG Value +); + #include "ke_x.h"
#endif /* __NTOSKRNL_INCLUDE_INTERNAL_KE_H */
Modified: trunk/reactos/ntoskrnl/ke/dpc.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ke/dpc.c?rev=24385... ============================================================================== --- trunk/reactos/ntoskrnl/ke/dpc.c (original) +++ trunk/reactos/ntoskrnl/ke/dpc.c Wed Oct 4 07:37:11 2006 @@ -22,6 +22,7 @@ ULONG KiMinimumDpcRate = 3; ULONG KiAdjustDpcThreshold = 20; ULONG KiIdealDpcRate = 20; +BOOLEAN KeThreadDpcEnable; KMUTEX KiGenericCallDpcMutex;
/* PRIVATE FUNCTIONS *********************************************************/
Modified: trunk/reactos/ntoskrnl/ke/i386/cpu.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ke/i386/cpu.c?rev=... ============================================================================== --- trunk/reactos/ntoskrnl/ke/i386/cpu.c (original) +++ trunk/reactos/ntoskrnl/ke/i386/cpu.c Wed Oct 4 07:37:11 2006 @@ -63,7 +63,6 @@ ULONG KeI386FxsrPresent = 0; ULONG KeI386MachineType; ULONG Ke386Pae = FALSE; -ULONG Ke386GlobalPagesEnabled = FALSE; ULONG Ke386NoExecute = FALSE; BOOLEAN KiI386PentiumLockErrataPresent; ULONG KeLargestCacheLine = 0x40; @@ -760,6 +759,60 @@ KeI386MachineType = KeLoaderBlock->u.I386.MachineType & 0x000FF; }
+ULONG_PTR +NTAPI +KiLoadFastSyscallMachineSpecificRegisters(IN ULONG_PTR Context) +{ + /* Set CS and ESP */ + Ke386Wrmsr(0x174, KGDT_R0_CODE, 0); + Ke386Wrmsr(0x175, 0, 0); + + /* Set LSTAR */ + Ke386Wrmsr(0x176, KiFastCallEntry, 0); + return 0; +} + +VOID +NTAPI +KiRestoreFastSyscallReturnState(VOID) +{ + /* FIXME: NT has support for SYSCALL, IA64-SYSENTER, etc. */ + + /* Check if the CPU Supports fast system call */ + if (KeFeatureBits & KF_FAST_SYSCALL) + { + /* Do an IPI to enable it */ + KeIpiGenericCall(KiLoadFastSyscallMachineSpecificRegisters, 0); + } +} + +ULONG_PTR +NTAPI +Ki386EnableDE(IN ULONG_PTR Context) +{ + /* Enable DE */ + Ke386SetCr4(Ke386GetCr4() | CR4_DE); + return 0; +} + +ULONG_PTR +NTAPI +Ki386EnableFxsr(IN ULONG_PTR Context) +{ + /* Enable FXSR */ + Ke386SetCr4(Ke386GetCr4() | CR4_FXSR); + return 0; +} + +ULONG_PTR +NTAPI +Ki386EnableXMMIExceptions(IN ULONG_PTR Context) +{ + /* FIXME: Support this */ + DPRINT1("Your machine supports XMMI exceptions but ReactOS doesn't\n"); + return 0; +} + /* PUBLIC FUNCTIONS **********************************************************/
/*
Modified: trunk/reactos/ntoskrnl/ke/i386/kiinit.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ke/i386/kiinit.c?r... ============================================================================== --- trunk/reactos/ntoskrnl/ke/i386/kiinit.c (original) +++ trunk/reactos/ntoskrnl/ke/i386/kiinit.c Wed Oct 4 07:37:11 2006 @@ -19,6 +19,137 @@ KSPIN_LOCK Ki486CompatibilityLock;
/* FUNCTIONS *****************************************************************/ + +VOID +NTAPI +KiInitMachineDependent(VOID) +{ + ULONG Protect; + ULONG CpuCount; + BOOLEAN FbCaching = FALSE; + NTSTATUS Status; + //ULONG ReturnLength; + ULONG i, Affinity; + + /* Check for large page support */ + if (KeFeatureBits & KF_LARGE_PAGE) + { + /* FIXME: Support this */ + DPRINT1("Your machine supports PGE but ReactOS doesn't yet.\n"); + } + + /* 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)) + { + /* FIXME: ROS HAL Doesn't initialize this! */ +#if 1 + Status = STATUS_UNSUCCESSFUL; +#else + /* Query the HAL to make sure we can use it */ + Status = HalQuerySystemInformation(HalFrameBufferCachingInformation, + sizeof(BOOLEAN), + &FbCaching, + &ReturnLength); +#endif + 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(); + + /* 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 */ + Protect = MmGetPageProtect(NULL, RtlPrefetchMemoryNonTemporal); + MmSetPageProtect(NULL, + RtlPrefetchMemoryNonTemporal, + Protect | PAGE_IS_WRITABLE); + *(PCHAR)RtlPrefetchMemoryNonTemporal = 0x90; + MmSetPageProtect(NULL, RtlPrefetchMemoryNonTemporal, Protect); + } + } + + /* 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 (;;) + { + // + // FIXME: TODO + // + break; + } + } + + /* Check if we have MTRR without PAT */ + if (!(KeFeatureBits & KF_PAT) && (KeFeatureBits & KF_MTRR)) + { + /* Then manually initialize MTRR for the CPU */ + KiInitializeMTRR((BOOLEAN)i); + } + + /* Check if we have AMD MTRR and initialize it for the CPU */ + if (KeFeatureBits & KF_AMDK6MTRR) KiAmdK6InitializeMTRR(); + + /* FIXME: Apply P5 LOCK Errata fixups */ + } + } + + /* Return affinity back to where it was */ + KeRevertToUserAffinityThread(); +}
VOID NTAPI
Added: trunk/reactos/ntoskrnl/ke/i386/mtrr.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ke/i386/mtrr.c?rev... ============================================================================== --- trunk/reactos/ntoskrnl/ke/i386/mtrr.c (added) +++ trunk/reactos/ntoskrnl/ke/i386/mtrr.c Wed Oct 4 07:37:11 2006 @@ -1,0 +1,33 @@ +/* +* PROJECT: ReactOS Kernel +* LICENSE: GPL - See COPYING in the top level directory +* FILE: ntoskrnl/ke/i386/mtrr.c +* PURPOSE: Support for MTRR and AMD K6 MTRR +* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) +*/ + +/* INCLUDES ******************************************************************/ + +#include <ntoskrnl.h> +#define NDEBUG +#include <debug.h> + +/* GLOBALS *******************************************************************/ + +/* FUNCTIONS *****************************************************************/ + +VOID +NTAPI +KiInitializeMTRR(IN BOOLEAN FinalCpu) +{ + /* FIXME: Support this */ + DPRINT1("Your machine supports MTRR but ReactOS doesn't yet.\n"); +} + +VOID +NTAPI +KiAmdK6InitializeMTRR(VOID) +{ + /* FIXME: Support this */ + DPRINT1("Your machine supports AMD MTRR but ReactOS doesn't yet.\n"); +}
Added: trunk/reactos/ntoskrnl/ke/i386/patpge.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ke/i386/patpge.c?r... ============================================================================== --- trunk/reactos/ntoskrnl/ke/i386/patpge.c (added) +++ trunk/reactos/ntoskrnl/ke/i386/patpge.c Wed Oct 4 07:37:11 2006 @@ -1,0 +1,63 @@ +/* +* PROJECT: ReactOS Kernel +* LICENSE: GPL - See COPYING in the top level directory +* FILE: ntoskrnl/ke/i386/patpge.c +* PURPOSE: Support for PAT and PGE (Large Pages) +* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) +*/ + +/* INCLUDES ******************************************************************/ + +#include <ntoskrnl.h> +#define NDEBUG +#include <debug.h> + +/* GLOBALS *******************************************************************/ + +ULONG Ke386GlobalPagesEnabled; + +/* FUNCTIONS *****************************************************************/ + +ULONG_PTR +NTAPI +Ki386EnableGlobalPage(IN volatile ULONG_PTR Context) +{ + volatile PLONG Count = (PLONG)Context; + ULONG Cr4, Cr3; + + /* Disable interrupts */ + _disable(); + + /* Decrease CPU Count and loop until it's reached 0 */ + do {InterlockedDecrement(Count);} while (!*Count); + + /* Now check if this is the Boot CPU */ + if (!KeGetPcr()->Number) + { + /* It is.FIXME: Patch KeFlushCurrentTb */ + } + + /* Now get CR4 and make sure PGE is masked out */ + Cr4 = Ke386GetCr4(); + Ke386SetCr4(Cr4 & ~CR4_PGE); + + /* Flush the TLB */ + Ke386GetPageTableDirectory(Cr3); + Ke386SetPageTableDirectory(Cr3); + + /* Now enable PGE */ + Ke386SetCr4(Cr4 | CR4_PGE); + Ke386GlobalPagesEnabled = TRUE; + + /* Restore interrupts */ + _enable(); + return 0; +} + +VOID +NTAPI +KiInitializePAT(VOID) +{ + /* FIXME: Support this */ + DPRINT1("Your machine supports PAT but ReactOS doesn't yet.\n"); +}
Modified: trunk/reactos/ntoskrnl/ke/krnlinit.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ke/krnlinit.c?rev=... ============================================================================== --- trunk/reactos/ntoskrnl/ke/krnlinit.c (original) +++ trunk/reactos/ntoskrnl/ke/krnlinit.c Wed Oct 4 07:37:11 2006 @@ -6,7 +6,7 @@ * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) */
-/* INCLUDES *****************************************************************/ +/* INCLUDES ******************************************************************/
#include <ntoskrnl.h> #define NDEBUG @@ -273,63 +273,18 @@ } }
-/* FIXME: Rename and make portable */ VOID NTAPI -KeInit2(VOID) -{ - ULONG Protect; - - /* Check if Fxsr was found */ - if (KeI386FxsrPresent) - { - /* Enable it. FIXME: Send an IPI */ - Ke386SetCr4(Ke386GetCr4() | X86_CR4_OSFXSR); - - /* Check if XMM was found too */ - if (KeI386XMMIPresent) - { - /* Enable it: FIXME: Send an IPI. */ - Ke386SetCr4(Ke386GetCr4() | X86_CR4_OSXMMEXCPT); - - /* FIXME: Implement and enable XMM Page Zeroing for Mm */ - } - } - - if (KeFeatureBits & KF_GLOBAL_PAGE) - { - ULONG Flags; - /* Enable global pages */ - Ke386GlobalPagesEnabled = TRUE; - Ke386SaveFlags(Flags); - Ke386DisableInterrupts(); - Ke386SetCr4(Ke386GetCr4() | X86_CR4_PGE); - Ke386RestoreFlags(Flags); - } - - if (KeFeatureBits & KF_FAST_SYSCALL) - { - extern void KiFastCallEntry(void); - - /* CS Selector of the target segment. */ - Ke386Wrmsr(0x174, KGDT_R0_CODE, 0); - /* Target ESP. */ - Ke386Wrmsr(0x175, 0, 0); - /* Target EIP. */ - Ke386Wrmsr(0x176, (ULONG_PTR)KiFastCallEntry, 0); - } - - /* Does the CPU Support 'prefetchnta' (SSE) */ - if(KeFeatureBits & KF_XMMI) - { - Protect = MmGetPageProtect(NULL, (PVOID)RtlPrefetchMemoryNonTemporal); - MmSetPageProtect(NULL, (PVOID)RtlPrefetchMemoryNonTemporal, Protect | PAGE_IS_WRITABLE); - /* Replace the ret by a nop */ - *(PCHAR)RtlPrefetchMemoryNonTemporal = 0x90; - MmSetPageProtect(NULL, (PVOID)RtlPrefetchMemoryNonTemporal, Protect); - } - - /* Set IDT to writable */ - Protect = MmGetPageProtect(NULL, (PVOID)KiIdt); - MmSetPageProtect(NULL, (PVOID)KiIdt, Protect | PAGE_IS_WRITABLE); -} +KeInitSystem(VOID) +{ + /* Check if Threaded DPCs are enabled */ + if (KeThreadDpcEnable) + { + /* FIXME: TODO */ + DPRINT1("Threaded DPCs not yet supported\n"); + } + + /* Initialize non-portable parts of the kernel */ + KiInitMachineDependent(); +} +
Modified: trunk/reactos/ntoskrnl/ntoskrnl.rbuild URL: http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ntoskrnl.rbuild?re... ============================================================================== --- trunk/reactos/ntoskrnl/ntoskrnl.rbuild (original) +++ trunk/reactos/ntoskrnl/ntoskrnl.rbuild Wed Oct 4 07:37:11 2006 @@ -37,6 +37,8 @@ <!-- <file>irq.c</file> --> <file>kiinit.c</file> <file>ldt.c</file> + <file>mtrr.c</file> + <file>patpge.c</file> <file>thread.c</file> <file>trap.s</file> <file>usercall_asm.S</file>