]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/cpuid.c
xnu-7195.60.75.tar.gz
[apple/xnu.git] / osfmk / i386 / cpuid.c
CommitLineData
1c79356b 1/*
0a7de745 2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
2d21ac55 31#include <vm/vm_page.h>
91447636
A
32#include <pexpert/pexpert.h>
33
0a7de745 34#include <i386/cpu_threads.h>
b0d623f7 35#include <i386/cpuid.h>
f427ee49 36#include <i386/machine_routines.h>
1c79356b 37
0a7de745
A
38int force_tecs_at_idle;
39int tecs_mode_supported;
40
41static boolean_t cpuid_dbg
7ddcb079 42#if DEBUG
0a7de745 43 = TRUE;
7ddcb079 44#else
0a7de745 45 = FALSE;
7ddcb079 46#endif
0a7de745
A
47#define DBG(x...) \
48 do { \
49 if (cpuid_dbg) \
50 kprintf(x); \
51 } while (0) \
7ddcb079 52
0a7de745
A
53#define min(a, b) ((a) < (b) ? (a) : (b))
54#define quad(hi, lo) (((uint64_t)(hi)) << 32 | (lo))
b0d623f7
A
55
56/*
57 * Leaf 2 cache descriptor encodings.
58 */
59typedef enum {
0a7de745
A
60 _NULL_, /* NULL (empty) descriptor */
61 CACHE, /* Cache */
62 TLB, /* TLB */
63 STLB, /* Shared second-level unified TLB */
64 PREFETCH /* Prefetch size */
b0d623f7
A
65} cpuid_leaf2_desc_type_t;
66
67typedef enum {
0a7de745
A
68 NA, /* Not Applicable */
69 FULLY, /* Fully-associative */
70 TRACE, /* Trace Cache (P4 only) */
71 INST, /* Instruction TLB */
72 DATA, /* Data TLB */
73 DATA0, /* Data TLB, 1st level */
74 DATA1, /* Data TLB, 2nd level */
75 L1, /* L1 (unified) cache */
76 L1_INST, /* L1 Instruction cache */
77 L1_DATA, /* L1 Data cache */
78 L2, /* L2 (unified) cache */
79 L3, /* L3 (unified) cache */
80 L2_2LINESECTOR, /* L2 (unified) cache with 2 lines per sector */
81 L3_2LINESECTOR, /* L3(unified) cache with 2 lines per sector */
82 SMALL, /* Small page TLB */
83 LARGE, /* Large page TLB */
84 BOTH /* Small and Large page TLB */
b0d623f7
A
85} cpuid_leaf2_qualifier_t;
86
87typedef struct cpuid_cache_descriptor {
0a7de745
A
88 uint8_t value; /* descriptor code */
89 uint8_t type; /* cpuid_leaf2_desc_type_t */
90 uint8_t level; /* level of cache/TLB hierachy */
91 uint8_t ways; /* wayness of cache */
92 uint16_t size; /* cachesize or TLB pagesize */
93 uint16_t entries; /* number of TLB entries or linesize */
b0d623f7
A
94} cpuid_cache_descriptor_t;
95
96/*
0a7de745 97 * These multipliers are used to encode 1*K .. 64*M in a 16 bit size field
b0d623f7 98 */
0a7de745
A
99#define K (1)
100#define M (1024)
b0d623f7
A
101
102/*
103 * Intel cache descriptor table:
104 */
105static cpuid_cache_descriptor_t intel_cpuid_leaf2_descriptor_table[] = {
106// -------------------------------------------------------
107// value type level ways size entries
108// -------------------------------------------------------
0a7de745
A
109 { 0x00, _NULL_, NA, NA, NA, NA },
110 { 0x01, TLB, INST, 4, SMALL, 32 },
111 { 0x02, TLB, INST, FULLY, LARGE, 2 },
112 { 0x03, TLB, DATA, 4, SMALL, 64 },
113 { 0x04, TLB, DATA, 4, LARGE, 8 },
114 { 0x05, TLB, DATA1, 4, LARGE, 32 },
115 { 0x06, CACHE, L1_INST, 4, 8 * K, 32 },
116 { 0x08, CACHE, L1_INST, 4, 16 * K, 32 },
117 { 0x09, CACHE, L1_INST, 4, 32 * K, 64 },
118 { 0x0A, CACHE, L1_DATA, 2, 8 * K, 32 },
119 { 0x0B, TLB, INST, 4, LARGE, 4 },
120 { 0x0C, CACHE, L1_DATA, 4, 16 * K, 32 },
121 { 0x0D, CACHE, L1_DATA, 4, 16 * K, 64 },
122 { 0x0E, CACHE, L1_DATA, 6, 24 * K, 64 },
123 { 0x21, CACHE, L2, 8, 256 * K, 64 },
124 { 0x22, CACHE, L3_2LINESECTOR, 4, 512 * K, 64 },
125 { 0x23, CACHE, L3_2LINESECTOR, 8, 1 * M, 64 },
126 { 0x25, CACHE, L3_2LINESECTOR, 8, 2 * M, 64 },
127 { 0x29, CACHE, L3_2LINESECTOR, 8, 4 * M, 64 },
128 { 0x2C, CACHE, L1_DATA, 8, 32 * K, 64 },
129 { 0x30, CACHE, L1_INST, 8, 32 * K, 64 },
130 { 0x40, CACHE, L2, NA, 0, NA },
131 { 0x41, CACHE, L2, 4, 128 * K, 32 },
132 { 0x42, CACHE, L2, 4, 256 * K, 32 },
133 { 0x43, CACHE, L2, 4, 512 * K, 32 },
134 { 0x44, CACHE, L2, 4, 1 * M, 32 },
135 { 0x45, CACHE, L2, 4, 2 * M, 32 },
136 { 0x46, CACHE, L3, 4, 4 * M, 64 },
137 { 0x47, CACHE, L3, 8, 8 * M, 64 },
138 { 0x48, CACHE, L2, 12, 3 * M, 64 },
139 { 0x49, CACHE, L2, 16, 4 * M, 64 },
140 { 0x4A, CACHE, L3, 12, 6 * M, 64 },
141 { 0x4B, CACHE, L3, 16, 8 * M, 64 },
142 { 0x4C, CACHE, L3, 12, 12 * M, 64 },
143 { 0x4D, CACHE, L3, 16, 16 * M, 64 },
144 { 0x4E, CACHE, L2, 24, 6 * M, 64 },
145 { 0x4F, TLB, INST, NA, SMALL, 32 },
146 { 0x50, TLB, INST, NA, BOTH, 64 },
147 { 0x51, TLB, INST, NA, BOTH, 128 },
148 { 0x52, TLB, INST, NA, BOTH, 256 },
149 { 0x55, TLB, INST, FULLY, BOTH, 7 },
150 { 0x56, TLB, DATA0, 4, LARGE, 16 },
151 { 0x57, TLB, DATA0, 4, SMALL, 16 },
152 { 0x59, TLB, DATA0, FULLY, SMALL, 16 },
153 { 0x5A, TLB, DATA0, 4, LARGE, 32 },
154 { 0x5B, TLB, DATA, NA, BOTH, 64 },
155 { 0x5C, TLB, DATA, NA, BOTH, 128 },
156 { 0x5D, TLB, DATA, NA, BOTH, 256 },
157 { 0x60, CACHE, L1, 16 * K, 8, 64 },
158 { 0x61, CACHE, L1, 4, 8 * K, 64 },
159 { 0x62, CACHE, L1, 4, 16 * K, 64 },
160 { 0x63, CACHE, L1, 4, 32 * K, 64 },
161 { 0x70, CACHE, TRACE, 8, 12 * K, NA },
162 { 0x71, CACHE, TRACE, 8, 16 * K, NA },
163 { 0x72, CACHE, TRACE, 8, 32 * K, NA },
164 { 0x76, TLB, INST, NA, BOTH, 8 },
165 { 0x78, CACHE, L2, 4, 1 * M, 64 },
166 { 0x79, CACHE, L2_2LINESECTOR, 8, 128 * K, 64 },
167 { 0x7A, CACHE, L2_2LINESECTOR, 8, 256 * K, 64 },
168 { 0x7B, CACHE, L2_2LINESECTOR, 8, 512 * K, 64 },
169 { 0x7C, CACHE, L2_2LINESECTOR, 8, 1 * M, 64 },
170 { 0x7D, CACHE, L2, 8, 2 * M, 64 },
171 { 0x7F, CACHE, L2, 2, 512 * K, 64 },
172 { 0x80, CACHE, L2, 8, 512 * K, 64 },
173 { 0x82, CACHE, L2, 8, 256 * K, 32 },
174 { 0x83, CACHE, L2, 8, 512 * K, 32 },
175 { 0x84, CACHE, L2, 8, 1 * M, 32 },
176 { 0x85, CACHE, L2, 8, 2 * M, 32 },
177 { 0x86, CACHE, L2, 4, 512 * K, 64 },
178 { 0x87, CACHE, L2, 8, 1 * M, 64 },
179 { 0xB0, TLB, INST, 4, SMALL, 128 },
180 { 0xB1, TLB, INST, 4, LARGE, 8 },
181 { 0xB2, TLB, INST, 4, SMALL, 64 },
182 { 0xB3, TLB, DATA, 4, SMALL, 128 },
183 { 0xB4, TLB, DATA1, 4, SMALL, 256 },
184 { 0xB5, TLB, DATA1, 8, SMALL, 64 },
185 { 0xB6, TLB, DATA1, 8, SMALL, 128 },
186 { 0xBA, TLB, DATA1, 4, BOTH, 64 },
187 { 0xC1, STLB, DATA1, 8, SMALL, 1024},
188 { 0xCA, STLB, DATA1, 4, SMALL, 512 },
189 { 0xD0, CACHE, L3, 4, 512 * K, 64 },
190 { 0xD1, CACHE, L3, 4, 1 * M, 64 },
191 { 0xD2, CACHE, L3, 4, 2 * M, 64 },
192 { 0xD3, CACHE, L3, 4, 4 * M, 64 },
193 { 0xD4, CACHE, L3, 4, 8 * M, 64 },
194 { 0xD6, CACHE, L3, 8, 1 * M, 64 },
195 { 0xD7, CACHE, L3, 8, 2 * M, 64 },
196 { 0xD8, CACHE, L3, 8, 4 * M, 64 },
197 { 0xD9, CACHE, L3, 8, 8 * M, 64 },
198 { 0xDA, CACHE, L3, 8, 12 * M, 64 },
199 { 0xDC, CACHE, L3, 12, 1536 * K, 64 },
200 { 0xDD, CACHE, L3, 12, 3 * M, 64 },
201 { 0xDE, CACHE, L3, 12, 6 * M, 64 },
202 { 0xDF, CACHE, L3, 12, 12 * M, 64 },
203 { 0xE0, CACHE, L3, 12, 18 * M, 64 },
204 { 0xE2, CACHE, L3, 16, 2 * M, 64 },
205 { 0xE3, CACHE, L3, 16, 4 * M, 64 },
206 { 0xE4, CACHE, L3, 16, 8 * M, 64 },
207 { 0xE5, CACHE, L3, 16, 16 * M, 64 },
208 { 0xE6, CACHE, L3, 16, 24 * M, 64 },
209 { 0xF0, PREFETCH, NA, NA, 64, NA },
210 { 0xF1, PREFETCH, NA, NA, 128, NA },
211 { 0xFF, CACHE, NA, NA, 0, NA }
b0d623f7 212};
0a7de745
A
213#define INTEL_LEAF2_DESC_NUM (sizeof(intel_cpuid_leaf2_descriptor_table) / \
214 sizeof(cpuid_cache_descriptor_t))
215
f427ee49
A
216boolean_t cpuid_tsx_disabled = false; /* true if XNU disabled TSX */
217boolean_t cpuid_tsx_supported = false;
eb6b6ca3 218
0a7de745 219static void do_cwas(i386_cpu_info_t *cpuinfo, boolean_t on_slave);
94ff46dc 220static void cpuid_do_precpuid_was(void);
b0d623f7
A
221
222static inline cpuid_cache_descriptor_t *
223cpuid_leaf2_find(uint8_t value)
224{
0a7de745 225 unsigned int i;
b0d623f7 226
0a7de745
A
227 for (i = 0; i < INTEL_LEAF2_DESC_NUM; i++) {
228 if (intel_cpuid_leaf2_descriptor_table[i].value == value) {
b0d623f7 229 return &intel_cpuid_leaf2_descriptor_table[i];
0a7de745
A
230 }
231 }
b0d623f7
A
232 return NULL;
233}
1c79356b
A
234
235/*
55e303ae 236 * CPU identification routines.
1c79356b 237 */
1c79356b 238
0a7de745
A
239static i386_cpu_info_t cpuid_cpu_info;
240static i386_cpu_info_t *cpuid_cpu_infop = NULL;
d7e50217 241
0a7de745
A
242static void
243cpuid_fn(uint32_t selector, uint32_t *result)
b0d623f7
A
244{
245 do_cpuid(selector, result);
7ddcb079 246 DBG("cpuid_fn(0x%08x) eax:0x%08x ebx:0x%08x ecx:0x%08x edx:0x%08x\n",
0a7de745 247 selector, result[0], result[1], result[2], result[3]);
b0d623f7 248}
b0d623f7 249
7ddcb079
A
250static const char *cache_type_str[LCACHE_MAX] = {
251 "Lnone", "L1I", "L1D", "L2U", "L3U"
252};
253
0a7de745
A
254static void
255do_cwas(i386_cpu_info_t *cpuinfo, boolean_t on_slave)
256{
257 extern int force_thread_policy_tecs;
eb6b6ca3 258 cwa_classifier_e wa_reqd;
0a7de745
A
259
260 /*
261 * Workaround for reclaiming perf counter 3 due to TSX memory ordering erratum.
262 * This workaround does not support being forcibly set (since an MSR must be
263 * enumerated, lest we #GP when forced to access it.)
f427ee49
A
264 *
265 * Note that if disabling TSX is supported, disablement is prefered over forcing
266 * TSX transactions to abort.
0a7de745 267 */
f427ee49
A
268 if (cpuid_wa_required(CPU_INTEL_TSXDA) == CWA_ON) {
269 /* This must be executed on all logical processors */
270 wrmsr64(MSR_IA32_TSX_CTRL, MSR_IA32_TSXCTRL_TSX_CPU_CLEAR | MSR_IA32_TSXCTRL_RTM_DISABLE);
271 } else if (cpuid_wa_required(CPU_INTEL_TSXFA) == CWA_ON) {
94ff46dc 272 /* This must be executed on all logical processors */
0a7de745
A
273 wrmsr64(MSR_IA32_TSX_FORCE_ABORT,
274 rdmsr64(MSR_IA32_TSX_FORCE_ABORT) | MSR_IA32_TSXFA_RTM_FORCE_ABORT);
275 }
276
f427ee49
A
277 if (((wa_reqd = cpuid_wa_required(CPU_INTEL_SRBDS)) & CWA_ON) != 0 &&
278 ((wa_reqd & CWA_FORCE_ON) == CWA_ON ||
279 (cpuinfo->cpuid_leaf7_extfeatures & CPUID_LEAF7_EXTFEATURE_SRBDS_CTRL) != 0)) {
280 /* This must be executed on all logical processors */
281 uint64_t mcuoptctrl = rdmsr64(MSR_IA32_MCU_OPT_CTRL);
282 mcuoptctrl |= MSR_IA32_MCUOPTCTRL_RNGDS_MITG_DIS;
283 wrmsr64(MSR_IA32_MCU_OPT_CTRL, mcuoptctrl);
284 }
eb6b6ca3 285
0a7de745
A
286 if (on_slave) {
287 return;
288 }
289
290 switch (cpuid_wa_required(CPU_INTEL_SEGCHK)) {
291 case CWA_FORCE_ON:
292 force_thread_policy_tecs = 1;
293
294 /* If hyperthreaded, enable idle workaround */
295 if (cpuinfo->thread_count > cpuinfo->core_count) {
296 force_tecs_at_idle = 1;
297 }
298
f427ee49 299 OS_FALLTHROUGH;
0a7de745
A
300 case CWA_ON:
301 tecs_mode_supported = 1;
302 break;
303
304 case CWA_FORCE_OFF:
305 case CWA_OFF:
306 tecs_mode_supported = 0;
307 force_tecs_at_idle = 0;
308 force_thread_policy_tecs = 0;
309 break;
310
311 default:
312 break;
313 }
314}
315
316void
317cpuid_do_was(void)
318{
319 do_cwas(cpuid_info(), TRUE);
320}
321
2d21ac55
A
322/* this function is Intel-specific */
323static void
324cpuid_set_cache_info( i386_cpu_info_t * info_p )
91447636 325{
0a7de745
A
326 uint32_t cpuid_result[4];
327 uint32_t reg[4];
328 uint32_t index;
329 uint32_t linesizes[LCACHE_MAX];
330 unsigned int i;
331 unsigned int j;
332 boolean_t cpuid_deterministic_supported = FALSE;
55e303ae 333
7ddcb079
A
334 DBG("cpuid_set_cache_info(%p)\n", info_p);
335
0a7de745 336 bzero( linesizes, sizeof(linesizes));
2d21ac55
A
337
338 /* Get processor cache descriptor info using leaf 2. We don't use
339 * this internally, but must publish it for KEXTs.
340 */
7e4a7d39 341 cpuid_fn(2, cpuid_result);
55e303ae 342 for (j = 0; j < 4; j++) {
0a7de745 343 if ((cpuid_result[j] >> 31) == 1) { /* bit31 is validity */
55e303ae 344 continue;
0a7de745 345 }
f427ee49 346 ((uint32_t *)(void *)info_p->cache_info)[j] = cpuid_result[j];
55e303ae
A
347 }
348 /* first byte gives number of cpuid calls to get all descriptors */
349 for (i = 1; i < info_p->cache_info[0]; i++) {
0a7de745 350 if (i * 16 > sizeof(info_p->cache_info)) {
55e303ae 351 break;
0a7de745 352 }
7e4a7d39 353 cpuid_fn(2, cpuid_result);
55e303ae 354 for (j = 0; j < 4; j++) {
0a7de745 355 if ((cpuid_result[j] >> 31) == 1) {
55e303ae 356 continue;
0a7de745 357 }
f427ee49 358 ((uint32_t *)(void *)info_p->cache_info)[4 * i + j] =
0a7de745 359 cpuid_result[j];
55e303ae
A
360 }
361 }
362
0c530ab8 363 /*
2d21ac55
A
364 * Get cache info using leaf 4, the "deterministic cache parameters."
365 * Most processors Mac OS X supports implement this flavor of CPUID.
366 * Loop over each cache on the processor.
0c530ab8 367 */
7e4a7d39 368 cpuid_fn(0, cpuid_result);
0a7de745 369 if (cpuid_result[eax] >= 4) {
2d21ac55 370 cpuid_deterministic_supported = TRUE;
0a7de745 371 }
2d21ac55
A
372
373 for (index = 0; cpuid_deterministic_supported; index++) {
0a7de745
A
374 cache_type_t type = Lnone;
375 uint32_t cache_type;
376 uint32_t cache_level;
377 uint32_t cache_sharing;
378 uint32_t cache_linesize;
379 uint32_t cache_sets;
380 uint32_t cache_associativity;
381 uint32_t cache_size;
382 uint32_t cache_partitions;
383 uint32_t colors;
384
385 reg[eax] = 4; /* cpuid request 4 */
386 reg[ecx] = index; /* index starting at 0 */
2d21ac55 387 cpuid(reg);
7ddcb079 388 DBG("cpuid(4) index=%d eax=0x%x\n", index, reg[eax]);
7e4a7d39 389 cache_type = bitfield32(reg[eax], 4, 0);
0a7de745
A
390 if (cache_type == 0) {
391 break; /* no more caches */
392 }
393 cache_level = bitfield32(reg[eax], 7, 5);
394 cache_sharing = bitfield32(reg[eax], 25, 14) + 1;
395 info_p->cpuid_cores_per_package
396 = bitfield32(reg[eax], 31, 26) + 1;
397 cache_linesize = bitfield32(reg[ebx], 11, 0) + 1;
398 cache_partitions = bitfield32(reg[ebx], 21, 12) + 1;
399 cache_associativity = bitfield32(reg[ebx], 31, 22) + 1;
400 cache_sets = bitfield32(reg[ecx], 31, 0) + 1;
401
2d21ac55
A
402 /* Map type/levels returned by CPUID into cache_type_t */
403 switch (cache_level) {
404 case 1:
405 type = cache_type == 1 ? L1D :
0a7de745
A
406 cache_type == 2 ? L1I :
407 Lnone;
2d21ac55
A
408 break;
409 case 2:
410 type = cache_type == 3 ? L2U :
0a7de745 411 Lnone;
2d21ac55
A
412 break;
413 case 3:
414 type = cache_type == 3 ? L3U :
0a7de745 415 Lnone;
2d21ac55
A
416 break;
417 default:
418 type = Lnone;
419 }
0a7de745 420
2d21ac55 421 /* The total size of a cache is:
b0d623f7 422 * ( linesize * sets * associativity * partitions )
2d21ac55
A
423 */
424 if (type != Lnone) {
b0d623f7 425 cache_size = cache_linesize * cache_sets *
0a7de745 426 cache_associativity * cache_partitions;
2d21ac55
A
427 info_p->cache_size[type] = cache_size;
428 info_p->cache_sharing[type] = cache_sharing;
429 info_p->cache_partitions[type] = cache_partitions;
430 linesizes[type] = cache_linesize;
6d2010ae 431
7ddcb079
A
432 DBG(" cache_size[%s] : %d\n",
433 cache_type_str[type], cache_size);
434 DBG(" cache_sharing[%s] : %d\n",
435 cache_type_str[type], cache_sharing);
436 DBG(" cache_partitions[%s]: %d\n",
437 cache_type_str[type], cache_partitions);
438
6d2010ae
A
439 /*
440 * Overwrite associativity determined via
441 * CPUID.0x80000006 -- this leaf is more
442 * accurate
443 */
0a7de745 444 if (type == L2U) {
6d2010ae 445 info_p->cpuid_cache_L2_associativity = cache_associativity;
0a7de745
A
446 }
447 /*
448 * Adjust #sets to account for the N CBos
449 * This is because addresses are hashed across CBos
450 */
451 if (type == L3U && info_p->core_count) {
452 cache_sets = cache_sets / info_p->core_count;
453 }
6d2010ae 454
2d21ac55
A
455 /* Compute the number of page colors for this cache,
456 * which is:
457 * ( linesize * sets ) / page_size
458 *
459 * To help visualize this, consider two views of a
460 * physical address. To the cache, it is composed
461 * of a line offset, a set selector, and a tag.
462 * To VM, it is composed of a page offset, a page
463 * color, and other bits in the pageframe number:
464 *
465 * +-----------------+---------+--------+
466 * cache: | tag | set | offset |
467 * +-----------------+---------+--------+
468 *
469 * +-----------------+-------+----------+
470 * VM: | don't care | color | pg offset|
471 * +-----------------+-------+----------+
472 *
473 * The color is those bits in (set+offset) not covered
474 * by the page offset.
475 */
0a7de745
A
476 colors = (cache_linesize * cache_sets) >> 12;
477
478 if (colors > vm_cache_geometry_colors) {
2d21ac55 479 vm_cache_geometry_colors = colors;
0a7de745 480 }
2d21ac55 481 }
0a7de745 482 }
7ddcb079 483 DBG(" vm_cache_geometry_colors: %d\n", vm_cache_geometry_colors);
0a7de745 484
2d21ac55
A
485 /*
486 * If deterministic cache parameters are not available, use
487 * something else
488 */
489 if (info_p->cpuid_cores_per_package == 0) {
490 info_p->cpuid_cores_per_package = 1;
91447636 491
2d21ac55
A
492 /* cpuid define in 1024 quantities */
493 info_p->cache_size[L2U] = info_p->cpuid_cache_size * 1024;
494 info_p->cache_sharing[L2U] = 1;
495 info_p->cache_partitions[L2U] = 1;
91447636 496
2d21ac55 497 linesizes[L2U] = info_p->cpuid_cache_linesize;
7ddcb079
A
498
499 DBG(" cache_size[L2U] : %d\n",
500 info_p->cache_size[L2U]);
501 DBG(" cache_sharing[L2U] : 1\n");
502 DBG(" cache_partitions[L2U]: 1\n");
503 DBG(" linesizes[L2U] : %d\n",
504 info_p->cpuid_cache_linesize);
2d21ac55 505 }
0a7de745 506
2d21ac55
A
507 /*
508 * What linesize to publish? We use the L2 linesize if any,
509 * else the L1D.
510 */
0a7de745 511 if (linesizes[L2U]) {
2d21ac55 512 info_p->cache_linesize = linesizes[L2U];
0a7de745 513 } else if (linesizes[L1D]) {
2d21ac55 514 info_p->cache_linesize = linesizes[L1D];
0a7de745
A
515 } else {
516 panic("no linesize");
517 }
7ddcb079 518 DBG(" cache_linesize : %d\n", info_p->cache_linesize);
593a1d5f
A
519
520 /*
b0d623f7 521 * Extract and publish TLB information from Leaf 2 descriptors.
593a1d5f 522 */
7ddcb079 523 DBG(" %ld leaf2 descriptors:\n", sizeof(info_p->cache_info));
593a1d5f 524 for (i = 1; i < sizeof(info_p->cache_info); i++) {
0a7de745
A
525 cpuid_cache_descriptor_t *descp;
526 int id;
527 int level;
528 int page;
593a1d5f 529
7ddcb079 530 DBG(" 0x%02x", info_p->cache_info[i]);
b0d623f7 531 descp = cpuid_leaf2_find(info_p->cache_info[i]);
0a7de745 532 if (descp == NULL) {
b0d623f7 533 continue;
0a7de745 534 }
b0d623f7
A
535
536 switch (descp->type) {
537 case TLB:
538 page = (descp->size == SMALL) ? TLB_SMALL : TLB_LARGE;
539 /* determine I or D: */
540 switch (descp->level) {
541 case INST:
542 id = TLB_INST;
543 break;
544 case DATA:
545 case DATA0:
546 case DATA1:
547 id = TLB_DATA;
548 break;
549 default:
550 continue;
551 }
552 /* determine level: */
553 switch (descp->level) {
554 case DATA1:
555 level = 1;
556 break;
557 default:
558 level = 0;
559 }
560 info_p->cpuid_tlb[id][page][level] = descp->entries;
593a1d5f 561 break;
b0d623f7
A
562 case STLB:
563 info_p->cpuid_stlb = descp->entries;
593a1d5f
A
564 }
565 }
7ddcb079 566 DBG("\n");
91447636
A
567}
568
569static void
2d21ac55 570cpuid_set_generic_info(i386_cpu_info_t *info_p)
91447636 571{
0a7de745
A
572 uint32_t reg[4];
573 char str[128], *p;
91447636 574
7ddcb079
A
575 DBG("cpuid_set_generic_info(%p)\n", info_p);
576
2d21ac55 577 /* do cpuid 0 to get vendor */
7e4a7d39
A
578 cpuid_fn(0, reg);
579 info_p->cpuid_max_basic = reg[eax];
580 bcopy((char *)&reg[ebx], &info_p->cpuid_vendor[0], 4); /* ug */
581 bcopy((char *)&reg[ecx], &info_p->cpuid_vendor[8], 4);
582 bcopy((char *)&reg[edx], &info_p->cpuid_vendor[4], 4);
2d21ac55
A
583 info_p->cpuid_vendor[12] = 0;
584
91447636 585 /* get extended cpuid results */
7e4a7d39
A
586 cpuid_fn(0x80000000, reg);
587 info_p->cpuid_max_ext = reg[eax];
91447636
A
588
589 /* check to see if we can get brand string */
b0d623f7 590 if (info_p->cpuid_max_ext >= 0x80000004) {
91447636
A
591 /*
592 * The brand string 48 bytes (max), guaranteed to
593 * be NUL terminated.
594 */
7e4a7d39
A
595 cpuid_fn(0x80000002, reg);
596 bcopy((char *)reg, &str[0], 16);
597 cpuid_fn(0x80000003, reg);
598 bcopy((char *)reg, &str[16], 16);
599 cpuid_fn(0x80000004, reg);
600 bcopy((char *)reg, &str[32], 16);
91447636 601 for (p = str; *p != '\0'; p++) {
0a7de745
A
602 if (*p != ' ') {
603 break;
604 }
91447636 605 }
2d21ac55 606 strlcpy(info_p->cpuid_brand_string,
0a7de745
A
607 p, sizeof(info_p->cpuid_brand_string));
608
609 if (!strncmp(info_p->cpuid_brand_string, CPUID_STRING_UNKNOWN,
610 min(sizeof(info_p->cpuid_brand_string),
611 strlen(CPUID_STRING_UNKNOWN) + 1))) {
612 /*
613 * This string means we have a firmware-programmable brand string,
614 * and the firmware couldn't figure out what sort of CPU we have.
615 */
616 info_p->cpuid_brand_string[0] = '\0';
617 }
91447636 618 }
0a7de745 619
2d21ac55 620 /* Get cache and addressing info. */
b0d623f7 621 if (info_p->cpuid_max_ext >= 0x80000006) {
6d2010ae 622 uint32_t assoc;
7e4a7d39
A
623 cpuid_fn(0x80000006, reg);
624 info_p->cpuid_cache_linesize = bitfield32(reg[ecx], 7, 0);
0a7de745 625 assoc = bitfield32(reg[ecx], 15, 12);
6d2010ae
A
626 /*
627 * L2 associativity is encoded, though in an insufficiently
628 * descriptive fashion, e.g. 24-way is mapped to 16-way.
629 * Represent a fully associative cache as 0xFFFF.
630 * Overwritten by associativity as determined via CPUID.4
631 * if available.
632 */
0a7de745 633 if (assoc == 6) {
6d2010ae 634 assoc = 8;
0a7de745 635 } else if (assoc == 8) {
6d2010ae 636 assoc = 16;
0a7de745 637 } else if (assoc == 0xF) {
6d2010ae 638 assoc = 0xFFFF;
0a7de745 639 }
6d2010ae 640 info_p->cpuid_cache_L2_associativity = assoc;
0a7de745 641 info_p->cpuid_cache_size = bitfield32(reg[ecx], 31, 16);
7e4a7d39 642 cpuid_fn(0x80000008, reg);
2d21ac55 643 info_p->cpuid_address_bits_physical =
0a7de745 644 bitfield32(reg[eax], 7, 0);
2d21ac55 645 info_p->cpuid_address_bits_virtual =
0a7de745 646 bitfield32(reg[eax], 15, 8);
2d21ac55
A
647 }
648
6d2010ae
A
649 /*
650 * Get processor signature and decode
651 * and bracket this with the approved procedure for reading the
652 * the microcode version number a.k.a. signature a.k.a. BIOS ID
653 */
654 wrmsr64(MSR_IA32_BIOS_SIGN_ID, 0);
7e4a7d39 655 cpuid_fn(1, reg);
6d2010ae 656 info_p->cpuid_microcode_version =
0a7de745 657 (uint32_t) (rdmsr64(MSR_IA32_BIOS_SIGN_ID) >> 32);
7e4a7d39 658 info_p->cpuid_signature = reg[eax];
0a7de745
A
659 info_p->cpuid_stepping = bitfield32(reg[eax], 3, 0);
660 info_p->cpuid_model = bitfield32(reg[eax], 7, 4);
661 info_p->cpuid_family = bitfield32(reg[eax], 11, 8);
7e4a7d39
A
662 info_p->cpuid_type = bitfield32(reg[eax], 13, 12);
663 info_p->cpuid_extmodel = bitfield32(reg[eax], 19, 16);
664 info_p->cpuid_extfamily = bitfield32(reg[eax], 27, 20);
0a7de745 665 info_p->cpuid_brand = bitfield32(reg[ebx], 7, 0);
7e4a7d39 666 info_p->cpuid_features = quad(reg[ecx], reg[edx]);
2d21ac55 667
6d2010ae 668 /* Get "processor flag"; necessary for microcode update matching */
0a7de745 669 info_p->cpuid_processor_flag = (rdmsr64(MSR_IA32_PLATFORM_ID) >> 50) & 0x7;
6d2010ae 670
2d21ac55 671 /* Fold extensions into family/model */
0a7de745 672 if (info_p->cpuid_family == 0x0f) {
2d21ac55 673 info_p->cpuid_family += info_p->cpuid_extfamily;
0a7de745
A
674 }
675 if (info_p->cpuid_family == 0x0f || info_p->cpuid_family == 0x06) {
2d21ac55 676 info_p->cpuid_model += (info_p->cpuid_extmodel << 4);
0a7de745 677 }
2d21ac55 678
0a7de745 679 if (info_p->cpuid_features & CPUID_FEATURE_HTT) {
2d21ac55 680 info_p->cpuid_logical_per_package =
0a7de745
A
681 bitfield32(reg[ebx], 23, 16);
682 } else {
2d21ac55 683 info_p->cpuid_logical_per_package = 1;
0a7de745 684 }
0c530ab8 685
b0d623f7 686 if (info_p->cpuid_max_ext >= 0x80000001) {
7e4a7d39 687 cpuid_fn(0x80000001, reg);
0c530ab8 688 info_p->cpuid_extfeatures =
0a7de745 689 quad(reg[ecx], reg[edx]);
2d21ac55
A
690 }
691
7ddcb079
A
692 DBG(" max_basic : %d\n", info_p->cpuid_max_basic);
693 DBG(" max_ext : 0x%08x\n", info_p->cpuid_max_ext);
694 DBG(" vendor : %s\n", info_p->cpuid_vendor);
695 DBG(" brand_string : %s\n", info_p->cpuid_brand_string);
696 DBG(" signature : 0x%08x\n", info_p->cpuid_signature);
697 DBG(" stepping : %d\n", info_p->cpuid_stepping);
698 DBG(" model : %d\n", info_p->cpuid_model);
699 DBG(" family : %d\n", info_p->cpuid_family);
700 DBG(" type : %d\n", info_p->cpuid_type);
701 DBG(" extmodel : %d\n", info_p->cpuid_extmodel);
702 DBG(" extfamily : %d\n", info_p->cpuid_extfamily);
703 DBG(" brand : %d\n", info_p->cpuid_brand);
704 DBG(" features : 0x%016llx\n", info_p->cpuid_features);
705 DBG(" extfeatures : 0x%016llx\n", info_p->cpuid_extfeatures);
706 DBG(" logical_per_package : %d\n", info_p->cpuid_logical_per_package);
316670eb 707 DBG(" microcode_version : 0x%08x\n", info_p->cpuid_microcode_version);
7ddcb079 708
c910b4d9 709 /* Fold in the Invariant TSC feature bit, if present */
b0d623f7 710 if (info_p->cpuid_max_ext >= 0x80000007) {
0a7de745 711 cpuid_fn(0x80000007, reg);
c910b4d9 712 info_p->cpuid_extfeatures |=
0a7de745 713 reg[edx] & (uint32_t)CPUID_EXTFEATURE_TSCI;
7ddcb079
A
714 DBG(" extfeatures : 0x%016llx\n",
715 info_p->cpuid_extfeatures);
c910b4d9
A
716 }
717
b0d623f7 718 if (info_p->cpuid_max_basic >= 0x5) {
0a7de745 719 cpuid_mwait_leaf_t *cmp = &info_p->cpuid_mwait_leaf;
7e4a7d39 720
2d21ac55
A
721 /*
722 * Extract the Monitor/Mwait Leaf info:
723 */
7e4a7d39
A
724 cpuid_fn(5, reg);
725 cmp->linesize_min = reg[eax];
726 cmp->linesize_max = reg[ebx];
727 cmp->extensions = reg[ecx];
728 cmp->sub_Cstates = reg[edx];
729 info_p->cpuid_mwait_leafp = cmp;
7ddcb079
A
730
731 DBG(" Monitor/Mwait Leaf:\n");
732 DBG(" linesize_min : %d\n", cmp->linesize_min);
733 DBG(" linesize_max : %d\n", cmp->linesize_max);
734 DBG(" extensions : %d\n", cmp->extensions);
735 DBG(" sub_Cstates : 0x%08x\n", cmp->sub_Cstates);
b0d623f7 736 }
2d21ac55 737
b0d623f7 738 if (info_p->cpuid_max_basic >= 0x6) {
0a7de745 739 cpuid_thermal_leaf_t *ctp = &info_p->cpuid_thermal_leaf;
7e4a7d39 740
2d21ac55 741 /*
b0d623f7 742 * The thermal and Power Leaf:
2d21ac55 743 */
7e4a7d39 744 cpuid_fn(6, reg);
0a7de745 745 ctp->sensor = bitfield32(reg[eax], 0, 0);
7e4a7d39 746 ctp->dynamic_acceleration = bitfield32(reg[eax], 1, 1);
b7266188 747 ctp->invariant_APIC_timer = bitfield32(reg[eax], 2, 2);
bd504ef0
A
748 ctp->core_power_limits = bitfield32(reg[eax], 4, 4);
749 ctp->fine_grain_clock_mod = bitfield32(reg[eax], 5, 5);
750 ctp->package_thermal_intr = bitfield32(reg[eax], 6, 6);
0a7de745
A
751 ctp->thresholds = bitfield32(reg[ebx], 3, 0);
752 ctp->ACNT_MCNT = bitfield32(reg[ecx], 0, 0);
753 ctp->hardware_feedback = bitfield32(reg[ecx], 1, 1);
754 ctp->energy_policy = bitfield32(reg[ecx], 3, 3);
7e4a7d39 755 info_p->cpuid_thermal_leafp = ctp;
7ddcb079
A
756
757 DBG(" Thermal/Power Leaf:\n");
758 DBG(" sensor : %d\n", ctp->sensor);
759 DBG(" dynamic_acceleration : %d\n", ctp->dynamic_acceleration);
760 DBG(" invariant_APIC_timer : %d\n", ctp->invariant_APIC_timer);
761 DBG(" core_power_limits : %d\n", ctp->core_power_limits);
762 DBG(" fine_grain_clock_mod : %d\n", ctp->fine_grain_clock_mod);
763 DBG(" package_thermal_intr : %d\n", ctp->package_thermal_intr);
764 DBG(" thresholds : %d\n", ctp->thresholds);
765 DBG(" ACNT_MCNT : %d\n", ctp->ACNT_MCNT);
bd504ef0 766 DBG(" ACNT2 : %d\n", ctp->hardware_feedback);
7ddcb079 767 DBG(" energy_policy : %d\n", ctp->energy_policy);
b0d623f7 768 }
2d21ac55 769
b0d623f7 770 if (info_p->cpuid_max_basic >= 0xa) {
0a7de745 771 cpuid_arch_perf_leaf_t *capp = &info_p->cpuid_arch_perf_leaf;
7e4a7d39 772
2d21ac55 773 /*
b0d623f7 774 * Architectural Performance Monitoring Leaf:
2d21ac55 775 */
7e4a7d39 776 cpuid_fn(0xa, reg);
0a7de745
A
777 capp->version = bitfield32(reg[eax], 7, 0);
778 capp->number = bitfield32(reg[eax], 15, 8);
779 capp->width = bitfield32(reg[eax], 23, 16);
7e4a7d39 780 capp->events_number = bitfield32(reg[eax], 31, 24);
0a7de745
A
781 capp->events = reg[ebx];
782 capp->fixed_number = bitfield32(reg[edx], 4, 0);
783 capp->fixed_width = bitfield32(reg[edx], 12, 5);
7e4a7d39 784 info_p->cpuid_arch_perf_leafp = capp;
7ddcb079
A
785
786 DBG(" Architectural Performance Monitoring Leaf:\n");
787 DBG(" version : %d\n", capp->version);
788 DBG(" number : %d\n", capp->number);
789 DBG(" width : %d\n", capp->width);
790 DBG(" events_number : %d\n", capp->events_number);
791 DBG(" events : %d\n", capp->events);
792 DBG(" fixed_number : %d\n", capp->fixed_number);
793 DBG(" fixed_width : %d\n", capp->fixed_width);
0c530ab8 794 }
55e303ae 795
060df5ea 796 if (info_p->cpuid_max_basic >= 0xd) {
0a7de745 797 cpuid_xsave_leaf_t *xsp;
060df5ea
A
798 /*
799 * XSAVE Features:
800 */
3e170ce0 801 xsp = &info_p->cpuid_xsave_leaf[0];
060df5ea 802 info_p->cpuid_xsave_leafp = xsp;
3e170ce0
A
803 xsp->extended_state[eax] = 0xd;
804 xsp->extended_state[ecx] = 0;
805 cpuid(xsp->extended_state);
806 DBG(" XSAVE Main leaf:\n");
807 DBG(" EAX : 0x%x\n", xsp->extended_state[eax]);
808 DBG(" EBX : 0x%x\n", xsp->extended_state[ebx]);
809 DBG(" ECX : 0x%x\n", xsp->extended_state[ecx]);
810 DBG(" EDX : 0x%x\n", xsp->extended_state[edx]);
7ddcb079 811
3e170ce0
A
812 xsp = &info_p->cpuid_xsave_leaf[1];
813 xsp->extended_state[eax] = 0xd;
814 xsp->extended_state[ecx] = 1;
815 cpuid(xsp->extended_state);
816 DBG(" XSAVE Sub-leaf1:\n");
7ddcb079
A
817 DBG(" EAX : 0x%x\n", xsp->extended_state[eax]);
818 DBG(" EBX : 0x%x\n", xsp->extended_state[ebx]);
819 DBG(" ECX : 0x%x\n", xsp->extended_state[ecx]);
820 DBG(" EDX : 0x%x\n", xsp->extended_state[edx]);
060df5ea
A
821 }
822
bd504ef0 823 if (info_p->cpuid_model >= CPUID_MODEL_IVYBRIDGE) {
13f56ec4 824 /*
bd504ef0 825 * Leaf7 Features:
13f56ec4
A
826 */
827 cpuid_fn(0x7, reg);
a1c7dba1 828 info_p->cpuid_leaf7_features = quad(reg[ecx], reg[ebx]);
0a7de745 829 info_p->cpuid_leaf7_extfeatures = reg[edx];
13f56ec4 830
f427ee49
A
831 cpuid_tsx_supported = (reg[ebx] & (CPUID_LEAF7_FEATURE_HLE | CPUID_LEAF7_FEATURE_RTM)) != 0;
832
13f56ec4
A
833 DBG(" Feature Leaf7:\n");
834 DBG(" EBX : 0x%x\n", reg[ebx]);
a1c7dba1 835 DBG(" ECX : 0x%x\n", reg[ecx]);
0a7de745 836 DBG(" EDX : 0x%x\n", reg[edx]);
13f56ec4 837 }
2dced7af
A
838
839 if (info_p->cpuid_max_basic >= 0x15) {
840 /*
841 * TCS/CCC frequency leaf:
842 */
843 cpuid_fn(0x15, reg);
844 info_p->cpuid_tsc_leaf.denominator = reg[eax];
845 info_p->cpuid_tsc_leaf.numerator = reg[ebx];
846
847 DBG(" TSC/CCC Information Leaf:\n");
848 DBG(" numerator : 0x%x\n", reg[ebx]);
849 DBG(" denominator : 0x%x\n", reg[eax]);
850 }
851
852 return;
55e303ae
A
853}
854
7e4a7d39
A
855static uint32_t
856cpuid_set_cpufamily(i386_cpu_info_t *info_p)
857{
858 uint32_t cpufamily = CPUFAMILY_UNKNOWN;
859
860 switch (info_p->cpuid_family) {
861 case 6:
862 switch (info_p->cpuid_model) {
7e4a7d39
A
863 case 23:
864 cpufamily = CPUFAMILY_INTEL_PENRYN;
865 break;
866 case CPUID_MODEL_NEHALEM:
867 case CPUID_MODEL_FIELDS:
868 case CPUID_MODEL_DALES:
869 case CPUID_MODEL_NEHALEM_EX:
870 cpufamily = CPUFAMILY_INTEL_NEHALEM;
871 break;
d1ecb069
A
872 case CPUID_MODEL_DALES_32NM:
873 case CPUID_MODEL_WESTMERE:
874 case CPUID_MODEL_WESTMERE_EX:
875 cpufamily = CPUFAMILY_INTEL_WESTMERE;
876 break;
060df5ea
A
877 case CPUID_MODEL_SANDYBRIDGE:
878 case CPUID_MODEL_JAKETOWN:
879 cpufamily = CPUFAMILY_INTEL_SANDYBRIDGE;
880 break;
13f56ec4 881 case CPUID_MODEL_IVYBRIDGE:
15129b1c 882 case CPUID_MODEL_IVYBRIDGE_EP:
13f56ec4
A
883 cpufamily = CPUFAMILY_INTEL_IVYBRIDGE;
884 break;
bd504ef0 885 case CPUID_MODEL_HASWELL:
a1c7dba1 886 case CPUID_MODEL_HASWELL_EP:
bd504ef0
A
887 case CPUID_MODEL_HASWELL_ULT:
888 case CPUID_MODEL_CRYSTALWELL:
889 cpufamily = CPUFAMILY_INTEL_HASWELL;
890 break;
a1c7dba1
A
891 case CPUID_MODEL_BROADWELL:
892 case CPUID_MODEL_BRYSTALWELL:
893 cpufamily = CPUFAMILY_INTEL_BROADWELL;
894 break;
2dced7af
A
895 case CPUID_MODEL_SKYLAKE:
896 case CPUID_MODEL_SKYLAKE_DT:
5ba3f43e 897 case CPUID_MODEL_SKYLAKE_W:
2dced7af
A
898 cpufamily = CPUFAMILY_INTEL_SKYLAKE;
899 break;
0a7de745
A
900 case CPUID_MODEL_KABYLAKE:
901 case CPUID_MODEL_KABYLAKE_DT:
902 cpufamily = CPUFAMILY_INTEL_KABYLAKE;
903 break;
f427ee49
A
904 case CPUID_MODEL_ICELAKE:
905 case CPUID_MODEL_ICELAKE_H:
906 case CPUID_MODEL_ICELAKE_DT:
907 cpufamily = CPUFAMILY_INTEL_ICELAKE;
908 break;
7e4a7d39
A
909 }
910 break;
911 }
912
913 info_p->cpuid_cpufamily = cpufamily;
7ddcb079 914 DBG("cpuid_set_cpufamily(%p) returning 0x%x\n", info_p, cpufamily);
7e4a7d39
A
915 return cpufamily;
916}
060df5ea
A
917/*
918 * Must be invoked either when executing single threaded, or with
919 * independent synchronization.
920 */
2d21ac55
A
921void
922cpuid_set_info(void)
d7e50217 923{
0a7de745
A
924 i386_cpu_info_t *info_p = &cpuid_cpu_info;
925 boolean_t enable_x86_64h = TRUE;
7ddcb079 926
94ff46dc
A
927 /* Perform pre-cpuid workarounds (since their effects impact values returned via cpuid) */
928 cpuid_do_precpuid_was();
929
7e4a7d39 930 cpuid_set_generic_info(info_p);
55e303ae 931
2d21ac55 932 /* verify we are running on a supported CPU */
7e4a7d39 933 if ((strncmp(CPUID_VID_INTEL, info_p->cpuid_vendor,
0a7de745
A
934 min(strlen(CPUID_STRING_UNKNOWN) + 1,
935 sizeof(info_p->cpuid_vendor)))) ||
936 (cpuid_set_cpufamily(info_p) == CPUFAMILY_UNKNOWN)) {
2d21ac55 937 panic("Unsupported CPU");
0a7de745 938 }
2d21ac55 939
7e4a7d39 940 info_p->cpuid_cpu_type = CPU_TYPE_X86;
fe8ab488
A
941
942 if (!PE_parse_boot_argn("-enable_x86_64h", &enable_x86_64h, sizeof(enable_x86_64h))) {
0a7de745 943 boolean_t disable_x86_64h = FALSE;
fe8ab488
A
944
945 if (PE_parse_boot_argn("-disable_x86_64h", &disable_x86_64h, sizeof(disable_x86_64h))) {
946 enable_x86_64h = FALSE;
947 }
948 }
949
950 if (enable_x86_64h &&
951 ((info_p->cpuid_features & CPUID_X86_64_H_FEATURE_SUBSET) == CPUID_X86_64_H_FEATURE_SUBSET) &&
952 ((info_p->cpuid_extfeatures & CPUID_X86_64_H_EXTFEATURE_SUBSET) == CPUID_X86_64_H_EXTFEATURE_SUBSET) &&
953 ((info_p->cpuid_leaf7_features & CPUID_X86_64_H_LEAF7_FEATURE_SUBSET) == CPUID_X86_64_H_LEAF7_FEATURE_SUBSET)) {
954 info_p->cpuid_cpu_subtype = CPU_SUBTYPE_X86_64_H;
955 } else {
956 info_p->cpuid_cpu_subtype = CPU_SUBTYPE_X86_ARCH1;
957 }
5ba3f43e 958 /* cpuid_set_cache_info must be invoked after set_generic_info */
fe8ab488 959
7e4a7d39
A
960 /*
961 * Find the number of enabled cores and threads
962 * (which determines whether SMT/Hyperthreading is active).
963 */
5ba3f43e 964
f427ee49
A
965 /*
966 * Not all VMMs emulate MSR_CORE_THREAD_COUNT (0x35).
967 */
cb323159
A
968 if (0 != (info_p->cpuid_features & CPUID_FEATURE_VMM) &&
969 PE_parse_boot_argn("-nomsr35h", NULL, 0)) {
970 info_p->core_count = 1;
971 info_p->thread_count = 1;
5ba3f43e 972 cpuid_set_cache_info(info_p);
cb323159
A
973 } else {
974 switch (info_p->cpuid_cpufamily) {
975 case CPUFAMILY_INTEL_PENRYN:
976 cpuid_set_cache_info(info_p);
977 info_p->core_count = info_p->cpuid_cores_per_package;
978 info_p->thread_count = info_p->cpuid_logical_per_package;
979 break;
980 case CPUFAMILY_INTEL_WESTMERE: {
f427ee49
A
981 /*
982 * This should be the same as Nehalem but an A0 silicon bug returns
983 * invalid data in the top 12 bits. Hence, we use only bits [19..16]
984 * rather than [31..16] for core count - which actually can't exceed 8.
985 */
cb323159
A
986 uint64_t msr = rdmsr64(MSR_CORE_THREAD_COUNT);
987 if (0 == msr) {
988 /* Provide a non-zero default for some VMMs */
989 msr = (1 << 16) | 1;
990 }
991 info_p->core_count = bitfield32((uint32_t)msr, 19, 16);
992 info_p->thread_count = bitfield32((uint32_t)msr, 15, 0);
993 cpuid_set_cache_info(info_p);
994 break;
995 }
996 default: {
997 uint64_t msr = rdmsr64(MSR_CORE_THREAD_COUNT);
998 if (0 == msr) {
999 /* Provide a non-zero default for some VMMs */
1000 msr = (1 << 16) | 1;
1001 }
1002 info_p->core_count = bitfield32((uint32_t)msr, 31, 16);
1003 info_p->thread_count = bitfield32((uint32_t)msr, 15, 0);
1004 cpuid_set_cache_info(info_p);
1005 break;
1006 }
1007 }
0a7de745 1008 }
5ba3f43e 1009
7ddcb079
A
1010 DBG("cpuid_set_info():\n");
1011 DBG(" core_count : %d\n", info_p->core_count);
1012 DBG(" thread_count : %d\n", info_p->thread_count);
fe8ab488
A
1013 DBG(" cpu_type: 0x%08x\n", info_p->cpuid_cpu_type);
1014 DBG(" cpu_subtype: 0x%08x\n", info_p->cpuid_cpu_subtype);
593a1d5f 1015
39236c6e 1016 info_p->cpuid_model_string = ""; /* deprecated */
0a7de745 1017
f427ee49
A
1018 /* Init CPU LBRs */
1019 i386_lbr_init(info_p, true);
1020
0a7de745 1021 do_cwas(info_p, FALSE);
2d21ac55 1022}
55e303ae 1023
7ddcb079 1024static struct table {
0a7de745
A
1025 uint64_t mask;
1026 const char *name;
0c530ab8 1027} feature_map[] = {
0a7de745
A
1028 {CPUID_FEATURE_FPU, "FPU"},
1029 {CPUID_FEATURE_VME, "VME"},
1030 {CPUID_FEATURE_DE, "DE"},
1031 {CPUID_FEATURE_PSE, "PSE"},
1032 {CPUID_FEATURE_TSC, "TSC"},
1033 {CPUID_FEATURE_MSR, "MSR"},
1034 {CPUID_FEATURE_PAE, "PAE"},
1035 {CPUID_FEATURE_MCE, "MCE"},
1036 {CPUID_FEATURE_CX8, "CX8"},
1037 {CPUID_FEATURE_APIC, "APIC"},
1038 {CPUID_FEATURE_SEP, "SEP"},
1039 {CPUID_FEATURE_MTRR, "MTRR"},
1040 {CPUID_FEATURE_PGE, "PGE"},
1041 {CPUID_FEATURE_MCA, "MCA"},
1042 {CPUID_FEATURE_CMOV, "CMOV"},
1043 {CPUID_FEATURE_PAT, "PAT"},
1044 {CPUID_FEATURE_PSE36, "PSE36"},
1045 {CPUID_FEATURE_PSN, "PSN"},
1046 {CPUID_FEATURE_CLFSH, "CLFSH"},
1047 {CPUID_FEATURE_DS, "DS"},
1048 {CPUID_FEATURE_ACPI, "ACPI"},
1049 {CPUID_FEATURE_MMX, "MMX"},
1050 {CPUID_FEATURE_FXSR, "FXSR"},
1051 {CPUID_FEATURE_SSE, "SSE"},
1052 {CPUID_FEATURE_SSE2, "SSE2"},
1053 {CPUID_FEATURE_SS, "SS"},
1054 {CPUID_FEATURE_HTT, "HTT"},
1055 {CPUID_FEATURE_TM, "TM"},
1056 {CPUID_FEATURE_PBE, "PBE"},
1057 {CPUID_FEATURE_SSE3, "SSE3"},
d1ecb069 1058 {CPUID_FEATURE_PCLMULQDQ, "PCLMULQDQ"},
0a7de745
A
1059 {CPUID_FEATURE_DTES64, "DTES64"},
1060 {CPUID_FEATURE_MONITOR, "MON"},
1061 {CPUID_FEATURE_DSCPL, "DSCPL"},
1062 {CPUID_FEATURE_VMX, "VMX"},
1063 {CPUID_FEATURE_SMX, "SMX"},
1064 {CPUID_FEATURE_EST, "EST"},
1065 {CPUID_FEATURE_TM2, "TM2"},
1066 {CPUID_FEATURE_SSSE3, "SSSE3"},
1067 {CPUID_FEATURE_CID, "CID"},
1068 {CPUID_FEATURE_FMA, "FMA"},
1069 {CPUID_FEATURE_CX16, "CX16"},
1070 {CPUID_FEATURE_xTPR, "TPR"},
1071 {CPUID_FEATURE_PDCM, "PDCM"},
1072 {CPUID_FEATURE_SSE4_1, "SSE4.1"},
1073 {CPUID_FEATURE_SSE4_2, "SSE4.2"},
1074 {CPUID_FEATURE_x2APIC, "x2APIC"},
1075 {CPUID_FEATURE_MOVBE, "MOVBE"},
1076 {CPUID_FEATURE_POPCNT, "POPCNT"},
1077 {CPUID_FEATURE_AES, "AES"},
1078 {CPUID_FEATURE_VMM, "VMM"},
1079 {CPUID_FEATURE_PCID, "PCID"},
1080 {CPUID_FEATURE_XSAVE, "XSAVE"},
1081 {CPUID_FEATURE_OSXSAVE, "OSXSAVE"},
1082 {CPUID_FEATURE_SEGLIM64, "SEGLIM64"},
1083 {CPUID_FEATURE_TSCTMR, "TSCTMR"},
1084 {CPUID_FEATURE_AVX1_0, "AVX1.0"},
1085 {CPUID_FEATURE_RDRAND, "RDRAND"},
1086 {CPUID_FEATURE_F16C, "F16C"},
0c530ab8
A
1087 {0, 0}
1088},
0a7de745 1089 extfeature_map[] = {
0c530ab8 1090 {CPUID_EXTFEATURE_SYSCALL, "SYSCALL"},
0a7de745 1091 {CPUID_EXTFEATURE_XD, "XD"},
d1ecb069 1092 {CPUID_EXTFEATURE_1GBPAGE, "1GBPAGE"},
0a7de745
A
1093 {CPUID_EXTFEATURE_EM64T, "EM64T"},
1094 {CPUID_EXTFEATURE_LAHF, "LAHF"},
1095 {CPUID_EXTFEATURE_LZCNT, "LZCNT"},
fe8ab488 1096 {CPUID_EXTFEATURE_PREFETCHW, "PREFETCHW"},
0a7de745
A
1097 {CPUID_EXTFEATURE_RDTSCP, "RDTSCP"},
1098 {CPUID_EXTFEATURE_TSCI, "TSCI"},
55e303ae 1099 {0, 0}
13f56ec4 1100},
0a7de745 1101 leaf7_feature_map[] = {
13f56ec4 1102 {CPUID_LEAF7_FEATURE_RDWRFSGS, "RDWRFSGS"},
0a7de745
A
1103 {CPUID_LEAF7_FEATURE_TSCOFF, "TSC_THREAD_OFFSET"},
1104 {CPUID_LEAF7_FEATURE_SGX, "SGX"},
1105 {CPUID_LEAF7_FEATURE_BMI1, "BMI1"},
1106 {CPUID_LEAF7_FEATURE_HLE, "HLE"},
1107 {CPUID_LEAF7_FEATURE_AVX2, "AVX2"},
1108 {CPUID_LEAF7_FEATURE_FDPEO, "FDPEO"},
1109 {CPUID_LEAF7_FEATURE_SMEP, "SMEP"},
1110 {CPUID_LEAF7_FEATURE_BMI2, "BMI2"},
1111 {CPUID_LEAF7_FEATURE_ERMS, "ERMS"},
1112 {CPUID_LEAF7_FEATURE_INVPCID, "INVPCID"},
1113 {CPUID_LEAF7_FEATURE_RTM, "RTM"},
1114 {CPUID_LEAF7_FEATURE_PQM, "PQM"},
1115 {CPUID_LEAF7_FEATURE_FPU_CSDS, "FPU_CSDS"},
1116 {CPUID_LEAF7_FEATURE_MPX, "MPX"},
1117 {CPUID_LEAF7_FEATURE_PQE, "PQE"},
1118 {CPUID_LEAF7_FEATURE_AVX512F, "AVX512F"},
5ba3f43e 1119 {CPUID_LEAF7_FEATURE_AVX512DQ, "AVX512DQ"},
0a7de745
A
1120 {CPUID_LEAF7_FEATURE_RDSEED, "RDSEED"},
1121 {CPUID_LEAF7_FEATURE_ADX, "ADX"},
1122 {CPUID_LEAF7_FEATURE_SMAP, "SMAP"},
1123 {CPUID_LEAF7_FEATURE_AVX512IFMA, "AVX512IFMA"},
1124 {CPUID_LEAF7_FEATURE_CLFSOPT, "CLFSOPT"},
1125 {CPUID_LEAF7_FEATURE_CLWB, "CLWB"},
1126 {CPUID_LEAF7_FEATURE_IPT, "IPT"},
1127 {CPUID_LEAF7_FEATURE_AVX512CD, "AVX512CD"},
1128 {CPUID_LEAF7_FEATURE_SHA, "SHA"},
5ba3f43e
A
1129 {CPUID_LEAF7_FEATURE_AVX512BW, "AVX512BW"},
1130 {CPUID_LEAF7_FEATURE_AVX512VL, "AVX512VL"},
0a7de745 1131 {CPUID_LEAF7_FEATURE_PREFETCHWT1, "PREFETCHWT1"},
5ba3f43e 1132 {CPUID_LEAF7_FEATURE_AVX512VBMI, "AVX512VBMI"},
0a7de745
A
1133 {CPUID_LEAF7_FEATURE_UMIP, "UMIP"},
1134 {CPUID_LEAF7_FEATURE_PKU, "PKU"},
1135 {CPUID_LEAF7_FEATURE_OSPKE, "OSPKE"},
1136 {CPUID_LEAF7_FEATURE_WAITPKG, "WAITPKG"},
1137 {CPUID_LEAF7_FEATURE_GFNI, "GFNI"},
cb323159
A
1138 {CPUID_LEAF7_FEATURE_VAES, "VAES"},
1139 {CPUID_LEAF7_FEATURE_VPCLMULQDQ, "VPCLMULQDQ"},
1140 {CPUID_LEAF7_FEATURE_AVX512VNNI, "AVX512VNNI"},
1141 {CPUID_LEAF7_FEATURE_AVX512BITALG, "AVX512BITALG"},
1142 {CPUID_LEAF7_FEATURE_AVX512VPCDQ, "AVX512VPOPCNTDQ"},
0a7de745
A
1143 {CPUID_LEAF7_FEATURE_RDPID, "RDPID"},
1144 {CPUID_LEAF7_FEATURE_CLDEMOTE, "CLDEMOTE"},
1145 {CPUID_LEAF7_FEATURE_MOVDIRI, "MOVDIRI"},
1146 {CPUID_LEAF7_FEATURE_MOVDIRI64B, "MOVDIRI64B"},
1147 {CPUID_LEAF7_FEATURE_SGXLC, "SGXLC"},
1148 {0, 0}
1149},
1150 leaf7_extfeature_map[] = {
1151 { CPUID_LEAF7_EXTFEATURE_AVX5124VNNIW, "AVX5124VNNIW" },
1152 { CPUID_LEAF7_EXTFEATURE_AVX5124FMAPS, "AVX5124FMAPS" },
cb323159 1153 { CPUID_LEAF7_EXTFEATURE_FSREPMOV, "FSREPMOV" },
0a7de745
A
1154 { CPUID_LEAF7_EXTFEATURE_MDCLEAR, "MDCLEAR" },
1155 { CPUID_LEAF7_EXTFEATURE_TSXFA, "TSXFA" },
1156 { CPUID_LEAF7_EXTFEATURE_IBRS, "IBRS" },
1157 { CPUID_LEAF7_EXTFEATURE_STIBP, "STIBP" },
1158 { CPUID_LEAF7_EXTFEATURE_L1DF, "L1DF" },
1159 { CPUID_LEAF7_EXTFEATURE_ACAPMSR, "ACAPMSR" },
1160 { CPUID_LEAF7_EXTFEATURE_CCAPMSR, "CCAPMSR" },
1161 { CPUID_LEAF7_EXTFEATURE_SSBD, "SSBD" },
13f56ec4 1162 {0, 0}
55e303ae
A
1163};
1164
7ddcb079
A
1165static char *
1166cpuid_get_names(struct table *map, uint64_t bits, char *buf, unsigned buf_len)
1167{
0a7de745
A
1168 size_t len = 0;
1169 char *p = buf;
1170 int i;
7ddcb079
A
1171
1172 for (i = 0; map[i].mask != 0; i++) {
0a7de745 1173 if ((bits & map[i].mask) == 0) {
7ddcb079 1174 continue;
0a7de745
A
1175 }
1176 if (len && ((size_t) (p - buf) < (buf_len - 1))) {
7ddcb079 1177 *p++ = ' ';
0a7de745
A
1178 }
1179 len = min(strlen(map[i].name), (size_t)((buf_len - 1) - (p - buf)));
1180 if (len == 0) {
7ddcb079 1181 break;
0a7de745 1182 }
7ddcb079
A
1183 bcopy(map[i].name, p, len);
1184 p += len;
1185 }
1186 *p = '\0';
1187 return buf;
1188}
1189
0a7de745 1190i386_cpu_info_t *
0c530ab8
A
1191cpuid_info(void)
1192{
593a1d5f 1193 /* Set-up the cpuid_info stucture lazily */
0c530ab8 1194 if (cpuid_cpu_infop == NULL) {
39236c6e 1195 PE_parse_boot_argn("-cpuid", &cpuid_dbg, sizeof(cpuid_dbg));
2d21ac55 1196 cpuid_set_info();
0c530ab8
A
1197 cpuid_cpu_infop = &cpuid_cpu_info;
1198 }
1199 return cpuid_cpu_infop;
1200}
1201
55e303ae 1202char *
0c530ab8 1203cpuid_get_feature_names(uint64_t features, char *buf, unsigned buf_len)
55e303ae 1204{
0a7de745 1205 return cpuid_get_names(feature_map, features, buf, buf_len);
0c530ab8
A
1206}
1207
1208char *
1209cpuid_get_extfeature_names(uint64_t extfeatures, char *buf, unsigned buf_len)
1210{
0a7de745 1211 return cpuid_get_names(extfeature_map, extfeatures, buf, buf_len);
55e303ae
A
1212}
1213
13f56ec4
A
1214char *
1215cpuid_get_leaf7_feature_names(uint64_t features, char *buf, unsigned buf_len)
1216{
0a7de745
A
1217 return cpuid_get_names(leaf7_feature_map, features, buf, buf_len);
1218}
1219
1220char *
1221cpuid_get_leaf7_extfeature_names(uint64_t features, char *buf, unsigned buf_len)
1222{
1223 return cpuid_get_names(leaf7_extfeature_map, features, buf, buf_len);
13f56ec4
A
1224}
1225
55e303ae
A
1226void
1227cpuid_feature_display(
0a7de745 1228 const char *header)
0c530ab8 1229{
0a7de745 1230 char buf[320];
0c530ab8 1231
7ddcb079 1232 kprintf("%s: %s", header,
0a7de745
A
1233 cpuid_get_feature_names(cpuid_features(), buf, sizeof(buf)));
1234 if (cpuid_leaf7_features()) {
13f56ec4 1235 kprintf(" %s", cpuid_get_leaf7_feature_names(
0a7de745
A
1236 cpuid_leaf7_features(), buf, sizeof(buf)));
1237 }
1238 if (cpuid_leaf7_extfeatures()) {
1239 kprintf(" %s", cpuid_get_leaf7_extfeature_names(
1240 cpuid_leaf7_extfeatures(), buf, sizeof(buf)));
1241 }
7ddcb079 1242 kprintf("\n");
0c530ab8 1243 if (cpuid_features() & CPUID_FEATURE_HTT) {
0a7de745 1244#define s_if_plural(n) ((n > 1) ? "s" : "")
0c530ab8 1245 kprintf(" HTT: %d core%s per package;"
0a7de745
A
1246 " %d logical cpu%s per package\n",
1247 cpuid_cpu_infop->cpuid_cores_per_package,
1248 s_if_plural(cpuid_cpu_infop->cpuid_cores_per_package),
1249 cpuid_cpu_infop->cpuid_logical_per_package,
1250 s_if_plural(cpuid_cpu_infop->cpuid_logical_per_package));
0c530ab8
A
1251 }
1252}
1253
1254void
1255cpuid_extfeature_display(
0a7de745 1256 const char *header)
c0fea474 1257{
0a7de745 1258 char buf[256];
c0fea474 1259
0c530ab8 1260 kprintf("%s: %s\n", header,
0a7de745
A
1261 cpuid_get_extfeature_names(cpuid_extfeatures(),
1262 buf, sizeof(buf)));
1c79356b
A
1263}
1264
1c79356b
A
1265void
1266cpuid_cpu_display(
0a7de745 1267 const char *header)
d7e50217 1268{
0a7de745
A
1269 if (cpuid_cpu_infop->cpuid_brand_string[0] != '\0') {
1270 kprintf("%s: %s\n", header, cpuid_cpu_infop->cpuid_brand_string);
1271 }
d7e50217
A
1272}
1273
55e303ae
A
1274unsigned int
1275cpuid_family(void)
1276{
0c530ab8 1277 return cpuid_info()->cpuid_family;
4452a7af
A
1278}
1279
7e4a7d39
A
1280uint32_t
1281cpuid_cpufamily(void)
1282{
1283 return cpuid_info()->cpuid_cpufamily;
1284}
1285
0c530ab8
A
1286cpu_type_t
1287cpuid_cputype(void)
1288{
1289 return cpuid_info()->cpuid_cpu_type;
1290}
1291
1292cpu_subtype_t
1293cpuid_cpusubtype(void)
1294{
1295 return cpuid_info()->cpuid_cpu_subtype;
1296}
1297
1298uint64_t
55e303ae
A
1299cpuid_features(void)
1300{
91447636 1301 static int checked = 0;
0a7de745 1302 char fpu_arg[20] = { 0 };
0c530ab8
A
1303
1304 (void) cpuid_info();
91447636 1305 if (!checked) {
0a7de745
A
1306 /* check for boot-time fpu limitations */
1307 if (PE_parse_boot_argn("_fpu", &fpu_arg[0], sizeof(fpu_arg))) {
1308 printf("limiting fpu features to: %s\n", fpu_arg);
1309 if (!strncmp("387", fpu_arg, sizeof("387")) || !strncmp("mmx", fpu_arg, sizeof("mmx"))) {
1310 printf("no sse or sse2\n");
1311 cpuid_cpu_infop->cpuid_features &= ~(CPUID_FEATURE_SSE | CPUID_FEATURE_SSE2 | CPUID_FEATURE_FXSR);
1312 } else if (!strncmp("sse", fpu_arg, sizeof("sse"))) {
1313 printf("no sse2\n");
1314 cpuid_cpu_infop->cpuid_features &= ~(CPUID_FEATURE_SSE2);
91447636 1315 }
0a7de745
A
1316 }
1317 checked = 1;
91447636 1318 }
39236c6e 1319 return cpuid_cpu_infop->cpuid_features;
55e303ae
A
1320}
1321
0c530ab8
A
1322uint64_t
1323cpuid_extfeatures(void)
55e303ae 1324{
0c530ab8 1325 return cpuid_info()->cpuid_extfeatures;
55e303ae 1326}
0a7de745 1327
13f56ec4
A
1328uint64_t
1329cpuid_leaf7_features(void)
1330{
1331 return cpuid_info()->cpuid_leaf7_features;
1332}
13f56ec4 1333
0a7de745
A
1334uint64_t
1335cpuid_leaf7_extfeatures(void)
1336{
1337 return cpuid_info()->cpuid_leaf7_extfeatures;
1338}
1339
f427ee49
A
1340const char *
1341cpuid_vmm_family_string(void)
1342{
1343 switch (cpuid_vmm_info()->cpuid_vmm_family) {
1344 case CPUID_VMM_FAMILY_NONE:
1345 return "None";
1346
1347 case CPUID_VMM_FAMILY_VMWARE:
1348 return "VMWare";
1349
1350 case CPUID_VMM_FAMILY_PARALLELS:
1351 return "Parallels";
1352
1353 case CPUID_VMM_FAMILY_HYVE:
1354 return "xHyve";
1355
1356 case CPUID_VMM_FAMILY_HVF:
1357 return "HVF";
1358
1359 case CPUID_VMM_FAMILY_KVM:
1360 return "KVM";
1361
1362 case CPUID_VMM_FAMILY_UNKNOWN:
1363 /*FALLTHROUGH*/
1364 default:
1365 return "Unknown VMM";
1366 }
1367}
1368
0a7de745
A
1369static i386_vmm_info_t *_cpuid_vmm_infop = NULL;
1370static i386_vmm_info_t _cpuid_vmm_info;
0c530ab8 1371
316670eb
A
1372static void
1373cpuid_init_vmm_info(i386_vmm_info_t *info_p)
0c530ab8 1374{
f427ee49 1375 uint32_t reg[4], maxbasic_regs[4];
0a7de745 1376 uint32_t max_vmm_leaf;
0c530ab8 1377
316670eb 1378 bzero(info_p, sizeof(*info_p));
0c530ab8 1379
0a7de745 1380 if (!cpuid_vmm_present()) {
316670eb 1381 return;
0a7de745 1382 }
0c530ab8 1383
316670eb
A
1384 DBG("cpuid_init_vmm_info(%p)\n", info_p);
1385
f427ee49
A
1386 /*
1387 * Get the highest basic leaf value, then save the cpuid details for that leaf
1388 * for comparison with the [ostensible] VMM leaf.
1389 */
1390 cpuid_fn(0, reg);
1391 cpuid_fn(reg[eax], maxbasic_regs);
1392
316670eb
A
1393 /* do cpuid 0x40000000 to get VMM vendor */
1394 cpuid_fn(0x40000000, reg);
f427ee49
A
1395
1396 /*
1397 * If leaf 0x40000000 is non-existent, cpuid will return the values as
1398 * if the highest basic leaf was requested, so compare to those values
1399 * we just retrieved to see if no vmm is present.
1400 */
1401 if (bcmp(reg, maxbasic_regs, sizeof(reg)) == 0) {
1402 info_p->cpuid_vmm_family = CPUID_VMM_FAMILY_NONE;
1403 DBG(" vmm_vendor : NONE\n");
1404 return;
1405 }
1406
316670eb
A
1407 max_vmm_leaf = reg[eax];
1408 bcopy((char *)&reg[ebx], &info_p->cpuid_vmm_vendor[0], 4);
1409 bcopy((char *)&reg[ecx], &info_p->cpuid_vmm_vendor[4], 4);
1410 bcopy((char *)&reg[edx], &info_p->cpuid_vmm_vendor[8], 4);
1411 info_p->cpuid_vmm_vendor[12] = '\0';
1412
1413 if (0 == strcmp(info_p->cpuid_vmm_vendor, CPUID_VMM_ID_VMWARE)) {
1414 /* VMware identification string: kb.vmware.com/kb/1009458 */
1415 info_p->cpuid_vmm_family = CPUID_VMM_FAMILY_VMWARE;
f427ee49 1416 } else if (0 == bcmp(info_p->cpuid_vmm_vendor, CPUID_VMM_ID_PARALLELS, 12)) {
fe8ab488
A
1417 /* Parallels identification string */
1418 info_p->cpuid_vmm_family = CPUID_VMM_FAMILY_PARALLELS;
f427ee49
A
1419 } else if (0 == bcmp(info_p->cpuid_vmm_vendor, CPUID_VMM_ID_HYVE, 12)) {
1420 /* bhyve/xhyve identification string */
1421 info_p->cpuid_vmm_family = CPUID_VMM_FAMILY_HYVE;
1422 } else if (0 == bcmp(info_p->cpuid_vmm_vendor, CPUID_VMM_ID_HVF, 12)) {
1423 /* HVF identification string */
1424 info_p->cpuid_vmm_family = CPUID_VMM_FAMILY_HVF;
1425 } else if (0 == bcmp(info_p->cpuid_vmm_vendor, CPUID_VMM_ID_KVM, 12)) {
1426 /* KVM identification string */
1427 info_p->cpuid_vmm_family = CPUID_VMM_FAMILY_KVM;
316670eb
A
1428 } else {
1429 info_p->cpuid_vmm_family = CPUID_VMM_FAMILY_UNKNOWN;
0c530ab8 1430 }
316670eb
A
1431
1432 /* VMM generic leaves: https://lkml.org/lkml/2008/10/1/246 */
1433 if (max_vmm_leaf >= 0x40000010) {
1434 cpuid_fn(0x40000010, reg);
0a7de745 1435
316670eb
A
1436 info_p->cpuid_vmm_tsc_frequency = reg[eax];
1437 info_p->cpuid_vmm_bus_frequency = reg[ebx];
0c530ab8 1438 }
316670eb
A
1439
1440 DBG(" vmm_vendor : %s\n", info_p->cpuid_vmm_vendor);
1441 DBG(" vmm_family : %u\n", info_p->cpuid_vmm_family);
1442 DBG(" vmm_bus_frequency : %u\n", info_p->cpuid_vmm_bus_frequency);
1443 DBG(" vmm_tsc_frequency : %u\n", info_p->cpuid_vmm_tsc_frequency);
0c530ab8
A
1444}
1445
316670eb
A
1446boolean_t
1447cpuid_vmm_present(void)
1448{
1449 return (cpuid_features() & CPUID_FEATURE_VMM) ? TRUE : FALSE;
1450}
1451
1452i386_vmm_info_t *
1453cpuid_vmm_info(void)
1454{
1455 if (_cpuid_vmm_infop == NULL) {
1456 cpuid_init_vmm_info(&_cpuid_vmm_info);
1457 _cpuid_vmm_infop = &_cpuid_vmm_info;
1458 }
1459 return _cpuid_vmm_infop;
1460}
1461
1462uint32_t
1463cpuid_vmm_family(void)
1464{
1465 return cpuid_vmm_info()->cpuid_vmm_family;
1466}
39236c6e 1467
0a7de745
A
1468cwa_classifier_e
1469cpuid_wa_required(cpu_wa_e wa)
1470{
94ff46dc 1471 i386_cpu_info_t *info_p = &cpuid_cpu_info;
0a7de745
A
1472 static uint64_t bootarg_cpu_wa_enables = 0;
1473 static uint64_t bootarg_cpu_wa_disables = 0;
1474 static int bootargs_overrides_processed = 0;
f427ee49 1475 uint32_t reg[4];
0a7de745
A
1476
1477 if (!bootargs_overrides_processed) {
1478 if (!PE_parse_boot_argn("cwae", &bootarg_cpu_wa_enables, sizeof(bootarg_cpu_wa_enables))) {
1479 bootarg_cpu_wa_enables = 0;
1480 }
1481
1482 if (!PE_parse_boot_argn("cwad", &bootarg_cpu_wa_disables, sizeof(bootarg_cpu_wa_disables))) {
1483 bootarg_cpu_wa_disables = 0;
1484 }
1485 bootargs_overrides_processed = 1;
1486 }
1487
1488 if (bootarg_cpu_wa_enables & (1 << wa)) {
1489 return CWA_FORCE_ON;
1490 }
1491
1492 if (bootarg_cpu_wa_disables & (1 << wa)) {
1493 return CWA_FORCE_OFF;
1494 }
1495
1496 switch (wa) {
1497 case CPU_INTEL_SEGCHK:
1498 /* First, check to see if this CPU requires the workaround */
1499 if ((info_p->cpuid_leaf7_extfeatures & CPUID_LEAF7_EXTFEATURE_ACAPMSR) != 0) {
1500 /* We have ARCHCAP, so check it for either RDCL_NO or MDS_NO */
1501 uint64_t archcap_msr = rdmsr64(MSR_IA32_ARCH_CAPABILITIES);
1502 if ((archcap_msr & (MSR_IA32_ARCH_CAPABILITIES_RDCL_NO | MSR_IA32_ARCH_CAPABILITIES_MDS_NO)) != 0) {
1503 /* Workaround not needed */
1504 return CWA_OFF;
1505 }
1506 }
1507
1508 if ((info_p->cpuid_leaf7_extfeatures & CPUID_LEAF7_EXTFEATURE_MDCLEAR) != 0) {
1509 return CWA_ON;
1510 }
1511
1512 /*
1513 * If the CPU supports the ARCHCAP MSR and neither the RDCL_NO bit nor the MDS_NO
1514 * bit are set, OR the CPU does not support the ARCHCAP MSR and the CPU does
1515 * not enumerate the presence of the enhanced VERW instruction, report
1516 * that the workaround should not be enabled.
1517 */
1518 break;
1519
1520 case CPU_INTEL_TSXFA:
f427ee49
A
1521 /*
1522 * Note that if TSX was disabled in cpuid_do_precpuid_was(), the cached cpuid
1523 * info will indicate that RTM is *not* supported and this workaround will not
1524 * be enabled.
1525 */
0a7de745 1526 /*
94ff46dc 1527 * Otherwise, if the CPU supports both TSX(HLE) and FORCE_ABORT, return that
0a7de745
A
1528 * the workaround should be enabled.
1529 */
1530 if ((info_p->cpuid_leaf7_extfeatures & CPUID_LEAF7_EXTFEATURE_TSXFA) != 0 &&
1531 (info_p->cpuid_leaf7_features & CPUID_LEAF7_FEATURE_RTM) != 0) {
1532 return CWA_ON;
1533 }
1534 break;
1535
f427ee49
A
1536 case CPU_INTEL_TSXDA:
1537 /*
1538 * Since this workaround might be requested before cpuid_set_info() is complete,
1539 * we need to invoke cpuid directly when looking for the required bits.
1540 */
1541 cpuid_fn(0x7, reg);
1542 if (reg[edx] & CPUID_LEAF7_EXTFEATURE_ACAPMSR) {
1543 uint64_t archcap_msr = rdmsr64(MSR_IA32_ARCH_CAPABILITIES);
1544 /*
1545 * If this CPU supports TSX (HLE being the proxy for TSX detection) AND it does
1546 * not include a hardware fix for TAA and it supports the TSX_CTRL MSR, disable TSX entirely.
1547 * (Note this can be overridden (above) if the cwad boot-arg's value has bit 2 set.)
1548 */
1549 if ((reg[ebx] & CPUID_LEAF7_FEATURE_HLE) != 0 &&
1550 (archcap_msr & (MSR_IA32_ARCH_CAPABILITIES_TAA_NO | MSR_IA32_ARCH_CAPABILITIES_TSX_CTRL))
1551 == MSR_IA32_ARCH_CAPABILITIES_TSX_CTRL) {
1552 return CWA_ON;
1553 }
1554 }
1555 break;
1556
1557 case CPU_INTEL_SRBDS:
1558 /*
1559 * SRBDS mitigations are enabled by default. CWA_ON returned here indicates
1560 * the caller should disable the mitigation. Mitigations should be disabled
1561 * at least for CPUs that advertise MDS_NO *and* (either TAA_NO is set OR TSX
1562 * has been disabled).
1563 */
1564 if ((info_p->cpuid_leaf7_extfeatures & CPUID_LEAF7_EXTFEATURE_SRBDS_CTRL) != 0) {
1565 if ((info_p->cpuid_leaf7_extfeatures & CPUID_LEAF7_EXTFEATURE_ACAPMSR) != 0) {
1566 uint64_t archcap_msr = rdmsr64(MSR_IA32_ARCH_CAPABILITIES);
1567 if ((archcap_msr & MSR_IA32_ARCH_CAPABILITIES_MDS_NO) != 0 &&
1568 ((archcap_msr & MSR_IA32_ARCH_CAPABILITIES_TAA_NO) != 0 ||
1569 cpuid_tsx_disabled)) {
1570 return CWA_ON;
1571 }
1572 }
1573 }
1574 break;
eb6b6ca3 1575
0a7de745
A
1576 default:
1577 break;
1578 }
1579
1580 return CWA_OFF;
1581}
94ff46dc
A
1582
1583static void
1584cpuid_do_precpuid_was(void)
1585{
1586 /*
1587 * Note that care must be taken not to use any data from the cached cpuid data since it is
1588 * likely uninitialized at this point. That includes calling functions that make use of
1589 * that data as well.
1590 */
1591
f427ee49
A
1592 /* Note the TSX disablement, we do not support force-on since it depends on MSRs being present */
1593 if (cpuid_wa_required(CPU_INTEL_TSXDA) == CWA_ON) {
1594 /* This must be executed on all logical processors */
1595 wrmsr64(MSR_IA32_TSX_CTRL, MSR_IA32_TSXCTRL_TSX_CPU_CLEAR | MSR_IA32_TSXCTRL_RTM_DISABLE);
1596 cpuid_tsx_disabled = true;
1597 }
94ff46dc 1598}