2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ke/i386/cpu.c
5 * PURPOSE: Routines for CPU-level support
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES *****************************************************************/
15 /* FIXME: Local EFLAGS defines not used anywhere else */
16 #define EFLAGS_IOPL 0x3000
17 #define EFLAGS_NF 0x4000
18 #define EFLAGS_RF 0x10000
19 #define EFLAGS_ID 0x200000
21 /* GLOBALS *******************************************************************/
26 /* The TSS to use for Double Fault Traps (INT 0x9) */
27 UCHAR KiDoubleFaultTSS
[KTSS_IO_MAPS
];
29 /* The TSS to use for NMI Fault Traps (INT 0x2) */
30 UCHAR KiNMITSS
[KTSS_IO_MAPS
];
33 KGDTENTRY KiBootGdt
[256] =
35 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}}, /* KGDT_NULL */
36 {0xffff, 0x0000, {{0x00, 0x9b, 0xcf, 0x00}}}, /* KGDT_R0_CODE */
37 {0xffff, 0x0000, {{0x00, 0x93, 0xcf, 0x00}}}, /* KGDT_R0_DATA */
38 {0xffff, 0x0000, {{0x00, 0xfb, 0xcf, 0x00}}}, /* KGDT_R3_CODE */
39 {0xffff, 0x0000, {{0x00, 0xf3, 0xcf, 0x00}}}, /* KGDT_R3_DATA*/
40 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}}, /* KGDT_TSS */
41 {0x0fff, 0x0000, {{0x00, 0x93, 0xc0, 0xff}}}, /* KGDT_R0_PCR */
42 {0x0fff, 0x0000, {{0x00, 0xf3, 0x40, 0x00}}}, /* KGDT_R3_TEB */
43 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}}, /* KGDT_UNUSED */
44 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}}, /* KGDT_LDT */
45 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}}, /* KGDT_DF_TSS */
46 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}} /* KGDT_NMI_TSS */
50 KDESCRIPTOR KiGdtDescriptor
= {sizeof(KiBootGdt
), (ULONG
)KiBootGdt
};
52 /* CPU Features and Flags */
55 ULONG KeProcessorArchitecture
;
56 ULONG KeProcessorLevel
;
57 ULONG KeProcessorRevision
;
59 ULONG KiFastSystemCallDisable
= 1;
60 ULONG KeI386NpxPresent
= 0;
61 ULONG KiMXCsrMask
= 0;
62 ULONG MxcsrFeatureMask
= 0;
63 ULONG KeI386XMMIPresent
= 0;
64 ULONG KeI386FxsrPresent
= 0;
65 ULONG KeI386MachineType
;
66 ULONG Ke386Pae
= FALSE
;
67 ULONG Ke386NoExecute
= FALSE
;
68 ULONG KeLargestCacheLine
= 0x40;
69 ULONG KeDcacheFlushCount
= 0;
70 ULONG KeIcacheFlushCount
= 0;
71 ULONG KiDmaIoCoherency
= 0;
72 CHAR KeNumberProcessors
;
73 KAFFINITY KeActiveProcessors
= 1;
74 BOOLEAN KiI386PentiumLockErrataPresent
;
75 BOOLEAN KiSMTProcessorsPresent
;
78 static const CHAR CmpIntelID
[] = "GenuineIntel";
79 static const CHAR CmpAmdID
[] = "AuthenticAMD";
80 static const CHAR CmpCyrixID
[] = "CyrixInstead";
81 static const CHAR CmpTransmetaID
[] = "GenuineTMx86";
82 static const CHAR CmpCentaurID
[] = "CentaurHauls";
83 static const CHAR CmpRiseID
[] = "RiseRiseRise";
85 /* SUPPORT ROUTINES FOR MSVC COMPATIBILITY ***********************************/
89 CPUID(OUT ULONG CpuInfo
[4],
92 Ki386Cpuid(InfoType
, &CpuInfo
[0], &CpuInfo
[1], &CpuInfo
[2], &CpuInfo
[3]);
96 WRMSR(IN ULONG Register
,
99 LARGE_INTEGER LargeVal
;
100 LargeVal
.QuadPart
= Value
;
101 Ke386Wrmsr(Register
, LargeVal
.HighPart
, LargeVal
.LowPart
);
105 RDMSR(IN ULONG Register
)
107 LARGE_INTEGER LargeVal
;
108 Ke386Rdmsr(Register
, LargeVal
.HighPart
, LargeVal
.LowPart
);
109 return LargeVal
.QuadPart
;
112 /* FUNCTIONS *****************************************************************/
116 KiSetProcessorType(VOID
)
118 ULONG EFlags
, NewEFlags
;
120 ULONG Stepping
, Type
;
122 /* Start by assuming no CPUID data */
123 KeGetCurrentPrcb()->CpuID
= 0;
126 Ke386SaveFlags(EFlags
);
128 /* XOR out the ID bit and update EFlags */
129 NewEFlags
= EFlags
^ EFLAGS_ID
;
130 Ke386RestoreFlags(NewEFlags
);
132 /* Get them back and see if they were modified */
133 Ke386SaveFlags(NewEFlags
);
134 if (NewEFlags
!= EFlags
)
136 /* The modification worked, so CPUID exists. Set the ID Bit again. */
138 Ke386RestoreFlags(EFlags
);
140 /* Peform CPUID 0 to see if CPUID 1 is supported */
148 * Get the Stepping and Type. The stepping contains both the
149 * Model and the Step, while the Type contains the returned Type.
150 * We ignore the family.
152 * For the stepping, we convert this: zzzzzzxy into this: x0y
154 Stepping
= Reg
[0] & 0xF0;
156 Stepping
+= (Reg
[0] & 0xFF);
158 Type
= Reg
[0] & 0xF00;
161 /* Save them in the PRCB */
162 KeGetCurrentPrcb()->CpuID
= TRUE
;
163 KeGetCurrentPrcb()->CpuType
= (UCHAR
)Type
;
164 KeGetCurrentPrcb()->CpuStep
= (USHORT
)Stepping
;
168 DPRINT1("CPUID Support lacking\n");
173 DPRINT1("CPUID Support lacking\n");
177 Ke386RestoreFlags(EFlags
);
184 PKPRCB Prcb
= KeGetCurrentPrcb();
188 /* Assume no Vendor ID and fail if no CPUID Support. */
189 Prcb
->VendorString
[0] = 0;
190 if (!Prcb
->CpuID
) return 0;
192 /* Get the Vendor ID and null-terminate it */
196 /* Re-arrange vendor string */
198 Vendor
[2] = Vendor
[3];
201 /* Copy it to the PRCB and null-terminate it again */
202 RtlCopyMemory(Prcb
->VendorString
,
204 sizeof(Prcb
->VendorString
) - sizeof(CHAR
));
205 Prcb
->VendorString
[sizeof(Prcb
->VendorString
) - sizeof(CHAR
)] = ANSI_NULL
;
207 /* Now check the CPU Type */
208 if (!strcmp(Prcb
->VendorString
, CmpIntelID
))
212 else if (!strcmp(Prcb
->VendorString
, CmpAmdID
))
216 else if (!strcmp(Prcb
->VendorString
, CmpCyrixID
))
218 DPRINT1("Cyrix CPUs not fully supported\n");
221 else if (!strcmp(Prcb
->VendorString
, CmpTransmetaID
))
223 DPRINT1("Transmeta CPUs not fully supported\n");
226 else if (!strcmp(Prcb
->VendorString
, CmpCentaurID
))
228 DPRINT1("VIA CPUs not fully supported\n");
231 else if (!strcmp(Prcb
->VendorString
, CmpRiseID
))
233 DPRINT1("Rise CPUs not fully supported\n");
243 KiGetFeatureBits(VOID
)
245 PKPRCB Prcb
= KeGetCurrentPrcb();
247 ULONG FeatureBits
= KF_WORKING_PTE
;
249 BOOLEAN ExtendedCPUID
= TRUE
;
250 ULONG CpuFeatures
= 0;
252 /* Get the Vendor ID */
253 Vendor
= KiGetCpuVendor();
255 /* Make sure we got a valid vendor ID at least. */
256 if (!Vendor
) return FeatureBits
;
258 /* Get the CPUID Info. Features are in Reg[3]. */
261 /* Set the initial APIC ID */
262 Prcb
->InitialApicId
= (UCHAR
)(Reg
[1] >> 24);
264 /* Check for AMD CPU */
265 if (Vendor
== CPU_AMD
)
267 /* Check if this is a K5 or higher. */
268 if ((Reg
[0] & 0x0F00) >= 0x0500)
270 /* Check if this is a K5 specifically. */
271 if ((Reg
[0] & 0x0F00) == 0x0500)
273 /* Get the Model Number */
274 switch (Reg
[0] & 0x00F0)
276 /* Check if this is the Model 1 */
279 /* Check if this is Step 0 or 1. They don't support PGE */
280 if ((Reg
[0] & 0x000F) > 0x03) break;
284 /* Model 0 doesn't support PGE at all. */
290 /* K6-2, Step 8 and over have support for MTRR. */
291 if ((Reg
[0] & 0x000F) >= 0x8) FeatureBits
|= KF_AMDK6MTRR
;
296 /* As does the K6-3 */
297 FeatureBits
|= KF_AMDK6MTRR
;
307 /* Families below 5 don't support PGE, PSE or CMOV at all */
308 Reg
[3] &= ~(0x08 | 0x2000 | 0x8000);
310 /* They also don't support advanced CPUID functions. */
311 ExtendedCPUID
= FALSE
;
314 /* Set the current features */
315 CpuFeatures
= Reg
[3];
318 /* Now check if this is Intel */
319 if (Vendor
== CPU_INTEL
)
321 /* Check if it's a P6 */
322 if (Prcb
->CpuType
== 6)
324 /* Perform the special sequence to get the MicroCode Signature */
327 Prcb
->UpdateSignature
.QuadPart
= RDMSR(0x8B);
329 else if (Prcb
->CpuType
== 5)
331 /* On P5, enable workaround for the LOCK errata. */
332 KiI386PentiumLockErrataPresent
= TRUE
;
335 /* Check for broken P6 with bad SMP PTE implementation */
336 if (((Reg
[0] & 0x0FF0) == 0x0610 && (Reg
[0] & 0x000F) <= 0x9) ||
337 ((Reg
[0] & 0x0FF0) == 0x0630 && (Reg
[0] & 0x000F) <= 0x4))
339 /* Remove support for correct PTE support. */
340 FeatureBits
&= ~KF_WORKING_PTE
;
343 /* Check if the CPU is too old to support SYSENTER */
344 if ((Prcb
->CpuType
< 6) ||
345 ((Prcb
->CpuType
== 6) && (Prcb
->CpuStep
< 0x0303)))
351 /* Set the current features */
352 CpuFeatures
= Reg
[3];
355 /* Convert all CPUID Feature bits into our format */
356 if (CpuFeatures
& 0x00000002) FeatureBits
|= KF_V86_VIS
| KF_CR4
;
357 if (CpuFeatures
& 0x00000008) FeatureBits
|= KF_LARGE_PAGE
| KF_CR4
;
358 if (CpuFeatures
& 0x00000010) FeatureBits
|= KF_RDTSC
;
359 if (CpuFeatures
& 0x00000100) FeatureBits
|= KF_CMPXCHG8B
;
360 if (CpuFeatures
& 0x00000800) FeatureBits
|= KF_FAST_SYSCALL
;
361 if (CpuFeatures
& 0x00001000) FeatureBits
|= KF_MTRR
;
362 if (CpuFeatures
& 0x00002000) FeatureBits
|= KF_GLOBAL_PAGE
| KF_CR4
;
363 if (CpuFeatures
& 0x00008000) FeatureBits
|= KF_CMOV
;
364 if (CpuFeatures
& 0x00010000) FeatureBits
|= KF_PAT
;
365 if (CpuFeatures
& 0x00200000) FeatureBits
|= KF_DTS
;
366 if (CpuFeatures
& 0x00800000) FeatureBits
|= KF_MMX
;
367 if (CpuFeatures
& 0x01000000) FeatureBits
|= KF_FXSR
;
368 if (CpuFeatures
& 0x02000000) FeatureBits
|= KF_XMMI
;
369 if (CpuFeatures
& 0x04000000) FeatureBits
|= KF_XMMI64
;
371 /* Check if the CPU has hyper-threading */
372 if (CpuFeatures
& 0x10000000)
374 /* Set the number of logical CPUs */
375 Prcb
->LogicalProcessorsPerPhysicalProcessor
= (UCHAR
)(Reg
[1] >> 16);
376 if (Prcb
->LogicalProcessorsPerPhysicalProcessor
> 1)
378 /* We're on dual-core */
379 KiSMTProcessorsPresent
= TRUE
;
384 /* We only have a single CPU */
385 Prcb
->LogicalProcessorsPerPhysicalProcessor
= 1;
388 /* Check if CPUID 0x80000000 is supported */
392 CPUID(Reg
, 0x80000000);
393 if ((Reg
[0] & 0xffffff00) == 0x80000000)
395 /* Check if CPUID 0x80000001 is supported */
396 if (Reg
[0] >= 0x80000001)
398 /* Check which extended features are available. */
399 CPUID(Reg
, 0x80000001);
401 /* Check if NX-bit is supported */
402 if (Reg
[3] & 0x00100000) FeatureBits
|= KF_NX_BIT
;
404 /* Now handle each features for each CPU Vendor */
408 if (Reg
[3] & 0x80000000) FeatureBits
|= KF_3DNOW
;
415 /* Return the Feature Bits */
421 KiGetCacheInformation(VOID
)
423 PKIPCR Pcr
= (PKIPCR
)KeGetPcr();
426 ULONG CacheRequests
= 0, i
;
427 ULONG CurrentRegister
;
429 BOOLEAN FirstPass
= TRUE
;
431 /* Set default L2 size */
432 Pcr
->SecondLevelCacheSize
= 0;
434 /* Get the Vendor ID and make sure we support CPUID */
435 Vendor
= KiGetCpuVendor();
438 /* Check the Vendor ID */
441 /* Handle Intel case */
444 /*Check if we support CPUID 2 */
448 /* We need to loop for the number of times CPUID will tell us to */
451 /* Do the CPUID call */
454 /* Check if it was the first call */
458 * The number of times to loop is the first byte. Read
459 * it and then destroy it so we don't get confused.
461 CacheRequests
= Data
[0] & 0xFF;
462 Data
[0] &= 0xFFFFFF00;
464 /* Don't go over this again */
468 /* Loop all 4 registers */
469 for (i
= 0; i
< 4; i
++)
471 /* Get the current register */
472 CurrentRegister
= Data
[i
];
475 * If the upper bit is set, then this register should
478 if (CurrentRegister
& 0x80000000) continue;
480 /* Keep looping for every byte inside this register */
481 while (CurrentRegister
)
483 /* Read a byte, skip a byte. */
484 RegisterByte
= (UCHAR
)(CurrentRegister
& 0xFF);
485 CurrentRegister
>>= 8;
486 if (!RegisterByte
) continue;
489 * Valid values are from 0x40 (0 bytes) to 0x49
490 * (32MB), or from 0x80 to 0x89 (same size but
493 if (((RegisterByte
> 0x40) &&
494 (RegisterByte
<= 0x49)) ||
495 ((RegisterByte
> 0x80) &&
496 (RegisterByte
<= 0x89)))
498 /* Mask out only the first nibble */
499 RegisterByte
&= 0x0F;
501 /* Set the L2 Cache Size */
502 Pcr
->SecondLevelCacheSize
= 0x10000 <<
507 } while (--CacheRequests
);
514 DPRINT1("Not handling AMD caches yet\n");
525 /* Save current CR0 */
528 /* If this is a 486, enable Write-Protection */
529 if (KeGetCurrentPrcb()->CpuType
> 3) Cr0
|= CR0_WP
;
537 KiInitializeTSS2(IN PKTSS Tss
,
538 IN PKGDTENTRY TssEntry OPTIONAL
)
542 /* Make sure the GDT Entry is valid */
546 TssEntry
->LimitLow
= sizeof(KTSS
) - 1;
547 TssEntry
->HighWord
.Bits
.LimitHi
= 0;
550 /* Now clear the I/O Map */
551 RtlFillMemory(Tss
->IoMaps
[0].IoMap
, 8096, -1);
553 /* Initialize Interrupt Direction Maps */
554 p
= (PUCHAR
)(Tss
->IoMaps
[0].DirectionMap
);
555 RtlZeroMemory(p
, 32);
557 /* Add DPMI support for interrupts */
562 /* Initialize the default Interrupt Direction Map */
563 p
= Tss
->IntDirectionMap
;
564 RtlZeroMemory(Tss
->IntDirectionMap
, 32);
566 /* Add DPMI support */
574 KiInitializeTSS(IN PKTSS Tss
)
576 /* Set an invalid map base */
577 Tss
->IoMapBase
= KiComputeIopmOffset(IO_ACCESS_MAP_NONE
);
579 /* Disable traps during Task Switches */
582 /* Set LDT and Ring 0 SS */
584 Tss
->Ss0
= KGDT_R0_DATA
;
589 Ki386InitializeTss(IN PKTSS Tss
,
593 PKGDTENTRY TssEntry
, TaskGateEntry
;
595 /* Initialize the boot TSS. */
596 TssEntry
= &Gdt
[KGDT_TSS
/ sizeof(KGDTENTRY
)];
597 TssEntry
->HighWord
.Bits
.Type
= I386_TSS
;
598 TssEntry
->HighWord
.Bits
.Pres
= 1;
599 TssEntry
->HighWord
.Bits
.Dpl
= 0;
600 KiInitializeTSS2(Tss
, TssEntry
);
601 KiInitializeTSS(Tss
);
603 /* Load the task register */
604 Ke386SetTr(KGDT_TSS
);
606 /* Setup the Task Gate for Double Fault Traps */
607 TaskGateEntry
= (PKGDTENTRY
)&Idt
[8];
608 TaskGateEntry
->HighWord
.Bits
.Type
= I386_TASK_GATE
;
609 TaskGateEntry
->HighWord
.Bits
.Pres
= 1;
610 TaskGateEntry
->HighWord
.Bits
.Dpl
= 0;
611 ((PKIDTENTRY
)TaskGateEntry
)->Selector
= KGDT_DF_TSS
;
613 /* Initialize the TSS used for handling double faults. */
614 Tss
= (PKTSS
)KiDoubleFaultTSS
;
615 KiInitializeTSS(Tss
);
616 Tss
->CR3
= __readcr3();
617 Tss
->Esp0
= PtrToUlong(KiDoubleFaultStack
);
618 Tss
->Eip
= PtrToUlong(KiTrap8
);
619 Tss
->Cs
= KGDT_R0_CODE
;
620 Tss
->Fs
= KGDT_R0_PCR
;
621 Tss
->Ss
= Ke386GetSs();
622 Tss
->Es
= KGDT_R3_DATA
| RPL_MASK
;
623 Tss
->Ds
= KGDT_R3_DATA
| RPL_MASK
;
625 /* Setup the Double Trap TSS entry in the GDT */
626 TssEntry
= &Gdt
[KGDT_DF_TSS
/ sizeof(KGDTENTRY
)];
627 TssEntry
->HighWord
.Bits
.Type
= I386_TSS
;
628 TssEntry
->HighWord
.Bits
.Pres
= 1;
629 TssEntry
->HighWord
.Bits
.Dpl
= 0;
630 TssEntry
->BaseLow
= (USHORT
)((ULONG_PTR
)Tss
& 0xFFFF);
631 TssEntry
->HighWord
.Bytes
.BaseMid
= (UCHAR
)((ULONG_PTR
)Tss
>> 16);
632 TssEntry
->HighWord
.Bytes
.BaseHi
= (UCHAR
)((ULONG_PTR
)Tss
>> 24);
633 TssEntry
->LimitLow
= KTSS_IO_MAPS
;
635 /* Now setup the NMI Task Gate */
636 TaskGateEntry
= (PKGDTENTRY
)&Idt
[2];
637 TaskGateEntry
->HighWord
.Bits
.Type
= I386_TASK_GATE
;
638 TaskGateEntry
->HighWord
.Bits
.Pres
= 1;
639 TaskGateEntry
->HighWord
.Bits
.Dpl
= 0;
640 ((PKIDTENTRY
)TaskGateEntry
)->Selector
= KGDT_NMI_TSS
;
642 /* Initialize the actual TSS */
643 Tss
= (PKTSS
)KiNMITSS
;
644 KiInitializeTSS(Tss
);
645 Tss
->CR3
= __readcr3();
646 Tss
->Esp0
= PtrToUlong(KiDoubleFaultStack
);
647 Tss
->Eip
= PtrToUlong(KiTrap2
);
648 Tss
->Cs
= KGDT_R0_CODE
;
649 Tss
->Fs
= KGDT_R0_PCR
;
650 Tss
->Ss
= Ke386GetSs();
651 Tss
->Es
= KGDT_R3_DATA
| RPL_MASK
;
652 Tss
->Ds
= KGDT_R3_DATA
| RPL_MASK
;
654 /* And its associated TSS Entry */
655 TssEntry
= &Gdt
[KGDT_NMI_TSS
/ sizeof(KGDTENTRY
)];
656 TssEntry
->HighWord
.Bits
.Type
= I386_TSS
;
657 TssEntry
->HighWord
.Bits
.Pres
= 1;
658 TssEntry
->HighWord
.Bits
.Dpl
= 0;
659 TssEntry
->BaseLow
= (USHORT
)((ULONG_PTR
)Tss
& 0xFFFF);
660 TssEntry
->HighWord
.Bytes
.BaseMid
= (UCHAR
)((ULONG_PTR
)Tss
>> 16);
661 TssEntry
->HighWord
.Bytes
.BaseHi
= (UCHAR
)((ULONG_PTR
)Tss
>> 24);
662 TssEntry
->LimitLow
= KTSS_IO_MAPS
;
667 KeFlushCurrentTb(VOID
)
669 /* Flush the TLB by resetting CR3 */
670 __writecr3((ULONGLONG
)__readcr3());
675 KiSaveProcessorControlState(IN PKPROCESSOR_STATE ProcessorState
)
677 /* Save the CR registers */
678 ProcessorState
->SpecialRegisters
.Cr0
= __readcr0();
679 ProcessorState
->SpecialRegisters
.Cr2
= __readcr2();
680 ProcessorState
->SpecialRegisters
.Cr3
= __readcr3();
681 ProcessorState
->SpecialRegisters
.Cr4
= __readcr4();
683 /* Save the DR registers */
684 ProcessorState
->SpecialRegisters
.KernelDr0
= Ke386GetDr0();
685 ProcessorState
->SpecialRegisters
.KernelDr1
= Ke386GetDr1();
686 ProcessorState
->SpecialRegisters
.KernelDr2
= Ke386GetDr2();
687 ProcessorState
->SpecialRegisters
.KernelDr3
= Ke386GetDr3();
688 ProcessorState
->SpecialRegisters
.KernelDr6
= Ke386GetDr6();
689 ProcessorState
->SpecialRegisters
.KernelDr7
= Ke386GetDr7();
692 /* Save GDT, IDT, LDT and TSS */
693 Ke386GetGlobalDescriptorTable(ProcessorState
->SpecialRegisters
.Gdtr
);
694 Ke386GetInterruptDescriptorTable(ProcessorState
->SpecialRegisters
.Idtr
);
695 Ke386GetTr(ProcessorState
->SpecialRegisters
.Tr
);
696 Ke386GetLocalDescriptorTable(ProcessorState
->SpecialRegisters
.Ldtr
);
701 KiInitializeMachineType(VOID
)
703 /* Set the Machine Type we got from NTLDR */
704 KeI386MachineType
= KeLoaderBlock
->u
.I386
.MachineType
& 0x000FF;
709 KiLoadFastSyscallMachineSpecificRegisters(IN ULONG_PTR Context
)
712 Ke386Wrmsr(0x174, KGDT_R0_CODE
, 0);
713 Ke386Wrmsr(0x175, KeGetCurrentPrcb()->DpcStack
, 0);
716 Ke386Wrmsr(0x176, KiFastCallEntry
, 0);
722 KiRestoreFastSyscallReturnState(VOID
)
724 /* FIXME: NT has support for SYSCALL, IA64-SYSENTER, etc. */
726 /* Check if the CPU Supports fast system call */
727 if (KeFeatureBits
& KF_FAST_SYSCALL
)
729 /* Do an IPI to enable it */
730 KeIpiGenericCall(KiLoadFastSyscallMachineSpecificRegisters
, 0);
736 Ki386EnableDE(IN ULONG_PTR Context
)
739 __writecr4(__readcr4() | CR4_DE
);
745 Ki386EnableFxsr(IN ULONG_PTR Context
)
748 __writecr4(__readcr4() | CR4_FXSR
);
754 Ki386EnableXMMIExceptions(IN ULONG_PTR Context
)
756 #if 0 // needs kitrap13
759 /* Get the IDT Entry for Interrupt 19 */
760 IdtEntry
= ((PKIPCR
)KeGetPcr())->IDT
[19];
763 IdtEntry
->Selector
= KGDT_R0_CODE
;
764 IdtEntry
->Offset
= (KiTrap13
& 0xFFFF);
765 IdtEntry
->ExtendedOffset
= (KiTrap13
>> 16) & 0xFFFF;
766 ((PKIDT_ACCESS
)&IdtEntry
->Access
)->Dpl
= 0;
767 ((PKIDT_ACCESS
)&IdtEntry
->Access
)->Present
= 1;
768 ((PKIDT_ACCESS
)&IdtEntry
->Access
)->SegmentType
= I386_INTERRUPT_GATE
;
771 /* Enable XMMI exceptions */
772 __writecr4(__readcr4() | CR4_XMMEXCPT
);
778 KiI386PentiumLockErrataFixup(VOID
)
780 KDESCRIPTOR IdtDescriptor
;
781 PKIDTENTRY NewIdt
, NewIdt2
;
783 /* Allocate memory for a new IDT */
784 NewIdt
= ExAllocatePool(NonPagedPool
, 2 * PAGE_SIZE
);
786 /* Put everything after the first 7 entries on a new page */
787 NewIdt2
= (PVOID
)((ULONG_PTR
)NewIdt
+ PAGE_SIZE
- (7 * sizeof(KIDTENTRY
)));
789 /* Disable interrupts */
792 /* Get the current IDT and copy it */
793 Ke386GetInterruptDescriptorTable(IdtDescriptor
);
794 RtlCopyMemory(NewIdt2
,
795 (PVOID
)IdtDescriptor
.Base
,
796 IdtDescriptor
.Limit
+ 1);
797 IdtDescriptor
.Base
= (ULONG
)NewIdt2
;
799 /* Set the new IDT */
800 Ke386SetInterruptDescriptorTable(IdtDescriptor
);
801 ((PKIPCR
)KeGetPcr())->IDT
= NewIdt2
;
803 /* Restore interrupts */
806 /* Set the first 7 entries as read-only to produce a fault */
807 MmSetPageProtect(NULL
, NewIdt
, PAGE_READONLY
);
810 /* PUBLIC FUNCTIONS **********************************************************/
817 KeSaveFloatingPointState(OUT PKFLOATING_SAVE Save
)
819 PFNSAVE_FORMAT FpState
;
820 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
821 DPRINT1("%s is not really implemented\n", __FUNCTION__
);
823 /* check if we are doing software emulation */
824 if (!KeI386NpxPresent
) return STATUS_ILLEGAL_FLOAT_CONTEXT
;
826 FpState
= ExAllocatePool(NonPagedPool
, sizeof (FNSAVE_FORMAT
));
827 if (!FpState
) return STATUS_INSUFFICIENT_RESOURCES
;
829 *((PVOID
*) Save
) = FpState
;
831 asm volatile("fnsave %0\n\t" : "=m" (*FpState
));
839 KeGetCurrentThread()->DispatcherHeader
.NpxIrql
= KeGetCurrentIrql();
840 return STATUS_SUCCESS
;
848 KeRestoreFloatingPointState(IN PKFLOATING_SAVE Save
)
850 PFNSAVE_FORMAT FpState
= *((PVOID
*) Save
);
851 ASSERT(KeGetCurrentThread()->DispatcherHeader
.NpxIrql
== KeGetCurrentIrql());
852 DPRINT1("%s is not really implemented\n", __FUNCTION__
);
855 asm volatile("fnclex\n\t");
856 asm volatile("frstor %0\n\t" : "=m" (*FpState
));
866 return STATUS_SUCCESS
;
874 KeGetRecommendedSharedDataAlignment(VOID
)
876 /* Return the global variable */
877 return KeLargestCacheLine
;
885 KeFlushEntireTb(IN BOOLEAN Invalid
,
886 IN BOOLEAN AllProcessors
)
890 /* Raise the IRQL for the TB Flush */
891 OldIrql
= KeRaiseIrqlToSynchLevel();
894 /* FIXME: Support IPI Flush */
895 #error Not yet implemented!
898 /* Flush the TB for the Current CPU */
901 /* Return to Original IRQL */
902 KeLowerIrql(OldIrql
);
910 KeSetDmaIoCoherency(IN ULONG Coherency
)
912 /* Save the coherency globally */
913 KiDmaIoCoherency
= Coherency
;
921 KeQueryActiveProcessors(VOID
)
925 /* Simply return the number of active processors */
926 return KeActiveProcessors
;
934 KeSaveStateForHibernate(IN PKPROCESSOR_STATE State
)
936 /* Capture the context */
937 RtlCaptureContext(&State
->ContextFrame
);
939 /* Capture the control state */
940 KiSaveProcessorControlState(State
);