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