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