]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpuid.h
980945d50a80ba4943166263530f07eefed497d4
[apple/xnu.git] / osfmk / i386 / cpuid.h
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31
32 /*
33 * x86 CPU identification
34 *
35 */
36
37 #ifndef _MACHINE_CPUID_H_
38 #define _MACHINE_CPUID_H_
39
40 #include <sys/appleapiopts.h>
41
42 #ifdef __APPLE_API_PRIVATE
43
44 #define CPUID_VID_INTEL "GenuineIntel"
45 #define CPUID_VID_AMD "AuthenticAMD"
46
47 #define CPUID_VMM_ID_VMWARE "VMwareVMware"
48 #define CPUID_VMM_ID_PARALLELS "Parallels\0\0\0"
49
50 #define CPUID_STRING_UNKNOWN "Unknown CPU Typ"
51
52 #define _Bit(n) (1ULL << n)
53 #define _HBit(n) (1ULL << ((n)+32))
54
55 /*
56 * The CPUID_FEATURE_XXX values define 64-bit values
57 * returned in %ecx:%edx to a CPUID request with %eax of 1:
58 */
59 #define CPUID_FEATURE_FPU _Bit(0) /* Floating point unit on-chip */
60 #define CPUID_FEATURE_VME _Bit(1) /* Virtual Mode Extension */
61 #define CPUID_FEATURE_DE _Bit(2) /* Debugging Extension */
62 #define CPUID_FEATURE_PSE _Bit(3) /* Page Size Extension */
63 #define CPUID_FEATURE_TSC _Bit(4) /* Time Stamp Counter */
64 #define CPUID_FEATURE_MSR _Bit(5) /* Model Specific Registers */
65 #define CPUID_FEATURE_PAE _Bit(6) /* Physical Address Extension */
66 #define CPUID_FEATURE_MCE _Bit(7) /* Machine Check Exception */
67 #define CPUID_FEATURE_CX8 _Bit(8) /* CMPXCHG8B */
68 #define CPUID_FEATURE_APIC _Bit(9) /* On-chip APIC */
69 #define CPUID_FEATURE_SEP _Bit(11) /* Fast System Call */
70 #define CPUID_FEATURE_MTRR _Bit(12) /* Memory Type Range Register */
71 #define CPUID_FEATURE_PGE _Bit(13) /* Page Global Enable */
72 #define CPUID_FEATURE_MCA _Bit(14) /* Machine Check Architecture */
73 #define CPUID_FEATURE_CMOV _Bit(15) /* Conditional Move Instruction */
74 #define CPUID_FEATURE_PAT _Bit(16) /* Page Attribute Table */
75 #define CPUID_FEATURE_PSE36 _Bit(17) /* 36-bit Page Size Extension */
76 #define CPUID_FEATURE_PSN _Bit(18) /* Processor Serial Number */
77 #define CPUID_FEATURE_CLFSH _Bit(19) /* CLFLUSH Instruction supported */
78 #define CPUID_FEATURE_DS _Bit(21) /* Debug Store */
79 #define CPUID_FEATURE_ACPI _Bit(22) /* Thermal monitor and Clock Ctrl */
80 #define CPUID_FEATURE_MMX _Bit(23) /* MMX supported */
81 #define CPUID_FEATURE_FXSR _Bit(24) /* Fast floating pt save/restore */
82 #define CPUID_FEATURE_SSE _Bit(25) /* Streaming SIMD extensions */
83 #define CPUID_FEATURE_SSE2 _Bit(26) /* Streaming SIMD extensions 2 */
84 #define CPUID_FEATURE_SS _Bit(27) /* Self-Snoop */
85 #define CPUID_FEATURE_HTT _Bit(28) /* Hyper-Threading Technology */
86 #define CPUID_FEATURE_TM _Bit(29) /* Thermal Monitor (TM1) */
87 #define CPUID_FEATURE_PBE _Bit(31) /* Pend Break Enable */
88
89 #define CPUID_FEATURE_SSE3 _HBit(0) /* Streaming SIMD extensions 3 */
90 #define CPUID_FEATURE_PCLMULQDQ _HBit(1) /* PCLMULQDQ instruction */
91 #define CPUID_FEATURE_DTES64 _HBit(2) /* 64-bit DS layout */
92 #define CPUID_FEATURE_MONITOR _HBit(3) /* Monitor/mwait */
93 #define CPUID_FEATURE_DSCPL _HBit(4) /* Debug Store CPL */
94 #define CPUID_FEATURE_VMX _HBit(5) /* VMX */
95 #define CPUID_FEATURE_SMX _HBit(6) /* SMX */
96 #define CPUID_FEATURE_EST _HBit(7) /* Enhanced SpeedsTep (GV3) */
97 #define CPUID_FEATURE_TM2 _HBit(8) /* Thermal Monitor 2 */
98 #define CPUID_FEATURE_SSSE3 _HBit(9) /* Supplemental SSE3 instructions */
99 #define CPUID_FEATURE_CID _HBit(10) /* L1 Context ID */
100 #define CPUID_FEATURE_SEGLIM64 _HBit(11) /* 64-bit segment limit checking */
101 #define CPUID_FEATURE_FMA _HBit(12) /* Fused-Multiply-Add support */
102 #define CPUID_FEATURE_CX16 _HBit(13) /* CmpXchg16b instruction */
103 #define CPUID_FEATURE_xTPR _HBit(14) /* Send Task PRiority msgs */
104 #define CPUID_FEATURE_PDCM _HBit(15) /* Perf/Debug Capability MSR */
105
106 #define CPUID_FEATURE_PCID _HBit(17) /* ASID-PCID support */
107 #define CPUID_FEATURE_DCA _HBit(18) /* Direct Cache Access */
108 #define CPUID_FEATURE_SSE4_1 _HBit(19) /* Streaming SIMD extensions 4.1 */
109 #define CPUID_FEATURE_SSE4_2 _HBit(20) /* Streaming SIMD extensions 4.2 */
110 #define CPUID_FEATURE_x2APIC _HBit(21) /* Extended APIC Mode */
111 #define CPUID_FEATURE_MOVBE _HBit(22) /* MOVBE instruction */
112 #define CPUID_FEATURE_POPCNT _HBit(23) /* POPCNT instruction */
113 #define CPUID_FEATURE_TSCTMR _HBit(24) /* TSC deadline timer */
114 #define CPUID_FEATURE_AES _HBit(25) /* AES instructions */
115 #define CPUID_FEATURE_XSAVE _HBit(26) /* XSAVE instructions */
116 #define CPUID_FEATURE_OSXSAVE _HBit(27) /* XGETBV/XSETBV instructions */
117 #define CPUID_FEATURE_AVX1_0 _HBit(28) /* AVX 1.0 instructions */
118 #define CPUID_FEATURE_F16C _HBit(29) /* Float16 convert instructions */
119 #define CPUID_FEATURE_RDRAND _HBit(30) /* RDRAND instruction */
120 #define CPUID_FEATURE_VMM _HBit(31) /* VMM (Hypervisor) present */
121
122 /*
123 * Leaf 7, subleaf 0 additional features.
124 * Bits returned in %ebx:%ecx to a CPUID request with {%eax,%ecx} of (0x7,0x0}:
125 */
126 #define CPUID_LEAF7_FEATURE_RDWRFSGS _Bit(0) /* FS/GS base read/write */
127 #define CPUID_LEAF7_FEATURE_TSCOFF _Bit(1) /* TSC thread offset */
128 #define CPUID_LEAF7_FEATURE_BMI1 _Bit(3) /* Bit Manipulation Instrs, set 1 */
129 #define CPUID_LEAF7_FEATURE_HLE _Bit(4) /* Hardware Lock Elision*/
130 #define CPUID_LEAF7_FEATURE_AVX2 _Bit(5) /* AVX2 Instructions */
131 #define CPUID_LEAF7_FEATURE_SMEP _Bit(7) /* Supervisor Mode Execute Protect */
132 #define CPUID_LEAF7_FEATURE_BMI2 _Bit(8) /* Bit Manipulation Instrs, set 2 */
133 #define CPUID_LEAF7_FEATURE_ERMS _Bit(9) /* Enhanced Rep Movsb/Stosb */
134 #define CPUID_LEAF7_FEATURE_INVPCID _Bit(10) /* INVPCID intruction, TDB */
135 #define CPUID_LEAF7_FEATURE_RTM _Bit(11) /* RTM */
136 #define CPUID_LEAF7_FEATURE_RDSEED _Bit(18) /* RDSEED Instruction */
137 #define CPUID_LEAF7_FEATURE_ADX _Bit(19) /* ADX Instructions */
138 #define CPUID_LEAF7_FEATURE_SMAP _Bit(20) /* Supervisor Mode Access Protect */
139
140 /*
141 * The CPUID_EXTFEATURE_XXX values define 64-bit values
142 * returned in %ecx:%edx to a CPUID request with %eax of 0x80000001:
143 */
144 #define CPUID_EXTFEATURE_SYSCALL _Bit(11) /* SYSCALL/sysret */
145 #define CPUID_EXTFEATURE_XD _Bit(20) /* eXecute Disable */
146
147 #define CPUID_EXTFEATURE_1GBPAGE _Bit(26) /* 1GB pages */
148 #define CPUID_EXTFEATURE_RDTSCP _Bit(27) /* RDTSCP */
149 #define CPUID_EXTFEATURE_EM64T _Bit(29) /* Extended Mem 64 Technology */
150
151 #define CPUID_EXTFEATURE_LAHF _HBit(0) /* LAFH/SAHF instructions */
152 #define CPUID_EXTFEATURE_LZCNT _HBit(5) /* LZCNT instruction */
153 #define CPUID_EXTFEATURE_PREFETCHW _HBit(8) /* PREFETCHW instruction */
154
155 /*
156 * The CPUID_EXTFEATURE_XXX values define 64-bit values
157 * returned in %ecx:%edx to a CPUID request with %eax of 0x80000007:
158 */
159 #define CPUID_EXTFEATURE_TSCI _Bit(8) /* TSC Invariant */
160
161 /*
162 * CPUID_X86_64_H_FEATURE_SUBSET and CPUID_X86_64_H_LEAF7_FEATURE_SUBSET
163 * indicate the bitmask of features that must be present before the system
164 * is eligible to run the "x86_64h" "Haswell feature subset" slice.
165 */
166 #define CPUID_X86_64_H_FEATURE_SUBSET ( CPUID_FEATURE_FMA | \
167 CPUID_FEATURE_SSE4_2 | \
168 CPUID_FEATURE_MOVBE | \
169 CPUID_FEATURE_POPCNT | \
170 CPUID_FEATURE_AVX1_0 \
171 )
172
173 #define CPUID_X86_64_H_EXTFEATURE_SUBSET ( CPUID_EXTFEATURE_LZCNT \
174 )
175
176 #define CPUID_X86_64_H_LEAF7_FEATURE_SUBSET ( CPUID_LEAF7_FEATURE_BMI1 | \
177 CPUID_LEAF7_FEATURE_AVX2 | \
178 CPUID_LEAF7_FEATURE_BMI2 \
179 )
180
181 #define CPUID_CACHE_SIZE 16 /* Number of descriptor values */
182
183 #define CPUID_MWAIT_EXTENSION _Bit(0) /* enumeration of WMAIT extensions */
184 #define CPUID_MWAIT_BREAK _Bit(1) /* interrupts are break events */
185
186 #define CPUID_MODEL_YONAH 0x0E
187 #define CPUID_MODEL_MEROM 0x0F
188 #define CPUID_MODEL_PENRYN 0x17
189 #define CPUID_MODEL_NEHALEM 0x1A
190 #define CPUID_MODEL_FIELDS 0x1E /* Lynnfield, Clarksfield */
191 #define CPUID_MODEL_DALES 0x1F /* Havendale, Auburndale */
192 #define CPUID_MODEL_NEHALEM_EX 0x2E
193 #define CPUID_MODEL_DALES_32NM 0x25 /* Clarkdale, Arrandale */
194 #define CPUID_MODEL_WESTMERE 0x2C /* Gulftown, Westmere-EP/-WS */
195 #define CPUID_MODEL_WESTMERE_EX 0x2F
196 #define CPUID_MODEL_SANDYBRIDGE 0x2A
197 #define CPUID_MODEL_JAKETOWN 0x2D
198 #define CPUID_MODEL_IVYBRIDGE 0x3A
199 #define CPUID_MODEL_IVYBRIDGE_EP 0x3E
200 #define CPUID_MODEL_CRYSTALWELL 0x46
201 #define CPUID_MODEL_HASWELL 0x3C
202 #define CPUID_MODEL_HASWELL_EP 0x3F
203 #define CPUID_MODEL_HASWELL_ULT 0x45
204 #define CPUID_MODEL_BROADWELL 0x3D
205 #define CPUID_MODEL_BROADWELL_ULX 0x3D
206 #define CPUID_MODEL_BROADWELL_ULT 0x3D
207 #define CPUID_MODEL_BRYSTALWELL 0x47
208
209 #define CPUID_VMM_FAMILY_UNKNOWN 0x0
210 #define CPUID_VMM_FAMILY_VMWARE 0x1
211 #define CPUID_VMM_FAMILY_PARALLELS 0x2
212
213 #ifndef ASSEMBLER
214 #include <stdint.h>
215 #include <mach/mach_types.h>
216 #include <kern/kern_types.h>
217 #include <mach/machine.h>
218
219
220 typedef enum { eax, ebx, ecx, edx } cpuid_register_t;
221 static inline void
222 cpuid(uint32_t *data)
223 {
224 __asm__ volatile ("cpuid"
225 : "=a" (data[eax]),
226 "=b" (data[ebx]),
227 "=c" (data[ecx]),
228 "=d" (data[edx])
229 : "a" (data[eax]),
230 "b" (data[ebx]),
231 "c" (data[ecx]),
232 "d" (data[edx]));
233 }
234
235 static inline void
236 do_cpuid(uint32_t selector, uint32_t *data)
237 {
238 __asm__ volatile ("cpuid"
239 : "=a" (data[0]),
240 "=b" (data[1]),
241 "=c" (data[2]),
242 "=d" (data[3])
243 : "a"(selector),
244 "b" (0),
245 "c" (0),
246 "d" (0));
247 }
248
249 /*
250 * Cache ID descriptor structure, used to parse CPUID leaf 2.
251 * Note: not used in kernel.
252 */
253 typedef enum { Lnone, L1I, L1D, L2U, L3U, LCACHE_MAX } cache_type_t ;
254 typedef struct {
255 unsigned char value; /* Descriptor value */
256 cache_type_t type; /* Cache type */
257 unsigned int size; /* Cache size */
258 unsigned int linesize; /* Cache line size */
259 #ifdef KERNEL
260 const char *description; /* Cache description */
261 #endif /* KERNEL */
262 } cpuid_cache_desc_t;
263
264 #ifdef KERNEL
265 #define CACHE_DESC(value,type,size,linesize,text) \
266 { value, type, size, linesize, text }
267 #else
268 #define CACHE_DESC(value,type,size,linesize,text) \
269 { value, type, size, linesize }
270 #endif /* KERNEL */
271
272 /* Monitor/mwait Leaf: */
273 typedef struct {
274 uint32_t linesize_min;
275 uint32_t linesize_max;
276 uint32_t extensions;
277 uint32_t sub_Cstates;
278 } cpuid_mwait_leaf_t;
279
280 /* Thermal and Power Management Leaf: */
281 typedef struct {
282 boolean_t sensor;
283 boolean_t dynamic_acceleration;
284 boolean_t invariant_APIC_timer;
285 boolean_t core_power_limits;
286 boolean_t fine_grain_clock_mod;
287 boolean_t package_thermal_intr;
288 uint32_t thresholds;
289 boolean_t ACNT_MCNT;
290 boolean_t hardware_feedback;
291 boolean_t energy_policy;
292 } cpuid_thermal_leaf_t;
293
294
295 /* XSAVE Feature Leaf: */
296 typedef struct {
297 uint32_t extended_state[4]; /* eax .. edx */
298 } cpuid_xsave_leaf_t;
299
300
301 /* Architectural Performance Monitoring Leaf: */
302 typedef struct {
303 uint8_t version;
304 uint8_t number;
305 uint8_t width;
306 uint8_t events_number;
307 uint32_t events;
308 uint8_t fixed_number;
309 uint8_t fixed_width;
310 } cpuid_arch_perf_leaf_t;
311
312 /* Physical CPU info - this is exported out of the kernel (kexts), so be wary of changes */
313 typedef struct {
314 char cpuid_vendor[16];
315 char cpuid_brand_string[48];
316 const char *cpuid_model_string;
317
318 cpu_type_t cpuid_type; /* this is *not* a cpu_type_t in our <mach/machine.h> */
319 uint8_t cpuid_family;
320 uint8_t cpuid_model;
321 uint8_t cpuid_extmodel;
322 uint8_t cpuid_extfamily;
323 uint8_t cpuid_stepping;
324 uint64_t cpuid_features;
325 uint64_t cpuid_extfeatures;
326 uint32_t cpuid_signature;
327 uint8_t cpuid_brand;
328 uint8_t cpuid_processor_flag;
329
330 uint32_t cache_size[LCACHE_MAX];
331 uint32_t cache_linesize;
332
333 uint8_t cache_info[64]; /* list of cache descriptors */
334
335 uint32_t cpuid_cores_per_package;
336 uint32_t cpuid_logical_per_package;
337 uint32_t cache_sharing[LCACHE_MAX];
338 uint32_t cache_partitions[LCACHE_MAX];
339
340 cpu_type_t cpuid_cpu_type; /* <mach/machine.h> */
341 cpu_subtype_t cpuid_cpu_subtype; /* <mach/machine.h> */
342
343 /* Per-vendor info */
344 cpuid_mwait_leaf_t cpuid_mwait_leaf;
345 #define cpuid_mwait_linesize_max cpuid_mwait_leaf.linesize_max
346 #define cpuid_mwait_linesize_min cpuid_mwait_leaf.linesize_min
347 #define cpuid_mwait_extensions cpuid_mwait_leaf.extensions
348 #define cpuid_mwait_sub_Cstates cpuid_mwait_leaf.sub_Cstates
349 cpuid_thermal_leaf_t cpuid_thermal_leaf;
350 cpuid_arch_perf_leaf_t cpuid_arch_perf_leaf;
351 cpuid_xsave_leaf_t cpuid_xsave_leaf;
352
353 /* Cache details: */
354 uint32_t cpuid_cache_linesize;
355 uint32_t cpuid_cache_L2_associativity;
356 uint32_t cpuid_cache_size;
357
358 /* Virtual and physical address aize: */
359 uint32_t cpuid_address_bits_physical;
360 uint32_t cpuid_address_bits_virtual;
361
362 uint32_t cpuid_microcode_version;
363
364 /* Numbers of tlbs per processor [i|d, small|large, level0|level1] */
365 uint32_t cpuid_tlb[2][2][2];
366 #define TLB_INST 0
367 #define TLB_DATA 1
368 #define TLB_SMALL 0
369 #define TLB_LARGE 1
370 uint32_t cpuid_stlb;
371
372 uint32_t core_count;
373 uint32_t thread_count;
374
375 /* Max leaf ids available from CPUID */
376 uint32_t cpuid_max_basic;
377 uint32_t cpuid_max_ext;
378
379 /* Family-specific info links */
380 uint32_t cpuid_cpufamily;
381 cpuid_mwait_leaf_t *cpuid_mwait_leafp;
382 cpuid_thermal_leaf_t *cpuid_thermal_leafp;
383 cpuid_arch_perf_leaf_t *cpuid_arch_perf_leafp;
384 cpuid_xsave_leaf_t *cpuid_xsave_leafp;
385 uint64_t cpuid_leaf7_features;
386 } i386_cpu_info_t;
387
388 #ifdef MACH_KERNEL_PRIVATE
389 typedef struct {
390 char cpuid_vmm_vendor[16];
391 uint32_t cpuid_vmm_family;
392 uint32_t cpuid_vmm_bus_frequency;
393 uint32_t cpuid_vmm_tsc_frequency;
394 } i386_vmm_info_t;
395 #endif
396
397 #ifdef __cplusplus
398 extern "C" {
399 #endif
400
401 /*
402 * External declarations
403 */
404 extern cpu_type_t cpuid_cputype(void);
405 extern cpu_subtype_t cpuid_cpusubtype(void);
406 extern void cpuid_cpu_display(const char *);
407 extern void cpuid_feature_display(const char *);
408 extern void cpuid_extfeature_display(const char *);
409 extern char * cpuid_get_feature_names(uint64_t, char *, unsigned);
410 extern char * cpuid_get_extfeature_names(uint64_t, char *, unsigned);
411 extern char * cpuid_get_leaf7_feature_names(uint64_t, char *, unsigned);
412
413 extern uint64_t cpuid_features(void);
414 extern uint64_t cpuid_extfeatures(void);
415 extern uint64_t cpuid_leaf7_features(void);
416 extern uint32_t cpuid_family(void);
417 extern uint32_t cpuid_cpufamily(void);
418
419 extern i386_cpu_info_t *cpuid_info(void);
420 extern void cpuid_set_info(void);
421
422 #ifdef MACH_KERNEL_PRIVATE
423 extern boolean_t cpuid_vmm_present(void);
424 extern i386_vmm_info_t *cpuid_vmm_info(void);
425 extern uint32_t cpuid_vmm_family(void);
426 #endif
427
428 #ifdef __cplusplus
429 }
430 #endif
431
432 #endif /* ASSEMBLER */
433
434 #endif /* __APPLE_API_PRIVATE */
435 #endif /* _MACHINE_CPUID_H_ */