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