ReactOS 0.4.17-dev-683-g0dafdc5
cpu.c File Reference
#include <ntoskrnl.h>
#include <x86x64/Cpuid.h>
#include <x86x64/Msr.h>
#include <debug.h>
Include dependency graph for cpu.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

VOID NTAPI KiSetProcessorType (VOID)
 
ULONG64 NTAPI KiGetFeatureBits (VOID)
 Evaluates the KeFeatureFlag bits for the current CPU.
 
VOID NTAPI KiGetCacheInformation (VOID)
 
VOID NTAPI KeFlushCurrentTb (VOID)
 
VOID NTAPI KiRestoreProcessorControlState (PKPROCESSOR_STATE ProcessorState)
 
VOID NTAPI KiSaveProcessorControlState (OUT PKPROCESSOR_STATE ProcessorState)
 
VOID NTAPI KiSaveProcessorState (_In_ PKTRAP_FRAME TrapFrame, _In_ PKEXCEPTION_FRAME ExceptionFrame)
 
VOID NTAPI KiRestoreProcessorState (_Out_ PKTRAP_FRAME TrapFrame, _Out_ PKEXCEPTION_FRAME ExceptionFrame)
 
VOID NTAPI KeFlushEntireTb (IN BOOLEAN Invalid, IN BOOLEAN AllProcessors)
 
NTSTATUS NTAPI KxSaveFloatingPointState (OUT PKFLOATING_SAVE FloatingState)
 
NTSTATUS NTAPI KxRestoreFloatingPointState (IN PKFLOATING_SAVE FloatingState)
 
BOOLEAN NTAPI KeInvalidateAllCaches (VOID)
 
ULONG NTAPI KeGetRecommendedSharedDataAlignment (VOID)
 
VOID __cdecl KeSaveStateForHibernate (IN PKPROCESSOR_STATE State)
 
VOID NTAPI KeSetDmaIoCoherency (IN ULONG Coherency)
 

Variables

KTSS64 KiBootTss
 
ULONG KeI386CpuType
 
ULONG KeI386CpuStep
 
ULONG KeI386MachineType
 
ULONG KeI386NpxPresent = 1
 
ULONG KeLargestCacheLine = 0x40
 
ULONG KiDmaIoCoherency = 0
 
BOOLEAN KiSMTProcessorsPresent
 
volatile LONG KiTbFlushTimeStamp
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 16 of file cpu.c.

Function Documentation

◆ KeFlushCurrentTb()

VOID NTAPI KeFlushCurrentTb ( VOID  )

Definition at line 437 of file cpu.c.

438{
439 /* Flush the TLB by resetting CR3 */
441}
__INTRIN_INLINE unsigned long __readcr3(void)
Definition: intrin_x86.h:1850
__INTRIN_INLINE void __writecr3(unsigned int Data)
Definition: intrin_x86.h:1826

Referenced by KeFlushEntireTb(), and KiFlushTargetEntireTb().

◆ KeFlushEntireTb()

VOID NTAPI KeFlushEntireTb ( IN BOOLEAN  Invalid,
IN BOOLEAN  AllProcessors 
)

Definition at line 563 of file cpu.c.

565{
567
568 // FIXME: halfplemented
569 /* Raise the IRQL for the TB Flush */
571
572 /* Flush the TB for the Current CPU, and update the flush stamp */
574
575 /* Update the flush stamp and return to original IRQL */
578
579}
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
KIRQL NTAPI KeRaiseIrqlToSynchLevel(VOID)
Definition: pic.c:156
#define InterlockedExchangeAdd
Definition: interlocked.h:196
VOID NTAPI KeFlushCurrentTb(VOID)
Definition: cpu.c:437
volatile LONG KiTbFlushTimeStamp
Definition: cpu.c:34
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:778

Referenced by MiAllocatePoolPages(), MiDeleteSystemPageableVm(), MiDereferenceSession(), MiMapLockedPagesInUserSpace(), MiProtectFreeNonPagedPool(), MiSetSystemCodeProtection(), MmFreeSpecialPool(), MmMapIoSpace(), MmUnmapIoSpace(), and MmUnmapReservedMapping().

◆ KeGetRecommendedSharedDataAlignment()

◆ KeInvalidateAllCaches()

BOOLEAN NTAPI KeInvalidateAllCaches ( VOID  )

Definition at line 599 of file cpu.c.

600{
601 /* Invalidate all caches */
602 __wbinvd();
603 return TRUE;
604}
#define TRUE
Definition: types.h:120
PPC_QUAL void __wbinvd(void)
Definition: intrin_ppc.h:759

◆ KeSaveStateForHibernate()

VOID __cdecl KeSaveStateForHibernate ( IN PKPROCESSOR_STATE  State)

Definition at line 622 of file cpu.c.

623{
624 /* Capture the context */
625 RtlCaptureContext(&State->ContextFrame);
626
627 /* Capture the control state */
629}
NTSYSAPI VOID NTAPI RtlCaptureContext(_Out_ PCONTEXT ContextRecord)
VOID NTAPI KiSaveProcessorControlState(OUT PKPROCESSOR_STATE ProcessorState)
Definition: cpu.c:487

◆ KeSetDmaIoCoherency()

VOID NTAPI KeSetDmaIoCoherency ( IN ULONG  Coherency)

Definition at line 636 of file cpu.c.

637{
638 /* Save the coherency globally */
639 KiDmaIoCoherency = Coherency;
640}
ULONG KiDmaIoCoherency
Definition: cpu.c:30

◆ KiGetCacheInformation()

VOID NTAPI KiGetCacheInformation ( VOID  )

Definition at line 334 of file cpu.c.

335{
336 PKIPCR Pcr = (PKIPCR)KeGetPcr();
337 ULONG CacheRequests = 0, i;
338 ULONG CurrentRegister;
339 UCHAR RegisterByte;
340 BOOLEAN FirstPass = TRUE;
341 CPU_INFO CpuInfo;
342
343 /* Set default L2 size */
344 Pcr->SecondLevelCacheSize = 0;
345
346 /* Check the Vendor ID */
347 switch (Pcr->Prcb.CpuVendor)
348 {
349 /* Handle Intel case */
350 case CPU_INTEL:
351
352 /*Check if we support CPUID 2 */
353 KiCpuId(&CpuInfo, 0);
354 if (CpuInfo.Eax >= 2)
355 {
356 /* We need to loop for the number of times CPUID will tell us to */
357 do
358 {
359 /* Do the CPUID call */
360 KiCpuId(&CpuInfo, 2);
361
362 /* Check if it was the first call */
363 if (FirstPass)
364 {
365 /*
366 * The number of times to loop is the first byte. Read
367 * it and then destroy it so we don't get confused.
368 */
369 CacheRequests = CpuInfo.Eax & 0xFF;
370 CpuInfo.Eax &= 0xFFFFFF00;
371
372 /* Don't go over this again */
373 FirstPass = FALSE;
374 }
375
376 /* Loop all 4 registers */
377 for (i = 0; i < 4; i++)
378 {
379 /* Get the current register */
380 CurrentRegister = CpuInfo.AsUINT32[i];
381
382 /*
383 * If the upper bit is set, then this register should
384 * be skipped.
385 */
386 if (CurrentRegister & 0x80000000) continue;
387
388 /* Keep looping for every byte inside this register */
389 while (CurrentRegister)
390 {
391 /* Read a byte, skip a byte. */
392 RegisterByte = (UCHAR)(CurrentRegister & 0xFF);
393 CurrentRegister >>= 8;
394 if (!RegisterByte) continue;
395
396 /*
397 * Valid values are from 0x40 (0 bytes) to 0x49
398 * (32MB), or from 0x80 to 0x89 (same size but
399 * 8-way associative.
400 */
401 if (((RegisterByte > 0x40) &&
402 (RegisterByte <= 0x49)) ||
403 ((RegisterByte > 0x80) &&
404 (RegisterByte <= 0x89)))
405 {
406 /* Mask out only the first nibble */
407 RegisterByte &= 0x0F;
408
409 /* Set the L2 Cache Size */
410 Pcr->SecondLevelCacheSize = 0x10000 <<
411 RegisterByte;
412 }
413 }
414 }
415 } while (--CacheRequests);
416 }
417 break;
418
419 case CPU_AMD:
420
421 /* Check if we support CPUID 0x80000006 */
422 KiCpuId(&CpuInfo, 0x80000000);
423 if (CpuInfo.Eax >= 6)
424 {
425 /* Get 2nd level cache and tlb size */
426 KiCpuId(&CpuInfo, 0x80000006);
427
428 /* Set the L2 Cache Size */
429 Pcr->SecondLevelCacheSize = (CpuInfo.Ecx & 0xFFFF0000) >> 6;
430 }
431 break;
432 }
433}
unsigned char BOOLEAN
Definition: actypes.h:127
#define FALSE
Definition: types.h:117
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
struct _KIPCR * PKIPCR
@ CPU_INTEL
Definition: ketypes.h:100
@ CPU_AMD
Definition: ketypes.h:99
#define KeGetPcr()
Definition: ketypes.h:81
KPRCB Prcb
Definition: ketypes.h:1003
ULONG SecondLevelCacheSize
Definition: ketypes.h:995
UCHAR CpuVendor
Definition: ketypes.h:704
unsigned char UCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
ULONG Eax
Definition: ketypes.h:401
UINT32 AsUINT32[4]
Definition: ketypes.h:398
ULONG Ecx
Definition: ketypes.h:403

◆ KiGetFeatureBits()

ULONG64 NTAPI KiGetFeatureBits ( VOID  )

Evaluates the KeFeatureFlag bits for the current CPU.

Returns
The feature flags for this CPU.
See also
https://www.geoffchappell.com/studies/windows/km/ntoskrnl/structs/kprcb/featurebits.htm
Todo:
  • KF_VIRT_FIRMWARE_ENABLED 0x08000000 (see notes from Geoff Chappell)
  • KF_FPU_LEAKAGE 0x0000020000000000ULL
  • KF_CAT 0x0000100000000000ULL
  • KF_CET_SS 0x0000400000000000ULL

Definition at line 73 of file cpu.c.

74{
75 PKPRCB Prcb = KeGetCurrentPrcb();
76 ULONG Vendor;
77 ULONG64 FeatureBits = 0;
80 CPUID_EXTENDED_FUNCTION_REGS extendedFunction;
81
82 /* Get the Vendor ID */
83 Vendor = Prcb->CpuVendor;
84
85 /* Make sure we got a valid vendor ID at least. */
86 if (Vendor == CPU_UNKNOWN) return FeatureBits;
87
88 /* Get signature CPUID for the maximum function */
90
91 /* Get the CPUID Info. */
93
94 /* Set the initial APIC ID */
95 Prcb->InitialApicId = (UCHAR)VersionInfo.Ebx.Bits.InitialLocalApicId;
96
97 /* Convert all CPUID Feature bits into our format */
98 if (VersionInfo.Edx.Bits.VME) FeatureBits |= KF_CR4;
99 if (VersionInfo.Edx.Bits.PSE) FeatureBits |= KF_LARGE_PAGE | KF_CR4;
100 if (VersionInfo.Edx.Bits.TSC) FeatureBits |= KF_RDTSC;
101 if (VersionInfo.Edx.Bits.CX8) FeatureBits |= KF_CMPXCHG8B;
102 if (VersionInfo.Edx.Bits.SEP) FeatureBits |= KF_FAST_SYSCALL;
103 if (VersionInfo.Edx.Bits.MTRR) FeatureBits |= KF_MTRR;
104 if (VersionInfo.Edx.Bits.PGE) FeatureBits |= KF_GLOBAL_PAGE | KF_CR4;
105 if (VersionInfo.Edx.Bits.CMOV) FeatureBits |= KF_CMOV;
106 if (VersionInfo.Edx.Bits.PAT) FeatureBits |= KF_PAT;
107 if (VersionInfo.Edx.Bits.DS) FeatureBits |= KF_DTS;
108 if (VersionInfo.Edx.Bits.MMX) FeatureBits |= KF_MMX;
109 if (VersionInfo.Edx.Bits.FXSR) FeatureBits |= KF_FXSR;
110 if (VersionInfo.Edx.Bits.SSE) FeatureBits |= KF_XMMI;
111 if (VersionInfo.Edx.Bits.SSE2) FeatureBits |= KF_XMMI64;
112
113 if (VersionInfo.Ecx.Bits.SSE3) FeatureBits |= KF_SSE3;
114 if (VersionInfo.Ecx.Bits.SSSE3) FeatureBits |= KF_SSSE3;
115 if (VersionInfo.Ecx.Bits.CMPXCHG16B) FeatureBits |= KF_CMPXCHG16B;
116 if (VersionInfo.Ecx.Bits.SSE4_1) FeatureBits |= KF_SSE4_1;
117 if (VersionInfo.Ecx.Bits.SSE4_2) FeatureBits |= KF_SSE4_2;
118 if (VersionInfo.Ecx.Bits.XSAVE) FeatureBits |= KF_XSTATE;
119 if (VersionInfo.Ecx.Bits.RDRAND) FeatureBits |= KF_RDRAND;
120 if (VersionInfo.Ecx.Bits.AVX) FeatureBits |= KF_AVX;
121
122 /* Check if the CPU has hyper-threading */
123 if (VersionInfo.Edx.Bits.HTT)
124 {
125 /* Set the number of logical CPUs */
127 VersionInfo.Ebx.Bits.MaximumAddressableIdsForLogicalProcessors;
129 {
130 /* We're on dual-core */
132 }
133 }
134 else
135 {
136 /* We only have a single CPU */
138 }
139
140 /* Check if CPUID_THERMAL_POWER_MANAGEMENT (0x06) is supported */
142 {
143 /* Read CPUID_THERMAL_POWER_MANAGEMENT */
146
147 if (PowerInfo.Undoc.Ecx.ACNT2) FeatureBits |= KF_ACNT2;
148 }
149
150 /* Check if CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS (0x07) is supported */
152 {
153 /* Read CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS */
155 __cpuidex(ExtFlags.AsInt32,
158
159 if (ExtFlags.Ebx.Bits.SMEP) FeatureBits |= KF_SMEP;
160 if (ExtFlags.Ebx.Bits.FSGSBASE) FeatureBits |= KF_RDWRFSGSBASE;
161 if (ExtFlags.Ebx.Bits.SMAP) FeatureBits |= KF_SMAP;
162 if (ExtFlags.Ebx.Bits.AVX2) FeatureBits |= KF_AVX2;
163 if (ExtFlags.Ebx.Bits.AVX512F) FeatureBits |= KF_AVX512F;
164 }
165
166 /* Check if CPUID_EXTENDED_STATE (0x0D) is supported */
167 if (signature.MaxLeaf >= CPUID_EXTENDED_STATE)
168 {
169 /* Read CPUID_EXTENDED_STATE */
171 __cpuidex(ExtStateSub.AsInt32,
174
175 if (ExtStateSub.Eax.Bits.XSAVEOPT) FeatureBits |= KF_XSAVEOPT;
176 if (ExtStateSub.Eax.Bits.XSAVES) FeatureBits |= KF_XSAVES;
177 }
178
179 /* Check extended cpuid features */
180 __cpuid(extendedFunction.AsInt32, CPUID_EXTENDED_FUNCTION);
181 if ((extendedFunction.MaxLeaf & 0xffffff00) == 0x80000000)
182 {
183 /* Check if CPUID_EXTENDED_CPU_SIG (0x80000001) is supported */
184 if (extendedFunction.MaxLeaf >= CPUID_EXTENDED_CPU_SIG)
185 {
186 /* Read CPUID_EXTENDED_CPU_SIG */
189
190 /* Check if NX-bit is supported */
191 if (ExtSig.Intel.Edx.Bits.NX) FeatureBits |= KF_NX_BIT;
192 if (ExtSig.Intel.Edx.Bits.Page1GB) FeatureBits |= KF_HUGEPAGE;
193 if (ExtSig.Intel.Edx.Bits.RDTSCP) FeatureBits |= KF_RDTSCP;
194
195 /* AMD specific */
196 if (Vendor == CPU_AMD)
197 {
198 if (ExtSig.Amd.Edx.Bits.ThreeDNow) FeatureBits |= KF_3DNOW;
199 }
200 }
201 }
202
203 /* Vendor specific */
204 if (Vendor == CPU_INTEL)
205 {
206 FeatureBits |= KF_GENUINE_INTEL;
207
208 /* Check for models that support LBR */
209 if (VersionInfo.Eax.Bits.FamilyId == 6)
210 {
211 if ((VersionInfo.Eax.Bits.Model == 15) ||
212 (VersionInfo.Eax.Bits.Model == 22) ||
213 (VersionInfo.Eax.Bits.Model == 23) ||
214 (VersionInfo.Eax.Bits.Model == 26))
215 {
216 FeatureBits |= KF_BRANCH;
217 }
218 }
219
220 /* Check if VMX is available */
221 if (VersionInfo.Ecx.Bits.VMX)
222 {
223 /* Read PROCBASED ctls and check if secondary are allowed */
226 if (ProcBasedCtls.Bits.Allowed1.ActivateSecondaryControls)
227 {
228 /* Read secondary controls and check if EPT is allowed */
231 if (ProcBasedCtls2.Bits.Allowed1.EPT)
232 FeatureBits |= KF_SLAT;
233 }
234 }
235 }
236 else if (Vendor == CPU_AMD)
237 {
238 FeatureBits |= KF_AUTHENTICAMD;
239 FeatureBits |= KF_BRANCH;
240
241 /* Check extended cpuid features */
242 if ((extendedFunction.MaxLeaf & 0xffffff00) == 0x80000000)
243 {
244 /* Check if CPUID_AMD_SVM_FEATURES (0x8000000A) is supported */
245 if (extendedFunction.MaxLeaf >= CPUID_AMD_SVM_FEATURES)
246 {
247 /* Read CPUID_AMD_SVM_FEATURES and check if Nested Paging is available */
248 CPUID_AMD_SVM_FEATURES_REGS SvmFeatures;
250 if (SvmFeatures.Edx.Bits.NP) FeatureBits |= KF_SLAT;
251 }
252 }
253 }
254
255 /* Return the Feature Bits */
256 return FeatureBits;
257}
#define MSR_IA32_VMX_PROCBASED_CTLS
#define MSR_IA32_VMX_PROCBASED_CTLS2
#define CPUID_AMD_SVM_FEATURES
Definition: Cpuid.h:160
#define CPUID_EXTENDED_FUNCTION
Definition: Cpuid.h:3802
#define CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS
Definition: Cpuid.h:1301
#define CPUID_SIGNATURE
Definition: Cpuid.h:45
#define CPUID_VERSION_INFO
Definition: Cpuid.h:81
#define CPUID_EXTENDED_STATE
Definition: Cpuid.h:1948
#define CPUID_THERMAL_POWER_MANAGEMENT
Definition: Cpuid.h:1114
#define CPUID_EXTENDED_STATE_SUB_LEAF
Definition: Cpuid.h:2063
#define CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_SUB_LEAF_INFO
Definition: Cpuid.h:1306
#define CPUID_EXTENDED_CPU_SIG
Definition: Cpuid.h:3825
OSVERSIONINFOW VersionInfo
Definition: wkssvc.c:40
PPC_QUAL void __cpuid(int CPUInfo[], const int InfoType)
Definition: intrin_ppc.h:682
PPC_QUAL unsigned long long __readmsr()
Definition: intrin_ppc.h:741
__INTRIN_INLINE void __cpuidex(int CPUInfo[4], int InfoType, int ECXValue)
Definition: intrin_x86.h:1680
if(dx< 0)
Definition: linetemp.h:194
unsigned __int64 ULONG64
Definition: imports.h:198
#define KF_SSE4_2
Definition: ketypes.h:74
#define KF_SMEP
Definition: ketypes.h:31
#define KF_SSSE3
Definition: ketypes.h:72
#define KF_MTRR
Definition: ketypes.h:37
#define KF_HUGEPAGE
Definition: ketypes.h:67
#define KF_XSTATE
Definition: ketypes.h:55
#define KF_DTS
Definition: ketypes.h:40
#define KF_CMPXCHG16B
Definition: ketypes.h:52
#define KF_CR4
Definition: ketypes.h:33
#define KF_AUTHENTICAMD
Definition: ketypes.h:53
#define KF_XMMI64
Definition: ketypes.h:48
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1197
#define KF_CMOV
Definition: ketypes.h:34
#define KF_ACNT2
Definition: ketypes.h:54
#define KF_SSE4_1
Definition: ketypes.h:73
#define KF_RDWRFSGSBASE
Definition: ketypes.h:60
#define KF_CMPXCHG8B
Definition: ketypes.h:38
#define KF_RDTSC
Definition: ketypes.h:32
#define KF_FAST_SYSCALL
Definition: ketypes.h:43
#define KF_3DNOW
Definition: ketypes.h:45
#define KF_NX_BIT
Definition: ketypes.h:61
#define KF_FXSR
Definition: ketypes.h:42
#define KF_RDRAND
Definition: ketypes.h:64
@ CPU_UNKNOWN
Definition: ketypes.h:98
#define KF_XSAVES
Definition: ketypes.h:68
#define KF_LARGE_PAGE
Definition: ketypes.h:36
#define KF_BRANCH
Definition: ketypes.h:49
#define KF_AVX
Definition: ketypes.h:77
#define KF_GENUINE_INTEL
Definition: ketypes.h:56
#define KF_XMMI
Definition: ketypes.h:44
#define KF_MMX
Definition: ketypes.h:39
#define KF_XSAVEOPT
Definition: ketypes.h:47
#define KF_SSE3
Definition: ketypes.h:51
#define KF_SLAT
Definition: ketypes.h:58
#define KF_PAT
Definition: ketypes.h:41
#define KF_AVX2
Definition: ketypes.h:78
#define KF_RDTSCP
Definition: ketypes.h:66
#define KF_GLOBAL_PAGE
Definition: ketypes.h:35
#define KF_SMAP
Definition: ketypes.h:65
#define KF_AVX512F
Definition: ketypes.h:79
BOOLEAN KiSMTProcessorsPresent
Definition: cpu.c:31
UCHAR LogicalProcessorsPerPhysicalProcessor
Definition: ketypes.h:759
ULONG InitialApicId
Definition: ketypes.h:721
struct CPUID_AMD_SVM_FEATURES_EDX::@3917 Bits
CPUID_AMD_SVM_FEATURES_EDX Edx
Definition: Cpuid.h:220
struct CPUID_EXTENDED_CPU_SIG_REGS::@3915 Amd
struct CPUID_EXTENDED_CPU_SIG_REGS::@3914 Intel
struct CPUID_EXTENDED_STATE_SUB_LEAF_EAX::@4012 Bits
CPUID_EXTENDED_STATE_SUB_LEAF_EAX Eax
Definition: Cpuid.h:107
struct CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_EBX::@4002 Bits
CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_EBX Ebx
Definition: Cpuid.h:80
struct CPUID_THERMAL_POWER_MANAGEMENT_REGS::@3903 Undoc
struct MSR_IA32_VMX_PROCBASED_CTLS2_REGISTER::@4049 Bits
struct MSR_IA32_VMX_PROCBASED_CTLS_REGISTER::@4048 Bits

◆ KiRestoreProcessorControlState()

VOID NTAPI KiRestoreProcessorControlState ( PKPROCESSOR_STATE  ProcessorState)

Definition at line 445 of file cpu.c.

446{
447 /* Restore the CR registers */
448 __writecr0(ProcessorState->SpecialRegisters.Cr0);
449// __writecr2(ProcessorState->SpecialRegisters.Cr2);
450 __writecr3(ProcessorState->SpecialRegisters.Cr3);
451 __writecr4(ProcessorState->SpecialRegisters.Cr4);
452 __writecr8(ProcessorState->SpecialRegisters.Cr8);
453
454 /* Restore the DR registers */
455 __writedr(0, ProcessorState->SpecialRegisters.KernelDr0);
456 __writedr(1, ProcessorState->SpecialRegisters.KernelDr1);
457 __writedr(2, ProcessorState->SpecialRegisters.KernelDr2);
458 __writedr(3, ProcessorState->SpecialRegisters.KernelDr3);
459 __writedr(6, ProcessorState->SpecialRegisters.KernelDr6);
460 __writedr(7, ProcessorState->SpecialRegisters.KernelDr7);
461
462 /* Restore GDT, IDT, LDT and TSS */
463 __lgdt(&ProcessorState->SpecialRegisters.Gdtr.Limit);
464// __lldt(&ProcessorState->SpecialRegisters.Ldtr);
465// __ltr(&ProcessorState->SpecialRegisters.Tr);
466 __lidt(&ProcessorState->SpecialRegisters.Idtr.Limit);
467
468 _mm_setcsr(ProcessorState->SpecialRegisters.MxCsr);
469// ProcessorState->SpecialRegisters.DebugControl
470// ProcessorState->SpecialRegisters.LastBranchToRip
471// ProcessorState->SpecialRegisters.LastBranchFromRip
472// ProcessorState->SpecialRegisters.LastExceptionToRip
473// ProcessorState->SpecialRegisters.LastExceptionFromRip
474
475 /* Restore MSRs */
482
483}
PPC_QUAL void __writemsr(const unsigned long Value)
Definition: intrin_ppc.h:748
__INTRIN_INLINE void __lidt(void *Source)
Definition: intrin_x86.h:2059
__INTRIN_INLINE void __writecr0(unsigned int Data)
Definition: intrin_x86.h:1821
__INTRIN_INLINE void __writecr4(unsigned int Data)
Definition: intrin_x86.h:1831
__INTRIN_INLINE void __writedr(unsigned reg, unsigned int value)
Definition: intrin_x86.h:1967
#define X86_MSR_CSTAR
Definition: ke.h:75
#define X86_MSR_GSBASE
Definition: ke.h:70
#define X86_MSR_LSTAR
Definition: ke.h:74
#define X86_MSR_SFMASK
Definition: ke.h:76
#define X86_MSR_KERNEL_GSBASE
Definition: ke.h:71
#define X86_MSR_STAR
Definition: ke.h:73
USHORT Limit
Definition: ketypes.h:449
KSPECIAL_REGISTERS SpecialRegisters
Definition: ketypes.h:635
ULONG64 KernelDr1
Definition: ketypes.h:606
ULONG64 MsrLStar
Definition: ketypes.h:625
ULONG64 KernelDr2
Definition: ketypes.h:607
ULONG64 KernelDr0
Definition: ketypes.h:605
KDESCRIPTOR Gdtr
Definition: ketypes.h:611
ULONG64 MsrGsBase
Definition: ketypes.h:622
KDESCRIPTOR Idtr
Definition: ketypes.h:612
ULONG64 MsrCStar
Definition: ketypes.h:626
ULONG64 KernelDr7
Definition: ketypes.h:610
ULONG64 KernelDr6
Definition: ketypes.h:609
ULONG64 MsrSyscallMask
Definition: ketypes.h:627
ULONG64 KernelDr3
Definition: ketypes.h:608
ULONG64 MsrGsSwap
Definition: ketypes.h:623
void _mm_setcsr(unsigned int a)
Definition: xmmintrin.h:542

Referenced by KiRestoreProcessorState().

◆ KiRestoreProcessorState()

VOID NTAPI KiRestoreProcessorState ( _Out_ PKTRAP_FRAME  TrapFrame,
_Out_ PKEXCEPTION_FRAME  ExceptionFrame 
)

Definition at line 544 of file cpu.c.

547{
548 PKPRCB Prcb = KeGetCurrentPrcb();
549
550 /* Restore all context */
552 ExceptionFrame,
553 TrapFrame,
555 TrapFrame->PreviousMode);
556
557 /* Restore control registers */
559}
VOID NTAPI KeContextToTrapFrame(PCONTEXT Context, PKEXCEPTION_FRAME ExeptionFrame, PKTRAP_FRAME TrapFrame, ULONG ContextFlags, KPROCESSOR_MODE PreviousMode)
VOID NTAPI KiRestoreProcessorControlState(PKPROCESSOR_STATE ProcessorState)
Definition: cpu.c:445
#define CONTEXT_ALL
KPROCESSOR_STATE ProcessorState
Definition: ketypes.h:683
CONTEXT ContextFrame
Definition: ketypes.h:636

Referenced by KiProcessorFreezeHandler().

◆ KiSaveProcessorControlState()

VOID NTAPI KiSaveProcessorControlState ( OUT PKPROCESSOR_STATE  ProcessorState)

Definition at line 487 of file cpu.c.

488{
489 /* Save the CR registers */
490 ProcessorState->SpecialRegisters.Cr0 = __readcr0();
491 ProcessorState->SpecialRegisters.Cr2 = __readcr2();
492 ProcessorState->SpecialRegisters.Cr3 = __readcr3();
493 ProcessorState->SpecialRegisters.Cr4 = __readcr4();
494 ProcessorState->SpecialRegisters.Cr8 = __readcr8();
495
496 /* Save the DR registers */
497 ProcessorState->SpecialRegisters.KernelDr0 = __readdr(0);
498 ProcessorState->SpecialRegisters.KernelDr1 = __readdr(1);
499 ProcessorState->SpecialRegisters.KernelDr2 = __readdr(2);
500 ProcessorState->SpecialRegisters.KernelDr3 = __readdr(3);
501 ProcessorState->SpecialRegisters.KernelDr6 = __readdr(6);
502 ProcessorState->SpecialRegisters.KernelDr7 = __readdr(7);
503
504 /* Save GDT, IDT, LDT and TSS */
505 __sgdt(&ProcessorState->SpecialRegisters.Gdtr.Limit);
506 __sldt(&ProcessorState->SpecialRegisters.Ldtr);
507 __str(&ProcessorState->SpecialRegisters.Tr);
508 __sidt(&ProcessorState->SpecialRegisters.Idtr.Limit);
509
510 ProcessorState->SpecialRegisters.MxCsr = _mm_getcsr();
511// ProcessorState->SpecialRegisters.DebugControl =
512// ProcessorState->SpecialRegisters.LastBranchToRip =
513// ProcessorState->SpecialRegisters.LastBranchFromRip =
514// ProcessorState->SpecialRegisters.LastExceptionToRip =
515// ProcessorState->SpecialRegisters.LastExceptionFromRip =
516
517 /* Save MSRs */
518 ProcessorState->SpecialRegisters.MsrGsBase = __readmsr(X86_MSR_GSBASE);
519 ProcessorState->SpecialRegisters.MsrGsSwap = __readmsr(X86_MSR_KERNEL_GSBASE);
520 ProcessorState->SpecialRegisters.MsrStar = __readmsr(X86_MSR_STAR);
521 ProcessorState->SpecialRegisters.MsrLStar = __readmsr(X86_MSR_LSTAR);
522 ProcessorState->SpecialRegisters.MsrCStar = __readmsr(X86_MSR_CSTAR);
523 ProcessorState->SpecialRegisters.MsrSyscallMask = __readmsr(X86_MSR_SFMASK);
524}
__INTRIN_INLINE unsigned int __readdr(unsigned int reg)
Definition: intrin_x86.h:1934
__INTRIN_INLINE unsigned long __readcr4(void)
Definition: intrin_x86.h:1857
__INTRIN_INLINE unsigned long __readcr0(void)
Definition: intrin_x86.h:1836
__INTRIN_INLINE unsigned long __readcr2(void)
Definition: intrin_x86.h:1843
__INTRIN_INLINE void __sidt(void *Destination)
Definition: intrin_x86.h:2064
unsigned int _mm_getcsr(void)
Definition: xmmintrin.h:535

Referenced by KeSaveStateForHibernate(), and KiSaveProcessorState().

◆ KiSaveProcessorState()

VOID NTAPI KiSaveProcessorState ( _In_ PKTRAP_FRAME  TrapFrame,
_In_ PKEXCEPTION_FRAME  ExceptionFrame 
)

Definition at line 528 of file cpu.c.

531{
532 PKPRCB Prcb = KeGetCurrentPrcb();
533
534 /* Save all context */
536 KeTrapFrameToContext(TrapFrame, ExceptionFrame, &Prcb->ProcessorState.ContextFrame);
537
538 /* Save control registers */
540}
VOID NTAPI KeTrapFrameToContext(IN PKTRAP_FRAME TrapFrame, IN PKEXCEPTION_FRAME ExceptionFrame, IN OUT PCONTEXT Context)
Definition: context.c:169
ULONG ContextFlags
Definition: nt_native.h:1429

Referenced by KiProcessorFreezeHandler(), and KiTrap02Handler().

◆ KiSetProcessorType()

VOID NTAPI KiSetProcessorType ( VOID  )

Definition at line 40 of file cpu.c.

41{
42 PKPRCB Prcb = KeGetCurrentPrcb();
43 USHORT Family, Model, Stepping;
44
45 /* This initializes Prcb->CpuVendor */
48
49 /* Get the family, model and stepping */
50 KiGetCpuSignature(&Family, &Model, &Stepping);
51
52 /* Save them in the PRCB */
53 Prcb->CpuID = TRUE;
54 Prcb->CpuType = (UCHAR)Family;
55 Prcb->CpuStep = ((Model << 8) | Stepping);
56}
VOID NTAPI KiGetCpuSignature(_Out_ PUSHORT Family, _Out_ PUSHORT Model, _Out_ PUSHORT Stepping)
Get the CPU signature.
Definition: cpuinfo.c:118
VOID NTAPI KiGetCpuVendorString(_Out_writes_z_(CPU_VENDOR_STR_LEN) CHAR VendorString[CPU_VENDOR_STR_LEN])
Get the CPUID information for a given CPU.
Definition: cpuinfo.c:24
CPU_VENDORS NTAPI KiIdentifyCpuVendor(_In_reads_z_(CPU_VENDOR_STR_LEN) const CHAR VendorString[CPU_VENDOR_STR_LEN])
Identify the CPU vendor by the vendor string.
Definition: cpuinfo.c:55
unsigned short USHORT
Definition: pedump.c:61
static int Family
Definition: ping.c:62
CHAR CpuType
Definition: ketypes.h:684
USHORT CpuStep
Definition: ketypes.h:689
CHAR CpuID
Definition: ketypes.h:685
UCHAR VendorString[13]
Definition: ketypes.h:901

◆ KxRestoreFloatingPointState()

NTSTATUS NTAPI KxRestoreFloatingPointState ( IN PKFLOATING_SAVE  FloatingState)

Definition at line 591 of file cpu.c.

592{
593 UNREFERENCED_PARAMETER(FloatingState);
594 return STATUS_SUCCESS;
595}
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:329
#define STATUS_SUCCESS
Definition: shellext.h:65

◆ KxSaveFloatingPointState()

NTSTATUS NTAPI KxSaveFloatingPointState ( OUT PKFLOATING_SAVE  FloatingState)

Definition at line 583 of file cpu.c.

584{
585 UNREFERENCED_PARAMETER(FloatingState);
586 return STATUS_SUCCESS;
587}

Variable Documentation

◆ KeI386CpuStep

ULONG KeI386CpuStep

Definition at line 26 of file cpu.c.

◆ KeI386CpuType

ULONG KeI386CpuType

Definition at line 25 of file cpu.c.

Referenced by KeInvalidateAllCaches().

◆ KeI386MachineType

ULONG KeI386MachineType

Definition at line 27 of file cpu.c.

Referenced by KiInitializeMachineType().

◆ KeI386NpxPresent

ULONG KeI386NpxPresent = 1

Definition at line 28 of file cpu.c.

Referenced by KeRestoreFloatingPointState(), and KeSaveFloatingPointState().

◆ KeLargestCacheLine

ULONG KeLargestCacheLine = 0x40

Definition at line 29 of file cpu.c.

Referenced by KeGetRecommendedSharedDataAlignment(), and KiGetCacheInformation().

◆ KiBootTss

KTSS64 KiBootTss

Definition at line 22 of file cpu.c.

◆ KiDmaIoCoherency

ULONG KiDmaIoCoherency = 0

Definition at line 30 of file cpu.c.

Referenced by KeSetDmaIoCoherency(), and KiInitializeKernel().

◆ KiSMTProcessorsPresent

BOOLEAN KiSMTProcessorsPresent

Definition at line 31 of file cpu.c.

Referenced by KiGetFeatureBits().

◆ KiTbFlushTimeStamp

volatile LONG KiTbFlushTimeStamp

Definition at line 34 of file cpu.c.

Referenced by KeFlushEntireTb().