Home | History | Annotate | Line # | Download | only in x86
cpu.c revision 1.2.4.2
      1  1.2.4.2  ad /*	$NetBSD: cpu.c,v 1.2.4.2 2007/12/03 19:04:39 ad Exp $	*/
      2  1.2.4.2  ad /* NetBSD: cpu.c,v 1.18 2004/02/20 17:35:01 yamt Exp  */
      3  1.2.4.2  ad 
      4  1.2.4.2  ad /*-
      5  1.2.4.2  ad  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      6  1.2.4.2  ad  * All rights reserved.
      7  1.2.4.2  ad  *
      8  1.2.4.2  ad  * This code is derived from software contributed to The NetBSD Foundation
      9  1.2.4.2  ad  * by RedBack Networks Inc.
     10  1.2.4.2  ad  *
     11  1.2.4.2  ad  * Author: Bill Sommerfeld
     12  1.2.4.2  ad  *
     13  1.2.4.2  ad  * Redistribution and use in source and binary forms, with or without
     14  1.2.4.2  ad  * modification, are permitted provided that the following conditions
     15  1.2.4.2  ad  * are met:
     16  1.2.4.2  ad  * 1. Redistributions of source code must retain the above copyright
     17  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer.
     18  1.2.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
     20  1.2.4.2  ad  *    documentation and/or other materials provided with the distribution.
     21  1.2.4.2  ad  * 3. All advertising materials mentioning features or use of this software
     22  1.2.4.2  ad  *    must display the following acknowledgement:
     23  1.2.4.2  ad  *        This product includes software developed by the NetBSD
     24  1.2.4.2  ad  *        Foundation, Inc. and its contributors.
     25  1.2.4.2  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     26  1.2.4.2  ad  *    contributors may be used to endorse or promote products derived
     27  1.2.4.2  ad  *    from this software without specific prior written permission.
     28  1.2.4.2  ad  *
     29  1.2.4.2  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     30  1.2.4.2  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     31  1.2.4.2  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     32  1.2.4.2  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     33  1.2.4.2  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     34  1.2.4.2  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     35  1.2.4.2  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     36  1.2.4.2  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     37  1.2.4.2  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     38  1.2.4.2  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     39  1.2.4.2  ad  * POSSIBILITY OF SUCH DAMAGE.
     40  1.2.4.2  ad  */
     41  1.2.4.2  ad 
     42  1.2.4.2  ad /*
     43  1.2.4.2  ad  * Copyright (c) 1999 Stefan Grefen
     44  1.2.4.2  ad  *
     45  1.2.4.2  ad  * Redistribution and use in source and binary forms, with or without
     46  1.2.4.2  ad  * modification, are permitted provided that the following conditions
     47  1.2.4.2  ad  * are met:
     48  1.2.4.2  ad  * 1. Redistributions of source code must retain the above copyright
     49  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer.
     50  1.2.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     51  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
     52  1.2.4.2  ad  *    documentation and/or other materials provided with the distribution.
     53  1.2.4.2  ad  * 3. All advertising materials mentioning features or use of this software
     54  1.2.4.2  ad  *    must display the following acknowledgement:
     55  1.2.4.2  ad  *      This product includes software developed by the NetBSD
     56  1.2.4.2  ad  *      Foundation, Inc. and its contributors.
     57  1.2.4.2  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     58  1.2.4.2  ad  *    contributors may be used to endorse or promote products derived
     59  1.2.4.2  ad  *    from this software without specific prior written permission.
     60  1.2.4.2  ad  *
     61  1.2.4.2  ad  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
     62  1.2.4.2  ad  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63  1.2.4.2  ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64  1.2.4.2  ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
     65  1.2.4.2  ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66  1.2.4.2  ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67  1.2.4.2  ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68  1.2.4.2  ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69  1.2.4.2  ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70  1.2.4.2  ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71  1.2.4.2  ad  * SUCH DAMAGE.
     72  1.2.4.2  ad  */
     73  1.2.4.2  ad 
     74  1.2.4.2  ad #include <sys/cdefs.h>
     75  1.2.4.2  ad __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.2.4.2 2007/12/03 19:04:39 ad Exp $");
     76  1.2.4.2  ad 
     77  1.2.4.2  ad #include "opt_ddb.h"
     78  1.2.4.2  ad #include "opt_multiprocessor.h"
     79  1.2.4.2  ad #include "opt_mpbios.h"		/* for MPDEBUG */
     80  1.2.4.2  ad #include "opt_mtrr.h"
     81  1.2.4.2  ad #include "opt_xen.h"
     82  1.2.4.2  ad 
     83  1.2.4.2  ad #include "lapic.h"
     84  1.2.4.2  ad #include "ioapic.h"
     85  1.2.4.2  ad 
     86  1.2.4.2  ad #include <sys/param.h>
     87  1.2.4.2  ad #include <sys/proc.h>
     88  1.2.4.2  ad #include <sys/user.h>
     89  1.2.4.2  ad #include <sys/systm.h>
     90  1.2.4.2  ad #include <sys/device.h>
     91  1.2.4.2  ad #include <sys/malloc.h>
     92  1.2.4.2  ad 
     93  1.2.4.2  ad #include <uvm/uvm_extern.h>
     94  1.2.4.2  ad 
     95  1.2.4.2  ad #include <machine/cpu.h>
     96  1.2.4.2  ad #include <machine/cpufunc.h>
     97  1.2.4.2  ad #include <machine/cpuvar.h>
     98  1.2.4.2  ad #include <machine/pmap.h>
     99  1.2.4.2  ad #include <machine/vmparam.h>
    100  1.2.4.2  ad #include <machine/mpbiosvar.h>
    101  1.2.4.2  ad #include <machine/pcb.h>
    102  1.2.4.2  ad #include <machine/specialreg.h>
    103  1.2.4.2  ad #include <machine/segments.h>
    104  1.2.4.2  ad #include <machine/gdt.h>
    105  1.2.4.2  ad #include <machine/mtrr.h>
    106  1.2.4.2  ad #include <machine/pio.h>
    107  1.2.4.2  ad 
    108  1.2.4.2  ad #ifdef XEN3
    109  1.2.4.2  ad #include <xen/vcpuvar.h>
    110  1.2.4.2  ad #endif
    111  1.2.4.2  ad 
    112  1.2.4.2  ad #if NLAPIC > 0
    113  1.2.4.2  ad #include <machine/apicvar.h>
    114  1.2.4.2  ad #include <machine/i82489reg.h>
    115  1.2.4.2  ad #include <machine/i82489var.h>
    116  1.2.4.2  ad #endif
    117  1.2.4.2  ad 
    118  1.2.4.2  ad #if NIOAPIC > 0
    119  1.2.4.2  ad #include <machine/i82093var.h>
    120  1.2.4.2  ad #endif
    121  1.2.4.2  ad 
    122  1.2.4.2  ad #include <dev/ic/mc146818reg.h>
    123  1.2.4.2  ad #include <dev/isa/isareg.h>
    124  1.2.4.2  ad 
    125  1.2.4.2  ad int     cpu_match(struct device *, struct cfdata *, void *);
    126  1.2.4.2  ad void    cpu_attach(struct device *, struct device *, void *);
    127  1.2.4.2  ad #ifdef XEN3
    128  1.2.4.2  ad int     vcpu_match(struct device *, struct cfdata *, void *);
    129  1.2.4.2  ad void    vcpu_attach(struct device *, struct device *, void *);
    130  1.2.4.2  ad #endif
    131  1.2.4.2  ad void    cpu_attach_common(struct device *, struct device *, void *);
    132  1.2.4.2  ad 
    133  1.2.4.2  ad struct cpu_softc {
    134  1.2.4.2  ad 	struct device sc_dev;		/* device tree glue */
    135  1.2.4.2  ad 	struct cpu_info *sc_info;	/* pointer to CPU info */
    136  1.2.4.2  ad };
    137  1.2.4.2  ad 
    138  1.2.4.2  ad int mp_cpu_start(struct cpu_info *);
    139  1.2.4.2  ad void mp_cpu_start_cleanup(struct cpu_info *);
    140  1.2.4.2  ad const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
    141  1.2.4.2  ad 				      mp_cpu_start_cleanup };
    142  1.2.4.2  ad 
    143  1.2.4.2  ad CFATTACH_DECL(cpu, sizeof(struct cpu_softc),
    144  1.2.4.2  ad     cpu_match, cpu_attach, NULL, NULL);
    145  1.2.4.2  ad #ifdef XEN3
    146  1.2.4.2  ad CFATTACH_DECL(vcpu, sizeof(struct cpu_softc),
    147  1.2.4.2  ad     vcpu_match, vcpu_attach, NULL, NULL);
    148  1.2.4.2  ad #endif
    149  1.2.4.2  ad 
    150  1.2.4.2  ad /*
    151  1.2.4.2  ad  * Statically-allocated CPU info for the primary CPU (or the only
    152  1.2.4.2  ad  * CPU, on uniprocessors).  The CPU info list is initialized to
    153  1.2.4.2  ad  * point at it.
    154  1.2.4.2  ad  */
    155  1.2.4.2  ad #ifdef TRAPLOG
    156  1.2.4.2  ad #include <machine/tlog.h>
    157  1.2.4.2  ad struct tlog tlog_primary;
    158  1.2.4.2  ad #endif
    159  1.2.4.2  ad struct cpu_info cpu_info_primary = {
    160  1.2.4.2  ad 	.ci_self = &cpu_info_primary,
    161  1.2.4.2  ad #ifndef __x86_64__
    162  1.2.4.2  ad 	.ci_self150 = (uint8_t *)&cpu_info_primary + 0x150,
    163  1.2.4.2  ad #endif
    164  1.2.4.2  ad 	.ci_curlwp = &lwp0,
    165  1.2.4.2  ad #ifdef TRAPLOG
    166  1.2.4.2  ad 	.ci_tlog = &tlog_primary,
    167  1.2.4.2  ad #endif
    168  1.2.4.2  ad 
    169  1.2.4.2  ad };
    170  1.2.4.2  ad struct cpu_info phycpu_info_primary = {
    171  1.2.4.2  ad 	.ci_self = &phycpu_info_primary,
    172  1.2.4.2  ad #ifndef __x86_64__
    173  1.2.4.2  ad 	.ci_self150 = (uint8_t *)&phycpu_info_primary + 0x150,
    174  1.2.4.2  ad #endif
    175  1.2.4.2  ad };
    176  1.2.4.2  ad 
    177  1.2.4.2  ad struct cpu_info *cpu_info_list = &cpu_info_primary;
    178  1.2.4.2  ad 
    179  1.2.4.2  ad static void	cpu_set_tss_gates(struct cpu_info *ci);
    180  1.2.4.2  ad 
    181  1.2.4.2  ad u_int32_t cpus_attached = 0;
    182  1.2.4.2  ad 
    183  1.2.4.2  ad struct cpu_info *phycpu_info[X86_MAXPROCS] = { &cpu_info_primary };
    184  1.2.4.2  ad 
    185  1.2.4.2  ad #ifdef MULTIPROCESSOR
    186  1.2.4.2  ad /*
    187  1.2.4.2  ad  * Array of CPU info structures.  Must be statically-allocated because
    188  1.2.4.2  ad  * curproc, etc. are used early.
    189  1.2.4.2  ad  */
    190  1.2.4.2  ad struct cpu_info *cpu_info[X86_MAXPROCS] = { &cpu_info_primary };
    191  1.2.4.2  ad 
    192  1.2.4.2  ad u_int32_t cpus_running = 0;
    193  1.2.4.2  ad 
    194  1.2.4.2  ad void    	cpu_hatch(void *);
    195  1.2.4.2  ad static void    	cpu_boot_secondary(struct cpu_info *ci);
    196  1.2.4.2  ad static void    	cpu_start_secondary(struct cpu_info *ci);
    197  1.2.4.2  ad static void	cpu_copy_trampoline(void);
    198  1.2.4.2  ad 
    199  1.2.4.2  ad /*
    200  1.2.4.2  ad  * Runs once per boot once multiprocessor goo has been detected and
    201  1.2.4.2  ad  * the local APIC on the boot processor has been mapped.
    202  1.2.4.2  ad  *
    203  1.2.4.2  ad  * Called from lapic_boot_init() (from mpbios_scan()).
    204  1.2.4.2  ad  */
    205  1.2.4.2  ad void
    206  1.2.4.2  ad cpu_init_first()
    207  1.2.4.2  ad {
    208  1.2.4.2  ad 	int cpunum = lapic_cpu_number();
    209  1.2.4.2  ad 
    210  1.2.4.2  ad 	if (cpunum != 0) {
    211  1.2.4.2  ad 		cpu_info[0] = NULL;
    212  1.2.4.2  ad 		cpu_info[cpunum] = &cpu_info_primary;
    213  1.2.4.2  ad 	}
    214  1.2.4.2  ad 
    215  1.2.4.2  ad 	cpu_copy_trampoline();
    216  1.2.4.2  ad }
    217  1.2.4.2  ad #endif
    218  1.2.4.2  ad 
    219  1.2.4.2  ad int
    220  1.2.4.2  ad cpu_match(parent, match, aux)
    221  1.2.4.2  ad 	struct device *parent;
    222  1.2.4.2  ad 	struct cfdata *match;
    223  1.2.4.2  ad 	void *aux;
    224  1.2.4.2  ad {
    225  1.2.4.2  ad 
    226  1.2.4.2  ad 	return 1;
    227  1.2.4.2  ad }
    228  1.2.4.2  ad 
    229  1.2.4.2  ad void
    230  1.2.4.2  ad cpu_attach(parent, self, aux)
    231  1.2.4.2  ad 	struct device *parent, *self;
    232  1.2.4.2  ad 	void *aux;
    233  1.2.4.2  ad {
    234  1.2.4.2  ad #ifdef XEN3
    235  1.2.4.2  ad 	struct cpu_softc *sc = (void *) self;
    236  1.2.4.2  ad 	struct cpu_attach_args *caa = aux;
    237  1.2.4.2  ad 	struct cpu_info *ci;
    238  1.2.4.2  ad 	int cpunum = caa->cpu_number;
    239  1.2.4.2  ad 
    240  1.2.4.2  ad 	/*
    241  1.2.4.2  ad 	 * If we're an Application Processor, allocate a cpu_info
    242  1.2.4.2  ad 	 * structure, otherwise use the primary's.
    243  1.2.4.2  ad 	 */
    244  1.2.4.2  ad 	if (caa->cpu_role == CPU_ROLE_AP) {
    245  1.2.4.2  ad 		ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
    246  1.2.4.2  ad 		if (phycpu_info[cpunum] != NULL)
    247  1.2.4.2  ad 			panic("cpu at apic id %d already attached?", cpunum);
    248  1.2.4.2  ad 		phycpu_info[cpunum] = ci;
    249  1.2.4.2  ad 	} else {
    250  1.2.4.2  ad 		ci = &phycpu_info_primary;
    251  1.2.4.2  ad 		if (cpunum != 0) {
    252  1.2.4.2  ad 			phycpu_info[0] = NULL;
    253  1.2.4.2  ad 			phycpu_info[cpunum] = ci;
    254  1.2.4.2  ad 		}
    255  1.2.4.2  ad 	}
    256  1.2.4.2  ad 
    257  1.2.4.2  ad 	ci->ci_self = ci;
    258  1.2.4.2  ad 	sc->sc_info = ci;
    259  1.2.4.2  ad 
    260  1.2.4.2  ad 	ci->ci_dev = self;
    261  1.2.4.2  ad 	ci->ci_apicid = caa->cpu_number;
    262  1.2.4.2  ad 	ci->ci_cpuid = ci->ci_apicid;
    263  1.2.4.2  ad 
    264  1.2.4.2  ad 	printf(": ");
    265  1.2.4.2  ad 	switch (caa->cpu_role) {
    266  1.2.4.2  ad 	case CPU_ROLE_SP:
    267  1.2.4.2  ad 		printf("(uniprocessor)\n");
    268  1.2.4.2  ad 		ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
    269  1.2.4.2  ad 		break;
    270  1.2.4.2  ad 
    271  1.2.4.2  ad 	case CPU_ROLE_BP:
    272  1.2.4.2  ad 		printf("(boot processor)\n");
    273  1.2.4.2  ad 		ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
    274  1.2.4.2  ad #if NIOAPIC > 0
    275  1.2.4.2  ad 		ioapic_bsp_id = caa->cpu_number;
    276  1.2.4.2  ad #endif
    277  1.2.4.2  ad 		break;
    278  1.2.4.2  ad 
    279  1.2.4.2  ad 	case CPU_ROLE_AP:
    280  1.2.4.2  ad 		/*
    281  1.2.4.2  ad 		 * report on an AP
    282  1.2.4.2  ad 		 */
    283  1.2.4.2  ad 		printf("(application processor)\n");
    284  1.2.4.2  ad 		break;
    285  1.2.4.2  ad 
    286  1.2.4.2  ad 	default:
    287  1.2.4.2  ad 		panic("unknown processor type??\n");
    288  1.2.4.2  ad 	}
    289  1.2.4.2  ad 	return;
    290  1.2.4.2  ad #else
    291  1.2.4.2  ad 	cpu_attach_common(parent, self, aux);
    292  1.2.4.2  ad #endif
    293  1.2.4.2  ad }
    294  1.2.4.2  ad 
    295  1.2.4.2  ad #ifdef XEN3
    296  1.2.4.2  ad int
    297  1.2.4.2  ad vcpu_match(parent, match, aux)
    298  1.2.4.2  ad 	struct device *parent;
    299  1.2.4.2  ad 	struct cfdata *match;
    300  1.2.4.2  ad 	void *aux;
    301  1.2.4.2  ad {
    302  1.2.4.2  ad 	struct vcpu_attach_args *vcaa = aux;
    303  1.2.4.2  ad 
    304  1.2.4.2  ad 	if (strcmp(vcaa->vcaa_name, match->cf_name) == 0)
    305  1.2.4.2  ad 		return 1;
    306  1.2.4.2  ad 	return 0;
    307  1.2.4.2  ad }
    308  1.2.4.2  ad 
    309  1.2.4.2  ad void
    310  1.2.4.2  ad vcpu_attach(parent, self, aux)
    311  1.2.4.2  ad 	struct device *parent, *self;
    312  1.2.4.2  ad 	void *aux;
    313  1.2.4.2  ad {
    314  1.2.4.2  ad 	struct vcpu_attach_args *vcaa = aux;
    315  1.2.4.2  ad 
    316  1.2.4.2  ad 	cpu_attach_common(parent, self, &vcaa->vcaa_caa);
    317  1.2.4.2  ad }
    318  1.2.4.2  ad #endif
    319  1.2.4.2  ad 
    320  1.2.4.2  ad static void
    321  1.2.4.2  ad cpu_vm_init(struct cpu_info *ci)
    322  1.2.4.2  ad {
    323  1.2.4.2  ad 	int ncolors = 2, i;
    324  1.2.4.2  ad 
    325  1.2.4.2  ad 	for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
    326  1.2.4.2  ad 		struct x86_cache_info *cai;
    327  1.2.4.2  ad 		int tcolors;
    328  1.2.4.2  ad 
    329  1.2.4.2  ad 		cai = &ci->ci_cinfo[i];
    330  1.2.4.2  ad 
    331  1.2.4.2  ad 		tcolors = atop(cai->cai_totalsize);
    332  1.2.4.2  ad 		switch(cai->cai_associativity) {
    333  1.2.4.2  ad 		case 0xff:
    334  1.2.4.2  ad 			tcolors = 1; /* fully associative */
    335  1.2.4.2  ad 			break;
    336  1.2.4.2  ad 		case 0:
    337  1.2.4.2  ad 		case 1:
    338  1.2.4.2  ad 			break;
    339  1.2.4.2  ad 		default:
    340  1.2.4.2  ad 			tcolors /= cai->cai_associativity;
    341  1.2.4.2  ad 		}
    342  1.2.4.2  ad 		ncolors = max(ncolors, tcolors);
    343  1.2.4.2  ad 	}
    344  1.2.4.2  ad 
    345  1.2.4.2  ad 	/*
    346  1.2.4.2  ad 	 * Knowing the size of the largest cache on this CPU, re-color
    347  1.2.4.2  ad 	 * our pages.
    348  1.2.4.2  ad 	 */
    349  1.2.4.2  ad 	if (ncolors <= uvmexp.ncolors)
    350  1.2.4.2  ad 		return;
    351  1.2.4.2  ad 	printf("%s: %d page colors\n", ci->ci_dev->dv_xname, ncolors);
    352  1.2.4.2  ad 	uvm_page_recolor(ncolors);
    353  1.2.4.2  ad }
    354  1.2.4.2  ad 
    355  1.2.4.2  ad void
    356  1.2.4.2  ad cpu_attach_common(parent, self, aux)
    357  1.2.4.2  ad 	struct device *parent, *self;
    358  1.2.4.2  ad 	void *aux;
    359  1.2.4.2  ad {
    360  1.2.4.2  ad 	struct cpu_softc *sc = (void *) self;
    361  1.2.4.2  ad 	struct cpu_attach_args *caa = aux;
    362  1.2.4.2  ad 	struct cpu_info *ci;
    363  1.2.4.2  ad #if defined(MULTIPROCESSOR)
    364  1.2.4.2  ad 	int cpunum = caa->cpu_number;
    365  1.2.4.2  ad #endif
    366  1.2.4.2  ad 
    367  1.2.4.2  ad 	/*
    368  1.2.4.2  ad 	 * If we're an Application Processor, allocate a cpu_info
    369  1.2.4.2  ad 	 * structure, otherwise use the primary's.
    370  1.2.4.2  ad 	 */
    371  1.2.4.2  ad 	if (caa->cpu_role == CPU_ROLE_AP) {
    372  1.2.4.2  ad 		ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
    373  1.2.4.2  ad #if defined(MULTIPROCESSOR)
    374  1.2.4.2  ad 		if (cpu_info[cpunum] != NULL)
    375  1.2.4.2  ad 			panic("cpu at apic id %d already attached?", cpunum);
    376  1.2.4.2  ad 		cpu_info[cpunum] = ci;
    377  1.2.4.2  ad #endif
    378  1.2.4.2  ad #ifdef TRAPLOG
    379  1.2.4.2  ad 		ci->ci_tlog_base = malloc(sizeof(struct tlog),
    380  1.2.4.2  ad 		    M_DEVBUF, M_WAITOK);
    381  1.2.4.2  ad #endif
    382  1.2.4.2  ad 	} else {
    383  1.2.4.2  ad 		ci = &cpu_info_primary;
    384  1.2.4.2  ad #if defined(MULTIPROCESSOR)
    385  1.2.4.2  ad 		if (cpunum != lapic_cpu_number()) {
    386  1.2.4.2  ad 			panic("%s: running CPU is at apic %d"
    387  1.2.4.2  ad 			    " instead of at expected %d",
    388  1.2.4.2  ad 			    sc->sc_dev.dv_xname, lapic_cpu_number(), cpunum);
    389  1.2.4.2  ad 		}
    390  1.2.4.2  ad #endif
    391  1.2.4.2  ad 	}
    392  1.2.4.2  ad 
    393  1.2.4.2  ad 	ci->ci_self = ci;
    394  1.2.4.2  ad 	sc->sc_info = ci;
    395  1.2.4.2  ad 
    396  1.2.4.2  ad 	ci->ci_dev = self;
    397  1.2.4.2  ad 	ci->ci_apicid = caa->cpu_number;
    398  1.2.4.2  ad #ifdef MULTIPROCESSOR
    399  1.2.4.2  ad 	ci->ci_cpuid = ci->ci_apicid;
    400  1.2.4.2  ad #else
    401  1.2.4.2  ad 	ci->ci_cpuid = 0;	/* False for APs, but they're not used anyway */
    402  1.2.4.2  ad #endif
    403  1.2.4.2  ad 	ci->ci_cpumask = (1 << ci->ci_cpuid);
    404  1.2.4.2  ad 	ci->ci_func = caa->cpu_func;
    405  1.2.4.2  ad 
    406  1.2.4.2  ad #ifndef __x86_64__
    407  1.2.4.2  ad 	simple_lock_init(&ci->ci_slock);
    408  1.2.4.2  ad #endif
    409  1.2.4.2  ad 
    410  1.2.4.2  ad 	if (caa->cpu_role == CPU_ROLE_AP) {
    411  1.2.4.2  ad #if defined(MULTIPROCESSOR)
    412  1.2.4.2  ad 		int error;
    413  1.2.4.2  ad 
    414  1.2.4.2  ad 		error = mi_cpu_attach(ci);
    415  1.2.4.2  ad 		if (error != 0) {
    416  1.2.4.2  ad 			aprint_normal("\n");
    417  1.2.4.2  ad 			aprint_error("%s: mi_cpu_attach failed with %d\n",
    418  1.2.4.2  ad 			    sc->sc_dev.dv_xname, error);
    419  1.2.4.2  ad 			return;
    420  1.2.4.2  ad 		}
    421  1.2.4.2  ad #endif
    422  1.2.4.2  ad 	} else {
    423  1.2.4.2  ad 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
    424  1.2.4.2  ad 	}
    425  1.2.4.2  ad 
    426  1.2.4.2  ad 	pmap_reference(pmap_kernel());
    427  1.2.4.2  ad 	ci->ci_pmap = pmap_kernel();
    428  1.2.4.2  ad 	ci->ci_tlbstate = TLBSTATE_STALE;
    429  1.2.4.2  ad 
    430  1.2.4.2  ad 	/* further PCB init done later. */
    431  1.2.4.2  ad 
    432  1.2.4.2  ad 	printf(": ");
    433  1.2.4.2  ad 
    434  1.2.4.2  ad 	switch (caa->cpu_role) {
    435  1.2.4.2  ad 	case CPU_ROLE_SP:
    436  1.2.4.2  ad 		printf("(uniprocessor)\n");
    437  1.2.4.2  ad 		ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
    438  1.2.4.2  ad 		cpu_intr_init(ci);
    439  1.2.4.2  ad 		identifycpu(ci);
    440  1.2.4.2  ad 		cpu_init(ci);
    441  1.2.4.2  ad 		cpu_set_tss_gates(ci);
    442  1.2.4.2  ad 		break;
    443  1.2.4.2  ad 
    444  1.2.4.2  ad 	case CPU_ROLE_BP:
    445  1.2.4.2  ad 		printf("apid %d (boot processor)\n", caa->cpu_number);
    446  1.2.4.2  ad 		ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
    447  1.2.4.2  ad 		cpu_intr_init(ci);
    448  1.2.4.2  ad 		identifycpu(ci);
    449  1.2.4.2  ad 		cpu_init(ci);
    450  1.2.4.2  ad 		cpu_set_tss_gates(ci);
    451  1.2.4.2  ad 		break;
    452  1.2.4.2  ad 
    453  1.2.4.2  ad 	case CPU_ROLE_AP:
    454  1.2.4.2  ad 		/*
    455  1.2.4.2  ad 		 * report on an AP
    456  1.2.4.2  ad 		 */
    457  1.2.4.2  ad 		printf("apid %d (application processor)\n", caa->cpu_number);
    458  1.2.4.2  ad 
    459  1.2.4.2  ad #if defined(MULTIPROCESSOR)
    460  1.2.4.2  ad 		cpu_intr_init(ci);
    461  1.2.4.2  ad 		gdt_alloc_cpu(ci);
    462  1.2.4.2  ad 		cpu_set_tss_gates(ci);
    463  1.2.4.2  ad 		cpu_start_secondary(ci);
    464  1.2.4.2  ad 		if (ci->ci_flags & CPUF_PRESENT) {
    465  1.2.4.2  ad 			identifycpu(ci);
    466  1.2.4.2  ad 			ci->ci_next = cpu_info_list->ci_next;
    467  1.2.4.2  ad 			cpu_info_list->ci_next = ci;
    468  1.2.4.2  ad 		}
    469  1.2.4.2  ad #else
    470  1.2.4.2  ad 		printf("%s: not started\n", sc->sc_dev.dv_xname);
    471  1.2.4.2  ad #endif
    472  1.2.4.2  ad 		break;
    473  1.2.4.2  ad 
    474  1.2.4.2  ad 	default:
    475  1.2.4.2  ad 		panic("unknown processor type??\n");
    476  1.2.4.2  ad 	}
    477  1.2.4.2  ad 	cpu_vm_init(ci);
    478  1.2.4.2  ad 
    479  1.2.4.2  ad 	cpus_attached |= (1 << ci->ci_cpuid);
    480  1.2.4.2  ad 
    481  1.2.4.2  ad #if defined(MULTIPROCESSOR)
    482  1.2.4.2  ad 	if (mp_verbose) {
    483  1.2.4.2  ad 		struct lwp *l = ci->ci_data.cpu_idlelwp;
    484  1.2.4.2  ad 
    485  1.2.4.2  ad 		aprint_verbose("%s: idle lwp at %p, idle sp at 0x%x\n",
    486  1.2.4.2  ad 		    sc->sc_dev.dv_xname, l, l->l_addr->u_pcb.pcb_esp);
    487  1.2.4.2  ad 	}
    488  1.2.4.2  ad #endif
    489  1.2.4.2  ad }
    490  1.2.4.2  ad 
    491  1.2.4.2  ad /*
    492  1.2.4.2  ad  * Initialize the processor appropriately.
    493  1.2.4.2  ad  */
    494  1.2.4.2  ad 
    495  1.2.4.2  ad void
    496  1.2.4.2  ad cpu_init(ci)
    497  1.2.4.2  ad 	struct cpu_info *ci;
    498  1.2.4.2  ad {
    499  1.2.4.2  ad 	/* configure the CPU if needed */
    500  1.2.4.2  ad 	if (ci->cpu_setup != NULL)
    501  1.2.4.2  ad 		(*ci->cpu_setup)(ci);
    502  1.2.4.2  ad 
    503  1.2.4.2  ad 	/*
    504  1.2.4.2  ad 	 * On a P6 or above, enable global TLB caching if the
    505  1.2.4.2  ad 	 * hardware supports it.
    506  1.2.4.2  ad 	 */
    507  1.2.4.2  ad 	if (cpu_feature & CPUID_PGE)
    508  1.2.4.2  ad 		lcr4(rcr4() | CR4_PGE);	/* enable global TLB caching */
    509  1.2.4.2  ad 
    510  1.2.4.2  ad #ifdef XXXMTRR
    511  1.2.4.2  ad 	/*
    512  1.2.4.2  ad 	 * On a P6 or above, initialize MTRR's if the hardware supports them.
    513  1.2.4.2  ad 	 */
    514  1.2.4.2  ad 	if (cpu_feature & CPUID_MTRR) {
    515  1.2.4.2  ad 		if ((ci->ci_flags & CPUF_AP) == 0)
    516  1.2.4.2  ad 			i686_mtrr_init_first();
    517  1.2.4.2  ad 		mtrr_init_cpu(ci);
    518  1.2.4.2  ad 	}
    519  1.2.4.2  ad #endif
    520  1.2.4.2  ad 	/*
    521  1.2.4.2  ad 	 * If we have FXSAVE/FXRESTOR, use them.
    522  1.2.4.2  ad 	 */
    523  1.2.4.2  ad 	if (cpu_feature & CPUID_FXSR) {
    524  1.2.4.2  ad 		lcr4(rcr4() | CR4_OSFXSR);
    525  1.2.4.2  ad 
    526  1.2.4.2  ad 		/*
    527  1.2.4.2  ad 		 * If we have SSE/SSE2, enable XMM exceptions.
    528  1.2.4.2  ad 		 */
    529  1.2.4.2  ad 		if (cpu_feature & (CPUID_SSE|CPUID_SSE2))
    530  1.2.4.2  ad 			lcr4(rcr4() | CR4_OSXMMEXCPT);
    531  1.2.4.2  ad 	}
    532  1.2.4.2  ad 
    533  1.2.4.2  ad #ifdef MULTIPROCESSOR
    534  1.2.4.2  ad 	ci->ci_flags |= CPUF_RUNNING;
    535  1.2.4.2  ad 	cpus_running |= 1 << ci->ci_cpuid;
    536  1.2.4.2  ad #endif
    537  1.2.4.2  ad }
    538  1.2.4.2  ad 
    539  1.2.4.2  ad 
    540  1.2.4.2  ad #ifdef MULTIPROCESSOR
    541  1.2.4.2  ad void
    542  1.2.4.2  ad cpu_boot_secondary_processors()
    543  1.2.4.2  ad {
    544  1.2.4.2  ad 	struct cpu_info *ci;
    545  1.2.4.2  ad 	u_long i;
    546  1.2.4.2  ad 
    547  1.2.4.2  ad 	for (i=0; i < X86_MAXPROCS; i++) {
    548  1.2.4.2  ad 		ci = cpu_info[i];
    549  1.2.4.2  ad 		if (ci == NULL)
    550  1.2.4.2  ad 			continue;
    551  1.2.4.2  ad 		if (ci->ci_data.cpu_idlelwp == NULL)
    552  1.2.4.2  ad 			continue;
    553  1.2.4.2  ad 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    554  1.2.4.2  ad 			continue;
    555  1.2.4.2  ad 		if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
    556  1.2.4.2  ad 			continue;
    557  1.2.4.2  ad 		cpu_boot_secondary(ci);
    558  1.2.4.2  ad 	}
    559  1.2.4.2  ad }
    560  1.2.4.2  ad 
    561  1.2.4.2  ad static void
    562  1.2.4.2  ad cpu_init_idle_lwp(struct cpu_info *ci)
    563  1.2.4.2  ad {
    564  1.2.4.2  ad 	struct lwp *l = ci->ci_data.cpu_idlelwp;
    565  1.2.4.2  ad 	struct pcb *pcb = &l->l_addr->u_pcb;
    566  1.2.4.2  ad 
    567  1.2.4.2  ad 	pcb->pcb_cr0 = rcr0();
    568  1.2.4.2  ad }
    569  1.2.4.2  ad 
    570  1.2.4.2  ad void
    571  1.2.4.2  ad cpu_init_idle_lwps()
    572  1.2.4.2  ad {
    573  1.2.4.2  ad 	struct cpu_info *ci;
    574  1.2.4.2  ad 	u_long i;
    575  1.2.4.2  ad 
    576  1.2.4.2  ad 	for (i = 0; i < X86_MAXPROCS; i++) {
    577  1.2.4.2  ad 		ci = cpu_info[i];
    578  1.2.4.2  ad 		if (ci == NULL)
    579  1.2.4.2  ad 			continue;
    580  1.2.4.2  ad 		if (ci->ci_data.cpu_idlelwp == NULL)
    581  1.2.4.2  ad 			continue;
    582  1.2.4.2  ad 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    583  1.2.4.2  ad 			continue;
    584  1.2.4.2  ad 		cpu_init_idle_lwp(ci);
    585  1.2.4.2  ad 	}
    586  1.2.4.2  ad }
    587  1.2.4.2  ad 
    588  1.2.4.2  ad void
    589  1.2.4.2  ad cpu_start_secondary (ci)
    590  1.2.4.2  ad 	struct cpu_info *ci;
    591  1.2.4.2  ad {
    592  1.2.4.2  ad 	int i;
    593  1.2.4.2  ad 	struct pmap *kpm = pmap_kernel();
    594  1.2.4.2  ad 	extern u_int32_t mp_pdirpa;
    595  1.2.4.2  ad 
    596  1.2.4.2  ad 	mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */
    597  1.2.4.2  ad 
    598  1.2.4.2  ad 	ci->ci_flags |= CPUF_AP;
    599  1.2.4.2  ad 
    600  1.2.4.2  ad 	printf("%s: starting\n", ci->ci_dev->dv_xname);
    601  1.2.4.2  ad 
    602  1.2.4.2  ad 	ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
    603  1.2.4.2  ad 	CPU_STARTUP(ci);
    604  1.2.4.2  ad 
    605  1.2.4.2  ad 	/*
    606  1.2.4.2  ad 	 * wait for it to become ready
    607  1.2.4.2  ad 	 */
    608  1.2.4.2  ad 	for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i>0;i--) {
    609  1.2.4.2  ad 		delay(10);
    610  1.2.4.2  ad 	}
    611  1.2.4.2  ad 	if (! (ci->ci_flags & CPUF_PRESENT)) {
    612  1.2.4.2  ad 		printf("%s: failed to become ready\n", ci->ci_dev->dv_xname);
    613  1.2.4.2  ad #if defined(MPDEBUG) && defined(DDB)
    614  1.2.4.2  ad 		printf("dropping into debugger; continue from here to resume boot\n");
    615  1.2.4.2  ad 		Debugger();
    616  1.2.4.2  ad #endif
    617  1.2.4.2  ad 	}
    618  1.2.4.2  ad 
    619  1.2.4.2  ad 	CPU_START_CLEANUP(ci);
    620  1.2.4.2  ad }
    621  1.2.4.2  ad 
    622  1.2.4.2  ad void
    623  1.2.4.2  ad cpu_boot_secondary(ci)
    624  1.2.4.2  ad 	struct cpu_info *ci;
    625  1.2.4.2  ad {
    626  1.2.4.2  ad 	int i;
    627  1.2.4.2  ad 
    628  1.2.4.2  ad 	ci->ci_flags |= CPUF_GO; /* XXX atomic */
    629  1.2.4.2  ad 
    630  1.2.4.2  ad 	for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i>0;i--) {
    631  1.2.4.2  ad 		delay(10);
    632  1.2.4.2  ad 	}
    633  1.2.4.2  ad 	if (! (ci->ci_flags & CPUF_RUNNING)) {
    634  1.2.4.2  ad 		printf("CPU failed to start\n");
    635  1.2.4.2  ad #if defined(MPDEBUG) && defined(DDB)
    636  1.2.4.2  ad 		printf("dropping into debugger; continue from here to resume boot\n");
    637  1.2.4.2  ad 		Debugger();
    638  1.2.4.2  ad #endif
    639  1.2.4.2  ad 	}
    640  1.2.4.2  ad }
    641  1.2.4.2  ad 
    642  1.2.4.2  ad /*
    643  1.2.4.2  ad  * The CPU ends up here when its ready to run
    644  1.2.4.2  ad  * This is called from code in mptramp.s; at this point, we are running
    645  1.2.4.2  ad  * in the idle pcb/idle stack of the new CPU.  When this function returns,
    646  1.2.4.2  ad  * this processor will enter the idle loop and start looking for work.
    647  1.2.4.2  ad  *
    648  1.2.4.2  ad  * XXX should share some of this with init386 in machdep.c
    649  1.2.4.2  ad  */
    650  1.2.4.2  ad void
    651  1.2.4.2  ad cpu_hatch(void *v)
    652  1.2.4.2  ad {
    653  1.2.4.2  ad 	struct cpu_info *ci = (struct cpu_info *)v;
    654  1.2.4.2  ad 	int s;
    655  1.2.4.2  ad #ifdef __x86_64__
    656  1.2.4.2  ad         cpu_init_msrs(ci);
    657  1.2.4.2  ad #endif
    658  1.2.4.2  ad 
    659  1.2.4.2  ad 	cpu_probe_features(ci);
    660  1.2.4.2  ad 	cpu_feature &= ci->ci_feature_flags;
    661  1.2.4.2  ad 	/* not on Xen... */
    662  1.2.4.2  ad 	cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX);
    663  1.2.4.2  ad 
    664  1.2.4.2  ad #ifdef DEBUG
    665  1.2.4.2  ad 	if (ci->ci_flags & CPUF_PRESENT)
    666  1.2.4.2  ad 		panic("%s: already running!?", ci->ci_dev->dv_xname);
    667  1.2.4.2  ad #endif
    668  1.2.4.2  ad 
    669  1.2.4.2  ad 	ci->ci_flags |= CPUF_PRESENT;
    670  1.2.4.2  ad 
    671  1.2.4.2  ad 	lapic_enable();
    672  1.2.4.2  ad 	lapic_initclocks();
    673  1.2.4.2  ad 
    674  1.2.4.2  ad 	while ((ci->ci_flags & CPUF_GO) == 0)
    675  1.2.4.2  ad 		delay(10);
    676  1.2.4.2  ad #ifdef DEBUG
    677  1.2.4.2  ad 	if (ci->ci_flags & CPUF_RUNNING)
    678  1.2.4.2  ad 		panic("%s: already running!?", ci->ci_dev->dv_xname);
    679  1.2.4.2  ad #endif
    680  1.2.4.2  ad 
    681  1.2.4.2  ad 	lcr0(ci->ci_data.cpu_idlelwp->l_addr->u_pcb.pcb_cr0);
    682  1.2.4.2  ad 	cpu_init_idt();
    683  1.2.4.2  ad 	lapic_set_lvt();
    684  1.2.4.2  ad 	gdt_init_cpu(ci);
    685  1.2.4.2  ad 	npxinit(ci);
    686  1.2.4.2  ad 
    687  1.2.4.2  ad 	lldt(GSEL(GLDT_SEL, SEL_KPL));
    688  1.2.4.2  ad 
    689  1.2.4.2  ad 	cpu_init(ci);
    690  1.2.4.2  ad 
    691  1.2.4.2  ad 	s = splhigh();
    692  1.2.4.2  ad 	lapic_tpr = 0;
    693  1.2.4.2  ad 	enable_intr();
    694  1.2.4.2  ad 
    695  1.2.4.2  ad 	printf("%s: CPU %ld running\n",ci->ci_dev->dv_xname, ci->ci_cpuid);
    696  1.2.4.2  ad 	if (ci->ci_feature_flags & CPUID_TSC)
    697  1.2.4.2  ad 		cc_microset(ci);
    698  1.2.4.2  ad 	splx(s);
    699  1.2.4.2  ad }
    700  1.2.4.2  ad 
    701  1.2.4.2  ad #if defined(DDB)
    702  1.2.4.2  ad 
    703  1.2.4.2  ad #include <ddb/db_output.h>
    704  1.2.4.2  ad #include <machine/db_machdep.h>
    705  1.2.4.2  ad 
    706  1.2.4.2  ad /*
    707  1.2.4.2  ad  * Dump CPU information from ddb.
    708  1.2.4.2  ad  */
    709  1.2.4.2  ad void
    710  1.2.4.2  ad cpu_debug_dump(void)
    711  1.2.4.2  ad {
    712  1.2.4.2  ad 	struct cpu_info *ci;
    713  1.2.4.2  ad 	CPU_INFO_ITERATOR cii;
    714  1.2.4.2  ad 
    715  1.2.4.2  ad 	db_printf("addr		dev	id	flags	ipis	curproc		fpcurproc\n");
    716  1.2.4.2  ad 	for (CPU_INFO_FOREACH(cii, ci)) {
    717  1.2.4.2  ad 		db_printf("%p	%s	%ld	%x	%x	%10p	%10p\n",
    718  1.2.4.2  ad 		    ci,
    719  1.2.4.2  ad 		    ci->ci_dev == NULL ? "BOOT" : ci->ci_dev->dv_xname,
    720  1.2.4.2  ad 		    ci->ci_cpuid,
    721  1.2.4.2  ad 		    ci->ci_flags, ci->ci_ipis,
    722  1.2.4.2  ad 		    ci->ci_curlwp,
    723  1.2.4.2  ad 		    ci->ci_fpcurlwp);
    724  1.2.4.2  ad 	}
    725  1.2.4.2  ad }
    726  1.2.4.2  ad #endif
    727  1.2.4.2  ad 
    728  1.2.4.2  ad static void
    729  1.2.4.2  ad cpu_copy_trampoline()
    730  1.2.4.2  ad {
    731  1.2.4.2  ad 	/*
    732  1.2.4.2  ad 	 * Copy boot code.
    733  1.2.4.2  ad 	 */
    734  1.2.4.2  ad 	extern u_char cpu_spinup_trampoline[];
    735  1.2.4.2  ad 	extern u_char cpu_spinup_trampoline_end[];
    736  1.2.4.2  ad 	pmap_kenter_pa((vaddr_t)MP_TRAMPOLINE,	/* virtual */
    737  1.2.4.2  ad 	    (paddr_t)MP_TRAMPOLINE,	/* physical */
    738  1.2.4.2  ad 	    VM_PROT_ALL);		/* protection */
    739  1.2.4.2  ad 	memcpy((void *)MP_TRAMPOLINE,
    740  1.2.4.2  ad 	    cpu_spinup_trampoline,
    741  1.2.4.2  ad 	    cpu_spinup_trampoline_end-cpu_spinup_trampoline);
    742  1.2.4.2  ad }
    743  1.2.4.2  ad 
    744  1.2.4.2  ad #endif
    745  1.2.4.2  ad 
    746  1.2.4.2  ad 
    747  1.2.4.2  ad /* XXX */
    748  1.2.4.2  ad #define IDTVEC(name)	__CONCAT(X, name)
    749  1.2.4.2  ad typedef void (vector)(void);
    750  1.2.4.2  ad extern vector IDTVEC(tss_trap08);
    751  1.2.4.2  ad #ifdef DDB
    752  1.2.4.2  ad extern vector Xintrddbipi;
    753  1.2.4.2  ad extern int ddb_vec;
    754  1.2.4.2  ad #endif
    755  1.2.4.2  ad 
    756  1.2.4.2  ad static void
    757  1.2.4.2  ad cpu_set_tss_gates(struct cpu_info *ci)
    758  1.2.4.2  ad {
    759  1.2.4.2  ad #if defined(DDB) && defined(MULTIPROCESSOR)
    760  1.2.4.2  ad 	/*
    761  1.2.4.2  ad 	 * Set up separate handler for the DDB IPI, so that it doesn't
    762  1.2.4.2  ad 	 * stomp on a possibly corrupted stack.
    763  1.2.4.2  ad 	 *
    764  1.2.4.2  ad 	 * XXX overwriting the gate set in db_machine_init.
    765  1.2.4.2  ad 	 * Should rearrange the code so that it's set only once.
    766  1.2.4.2  ad 	 */
    767  1.2.4.2  ad 	ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
    768  1.2.4.2  ad 	    UVM_KMF_WIRED);
    769  1.2.4.2  ad 	cpu_init_tss(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
    770  1.2.4.2  ad 	    Xintrddbipi);
    771  1.2.4.2  ad 
    772  1.2.4.2  ad 	setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
    773  1.2.4.2  ad 	    SDT_SYS386TSS, SEL_KPL, 0, 0);
    774  1.2.4.2  ad 	ci->ci_gdt[GIPITSS_SEL].sd = sd;
    775  1.2.4.2  ad 
    776  1.2.4.2  ad 	setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
    777  1.2.4.2  ad 	    GSEL(GIPITSS_SEL, SEL_KPL));
    778  1.2.4.2  ad #endif
    779  1.2.4.2  ad }
    780  1.2.4.2  ad 
    781  1.2.4.2  ad int
    782  1.2.4.2  ad mp_cpu_start(struct cpu_info *ci)
    783  1.2.4.2  ad {
    784  1.2.4.2  ad #if 0
    785  1.2.4.2  ad #if NLAPIC > 0
    786  1.2.4.2  ad 	int error;
    787  1.2.4.2  ad #endif
    788  1.2.4.2  ad 	unsigned short dwordptr[2];
    789  1.2.4.2  ad 
    790  1.2.4.2  ad 	/*
    791  1.2.4.2  ad 	 * "The BSP must initialize CMOS shutdown code to 0Ah ..."
    792  1.2.4.2  ad 	 */
    793  1.2.4.2  ad 
    794  1.2.4.2  ad 	outb(IO_RTC, NVRAM_RESET);
    795  1.2.4.2  ad 	outb(IO_RTC+1, NVRAM_RESET_JUMP);
    796  1.2.4.2  ad 
    797  1.2.4.2  ad 	/*
    798  1.2.4.2  ad 	 * "and the warm reset vector (DWORD based at 40:67) to point
    799  1.2.4.2  ad 	 * to the AP startup code ..."
    800  1.2.4.2  ad 	 */
    801  1.2.4.2  ad 
    802  1.2.4.2  ad 	dwordptr[0] = 0;
    803  1.2.4.2  ad 	dwordptr[1] = MP_TRAMPOLINE >> 4;
    804  1.2.4.2  ad 
    805  1.2.4.2  ad 	pmap_kenter_pa (0, 0, VM_PROT_READ|VM_PROT_WRITE);
    806  1.2.4.2  ad 	memcpy ((u_int8_t *) 0x467, dwordptr, 4);
    807  1.2.4.2  ad 	pmap_kremove (0, PAGE_SIZE);
    808  1.2.4.2  ad 
    809  1.2.4.2  ad #if NLAPIC > 0
    810  1.2.4.2  ad 	/*
    811  1.2.4.2  ad 	 * ... prior to executing the following sequence:"
    812  1.2.4.2  ad 	 */
    813  1.2.4.2  ad 
    814  1.2.4.2  ad 	if (ci->ci_flags & CPUF_AP) {
    815  1.2.4.2  ad 		if ((error = x86_ipi_init(ci->ci_apicid)) != 0)
    816  1.2.4.2  ad 			return error;
    817  1.2.4.2  ad 
    818  1.2.4.2  ad 		delay(10000);
    819  1.2.4.2  ad 
    820  1.2.4.2  ad 		if (cpu_feature & CPUID_APIC) {
    821  1.2.4.2  ad 
    822  1.2.4.2  ad 			if ((error = x86_ipi(MP_TRAMPOLINE/PAGE_SIZE,
    823  1.2.4.2  ad 					     ci->ci_apicid,
    824  1.2.4.2  ad 					     LAPIC_DLMODE_STARTUP)) != 0)
    825  1.2.4.2  ad 				return error;
    826  1.2.4.2  ad 			delay(200);
    827  1.2.4.2  ad 
    828  1.2.4.2  ad 			if ((error = x86_ipi(MP_TRAMPOLINE/PAGE_SIZE,
    829  1.2.4.2  ad 					     ci->ci_apicid,
    830  1.2.4.2  ad 					     LAPIC_DLMODE_STARTUP)) != 0)
    831  1.2.4.2  ad 				return error;
    832  1.2.4.2  ad 			delay(200);
    833  1.2.4.2  ad 		}
    834  1.2.4.2  ad 	}
    835  1.2.4.2  ad #endif
    836  1.2.4.2  ad #endif /* 0 */
    837  1.2.4.2  ad 	return 0;
    838  1.2.4.2  ad }
    839  1.2.4.2  ad 
    840  1.2.4.2  ad void
    841  1.2.4.2  ad mp_cpu_start_cleanup(struct cpu_info *ci)
    842  1.2.4.2  ad {
    843  1.2.4.2  ad #if 0
    844  1.2.4.2  ad 	/*
    845  1.2.4.2  ad 	 * Ensure the NVRAM reset byte contains something vaguely sane.
    846  1.2.4.2  ad 	 */
    847  1.2.4.2  ad 
    848  1.2.4.2  ad 	outb(IO_RTC, NVRAM_RESET);
    849  1.2.4.2  ad 	outb(IO_RTC+1, NVRAM_RESET_RST);
    850  1.2.4.2  ad #endif
    851  1.2.4.2  ad }
    852  1.2.4.2  ad 
    853  1.2.4.2  ad #ifdef __x86_64__
    854  1.2.4.2  ad 
    855  1.2.4.2  ad void
    856  1.2.4.2  ad cpu_init_msrs(struct cpu_info *ci)
    857  1.2.4.2  ad {
    858  1.2.4.2  ad 	HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
    859  1.2.4.2  ad 	HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (u_int64_t) ci);
    860  1.2.4.2  ad 	HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
    861  1.2.4.2  ad }
    862  1.2.4.2  ad #endif	/* __x86_64__ */
    863  1.2.4.2  ad 
    864  1.2.4.2  ad void
    865  1.2.4.2  ad cpu_get_tsc_freq(struct cpu_info *ci)
    866  1.2.4.2  ad {
    867  1.2.4.2  ad #ifdef XEN3
    868  1.2.4.2  ad 	const volatile vcpu_time_info_t *tinfo =
    869  1.2.4.2  ad 		   &HYPERVISOR_shared_info->vcpu_info[0].time;
    870  1.2.4.2  ad 	delay(1000000);
    871  1.2.4.2  ad 	uint64_t freq = 1000000000ULL << 32;
    872  1.2.4.2  ad 	freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
    873  1.2.4.2  ad 	if ( tinfo->tsc_shift < 0 )
    874  1.2.4.2  ad 		freq = freq << -tinfo->tsc_shift;
    875  1.2.4.2  ad 	else
    876  1.2.4.2  ad 		freq = freq >> tinfo->tsc_shift;
    877  1.2.4.2  ad 	ci->ci_tsc_freq = freq;
    878  1.2.4.2  ad #else
    879  1.2.4.2  ad 	/* XXX this needs to read the shared_info of the CPU being probed.. */
    880  1.2.4.2  ad 	ci->ci_tsc_freq = HYPERVISOR_shared_info->cpu_freq;
    881  1.2.4.2  ad #endif /* XEN3 */
    882  1.2.4.2  ad }
    883  1.2.4.2  ad /*	$NetBSD: cpu.c,v 1.2.4.2 2007/12/03 19:04:39 ad Exp $	*/
    884  1.2.4.2  ad /* NetBSD: cpu.c,v 1.18 2004/02/20 17:35:01 yamt Exp  */
    885  1.2.4.2  ad 
    886  1.2.4.2  ad /*-
    887  1.2.4.2  ad  * Copyright (c) 2000 The NetBSD Foundation, Inc.
    888  1.2.4.2  ad  * All rights reserved.
    889  1.2.4.2  ad  *
    890  1.2.4.2  ad  * This code is derived from software contributed to The NetBSD Foundation
    891  1.2.4.2  ad  * by RedBack Networks Inc.
    892  1.2.4.2  ad  *
    893  1.2.4.2  ad  * Author: Bill Sommerfeld
    894  1.2.4.2  ad  *
    895  1.2.4.2  ad  * Redistribution and use in source and binary forms, with or without
    896  1.2.4.2  ad  * modification, are permitted provided that the following conditions
    897  1.2.4.2  ad  * are met:
    898  1.2.4.2  ad  * 1. Redistributions of source code must retain the above copyright
    899  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer.
    900  1.2.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
    901  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
    902  1.2.4.2  ad  *    documentation and/or other materials provided with the distribution.
    903  1.2.4.2  ad  * 3. All advertising materials mentioning features or use of this software
    904  1.2.4.2  ad  *    must display the following acknowledgement:
    905  1.2.4.2  ad  *        This product includes software developed by the NetBSD
    906  1.2.4.2  ad  *        Foundation, Inc. and its contributors.
    907  1.2.4.2  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
    908  1.2.4.2  ad  *    contributors may be used to endorse or promote products derived
    909  1.2.4.2  ad  *    from this software without specific prior written permission.
    910  1.2.4.2  ad  *
    911  1.2.4.2  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
    912  1.2.4.2  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    913  1.2.4.2  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    914  1.2.4.2  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
    915  1.2.4.2  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    916  1.2.4.2  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    917  1.2.4.2  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    918  1.2.4.2  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    919  1.2.4.2  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    920  1.2.4.2  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    921  1.2.4.2  ad  * POSSIBILITY OF SUCH DAMAGE.
    922  1.2.4.2  ad  */
    923  1.2.4.2  ad 
    924  1.2.4.2  ad /*
    925  1.2.4.2  ad  * Copyright (c) 1999 Stefan Grefen
    926  1.2.4.2  ad  *
    927  1.2.4.2  ad  * Redistribution and use in source and binary forms, with or without
    928  1.2.4.2  ad  * modification, are permitted provided that the following conditions
    929  1.2.4.2  ad  * are met:
    930  1.2.4.2  ad  * 1. Redistributions of source code must retain the above copyright
    931  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer.
    932  1.2.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
    933  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
    934  1.2.4.2  ad  *    documentation and/or other materials provided with the distribution.
    935  1.2.4.2  ad  * 3. All advertising materials mentioning features or use of this software
    936  1.2.4.2  ad  *    must display the following acknowledgement:
    937  1.2.4.2  ad  *      This product includes software developed by the NetBSD
    938  1.2.4.2  ad  *      Foundation, Inc. and its contributors.
    939  1.2.4.2  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
    940  1.2.4.2  ad  *    contributors may be used to endorse or promote products derived
    941  1.2.4.2  ad  *    from this software without specific prior written permission.
    942  1.2.4.2  ad  *
    943  1.2.4.2  ad  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
    944  1.2.4.2  ad  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    945  1.2.4.2  ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    946  1.2.4.2  ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
    947  1.2.4.2  ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    948  1.2.4.2  ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    949  1.2.4.2  ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    950  1.2.4.2  ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    951  1.2.4.2  ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    952  1.2.4.2  ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    953  1.2.4.2  ad  * SUCH DAMAGE.
    954  1.2.4.2  ad  */
    955  1.2.4.2  ad 
    956  1.2.4.2  ad #include <sys/cdefs.h>
    957  1.2.4.2  ad __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.2.4.2 2007/12/03 19:04:39 ad Exp $");
    958  1.2.4.2  ad 
    959  1.2.4.2  ad #include "opt_ddb.h"
    960  1.2.4.2  ad #include "opt_multiprocessor.h"
    961  1.2.4.2  ad #include "opt_mpbios.h"		/* for MPDEBUG */
    962  1.2.4.2  ad #include "opt_mtrr.h"
    963  1.2.4.2  ad #include "opt_xen.h"
    964  1.2.4.2  ad 
    965  1.2.4.2  ad #include "lapic.h"
    966  1.2.4.2  ad #include "ioapic.h"
    967  1.2.4.2  ad 
    968  1.2.4.2  ad #include <sys/param.h>
    969  1.2.4.2  ad #include <sys/proc.h>
    970  1.2.4.2  ad #include <sys/user.h>
    971  1.2.4.2  ad #include <sys/systm.h>
    972  1.2.4.2  ad #include <sys/device.h>
    973  1.2.4.2  ad #include <sys/malloc.h>
    974  1.2.4.2  ad 
    975  1.2.4.2  ad #include <uvm/uvm_extern.h>
    976  1.2.4.2  ad 
    977  1.2.4.2  ad #include <machine/cpu.h>
    978  1.2.4.2  ad #include <machine/cpufunc.h>
    979  1.2.4.2  ad #include <machine/cpuvar.h>
    980  1.2.4.2  ad #include <machine/pmap.h>
    981  1.2.4.2  ad #include <machine/vmparam.h>
    982  1.2.4.2  ad #include <machine/mpbiosvar.h>
    983  1.2.4.2  ad #include <machine/pcb.h>
    984  1.2.4.2  ad #include <machine/specialreg.h>
    985  1.2.4.2  ad #include <machine/segments.h>
    986  1.2.4.2  ad #include <machine/gdt.h>
    987  1.2.4.2  ad #include <machine/mtrr.h>
    988  1.2.4.2  ad #include <machine/pio.h>
    989  1.2.4.2  ad 
    990  1.2.4.2  ad #ifdef XEN3
    991  1.2.4.2  ad #include <xen/vcpuvar.h>
    992  1.2.4.2  ad #endif
    993  1.2.4.2  ad 
    994  1.2.4.2  ad #if NLAPIC > 0
    995  1.2.4.2  ad #include <machine/apicvar.h>
    996  1.2.4.2  ad #include <machine/i82489reg.h>
    997  1.2.4.2  ad #include <machine/i82489var.h>
    998  1.2.4.2  ad #endif
    999  1.2.4.2  ad 
   1000  1.2.4.2  ad #if NIOAPIC > 0
   1001  1.2.4.2  ad #include <machine/i82093var.h>
   1002  1.2.4.2  ad #endif
   1003  1.2.4.2  ad 
   1004  1.2.4.2  ad #include <dev/ic/mc146818reg.h>
   1005  1.2.4.2  ad #include <dev/isa/isareg.h>
   1006  1.2.4.2  ad 
   1007  1.2.4.2  ad int     cpu_match(struct device *, struct cfdata *, void *);
   1008  1.2.4.2  ad void    cpu_attach(struct device *, struct device *, void *);
   1009  1.2.4.2  ad #ifdef XEN3
   1010  1.2.4.2  ad int     vcpu_match(struct device *, struct cfdata *, void *);
   1011  1.2.4.2  ad void    vcpu_attach(struct device *, struct device *, void *);
   1012  1.2.4.2  ad #endif
   1013  1.2.4.2  ad void    cpu_attach_common(struct device *, struct device *, void *);
   1014  1.2.4.2  ad 
   1015  1.2.4.2  ad struct cpu_softc {
   1016  1.2.4.2  ad 	struct device sc_dev;		/* device tree glue */
   1017  1.2.4.2  ad 	struct cpu_info *sc_info;	/* pointer to CPU info */
   1018  1.2.4.2  ad };
   1019  1.2.4.2  ad 
   1020  1.2.4.2  ad int mp_cpu_start(struct cpu_info *);
   1021  1.2.4.2  ad void mp_cpu_start_cleanup(struct cpu_info *);
   1022  1.2.4.2  ad const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
   1023  1.2.4.2  ad 				      mp_cpu_start_cleanup };
   1024  1.2.4.2  ad 
   1025  1.2.4.2  ad CFATTACH_DECL(cpu, sizeof(struct cpu_softc),
   1026  1.2.4.2  ad     cpu_match, cpu_attach, NULL, NULL);
   1027  1.2.4.2  ad #ifdef XEN3
   1028  1.2.4.2  ad CFATTACH_DECL(vcpu, sizeof(struct cpu_softc),
   1029  1.2.4.2  ad     vcpu_match, vcpu_attach, NULL, NULL);
   1030  1.2.4.2  ad #endif
   1031  1.2.4.2  ad 
   1032  1.2.4.2  ad /*
   1033  1.2.4.2  ad  * Statically-allocated CPU info for the primary CPU (or the only
   1034  1.2.4.2  ad  * CPU, on uniprocessors).  The CPU info list is initialized to
   1035  1.2.4.2  ad  * point at it.
   1036  1.2.4.2  ad  */
   1037  1.2.4.2  ad #ifdef TRAPLOG
   1038  1.2.4.2  ad #include <machine/tlog.h>
   1039  1.2.4.2  ad struct tlog tlog_primary;
   1040  1.2.4.2  ad #endif
   1041  1.2.4.2  ad struct cpu_info cpu_info_primary = {
   1042  1.2.4.2  ad 	.ci_self = &cpu_info_primary,
   1043  1.2.4.2  ad #ifndef __x86_64__
   1044  1.2.4.2  ad 	.ci_self150 = (uint8_t *)&cpu_info_primary + 0x150,
   1045  1.2.4.2  ad #endif
   1046  1.2.4.2  ad 	.ci_curlwp = &lwp0,
   1047  1.2.4.2  ad #ifdef TRAPLOG
   1048  1.2.4.2  ad 	.ci_tlog = &tlog_primary,
   1049  1.2.4.2  ad #endif
   1050  1.2.4.2  ad 
   1051  1.2.4.2  ad };
   1052  1.2.4.2  ad struct cpu_info phycpu_info_primary = {
   1053  1.2.4.2  ad 	.ci_self = &phycpu_info_primary,
   1054  1.2.4.2  ad #ifndef __x86_64__
   1055  1.2.4.2  ad 	.ci_self150 = (uint8_t *)&phycpu_info_primary + 0x150,
   1056  1.2.4.2  ad #endif
   1057  1.2.4.2  ad };
   1058  1.2.4.2  ad 
   1059  1.2.4.2  ad struct cpu_info *cpu_info_list = &cpu_info_primary;
   1060  1.2.4.2  ad 
   1061  1.2.4.2  ad static void	cpu_set_tss_gates(struct cpu_info *ci);
   1062  1.2.4.2  ad 
   1063  1.2.4.2  ad u_int32_t cpus_attached = 0;
   1064  1.2.4.2  ad 
   1065  1.2.4.2  ad struct cpu_info *phycpu_info[X86_MAXPROCS] = { &cpu_info_primary };
   1066  1.2.4.2  ad 
   1067  1.2.4.2  ad #ifdef MULTIPROCESSOR
   1068  1.2.4.2  ad /*
   1069  1.2.4.2  ad  * Array of CPU info structures.  Must be statically-allocated because
   1070  1.2.4.2  ad  * curproc, etc. are used early.
   1071  1.2.4.2  ad  */
   1072  1.2.4.2  ad struct cpu_info *cpu_info[X86_MAXPROCS] = { &cpu_info_primary };
   1073  1.2.4.2  ad 
   1074  1.2.4.2  ad u_int32_t cpus_running = 0;
   1075  1.2.4.2  ad 
   1076  1.2.4.2  ad void    	cpu_hatch(void *);
   1077  1.2.4.2  ad static void    	cpu_boot_secondary(struct cpu_info *ci);
   1078  1.2.4.2  ad static void    	cpu_start_secondary(struct cpu_info *ci);
   1079  1.2.4.2  ad static void	cpu_copy_trampoline(void);
   1080  1.2.4.2  ad 
   1081  1.2.4.2  ad /*
   1082  1.2.4.2  ad  * Runs once per boot once multiprocessor goo has been detected and
   1083  1.2.4.2  ad  * the local APIC on the boot processor has been mapped.
   1084  1.2.4.2  ad  *
   1085  1.2.4.2  ad  * Called from lapic_boot_init() (from mpbios_scan()).
   1086  1.2.4.2  ad  */
   1087  1.2.4.2  ad void
   1088  1.2.4.2  ad cpu_init_first()
   1089  1.2.4.2  ad {
   1090  1.2.4.2  ad 	int cpunum = lapic_cpu_number();
   1091  1.2.4.2  ad 
   1092  1.2.4.2  ad 	if (cpunum != 0) {
   1093  1.2.4.2  ad 		cpu_info[0] = NULL;
   1094  1.2.4.2  ad 		cpu_info[cpunum] = &cpu_info_primary;
   1095  1.2.4.2  ad 	}
   1096  1.2.4.2  ad 
   1097  1.2.4.2  ad 	cpu_copy_trampoline();
   1098  1.2.4.2  ad }
   1099  1.2.4.2  ad #endif
   1100  1.2.4.2  ad 
   1101  1.2.4.2  ad int
   1102  1.2.4.2  ad cpu_match(parent, match, aux)
   1103  1.2.4.2  ad 	struct device *parent;
   1104  1.2.4.2  ad 	struct cfdata *match;
   1105  1.2.4.2  ad 	void *aux;
   1106  1.2.4.2  ad {
   1107  1.2.4.2  ad 
   1108  1.2.4.2  ad 	return 1;
   1109  1.2.4.2  ad }
   1110  1.2.4.2  ad 
   1111  1.2.4.2  ad void
   1112  1.2.4.2  ad cpu_attach(parent, self, aux)
   1113  1.2.4.2  ad 	struct device *parent, *self;
   1114  1.2.4.2  ad 	void *aux;
   1115  1.2.4.2  ad {
   1116  1.2.4.2  ad #ifdef XEN3
   1117  1.2.4.2  ad 	struct cpu_softc *sc = (void *) self;
   1118  1.2.4.2  ad 	struct cpu_attach_args *caa = aux;
   1119  1.2.4.2  ad 	struct cpu_info *ci;
   1120  1.2.4.2  ad 	int cpunum = caa->cpu_number;
   1121  1.2.4.2  ad 
   1122  1.2.4.2  ad 	/*
   1123  1.2.4.2  ad 	 * If we're an Application Processor, allocate a cpu_info
   1124  1.2.4.2  ad 	 * structure, otherwise use the primary's.
   1125  1.2.4.2  ad 	 */
   1126  1.2.4.2  ad 	if (caa->cpu_role == CPU_ROLE_AP) {
   1127  1.2.4.2  ad 		ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
   1128  1.2.4.2  ad 		if (phycpu_info[cpunum] != NULL)
   1129  1.2.4.2  ad 			panic("cpu at apic id %d already attached?", cpunum);
   1130  1.2.4.2  ad 		phycpu_info[cpunum] = ci;
   1131  1.2.4.2  ad 	} else {
   1132  1.2.4.2  ad 		ci = &phycpu_info_primary;
   1133  1.2.4.2  ad 		if (cpunum != 0) {
   1134  1.2.4.2  ad 			phycpu_info[0] = NULL;
   1135  1.2.4.2  ad 			phycpu_info[cpunum] = ci;
   1136  1.2.4.2  ad 		}
   1137  1.2.4.2  ad 	}
   1138  1.2.4.2  ad 
   1139  1.2.4.2  ad 	ci->ci_self = ci;
   1140  1.2.4.2  ad 	sc->sc_info = ci;
   1141  1.2.4.2  ad 
   1142  1.2.4.2  ad 	ci->ci_dev = self;
   1143  1.2.4.2  ad 	ci->ci_apicid = caa->cpu_number;
   1144  1.2.4.2  ad 	ci->ci_cpuid = ci->ci_apicid;
   1145  1.2.4.2  ad 
   1146  1.2.4.2  ad 	printf(": ");
   1147  1.2.4.2  ad 	switch (caa->cpu_role) {
   1148  1.2.4.2  ad 	case CPU_ROLE_SP:
   1149  1.2.4.2  ad 		printf("(uniprocessor)\n");
   1150  1.2.4.2  ad 		ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
   1151  1.2.4.2  ad 		break;
   1152  1.2.4.2  ad 
   1153  1.2.4.2  ad 	case CPU_ROLE_BP:
   1154  1.2.4.2  ad 		printf("(boot processor)\n");
   1155  1.2.4.2  ad 		ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
   1156  1.2.4.2  ad #if NIOAPIC > 0
   1157  1.2.4.2  ad 		ioapic_bsp_id = caa->cpu_number;
   1158  1.2.4.2  ad #endif
   1159  1.2.4.2  ad 		break;
   1160  1.2.4.2  ad 
   1161  1.2.4.2  ad 	case CPU_ROLE_AP:
   1162  1.2.4.2  ad 		/*
   1163  1.2.4.2  ad 		 * report on an AP
   1164  1.2.4.2  ad 		 */
   1165  1.2.4.2  ad 		printf("(application processor)\n");
   1166  1.2.4.2  ad 		break;
   1167  1.2.4.2  ad 
   1168  1.2.4.2  ad 	default:
   1169  1.2.4.2  ad 		panic("unknown processor type??\n");
   1170  1.2.4.2  ad 	}
   1171  1.2.4.2  ad 	return;
   1172  1.2.4.2  ad #else
   1173  1.2.4.2  ad 	cpu_attach_common(parent, self, aux);
   1174  1.2.4.2  ad #endif
   1175  1.2.4.2  ad }
   1176  1.2.4.2  ad 
   1177  1.2.4.2  ad #ifdef XEN3
   1178  1.2.4.2  ad int
   1179  1.2.4.2  ad vcpu_match(parent, match, aux)
   1180  1.2.4.2  ad 	struct device *parent;
   1181  1.2.4.2  ad 	struct cfdata *match;
   1182  1.2.4.2  ad 	void *aux;
   1183  1.2.4.2  ad {
   1184  1.2.4.2  ad 	struct vcpu_attach_args *vcaa = aux;
   1185  1.2.4.2  ad 
   1186  1.2.4.2  ad 	if (strcmp(vcaa->vcaa_name, match->cf_name) == 0)
   1187  1.2.4.2  ad 		return 1;
   1188  1.2.4.2  ad 	return 0;
   1189  1.2.4.2  ad }
   1190  1.2.4.2  ad 
   1191  1.2.4.2  ad void
   1192  1.2.4.2  ad vcpu_attach(parent, self, aux)
   1193  1.2.4.2  ad 	struct device *parent, *self;
   1194  1.2.4.2  ad 	void *aux;
   1195  1.2.4.2  ad {
   1196  1.2.4.2  ad 	struct vcpu_attach_args *vcaa = aux;
   1197  1.2.4.2  ad 
   1198  1.2.4.2  ad 	cpu_attach_common(parent, self, &vcaa->vcaa_caa);
   1199  1.2.4.2  ad }
   1200  1.2.4.2  ad #endif
   1201  1.2.4.2  ad 
   1202  1.2.4.2  ad static void
   1203  1.2.4.2  ad cpu_vm_init(struct cpu_info *ci)
   1204  1.2.4.2  ad {
   1205  1.2.4.2  ad 	int ncolors = 2, i;
   1206  1.2.4.2  ad 
   1207  1.2.4.2  ad 	for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
   1208  1.2.4.2  ad 		struct x86_cache_info *cai;
   1209  1.2.4.2  ad 		int tcolors;
   1210  1.2.4.2  ad 
   1211  1.2.4.2  ad 		cai = &ci->ci_cinfo[i];
   1212  1.2.4.2  ad 
   1213  1.2.4.2  ad 		tcolors = atop(cai->cai_totalsize);
   1214  1.2.4.2  ad 		switch(cai->cai_associativity) {
   1215  1.2.4.2  ad 		case 0xff:
   1216  1.2.4.2  ad 			tcolors = 1; /* fully associative */
   1217  1.2.4.2  ad 			break;
   1218  1.2.4.2  ad 		case 0:
   1219  1.2.4.2  ad 		case 1:
   1220  1.2.4.2  ad 			break;
   1221  1.2.4.2  ad 		default:
   1222  1.2.4.2  ad 			tcolors /= cai->cai_associativity;
   1223  1.2.4.2  ad 		}
   1224  1.2.4.2  ad 		ncolors = max(ncolors, tcolors);
   1225  1.2.4.2  ad 	}
   1226  1.2.4.2  ad 
   1227  1.2.4.2  ad 	/*
   1228  1.2.4.2  ad 	 * Knowing the size of the largest cache on this CPU, re-color
   1229  1.2.4.2  ad 	 * our pages.
   1230  1.2.4.2  ad 	 */
   1231  1.2.4.2  ad 	if (ncolors <= uvmexp.ncolors)
   1232  1.2.4.2  ad 		return;
   1233  1.2.4.2  ad 	printf("%s: %d page colors\n", ci->ci_dev->dv_xname, ncolors);
   1234  1.2.4.2  ad 	uvm_page_recolor(ncolors);
   1235  1.2.4.2  ad }
   1236  1.2.4.2  ad 
   1237  1.2.4.2  ad void
   1238  1.2.4.2  ad cpu_attach_common(parent, self, aux)
   1239  1.2.4.2  ad 	struct device *parent, *self;
   1240  1.2.4.2  ad 	void *aux;
   1241  1.2.4.2  ad {
   1242  1.2.4.2  ad 	struct cpu_softc *sc = (void *) self;
   1243  1.2.4.2  ad 	struct cpu_attach_args *caa = aux;
   1244  1.2.4.2  ad 	struct cpu_info *ci;
   1245  1.2.4.2  ad #if defined(MULTIPROCESSOR)
   1246  1.2.4.2  ad 	int cpunum = caa->cpu_number;
   1247  1.2.4.2  ad #endif
   1248  1.2.4.2  ad 
   1249  1.2.4.2  ad 	/*
   1250  1.2.4.2  ad 	 * If we're an Application Processor, allocate a cpu_info
   1251  1.2.4.2  ad 	 * structure, otherwise use the primary's.
   1252  1.2.4.2  ad 	 */
   1253  1.2.4.2  ad 	if (caa->cpu_role == CPU_ROLE_AP) {
   1254  1.2.4.2  ad 		ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
   1255  1.2.4.2  ad #if defined(MULTIPROCESSOR)
   1256  1.2.4.2  ad 		if (cpu_info[cpunum] != NULL)
   1257  1.2.4.2  ad 			panic("cpu at apic id %d already attached?", cpunum);
   1258  1.2.4.2  ad 		cpu_info[cpunum] = ci;
   1259  1.2.4.2  ad #endif
   1260  1.2.4.2  ad #ifdef TRAPLOG
   1261  1.2.4.2  ad 		ci->ci_tlog_base = malloc(sizeof(struct tlog),
   1262  1.2.4.2  ad 		    M_DEVBUF, M_WAITOK);
   1263  1.2.4.2  ad #endif
   1264  1.2.4.2  ad 	} else {
   1265  1.2.4.2  ad 		ci = &cpu_info_primary;
   1266  1.2.4.2  ad #if defined(MULTIPROCESSOR)
   1267  1.2.4.2  ad 		if (cpunum != lapic_cpu_number()) {
   1268  1.2.4.2  ad 			panic("%s: running CPU is at apic %d"
   1269  1.2.4.2  ad 			    " instead of at expected %d",
   1270  1.2.4.2  ad 			    sc->sc_dev.dv_xname, lapic_cpu_number(), cpunum);
   1271  1.2.4.2  ad 		}
   1272  1.2.4.2  ad #endif
   1273  1.2.4.2  ad 	}
   1274  1.2.4.2  ad 
   1275  1.2.4.2  ad 	ci->ci_self = ci;
   1276  1.2.4.2  ad 	sc->sc_info = ci;
   1277  1.2.4.2  ad 
   1278  1.2.4.2  ad 	ci->ci_dev = self;
   1279  1.2.4.2  ad 	ci->ci_apicid = caa->cpu_number;
   1280  1.2.4.2  ad #ifdef MULTIPROCESSOR
   1281  1.2.4.2  ad 	ci->ci_cpuid = ci->ci_apicid;
   1282  1.2.4.2  ad #else
   1283  1.2.4.2  ad 	ci->ci_cpuid = 0;	/* False for APs, but they're not used anyway */
   1284  1.2.4.2  ad #endif
   1285  1.2.4.2  ad 	ci->ci_cpumask = (1 << ci->ci_cpuid);
   1286  1.2.4.2  ad 	ci->ci_func = caa->cpu_func;
   1287  1.2.4.2  ad 
   1288  1.2.4.2  ad #ifndef __x86_64__
   1289  1.2.4.2  ad 	simple_lock_init(&ci->ci_slock);
   1290  1.2.4.2  ad #endif
   1291  1.2.4.2  ad 
   1292  1.2.4.2  ad 	if (caa->cpu_role == CPU_ROLE_AP) {
   1293  1.2.4.2  ad #if defined(MULTIPROCESSOR)
   1294  1.2.4.2  ad 		int error;
   1295  1.2.4.2  ad 
   1296  1.2.4.2  ad 		error = mi_cpu_attach(ci);
   1297  1.2.4.2  ad 		if (error != 0) {
   1298  1.2.4.2  ad 			aprint_normal("\n");
   1299  1.2.4.2  ad 			aprint_error("%s: mi_cpu_attach failed with %d\n",
   1300  1.2.4.2  ad 			    sc->sc_dev.dv_xname, error);
   1301  1.2.4.2  ad 			return;
   1302  1.2.4.2  ad 		}
   1303  1.2.4.2  ad #endif
   1304  1.2.4.2  ad 	} else {
   1305  1.2.4.2  ad 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
   1306  1.2.4.2  ad 	}
   1307  1.2.4.2  ad 
   1308  1.2.4.2  ad 	pmap_reference(pmap_kernel());
   1309  1.2.4.2  ad 	ci->ci_pmap = pmap_kernel();
   1310  1.2.4.2  ad 	ci->ci_tlbstate = TLBSTATE_STALE;
   1311  1.2.4.2  ad 
   1312  1.2.4.2  ad 	/* further PCB init done later. */
   1313  1.2.4.2  ad 
   1314  1.2.4.2  ad 	printf(": ");
   1315  1.2.4.2  ad 
   1316  1.2.4.2  ad 	switch (caa->cpu_role) {
   1317  1.2.4.2  ad 	case CPU_ROLE_SP:
   1318  1.2.4.2  ad 		printf("(uniprocessor)\n");
   1319  1.2.4.2  ad 		ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
   1320  1.2.4.2  ad 		cpu_intr_init(ci);
   1321  1.2.4.2  ad 		identifycpu(ci);
   1322  1.2.4.2  ad 		cpu_init(ci);
   1323  1.2.4.2  ad 		cpu_set_tss_gates(ci);
   1324  1.2.4.2  ad 		break;
   1325  1.2.4.2  ad 
   1326  1.2.4.2  ad 	case CPU_ROLE_BP:
   1327  1.2.4.2  ad 		printf("apid %d (boot processor)\n", caa->cpu_number);
   1328  1.2.4.2  ad 		ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
   1329  1.2.4.2  ad 		cpu_intr_init(ci);
   1330  1.2.4.2  ad 		identifycpu(ci);
   1331  1.2.4.2  ad 		cpu_init(ci);
   1332  1.2.4.2  ad 		cpu_set_tss_gates(ci);
   1333  1.2.4.2  ad 		break;
   1334  1.2.4.2  ad 
   1335  1.2.4.2  ad 	case CPU_ROLE_AP:
   1336  1.2.4.2  ad 		/*
   1337  1.2.4.2  ad 		 * report on an AP
   1338  1.2.4.2  ad 		 */
   1339  1.2.4.2  ad 		printf("apid %d (application processor)\n", caa->cpu_number);
   1340  1.2.4.2  ad 
   1341  1.2.4.2  ad #if defined(MULTIPROCESSOR)
   1342  1.2.4.2  ad 		cpu_intr_init(ci);
   1343  1.2.4.2  ad 		gdt_alloc_cpu(ci);
   1344  1.2.4.2  ad 		cpu_set_tss_gates(ci);
   1345  1.2.4.2  ad 		cpu_start_secondary(ci);
   1346  1.2.4.2  ad 		if (ci->ci_flags & CPUF_PRESENT) {
   1347  1.2.4.2  ad 			identifycpu(ci);
   1348  1.2.4.2  ad 			ci->ci_next = cpu_info_list->ci_next;
   1349  1.2.4.2  ad 			cpu_info_list->ci_next = ci;
   1350  1.2.4.2  ad 		}
   1351  1.2.4.2  ad #else
   1352  1.2.4.2  ad 		printf("%s: not started\n", sc->sc_dev.dv_xname);
   1353  1.2.4.2  ad #endif
   1354  1.2.4.2  ad 		break;
   1355  1.2.4.2  ad 
   1356  1.2.4.2  ad 	default:
   1357  1.2.4.2  ad 		panic("unknown processor type??\n");
   1358  1.2.4.2  ad 	}
   1359  1.2.4.2  ad 	cpu_vm_init(ci);
   1360  1.2.4.2  ad 
   1361  1.2.4.2  ad 	cpus_attached |= (1 << ci->ci_cpuid);
   1362  1.2.4.2  ad 
   1363  1.2.4.2  ad #if defined(MULTIPROCESSOR)
   1364  1.2.4.2  ad 	if (mp_verbose) {
   1365  1.2.4.2  ad 		struct lwp *l = ci->ci_data.cpu_idlelwp;
   1366  1.2.4.2  ad 
   1367  1.2.4.2  ad 		aprint_verbose("%s: idle lwp at %p, idle sp at 0x%x\n",
   1368  1.2.4.2  ad 		    sc->sc_dev.dv_xname, l, l->l_addr->u_pcb.pcb_esp);
   1369  1.2.4.2  ad 	}
   1370  1.2.4.2  ad #endif
   1371  1.2.4.2  ad }
   1372  1.2.4.2  ad 
   1373  1.2.4.2  ad /*
   1374  1.2.4.2  ad  * Initialize the processor appropriately.
   1375  1.2.4.2  ad  */
   1376  1.2.4.2  ad 
   1377  1.2.4.2  ad void
   1378  1.2.4.2  ad cpu_init(ci)
   1379  1.2.4.2  ad 	struct cpu_info *ci;
   1380  1.2.4.2  ad {
   1381  1.2.4.2  ad 	/* configure the CPU if needed */
   1382  1.2.4.2  ad 	if (ci->cpu_setup != NULL)
   1383  1.2.4.2  ad 		(*ci->cpu_setup)(ci);
   1384  1.2.4.2  ad 
   1385  1.2.4.2  ad 	/*
   1386  1.2.4.2  ad 	 * On a P6 or above, enable global TLB caching if the
   1387  1.2.4.2  ad 	 * hardware supports it.
   1388  1.2.4.2  ad 	 */
   1389  1.2.4.2  ad 	if (cpu_feature & CPUID_PGE)
   1390  1.2.4.2  ad 		lcr4(rcr4() | CR4_PGE);	/* enable global TLB caching */
   1391  1.2.4.2  ad 
   1392  1.2.4.2  ad #ifdef XXXMTRR
   1393  1.2.4.2  ad 	/*
   1394  1.2.4.2  ad 	 * On a P6 or above, initialize MTRR's if the hardware supports them.
   1395  1.2.4.2  ad 	 */
   1396  1.2.4.2  ad 	if (cpu_feature & CPUID_MTRR) {
   1397  1.2.4.2  ad 		if ((ci->ci_flags & CPUF_AP) == 0)
   1398  1.2.4.2  ad 			i686_mtrr_init_first();
   1399  1.2.4.2  ad 		mtrr_init_cpu(ci);
   1400  1.2.4.2  ad 	}
   1401  1.2.4.2  ad #endif
   1402  1.2.4.2  ad 	/*
   1403  1.2.4.2  ad 	 * If we have FXSAVE/FXRESTOR, use them.
   1404  1.2.4.2  ad 	 */
   1405  1.2.4.2  ad 	if (cpu_feature & CPUID_FXSR) {
   1406  1.2.4.2  ad 		lcr4(rcr4() | CR4_OSFXSR);
   1407  1.2.4.2  ad 
   1408  1.2.4.2  ad 		/*
   1409  1.2.4.2  ad 		 * If we have SSE/SSE2, enable XMM exceptions.
   1410  1.2.4.2  ad 		 */
   1411  1.2.4.2  ad 		if (cpu_feature & (CPUID_SSE|CPUID_SSE2))
   1412  1.2.4.2  ad 			lcr4(rcr4() | CR4_OSXMMEXCPT);
   1413  1.2.4.2  ad 	}
   1414  1.2.4.2  ad 
   1415  1.2.4.2  ad #ifdef MULTIPROCESSOR
   1416  1.2.4.2  ad 	ci->ci_flags |= CPUF_RUNNING;
   1417  1.2.4.2  ad 	cpus_running |= 1 << ci->ci_cpuid;
   1418  1.2.4.2  ad #endif
   1419  1.2.4.2  ad }
   1420  1.2.4.2  ad 
   1421  1.2.4.2  ad 
   1422  1.2.4.2  ad #ifdef MULTIPROCESSOR
   1423  1.2.4.2  ad void
   1424  1.2.4.2  ad cpu_boot_secondary_processors()
   1425  1.2.4.2  ad {
   1426  1.2.4.2  ad 	struct cpu_info *ci;
   1427  1.2.4.2  ad 	u_long i;
   1428  1.2.4.2  ad 
   1429  1.2.4.2  ad 	for (i=0; i < X86_MAXPROCS; i++) {
   1430  1.2.4.2  ad 		ci = cpu_info[i];
   1431  1.2.4.2  ad 		if (ci == NULL)
   1432  1.2.4.2  ad 			continue;
   1433  1.2.4.2  ad 		if (ci->ci_data.cpu_idlelwp == NULL)
   1434  1.2.4.2  ad 			continue;
   1435  1.2.4.2  ad 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
   1436  1.2.4.2  ad 			continue;
   1437  1.2.4.2  ad 		if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
   1438  1.2.4.2  ad 			continue;
   1439  1.2.4.2  ad 		cpu_boot_secondary(ci);
   1440  1.2.4.2  ad 	}
   1441  1.2.4.2  ad }
   1442  1.2.4.2  ad 
   1443  1.2.4.2  ad static void
   1444  1.2.4.2  ad cpu_init_idle_lwp(struct cpu_info *ci)
   1445  1.2.4.2  ad {
   1446  1.2.4.2  ad 	struct lwp *l = ci->ci_data.cpu_idlelwp;
   1447  1.2.4.2  ad 	struct pcb *pcb = &l->l_addr->u_pcb;
   1448  1.2.4.2  ad 
   1449  1.2.4.2  ad 	pcb->pcb_cr0 = rcr0();
   1450  1.2.4.2  ad }
   1451  1.2.4.2  ad 
   1452  1.2.4.2  ad void
   1453  1.2.4.2  ad cpu_init_idle_lwps()
   1454  1.2.4.2  ad {
   1455  1.2.4.2  ad 	struct cpu_info *ci;
   1456  1.2.4.2  ad 	u_long i;
   1457  1.2.4.2  ad 
   1458  1.2.4.2  ad 	for (i = 0; i < X86_MAXPROCS; i++) {
   1459  1.2.4.2  ad 		ci = cpu_info[i];
   1460  1.2.4.2  ad 		if (ci == NULL)
   1461  1.2.4.2  ad 			continue;
   1462  1.2.4.2  ad 		if (ci->ci_data.cpu_idlelwp == NULL)
   1463  1.2.4.2  ad 			continue;
   1464  1.2.4.2  ad 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
   1465  1.2.4.2  ad 			continue;
   1466  1.2.4.2  ad 		cpu_init_idle_lwp(ci);
   1467  1.2.4.2  ad 	}
   1468  1.2.4.2  ad }
   1469  1.2.4.2  ad 
   1470  1.2.4.2  ad void
   1471  1.2.4.2  ad cpu_start_secondary (ci)
   1472  1.2.4.2  ad 	struct cpu_info *ci;
   1473  1.2.4.2  ad {
   1474  1.2.4.2  ad 	int i;
   1475  1.2.4.2  ad 	struct pmap *kpm = pmap_kernel();
   1476  1.2.4.2  ad 	extern u_int32_t mp_pdirpa;
   1477  1.2.4.2  ad 
   1478  1.2.4.2  ad 	mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */
   1479  1.2.4.2  ad 
   1480  1.2.4.2  ad 	ci->ci_flags |= CPUF_AP;
   1481  1.2.4.2  ad 
   1482  1.2.4.2  ad 	printf("%s: starting\n", ci->ci_dev->dv_xname);
   1483  1.2.4.2  ad 
   1484  1.2.4.2  ad 	ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
   1485  1.2.4.2  ad 	CPU_STARTUP(ci);
   1486  1.2.4.2  ad 
   1487  1.2.4.2  ad 	/*
   1488  1.2.4.2  ad 	 * wait for it to become ready
   1489  1.2.4.2  ad 	 */
   1490  1.2.4.2  ad 	for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i>0;i--) {
   1491  1.2.4.2  ad 		delay(10);
   1492  1.2.4.2  ad 	}
   1493  1.2.4.2  ad 	if (! (ci->ci_flags & CPUF_PRESENT)) {
   1494  1.2.4.2  ad 		printf("%s: failed to become ready\n", ci->ci_dev->dv_xname);
   1495  1.2.4.2  ad #if defined(MPDEBUG) && defined(DDB)
   1496  1.2.4.2  ad 		printf("dropping into debugger; continue from here to resume boot\n");
   1497  1.2.4.2  ad 		Debugger();
   1498  1.2.4.2  ad #endif
   1499  1.2.4.2  ad 	}
   1500  1.2.4.2  ad 
   1501  1.2.4.2  ad 	CPU_START_CLEANUP(ci);
   1502  1.2.4.2  ad }
   1503  1.2.4.2  ad 
   1504  1.2.4.2  ad void
   1505  1.2.4.2  ad cpu_boot_secondary(ci)
   1506  1.2.4.2  ad 	struct cpu_info *ci;
   1507  1.2.4.2  ad {
   1508  1.2.4.2  ad 	int i;
   1509  1.2.4.2  ad 
   1510  1.2.4.2  ad 	ci->ci_flags |= CPUF_GO; /* XXX atomic */
   1511  1.2.4.2  ad 
   1512  1.2.4.2  ad 	for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i>0;i--) {
   1513  1.2.4.2  ad 		delay(10);
   1514  1.2.4.2  ad 	}
   1515  1.2.4.2  ad 	if (! (ci->ci_flags & CPUF_RUNNING)) {
   1516  1.2.4.2  ad 		printf("CPU failed to start\n");
   1517  1.2.4.2  ad #if defined(MPDEBUG) && defined(DDB)
   1518  1.2.4.2  ad 		printf("dropping into debugger; continue from here to resume boot\n");
   1519  1.2.4.2  ad 		Debugger();
   1520  1.2.4.2  ad #endif
   1521  1.2.4.2  ad 	}
   1522  1.2.4.2  ad }
   1523  1.2.4.2  ad 
   1524  1.2.4.2  ad /*
   1525  1.2.4.2  ad  * The CPU ends up here when its ready to run
   1526  1.2.4.2  ad  * This is called from code in mptramp.s; at this point, we are running
   1527  1.2.4.2  ad  * in the idle pcb/idle stack of the new CPU.  When this function returns,
   1528  1.2.4.2  ad  * this processor will enter the idle loop and start looking for work.
   1529  1.2.4.2  ad  *
   1530  1.2.4.2  ad  * XXX should share some of this with init386 in machdep.c
   1531  1.2.4.2  ad  */
   1532  1.2.4.2  ad void
   1533  1.2.4.2  ad cpu_hatch(void *v)
   1534  1.2.4.2  ad {
   1535  1.2.4.2  ad 	struct cpu_info *ci = (struct cpu_info *)v;
   1536  1.2.4.2  ad 	int s;
   1537  1.2.4.2  ad #ifdef __x86_64__
   1538  1.2.4.2  ad         cpu_init_msrs(ci);
   1539  1.2.4.2  ad #endif
   1540  1.2.4.2  ad 
   1541  1.2.4.2  ad 	cpu_probe_features(ci);
   1542  1.2.4.2  ad 	cpu_feature &= ci->ci_feature_flags;
   1543  1.2.4.2  ad 	/* not on Xen... */
   1544  1.2.4.2  ad 	cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX);
   1545  1.2.4.2  ad 
   1546  1.2.4.2  ad #ifdef DEBUG
   1547  1.2.4.2  ad 	if (ci->ci_flags & CPUF_PRESENT)
   1548  1.2.4.2  ad 		panic("%s: already running!?", ci->ci_dev->dv_xname);
   1549  1.2.4.2  ad #endif
   1550  1.2.4.2  ad 
   1551  1.2.4.2  ad 	ci->ci_flags |= CPUF_PRESENT;
   1552  1.2.4.2  ad 
   1553  1.2.4.2  ad 	lapic_enable();
   1554  1.2.4.2  ad 	lapic_initclocks();
   1555  1.2.4.2  ad 
   1556  1.2.4.2  ad 	while ((ci->ci_flags & CPUF_GO) == 0)
   1557  1.2.4.2  ad 		delay(10);
   1558  1.2.4.2  ad #ifdef DEBUG
   1559  1.2.4.2  ad 	if (ci->ci_flags & CPUF_RUNNING)
   1560  1.2.4.2  ad 		panic("%s: already running!?", ci->ci_dev->dv_xname);
   1561  1.2.4.2  ad #endif
   1562  1.2.4.2  ad 
   1563  1.2.4.2  ad 	lcr0(ci->ci_data.cpu_idlelwp->l_addr->u_pcb.pcb_cr0);
   1564  1.2.4.2  ad 	cpu_init_idt();
   1565  1.2.4.2  ad 	lapic_set_lvt();
   1566  1.2.4.2  ad 	gdt_init_cpu(ci);
   1567  1.2.4.2  ad 	npxinit(ci);
   1568  1.2.4.2  ad 
   1569  1.2.4.2  ad 	lldt(GSEL(GLDT_SEL, SEL_KPL));
   1570  1.2.4.2  ad 
   1571  1.2.4.2  ad 	cpu_init(ci);
   1572  1.2.4.2  ad 
   1573  1.2.4.2  ad 	s = splhigh();
   1574  1.2.4.2  ad 	lapic_tpr = 0;
   1575  1.2.4.2  ad 	enable_intr();
   1576  1.2.4.2  ad 
   1577  1.2.4.2  ad 	printf("%s: CPU %ld running\n",ci->ci_dev->dv_xname, ci->ci_cpuid);
   1578  1.2.4.2  ad 	if (ci->ci_feature_flags & CPUID_TSC)
   1579  1.2.4.2  ad 		cc_microset(ci);
   1580  1.2.4.2  ad 	splx(s);
   1581  1.2.4.2  ad }
   1582  1.2.4.2  ad 
   1583  1.2.4.2  ad #if defined(DDB)
   1584  1.2.4.2  ad 
   1585  1.2.4.2  ad #include <ddb/db_output.h>
   1586  1.2.4.2  ad #include <machine/db_machdep.h>
   1587  1.2.4.2  ad 
   1588  1.2.4.2  ad /*
   1589  1.2.4.2  ad  * Dump CPU information from ddb.
   1590  1.2.4.2  ad  */
   1591  1.2.4.2  ad void
   1592  1.2.4.2  ad cpu_debug_dump(void)
   1593  1.2.4.2  ad {
   1594  1.2.4.2  ad 	struct cpu_info *ci;
   1595  1.2.4.2  ad 	CPU_INFO_ITERATOR cii;
   1596  1.2.4.2  ad 
   1597  1.2.4.2  ad 	db_printf("addr		dev	id	flags	ipis	curproc		fpcurproc\n");
   1598  1.2.4.2  ad 	for (CPU_INFO_FOREACH(cii, ci)) {
   1599  1.2.4.2  ad 		db_printf("%p	%s	%ld	%x	%x	%10p	%10p\n",
   1600  1.2.4.2  ad 		    ci,
   1601  1.2.4.2  ad 		    ci->ci_dev == NULL ? "BOOT" : ci->ci_dev->dv_xname,
   1602  1.2.4.2  ad 		    ci->ci_cpuid,
   1603  1.2.4.2  ad 		    ci->ci_flags, ci->ci_ipis,
   1604  1.2.4.2  ad 		    ci->ci_curlwp,
   1605  1.2.4.2  ad 		    ci->ci_fpcurlwp);
   1606  1.2.4.2  ad 	}
   1607  1.2.4.2  ad }
   1608  1.2.4.2  ad #endif
   1609  1.2.4.2  ad 
   1610  1.2.4.2  ad static void
   1611  1.2.4.2  ad cpu_copy_trampoline()
   1612  1.2.4.2  ad {
   1613  1.2.4.2  ad 	/*
   1614  1.2.4.2  ad 	 * Copy boot code.
   1615  1.2.4.2  ad 	 */
   1616  1.2.4.2  ad 	extern u_char cpu_spinup_trampoline[];
   1617  1.2.4.2  ad 	extern u_char cpu_spinup_trampoline_end[];
   1618  1.2.4.2  ad 	pmap_kenter_pa((vaddr_t)MP_TRAMPOLINE,	/* virtual */
   1619  1.2.4.2  ad 	    (paddr_t)MP_TRAMPOLINE,	/* physical */
   1620  1.2.4.2  ad 	    VM_PROT_ALL);		/* protection */
   1621  1.2.4.2  ad 	memcpy((void *)MP_TRAMPOLINE,
   1622  1.2.4.2  ad 	    cpu_spinup_trampoline,
   1623  1.2.4.2  ad 	    cpu_spinup_trampoline_end-cpu_spinup_trampoline);
   1624  1.2.4.2  ad }
   1625  1.2.4.2  ad 
   1626  1.2.4.2  ad #endif
   1627  1.2.4.2  ad 
   1628  1.2.4.2  ad 
   1629  1.2.4.2  ad /* XXX */
   1630  1.2.4.2  ad #define IDTVEC(name)	__CONCAT(X, name)
   1631  1.2.4.2  ad typedef void (vector)(void);
   1632  1.2.4.2  ad extern vector IDTVEC(tss_trap08);
   1633  1.2.4.2  ad #ifdef DDB
   1634  1.2.4.2  ad extern vector Xintrddbipi;
   1635  1.2.4.2  ad extern int ddb_vec;
   1636  1.2.4.2  ad #endif
   1637  1.2.4.2  ad 
   1638  1.2.4.2  ad static void
   1639  1.2.4.2  ad cpu_set_tss_gates(struct cpu_info *ci)
   1640  1.2.4.2  ad {
   1641  1.2.4.2  ad #if defined(DDB) && defined(MULTIPROCESSOR)
   1642  1.2.4.2  ad 	/*
   1643  1.2.4.2  ad 	 * Set up separate handler for the DDB IPI, so that it doesn't
   1644  1.2.4.2  ad 	 * stomp on a possibly corrupted stack.
   1645  1.2.4.2  ad 	 *
   1646  1.2.4.2  ad 	 * XXX overwriting the gate set in db_machine_init.
   1647  1.2.4.2  ad 	 * Should rearrange the code so that it's set only once.
   1648  1.2.4.2  ad 	 */
   1649  1.2.4.2  ad 	ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
   1650  1.2.4.2  ad 	    UVM_KMF_WIRED);
   1651  1.2.4.2  ad 	cpu_init_tss(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
   1652  1.2.4.2  ad 	    Xintrddbipi);
   1653  1.2.4.2  ad 
   1654  1.2.4.2  ad 	setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
   1655  1.2.4.2  ad 	    SDT_SYS386TSS, SEL_KPL, 0, 0);
   1656  1.2.4.2  ad 	ci->ci_gdt[GIPITSS_SEL].sd = sd;
   1657  1.2.4.2  ad 
   1658  1.2.4.2  ad 	setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
   1659  1.2.4.2  ad 	    GSEL(GIPITSS_SEL, SEL_KPL));
   1660  1.2.4.2  ad #endif
   1661  1.2.4.2  ad }
   1662  1.2.4.2  ad 
   1663  1.2.4.2  ad int
   1664  1.2.4.2  ad mp_cpu_start(struct cpu_info *ci)
   1665  1.2.4.2  ad {
   1666  1.2.4.2  ad #if 0
   1667  1.2.4.2  ad #if NLAPIC > 0
   1668  1.2.4.2  ad 	int error;
   1669  1.2.4.2  ad #endif
   1670  1.2.4.2  ad 	unsigned short dwordptr[2];
   1671  1.2.4.2  ad 
   1672  1.2.4.2  ad 	/*
   1673  1.2.4.2  ad 	 * "The BSP must initialize CMOS shutdown code to 0Ah ..."
   1674  1.2.4.2  ad 	 */
   1675  1.2.4.2  ad 
   1676  1.2.4.2  ad 	outb(IO_RTC, NVRAM_RESET);
   1677  1.2.4.2  ad 	outb(IO_RTC+1, NVRAM_RESET_JUMP);
   1678  1.2.4.2  ad 
   1679  1.2.4.2  ad 	/*
   1680  1.2.4.2  ad 	 * "and the warm reset vector (DWORD based at 40:67) to point
   1681  1.2.4.2  ad 	 * to the AP startup code ..."
   1682  1.2.4.2  ad 	 */
   1683  1.2.4.2  ad 
   1684  1.2.4.2  ad 	dwordptr[0] = 0;
   1685  1.2.4.2  ad 	dwordptr[1] = MP_TRAMPOLINE >> 4;
   1686  1.2.4.2  ad 
   1687  1.2.4.2  ad 	pmap_kenter_pa (0, 0, VM_PROT_READ|VM_PROT_WRITE);
   1688  1.2.4.2  ad 	memcpy ((u_int8_t *) 0x467, dwordptr, 4);
   1689  1.2.4.2  ad 	pmap_kremove (0, PAGE_SIZE);
   1690  1.2.4.2  ad 
   1691  1.2.4.2  ad #if NLAPIC > 0
   1692  1.2.4.2  ad 	/*
   1693  1.2.4.2  ad 	 * ... prior to executing the following sequence:"
   1694  1.2.4.2  ad 	 */
   1695  1.2.4.2  ad 
   1696  1.2.4.2  ad 	if (ci->ci_flags & CPUF_AP) {
   1697  1.2.4.2  ad 		if ((error = x86_ipi_init(ci->ci_apicid)) != 0)
   1698  1.2.4.2  ad 			return error;
   1699  1.2.4.2  ad 
   1700  1.2.4.2  ad 		delay(10000);
   1701  1.2.4.2  ad 
   1702  1.2.4.2  ad 		if (cpu_feature & CPUID_APIC) {
   1703  1.2.4.2  ad 
   1704  1.2.4.2  ad 			if ((error = x86_ipi(MP_TRAMPOLINE/PAGE_SIZE,
   1705  1.2.4.2  ad 					     ci->ci_apicid,
   1706  1.2.4.2  ad 					     LAPIC_DLMODE_STARTUP)) != 0)
   1707  1.2.4.2  ad 				return error;
   1708  1.2.4.2  ad 			delay(200);
   1709  1.2.4.2  ad 
   1710  1.2.4.2  ad 			if ((error = x86_ipi(MP_TRAMPOLINE/PAGE_SIZE,
   1711  1.2.4.2  ad 					     ci->ci_apicid,
   1712  1.2.4.2  ad 					     LAPIC_DLMODE_STARTUP)) != 0)
   1713  1.2.4.2  ad 				return error;
   1714  1.2.4.2  ad 			delay(200);
   1715  1.2.4.2  ad 		}
   1716  1.2.4.2  ad 	}
   1717  1.2.4.2  ad #endif
   1718  1.2.4.2  ad #endif /* 0 */
   1719  1.2.4.2  ad 	return 0;
   1720  1.2.4.2  ad }
   1721  1.2.4.2  ad 
   1722  1.2.4.2  ad void
   1723  1.2.4.2  ad mp_cpu_start_cleanup(struct cpu_info *ci)
   1724  1.2.4.2  ad {
   1725  1.2.4.2  ad #if 0
   1726  1.2.4.2  ad 	/*
   1727  1.2.4.2  ad 	 * Ensure the NVRAM reset byte contains something vaguely sane.
   1728  1.2.4.2  ad 	 */
   1729  1.2.4.2  ad 
   1730  1.2.4.2  ad 	outb(IO_RTC, NVRAM_RESET);
   1731  1.2.4.2  ad 	outb(IO_RTC+1, NVRAM_RESET_RST);
   1732  1.2.4.2  ad #endif
   1733  1.2.4.2  ad }
   1734  1.2.4.2  ad 
   1735  1.2.4.2  ad #ifdef __x86_64__
   1736  1.2.4.2  ad 
   1737  1.2.4.2  ad void
   1738  1.2.4.2  ad cpu_init_msrs(struct cpu_info *ci)
   1739  1.2.4.2  ad {
   1740  1.2.4.2  ad 	HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
   1741  1.2.4.2  ad 	HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (u_int64_t) ci);
   1742  1.2.4.2  ad 	HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
   1743  1.2.4.2  ad }
   1744  1.2.4.2  ad #endif	/* __x86_64__ */
   1745  1.2.4.2  ad 
   1746  1.2.4.2  ad void
   1747  1.2.4.2  ad cpu_get_tsc_freq(struct cpu_info *ci)
   1748  1.2.4.2  ad {
   1749  1.2.4.2  ad #ifdef XEN3
   1750  1.2.4.2  ad 	const volatile vcpu_time_info_t *tinfo =
   1751  1.2.4.2  ad 		   &HYPERVISOR_shared_info->vcpu_info[0].time;
   1752  1.2.4.2  ad 	delay(1000000);
   1753  1.2.4.2  ad 	uint64_t freq = 1000000000ULL << 32;
   1754  1.2.4.2  ad 	freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
   1755  1.2.4.2  ad 	if ( tinfo->tsc_shift < 0 )
   1756  1.2.4.2  ad 		freq = freq << -tinfo->tsc_shift;
   1757  1.2.4.2  ad 	else
   1758  1.2.4.2  ad 		freq = freq >> tinfo->tsc_shift;
   1759  1.2.4.2  ad 	ci->ci_tsc_freq = freq;
   1760  1.2.4.2  ad #else
   1761  1.2.4.2  ad 	/* XXX this needs to read the shared_info of the CPU being probed.. */
   1762  1.2.4.2  ad 	ci->ci_tsc_freq = HYPERVISOR_shared_info->cpu_freq;
   1763  1.2.4.2  ad #endif /* XEN3 */
   1764  1.2.4.2  ad }
   1765