1f4a2713aSLionel Sambuc /*===---- cpuid.h - X86 cpu model detection --------------------------------===
2f4a2713aSLionel Sambuc *
3f4a2713aSLionel Sambuc * Permission is hereby granted, free of charge, to any person obtaining a copy
4f4a2713aSLionel Sambuc * of this software and associated documentation files (the "Software"), to deal
5f4a2713aSLionel Sambuc * in the Software without restriction, including without limitation the rights
6f4a2713aSLionel Sambuc * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7f4a2713aSLionel Sambuc * copies of the Software, and to permit persons to whom the Software is
8f4a2713aSLionel Sambuc * furnished to do so, subject to the following conditions:
9f4a2713aSLionel Sambuc *
10f4a2713aSLionel Sambuc * The above copyright notice and this permission notice shall be included in
11f4a2713aSLionel Sambuc * all copies or substantial portions of the Software.
12f4a2713aSLionel Sambuc *
13f4a2713aSLionel Sambuc * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14f4a2713aSLionel Sambuc * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15f4a2713aSLionel Sambuc * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16f4a2713aSLionel Sambuc * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17f4a2713aSLionel Sambuc * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18f4a2713aSLionel Sambuc * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19f4a2713aSLionel Sambuc * THE SOFTWARE.
20f4a2713aSLionel Sambuc *
21f4a2713aSLionel Sambuc *===-----------------------------------------------------------------------===
22f4a2713aSLionel Sambuc */
23f4a2713aSLionel Sambuc
24f4a2713aSLionel Sambuc #if !(__x86_64__ || __i386__)
25f4a2713aSLionel Sambuc #error this header is for x86 only
26f4a2713aSLionel Sambuc #endif
27f4a2713aSLionel Sambuc
28*0a6a1f1dSLionel Sambuc /* Responses identification request with %eax 0 */
29*0a6a1f1dSLionel Sambuc /* AMD: "AuthenticAMD" */
30*0a6a1f1dSLionel Sambuc #define signature_AMD_ebx 0x68747541
31*0a6a1f1dSLionel Sambuc #define signature_AMD_edx 0x69746e65
32*0a6a1f1dSLionel Sambuc #define signature_AMD_ecx 0x444d4163
33*0a6a1f1dSLionel Sambuc /* CENTAUR: "CentaurHauls" */
34*0a6a1f1dSLionel Sambuc #define signature_CENTAUR_ebx 0x746e6543
35*0a6a1f1dSLionel Sambuc #define signature_CENTAUR_edx 0x48727561
36*0a6a1f1dSLionel Sambuc #define signature_CENTAUR_ecx 0x736c7561
37*0a6a1f1dSLionel Sambuc /* CYRIX: "CyrixInstead" */
38*0a6a1f1dSLionel Sambuc #define signature_CYRIX_ebx 0x69727943
39*0a6a1f1dSLionel Sambuc #define signature_CYRIX_edx 0x736e4978
40*0a6a1f1dSLionel Sambuc #define signature_CYRIX_ecx 0x64616574
41*0a6a1f1dSLionel Sambuc /* INTEL: "GenuineIntel" */
42*0a6a1f1dSLionel Sambuc #define signature_INTEL_ebx 0x756e6547
43*0a6a1f1dSLionel Sambuc #define signature_INTEL_edx 0x49656e69
44*0a6a1f1dSLionel Sambuc #define signature_INTEL_ecx 0x6c65746e
45*0a6a1f1dSLionel Sambuc /* TM1: "TransmetaCPU" */
46*0a6a1f1dSLionel Sambuc #define signature_TM1_ebx 0x6e617254
47*0a6a1f1dSLionel Sambuc #define signature_TM1_edx 0x74656d73
48*0a6a1f1dSLionel Sambuc #define signature_TM1_ecx 0x55504361
49*0a6a1f1dSLionel Sambuc /* TM2: "GenuineTMx86" */
50*0a6a1f1dSLionel Sambuc #define signature_TM2_ebx 0x756e6547
51*0a6a1f1dSLionel Sambuc #define signature_TM2_edx 0x54656e69
52*0a6a1f1dSLionel Sambuc #define signature_TM2_ecx 0x3638784d
53*0a6a1f1dSLionel Sambuc /* NSC: "Geode by NSC" */
54*0a6a1f1dSLionel Sambuc #define signature_NSC_ebx 0x646f6547
55*0a6a1f1dSLionel Sambuc #define signature_NSC_edx 0x43534e20
56*0a6a1f1dSLionel Sambuc #define signature_NSC_ecx 0x79622065
57*0a6a1f1dSLionel Sambuc /* NEXGEN: "NexGenDriven" */
58*0a6a1f1dSLionel Sambuc #define signature_NEXGEN_ebx 0x4778654e
59*0a6a1f1dSLionel Sambuc #define signature_NEXGEN_edx 0x72446e65
60*0a6a1f1dSLionel Sambuc #define signature_NEXGEN_ecx 0x6e657669
61*0a6a1f1dSLionel Sambuc /* RISE: "RiseRiseRise" */
62*0a6a1f1dSLionel Sambuc #define signature_RISE_ebx 0x65736952
63*0a6a1f1dSLionel Sambuc #define signature_RISE_edx 0x65736952
64*0a6a1f1dSLionel Sambuc #define signature_RISE_ecx 0x65736952
65*0a6a1f1dSLionel Sambuc /* SIS: "SiS SiS SiS " */
66*0a6a1f1dSLionel Sambuc #define signature_SIS_ebx 0x20536953
67*0a6a1f1dSLionel Sambuc #define signature_SIS_edx 0x20536953
68*0a6a1f1dSLionel Sambuc #define signature_SIS_ecx 0x20536953
69*0a6a1f1dSLionel Sambuc /* UMC: "UMC UMC UMC " */
70*0a6a1f1dSLionel Sambuc #define signature_UMC_ebx 0x20434d55
71*0a6a1f1dSLionel Sambuc #define signature_UMC_edx 0x20434d55
72*0a6a1f1dSLionel Sambuc #define signature_UMC_ecx 0x20434d55
73*0a6a1f1dSLionel Sambuc /* VIA: "VIA VIA VIA " */
74*0a6a1f1dSLionel Sambuc #define signature_VIA_ebx 0x20414956
75*0a6a1f1dSLionel Sambuc #define signature_VIA_edx 0x20414956
76*0a6a1f1dSLionel Sambuc #define signature_VIA_ecx 0x20414956
77*0a6a1f1dSLionel Sambuc /* VORTEX: "Vortex86 SoC" */
78*0a6a1f1dSLionel Sambuc #define signature_VORTEX_ebx 0x74726f56
79*0a6a1f1dSLionel Sambuc #define signature_VORTEX_edx 0x36387865
80*0a6a1f1dSLionel Sambuc #define signature_VORTEX_ecx 0x436f5320
81*0a6a1f1dSLionel Sambuc
82f4a2713aSLionel Sambuc /* Features in %ecx for level 1 */
83f4a2713aSLionel Sambuc #define bit_SSE3 0x00000001
84f4a2713aSLionel Sambuc #define bit_PCLMULQDQ 0x00000002
85f4a2713aSLionel Sambuc #define bit_DTES64 0x00000004
86f4a2713aSLionel Sambuc #define bit_MONITOR 0x00000008
87f4a2713aSLionel Sambuc #define bit_DSCPL 0x00000010
88f4a2713aSLionel Sambuc #define bit_VMX 0x00000020
89f4a2713aSLionel Sambuc #define bit_SMX 0x00000040
90f4a2713aSLionel Sambuc #define bit_EIST 0x00000080
91f4a2713aSLionel Sambuc #define bit_TM2 0x00000100
92f4a2713aSLionel Sambuc #define bit_SSSE3 0x00000200
93f4a2713aSLionel Sambuc #define bit_CNXTID 0x00000400
94f4a2713aSLionel Sambuc #define bit_FMA 0x00001000
95f4a2713aSLionel Sambuc #define bit_CMPXCHG16B 0x00002000
96f4a2713aSLionel Sambuc #define bit_xTPR 0x00004000
97f4a2713aSLionel Sambuc #define bit_PDCM 0x00008000
98f4a2713aSLionel Sambuc #define bit_PCID 0x00020000
99f4a2713aSLionel Sambuc #define bit_DCA 0x00040000
100f4a2713aSLionel Sambuc #define bit_SSE41 0x00080000
101f4a2713aSLionel Sambuc #define bit_SSE42 0x00100000
102f4a2713aSLionel Sambuc #define bit_x2APIC 0x00200000
103f4a2713aSLionel Sambuc #define bit_MOVBE 0x00400000
104f4a2713aSLionel Sambuc #define bit_POPCNT 0x00800000
105f4a2713aSLionel Sambuc #define bit_TSCDeadline 0x01000000
106f4a2713aSLionel Sambuc #define bit_AESNI 0x02000000
107f4a2713aSLionel Sambuc #define bit_XSAVE 0x04000000
108f4a2713aSLionel Sambuc #define bit_OSXSAVE 0x08000000
109f4a2713aSLionel Sambuc #define bit_AVX 0x10000000
110*0a6a1f1dSLionel Sambuc #define bit_RDRND 0x40000000
111f4a2713aSLionel Sambuc
112f4a2713aSLionel Sambuc /* Features in %edx for level 1 */
113f4a2713aSLionel Sambuc #define bit_FPU 0x00000001
114f4a2713aSLionel Sambuc #define bit_VME 0x00000002
115f4a2713aSLionel Sambuc #define bit_DE 0x00000004
116f4a2713aSLionel Sambuc #define bit_PSE 0x00000008
117f4a2713aSLionel Sambuc #define bit_TSC 0x00000010
118f4a2713aSLionel Sambuc #define bit_MSR 0x00000020
119f4a2713aSLionel Sambuc #define bit_PAE 0x00000040
120f4a2713aSLionel Sambuc #define bit_MCE 0x00000080
121f4a2713aSLionel Sambuc #define bit_CX8 0x00000100
122f4a2713aSLionel Sambuc #define bit_APIC 0x00000200
123f4a2713aSLionel Sambuc #define bit_SEP 0x00000800
124f4a2713aSLionel Sambuc #define bit_MTRR 0x00001000
125f4a2713aSLionel Sambuc #define bit_PGE 0x00002000
126f4a2713aSLionel Sambuc #define bit_MCA 0x00004000
127f4a2713aSLionel Sambuc #define bit_CMOV 0x00008000
128f4a2713aSLionel Sambuc #define bit_PAT 0x00010000
129f4a2713aSLionel Sambuc #define bit_PSE36 0x00020000
130f4a2713aSLionel Sambuc #define bit_PSN 0x00040000
131f4a2713aSLionel Sambuc #define bit_CLFSH 0x00080000
132f4a2713aSLionel Sambuc #define bit_DS 0x00200000
133f4a2713aSLionel Sambuc #define bit_ACPI 0x00400000
134f4a2713aSLionel Sambuc #define bit_MMX 0x00800000
135f4a2713aSLionel Sambuc #define bit_FXSR 0x01000000
136*0a6a1f1dSLionel Sambuc #define bit_FXSAVE bit_FXSR /* for gcc compat */
137f4a2713aSLionel Sambuc #define bit_SSE 0x02000000
138f4a2713aSLionel Sambuc #define bit_SSE2 0x04000000
139f4a2713aSLionel Sambuc #define bit_SS 0x08000000
140f4a2713aSLionel Sambuc #define bit_HTT 0x10000000
141f4a2713aSLionel Sambuc #define bit_TM 0x20000000
142f4a2713aSLionel Sambuc #define bit_PBE 0x80000000
143f4a2713aSLionel Sambuc
144f4a2713aSLionel Sambuc /* Features in %ebx for level 7 sub-leaf 0 */
145f4a2713aSLionel Sambuc #define bit_FSGSBASE 0x00000001
146f4a2713aSLionel Sambuc #define bit_SMEP 0x00000080
147f4a2713aSLionel Sambuc #define bit_ENH_MOVSB 0x00000200
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc #if __i386__
150f4a2713aSLionel Sambuc #define __cpuid(__level, __eax, __ebx, __ecx, __edx) \
151*0a6a1f1dSLionel Sambuc __asm("cpuid" : "=a"(__eax), "=b" (__ebx), "=c"(__ecx), "=d"(__edx) \
152f4a2713aSLionel Sambuc : "0"(__level))
153f4a2713aSLionel Sambuc
154f4a2713aSLionel Sambuc #define __cpuid_count(__level, __count, __eax, __ebx, __ecx, __edx) \
155*0a6a1f1dSLionel Sambuc __asm("cpuid" : "=a"(__eax), "=b" (__ebx), "=c"(__ecx), "=d"(__edx) \
156f4a2713aSLionel Sambuc : "0"(__level), "2"(__count))
157f4a2713aSLionel Sambuc #else
158*0a6a1f1dSLionel Sambuc /* x86-64 uses %rbx as the base register, so preserve it. */
159f4a2713aSLionel Sambuc #define __cpuid(__level, __eax, __ebx, __ecx, __edx) \
160*0a6a1f1dSLionel Sambuc __asm(" xchgq %%rbx,%q1\n" \
161*0a6a1f1dSLionel Sambuc " cpuid\n" \
162*0a6a1f1dSLionel Sambuc " xchgq %%rbx,%q1" \
163*0a6a1f1dSLionel Sambuc : "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
164f4a2713aSLionel Sambuc : "0"(__level))
165f4a2713aSLionel Sambuc
166f4a2713aSLionel Sambuc #define __cpuid_count(__level, __count, __eax, __ebx, __ecx, __edx) \
167*0a6a1f1dSLionel Sambuc __asm(" xchgq %%rbx,%q1\n" \
168*0a6a1f1dSLionel Sambuc " cpuid\n" \
169*0a6a1f1dSLionel Sambuc " xchgq %%rbx,%q1" \
170*0a6a1f1dSLionel Sambuc : "=a"(__eax), "=r" (__ebx), "=c"(__ecx), "=d"(__edx) \
171f4a2713aSLionel Sambuc : "0"(__level), "2"(__count))
172f4a2713aSLionel Sambuc #endif
173f4a2713aSLionel Sambuc
__get_cpuid(unsigned int __level,unsigned int * __eax,unsigned int * __ebx,unsigned int * __ecx,unsigned int * __edx)174f4a2713aSLionel Sambuc static __inline int __get_cpuid (unsigned int __level, unsigned int *__eax,
175f4a2713aSLionel Sambuc unsigned int *__ebx, unsigned int *__ecx,
176f4a2713aSLionel Sambuc unsigned int *__edx) {
177f4a2713aSLionel Sambuc __cpuid(__level, *__eax, *__ebx, *__ecx, *__edx);
178f4a2713aSLionel Sambuc return 1;
179f4a2713aSLionel Sambuc }
180f4a2713aSLionel Sambuc
__get_cpuid_max(unsigned int __level,unsigned int * __sig)181f4a2713aSLionel Sambuc static __inline int __get_cpuid_max (unsigned int __level, unsigned int *__sig)
182f4a2713aSLionel Sambuc {
183f4a2713aSLionel Sambuc unsigned int __eax, __ebx, __ecx, __edx;
184f4a2713aSLionel Sambuc #if __i386__
185f4a2713aSLionel Sambuc int __cpuid_supported;
186f4a2713aSLionel Sambuc
187f4a2713aSLionel Sambuc __asm(" pushfl\n"
188f4a2713aSLionel Sambuc " popl %%eax\n"
189f4a2713aSLionel Sambuc " movl %%eax,%%ecx\n"
190f4a2713aSLionel Sambuc " xorl $0x00200000,%%eax\n"
191f4a2713aSLionel Sambuc " pushl %%eax\n"
192f4a2713aSLionel Sambuc " popfl\n"
193f4a2713aSLionel Sambuc " pushfl\n"
194f4a2713aSLionel Sambuc " popl %%eax\n"
195f4a2713aSLionel Sambuc " movl $0,%0\n"
196f4a2713aSLionel Sambuc " cmpl %%eax,%%ecx\n"
197f4a2713aSLionel Sambuc " je 1f\n"
198f4a2713aSLionel Sambuc " movl $1,%0\n"
199f4a2713aSLionel Sambuc "1:"
200f4a2713aSLionel Sambuc : "=r" (__cpuid_supported) : : "eax", "ecx");
201f4a2713aSLionel Sambuc if (!__cpuid_supported)
202f4a2713aSLionel Sambuc return 0;
203f4a2713aSLionel Sambuc #endif
204f4a2713aSLionel Sambuc
205f4a2713aSLionel Sambuc __cpuid(__level, __eax, __ebx, __ecx, __edx);
206f4a2713aSLionel Sambuc if (__sig)
207f4a2713aSLionel Sambuc *__sig = __ebx;
208f4a2713aSLionel Sambuc return __eax;
209f4a2713aSLionel Sambuc }
210