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