xref: /titanic_50/usr/src/uts/i86pc/os/cpuid.c (revision 53548f91e84cd97a638c23b5b295cc69089a5030)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011 by Delphix. All rights reserved.
24  * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25  * Copyright 2014 Josef "Jeff" Sipek <jeffpc@josefsipek.net>
26  */
27 /*
28  * Copyright (c) 2010, Intel Corporation.
29  * All rights reserved.
30  */
31 /*
32  * Portions Copyright 2009 Advanced Micro Devices, Inc.
33  */
34 /*
35  * Copyright 2016 Joyent, Inc.
36  */
37 /*
38  * Various routines to handle identification
39  * and classification of x86 processors.
40  */
41 
42 #include <sys/types.h>
43 #include <sys/archsystm.h>
44 #include <sys/x86_archext.h>
45 #include <sys/kmem.h>
46 #include <sys/systm.h>
47 #include <sys/cmn_err.h>
48 #include <sys/sunddi.h>
49 #include <sys/sunndi.h>
50 #include <sys/cpuvar.h>
51 #include <sys/processor.h>
52 #include <sys/sysmacros.h>
53 #include <sys/pg.h>
54 #include <sys/fp.h>
55 #include <sys/controlregs.h>
56 #include <sys/bitmap.h>
57 #include <sys/auxv_386.h>
58 #include <sys/memnode.h>
59 #include <sys/pci_cfgspace.h>
60 #include <sys/comm_page.h>
61 #include <sys/tsc.h>
62 
63 #ifdef __xpv
64 #include <sys/hypervisor.h>
65 #else
66 #include <sys/ontrap.h>
67 #endif
68 
69 /*
70  * Pass 0 of cpuid feature analysis happens in locore. It contains special code
71  * to recognize Cyrix processors that are not cpuid-compliant, and to deal with
72  * them accordingly. For most modern processors, feature detection occurs here
73  * in pass 1.
74  *
75  * Pass 1 of cpuid feature analysis happens just at the beginning of mlsetup()
76  * for the boot CPU and does the basic analysis that the early kernel needs.
77  * x86_featureset is set based on the return value of cpuid_pass1() of the boot
78  * CPU.
79  *
80  * Pass 1 includes:
81  *
82  *	o Determining vendor/model/family/stepping and setting x86_type and
83  *	  x86_vendor accordingly.
84  *	o Processing the feature flags returned by the cpuid instruction while
85  *	  applying any workarounds or tricks for the specific processor.
86  *	o Mapping the feature flags into Solaris feature bits (X86_*).
87  *	o Processing extended feature flags if supported by the processor,
88  *	  again while applying specific processor knowledge.
89  *	o Determining the CMT characteristics of the system.
90  *
91  * Pass 1 is done on non-boot CPUs during their initialization and the results
92  * are used only as a meager attempt at ensuring that all processors within the
93  * system support the same features.
94  *
95  * Pass 2 of cpuid feature analysis happens just at the beginning
96  * of startup().  It just copies in and corrects the remainder
97  * of the cpuid data we depend on: standard cpuid functions that we didn't
98  * need for pass1 feature analysis, and extended cpuid functions beyond the
99  * simple feature processing done in pass1.
100  *
101  * Pass 3 of cpuid analysis is invoked after basic kernel services; in
102  * particular kernel memory allocation has been made available. It creates a
103  * readable brand string based on the data collected in the first two passes.
104  *
105  * Pass 4 of cpuid analysis is invoked after post_startup() when all
106  * the support infrastructure for various hardware features has been
107  * initialized. It determines which processor features will be reported
108  * to userland via the aux vector.
109  *
110  * All passes are executed on all CPUs, but only the boot CPU determines what
111  * features the kernel will use.
112  *
113  * Much of the worst junk in this file is for the support of processors
114  * that didn't really implement the cpuid instruction properly.
115  *
116  * NOTE: The accessor functions (cpuid_get*) are aware of, and ASSERT upon,
117  * the pass numbers.  Accordingly, changes to the pass code may require changes
118  * to the accessor code.
119  */
120 
121 uint_t x86_vendor = X86_VENDOR_IntelClone;
122 uint_t x86_type = X86_TYPE_OTHER;
123 uint_t x86_clflush_size = 0;
124 
125 uint_t pentiumpro_bug4046376;
126 
127 uchar_t x86_featureset[BT_SIZEOFMAP(NUM_X86_FEATURES)];
128 
129 static char *x86_feature_names[NUM_X86_FEATURES] = {
130 	"lgpg",
131 	"tsc",
132 	"msr",
133 	"mtrr",
134 	"pge",
135 	"de",
136 	"cmov",
137 	"mmx",
138 	"mca",
139 	"pae",
140 	"cv8",
141 	"pat",
142 	"sep",
143 	"sse",
144 	"sse2",
145 	"htt",
146 	"asysc",
147 	"nx",
148 	"sse3",
149 	"cx16",
150 	"cmp",
151 	"tscp",
152 	"mwait",
153 	"sse4a",
154 	"cpuid",
155 	"ssse3",
156 	"sse4_1",
157 	"sse4_2",
158 	"1gpg",
159 	"clfsh",
160 	"64",
161 	"aes",
162 	"pclmulqdq",
163 	"xsave",
164 	"avx",
165 	"vmx",
166 	"svm",
167 	"topoext",
168 	"f16c",
169 	"rdrand",
170 	"x2apic",
171 	"avx2",
172 	"bmi1",
173 	"bmi2",
174 	"fma",
175 	"smep",
176 	"adx",
177 	"rdseed"
178 };
179 
180 boolean_t
181 is_x86_feature(void *featureset, uint_t feature)
182 {
183 	ASSERT(feature < NUM_X86_FEATURES);
184 	return (BT_TEST((ulong_t *)featureset, feature));
185 }
186 
187 void
188 add_x86_feature(void *featureset, uint_t feature)
189 {
190 	ASSERT(feature < NUM_X86_FEATURES);
191 	BT_SET((ulong_t *)featureset, feature);
192 }
193 
194 void
195 remove_x86_feature(void *featureset, uint_t feature)
196 {
197 	ASSERT(feature < NUM_X86_FEATURES);
198 	BT_CLEAR((ulong_t *)featureset, feature);
199 }
200 
201 boolean_t
202 compare_x86_featureset(void *setA, void *setB)
203 {
204 	/*
205 	 * We assume that the unused bits of the bitmap are always zero.
206 	 */
207 	if (memcmp(setA, setB, BT_SIZEOFMAP(NUM_X86_FEATURES)) == 0) {
208 		return (B_TRUE);
209 	} else {
210 		return (B_FALSE);
211 	}
212 }
213 
214 void
215 print_x86_featureset(void *featureset)
216 {
217 	uint_t i;
218 
219 	for (i = 0; i < NUM_X86_FEATURES; i++) {
220 		if (is_x86_feature(featureset, i)) {
221 			cmn_err(CE_CONT, "?x86_feature: %s\n",
222 			    x86_feature_names[i]);
223 		}
224 	}
225 }
226 
227 static size_t xsave_state_size = 0;
228 uint64_t xsave_bv_all = (XFEATURE_LEGACY_FP | XFEATURE_SSE);
229 boolean_t xsave_force_disable = B_FALSE;
230 
231 /*
232  * This is set to platform type we are running on.
233  */
234 static int platform_type = -1;
235 
236 #if !defined(__xpv)
237 /*
238  * Variable to patch if hypervisor platform detection needs to be
239  * disabled (e.g. platform_type will always be HW_NATIVE if this is 0).
240  */
241 int enable_platform_detection = 1;
242 #endif
243 
244 /*
245  * monitor/mwait info.
246  *
247  * size_actual and buf_actual are the real address and size allocated to get
248  * proper mwait_buf alignement.  buf_actual and size_actual should be passed
249  * to kmem_free().  Currently kmem_alloc() and mwait happen to both use
250  * processor cache-line alignment, but this is not guarantied in the furture.
251  */
252 struct mwait_info {
253 	size_t		mon_min;	/* min size to avoid missed wakeups */
254 	size_t		mon_max;	/* size to avoid false wakeups */
255 	size_t		size_actual;	/* size actually allocated */
256 	void		*buf_actual;	/* memory actually allocated */
257 	uint32_t	support;	/* processor support of monitor/mwait */
258 };
259 
260 /*
261  * xsave/xrestor info.
262  *
263  * This structure contains HW feature bits and size of the xsave save area.
264  * Note: the kernel will use the maximum size required for all hardware
265  * features. It is not optimize for potential memory savings if features at
266  * the end of the save area are not enabled.
267  */
268 struct xsave_info {
269 	uint32_t	xsav_hw_features_low;   /* Supported HW features */
270 	uint32_t	xsav_hw_features_high;  /* Supported HW features */
271 	size_t		xsav_max_size;  /* max size save area for HW features */
272 	size_t		ymm_size;	/* AVX: size of ymm save area */
273 	size_t		ymm_offset;	/* AVX: offset for ymm save area */
274 };
275 
276 
277 /*
278  * These constants determine how many of the elements of the
279  * cpuid we cache in the cpuid_info data structure; the
280  * remaining elements are accessible via the cpuid instruction.
281  */
282 
283 #define	NMAX_CPI_STD	8		/* eax = 0 .. 7 */
284 #define	NMAX_CPI_EXTD	0x1f		/* eax = 0x80000000 .. 0x8000001e */
285 
286 /*
287  * Some terminology needs to be explained:
288  *  - Socket: Something that can be plugged into a motherboard.
289  *  - Package: Same as socket
290  *  - Chip: Same as socket. Note that AMD's documentation uses term "chip"
291  *    differently: there, chip is the same as processor node (below)
292  *  - Processor node: Some AMD processors have more than one
293  *    "subprocessor" embedded in a package. These subprocessors (nodes)
294  *    are fully-functional processors themselves with cores, caches,
295  *    memory controllers, PCI configuration spaces. They are connected
296  *    inside the package with Hypertransport links. On single-node
297  *    processors, processor node is equivalent to chip/socket/package.
298  *  - Compute Unit: Some AMD processors pair cores in "compute units" that
299  *    share the FPU and the I$ and L2 caches.
300  */
301 
302 struct cpuid_info {
303 	uint_t cpi_pass;		/* last pass completed */
304 	/*
305 	 * standard function information
306 	 */
307 	uint_t cpi_maxeax;		/* fn 0: %eax */
308 	char cpi_vendorstr[13];		/* fn 0: %ebx:%ecx:%edx */
309 	uint_t cpi_vendor;		/* enum of cpi_vendorstr */
310 
311 	uint_t cpi_family;		/* fn 1: extended family */
312 	uint_t cpi_model;		/* fn 1: extended model */
313 	uint_t cpi_step;		/* fn 1: stepping */
314 	chipid_t cpi_chipid;		/* fn 1: %ebx:  Intel: chip # */
315 					/*		AMD: package/socket # */
316 	uint_t cpi_brandid;		/* fn 1: %ebx: brand ID */
317 	int cpi_clogid;			/* fn 1: %ebx: thread # */
318 	uint_t cpi_ncpu_per_chip;	/* fn 1: %ebx: logical cpu count */
319 	uint8_t cpi_cacheinfo[16];	/* fn 2: intel-style cache desc */
320 	uint_t cpi_ncache;		/* fn 2: number of elements */
321 	uint_t cpi_ncpu_shr_last_cache;	/* fn 4: %eax: ncpus sharing cache */
322 	id_t cpi_last_lvl_cacheid;	/* fn 4: %eax: derived cache id */
323 	uint_t cpi_std_4_size;		/* fn 4: number of fn 4 elements */
324 	struct cpuid_regs **cpi_std_4;	/* fn 4: %ecx == 0 .. fn4_size */
325 	struct cpuid_regs cpi_std[NMAX_CPI_STD];	/* 0 .. 7 */
326 	/*
327 	 * extended function information
328 	 */
329 	uint_t cpi_xmaxeax;		/* fn 0x80000000: %eax */
330 	char cpi_brandstr[49];		/* fn 0x8000000[234] */
331 	uint8_t cpi_pabits;		/* fn 0x80000006: %eax */
332 	uint8_t	cpi_vabits;		/* fn 0x80000006: %eax */
333 	struct	cpuid_regs cpi_extd[NMAX_CPI_EXTD];	/* 0x800000XX */
334 
335 	id_t cpi_coreid;		/* same coreid => strands share core */
336 	int cpi_pkgcoreid;		/* core number within single package */
337 	uint_t cpi_ncore_per_chip;	/* AMD: fn 0x80000008: %ecx[7-0] */
338 					/* Intel: fn 4: %eax[31-26] */
339 	/*
340 	 * supported feature information
341 	 */
342 	uint32_t cpi_support[6];
343 #define	STD_EDX_FEATURES	0
344 #define	AMD_EDX_FEATURES	1
345 #define	TM_EDX_FEATURES		2
346 #define	STD_ECX_FEATURES	3
347 #define	AMD_ECX_FEATURES	4
348 #define	STD_EBX_FEATURES	5
349 	/*
350 	 * Synthesized information, where known.
351 	 */
352 	uint32_t cpi_chiprev;		/* See X86_CHIPREV_* in x86_archext.h */
353 	const char *cpi_chiprevstr;	/* May be NULL if chiprev unknown */
354 	uint32_t cpi_socket;		/* Chip package/socket type */
355 
356 	struct mwait_info cpi_mwait;	/* fn 5: monitor/mwait info */
357 	uint32_t cpi_apicid;
358 	uint_t cpi_procnodeid;		/* AMD: nodeID on HT, Intel: chipid */
359 	uint_t cpi_procnodes_per_pkg;	/* AMD: # of nodes in the package */
360 					/* Intel: 1 */
361 	uint_t cpi_compunitid;		/* AMD: ComputeUnit ID, Intel: coreid */
362 	uint_t cpi_cores_per_compunit;	/* AMD: # of cores in the ComputeUnit */
363 
364 	struct xsave_info cpi_xsave;	/* fn D: xsave/xrestor info */
365 };
366 
367 
368 static struct cpuid_info cpuid_info0;
369 
370 /*
371  * These bit fields are defined by the Intel Application Note AP-485
372  * "Intel Processor Identification and the CPUID Instruction"
373  */
374 #define	CPI_FAMILY_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 27, 20)
375 #define	CPI_MODEL_XTD(cpi)	BITX((cpi)->cpi_std[1].cp_eax, 19, 16)
376 #define	CPI_TYPE(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 13, 12)
377 #define	CPI_FAMILY(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 11, 8)
378 #define	CPI_STEP(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 3, 0)
379 #define	CPI_MODEL(cpi)		BITX((cpi)->cpi_std[1].cp_eax, 7, 4)
380 
381 #define	CPI_FEATURES_EDX(cpi)		((cpi)->cpi_std[1].cp_edx)
382 #define	CPI_FEATURES_ECX(cpi)		((cpi)->cpi_std[1].cp_ecx)
383 #define	CPI_FEATURES_XTD_EDX(cpi)	((cpi)->cpi_extd[1].cp_edx)
384 #define	CPI_FEATURES_XTD_ECX(cpi)	((cpi)->cpi_extd[1].cp_ecx)
385 #define	CPI_FEATURES_7_0_EBX(cpi)	((cpi)->cpi_std[7].cp_ebx)
386 
387 #define	CPI_BRANDID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 7, 0)
388 #define	CPI_CHUNKS(cpi)		BITX((cpi)->cpi_std[1].cp_ebx, 15, 7)
389 #define	CPI_CPU_COUNT(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 23, 16)
390 #define	CPI_APIC_ID(cpi)	BITX((cpi)->cpi_std[1].cp_ebx, 31, 24)
391 
392 #define	CPI_MAXEAX_MAX		0x100		/* sanity control */
393 #define	CPI_XMAXEAX_MAX		0x80000100
394 #define	CPI_FN4_ECX_MAX		0x20		/* sanity: max fn 4 levels */
395 #define	CPI_FNB_ECX_MAX		0x20		/* sanity: max fn B levels */
396 
397 /*
398  * Function 4 (Deterministic Cache Parameters) macros
399  * Defined by Intel Application Note AP-485
400  */
401 #define	CPI_NUM_CORES(regs)		BITX((regs)->cp_eax, 31, 26)
402 #define	CPI_NTHR_SHR_CACHE(regs)	BITX((regs)->cp_eax, 25, 14)
403 #define	CPI_FULL_ASSOC_CACHE(regs)	BITX((regs)->cp_eax, 9, 9)
404 #define	CPI_SELF_INIT_CACHE(regs)	BITX((regs)->cp_eax, 8, 8)
405 #define	CPI_CACHE_LVL(regs)		BITX((regs)->cp_eax, 7, 5)
406 #define	CPI_CACHE_TYPE(regs)		BITX((regs)->cp_eax, 4, 0)
407 #define	CPI_CPU_LEVEL_TYPE(regs)	BITX((regs)->cp_ecx, 15, 8)
408 
409 #define	CPI_CACHE_WAYS(regs)		BITX((regs)->cp_ebx, 31, 22)
410 #define	CPI_CACHE_PARTS(regs)		BITX((regs)->cp_ebx, 21, 12)
411 #define	CPI_CACHE_COH_LN_SZ(regs)	BITX((regs)->cp_ebx, 11, 0)
412 
413 #define	CPI_CACHE_SETS(regs)		BITX((regs)->cp_ecx, 31, 0)
414 
415 #define	CPI_PREFCH_STRIDE(regs)		BITX((regs)->cp_edx, 9, 0)
416 
417 
418 /*
419  * A couple of shorthand macros to identify "later" P6-family chips
420  * like the Pentium M and Core.  First, the "older" P6-based stuff
421  * (loosely defined as "pre-Pentium-4"):
422  * P6, PII, Mobile PII, PII Xeon, PIII, Mobile PIII, PIII Xeon
423  */
424 
425 #define	IS_LEGACY_P6(cpi) (			\
426 	cpi->cpi_family == 6 && 		\
427 		(cpi->cpi_model == 1 ||		\
428 		cpi->cpi_model == 3 ||		\
429 		cpi->cpi_model == 5 ||		\
430 		cpi->cpi_model == 6 ||		\
431 		cpi->cpi_model == 7 ||		\
432 		cpi->cpi_model == 8 ||		\
433 		cpi->cpi_model == 0xA ||	\
434 		cpi->cpi_model == 0xB)		\
435 )
436 
437 /* A "new F6" is everything with family 6 that's not the above */
438 #define	IS_NEW_F6(cpi) ((cpi->cpi_family == 6) && !IS_LEGACY_P6(cpi))
439 
440 /* Extended family/model support */
441 #define	IS_EXTENDED_MODEL_INTEL(cpi) (cpi->cpi_family == 0x6 || \
442 	cpi->cpi_family >= 0xf)
443 
444 /*
445  * Info for monitor/mwait idle loop.
446  *
447  * See cpuid section of "Intel 64 and IA-32 Architectures Software Developer's
448  * Manual Volume 2A: Instruction Set Reference, A-M" #25366-022US, November
449  * 2006.
450  * See MONITOR/MWAIT section of "AMD64 Architecture Programmer's Manual
451  * Documentation Updates" #33633, Rev 2.05, December 2006.
452  */
453 #define	MWAIT_SUPPORT		(0x00000001)	/* mwait supported */
454 #define	MWAIT_EXTENSIONS	(0x00000002)	/* extenstion supported */
455 #define	MWAIT_ECX_INT_ENABLE	(0x00000004)	/* ecx 1 extension supported */
456 #define	MWAIT_SUPPORTED(cpi)	((cpi)->cpi_std[1].cp_ecx & CPUID_INTC_ECX_MON)
457 #define	MWAIT_INT_ENABLE(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x2)
458 #define	MWAIT_EXTENSION(cpi)	((cpi)->cpi_std[5].cp_ecx & 0x1)
459 #define	MWAIT_SIZE_MIN(cpi)	BITX((cpi)->cpi_std[5].cp_eax, 15, 0)
460 #define	MWAIT_SIZE_MAX(cpi)	BITX((cpi)->cpi_std[5].cp_ebx, 15, 0)
461 /*
462  * Number of sub-cstates for a given c-state.
463  */
464 #define	MWAIT_NUM_SUBC_STATES(cpi, c_state)			\
465 	BITX((cpi)->cpi_std[5].cp_edx, c_state + 3, c_state)
466 
467 /*
468  * XSAVE leaf 0xD enumeration
469  */
470 #define	CPUID_LEAFD_2_YMM_OFFSET	576
471 #define	CPUID_LEAFD_2_YMM_SIZE		256
472 
473 /*
474  * Functions we consune from cpuid_subr.c;  don't publish these in a header
475  * file to try and keep people using the expected cpuid_* interfaces.
476  */
477 extern uint32_t _cpuid_skt(uint_t, uint_t, uint_t, uint_t);
478 extern const char *_cpuid_sktstr(uint_t, uint_t, uint_t, uint_t);
479 extern uint32_t _cpuid_chiprev(uint_t, uint_t, uint_t, uint_t);
480 extern const char *_cpuid_chiprevstr(uint_t, uint_t, uint_t, uint_t);
481 extern uint_t _cpuid_vendorstr_to_vendorcode(char *);
482 
483 /*
484  * Apply up various platform-dependent restrictions where the
485  * underlying platform restrictions mean the CPU can be marked
486  * as less capable than its cpuid instruction would imply.
487  */
488 #if defined(__xpv)
489 static void
490 platform_cpuid_mangle(uint_t vendor, uint32_t eax, struct cpuid_regs *cp)
491 {
492 	switch (eax) {
493 	case 1: {
494 		uint32_t mcamask = DOMAIN_IS_INITDOMAIN(xen_info) ?
495 		    0 : CPUID_INTC_EDX_MCA;
496 		cp->cp_edx &=
497 		    ~(mcamask |
498 		    CPUID_INTC_EDX_PSE |
499 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
500 		    CPUID_INTC_EDX_SEP | CPUID_INTC_EDX_MTRR |
501 		    CPUID_INTC_EDX_PGE | CPUID_INTC_EDX_PAT |
502 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
503 		    CPUID_INTC_EDX_PSE36 | CPUID_INTC_EDX_HTT);
504 		break;
505 	}
506 
507 	case 0x80000001:
508 		cp->cp_edx &=
509 		    ~(CPUID_AMD_EDX_PSE |
510 		    CPUID_INTC_EDX_VME | CPUID_INTC_EDX_DE |
511 		    CPUID_AMD_EDX_MTRR | CPUID_AMD_EDX_PGE |
512 		    CPUID_AMD_EDX_PAT | CPUID_AMD_EDX_PSE36 |
513 		    CPUID_AMD_EDX_SYSC | CPUID_INTC_EDX_SEP |
514 		    CPUID_AMD_EDX_TSCP);
515 		cp->cp_ecx &= ~CPUID_AMD_ECX_CMP_LGCY;
516 		break;
517 	default:
518 		break;
519 	}
520 
521 	switch (vendor) {
522 	case X86_VENDOR_Intel:
523 		switch (eax) {
524 		case 4:
525 			/*
526 			 * Zero out the (ncores-per-chip - 1) field
527 			 */
528 			cp->cp_eax &= 0x03fffffff;
529 			break;
530 		default:
531 			break;
532 		}
533 		break;
534 	case X86_VENDOR_AMD:
535 		switch (eax) {
536 
537 		case 0x80000001:
538 			cp->cp_ecx &= ~CPUID_AMD_ECX_CR8D;
539 			break;
540 
541 		case 0x80000008:
542 			/*
543 			 * Zero out the (ncores-per-chip - 1) field
544 			 */
545 			cp->cp_ecx &= 0xffffff00;
546 			break;
547 		default:
548 			break;
549 		}
550 		break;
551 	default:
552 		break;
553 	}
554 }
555 #else
556 #define	platform_cpuid_mangle(vendor, eax, cp)	/* nothing */
557 #endif
558 
559 /*
560  *  Some undocumented ways of patching the results of the cpuid
561  *  instruction to permit running Solaris 10 on future cpus that
562  *  we don't currently support.  Could be set to non-zero values
563  *  via settings in eeprom.
564  */
565 
566 uint32_t cpuid_feature_ecx_include;
567 uint32_t cpuid_feature_ecx_exclude;
568 uint32_t cpuid_feature_edx_include;
569 uint32_t cpuid_feature_edx_exclude;
570 
571 /*
572  * Allocate space for mcpu_cpi in the machcpu structure for all non-boot CPUs.
573  */
574 void
575 cpuid_alloc_space(cpu_t *cpu)
576 {
577 	/*
578 	 * By convention, cpu0 is the boot cpu, which is set up
579 	 * before memory allocation is available.  All other cpus get
580 	 * their cpuid_info struct allocated here.
581 	 */
582 	ASSERT(cpu->cpu_id != 0);
583 	ASSERT(cpu->cpu_m.mcpu_cpi == NULL);
584 	cpu->cpu_m.mcpu_cpi =
585 	    kmem_zalloc(sizeof (*cpu->cpu_m.mcpu_cpi), KM_SLEEP);
586 }
587 
588 void
589 cpuid_free_space(cpu_t *cpu)
590 {
591 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
592 	int i;
593 
594 	ASSERT(cpi != NULL);
595 	ASSERT(cpi != &cpuid_info0);
596 
597 	/*
598 	 * Free up any function 4 related dynamic storage
599 	 */
600 	for (i = 1; i < cpi->cpi_std_4_size; i++)
601 		kmem_free(cpi->cpi_std_4[i], sizeof (struct cpuid_regs));
602 	if (cpi->cpi_std_4_size > 0)
603 		kmem_free(cpi->cpi_std_4,
604 		    cpi->cpi_std_4_size * sizeof (struct cpuid_regs *));
605 
606 	kmem_free(cpi, sizeof (*cpi));
607 	cpu->cpu_m.mcpu_cpi = NULL;
608 }
609 
610 #if !defined(__xpv)
611 /*
612  * Determine the type of the underlying platform. This is used to customize
613  * initialization of various subsystems (e.g. TSC). determine_platform() must
614  * only ever be called once to prevent two processors from seeing different
615  * values of platform_type. Must be called before cpuid_pass1(), the earliest
616  * consumer to execute (uses _cpuid_chiprev --> synth_amd_info --> get_hwenv).
617  */
618 void
619 determine_platform(void)
620 {
621 	struct cpuid_regs cp;
622 	uint32_t base;
623 	uint32_t regs[4];
624 	char *hvstr = (char *)regs;
625 
626 	ASSERT(platform_type == -1);
627 
628 	platform_type = HW_NATIVE;
629 
630 	if (!enable_platform_detection)
631 		return;
632 
633 	/*
634 	 * If Hypervisor CPUID bit is set, try to determine hypervisor
635 	 * vendor signature, and set platform type accordingly.
636 	 *
637 	 * References:
638 	 * http://lkml.org/lkml/2008/10/1/246
639 	 * http://kb.vmware.com/kb/1009458
640 	 */
641 	cp.cp_eax = 0x1;
642 	(void) __cpuid_insn(&cp);
643 	if ((cp.cp_ecx & CPUID_INTC_ECX_HV) != 0) {
644 		cp.cp_eax = 0x40000000;
645 		(void) __cpuid_insn(&cp);
646 		regs[0] = cp.cp_ebx;
647 		regs[1] = cp.cp_ecx;
648 		regs[2] = cp.cp_edx;
649 		regs[3] = 0;
650 		if (strcmp(hvstr, HVSIG_XEN_HVM) == 0) {
651 			platform_type = HW_XEN_HVM;
652 			return;
653 		}
654 		if (strcmp(hvstr, HVSIG_VMWARE) == 0) {
655 			platform_type = HW_VMWARE;
656 			return;
657 		}
658 		if (strcmp(hvstr, HVSIG_KVM) == 0) {
659 			platform_type = HW_KVM;
660 			return;
661 		}
662 		if (strcmp(hvstr, HVSIG_MICROSOFT) == 0)
663 			platform_type = HW_MICROSOFT;
664 	} else {
665 		/*
666 		 * Check older VMware hardware versions. VMware hypervisor is
667 		 * detected by performing an IN operation to VMware hypervisor
668 		 * port and checking that value returned in %ebx is VMware
669 		 * hypervisor magic value.
670 		 *
671 		 * References: http://kb.vmware.com/kb/1009458
672 		 */
673 		vmware_port(VMWARE_HVCMD_GETVERSION, regs);
674 		if (regs[1] == VMWARE_HVMAGIC) {
675 			platform_type = HW_VMWARE;
676 			return;
677 		}
678 	}
679 
680 	/*
681 	 * Check Xen hypervisor. In a fully virtualized domain,
682 	 * Xen's pseudo-cpuid function returns a string representing the
683 	 * Xen signature in %ebx, %ecx, and %edx. %eax contains the maximum
684 	 * supported cpuid function. We need at least a (base + 2) leaf value
685 	 * to do what we want to do. Try different base values, since the
686 	 * hypervisor might use a different one depending on whether Hyper-V
687 	 * emulation is switched on by default or not.
688 	 */
689 	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
690 		cp.cp_eax = base;
691 		(void) __cpuid_insn(&cp);
692 		regs[0] = cp.cp_ebx;
693 		regs[1] = cp.cp_ecx;
694 		regs[2] = cp.cp_edx;
695 		regs[3] = 0;
696 		if (strcmp(hvstr, HVSIG_XEN_HVM) == 0 &&
697 		    cp.cp_eax >= (base + 2)) {
698 			platform_type &= ~HW_NATIVE;
699 			platform_type |= HW_XEN_HVM;
700 			return;
701 		}
702 	}
703 }
704 
705 int
706 get_hwenv(void)
707 {
708 	ASSERT(platform_type != -1);
709 	return (platform_type);
710 }
711 
712 int
713 is_controldom(void)
714 {
715 	return (0);
716 }
717 
718 #else
719 
720 int
721 get_hwenv(void)
722 {
723 	return (HW_XEN_PV);
724 }
725 
726 int
727 is_controldom(void)
728 {
729 	return (DOMAIN_IS_INITDOMAIN(xen_info));
730 }
731 
732 #endif	/* __xpv */
733 
734 static void
735 cpuid_intel_getids(cpu_t *cpu, void *feature)
736 {
737 	uint_t i;
738 	uint_t chipid_shift = 0;
739 	uint_t coreid_shift = 0;
740 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
741 
742 	for (i = 1; i < cpi->cpi_ncpu_per_chip; i <<= 1)
743 		chipid_shift++;
744 
745 	cpi->cpi_chipid = cpi->cpi_apicid >> chipid_shift;
746 	cpi->cpi_clogid = cpi->cpi_apicid & ((1 << chipid_shift) - 1);
747 
748 	if (is_x86_feature(feature, X86FSET_CMP)) {
749 		/*
750 		 * Multi-core (and possibly multi-threaded)
751 		 * processors.
752 		 */
753 		uint_t ncpu_per_core;
754 		if (cpi->cpi_ncore_per_chip == 1)
755 			ncpu_per_core = cpi->cpi_ncpu_per_chip;
756 		else if (cpi->cpi_ncore_per_chip > 1)
757 			ncpu_per_core = cpi->cpi_ncpu_per_chip /
758 			    cpi->cpi_ncore_per_chip;
759 		/*
760 		 * 8bit APIC IDs on dual core Pentiums
761 		 * look like this:
762 		 *
763 		 * +-----------------------+------+------+
764 		 * | Physical Package ID   |  MC  |  HT  |
765 		 * +-----------------------+------+------+
766 		 * <------- chipid -------->
767 		 * <------- coreid --------------->
768 		 *			   <--- clogid -->
769 		 *			   <------>
770 		 *			   pkgcoreid
771 		 *
772 		 * Where the number of bits necessary to
773 		 * represent MC and HT fields together equals
774 		 * to the minimum number of bits necessary to
775 		 * store the value of cpi->cpi_ncpu_per_chip.
776 		 * Of those bits, the MC part uses the number
777 		 * of bits necessary to store the value of
778 		 * cpi->cpi_ncore_per_chip.
779 		 */
780 		for (i = 1; i < ncpu_per_core; i <<= 1)
781 			coreid_shift++;
782 		cpi->cpi_coreid = cpi->cpi_apicid >> coreid_shift;
783 		cpi->cpi_pkgcoreid = cpi->cpi_clogid >> coreid_shift;
784 	} else if (is_x86_feature(feature, X86FSET_HTT)) {
785 		/*
786 		 * Single-core multi-threaded processors.
787 		 */
788 		cpi->cpi_coreid = cpi->cpi_chipid;
789 		cpi->cpi_pkgcoreid = 0;
790 	}
791 	cpi->cpi_procnodeid = cpi->cpi_chipid;
792 	cpi->cpi_compunitid = cpi->cpi_coreid;
793 }
794 
795 static void
796 cpuid_amd_getids(cpu_t *cpu)
797 {
798 	int i, first_half, coreidsz;
799 	uint32_t nb_caps_reg;
800 	uint_t node2_1;
801 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
802 	struct cpuid_regs *cp;
803 
804 	/*
805 	 * AMD CMP chips currently have a single thread per core.
806 	 *
807 	 * Since no two cpus share a core we must assign a distinct coreid
808 	 * per cpu, and we do this by using the cpu_id.  This scheme does not,
809 	 * however, guarantee that sibling cores of a chip will have sequential
810 	 * coreids starting at a multiple of the number of cores per chip -
811 	 * that is usually the case, but if the ACPI MADT table is presented
812 	 * in a different order then we need to perform a few more gymnastics
813 	 * for the pkgcoreid.
814 	 *
815 	 * All processors in the system have the same number of enabled
816 	 * cores. Cores within a processor are always numbered sequentially
817 	 * from 0 regardless of how many or which are disabled, and there
818 	 * is no way for operating system to discover the real core id when some
819 	 * are disabled.
820 	 *
821 	 * In family 0x15, the cores come in pairs called compute units. They
822 	 * share I$ and L2 caches and the FPU. Enumeration of this feature is
823 	 * simplified by the new topology extensions CPUID leaf, indicated by
824 	 * the X86 feature X86FSET_TOPOEXT.
825 	 */
826 
827 	cpi->cpi_coreid = cpu->cpu_id;
828 	cpi->cpi_compunitid = cpu->cpu_id;
829 
830 	if (cpi->cpi_xmaxeax >= 0x80000008) {
831 
832 		coreidsz = BITX((cpi)->cpi_extd[8].cp_ecx, 15, 12);
833 
834 		/*
835 		 * In AMD parlance chip is really a node while Solaris
836 		 * sees chip as equivalent to socket/package.
837 		 */
838 		cpi->cpi_ncore_per_chip =
839 		    BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
840 		if (coreidsz == 0) {
841 			/* Use legacy method */
842 			for (i = 1; i < cpi->cpi_ncore_per_chip; i <<= 1)
843 				coreidsz++;
844 			if (coreidsz == 0)
845 				coreidsz = 1;
846 		}
847 	} else {
848 		/* Assume single-core part */
849 		cpi->cpi_ncore_per_chip = 1;
850 		coreidsz = 1;
851 	}
852 
853 	cpi->cpi_clogid = cpi->cpi_pkgcoreid =
854 	    cpi->cpi_apicid & ((1<<coreidsz) - 1);
855 	cpi->cpi_ncpu_per_chip = cpi->cpi_ncore_per_chip;
856 
857 	/* Get node ID, compute unit ID */
858 	if (is_x86_feature(x86_featureset, X86FSET_TOPOEXT) &&
859 	    cpi->cpi_xmaxeax >= 0x8000001e) {
860 		cp = &cpi->cpi_extd[0x1e];
861 		cp->cp_eax = 0x8000001e;
862 		(void) __cpuid_insn(cp);
863 
864 		cpi->cpi_procnodes_per_pkg = BITX(cp->cp_ecx, 10, 8) + 1;
865 		cpi->cpi_procnodeid = BITX(cp->cp_ecx, 7, 0);
866 		cpi->cpi_cores_per_compunit = BITX(cp->cp_ebx, 15, 8) + 1;
867 		cpi->cpi_compunitid = BITX(cp->cp_ebx, 7, 0)
868 		    + (cpi->cpi_ncore_per_chip / cpi->cpi_cores_per_compunit)
869 		    * (cpi->cpi_procnodeid / cpi->cpi_procnodes_per_pkg);
870 	} else if (cpi->cpi_family == 0xf || cpi->cpi_family >= 0x11) {
871 		cpi->cpi_procnodeid = (cpi->cpi_apicid >> coreidsz) & 7;
872 	} else if (cpi->cpi_family == 0x10) {
873 		/*
874 		 * See if we are a multi-node processor.
875 		 * All processors in the system have the same number of nodes
876 		 */
877 		nb_caps_reg =  pci_getl_func(0, 24, 3, 0xe8);
878 		if ((cpi->cpi_model < 8) || BITX(nb_caps_reg, 29, 29) == 0) {
879 			/* Single-node */
880 			cpi->cpi_procnodeid = BITX(cpi->cpi_apicid, 5,
881 			    coreidsz);
882 		} else {
883 
884 			/*
885 			 * Multi-node revision D (2 nodes per package
886 			 * are supported)
887 			 */
888 			cpi->cpi_procnodes_per_pkg = 2;
889 
890 			first_half = (cpi->cpi_pkgcoreid <=
891 			    (cpi->cpi_ncore_per_chip/2 - 1));
892 
893 			if (cpi->cpi_apicid == cpi->cpi_pkgcoreid) {
894 				/* We are BSP */
895 				cpi->cpi_procnodeid = (first_half ? 0 : 1);
896 			} else {
897 
898 				/* We are AP */
899 				/* NodeId[2:1] bits to use for reading F3xe8 */
900 				node2_1 = BITX(cpi->cpi_apicid, 5, 4) << 1;
901 
902 				nb_caps_reg =
903 				    pci_getl_func(0, 24 + node2_1, 3, 0xe8);
904 
905 				/*
906 				 * Check IntNodeNum bit (31:30, but bit 31 is
907 				 * always 0 on dual-node processors)
908 				 */
909 				if (BITX(nb_caps_reg, 30, 30) == 0)
910 					cpi->cpi_procnodeid = node2_1 +
911 					    !first_half;
912 				else
913 					cpi->cpi_procnodeid = node2_1 +
914 					    first_half;
915 			}
916 		}
917 	} else {
918 		cpi->cpi_procnodeid = 0;
919 	}
920 
921 	cpi->cpi_chipid =
922 	    cpi->cpi_procnodeid / cpi->cpi_procnodes_per_pkg;
923 }
924 
925 /*
926  * Setup XFeature_Enabled_Mask register. Required by xsave feature.
927  */
928 void
929 setup_xfem(void)
930 {
931 	uint64_t flags = XFEATURE_LEGACY_FP;
932 
933 	ASSERT(is_x86_feature(x86_featureset, X86FSET_XSAVE));
934 
935 	if (is_x86_feature(x86_featureset, X86FSET_SSE))
936 		flags |= XFEATURE_SSE;
937 
938 	if (is_x86_feature(x86_featureset, X86FSET_AVX))
939 		flags |= XFEATURE_AVX;
940 
941 	set_xcr(XFEATURE_ENABLED_MASK, flags);
942 
943 	xsave_bv_all = flags;
944 }
945 
946 void
947 cpuid_pass1(cpu_t *cpu, uchar_t *featureset)
948 {
949 	uint32_t mask_ecx, mask_edx;
950 	struct cpuid_info *cpi;
951 	struct cpuid_regs *cp;
952 	int xcpuid;
953 #if !defined(__xpv)
954 	extern int idle_cpu_prefer_mwait;
955 #endif
956 
957 	/*
958 	 * Space statically allocated for BSP, ensure pointer is set
959 	 */
960 	if (cpu->cpu_id == 0) {
961 		if (cpu->cpu_m.mcpu_cpi == NULL)
962 			cpu->cpu_m.mcpu_cpi = &cpuid_info0;
963 	}
964 
965 	add_x86_feature(featureset, X86FSET_CPUID);
966 
967 	cpi = cpu->cpu_m.mcpu_cpi;
968 	ASSERT(cpi != NULL);
969 	cp = &cpi->cpi_std[0];
970 	cp->cp_eax = 0;
971 	cpi->cpi_maxeax = __cpuid_insn(cp);
972 	{
973 		uint32_t *iptr = (uint32_t *)cpi->cpi_vendorstr;
974 		*iptr++ = cp->cp_ebx;
975 		*iptr++ = cp->cp_edx;
976 		*iptr++ = cp->cp_ecx;
977 		*(char *)&cpi->cpi_vendorstr[12] = '\0';
978 	}
979 
980 	cpi->cpi_vendor = _cpuid_vendorstr_to_vendorcode(cpi->cpi_vendorstr);
981 	x86_vendor = cpi->cpi_vendor; /* for compatibility */
982 
983 	/*
984 	 * Limit the range in case of weird hardware
985 	 */
986 	if (cpi->cpi_maxeax > CPI_MAXEAX_MAX)
987 		cpi->cpi_maxeax = CPI_MAXEAX_MAX;
988 	if (cpi->cpi_maxeax < 1)
989 		goto pass1_done;
990 
991 	cp = &cpi->cpi_std[1];
992 	cp->cp_eax = 1;
993 	(void) __cpuid_insn(cp);
994 
995 	/*
996 	 * Extract identifying constants for easy access.
997 	 */
998 	cpi->cpi_model = CPI_MODEL(cpi);
999 	cpi->cpi_family = CPI_FAMILY(cpi);
1000 
1001 	if (cpi->cpi_family == 0xf)
1002 		cpi->cpi_family += CPI_FAMILY_XTD(cpi);
1003 
1004 	/*
1005 	 * Beware: AMD uses "extended model" iff base *FAMILY* == 0xf.
1006 	 * Intel, and presumably everyone else, uses model == 0xf, as
1007 	 * one would expect (max value means possible overflow).  Sigh.
1008 	 */
1009 
1010 	switch (cpi->cpi_vendor) {
1011 	case X86_VENDOR_Intel:
1012 		if (IS_EXTENDED_MODEL_INTEL(cpi))
1013 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
1014 		break;
1015 	case X86_VENDOR_AMD:
1016 		if (CPI_FAMILY(cpi) == 0xf)
1017 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
1018 		break;
1019 	default:
1020 		if (cpi->cpi_model == 0xf)
1021 			cpi->cpi_model += CPI_MODEL_XTD(cpi) << 4;
1022 		break;
1023 	}
1024 
1025 	cpi->cpi_step = CPI_STEP(cpi);
1026 	cpi->cpi_brandid = CPI_BRANDID(cpi);
1027 
1028 	/*
1029 	 * *default* assumptions:
1030 	 * - believe %edx feature word
1031 	 * - ignore %ecx feature word
1032 	 * - 32-bit virtual and physical addressing
1033 	 */
1034 	mask_edx = 0xffffffff;
1035 	mask_ecx = 0;
1036 
1037 	cpi->cpi_pabits = cpi->cpi_vabits = 32;
1038 
1039 	switch (cpi->cpi_vendor) {
1040 	case X86_VENDOR_Intel:
1041 		if (cpi->cpi_family == 5)
1042 			x86_type = X86_TYPE_P5;
1043 		else if (IS_LEGACY_P6(cpi)) {
1044 			x86_type = X86_TYPE_P6;
1045 			pentiumpro_bug4046376 = 1;
1046 			/*
1047 			 * Clear the SEP bit when it was set erroneously
1048 			 */
1049 			if (cpi->cpi_model < 3 && cpi->cpi_step < 3)
1050 				cp->cp_edx &= ~CPUID_INTC_EDX_SEP;
1051 		} else if (IS_NEW_F6(cpi) || cpi->cpi_family == 0xf) {
1052 			x86_type = X86_TYPE_P4;
1053 			/*
1054 			 * We don't currently depend on any of the %ecx
1055 			 * features until Prescott, so we'll only check
1056 			 * this from P4 onwards.  We might want to revisit
1057 			 * that idea later.
1058 			 */
1059 			mask_ecx = 0xffffffff;
1060 		} else if (cpi->cpi_family > 0xf)
1061 			mask_ecx = 0xffffffff;
1062 		/*
1063 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
1064 		 * to obtain the monitor linesize.
1065 		 */
1066 		if (cpi->cpi_maxeax < 5)
1067 			mask_ecx &= ~CPUID_INTC_ECX_MON;
1068 		break;
1069 	case X86_VENDOR_IntelClone:
1070 	default:
1071 		break;
1072 	case X86_VENDOR_AMD:
1073 #if defined(OPTERON_ERRATUM_108)
1074 		if (cpi->cpi_family == 0xf && cpi->cpi_model == 0xe) {
1075 			cp->cp_eax = (0xf0f & cp->cp_eax) | 0xc0;
1076 			cpi->cpi_model = 0xc;
1077 		} else
1078 #endif
1079 		if (cpi->cpi_family == 5) {
1080 			/*
1081 			 * AMD K5 and K6
1082 			 *
1083 			 * These CPUs have an incomplete implementation
1084 			 * of MCA/MCE which we mask away.
1085 			 */
1086 			mask_edx &= ~(CPUID_INTC_EDX_MCE | CPUID_INTC_EDX_MCA);
1087 
1088 			/*
1089 			 * Model 0 uses the wrong (APIC) bit
1090 			 * to indicate PGE.  Fix it here.
1091 			 */
1092 			if (cpi->cpi_model == 0) {
1093 				if (cp->cp_edx & 0x200) {
1094 					cp->cp_edx &= ~0x200;
1095 					cp->cp_edx |= CPUID_INTC_EDX_PGE;
1096 				}
1097 			}
1098 
1099 			/*
1100 			 * Early models had problems w/ MMX; disable.
1101 			 */
1102 			if (cpi->cpi_model < 6)
1103 				mask_edx &= ~CPUID_INTC_EDX_MMX;
1104 		}
1105 
1106 		/*
1107 		 * For newer families, SSE3 and CX16, at least, are valid;
1108 		 * enable all
1109 		 */
1110 		if (cpi->cpi_family >= 0xf)
1111 			mask_ecx = 0xffffffff;
1112 		/*
1113 		 * We don't support MONITOR/MWAIT if leaf 5 is not available
1114 		 * to obtain the monitor linesize.
1115 		 */
1116 		if (cpi->cpi_maxeax < 5)
1117 			mask_ecx &= ~CPUID_INTC_ECX_MON;
1118 
1119 #if !defined(__xpv)
1120 		/*
1121 		 * Do not use MONITOR/MWAIT to halt in the idle loop on any AMD
1122 		 * processors.  AMD does not intend MWAIT to be used in the cpu
1123 		 * idle loop on current and future processors.  10h and future
1124 		 * AMD processors use more power in MWAIT than HLT.
1125 		 * Pre-family-10h Opterons do not have the MWAIT instruction.
1126 		 */
1127 		idle_cpu_prefer_mwait = 0;
1128 #endif
1129 
1130 		break;
1131 	case X86_VENDOR_TM:
1132 		/*
1133 		 * workaround the NT workaround in CMS 4.1
1134 		 */
1135 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4 &&
1136 		    (cpi->cpi_step == 2 || cpi->cpi_step == 3))
1137 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
1138 		break;
1139 	case X86_VENDOR_Centaur:
1140 		/*
1141 		 * workaround the NT workarounds again
1142 		 */
1143 		if (cpi->cpi_family == 6)
1144 			cp->cp_edx |= CPUID_INTC_EDX_CX8;
1145 		break;
1146 	case X86_VENDOR_Cyrix:
1147 		/*
1148 		 * We rely heavily on the probing in locore
1149 		 * to actually figure out what parts, if any,
1150 		 * of the Cyrix cpuid instruction to believe.
1151 		 */
1152 		switch (x86_type) {
1153 		case X86_TYPE_CYRIX_486:
1154 			mask_edx = 0;
1155 			break;
1156 		case X86_TYPE_CYRIX_6x86:
1157 			mask_edx = 0;
1158 			break;
1159 		case X86_TYPE_CYRIX_6x86L:
1160 			mask_edx =
1161 			    CPUID_INTC_EDX_DE |
1162 			    CPUID_INTC_EDX_CX8;
1163 			break;
1164 		case X86_TYPE_CYRIX_6x86MX:
1165 			mask_edx =
1166 			    CPUID_INTC_EDX_DE |
1167 			    CPUID_INTC_EDX_MSR |
1168 			    CPUID_INTC_EDX_CX8 |
1169 			    CPUID_INTC_EDX_PGE |
1170 			    CPUID_INTC_EDX_CMOV |
1171 			    CPUID_INTC_EDX_MMX;
1172 			break;
1173 		case X86_TYPE_CYRIX_GXm:
1174 			mask_edx =
1175 			    CPUID_INTC_EDX_MSR |
1176 			    CPUID_INTC_EDX_CX8 |
1177 			    CPUID_INTC_EDX_CMOV |
1178 			    CPUID_INTC_EDX_MMX;
1179 			break;
1180 		case X86_TYPE_CYRIX_MediaGX:
1181 			break;
1182 		case X86_TYPE_CYRIX_MII:
1183 		case X86_TYPE_VIA_CYRIX_III:
1184 			mask_edx =
1185 			    CPUID_INTC_EDX_DE |
1186 			    CPUID_INTC_EDX_TSC |
1187 			    CPUID_INTC_EDX_MSR |
1188 			    CPUID_INTC_EDX_CX8 |
1189 			    CPUID_INTC_EDX_PGE |
1190 			    CPUID_INTC_EDX_CMOV |
1191 			    CPUID_INTC_EDX_MMX;
1192 			break;
1193 		default:
1194 			break;
1195 		}
1196 		break;
1197 	}
1198 
1199 #if defined(__xpv)
1200 	/*
1201 	 * Do not support MONITOR/MWAIT under a hypervisor
1202 	 */
1203 	mask_ecx &= ~CPUID_INTC_ECX_MON;
1204 	/*
1205 	 * Do not support XSAVE under a hypervisor for now
1206 	 */
1207 	xsave_force_disable = B_TRUE;
1208 
1209 #endif	/* __xpv */
1210 
1211 	if (xsave_force_disable) {
1212 		mask_ecx &= ~CPUID_INTC_ECX_XSAVE;
1213 		mask_ecx &= ~CPUID_INTC_ECX_AVX;
1214 		mask_ecx &= ~CPUID_INTC_ECX_F16C;
1215 		mask_ecx &= ~CPUID_INTC_ECX_FMA;
1216 	}
1217 
1218 	/*
1219 	 * Now we've figured out the masks that determine
1220 	 * which bits we choose to believe, apply the masks
1221 	 * to the feature words, then map the kernel's view
1222 	 * of these feature words into its feature word.
1223 	 */
1224 	cp->cp_edx &= mask_edx;
1225 	cp->cp_ecx &= mask_ecx;
1226 
1227 	/*
1228 	 * apply any platform restrictions (we don't call this
1229 	 * immediately after __cpuid_insn here, because we need the
1230 	 * workarounds applied above first)
1231 	 */
1232 	platform_cpuid_mangle(cpi->cpi_vendor, 1, cp);
1233 
1234 	/*
1235 	 * In addition to ecx and edx, Intel is storing a bunch of instruction
1236 	 * set extensions in leaf 7's ebx.
1237 	 */
1238 	if (cpi->cpi_vendor == X86_VENDOR_Intel && cpi->cpi_maxeax >= 7) {
1239 		struct cpuid_regs *ecp;
1240 		ecp = &cpi->cpi_std[7];
1241 		ecp->cp_eax = 7;
1242 		ecp->cp_ecx = 0;
1243 		(void) __cpuid_insn(ecp);
1244 		/*
1245 		 * If XSAVE has been disabled, just ignore all of the AVX
1246 		 * dependent flags here.
1247 		 */
1248 		if (xsave_force_disable) {
1249 			ecp->cp_ebx &= ~CPUID_INTC_EBX_7_0_BMI1;
1250 			ecp->cp_ebx &= ~CPUID_INTC_EBX_7_0_BMI2;
1251 			ecp->cp_ebx &= ~CPUID_INTC_EBX_7_0_AVX2;
1252 		}
1253 
1254 		if (ecp->cp_ebx & CPUID_INTC_EBX_7_0_SMEP)
1255 			add_x86_feature(featureset, X86FSET_SMEP);
1256 
1257 		if (ecp->cp_ebx & CPUID_INTC_EBX_7_0_RDSEED)
1258 			add_x86_feature(featureset, X86FSET_RDSEED);
1259 
1260 		if (ecp->cp_ebx & CPUID_INTC_EBX_7_0_ADX)
1261 			add_x86_feature(featureset, X86FSET_ADX);
1262 	}
1263 
1264 	/*
1265 	 * fold in overrides from the "eeprom" mechanism
1266 	 */
1267 	cp->cp_edx |= cpuid_feature_edx_include;
1268 	cp->cp_edx &= ~cpuid_feature_edx_exclude;
1269 
1270 	cp->cp_ecx |= cpuid_feature_ecx_include;
1271 	cp->cp_ecx &= ~cpuid_feature_ecx_exclude;
1272 
1273 	if (cp->cp_edx & CPUID_INTC_EDX_PSE) {
1274 		add_x86_feature(featureset, X86FSET_LARGEPAGE);
1275 	}
1276 	if (cp->cp_edx & CPUID_INTC_EDX_TSC) {
1277 		add_x86_feature(featureset, X86FSET_TSC);
1278 	}
1279 	if (cp->cp_edx & CPUID_INTC_EDX_MSR) {
1280 		add_x86_feature(featureset, X86FSET_MSR);
1281 	}
1282 	if (cp->cp_edx & CPUID_INTC_EDX_MTRR) {
1283 		add_x86_feature(featureset, X86FSET_MTRR);
1284 	}
1285 	if (cp->cp_edx & CPUID_INTC_EDX_PGE) {
1286 		add_x86_feature(featureset, X86FSET_PGE);
1287 	}
1288 	if (cp->cp_edx & CPUID_INTC_EDX_CMOV) {
1289 		add_x86_feature(featureset, X86FSET_CMOV);
1290 	}
1291 	if (cp->cp_edx & CPUID_INTC_EDX_MMX) {
1292 		add_x86_feature(featureset, X86FSET_MMX);
1293 	}
1294 	if ((cp->cp_edx & CPUID_INTC_EDX_MCE) != 0 &&
1295 	    (cp->cp_edx & CPUID_INTC_EDX_MCA) != 0) {
1296 		add_x86_feature(featureset, X86FSET_MCA);
1297 	}
1298 	if (cp->cp_edx & CPUID_INTC_EDX_PAE) {
1299 		add_x86_feature(featureset, X86FSET_PAE);
1300 	}
1301 	if (cp->cp_edx & CPUID_INTC_EDX_CX8) {
1302 		add_x86_feature(featureset, X86FSET_CX8);
1303 	}
1304 	if (cp->cp_ecx & CPUID_INTC_ECX_CX16) {
1305 		add_x86_feature(featureset, X86FSET_CX16);
1306 	}
1307 	if (cp->cp_edx & CPUID_INTC_EDX_PAT) {
1308 		add_x86_feature(featureset, X86FSET_PAT);
1309 	}
1310 	if (cp->cp_edx & CPUID_INTC_EDX_SEP) {
1311 		add_x86_feature(featureset, X86FSET_SEP);
1312 	}
1313 	if (cp->cp_edx & CPUID_INTC_EDX_FXSR) {
1314 		/*
1315 		 * In our implementation, fxsave/fxrstor
1316 		 * are prerequisites before we'll even
1317 		 * try and do SSE things.
1318 		 */
1319 		if (cp->cp_edx & CPUID_INTC_EDX_SSE) {
1320 			add_x86_feature(featureset, X86FSET_SSE);
1321 		}
1322 		if (cp->cp_edx & CPUID_INTC_EDX_SSE2) {
1323 			add_x86_feature(featureset, X86FSET_SSE2);
1324 		}
1325 		if (cp->cp_ecx & CPUID_INTC_ECX_SSE3) {
1326 			add_x86_feature(featureset, X86FSET_SSE3);
1327 		}
1328 		if (cp->cp_ecx & CPUID_INTC_ECX_SSSE3) {
1329 			add_x86_feature(featureset, X86FSET_SSSE3);
1330 		}
1331 		if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_1) {
1332 			add_x86_feature(featureset, X86FSET_SSE4_1);
1333 		}
1334 		if (cp->cp_ecx & CPUID_INTC_ECX_SSE4_2) {
1335 			add_x86_feature(featureset, X86FSET_SSE4_2);
1336 		}
1337 		if (cp->cp_ecx & CPUID_INTC_ECX_AES) {
1338 			add_x86_feature(featureset, X86FSET_AES);
1339 		}
1340 		if (cp->cp_ecx & CPUID_INTC_ECX_PCLMULQDQ) {
1341 			add_x86_feature(featureset, X86FSET_PCLMULQDQ);
1342 		}
1343 
1344 		if (cp->cp_ecx & CPUID_INTC_ECX_XSAVE) {
1345 			add_x86_feature(featureset, X86FSET_XSAVE);
1346 
1347 			/* We only test AVX when there is XSAVE */
1348 			if (cp->cp_ecx & CPUID_INTC_ECX_AVX) {
1349 				add_x86_feature(featureset,
1350 				    X86FSET_AVX);
1351 
1352 				/*
1353 				 * Intel says we can't check these without also
1354 				 * checking AVX.
1355 				 */
1356 				if (cp->cp_ecx & CPUID_INTC_ECX_F16C)
1357 					add_x86_feature(featureset,
1358 					    X86FSET_F16C);
1359 
1360 				if (cp->cp_ecx & CPUID_INTC_ECX_FMA)
1361 					add_x86_feature(featureset,
1362 					    X86FSET_FMA);
1363 
1364 				if (cpi->cpi_std[7].cp_ebx &
1365 				    CPUID_INTC_EBX_7_0_BMI1)
1366 					add_x86_feature(featureset,
1367 					    X86FSET_BMI1);
1368 
1369 				if (cpi->cpi_std[7].cp_ebx &
1370 				    CPUID_INTC_EBX_7_0_BMI2)
1371 					add_x86_feature(featureset,
1372 					    X86FSET_BMI2);
1373 
1374 				if (cpi->cpi_std[7].cp_ebx &
1375 				    CPUID_INTC_EBX_7_0_AVX2)
1376 					add_x86_feature(featureset,
1377 					    X86FSET_AVX2);
1378 			}
1379 		}
1380 	}
1381 	if (cp->cp_ecx & CPUID_INTC_ECX_X2APIC) {
1382 		add_x86_feature(featureset, X86FSET_X2APIC);
1383 	}
1384 	if (cp->cp_edx & CPUID_INTC_EDX_DE) {
1385 		add_x86_feature(featureset, X86FSET_DE);
1386 	}
1387 #if !defined(__xpv)
1388 	if (cp->cp_ecx & CPUID_INTC_ECX_MON) {
1389 
1390 		/*
1391 		 * We require the CLFLUSH instruction for erratum workaround
1392 		 * to use MONITOR/MWAIT.
1393 		 */
1394 		if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
1395 			cpi->cpi_mwait.support |= MWAIT_SUPPORT;
1396 			add_x86_feature(featureset, X86FSET_MWAIT);
1397 		} else {
1398 			extern int idle_cpu_assert_cflush_monitor;
1399 
1400 			/*
1401 			 * All processors we are aware of which have
1402 			 * MONITOR/MWAIT also have CLFLUSH.
1403 			 */
1404 			if (idle_cpu_assert_cflush_monitor) {
1405 				ASSERT((cp->cp_ecx & CPUID_INTC_ECX_MON) &&
1406 				    (cp->cp_edx & CPUID_INTC_EDX_CLFSH));
1407 			}
1408 		}
1409 	}
1410 #endif	/* __xpv */
1411 
1412 	if (cp->cp_ecx & CPUID_INTC_ECX_VMX) {
1413 		add_x86_feature(featureset, X86FSET_VMX);
1414 	}
1415 
1416 	if (cp->cp_ecx & CPUID_INTC_ECX_RDRAND)
1417 		add_x86_feature(featureset, X86FSET_RDRAND);
1418 
1419 	/*
1420 	 * Only need it first time, rest of the cpus would follow suit.
1421 	 * we only capture this for the bootcpu.
1422 	 */
1423 	if (cp->cp_edx & CPUID_INTC_EDX_CLFSH) {
1424 		add_x86_feature(featureset, X86FSET_CLFSH);
1425 		x86_clflush_size = (BITX(cp->cp_ebx, 15, 8) * 8);
1426 	}
1427 	if (is_x86_feature(featureset, X86FSET_PAE))
1428 		cpi->cpi_pabits = 36;
1429 
1430 	/*
1431 	 * Hyperthreading configuration is slightly tricky on Intel
1432 	 * and pure clones, and even trickier on AMD.
1433 	 *
1434 	 * (AMD chose to set the HTT bit on their CMP processors,
1435 	 * even though they're not actually hyperthreaded.  Thus it
1436 	 * takes a bit more work to figure out what's really going
1437 	 * on ... see the handling of the CMP_LGCY bit below)
1438 	 */
1439 	if (cp->cp_edx & CPUID_INTC_EDX_HTT) {
1440 		cpi->cpi_ncpu_per_chip = CPI_CPU_COUNT(cpi);
1441 		if (cpi->cpi_ncpu_per_chip > 1)
1442 			add_x86_feature(featureset, X86FSET_HTT);
1443 	} else {
1444 		cpi->cpi_ncpu_per_chip = 1;
1445 	}
1446 
1447 	/*
1448 	 * Work on the "extended" feature information, doing
1449 	 * some basic initialization for cpuid_pass2()
1450 	 */
1451 	xcpuid = 0;
1452 	switch (cpi->cpi_vendor) {
1453 	case X86_VENDOR_Intel:
1454 		if (IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf)
1455 			xcpuid++;
1456 		break;
1457 	case X86_VENDOR_AMD:
1458 		if (cpi->cpi_family > 5 ||
1459 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
1460 			xcpuid++;
1461 		break;
1462 	case X86_VENDOR_Cyrix:
1463 		/*
1464 		 * Only these Cyrix CPUs are -known- to support
1465 		 * extended cpuid operations.
1466 		 */
1467 		if (x86_type == X86_TYPE_VIA_CYRIX_III ||
1468 		    x86_type == X86_TYPE_CYRIX_GXm)
1469 			xcpuid++;
1470 		break;
1471 	case X86_VENDOR_Centaur:
1472 	case X86_VENDOR_TM:
1473 	default:
1474 		xcpuid++;
1475 		break;
1476 	}
1477 
1478 	if (xcpuid) {
1479 		cp = &cpi->cpi_extd[0];
1480 		cp->cp_eax = 0x80000000;
1481 		cpi->cpi_xmaxeax = __cpuid_insn(cp);
1482 	}
1483 
1484 	if (cpi->cpi_xmaxeax & 0x80000000) {
1485 
1486 		if (cpi->cpi_xmaxeax > CPI_XMAXEAX_MAX)
1487 			cpi->cpi_xmaxeax = CPI_XMAXEAX_MAX;
1488 
1489 		switch (cpi->cpi_vendor) {
1490 		case X86_VENDOR_Intel:
1491 		case X86_VENDOR_AMD:
1492 			if (cpi->cpi_xmaxeax < 0x80000001)
1493 				break;
1494 			cp = &cpi->cpi_extd[1];
1495 			cp->cp_eax = 0x80000001;
1496 			(void) __cpuid_insn(cp);
1497 
1498 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
1499 			    cpi->cpi_family == 5 &&
1500 			    cpi->cpi_model == 6 &&
1501 			    cpi->cpi_step == 6) {
1502 				/*
1503 				 * K6 model 6 uses bit 10 to indicate SYSC
1504 				 * Later models use bit 11. Fix it here.
1505 				 */
1506 				if (cp->cp_edx & 0x400) {
1507 					cp->cp_edx &= ~0x400;
1508 					cp->cp_edx |= CPUID_AMD_EDX_SYSC;
1509 				}
1510 			}
1511 
1512 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000001, cp);
1513 
1514 			/*
1515 			 * Compute the additions to the kernel's feature word.
1516 			 */
1517 			if (cp->cp_edx & CPUID_AMD_EDX_NX) {
1518 				add_x86_feature(featureset, X86FSET_NX);
1519 			}
1520 
1521 			/*
1522 			 * Regardless whether or not we boot 64-bit,
1523 			 * we should have a way to identify whether
1524 			 * the CPU is capable of running 64-bit.
1525 			 */
1526 			if (cp->cp_edx & CPUID_AMD_EDX_LM) {
1527 				add_x86_feature(featureset, X86FSET_64);
1528 			}
1529 
1530 #if defined(__amd64)
1531 			/* 1 GB large page - enable only for 64 bit kernel */
1532 			if (cp->cp_edx & CPUID_AMD_EDX_1GPG) {
1533 				add_x86_feature(featureset, X86FSET_1GPG);
1534 			}
1535 #endif
1536 
1537 			if ((cpi->cpi_vendor == X86_VENDOR_AMD) &&
1538 			    (cpi->cpi_std[1].cp_edx & CPUID_INTC_EDX_FXSR) &&
1539 			    (cp->cp_ecx & CPUID_AMD_ECX_SSE4A)) {
1540 				add_x86_feature(featureset, X86FSET_SSE4A);
1541 			}
1542 
1543 			/*
1544 			 * If both the HTT and CMP_LGCY bits are set,
1545 			 * then we're not actually HyperThreaded.  Read
1546 			 * "AMD CPUID Specification" for more details.
1547 			 */
1548 			if (cpi->cpi_vendor == X86_VENDOR_AMD &&
1549 			    is_x86_feature(featureset, X86FSET_HTT) &&
1550 			    (cp->cp_ecx & CPUID_AMD_ECX_CMP_LGCY)) {
1551 				remove_x86_feature(featureset, X86FSET_HTT);
1552 				add_x86_feature(featureset, X86FSET_CMP);
1553 			}
1554 #if defined(__amd64)
1555 			/*
1556 			 * It's really tricky to support syscall/sysret in
1557 			 * the i386 kernel; we rely on sysenter/sysexit
1558 			 * instead.  In the amd64 kernel, things are -way-
1559 			 * better.
1560 			 */
1561 			if (cp->cp_edx & CPUID_AMD_EDX_SYSC) {
1562 				add_x86_feature(featureset, X86FSET_ASYSC);
1563 			}
1564 
1565 			/*
1566 			 * While we're thinking about system calls, note
1567 			 * that AMD processors don't support sysenter
1568 			 * in long mode at all, so don't try to program them.
1569 			 */
1570 			if (x86_vendor == X86_VENDOR_AMD) {
1571 				remove_x86_feature(featureset, X86FSET_SEP);
1572 			}
1573 #endif
1574 			if (cp->cp_edx & CPUID_AMD_EDX_TSCP) {
1575 				add_x86_feature(featureset, X86FSET_TSCP);
1576 			}
1577 
1578 			if (cp->cp_ecx & CPUID_AMD_ECX_SVM) {
1579 				add_x86_feature(featureset, X86FSET_SVM);
1580 			}
1581 
1582 			if (cp->cp_ecx & CPUID_AMD_ECX_TOPOEXT) {
1583 				add_x86_feature(featureset, X86FSET_TOPOEXT);
1584 			}
1585 			break;
1586 		default:
1587 			break;
1588 		}
1589 
1590 		/*
1591 		 * Get CPUID data about processor cores and hyperthreads.
1592 		 */
1593 		switch (cpi->cpi_vendor) {
1594 		case X86_VENDOR_Intel:
1595 			if (cpi->cpi_maxeax >= 4) {
1596 				cp = &cpi->cpi_std[4];
1597 				cp->cp_eax = 4;
1598 				cp->cp_ecx = 0;
1599 				(void) __cpuid_insn(cp);
1600 				platform_cpuid_mangle(cpi->cpi_vendor, 4, cp);
1601 			}
1602 			/*FALLTHROUGH*/
1603 		case X86_VENDOR_AMD:
1604 			if (cpi->cpi_xmaxeax < 0x80000008)
1605 				break;
1606 			cp = &cpi->cpi_extd[8];
1607 			cp->cp_eax = 0x80000008;
1608 			(void) __cpuid_insn(cp);
1609 			platform_cpuid_mangle(cpi->cpi_vendor, 0x80000008, cp);
1610 
1611 			/*
1612 			 * Virtual and physical address limits from
1613 			 * cpuid override previously guessed values.
1614 			 */
1615 			cpi->cpi_pabits = BITX(cp->cp_eax, 7, 0);
1616 			cpi->cpi_vabits = BITX(cp->cp_eax, 15, 8);
1617 			break;
1618 		default:
1619 			break;
1620 		}
1621 
1622 		/*
1623 		 * Derive the number of cores per chip
1624 		 */
1625 		switch (cpi->cpi_vendor) {
1626 		case X86_VENDOR_Intel:
1627 			if (cpi->cpi_maxeax < 4) {
1628 				cpi->cpi_ncore_per_chip = 1;
1629 				break;
1630 			} else {
1631 				cpi->cpi_ncore_per_chip =
1632 				    BITX((cpi)->cpi_std[4].cp_eax, 31, 26) + 1;
1633 			}
1634 			break;
1635 		case X86_VENDOR_AMD:
1636 			if (cpi->cpi_xmaxeax < 0x80000008) {
1637 				cpi->cpi_ncore_per_chip = 1;
1638 				break;
1639 			} else {
1640 				/*
1641 				 * On family 0xf cpuid fn 2 ECX[7:0] "NC" is
1642 				 * 1 less than the number of physical cores on
1643 				 * the chip.  In family 0x10 this value can
1644 				 * be affected by "downcoring" - it reflects
1645 				 * 1 less than the number of cores actually
1646 				 * enabled on this node.
1647 				 */
1648 				cpi->cpi_ncore_per_chip =
1649 				    BITX((cpi)->cpi_extd[8].cp_ecx, 7, 0) + 1;
1650 			}
1651 			break;
1652 		default:
1653 			cpi->cpi_ncore_per_chip = 1;
1654 			break;
1655 		}
1656 
1657 		/*
1658 		 * Get CPUID data about TSC Invariance in Deep C-State.
1659 		 */
1660 		switch (cpi->cpi_vendor) {
1661 		case X86_VENDOR_Intel:
1662 			if (cpi->cpi_maxeax >= 7) {
1663 				cp = &cpi->cpi_extd[7];
1664 				cp->cp_eax = 0x80000007;
1665 				cp->cp_ecx = 0;
1666 				(void) __cpuid_insn(cp);
1667 			}
1668 			break;
1669 		default:
1670 			break;
1671 		}
1672 	} else {
1673 		cpi->cpi_ncore_per_chip = 1;
1674 	}
1675 
1676 	/*
1677 	 * If more than one core, then this processor is CMP.
1678 	 */
1679 	if (cpi->cpi_ncore_per_chip > 1) {
1680 		add_x86_feature(featureset, X86FSET_CMP);
1681 	}
1682 
1683 	/*
1684 	 * If the number of cores is the same as the number
1685 	 * of CPUs, then we cannot have HyperThreading.
1686 	 */
1687 	if (cpi->cpi_ncpu_per_chip == cpi->cpi_ncore_per_chip) {
1688 		remove_x86_feature(featureset, X86FSET_HTT);
1689 	}
1690 
1691 	cpi->cpi_apicid = CPI_APIC_ID(cpi);
1692 	cpi->cpi_procnodes_per_pkg = 1;
1693 	cpi->cpi_cores_per_compunit = 1;
1694 	if (is_x86_feature(featureset, X86FSET_HTT) == B_FALSE &&
1695 	    is_x86_feature(featureset, X86FSET_CMP) == B_FALSE) {
1696 		/*
1697 		 * Single-core single-threaded processors.
1698 		 */
1699 		cpi->cpi_chipid = -1;
1700 		cpi->cpi_clogid = 0;
1701 		cpi->cpi_coreid = cpu->cpu_id;
1702 		cpi->cpi_pkgcoreid = 0;
1703 		if (cpi->cpi_vendor == X86_VENDOR_AMD)
1704 			cpi->cpi_procnodeid = BITX(cpi->cpi_apicid, 3, 0);
1705 		else
1706 			cpi->cpi_procnodeid = cpi->cpi_chipid;
1707 	} else if (cpi->cpi_ncpu_per_chip > 1) {
1708 		if (cpi->cpi_vendor == X86_VENDOR_Intel)
1709 			cpuid_intel_getids(cpu, featureset);
1710 		else if (cpi->cpi_vendor == X86_VENDOR_AMD)
1711 			cpuid_amd_getids(cpu);
1712 		else {
1713 			/*
1714 			 * All other processors are currently
1715 			 * assumed to have single cores.
1716 			 */
1717 			cpi->cpi_coreid = cpi->cpi_chipid;
1718 			cpi->cpi_pkgcoreid = 0;
1719 			cpi->cpi_procnodeid = cpi->cpi_chipid;
1720 			cpi->cpi_compunitid = cpi->cpi_chipid;
1721 		}
1722 	}
1723 
1724 	/*
1725 	 * Synthesize chip "revision" and socket type
1726 	 */
1727 	cpi->cpi_chiprev = _cpuid_chiprev(cpi->cpi_vendor, cpi->cpi_family,
1728 	    cpi->cpi_model, cpi->cpi_step);
1729 	cpi->cpi_chiprevstr = _cpuid_chiprevstr(cpi->cpi_vendor,
1730 	    cpi->cpi_family, cpi->cpi_model, cpi->cpi_step);
1731 	cpi->cpi_socket = _cpuid_skt(cpi->cpi_vendor, cpi->cpi_family,
1732 	    cpi->cpi_model, cpi->cpi_step);
1733 
1734 pass1_done:
1735 	cpi->cpi_pass = 1;
1736 }
1737 
1738 /*
1739  * Make copies of the cpuid table entries we depend on, in
1740  * part for ease of parsing now, in part so that we have only
1741  * one place to correct any of it, in part for ease of
1742  * later export to userland, and in part so we can look at
1743  * this stuff in a crash dump.
1744  */
1745 
1746 /*ARGSUSED*/
1747 void
1748 cpuid_pass2(cpu_t *cpu)
1749 {
1750 	uint_t n, nmax;
1751 	int i;
1752 	struct cpuid_regs *cp;
1753 	uint8_t *dp;
1754 	uint32_t *iptr;
1755 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
1756 
1757 	ASSERT(cpi->cpi_pass == 1);
1758 
1759 	if (cpi->cpi_maxeax < 1)
1760 		goto pass2_done;
1761 
1762 	if ((nmax = cpi->cpi_maxeax + 1) > NMAX_CPI_STD)
1763 		nmax = NMAX_CPI_STD;
1764 	/*
1765 	 * (We already handled n == 0 and n == 1 in pass 1)
1766 	 */
1767 	for (n = 2, cp = &cpi->cpi_std[2]; n < nmax; n++, cp++) {
1768 		cp->cp_eax = n;
1769 
1770 		/*
1771 		 * CPUID function 4 expects %ecx to be initialized
1772 		 * with an index which indicates which cache to return
1773 		 * information about. The OS is expected to call function 4
1774 		 * with %ecx set to 0, 1, 2, ... until it returns with
1775 		 * EAX[4:0] set to 0, which indicates there are no more
1776 		 * caches.
1777 		 *
1778 		 * Here, populate cpi_std[4] with the information returned by
1779 		 * function 4 when %ecx == 0, and do the rest in cpuid_pass3()
1780 		 * when dynamic memory allocation becomes available.
1781 		 *
1782 		 * Note: we need to explicitly initialize %ecx here, since
1783 		 * function 4 may have been previously invoked.
1784 		 */
1785 		if (n == 4)
1786 			cp->cp_ecx = 0;
1787 
1788 		(void) __cpuid_insn(cp);
1789 		platform_cpuid_mangle(cpi->cpi_vendor, n, cp);
1790 		switch (n) {
1791 		case 2:
1792 			/*
1793 			 * "the lower 8 bits of the %eax register
1794 			 * contain a value that identifies the number
1795 			 * of times the cpuid [instruction] has to be
1796 			 * executed to obtain a complete image of the
1797 			 * processor's caching systems."
1798 			 *
1799 			 * How *do* they make this stuff up?
1800 			 */
1801 			cpi->cpi_ncache = sizeof (*cp) *
1802 			    BITX(cp->cp_eax, 7, 0);
1803 			if (cpi->cpi_ncache == 0)
1804 				break;
1805 			cpi->cpi_ncache--;	/* skip count byte */
1806 
1807 			/*
1808 			 * Well, for now, rather than attempt to implement
1809 			 * this slightly dubious algorithm, we just look
1810 			 * at the first 15 ..
1811 			 */
1812 			if (cpi->cpi_ncache > (sizeof (*cp) - 1))
1813 				cpi->cpi_ncache = sizeof (*cp) - 1;
1814 
1815 			dp = cpi->cpi_cacheinfo;
1816 			if (BITX(cp->cp_eax, 31, 31) == 0) {
1817 				uint8_t *p = (void *)&cp->cp_eax;
1818 				for (i = 1; i < 4; i++)
1819 					if (p[i] != 0)
1820 						*dp++ = p[i];
1821 			}
1822 			if (BITX(cp->cp_ebx, 31, 31) == 0) {
1823 				uint8_t *p = (void *)&cp->cp_ebx;
1824 				for (i = 0; i < 4; i++)
1825 					if (p[i] != 0)
1826 						*dp++ = p[i];
1827 			}
1828 			if (BITX(cp->cp_ecx, 31, 31) == 0) {
1829 				uint8_t *p = (void *)&cp->cp_ecx;
1830 				for (i = 0; i < 4; i++)
1831 					if (p[i] != 0)
1832 						*dp++ = p[i];
1833 			}
1834 			if (BITX(cp->cp_edx, 31, 31) == 0) {
1835 				uint8_t *p = (void *)&cp->cp_edx;
1836 				for (i = 0; i < 4; i++)
1837 					if (p[i] != 0)
1838 						*dp++ = p[i];
1839 			}
1840 			break;
1841 
1842 		case 3:	/* Processor serial number, if PSN supported */
1843 			break;
1844 
1845 		case 4:	/* Deterministic cache parameters */
1846 			break;
1847 
1848 		case 5:	/* Monitor/Mwait parameters */
1849 		{
1850 			size_t mwait_size;
1851 
1852 			/*
1853 			 * check cpi_mwait.support which was set in cpuid_pass1
1854 			 */
1855 			if (!(cpi->cpi_mwait.support & MWAIT_SUPPORT))
1856 				break;
1857 
1858 			/*
1859 			 * Protect ourself from insane mwait line size.
1860 			 * Workaround for incomplete hardware emulator(s).
1861 			 */
1862 			mwait_size = (size_t)MWAIT_SIZE_MAX(cpi);
1863 			if (mwait_size < sizeof (uint32_t) ||
1864 			    !ISP2(mwait_size)) {
1865 #if DEBUG
1866 				cmn_err(CE_NOTE, "Cannot handle cpu %d mwait "
1867 				    "size %ld", cpu->cpu_id, (long)mwait_size);
1868 #endif
1869 				break;
1870 			}
1871 
1872 			cpi->cpi_mwait.mon_min = (size_t)MWAIT_SIZE_MIN(cpi);
1873 			cpi->cpi_mwait.mon_max = mwait_size;
1874 			if (MWAIT_EXTENSION(cpi)) {
1875 				cpi->cpi_mwait.support |= MWAIT_EXTENSIONS;
1876 				if (MWAIT_INT_ENABLE(cpi))
1877 					cpi->cpi_mwait.support |=
1878 					    MWAIT_ECX_INT_ENABLE;
1879 			}
1880 			break;
1881 		}
1882 		default:
1883 			break;
1884 		}
1885 	}
1886 
1887 	if (cpi->cpi_maxeax >= 0xB && cpi->cpi_vendor == X86_VENDOR_Intel) {
1888 		struct cpuid_regs regs;
1889 
1890 		cp = &regs;
1891 		cp->cp_eax = 0xB;
1892 		cp->cp_edx = cp->cp_ebx = cp->cp_ecx = 0;
1893 
1894 		(void) __cpuid_insn(cp);
1895 
1896 		/*
1897 		 * Check CPUID.EAX=0BH, ECX=0H:EBX is non-zero, which
1898 		 * indicates that the extended topology enumeration leaf is
1899 		 * available.
1900 		 */
1901 		if (cp->cp_ebx) {
1902 			uint32_t x2apic_id;
1903 			uint_t coreid_shift = 0;
1904 			uint_t ncpu_per_core = 1;
1905 			uint_t chipid_shift = 0;
1906 			uint_t ncpu_per_chip = 1;
1907 			uint_t i;
1908 			uint_t level;
1909 
1910 			for (i = 0; i < CPI_FNB_ECX_MAX; i++) {
1911 				cp->cp_eax = 0xB;
1912 				cp->cp_ecx = i;
1913 
1914 				(void) __cpuid_insn(cp);
1915 				level = CPI_CPU_LEVEL_TYPE(cp);
1916 
1917 				if (level == 1) {
1918 					x2apic_id = cp->cp_edx;
1919 					coreid_shift = BITX(cp->cp_eax, 4, 0);
1920 					ncpu_per_core = BITX(cp->cp_ebx, 15, 0);
1921 				} else if (level == 2) {
1922 					x2apic_id = cp->cp_edx;
1923 					chipid_shift = BITX(cp->cp_eax, 4, 0);
1924 					ncpu_per_chip = BITX(cp->cp_ebx, 15, 0);
1925 				}
1926 			}
1927 
1928 			cpi->cpi_apicid = x2apic_id;
1929 			cpi->cpi_ncpu_per_chip = ncpu_per_chip;
1930 			cpi->cpi_ncore_per_chip = ncpu_per_chip /
1931 			    ncpu_per_core;
1932 			cpi->cpi_chipid = x2apic_id >> chipid_shift;
1933 			cpi->cpi_clogid = x2apic_id & ((1 << chipid_shift) - 1);
1934 			cpi->cpi_coreid = x2apic_id >> coreid_shift;
1935 			cpi->cpi_pkgcoreid = cpi->cpi_clogid >> coreid_shift;
1936 		}
1937 
1938 		/* Make cp NULL so that we don't stumble on others */
1939 		cp = NULL;
1940 	}
1941 
1942 	/*
1943 	 * XSAVE enumeration
1944 	 */
1945 	if (cpi->cpi_maxeax >= 0xD) {
1946 		struct cpuid_regs regs;
1947 		boolean_t cpuid_d_valid = B_TRUE;
1948 
1949 		cp = &regs;
1950 		cp->cp_eax = 0xD;
1951 		cp->cp_edx = cp->cp_ebx = cp->cp_ecx = 0;
1952 
1953 		(void) __cpuid_insn(cp);
1954 
1955 		/*
1956 		 * Sanity checks for debug
1957 		 */
1958 		if ((cp->cp_eax & XFEATURE_LEGACY_FP) == 0 ||
1959 		    (cp->cp_eax & XFEATURE_SSE) == 0) {
1960 			cpuid_d_valid = B_FALSE;
1961 		}
1962 
1963 		cpi->cpi_xsave.xsav_hw_features_low = cp->cp_eax;
1964 		cpi->cpi_xsave.xsav_hw_features_high = cp->cp_edx;
1965 		cpi->cpi_xsave.xsav_max_size = cp->cp_ecx;
1966 
1967 		/*
1968 		 * If the hw supports AVX, get the size and offset in the save
1969 		 * area for the ymm state.
1970 		 */
1971 		if (cpi->cpi_xsave.xsav_hw_features_low & XFEATURE_AVX) {
1972 			cp->cp_eax = 0xD;
1973 			cp->cp_ecx = 2;
1974 			cp->cp_edx = cp->cp_ebx = 0;
1975 
1976 			(void) __cpuid_insn(cp);
1977 
1978 			if (cp->cp_ebx != CPUID_LEAFD_2_YMM_OFFSET ||
1979 			    cp->cp_eax != CPUID_LEAFD_2_YMM_SIZE) {
1980 				cpuid_d_valid = B_FALSE;
1981 			}
1982 
1983 			cpi->cpi_xsave.ymm_size = cp->cp_eax;
1984 			cpi->cpi_xsave.ymm_offset = cp->cp_ebx;
1985 		}
1986 
1987 		if (is_x86_feature(x86_featureset, X86FSET_XSAVE)) {
1988 			xsave_state_size = 0;
1989 		} else if (cpuid_d_valid) {
1990 			xsave_state_size = cpi->cpi_xsave.xsav_max_size;
1991 		} else {
1992 			/* Broken CPUID 0xD, probably in HVM */
1993 			cmn_err(CE_WARN, "cpu%d: CPUID.0xD returns invalid "
1994 			    "value: hw_low = %d, hw_high = %d, xsave_size = %d"
1995 			    ", ymm_size = %d, ymm_offset = %d\n",
1996 			    cpu->cpu_id, cpi->cpi_xsave.xsav_hw_features_low,
1997 			    cpi->cpi_xsave.xsav_hw_features_high,
1998 			    (int)cpi->cpi_xsave.xsav_max_size,
1999 			    (int)cpi->cpi_xsave.ymm_size,
2000 			    (int)cpi->cpi_xsave.ymm_offset);
2001 
2002 			if (xsave_state_size != 0) {
2003 				/*
2004 				 * This must be a non-boot CPU. We cannot
2005 				 * continue, because boot cpu has already
2006 				 * enabled XSAVE.
2007 				 */
2008 				ASSERT(cpu->cpu_id != 0);
2009 				cmn_err(CE_PANIC, "cpu%d: we have already "
2010 				    "enabled XSAVE on boot cpu, cannot "
2011 				    "continue.", cpu->cpu_id);
2012 			} else {
2013 				/*
2014 				 * If we reached here on the boot CPU, it's also
2015 				 * almost certain that we'll reach here on the
2016 				 * non-boot CPUs. When we're here on a boot CPU
2017 				 * we should disable the feature, on a non-boot
2018 				 * CPU we need to confirm that we have.
2019 				 */
2020 				if (cpu->cpu_id == 0) {
2021 					remove_x86_feature(x86_featureset,
2022 					    X86FSET_XSAVE);
2023 					remove_x86_feature(x86_featureset,
2024 					    X86FSET_AVX);
2025 					remove_x86_feature(x86_featureset,
2026 					    X86FSET_F16C);
2027 					remove_x86_feature(x86_featureset,
2028 					    X86FSET_BMI1);
2029 					remove_x86_feature(x86_featureset,
2030 					    X86FSET_BMI2);
2031 					remove_x86_feature(x86_featureset,
2032 					    X86FSET_FMA);
2033 					remove_x86_feature(x86_featureset,
2034 					    X86FSET_AVX2);
2035 					CPI_FEATURES_ECX(cpi) &=
2036 					    ~CPUID_INTC_ECX_XSAVE;
2037 					CPI_FEATURES_ECX(cpi) &=
2038 					    ~CPUID_INTC_ECX_AVX;
2039 					CPI_FEATURES_ECX(cpi) &=
2040 					    ~CPUID_INTC_ECX_F16C;
2041 					CPI_FEATURES_ECX(cpi) &=
2042 					    ~CPUID_INTC_ECX_FMA;
2043 					CPI_FEATURES_7_0_EBX(cpi) &=
2044 					    ~CPUID_INTC_EBX_7_0_BMI1;
2045 					CPI_FEATURES_7_0_EBX(cpi) &=
2046 					    ~CPUID_INTC_EBX_7_0_BMI2;
2047 					CPI_FEATURES_7_0_EBX(cpi) &=
2048 					    ~CPUID_INTC_EBX_7_0_AVX2;
2049 					xsave_force_disable = B_TRUE;
2050 				} else {
2051 					VERIFY(is_x86_feature(x86_featureset,
2052 					    X86FSET_XSAVE) == B_FALSE);
2053 				}
2054 			}
2055 		}
2056 	}
2057 
2058 
2059 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0)
2060 		goto pass2_done;
2061 
2062 	if ((nmax = cpi->cpi_xmaxeax - 0x80000000 + 1) > NMAX_CPI_EXTD)
2063 		nmax = NMAX_CPI_EXTD;
2064 	/*
2065 	 * Copy the extended properties, fixing them as we go.
2066 	 * (We already handled n == 0 and n == 1 in pass 1)
2067 	 */
2068 	iptr = (void *)cpi->cpi_brandstr;
2069 	for (n = 2, cp = &cpi->cpi_extd[2]; n < nmax; cp++, n++) {
2070 		cp->cp_eax = 0x80000000 + n;
2071 		(void) __cpuid_insn(cp);
2072 		platform_cpuid_mangle(cpi->cpi_vendor, 0x80000000 + n, cp);
2073 		switch (n) {
2074 		case 2:
2075 		case 3:
2076 		case 4:
2077 			/*
2078 			 * Extract the brand string
2079 			 */
2080 			*iptr++ = cp->cp_eax;
2081 			*iptr++ = cp->cp_ebx;
2082 			*iptr++ = cp->cp_ecx;
2083 			*iptr++ = cp->cp_edx;
2084 			break;
2085 		case 5:
2086 			switch (cpi->cpi_vendor) {
2087 			case X86_VENDOR_AMD:
2088 				/*
2089 				 * The Athlon and Duron were the first
2090 				 * parts to report the sizes of the
2091 				 * TLB for large pages. Before then,
2092 				 * we don't trust the data.
2093 				 */
2094 				if (cpi->cpi_family < 6 ||
2095 				    (cpi->cpi_family == 6 &&
2096 				    cpi->cpi_model < 1))
2097 					cp->cp_eax = 0;
2098 				break;
2099 			default:
2100 				break;
2101 			}
2102 			break;
2103 		case 6:
2104 			switch (cpi->cpi_vendor) {
2105 			case X86_VENDOR_AMD:
2106 				/*
2107 				 * The Athlon and Duron were the first
2108 				 * AMD parts with L2 TLB's.
2109 				 * Before then, don't trust the data.
2110 				 */
2111 				if (cpi->cpi_family < 6 ||
2112 				    cpi->cpi_family == 6 &&
2113 				    cpi->cpi_model < 1)
2114 					cp->cp_eax = cp->cp_ebx = 0;
2115 				/*
2116 				 * AMD Duron rev A0 reports L2
2117 				 * cache size incorrectly as 1K
2118 				 * when it is really 64K
2119 				 */
2120 				if (cpi->cpi_family == 6 &&
2121 				    cpi->cpi_model == 3 &&
2122 				    cpi->cpi_step == 0) {
2123 					cp->cp_ecx &= 0xffff;
2124 					cp->cp_ecx |= 0x400000;
2125 				}
2126 				break;
2127 			case X86_VENDOR_Cyrix:	/* VIA C3 */
2128 				/*
2129 				 * VIA C3 processors are a bit messed
2130 				 * up w.r.t. encoding cache sizes in %ecx
2131 				 */
2132 				if (cpi->cpi_family != 6)
2133 					break;
2134 				/*
2135 				 * model 7 and 8 were incorrectly encoded
2136 				 *
2137 				 * xxx is model 8 really broken?
2138 				 */
2139 				if (cpi->cpi_model == 7 ||
2140 				    cpi->cpi_model == 8)
2141 					cp->cp_ecx =
2142 					    BITX(cp->cp_ecx, 31, 24) << 16 |
2143 					    BITX(cp->cp_ecx, 23, 16) << 12 |
2144 					    BITX(cp->cp_ecx, 15, 8) << 8 |
2145 					    BITX(cp->cp_ecx, 7, 0);
2146 				/*
2147 				 * model 9 stepping 1 has wrong associativity
2148 				 */
2149 				if (cpi->cpi_model == 9 && cpi->cpi_step == 1)
2150 					cp->cp_ecx |= 8 << 12;
2151 				break;
2152 			case X86_VENDOR_Intel:
2153 				/*
2154 				 * Extended L2 Cache features function.
2155 				 * First appeared on Prescott.
2156 				 */
2157 			default:
2158 				break;
2159 			}
2160 			break;
2161 		default:
2162 			break;
2163 		}
2164 	}
2165 
2166 pass2_done:
2167 	cpi->cpi_pass = 2;
2168 }
2169 
2170 static const char *
2171 intel_cpubrand(const struct cpuid_info *cpi)
2172 {
2173 	int i;
2174 
2175 	if (!is_x86_feature(x86_featureset, X86FSET_CPUID) ||
2176 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
2177 		return ("i486");
2178 
2179 	switch (cpi->cpi_family) {
2180 	case 5:
2181 		return ("Intel Pentium(r)");
2182 	case 6:
2183 		switch (cpi->cpi_model) {
2184 			uint_t celeron, xeon;
2185 			const struct cpuid_regs *cp;
2186 		case 0:
2187 		case 1:
2188 		case 2:
2189 			return ("Intel Pentium(r) Pro");
2190 		case 3:
2191 		case 4:
2192 			return ("Intel Pentium(r) II");
2193 		case 6:
2194 			return ("Intel Celeron(r)");
2195 		case 5:
2196 		case 7:
2197 			celeron = xeon = 0;
2198 			cp = &cpi->cpi_std[2];	/* cache info */
2199 
2200 			for (i = 1; i < 4; i++) {
2201 				uint_t tmp;
2202 
2203 				tmp = (cp->cp_eax >> (8 * i)) & 0xff;
2204 				if (tmp == 0x40)
2205 					celeron++;
2206 				if (tmp >= 0x44 && tmp <= 0x45)
2207 					xeon++;
2208 			}
2209 
2210 			for (i = 0; i < 2; i++) {
2211 				uint_t tmp;
2212 
2213 				tmp = (cp->cp_ebx >> (8 * i)) & 0xff;
2214 				if (tmp == 0x40)
2215 					celeron++;
2216 				else if (tmp >= 0x44 && tmp <= 0x45)
2217 					xeon++;
2218 			}
2219 
2220 			for (i = 0; i < 4; i++) {
2221 				uint_t tmp;
2222 
2223 				tmp = (cp->cp_ecx >> (8 * i)) & 0xff;
2224 				if (tmp == 0x40)
2225 					celeron++;
2226 				else if (tmp >= 0x44 && tmp <= 0x45)
2227 					xeon++;
2228 			}
2229 
2230 			for (i = 0; i < 4; i++) {
2231 				uint_t tmp;
2232 
2233 				tmp = (cp->cp_edx >> (8 * i)) & 0xff;
2234 				if (tmp == 0x40)
2235 					celeron++;
2236 				else if (tmp >= 0x44 && tmp <= 0x45)
2237 					xeon++;
2238 			}
2239 
2240 			if (celeron)
2241 				return ("Intel Celeron(r)");
2242 			if (xeon)
2243 				return (cpi->cpi_model == 5 ?
2244 				    "Intel Pentium(r) II Xeon(tm)" :
2245 				    "Intel Pentium(r) III Xeon(tm)");
2246 			return (cpi->cpi_model == 5 ?
2247 			    "Intel Pentium(r) II or Pentium(r) II Xeon(tm)" :
2248 			    "Intel Pentium(r) III or Pentium(r) III Xeon(tm)");
2249 		default:
2250 			break;
2251 		}
2252 	default:
2253 		break;
2254 	}
2255 
2256 	/* BrandID is present if the field is nonzero */
2257 	if (cpi->cpi_brandid != 0) {
2258 		static const struct {
2259 			uint_t bt_bid;
2260 			const char *bt_str;
2261 		} brand_tbl[] = {
2262 			{ 0x1,	"Intel(r) Celeron(r)" },
2263 			{ 0x2,	"Intel(r) Pentium(r) III" },
2264 			{ 0x3,	"Intel(r) Pentium(r) III Xeon(tm)" },
2265 			{ 0x4,	"Intel(r) Pentium(r) III" },
2266 			{ 0x6,	"Mobile Intel(r) Pentium(r) III" },
2267 			{ 0x7,	"Mobile Intel(r) Celeron(r)" },
2268 			{ 0x8,	"Intel(r) Pentium(r) 4" },
2269 			{ 0x9,	"Intel(r) Pentium(r) 4" },
2270 			{ 0xa,	"Intel(r) Celeron(r)" },
2271 			{ 0xb,	"Intel(r) Xeon(tm)" },
2272 			{ 0xc,	"Intel(r) Xeon(tm) MP" },
2273 			{ 0xe,	"Mobile Intel(r) Pentium(r) 4" },
2274 			{ 0xf,	"Mobile Intel(r) Celeron(r)" },
2275 			{ 0x11, "Mobile Genuine Intel(r)" },
2276 			{ 0x12, "Intel(r) Celeron(r) M" },
2277 			{ 0x13, "Mobile Intel(r) Celeron(r)" },
2278 			{ 0x14, "Intel(r) Celeron(r)" },
2279 			{ 0x15, "Mobile Genuine Intel(r)" },
2280 			{ 0x16,	"Intel(r) Pentium(r) M" },
2281 			{ 0x17, "Mobile Intel(r) Celeron(r)" }
2282 		};
2283 		uint_t btblmax = sizeof (brand_tbl) / sizeof (brand_tbl[0]);
2284 		uint_t sgn;
2285 
2286 		sgn = (cpi->cpi_family << 8) |
2287 		    (cpi->cpi_model << 4) | cpi->cpi_step;
2288 
2289 		for (i = 0; i < btblmax; i++)
2290 			if (brand_tbl[i].bt_bid == cpi->cpi_brandid)
2291 				break;
2292 		if (i < btblmax) {
2293 			if (sgn == 0x6b1 && cpi->cpi_brandid == 3)
2294 				return ("Intel(r) Celeron(r)");
2295 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xb)
2296 				return ("Intel(r) Xeon(tm) MP");
2297 			if (sgn < 0xf13 && cpi->cpi_brandid == 0xe)
2298 				return ("Intel(r) Xeon(tm)");
2299 			return (brand_tbl[i].bt_str);
2300 		}
2301 	}
2302 
2303 	return (NULL);
2304 }
2305 
2306 static const char *
2307 amd_cpubrand(const struct cpuid_info *cpi)
2308 {
2309 	if (!is_x86_feature(x86_featureset, X86FSET_CPUID) ||
2310 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5)
2311 		return ("i486 compatible");
2312 
2313 	switch (cpi->cpi_family) {
2314 	case 5:
2315 		switch (cpi->cpi_model) {
2316 		case 0:
2317 		case 1:
2318 		case 2:
2319 		case 3:
2320 		case 4:
2321 		case 5:
2322 			return ("AMD-K5(r)");
2323 		case 6:
2324 		case 7:
2325 			return ("AMD-K6(r)");
2326 		case 8:
2327 			return ("AMD-K6(r)-2");
2328 		case 9:
2329 			return ("AMD-K6(r)-III");
2330 		default:
2331 			return ("AMD (family 5)");
2332 		}
2333 	case 6:
2334 		switch (cpi->cpi_model) {
2335 		case 1:
2336 			return ("AMD-K7(tm)");
2337 		case 0:
2338 		case 2:
2339 		case 4:
2340 			return ("AMD Athlon(tm)");
2341 		case 3:
2342 		case 7:
2343 			return ("AMD Duron(tm)");
2344 		case 6:
2345 		case 8:
2346 		case 10:
2347 			/*
2348 			 * Use the L2 cache size to distinguish
2349 			 */
2350 			return ((cpi->cpi_extd[6].cp_ecx >> 16) >= 256 ?
2351 			    "AMD Athlon(tm)" : "AMD Duron(tm)");
2352 		default:
2353 			return ("AMD (family 6)");
2354 		}
2355 	default:
2356 		break;
2357 	}
2358 
2359 	if (cpi->cpi_family == 0xf && cpi->cpi_model == 5 &&
2360 	    cpi->cpi_brandid != 0) {
2361 		switch (BITX(cpi->cpi_brandid, 7, 5)) {
2362 		case 3:
2363 			return ("AMD Opteron(tm) UP 1xx");
2364 		case 4:
2365 			return ("AMD Opteron(tm) DP 2xx");
2366 		case 5:
2367 			return ("AMD Opteron(tm) MP 8xx");
2368 		default:
2369 			return ("AMD Opteron(tm)");
2370 		}
2371 	}
2372 
2373 	return (NULL);
2374 }
2375 
2376 static const char *
2377 cyrix_cpubrand(struct cpuid_info *cpi, uint_t type)
2378 {
2379 	if (!is_x86_feature(x86_featureset, X86FSET_CPUID) ||
2380 	    cpi->cpi_maxeax < 1 || cpi->cpi_family < 5 ||
2381 	    type == X86_TYPE_CYRIX_486)
2382 		return ("i486 compatible");
2383 
2384 	switch (type) {
2385 	case X86_TYPE_CYRIX_6x86:
2386 		return ("Cyrix 6x86");
2387 	case X86_TYPE_CYRIX_6x86L:
2388 		return ("Cyrix 6x86L");
2389 	case X86_TYPE_CYRIX_6x86MX:
2390 		return ("Cyrix 6x86MX");
2391 	case X86_TYPE_CYRIX_GXm:
2392 		return ("Cyrix GXm");
2393 	case X86_TYPE_CYRIX_MediaGX:
2394 		return ("Cyrix MediaGX");
2395 	case X86_TYPE_CYRIX_MII:
2396 		return ("Cyrix M2");
2397 	case X86_TYPE_VIA_CYRIX_III:
2398 		return ("VIA Cyrix M3");
2399 	default:
2400 		/*
2401 		 * Have another wild guess ..
2402 		 */
2403 		if (cpi->cpi_family == 4 && cpi->cpi_model == 9)
2404 			return ("Cyrix 5x86");
2405 		else if (cpi->cpi_family == 5) {
2406 			switch (cpi->cpi_model) {
2407 			case 2:
2408 				return ("Cyrix 6x86");	/* Cyrix M1 */
2409 			case 4:
2410 				return ("Cyrix MediaGX");
2411 			default:
2412 				break;
2413 			}
2414 		} else if (cpi->cpi_family == 6) {
2415 			switch (cpi->cpi_model) {
2416 			case 0:
2417 				return ("Cyrix 6x86MX"); /* Cyrix M2? */
2418 			case 5:
2419 			case 6:
2420 			case 7:
2421 			case 8:
2422 			case 9:
2423 				return ("VIA C3");
2424 			default:
2425 				break;
2426 			}
2427 		}
2428 		break;
2429 	}
2430 	return (NULL);
2431 }
2432 
2433 /*
2434  * This only gets called in the case that the CPU extended
2435  * feature brand string (0x80000002, 0x80000003, 0x80000004)
2436  * aren't available, or contain null bytes for some reason.
2437  */
2438 static void
2439 fabricate_brandstr(struct cpuid_info *cpi)
2440 {
2441 	const char *brand = NULL;
2442 
2443 	switch (cpi->cpi_vendor) {
2444 	case X86_VENDOR_Intel:
2445 		brand = intel_cpubrand(cpi);
2446 		break;
2447 	case X86_VENDOR_AMD:
2448 		brand = amd_cpubrand(cpi);
2449 		break;
2450 	case X86_VENDOR_Cyrix:
2451 		brand = cyrix_cpubrand(cpi, x86_type);
2452 		break;
2453 	case X86_VENDOR_NexGen:
2454 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
2455 			brand = "NexGen Nx586";
2456 		break;
2457 	case X86_VENDOR_Centaur:
2458 		if (cpi->cpi_family == 5)
2459 			switch (cpi->cpi_model) {
2460 			case 4:
2461 				brand = "Centaur C6";
2462 				break;
2463 			case 8:
2464 				brand = "Centaur C2";
2465 				break;
2466 			case 9:
2467 				brand = "Centaur C3";
2468 				break;
2469 			default:
2470 				break;
2471 			}
2472 		break;
2473 	case X86_VENDOR_Rise:
2474 		if (cpi->cpi_family == 5 &&
2475 		    (cpi->cpi_model == 0 || cpi->cpi_model == 2))
2476 			brand = "Rise mP6";
2477 		break;
2478 	case X86_VENDOR_SiS:
2479 		if (cpi->cpi_family == 5 && cpi->cpi_model == 0)
2480 			brand = "SiS 55x";
2481 		break;
2482 	case X86_VENDOR_TM:
2483 		if (cpi->cpi_family == 5 && cpi->cpi_model == 4)
2484 			brand = "Transmeta Crusoe TM3x00 or TM5x00";
2485 		break;
2486 	case X86_VENDOR_NSC:
2487 	case X86_VENDOR_UMC:
2488 	default:
2489 		break;
2490 	}
2491 	if (brand) {
2492 		(void) strcpy((char *)cpi->cpi_brandstr, brand);
2493 		return;
2494 	}
2495 
2496 	/*
2497 	 * If all else fails ...
2498 	 */
2499 	(void) snprintf(cpi->cpi_brandstr, sizeof (cpi->cpi_brandstr),
2500 	    "%s %d.%d.%d", cpi->cpi_vendorstr, cpi->cpi_family,
2501 	    cpi->cpi_model, cpi->cpi_step);
2502 }
2503 
2504 /*
2505  * This routine is called just after kernel memory allocation
2506  * becomes available on cpu0, and as part of mp_startup() on
2507  * the other cpus.
2508  *
2509  * Fixup the brand string, and collect any information from cpuid
2510  * that requires dynamically allocated storage to represent.
2511  */
2512 /*ARGSUSED*/
2513 void
2514 cpuid_pass3(cpu_t *cpu)
2515 {
2516 	int	i, max, shft, level, size;
2517 	struct cpuid_regs regs;
2518 	struct cpuid_regs *cp;
2519 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
2520 
2521 	ASSERT(cpi->cpi_pass == 2);
2522 
2523 	/*
2524 	 * Function 4: Deterministic cache parameters
2525 	 *
2526 	 * Take this opportunity to detect the number of threads
2527 	 * sharing the last level cache, and construct a corresponding
2528 	 * cache id. The respective cpuid_info members are initialized
2529 	 * to the default case of "no last level cache sharing".
2530 	 */
2531 	cpi->cpi_ncpu_shr_last_cache = 1;
2532 	cpi->cpi_last_lvl_cacheid = cpu->cpu_id;
2533 
2534 	if (cpi->cpi_maxeax >= 4 && cpi->cpi_vendor == X86_VENDOR_Intel) {
2535 
2536 		/*
2537 		 * Find the # of elements (size) returned by fn 4, and along
2538 		 * the way detect last level cache sharing details.
2539 		 */
2540 		bzero(&regs, sizeof (regs));
2541 		cp = &regs;
2542 		for (i = 0, max = 0; i < CPI_FN4_ECX_MAX; i++) {
2543 			cp->cp_eax = 4;
2544 			cp->cp_ecx = i;
2545 
2546 			(void) __cpuid_insn(cp);
2547 
2548 			if (CPI_CACHE_TYPE(cp) == 0)
2549 				break;
2550 			level = CPI_CACHE_LVL(cp);
2551 			if (level > max) {
2552 				max = level;
2553 				cpi->cpi_ncpu_shr_last_cache =
2554 				    CPI_NTHR_SHR_CACHE(cp) + 1;
2555 			}
2556 		}
2557 		cpi->cpi_std_4_size = size = i;
2558 
2559 		/*
2560 		 * Allocate the cpi_std_4 array. The first element
2561 		 * references the regs for fn 4, %ecx == 0, which
2562 		 * cpuid_pass2() stashed in cpi->cpi_std[4].
2563 		 */
2564 		if (size > 0) {
2565 			cpi->cpi_std_4 =
2566 			    kmem_alloc(size * sizeof (cp), KM_SLEEP);
2567 			cpi->cpi_std_4[0] = &cpi->cpi_std[4];
2568 
2569 			/*
2570 			 * Allocate storage to hold the additional regs
2571 			 * for function 4, %ecx == 1 .. cpi_std_4_size.
2572 			 *
2573 			 * The regs for fn 4, %ecx == 0 has already
2574 			 * been allocated as indicated above.
2575 			 */
2576 			for (i = 1; i < size; i++) {
2577 				cp = cpi->cpi_std_4[i] =
2578 				    kmem_zalloc(sizeof (regs), KM_SLEEP);
2579 				cp->cp_eax = 4;
2580 				cp->cp_ecx = i;
2581 
2582 				(void) __cpuid_insn(cp);
2583 			}
2584 		}
2585 		/*
2586 		 * Determine the number of bits needed to represent
2587 		 * the number of CPUs sharing the last level cache.
2588 		 *
2589 		 * Shift off that number of bits from the APIC id to
2590 		 * derive the cache id.
2591 		 */
2592 		shft = 0;
2593 		for (i = 1; i < cpi->cpi_ncpu_shr_last_cache; i <<= 1)
2594 			shft++;
2595 		cpi->cpi_last_lvl_cacheid = cpi->cpi_apicid >> shft;
2596 	}
2597 
2598 	/*
2599 	 * Now fixup the brand string
2600 	 */
2601 	if ((cpi->cpi_xmaxeax & 0x80000000) == 0) {
2602 		fabricate_brandstr(cpi);
2603 	} else {
2604 
2605 		/*
2606 		 * If we successfully extracted a brand string from the cpuid
2607 		 * instruction, clean it up by removing leading spaces and
2608 		 * similar junk.
2609 		 */
2610 		if (cpi->cpi_brandstr[0]) {
2611 			size_t maxlen = sizeof (cpi->cpi_brandstr);
2612 			char *src, *dst;
2613 
2614 			dst = src = (char *)cpi->cpi_brandstr;
2615 			src[maxlen - 1] = '\0';
2616 			/*
2617 			 * strip leading spaces
2618 			 */
2619 			while (*src == ' ')
2620 				src++;
2621 			/*
2622 			 * Remove any 'Genuine' or "Authentic" prefixes
2623 			 */
2624 			if (strncmp(src, "Genuine ", 8) == 0)
2625 				src += 8;
2626 			if (strncmp(src, "Authentic ", 10) == 0)
2627 				src += 10;
2628 
2629 			/*
2630 			 * Now do an in-place copy.
2631 			 * Map (R) to (r) and (TM) to (tm).
2632 			 * The era of teletypes is long gone, and there's
2633 			 * -really- no need to shout.
2634 			 */
2635 			while (*src != '\0') {
2636 				if (src[0] == '(') {
2637 					if (strncmp(src + 1, "R)", 2) == 0) {
2638 						(void) strncpy(dst, "(r)", 3);
2639 						src += 3;
2640 						dst += 3;
2641 						continue;
2642 					}
2643 					if (strncmp(src + 1, "TM)", 3) == 0) {
2644 						(void) strncpy(dst, "(tm)", 4);
2645 						src += 4;
2646 						dst += 4;
2647 						continue;
2648 					}
2649 				}
2650 				*dst++ = *src++;
2651 			}
2652 			*dst = '\0';
2653 
2654 			/*
2655 			 * Finally, remove any trailing spaces
2656 			 */
2657 			while (--dst > cpi->cpi_brandstr)
2658 				if (*dst == ' ')
2659 					*dst = '\0';
2660 				else
2661 					break;
2662 		} else
2663 			fabricate_brandstr(cpi);
2664 	}
2665 	cpi->cpi_pass = 3;
2666 }
2667 
2668 /*
2669  * This routine is called out of bind_hwcap() much later in the life
2670  * of the kernel (post_startup()).  The job of this routine is to resolve
2671  * the hardware feature support and kernel support for those features into
2672  * what we're actually going to tell applications via the aux vector.
2673  */
2674 void
2675 cpuid_pass4(cpu_t *cpu, uint_t *hwcap_out)
2676 {
2677 	struct cpuid_info *cpi;
2678 	uint_t hwcap_flags = 0, hwcap_flags_2 = 0;
2679 
2680 	if (cpu == NULL)
2681 		cpu = CPU;
2682 	cpi = cpu->cpu_m.mcpu_cpi;
2683 
2684 	ASSERT(cpi->cpi_pass == 3);
2685 
2686 	if (cpi->cpi_maxeax >= 1) {
2687 		uint32_t *edx = &cpi->cpi_support[STD_EDX_FEATURES];
2688 		uint32_t *ecx = &cpi->cpi_support[STD_ECX_FEATURES];
2689 		uint32_t *ebx = &cpi->cpi_support[STD_EBX_FEATURES];
2690 
2691 		*edx = CPI_FEATURES_EDX(cpi);
2692 		*ecx = CPI_FEATURES_ECX(cpi);
2693 		*ebx = CPI_FEATURES_7_0_EBX(cpi);
2694 
2695 		/*
2696 		 * [these require explicit kernel support]
2697 		 */
2698 		if (!is_x86_feature(x86_featureset, X86FSET_SEP))
2699 			*edx &= ~CPUID_INTC_EDX_SEP;
2700 
2701 		if (!is_x86_feature(x86_featureset, X86FSET_SSE))
2702 			*edx &= ~(CPUID_INTC_EDX_FXSR|CPUID_INTC_EDX_SSE);
2703 		if (!is_x86_feature(x86_featureset, X86FSET_SSE2))
2704 			*edx &= ~CPUID_INTC_EDX_SSE2;
2705 
2706 		if (!is_x86_feature(x86_featureset, X86FSET_HTT))
2707 			*edx &= ~CPUID_INTC_EDX_HTT;
2708 
2709 		if (!is_x86_feature(x86_featureset, X86FSET_SSE3))
2710 			*ecx &= ~CPUID_INTC_ECX_SSE3;
2711 
2712 		if (!is_x86_feature(x86_featureset, X86FSET_SSSE3))
2713 			*ecx &= ~CPUID_INTC_ECX_SSSE3;
2714 		if (!is_x86_feature(x86_featureset, X86FSET_SSE4_1))
2715 			*ecx &= ~CPUID_INTC_ECX_SSE4_1;
2716 		if (!is_x86_feature(x86_featureset, X86FSET_SSE4_2))
2717 			*ecx &= ~CPUID_INTC_ECX_SSE4_2;
2718 		if (!is_x86_feature(x86_featureset, X86FSET_AES))
2719 			*ecx &= ~CPUID_INTC_ECX_AES;
2720 		if (!is_x86_feature(x86_featureset, X86FSET_PCLMULQDQ))
2721 			*ecx &= ~CPUID_INTC_ECX_PCLMULQDQ;
2722 		if (!is_x86_feature(x86_featureset, X86FSET_XSAVE))
2723 			*ecx &= ~(CPUID_INTC_ECX_XSAVE |
2724 			    CPUID_INTC_ECX_OSXSAVE);
2725 		if (!is_x86_feature(x86_featureset, X86FSET_AVX))
2726 			*ecx &= ~CPUID_INTC_ECX_AVX;
2727 		if (!is_x86_feature(x86_featureset, X86FSET_F16C))
2728 			*ecx &= ~CPUID_INTC_ECX_F16C;
2729 		if (!is_x86_feature(x86_featureset, X86FSET_FMA))
2730 			*ecx &= ~CPUID_INTC_ECX_FMA;
2731 		if (!is_x86_feature(x86_featureset, X86FSET_BMI1))
2732 			*ebx &= ~CPUID_INTC_EBX_7_0_BMI1;
2733 		if (!is_x86_feature(x86_featureset, X86FSET_BMI2))
2734 			*ebx &= ~CPUID_INTC_EBX_7_0_BMI2;
2735 		if (!is_x86_feature(x86_featureset, X86FSET_AVX2))
2736 			*ebx &= ~CPUID_INTC_EBX_7_0_AVX2;
2737 		if (!is_x86_feature(x86_featureset, X86FSET_RDSEED))
2738 			*ebx &= ~CPUID_INTC_EBX_7_0_RDSEED;
2739 		if (!is_x86_feature(x86_featureset, X86FSET_ADX))
2740 			*ebx &= ~CPUID_INTC_EBX_7_0_ADX;
2741 
2742 		/*
2743 		 * [no explicit support required beyond x87 fp context]
2744 		 */
2745 		if (!fpu_exists)
2746 			*edx &= ~(CPUID_INTC_EDX_FPU | CPUID_INTC_EDX_MMX);
2747 
2748 		/*
2749 		 * Now map the supported feature vector to things that we
2750 		 * think userland will care about.
2751 		 */
2752 		if (*edx & CPUID_INTC_EDX_SEP)
2753 			hwcap_flags |= AV_386_SEP;
2754 		if (*edx & CPUID_INTC_EDX_SSE)
2755 			hwcap_flags |= AV_386_FXSR | AV_386_SSE;
2756 		if (*edx & CPUID_INTC_EDX_SSE2)
2757 			hwcap_flags |= AV_386_SSE2;
2758 		if (*ecx & CPUID_INTC_ECX_SSE3)
2759 			hwcap_flags |= AV_386_SSE3;
2760 		if (*ecx & CPUID_INTC_ECX_SSSE3)
2761 			hwcap_flags |= AV_386_SSSE3;
2762 		if (*ecx & CPUID_INTC_ECX_SSE4_1)
2763 			hwcap_flags |= AV_386_SSE4_1;
2764 		if (*ecx & CPUID_INTC_ECX_SSE4_2)
2765 			hwcap_flags |= AV_386_SSE4_2;
2766 		if (*ecx & CPUID_INTC_ECX_MOVBE)
2767 			hwcap_flags |= AV_386_MOVBE;
2768 		if (*ecx & CPUID_INTC_ECX_AES)
2769 			hwcap_flags |= AV_386_AES;
2770 		if (*ecx & CPUID_INTC_ECX_PCLMULQDQ)
2771 			hwcap_flags |= AV_386_PCLMULQDQ;
2772 		if ((*ecx & CPUID_INTC_ECX_XSAVE) &&
2773 		    (*ecx & CPUID_INTC_ECX_OSXSAVE)) {
2774 			hwcap_flags |= AV_386_XSAVE;
2775 
2776 			if (*ecx & CPUID_INTC_ECX_AVX) {
2777 				hwcap_flags |= AV_386_AVX;
2778 				if (*ecx & CPUID_INTC_ECX_F16C)
2779 					hwcap_flags_2 |= AV_386_2_F16C;
2780 				if (*ecx & CPUID_INTC_ECX_FMA)
2781 					hwcap_flags_2 |= AV_386_2_FMA;
2782 				if (*ebx & CPUID_INTC_EBX_7_0_BMI1)
2783 					hwcap_flags_2 |= AV_386_2_BMI1;
2784 				if (*ebx & CPUID_INTC_EBX_7_0_BMI2)
2785 					hwcap_flags_2 |= AV_386_2_BMI2;
2786 				if (*ebx & CPUID_INTC_EBX_7_0_AVX2)
2787 					hwcap_flags_2 |= AV_386_2_AVX2;
2788 			}
2789 		}
2790 		if (*ecx & CPUID_INTC_ECX_VMX)
2791 			hwcap_flags |= AV_386_VMX;
2792 		if (*ecx & CPUID_INTC_ECX_POPCNT)
2793 			hwcap_flags |= AV_386_POPCNT;
2794 		if (*edx & CPUID_INTC_EDX_FPU)
2795 			hwcap_flags |= AV_386_FPU;
2796 		if (*edx & CPUID_INTC_EDX_MMX)
2797 			hwcap_flags |= AV_386_MMX;
2798 
2799 		if (*edx & CPUID_INTC_EDX_TSC)
2800 			hwcap_flags |= AV_386_TSC;
2801 		if (*edx & CPUID_INTC_EDX_CX8)
2802 			hwcap_flags |= AV_386_CX8;
2803 		if (*edx & CPUID_INTC_EDX_CMOV)
2804 			hwcap_flags |= AV_386_CMOV;
2805 		if (*ecx & CPUID_INTC_ECX_CX16)
2806 			hwcap_flags |= AV_386_CX16;
2807 
2808 		if (*ecx & CPUID_INTC_ECX_RDRAND)
2809 			hwcap_flags_2 |= AV_386_2_RDRAND;
2810 		if (*ebx & CPUID_INTC_EBX_7_0_ADX)
2811 			hwcap_flags_2 |= AV_386_2_ADX;
2812 		if (*ebx & CPUID_INTC_EBX_7_0_RDSEED)
2813 			hwcap_flags_2 |= AV_386_2_RDSEED;
2814 		if (*ebx & CPUID_INTC_EBX_7_0_SHA)
2815 			hwcap_flags_2 |= AV_386_2_SHA;
2816 
2817 	}
2818 
2819 	if (cpi->cpi_xmaxeax < 0x80000001)
2820 		goto pass4_done;
2821 
2822 	switch (cpi->cpi_vendor) {
2823 		struct cpuid_regs cp;
2824 		uint32_t *edx, *ecx;
2825 
2826 	case X86_VENDOR_Intel:
2827 		/*
2828 		 * Seems like Intel duplicated what we necessary
2829 		 * here to make the initial crop of 64-bit OS's work.
2830 		 * Hopefully, those are the only "extended" bits
2831 		 * they'll add.
2832 		 */
2833 		/*FALLTHROUGH*/
2834 
2835 	case X86_VENDOR_AMD:
2836 		edx = &cpi->cpi_support[AMD_EDX_FEATURES];
2837 		ecx = &cpi->cpi_support[AMD_ECX_FEATURES];
2838 
2839 		*edx = CPI_FEATURES_XTD_EDX(cpi);
2840 		*ecx = CPI_FEATURES_XTD_ECX(cpi);
2841 
2842 		/*
2843 		 * [these features require explicit kernel support]
2844 		 */
2845 		switch (cpi->cpi_vendor) {
2846 		case X86_VENDOR_Intel:
2847 			if (!is_x86_feature(x86_featureset, X86FSET_TSCP))
2848 				*edx &= ~CPUID_AMD_EDX_TSCP;
2849 			break;
2850 
2851 		case X86_VENDOR_AMD:
2852 			if (!is_x86_feature(x86_featureset, X86FSET_TSCP))
2853 				*edx &= ~CPUID_AMD_EDX_TSCP;
2854 			if (!is_x86_feature(x86_featureset, X86FSET_SSE4A))
2855 				*ecx &= ~CPUID_AMD_ECX_SSE4A;
2856 			break;
2857 
2858 		default:
2859 			break;
2860 		}
2861 
2862 		/*
2863 		 * [no explicit support required beyond
2864 		 * x87 fp context and exception handlers]
2865 		 */
2866 		if (!fpu_exists)
2867 			*edx &= ~(CPUID_AMD_EDX_MMXamd |
2868 			    CPUID_AMD_EDX_3DNow | CPUID_AMD_EDX_3DNowx);
2869 
2870 		if (!is_x86_feature(x86_featureset, X86FSET_NX))
2871 			*edx &= ~CPUID_AMD_EDX_NX;
2872 #if !defined(__amd64)
2873 		*edx &= ~CPUID_AMD_EDX_LM;
2874 #endif
2875 		/*
2876 		 * Now map the supported feature vector to
2877 		 * things that we think userland will care about.
2878 		 */
2879 #if defined(__amd64)
2880 		if (*edx & CPUID_AMD_EDX_SYSC)
2881 			hwcap_flags |= AV_386_AMD_SYSC;
2882 #endif
2883 		if (*edx & CPUID_AMD_EDX_MMXamd)
2884 			hwcap_flags |= AV_386_AMD_MMX;
2885 		if (*edx & CPUID_AMD_EDX_3DNow)
2886 			hwcap_flags |= AV_386_AMD_3DNow;
2887 		if (*edx & CPUID_AMD_EDX_3DNowx)
2888 			hwcap_flags |= AV_386_AMD_3DNowx;
2889 		if (*ecx & CPUID_AMD_ECX_SVM)
2890 			hwcap_flags |= AV_386_AMD_SVM;
2891 
2892 		switch (cpi->cpi_vendor) {
2893 		case X86_VENDOR_AMD:
2894 			if (*edx & CPUID_AMD_EDX_TSCP)
2895 				hwcap_flags |= AV_386_TSCP;
2896 			if (*ecx & CPUID_AMD_ECX_AHF64)
2897 				hwcap_flags |= AV_386_AHF;
2898 			if (*ecx & CPUID_AMD_ECX_SSE4A)
2899 				hwcap_flags |= AV_386_AMD_SSE4A;
2900 			if (*ecx & CPUID_AMD_ECX_LZCNT)
2901 				hwcap_flags |= AV_386_AMD_LZCNT;
2902 			break;
2903 
2904 		case X86_VENDOR_Intel:
2905 			if (*edx & CPUID_AMD_EDX_TSCP)
2906 				hwcap_flags |= AV_386_TSCP;
2907 			/*
2908 			 * Aarrgh.
2909 			 * Intel uses a different bit in the same word.
2910 			 */
2911 			if (*ecx & CPUID_INTC_ECX_AHF64)
2912 				hwcap_flags |= AV_386_AHF;
2913 			break;
2914 
2915 		default:
2916 			break;
2917 		}
2918 		break;
2919 
2920 	case X86_VENDOR_TM:
2921 		cp.cp_eax = 0x80860001;
2922 		(void) __cpuid_insn(&cp);
2923 		cpi->cpi_support[TM_EDX_FEATURES] = cp.cp_edx;
2924 		break;
2925 
2926 	default:
2927 		break;
2928 	}
2929 
2930 pass4_done:
2931 	cpi->cpi_pass = 4;
2932 	if (hwcap_out != NULL) {
2933 		hwcap_out[0] = hwcap_flags;
2934 		hwcap_out[1] = hwcap_flags_2;
2935 	}
2936 }
2937 
2938 
2939 /*
2940  * Simulate the cpuid instruction using the data we previously
2941  * captured about this CPU.  We try our best to return the truth
2942  * about the hardware, independently of kernel support.
2943  */
2944 uint32_t
2945 cpuid_insn(cpu_t *cpu, struct cpuid_regs *cp)
2946 {
2947 	struct cpuid_info *cpi;
2948 	struct cpuid_regs *xcp;
2949 
2950 	if (cpu == NULL)
2951 		cpu = CPU;
2952 	cpi = cpu->cpu_m.mcpu_cpi;
2953 
2954 	ASSERT(cpuid_checkpass(cpu, 3));
2955 
2956 	/*
2957 	 * CPUID data is cached in two separate places: cpi_std for standard
2958 	 * CPUID functions, and cpi_extd for extended CPUID functions.
2959 	 */
2960 	if (cp->cp_eax <= cpi->cpi_maxeax && cp->cp_eax < NMAX_CPI_STD)
2961 		xcp = &cpi->cpi_std[cp->cp_eax];
2962 	else if (cp->cp_eax >= 0x80000000 && cp->cp_eax <= cpi->cpi_xmaxeax &&
2963 	    cp->cp_eax < 0x80000000 + NMAX_CPI_EXTD)
2964 		xcp = &cpi->cpi_extd[cp->cp_eax - 0x80000000];
2965 	else
2966 		/*
2967 		 * The caller is asking for data from an input parameter which
2968 		 * the kernel has not cached.  In this case we go fetch from
2969 		 * the hardware and return the data directly to the user.
2970 		 */
2971 		return (__cpuid_insn(cp));
2972 
2973 	cp->cp_eax = xcp->cp_eax;
2974 	cp->cp_ebx = xcp->cp_ebx;
2975 	cp->cp_ecx = xcp->cp_ecx;
2976 	cp->cp_edx = xcp->cp_edx;
2977 	return (cp->cp_eax);
2978 }
2979 
2980 int
2981 cpuid_checkpass(cpu_t *cpu, int pass)
2982 {
2983 	return (cpu != NULL && cpu->cpu_m.mcpu_cpi != NULL &&
2984 	    cpu->cpu_m.mcpu_cpi->cpi_pass >= pass);
2985 }
2986 
2987 int
2988 cpuid_getbrandstr(cpu_t *cpu, char *s, size_t n)
2989 {
2990 	ASSERT(cpuid_checkpass(cpu, 3));
2991 
2992 	return (snprintf(s, n, "%s", cpu->cpu_m.mcpu_cpi->cpi_brandstr));
2993 }
2994 
2995 int
2996 cpuid_is_cmt(cpu_t *cpu)
2997 {
2998 	if (cpu == NULL)
2999 		cpu = CPU;
3000 
3001 	ASSERT(cpuid_checkpass(cpu, 1));
3002 
3003 	return (cpu->cpu_m.mcpu_cpi->cpi_chipid >= 0);
3004 }
3005 
3006 /*
3007  * AMD and Intel both implement the 64-bit variant of the syscall
3008  * instruction (syscallq), so if there's -any- support for syscall,
3009  * cpuid currently says "yes, we support this".
3010  *
3011  * However, Intel decided to -not- implement the 32-bit variant of the
3012  * syscall instruction, so we provide a predicate to allow our caller
3013  * to test that subtlety here.
3014  *
3015  * XXPV	Currently, 32-bit syscall instructions don't work via the hypervisor,
3016  *	even in the case where the hardware would in fact support it.
3017  */
3018 /*ARGSUSED*/
3019 int
3020 cpuid_syscall32_insn(cpu_t *cpu)
3021 {
3022 	ASSERT(cpuid_checkpass((cpu == NULL ? CPU : cpu), 1));
3023 
3024 #if !defined(__xpv)
3025 	if (cpu == NULL)
3026 		cpu = CPU;
3027 
3028 	/*CSTYLED*/
3029 	{
3030 		struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
3031 
3032 		if (cpi->cpi_vendor == X86_VENDOR_AMD &&
3033 		    cpi->cpi_xmaxeax >= 0x80000001 &&
3034 		    (CPI_FEATURES_XTD_EDX(cpi) & CPUID_AMD_EDX_SYSC))
3035 			return (1);
3036 	}
3037 #endif
3038 	return (0);
3039 }
3040 
3041 int
3042 cpuid_getidstr(cpu_t *cpu, char *s, size_t n)
3043 {
3044 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
3045 
3046 	static const char fmt[] =
3047 	    "x86 (%s %X family %d model %d step %d clock %d MHz)";
3048 	static const char fmt_ht[] =
3049 	    "x86 (chipid 0x%x %s %X family %d model %d step %d clock %d MHz)";
3050 
3051 	ASSERT(cpuid_checkpass(cpu, 1));
3052 
3053 	if (cpuid_is_cmt(cpu))
3054 		return (snprintf(s, n, fmt_ht, cpi->cpi_chipid,
3055 		    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
3056 		    cpi->cpi_family, cpi->cpi_model,
3057 		    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
3058 	return (snprintf(s, n, fmt,
3059 	    cpi->cpi_vendorstr, cpi->cpi_std[1].cp_eax,
3060 	    cpi->cpi_family, cpi->cpi_model,
3061 	    cpi->cpi_step, cpu->cpu_type_info.pi_clock));
3062 }
3063 
3064 const char *
3065 cpuid_getvendorstr(cpu_t *cpu)
3066 {
3067 	ASSERT(cpuid_checkpass(cpu, 1));
3068 	return ((const char *)cpu->cpu_m.mcpu_cpi->cpi_vendorstr);
3069 }
3070 
3071 uint_t
3072 cpuid_getvendor(cpu_t *cpu)
3073 {
3074 	ASSERT(cpuid_checkpass(cpu, 1));
3075 	return (cpu->cpu_m.mcpu_cpi->cpi_vendor);
3076 }
3077 
3078 uint_t
3079 cpuid_getfamily(cpu_t *cpu)
3080 {
3081 	ASSERT(cpuid_checkpass(cpu, 1));
3082 	return (cpu->cpu_m.mcpu_cpi->cpi_family);
3083 }
3084 
3085 uint_t
3086 cpuid_getmodel(cpu_t *cpu)
3087 {
3088 	ASSERT(cpuid_checkpass(cpu, 1));
3089 	return (cpu->cpu_m.mcpu_cpi->cpi_model);
3090 }
3091 
3092 uint_t
3093 cpuid_get_ncpu_per_chip(cpu_t *cpu)
3094 {
3095 	ASSERT(cpuid_checkpass(cpu, 1));
3096 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_per_chip);
3097 }
3098 
3099 uint_t
3100 cpuid_get_ncore_per_chip(cpu_t *cpu)
3101 {
3102 	ASSERT(cpuid_checkpass(cpu, 1));
3103 	return (cpu->cpu_m.mcpu_cpi->cpi_ncore_per_chip);
3104 }
3105 
3106 uint_t
3107 cpuid_get_ncpu_sharing_last_cache(cpu_t *cpu)
3108 {
3109 	ASSERT(cpuid_checkpass(cpu, 2));
3110 	return (cpu->cpu_m.mcpu_cpi->cpi_ncpu_shr_last_cache);
3111 }
3112 
3113 id_t
3114 cpuid_get_last_lvl_cacheid(cpu_t *cpu)
3115 {
3116 	ASSERT(cpuid_checkpass(cpu, 2));
3117 	return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
3118 }
3119 
3120 uint_t
3121 cpuid_getstep(cpu_t *cpu)
3122 {
3123 	ASSERT(cpuid_checkpass(cpu, 1));
3124 	return (cpu->cpu_m.mcpu_cpi->cpi_step);
3125 }
3126 
3127 uint_t
3128 cpuid_getsig(struct cpu *cpu)
3129 {
3130 	ASSERT(cpuid_checkpass(cpu, 1));
3131 	return (cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_eax);
3132 }
3133 
3134 uint32_t
3135 cpuid_getchiprev(struct cpu *cpu)
3136 {
3137 	ASSERT(cpuid_checkpass(cpu, 1));
3138 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprev);
3139 }
3140 
3141 const char *
3142 cpuid_getchiprevstr(struct cpu *cpu)
3143 {
3144 	ASSERT(cpuid_checkpass(cpu, 1));
3145 	return (cpu->cpu_m.mcpu_cpi->cpi_chiprevstr);
3146 }
3147 
3148 uint32_t
3149 cpuid_getsockettype(struct cpu *cpu)
3150 {
3151 	ASSERT(cpuid_checkpass(cpu, 1));
3152 	return (cpu->cpu_m.mcpu_cpi->cpi_socket);
3153 }
3154 
3155 const char *
3156 cpuid_getsocketstr(cpu_t *cpu)
3157 {
3158 	static const char *socketstr = NULL;
3159 	struct cpuid_info *cpi;
3160 
3161 	ASSERT(cpuid_checkpass(cpu, 1));
3162 	cpi = cpu->cpu_m.mcpu_cpi;
3163 
3164 	/* Assume that socket types are the same across the system */
3165 	if (socketstr == NULL)
3166 		socketstr = _cpuid_sktstr(cpi->cpi_vendor, cpi->cpi_family,
3167 		    cpi->cpi_model, cpi->cpi_step);
3168 
3169 
3170 	return (socketstr);
3171 }
3172 
3173 int
3174 cpuid_get_chipid(cpu_t *cpu)
3175 {
3176 	ASSERT(cpuid_checkpass(cpu, 1));
3177 
3178 	if (cpuid_is_cmt(cpu))
3179 		return (cpu->cpu_m.mcpu_cpi->cpi_chipid);
3180 	return (cpu->cpu_id);
3181 }
3182 
3183 id_t
3184 cpuid_get_coreid(cpu_t *cpu)
3185 {
3186 	ASSERT(cpuid_checkpass(cpu, 1));
3187 	return (cpu->cpu_m.mcpu_cpi->cpi_coreid);
3188 }
3189 
3190 int
3191 cpuid_get_pkgcoreid(cpu_t *cpu)
3192 {
3193 	ASSERT(cpuid_checkpass(cpu, 1));
3194 	return (cpu->cpu_m.mcpu_cpi->cpi_pkgcoreid);
3195 }
3196 
3197 int
3198 cpuid_get_clogid(cpu_t *cpu)
3199 {
3200 	ASSERT(cpuid_checkpass(cpu, 1));
3201 	return (cpu->cpu_m.mcpu_cpi->cpi_clogid);
3202 }
3203 
3204 int
3205 cpuid_get_cacheid(cpu_t *cpu)
3206 {
3207 	ASSERT(cpuid_checkpass(cpu, 1));
3208 	return (cpu->cpu_m.mcpu_cpi->cpi_last_lvl_cacheid);
3209 }
3210 
3211 uint_t
3212 cpuid_get_procnodeid(cpu_t *cpu)
3213 {
3214 	ASSERT(cpuid_checkpass(cpu, 1));
3215 	return (cpu->cpu_m.mcpu_cpi->cpi_procnodeid);
3216 }
3217 
3218 uint_t
3219 cpuid_get_procnodes_per_pkg(cpu_t *cpu)
3220 {
3221 	ASSERT(cpuid_checkpass(cpu, 1));
3222 	return (cpu->cpu_m.mcpu_cpi->cpi_procnodes_per_pkg);
3223 }
3224 
3225 uint_t
3226 cpuid_get_compunitid(cpu_t *cpu)
3227 {
3228 	ASSERT(cpuid_checkpass(cpu, 1));
3229 	return (cpu->cpu_m.mcpu_cpi->cpi_compunitid);
3230 }
3231 
3232 uint_t
3233 cpuid_get_cores_per_compunit(cpu_t *cpu)
3234 {
3235 	ASSERT(cpuid_checkpass(cpu, 1));
3236 	return (cpu->cpu_m.mcpu_cpi->cpi_cores_per_compunit);
3237 }
3238 
3239 /*ARGSUSED*/
3240 int
3241 cpuid_have_cr8access(cpu_t *cpu)
3242 {
3243 #if defined(__amd64)
3244 	return (1);
3245 #else
3246 	struct cpuid_info *cpi;
3247 
3248 	ASSERT(cpu != NULL);
3249 	cpi = cpu->cpu_m.mcpu_cpi;
3250 	if (cpi->cpi_vendor == X86_VENDOR_AMD && cpi->cpi_maxeax >= 1 &&
3251 	    (CPI_FEATURES_XTD_ECX(cpi) & CPUID_AMD_ECX_CR8D) != 0)
3252 		return (1);
3253 	return (0);
3254 #endif
3255 }
3256 
3257 uint32_t
3258 cpuid_get_apicid(cpu_t *cpu)
3259 {
3260 	ASSERT(cpuid_checkpass(cpu, 1));
3261 	if (cpu->cpu_m.mcpu_cpi->cpi_maxeax < 1) {
3262 		return (UINT32_MAX);
3263 	} else {
3264 		return (cpu->cpu_m.mcpu_cpi->cpi_apicid);
3265 	}
3266 }
3267 
3268 void
3269 cpuid_get_addrsize(cpu_t *cpu, uint_t *pabits, uint_t *vabits)
3270 {
3271 	struct cpuid_info *cpi;
3272 
3273 	if (cpu == NULL)
3274 		cpu = CPU;
3275 	cpi = cpu->cpu_m.mcpu_cpi;
3276 
3277 	ASSERT(cpuid_checkpass(cpu, 1));
3278 
3279 	if (pabits)
3280 		*pabits = cpi->cpi_pabits;
3281 	if (vabits)
3282 		*vabits = cpi->cpi_vabits;
3283 }
3284 
3285 /*
3286  * Returns the number of data TLB entries for a corresponding
3287  * pagesize.  If it can't be computed, or isn't known, the
3288  * routine returns zero.  If you ask about an architecturally
3289  * impossible pagesize, the routine will panic (so that the
3290  * hat implementor knows that things are inconsistent.)
3291  */
3292 uint_t
3293 cpuid_get_dtlb_nent(cpu_t *cpu, size_t pagesize)
3294 {
3295 	struct cpuid_info *cpi;
3296 	uint_t dtlb_nent = 0;
3297 
3298 	if (cpu == NULL)
3299 		cpu = CPU;
3300 	cpi = cpu->cpu_m.mcpu_cpi;
3301 
3302 	ASSERT(cpuid_checkpass(cpu, 1));
3303 
3304 	/*
3305 	 * Check the L2 TLB info
3306 	 */
3307 	if (cpi->cpi_xmaxeax >= 0x80000006) {
3308 		struct cpuid_regs *cp = &cpi->cpi_extd[6];
3309 
3310 		switch (pagesize) {
3311 
3312 		case 4 * 1024:
3313 			/*
3314 			 * All zero in the top 16 bits of the register
3315 			 * indicates a unified TLB. Size is in low 16 bits.
3316 			 */
3317 			if ((cp->cp_ebx & 0xffff0000) == 0)
3318 				dtlb_nent = cp->cp_ebx & 0x0000ffff;
3319 			else
3320 				dtlb_nent = BITX(cp->cp_ebx, 27, 16);
3321 			break;
3322 
3323 		case 2 * 1024 * 1024:
3324 			if ((cp->cp_eax & 0xffff0000) == 0)
3325 				dtlb_nent = cp->cp_eax & 0x0000ffff;
3326 			else
3327 				dtlb_nent = BITX(cp->cp_eax, 27, 16);
3328 			break;
3329 
3330 		default:
3331 			panic("unknown L2 pagesize");
3332 			/*NOTREACHED*/
3333 		}
3334 	}
3335 
3336 	if (dtlb_nent != 0)
3337 		return (dtlb_nent);
3338 
3339 	/*
3340 	 * No L2 TLB support for this size, try L1.
3341 	 */
3342 	if (cpi->cpi_xmaxeax >= 0x80000005) {
3343 		struct cpuid_regs *cp = &cpi->cpi_extd[5];
3344 
3345 		switch (pagesize) {
3346 		case 4 * 1024:
3347 			dtlb_nent = BITX(cp->cp_ebx, 23, 16);
3348 			break;
3349 		case 2 * 1024 * 1024:
3350 			dtlb_nent = BITX(cp->cp_eax, 23, 16);
3351 			break;
3352 		default:
3353 			panic("unknown L1 d-TLB pagesize");
3354 			/*NOTREACHED*/
3355 		}
3356 	}
3357 
3358 	return (dtlb_nent);
3359 }
3360 
3361 /*
3362  * Return 0 if the erratum is not present or not applicable, positive
3363  * if it is, and negative if the status of the erratum is unknown.
3364  *
3365  * See "Revision Guide for AMD Athlon(tm) 64 and AMD Opteron(tm)
3366  * Processors" #25759, Rev 3.57, August 2005
3367  */
3368 int
3369 cpuid_opteron_erratum(cpu_t *cpu, uint_t erratum)
3370 {
3371 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
3372 	uint_t eax;
3373 
3374 	/*
3375 	 * Bail out if this CPU isn't an AMD CPU, or if it's
3376 	 * a legacy (32-bit) AMD CPU.
3377 	 */
3378 	if (cpi->cpi_vendor != X86_VENDOR_AMD ||
3379 	    cpi->cpi_family == 4 || cpi->cpi_family == 5 ||
3380 	    cpi->cpi_family == 6)
3381 
3382 		return (0);
3383 
3384 	eax = cpi->cpi_std[1].cp_eax;
3385 
3386 #define	SH_B0(eax)	(eax == 0xf40 || eax == 0xf50)
3387 #define	SH_B3(eax) 	(eax == 0xf51)
3388 #define	B(eax)		(SH_B0(eax) || SH_B3(eax))
3389 
3390 #define	SH_C0(eax)	(eax == 0xf48 || eax == 0xf58)
3391 
3392 #define	SH_CG(eax)	(eax == 0xf4a || eax == 0xf5a || eax == 0xf7a)
3393 #define	DH_CG(eax)	(eax == 0xfc0 || eax == 0xfe0 || eax == 0xff0)
3394 #define	CH_CG(eax)	(eax == 0xf82 || eax == 0xfb2)
3395 #define	CG(eax)		(SH_CG(eax) || DH_CG(eax) || CH_CG(eax))
3396 
3397 #define	SH_D0(eax)	(eax == 0x10f40 || eax == 0x10f50 || eax == 0x10f70)
3398 #define	DH_D0(eax)	(eax == 0x10fc0 || eax == 0x10ff0)
3399 #define	CH_D0(eax)	(eax == 0x10f80 || eax == 0x10fb0)
3400 #define	D0(eax)		(SH_D0(eax) || DH_D0(eax) || CH_D0(eax))
3401 
3402 #define	SH_E0(eax)	(eax == 0x20f50 || eax == 0x20f40 || eax == 0x20f70)
3403 #define	JH_E1(eax)	(eax == 0x20f10)	/* JH8_E0 had 0x20f30 */
3404 #define	DH_E3(eax)	(eax == 0x20fc0 || eax == 0x20ff0)
3405 #define	SH_E4(eax)	(eax == 0x20f51 || eax == 0x20f71)
3406 #define	BH_E4(eax)	(eax == 0x20fb1)
3407 #define	SH_E5(eax)	(eax == 0x20f42)
3408 #define	DH_E6(eax)	(eax == 0x20ff2 || eax == 0x20fc2)
3409 #define	JH_E6(eax)	(eax == 0x20f12 || eax == 0x20f32)
3410 #define	EX(eax)		(SH_E0(eax) || JH_E1(eax) || DH_E3(eax) || \
3411 			    SH_E4(eax) || BH_E4(eax) || SH_E5(eax) || \
3412 			    DH_E6(eax) || JH_E6(eax))
3413 
3414 #define	DR_AX(eax)	(eax == 0x100f00 || eax == 0x100f01 || eax == 0x100f02)
3415 #define	DR_B0(eax)	(eax == 0x100f20)
3416 #define	DR_B1(eax)	(eax == 0x100f21)
3417 #define	DR_BA(eax)	(eax == 0x100f2a)
3418 #define	DR_B2(eax)	(eax == 0x100f22)
3419 #define	DR_B3(eax)	(eax == 0x100f23)
3420 #define	RB_C0(eax)	(eax == 0x100f40)
3421 
3422 	switch (erratum) {
3423 	case 1:
3424 		return (cpi->cpi_family < 0x10);
3425 	case 51:	/* what does the asterisk mean? */
3426 		return (B(eax) || SH_C0(eax) || CG(eax));
3427 	case 52:
3428 		return (B(eax));
3429 	case 57:
3430 		return (cpi->cpi_family <= 0x11);
3431 	case 58:
3432 		return (B(eax));
3433 	case 60:
3434 		return (cpi->cpi_family <= 0x11);
3435 	case 61:
3436 	case 62:
3437 	case 63:
3438 	case 64:
3439 	case 65:
3440 	case 66:
3441 	case 68:
3442 	case 69:
3443 	case 70:
3444 	case 71:
3445 		return (B(eax));
3446 	case 72:
3447 		return (SH_B0(eax));
3448 	case 74:
3449 		return (B(eax));
3450 	case 75:
3451 		return (cpi->cpi_family < 0x10);
3452 	case 76:
3453 		return (B(eax));
3454 	case 77:
3455 		return (cpi->cpi_family <= 0x11);
3456 	case 78:
3457 		return (B(eax) || SH_C0(eax));
3458 	case 79:
3459 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
3460 	case 80:
3461 	case 81:
3462 	case 82:
3463 		return (B(eax));
3464 	case 83:
3465 		return (B(eax) || SH_C0(eax) || CG(eax));
3466 	case 85:
3467 		return (cpi->cpi_family < 0x10);
3468 	case 86:
3469 		return (SH_C0(eax) || CG(eax));
3470 	case 88:
3471 #if !defined(__amd64)
3472 		return (0);
3473 #else
3474 		return (B(eax) || SH_C0(eax));
3475 #endif
3476 	case 89:
3477 		return (cpi->cpi_family < 0x10);
3478 	case 90:
3479 		return (B(eax) || SH_C0(eax) || CG(eax));
3480 	case 91:
3481 	case 92:
3482 		return (B(eax) || SH_C0(eax));
3483 	case 93:
3484 		return (SH_C0(eax));
3485 	case 94:
3486 		return (B(eax) || SH_C0(eax) || CG(eax));
3487 	case 95:
3488 #if !defined(__amd64)
3489 		return (0);
3490 #else
3491 		return (B(eax) || SH_C0(eax));
3492 #endif
3493 	case 96:
3494 		return (B(eax) || SH_C0(eax) || CG(eax));
3495 	case 97:
3496 	case 98:
3497 		return (SH_C0(eax) || CG(eax));
3498 	case 99:
3499 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
3500 	case 100:
3501 		return (B(eax) || SH_C0(eax));
3502 	case 101:
3503 	case 103:
3504 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
3505 	case 104:
3506 		return (SH_C0(eax) || CG(eax) || D0(eax));
3507 	case 105:
3508 	case 106:
3509 	case 107:
3510 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
3511 	case 108:
3512 		return (DH_CG(eax));
3513 	case 109:
3514 		return (SH_C0(eax) || CG(eax) || D0(eax));
3515 	case 110:
3516 		return (D0(eax) || EX(eax));
3517 	case 111:
3518 		return (CG(eax));
3519 	case 112:
3520 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
3521 	case 113:
3522 		return (eax == 0x20fc0);
3523 	case 114:
3524 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
3525 	case 115:
3526 		return (SH_E0(eax) || JH_E1(eax));
3527 	case 116:
3528 		return (SH_E0(eax) || JH_E1(eax) || DH_E3(eax));
3529 	case 117:
3530 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax));
3531 	case 118:
3532 		return (SH_E0(eax) || JH_E1(eax) || SH_E4(eax) || BH_E4(eax) ||
3533 		    JH_E6(eax));
3534 	case 121:
3535 		return (B(eax) || SH_C0(eax) || CG(eax) || D0(eax) || EX(eax));
3536 	case 122:
3537 		return (cpi->cpi_family < 0x10 || cpi->cpi_family == 0x11);
3538 	case 123:
3539 		return (JH_E1(eax) || BH_E4(eax) || JH_E6(eax));
3540 	case 131:
3541 		return (cpi->cpi_family < 0x10);
3542 	case 6336786:
3543 		/*
3544 		 * Test for AdvPowerMgmtInfo.TscPStateInvariant
3545 		 * if this is a K8 family or newer processor
3546 		 */
3547 		if (CPI_FAMILY(cpi) == 0xf) {
3548 			struct cpuid_regs regs;
3549 			regs.cp_eax = 0x80000007;
3550 			(void) __cpuid_insn(&regs);
3551 			return (!(regs.cp_edx & 0x100));
3552 		}
3553 		return (0);
3554 	case 6323525:
3555 		return (((((eax >> 12) & 0xff00) + (eax & 0xf00)) |
3556 		    (((eax >> 4) & 0xf) | ((eax >> 12) & 0xf0))) < 0xf40);
3557 
3558 	case 6671130:
3559 		/*
3560 		 * check for processors (pre-Shanghai) that do not provide
3561 		 * optimal management of 1gb ptes in its tlb.
3562 		 */
3563 		return (cpi->cpi_family == 0x10 && cpi->cpi_model < 4);
3564 
3565 	case 298:
3566 		return (DR_AX(eax) || DR_B0(eax) || DR_B1(eax) || DR_BA(eax) ||
3567 		    DR_B2(eax) || RB_C0(eax));
3568 
3569 	case 721:
3570 #if defined(__amd64)
3571 		return (cpi->cpi_family == 0x10 || cpi->cpi_family == 0x12);
3572 #else
3573 		return (0);
3574 #endif
3575 
3576 	default:
3577 		return (-1);
3578 
3579 	}
3580 }
3581 
3582 /*
3583  * Determine if specified erratum is present via OSVW (OS Visible Workaround).
3584  * Return 1 if erratum is present, 0 if not present and -1 if indeterminate.
3585  */
3586 int
3587 osvw_opteron_erratum(cpu_t *cpu, uint_t erratum)
3588 {
3589 	struct cpuid_info	*cpi;
3590 	uint_t			osvwid;
3591 	static int		osvwfeature = -1;
3592 	uint64_t		osvwlength;
3593 
3594 
3595 	cpi = cpu->cpu_m.mcpu_cpi;
3596 
3597 	/* confirm OSVW supported */
3598 	if (osvwfeature == -1) {
3599 		osvwfeature = cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW;
3600 	} else {
3601 		/* assert that osvw feature setting is consistent on all cpus */
3602 		ASSERT(osvwfeature ==
3603 		    (cpi->cpi_extd[1].cp_ecx & CPUID_AMD_ECX_OSVW));
3604 	}
3605 	if (!osvwfeature)
3606 		return (-1);
3607 
3608 	osvwlength = rdmsr(MSR_AMD_OSVW_ID_LEN) & OSVW_ID_LEN_MASK;
3609 
3610 	switch (erratum) {
3611 	case 298:	/* osvwid is 0 */
3612 		osvwid = 0;
3613 		if (osvwlength <= (uint64_t)osvwid) {
3614 			/* osvwid 0 is unknown */
3615 			return (-1);
3616 		}
3617 
3618 		/*
3619 		 * Check the OSVW STATUS MSR to determine the state
3620 		 * of the erratum where:
3621 		 *   0 - fixed by HW
3622 		 *   1 - BIOS has applied the workaround when BIOS
3623 		 *   workaround is available. (Or for other errata,
3624 		 *   OS workaround is required.)
3625 		 * For a value of 1, caller will confirm that the
3626 		 * erratum 298 workaround has indeed been applied by BIOS.
3627 		 *
3628 		 * A 1 may be set in cpus that have a HW fix
3629 		 * in a mixed cpu system. Regarding erratum 298:
3630 		 *   In a multiprocessor platform, the workaround above
3631 		 *   should be applied to all processors regardless of
3632 		 *   silicon revision when an affected processor is
3633 		 *   present.
3634 		 */
3635 
3636 		return (rdmsr(MSR_AMD_OSVW_STATUS +
3637 		    (osvwid / OSVW_ID_CNT_PER_MSR)) &
3638 		    (1ULL << (osvwid % OSVW_ID_CNT_PER_MSR)));
3639 
3640 	default:
3641 		return (-1);
3642 	}
3643 }
3644 
3645 static const char assoc_str[] = "associativity";
3646 static const char line_str[] = "line-size";
3647 static const char size_str[] = "size";
3648 
3649 static void
3650 add_cache_prop(dev_info_t *devi, const char *label, const char *type,
3651     uint32_t val)
3652 {
3653 	char buf[128];
3654 
3655 	/*
3656 	 * ndi_prop_update_int() is used because it is desirable for
3657 	 * DDI_PROP_HW_DEF and DDI_PROP_DONTSLEEP to be set.
3658 	 */
3659 	if (snprintf(buf, sizeof (buf), "%s-%s", label, type) < sizeof (buf))
3660 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi, buf, val);
3661 }
3662 
3663 /*
3664  * Intel-style cache/tlb description
3665  *
3666  * Standard cpuid level 2 gives a randomly ordered
3667  * selection of tags that index into a table that describes
3668  * cache and tlb properties.
3669  */
3670 
3671 static const char l1_icache_str[] = "l1-icache";
3672 static const char l1_dcache_str[] = "l1-dcache";
3673 static const char l2_cache_str[] = "l2-cache";
3674 static const char l3_cache_str[] = "l3-cache";
3675 static const char itlb4k_str[] = "itlb-4K";
3676 static const char dtlb4k_str[] = "dtlb-4K";
3677 static const char itlb2M_str[] = "itlb-2M";
3678 static const char itlb4M_str[] = "itlb-4M";
3679 static const char dtlb4M_str[] = "dtlb-4M";
3680 static const char dtlb24_str[] = "dtlb0-2M-4M";
3681 static const char itlb424_str[] = "itlb-4K-2M-4M";
3682 static const char itlb24_str[] = "itlb-2M-4M";
3683 static const char dtlb44_str[] = "dtlb-4K-4M";
3684 static const char sl1_dcache_str[] = "sectored-l1-dcache";
3685 static const char sl2_cache_str[] = "sectored-l2-cache";
3686 static const char itrace_str[] = "itrace-cache";
3687 static const char sl3_cache_str[] = "sectored-l3-cache";
3688 static const char sh_l2_tlb4k_str[] = "shared-l2-tlb-4k";
3689 
3690 static const struct cachetab {
3691 	uint8_t 	ct_code;
3692 	uint8_t		ct_assoc;
3693 	uint16_t 	ct_line_size;
3694 	size_t		ct_size;
3695 	const char	*ct_label;
3696 } intel_ctab[] = {
3697 	/*
3698 	 * maintain descending order!
3699 	 *
3700 	 * Codes ignored - Reason
3701 	 * ----------------------
3702 	 * 40H - intel_cpuid_4_cache_info() disambiguates l2/l3 cache
3703 	 * f0H/f1H - Currently we do not interpret prefetch size by design
3704 	 */
3705 	{ 0xe4, 16, 64, 8*1024*1024, l3_cache_str},
3706 	{ 0xe3, 16, 64, 4*1024*1024, l3_cache_str},
3707 	{ 0xe2, 16, 64, 2*1024*1024, l3_cache_str},
3708 	{ 0xde, 12, 64, 6*1024*1024, l3_cache_str},
3709 	{ 0xdd, 12, 64, 3*1024*1024, l3_cache_str},
3710 	{ 0xdc, 12, 64, ((1*1024*1024)+(512*1024)), l3_cache_str},
3711 	{ 0xd8, 8, 64, 4*1024*1024, l3_cache_str},
3712 	{ 0xd7, 8, 64, 2*1024*1024, l3_cache_str},
3713 	{ 0xd6, 8, 64, 1*1024*1024, l3_cache_str},
3714 	{ 0xd2, 4, 64, 2*1024*1024, l3_cache_str},
3715 	{ 0xd1, 4, 64, 1*1024*1024, l3_cache_str},
3716 	{ 0xd0, 4, 64, 512*1024, l3_cache_str},
3717 	{ 0xca, 4, 0, 512, sh_l2_tlb4k_str},
3718 	{ 0xc0, 4, 0, 8, dtlb44_str },
3719 	{ 0xba, 4, 0, 64, dtlb4k_str },
3720 	{ 0xb4, 4, 0, 256, dtlb4k_str },
3721 	{ 0xb3, 4, 0, 128, dtlb4k_str },
3722 	{ 0xb2, 4, 0, 64, itlb4k_str },
3723 	{ 0xb0, 4, 0, 128, itlb4k_str },
3724 	{ 0x87, 8, 64, 1024*1024, l2_cache_str},
3725 	{ 0x86, 4, 64, 512*1024, l2_cache_str},
3726 	{ 0x85, 8, 32, 2*1024*1024, l2_cache_str},
3727 	{ 0x84, 8, 32, 1024*1024, l2_cache_str},
3728 	{ 0x83, 8, 32, 512*1024, l2_cache_str},
3729 	{ 0x82, 8, 32, 256*1024, l2_cache_str},
3730 	{ 0x80, 8, 64, 512*1024, l2_cache_str},
3731 	{ 0x7f, 2, 64, 512*1024, l2_cache_str},
3732 	{ 0x7d, 8, 64, 2*1024*1024, sl2_cache_str},
3733 	{ 0x7c, 8, 64, 1024*1024, sl2_cache_str},
3734 	{ 0x7b, 8, 64, 512*1024, sl2_cache_str},
3735 	{ 0x7a, 8, 64, 256*1024, sl2_cache_str},
3736 	{ 0x79, 8, 64, 128*1024, sl2_cache_str},
3737 	{ 0x78, 8, 64, 1024*1024, l2_cache_str},
3738 	{ 0x73, 8, 0, 64*1024, itrace_str},
3739 	{ 0x72, 8, 0, 32*1024, itrace_str},
3740 	{ 0x71, 8, 0, 16*1024, itrace_str},
3741 	{ 0x70, 8, 0, 12*1024, itrace_str},
3742 	{ 0x68, 4, 64, 32*1024, sl1_dcache_str},
3743 	{ 0x67, 4, 64, 16*1024, sl1_dcache_str},
3744 	{ 0x66, 4, 64, 8*1024, sl1_dcache_str},
3745 	{ 0x60, 8, 64, 16*1024, sl1_dcache_str},
3746 	{ 0x5d, 0, 0, 256, dtlb44_str},
3747 	{ 0x5c, 0, 0, 128, dtlb44_str},
3748 	{ 0x5b, 0, 0, 64, dtlb44_str},
3749 	{ 0x5a, 4, 0, 32, dtlb24_str},
3750 	{ 0x59, 0, 0, 16, dtlb4k_str},
3751 	{ 0x57, 4, 0, 16, dtlb4k_str},
3752 	{ 0x56, 4, 0, 16, dtlb4M_str},
3753 	{ 0x55, 0, 0, 7, itlb24_str},
3754 	{ 0x52, 0, 0, 256, itlb424_str},
3755 	{ 0x51, 0, 0, 128, itlb424_str},
3756 	{ 0x50, 0, 0, 64, itlb424_str},
3757 	{ 0x4f, 0, 0, 32, itlb4k_str},
3758 	{ 0x4e, 24, 64, 6*1024*1024, l2_cache_str},
3759 	{ 0x4d, 16, 64, 16*1024*1024, l3_cache_str},
3760 	{ 0x4c, 12, 64, 12*1024*1024, l3_cache_str},
3761 	{ 0x4b, 16, 64, 8*1024*1024, l3_cache_str},
3762 	{ 0x4a, 12, 64, 6*1024*1024, l3_cache_str},
3763 	{ 0x49, 16, 64, 4*1024*1024, l3_cache_str},
3764 	{ 0x48, 12, 64, 3*1024*1024, l2_cache_str},
3765 	{ 0x47, 8, 64, 8*1024*1024, l3_cache_str},
3766 	{ 0x46, 4, 64, 4*1024*1024, l3_cache_str},
3767 	{ 0x45, 4, 32, 2*1024*1024, l2_cache_str},
3768 	{ 0x44, 4, 32, 1024*1024, l2_cache_str},
3769 	{ 0x43, 4, 32, 512*1024, l2_cache_str},
3770 	{ 0x42, 4, 32, 256*1024, l2_cache_str},
3771 	{ 0x41, 4, 32, 128*1024, l2_cache_str},
3772 	{ 0x3e, 4, 64, 512*1024, sl2_cache_str},
3773 	{ 0x3d, 6, 64, 384*1024, sl2_cache_str},
3774 	{ 0x3c, 4, 64, 256*1024, sl2_cache_str},
3775 	{ 0x3b, 2, 64, 128*1024, sl2_cache_str},
3776 	{ 0x3a, 6, 64, 192*1024, sl2_cache_str},
3777 	{ 0x39, 4, 64, 128*1024, sl2_cache_str},
3778 	{ 0x30, 8, 64, 32*1024, l1_icache_str},
3779 	{ 0x2c, 8, 64, 32*1024, l1_dcache_str},
3780 	{ 0x29, 8, 64, 4096*1024, sl3_cache_str},
3781 	{ 0x25, 8, 64, 2048*1024, sl3_cache_str},
3782 	{ 0x23, 8, 64, 1024*1024, sl3_cache_str},
3783 	{ 0x22, 4, 64, 512*1024, sl3_cache_str},
3784 	{ 0x0e, 6, 64, 24*1024, l1_dcache_str},
3785 	{ 0x0d, 4, 32, 16*1024, l1_dcache_str},
3786 	{ 0x0c, 4, 32, 16*1024, l1_dcache_str},
3787 	{ 0x0b, 4, 0, 4, itlb4M_str},
3788 	{ 0x0a, 2, 32, 8*1024, l1_dcache_str},
3789 	{ 0x08, 4, 32, 16*1024, l1_icache_str},
3790 	{ 0x06, 4, 32, 8*1024, l1_icache_str},
3791 	{ 0x05, 4, 0, 32, dtlb4M_str},
3792 	{ 0x04, 4, 0, 8, dtlb4M_str},
3793 	{ 0x03, 4, 0, 64, dtlb4k_str},
3794 	{ 0x02, 4, 0, 2, itlb4M_str},
3795 	{ 0x01, 4, 0, 32, itlb4k_str},
3796 	{ 0 }
3797 };
3798 
3799 static const struct cachetab cyrix_ctab[] = {
3800 	{ 0x70, 4, 0, 32, "tlb-4K" },
3801 	{ 0x80, 4, 16, 16*1024, "l1-cache" },
3802 	{ 0 }
3803 };
3804 
3805 /*
3806  * Search a cache table for a matching entry
3807  */
3808 static const struct cachetab *
3809 find_cacheent(const struct cachetab *ct, uint_t code)
3810 {
3811 	if (code != 0) {
3812 		for (; ct->ct_code != 0; ct++)
3813 			if (ct->ct_code <= code)
3814 				break;
3815 		if (ct->ct_code == code)
3816 			return (ct);
3817 	}
3818 	return (NULL);
3819 }
3820 
3821 /*
3822  * Populate cachetab entry with L2 or L3 cache-information using
3823  * cpuid function 4. This function is called from intel_walk_cacheinfo()
3824  * when descriptor 0x49 is encountered. It returns 0 if no such cache
3825  * information is found.
3826  */
3827 static int
3828 intel_cpuid_4_cache_info(struct cachetab *ct, struct cpuid_info *cpi)
3829 {
3830 	uint32_t level, i;
3831 	int ret = 0;
3832 
3833 	for (i = 0; i < cpi->cpi_std_4_size; i++) {
3834 		level = CPI_CACHE_LVL(cpi->cpi_std_4[i]);
3835 
3836 		if (level == 2 || level == 3) {
3837 			ct->ct_assoc = CPI_CACHE_WAYS(cpi->cpi_std_4[i]) + 1;
3838 			ct->ct_line_size =
3839 			    CPI_CACHE_COH_LN_SZ(cpi->cpi_std_4[i]) + 1;
3840 			ct->ct_size = ct->ct_assoc *
3841 			    (CPI_CACHE_PARTS(cpi->cpi_std_4[i]) + 1) *
3842 			    ct->ct_line_size *
3843 			    (cpi->cpi_std_4[i]->cp_ecx + 1);
3844 
3845 			if (level == 2) {
3846 				ct->ct_label = l2_cache_str;
3847 			} else if (level == 3) {
3848 				ct->ct_label = l3_cache_str;
3849 			}
3850 			ret = 1;
3851 		}
3852 	}
3853 
3854 	return (ret);
3855 }
3856 
3857 /*
3858  * Walk the cacheinfo descriptor, applying 'func' to every valid element
3859  * The walk is terminated if the walker returns non-zero.
3860  */
3861 static void
3862 intel_walk_cacheinfo(struct cpuid_info *cpi,
3863     void *arg, int (*func)(void *, const struct cachetab *))
3864 {
3865 	const struct cachetab *ct;
3866 	struct cachetab des_49_ct, des_b1_ct;
3867 	uint8_t *dp;
3868 	int i;
3869 
3870 	if ((dp = cpi->cpi_cacheinfo) == NULL)
3871 		return;
3872 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
3873 		/*
3874 		 * For overloaded descriptor 0x49 we use cpuid function 4
3875 		 * if supported by the current processor, to create
3876 		 * cache information.
3877 		 * For overloaded descriptor 0xb1 we use X86_PAE flag
3878 		 * to disambiguate the cache information.
3879 		 */
3880 		if (*dp == 0x49 && cpi->cpi_maxeax >= 0x4 &&
3881 		    intel_cpuid_4_cache_info(&des_49_ct, cpi) == 1) {
3882 				ct = &des_49_ct;
3883 		} else if (*dp == 0xb1) {
3884 			des_b1_ct.ct_code = 0xb1;
3885 			des_b1_ct.ct_assoc = 4;
3886 			des_b1_ct.ct_line_size = 0;
3887 			if (is_x86_feature(x86_featureset, X86FSET_PAE)) {
3888 				des_b1_ct.ct_size = 8;
3889 				des_b1_ct.ct_label = itlb2M_str;
3890 			} else {
3891 				des_b1_ct.ct_size = 4;
3892 				des_b1_ct.ct_label = itlb4M_str;
3893 			}
3894 			ct = &des_b1_ct;
3895 		} else {
3896 			if ((ct = find_cacheent(intel_ctab, *dp)) == NULL) {
3897 				continue;
3898 			}
3899 		}
3900 
3901 		if (func(arg, ct) != 0) {
3902 			break;
3903 		}
3904 	}
3905 }
3906 
3907 /*
3908  * (Like the Intel one, except for Cyrix CPUs)
3909  */
3910 static void
3911 cyrix_walk_cacheinfo(struct cpuid_info *cpi,
3912     void *arg, int (*func)(void *, const struct cachetab *))
3913 {
3914 	const struct cachetab *ct;
3915 	uint8_t *dp;
3916 	int i;
3917 
3918 	if ((dp = cpi->cpi_cacheinfo) == NULL)
3919 		return;
3920 	for (i = 0; i < cpi->cpi_ncache; i++, dp++) {
3921 		/*
3922 		 * Search Cyrix-specific descriptor table first ..
3923 		 */
3924 		if ((ct = find_cacheent(cyrix_ctab, *dp)) != NULL) {
3925 			if (func(arg, ct) != 0)
3926 				break;
3927 			continue;
3928 		}
3929 		/*
3930 		 * .. else fall back to the Intel one
3931 		 */
3932 		if ((ct = find_cacheent(intel_ctab, *dp)) != NULL) {
3933 			if (func(arg, ct) != 0)
3934 				break;
3935 			continue;
3936 		}
3937 	}
3938 }
3939 
3940 /*
3941  * A cacheinfo walker that adds associativity, line-size, and size properties
3942  * to the devinfo node it is passed as an argument.
3943  */
3944 static int
3945 add_cacheent_props(void *arg, const struct cachetab *ct)
3946 {
3947 	dev_info_t *devi = arg;
3948 
3949 	add_cache_prop(devi, ct->ct_label, assoc_str, ct->ct_assoc);
3950 	if (ct->ct_line_size != 0)
3951 		add_cache_prop(devi, ct->ct_label, line_str,
3952 		    ct->ct_line_size);
3953 	add_cache_prop(devi, ct->ct_label, size_str, ct->ct_size);
3954 	return (0);
3955 }
3956 
3957 
3958 static const char fully_assoc[] = "fully-associative?";
3959 
3960 /*
3961  * AMD style cache/tlb description
3962  *
3963  * Extended functions 5 and 6 directly describe properties of
3964  * tlbs and various cache levels.
3965  */
3966 static void
3967 add_amd_assoc(dev_info_t *devi, const char *label, uint_t assoc)
3968 {
3969 	switch (assoc) {
3970 	case 0:	/* reserved; ignore */
3971 		break;
3972 	default:
3973 		add_cache_prop(devi, label, assoc_str, assoc);
3974 		break;
3975 	case 0xff:
3976 		add_cache_prop(devi, label, fully_assoc, 1);
3977 		break;
3978 	}
3979 }
3980 
3981 static void
3982 add_amd_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
3983 {
3984 	if (size == 0)
3985 		return;
3986 	add_cache_prop(devi, label, size_str, size);
3987 	add_amd_assoc(devi, label, assoc);
3988 }
3989 
3990 static void
3991 add_amd_cache(dev_info_t *devi, const char *label,
3992     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
3993 {
3994 	if (size == 0 || line_size == 0)
3995 		return;
3996 	add_amd_assoc(devi, label, assoc);
3997 	/*
3998 	 * Most AMD parts have a sectored cache. Multiple cache lines are
3999 	 * associated with each tag. A sector consists of all cache lines
4000 	 * associated with a tag. For example, the AMD K6-III has a sector
4001 	 * size of 2 cache lines per tag.
4002 	 */
4003 	if (lines_per_tag != 0)
4004 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
4005 	add_cache_prop(devi, label, line_str, line_size);
4006 	add_cache_prop(devi, label, size_str, size * 1024);
4007 }
4008 
4009 static void
4010 add_amd_l2_assoc(dev_info_t *devi, const char *label, uint_t assoc)
4011 {
4012 	switch (assoc) {
4013 	case 0:	/* off */
4014 		break;
4015 	case 1:
4016 	case 2:
4017 	case 4:
4018 		add_cache_prop(devi, label, assoc_str, assoc);
4019 		break;
4020 	case 6:
4021 		add_cache_prop(devi, label, assoc_str, 8);
4022 		break;
4023 	case 8:
4024 		add_cache_prop(devi, label, assoc_str, 16);
4025 		break;
4026 	case 0xf:
4027 		add_cache_prop(devi, label, fully_assoc, 1);
4028 		break;
4029 	default: /* reserved; ignore */
4030 		break;
4031 	}
4032 }
4033 
4034 static void
4035 add_amd_l2_tlb(dev_info_t *devi, const char *label, uint_t assoc, uint_t size)
4036 {
4037 	if (size == 0 || assoc == 0)
4038 		return;
4039 	add_amd_l2_assoc(devi, label, assoc);
4040 	add_cache_prop(devi, label, size_str, size);
4041 }
4042 
4043 static void
4044 add_amd_l2_cache(dev_info_t *devi, const char *label,
4045     uint_t size, uint_t assoc, uint_t lines_per_tag, uint_t line_size)
4046 {
4047 	if (size == 0 || assoc == 0 || line_size == 0)
4048 		return;
4049 	add_amd_l2_assoc(devi, label, assoc);
4050 	if (lines_per_tag != 0)
4051 		add_cache_prop(devi, label, "lines-per-tag", lines_per_tag);
4052 	add_cache_prop(devi, label, line_str, line_size);
4053 	add_cache_prop(devi, label, size_str, size * 1024);
4054 }
4055 
4056 static void
4057 amd_cache_info(struct cpuid_info *cpi, dev_info_t *devi)
4058 {
4059 	struct cpuid_regs *cp;
4060 
4061 	if (cpi->cpi_xmaxeax < 0x80000005)
4062 		return;
4063 	cp = &cpi->cpi_extd[5];
4064 
4065 	/*
4066 	 * 4M/2M L1 TLB configuration
4067 	 *
4068 	 * We report the size for 2M pages because AMD uses two
4069 	 * TLB entries for one 4M page.
4070 	 */
4071 	add_amd_tlb(devi, "dtlb-2M",
4072 	    BITX(cp->cp_eax, 31, 24), BITX(cp->cp_eax, 23, 16));
4073 	add_amd_tlb(devi, "itlb-2M",
4074 	    BITX(cp->cp_eax, 15, 8), BITX(cp->cp_eax, 7, 0));
4075 
4076 	/*
4077 	 * 4K L1 TLB configuration
4078 	 */
4079 
4080 	switch (cpi->cpi_vendor) {
4081 		uint_t nentries;
4082 	case X86_VENDOR_TM:
4083 		if (cpi->cpi_family >= 5) {
4084 			/*
4085 			 * Crusoe processors have 256 TLB entries, but
4086 			 * cpuid data format constrains them to only
4087 			 * reporting 255 of them.
4088 			 */
4089 			if ((nentries = BITX(cp->cp_ebx, 23, 16)) == 255)
4090 				nentries = 256;
4091 			/*
4092 			 * Crusoe processors also have a unified TLB
4093 			 */
4094 			add_amd_tlb(devi, "tlb-4K", BITX(cp->cp_ebx, 31, 24),
4095 			    nentries);
4096 			break;
4097 		}
4098 		/*FALLTHROUGH*/
4099 	default:
4100 		add_amd_tlb(devi, itlb4k_str,
4101 		    BITX(cp->cp_ebx, 31, 24), BITX(cp->cp_ebx, 23, 16));
4102 		add_amd_tlb(devi, dtlb4k_str,
4103 		    BITX(cp->cp_ebx, 15, 8), BITX(cp->cp_ebx, 7, 0));
4104 		break;
4105 	}
4106 
4107 	/*
4108 	 * data L1 cache configuration
4109 	 */
4110 
4111 	add_amd_cache(devi, l1_dcache_str,
4112 	    BITX(cp->cp_ecx, 31, 24), BITX(cp->cp_ecx, 23, 16),
4113 	    BITX(cp->cp_ecx, 15, 8), BITX(cp->cp_ecx, 7, 0));
4114 
4115 	/*
4116 	 * code L1 cache configuration
4117 	 */
4118 
4119 	add_amd_cache(devi, l1_icache_str,
4120 	    BITX(cp->cp_edx, 31, 24), BITX(cp->cp_edx, 23, 16),
4121 	    BITX(cp->cp_edx, 15, 8), BITX(cp->cp_edx, 7, 0));
4122 
4123 	if (cpi->cpi_xmaxeax < 0x80000006)
4124 		return;
4125 	cp = &cpi->cpi_extd[6];
4126 
4127 	/* Check for a unified L2 TLB for large pages */
4128 
4129 	if (BITX(cp->cp_eax, 31, 16) == 0)
4130 		add_amd_l2_tlb(devi, "l2-tlb-2M",
4131 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
4132 	else {
4133 		add_amd_l2_tlb(devi, "l2-dtlb-2M",
4134 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
4135 		add_amd_l2_tlb(devi, "l2-itlb-2M",
4136 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
4137 	}
4138 
4139 	/* Check for a unified L2 TLB for 4K pages */
4140 
4141 	if (BITX(cp->cp_ebx, 31, 16) == 0) {
4142 		add_amd_l2_tlb(devi, "l2-tlb-4K",
4143 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
4144 	} else {
4145 		add_amd_l2_tlb(devi, "l2-dtlb-4K",
4146 		    BITX(cp->cp_eax, 31, 28), BITX(cp->cp_eax, 27, 16));
4147 		add_amd_l2_tlb(devi, "l2-itlb-4K",
4148 		    BITX(cp->cp_eax, 15, 12), BITX(cp->cp_eax, 11, 0));
4149 	}
4150 
4151 	add_amd_l2_cache(devi, l2_cache_str,
4152 	    BITX(cp->cp_ecx, 31, 16), BITX(cp->cp_ecx, 15, 12),
4153 	    BITX(cp->cp_ecx, 11, 8), BITX(cp->cp_ecx, 7, 0));
4154 }
4155 
4156 /*
4157  * There are two basic ways that the x86 world describes it cache
4158  * and tlb architecture - Intel's way and AMD's way.
4159  *
4160  * Return which flavor of cache architecture we should use
4161  */
4162 static int
4163 x86_which_cacheinfo(struct cpuid_info *cpi)
4164 {
4165 	switch (cpi->cpi_vendor) {
4166 	case X86_VENDOR_Intel:
4167 		if (cpi->cpi_maxeax >= 2)
4168 			return (X86_VENDOR_Intel);
4169 		break;
4170 	case X86_VENDOR_AMD:
4171 		/*
4172 		 * The K5 model 1 was the first part from AMD that reported
4173 		 * cache sizes via extended cpuid functions.
4174 		 */
4175 		if (cpi->cpi_family > 5 ||
4176 		    (cpi->cpi_family == 5 && cpi->cpi_model >= 1))
4177 			return (X86_VENDOR_AMD);
4178 		break;
4179 	case X86_VENDOR_TM:
4180 		if (cpi->cpi_family >= 5)
4181 			return (X86_VENDOR_AMD);
4182 		/*FALLTHROUGH*/
4183 	default:
4184 		/*
4185 		 * If they have extended CPU data for 0x80000005
4186 		 * then we assume they have AMD-format cache
4187 		 * information.
4188 		 *
4189 		 * If not, and the vendor happens to be Cyrix,
4190 		 * then try our-Cyrix specific handler.
4191 		 *
4192 		 * If we're not Cyrix, then assume we're using Intel's
4193 		 * table-driven format instead.
4194 		 */
4195 		if (cpi->cpi_xmaxeax >= 0x80000005)
4196 			return (X86_VENDOR_AMD);
4197 		else if (cpi->cpi_vendor == X86_VENDOR_Cyrix)
4198 			return (X86_VENDOR_Cyrix);
4199 		else if (cpi->cpi_maxeax >= 2)
4200 			return (X86_VENDOR_Intel);
4201 		break;
4202 	}
4203 	return (-1);
4204 }
4205 
4206 void
4207 cpuid_set_cpu_properties(void *dip, processorid_t cpu_id,
4208     struct cpuid_info *cpi)
4209 {
4210 	dev_info_t *cpu_devi;
4211 	int create;
4212 
4213 	cpu_devi = (dev_info_t *)dip;
4214 
4215 	/* device_type */
4216 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
4217 	    "device_type", "cpu");
4218 
4219 	/* reg */
4220 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4221 	    "reg", cpu_id);
4222 
4223 	/* cpu-mhz, and clock-frequency */
4224 	if (cpu_freq > 0) {
4225 		long long mul;
4226 
4227 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4228 		    "cpu-mhz", cpu_freq);
4229 		if ((mul = cpu_freq * 1000000LL) <= INT_MAX)
4230 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4231 			    "clock-frequency", (int)mul);
4232 	}
4233 
4234 	if (!is_x86_feature(x86_featureset, X86FSET_CPUID)) {
4235 		return;
4236 	}
4237 
4238 	/* vendor-id */
4239 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
4240 	    "vendor-id", cpi->cpi_vendorstr);
4241 
4242 	if (cpi->cpi_maxeax == 0) {
4243 		return;
4244 	}
4245 
4246 	/*
4247 	 * family, model, and step
4248 	 */
4249 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4250 	    "family", CPI_FAMILY(cpi));
4251 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4252 	    "cpu-model", CPI_MODEL(cpi));
4253 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4254 	    "stepping-id", CPI_STEP(cpi));
4255 
4256 	/* type */
4257 	switch (cpi->cpi_vendor) {
4258 	case X86_VENDOR_Intel:
4259 		create = 1;
4260 		break;
4261 	default:
4262 		create = 0;
4263 		break;
4264 	}
4265 	if (create)
4266 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4267 		    "type", CPI_TYPE(cpi));
4268 
4269 	/* ext-family */
4270 	switch (cpi->cpi_vendor) {
4271 	case X86_VENDOR_Intel:
4272 	case X86_VENDOR_AMD:
4273 		create = cpi->cpi_family >= 0xf;
4274 		break;
4275 	default:
4276 		create = 0;
4277 		break;
4278 	}
4279 	if (create)
4280 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4281 		    "ext-family", CPI_FAMILY_XTD(cpi));
4282 
4283 	/* ext-model */
4284 	switch (cpi->cpi_vendor) {
4285 	case X86_VENDOR_Intel:
4286 		create = IS_EXTENDED_MODEL_INTEL(cpi);
4287 		break;
4288 	case X86_VENDOR_AMD:
4289 		create = CPI_FAMILY(cpi) == 0xf;
4290 		break;
4291 	default:
4292 		create = 0;
4293 		break;
4294 	}
4295 	if (create)
4296 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4297 		    "ext-model", CPI_MODEL_XTD(cpi));
4298 
4299 	/* generation */
4300 	switch (cpi->cpi_vendor) {
4301 	case X86_VENDOR_AMD:
4302 		/*
4303 		 * AMD K5 model 1 was the first part to support this
4304 		 */
4305 		create = cpi->cpi_xmaxeax >= 0x80000001;
4306 		break;
4307 	default:
4308 		create = 0;
4309 		break;
4310 	}
4311 	if (create)
4312 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4313 		    "generation", BITX((cpi)->cpi_extd[1].cp_eax, 11, 8));
4314 
4315 	/* brand-id */
4316 	switch (cpi->cpi_vendor) {
4317 	case X86_VENDOR_Intel:
4318 		/*
4319 		 * brand id first appeared on Pentium III Xeon model 8,
4320 		 * and Celeron model 8 processors and Opteron
4321 		 */
4322 		create = cpi->cpi_family > 6 ||
4323 		    (cpi->cpi_family == 6 && cpi->cpi_model >= 8);
4324 		break;
4325 	case X86_VENDOR_AMD:
4326 		create = cpi->cpi_family >= 0xf;
4327 		break;
4328 	default:
4329 		create = 0;
4330 		break;
4331 	}
4332 	if (create && cpi->cpi_brandid != 0) {
4333 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4334 		    "brand-id", cpi->cpi_brandid);
4335 	}
4336 
4337 	/* chunks, and apic-id */
4338 	switch (cpi->cpi_vendor) {
4339 		/*
4340 		 * first available on Pentium IV and Opteron (K8)
4341 		 */
4342 	case X86_VENDOR_Intel:
4343 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
4344 		break;
4345 	case X86_VENDOR_AMD:
4346 		create = cpi->cpi_family >= 0xf;
4347 		break;
4348 	default:
4349 		create = 0;
4350 		break;
4351 	}
4352 	if (create) {
4353 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4354 		    "chunks", CPI_CHUNKS(cpi));
4355 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4356 		    "apic-id", cpi->cpi_apicid);
4357 		if (cpi->cpi_chipid >= 0) {
4358 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4359 			    "chip#", cpi->cpi_chipid);
4360 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4361 			    "clog#", cpi->cpi_clogid);
4362 		}
4363 	}
4364 
4365 	/* cpuid-features */
4366 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4367 	    "cpuid-features", CPI_FEATURES_EDX(cpi));
4368 
4369 
4370 	/* cpuid-features-ecx */
4371 	switch (cpi->cpi_vendor) {
4372 	case X86_VENDOR_Intel:
4373 		create = IS_NEW_F6(cpi) || cpi->cpi_family >= 0xf;
4374 		break;
4375 	case X86_VENDOR_AMD:
4376 		create = cpi->cpi_family >= 0xf;
4377 		break;
4378 	default:
4379 		create = 0;
4380 		break;
4381 	}
4382 	if (create)
4383 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4384 		    "cpuid-features-ecx", CPI_FEATURES_ECX(cpi));
4385 
4386 	/* ext-cpuid-features */
4387 	switch (cpi->cpi_vendor) {
4388 	case X86_VENDOR_Intel:
4389 	case X86_VENDOR_AMD:
4390 	case X86_VENDOR_Cyrix:
4391 	case X86_VENDOR_TM:
4392 	case X86_VENDOR_Centaur:
4393 		create = cpi->cpi_xmaxeax >= 0x80000001;
4394 		break;
4395 	default:
4396 		create = 0;
4397 		break;
4398 	}
4399 	if (create) {
4400 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4401 		    "ext-cpuid-features", CPI_FEATURES_XTD_EDX(cpi));
4402 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cpu_devi,
4403 		    "ext-cpuid-features-ecx", CPI_FEATURES_XTD_ECX(cpi));
4404 	}
4405 
4406 	/*
4407 	 * Brand String first appeared in Intel Pentium IV, AMD K5
4408 	 * model 1, and Cyrix GXm.  On earlier models we try and
4409 	 * simulate something similar .. so this string should always
4410 	 * same -something- about the processor, however lame.
4411 	 */
4412 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, cpu_devi,
4413 	    "brand-string", cpi->cpi_brandstr);
4414 
4415 	/*
4416 	 * Finally, cache and tlb information
4417 	 */
4418 	switch (x86_which_cacheinfo(cpi)) {
4419 	case X86_VENDOR_Intel:
4420 		intel_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
4421 		break;
4422 	case X86_VENDOR_Cyrix:
4423 		cyrix_walk_cacheinfo(cpi, cpu_devi, add_cacheent_props);
4424 		break;
4425 	case X86_VENDOR_AMD:
4426 		amd_cache_info(cpi, cpu_devi);
4427 		break;
4428 	default:
4429 		break;
4430 	}
4431 }
4432 
4433 struct l2info {
4434 	int *l2i_csz;
4435 	int *l2i_lsz;
4436 	int *l2i_assoc;
4437 	int l2i_ret;
4438 };
4439 
4440 /*
4441  * A cacheinfo walker that fetches the size, line-size and associativity
4442  * of the L2 cache
4443  */
4444 static int
4445 intel_l2cinfo(void *arg, const struct cachetab *ct)
4446 {
4447 	struct l2info *l2i = arg;
4448 	int *ip;
4449 
4450 	if (ct->ct_label != l2_cache_str &&
4451 	    ct->ct_label != sl2_cache_str)
4452 		return (0);	/* not an L2 -- keep walking */
4453 
4454 	if ((ip = l2i->l2i_csz) != NULL)
4455 		*ip = ct->ct_size;
4456 	if ((ip = l2i->l2i_lsz) != NULL)
4457 		*ip = ct->ct_line_size;
4458 	if ((ip = l2i->l2i_assoc) != NULL)
4459 		*ip = ct->ct_assoc;
4460 	l2i->l2i_ret = ct->ct_size;
4461 	return (1);		/* was an L2 -- terminate walk */
4462 }
4463 
4464 /*
4465  * AMD L2/L3 Cache and TLB Associativity Field Definition:
4466  *
4467  *	Unlike the associativity for the L1 cache and tlb where the 8 bit
4468  *	value is the associativity, the associativity for the L2 cache and
4469  *	tlb is encoded in the following table. The 4 bit L2 value serves as
4470  *	an index into the amd_afd[] array to determine the associativity.
4471  *	-1 is undefined. 0 is fully associative.
4472  */
4473 
4474 static int amd_afd[] =
4475 	{-1, 1, 2, -1, 4, -1, 8, -1, 16, -1, 32, 48, 64, 96, 128, 0};
4476 
4477 static void
4478 amd_l2cacheinfo(struct cpuid_info *cpi, struct l2info *l2i)
4479 {
4480 	struct cpuid_regs *cp;
4481 	uint_t size, assoc;
4482 	int i;
4483 	int *ip;
4484 
4485 	if (cpi->cpi_xmaxeax < 0x80000006)
4486 		return;
4487 	cp = &cpi->cpi_extd[6];
4488 
4489 	if ((i = BITX(cp->cp_ecx, 15, 12)) != 0 &&
4490 	    (size = BITX(cp->cp_ecx, 31, 16)) != 0) {
4491 		uint_t cachesz = size * 1024;
4492 		assoc = amd_afd[i];
4493 
4494 		ASSERT(assoc != -1);
4495 
4496 		if ((ip = l2i->l2i_csz) != NULL)
4497 			*ip = cachesz;
4498 		if ((ip = l2i->l2i_lsz) != NULL)
4499 			*ip = BITX(cp->cp_ecx, 7, 0);
4500 		if ((ip = l2i->l2i_assoc) != NULL)
4501 			*ip = assoc;
4502 		l2i->l2i_ret = cachesz;
4503 	}
4504 }
4505 
4506 int
4507 getl2cacheinfo(cpu_t *cpu, int *csz, int *lsz, int *assoc)
4508 {
4509 	struct cpuid_info *cpi = cpu->cpu_m.mcpu_cpi;
4510 	struct l2info __l2info, *l2i = &__l2info;
4511 
4512 	l2i->l2i_csz = csz;
4513 	l2i->l2i_lsz = lsz;
4514 	l2i->l2i_assoc = assoc;
4515 	l2i->l2i_ret = -1;
4516 
4517 	switch (x86_which_cacheinfo(cpi)) {
4518 	case X86_VENDOR_Intel:
4519 		intel_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
4520 		break;
4521 	case X86_VENDOR_Cyrix:
4522 		cyrix_walk_cacheinfo(cpi, l2i, intel_l2cinfo);
4523 		break;
4524 	case X86_VENDOR_AMD:
4525 		amd_l2cacheinfo(cpi, l2i);
4526 		break;
4527 	default:
4528 		break;
4529 	}
4530 	return (l2i->l2i_ret);
4531 }
4532 
4533 #if !defined(__xpv)
4534 
4535 uint32_t *
4536 cpuid_mwait_alloc(cpu_t *cpu)
4537 {
4538 	uint32_t	*ret;
4539 	size_t		mwait_size;
4540 
4541 	ASSERT(cpuid_checkpass(CPU, 2));
4542 
4543 	mwait_size = CPU->cpu_m.mcpu_cpi->cpi_mwait.mon_max;
4544 	if (mwait_size == 0)
4545 		return (NULL);
4546 
4547 	/*
4548 	 * kmem_alloc() returns cache line size aligned data for mwait_size
4549 	 * allocations.  mwait_size is currently cache line sized.  Neither
4550 	 * of these implementation details are guarantied to be true in the
4551 	 * future.
4552 	 *
4553 	 * First try allocating mwait_size as kmem_alloc() currently returns
4554 	 * correctly aligned memory.  If kmem_alloc() does not return
4555 	 * mwait_size aligned memory, then use mwait_size ROUNDUP.
4556 	 *
4557 	 * Set cpi_mwait.buf_actual and cpi_mwait.size_actual in case we
4558 	 * decide to free this memory.
4559 	 */
4560 	ret = kmem_zalloc(mwait_size, KM_SLEEP);
4561 	if (ret == (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size)) {
4562 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
4563 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size;
4564 		*ret = MWAIT_RUNNING;
4565 		return (ret);
4566 	} else {
4567 		kmem_free(ret, mwait_size);
4568 		ret = kmem_zalloc(mwait_size * 2, KM_SLEEP);
4569 		cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = ret;
4570 		cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = mwait_size * 2;
4571 		ret = (uint32_t *)P2ROUNDUP((uintptr_t)ret, mwait_size);
4572 		*ret = MWAIT_RUNNING;
4573 		return (ret);
4574 	}
4575 }
4576 
4577 void
4578 cpuid_mwait_free(cpu_t *cpu)
4579 {
4580 	if (cpu->cpu_m.mcpu_cpi == NULL) {
4581 		return;
4582 	}
4583 
4584 	if (cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual != NULL &&
4585 	    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual > 0) {
4586 		kmem_free(cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual,
4587 		    cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual);
4588 	}
4589 
4590 	cpu->cpu_m.mcpu_cpi->cpi_mwait.buf_actual = NULL;
4591 	cpu->cpu_m.mcpu_cpi->cpi_mwait.size_actual = 0;
4592 }
4593 
4594 void
4595 patch_tsc_read(int flag)
4596 {
4597 	size_t cnt;
4598 
4599 	switch (flag) {
4600 	case TSC_NONE:
4601 		cnt = &_no_rdtsc_end - &_no_rdtsc_start;
4602 		(void) memcpy((void *)tsc_read, (void *)&_no_rdtsc_start, cnt);
4603 		break;
4604 	case TSC_RDTSC_MFENCE:
4605 		cnt = &_tsc_mfence_end - &_tsc_mfence_start;
4606 		(void) memcpy((void *)tsc_read,
4607 		    (void *)&_tsc_mfence_start, cnt);
4608 		break;
4609 	case TSC_RDTSC_LFENCE:
4610 		cnt = &_tsc_lfence_end - &_tsc_lfence_start;
4611 		(void) memcpy((void *)tsc_read,
4612 		    (void *)&_tsc_lfence_start, cnt);
4613 		break;
4614 	case TSC_TSCP:
4615 		cnt = &_tscp_end - &_tscp_start;
4616 		(void) memcpy((void *)tsc_read, (void *)&_tscp_start, cnt);
4617 		break;
4618 	default:
4619 		/* Bail for unexpected TSC types. (TSC_NONE covers 0) */
4620 		cmn_err(CE_PANIC, "Unrecogized TSC type: %d", flag);
4621 		break;
4622 	}
4623 	tsc_type = flag;
4624 }
4625 
4626 int
4627 cpuid_deep_cstates_supported(void)
4628 {
4629 	struct cpuid_info *cpi;
4630 	struct cpuid_regs regs;
4631 
4632 	ASSERT(cpuid_checkpass(CPU, 1));
4633 
4634 	cpi = CPU->cpu_m.mcpu_cpi;
4635 
4636 	if (!is_x86_feature(x86_featureset, X86FSET_CPUID))
4637 		return (0);
4638 
4639 	switch (cpi->cpi_vendor) {
4640 	case X86_VENDOR_Intel:
4641 		if (cpi->cpi_xmaxeax < 0x80000007)
4642 			return (0);
4643 
4644 		/*
4645 		 * TSC run at a constant rate in all ACPI C-states?
4646 		 */
4647 		regs.cp_eax = 0x80000007;
4648 		(void) __cpuid_insn(&regs);
4649 		return (regs.cp_edx & CPUID_TSC_CSTATE_INVARIANCE);
4650 
4651 	default:
4652 		return (0);
4653 	}
4654 }
4655 
4656 #endif	/* !__xpv */
4657 
4658 void
4659 post_startup_cpu_fixups(void)
4660 {
4661 #ifndef __xpv
4662 	/*
4663 	 * Some AMD processors support C1E state. Entering this state will
4664 	 * cause the local APIC timer to stop, which we can't deal with at
4665 	 * this time.
4666 	 */
4667 	if (cpuid_getvendor(CPU) == X86_VENDOR_AMD) {
4668 		on_trap_data_t otd;
4669 		uint64_t reg;
4670 
4671 		if (!on_trap(&otd, OT_DATA_ACCESS)) {
4672 			reg = rdmsr(MSR_AMD_INT_PENDING_CMP_HALT);
4673 			/* Disable C1E state if it is enabled by BIOS */
4674 			if ((reg >> AMD_ACTONCMPHALT_SHIFT) &
4675 			    AMD_ACTONCMPHALT_MASK) {
4676 				reg &= ~(AMD_ACTONCMPHALT_MASK <<
4677 				    AMD_ACTONCMPHALT_SHIFT);
4678 				wrmsr(MSR_AMD_INT_PENDING_CMP_HALT, reg);
4679 			}
4680 		}
4681 		no_trap();
4682 	}
4683 #endif	/* !__xpv */
4684 }
4685 
4686 /*
4687  * Setup necessary registers to enable XSAVE feature on this processor.
4688  * This function needs to be called early enough, so that no xsave/xrstor
4689  * ops will execute on the processor before the MSRs are properly set up.
4690  *
4691  * Current implementation has the following assumption:
4692  * - cpuid_pass1() is done, so that X86 features are known.
4693  * - fpu_probe() is done, so that fp_save_mech is chosen.
4694  */
4695 void
4696 xsave_setup_msr(cpu_t *cpu)
4697 {
4698 	ASSERT(fp_save_mech == FP_XSAVE);
4699 	ASSERT(is_x86_feature(x86_featureset, X86FSET_XSAVE));
4700 
4701 	/* Enable OSXSAVE in CR4. */
4702 	setcr4(getcr4() | CR4_OSXSAVE);
4703 	/*
4704 	 * Update SW copy of ECX, so that /dev/cpu/self/cpuid will report
4705 	 * correct value.
4706 	 */
4707 	cpu->cpu_m.mcpu_cpi->cpi_std[1].cp_ecx |= CPUID_INTC_ECX_OSXSAVE;
4708 	setup_xfem();
4709 }
4710 
4711 /*
4712  * Starting with the Westmere processor the local
4713  * APIC timer will continue running in all C-states,
4714  * including the deepest C-states.
4715  */
4716 int
4717 cpuid_arat_supported(void)
4718 {
4719 	struct cpuid_info *cpi;
4720 	struct cpuid_regs regs;
4721 
4722 	ASSERT(cpuid_checkpass(CPU, 1));
4723 	ASSERT(is_x86_feature(x86_featureset, X86FSET_CPUID));
4724 
4725 	cpi = CPU->cpu_m.mcpu_cpi;
4726 
4727 	switch (cpi->cpi_vendor) {
4728 	case X86_VENDOR_Intel:
4729 		/*
4730 		 * Always-running Local APIC Timer is
4731 		 * indicated by CPUID.6.EAX[2].
4732 		 */
4733 		if (cpi->cpi_maxeax >= 6) {
4734 			regs.cp_eax = 6;
4735 			(void) cpuid_insn(NULL, &regs);
4736 			return (regs.cp_eax & CPUID_CSTATE_ARAT);
4737 		} else {
4738 			return (0);
4739 		}
4740 	default:
4741 		return (0);
4742 	}
4743 }
4744 
4745 /*
4746  * Check support for Intel ENERGY_PERF_BIAS feature
4747  */
4748 int
4749 cpuid_iepb_supported(struct cpu *cp)
4750 {
4751 	struct cpuid_info *cpi = cp->cpu_m.mcpu_cpi;
4752 	struct cpuid_regs regs;
4753 
4754 	ASSERT(cpuid_checkpass(cp, 1));
4755 
4756 	if (!(is_x86_feature(x86_featureset, X86FSET_CPUID)) ||
4757 	    !(is_x86_feature(x86_featureset, X86FSET_MSR))) {
4758 		return (0);
4759 	}
4760 
4761 	/*
4762 	 * Intel ENERGY_PERF_BIAS MSR is indicated by
4763 	 * capability bit CPUID.6.ECX.3
4764 	 */
4765 	if ((cpi->cpi_vendor != X86_VENDOR_Intel) || (cpi->cpi_maxeax < 6))
4766 		return (0);
4767 
4768 	regs.cp_eax = 0x6;
4769 	(void) cpuid_insn(NULL, &regs);
4770 	return (regs.cp_ecx & CPUID_EPB_SUPPORT);
4771 }
4772 
4773 /*
4774  * Check support for TSC deadline timer
4775  *
4776  * TSC deadline timer provides a superior software programming
4777  * model over local APIC timer that eliminates "time drifts".
4778  * Instead of specifying a relative time, software specifies an
4779  * absolute time as the target at which the processor should
4780  * generate a timer event.
4781  */
4782 int
4783 cpuid_deadline_tsc_supported(void)
4784 {
4785 	struct cpuid_info *cpi = CPU->cpu_m.mcpu_cpi;
4786 	struct cpuid_regs regs;
4787 
4788 	ASSERT(cpuid_checkpass(CPU, 1));
4789 	ASSERT(is_x86_feature(x86_featureset, X86FSET_CPUID));
4790 
4791 	switch (cpi->cpi_vendor) {
4792 	case X86_VENDOR_Intel:
4793 		if (cpi->cpi_maxeax >= 1) {
4794 			regs.cp_eax = 1;
4795 			(void) cpuid_insn(NULL, &regs);
4796 			return (regs.cp_ecx & CPUID_DEADLINE_TSC);
4797 		} else {
4798 			return (0);
4799 		}
4800 	default:
4801 		return (0);
4802 	}
4803 }
4804 
4805 #if defined(__amd64) && !defined(__xpv)
4806 /*
4807  * Patch in versions of bcopy for high performance Intel Nhm processors
4808  * and later...
4809  */
4810 void
4811 patch_memops(uint_t vendor)
4812 {
4813 	size_t cnt, i;
4814 	caddr_t to, from;
4815 
4816 	if ((vendor == X86_VENDOR_Intel) &&
4817 	    is_x86_feature(x86_featureset, X86FSET_SSE4_2)) {
4818 		cnt = &bcopy_patch_end - &bcopy_patch_start;
4819 		to = &bcopy_ck_size;
4820 		from = &bcopy_patch_start;
4821 		for (i = 0; i < cnt; i++) {
4822 			*to++ = *from++;
4823 		}
4824 	}
4825 }
4826 #endif  /* __amd64 && !__xpv */
4827 
4828 /*
4829  * This function finds the number of bits to represent the number of cores per
4830  * chip and the number of strands per core for the Intel platforms.
4831  * It re-uses the x2APIC cpuid code of the cpuid_pass2().
4832  */
4833 void
4834 cpuid_get_ext_topo(uint_t vendor, uint_t *core_nbits, uint_t *strand_nbits)
4835 {
4836 	struct cpuid_regs regs;
4837 	struct cpuid_regs *cp = &regs;
4838 
4839 	if (vendor != X86_VENDOR_Intel) {
4840 		return;
4841 	}
4842 
4843 	/* if the cpuid level is 0xB, extended topo is available. */
4844 	cp->cp_eax = 0;
4845 	if (__cpuid_insn(cp) >= 0xB) {
4846 
4847 		cp->cp_eax = 0xB;
4848 		cp->cp_edx = cp->cp_ebx = cp->cp_ecx = 0;
4849 		(void) __cpuid_insn(cp);
4850 
4851 		/*
4852 		 * Check CPUID.EAX=0BH, ECX=0H:EBX is non-zero, which
4853 		 * indicates that the extended topology enumeration leaf is
4854 		 * available.
4855 		 */
4856 		if (cp->cp_ebx) {
4857 			uint_t coreid_shift = 0;
4858 			uint_t chipid_shift = 0;
4859 			uint_t i;
4860 			uint_t level;
4861 
4862 			for (i = 0; i < CPI_FNB_ECX_MAX; i++) {
4863 				cp->cp_eax = 0xB;
4864 				cp->cp_ecx = i;
4865 
4866 				(void) __cpuid_insn(cp);
4867 				level = CPI_CPU_LEVEL_TYPE(cp);
4868 
4869 				if (level == 1) {
4870 					/*
4871 					 * Thread level processor topology
4872 					 * Number of bits shift right APIC ID
4873 					 * to get the coreid.
4874 					 */
4875 					coreid_shift = BITX(cp->cp_eax, 4, 0);
4876 				} else if (level == 2) {
4877 					/*
4878 					 * Core level processor topology
4879 					 * Number of bits shift right APIC ID
4880 					 * to get the chipid.
4881 					 */
4882 					chipid_shift = BITX(cp->cp_eax, 4, 0);
4883 				}
4884 			}
4885 
4886 			if (coreid_shift > 0 && chipid_shift > coreid_shift) {
4887 				*strand_nbits = coreid_shift;
4888 				*core_nbits = chipid_shift - coreid_shift;
4889 			}
4890 		}
4891 	}
4892 }
4893