cpu.c revision 1.1.2.4 1 /* $NetBSD: cpu.c,v 1.1.2.4 2007/11/18 20:57:21 bouyer 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 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by RedBack Networks Inc.
10 *
11 * Author: Bill Sommerfeld
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /*
43 * Copyright (c) 1999 Stefan Grefen
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by the NetBSD
56 * Foundation, Inc. and its contributors.
57 * 4. Neither the name of The NetBSD Foundation nor the names of its
58 * contributors may be used to endorse or promote products derived
59 * from this software without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
62 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 */
73
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.1.2.4 2007/11/18 20:57:21 bouyer Exp $");
76
77 #include "opt_ddb.h"
78 #include "opt_multiprocessor.h"
79 #include "opt_mpbios.h" /* for MPDEBUG */
80 #include "opt_mtrr.h"
81 #include "opt_xen.h"
82
83 #include "lapic.h"
84 #include "ioapic.h"
85
86 #include <sys/param.h>
87 #include <sys/proc.h>
88 #include <sys/user.h>
89 #include <sys/systm.h>
90 #include <sys/device.h>
91 #include <sys/malloc.h>
92
93 #include <uvm/uvm_extern.h>
94
95 #include <machine/cpu.h>
96 #include <machine/cpufunc.h>
97 #include <machine/cpuvar.h>
98 #include <machine/pmap.h>
99 #include <machine/vmparam.h>
100 #include <machine/mpbiosvar.h>
101 #include <machine/pcb.h>
102 #include <machine/specialreg.h>
103 #include <machine/segments.h>
104 #include <machine/gdt.h>
105 #include <machine/mtrr.h>
106 #include <machine/pio.h>
107
108 #ifdef XEN3
109 #include <xen/vcpuvar.h>
110 #endif
111
112 #if NLAPIC > 0
113 #include <machine/apicvar.h>
114 #include <machine/i82489reg.h>
115 #include <machine/i82489var.h>
116 #endif
117
118 #if NIOAPIC > 0
119 #include <machine/i82093var.h>
120 #endif
121
122 #include <dev/ic/mc146818reg.h>
123 #include <dev/isa/isareg.h>
124
125 int cpu_match(struct device *, struct cfdata *, void *);
126 void cpu_attach(struct device *, struct device *, void *);
127 #ifdef XEN3
128 int vcpu_match(struct device *, struct cfdata *, void *);
129 void vcpu_attach(struct device *, struct device *, void *);
130 #endif
131 void cpu_attach_common(struct device *, struct device *, void *);
132
133 struct cpu_softc {
134 struct device sc_dev; /* device tree glue */
135 struct cpu_info *sc_info; /* pointer to CPU info */
136 };
137
138 int mp_cpu_start(struct cpu_info *);
139 void mp_cpu_start_cleanup(struct cpu_info *);
140 const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
141 mp_cpu_start_cleanup };
142
143 CFATTACH_DECL(cpu, sizeof(struct cpu_softc),
144 cpu_match, cpu_attach, NULL, NULL);
145 #ifdef XEN3
146 CFATTACH_DECL(vcpu, sizeof(struct cpu_softc),
147 vcpu_match, vcpu_attach, NULL, NULL);
148 #endif
149
150 /*
151 * Statically-allocated CPU info for the primary CPU (or the only
152 * CPU, on uniprocessors). The CPU info list is initialized to
153 * point at it.
154 */
155 #ifdef TRAPLOG
156 #include <machine/tlog.h>
157 struct tlog tlog_primary;
158 #endif
159 struct cpu_info cpu_info_primary = {
160 .ci_self = &cpu_info_primary,
161 #ifndef __x86_64__
162 .ci_self150 = (uint8_t *)&cpu_info_primary + 0x150,
163 #endif
164 .ci_curlwp = &lwp0,
165 #ifdef TRAPLOG
166 .ci_tlog = &tlog_primary,
167 #endif
168
169 };
170 struct cpu_info phycpu_info_primary = {
171 .ci_self = &phycpu_info_primary,
172 #ifndef __x86_64__
173 .ci_self150 = (uint8_t *)&phycpu_info_primary + 0x150,
174 #endif
175 };
176
177 struct cpu_info *cpu_info_list = &cpu_info_primary;
178
179 static void cpu_set_tss_gates(struct cpu_info *ci);
180
181 u_int32_t cpus_attached = 0;
182
183 struct cpu_info *phycpu_info[X86_MAXPROCS] = { &cpu_info_primary };
184
185 #ifdef MULTIPROCESSOR
186 /*
187 * Array of CPU info structures. Must be statically-allocated because
188 * curproc, etc. are used early.
189 */
190 struct cpu_info *cpu_info[X86_MAXPROCS] = { &cpu_info_primary };
191
192 u_int32_t cpus_running = 0;
193
194 void cpu_hatch(void *);
195 static void cpu_boot_secondary(struct cpu_info *ci);
196 static void cpu_start_secondary(struct cpu_info *ci);
197 static void cpu_copy_trampoline(void);
198
199 /*
200 * Runs once per boot once multiprocessor goo has been detected and
201 * the local APIC on the boot processor has been mapped.
202 *
203 * Called from lapic_boot_init() (from mpbios_scan()).
204 */
205 void
206 cpu_init_first()
207 {
208 int cpunum = lapic_cpu_number();
209
210 if (cpunum != 0) {
211 cpu_info[0] = NULL;
212 cpu_info[cpunum] = &cpu_info_primary;
213 }
214
215 cpu_copy_trampoline();
216 }
217 #endif
218
219 int
220 cpu_match(parent, match, aux)
221 struct device *parent;
222 struct cfdata *match;
223 void *aux;
224 {
225
226 return 1;
227 }
228
229 void
230 cpu_attach(parent, self, aux)
231 struct device *parent, *self;
232 void *aux;
233 {
234 #ifdef XEN3
235 struct cpu_softc *sc = (void *) self;
236 struct cpu_attach_args *caa = aux;
237 struct cpu_info *ci;
238 int cpunum = caa->cpu_number;
239
240 /*
241 * If we're an Application Processor, allocate a cpu_info
242 * structure, otherwise use the primary's.
243 */
244 if (caa->cpu_role == CPU_ROLE_AP) {
245 ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
246 if (phycpu_info[cpunum] != NULL)
247 panic("cpu at apic id %d already attached?", cpunum);
248 phycpu_info[cpunum] = ci;
249 } else {
250 ci = &phycpu_info_primary;
251 if (cpunum != 0) {
252 phycpu_info[0] = NULL;
253 phycpu_info[cpunum] = ci;
254 }
255 }
256
257 ci->ci_self = ci;
258 sc->sc_info = ci;
259
260 ci->ci_dev = self;
261 ci->ci_apicid = caa->cpu_number;
262 ci->ci_cpuid = ci->ci_apicid;
263
264 printf(": ");
265 switch (caa->cpu_role) {
266 case CPU_ROLE_SP:
267 printf("(uniprocessor)\n");
268 ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
269 break;
270
271 case CPU_ROLE_BP:
272 printf("(boot processor)\n");
273 ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
274 #if NIOAPIC > 0
275 ioapic_bsp_id = caa->cpu_number;
276 #endif
277 break;
278
279 case CPU_ROLE_AP:
280 /*
281 * report on an AP
282 */
283 printf("(application processor)\n");
284 break;
285
286 default:
287 panic("unknown processor type??\n");
288 }
289 return;
290 #else
291 cpu_attach_common(parent, self, aux);
292 #endif
293 }
294
295 #ifdef XEN3
296 int
297 vcpu_match(parent, match, aux)
298 struct device *parent;
299 struct cfdata *match;
300 void *aux;
301 {
302 struct vcpu_attach_args *vcaa = aux;
303
304 if (strcmp(vcaa->vcaa_name, match->cf_name) == 0)
305 return 1;
306 return 0;
307 }
308
309 void
310 vcpu_attach(parent, self, aux)
311 struct device *parent, *self;
312 void *aux;
313 {
314 struct vcpu_attach_args *vcaa = aux;
315
316 cpu_attach_common(parent, self, &vcaa->vcaa_caa);
317 }
318 #endif
319
320 static void
321 cpu_vm_init(struct cpu_info *ci)
322 {
323 int ncolors = 2, i;
324
325 for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
326 struct x86_cache_info *cai;
327 int tcolors;
328
329 cai = &ci->ci_cinfo[i];
330
331 tcolors = atop(cai->cai_totalsize);
332 switch(cai->cai_associativity) {
333 case 0xff:
334 tcolors = 1; /* fully associative */
335 break;
336 case 0:
337 case 1:
338 break;
339 default:
340 tcolors /= cai->cai_associativity;
341 }
342 ncolors = max(ncolors, tcolors);
343 }
344
345 /*
346 * Knowing the size of the largest cache on this CPU, re-color
347 * our pages.
348 */
349 if (ncolors <= uvmexp.ncolors)
350 return;
351 printf("%s: %d page colors\n", ci->ci_dev->dv_xname, ncolors);
352 uvm_page_recolor(ncolors);
353 }
354
355 void
356 cpu_attach_common(parent, self, aux)
357 struct device *parent, *self;
358 void *aux;
359 {
360 struct cpu_softc *sc = (void *) self;
361 struct cpu_attach_args *caa = aux;
362 struct cpu_info *ci;
363 #if defined(MULTIPROCESSOR)
364 int cpunum = caa->cpu_number;
365 #endif
366
367 /*
368 * If we're an Application Processor, allocate a cpu_info
369 * structure, otherwise use the primary's.
370 */
371 if (caa->cpu_role == CPU_ROLE_AP) {
372 ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
373 #if defined(MULTIPROCESSOR)
374 if (cpu_info[cpunum] != NULL)
375 panic("cpu at apic id %d already attached?", cpunum);
376 cpu_info[cpunum] = ci;
377 #endif
378 #ifdef TRAPLOG
379 ci->ci_tlog_base = malloc(sizeof(struct tlog),
380 M_DEVBUF, M_WAITOK);
381 #endif
382 } else {
383 ci = &cpu_info_primary;
384 #if defined(MULTIPROCESSOR)
385 if (cpunum != lapic_cpu_number()) {
386 panic("%s: running CPU is at apic %d"
387 " instead of at expected %d",
388 sc->sc_dev.dv_xname, lapic_cpu_number(), cpunum);
389 }
390 #endif
391 }
392
393 ci->ci_self = ci;
394 sc->sc_info = ci;
395
396 ci->ci_dev = self;
397 ci->ci_apicid = caa->cpu_number;
398 #ifdef MULTIPROCESSOR
399 ci->ci_cpuid = ci->ci_apicid;
400 #else
401 ci->ci_cpuid = 0; /* False for APs, but they're not used anyway */
402 #endif
403 ci->ci_cpumask = (1 << ci->ci_cpuid);
404 ci->ci_func = caa->cpu_func;
405
406 #ifndef __x86_64__
407 simple_lock_init(&ci->ci_slock);
408 #endif
409
410 if (caa->cpu_role == CPU_ROLE_AP) {
411 #if defined(MULTIPROCESSOR)
412 int error;
413
414 error = mi_cpu_attach(ci);
415 if (error != 0) {
416 aprint_normal("\n");
417 aprint_error("%s: mi_cpu_attach failed with %d\n",
418 sc->sc_dev.dv_xname, error);
419 return;
420 }
421 #endif
422 } else {
423 KASSERT(ci->ci_data.cpu_idlelwp != NULL);
424 }
425
426 pmap_reference(pmap_kernel());
427 ci->ci_pmap = pmap_kernel();
428 ci->ci_tlbstate = TLBSTATE_STALE;
429
430 /* further PCB init done later. */
431
432 printf(": ");
433
434 switch (caa->cpu_role) {
435 case CPU_ROLE_SP:
436 printf("(uniprocessor)\n");
437 ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
438 cpu_intr_init(ci);
439 identifycpu(ci);
440 cpu_init(ci);
441 cpu_set_tss_gates(ci);
442 break;
443
444 case CPU_ROLE_BP:
445 printf("apid %d (boot processor)\n", caa->cpu_number);
446 ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
447 cpu_intr_init(ci);
448 identifycpu(ci);
449 cpu_init(ci);
450 cpu_set_tss_gates(ci);
451 break;
452
453 case CPU_ROLE_AP:
454 /*
455 * report on an AP
456 */
457 printf("apid %d (application processor)\n", caa->cpu_number);
458
459 #if defined(MULTIPROCESSOR)
460 cpu_intr_init(ci);
461 gdt_alloc_cpu(ci);
462 cpu_set_tss_gates(ci);
463 cpu_start_secondary(ci);
464 if (ci->ci_flags & CPUF_PRESENT) {
465 identifycpu(ci);
466 ci->ci_next = cpu_info_list->ci_next;
467 cpu_info_list->ci_next = ci;
468 }
469 #else
470 printf("%s: not started\n", sc->sc_dev.dv_xname);
471 #endif
472 break;
473
474 default:
475 panic("unknown processor type??\n");
476 }
477 cpu_vm_init(ci);
478
479 cpus_attached |= (1 << ci->ci_cpuid);
480
481 #if defined(MULTIPROCESSOR)
482 if (mp_verbose) {
483 struct lwp *l = ci->ci_data.cpu_idlelwp;
484
485 aprint_verbose("%s: idle lwp at %p, idle sp at 0x%x\n",
486 sc->sc_dev.dv_xname, l, l->l_addr->u_pcb.pcb_esp);
487 }
488 #endif
489 }
490
491 /*
492 * Initialize the processor appropriately.
493 */
494
495 void
496 cpu_init(ci)
497 struct cpu_info *ci;
498 {
499 /* configure the CPU if needed */
500 if (ci->cpu_setup != NULL)
501 (*ci->cpu_setup)(ci);
502
503 /*
504 * On a P6 or above, enable global TLB caching if the
505 * hardware supports it.
506 */
507 if (cpu_feature & CPUID_PGE)
508 lcr4(rcr4() | CR4_PGE); /* enable global TLB caching */
509
510 #ifdef XXXMTRR
511 /*
512 * On a P6 or above, initialize MTRR's if the hardware supports them.
513 */
514 if (cpu_feature & CPUID_MTRR) {
515 if ((ci->ci_flags & CPUF_AP) == 0)
516 i686_mtrr_init_first();
517 mtrr_init_cpu(ci);
518 }
519 #endif
520 /*
521 * If we have FXSAVE/FXRESTOR, use them.
522 */
523 if (cpu_feature & CPUID_FXSR) {
524 lcr4(rcr4() | CR4_OSFXSR);
525
526 /*
527 * If we have SSE/SSE2, enable XMM exceptions.
528 */
529 if (cpu_feature & (CPUID_SSE|CPUID_SSE2))
530 lcr4(rcr4() | CR4_OSXMMEXCPT);
531 }
532
533 #ifdef MULTIPROCESSOR
534 ci->ci_flags |= CPUF_RUNNING;
535 cpus_running |= 1 << ci->ci_cpuid;
536 #endif
537 }
538
539
540 #ifdef MULTIPROCESSOR
541 void
542 cpu_boot_secondary_processors()
543 {
544 struct cpu_info *ci;
545 u_long i;
546
547 for (i=0; i < X86_MAXPROCS; i++) {
548 ci = cpu_info[i];
549 if (ci == NULL)
550 continue;
551 if (ci->ci_data.cpu_idlelwp == NULL)
552 continue;
553 if ((ci->ci_flags & CPUF_PRESENT) == 0)
554 continue;
555 if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
556 continue;
557 cpu_boot_secondary(ci);
558 }
559 }
560
561 static void
562 cpu_init_idle_lwp(struct cpu_info *ci)
563 {
564 struct lwp *l = ci->ci_data.cpu_idlelwp;
565 struct pcb *pcb = &l->l_addr->u_pcb;
566
567 pcb->pcb_cr0 = rcr0();
568 }
569
570 void
571 cpu_init_idle_lwps()
572 {
573 struct cpu_info *ci;
574 u_long i;
575
576 for (i = 0; i < X86_MAXPROCS; i++) {
577 ci = cpu_info[i];
578 if (ci == NULL)
579 continue;
580 if (ci->ci_data.cpu_idlelwp == NULL)
581 continue;
582 if ((ci->ci_flags & CPUF_PRESENT) == 0)
583 continue;
584 cpu_init_idle_lwp(ci);
585 }
586 }
587
588 void
589 cpu_start_secondary (ci)
590 struct cpu_info *ci;
591 {
592 int i;
593 struct pmap *kpm = pmap_kernel();
594 extern u_int32_t mp_pdirpa;
595
596 mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */
597
598 ci->ci_flags |= CPUF_AP;
599
600 printf("%s: starting\n", ci->ci_dev->dv_xname);
601
602 ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
603 CPU_STARTUP(ci);
604
605 /*
606 * wait for it to become ready
607 */
608 for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i>0;i--) {
609 delay(10);
610 }
611 if (! (ci->ci_flags & CPUF_PRESENT)) {
612 printf("%s: failed to become ready\n", ci->ci_dev->dv_xname);
613 #if defined(MPDEBUG) && defined(DDB)
614 printf("dropping into debugger; continue from here to resume boot\n");
615 Debugger();
616 #endif
617 }
618
619 CPU_START_CLEANUP(ci);
620 }
621
622 void
623 cpu_boot_secondary(ci)
624 struct cpu_info *ci;
625 {
626 int i;
627
628 ci->ci_flags |= CPUF_GO; /* XXX atomic */
629
630 for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i>0;i--) {
631 delay(10);
632 }
633 if (! (ci->ci_flags & CPUF_RUNNING)) {
634 printf("CPU failed to start\n");
635 #if defined(MPDEBUG) && defined(DDB)
636 printf("dropping into debugger; continue from here to resume boot\n");
637 Debugger();
638 #endif
639 }
640 }
641
642 /*
643 * The CPU ends up here when its ready to run
644 * This is called from code in mptramp.s; at this point, we are running
645 * in the idle pcb/idle stack of the new CPU. When this function returns,
646 * this processor will enter the idle loop and start looking for work.
647 *
648 * XXX should share some of this with init386 in machdep.c
649 */
650 void
651 cpu_hatch(void *v)
652 {
653 struct cpu_info *ci = (struct cpu_info *)v;
654 int s;
655 #ifdef __x86_64__
656 cpu_init_msrs(ci);
657 #endif
658
659 cpu_probe_features(ci);
660 cpu_feature &= ci->ci_feature_flags;
661 /* not on Xen... */
662 cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX);
663
664 #ifdef DEBUG
665 if (ci->ci_flags & CPUF_PRESENT)
666 panic("%s: already running!?", ci->ci_dev->dv_xname);
667 #endif
668
669 ci->ci_flags |= CPUF_PRESENT;
670
671 lapic_enable();
672 lapic_initclocks();
673
674 while ((ci->ci_flags & CPUF_GO) == 0)
675 delay(10);
676 #ifdef DEBUG
677 if (ci->ci_flags & CPUF_RUNNING)
678 panic("%s: already running!?", ci->ci_dev->dv_xname);
679 #endif
680
681 lcr0(ci->ci_data.cpu_idlelwp->l_addr->u_pcb.pcb_cr0);
682 cpu_init_idt();
683 lapic_set_lvt();
684 gdt_init_cpu(ci);
685 npxinit(ci);
686
687 lldt(GSEL(GLDT_SEL, SEL_KPL));
688
689 cpu_init(ci);
690
691 s = splhigh();
692 lapic_tpr = 0;
693 enable_intr();
694
695 printf("%s: CPU %ld running\n",ci->ci_dev->dv_xname, ci->ci_cpuid);
696 if (ci->ci_feature_flags & CPUID_TSC)
697 cc_microset(ci);
698 splx(s);
699 }
700
701 #if defined(DDB)
702
703 #include <ddb/db_output.h>
704 #include <machine/db_machdep.h>
705
706 /*
707 * Dump CPU information from ddb.
708 */
709 void
710 cpu_debug_dump(void)
711 {
712 struct cpu_info *ci;
713 CPU_INFO_ITERATOR cii;
714
715 db_printf("addr dev id flags ipis curproc fpcurproc\n");
716 for (CPU_INFO_FOREACH(cii, ci)) {
717 db_printf("%p %s %ld %x %x %10p %10p\n",
718 ci,
719 ci->ci_dev == NULL ? "BOOT" : ci->ci_dev->dv_xname,
720 ci->ci_cpuid,
721 ci->ci_flags, ci->ci_ipis,
722 ci->ci_curlwp,
723 ci->ci_fpcurlwp);
724 }
725 }
726 #endif
727
728 static void
729 cpu_copy_trampoline()
730 {
731 /*
732 * Copy boot code.
733 */
734 extern u_char cpu_spinup_trampoline[];
735 extern u_char cpu_spinup_trampoline_end[];
736 pmap_kenter_pa((vaddr_t)MP_TRAMPOLINE, /* virtual */
737 (paddr_t)MP_TRAMPOLINE, /* physical */
738 VM_PROT_ALL); /* protection */
739 memcpy((void *)MP_TRAMPOLINE,
740 cpu_spinup_trampoline,
741 cpu_spinup_trampoline_end-cpu_spinup_trampoline);
742 }
743
744 #endif
745
746
747 /* XXX */
748 #define IDTVEC(name) __CONCAT(X, name)
749 typedef void (vector)(void);
750 extern vector IDTVEC(tss_trap08);
751 #ifdef DDB
752 extern vector Xintrddbipi;
753 extern int ddb_vec;
754 #endif
755
756 static void
757 cpu_set_tss_gates(struct cpu_info *ci)
758 {
759 #if defined(DDB) && defined(MULTIPROCESSOR)
760 /*
761 * Set up separate handler for the DDB IPI, so that it doesn't
762 * stomp on a possibly corrupted stack.
763 *
764 * XXX overwriting the gate set in db_machine_init.
765 * Should rearrange the code so that it's set only once.
766 */
767 ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
768 UVM_KMF_WIRED);
769 cpu_init_tss(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
770 Xintrddbipi);
771
772 setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
773 SDT_SYS386TSS, SEL_KPL, 0, 0);
774 ci->ci_gdt[GIPITSS_SEL].sd = sd;
775
776 setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
777 GSEL(GIPITSS_SEL, SEL_KPL));
778 #endif
779 }
780
781 int
782 mp_cpu_start(struct cpu_info *ci)
783 {
784 #if 0
785 #if NLAPIC > 0
786 int error;
787 #endif
788 unsigned short dwordptr[2];
789
790 /*
791 * "The BSP must initialize CMOS shutdown code to 0Ah ..."
792 */
793
794 outb(IO_RTC, NVRAM_RESET);
795 outb(IO_RTC+1, NVRAM_RESET_JUMP);
796
797 /*
798 * "and the warm reset vector (DWORD based at 40:67) to point
799 * to the AP startup code ..."
800 */
801
802 dwordptr[0] = 0;
803 dwordptr[1] = MP_TRAMPOLINE >> 4;
804
805 pmap_kenter_pa (0, 0, VM_PROT_READ|VM_PROT_WRITE);
806 memcpy ((u_int8_t *) 0x467, dwordptr, 4);
807 pmap_kremove (0, PAGE_SIZE);
808
809 #if NLAPIC > 0
810 /*
811 * ... prior to executing the following sequence:"
812 */
813
814 if (ci->ci_flags & CPUF_AP) {
815 if ((error = x86_ipi_init(ci->ci_apicid)) != 0)
816 return error;
817
818 delay(10000);
819
820 if (cpu_feature & CPUID_APIC) {
821
822 if ((error = x86_ipi(MP_TRAMPOLINE/PAGE_SIZE,
823 ci->ci_apicid,
824 LAPIC_DLMODE_STARTUP)) != 0)
825 return error;
826 delay(200);
827
828 if ((error = x86_ipi(MP_TRAMPOLINE/PAGE_SIZE,
829 ci->ci_apicid,
830 LAPIC_DLMODE_STARTUP)) != 0)
831 return error;
832 delay(200);
833 }
834 }
835 #endif
836 #endif /* 0 */
837 return 0;
838 }
839
840 void
841 mp_cpu_start_cleanup(struct cpu_info *ci)
842 {
843 #if 0
844 /*
845 * Ensure the NVRAM reset byte contains something vaguely sane.
846 */
847
848 outb(IO_RTC, NVRAM_RESET);
849 outb(IO_RTC+1, NVRAM_RESET_RST);
850 #endif
851 }
852
853 #ifdef __x86_64__
854
855 void
856 cpu_init_msrs(struct cpu_info *ci)
857 {
858 HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
859 HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (u_int64_t) ci);
860 HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
861 }
862 #endif /* __x86_64__ */
863
864 void
865 cpu_get_tsc_freq(struct cpu_info *ci)
866 {
867 #ifdef XEN3
868 const volatile vcpu_time_info_t *tinfo =
869 &HYPERVISOR_shared_info->vcpu_info[0].time;
870 delay(1000000);
871 uint64_t freq = 1000000000ULL << 32;
872 freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
873 if ( tinfo->tsc_shift < 0 )
874 freq = freq << -tinfo->tsc_shift;
875 else
876 freq = freq >> tinfo->tsc_shift;
877 ci->ci_tsc_freq = freq;
878 #else
879 /* XXX this needs to read the shared_info of the CPU being probed.. */
880 ci->ci_tsc_freq = HYPERVISOR_shared_info->cpu_freq;
881 #endif /* XEN3 */
882 }
883