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