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