]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/cpuid.c
xnu-1228.9.59.tar.gz
[apple/xnu.git] / osfmk / i386 / cpuid.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0c530ab8
A
31#include <platforms.h>
32#include <mach_kdb.h>
2d21ac55 33#include <vm/vm_page.h>
91447636
A
34#include <pexpert/pexpert.h>
35
55e303ae 36#include "cpuid.h"
0c530ab8
A
37#if MACH_KDB
38#include <i386/db_machdep.h>
39#include <ddb/db_aout.h>
40#include <ddb/db_access.h>
41#include <ddb/db_sym.h>
42#include <ddb/db_variables.h>
43#include <ddb/db_command.h>
44#include <ddb/db_output.h>
45#include <ddb/db_expr.h>
46#endif
1c79356b 47
55e303ae 48#define min(a,b) ((a) < (b) ? (a) : (b))
0c530ab8
A
49#define quad(hi,lo) (((uint64_t)(hi)) << 32 | (lo))
50
51#define bit(n) (1UL << (n))
52#define bitmask(h,l) ((bit(h)|(bit(h)-1)) & ~(bit(l)-1))
53#define bitfield(x,h,l) (((x) & bitmask(h,l)) >> l)
1c79356b
A
54
55/*
55e303ae 56 * CPU identification routines.
1c79356b 57 */
1c79356b 58
0c530ab8 59static i386_cpu_info_t *cpuid_cpu_infop = NULL;
55e303ae 60static i386_cpu_info_t cpuid_cpu_info;
d7e50217 61
2d21ac55
A
62/* this function is Intel-specific */
63static void
64cpuid_set_cache_info( i386_cpu_info_t * info_p )
91447636
A
65{
66 uint32_t cpuid_result[4];
2d21ac55
A
67 uint32_t reg[4];
68 uint32_t index;
69 uint32_t linesizes[LCACHE_MAX];
91447636
A
70 unsigned int i;
71 unsigned int j;
2d21ac55 72 boolean_t cpuid_deterministic_supported = FALSE;
55e303ae 73
2d21ac55
A
74 bzero( linesizes, sizeof(linesizes) );
75
76 /* Get processor cache descriptor info using leaf 2. We don't use
77 * this internally, but must publish it for KEXTs.
78 */
55e303ae
A
79 do_cpuid(2, cpuid_result);
80 for (j = 0; j < 4; j++) {
81 if ((cpuid_result[j] >> 31) == 1) /* bit31 is validity */
82 continue;
83 ((uint32_t *) info_p->cache_info)[j] = cpuid_result[j];
84 }
85 /* first byte gives number of cpuid calls to get all descriptors */
86 for (i = 1; i < info_p->cache_info[0]; i++) {
87 if (i*16 > sizeof(info_p->cache_info))
88 break;
89 do_cpuid(2, cpuid_result);
90 for (j = 0; j < 4; j++) {
91 if ((cpuid_result[j] >> 31) == 1)
92 continue;
93 ((uint32_t *) info_p->cache_info)[4*i+j] =
94 cpuid_result[j];
95 }
96 }
97
0c530ab8 98 /*
2d21ac55
A
99 * Get cache info using leaf 4, the "deterministic cache parameters."
100 * Most processors Mac OS X supports implement this flavor of CPUID.
101 * Loop over each cache on the processor.
0c530ab8
A
102 */
103 do_cpuid(0, cpuid_result);
2d21ac55
A
104 if (cpuid_result[eax] >= 4)
105 cpuid_deterministic_supported = TRUE;
106
107 for (index = 0; cpuid_deterministic_supported; index++) {
108 cache_type_t type = Lnone;
109 uint32_t cache_type;
110 uint32_t cache_level;
111 uint32_t cache_sharing;
112 uint32_t cache_linesize;
113 uint32_t cache_sets;
114 uint32_t cache_associativity;
115 uint32_t cache_size;
116 uint32_t cache_partitions;
117 uint32_t colors;
118
119 reg[eax] = 4; /* cpuid request 4 */
120 reg[ecx] = index; /* index starting at 0 */
121 cpuid(reg);
0c530ab8 122//kprintf("cpuid(4) index=%d eax=%p\n", index, reg[eax]);
2d21ac55
A
123 cache_type = bitfield(reg[eax], 4, 0);
124 if (cache_type == 0)
125 break; /* no more caches */
126 cache_level = bitfield(reg[eax], 7, 5);
127 cache_sharing = bitfield(reg[eax], 25, 14) + 1;
128 info_p->cpuid_cores_per_package
129 = bitfield(reg[eax], 31, 26) + 1;
130 cache_linesize = bitfield(reg[ebx], 11, 0) + 1;
131 cache_partitions = bitfield(reg[ebx], 21, 12) + 1;
132 cache_associativity = bitfield(reg[ebx], 31, 22) + 1;
133 cache_sets = bitfield(reg[ecx], 31, 0) + 1;
134
135 /* Map type/levels returned by CPUID into cache_type_t */
136 switch (cache_level) {
137 case 1:
138 type = cache_type == 1 ? L1D :
139 cache_type == 2 ? L1I :
140 Lnone;
141 break;
142 case 2:
143 type = cache_type == 3 ? L2U :
144 Lnone;
145 break;
146 case 3:
147 type = cache_type == 3 ? L3U :
148 Lnone;
149 break;
150 default:
151 type = Lnone;
152 }
153
154 /* The total size of a cache is:
155 * ( linesize * sets * associativity )
156 */
157 if (type != Lnone) {
158 cache_size = cache_linesize * cache_sets * cache_associativity;
159 info_p->cache_size[type] = cache_size;
160 info_p->cache_sharing[type] = cache_sharing;
161 info_p->cache_partitions[type] = cache_partitions;
162 linesizes[type] = cache_linesize;
163
164 /* Compute the number of page colors for this cache,
165 * which is:
166 * ( linesize * sets ) / page_size
167 *
168 * To help visualize this, consider two views of a
169 * physical address. To the cache, it is composed
170 * of a line offset, a set selector, and a tag.
171 * To VM, it is composed of a page offset, a page
172 * color, and other bits in the pageframe number:
173 *
174 * +-----------------+---------+--------+
175 * cache: | tag | set | offset |
176 * +-----------------+---------+--------+
177 *
178 * +-----------------+-------+----------+
179 * VM: | don't care | color | pg offset|
180 * +-----------------+-------+----------+
181 *
182 * The color is those bits in (set+offset) not covered
183 * by the page offset.
184 */
185 colors = ( cache_linesize * cache_sets ) >> 12;
186
187 if ( colors > vm_cache_geometry_colors )
188 vm_cache_geometry_colors = colors;
189 }
190 }
191
192 /*
193 * If deterministic cache parameters are not available, use
194 * something else
195 */
196 if (info_p->cpuid_cores_per_package == 0) {
197 info_p->cpuid_cores_per_package = 1;
91447636 198
2d21ac55
A
199 /* cpuid define in 1024 quantities */
200 info_p->cache_size[L2U] = info_p->cpuid_cache_size * 1024;
201 info_p->cache_sharing[L2U] = 1;
202 info_p->cache_partitions[L2U] = 1;
91447636 203
2d21ac55
A
204 linesizes[L2U] = info_p->cpuid_cache_linesize;
205 }
206
207 /*
208 * What linesize to publish? We use the L2 linesize if any,
209 * else the L1D.
210 */
211 if ( linesizes[L2U] )
212 info_p->cache_linesize = linesizes[L2U];
213 else if (linesizes[L1D])
214 info_p->cache_linesize = linesizes[L1D];
215 else panic("no linesize");
593a1d5f
A
216
217 /*
218 * Extract and publish TLB information.
219 */
220 for (i = 1; i < sizeof(info_p->cache_info); i++) {
221 uint8_t desc = info_p->cache_info[i];
222
223 switch (desc) {
224 case CPUID_CACHE_ITLB_4K_32_4:
225 info_p->cpuid_itlb_small = 32;
226 break;
227 case CPUID_CACHE_ITLB_4M_2:
228 info_p->cpuid_itlb_large = 2;
229 break;
230 case CPUID_CACHE_DTLB_4K_64_4:
231 info_p->cpuid_dtlb_small = 64;
232 break;
233 case CPUID_CACHE_DTLB_4M_8_4:
234 info_p->cpuid_dtlb_large = 8;
235 break;
236 case CPUID_CACHE_DTLB_4M_32_4:
237 info_p->cpuid_dtlb_large = 32;
238 break;
239 case CPUID_CACHE_ITLB_64:
240 info_p->cpuid_itlb_small = 64;
241 info_p->cpuid_itlb_large = 64;
242 break;
243 case CPUID_CACHE_ITLB_128:
244 info_p->cpuid_itlb_small = 128;
245 info_p->cpuid_itlb_large = 128;
246 break;
247 case CPUID_CACHE_ITLB_256:
248 info_p->cpuid_itlb_small = 256;
249 info_p->cpuid_itlb_large = 256;
250 break;
251 case CPUID_CACHE_DTLB_64:
252 info_p->cpuid_dtlb_small = 64;
253 info_p->cpuid_dtlb_large = 64;
254 break;
255 case CPUID_CACHE_DTLB_128:
256 info_p->cpuid_dtlb_small = 128;
257 info_p->cpuid_dtlb_large = 128;
258 break;
259 case CPUID_CACHE_DTLB_256:
260 info_p->cpuid_dtlb_small = 256;
261 info_p->cpuid_dtlb_large = 256;
262 break;
263 case CPUID_CACHE_ITLB_4M2M_7:
264 info_p->cpuid_itlb_large = 7;
265 break;
266 case CPUID_CACHE_DTLB_4K_16_4:
267 info_p->cpuid_dtlb_small = 16;
268 break;
269 case CPUID_CACHE_DTLB_4M2M_32_4:
270 info_p->cpuid_dtlb_large = 32;
271 break;
272 case CPUID_CACHE_ITLB_4K_128_4:
273 info_p->cpuid_itlb_small = 128;
274 break;
275 case CPUID_CACHE_ITLB_4M_8:
276 info_p->cpuid_itlb_large = 8;
277 break;
278 case CPUID_CACHE_DTLB_4K_128_4:
279 info_p->cpuid_dtlb_small = 128;
280 break;
281 case CPUID_CACHE_DTLB_4K_256_4:
282 info_p->cpuid_dtlb_small = 256;
283 break;
284 }
285 }
91447636
A
286}
287
288static void
2d21ac55 289cpuid_set_generic_info(i386_cpu_info_t *info_p)
91447636 290{
2d21ac55 291 uint32_t cpuid_reg[4];
91447636
A
292 uint32_t max_extid;
293 char str[128], *p;
294
2d21ac55
A
295 /* do cpuid 0 to get vendor */
296 do_cpuid(0, cpuid_reg);
297 bcopy((char *)&cpuid_reg[ebx], &info_p->cpuid_vendor[0], 4); /* ug */
298 bcopy((char *)&cpuid_reg[ecx], &info_p->cpuid_vendor[8], 4);
299 bcopy((char *)&cpuid_reg[edx], &info_p->cpuid_vendor[4], 4);
300 info_p->cpuid_vendor[12] = 0;
301
91447636 302 /* get extended cpuid results */
2d21ac55
A
303 do_cpuid(0x80000000, cpuid_reg);
304 max_extid = cpuid_reg[eax];
91447636
A
305
306 /* check to see if we can get brand string */
307 if (max_extid >= 0x80000004) {
308 /*
309 * The brand string 48 bytes (max), guaranteed to
310 * be NUL terminated.
311 */
2d21ac55
A
312 do_cpuid(0x80000002, cpuid_reg);
313 bcopy((char *)cpuid_reg, &str[0], 16);
314 do_cpuid(0x80000003, cpuid_reg);
315 bcopy((char *)cpuid_reg, &str[16], 16);
316 do_cpuid(0x80000004, cpuid_reg);
317 bcopy((char *)cpuid_reg, &str[32], 16);
91447636
A
318 for (p = str; *p != '\0'; p++) {
319 if (*p != ' ') break;
320 }
2d21ac55
A
321 strlcpy(info_p->cpuid_brand_string,
322 p, sizeof(info_p->cpuid_brand_string));
91447636 323
2d21ac55
A
324 if (!strncmp(info_p->cpuid_brand_string, CPUID_STRING_UNKNOWN,
325 min(sizeof(info_p->cpuid_brand_string),
326 strlen(CPUID_STRING_UNKNOWN) + 1))) {
91447636 327 /*
2d21ac55
A
328 * This string means we have a firmware-programmable brand string,
329 * and the firmware couldn't figure out what sort of CPU we have.
91447636
A
330 */
331 info_p->cpuid_brand_string[0] = '\0';
332 }
333 }
334
2d21ac55
A
335 /* Get cache and addressing info. */
336 if (max_extid >= 0x80000006) {
337 do_cpuid(0x80000006, cpuid_reg);
338 info_p->cpuid_cache_linesize = bitfield(cpuid_reg[ecx], 7, 0);
339 info_p->cpuid_cache_L2_associativity =
340 bitfield(cpuid_reg[ecx],15,12);
341 info_p->cpuid_cache_size = bitfield(cpuid_reg[ecx],31,16);
342 do_cpuid(0x80000008, cpuid_reg);
343 info_p->cpuid_address_bits_physical =
344 bitfield(cpuid_reg[eax], 7, 0);
345 info_p->cpuid_address_bits_virtual =
346 bitfield(cpuid_reg[eax],15, 8);
347 }
348
91447636 349 /* get processor signature and decode */
2d21ac55
A
350 do_cpuid(1, cpuid_reg);
351 info_p->cpuid_signature = cpuid_reg[eax];
352 info_p->cpuid_stepping = bitfield(cpuid_reg[eax], 3, 0);
353 info_p->cpuid_model = bitfield(cpuid_reg[eax], 7, 4);
354 info_p->cpuid_family = bitfield(cpuid_reg[eax], 11, 8);
355 info_p->cpuid_type = bitfield(cpuid_reg[eax], 13, 12);
356 info_p->cpuid_extmodel = bitfield(cpuid_reg[eax], 19, 16);
357 info_p->cpuid_extfamily = bitfield(cpuid_reg[eax], 27, 20);
358 info_p->cpuid_brand = bitfield(cpuid_reg[ebx], 7, 0);
359 info_p->cpuid_features = quad(cpuid_reg[ecx], cpuid_reg[edx]);
360
361 /* Fold extensions into family/model */
362 if (info_p->cpuid_family == 0x0f)
363 info_p->cpuid_family += info_p->cpuid_extfamily;
593a1d5f 364 if (info_p->cpuid_family == 0x0f || info_p->cpuid_family == 0x06)
2d21ac55
A
365 info_p->cpuid_model += (info_p->cpuid_extmodel << 4);
366
367 if (info_p->cpuid_features & CPUID_FEATURE_HTT)
368 info_p->cpuid_logical_per_package =
369 bitfield(cpuid_reg[ebx], 23, 16);
370 else
371 info_p->cpuid_logical_per_package = 1;
0c530ab8
A
372
373 if (max_extid >= 0x80000001) {
2d21ac55 374 do_cpuid(0x80000001, cpuid_reg);
0c530ab8 375 info_p->cpuid_extfeatures =
2d21ac55
A
376 quad(cpuid_reg[ecx], cpuid_reg[edx]);
377 }
378
593a1d5f 379 if (info_p->cpuid_features & CPUID_FEATURE_MONITOR) {
2d21ac55
A
380 /*
381 * Extract the Monitor/Mwait Leaf info:
382 */
383 do_cpuid(5, cpuid_reg);
384 info_p->cpuid_mwait_linesize_min = cpuid_reg[eax];
385 info_p->cpuid_mwait_linesize_max = cpuid_reg[ebx];
386 info_p->cpuid_mwait_extensions = cpuid_reg[ecx];
387 info_p->cpuid_mwait_sub_Cstates = cpuid_reg[edx];
388
389 /*
390 * And the thermal and Power Leaf while we're at it:
391 */
392 do_cpuid(6, cpuid_reg);
393 info_p->cpuid_thermal_sensor =
394 bitfield(cpuid_reg[eax], 0, 0);
395 info_p->cpuid_thermal_dynamic_acceleration =
396 bitfield(cpuid_reg[eax], 1, 1);
397 info_p->cpuid_thermal_thresholds =
398 bitfield(cpuid_reg[ebx], 3, 0);
399 info_p->cpuid_thermal_ACNT_MCNT =
400 bitfield(cpuid_reg[ecx], 0, 0);
401
402 /*
403 * And the Architectural Performance Monitoring Leaf:
404 */
405 do_cpuid(0xa, cpuid_reg);
406 info_p->cpuid_arch_perf_version =
407 bitfield(cpuid_reg[eax], 7, 0);
408 info_p->cpuid_arch_perf_number =
409 bitfield(cpuid_reg[eax],15, 8);
410 info_p->cpuid_arch_perf_width =
411 bitfield(cpuid_reg[eax],23,16);
412 info_p->cpuid_arch_perf_events_number =
413 bitfield(cpuid_reg[eax],31,24);
414 info_p->cpuid_arch_perf_events =
415 cpuid_reg[ebx];
416 info_p->cpuid_arch_perf_fixed_number =
417 bitfield(cpuid_reg[edx], 4, 0);
418 info_p->cpuid_arch_perf_fixed_width =
419 bitfield(cpuid_reg[edx],12, 5);
420
0c530ab8 421 }
55e303ae
A
422
423 return;
424}
425
2d21ac55
A
426void
427cpuid_set_info(void)
d7e50217 428{
2d21ac55
A
429 bzero((void *)&cpuid_cpu_info, sizeof(cpuid_cpu_info));
430
431 cpuid_set_generic_info(&cpuid_cpu_info);
55e303ae 432
2d21ac55
A
433 /* verify we are running on a supported CPU */
434 if ((strncmp(CPUID_VID_INTEL, cpuid_cpu_info.cpuid_vendor,
435 min(strlen(CPUID_STRING_UNKNOWN) + 1,
436 sizeof(cpuid_cpu_info.cpuid_vendor)))) ||
437 (cpuid_cpu_info.cpuid_family != 6) ||
438 (cpuid_cpu_info.cpuid_model < 13))
439 panic("Unsupported CPU");
440
441 cpuid_cpu_info.cpuid_cpu_type = CPU_TYPE_X86;
442 cpuid_cpu_info.cpuid_cpu_subtype = CPU_SUBTYPE_X86_ARCH1;
443
444 cpuid_set_cache_info(&cpuid_cpu_info);
445
593a1d5f
A
446 if (cpuid_cpu_info.core_count == 0) {
447 cpuid_cpu_info.core_count =
448 cpuid_cpu_info.cpuid_cores_per_package;
449 cpuid_cpu_info.thread_count =
450 cpuid_cpu_info.cpuid_logical_per_package;
451 }
452
2d21ac55
A
453 cpuid_cpu_info.cpuid_model_string = ""; /* deprecated */
454}
55e303ae
A
455
456static struct {
0c530ab8 457 uint64_t mask;
91447636 458 const char *name;
0c530ab8 459} feature_map[] = {
55e303ae
A
460 {CPUID_FEATURE_FPU, "FPU",},
461 {CPUID_FEATURE_VME, "VME",},
462 {CPUID_FEATURE_DE, "DE",},
463 {CPUID_FEATURE_PSE, "PSE",},
464 {CPUID_FEATURE_TSC, "TSC",},
465 {CPUID_FEATURE_MSR, "MSR",},
466 {CPUID_FEATURE_PAE, "PAE",},
467 {CPUID_FEATURE_MCE, "MCE",},
468 {CPUID_FEATURE_CX8, "CX8",},
469 {CPUID_FEATURE_APIC, "APIC",},
470 {CPUID_FEATURE_SEP, "SEP",},
471 {CPUID_FEATURE_MTRR, "MTRR",},
472 {CPUID_FEATURE_PGE, "PGE",},
473 {CPUID_FEATURE_MCA, "MCA",},
474 {CPUID_FEATURE_CMOV, "CMOV",},
475 {CPUID_FEATURE_PAT, "PAT",},
476 {CPUID_FEATURE_PSE36, "PSE36",},
477 {CPUID_FEATURE_PSN, "PSN",},
478 {CPUID_FEATURE_CLFSH, "CLFSH",},
479 {CPUID_FEATURE_DS, "DS",},
480 {CPUID_FEATURE_ACPI, "ACPI",},
481 {CPUID_FEATURE_MMX, "MMX",},
482 {CPUID_FEATURE_FXSR, "FXSR",},
483 {CPUID_FEATURE_SSE, "SSE",},
484 {CPUID_FEATURE_SSE2, "SSE2",},
485 {CPUID_FEATURE_SS, "SS",},
486 {CPUID_FEATURE_HTT, "HTT",},
487 {CPUID_FEATURE_TM, "TM",},
0c530ab8
A
488 {CPUID_FEATURE_SSE3, "SSE3"},
489 {CPUID_FEATURE_MONITOR, "MON"},
490 {CPUID_FEATURE_DSCPL, "DSCPL"},
491 {CPUID_FEATURE_VMX, "VMX"},
492 {CPUID_FEATURE_SMX, "SMX"},
493 {CPUID_FEATURE_EST, "EST"},
494 {CPUID_FEATURE_TM2, "TM2"},
2d21ac55 495 {CPUID_FEATURE_SSSE3, "SSSE3"},
0c530ab8
A
496 {CPUID_FEATURE_CID, "CID"},
497 {CPUID_FEATURE_CX16, "CX16"},
498 {CPUID_FEATURE_xTPR, "TPR"},
499 {CPUID_FEATURE_PDCM, "PDCM"},
0c530ab8
A
500 {CPUID_FEATURE_SSE4_1, "SSE4.1"},
501 {CPUID_FEATURE_SSE4_2, "SSE4.2"},
593a1d5f 502 {CPUID_FEATURE_xAPIC, "xAPIC"},
0c530ab8
A
503 {CPUID_FEATURE_POPCNT, "POPCNT"},
504 {0, 0}
505},
506extfeature_map[] = {
507 {CPUID_EXTFEATURE_SYSCALL, "SYSCALL"},
508 {CPUID_EXTFEATURE_XD, "XD"},
509 {CPUID_EXTFEATURE_EM64T, "EM64T"},
510 {CPUID_EXTFEATURE_LAHF, "LAHF"},
55e303ae
A
511 {0, 0}
512};
513
0c530ab8
A
514i386_cpu_info_t *
515cpuid_info(void)
516{
593a1d5f 517 /* Set-up the cpuid_info stucture lazily */
0c530ab8 518 if (cpuid_cpu_infop == NULL) {
2d21ac55 519 cpuid_set_info();
0c530ab8
A
520 cpuid_cpu_infop = &cpuid_cpu_info;
521 }
522 return cpuid_cpu_infop;
523}
524
55e303ae 525char *
0c530ab8 526cpuid_get_feature_names(uint64_t features, char *buf, unsigned buf_len)
55e303ae 527{
0c530ab8
A
528 int len = -1;
529 char *p = buf;
55e303ae 530 int i;
0c530ab8
A
531
532 for (i = 0; feature_map[i].mask != 0; i++) {
533 if ((features & feature_map[i].mask) == 0)
534 continue;
535 if (len > 0)
536 *p++ = ' ';
537 len = min(strlen(feature_map[i].name), (buf_len-1) - (p-buf));
538 if (len == 0)
539 break;
540 bcopy(feature_map[i].name, p, len);
541 p += len;
542 }
543 *p = '\0';
544 return buf;
545}
546
547char *
548cpuid_get_extfeature_names(uint64_t extfeatures, char *buf, unsigned buf_len)
549{
550 int len = -1;
55e303ae 551 char *p = buf;
0c530ab8 552 int i;
55e303ae 553
0c530ab8
A
554 for (i = 0; extfeature_map[i].mask != 0; i++) {
555 if ((extfeatures & extfeature_map[i].mask) == 0)
55e303ae 556 continue;
0c530ab8 557 if (len > 0)
55e303ae 558 *p++ = ' ';
0c530ab8 559 len = min(strlen(extfeature_map[i].name), (buf_len-1)-(p-buf));
55e303ae
A
560 if (len == 0)
561 break;
0c530ab8 562 bcopy(extfeature_map[i].name, p, len);
55e303ae
A
563 p += len;
564 }
565 *p = '\0';
566 return buf;
567}
568
2d21ac55
A
569
570#if CONFIG_NO_KPRINTF_STRINGS
571void
572cpuid_feature_display(
573 __unused const char *header)
574{
575}
576
577void
578cpuid_extfeature_display(
579 __unused const char *header)
580{
581}
582
583void
584cpuid_cpu_display(
585 __unused const char *header)
586{
587}
588#else /* CONFIG_NO_KPRINTF_STRINGS */
55e303ae
A
589void
590cpuid_feature_display(
0c530ab8
A
591 const char *header)
592{
593 char buf[256];
594
595 kprintf("%s: %s\n", header,
596 cpuid_get_feature_names(cpuid_features(),
597 buf, sizeof(buf)));
598 if (cpuid_features() & CPUID_FEATURE_HTT) {
599#define s_if_plural(n) ((n > 1) ? "s" : "")
600 kprintf(" HTT: %d core%s per package;"
601 " %d logical cpu%s per package\n",
602 cpuid_cpu_info.cpuid_cores_per_package,
603 s_if_plural(cpuid_cpu_info.cpuid_cores_per_package),
604 cpuid_cpu_info.cpuid_logical_per_package,
605 s_if_plural(cpuid_cpu_info.cpuid_logical_per_package));
606 }
607}
608
609void
610cpuid_extfeature_display(
611 const char *header)
c0fea474
A
612{
613 char buf[256];
614
0c530ab8
A
615 kprintf("%s: %s\n", header,
616 cpuid_get_extfeature_names(cpuid_extfeatures(),
617 buf, sizeof(buf)));
1c79356b
A
618}
619
1c79356b
A
620void
621cpuid_cpu_display(
0c530ab8 622 const char *header)
d7e50217 623{
2d21ac55 624 if (cpuid_cpu_info.cpuid_brand_string[0] != '\0') {
0c530ab8 625 kprintf("%s: %s\n", header, cpuid_cpu_info.cpuid_brand_string);
91447636 626 }
d7e50217 627}
2d21ac55 628#endif /* !CONFIG_NO_KPRINTF_STRINGS */
d7e50217 629
55e303ae
A
630unsigned int
631cpuid_family(void)
632{
0c530ab8 633 return cpuid_info()->cpuid_family;
4452a7af
A
634}
635
0c530ab8
A
636cpu_type_t
637cpuid_cputype(void)
638{
639 return cpuid_info()->cpuid_cpu_type;
640}
641
642cpu_subtype_t
643cpuid_cpusubtype(void)
644{
645 return cpuid_info()->cpuid_cpu_subtype;
646}
647
648uint64_t
55e303ae
A
649cpuid_features(void)
650{
91447636 651 static int checked = 0;
593a1d5f 652 char fpu_arg[20] = { 0 };
0c530ab8
A
653
654 (void) cpuid_info();
91447636
A
655 if (!checked) {
656 /* check for boot-time fpu limitations */
593a1d5f 657 if (PE_parse_boot_argn("_fpu", &fpu_arg[0], sizeof (fpu_arg))) {
91447636 658 printf("limiting fpu features to: %s\n", fpu_arg);
2d21ac55 659 if (!strncmp("387", fpu_arg, sizeof("387")) || !strncmp("mmx", fpu_arg, sizeof("mmx"))) {
91447636
A
660 printf("no sse or sse2\n");
661 cpuid_cpu_info.cpuid_features &= ~(CPUID_FEATURE_SSE | CPUID_FEATURE_SSE2 | CPUID_FEATURE_FXSR);
2d21ac55 662 } else if (!strncmp("sse", fpu_arg, sizeof("sse"))) {
91447636
A
663 printf("no sse2\n");
664 cpuid_cpu_info.cpuid_features &= ~(CPUID_FEATURE_SSE2);
665 }
666 }
667 checked = 1;
668 }
55e303ae
A
669 return cpuid_cpu_info.cpuid_features;
670}
671
0c530ab8
A
672uint64_t
673cpuid_extfeatures(void)
55e303ae 674{
0c530ab8 675 return cpuid_info()->cpuid_extfeatures;
55e303ae 676}
0c530ab8 677
55e303ae 678
0c530ab8
A
679#if MACH_KDB
680
681/*
682 * Display the cpuid
683 * *
684 * cp
685 */
686void
687db_cpuid(__unused db_expr_t addr,
688 __unused int have_addr,
689 __unused db_expr_t count,
690 __unused char *modif)
691{
692
693 uint32_t i, mid;
694 uint32_t cpid[4];
695
696 do_cpuid(0, cpid); /* Get the first cpuid which is the number of
697 * basic ids */
698 db_printf("%08X - %08X %08X %08X %08X\n",
699 0, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
700
701 mid = cpid[eax]; /* Set the number */
702 for (i = 1; i <= mid; i++) { /* Dump 'em out */
703 do_cpuid(i, cpid); /* Get the next */
704 db_printf("%08X - %08X %08X %08X %08X\n",
705 i, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
706 }
707 db_printf("\n");
708
709 do_cpuid(0x80000000, cpid); /* Get the first extended cpuid which
710 * is the number of extended ids */
711 db_printf("%08X - %08X %08X %08X %08X\n",
712 0x80000000, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
713
714 mid = cpid[eax]; /* Set the number */
715 for (i = 0x80000001; i <= mid; i++) { /* Dump 'em out */
716 do_cpuid(i, cpid); /* Get the next */
717 db_printf("%08X - %08X %08X %08X %08X\n",
718 i, cpid[eax], cpid[ebx], cpid[ecx], cpid[edx]);
719 }
720}
721
722#endif