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