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