Home | History | Annotate | Line # | Download | only in x86
cpu.c revision 1.56.2.2
      1 /*	$NetBSD: cpu.c,v 1.56.2.2 2011/06/23 14:19:50 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.56.2.2 2011/06/23 14:19:50 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/atomic.h>
     87 #include <sys/reboot.h>
     88 #include <sys/idle.h>
     89 
     90 #include <uvm/uvm.h>
     91 
     92 #include <machine/cpufunc.h>
     93 #include <machine/cpuvar.h>
     94 #include <machine/pmap.h>
     95 #include <machine/vmparam.h>
     96 #include <machine/mpbiosvar.h>
     97 #include <machine/pcb.h>
     98 #include <machine/specialreg.h>
     99 #include <machine/segments.h>
    100 #include <machine/gdt.h>
    101 #include <machine/mtrr.h>
    102 #include <machine/pio.h>
    103 #include <machine/fpu.h>
    104 
    105 #include <xen/xen.h>
    106 #include <xen/xen3-public/vcpu.h>
    107 #include <xen/vcpuvar.h>
    108 
    109 #if NLAPIC > 0
    110 #include <machine/apicvar.h>
    111 #include <machine/i82489reg.h>
    112 #include <machine/i82489var.h>
    113 #endif
    114 
    115 #include <dev/ic/mc146818reg.h>
    116 #include <dev/isa/isareg.h>
    117 
    118 #if MAXCPUS > 32
    119 #error cpu_info contains 32bit bitmasks
    120 #endif
    121 
    122 static int	cpu_match(device_t, cfdata_t, void *);
    123 static void	cpu_attach(device_t, device_t, void *);
    124 static void	cpu_defer(device_t);
    125 static int	cpu_rescan(device_t, const char *, const int *);
    126 static void	cpu_childdetached(device_t, device_t);
    127 static int	vcpu_match(device_t, cfdata_t, void *);
    128 static void	vcpu_attach(device_t, device_t, void *);
    129 static void	cpu_attach_common(device_t, device_t, void *);
    130 void		cpu_offline_md(void);
    131 
    132 struct cpu_softc {
    133 	device_t sc_dev;		/* device tree glue */
    134 	struct cpu_info *sc_info;	/* pointer to CPU info */
    135 	bool sc_wasonline;
    136 };
    137 
    138 int mp_cpu_start(struct cpu_info *, paddr_t);
    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_DECL2_NEW(cpu, sizeof(struct cpu_softc),
    144     cpu_match, cpu_attach, NULL, NULL, cpu_rescan, cpu_childdetached);
    145 
    146 CFATTACH_DECL_NEW(vcpu, sizeof(struct cpu_softc),
    147     vcpu_match, vcpu_attach, NULL, NULL);
    148 
    149 /*
    150  * Statically-allocated CPU info for the primary CPU (or the only
    151  * CPU, on uniprocessors).  The CPU info list is initialized to
    152  * point at it.
    153  */
    154 #ifdef TRAPLOG
    155 #include <machine/tlog.h>
    156 struct tlog tlog_primary;
    157 #endif
    158 struct cpu_info cpu_info_primary __aligned(CACHE_LINE_SIZE) = {
    159 	.ci_dev = 0,
    160 	.ci_self = &cpu_info_primary,
    161 	.ci_idepth = -1,
    162 	.ci_curlwp = &lwp0,
    163 	.ci_curldt = -1,
    164 	.ci_cpumask = 1,
    165 #ifdef TRAPLOG
    166 	.ci_tlog = &tlog_primary,
    167 #endif
    168 
    169 };
    170 struct cpu_info phycpu_info_primary __aligned(CACHE_LINE_SIZE) = {
    171 	.ci_dev = 0,
    172 	.ci_self = &phycpu_info_primary,
    173 };
    174 
    175 struct cpu_info *cpu_info_list = &cpu_info_primary;
    176 struct cpu_info *phycpu_info_list = &phycpu_info_primary;
    177 
    178 static void	cpu_set_tss_gates(struct cpu_info *ci);
    179 
    180 uint32_t cpus_attached = 1;
    181 uint32_t cpus_running = 0;
    182 
    183 uint32_t phycpus_attached = 0;
    184 uint32_t phycpus_running = 0;
    185 
    186 uint32_t cpu_feature[5]; /* X86 CPUID feature bits
    187 			  *	[0] basic features %edx
    188 			  *	[1] basic features %ecx
    189 			  *	[2] extended features %edx
    190 			  *	[3] extended features %ecx
    191 			  *	[4] VIA padlock features
    192 			  */
    193 
    194 bool x86_mp_online;
    195 paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
    196 
    197 #if defined(MULTIPROCESSOR)
    198 void    	cpu_hatch(void *);
    199 static void    	cpu_boot_secondary(struct cpu_info *ci);
    200 static void    	cpu_start_secondary(struct cpu_info *ci);
    201 #endif	/* MULTIPROCESSOR */
    202 
    203 static int
    204 cpu_match(device_t parent, cfdata_t match, void *aux)
    205 {
    206 
    207 	return 1;
    208 }
    209 
    210 static void
    211 cpu_attach(device_t parent, device_t self, void *aux)
    212 {
    213 	struct cpu_softc *sc = device_private(self);
    214 	struct cpu_attach_args *caa = aux;
    215 	struct cpu_info *ci;
    216 	uintptr_t ptr;
    217 	static int nphycpu = 0;
    218 
    219 	sc->sc_dev = self;
    220 
    221 	if (phycpus_attached == ~0) {
    222 		aprint_error(": increase MAXCPUS\n");
    223 		return;
    224 	}
    225 
    226 	/*
    227 	 * If we're an Application Processor, allocate a cpu_info
    228 	 * If we're the first attached CPU use the primary cpu_info,
    229 	 * otherwise allocate a new one
    230 	 */
    231 	aprint_naive("\n");
    232 	aprint_normal("\n");
    233 	if (nphycpu > 0) {
    234 		struct cpu_info *tmp;
    235 		ptr = (uintptr_t)kmem_zalloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
    236 		    KM_SLEEP);
    237 		ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
    238 		ci->ci_curldt = -1;
    239 
    240 		tmp = phycpu_info_list;
    241 		while (tmp->ci_next)
    242 			tmp = tmp->ci_next;
    243 
    244 		tmp->ci_next = ci;
    245 	} else {
    246 		ci = &phycpu_info_primary;
    247 	}
    248 
    249 	ci->ci_self = ci;
    250 	sc->sc_info = ci;
    251 
    252 	ci->ci_dev = self;
    253 	ci->ci_acpiid = caa->cpu_id;
    254 	ci->ci_cpuid = caa->cpu_number;
    255 	ci->ci_vcpu = NULL;
    256 	ci->ci_index = nphycpu++;
    257 	ci->ci_cpumask = (1 << cpu_index(ci));
    258 
    259 	atomic_or_32(&phycpus_attached, ci->ci_cpumask);
    260 
    261 	if (!pmf_device_register(self, NULL, NULL))
    262 		aprint_error_dev(self, "couldn't establish power handler\n");
    263 
    264 	(void)config_defer(self, cpu_defer);
    265 }
    266 
    267 static void
    268 cpu_defer(device_t self)
    269 {
    270 	cpu_rescan(self, NULL, NULL);
    271 }
    272 
    273 static int
    274 cpu_rescan(device_t self, const char *ifattr, const int *locators)
    275 {
    276 	struct cpu_softc *sc = device_private(self);
    277 	struct cpufeature_attach_args cfaa;
    278 	struct cpu_info *ci = sc->sc_info;
    279 
    280 	memset(&cfaa, 0, sizeof(cfaa));
    281 	cfaa.ci = ci;
    282 
    283 	if (ifattr_match(ifattr, "cpufeaturebus")) {
    284 
    285 		if (ci->ci_frequency == NULL) {
    286 			cfaa.name = "frequency";
    287 			ci->ci_frequency = config_found_ia(self,
    288 			    "cpufeaturebus", &cfaa, NULL);
    289 		}
    290 	}
    291 
    292 	return 0;
    293 }
    294 
    295 static void
    296 cpu_childdetached(device_t self, device_t child)
    297 {
    298 	struct cpu_softc *sc = device_private(self);
    299 	struct cpu_info *ci = sc->sc_info;
    300 
    301 	if (ci->ci_frequency == child)
    302 		ci->ci_frequency = NULL;
    303 }
    304 
    305 static int
    306 vcpu_match(device_t parent, cfdata_t match, void *aux)
    307 {
    308 	struct vcpu_attach_args *vcaa = aux;
    309 	struct vcpu_runstate_info vcr;
    310 	int error;
    311 
    312 	if (strcmp(vcaa->vcaa_name, match->cf_name) == 0) {
    313 		error = HYPERVISOR_vcpu_op(VCPUOP_get_runstate_info,
    314 					   vcaa->vcaa_caa.cpu_number,
    315 					   &vcr);
    316 		switch (error) {
    317 		case 0:
    318 			return 1;
    319 		case -ENOENT:
    320 			return 0;
    321 		default:
    322 			panic("Unknown hypervisor error %d returned on vcpu runstate probe\n", error);
    323 		}
    324 	}
    325 
    326 	return 0;
    327 }
    328 
    329 static void
    330 vcpu_attach(device_t parent, device_t self, void *aux)
    331 {
    332 	struct vcpu_attach_args *vcaa = aux;
    333 
    334 	KASSERT(vcaa->vcaa_caa.cpu_func == NULL);
    335 	vcaa->vcaa_caa.cpu_func = &mp_cpu_funcs;
    336 	cpu_attach_common(parent, self, &vcaa->vcaa_caa);
    337 }
    338 
    339 static int
    340 vcpu_is_up(struct cpu_info *ci)
    341 {
    342 	KASSERT(ci != NULL);
    343 	return HYPERVISOR_vcpu_op(VCPUOP_is_up, ci->ci_cpuid, NULL);
    344 }
    345 
    346 static void
    347 cpu_vm_init(struct cpu_info *ci)
    348 {
    349 	int ncolors = 2, i;
    350 
    351 	for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
    352 		struct x86_cache_info *cai;
    353 		int tcolors;
    354 
    355 		cai = &ci->ci_cinfo[i];
    356 
    357 		tcolors = atop(cai->cai_totalsize);
    358 		switch(cai->cai_associativity) {
    359 		case 0xff:
    360 			tcolors = 1; /* fully associative */
    361 			break;
    362 		case 0:
    363 		case 1:
    364 			break;
    365 		default:
    366 			tcolors /= cai->cai_associativity;
    367 		}
    368 		ncolors = max(ncolors, tcolors);
    369 	}
    370 
    371 	/*
    372 	 * Knowing the size of the largest cache on this CPU, re-color
    373 	 * our pages.
    374 	 */
    375 	if (ncolors <= uvmexp.ncolors)
    376 		return;
    377 	aprint_debug_dev(ci->ci_dev, "%d page colors\n", ncolors);
    378 	uvm_page_recolor(ncolors);
    379 }
    380 
    381 static void
    382 cpu_attach_common(device_t parent, device_t self, void *aux)
    383 {
    384 	struct cpu_softc *sc = device_private(self);
    385 	struct cpu_attach_args *caa = aux;
    386 	struct cpu_info *ci;
    387 	uintptr_t ptr;
    388 	int cpunum = caa->cpu_number;
    389 	static bool again = false;
    390 
    391 	sc->sc_dev = self;
    392 
    393 	/*
    394 	 * If we're an Application Processor, allocate a cpu_info
    395 	 * structure, otherwise use the primary's.
    396 	 */
    397 	if (caa->cpu_role == CPU_ROLE_AP) {
    398 		aprint_naive(": Application Processor\n");
    399 		ptr = (uintptr_t)kmem_alloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
    400 		    KM_SLEEP);
    401 		ci = (struct cpu_info *)roundup2(ptr, CACHE_LINE_SIZE);
    402 		memset(ci, 0, sizeof(*ci));
    403 #ifdef TRAPLOG
    404 		ci->ci_tlog_base = kmem_zalloc(sizeof(struct tlog), KM_SLEEP);
    405 #endif
    406 	} else {
    407 		aprint_naive(": %s Processor\n",
    408 		    caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
    409 		ci = &cpu_info_primary;
    410 	}
    411 
    412 	ci->ci_self = ci;
    413 	sc->sc_info = ci;
    414 	ci->ci_dev = self;
    415 	ci->ci_cpuid = cpunum;
    416 
    417 	KASSERT(HYPERVISOR_shared_info != NULL);
    418 	ci->ci_vcpu = &HYPERVISOR_shared_info->vcpu_info[cpunum];
    419 
    420 	KASSERT(ci->ci_func == 0);
    421 	ci->ci_func = caa->cpu_func;
    422 
    423 	/* Must be called before mi_cpu_attach(). */
    424 	cpu_vm_init(ci);
    425 
    426 	if (caa->cpu_role == CPU_ROLE_AP) {
    427 		int error;
    428 
    429 		error = mi_cpu_attach(ci);
    430 
    431 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
    432 		if (error != 0) {
    433 			aprint_normal("\n");
    434 			aprint_error_dev(self,
    435 			    "mi_cpu_attach failed with %d\n", error);
    436 			return;
    437 		}
    438 
    439 	} else {
    440 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
    441 	}
    442 
    443 	ci->ci_cpumask = (1 << cpu_index(ci));
    444 	pmap_reference(pmap_kernel());
    445 	ci->ci_pmap = pmap_kernel();
    446 	ci->ci_tlbstate = TLBSTATE_STALE;
    447 
    448 	/*
    449 	 * Boot processor may not be attached first, but the below
    450 	 * must be done to allow booting other processors.
    451 	 */
    452 	if (!again) {
    453 		atomic_or_32(&ci->ci_flags, CPUF_PRESENT | CPUF_PRIMARY);
    454 		/* Basic init. */
    455 		cpu_intr_init(ci);
    456 		cpu_get_tsc_freq(ci);
    457 		cpu_init(ci);
    458 		cpu_set_tss_gates(ci);
    459 		pmap_cpu_init_late(ci);
    460 
    461 		/* Every processor needs to init it's own ipi h/w (similar to lapic) */
    462 		xen_ipi_init();
    463 		/* XXX: clock_init() */
    464 
    465 		/* Make sure DELAY() is initialized. */
    466 		DELAY(1);
    467 		again = true;
    468 	}
    469 
    470 	/* further PCB init done later. */
    471 
    472 	switch (caa->cpu_role) {
    473 	case CPU_ROLE_SP:
    474 		atomic_or_32(&ci->ci_flags, CPUF_SP);
    475 		cpu_identify(ci);
    476 #if 0
    477 		x86_errata();
    478 #endif
    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 		cpu_init(ci);
    487 #if 0
    488 		x86_errata();
    489 #endif
    490 		x86_cpu_idle_init();
    491 
    492 		break;
    493 
    494 	case CPU_ROLE_AP:
    495 		atomic_or_32(&ci->ci_flags, CPUF_AP);
    496 
    497 		/*
    498 		 * report on an AP
    499 		 */
    500 
    501 #if defined(MULTIPROCESSOR)
    502 		/* interrupt handler stack */
    503 		cpu_intr_init(ci);
    504 
    505 		/* Setup gdt */
    506 		gdt_alloc_cpu(ci);
    507 		//gdt_init_cpu(ci);
    508 
    509 		cpu_set_tss_gates(ci);
    510 		cpu_start_secondary(ci);
    511 
    512 		if (ci->ci_flags & CPUF_PRESENT) {
    513 			struct cpu_info *tmp;
    514 
    515 			cpu_identify(ci);
    516 			tmp = cpu_info_list;
    517 			while (tmp->ci_next)
    518 				tmp = tmp->ci_next;
    519 
    520 			tmp->ci_next = ci;
    521 		}
    522 #else
    523 		aprint_error(": not started\n");
    524 #endif
    525 		break;
    526 
    527 	default:
    528 		aprint_normal("\n");
    529 		panic("unknown processor type??\n");
    530 	}
    531 
    532 	pat_init(ci);
    533 	atomic_or_32(&cpus_attached, ci->ci_cpumask);
    534 
    535 #if 0
    536 	if (!pmf_device_register(self, cpu_suspend, cpu_resume))
    537 		aprint_error_dev(self, "couldn't establish power handler\n");
    538 #endif
    539 
    540 #ifdef MPVERBOSE
    541 	if (mp_verbose) {
    542 		struct lwp *l = ci->ci_data.cpu_idlelwp;
    543 		struct pcb *pcb = lwp_getpcb(l);
    544 
    545 		aprint_verbose_dev(self,
    546 		    "idle lwp at %p, idle sp at 0x%p\n",
    547 		    l,
    548 #ifdef i386
    549 		    (void *)pcb->pcb_esp
    550 #else /* i386 */
    551 		    (void *)pcb->pcb_rsp
    552 #endif /* i386 */
    553 		);
    554 
    555 	}
    556 #endif /* MPVERBOSE */
    557 }
    558 
    559 /*
    560  * Initialize the processor appropriately.
    561  */
    562 
    563 void
    564 cpu_init(struct cpu_info *ci)
    565 {
    566 
    567 	/*
    568 	 * On a P6 or above, enable global TLB caching if the
    569 	 * hardware supports it.
    570 	 */
    571 	if (cpu_feature[0] & CPUID_PGE)
    572 		lcr4(rcr4() | CR4_PGE);	/* enable global TLB caching */
    573 
    574 #ifdef XXXMTRR
    575 	/*
    576 	 * On a P6 or above, initialize MTRR's if the hardware supports them.
    577 	 */
    578 	if (cpu_feature[0] & CPUID_MTRR) {
    579 		if ((ci->ci_flags & CPUF_AP) == 0)
    580 			i686_mtrr_init_first();
    581 		mtrr_init_cpu(ci);
    582 	}
    583 #endif
    584 	/*
    585 	 * If we have FXSAVE/FXRESTOR, use them.
    586 	 */
    587 	if (cpu_feature[0] & CPUID_FXSR) {
    588 		lcr4(rcr4() | CR4_OSFXSR);
    589 
    590 		/*
    591 		 * If we have SSE/SSE2, enable XMM exceptions.
    592 		 */
    593 		if (cpu_feature[0] & (CPUID_SSE|CPUID_SSE2))
    594 			lcr4(rcr4() | CR4_OSXMMEXCPT);
    595 	}
    596 
    597 #ifdef __x86_64__
    598 	/* No user PGD mapped for this CPU yet */
    599 	ci->ci_xen_current_user_pgd = 0;
    600 #endif
    601 
    602 	atomic_or_32(&cpus_running, ci->ci_cpumask);
    603 	atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
    604 
    605 	/* XXX: register vcpu_register_runstate_memory_area, and figure out how to make sure this VCPU is running ? */
    606 }
    607 
    608 
    609 #ifdef MULTIPROCESSOR
    610 
    611 void
    612 cpu_boot_secondary_processors(void)
    613 {
    614 	struct cpu_info *ci;
    615 	u_long i;
    616 	for (i = 0; i < maxcpus; i++) {
    617 		ci = cpu_lookup(i);
    618 		if (ci == NULL)
    619 			continue;
    620 		if (ci->ci_data.cpu_idlelwp == NULL)
    621 			continue;
    622 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    623 			continue;
    624 		if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
    625 			continue;
    626 		cpu_boot_secondary(ci);
    627 	}
    628 
    629 	x86_mp_online = true;
    630 }
    631 
    632 static void
    633 cpu_init_idle_lwp(struct cpu_info *ci)
    634 {
    635 	struct lwp *l = ci->ci_data.cpu_idlelwp;
    636 	struct pcb *pcb = lwp_getpcb(l);
    637 
    638 	pcb->pcb_cr0 = rcr0();
    639 }
    640 
    641 void
    642 cpu_init_idle_lwps(void)
    643 {
    644 	struct cpu_info *ci;
    645 	u_long i;
    646 
    647 	for (i = 0; i < maxcpus; i++) {
    648 		ci = cpu_lookup(i);
    649 		if (ci == NULL)
    650 			continue;
    651 		if (ci->ci_data.cpu_idlelwp == NULL)
    652 			continue;
    653 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    654 			continue;
    655 		cpu_init_idle_lwp(ci);
    656 	}
    657 }
    658 
    659 static void
    660 cpu_start_secondary(struct cpu_info *ci)
    661 {
    662 	int i;
    663 
    664 	aprint_debug_dev(ci->ci_dev, "starting\n");
    665 
    666 	ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
    667 
    668 	if (CPU_STARTUP(ci, (vaddr_t) cpu_hatch) != 0) {
    669 		return;
    670 	}
    671 
    672 	/*
    673 	 * wait for it to become ready
    674 	 */
    675 	for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i > 0; i--) {
    676 #ifdef MPDEBUG
    677 		extern int cpu_trace[3];
    678 		static int otrace[3];
    679 		if (memcmp(otrace, cpu_trace, sizeof(otrace)) != 0) {
    680 			aprint_debug_dev(ci->ci_dev, "trace %02x %02x %02x\n",
    681 				cpu_trace[0], cpu_trace[1], cpu_trace[2]);
    682 			memcpy(otrace, cpu_trace, sizeof(otrace));
    683 		}
    684 #endif
    685 		delay(10);
    686 	}
    687 	if ((ci->ci_flags & CPUF_PRESENT) == 0) {
    688 		aprint_error_dev(ci->ci_dev, "failed to become ready\n");
    689 #if defined(MPDEBUG) && defined(DDB)
    690 		printf("dropping into debugger; continue from here to resume boot\n");
    691 		Debugger();
    692 #endif
    693 	}
    694 
    695 	CPU_START_CLEANUP(ci);
    696 }
    697 
    698 void
    699 cpu_boot_secondary(struct cpu_info *ci)
    700 {
    701 	int i;
    702 	atomic_or_32(&ci->ci_flags, CPUF_GO);
    703 	for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i > 0; i--) {
    704 		delay(10);
    705 	}
    706 	if ((ci->ci_flags & CPUF_RUNNING) == 0) {
    707 		aprint_error_dev(ci->ci_dev, "CPU failed to start\n");
    708 #if defined(MPDEBUG) && defined(DDB)
    709 		printf("dropping into debugger; continue from here to resume boot\n");
    710 		Debugger();
    711 #endif
    712 	}
    713 }
    714 
    715 /*
    716  * APs end up here immediately after initialisation and VCPUOP_up in
    717  * mp_cpu_start().
    718  * At this point, we are running in the idle pcb/idle stack of the new
    719  * CPU.  This function jumps to the idle loop and starts looking for
    720  * work.
    721  */
    722 extern void x86_64_tls_switch(struct lwp *);
    723 void
    724 cpu_hatch(void *v)
    725 {
    726 	struct cpu_info *ci = (struct cpu_info *)v;
    727 	struct pcb *pcb;
    728 	int s, i;
    729 
    730 	/* Setup TLS and kernel GS */
    731 	cpu_init_msrs(ci, true);
    732 	cpu_probe(ci);
    733 
    734 	atomic_or_32(&ci->ci_flags, CPUF_PRESENT);
    735 
    736 	while ((ci->ci_flags & CPUF_GO) == 0) {
    737 		/* Don't use delay, boot CPU may be patching the text. */
    738 		for (i = 10000; i != 0; i--)
    739 			x86_pause();
    740 	}
    741 
    742 	/* Because the text may have been patched in x86_patch(). */
    743 	x86_flush();
    744 	tlbflushg();
    745 
    746 	KASSERT((ci->ci_flags & CPUF_RUNNING) == 0);
    747 
    748 	pcb = lwp_getpcb(curlwp);
    749 	pcb->pcb_cr3 = pmap_pdirpa(pmap_kernel(), 0); /* XXX: consider using pmap_load() ? */
    750 	pcb = lwp_getpcb(ci->ci_data.cpu_idlelwp);
    751 	lcr0(pcb->pcb_cr0);
    752 
    753 	cpu_init_idt();
    754 	gdt_init_cpu(ci);
    755 	xen_ipi_init();
    756 
    757 	xen_initclocks();
    758 
    759 	/* XXX: lapic_initclocks(); */
    760 
    761 #ifdef i386
    762 #if NNPX > 0
    763 	npxinit(ci);
    764 #endif
    765 #else
    766 	fpuinit(ci);
    767 	/* XXX: fixme compile fpuinit(ci); */
    768 #endif
    769 
    770 	lldt(GSEL(GLDT_SEL, SEL_KPL));
    771 	ltr(ci->ci_tss_sel);
    772 
    773 	cpu_init(ci);
    774 	cpu_get_tsc_freq(ci);
    775 
    776 	s = splhigh();
    777 	x86_enable_intr();
    778 	splx(s);
    779 #if 0
    780 	x86_errata();
    781 #endif
    782 
    783 	aprint_debug_dev(ci->ci_dev, "running\n");
    784 
    785 	printf("\n\nAbout to switch to idle_loop()\n\n");
    786 
    787 	cpu_switchto(NULL, ci->ci_data.cpu_idlelwp, true);
    788 
    789 	panic("switch to idle_loop context returned!\n");
    790 	/* NOTREACHED */
    791 }
    792 
    793 #if defined(DDB)
    794 
    795 #include <ddb/db_output.h>
    796 #include <machine/db_machdep.h>
    797 
    798 /*
    799  * Dump CPU information from ddb.
    800  */
    801 void
    802 cpu_debug_dump(void)
    803 {
    804 	struct cpu_info *ci;
    805 	CPU_INFO_ITERATOR cii;
    806 
    807 	db_printf("addr		dev	id	flags	ipis	curlwp 		fpcurlwp\n");
    808 	for (CPU_INFO_FOREACH(cii, ci)) {
    809 		db_printf("%p	%s	%ld	%x	%x	%10p	%10p\n",
    810 		    ci,
    811 		    ci->ci_dev == NULL ? "BOOT" : device_xname(ci->ci_dev),
    812 		    (long)ci->ci_cpuid,
    813 		    ci->ci_flags, ci->ci_ipis,
    814 		    ci->ci_curlwp,
    815 		    ci->ci_fpcurlwp);
    816 	}
    817 }
    818 #endif /* DDB */
    819 
    820 #endif /* MULTIPROCESSOR */
    821 
    822 #ifdef i386
    823 #if 0
    824 static void
    825 tss_init(struct i386tss *tss, void *stack, void *func)
    826 {
    827 	memset(tss, 0, sizeof *tss);
    828 	tss->tss_esp0 = tss->tss_esp = (int)((char *)stack + USPACE - 16);
    829 	tss->tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
    830 	tss->__tss_cs = GSEL(GCODE_SEL, SEL_KPL);
    831 	tss->tss_fs = GSEL(GCPU_SEL, SEL_KPL);
    832 	tss->tss_gs = tss->__tss_es = tss->__tss_ds =
    833 	    tss->__tss_ss = GSEL(GDATA_SEL, SEL_KPL);
    834 	tss->tss_cr3 = pmap_kernel()->pm_pdirpa;
    835 	tss->tss_esp = (int)((char *)stack + USPACE - 16);
    836 	tss->tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
    837 	tss->__tss_eflags = PSL_MBO | PSL_NT;   /* XXX not needed? */
    838 	tss->__tss_eip = (int)func;
    839 }
    840 #endif
    841 
    842 /* XXX */
    843 #define IDTVEC(name)	__CONCAT(X, name)
    844 typedef void (vector)(void);
    845 extern vector IDTVEC(tss_trap08);
    846 #ifdef DDB
    847 extern vector Xintrddbipi;
    848 extern int ddb_vec;
    849 #endif
    850 
    851 static void
    852 cpu_set_tss_gates(struct cpu_info *ci)
    853 {
    854 #if 0
    855 	struct segment_descriptor sd;
    856 
    857 	ci->ci_doubleflt_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
    858 	    UVM_KMF_WIRED);
    859 	tss_init(&ci->ci_doubleflt_tss, ci->ci_doubleflt_stack,
    860 	    IDTVEC(tss_trap08));
    861 	setsegment(&sd, &ci->ci_doubleflt_tss, sizeof(struct i386tss) - 1,
    862 	    SDT_SYS386TSS, SEL_KPL, 0, 0);
    863 	ci->ci_gdt[GTRAPTSS_SEL].sd = sd;
    864 	setgate(&idt[8], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
    865 	    GSEL(GTRAPTSS_SEL, SEL_KPL));
    866 #endif
    867 
    868 #if defined(DDB) && defined(MULTIPROCESSOR)
    869 	/*
    870 	 * Set up separate handler for the DDB IPI, so that it doesn't
    871 	 * stomp on a possibly corrupted stack.
    872 	 *
    873 	 * XXX overwriting the gate set in db_machine_init.
    874 	 * Should rearrange the code so that it's set only once.
    875 	 */
    876 	ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
    877 	    UVM_KMF_WIRED);
    878 	tss_init(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
    879 	    Xintrddbipi);
    880 
    881 	setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
    882 	    SDT_SYS386TSS, SEL_KPL, 0, 0);
    883 	ci->ci_gdt[GIPITSS_SEL].sd = sd;
    884 
    885 	setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
    886 	    GSEL(GIPITSS_SEL, SEL_KPL));
    887 #endif
    888 }
    889 #else
    890 static void
    891 cpu_set_tss_gates(struct cpu_info *ci)
    892 {
    893 
    894 }
    895 #endif	/* i386 */
    896 
    897 extern void hypervisor_callback(void);
    898 extern void failsafe_callback(void);
    899 #ifdef __x86_64__
    900 typedef void (vector)(void);
    901 extern vector Xsyscall, Xsyscall32;
    902 #endif
    903 
    904 /*
    905  * Setup the "trampoline". On Xen, we setup nearly all cpu context
    906  * outside a trampoline, so we prototype and call targetrip like so:
    907  * void targetrip(struct cpu_info *);
    908  */
    909 
    910 static void
    911 gdt_prepframes(paddr_t *frames, vaddr_t base, uint32_t entries)
    912 {
    913 	int i;
    914 	for (i = 0; i < roundup(entries, PAGE_SIZE) >> PAGE_SHIFT; i++) {
    915 
    916 		frames[i] = ((paddr_t) xpmap_ptetomach(
    917 				(pt_entry_t *) (base + (i << PAGE_SHIFT))))
    918 			>> PAGE_SHIFT;
    919 
    920 		/* Mark Read-only */
    921 		pmap_pte_clearbits(kvtopte(base + (i << PAGE_SHIFT)),
    922 		    PG_RW);
    923 	}
    924 }
    925 
    926 extern char *ldtstore; /* XXX: Xen MP todo */
    927 
    928 static void
    929 xen_init_amd64_vcpuctxt(struct cpu_info *ci,
    930 			struct vcpu_guest_context *initctx,
    931 			void targetrip(struct cpu_info *))
    932 {
    933 	/* page frames to point at GDT */
    934 	extern int gdt_size;
    935 	paddr_t frames[16];
    936 	psize_t gdt_ents;
    937 
    938 	struct lwp *l;
    939 	struct pcb *pcb;
    940 
    941 	volatile struct vcpu_info *vci;
    942 
    943 	KASSERT(ci != NULL);
    944 	KASSERT(ci != &cpu_info_primary);
    945 	KASSERT(initctx != NULL);
    946 	KASSERT(targetrip != NULL);
    947 
    948 	memset(initctx, 0, sizeof *initctx);
    949 
    950 	gdt_ents = roundup(gdt_size, PAGE_SIZE) >> PAGE_SHIFT; /* XXX: re-investigate roundup(gdt_size... ) for gdt_ents. */
    951 	KASSERT(gdt_ents <= 16);
    952 
    953 	gdt_prepframes(frames, (vaddr_t) ci->ci_gdt, gdt_ents);
    954 
    955 	/* XXX: The stuff in here is amd64 specific. move to mptramp.[Sc] ? */
    956 
    957 	/* Initialise the vcpu context: We use idle_loop()'s pcb context. */
    958 
    959 	l = ci->ci_data.cpu_idlelwp;
    960 
    961 	KASSERT(l != NULL);
    962 	pcb = lwp_getpcb(l);
    963 	KASSERT(pcb != NULL);
    964 
    965 	/* resume with interrupts off */
    966 	vci = ci->ci_vcpu;
    967 	vci->evtchn_upcall_mask = 1;
    968 	xen_mb();
    969 
    970 	/* resume in kernel-mode */
    971 	initctx->flags = VGCF_in_kernel | VGCF_online;
    972 
    973 	/* Stack and entry points */
    974 	initctx->user_regs.rbp = pcb->pcb_rbp;
    975 	initctx->user_regs.rdi = (uint64_t) ci; /* targetrip(ci); */
    976 	initctx->user_regs.rip = (vaddr_t) targetrip;
    977 
    978 	initctx->user_regs.cs = GSEL(GCODE_SEL, SEL_KPL);
    979 
    980 	initctx->user_regs.rflags = pcb->pcb_flags;
    981 	initctx->user_regs.rsp = pcb->pcb_rsp;
    982 
    983 	/* Data segments */
    984 	initctx->user_regs.ss = GSEL(GDATA_SEL, SEL_KPL);
    985 	initctx->user_regs.es = GSEL(GDATA_SEL, SEL_KPL);
    986 	initctx->user_regs.ds = GSEL(GDATA_SEL, SEL_KPL);
    987 
    988 	/* GDT */
    989 	memcpy(initctx->gdt_frames, frames, sizeof frames);
    990 	initctx->gdt_ents = gdt_ents;
    991 
    992 	/* LDT */
    993 	initctx->ldt_base = (unsigned long) ldtstore;
    994 	initctx->ldt_ents = LDT_SIZE >> 3;
    995 
    996 	/* Kernel context state */
    997 	initctx->kernel_ss = GSEL(GDATA_SEL, SEL_KPL);
    998 	initctx->kernel_sp = pcb->pcb_rsp0;
    999 	initctx->ctrlreg[0] = pcb->pcb_cr0;
   1000 	initctx->ctrlreg[1] = 0; /* "resuming" from kernel - no User cr3. */
   1001 	initctx->ctrlreg[2] = pcb->pcb_cr2; /* XXX: */
   1002 	initctx->ctrlreg[3] = xpmap_ptom(pcb->pcb_cr3);
   1003 	initctx->ctrlreg[4] = CR4_PAE | CR4_OSFXSR | CR4_OSXMMEXCPT;
   1004 
   1005 
   1006 	/* Xen callbacks */
   1007 	initctx->event_callback_eip = (unsigned long) hypervisor_callback;
   1008 	initctx->failsafe_callback_eip = (unsigned long) failsafe_callback;
   1009 	initctx->syscall_callback_eip = (unsigned long) Xsyscall;
   1010 
   1011 	return;
   1012 }
   1013 
   1014 int
   1015 mp_cpu_start(struct cpu_info *ci, vaddr_t target)
   1016 {
   1017 
   1018 	int hyperror;
   1019 	struct vcpu_guest_context vcpuctx;
   1020 
   1021 	KASSERT(ci != NULL);
   1022 	KASSERT(ci != &cpu_info_primary);
   1023 	KASSERT(ci->ci_flags & CPUF_AP);
   1024 
   1025 	xen_init_amd64_vcpuctxt(ci, &vcpuctx, (void (*)(struct cpu_info *))target);
   1026 
   1027 	/* Initialise the given vcpu to execute cpu_hatch(ci); */
   1028 	if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_initialise, ci->ci_cpuid, &vcpuctx))) {
   1029 		aprint_error(": context initialisation failed. errno = %d\n", hyperror);
   1030 		return hyperror;
   1031 	}
   1032 
   1033 	/* Start it up */
   1034 
   1035 	/* First bring it down - yay, thanks Xen documentation for omitting this slight detail - lost only about 1 week reading through crap */
   1036 	if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL))) {
   1037 		aprint_error(": VCPUOP_down hypervisor command failed. errno = %d\n", hyperror);
   1038 		return hyperror;
   1039 	}
   1040 
   1041 	if ((hyperror = HYPERVISOR_vcpu_op(VCPUOP_up, ci->ci_cpuid, NULL))) {
   1042 		aprint_error(": VCPUOP_up hypervisor command failed. errno = %d\n", hyperror);
   1043 		return hyperror;
   1044 	}
   1045 
   1046 	if (!vcpu_is_up(ci)) {
   1047 		aprint_error(": did not come up\n");
   1048 		return -1;
   1049 	}
   1050 
   1051 	return 0;
   1052 }
   1053 
   1054 void
   1055 mp_cpu_start_cleanup(struct cpu_info *ci)
   1056 {
   1057 #if 0
   1058 	/*
   1059 	 * Ensure the NVRAM reset byte contains something vaguely sane.
   1060 	 */
   1061 
   1062 	outb(IO_RTC, NVRAM_RESET);
   1063 	outb(IO_RTC+1, NVRAM_RESET_RST);
   1064 #endif
   1065 	if (vcpu_is_up(ci)) {
   1066 		aprint_debug_dev(ci->ci_dev, "is started.\n");
   1067 	}
   1068 	else {
   1069 		aprint_error_dev(ci->ci_dev, "did not start up.\n");
   1070 	}
   1071 
   1072 }
   1073 
   1074 void
   1075 cpu_init_msrs(struct cpu_info *ci, bool full)
   1076 {
   1077 #ifdef __x86_64__
   1078 	if (full) {
   1079 		HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
   1080 		HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (uint64_t) ci);
   1081 		HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
   1082 	}
   1083 #endif	/* __x86_64__ */
   1084 
   1085 	if (cpu_feature[2] & CPUID_NOX)
   1086 		wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE);
   1087 
   1088 }
   1089 
   1090 void
   1091 cpu_offline_md(void)
   1092 {
   1093         int s;
   1094 
   1095         s = splhigh();
   1096 #ifdef __i386__
   1097         npxsave_cpu(true);
   1098 #else
   1099         fpusave_cpu(true);
   1100 #endif
   1101         splx(s);
   1102 }
   1103 
   1104 #if 0
   1105 /* XXX joerg restructure and restart CPUs individually */
   1106 static bool
   1107 cpu_suspend(device_t dv, const pmf_qual_t *qual)
   1108 {
   1109 	struct cpu_softc *sc = device_private(dv);
   1110 	struct cpu_info *ci = sc->sc_info;
   1111 	int err;
   1112 
   1113 	if (ci->ci_flags & CPUF_PRIMARY)
   1114 		return true;
   1115 	if (ci->ci_data.cpu_idlelwp == NULL)
   1116 		return true;
   1117 	if ((ci->ci_flags & CPUF_PRESENT) == 0)
   1118 		return true;
   1119 
   1120 	sc->sc_wasonline = !(ci->ci_schedstate.spc_flags & SPCF_OFFLINE);
   1121 
   1122 	if (sc->sc_wasonline) {
   1123 		mutex_enter(&cpu_lock);
   1124 		err = cpu_setstate(ci, false);
   1125 		mutex_exit(&cpu_lock);
   1126 
   1127 		if (err)
   1128 			return false;
   1129 	}
   1130 
   1131 	return true;
   1132 }
   1133 
   1134 static bool
   1135 cpu_resume(device_t dv, const pmf_qual_t *qual)
   1136 {
   1137 	struct cpu_softc *sc = device_private(dv);
   1138 	struct cpu_info *ci = sc->sc_info;
   1139 	int err = 0;
   1140 
   1141 	if (ci->ci_flags & CPUF_PRIMARY)
   1142 		return true;
   1143 	if (ci->ci_data.cpu_idlelwp == NULL)
   1144 		return true;
   1145 	if ((ci->ci_flags & CPUF_PRESENT) == 0)
   1146 		return true;
   1147 
   1148 	if (sc->sc_wasonline) {
   1149 		mutex_enter(&cpu_lock);
   1150 		err = cpu_setstate(ci, true);
   1151 		mutex_exit(&cpu_lock);
   1152 	}
   1153 
   1154 	return err == 0;
   1155 }
   1156 #endif
   1157 
   1158 void
   1159 cpu_get_tsc_freq(struct cpu_info *ci)
   1160 {
   1161 	uint32_t vcpu_tversion;
   1162 	const volatile vcpu_time_info_t *tinfo = &ci->ci_vcpu->time;
   1163 
   1164 	vcpu_tversion = tinfo->version;
   1165 	while (tinfo->version == vcpu_tversion); /* Wait for a time update. XXX: timeout ? */
   1166 
   1167 	uint64_t freq = 1000000000ULL << 32;
   1168 	freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
   1169 	if ( tinfo->tsc_shift < 0 )
   1170 		freq = freq << -tinfo->tsc_shift;
   1171 	else
   1172 		freq = freq >> tinfo->tsc_shift;
   1173 	ci->ci_data.cpu_cc_freq = freq;
   1174 }
   1175 
   1176 void
   1177 x86_cpu_idle_xen(void)
   1178 {
   1179 	struct cpu_info *ci = curcpu();
   1180 
   1181 	KASSERT(ci->ci_ilevel == IPL_NONE);
   1182 
   1183 	x86_disable_intr();
   1184 	if (!__predict_false(ci->ci_want_resched)) {
   1185 		idle_block();
   1186 
   1187 	} else {
   1188 		x86_enable_intr();
   1189 	}
   1190 }
   1191 
   1192 /*
   1193  * Loads pmap for the current CPU.
   1194  */
   1195 void
   1196 cpu_load_pmap(struct pmap *pmap)
   1197 {
   1198 #ifdef i386
   1199 #ifdef PAE
   1200 	int i, s;
   1201 	struct cpu_info *ci;
   1202 
   1203 	s = splvm(); /* just to be safe */
   1204 	xpq_queue_lock();
   1205 	ci = curcpu();
   1206 	paddr_t l3_pd = xpmap_ptom_masked(ci->ci_pae_l3_pdirpa);
   1207 	/* don't update the kernel L3 slot */
   1208 	for (i = 0 ; i < PDP_SIZE - 1; i++) {
   1209 		xpq_queue_pte_update(l3_pd + i * sizeof(pd_entry_t),
   1210 		    xpmap_ptom(pmap->pm_pdirpa[i]) | PG_V);
   1211 	}
   1212 	xpq_queue_unlock();
   1213 	splx(s);
   1214 	tlbflush();
   1215 #else /* PAE */
   1216 	lcr3(pmap_pdirpa(pmap, 0));
   1217 #endif /* PAE */
   1218 #endif /* i386 */
   1219 
   1220 #ifdef __x86_64__
   1221 	int i, s;
   1222 	pd_entry_t *old_pgd, *new_pgd;
   1223 	paddr_t addr;
   1224 	struct cpu_info *ci;
   1225 
   1226 	/* kernel pmap always in cr3 and should never go in user cr3 */
   1227 	if (pmap_pdirpa(pmap, 0) != pmap_pdirpa(pmap_kernel(), 0)) {
   1228 		ci = curcpu();
   1229 		/*
   1230 		 * Map user space address in kernel space and load
   1231 		 * user cr3
   1232 		 */
   1233 		s = splvm();
   1234 		new_pgd = pmap->pm_pdir;
   1235 		old_pgd = pmap_kernel()->pm_pdir;
   1236 		addr = xpmap_ptom(pmap_pdirpa(pmap_kernel(), 0));
   1237 		xpq_queue_lock();
   1238 		for (i = 0; i < PDIR_SLOT_PTE;
   1239 		    i++, addr += sizeof(pd_entry_t)) {
   1240 			if ((new_pgd[i] & PG_V) || (old_pgd[i] & PG_V))
   1241 				xpq_queue_pte_update(addr, new_pgd[i]);
   1242 		}
   1243 		xpq_queue_unlock();
   1244 		tlbflush();
   1245 		xpq_queue_lock();
   1246 		xen_set_user_pgd(pmap_pdirpa(pmap, 0));
   1247 		ci->ci_xen_current_user_pgd = pmap_pdirpa(pmap, 0);
   1248 		xpq_queue_unlock();
   1249 		splx(s);
   1250 	}
   1251 #endif /* __x86_64__ */
   1252 }
   1253