initcpu.c revision 284338
11556Srgrimes/*- 21556Srgrimes * Copyright (c) KATO Takenori, 1997, 1998. 31556Srgrimes * 41556Srgrimes * All rights reserved. Unpublished rights reserved under the copyright 51556Srgrimes * laws of Japan. 61556Srgrimes * 71556Srgrimes * Redistribution and use in source and binary forms, with or without 81556Srgrimes * modification, are permitted provided that the following conditions 91556Srgrimes * are met: 101556Srgrimes * 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer as 131556Srgrimes * the first lines of this file unmodified. 141556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 151556Srgrimes * notice, this list of conditions and the following disclaimer in the 161556Srgrimes * documentation and/or other materials provided with the distribution. 171556Srgrimes * 181556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 191556Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 201556Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 211556Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 221556Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 231556Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 241556Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 251556Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 261556Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 271556Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 281556Srgrimes */ 291556Srgrimes 301556Srgrimes#include <sys/cdefs.h> 311556Srgrimes__FBSDID("$FreeBSD: stable/10/sys/amd64/amd64/initcpu.c 284338 2015-06-13 07:31:50Z kib $"); 321556Srgrimes 331556Srgrimes#include "opt_cpu.h" 3436150Scharnier 3536150Scharnier#include <sys/param.h> 3636150Scharnier#include <sys/kernel.h> 371556Srgrimes#include <sys/pcpu.h> 3899110Sobrien#include <sys/systm.h> 3999110Sobrien#include <sys/sysctl.h> 401556Srgrimes 4117987Speter#include <machine/cputypes.h> 42149017Sstefanf#include <machine/md_var.h> 43209337Sjilles#include <machine/specialreg.h> 4417987Speter 451556Srgrimes#include <vm/vm.h> 461556Srgrimes#include <vm/pmap.h> 471556Srgrimes 481556Srgrimesstatic int hw_instruction_sse; 491556SrgrimesSYSCTL_INT(_hw, OID_AUTO, instruction_sse, CTLFLAG_RD, 501556Srgrimes &hw_instruction_sse, 0, "SIMD/MMX2 instructions available in CPU"); 511556Srgrimes/* 521556Srgrimes * -1: automatic (default) 531556Srgrimes * 0: keep enable CLFLUSH 541556Srgrimes * 1: force disable CLFLUSH 551556Srgrimes */ 561556Srgrimesstatic int hw_clflush_disable = -1; 571556Srgrimes 5817987Speterint cpu; /* Are we 386, 386sx, 486, etc? */ 5959436Scracaueru_int cpu_feature; /* Feature flags */ 60214304Sjillesu_int cpu_feature2; /* Feature flags */ 6117987Speteru_int amd_feature; /* AMD feature flags */ 621556Srgrimesu_int amd_feature2; /* AMD feature flags */ 6317987Speteru_int amd_pminfo; /* AMD advanced power management info */ 641556Srgrimesu_int via_feature_rng; /* VIA RNG features */ 651556Srgrimesu_int via_feature_xcrypt; /* VIA ACE features */ 661556Srgrimesu_int cpu_high; /* Highest arg to CPUID */ 671556Srgrimesu_int cpu_exthigh; /* Highest arg to extended CPUID */ 681556Srgrimesu_int cpu_id; /* Stepping ID */ 69142845Sobrienu_int cpu_procinfo; /* HyperThreading Info / Brand Index / CLFUSH */ 70142845Sobrienu_int cpu_procinfo2; /* Multicore info */ 711556Srgrimeschar cpu_vendor[20]; /* CPU Origin code */ 72214709Sjillesu_int cpu_vendor_id; /* CPU vendor ID */ 73214709Sjillesu_int cpu_fxsr; /* SSE enabled */ 74214709Sjillesu_int cpu_mxcsr_mask; /* Valid bits in mxcsr */ 75214709Sjillesu_int cpu_clflush_line_size = 32; 76214709Sjillesu_int cpu_stdext_feature; 771556Srgrimesu_int cpu_stdext_feature2; 7817987Speteru_int cpu_max_ext_state_size; 791556Srgrimesu_int cpu_mon_mwait_flags; /* MONITOR/MWAIT flags (CPUID.05H.ECX) */ 801556Srgrimesu_int cpu_mon_min_size; /* MONITOR minimum range size, bytes */ 811556Srgrimesu_int cpu_mon_max_size; /* MONITOR minimum range size, bytes */ 821556Srgrimesu_int cpu_maxphyaddr; /* Max phys addr width in bits */ 831556Srgrimes 841556SrgrimesSYSCTL_UINT(_hw, OID_AUTO, via_feature_rng, CTLFLAG_RD, 851556Srgrimes &via_feature_rng, 0, "VIA RNG feature available in CPU"); 861556SrgrimesSYSCTL_UINT(_hw, OID_AUTO, via_feature_xcrypt, CTLFLAG_RD, 871556Srgrimes &via_feature_xcrypt, 0, "VIA xcrypt feature available in CPU"); 881556Srgrimes 89206145Sjillesstatic void 90206145Sjillesinit_amd(void) 91206145Sjilles{ 92206145Sjilles 931556Srgrimes /* 941556Srgrimes * Work around Erratum 721 for Family 10h and 12h processors. 95213760Sobrien * These processors may incorrectly update the stack pointer 96213760Sobrien * after a long series of push and/or near-call instructions, 97213760Sobrien * or a long series of pop and/or near-return instructions. 98213760Sobrien * 991556Srgrimes * http://support.amd.com/us/Processor_TechDocs/41322_10h_Rev_Gd.pdf 100213760Sobrien * http://support.amd.com/us/Processor_TechDocs/44739_12h_Rev_Gd.pdf 1011556Srgrimes * 102213760Sobrien * Hypervisors do not provide access to the errata MSR, 103213760Sobrien * causing #GP exception on attempt to apply the errata. The 104213760Sobrien * MSR write shall be done on host and persist globally 105213760Sobrien * anyway, so do not try to do it when under virtualization. 106213760Sobrien */ 107213760Sobrien switch (CPUID_TO_FAMILY(cpu_id)) { 108213760Sobrien case 0x10: 1091556Srgrimes case 0x12: 1101556Srgrimes if ((cpu_feature2 & CPUID2_HV) == 0) 111214525Sjilles wrmsr(0xc0011029, rdmsr(0xc0011029) | 1); 112213811Sobrien break; 113213811Sobrien } 114213811Sobrien} 115213811Sobrien 116213811Sobrien/* 117213811Sobrien * Initialize special VIA features 118213811Sobrien */ 119213811Sobrienstatic void 120213811Sobrieninit_via(void) 121213811Sobrien{ 122213811Sobrien u_int regs[4], val; 123213811Sobrien 124213811Sobrien /* 125213811Sobrien * Check extended CPUID for PadLock features. 126213811Sobrien * 1271556Srgrimes * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/programming_guide.pdf 12817987Speter */ 129213811Sobrien do_cpuid(0xc0000000, regs); 130206145Sjilles if (regs[0] >= 0xc0000001) { 131206145Sjilles do_cpuid(0xc0000001, regs); 132206145Sjilles val = regs[3]; 133206145Sjilles } else 134206145Sjilles return; 135206145Sjilles 136206145Sjilles /* Enable RNG if present. */ 137206145Sjilles if ((val & VIA_CPUID_HAS_RNG) != 0) { 138206145Sjilles via_feature_rng = VIA_HAS_RNG; 139206145Sjilles wrmsr(0x110B, rdmsr(0x110B) | VIA_CPUID_DO_RNG); 140206145Sjilles } 141206145Sjilles 142206145Sjilles /* Enable PadLock if present. */ 143206145Sjilles if ((val & VIA_CPUID_HAS_ACE) != 0) 144206145Sjilles via_feature_xcrypt |= VIA_HAS_AES; 145213811Sobrien if ((val & VIA_CPUID_HAS_ACE2) != 0) 146206145Sjilles via_feature_xcrypt |= VIA_HAS_AESCTR; 147206145Sjilles if ((val & VIA_CPUID_HAS_PHE) != 0) 148206145Sjilles via_feature_xcrypt |= VIA_HAS_SHA; 149206145Sjilles if ((val & VIA_CPUID_HAS_PMM) != 0) 150206145Sjilles via_feature_xcrypt |= VIA_HAS_MM; 151206145Sjilles if (via_feature_xcrypt != 0) 152206145Sjilles wrmsr(0x1107, rdmsr(0x1107) | (1 << 28)); 153206145Sjilles} 154206145Sjilles 155206145Sjilles/* 156206145Sjilles * Initialize CPU control registers 157206145Sjilles */ 158206145Sjillesvoid 159206145Sjillesinitializecpu(void) 160213811Sobrien{ 161206145Sjilles uint64_t msr; 162206145Sjilles uint32_t cr4; 163206145Sjilles 164206145Sjilles cr4 = rcr4(); 165206145Sjilles if ((cpu_feature & CPUID_XMM) && (cpu_feature & CPUID_FXSR)) { 166206145Sjilles cr4 |= CR4_FXSR | CR4_XMM; 167206145Sjilles cpu_fxsr = hw_instruction_sse = 1; 168206145Sjilles } 169206145Sjilles if (cpu_stdext_feature & CPUID_STDEXT_FSGSBASE) 170206145Sjilles cr4 |= CR4_FSGSBASE; 171206145Sjilles 172206145Sjilles /* 173206145Sjilles * Postpone enabling the SMEP on the boot CPU until the page 174206145Sjilles * tables are switched from the boot loader identity mapping 175206145Sjilles * to the kernel tables. The boot loader enables the U bit in 176206145Sjilles * its tables. 177206145Sjilles */ 178206145Sjilles if (!IS_BSP() && (cpu_stdext_feature & CPUID_STDEXT_SMEP)) 179206145Sjilles cr4 |= CR4_SMEP; 180213811Sobrien load_cr4(cr4); 181206145Sjilles if ((amd_feature & AMDID_NX) != 0) { 182206145Sjilles msr = rdmsr(MSR_EFER) | EFER_NXE; 183206145Sjilles wrmsr(MSR_EFER, msr); 184206145Sjilles pg_nx = PG_NX; 185206145Sjilles } 186206145Sjilles switch (cpu_vendor_id) { 187206145Sjilles case CPU_VENDOR_AMD: 188206145Sjilles init_amd(); 189206145Sjilles break; 190206145Sjilles case CPU_VENDOR_CENTAUR: 191206145Sjilles init_via(); 192206145Sjilles break; 193206145Sjilles } 194206145Sjilles} 195206145Sjilles 1961556Srgrimesvoid 1971556Srgrimesinitializecpucache(void) 1981556Srgrimes{ 1991556Srgrimes 2001556Srgrimes /* 2011556Srgrimes * CPUID with %eax = 1, %ebx returns 20290111Simp * Bits 15-8: CLFLUSH line size 20317987Speter * (Value * 8 = cache line size in bytes) 2041556Srgrimes */ 2051556Srgrimes if ((cpu_feature & CPUID_CLFSH) != 0) 206206145Sjilles cpu_clflush_line_size = ((cpu_procinfo >> 8) & 0xff) * 8; 207206145Sjilles /* 208206145Sjilles * XXXKIB: (temporary) hack to work around traps generated 209206145Sjilles * when CLFLUSHing APIC register window under virtualization 210208656Sjilles * environments. These environments tend to disable the 211206145Sjilles * CPUID_SS feature even though the native CPU supports it. 21260593Scracauer */ 2131556Srgrimes TUNABLE_INT_FETCH("hw.clflush_disable", &hw_clflush_disable); 2141556Srgrimes if (vm_guest != VM_GUEST_NO && hw_clflush_disable == -1) 2151556Srgrimes cpu_feature &= ~CPUID_CLFSH; 2161556Srgrimes /* 2171556Srgrimes * Allow to disable CLFLUSH feature manually by 2181556Srgrimes * hw.clflush_disable tunable. 2191556Srgrimes */ 2201556Srgrimes if (hw_clflush_disable == 1) 2211556Srgrimes cpu_feature &= ~CPUID_CLFSH; 2221556Srgrimes} 2231556Srgrimes