ReactOS  0.4.10-dev-2-g0fbaea8
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 
20 
21 /* FUNCTIONS *****************************************************************/
22 
23 VOID
24 NTAPI
26 KiInitializeKernel(IN PKPROCESS InitProcess,
27  IN PKTHREAD InitThread,
28  IN PVOID IdleStack,
29  IN PKPRCB Prcb,
30  IN PLOADER_PARAMETER_BLOCK LoaderBlock);
31 
32 
33 VOID
34 NTAPI
38  IN PVOID Stack)
39 {
40  PKPRCB Prcb = KeGetCurrentPrcb();
41 
42  /* Setup the Thread */
43  KeInitializeThread(Process, Thread, NULL, NULL, NULL, NULL, NULL, Stack);
44 
45  Thread->NextProcessor = Prcb->Number;
46  Thread->Priority = HIGH_PRIORITY;
47  Thread->State = Running;
48  Thread->Affinity = (ULONG_PTR)1 << Prcb->Number;
49  Thread->WaitIrql = DISPATCH_LEVEL;
50  Process->ActiveProcessors |= (ULONG_PTR)1 << Prcb->Number;
51 
52 }
53 
54 VOID
55 NTAPI
58 {
59  PLOADER_PARAMETER_BLOCK LoaderBlock = KeLoaderBlock; // hack
60  PKPRCB Prcb = KeGetCurrentPrcb();
62  PKPROCESS Process = Thread->ApcState.Process;
63  PVOID KernelStack = (PVOID)KeLoaderBlock->KernelStack;
64 
65  /* Initialize the Power Management Support for this PRCB */
66  PoInitializePrcb(Prcb);
67 
68  /* Save CPU state */
70 
71  /* Get cache line information for this CPU */
73 
74  /* Initialize spinlocks and DPC data */
75  KiInitSpinLocks(Prcb, Prcb->Number);
76 
77  /* Set up the thread-related fields in the PRCB */
78  Prcb->CurrentThread = Thread;
79  Prcb->NextThread = NULL;
80  Prcb->IdleThread = Thread;
81 
82  /* Initialize PRCB pool lookaside pointers */
84 
85  /* Lower to APC_LEVEL */
87 
88  /* Check if this is the boot cpu */
89  if (Prcb->Number == 0)
90  {
91  /* Initialize the kernel */
92  KiInitializeKernel(Process, Thread, KernelStack, Prcb, LoaderBlock);
93  }
94  else
95  {
96  /* Initialize the startup thread */
97  KiInitializeHandBuiltThread(Thread, Process, KernelStack);
98 
99  /* Initialize cpu with HAL */
100  if (!HalInitSystem(0, LoaderBlock))
101  {
102  /* Initialization failed */
103  KeBugCheck(HAL_INITIALIZATION_FAILED);
104  }
105  }
106 
107  /* Raise to Dispatch */
109 
110  /* Set the Idle Priority to 0. This will jump into Phase 1 */
111  KeSetPriorityThread(Thread, 0);
112 
113  /* If there's no thread scheduled, put this CPU in the Idle summary */
114  KiAcquirePrcbLock(Prcb);
115  if (!Prcb->NextThread) KiIdleSummary |= (ULONG_PTR)1 << Prcb->Number;
116  KiReleasePrcbLock(Prcb);
117 
118  /* Raise back to HIGH_LEVEL and clear the PRCB for the loader block */
120  LoaderBlock->Prcb = 0;
121 
122  /* Set the priority of this thread to 0 */
123  Thread = KeGetCurrentThread();
124  Thread->Priority = 0;
125 
126  /* Force interrupts enabled and lower IRQL back to DISPATCH_LEVEL */
127  _enable();
129 
130  /* Set the right wait IRQL */
131  Thread->WaitIrql = DISPATCH_LEVEL;
132 
133  /* Jump into the idle loop */
134  KiIdleLoop();
135 }
136 
137 VOID
138 NTAPI
141  IN PKTHREAD InitThread,
142  IN PVOID IdleStack,
143  IN PKPRCB Prcb,
144  IN PLOADER_PARAMETER_BLOCK LoaderBlock)
145 {
146  ULONG_PTR PageDirectory[2];
147  PVOID DpcStack;
148  ULONG i;
149 
150  /* Set Node Data */
151  KeNodeBlock[0] = &KiNode0;
152  Prcb->ParentNode = KeNodeBlock[0];
153  KeNodeBlock[0]->ProcessorMask = Prcb->SetMember;
154 
155  /* Set boot-level flags */
156  KeFeatureBits = Prcb->FeatureBits;
157 
158  /* Initialize 8/16 bit SList support */
160 
161  /* Set the current MP Master KPRCB to the Boot PRCB */
162  Prcb->MultiThreadSetMaster = Prcb;
163 
164  /* Initialize Bugcheck Callback data */
168 
169  /* Initialize the Timer Expiration DPC */
172 
173  /* Initialize Profiling data */
177 
178  /* Loop the timer table */
179  for (i = 0; i < TIMER_TABLE_SIZE; i++)
180  {
181  /* Initialize the list and entries */
183  KiTimerTableListHead[i].Time.HighPart = 0xFFFFFFFF;
185  }
186 
187  /* Initialize the Swap event and all swap lists */
192 
193  /* Initialize the mutex for generic DPC calls */
195 
196  /* Initialize the syscall table */
202 
203  /* Copy the the current table into the shadow table for win32k */
206  sizeof(KeServiceDescriptorTable));
207 
208  /* Initialize the Idle Process and the Process Listhead */
210  PageDirectory[0] = 0;
211  PageDirectory[1] = 0;
212  KeInitializeProcess(InitProcess,
213  0,
214  0xFFFFFFFF,
215  PageDirectory,
216  FALSE);
217  InitProcess->QuantumReset = MAXCHAR;
218 
219  /* Initialize the startup thread */
220  KiInitializeHandBuiltThread(InitThread, InitProcess, IdleStack);
221 
222  /* Initialize the Kernel Executive */
223  ExpInitializeExecutive(0, LoaderBlock);
224 
225  /* Calculate the time reciprocal */
229 
230  /* Update DPC Values in case they got updated by the executive */
231  Prcb->MaximumDpcQueueDepth = KiMaximumDpcQueueDepth;
232  Prcb->MinimumDpcRate = KiMinimumDpcRate;
233  Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
234 
235  /* Allocate the DPC Stack */
236  DpcStack = MmCreateKernelStack(FALSE, 0);
237  if (!DpcStack) KeBugCheckEx(NO_PAGES_AVAILABLE, 1, 0, 0, 0);
238  Prcb->DpcStack = DpcStack;
239 }
240 
DWORD *typedef PVOID
Definition: winlogon.h:52
VOID NTAPI KiInitializeHandBuiltThread(IN PKTHREAD Thread, IN PKPROCESS Process, IN PVOID Stack)
Definition: krnlinit.c:35
ULONG KeMaximumIncrement
Definition: clock.c:20
#define IN
Definition: typedefs.h:38
VOID NTAPI INIT_FUNCTION KiInitSpinLocks(IN PKPRCB Prcb, IN CCHAR Number)
Definition: krnlinit.c:187
#define TRUE
Definition: types.h:120
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:1672
ULONG_PTR MainSSDT[]
Definition: napi.h:9
$ULONG LowPart
Definition: ntbasedef.h:576
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:63
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:57
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:1668
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:416
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:140
VOID NTAPI KeBugCheck(ULONG BugCheckCode)
Definition: bug.c:1469
LARGE_INTEGER KiTimeIncrementReciprocal
Definition: timerobj.c:18
UCHAR KiTimeIncrementShiftCount
Definition: timerobj.c:19
#define HIGH_PRIORITY
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
unsigned char BOOLEAN
#define TIMER_TABLE_SIZE
Definition: ketypes.h:836
$ULONG HighPart
Definition: ntbasedef.h:577
#define MAXCHAR
Definition: umtypes.h:100
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
ULONG KiServiceLimit
Definition: krnlinit.c:26
BOOLEAN RtlpUse16ByteSLists
ULONG KiMinimumDpcRate
Definition: dpc.c:20
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
VOID KiGetCacheInformation(VOID)
Definition: cpu.c:214
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:372
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 KF_CMPXCHG16B
Definition: ketypes.h:163
#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
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:94
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:1685