xref: /freebsd/sys/x86/x86/cpu_machdep.c (revision 149b9c234b00ff902718e1f76c809609a5465198)
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>
427705dd4dSKonstantin Belousov #include "opt_acpi.h"
43dfe7b3bfSKonstantin Belousov #include "opt_atpic.h"
44dfe7b3bfSKonstantin Belousov #include "opt_cpu.h"
45dfe7b3bfSKonstantin Belousov #include "opt_ddb.h"
46dfe7b3bfSKonstantin Belousov #include "opt_inet.h"
47dfe7b3bfSKonstantin Belousov #include "opt_isa.h"
48835c2787SKonstantin Belousov #include "opt_kdb.h"
49dfe7b3bfSKonstantin Belousov #include "opt_kstack_pages.h"
50dfe7b3bfSKonstantin Belousov #include "opt_maxmem.h"
51dfe7b3bfSKonstantin Belousov #include "opt_platform.h"
5203f868b1SMark Johnston #include "opt_sched.h"
53dfe7b3bfSKonstantin Belousov #ifdef __i386__
54dfe7b3bfSKonstantin Belousov #include "opt_apic.h"
55dfe7b3bfSKonstantin Belousov #endif
56dfe7b3bfSKonstantin Belousov 
57dfe7b3bfSKonstantin Belousov #include <sys/param.h>
58dfe7b3bfSKonstantin Belousov #include <sys/proc.h>
59dfe7b3bfSKonstantin Belousov #include <sys/systm.h>
60dfe7b3bfSKonstantin Belousov #include <sys/bus.h>
61dfe7b3bfSKonstantin Belousov #include <sys/cpu.h>
627355a02bSKonstantin Belousov #include <sys/domainset.h>
63dfe7b3bfSKonstantin Belousov #include <sys/kdb.h>
64dfe7b3bfSKonstantin Belousov #include <sys/kernel.h>
65dfe7b3bfSKonstantin Belousov #include <sys/ktr.h>
66dfe7b3bfSKonstantin Belousov #include <sys/lock.h>
67dfe7b3bfSKonstantin Belousov #include <sys/malloc.h>
68dfe7b3bfSKonstantin Belousov #include <sys/mutex.h>
69dfe7b3bfSKonstantin Belousov #include <sys/pcpu.h>
70dfe7b3bfSKonstantin Belousov #include <sys/rwlock.h>
71dfe7b3bfSKonstantin Belousov #include <sys/sched.h>
72dfe7b3bfSKonstantin Belousov #include <sys/smp.h>
73dfe7b3bfSKonstantin Belousov #include <sys/sysctl.h>
74dfe7b3bfSKonstantin Belousov 
75dfe7b3bfSKonstantin Belousov #include <machine/clock.h>
76dfe7b3bfSKonstantin Belousov #include <machine/cpu.h>
77652ae7b1SAdam Fenn #include <machine/cpufunc.h>
78dfe7b3bfSKonstantin Belousov #include <machine/cputypes.h>
79dfe7b3bfSKonstantin Belousov #include <machine/specialreg.h>
80dfe7b3bfSKonstantin Belousov #include <machine/md_var.h>
81dfe7b3bfSKonstantin Belousov #include <machine/tss.h>
82dfe7b3bfSKonstantin Belousov #ifdef SMP
83dfe7b3bfSKonstantin Belousov #include <machine/smp.h>
84dfe7b3bfSKonstantin Belousov #endif
853da25bdbSAndriy Gapon #ifdef CPU_ELAN
863da25bdbSAndriy Gapon #include <machine/elan_mmcr.h>
873da25bdbSAndriy Gapon #endif
88b57a73f8SKonstantin Belousov #include <x86/acpica_machdep.h>
89652ae7b1SAdam Fenn #include <x86/ifunc.h>
90dfe7b3bfSKonstantin Belousov 
91dfe7b3bfSKonstantin Belousov #include <vm/vm.h>
92dfe7b3bfSKonstantin Belousov #include <vm/vm_extern.h>
93dfe7b3bfSKonstantin Belousov #include <vm/vm_kern.h>
94dfe7b3bfSKonstantin Belousov #include <vm/vm_page.h>
95dfe7b3bfSKonstantin Belousov #include <vm/vm_map.h>
96dfe7b3bfSKonstantin Belousov #include <vm/vm_object.h>
97dfe7b3bfSKonstantin Belousov #include <vm/vm_pager.h>
98dfe7b3bfSKonstantin Belousov #include <vm/vm_param.h>
99dfe7b3bfSKonstantin Belousov 
1008428d0f1SAndriy Gapon #include <isa/isareg.h>
1018428d0f1SAndriy Gapon 
1027705dd4dSKonstantin Belousov #include <contrib/dev/acpica/include/acpi.h>
1037705dd4dSKonstantin Belousov 
104d9e8bbb6SKonstantin Belousov #define	STATE_RUNNING	0x0
105d9e8bbb6SKonstantin Belousov #define	STATE_MWAIT	0x1
106d9e8bbb6SKonstantin Belousov #define	STATE_SLEEPING	0x2
107d9e8bbb6SKonstantin Belousov 
1088428d0f1SAndriy Gapon #ifdef SMP
1098428d0f1SAndriy Gapon static u_int	cpu_reset_proxyid;
1108428d0f1SAndriy Gapon static volatile u_int	cpu_reset_proxy_active;
1118428d0f1SAndriy Gapon #endif
1128428d0f1SAndriy Gapon 
113a2495c36SRoger Pau Monné char bootmethod[16];
114a2495c36SRoger Pau Monné SYSCTL_STRING(_machdep, OID_AUTO, bootmethod, CTLFLAG_RD, bootmethod, 0,
115a2495c36SRoger Pau Monné     "System firmware boot method");
116a2495c36SRoger Pau Monné 
117fa83f689SKonstantin Belousov struct msr_op_arg {
118fa83f689SKonstantin Belousov 	u_int msr;
119fa83f689SKonstantin Belousov 	int op;
120fa83f689SKonstantin Belousov 	uint64_t arg1;
121d0bc4b46SKonstantin Belousov 	uint64_t *res;
122fa83f689SKonstantin Belousov };
123fa83f689SKonstantin Belousov 
124fa83f689SKonstantin Belousov static void
125fa83f689SKonstantin Belousov x86_msr_op_one(void *argp)
126fa83f689SKonstantin Belousov {
127fa83f689SKonstantin Belousov 	struct msr_op_arg *a;
128fa83f689SKonstantin Belousov 	uint64_t v;
129fa83f689SKonstantin Belousov 
130fa83f689SKonstantin Belousov 	a = argp;
131fa83f689SKonstantin Belousov 	switch (a->op) {
132fa83f689SKonstantin Belousov 	case MSR_OP_ANDNOT:
133fa83f689SKonstantin Belousov 		v = rdmsr(a->msr);
134fa83f689SKonstantin Belousov 		v &= ~a->arg1;
135fa83f689SKonstantin Belousov 		wrmsr(a->msr, v);
136fa83f689SKonstantin Belousov 		break;
137fa83f689SKonstantin Belousov 	case MSR_OP_OR:
138fa83f689SKonstantin Belousov 		v = rdmsr(a->msr);
139fa83f689SKonstantin Belousov 		v |= a->arg1;
140fa83f689SKonstantin Belousov 		wrmsr(a->msr, v);
141fa83f689SKonstantin Belousov 		break;
142fa83f689SKonstantin Belousov 	case MSR_OP_WRITE:
143fa83f689SKonstantin Belousov 		wrmsr(a->msr, a->arg1);
144fa83f689SKonstantin Belousov 		break;
145d0bc4b46SKonstantin Belousov 	case MSR_OP_READ:
146d0bc4b46SKonstantin Belousov 		v = rdmsr(a->msr);
147d0bc4b46SKonstantin Belousov 		*a->res = v;
148d0bc4b46SKonstantin Belousov 		break;
149fa83f689SKonstantin Belousov 	}
150fa83f689SKonstantin Belousov }
151fa83f689SKonstantin Belousov 
152fa83f689SKonstantin Belousov #define	MSR_OP_EXMODE_MASK	0xf0000000
153fa83f689SKonstantin Belousov #define	MSR_OP_OP_MASK		0x000000ff
154d0bc4b46SKonstantin Belousov #define	MSR_OP_GET_CPUID(x)	(((x) & ~MSR_OP_EXMODE_MASK) >> 8)
155fa83f689SKonstantin Belousov 
156fa83f689SKonstantin Belousov void
157d0bc4b46SKonstantin Belousov x86_msr_op(u_int msr, u_int op, uint64_t arg1, uint64_t *res)
158fa83f689SKonstantin Belousov {
159fa83f689SKonstantin Belousov 	struct thread *td;
160fa83f689SKonstantin Belousov 	struct msr_op_arg a;
161d0bc4b46SKonstantin Belousov 	cpuset_t set;
162fa83f689SKonstantin Belousov 	u_int exmode;
163d0bc4b46SKonstantin Belousov 	int bound_cpu, cpu, i, is_bound;
164fa83f689SKonstantin Belousov 
165fa83f689SKonstantin Belousov 	a.op = op & MSR_OP_OP_MASK;
166fa83f689SKonstantin Belousov 	MPASS(a.op == MSR_OP_ANDNOT || a.op == MSR_OP_OR ||
167d0bc4b46SKonstantin Belousov 	    a.op == MSR_OP_WRITE || a.op == MSR_OP_READ);
168fa83f689SKonstantin Belousov 	exmode = op & MSR_OP_EXMODE_MASK;
169d0bc4b46SKonstantin Belousov 	MPASS(exmode == MSR_OP_LOCAL || exmode == MSR_OP_SCHED_ALL ||
170d0bc4b46SKonstantin Belousov 	    exmode == MSR_OP_SCHED_ONE || exmode == MSR_OP_RENDEZVOUS_ALL ||
171d0bc4b46SKonstantin Belousov 	    exmode == MSR_OP_RENDEZVOUS_ONE);
172fa83f689SKonstantin Belousov 	a.msr = msr;
173fa83f689SKonstantin Belousov 	a.arg1 = arg1;
174d0bc4b46SKonstantin Belousov 	a.res = res;
175fa83f689SKonstantin Belousov 	switch (exmode) {
176fa83f689SKonstantin Belousov 	case MSR_OP_LOCAL:
177fa83f689SKonstantin Belousov 		x86_msr_op_one(&a);
178fa83f689SKonstantin Belousov 		break;
179d0bc4b46SKonstantin Belousov 	case MSR_OP_SCHED_ALL:
180fa83f689SKonstantin Belousov 		td = curthread;
181fa83f689SKonstantin Belousov 		thread_lock(td);
182fa83f689SKonstantin Belousov 		is_bound = sched_is_bound(td);
183fa83f689SKonstantin Belousov 		bound_cpu = td->td_oncpu;
184fa83f689SKonstantin Belousov 		CPU_FOREACH(i) {
185fa83f689SKonstantin Belousov 			sched_bind(td, i);
186fa83f689SKonstantin Belousov 			x86_msr_op_one(&a);
187fa83f689SKonstantin Belousov 		}
188fa83f689SKonstantin Belousov 		if (is_bound)
189fa83f689SKonstantin Belousov 			sched_bind(td, bound_cpu);
190fa83f689SKonstantin Belousov 		else
191fa83f689SKonstantin Belousov 			sched_unbind(td);
192fa83f689SKonstantin Belousov 		thread_unlock(td);
193fa83f689SKonstantin Belousov 		break;
194d0bc4b46SKonstantin Belousov 	case MSR_OP_SCHED_ONE:
195d0bc4b46SKonstantin Belousov 		td = curthread;
196d0bc4b46SKonstantin Belousov 		cpu = MSR_OP_GET_CPUID(op);
197d0bc4b46SKonstantin Belousov 		thread_lock(td);
198d0bc4b46SKonstantin Belousov 		is_bound = sched_is_bound(td);
199d0bc4b46SKonstantin Belousov 		bound_cpu = td->td_oncpu;
200d0bc4b46SKonstantin Belousov 		if (!is_bound || bound_cpu != cpu)
201d0bc4b46SKonstantin Belousov 			sched_bind(td, cpu);
202d0bc4b46SKonstantin Belousov 		x86_msr_op_one(&a);
203d0bc4b46SKonstantin Belousov 		if (is_bound) {
204d0bc4b46SKonstantin Belousov 			if (bound_cpu != cpu)
205d0bc4b46SKonstantin Belousov 				sched_bind(td, bound_cpu);
206d0bc4b46SKonstantin Belousov 		} else {
207d0bc4b46SKonstantin Belousov 			sched_unbind(td);
208d0bc4b46SKonstantin Belousov 		}
209d0bc4b46SKonstantin Belousov 		thread_unlock(td);
210d0bc4b46SKonstantin Belousov 		break;
211d0bc4b46SKonstantin Belousov 	case MSR_OP_RENDEZVOUS_ALL:
212d0bc4b46SKonstantin Belousov 		smp_rendezvous(smp_no_rendezvous_barrier, x86_msr_op_one,
213d0bc4b46SKonstantin Belousov 		    smp_no_rendezvous_barrier, &a);
214d0bc4b46SKonstantin Belousov 		break;
215d0bc4b46SKonstantin Belousov 	case MSR_OP_RENDEZVOUS_ONE:
216d0bc4b46SKonstantin Belousov 		cpu = MSR_OP_GET_CPUID(op);
217d0bc4b46SKonstantin Belousov 		CPU_SETOF(cpu, &set);
218d0bc4b46SKonstantin Belousov 		smp_rendezvous_cpus(set, smp_no_rendezvous_barrier,
219d0bc4b46SKonstantin Belousov 		    x86_msr_op_one, smp_no_rendezvous_barrier, &a);
220fa83f689SKonstantin Belousov 		break;
221fa83f689SKonstantin Belousov 	}
222fa83f689SKonstantin Belousov }
223fa83f689SKonstantin Belousov 
224665919aaSConrad Meyer /*
225665919aaSConrad Meyer  * Automatically initialized per CPU errata in cpu_idle_tun below.
226665919aaSConrad Meyer  */
227665919aaSConrad Meyer bool mwait_cpustop_broken = false;
228665919aaSConrad Meyer SYSCTL_BOOL(_machdep, OID_AUTO, mwait_cpustop_broken, CTLFLAG_RDTUN,
229665919aaSConrad Meyer     &mwait_cpustop_broken, 0,
230665919aaSConrad Meyer     "Can not reliably wake MONITOR/MWAIT cpus without interrupts");
2318428d0f1SAndriy Gapon 
232dfe7b3bfSKonstantin Belousov /*
233dfe7b3bfSKonstantin Belousov  * Flush the D-cache for non-DMA I/O so that the I-cache can
234dfe7b3bfSKonstantin Belousov  * be made coherent later.
235dfe7b3bfSKonstantin Belousov  */
236dfe7b3bfSKonstantin Belousov void
237dfe7b3bfSKonstantin Belousov cpu_flush_dcache(void *ptr, size_t len)
238dfe7b3bfSKonstantin Belousov {
239dfe7b3bfSKonstantin Belousov 	/* Not applicable */
240dfe7b3bfSKonstantin Belousov }
241dfe7b3bfSKonstantin Belousov 
242b57a73f8SKonstantin Belousov void
243b57a73f8SKonstantin Belousov acpi_cpu_c1(void)
244b57a73f8SKonstantin Belousov {
245b57a73f8SKonstantin Belousov 
246b57a73f8SKonstantin Belousov 	__asm __volatile("sti; hlt");
247b57a73f8SKonstantin Belousov }
248b57a73f8SKonstantin Belousov 
24919d4720bSJonathan T. Looney /*
25019d4720bSJonathan T. Looney  * Use mwait to pause execution while waiting for an interrupt or
25119d4720bSJonathan T. Looney  * another thread to signal that there is more work.
25219d4720bSJonathan T. Looney  *
25319d4720bSJonathan T. Looney  * NOTE: Interrupts will cause a wakeup; however, this function does
25419d4720bSJonathan T. Looney  * not enable interrupt handling. The caller is responsible to enable
25519d4720bSJonathan T. Looney  * interrupts.
25619d4720bSJonathan T. Looney  */
257b57a73f8SKonstantin Belousov void
258b57a73f8SKonstantin Belousov acpi_cpu_idle_mwait(uint32_t mwait_hint)
259b57a73f8SKonstantin Belousov {
260b57a73f8SKonstantin Belousov 	int *state;
2613621ba1eSKonstantin Belousov 	uint64_t v;
262b57a73f8SKonstantin Belousov 
263b57a73f8SKonstantin Belousov 	/*
264319117fdSKonstantin Belousov 	 * A comment in Linux patch claims that 'CPUs run faster with
265319117fdSKonstantin Belousov 	 * speculation protection disabled. All CPU threads in a core
266319117fdSKonstantin Belousov 	 * must disable speculation protection for it to be
267319117fdSKonstantin Belousov 	 * disabled. Disable it while we are idle so the other
268319117fdSKonstantin Belousov 	 * hyperthread can run fast.'
269319117fdSKonstantin Belousov 	 *
270b57a73f8SKonstantin Belousov 	 * XXXKIB.  Software coordination mode should be supported,
271b57a73f8SKonstantin Belousov 	 * but all Intel CPUs provide hardware coordination.
272b57a73f8SKonstantin Belousov 	 */
273d9e8bbb6SKonstantin Belousov 
27483dc49beSConrad Meyer 	state = &PCPU_PTR(monitorbuf)->idle_state;
275a5bd21d0SKonstantin Belousov 	KASSERT(atomic_load_int(state) == STATE_SLEEPING,
276d9e8bbb6SKonstantin Belousov 	    ("cpu_mwait_cx: wrong monitorbuf state"));
277a5bd21d0SKonstantin Belousov 	atomic_store_int(state, STATE_MWAIT);
2783621ba1eSKonstantin Belousov 	if (PCPU_GET(ibpb_set) || hw_ssb_active) {
2793621ba1eSKonstantin Belousov 		v = rdmsr(MSR_IA32_SPEC_CTRL);
2803621ba1eSKonstantin Belousov 		wrmsr(MSR_IA32_SPEC_CTRL, v & ~(IA32_SPEC_CTRL_IBRS |
2813621ba1eSKonstantin Belousov 		    IA32_SPEC_CTRL_STIBP | IA32_SPEC_CTRL_SSBD));
2823621ba1eSKonstantin Belousov 	} else {
2833621ba1eSKonstantin Belousov 		v = 0;
2843621ba1eSKonstantin Belousov 	}
285b57a73f8SKonstantin Belousov 	cpu_monitor(state, 0, 0);
286a5bd21d0SKonstantin Belousov 	if (atomic_load_int(state) == STATE_MWAIT)
287b57a73f8SKonstantin Belousov 		cpu_mwait(MWAIT_INTRBREAK, mwait_hint);
2883621ba1eSKonstantin Belousov 
2893621ba1eSKonstantin Belousov 	/*
2903621ba1eSKonstantin Belousov 	 * SSB cannot be disabled while we sleep, or rather, if it was
2913621ba1eSKonstantin Belousov 	 * disabled, the sysctl thread will bind to our cpu to tweak
2923621ba1eSKonstantin Belousov 	 * MSR.
2933621ba1eSKonstantin Belousov 	 */
2943621ba1eSKonstantin Belousov 	if (v != 0)
2953621ba1eSKonstantin Belousov 		wrmsr(MSR_IA32_SPEC_CTRL, v);
296d9e8bbb6SKonstantin Belousov 
297d9e8bbb6SKonstantin Belousov 	/*
298d9e8bbb6SKonstantin Belousov 	 * We should exit on any event that interrupts mwait, because
299d9e8bbb6SKonstantin Belousov 	 * that event might be a wanted interrupt.
300d9e8bbb6SKonstantin Belousov 	 */
301a5bd21d0SKonstantin Belousov 	atomic_store_int(state, STATE_RUNNING);
302b57a73f8SKonstantin Belousov }
303b57a73f8SKonstantin Belousov 
304dfe7b3bfSKonstantin Belousov /* Get current clock frequency for the given cpu id. */
305dfe7b3bfSKonstantin Belousov int
306dfe7b3bfSKonstantin Belousov cpu_est_clockrate(int cpu_id, uint64_t *rate)
307dfe7b3bfSKonstantin Belousov {
308dfe7b3bfSKonstantin Belousov 	uint64_t tsc1, tsc2;
309dfe7b3bfSKonstantin Belousov 	uint64_t acnt, mcnt, perf;
310dfe7b3bfSKonstantin Belousov 	register_t reg;
311dfe7b3bfSKonstantin Belousov 
312dfe7b3bfSKonstantin Belousov 	if (pcpu_find(cpu_id) == NULL || rate == NULL)
313dfe7b3bfSKonstantin Belousov 		return (EINVAL);
314dfe7b3bfSKonstantin Belousov #ifdef __i386__
315dfe7b3bfSKonstantin Belousov 	if ((cpu_feature & CPUID_TSC) == 0)
316dfe7b3bfSKonstantin Belousov 		return (EOPNOTSUPP);
317dfe7b3bfSKonstantin Belousov #endif
318dfe7b3bfSKonstantin Belousov 
319dfe7b3bfSKonstantin Belousov 	/*
320dfe7b3bfSKonstantin Belousov 	 * If TSC is P-state invariant and APERF/MPERF MSRs do not exist,
321dfe7b3bfSKonstantin Belousov 	 * DELAY(9) based logic fails.
322dfe7b3bfSKonstantin Belousov 	 */
323dfe7b3bfSKonstantin Belousov 	if (tsc_is_invariant && !tsc_perf_stat)
324dfe7b3bfSKonstantin Belousov 		return (EOPNOTSUPP);
325dfe7b3bfSKonstantin Belousov 
326dfe7b3bfSKonstantin Belousov #ifdef SMP
327dfe7b3bfSKonstantin Belousov 	if (smp_cpus > 1) {
328dfe7b3bfSKonstantin Belousov 		/* Schedule ourselves on the indicated cpu. */
329dfe7b3bfSKonstantin Belousov 		thread_lock(curthread);
330dfe7b3bfSKonstantin Belousov 		sched_bind(curthread, cpu_id);
331dfe7b3bfSKonstantin Belousov 		thread_unlock(curthread);
332dfe7b3bfSKonstantin Belousov 	}
333dfe7b3bfSKonstantin Belousov #endif
334dfe7b3bfSKonstantin Belousov 
335dfe7b3bfSKonstantin Belousov 	/* Calibrate by measuring a short delay. */
336dfe7b3bfSKonstantin Belousov 	reg = intr_disable();
337dfe7b3bfSKonstantin Belousov 	if (tsc_is_invariant) {
338dfe7b3bfSKonstantin Belousov 		wrmsr(MSR_MPERF, 0);
339dfe7b3bfSKonstantin Belousov 		wrmsr(MSR_APERF, 0);
340dfe7b3bfSKonstantin Belousov 		tsc1 = rdtsc();
341dfe7b3bfSKonstantin Belousov 		DELAY(1000);
342dfe7b3bfSKonstantin Belousov 		mcnt = rdmsr(MSR_MPERF);
343dfe7b3bfSKonstantin Belousov 		acnt = rdmsr(MSR_APERF);
344dfe7b3bfSKonstantin Belousov 		tsc2 = rdtsc();
345dfe7b3bfSKonstantin Belousov 		intr_restore(reg);
346dfe7b3bfSKonstantin Belousov 		perf = 1000 * acnt / mcnt;
347dfe7b3bfSKonstantin Belousov 		*rate = (tsc2 - tsc1) * perf;
348dfe7b3bfSKonstantin Belousov 	} else {
349dfe7b3bfSKonstantin Belousov 		tsc1 = rdtsc();
350dfe7b3bfSKonstantin Belousov 		DELAY(1000);
351dfe7b3bfSKonstantin Belousov 		tsc2 = rdtsc();
352dfe7b3bfSKonstantin Belousov 		intr_restore(reg);
353dfe7b3bfSKonstantin Belousov 		*rate = (tsc2 - tsc1) * 1000;
354dfe7b3bfSKonstantin Belousov 	}
355dfe7b3bfSKonstantin Belousov 
356dfe7b3bfSKonstantin Belousov #ifdef SMP
357dfe7b3bfSKonstantin Belousov 	if (smp_cpus > 1) {
358dfe7b3bfSKonstantin Belousov 		thread_lock(curthread);
359dfe7b3bfSKonstantin Belousov 		sched_unbind(curthread);
360dfe7b3bfSKonstantin Belousov 		thread_unlock(curthread);
361dfe7b3bfSKonstantin Belousov 	}
362dfe7b3bfSKonstantin Belousov #endif
363dfe7b3bfSKonstantin Belousov 
364dfe7b3bfSKonstantin Belousov 	return (0);
365dfe7b3bfSKonstantin Belousov }
366dfe7b3bfSKonstantin Belousov 
367dfe7b3bfSKonstantin Belousov /*
368dfe7b3bfSKonstantin Belousov  * Shutdown the CPU as much as possible
369dfe7b3bfSKonstantin Belousov  */
370dfe7b3bfSKonstantin Belousov void
371dfe7b3bfSKonstantin Belousov cpu_halt(void)
372dfe7b3bfSKonstantin Belousov {
373dfe7b3bfSKonstantin Belousov 	for (;;)
374dfe7b3bfSKonstantin Belousov 		halt();
375dfe7b3bfSKonstantin Belousov }
376dfe7b3bfSKonstantin Belousov 
3778428d0f1SAndriy Gapon static void
378b7b25af0SAndriy Gapon cpu_reset_real(void)
3798428d0f1SAndriy Gapon {
3808428d0f1SAndriy Gapon 	struct region_descriptor null_idt;
3818428d0f1SAndriy Gapon 	int b;
3828428d0f1SAndriy Gapon 
3838428d0f1SAndriy Gapon 	disable_intr();
3848428d0f1SAndriy Gapon #ifdef CPU_ELAN
3858428d0f1SAndriy Gapon 	if (elan_mmcr != NULL)
3868428d0f1SAndriy Gapon 		elan_mmcr->RESCFG = 1;
3878428d0f1SAndriy Gapon #endif
3888428d0f1SAndriy Gapon #ifdef __i386__
3898428d0f1SAndriy Gapon 	if (cpu == CPU_GEODE1100) {
3908428d0f1SAndriy Gapon 		/* Attempt Geode's own reset */
3918428d0f1SAndriy Gapon 		outl(0xcf8, 0x80009044ul);
3928428d0f1SAndriy Gapon 		outl(0xcfc, 0xf);
3938428d0f1SAndriy Gapon 	}
3948428d0f1SAndriy Gapon #endif
3958428d0f1SAndriy Gapon #if !defined(BROKEN_KEYBOARD_RESET)
3968428d0f1SAndriy Gapon 	/*
3978428d0f1SAndriy Gapon 	 * Attempt to do a CPU reset via the keyboard controller,
3988428d0f1SAndriy Gapon 	 * do not turn off GateA20, as any machine that fails
3998428d0f1SAndriy Gapon 	 * to do the reset here would then end up in no man's land.
4008428d0f1SAndriy Gapon 	 */
4018428d0f1SAndriy Gapon 	outb(IO_KBD + 4, 0xFE);
4028428d0f1SAndriy Gapon 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
4038428d0f1SAndriy Gapon #endif
4048428d0f1SAndriy Gapon 
4058428d0f1SAndriy Gapon 	/*
4068428d0f1SAndriy Gapon 	 * Attempt to force a reset via the Reset Control register at
4078428d0f1SAndriy Gapon 	 * I/O port 0xcf9.  Bit 2 forces a system reset when it
4088428d0f1SAndriy Gapon 	 * transitions from 0 to 1.  Bit 1 selects the type of reset
4098428d0f1SAndriy Gapon 	 * to attempt: 0 selects a "soft" reset, and 1 selects a
4108428d0f1SAndriy Gapon 	 * "hard" reset.  We try a "hard" reset.  The first write sets
4118428d0f1SAndriy Gapon 	 * bit 1 to select a "hard" reset and clears bit 2.  The
4128428d0f1SAndriy Gapon 	 * second write forces a 0 -> 1 transition in bit 2 to trigger
4138428d0f1SAndriy Gapon 	 * a reset.
4148428d0f1SAndriy Gapon 	 */
4158428d0f1SAndriy Gapon 	outb(0xcf9, 0x2);
4168428d0f1SAndriy Gapon 	outb(0xcf9, 0x6);
4178428d0f1SAndriy Gapon 	DELAY(500000);  /* wait 0.5 sec to see if that did it */
4188428d0f1SAndriy Gapon 
4198428d0f1SAndriy Gapon 	/*
4208428d0f1SAndriy Gapon 	 * Attempt to force a reset via the Fast A20 and Init register
4218428d0f1SAndriy Gapon 	 * at I/O port 0x92.  Bit 1 serves as an alternate A20 gate.
4228428d0f1SAndriy Gapon 	 * Bit 0 asserts INIT# when set to 1.  We are careful to only
4238428d0f1SAndriy Gapon 	 * preserve bit 1 while setting bit 0.  We also must clear bit
4248428d0f1SAndriy Gapon 	 * 0 before setting it if it isn't already clear.
4258428d0f1SAndriy Gapon 	 */
4268428d0f1SAndriy Gapon 	b = inb(0x92);
4278428d0f1SAndriy Gapon 	if (b != 0xff) {
4288428d0f1SAndriy Gapon 		if ((b & 0x1) != 0)
4298428d0f1SAndriy Gapon 			outb(0x92, b & 0xfe);
4308428d0f1SAndriy Gapon 		outb(0x92, b | 0x1);
4318428d0f1SAndriy Gapon 		DELAY(500000);  /* wait 0.5 sec to see if that did it */
4328428d0f1SAndriy Gapon 	}
4338428d0f1SAndriy Gapon 
4348428d0f1SAndriy Gapon 	printf("No known reset method worked, attempting CPU shutdown\n");
4358428d0f1SAndriy Gapon 	DELAY(1000000); /* wait 1 sec for printf to complete */
4368428d0f1SAndriy Gapon 
4378428d0f1SAndriy Gapon 	/* Wipe the IDT. */
4388428d0f1SAndriy Gapon 	null_idt.rd_limit = 0;
4398428d0f1SAndriy Gapon 	null_idt.rd_base = 0;
4408428d0f1SAndriy Gapon 	lidt(&null_idt);
4418428d0f1SAndriy Gapon 
4428428d0f1SAndriy Gapon 	/* "good night, sweet prince .... <THUNK!>" */
4438428d0f1SAndriy Gapon 	breakpoint();
4448428d0f1SAndriy Gapon 
4458428d0f1SAndriy Gapon 	/* NOTREACHED */
4468428d0f1SAndriy Gapon 	while(1);
4478428d0f1SAndriy Gapon }
4488428d0f1SAndriy Gapon 
4498428d0f1SAndriy Gapon #ifdef SMP
4508428d0f1SAndriy Gapon static void
451b7b25af0SAndriy Gapon cpu_reset_proxy(void)
4528428d0f1SAndriy Gapon {
4538428d0f1SAndriy Gapon 
4548428d0f1SAndriy Gapon 	cpu_reset_proxy_active = 1;
4558428d0f1SAndriy Gapon 	while (cpu_reset_proxy_active == 1)
4568428d0f1SAndriy Gapon 		ia32_pause(); /* Wait for other cpu to see that we've started */
4578428d0f1SAndriy Gapon 
4588428d0f1SAndriy Gapon 	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
4598428d0f1SAndriy Gapon 	DELAY(1000000);
4608428d0f1SAndriy Gapon 	cpu_reset_real();
4618428d0f1SAndriy Gapon }
4628428d0f1SAndriy Gapon #endif
4638428d0f1SAndriy Gapon 
4648428d0f1SAndriy Gapon void
465b7b25af0SAndriy Gapon cpu_reset(void)
4668428d0f1SAndriy Gapon {
4678428d0f1SAndriy Gapon #ifdef SMP
468665919aaSConrad Meyer 	struct monitorbuf *mb;
4698428d0f1SAndriy Gapon 	cpuset_t map;
4708428d0f1SAndriy Gapon 	u_int cnt;
4718428d0f1SAndriy Gapon 
4728428d0f1SAndriy Gapon 	if (smp_started) {
4738428d0f1SAndriy Gapon 		map = all_cpus;
4748428d0f1SAndriy Gapon 		CPU_CLR(PCPU_GET(cpuid), &map);
475e2650af1SStefan Eßer 		CPU_ANDNOT(&map, &map, &stopped_cpus);
4768428d0f1SAndriy Gapon 		if (!CPU_EMPTY(&map)) {
4778428d0f1SAndriy Gapon 			printf("cpu_reset: Stopping other CPUs\n");
4788428d0f1SAndriy Gapon 			stop_cpus(map);
4798428d0f1SAndriy Gapon 		}
4808428d0f1SAndriy Gapon 
4818428d0f1SAndriy Gapon 		if (PCPU_GET(cpuid) != 0) {
4828428d0f1SAndriy Gapon 			cpu_reset_proxyid = PCPU_GET(cpuid);
4838428d0f1SAndriy Gapon 			cpustop_restartfunc = cpu_reset_proxy;
4848428d0f1SAndriy Gapon 			cpu_reset_proxy_active = 0;
4858428d0f1SAndriy Gapon 			printf("cpu_reset: Restarting BSP\n");
4868428d0f1SAndriy Gapon 
4878428d0f1SAndriy Gapon 			/* Restart CPU #0. */
4888428d0f1SAndriy Gapon 			CPU_SETOF(0, &started_cpus);
489665919aaSConrad Meyer 			mb = &pcpu_find(0)->pc_monitorbuf;
490665919aaSConrad Meyer 			atomic_store_int(&mb->stop_state,
491665919aaSConrad Meyer 			    MONITOR_STOPSTATE_RUNNING);
4928428d0f1SAndriy Gapon 
4938428d0f1SAndriy Gapon 			cnt = 0;
4948428d0f1SAndriy Gapon 			while (cpu_reset_proxy_active == 0 && cnt < 10000000) {
4958428d0f1SAndriy Gapon 				ia32_pause();
4968428d0f1SAndriy Gapon 				cnt++;	/* Wait for BSP to announce restart */
4978428d0f1SAndriy Gapon 			}
4988428d0f1SAndriy Gapon 			if (cpu_reset_proxy_active == 0) {
4998428d0f1SAndriy Gapon 				printf("cpu_reset: Failed to restart BSP\n");
5008428d0f1SAndriy Gapon 			} else {
5018428d0f1SAndriy Gapon 				cpu_reset_proxy_active = 2;
5028428d0f1SAndriy Gapon 				while (1)
5038428d0f1SAndriy Gapon 					ia32_pause();
5048428d0f1SAndriy Gapon 				/* NOTREACHED */
5058428d0f1SAndriy Gapon 			}
5068428d0f1SAndriy Gapon 		}
5078428d0f1SAndriy Gapon 	}
5088428d0f1SAndriy Gapon #endif
5098428d0f1SAndriy Gapon 	cpu_reset_real();
5108428d0f1SAndriy Gapon 	/* NOTREACHED */
5118428d0f1SAndriy Gapon }
5128428d0f1SAndriy Gapon 
513b57a73f8SKonstantin Belousov bool
514b57a73f8SKonstantin Belousov cpu_mwait_usable(void)
515b57a73f8SKonstantin Belousov {
516b57a73f8SKonstantin Belousov 
517b57a73f8SKonstantin Belousov 	return ((cpu_feature2 & CPUID2_MON) != 0 && ((cpu_mon_mwait_flags &
518b57a73f8SKonstantin Belousov 	    (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)) ==
519b57a73f8SKonstantin Belousov 	    (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)));
520b57a73f8SKonstantin Belousov }
521b57a73f8SKonstantin Belousov 
522dfe7b3bfSKonstantin Belousov void (*cpu_idle_hook)(sbintime_t) = NULL;	/* ACPI idle hook. */
523d3ba71b2SKonstantin Belousov 
524d3ba71b2SKonstantin Belousov int cpu_amdc1e_bug = 0;			/* AMD C1E APIC workaround required. */
525d3ba71b2SKonstantin Belousov 
526dfe7b3bfSKonstantin Belousov static int	idle_mwait = 1;		/* Use MONITOR/MWAIT for short idle. */
527dfe7b3bfSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, idle_mwait, CTLFLAG_RWTUN, &idle_mwait,
528dfe7b3bfSKonstantin Belousov     0, "Use MONITOR/MWAIT for short idle");
529dfe7b3bfSKonstantin Belousov 
53003f868b1SMark Johnston static bool
53103f868b1SMark Johnston cpu_idle_enter(int *statep, int newstate)
532dfe7b3bfSKonstantin Belousov {
53303f868b1SMark Johnston 	KASSERT(atomic_load_int(statep) == STATE_RUNNING,
53403f868b1SMark Johnston 	    ("%s: state %d", __func__, atomic_load_int(statep)));
535dfe7b3bfSKonstantin Belousov 
53603f868b1SMark Johnston 	/*
53703f868b1SMark Johnston 	 * A fence is needed to prevent reordering of the load in
53803f868b1SMark Johnston 	 * sched_runnable() with this store to the idle state word.  Without it,
53903f868b1SMark Johnston 	 * cpu_idle_wakeup() can observe the state as STATE_RUNNING after having
54003f868b1SMark Johnston 	 * added load to the queue, and elide an IPI.  Then, sched_runnable()
54103f868b1SMark Johnston 	 * can observe tdq_load == 0, so the CPU ends up idling with pending
54203f868b1SMark Johnston 	 * work.  tdq_notify() similarly ensures that a prior update to tdq_load
54303f868b1SMark Johnston 	 * is visible before calling cpu_idle_wakeup().
54403f868b1SMark Johnston 	 */
54503f868b1SMark Johnston 	atomic_store_int(statep, newstate);
54603f868b1SMark Johnston #if defined(SCHED_ULE) && defined(SMP)
54703f868b1SMark Johnston 	atomic_thread_fence_seq_cst();
54803f868b1SMark Johnston #endif
549dfe7b3bfSKonstantin Belousov 
550dfe7b3bfSKonstantin Belousov 	/*
551dfe7b3bfSKonstantin Belousov 	 * Since we may be in a critical section from cpu_idle(), if
552dfe7b3bfSKonstantin Belousov 	 * an interrupt fires during that critical section we may have
553dfe7b3bfSKonstantin Belousov 	 * a pending preemption.  If the CPU halts, then that thread
554dfe7b3bfSKonstantin Belousov 	 * may not execute until a later interrupt awakens the CPU.
555dfe7b3bfSKonstantin Belousov 	 * To handle this race, check for a runnable thread after
556dfe7b3bfSKonstantin Belousov 	 * disabling interrupts and immediately return if one is
557dfe7b3bfSKonstantin Belousov 	 * found.  Also, we must absolutely guarentee that hlt is
558dfe7b3bfSKonstantin Belousov 	 * the next instruction after sti.  This ensures that any
559dfe7b3bfSKonstantin Belousov 	 * interrupt that fires after the call to disable_intr() will
560dfe7b3bfSKonstantin Belousov 	 * immediately awaken the CPU from hlt.  Finally, please note
561dfe7b3bfSKonstantin Belousov 	 * that on x86 this works fine because of interrupts enabled only
562dfe7b3bfSKonstantin Belousov 	 * after the instruction following sti takes place, while IF is set
563dfe7b3bfSKonstantin Belousov 	 * to 1 immediately, allowing hlt instruction to acknowledge the
564dfe7b3bfSKonstantin Belousov 	 * interrupt.
565dfe7b3bfSKonstantin Belousov 	 */
566dfe7b3bfSKonstantin Belousov 	disable_intr();
56703f868b1SMark Johnston 	if (sched_runnable()) {
568dfe7b3bfSKonstantin Belousov 		enable_intr();
56903f868b1SMark Johnston 		atomic_store_int(statep, STATE_RUNNING);
57003f868b1SMark Johnston 		return (false);
57103f868b1SMark Johnston 	} else {
57203f868b1SMark Johnston 		return (true);
57303f868b1SMark Johnston 	}
57403f868b1SMark Johnston }
57503f868b1SMark Johnston 
57603f868b1SMark Johnston static void
57703f868b1SMark Johnston cpu_idle_exit(int *statep)
57803f868b1SMark Johnston {
57903f868b1SMark Johnston 	atomic_store_int(statep, STATE_RUNNING);
58003f868b1SMark Johnston }
58103f868b1SMark Johnston 
58203f868b1SMark Johnston static void
58303f868b1SMark Johnston cpu_idle_acpi(sbintime_t sbt)
58403f868b1SMark Johnston {
58503f868b1SMark Johnston 	int *state;
58603f868b1SMark Johnston 
58703f868b1SMark Johnston 	state = &PCPU_PTR(monitorbuf)->idle_state;
58803f868b1SMark Johnston 	if (cpu_idle_enter(state, STATE_SLEEPING)) {
58903f868b1SMark Johnston 		if (cpu_idle_hook)
59003f868b1SMark Johnston 			cpu_idle_hook(sbt);
591dfe7b3bfSKonstantin Belousov 		else
592b57a73f8SKonstantin Belousov 			acpi_cpu_c1();
59303f868b1SMark Johnston 		cpu_idle_exit(state);
59403f868b1SMark Johnston 	}
59503f868b1SMark Johnston }
59603f868b1SMark Johnston 
59703f868b1SMark Johnston static void
59803f868b1SMark Johnston cpu_idle_hlt(sbintime_t sbt)
59903f868b1SMark Johnston {
60003f868b1SMark Johnston 	int *state;
60103f868b1SMark Johnston 
60203f868b1SMark Johnston 	state = &PCPU_PTR(monitorbuf)->idle_state;
60303f868b1SMark Johnston 	if (cpu_idle_enter(state, STATE_SLEEPING)) {
60403f868b1SMark Johnston 		acpi_cpu_c1();
605a5bd21d0SKonstantin Belousov 		atomic_store_int(state, STATE_RUNNING);
606dfe7b3bfSKonstantin Belousov 	}
60703f868b1SMark Johnston }
608dfe7b3bfSKonstantin Belousov 
609dfe7b3bfSKonstantin Belousov static void
610dfe7b3bfSKonstantin Belousov cpu_idle_mwait(sbintime_t sbt)
611dfe7b3bfSKonstantin Belousov {
612dfe7b3bfSKonstantin Belousov 	int *state;
613dfe7b3bfSKonstantin Belousov 
61483dc49beSConrad Meyer 	state = &PCPU_PTR(monitorbuf)->idle_state;
61503f868b1SMark Johnston 	if (cpu_idle_enter(state, STATE_MWAIT)) {
616dfe7b3bfSKonstantin Belousov 		cpu_monitor(state, 0, 0);
617a5bd21d0SKonstantin Belousov 		if (atomic_load_int(state) == STATE_MWAIT)
618dfe7b3bfSKonstantin Belousov 			__asm __volatile("sti; mwait" : : "a" (MWAIT_C1), "c" (0));
619dfe7b3bfSKonstantin Belousov 		else
620dfe7b3bfSKonstantin Belousov 			enable_intr();
62103f868b1SMark Johnston 		cpu_idle_exit(state);
62203f868b1SMark Johnston 	}
623dfe7b3bfSKonstantin Belousov }
624dfe7b3bfSKonstantin Belousov 
625dfe7b3bfSKonstantin Belousov static void
626dfe7b3bfSKonstantin Belousov cpu_idle_spin(sbintime_t sbt)
627dfe7b3bfSKonstantin Belousov {
628dfe7b3bfSKonstantin Belousov 	int *state;
629dfe7b3bfSKonstantin Belousov 	int i;
630dfe7b3bfSKonstantin Belousov 
63183dc49beSConrad Meyer 	state = &PCPU_PTR(monitorbuf)->idle_state;
632a5bd21d0SKonstantin Belousov 	atomic_store_int(state, STATE_RUNNING);
633dfe7b3bfSKonstantin Belousov 
634dfe7b3bfSKonstantin Belousov 	/*
635dfe7b3bfSKonstantin Belousov 	 * The sched_runnable() call is racy but as long as there is
636dfe7b3bfSKonstantin Belousov 	 * a loop missing it one time will have just a little impact if any
637dfe7b3bfSKonstantin Belousov 	 * (and it is much better than missing the check at all).
638dfe7b3bfSKonstantin Belousov 	 */
639dfe7b3bfSKonstantin Belousov 	for (i = 0; i < 1000; i++) {
640dfe7b3bfSKonstantin Belousov 		if (sched_runnable())
641dfe7b3bfSKonstantin Belousov 			return;
642dfe7b3bfSKonstantin Belousov 		cpu_spinwait();
643dfe7b3bfSKonstantin Belousov 	}
644dfe7b3bfSKonstantin Belousov }
645dfe7b3bfSKonstantin Belousov 
646dfe7b3bfSKonstantin Belousov void (*cpu_idle_fn)(sbintime_t) = cpu_idle_acpi;
647dfe7b3bfSKonstantin Belousov 
648dfe7b3bfSKonstantin Belousov void
649dfe7b3bfSKonstantin Belousov cpu_idle(int busy)
650dfe7b3bfSKonstantin Belousov {
651dfe7b3bfSKonstantin Belousov 	uint64_t msr;
652dfe7b3bfSKonstantin Belousov 	sbintime_t sbt = -1;
653dfe7b3bfSKonstantin Belousov 
654ece453d5SMark Johnston 	CTR1(KTR_SPARE2, "cpu_idle(%d)", busy);
655ed95805eSJohn Baldwin 
656dfe7b3bfSKonstantin Belousov 	/* If we are busy - try to use fast methods. */
657dfe7b3bfSKonstantin Belousov 	if (busy) {
658dfe7b3bfSKonstantin Belousov 		if ((cpu_feature2 & CPUID2_MON) && idle_mwait) {
659dfe7b3bfSKonstantin Belousov 			cpu_idle_mwait(busy);
660dfe7b3bfSKonstantin Belousov 			goto out;
661dfe7b3bfSKonstantin Belousov 		}
662dfe7b3bfSKonstantin Belousov 	}
663dfe7b3bfSKonstantin Belousov 
664dfe7b3bfSKonstantin Belousov 	/* If we have time - switch timers into idle mode. */
665dfe7b3bfSKonstantin Belousov 	if (!busy) {
666dfe7b3bfSKonstantin Belousov 		critical_enter();
667dfe7b3bfSKonstantin Belousov 		sbt = cpu_idleclock();
668dfe7b3bfSKonstantin Belousov 	}
669dfe7b3bfSKonstantin Belousov 
670dfe7b3bfSKonstantin Belousov 	/* Apply AMD APIC timer C1E workaround. */
671d3ba71b2SKonstantin Belousov 	if (cpu_amdc1e_bug && cpu_disable_c3_sleep) {
672dfe7b3bfSKonstantin Belousov 		msr = rdmsr(MSR_AMDK8_IPM);
673d3ba71b2SKonstantin Belousov 		if ((msr & (AMDK8_SMIONCMPHALT | AMDK8_C1EONCMPHALT)) != 0)
674d3ba71b2SKonstantin Belousov 			wrmsr(MSR_AMDK8_IPM, msr & ~(AMDK8_SMIONCMPHALT |
675d3ba71b2SKonstantin Belousov 			    AMDK8_C1EONCMPHALT));
676dfe7b3bfSKonstantin Belousov 	}
677dfe7b3bfSKonstantin Belousov 
678dfe7b3bfSKonstantin Belousov 	/* Call main idle method. */
679dfe7b3bfSKonstantin Belousov 	cpu_idle_fn(sbt);
680dfe7b3bfSKonstantin Belousov 
681dfe7b3bfSKonstantin Belousov 	/* Switch timers back into active mode. */
682dfe7b3bfSKonstantin Belousov 	if (!busy) {
683dfe7b3bfSKonstantin Belousov 		cpu_activeclock();
684dfe7b3bfSKonstantin Belousov 		critical_exit();
685dfe7b3bfSKonstantin Belousov 	}
686dfe7b3bfSKonstantin Belousov out:
687ece453d5SMark Johnston 	CTR1(KTR_SPARE2, "cpu_idle(%d) done", busy);
688dfe7b3bfSKonstantin Belousov }
689dfe7b3bfSKonstantin Belousov 
6903f3937b4SKonstantin Belousov static int cpu_idle_apl31_workaround;
691*149b9c23SZhenlei Huang SYSCTL_INT(_machdep, OID_AUTO, idle_apl31, CTLFLAG_RWTUN | CTLFLAG_NOFETCH,
6923f3937b4SKonstantin Belousov     &cpu_idle_apl31_workaround, 0,
693160be7ccSKonstantin Belousov     "Apollo Lake APL31 MWAIT bug workaround");
6943f3937b4SKonstantin Belousov 
695dfe7b3bfSKonstantin Belousov int
696dfe7b3bfSKonstantin Belousov cpu_idle_wakeup(int cpu)
697dfe7b3bfSKonstantin Belousov {
69883dc49beSConrad Meyer 	struct monitorbuf *mb;
699dfe7b3bfSKonstantin Belousov 	int *state;
700dfe7b3bfSKonstantin Belousov 
70183dc49beSConrad Meyer 	mb = &pcpu_find(cpu)->pc_monitorbuf;
70283dc49beSConrad Meyer 	state = &mb->idle_state;
703a5bd21d0SKonstantin Belousov 	switch (atomic_load_int(state)) {
704a5bd21d0SKonstantin Belousov 	case STATE_SLEEPING:
705dfe7b3bfSKonstantin Belousov 		return (0);
706a5bd21d0SKonstantin Belousov 	case STATE_MWAIT:
707a5bd21d0SKonstantin Belousov 		atomic_store_int(state, STATE_RUNNING);
7083f3937b4SKonstantin Belousov 		return (cpu_idle_apl31_workaround ? 0 : 1);
709a5bd21d0SKonstantin Belousov 	case STATE_RUNNING:
710a5bd21d0SKonstantin Belousov 		return (1);
711a5bd21d0SKonstantin Belousov 	default:
712a5bd21d0SKonstantin Belousov 		panic("bad monitor state");
713a5bd21d0SKonstantin Belousov 		return (1);
714a5bd21d0SKonstantin Belousov 	}
715dfe7b3bfSKonstantin Belousov }
716dfe7b3bfSKonstantin Belousov 
717dfe7b3bfSKonstantin Belousov /*
718dfe7b3bfSKonstantin Belousov  * Ordered by speed/power consumption.
719dfe7b3bfSKonstantin Belousov  */
7207266f524SMark Johnston static const struct {
721dfe7b3bfSKonstantin Belousov 	void	*id_fn;
7227266f524SMark Johnston 	const char *id_name;
723a5f472c5SKonstantin Belousov 	int	id_cpuid2_flag;
724dfe7b3bfSKonstantin Belousov } idle_tbl[] = {
725a5f472c5SKonstantin Belousov 	{ .id_fn = cpu_idle_spin, .id_name = "spin" },
726a5f472c5SKonstantin Belousov 	{ .id_fn = cpu_idle_mwait, .id_name = "mwait",
727a5f472c5SKonstantin Belousov 	    .id_cpuid2_flag = CPUID2_MON },
728a5f472c5SKonstantin Belousov 	{ .id_fn = cpu_idle_hlt, .id_name = "hlt" },
729a5f472c5SKonstantin Belousov 	{ .id_fn = cpu_idle_acpi, .id_name = "acpi" },
730dfe7b3bfSKonstantin Belousov };
731dfe7b3bfSKonstantin Belousov 
732dfe7b3bfSKonstantin Belousov static int
733dfe7b3bfSKonstantin Belousov idle_sysctl_available(SYSCTL_HANDLER_ARGS)
734dfe7b3bfSKonstantin Belousov {
735dfe7b3bfSKonstantin Belousov 	char *avail, *p;
736dfe7b3bfSKonstantin Belousov 	int error;
737dfe7b3bfSKonstantin Belousov 	int i;
738dfe7b3bfSKonstantin Belousov 
739dfe7b3bfSKonstantin Belousov 	avail = malloc(256, M_TEMP, M_WAITOK);
740dfe7b3bfSKonstantin Belousov 	p = avail;
741a5f472c5SKonstantin Belousov 	for (i = 0; i < nitems(idle_tbl); i++) {
742a5f472c5SKonstantin Belousov 		if (idle_tbl[i].id_cpuid2_flag != 0 &&
743a5f472c5SKonstantin Belousov 		    (cpu_feature2 & idle_tbl[i].id_cpuid2_flag) == 0)
744dfe7b3bfSKonstantin Belousov 			continue;
745dfe7b3bfSKonstantin Belousov 		if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
746dfe7b3bfSKonstantin Belousov 		    cpu_idle_hook == NULL)
747dfe7b3bfSKonstantin Belousov 			continue;
748dfe7b3bfSKonstantin Belousov 		p += sprintf(p, "%s%s", p != avail ? ", " : "",
749dfe7b3bfSKonstantin Belousov 		    idle_tbl[i].id_name);
750dfe7b3bfSKonstantin Belousov 	}
751dfe7b3bfSKonstantin Belousov 	error = sysctl_handle_string(oidp, avail, 0, req);
752dfe7b3bfSKonstantin Belousov 	free(avail, M_TEMP);
753dfe7b3bfSKonstantin Belousov 	return (error);
754dfe7b3bfSKonstantin Belousov }
755dfe7b3bfSKonstantin Belousov 
7567029da5cSPawel Biernacki SYSCTL_PROC(_machdep, OID_AUTO, idle_available,
7571d6fb900SAlexander Motin     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7587029da5cSPawel Biernacki     0, 0, idle_sysctl_available, "A",
7597029da5cSPawel Biernacki     "list of available idle functions");
760dfe7b3bfSKonstantin Belousov 
76155ba21d4SKonstantin Belousov static bool
762a5f472c5SKonstantin Belousov cpu_idle_selector(const char *new_idle_name)
76355ba21d4SKonstantin Belousov {
76455ba21d4SKonstantin Belousov 	int i;
76555ba21d4SKonstantin Belousov 
766a5f472c5SKonstantin Belousov 	for (i = 0; i < nitems(idle_tbl); i++) {
767a5f472c5SKonstantin Belousov 		if (idle_tbl[i].id_cpuid2_flag != 0 &&
768a5f472c5SKonstantin Belousov 		    (cpu_feature2 & idle_tbl[i].id_cpuid2_flag) == 0)
76955ba21d4SKonstantin Belousov 			continue;
77055ba21d4SKonstantin Belousov 		if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
77155ba21d4SKonstantin Belousov 		    cpu_idle_hook == NULL)
77255ba21d4SKonstantin Belousov 			continue;
77355ba21d4SKonstantin Belousov 		if (strcmp(idle_tbl[i].id_name, new_idle_name))
77455ba21d4SKonstantin Belousov 			continue;
77555ba21d4SKonstantin Belousov 		cpu_idle_fn = idle_tbl[i].id_fn;
77655ba21d4SKonstantin Belousov 		if (bootverbose)
77755ba21d4SKonstantin Belousov 			printf("CPU idle set to %s\n", idle_tbl[i].id_name);
77855ba21d4SKonstantin Belousov 		return (true);
77955ba21d4SKonstantin Belousov 	}
78055ba21d4SKonstantin Belousov 	return (false);
78155ba21d4SKonstantin Belousov }
78255ba21d4SKonstantin Belousov 
783dfe7b3bfSKonstantin Belousov static int
784a5f472c5SKonstantin Belousov cpu_idle_sysctl(SYSCTL_HANDLER_ARGS)
785dfe7b3bfSKonstantin Belousov {
7867266f524SMark Johnston 	char buf[16];
7877266f524SMark Johnston 	const char *p;
78855ba21d4SKonstantin Belousov 	int error, i;
789dfe7b3bfSKonstantin Belousov 
790dfe7b3bfSKonstantin Belousov 	p = "unknown";
791a5f472c5SKonstantin Belousov 	for (i = 0; i < nitems(idle_tbl); i++) {
792dfe7b3bfSKonstantin Belousov 		if (idle_tbl[i].id_fn == cpu_idle_fn) {
793dfe7b3bfSKonstantin Belousov 			p = idle_tbl[i].id_name;
794dfe7b3bfSKonstantin Belousov 			break;
795dfe7b3bfSKonstantin Belousov 		}
796dfe7b3bfSKonstantin Belousov 	}
797dfe7b3bfSKonstantin Belousov 	strncpy(buf, p, sizeof(buf));
798dfe7b3bfSKonstantin Belousov 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
799dfe7b3bfSKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
800dfe7b3bfSKonstantin Belousov 		return (error);
801a5f472c5SKonstantin Belousov 	return (cpu_idle_selector(buf) ? 0 : EINVAL);
802dfe7b3bfSKonstantin Belousov }
803dfe7b3bfSKonstantin Belousov 
8047029da5cSPawel Biernacki SYSCTL_PROC(_machdep, OID_AUTO, idle,
805*149b9c23SZhenlei Huang     CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
8067029da5cSPawel Biernacki     0, 0, cpu_idle_sysctl, "A",
8077029da5cSPawel Biernacki     "currently selected idle function");
808835c2787SKonstantin Belousov 
80955ba21d4SKonstantin Belousov static void
810a5f472c5SKonstantin Belousov cpu_idle_tun(void *unused __unused)
81155ba21d4SKonstantin Belousov {
81255ba21d4SKonstantin Belousov 	char tunvar[16];
81355ba21d4SKonstantin Belousov 
81455ba21d4SKonstantin Belousov 	if (TUNABLE_STR_FETCH("machdep.idle", tunvar, sizeof(tunvar)))
815a5f472c5SKonstantin Belousov 		cpu_idle_selector(tunvar);
81645ed991dSKonstantin Belousov 	else if (cpu_vendor_id == CPU_VENDOR_AMD &&
81745ed991dSKonstantin Belousov 	    CPUID_TO_FAMILY(cpu_id) == 0x17 && CPUID_TO_MODEL(cpu_id) == 0x1) {
81845ed991dSKonstantin Belousov 		/* Ryzen erratas 1057, 1109. */
81945ed991dSKonstantin Belousov 		cpu_idle_selector("hlt");
82045ed991dSKonstantin Belousov 		idle_mwait = 0;
821665919aaSConrad Meyer 		mwait_cpustop_broken = true;
82245ed991dSKonstantin Belousov 	}
82345ed991dSKonstantin Belousov 
824b223c1f1SCorvin Köhne 	if (cpu_vendor_id == CPU_VENDOR_INTEL &&
825122405c9SCorvin Köhne 	    CPUID_TO_FAMILY(cpu_id) == 0x6 && CPUID_TO_MODEL(cpu_id) == 0x5c) {
8263f3937b4SKonstantin Belousov 		/*
827160be7ccSKonstantin Belousov 		 * Apollo Lake errata APL31 (public errata APL30).
828160be7ccSKonstantin Belousov 		 * Stores to the armed address range may not trigger
829160be7ccSKonstantin Belousov 		 * MWAIT to resume execution.  OS needs to use
830160be7ccSKonstantin Belousov 		 * interrupts to wake processors from MWAIT-induced
831160be7ccSKonstantin Belousov 		 * sleep states.
8323f3937b4SKonstantin Belousov 		 */
8333f3937b4SKonstantin Belousov 		cpu_idle_apl31_workaround = 1;
834665919aaSConrad Meyer 		mwait_cpustop_broken = true;
8353f3937b4SKonstantin Belousov 	}
8363f3937b4SKonstantin Belousov 	TUNABLE_INT_FETCH("machdep.idle_apl31", &cpu_idle_apl31_workaround);
83755ba21d4SKonstantin Belousov }
838a5f472c5SKonstantin Belousov SYSINIT(cpu_idle_tun, SI_SUB_CPU, SI_ORDER_MIDDLE, cpu_idle_tun, NULL);
83955ba21d4SKonstantin Belousov 
840ba0ced82SEric van Gyzen static int panic_on_nmi = 0xff;
841295f4b6cSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RWTUN,
842295f4b6cSKonstantin Belousov     &panic_on_nmi, 0,
843ba0ced82SEric van Gyzen     "Panic on NMI: 1 = H/W failure; 2 = unknown; 0xff = all");
844835c2787SKonstantin Belousov int nmi_is_broadcast = 1;
845835c2787SKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, nmi_is_broadcast, CTLFLAG_RWTUN,
846835c2787SKonstantin Belousov     &nmi_is_broadcast, 0,
847835c2787SKonstantin Belousov     "Chipset NMI is broadcast");
848855e49f3SAlexander Motin int (*apei_nmi)(void);
849835c2787SKonstantin Belousov 
850295f4b6cSKonstantin Belousov void
851295f4b6cSKonstantin Belousov nmi_call_kdb(u_int cpu, u_int type, struct trapframe *frame)
852835c2787SKonstantin Belousov {
8530fb3a72aSAndriy Gapon 	bool claimed = false;
854835c2787SKonstantin Belousov 
8550fb3a72aSAndriy Gapon #ifdef DEV_ISA
856835c2787SKonstantin Belousov 	/* machine/parity/power fail/"kitchen sink" faults */
8570fb3a72aSAndriy Gapon 	if (isa_nmi(frame->tf_err)) {
8580fb3a72aSAndriy Gapon 		claimed = true;
859ba0ced82SEric van Gyzen 		if ((panic_on_nmi & 1) != 0)
8600fb3a72aSAndriy Gapon 			panic("NMI indicates hardware failure");
8610fb3a72aSAndriy Gapon 	}
8620fb3a72aSAndriy Gapon #endif /* DEV_ISA */
863ba0ced82SEric van Gyzen 
864855e49f3SAlexander Motin 	/* ACPI Platform Error Interfaces callback. */
865855e49f3SAlexander Motin 	if (apei_nmi != NULL && (*apei_nmi)())
866855e49f3SAlexander Motin 		claimed = true;
867855e49f3SAlexander Motin 
868835c2787SKonstantin Belousov 	/*
869ba0ced82SEric van Gyzen 	 * NMIs can be useful for debugging.  They can be hooked up to a
870ba0ced82SEric van Gyzen 	 * pushbutton, usually on an ISA, PCI, or PCIe card.  They can also be
871ba0ced82SEric van Gyzen 	 * generated by an IPMI BMC, either manually or in response to a
872ba0ced82SEric van Gyzen 	 * watchdog timeout.  For example, see the "power diag" command in
873ba0ced82SEric van Gyzen 	 * ports/sysutils/ipmitool.  They can also be generated by a
874ba0ced82SEric van Gyzen 	 * hypervisor; see "bhyvectl --inject-nmi".
875835c2787SKonstantin Belousov 	 */
876ba0ced82SEric van Gyzen 
877ba0ced82SEric van Gyzen #ifdef KDB
878ba0ced82SEric van Gyzen 	if (!claimed && (panic_on_nmi & 2) != 0) {
879ba0ced82SEric van Gyzen 		if (debugger_on_panic) {
880835c2787SKonstantin Belousov 			printf("NMI/cpu%d ... going to debugger\n", cpu);
881ba0ced82SEric van Gyzen 			claimed = kdb_trap(type, 0, frame);
882ba0ced82SEric van Gyzen 		}
883835c2787SKonstantin Belousov 	}
884835c2787SKonstantin Belousov #endif /* KDB */
885ba0ced82SEric van Gyzen 
886ba0ced82SEric van Gyzen 	if (!claimed && panic_on_nmi != 0)
887ba0ced82SEric van Gyzen 		panic("NMI");
888295f4b6cSKonstantin Belousov }
889835c2787SKonstantin Belousov 
890295f4b6cSKonstantin Belousov void
891295f4b6cSKonstantin Belousov nmi_handle_intr(u_int type, struct trapframe *frame)
892835c2787SKonstantin Belousov {
893835c2787SKonstantin Belousov 
894835c2787SKonstantin Belousov #ifdef SMP
895295f4b6cSKonstantin Belousov 	if (nmi_is_broadcast) {
896295f4b6cSKonstantin Belousov 		nmi_call_kdb_smp(type, frame);
897295f4b6cSKonstantin Belousov 		return;
898295f4b6cSKonstantin Belousov 	}
899835c2787SKonstantin Belousov #endif
9001d6dfd12SKonstantin Belousov 	nmi_call_kdb(PCPU_GET(cpuid), type, frame);
901835c2787SKonstantin Belousov }
902319117fdSKonstantin Belousov 
903a324b7f7SKonstantin Belousov static int hw_ibrs_active;
904a324b7f7SKonstantin Belousov int hw_ibrs_ibpb_active;
905319117fdSKonstantin Belousov int hw_ibrs_disable = 1;
906319117fdSKonstantin Belousov 
907319117fdSKonstantin Belousov SYSCTL_INT(_hw, OID_AUTO, ibrs_active, CTLFLAG_RD, &hw_ibrs_active, 0,
908b31b965eSKonstantin Belousov     "Indirect Branch Restricted Speculation active");
909319117fdSKonstantin Belousov 
9107029da5cSPawel Biernacki SYSCTL_NODE(_machdep_mitigations, OID_AUTO, ibrs,
9117029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
912961aacb1SScott Long     "Indirect Branch Restricted Speculation active");
913961aacb1SScott Long 
914961aacb1SScott Long SYSCTL_INT(_machdep_mitigations_ibrs, OID_AUTO, active, CTLFLAG_RD,
915961aacb1SScott Long     &hw_ibrs_active, 0, "Indirect Branch Restricted Speculation active");
916961aacb1SScott Long 
917319117fdSKonstantin Belousov void
918a324b7f7SKonstantin Belousov hw_ibrs_recalculate(bool for_all_cpus)
919319117fdSKonstantin Belousov {
920319117fdSKonstantin Belousov 	if ((cpu_ia32_arch_caps & IA32_ARCH_CAP_IBRS_ALL) != 0) {
921a324b7f7SKonstantin Belousov 		x86_msr_op(MSR_IA32_SPEC_CTRL, (for_all_cpus ?
922d0bc4b46SKonstantin Belousov 		    MSR_OP_RENDEZVOUS_ALL : MSR_OP_LOCAL) |
923a324b7f7SKonstantin Belousov 		    (hw_ibrs_disable != 0 ? MSR_OP_ANDNOT : MSR_OP_OR),
924d0bc4b46SKonstantin Belousov 		    IA32_SPEC_CTRL_IBRS, NULL);
925a324b7f7SKonstantin Belousov 		hw_ibrs_active = hw_ibrs_disable == 0;
926a324b7f7SKonstantin Belousov 		hw_ibrs_ibpb_active = 0;
927a324b7f7SKonstantin Belousov 	} else {
928a324b7f7SKonstantin Belousov 		hw_ibrs_active = hw_ibrs_ibpb_active = (cpu_stdext_feature3 &
929a324b7f7SKonstantin Belousov 		    CPUID_STDEXT3_IBPB) != 0 && !hw_ibrs_disable;
930319117fdSKonstantin Belousov 	}
931319117fdSKonstantin Belousov }
932319117fdSKonstantin Belousov 
933319117fdSKonstantin Belousov static int
934319117fdSKonstantin Belousov hw_ibrs_disable_handler(SYSCTL_HANDLER_ARGS)
935319117fdSKonstantin Belousov {
936319117fdSKonstantin Belousov 	int error, val;
937319117fdSKonstantin Belousov 
938319117fdSKonstantin Belousov 	val = hw_ibrs_disable;
939319117fdSKonstantin Belousov 	error = sysctl_handle_int(oidp, &val, 0, req);
940319117fdSKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
941319117fdSKonstantin Belousov 		return (error);
942319117fdSKonstantin Belousov 	hw_ibrs_disable = val != 0;
943a324b7f7SKonstantin Belousov 	hw_ibrs_recalculate(true);
944319117fdSKonstantin Belousov 	return (0);
945319117fdSKonstantin Belousov }
946319117fdSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, ibrs_disable, CTLTYPE_INT | CTLFLAG_RWTUN |
947319117fdSKonstantin Belousov     CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0, hw_ibrs_disable_handler, "I",
948b31b965eSKonstantin Belousov     "Disable Indirect Branch Restricted Speculation");
9498fbcc334SKonstantin Belousov 
950961aacb1SScott Long SYSCTL_PROC(_machdep_mitigations_ibrs, OID_AUTO, disable, CTLTYPE_INT |
951961aacb1SScott Long     CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
952961aacb1SScott Long     hw_ibrs_disable_handler, "I",
953961aacb1SScott Long     "Disable Indirect Branch Restricted Speculation");
954961aacb1SScott Long 
9553621ba1eSKonstantin Belousov int hw_ssb_active;
9563621ba1eSKonstantin Belousov int hw_ssb_disable;
9573621ba1eSKonstantin Belousov 
9583621ba1eSKonstantin Belousov SYSCTL_INT(_hw, OID_AUTO, spec_store_bypass_disable_active, CTLFLAG_RD,
9593621ba1eSKonstantin Belousov     &hw_ssb_active, 0,
9603621ba1eSKonstantin Belousov     "Speculative Store Bypass Disable active");
9613621ba1eSKonstantin Belousov 
9627029da5cSPawel Biernacki SYSCTL_NODE(_machdep_mitigations, OID_AUTO, ssb,
9637029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
964961aacb1SScott Long     "Speculative Store Bypass Disable active");
965961aacb1SScott Long 
966961aacb1SScott Long SYSCTL_INT(_machdep_mitigations_ssb, OID_AUTO, active, CTLFLAG_RD,
967961aacb1SScott Long     &hw_ssb_active, 0, "Speculative Store Bypass Disable active");
968961aacb1SScott Long 
9693621ba1eSKonstantin Belousov static void
9703621ba1eSKonstantin Belousov hw_ssb_set(bool enable, bool for_all_cpus)
9713621ba1eSKonstantin Belousov {
9723621ba1eSKonstantin Belousov 
9733621ba1eSKonstantin Belousov 	if ((cpu_stdext_feature3 & CPUID_STDEXT3_SSBD) == 0) {
9743621ba1eSKonstantin Belousov 		hw_ssb_active = 0;
9753621ba1eSKonstantin Belousov 		return;
9763621ba1eSKonstantin Belousov 	}
9773621ba1eSKonstantin Belousov 	hw_ssb_active = enable;
978fa83f689SKonstantin Belousov 	x86_msr_op(MSR_IA32_SPEC_CTRL,
979fa83f689SKonstantin Belousov 	    (enable ? MSR_OP_OR : MSR_OP_ANDNOT) |
980d0bc4b46SKonstantin Belousov 	    (for_all_cpus ? MSR_OP_SCHED_ALL : MSR_OP_LOCAL),
981d0bc4b46SKonstantin Belousov 	    IA32_SPEC_CTRL_SSBD, NULL);
9823621ba1eSKonstantin Belousov }
9833621ba1eSKonstantin Belousov 
9843621ba1eSKonstantin Belousov void
9853621ba1eSKonstantin Belousov hw_ssb_recalculate(bool all_cpus)
9863621ba1eSKonstantin Belousov {
9873621ba1eSKonstantin Belousov 
9883621ba1eSKonstantin Belousov 	switch (hw_ssb_disable) {
9893621ba1eSKonstantin Belousov 	default:
9903621ba1eSKonstantin Belousov 		hw_ssb_disable = 0;
9913621ba1eSKonstantin Belousov 		/* FALLTHROUGH */
9923621ba1eSKonstantin Belousov 	case 0: /* off */
9933621ba1eSKonstantin Belousov 		hw_ssb_set(false, all_cpus);
9943621ba1eSKonstantin Belousov 		break;
9953621ba1eSKonstantin Belousov 	case 1: /* on */
9963621ba1eSKonstantin Belousov 		hw_ssb_set(true, all_cpus);
9973621ba1eSKonstantin Belousov 		break;
9983621ba1eSKonstantin Belousov 	case 2: /* auto */
99923437573SKonstantin Belousov 		hw_ssb_set((cpu_ia32_arch_caps & IA32_ARCH_CAP_SSB_NO) != 0 ?
10003621ba1eSKonstantin Belousov 		    false : true, all_cpus);
10013621ba1eSKonstantin Belousov 		break;
10023621ba1eSKonstantin Belousov 	}
10033621ba1eSKonstantin Belousov }
10043621ba1eSKonstantin Belousov 
10053621ba1eSKonstantin Belousov static int
10063621ba1eSKonstantin Belousov hw_ssb_disable_handler(SYSCTL_HANDLER_ARGS)
10073621ba1eSKonstantin Belousov {
10083621ba1eSKonstantin Belousov 	int error, val;
10093621ba1eSKonstantin Belousov 
10103621ba1eSKonstantin Belousov 	val = hw_ssb_disable;
10113621ba1eSKonstantin Belousov 	error = sysctl_handle_int(oidp, &val, 0, req);
10123621ba1eSKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
10133621ba1eSKonstantin Belousov 		return (error);
10143621ba1eSKonstantin Belousov 	hw_ssb_disable = val;
10153621ba1eSKonstantin Belousov 	hw_ssb_recalculate(true);
10163621ba1eSKonstantin Belousov 	return (0);
10173621ba1eSKonstantin Belousov }
10183621ba1eSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, spec_store_bypass_disable, CTLTYPE_INT |
10193621ba1eSKonstantin Belousov     CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
10203621ba1eSKonstantin Belousov     hw_ssb_disable_handler, "I",
1021a212f56dSPiotr Pawel Stefaniak     "Speculative Store Bypass Disable (0 - off, 1 - on, 2 - auto)");
10223621ba1eSKonstantin Belousov 
1023961aacb1SScott Long SYSCTL_PROC(_machdep_mitigations_ssb, OID_AUTO, disable, CTLTYPE_INT |
1024961aacb1SScott Long     CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
1025961aacb1SScott Long     hw_ssb_disable_handler, "I",
1026a212f56dSPiotr Pawel Stefaniak     "Speculative Store Bypass Disable (0 - off, 1 - on, 2 - auto)");
1027961aacb1SScott Long 
10287355a02bSKonstantin Belousov int hw_mds_disable;
10297355a02bSKonstantin Belousov 
10307355a02bSKonstantin Belousov /*
10317355a02bSKonstantin Belousov  * Handler for Microarchitectural Data Sampling issues.  Really not a
10327355a02bSKonstantin Belousov  * pointer to C function: on amd64 the code must not change any CPU
10337355a02bSKonstantin Belousov  * architectural state except possibly %rflags. Also, it is always
10347355a02bSKonstantin Belousov  * called with interrupts disabled.
10357355a02bSKonstantin Belousov  */
10367355a02bSKonstantin Belousov void mds_handler_void(void);
10377355a02bSKonstantin Belousov void mds_handler_verw(void);
10387355a02bSKonstantin Belousov void mds_handler_ivb(void);
10397355a02bSKonstantin Belousov void mds_handler_bdw(void);
10407355a02bSKonstantin Belousov void mds_handler_skl_sse(void);
10417355a02bSKonstantin Belousov void mds_handler_skl_avx(void);
10427355a02bSKonstantin Belousov void mds_handler_skl_avx512(void);
10437355a02bSKonstantin Belousov void mds_handler_silvermont(void);
1044e2e0470dSKonstantin Belousov void (*mds_handler)(void) = mds_handler_void;
10457355a02bSKonstantin Belousov 
10467355a02bSKonstantin Belousov static int
10477355a02bSKonstantin Belousov sysctl_hw_mds_disable_state_handler(SYSCTL_HANDLER_ARGS)
10487355a02bSKonstantin Belousov {
10497355a02bSKonstantin Belousov 	const char *state;
10507355a02bSKonstantin Belousov 
10517355a02bSKonstantin Belousov 	if (mds_handler == mds_handler_void)
10527355a02bSKonstantin Belousov 		state = "inactive";
10537355a02bSKonstantin Belousov 	else if (mds_handler == mds_handler_verw)
10547355a02bSKonstantin Belousov 		state = "VERW";
10557355a02bSKonstantin Belousov 	else if (mds_handler == mds_handler_ivb)
10567355a02bSKonstantin Belousov 		state = "software IvyBridge";
10577355a02bSKonstantin Belousov 	else if (mds_handler == mds_handler_bdw)
10587355a02bSKonstantin Belousov 		state = "software Broadwell";
10597355a02bSKonstantin Belousov 	else if (mds_handler == mds_handler_skl_sse)
10607355a02bSKonstantin Belousov 		state = "software Skylake SSE";
10617355a02bSKonstantin Belousov 	else if (mds_handler == mds_handler_skl_avx)
10627355a02bSKonstantin Belousov 		state = "software Skylake AVX";
10637355a02bSKonstantin Belousov 	else if (mds_handler == mds_handler_skl_avx512)
10647355a02bSKonstantin Belousov 		state = "software Skylake AVX512";
10657355a02bSKonstantin Belousov 	else if (mds_handler == mds_handler_silvermont)
10667355a02bSKonstantin Belousov 		state = "software Silvermont";
10677355a02bSKonstantin Belousov 	else
10687355a02bSKonstantin Belousov 		state = "unknown";
10697355a02bSKonstantin Belousov 	return (SYSCTL_OUT(req, state, strlen(state)));
10707355a02bSKonstantin Belousov }
10717355a02bSKonstantin Belousov 
10727355a02bSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, mds_disable_state,
10737355a02bSKonstantin Belousov     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
10747355a02bSKonstantin Belousov     sysctl_hw_mds_disable_state_handler, "A",
10757355a02bSKonstantin Belousov     "Microarchitectural Data Sampling Mitigation state");
10767355a02bSKonstantin Belousov 
10777029da5cSPawel Biernacki SYSCTL_NODE(_machdep_mitigations, OID_AUTO, mds,
10787029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1079961aacb1SScott Long     "Microarchitectural Data Sampling Mitigation state");
1080961aacb1SScott Long 
1081961aacb1SScott Long SYSCTL_PROC(_machdep_mitigations_mds, OID_AUTO, state,
1082961aacb1SScott Long     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1083961aacb1SScott Long     sysctl_hw_mds_disable_state_handler, "A",
1084961aacb1SScott Long     "Microarchitectural Data Sampling Mitigation state");
1085961aacb1SScott Long 
10867355a02bSKonstantin Belousov _Static_assert(__offsetof(struct pcpu, pc_mds_tmp) % 64 == 0, "MDS AVX512");
10877355a02bSKonstantin Belousov 
10887355a02bSKonstantin Belousov void
10897355a02bSKonstantin Belousov hw_mds_recalculate(void)
10907355a02bSKonstantin Belousov {
10917355a02bSKonstantin Belousov 	struct pcpu *pc;
10927355a02bSKonstantin Belousov 	vm_offset_t b64;
10937355a02bSKonstantin Belousov 	u_long xcr0;
10947355a02bSKonstantin Belousov 	int i;
10957355a02bSKonstantin Belousov 
10967355a02bSKonstantin Belousov 	/*
10977355a02bSKonstantin Belousov 	 * Allow user to force VERW variant even if MD_CLEAR is not
10987355a02bSKonstantin Belousov 	 * reported.  For instance, hypervisor might unknowingly
10997355a02bSKonstantin Belousov 	 * filter the cap out.
11007355a02bSKonstantin Belousov 	 * For the similar reasons, and for testing, allow to enable
110136e1ad61SKonstantin Belousov 	 * mitigation even when MDS_NO cap is set.
11027355a02bSKonstantin Belousov 	 */
11037355a02bSKonstantin Belousov 	if (cpu_vendor_id != CPU_VENDOR_INTEL || hw_mds_disable == 0 ||
110436e1ad61SKonstantin Belousov 	    ((cpu_ia32_arch_caps & IA32_ARCH_CAP_MDS_NO) != 0 &&
110536e1ad61SKonstantin Belousov 	    hw_mds_disable == 3)) {
11067355a02bSKonstantin Belousov 		mds_handler = mds_handler_void;
11077355a02bSKonstantin Belousov 	} else if (((cpu_stdext_feature3 & CPUID_STDEXT3_MD_CLEAR) != 0 &&
11087355a02bSKonstantin Belousov 	    hw_mds_disable == 3) || hw_mds_disable == 1) {
11097355a02bSKonstantin Belousov 		mds_handler = mds_handler_verw;
11107355a02bSKonstantin Belousov 	} else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
11117355a02bSKonstantin Belousov 	    (CPUID_TO_MODEL(cpu_id) == 0x2e || CPUID_TO_MODEL(cpu_id) == 0x1e ||
11127355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x1f || CPUID_TO_MODEL(cpu_id) == 0x1a ||
11137355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x2f || CPUID_TO_MODEL(cpu_id) == 0x25 ||
11147355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x2c || CPUID_TO_MODEL(cpu_id) == 0x2d ||
11157355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x2a || CPUID_TO_MODEL(cpu_id) == 0x3e ||
11167355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x3a) &&
11177355a02bSKonstantin Belousov 	    (hw_mds_disable == 2 || hw_mds_disable == 3)) {
11187355a02bSKonstantin Belousov 		/*
11197355a02bSKonstantin Belousov 		 * Nehalem, SandyBridge, IvyBridge
11207355a02bSKonstantin Belousov 		 */
11217355a02bSKonstantin Belousov 		CPU_FOREACH(i) {
11227355a02bSKonstantin Belousov 			pc = pcpu_find(i);
11237355a02bSKonstantin Belousov 			if (pc->pc_mds_buf == NULL) {
11247355a02bSKonstantin Belousov 				pc->pc_mds_buf = malloc_domainset(672, M_TEMP,
11257355a02bSKonstantin Belousov 				    DOMAINSET_PREF(pc->pc_domain), M_WAITOK);
11267355a02bSKonstantin Belousov 				bzero(pc->pc_mds_buf, 16);
11277355a02bSKonstantin Belousov 			}
11287355a02bSKonstantin Belousov 		}
11297355a02bSKonstantin Belousov 		mds_handler = mds_handler_ivb;
11307355a02bSKonstantin Belousov 	} else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
11317355a02bSKonstantin Belousov 	    (CPUID_TO_MODEL(cpu_id) == 0x3f || CPUID_TO_MODEL(cpu_id) == 0x3c ||
11327355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x45 || CPUID_TO_MODEL(cpu_id) == 0x46 ||
11337355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x56 || CPUID_TO_MODEL(cpu_id) == 0x4f ||
11347355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x47 || CPUID_TO_MODEL(cpu_id) == 0x3d) &&
11357355a02bSKonstantin Belousov 	    (hw_mds_disable == 2 || hw_mds_disable == 3)) {
11367355a02bSKonstantin Belousov 		/*
11377355a02bSKonstantin Belousov 		 * Haswell, Broadwell
11387355a02bSKonstantin Belousov 		 */
11397355a02bSKonstantin Belousov 		CPU_FOREACH(i) {
11407355a02bSKonstantin Belousov 			pc = pcpu_find(i);
11417355a02bSKonstantin Belousov 			if (pc->pc_mds_buf == NULL) {
11427355a02bSKonstantin Belousov 				pc->pc_mds_buf = malloc_domainset(1536, M_TEMP,
11437355a02bSKonstantin Belousov 				    DOMAINSET_PREF(pc->pc_domain), M_WAITOK);
11447355a02bSKonstantin Belousov 				bzero(pc->pc_mds_buf, 16);
11457355a02bSKonstantin Belousov 			}
11467355a02bSKonstantin Belousov 		}
11477355a02bSKonstantin Belousov 		mds_handler = mds_handler_bdw;
11487355a02bSKonstantin Belousov 	} else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
11497355a02bSKonstantin Belousov 	    ((CPUID_TO_MODEL(cpu_id) == 0x55 && (cpu_id &
11507355a02bSKonstantin Belousov 	    CPUID_STEPPING) <= 5) ||
11517355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x4e || CPUID_TO_MODEL(cpu_id) == 0x5e ||
11527355a02bSKonstantin Belousov 	    (CPUID_TO_MODEL(cpu_id) == 0x8e && (cpu_id &
11537355a02bSKonstantin Belousov 	    CPUID_STEPPING) <= 0xb) ||
11547355a02bSKonstantin Belousov 	    (CPUID_TO_MODEL(cpu_id) == 0x9e && (cpu_id &
11557355a02bSKonstantin Belousov 	    CPUID_STEPPING) <= 0xc)) &&
11567355a02bSKonstantin Belousov 	    (hw_mds_disable == 2 || hw_mds_disable == 3)) {
11577355a02bSKonstantin Belousov 		/*
11587355a02bSKonstantin Belousov 		 * Skylake, KabyLake, CoffeeLake, WhiskeyLake,
11597355a02bSKonstantin Belousov 		 * CascadeLake
11607355a02bSKonstantin Belousov 		 */
11617355a02bSKonstantin Belousov 		CPU_FOREACH(i) {
11627355a02bSKonstantin Belousov 			pc = pcpu_find(i);
11637355a02bSKonstantin Belousov 			if (pc->pc_mds_buf == NULL) {
11647355a02bSKonstantin Belousov 				pc->pc_mds_buf = malloc_domainset(6 * 1024,
11657355a02bSKonstantin Belousov 				    M_TEMP, DOMAINSET_PREF(pc->pc_domain),
11667355a02bSKonstantin Belousov 				    M_WAITOK);
11677355a02bSKonstantin Belousov 				b64 = (vm_offset_t)malloc_domainset(64 + 63,
11687355a02bSKonstantin Belousov 				    M_TEMP, DOMAINSET_PREF(pc->pc_domain),
11697355a02bSKonstantin Belousov 				    M_WAITOK);
11707355a02bSKonstantin Belousov 				pc->pc_mds_buf64 = (void *)roundup2(b64, 64);
11717355a02bSKonstantin Belousov 				bzero(pc->pc_mds_buf64, 64);
11727355a02bSKonstantin Belousov 			}
11737355a02bSKonstantin Belousov 		}
11747355a02bSKonstantin Belousov 		xcr0 = rxcr(0);
11757355a02bSKonstantin Belousov 		if ((xcr0 & XFEATURE_ENABLED_ZMM_HI256) != 0 &&
117699a6085fSScott Long 		    (cpu_stdext_feature & CPUID_STDEXT_AVX512DQ) != 0)
11777355a02bSKonstantin Belousov 			mds_handler = mds_handler_skl_avx512;
11787355a02bSKonstantin Belousov 		else if ((xcr0 & XFEATURE_ENABLED_AVX) != 0 &&
11797355a02bSKonstantin Belousov 		    (cpu_feature2 & CPUID2_AVX) != 0)
11807355a02bSKonstantin Belousov 			mds_handler = mds_handler_skl_avx;
11817355a02bSKonstantin Belousov 		else
11827355a02bSKonstantin Belousov 			mds_handler = mds_handler_skl_sse;
11837355a02bSKonstantin Belousov 	} else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
11847355a02bSKonstantin Belousov 	    ((CPUID_TO_MODEL(cpu_id) == 0x37 ||
11857355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x4a ||
11867355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x4c ||
11877355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x4d ||
11887355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x5a ||
11897355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x5d ||
11907355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x6e ||
11917355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x65 ||
11927355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x75 ||
11937355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x1c ||
11947355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x26 ||
11957355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x27 ||
11967355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x35 ||
11977355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x36 ||
11987355a02bSKonstantin Belousov 	    CPUID_TO_MODEL(cpu_id) == 0x7a))) {
11997355a02bSKonstantin Belousov 		/* Silvermont, Airmont */
12007355a02bSKonstantin Belousov 		CPU_FOREACH(i) {
12017355a02bSKonstantin Belousov 			pc = pcpu_find(i);
12027355a02bSKonstantin Belousov 			if (pc->pc_mds_buf == NULL)
12037355a02bSKonstantin Belousov 				pc->pc_mds_buf = malloc(256, M_TEMP, M_WAITOK);
12047355a02bSKonstantin Belousov 		}
12057355a02bSKonstantin Belousov 		mds_handler = mds_handler_silvermont;
12067355a02bSKonstantin Belousov 	} else {
12077355a02bSKonstantin Belousov 		hw_mds_disable = 0;
12087355a02bSKonstantin Belousov 		mds_handler = mds_handler_void;
12097355a02bSKonstantin Belousov 	}
12107355a02bSKonstantin Belousov }
12117355a02bSKonstantin Belousov 
121248ec6d3bSKonstantin Belousov static void
121348ec6d3bSKonstantin Belousov hw_mds_recalculate_boot(void *arg __unused)
121448ec6d3bSKonstantin Belousov {
121548ec6d3bSKonstantin Belousov 
121648ec6d3bSKonstantin Belousov 	hw_mds_recalculate();
121748ec6d3bSKonstantin Belousov }
121848ec6d3bSKonstantin Belousov SYSINIT(mds_recalc, SI_SUB_SMP, SI_ORDER_ANY, hw_mds_recalculate_boot, NULL);
121948ec6d3bSKonstantin Belousov 
12207355a02bSKonstantin Belousov static int
12217355a02bSKonstantin Belousov sysctl_mds_disable_handler(SYSCTL_HANDLER_ARGS)
12227355a02bSKonstantin Belousov {
12237355a02bSKonstantin Belousov 	int error, val;
12247355a02bSKonstantin Belousov 
12257355a02bSKonstantin Belousov 	val = hw_mds_disable;
12267355a02bSKonstantin Belousov 	error = sysctl_handle_int(oidp, &val, 0, req);
12277355a02bSKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
12287355a02bSKonstantin Belousov 		return (error);
12297355a02bSKonstantin Belousov 	if (val < 0 || val > 3)
12307355a02bSKonstantin Belousov 		return (EINVAL);
12317355a02bSKonstantin Belousov 	hw_mds_disable = val;
12327355a02bSKonstantin Belousov 	hw_mds_recalculate();
12337355a02bSKonstantin Belousov 	return (0);
12347355a02bSKonstantin Belousov }
12357355a02bSKonstantin Belousov 
12367355a02bSKonstantin Belousov SYSCTL_PROC(_hw, OID_AUTO, mds_disable, CTLTYPE_INT |
12377355a02bSKonstantin Belousov     CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
12387355a02bSKonstantin Belousov     sysctl_mds_disable_handler, "I",
12397355a02bSKonstantin Belousov     "Microarchitectural Data Sampling Mitigation "
1240a212f56dSPiotr Pawel Stefaniak     "(0 - off, 1 - on VERW, 2 - on SW, 3 - on AUTO)");
12417355a02bSKonstantin Belousov 
1242961aacb1SScott Long SYSCTL_PROC(_machdep_mitigations_mds, OID_AUTO, disable, CTLTYPE_INT |
1243961aacb1SScott Long     CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
1244961aacb1SScott Long     sysctl_mds_disable_handler, "I",
1245961aacb1SScott Long     "Microarchitectural Data Sampling Mitigation "
1246a212f56dSPiotr Pawel Stefaniak     "(0 - off, 1 - on VERW, 2 - on SW, 3 - on AUTO)");
1247e3721601SScott Long 
1248e3721601SScott Long /*
1249e3721601SScott Long  * Intel Transactional Memory Asynchronous Abort Mitigation
1250e3721601SScott Long  * CVE-2019-11135
1251e3721601SScott Long  */
1252e3721601SScott Long int x86_taa_enable;
1253e3721601SScott Long int x86_taa_state;
1254e3721601SScott Long enum {
1255184b15ffSScott Long 	TAA_NONE	= 0,	/* No mitigation enabled */
1256184b15ffSScott Long 	TAA_TSX_DISABLE	= 1,	/* Disable TSX via MSR */
1257184b15ffSScott Long 	TAA_VERW	= 2,	/* Use VERW mitigation */
1258184b15ffSScott Long 	TAA_AUTO	= 3,	/* Automatically select the mitigation */
1259184b15ffSScott Long 
1260184b15ffSScott Long 	/* The states below are not selectable by the operator */
1261184b15ffSScott Long 
1262184b15ffSScott Long 	TAA_TAA_UC	= 4,	/* Mitigation present in microcode */
1263184b15ffSScott Long 	TAA_NOT_PRESENT	= 5	/* TSX is not present */
1264e3721601SScott Long };
1265e3721601SScott Long 
1266e3721601SScott Long static void
1267e3721601SScott Long taa_set(bool enable, bool all)
1268e3721601SScott Long {
1269e3721601SScott Long 
1270fa83f689SKonstantin Belousov 	x86_msr_op(MSR_IA32_TSX_CTRL,
1271fa83f689SKonstantin Belousov 	    (enable ? MSR_OP_OR : MSR_OP_ANDNOT) |
1272d0bc4b46SKonstantin Belousov 	    (all ? MSR_OP_RENDEZVOUS_ALL : MSR_OP_LOCAL),
1273d0bc4b46SKonstantin Belousov 	    IA32_TSX_CTRL_RTM_DISABLE | IA32_TSX_CTRL_TSX_CPUID_CLEAR,
1274d0bc4b46SKonstantin Belousov 	    NULL);
1275e3721601SScott Long }
1276e3721601SScott Long 
1277e3721601SScott Long void
1278e3721601SScott Long x86_taa_recalculate(void)
1279e3721601SScott Long {
1280e3721601SScott Long 	static int taa_saved_mds_disable = 0;
1281e3721601SScott Long 	int taa_need = 0, taa_state = 0;
1282e3721601SScott Long 	int mds_disable = 0, need_mds_recalc = 0;
1283e3721601SScott Long 
1284e3721601SScott Long 	/* Check CPUID.07h.EBX.HLE and RTM for the presence of TSX */
1285e3721601SScott Long 	if ((cpu_stdext_feature & CPUID_STDEXT_HLE) == 0 ||
1286e3721601SScott Long 	    (cpu_stdext_feature & CPUID_STDEXT_RTM) == 0) {
1287e3721601SScott Long 		/* TSX is not present */
1288184b15ffSScott Long 		x86_taa_state = TAA_NOT_PRESENT;
1289e3721601SScott Long 		return;
1290e3721601SScott Long 	}
1291e3721601SScott Long 
1292e3721601SScott Long 	/* Check to see what mitigation options the CPU gives us */
1293e3721601SScott Long 	if (cpu_ia32_arch_caps & IA32_ARCH_CAP_TAA_NO) {
1294e3721601SScott Long 		/* CPU is not suseptible to TAA */
12950d423176SScott Long 		taa_need = TAA_TAA_UC;
1296e3721601SScott Long 	} else if (cpu_ia32_arch_caps & IA32_ARCH_CAP_TSX_CTRL) {
1297e3721601SScott Long 		/*
1298e3721601SScott Long 		 * CPU can turn off TSX.  This is the next best option
1299e3721601SScott Long 		 * if TAA_NO hardware mitigation isn't present
1300e3721601SScott Long 		 */
1301e3721601SScott Long 		taa_need = TAA_TSX_DISABLE;
1302e3721601SScott Long 	} else {
1303e3721601SScott Long 		/* No TSX/TAA specific remedies are available. */
1304e3721601SScott Long 		if (x86_taa_enable == TAA_TSX_DISABLE) {
1305e3721601SScott Long 			if (bootverbose)
1306e3721601SScott Long 				printf("TSX control not available\n");
1307e3721601SScott Long 			return;
1308e3721601SScott Long 		} else
1309e3721601SScott Long 			taa_need = TAA_VERW;
1310e3721601SScott Long 	}
1311e3721601SScott Long 
1312e3721601SScott Long 	/* Can we automatically take action, or are we being forced? */
1313e3721601SScott Long 	if (x86_taa_enable == TAA_AUTO)
1314e3721601SScott Long 		taa_state = taa_need;
1315e3721601SScott Long 	else
1316e3721601SScott Long 		taa_state = x86_taa_enable;
1317e3721601SScott Long 
1318e3721601SScott Long 	/* No state change, nothing to do */
1319e3721601SScott Long 	if (taa_state == x86_taa_state) {
1320e3721601SScott Long 		if (bootverbose)
1321e3721601SScott Long 			printf("No TSX change made\n");
1322e3721601SScott Long 		return;
1323e3721601SScott Long 	}
1324e3721601SScott Long 
1325e3721601SScott Long 	/* Does the MSR need to be turned on or off? */
1326e3721601SScott Long 	if (taa_state == TAA_TSX_DISABLE)
1327e3721601SScott Long 		taa_set(true, true);
1328e3721601SScott Long 	else if (x86_taa_state == TAA_TSX_DISABLE)
1329e3721601SScott Long 		taa_set(false, true);
1330e3721601SScott Long 
1331e3721601SScott Long 	/* Does MDS need to be set to turn on VERW? */
1332e3721601SScott Long 	if (taa_state == TAA_VERW) {
1333e3721601SScott Long 		taa_saved_mds_disable = hw_mds_disable;
1334e3721601SScott Long 		mds_disable = hw_mds_disable = 1;
1335e3721601SScott Long 		need_mds_recalc = 1;
1336e3721601SScott Long 	} else if (x86_taa_state == TAA_VERW) {
1337e3721601SScott Long 		mds_disable = hw_mds_disable = taa_saved_mds_disable;
1338e3721601SScott Long 		need_mds_recalc = 1;
1339e3721601SScott Long 	}
1340e3721601SScott Long 	if (need_mds_recalc) {
1341e3721601SScott Long 		hw_mds_recalculate();
1342e3721601SScott Long 		if (mds_disable != hw_mds_disable) {
1343e3721601SScott Long 			if (bootverbose)
1344e3721601SScott Long 				printf("Cannot change MDS state for TAA\n");
1345e3721601SScott Long 			/* Don't update our state */
1346e3721601SScott Long 			return;
1347e3721601SScott Long 		}
1348e3721601SScott Long 	}
1349e3721601SScott Long 
1350e3721601SScott Long 	x86_taa_state = taa_state;
1351e3721601SScott Long 	return;
1352e3721601SScott Long }
1353e3721601SScott Long 
1354e3721601SScott Long static void
1355e3721601SScott Long taa_recalculate_boot(void * arg __unused)
1356e3721601SScott Long {
1357e3721601SScott Long 
1358e3721601SScott Long 	x86_taa_recalculate();
1359e3721601SScott Long }
1360e3721601SScott Long SYSINIT(taa_recalc, SI_SUB_SMP, SI_ORDER_ANY, taa_recalculate_boot, NULL);
1361e3721601SScott Long 
13627029da5cSPawel Biernacki SYSCTL_NODE(_machdep_mitigations, OID_AUTO, taa,
13637029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1364e3721601SScott Long     "TSX Asynchronous Abort Mitigation");
1365e3721601SScott Long 
1366e3721601SScott Long static int
1367e3721601SScott Long sysctl_taa_handler(SYSCTL_HANDLER_ARGS)
1368e3721601SScott Long {
1369e3721601SScott Long 	int error, val;
1370e3721601SScott Long 
1371e3721601SScott Long 	val = x86_taa_enable;
1372e3721601SScott Long 	error = sysctl_handle_int(oidp, &val, 0, req);
1373e3721601SScott Long 	if (error != 0 || req->newptr == NULL)
1374e3721601SScott Long 		return (error);
1375e3721601SScott Long 	if (val < TAA_NONE || val > TAA_AUTO)
1376e3721601SScott Long 		return (EINVAL);
1377e3721601SScott Long 	x86_taa_enable = val;
1378e3721601SScott Long 	x86_taa_recalculate();
1379e3721601SScott Long 	return (0);
1380e3721601SScott Long }
1381e3721601SScott Long 
1382e3721601SScott Long SYSCTL_PROC(_machdep_mitigations_taa, OID_AUTO, enable, CTLTYPE_INT |
1383e3721601SScott Long     CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
1384e3721601SScott Long     sysctl_taa_handler, "I",
1385e3721601SScott Long     "TAA Mitigation enablement control "
1386a212f56dSPiotr Pawel Stefaniak     "(0 - off, 1 - disable TSX, 2 - VERW, 3 - on AUTO)");
1387e3721601SScott Long 
1388e3721601SScott Long static int
1389e3721601SScott Long sysctl_taa_state_handler(SYSCTL_HANDLER_ARGS)
1390e3721601SScott Long {
1391e3721601SScott Long 	const char *state;
1392e3721601SScott Long 
1393e3721601SScott Long 	switch (x86_taa_state) {
1394e3721601SScott Long 	case TAA_NONE:
1395e3721601SScott Long 		state = "inactive";
1396e3721601SScott Long 		break;
1397e3721601SScott Long 	case TAA_TSX_DISABLE:
1398e3721601SScott Long 		state = "TSX disabled";
1399e3721601SScott Long 		break;
1400e3721601SScott Long 	case TAA_VERW:
1401e3721601SScott Long 		state = "VERW";
1402e3721601SScott Long 		break;
1403184b15ffSScott Long 	case TAA_TAA_UC:
1404184b15ffSScott Long 		state = "Mitigated in microcode";
1405e3721601SScott Long 		break;
1406184b15ffSScott Long 	case TAA_NOT_PRESENT:
1407184b15ffSScott Long 		state = "TSX not present";
1408ee02bd9cSConrad Meyer 		break;
1409e3721601SScott Long 	default:
1410e3721601SScott Long 		state = "unknown";
1411e3721601SScott Long 	}
1412e3721601SScott Long 
1413e3721601SScott Long 	return (SYSCTL_OUT(req, state, strlen(state)));
1414e3721601SScott Long }
1415e3721601SScott Long 
1416e3721601SScott Long SYSCTL_PROC(_machdep_mitigations_taa, OID_AUTO, state,
1417e3721601SScott Long     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1418e3721601SScott Long     sysctl_taa_state_handler, "A",
1419e3721601SScott Long     "TAA Mitigation state");
1420e3721601SScott Long 
1421ea602083SKonstantin Belousov int __read_frequently cpu_flush_rsb_ctxsw;
1422ea602083SKonstantin Belousov SYSCTL_INT(_machdep_mitigations, OID_AUTO, flush_rsb_ctxsw,
1423ea602083SKonstantin Belousov     CTLFLAG_RW | CTLFLAG_NOFETCH, &cpu_flush_rsb_ctxsw, 0,
1424ea602083SKonstantin Belousov     "Flush Return Stack Buffer on context switch");
1425ea602083SKonstantin Belousov 
142617edf152SKonstantin Belousov SYSCTL_NODE(_machdep_mitigations, OID_AUTO, rngds,
142717edf152SKonstantin Belousov     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
142817edf152SKonstantin Belousov     "MCU Optimization, disable RDSEED mitigation");
142917edf152SKonstantin Belousov 
143017edf152SKonstantin Belousov int x86_rngds_mitg_enable = 1;
143117edf152SKonstantin Belousov void
143217edf152SKonstantin Belousov x86_rngds_mitg_recalculate(bool all_cpus)
143317edf152SKonstantin Belousov {
143417edf152SKonstantin Belousov 	if ((cpu_stdext_feature3 & CPUID_STDEXT3_MCUOPT) == 0)
143517edf152SKonstantin Belousov 		return;
143617edf152SKonstantin Belousov 	x86_msr_op(MSR_IA32_MCU_OPT_CTRL,
143717edf152SKonstantin Belousov 	    (x86_rngds_mitg_enable ? MSR_OP_OR : MSR_OP_ANDNOT) |
1438d0bc4b46SKonstantin Belousov 	    (all_cpus ? MSR_OP_RENDEZVOUS_ALL : MSR_OP_LOCAL),
1439d0bc4b46SKonstantin Belousov 	    IA32_RNGDS_MITG_DIS, NULL);
144017edf152SKonstantin Belousov }
144117edf152SKonstantin Belousov 
144217edf152SKonstantin Belousov static int
144317edf152SKonstantin Belousov sysctl_rngds_mitg_enable_handler(SYSCTL_HANDLER_ARGS)
144417edf152SKonstantin Belousov {
144517edf152SKonstantin Belousov 	int error, val;
144617edf152SKonstantin Belousov 
144717edf152SKonstantin Belousov 	val = x86_rngds_mitg_enable;
144817edf152SKonstantin Belousov 	error = sysctl_handle_int(oidp, &val, 0, req);
144917edf152SKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
145017edf152SKonstantin Belousov 		return (error);
145117edf152SKonstantin Belousov 	x86_rngds_mitg_enable = val;
145217edf152SKonstantin Belousov 	x86_rngds_mitg_recalculate(true);
145317edf152SKonstantin Belousov 	return (0);
145417edf152SKonstantin Belousov }
145517edf152SKonstantin Belousov SYSCTL_PROC(_machdep_mitigations_rngds, OID_AUTO, enable, CTLTYPE_INT |
145617edf152SKonstantin Belousov     CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
145717edf152SKonstantin Belousov     sysctl_rngds_mitg_enable_handler, "I",
145817edf152SKonstantin Belousov     "MCU Optimization, disabling RDSEED mitigation control "
1459a212f56dSPiotr Pawel Stefaniak     "(0 - mitigation disabled (RDSEED optimized), 1 - mitigation enabled)");
146017edf152SKonstantin Belousov 
146117edf152SKonstantin Belousov static int
146217edf152SKonstantin Belousov sysctl_rngds_state_handler(SYSCTL_HANDLER_ARGS)
146317edf152SKonstantin Belousov {
146417edf152SKonstantin Belousov 	const char *state;
146517edf152SKonstantin Belousov 
146617edf152SKonstantin Belousov 	if ((cpu_stdext_feature3 & CPUID_STDEXT3_MCUOPT) == 0) {
146717edf152SKonstantin Belousov 		state = "Not applicable";
146817edf152SKonstantin Belousov 	} else if (x86_rngds_mitg_enable == 0) {
146917edf152SKonstantin Belousov 		state = "RDSEED not serialized";
147017edf152SKonstantin Belousov 	} else {
147117edf152SKonstantin Belousov 		state = "Mitigated";
147217edf152SKonstantin Belousov 	}
147317edf152SKonstantin Belousov 	return (SYSCTL_OUT(req, state, strlen(state)));
147417edf152SKonstantin Belousov }
147517edf152SKonstantin Belousov SYSCTL_PROC(_machdep_mitigations_rngds, OID_AUTO, state,
147617edf152SKonstantin Belousov     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
147717edf152SKonstantin Belousov     sysctl_rngds_state_handler, "A",
147817edf152SKonstantin Belousov     "MCU Optimization state");
147917edf152SKonstantin Belousov 
1480ebaea1bcSOlivier Certner 
1481ebaea1bcSOlivier Certner /*
1482ebaea1bcSOlivier Certner  * Zenbleed.
1483ebaea1bcSOlivier Certner  *
1484ebaea1bcSOlivier Certner  * No corresponding errata is publicly listed.  AMD has issued a security
1485ebaea1bcSOlivier Certner  * bulletin (AMD-SB-7008), entitled "Cross-Process Information Leak".  This
1486ebaea1bcSOlivier Certner  * document lists (as of August 2023) platform firmware's availability target
1487ebaea1bcSOlivier Certner  * dates, with most being November/December 2023.  It will then be up to
1488ebaea1bcSOlivier Certner  * motherboard manufacturers to produce corresponding BIOS updates, which will
1489ebaea1bcSOlivier Certner  * happen with an inevitable lag.  Additionally, for a variety of reasons,
1490ebaea1bcSOlivier Certner  * operators might not be able to apply them everywhere due.  On the side of
1491ebaea1bcSOlivier Certner  * standalone CPU microcodes, no plans for availability have been published so
1492ebaea1bcSOlivier Certner  * far.  However, a developer appearing to be an AMD employee has hardcoded in
1493ebaea1bcSOlivier Certner  * Linux revision numbers of future microcodes that are presumed to fix the
1494ebaea1bcSOlivier Certner  * vulnerability.
1495ebaea1bcSOlivier Certner  *
1496ebaea1bcSOlivier Certner  * Given the stability issues encountered with early microcode releases for Rome
1497ebaea1bcSOlivier Certner  * (the only microcode publicly released so far) and the absence of official
1498ebaea1bcSOlivier Certner  * communication on standalone CPU microcodes, we have opted instead for
1499ebaea1bcSOlivier Certner  * matching by default all AMD Zen2 processors which, according to the
1500ebaea1bcSOlivier Certner  * vulnerability's discoverer, are all affected (see
1501ebaea1bcSOlivier Certner  * https://lock.cmpxchg8b.com/zenbleed.html).  This policy, also adopted by
1502ebaea1bcSOlivier Certner  * OpenBSD, may be overriden using the tunable/sysctl
1503ebaea1bcSOlivier Certner  * 'machdep.mitigations.zenbleed.enable'.  We might revise it later depending on
1504ebaea1bcSOlivier Certner  * official statements, microcode updates' public availability and community
1505ebaea1bcSOlivier Certner  * assessment that they actually fix the vulnerability without any instability
1506ebaea1bcSOlivier Certner  * side effects.
1507ebaea1bcSOlivier Certner  */
1508ebaea1bcSOlivier Certner 
1509ebaea1bcSOlivier Certner SYSCTL_NODE(_machdep_mitigations, OID_AUTO, zenbleed,
1510ebaea1bcSOlivier Certner     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1511ebaea1bcSOlivier Certner     "Zenbleed OS-triggered prevention (via chicken bit)");
1512ebaea1bcSOlivier Certner 
1513ebaea1bcSOlivier Certner /* 2 is auto, see below. */
1514ebaea1bcSOlivier Certner int zenbleed_enable = 2;
1515ebaea1bcSOlivier Certner 
1516ebaea1bcSOlivier Certner void
1517ebaea1bcSOlivier Certner zenbleed_sanitize_enable(void)
1518ebaea1bcSOlivier Certner {
1519ebaea1bcSOlivier Certner 	/* Default to auto (2). */
1520ebaea1bcSOlivier Certner 	if (zenbleed_enable < 0 || zenbleed_enable > 2)
1521ebaea1bcSOlivier Certner 		zenbleed_enable = 2;
1522ebaea1bcSOlivier Certner }
1523ebaea1bcSOlivier Certner 
1524ebaea1bcSOlivier Certner static bool
1525ebaea1bcSOlivier Certner zenbleed_chicken_bit_applicable(void)
1526ebaea1bcSOlivier Certner {
1527ebaea1bcSOlivier Certner 	/* Concerns only bare-metal AMD Zen2 processors. */
1528ebaea1bcSOlivier Certner 	return (cpu_vendor_id == CPU_VENDOR_AMD &&
1529ebaea1bcSOlivier Certner 	    CPUID_TO_FAMILY(cpu_id) == 0x17 &&
1530ebaea1bcSOlivier Certner 	    CPUID_TO_MODEL(cpu_id) >= 0x30 &&
1531ebaea1bcSOlivier Certner 	    vm_guest == VM_GUEST_NO);
1532ebaea1bcSOlivier Certner }
1533ebaea1bcSOlivier Certner 
1534ebaea1bcSOlivier Certner static bool
1535ebaea1bcSOlivier Certner zenbleed_chicken_bit_should_enable(void)
1536ebaea1bcSOlivier Certner {
1537ebaea1bcSOlivier Certner 	/*
1538ebaea1bcSOlivier Certner 	 * Obey tunable/sysctl.
1539ebaea1bcSOlivier Certner 	 *
1540ebaea1bcSOlivier Certner 	 * As explained above, currently, the automatic setting (2) and the "on"
1541ebaea1bcSOlivier Certner 	 * one (1) have the same effect.  In the future, we might additionally
1542ebaea1bcSOlivier Certner 	 * check for specific microcode revisions as part of the automatic
1543ebaea1bcSOlivier Certner 	 * determination.
1544ebaea1bcSOlivier Certner 	 */
1545ebaea1bcSOlivier Certner 	return (zenbleed_enable != 0);
1546ebaea1bcSOlivier Certner }
1547ebaea1bcSOlivier Certner 
1548ebaea1bcSOlivier Certner void
1549ebaea1bcSOlivier Certner zenbleed_check_and_apply(bool all_cpus)
1550ebaea1bcSOlivier Certner {
1551ebaea1bcSOlivier Certner 	bool set;
1552ebaea1bcSOlivier Certner 
1553ebaea1bcSOlivier Certner 	if (!zenbleed_chicken_bit_applicable())
1554ebaea1bcSOlivier Certner 		return;
1555ebaea1bcSOlivier Certner 
1556ebaea1bcSOlivier Certner 	set = zenbleed_chicken_bit_should_enable();
1557ebaea1bcSOlivier Certner 
1558ebaea1bcSOlivier Certner 	x86_msr_op(MSR_DE_CFG,
1559ebaea1bcSOlivier Certner 	    (set ? MSR_OP_OR : MSR_OP_ANDNOT) |
1560ebaea1bcSOlivier Certner 	    (all_cpus ? MSR_OP_RENDEZVOUS_ALL : MSR_OP_LOCAL),
1561ebaea1bcSOlivier Certner 	    DE_CFG_ZEN2_FP_BACKUP_FIX_BIT, NULL);
1562ebaea1bcSOlivier Certner }
1563ebaea1bcSOlivier Certner 
1564ebaea1bcSOlivier Certner static int
1565ebaea1bcSOlivier Certner sysctl_zenbleed_enable_handler(SYSCTL_HANDLER_ARGS)
1566ebaea1bcSOlivier Certner {
1567ebaea1bcSOlivier Certner 	int error, val;
1568ebaea1bcSOlivier Certner 
1569ebaea1bcSOlivier Certner 	val = zenbleed_enable;
1570ebaea1bcSOlivier Certner 	error = sysctl_handle_int(oidp, &val, 0, req);
1571ebaea1bcSOlivier Certner 	if (error != 0 || req->newptr == NULL)
1572ebaea1bcSOlivier Certner 		return (error);
1573ebaea1bcSOlivier Certner 	zenbleed_enable = val;
1574ebaea1bcSOlivier Certner 	zenbleed_sanitize_enable();
1575ebaea1bcSOlivier Certner 	zenbleed_check_and_apply(true);
1576ebaea1bcSOlivier Certner 	return (0);
1577ebaea1bcSOlivier Certner }
1578ebaea1bcSOlivier Certner SYSCTL_PROC(_machdep_mitigations_zenbleed, OID_AUTO, enable, CTLTYPE_INT |
1579ebaea1bcSOlivier Certner     CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
1580ebaea1bcSOlivier Certner     sysctl_zenbleed_enable_handler, "I",
1581ebaea1bcSOlivier Certner     "Enable Zenbleed OS-triggered mitigation (chicken bit) "
1582ebaea1bcSOlivier Certner     "(0: Force disable, 1: Force enable, 2: Automatic determination)");
1583ebaea1bcSOlivier Certner 
1584ebaea1bcSOlivier Certner static int
1585ebaea1bcSOlivier Certner sysctl_zenbleed_state_handler(SYSCTL_HANDLER_ARGS)
1586ebaea1bcSOlivier Certner {
1587ebaea1bcSOlivier Certner 	const char *state;
1588ebaea1bcSOlivier Certner 
1589ebaea1bcSOlivier Certner 	if (!zenbleed_chicken_bit_applicable())
1590ebaea1bcSOlivier Certner 		state = "Not applicable";
1591ebaea1bcSOlivier Certner 	else if (zenbleed_chicken_bit_should_enable())
1592ebaea1bcSOlivier Certner 		state = "Mitigation enabled";
1593ebaea1bcSOlivier Certner 	else
1594ebaea1bcSOlivier Certner 		state = "Mitigation disabled";
1595ebaea1bcSOlivier Certner 	return (SYSCTL_OUT(req, state, strlen(state)));
1596ebaea1bcSOlivier Certner }
1597ebaea1bcSOlivier Certner SYSCTL_PROC(_machdep_mitigations_zenbleed, OID_AUTO, state,
1598ebaea1bcSOlivier Certner     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1599ebaea1bcSOlivier Certner     sysctl_zenbleed_state_handler, "A",
1600ebaea1bcSOlivier Certner     "Zenbleed OS-triggered mitigation (chicken bit) state");
1601ebaea1bcSOlivier Certner 
1602ebaea1bcSOlivier Certner 
16038fbcc334SKonstantin Belousov /*
16048fbcc334SKonstantin Belousov  * Enable and restore kernel text write permissions.
16058fbcc334SKonstantin Belousov  * Callers must ensure that disable_wp()/restore_wp() are executed
16068fbcc334SKonstantin Belousov  * without rescheduling on the same core.
16078fbcc334SKonstantin Belousov  */
16088fbcc334SKonstantin Belousov bool
16098fbcc334SKonstantin Belousov disable_wp(void)
16108fbcc334SKonstantin Belousov {
16118fbcc334SKonstantin Belousov 	u_int cr0;
16128fbcc334SKonstantin Belousov 
16138fbcc334SKonstantin Belousov 	cr0 = rcr0();
16148fbcc334SKonstantin Belousov 	if ((cr0 & CR0_WP) == 0)
16158fbcc334SKonstantin Belousov 		return (false);
16168fbcc334SKonstantin Belousov 	load_cr0(cr0 & ~CR0_WP);
16178fbcc334SKonstantin Belousov 	return (true);
16188fbcc334SKonstantin Belousov }
16198fbcc334SKonstantin Belousov 
16208fbcc334SKonstantin Belousov void
16218fbcc334SKonstantin Belousov restore_wp(bool old_wp)
16228fbcc334SKonstantin Belousov {
16238fbcc334SKonstantin Belousov 
16248fbcc334SKonstantin Belousov 	if (old_wp)
16258fbcc334SKonstantin Belousov 		load_cr0(rcr0() | CR0_WP);
16268fbcc334SKonstantin Belousov }
16278fbcc334SKonstantin Belousov 
16287705dd4dSKonstantin Belousov bool
16297705dd4dSKonstantin Belousov acpi_get_fadt_bootflags(uint16_t *flagsp)
16307705dd4dSKonstantin Belousov {
16317705dd4dSKonstantin Belousov #ifdef DEV_ACPI
16327705dd4dSKonstantin Belousov 	ACPI_TABLE_FADT *fadt;
16337705dd4dSKonstantin Belousov 	vm_paddr_t physaddr;
16347705dd4dSKonstantin Belousov 
16357705dd4dSKonstantin Belousov 	physaddr = acpi_find_table(ACPI_SIG_FADT);
16367705dd4dSKonstantin Belousov 	if (physaddr == 0)
16377705dd4dSKonstantin Belousov 		return (false);
16387705dd4dSKonstantin Belousov 	fadt = acpi_map_table(physaddr, ACPI_SIG_FADT);
16397705dd4dSKonstantin Belousov 	if (fadt == NULL)
16407705dd4dSKonstantin Belousov 		return (false);
16417705dd4dSKonstantin Belousov 	*flagsp = fadt->BootFlags;
16427705dd4dSKonstantin Belousov 	acpi_unmap_table(fadt);
16437705dd4dSKonstantin Belousov 	return (true);
16447705dd4dSKonstantin Belousov #else
16457705dd4dSKonstantin Belousov 	return (false);
16467705dd4dSKonstantin Belousov #endif
16477705dd4dSKonstantin Belousov }
1648652ae7b1SAdam Fenn 
1649652ae7b1SAdam Fenn DEFINE_IFUNC(, uint64_t, rdtsc_ordered, (void))
1650652ae7b1SAdam Fenn {
1651652ae7b1SAdam Fenn 	bool cpu_is_amd = cpu_vendor_id == CPU_VENDOR_AMD ||
1652652ae7b1SAdam Fenn 	    cpu_vendor_id == CPU_VENDOR_HYGON;
1653652ae7b1SAdam Fenn 
1654652ae7b1SAdam Fenn 	if ((amd_feature & AMDID_RDTSCP) != 0)
1655652ae7b1SAdam Fenn 		return (rdtscp);
1656652ae7b1SAdam Fenn 	else if ((cpu_feature & CPUID_SSE2) != 0)
1657652ae7b1SAdam Fenn 		return (cpu_is_amd ? rdtsc_ordered_mfence :
1658652ae7b1SAdam Fenn 		    rdtsc_ordered_lfence);
1659652ae7b1SAdam Fenn 	else
1660652ae7b1SAdam Fenn 		return (rdtsc);
1661652ae7b1SAdam Fenn }
1662