3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #ifndef __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H
20 #define __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H
23 #pragma GCC system_header
26 #define KTRAP_FRAME_DEBUGEBP (0x0)
27 #define KTRAP_FRAME_DEBUGEIP (0x4)
28 #define KTRAP_FRAME_DEBUGARGMARK (0x8)
29 #define KTRAP_FRAME_DEBUGPOINTER (0xC)
30 #define KTRAP_FRAME_TEMPSS (0x10)
31 #define KTRAP_FRAME_TEMPESP (0x14)
32 #define KTRAP_FRAME_DR0 (0x18)
33 #define KTRAP_FRAME_DR1 (0x1C)
34 #define KTRAP_FRAME_DR2 (0x20)
35 #define KTRAP_FRAME_DR3 (0x24)
36 #define KTRAP_FRAME_DR6 (0x28)
37 #define KTRAP_FRAME_DR7 (0x2C)
38 #define KTRAP_FRAME_GS (0x30)
39 #define KTRAP_FRAME_RESERVED1 (0x32)
40 #define KTRAP_FRAME_ES (0x34)
41 #define KTRAP_FRAME_RESERVED2 (0x36)
42 #define KTRAP_FRAME_DS (0x38)
43 #define KTRAP_FRAME_RESERVED3 (0x3A)
44 #define KTRAP_FRAME_EDX (0x3C)
45 #define KTRAP_FRAME_ECX (0x40)
46 #define KTRAP_FRAME_EAX (0x44)
47 #define KTRAP_FRAME_PREVIOUS_MODE (0x48)
48 #define KTRAP_FRAME_EXCEPTION_LIST (0x4C)
49 #define KTRAP_FRAME_FS (0x50)
50 #define KTRAP_FRAME_RESERVED4 (0x52)
51 #define KTRAP_FRAME_EDI (0x54)
52 #define KTRAP_FRAME_ESI (0x58)
53 #define KTRAP_FRAME_EBX (0x5C)
54 #define KTRAP_FRAME_EBP (0x60)
55 #define KTRAP_FRAME_ERROR_CODE (0x64)
56 #define KTRAP_FRAME_EIP (0x68)
57 #define KTRAP_FRAME_CS (0x6C)
58 #define KTRAP_FRAME_EFLAGS (0x70)
59 #define KTRAP_FRAME_ESP (0x74)
60 #define KTRAP_FRAME_SS (0x78)
61 #define KTRAP_FRAME_RESERVED5 (0x7A)
62 #define KTRAP_FRAME_V86_ES (0x7C)
63 #define KTRAP_FRAME_RESERVED6 (0x7E)
64 #define KTRAP_FRAME_V86_DS (0x80)
65 #define KTRAP_FRAME_RESERVED7 (0x82)
66 #define KTRAP_FRAME_V86_FS (0x84)
67 #define KTRAP_FRAME_RESERVED8 (0x86)
68 #define KTRAP_FRAME_V86_GS (0x88)
69 #define KTRAP_FRAME_RESERVED9 (0x8A)
70 #define KTRAP_FRAME_SIZE (0x8C)
72 #define X86_EFLAGS_TF 0x00000100 /* Trap flag */
73 #define X86_EFLAGS_IF 0x00000200 /* Interrupt Enable flag */
74 #define X86_EFLAGS_IOPL 0x00003000 /* I/O Privilege Level bits */
75 #define X86_EFLAGS_NT 0x00004000 /* Nested Task flag */
76 #define X86_EFLAGS_RF 0x00010000 /* Resume flag */
77 #define X86_EFLAGS_VM 0x00020000 /* Virtual Mode */
78 #define X86_EFLAGS_ID 0x00200000 /* CPUID detection flag */
80 #define X86_CR0_PE 0x00000001 /* enable Protected Mode */
81 #define X86_CR0_NE 0x00000020 /* enable native FPU error reporting */
82 #define X86_CR0_TS 0x00000008 /* enable exception on FPU instruction for task switch */
83 #define X86_CR0_EM 0x00000004 /* enable FPU emulation (disable FPU) */
84 #define X86_CR0_MP 0x00000002 /* enable FPU monitoring */
85 #define X86_CR0_WP 0x00010000 /* enable Write Protect (copy on write) */
86 #define X86_CR0_PG 0x80000000 /* enable Paging */
88 #define X86_CR4_PAE 0x00000020 /* enable physical address extensions */
89 #define X86_CR4_PGE 0x00000080 /* enable global pages */
90 #define X86_CR4_OSFXSR 0x00000200 /* enable FXSAVE/FXRSTOR instructions */
91 #define X86_CR4_OSXMMEXCPT 0x00000400 /* enable #XF exception */
93 #define X86_FEATURE_TSC 0x00000010 /* time stamp counters are present */
94 #define X86_FEATURE_PAE 0x00000040 /* physical address extension is present */
95 #define X86_FEATURE_CX8 0x00000100 /* CMPXCHG8B instruction present */
96 #define X86_FEATURE_SYSCALL 0x00000800 /* SYSCALL/SYSRET support present */
97 #define X86_FEATURE_PGE 0x00002000 /* Page Global Enable */
98 #define X86_FEATURE_MMX 0x00800000 /* MMX extension present */
99 #define X86_FEATURE_FXSR 0x01000000 /* FXSAVE/FXRSTOR instructions present */
100 #define X86_FEATURE_SSE 0x02000000 /* SSE extension present */
101 #define X86_FEATURE_SSE2 0x04000000 /* SSE2 extension present */
102 #define X86_FEATURE_HT 0x10000000 /* Hyper-Threading present */
104 #define X86_EXT_FEATURE_SSE3 0x00000001 /* SSE3 extension present */
105 #define X86_EXT_FEATURE_3DNOW 0x40000000 /* 3DNOW! extension present */
107 /* Possible values for KTHREAD's NpxState */
108 #define NPX_STATE_INVALID 0x01
109 #define NPX_STATE_VALID 0x02
110 #define NPX_STATE_DIRTY 0x04
114 typedef struct _KIRQ_TRAPFRAME
132 } KIRQ_TRAPFRAME
, *PKIRQ_TRAPFRAME
;
134 typedef struct _KGDTENTRY
{
151 ULONG Reserved_0
: 1;
152 ULONG Default_Big
: 1;
153 ULONG Granularity
: 1;
157 } KGDTENTRY
, *PKGDTENTRY
;
159 typedef struct _KIDTENTRY
{
163 USHORT ExtendedOffset
;
164 } KIDTENTRY
, *PKIDTENTRY
;
166 extern ULONG Ke386CacheAlignment
;
170 KiInitializeGdt(struct _KPCR
* Pcr
);
172 Ki386ApplicationProcessorInitializeTSS(VOID
);
174 Ki386BootInitializeTSS(VOID
);
176 KiGdtPrepareForApplicationProcessorInit(ULONG Id
);
178 Ki386InitializeLdt(VOID
);
180 Ki386SetProcessorFeatures(VOID
);
181 ULONG
KeAllocateGdtSelector(ULONG Desc
[2]);
182 VOID
KeFreeGdtSelector(ULONG Entry
);
184 NtEarlyInitVdm(VOID
);
186 KeApplicationProcessorInitDispatcher(VOID
);
188 KeCreateApplicationProcessorIdleThread(ULONG Id
);
192 (STDCALL
*PKSYSTEM_ROUTINE
)(PKSTART_ROUTINE StartRoutine
,
197 Ke386InitThreadWithContext(PKTHREAD Thread
,
198 PKSYSTEM_ROUTINE SystemRoutine
,
199 PKSTART_ROUTINE StartRoutine
,
203 #ifdef _NTOSKRNL_ /* FIXME: Move flags above to NDK instead of here */
206 KiThreadStartup(PKSYSTEM_ROUTINE SystemRoutine
,
207 PKSTART_ROUTINE StartRoutine
,
210 KTRAP_FRAME TrapFrame
);
214 #define LOCK "lock ; "
217 #define KeGetCurrentIrql() (((PKPCR)KPCR_BASE)->Irql)
220 #if defined(__GNUC__)
221 #define Ke386DisableInterrupts() __asm__("cli\n\t");
222 #define Ke386EnableInterrupts() __asm__("sti\n\t");
223 #define Ke386HaltProcessor() __asm__("hlt\n\t");
224 #define Ke386GetPageTableDirectory(X) \
225 __asm__("movl %%cr3,%0\n\t" : "=d" (X));
226 #define Ke386SetPageTableDirectory(X) \
227 __asm__("movl %0,%%cr3\n\t" \
230 #define Ke386SetFileSelector(X) \
231 __asm__("movl %0,%%cr3\n\t" \
234 #define Ke386SetLocalDescriptorTable(X) \
235 __asm__("lldt %0\n\t" \
238 #define Ke386SetGlobalDescriptorTable(X) \
239 __asm__("lgdt %0\n\t" \
242 #define Ke386SaveFlags(x) __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */)
243 #define Ke386RestoreFlags(x) __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
245 #define _Ke386GetCr(N) ({ \
247 __asm__("movl %%cr" #N ",%0\n\t" :"=r" (__d)); \
250 #define _Ke386SetCr(N,X) __asm__ __volatile__("movl %0,%%cr" #N : :"r" (X));
252 #define Ke386GetCr0() _Ke386GetCr(0)
253 #define Ke386SetCr0(X) _Ke386SetCr(0,X)
254 #define Ke386GetCr2() _Ke386GetCr(2)
255 #define Ke386SetCr2(X) _Ke386SetCr(2,X)
256 #define Ke386GetCr4() _Ke386GetCr(4)
257 #define Ke386SetCr4(X) _Ke386SetCr(4,X)
259 static inline LONG
Ke386TestAndClearBit(ULONG BitPos
, volatile PULONG Addr
)
263 __asm__
__volatile__(LOCK
266 :"=r" (OldBit
),"=m" (*Addr
)
272 static inline LONG
Ke386TestAndSetBit(ULONG BitPos
, volatile PULONG Addr
)
276 __asm__
__volatile__(LOCK
279 :"=r" (OldBit
),"=m" (*Addr
)
286 static inline void Ki386Cpuid(ULONG Op
, PULONG Eax
, PULONG Ebx
, PULONG Ecx
, PULONG Edx
)
289 : "=a" (*Eax
), "=b" (*Ebx
), "=c" (*Ecx
), "=d" (*Edx
)
293 #define Ke386Rdmsr(msr,val1,val2) __asm__ __volatile__("rdmsr" : "=a" (val1), "=d" (val2) : "c" (msr))
295 #define Ke386Wrmsr(msr,val1,val2) __asm__ __volatile__("wrmsr" : /* no outputs */ : "c" (msr), "a" (val1), "d" (val2))
298 #elif defined(_MSC_VER)
300 #define Ke386DisableInterrupts() __asm cli
301 #define Ke386EnableInterrupts() __asm sti
302 #define Ke386HaltProcessor() __asm hlt
303 #define Ke386GetPageTableDirectory(X) \
304 __asm mov eax, cr3; \
306 #define Ke386SetPageTableDirectory(X) \
310 #error Unknown compiler for inline assembler
315 #endif /* __NTOSKRNL_INCLUDE_INTERNAL_I386_KE_H */