cpu.c revision 1.15 1 /* $NetBSD: cpu.c,v 1.15 2008/01/04 15:55:30 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2000, 2006, 2007 The 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 * 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 NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1999 Stefan Grefen
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the NetBSD
53 * Foundation, Inc. and its contributors.
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
55 * contributors may be used to endorse or promote products derived
56 * from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
59 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.15 2008/01/04 15:55:30 yamt Exp $");
73
74 #include "opt_ddb.h"
75 #include "opt_multiprocessor.h"
76 #include "opt_mpbios.h" /* for MPDEBUG */
77 #include "opt_mtrr.h"
78
79 #include "lapic.h"
80 #include "ioapic.h"
81
82 #include <sys/param.h>
83 #include <sys/proc.h>
84 #include <sys/user.h>
85 #include <sys/systm.h>
86 #include <sys/device.h>
87 #include <sys/malloc.h>
88 #include <sys/cpu.h>
89 #include <sys/atomic.h>
90
91 #include <uvm/uvm_extern.h>
92
93 #include <machine/cpufunc.h>
94 #include <machine/cpuvar.h>
95 #include <machine/pmap.h>
96 #include <machine/vmparam.h>
97 #include <machine/mpbiosvar.h>
98 #include <machine/pcb.h>
99 #include <machine/specialreg.h>
100 #include <machine/segments.h>
101 #include <machine/gdt.h>
102 #include <machine/mtrr.h>
103 #include <machine/pio.h>
104
105 #ifdef i386
106 #include <machine/tlog.h>
107 #endif
108
109 #if NLAPIC > 0
110 #include <machine/apicvar.h>
111 #include <machine/i82489reg.h>
112 #include <machine/i82489var.h>
113 #endif
114
115 #if NIOAPIC > 0
116 #include <machine/i82093var.h>
117 #endif
118
119 #include <dev/ic/mc146818reg.h>
120 #include <i386/isa/nvram.h>
121 #include <dev/isa/isareg.h>
122
123 int cpu_match(struct device *, struct cfdata *, void *);
124 void cpu_attach(struct device *, struct device *, void *);
125
126 static bool cpu_suspend(device_t);
127 static bool cpu_resume(device_t);
128
129 struct cpu_softc {
130 struct device sc_dev; /* device tree glue */
131 struct cpu_info *sc_info; /* pointer to CPU info */
132 };
133
134 int mp_cpu_start(struct cpu_info *, paddr_t);
135 void mp_cpu_start_cleanup(struct cpu_info *);
136 const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
137 mp_cpu_start_cleanup };
138
139
140 CFATTACH_DECL(cpu, sizeof(struct cpu_softc),
141 cpu_match, cpu_attach, NULL, NULL);
142
143 /*
144 * Statically-allocated CPU info for the primary CPU (or the only
145 * CPU, on uniprocessors). The CPU info list is initialized to
146 * point at it.
147 */
148 #ifdef TRAPLOG
149 struct tlog tlog_primary;
150 #endif
151 struct cpu_info cpu_info_primary = {
152 .ci_dev = 0,
153 .ci_self = &cpu_info_primary,
154 .ci_idepth = -1,
155 .ci_curlwp = &lwp0,
156 #ifdef TRAPLOG
157 .ci_tlog_base = &tlog_primary,
158 #endif /* !TRAPLOG */
159 };
160
161 struct cpu_info *cpu_info_list = &cpu_info_primary;
162
163 static void cpu_set_tss_gates(struct cpu_info *);
164
165 #ifdef i386
166 static void tss_init(struct i386tss *, void *, void *);
167 #endif
168
169 #ifdef MULTIPROCESSOR
170 static void cpu_init_idle_lwp(struct cpu_info *);
171 #endif
172
173 uint32_t cpus_attached = 0;
174 uint32_t cpus_running = 0;
175
176 extern char x86_64_doubleflt_stack[];
177
178 bool x86_mp_online;
179 paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
180
181 static vaddr_t cmos_data_mapping;
182
183 #ifdef MULTIPROCESSOR
184 /*
185 * Array of CPU info structures. Must be statically-allocated because
186 * curproc, etc. are used early.
187 */
188 struct cpu_info *cpu_info[X86_MAXPROCS] = { &cpu_info_primary, };
189
190 void cpu_hatch(void *);
191 static void cpu_boot_secondary(struct cpu_info *ci);
192 static void cpu_start_secondary(struct cpu_info *ci);
193 static void cpu_copy_trampoline(void);
194
195 /*
196 * Runs once per boot once multiprocessor goo has been detected and
197 * the local APIC on the boot processor has been mapped.
198 *
199 * Called from lapic_boot_init() (from mpbios_scan()).
200 */
201 void
202 cpu_init_first(void)
203 {
204 int cpunum = lapic_cpu_number();
205
206 if (cpunum != 0) {
207 cpu_info[0] = NULL;
208 cpu_info[cpunum] = &cpu_info_primary;
209 }
210
211 cpu_info_primary.ci_cpuid = cpunum;
212 cpu_copy_trampoline();
213
214 cmos_data_mapping = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, UVM_KMF_VAONLY);
215 if (cmos_data_mapping == 0)
216 panic("No KVA for page 0");
217 pmap_kenter_pa(cmos_data_mapping, 0, VM_PROT_READ|VM_PROT_WRITE);
218 pmap_update(pmap_kernel());
219 }
220 #endif
221
222 int
223 cpu_match(struct device *parent, struct cfdata *match,
224 void *aux)
225 {
226
227 return 1;
228 }
229
230 static void
231 cpu_vm_init(struct cpu_info *ci)
232 {
233 int ncolors = 2, i;
234
235 for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
236 struct x86_cache_info *cai;
237 int tcolors;
238
239 cai = &ci->ci_cinfo[i];
240
241 tcolors = atop(cai->cai_totalsize);
242 switch(cai->cai_associativity) {
243 case 0xff:
244 tcolors = 1; /* fully associative */
245 break;
246 case 0:
247 case 1:
248 break;
249 default:
250 tcolors /= cai->cai_associativity;
251 }
252 ncolors = max(ncolors, tcolors);
253 }
254
255 /*
256 * Knowing the size of the largest cache on this CPU, re-color
257 * our pages.
258 */
259 if (ncolors <= uvmexp.ncolors)
260 return;
261 aprint_verbose("%s: %d page colors\n", ci->ci_dev->dv_xname, ncolors);
262 uvm_page_recolor(ncolors);
263 }
264
265
266 void
267 cpu_attach(struct device *parent, struct device *self, void *aux)
268 {
269 struct cpu_softc *sc = (void *) self;
270 struct cpu_attach_args *caa = aux;
271 struct cpu_info *ci;
272 #if defined(MULTIPROCESSOR)
273 int cpunum = caa->cpu_number;
274 #endif
275
276 /*
277 * If we're an Application Processor, allocate a cpu_info
278 * structure, otherwise use the primary's.
279 */
280 if (caa->cpu_role == CPU_ROLE_AP) {
281 aprint_naive(": Application Processor\n");
282 ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK);
283 memset(ci, 0, sizeof(*ci));
284 #if defined(MULTIPROCESSOR)
285 if (cpu_info[cpunum] != NULL) {
286 printf("\n");
287 panic("cpu at apic id %d already attached?", cpunum);
288 }
289 cpu_info[cpunum] = ci;
290 #endif
291 #ifdef TRAPLOG
292 ci->ci_tlog_base = malloc(sizeof(struct tlog),
293 M_DEVBUF, M_WAITOK);
294 #endif
295 } else {
296 aprint_naive(": %s Processor\n",
297 caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
298 ci = &cpu_info_primary;
299 #if defined(MULTIPROCESSOR)
300 if (cpunum != lapic_cpu_number()) {
301 printf("\n");
302 panic("%s: running CPU is at apic %d"
303 " instead of at expected %d",
304 sc->sc_dev.dv_xname, lapic_cpu_number(), cpunum);
305 }
306 #endif
307 }
308
309 ci->ci_self = ci;
310 sc->sc_info = ci;
311
312 ci->ci_dev = self;
313 ci->ci_apicid = caa->cpu_number;
314 #ifdef MULTIPROCESSOR
315 ci->ci_cpuid = ci->ci_apicid;
316 #else
317 ci->ci_cpuid = 0; /* False for APs, but they're not used anyway */
318 #endif
319 ci->ci_cpumask = (1 << ci->ci_cpuid);
320 ci->ci_func = caa->cpu_func;
321
322 if (caa->cpu_role == CPU_ROLE_AP) {
323 #ifdef MULTIPROCESSOR
324 int error;
325
326 error = mi_cpu_attach(ci);
327 if (error != 0) {
328 aprint_normal("\n");
329 aprint_error("%s: mi_cpu_attach failed with %d\n",
330 sc->sc_dev.dv_xname, error);
331 return;
332 }
333 #endif
334 #if defined(i386)
335 cpu_init_tss(ci);
336 #endif /* defined(i386) */
337 } else {
338 KASSERT(ci->ci_data.cpu_idlelwp != NULL);
339 }
340
341 pmap_reference(pmap_kernel());
342 ci->ci_pmap = pmap_kernel();
343 ci->ci_tlbstate = TLBSTATE_STALE;
344
345 /* further PCB init done later. */
346
347 switch (caa->cpu_role) {
348 case CPU_ROLE_SP:
349 aprint_normal(": (uniprocessor)\n");
350 atomic_or_32(&ci->ci_flags,
351 CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY);
352 cpu_intr_init(ci);
353 identifycpu(ci);
354 cpu_init(ci);
355 cpu_set_tss_gates(ci);
356 pmap_cpu_init_late(ci);
357 x86_errata();
358 break;
359
360 case CPU_ROLE_BP:
361 aprint_normal(": (boot processor)\n");
362 atomic_or_32(&ci->ci_flags,
363 CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY);
364 cpu_intr_init(ci);
365 identifycpu(ci);
366 cpu_init(ci);
367 cpu_set_tss_gates(ci);
368 pmap_cpu_init_late(ci);
369 #if NLAPIC > 0
370 /*
371 * Enable local apic
372 */
373 lapic_enable();
374 lapic_calibrate_timer(ci);
375 #endif
376 #if NIOAPIC > 0
377 ioapic_bsp_id = caa->cpu_number;
378 #endif
379 x86_errata();
380 break;
381
382 case CPU_ROLE_AP:
383 /*
384 * report on an AP
385 */
386 aprint_normal(": (application processor)\n");
387
388 #if defined(MULTIPROCESSOR)
389 cpu_intr_init(ci);
390 gdt_alloc_cpu(ci);
391 cpu_set_tss_gates(ci);
392 pmap_cpu_init_early(ci);
393 pmap_cpu_init_late(ci);
394 cpu_start_secondary(ci);
395 if (ci->ci_flags & CPUF_PRESENT) {
396 identifycpu(ci);
397 ci->ci_next = cpu_info_list->ci_next;
398 cpu_info_list->ci_next = ci;
399 }
400 #else
401 aprint_normal("%s: not started\n", sc->sc_dev.dv_xname);
402 #endif
403 break;
404
405 default:
406 printf("\n");
407 panic("unknown processor type??\n");
408 }
409 cpu_vm_init(ci);
410
411 cpus_attached |= ci->ci_cpumask;
412
413 if (!pmf_device_register(self, cpu_suspend, cpu_resume))
414 aprint_error_dev(self, "couldn't establish power handler\n");
415
416 #if defined(MULTIPROCESSOR)
417 if (mp_verbose) {
418 struct lwp *l = ci->ci_data.cpu_idlelwp;
419
420 aprint_verbose(
421 "%s: idle lwp at %p, idle sp at %p\n",
422 sc->sc_dev.dv_xname, l,
423 #ifdef i386
424 (void *)l->l_addr->u_pcb.pcb_esp
425 #else
426 (void *)l->l_addr->u_pcb.pcb_rsp
427 #endif
428 );
429 }
430 #endif
431 }
432
433 /*
434 * Initialize the processor appropriately.
435 */
436
437 void
438 cpu_init(struct cpu_info *ci)
439 {
440 /* configure the CPU if needed */
441 if (ci->cpu_setup != NULL)
442 (*ci->cpu_setup)(ci);
443
444 #ifdef i386
445 /*
446 * On a 486 or above, enable ring 0 write protection.
447 */
448 if (ci->ci_cpu_class >= CPUCLASS_486)
449 lcr0(rcr0() | CR0_WP);
450 #else
451 lcr0(rcr0() | CR0_WP);
452 #endif
453
454 /*
455 * On a P6 or above, enable global TLB caching if the
456 * hardware supports it.
457 */
458 if (cpu_feature & CPUID_PGE)
459 lcr4(rcr4() | CR4_PGE); /* enable global TLB caching */
460
461 /*
462 * If we have FXSAVE/FXRESTOR, use them.
463 */
464 if (cpu_feature & CPUID_FXSR) {
465 lcr4(rcr4() | CR4_OSFXSR);
466
467 /*
468 * If we have SSE/SSE2, enable XMM exceptions.
469 */
470 if (cpu_feature & (CPUID_SSE|CPUID_SSE2))
471 lcr4(rcr4() | CR4_OSXMMEXCPT);
472 }
473
474 #ifdef MTRR
475 /*
476 * On a P6 or above, initialize MTRR's if the hardware supports them.
477 */
478 if (cpu_feature & CPUID_MTRR) {
479 if ((ci->ci_flags & CPUF_AP) == 0)
480 i686_mtrr_init_first();
481 mtrr_init_cpu(ci);
482 }
483
484 #ifdef i386
485 if (strcmp((char *)(ci->ci_vendor), "AuthenticAMD") == 0) {
486 /*
487 * Must be a K6-2 Step >= 7 or a K6-III.
488 */
489 if (CPUID2FAMILY(ci->ci_signature) == 5) {
490 if (CPUID2MODEL(ci->ci_signature) > 8 ||
491 (CPUID2MODEL(ci->ci_signature) == 8 &&
492 CPUID2STEPPING(ci->ci_signature) >= 7)) {
493 mtrr_funcs = &k6_mtrr_funcs;
494 k6_mtrr_init_first();
495 mtrr_init_cpu(ci);
496 }
497 }
498 }
499 #endif /* i386 */
500 #endif /* MTRR */
501
502 atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
503 atomic_or_32(&cpus_running, ci->ci_cpumask);
504
505 #ifndef MULTIPROCESSOR
506 /* XXX */
507 x86_patch();
508 #endif
509 }
510
511 #ifdef MULTIPROCESSOR
512 void
513 cpu_boot_secondary_processors(void)
514 {
515 struct cpu_info *ci;
516 u_long i;
517
518 /* Now that we know the number of CPUs, patch the text segment. */
519 x86_patch();
520
521 for (i=0; i < X86_MAXPROCS; i++) {
522 ci = cpu_info[i];
523 if (ci == NULL)
524 continue;
525 if (ci->ci_data.cpu_idlelwp == NULL)
526 continue;
527 if ((ci->ci_flags & CPUF_PRESENT) == 0)
528 continue;
529 if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
530 continue;
531 cpu_boot_secondary(ci);
532 }
533
534 x86_mp_online = true;
535 }
536
537 static void
538 cpu_init_idle_lwp(struct cpu_info *ci)
539 {
540 struct lwp *l = ci->ci_data.cpu_idlelwp;
541 struct pcb *pcb = &l->l_addr->u_pcb;
542
543 pcb->pcb_cr0 = rcr0();
544 }
545
546 void
547 cpu_init_idle_lwps(void)
548 {
549 struct cpu_info *ci;
550 u_long i;
551
552 for (i = 0; i < X86_MAXPROCS; i++) {
553 ci = cpu_info[i];
554 if (ci == NULL)
555 continue;
556 if (ci->ci_data.cpu_idlelwp == NULL)
557 continue;
558 if ((ci->ci_flags & CPUF_PRESENT) == 0)
559 continue;
560 cpu_init_idle_lwp(ci);
561 }
562 }
563
564 void
565 cpu_start_secondary(struct cpu_info *ci)
566 {
567 int i;
568 extern paddr_t mp_pdirpa;
569
570 mp_pdirpa = pmap_init_tmp_pgtbl(mp_trampoline_paddr);
571
572 atomic_or_32(&ci->ci_flags, CPUF_AP);
573
574 aprint_debug("%s: starting\n", ci->ci_dev->dv_xname);
575
576 ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
577 CPU_STARTUP(ci, mp_trampoline_paddr);
578
579 /*
580 * wait for it to become ready
581 */
582 for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i>0;i--) {
583 i8254_delay(10);
584 }
585 if ((ci->ci_flags & CPUF_PRESENT) == 0) {
586 aprint_error("%s: failed to become ready\n",
587 ci->ci_dev->dv_xname);
588 #if defined(MPDEBUG) && defined(DDB)
589 printf("dropping into debugger; continue from here to resume boot\n");
590 Debugger();
591 #endif
592 }
593
594 CPU_START_CLEANUP(ci);
595 }
596
597 void
598 cpu_boot_secondary(struct cpu_info *ci)
599 {
600 int i;
601
602 atomic_or_32(&ci->ci_flags, CPUF_GO);
603 for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i>0;i--) {
604 i8254_delay(10);
605 }
606 if ((ci->ci_flags & CPUF_RUNNING) == 0) {
607 aprint_error("%s: failed to start\n", ci->ci_dev->dv_xname);
608 #if defined(MPDEBUG) && defined(DDB)
609 printf("dropping into debugger; continue from here to resume boot\n");
610 Debugger();
611 #endif
612 }
613 }
614
615 /*
616 * The CPU ends up here when its ready to run
617 * This is called from code in mptramp.s; at this point, we are running
618 * in the idle pcb/idle stack of the new CPU. When this function returns,
619 * this processor will enter the idle loop and start looking for work.
620 */
621 void
622 cpu_hatch(void *v)
623 {
624 struct cpu_info *ci = (struct cpu_info *)v;
625 int s, i;
626
627 #ifdef __x86_64__
628 cpu_init_msrs(ci, true);
629 #endif
630 cpu_probe_features(ci);
631 cpu_feature &= ci->ci_feature_flags;
632 cpu_feature2 &= ci->ci_feature2_flags;
633
634 KDASSERT((ci->ci_flags & CPUF_PRESENT) == 0);
635 atomic_or_32(&ci->ci_flags, CPUF_PRESENT);
636 while ((ci->ci_flags & CPUF_GO) == 0) {
637 /* Don't use delay, boot CPU may be patching the text. */
638 for (i = 10000; i != 0; i--)
639 x86_pause();
640 }
641
642 /* Beacuse the text may have been patched in x86_patch(). */
643 wbinvd();
644 x86_flush();
645
646 KASSERT((ci->ci_flags & CPUF_RUNNING) == 0);
647
648 lcr3(pmap_kernel()->pm_pdirpa);
649 curlwp->l_addr->u_pcb.pcb_cr3 = pmap_kernel()->pm_pdirpa;
650 lcr0(ci->ci_data.cpu_idlelwp->l_addr->u_pcb.pcb_cr0);
651 cpu_init_idt();
652 gdt_init_cpu(ci);
653 lapic_enable();
654 lapic_set_lvt();
655 lapic_initclocks();
656
657 #ifdef i386
658 npxinit(ci);
659 #else
660 fpuinit(ci);
661 #endif
662 lldt(GSYSSEL(GLDT_SEL, SEL_KPL));
663 #ifdef i386
664 ltr(ci->ci_tss_sel);
665 #endif
666
667 cpu_init(ci);
668 cpu_get_tsc_freq(ci);
669
670 s = splhigh();
671 #ifdef i386
672 lapic_tpr = 0;
673 #else
674 lcr8(0);
675 #endif
676 x86_enable_intr();
677 splx(s);
678 x86_errata();
679
680 aprint_debug("%s: CPU %ld running\n", ci->ci_dev->dv_xname,
681 (long)ci->ci_cpuid);
682 }
683
684 #if defined(DDB)
685
686 #include <ddb/db_output.h>
687 #include <machine/db_machdep.h>
688
689 /*
690 * Dump CPU information from ddb.
691 */
692 void
693 cpu_debug_dump(void)
694 {
695 struct cpu_info *ci;
696 CPU_INFO_ITERATOR cii;
697
698 db_printf("addr dev id flags ipis curproc fpcurproc\n");
699 for (CPU_INFO_FOREACH(cii, ci)) {
700 db_printf("%p %s %ld %x %x %10p %10p\n",
701 ci,
702 ci->ci_dev == NULL ? "BOOT" : ci->ci_dev->dv_xname,
703 (long)ci->ci_cpuid,
704 ci->ci_flags, ci->ci_ipis,
705 ci->ci_curlwp,
706 ci->ci_fpcurlwp);
707 }
708 }
709 #endif
710
711 static void
712 cpu_copy_trampoline(void)
713 {
714 /*
715 * Copy boot code.
716 */
717 extern u_char cpu_spinup_trampoline[];
718 extern u_char cpu_spinup_trampoline_end[];
719
720 vaddr_t mp_trampoline_vaddr;
721
722 mp_trampoline_vaddr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
723 UVM_KMF_VAONLY);
724
725 pmap_kenter_pa(mp_trampoline_vaddr, mp_trampoline_paddr,
726 VM_PROT_READ | VM_PROT_WRITE);
727 pmap_update(pmap_kernel());
728 memcpy((void *)mp_trampoline_vaddr,
729 cpu_spinup_trampoline,
730 cpu_spinup_trampoline_end-cpu_spinup_trampoline);
731
732 pmap_kremove(mp_trampoline_vaddr, PAGE_SIZE);
733 pmap_update(pmap_kernel());
734 uvm_km_free(kernel_map, mp_trampoline_vaddr, PAGE_SIZE, UVM_KMF_VAONLY);
735 }
736
737 #endif
738
739 #ifdef i386
740 static void
741 tss_init(struct i386tss *tss, void *stack, void *func)
742 {
743 memset(tss, 0, sizeof *tss);
744 tss->tss_esp0 = tss->tss_esp = (int)((char *)stack + USPACE - 16);
745 tss->tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
746 tss->__tss_cs = GSEL(GCODE_SEL, SEL_KPL);
747 tss->tss_fs = GSEL(GCPU_SEL, SEL_KPL);
748 tss->tss_gs = tss->__tss_es = tss->__tss_ds =
749 tss->__tss_ss = GSEL(GDATA_SEL, SEL_KPL);
750 tss->tss_cr3 = pmap_kernel()->pm_pdirpa;
751 tss->tss_esp = (int)((char *)stack + USPACE - 16);
752 tss->tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
753 tss->__tss_eflags = PSL_MBO | PSL_NT; /* XXX not needed? */
754 tss->__tss_eip = (int)func;
755 }
756
757 /* XXX */
758 #define IDTVEC(name) __CONCAT(X, name)
759 typedef void (vector)(void);
760 extern vector IDTVEC(tss_trap08);
761 #ifdef DDB
762 extern vector Xintrddbipi;
763 extern int ddb_vec;
764 #endif
765
766 static void
767 cpu_set_tss_gates(struct cpu_info *ci)
768 {
769 struct segment_descriptor sd;
770
771 ci->ci_doubleflt_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
772 UVM_KMF_WIRED);
773 tss_init(&ci->ci_doubleflt_tss, ci->ci_doubleflt_stack,
774 IDTVEC(tss_trap08));
775 setsegment(&sd, &ci->ci_doubleflt_tss, sizeof(struct i386tss) - 1,
776 SDT_SYS386TSS, SEL_KPL, 0, 0);
777 ci->ci_gdt[GTRAPTSS_SEL].sd = sd;
778 setgate(&idt[8], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
779 GSEL(GTRAPTSS_SEL, SEL_KPL));
780
781 #if defined(DDB) && defined(MULTIPROCESSOR)
782 /*
783 * Set up separate handler for the DDB IPI, so that it doesn't
784 * stomp on a possibly corrupted stack.
785 *
786 * XXX overwriting the gate set in db_machine_init.
787 * Should rearrange the code so that it's set only once.
788 */
789 ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
790 UVM_KMF_WIRED);
791 tss_init(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack, Xintrddbipi);
792
793 setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
794 SDT_SYS386TSS, SEL_KPL, 0, 0);
795 ci->ci_gdt[GIPITSS_SEL].sd = sd;
796
797 setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
798 GSEL(GIPITSS_SEL, SEL_KPL));
799 #endif
800 }
801 #else
802 static void
803 cpu_set_tss_gates(struct cpu_info *ci)
804 {
805
806 }
807 #endif /* i386 */
808
809 int
810 mp_cpu_start(struct cpu_info *ci, paddr_t target)
811 {
812 #if NLAPIC > 0
813 int error;
814 #endif
815 unsigned short dwordptr[2];
816
817 /*
818 * Bootstrap code must be addressable in real mode
819 * and it must be page aligned.
820 */
821 KASSERT(target < 0x10000 && target % PAGE_SIZE == 0);
822
823 /*
824 * "The BSP must initialize CMOS shutdown code to 0Ah ..."
825 */
826
827 outb(IO_RTC, NVRAM_RESET);
828 outb(IO_RTC+1, NVRAM_RESET_JUMP);
829
830 /*
831 * "and the warm reset vector (DWORD based at 40:67) to point
832 * to the AP startup code ..."
833 */
834
835 dwordptr[0] = 0;
836 dwordptr[1] = target >> 4;
837
838 memcpy((uint8_t *)(cmos_data_mapping + 0x467), dwordptr, 4);
839
840 #if NLAPIC > 0
841 /*
842 * ... prior to executing the following sequence:"
843 */
844
845 if (ci->ci_flags & CPUF_AP) {
846 if ((error = x86_ipi_init(ci->ci_apicid)) != 0)
847 return error;
848
849 i8254_delay(10000);
850
851 if (cpu_feature & CPUID_APIC) {
852
853 if ((error = x86_ipi(target / PAGE_SIZE,
854 ci->ci_apicid,
855 LAPIC_DLMODE_STARTUP)) != 0)
856 return error;
857 i8254_delay(200);
858
859 if ((error = x86_ipi(target / PAGE_SIZE,
860 ci->ci_apicid,
861 LAPIC_DLMODE_STARTUP)) != 0)
862 return error;
863 i8254_delay(200);
864 }
865 }
866 #endif
867 return 0;
868 }
869
870 void
871 mp_cpu_start_cleanup(struct cpu_info *ci)
872 {
873 /*
874 * Ensure the NVRAM reset byte contains something vaguely sane.
875 */
876
877 outb(IO_RTC, NVRAM_RESET);
878 outb(IO_RTC+1, NVRAM_RESET_RST);
879 }
880
881 #ifdef __x86_64__
882 typedef void (vector)(void);
883 extern vector Xsyscall, Xsyscall32;
884
885 void
886 cpu_init_msrs(struct cpu_info *ci, bool full)
887 {
888 wrmsr(MSR_STAR,
889 ((uint64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) |
890 ((uint64_t)LSEL(LSYSRETBASE_SEL, SEL_UPL) << 48));
891 wrmsr(MSR_LSTAR, (uint64_t)Xsyscall);
892 wrmsr(MSR_CSTAR, (uint64_t)Xsyscall32);
893 wrmsr(MSR_SFMASK, PSL_NT|PSL_T|PSL_I|PSL_C);
894
895 if (full) {
896 wrmsr(MSR_FSBASE, 0);
897 wrmsr(MSR_GSBASE, (u_int64_t)ci);
898 wrmsr(MSR_KERNELGSBASE, 0);
899 }
900
901 if (cpu_feature & CPUID_NOX)
902 wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE);
903 }
904 #endif /* __x86_64__ */
905
906 /* XXX joerg restructure and restart CPUs individually */
907 static bool
908 cpu_suspend(device_t dv)
909 {
910 struct cpu_softc *sc = device_private(dv);
911 struct cpu_info *ci = sc->sc_info;
912 int err;
913
914 if (ci->ci_flags & CPUF_PRIMARY)
915 return true;
916 if (ci->ci_data.cpu_idlelwp == NULL)
917 return true;
918 if ((ci->ci_flags & CPUF_PRESENT) == 0)
919 return true;
920
921 mutex_enter(&cpu_lock);
922 err = cpu_setonline(ci, false);
923 mutex_exit(&cpu_lock);
924 return err == 0;
925 }
926
927 static bool
928 cpu_resume(device_t dv)
929 {
930 struct cpu_softc *sc = device_private(dv);
931 struct cpu_info *ci = sc->sc_info;
932 int err;
933
934 if (ci->ci_flags & CPUF_PRIMARY)
935 return true;
936 if (ci->ci_data.cpu_idlelwp == NULL)
937 return true;
938 if ((ci->ci_flags & CPUF_PRESENT) == 0)
939 return true;
940
941 mutex_enter(&cpu_lock);
942 err = cpu_setonline(ci, true);
943 mutex_exit(&cpu_lock);
944
945 return err == 0;
946 }
947
948 void
949 cpu_get_tsc_freq(struct cpu_info *ci)
950 {
951 uint64_t last_tsc;
952 u_int junk[4];
953
954 if (ci->ci_feature_flags & CPUID_TSC) {
955 /* Serialize. */
956 x86_cpuid(0, junk);
957 last_tsc = rdtsc();
958 i8254_delay(100000);
959 ci->ci_tsc_freq = (rdtsc() - last_tsc) * 10;
960 }
961 }
962