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