1 /* $NetBSD: identcpu.c,v 1.67 2017/11/11 11:00:46 maxv Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Frank van der Linden, and by Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.67 2017/11/11 11:00:46 maxv Exp $"); 34 35 #include "opt_xen.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/device.h> 40 #include <sys/cpu.h> 41 42 #include <uvm/uvm_extern.h> 43 44 #include <machine/specialreg.h> 45 #include <machine/pio.h> 46 #include <machine/cpu.h> 47 48 #include <x86/cputypes.h> 49 #include <x86/cacheinfo.h> 50 #include <x86/cpuvar.h> 51 #include <x86/cpu_msr.h> 52 #include <x86/fpu.h> 53 54 #include <x86/x86/vmtreg.h> /* for vmt_hvcall() */ 55 #include <x86/x86/vmtvar.h> /* for vmt_hvcall() */ 56 57 static const struct x86_cache_info intel_cpuid_cache_info[] = INTEL_CACHE_INFO; 58 59 static const struct x86_cache_info amd_cpuid_l2cache_assoc_info[] = 60 AMD_L2CACHE_INFO; 61 62 static const struct x86_cache_info amd_cpuid_l3cache_assoc_info[] = 63 AMD_L3CACHE_INFO; 64 65 int cpu_vendor; 66 char cpu_brand_string[49]; 67 68 int x86_fpu_save __read_mostly; 69 unsigned int x86_fpu_save_size __read_mostly = 512; 70 uint64_t x86_xsave_features __read_mostly = 0; 71 72 /* 73 * Note: these are just the ones that may not have a cpuid instruction. 74 * We deal with the rest in a different way. 75 */ 76 const int i386_nocpuid_cpus[] = { 77 CPUVENDOR_INTEL, CPUCLASS_386, /* CPU_386SX */ 78 CPUVENDOR_INTEL, CPUCLASS_386, /* CPU_386 */ 79 CPUVENDOR_INTEL, CPUCLASS_486, /* CPU_486SX */ 80 CPUVENDOR_INTEL, CPUCLASS_486, /* CPU_486 */ 81 CPUVENDOR_CYRIX, CPUCLASS_486, /* CPU_486DLC */ 82 CPUVENDOR_CYRIX, CPUCLASS_486, /* CPU_6x86 */ 83 CPUVENDOR_NEXGEN, CPUCLASS_386, /* CPU_NX586 */ 84 }; 85 86 static const char cpu_vendor_names[][10] = { 87 "Unknown", "Intel", "NS/Cyrix", "NexGen", "AMD", "IDT/VIA", "Transmeta", 88 "Vortex86" 89 }; 90 91 static const struct x86_cache_info * 92 cache_info_lookup(const struct x86_cache_info *cai, uint8_t desc) 93 { 94 int i; 95 96 for (i = 0; cai[i].cai_desc != 0; i++) { 97 if (cai[i].cai_desc == desc) 98 return (&cai[i]); 99 } 100 101 return (NULL); 102 } 103 104 static void 105 cpu_probe_intel_cache(struct cpu_info *ci) 106 { 107 const struct x86_cache_info *cai; 108 u_int descs[4]; 109 int iterations, i, j; 110 uint8_t desc; 111 112 if (cpuid_level >= 2) { 113 /* Parse the cache info from `cpuid leaf 2', if we have it. */ 114 x86_cpuid(2, descs); 115 iterations = descs[0] & 0xff; 116 while (iterations-- > 0) { 117 for (i = 0; i < 4; i++) { 118 if (descs[i] & 0x80000000) 119 continue; 120 for (j = 0; j < 4; j++) { 121 if (i == 0 && j == 0) 122 continue; 123 desc = (descs[i] >> (j * 8)) & 0xff; 124 if (desc == 0) 125 continue; 126 cai = cache_info_lookup( 127 intel_cpuid_cache_info, desc); 128 if (cai != NULL) { 129 ci->ci_cinfo[cai->cai_index] = 130 *cai; 131 } 132 } 133 } 134 } 135 } 136 137 if (cpuid_level >= 4) { 138 int type, level; 139 int ways, partitions, linesize, sets; 140 int caitype = -1; 141 int totalsize; 142 143 /* Parse the cache info from `cpuid leaf 4', if we have it. */ 144 for (i = 0; ; i++) { 145 x86_cpuid2(4, i, descs); 146 type = __SHIFTOUT(descs[0], CPUID_DCP_CACHETYPE); 147 if (type == CPUID_DCP_CACHETYPE_N) 148 break; 149 level = __SHIFTOUT(descs[0], CPUID_DCP_CACHELEVEL); 150 switch (level) { 151 case 1: 152 if (type == CPUID_DCP_CACHETYPE_I) 153 caitype = CAI_ICACHE; 154 else if (type == CPUID_DCP_CACHETYPE_D) 155 caitype = CAI_DCACHE; 156 else 157 caitype = -1; 158 break; 159 case 2: 160 if (type == CPUID_DCP_CACHETYPE_U) 161 caitype = CAI_L2CACHE; 162 else 163 caitype = -1; 164 break; 165 case 3: 166 if (type == CPUID_DCP_CACHETYPE_U) 167 caitype = CAI_L3CACHE; 168 else 169 caitype = -1; 170 break; 171 default: 172 caitype = -1; 173 break; 174 } 175 if (caitype == -1) 176 continue; 177 178 ways = __SHIFTOUT(descs[1], CPUID_DCP_WAYS) + 1; 179 partitions =__SHIFTOUT(descs[1], CPUID_DCP_PARTITIONS) 180 + 1; 181 linesize = __SHIFTOUT(descs[1], CPUID_DCP_LINESIZE) 182 + 1; 183 sets = descs[2] + 1; 184 totalsize = ways * partitions * linesize * sets; 185 ci->ci_cinfo[caitype].cai_totalsize = totalsize; 186 ci->ci_cinfo[caitype].cai_associativity = ways; 187 ci->ci_cinfo[caitype].cai_linesize = linesize; 188 } 189 } 190 } 191 192 static void 193 cpu_probe_intel(struct cpu_info *ci) 194 { 195 196 if (cpu_vendor != CPUVENDOR_INTEL) 197 return; 198 199 cpu_probe_intel_cache(ci); 200 } 201 202 static void 203 cpu_probe_amd_cache(struct cpu_info *ci) 204 { 205 const struct x86_cache_info *cp; 206 struct x86_cache_info *cai; 207 int family, model; 208 u_int descs[4]; 209 u_int lfunc; 210 211 family = CPUID_TO_FAMILY(ci->ci_signature); 212 model = CPUID_TO_MODEL(ci->ci_signature); 213 214 /* 215 * K5 model 0 has none of this info. 216 */ 217 if (family == 5 && model == 0) 218 return; 219 220 /* 221 * Determine the largest extended function value. 222 */ 223 x86_cpuid(0x80000000, descs); 224 lfunc = descs[0]; 225 226 /* 227 * Determine L1 cache/TLB info. 228 */ 229 if (lfunc < 0x80000005) { 230 /* No L1 cache info available. */ 231 return; 232 } 233 234 x86_cpuid(0x80000005, descs); 235 236 /* 237 * K6-III and higher have large page TLBs. 238 */ 239 if ((family == 5 && model >= 9) || family >= 6) { 240 cai = &ci->ci_cinfo[CAI_ITLB2]; 241 cai->cai_totalsize = AMD_L1_EAX_ITLB_ENTRIES(descs[0]); 242 cai->cai_associativity = AMD_L1_EAX_ITLB_ASSOC(descs[0]); 243 cai->cai_linesize = (4 * 1024 * 1024); 244 245 cai = &ci->ci_cinfo[CAI_DTLB2]; 246 cai->cai_totalsize = AMD_L1_EAX_DTLB_ENTRIES(descs[0]); 247 cai->cai_associativity = AMD_L1_EAX_DTLB_ASSOC(descs[0]); 248 cai->cai_linesize = (4 * 1024 * 1024); 249 } 250 251 cai = &ci->ci_cinfo[CAI_ITLB]; 252 cai->cai_totalsize = AMD_L1_EBX_ITLB_ENTRIES(descs[1]); 253 cai->cai_associativity = AMD_L1_EBX_ITLB_ASSOC(descs[1]); 254 cai->cai_linesize = (4 * 1024); 255 256 cai = &ci->ci_cinfo[CAI_DTLB]; 257 cai->cai_totalsize = AMD_L1_EBX_DTLB_ENTRIES(descs[1]); 258 cai->cai_associativity = AMD_L1_EBX_DTLB_ASSOC(descs[1]); 259 cai->cai_linesize = (4 * 1024); 260 261 cai = &ci->ci_cinfo[CAI_DCACHE]; 262 cai->cai_totalsize = AMD_L1_ECX_DC_SIZE(descs[2]); 263 cai->cai_associativity = AMD_L1_ECX_DC_ASSOC(descs[2]); 264 cai->cai_linesize = AMD_L1_ECX_DC_LS(descs[2]); 265 266 cai = &ci->ci_cinfo[CAI_ICACHE]; 267 cai->cai_totalsize = AMD_L1_EDX_IC_SIZE(descs[3]); 268 cai->cai_associativity = AMD_L1_EDX_IC_ASSOC(descs[3]); 269 cai->cai_linesize = AMD_L1_EDX_IC_LS(descs[3]); 270 271 /* 272 * Determine L2 cache/TLB info. 273 */ 274 if (lfunc < 0x80000006) { 275 /* No L2 cache info available. */ 276 return; 277 } 278 279 x86_cpuid(0x80000006, descs); 280 281 cai = &ci->ci_cinfo[CAI_L2CACHE]; 282 cai->cai_totalsize = AMD_L2_ECX_C_SIZE(descs[2]); 283 cai->cai_associativity = AMD_L2_ECX_C_ASSOC(descs[2]); 284 cai->cai_linesize = AMD_L2_ECX_C_LS(descs[2]); 285 286 cp = cache_info_lookup(amd_cpuid_l2cache_assoc_info, 287 cai->cai_associativity); 288 if (cp != NULL) 289 cai->cai_associativity = cp->cai_associativity; 290 else 291 cai->cai_associativity = 0; /* XXX Unknown/reserved */ 292 293 if (family < 0xf) { 294 /* No L3 cache info available. */ 295 return; 296 } 297 298 cai = &ci->ci_cinfo[CAI_L3CACHE]; 299 cai->cai_totalsize = AMD_L3_EDX_C_SIZE(descs[3]); 300 cai->cai_associativity = AMD_L3_EDX_C_ASSOC(descs[3]); 301 cai->cai_linesize = AMD_L3_EDX_C_LS(descs[3]); 302 303 cp = cache_info_lookup(amd_cpuid_l3cache_assoc_info, 304 cai->cai_associativity); 305 if (cp != NULL) 306 cai->cai_associativity = cp->cai_associativity; 307 else 308 cai->cai_associativity = 0; /* XXX Unknown reserved */ 309 310 if (lfunc < 0x80000019) { 311 /* No 1GB Page TLB */ 312 return; 313 } 314 315 x86_cpuid(0x80000019, descs); 316 317 cai = &ci->ci_cinfo[CAI_L1_1GBDTLB]; 318 cai->cai_totalsize = AMD_L1_1GB_EAX_DTLB_ENTRIES(descs[1]); 319 cai->cai_associativity = AMD_L1_1GB_EAX_DTLB_ASSOC(descs[1]); 320 cai->cai_linesize = (1 * 1024); 321 322 cai = &ci->ci_cinfo[CAI_L1_1GBITLB]; 323 cai->cai_totalsize = AMD_L1_1GB_EAX_IUTLB_ENTRIES(descs[0]); 324 cai->cai_associativity = AMD_L1_1GB_EAX_IUTLB_ASSOC(descs[0]); 325 cai->cai_linesize = (1 * 1024); 326 327 cai = &ci->ci_cinfo[CAI_L2_1GBDTLB]; 328 cai->cai_totalsize = AMD_L2_1GB_EBX_DUTLB_ENTRIES(descs[1]); 329 cai->cai_associativity = AMD_L2_1GB_EBX_DUTLB_ASSOC(descs[1]); 330 cai->cai_linesize = (1 * 1024); 331 332 cai = &ci->ci_cinfo[CAI_L2_1GBITLB]; 333 cai->cai_totalsize = AMD_L2_1GB_EBX_IUTLB_ENTRIES(descs[0]); 334 cai->cai_associativity = AMD_L2_1GB_EBX_IUTLB_ASSOC(descs[0]); 335 cai->cai_linesize = (1 * 1024); 336 } 337 338 static void 339 cpu_probe_k5(struct cpu_info *ci) 340 { 341 int flag; 342 343 if (cpu_vendor != CPUVENDOR_AMD || 344 CPUID_TO_FAMILY(ci->ci_signature) != 5) 345 return; 346 347 if (CPUID_TO_MODEL(ci->ci_signature) == 0) { 348 /* 349 * According to the AMD Processor Recognition App Note, 350 * the AMD-K5 Model 0 uses the wrong bit to indicate 351 * support for global PTEs, instead using bit 9 (APIC) 352 * rather than bit 13 (i.e. "0x200" vs. 0x2000". Oops!). 353 */ 354 flag = ci->ci_feat_val[0]; 355 if ((flag & CPUID_APIC) != 0) 356 flag = (flag & ~CPUID_APIC) | CPUID_PGE; 357 ci->ci_feat_val[0] = flag; 358 } 359 360 cpu_probe_amd_cache(ci); 361 } 362 363 static void 364 cpu_probe_k678(struct cpu_info *ci) 365 { 366 367 if (cpu_vendor != CPUVENDOR_AMD || 368 CPUID_TO_FAMILY(ci->ci_signature) < 6) 369 return; 370 371 cpu_probe_amd_cache(ci); 372 } 373 374 static inline uint8_t 375 cyrix_read_reg(uint8_t reg) 376 { 377 378 outb(0x22, reg); 379 return inb(0x23); 380 } 381 382 static inline void 383 cyrix_write_reg(uint8_t reg, uint8_t data) 384 { 385 386 outb(0x22, reg); 387 outb(0x23, data); 388 } 389 390 static void 391 cpu_probe_cyrix_cmn(struct cpu_info *ci) 392 { 393 /* 394 * i8254 latch check routine: 395 * National Geode (formerly Cyrix MediaGX) has a serious bug in 396 * its built-in i8254-compatible clock module (cs5510 cs5520). 397 * Set the variable 'clock_broken_latch' to indicate it. 398 * 399 * This bug is not present in the cs5530, and the flag 400 * is disabled again in sys/arch/i386/pci/pcib.c if this later 401 * model device is detected. Ideally, this work-around should not 402 * even be in here, it should be in there. XXX 403 */ 404 uint8_t c3; 405 #ifndef XEN 406 extern int clock_broken_latch; 407 408 switch (ci->ci_signature) { 409 case 0x440: /* Cyrix MediaGX */ 410 case 0x540: /* GXm */ 411 clock_broken_latch = 1; 412 break; 413 } 414 #endif 415 416 /* set up various cyrix registers */ 417 /* 418 * Enable suspend on halt (powersave mode). 419 * When powersave mode is enabled, the TSC stops counting 420 * while the CPU is halted in idle() waiting for an interrupt. 421 * This means we can't use the TSC for interval time in 422 * microtime(9), and thus it is disabled here. 423 * 424 * It still makes a perfectly good cycle counter 425 * for program profiling, so long as you remember you're 426 * counting cycles, and not time. Further, if you don't 427 * mind not using powersave mode, the TSC works just fine, 428 * so this should really be optional. XXX 429 */ 430 cyrix_write_reg(0xc2, cyrix_read_reg(0xc2) | 0x08); 431 432 /* 433 * Do not disable the TSC on the Geode GX, it's reported to 434 * work fine. 435 */ 436 if (ci->ci_signature != 0x552) 437 ci->ci_feat_val[0] &= ~CPUID_TSC; 438 439 /* enable access to ccr4/ccr5 */ 440 c3 = cyrix_read_reg(0xC3); 441 cyrix_write_reg(0xC3, c3 | 0x10); 442 /* cyrix's workaround for the "coma bug" */ 443 cyrix_write_reg(0x31, cyrix_read_reg(0x31) | 0xf8); 444 cyrix_write_reg(0x32, cyrix_read_reg(0x32) | 0x7f); 445 cyrix_write_reg(0x33, cyrix_read_reg(0x33) & ~0xff); 446 cyrix_write_reg(0x3c, cyrix_read_reg(0x3c) | 0x87); 447 /* disable access to ccr4/ccr5 */ 448 cyrix_write_reg(0xC3, c3); 449 } 450 451 static void 452 cpu_probe_cyrix(struct cpu_info *ci) 453 { 454 455 if (cpu_vendor != CPUVENDOR_CYRIX || 456 CPUID_TO_FAMILY(ci->ci_signature) < 4 || 457 CPUID_TO_FAMILY(ci->ci_signature) > 6) 458 return; 459 460 cpu_probe_cyrix_cmn(ci); 461 } 462 463 static void 464 cpu_probe_winchip(struct cpu_info *ci) 465 { 466 467 if (cpu_vendor != CPUVENDOR_IDT) 468 return; 469 470 switch (CPUID_TO_FAMILY(ci->ci_signature)) { 471 case 5: 472 /* WinChip C6 */ 473 if (CPUID_TO_MODEL(ci->ci_signature) == 4) 474 ci->ci_feat_val[0] &= ~CPUID_TSC; 475 break; 476 case 6: 477 /* 478 * VIA Eden ESP 479 * 480 * Quoting from page 3-4 of: "VIA Eden ESP Processor Datasheet" 481 * http://www.via.com.tw/download/mainboards/6/14/Eden20v115.pdf 482 * 483 * 1. The CMPXCHG8B instruction is provided and always enabled, 484 * however, it appears disabled in the corresponding CPUID 485 * function bit 0 to avoid a bug in an early version of 486 * Windows NT. However, this default can be changed via a 487 * bit in the FCR MSR. 488 */ 489 ci->ci_feat_val[0] |= CPUID_CX8; 490 wrmsr(MSR_VIA_FCR, rdmsr(MSR_VIA_FCR) | 0x00000001); 491 break; 492 } 493 } 494 495 static void 496 cpu_probe_c3(struct cpu_info *ci) 497 { 498 u_int family, model, stepping, descs[4], lfunc, msr; 499 struct x86_cache_info *cai; 500 501 if (cpu_vendor != CPUVENDOR_IDT || 502 CPUID_TO_FAMILY(ci->ci_signature) < 6) 503 return; 504 505 family = CPUID_TO_FAMILY(ci->ci_signature); 506 model = CPUID_TO_MODEL(ci->ci_signature); 507 stepping = CPUID_TO_STEPPING(ci->ci_signature); 508 509 /* Determine the largest extended function value. */ 510 x86_cpuid(0x80000000, descs); 511 lfunc = descs[0]; 512 513 if (family > 6 || model > 0x9 || (model == 0x9 && stepping >= 3)) { 514 /* Nehemiah or Esther */ 515 x86_cpuid(0xc0000000, descs); 516 lfunc = descs[0]; 517 if (lfunc >= 0xc0000001) { /* has ACE, RNG */ 518 int rng_enable = 0, ace_enable = 0; 519 x86_cpuid(0xc0000001, descs); 520 lfunc = descs[3]; 521 ci->ci_feat_val[4] = lfunc; 522 /* Check for and enable RNG */ 523 if (lfunc & CPUID_VIA_HAS_RNG) { 524 if (!(lfunc & CPUID_VIA_DO_RNG)) { 525 rng_enable++; 526 ci->ci_feat_val[4] |= CPUID_VIA_DO_RNG; 527 } 528 } 529 /* Check for and enable ACE (AES-CBC) */ 530 if (lfunc & CPUID_VIA_HAS_ACE) { 531 if (!(lfunc & CPUID_VIA_DO_ACE)) { 532 ace_enable++; 533 ci->ci_feat_val[4] |= CPUID_VIA_DO_ACE; 534 } 535 } 536 /* Check for and enable SHA */ 537 if (lfunc & CPUID_VIA_HAS_PHE) { 538 if (!(lfunc & CPUID_VIA_DO_PHE)) { 539 ace_enable++; 540 ci->ci_feat_val[4] |= CPUID_VIA_DO_PHE; 541 } 542 } 543 /* Check for and enable ACE2 (AES-CTR) */ 544 if (lfunc & CPUID_VIA_HAS_ACE2) { 545 if (!(lfunc & CPUID_VIA_DO_ACE2)) { 546 ace_enable++; 547 ci->ci_feat_val[4] |= CPUID_VIA_DO_ACE2; 548 } 549 } 550 /* Check for and enable PMM (modmult engine) */ 551 if (lfunc & CPUID_VIA_HAS_PMM) { 552 if (!(lfunc & CPUID_VIA_DO_PMM)) { 553 ace_enable++; 554 ci->ci_feat_val[4] |= CPUID_VIA_DO_PMM; 555 } 556 } 557 558 /* 559 * Actually do the enables. It's a little gross, 560 * but per the PadLock programming guide, "Enabling 561 * PadLock", condition 3, we must enable SSE too or 562 * else the first use of RNG or ACE instructions 563 * will generate a trap. 564 * 565 * We must do this early because of kernel RNG 566 * initialization but it is safe without the full 567 * FPU-detect as all these CPUs have SSE. 568 */ 569 lcr4(rcr4() | CR4_OSFXSR); 570 571 if (rng_enable) { 572 msr = rdmsr(MSR_VIA_RNG); 573 msr |= MSR_VIA_RNG_ENABLE; 574 /* C7 stepping 8 and subsequent CPUs have dual RNG */ 575 if (model > 0xA || (model == 0xA && stepping > 0x7)) { 576 msr |= MSR_VIA_RNG_2NOISE; 577 } 578 wrmsr(MSR_VIA_RNG, msr); 579 } 580 581 if (ace_enable) { 582 msr = rdmsr(MSR_VIA_ACE); 583 wrmsr(MSR_VIA_ACE, msr | MSR_VIA_ACE_ENABLE); 584 } 585 586 } 587 } 588 589 /* 590 * Determine L1 cache/TLB info. 591 */ 592 if (lfunc < 0x80000005) { 593 /* No L1 cache info available. */ 594 return; 595 } 596 597 x86_cpuid(0x80000005, descs); 598 599 cai = &ci->ci_cinfo[CAI_ITLB]; 600 cai->cai_totalsize = VIA_L1_EBX_ITLB_ENTRIES(descs[1]); 601 cai->cai_associativity = VIA_L1_EBX_ITLB_ASSOC(descs[1]); 602 cai->cai_linesize = (4 * 1024); 603 604 cai = &ci->ci_cinfo[CAI_DTLB]; 605 cai->cai_totalsize = VIA_L1_EBX_DTLB_ENTRIES(descs[1]); 606 cai->cai_associativity = VIA_L1_EBX_DTLB_ASSOC(descs[1]); 607 cai->cai_linesize = (4 * 1024); 608 609 cai = &ci->ci_cinfo[CAI_DCACHE]; 610 cai->cai_totalsize = VIA_L1_ECX_DC_SIZE(descs[2]); 611 cai->cai_associativity = VIA_L1_ECX_DC_ASSOC(descs[2]); 612 cai->cai_linesize = VIA_L1_EDX_IC_LS(descs[2]); 613 if (family == 6 && model == 9 && stepping == 8) { 614 /* Erratum: stepping 8 reports 4 when it should be 2 */ 615 cai->cai_associativity = 2; 616 } 617 618 cai = &ci->ci_cinfo[CAI_ICACHE]; 619 cai->cai_totalsize = VIA_L1_EDX_IC_SIZE(descs[3]); 620 cai->cai_associativity = VIA_L1_EDX_IC_ASSOC(descs[3]); 621 cai->cai_linesize = VIA_L1_EDX_IC_LS(descs[3]); 622 if (family == 6 && model == 9 && stepping == 8) { 623 /* Erratum: stepping 8 reports 4 when it should be 2 */ 624 cai->cai_associativity = 2; 625 } 626 627 /* 628 * Determine L2 cache/TLB info. 629 */ 630 if (lfunc < 0x80000006) { 631 /* No L2 cache info available. */ 632 return; 633 } 634 635 x86_cpuid(0x80000006, descs); 636 637 cai = &ci->ci_cinfo[CAI_L2CACHE]; 638 if (family > 6 || model >= 9) { 639 cai->cai_totalsize = VIA_L2N_ECX_C_SIZE(descs[2]); 640 cai->cai_associativity = VIA_L2N_ECX_C_ASSOC(descs[2]); 641 cai->cai_linesize = VIA_L2N_ECX_C_LS(descs[2]); 642 } else { 643 cai->cai_totalsize = VIA_L2_ECX_C_SIZE(descs[2]); 644 cai->cai_associativity = VIA_L2_ECX_C_ASSOC(descs[2]); 645 cai->cai_linesize = VIA_L2_ECX_C_LS(descs[2]); 646 } 647 } 648 649 static void 650 cpu_probe_geode(struct cpu_info *ci) 651 { 652 653 if (memcmp("Geode by NSC", ci->ci_vendor, 12) != 0 || 654 CPUID_TO_FAMILY(ci->ci_signature) != 5) 655 return; 656 657 cpu_probe_cyrix_cmn(ci); 658 cpu_probe_amd_cache(ci); 659 } 660 661 static void 662 cpu_probe_vortex86(struct cpu_info *ci) 663 { 664 #define PCI_MODE1_ADDRESS_REG 0x0cf8 665 #define PCI_MODE1_DATA_REG 0x0cfc 666 #define PCI_MODE1_ENABLE 0x80000000UL 667 668 uint32_t reg; 669 670 if (cpu_vendor != CPUVENDOR_VORTEX86) 671 return; 672 /* 673 * CPU model available from "Customer ID register" in 674 * North Bridge Function 0 PCI space 675 * we can't use pci_conf_read() because the PCI subsystem is not 676 * not initialised early enough 677 */ 678 679 outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE | 0x90); 680 reg = inl(PCI_MODE1_DATA_REG); 681 682 switch(reg) { 683 case 0x31504d44: 684 strcpy(cpu_brand_string, "Vortex86SX"); 685 break; 686 case 0x32504d44: 687 strcpy(cpu_brand_string, "Vortex86DX"); 688 break; 689 case 0x33504d44: 690 strcpy(cpu_brand_string, "Vortex86MX"); 691 break; 692 case 0x37504d44: 693 strcpy(cpu_brand_string, "Vortex86EX"); 694 break; 695 default: 696 strcpy(cpu_brand_string, "Unknown Vortex86"); 697 break; 698 } 699 700 #undef PCI_MODE1_ENABLE 701 #undef PCI_MODE1_ADDRESS_REG 702 #undef PCI_MODE1_DATA_REG 703 } 704 705 static void 706 cpu_probe_old_fpu(struct cpu_info *ci) 707 { 708 #if defined(__i386__) && !defined(XEN) 709 710 clts(); 711 fninit(); 712 713 /* Check for 'FDIV' bug on the original Pentium */ 714 if (npx586bug1(4195835, 3145727) != 0) 715 /* NB 120+MHz cpus are not affected */ 716 i386_fpu_fdivbug = 1; 717 718 stts(); 719 #endif 720 } 721 722 static void 723 cpu_probe_fpu(struct cpu_info *ci) 724 { 725 u_int descs[4]; 726 727 x86_fpu_save = FPU_SAVE_FSAVE; 728 729 #ifdef i386 /* amd64 always has fxsave, sse and sse2 */ 730 /* If we have FXSAVE/FXRESTOR, use them. */ 731 if ((ci->ci_feat_val[0] & CPUID_FXSR) == 0) { 732 i386_use_fxsave = 0; 733 /* Allow for no fpu even if cpuid is supported */ 734 cpu_probe_old_fpu(ci); 735 return; 736 } 737 738 i386_use_fxsave = 1; 739 /* 740 * If we have SSE/SSE2, enable XMM exceptions, and 741 * notify userland. 742 */ 743 if (ci->ci_feat_val[0] & CPUID_SSE) 744 i386_has_sse = 1; 745 if (ci->ci_feat_val[0] & CPUID_SSE2) 746 i386_has_sse2 = 1; 747 #else 748 /* 749 * For amd64 i386_use_fxsave, i386_has_sse and i386_has_sse2 are 750 * #defined to 1. 751 */ 752 #endif /* i386 */ 753 754 x86_fpu_save = FPU_SAVE_FXSAVE; 755 756 /* See if xsave (for AVX) is supported */ 757 if ((ci->ci_feat_val[1] & CPUID2_XSAVE) == 0) 758 return; 759 760 x86_fpu_save = FPU_SAVE_XSAVE; 761 762 /* xsaveopt ought to be faster than xsave */ 763 x86_cpuid2(0xd, 1, descs); 764 if (descs[0] & CPUID_PES1_XSAVEOPT) 765 x86_fpu_save = FPU_SAVE_XSAVEOPT; 766 767 /* Get features and maximum size of the save area */ 768 x86_cpuid(0xd, descs); 769 if (descs[2] > 512) 770 x86_fpu_save_size = descs[2]; 771 772 #ifdef XEN 773 /* Don't use xsave, force fxsave with x86_xsave_features = 0. */ 774 x86_fpu_save = FPU_SAVE_FXSAVE; 775 #else 776 x86_xsave_features = (uint64_t)descs[3] << 32 | descs[0]; 777 #endif 778 } 779 780 void 781 cpu_probe(struct cpu_info *ci) 782 { 783 u_int descs[4]; 784 int i; 785 uint32_t miscbytes; 786 uint32_t brand[12]; 787 788 cpu_vendor = i386_nocpuid_cpus[cputype << 1]; 789 cpu_class = i386_nocpuid_cpus[(cputype << 1) + 1]; 790 791 if (cpuid_level < 0) { 792 /* cpuid instruction not supported */ 793 cpu_probe_old_fpu(ci); 794 return; 795 } 796 797 for (i = 0; i < __arraycount(ci->ci_feat_val); i++) { 798 ci->ci_feat_val[i] = 0; 799 } 800 801 x86_cpuid(0, descs); 802 cpuid_level = descs[0]; 803 ci->ci_max_cpuid = descs[0]; 804 805 ci->ci_vendor[0] = descs[1]; 806 ci->ci_vendor[2] = descs[2]; 807 ci->ci_vendor[1] = descs[3]; 808 ci->ci_vendor[3] = 0; 809 810 if (memcmp(ci->ci_vendor, "GenuineIntel", 12) == 0) 811 cpu_vendor = CPUVENDOR_INTEL; 812 else if (memcmp(ci->ci_vendor, "AuthenticAMD", 12) == 0) 813 cpu_vendor = CPUVENDOR_AMD; 814 else if (memcmp(ci->ci_vendor, "CyrixInstead", 12) == 0) 815 cpu_vendor = CPUVENDOR_CYRIX; 816 else if (memcmp(ci->ci_vendor, "Geode by NSC", 12) == 0) 817 cpu_vendor = CPUVENDOR_CYRIX; 818 else if (memcmp(ci->ci_vendor, "CentaurHauls", 12) == 0) 819 cpu_vendor = CPUVENDOR_IDT; 820 else if (memcmp(ci->ci_vendor, "GenuineTMx86", 12) == 0) 821 cpu_vendor = CPUVENDOR_TRANSMETA; 822 else if (memcmp(ci->ci_vendor, "Vortex86 SoC", 12) == 0) 823 cpu_vendor = CPUVENDOR_VORTEX86; 824 else 825 cpu_vendor = CPUVENDOR_UNKNOWN; 826 827 if (cpuid_level >= 1) { 828 x86_cpuid(1, descs); 829 ci->ci_signature = descs[0]; 830 miscbytes = descs[1]; 831 ci->ci_feat_val[1] = descs[2]; 832 ci->ci_feat_val[0] = descs[3]; 833 834 /* Determine family + class. */ 835 cpu_class = CPUID_TO_FAMILY(ci->ci_signature) 836 + (CPUCLASS_386 - 3); 837 if (cpu_class > CPUCLASS_686) 838 cpu_class = CPUCLASS_686; 839 840 /* CLFLUSH line size is next 8 bits */ 841 if (ci->ci_feat_val[0] & CPUID_CFLUSH) 842 ci->ci_cflush_lsize 843 = __SHIFTOUT(miscbytes, CPUID_CLFUSH_SIZE) << 3; 844 ci->ci_initapicid = __SHIFTOUT(miscbytes, CPUID_LOCAL_APIC_ID); 845 } 846 847 /* 848 * Get the basic information from the extended cpuid leafs. 849 * These were first implemented by amd, but most of the values 850 * match with those generated by modern intel cpus. 851 */ 852 x86_cpuid(0x80000000, descs); 853 if (descs[0] >= 0x80000000) 854 ci->ci_max_ext_cpuid = descs[0]; 855 else 856 ci->ci_max_ext_cpuid = 0; 857 858 if (ci->ci_max_ext_cpuid >= 0x80000001) { 859 /* Determine the extended feature flags. */ 860 x86_cpuid(0x80000001, descs); 861 ci->ci_feat_val[3] = descs[2]; /* %ecx */ 862 ci->ci_feat_val[2] = descs[3]; /* %edx */ 863 } 864 865 if (ci->ci_max_ext_cpuid >= 0x80000004) { 866 x86_cpuid(0x80000002, brand); 867 x86_cpuid(0x80000003, brand + 4); 868 x86_cpuid(0x80000004, brand + 8); 869 /* Skip leading spaces on brand */ 870 for (i = 0; i < 48; i++) { 871 if (((char *) brand)[i] != ' ') 872 break; 873 } 874 memcpy(cpu_brand_string, ((char *) brand) + i, 48 - i); 875 } 876 877 /* 878 * Get the structured extended features. 879 */ 880 if (cpuid_level >= 7) { 881 x86_cpuid(7, descs); 882 ci->ci_feat_val[5] = descs[1]; /* %ebx */ 883 ci->ci_feat_val[6] = descs[2]; /* %ecx */ 884 } 885 886 cpu_probe_intel(ci); 887 cpu_probe_k5(ci); 888 cpu_probe_k678(ci); 889 cpu_probe_cyrix(ci); 890 cpu_probe_winchip(ci); 891 cpu_probe_c3(ci); 892 cpu_probe_geode(ci); 893 cpu_probe_vortex86(ci); 894 895 cpu_probe_fpu(ci); 896 897 x86_cpu_topology(ci); 898 899 if (cpu_vendor != CPUVENDOR_AMD && (ci->ci_feat_val[0] & CPUID_TM) && 900 (rdmsr(MSR_MISC_ENABLE) & (1 << 3)) == 0) { 901 /* Enable thermal monitor 1. */ 902 wrmsr(MSR_MISC_ENABLE, rdmsr(MSR_MISC_ENABLE) | (1<<3)); 903 } 904 905 ci->ci_feat_val[0] &= ~CPUID_FEAT_BLACKLIST; 906 if (ci == &cpu_info_primary) { 907 /* If first. Boot Processor is the cpu_feature reference. */ 908 for (i = 0; i < __arraycount(cpu_feature); i++) { 909 cpu_feature[i] = ci->ci_feat_val[i]; 910 } 911 identify_hypervisor(); 912 #ifndef XEN 913 /* Early patch of text segment. */ 914 x86_patch(true); 915 #endif 916 } else { 917 /* 918 * If not first. Warn about cpu_feature mismatch for 919 * secondary CPUs. 920 */ 921 for (i = 0; i < __arraycount(cpu_feature); i++) { 922 if (cpu_feature[i] != ci->ci_feat_val[i]) 923 aprint_error_dev(ci->ci_dev, 924 "feature mismatch: cpu_feature[%d] is " 925 "%#x, but CPU reported %#x\n", 926 i, cpu_feature[i], ci->ci_feat_val[i]); 927 } 928 } 929 } 930 931 /* Write what we know about the cpu to the console... */ 932 void 933 cpu_identify(struct cpu_info *ci) 934 { 935 936 cpu_setmodel("%s %d86-class", 937 cpu_vendor_names[cpu_vendor], cpu_class + 3); 938 if (cpu_brand_string[0] != '\0') { 939 aprint_normal_dev(ci->ci_dev, "%s", cpu_brand_string); 940 } else { 941 aprint_normal_dev(ci->ci_dev, "%s", cpu_getmodel()); 942 if (ci->ci_data.cpu_cc_freq != 0) 943 aprint_normal(", %dMHz", 944 (int)(ci->ci_data.cpu_cc_freq / 1000000)); 945 } 946 if (ci->ci_signature != 0) 947 aprint_normal(", id 0x%x", ci->ci_signature); 948 aprint_normal("\n"); 949 aprint_normal_dev(ci->ci_dev, "package %lu, core %lu, smt %lu\n", 950 ci->ci_package_id, ci->ci_core_id, ci->ci_smt_id); 951 if (cpu_brand_string[0] == '\0') { 952 strlcpy(cpu_brand_string, cpu_getmodel(), 953 sizeof(cpu_brand_string)); 954 } 955 if (cpu_class == CPUCLASS_386) { 956 panic("NetBSD requires an 80486DX or later processor"); 957 } 958 if (cputype == CPU_486DLC) { 959 aprint_error("WARNING: BUGGY CYRIX CACHE\n"); 960 } 961 962 #if !defined(XEN) || defined(DOM0OPS) /* on Xen rdmsr is for Dom0 only */ 963 if (cpu_vendor == CPUVENDOR_AMD /* check enablement of an */ 964 && device_unit(ci->ci_dev) == 0 /* AMD feature only once */ 965 && ((cpu_feature[3] & CPUID_SVM) == CPUID_SVM)) { 966 uint64_t val; 967 968 val = rdmsr(MSR_VMCR); 969 if (((val & VMCR_SVMED) == VMCR_SVMED) 970 && ((val & VMCR_LOCK) == VMCR_LOCK)) { 971 aprint_normal_dev(ci->ci_dev, 972 "SVM disabled by the BIOS\n"); 973 } 974 } 975 #endif 976 977 #ifdef i386 978 if (i386_fpu_fdivbug == 1) 979 aprint_normal_dev(ci->ci_dev, 980 "WARNING: Pentium FDIV bug detected!\n"); 981 982 if (cpu_vendor == CPUVENDOR_TRANSMETA) { 983 u_int descs[4]; 984 x86_cpuid(0x80860000, descs); 985 if (descs[0] >= 0x80860007) 986 /* Create longrun sysctls */ 987 tmx86_init_longrun(); 988 } 989 #endif /* i386 */ 990 991 } 992 993 /* 994 * Hypervisor 995 */ 996 vm_guest_t vm_guest = VM_GUEST_NO; 997 998 static const char * const vm_bios_vendors[] = { 999 "QEMU", /* QEMU */ 1000 "Plex86", /* Plex86 */ 1001 "Bochs", /* Bochs */ 1002 "Xen", /* Xen */ 1003 "BHYVE", /* bhyve */ 1004 "Seabios", /* KVM */ 1005 }; 1006 1007 static const char * const vm_system_products[] = { 1008 "VMware Virtual Platform", /* VMWare VM */ 1009 "Virtual Machine", /* Microsoft VirtualPC */ 1010 "VirtualBox", /* Sun xVM VirtualBox */ 1011 "Parallels Virtual Platform", /* Parallels VM */ 1012 "KVM", /* KVM */ 1013 }; 1014 1015 void 1016 identify_hypervisor(void) 1017 { 1018 u_int regs[6]; 1019 char hv_vendor[12]; 1020 const char *p; 1021 int i; 1022 1023 if (vm_guest != VM_GUEST_NO) 1024 return; 1025 1026 /* 1027 * [RFC] CPUID usage for interaction between Hypervisors and Linux. 1028 * http://lkml.org/lkml/2008/10/1/246 1029 * 1030 * KB1009458: Mechanisms to determine if software is running in 1031 * a VMware virtual machine 1032 * http://kb.vmware.com/kb/1009458 1033 */ 1034 if (ISSET(cpu_feature[1], CPUID2_RAZ)) { 1035 vm_guest = VM_GUEST_VM; 1036 x86_cpuid(0x40000000, regs); 1037 if (regs[0] >= 0x40000000) { 1038 memcpy(&hv_vendor[0], ®s[1], sizeof(*regs)); 1039 memcpy(&hv_vendor[4], ®s[2], sizeof(*regs)); 1040 memcpy(&hv_vendor[8], ®s[3], sizeof(*regs)); 1041 if (memcmp(hv_vendor, "VMwareVMware", 12) == 0) 1042 vm_guest = VM_GUEST_VMWARE; 1043 else if (memcmp(hv_vendor, "Microsoft Hv", 12) == 0) 1044 vm_guest = VM_GUEST_HV; 1045 else if (memcmp(hv_vendor, "KVMKVMKVM\0\0\0", 12) == 0) 1046 vm_guest = VM_GUEST_KVM; 1047 /* FreeBSD bhyve: "bhyve bhyve " */ 1048 /* OpenBSD vmm: "OpenBSDVMM58" */ 1049 } 1050 return; 1051 } 1052 1053 /* 1054 * Examine SMBIOS strings for older hypervisors. 1055 */ 1056 p = pmf_get_platform("system-serial"); 1057 if (p != NULL) { 1058 if (strncmp(p, "VMware-", 7) == 0 || strncmp(p, "VMW", 3) == 0) { 1059 vmt_hvcall(VM_CMD_GET_VERSION, regs); 1060 if (regs[1] == VM_MAGIC) { 1061 vm_guest = VM_GUEST_VMWARE; 1062 return; 1063 } 1064 } 1065 } 1066 p = pmf_get_platform("bios-vendor"); 1067 if (p != NULL) { 1068 for (i = 0; i < __arraycount(vm_bios_vendors); i++) { 1069 if (strcmp(p, vm_bios_vendors[i]) == 0) { 1070 vm_guest = VM_GUEST_VM; 1071 return; 1072 } 1073 } 1074 } 1075 p = pmf_get_platform("system-product"); 1076 if (p != NULL) { 1077 for (i = 0; i < __arraycount(vm_system_products); i++) { 1078 if (strcmp(p, vm_system_products[i]) == 0) { 1079 vm_guest = VM_GUEST_VM; 1080 return; 1081 } 1082 } 1083 } 1084 } 1085