1cda07865SPeter Wemm /*- 2c49761ddSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3c49761ddSPedro F. Giffuni * 44536af6aSKATO Takenori * Copyright (c) KATO Takenori, 1997, 1998. 5a8e282d6SKATO Takenori * 6a8e282d6SKATO Takenori * All rights reserved. Unpublished rights reserved under the copyright 7a8e282d6SKATO Takenori * laws of Japan. 8a8e282d6SKATO Takenori * 9a8e282d6SKATO Takenori * Redistribution and use in source and binary forms, with or without 10a8e282d6SKATO Takenori * modification, are permitted provided that the following conditions 11a8e282d6SKATO Takenori * are met: 12a8e282d6SKATO Takenori * 13a8e282d6SKATO Takenori * 1. Redistributions of source code must retain the above copyright 14a8e282d6SKATO Takenori * notice, this list of conditions and the following disclaimer as 15a8e282d6SKATO Takenori * the first lines of this file unmodified. 16a8e282d6SKATO Takenori * 2. Redistributions in binary form must reproduce the above copyright 17a8e282d6SKATO Takenori * notice, this list of conditions and the following disclaimer in the 18a8e282d6SKATO Takenori * documentation and/or other materials provided with the distribution. 19a8e282d6SKATO Takenori * 20a8e282d6SKATO Takenori * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21a8e282d6SKATO Takenori * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22a8e282d6SKATO Takenori * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23a8e282d6SKATO Takenori * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24a8e282d6SKATO Takenori * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25a8e282d6SKATO Takenori * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26a8e282d6SKATO Takenori * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27a8e282d6SKATO Takenori * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28a8e282d6SKATO Takenori * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29a8e282d6SKATO Takenori * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30a8e282d6SKATO Takenori */ 31a8e282d6SKATO Takenori 3256ae44c5SDavid E. O'Brien #include <sys/cdefs.h> 3356ae44c5SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3456ae44c5SDavid E. O'Brien 35a8e282d6SKATO Takenori #include "opt_cpu.h" 36a8e282d6SKATO Takenori 37a8e282d6SKATO Takenori #include <sys/param.h> 38a8e282d6SKATO Takenori #include <sys/kernel.h> 39cd9e9d1bSKonstantin Belousov #include <sys/pcpu.h> 40a8e282d6SKATO Takenori #include <sys/systm.h> 419d146ac5SPeter Wemm #include <sys/sysctl.h> 42a8e282d6SKATO Takenori 43a8e282d6SKATO Takenori #include <machine/cputypes.h> 44a8e282d6SKATO Takenori #include <machine/md_var.h> 45a333a508SPeter Grehan #include <machine/psl.h> 46a8e282d6SKATO Takenori #include <machine/specialreg.h> 47a8e282d6SKATO Takenori 48430e272cSPeter Wemm #include <vm/vm.h> 49430e272cSPeter Wemm #include <vm/pmap.h> 5020916c1fSKATO Takenori 5110deca7eSJohn Baldwin static int hw_instruction_sse; 529d146ac5SPeter Wemm SYSCTL_INT(_hw, OID_AUTO, instruction_sse, CTLFLAG_RD, 5310deca7eSJohn Baldwin &hw_instruction_sse, 0, "SIMD/MMX2 instructions available in CPU"); 54cd155b56SDon Lewis static int lower_sharedpage_init; 55cd155b56SDon Lewis int hw_lower_amd64_sharedpage; 56cd155b56SDon Lewis SYSCTL_INT(_hw, OID_AUTO, lower_amd64_sharedpage, CTLFLAG_RDTUN, 57cd155b56SDon Lewis &hw_lower_amd64_sharedpage, 0, 58cd155b56SDon Lewis "Lower sharedpage to work around Ryzen issue with executing code near the top of user memory"); 596f5c96c4SJun Kuriyama /* 606f5c96c4SJun Kuriyama * -1: automatic (default) 616f5c96c4SJun Kuriyama * 0: keep enable CLFLUSH 626f5c96c4SJun Kuriyama * 1: force disable CLFLUSH 636f5c96c4SJun Kuriyama */ 646f5c96c4SJun Kuriyama static int hw_clflush_disable = -1; 659d146ac5SPeter Wemm 663ce5dbccSJung-uk Kim static void 673ce5dbccSJung-uk Kim init_amd(void) 683ce5dbccSJung-uk Kim { 69f9ac50acSAndriy Gapon uint64_t msr; 703ce5dbccSJung-uk Kim 713ce5dbccSJung-uk Kim /* 72d3ba71b2SKonstantin Belousov * C1E renders the local APIC timer dead, so we disable it by 73d3ba71b2SKonstantin Belousov * reading the Interrupt Pending Message register and clearing 74d3ba71b2SKonstantin Belousov * both C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27). 75d3ba71b2SKonstantin Belousov * 76d3ba71b2SKonstantin Belousov * Reference: 77d3ba71b2SKonstantin Belousov * "BIOS and Kernel Developer's Guide for AMD NPT Family 0Fh Processors" 78d3ba71b2SKonstantin Belousov * #32559 revision 3.00+ 79d3ba71b2SKonstantin Belousov * 80d3ba71b2SKonstantin Belousov * Detect the presence of C1E capability mostly on latest 81d3ba71b2SKonstantin Belousov * dual-cores (or future) k8 family. Affected models range is 82d3ba71b2SKonstantin Belousov * taken from Linux sources. 83d3ba71b2SKonstantin Belousov */ 84d3ba71b2SKonstantin Belousov if ((CPUID_TO_FAMILY(cpu_id) == 0xf || 85d3ba71b2SKonstantin Belousov CPUID_TO_FAMILY(cpu_id) == 0x10) && (cpu_feature2 & CPUID2_HV) == 0) 86d3ba71b2SKonstantin Belousov cpu_amdc1e_bug = 1; 87d3ba71b2SKonstantin Belousov 88d3ba71b2SKonstantin Belousov /* 893ce5dbccSJung-uk Kim * Work around Erratum 721 for Family 10h and 12h processors. 903ce5dbccSJung-uk Kim * These processors may incorrectly update the stack pointer 913ce5dbccSJung-uk Kim * after a long series of push and/or near-call instructions, 923ce5dbccSJung-uk Kim * or a long series of pop and/or near-return instructions. 933ce5dbccSJung-uk Kim * 943ce5dbccSJung-uk Kim * http://support.amd.com/us/Processor_TechDocs/41322_10h_Rev_Gd.pdf 953ce5dbccSJung-uk Kim * http://support.amd.com/us/Processor_TechDocs/44739_12h_Rev_Gd.pdf 9665211d02SKonstantin Belousov * 9765211d02SKonstantin Belousov * Hypervisors do not provide access to the errata MSR, 9865211d02SKonstantin Belousov * causing #GP exception on attempt to apply the errata. The 9965211d02SKonstantin Belousov * MSR write shall be done on host and persist globally 10065211d02SKonstantin Belousov * anyway, so do not try to do it when under virtualization. 1013ce5dbccSJung-uk Kim */ 1023ce5dbccSJung-uk Kim switch (CPUID_TO_FAMILY(cpu_id)) { 1033ce5dbccSJung-uk Kim case 0x10: 1043ce5dbccSJung-uk Kim case 0x12: 10565211d02SKonstantin Belousov if ((cpu_feature2 & CPUID2_HV) == 0) 106d8dc46f6SJohn Baldwin wrmsr(MSR_DE_CFG, rdmsr(MSR_DE_CFG) | 1); 1073ce5dbccSJung-uk Kim break; 1083ce5dbccSJung-uk Kim } 109e5e44520SAndriy Gapon 110e5e44520SAndriy Gapon /* 111e5e44520SAndriy Gapon * BIOS may fail to set InitApicIdCpuIdLo to 1 as it should per BKDG. 112e5e44520SAndriy Gapon * So, do it here or otherwise some tools could be confused by 113e5e44520SAndriy Gapon * Initial Local APIC ID reported with CPUID Function 1 in EBX. 114e5e44520SAndriy Gapon */ 115e5e44520SAndriy Gapon if (CPUID_TO_FAMILY(cpu_id) == 0x10) { 116e5e44520SAndriy Gapon if ((cpu_feature2 & CPUID2_HV) == 0) { 117e5e44520SAndriy Gapon msr = rdmsr(MSR_NB_CFG1); 118e5e44520SAndriy Gapon msr |= (uint64_t)1 << 54; 119e5e44520SAndriy Gapon wrmsr(MSR_NB_CFG1, msr); 120e5e44520SAndriy Gapon } 121e5e44520SAndriy Gapon } 122a2d87b79SAndriy Gapon 123a2d87b79SAndriy Gapon /* 124a2d87b79SAndriy Gapon * BIOS may configure Family 10h processors to convert WC+ cache type 125a2d87b79SAndriy Gapon * to CD. That can hurt performance of guest VMs using nested paging. 126a2d87b79SAndriy Gapon * The relevant MSR bit is not documented in the BKDG, 127a2d87b79SAndriy Gapon * the fix is borrowed from Linux. 128a2d87b79SAndriy Gapon */ 129a2d87b79SAndriy Gapon if (CPUID_TO_FAMILY(cpu_id) == 0x10) { 130a2d87b79SAndriy Gapon if ((cpu_feature2 & CPUID2_HV) == 0) { 131a2d87b79SAndriy Gapon msr = rdmsr(0xc001102a); 132a2d87b79SAndriy Gapon msr &= ~((uint64_t)1 << 24); 133a2d87b79SAndriy Gapon wrmsr(0xc001102a, msr); 134a2d87b79SAndriy Gapon } 135a2d87b79SAndriy Gapon } 136f1382605SAndriy Gapon 137f1382605SAndriy Gapon /* 138f1382605SAndriy Gapon * Work around Erratum 793: Specific Combination of Writes to Write 139f1382605SAndriy Gapon * Combined Memory Types and Locked Instructions May Cause Core Hang. 140f1382605SAndriy Gapon * See Revision Guide for AMD Family 16h Models 00h-0Fh Processors, 141f1382605SAndriy Gapon * revision 3.04 or later, publication 51810. 142f1382605SAndriy Gapon */ 143f1382605SAndriy Gapon if (CPUID_TO_FAMILY(cpu_id) == 0x16 && CPUID_TO_MODEL(cpu_id) <= 0xf) { 144f1382605SAndriy Gapon if ((cpu_feature2 & CPUID2_HV) == 0) { 145bebcdc00SJohn Baldwin msr = rdmsr(MSR_LS_CFG); 146f1382605SAndriy Gapon msr |= (uint64_t)1 << 15; 147bebcdc00SJohn Baldwin wrmsr(MSR_LS_CFG, msr); 148f1382605SAndriy Gapon } 149f1382605SAndriy Gapon } 150cd155b56SDon Lewis 15145ed991dSKonstantin Belousov /* Ryzen erratas. */ 15245ed991dSKonstantin Belousov if (CPUID_TO_FAMILY(cpu_id) == 0x17 && CPUID_TO_MODEL(cpu_id) == 0x1 && 15345ed991dSKonstantin Belousov (cpu_feature2 & CPUID2_HV) == 0) { 15445ed991dSKonstantin Belousov /* 1021 */ 155d8dc46f6SJohn Baldwin msr = rdmsr(MSR_DE_CFG); 15645ed991dSKonstantin Belousov msr |= 0x2000; 157d8dc46f6SJohn Baldwin wrmsr(MSR_DE_CFG, msr); 15845ed991dSKonstantin Belousov 15945ed991dSKonstantin Belousov /* 1033 */ 160bebcdc00SJohn Baldwin msr = rdmsr(MSR_LS_CFG); 16145ed991dSKonstantin Belousov msr |= 0x10; 162bebcdc00SJohn Baldwin wrmsr(MSR_LS_CFG, msr); 16345ed991dSKonstantin Belousov 16445ed991dSKonstantin Belousov /* 1049 */ 16545ed991dSKonstantin Belousov msr = rdmsr(0xc0011028); 16645ed991dSKonstantin Belousov msr |= 0x10; 16745ed991dSKonstantin Belousov wrmsr(0xc0011028, msr); 16845ed991dSKonstantin Belousov 16945ed991dSKonstantin Belousov /* 1095 */ 170bebcdc00SJohn Baldwin msr = rdmsr(MSR_LS_CFG); 17145ed991dSKonstantin Belousov msr |= 0x200000000000000; 172bebcdc00SJohn Baldwin wrmsr(MSR_LS_CFG, msr); 17345ed991dSKonstantin Belousov } 17445ed991dSKonstantin Belousov 175cd155b56SDon Lewis /* 176cd155b56SDon Lewis * Work around a problem on Ryzen that is triggered by executing 177cd155b56SDon Lewis * code near the top of user memory, in our case the signal 178cd155b56SDon Lewis * trampoline code in the shared page on amd64. 179cd155b56SDon Lewis * 180cd155b56SDon Lewis * This function is executed once for the BSP before tunables take 181cd155b56SDon Lewis * effect so the value determined here can be overridden by the 182cd155b56SDon Lewis * tunable. This function is then executed again for each AP and 183cd155b56SDon Lewis * also on resume. Set a flag the first time so that value set by 184cd155b56SDon Lewis * the tunable is not overwritten. 185cd155b56SDon Lewis * 186cd155b56SDon Lewis * The stepping and/or microcode versions should be checked after 187cd155b56SDon Lewis * this issue is fixed by AMD so that we don't use this mode if not 188cd155b56SDon Lewis * needed. 189cd155b56SDon Lewis */ 190cd155b56SDon Lewis if (lower_sharedpage_init == 0) { 191cd155b56SDon Lewis lower_sharedpage_init = 1; 1922ee49facSKonstantin Belousov if (CPUID_TO_FAMILY(cpu_id) == 0x17 || 1932ee49facSKonstantin Belousov CPUID_TO_FAMILY(cpu_id) == 0x18) { 194cd155b56SDon Lewis hw_lower_amd64_sharedpage = 1; 195cd155b56SDon Lewis } 196cd155b56SDon Lewis } 1973ce5dbccSJung-uk Kim } 1983ce5dbccSJung-uk Kim 19992df0bdaSJung-uk Kim /* 200cd45fec0SJung-uk Kim * Initialize special VIA features 20192df0bdaSJung-uk Kim */ 20292df0bdaSJung-uk Kim static void 20392df0bdaSJung-uk Kim init_via(void) 20492df0bdaSJung-uk Kim { 20592df0bdaSJung-uk Kim u_int regs[4], val; 20692df0bdaSJung-uk Kim 207cd45fec0SJung-uk Kim /* 208cd45fec0SJung-uk Kim * Check extended CPUID for PadLock features. 209cd45fec0SJung-uk Kim * 210cd45fec0SJung-uk Kim * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/programming_guide.pdf 211cd45fec0SJung-uk Kim */ 21292df0bdaSJung-uk Kim do_cpuid(0xc0000000, regs); 213cd45fec0SJung-uk Kim if (regs[0] >= 0xc0000001) { 21492df0bdaSJung-uk Kim do_cpuid(0xc0000001, regs); 21592df0bdaSJung-uk Kim val = regs[3]; 21692df0bdaSJung-uk Kim } else 217cd45fec0SJung-uk Kim return; 21892df0bdaSJung-uk Kim 219cd45fec0SJung-uk Kim /* Enable RNG if present. */ 220cd45fec0SJung-uk Kim if ((val & VIA_CPUID_HAS_RNG) != 0) { 22192df0bdaSJung-uk Kim via_feature_rng = VIA_HAS_RNG; 222cd45fec0SJung-uk Kim wrmsr(0x110B, rdmsr(0x110B) | VIA_CPUID_DO_RNG); 22392df0bdaSJung-uk Kim } 224cd45fec0SJung-uk Kim 225cd45fec0SJung-uk Kim /* Enable PadLock if present. */ 226cd45fec0SJung-uk Kim if ((val & VIA_CPUID_HAS_ACE) != 0) 22792df0bdaSJung-uk Kim via_feature_xcrypt |= VIA_HAS_AES; 228cd45fec0SJung-uk Kim if ((val & VIA_CPUID_HAS_ACE2) != 0) 22992df0bdaSJung-uk Kim via_feature_xcrypt |= VIA_HAS_AESCTR; 230cd45fec0SJung-uk Kim if ((val & VIA_CPUID_HAS_PHE) != 0) 23192df0bdaSJung-uk Kim via_feature_xcrypt |= VIA_HAS_SHA; 232cd45fec0SJung-uk Kim if ((val & VIA_CPUID_HAS_PMM) != 0) 23392df0bdaSJung-uk Kim via_feature_xcrypt |= VIA_HAS_MM; 234cd45fec0SJung-uk Kim if (via_feature_xcrypt != 0) 235cd45fec0SJung-uk Kim wrmsr(0x1107, rdmsr(0x1107) | (1 << 28)); 23692df0bdaSJung-uk Kim } 23792df0bdaSJung-uk Kim 2389d146ac5SPeter Wemm /* 239a333a508SPeter Grehan * The value for the TSC_AUX MSR and rdtscp/rdpid on the invoking CPU. 240a333a508SPeter Grehan * 241a333a508SPeter Grehan * Caller should prevent CPU migration. 2423a3f1e9dSPeter Grehan */ 2433a3f1e9dSPeter Grehan u_int 2443a3f1e9dSPeter Grehan cpu_auxmsr(void) 2453a3f1e9dSPeter Grehan { 246a333a508SPeter Grehan KASSERT((read_rflags() & PSL_I) == 0, ("context switch possible")); 2473a3f1e9dSPeter Grehan return (PCPU_GET(cpuid)); 2483a3f1e9dSPeter Grehan } 2493a3f1e9dSPeter Grehan 2503a3f1e9dSPeter Grehan /* 251430e272cSPeter Wemm * Initialize CPU control registers 2529d146ac5SPeter Wemm */ 2539d146ac5SPeter Wemm void 254430e272cSPeter Wemm initializecpu(void) 2559d146ac5SPeter Wemm { 256430e272cSPeter Wemm uint64_t msr; 257cd9e9d1bSKonstantin Belousov uint32_t cr4; 258430e272cSPeter Wemm 259cd9e9d1bSKonstantin Belousov cr4 = rcr4(); 2609d146ac5SPeter Wemm if ((cpu_feature & CPUID_XMM) && (cpu_feature & CPUID_FXSR)) { 261cd9e9d1bSKonstantin Belousov cr4 |= CR4_FXSR | CR4_XMM; 2629d146ac5SPeter Wemm cpu_fxsr = hw_instruction_sse = 1; 2639d146ac5SPeter Wemm } 264cd9e9d1bSKonstantin Belousov if (cpu_stdext_feature & CPUID_STDEXT_FSGSBASE) 265cd9e9d1bSKonstantin Belousov cr4 |= CR4_FSGSBASE; 266cd9e9d1bSKonstantin Belousov 267e7a9df16SKonstantin Belousov if (cpu_stdext_feature2 & CPUID_STDEXT2_PKU) 268e7a9df16SKonstantin Belousov cr4 |= CR4_PKE; 269e7a9df16SKonstantin Belousov 270cd9e9d1bSKonstantin Belousov /* 271ea602083SKonstantin Belousov * If SMEP is present, we only need to flush RSB (by default) 272ea602083SKonstantin Belousov * on context switches, to prevent cross-process ret2spec 273ea602083SKonstantin Belousov * attacks. Do it automatically if ibrs_disable is set, to 274ea602083SKonstantin Belousov * complete the mitigation. 275ea602083SKonstantin Belousov * 276cd9e9d1bSKonstantin Belousov * Postpone enabling the SMEP on the boot CPU until the page 277cd9e9d1bSKonstantin Belousov * tables are switched from the boot loader identity mapping 278cd9e9d1bSKonstantin Belousov * to the kernel tables. The boot loader enables the U bit in 279cd9e9d1bSKonstantin Belousov * its tables. 280cd9e9d1bSKonstantin Belousov */ 281ea602083SKonstantin Belousov if (IS_BSP()) { 282ea602083SKonstantin Belousov if (cpu_stdext_feature & CPUID_STDEXT_SMEP && 283ea602083SKonstantin Belousov !TUNABLE_INT_FETCH( 284ea602083SKonstantin Belousov "machdep.mitigations.cpu_flush_rsb_ctxsw", 285ea602083SKonstantin Belousov &cpu_flush_rsb_ctxsw) && 286ea602083SKonstantin Belousov hw_ibrs_disable) 287ea602083SKonstantin Belousov cpu_flush_rsb_ctxsw = 1; 288ea602083SKonstantin Belousov } else { 289b3a7db3bSKonstantin Belousov if (cpu_stdext_feature & CPUID_STDEXT_SMEP) 290cd9e9d1bSKonstantin Belousov cr4 |= CR4_SMEP; 291b3a7db3bSKonstantin Belousov if (cpu_stdext_feature & CPUID_STDEXT_SMAP) 292b3a7db3bSKonstantin Belousov cr4 |= CR4_SMAP; 293b3a7db3bSKonstantin Belousov } 294cd9e9d1bSKonstantin Belousov load_cr4(cr4); 295*050f5a84SDmitry Chagin /* Reload cpu ext features to reflect cr4 changes */ 296*050f5a84SDmitry Chagin if (IS_BSP()) 297*050f5a84SDmitry Chagin identify_cpu_ext_features(); 298beb24065SJonathan T. Looney if (IS_BSP() && (amd_feature & AMDID_NX) != 0) { 299430e272cSPeter Wemm msr = rdmsr(MSR_EFER) | EFER_NXE; 300430e272cSPeter Wemm wrmsr(MSR_EFER, msr); 301430e272cSPeter Wemm pg_nx = PG_NX; 3029d146ac5SPeter Wemm } 303a324b7f7SKonstantin Belousov hw_ibrs_recalculate(false); 3043621ba1eSKonstantin Belousov hw_ssb_recalculate(false); 3052dec2b4aSKonstantin Belousov amd64_syscall_ret_flush_l1d_recalc(); 30617edf152SKonstantin Belousov x86_rngds_mitg_recalculate(false); 3073ce5dbccSJung-uk Kim switch (cpu_vendor_id) { 3083ce5dbccSJung-uk Kim case CPU_VENDOR_AMD: 3092ee49facSKonstantin Belousov case CPU_VENDOR_HYGON: 3103ce5dbccSJung-uk Kim init_amd(); 3113ce5dbccSJung-uk Kim break; 3123ce5dbccSJung-uk Kim case CPU_VENDOR_CENTAUR: 31392df0bdaSJung-uk Kim init_via(); 3143ce5dbccSJung-uk Kim break; 3153ce5dbccSJung-uk Kim } 31639d70f6bSKonstantin Belousov 31739d70f6bSKonstantin Belousov if ((amd_feature & AMDID_RDTSCP) != 0 || 31839d70f6bSKonstantin Belousov (cpu_stdext_feature2 & CPUID_STDEXT2_RDPID) != 0) 3193a3f1e9dSPeter Grehan wrmsr(MSR_TSC_AUX, cpu_auxmsr()); 320ec24e8d4SKonstantin Belousov } 321ec24e8d4SKonstantin Belousov 322ec24e8d4SKonstantin Belousov void 323cd234300SBrooks Davis initializecpucache(void) 324ec24e8d4SKonstantin Belousov { 325206a3368SKonstantin Belousov 326206a3368SKonstantin Belousov /* 327206a3368SKonstantin Belousov * CPUID with %eax = 1, %ebx returns 328206a3368SKonstantin Belousov * Bits 15-8: CLFLUSH line size 329206a3368SKonstantin Belousov * (Value * 8 = cache line size in bytes) 330206a3368SKonstantin Belousov */ 331206a3368SKonstantin Belousov if ((cpu_feature & CPUID_CLFSH) != 0) 332206a3368SKonstantin Belousov cpu_clflush_line_size = ((cpu_procinfo >> 8) & 0xff) * 8; 333b02395c6SKonstantin Belousov /* 3347134e390SJohn Baldwin * XXXKIB: (temporary) hack to work around traps generated 3357134e390SJohn Baldwin * when CLFLUSHing APIC register window under virtualization 3367134e390SJohn Baldwin * environments. These environments tend to disable the 3377134e390SJohn Baldwin * CPUID_SS feature even though the native CPU supports it. 338b02395c6SKonstantin Belousov */ 3396f5c96c4SJun Kuriyama TUNABLE_INT_FETCH("hw.clflush_disable", &hw_clflush_disable); 340af95bbf5SKonstantin Belousov if (vm_guest != VM_GUEST_NO && hw_clflush_disable == -1) { 341b02395c6SKonstantin Belousov cpu_feature &= ~CPUID_CLFSH; 342af95bbf5SKonstantin Belousov cpu_stdext_feature &= ~CPUID_STDEXT_CLFLUSHOPT; 343af95bbf5SKonstantin Belousov } 344af95bbf5SKonstantin Belousov 3456f5c96c4SJun Kuriyama /* 346af95bbf5SKonstantin Belousov * The kernel's use of CLFLUSH{,OPT} can be disabled manually 347af95bbf5SKonstantin Belousov * by setting the hw.clflush_disable tunable. 3486f5c96c4SJun Kuriyama */ 349af95bbf5SKonstantin Belousov if (hw_clflush_disable == 1) { 3506f5c96c4SJun Kuriyama cpu_feature &= ~CPUID_CLFSH; 351af95bbf5SKonstantin Belousov cpu_stdext_feature &= ~CPUID_STDEXT_CLFLUSHOPT; 352af95bbf5SKonstantin Belousov } 3536f5c96c4SJun Kuriyama } 354