Home | History | Annotate | Line # | Download | only in x86
cpu.c revision 1.1.2.1
      1 /*	$NetBSD: cpu.c,v 1.1.2.1 2007/10/17 21:08:20 bouyer 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  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by RedBack Networks Inc.
     10  *
     11  * Author: Bill Sommerfeld
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *        This product includes software developed by the NetBSD
     24  *        Foundation, Inc. and its contributors.
     25  * 4. Neither the name of The NetBSD Foundation nor the names of its
     26  *    contributors may be used to endorse or promote products derived
     27  *    from this software without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     39  * POSSIBILITY OF SUCH DAMAGE.
     40  */
     41 
     42 /*
     43  * Copyright (c) 1999 Stefan Grefen
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. All advertising materials mentioning features or use of this software
     54  *    must display the following acknowledgement:
     55  *      This product includes software developed by the NetBSD
     56  *      Foundation, Inc. and its contributors.
     57  * 4. Neither the name of The NetBSD Foundation nor the names of its
     58  *    contributors may be used to endorse or promote products derived
     59  *    from this software without specific prior written permission.
     60  *
     61  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
     62  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
     65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71  * SUCH DAMAGE.
     72  */
     73 
     74 #include <sys/cdefs.h>
     75 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.1.2.1 2007/10/17 21:08:20 bouyer Exp $");
     76 
     77 #include "opt_ddb.h"
     78 #include "opt_multiprocessor.h"
     79 #include "opt_mpbios.h"		/* for MPDEBUG */
     80 #include "opt_mtrr.h"
     81 #include "opt_xen.h"
     82 
     83 #include "lapic.h"
     84 #include "ioapic.h"
     85 
     86 #include <sys/param.h>
     87 #include <sys/proc.h>
     88 #include <sys/user.h>
     89 #include <sys/systm.h>
     90 #include <sys/device.h>
     91 #include <sys/malloc.h>
     92 
     93 #include <uvm/uvm_extern.h>
     94 
     95 #include <machine/cpu.h>
     96 #include <machine/cpufunc.h>
     97 #include <machine/cpuvar.h>
     98 #include <machine/pmap.h>
     99 #include <machine/vmparam.h>
    100 #include <machine/mpbiosvar.h>
    101 #include <machine/pcb.h>
    102 #include <machine/specialreg.h>
    103 #include <machine/segments.h>
    104 #include <machine/gdt.h>
    105 #include <machine/mtrr.h>
    106 #include <machine/pio.h>
    107 
    108 #ifdef XEN3
    109 #include <xen/vcpuvar.h>
    110 #endif
    111 
    112 #if NLAPIC > 0
    113 #include <machine/apicvar.h>
    114 #include <machine/i82489reg.h>
    115 #include <machine/i82489var.h>
    116 #endif
    117 
    118 #if NIOAPIC > 0
    119 #include <machine/i82093var.h>
    120 #endif
    121 
    122 #include <dev/ic/mc146818reg.h>
    123 #include <dev/isa/isareg.h>
    124 
    125 int     cpu_match(struct device *, struct cfdata *, void *);
    126 void    cpu_attach(struct device *, struct device *, void *);
    127 #ifdef XEN3
    128 int     vcpu_match(struct device *, struct cfdata *, void *);
    129 void    vcpu_attach(struct device *, struct device *, void *);
    130 #endif
    131 void    cpu_attach_common(struct device *, struct device *, void *);
    132 
    133 struct cpu_softc {
    134 	struct device sc_dev;		/* device tree glue */
    135 	struct cpu_info *sc_info;	/* pointer to CPU info */
    136 };
    137 
    138 int mp_cpu_start(struct cpu_info *);
    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_DECL(cpu, sizeof(struct cpu_softc),
    144     cpu_match, cpu_attach, NULL, NULL);
    145 #ifdef XEN3
    146 CFATTACH_DECL(vcpu, sizeof(struct cpu_softc),
    147     vcpu_match, vcpu_attach, NULL, NULL);
    148 #endif
    149 
    150 /*
    151  * Statically-allocated CPU info for the primary CPU (or the only
    152  * CPU, on uniprocessors).  The CPU info list is initialized to
    153  * point at it.
    154  */
    155 #ifdef TRAPLOG
    156 #include <machine/tlog.h>
    157 struct tlog tlog_primary;
    158 #endif
    159 struct cpu_info cpu_info_primary = {
    160 	.ci_self = &cpu_info_primary,
    161 #ifndef __x86_64__
    162 	.ci_self150 = (uint8_t *)&cpu_info_primary + 0x150,
    163 #endif
    164 	.ci_curlwp = &lwp0,
    165 #ifdef TRAPLOG
    166 	.ci_tlog = &tlog_primary,
    167 #endif
    168 
    169 };
    170 struct cpu_info phycpu_info_primary = {
    171 	.ci_self = &phycpu_info_primary,
    172 #ifndef __x86_64__
    173 	.ci_self150 = (uint8_t *)&phycpu_info_primary + 0x150,
    174 #endif
    175 };
    176 
    177 struct cpu_info *cpu_info_list = &cpu_info_primary;
    178 
    179 static void	cpu_set_tss_gates(struct cpu_info *ci);
    180 
    181 u_int32_t cpus_attached = 0;
    182 
    183 struct cpu_info *phycpu_info[X86_MAXPROCS] = { &cpu_info_primary };
    184 
    185 #ifdef MULTIPROCESSOR
    186 /*
    187  * Array of CPU info structures.  Must be statically-allocated because
    188  * curproc, etc. are used early.
    189  */
    190 struct cpu_info *cpu_info[X86_MAXPROCS] = { &cpu_info_primary };
    191 
    192 u_int32_t cpus_running = 0;
    193 
    194 void    	cpu_hatch(void *);
    195 static void    	cpu_boot_secondary(struct cpu_info *ci);
    196 static void    	cpu_start_secondary(struct cpu_info *ci);
    197 static void	cpu_copy_trampoline(void);
    198 
    199 /*
    200  * Runs once per boot once multiprocessor goo has been detected and
    201  * the local APIC on the boot processor has been mapped.
    202  *
    203  * Called from lapic_boot_init() (from mpbios_scan()).
    204  */
    205 void
    206 cpu_init_first()
    207 {
    208 	int cpunum = lapic_cpu_number();
    209 
    210 	if (cpunum != 0) {
    211 		cpu_info[0] = NULL;
    212 		cpu_info[cpunum] = &cpu_info_primary;
    213 	}
    214 
    215 	cpu_copy_trampoline();
    216 }
    217 #endif
    218 
    219 int
    220 cpu_match(parent, match, aux)
    221 	struct device *parent;
    222 	struct cfdata *match;
    223 	void *aux;
    224 {
    225 
    226 	return 1;
    227 }
    228 
    229 void
    230 cpu_attach(parent, self, aux)
    231 	struct device *parent, *self;
    232 	void *aux;
    233 {
    234 #ifdef XEN3
    235 	struct cpu_softc *sc = (void *) self;
    236 	struct cpu_attach_args *caa = aux;
    237 	struct cpu_info *ci;
    238 	int cpunum = caa->cpu_number;
    239 
    240 	/*
    241 	 * If we're an Application Processor, allocate a cpu_info
    242 	 * structure, otherwise use the primary's.
    243 	 */
    244 	if (caa->cpu_role == CPU_ROLE_AP) {
    245 		ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
    246 		if (phycpu_info[cpunum] != NULL)
    247 			panic("cpu at apic id %d already attached?", cpunum);
    248 		phycpu_info[cpunum] = ci;
    249 	} else {
    250 		ci = &phycpu_info_primary;
    251 		if (cpunum != 0) {
    252 			phycpu_info[0] = NULL;
    253 			phycpu_info[cpunum] = ci;
    254 		}
    255 	}
    256 
    257 	ci->ci_self = ci;
    258 	sc->sc_info = ci;
    259 
    260 	ci->ci_dev = self;
    261 	ci->ci_apicid = caa->cpu_number;
    262 	ci->ci_cpuid = ci->ci_apicid;
    263 
    264 	printf(": ");
    265 	switch (caa->cpu_role) {
    266 	case CPU_ROLE_SP:
    267 		printf("(uniprocessor)\n");
    268 		ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
    269 		break;
    270 
    271 	case CPU_ROLE_BP:
    272 		printf("(boot processor)\n");
    273 		ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
    274 #if NIOAPIC > 0
    275 		ioapic_bsp_id = caa->cpu_number;
    276 #endif
    277 		break;
    278 
    279 	case CPU_ROLE_AP:
    280 		/*
    281 		 * report on an AP
    282 		 */
    283 		printf("(application processor)\n");
    284 		break;
    285 
    286 	default:
    287 		panic("unknown processor type??\n");
    288 	}
    289 	return;
    290 #else
    291 	cpu_attach_common(parent, self, aux);
    292 #endif
    293 }
    294 
    295 #ifdef XEN3
    296 int
    297 vcpu_match(parent, match, aux)
    298 	struct device *parent;
    299 	struct cfdata *match;
    300 	void *aux;
    301 {
    302 	struct vcpu_attach_args *vcaa = aux;
    303 
    304 	if (strcmp(vcaa->vcaa_name, match->cf_name) == 0)
    305 		return 1;
    306 	return 0;
    307 }
    308 
    309 void
    310 vcpu_attach(parent, self, aux)
    311 	struct device *parent, *self;
    312 	void *aux;
    313 {
    314 	struct vcpu_attach_args *vcaa = aux;
    315 
    316 	cpu_attach_common(parent, self, &vcaa->vcaa_caa);
    317 }
    318 #endif
    319 
    320 static void
    321 cpu_vm_init(struct cpu_info *ci)
    322 {
    323 	int ncolors = 2, i;
    324 
    325 	for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
    326 		struct x86_cache_info *cai;
    327 		int tcolors;
    328 
    329 		cai = &ci->ci_cinfo[i];
    330 
    331 		tcolors = atop(cai->cai_totalsize);
    332 		switch(cai->cai_associativity) {
    333 		case 0xff:
    334 			tcolors = 1; /* fully associative */
    335 			break;
    336 		case 0:
    337 		case 1:
    338 			break;
    339 		default:
    340 			tcolors /= cai->cai_associativity;
    341 		}
    342 		ncolors = max(ncolors, tcolors);
    343 	}
    344 
    345 	/*
    346 	 * Knowing the size of the largest cache on this CPU, re-color
    347 	 * our pages.
    348 	 */
    349 	if (ncolors <= uvmexp.ncolors)
    350 		return;
    351 	printf("%s: %d page colors\n", ci->ci_dev->dv_xname, ncolors);
    352 	uvm_page_recolor(ncolors);
    353 }
    354 
    355 void
    356 cpu_attach_common(parent, self, aux)
    357 	struct device *parent, *self;
    358 	void *aux;
    359 {
    360 	struct cpu_softc *sc = (void *) self;
    361 	struct cpu_attach_args *caa = aux;
    362 	struct cpu_info *ci;
    363 #if defined(MULTIPROCESSOR)
    364 	int cpunum = caa->cpu_number;
    365 #endif
    366 
    367 	/*
    368 	 * If we're an Application Processor, allocate a cpu_info
    369 	 * structure, otherwise use the primary's.
    370 	 */
    371 	if (caa->cpu_role == CPU_ROLE_AP) {
    372 		ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
    373 #if defined(MULTIPROCESSOR)
    374 		if (cpu_info[cpunum] != NULL)
    375 			panic("cpu at apic id %d already attached?", cpunum);
    376 		cpu_info[cpunum] = ci;
    377 #endif
    378 #ifdef TRAPLOG
    379 		ci->ci_tlog_base = malloc(sizeof(struct tlog),
    380 		    M_DEVBUF, M_WAITOK);
    381 #endif
    382 	} else {
    383 		ci = &cpu_info_primary;
    384 #if defined(MULTIPROCESSOR)
    385 		if (cpunum != lapic_cpu_number()) {
    386 			panic("%s: running CPU is at apic %d"
    387 			    " instead of at expected %d",
    388 			    sc->sc_dev.dv_xname, lapic_cpu_number(), cpunum);
    389 		}
    390 #endif
    391 	}
    392 
    393 	ci->ci_self = ci;
    394 	sc->sc_info = ci;
    395 
    396 	ci->ci_dev = self;
    397 	ci->ci_apicid = caa->cpu_number;
    398 #ifdef MULTIPROCESSOR
    399 	ci->ci_cpuid = ci->ci_apicid;
    400 #else
    401 	ci->ci_cpuid = 0;	/* False for APs, but they're not used anyway */
    402 #endif
    403 	ci->ci_func = caa->cpu_func;
    404 
    405 #ifndef __x86_64__
    406 	simple_lock_init(&ci->ci_slock);
    407 #endif
    408 
    409 	if (caa->cpu_role == CPU_ROLE_AP) {
    410 #if defined(MULTIPROCESSOR)
    411 		int error;
    412 
    413 		error = mi_cpu_attach(ci);
    414 		if (error != 0) {
    415 			aprint_normal("\n");
    416 			aprint_error("%s: mi_cpu_attach failed with %d\n",
    417 			    sc->sc_dev.dv_xname, error);
    418 			return;
    419 		}
    420 #endif
    421 	} else {
    422 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
    423 	}
    424 
    425 	pmap_reference(pmap_kernel());
    426 #ifndef __x86_64__
    427 	ci->ci_pmap = pmap_kernel();
    428 	ci->ci_tlbstate = TLBSTATE_STALE;
    429 #endif
    430 
    431 	/* further PCB init done later. */
    432 
    433 	printf(": ");
    434 
    435 	switch (caa->cpu_role) {
    436 	case CPU_ROLE_SP:
    437 		printf("(uniprocessor)\n");
    438 		ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
    439 		cpu_intr_init(ci);
    440 		identifycpu(ci);
    441 		cpu_init(ci);
    442 		cpu_set_tss_gates(ci);
    443 		break;
    444 
    445 	case CPU_ROLE_BP:
    446 		printf("apid %d (boot processor)\n", caa->cpu_number);
    447 		ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
    448 		cpu_intr_init(ci);
    449 		identifycpu(ci);
    450 		cpu_init(ci);
    451 		cpu_set_tss_gates(ci);
    452 		break;
    453 
    454 	case CPU_ROLE_AP:
    455 		/*
    456 		 * report on an AP
    457 		 */
    458 		printf("apid %d (application processor)\n", caa->cpu_number);
    459 
    460 #if defined(MULTIPROCESSOR)
    461 		cpu_intr_init(ci);
    462 		gdt_alloc_cpu(ci);
    463 		cpu_set_tss_gates(ci);
    464 		cpu_start_secondary(ci);
    465 		if (ci->ci_flags & CPUF_PRESENT) {
    466 			identifycpu(ci);
    467 			ci->ci_next = cpu_info_list->ci_next;
    468 			cpu_info_list->ci_next = ci;
    469 		}
    470 #else
    471 		printf("%s: not started\n", sc->sc_dev.dv_xname);
    472 #endif
    473 		break;
    474 
    475 	default:
    476 		panic("unknown processor type??\n");
    477 	}
    478 	cpu_vm_init(ci);
    479 
    480 	cpus_attached |= (1 << ci->ci_cpuid);
    481 
    482 #if defined(MULTIPROCESSOR)
    483 	if (mp_verbose) {
    484 		struct lwp *l = ci->ci_data.cpu_idlelwp;
    485 
    486 		aprint_verbose("%s: idle lwp at %p, idle sp at 0x%x\n",
    487 		    sc->sc_dev.dv_xname, l, l->l_addr->u_pcb.pcb_esp);
    488 	}
    489 #endif
    490 }
    491 
    492 /*
    493  * Initialize the processor appropriately.
    494  */
    495 
    496 void
    497 cpu_init(ci)
    498 	struct cpu_info *ci;
    499 {
    500 	/* configure the CPU if needed */
    501 	if (ci->cpu_setup != NULL)
    502 		(*ci->cpu_setup)(ci);
    503 
    504 #if defined(I686_CPU)
    505 	/*
    506 	 * On a P6 or above, enable global TLB caching if the
    507 	 * hardware supports it.
    508 	 */
    509 	if (cpu_feature & CPUID_PGE)
    510 		lcr4(rcr4() | CR4_PGE);	/* enable global TLB caching */
    511 
    512 #ifdef MTRR
    513 	/*
    514 	 * On a P6 or above, initialize MTRR's if the hardware supports them.
    515 	 */
    516 	if (cpu_feature & CPUID_MTRR) {
    517 		if ((ci->ci_flags & CPUF_AP) == 0)
    518 			i686_mtrr_init_first();
    519 		mtrr_init_cpu(ci);
    520 	}
    521 #endif
    522 #endif
    523 #if defined(I686_CPU)
    524 	/*
    525 	 * If we have FXSAVE/FXRESTOR, use them.
    526 	 */
    527 	if (cpu_feature & CPUID_FXSR) {
    528 		lcr4(rcr4() | CR4_OSFXSR);
    529 
    530 		/*
    531 		 * If we have SSE/SSE2, enable XMM exceptions.
    532 		 */
    533 		if (cpu_feature & (CPUID_SSE|CPUID_SSE2))
    534 			lcr4(rcr4() | CR4_OSXMMEXCPT);
    535 	}
    536 #endif /* I686_CPU */
    537 
    538 #ifdef MULTIPROCESSOR
    539 	ci->ci_flags |= CPUF_RUNNING;
    540 	cpus_running |= 1 << ci->ci_cpuid;
    541 #endif
    542 }
    543 
    544 
    545 #ifdef MULTIPROCESSOR
    546 void
    547 cpu_boot_secondary_processors()
    548 {
    549 	struct cpu_info *ci;
    550 	u_long i;
    551 
    552 	for (i=0; i < X86_MAXPROCS; i++) {
    553 		ci = cpu_info[i];
    554 		if (ci == NULL)
    555 			continue;
    556 		if (ci->ci_data.cpu_idlelwp == NULL)
    557 			continue;
    558 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    559 			continue;
    560 		if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
    561 			continue;
    562 		cpu_boot_secondary(ci);
    563 	}
    564 }
    565 
    566 static void
    567 cpu_init_idle_lwp(struct cpu_info *ci)
    568 {
    569 	struct lwp *l = ci->ci_data.cpu_idlelwp;
    570 	struct pcb *pcb = &l->l_addr->u_pcb;
    571 
    572 	pcb->pcb_cr0 = rcr0();
    573 }
    574 
    575 void
    576 cpu_init_idle_lwps()
    577 {
    578 	struct cpu_info *ci;
    579 	u_long i;
    580 
    581 	for (i = 0; i < X86_MAXPROCS; i++) {
    582 		ci = cpu_info[i];
    583 		if (ci == NULL)
    584 			continue;
    585 		if (ci->ci_data.cpu_idlelwp == NULL)
    586 			continue;
    587 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    588 			continue;
    589 		cpu_init_idle_lwp(ci);
    590 	}
    591 }
    592 
    593 void
    594 cpu_start_secondary (ci)
    595 	struct cpu_info *ci;
    596 {
    597 	int i;
    598 	struct pmap *kpm = pmap_kernel();
    599 	extern u_int32_t mp_pdirpa;
    600 
    601 	mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */
    602 
    603 	ci->ci_flags |= CPUF_AP;
    604 
    605 	printf("%s: starting\n", ci->ci_dev->dv_xname);
    606 
    607 	ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
    608 	CPU_STARTUP(ci);
    609 
    610 	/*
    611 	 * wait for it to become ready
    612 	 */
    613 	for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i>0;i--) {
    614 		delay(10);
    615 	}
    616 	if (! (ci->ci_flags & CPUF_PRESENT)) {
    617 		printf("%s: failed to become ready\n", ci->ci_dev->dv_xname);
    618 #if defined(MPDEBUG) && defined(DDB)
    619 		printf("dropping into debugger; continue from here to resume boot\n");
    620 		Debugger();
    621 #endif
    622 	}
    623 
    624 	CPU_START_CLEANUP(ci);
    625 }
    626 
    627 void
    628 cpu_boot_secondary(ci)
    629 	struct cpu_info *ci;
    630 {
    631 	int i;
    632 
    633 	ci->ci_flags |= CPUF_GO; /* XXX atomic */
    634 
    635 	for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i>0;i--) {
    636 		delay(10);
    637 	}
    638 	if (! (ci->ci_flags & CPUF_RUNNING)) {
    639 		printf("CPU failed to start\n");
    640 #if defined(MPDEBUG) && defined(DDB)
    641 		printf("dropping into debugger; continue from here to resume boot\n");
    642 		Debugger();
    643 #endif
    644 	}
    645 }
    646 
    647 /*
    648  * The CPU ends up here when its ready to run
    649  * This is called from code in mptramp.s; at this point, we are running
    650  * in the idle pcb/idle stack of the new CPU.  When this function returns,
    651  * this processor will enter the idle loop and start looking for work.
    652  *
    653  * XXX should share some of this with init386 in machdep.c
    654  */
    655 void
    656 cpu_hatch(void *v)
    657 {
    658 	struct cpu_info *ci = (struct cpu_info *)v;
    659 	int s;
    660 
    661 	cpu_probe_features(ci);
    662 	cpu_feature &= ci->ci_feature_flags;
    663 	/* not on Xen... */
    664 	cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX);
    665 
    666 #ifdef DEBUG
    667 	if (ci->ci_flags & CPUF_PRESENT)
    668 		panic("%s: already running!?", ci->ci_dev->dv_xname);
    669 #endif
    670 
    671 	ci->ci_flags |= CPUF_PRESENT;
    672 
    673 	lapic_enable();
    674 	lapic_initclocks();
    675 
    676 	while ((ci->ci_flags & CPUF_GO) == 0)
    677 		delay(10);
    678 #ifdef DEBUG
    679 	if (ci->ci_flags & CPUF_RUNNING)
    680 		panic("%s: already running!?", ci->ci_dev->dv_xname);
    681 #endif
    682 
    683 	lcr0(ci->ci_data.cpu_idlelwp->l_addr->u_pcb.pcb_cr0);
    684 	cpu_init_idt();
    685 	lapic_set_lvt();
    686 	gdt_init_cpu(ci);
    687 	npxinit(ci);
    688 
    689 	lldt(GSEL(GLDT_SEL, SEL_KPL));
    690 
    691 	cpu_init(ci);
    692 
    693 	s = splhigh();
    694 	lapic_tpr = 0;
    695 	enable_intr();
    696 
    697 	printf("%s: CPU %ld running\n",ci->ci_dev->dv_xname, ci->ci_cpuid);
    698 #if defined(I586_CPU) || defined(I686_CPU)
    699 	if (ci->ci_feature_flags & CPUID_TSC)
    700 		cc_microset(ci);
    701 #endif
    702 	splx(s);
    703 }
    704 
    705 #if defined(DDB)
    706 
    707 #include <ddb/db_output.h>
    708 #include <machine/db_machdep.h>
    709 
    710 /*
    711  * Dump CPU information from ddb.
    712  */
    713 void
    714 cpu_debug_dump(void)
    715 {
    716 	struct cpu_info *ci;
    717 	CPU_INFO_ITERATOR cii;
    718 
    719 	db_printf("addr		dev	id	flags	ipis	curproc		fpcurproc\n");
    720 	for (CPU_INFO_FOREACH(cii, ci)) {
    721 		db_printf("%p	%s	%ld	%x	%x	%10p	%10p\n",
    722 		    ci,
    723 		    ci->ci_dev == NULL ? "BOOT" : ci->ci_dev->dv_xname,
    724 		    ci->ci_cpuid,
    725 		    ci->ci_flags, ci->ci_ipis,
    726 		    ci->ci_curlwp,
    727 		    ci->ci_fpcurlwp);
    728 	}
    729 }
    730 #endif
    731 
    732 static void
    733 cpu_copy_trampoline()
    734 {
    735 	/*
    736 	 * Copy boot code.
    737 	 */
    738 	extern u_char cpu_spinup_trampoline[];
    739 	extern u_char cpu_spinup_trampoline_end[];
    740 	pmap_kenter_pa((vaddr_t)MP_TRAMPOLINE,	/* virtual */
    741 	    (paddr_t)MP_TRAMPOLINE,	/* physical */
    742 	    VM_PROT_ALL);		/* protection */
    743 	memcpy((void *)MP_TRAMPOLINE,
    744 	    cpu_spinup_trampoline,
    745 	    cpu_spinup_trampoline_end-cpu_spinup_trampoline);
    746 }
    747 
    748 #endif
    749 
    750 
    751 /* XXX */
    752 #define IDTVEC(name)	__CONCAT(X, name)
    753 typedef void (vector)(void);
    754 extern vector IDTVEC(tss_trap08);
    755 #ifdef DDB
    756 extern vector Xintrddbipi;
    757 extern int ddb_vec;
    758 #endif
    759 
    760 static void
    761 cpu_set_tss_gates(struct cpu_info *ci)
    762 {
    763 #if defined(DDB) && defined(MULTIPROCESSOR)
    764 	/*
    765 	 * Set up separate handler for the DDB IPI, so that it doesn't
    766 	 * stomp on a possibly corrupted stack.
    767 	 *
    768 	 * XXX overwriting the gate set in db_machine_init.
    769 	 * Should rearrange the code so that it's set only once.
    770 	 */
    771 	ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
    772 	    UVM_KMF_WIRED);
    773 	cpu_init_tss(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
    774 	    Xintrddbipi);
    775 
    776 	setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
    777 	    SDT_SYS386TSS, SEL_KPL, 0, 0);
    778 	ci->ci_gdt[GIPITSS_SEL].sd = sd;
    779 
    780 	setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
    781 	    GSEL(GIPITSS_SEL, SEL_KPL));
    782 #endif
    783 }
    784 
    785 int
    786 mp_cpu_start(struct cpu_info *ci)
    787 {
    788 #if 0
    789 #if NLAPIC > 0
    790 	int error;
    791 #endif
    792 	unsigned short dwordptr[2];
    793 
    794 	/*
    795 	 * "The BSP must initialize CMOS shutdown code to 0Ah ..."
    796 	 */
    797 
    798 	outb(IO_RTC, NVRAM_RESET);
    799 	outb(IO_RTC+1, NVRAM_RESET_JUMP);
    800 
    801 	/*
    802 	 * "and the warm reset vector (DWORD based at 40:67) to point
    803 	 * to the AP startup code ..."
    804 	 */
    805 
    806 	dwordptr[0] = 0;
    807 	dwordptr[1] = MP_TRAMPOLINE >> 4;
    808 
    809 	pmap_kenter_pa (0, 0, VM_PROT_READ|VM_PROT_WRITE);
    810 	memcpy ((u_int8_t *) 0x467, dwordptr, 4);
    811 	pmap_kremove (0, PAGE_SIZE);
    812 
    813 #if NLAPIC > 0
    814 	/*
    815 	 * ... prior to executing the following sequence:"
    816 	 */
    817 
    818 	if (ci->ci_flags & CPUF_AP) {
    819 		if ((error = x86_ipi_init(ci->ci_apicid)) != 0)
    820 			return error;
    821 
    822 		delay(10000);
    823 
    824 		if (cpu_feature & CPUID_APIC) {
    825 
    826 			if ((error = x86_ipi(MP_TRAMPOLINE/PAGE_SIZE,
    827 					     ci->ci_apicid,
    828 					     LAPIC_DLMODE_STARTUP)) != 0)
    829 				return error;
    830 			delay(200);
    831 
    832 			if ((error = x86_ipi(MP_TRAMPOLINE/PAGE_SIZE,
    833 					     ci->ci_apicid,
    834 					     LAPIC_DLMODE_STARTUP)) != 0)
    835 				return error;
    836 			delay(200);
    837 		}
    838 	}
    839 #endif
    840 #endif /* 0 */
    841 	return 0;
    842 }
    843 
    844 void
    845 mp_cpu_start_cleanup(struct cpu_info *ci)
    846 {
    847 #if 0
    848 	/*
    849 	 * Ensure the NVRAM reset byte contains something vaguely sane.
    850 	 */
    851 
    852 	outb(IO_RTC, NVRAM_RESET);
    853 	outb(IO_RTC+1, NVRAM_RESET_RST);
    854 #endif
    855 }
    856 
    857 #ifdef __x86_64__
    858 
    859 void
    860 cpu_init_msrs(struct cpu_info *ci)
    861 {
    862 	HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
    863 	HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (u_int64_t) ci);
    864 	HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
    865 }
    866 #endif	/* __x86_64__ */
    867