Home | History | Annotate | Line # | Download | only in x86
cpu.c revision 1.85
      1 /*	$NetBSD: cpu.c,v 1.85 2012/02/23 07:30:30 cherry Exp $	*/
      2 /* NetBSD: cpu.c,v 1.18 2004/02/20 17:35:01 yamt Exp  */
      3 
      4 /*-
      5  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      6  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by RedBack Networks Inc.
     11  *
     12  * Author: Bill Sommerfeld
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Copyright (c) 1999 Stefan Grefen
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *      This product includes software developed by the NetBSD
     50  *      Foundation, Inc. and its contributors.
     51  * 4. Neither the name of The NetBSD Foundation nor the names of its
     52  *    contributors may be used to endorse or promote products derived
     53  *    from this software without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
     56  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.85 2012/02/23 07:30:30 cherry Exp $");
     70 
     71 #include "opt_ddb.h"
     72 #include "opt_multiprocessor.h"
     73 #include "opt_mpbios.h"		/* for MPDEBUG */
     74 #include "opt_mtrr.h"
     75 #include "opt_xen.h"
     76 
     77 #include "lapic.h"
     78 #include "ioapic.h"
     79 
     80 #include <sys/param.h>
     81 #include <sys/proc.h>
     82 #include <sys/systm.h>
     83 #include <sys/device.h>
     84 #include <sys/kmem.h>
     85 #include <sys/cpu.h>
     86 #include <sys/cpufreq.h>
     87 #include <sys/atomic.h>
     88 #include <sys/reboot.h>
     89 #include <sys/idle.h>
     90 
     91 #include <uvm/uvm.h>
     92 
     93 #include <machine/cpufunc.h>
     94 #include <machine/cpuvar.h>
     95 #include <machine/pmap.h>
     96 #include <machine/vmparam.h>
     97 #include <machine/mpbiosvar.h>
     98 #include <machine/pcb.h>
     99 #include <machine/specialreg.h>
    100 #include <machine/segments.h>
    101 #include <machine/gdt.h>
    102 #include <machine/mtrr.h>
    103 #include <machine/pio.h>
    104 
    105 #ifdef i386
    106 #include <machine/npx.h>
    107 #else
    108 #include <machine/fpu.h>
    109 #endif
    110 
    111 #include <xen/xen.h>
    112 #include <xen/xen-public/vcpu.h>
    113 #include <xen/vcpuvar.h>
    114 
    115 #if NLAPIC > 0
    116 #include <machine/apicvar.h>
    117 #include <machine/i82489reg.h>
    118 #include <machine/i82489var.h>
    119 #endif
    120 
    121 #include <dev/ic/mc146818reg.h>
    122 #include <dev/isa/isareg.h>
    123 
    124 #if MAXCPUS > 32
    125 #error cpu_info contains 32bit bitmasks
    126 #endif
    127 
    128 static int	cpu_match(device_t, cfdata_t, void *);
    129 static void	cpu_attach(device_t, device_t, void *);
    130 static void	cpu_defer(device_t);
    131 static int	cpu_rescan(device_t, const char *, const int *);
    132 static void	cpu_childdetached(device_t, device_t);
    133 static int	vcpu_match(device_t, cfdata_t, void *);
    134 static void	vcpu_attach(device_t, device_t, void *);
    135 static void	cpu_attach_common(device_t, device_t, void *);
    136 void		cpu_offline_md(void);
    137 
    138 struct cpu_softc {
    139 	device_t sc_dev;		/* device tree glue */
    140 	struct cpu_info *sc_info;	/* pointer to CPU info */
    141 	bool sc_wasonline;
    142 };
    143 
    144 int mp_cpu_start(struct cpu_info *, vaddr_t);
    145 void mp_cpu_start_cleanup(struct cpu_info *);
    146 const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
    147 				      mp_cpu_start_cleanup };
    148 
    149 CFATTACH_DECL2_NEW(cpu, sizeof(struct cpu_softc),
    150     cpu_match, cpu_attach, NULL, NULL, cpu_rescan, cpu_childdetached);
    151 
    152 CFATTACH_DECL_NEW(vcpu, sizeof(struct cpu_softc),
    153     vcpu_match, vcpu_attach, NULL, NULL);
    154 
    155 /*
    156  * Statically-allocated CPU info for the primary CPU (or the only
    157  * CPU, on uniprocessors).  The CPU info list is initialized to
    158  * point at it.
    159  */
    160 #ifdef TRAPLOG
    161 #include <machine/tlog.h>
    162 struct tlog tlog_primary;
    163 #endif
    164 struct cpu_info cpu_info_primary __aligned(CACHE_LINE_SIZE) = {
    165 	.ci_dev = 0,
    166 	.ci_self = &cpu_info_primary,
    167 	.ci_idepth = -1,
    168 	.ci_curlwp = &lwp0,
    169 	.ci_curldt = -1,
    170 	.ci_cpumask = 1,
    171 #ifdef TRAPLOG
    172 	.ci_tlog = &tlog_primary,
    173 #endif
    174 
    175 };
    176 struct cpu_info phycpu_info_primary __aligned(CACHE_LINE_SIZE) = {
    177 	.ci_dev = 0,
    178 	.ci_self = &phycpu_info_primary,
    179 };
    180 
    181 struct cpu_info *cpu_info_list = &cpu_info_primary;
    182 struct cpu_info *phycpu_info_list = &phycpu_info_primary;
    183 
    184 uint32_t cpus_attached = 1;
    185 uint32_t cpus_running = 1;
    186 
    187 uint32_t phycpus_attached = 0;
    188 uint32_t phycpus_running = 0;
    189 
    190 uint32_t cpu_feature[5]; /* X86 CPUID feature bits
    191 			  *	[0] basic features %edx
    192 			  *	[1] basic features %ecx
    193 			  *	[2] extended features %edx
    194 			  *	[3] extended features %ecx
    195 			  *	[4] VIA padlock features
    196 			  */
    197 
    198 bool x86_mp_online;
    199 paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
    200 
    201 #if defined(MULTIPROCESSOR)
    202 void    	cpu_hatch(void *);
    203 static void    	cpu_boot_secondary(struct cpu_info *ci);
    204 static void    	cpu_start_secondary(struct cpu_info *ci);
    205 #endif	/* MULTIPROCESSOR */
    206 
    207 static int
    208 cpu_match(device_t parent, cfdata_t match, void *aux)
    209 {
    210 
    211 	return 1;
    212 }
    213 
    214 static void
    215 cpu_attach(device_t parent, device_t self, void *aux)
    216 {
    217 	struct cpu_softc *sc = device_private(self);
    218 	struct cpu_attach_args *caa = aux;
    219 	struct cpu_info *ci;
    220 	uintptr_t ptr;
    221 	static int nphycpu = 0;
    222 
    223 	sc->sc_dev = self;
    224 
    225 	if (phycpus_attached == ~0) {
    226 		aprint_error(": increase MAXCPUS\n");
    227 		return;
    228 	}
    229 
    230 	/*
    231 	 * If we're an Application Processor, allocate a cpu_info
    232 	 * If we're the first attached CPU use the primary cpu_info,
    233 	 * otherwise allocate a new one
    234 	 */
    235 	aprint_naive("\n");
    236 	aprint_normal("\n");
    237 	if (nphycpu > 0) {
    238 		struct cpu_info *tmp;
    239 		ptr = (uintptr_t)kmem_zalloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
    240 		    KM_SLEEP);
    241 		ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
    242 		ci->ci_curldt = -1;
    243 
    244 		tmp = phycpu_info_list;
    245 		while (tmp->ci_next)
    246 			tmp = tmp->ci_next;
    247 
    248 		tmp->ci_next = ci;
    249 	} else {
    250 		ci = &phycpu_info_primary;
    251 	}
    252 
    253 	ci->ci_self = ci;
    254 	sc->sc_info = ci;
    255 
    256 	ci->ci_dev = self;
    257 	ci->ci_acpiid = caa->cpu_id;
    258 	ci->ci_cpuid = caa->cpu_number;
    259 	ci->ci_vcpu = NULL;
    260 	ci->ci_index = nphycpu++;
    261 	ci->ci_cpumask = (1 << cpu_index(ci));
    262 
    263 	atomic_or_32(&phycpus_attached, ci->ci_cpumask);
    264 
    265 	if (!pmf_device_register(self, NULL, NULL))
    266 		aprint_error_dev(self, "couldn't establish power handler\n");
    267 
    268 	(void)config_defer(self, cpu_defer);
    269 }
    270 
    271 static void
    272 cpu_defer(device_t self)
    273 {
    274 	cpu_rescan(self, NULL, NULL);
    275 }
    276 
    277 static int
    278 cpu_rescan(device_t self, const char *ifattr, const int *locators)
    279 {
    280 	struct cpu_softc *sc = device_private(self);
    281 	struct cpufeature_attach_args cfaa;
    282 	struct cpu_info *ci = sc->sc_info;
    283 
    284 	memset(&cfaa, 0, sizeof(cfaa));
    285 	cfaa.ci = ci;
    286 
    287 	if (ifattr_match(ifattr, "cpufeaturebus")) {
    288 
    289 		if (ci->ci_frequency == NULL) {
    290 			cfaa.name = "frequency";
    291 			ci->ci_frequency = config_found_ia(self,
    292 			    "cpufeaturebus", &cfaa, NULL);
    293 		}
    294 	}
    295 
    296 	return 0;
    297 }
    298 
    299 static void
    300 cpu_childdetached(device_t self, device_t child)
    301 {
    302 	struct cpu_softc *sc = device_private(self);
    303 	struct cpu_info *ci = sc->sc_info;
    304 
    305 	if (ci->ci_frequency == child)
    306 		ci->ci_frequency = NULL;
    307 }
    308 
    309 static int
    310 vcpu_match(device_t parent, cfdata_t match, void *aux)
    311 {
    312 	struct vcpu_attach_args *vcaa = aux;
    313 	struct vcpu_runstate_info vcr;
    314 	int error;
    315 
    316 	if (strcmp(vcaa->vcaa_name, match->cf_name) == 0) {
    317 		error = HYPERVISOR_vcpu_op(VCPUOP_get_runstate_info,
    318 					   vcaa->vcaa_caa.cpu_number,
    319 					   &vcr);
    320 		switch (error) {
    321 		case 0:
    322 			return 1;
    323 		case -ENOENT:
    324 			return 0;
    325 		default:
    326 			panic("Unknown hypervisor error %d returned on vcpu runstate probe\n", error);
    327 		}
    328 	}
    329 
    330 	return 0;
    331 }
    332 
    333 static void
    334 vcpu_attach(device_t parent, device_t self, void *aux)
    335 {
    336 	struct vcpu_attach_args *vcaa = aux;
    337 
    338 	KASSERT(vcaa->vcaa_caa.cpu_func == NULL);
    339 	vcaa->vcaa_caa.cpu_func = &mp_cpu_funcs;
    340 	cpu_attach_common(parent, self, &vcaa->vcaa_caa);
    341 
    342 	if (!pmf_device_register(self, NULL, NULL))
    343 		aprint_error_dev(self, "couldn't establish power handler\n");
    344 }
    345 
    346 static int
    347 vcpu_is_up(struct cpu_info *ci)
    348 {
    349 	KASSERT(ci != NULL);
    350 	return HYPERVISOR_vcpu_op(VCPUOP_is_up, ci->ci_cpuid, NULL);
    351 }
    352 
    353 static void
    354 cpu_vm_init(struct cpu_info *ci)
    355 {
    356 	int ncolors = 2, i;
    357 
    358 	for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
    359 		struct x86_cache_info *cai;
    360 		int tcolors;
    361 
    362 		cai = &ci->ci_cinfo[i];
    363 
    364 		tcolors = atop(cai->cai_totalsize);
    365 		switch(cai->cai_associativity) {
    366 		case 0xff:
    367 			tcolors = 1; /* fully associative */
    368 			break;
    369 		case 0:
    370 		case 1:
    371 			break;
    372 		default:
    373 			tcolors /= cai->cai_associativity;
    374 		}
    375 		ncolors = max(ncolors, tcolors);
    376 	}
    377 
    378 	/*
    379 	 * Knowing the size of the largest cache on this CPU, potentially
    380 	 * re-color our pages.
    381 	 */
    382 	aprint_debug_dev(ci->ci_dev, "%d page colors\n", ncolors);
    383 	uvm_page_recolor(ncolors);
    384 }
    385 
    386 static void
    387 cpu_attach_common(device_t parent, device_t self, void *aux)
    388 {
    389 	struct cpu_softc *sc = device_private(self);
    390 	struct cpu_attach_args *caa = aux;
    391 	struct cpu_info *ci;
    392 	uintptr_t ptr;
    393 	int cpunum = caa->cpu_number;
    394 	static bool again = false;
    395 
    396 	sc->sc_dev = self;
    397 
    398 	/*
    399 	 * If we're an Application Processor, allocate a cpu_info
    400 	 * structure, otherwise use the primary's.
    401 	 */
    402 	if (caa->cpu_role == CPU_ROLE_AP) {
    403 		aprint_naive(": Application Processor\n");
    404 		ptr = (uintptr_t)kmem_alloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
    405 		    KM_SLEEP);
    406 		ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
    407 		memset(ci, 0, sizeof(*ci));
    408 #ifdef TRAPLOG
    409 		ci->ci_tlog_base = kmem_zalloc(sizeof(struct tlog), KM_SLEEP);
    410 #endif
    411 	} else {
    412 		aprint_naive(": %s Processor\n",
    413 		    caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
    414 		ci = &cpu_info_primary;
    415 	}
    416 
    417 	ci->ci_self = ci;
    418 	sc->sc_info = ci;
    419 	ci->ci_dev = self;
    420 	ci->ci_cpuid = cpunum;
    421 
    422 	KASSERT(HYPERVISOR_shared_info != NULL);
    423 	ci->ci_vcpu = &HYPERVISOR_shared_info->vcpu_info[cpunum];
    424 
    425 	KASSERT(ci->ci_func == 0);
    426 	ci->ci_func = caa->cpu_func;
    427 
    428 	/* Must be called before mi_cpu_attach(). */
    429 	cpu_vm_init(ci);
    430 
    431 	if (caa->cpu_role == CPU_ROLE_AP) {
    432 		int error;
    433 
    434 		error = mi_cpu_attach(ci);
    435 
    436 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
    437 		if (error != 0) {
    438 			aprint_normal("\n");
    439 			aprint_error_dev(self,
    440 			    "mi_cpu_attach failed with %d\n", error);
    441 			return;
    442 		}
    443 
    444 	} else {
    445 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
    446 	}
    447 
    448 	ci->ci_cpumask = (1 << cpu_index(ci));
    449 	pmap_reference(pmap_kernel());
    450 	ci->ci_pmap = pmap_kernel();
    451 	ci->ci_tlbstate = TLBSTATE_STALE;
    452 
    453 	/*
    454 	 * Boot processor may not be attached first, but the below
    455 	 * must be done to allow booting other processors.
    456 	 */
    457 	if (!again) {
    458 		atomic_or_32(&ci->ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
    459 		/* Basic init. */
    460 		cpu_intr_init(ci);
    461 		cpu_get_tsc_freq(ci);
    462 		cpu_init(ci);
    463 		pmap_cpu_init_late(ci);
    464 
    465 		/* Every processor needs to init it's own ipi h/w (similar to lapic) */
    466 		xen_ipi_init();
    467 
    468 		/* Make sure DELAY() is initialized. */
    469 		DELAY(1);
    470 		again = true;
    471 	}
    472 
    473 	/* further PCB init done later. */
    474 
    475 	switch (caa->cpu_role) {
    476 	case CPU_ROLE_SP:
    477 		atomic_or_32(&ci->ci_flags, CPUF_SP);
    478 		cpu_identify(ci);
    479 		x86_cpu_idle_init();
    480 
    481 		break;
    482 
    483 	case CPU_ROLE_BP:
    484 		atomic_or_32(&ci->ci_flags, CPUF_BSP);
    485 		cpu_identify(ci);
    486 		x86_cpu_idle_init();
    487 
    488 		break;
    489 
    490 	case CPU_ROLE_AP:
    491 		atomic_or_32(&ci->ci_flags, CPUF_AP);
    492 
    493 		/*
    494 		 * report on an AP
    495 		 */
    496 
    497 #if defined(MULTIPROCESSOR)
    498 		/* interrupt handler stack */
    499 		cpu_intr_init(ci);
    500 
    501 		/* Setup per-cpu memory for gdt */
    502 		gdt_alloc_cpu(ci);
    503 
    504 		pmap_cpu_init_late(ci);
    505 		cpu_start_secondary(ci);
    506 
    507 		if (ci->ci_flags & CPUF_PRESENT) {
    508 			struct cpu_info *tmp;
    509 
    510 			cpu_identify(ci);
    511 			tmp = cpu_info_list;
    512 			while (tmp->ci_next)
    513 				tmp = tmp->ci_next;
    514 
    515 			tmp->ci_next = ci;
    516 		}
    517 #else
    518 		aprint_error(": not started\n");
    519 #endif
    520 		break;
    521 
    522 	default:
    523 		aprint_normal("\n");
    524 		panic("unknown processor type??\n");
    525 	}
    526 
    527 	atomic_or_32(&cpus_attached, ci->ci_cpumask);
    528 
    529 #ifdef MPVERBOSE
    530 	if (mp_verbose) {
    531 		struct lwp *l = ci->ci_data.cpu_idlelwp;
    532 		struct pcb *pcb = lwp_getpcb(l);
    533 
    534 		aprint_verbose_dev(self,
    535 		    "idle lwp at %p, idle sp at 0x%p\n",
    536 		    l,
    537 #ifdef i386
    538 		    (void *)pcb->pcb_esp
    539 #else /* i386 */
    540 		    (void *)pcb->pcb_rsp
    541 #endif /* i386 */
    542 		);
    543 
    544 	}
    545 #endif /* MPVERBOSE */
    546 }
    547 
    548 /*
    549  * Initialize the processor appropriately.
    550  */
    551 
    552 void
    553 cpu_init(struct cpu_info *ci)
    554 {
    555 
    556 	/*
    557 	 * If we have FXSAVE/FXRESTOR, use them.
    558 	 */
    559 	if (cpu_feature[0] & CPUID_FXSR) {
    560 		lcr4(rcr4() | CR4_OSFXSR);
    561 
    562 		/*
    563 		 * If we have SSE/SSE2, enable XMM exceptions.
    564 		 */
    565 		if (cpu_feature[0] & (CPUID_SSE|CPUID_SSE2))
    566 			lcr4(rcr4() | CR4_OSXMMEXCPT);
    567 	}
    568 
    569 #ifdef __x86_64__
    570 	/* No user PGD mapped for this CPU yet */
    571 	ci->ci_xen_current_user_pgd = 0;
    572 #endif
    573 #if defined(__x86_64__) || defined(PAE)
    574 	mutex_init(&ci->ci_kpm_mtx, MUTEX_DEFAULT, IPL_VM);
    575 #endif
    576 
    577 	atomic_or_32(&cpus_running, ci->ci_cpumask);
    578 	atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
    579 }
    580 
    581 
    582 #ifdef MULTIPROCESSOR
    583 
    584 void
    585 cpu_boot_secondary_processors(void)
    586 {
    587 	struct cpu_info *ci;
    588 	u_long i;
    589 	for (i = 0; i < maxcpus; i++) {
    590 		ci = cpu_lookup(i);
    591 		if (ci == NULL)
    592 			continue;
    593 		if (ci->ci_data.cpu_idlelwp == NULL)
    594 			continue;
    595 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    596 			continue;
    597 		if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
    598 			continue;
    599 		cpu_boot_secondary(ci);
    600 	}
    601 
    602 	x86_mp_online = true;
    603 }
    604 
    605 static void
    606 cpu_init_idle_lwp(struct cpu_info *ci)
    607 {
    608 	struct lwp *l = ci->ci_data.cpu_idlelwp;
    609 	struct pcb *pcb = lwp_getpcb(l);
    610 
    611 	pcb->pcb_cr0 = rcr0();
    612 }
    613 
    614 void
    615 cpu_init_idle_lwps(void)
    616 {
    617 	struct cpu_info *ci;
    618 	u_long i;
    619 
    620 	for (i = 0; i < maxcpus; i++) {
    621 		ci = cpu_lookup(i);
    622 		if (ci == NULL)
    623 			continue;
    624 		if (ci->ci_data.cpu_idlelwp == NULL)
    625 			continue;
    626 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    627 			continue;
    628 		cpu_init_idle_lwp(ci);
    629 	}
    630 }
    631 
    632 static void
    633 cpu_start_secondary(struct cpu_info *ci)
    634 {
    635 	int i;
    636 
    637 	aprint_debug_dev(ci->ci_dev, "starting\n");
    638 
    639 	ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
    640 
    641 	if (CPU_STARTUP(ci, (vaddr_t) cpu_hatch) != 0) {
    642 		return;
    643 	}
    644 
    645 	/*
    646 	 * wait for it to become ready
    647 	 */
    648 	for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i > 0; i--) {
    649 		delay(10);
    650 	}
    651 	if ((ci->ci_flags & CPUF_PRESENT) == 0) {
    652 		aprint_error_dev(ci->ci_dev, "failed to become ready\n");
    653 #if defined(MPDEBUG) && defined(DDB)
    654 		printf("dropping into debugger; continue from here to resume boot\n");
    655 		Debugger();
    656 #endif
    657 	}
    658 
    659 	CPU_START_CLEANUP(ci);
    660 }
    661 
    662 void
    663 cpu_boot_secondary(struct cpu_info *ci)
    664 {
    665 	int i;
    666 	atomic_or_32(&ci->ci_flags, CPUF_GO);
    667 	for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i > 0; i--) {
    668 		delay(10);
    669 	}
    670 	if ((ci->ci_flags & CPUF_RUNNING) == 0) {
    671 		aprint_error_dev(ci->ci_dev, "CPU failed to start\n");
    672 #if defined(MPDEBUG) && defined(DDB)
    673 		printf("dropping into debugger; continue from here to resume boot\n");
    674 		Debugger();
    675 #endif
    676 	}
    677 }
    678 
    679 /*
    680  * APs end up here immediately after initialisation and VCPUOP_up in
    681  * mp_cpu_start().
    682  * At this point, we are running in the idle pcb/idle stack of the new
    683  * CPU.  This function jumps to the idle loop and starts looking for
    684  * work.
    685  */
    686 extern void x86_64_tls_switch(struct lwp *);
    687 void
    688 cpu_hatch(void *v)
    689 {
    690 	struct cpu_info *ci = (struct cpu_info *)v;
    691 	struct pcb *pcb;
    692 	int s, i;
    693 
    694 	/* Setup TLS and kernel GS/FS */
    695 	cpu_init_msrs(ci, true);
    696 	cpu_init_idt();
    697 	gdt_init_cpu(ci);
    698 
    699 	cpu_probe(ci);
    700 
    701 	atomic_or_32(&ci->ci_flags, CPUF_PRESENT);
    702 
    703 	while ((ci->ci_flags & CPUF_GO) == 0) {
    704 		/* Don't use delay, boot CPU may be patching the text. */
    705 		for (i = 10000; i != 0; i--)
    706 			x86_pause();
    707 	}
    708 
    709 	/* Because the text may have been patched in x86_patch(). */
    710 	x86_flush();
    711 	tlbflushg();
    712 
    713 	KASSERT((ci->ci_flags & CPUF_RUNNING) == 0);
    714 
    715 	pcb = lwp_getpcb(curlwp);
    716 	pcb->pcb_cr3 = pmap_pdirpa(pmap_kernel(), 0);
    717 	pcb = lwp_getpcb(ci->ci_data.cpu_idlelwp);
    718 
    719 	xen_ipi_init();
    720 
    721 	xen_initclocks();
    722 
    723 #ifdef __x86_64__
    724 	fpuinit(ci);
    725 #endif
    726 
    727 	lldt(GSEL(GLDT_SEL, SEL_KPL));
    728 
    729 	cpu_init(ci);
    730 	cpu_get_tsc_freq(ci);
    731 
    732 	s = splhigh();
    733 	x86_enable_intr();
    734 	splx(s);
    735 
    736 	aprint_debug_dev(ci->ci_dev, "running\n");
    737 
    738 	cpu_switchto(NULL, ci->ci_data.cpu_idlelwp, true);
    739 
    740 	panic("switch to idle_loop context returned!\n");
    741 	/* NOTREACHED */
    742 }
    743 
    744 #if defined(DDB)
    745 
    746 #include <ddb/db_output.h>
    747 #include <machine/db_machdep.h>
    748 
    749 /*
    750  * Dump CPU information from ddb.
    751  */
    752 void
    753 cpu_debug_dump(void)
    754 {
    755 	struct cpu_info *ci;
    756 	CPU_INFO_ITERATOR cii;
    757 
    758 	db_printf("addr		dev	id	flags	ipis	curlwp 		fpcurlwp\n");
    759 	for (CPU_INFO_FOREACH(cii, ci)) {
    760 		db_printf("%p	%s	%ld	%x	%x	%10p	%10p\n",
    761 		    ci,
    762 		    ci->ci_dev == NULL ? "BOOT" : device_xname(ci->ci_dev),
    763 		    (long)ci->ci_cpuid,
    764 		    ci->ci_flags, ci->ci_ipis,
    765 		    ci->ci_curlwp,
    766 		    ci->ci_fpcurlwp);
    767 	}
    768 }
    769 #endif /* DDB */
    770 
    771 #endif /* MULTIPROCESSOR */
    772 
    773 extern void hypervisor_callback(void);
    774 extern void failsafe_callback(void);
    775 #ifdef __x86_64__
    776 typedef void (vector)(void);
    777 extern vector Xsyscall, Xsyscall32;
    778 #endif
    779 
    780 /*
    781  * Setup the "trampoline". On Xen, we setup nearly all cpu context
    782  * outside a trampoline, so we prototype and call targetip like so:
    783  * void targetip(struct cpu_info *);
    784  */
    785 
    786 static void
    787 gdt_prepframes(paddr_t *frames, vaddr_t base, uint32_t entries)
    788 {
    789 	int i;
    790 	for (i = 0; i < roundup(entries, PAGE_SIZE) >> PAGE_SHIFT; i++) {
    791 
    792 		frames[i] = ((paddr_t) xpmap_ptetomach(
    793 				(pt_entry_t *) (base + (i << PAGE_SHIFT))))
    794 			>> PAGE_SHIFT;
    795 
    796 		/* Mark Read-only */
    797 		pmap_pte_clearbits(kvtopte(base + (i << PAGE_SHIFT)),
    798 		    PG_RW);
    799 	}
    800 }
    801 
    802 #ifdef __x86_64__
    803 extern char *ldtstore;
    804 
    805 static void
    806 xen_init_amd64_vcpuctxt(struct cpu_info *ci,
    807 			struct vcpu_guest_context *initctx,
    808 			void targetrip(struct cpu_info *))
    809 {
    810 	/* page frames to point at GDT */
    811 	extern int gdt_size;
    812 	paddr_t frames[16];
    813 	psize_t gdt_ents;
    814 
    815 	struct lwp *l;
    816 	struct pcb *pcb;
    817 
    818 	volatile struct vcpu_info *vci;
    819 
    820 	KASSERT(ci != NULL);
    821 	KASSERT(ci != &cpu_info_primary);
    822 	KASSERT(initctx != NULL);
    823 	KASSERT(targetrip != NULL);
    824 
    825 	memset(initctx, 0, sizeof *initctx);
    826 
    827 	gdt_ents = roundup(gdt_size, PAGE_SIZE) >> PAGE_SHIFT;
    828 	KASSERT(gdt_ents <= 16);
    829 
    830 	gdt_prepframes(frames, (vaddr_t) ci->ci_gdt, gdt_ents);
    831 
    832 	/* Initialise the vcpu context: We use idle_loop()'s pcb context. */
    833 
    834 	l = ci->ci_data.cpu_idlelwp;
    835 
    836 	KASSERT(l != NULL);
    837 	pcb = lwp_getpcb(l);
    838 	KASSERT(pcb != NULL);
    839 
    840 	/* resume with interrupts off */
    841 	vci = ci->ci_vcpu;
    842 	vci->evtchn_upcall_mask = 1;
    843 	xen_mb();
    844 
    845 	/* resume in kernel-mode */
    846 	initctx->flags = VGCF_in_kernel | VGCF_online;
    847 
    848 	/* Stack and entry points:
    849 	 * We arrange for the stack frame for cpu_hatch() to
    850 	 * appear as a callee frame of lwp_trampoline(). Being a
    851 	 * leaf frame prevents trampling on any of the MD stack setup
    852 	 * that x86/vm_machdep.c:cpu_lwp_fork() does for idle_loop()
    853 	 */
    854 
    855 	initctx->user_regs.rdi = (uint64_t) ci; /* targetrip(ci); */
    856 	initctx->user_regs.rip = (vaddr_t) targetrip;
    857 
    858 	initctx->user_regs.cs = GSEL(GCODE_SEL, SEL_KPL);
    859 
    860 	initctx->user_regs.rflags = pcb->pcb_flags;
    861 	initctx->user_regs.rsp = pcb->pcb_rsp;
    862 
    863 	/* Data segments */
    864 	initctx->user_regs.ss = GSEL(GDATA_SEL, SEL_KPL);
    865 	initctx->user_regs.es = GSEL(GDATA_SEL, SEL_KPL);
    866 	initctx->user_regs.ds = GSEL(GDATA_SEL, SEL_KPL);
    867 
    868 	/* GDT */
    869 	memcpy(initctx->gdt_frames, frames, sizeof frames);
    870 	initctx->gdt_ents = gdt_ents;
    871 
    872 	/* LDT */
    873 	initctx->ldt_base = (unsigned long) ldtstore;
    874 	initctx->ldt_ents = LDT_SIZE >> 3;
    875 
    876 	/* Kernel context state */
    877 	initctx->kernel_ss = GSEL(GDATA_SEL, SEL_KPL);
    878 	initctx->kernel_sp = pcb->pcb_rsp0;
    879 	initctx->ctrlreg[0] = pcb->pcb_cr0;
    880 	initctx->ctrlreg[1] = 0; /* "resuming" from kernel - no User cr3. */
    881 	initctx->ctrlreg[2] = (vaddr_t) targetrip;
    882 	/*
    883 	 * Use pmap_kernel() L4 PD directly, until we setup the
    884 	 * per-cpu L4 PD in pmap_cpu_init_late()
    885 	 */
    886 	initctx->ctrlreg[3] = xen_pfn_to_cr3(x86_btop(xpmap_ptom(ci->ci_kpm_pdirpa)));
    887 	initctx->ctrlreg[4] = CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT;
    888 
    889 
    890 	/* Xen callbacks */
    891 	initctx->event_callback_eip = (unsigned long) hypervisor_callback;
    892 	initctx->failsafe_callback_eip = (unsigned long) failsafe_callback;
    893 	initctx->syscall_callback_eip = (unsigned long) Xsyscall;
    894 
    895 	return;
    896 }
    897 #else /* i386 */
    898 extern union descriptor *ldt;
    899 extern void Xsyscall(void);
    900 
    901 static void
    902 xen_init_i386_vcpuctxt(struct cpu_info *ci,
    903 			struct vcpu_guest_context *initctx,
    904 			void targeteip(struct cpu_info *))
    905 {
    906 	/* page frames to point at GDT */
    907 	extern int gdt_size;
    908 	paddr_t frames[16];
    909 	psize_t gdt_ents;
    910 
    911 	struct lwp *l;
    912 	struct pcb *pcb;
    913 
    914 	volatile struct vcpu_info *vci;
    915 
    916 	KASSERT(ci != NULL);
    917 	KASSERT(ci != &cpu_info_primary);
    918 	KASSERT(initctx != NULL);
    919 	KASSERT(targeteip != NULL);
    920 
    921 	memset(initctx, 0, sizeof *initctx);
    922 
    923 	gdt_ents = roundup(gdt_size, PAGE_SIZE) >> PAGE_SHIFT;
    924 	KASSERT(gdt_ents <= 16);
    925 
    926 	gdt_prepframes(frames, (vaddr_t) ci->ci_gdt, gdt_ents);
    927 
    928 	/*
    929 	 * Initialise the vcpu context:
    930 	 * We use this cpu's idle_loop() pcb context.
    931 	 */
    932 
    933 	l = ci->ci_data.cpu_idlelwp;
    934 
    935 	KASSERT(l != NULL);
    936 	pcb = lwp_getpcb(l);
    937 	KASSERT(pcb != NULL);
    938 
    939 	/* resume with interrupts off */
    940 	vci = ci->ci_vcpu;
    941 	vci->evtchn_upcall_mask = 1;
    942 	xen_mb();
    943 
    944 	/* resume in kernel-mode */
    945 	initctx->flags = VGCF_in_kernel | VGCF_online;
    946 
    947 	/* Stack frame setup for cpu_hatch():
    948 	 * We arrange for the stack frame for cpu_hatch() to
    949 	 * appear as a callee frame of lwp_trampoline(). Being a
    950 	 * leaf frame prevents trampling on any of the MD stack setup
    951 	 * that x86/vm_machdep.c:cpu_lwp_fork() does for idle_loop()
    952 	 */
    953 
    954 	initctx->user_regs.esp = pcb->pcb_esp - 4; /* Leave word for
    955 						      arg1 */
    956 	{ /* targeteip(ci); */
    957 		uint32_t *arg = (uint32_t *) initctx->user_regs.esp;
    958 		arg[1] = (uint32_t) ci; /* arg1 */
    959 
    960 	}
    961 
    962 	initctx->user_regs.eip = (vaddr_t) targeteip;
    963 	initctx->user_regs.cs = GSEL(GCODE_SEL, SEL_KPL);
    964 	initctx->user_regs.eflags |= pcb->pcb_iopl;
    965 
    966 	/* Data segments */
    967 	initctx->user_regs.ss = GSEL(GDATA_SEL, SEL_KPL);
    968 	initctx->user_regs.es = GSEL(GDATA_SEL, SEL_KPL);
    969 	initctx->user_regs.ds = GSEL(GDATA_SEL, SEL_KPL);
    970 	initctx->user_regs.fs = GSEL(GDATA_SEL, SEL_KPL);
    971 
    972 	/* GDT */
    973 	memcpy(initctx->gdt_frames, frames, sizeof frames);
    974 	initctx->gdt_ents = gdt_ents;
    975 
    976 	/* LDT */
    977 	initctx->ldt_base = (unsigned long) ldt;
    978 	initctx->ldt_ents = NLDT;
    979 
    980 	/* Kernel context state */
    981 	initctx->kernel_ss = GSEL(GDATA_SEL, SEL_KPL);
    982 	initctx->kernel_sp = pcb->pcb_esp0;
    983 	initctx->ctrlreg[0] = pcb->pcb_cr0;
    984 	initctx->ctrlreg[1] = 0; /* "resuming" from kernel - no User cr3. */
    985 	initctx->ctrlreg[2] = (vaddr_t) targeteip;
    986 #ifdef PAE
    987 	initctx->ctrlreg[3] = xen_pfn_to_cr3(x86_btop(xpmap_ptom(ci->ci_pae_l3_pdirpa)));
    988 #else /* PAE */
    989 	initctx->ctrlreg[3] = xen_pfn_to_cr3(x86_btop(xpmap_ptom(pcb->pcb_cr3)));
    990 #endif /* PAE */
    991 	initctx->ctrlreg[4] = /* CR4_PAE |  */CR4_OSFXSR | CR4_OSXMMEXCPT;
    992 
    993 
    994 	/* Xen callbacks */
    995 	initctx->event_callback_eip = (unsigned long) hypervisor_callback;
    996 	initctx->event_callback_cs = GSEL(GCODE_SEL, SEL_KPL);
    997 	initctx->failsafe_callback_eip = (unsigned long) failsafe_callback;
    998 	initctx->failsafe_callback_cs = GSEL(GCODE_SEL, SEL_KPL);
    999 
   1000 	return;
   1001 }
   1002 #endif /* __x86_64__ */
   1003 
   1004 int
   1005 mp_cpu_start(struct cpu_info *ci, vaddr_t target)
   1006 {
   1007 
   1008 	int hyperror;
   1009 	struct vcpu_guest_context vcpuctx;
   1010 
   1011 	KASSERT(ci != NULL);
   1012 	KASSERT(ci != &cpu_info_primary);
   1013 	KASSERT(ci->ci_flags & CPUF_AP);
   1014 
   1015 #ifdef __x86_64__
   1016 	xen_init_amd64_vcpuctxt(ci, &vcpuctx, (void (*)(struct cpu_info *))target);
   1017 #else  /* i386 */
   1018 	xen_init_i386_vcpuctxt(ci, &vcpuctx, (void (*)(struct cpu_info *))target);
   1019 #endif /* __x86_64__ */
   1020 
   1021 	/* Initialise the given vcpu to execute cpu_hatch(ci); */
   1022 	if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_initialise, ci->ci_cpuid, &vcpuctx))) {
   1023 		aprint_error(": context initialisation failed. errno = %d\n", hyperror);
   1024 		return hyperror;
   1025 	}
   1026 
   1027 	/* Start it up */
   1028 
   1029 	/* First bring it down */
   1030 	if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL))) {
   1031 		aprint_error(": VCPUOP_down hypervisor command failed. errno = %d\n", hyperror);
   1032 		return hyperror;
   1033 	}
   1034 
   1035 	if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_up, ci->ci_cpuid, NULL))) {
   1036 		aprint_error(": VCPUOP_up hypervisor command failed. errno = %d\n", hyperror);
   1037 		return hyperror;
   1038 	}
   1039 
   1040 	if (!vcpu_is_up(ci)) {
   1041 		aprint_error(": did not come up\n");
   1042 		return -1;
   1043 	}
   1044 
   1045 	return 0;
   1046 }
   1047 
   1048 void
   1049 mp_cpu_start_cleanup(struct cpu_info *ci)
   1050 {
   1051 	if (vcpu_is_up(ci)) {
   1052 		aprint_debug_dev(ci->ci_dev, "is started.\n");
   1053 	}
   1054 	else {
   1055 		aprint_error_dev(ci->ci_dev, "did not start up.\n");
   1056 	}
   1057 
   1058 }
   1059 
   1060 /* curcpu() uses %fs - shim for until cpu_init_msrs(), below */
   1061 static struct cpu_info *cpu_primary(void)
   1062 {
   1063 	return &cpu_info_primary;
   1064 }
   1065 /* XXX: rename to something more generic. users other than xpq exist */
   1066 struct cpu_info	* (*xpq_cpu)(void) = cpu_primary;
   1067 
   1068 void
   1069 cpu_init_msrs(struct cpu_info *ci, bool full)
   1070 {
   1071 #ifdef __x86_64__
   1072 	if (full) {
   1073 		HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
   1074 		HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (uint64_t) ci);
   1075 		HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
   1076 		xpq_cpu = x86_curcpu;
   1077 	}
   1078 #endif	/* __x86_64__ */
   1079 
   1080 	if (cpu_feature[2] & CPUID_NOX)
   1081 		wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE);
   1082 
   1083 }
   1084 
   1085 void
   1086 cpu_offline_md(void)
   1087 {
   1088         int s;
   1089 
   1090         s = splhigh();
   1091 #ifdef __i386__
   1092         npxsave_cpu(true);
   1093 #else
   1094         fpusave_cpu(true);
   1095 #endif
   1096         splx(s);
   1097 }
   1098 
   1099 void
   1100 cpu_get_tsc_freq(struct cpu_info *ci)
   1101 {
   1102 	uint32_t vcpu_tversion;
   1103 	const volatile vcpu_time_info_t *tinfo = &ci->ci_vcpu->time;
   1104 
   1105 	vcpu_tversion = tinfo->version;
   1106 	while (tinfo->version == vcpu_tversion); /* Wait for a time update. XXX: timeout ? */
   1107 
   1108 	uint64_t freq = 1000000000ULL << 32;
   1109 	freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
   1110 	if ( tinfo->tsc_shift < 0 )
   1111 		freq = freq << -tinfo->tsc_shift;
   1112 	else
   1113 		freq = freq >> tinfo->tsc_shift;
   1114 	ci->ci_data.cpu_cc_freq = freq;
   1115 }
   1116 
   1117 void
   1118 x86_cpu_idle_xen(void)
   1119 {
   1120 	struct cpu_info *ci = curcpu();
   1121 
   1122 	KASSERT(ci->ci_ilevel == IPL_NONE);
   1123 
   1124 	x86_disable_intr();
   1125 	if (!__predict_false(ci->ci_want_resched)) {
   1126 		idle_block();
   1127 	} else {
   1128 		x86_enable_intr();
   1129 	}
   1130 }
   1131 
   1132 /*
   1133  * Loads pmap for the current CPU.
   1134  */
   1135 void
   1136 cpu_load_pmap(struct pmap *pmap, struct pmap *oldpmap)
   1137 {
   1138 	KASSERT(pmap != pmap_kernel());
   1139 
   1140 #if defined(__x86_64__) || defined(PAE)
   1141 	struct cpu_info *ci = curcpu();
   1142 	uint32_t cpumask = ci->ci_cpumask;
   1143 
   1144 	mutex_enter(&ci->ci_kpm_mtx);
   1145 	/* make new pmap visible to pmap_kpm_sync_xcall() */
   1146 	atomic_or_32(&pmap->pm_xen_ptp_cpus, cpumask);
   1147 #endif
   1148 #ifdef i386
   1149 #ifdef PAE
   1150 	{
   1151 		int i;
   1152 		paddr_t l3_pd = xpmap_ptom_masked(ci->ci_pae_l3_pdirpa);
   1153 		/* don't update the kernel L3 slot */
   1154 		for (i = 0 ; i < PDP_SIZE - 1; i++) {
   1155 			xpq_queue_pte_update(l3_pd + i * sizeof(pd_entry_t),
   1156 			    xpmap_ptom(pmap->pm_pdirpa[i]) | PG_V);
   1157 		}
   1158 		tlbflush();
   1159 	}
   1160 #else /* PAE */
   1161 	lcr3(pmap_pdirpa(pmap, 0));
   1162 #endif /* PAE */
   1163 #endif /* i386 */
   1164 
   1165 #ifdef __x86_64__
   1166 	{
   1167 		int i;
   1168 		pd_entry_t *new_pgd;
   1169 		paddr_t l4_pd_ma;
   1170 
   1171 		l4_pd_ma = xpmap_ptom_masked(ci->ci_kpm_pdirpa);
   1172 
   1173 		/*
   1174 		 * Map user space address in kernel space and load
   1175 		 * user cr3
   1176 		 */
   1177 		new_pgd = pmap->pm_pdir;
   1178 		KASSERT(pmap == ci->ci_pmap);
   1179 
   1180 		/* Copy user pmap L4 PDEs (in user addr. range) to per-cpu L4 */
   1181 		for (i = 0; i < PDIR_SLOT_PTE; i++) {
   1182 			KASSERT(pmap != pmap_kernel() || new_pgd[i] == 0);
   1183 			if (ci->ci_kpm_pdir[i] != new_pgd[i]) {
   1184 				xpq_queue_pte_update(
   1185 				   l4_pd_ma + i * sizeof(pd_entry_t),
   1186 				    new_pgd[i]);
   1187 			}
   1188 		}
   1189 
   1190 		xen_set_user_pgd(pmap_pdirpa(pmap, 0));
   1191 		ci->ci_xen_current_user_pgd = pmap_pdirpa(pmap, 0);
   1192 
   1193 		tlbflush();
   1194 	}
   1195 
   1196 #endif /* __x86_64__ */
   1197 #if defined(__x86_64__) || defined(PAE)
   1198 	/* old pmap no longer visible to pmap_kpm_sync_xcall() */
   1199 	atomic_and_32(&oldpmap->pm_xen_ptp_cpus, ~cpumask);
   1200 	mutex_exit(&ci->ci_kpm_mtx);
   1201 #endif
   1202 }
   1203 
   1204  /*
   1205   * pmap_cpu_init_late: perform late per-CPU initialization.
   1206   * Short note about percpu PDIR pages:
   1207   * Both the PAE and __x86_64__ architectures have per-cpu PDIR
   1208   * tables. This is to get around Xen's pagetable setup constraints for
   1209   * PAE (multiple L3[3]s cannot point to the same L2 - Xen
   1210   * will refuse to pin a table setup this way.) and for multiple cpus
   1211   * to map in different user pmaps on __x86_64__ (see: cpu_load_pmap())
   1212   *
   1213   * What this means for us is that the PDIR of the pmap_kernel() is
   1214   * considered to be a canonical "SHADOW" PDIR with the following
   1215   * properties:
   1216   * - Its recursive mapping points to itself
   1217   * - per-cpu recurseive mappings point to themselves on __x86_64__
   1218   * - per-cpu L4 pages' kernel entries are expected to be in sync with
   1219   *   the shadow
   1220   */
   1221 
   1222 void
   1223 pmap_cpu_init_late(struct cpu_info *ci)
   1224 {
   1225 #if defined(PAE) || defined(__x86_64__)
   1226 	/*
   1227 	 * The BP has already its own PD page allocated during early
   1228 	 * MD startup.
   1229 	 */
   1230 
   1231 #if defined(__x86_64__)
   1232 	/* Setup per-cpu normal_pdes */
   1233 	int i;
   1234 	extern pd_entry_t * const normal_pdes[];
   1235 	for (i = 0;i < PTP_LEVELS - 1;i++) {
   1236 		ci->ci_normal_pdes[i] = normal_pdes[i];
   1237 	}
   1238 #endif /* __x86_64__ */
   1239 
   1240 	if (ci == &cpu_info_primary)
   1241 		return;
   1242 
   1243 	KASSERT(ci != NULL);
   1244 
   1245 #if defined(PAE)
   1246 	cpu_alloc_l3_page(ci);
   1247 	KASSERT(ci->ci_pae_l3_pdirpa != 0);
   1248 
   1249 	/* Initialise L2 entries 0 - 2: Point them to pmap_kernel() */
   1250 	int i;
   1251 	for (i = 0 ; i < PDP_SIZE - 1; i++) {
   1252 		ci->ci_pae_l3_pdir[i] =
   1253 		    xpmap_ptom_masked(pmap_kernel()->pm_pdirpa[i]) | PG_V;
   1254 	}
   1255 #endif /* PAE */
   1256 
   1257 	ci->ci_kpm_pdir = (pd_entry_t *)uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
   1258 	    UVM_KMF_WIRED | UVM_KMF_ZERO | UVM_KMF_NOWAIT);
   1259 
   1260 	if (ci->ci_kpm_pdir == NULL) {
   1261 		panic("%s: failed to allocate L4 per-cpu PD for CPU %d\n",
   1262 		      __func__, cpu_index(ci));
   1263 	}
   1264 	ci->ci_kpm_pdirpa = vtophys((vaddr_t) ci->ci_kpm_pdir);
   1265 	KASSERT(ci->ci_kpm_pdirpa != 0);
   1266 
   1267 #if defined(__x86_64__)
   1268 	/*
   1269 	 * Copy over the pmap_kernel() shadow L4 entries
   1270 	 */
   1271 
   1272 	memcpy(ci->ci_kpm_pdir, pmap_kernel()->pm_pdir, PAGE_SIZE);
   1273 
   1274 	/* Recursive kernel mapping */
   1275 	ci->ci_kpm_pdir[PDIR_SLOT_PTE] = xpmap_ptom_masked(ci->ci_kpm_pdirpa) | PG_k | PG_V;
   1276 #elif defined(PAE)
   1277 	/* Copy over the pmap_kernel() shadow L2 entries that map the kernel */
   1278 	memcpy(ci->ci_kpm_pdir, pmap_kernel()->pm_pdir + PDIR_SLOT_KERN, nkptp[PTP_LEVELS - 1] * sizeof(pd_entry_t));
   1279 #endif /* __x86_64__ else PAE */
   1280 
   1281 	/* Xen wants R/O */
   1282 	pmap_protect(pmap_kernel(), (vaddr_t)ci->ci_kpm_pdir,
   1283 	    (vaddr_t)ci->ci_kpm_pdir + PAGE_SIZE, VM_PROT_READ);
   1284 	pmap_update(pmap_kernel());
   1285 #if defined(PAE)
   1286 	/* Initialise L3 entry 3. This mapping is shared across all
   1287 	 * pmaps and is static, ie; loading a new pmap will not update
   1288 	 * this entry.
   1289 	 */
   1290 
   1291 	ci->ci_pae_l3_pdir[3] = xpmap_ptom_masked(ci->ci_kpm_pdirpa) | PG_k | PG_V;
   1292 
   1293 	/* Mark L3 R/O (Xen wants this) */
   1294 	pmap_protect(pmap_kernel(), (vaddr_t)ci->ci_pae_l3_pdir,
   1295 	    (vaddr_t)ci->ci_pae_l3_pdir + PAGE_SIZE, VM_PROT_READ);
   1296 	pmap_update(pmap_kernel());
   1297 
   1298 	xpq_queue_pin_l3_table(xpmap_ptom_masked(ci->ci_pae_l3_pdirpa));
   1299 
   1300 #elif defined(__x86_64__)
   1301 	xpq_queue_pin_l4_table(xpmap_ptom_masked(ci->ci_kpm_pdirpa));
   1302 #endif /* PAE , __x86_64__ */
   1303 #endif /* defined(PAE) || defined(__x86_64__) */
   1304 }
   1305 
   1306 /*
   1307  * Notify all other cpus to halt.
   1308  */
   1309 
   1310 void
   1311 cpu_broadcast_halt(void)
   1312 {
   1313 	xen_broadcast_ipi(XEN_IPI_HALT);
   1314 }
   1315 
   1316 /*
   1317  * Send a dummy ipi to a cpu.
   1318  */
   1319 
   1320 void
   1321 cpu_kick(struct cpu_info *ci)
   1322 {
   1323 	(void)xen_send_ipi(ci, XEN_IPI_KICK);
   1324 }
   1325