1dfe7b3bfSKonstantin Belousov /*- 2dfe7b3bfSKonstantin Belousov * Copyright (c) 2003 Peter Wemm. 3dfe7b3bfSKonstantin Belousov * Copyright (c) 1992 Terrence R. Lambert. 4dfe7b3bfSKonstantin Belousov * Copyright (c) 1982, 1987, 1990 The Regents of the University of California. 5dfe7b3bfSKonstantin Belousov * All rights reserved. 6dfe7b3bfSKonstantin Belousov * 7dfe7b3bfSKonstantin Belousov * This code is derived from software contributed to Berkeley by 8dfe7b3bfSKonstantin Belousov * William Jolitz. 9dfe7b3bfSKonstantin Belousov * 10dfe7b3bfSKonstantin Belousov * Redistribution and use in source and binary forms, with or without 11dfe7b3bfSKonstantin Belousov * modification, are permitted provided that the following conditions 12dfe7b3bfSKonstantin Belousov * are met: 13dfe7b3bfSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright 14dfe7b3bfSKonstantin Belousov * notice, this list of conditions and the following disclaimer. 15dfe7b3bfSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright 16dfe7b3bfSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the 17dfe7b3bfSKonstantin Belousov * documentation and/or other materials provided with the distribution. 18dfe7b3bfSKonstantin Belousov * 3. All advertising materials mentioning features or use of this software 19dfe7b3bfSKonstantin Belousov * must display the following acknowledgement: 20dfe7b3bfSKonstantin Belousov * This product includes software developed by the University of 21dfe7b3bfSKonstantin Belousov * California, Berkeley and its contributors. 22dfe7b3bfSKonstantin Belousov * 4. Neither the name of the University nor the names of its contributors 23dfe7b3bfSKonstantin Belousov * may be used to endorse or promote products derived from this software 24dfe7b3bfSKonstantin Belousov * without specific prior written permission. 25dfe7b3bfSKonstantin Belousov * 26dfe7b3bfSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27dfe7b3bfSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28dfe7b3bfSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29dfe7b3bfSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30dfe7b3bfSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31dfe7b3bfSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32dfe7b3bfSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33dfe7b3bfSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34dfe7b3bfSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35dfe7b3bfSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36dfe7b3bfSKonstantin Belousov * SUCH DAMAGE. 37dfe7b3bfSKonstantin Belousov * 38dfe7b3bfSKonstantin Belousov * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91 39dfe7b3bfSKonstantin Belousov */ 40dfe7b3bfSKonstantin Belousov 41dfe7b3bfSKonstantin Belousov #include <sys/cdefs.h> 42dfe7b3bfSKonstantin Belousov __FBSDID("$FreeBSD$"); 43dfe7b3bfSKonstantin Belousov 447705dd4dSKonstantin Belousov #include "opt_acpi.h" 45dfe7b3bfSKonstantin Belousov #include "opt_atpic.h" 46dfe7b3bfSKonstantin Belousov #include "opt_cpu.h" 47dfe7b3bfSKonstantin Belousov #include "opt_ddb.h" 48dfe7b3bfSKonstantin Belousov #include "opt_inet.h" 49dfe7b3bfSKonstantin Belousov #include "opt_isa.h" 50835c2787SKonstantin Belousov #include "opt_kdb.h" 51dfe7b3bfSKonstantin Belousov #include "opt_kstack_pages.h" 52dfe7b3bfSKonstantin Belousov #include "opt_maxmem.h" 53dfe7b3bfSKonstantin Belousov #include "opt_mp_watchdog.h" 54dfe7b3bfSKonstantin Belousov #include "opt_platform.h" 55dfe7b3bfSKonstantin Belousov #ifdef __i386__ 56dfe7b3bfSKonstantin Belousov #include "opt_apic.h" 57dfe7b3bfSKonstantin Belousov #endif 58dfe7b3bfSKonstantin Belousov 59dfe7b3bfSKonstantin Belousov #include <sys/param.h> 60dfe7b3bfSKonstantin Belousov #include <sys/proc.h> 61dfe7b3bfSKonstantin Belousov #include <sys/systm.h> 62dfe7b3bfSKonstantin Belousov #include <sys/bus.h> 63dfe7b3bfSKonstantin Belousov #include <sys/cpu.h> 647355a02bSKonstantin Belousov #include <sys/domainset.h> 65dfe7b3bfSKonstantin Belousov #include <sys/kdb.h> 66dfe7b3bfSKonstantin Belousov #include <sys/kernel.h> 67dfe7b3bfSKonstantin Belousov #include <sys/ktr.h> 68dfe7b3bfSKonstantin Belousov #include <sys/lock.h> 69dfe7b3bfSKonstantin Belousov #include <sys/malloc.h> 70dfe7b3bfSKonstantin Belousov #include <sys/mutex.h> 71dfe7b3bfSKonstantin Belousov #include <sys/pcpu.h> 72dfe7b3bfSKonstantin Belousov #include <sys/rwlock.h> 73dfe7b3bfSKonstantin Belousov #include <sys/sched.h> 74dfe7b3bfSKonstantin Belousov #include <sys/smp.h> 75dfe7b3bfSKonstantin Belousov #include <sys/sysctl.h> 76dfe7b3bfSKonstantin Belousov 77dfe7b3bfSKonstantin Belousov #include <machine/clock.h> 78dfe7b3bfSKonstantin Belousov #include <machine/cpu.h> 79dfe7b3bfSKonstantin Belousov #include <machine/cputypes.h> 80dfe7b3bfSKonstantin Belousov #include <machine/specialreg.h> 81dfe7b3bfSKonstantin Belousov #include <machine/md_var.h> 82dfe7b3bfSKonstantin Belousov #include <machine/mp_watchdog.h> 83dfe7b3bfSKonstantin Belousov #include <machine/tss.h> 84dfe7b3bfSKonstantin Belousov #ifdef SMP 85dfe7b3bfSKonstantin Belousov #include <machine/smp.h> 86dfe7b3bfSKonstantin Belousov #endif 873da25bdbSAndriy Gapon #ifdef CPU_ELAN 883da25bdbSAndriy Gapon #include <machine/elan_mmcr.h> 893da25bdbSAndriy Gapon #endif 90b57a73f8SKonstantin Belousov #include <x86/acpica_machdep.h> 91dfe7b3bfSKonstantin Belousov 92dfe7b3bfSKonstantin Belousov #include <vm/vm.h> 93dfe7b3bfSKonstantin Belousov #include <vm/vm_extern.h> 94dfe7b3bfSKonstantin Belousov #include <vm/vm_kern.h> 95dfe7b3bfSKonstantin Belousov #include <vm/vm_page.h> 96dfe7b3bfSKonstantin Belousov #include <vm/vm_map.h> 97dfe7b3bfSKonstantin Belousov #include <vm/vm_object.h> 98dfe7b3bfSKonstantin Belousov #include <vm/vm_pager.h> 99dfe7b3bfSKonstantin Belousov #include <vm/vm_param.h> 100dfe7b3bfSKonstantin Belousov 1018428d0f1SAndriy Gapon #include <isa/isareg.h> 1028428d0f1SAndriy Gapon 1037705dd4dSKonstantin Belousov #include <contrib/dev/acpica/include/acpi.h> 1047705dd4dSKonstantin Belousov 105d9e8bbb6SKonstantin Belousov #define STATE_RUNNING 0x0 106d9e8bbb6SKonstantin Belousov #define STATE_MWAIT 0x1 107d9e8bbb6SKonstantin Belousov #define STATE_SLEEPING 0x2 108d9e8bbb6SKonstantin Belousov 1098428d0f1SAndriy Gapon #ifdef SMP 1108428d0f1SAndriy Gapon static u_int cpu_reset_proxyid; 1118428d0f1SAndriy Gapon static volatile u_int cpu_reset_proxy_active; 1128428d0f1SAndriy Gapon #endif 1138428d0f1SAndriy Gapon 114*fa83f689SKonstantin Belousov struct msr_op_arg { 115*fa83f689SKonstantin Belousov u_int msr; 116*fa83f689SKonstantin Belousov int op; 117*fa83f689SKonstantin Belousov uint64_t arg1; 118*fa83f689SKonstantin Belousov }; 119*fa83f689SKonstantin Belousov 120*fa83f689SKonstantin Belousov static void 121*fa83f689SKonstantin Belousov x86_msr_op_one(void *argp) 122*fa83f689SKonstantin Belousov { 123*fa83f689SKonstantin Belousov struct msr_op_arg *a; 124*fa83f689SKonstantin Belousov uint64_t v; 125*fa83f689SKonstantin Belousov 126*fa83f689SKonstantin Belousov a = argp; 127*fa83f689SKonstantin Belousov switch (a->op) { 128*fa83f689SKonstantin Belousov case MSR_OP_ANDNOT: 129*fa83f689SKonstantin Belousov v = rdmsr(a->msr); 130*fa83f689SKonstantin Belousov v &= ~a->arg1; 131*fa83f689SKonstantin Belousov wrmsr(a->msr, v); 132*fa83f689SKonstantin Belousov break; 133*fa83f689SKonstantin Belousov case MSR_OP_OR: 134*fa83f689SKonstantin Belousov v = rdmsr(a->msr); 135*fa83f689SKonstantin Belousov v |= a->arg1; 136*fa83f689SKonstantin Belousov wrmsr(a->msr, v); 137*fa83f689SKonstantin Belousov break; 138*fa83f689SKonstantin Belousov case MSR_OP_WRITE: 139*fa83f689SKonstantin Belousov wrmsr(a->msr, a->arg1); 140*fa83f689SKonstantin Belousov break; 141*fa83f689SKonstantin Belousov } 142*fa83f689SKonstantin Belousov } 143*fa83f689SKonstantin Belousov 144*fa83f689SKonstantin Belousov #define MSR_OP_EXMODE_MASK 0xf0000000 145*fa83f689SKonstantin Belousov #define MSR_OP_OP_MASK 0x000000ff 146*fa83f689SKonstantin Belousov 147*fa83f689SKonstantin Belousov void 148*fa83f689SKonstantin Belousov x86_msr_op(u_int msr, u_int op, uint64_t arg1) 149*fa83f689SKonstantin Belousov { 150*fa83f689SKonstantin Belousov struct thread *td; 151*fa83f689SKonstantin Belousov struct msr_op_arg a; 152*fa83f689SKonstantin Belousov u_int exmode; 153*fa83f689SKonstantin Belousov int bound_cpu, i, is_bound; 154*fa83f689SKonstantin Belousov 155*fa83f689SKonstantin Belousov a.op = op & MSR_OP_OP_MASK; 156*fa83f689SKonstantin Belousov MPASS(a.op == MSR_OP_ANDNOT || a.op == MSR_OP_OR || 157*fa83f689SKonstantin Belousov a.op == MSR_OP_WRITE); 158*fa83f689SKonstantin Belousov exmode = op & MSR_OP_EXMODE_MASK; 159*fa83f689SKonstantin Belousov MPASS(exmode == MSR_OP_LOCAL || exmode == MSR_OP_SCHED || 160*fa83f689SKonstantin Belousov exmode == MSR_OP_RENDEZVOUS); 161*fa83f689SKonstantin Belousov a.msr = msr; 162*fa83f689SKonstantin Belousov a.arg1 = arg1; 163*fa83f689SKonstantin Belousov switch (exmode) { 164*fa83f689SKonstantin Belousov case MSR_OP_LOCAL: 165*fa83f689SKonstantin Belousov x86_msr_op_one(&a); 166*fa83f689SKonstantin Belousov break; 167*fa83f689SKonstantin Belousov case MSR_OP_SCHED: 168*fa83f689SKonstantin Belousov td = curthread; 169*fa83f689SKonstantin Belousov thread_lock(td); 170*fa83f689SKonstantin Belousov is_bound = sched_is_bound(td); 171*fa83f689SKonstantin Belousov bound_cpu = td->td_oncpu; 172*fa83f689SKonstantin Belousov CPU_FOREACH(i) { 173*fa83f689SKonstantin Belousov sched_bind(td, i); 174*fa83f689SKonstantin Belousov x86_msr_op_one(&a); 175*fa83f689SKonstantin Belousov } 176*fa83f689SKonstantin Belousov if (is_bound) 177*fa83f689SKonstantin Belousov sched_bind(td, bound_cpu); 178*fa83f689SKonstantin Belousov else 179*fa83f689SKonstantin Belousov sched_unbind(td); 180*fa83f689SKonstantin Belousov thread_unlock(td); 181*fa83f689SKonstantin Belousov break; 182*fa83f689SKonstantin Belousov case MSR_OP_RENDEZVOUS: 183*fa83f689SKonstantin Belousov smp_rendezvous(NULL, x86_msr_op_one, NULL, &a); 184*fa83f689SKonstantin Belousov break; 185*fa83f689SKonstantin Belousov } 186*fa83f689SKonstantin Belousov } 187*fa83f689SKonstantin Belousov 188665919aaSConrad Meyer /* 189665919aaSConrad Meyer * Automatically initialized per CPU errata in cpu_idle_tun below. 190665919aaSConrad Meyer */ 191665919aaSConrad Meyer bool mwait_cpustop_broken = false; 192665919aaSConrad Meyer SYSCTL_BOOL(_machdep, OID_AUTO, mwait_cpustop_broken, CTLFLAG_RDTUN, 193665919aaSConrad Meyer &mwait_cpustop_broken, 0, 194665919aaSConrad Meyer "Can not reliably wake MONITOR/MWAIT cpus without interrupts"); 1958428d0f1SAndriy Gapon 196dfe7b3bfSKonstantin Belousov /* 197dfe7b3bfSKonstantin Belousov * Machine dependent boot() routine 198dfe7b3bfSKonstantin Belousov * 199dfe7b3bfSKonstantin Belousov * I haven't seen anything to put here yet 200dfe7b3bfSKonstantin Belousov * Possibly some stuff might be grafted back here from boot() 201dfe7b3bfSKonstantin Belousov */ 202dfe7b3bfSKonstantin Belousov void 203dfe7b3bfSKonstantin Belousov cpu_boot(int howto) 204dfe7b3bfSKonstantin Belousov { 205dfe7b3bfSKonstantin Belousov } 206dfe7b3bfSKonstantin Belousov 207dfe7b3bfSKonstantin Belousov /* 208dfe7b3bfSKonstantin Belousov * Flush the D-cache for non-DMA I/O so that the I-cache can 209dfe7b3bfSKonstantin Belousov * be made coherent later. 210dfe7b3bfSKonstantin Belousov */ 211dfe7b3bfSKonstantin Belousov void 212dfe7b3bfSKonstantin Belousov cpu_flush_dcache(void *ptr, size_t len) 213dfe7b3bfSKonstantin Belousov { 214dfe7b3bfSKonstantin Belousov /* Not applicable */ 215dfe7b3bfSKonstantin Belousov } 216dfe7b3bfSKonstantin Belousov 217b57a73f8SKonstantin Belousov void 218b57a73f8SKonstantin Belousov acpi_cpu_c1(void) 219b57a73f8SKonstantin Belousov { 220b57a73f8SKonstantin Belousov 221b57a73f8SKonstantin Belousov __asm __volatile("sti; hlt"); 222b57a73f8SKonstantin Belousov } 223b57a73f8SKonstantin Belousov 22419d4720bSJonathan T. Looney /* 22519d4720bSJonathan T. Looney * Use mwait to pause execution while waiting for an interrupt or 22619d4720bSJonathan T. Looney * another thread to signal that there is more work. 22719d4720bSJonathan T. Looney * 22819d4720bSJonathan T. Looney * NOTE: Interrupts will cause a wakeup; however, this function does 22919d4720bSJonathan T. Looney * not enable interrupt handling. The caller is responsible to enable 23019d4720bSJonathan T. Looney * interrupts. 23119d4720bSJonathan T. Looney */ 232b57a73f8SKonstantin Belousov void 233b57a73f8SKonstantin Belousov acpi_cpu_idle_mwait(uint32_t mwait_hint) 234b57a73f8SKonstantin Belousov { 235b57a73f8SKonstantin Belousov int *state; 2363621ba1eSKonstantin Belousov uint64_t v; 237b57a73f8SKonstantin Belousov 238b57a73f8SKonstantin Belousov /* 239319117fdSKonstantin Belousov * A comment in Linux patch claims that 'CPUs run faster with 240319117fdSKonstantin Belousov * speculation protection disabled. All CPU threads in a core 241319117fdSKonstantin Belousov * must disable speculation protection for it to be 242319117fdSKonstantin Belousov * disabled. Disable it while we are idle so the other 243319117fdSKonstantin Belousov * hyperthread can run fast.' 244319117fdSKonstantin Belousov * 245b57a73f8SKonstantin Belousov * XXXKIB. Software coordination mode should be supported, 246b57a73f8SKonstantin Belousov * but all Intel CPUs provide hardware coordination. 247b57a73f8SKonstantin Belousov */ 248d9e8bbb6SKonstantin Belousov 24983dc49beSConrad Meyer state = &PCPU_PTR(monitorbuf)->idle_state; 250a5bd21d0SKonstantin Belousov KASSERT(atomic_load_int(state) == STATE_SLEEPING, 251d9e8bbb6SKonstantin Belousov ("cpu_mwait_cx: wrong monitorbuf state")); 252a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_MWAIT); 2533621ba1eSKonstantin Belousov if (PCPU_GET(ibpb_set) || hw_ssb_active) { 2543621ba1eSKonstantin Belousov v = rdmsr(MSR_IA32_SPEC_CTRL); 2553621ba1eSKonstantin Belousov wrmsr(MSR_IA32_SPEC_CTRL, v & ~(IA32_SPEC_CTRL_IBRS | 2563621ba1eSKonstantin Belousov IA32_SPEC_CTRL_STIBP | IA32_SPEC_CTRL_SSBD)); 2573621ba1eSKonstantin Belousov } else { 2583621ba1eSKonstantin Belousov v = 0; 2593621ba1eSKonstantin Belousov } 260b57a73f8SKonstantin Belousov cpu_monitor(state, 0, 0); 261a5bd21d0SKonstantin Belousov if (atomic_load_int(state) == STATE_MWAIT) 262b57a73f8SKonstantin Belousov cpu_mwait(MWAIT_INTRBREAK, mwait_hint); 2633621ba1eSKonstantin Belousov 2643621ba1eSKonstantin Belousov /* 2653621ba1eSKonstantin Belousov * SSB cannot be disabled while we sleep, or rather, if it was 2663621ba1eSKonstantin Belousov * disabled, the sysctl thread will bind to our cpu to tweak 2673621ba1eSKonstantin Belousov * MSR. 2683621ba1eSKonstantin Belousov */ 2693621ba1eSKonstantin Belousov if (v != 0) 2703621ba1eSKonstantin Belousov wrmsr(MSR_IA32_SPEC_CTRL, v); 271d9e8bbb6SKonstantin Belousov 272d9e8bbb6SKonstantin Belousov /* 273d9e8bbb6SKonstantin Belousov * We should exit on any event that interrupts mwait, because 274d9e8bbb6SKonstantin Belousov * that event might be a wanted interrupt. 275d9e8bbb6SKonstantin Belousov */ 276a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 277b57a73f8SKonstantin Belousov } 278b57a73f8SKonstantin Belousov 279dfe7b3bfSKonstantin Belousov /* Get current clock frequency for the given cpu id. */ 280dfe7b3bfSKonstantin Belousov int 281dfe7b3bfSKonstantin Belousov cpu_est_clockrate(int cpu_id, uint64_t *rate) 282dfe7b3bfSKonstantin Belousov { 283dfe7b3bfSKonstantin Belousov uint64_t tsc1, tsc2; 284dfe7b3bfSKonstantin Belousov uint64_t acnt, mcnt, perf; 285dfe7b3bfSKonstantin Belousov register_t reg; 286dfe7b3bfSKonstantin Belousov 287dfe7b3bfSKonstantin Belousov if (pcpu_find(cpu_id) == NULL || rate == NULL) 288dfe7b3bfSKonstantin Belousov return (EINVAL); 289dfe7b3bfSKonstantin Belousov #ifdef __i386__ 290dfe7b3bfSKonstantin Belousov if ((cpu_feature & CPUID_TSC) == 0) 291dfe7b3bfSKonstantin Belousov return (EOPNOTSUPP); 292dfe7b3bfSKonstantin Belousov #endif 293dfe7b3bfSKonstantin Belousov 294dfe7b3bfSKonstantin Belousov /* 295dfe7b3bfSKonstantin Belousov * If TSC is P-state invariant and APERF/MPERF MSRs do not exist, 296dfe7b3bfSKonstantin Belousov * DELAY(9) based logic fails. 297dfe7b3bfSKonstantin Belousov */ 298dfe7b3bfSKonstantin Belousov if (tsc_is_invariant && !tsc_perf_stat) 299dfe7b3bfSKonstantin Belousov return (EOPNOTSUPP); 300dfe7b3bfSKonstantin Belousov 301dfe7b3bfSKonstantin Belousov #ifdef SMP 302dfe7b3bfSKonstantin Belousov if (smp_cpus > 1) { 303dfe7b3bfSKonstantin Belousov /* Schedule ourselves on the indicated cpu. */ 304dfe7b3bfSKonstantin Belousov thread_lock(curthread); 305dfe7b3bfSKonstantin Belousov sched_bind(curthread, cpu_id); 306dfe7b3bfSKonstantin Belousov thread_unlock(curthread); 307dfe7b3bfSKonstantin Belousov } 308dfe7b3bfSKonstantin Belousov #endif 309dfe7b3bfSKonstantin Belousov 310dfe7b3bfSKonstantin Belousov /* Calibrate by measuring a short delay. */ 311dfe7b3bfSKonstantin Belousov reg = intr_disable(); 312dfe7b3bfSKonstantin Belousov if (tsc_is_invariant) { 313dfe7b3bfSKonstantin Belousov wrmsr(MSR_MPERF, 0); 314dfe7b3bfSKonstantin Belousov wrmsr(MSR_APERF, 0); 315dfe7b3bfSKonstantin Belousov tsc1 = rdtsc(); 316dfe7b3bfSKonstantin Belousov DELAY(1000); 317dfe7b3bfSKonstantin Belousov mcnt = rdmsr(MSR_MPERF); 318dfe7b3bfSKonstantin Belousov acnt = rdmsr(MSR_APERF); 319dfe7b3bfSKonstantin Belousov tsc2 = rdtsc(); 320dfe7b3bfSKonstantin Belousov intr_restore(reg); 321dfe7b3bfSKonstantin Belousov perf = 1000 * acnt / mcnt; 322dfe7b3bfSKonstantin Belousov *rate = (tsc2 - tsc1) * perf; 323dfe7b3bfSKonstantin Belousov } else { 324dfe7b3bfSKonstantin Belousov tsc1 = rdtsc(); 325dfe7b3bfSKonstantin Belousov DELAY(1000); 326dfe7b3bfSKonstantin Belousov tsc2 = rdtsc(); 327dfe7b3bfSKonstantin Belousov intr_restore(reg); 328dfe7b3bfSKonstantin Belousov *rate = (tsc2 - tsc1) * 1000; 329dfe7b3bfSKonstantin Belousov } 330dfe7b3bfSKonstantin Belousov 331dfe7b3bfSKonstantin Belousov #ifdef SMP 332dfe7b3bfSKonstantin Belousov if (smp_cpus > 1) { 333dfe7b3bfSKonstantin Belousov thread_lock(curthread); 334dfe7b3bfSKonstantin Belousov sched_unbind(curthread); 335dfe7b3bfSKonstantin Belousov thread_unlock(curthread); 336dfe7b3bfSKonstantin Belousov } 337dfe7b3bfSKonstantin Belousov #endif 338dfe7b3bfSKonstantin Belousov 339dfe7b3bfSKonstantin Belousov return (0); 340dfe7b3bfSKonstantin Belousov } 341dfe7b3bfSKonstantin Belousov 342dfe7b3bfSKonstantin Belousov /* 343dfe7b3bfSKonstantin Belousov * Shutdown the CPU as much as possible 344dfe7b3bfSKonstantin Belousov */ 345dfe7b3bfSKonstantin Belousov void 346dfe7b3bfSKonstantin Belousov cpu_halt(void) 347dfe7b3bfSKonstantin Belousov { 348dfe7b3bfSKonstantin Belousov for (;;) 349dfe7b3bfSKonstantin Belousov halt(); 350dfe7b3bfSKonstantin Belousov } 351dfe7b3bfSKonstantin Belousov 3528428d0f1SAndriy Gapon static void 353b7b25af0SAndriy Gapon cpu_reset_real(void) 3548428d0f1SAndriy Gapon { 3558428d0f1SAndriy Gapon struct region_descriptor null_idt; 3568428d0f1SAndriy Gapon int b; 3578428d0f1SAndriy Gapon 3588428d0f1SAndriy Gapon disable_intr(); 3598428d0f1SAndriy Gapon #ifdef CPU_ELAN 3608428d0f1SAndriy Gapon if (elan_mmcr != NULL) 3618428d0f1SAndriy Gapon elan_mmcr->RESCFG = 1; 3628428d0f1SAndriy Gapon #endif 3638428d0f1SAndriy Gapon #ifdef __i386__ 3648428d0f1SAndriy Gapon if (cpu == CPU_GEODE1100) { 3658428d0f1SAndriy Gapon /* Attempt Geode's own reset */ 3668428d0f1SAndriy Gapon outl(0xcf8, 0x80009044ul); 3678428d0f1SAndriy Gapon outl(0xcfc, 0xf); 3688428d0f1SAndriy Gapon } 3698428d0f1SAndriy Gapon #endif 3708428d0f1SAndriy Gapon #if !defined(BROKEN_KEYBOARD_RESET) 3718428d0f1SAndriy Gapon /* 3728428d0f1SAndriy Gapon * Attempt to do a CPU reset via the keyboard controller, 3738428d0f1SAndriy Gapon * do not turn off GateA20, as any machine that fails 3748428d0f1SAndriy Gapon * to do the reset here would then end up in no man's land. 3758428d0f1SAndriy Gapon */ 3768428d0f1SAndriy Gapon outb(IO_KBD + 4, 0xFE); 3778428d0f1SAndriy Gapon DELAY(500000); /* wait 0.5 sec to see if that did it */ 3788428d0f1SAndriy Gapon #endif 3798428d0f1SAndriy Gapon 3808428d0f1SAndriy Gapon /* 3818428d0f1SAndriy Gapon * Attempt to force a reset via the Reset Control register at 3828428d0f1SAndriy Gapon * I/O port 0xcf9. Bit 2 forces a system reset when it 3838428d0f1SAndriy Gapon * transitions from 0 to 1. Bit 1 selects the type of reset 3848428d0f1SAndriy Gapon * to attempt: 0 selects a "soft" reset, and 1 selects a 3858428d0f1SAndriy Gapon * "hard" reset. We try a "hard" reset. The first write sets 3868428d0f1SAndriy Gapon * bit 1 to select a "hard" reset and clears bit 2. The 3878428d0f1SAndriy Gapon * second write forces a 0 -> 1 transition in bit 2 to trigger 3888428d0f1SAndriy Gapon * a reset. 3898428d0f1SAndriy Gapon */ 3908428d0f1SAndriy Gapon outb(0xcf9, 0x2); 3918428d0f1SAndriy Gapon outb(0xcf9, 0x6); 3928428d0f1SAndriy Gapon DELAY(500000); /* wait 0.5 sec to see if that did it */ 3938428d0f1SAndriy Gapon 3948428d0f1SAndriy Gapon /* 3958428d0f1SAndriy Gapon * Attempt to force a reset via the Fast A20 and Init register 3968428d0f1SAndriy Gapon * at I/O port 0x92. Bit 1 serves as an alternate A20 gate. 3978428d0f1SAndriy Gapon * Bit 0 asserts INIT# when set to 1. We are careful to only 3988428d0f1SAndriy Gapon * preserve bit 1 while setting bit 0. We also must clear bit 3998428d0f1SAndriy Gapon * 0 before setting it if it isn't already clear. 4008428d0f1SAndriy Gapon */ 4018428d0f1SAndriy Gapon b = inb(0x92); 4028428d0f1SAndriy Gapon if (b != 0xff) { 4038428d0f1SAndriy Gapon if ((b & 0x1) != 0) 4048428d0f1SAndriy Gapon outb(0x92, b & 0xfe); 4058428d0f1SAndriy Gapon outb(0x92, b | 0x1); 4068428d0f1SAndriy Gapon DELAY(500000); /* wait 0.5 sec to see if that did it */ 4078428d0f1SAndriy Gapon } 4088428d0f1SAndriy Gapon 4098428d0f1SAndriy Gapon printf("No known reset method worked, attempting CPU shutdown\n"); 4108428d0f1SAndriy Gapon DELAY(1000000); /* wait 1 sec for printf to complete */ 4118428d0f1SAndriy Gapon 4128428d0f1SAndriy Gapon /* Wipe the IDT. */ 4138428d0f1SAndriy Gapon null_idt.rd_limit = 0; 4148428d0f1SAndriy Gapon null_idt.rd_base = 0; 4158428d0f1SAndriy Gapon lidt(&null_idt); 4168428d0f1SAndriy Gapon 4178428d0f1SAndriy Gapon /* "good night, sweet prince .... <THUNK!>" */ 4188428d0f1SAndriy Gapon breakpoint(); 4198428d0f1SAndriy Gapon 4208428d0f1SAndriy Gapon /* NOTREACHED */ 4218428d0f1SAndriy Gapon while(1); 4228428d0f1SAndriy Gapon } 4238428d0f1SAndriy Gapon 4248428d0f1SAndriy Gapon #ifdef SMP 4258428d0f1SAndriy Gapon static void 426b7b25af0SAndriy Gapon cpu_reset_proxy(void) 4278428d0f1SAndriy Gapon { 4288428d0f1SAndriy Gapon 4298428d0f1SAndriy Gapon cpu_reset_proxy_active = 1; 4308428d0f1SAndriy Gapon while (cpu_reset_proxy_active == 1) 4318428d0f1SAndriy Gapon ia32_pause(); /* Wait for other cpu to see that we've started */ 4328428d0f1SAndriy Gapon 4338428d0f1SAndriy Gapon printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid); 4348428d0f1SAndriy Gapon DELAY(1000000); 4358428d0f1SAndriy Gapon cpu_reset_real(); 4368428d0f1SAndriy Gapon } 4378428d0f1SAndriy Gapon #endif 4388428d0f1SAndriy Gapon 4398428d0f1SAndriy Gapon void 440b7b25af0SAndriy Gapon cpu_reset(void) 4418428d0f1SAndriy Gapon { 4428428d0f1SAndriy Gapon #ifdef SMP 443665919aaSConrad Meyer struct monitorbuf *mb; 4448428d0f1SAndriy Gapon cpuset_t map; 4458428d0f1SAndriy Gapon u_int cnt; 4468428d0f1SAndriy Gapon 4478428d0f1SAndriy Gapon if (smp_started) { 4488428d0f1SAndriy Gapon map = all_cpus; 4498428d0f1SAndriy Gapon CPU_CLR(PCPU_GET(cpuid), &map); 4508428d0f1SAndriy Gapon CPU_NAND(&map, &stopped_cpus); 4518428d0f1SAndriy Gapon if (!CPU_EMPTY(&map)) { 4528428d0f1SAndriy Gapon printf("cpu_reset: Stopping other CPUs\n"); 4538428d0f1SAndriy Gapon stop_cpus(map); 4548428d0f1SAndriy Gapon } 4558428d0f1SAndriy Gapon 4568428d0f1SAndriy Gapon if (PCPU_GET(cpuid) != 0) { 4578428d0f1SAndriy Gapon cpu_reset_proxyid = PCPU_GET(cpuid); 4588428d0f1SAndriy Gapon cpustop_restartfunc = cpu_reset_proxy; 4598428d0f1SAndriy Gapon cpu_reset_proxy_active = 0; 4608428d0f1SAndriy Gapon printf("cpu_reset: Restarting BSP\n"); 4618428d0f1SAndriy Gapon 4628428d0f1SAndriy Gapon /* Restart CPU #0. */ 4638428d0f1SAndriy Gapon CPU_SETOF(0, &started_cpus); 464665919aaSConrad Meyer mb = &pcpu_find(0)->pc_monitorbuf; 465665919aaSConrad Meyer atomic_store_int(&mb->stop_state, 466665919aaSConrad Meyer MONITOR_STOPSTATE_RUNNING); 4678428d0f1SAndriy Gapon 4688428d0f1SAndriy Gapon cnt = 0; 4698428d0f1SAndriy Gapon while (cpu_reset_proxy_active == 0 && cnt < 10000000) { 4708428d0f1SAndriy Gapon ia32_pause(); 4718428d0f1SAndriy Gapon cnt++; /* Wait for BSP to announce restart */ 4728428d0f1SAndriy Gapon } 4738428d0f1SAndriy Gapon if (cpu_reset_proxy_active == 0) { 4748428d0f1SAndriy Gapon printf("cpu_reset: Failed to restart BSP\n"); 4758428d0f1SAndriy Gapon } else { 4768428d0f1SAndriy Gapon cpu_reset_proxy_active = 2; 4778428d0f1SAndriy Gapon while (1) 4788428d0f1SAndriy Gapon ia32_pause(); 4798428d0f1SAndriy Gapon /* NOTREACHED */ 4808428d0f1SAndriy Gapon } 4818428d0f1SAndriy Gapon } 4828428d0f1SAndriy Gapon 4838428d0f1SAndriy Gapon DELAY(1000000); 4848428d0f1SAndriy Gapon } 4858428d0f1SAndriy Gapon #endif 4868428d0f1SAndriy Gapon cpu_reset_real(); 4878428d0f1SAndriy Gapon /* NOTREACHED */ 4888428d0f1SAndriy Gapon } 4898428d0f1SAndriy Gapon 490b57a73f8SKonstantin Belousov bool 491b57a73f8SKonstantin Belousov cpu_mwait_usable(void) 492b57a73f8SKonstantin Belousov { 493b57a73f8SKonstantin Belousov 494b57a73f8SKonstantin Belousov return ((cpu_feature2 & CPUID2_MON) != 0 && ((cpu_mon_mwait_flags & 495b57a73f8SKonstantin Belousov (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)) == 496b57a73f8SKonstantin Belousov (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK))); 497b57a73f8SKonstantin Belousov } 498b57a73f8SKonstantin Belousov 499dfe7b3bfSKonstantin Belousov void (*cpu_idle_hook)(sbintime_t) = NULL; /* ACPI idle hook. */ 500dfe7b3bfSKonstantin Belousov static int cpu_ident_amdc1e = 0; /* AMD C1E supported. */ 501dfe7b3bfSKonstantin Belousov static int idle_mwait = 1; /* Use MONITOR/MWAIT for short idle. */ 502dfe7b3bfSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, idle_mwait, CTLFLAG_RWTUN, &idle_mwait, 503dfe7b3bfSKonstantin Belousov 0, "Use MONITOR/MWAIT for short idle"); 504dfe7b3bfSKonstantin Belousov 505dfe7b3bfSKonstantin Belousov static void 506dfe7b3bfSKonstantin Belousov cpu_idle_acpi(sbintime_t sbt) 507dfe7b3bfSKonstantin Belousov { 508dfe7b3bfSKonstantin Belousov int *state; 509dfe7b3bfSKonstantin Belousov 51083dc49beSConrad Meyer state = &PCPU_PTR(monitorbuf)->idle_state; 511a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_SLEEPING); 512dfe7b3bfSKonstantin Belousov 513dfe7b3bfSKonstantin Belousov /* See comments in cpu_idle_hlt(). */ 514dfe7b3bfSKonstantin Belousov disable_intr(); 515dfe7b3bfSKonstantin Belousov if (sched_runnable()) 516dfe7b3bfSKonstantin Belousov enable_intr(); 517dfe7b3bfSKonstantin Belousov else if (cpu_idle_hook) 518dfe7b3bfSKonstantin Belousov cpu_idle_hook(sbt); 519dfe7b3bfSKonstantin Belousov else 520b57a73f8SKonstantin Belousov acpi_cpu_c1(); 521a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 522dfe7b3bfSKonstantin Belousov } 523dfe7b3bfSKonstantin Belousov 524dfe7b3bfSKonstantin Belousov static void 525dfe7b3bfSKonstantin Belousov cpu_idle_hlt(sbintime_t sbt) 526dfe7b3bfSKonstantin Belousov { 527dfe7b3bfSKonstantin Belousov int *state; 528dfe7b3bfSKonstantin Belousov 52983dc49beSConrad Meyer state = &PCPU_PTR(monitorbuf)->idle_state; 530a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_SLEEPING); 531dfe7b3bfSKonstantin Belousov 532dfe7b3bfSKonstantin Belousov /* 533dfe7b3bfSKonstantin Belousov * Since we may be in a critical section from cpu_idle(), if 534dfe7b3bfSKonstantin Belousov * an interrupt fires during that critical section we may have 535dfe7b3bfSKonstantin Belousov * a pending preemption. If the CPU halts, then that thread 536dfe7b3bfSKonstantin Belousov * may not execute until a later interrupt awakens the CPU. 537dfe7b3bfSKonstantin Belousov * To handle this race, check for a runnable thread after 538dfe7b3bfSKonstantin Belousov * disabling interrupts and immediately return if one is 539dfe7b3bfSKonstantin Belousov * found. Also, we must absolutely guarentee that hlt is 540dfe7b3bfSKonstantin Belousov * the next instruction after sti. This ensures that any 541dfe7b3bfSKonstantin Belousov * interrupt that fires after the call to disable_intr() will 542dfe7b3bfSKonstantin Belousov * immediately awaken the CPU from hlt. Finally, please note 543dfe7b3bfSKonstantin Belousov * that on x86 this works fine because of interrupts enabled only 544dfe7b3bfSKonstantin Belousov * after the instruction following sti takes place, while IF is set 545dfe7b3bfSKonstantin Belousov * to 1 immediately, allowing hlt instruction to acknowledge the 546dfe7b3bfSKonstantin Belousov * interrupt. 547dfe7b3bfSKonstantin Belousov */ 548dfe7b3bfSKonstantin Belousov disable_intr(); 549dfe7b3bfSKonstantin Belousov if (sched_runnable()) 550dfe7b3bfSKonstantin Belousov enable_intr(); 551dfe7b3bfSKonstantin Belousov else 552b57a73f8SKonstantin Belousov acpi_cpu_c1(); 553a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 554dfe7b3bfSKonstantin Belousov } 555dfe7b3bfSKonstantin Belousov 556dfe7b3bfSKonstantin Belousov static void 557dfe7b3bfSKonstantin Belousov cpu_idle_mwait(sbintime_t sbt) 558dfe7b3bfSKonstantin Belousov { 559dfe7b3bfSKonstantin Belousov int *state; 560dfe7b3bfSKonstantin Belousov 56183dc49beSConrad Meyer state = &PCPU_PTR(monitorbuf)->idle_state; 562a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_MWAIT); 563dfe7b3bfSKonstantin Belousov 564dfe7b3bfSKonstantin Belousov /* See comments in cpu_idle_hlt(). */ 565dfe7b3bfSKonstantin Belousov disable_intr(); 566dfe7b3bfSKonstantin Belousov if (sched_runnable()) { 567a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 568dfe7b3bfSKonstantin Belousov enable_intr(); 569dfe7b3bfSKonstantin Belousov return; 570dfe7b3bfSKonstantin Belousov } 571a5bd21d0SKonstantin Belousov 572dfe7b3bfSKonstantin Belousov cpu_monitor(state, 0, 0); 573a5bd21d0SKonstantin Belousov if (atomic_load_int(state) == STATE_MWAIT) 574dfe7b3bfSKonstantin Belousov __asm __volatile("sti; mwait" : : "a" (MWAIT_C1), "c" (0)); 575dfe7b3bfSKonstantin Belousov else 576dfe7b3bfSKonstantin Belousov enable_intr(); 577a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 578dfe7b3bfSKonstantin Belousov } 579dfe7b3bfSKonstantin Belousov 580dfe7b3bfSKonstantin Belousov static void 581dfe7b3bfSKonstantin Belousov cpu_idle_spin(sbintime_t sbt) 582dfe7b3bfSKonstantin Belousov { 583dfe7b3bfSKonstantin Belousov int *state; 584dfe7b3bfSKonstantin Belousov int i; 585dfe7b3bfSKonstantin Belousov 58683dc49beSConrad Meyer state = &PCPU_PTR(monitorbuf)->idle_state; 587a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 588dfe7b3bfSKonstantin Belousov 589dfe7b3bfSKonstantin Belousov /* 590dfe7b3bfSKonstantin Belousov * The sched_runnable() call is racy but as long as there is 591dfe7b3bfSKonstantin Belousov * a loop missing it one time will have just a little impact if any 592dfe7b3bfSKonstantin Belousov * (and it is much better than missing the check at all). 593dfe7b3bfSKonstantin Belousov */ 594dfe7b3bfSKonstantin Belousov for (i = 0; i < 1000; i++) { 595dfe7b3bfSKonstantin Belousov if (sched_runnable()) 596dfe7b3bfSKonstantin Belousov return; 597dfe7b3bfSKonstantin Belousov cpu_spinwait(); 598dfe7b3bfSKonstantin Belousov } 599dfe7b3bfSKonstantin Belousov } 600dfe7b3bfSKonstantin Belousov 601dfe7b3bfSKonstantin Belousov /* 602dfe7b3bfSKonstantin Belousov * C1E renders the local APIC timer dead, so we disable it by 603dfe7b3bfSKonstantin Belousov * reading the Interrupt Pending Message register and clearing 604dfe7b3bfSKonstantin Belousov * both C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27). 605dfe7b3bfSKonstantin Belousov * 606dfe7b3bfSKonstantin Belousov * Reference: 607dfe7b3bfSKonstantin Belousov * "BIOS and Kernel Developer's Guide for AMD NPT Family 0Fh Processors" 608dfe7b3bfSKonstantin Belousov * #32559 revision 3.00+ 609dfe7b3bfSKonstantin Belousov */ 610dfe7b3bfSKonstantin Belousov #define MSR_AMDK8_IPM 0xc0010055 611dfe7b3bfSKonstantin Belousov #define AMDK8_SMIONCMPHALT (1ULL << 27) 612dfe7b3bfSKonstantin Belousov #define AMDK8_C1EONCMPHALT (1ULL << 28) 613dfe7b3bfSKonstantin Belousov #define AMDK8_CMPHALT (AMDK8_SMIONCMPHALT | AMDK8_C1EONCMPHALT) 614dfe7b3bfSKonstantin Belousov 615dfe7b3bfSKonstantin Belousov void 616dfe7b3bfSKonstantin Belousov cpu_probe_amdc1e(void) 617dfe7b3bfSKonstantin Belousov { 618dfe7b3bfSKonstantin Belousov 619dfe7b3bfSKonstantin Belousov /* 620dfe7b3bfSKonstantin Belousov * Detect the presence of C1E capability mostly on latest 621dfe7b3bfSKonstantin Belousov * dual-cores (or future) k8 family. 622dfe7b3bfSKonstantin Belousov */ 623dfe7b3bfSKonstantin Belousov if (cpu_vendor_id == CPU_VENDOR_AMD && 624dfe7b3bfSKonstantin Belousov (cpu_id & 0x00000f00) == 0x00000f00 && 625dfe7b3bfSKonstantin Belousov (cpu_id & 0x0fff0000) >= 0x00040000) { 626dfe7b3bfSKonstantin Belousov cpu_ident_amdc1e = 1; 627dfe7b3bfSKonstantin Belousov } 628dfe7b3bfSKonstantin Belousov } 629dfe7b3bfSKonstantin Belousov 630dfe7b3bfSKonstantin Belousov void (*cpu_idle_fn)(sbintime_t) = cpu_idle_acpi; 631dfe7b3bfSKonstantin Belousov 632dfe7b3bfSKonstantin Belousov void 633dfe7b3bfSKonstantin Belousov cpu_idle(int busy) 634dfe7b3bfSKonstantin Belousov { 635dfe7b3bfSKonstantin Belousov uint64_t msr; 636dfe7b3bfSKonstantin Belousov sbintime_t sbt = -1; 637dfe7b3bfSKonstantin Belousov 638dfe7b3bfSKonstantin Belousov CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", 639dfe7b3bfSKonstantin Belousov busy, curcpu); 640ed95805eSJohn Baldwin #ifdef MP_WATCHDOG 641dfe7b3bfSKonstantin Belousov ap_watchdog(PCPU_GET(cpuid)); 642dfe7b3bfSKonstantin Belousov #endif 643ed95805eSJohn Baldwin 644dfe7b3bfSKonstantin Belousov /* If we are busy - try to use fast methods. */ 645dfe7b3bfSKonstantin Belousov if (busy) { 646dfe7b3bfSKonstantin Belousov if ((cpu_feature2 & CPUID2_MON) && idle_mwait) { 647dfe7b3bfSKonstantin Belousov cpu_idle_mwait(busy); 648dfe7b3bfSKonstantin Belousov goto out; 649dfe7b3bfSKonstantin Belousov } 650dfe7b3bfSKonstantin Belousov } 651dfe7b3bfSKonstantin Belousov 652dfe7b3bfSKonstantin Belousov /* If we have time - switch timers into idle mode. */ 653dfe7b3bfSKonstantin Belousov if (!busy) { 654dfe7b3bfSKonstantin Belousov critical_enter(); 655dfe7b3bfSKonstantin Belousov sbt = cpu_idleclock(); 656dfe7b3bfSKonstantin Belousov } 657dfe7b3bfSKonstantin Belousov 658dfe7b3bfSKonstantin Belousov /* Apply AMD APIC timer C1E workaround. */ 659dfe7b3bfSKonstantin Belousov if (cpu_ident_amdc1e && cpu_disable_c3_sleep) { 660dfe7b3bfSKonstantin Belousov msr = rdmsr(MSR_AMDK8_IPM); 661dfe7b3bfSKonstantin Belousov if (msr & AMDK8_CMPHALT) 662dfe7b3bfSKonstantin Belousov wrmsr(MSR_AMDK8_IPM, msr & ~AMDK8_CMPHALT); 663dfe7b3bfSKonstantin Belousov } 664dfe7b3bfSKonstantin Belousov 665dfe7b3bfSKonstantin Belousov /* Call main idle method. */ 666dfe7b3bfSKonstantin Belousov cpu_idle_fn(sbt); 667dfe7b3bfSKonstantin Belousov 668dfe7b3bfSKonstantin Belousov /* Switch timers back into active mode. */ 669dfe7b3bfSKonstantin Belousov if (!busy) { 670dfe7b3bfSKonstantin Belousov cpu_activeclock(); 671dfe7b3bfSKonstantin Belousov critical_exit(); 672dfe7b3bfSKonstantin Belousov } 673dfe7b3bfSKonstantin Belousov out: 674dfe7b3bfSKonstantin Belousov CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", 675dfe7b3bfSKonstantin Belousov busy, curcpu); 676dfe7b3bfSKonstantin Belousov } 677dfe7b3bfSKonstantin Belousov 6783f3937b4SKonstantin Belousov static int cpu_idle_apl31_workaround; 6793f3937b4SKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, idle_apl31, CTLFLAG_RW, 6803f3937b4SKonstantin Belousov &cpu_idle_apl31_workaround, 0, 681160be7ccSKonstantin Belousov "Apollo Lake APL31 MWAIT bug workaround"); 6823f3937b4SKonstantin Belousov 683dfe7b3bfSKonstantin Belousov int 684dfe7b3bfSKonstantin Belousov cpu_idle_wakeup(int cpu) 685dfe7b3bfSKonstantin Belousov { 68683dc49beSConrad Meyer struct monitorbuf *mb; 687dfe7b3bfSKonstantin Belousov int *state; 688dfe7b3bfSKonstantin Belousov 68983dc49beSConrad Meyer mb = &pcpu_find(cpu)->pc_monitorbuf; 69083dc49beSConrad Meyer state = &mb->idle_state; 691a5bd21d0SKonstantin Belousov switch (atomic_load_int(state)) { 692a5bd21d0SKonstantin Belousov case STATE_SLEEPING: 693dfe7b3bfSKonstantin Belousov return (0); 694a5bd21d0SKonstantin Belousov case STATE_MWAIT: 695a5bd21d0SKonstantin Belousov atomic_store_int(state, STATE_RUNNING); 6963f3937b4SKonstantin Belousov return (cpu_idle_apl31_workaround ? 0 : 1); 697a5bd21d0SKonstantin Belousov case STATE_RUNNING: 698a5bd21d0SKonstantin Belousov return (1); 699a5bd21d0SKonstantin Belousov default: 700a5bd21d0SKonstantin Belousov panic("bad monitor state"); 701a5bd21d0SKonstantin Belousov return (1); 702a5bd21d0SKonstantin Belousov } 703dfe7b3bfSKonstantin Belousov } 704dfe7b3bfSKonstantin Belousov 705dfe7b3bfSKonstantin Belousov /* 706dfe7b3bfSKonstantin Belousov * Ordered by speed/power consumption. 707dfe7b3bfSKonstantin Belousov */ 708a5f472c5SKonstantin Belousov static struct { 709dfe7b3bfSKonstantin Belousov void *id_fn; 710dfe7b3bfSKonstantin Belousov char *id_name; 711a5f472c5SKonstantin Belousov int id_cpuid2_flag; 712dfe7b3bfSKonstantin Belousov } idle_tbl[] = { 713a5f472c5SKonstantin Belousov { .id_fn = cpu_idle_spin, .id_name = "spin" }, 714a5f472c5SKonstantin Belousov { .id_fn = cpu_idle_mwait, .id_name = "mwait", 715a5f472c5SKonstantin Belousov .id_cpuid2_flag = CPUID2_MON }, 716a5f472c5SKonstantin Belousov { .id_fn = cpu_idle_hlt, .id_name = "hlt" }, 717a5f472c5SKonstantin Belousov { .id_fn = cpu_idle_acpi, .id_name = "acpi" }, 718dfe7b3bfSKonstantin Belousov }; 719dfe7b3bfSKonstantin Belousov 720dfe7b3bfSKonstantin Belousov static int 721dfe7b3bfSKonstantin Belousov idle_sysctl_available(SYSCTL_HANDLER_ARGS) 722dfe7b3bfSKonstantin Belousov { 723dfe7b3bfSKonstantin Belousov char *avail, *p; 724dfe7b3bfSKonstantin Belousov int error; 725dfe7b3bfSKonstantin Belousov int i; 726dfe7b3bfSKonstantin Belousov 727dfe7b3bfSKonstantin Belousov avail = malloc(256, M_TEMP, M_WAITOK); 728dfe7b3bfSKonstantin Belousov p = avail; 729a5f472c5SKonstantin Belousov for (i = 0; i < nitems(idle_tbl); i++) { 730a5f472c5SKonstantin Belousov if (idle_tbl[i].id_cpuid2_flag != 0 && 731a5f472c5SKonstantin Belousov (cpu_feature2 & idle_tbl[i].id_cpuid2_flag) == 0) 732dfe7b3bfSKonstantin Belousov continue; 733dfe7b3bfSKonstantin Belousov if (strcmp(idle_tbl[i].id_name, "acpi") == 0 && 734dfe7b3bfSKonstantin Belousov cpu_idle_hook == NULL) 735dfe7b3bfSKonstantin Belousov continue; 736dfe7b3bfSKonstantin Belousov p += sprintf(p, "%s%s", p != avail ? ", " : "", 737dfe7b3bfSKonstantin Belousov idle_tbl[i].id_name); 738dfe7b3bfSKonstantin Belousov } 739dfe7b3bfSKonstantin Belousov error = sysctl_handle_string(oidp, avail, 0, req); 740dfe7b3bfSKonstantin Belousov free(avail, M_TEMP); 741dfe7b3bfSKonstantin Belousov return (error); 742dfe7b3bfSKonstantin Belousov } 743dfe7b3bfSKonstantin Belousov 744dfe7b3bfSKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD, 745dfe7b3bfSKonstantin Belousov 0, 0, idle_sysctl_available, "A", "list of available idle functions"); 746dfe7b3bfSKonstantin Belousov 74755ba21d4SKonstantin Belousov static bool 748a5f472c5SKonstantin Belousov cpu_idle_selector(const char *new_idle_name) 74955ba21d4SKonstantin Belousov { 75055ba21d4SKonstantin Belousov int i; 75155ba21d4SKonstantin Belousov 752a5f472c5SKonstantin Belousov for (i = 0; i < nitems(idle_tbl); i++) { 753a5f472c5SKonstantin Belousov if (idle_tbl[i].id_cpuid2_flag != 0 && 754a5f472c5SKonstantin Belousov (cpu_feature2 & idle_tbl[i].id_cpuid2_flag) == 0) 75555ba21d4SKonstantin Belousov continue; 75655ba21d4SKonstantin Belousov if (strcmp(idle_tbl[i].id_name, "acpi") == 0 && 75755ba21d4SKonstantin Belousov cpu_idle_hook == NULL) 75855ba21d4SKonstantin Belousov continue; 75955ba21d4SKonstantin Belousov if (strcmp(idle_tbl[i].id_name, new_idle_name)) 76055ba21d4SKonstantin Belousov continue; 76155ba21d4SKonstantin Belousov cpu_idle_fn = idle_tbl[i].id_fn; 76255ba21d4SKonstantin Belousov if (bootverbose) 76355ba21d4SKonstantin Belousov printf("CPU idle set to %s\n", idle_tbl[i].id_name); 76455ba21d4SKonstantin Belousov return (true); 76555ba21d4SKonstantin Belousov } 76655ba21d4SKonstantin Belousov return (false); 76755ba21d4SKonstantin Belousov } 76855ba21d4SKonstantin Belousov 769dfe7b3bfSKonstantin Belousov static int 770a5f472c5SKonstantin Belousov cpu_idle_sysctl(SYSCTL_HANDLER_ARGS) 771dfe7b3bfSKonstantin Belousov { 77255ba21d4SKonstantin Belousov char buf[16], *p; 77355ba21d4SKonstantin Belousov int error, i; 774dfe7b3bfSKonstantin Belousov 775dfe7b3bfSKonstantin Belousov p = "unknown"; 776a5f472c5SKonstantin Belousov for (i = 0; i < nitems(idle_tbl); i++) { 777dfe7b3bfSKonstantin Belousov if (idle_tbl[i].id_fn == cpu_idle_fn) { 778dfe7b3bfSKonstantin Belousov p = idle_tbl[i].id_name; 779dfe7b3bfSKonstantin Belousov break; 780dfe7b3bfSKonstantin Belousov } 781dfe7b3bfSKonstantin Belousov } 782dfe7b3bfSKonstantin Belousov strncpy(buf, p, sizeof(buf)); 783dfe7b3bfSKonstantin Belousov error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 784dfe7b3bfSKonstantin Belousov if (error != 0 || req->newptr == NULL) 785dfe7b3bfSKonstantin Belousov return (error); 786a5f472c5SKonstantin Belousov return (cpu_idle_selector(buf) ? 0 : EINVAL); 787dfe7b3bfSKonstantin Belousov } 788dfe7b3bfSKonstantin Belousov 789dfe7b3bfSKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0, 790a5f472c5SKonstantin Belousov cpu_idle_sysctl, "A", "currently selected idle function"); 791835c2787SKonstantin Belousov 79255ba21d4SKonstantin Belousov static void 793a5f472c5SKonstantin Belousov cpu_idle_tun(void *unused __unused) 79455ba21d4SKonstantin Belousov { 79555ba21d4SKonstantin Belousov char tunvar[16]; 79655ba21d4SKonstantin Belousov 79755ba21d4SKonstantin Belousov if (TUNABLE_STR_FETCH("machdep.idle", tunvar, sizeof(tunvar))) 798a5f472c5SKonstantin Belousov cpu_idle_selector(tunvar); 79945ed991dSKonstantin Belousov else if (cpu_vendor_id == CPU_VENDOR_AMD && 80045ed991dSKonstantin Belousov CPUID_TO_FAMILY(cpu_id) == 0x17 && CPUID_TO_MODEL(cpu_id) == 0x1) { 80145ed991dSKonstantin Belousov /* Ryzen erratas 1057, 1109. */ 80245ed991dSKonstantin Belousov cpu_idle_selector("hlt"); 80345ed991dSKonstantin Belousov idle_mwait = 0; 804665919aaSConrad Meyer mwait_cpustop_broken = true; 80545ed991dSKonstantin Belousov } 80645ed991dSKonstantin Belousov 8073f3937b4SKonstantin Belousov if (cpu_vendor_id == CPU_VENDOR_INTEL && cpu_id == 0x506c9) { 8083f3937b4SKonstantin Belousov /* 809160be7ccSKonstantin Belousov * Apollo Lake errata APL31 (public errata APL30). 810160be7ccSKonstantin Belousov * Stores to the armed address range may not trigger 811160be7ccSKonstantin Belousov * MWAIT to resume execution. OS needs to use 812160be7ccSKonstantin Belousov * interrupts to wake processors from MWAIT-induced 813160be7ccSKonstantin Belousov * sleep states. 8143f3937b4SKonstantin Belousov */ 8153f3937b4SKonstantin Belousov cpu_idle_apl31_workaround = 1; 816665919aaSConrad Meyer mwait_cpustop_broken = true; 8173f3937b4SKonstantin Belousov } 8183f3937b4SKonstantin Belousov TUNABLE_INT_FETCH("machdep.idle_apl31", &cpu_idle_apl31_workaround); 81955ba21d4SKonstantin Belousov } 820a5f472c5SKonstantin Belousov SYSINIT(cpu_idle_tun, SI_SUB_CPU, SI_ORDER_MIDDLE, cpu_idle_tun, NULL); 82155ba21d4SKonstantin Belousov 822295f4b6cSKonstantin Belousov static int panic_on_nmi = 1; 823295f4b6cSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RWTUN, 824295f4b6cSKonstantin Belousov &panic_on_nmi, 0, 825413ed27cSAndriy Gapon "Panic on NMI raised by hardware failure"); 826835c2787SKonstantin Belousov int nmi_is_broadcast = 1; 827835c2787SKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, nmi_is_broadcast, CTLFLAG_RWTUN, 828835c2787SKonstantin Belousov &nmi_is_broadcast, 0, 829835c2787SKonstantin Belousov "Chipset NMI is broadcast"); 830835c2787SKonstantin Belousov #ifdef KDB 831835c2787SKonstantin Belousov int kdb_on_nmi = 1; 832835c2787SKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RWTUN, 833835c2787SKonstantin Belousov &kdb_on_nmi, 0, 834413ed27cSAndriy Gapon "Go to KDB on NMI with unknown source"); 835835c2787SKonstantin Belousov #endif 836835c2787SKonstantin Belousov 837295f4b6cSKonstantin Belousov void 838295f4b6cSKonstantin Belousov nmi_call_kdb(u_int cpu, u_int type, struct trapframe *frame) 839835c2787SKonstantin Belousov { 8400fb3a72aSAndriy Gapon bool claimed = false; 841835c2787SKonstantin Belousov 8420fb3a72aSAndriy Gapon #ifdef DEV_ISA 843835c2787SKonstantin Belousov /* machine/parity/power fail/"kitchen sink" faults */ 8440fb3a72aSAndriy Gapon if (isa_nmi(frame->tf_err)) { 8450fb3a72aSAndriy Gapon claimed = true; 8460fb3a72aSAndriy Gapon if (panic_on_nmi) 8470fb3a72aSAndriy Gapon panic("NMI indicates hardware failure"); 8480fb3a72aSAndriy Gapon } 8490fb3a72aSAndriy Gapon #endif /* DEV_ISA */ 850835c2787SKonstantin Belousov #ifdef KDB 8510fb3a72aSAndriy Gapon if (!claimed && kdb_on_nmi) { 852835c2787SKonstantin Belousov /* 853835c2787SKonstantin Belousov * NMI can be hooked up to a pushbutton for debugging. 854835c2787SKonstantin Belousov */ 855835c2787SKonstantin Belousov printf("NMI/cpu%d ... going to debugger\n", cpu); 856835c2787SKonstantin Belousov kdb_trap(type, 0, frame); 857835c2787SKonstantin Belousov } 858835c2787SKonstantin Belousov #endif /* KDB */ 859295f4b6cSKonstantin Belousov } 860835c2787SKonstantin Belousov 861295f4b6cSKonstantin Belousov void 862295f4b6cSKonstantin Belousov nmi_handle_intr(u_int type, struct trapframe *frame) 863835c2787SKonstantin Belousov { 864835c2787SKonstantin Belousov 865835c2787SKonstantin Belousov #ifdef SMP 866295f4b6cSKonstantin Belousov if (nmi_is_broadcast) { 867295f4b6cSKonstantin Belousov nmi_call_kdb_smp(type, frame); 868295f4b6cSKonstantin Belousov return; 869295f4b6cSKonstantin Belousov } 870835c2787SKonstantin Belousov #endif 8711d6dfd12SKonstantin Belousov nmi_call_kdb(PCPU_GET(cpuid), type, frame); 872835c2787SKonstantin Belousov } 873319117fdSKonstantin Belousov 874319117fdSKonstantin Belousov int hw_ibrs_active; 875319117fdSKonstantin Belousov int hw_ibrs_disable = 1; 876319117fdSKonstantin Belousov 877319117fdSKonstantin Belousov SYSCTL_INT(_hw, OID_AUTO, ibrs_active, CTLFLAG_RD, &hw_ibrs_active, 0, 878b31b965eSKonstantin Belousov "Indirect Branch Restricted Speculation active"); 879319117fdSKonstantin Belousov 880319117fdSKonstantin Belousov void 881319117fdSKonstantin Belousov hw_ibrs_recalculate(void) 882319117fdSKonstantin Belousov { 883319117fdSKonstantin Belousov if ((cpu_ia32_arch_caps & IA32_ARCH_CAP_IBRS_ALL) != 0) { 884*fa83f689SKonstantin Belousov x86_msr_op(MSR_IA32_SPEC_CTRL, MSR_OP_LOCAL | 885*fa83f689SKonstantin Belousov (hw_ibrs_disable ? MSR_OP_ANDNOT : MSR_OP_OR), 886*fa83f689SKonstantin Belousov IA32_SPEC_CTRL_IBRS); 887319117fdSKonstantin Belousov return; 888319117fdSKonstantin Belousov } 889319117fdSKonstantin Belousov hw_ibrs_active = (cpu_stdext_feature3 & CPUID_STDEXT3_IBPB) != 0 && 890319117fdSKonstantin Belousov !hw_ibrs_disable; 891319117fdSKonstantin Belousov } 892319117fdSKonstantin Belousov 893319117fdSKonstantin Belousov static int 894319117fdSKonstantin Belousov hw_ibrs_disable_handler(SYSCTL_HANDLER_ARGS) 895319117fdSKonstantin Belousov { 896319117fdSKonstantin Belousov int error, val; 897319117fdSKonstantin Belousov 898319117fdSKonstantin Belousov val = hw_ibrs_disable; 899319117fdSKonstantin Belousov error = sysctl_handle_int(oidp, &val, 0, req); 900319117fdSKonstantin Belousov if (error != 0 || req->newptr == NULL) 901319117fdSKonstantin Belousov return (error); 902319117fdSKonstantin Belousov hw_ibrs_disable = val != 0; 903319117fdSKonstantin Belousov hw_ibrs_recalculate(); 904319117fdSKonstantin Belousov return (0); 905319117fdSKonstantin Belousov } 906319117fdSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, ibrs_disable, CTLTYPE_INT | CTLFLAG_RWTUN | 907319117fdSKonstantin Belousov CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0, hw_ibrs_disable_handler, "I", 908b31b965eSKonstantin Belousov "Disable Indirect Branch Restricted Speculation"); 9098fbcc334SKonstantin Belousov 9103621ba1eSKonstantin Belousov int hw_ssb_active; 9113621ba1eSKonstantin Belousov int hw_ssb_disable; 9123621ba1eSKonstantin Belousov 9133621ba1eSKonstantin Belousov SYSCTL_INT(_hw, OID_AUTO, spec_store_bypass_disable_active, CTLFLAG_RD, 9143621ba1eSKonstantin Belousov &hw_ssb_active, 0, 9153621ba1eSKonstantin Belousov "Speculative Store Bypass Disable active"); 9163621ba1eSKonstantin Belousov 9173621ba1eSKonstantin Belousov static void 9183621ba1eSKonstantin Belousov hw_ssb_set(bool enable, bool for_all_cpus) 9193621ba1eSKonstantin Belousov { 9203621ba1eSKonstantin Belousov 9213621ba1eSKonstantin Belousov if ((cpu_stdext_feature3 & CPUID_STDEXT3_SSBD) == 0) { 9223621ba1eSKonstantin Belousov hw_ssb_active = 0; 9233621ba1eSKonstantin Belousov return; 9243621ba1eSKonstantin Belousov } 9253621ba1eSKonstantin Belousov hw_ssb_active = enable; 926*fa83f689SKonstantin Belousov x86_msr_op(MSR_IA32_SPEC_CTRL, 927*fa83f689SKonstantin Belousov (enable ? MSR_OP_OR : MSR_OP_ANDNOT) | 928*fa83f689SKonstantin Belousov (for_all_cpus ? MSR_OP_SCHED : MSR_OP_LOCAL), IA32_SPEC_CTRL_SSBD); 9293621ba1eSKonstantin Belousov } 9303621ba1eSKonstantin Belousov 9313621ba1eSKonstantin Belousov void 9323621ba1eSKonstantin Belousov hw_ssb_recalculate(bool all_cpus) 9333621ba1eSKonstantin Belousov { 9343621ba1eSKonstantin Belousov 9353621ba1eSKonstantin Belousov switch (hw_ssb_disable) { 9363621ba1eSKonstantin Belousov default: 9373621ba1eSKonstantin Belousov hw_ssb_disable = 0; 9383621ba1eSKonstantin Belousov /* FALLTHROUGH */ 9393621ba1eSKonstantin Belousov case 0: /* off */ 9403621ba1eSKonstantin Belousov hw_ssb_set(false, all_cpus); 9413621ba1eSKonstantin Belousov break; 9423621ba1eSKonstantin Belousov case 1: /* on */ 9433621ba1eSKonstantin Belousov hw_ssb_set(true, all_cpus); 9443621ba1eSKonstantin Belousov break; 9453621ba1eSKonstantin Belousov case 2: /* auto */ 94623437573SKonstantin Belousov hw_ssb_set((cpu_ia32_arch_caps & IA32_ARCH_CAP_SSB_NO) != 0 ? 9473621ba1eSKonstantin Belousov false : true, all_cpus); 9483621ba1eSKonstantin Belousov break; 9493621ba1eSKonstantin Belousov } 9503621ba1eSKonstantin Belousov } 9513621ba1eSKonstantin Belousov 9523621ba1eSKonstantin Belousov static int 9533621ba1eSKonstantin Belousov hw_ssb_disable_handler(SYSCTL_HANDLER_ARGS) 9543621ba1eSKonstantin Belousov { 9553621ba1eSKonstantin Belousov int error, val; 9563621ba1eSKonstantin Belousov 9573621ba1eSKonstantin Belousov val = hw_ssb_disable; 9583621ba1eSKonstantin Belousov error = sysctl_handle_int(oidp, &val, 0, req); 9593621ba1eSKonstantin Belousov if (error != 0 || req->newptr == NULL) 9603621ba1eSKonstantin Belousov return (error); 9613621ba1eSKonstantin Belousov hw_ssb_disable = val; 9623621ba1eSKonstantin Belousov hw_ssb_recalculate(true); 9633621ba1eSKonstantin Belousov return (0); 9643621ba1eSKonstantin Belousov } 9653621ba1eSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, spec_store_bypass_disable, CTLTYPE_INT | 9663621ba1eSKonstantin Belousov CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0, 9673621ba1eSKonstantin Belousov hw_ssb_disable_handler, "I", 9683621ba1eSKonstantin Belousov "Speculative Store Bypass Disable (0 - off, 1 - on, 2 - auto"); 9693621ba1eSKonstantin Belousov 9707355a02bSKonstantin Belousov int hw_mds_disable; 9717355a02bSKonstantin Belousov 9727355a02bSKonstantin Belousov /* 9737355a02bSKonstantin Belousov * Handler for Microarchitectural Data Sampling issues. Really not a 9747355a02bSKonstantin Belousov * pointer to C function: on amd64 the code must not change any CPU 9757355a02bSKonstantin Belousov * architectural state except possibly %rflags. Also, it is always 9767355a02bSKonstantin Belousov * called with interrupts disabled. 9777355a02bSKonstantin Belousov */ 9787355a02bSKonstantin Belousov void mds_handler_void(void); 9797355a02bSKonstantin Belousov void mds_handler_verw(void); 9807355a02bSKonstantin Belousov void mds_handler_ivb(void); 9817355a02bSKonstantin Belousov void mds_handler_bdw(void); 9827355a02bSKonstantin Belousov void mds_handler_skl_sse(void); 9837355a02bSKonstantin Belousov void mds_handler_skl_avx(void); 9847355a02bSKonstantin Belousov void mds_handler_skl_avx512(void); 9857355a02bSKonstantin Belousov void mds_handler_silvermont(void); 986e2e0470dSKonstantin Belousov void (*mds_handler)(void) = mds_handler_void; 9877355a02bSKonstantin Belousov 9887355a02bSKonstantin Belousov static int 9897355a02bSKonstantin Belousov sysctl_hw_mds_disable_state_handler(SYSCTL_HANDLER_ARGS) 9907355a02bSKonstantin Belousov { 9917355a02bSKonstantin Belousov const char *state; 9927355a02bSKonstantin Belousov 9937355a02bSKonstantin Belousov if (mds_handler == mds_handler_void) 9947355a02bSKonstantin Belousov state = "inactive"; 9957355a02bSKonstantin Belousov else if (mds_handler == mds_handler_verw) 9967355a02bSKonstantin Belousov state = "VERW"; 9977355a02bSKonstantin Belousov else if (mds_handler == mds_handler_ivb) 9987355a02bSKonstantin Belousov state = "software IvyBridge"; 9997355a02bSKonstantin Belousov else if (mds_handler == mds_handler_bdw) 10007355a02bSKonstantin Belousov state = "software Broadwell"; 10017355a02bSKonstantin Belousov else if (mds_handler == mds_handler_skl_sse) 10027355a02bSKonstantin Belousov state = "software Skylake SSE"; 10037355a02bSKonstantin Belousov else if (mds_handler == mds_handler_skl_avx) 10047355a02bSKonstantin Belousov state = "software Skylake AVX"; 10057355a02bSKonstantin Belousov else if (mds_handler == mds_handler_skl_avx512) 10067355a02bSKonstantin Belousov state = "software Skylake AVX512"; 10077355a02bSKonstantin Belousov else if (mds_handler == mds_handler_silvermont) 10087355a02bSKonstantin Belousov state = "software Silvermont"; 10097355a02bSKonstantin Belousov else 10107355a02bSKonstantin Belousov state = "unknown"; 10117355a02bSKonstantin Belousov return (SYSCTL_OUT(req, state, strlen(state))); 10127355a02bSKonstantin Belousov } 10137355a02bSKonstantin Belousov 10147355a02bSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, mds_disable_state, 10157355a02bSKonstantin Belousov CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 10167355a02bSKonstantin Belousov sysctl_hw_mds_disable_state_handler, "A", 10177355a02bSKonstantin Belousov "Microarchitectural Data Sampling Mitigation state"); 10187355a02bSKonstantin Belousov 10197355a02bSKonstantin Belousov _Static_assert(__offsetof(struct pcpu, pc_mds_tmp) % 64 == 0, "MDS AVX512"); 10207355a02bSKonstantin Belousov 10217355a02bSKonstantin Belousov void 10227355a02bSKonstantin Belousov hw_mds_recalculate(void) 10237355a02bSKonstantin Belousov { 10247355a02bSKonstantin Belousov struct pcpu *pc; 10257355a02bSKonstantin Belousov vm_offset_t b64; 10267355a02bSKonstantin Belousov u_long xcr0; 10277355a02bSKonstantin Belousov int i; 10287355a02bSKonstantin Belousov 10297355a02bSKonstantin Belousov /* 10307355a02bSKonstantin Belousov * Allow user to force VERW variant even if MD_CLEAR is not 10317355a02bSKonstantin Belousov * reported. For instance, hypervisor might unknowingly 10327355a02bSKonstantin Belousov * filter the cap out. 10337355a02bSKonstantin Belousov * For the similar reasons, and for testing, allow to enable 10347355a02bSKonstantin Belousov * mitigation even for RDCL_NO or MDS_NO caps. 10357355a02bSKonstantin Belousov */ 10367355a02bSKonstantin Belousov if (cpu_vendor_id != CPU_VENDOR_INTEL || hw_mds_disable == 0 || 10377355a02bSKonstantin Belousov ((cpu_ia32_arch_caps & (IA32_ARCH_CAP_RDCL_NO | 10387355a02bSKonstantin Belousov IA32_ARCH_CAP_MDS_NO)) != 0 && hw_mds_disable == 3)) { 10397355a02bSKonstantin Belousov mds_handler = mds_handler_void; 10407355a02bSKonstantin Belousov } else if (((cpu_stdext_feature3 & CPUID_STDEXT3_MD_CLEAR) != 0 && 10417355a02bSKonstantin Belousov hw_mds_disable == 3) || hw_mds_disable == 1) { 10427355a02bSKonstantin Belousov mds_handler = mds_handler_verw; 10437355a02bSKonstantin Belousov } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 && 10447355a02bSKonstantin Belousov (CPUID_TO_MODEL(cpu_id) == 0x2e || CPUID_TO_MODEL(cpu_id) == 0x1e || 10457355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x1f || CPUID_TO_MODEL(cpu_id) == 0x1a || 10467355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x2f || CPUID_TO_MODEL(cpu_id) == 0x25 || 10477355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x2c || CPUID_TO_MODEL(cpu_id) == 0x2d || 10487355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x2a || CPUID_TO_MODEL(cpu_id) == 0x3e || 10497355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x3a) && 10507355a02bSKonstantin Belousov (hw_mds_disable == 2 || hw_mds_disable == 3)) { 10517355a02bSKonstantin Belousov /* 10527355a02bSKonstantin Belousov * Nehalem, SandyBridge, IvyBridge 10537355a02bSKonstantin Belousov */ 10547355a02bSKonstantin Belousov CPU_FOREACH(i) { 10557355a02bSKonstantin Belousov pc = pcpu_find(i); 10567355a02bSKonstantin Belousov if (pc->pc_mds_buf == NULL) { 10577355a02bSKonstantin Belousov pc->pc_mds_buf = malloc_domainset(672, M_TEMP, 10587355a02bSKonstantin Belousov DOMAINSET_PREF(pc->pc_domain), M_WAITOK); 10597355a02bSKonstantin Belousov bzero(pc->pc_mds_buf, 16); 10607355a02bSKonstantin Belousov } 10617355a02bSKonstantin Belousov } 10627355a02bSKonstantin Belousov mds_handler = mds_handler_ivb; 10637355a02bSKonstantin Belousov } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 && 10647355a02bSKonstantin Belousov (CPUID_TO_MODEL(cpu_id) == 0x3f || CPUID_TO_MODEL(cpu_id) == 0x3c || 10657355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x45 || CPUID_TO_MODEL(cpu_id) == 0x46 || 10667355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x56 || CPUID_TO_MODEL(cpu_id) == 0x4f || 10677355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x47 || CPUID_TO_MODEL(cpu_id) == 0x3d) && 10687355a02bSKonstantin Belousov (hw_mds_disable == 2 || hw_mds_disable == 3)) { 10697355a02bSKonstantin Belousov /* 10707355a02bSKonstantin Belousov * Haswell, Broadwell 10717355a02bSKonstantin Belousov */ 10727355a02bSKonstantin Belousov CPU_FOREACH(i) { 10737355a02bSKonstantin Belousov pc = pcpu_find(i); 10747355a02bSKonstantin Belousov if (pc->pc_mds_buf == NULL) { 10757355a02bSKonstantin Belousov pc->pc_mds_buf = malloc_domainset(1536, M_TEMP, 10767355a02bSKonstantin Belousov DOMAINSET_PREF(pc->pc_domain), M_WAITOK); 10777355a02bSKonstantin Belousov bzero(pc->pc_mds_buf, 16); 10787355a02bSKonstantin Belousov } 10797355a02bSKonstantin Belousov } 10807355a02bSKonstantin Belousov mds_handler = mds_handler_bdw; 10817355a02bSKonstantin Belousov } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 && 10827355a02bSKonstantin Belousov ((CPUID_TO_MODEL(cpu_id) == 0x55 && (cpu_id & 10837355a02bSKonstantin Belousov CPUID_STEPPING) <= 5) || 10847355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x4e || CPUID_TO_MODEL(cpu_id) == 0x5e || 10857355a02bSKonstantin Belousov (CPUID_TO_MODEL(cpu_id) == 0x8e && (cpu_id & 10867355a02bSKonstantin Belousov CPUID_STEPPING) <= 0xb) || 10877355a02bSKonstantin Belousov (CPUID_TO_MODEL(cpu_id) == 0x9e && (cpu_id & 10887355a02bSKonstantin Belousov CPUID_STEPPING) <= 0xc)) && 10897355a02bSKonstantin Belousov (hw_mds_disable == 2 || hw_mds_disable == 3)) { 10907355a02bSKonstantin Belousov /* 10917355a02bSKonstantin Belousov * Skylake, KabyLake, CoffeeLake, WhiskeyLake, 10927355a02bSKonstantin Belousov * CascadeLake 10937355a02bSKonstantin Belousov */ 10947355a02bSKonstantin Belousov CPU_FOREACH(i) { 10957355a02bSKonstantin Belousov pc = pcpu_find(i); 10967355a02bSKonstantin Belousov if (pc->pc_mds_buf == NULL) { 10977355a02bSKonstantin Belousov pc->pc_mds_buf = malloc_domainset(6 * 1024, 10987355a02bSKonstantin Belousov M_TEMP, DOMAINSET_PREF(pc->pc_domain), 10997355a02bSKonstantin Belousov M_WAITOK); 11007355a02bSKonstantin Belousov b64 = (vm_offset_t)malloc_domainset(64 + 63, 11017355a02bSKonstantin Belousov M_TEMP, DOMAINSET_PREF(pc->pc_domain), 11027355a02bSKonstantin Belousov M_WAITOK); 11037355a02bSKonstantin Belousov pc->pc_mds_buf64 = (void *)roundup2(b64, 64); 11047355a02bSKonstantin Belousov bzero(pc->pc_mds_buf64, 64); 11057355a02bSKonstantin Belousov } 11067355a02bSKonstantin Belousov } 11077355a02bSKonstantin Belousov xcr0 = rxcr(0); 11087355a02bSKonstantin Belousov if ((xcr0 & XFEATURE_ENABLED_ZMM_HI256) != 0 && 110999a6085fSScott Long (cpu_stdext_feature & CPUID_STDEXT_AVX512DQ) != 0) 11107355a02bSKonstantin Belousov mds_handler = mds_handler_skl_avx512; 11117355a02bSKonstantin Belousov else if ((xcr0 & XFEATURE_ENABLED_AVX) != 0 && 11127355a02bSKonstantin Belousov (cpu_feature2 & CPUID2_AVX) != 0) 11137355a02bSKonstantin Belousov mds_handler = mds_handler_skl_avx; 11147355a02bSKonstantin Belousov else 11157355a02bSKonstantin Belousov mds_handler = mds_handler_skl_sse; 11167355a02bSKonstantin Belousov } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 && 11177355a02bSKonstantin Belousov ((CPUID_TO_MODEL(cpu_id) == 0x37 || 11187355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x4a || 11197355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x4c || 11207355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x4d || 11217355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x5a || 11227355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x5d || 11237355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x6e || 11247355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x65 || 11257355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x75 || 11267355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x1c || 11277355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x26 || 11287355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x27 || 11297355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x35 || 11307355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x36 || 11317355a02bSKonstantin Belousov CPUID_TO_MODEL(cpu_id) == 0x7a))) { 11327355a02bSKonstantin Belousov /* Silvermont, Airmont */ 11337355a02bSKonstantin Belousov CPU_FOREACH(i) { 11347355a02bSKonstantin Belousov pc = pcpu_find(i); 11357355a02bSKonstantin Belousov if (pc->pc_mds_buf == NULL) 11367355a02bSKonstantin Belousov pc->pc_mds_buf = malloc(256, M_TEMP, M_WAITOK); 11377355a02bSKonstantin Belousov } 11387355a02bSKonstantin Belousov mds_handler = mds_handler_silvermont; 11397355a02bSKonstantin Belousov } else { 11407355a02bSKonstantin Belousov hw_mds_disable = 0; 11417355a02bSKonstantin Belousov mds_handler = mds_handler_void; 11427355a02bSKonstantin Belousov } 11437355a02bSKonstantin Belousov } 11447355a02bSKonstantin Belousov 114548ec6d3bSKonstantin Belousov static void 114648ec6d3bSKonstantin Belousov hw_mds_recalculate_boot(void *arg __unused) 114748ec6d3bSKonstantin Belousov { 114848ec6d3bSKonstantin Belousov 114948ec6d3bSKonstantin Belousov hw_mds_recalculate(); 115048ec6d3bSKonstantin Belousov } 115148ec6d3bSKonstantin Belousov SYSINIT(mds_recalc, SI_SUB_SMP, SI_ORDER_ANY, hw_mds_recalculate_boot, NULL); 115248ec6d3bSKonstantin Belousov 11537355a02bSKonstantin Belousov static int 11547355a02bSKonstantin Belousov sysctl_mds_disable_handler(SYSCTL_HANDLER_ARGS) 11557355a02bSKonstantin Belousov { 11567355a02bSKonstantin Belousov int error, val; 11577355a02bSKonstantin Belousov 11587355a02bSKonstantin Belousov val = hw_mds_disable; 11597355a02bSKonstantin Belousov error = sysctl_handle_int(oidp, &val, 0, req); 11607355a02bSKonstantin Belousov if (error != 0 || req->newptr == NULL) 11617355a02bSKonstantin Belousov return (error); 11627355a02bSKonstantin Belousov if (val < 0 || val > 3) 11637355a02bSKonstantin Belousov return (EINVAL); 11647355a02bSKonstantin Belousov hw_mds_disable = val; 11657355a02bSKonstantin Belousov hw_mds_recalculate(); 11667355a02bSKonstantin Belousov return (0); 11677355a02bSKonstantin Belousov } 11687355a02bSKonstantin Belousov 11697355a02bSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, mds_disable, CTLTYPE_INT | 11707355a02bSKonstantin Belousov CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0, 11717355a02bSKonstantin Belousov sysctl_mds_disable_handler, "I", 11727355a02bSKonstantin Belousov "Microarchitectural Data Sampling Mitigation " 11737355a02bSKonstantin Belousov "(0 - off, 1 - on VERW, 2 - on SW, 3 - on AUTO"); 11747355a02bSKonstantin Belousov 1175e3721601SScott Long 1176e3721601SScott Long /* 1177e3721601SScott Long * Intel Transactional Memory Asynchronous Abort Mitigation 1178e3721601SScott Long * CVE-2019-11135 1179e3721601SScott Long */ 1180e3721601SScott Long int x86_taa_enable; 1181e3721601SScott Long int x86_taa_state; 1182e3721601SScott Long enum { 1183e3721601SScott Long TAA_NONE = 0, 1184e3721601SScott Long TAA_TSX_DISABLE = 1, 1185e3721601SScott Long TAA_VERW = 2, 1186e3721601SScott Long TAA_AUTO = 3, 1187e3721601SScott Long TAA_TAA_NO = 4 1188e3721601SScott Long }; 1189e3721601SScott Long 1190e3721601SScott Long static void 1191e3721601SScott Long taa_set(bool enable, bool all) 1192e3721601SScott Long { 1193e3721601SScott Long 1194*fa83f689SKonstantin Belousov x86_msr_op(MSR_IA32_TSX_CTRL, 1195*fa83f689SKonstantin Belousov (enable ? MSR_OP_OR : MSR_OP_ANDNOT) | 1196*fa83f689SKonstantin Belousov (all ? MSR_OP_RENDEZVOUS : MSR_OP_LOCAL), 1197*fa83f689SKonstantin Belousov IA32_TSX_CTRL_RTM_DISABLE | IA32_TSX_CTRL_TSX_CPUID_CLEAR); 1198e3721601SScott Long } 1199e3721601SScott Long 1200e3721601SScott Long void 1201e3721601SScott Long x86_taa_recalculate(void) 1202e3721601SScott Long { 1203e3721601SScott Long static int taa_saved_mds_disable = 0; 1204e3721601SScott Long int taa_need = 0, taa_state = 0; 1205e3721601SScott Long int mds_disable = 0, need_mds_recalc = 0; 1206e3721601SScott Long 1207e3721601SScott Long /* Check CPUID.07h.EBX.HLE and RTM for the presence of TSX */ 1208e3721601SScott Long if ((cpu_stdext_feature & CPUID_STDEXT_HLE) == 0 || 1209e3721601SScott Long (cpu_stdext_feature & CPUID_STDEXT_RTM) == 0) { 1210e3721601SScott Long /* TSX is not present */ 1211e3721601SScott Long x86_taa_state = 0; 1212e3721601SScott Long return; 1213e3721601SScott Long } 1214e3721601SScott Long 1215e3721601SScott Long /* Check to see what mitigation options the CPU gives us */ 1216e3721601SScott Long if (cpu_ia32_arch_caps & IA32_ARCH_CAP_TAA_NO) { 1217e3721601SScott Long /* CPU is not suseptible to TAA */ 1218e3721601SScott Long taa_need = TAA_NONE; 1219e3721601SScott Long taa_state = TAA_TAA_NO; 1220e3721601SScott Long } else if (cpu_ia32_arch_caps & IA32_ARCH_CAP_TSX_CTRL) { 1221e3721601SScott Long /* 1222e3721601SScott Long * CPU can turn off TSX. This is the next best option 1223e3721601SScott Long * if TAA_NO hardware mitigation isn't present 1224e3721601SScott Long */ 1225e3721601SScott Long taa_need = TAA_TSX_DISABLE; 1226e3721601SScott Long } else { 1227e3721601SScott Long /* No TSX/TAA specific remedies are available. */ 1228e3721601SScott Long if (x86_taa_enable == TAA_TSX_DISABLE) { 1229e3721601SScott Long if (bootverbose) 1230e3721601SScott Long printf("TSX control not available\n"); 1231e3721601SScott Long return; 1232e3721601SScott Long } else 1233e3721601SScott Long taa_need = TAA_VERW; 1234e3721601SScott Long } 1235e3721601SScott Long 1236e3721601SScott Long /* Can we automatically take action, or are we being forced? */ 1237e3721601SScott Long if (x86_taa_enable == TAA_AUTO) 1238e3721601SScott Long taa_state = taa_need; 1239e3721601SScott Long else 1240e3721601SScott Long taa_state = x86_taa_enable; 1241e3721601SScott Long 1242e3721601SScott Long /* No state change, nothing to do */ 1243e3721601SScott Long if (taa_state == x86_taa_state) { 1244e3721601SScott Long if (bootverbose) 1245e3721601SScott Long printf("No TSX change made\n"); 1246e3721601SScott Long return; 1247e3721601SScott Long } 1248e3721601SScott Long 1249e3721601SScott Long /* Does the MSR need to be turned on or off? */ 1250e3721601SScott Long if (taa_state == TAA_TSX_DISABLE) 1251e3721601SScott Long taa_set(true, true); 1252e3721601SScott Long else if (x86_taa_state == TAA_TSX_DISABLE) 1253e3721601SScott Long taa_set(false, true); 1254e3721601SScott Long 1255e3721601SScott Long /* Does MDS need to be set to turn on VERW? */ 1256e3721601SScott Long if (taa_state == TAA_VERW) { 1257e3721601SScott Long taa_saved_mds_disable = hw_mds_disable; 1258e3721601SScott Long mds_disable = hw_mds_disable = 1; 1259e3721601SScott Long need_mds_recalc = 1; 1260e3721601SScott Long } else if (x86_taa_state == TAA_VERW) { 1261e3721601SScott Long mds_disable = hw_mds_disable = taa_saved_mds_disable; 1262e3721601SScott Long need_mds_recalc = 1; 1263e3721601SScott Long } 1264e3721601SScott Long if (need_mds_recalc) { 1265e3721601SScott Long hw_mds_recalculate(); 1266e3721601SScott Long if (mds_disable != hw_mds_disable) { 1267e3721601SScott Long if (bootverbose) 1268e3721601SScott Long printf("Cannot change MDS state for TAA\n"); 1269e3721601SScott Long /* Don't update our state */ 1270e3721601SScott Long return; 1271e3721601SScott Long } 1272e3721601SScott Long } 1273e3721601SScott Long 1274e3721601SScott Long x86_taa_state = taa_state; 1275e3721601SScott Long return; 1276e3721601SScott Long } 1277e3721601SScott Long 1278e3721601SScott Long static void 1279e3721601SScott Long taa_recalculate_boot(void * arg __unused) 1280e3721601SScott Long { 1281e3721601SScott Long 1282e3721601SScott Long x86_taa_recalculate(); 1283e3721601SScott Long } 1284e3721601SScott Long SYSINIT(taa_recalc, SI_SUB_SMP, SI_ORDER_ANY, taa_recalculate_boot, NULL); 1285e3721601SScott Long 1286e3721601SScott Long SYSCTL_NODE(_machdep_mitigations, OID_AUTO, taa, CTLFLAG_RW, 0, 1287e3721601SScott Long "TSX Asynchronous Abort Mitigation"); 1288e3721601SScott Long 1289e3721601SScott Long static int 1290e3721601SScott Long sysctl_taa_handler(SYSCTL_HANDLER_ARGS) 1291e3721601SScott Long { 1292e3721601SScott Long int error, val; 1293e3721601SScott Long 1294e3721601SScott Long val = x86_taa_enable; 1295e3721601SScott Long error = sysctl_handle_int(oidp, &val, 0, req); 1296e3721601SScott Long if (error != 0 || req->newptr == NULL) 1297e3721601SScott Long return (error); 1298e3721601SScott Long if (val < TAA_NONE || val > TAA_AUTO) 1299e3721601SScott Long return (EINVAL); 1300e3721601SScott Long x86_taa_enable = val; 1301e3721601SScott Long x86_taa_recalculate(); 1302e3721601SScott Long return (0); 1303e3721601SScott Long } 1304e3721601SScott Long 1305e3721601SScott Long SYSCTL_PROC(_machdep_mitigations_taa, OID_AUTO, enable, CTLTYPE_INT | 1306e3721601SScott Long CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0, 1307e3721601SScott Long sysctl_taa_handler, "I", 1308e3721601SScott Long "TAA Mitigation enablement control " 1309e3721601SScott Long "(0 - off, 1 - disable TSX, 2 - VERW, 3 - on AUTO"); 1310e3721601SScott Long 1311e3721601SScott Long static int 1312e3721601SScott Long sysctl_taa_state_handler(SYSCTL_HANDLER_ARGS) 1313e3721601SScott Long { 1314e3721601SScott Long const char *state; 1315e3721601SScott Long 1316e3721601SScott Long switch (x86_taa_state) { 1317e3721601SScott Long case TAA_NONE: 1318e3721601SScott Long state = "inactive"; 1319e3721601SScott Long break; 1320e3721601SScott Long case TAA_TSX_DISABLE: 1321e3721601SScott Long state = "TSX disabled"; 1322e3721601SScott Long break; 1323e3721601SScott Long case TAA_VERW: 1324e3721601SScott Long state = "VERW"; 1325e3721601SScott Long break; 1326e3721601SScott Long case TAA_TAA_NO: 1327e3721601SScott Long state = "Not vulnerable"; 1328e3721601SScott Long break; 1329e3721601SScott Long default: 1330e3721601SScott Long state = "unknown"; 1331e3721601SScott Long } 1332e3721601SScott Long 1333e3721601SScott Long return (SYSCTL_OUT(req, state, strlen(state))); 1334e3721601SScott Long } 1335e3721601SScott Long 1336e3721601SScott Long SYSCTL_PROC(_machdep_mitigations_taa, OID_AUTO, state, 1337e3721601SScott Long CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 1338e3721601SScott Long sysctl_taa_state_handler, "A", 1339e3721601SScott Long "TAA Mitigation state"); 1340e3721601SScott Long 13418fbcc334SKonstantin Belousov /* 13428fbcc334SKonstantin Belousov * Enable and restore kernel text write permissions. 13438fbcc334SKonstantin Belousov * Callers must ensure that disable_wp()/restore_wp() are executed 13448fbcc334SKonstantin Belousov * without rescheduling on the same core. 13458fbcc334SKonstantin Belousov */ 13468fbcc334SKonstantin Belousov bool 13478fbcc334SKonstantin Belousov disable_wp(void) 13488fbcc334SKonstantin Belousov { 13498fbcc334SKonstantin Belousov u_int cr0; 13508fbcc334SKonstantin Belousov 13518fbcc334SKonstantin Belousov cr0 = rcr0(); 13528fbcc334SKonstantin Belousov if ((cr0 & CR0_WP) == 0) 13538fbcc334SKonstantin Belousov return (false); 13548fbcc334SKonstantin Belousov load_cr0(cr0 & ~CR0_WP); 13558fbcc334SKonstantin Belousov return (true); 13568fbcc334SKonstantin Belousov } 13578fbcc334SKonstantin Belousov 13588fbcc334SKonstantin Belousov void 13598fbcc334SKonstantin Belousov restore_wp(bool old_wp) 13608fbcc334SKonstantin Belousov { 13618fbcc334SKonstantin Belousov 13628fbcc334SKonstantin Belousov if (old_wp) 13638fbcc334SKonstantin Belousov load_cr0(rcr0() | CR0_WP); 13648fbcc334SKonstantin Belousov } 13658fbcc334SKonstantin Belousov 13667705dd4dSKonstantin Belousov bool 13677705dd4dSKonstantin Belousov acpi_get_fadt_bootflags(uint16_t *flagsp) 13687705dd4dSKonstantin Belousov { 13697705dd4dSKonstantin Belousov #ifdef DEV_ACPI 13707705dd4dSKonstantin Belousov ACPI_TABLE_FADT *fadt; 13717705dd4dSKonstantin Belousov vm_paddr_t physaddr; 13727705dd4dSKonstantin Belousov 13737705dd4dSKonstantin Belousov physaddr = acpi_find_table(ACPI_SIG_FADT); 13747705dd4dSKonstantin Belousov if (physaddr == 0) 13757705dd4dSKonstantin Belousov return (false); 13767705dd4dSKonstantin Belousov fadt = acpi_map_table(physaddr, ACPI_SIG_FADT); 13777705dd4dSKonstantin Belousov if (fadt == NULL) 13787705dd4dSKonstantin Belousov return (false); 13797705dd4dSKonstantin Belousov *flagsp = fadt->BootFlags; 13807705dd4dSKonstantin Belousov acpi_unmap_table(fadt); 13817705dd4dSKonstantin Belousov return (true); 13827705dd4dSKonstantin Belousov #else 13837705dd4dSKonstantin Belousov return (false); 13847705dd4dSKonstantin Belousov #endif 13857705dd4dSKonstantin Belousov } 1386