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