cpu.c revision 1.201 1 /* $NetBSD: cpu.c,v 1.201 2021/08/07 16:19:08 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2000-2020 NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bill Sommerfeld of RedBack Networks Inc, and by Andrew Doran.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1999 Stefan Grefen
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the NetBSD
46 * Foundation, Inc. and its contributors.
47 * 4. Neither the name of The NetBSD Foundation nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
52 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.201 2021/08/07 16:19:08 thorpej Exp $");
66
67 #include "opt_ddb.h"
68 #include "opt_mpbios.h" /* for MPDEBUG */
69 #include "opt_mtrr.h"
70 #include "opt_multiprocessor.h"
71 #include "opt_svs.h"
72
73 #include "lapic.h"
74 #include "ioapic.h"
75 #include "acpica.h"
76 #include "hpet.h"
77
78 #include <sys/param.h>
79 #include <sys/proc.h>
80 #include <sys/systm.h>
81 #include <sys/device.h>
82 #include <sys/cpu.h>
83 #include <sys/cpufreq.h>
84 #include <sys/idle.h>
85 #include <sys/atomic.h>
86 #include <sys/reboot.h>
87 #include <sys/csan.h>
88
89 #include <uvm/uvm.h>
90
91 #include "acpica.h" /* for NACPICA, for mp_verbose */
92
93 #include <x86/machdep.h>
94 #include <machine/cpufunc.h>
95 #include <machine/cpuvar.h>
96 #include <machine/pmap.h>
97 #include <machine/vmparam.h>
98 #if defined(MULTIPROCESSOR)
99 #include <machine/mpbiosvar.h>
100 #endif
101 #include <machine/mpconfig.h> /* for mp_verbose */
102 #include <machine/pcb.h>
103 #include <machine/specialreg.h>
104 #include <machine/segments.h>
105 #include <machine/gdt.h>
106 #include <machine/mtrr.h>
107 #include <machine/pio.h>
108 #include <machine/cpu_counter.h>
109
110 #include <x86/fpu.h>
111
112 #if NACPICA > 0
113 #include <dev/acpi/acpi_srat.h>
114 #endif
115
116 #if NLAPIC > 0
117 #include <machine/apicvar.h>
118 #include <machine/i82489reg.h>
119 #include <machine/i82489var.h>
120 #endif
121
122 #include <dev/ic/mc146818reg.h>
123 #include <dev/ic/hpetvar.h>
124 #include <i386/isa/nvram.h>
125 #include <dev/isa/isareg.h>
126
127 #include "tsc.h"
128
129 #ifndef XENPV
130 #include "hyperv.h"
131 #if NHYPERV > 0
132 #include <x86/x86/hypervvar.h>
133 #endif
134 #endif
135
136 #ifdef XEN
137 #include <xen/hypervisor.h>
138 #endif
139
140 static int cpu_match(device_t, cfdata_t, void *);
141 static void cpu_attach(device_t, device_t, void *);
142 static void cpu_defer(device_t);
143 static int cpu_rescan(device_t, const char *, const int *);
144 static void cpu_childdetached(device_t, device_t);
145 static bool cpu_stop(device_t);
146 static bool cpu_suspend(device_t, const pmf_qual_t *);
147 static bool cpu_resume(device_t, const pmf_qual_t *);
148 static bool cpu_shutdown(device_t, int);
149
150 struct cpu_softc {
151 device_t sc_dev; /* device tree glue */
152 struct cpu_info *sc_info; /* pointer to CPU info */
153 bool sc_wasonline;
154 };
155
156 #ifdef MULTIPROCESSOR
157 int mp_cpu_start(struct cpu_info *, paddr_t);
158 void mp_cpu_start_cleanup(struct cpu_info *);
159 const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
160 mp_cpu_start_cleanup };
161 #endif
162
163
164 CFATTACH_DECL2_NEW(cpu, sizeof(struct cpu_softc),
165 cpu_match, cpu_attach, NULL, NULL, cpu_rescan, cpu_childdetached);
166
167 /*
168 * Statically-allocated CPU info for the primary CPU (or the only
169 * CPU, on uniprocessors). The CPU info list is initialized to
170 * point at it.
171 */
172 struct cpu_info cpu_info_primary __aligned(CACHE_LINE_SIZE) = {
173 .ci_dev = 0,
174 .ci_self = &cpu_info_primary,
175 .ci_idepth = -1,
176 .ci_curlwp = &lwp0,
177 .ci_curldt = -1,
178 };
179
180 struct cpu_info *cpu_info_list = &cpu_info_primary;
181
182 #ifdef i386
183 void cpu_set_tss_gates(struct cpu_info *);
184 #endif
185
186 static void cpu_init_idle_lwp(struct cpu_info *);
187
188 uint32_t cpu_feature[7] __read_mostly; /* X86 CPUID feature bits */
189 /* [0] basic features cpuid.1:%edx
190 * [1] basic features cpuid.1:%ecx (CPUID2_xxx bits)
191 * [2] extended features cpuid:80000001:%edx
192 * [3] extended features cpuid:80000001:%ecx
193 * [4] VIA padlock features
194 * [5] structured extended features cpuid.7:%ebx
195 * [6] structured extended features cpuid.7:%ecx
196 */
197
198 #ifdef MULTIPROCESSOR
199 bool x86_mp_online;
200 paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
201 #endif
202 #if NLAPIC > 0
203 static vaddr_t cmos_data_mapping;
204 #endif
205 struct cpu_info *cpu_starting;
206
207 #ifdef MULTIPROCESSOR
208 void cpu_hatch(void *);
209 static void cpu_boot_secondary(struct cpu_info *ci);
210 static void cpu_start_secondary(struct cpu_info *ci);
211 #if NLAPIC > 0
212 static void cpu_copy_trampoline(paddr_t);
213 #endif
214 #endif /* MULTIPROCESSOR */
215
216 /*
217 * Runs once per boot once multiprocessor goo has been detected and
218 * the local APIC on the boot processor has been mapped.
219 *
220 * Called from lapic_boot_init() (from mpbios_scan()).
221 */
222 #if NLAPIC > 0
223 void
224 cpu_init_first(void)
225 {
226
227 cpu_info_primary.ci_cpuid = lapic_cpu_number();
228
229 cmos_data_mapping = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, UVM_KMF_VAONLY);
230 if (cmos_data_mapping == 0)
231 panic("No KVA for page 0");
232 pmap_kenter_pa(cmos_data_mapping, 0, VM_PROT_READ|VM_PROT_WRITE, 0);
233 pmap_update(pmap_kernel());
234 }
235 #endif
236
237 static int
238 cpu_match(device_t parent, cfdata_t match, void *aux)
239 {
240
241 return 1;
242 }
243
244 #ifdef __HAVE_PCPU_AREA
245 void
246 cpu_pcpuarea_init(struct cpu_info *ci)
247 {
248 struct vm_page *pg;
249 size_t i, npages;
250 vaddr_t base, va;
251 paddr_t pa;
252
253 CTASSERT(sizeof(struct pcpu_entry) % PAGE_SIZE == 0);
254
255 npages = sizeof(struct pcpu_entry) / PAGE_SIZE;
256 base = (vaddr_t)&pcpuarea->ent[cpu_index(ci)];
257
258 for (i = 0; i < npages; i++) {
259 pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
260 if (pg == NULL) {
261 panic("failed to allocate pcpu PA");
262 }
263
264 va = base + i * PAGE_SIZE;
265 pa = VM_PAGE_TO_PHYS(pg);
266
267 pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
268 }
269
270 pmap_update(pmap_kernel());
271 }
272 #endif
273
274 static void
275 cpu_vm_init(struct cpu_info *ci)
276 {
277 unsigned int ncolors = 2;
278
279 /*
280 * XXX: for AP's the cache info has not been initialized yet
281 * but that does not matter because uvm only pays attention at
282 * the maximum only. We should fix it once cpus have different
283 * cache sizes.
284 */
285 for (unsigned int i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
286 struct x86_cache_info *cai;
287 unsigned int tcolors;
288
289 cai = &ci->ci_cinfo[i];
290
291 tcolors = atop(cai->cai_totalsize);
292 switch (cai->cai_associativity) {
293 case 0xff:
294 tcolors = 1; /* fully associative */
295 break;
296 case 0:
297 case 1:
298 break;
299 default:
300 tcolors /= cai->cai_associativity;
301 }
302 if (tcolors <= ncolors)
303 continue;
304 ncolors = tcolors;
305 }
306
307 /*
308 * If the desired number of colors is not a power of
309 * two, it won't be good. Find the greatest power of
310 * two which is an even divisor of the number of colors,
311 * to preserve even coloring of pages.
312 */
313 if (ncolors & (ncolors - 1) ) {
314 unsigned int try, picked = 1;
315 for (try = 1; try < ncolors; try *= 2) {
316 if (ncolors % try == 0) picked = try;
317 }
318 if (picked == 1) {
319 panic("desired number of cache colors %u is "
320 " > 1, but not even!", ncolors);
321 }
322 ncolors = picked;
323 }
324
325 /*
326 * Knowing the size of the largest cache on this CPU, potentially
327 * re-color our pages.
328 */
329 aprint_debug_dev(ci->ci_dev, "%d page colors\n", ncolors);
330 uvm_page_recolor(ncolors);
331
332 pmap_tlb_cpu_init(ci);
333 #ifndef __HAVE_DIRECT_MAP
334 pmap_vpage_cpu_init(ci);
335 #endif
336 }
337
338 static void
339 cpu_attach(device_t parent, device_t self, void *aux)
340 {
341 struct cpu_softc *sc = device_private(self);
342 struct cpu_attach_args *caa = aux;
343 struct cpu_info *ci;
344 uintptr_t ptr;
345 #if NLAPIC > 0
346 int cpunum = caa->cpu_number;
347 #endif
348 static bool again;
349
350 sc->sc_dev = self;
351
352 if (ncpu > maxcpus) {
353 #ifndef _LP64
354 aprint_error(": too many CPUs, please use NetBSD/amd64\n");
355 #else
356 aprint_error(": too many CPUs\n");
357 #endif
358 return;
359 }
360
361 /*
362 * If we're an Application Processor, allocate a cpu_info
363 * structure, otherwise use the primary's.
364 */
365 if (caa->cpu_role == CPU_ROLE_AP) {
366 if ((boothowto & RB_MD1) != 0) {
367 aprint_error(": multiprocessor boot disabled\n");
368 if (!pmf_device_register(self, NULL, NULL))
369 aprint_error_dev(self,
370 "couldn't establish power handler\n");
371 return;
372 }
373 aprint_naive(": Application Processor\n");
374 ptr = (uintptr_t)uvm_km_alloc(kernel_map,
375 sizeof(*ci) + CACHE_LINE_SIZE - 1, 0,
376 UVM_KMF_WIRED|UVM_KMF_ZERO);
377 ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
378 ci->ci_curldt = -1;
379 } else {
380 aprint_naive(": %s Processor\n",
381 caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
382 ci = &cpu_info_primary;
383 #if NLAPIC > 0
384 if (cpunum != lapic_cpu_number()) {
385 /* XXX should be done earlier. */
386 uint32_t reg;
387 aprint_verbose("\n");
388 aprint_verbose_dev(self, "running CPU at apic %d"
389 " instead of at expected %d", lapic_cpu_number(),
390 cpunum);
391 reg = lapic_readreg(LAPIC_ID);
392 lapic_writereg(LAPIC_ID, (reg & ~LAPIC_ID_MASK) |
393 (cpunum << LAPIC_ID_SHIFT));
394 }
395 if (cpunum != lapic_cpu_number()) {
396 aprint_error_dev(self, "unable to reset apic id\n");
397 }
398 #endif
399 }
400
401 ci->ci_self = ci;
402 sc->sc_info = ci;
403 ci->ci_dev = self;
404 ci->ci_acpiid = caa->cpu_id;
405 ci->ci_cpuid = caa->cpu_number;
406 ci->ci_func = caa->cpu_func;
407 ci->ci_kfpu_spl = -1;
408 aprint_normal("\n");
409
410 /* Must be before mi_cpu_attach(). */
411 cpu_vm_init(ci);
412
413 if (caa->cpu_role == CPU_ROLE_AP) {
414 int error;
415
416 error = mi_cpu_attach(ci);
417 if (error != 0) {
418 aprint_error_dev(self,
419 "mi_cpu_attach failed with %d\n", error);
420 return;
421 }
422 #ifdef __HAVE_PCPU_AREA
423 cpu_pcpuarea_init(ci);
424 #endif
425 cpu_init_tss(ci);
426 } else {
427 KASSERT(ci->ci_data.cpu_idlelwp != NULL);
428 #if NACPICA > 0
429 /* Parse out NUMA info for cpu_identify(). */
430 acpisrat_init();
431 #endif
432 }
433
434 #ifdef SVS
435 cpu_svs_init(ci);
436 #endif
437
438 pmap_reference(pmap_kernel());
439 ci->ci_pmap = pmap_kernel();
440 ci->ci_tlbstate = TLBSTATE_STALE;
441
442 /*
443 * Boot processor may not be attached first, but the below
444 * must be done to allow booting other processors.
445 */
446 if (!again) {
447 /* Make sure DELAY() (likely i8254_delay()) is initialized. */
448 DELAY(1);
449
450 /*
451 * Basic init. Compute an approximate frequency for the TSC
452 * using the i8254. If there's a HPET we'll redo it later.
453 */
454 atomic_or_32(&ci->ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
455 cpu_intr_init(ci);
456 tsc_setfunc(ci);
457 cpu_get_tsc_freq(ci);
458 cpu_init(ci);
459 #ifdef i386
460 cpu_set_tss_gates(ci);
461 #endif
462 pmap_cpu_init_late(ci);
463 #if NLAPIC > 0
464 if (caa->cpu_role != CPU_ROLE_SP) {
465 /* Enable lapic. */
466 lapic_enable();
467 lapic_set_lvt();
468 if (!vm_guest_is_xenpvh_or_pvhvm())
469 lapic_calibrate_timer(false);
470 }
471 #endif
472 kcsan_cpu_init(ci);
473 again = true;
474 }
475
476 /* further PCB init done later. */
477
478 switch (caa->cpu_role) {
479 case CPU_ROLE_SP:
480 atomic_or_32(&ci->ci_flags, CPUF_SP);
481 cpu_identify(ci);
482 x86_errata();
483 x86_cpu_idle_init();
484 #ifdef XENPVHVM
485 xen_hvm_init_cpu(ci);
486 #endif
487 break;
488
489 case CPU_ROLE_BP:
490 atomic_or_32(&ci->ci_flags, CPUF_BSP);
491 cpu_identify(ci);
492 x86_errata();
493 x86_cpu_idle_init();
494 #ifdef XENPVHVM
495 xen_hvm_init_cpu(ci);
496 #endif
497 break;
498
499 #ifdef MULTIPROCESSOR
500 case CPU_ROLE_AP:
501 /*
502 * report on an AP
503 */
504 cpu_intr_init(ci);
505 idt_vec_init_cpu_md(&ci->ci_idtvec, cpu_index(ci));
506 gdt_alloc_cpu(ci);
507 #ifdef i386
508 cpu_set_tss_gates(ci);
509 #endif
510 pmap_cpu_init_late(ci);
511 cpu_start_secondary(ci);
512 if (ci->ci_flags & CPUF_PRESENT) {
513 struct cpu_info *tmp;
514
515 cpu_identify(ci);
516 tmp = cpu_info_list;
517 while (tmp->ci_next)
518 tmp = tmp->ci_next;
519
520 tmp->ci_next = ci;
521 }
522 break;
523 #endif
524
525 default:
526 panic("unknown processor type??\n");
527 }
528
529 pat_init(ci);
530
531 if (!pmf_device_register1(self, cpu_suspend, cpu_resume, cpu_shutdown))
532 aprint_error_dev(self, "couldn't establish power handler\n");
533
534 #ifdef MULTIPROCESSOR
535 if (mp_verbose) {
536 struct lwp *l = ci->ci_data.cpu_idlelwp;
537 struct pcb *pcb = lwp_getpcb(l);
538
539 aprint_verbose_dev(self,
540 "idle lwp at %p, idle sp at %p\n",
541 l,
542 #ifdef i386
543 (void *)pcb->pcb_esp
544 #else
545 (void *)pcb->pcb_rsp
546 #endif
547 );
548 }
549 #endif
550
551 /*
552 * Postpone the "cpufeaturebus" scan.
553 * It is safe to scan the pseudo-bus
554 * only after all CPUs have attached.
555 */
556 (void)config_defer(self, cpu_defer);
557 }
558
559 static void
560 cpu_defer(device_t self)
561 {
562 cpu_rescan(self, NULL, NULL);
563 }
564
565 static int
566 cpu_rescan(device_t self, const char *ifattr, const int *locators)
567 {
568 struct cpu_softc *sc = device_private(self);
569 struct cpufeature_attach_args cfaa;
570 struct cpu_info *ci = sc->sc_info;
571
572 /*
573 * If we booted with RB_MD1 to disable multiprocessor, the
574 * auto-configuration data still contains the additional
575 * CPUs. But their initialization was mostly bypassed
576 * during attach, so we have to make sure we don't look at
577 * their featurebus info, since it wasn't retrieved.
578 */
579 if (ci == NULL)
580 return 0;
581
582 memset(&cfaa, 0, sizeof(cfaa));
583 cfaa.ci = ci;
584
585 if (ifattr_match(ifattr, "cpufeaturebus")) {
586 if (ci->ci_frequency == NULL) {
587 cfaa.name = "frequency";
588 ci->ci_frequency =
589 config_found(self, &cfaa, NULL,
590 CFARGS(.iattr = "cpufeaturebus"));
591 }
592
593 if (ci->ci_padlock == NULL) {
594 cfaa.name = "padlock";
595 ci->ci_padlock =
596 config_found(self, &cfaa, NULL,
597 CFARGS(.iattr = "cpufeaturebus"));
598 }
599
600 if (ci->ci_temperature == NULL) {
601 cfaa.name = "temperature";
602 ci->ci_temperature =
603 config_found(self, &cfaa, NULL,
604 CFARGS(.iattr = "cpufeaturebus"));
605 }
606
607 if (ci->ci_vm == NULL) {
608 cfaa.name = "vm";
609 ci->ci_vm =
610 config_found(self, &cfaa, NULL,
611 CFARGS(.iattr = "cpufeaturebus"));
612 }
613 }
614
615 return 0;
616 }
617
618 static void
619 cpu_childdetached(device_t self, device_t child)
620 {
621 struct cpu_softc *sc = device_private(self);
622 struct cpu_info *ci = sc->sc_info;
623
624 if (ci->ci_frequency == child)
625 ci->ci_frequency = NULL;
626
627 if (ci->ci_padlock == child)
628 ci->ci_padlock = NULL;
629
630 if (ci->ci_temperature == child)
631 ci->ci_temperature = NULL;
632
633 if (ci->ci_vm == child)
634 ci->ci_vm = NULL;
635 }
636
637 /*
638 * Initialize the processor appropriately.
639 */
640
641 void
642 cpu_init(struct cpu_info *ci)
643 {
644 extern int x86_fpu_save;
645 uint32_t cr4 = 0;
646
647 lcr0(rcr0() | CR0_WP);
648
649 /* If global TLB caching is supported, enable it */
650 if (cpu_feature[0] & CPUID_PGE)
651 cr4 |= CR4_PGE;
652
653 /*
654 * If we have FXSAVE/FXRESTOR, use them.
655 */
656 if (cpu_feature[0] & CPUID_FXSR) {
657 cr4 |= CR4_OSFXSR;
658
659 /*
660 * If we have SSE/SSE2, enable XMM exceptions.
661 */
662 if (cpu_feature[0] & (CPUID_SSE|CPUID_SSE2))
663 cr4 |= CR4_OSXMMEXCPT;
664 }
665
666 /* If xsave is supported, enable it */
667 if (cpu_feature[1] & CPUID2_XSAVE)
668 cr4 |= CR4_OSXSAVE;
669
670 /* If SMEP is supported, enable it */
671 if (cpu_feature[5] & CPUID_SEF_SMEP)
672 cr4 |= CR4_SMEP;
673
674 /* If SMAP is supported, enable it */
675 if (cpu_feature[5] & CPUID_SEF_SMAP)
676 cr4 |= CR4_SMAP;
677
678 #ifdef SVS
679 /* If PCID is supported, enable it */
680 if (svs_pcid)
681 cr4 |= CR4_PCIDE;
682 #endif
683
684 if (cr4) {
685 cr4 |= rcr4();
686 lcr4(cr4);
687 }
688
689 /*
690 * Changing CR4 register may change cpuid values. For example, setting
691 * CR4_OSXSAVE sets CPUID2_OSXSAVE. The CPUID2_OSXSAVE is in
692 * ci_feat_val[1], so update it.
693 * XXX Other than ci_feat_val[1] might be changed.
694 */
695 if (cpuid_level >= 1) {
696 u_int descs[4];
697
698 x86_cpuid(1, descs);
699 ci->ci_feat_val[1] = descs[2];
700 }
701
702 if (x86_fpu_save >= FPU_SAVE_FXSAVE) {
703 fpuinit_mxcsr_mask();
704 }
705
706 /* If xsave is enabled, enable all fpu features */
707 if (cr4 & CR4_OSXSAVE)
708 wrxcr(0, x86_xsave_features & XCR0_FPU);
709
710 #ifdef MTRR
711 /*
712 * On a P6 or above, initialize MTRR's if the hardware supports them.
713 */
714 if (cpu_feature[0] & CPUID_MTRR) {
715 if ((ci->ci_flags & CPUF_AP) == 0)
716 i686_mtrr_init_first();
717 mtrr_init_cpu(ci);
718 }
719
720 #ifdef i386
721 if (strcmp((char *)(ci->ci_vendor), "AuthenticAMD") == 0) {
722 /*
723 * Must be a K6-2 Step >= 7 or a K6-III.
724 */
725 if (CPUID_TO_FAMILY(ci->ci_signature) == 5) {
726 if (CPUID_TO_MODEL(ci->ci_signature) > 8 ||
727 (CPUID_TO_MODEL(ci->ci_signature) == 8 &&
728 CPUID_TO_STEPPING(ci->ci_signature) >= 7)) {
729 mtrr_funcs = &k6_mtrr_funcs;
730 k6_mtrr_init_first();
731 mtrr_init_cpu(ci);
732 }
733 }
734 }
735 #endif /* i386 */
736 #endif /* MTRR */
737
738 if (ci != &cpu_info_primary) {
739 /* Synchronize TSC */
740 atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
741 tsc_sync_ap(ci);
742 } else {
743 atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
744 }
745 }
746
747 #ifdef MULTIPROCESSOR
748 void
749 cpu_boot_secondary_processors(void)
750 {
751 struct cpu_info *ci;
752 kcpuset_t *cpus;
753 u_long i;
754
755 /* Now that we know the number of CPUs, patch the text segment. */
756 x86_patch(false);
757
758 #if NACPICA > 0
759 /* Finished with NUMA info for now. */
760 acpisrat_exit();
761 #endif
762
763 kcpuset_create(&cpus, true);
764 kcpuset_set(cpus, cpu_index(curcpu()));
765 for (i = 0; i < maxcpus; i++) {
766 ci = cpu_lookup(i);
767 if (ci == NULL)
768 continue;
769 if (ci->ci_data.cpu_idlelwp == NULL)
770 continue;
771 if ((ci->ci_flags & CPUF_PRESENT) == 0)
772 continue;
773 if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
774 continue;
775 cpu_boot_secondary(ci);
776 kcpuset_set(cpus, cpu_index(ci));
777 }
778 while (!kcpuset_match(cpus, kcpuset_running))
779 ;
780 kcpuset_destroy(cpus);
781
782 x86_mp_online = true;
783
784 /* Now that we know about the TSC, attach the timecounter. */
785 tsc_tc_init();
786 }
787 #endif
788
789 static void
790 cpu_init_idle_lwp(struct cpu_info *ci)
791 {
792 struct lwp *l = ci->ci_data.cpu_idlelwp;
793 struct pcb *pcb = lwp_getpcb(l);
794
795 pcb->pcb_cr0 = rcr0();
796 }
797
798 void
799 cpu_init_idle_lwps(void)
800 {
801 struct cpu_info *ci;
802 u_long i;
803
804 for (i = 0; i < maxcpus; i++) {
805 ci = cpu_lookup(i);
806 if (ci == NULL)
807 continue;
808 if (ci->ci_data.cpu_idlelwp == NULL)
809 continue;
810 if ((ci->ci_flags & CPUF_PRESENT) == 0)
811 continue;
812 cpu_init_idle_lwp(ci);
813 }
814 }
815
816 #ifdef MULTIPROCESSOR
817 void
818 cpu_start_secondary(struct cpu_info *ci)
819 {
820 u_long psl;
821 int i;
822
823 #if NLAPIC > 0
824 paddr_t mp_pdirpa;
825 mp_pdirpa = pmap_init_tmp_pgtbl(mp_trampoline_paddr);
826 cpu_copy_trampoline(mp_pdirpa);
827 #endif
828
829 atomic_or_32(&ci->ci_flags, CPUF_AP);
830 ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
831 if (CPU_STARTUP(ci, mp_trampoline_paddr) != 0) {
832 return;
833 }
834
835 /*
836 * Wait for it to become ready. Setting cpu_starting opens the
837 * initial gate and allows the AP to start soft initialization.
838 */
839 KASSERT(cpu_starting == NULL);
840 cpu_starting = ci;
841 for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i > 0; i--) {
842 delay_func(10);
843 }
844
845 if ((ci->ci_flags & CPUF_PRESENT) == 0) {
846 aprint_error_dev(ci->ci_dev, "failed to become ready\n");
847 #if defined(MPDEBUG) && defined(DDB)
848 printf("dropping into debugger; continue from here to resume boot\n");
849 Debugger();
850 #endif
851 } else {
852 /*
853 * Synchronize time stamp counters. Invalidate cache and do
854 * twice (in tsc_sync_bp) to minimize possible cache effects.
855 * Disable interrupts to try and rule out any external
856 * interference.
857 */
858 psl = x86_read_psl();
859 x86_disable_intr();
860 tsc_sync_bp(ci);
861 x86_write_psl(psl);
862 }
863
864 CPU_START_CLEANUP(ci);
865 cpu_starting = NULL;
866 }
867
868 void
869 cpu_boot_secondary(struct cpu_info *ci)
870 {
871 int64_t drift;
872 u_long psl;
873 int i;
874
875 atomic_or_32(&ci->ci_flags, CPUF_GO);
876 for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i > 0; i--) {
877 delay_func(10);
878 }
879 if ((ci->ci_flags & CPUF_RUNNING) == 0) {
880 aprint_error_dev(ci->ci_dev, "failed to start\n");
881 #if defined(MPDEBUG) && defined(DDB)
882 printf("dropping into debugger; continue from here to resume boot\n");
883 Debugger();
884 #endif
885 } else {
886 /* Synchronize TSC again, check for drift. */
887 drift = ci->ci_data.cpu_cc_skew;
888 psl = x86_read_psl();
889 x86_disable_intr();
890 tsc_sync_bp(ci);
891 x86_write_psl(psl);
892 drift -= ci->ci_data.cpu_cc_skew;
893 aprint_debug_dev(ci->ci_dev, "TSC skew=%lld drift=%lld\n",
894 (long long)ci->ci_data.cpu_cc_skew, (long long)drift);
895 tsc_sync_drift(drift);
896 }
897 }
898
899 /*
900 * The CPU ends up here when it's ready to run.
901 * This is called from code in mptramp.s; at this point, we are running
902 * in the idle pcb/idle stack of the new CPU. When this function returns,
903 * this processor will enter the idle loop and start looking for work.
904 */
905 void
906 cpu_hatch(void *v)
907 {
908 struct cpu_info *ci = (struct cpu_info *)v;
909 struct pcb *pcb;
910 int s, i;
911
912 /* ------------------------------------------------------------- */
913
914 /*
915 * This section of code must be compiled with SSP disabled, to
916 * prevent a race against cpu0. See sys/conf/ssp.mk.
917 */
918
919 cpu_init_msrs(ci, true);
920 cpu_probe(ci);
921 cpu_speculation_init(ci);
922 #if NHYPERV > 0
923 hyperv_init_cpu(ci);
924 #endif
925
926 ci->ci_data.cpu_cc_freq = cpu_info_primary.ci_data.cpu_cc_freq;
927 /* cpu_get_tsc_freq(ci); */
928
929 KDASSERT((ci->ci_flags & CPUF_PRESENT) == 0);
930
931 /*
932 * Synchronize the TSC for the first time. Note that interrupts are
933 * off at this point.
934 */
935 atomic_or_32(&ci->ci_flags, CPUF_PRESENT);
936 tsc_sync_ap(ci);
937
938 /* ------------------------------------------------------------- */
939
940 /*
941 * Wait to be brought online.
942 *
943 * Use MONITOR/MWAIT if available. These instructions put the CPU in
944 * a low consumption mode (C-state), and if the TSC is not invariant,
945 * this causes the TSC to drift. We want this to happen, so that we
946 * can later detect (in tsc_tc_init) any abnormal drift with invariant
947 * TSCs. That's just for safety; by definition such drifts should
948 * never occur with invariant TSCs.
949 *
950 * If not available, try PAUSE. We'd like to use HLT, but we have
951 * interrupts off.
952 */
953 while ((ci->ci_flags & CPUF_GO) == 0) {
954 if ((cpu_feature[1] & CPUID2_MONITOR) != 0) {
955 x86_monitor(&ci->ci_flags, 0, 0);
956 if ((ci->ci_flags & CPUF_GO) != 0) {
957 continue;
958 }
959 x86_mwait(0, 0);
960 } else {
961 /*
962 * XXX The loop repetition count could be a lot higher, but
963 * XXX currently qemu emulator takes a _very_long_time_ to
964 * XXX execute the pause instruction. So for now, use a low
965 * XXX value to allow the cpu to hatch before timing out.
966 */
967 for (i = 50; i != 0; i--) {
968 x86_pause();
969 }
970 }
971 }
972
973 /* Because the text may have been patched in x86_patch(). */
974 wbinvd();
975 x86_flush();
976 tlbflushg();
977
978 KASSERT((ci->ci_flags & CPUF_RUNNING) == 0);
979
980 #ifdef PAE
981 pd_entry_t * l3_pd = ci->ci_pae_l3_pdir;
982 for (i = 0 ; i < PDP_SIZE; i++) {
983 l3_pd[i] = pmap_kernel()->pm_pdirpa[i] | PTE_P;
984 }
985 lcr3(ci->ci_pae_l3_pdirpa);
986 #else
987 lcr3(pmap_pdirpa(pmap_kernel(), 0));
988 #endif
989
990 pcb = lwp_getpcb(curlwp);
991 pcb->pcb_cr3 = rcr3();
992 pcb = lwp_getpcb(ci->ci_data.cpu_idlelwp);
993 lcr0(pcb->pcb_cr0);
994
995 cpu_init_idt(ci);
996 gdt_init_cpu(ci);
997 #if NLAPIC > 0
998 lapic_enable();
999 lapic_set_lvt();
1000 #endif
1001
1002 fpuinit(ci);
1003 lldt(GSYSSEL(GLDT_SEL, SEL_KPL));
1004 ltr(ci->ci_tss_sel);
1005
1006 /*
1007 * cpu_init will re-synchronize the TSC, and will detect any abnormal
1008 * drift that would have been caused by the use of MONITOR/MWAIT
1009 * above.
1010 */
1011 cpu_init(ci);
1012 #ifdef XENPVHVM
1013 xen_hvm_init_cpu(ci);
1014 #endif
1015 (*x86_initclock_func)();
1016 cpu_get_tsc_freq(ci);
1017
1018 s = splhigh();
1019 #if NLAPIC > 0
1020 lapic_write_tpri(0);
1021 #endif
1022 x86_enable_intr();
1023 splx(s);
1024 x86_errata();
1025
1026 aprint_debug_dev(ci->ci_dev, "running\n");
1027
1028 kcsan_cpu_init(ci);
1029
1030 idle_loop(NULL);
1031 KASSERT(false);
1032 }
1033 #endif
1034
1035 #if defined(DDB)
1036
1037 #include <ddb/db_output.h>
1038 #include <machine/db_machdep.h>
1039
1040 /*
1041 * Dump CPU information from ddb.
1042 */
1043 void
1044 cpu_debug_dump(void)
1045 {
1046 struct cpu_info *ci;
1047 CPU_INFO_ITERATOR cii;
1048 const char sixtyfour64space[] =
1049 #ifdef _LP64
1050 " "
1051 #endif
1052 "";
1053
1054 db_printf("addr %sdev id flags ipis spl curlwp "
1055 "\n", sixtyfour64space);
1056 for (CPU_INFO_FOREACH(cii, ci)) {
1057 db_printf("%p %s %ld %x %x %d %10p\n",
1058 ci,
1059 ci->ci_dev == NULL ? "BOOT" : device_xname(ci->ci_dev),
1060 (long)ci->ci_cpuid,
1061 ci->ci_flags, ci->ci_ipis, ci->ci_ilevel,
1062 ci->ci_curlwp);
1063 }
1064 }
1065 #endif
1066
1067 #ifdef MULTIPROCESSOR
1068 #if NLAPIC > 0
1069 static void
1070 cpu_copy_trampoline(paddr_t pdir_pa)
1071 {
1072 extern uint32_t nox_flag;
1073 extern u_char cpu_spinup_trampoline[];
1074 extern u_char cpu_spinup_trampoline_end[];
1075 vaddr_t mp_trampoline_vaddr;
1076 struct {
1077 uint32_t large;
1078 uint32_t nox;
1079 uint32_t pdir;
1080 } smp_data;
1081 CTASSERT(sizeof(smp_data) == 3 * 4);
1082
1083 smp_data.large = (pmap_largepages != 0);
1084 smp_data.nox = nox_flag;
1085 smp_data.pdir = (uint32_t)(pdir_pa & 0xFFFFFFFF);
1086
1087 /* Enter the physical address */
1088 mp_trampoline_vaddr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
1089 UVM_KMF_VAONLY);
1090 pmap_kenter_pa(mp_trampoline_vaddr, mp_trampoline_paddr,
1091 VM_PROT_READ | VM_PROT_WRITE, 0);
1092 pmap_update(pmap_kernel());
1093
1094 /* Copy boot code */
1095 memcpy((void *)mp_trampoline_vaddr,
1096 cpu_spinup_trampoline,
1097 cpu_spinup_trampoline_end - cpu_spinup_trampoline);
1098
1099 /* Copy smp_data at the end */
1100 memcpy((void *)(mp_trampoline_vaddr + PAGE_SIZE - sizeof(smp_data)),
1101 &smp_data, sizeof(smp_data));
1102
1103 pmap_kremove(mp_trampoline_vaddr, PAGE_SIZE);
1104 pmap_update(pmap_kernel());
1105 uvm_km_free(kernel_map, mp_trampoline_vaddr, PAGE_SIZE, UVM_KMF_VAONLY);
1106 }
1107 #endif
1108
1109 int
1110 mp_cpu_start(struct cpu_info *ci, paddr_t target)
1111 {
1112 int error;
1113
1114 /*
1115 * Bootstrap code must be addressable in real mode
1116 * and it must be page aligned.
1117 */
1118 KASSERT(target < 0x10000 && target % PAGE_SIZE == 0);
1119
1120 /*
1121 * "The BSP must initialize CMOS shutdown code to 0Ah ..."
1122 */
1123
1124 outb(IO_RTC, NVRAM_RESET);
1125 outb(IO_RTC+1, NVRAM_RESET_JUMP);
1126
1127 #if NLAPIC > 0
1128 /*
1129 * "and the warm reset vector (DWORD based at 40:67) to point
1130 * to the AP startup code ..."
1131 */
1132 unsigned short dwordptr[2];
1133 dwordptr[0] = 0;
1134 dwordptr[1] = target >> 4;
1135
1136 memcpy((uint8_t *)cmos_data_mapping + 0x467, dwordptr, 4);
1137 #endif
1138
1139 if ((cpu_feature[0] & CPUID_APIC) == 0) {
1140 aprint_error("mp_cpu_start: CPU does not have APIC\n");
1141 return ENODEV;
1142 }
1143
1144 /*
1145 * ... prior to executing the following sequence:". We'll also add in
1146 * local cache flush, in case the BIOS has left the AP with its cache
1147 * disabled. It may not be able to cope with MP coherency.
1148 */
1149 wbinvd();
1150
1151 if (ci->ci_flags & CPUF_AP) {
1152 error = x86_ipi_init(ci->ci_cpuid);
1153 if (error != 0) {
1154 aprint_error_dev(ci->ci_dev, "%s: IPI not taken (1)\n",
1155 __func__);
1156 return error;
1157 }
1158 delay_func(10000);
1159
1160 error = x86_ipi_startup(ci->ci_cpuid, target / PAGE_SIZE);
1161 if (error != 0) {
1162 aprint_error_dev(ci->ci_dev, "%s: IPI not taken (2)\n",
1163 __func__);
1164 return error;
1165 }
1166 delay_func(200);
1167
1168 error = x86_ipi_startup(ci->ci_cpuid, target / PAGE_SIZE);
1169 if (error != 0) {
1170 aprint_error_dev(ci->ci_dev, "%s: IPI not taken (3)\n",
1171 __func__);
1172 return error;
1173 }
1174 delay_func(200);
1175 }
1176
1177 return 0;
1178 }
1179
1180 void
1181 mp_cpu_start_cleanup(struct cpu_info *ci)
1182 {
1183 /*
1184 * Ensure the NVRAM reset byte contains something vaguely sane.
1185 */
1186
1187 outb(IO_RTC, NVRAM_RESET);
1188 outb(IO_RTC+1, NVRAM_RESET_RST);
1189 }
1190 #endif
1191
1192 #ifdef __x86_64__
1193 typedef void (vector)(void);
1194 extern vector Xsyscall, Xsyscall32, Xsyscall_svs;
1195 #endif
1196
1197 void
1198 cpu_init_msrs(struct cpu_info *ci, bool full)
1199 {
1200 #ifdef __x86_64__
1201 wrmsr(MSR_STAR,
1202 ((uint64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) |
1203 ((uint64_t)LSEL(LSYSRETBASE_SEL, SEL_UPL) << 48));
1204 wrmsr(MSR_LSTAR, (uint64_t)Xsyscall);
1205 wrmsr(MSR_CSTAR, (uint64_t)Xsyscall32);
1206 wrmsr(MSR_SFMASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D|PSL_AC);
1207
1208 #ifdef SVS
1209 if (svs_enabled)
1210 wrmsr(MSR_LSTAR, (uint64_t)Xsyscall_svs);
1211 #endif
1212
1213 if (full) {
1214 wrmsr(MSR_FSBASE, 0);
1215 wrmsr(MSR_GSBASE, (uint64_t)ci);
1216 wrmsr(MSR_KERNELGSBASE, 0);
1217 }
1218 #endif /* __x86_64__ */
1219
1220 if (cpu_feature[2] & CPUID_NOX)
1221 wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE);
1222 }
1223
1224 void
1225 cpu_offline_md(void)
1226 {
1227 return;
1228 }
1229
1230 /* XXX joerg restructure and restart CPUs individually */
1231 static bool
1232 cpu_stop(device_t dv)
1233 {
1234 struct cpu_softc *sc = device_private(dv);
1235 struct cpu_info *ci = sc->sc_info;
1236 int err;
1237
1238 KASSERT((ci->ci_flags & CPUF_PRESENT) != 0);
1239
1240 if (CPU_IS_PRIMARY(ci))
1241 return true;
1242
1243 if (ci->ci_data.cpu_idlelwp == NULL)
1244 return true;
1245
1246 sc->sc_wasonline = !(ci->ci_schedstate.spc_flags & SPCF_OFFLINE);
1247
1248 if (sc->sc_wasonline) {
1249 mutex_enter(&cpu_lock);
1250 err = cpu_setstate(ci, false);
1251 mutex_exit(&cpu_lock);
1252
1253 if (err != 0)
1254 return false;
1255 }
1256
1257 return true;
1258 }
1259
1260 static bool
1261 cpu_suspend(device_t dv, const pmf_qual_t *qual)
1262 {
1263 struct cpu_softc *sc = device_private(dv);
1264 struct cpu_info *ci = sc->sc_info;
1265
1266 if ((ci->ci_flags & CPUF_PRESENT) == 0)
1267 return true;
1268 else {
1269 cpufreq_suspend(ci);
1270 }
1271
1272 return cpu_stop(dv);
1273 }
1274
1275 static bool
1276 cpu_resume(device_t dv, const pmf_qual_t *qual)
1277 {
1278 struct cpu_softc *sc = device_private(dv);
1279 struct cpu_info *ci = sc->sc_info;
1280 int err = 0;
1281
1282 if ((ci->ci_flags & CPUF_PRESENT) == 0)
1283 return true;
1284
1285 if (CPU_IS_PRIMARY(ci))
1286 goto out;
1287
1288 if (ci->ci_data.cpu_idlelwp == NULL)
1289 goto out;
1290
1291 if (sc->sc_wasonline) {
1292 mutex_enter(&cpu_lock);
1293 err = cpu_setstate(ci, true);
1294 mutex_exit(&cpu_lock);
1295 }
1296
1297 out:
1298 if (err != 0)
1299 return false;
1300
1301 cpufreq_resume(ci);
1302
1303 return true;
1304 }
1305
1306 static bool
1307 cpu_shutdown(device_t dv, int how)
1308 {
1309 struct cpu_softc *sc = device_private(dv);
1310 struct cpu_info *ci = sc->sc_info;
1311
1312 if ((ci->ci_flags & CPUF_BSP) != 0)
1313 return false;
1314
1315 if ((ci->ci_flags & CPUF_PRESENT) == 0)
1316 return true;
1317
1318 return cpu_stop(dv);
1319 }
1320
1321 /* Get the TSC frequency and set it to ci->ci_data.cpu_cc_freq. */
1322 void
1323 cpu_get_tsc_freq(struct cpu_info *ci)
1324 {
1325 uint64_t freq = 0, freq_from_cpuid, t0, t1;
1326 int64_t overhead;
1327
1328 if (CPU_IS_PRIMARY(ci) && cpu_hascounter()) {
1329 /*
1330 * If it's the first call of this function, try to get TSC
1331 * freq from CPUID by calling cpu_tsc_freq_cpuid().
1332 * The function also set lapic_per_second variable if it's
1333 * known. This is required for Intel's Comet Lake and newer
1334 * processors to set LAPIC timer correctly.
1335 */
1336 if (ci->ci_data.cpu_cc_freq == 0)
1337 freq = freq_from_cpuid = cpu_tsc_freq_cpuid(ci);
1338 #if NHPET > 0
1339 if (freq == 0)
1340 freq = hpet_tsc_freq();
1341 #endif
1342 if (freq == 0) {
1343 /*
1344 * Work out the approximate overhead involved below.
1345 * Discard the result of the first go around the
1346 * loop.
1347 */
1348 overhead = 0;
1349 for (int i = 0; i <= 8; i++) {
1350 t0 = cpu_counter();
1351 delay_func(0);
1352 t1 = cpu_counter();
1353 if (i > 0) {
1354 overhead += (t1 - t0);
1355 }
1356 }
1357 overhead >>= 3;
1358
1359 /* Now do the calibration. */
1360 t0 = cpu_counter();
1361 delay_func(100000);
1362 t1 = cpu_counter();
1363 freq = (t1 - t0 - overhead) * 10;
1364 }
1365 if (ci->ci_data.cpu_cc_freq != 0) {
1366 freq_from_cpuid = cpu_tsc_freq_cpuid(ci);
1367 if ((freq_from_cpuid != 0)
1368 && (freq != freq_from_cpuid))
1369 aprint_verbose_dev(ci->ci_dev, "TSC freq "
1370 "calibrated %" PRIu64 " Hz\n", freq);
1371 }
1372 } else {
1373 freq = cpu_info_primary.ci_data.cpu_cc_freq;
1374 }
1375
1376 ci->ci_data.cpu_cc_freq = freq;
1377 }
1378
1379 void
1380 x86_cpu_idle_mwait(void)
1381 {
1382 struct cpu_info *ci = curcpu();
1383
1384 KASSERT(ci->ci_ilevel == IPL_NONE);
1385
1386 x86_monitor(&ci->ci_want_resched, 0, 0);
1387 if (__predict_false(ci->ci_want_resched)) {
1388 return;
1389 }
1390 x86_mwait(0, 0);
1391 }
1392
1393 void
1394 x86_cpu_idle_halt(void)
1395 {
1396 struct cpu_info *ci = curcpu();
1397
1398 KASSERT(ci->ci_ilevel == IPL_NONE);
1399
1400 x86_disable_intr();
1401 if (!__predict_false(ci->ci_want_resched)) {
1402 x86_stihlt();
1403 } else {
1404 x86_enable_intr();
1405 }
1406 }
1407
1408 /*
1409 * Loads pmap for the current CPU.
1410 */
1411 void
1412 cpu_load_pmap(struct pmap *pmap, struct pmap *oldpmap)
1413 {
1414 #ifdef SVS
1415 if (svs_enabled) {
1416 svs_pdir_switch(pmap);
1417 }
1418 #endif
1419
1420 #ifdef PAE
1421 struct cpu_info *ci = curcpu();
1422 bool interrupts_enabled;
1423 pd_entry_t *l3_pd = ci->ci_pae_l3_pdir;
1424 int i;
1425
1426 /*
1427 * disable interrupts to block TLB shootdowns, which can reload cr3.
1428 * while this doesn't block NMIs, it's probably ok as NMIs unlikely
1429 * reload cr3.
1430 */
1431 interrupts_enabled = (x86_read_flags() & PSL_I) != 0;
1432 if (interrupts_enabled)
1433 x86_disable_intr();
1434
1435 for (i = 0 ; i < PDP_SIZE; i++) {
1436 l3_pd[i] = pmap->pm_pdirpa[i] | PTE_P;
1437 }
1438
1439 if (interrupts_enabled)
1440 x86_enable_intr();
1441 tlbflush();
1442 #else
1443 lcr3(pmap_pdirpa(pmap, 0));
1444 #endif
1445 }
1446
1447 /*
1448 * Notify all other cpus to halt.
1449 */
1450
1451 void
1452 cpu_broadcast_halt(void)
1453 {
1454 x86_broadcast_ipi(X86_IPI_HALT);
1455 }
1456
1457 /*
1458 * Send a dummy ipi to a cpu to force it to run splraise()/spllower(),
1459 * and trigger an AST on the running LWP.
1460 */
1461
1462 void
1463 cpu_kick(struct cpu_info *ci)
1464 {
1465 x86_send_ipi(ci, X86_IPI_AST);
1466 }
1467