Home | History | Annotate | Line # | Download | only in x86
cpu.c revision 1.27.4.2
      1 /*	$NetBSD: cpu.c,v 1.27.4.2 2008/12/13 01:13:43 haad 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.27.4.2 2008/12/13 01:13:43 haad 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 bool x86_mp_online;
    177 paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
    178 
    179 struct cpu_info *phycpu_info[X86_MAXPROCS] = { &cpu_info_primary };
    180 
    181 #ifdef MULTIPROCESSOR
    182 /*
    183  * Array of CPU info structures.  Must be statically-allocated because
    184  * curproc, etc. are used early.
    185  */
    186 struct cpu_info *cpu_info[X86_MAXPROCS] = { &cpu_info_primary };
    187 
    188 void    	cpu_hatch(void *);
    189 static void    	cpu_boot_secondary(struct cpu_info *ci);
    190 static void    	cpu_start_secondary(struct cpu_info *ci);
    191 static void	cpu_copy_trampoline(void);
    192 
    193 /*
    194  * Runs once per boot once multiprocessor goo has been detected and
    195  * the local APIC on the boot processor has been mapped.
    196  *
    197  * Called from lapic_boot_init() (from mpbios_scan()).
    198  */
    199 void
    200 cpu_init_first(void)
    201 {
    202 	int cpunum = lapic_cpu_number();
    203 
    204 	if (cpunum != 0) {
    205 		cpu_info[0] = NULL;
    206 		cpu_info[cpunum] = &cpu_info_primary;
    207 	}
    208 
    209 	cpu_copy_trampoline();
    210 }
    211 #endif
    212 
    213 int
    214 cpu_match(device_t parent, cfdata_t match, void *aux)
    215 {
    216 
    217 	return 1;
    218 }
    219 
    220 void
    221 cpu_attach(device_t parent, device_t self, void *aux)
    222 {
    223 #ifdef XEN3
    224 	struct cpu_softc *sc = device_private(self);
    225 	struct cpu_attach_args *caa = aux;
    226 	struct cpu_info *ci;
    227 	int cpunum = caa->cpu_number;
    228 
    229 	sc->sc_dev = self;
    230 
    231 	/*
    232 	 * If we're an Application Processor, allocate a cpu_info
    233 	 * structure, otherwise use the primary's.
    234 	 */
    235 	if (caa->cpu_role == CPU_ROLE_AP) {
    236 		ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
    237 		ci->ci_curldt = -1;
    238 		if (phycpu_info[cpunum] != NULL)
    239 			panic("cpu at apic id %d already attached?", cpunum);
    240 		phycpu_info[cpunum] = ci;
    241 	} else {
    242 		ci = &phycpu_info_primary;
    243 		if (cpunum != 0) {
    244 			phycpu_info[0] = NULL;
    245 			phycpu_info[cpunum] = ci;
    246 		}
    247 	}
    248 
    249 	ci->ci_self = ci;
    250 	sc->sc_info = ci;
    251 
    252 	ci->ci_dev = self;
    253 	ci->ci_cpuid = caa->cpu_number;
    254 	ci->ci_vcpu = NULL;
    255 
    256 	printf(": ");
    257 	switch (caa->cpu_role) {
    258 	case CPU_ROLE_SP:
    259 		printf("(uniprocessor)\n");
    260 		ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
    261 		break;
    262 
    263 	case CPU_ROLE_BP:
    264 		printf("(boot processor)\n");
    265 		ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
    266 		break;
    267 
    268 	case CPU_ROLE_AP:
    269 		/*
    270 		 * report on an AP
    271 		 */
    272 		printf("(application processor)\n");
    273 		break;
    274 
    275 	default:
    276 		panic("unknown processor type??\n");
    277 	}
    278 	return;
    279 #else
    280 	cpu_attach_common(parent, self, aux);
    281 #endif
    282 }
    283 
    284 #ifdef XEN3
    285 int
    286 vcpu_match(device_t parent, cfdata_t match, void *aux)
    287 {
    288 	struct vcpu_attach_args *vcaa = aux;
    289 
    290 	if (strcmp(vcaa->vcaa_name, match->cf_name) == 0)
    291 		return 1;
    292 	return 0;
    293 }
    294 
    295 void
    296 vcpu_attach(device_t parent, device_t self, void *aux)
    297 {
    298 	struct vcpu_attach_args *vcaa = aux;
    299 
    300 	cpu_attach_common(parent, self, &vcaa->vcaa_caa);
    301 }
    302 #endif
    303 
    304 static void
    305 cpu_vm_init(struct cpu_info *ci)
    306 {
    307 	int ncolors = 2, i;
    308 
    309 	for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
    310 		struct x86_cache_info *cai;
    311 		int tcolors;
    312 
    313 		cai = &ci->ci_cinfo[i];
    314 
    315 		tcolors = atop(cai->cai_totalsize);
    316 		switch(cai->cai_associativity) {
    317 		case 0xff:
    318 			tcolors = 1; /* fully associative */
    319 			break;
    320 		case 0:
    321 		case 1:
    322 			break;
    323 		default:
    324 			tcolors /= cai->cai_associativity;
    325 		}
    326 		ncolors = max(ncolors, tcolors);
    327 	}
    328 
    329 	/*
    330 	 * Knowing the size of the largest cache on this CPU, re-color
    331 	 * our pages.
    332 	 */
    333 	if (ncolors <= uvmexp.ncolors)
    334 		return;
    335 	aprint_debug_dev(ci->ci_dev, "%d page colors\n", ncolors);
    336 	uvm_page_recolor(ncolors);
    337 }
    338 
    339 void
    340 cpu_attach_common(device_t parent, device_t self, void *aux)
    341 {
    342 	struct cpu_softc *sc = device_private(self);
    343 	struct cpu_attach_args *caa = aux;
    344 	struct cpu_info *ci;
    345 	uintptr_t ptr;
    346 	int cpunum = caa->cpu_number;
    347 
    348 	sc->sc_dev = self;
    349 
    350 	/*
    351 	 * If we're an Application Processor, allocate a cpu_info
    352 	 * structure, otherwise use the primary's.
    353 	 */
    354 	if (caa->cpu_role == CPU_ROLE_AP) {
    355 		if (cpunum >= X86_MAXPROCS) {
    356 			aprint_error(": apic id %d ignored, "
    357 				"please increase X86_MAXPROCS\n", cpunum);
    358 		}
    359 
    360 		aprint_naive(": Application Processor\n");
    361 		ptr = (uintptr_t)malloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
    362 		    M_DEVBUF, M_WAITOK);
    363 		ci = (struct cpu_info *)((ptr + CACHE_LINE_SIZE - 1) &
    364 		    ~(CACHE_LINE_SIZE - 1));
    365 		memset(ci, 0, sizeof(*ci));
    366 #if defined(MULTIPROCESSOR)
    367 		if (cpu_info[cpunum] != NULL)
    368 			panic("cpu at apic id %d already attached?", cpunum);
    369 		cpu_info[cpunum] = ci;
    370 #endif
    371 #ifdef TRAPLOG
    372 		ci->ci_tlog_base = malloc(sizeof(struct tlog),
    373 		    M_DEVBUF, M_WAITOK);
    374 #endif
    375 	} else {
    376 		aprint_naive(": %s Processor\n",
    377 		    caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
    378 		ci = &cpu_info_primary;
    379 #if defined(MULTIPROCESSOR)
    380 		if (cpunum != lapic_cpu_number()) {
    381 			panic("%s: running CPU is at apic %d"
    382 			    " instead of at expected %d",
    383 			    device_xname(sc->sc_dev), lapic_cpu_number(), cpunum);
    384 		}
    385 #endif
    386 	}
    387 
    388 	ci->ci_self = ci;
    389 	sc->sc_info = ci;
    390 
    391 	ci->ci_dev = self;
    392 	ci->ci_cpuid = cpunum;
    393 
    394 	KASSERT(HYPERVISOR_shared_info != NULL);
    395 	ci->ci_vcpu = &HYPERVISOR_shared_info->vcpu_info[cpunum];
    396 
    397 	ci->ci_func = caa->cpu_func;
    398 
    399 	if (caa->cpu_role == CPU_ROLE_AP) {
    400 #if defined(MULTIPROCESSOR)
    401 		int error;
    402 
    403 		error = mi_cpu_attach(ci);
    404 		if (error != 0) {
    405 			aprint_normal("\n");
    406 			aprint_error_dev(sc->sc_dev, "mi_cpu_attach failed with %d\n",
    407 			    error);
    408 			return;
    409 		}
    410 #endif
    411 	} else {
    412 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
    413 	}
    414 
    415 	ci->ci_cpumask = (1 << cpu_index(ci));
    416 	pmap_reference(pmap_kernel());
    417 	ci->ci_pmap = pmap_kernel();
    418 	ci->ci_tlbstate = TLBSTATE_STALE;
    419 
    420 	/* further PCB init done later. */
    421 
    422 	switch (caa->cpu_role) {
    423 	case CPU_ROLE_SP:
    424 		atomic_or_32(&ci->ci_flags,
    425 		     CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY);
    426 		cpu_intr_init(ci);
    427 		cpu_get_tsc_freq(ci);
    428 		cpu_identify(ci);
    429 		cpu_init(ci);
    430 		cpu_set_tss_gates(ci);
    431 		pmap_cpu_init_late(ci);
    432 		x86_cpu_idle_init();
    433 #if 0
    434 		x86_errata();
    435 #endif
    436 		break;
    437 
    438 	case CPU_ROLE_BP:
    439 		atomic_or_32(&ci->ci_flags,
    440 		    CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY);
    441 		cpu_intr_init(ci);
    442 		cpu_get_tsc_freq(ci);
    443 		cpu_identify(ci);
    444 		cpu_init(ci);
    445 		cpu_set_tss_gates(ci);
    446 		pmap_cpu_init_late(ci);
    447 		x86_cpu_idle_init();
    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 
    466 #if defined(MULTIPROCESSOR)
    467 		cpu_intr_init(ci);
    468 		gdt_alloc_cpu(ci);
    469 		cpu_set_tss_gates(ci);
    470 		pmap_cpu_init_early(ci);
    471 		pmap_cpu_init_late(ci);
    472 		cpu_start_secondary(ci);
    473 		if (ci->ci_flags & CPUF_PRESENT) {
    474 			struct cpu_info *tmp;
    475 
    476 			identifycpu(ci);
    477 			tmp = cpu_info_list;
    478 			while (tmp->ci_next)
    479 				tmp = tmp->ci_next;
    480 
    481 			tmp->ci_next = ci;
    482 		}
    483 #else
    484 		aprint_normal_dev(sc->sc_dev, "not started\n");
    485 #endif
    486 		break;
    487 
    488 	default:
    489 		aprint_normal("\n");
    490 		panic("unknown processor type??\n");
    491 	}
    492 	cpu_vm_init(ci);
    493 
    494 	cpus_attached |= (1 << ci->ci_cpuid);
    495 
    496 #if 0
    497 	if (!pmf_device_register(self, cpu_suspend, cpu_resume))
    498 		aprint_error_dev(self, "couldn't establish power handler\n");
    499 #endif
    500 
    501 #if defined(MULTIPROCESSOR)
    502 	if (mp_verbose) {
    503 		struct lwp *l = ci->ci_data.cpu_idlelwp;
    504 
    505 		aprint_verbose_dev(sc->sc_dev, "idle lwp at %p, idle sp at 0x%p\n",
    506 		    l,
    507 #ifdef i386
    508 		    (void *)l->l_addr->u_pcb.pcb_esp
    509 #else
    510 		    (void *)l->l_addr->u_pcb.pcb_rsp
    511 #endif
    512 		);
    513 
    514 	}
    515 #endif
    516 }
    517 
    518 /*
    519  * Initialize the processor appropriately.
    520  */
    521 
    522 void
    523 cpu_init(struct cpu_info *ci)
    524 {
    525 
    526 	/*
    527 	 * On a P6 or above, enable global TLB caching if the
    528 	 * hardware supports it.
    529 	 */
    530 	if (cpu_feature & CPUID_PGE)
    531 		lcr4(rcr4() | CR4_PGE);	/* enable global TLB caching */
    532 
    533 #ifdef XXXMTRR
    534 	/*
    535 	 * On a P6 or above, initialize MTRR's if the hardware supports them.
    536 	 */
    537 	if (cpu_feature & CPUID_MTRR) {
    538 		if ((ci->ci_flags & CPUF_AP) == 0)
    539 			i686_mtrr_init_first();
    540 		mtrr_init_cpu(ci);
    541 	}
    542 #endif
    543 	/*
    544 	 * If we have FXSAVE/FXRESTOR, use them.
    545 	 */
    546 	if (cpu_feature & CPUID_FXSR) {
    547 		lcr4(rcr4() | CR4_OSFXSR);
    548 
    549 		/*
    550 		 * If we have SSE/SSE2, enable XMM exceptions.
    551 		 */
    552 		if (cpu_feature & (CPUID_SSE|CPUID_SSE2))
    553 			lcr4(rcr4() | CR4_OSXMMEXCPT);
    554 	}
    555 
    556 #ifdef MULTIPROCESSOR
    557 	atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
    558 	atomic_or_32(&cpus_running, ci->ci_cpumask);
    559 #endif
    560 }
    561 
    562 
    563 #ifdef MULTIPROCESSOR
    564 void
    565 cpu_boot_secondary_processors(void)
    566 {
    567 	struct cpu_info *ci;
    568 	u_long i;
    569 
    570 	for (i = 0; i < X86_MAXPROCS; i++) {
    571 		ci = cpu_info[i];
    572 		if (ci == NULL)
    573 			continue;
    574 		if (ci->ci_data.cpu_idlelwp == NULL)
    575 			continue;
    576 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    577 			continue;
    578 		if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
    579 			continue;
    580 		cpu_boot_secondary(ci);
    581 	}
    582 
    583 	x86_mp_online = true;
    584 }
    585 
    586 static void
    587 cpu_init_idle_lwp(struct cpu_info *ci)
    588 {
    589 	struct lwp *l = ci->ci_data.cpu_idlelwp;
    590 	struct pcb *pcb = &l->l_addr->u_pcb;
    591 
    592 	pcb->pcb_cr0 = rcr0();
    593 }
    594 
    595 void
    596 cpu_init_idle_lwps(void)
    597 {
    598 	struct cpu_info *ci;
    599 	u_long i;
    600 
    601 	for (i = 0; i < X86_MAXPROCS; i++) {
    602 		ci = cpu_info[i];
    603 		if (ci == NULL)
    604 			continue;
    605 		if (ci->ci_data.cpu_idlelwp == NULL)
    606 			continue;
    607 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    608 			continue;
    609 		cpu_init_idle_lwp(ci);
    610 	}
    611 }
    612 
    613 void
    614 cpu_start_secondary(struct cpu_info *ci)
    615 {
    616 	int i;
    617 	struct pmap *kpm = pmap_kernel();
    618 	extern uint32_t mp_pdirpa;
    619 
    620 	mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */
    621 
    622 	atomic_or_32(&ci->ci_flags, CPUF_AP);
    623 
    624 	aprint_debug_dev(ci->ci_dev, "starting\n");
    625 
    626 	ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
    627 	if (CPU_STARTUP(ci, mp_trampoline_paddr) != 0)
    628 		return;
    629 
    630 	/*
    631 	 * wait for it to become ready
    632 	 */
    633 	for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i > 0; i--) {
    634 #ifdef MPDEBUG
    635 		extern int cpu_trace[3];
    636 		static int otrace[3];
    637 		if (memcmp(otrace, cpu_trace, sizeof(otrace)) != 0) {
    638 			aprint_debug_dev(ci->ci_dev, "trace %02x %02x %02x\n",
    639 				cpu_trace[0], cpu_trace[1], cpu_trace[2]);
    640 			memcpy(otrace, cpu_trace, sizeof(otrace));
    641 		}
    642 #endif
    643 		delay(10);
    644 	}
    645 	if ((ci->ci_flags & CPUF_PRESENT) == 0) {
    646 		aprint_error_dev(ci->ci_dev, "failed to become ready\n");
    647 #if defined(MPDEBUG) && defined(DDB)
    648 		printf("dropping into debugger; continue from here to resume boot\n");
    649 		Debugger();
    650 #endif
    651 	}
    652 
    653 	CPU_START_CLEANUP(ci);
    654 }
    655 
    656 void
    657 cpu_boot_secondary(struct cpu_info *ci)
    658 {
    659 	int i;
    660 
    661 	atomic_or_32(&ci->ci_flags, CPUF_GO);
    662 	for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i > 0; i--) {
    663 		delay(10);
    664 	}
    665 	if ((ci->ci_flags & CPUF_RUNNING) == 0) {
    666 		aprint_error_dev(ci->ci_dev, "CPU failed to start\n");
    667 #if defined(MPDEBUG) && defined(DDB)
    668 		printf("dropping into debugger; continue from here to resume boot\n");
    669 		Debugger();
    670 #endif
    671 	}
    672 }
    673 
    674 /*
    675  * The CPU ends up here when its ready to run
    676  * This is called from code in mptramp.s; at this point, we are running
    677  * in the idle pcb/idle stack of the new CPU.  When this function returns,
    678  * this processor will enter the idle loop and start looking for work.
    679  *
    680  * XXX should share some of this with init386 in machdep.c
    681  */
    682 void
    683 cpu_hatch(void *v)
    684 {
    685 	struct cpu_info *ci = (struct cpu_info *)v;
    686 	int s, i;
    687 	uint32_t blacklist_features;
    688 
    689 #ifdef __x86_64__
    690         cpu_init_msrs(ci, true);
    691 #endif
    692 
    693 	cpu_probe(ci);
    694 
    695 	/* not on Xen... */
    696 	blacklist_features = ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX); /* XXX add CPUID_SVM */
    697 
    698 	cpu_feature &= blacklist_features;
    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