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