ReactOS 0.4.16-dev-197-g92996da
intrin_i.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef __cplusplus
4extern "C"
5{
6#endif
7
11 _In_ PVOID pGdt,
12 _In_ USHORT Selector)
13{
14 return (PKGDTENTRY)((ULONG_PTR)pGdt + (Selector & ~RPL_MASK));
15}
16
18VOID
22{
23 Entry->BaseLow = (USHORT)(Base & 0xffff);
24 Entry->HighWord.Bytes.BaseMid = (UCHAR)((Base >> 16) & 0xff);
25 Entry->HighWord.Bytes.BaseHi = (UCHAR)((Base >> 24) & 0xff);
26 // Entry->BaseUpper = (ULONG)(Base >> 32);
27}
28
30VOID
34{
35 if (Limit < 0x100000)
36 {
37 Entry->HighWord.Bits.Granularity = 0;
38 }
39 else
40 {
41 Limit >>= 12;
42 Entry->HighWord.Bits.Granularity = 1;
43 }
44 Entry->LimitLow = (USHORT)(Limit & 0xffff);
45 Entry->HighWord.Bits.LimitHi = ((Limit >> 16) & 0x0f);
46}
47
49VOID
55 _In_ UCHAR Dpl,
56 _In_ BOOLEAN Granularity,
57 _In_ UCHAR SegMode) // 0: 16-bit, 1: 32-bit, 2: 64-bit
58{
61 Entry->HighWord.Bits.Type = (Type & 0x1f);
62 Entry->HighWord.Bits.Dpl = (Dpl & 0x3);
63 Entry->HighWord.Bits.Pres = (Type != 0); // Present, must be 1 when the GDT entry is valid.
64 Entry->HighWord.Bits.Sys = 0; // System
65 Entry->HighWord.Bits.Reserved_0 = 0; // LongMode = !!(SegMode & 1);
66 Entry->HighWord.Bits.Default_Big = !!(SegMode & 2);
67 Entry->HighWord.Bits.Granularity |= !!Granularity; // The flag may have been already set by KiSetGdtDescriptorLimit().
68 // Entry->MustBeZero = 0;
69}
70
72VOID
78 _In_ UCHAR Dpl,
79 _In_ UCHAR SegMode) // 0: 16-bit, 1: 32-bit, 2: 64-bit
80{
81 KiSetGdtEntryEx(Entry, Base, Limit, Type, Dpl, FALSE, SegMode);
82}
83
84#if defined(__GNUC__)
85
87VOID
89{
91 __asm__ __volatile__(
92 "lgdt %0"
93 : "=m" (*desc)
94 : /* no input */
95 : "memory");
96}
97
99VOID
100__sgdt(_Out_ PVOID Descriptor)
101{
103 __asm__ __volatile__(
104 "sgdt %0"
105 : "=m" (*desc)
106 : /* no input */
107 : "memory");
108}
109
111VOID
112Ke386FxStore(IN PFX_SAVE_AREA SaveArea)
113{
114 asm volatile ("fxrstor (%0)" : : "r"(SaveArea));
115}
116
118VOID
119Ke386FxSave(IN PFX_SAVE_AREA SaveArea)
120{
121 asm volatile ("fxsave (%0)" : : "r"(SaveArea));
122}
123
125VOID
126Ke386FnSave(IN PFLOATING_SAVE_AREA SaveArea)
127{
128 asm volatile ("fnsave (%0); wait" : : "r"(SaveArea));
129}
130
132VOID
133Ke386SaveFpuState(IN PFX_SAVE_AREA SaveArea)
134{
137 {
138 __asm__ __volatile__ ("fxsave %0\n" : : "m"(*SaveArea));
139 }
140 else
141 {
142 __asm__ __volatile__ ("fnsave %0\n wait\n" : : "m"(*SaveArea));
143 }
144}
145
147VOID
148Ke386RestoreFpuState(_In_ PFX_SAVE_AREA SaveArea)
149{
152 {
153 __asm__ __volatile__ ("fxrstor %0\n" : : "m"(*SaveArea));
154 }
155 else
156 {
157 __asm__ __volatile__ ("frstor %0\n\t" : "=m" (*SaveArea));
158 }
159}
160
162VOID
163Ke386ClearFpExceptions(VOID)
164{
165 __asm__ __volatile__ ("fnclex");
166}
167
169VOID
170__sldt(PVOID Descriptor)
171{
172 __asm__ __volatile__(
173 "sldt %0"
174 : "=m" (*((short*)Descriptor))
175 : /* no input */
176 : "memory");
177}
178
179#define Ke386SetLocalDescriptorTable(X) \
180 __asm__("lldt %w0\n\t" \
181 : /* no outputs */ \
182 : "q" (X));
183
184#define Ke386SetTr(X) __asm__ __volatile__("ltr %%ax" : :"a" (X));
185
187USHORT
188Ke386GetTr(VOID)
189{
190 USHORT Tr;
191 __asm__("str %0\n\t"
192 : "=m" (Tr));
193 return Tr;
194}
195
196#define _Ke386GetSeg(N) ({ \
197 unsigned int __d; \
198 __asm__("movl %%" #N ",%0\n\t" :"=r" (__d)); \
199 __d; \
200 })
201
202#define _Ke386SetSeg(N,X) __asm__ __volatile__("movl %0,%%" #N : :"r" (X));
203
204#define Ke386FnInit() __asm__("fninit\n\t");
205#define Ke386ClearDirectionFlag() __asm__ __volatile__ ("cld")
206
207
208//
209// CR Macros
210//
211#define Ke386SetCr2(X) __asm__ __volatile__("movl %0,%%cr2" : :"r" (X));
212
213//
214// Segment Macros
215//
216#define Ke386GetSs() _Ke386GetSeg(ss)
217#define Ke386GetFs() _Ke386GetSeg(fs)
218#define Ke386GetDs() _Ke386GetSeg(ds)
219#define Ke386GetEs() _Ke386GetSeg(es)
220#define Ke386GetGs() _Ke386GetSeg(gs)
221#define Ke386SetFs(X) _Ke386SetSeg(fs, X)
222#define Ke386SetDs(X) _Ke386SetSeg(ds, X)
223#define Ke386SetEs(X) _Ke386SetSeg(es, X)
224#define Ke386SetSs(X) _Ke386SetSeg(ss, X)
225#define Ke386SetGs(X) _Ke386SetSeg(gs, X)
226
227#elif defined(_MSC_VER)
228
230VOID
231Ke386FnInit(VOID)
232{
233 __asm fninit;
234}
235
237VOID
238__sgdt(OUT PVOID Descriptor)
239{
240 __asm
241 {
242 mov eax, Descriptor
243 sgdt [eax]
244 }
245}
246
248VOID
249__fxsave(OUT PFX_SAVE_AREA SaveArea)
250{
251 __asm mov eax, SaveArea
252 __asm fxsave [eax]
253}
254
256VOID
257__fxrstor(IN PFX_SAVE_AREA SaveArea)
258{
259 __asm mov eax, SaveArea
260 __asm fxrstor [eax]
261}
262
264VOID
265__frstor(_In_ PFX_SAVE_AREA SaveArea)
266{
267 __asm mov eax, SaveArea
268 __asm frstor [eax]
269}
270
272VOID
273__fnsave(OUT PFLOATING_SAVE_AREA SaveArea)
274{
275 __asm mov eax, SaveArea
276 __asm fnsave [eax]
277 __asm wait;
278}
279
281VOID
282__lgdt(IN PVOID Descriptor)
283{
284 __asm
285 {
286 mov eax, Descriptor
287 lgdt [eax]
288 }
289}
290
292VOID
293__sldt(PVOID Descriptor)
294{
295 __asm
296 {
297 sldt ax
298 mov ecx, Descriptor
299 mov [ecx], ax
300 }
301}
302
304VOID
305Ke386SetLocalDescriptorTable(IN USHORT Descriptor)
306{
307 __asm lldt Descriptor;
308}
309
311VOID
312Ke386SetTr(IN USHORT Tr)
313{
314 __asm ltr Tr;
315}
316
318USHORT
319Ke386GetTr(VOID)
320{
321 __asm str ax;
322}
323
324//
325// CR Macros
326//
328VOID
329Ke386SetCr2(IN ULONG Value)
330{
331 __asm mov eax, Value;
332 __asm mov cr2, eax;
333}
334
335//
336// Segment Macros
337//
339USHORT
340Ke386GetSs(VOID)
341{
342 __asm mov ax, ss;
343}
344
346USHORT
347Ke386GetFs(VOID)
348{
349 __asm mov ax, fs;
350}
351
353USHORT
354Ke386GetDs(VOID)
355{
356 __asm mov ax, ds;
357}
358
360USHORT
361Ke386GetEs(VOID)
362{
363 __asm mov ax, es;
364}
365
367VOID
368Ke386SetSs(IN USHORT Value)
369{
370 __asm mov ax, Value;
371 __asm mov ss, ax;
372}
373
375VOID
376Ke386SetFs(IN USHORT Value)
377{
378 __asm mov ax, Value;
379 __asm mov fs, ax;
380}
381
383VOID
384Ke386SetDs(IN USHORT Value)
385{
386 __asm mov ax, Value;
387 __asm mov ds, ax;
388}
389
391VOID
392Ke386SetEs(IN USHORT Value)
393{
394 __asm mov ax, Value;
395 __asm mov es, ax;
396}
397
399VOID
400Ke386SetGs(IN USHORT Value)
401{
402 __asm mov ax, Value;
403 __asm mov gs, ax;
404}
405
407
409VOID
410Ke386SaveFpuState(IN PVOID SaveArea)
411{
413 {
414 __fxsave((PFX_SAVE_AREA)SaveArea);
415 }
416 else
417 {
418 __fnsave((PFLOATING_SAVE_AREA)SaveArea);
419 }
420}
421
423VOID
424Ke386RestoreFpuState(_In_ PVOID SaveArea)
425{
427 {
428 __fxrstor((PFX_SAVE_AREA)SaveArea);
429 }
430 else
431 {
432 __frstor((PFX_SAVE_AREA)SaveArea);
433 }
434}
435
437VOID
438Ke386ClearFpExceptions(VOID)
439{
440 __asm fnclex;
441}
442
443#define Ke386FnSave __fnsave
444#define Ke386FxSave __fxsave
445// The name suggest, that the original author didn't understand what frstor means
446#define Ke386FxStore __fxrstor
447
448#else
449#error Unknown compiler for inline assembler
450#endif
451
452#define Ke386GetGlobalDescriptorTable __sgdt
453#define Ke386SetGlobalDescriptorTable __lgdt
454#define Ke386GetLocalDescriptorTable __sldt
455
456#ifdef __cplusplus
457} // extern "C"
458#endif
459
460/* EOF */
unsigned char BOOLEAN
Type
Definition: Type.h:7
FORCEINLINE VOID KiSetGdtDescriptorLimit(PKGDTENTRY Entry, ULONG Limit)
Definition: intrin_i.h:40
FORCEINLINE PKGDTENTRY64 KiGetGdtEntry(PVOID pGdt, USHORT Selector)
Definition: intrin_i.h:13
FORCEINLINE VOID KiSetGdtDescriptorBase(PKGDTENTRY Entry, ULONG64 Base)
Definition: intrin_i.h:30
unsigned int ULONG32
Definition: basetsd.h:123
#define FALSE
Definition: types.h:117
#define ULONG_PTR
Definition: config.h:101
#define fs
Definition: i386-dis.c:444
#define es
Definition: i386-dis.c:440
#define ss
Definition: i386-dis.c:441
#define ds
Definition: i386-dis.c:443
#define gs
Definition: i386-dis.c:445
FORCEINLINE VOID KiSetGdtEntryEx(_Inout_ PKGDTENTRY Entry, _In_ ULONG32 Base, _In_ ULONG Limit, _In_ UCHAR Type, _In_ UCHAR Dpl, _In_ BOOLEAN Granularity, _In_ UCHAR SegMode)
Definition: intrin_i.h:50
FORCEINLINE VOID KiSetGdtEntry(_Inout_ PKGDTENTRY Entry, _In_ ULONG32 Base, _In_ ULONG Limit, _In_ UCHAR Type, _In_ UCHAR Dpl, _In_ UCHAR SegMode)
Definition: intrin_i.h:73
static const WCHAR desc[]
Definition: protectdata.c:36
#define _Inout_
Definition: ms_sal.h:378
#define _Out_
Definition: ms_sal.h:345
#define _In_
Definition: ms_sal.h:308
#define RPL_MASK
Definition: ketypes.h:119
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2451
ULONG KeI386FxsrPresent
Definition: cpu.c:33
unsigned short USHORT
Definition: pedump.c:61
__asm__(".p2align 4, 0x90\n" ".seh_proc __seh2_global_filter_func\n" "__seh2_global_filter_func:\n" "\tsub %rbp, %rax\n" "\tpush %rbp\n" "\t.seh_pushreg %rbp\n" "\tsub $32, %rsp\n" "\t.seh_stackalloc 32\n" "\t.seh_endprologue\n" "\tsub %rax, %rdx\n" "\tmov %rdx, %rbp\n" "\tjmp *%r8\n" "__seh2_global_filter_func_exit:\n" "\t.p2align 4\n" "\tadd $32, %rsp\n" "\tpop %rbp\n" "\tret\n" "\t.seh_endproc")
const WCHAR * str
base of all file and directory entries
Definition: entries.h:83
ecx edi movl ebx edx edi decl ecx esi eax jecxz decl eax andl eax esi movl edx movl TEMP incl eax andl eax ecx incl ebx testl eax jnz xchgl ecx incl TEMP esp ecx subl ebx pushl ecx ecx edx ecx shrl ecx mm0 mm4 mm0 mm4 mm1 mm5 mm1 mm5 mm2 mm6 mm2 mm6 mm3 mm7 mm3 mm7 paddd mm0 paddd mm4 paddd mm0 paddd mm4 paddd mm0 paddd mm4 movq mm1 movq mm5 psrlq mm1 psrlq mm5 paddd mm0 paddd mm4 psrad mm0 psrad mm4 packssdw mm0 packssdw mm4 mm1 punpckldq mm0 pand mm1 pand mm0 por mm1 movq edi esi edx edi decl ecx jnz popl ecx andl ecx jecxz mm0 mm0 mm1 mm1 mm2 mm2 mm3 mm3 paddd mm0 paddd mm0 paddd mm0 movq mm1 psrlq mm1 paddd mm0 psrad mm0 packssdw mm0 movd eax movw ax
Definition: synth_sse3d.h:180
ecx edi movl ebx edx edi decl ecx esi eax jecxz decl eax andl eax esi movl eax
Definition: synth_sse3d.h:85
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
_Must_inspect_result_ _In_ WDFIORESLIST _In_ PIO_RESOURCE_DESCRIPTOR Descriptor
Definition: wdfresource.h:342
#define FORCEINLINE
Definition: wdftypes.h:67
_In_ LONG _In_ LONG Limit
Definition: kefuncs.h:304
unsigned char UCHAR
Definition: xmlstorage.h:181