Home | History | Annotate | Line # | Download | only in x86
cpu.c revision 1.1.2.2
      1  1.1.2.2  bouyer /*	$NetBSD: cpu.c,v 1.1.2.2 2007/10/25 23:59:24 bouyer Exp $	*/
      2  1.1.2.1  bouyer /* NetBSD: cpu.c,v 1.18 2004/02/20 17:35:01 yamt Exp  */
      3  1.1.2.1  bouyer 
      4  1.1.2.1  bouyer /*-
      5  1.1.2.1  bouyer  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      6  1.1.2.1  bouyer  * All rights reserved.
      7  1.1.2.1  bouyer  *
      8  1.1.2.1  bouyer  * This code is derived from software contributed to The NetBSD Foundation
      9  1.1.2.1  bouyer  * by RedBack Networks Inc.
     10  1.1.2.1  bouyer  *
     11  1.1.2.1  bouyer  * Author: Bill Sommerfeld
     12  1.1.2.1  bouyer  *
     13  1.1.2.1  bouyer  * Redistribution and use in source and binary forms, with or without
     14  1.1.2.1  bouyer  * modification, are permitted provided that the following conditions
     15  1.1.2.1  bouyer  * are met:
     16  1.1.2.1  bouyer  * 1. Redistributions of source code must retain the above copyright
     17  1.1.2.1  bouyer  *    notice, this list of conditions and the following disclaimer.
     18  1.1.2.1  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1.2.1  bouyer  *    notice, this list of conditions and the following disclaimer in the
     20  1.1.2.1  bouyer  *    documentation and/or other materials provided with the distribution.
     21  1.1.2.1  bouyer  * 3. All advertising materials mentioning features or use of this software
     22  1.1.2.1  bouyer  *    must display the following acknowledgement:
     23  1.1.2.1  bouyer  *        This product includes software developed by the NetBSD
     24  1.1.2.1  bouyer  *        Foundation, Inc. and its contributors.
     25  1.1.2.1  bouyer  * 4. Neither the name of The NetBSD Foundation nor the names of its
     26  1.1.2.1  bouyer  *    contributors may be used to endorse or promote products derived
     27  1.1.2.1  bouyer  *    from this software without specific prior written permission.
     28  1.1.2.1  bouyer  *
     29  1.1.2.1  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     30  1.1.2.1  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     31  1.1.2.1  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     32  1.1.2.1  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     33  1.1.2.1  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     34  1.1.2.1  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     35  1.1.2.1  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     36  1.1.2.1  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     37  1.1.2.1  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     38  1.1.2.1  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     39  1.1.2.1  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     40  1.1.2.1  bouyer  */
     41  1.1.2.1  bouyer 
     42  1.1.2.1  bouyer /*
     43  1.1.2.1  bouyer  * Copyright (c) 1999 Stefan Grefen
     44  1.1.2.1  bouyer  *
     45  1.1.2.1  bouyer  * Redistribution and use in source and binary forms, with or without
     46  1.1.2.1  bouyer  * modification, are permitted provided that the following conditions
     47  1.1.2.1  bouyer  * are met:
     48  1.1.2.1  bouyer  * 1. Redistributions of source code must retain the above copyright
     49  1.1.2.1  bouyer  *    notice, this list of conditions and the following disclaimer.
     50  1.1.2.1  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     51  1.1.2.1  bouyer  *    notice, this list of conditions and the following disclaimer in the
     52  1.1.2.1  bouyer  *    documentation and/or other materials provided with the distribution.
     53  1.1.2.1  bouyer  * 3. All advertising materials mentioning features or use of this software
     54  1.1.2.1  bouyer  *    must display the following acknowledgement:
     55  1.1.2.1  bouyer  *      This product includes software developed by the NetBSD
     56  1.1.2.1  bouyer  *      Foundation, Inc. and its contributors.
     57  1.1.2.1  bouyer  * 4. Neither the name of The NetBSD Foundation nor the names of its
     58  1.1.2.1  bouyer  *    contributors may be used to endorse or promote products derived
     59  1.1.2.1  bouyer  *    from this software without specific prior written permission.
     60  1.1.2.1  bouyer  *
     61  1.1.2.1  bouyer  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
     62  1.1.2.1  bouyer  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63  1.1.2.1  bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64  1.1.2.1  bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
     65  1.1.2.1  bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66  1.1.2.1  bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67  1.1.2.1  bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68  1.1.2.1  bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69  1.1.2.1  bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70  1.1.2.1  bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71  1.1.2.1  bouyer  * SUCH DAMAGE.
     72  1.1.2.1  bouyer  */
     73  1.1.2.1  bouyer 
     74  1.1.2.1  bouyer #include <sys/cdefs.h>
     75  1.1.2.2  bouyer __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.1.2.2 2007/10/25 23:59:24 bouyer Exp $");
     76  1.1.2.1  bouyer 
     77  1.1.2.1  bouyer #include "opt_ddb.h"
     78  1.1.2.1  bouyer #include "opt_multiprocessor.h"
     79  1.1.2.1  bouyer #include "opt_mpbios.h"		/* for MPDEBUG */
     80  1.1.2.1  bouyer #include "opt_mtrr.h"
     81  1.1.2.1  bouyer #include "opt_xen.h"
     82  1.1.2.1  bouyer 
     83  1.1.2.1  bouyer #include "lapic.h"
     84  1.1.2.1  bouyer #include "ioapic.h"
     85  1.1.2.1  bouyer 
     86  1.1.2.1  bouyer #include <sys/param.h>
     87  1.1.2.1  bouyer #include <sys/proc.h>
     88  1.1.2.1  bouyer #include <sys/user.h>
     89  1.1.2.1  bouyer #include <sys/systm.h>
     90  1.1.2.1  bouyer #include <sys/device.h>
     91  1.1.2.1  bouyer #include <sys/malloc.h>
     92  1.1.2.1  bouyer 
     93  1.1.2.1  bouyer #include <uvm/uvm_extern.h>
     94  1.1.2.1  bouyer 
     95  1.1.2.1  bouyer #include <machine/cpu.h>
     96  1.1.2.1  bouyer #include <machine/cpufunc.h>
     97  1.1.2.1  bouyer #include <machine/cpuvar.h>
     98  1.1.2.1  bouyer #include <machine/pmap.h>
     99  1.1.2.1  bouyer #include <machine/vmparam.h>
    100  1.1.2.1  bouyer #include <machine/mpbiosvar.h>
    101  1.1.2.1  bouyer #include <machine/pcb.h>
    102  1.1.2.1  bouyer #include <machine/specialreg.h>
    103  1.1.2.1  bouyer #include <machine/segments.h>
    104  1.1.2.1  bouyer #include <machine/gdt.h>
    105  1.1.2.1  bouyer #include <machine/mtrr.h>
    106  1.1.2.1  bouyer #include <machine/pio.h>
    107  1.1.2.1  bouyer 
    108  1.1.2.1  bouyer #ifdef XEN3
    109  1.1.2.1  bouyer #include <xen/vcpuvar.h>
    110  1.1.2.1  bouyer #endif
    111  1.1.2.1  bouyer 
    112  1.1.2.1  bouyer #if NLAPIC > 0
    113  1.1.2.1  bouyer #include <machine/apicvar.h>
    114  1.1.2.1  bouyer #include <machine/i82489reg.h>
    115  1.1.2.1  bouyer #include <machine/i82489var.h>
    116  1.1.2.1  bouyer #endif
    117  1.1.2.1  bouyer 
    118  1.1.2.1  bouyer #if NIOAPIC > 0
    119  1.1.2.1  bouyer #include <machine/i82093var.h>
    120  1.1.2.1  bouyer #endif
    121  1.1.2.1  bouyer 
    122  1.1.2.1  bouyer #include <dev/ic/mc146818reg.h>
    123  1.1.2.1  bouyer #include <dev/isa/isareg.h>
    124  1.1.2.1  bouyer 
    125  1.1.2.1  bouyer int     cpu_match(struct device *, struct cfdata *, void *);
    126  1.1.2.1  bouyer void    cpu_attach(struct device *, struct device *, void *);
    127  1.1.2.1  bouyer #ifdef XEN3
    128  1.1.2.1  bouyer int     vcpu_match(struct device *, struct cfdata *, void *);
    129  1.1.2.1  bouyer void    vcpu_attach(struct device *, struct device *, void *);
    130  1.1.2.1  bouyer #endif
    131  1.1.2.1  bouyer void    cpu_attach_common(struct device *, struct device *, void *);
    132  1.1.2.1  bouyer 
    133  1.1.2.1  bouyer struct cpu_softc {
    134  1.1.2.1  bouyer 	struct device sc_dev;		/* device tree glue */
    135  1.1.2.1  bouyer 	struct cpu_info *sc_info;	/* pointer to CPU info */
    136  1.1.2.1  bouyer };
    137  1.1.2.1  bouyer 
    138  1.1.2.1  bouyer int mp_cpu_start(struct cpu_info *);
    139  1.1.2.1  bouyer void mp_cpu_start_cleanup(struct cpu_info *);
    140  1.1.2.1  bouyer const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
    141  1.1.2.1  bouyer 				      mp_cpu_start_cleanup };
    142  1.1.2.1  bouyer 
    143  1.1.2.1  bouyer CFATTACH_DECL(cpu, sizeof(struct cpu_softc),
    144  1.1.2.1  bouyer     cpu_match, cpu_attach, NULL, NULL);
    145  1.1.2.1  bouyer #ifdef XEN3
    146  1.1.2.1  bouyer CFATTACH_DECL(vcpu, sizeof(struct cpu_softc),
    147  1.1.2.1  bouyer     vcpu_match, vcpu_attach, NULL, NULL);
    148  1.1.2.1  bouyer #endif
    149  1.1.2.1  bouyer 
    150  1.1.2.1  bouyer /*
    151  1.1.2.1  bouyer  * Statically-allocated CPU info for the primary CPU (or the only
    152  1.1.2.1  bouyer  * CPU, on uniprocessors).  The CPU info list is initialized to
    153  1.1.2.1  bouyer  * point at it.
    154  1.1.2.1  bouyer  */
    155  1.1.2.1  bouyer #ifdef TRAPLOG
    156  1.1.2.1  bouyer #include <machine/tlog.h>
    157  1.1.2.1  bouyer struct tlog tlog_primary;
    158  1.1.2.1  bouyer #endif
    159  1.1.2.1  bouyer struct cpu_info cpu_info_primary = {
    160  1.1.2.1  bouyer 	.ci_self = &cpu_info_primary,
    161  1.1.2.1  bouyer #ifndef __x86_64__
    162  1.1.2.1  bouyer 	.ci_self150 = (uint8_t *)&cpu_info_primary + 0x150,
    163  1.1.2.1  bouyer #endif
    164  1.1.2.1  bouyer 	.ci_curlwp = &lwp0,
    165  1.1.2.1  bouyer #ifdef TRAPLOG
    166  1.1.2.1  bouyer 	.ci_tlog = &tlog_primary,
    167  1.1.2.1  bouyer #endif
    168  1.1.2.1  bouyer 
    169  1.1.2.1  bouyer };
    170  1.1.2.1  bouyer struct cpu_info phycpu_info_primary = {
    171  1.1.2.1  bouyer 	.ci_self = &phycpu_info_primary,
    172  1.1.2.1  bouyer #ifndef __x86_64__
    173  1.1.2.1  bouyer 	.ci_self150 = (uint8_t *)&phycpu_info_primary + 0x150,
    174  1.1.2.1  bouyer #endif
    175  1.1.2.1  bouyer };
    176  1.1.2.1  bouyer 
    177  1.1.2.1  bouyer struct cpu_info *cpu_info_list = &cpu_info_primary;
    178  1.1.2.1  bouyer 
    179  1.1.2.1  bouyer static void	cpu_set_tss_gates(struct cpu_info *ci);
    180  1.1.2.1  bouyer 
    181  1.1.2.1  bouyer u_int32_t cpus_attached = 0;
    182  1.1.2.1  bouyer 
    183  1.1.2.1  bouyer struct cpu_info *phycpu_info[X86_MAXPROCS] = { &cpu_info_primary };
    184  1.1.2.1  bouyer 
    185  1.1.2.1  bouyer #ifdef MULTIPROCESSOR
    186  1.1.2.1  bouyer /*
    187  1.1.2.1  bouyer  * Array of CPU info structures.  Must be statically-allocated because
    188  1.1.2.1  bouyer  * curproc, etc. are used early.
    189  1.1.2.1  bouyer  */
    190  1.1.2.1  bouyer struct cpu_info *cpu_info[X86_MAXPROCS] = { &cpu_info_primary };
    191  1.1.2.1  bouyer 
    192  1.1.2.1  bouyer u_int32_t cpus_running = 0;
    193  1.1.2.1  bouyer 
    194  1.1.2.1  bouyer void    	cpu_hatch(void *);
    195  1.1.2.1  bouyer static void    	cpu_boot_secondary(struct cpu_info *ci);
    196  1.1.2.1  bouyer static void    	cpu_start_secondary(struct cpu_info *ci);
    197  1.1.2.1  bouyer static void	cpu_copy_trampoline(void);
    198  1.1.2.1  bouyer 
    199  1.1.2.1  bouyer /*
    200  1.1.2.1  bouyer  * Runs once per boot once multiprocessor goo has been detected and
    201  1.1.2.1  bouyer  * the local APIC on the boot processor has been mapped.
    202  1.1.2.1  bouyer  *
    203  1.1.2.1  bouyer  * Called from lapic_boot_init() (from mpbios_scan()).
    204  1.1.2.1  bouyer  */
    205  1.1.2.1  bouyer void
    206  1.1.2.1  bouyer cpu_init_first()
    207  1.1.2.1  bouyer {
    208  1.1.2.1  bouyer 	int cpunum = lapic_cpu_number();
    209  1.1.2.1  bouyer 
    210  1.1.2.1  bouyer 	if (cpunum != 0) {
    211  1.1.2.1  bouyer 		cpu_info[0] = NULL;
    212  1.1.2.1  bouyer 		cpu_info[cpunum] = &cpu_info_primary;
    213  1.1.2.1  bouyer 	}
    214  1.1.2.1  bouyer 
    215  1.1.2.1  bouyer 	cpu_copy_trampoline();
    216  1.1.2.1  bouyer }
    217  1.1.2.1  bouyer #endif
    218  1.1.2.1  bouyer 
    219  1.1.2.1  bouyer int
    220  1.1.2.1  bouyer cpu_match(parent, match, aux)
    221  1.1.2.1  bouyer 	struct device *parent;
    222  1.1.2.1  bouyer 	struct cfdata *match;
    223  1.1.2.1  bouyer 	void *aux;
    224  1.1.2.1  bouyer {
    225  1.1.2.1  bouyer 
    226  1.1.2.1  bouyer 	return 1;
    227  1.1.2.1  bouyer }
    228  1.1.2.1  bouyer 
    229  1.1.2.1  bouyer void
    230  1.1.2.1  bouyer cpu_attach(parent, self, aux)
    231  1.1.2.1  bouyer 	struct device *parent, *self;
    232  1.1.2.1  bouyer 	void *aux;
    233  1.1.2.1  bouyer {
    234  1.1.2.1  bouyer #ifdef XEN3
    235  1.1.2.1  bouyer 	struct cpu_softc *sc = (void *) self;
    236  1.1.2.1  bouyer 	struct cpu_attach_args *caa = aux;
    237  1.1.2.1  bouyer 	struct cpu_info *ci;
    238  1.1.2.1  bouyer 	int cpunum = caa->cpu_number;
    239  1.1.2.1  bouyer 
    240  1.1.2.1  bouyer 	/*
    241  1.1.2.1  bouyer 	 * If we're an Application Processor, allocate a cpu_info
    242  1.1.2.1  bouyer 	 * structure, otherwise use the primary's.
    243  1.1.2.1  bouyer 	 */
    244  1.1.2.1  bouyer 	if (caa->cpu_role == CPU_ROLE_AP) {
    245  1.1.2.1  bouyer 		ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
    246  1.1.2.1  bouyer 		if (phycpu_info[cpunum] != NULL)
    247  1.1.2.1  bouyer 			panic("cpu at apic id %d already attached?", cpunum);
    248  1.1.2.1  bouyer 		phycpu_info[cpunum] = ci;
    249  1.1.2.1  bouyer 	} else {
    250  1.1.2.1  bouyer 		ci = &phycpu_info_primary;
    251  1.1.2.1  bouyer 		if (cpunum != 0) {
    252  1.1.2.1  bouyer 			phycpu_info[0] = NULL;
    253  1.1.2.1  bouyer 			phycpu_info[cpunum] = ci;
    254  1.1.2.1  bouyer 		}
    255  1.1.2.1  bouyer 	}
    256  1.1.2.1  bouyer 
    257  1.1.2.1  bouyer 	ci->ci_self = ci;
    258  1.1.2.1  bouyer 	sc->sc_info = ci;
    259  1.1.2.1  bouyer 
    260  1.1.2.1  bouyer 	ci->ci_dev = self;
    261  1.1.2.1  bouyer 	ci->ci_apicid = caa->cpu_number;
    262  1.1.2.1  bouyer 	ci->ci_cpuid = ci->ci_apicid;
    263  1.1.2.1  bouyer 
    264  1.1.2.1  bouyer 	printf(": ");
    265  1.1.2.1  bouyer 	switch (caa->cpu_role) {
    266  1.1.2.1  bouyer 	case CPU_ROLE_SP:
    267  1.1.2.1  bouyer 		printf("(uniprocessor)\n");
    268  1.1.2.1  bouyer 		ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
    269  1.1.2.1  bouyer 		break;
    270  1.1.2.1  bouyer 
    271  1.1.2.1  bouyer 	case CPU_ROLE_BP:
    272  1.1.2.1  bouyer 		printf("(boot processor)\n");
    273  1.1.2.1  bouyer 		ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
    274  1.1.2.1  bouyer #if NIOAPIC > 0
    275  1.1.2.1  bouyer 		ioapic_bsp_id = caa->cpu_number;
    276  1.1.2.1  bouyer #endif
    277  1.1.2.1  bouyer 		break;
    278  1.1.2.1  bouyer 
    279  1.1.2.1  bouyer 	case CPU_ROLE_AP:
    280  1.1.2.1  bouyer 		/*
    281  1.1.2.1  bouyer 		 * report on an AP
    282  1.1.2.1  bouyer 		 */
    283  1.1.2.1  bouyer 		printf("(application processor)\n");
    284  1.1.2.1  bouyer 		break;
    285  1.1.2.1  bouyer 
    286  1.1.2.1  bouyer 	default:
    287  1.1.2.1  bouyer 		panic("unknown processor type??\n");
    288  1.1.2.1  bouyer 	}
    289  1.1.2.1  bouyer 	return;
    290  1.1.2.1  bouyer #else
    291  1.1.2.1  bouyer 	cpu_attach_common(parent, self, aux);
    292  1.1.2.1  bouyer #endif
    293  1.1.2.1  bouyer }
    294  1.1.2.1  bouyer 
    295  1.1.2.1  bouyer #ifdef XEN3
    296  1.1.2.1  bouyer int
    297  1.1.2.1  bouyer vcpu_match(parent, match, aux)
    298  1.1.2.1  bouyer 	struct device *parent;
    299  1.1.2.1  bouyer 	struct cfdata *match;
    300  1.1.2.1  bouyer 	void *aux;
    301  1.1.2.1  bouyer {
    302  1.1.2.1  bouyer 	struct vcpu_attach_args *vcaa = aux;
    303  1.1.2.1  bouyer 
    304  1.1.2.1  bouyer 	if (strcmp(vcaa->vcaa_name, match->cf_name) == 0)
    305  1.1.2.1  bouyer 		return 1;
    306  1.1.2.1  bouyer 	return 0;
    307  1.1.2.1  bouyer }
    308  1.1.2.1  bouyer 
    309  1.1.2.1  bouyer void
    310  1.1.2.1  bouyer vcpu_attach(parent, self, aux)
    311  1.1.2.1  bouyer 	struct device *parent, *self;
    312  1.1.2.1  bouyer 	void *aux;
    313  1.1.2.1  bouyer {
    314  1.1.2.1  bouyer 	struct vcpu_attach_args *vcaa = aux;
    315  1.1.2.1  bouyer 
    316  1.1.2.1  bouyer 	cpu_attach_common(parent, self, &vcaa->vcaa_caa);
    317  1.1.2.1  bouyer }
    318  1.1.2.1  bouyer #endif
    319  1.1.2.1  bouyer 
    320  1.1.2.1  bouyer static void
    321  1.1.2.1  bouyer cpu_vm_init(struct cpu_info *ci)
    322  1.1.2.1  bouyer {
    323  1.1.2.1  bouyer 	int ncolors = 2, i;
    324  1.1.2.1  bouyer 
    325  1.1.2.1  bouyer 	for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
    326  1.1.2.1  bouyer 		struct x86_cache_info *cai;
    327  1.1.2.1  bouyer 		int tcolors;
    328  1.1.2.1  bouyer 
    329  1.1.2.1  bouyer 		cai = &ci->ci_cinfo[i];
    330  1.1.2.1  bouyer 
    331  1.1.2.1  bouyer 		tcolors = atop(cai->cai_totalsize);
    332  1.1.2.1  bouyer 		switch(cai->cai_associativity) {
    333  1.1.2.1  bouyer 		case 0xff:
    334  1.1.2.1  bouyer 			tcolors = 1; /* fully associative */
    335  1.1.2.1  bouyer 			break;
    336  1.1.2.1  bouyer 		case 0:
    337  1.1.2.1  bouyer 		case 1:
    338  1.1.2.1  bouyer 			break;
    339  1.1.2.1  bouyer 		default:
    340  1.1.2.1  bouyer 			tcolors /= cai->cai_associativity;
    341  1.1.2.1  bouyer 		}
    342  1.1.2.1  bouyer 		ncolors = max(ncolors, tcolors);
    343  1.1.2.1  bouyer 	}
    344  1.1.2.1  bouyer 
    345  1.1.2.1  bouyer 	/*
    346  1.1.2.1  bouyer 	 * Knowing the size of the largest cache on this CPU, re-color
    347  1.1.2.1  bouyer 	 * our pages.
    348  1.1.2.1  bouyer 	 */
    349  1.1.2.1  bouyer 	if (ncolors <= uvmexp.ncolors)
    350  1.1.2.1  bouyer 		return;
    351  1.1.2.1  bouyer 	printf("%s: %d page colors\n", ci->ci_dev->dv_xname, ncolors);
    352  1.1.2.1  bouyer 	uvm_page_recolor(ncolors);
    353  1.1.2.1  bouyer }
    354  1.1.2.1  bouyer 
    355  1.1.2.1  bouyer void
    356  1.1.2.1  bouyer cpu_attach_common(parent, self, aux)
    357  1.1.2.1  bouyer 	struct device *parent, *self;
    358  1.1.2.1  bouyer 	void *aux;
    359  1.1.2.1  bouyer {
    360  1.1.2.1  bouyer 	struct cpu_softc *sc = (void *) self;
    361  1.1.2.1  bouyer 	struct cpu_attach_args *caa = aux;
    362  1.1.2.1  bouyer 	struct cpu_info *ci;
    363  1.1.2.1  bouyer #if defined(MULTIPROCESSOR)
    364  1.1.2.1  bouyer 	int cpunum = caa->cpu_number;
    365  1.1.2.1  bouyer #endif
    366  1.1.2.1  bouyer 
    367  1.1.2.1  bouyer 	/*
    368  1.1.2.1  bouyer 	 * If we're an Application Processor, allocate a cpu_info
    369  1.1.2.1  bouyer 	 * structure, otherwise use the primary's.
    370  1.1.2.1  bouyer 	 */
    371  1.1.2.1  bouyer 	if (caa->cpu_role == CPU_ROLE_AP) {
    372  1.1.2.1  bouyer 		ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
    373  1.1.2.1  bouyer #if defined(MULTIPROCESSOR)
    374  1.1.2.1  bouyer 		if (cpu_info[cpunum] != NULL)
    375  1.1.2.1  bouyer 			panic("cpu at apic id %d already attached?", cpunum);
    376  1.1.2.1  bouyer 		cpu_info[cpunum] = ci;
    377  1.1.2.1  bouyer #endif
    378  1.1.2.1  bouyer #ifdef TRAPLOG
    379  1.1.2.1  bouyer 		ci->ci_tlog_base = malloc(sizeof(struct tlog),
    380  1.1.2.1  bouyer 		    M_DEVBUF, M_WAITOK);
    381  1.1.2.1  bouyer #endif
    382  1.1.2.1  bouyer 	} else {
    383  1.1.2.1  bouyer 		ci = &cpu_info_primary;
    384  1.1.2.1  bouyer #if defined(MULTIPROCESSOR)
    385  1.1.2.1  bouyer 		if (cpunum != lapic_cpu_number()) {
    386  1.1.2.1  bouyer 			panic("%s: running CPU is at apic %d"
    387  1.1.2.1  bouyer 			    " instead of at expected %d",
    388  1.1.2.1  bouyer 			    sc->sc_dev.dv_xname, lapic_cpu_number(), cpunum);
    389  1.1.2.1  bouyer 		}
    390  1.1.2.1  bouyer #endif
    391  1.1.2.1  bouyer 	}
    392  1.1.2.1  bouyer 
    393  1.1.2.1  bouyer 	ci->ci_self = ci;
    394  1.1.2.1  bouyer 	sc->sc_info = ci;
    395  1.1.2.1  bouyer 
    396  1.1.2.1  bouyer 	ci->ci_dev = self;
    397  1.1.2.1  bouyer 	ci->ci_apicid = caa->cpu_number;
    398  1.1.2.1  bouyer #ifdef MULTIPROCESSOR
    399  1.1.2.1  bouyer 	ci->ci_cpuid = ci->ci_apicid;
    400  1.1.2.1  bouyer #else
    401  1.1.2.1  bouyer 	ci->ci_cpuid = 0;	/* False for APs, but they're not used anyway */
    402  1.1.2.1  bouyer #endif
    403  1.1.2.2  bouyer 	ci->ci_cpumask = (1 << ci->ci_cpuid);
    404  1.1.2.1  bouyer 	ci->ci_func = caa->cpu_func;
    405  1.1.2.1  bouyer 
    406  1.1.2.1  bouyer #ifndef __x86_64__
    407  1.1.2.1  bouyer 	simple_lock_init(&ci->ci_slock);
    408  1.1.2.1  bouyer #endif
    409  1.1.2.1  bouyer 
    410  1.1.2.1  bouyer 	if (caa->cpu_role == CPU_ROLE_AP) {
    411  1.1.2.1  bouyer #if defined(MULTIPROCESSOR)
    412  1.1.2.1  bouyer 		int error;
    413  1.1.2.1  bouyer 
    414  1.1.2.1  bouyer 		error = mi_cpu_attach(ci);
    415  1.1.2.1  bouyer 		if (error != 0) {
    416  1.1.2.1  bouyer 			aprint_normal("\n");
    417  1.1.2.1  bouyer 			aprint_error("%s: mi_cpu_attach failed with %d\n",
    418  1.1.2.1  bouyer 			    sc->sc_dev.dv_xname, error);
    419  1.1.2.1  bouyer 			return;
    420  1.1.2.1  bouyer 		}
    421  1.1.2.1  bouyer #endif
    422  1.1.2.1  bouyer 	} else {
    423  1.1.2.1  bouyer 		KASSERT(ci->ci_data.cpu_idlelwp != NULL);
    424  1.1.2.1  bouyer 	}
    425  1.1.2.1  bouyer 
    426  1.1.2.1  bouyer 	pmap_reference(pmap_kernel());
    427  1.1.2.1  bouyer 	ci->ci_pmap = pmap_kernel();
    428  1.1.2.1  bouyer 	ci->ci_tlbstate = TLBSTATE_STALE;
    429  1.1.2.1  bouyer 
    430  1.1.2.1  bouyer 	/* further PCB init done later. */
    431  1.1.2.1  bouyer 
    432  1.1.2.1  bouyer 	printf(": ");
    433  1.1.2.1  bouyer 
    434  1.1.2.1  bouyer 	switch (caa->cpu_role) {
    435  1.1.2.1  bouyer 	case CPU_ROLE_SP:
    436  1.1.2.1  bouyer 		printf("(uniprocessor)\n");
    437  1.1.2.1  bouyer 		ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
    438  1.1.2.1  bouyer 		cpu_intr_init(ci);
    439  1.1.2.1  bouyer 		identifycpu(ci);
    440  1.1.2.1  bouyer 		cpu_init(ci);
    441  1.1.2.1  bouyer 		cpu_set_tss_gates(ci);
    442  1.1.2.1  bouyer 		break;
    443  1.1.2.1  bouyer 
    444  1.1.2.1  bouyer 	case CPU_ROLE_BP:
    445  1.1.2.1  bouyer 		printf("apid %d (boot processor)\n", caa->cpu_number);
    446  1.1.2.1  bouyer 		ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
    447  1.1.2.1  bouyer 		cpu_intr_init(ci);
    448  1.1.2.1  bouyer 		identifycpu(ci);
    449  1.1.2.1  bouyer 		cpu_init(ci);
    450  1.1.2.1  bouyer 		cpu_set_tss_gates(ci);
    451  1.1.2.1  bouyer 		break;
    452  1.1.2.1  bouyer 
    453  1.1.2.1  bouyer 	case CPU_ROLE_AP:
    454  1.1.2.1  bouyer 		/*
    455  1.1.2.1  bouyer 		 * report on an AP
    456  1.1.2.1  bouyer 		 */
    457  1.1.2.1  bouyer 		printf("apid %d (application processor)\n", caa->cpu_number);
    458  1.1.2.1  bouyer 
    459  1.1.2.1  bouyer #if defined(MULTIPROCESSOR)
    460  1.1.2.1  bouyer 		cpu_intr_init(ci);
    461  1.1.2.1  bouyer 		gdt_alloc_cpu(ci);
    462  1.1.2.1  bouyer 		cpu_set_tss_gates(ci);
    463  1.1.2.1  bouyer 		cpu_start_secondary(ci);
    464  1.1.2.1  bouyer 		if (ci->ci_flags & CPUF_PRESENT) {
    465  1.1.2.1  bouyer 			identifycpu(ci);
    466  1.1.2.1  bouyer 			ci->ci_next = cpu_info_list->ci_next;
    467  1.1.2.1  bouyer 			cpu_info_list->ci_next = ci;
    468  1.1.2.1  bouyer 		}
    469  1.1.2.1  bouyer #else
    470  1.1.2.1  bouyer 		printf("%s: not started\n", sc->sc_dev.dv_xname);
    471  1.1.2.1  bouyer #endif
    472  1.1.2.1  bouyer 		break;
    473  1.1.2.1  bouyer 
    474  1.1.2.1  bouyer 	default:
    475  1.1.2.1  bouyer 		panic("unknown processor type??\n");
    476  1.1.2.1  bouyer 	}
    477  1.1.2.1  bouyer 	cpu_vm_init(ci);
    478  1.1.2.1  bouyer 
    479  1.1.2.1  bouyer 	cpus_attached |= (1 << ci->ci_cpuid);
    480  1.1.2.1  bouyer 
    481  1.1.2.1  bouyer #if defined(MULTIPROCESSOR)
    482  1.1.2.1  bouyer 	if (mp_verbose) {
    483  1.1.2.1  bouyer 		struct lwp *l = ci->ci_data.cpu_idlelwp;
    484  1.1.2.1  bouyer 
    485  1.1.2.1  bouyer 		aprint_verbose("%s: idle lwp at %p, idle sp at 0x%x\n",
    486  1.1.2.1  bouyer 		    sc->sc_dev.dv_xname, l, l->l_addr->u_pcb.pcb_esp);
    487  1.1.2.1  bouyer 	}
    488  1.1.2.1  bouyer #endif
    489  1.1.2.1  bouyer }
    490  1.1.2.1  bouyer 
    491  1.1.2.1  bouyer /*
    492  1.1.2.1  bouyer  * Initialize the processor appropriately.
    493  1.1.2.1  bouyer  */
    494  1.1.2.1  bouyer 
    495  1.1.2.1  bouyer void
    496  1.1.2.1  bouyer cpu_init(ci)
    497  1.1.2.1  bouyer 	struct cpu_info *ci;
    498  1.1.2.1  bouyer {
    499  1.1.2.1  bouyer 	/* configure the CPU if needed */
    500  1.1.2.1  bouyer 	if (ci->cpu_setup != NULL)
    501  1.1.2.1  bouyer 		(*ci->cpu_setup)(ci);
    502  1.1.2.1  bouyer 
    503  1.1.2.1  bouyer #if defined(I686_CPU)
    504  1.1.2.1  bouyer 	/*
    505  1.1.2.1  bouyer 	 * On a P6 or above, enable global TLB caching if the
    506  1.1.2.1  bouyer 	 * hardware supports it.
    507  1.1.2.1  bouyer 	 */
    508  1.1.2.1  bouyer 	if (cpu_feature & CPUID_PGE)
    509  1.1.2.1  bouyer 		lcr4(rcr4() | CR4_PGE);	/* enable global TLB caching */
    510  1.1.2.1  bouyer 
    511  1.1.2.1  bouyer #ifdef MTRR
    512  1.1.2.1  bouyer 	/*
    513  1.1.2.1  bouyer 	 * On a P6 or above, initialize MTRR's if the hardware supports them.
    514  1.1.2.1  bouyer 	 */
    515  1.1.2.1  bouyer 	if (cpu_feature & CPUID_MTRR) {
    516  1.1.2.1  bouyer 		if ((ci->ci_flags & CPUF_AP) == 0)
    517  1.1.2.1  bouyer 			i686_mtrr_init_first();
    518  1.1.2.1  bouyer 		mtrr_init_cpu(ci);
    519  1.1.2.1  bouyer 	}
    520  1.1.2.1  bouyer #endif
    521  1.1.2.1  bouyer #endif
    522  1.1.2.1  bouyer #if defined(I686_CPU)
    523  1.1.2.1  bouyer 	/*
    524  1.1.2.1  bouyer 	 * If we have FXSAVE/FXRESTOR, use them.
    525  1.1.2.1  bouyer 	 */
    526  1.1.2.1  bouyer 	if (cpu_feature & CPUID_FXSR) {
    527  1.1.2.1  bouyer 		lcr4(rcr4() | CR4_OSFXSR);
    528  1.1.2.1  bouyer 
    529  1.1.2.1  bouyer 		/*
    530  1.1.2.1  bouyer 		 * If we have SSE/SSE2, enable XMM exceptions.
    531  1.1.2.1  bouyer 		 */
    532  1.1.2.1  bouyer 		if (cpu_feature & (CPUID_SSE|CPUID_SSE2))
    533  1.1.2.1  bouyer 			lcr4(rcr4() | CR4_OSXMMEXCPT);
    534  1.1.2.1  bouyer 	}
    535  1.1.2.1  bouyer #endif /* I686_CPU */
    536  1.1.2.1  bouyer 
    537  1.1.2.1  bouyer #ifdef MULTIPROCESSOR
    538  1.1.2.1  bouyer 	ci->ci_flags |= CPUF_RUNNING;
    539  1.1.2.1  bouyer 	cpus_running |= 1 << ci->ci_cpuid;
    540  1.1.2.1  bouyer #endif
    541  1.1.2.1  bouyer }
    542  1.1.2.1  bouyer 
    543  1.1.2.1  bouyer 
    544  1.1.2.1  bouyer #ifdef MULTIPROCESSOR
    545  1.1.2.1  bouyer void
    546  1.1.2.1  bouyer cpu_boot_secondary_processors()
    547  1.1.2.1  bouyer {
    548  1.1.2.1  bouyer 	struct cpu_info *ci;
    549  1.1.2.1  bouyer 	u_long i;
    550  1.1.2.1  bouyer 
    551  1.1.2.1  bouyer 	for (i=0; i < X86_MAXPROCS; i++) {
    552  1.1.2.1  bouyer 		ci = cpu_info[i];
    553  1.1.2.1  bouyer 		if (ci == NULL)
    554  1.1.2.1  bouyer 			continue;
    555  1.1.2.1  bouyer 		if (ci->ci_data.cpu_idlelwp == NULL)
    556  1.1.2.1  bouyer 			continue;
    557  1.1.2.1  bouyer 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    558  1.1.2.1  bouyer 			continue;
    559  1.1.2.1  bouyer 		if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
    560  1.1.2.1  bouyer 			continue;
    561  1.1.2.1  bouyer 		cpu_boot_secondary(ci);
    562  1.1.2.1  bouyer 	}
    563  1.1.2.1  bouyer }
    564  1.1.2.1  bouyer 
    565  1.1.2.1  bouyer static void
    566  1.1.2.1  bouyer cpu_init_idle_lwp(struct cpu_info *ci)
    567  1.1.2.1  bouyer {
    568  1.1.2.1  bouyer 	struct lwp *l = ci->ci_data.cpu_idlelwp;
    569  1.1.2.1  bouyer 	struct pcb *pcb = &l->l_addr->u_pcb;
    570  1.1.2.1  bouyer 
    571  1.1.2.1  bouyer 	pcb->pcb_cr0 = rcr0();
    572  1.1.2.1  bouyer }
    573  1.1.2.1  bouyer 
    574  1.1.2.1  bouyer void
    575  1.1.2.1  bouyer cpu_init_idle_lwps()
    576  1.1.2.1  bouyer {
    577  1.1.2.1  bouyer 	struct cpu_info *ci;
    578  1.1.2.1  bouyer 	u_long i;
    579  1.1.2.1  bouyer 
    580  1.1.2.1  bouyer 	for (i = 0; i < X86_MAXPROCS; i++) {
    581  1.1.2.1  bouyer 		ci = cpu_info[i];
    582  1.1.2.1  bouyer 		if (ci == NULL)
    583  1.1.2.1  bouyer 			continue;
    584  1.1.2.1  bouyer 		if (ci->ci_data.cpu_idlelwp == NULL)
    585  1.1.2.1  bouyer 			continue;
    586  1.1.2.1  bouyer 		if ((ci->ci_flags & CPUF_PRESENT) == 0)
    587  1.1.2.1  bouyer 			continue;
    588  1.1.2.1  bouyer 		cpu_init_idle_lwp(ci);
    589  1.1.2.1  bouyer 	}
    590  1.1.2.1  bouyer }
    591  1.1.2.1  bouyer 
    592  1.1.2.1  bouyer void
    593  1.1.2.1  bouyer cpu_start_secondary (ci)
    594  1.1.2.1  bouyer 	struct cpu_info *ci;
    595  1.1.2.1  bouyer {
    596  1.1.2.1  bouyer 	int i;
    597  1.1.2.1  bouyer 	struct pmap *kpm = pmap_kernel();
    598  1.1.2.1  bouyer 	extern u_int32_t mp_pdirpa;
    599  1.1.2.1  bouyer 
    600  1.1.2.1  bouyer 	mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */
    601  1.1.2.1  bouyer 
    602  1.1.2.1  bouyer 	ci->ci_flags |= CPUF_AP;
    603  1.1.2.1  bouyer 
    604  1.1.2.1  bouyer 	printf("%s: starting\n", ci->ci_dev->dv_xname);
    605  1.1.2.1  bouyer 
    606  1.1.2.1  bouyer 	ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
    607  1.1.2.1  bouyer 	CPU_STARTUP(ci);
    608  1.1.2.1  bouyer 
    609  1.1.2.1  bouyer 	/*
    610  1.1.2.1  bouyer 	 * wait for it to become ready
    611  1.1.2.1  bouyer 	 */
    612  1.1.2.1  bouyer 	for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i>0;i--) {
    613  1.1.2.1  bouyer 		delay(10);
    614  1.1.2.1  bouyer 	}
    615  1.1.2.1  bouyer 	if (! (ci->ci_flags & CPUF_PRESENT)) {
    616  1.1.2.1  bouyer 		printf("%s: failed to become ready\n", ci->ci_dev->dv_xname);
    617  1.1.2.1  bouyer #if defined(MPDEBUG) && defined(DDB)
    618  1.1.2.1  bouyer 		printf("dropping into debugger; continue from here to resume boot\n");
    619  1.1.2.1  bouyer 		Debugger();
    620  1.1.2.1  bouyer #endif
    621  1.1.2.1  bouyer 	}
    622  1.1.2.1  bouyer 
    623  1.1.2.1  bouyer 	CPU_START_CLEANUP(ci);
    624  1.1.2.1  bouyer }
    625  1.1.2.1  bouyer 
    626  1.1.2.1  bouyer void
    627  1.1.2.1  bouyer cpu_boot_secondary(ci)
    628  1.1.2.1  bouyer 	struct cpu_info *ci;
    629  1.1.2.1  bouyer {
    630  1.1.2.1  bouyer 	int i;
    631  1.1.2.1  bouyer 
    632  1.1.2.1  bouyer 	ci->ci_flags |= CPUF_GO; /* XXX atomic */
    633  1.1.2.1  bouyer 
    634  1.1.2.1  bouyer 	for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i>0;i--) {
    635  1.1.2.1  bouyer 		delay(10);
    636  1.1.2.1  bouyer 	}
    637  1.1.2.1  bouyer 	if (! (ci->ci_flags & CPUF_RUNNING)) {
    638  1.1.2.1  bouyer 		printf("CPU failed to start\n");
    639  1.1.2.1  bouyer #if defined(MPDEBUG) && defined(DDB)
    640  1.1.2.1  bouyer 		printf("dropping into debugger; continue from here to resume boot\n");
    641  1.1.2.1  bouyer 		Debugger();
    642  1.1.2.1  bouyer #endif
    643  1.1.2.1  bouyer 	}
    644  1.1.2.1  bouyer }
    645  1.1.2.1  bouyer 
    646  1.1.2.1  bouyer /*
    647  1.1.2.1  bouyer  * The CPU ends up here when its ready to run
    648  1.1.2.1  bouyer  * This is called from code in mptramp.s; at this point, we are running
    649  1.1.2.1  bouyer  * in the idle pcb/idle stack of the new CPU.  When this function returns,
    650  1.1.2.1  bouyer  * this processor will enter the idle loop and start looking for work.
    651  1.1.2.1  bouyer  *
    652  1.1.2.1  bouyer  * XXX should share some of this with init386 in machdep.c
    653  1.1.2.1  bouyer  */
    654  1.1.2.1  bouyer void
    655  1.1.2.1  bouyer cpu_hatch(void *v)
    656  1.1.2.1  bouyer {
    657  1.1.2.1  bouyer 	struct cpu_info *ci = (struct cpu_info *)v;
    658  1.1.2.1  bouyer 	int s;
    659  1.1.2.2  bouyer #ifdef __x86_64__
    660  1.1.2.2  bouyer         cpu_init_msrs(ci);
    661  1.1.2.2  bouyer #endif
    662  1.1.2.1  bouyer 
    663  1.1.2.1  bouyer 	cpu_probe_features(ci);
    664  1.1.2.1  bouyer 	cpu_feature &= ci->ci_feature_flags;
    665  1.1.2.1  bouyer 	/* not on Xen... */
    666  1.1.2.1  bouyer 	cpu_feature &= ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX);
    667  1.1.2.1  bouyer 
    668  1.1.2.1  bouyer #ifdef DEBUG
    669  1.1.2.1  bouyer 	if (ci->ci_flags & CPUF_PRESENT)
    670  1.1.2.1  bouyer 		panic("%s: already running!?", ci->ci_dev->dv_xname);
    671  1.1.2.1  bouyer #endif
    672  1.1.2.1  bouyer 
    673  1.1.2.1  bouyer 	ci->ci_flags |= CPUF_PRESENT;
    674  1.1.2.1  bouyer 
    675  1.1.2.1  bouyer 	lapic_enable();
    676  1.1.2.1  bouyer 	lapic_initclocks();
    677  1.1.2.1  bouyer 
    678  1.1.2.1  bouyer 	while ((ci->ci_flags & CPUF_GO) == 0)
    679  1.1.2.1  bouyer 		delay(10);
    680  1.1.2.1  bouyer #ifdef DEBUG
    681  1.1.2.1  bouyer 	if (ci->ci_flags & CPUF_RUNNING)
    682  1.1.2.1  bouyer 		panic("%s: already running!?", ci->ci_dev->dv_xname);
    683  1.1.2.1  bouyer #endif
    684  1.1.2.1  bouyer 
    685  1.1.2.1  bouyer 	lcr0(ci->ci_data.cpu_idlelwp->l_addr->u_pcb.pcb_cr0);
    686  1.1.2.1  bouyer 	cpu_init_idt();
    687  1.1.2.1  bouyer 	lapic_set_lvt();
    688  1.1.2.1  bouyer 	gdt_init_cpu(ci);
    689  1.1.2.1  bouyer 	npxinit(ci);
    690  1.1.2.1  bouyer 
    691  1.1.2.1  bouyer 	lldt(GSEL(GLDT_SEL, SEL_KPL));
    692  1.1.2.1  bouyer 
    693  1.1.2.1  bouyer 	cpu_init(ci);
    694  1.1.2.1  bouyer 
    695  1.1.2.1  bouyer 	s = splhigh();
    696  1.1.2.1  bouyer 	lapic_tpr = 0;
    697  1.1.2.1  bouyer 	enable_intr();
    698  1.1.2.1  bouyer 
    699  1.1.2.1  bouyer 	printf("%s: CPU %ld running\n",ci->ci_dev->dv_xname, ci->ci_cpuid);
    700  1.1.2.1  bouyer #if defined(I586_CPU) || defined(I686_CPU)
    701  1.1.2.1  bouyer 	if (ci->ci_feature_flags & CPUID_TSC)
    702  1.1.2.1  bouyer 		cc_microset(ci);
    703  1.1.2.1  bouyer #endif
    704  1.1.2.1  bouyer 	splx(s);
    705  1.1.2.1  bouyer }
    706  1.1.2.1  bouyer 
    707  1.1.2.1  bouyer #if defined(DDB)
    708  1.1.2.1  bouyer 
    709  1.1.2.1  bouyer #include <ddb/db_output.h>
    710  1.1.2.1  bouyer #include <machine/db_machdep.h>
    711  1.1.2.1  bouyer 
    712  1.1.2.1  bouyer /*
    713  1.1.2.1  bouyer  * Dump CPU information from ddb.
    714  1.1.2.1  bouyer  */
    715  1.1.2.1  bouyer void
    716  1.1.2.1  bouyer cpu_debug_dump(void)
    717  1.1.2.1  bouyer {
    718  1.1.2.1  bouyer 	struct cpu_info *ci;
    719  1.1.2.1  bouyer 	CPU_INFO_ITERATOR cii;
    720  1.1.2.1  bouyer 
    721  1.1.2.1  bouyer 	db_printf("addr		dev	id	flags	ipis	curproc		fpcurproc\n");
    722  1.1.2.1  bouyer 	for (CPU_INFO_FOREACH(cii, ci)) {
    723  1.1.2.1  bouyer 		db_printf("%p	%s	%ld	%x	%x	%10p	%10p\n",
    724  1.1.2.1  bouyer 		    ci,
    725  1.1.2.1  bouyer 		    ci->ci_dev == NULL ? "BOOT" : ci->ci_dev->dv_xname,
    726  1.1.2.1  bouyer 		    ci->ci_cpuid,
    727  1.1.2.1  bouyer 		    ci->ci_flags, ci->ci_ipis,
    728  1.1.2.1  bouyer 		    ci->ci_curlwp,
    729  1.1.2.1  bouyer 		    ci->ci_fpcurlwp);
    730  1.1.2.1  bouyer 	}
    731  1.1.2.1  bouyer }
    732  1.1.2.1  bouyer #endif
    733  1.1.2.1  bouyer 
    734  1.1.2.1  bouyer static void
    735  1.1.2.1  bouyer cpu_copy_trampoline()
    736  1.1.2.1  bouyer {
    737  1.1.2.1  bouyer 	/*
    738  1.1.2.1  bouyer 	 * Copy boot code.
    739  1.1.2.1  bouyer 	 */
    740  1.1.2.1  bouyer 	extern u_char cpu_spinup_trampoline[];
    741  1.1.2.1  bouyer 	extern u_char cpu_spinup_trampoline_end[];
    742  1.1.2.1  bouyer 	pmap_kenter_pa((vaddr_t)MP_TRAMPOLINE,	/* virtual */
    743  1.1.2.1  bouyer 	    (paddr_t)MP_TRAMPOLINE,	/* physical */
    744  1.1.2.1  bouyer 	    VM_PROT_ALL);		/* protection */
    745  1.1.2.1  bouyer 	memcpy((void *)MP_TRAMPOLINE,
    746  1.1.2.1  bouyer 	    cpu_spinup_trampoline,
    747  1.1.2.1  bouyer 	    cpu_spinup_trampoline_end-cpu_spinup_trampoline);
    748  1.1.2.1  bouyer }
    749  1.1.2.1  bouyer 
    750  1.1.2.1  bouyer #endif
    751  1.1.2.1  bouyer 
    752  1.1.2.1  bouyer 
    753  1.1.2.1  bouyer /* XXX */
    754  1.1.2.1  bouyer #define IDTVEC(name)	__CONCAT(X, name)
    755  1.1.2.1  bouyer typedef void (vector)(void);
    756  1.1.2.1  bouyer extern vector IDTVEC(tss_trap08);
    757  1.1.2.1  bouyer #ifdef DDB
    758  1.1.2.1  bouyer extern vector Xintrddbipi;
    759  1.1.2.1  bouyer extern int ddb_vec;
    760  1.1.2.1  bouyer #endif
    761  1.1.2.1  bouyer 
    762  1.1.2.1  bouyer static void
    763  1.1.2.1  bouyer cpu_set_tss_gates(struct cpu_info *ci)
    764  1.1.2.1  bouyer {
    765  1.1.2.1  bouyer #if defined(DDB) && defined(MULTIPROCESSOR)
    766  1.1.2.1  bouyer 	/*
    767  1.1.2.1  bouyer 	 * Set up separate handler for the DDB IPI, so that it doesn't
    768  1.1.2.1  bouyer 	 * stomp on a possibly corrupted stack.
    769  1.1.2.1  bouyer 	 *
    770  1.1.2.1  bouyer 	 * XXX overwriting the gate set in db_machine_init.
    771  1.1.2.1  bouyer 	 * Should rearrange the code so that it's set only once.
    772  1.1.2.1  bouyer 	 */
    773  1.1.2.1  bouyer 	ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
    774  1.1.2.1  bouyer 	    UVM_KMF_WIRED);
    775  1.1.2.1  bouyer 	cpu_init_tss(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
    776  1.1.2.1  bouyer 	    Xintrddbipi);
    777  1.1.2.1  bouyer 
    778  1.1.2.1  bouyer 	setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
    779  1.1.2.1  bouyer 	    SDT_SYS386TSS, SEL_KPL, 0, 0);
    780  1.1.2.1  bouyer 	ci->ci_gdt[GIPITSS_SEL].sd = sd;
    781  1.1.2.1  bouyer 
    782  1.1.2.1  bouyer 	setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
    783  1.1.2.1  bouyer 	    GSEL(GIPITSS_SEL, SEL_KPL));
    784  1.1.2.1  bouyer #endif
    785  1.1.2.1  bouyer }
    786  1.1.2.1  bouyer 
    787  1.1.2.1  bouyer int
    788  1.1.2.1  bouyer mp_cpu_start(struct cpu_info *ci)
    789  1.1.2.1  bouyer {
    790  1.1.2.1  bouyer #if 0
    791  1.1.2.1  bouyer #if NLAPIC > 0
    792  1.1.2.1  bouyer 	int error;
    793  1.1.2.1  bouyer #endif
    794  1.1.2.1  bouyer 	unsigned short dwordptr[2];
    795  1.1.2.1  bouyer 
    796  1.1.2.1  bouyer 	/*
    797  1.1.2.1  bouyer 	 * "The BSP must initialize CMOS shutdown code to 0Ah ..."
    798  1.1.2.1  bouyer 	 */
    799  1.1.2.1  bouyer 
    800  1.1.2.1  bouyer 	outb(IO_RTC, NVRAM_RESET);
    801  1.1.2.1  bouyer 	outb(IO_RTC+1, NVRAM_RESET_JUMP);
    802  1.1.2.1  bouyer 
    803  1.1.2.1  bouyer 	/*
    804  1.1.2.1  bouyer 	 * "and the warm reset vector (DWORD based at 40:67) to point
    805  1.1.2.1  bouyer 	 * to the AP startup code ..."
    806  1.1.2.1  bouyer 	 */
    807  1.1.2.1  bouyer 
    808  1.1.2.1  bouyer 	dwordptr[0] = 0;
    809  1.1.2.1  bouyer 	dwordptr[1] = MP_TRAMPOLINE >> 4;
    810  1.1.2.1  bouyer 
    811  1.1.2.1  bouyer 	pmap_kenter_pa (0, 0, VM_PROT_READ|VM_PROT_WRITE);
    812  1.1.2.1  bouyer 	memcpy ((u_int8_t *) 0x467, dwordptr, 4);
    813  1.1.2.1  bouyer 	pmap_kremove (0, PAGE_SIZE);
    814  1.1.2.1  bouyer 
    815  1.1.2.1  bouyer #if NLAPIC > 0
    816  1.1.2.1  bouyer 	/*
    817  1.1.2.1  bouyer 	 * ... prior to executing the following sequence:"
    818  1.1.2.1  bouyer 	 */
    819  1.1.2.1  bouyer 
    820  1.1.2.1  bouyer 	if (ci->ci_flags & CPUF_AP) {
    821  1.1.2.1  bouyer 		if ((error = x86_ipi_init(ci->ci_apicid)) != 0)
    822  1.1.2.1  bouyer 			return error;
    823  1.1.2.1  bouyer 
    824  1.1.2.1  bouyer 		delay(10000);
    825  1.1.2.1  bouyer 
    826  1.1.2.1  bouyer 		if (cpu_feature & CPUID_APIC) {
    827  1.1.2.1  bouyer 
    828  1.1.2.1  bouyer 			if ((error = x86_ipi(MP_TRAMPOLINE/PAGE_SIZE,
    829  1.1.2.1  bouyer 					     ci->ci_apicid,
    830  1.1.2.1  bouyer 					     LAPIC_DLMODE_STARTUP)) != 0)
    831  1.1.2.1  bouyer 				return error;
    832  1.1.2.1  bouyer 			delay(200);
    833  1.1.2.1  bouyer 
    834  1.1.2.1  bouyer 			if ((error = x86_ipi(MP_TRAMPOLINE/PAGE_SIZE,
    835  1.1.2.1  bouyer 					     ci->ci_apicid,
    836  1.1.2.1  bouyer 					     LAPIC_DLMODE_STARTUP)) != 0)
    837  1.1.2.1  bouyer 				return error;
    838  1.1.2.1  bouyer 			delay(200);
    839  1.1.2.1  bouyer 		}
    840  1.1.2.1  bouyer 	}
    841  1.1.2.1  bouyer #endif
    842  1.1.2.1  bouyer #endif /* 0 */
    843  1.1.2.1  bouyer 	return 0;
    844  1.1.2.1  bouyer }
    845  1.1.2.1  bouyer 
    846  1.1.2.1  bouyer void
    847  1.1.2.1  bouyer mp_cpu_start_cleanup(struct cpu_info *ci)
    848  1.1.2.1  bouyer {
    849  1.1.2.1  bouyer #if 0
    850  1.1.2.1  bouyer 	/*
    851  1.1.2.1  bouyer 	 * Ensure the NVRAM reset byte contains something vaguely sane.
    852  1.1.2.1  bouyer 	 */
    853  1.1.2.1  bouyer 
    854  1.1.2.1  bouyer 	outb(IO_RTC, NVRAM_RESET);
    855  1.1.2.1  bouyer 	outb(IO_RTC+1, NVRAM_RESET_RST);
    856  1.1.2.1  bouyer #endif
    857  1.1.2.1  bouyer }
    858  1.1.2.1  bouyer 
    859  1.1.2.1  bouyer #ifdef __x86_64__
    860  1.1.2.1  bouyer 
    861  1.1.2.1  bouyer void
    862  1.1.2.1  bouyer cpu_init_msrs(struct cpu_info *ci)
    863  1.1.2.1  bouyer {
    864  1.1.2.1  bouyer 	HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
    865  1.1.2.1  bouyer 	HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (u_int64_t) ci);
    866  1.1.2.1  bouyer 	HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
    867  1.1.2.1  bouyer }
    868  1.1.2.1  bouyer #endif	/* __x86_64__ */
    869