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