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