1cda07865SPeter Wemm /*- 24536af6aSKATO Takenori * Copyright (c) KATO Takenori, 1997, 1998. 3a8e282d6SKATO Takenori * 4a8e282d6SKATO Takenori * All rights reserved. Unpublished rights reserved under the copyright 5a8e282d6SKATO Takenori * laws of Japan. 6a8e282d6SKATO Takenori * 7a8e282d6SKATO Takenori * Redistribution and use in source and binary forms, with or without 8a8e282d6SKATO Takenori * modification, are permitted provided that the following conditions 9a8e282d6SKATO Takenori * are met: 10a8e282d6SKATO Takenori * 11a8e282d6SKATO Takenori * 1. Redistributions of source code must retain the above copyright 12a8e282d6SKATO Takenori * notice, this list of conditions and the following disclaimer as 13a8e282d6SKATO Takenori * the first lines of this file unmodified. 14a8e282d6SKATO Takenori * 2. Redistributions in binary form must reproduce the above copyright 15a8e282d6SKATO Takenori * notice, this list of conditions and the following disclaimer in the 16a8e282d6SKATO Takenori * documentation and/or other materials provided with the distribution. 17a8e282d6SKATO Takenori * 18a8e282d6SKATO Takenori * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19a8e282d6SKATO Takenori * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20a8e282d6SKATO Takenori * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21a8e282d6SKATO Takenori * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22a8e282d6SKATO Takenori * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23a8e282d6SKATO Takenori * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24a8e282d6SKATO Takenori * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25a8e282d6SKATO Takenori * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26a8e282d6SKATO Takenori * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27a8e282d6SKATO Takenori * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28a8e282d6SKATO Takenori */ 29a8e282d6SKATO Takenori 3056ae44c5SDavid E. O'Brien #include <sys/cdefs.h> 3156ae44c5SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3256ae44c5SDavid E. O'Brien 33a8e282d6SKATO Takenori #include "opt_cpu.h" 34a8e282d6SKATO Takenori 35a8e282d6SKATO Takenori #include <sys/param.h> 36a8e282d6SKATO Takenori #include <sys/kernel.h> 37cd9e9d1bSKonstantin Belousov #include <sys/pcpu.h> 38a8e282d6SKATO Takenori #include <sys/systm.h> 399d146ac5SPeter Wemm #include <sys/sysctl.h> 40a8e282d6SKATO Takenori 41a8e282d6SKATO Takenori #include <machine/cputypes.h> 42a8e282d6SKATO Takenori #include <machine/md_var.h> 43a8e282d6SKATO Takenori #include <machine/specialreg.h> 44a8e282d6SKATO Takenori 45430e272cSPeter Wemm #include <vm/vm.h> 46430e272cSPeter Wemm #include <vm/pmap.h> 4720916c1fSKATO Takenori 4810deca7eSJohn Baldwin static int hw_instruction_sse; 499d146ac5SPeter Wemm SYSCTL_INT(_hw, OID_AUTO, instruction_sse, CTLFLAG_RD, 5010deca7eSJohn Baldwin &hw_instruction_sse, 0, "SIMD/MMX2 instructions available in CPU"); 51*cd155b56SDon Lewis static int lower_sharedpage_init; 52*cd155b56SDon Lewis int hw_lower_amd64_sharedpage; 53*cd155b56SDon Lewis SYSCTL_INT(_hw, OID_AUTO, lower_amd64_sharedpage, CTLFLAG_RDTUN, 54*cd155b56SDon Lewis &hw_lower_amd64_sharedpage, 0, 55*cd155b56SDon Lewis "Lower sharedpage to work around Ryzen issue with executing code near the top of user memory"); 566f5c96c4SJun Kuriyama /* 576f5c96c4SJun Kuriyama * -1: automatic (default) 586f5c96c4SJun Kuriyama * 0: keep enable CLFLUSH 596f5c96c4SJun Kuriyama * 1: force disable CLFLUSH 606f5c96c4SJun Kuriyama */ 616f5c96c4SJun Kuriyama static int hw_clflush_disable = -1; 629d146ac5SPeter Wemm 633ce5dbccSJung-uk Kim static void 643ce5dbccSJung-uk Kim init_amd(void) 653ce5dbccSJung-uk Kim { 66f9ac50acSAndriy Gapon uint64_t msr; 673ce5dbccSJung-uk Kim 683ce5dbccSJung-uk Kim /* 693ce5dbccSJung-uk Kim * Work around Erratum 721 for Family 10h and 12h processors. 703ce5dbccSJung-uk Kim * These processors may incorrectly update the stack pointer 713ce5dbccSJung-uk Kim * after a long series of push and/or near-call instructions, 723ce5dbccSJung-uk Kim * or a long series of pop and/or near-return instructions. 733ce5dbccSJung-uk Kim * 743ce5dbccSJung-uk Kim * http://support.amd.com/us/Processor_TechDocs/41322_10h_Rev_Gd.pdf 753ce5dbccSJung-uk Kim * http://support.amd.com/us/Processor_TechDocs/44739_12h_Rev_Gd.pdf 7665211d02SKonstantin Belousov * 7765211d02SKonstantin Belousov * Hypervisors do not provide access to the errata MSR, 7865211d02SKonstantin Belousov * causing #GP exception on attempt to apply the errata. The 7965211d02SKonstantin Belousov * MSR write shall be done on host and persist globally 8065211d02SKonstantin Belousov * anyway, so do not try to do it when under virtualization. 813ce5dbccSJung-uk Kim */ 823ce5dbccSJung-uk Kim switch (CPUID_TO_FAMILY(cpu_id)) { 833ce5dbccSJung-uk Kim case 0x10: 843ce5dbccSJung-uk Kim case 0x12: 8565211d02SKonstantin Belousov if ((cpu_feature2 & CPUID2_HV) == 0) 863ce5dbccSJung-uk Kim wrmsr(0xc0011029, rdmsr(0xc0011029) | 1); 873ce5dbccSJung-uk Kim break; 883ce5dbccSJung-uk Kim } 89e5e44520SAndriy Gapon 90e5e44520SAndriy Gapon /* 91e5e44520SAndriy Gapon * BIOS may fail to set InitApicIdCpuIdLo to 1 as it should per BKDG. 92e5e44520SAndriy Gapon * So, do it here or otherwise some tools could be confused by 93e5e44520SAndriy Gapon * Initial Local APIC ID reported with CPUID Function 1 in EBX. 94e5e44520SAndriy Gapon */ 95e5e44520SAndriy Gapon if (CPUID_TO_FAMILY(cpu_id) == 0x10) { 96e5e44520SAndriy Gapon if ((cpu_feature2 & CPUID2_HV) == 0) { 97e5e44520SAndriy Gapon msr = rdmsr(MSR_NB_CFG1); 98e5e44520SAndriy Gapon msr |= (uint64_t)1 << 54; 99e5e44520SAndriy Gapon wrmsr(MSR_NB_CFG1, msr); 100e5e44520SAndriy Gapon } 101e5e44520SAndriy Gapon } 102a2d87b79SAndriy Gapon 103a2d87b79SAndriy Gapon /* 104a2d87b79SAndriy Gapon * BIOS may configure Family 10h processors to convert WC+ cache type 105a2d87b79SAndriy Gapon * to CD. That can hurt performance of guest VMs using nested paging. 106a2d87b79SAndriy Gapon * The relevant MSR bit is not documented in the BKDG, 107a2d87b79SAndriy Gapon * the fix is borrowed from Linux. 108a2d87b79SAndriy Gapon */ 109a2d87b79SAndriy Gapon if (CPUID_TO_FAMILY(cpu_id) == 0x10) { 110a2d87b79SAndriy Gapon if ((cpu_feature2 & CPUID2_HV) == 0) { 111a2d87b79SAndriy Gapon msr = rdmsr(0xc001102a); 112a2d87b79SAndriy Gapon msr &= ~((uint64_t)1 << 24); 113a2d87b79SAndriy Gapon wrmsr(0xc001102a, msr); 114a2d87b79SAndriy Gapon } 115a2d87b79SAndriy Gapon } 116f1382605SAndriy Gapon 117f1382605SAndriy Gapon /* 118f1382605SAndriy Gapon * Work around Erratum 793: Specific Combination of Writes to Write 119f1382605SAndriy Gapon * Combined Memory Types and Locked Instructions May Cause Core Hang. 120f1382605SAndriy Gapon * See Revision Guide for AMD Family 16h Models 00h-0Fh Processors, 121f1382605SAndriy Gapon * revision 3.04 or later, publication 51810. 122f1382605SAndriy Gapon */ 123f1382605SAndriy Gapon if (CPUID_TO_FAMILY(cpu_id) == 0x16 && CPUID_TO_MODEL(cpu_id) <= 0xf) { 124f1382605SAndriy Gapon if ((cpu_feature2 & CPUID2_HV) == 0) { 125f1382605SAndriy Gapon msr = rdmsr(0xc0011020); 126f1382605SAndriy Gapon msr |= (uint64_t)1 << 15; 127f1382605SAndriy Gapon wrmsr(0xc0011020, msr); 128f1382605SAndriy Gapon } 129f1382605SAndriy Gapon } 130*cd155b56SDon Lewis 131*cd155b56SDon Lewis /* 132*cd155b56SDon Lewis * Work around a problem on Ryzen that is triggered by executing 133*cd155b56SDon Lewis * code near the top of user memory, in our case the signal 134*cd155b56SDon Lewis * trampoline code in the shared page on amd64. 135*cd155b56SDon Lewis * 136*cd155b56SDon Lewis * This function is executed once for the BSP before tunables take 137*cd155b56SDon Lewis * effect so the value determined here can be overridden by the 138*cd155b56SDon Lewis * tunable. This function is then executed again for each AP and 139*cd155b56SDon Lewis * also on resume. Set a flag the first time so that value set by 140*cd155b56SDon Lewis * the tunable is not overwritten. 141*cd155b56SDon Lewis * 142*cd155b56SDon Lewis * The stepping and/or microcode versions should be checked after 143*cd155b56SDon Lewis * this issue is fixed by AMD so that we don't use this mode if not 144*cd155b56SDon Lewis * needed. 145*cd155b56SDon Lewis */ 146*cd155b56SDon Lewis if (lower_sharedpage_init == 0) { 147*cd155b56SDon Lewis lower_sharedpage_init = 1; 148*cd155b56SDon Lewis if (CPUID_TO_FAMILY(cpu_id) == 0x17) { 149*cd155b56SDon Lewis hw_lower_amd64_sharedpage = 1; 150*cd155b56SDon Lewis } 151*cd155b56SDon Lewis } 1523ce5dbccSJung-uk Kim } 1533ce5dbccSJung-uk Kim 15492df0bdaSJung-uk Kim /* 155cd45fec0SJung-uk Kim * Initialize special VIA features 15692df0bdaSJung-uk Kim */ 15792df0bdaSJung-uk Kim static void 15892df0bdaSJung-uk Kim init_via(void) 15992df0bdaSJung-uk Kim { 16092df0bdaSJung-uk Kim u_int regs[4], val; 16192df0bdaSJung-uk Kim 162cd45fec0SJung-uk Kim /* 163cd45fec0SJung-uk Kim * Check extended CPUID for PadLock features. 164cd45fec0SJung-uk Kim * 165cd45fec0SJung-uk Kim * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/programming_guide.pdf 166cd45fec0SJung-uk Kim */ 16792df0bdaSJung-uk Kim do_cpuid(0xc0000000, regs); 168cd45fec0SJung-uk Kim if (regs[0] >= 0xc0000001) { 16992df0bdaSJung-uk Kim do_cpuid(0xc0000001, regs); 17092df0bdaSJung-uk Kim val = regs[3]; 17192df0bdaSJung-uk Kim } else 172cd45fec0SJung-uk Kim return; 17392df0bdaSJung-uk Kim 174cd45fec0SJung-uk Kim /* Enable RNG if present. */ 175cd45fec0SJung-uk Kim if ((val & VIA_CPUID_HAS_RNG) != 0) { 17692df0bdaSJung-uk Kim via_feature_rng = VIA_HAS_RNG; 177cd45fec0SJung-uk Kim wrmsr(0x110B, rdmsr(0x110B) | VIA_CPUID_DO_RNG); 17892df0bdaSJung-uk Kim } 179cd45fec0SJung-uk Kim 180cd45fec0SJung-uk Kim /* Enable PadLock if present. */ 181cd45fec0SJung-uk Kim if ((val & VIA_CPUID_HAS_ACE) != 0) 18292df0bdaSJung-uk Kim via_feature_xcrypt |= VIA_HAS_AES; 183cd45fec0SJung-uk Kim if ((val & VIA_CPUID_HAS_ACE2) != 0) 18492df0bdaSJung-uk Kim via_feature_xcrypt |= VIA_HAS_AESCTR; 185cd45fec0SJung-uk Kim if ((val & VIA_CPUID_HAS_PHE) != 0) 18692df0bdaSJung-uk Kim via_feature_xcrypt |= VIA_HAS_SHA; 187cd45fec0SJung-uk Kim if ((val & VIA_CPUID_HAS_PMM) != 0) 18892df0bdaSJung-uk Kim via_feature_xcrypt |= VIA_HAS_MM; 189cd45fec0SJung-uk Kim if (via_feature_xcrypt != 0) 190cd45fec0SJung-uk Kim wrmsr(0x1107, rdmsr(0x1107) | (1 << 28)); 19192df0bdaSJung-uk Kim } 19292df0bdaSJung-uk Kim 1939d146ac5SPeter Wemm /* 194430e272cSPeter Wemm * Initialize CPU control registers 1959d146ac5SPeter Wemm */ 1969d146ac5SPeter Wemm void 197430e272cSPeter Wemm initializecpu(void) 1989d146ac5SPeter Wemm { 199430e272cSPeter Wemm uint64_t msr; 200cd9e9d1bSKonstantin Belousov uint32_t cr4; 201430e272cSPeter Wemm 202cd9e9d1bSKonstantin Belousov cr4 = rcr4(); 2039d146ac5SPeter Wemm if ((cpu_feature & CPUID_XMM) && (cpu_feature & CPUID_FXSR)) { 204cd9e9d1bSKonstantin Belousov cr4 |= CR4_FXSR | CR4_XMM; 2059d146ac5SPeter Wemm cpu_fxsr = hw_instruction_sse = 1; 2069d146ac5SPeter Wemm } 207cd9e9d1bSKonstantin Belousov if (cpu_stdext_feature & CPUID_STDEXT_FSGSBASE) 208cd9e9d1bSKonstantin Belousov cr4 |= CR4_FSGSBASE; 209cd9e9d1bSKonstantin Belousov 210cd9e9d1bSKonstantin Belousov /* 211cd9e9d1bSKonstantin Belousov * Postpone enabling the SMEP on the boot CPU until the page 212cd9e9d1bSKonstantin Belousov * tables are switched from the boot loader identity mapping 213cd9e9d1bSKonstantin Belousov * to the kernel tables. The boot loader enables the U bit in 214cd9e9d1bSKonstantin Belousov * its tables. 215cd9e9d1bSKonstantin Belousov */ 216cd9e9d1bSKonstantin Belousov if (!IS_BSP() && (cpu_stdext_feature & CPUID_STDEXT_SMEP)) 217cd9e9d1bSKonstantin Belousov cr4 |= CR4_SMEP; 218cd9e9d1bSKonstantin Belousov load_cr4(cr4); 219430e272cSPeter Wemm if ((amd_feature & AMDID_NX) != 0) { 220430e272cSPeter Wemm msr = rdmsr(MSR_EFER) | EFER_NXE; 221430e272cSPeter Wemm wrmsr(MSR_EFER, msr); 222430e272cSPeter Wemm pg_nx = PG_NX; 2239d146ac5SPeter Wemm } 2243ce5dbccSJung-uk Kim switch (cpu_vendor_id) { 2253ce5dbccSJung-uk Kim case CPU_VENDOR_AMD: 2263ce5dbccSJung-uk Kim init_amd(); 2273ce5dbccSJung-uk Kim break; 2283ce5dbccSJung-uk Kim case CPU_VENDOR_CENTAUR: 22992df0bdaSJung-uk Kim init_via(); 2303ce5dbccSJung-uk Kim break; 2313ce5dbccSJung-uk Kim } 232ec24e8d4SKonstantin Belousov } 233ec24e8d4SKonstantin Belousov 234ec24e8d4SKonstantin Belousov void 235cd234300SBrooks Davis initializecpucache(void) 236ec24e8d4SKonstantin Belousov { 237206a3368SKonstantin Belousov 238206a3368SKonstantin Belousov /* 239206a3368SKonstantin Belousov * CPUID with %eax = 1, %ebx returns 240206a3368SKonstantin Belousov * Bits 15-8: CLFLUSH line size 241206a3368SKonstantin Belousov * (Value * 8 = cache line size in bytes) 242206a3368SKonstantin Belousov */ 243206a3368SKonstantin Belousov if ((cpu_feature & CPUID_CLFSH) != 0) 244206a3368SKonstantin Belousov cpu_clflush_line_size = ((cpu_procinfo >> 8) & 0xff) * 8; 245b02395c6SKonstantin Belousov /* 2467134e390SJohn Baldwin * XXXKIB: (temporary) hack to work around traps generated 2477134e390SJohn Baldwin * when CLFLUSHing APIC register window under virtualization 2487134e390SJohn Baldwin * environments. These environments tend to disable the 2497134e390SJohn Baldwin * CPUID_SS feature even though the native CPU supports it. 250b02395c6SKonstantin Belousov */ 2516f5c96c4SJun Kuriyama TUNABLE_INT_FETCH("hw.clflush_disable", &hw_clflush_disable); 252af95bbf5SKonstantin Belousov if (vm_guest != VM_GUEST_NO && hw_clflush_disable == -1) { 253b02395c6SKonstantin Belousov cpu_feature &= ~CPUID_CLFSH; 254af95bbf5SKonstantin Belousov cpu_stdext_feature &= ~CPUID_STDEXT_CLFLUSHOPT; 255af95bbf5SKonstantin Belousov } 256af95bbf5SKonstantin Belousov 2576f5c96c4SJun Kuriyama /* 258af95bbf5SKonstantin Belousov * The kernel's use of CLFLUSH{,OPT} can be disabled manually 259af95bbf5SKonstantin Belousov * by setting the hw.clflush_disable tunable. 2606f5c96c4SJun Kuriyama */ 261af95bbf5SKonstantin Belousov if (hw_clflush_disable == 1) { 2626f5c96c4SJun Kuriyama cpu_feature &= ~CPUID_CLFSH; 263af95bbf5SKonstantin Belousov cpu_stdext_feature &= ~CPUID_STDEXT_CLFLUSHOPT; 264af95bbf5SKonstantin Belousov } 2656f5c96c4SJun Kuriyama } 266