1 /*-
2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1992 Terrence R. Lambert.
4 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * William Jolitz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
39 */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include "opt_acpi.h"
45 #include "opt_atpic.h"
46 #include "opt_cpu.h"
47 #include "opt_ddb.h"
48 #include "opt_inet.h"
49 #include "opt_isa.h"
50 #include "opt_kdb.h"
51 #include "opt_kstack_pages.h"
52 #include "opt_maxmem.h"
53 #include "opt_mp_watchdog.h"
54 #include "opt_platform.h"
55 #ifdef __i386__
56 #include "opt_apic.h"
57 #endif
58
59 #include <sys/param.h>
60 #include <sys/proc.h>
61 #include <sys/systm.h>
62 #include <sys/bus.h>
63 #include <sys/cpu.h>
64 #include <sys/domainset.h>
65 #include <sys/kdb.h>
66 #include <sys/kernel.h>
67 #include <sys/ktr.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/mutex.h>
71 #include <sys/pcpu.h>
72 #include <sys/rwlock.h>
73 #include <sys/sched.h>
74 #include <sys/smp.h>
75 #include <sys/sysctl.h>
76
77 #include <machine/clock.h>
78 #include <machine/cpu.h>
79 #include <machine/cputypes.h>
80 #include <machine/specialreg.h>
81 #include <machine/md_var.h>
82 #include <machine/mp_watchdog.h>
83 #include <machine/tss.h>
84 #ifdef SMP
85 #include <machine/smp.h>
86 #endif
87 #ifdef CPU_ELAN
88 #include <machine/elan_mmcr.h>
89 #endif
90 #include <x86/acpica_machdep.h>
91
92 #include <vm/vm.h>
93 #include <vm/vm_extern.h>
94 #include <vm/vm_kern.h>
95 #include <vm/vm_page.h>
96 #include <vm/vm_map.h>
97 #include <vm/vm_object.h>
98 #include <vm/vm_pager.h>
99 #include <vm/vm_param.h>
100
101 #include <isa/isareg.h>
102
103 #include <contrib/dev/acpica/include/acpi.h>
104
105 #define STATE_RUNNING 0x0
106 #define STATE_MWAIT 0x1
107 #define STATE_SLEEPING 0x2
108
109 #ifdef SMP
110 static u_int cpu_reset_proxyid;
111 static volatile u_int cpu_reset_proxy_active;
112 #endif
113
114
115 /*
116 * Machine dependent boot() routine
117 *
118 * I haven't seen anything to put here yet
119 * Possibly some stuff might be grafted back here from boot()
120 */
121 void
cpu_boot(int howto)122 cpu_boot(int howto)
123 {
124 }
125
126 /*
127 * Flush the D-cache for non-DMA I/O so that the I-cache can
128 * be made coherent later.
129 */
130 void
cpu_flush_dcache(void * ptr,size_t len)131 cpu_flush_dcache(void *ptr, size_t len)
132 {
133 /* Not applicable */
134 }
135
136 void
acpi_cpu_c1(void)137 acpi_cpu_c1(void)
138 {
139
140 __asm __volatile("sti; hlt");
141 }
142
143 /*
144 * Use mwait to pause execution while waiting for an interrupt or
145 * another thread to signal that there is more work.
146 *
147 * NOTE: Interrupts will cause a wakeup; however, this function does
148 * not enable interrupt handling. The caller is responsible to enable
149 * interrupts.
150 */
151 void
acpi_cpu_idle_mwait(uint32_t mwait_hint)152 acpi_cpu_idle_mwait(uint32_t mwait_hint)
153 {
154 int *state;
155 uint64_t v;
156
157 /*
158 * A comment in Linux patch claims that 'CPUs run faster with
159 * speculation protection disabled. All CPU threads in a core
160 * must disable speculation protection for it to be
161 * disabled. Disable it while we are idle so the other
162 * hyperthread can run fast.'
163 *
164 * XXXKIB. Software coordination mode should be supported,
165 * but all Intel CPUs provide hardware coordination.
166 */
167
168 state = (int *)PCPU_PTR(monitorbuf);
169 KASSERT(atomic_load_int(state) == STATE_SLEEPING,
170 ("cpu_mwait_cx: wrong monitorbuf state"));
171 atomic_store_int(state, STATE_MWAIT);
172 if (PCPU_GET(ibpb_set) || hw_ssb_active) {
173 v = rdmsr(MSR_IA32_SPEC_CTRL);
174 wrmsr(MSR_IA32_SPEC_CTRL, v & ~(IA32_SPEC_CTRL_IBRS |
175 IA32_SPEC_CTRL_STIBP | IA32_SPEC_CTRL_SSBD));
176 } else {
177 v = 0;
178 }
179 cpu_monitor(state, 0, 0);
180 if (atomic_load_int(state) == STATE_MWAIT)
181 cpu_mwait(MWAIT_INTRBREAK, mwait_hint);
182
183 /*
184 * SSB cannot be disabled while we sleep, or rather, if it was
185 * disabled, the sysctl thread will bind to our cpu to tweak
186 * MSR.
187 */
188 if (v != 0)
189 wrmsr(MSR_IA32_SPEC_CTRL, v);
190
191 /*
192 * We should exit on any event that interrupts mwait, because
193 * that event might be a wanted interrupt.
194 */
195 atomic_store_int(state, STATE_RUNNING);
196 }
197
198 /* Get current clock frequency for the given cpu id. */
199 int
cpu_est_clockrate(int cpu_id,uint64_t * rate)200 cpu_est_clockrate(int cpu_id, uint64_t *rate)
201 {
202 uint64_t tsc1, tsc2;
203 uint64_t acnt, mcnt, perf;
204 register_t reg;
205
206 if (pcpu_find(cpu_id) == NULL || rate == NULL)
207 return (EINVAL);
208 #ifdef __i386__
209 if ((cpu_feature & CPUID_TSC) == 0)
210 return (EOPNOTSUPP);
211 #endif
212
213 /*
214 * If TSC is P-state invariant and APERF/MPERF MSRs do not exist,
215 * DELAY(9) based logic fails.
216 */
217 if (tsc_is_invariant && !tsc_perf_stat)
218 return (EOPNOTSUPP);
219
220 #ifdef SMP
221 if (smp_cpus > 1) {
222 /* Schedule ourselves on the indicated cpu. */
223 thread_lock(curthread);
224 sched_bind(curthread, cpu_id);
225 thread_unlock(curthread);
226 }
227 #endif
228
229 /* Calibrate by measuring a short delay. */
230 reg = intr_disable();
231 if (tsc_is_invariant) {
232 wrmsr(MSR_MPERF, 0);
233 wrmsr(MSR_APERF, 0);
234 tsc1 = rdtsc();
235 DELAY(1000);
236 mcnt = rdmsr(MSR_MPERF);
237 acnt = rdmsr(MSR_APERF);
238 tsc2 = rdtsc();
239 intr_restore(reg);
240 perf = 1000 * acnt / mcnt;
241 *rate = (tsc2 - tsc1) * perf;
242 } else {
243 tsc1 = rdtsc();
244 DELAY(1000);
245 tsc2 = rdtsc();
246 intr_restore(reg);
247 *rate = (tsc2 - tsc1) * 1000;
248 }
249
250 #ifdef SMP
251 if (smp_cpus > 1) {
252 thread_lock(curthread);
253 sched_unbind(curthread);
254 thread_unlock(curthread);
255 }
256 #endif
257
258 return (0);
259 }
260
261 /*
262 * Shutdown the CPU as much as possible
263 */
264 void
cpu_halt(void)265 cpu_halt(void)
266 {
267 for (;;)
268 halt();
269 }
270
271 static void
cpu_reset_real(void)272 cpu_reset_real(void)
273 {
274 struct region_descriptor null_idt;
275 int b;
276
277 disable_intr();
278 #ifdef CPU_ELAN
279 if (elan_mmcr != NULL)
280 elan_mmcr->RESCFG = 1;
281 #endif
282 #ifdef __i386__
283 if (cpu == CPU_GEODE1100) {
284 /* Attempt Geode's own reset */
285 outl(0xcf8, 0x80009044ul);
286 outl(0xcfc, 0xf);
287 }
288 #endif
289 #if !defined(BROKEN_KEYBOARD_RESET)
290 /*
291 * Attempt to do a CPU reset via the keyboard controller,
292 * do not turn off GateA20, as any machine that fails
293 * to do the reset here would then end up in no man's land.
294 */
295 outb(IO_KBD + 4, 0xFE);
296 DELAY(500000); /* wait 0.5 sec to see if that did it */
297 #endif
298
299 /*
300 * Attempt to force a reset via the Reset Control register at
301 * I/O port 0xcf9. Bit 2 forces a system reset when it
302 * transitions from 0 to 1. Bit 1 selects the type of reset
303 * to attempt: 0 selects a "soft" reset, and 1 selects a
304 * "hard" reset. We try a "hard" reset. The first write sets
305 * bit 1 to select a "hard" reset and clears bit 2. The
306 * second write forces a 0 -> 1 transition in bit 2 to trigger
307 * a reset.
308 */
309 outb(0xcf9, 0x2);
310 outb(0xcf9, 0x6);
311 DELAY(500000); /* wait 0.5 sec to see if that did it */
312
313 /*
314 * Attempt to force a reset via the Fast A20 and Init register
315 * at I/O port 0x92. Bit 1 serves as an alternate A20 gate.
316 * Bit 0 asserts INIT# when set to 1. We are careful to only
317 * preserve bit 1 while setting bit 0. We also must clear bit
318 * 0 before setting it if it isn't already clear.
319 */
320 b = inb(0x92);
321 if (b != 0xff) {
322 if ((b & 0x1) != 0)
323 outb(0x92, b & 0xfe);
324 outb(0x92, b | 0x1);
325 DELAY(500000); /* wait 0.5 sec to see if that did it */
326 }
327
328 printf("No known reset method worked, attempting CPU shutdown\n");
329 DELAY(1000000); /* wait 1 sec for printf to complete */
330
331 /* Wipe the IDT. */
332 null_idt.rd_limit = 0;
333 null_idt.rd_base = 0;
334 lidt(&null_idt);
335
336 /* "good night, sweet prince .... <THUNK!>" */
337 breakpoint();
338
339 /* NOTREACHED */
340 while(1);
341 }
342
343 #ifdef SMP
344 static void
cpu_reset_proxy(void)345 cpu_reset_proxy(void)
346 {
347
348 cpu_reset_proxy_active = 1;
349 while (cpu_reset_proxy_active == 1)
350 ia32_pause(); /* Wait for other cpu to see that we've started */
351
352 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
353 DELAY(1000000);
354 cpu_reset_real();
355 }
356 #endif
357
358 void
cpu_reset(void)359 cpu_reset(void)
360 {
361 #ifdef SMP
362 cpuset_t map;
363 u_int cnt;
364
365 if (smp_started) {
366 map = all_cpus;
367 CPU_CLR(PCPU_GET(cpuid), &map);
368 CPU_NAND(&map, &stopped_cpus);
369 if (!CPU_EMPTY(&map)) {
370 printf("cpu_reset: Stopping other CPUs\n");
371 stop_cpus(map);
372 }
373
374 if (PCPU_GET(cpuid) != 0) {
375 cpu_reset_proxyid = PCPU_GET(cpuid);
376 cpustop_restartfunc = cpu_reset_proxy;
377 cpu_reset_proxy_active = 0;
378 printf("cpu_reset: Restarting BSP\n");
379
380 /* Restart CPU #0. */
381 CPU_SETOF(0, &started_cpus);
382 wmb();
383
384 cnt = 0;
385 while (cpu_reset_proxy_active == 0 && cnt < 10000000) {
386 ia32_pause();
387 cnt++; /* Wait for BSP to announce restart */
388 }
389 if (cpu_reset_proxy_active == 0) {
390 printf("cpu_reset: Failed to restart BSP\n");
391 } else {
392 cpu_reset_proxy_active = 2;
393 while (1)
394 ia32_pause();
395 /* NOTREACHED */
396 }
397 }
398
399 DELAY(1000000);
400 }
401 #endif
402 cpu_reset_real();
403 /* NOTREACHED */
404 }
405
406 bool
cpu_mwait_usable(void)407 cpu_mwait_usable(void)
408 {
409
410 return ((cpu_feature2 & CPUID2_MON) != 0 && ((cpu_mon_mwait_flags &
411 (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)) ==
412 (CPUID5_MON_MWAIT_EXT | CPUID5_MWAIT_INTRBREAK)));
413 }
414
415 void (*cpu_idle_hook)(sbintime_t) = NULL; /* ACPI idle hook. */
416 static int cpu_ident_amdc1e = 0; /* AMD C1E supported. */
417 static int idle_mwait = 1; /* Use MONITOR/MWAIT for short idle. */
418 SYSCTL_INT(_machdep, OID_AUTO, idle_mwait, CTLFLAG_RWTUN, &idle_mwait,
419 0, "Use MONITOR/MWAIT for short idle");
420
421 static void
cpu_idle_acpi(sbintime_t sbt)422 cpu_idle_acpi(sbintime_t sbt)
423 {
424 int *state;
425
426 state = (int *)PCPU_PTR(monitorbuf);
427 atomic_store_int(state, STATE_SLEEPING);
428
429 /* See comments in cpu_idle_hlt(). */
430 disable_intr();
431 if (sched_runnable())
432 enable_intr();
433 else if (cpu_idle_hook)
434 cpu_idle_hook(sbt);
435 else
436 acpi_cpu_c1();
437 atomic_store_int(state, STATE_RUNNING);
438 }
439
440 static void
cpu_idle_hlt(sbintime_t sbt)441 cpu_idle_hlt(sbintime_t sbt)
442 {
443 int *state;
444
445 state = (int *)PCPU_PTR(monitorbuf);
446 atomic_store_int(state, STATE_SLEEPING);
447
448 /*
449 * Since we may be in a critical section from cpu_idle(), if
450 * an interrupt fires during that critical section we may have
451 * a pending preemption. If the CPU halts, then that thread
452 * may not execute until a later interrupt awakens the CPU.
453 * To handle this race, check for a runnable thread after
454 * disabling interrupts and immediately return if one is
455 * found. Also, we must absolutely guarentee that hlt is
456 * the next instruction after sti. This ensures that any
457 * interrupt that fires after the call to disable_intr() will
458 * immediately awaken the CPU from hlt. Finally, please note
459 * that on x86 this works fine because of interrupts enabled only
460 * after the instruction following sti takes place, while IF is set
461 * to 1 immediately, allowing hlt instruction to acknowledge the
462 * interrupt.
463 */
464 disable_intr();
465 if (sched_runnable())
466 enable_intr();
467 else
468 acpi_cpu_c1();
469 atomic_store_int(state, STATE_RUNNING);
470 }
471
472 static void
cpu_idle_mwait(sbintime_t sbt)473 cpu_idle_mwait(sbintime_t sbt)
474 {
475 int *state;
476
477 state = (int *)PCPU_PTR(monitorbuf);
478 atomic_store_int(state, STATE_MWAIT);
479
480 /* See comments in cpu_idle_hlt(). */
481 disable_intr();
482 if (sched_runnable()) {
483 atomic_store_int(state, STATE_RUNNING);
484 enable_intr();
485 return;
486 }
487
488 cpu_monitor(state, 0, 0);
489 if (atomic_load_int(state) == STATE_MWAIT)
490 __asm __volatile("sti; mwait" : : "a" (MWAIT_C1), "c" (0));
491 else
492 enable_intr();
493 atomic_store_int(state, STATE_RUNNING);
494 }
495
496 static void
cpu_idle_spin(sbintime_t sbt)497 cpu_idle_spin(sbintime_t sbt)
498 {
499 int *state;
500 int i;
501
502 state = (int *)PCPU_PTR(monitorbuf);
503 atomic_store_int(state, STATE_RUNNING);
504
505 /*
506 * The sched_runnable() call is racy but as long as there is
507 * a loop missing it one time will have just a little impact if any
508 * (and it is much better than missing the check at all).
509 */
510 for (i = 0; i < 1000; i++) {
511 if (sched_runnable())
512 return;
513 cpu_spinwait();
514 }
515 }
516
517 /*
518 * C1E renders the local APIC timer dead, so we disable it by
519 * reading the Interrupt Pending Message register and clearing
520 * both C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
521 *
522 * Reference:
523 * "BIOS and Kernel Developer's Guide for AMD NPT Family 0Fh Processors"
524 * #32559 revision 3.00+
525 */
526 #define MSR_AMDK8_IPM 0xc0010055
527 #define AMDK8_SMIONCMPHALT (1ULL << 27)
528 #define AMDK8_C1EONCMPHALT (1ULL << 28)
529 #define AMDK8_CMPHALT (AMDK8_SMIONCMPHALT | AMDK8_C1EONCMPHALT)
530
531 void
cpu_probe_amdc1e(void)532 cpu_probe_amdc1e(void)
533 {
534
535 /*
536 * Detect the presence of C1E capability mostly on latest
537 * dual-cores (or future) k8 family.
538 */
539 if (cpu_vendor_id == CPU_VENDOR_AMD &&
540 (cpu_id & 0x00000f00) == 0x00000f00 &&
541 (cpu_id & 0x0fff0000) >= 0x00040000) {
542 cpu_ident_amdc1e = 1;
543 }
544 }
545
546 void (*cpu_idle_fn)(sbintime_t) = cpu_idle_acpi;
547
548 void
cpu_idle(int busy)549 cpu_idle(int busy)
550 {
551 uint64_t msr;
552 sbintime_t sbt = -1;
553
554 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d",
555 busy, curcpu);
556 #ifdef MP_WATCHDOG
557 ap_watchdog(PCPU_GET(cpuid));
558 #endif
559
560 /* If we are busy - try to use fast methods. */
561 if (busy) {
562 if ((cpu_feature2 & CPUID2_MON) && idle_mwait) {
563 cpu_idle_mwait(busy);
564 goto out;
565 }
566 }
567
568 /* If we have time - switch timers into idle mode. */
569 if (!busy) {
570 critical_enter();
571 sbt = cpu_idleclock();
572 }
573
574 /* Apply AMD APIC timer C1E workaround. */
575 if (cpu_ident_amdc1e && cpu_disable_c3_sleep) {
576 msr = rdmsr(MSR_AMDK8_IPM);
577 if (msr & AMDK8_CMPHALT)
578 wrmsr(MSR_AMDK8_IPM, msr & ~AMDK8_CMPHALT);
579 }
580
581 /* Call main idle method. */
582 cpu_idle_fn(sbt);
583
584 /* Switch timers back into active mode. */
585 if (!busy) {
586 cpu_activeclock();
587 critical_exit();
588 }
589 out:
590 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done",
591 busy, curcpu);
592 }
593
594 static int cpu_idle_apl31_workaround;
595 SYSCTL_INT(_machdep, OID_AUTO, idle_apl31, CTLFLAG_RW,
596 &cpu_idle_apl31_workaround, 0,
597 "Apollo Lake APL31 MWAIT bug workaround");
598
599 int
cpu_idle_wakeup(int cpu)600 cpu_idle_wakeup(int cpu)
601 {
602 int *state;
603
604 state = (int *)pcpu_find(cpu)->pc_monitorbuf;
605 switch (atomic_load_int(state)) {
606 case STATE_SLEEPING:
607 return (0);
608 case STATE_MWAIT:
609 atomic_store_int(state, STATE_RUNNING);
610 return (cpu_idle_apl31_workaround ? 0 : 1);
611 case STATE_RUNNING:
612 return (1);
613 default:
614 panic("bad monitor state");
615 return (1);
616 }
617 }
618
619 /*
620 * Ordered by speed/power consumption.
621 */
622 static struct {
623 void *id_fn;
624 char *id_name;
625 int id_cpuid2_flag;
626 } idle_tbl[] = {
627 { .id_fn = cpu_idle_spin, .id_name = "spin" },
628 { .id_fn = cpu_idle_mwait, .id_name = "mwait",
629 .id_cpuid2_flag = CPUID2_MON },
630 { .id_fn = cpu_idle_hlt, .id_name = "hlt" },
631 { .id_fn = cpu_idle_acpi, .id_name = "acpi" },
632 };
633
634 static int
idle_sysctl_available(SYSCTL_HANDLER_ARGS)635 idle_sysctl_available(SYSCTL_HANDLER_ARGS)
636 {
637 char *avail, *p;
638 int error;
639 int i;
640
641 avail = malloc(256, M_TEMP, M_WAITOK);
642 p = avail;
643 for (i = 0; i < nitems(idle_tbl); i++) {
644 if (idle_tbl[i].id_cpuid2_flag != 0 &&
645 (cpu_feature2 & idle_tbl[i].id_cpuid2_flag) == 0)
646 continue;
647 if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
648 cpu_idle_hook == NULL)
649 continue;
650 p += sprintf(p, "%s%s", p != avail ? ", " : "",
651 idle_tbl[i].id_name);
652 }
653 error = sysctl_handle_string(oidp, avail, 0, req);
654 free(avail, M_TEMP);
655 return (error);
656 }
657
658 SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD,
659 0, 0, idle_sysctl_available, "A", "list of available idle functions");
660
661 static bool
cpu_idle_selector(const char * new_idle_name)662 cpu_idle_selector(const char *new_idle_name)
663 {
664 int i;
665
666 for (i = 0; i < nitems(idle_tbl); i++) {
667 if (idle_tbl[i].id_cpuid2_flag != 0 &&
668 (cpu_feature2 & idle_tbl[i].id_cpuid2_flag) == 0)
669 continue;
670 if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
671 cpu_idle_hook == NULL)
672 continue;
673 if (strcmp(idle_tbl[i].id_name, new_idle_name))
674 continue;
675 cpu_idle_fn = idle_tbl[i].id_fn;
676 if (bootverbose)
677 printf("CPU idle set to %s\n", idle_tbl[i].id_name);
678 return (true);
679 }
680 return (false);
681 }
682
683 static int
cpu_idle_sysctl(SYSCTL_HANDLER_ARGS)684 cpu_idle_sysctl(SYSCTL_HANDLER_ARGS)
685 {
686 char buf[16], *p;
687 int error, i;
688
689 p = "unknown";
690 for (i = 0; i < nitems(idle_tbl); i++) {
691 if (idle_tbl[i].id_fn == cpu_idle_fn) {
692 p = idle_tbl[i].id_name;
693 break;
694 }
695 }
696 strncpy(buf, p, sizeof(buf));
697 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
698 if (error != 0 || req->newptr == NULL)
699 return (error);
700 return (cpu_idle_selector(buf) ? 0 : EINVAL);
701 }
702
703 SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
704 cpu_idle_sysctl, "A", "currently selected idle function");
705
706 static void
cpu_idle_tun(void * unused __unused)707 cpu_idle_tun(void *unused __unused)
708 {
709 char tunvar[16];
710
711 if (TUNABLE_STR_FETCH("machdep.idle", tunvar, sizeof(tunvar)))
712 cpu_idle_selector(tunvar);
713 else if (cpu_vendor_id == CPU_VENDOR_AMD &&
714 CPUID_TO_FAMILY(cpu_id) == 0x17 && CPUID_TO_MODEL(cpu_id) == 0x1) {
715 /* Ryzen erratas 1057, 1109. */
716 cpu_idle_selector("hlt");
717 idle_mwait = 0;
718 }
719
720 if (cpu_vendor_id == CPU_VENDOR_INTEL && cpu_id == 0x506c9) {
721 /*
722 * Apollo Lake errata APL31 (public errata APL30).
723 * Stores to the armed address range may not trigger
724 * MWAIT to resume execution. OS needs to use
725 * interrupts to wake processors from MWAIT-induced
726 * sleep states.
727 */
728 cpu_idle_apl31_workaround = 1;
729 }
730 TUNABLE_INT_FETCH("machdep.idle_apl31", &cpu_idle_apl31_workaround);
731 }
732 SYSINIT(cpu_idle_tun, SI_SUB_CPU, SI_ORDER_MIDDLE, cpu_idle_tun, NULL);
733
734 static int panic_on_nmi = 1;
735 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RWTUN,
736 &panic_on_nmi, 0,
737 "Panic on NMI raised by hardware failure");
738 int nmi_is_broadcast = 1;
739 SYSCTL_INT(_machdep, OID_AUTO, nmi_is_broadcast, CTLFLAG_RWTUN,
740 &nmi_is_broadcast, 0,
741 "Chipset NMI is broadcast");
742 #ifdef KDB
743 int kdb_on_nmi = 1;
744 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RWTUN,
745 &kdb_on_nmi, 0,
746 "Go to KDB on NMI with unknown source");
747 #endif
748
749 void
nmi_call_kdb(u_int cpu,u_int type,struct trapframe * frame)750 nmi_call_kdb(u_int cpu, u_int type, struct trapframe *frame)
751 {
752 bool claimed = false;
753
754 #ifdef DEV_ISA
755 /* machine/parity/power fail/"kitchen sink" faults */
756 if (isa_nmi(frame->tf_err)) {
757 claimed = true;
758 if (panic_on_nmi)
759 panic("NMI indicates hardware failure");
760 }
761 #endif /* DEV_ISA */
762 #ifdef KDB
763 if (!claimed && kdb_on_nmi) {
764 /*
765 * NMI can be hooked up to a pushbutton for debugging.
766 */
767 printf("NMI/cpu%d ... going to debugger\n", cpu);
768 kdb_trap(type, 0, frame);
769 }
770 #endif /* KDB */
771 }
772
773 void
nmi_handle_intr(u_int type,struct trapframe * frame)774 nmi_handle_intr(u_int type, struct trapframe *frame)
775 {
776
777 #ifdef SMP
778 if (nmi_is_broadcast) {
779 nmi_call_kdb_smp(type, frame);
780 return;
781 }
782 #endif
783 nmi_call_kdb(PCPU_GET(cpuid), type, frame);
784 }
785
786 int hw_ibrs_active;
787 int hw_ibrs_disable = 1;
788
789 SYSCTL_INT(_hw, OID_AUTO, ibrs_active, CTLFLAG_RD, &hw_ibrs_active, 0,
790 "Indirect Branch Restricted Speculation active");
791
792 void
hw_ibrs_recalculate(void)793 hw_ibrs_recalculate(void)
794 {
795 uint64_t v;
796
797 if ((cpu_ia32_arch_caps & IA32_ARCH_CAP_IBRS_ALL) != 0) {
798 if (hw_ibrs_disable) {
799 v = rdmsr(MSR_IA32_SPEC_CTRL);
800 v &= ~(uint64_t)IA32_SPEC_CTRL_IBRS;
801 wrmsr(MSR_IA32_SPEC_CTRL, v);
802 } else {
803 v = rdmsr(MSR_IA32_SPEC_CTRL);
804 v |= IA32_SPEC_CTRL_IBRS;
805 wrmsr(MSR_IA32_SPEC_CTRL, v);
806 }
807 return;
808 }
809 hw_ibrs_active = (cpu_stdext_feature3 & CPUID_STDEXT3_IBPB) != 0 &&
810 !hw_ibrs_disable;
811 }
812
813 static int
hw_ibrs_disable_handler(SYSCTL_HANDLER_ARGS)814 hw_ibrs_disable_handler(SYSCTL_HANDLER_ARGS)
815 {
816 int error, val;
817
818 val = hw_ibrs_disable;
819 error = sysctl_handle_int(oidp, &val, 0, req);
820 if (error != 0 || req->newptr == NULL)
821 return (error);
822 hw_ibrs_disable = val != 0;
823 hw_ibrs_recalculate();
824 return (0);
825 }
826 SYSCTL_PROC(_hw, OID_AUTO, ibrs_disable, CTLTYPE_INT | CTLFLAG_RWTUN |
827 CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0, hw_ibrs_disable_handler, "I",
828 "Disable Indirect Branch Restricted Speculation");
829
830 int hw_ssb_active;
831 int hw_ssb_disable;
832
833 SYSCTL_INT(_hw, OID_AUTO, spec_store_bypass_disable_active, CTLFLAG_RD,
834 &hw_ssb_active, 0,
835 "Speculative Store Bypass Disable active");
836
837 static void
hw_ssb_set_one(bool enable)838 hw_ssb_set_one(bool enable)
839 {
840 uint64_t v;
841
842 v = rdmsr(MSR_IA32_SPEC_CTRL);
843 if (enable)
844 v |= (uint64_t)IA32_SPEC_CTRL_SSBD;
845 else
846 v &= ~(uint64_t)IA32_SPEC_CTRL_SSBD;
847 wrmsr(MSR_IA32_SPEC_CTRL, v);
848 }
849
850 static void
hw_ssb_set(bool enable,bool for_all_cpus)851 hw_ssb_set(bool enable, bool for_all_cpus)
852 {
853 struct thread *td;
854 int bound_cpu, i, is_bound;
855
856 if ((cpu_stdext_feature3 & CPUID_STDEXT3_SSBD) == 0) {
857 hw_ssb_active = 0;
858 return;
859 }
860 hw_ssb_active = enable;
861 if (for_all_cpus) {
862 td = curthread;
863 thread_lock(td);
864 is_bound = sched_is_bound(td);
865 bound_cpu = td->td_oncpu;
866 CPU_FOREACH(i) {
867 sched_bind(td, i);
868 hw_ssb_set_one(enable);
869 }
870 if (is_bound)
871 sched_bind(td, bound_cpu);
872 else
873 sched_unbind(td);
874 thread_unlock(td);
875 } else {
876 hw_ssb_set_one(enable);
877 }
878 }
879
880 void
hw_ssb_recalculate(bool all_cpus)881 hw_ssb_recalculate(bool all_cpus)
882 {
883
884 switch (hw_ssb_disable) {
885 default:
886 hw_ssb_disable = 0;
887 /* FALLTHROUGH */
888 case 0: /* off */
889 hw_ssb_set(false, all_cpus);
890 break;
891 case 1: /* on */
892 hw_ssb_set(true, all_cpus);
893 break;
894 case 2: /* auto */
895 hw_ssb_set((cpu_ia32_arch_caps & IA32_ARCH_CAP_SSB_NO) != 0 ?
896 false : true, all_cpus);
897 break;
898 }
899 }
900
901 static int
hw_ssb_disable_handler(SYSCTL_HANDLER_ARGS)902 hw_ssb_disable_handler(SYSCTL_HANDLER_ARGS)
903 {
904 int error, val;
905
906 val = hw_ssb_disable;
907 error = sysctl_handle_int(oidp, &val, 0, req);
908 if (error != 0 || req->newptr == NULL)
909 return (error);
910 hw_ssb_disable = val;
911 hw_ssb_recalculate(true);
912 return (0);
913 }
914 SYSCTL_PROC(_hw, OID_AUTO, spec_store_bypass_disable, CTLTYPE_INT |
915 CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
916 hw_ssb_disable_handler, "I",
917 "Speculative Store Bypass Disable (0 - off, 1 - on, 2 - auto");
918
919 int hw_mds_disable;
920
921 /*
922 * Handler for Microarchitectural Data Sampling issues. Really not a
923 * pointer to C function: on amd64 the code must not change any CPU
924 * architectural state except possibly %rflags. Also, it is always
925 * called with interrupts disabled.
926 */
927 void mds_handler_void(void);
928 void mds_handler_verw(void);
929 void mds_handler_ivb(void);
930 void mds_handler_bdw(void);
931 void mds_handler_skl_sse(void);
932 void mds_handler_skl_avx(void);
933 void mds_handler_skl_avx512(void);
934 void mds_handler_silvermont(void);
935 void (*mds_handler)(void) = mds_handler_void;
936
937 static int
sysctl_hw_mds_disable_state_handler(SYSCTL_HANDLER_ARGS)938 sysctl_hw_mds_disable_state_handler(SYSCTL_HANDLER_ARGS)
939 {
940 const char *state;
941
942 if (mds_handler == mds_handler_void)
943 state = "inactive";
944 else if (mds_handler == mds_handler_verw)
945 state = "VERW";
946 else if (mds_handler == mds_handler_ivb)
947 state = "software IvyBridge";
948 else if (mds_handler == mds_handler_bdw)
949 state = "software Broadwell";
950 else if (mds_handler == mds_handler_skl_sse)
951 state = "software Skylake SSE";
952 else if (mds_handler == mds_handler_skl_avx)
953 state = "software Skylake AVX";
954 else if (mds_handler == mds_handler_skl_avx512)
955 state = "software Skylake AVX512";
956 else if (mds_handler == mds_handler_silvermont)
957 state = "software Silvermont";
958 else
959 state = "unknown";
960 return (SYSCTL_OUT(req, state, strlen(state)));
961 }
962
963 SYSCTL_PROC(_hw, OID_AUTO, mds_disable_state,
964 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
965 sysctl_hw_mds_disable_state_handler, "A",
966 "Microarchitectural Data Sampling Mitigation state");
967
968 _Static_assert(__offsetof(struct pcpu, pc_mds_tmp) % 64 == 0, "MDS AVX512");
969
970 void
hw_mds_recalculate(void)971 hw_mds_recalculate(void)
972 {
973 struct pcpu *pc;
974 vm_offset_t b64;
975 u_long xcr0;
976 int i;
977
978 /*
979 * Allow user to force VERW variant even if MD_CLEAR is not
980 * reported. For instance, hypervisor might unknowingly
981 * filter the cap out.
982 * For the similar reasons, and for testing, allow to enable
983 * mitigation even for RDCL_NO or MDS_NO caps.
984 */
985 if (cpu_vendor_id != CPU_VENDOR_INTEL || hw_mds_disable == 0 ||
986 ((cpu_ia32_arch_caps & (IA32_ARCH_CAP_RDCL_NO |
987 IA32_ARCH_CAP_MDS_NO)) != 0 && hw_mds_disable == 3)) {
988 mds_handler = mds_handler_void;
989 } else if (((cpu_stdext_feature3 & CPUID_STDEXT3_MD_CLEAR) != 0 &&
990 hw_mds_disable == 3) || hw_mds_disable == 1) {
991 mds_handler = mds_handler_verw;
992 } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
993 (CPUID_TO_MODEL(cpu_id) == 0x2e || CPUID_TO_MODEL(cpu_id) == 0x1e ||
994 CPUID_TO_MODEL(cpu_id) == 0x1f || CPUID_TO_MODEL(cpu_id) == 0x1a ||
995 CPUID_TO_MODEL(cpu_id) == 0x2f || CPUID_TO_MODEL(cpu_id) == 0x25 ||
996 CPUID_TO_MODEL(cpu_id) == 0x2c || CPUID_TO_MODEL(cpu_id) == 0x2d ||
997 CPUID_TO_MODEL(cpu_id) == 0x2a || CPUID_TO_MODEL(cpu_id) == 0x3e ||
998 CPUID_TO_MODEL(cpu_id) == 0x3a) &&
999 (hw_mds_disable == 2 || hw_mds_disable == 3)) {
1000 /*
1001 * Nehalem, SandyBridge, IvyBridge
1002 */
1003 CPU_FOREACH(i) {
1004 pc = pcpu_find(i);
1005 if (pc->pc_mds_buf == NULL) {
1006 pc->pc_mds_buf = malloc_domainset(672, M_TEMP,
1007 DOMAINSET_PREF(pc->pc_domain), M_WAITOK);
1008 bzero(pc->pc_mds_buf, 16);
1009 }
1010 }
1011 mds_handler = mds_handler_ivb;
1012 } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
1013 (CPUID_TO_MODEL(cpu_id) == 0x3f || CPUID_TO_MODEL(cpu_id) == 0x3c ||
1014 CPUID_TO_MODEL(cpu_id) == 0x45 || CPUID_TO_MODEL(cpu_id) == 0x46 ||
1015 CPUID_TO_MODEL(cpu_id) == 0x56 || CPUID_TO_MODEL(cpu_id) == 0x4f ||
1016 CPUID_TO_MODEL(cpu_id) == 0x47 || CPUID_TO_MODEL(cpu_id) == 0x3d) &&
1017 (hw_mds_disable == 2 || hw_mds_disable == 3)) {
1018 /*
1019 * Haswell, Broadwell
1020 */
1021 CPU_FOREACH(i) {
1022 pc = pcpu_find(i);
1023 if (pc->pc_mds_buf == NULL) {
1024 pc->pc_mds_buf = malloc_domainset(1536, M_TEMP,
1025 DOMAINSET_PREF(pc->pc_domain), M_WAITOK);
1026 bzero(pc->pc_mds_buf, 16);
1027 }
1028 }
1029 mds_handler = mds_handler_bdw;
1030 } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
1031 ((CPUID_TO_MODEL(cpu_id) == 0x55 && (cpu_id &
1032 CPUID_STEPPING) <= 5) ||
1033 CPUID_TO_MODEL(cpu_id) == 0x4e || CPUID_TO_MODEL(cpu_id) == 0x5e ||
1034 (CPUID_TO_MODEL(cpu_id) == 0x8e && (cpu_id &
1035 CPUID_STEPPING) <= 0xb) ||
1036 (CPUID_TO_MODEL(cpu_id) == 0x9e && (cpu_id &
1037 CPUID_STEPPING) <= 0xc)) &&
1038 (hw_mds_disable == 2 || hw_mds_disable == 3)) {
1039 /*
1040 * Skylake, KabyLake, CoffeeLake, WhiskeyLake,
1041 * CascadeLake
1042 */
1043 CPU_FOREACH(i) {
1044 pc = pcpu_find(i);
1045 if (pc->pc_mds_buf == NULL) {
1046 pc->pc_mds_buf = malloc_domainset(6 * 1024,
1047 M_TEMP, DOMAINSET_PREF(pc->pc_domain),
1048 M_WAITOK);
1049 b64 = (vm_offset_t)malloc_domainset(64 + 63,
1050 M_TEMP, DOMAINSET_PREF(pc->pc_domain),
1051 M_WAITOK);
1052 pc->pc_mds_buf64 = (void *)roundup2(b64, 64);
1053 bzero(pc->pc_mds_buf64, 64);
1054 }
1055 }
1056 xcr0 = rxcr(0);
1057 if ((xcr0 & XFEATURE_ENABLED_ZMM_HI256) != 0 &&
1058 (cpu_stdext_feature2 & CPUID_STDEXT_AVX512DQ) != 0)
1059 mds_handler = mds_handler_skl_avx512;
1060 else if ((xcr0 & XFEATURE_ENABLED_AVX) != 0 &&
1061 (cpu_feature2 & CPUID2_AVX) != 0)
1062 mds_handler = mds_handler_skl_avx;
1063 else
1064 mds_handler = mds_handler_skl_sse;
1065 } else if (CPUID_TO_FAMILY(cpu_id) == 0x6 &&
1066 ((CPUID_TO_MODEL(cpu_id) == 0x37 ||
1067 CPUID_TO_MODEL(cpu_id) == 0x4a ||
1068 CPUID_TO_MODEL(cpu_id) == 0x4c ||
1069 CPUID_TO_MODEL(cpu_id) == 0x4d ||
1070 CPUID_TO_MODEL(cpu_id) == 0x5a ||
1071 CPUID_TO_MODEL(cpu_id) == 0x5d ||
1072 CPUID_TO_MODEL(cpu_id) == 0x6e ||
1073 CPUID_TO_MODEL(cpu_id) == 0x65 ||
1074 CPUID_TO_MODEL(cpu_id) == 0x75 ||
1075 CPUID_TO_MODEL(cpu_id) == 0x1c ||
1076 CPUID_TO_MODEL(cpu_id) == 0x26 ||
1077 CPUID_TO_MODEL(cpu_id) == 0x27 ||
1078 CPUID_TO_MODEL(cpu_id) == 0x35 ||
1079 CPUID_TO_MODEL(cpu_id) == 0x36 ||
1080 CPUID_TO_MODEL(cpu_id) == 0x7a))) {
1081 /* Silvermont, Airmont */
1082 CPU_FOREACH(i) {
1083 pc = pcpu_find(i);
1084 if (pc->pc_mds_buf == NULL)
1085 pc->pc_mds_buf = malloc(256, M_TEMP, M_WAITOK);
1086 }
1087 mds_handler = mds_handler_silvermont;
1088 } else {
1089 hw_mds_disable = 0;
1090 mds_handler = mds_handler_void;
1091 }
1092 }
1093
1094 static void
hw_mds_recalculate_boot(void * arg __unused)1095 hw_mds_recalculate_boot(void *arg __unused)
1096 {
1097
1098 hw_mds_recalculate();
1099 }
1100 SYSINIT(mds_recalc, SI_SUB_SMP, SI_ORDER_ANY, hw_mds_recalculate_boot, NULL);
1101
1102 static int
sysctl_mds_disable_handler(SYSCTL_HANDLER_ARGS)1103 sysctl_mds_disable_handler(SYSCTL_HANDLER_ARGS)
1104 {
1105 int error, val;
1106
1107 val = hw_mds_disable;
1108 error = sysctl_handle_int(oidp, &val, 0, req);
1109 if (error != 0 || req->newptr == NULL)
1110 return (error);
1111 if (val < 0 || val > 3)
1112 return (EINVAL);
1113 hw_mds_disable = val;
1114 hw_mds_recalculate();
1115 return (0);
1116 }
1117
1118 SYSCTL_PROC(_hw, OID_AUTO, mds_disable, CTLTYPE_INT |
1119 CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, NULL, 0,
1120 sysctl_mds_disable_handler, "I",
1121 "Microarchitectural Data Sampling Mitigation "
1122 "(0 - off, 1 - on VERW, 2 - on SW, 3 - on AUTO");
1123
1124 /*
1125 * Enable and restore kernel text write permissions.
1126 * Callers must ensure that disable_wp()/restore_wp() are executed
1127 * without rescheduling on the same core.
1128 */
1129 bool
disable_wp(void)1130 disable_wp(void)
1131 {
1132 u_int cr0;
1133
1134 cr0 = rcr0();
1135 if ((cr0 & CR0_WP) == 0)
1136 return (false);
1137 load_cr0(cr0 & ~CR0_WP);
1138 return (true);
1139 }
1140
1141 void
restore_wp(bool old_wp)1142 restore_wp(bool old_wp)
1143 {
1144
1145 if (old_wp)
1146 load_cr0(rcr0() | CR0_WP);
1147 }
1148
1149 bool
acpi_get_fadt_bootflags(uint16_t * flagsp)1150 acpi_get_fadt_bootflags(uint16_t *flagsp)
1151 {
1152 #ifdef DEV_ACPI
1153 ACPI_TABLE_FADT *fadt;
1154 vm_paddr_t physaddr;
1155
1156 physaddr = acpi_find_table(ACPI_SIG_FADT);
1157 if (physaddr == 0)
1158 return (false);
1159 fadt = acpi_map_table(physaddr, ACPI_SIG_FADT);
1160 if (fadt == NULL)
1161 return (false);
1162 *flagsp = fadt->BootFlags;
1163 acpi_unmap_table(fadt);
1164 return (true);
1165 #else
1166 return (false);
1167 #endif
1168 }
1169