2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1992 Terrence R. Lambert.
4 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 #include "opt_atpic.h"
51 #include "opt_kstack_pages.h"
52 #include "opt_maxmem.h"
53 #include "opt_mp_watchdog.h"
54 #include "opt_platform.h"
59 #include <sys/param.h>
61 #include <sys/systm.h>
64 #include <sys/domainset.h>
66 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #include <sys/mutex.h>
72 #include <sys/rwlock.h>
73 #include <sys/sched.h>
75 #include <sys/sysctl.h>
77 #include <machine/clock.h>
78 #include <machine/cpu.h>
79 #include <machine/cputypes.h>
80 #include <machine/specialreg.h>
81 #include <machine/md_var.h>
82 #include <machine/mp_watchdog.h>
83 #include <machine/tss.h>
85 #include <machine/smp.h>
88 #include <machine/elan_mmcr.h>
90 #include <x86/acpica_machdep.h>
93 #include <vm/vm_extern.h>
94 #include <vm/vm_kern.h>
95 #include <vm/vm_page.h>
96 #include <vm/vm_map.h>
97 #include <vm/vm_object.h>
98 #include <vm/vm_pager.h>
99 #include <vm/vm_param.h>
101 #include <isa/isareg.h>
103 #include <contrib/dev/acpica/include/acpi.h>
105 #define STATE_RUNNING 0x0
106 #define STATE_MWAIT 0x1
107 #define STATE_SLEEPING 0x2
110 static u_int cpu_reset_proxyid;
111 static volatile u_int cpu_reset_proxy_active;
121 x86_msr_op_one(void *argp)
123 struct msr_op_arg *a;
139 wrmsr(a->msr, a->arg1);
144 #define MSR_OP_EXMODE_MASK 0xf0000000
145 #define MSR_OP_OP_MASK 0x000000ff
148 x86_msr_op(u_int msr, u_int op, uint64_t arg1)
153 int bound_cpu, i, is_bound;
155 a.op = op & MSR_OP_OP_MASK;
156 MPASS(a.op == MSR_OP_ANDNOT || a.op == MSR_OP_OR ||
157 a.op == MSR_OP_WRITE);
158 exmode = op & MSR_OP_EXMODE_MASK;
159 MPASS(exmode == MSR_OP_LOCAL || exmode == MSR_OP_SCHED ||
160 exmode == MSR_OP_RENDEZVOUS);
170 is_bound = sched_is_bound(td);
171 bound_cpu = td->td_oncpu;
177 sched_bind(td, bound_cpu);
182 case MSR_OP_RENDEZVOUS:
183 smp_rendezvous(NULL, x86_msr_op_one, NULL, &a);
189 * Machine dependent boot() routine
191 * I haven't seen anything to put here yet
192 * Possibly some stuff might be grafted back here from boot()
200 * Flush the D-cache for non-DMA I/O so that the I-cache can
201 * be made coherent later.
204 cpu_flush_dcache(void *ptr, size_t len)
213 __asm __volatile("sti; hlt");
217 * Use mwait to pause execution while waiting for an interrupt or
218 * another thread to signal that there is more work.
220 * NOTE: Interrupts will cause a wakeup; however, this function does
221 * not enable interrupt handling. The caller is responsible to enable
225 acpi_cpu_idle_mwait(uint32_t mwait_hint)
231 * A comment in Linux patch claims that 'CPUs run faster with
232 * speculation protection disabled. All CPU threads in a core
233 * must disable speculation protection for it to be
234 * disabled. Disable it while we are idle so the other
235 * hyperthread can run fast.'
237 * XXXKIB. Software coordination mode should be supported,
238 * but all Intel CPUs provide hardware coordination.
241 state = (int *)PCPU_PTR(monitorbuf);
242 KASSERT(atomic_load_int(state) == STATE_SLEEPING,
243 ("cpu_mwait_cx: wrong monitorbuf state"));
244 atomic_store_int(state, STATE_MWAIT);
245 if (PCPU_GET(ibpb_set) || hw_ssb_active) {
246 v = rdmsr(MSR_IA32_SPEC_CTRL);
247 wrmsr(MSR_IA32_SPEC_CTRL, v & ~(IA32_SPEC_CTRL_IBRS |
248 IA32_SPEC_CTRL_STIBP | IA32_SPEC_CTRL_SSBD));
252 cpu_monitor(state, 0, 0);
253 if (atomic_load_int(state) == STATE_MWAIT)
254 cpu_mwait(MWAIT_INTRBREAK, mwait_hint);
257 * SSB cannot be disabled while we sleep, or rather, if it was
258 * disabled, the sysctl thread will bind to our cpu to tweak
262 wrmsr(MSR_IA32_SPEC_CTRL, v);
265 * We should exit on any event that interrupts mwait, because
266 * that event might be a wanted interrupt.
268 atomic_store_int(state, STATE_RUNNING);
271 /* Get current clock frequency for the given cpu id. */
273 cpu_est_clockrate(int cpu_id, uint64_t *rate)
276 uint64_t acnt, mcnt, perf;
279 if (pcpu_find(cpu_id) == NULL || rate == NULL)
282 if ((cpu_feature & CPUID_TSC) == 0)
287 * If TSC is P-state invariant and APERF/MPERF MSRs do not exist,
288 * DELAY(9) based logic fails.
290 if (tsc_is_invariant && !tsc_perf_stat)
295 /* Schedule ourselves on the indicated cpu. */
296 thread_lock(curthread);
297 sched_bind(curthread, cpu_id);
298 thread_unlock(curthread);
302 /* Calibrate by measuring a short delay. */
303 reg = intr_disable();
304 if (tsc_is_invariant) {
309 mcnt = rdmsr(MSR_MPERF);
310 acnt = rdmsr(MSR_APERF);
313 perf = 1000 * acnt / mcnt;
314 *rate = (tsc2 - tsc1) * perf;
320 *rate = (tsc2 - tsc1) * 1000;
325 thread_lock(curthread);
326 sched_unbind(curthread);
327 thread_unlock(curthread);
335 * Shutdown the CPU as much as possible
347 struct region_descriptor null_idt;
352 if (elan_mmcr != NULL)
353 elan_mmcr->RESCFG = 1;
356 if (cpu == CPU_GEODE1100) {
357 /* Attempt Geode's own reset */
358 outl(0xcf8, 0x80009044ul);
362 #if !defined(BROKEN_KEYBOARD_RESET)
364 * Attempt to do a CPU reset via the keyboard controller,
365 * do not turn off GateA20, as any machine that fails
366 * to do the reset here would then end up in no man's land.
368 outb(IO_KBD + 4, 0xFE);
369 DELAY(500000); /* wait 0.5 sec to see if that did it */
373 * Attempt to force a reset via the Reset Control register at
374 * I/O port 0xcf9. Bit 2 forces a system reset when it
375 * transitions from 0 to 1. Bit 1 selects the type of reset
376 * to attempt: 0 selects a "soft" reset, and 1 selects a
377 * "hard" reset. We try a "hard" reset. The first write sets
378 * bit 1 to select a "hard" reset and clears bit 2. The
379 * second write forces a 0 -> 1 transition in bit 2 to trigger
384 DELAY(500000); /* wait 0.5 sec to see if that did it */
387 * Attempt to force a reset via the Fast A20 and Init register
388 * at I/O port 0x92. Bit 1 serves as an alternate A20 gate.
389 * Bit 0 asserts INIT# when set to 1. We are careful to only
390 * preserve bit 1 while setting bit 0. We also must clear bit
391 * 0 before setting it if it isn't already clear.
396 outb(0x92, b & 0xfe);
398 DELAY(500000); /* wait 0.5 sec to see if that did it */
401 printf("No known reset method worked, attempting CPU shutdown\n");
402 DELAY(1000000); /* wait 1 sec for printf to complete */
405 null_idt.rd_limit = 0;
406 null_idt.rd_base = 0;
409 /* "good night, sweet prince .... <THUNK!>" */
418 cpu_reset_proxy(void)
421 cpu_reset_proxy_active = 1;
422 while (cpu_reset_proxy_active == 1)
423 ia32_pause(); /* Wait for other cpu to see that we've started */
425 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
440 CPU_CLR(PCPU_GET(cpuid), &map);
441 CPU_NAND(&map, &stopped_cpus);
442 if (!CPU_EMPTY(&map)) {
443 printf("cpu_reset: Stopping other CPUs\n");
447 if (PCPU_GET(cpuid) != 0) {
448 cpu_reset_proxyid = PCPU_GET(cpuid);
449 cpustop_restartfunc = cpu_reset_proxy;
450 cpu_reset_proxy_active = 0;
451 printf("cpu_reset: Restarting BSP\n");
453 /* Restart CPU #0. */
454 CPU_SETOF(0, &started_cpus);
457 while (cpu_reset_proxy_active == 0 && cnt < 10000000) {
459 cnt++; /* Wait for BSP to announce restart */
461 if (cpu_reset_proxy_active == 0) {
462 printf("cpu_reset: Failed to restart BSP\n");
464 cpu_reset_proxy_active = 2;
479 cpu_mwait_usable(void)
482 return ((cpu_feature2 & CPUID2_MON) != 0 && ((cpu_mon_mwait_flags &
483 (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)) ==
484 (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)));
487 void (*cpu_idle_hook)(sbintime_t) = NULL; /* ACPI idle hook. */
489 int cpu_amdc1e_bug = 0; /* AMD C1E APIC workaround required. */
491 static int idle_mwait = 1; /* Use MONITOR/MWAIT for short idle. */
492 SYSCTL_INT(_machdep, OID_AUTO, idle_mwait, CTLFLAG_RWTUN, &idle_mwait,
493 0, "Use MONITOR/MWAIT for short idle");
496 cpu_idle_acpi(sbintime_t sbt)
500 state = (int *)PCPU_PTR(monitorbuf);
501 atomic_store_int(state, STATE_SLEEPING);
503 /* See comments in cpu_idle_hlt(). */
505 if (sched_runnable())
507 else if (cpu_idle_hook)
511 atomic_store_int(state, STATE_RUNNING);
515 cpu_idle_hlt(sbintime_t sbt)
519 state = (int *)PCPU_PTR(monitorbuf);
520 atomic_store_int(state, STATE_SLEEPING);
523 * Since we may be in a critical section from cpu_idle(), if
524 * an interrupt fires during that critical section we may have
525 * a pending preemption. If the CPU halts, then that thread
526 * may not execute until a later interrupt awakens the CPU.
527 * To handle this race, check for a runnable thread after
528 * disabling interrupts and immediately return if one is
529 * found. Also, we must absolutely guarentee that hlt is
530 * the next instruction after sti. This ensures that any
531 * interrupt that fires after the call to disable_intr() will
532 * immediately awaken the CPU from hlt. Finally, please note
533 * that on x86 this works fine because of interrupts enabled only
534 * after the instruction following sti takes place, while IF is set
535 * to 1 immediately, allowing hlt instruction to acknowledge the
539 if (sched_runnable())
543 atomic_store_int(state, STATE_RUNNING);
547 cpu_idle_mwait(sbintime_t sbt)
551 state = (int *)PCPU_PTR(monitorbuf);
552 atomic_store_int(state, STATE_MWAIT);
554 /* See comments in cpu_idle_hlt(). */
556 if (sched_runnable()) {
557 atomic_store_int(state, STATE_RUNNING);
562 cpu_monitor(state, 0, 0);
563 if (atomic_load_int(state) == STATE_MWAIT)
564 __asm __volatile("sti; mwait" : : "a" (MWAIT_C1), "c" (0));
567 atomic_store_int(state, STATE_RUNNING);
571 cpu_idle_spin(sbintime_t sbt)
576 state = (int *)PCPU_PTR(monitorbuf);
577 atomic_store_int(state, STATE_RUNNING);
580 * The sched_runnable() call is racy but as long as there is
581 * a loop missing it one time will have just a little impact if any
582 * (and it is much better than missing the check at all).
584 for (i = 0; i < 1000; i++) {
585 if (sched_runnable())
591 void (*cpu_idle_fn)(sbintime_t) = cpu_idle_acpi;
599 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d",
602 ap_watchdog(PCPU_GET(cpuid));
605 /* If we are busy - try to use fast methods. */
607 if ((cpu_feature2 & CPUID2_MON) && idle_mwait) {
608 cpu_idle_mwait(busy);
613 /* If we have time - switch timers into idle mode. */
616 sbt = cpu_idleclock();
619 /* Apply AMD APIC timer C1E workaround. */
620 if (cpu_amdc1e_bug && cpu_disable_c3_sleep) {
621 msr = rdmsr(MSR_AMDK8_IPM);
622 if ((msr & (AMDK8_SMIONCMPHALT | AMDK8_C1EONCMPHALT)) != 0)
623 wrmsr(MSR_AMDK8_IPM, msr & ~(AMDK8_SMIONCMPHALT |
624 AMDK8_C1EONCMPHALT));
627 /* Call main idle method. */
630 /* Switch timers back into active mode. */
636 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done",
640 static int cpu_idle_apl31_workaround;
641 SYSCTL_INT(_machdep, OID_AUTO, idle_apl31, CTLFLAG_RW,
642 &cpu_idle_apl31_workaround, 0,
643 "Apollo Lake APL31 MWAIT bug workaround");
646 cpu_idle_wakeup(int cpu)
650 state = (int *)pcpu_find(cpu)->pc_monitorbuf;
651 switch (atomic_load_int(state)) {
655 atomic_store_int(state, STATE_RUNNING);
656 return (cpu_idle_apl31_workaround ? 0 : 1);
660 panic("bad monitor state");
666 * Ordered by speed/power consumption.
673 { .id_fn = cpu_idle_spin, .id_name = "spin" },
674 { .id_fn = cpu_idle_mwait, .id_name = "mwait",
675 .id_cpuid2_flag = CPUID2_MON },
676 { .id_fn = cpu_idle_hlt, .id_name = "hlt" },
677 { .id_fn = cpu_idle_acpi, .id_name = "acpi" },
681 idle_sysctl_available(SYSCTL_HANDLER_ARGS)
687 avail = malloc(256, M_TEMP, M_WAITOK);
689 for (i = 0; i < nitems(idle_tbl); i++) {
690 if (idle_tbl[i].id_cpuid2_flag != 0 &&
691 (cpu_feature2 & idle_tbl[i].id_cpuid2_flag) == 0)
693 if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
694 cpu_idle_hook == NULL)
696 p += sprintf(p, "%s%s", p != avail ? ", " : "",
697 idle_tbl[i].id_name);
699 error = sysctl_handle_string(oidp, avail, 0, req);
704 SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD,
705 0, 0, idle_sysctl_available, "A", "list of available idle functions");
708 cpu_idle_selector(const char *new_idle_name)
712 for (i = 0; i < nitems(idle_tbl); i++) {
713 if (idle_tbl[i].id_cpuid2_flag != 0 &&
714 (cpu_feature2 & idle_tbl[i].id_cpuid2_flag) == 0)
716 if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
717 cpu_idle_hook == NULL)
719 if (strcmp(idle_tbl[i].id_name, new_idle_name))
721 cpu_idle_fn = idle_tbl[i].id_fn;
723 printf("CPU idle set to %s\n", idle_tbl[i].id_name);
730 cpu_idle_sysctl(SYSCTL_HANDLER_ARGS)
736 for (i = 0; i < nitems(idle_tbl); i++) {
737 if (idle_tbl[i].id_fn == cpu_idle_fn) {
738 p = idle_tbl[i].id_name;
742 strncpy(buf, p, sizeof(buf));
743 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
744 if (error != 0 || req->newptr == NULL)
746 return (cpu_idle_selector(buf) ? 0 : EINVAL);
749 SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
750 cpu_idle_sysctl, "A", "currently selected idle function");
753 cpu_idle_tun(void *unused __unused)
757 if (TUNABLE_STR_FETCH("machdep.idle", tunvar, sizeof(tunvar)))
758 cpu_idle_selector(tunvar);
759 else if (cpu_vendor_id == CPU_VENDOR_AMD &&
760 CPUID_TO_FAMILY(cpu_id) == 0x17 && CPUID_TO_MODEL(cpu_id) == 0x1) {
761 /* Ryzen erratas 1057, 1109. */
762 cpu_idle_selector("hlt");
766 if (cpu_vendor_id == CPU_VENDOR_INTEL && cpu_id == 0x506c9) {
768 * Apollo Lake errata APL31 (public errata APL30).
769 * Stores to the armed address range may not trigger
770 * MWAIT to resume execution. OS needs to use
771 * interrupts to wake processors from MWAIT-induced
774 cpu_idle_apl31_workaround = 1;
776 TUNABLE_INT_FETCH("machdep.idle_apl31", &cpu_idle_apl31_workaround);
778 SYSINIT(cpu_idle_tun, SI_SUB_CPU, SI_ORDER_MIDDLE, cpu_idle_tun, NULL);
780 static int panic_on_nmi = 0xff;
781 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RWTUN,
783 "Panic on NMI: 1 = H/W failure; 2 = unknown; 0xff = all");
784 int nmi_is_broadcast = 1;
785 SYSCTL_INT(_machdep, OID_AUTO, nmi_is_broadcast, CTLFLAG_RWTUN,
786 &nmi_is_broadcast, 0,
787 "Chipset NMI is broadcast");
788 int (*apei_nmi)(void);
791 nmi_call_kdb(u_int cpu, u_int type, struct trapframe *frame)
793 bool claimed = false;
796 /* machine/parity/power fail/"kitchen sink" faults */
797 if (isa_nmi(frame->tf_err)) {
799 if ((panic_on_nmi & 1) != 0)
800 panic("NMI indicates hardware failure");
804 /* ACPI Platform Error Interfaces callback. */
805 if (apei_nmi != NULL && (*apei_nmi)())
809 * NMIs can be useful for debugging. They can be hooked up to a
810 * pushbutton, usually on an ISA, PCI, or PCIe card. They can also be
811 * generated by an IPMI BMC, either manually or in response to a
812 * watchdog timeout. For example, see the "power diag" command in
813 * ports/sysutils/ipmitool. They can also be generated by a
814 * hypervisor; see "bhyvectl --inject-nmi".
818 if (!claimed && (panic_on_nmi & 2) != 0) {
819 if (debugger_on_panic) {
820 printf("NMI/cpu%d ... going to debugger\n", cpu);
821 claimed = kdb_trap(type, 0, frame);
826 if (!claimed && panic_on_nmi != 0)
831 nmi_handle_intr(u_int type, struct trapframe *frame)
835 if (nmi_is_broadcast) {
836 nmi_call_kdb_smp(type, frame);
840 nmi_call_kdb(PCPU_GET(cpuid), type, frame);
843 static int hw_ibrs_active;
844 int hw_ibrs_ibpb_active;
845 int hw_ibrs_disable = 1;
847 SYSCTL_INT(_hw, OID_AUTO, ibrs_active, CTLFLAG_RD, &hw_ibrs_active, 0,
848 "Indirect Branch Restricted Speculation active");
851 hw_ibrs_recalculate(bool for_all_cpus)
853 if ((cpu_ia32_arch_caps & IA32_ARCH_CAP_IBRS_ALL) != 0) {
854 x86_msr_op(MSR_IA32_SPEC_CTRL, (for_all_cpus ?
855 MSR_OP_RENDEZVOUS : MSR_OP_LOCAL) |
856 (hw_ibrs_disable != 0 ? MSR_OP_ANDNOT : MSR_OP_OR),
857 IA32_SPEC_CTRL_IBRS);
858 hw_ibrs_active = hw_ibrs_disable == 0;
859 hw_ibrs_ibpb_active = 0;
861 hw_ibrs_active = hw_ibrs_ibpb_active = (cpu_stdext_feature3 &
862 CPUID_STDEXT3_IBPB) != 0 && !hw_ibrs_disable;
867 hw_ibrs_disable_handler(SYSCTL_HANDLER_ARGS)
871 val = hw_ibrs_disable;
872 error = sysctl_handle_int(oidp, &val, 0, req);
873 if (error != 0 || req->newptr == NULL)
875 hw_ibrs_disable = val != 0;
876 hw_ibrs_recalculate(true);
879 SYSCTL_PROC(_hw, OID_AUTO, ibrs_disable, CTLTYPE_INT | CTLFLAG_RWTUN |
880 CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0, hw_ibrs_disable_handler, "I",
881 "Disable Indirect Branch Restricted Speculation");
886 SYSCTL_INT(_hw, OID_AUTO, spec_store_bypass_disable_active, CTLFLAG_RD,
888 "Speculative Store Bypass Disable active");
891 hw_ssb_set(bool enable, bool for_all_cpus)
894 if ((cpu_stdext_feature3 & CPUID_STDEXT3_SSBD) == 0) {
898 hw_ssb_active = enable;
899 x86_msr_op(MSR_IA32_SPEC_CTRL,
900 (enable ? MSR_OP_OR : MSR_OP_ANDNOT) |
901 (for_all_cpus ? MSR_OP_SCHED : MSR_OP_LOCAL), IA32_SPEC_CTRL_SSBD);
905 hw_ssb_recalculate(bool all_cpus)
908 switch (hw_ssb_disable) {
913 hw_ssb_set(false, all_cpus);
916 hw_ssb_set(true, all_cpus);
919 hw_ssb_set((cpu_ia32_arch_caps & IA32_ARCH_CAP_SSB_NO) != 0 ?
920 false : true, all_cpus);
926 hw_ssb_disable_handler(SYSCTL_HANDLER_ARGS)
930 val = hw_ssb_disable;
931 error = sysctl_handle_int(oidp, &val, 0, req);
932 if (error != 0 || req->newptr == NULL)
934 hw_ssb_disable = val;
935 hw_ssb_recalculate(true);
938 SYSCTL_PROC(_hw, OID_AUTO, spec_store_bypass_disable, CTLTYPE_INT |
939 CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
940 hw_ssb_disable_handler, "I",
941 "Speculative Store Bypass Disable (0 - off, 1 - on, 2 - auto");
946 * Handler for Microarchitectural Data Sampling issues. Really not a
947 * pointer to C function: on amd64 the code must not change any CPU
948 * architectural state except possibly %rflags. Also, it is always
949 * called with interrupts disabled.
951 void mds_handler_void(void);
952 void mds_handler_verw(void);
953 void mds_handler_ivb(void);
954 void mds_handler_bdw(void);
955 void mds_handler_skl_sse(void);
956 void mds_handler_skl_avx(void);
957 void mds_handler_skl_avx512(void);
958 void mds_handler_silvermont(void);
959 void (*mds_handler)(void) = mds_handler_void;
962 sysctl_hw_mds_disable_state_handler(SYSCTL_HANDLER_ARGS)
966 if (mds_handler == mds_handler_void)
968 else if (mds_handler == mds_handler_verw)
970 else if (mds_handler == mds_handler_ivb)
971 state = "software IvyBridge";
972 else if (mds_handler == mds_handler_bdw)
973 state = "software Broadwell";
974 else if (mds_handler == mds_handler_skl_sse)
975 state = "software Skylake SSE";
976 else if (mds_handler == mds_handler_skl_avx)
977 state = "software Skylake AVX";
978 else if (mds_handler == mds_handler_skl_avx512)
979 state = "software Skylake AVX512";
980 else if (mds_handler == mds_handler_silvermont)
981 state = "software Silvermont";
984 return (SYSCTL_OUT(req, state, strlen(state)));
987 SYSCTL_PROC(_hw, OID_AUTO, mds_disable_state,
988 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
989 sysctl_hw_mds_disable_state_handler, "A",
990 "Microarchitectural Data Sampling Mitigation state");
992 _Static_assert(__offsetof(struct pcpu, pc_mds_tmp) % 64 == 0, "MDS AVX512");
995 hw_mds_recalculate(void)
1003 * Allow user to force VERW variant even if MD_CLEAR is not
1004 * reported. For instance, hypervisor might unknowingly
1005 * filter the cap out.
1006 * For the similar reasons, and for testing, allow to enable
1007 * mitigation even when MDS_NO cap is set.
1009 if (cpu_vendor_id != CPU_VENDOR_INTEL || hw_mds_disable == 0 ||
1010 ((cpu_ia32_arch_caps & IA32_ARCH_CAP_MDS_NO) != 0 &&
1011 hw_mds_disable == 3)) {
1012 mds_handler = mds_handler_void;
1013 } else if (((cpu_stdext_feature3 & CPUID_STDEXT3_MD_CLEAR) != 0 &&
1014 hw_mds_disable == 3) || hw_mds_disable == 1) {
1015 mds_handler = mds_handler_verw;
1016 } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
1017 (CPUID_TO_MODEL(cpu_id) == 0x2e || CPUID_TO_MODEL(cpu_id) == 0x1e ||
1018 CPUID_TO_MODEL(cpu_id) == 0x1f || CPUID_TO_MODEL(cpu_id) == 0x1a ||
1019 CPUID_TO_MODEL(cpu_id) == 0x2f || CPUID_TO_MODEL(cpu_id) == 0x25 ||
1020 CPUID_TO_MODEL(cpu_id) == 0x2c || CPUID_TO_MODEL(cpu_id) == 0x2d ||
1021 CPUID_TO_MODEL(cpu_id) == 0x2a || CPUID_TO_MODEL(cpu_id) == 0x3e ||
1022 CPUID_TO_MODEL(cpu_id) == 0x3a) &&
1023 (hw_mds_disable == 2 || hw_mds_disable == 3)) {
1025 * Nehalem, SandyBridge, IvyBridge
1029 if (pc->pc_mds_buf == NULL) {
1030 pc->pc_mds_buf = malloc_domainset(672, M_TEMP,
1031 DOMAINSET_PREF(pc->pc_domain), M_WAITOK);
1032 bzero(pc->pc_mds_buf, 16);
1035 mds_handler = mds_handler_ivb;
1036 } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
1037 (CPUID_TO_MODEL(cpu_id) == 0x3f || CPUID_TO_MODEL(cpu_id) == 0x3c ||
1038 CPUID_TO_MODEL(cpu_id) == 0x45 || CPUID_TO_MODEL(cpu_id) == 0x46 ||
1039 CPUID_TO_MODEL(cpu_id) == 0x56 || CPUID_TO_MODEL(cpu_id) == 0x4f ||
1040 CPUID_TO_MODEL(cpu_id) == 0x47 || CPUID_TO_MODEL(cpu_id) == 0x3d) &&
1041 (hw_mds_disable == 2 || hw_mds_disable == 3)) {
1043 * Haswell, Broadwell
1047 if (pc->pc_mds_buf == NULL) {
1048 pc->pc_mds_buf = malloc_domainset(1536, M_TEMP,
1049 DOMAINSET_PREF(pc->pc_domain), M_WAITOK);
1050 bzero(pc->pc_mds_buf, 16);
1053 mds_handler = mds_handler_bdw;
1054 } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
1055 ((CPUID_TO_MODEL(cpu_id) == 0x55 && (cpu_id &
1056 CPUID_STEPPING) <= 5) ||
1057 CPUID_TO_MODEL(cpu_id) == 0x4e || CPUID_TO_MODEL(cpu_id) == 0x5e ||
1058 (CPUID_TO_MODEL(cpu_id) == 0x8e && (cpu_id &
1059 CPUID_STEPPING) <= 0xb) ||
1060 (CPUID_TO_MODEL(cpu_id) == 0x9e && (cpu_id &
1061 CPUID_STEPPING) <= 0xc)) &&
1062 (hw_mds_disable == 2 || hw_mds_disable == 3)) {
1064 * Skylake, KabyLake, CoffeeLake, WhiskeyLake,
1069 if (pc->pc_mds_buf == NULL) {
1070 pc->pc_mds_buf = malloc_domainset(6 * 1024,
1071 M_TEMP, DOMAINSET_PREF(pc->pc_domain),
1073 b64 = (vm_offset_t)malloc_domainset(64 + 63,
1074 M_TEMP, DOMAINSET_PREF(pc->pc_domain),
1076 pc->pc_mds_buf64 = (void *)roundup2(b64, 64);
1077 bzero(pc->pc_mds_buf64, 64);
1081 if ((xcr0 & XFEATURE_ENABLED_ZMM_HI256) != 0 &&
1082 (cpu_stdext_feature2 & CPUID_STDEXT_AVX512DQ) != 0)
1083 mds_handler = mds_handler_skl_avx512;
1084 else if ((xcr0 & XFEATURE_ENABLED_AVX) != 0 &&
1085 (cpu_feature2 & CPUID2_AVX) != 0)
1086 mds_handler = mds_handler_skl_avx;
1088 mds_handler = mds_handler_skl_sse;
1089 } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
1090 ((CPUID_TO_MODEL(cpu_id) == 0x37 ||
1091 CPUID_TO_MODEL(cpu_id) == 0x4a ||
1092 CPUID_TO_MODEL(cpu_id) == 0x4c ||
1093 CPUID_TO_MODEL(cpu_id) == 0x4d ||
1094 CPUID_TO_MODEL(cpu_id) == 0x5a ||
1095 CPUID_TO_MODEL(cpu_id) == 0x5d ||
1096 CPUID_TO_MODEL(cpu_id) == 0x6e ||
1097 CPUID_TO_MODEL(cpu_id) == 0x65 ||
1098 CPUID_TO_MODEL(cpu_id) == 0x75 ||
1099 CPUID_TO_MODEL(cpu_id) == 0x1c ||
1100 CPUID_TO_MODEL(cpu_id) == 0x26 ||
1101 CPUID_TO_MODEL(cpu_id) == 0x27 ||
1102 CPUID_TO_MODEL(cpu_id) == 0x35 ||
1103 CPUID_TO_MODEL(cpu_id) == 0x36 ||
1104 CPUID_TO_MODEL(cpu_id) == 0x7a))) {
1105 /* Silvermont, Airmont */
1108 if (pc->pc_mds_buf == NULL)
1109 pc->pc_mds_buf = malloc(256, M_TEMP, M_WAITOK);
1111 mds_handler = mds_handler_silvermont;
1114 mds_handler = mds_handler_void;
1119 hw_mds_recalculate_boot(void *arg __unused)
1122 hw_mds_recalculate();
1124 SYSINIT(mds_recalc, SI_SUB_SMP, SI_ORDER_ANY, hw_mds_recalculate_boot, NULL);
1127 sysctl_mds_disable_handler(SYSCTL_HANDLER_ARGS)
1131 val = hw_mds_disable;
1132 error = sysctl_handle_int(oidp, &val, 0, req);
1133 if (error != 0 || req->newptr == NULL)
1135 if (val < 0 || val > 3)
1137 hw_mds_disable = val;
1138 hw_mds_recalculate();
1142 SYSCTL_PROC(_hw, OID_AUTO, mds_disable, CTLTYPE_INT |
1143 CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
1144 sysctl_mds_disable_handler, "I",
1145 "Microarchitectural Data Sampling Mitigation "
1146 "(0 - off, 1 - on VERW, 2 - on SW, 3 - on AUTO");
1150 * Intel Transactional Memory Asynchronous Abort Mitigation
1156 TAA_NONE = 0, /* No mitigation enabled */
1157 TAA_TSX_DISABLE = 1, /* Disable TSX via MSR */
1158 TAA_VERW = 2, /* Use VERW mitigation */
1159 TAA_AUTO = 3, /* Automatically select the mitigation */
1161 /* The states below are not selectable by the operator */
1163 TAA_TAA_UC = 4, /* Mitigation present in microcode */
1164 TAA_NOT_PRESENT = 5 /* TSX is not present */
1168 taa_set(bool enable, bool all)
1171 x86_msr_op(MSR_IA32_TSX_CTRL,
1172 (enable ? MSR_OP_OR : MSR_OP_ANDNOT) |
1173 (all ? MSR_OP_RENDEZVOUS : MSR_OP_LOCAL),
1174 IA32_TSX_CTRL_RTM_DISABLE | IA32_TSX_CTRL_TSX_CPUID_CLEAR);
1178 x86_taa_recalculate(void)
1180 static int taa_saved_mds_disable = 0;
1181 int taa_need = 0, taa_state = 0;
1182 int mds_disable = 0, need_mds_recalc = 0;
1184 /* Check CPUID.07h.EBX.HLE and RTM for the presence of TSX */
1185 if ((cpu_stdext_feature & CPUID_STDEXT_HLE) == 0 ||
1186 (cpu_stdext_feature & CPUID_STDEXT_RTM) == 0) {
1187 /* TSX is not present */
1188 x86_taa_state = TAA_NOT_PRESENT;
1192 /* Check to see what mitigation options the CPU gives us */
1193 if (cpu_ia32_arch_caps & IA32_ARCH_CAP_TAA_NO) {
1194 /* CPU is not suseptible to TAA */
1195 taa_need = TAA_TAA_UC;
1196 } else if (cpu_ia32_arch_caps & IA32_ARCH_CAP_TSX_CTRL) {
1198 * CPU can turn off TSX. This is the next best option
1199 * if TAA_NO hardware mitigation isn't present
1201 taa_need = TAA_TSX_DISABLE;
1203 /* No TSX/TAA specific remedies are available. */
1204 if (x86_taa_enable == TAA_TSX_DISABLE) {
1206 printf("TSX control not available\n");
1209 taa_need = TAA_VERW;
1212 /* Can we automatically take action, or are we being forced? */
1213 if (x86_taa_enable == TAA_AUTO)
1214 taa_state = taa_need;
1216 taa_state = x86_taa_enable;
1218 /* No state change, nothing to do */
1219 if (taa_state == x86_taa_state) {
1221 printf("No TSX change made\n");
1225 /* Does the MSR need to be turned on or off? */
1226 if (taa_state == TAA_TSX_DISABLE)
1227 taa_set(true, true);
1228 else if (x86_taa_state == TAA_TSX_DISABLE)
1229 taa_set(false, true);
1231 /* Does MDS need to be set to turn on VERW? */
1232 if (taa_state == TAA_VERW) {
1233 taa_saved_mds_disable = hw_mds_disable;
1234 mds_disable = hw_mds_disable = 1;
1235 need_mds_recalc = 1;
1236 } else if (x86_taa_state == TAA_VERW) {
1237 mds_disable = hw_mds_disable = taa_saved_mds_disable;
1238 need_mds_recalc = 1;
1240 if (need_mds_recalc) {
1241 hw_mds_recalculate();
1242 if (mds_disable != hw_mds_disable) {
1244 printf("Cannot change MDS state for TAA\n");
1245 /* Don't update our state */
1250 x86_taa_state = taa_state;
1255 taa_recalculate_boot(void * arg __unused)
1258 x86_taa_recalculate();
1260 SYSINIT(taa_recalc, SI_SUB_SMP, SI_ORDER_ANY, taa_recalculate_boot, NULL);
1262 SYSCTL_NODE(_machdep_mitigations, OID_AUTO, taa, CTLFLAG_RW, 0,
1263 "TSX Asynchronous Abort Mitigation");
1266 sysctl_taa_handler(SYSCTL_HANDLER_ARGS)
1270 val = x86_taa_enable;
1271 error = sysctl_handle_int(oidp, &val, 0, req);
1272 if (error != 0 || req->newptr == NULL)
1274 if (val < TAA_NONE || val > TAA_AUTO)
1276 x86_taa_enable = val;
1277 x86_taa_recalculate();
1281 SYSCTL_PROC(_machdep_mitigations_taa, OID_AUTO, enable, CTLTYPE_INT |
1282 CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
1283 sysctl_taa_handler, "I",
1284 "TAA Mitigation enablement control "
1285 "(0 - off, 1 - disable TSX, 2 - VERW, 3 - on AUTO");
1288 sysctl_taa_state_handler(SYSCTL_HANDLER_ARGS)
1292 switch (x86_taa_state) {
1296 case TAA_TSX_DISABLE:
1297 state = "TSX disabled";
1303 state = "Mitigated in microcode";
1305 case TAA_NOT_PRESENT:
1306 state = "TSX not present";
1312 return (SYSCTL_OUT(req, state, strlen(state)));
1315 SYSCTL_PROC(_machdep_mitigations_taa, OID_AUTO, state,
1316 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1317 sysctl_taa_state_handler, "A",
1318 "TAA Mitigation state");
1320 int __read_frequently cpu_flush_rsb_ctxsw;
1321 SYSCTL_INT(_machdep_mitigations, OID_AUTO, flush_rsb_ctxsw,
1322 CTLFLAG_RW | CTLFLAG_NOFETCH, &cpu_flush_rsb_ctxsw, 0,
1323 "Flush Return Stack Buffer on context switch");
1325 SYSCTL_NODE(_machdep_mitigations, OID_AUTO, rngds,
1326 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1327 "MCU Optimization, disable RDSEED mitigation");
1329 int x86_rngds_mitg_enable = 1;
1331 x86_rngds_mitg_recalculate(bool all_cpus)
1333 if ((cpu_stdext_feature3 & CPUID_STDEXT3_MCUOPT) == 0)
1335 x86_msr_op(MSR_IA32_MCU_OPT_CTRL,
1336 (x86_rngds_mitg_enable ? MSR_OP_OR : MSR_OP_ANDNOT) |
1337 (all_cpus ? MSR_OP_RENDEZVOUS : MSR_OP_LOCAL),
1338 IA32_RNGDS_MITG_DIS);
1342 sysctl_rngds_mitg_enable_handler(SYSCTL_HANDLER_ARGS)
1346 val = x86_rngds_mitg_enable;
1347 error = sysctl_handle_int(oidp, &val, 0, req);
1348 if (error != 0 || req->newptr == NULL)
1350 x86_rngds_mitg_enable = val;
1351 x86_rngds_mitg_recalculate(true);
1354 SYSCTL_PROC(_machdep_mitigations_rngds, OID_AUTO, enable, CTLTYPE_INT |
1355 CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
1356 sysctl_rngds_mitg_enable_handler, "I",
1357 "MCU Optimization, disabling RDSEED mitigation control "
1358 "(0 - mitigation disabled (RDSEED optimized), 1 - mitigation enabled");
1361 sysctl_rngds_state_handler(SYSCTL_HANDLER_ARGS)
1365 if ((cpu_stdext_feature3 & CPUID_STDEXT3_MCUOPT) == 0) {
1366 state = "Not applicable";
1367 } else if (x86_rngds_mitg_enable == 0) {
1368 state = "RDSEED not serialized";
1370 state = "Mitigated";
1372 return (SYSCTL_OUT(req, state, strlen(state)));
1374 SYSCTL_PROC(_machdep_mitigations_rngds, OID_AUTO, state,
1375 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1376 sysctl_rngds_state_handler, "A",
1377 "MCU Optimization state");
1380 * Enable and restore kernel text write permissions.
1381 * Callers must ensure that disable_wp()/restore_wp() are executed
1382 * without rescheduling on the same core.
1390 if ((cr0 & CR0_WP) == 0)
1392 load_cr0(cr0 & ~CR0_WP);
1397 restore_wp(bool old_wp)
1401 load_cr0(rcr0() | CR0_WP);
1405 acpi_get_fadt_bootflags(uint16_t *flagsp)
1408 ACPI_TABLE_FADT *fadt;
1409 vm_paddr_t physaddr;
1411 physaddr = acpi_find_table(ACPI_SIG_FADT);
1414 fadt = acpi_map_table(physaddr, ACPI_SIG_FADT);
1417 *flagsp = fadt->BootFlags;
1418 acpi_unmap_table(fadt);