ReactOS 0.4.15-dev-8127-g6338913
krnlinit.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for krnlinit.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

VOID NTAPI KiInitializeKernel (IN PKPROCESS InitProcess, IN PKTHREAD InitThread, IN PVOID IdleStack, IN PKPRCB Prcb, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 
VOID KiCalculateCpuFrequency (IN PKPRCB Prcb)
 
VOID NTAPI KiInitializeHandBuiltThread (IN PKTHREAD Thread, IN PKPROCESS Process, IN PVOID Stack)
 
DECLSPEC_NORETURN VOID NTAPI KiSystemStartupBootStack (VOID)
 

Variables

ULONG_PTR MainSSDT []
 
UCHAR MainSSPT []
 
BOOLEAN RtlpUse16ByteSLists
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file krnlinit.c.

Function Documentation

◆ KiCalculateCpuFrequency()

VOID KiCalculateCpuFrequency ( IN PKPRCB  Prcb)

Definition at line 35 of file krnlinit.c.

37{
38 if (Prcb->FeatureBits & KF_RDTSC)
39 {
40 ULONG Sample = 0;
41 CPU_INFO CpuInfo;
42 KI_SAMPLE_MAP Samples[10];
43 PKI_SAMPLE_MAP CurrentSample = Samples;
44
45 /* Start sampling loop */
46 for (;;)
47 {
48 /* Do a dummy CPUID to start the sample */
49 KiCpuId(&CpuInfo, 0);
50
51 /* Fill out the starting data */
53 CurrentSample->TSCStart = __rdtsc();
54 CurrentSample->PerfFreq.QuadPart = -50000;
55
56 /* Sleep for this sample */
57 KeStallExecutionProcessor(CurrentSample->PerfFreq.QuadPart * -1 / 10);
58
59 /* Do another dummy CPUID */
60 KiCpuId(&CpuInfo, 0);
61
62 /* Fill out the ending data */
63 CurrentSample->PerfEnd =
64 KeQueryPerformanceCounter(&CurrentSample->PerfFreq);
65 CurrentSample->TSCEnd = __rdtsc();
66
67 /* Calculate the differences */
68 CurrentSample->PerfDelta = CurrentSample->PerfEnd.QuadPart -
69 CurrentSample->PerfStart.QuadPart;
70 CurrentSample->TSCDelta = CurrentSample->TSCEnd -
71 CurrentSample->TSCStart;
72
73 /* Compute CPU Speed */
74 CurrentSample->MHz = (ULONG)((CurrentSample->TSCDelta *
75 CurrentSample->
76 PerfFreq.QuadPart + 500000) /
77 (CurrentSample->PerfDelta *
78 1000000));
79
80 /* Check if this isn't the first sample */
81 if (Sample)
82 {
83 /* Check if we got a good precision within 1MHz */
84 if ((CurrentSample->MHz == CurrentSample[-1].MHz) ||
85 (CurrentSample->MHz == CurrentSample[-1].MHz + 1) ||
86 (CurrentSample->MHz == CurrentSample[-1].MHz - 1))
87 {
88 /* We did, stop sampling */
89 break;
90 }
91 }
92
93 /* Move on */
94 CurrentSample++;
95 Sample++;
96
97 if (Sample == RTL_NUMBER_OF(Samples))
98 {
99 /* No luck. Average the samples and be done */
100 ULONG TotalMHz = 0;
101 while (Sample--)
102 {
103 TotalMHz += Samples[Sample].MHz;
104 }
105 CurrentSample[-1].MHz = TotalMHz / RTL_NUMBER_OF(Samples);
106 DPRINT1("Sampling CPU frequency failed. Using average of %lu MHz\n", CurrentSample[-1].MHz);
107 break;
108 }
109 }
110
111 /* Save the CPU Speed */
112 Prcb->MHz = CurrentSample[-1].MHz;
113 }
114}
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define DPRINT1
Definition: precomp.h:8
VOID NTAPI KeStallExecutionProcessor(IN ULONG MicroSeconds)
Definition: ntoskrnl.c:81
#define NULL
Definition: types.h:112
LARGE_INTEGER NTAPI KeQueryPerformanceCounter(IN PLARGE_INTEGER PerformanceFreq)
Definition: timer.c:138
PPC_QUAL unsigned long long __rdtsc(void)
Definition: intrin_ppc.h:688
#define KF_RDTSC
Definition: ketypes.h:32
LARGE_INTEGER PerfEnd
Definition: ke.h:60
LONGLONG PerfDelta
Definition: ke.h:61
LARGE_INTEGER PerfFreq
Definition: ke.h:62
LONGLONG TSCStart
Definition: ke.h:63
LONGLONG TSCEnd
Definition: ke.h:64
ULONG MHz
Definition: ke.h:66
LARGE_INTEGER PerfStart
Definition: ke.h:59
LONGLONG TSCDelta
Definition: ke.h:65
uint32_t ULONG
Definition: typedefs.h:59
LONGLONG QuadPart
Definition: typedefs.h:114

Referenced by KiSystemStartupBootStack().

◆ KiInitializeHandBuiltThread()

VOID NTAPI KiInitializeHandBuiltThread ( IN PKTHREAD  Thread,
IN PKPROCESS  Process,
IN PVOID  Stack 
)

Definition at line 118 of file krnlinit.c.

122{
123 PKPRCB Prcb = KeGetCurrentPrcb();
124
125 /* Setup the Thread */
127
128 Thread->NextProcessor = Prcb->Number;
129 Thread->IdealProcessor = Prcb->Number;
130 Thread->Priority = HIGH_PRIORITY;
131 Thread->State = Running;
132 Thread->Affinity = (ULONG_PTR)1 << Prcb->Number;
133 Thread->WaitIrql = DISPATCH_LEVEL;
134 Process->ActiveProcessors |= (ULONG_PTR)1 << Prcb->Number;
135
136}
#define ULONG_PTR
Definition: config.h:101
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
_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
#define HIGH_PRIORITY
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1161
@ Running
Definition: ketypes.h:390
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:891
USHORT Number
Definition: ketypes.h:643
uint32_t ULONG_PTR
Definition: typedefs.h:65
_In_ WDFREQUEST _In_ PIO_STACK_LOCATION Stack
Definition: wdfrequest.h:639

Referenced by KiInitializeKernel(), and KiSystemStartupBootStack().

◆ KiInitializeKernel()

VOID NTAPI KiInitializeKernel ( IN PKPROCESS  InitProcess,
IN PKTHREAD  InitThread,
IN PVOID  IdleStack,
IN PKPRCB  Prcb,
IN PLOADER_PARAMETER_BLOCK  LoaderBlock 
)

Definition at line 225 of file krnlinit.c.

230{
231 ULONG_PTR PageDirectory[2];
232 PVOID DpcStack;
233 ULONG i;
234
235 /* Set boot-level flags */
236 KeFeatureBits = Prcb->FeatureBits;
237
238 /* Initialize 8/16 bit SList support */
240
241 /* Set the current MP Master KPRCB to the Boot PRCB */
242 Prcb->MultiThreadSetMaster = Prcb;
243
244 /* Initialize Bugcheck Callback data */
248
249 /* Initialize the Timer Expiration DPC */
252
253 /* Initialize Profiling data */
257
258 /* Loop the timer table */
259 for (i = 0; i < TIMER_TABLE_SIZE; i++)
260 {
261 /* Initialize the list and entries */
263 KiTimerTableListHead[i].Time.HighPart = 0xFFFFFFFF;
265 }
266
267 /* Initialize the Swap event and all swap lists */
272
273 /* Initialize the mutex for generic DPC calls */
275
276 /* Initialize the syscall table */
282
283 /* Copy the the current table into the shadow table for win32k */
287
288 /* Initialize the Idle Process and the Process Listhead */
290 PageDirectory[0] = 0;
291 PageDirectory[1] = 0;
292 KeInitializeProcess(InitProcess,
293 0,
295 PageDirectory,
296 FALSE);
297 InitProcess->QuantumReset = MAXCHAR;
298
299 /* Initialize the startup thread */
300 KiInitializeHandBuiltThread(InitThread, InitProcess, IdleStack);
301
302 /* Initialize the Kernel Executive */
303 ExpInitializeExecutive(0, LoaderBlock);
304
305 /* Calculate the time reciprocal */
309
310 /* Update DPC Values in case they got updated by the executive */
311 Prcb->MaximumDpcQueueDepth = KiMaximumDpcQueueDepth;
312 Prcb->MinimumDpcRate = KiMinimumDpcRate;
313 Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
314
315 /* Allocate the DPC Stack */
316 DpcStack = MmCreateKernelStack(FALSE, 0);
317 if (!DpcStack) KeBugCheckEx(NO_PAGES_AVAILABLE, 1, 0, 0, 0);
318 Prcb->DpcStack = DpcStack;
319}
ULONG_PTR MainSSDT[]
Definition: napi.h:9
VOID NTAPI KiInitializeHandBuiltThread(IN PKTHREAD Thread, IN PKPROCESS Process, IN PVOID Stack)
Definition: krnlinit.c:118
UCHAR MainSSPT[]
Definition: napi.h:15
BOOLEAN RtlpUse16ByteSLists
#define MAXULONG_PTR
Definition: basetsd.h:103
#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 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
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 KF_CMPXCHG16B
Definition: ketypes.h:52
@ SynchronizationEvent
VOID NTAPI ExpInitializeExecutive(IN ULONG Cpu, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: init.c:923
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
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
ULONG KeFeatureBits
Definition: krnlinit.c:22
UCHAR KiTimeIncrementShiftCount
Definition: timerobj.c:19
KSPIN_LOCK BugCheckCallbackLock
Definition: bug.c:24
ULONG KiMinimumDpcRate
Definition: dpc.c:20
LIST_ENTRY KiProfileListHead
Definition: profobj.c:18
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 KeInitializeProcess(struct _KPROCESS *Process, KPRIORITY Priority, KAFFINITY Affinity, PULONG_PTR DirectoryTableBase, IN BOOLEAN Enable)
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
PVOID NTAPI MmCreateKernelStack(BOOLEAN GuiStack, UCHAR Node)
ULONG KeMaximumIncrement
Definition: clock.c:20
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
ULARGE_INTEGER Time
Definition: ketypes.h:779
$ULONG LowPart
Definition: ntbasedef.h:569
$ULONG HighPart
Definition: ntbasedef.h:570
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define MAXCHAR
Definition: umtypes.h:112
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
#define TIMER_TABLE_SIZE
Definition: ketypes.h:848

Referenced by KiSystemStartupBootStack().

◆ KiSystemStartupBootStack()

DECLSPEC_NORETURN VOID NTAPI KiSystemStartupBootStack ( VOID  )

Definition at line 142 of file krnlinit.c.

143{
144 PLOADER_PARAMETER_BLOCK LoaderBlock = KeLoaderBlock; // hack
145 PKPRCB Prcb = KeGetCurrentPrcb();
147 PKPROCESS Process = Thread->ApcState.Process;
148 PVOID KernelStack = (PVOID)KeLoaderBlock->KernelStack;
149
150 /* Set Node Data */
151 Prcb->ParentNode = KeNodeBlock[0];
152 Prcb->ParentNode->ProcessorMask |= Prcb->SetMember;
153
154 /* Initialize the Power Management Support for this PRCB */
155 PoInitializePrcb(Prcb);
156
157 /* Save CPU state */
159
160 /* Get cache line information for this CPU */
162
163 /* Initialize spinlocks and DPC data */
164 KiInitSpinLocks(Prcb, Prcb->Number);
165
166 /* Set up the thread-related fields in the PRCB */
167 Prcb->CurrentThread = Thread;
168 Prcb->NextThread = NULL;
169 Prcb->IdleThread = Thread;
170
171 /* Initialize PRCB pool lookaside pointers */
173
174 /* Lower to APC_LEVEL */
176
177 /* Check if this is the boot cpu */
178 if (Prcb->Number == 0)
179 {
180 /* Initialize the kernel */
181 KiInitializeKernel(Process, Thread, KernelStack, Prcb, LoaderBlock);
182 }
183 else
184 {
185 /* Initialize the startup thread */
187 }
188
189 /* Calculate the CPU frequency */
191
192 /* Raise to Dispatch */
194
195 /* Set the Idle Priority to 0. This will jump into Phase 1 */
197
198 /* If there's no thread scheduled, put this CPU in the Idle summary */
199 KiAcquirePrcbLock(Prcb);
200 if (!Prcb->NextThread) KiIdleSummary |= (ULONG_PTR)1 << Prcb->Number;
201 KiReleasePrcbLock(Prcb);
202
203 /* Raise back to HIGH_LEVEL and clear the PRCB for the loader block */
205 LoaderBlock->Prcb = 0;
206
207 /* Set the priority of this thread to 0 */
209 Thread->Priority = 0;
210
211 /* Force interrupts enabled and lower IRQL back to DISPATCH_LEVEL */
212 _enable();
214
215 /* Set the right wait IRQL */
216 Thread->WaitIrql = DISPATCH_LEVEL;
217
218 /* Jump into the idle loop */
219 KiIdleLoop();
220}
VOID KiCalculateCpuFrequency(IN PKPRCB Prcb)
Definition: krnlinit.c:35
VOID NTAPI KiInitializeKernel(IN PKPROCESS InitProcess, IN PKTHREAD InitThread, IN PVOID IdleStack, IN PKPRCB Prcb, IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: krnlinit.c:225
#define HIGH_LEVEL
Definition: env_spec_w32.h:703
#define APC_LEVEL
Definition: env_spec_w32.h:695
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
KIRQL FASTCALL KfRaiseIrql(IN KIRQL NewIrql)
Definition: pic.c:187
#define KeGetCurrentThread
Definition: hal.h:55
void __cdecl _enable(void)
Definition: intrin_arm.h:373
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
struct _KTHREAD * PKTHREAD
Definition: nt_native.h:28
VOID KiGetCacheInformation(VOID)
Definition: cpu.c:418
KAFFINITY KiIdleSummary
Definition: thrdschd.c:25
DECLSPEC_NORETURN VOID KiIdleLoop(VOID)
Definition: stubs.c:99
PKNODE KeNodeBlock[1]
Definition: krnlinit.c:39
VOID NTAPI KiSaveProcessorControlState(OUT PKPROCESSOR_STATE ProcessorState)
Definition: cpu.c:576
VOID NTAPI KiInitSpinLocks(IN PKPRCB Prcb, IN CCHAR Number)
Definition: krnlinit.c:187
VOID NTAPI PoInitializePrcb(IN PKPRCB Prcb)
Definition: power.c:510
struct _KTHREAD * IdleThread
Definition: ketypes.h:652
UINT64 SetMember
Definition: ketypes.h:662
struct _KTHREAD * CurrentThread
Definition: ketypes.h:650
struct _KTHREAD * NextThread
Definition: ketypes.h:651
struct _KNODE * ParentNode
Definition: ketypes.h:835
KPROCESSOR_STATE ProcessorState
Definition: ketypes.h:663
ULONG_PTR KernelStack
Definition: arc.h:543
ULONG_PTR Prcb
Definition: arc.h:544
ULONG_PTR Thread
Definition: arc.h:546
KPRIORITY NTAPI KeSetPriorityThread(IN PKTHREAD Thread, IN KPRIORITY Priority)
Definition: thrdobj.c:1319

Referenced by KiSwitchToBootStack().

Variable Documentation

◆ MainSSDT

ULONG_PTR MainSSDT[]
extern

Definition at line 9 of file napi.h.

Referenced by KiInitializeKernel(), and KiInitSystem().

◆ MainSSPT

UCHAR MainSSPT[]
extern

Definition at line 15 of file napi.h.

Referenced by KiInitializeKernel(), and KiInitSystem().

◆ RtlpUse16ByteSLists

BOOLEAN RtlpUse16ByteSLists
extern

Referenced by KiInitializeKernel().