ReactOS  0.4.15-dev-3173-g40ee59d
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 CODE_SEG("INIT")
24 VOID
25 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 */
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 CODE_SEG("INIT")
56 VOID
57 NTAPI
59 {
60  PLOADER_PARAMETER_BLOCK LoaderBlock = KeLoaderBlock; // hack
61  PKPRCB Prcb = KeGetCurrentPrcb();
63  PKPROCESS Process = Thread->ApcState.Process;
64  PVOID KernelStack = (PVOID)KeLoaderBlock->KernelStack;
65 
66  /* Initialize the Power Management Support for this PRCB */
67  PoInitializePrcb(Prcb);
68 
69  /* Save CPU state */
71 
72  /* Get cache line information for this CPU */
74 
75  /* Initialize spinlocks and DPC data */
76  KiInitSpinLocks(Prcb, Prcb->Number);
77 
78  /* Set up the thread-related fields in the PRCB */
79  Prcb->CurrentThread = Thread;
80  Prcb->NextThread = NULL;
81  Prcb->IdleThread = Thread;
82 
83  /* Initialize PRCB pool lookaside pointers */
85 
86  /* Lower to APC_LEVEL */
88 
89  /* Check if this is the boot cpu */
90  if (Prcb->Number == 0)
91  {
92  /* Initialize the kernel */
93  KiInitializeKernel(Process, Thread, KernelStack, Prcb, LoaderBlock);
94  }
95  else
96  {
97  /* Initialize the startup thread */
99 
100  /* Initialize cpu with HAL */
101  if (!HalInitSystem(0, LoaderBlock))
102  {
103  /* Initialization failed */
104  KeBugCheck(HAL_INITIALIZATION_FAILED);
105  }
106  }
107 
108  /* Raise to Dispatch */
110 
111  /* Set the Idle Priority to 0. This will jump into Phase 1 */
113 
114  /* If there's no thread scheduled, put this CPU in the Idle summary */
115  KiAcquirePrcbLock(Prcb);
116  if (!Prcb->NextThread) KiIdleSummary |= (ULONG_PTR)1 << Prcb->Number;
117  KiReleasePrcbLock(Prcb);
118 
119  /* Raise back to HIGH_LEVEL and clear the PRCB for the loader block */
121  LoaderBlock->Prcb = 0;
122 
123  /* Set the priority of this thread to 0 */
125  Thread->Priority = 0;
126 
127  /* Force interrupts enabled and lower IRQL back to DISPATCH_LEVEL */
128  _enable();
130 
131  /* Set the right wait IRQL */
132  Thread->WaitIrql = DISPATCH_LEVEL;
133 
134  /* Jump into the idle loop */
135  KiIdleLoop();
136 }
137 
138 CODE_SEG("INIT")
139 VOID
140 NTAPI
142  IN PKTHREAD InitThread,
143  IN PVOID IdleStack,
144  IN PKPRCB Prcb,
145  IN PLOADER_PARAMETER_BLOCK LoaderBlock)
146 {
147  ULONG_PTR PageDirectory[2];
148  PVOID DpcStack;
149  ULONG i;
150 
151  /* Set Node Data */
152  KeNodeBlock[0] = &KiNode0;
153  Prcb->ParentNode = KeNodeBlock[0];
154  KeNodeBlock[0]->ProcessorMask = Prcb->SetMember;
155 
156  /* Set boot-level flags */
157  KeFeatureBits = Prcb->FeatureBits;
158 
159  /* Initialize 8/16 bit SList support */
161 
162  /* Set the current MP Master KPRCB to the Boot PRCB */
163  Prcb->MultiThreadSetMaster = Prcb;
164 
165  /* Initialize Bugcheck Callback data */
169 
170  /* Initialize the Timer Expiration DPC */
173 
174  /* Initialize Profiling data */
178 
179  /* Loop the timer table */
180  for (i = 0; i < TIMER_TABLE_SIZE; i++)
181  {
182  /* Initialize the list and entries */
184  KiTimerTableListHead[i].Time.HighPart = 0xFFFFFFFF;
186  }
187 
188  /* Initialize the Swap event and all swap lists */
193 
194  /* Initialize the mutex for generic DPC calls */
196 
197  /* Initialize the syscall table */
203 
204  /* Copy the the current table into the shadow table for win32k */
207  sizeof(KeServiceDescriptorTable));
208 
209  /* Initialize the Idle Process and the Process Listhead */
211  PageDirectory[0] = 0;
212  PageDirectory[1] = 0;
213  KeInitializeProcess(InitProcess,
214  0,
215  0xFFFFFFFF,
216  PageDirectory,
217  FALSE);
218  InitProcess->QuantumReset = MAXCHAR;
219 
220  /* Initialize the startup thread */
221  KiInitializeHandBuiltThread(InitThread, InitProcess, IdleStack);
222 
223  /* Initialize the Kernel Executive */
224  ExpInitializeExecutive(0, LoaderBlock);
225 
226  /* Calculate the time reciprocal */
230 
231  /* Update DPC Values in case they got updated by the executive */
232  Prcb->MaximumDpcQueueDepth = KiMaximumDpcQueueDepth;
233  Prcb->MinimumDpcRate = KiMinimumDpcRate;
234  Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
235 
236  /* Allocate the DPC Stack */
237  DpcStack = MmCreateKernelStack(FALSE, 0);
238  if (!DpcStack) KeBugCheckEx(NO_PAGES_AVAILABLE, 1, 0, 0, 0);
239  Prcb->DpcStack = DpcStack;
240 }
241 
DECLSPEC_NORETURN VOID KiIdleLoop(VOID)
Definition: stubs.c:165
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:39
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:230
#define TRUE
Definition: types.h:120
PKNODE KeNodeBlock[1]
Definition: krnlinit.c:39
void __cdecl _enable(void)
Definition: intrin_arm.h:373
LARGE_INTEGER NTAPI KiComputeReciprocal(IN LONG Divisor, OUT PUCHAR Shift)
Definition: krnlinit.c:123
VOID NTAPI ExpInitializeExecutive(IN ULONG Cpu, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: init.c:919
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1079
DECLSPEC_NORETURN VOID NTAPI KiSystemStartupBootStack(VOID)
Definition: krnlinit.c:58
ULONG_PTR MainSSDT[]
Definition: napi.h:9
$ULONG LowPart
Definition: ntbasedef.h:569
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:571
#define DECLSPEC_NORETURN
Definition: ntbasedef.h:176
uint32_t ULONG_PTR
Definition: typedefs.h:65
FAST_MUTEX KiGenericCallDpcMutex
Definition: dpc.c:24
_In_ WDFREQUEST _In_ PIO_STACK_LOCATION Stack
Definition: wdfrequest.h:636
ULONG_PTR Thread
Definition: arc.h:499
LIST_ENTRY KiProfileListHead
Definition: profobj.c:18
struct _KTHREAD * PKTHREAD
Definition: nt_native.h:28
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define FALSE
Definition: types.h:117
DECLSPEC_NORETURN VOID NTAPI KeBugCheck(ULONG BugCheckCode)
Definition: bug.c:1427
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:570
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:238
unsigned char BOOLEAN
FORCEINLINE VOID KiAcquirePrcbLock(IN PKPRCB Prcb)
Definition: ke_x.h:220
KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTableShadow[SSDT_MAX_ENTRIES]
Definition: procobj.c:24
ULONG KeFeatureBits
Definition: krnlinit.c:22
VOID NTAPI PoInitializePrcb(IN PKPRCB Prcb)
Definition: power.c:498
VOID NTAPI KiInitSpinLocks(IN PKPRCB Prcb, IN CCHAR Number)
Definition: krnlinit.c:187
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:709
KIRQL FASTCALL KfRaiseIrql(IN KIRQL NewIrql)
Definition: pic.c:187
#define TIMER_TABLE_SIZE
Definition: ketypes.h:836
$ULONG HighPart
Definition: ntbasedef.h:570
#define MAXCHAR
Definition: umtypes.h:112
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:2652
VOID KiGetCacheInformation(VOID)
Definition: cpu.c:204
LIST_ENTRY KeBugcheckCallbackListHead
Definition: bug.c:18
KPROCESSOR_STATE ProcessorState
Definition: ketypes.h:583
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:362
ULONG_PTR KiIdleSummary
Definition: thrdschd.c:25
KEVENT KiSwapEvent
Definition: procobj.c:21
LIST_ENTRY KiProfileSourceListHead
Definition: profobj.c:19
struct _KTHREAD * IdleThread
Definition: ketypes.h:572
#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
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
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:19
VOID NTAPI ExInitPoolLookasidePointers(VOID)
Definition: lookas.c:59
#define NULL
Definition: types.h:112
UCHAR MainSSPT[]
Definition: napi.h:15
VOID NTAPI KeSetTargetProcessorDpc(IN PKDPC Dpc, IN CCHAR Number)
Definition: dpc.c:971
_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:563
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
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:818
PVOID NTAPI MmCreateKernelStack(BOOLEAN GuiStack, UCHAR Node)
#define KeGetCurrentThread
Definition: hal.h:55
VOID NTAPI KeLowerIrql(KIRQL NewIrql)
Definition: spinlock.c:39
VOID NTAPI KeInitializeDpc(IN PKDPC Dpc, IN PKDEFERRED_ROUTINE DeferredRoutine, IN PVOID DeferredContext)
Definition: dpc.c:712
BOOLEAN NTAPI HalInitSystem(IN ULONG BootPhase, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: halinit.c:43
static CODE_SEG("PAGE")
Definition: isapnp.c:1482
#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:108
KSPIN_LOCK BugCheckCallbackLock
Definition: bug.c:20
ULONG_PTR KernelStack
Definition: arc.h:496
LIST_ENTRY KiProcessListHead
Definition: procobj.c:18
VOID NTAPI KiInitializeKernel(IN PKPROCESS InitProcess, IN PKTHREAD InitThread, IN PVOID IdleStack, IN PKPRCB Prcb, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: krnlinit.c:141