cpu.c revision 1.56.2.1 1 /* $NetBSD: cpu.c,v 1.56.2.1 2011/06/03 13:27:41 cherry 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.56.2.1 2011/06/03 13:27:41 cherry 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/systm.h>
83 #include <sys/device.h>
84 #include <sys/kmem.h>
85 #include <sys/cpu.h>
86 #include <sys/atomic.h>
87 #include <sys/reboot.h>
88 #include <sys/idle.h>
89
90 #include <uvm/uvm.h>
91
92 #include <machine/cpufunc.h>
93 #include <machine/cpuvar.h>
94 #include <machine/pmap.h>
95 #include <machine/vmparam.h>
96 #include <machine/mpbiosvar.h>
97 #include <machine/pcb.h>
98 #include <machine/specialreg.h>
99 #include <machine/segments.h>
100 #include <machine/gdt.h>
101 #include <machine/mtrr.h>
102 #include <machine/pio.h>
103 #include <machine/fpu.h>
104
105 #include <xen/xen.h>
106 #include <xen/xen3-public/vcpu.h>
107 #include <xen/vcpuvar.h>
108
109 #if NLAPIC > 0
110 #include <machine/apicvar.h>
111 #include <machine/i82489reg.h>
112 #include <machine/i82489var.h>
113 #endif
114
115 #include <dev/ic/mc146818reg.h>
116 #include <dev/isa/isareg.h>
117
118 #if MAXCPUS > 32
119 #error cpu_info contains 32bit bitmasks
120 #endif
121
122 static int cpu_match(device_t, cfdata_t, void *);
123 static void cpu_attach(device_t, device_t, void *);
124 static void cpu_defer(device_t);
125 static int cpu_rescan(device_t, const char *, const int *);
126 static void cpu_childdetached(device_t, device_t);
127 static int vcpu_match(device_t, cfdata_t, void *);
128 static void vcpu_attach(device_t, device_t, void *);
129 static void cpu_attach_common(device_t, device_t, void *);
130 void cpu_offline_md(void);
131
132 struct cpu_softc {
133 device_t sc_dev; /* device tree glue */
134 struct cpu_info *sc_info; /* pointer to CPU info */
135 bool sc_wasonline;
136 };
137
138 int mp_cpu_start(struct cpu_info *, paddr_t);
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_DECL2_NEW(cpu, sizeof(struct cpu_softc),
144 cpu_match, cpu_attach, NULL, NULL, cpu_rescan, cpu_childdetached);
145
146 CFATTACH_DECL_NEW(vcpu, sizeof(struct cpu_softc),
147 vcpu_match, vcpu_attach, NULL, NULL);
148
149 /*
150 * Statically-allocated CPU info for the primary CPU (or the only
151 * CPU, on uniprocessors). The CPU info list is initialized to
152 * point at it.
153 */
154 #ifdef TRAPLOG
155 #include <machine/tlog.h>
156 struct tlog tlog_primary;
157 #endif
158 struct cpu_info cpu_info_primary __aligned(CACHE_LINE_SIZE) = {
159 .ci_dev = 0,
160 .ci_self = &cpu_info_primary,
161 .ci_idepth = -1,
162 .ci_curlwp = &lwp0,
163 .ci_curldt = -1,
164 #ifdef TRAPLOG
165 .ci_tlog = &tlog_primary,
166 #endif
167
168 };
169 struct cpu_info phycpu_info_primary __aligned(CACHE_LINE_SIZE) = {
170 .ci_dev = 0,
171 .ci_self = &phycpu_info_primary,
172 };
173
174 struct cpu_info *cpu_info_list = &cpu_info_primary;
175 struct cpu_info *phycpu_info_list = &phycpu_info_primary;
176
177 static void cpu_set_tss_gates(struct cpu_info *ci);
178
179 uint32_t cpus_attached = 0;
180 uint32_t cpus_running = 0;
181
182 uint32_t phycpus_attached = 0;
183 uint32_t phycpus_running = 0;
184
185 uint32_t cpu_feature[5]; /* X86 CPUID feature bits
186 * [0] basic features %edx
187 * [1] basic features %ecx
188 * [2] extended features %edx
189 * [3] extended features %ecx
190 * [4] VIA padlock features
191 */
192
193 bool x86_mp_online;
194 paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
195
196 #if defined(MULTIPROCESSOR)
197 void cpu_hatch(void *);
198 static void cpu_boot_secondary(struct cpu_info *ci);
199 static void cpu_start_secondary(struct cpu_info *ci);
200 #endif /* MULTIPROCESSOR */
201
202 static int
203 cpu_match(device_t parent, cfdata_t match, void *aux)
204 {
205
206 return 1;
207 }
208
209 static void
210 cpu_attach(device_t parent, device_t self, void *aux)
211 {
212 struct cpu_softc *sc = device_private(self);
213 struct cpu_attach_args *caa = aux;
214 struct cpu_info *ci;
215 uintptr_t ptr;
216 static int nphycpu = 0;
217
218 sc->sc_dev = self;
219
220 if (phycpus_attached == ~0) {
221 aprint_error(": increase MAXCPUS\n");
222 return;
223 }
224
225 /*
226 * If we're an Application Processor, allocate a cpu_info
227 * If we're the first attached CPU use the primary cpu_info,
228 * otherwise allocate a new one
229 */
230 aprint_naive("\n");
231 aprint_normal("\n");
232 if (nphycpu > 0) {
233 struct cpu_info *tmp;
234 ptr = (uintptr_t)kmem_zalloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
235 KM_SLEEP);
236 ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
237 ci->ci_curldt = -1;
238
239 tmp = phycpu_info_list;
240 while (tmp->ci_next)
241 tmp = tmp->ci_next;
242
243 tmp->ci_next = ci;
244 } else {
245 ci = &phycpu_info_primary;
246 }
247
248 ci->ci_self = ci;
249 sc->sc_info = ci;
250
251 ci->ci_dev = self;
252 ci->ci_acpiid = caa->cpu_id;
253 ci->ci_cpuid = caa->cpu_number;
254 ci->ci_vcpu = NULL;
255 ci->ci_index = nphycpu++;
256 ci->ci_cpumask = (1 << cpu_index(ci));
257
258 atomic_or_32(&phycpus_attached, ci->ci_cpumask);
259
260 if (!pmf_device_register(self, NULL, NULL))
261 aprint_error_dev(self, "couldn't establish power handler\n");
262
263 (void)config_defer(self, cpu_defer);
264 }
265
266 static void
267 cpu_defer(device_t self)
268 {
269 cpu_rescan(self, NULL, NULL);
270 }
271
272 static int
273 cpu_rescan(device_t self, const char *ifattr, const int *locators)
274 {
275 struct cpu_softc *sc = device_private(self);
276 struct cpufeature_attach_args cfaa;
277 struct cpu_info *ci = sc->sc_info;
278
279 memset(&cfaa, 0, sizeof(cfaa));
280 cfaa.ci = ci;
281
282 if (ifattr_match(ifattr, "cpufeaturebus")) {
283
284 if (ci->ci_frequency == NULL) {
285 cfaa.name = "frequency";
286 ci->ci_frequency = config_found_ia(self,
287 "cpufeaturebus", &cfaa, NULL);
288 }
289 }
290
291 return 0;
292 }
293
294 static void
295 cpu_childdetached(device_t self, device_t child)
296 {
297 struct cpu_softc *sc = device_private(self);
298 struct cpu_info *ci = sc->sc_info;
299
300 if (ci->ci_frequency == child)
301 ci->ci_frequency = NULL;
302 }
303
304 static int
305 vcpu_match(device_t parent, cfdata_t match, void *aux)
306 {
307 struct vcpu_attach_args *vcaa = aux;
308 struct vcpu_runstate_info vcr;
309 int error;
310
311 if (strcmp(vcaa->vcaa_name, match->cf_name) == 0) {
312 error = HYPERVISOR_vcpu_op(VCPUOP_get_runstate_info,
313 vcaa->vcaa_caa.cpu_number,
314 &vcr);
315 switch (error) {
316 case 0:
317 return 1;
318 case -ENOENT:
319 return 0;
320 default:
321 panic("Unknown hypervisor error %d returned on vcpu runstate probe\n", error);
322 }
323 }
324
325 return 0;
326 }
327
328 static void
329 vcpu_attach(device_t parent, device_t self, void *aux)
330 {
331 struct vcpu_attach_args *vcaa = aux;
332
333 KASSERT(vcaa->vcaa_caa.cpu_func == NULL);
334 vcaa->vcaa_caa.cpu_func = &mp_cpu_funcs;
335 cpu_attach_common(parent, self, &vcaa->vcaa_caa);
336 }
337
338 static int
339 vcpu_is_up(struct cpu_info *ci)
340 {
341 KASSERT(ci != NULL);
342 return HYPERVISOR_vcpu_op(VCPUOP_is_up, ci->ci_cpuid, NULL);
343 }
344
345 static void
346 cpu_vm_init(struct cpu_info *ci)
347 {
348 int ncolors = 2, i;
349
350 for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
351 struct x86_cache_info *cai;
352 int tcolors;
353
354 cai = &ci->ci_cinfo[i];
355
356 tcolors = atop(cai->cai_totalsize);
357 switch(cai->cai_associativity) {
358 case 0xff:
359 tcolors = 1; /* fully associative */
360 break;
361 case 0:
362 case 1:
363 break;
364 default:
365 tcolors /= cai->cai_associativity;
366 }
367 ncolors = max(ncolors, tcolors);
368 }
369
370 /*
371 * Knowing the size of the largest cache on this CPU, re-color
372 * our pages.
373 */
374 if (ncolors <= uvmexp.ncolors)
375 return;
376 aprint_debug_dev(ci->ci_dev, "%d page colors\n", ncolors);
377 uvm_page_recolor(ncolors);
378 }
379
380 static void
381 cpu_attach_common(device_t parent, device_t self, void *aux)
382 {
383 struct cpu_softc *sc = device_private(self);
384 struct cpu_attach_args *caa = aux;
385 struct cpu_info *ci;
386 uintptr_t ptr;
387 int cpunum = caa->cpu_number;
388 static bool again = false;
389
390 sc->sc_dev = self;
391
392 /*
393 * If we're an Application Processor, allocate a cpu_info
394 * structure, otherwise use the primary's.
395 */
396 if (caa->cpu_role == CPU_ROLE_AP) {
397 aprint_naive(": Application Processor\n");
398 ptr = (uintptr_t)kmem_alloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
399 KM_SLEEP);
400 ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
401 memset(ci, 0, sizeof(*ci));
402 #ifdef TRAPLOG
403 ci->ci_tlog_base = kmem_zalloc(sizeof(struct tlog), KM_SLEEP);
404 #endif
405 } else {
406 aprint_naive(": %s Processor\n",
407 caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
408 ci = &cpu_info_primary;
409 }
410
411 ci->ci_self = ci;
412 sc->sc_info = ci;
413 ci->ci_dev = self;
414 ci->ci_cpuid = cpunum;
415
416 KASSERT(HYPERVISOR_shared_info != NULL);
417 ci->ci_vcpu = &HYPERVISOR_shared_info->vcpu_info[cpunum];
418
419 KASSERT(ci->ci_func == 0);
420 ci->ci_func = caa->cpu_func;
421
422 /* Must be called before mi_cpu_attach(). */
423 cpu_vm_init(ci);
424
425 if (caa->cpu_role == CPU_ROLE_AP) {
426 int error;
427
428 error = mi_cpu_attach(ci);
429
430 KASSERT(ci->ci_data.cpu_idlelwp != NULL);
431 if (error != 0) {
432 aprint_normal("\n");
433 aprint_error_dev(self,
434 "mi_cpu_attach failed with %d\n", error);
435 return;
436 }
437
438 } else {
439 KASSERT(ci->ci_data.cpu_idlelwp != NULL);
440 }
441
442 ci->ci_cpumask = (1 << cpu_index(ci));
443 pmap_reference(pmap_kernel());
444 ci->ci_pmap = pmap_kernel();
445 ci->ci_tlbstate = TLBSTATE_STALE;
446
447 /*
448 * Boot processor may not be attached first, but the below
449 * must be done to allow booting other processors.
450 */
451 if (!again) {
452 atomic_or_32(&ci->ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
453 /* Basic init. */
454 cpu_intr_init(ci);
455 cpu_get_tsc_freq(ci);
456 cpu_init(ci);
457 cpu_set_tss_gates(ci);
458 pmap_cpu_init_late(ci);
459
460 /* Every processor needs to init it's own ipi h/w (similar to lapic) */
461 xen_ipi_init();
462 /* XXX: clock_init() */
463
464 /* Make sure DELAY() is initialized. */
465 DELAY(1);
466 again = true;
467 }
468
469 /* further PCB init done later. */
470
471 switch (caa->cpu_role) {
472 case CPU_ROLE_SP:
473 atomic_or_32(&ci->ci_flags, CPUF_SP);
474 cpu_identify(ci);
475 #if 0
476 x86_errata();
477 #endif
478 x86_cpu_idle_init();
479
480 break;
481
482 case CPU_ROLE_BP:
483 atomic_or_32(&ci->ci_flags, CPUF_BSP);
484 cpu_identify(ci);
485 cpu_init(ci);
486 #if 0
487 x86_errata();
488 #endif
489 x86_cpu_idle_init();
490
491 break;
492
493 case CPU_ROLE_AP:
494 atomic_or_32(&ci->ci_flags, CPUF_AP);
495
496 /*
497 * report on an AP
498 */
499
500 #if defined(MULTIPROCESSOR)
501 /* interrupt handler stack */
502 cpu_intr_init(ci);
503
504 /* Setup gdt */
505 gdt_alloc_cpu(ci);
506 //gdt_init_cpu(ci);
507
508 cpu_set_tss_gates(ci);
509 pmap_cpu_init_early(ci);
510 pmap_cpu_init_late(ci);
511 cpu_start_secondary(ci);
512
513 if (ci->ci_flags & CPUF_PRESENT) {
514 struct cpu_info *tmp;
515
516 cpu_identify(ci);
517 tmp = cpu_info_list;
518 while (tmp->ci_next)
519 tmp = tmp->ci_next;
520
521 tmp->ci_next = ci;
522 }
523 #else
524 aprint_error(": not started\n");
525 #endif
526 break;
527
528 default:
529 aprint_normal("\n");
530 panic("unknown processor type??\n");
531 }
532
533 pat_init(ci);
534 atomic_or_32(&cpus_attached, ci->ci_cpumask);
535
536 #if 0
537 if (!pmf_device_register(self, cpu_suspend, cpu_resume))
538 aprint_error_dev(self, "couldn't establish power handler\n");
539 #endif
540
541 #ifdef MPVERBOSE
542 if (mp_verbose) {
543 struct lwp *l = ci->ci_data.cpu_idlelwp;
544 struct pcb *pcb = lwp_getpcb(l);
545
546 aprint_verbose_dev(self,
547 "idle lwp at %p, idle sp at 0x%p\n",
548 l,
549 #ifdef i386
550 (void *)pcb->pcb_esp
551 #else /* i386 */
552 (void *)pcb->pcb_rsp
553 #endif /* i386 */
554 );
555
556 }
557 #endif /* MPVERBOSE */
558 }
559
560 /*
561 * Initialize the processor appropriately.
562 */
563
564 void
565 cpu_init(struct cpu_info *ci)
566 {
567
568 /*
569 * On a P6 or above, enable global TLB caching if the
570 * hardware supports it.
571 */
572 if (cpu_feature[0] & CPUID_PGE)
573 lcr4(rcr4() | CR4_PGE); /* enable global TLB caching */
574
575 #ifdef XXXMTRR
576 /*
577 * On a P6 or above, initialize MTRR's if the hardware supports them.
578 */
579 if (cpu_feature[0] & CPUID_MTRR) {
580 if ((ci->ci_flags & CPUF_AP) == 0)
581 i686_mtrr_init_first();
582 mtrr_init_cpu(ci);
583 }
584 #endif
585 /*
586 * If we have FXSAVE/FXRESTOR, use them.
587 */
588 if (cpu_feature[0] & CPUID_FXSR) {
589 lcr4(rcr4() | CR4_OSFXSR);
590
591 /*
592 * If we have SSE/SSE2, enable XMM exceptions.
593 */
594 if (cpu_feature[0] & (CPUID_SSE|CPUID_SSE2))
595 lcr4(rcr4() | CR4_OSXMMEXCPT);
596 }
597
598 #ifdef __x86_64__
599 /* No user PGD mapped for this CPU yet */
600 ci->ci_xen_current_user_pgd = 0;
601 #endif
602
603 atomic_or_32(&cpus_running, ci->ci_cpumask);
604 atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
605
606 /* XXX: register vcpu_register_runstate_memory_area, and figure out how to make sure this VCPU is running ? */
607 }
608
609
610 #ifdef MULTIPROCESSOR
611
612 void
613 cpu_boot_secondary_processors(void)
614 {
615 struct cpu_info *ci;
616 u_long i;
617 for (i = 0; i < maxcpus; i++) {
618 ci = cpu_lookup(i);
619 if (ci == NULL)
620 continue;
621 if (ci->ci_data.cpu_idlelwp == NULL)
622 continue;
623 if ((ci->ci_flags & CPUF_PRESENT) == 0)
624 continue;
625 if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
626 continue;
627 cpu_boot_secondary(ci);
628 }
629
630 x86_mp_online = true;
631 }
632
633 static void
634 cpu_init_idle_lwp(struct cpu_info *ci)
635 {
636 struct lwp *l = ci->ci_data.cpu_idlelwp;
637 struct pcb *pcb = lwp_getpcb(l);
638
639 pcb->pcb_cr0 = rcr0();
640 }
641
642 void
643 cpu_init_idle_lwps(void)
644 {
645 struct cpu_info *ci;
646 u_long i;
647
648 for (i = 0; i < maxcpus; i++) {
649 ci = cpu_lookup(i);
650 if (ci == NULL)
651 continue;
652 if (ci->ci_data.cpu_idlelwp == NULL)
653 continue;
654 if ((ci->ci_flags & CPUF_PRESENT) == 0)
655 continue;
656 cpu_init_idle_lwp(ci);
657 }
658 }
659
660 static void
661 cpu_start_secondary(struct cpu_info *ci)
662 {
663 int i;
664
665 aprint_debug_dev(ci->ci_dev, "starting\n");
666
667 ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
668
669 if (CPU_STARTUP(ci, (vaddr_t) cpu_hatch) != 0) {
670 return;
671 }
672
673 /*
674 * wait for it to become ready
675 */
676 for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i > 0; i--) {
677 #ifdef MPDEBUG
678 extern int cpu_trace[3];
679 static int otrace[3];
680 if (memcmp(otrace, cpu_trace, sizeof(otrace)) != 0) {
681 aprint_debug_dev(ci->ci_dev, "trace %02x %02x %02x\n",
682 cpu_trace[0], cpu_trace[1], cpu_trace[2]);
683 memcpy(otrace, cpu_trace, sizeof(otrace));
684 }
685 #endif
686 delay(10);
687 }
688 if ((ci->ci_flags & CPUF_PRESENT) == 0) {
689 aprint_error_dev(ci->ci_dev, "failed to become ready\n");
690 #if defined(MPDEBUG) && defined(DDB)
691 printf("dropping into debugger; continue from here to resume boot\n");
692 Debugger();
693 #endif
694 }
695
696 CPU_START_CLEANUP(ci);
697 }
698
699 void
700 cpu_boot_secondary(struct cpu_info *ci)
701 {
702 int i;
703 atomic_or_32(&ci->ci_flags, CPUF_GO);
704 for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i > 0; i--) {
705 delay(10);
706 }
707 if ((ci->ci_flags & CPUF_RUNNING) == 0) {
708 aprint_error_dev(ci->ci_dev, "CPU failed to start\n");
709 #if defined(MPDEBUG) && defined(DDB)
710 printf("dropping into debugger; continue from here to resume boot\n");
711 Debugger();
712 #endif
713 }
714 }
715
716 /*
717 * APs end up here immediately after initialisation and VCPUOP_up in
718 * mp_cpu_start().
719 * At this point, we are running in the idle pcb/idle stack of the new
720 * CPU. This function jumps to the idle loop and starts looking for
721 * work.
722 */
723 extern void x86_64_tls_switch(struct lwp *);
724 void
725 cpu_hatch(void *v)
726 {
727 struct cpu_info *ci = (struct cpu_info *)v;
728 struct pcb *pcb;
729 int s, i;
730
731 /* Setup TLS and kernel GS */
732 cpu_init_msrs(ci, true);
733 cpu_probe(ci);
734
735 atomic_or_32(&ci->ci_flags, CPUF_PRESENT);
736
737 while ((ci->ci_flags & CPUF_GO) == 0) {
738 /* Don't use delay, boot CPU may be patching the text. */
739 for (i = 10000; i != 0; i--)
740 x86_pause();
741 }
742
743 /* Because the text may have been patched in x86_patch(). */
744 x86_flush();
745
746 KASSERT((ci->ci_flags & CPUF_RUNNING) == 0);
747
748 pcb = lwp_getpcb(curlwp);
749 pcb->pcb_cr3 = pmap_pdirpa(pmap_kernel(), 0); /* XXX: consider using pmap_load() ? */
750 pcb = lwp_getpcb(ci->ci_data.cpu_idlelwp);
751 lcr0(pcb->pcb_cr0);
752
753 cpu_init_idt();
754 gdt_init_cpu(ci);
755 xen_ipi_init();
756
757 xen_initclocks();
758
759 /* XXX: lapic_initclocks(); */
760
761 #ifdef i386
762 #if NNPX > 0
763 npxinit(ci);
764 #endif
765 #else
766 fpuinit(ci);
767 /* XXX: fixme compile fpuinit(ci); */
768 #endif
769
770 lldt(GSEL(GLDT_SEL, SEL_KPL));
771 ltr(ci->ci_tss_sel);
772
773 cpu_init(ci);
774 cpu_get_tsc_freq(ci);
775
776 s = splhigh();
777 x86_enable_intr();
778 splx(s);
779 #if 0
780 x86_errata();
781 #endif
782
783 aprint_debug_dev(ci->ci_dev, "running\n");
784
785 printf("\n\nAbout to switch to idle_loop()\n\n");
786
787 cpu_switchto(NULL, ci->ci_data.cpu_idlelwp, true);
788
789 panic("switch to idle_loop context returned!\n");
790 /* NOTREACHED */
791 }
792
793 #if defined(DDB)
794
795 #include <ddb/db_output.h>
796 #include <machine/db_machdep.h>
797
798 /*
799 * Dump CPU information from ddb.
800 */
801 void
802 cpu_debug_dump(void)
803 {
804 struct cpu_info *ci;
805 CPU_INFO_ITERATOR cii;
806
807 db_printf("addr dev id flags ipis curlwp fpcurlwp\n");
808 for (CPU_INFO_FOREACH(cii, ci)) {
809 db_printf("%p %s %ld %x %x %10p %10p\n",
810 ci,
811 ci->ci_dev == NULL ? "BOOT" : device_xname(ci->ci_dev),
812 (long)ci->ci_cpuid,
813 ci->ci_flags, ci->ci_ipis,
814 ci->ci_curlwp,
815 ci->ci_fpcurlwp);
816 }
817 }
818 #endif /* DDB */
819
820 #endif /* MULTIPROCESSOR */
821
822 #ifdef i386
823 #if 0
824 static void
825 tss_init(struct i386tss *tss, void *stack, void *func)
826 {
827 memset(tss, 0, sizeof *tss);
828 tss->tss_esp0 = tss->tss_esp = (int)((char *)stack + USPACE - 16);
829 tss->tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
830 tss->__tss_cs = GSEL(GCODE_SEL, SEL_KPL);
831 tss->tss_fs = GSEL(GCPU_SEL, SEL_KPL);
832 tss->tss_gs = tss->__tss_es = tss->__tss_ds =
833 tss->__tss_ss = GSEL(GDATA_SEL, SEL_KPL);
834 tss->tss_cr3 = pmap_kernel()->pm_pdirpa;
835 tss->tss_esp = (int)((char *)stack + USPACE - 16);
836 tss->tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
837 tss->__tss_eflags = PSL_MBO | PSL_NT; /* XXX not needed? */
838 tss->__tss_eip = (int)func;
839 }
840 #endif
841
842 /* XXX */
843 #define IDTVEC(name) __CONCAT(X, name)
844 typedef void (vector)(void);
845 extern vector IDTVEC(tss_trap08);
846 #ifdef DDB
847 extern vector Xintrddbipi;
848 extern int ddb_vec;
849 #endif
850
851 static void
852 cpu_set_tss_gates(struct cpu_info *ci)
853 {
854 #if 0
855 struct segment_descriptor sd;
856
857 ci->ci_doubleflt_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
858 UVM_KMF_WIRED);
859 tss_init(&ci->ci_doubleflt_tss, ci->ci_doubleflt_stack,
860 IDTVEC(tss_trap08));
861 setsegment(&sd, &ci->ci_doubleflt_tss, sizeof(struct i386tss) - 1,
862 SDT_SYS386TSS, SEL_KPL, 0, 0);
863 ci->ci_gdt[GTRAPTSS_SEL].sd = sd;
864 setgate(&idt[8], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
865 GSEL(GTRAPTSS_SEL, SEL_KPL));
866 #endif
867
868 #if defined(DDB) && defined(MULTIPROCESSOR)
869 /*
870 * Set up separate handler for the DDB IPI, so that it doesn't
871 * stomp on a possibly corrupted stack.
872 *
873 * XXX overwriting the gate set in db_machine_init.
874 * Should rearrange the code so that it's set only once.
875 */
876 ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
877 UVM_KMF_WIRED);
878 tss_init(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
879 Xintrddbipi);
880
881 setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
882 SDT_SYS386TSS, SEL_KPL, 0, 0);
883 ci->ci_gdt[GIPITSS_SEL].sd = sd;
884
885 setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
886 GSEL(GIPITSS_SEL, SEL_KPL));
887 #endif
888 }
889 #else
890 static void
891 cpu_set_tss_gates(struct cpu_info *ci)
892 {
893
894 }
895 #endif /* i386 */
896
897 extern void hypervisor_callback(void);
898 extern void failsafe_callback(void);
899 #ifdef __x86_64__
900 typedef void (vector)(void);
901 extern vector Xsyscall, Xsyscall32;
902 #endif
903
904 /*
905 * Setup the "trampoline". On Xen, we setup nearly all cpu context
906 * outside a trampoline, so we prototype and call targetrip like so:
907 * void targetrip(struct cpu_info *);
908 */
909
910 static void
911 gdt_prepframes(paddr_t *frames, vaddr_t base, uint32_t entries)
912 {
913 int i;
914 for (i = 0; i < roundup(entries, PAGE_SIZE) >> PAGE_SHIFT; i++) {
915
916 frames[i] = ((paddr_t) xpmap_ptetomach(
917 (pt_entry_t *) (base + (i << PAGE_SHIFT))))
918 >> PAGE_SHIFT;
919
920 /* Mark Read-only */
921 pmap_pte_clearbits(kvtopte(base + (i << PAGE_SHIFT)),
922 PG_RW);
923 }
924 }
925
926 extern char *ldtstore; /* XXX: Xen MP todo */
927
928 static void
929 xen_init_amd64_vcpuctxt(struct cpu_info *ci,
930 struct vcpu_guest_context *initctx,
931 void targetrip(struct cpu_info *))
932 {
933 /* page frames to point at GDT */
934 extern int gdt_size;
935 paddr_t frames[16];
936 psize_t gdt_ents;
937
938 struct lwp *l;
939 struct pcb *pcb;
940
941 volatile struct vcpu_info *vci;
942
943 KASSERT(ci != NULL);
944 KASSERT(ci != &cpu_info_primary);
945 KASSERT(initctx != NULL);
946 KASSERT(targetrip != NULL);
947
948 memset(initctx, 0, sizeof *initctx);
949
950 gdt_ents = roundup(gdt_size, PAGE_SIZE) >> PAGE_SHIFT; /* XXX: re-investigate roundup(gdt_size... ) for gdt_ents. */
951 KASSERT(gdt_ents <= 16);
952
953 gdt_prepframes(frames, (vaddr_t) ci->ci_gdt, gdt_ents);
954
955 /* XXX: The stuff in here is amd64 specific. move to mptramp.[Sc] ? */
956
957 /* Initialise the vcpu context: We use idle_loop()'s pcb context. */
958
959 l = ci->ci_data.cpu_idlelwp;
960
961 KASSERT(l != NULL);
962 pcb = lwp_getpcb(l);
963 KASSERT(pcb != NULL);
964
965 /* resume with interrupts off */
966 vci = ci->ci_vcpu;
967 vci->evtchn_upcall_mask = 1;
968 xen_mb();
969
970 /* resume in kernel-mode */
971 initctx->flags = VGCF_in_kernel | VGCF_online;
972
973 /* Stack and entry points */
974 initctx->user_regs.rbp = pcb->pcb_rbp;
975 initctx->user_regs.rdi = (uint64_t) ci; /* targetrip(ci); */
976 initctx->user_regs.rip = (vaddr_t) targetrip;
977
978 initctx->user_regs.cs = GSEL(GCODE_SEL, SEL_KPL);
979
980 initctx->user_regs.rflags = pcb->pcb_flags;
981 initctx->user_regs.rsp = pcb->pcb_rsp;
982
983 /* Data segments */
984 initctx->user_regs.ss = GSEL(GDATA_SEL, SEL_KPL);
985 initctx->user_regs.es = GSEL(GDATA_SEL, SEL_KPL);
986 initctx->user_regs.ds = GSEL(GDATA_SEL, SEL_KPL);
987
988 /* GDT */
989 memcpy(initctx->gdt_frames, frames, sizeof frames);
990 initctx->gdt_ents = gdt_ents;
991
992 /* LDT */
993 initctx->ldt_base = (unsigned long) ldtstore;
994 initctx->ldt_ents = LDT_SIZE >> 3;
995
996 /* Kernel context state */
997 initctx->kernel_ss = GSEL(GDATA_SEL, SEL_KPL);
998 initctx->kernel_sp = pcb->pcb_rsp0;
999 initctx->ctrlreg[0] = pcb->pcb_cr0;
1000 initctx->ctrlreg[1] = 0; /* "resuming" from kernel - no User cr3. */
1001 initctx->ctrlreg[2] = pcb->pcb_cr2; /* XXX: */
1002 initctx->ctrlreg[3] = xpmap_ptom(pcb->pcb_cr3);
1003 initctx->ctrlreg[4] = CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT;
1004
1005
1006 /* Xen callbacks */
1007 initctx->event_callback_eip = (unsigned long) hypervisor_callback;
1008 initctx->failsafe_callback_eip = (unsigned long) failsafe_callback;
1009 initctx->syscall_callback_eip = (unsigned long) Xsyscall;
1010
1011 return;
1012 }
1013
1014 int
1015 mp_cpu_start(struct cpu_info *ci, vaddr_t target)
1016 {
1017
1018 int hyperror;
1019 struct vcpu_guest_context vcpuctx;
1020
1021 KASSERT(ci != NULL);
1022 KASSERT(ci != &cpu_info_primary);
1023 KASSERT(ci->ci_flags & CPUF_AP);
1024
1025 xen_init_amd64_vcpuctxt(ci, &vcpuctx, (void (*)(struct cpu_info *))target);
1026
1027 /* Initialise the given vcpu to execute cpu_hatch(ci); */
1028 if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_initialise, ci->ci_cpuid, &vcpuctx))) {
1029 aprint_error(": context initialisation failed. errno = %d\n", hyperror);
1030 return hyperror;
1031 }
1032
1033 /* Start it up */
1034
1035 /* First bring it down - yay, thanks Xen documentation for omitting this slight detail - lost only about 1 week reading through crap */
1036 if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL))) {
1037 aprint_error(": VCPUOP_down hypervisor command failed. errno = %d\n", hyperror);
1038 return hyperror;
1039 }
1040
1041 if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_up, ci->ci_cpuid, NULL))) {
1042 aprint_error(": VCPUOP_up hypervisor command failed. errno = %d\n", hyperror);
1043 return hyperror;
1044 }
1045
1046 if (!vcpu_is_up(ci)) {
1047 aprint_error(": did not come up\n");
1048 return -1;
1049 }
1050
1051 return 0;
1052 }
1053
1054 void
1055 mp_cpu_start_cleanup(struct cpu_info *ci)
1056 {
1057 #if 0
1058 /*
1059 * Ensure the NVRAM reset byte contains something vaguely sane.
1060 */
1061
1062 outb(IO_RTC, NVRAM_RESET);
1063 outb(IO_RTC+1, NVRAM_RESET_RST);
1064 #endif
1065 if (vcpu_is_up(ci)) {
1066 aprint_debug_dev(ci->ci_dev, "is started.\n");
1067 }
1068 else {
1069 aprint_error_dev(ci->ci_dev, "did not start up.\n");
1070 }
1071
1072 }
1073
1074 void
1075 cpu_init_msrs(struct cpu_info *ci, bool full)
1076 {
1077 #ifdef __x86_64__
1078 if (full) {
1079 HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
1080 HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (uint64_t) ci);
1081 HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
1082 }
1083 #endif /* __x86_64__ */
1084
1085 if (cpu_feature[2] & CPUID_NOX)
1086 wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE);
1087
1088 }
1089
1090 void
1091 cpu_offline_md(void)
1092 {
1093 int s;
1094
1095 s = splhigh();
1096 #ifdef __i386__
1097 npxsave_cpu(true);
1098 #else
1099 fpusave_cpu(true);
1100 #endif
1101 splx(s);
1102 }
1103
1104 #if 0
1105 /* XXX joerg restructure and restart CPUs individually */
1106 static bool
1107 cpu_suspend(device_t dv, const pmf_qual_t *qual)
1108 {
1109 struct cpu_softc *sc = device_private(dv);
1110 struct cpu_info *ci = sc->sc_info;
1111 int err;
1112
1113 if (ci->ci_flags & CPUF_PRIMARY)
1114 return true;
1115 if (ci->ci_data.cpu_idlelwp == NULL)
1116 return true;
1117 if ((ci->ci_flags & CPUF_PRESENT) == 0)
1118 return true;
1119
1120 sc->sc_wasonline = !(ci->ci_schedstate.spc_flags & SPCF_OFFLINE);
1121
1122 if (sc->sc_wasonline) {
1123 mutex_enter(&cpu_lock);
1124 err = cpu_setstate(ci, false);
1125 mutex_exit(&cpu_lock);
1126
1127 if (err)
1128 return false;
1129 }
1130
1131 return true;
1132 }
1133
1134 static bool
1135 cpu_resume(device_t dv, const pmf_qual_t *qual)
1136 {
1137 struct cpu_softc *sc = device_private(dv);
1138 struct cpu_info *ci = sc->sc_info;
1139 int err = 0;
1140
1141 if (ci->ci_flags & CPUF_PRIMARY)
1142 return true;
1143 if (ci->ci_data.cpu_idlelwp == NULL)
1144 return true;
1145 if ((ci->ci_flags & CPUF_PRESENT) == 0)
1146 return true;
1147
1148 if (sc->sc_wasonline) {
1149 mutex_enter(&cpu_lock);
1150 err = cpu_setstate(ci, true);
1151 mutex_exit(&cpu_lock);
1152 }
1153
1154 return err == 0;
1155 }
1156 #endif
1157
1158 void
1159 cpu_get_tsc_freq(struct cpu_info *ci)
1160 {
1161 uint32_t vcpu_tversion;
1162 const volatile vcpu_time_info_t *tinfo = &ci->ci_vcpu->time;
1163
1164 vcpu_tversion = tinfo->version;
1165 while (tinfo->version == vcpu_tversion); /* Wait for a time update. XXX: timeout ? */
1166
1167 uint64_t freq = 1000000000ULL << 32;
1168 freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
1169 if ( tinfo->tsc_shift < 0 )
1170 freq = freq << -tinfo->tsc_shift;
1171 else
1172 freq = freq >> tinfo->tsc_shift;
1173 ci->ci_data.cpu_cc_freq = freq;
1174 }
1175
1176 void
1177 x86_cpu_idle_xen(void)
1178 {
1179 struct cpu_info *ci = curcpu();
1180
1181 KASSERT(ci->ci_ilevel == IPL_NONE);
1182
1183 x86_disable_intr();
1184 if (!__predict_false(ci->ci_want_resched)) {
1185 idle_block();
1186
1187 } else {
1188 x86_enable_intr();
1189 }
1190 }
1191
1192 /*
1193 * Loads pmap for the current CPU.
1194 */
1195 void
1196 cpu_load_pmap(struct pmap *pmap)
1197 {
1198 #ifdef i386
1199 #ifdef PAE
1200 int i, s;
1201 struct cpu_info *ci;
1202
1203 s = splvm(); /* just to be safe */
1204 xpq_queue_lock();
1205 ci = curcpu();
1206 paddr_t l3_pd = xpmap_ptom_masked(ci->ci_pae_l3_pdirpa);
1207 /* don't update the kernel L3 slot */
1208 for (i = 0 ; i < PDP_SIZE - 1; i++) {
1209 xpq_queue_pte_update(l3_pd + i * sizeof(pd_entry_t),
1210 xpmap_ptom(pmap->pm_pdirpa[i]) | PG_V);
1211 }
1212 xpq_queue_unlock();
1213 splx(s);
1214 tlbflush();
1215 #else /* PAE */
1216 lcr3(pmap_pdirpa(pmap, 0));
1217 #endif /* PAE */
1218 #endif /* i386 */
1219
1220 #ifdef __x86_64__
1221 int i, s;
1222 pd_entry_t *old_pgd, *new_pgd;
1223 paddr_t addr;
1224 struct cpu_info *ci;
1225
1226 /* kernel pmap always in cr3 and should never go in user cr3 */
1227 if (pmap_pdirpa(pmap, 0) != pmap_pdirpa(pmap_kernel(), 0)) {
1228 ci = curcpu();
1229 /*
1230 * Map user space address in kernel space and load
1231 * user cr3
1232 */
1233 s = splvm();
1234 new_pgd = pmap->pm_pdir;
1235 old_pgd = pmap_kernel()->pm_pdir;
1236 addr = xpmap_ptom(pmap_pdirpa(pmap_kernel(), 0));
1237 xpq_queue_lock();
1238 for (i = 0; i < PDIR_SLOT_PTE;
1239 i++, addr += sizeof(pd_entry_t)) {
1240 if ((new_pgd[i] & PG_V) || (old_pgd[i] & PG_V))
1241 xpq_queue_pte_update(addr, new_pgd[i]);
1242 }
1243 xpq_queue_unlock();
1244 tlbflush();
1245 xpq_queue_lock();
1246 xen_set_user_pgd(pmap_pdirpa(pmap, 0));
1247 ci->ci_xen_current_user_pgd = pmap_pdirpa(pmap, 0);
1248 xpq_queue_unlock();
1249 splx(s);
1250 }
1251 #endif /* __x86_64__ */
1252 }
1253