ReactOS 0.4.15-dev-7931-gfd331f1
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/krnlinit.c
5 * PURPOSE: Portable part of kernel initialization
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9/* INCLUDES ******************************************************************/
10
11#include <ntoskrnl.h>
12#define NDEBUG
13#include <debug.h>
14#include <internal/napi.h>
15
16/* GLOBALS *******************************************************************/
17
18/* Portable CPU Features and Flags */
24
25/* System call count */
27
28/* ARC Loader Block */
30
31/* PRCB Array */
33
34/* Number of processors */
36
37/* NUMA Node Support */
42
43/* Initial Process and Thread */
46
47/* System-defined Spinlocks */
65
66/* FUNCTIONS *****************************************************************/
67
68CODE_SEG("INIT")
69VOID
72{
73 ULONG i;
74
75 /* Initialize Bugcheck Callback data */
79
80 /* Initialize the Timer Expiration DPC */
83
84 /* Initialize Profiling data */
88
89 /* Loop the timer table */
90 for (i = 0; i < TIMER_TABLE_SIZE; i++)
91 {
92 /* Initialize the list and entries */
94 KiTimerTableListHead[i].Time.HighPart = 0xFFFFFFFF;
96 }
97
98 /* Initialize the Swap event and all swap lists */
103
104 /* Initialize the mutex for generic DPC calls */
106
107 /* Initialize the syscall table */
113
114 /* Copy the the current table into the shadow table for win32k */
118}
119
120CODE_SEG("INIT")
122NTAPI
125{
126 LARGE_INTEGER Reciprocal = {{0, 0}};
127 LONG BitCount = 0, Remainder = 1;
128
129 /* Start by calculating the remainder */
130 while (Reciprocal.HighPart >= 0)
131 {
132 /* Increase the loop (bit) count */
133 BitCount++;
134
135 /* Calculate the current fraction */
136 Reciprocal.HighPart = (Reciprocal.HighPart << 1) |
137 (Reciprocal.LowPart >> 31);
138 Reciprocal.LowPart <<= 1;
139
140 /* Double the remainder and see if we went past the divisor */
141 Remainder <<= 1;
142 if (Remainder >= Divisor)
143 {
144 /* Set the low-bit and calculate the new remainder */
146 Reciprocal.LowPart |= 1;
147 }
148 }
149
150 /* Check if we have a remainder */
151 if (Remainder)
152 {
153 /* Check if the current fraction value is too large */
154 if ((Reciprocal.LowPart == 0xFFFFFFFF) &&
155 (Reciprocal.HighPart == (LONG)0xFFFFFFFF))
156 {
157 /* Set the high bit and reduce the bit count */
158 Reciprocal.LowPart = 0;
159 Reciprocal.HighPart = 0x80000000;
160 BitCount--;
161 }
162 else
163 {
164 /* Check if only the lowest bits got too large */
165 if (Reciprocal.LowPart == 0xFFFFFFFF)
166 {
167 /* Reset them and increase the high bits instead */
168 Reciprocal.LowPart = 0;
169 Reciprocal.HighPart++;
170 }
171 else
172 {
173 /* All is well, increase the low bits */
174 Reciprocal.LowPart++;
175 }
176 }
177 }
178
179 /* Now calculate the actual shift and return the reciprocal */
180 *Shift = (UCHAR)BitCount - 64;
181 return Reciprocal;
182}
183
184CODE_SEG("INIT")
185VOID
186NTAPI
189{
190 ULONG i;
191
192 /* Initialize Dispatcher Fields */
193 Prcb->QueueIndex = 1;
194 Prcb->ReadySummary = 0;
195 Prcb->DeferredReadyListHead.Next = NULL;
196 for (i = 0; i < MAXIMUM_PRIORITY; i++)
197 {
198 /* Initialize the ready list */
199 InitializeListHead(&Prcb->DispatcherReadyListHead[i]);
200 }
201
202 /* Initialize DPC Fields */
203 InitializeListHead(&Prcb->DpcData[DPC_NORMAL].DpcListHead);
204 KeInitializeSpinLock(&Prcb->DpcData[DPC_NORMAL].DpcLock);
205 Prcb->DpcData[DPC_NORMAL].DpcQueueDepth = 0;
206 Prcb->DpcData[DPC_NORMAL].DpcCount = 0;
207 Prcb->DpcRoutineActive = FALSE;
208 Prcb->MaximumDpcQueueDepth = KiMaximumDpcQueueDepth;
209 Prcb->MinimumDpcRate = KiMinimumDpcRate;
210 Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
211 KeInitializeDpc(&Prcb->CallDpc, NULL, NULL);
212 KeSetTargetProcessorDpc(&Prcb->CallDpc, Number);
213 KeSetImportanceDpc(&Prcb->CallDpc, HighImportance);
214
215 /* Initialize the Wait List Head */
216 InitializeListHead(&Prcb->WaitListHead);
217
218 /* Initialize Queued Spinlocks */
219 Prcb->LockQueue[LockQueueDispatcherLock].Next = NULL;
220 Prcb->LockQueue[LockQueueDispatcherLock].Lock = &KiDispatcherLock;
221 Prcb->LockQueue[LockQueueExpansionLock].Next = NULL;
222 Prcb->LockQueue[LockQueueExpansionLock].Lock = NULL;
223 Prcb->LockQueue[LockQueuePfnLock].Next = NULL;
224 Prcb->LockQueue[LockQueuePfnLock].Lock = &MmPfnLock;
225 Prcb->LockQueue[LockQueueSystemSpaceLock].Next = NULL;
226 Prcb->LockQueue[LockQueueSystemSpaceLock].Lock = &MmSystemSpaceLock;
227 Prcb->LockQueue[LockQueueBcbLock].Next = NULL;
228 Prcb->LockQueue[LockQueueBcbLock].Lock = &CcBcbSpinLock;
229 Prcb->LockQueue[LockQueueMasterLock].Next = NULL;
230 Prcb->LockQueue[LockQueueMasterLock].Lock = &CcMasterSpinLock;
231 Prcb->LockQueue[LockQueueVacbLock].Next = NULL;
232 Prcb->LockQueue[LockQueueVacbLock].Lock = &CcVacbSpinLock;
233 Prcb->LockQueue[LockQueueWorkQueueLock].Next = NULL;
234 Prcb->LockQueue[LockQueueWorkQueueLock].Lock = &CcWorkQueueSpinLock;
235 Prcb->LockQueue[LockQueueNonPagedPoolLock].Next = NULL;
236 Prcb->LockQueue[LockQueueNonPagedPoolLock].Lock = &NonPagedPoolLock;
237 Prcb->LockQueue[LockQueueMmNonPagedPoolLock].Next = NULL;
238 Prcb->LockQueue[LockQueueMmNonPagedPoolLock].Lock = &MmNonPagedPoolLock;
239 Prcb->LockQueue[LockQueueIoCancelLock].Next = NULL;
240 Prcb->LockQueue[LockQueueIoCancelLock].Lock = &IopCancelSpinLock;
241 Prcb->LockQueue[LockQueueIoVpbLock].Next = NULL;
242 Prcb->LockQueue[LockQueueIoVpbLock].Lock = &IopVpbSpinLock;
243 Prcb->LockQueue[LockQueueIoDatabaseLock].Next = NULL;
244 Prcb->LockQueue[LockQueueIoDatabaseLock].Lock = &IopDatabaseLock;
245 Prcb->LockQueue[LockQueueIoCompletionLock].Next = NULL;
246 Prcb->LockQueue[LockQueueIoCompletionLock].Lock = &IopCompletionLock;
247 Prcb->LockQueue[LockQueueNtfsStructLock].Next = NULL;
248 Prcb->LockQueue[LockQueueNtfsStructLock].Lock = &NtfsStructLock;
249 Prcb->LockQueue[LockQueueAfdWorkQueueLock].Next = NULL;
250 Prcb->LockQueue[LockQueueAfdWorkQueueLock].Lock = &AfdWorkQueueSpinLock;
251 Prcb->LockQueue[LockQueueUnusedSpare16].Next = NULL;
252 Prcb->LockQueue[LockQueueUnusedSpare16].Lock = NULL;
253
254 /* Loop timer locks (shared amongst all CPUs) */
255 for (i = 0; i < LOCK_QUEUE_TIMER_TABLE_LOCKS; i++)
256 {
257 /* Setup the Queued Spinlock (done only once by the boot CPU) */
258 if (!Number)
260
261 /* Initialize the lock */
262 Prcb->LockQueue[LockQueueTimerTableLock + i].Next = NULL;
263 Prcb->LockQueue[LockQueueTimerTableLock + i].Lock =
265 }
266
267 /* Initialize the PRCB lock */
268 KeInitializeSpinLock(&Prcb->PrcbLock);
269
270 /* Check if this is the boot CPU */
271 if (!Number)
272 {
273 /* Initialize the lock themselves */
290 }
291}
292
293CODE_SEG("INIT")
295NTAPI
297{
298 /* Check if Threaded DPCs are enabled */
300 {
301 /* FIXME: TODO */
302 DPRINT1("Threaded DPCs not yet supported\n");
303 }
304
305 /* Initialize non-portable parts of the kernel */
307 return TRUE;
308}
#define CODE_SEG(...)
unsigned char BOOLEAN
ULONG_PTR MainSSDT[]
Definition: napi.h:9
UCHAR MainSSPT[]
Definition: napi.h:15
#define DPRINT1
Definition: precomp.h:8
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
ULONG_PTR KAFFINITY
Definition: compat.h:85
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
VOID NTAPI KeSetImportanceDpc(IN PKDPC Dpc, IN KDPC_IMPORTANCE Importance)
Definition: dpc.c:958
ULONG KSPIN_LOCK
Definition: env_spec_w32.h:72
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define KeInitializeSpinLock(sl)
Definition: env_spec_w32.h:604
#define MAXIMUM_PROCESSORS
Definition: rwlock.h:5
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
#define MAXIMUM_PRIORITY
KSPIN_LOCK IopCompletionLock
Definition: krnlinit.c:60
KSPIN_LOCK AfdWorkQueueSpinLock
Definition: krnlinit.c:62
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
PKPRCB KiProcessorBlock[MAXIMUM_PROCESSORS]
Definition: krnlinit.c:32
EPROCESS KiInitialProcess
Definition: krnlinit.c:45
KSPIN_LOCK MmPfnLock
Definition: krnlinit.c:49
KSPIN_LOCK CcMasterSpinLock
Definition: krnlinit.c:52
KSPIN_LOCK KiTimerTableLock[LOCK_QUEUE_TIMER_TABLE_LOCKS]
Definition: krnlinit.c:63
KSPIN_LOCK IopDatabaseLock
Definition: krnlinit.c:59
KSPIN_LOCK KiReverseStallIpiLock
Definition: krnlinit.c:64
BOOLEAN NTAPI KeInitSystem(VOID)
Definition: krnlinit.c:296
PLOADER_PARAMETER_BLOCK KeLoaderBlock
Definition: krnlinit.c:29
ETHREAD KiInitialThread
Definition: krnlinit.c:44
KSPIN_LOCK NonPagedPoolLock
Definition: krnlinit.c:55
KSPIN_LOCK MmNonPagedPoolLock
Definition: krnlinit.c:56
KSPIN_LOCK CcVacbSpinLock
Definition: krnlinit.c:53
KAFFINITY KeActiveProcessors
Definition: krnlinit.c:23
KSPIN_LOCK NtfsStructLock
Definition: krnlinit.c:61
UCHAR KeProcessNodeSeed
Definition: krnlinit.c:41
KSPIN_LOCK KiDispatcherLock
Definition: krnlinit.c:48
KSPIN_LOCK IopVpbSpinLock
Definition: krnlinit.c:58
KSPIN_LOCK MmSystemSpaceLock
Definition: krnlinit.c:50
KSPIN_LOCK IopCancelSpinLock
Definition: krnlinit.c:57
USHORT KeProcessorLevel
Definition: krnlinit.c:20
VOID NTAPI KiInitSystem(VOID)
Definition: krnlinit.c:71
KSPIN_LOCK CcWorkQueueSpinLock
Definition: krnlinit.c:54
UCHAR KeNumberNodes
Definition: krnlinit.c:40
USHORT KeProcessorRevision
Definition: krnlinit.c:21
KSPIN_LOCK CcBcbSpinLock
Definition: krnlinit.c:51
USHORT KeProcessorArchitecture
Definition: krnlinit.c:19
KNODE KiNode0
Definition: krnlinit.c:38
@ SynchronizationEvent
KTIMER_TABLE_ENTRY KiTimerTableListHead[TIMER_TABLE_SIZE]
Definition: timerobj.c:17
LIST_ENTRY KeBugcheckCallbackListHead
Definition: bug.c:22
LIST_ENTRY KiProcessOutSwapListHead
Definition: ke.h:136
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
BOOLEAN KeThreadDpcEnable
Definition: dpc.c:23
KDPC KiTimerExpireDpc
Definition: dpc.c:25
ULONG KeFeatureBits
Definition: krnlinit.c:22
KSPIN_LOCK BugCheckCallbackLock
Definition: bug.c:24
VOID NTAPI KiInitMachineDependent(VOID)
Definition: kiinit.c:48
ULONG KiMinimumDpcRate
Definition: dpc.c:20
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
KSPIN_LOCK KiProfileLock
Definition: profobj.c:20
LIST_ENTRY KiStackInSwapListHead
Definition: procobj.c:20
VOID NTAPI KiInitSpinLocks(IN PKPRCB Prcb, IN CCHAR Number)
Definition: krnlinit.c:187
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
#define NUMBER_OF_SYSCALLS
Definition: napi.h:20
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:207
long LONG
Definition: pedump.c:60
unsigned short USHORT
Definition: pedump.c:61
KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTable[SSDT_MAX_ENTRIES]
Definition: procobj.c:23
KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTableShadow[SSDT_MAX_ENTRIES]
Definition: procobj.c:24
base of all file and directory entries
Definition: entries.h:83
ULARGE_INTEGER Time
Definition: ketypes.h:779
$ULONG LowPart
Definition: ntbasedef.h:569
$ULONG HighPart
Definition: ntbasedef.h:570
#define NTAPI
Definition: typedefs.h:36
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define IN
Definition: typedefs.h:39
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
char CCHAR
Definition: typedefs.h:51
ULONG LowPart
Definition: typedefs.h:106
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
@ HighImportance
Definition: ketypes.h:695
#define DPC_NORMAL
#define LOCK_QUEUE_TIMER_TABLE_LOCKS
#define TIMER_TABLE_SIZE
Definition: ketypes.h:848
@ LockQueueNtfsStructLock
Definition: ketypes.h:670
@ LockQueueExpansionLock
Definition: ketypes.h:659
@ LockQueueTimerTableLock
Definition: ketypes.h:675
@ LockQueueWorkQueueLock
Definition: ketypes.h:666
@ LockQueueUnusedSpare16
Definition: ketypes.h:674
@ LockQueueNonPagedPoolLock
Definition: ketypes.h:664
@ LockQueueSystemSpaceLock
Definition: ketypes.h:661
@ LockQueueMasterLock
Definition: ketypes.h:663
@ LockQueueDispatcherLock
Definition: ketypes.h:658
@ LockQueueIoCancelLock
Definition: ketypes.h:665
@ LockQueuePfnLock
Definition: ketypes.h:660
@ LockQueueIoDatabaseLock
Definition: ketypes.h:668
@ LockQueueMmNonPagedPoolLock
Definition: ketypes.h:673
@ LockQueueBcbLock
Definition: ketypes.h:672
@ LockQueueIoVpbLock
Definition: ketypes.h:667
@ LockQueueIoCompletionLock
Definition: ketypes.h:669
@ LockQueueAfdWorkQueueLock
Definition: ketypes.h:671
@ LockQueueVacbLock
Definition: ketypes.h:662
_In_ ULONG Shift
Definition: rtlfuncs.h:2681
_In_ LARGE_INTEGER Divisor
Definition: rtlfuncs.h:3044
_In_ LARGE_INTEGER _Out_opt_ PLARGE_INTEGER Remainder
Definition: rtlfuncs.h:3045
unsigned char UCHAR
Definition: xmlstorage.h:181