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