1 #ifndef __NTOSKRNL_INCLUDE_INTERNAL_AMD64_KE_H
2 #define __NTOSKRNL_INCLUDE_INTERNAL_AMD64_KE_H
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 */
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 */
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 */
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 */
37 #define X86_EXT_FEATURE_SSE3 0x00000001 /* SSE3 extension present */
38 #define X86_EXT_FEATURE_3DNOW 0x40000000 /* 3DNOW! extension present */
40 #define FRAME_EDITED 0xFFF8
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
54 #define EFER_SVME 0x100
55 #define EFER_FFXSR 0x400
63 typedef struct _KIDT_INIT
69 } KIDT_INIT
, *PKIDT_INIT
;
71 extern ULONG Ke386CacheAlignment
;
72 extern ULONG KeI386NpxPresent
;
73 extern ULONG KeI386XMMIPresent
;
74 extern ULONG KeI386FxsrPresent
;
75 extern ULONG KeI386CpuType
;
76 extern ULONG KeI386CpuStep
;
79 // INT3 is 1 byte long
81 #define KD_BREAKPOINT_TYPE UCHAR
82 #define KD_BREAKPOINT_SIZE sizeof(UCHAR)
83 #define KD_BREAKPOINT_VALUE 0xCC
86 // Macros for getting and setting special purpose registers in portable code
88 #define KeGetContextPc(Context) \
91 #define KeSetContextPc(Context, ProgramCounter) \
92 ((Context)->Rip = (ProgramCounter))
94 #define KeGetTrapFramePc(TrapFrame) \
97 #define KiGetLinkedTrapFrame(x) \
98 (PKTRAP_FRAME)((x)->Rdx)
100 #define KeGetContextReturnRegister(Context) \
103 #define KeSetContextReturnRegister(Context, ReturnValue) \
104 ((Context)->Rax = (ReturnValue))
107 // Macro to get trap and exception frame from a thread stack
109 #define KeGetTrapFrame(Thread) \
110 (PKTRAP_FRAME)((ULONG_PTR)((Thread)->InitialStack) - \
114 // Macro to get context switches from the PRCB
115 // All architectures but x86 have it in the PRCB's KeContextSwitches
117 #define KeGetContextSwitches(Prcb) \
118 (Prcb->KeContextSwitches)
121 // Macro to get the second level cache size field name which differs between
122 // CISC and RISC architectures, as the former has unified I/D cache
124 #define KiGetSecondLevelDCacheSize() ((PKIPCR)KeGetPcr())->SecondLevelCacheSize
126 #define KeGetExceptionFrame(Thread) \
127 (PKEXCEPTION_FRAME)((ULONG_PTR)KeGetTrapFrame(Thread) - \
128 sizeof(KEXCEPTION_FRAME))
131 // Returns the Interrupt State from a Trap Frame.
132 // ON = TRUE, OFF = FALSE
134 #define KeGetTrapFrameInterruptState(TrapFrame) \
135 BooleanFlagOn((TrapFrame)->EFlags, EFLAGS_INTERRUPT_MASK)
138 // Invalidates the TLB entry for a specified address
142 KeInvalidateTlbEntry(IN PVOID Address
)
144 /* Invalidate the TLB entry for this address */
150 KeFlushProcessTb(VOID
)
152 /* Flush the TLB by resetting CR3 */
153 __writecr3(__readcr3());
158 KiRundownThread(IN PKTHREAD Thread
)
161 DbgPrint("KiRundownThread is unimplemented\n");
167 /* Registers an interrupt handler with an IDT vector */
170 KeRegisterInterruptHandler(IN ULONG Vector
,
176 /* Get the entry from the HAL */
177 Entry
= HalVectorToIDTEntry(Vector
);
179 /* Now set the data */
180 Idt
= &KeGetPcr()->IdtBase
[Entry
];
181 Idt
->OffsetLow
= (ULONG_PTR
)Handler
& 0xffff;
182 Idt
->OffsetMiddle
= ((ULONG_PTR
)Handler
>> 16) & 0xffff;
183 Idt
->OffsetHigh
= (ULONG_PTR
)Handler
>> 32;
184 Idt
->Selector
= KGDT64_R0_CODE
;
193 /* Returns the registered interrupt handler for a given IDT vector */
196 KeQueryInterruptHandler(IN ULONG Vector
)
201 /* Get the entry from the HAL */
202 Entry
= HalVectorToIDTEntry(Vector
);
204 /* Get the IDT entry */
205 Idt
= &KeGetPcr()->IdtBase
[Entry
];
207 /* Return the address */
208 return (PVOID
)((ULONG64
)Idt
->OffsetHigh
<< 32 |
209 (ULONG64
)Idt
->OffsetMiddle
<< 16 |
210 (ULONG64
)Idt
->OffsetLow
);
215 KiEndInterrupt(IN KIRQL Irql
,
216 IN PKTRAP_FRAME TrapFrame
)
218 DbgPrint("KiEndInterrupt is unimplemented\n");
221 #define Ki386PerfEnd(x)
227 KiInitializeTss(IN PKTSS Tss
, IN UINT64 Stack
);
229 VOID
KiDivideErrorFault();
230 VOID
KiDebugTrapOrFault();
231 VOID
KiNmiInterrupt();
232 VOID
KiBreakpointTrap();
233 VOID
KiOverflowTrap();
235 VOID
KiInvalidOpcodeFault();
236 VOID
KiNpxNotAvailableFault();
237 VOID
KiDoubleFaultAbort();
238 VOID
KiNpxSegmentOverrunAbort();
239 VOID
KiInvalidTssFault();
240 VOID
KiSegmentNotPresentFault();
242 VOID
KiGeneralProtectionFault();
244 VOID
KiFloatingErrorFault();
245 VOID
KiAlignmentFault();
246 VOID
KiMcheckAbort();
247 VOID
KiXmmException();
248 VOID
KiApcInterrupt();
249 VOID
KiRaiseAssertion();
250 VOID
KiDebugServiceTrap();
251 VOID
KiDpcInterrupt();
252 VOID
KiIpiInterrupt();
255 KiGdtPrepareForApplicationProcessorInit(ULONG Id
);
257 Ki386InitializeLdt(VOID
);
259 Ki386SetProcessorFeatures(VOID
);
263 KiGetCacheInformation(VOID
);
273 KiIsNpxErrataPresent(
279 KiSetProcessorType(VOID
);
283 KiGetFeatureBits(VOID
);
287 KiInitializeCpuFeatures();
289 ULONG
KeAllocateGdtSelector(ULONG Desc
[2]);
290 VOID
KeFreeGdtSelector(ULONG Entry
);
292 NtEarlyInitVdm(VOID
);
294 KeApplicationProcessorInitDispatcher(VOID
);
296 KeCreateApplicationProcessorIdleThread(ULONG Id
);
300 Ke386InitThreadWithContext(PKTHREAD Thread
,
301 PKSYSTEM_ROUTINE SystemRoutine
,
302 PKSTART_ROUTINE StartRoutine
,
305 #define KeArchInitThreadWithContext(Thread,SystemRoutine,StartRoutine,StartContext,Context) \
306 Ke386InitThreadWithContext(Thread,SystemRoutine,StartRoutine,StartContext,Context)
308 #ifdef _NTOSKRNL_ /* FIXME: Move flags above to NDK instead of here */
311 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine
,
312 PKSTART_ROUTINE StartRoutine
,
315 KTRAP_FRAME TrapFrame
);
321 extern NTKERNELAPI
volatile KSYSTEM_TIME KeTickCount
;
323 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_AMD64_KE_H */