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