ReactOS 0.4.15-dev-5667-ged97270
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
16extern ULONG_PTR MainSSDT[];
17extern UCHAR MainSSPT[];
18
20
21/* FUNCTIONS *****************************************************************/
22
23CODE_SEG("INIT")
24VOID
27 IN PKTHREAD InitThread,
28 IN PVOID IdleStack,
29 IN PKPRCB Prcb,
30 IN PLOADER_PARAMETER_BLOCK LoaderBlock);
31
32
33VOID
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
54CODE_SEG("INIT")
56VOID
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
138CODE_SEG("INIT")
139VOID
140NTAPI
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 */
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
unsigned char BOOLEAN
ULONG_PTR MainSSDT[]
Definition: napi.h:9
VOID NTAPI KiInitializeKernel(IN PKPROCESS InitProcess, IN PKTHREAD InitThread, IN PVOID IdleStack, IN PKPRCB Prcb, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: krnlinit.c:141
DECLSPEC_NORETURN VOID NTAPI KiSystemStartupBootStack(VOID)
Definition: krnlinit.c:58
VOID NTAPI KiInitializeHandBuiltThread(IN PKTHREAD Thread, IN PKPROCESS Process, IN PVOID Stack)
Definition: krnlinit.c:35
UCHAR MainSSPT[]
Definition: napi.h:15
BOOLEAN RtlpUse16ByteSLists
DECLSPEC_NORETURN VOID NTAPI KeBugCheck(ULONG BugCheckCode)
Definition: bug.c:1427
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
VOID NTAPI KeInitializeDpc(IN PKDPC Dpc, IN PKDEFERRED_ROUTINE DeferredRoutine, IN PVOID DeferredContext)
Definition: dpc.c:712
VOID NTAPI KeSetTargetProcessorDpc(IN PKDPC Dpc, IN CCHAR Number)
Definition: dpc.c:971
#define ULONG_PTR
Definition: config.h:101
#define HIGH_LEVEL
Definition: env_spec_w32.h:703
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define APC_LEVEL
Definition: env_spec_w32.h:695
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
#define KeInitializeSpinLock(sl)
Definition: env_spec_w32.h:604
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:223
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
KIRQL FASTCALL KfRaiseIrql(IN KIRQL NewIrql)
Definition: pic.c:187
#define KeGetCurrentThread
Definition: hal.h:55
BOOLEAN NTAPI HalInitSystem(IN ULONG BootPhase, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: halinit.c:43
#define HIGH_PRIORITY
void __cdecl _enable(void)
Definition: intrin_arm.h:373
static CODE_SEG("PAGE")
Definition: isapnp.c:1482
FORCEINLINE VOID KiReleasePrcbLock(IN PKPRCB Prcb)
Definition: ke_x.h:230
FORCEINLINE VOID KiAcquirePrcbLock(IN PKPRCB Prcb)
Definition: ke_x.h:220
PLOADER_PARAMETER_BLOCK KeLoaderBlock
Definition: krnlinit.c:29
VOID NTAPI ExInitPoolLookasidePointers(VOID)
Definition: lookas.c:59
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1080
#define KF_CMPXCHG16B
Definition: ketypes.h:163
@ Running
Definition: ketypes.h:423
struct _KTHREAD * PKTHREAD
Definition: nt_native.h:28
#define DECLSPEC_NORETURN
Definition: ntbasedef.h:176
@ SynchronizationEvent
VOID NTAPI ExpInitializeExecutive(IN ULONG Cpu, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: init.c:922
VOID KiGetCacheInformation(VOID)
Definition: cpu.c:239
KTIMER_TABLE_ENTRY KiTimerTableListHead[TIMER_TABLE_SIZE]
Definition: timerobj.c:17
LIST_ENTRY KeBugcheckCallbackListHead
Definition: bug.c:18
LIST_ENTRY KiProcessOutSwapListHead
Definition: ke.h:136
LARGE_INTEGER KiTimeIncrementReciprocal
Definition: timerobj.c:18
FAST_MUTEX KiGenericCallDpcMutex
Definition: dpc.c:24
KEVENT KiSwapEvent
Definition: procobj.c:21
ULONG KiAdjustDpcThreshold
Definition: dpc.c:21
ULONG KiMaximumDpcQueueDepth
Definition: dpc.c:19
KDPC KiTimerExpireDpc
Definition: dpc.c:25
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
ULONG KeFeatureBits
Definition: krnlinit.c:22
UCHAR KiTimeIncrementShiftCount
Definition: timerobj.c:19
KSPIN_LOCK BugCheckCallbackLock
Definition: bug.c:20
ULONG KiMinimumDpcRate
Definition: dpc.c:20
DECLSPEC_NORETURN VOID KiIdleLoop(VOID)
Definition: stubs.c:170
LIST_ENTRY KiProfileListHead
Definition: profobj.c:18
PKNODE KeNodeBlock[1]
Definition: krnlinit.c:39
LARGE_INTEGER NTAPI KiComputeReciprocal(IN LONG Divisor, OUT PUCHAR Shift)
Definition: krnlinit.c:123
LIST_ENTRY KiProcessInSwapListHead
Definition: procobj.c:19
ULONG KiServiceLimit
Definition: krnlinit.c:26
VOID NTAPI KiSaveProcessorControlState(OUT PKPROCESSOR_STATE ProcessorState)
Definition: cpu.c:397
KSPIN_LOCK KiProfileLock
Definition: profobj.c:20
LIST_ENTRY KiStackInSwapListHead
Definition: procobj.c:20
VOID NTAPI KeInitializeProcess(struct _KPROCESS *Process, KPRIORITY Priority, KAFFINITY Affinity, PULONG_PTR DirectoryTableBase, IN BOOLEAN Enable)
ULONG_PTR KiIdleSummary
Definition: thrdschd.c:25
VOID NTAPI KiInitSpinLocks(IN PKPRCB Prcb, IN CCHAR Number)
Definition: krnlinit.c:187
LIST_ENTRY KiProcessListHead
Definition: procobj.c:18
LIST_ENTRY KeBugcheckReasonCallbackListHead
Definition: ke.h:128
LIST_ENTRY KiProfileSourceListHead
Definition: ke.h:133
VOID NTAPI KiTimerExpiration(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
Definition: dpc.c:79
KNODE KiNode0
Definition: krnlinit.c:38
PVOID NTAPI MmCreateKernelStack(BOOLEAN GuiStack, UCHAR Node)
ULONG KeMaximumIncrement
Definition: clock.c:20
VOID NTAPI PoInitializePrcb(IN PKPRCB Prcb)
Definition: power.c:498
KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTable[SSDT_MAX_ENTRIES]
Definition: procobj.c:23
KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTableShadow[SSDT_MAX_ENTRIES]
Definition: procobj.c:24
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
base of all file and directory entries
Definition: entries.h:83
KAFFINITY ProcessorMask
Definition: ketypes.h:827
struct _KTHREAD * IdleThread
Definition: ketypes.h:573
struct _KTHREAD * CurrentThread
Definition: ketypes.h:571
struct _KTHREAD * NextThread
Definition: ketypes.h:572
USHORT Number
Definition: ketypes.h:564
KPROCESSOR_STATE ProcessorState
Definition: ketypes.h:584
ULARGE_INTEGER Time
Definition: ketypes.h:719
ULONG_PTR KernelStack
Definition: arc.h:496
ULONG_PTR Prcb
Definition: arc.h:497
ULONG_PTR Thread
Definition: arc.h:499
$ULONG LowPart
Definition: ntbasedef.h:569
$ULONG HighPart
Definition: ntbasedef.h:570
KPRIORITY NTAPI KeSetPriorityThread(IN PKTHREAD Thread, IN KPRIORITY Priority)
Definition: thrdobj.c:1327
#define NTAPI
Definition: typedefs.h:36
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define MAXCHAR
Definition: umtypes.h:112
_In_ WDFREQUEST _In_ PIO_STACK_LOCATION Stack
Definition: wdfrequest.h:639
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
#define TIMER_TABLE_SIZE
Definition: ketypes.h:836
unsigned char UCHAR
Definition: xmlstorage.h:181