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