ReactOS  r76032
ke.h
Go to the documentation of this file.
1 #ifndef __NTOSKRNL_INCLUDE_INTERNAL_AMD64_KE_H
2 #define __NTOSKRNL_INCLUDE_INTERNAL_AMD64_KE_H
3 
4 #define X86_EFLAGS_TF 0x00000100 /* Trap flag */
5 #define X86_EFLAGS_IF 0x00000200 /* Interrupt Enable flag */
6 #define X86_EFLAGS_IOPL 0x00003000 /* I/O Privilege Level bits */
7 #define X86_EFLAGS_NT 0x00004000 /* Nested Task flag */
8 #define X86_EFLAGS_RF 0x00010000 /* Resume flag */
9 #define X86_EFLAGS_VM 0x00020000 /* Virtual Mode */
10 #define X86_EFLAGS_ID 0x00200000 /* CPUID detection flag */
11 
12 #define X86_CR0_PE 0x00000001 /* enable Protected Mode */
13 #define X86_CR0_NE 0x00000020 /* enable native FPU error reporting */
14 #define X86_CR0_TS 0x00000008 /* enable exception on FPU instruction for task switch */
15 #define X86_CR0_EM 0x00000004 /* enable FPU emulation (disable FPU) */
16 #define X86_CR0_MP 0x00000002 /* enable FPU monitoring */
17 #define X86_CR0_WP 0x00010000 /* enable Write Protect (copy on write) */
18 #define X86_CR0_PG 0x80000000 /* enable Paging */
19 
20 #define X86_CR4_PAE 0x00000020 /* enable physical address extensions */
21 #define X86_CR4_PGE 0x00000080 /* enable global pages */
22 #define X86_CR4_OSFXSR 0x00000200 /* enable FXSAVE/FXRSTOR instructions */
23 #define X86_CR4_OSXMMEXCPT 0x00000400 /* enable #XF exception */
24 
25 #define X86_FEATURE_VME 0x00000002 /* Virtual 8086 Extensions are present */
26 #define X86_FEATURE_TSC 0x00000010 /* time stamp counters are present */
27 #define X86_FEATURE_PAE 0x00000040 /* physical address extension is present */
28 #define X86_FEATURE_CX8 0x00000100 /* CMPXCHG8B instruction present */
29 #define X86_FEATURE_SYSCALL 0x00000800 /* SYSCALL/SYSRET support present */
30 #define X86_FEATURE_PGE 0x00002000 /* Page Global Enable */
31 #define X86_FEATURE_MMX 0x00800000 /* MMX extension present */
32 #define X86_FEATURE_FXSR 0x01000000 /* FXSAVE/FXRSTOR instructions present */
33 #define X86_FEATURE_SSE 0x02000000 /* SSE extension present */
34 #define X86_FEATURE_SSE2 0x04000000 /* SSE2 extension present */
35 #define X86_FEATURE_HT 0x10000000 /* Hyper-Threading present */
36 
37 #define X86_EXT_FEATURE_SSE3 0x00000001 /* SSE3 extension present */
38 #define X86_EXT_FEATURE_3DNOW 0x40000000 /* 3DNOW! extension present */
39 
40 #define FRAME_EDITED 0xFFF8
41 
42 #define X86_MSR_GSBASE 0xC0000101
43 #define X86_MSR_KERNEL_GSBASE 0xC0000102
44 #define X86_MSR_EFER 0xC0000080
45 #define X86_MSR_STAR 0xC0000081
46 #define X86_MSR_LSTAR 0xC0000082
47 #define X86_MSR_CSTAR 0xC0000083
48 #define X86_MSR_SFMASK 0xC0000084
49 
50 #define EFER_SCE 0x01
51 #define EFER_LME 0x10
52 #define EFER_LMA 0x40
53 #define EFER_NXE 0x80
54 #define EFER_SVME 0x100
55 #define EFER_FFXSR 0x400
56 
57 #define AMD64_TSS 9
58 
59 #define APIC_EOI_REGISTER 0xFFFFFFFFFFFE00B0ULL
60 
61 #ifndef __ASM__
62 
63 #include "intrin_i.h"
64 
65 typedef struct _KIDT_INIT
66 {
72 
73 #include <pshpack1.h>
75 {
82 #include <poppack.h>
83 
84 extern ULONG KeI386NpxPresent;
87 extern ULONG KeI386CpuType;
88 extern ULONG KeI386CpuStep;
89 
90 //
91 // INT3 is 1 byte long
92 //
93 #define KD_BREAKPOINT_TYPE UCHAR
94 #define KD_BREAKPOINT_SIZE sizeof(UCHAR)
95 #define KD_BREAKPOINT_VALUE 0xCC
96 
97 //
98 // Macros for getting and setting special purpose registers in portable code
99 //
100 #define KeGetContextPc(Context) \
101  ((Context)->Rip)
102 
103 #define KeSetContextPc(Context, ProgramCounter) \
104  ((Context)->Rip = (ProgramCounter))
105 
106 #define KeGetTrapFramePc(TrapFrame) \
107  ((TrapFrame)->Rip)
108 
109 #define KiGetLinkedTrapFrame(x) \
110  (PKTRAP_FRAME)((x)->TrapFrame)
111 
112 #define KeGetContextReturnRegister(Context) \
113  ((Context)->Rax)
114 
115 #define KeSetContextReturnRegister(Context, ReturnValue) \
116  ((Context)->Rax = (ReturnValue))
117 
118 //
119 // Macro to get trap and exception frame from a thread stack
120 //
121 #define KeGetTrapFrame(Thread) \
122  (PKTRAP_FRAME)((ULONG_PTR)((Thread)->InitialStack) - \
123  sizeof(KTRAP_FRAME))
124 
125 //
126 // Macro to get context switches from the PRCB
127 // All architectures but x86 have it in the PRCB's KeContextSwitches
128 //
129 #define KeGetContextSwitches(Prcb) \
130  (Prcb->KeContextSwitches)
131 
132 //
133 // Macro to get the second level cache size field name which differs between
134 // CISC and RISC architectures, as the former has unified I/D cache
135 //
136 #define KiGetSecondLevelDCacheSize() ((PKIPCR)KeGetPcr())->SecondLevelCacheSize
137 
138 #define KeGetExceptionFrame(Thread) \
139  (PKEXCEPTION_FRAME)((ULONG_PTR)KeGetTrapFrame(Thread) - \
140  sizeof(KEXCEPTION_FRAME))
141 
142 //
143 // Returns the Interrupt State from a Trap Frame.
144 // ON = TRUE, OFF = FALSE
145 //
146 #define KeGetTrapFrameInterruptState(TrapFrame) \
147  BooleanFlagOn((TrapFrame)->EFlags, EFLAGS_INTERRUPT_MASK)
148 
149 /* Diable interrupts and return whether they were enabled before */
151 BOOLEAN
153 {
155 
156  /* Get EFLAGS and check if the interrupt bit is set */
157  Flags = __readeflags();
158 
159  /* Disable interrupts */
160  _disable();
161  return (Flags & EFLAGS_INTERRUPT_MASK) ? TRUE : FALSE;
162 }
163 
164 /* Restore previous interrupt state */
166 VOID
168 {
169  if (WereEnabled) _enable();
170 }
171 
172 //
173 // Invalidates the TLB entry for a specified address
174 //
176 VOID
178 {
179  /* Invalidate the TLB entry for this address */
180  __invlpg(Address);
181 }
182 
184 VOID
186 {
187  /* Flush the TLB by resetting CR3 */
189 }
190 
192 VOID
194  IN SIZE_T FlushSize)
195 {
196  //
197  // Always sweep the whole cache
198  //
199  UNREFERENCED_PARAMETER(BaseAddress);
200  UNREFERENCED_PARAMETER(FlushSize);
201  __wbinvd();
202 }
203 
205 VOID
207 {
208 #ifndef CONFIG_SMP
209  DbgPrint("KiRundownThread is unimplemented\n");
210 #else
211  /* Nothing to do */
212 #endif
213 }
214 
215 /* Registers an interrupt handler with an IDT vector */
217 VOID
219  IN PVOID Handler)
220 {
221  UCHAR Entry;
222  PKIDTENTRY64 Idt;
223 
224  /* Get the entry from the HAL */
225  Entry = HalVectorToIDTEntry(Vector);
226 
227  /* Now set the data */
228  Idt = &KeGetPcr()->IdtBase[Entry];
229  Idt->OffsetLow = (ULONG_PTR)Handler & 0xffff;
230  Idt->OffsetMiddle = ((ULONG_PTR)Handler >> 16) & 0xffff;
231  Idt->OffsetHigh = (ULONG_PTR)Handler >> 32;
232  Idt->Selector = KGDT64_R0_CODE;
233  Idt->IstIndex = 0;
234  Idt->Type = 0x0e;
235  Idt->Dpl = 0;
236  Idt->Present = 1;
237  Idt->Reserved0 = 0;
238  Idt->Reserved1 = 0;
239 }
240 
241 /* Returns the registered interrupt handler for a given IDT vector */
243 PVOID
245 {
246  UCHAR Entry;
247  PKIDTENTRY64 Idt;
248 
249  /* Get the entry from the HAL */
250  Entry = HalVectorToIDTEntry(Vector);
251 
252  /* Get the IDT entry */
253  Idt = &KeGetPcr()->IdtBase[Entry];
254 
255  /* Return the address */
256  return (PVOID)((ULONG64)Idt->OffsetHigh << 32 |
257  (ULONG64)Idt->OffsetMiddle << 16 |
258  (ULONG64)Idt->OffsetLow);
259 }
260 
261 VOID
264 {
265  /* Write 0 to the apic EOI register */
266  *((volatile ULONG*)APIC_EOI_REGISTER) = 0;
267 }
268 
269 VOID
272  IN PKTRAP_FRAME TrapFrame)
273 {
274  /* Make sure this is from the clock handler */
275  ASSERT(TrapFrame->ErrorCode == 0xc10c4);
276  //KeLowerIrql(Irql);
277 }
278 
279 BOOLEAN
282 {
283  /* Anything else but Ring 0 is Ring 3 */
284  return !!(TrapFrame->SegCs & MODE_MASK);
285 }
286 
287 #define Ki386PerfEnd()
288 
289 struct _KPCR;
290 
291 //VOID KiInitializeTss(IN PKTSS Tss, IN UINT64 Stack);
292 
293 VOID KiSwitchToBootStack(IN ULONG_PTR InitialStack);
318 
326 
332 
333 VOID
335  PKSYSTEM_ROUTINE SystemRoutine,
337  PVOID StartContext,
338  PCONTEXT Context);
339 #define KeArchInitThreadWithContext(Thread,SystemRoutine,StartRoutine,StartContext,Context) \
340  Ke386InitThreadWithContext(Thread,SystemRoutine,StartRoutine,StartContext,Context)
341 
342 #ifdef _NTOSKRNL_ /* FIXME: Move flags above to NDK instead of here */
343 VOID
344 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine,
346  PVOID StartContext,
347  BOOLEAN UserThread,
348  KTRAP_FRAME TrapFrame);
349 #endif
350 
351 #endif /* __ASM__ */
352 
353 // HACK
354 extern NTKERNELAPI volatile KSYSTEM_TIME KeTickCount;
355 
356 // win64 uses DMA macros, this one is not defined
357 NTHALAPI
358 NTSTATUS
359 NTAPI
361  IN PADAPTER_OBJECT AdapterObject,
365 
366 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_AMD64_KE_H */
367 
368 /* EOF */
DWORD *typedef PVOID
Definition: winlogon.h:52
struct _KI_INTERRUPT_DISPATCH_ENTRY * PKI_INTERRUPT_DISPATCH_ENTRY
VOID KiSegmentNotPresentFault(VOID)
VOID KeFreeGdtSelector(ULONG Entry)
#define IN
Definition: typedefs.h:38
VOID Ki386SetProcessorFeatures(VOID)
#define TRUE
Definition: types.h:120
UCHAR IstIndex
Definition: ke.h:69
PVOID ULONG Address
Definition: oprghdlr.h:14
#define MODE_MASK
Definition: orders.h:326
_Out_ PULONG NumberOfMapRegisters
Definition: halfuncs.h:209
VOID KiMcheckAbort(VOID)
struct _Entry Entry
Definition: kefuncs.h:640
VOID KiPageFault(VOID)
VOID KiGdtPrepareForApplicationProcessorInit(ULONG Id)
#define DbgPrint
Definition: loader.c:26
struct _KIDT_INIT * PKIDT_INIT
#define NTHALAPI
Definition: ntoskrnl.h:39
VOID KiGeneralProtectionFault(VOID)
_In_ ULONG _In_opt_ POBJECT_ATTRIBUTES _In_opt_ HANDLE _Out_opt_ PCLIENT_ID _In_ PKSTART_ROUTINE StartRoutine
Definition: psfuncs.h:87
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:315
VOID KiDivideErrorFault(VOID)
void __cdecl _enable(void)
Definition: intrin_arm.h:373
ULONG KiGetFeatureBits(VOID)
Definition: cpu.c:135
VOID KiInvalidOpcodeFault(VOID)
ULONG KeI386NpxPresent
Definition: cpu.c:31
ULONG KeAllocateGdtSelector(ULONG Desc[2])
UCHAR Dpl
Definition: ke.h:68
VOID KiDebugTrapOrFault(VOID)
Definition: ke.h:280
__INTRIN_INLINE unsigned long __readcr3(void)
Definition: intrin_x86.h:1601
union _KIDTENTRY64 * IdtBase
Definition: ke.h:296
_Out_ PKIRQL Irql
Definition: csq.h:179
FORCEINLINE PVOID KeQueryInterruptHandler(IN ULONG Vector)
Definition: ke.h:244
ULONG KeI386FxsrPresent
Definition: cpu.c:31
USHORT Selector
Definition: ketypes.h:467
DWORD Id
uint32_t ULONG_PTR
Definition: typedefs.h:63
UCHAR KIRQL
Definition: env_spec_w32.h:591
VOID KiBoundFault(VOID)
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
__INTRIN_INLINE void __writecr3(unsigned int Data)
Definition: intrin_x86.h:1572
__INTRIN_INLINE uintptr_t __readeflags(void)
Definition: intrin_x86.h:1456
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
PVOID ServiceRoutine
Definition: ke.h:70
#define FALSE
Definition: types.h:117
KSTART_ROUTINE * PKSTART_ROUTINE
Definition: ketypes.h:472
VOID KiApcInterrupt(VOID)
Definition: trapc.c:228
uint64_t ULONG64
Definition: typedefs.h:65
#define FORCEINLINE
Definition: ntbasedef.h:213
USHORT OffsetLow
Definition: ketypes.h:466
VOID KiNpxSegmentOverrunAbort(VOID)
VOID KiIpiInterrupt(VOID)
USHORT Reserved0
Definition: ketypes.h:469
#define NTKERNELAPI
FORCEINLINE BOOLEAN KeDisableInterrupts(VOID)
Definition: ke.h:152
ULONG KeI386XMMIPresent
Definition: cpu.c:30
VOID KiInitializeCpuFeatures(VOID)
USHORT Type
Definition: ketypes.h:470
VOID KiDoubleFaultAbort(VOID)
UCHAR _Op_push
Definition: ke.h:77
ULONG RelativeAddress
Definition: ke.h:80
unsigned char BOOLEAN
FORCEINLINE VOID KeInvalidateTlbEntry(IN PVOID Address)
Definition: ke.h:177
VOID KiNmiInterrupt(VOID)
struct _KIDT_INIT KIDT_INIT
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
UCHAR _Op_jmp
Definition: ke.h:79
VOID(NTAPI * PKSYSTEM_ROUTINE)(PKSTART_ROUTINE StartRoutine, PVOID StartContext)
Definition: ketypes.h:625
ULONG KeI386CpuType
Definition: cpu.c:28
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
VOID KiGetCacheInformation(VOID)
Definition: cpu.c:224
VOID KiInvalidTssFault(VOID)
USHORT Present
Definition: ketypes.h:472
PPC_QUAL void __wbinvd(void)
Definition: intrin_ppc.h:759
USHORT OffsetMiddle
Definition: ketypes.h:473
#define KGDT64_R0_CODE
Definition: ketypes.h:72
UCHAR InterruptId
Definition: ke.h:67
USHORT IstIndex
Definition: ketypes.h:468
unsigned char UCHAR
Definition: xmlstorage.h:181
VOID KiNpxNotAvailableFault(VOID)
VOID FORCEINLINE KiSendEOI(VOID)
Definition: ke.h:263
Definition: ke.h:74
ULONG KeI386CpuStep
Definition: cpu.c:29
USHORT Dpl
Definition: ketypes.h:471
#define APIC_EOI_REGISTER
Definition: ke.h:59
VOID KiDpcInterrupt(VOID)
NTKERNELAPI volatile KSYSTEM_TIME KeTickCount
Definition: clock.c:19
VOID KiStackFault(VOID)
ULONG Reserved1
Definition: ketypes.h:475
VOID KiSwitchToBootStack(IN ULONG_PTR InitialStack)
Definition: ke.h:796
Definition: ke.h:65
VOID KiXmmException(VOID)
ACPI_PHYSICAL_ADDRESS ACPI_SIZE BOOLEAN Warn BOOLEAN Physical UINT32 ACPI_TABLE_HEADER *OutTableHeader ACPI_TABLE_HEADER **OutTable ACPI_HANDLE UINT32 ACPI_WALK_CALLBACK ACPI_WALK_CALLBACK void void **ReturnValue UINT32 ACPI_BUFFER *RetPathPtr ACPI_OBJECT_HANDLER Handler
Definition: acpixf.h:658
VOID Ke386InitThreadWithContext(PKTHREAD Thread, PKSYSTEM_ROUTINE SystemRoutine, PKSTART_ROUTINE StartRoutine, PVOID StartContext, PCONTEXT Context)
FORCEINLINE PKPCR KeGetPcr(VOID)
Definition: ke.h:318
FORCEINLINE VOID KeRegisterInterruptHandler(IN ULONG Vector, IN PVOID Handler)
Definition: ke.h:218
ULONG_PTR SIZE_T
Definition: typedefs.h:78
UCHAR _Op_nop
Definition: ke.h:76
LONG NTSTATUS
Definition: DriverTester.h:11
VOID KiAlignmentFault(VOID)
VOID KeCreateApplicationProcessorIdleThread(ULONG Id)
VOID KiBreakpointTrap(VOID)
FORCEINLINE VOID KeRestoreInterrupts(BOOLEAN WereEnabled)
Definition: ke.h:167
VOID Ki386InitializeLdt(VOID)
FORCEINLINE VOID KiRundownThread(IN PKTHREAD Thread)
Definition: ke.h:206
struct _KI_INTERRUPT_DISPATCH_ENTRY KI_INTERRUPT_DISPATCH_ENTRY
UCHAR _Vector
Definition: ke.h:78
VOID FORCEINLINE KiEndInterrupt(IN KIRQL Irql, IN PKTRAP_FRAME TrapFrame)
Definition: ke.h:271
VOID KiFloatingErrorFault(VOID)
FORCEINLINE VOID KeFlushProcessTb(VOID)
Definition: ke.h:185
__INTRIN_INLINE void __invlpg(void *Address)
Definition: intrin_x86.h:1758
VOID NTAPI KiThreadStartup(VOID)
Definition: thrdini.c:63
_In_ PDEVICE_OBJECT _In_ ULONG _In_ PDRIVER_CONTROL ExecutionRoutine
Definition: iofuncs.h:1393
void __cdecl _disable(void)
Definition: intrin_arm.h:365
unsigned int ULONG
Definition: retypes.h:1
VOID NtEarlyInitVdm(VOID)
#define ULONG_PTR
Definition: config.h:101
DRIVER_CONTROL * PDRIVER_CONTROL
Definition: iotypes.h:192
VOID KiDebugServiceTrap(VOID)
VOID KiSetProcessorType(VOID)
Definition: cpu.c:55
VOID KiOverflowTrap(VOID)
VOID KiRaiseAssertion(VOID)
VOID KeApplicationProcessorInitDispatcher(VOID)
base of all file and directory entries
Definition: entries.h:82
ULONG OffsetHigh
Definition: ketypes.h:474
FORCEINLINE VOID KeSweepICache(IN PVOID BaseAddress, IN SIZE_T FlushSize)
Definition: ke.h:193
BOOLEAN FORCEINLINE KiUserTrap(IN PKTRAP_FRAME TrapFrame)
Definition: ke.h:281
#define EFLAGS_INTERRUPT_MASK
Definition: ketypes.h:126
NTHALAPI NTSTATUS NTAPI HalAllocateAdapterChannel(IN PADAPTER_OBJECT AdapterObject, IN PWAIT_CONTEXT_BLOCK Wcb, IN ULONG NumberOfMapRegisters, IN PDRIVER_CONTROL ExecutionRoutine)
Definition: dma.c:88
#define HalVectorToIDTEntry
Definition: halfuncs.h:50