ReactOS  r75907
krnlinit.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Kernel
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: ntoskrnl/ke/amd64/krnlinit.c
5  * PURPOSE: Portable part of kernel initialization
6  * PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
7  * Alex Ionescu (alex.ionescu@reactos.org)
8  */
9 
10 /* INCLUDES ******************************************************************/
11 
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15 
16 extern ULONG_PTR MainSSDT[];
17 extern UCHAR MainSSPT[];
18 
19 /* FUNCTIONS *****************************************************************/
20 
21 VOID
22 NTAPI
24 KiInitializeKernel(IN PKPROCESS InitProcess,
25  IN PKTHREAD InitThread,
26  IN PVOID IdleStack,
27  IN PKPRCB Prcb,
28  IN PLOADER_PARAMETER_BLOCK LoaderBlock);
29 
30 
31 VOID
32 NTAPI
36  IN PVOID Stack)
37 {
38  PKPRCB Prcb = KeGetCurrentPrcb();
39 
40  /* Setup the Thread */
41  KeInitializeThread(Process, Thread, NULL, NULL, NULL, NULL, NULL, Stack);
42 
43  Thread->NextProcessor = Prcb->Number;
44  Thread->Priority = HIGH_PRIORITY;
45  Thread->State = Running;
46  Thread->Affinity = (ULONG_PTR)1 << Prcb->Number;
47  Thread->WaitIrql = DISPATCH_LEVEL;
48  Process->ActiveProcessors |= (ULONG_PTR)1 << Prcb->Number;
49 
50 }
51 
52 VOID
53 NTAPI
56 {
57  PLOADER_PARAMETER_BLOCK LoaderBlock = KeLoaderBlock; // hack
58  PKPRCB Prcb = KeGetCurrentPrcb();
60  PKPROCESS Process = Thread->ApcState.Process;
61  PVOID KernelStack = (PVOID)KeLoaderBlock->KernelStack;
62 
63  /* Initialize the Power Management Support for this PRCB */
64  PoInitializePrcb(Prcb);
65 
66  /* Save CPU state */
68 
69  /* Get cache line information for this CPU */
71 
72  /* Initialize spinlocks and DPC data */
73  KiInitSpinLocks(Prcb, Prcb->Number);
74 
75  /* Set up the thread-related fields in the PRCB */
76  Prcb->CurrentThread = Thread;
77  Prcb->NextThread = NULL;
78  Prcb->IdleThread = Thread;
79 
80  /* Initialize PRCB pool lookaside pointers */
82 
83  /* Lower to APC_LEVEL */
85 
86  /* Check if this is the boot cpu */
87  if (Prcb->Number == 0)
88  {
89  /* Initialize the kernel */
90  KiInitializeKernel(Process, Thread, KernelStack, Prcb, LoaderBlock);
91  }
92  else
93  {
94  /* Initialize the startup thread */
95  KiInitializeHandBuiltThread(Thread, Process, KernelStack);
96 
97  /* Initialize cpu with HAL */
98  if (!HalInitSystem(0, LoaderBlock))
99  {
100  /* Initialization failed */
101  KeBugCheck(HAL_INITIALIZATION_FAILED);
102  }
103  }
104 
105  /* Raise to Dispatch */
107 
108  /* Set the Idle Priority to 0. This will jump into Phase 1 */
109  KeSetPriorityThread(Thread, 0);
110 
111  /* If there's no thread scheduled, put this CPU in the Idle summary */
112  KiAcquirePrcbLock(Prcb);
113  if (!Prcb->NextThread) KiIdleSummary |= (ULONG_PTR)1 << Prcb->Number;
114  KiReleasePrcbLock(Prcb);
115 
116  /* Raise back to HIGH_LEVEL and clear the PRCB for the loader block */
118  LoaderBlock->Prcb = 0;
119 
120  /* Set the priority of this thread to 0 */
121  Thread = KeGetCurrentThread();
122  Thread->Priority = 0;
123 
124  /* Force interrupts enabled and lower IRQL back to DISPATCH_LEVEL */
125  _enable();
127 
128  /* Set the right wait IRQL */
129  Thread->WaitIrql = DISPATCH_LEVEL;
130 
131  /* Jump into the idle loop */
132  KiIdleLoop();
133 }
134 
135 VOID
136 NTAPI
139  IN PKTHREAD InitThread,
140  IN PVOID IdleStack,
141  IN PKPRCB Prcb,
142  IN PLOADER_PARAMETER_BLOCK LoaderBlock)
143 {
144  ULONG_PTR PageDirectory[2];
145  PVOID DpcStack;
146  ULONG i;
147 
148  /* Set Node Data */
149  KeNodeBlock[0] = &KiNode0;
150  Prcb->ParentNode = KeNodeBlock[0];
151  KeNodeBlock[0]->ProcessorMask = Prcb->SetMember;
152 
153  /* Set boot-level flags */
154  KeFeatureBits = Prcb->FeatureBits;
155 
156  /* Set the current MP Master KPRCB to the Boot PRCB */
157  Prcb->MultiThreadSetMaster = Prcb;
158 
159  /* Initialize Bugcheck Callback data */
163 
164  /* Initialize the Timer Expiration DPC */
167 
168  /* Initialize Profiling data */
172 
173  /* Loop the timer table */
174  for (i = 0; i < TIMER_TABLE_SIZE; i++)
175  {
176  /* Initialize the list and entries */
178  KiTimerTableListHead[i].Time.HighPart = 0xFFFFFFFF;
180  }
181 
182  /* Initialize the Swap event and all swap lists */
187 
188  /* Initialize the mutex for generic DPC calls */
190 
191  /* Initialize the syscall table */
197 
198  /* Copy the the current table into the shadow table for win32k */
201  sizeof(KeServiceDescriptorTable));
202 
203  /* Initialize the Idle Process and the Process Listhead */
205  PageDirectory[0] = 0;
206  PageDirectory[1] = 0;
207  KeInitializeProcess(InitProcess,
208  0,
209  0xFFFFFFFF,
210  PageDirectory,
211  FALSE);
212  InitProcess->QuantumReset = MAXCHAR;
213 
214  /* Initialize the startup thread */
215  KiInitializeHandBuiltThread(InitThread, InitProcess, IdleStack);
216 
217  /* Initialize the Kernel Executive */
218  ExpInitializeExecutive(0, LoaderBlock);
219 
220  /* Calculate the time reciprocal */
224 
225  /* Update DPC Values in case they got updated by the executive */
226  Prcb->MaximumDpcQueueDepth = KiMaximumDpcQueueDepth;
227  Prcb->MinimumDpcRate = KiMinimumDpcRate;
228  Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
229 
230  /* Allocate the DPC Stack */
231  DpcStack = MmCreateKernelStack(FALSE, 0);
232  if (!DpcStack) KeBugCheckEx(NO_PAGES_AVAILABLE, 1, 0, 0, 0);
233  Prcb->DpcStack = DpcStack;
234 }
235 
DWORD *typedef PVOID
Definition: winlogon.h:52
VOID NTAPI KiInitializeHandBuiltThread(IN PKTHREAD Thread, IN PKPROCESS Process, IN PVOID Stack)
Definition: krnlinit.c:33
ULONG KeMaximumIncrement
Definition: clock.c:20
#define IN
Definition: typedefs.h:39
VOID NTAPI INIT_FUNCTION KiInitSpinLocks(IN PKPRCB Prcb, IN CCHAR Number)
Definition: krnlinit.c:187
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
KSPIN_LOCK KiProfileLock
Definition: profobj.c:20
KNODE KiNode0
Definition: krnlinit.c:38
KDPC KiTimerExpireDpc
Definition: dpc.c:25
FORCEINLINE VOID KiReleasePrcbLock(IN PKPRCB Prcb)
Definition: ke_x.h:224
PKNODE KeNodeBlock[1]
Definition: krnlinit.c:39
void __cdecl _enable(void)
Definition: intrin_arm.h:373
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1054
SCHAR Priority
Definition: ketypes.h:974
ULONG_PTR MainSSDT[]
Definition: napi.h:9
$ULONG LowPart
Definition: ntbasedef.h:568
ULONG KiAdjustDpcThreshold
Definition: dpc.c:21
VOID NTAPI KeInitializeProcess(struct _KPROCESS *Process, KPRIORITY Priority, KAFFINITY Affinity, PULONG_PTR DirectoryTableBase, IN BOOLEAN Enable)
ULONG_PTR Prcb
Definition: arc.h:497
struct _KTHREAD * NextThread
Definition: ketypes.h:567
uint32_t ULONG_PTR
Definition: typedefs.h:64
FAST_MUTEX KiGenericCallDpcMutex
Definition: dpc.c:24
ULONG_PTR Thread
Definition: arc.h:499
VOID NTAPI INIT_FUNCTION ExpInitializeExecutive(IN ULONG Cpu, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: init.c:914
LIST_ENTRY KiProfileListHead
Definition: profobj.c:18
struct _KTHREAD * PKTHREAD
Definition: nt_native.h:28
GLenum GLclampf GLint i
Definition: glfuncs.h:14
VOID NTAPI INIT_FUNCTION KiSystemStartupBootStack(VOID)
Definition: krnlinit.c:55
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define FALSE
Definition: types.h:117
KAPC_STATE ApcState
Definition: ketypes.h:969
VOID NTAPI KiTimerExpiration(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
Definition: dpc.c:79
struct _KTHREAD * CurrentThread
Definition: ketypes.h:566
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:251
FORCEINLINE VOID KiAcquirePrcbLock(IN PKPRCB Prcb)
Definition: ke_x.h:214
KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTableShadow[SSDT_MAX_ENTRIES]
Definition: procobj.c:24
ULONG KeFeatureBits
Definition: krnlinit.c:22
smooth NULL
Definition: ftsmooth.c:513
VOID NTAPI PoInitializePrcb(IN PKPRCB Prcb)
Definition: power.c:389
VOID NTAPI INIT_FUNCTION KiInitializeKernel(IN PKPROCESS InitProcess, IN PKTHREAD InitThread, IN PVOID IdleStack, IN PKPRCB Prcb, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: krnlinit.c:138
VOID NTAPI KeBugCheck(ULONG BugCheckCode)
Definition: bug.c:1469
LARGE_INTEGER KiTimeIncrementReciprocal
Definition: timerobj.c:18
UCHAR KiTimeIncrementShiftCount
Definition: timerobj.c:19
PLOADER_PARAMETER_BLOCK KeLoaderBlock
Definition: krnlinit.c:29
ULARGE_INTEGER Time
Definition: ketypes.h:667
VOID NTAPI INIT_FUNCTION ExInitPoolLookasidePointers(VOID)
Definition: lookas.c:63
KIRQL FASTCALL KfRaiseIrql(IN KIRQL NewIrql)
Definition: pic.c:187
#define TIMER_TABLE_SIZE
Definition: ketypes.h:821
$ULONG HighPart
Definition: ntbasedef.h:569
#define MAXCHAR
Definition: umtypes.h:100
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
ULONG KiServiceLimit
Definition: krnlinit.c:26
ULONG KiMinimumDpcRate
Definition: dpc.c:20
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
VOID KiGetCacheInformation(VOID)
Definition: cpu.c:224
LARGE_INTEGER NTAPI INIT_FUNCTION KiComputeReciprocal(IN LONG Divisor, OUT PUCHAR Shift)
Definition: krnlinit.c:123
LIST_ENTRY KeBugcheckCallbackListHead
Definition: bug.c:21
KPROCESSOR_STATE ProcessorState
Definition: ketypes.h:579
KTIMER_TABLE_ENTRY KiTimerTableListHead[TIMER_TABLE_SIZE]
Definition: timerobj.c:17
unsigned char UCHAR
Definition: xmlstorage.h:181
VOID NTAPI KiSaveProcessorControlState(OUT PKPROCESSOR_STATE ProcessorState)
Definition: cpu.c:382
ULONG_PTR KiIdleSummary
Definition: thrdschd.c:25
VOID FASTCALL KiIdleLoop(VOID)
Definition: stubs.c:113
KEVENT KiSwapEvent
Definition: procobj.c:21
LIST_ENTRY KiProfileSourceListHead
Definition: profobj.c:19
struct _KTHREAD * IdleThread
Definition: ketypes.h:568
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
ULONG KiMaximumDpcQueueDepth
Definition: dpc.c:19
#define HIGH_LEVEL
Definition: env_spec_w32.h:703
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
KPRIORITY NTAPI KeSetPriorityThread(IN PKTHREAD Thread, IN KPRIORITY Priority)
Definition: thrdobj.c:1327
VOID NTAPI KeInitializeThread(IN PKPROCESS Process, IN OUT PKTHREAD Thread, IN PKSYSTEM_ROUTINE SystemRoutine, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext, IN PCONTEXT Context, IN PVOID Teb, IN PVOID KernelStack)
Definition: thrdobj.c:900
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
LIST_ENTRY KeBugcheckReasonCallbackListHead
Definition: bug.c:22
UCHAR MainSSPT[]
Definition: napi.h:15
VOID NTAPI KeSetTargetProcessorDpc(IN PKDPC Dpc, IN CCHAR Number)
Definition: dpc.c:970
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTable[SSDT_MAX_ENTRIES]
Definition: procobj.c:23
USHORT Number
Definition: ketypes.h:559
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
LIST_ENTRY KiStackInSwapListHead
Definition: procobj.c:20
#define HIGH_PRIORITY
LIST_ENTRY KiProcessInSwapListHead
Definition: procobj.c:19
LIST_ENTRY KiProcessOutSwapListHead
Definition: procobj.c:19
KAFFINITY ProcessorMask
Definition: ketypes.h:776
PVOID NTAPI MmCreateKernelStack(BOOLEAN GuiStack, UCHAR Node)
#define KeGetCurrentThread
Definition: hal.h:44
VOID NTAPI KeInitializeDpc(IN PKDPC Dpc, IN PKDEFERRED_ROUTINE DeferredRoutine, IN PVOID DeferredContext)
Definition: dpc.c:711
BOOLEAN NTAPI HalInitSystem(IN ULONG BootPhase, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: halinit.c:43
#define APC_LEVEL
Definition: env_spec_w32.h:695
base of all file and directory entries
Definition: entries.h:82
VOID NTAPI KeBugCheckEx(_In_ ULONG BugCheckCode, _In_ ULONG_PTR BugCheckParameter1, _In_ ULONG_PTR BugCheckParameter2, _In_ ULONG_PTR BugCheckParameter3, _In_ ULONG_PTR BugCheckParameter4)
Definition: rtlcompat.c:90
KSPIN_LOCK BugCheckCallbackLock
Definition: bug.c:23
ULONG_PTR KernelStack
Definition: arc.h:496
LIST_ENTRY KiProcessListHead
Definition: procobj.c:18
#define INIT_FUNCTION
Definition: ntoskrnl.h:11
KIRQL WaitIrql
Definition: ketypes.h:999