cpu.c revision 1.40.2.4 1 /* $NetBSD: cpu.c,v 1.40.2.4 2010/10/22 07:21:44 uebayasi Exp $ */
2 /* NetBSD: cpu.c,v 1.18 2004/02/20 17:35:01 yamt Exp */
3
4 /*-
5 * Copyright (c) 2000 The NetBSD Foundation, Inc.
6 * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by RedBack Networks Inc.
11 *
12 * Author: Bill Sommerfeld
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * Copyright (c) 1999 Stefan Grefen
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the NetBSD
50 * Foundation, Inc. and its contributors.
51 * 4. Neither the name of The NetBSD Foundation nor the names of its
52 * contributors may be used to endorse or promote products derived
53 * from this software without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
56 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.40.2.4 2010/10/22 07:21:44 uebayasi Exp $");
70
71 #include "opt_ddb.h"
72 #include "opt_multiprocessor.h"
73 #include "opt_mpbios.h" /* for MPDEBUG */
74 #include "opt_mtrr.h"
75 #include "opt_xen.h"
76
77 #include "lapic.h"
78 #include "ioapic.h"
79
80 #include <sys/param.h>
81 #include <sys/proc.h>
82 #include <sys/systm.h>
83 #include <sys/device.h>
84 #include <sys/kmem.h>
85 #include <sys/cpu.h>
86 #include <sys/atomic.h>
87 #include <sys/reboot.h>
88
89 #include <uvm/uvm_extern.h>
90 #include <uvm/uvm_page.h>
91
92 #include <machine/cpufunc.h>
93 #include <machine/cpuvar.h>
94 #include <machine/pmap.h>
95 #include <machine/vmparam.h>
96 #include <machine/mpbiosvar.h>
97 #include <machine/pcb.h>
98 #include <machine/specialreg.h>
99 #include <machine/segments.h>
100 #include <machine/gdt.h>
101 #include <machine/mtrr.h>
102 #include <machine/pio.h>
103
104 #include <xen/vcpuvar.h>
105
106 #if NLAPIC > 0
107 #include <machine/apicvar.h>
108 #include <machine/i82489reg.h>
109 #include <machine/i82489var.h>
110 #endif
111
112 #include <dev/ic/mc146818reg.h>
113 #include <dev/isa/isareg.h>
114
115 #if MAXCPUS > 32
116 #error cpu_info contains 32bit bitmasks
117 #endif
118
119 int cpu_match(device_t, cfdata_t, void *);
120 void cpu_attach(device_t, device_t, void *);
121 int vcpu_match(device_t, cfdata_t, void *);
122 void vcpu_attach(device_t, device_t, void *);
123 void cpu_attach_common(device_t, device_t, void *);
124 void cpu_offline_md(void);
125
126 struct cpu_softc {
127 device_t sc_dev; /* device tree glue */
128 struct cpu_info *sc_info; /* pointer to CPU info */
129 bool sc_wasonline;
130 };
131
132 int mp_cpu_start(struct cpu_info *, paddr_t);
133 void mp_cpu_start_cleanup(struct cpu_info *);
134 const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
135 mp_cpu_start_cleanup };
136
137 CFATTACH_DECL_NEW(cpu, sizeof(struct cpu_softc),
138 cpu_match, cpu_attach, NULL, NULL);
139 CFATTACH_DECL_NEW(vcpu, sizeof(struct cpu_softc),
140 vcpu_match, vcpu_attach, NULL, NULL);
141
142 /*
143 * Statically-allocated CPU info for the primary CPU (or the only
144 * CPU, on uniprocessors). The CPU info list is initialized to
145 * point at it.
146 */
147 #ifdef TRAPLOG
148 #include <machine/tlog.h>
149 struct tlog tlog_primary;
150 #endif
151 struct cpu_info cpu_info_primary __aligned(CACHE_LINE_SIZE) = {
152 .ci_dev = 0,
153 .ci_self = &cpu_info_primary,
154 .ci_idepth = -1,
155 .ci_curlwp = &lwp0,
156 .ci_curldt = -1,
157 #ifdef TRAPLOG
158 .ci_tlog = &tlog_primary,
159 #endif
160
161 };
162 struct cpu_info phycpu_info_primary __aligned(CACHE_LINE_SIZE) = {
163 .ci_dev = 0,
164 .ci_self = &phycpu_info_primary,
165 };
166
167 struct cpu_info *cpu_info_list = &cpu_info_primary;
168 struct cpu_info *phycpu_info_list = &phycpu_info_primary;
169
170 static void cpu_set_tss_gates(struct cpu_info *ci);
171
172 uint32_t cpus_attached = 0;
173 uint32_t cpus_running = 0;
174
175 uint32_t phycpus_attached = 0;
176 uint32_t phycpus_running = 0;
177
178 uint32_t cpu_feature[5]; /* X86 CPUID feature bits
179 * [0] basic features %edx
180 * [1] basic features %ecx
181 * [2] extended features %edx
182 * [3] extended features %ecx
183 * [4] VIA padlock features
184 */
185
186 bool x86_mp_online;
187 paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
188
189 #if defined(MULTIPROCESSOR)
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
205 cpu_info_primary.ci_cpuid = lapic_cpu_number();
206 cpu_copy_trampoline();
207 }
208 #endif /* MULTIPROCESSOR */
209
210 int
211 cpu_match(device_t parent, cfdata_t match, void *aux)
212 {
213
214 return 1;
215 }
216
217 void
218 cpu_attach(device_t parent, device_t self, void *aux)
219 {
220 struct cpu_softc *sc = device_private(self);
221 struct cpu_attach_args *caa = aux;
222 struct cpu_info *ci;
223 uintptr_t ptr;
224 static bool again = false;
225
226 sc->sc_dev = self;
227
228 if (phycpus_attached == ~0) {
229 aprint_error(": increase MAXCPUS\n");
230 return;
231 }
232
233 /*
234 * If we're an Application Processor, allocate a cpu_info
235 * structure, otherwise use the primary's.
236 */
237 if (caa->cpu_role == CPU_ROLE_AP) {
238 if ((boothowto & RB_MD1) != 0) {
239 aprint_error(": multiprocessor boot disabled\n");
240 if (!pmf_device_register(self, NULL, NULL))
241 aprint_error_dev(self,
242 "couldn't establish power handler\n");
243 return;
244 }
245 aprint_naive(": Application Processor\n");
246 ptr = (uintptr_t)kmem_zalloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
247 KM_SLEEP);
248 ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
249 ci->ci_curldt = -1;
250 } else {
251 aprint_naive(": %s Processor\n",
252 caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
253 ci = &phycpu_info_primary;
254 }
255
256 ci->ci_self = ci;
257 sc->sc_info = ci;
258
259 ci->ci_dev = self;
260 ci->ci_cpuid = caa->cpu_number;
261 ci->ci_vcpu = NULL;
262
263 /*
264 * Boot processor may not be attached first, but the below
265 * must be done to allow booting other processors.
266 */
267 if (!again) {
268 atomic_or_32(&ci->ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
269 /* Basic init */
270 again = true;
271 }
272
273 printf(": ");
274 switch (caa->cpu_role) {
275 case CPU_ROLE_SP:
276 printf("(uniprocessor)\n");
277 atomic_or_32(&ci->ci_flags, CPUF_SP);
278 break;
279
280 case CPU_ROLE_BP:
281 printf("(boot processor)\n");
282 atomic_or_32(&ci->ci_flags, CPUF_BSP);
283 break;
284
285 case CPU_ROLE_AP:
286 /*
287 * report on an AP
288 */
289 printf("(application processor)\n");
290 if (ci->ci_flags & CPUF_PRESENT) {
291 struct cpu_info *tmp;
292
293 tmp = phycpu_info_list;
294 while (tmp->ci_next)
295 tmp = tmp->ci_next;
296
297 tmp->ci_next = ci;
298 }
299 break;
300
301 default:
302 panic("unknown processor type??\n");
303 }
304
305 atomic_or_32(&phycpus_attached, ci->ci_cpumask);
306
307 return;
308 }
309
310 int
311 vcpu_match(device_t parent, cfdata_t match, void *aux)
312 {
313 struct vcpu_attach_args *vcaa = aux;
314
315 if (strcmp(vcaa->vcaa_name, match->cf_name) == 0)
316 return 1;
317 return 0;
318 }
319
320 void
321 vcpu_attach(device_t parent, device_t self, void *aux)
322 {
323 struct vcpu_attach_args *vcaa = aux;
324
325 cpu_attach_common(parent, self, &vcaa->vcaa_caa);
326 }
327
328 static void
329 cpu_vm_init(struct cpu_info *ci)
330 {
331 int ncolors = 2, i;
332
333 for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
334 struct x86_cache_info *cai;
335 int tcolors;
336
337 cai = &ci->ci_cinfo[i];
338
339 tcolors = atop(cai->cai_totalsize);
340 switch(cai->cai_associativity) {
341 case 0xff:
342 tcolors = 1; /* fully associative */
343 break;
344 case 0:
345 case 1:
346 break;
347 default:
348 tcolors /= cai->cai_associativity;
349 }
350 ncolors = max(ncolors, tcolors);
351 }
352
353 /*
354 * Knowing the size of the largest cache on this CPU, re-color
355 * our pages.
356 */
357 if (ncolors <= uvmexp.ncolors)
358 return;
359 aprint_debug_dev(ci->ci_dev, "%d page colors\n", ncolors);
360 uvm_page_recolor(ncolors);
361 }
362
363 void
364 cpu_attach_common(device_t parent, device_t self, void *aux)
365 {
366 struct cpu_softc *sc = device_private(self);
367 struct cpu_attach_args *caa = aux;
368 struct cpu_info *ci;
369 uintptr_t ptr;
370 int cpunum = caa->cpu_number;
371 static bool again = false;
372
373 sc->sc_dev = self;
374
375 /*
376 * If we're an Application Processor, allocate a cpu_info
377 * structure, otherwise use the primary's.
378 */
379 if (caa->cpu_role == CPU_ROLE_AP) {
380 aprint_naive(": Application Processor\n");
381 ptr = (uintptr_t)kmem_alloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
382 KM_SLEEP);
383 ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
384 memset(ci, 0, sizeof(*ci));
385 #ifdef TRAPLOG
386 ci->ci_tlog_base = kmem_zalloc(sizeof(struct tlog), KM_SLEEP);
387 #endif
388 } else {
389 aprint_naive(": %s Processor\n",
390 caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
391 ci = &cpu_info_primary;
392 #if NLAPIC > 0
393 if (cpunum != lapic_cpu_number()) {
394 /* XXX should be done earlier */
395 uint32_t reg;
396 aprint_verbose("\n");
397 aprint_verbose_dev(self, "running CPU at apic %d"
398 " instead of at expected %d", lapic_cpu_number(),
399 cpunum);
400 reg = i82489_readreg(LAPIC_ID);
401 i82489_writereg(LAPIC_ID, (reg & ~LAPIC_ID_MASK) |
402 (cpunum << LAPIC_ID_SHIFT));
403 }
404 if (cpunum != lapic_cpu_number()) {
405 aprint_error_dev(self, "unable to reset apic id\n");
406 }
407 #endif
408 }
409
410 ci->ci_self = ci;
411 sc->sc_info = ci;
412 ci->ci_dev = self;
413 ci->ci_cpuid = cpunum;
414
415 KASSERT(HYPERVISOR_shared_info != NULL);
416 ci->ci_vcpu = &HYPERVISOR_shared_info->vcpu_info[cpunum];
417
418 ci->ci_func = caa->cpu_func;
419
420 /* Must be called before mi_cpu_attach(). */
421 cpu_vm_init(ci);
422
423 if (caa->cpu_role == CPU_ROLE_AP) {
424 int error;
425
426 error = mi_cpu_attach(ci);
427 if (error != 0) {
428 aprint_normal("\n");
429 aprint_error_dev(self,
430 "mi_cpu_attach failed with %d\n", error);
431 return;
432 }
433 } else {
434 KASSERT(ci->ci_data.cpu_idlelwp != NULL);
435 }
436
437 ci->ci_cpumask = (1 << cpu_index(ci));
438 pmap_reference(pmap_kernel());
439 ci->ci_pmap = pmap_kernel();
440 ci->ci_tlbstate = TLBSTATE_STALE;
441
442 /*
443 * Boot processor may not be attached first, but the below
444 * must be done to allow booting other processors.
445 */
446 if (!again) {
447 atomic_or_32(&ci->ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
448 /* Basic init. */
449 cpu_intr_init(ci);
450 cpu_get_tsc_freq(ci);
451 cpu_init(ci);
452 cpu_set_tss_gates(ci);
453 pmap_cpu_init_late(ci);
454 #if NLAPIC > 0
455 if (caa->cpu_role != CPU_ROLE_SP) {
456 /* Enable lapic. */
457 lapic_enable();
458 lapic_set_lvt();
459 lapic_calibrate_timer();
460 }
461 #endif
462 /* Make sure DELAY() is initialized. */
463 DELAY(1);
464 again = true;
465 }
466
467 /* further PCB init done later. */
468
469 switch (caa->cpu_role) {
470 case CPU_ROLE_SP:
471 atomic_or_32(&ci->ci_flags, CPUF_SP);
472 cpu_identify(ci);
473 #if 0
474 x86_errata();
475 #endif
476 x86_cpu_idle_init();
477 break;
478
479 case CPU_ROLE_BP:
480 atomic_or_32(&ci->ci_flags, CPUF_BSP);
481 cpu_identify(ci);
482 cpu_init(ci);
483 #if 0
484 x86_errata();
485 #endif
486 x86_cpu_idle_init();
487 break;
488
489 case CPU_ROLE_AP:
490 /*
491 * report on an AP
492 */
493
494 #if defined(MULTIPROCESSOR)
495 cpu_intr_init(ci);
496 gdt_alloc_cpu(ci);
497 cpu_set_tss_gates(ci);
498 pmap_cpu_init_early(ci);
499 pmap_cpu_init_late(ci);
500 cpu_start_secondary(ci);
501 if (ci->ci_flags & CPUF_PRESENT) {
502 struct cpu_info *tmp;
503
504 identifycpu(ci);
505 tmp = cpu_info_list;
506 while (tmp->ci_next)
507 tmp = tmp->ci_next;
508
509 tmp->ci_next = ci;
510 }
511 #else
512 aprint_error_dev(self, "not started\n");
513 #endif
514 break;
515
516 default:
517 aprint_normal("\n");
518 panic("unknown processor type??\n");
519 }
520
521 pat_init(ci);
522 atomic_or_32(&cpus_attached, ci->ci_cpumask);
523
524 #if 0
525 if (!pmf_device_register(self, cpu_suspend, cpu_resume))
526 aprint_error_dev(self, "couldn't establish power handler\n");
527 #endif
528
529 #if defined(MULTIPROCESSOR)
530 if (mp_verbose) {
531 struct lwp *l = ci->ci_data.cpu_idlelwp;
532 struct pcb *pcb = lwp_getpcb(l);
533
534 aprint_verbose_dev(self,
535 "idle lwp at %p, idle sp at 0x%p\n",
536 l,
537 #ifdef i386
538 (void *)pcb->pcb_esp
539 #else
540 (void *)pcb->pcb_rsp
541 #endif
542 );
543
544 }
545 #endif
546 }
547
548 /*
549 * Initialize the processor appropriately.
550 */
551
552 void
553 cpu_init(struct cpu_info *ci)
554 {
555
556 /*
557 * On a P6 or above, enable global TLB caching if the
558 * hardware supports it.
559 */
560 if (cpu_feature[0] & CPUID_PGE)
561 lcr4(rcr4() | CR4_PGE); /* enable global TLB caching */
562
563 #ifdef XXXMTRR
564 /*
565 * On a P6 or above, initialize MTRR's if the hardware supports them.
566 */
567 if (cpu_feature[0] & CPUID_MTRR) {
568 if ((ci->ci_flags & CPUF_AP) == 0)
569 i686_mtrr_init_first();
570 mtrr_init_cpu(ci);
571 }
572 #endif
573 /*
574 * If we have FXSAVE/FXRESTOR, use them.
575 */
576 if (cpu_feature[0] & CPUID_FXSR) {
577 lcr4(rcr4() | CR4_OSFXSR);
578
579 /*
580 * If we have SSE/SSE2, enable XMM exceptions.
581 */
582 if (cpu_feature[0] & (CPUID_SSE|CPUID_SSE2))
583 lcr4(rcr4() | CR4_OSXMMEXCPT);
584 }
585
586 #ifdef __x86_64__
587 /* No user PGD mapped for this CPU yet */
588 ci->ci_xen_current_user_pgd = 0;
589 #endif
590
591 atomic_or_32(&cpus_running, ci->ci_cpumask);
592 atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
593 }
594
595
596 #ifdef MULTIPROCESSOR
597 void
598 cpu_boot_secondary_processors(void)
599 {
600 struct cpu_info *ci;
601 u_long i;
602
603 for (i = 0; i < maxcpus; i++) {
604 ci = cpu_lookup(i);
605 if (ci == NULL)
606 continue;
607 if (ci->ci_data.cpu_idlelwp == NULL)
608 continue;
609 if ((ci->ci_flags & CPUF_PRESENT) == 0)
610 continue;
611 if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
612 continue;
613 cpu_boot_secondary(ci);
614 }
615
616 x86_mp_online = true;
617 }
618
619 static void
620 cpu_init_idle_lwp(struct cpu_info *ci)
621 {
622 struct lwp *l = ci->ci_data.cpu_idlelwp;
623 struct pcb *pcb = lwp_getpcb(l);
624
625 pcb->pcb_cr0 = rcr0();
626 }
627
628 void
629 cpu_init_idle_lwps(void)
630 {
631 struct cpu_info *ci;
632 u_long i;
633
634 for (i = 0; i < maxcpus; i++) {
635 ci = cpu_lookup(i);
636 if (ci == NULL)
637 continue;
638 if (ci->ci_data.cpu_idlelwp == NULL)
639 continue;
640 if ((ci->ci_flags & CPUF_PRESENT) == 0)
641 continue;
642 cpu_init_idle_lwp(ci);
643 }
644 }
645
646 void
647 cpu_start_secondary(struct cpu_info *ci)
648 {
649 int i;
650 struct pmap *kpm = pmap_kernel();
651 extern uint32_t mp_pdirpa;
652
653 mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */
654
655 atomic_or_32(&ci->ci_flags, CPUF_AP);
656
657 aprint_debug_dev(ci->ci_dev, "starting\n");
658
659 ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
660 if (CPU_STARTUP(ci, mp_trampoline_paddr) != 0)
661 return;
662
663 /*
664 * wait for it to become ready
665 */
666 for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i > 0; i--) {
667 #ifdef MPDEBUG
668 extern int cpu_trace[3];
669 static int otrace[3];
670 if (memcmp(otrace, cpu_trace, sizeof(otrace)) != 0) {
671 aprint_debug_dev(ci->ci_dev, "trace %02x %02x %02x\n",
672 cpu_trace[0], cpu_trace[1], cpu_trace[2]);
673 memcpy(otrace, cpu_trace, sizeof(otrace));
674 }
675 #endif
676 delay(10);
677 }
678 if ((ci->ci_flags & CPUF_PRESENT) == 0) {
679 aprint_error_dev(ci->ci_dev, "failed to become ready\n");
680 #if defined(MPDEBUG) && defined(DDB)
681 printf("dropping into debugger; continue from here to resume boot\n");
682 Debugger();
683 #endif
684 }
685
686 CPU_START_CLEANUP(ci);
687 }
688
689 void
690 cpu_boot_secondary(struct cpu_info *ci)
691 {
692 int i;
693
694 atomic_or_32(&ci->ci_flags, CPUF_GO);
695 for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i > 0; i--) {
696 delay(10);
697 }
698 if ((ci->ci_flags & CPUF_RUNNING) == 0) {
699 aprint_error_dev(ci->ci_dev, "CPU failed to start\n");
700 #if defined(MPDEBUG) && defined(DDB)
701 printf("dropping into debugger; continue from here to resume boot\n");
702 Debugger();
703 #endif
704 }
705 }
706
707 /*
708 * The CPU ends up here when its ready to run
709 * This is called from code in mptramp.s; at this point, we are running
710 * in the idle pcb/idle stack of the new CPU. When this function returns,
711 * this processor will enter the idle loop and start looking for work.
712 *
713 * XXX should share some of this with init386 in machdep.c
714 */
715 void
716 cpu_hatch(void *v)
717 {
718 struct cpu_info *ci = (struct cpu_info *)v;
719 struct pcb *pcb;
720 int s, i;
721
722 cpu_probe(ci);
723
724 cpu_feature[0] &= ~CPUID_FEAT_BLACKLIST;
725 cpu_feature[2] &= ~CPUID_FEAT_EXT_BLACKLIST;
726
727 cpu_init_msrs(ci, true);
728
729 KDASSERT((ci->ci_flags & CPUF_PRESENT) == 0);
730 atomic_or_32(&ci->ci_flags, CPUF_PRESENT);
731 while ((ci->ci_flags & CPUF_GO) == 0) {
732 /* Don't use delay, boot CPU may be patching the text. */
733 for (i = 10000; i != 0; i--)
734 x86_pause();
735 }
736
737 /* Because the text may have been patched in x86_patch(). */
738 wbinvd();
739 x86_flush();
740
741 KASSERT((ci->ci_flags & CPUF_RUNNING) == 0);
742
743 pcb = lwp_getpcb(curlwp);
744 lcr3(pmap_kernel()->pm_pdirpa);
745 pcb->pcb_cr3 = pmap_kernel()->pm_pdirpa;
746 pcb = lwp_getpcb(ci->ci_data.cpu_idlelwp);
747 lcr0(pcb->pcb_cr0);
748
749 cpu_init_idt();
750 gdt_init_cpu(ci);
751 lapic_enable();
752 lapic_set_lvt();
753 lapic_initclocks();
754
755 #ifdef i386
756 npxinit(ci);
757 #else
758 fpuinit(ci);
759 #endif
760
761 lldt(GSEL(GLDT_SEL, SEL_KPL));
762 ltr(ci->ci_tss_sel);
763
764 cpu_init(ci);
765 cpu_get_tsc_freq(ci);
766
767 s = splhigh();
768 #ifdef i386
769 lapic_tpr = 0;
770 #else
771 lcr8(0);
772 #endif
773 x86_enable_intr();
774 splx(s);
775 #if 0
776 x86_errata();
777 #endif
778
779 aprint_debug_dev(ci->ci_dev, "CPU %ld running\n",
780 (long)ci->ci_cpuid);
781 }
782
783 #if defined(DDB)
784
785 #include <ddb/db_output.h>
786 #include <machine/db_machdep.h>
787
788 /*
789 * Dump CPU information from ddb.
790 */
791 void
792 cpu_debug_dump(void)
793 {
794 struct cpu_info *ci;
795 CPU_INFO_ITERATOR cii;
796
797 db_printf("addr dev id flags ipis curlwp fpcurlwp\n");
798 for (CPU_INFO_FOREACH(cii, ci)) {
799 db_printf("%p %s %ld %x %x %10p %10p\n",
800 ci,
801 ci->ci_dev == NULL ? "BOOT" : device_xname(ci->ci_dev),
802 (long)ci->ci_cpuid,
803 ci->ci_flags, ci->ci_ipis,
804 ci->ci_curlwp,
805 ci->ci_fpcurlwp);
806 }
807 }
808 #endif /* DDB */
809
810 static void
811 cpu_copy_trampoline(void)
812 {
813 /*
814 * Copy boot code.
815 */
816 extern u_char cpu_spinup_trampoline[];
817 extern u_char cpu_spinup_trampoline_end[];
818
819 vaddr_t mp_trampoline_vaddr;
820
821 mp_trampoline_vaddr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
822 UVM_KMF_VAONLY);
823
824 pmap_kenter_pa(mp_trampoline_vaddr, mp_trampoline_paddr,
825 VM_PROT_READ | VM_PROT_WRITE, 0);
826 pmap_update(pmap_kernel());
827 memcpy((void *)mp_trampoline_vaddr,
828 cpu_spinup_trampoline,
829 cpu_spinup_trampoline_end - cpu_spinup_trampoline);
830
831 pmap_kremove(mp_trampoline_vaddr, PAGE_SIZE);
832 pmap_update(pmap_kernel());
833 uvm_km_free(kernel_map, mp_trampoline_vaddr, PAGE_SIZE, UVM_KMF_VAONLY);
834 }
835
836 #endif /* MULTIPROCESSOR */
837
838 #ifdef i386
839 #if 0
840 static void
841 tss_init(struct i386tss *tss, void *stack, void *func)
842 {
843 memset(tss, 0, sizeof *tss);
844 tss->tss_esp0 = tss->tss_esp = (int)((char *)stack + USPACE - 16);
845 tss->tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
846 tss->__tss_cs = GSEL(GCODE_SEL, SEL_KPL);
847 tss->tss_fs = GSEL(GCPU_SEL, SEL_KPL);
848 tss->tss_gs = tss->__tss_es = tss->__tss_ds =
849 tss->__tss_ss = GSEL(GDATA_SEL, SEL_KPL);
850 tss->tss_cr3 = pmap_kernel()->pm_pdirpa;
851 tss->tss_esp = (int)((char *)stack + USPACE - 16);
852 tss->tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
853 tss->__tss_eflags = PSL_MBO | PSL_NT; /* XXX not needed? */
854 tss->__tss_eip = (int)func;
855 }
856 #endif
857
858 /* XXX */
859 #define IDTVEC(name) __CONCAT(X, name)
860 typedef void (vector)(void);
861 extern vector IDTVEC(tss_trap08);
862 #ifdef DDB
863 extern vector Xintrddbipi;
864 extern int ddb_vec;
865 #endif
866
867 static void
868 cpu_set_tss_gates(struct cpu_info *ci)
869 {
870 #if 0
871 struct segment_descriptor sd;
872
873 ci->ci_doubleflt_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
874 UVM_KMF_WIRED);
875 tss_init(&ci->ci_doubleflt_tss, ci->ci_doubleflt_stack,
876 IDTVEC(tss_trap08));
877 setsegment(&sd, &ci->ci_doubleflt_tss, sizeof(struct i386tss) - 1,
878 SDT_SYS386TSS, SEL_KPL, 0, 0);
879 ci->ci_gdt[GTRAPTSS_SEL].sd = sd;
880 setgate(&idt[8], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
881 GSEL(GTRAPTSS_SEL, SEL_KPL));
882 #endif
883
884 #if defined(DDB) && defined(MULTIPROCESSOR)
885 /*
886 * Set up separate handler for the DDB IPI, so that it doesn't
887 * stomp on a possibly corrupted stack.
888 *
889 * XXX overwriting the gate set in db_machine_init.
890 * Should rearrange the code so that it's set only once.
891 */
892 ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
893 UVM_KMF_WIRED);
894 tss_init(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
895 Xintrddbipi);
896
897 setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
898 SDT_SYS386TSS, SEL_KPL, 0, 0);
899 ci->ci_gdt[GIPITSS_SEL].sd = sd;
900
901 setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
902 GSEL(GIPITSS_SEL, SEL_KPL));
903 #endif
904 }
905 #else
906 static void
907 cpu_set_tss_gates(struct cpu_info *ci)
908 {
909
910 }
911 #endif /* i386 */
912
913 int
914 mp_cpu_start(struct cpu_info *ci, paddr_t target)
915 {
916 #if 0
917 #if NLAPIC > 0
918 int error;
919 #endif
920 unsigned short dwordptr[2];
921
922 /*
923 * Bootstrap code must be addressable in real mode
924 * and it must be page aligned.
925 */
926 KASSERT(target < 0x10000 && target % PAGE_SIZE == 0);
927
928 /*
929 * "The BSP must initialize CMOS shutdown code to 0Ah ..."
930 */
931
932 outb(IO_RTC, NVRAM_RESET);
933 outb(IO_RTC+1, NVRAM_RESET_JUMP);
934
935 /*
936 * "and the warm reset vector (DWORD based at 40:67) to point
937 * to the AP startup code ..."
938 */
939
940 dwordptr[0] = 0;
941 dwordptr[1] = target >> 4;
942
943 pmap_kenter_pa (0, 0, VM_PROT_READ|VM_PROT_WRITE, 0);
944 pmap_update(pmap_kernel());
945
946 memcpy ((uint8_t *) 0x467, dwordptr, 4);
947
948 pmap_kremove (0, PAGE_SIZE);
949 pmap_update(pmap_kernel());
950
951 #if NLAPIC > 0
952 /*
953 * ... prior to executing the following sequence:"
954 */
955
956 if (ci->ci_flags & CPUF_AP) {
957 if ((error = x86_ipi_init(ci->ci_cpuid)) != 0)
958 return error;
959
960 delay(10000);
961
962 if (cpu_feature & CPUID_APIC) {
963 error = x86_ipi_init(ci->ci_cpuid);
964 if (error != 0) {
965 aprint_error_dev(ci->ci_dev, "%s: IPI not taken (1)\n",
966 __func__);
967 return error;
968 }
969
970 delay(10000);
971
972 error = x86_ipi(target / PAGE_SIZE, ci->ci_cpuid,
973 LAPIC_DLMODE_STARTUP);
974 if (error != 0) {
975 aprint_error_dev(ci->ci_dev, "%s: IPI not taken (2)\n",
976 __func__);
977 return error;
978 }
979 delay(200);
980
981 error = x86_ipi(target / PAGE_SIZE, ci->ci_cpuid,
982 LAPIC_DLMODE_STARTUP);
983 if (error != 0) {
984 aprint_error_dev(ci->ci_dev, "%s: IPI not taken ((3)\n",
985 __func__);
986 return error;
987 }
988 delay(200);
989 }
990 }
991 #endif
992 #endif /* 0 */
993 return 0;
994 }
995
996 void
997 mp_cpu_start_cleanup(struct cpu_info *ci)
998 {
999 #if 0
1000 /*
1001 * Ensure the NVRAM reset byte contains something vaguely sane.
1002 */
1003
1004 outb(IO_RTC, NVRAM_RESET);
1005 outb(IO_RTC+1, NVRAM_RESET_RST);
1006 #endif
1007 }
1008
1009 void
1010 cpu_init_msrs(struct cpu_info *ci, bool full)
1011 {
1012 #ifdef __x86_64__
1013 if (full) {
1014 HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
1015 HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (uint64_t) ci);
1016 HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
1017 }
1018 #endif /* __x86_64__ */
1019
1020 if (cpu_feature[2] & CPUID_NOX)
1021 wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE);
1022 }
1023
1024 void
1025 cpu_offline_md(void)
1026 {
1027 int s;
1028
1029 s = splhigh();
1030 #ifdef __i386__
1031 npxsave_cpu(true);
1032 #else
1033 fpusave_cpu(true);
1034 #endif
1035 splx(s);
1036 }
1037
1038 #if 0
1039 /* XXX joerg restructure and restart CPUs individually */
1040 static bool
1041 cpu_suspend(device_t dv, const pmf_qual_t *qual)
1042 {
1043 struct cpu_softc *sc = device_private(dv);
1044 struct cpu_info *ci = sc->sc_info;
1045 int err;
1046
1047 if (ci->ci_flags & CPUF_PRIMARY)
1048 return true;
1049 if (ci->ci_data.cpu_idlelwp == NULL)
1050 return true;
1051 if ((ci->ci_flags & CPUF_PRESENT) == 0)
1052 return true;
1053
1054 sc->sc_wasonline = !(ci->ci_schedstate.spc_flags & SPCF_OFFLINE);
1055
1056 if (sc->sc_wasonline) {
1057 mutex_enter(&cpu_lock);
1058 err = cpu_setstate(ci, false);
1059 mutex_exit(&cpu_lock);
1060
1061 if (err)
1062 return false;
1063 }
1064
1065 return true;
1066 }
1067
1068 static bool
1069 cpu_resume(device_t dv, const pmf_qual_t *qual)
1070 {
1071 struct cpu_softc *sc = device_private(dv);
1072 struct cpu_info *ci = sc->sc_info;
1073 int err = 0;
1074
1075 if (ci->ci_flags & CPUF_PRIMARY)
1076 return true;
1077 if (ci->ci_data.cpu_idlelwp == NULL)
1078 return true;
1079 if ((ci->ci_flags & CPUF_PRESENT) == 0)
1080 return true;
1081
1082 if (sc->sc_wasonline) {
1083 mutex_enter(&cpu_lock);
1084 err = cpu_setstate(ci, true);
1085 mutex_exit(&cpu_lock);
1086 }
1087
1088 return err == 0;
1089 }
1090 #endif
1091
1092 void
1093 cpu_get_tsc_freq(struct cpu_info *ci)
1094 {
1095 const volatile vcpu_time_info_t *tinfo = &ci->ci_vcpu->time;
1096 delay(1000000);
1097 uint64_t freq = 1000000000ULL << 32;
1098 freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
1099 if ( tinfo->tsc_shift < 0 )
1100 freq = freq << -tinfo->tsc_shift;
1101 else
1102 freq = freq >> tinfo->tsc_shift;
1103 ci->ci_data.cpu_cc_freq = freq;
1104 }
1105
1106 void
1107 x86_cpu_idle_xen(void)
1108 {
1109 struct cpu_info *ci = curcpu();
1110
1111 KASSERT(ci->ci_ilevel == IPL_NONE);
1112
1113 x86_disable_intr();
1114 if (!__predict_false(ci->ci_want_resched)) {
1115 idle_block();
1116 } else {
1117 x86_enable_intr();
1118 }
1119 }
1120
1121 /*
1122 * Loads pmap for the current CPU.
1123 */
1124 void
1125 cpu_load_pmap(struct pmap *pmap)
1126 {
1127 #ifdef i386
1128 #ifdef PAE
1129 int i, s;
1130 struct cpu_info *ci;
1131
1132 s = splvm(); /* just to be safe */
1133 ci = curcpu();
1134 paddr_t l3_pd = xpmap_ptom_masked(ci->ci_pae_l3_pdirpa);
1135 /* don't update the kernel L3 slot */
1136 for (i = 0 ; i < PDP_SIZE - 1; i++) {
1137 xpq_queue_pte_update(l3_pd + i * sizeof(pd_entry_t),
1138 xpmap_ptom(pmap->pm_pdirpa[i]) | PG_V);
1139 }
1140 splx(s);
1141 tlbflush();
1142 #else /* PAE */
1143 lcr3(pmap_pdirpa(pmap, 0));
1144 #endif /* PAE */
1145 #endif /* i386 */
1146
1147 #ifdef __x86_64__
1148 int i, s;
1149 pd_entry_t *old_pgd, *new_pgd;
1150 paddr_t addr;
1151 struct cpu_info *ci;
1152
1153 /* kernel pmap always in cr3 and should never go in user cr3 */
1154 if (pmap_pdirpa(pmap, 0) != pmap_pdirpa(pmap_kernel(), 0)) {
1155 ci = curcpu();
1156 /*
1157 * Map user space address in kernel space and load
1158 * user cr3
1159 */
1160 s = splvm();
1161 new_pgd = pmap->pm_pdir;
1162 old_pgd = pmap_kernel()->pm_pdir;
1163 addr = xpmap_ptom(pmap_pdirpa(pmap_kernel(), 0));
1164 for (i = 0; i < PDIR_SLOT_PTE;
1165 i++, addr += sizeof(pd_entry_t)) {
1166 if ((new_pgd[i] & PG_V) || (old_pgd[i] & PG_V))
1167 xpq_queue_pte_update(addr, new_pgd[i]);
1168 }
1169 tlbflush();
1170 xen_set_user_pgd(pmap_pdirpa(pmap, 0));
1171 ci->ci_xen_current_user_pgd = pmap_pdirpa(pmap, 0);
1172 splx(s);
1173 }
1174 #endif /* __x86_64__ */
1175 }
1176