xref: /onnv-gate/usr/src/uts/i86pc/os/mp_startup.c (revision 3021:900097ae20cf)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51455Sandrei  * Common Development and Distribution License (the "License").
61455Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221251Skchow  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/thread.h>
300Sstevel@tonic-gate #include <sys/cpuvar.h>
310Sstevel@tonic-gate #include <sys/t_lock.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/proc.h>
340Sstevel@tonic-gate #include <sys/disp.h>
350Sstevel@tonic-gate #include <sys/mmu.h>
360Sstevel@tonic-gate #include <sys/class.h>
370Sstevel@tonic-gate #include <sys/cmn_err.h>
380Sstevel@tonic-gate #include <sys/debug.h>
390Sstevel@tonic-gate #include <sys/asm_linkage.h>
400Sstevel@tonic-gate #include <sys/x_call.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/var.h>
430Sstevel@tonic-gate #include <sys/vtrace.h>
440Sstevel@tonic-gate #include <vm/hat.h>
450Sstevel@tonic-gate #include <sys/mmu.h>
460Sstevel@tonic-gate #include <vm/as.h>
470Sstevel@tonic-gate #include <vm/seg_kmem.h>
480Sstevel@tonic-gate #include <sys/segments.h>
490Sstevel@tonic-gate #include <sys/kmem.h>
500Sstevel@tonic-gate #include <sys/stack.h>
510Sstevel@tonic-gate #include <sys/smp_impldefs.h>
520Sstevel@tonic-gate #include <sys/x86_archext.h>
530Sstevel@tonic-gate #include <sys/machsystm.h>
540Sstevel@tonic-gate #include <sys/traptrace.h>
550Sstevel@tonic-gate #include <sys/clock.h>
560Sstevel@tonic-gate #include <sys/cpc_impl.h>
570Sstevel@tonic-gate #include <sys/chip.h>
580Sstevel@tonic-gate #include <sys/dtrace.h>
590Sstevel@tonic-gate #include <sys/archsystm.h>
600Sstevel@tonic-gate #include <sys/fp.h>
610Sstevel@tonic-gate #include <sys/reboot.h>
620Sstevel@tonic-gate #include <sys/kdi.h>
630Sstevel@tonic-gate #include <vm/hat_i86.h>
640Sstevel@tonic-gate #include <sys/memnode.h>
65938Sesaxe #include <sys/pci_cfgspace.h>
661414Scindi #include <sys/cpu_module.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate struct cpu	cpus[1];			/* CPU data */
690Sstevel@tonic-gate struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
700Sstevel@tonic-gate cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Useful for disabling MP bring-up for an MP capable kernel
740Sstevel@tonic-gate  * (a kernel that was built with MP defined)
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate int use_mp = 1;
770Sstevel@tonic-gate 
782006Sandrei /*
792006Sandrei  * To be set by a PSM to indicate what CPUs are available on the system.
802006Sandrei  */
812006Sandrei cpuset_t mp_cpus = 1;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * This variable is used by the hat layer to decide whether or not
850Sstevel@tonic-gate  * critical sections are needed to prevent race conditions.  For sun4m,
860Sstevel@tonic-gate  * this variable is set once enough MP initialization has been done in
870Sstevel@tonic-gate  * order to allow cross calls.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate int flushes_require_xcalls = 0;
902006Sandrei cpuset_t	cpu_ready_set = 1;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate extern	void	real_mode_start(void);
930Sstevel@tonic-gate extern	void	real_mode_end(void);
940Sstevel@tonic-gate static 	void	mp_startup(void);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static void cpu_sep_enable(void);
970Sstevel@tonic-gate static void cpu_sep_disable(void);
980Sstevel@tonic-gate static void cpu_asysc_enable(void);
990Sstevel@tonic-gate static void cpu_asysc_disable(void);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate extern int tsc_gethrtime_enable;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * Init CPU info - get CPU type info for processor_info system call.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate void
1070Sstevel@tonic-gate init_cpu_info(struct cpu *cp)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	processor_info_t *pi = &cp->cpu_type_info;
1100Sstevel@tonic-gate 	char buf[CPU_IDSTRLEN];
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/*
1130Sstevel@tonic-gate 	 * Get clock-frequency property for the CPU.
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	pi->pi_clock = cpu_freq;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1180Sstevel@tonic-gate 	if (fpu_exists)
1190Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	(void) cpuid_getidstr(cp, buf, sizeof (buf));
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1240Sstevel@tonic-gate 	(void) strcpy(cp->cpu_idstr, buf);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	(void) cpuid_getbrandstr(cp, buf, sizeof (buf));
1290Sstevel@tonic-gate 	cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1300Sstevel@tonic-gate 	(void) strcpy(cp->cpu_brandstr, buf);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * Configure syscall support on this CPU.
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate /*ARGSUSED*/
1390Sstevel@tonic-gate static void
1400Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	kpreempt_disable();
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #if defined(__amd64)
1450Sstevel@tonic-gate 	if (x86_feature & X86_ASYSC) {
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate #if !defined(__lint)
1480Sstevel@tonic-gate 		/*
1490Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1500Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1510Sstevel@tonic-gate 		 * here.
1520Sstevel@tonic-gate 		 */
1530Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1540Sstevel@tonic-gate 		ASSERT(UDS_SEL == U32CS_SEL + 8);
1550Sstevel@tonic-gate 		ASSERT(UCS_SEL == U32CS_SEL + 16);
1560Sstevel@tonic-gate #endif
1570Sstevel@tonic-gate 		/*
1580Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1590Sstevel@tonic-gate 		 */
1600Sstevel@tonic-gate 		cpu_asysc_enable();
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 		/*
1630Sstevel@tonic-gate 		 * Program the magic registers ..
1640Sstevel@tonic-gate 		 */
165770Skucharsk 		wrmsr(MSR_AMD_STAR, ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) <<
166770Skucharsk 		    32);
167770Skucharsk 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
168770Skucharsk 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 		/*
1710Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
1720Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
1730Sstevel@tonic-gate 		 */
174770Skucharsk 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate #endif
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	/*
1790Sstevel@tonic-gate 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
1800Sstevel@tonic-gate 	 * hard to use syscall/sysret, and it is more portable anyway.
1810Sstevel@tonic-gate 	 *
1820Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
1830Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
1840Sstevel@tonic-gate 	 */
1850Sstevel@tonic-gate 	if (x86_feature & X86_SEP) {
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate #if !defined(__lint)
1880Sstevel@tonic-gate 		/*
1890Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
1900Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1910Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
1920Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
1930Sstevel@tonic-gate 		 * Instruction Set Reference"
1940Sstevel@tonic-gate 		 */
1950Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
1980Sstevel@tonic-gate 		ASSERT32(UDS_SEL == UCS_SEL + 8);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
2010Sstevel@tonic-gate 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
2020Sstevel@tonic-gate #endif
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		cpu_sep_enable();
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		/*
2070Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2080Sstevel@tonic-gate 		 * via a context handler.
2090Sstevel@tonic-gate 		 */
210770Skucharsk 		wrmsr(MSR_INTC_SEP_ESP, 0ULL);
211770Skucharsk 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	kpreempt_enable();
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * Multiprocessor initialization.
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2210Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate static void
2240Sstevel@tonic-gate mp_startup_init(int cpun)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate #if defined(__amd64)
2270Sstevel@tonic-gate extern void *long_mode_64(void);
2280Sstevel@tonic-gate #endif	/* __amd64 */
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	struct cpu *cp;
2310Sstevel@tonic-gate 	struct tss *ntss;
2320Sstevel@tonic-gate 	kthread_id_t tp;
2330Sstevel@tonic-gate 	caddr_t	sp;
2340Sstevel@tonic-gate 	int size;
2350Sstevel@tonic-gate 	proc_t *procp;
2360Sstevel@tonic-gate 	extern void idle();
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	struct cpu_tables *tablesp;
2390Sstevel@tonic-gate 	rm_platter_t *real_mode_platter = (rm_platter_t *)rm_platter_va;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate #ifdef TRAPTRACE
2420Sstevel@tonic-gate 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
2430Sstevel@tonic-gate #endif
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if ((cp = kmem_zalloc(sizeof (*cp), KM_NOSLEEP)) == NULL) {
2480Sstevel@tonic-gate 		panic("mp_startup_init: cpu%d: "
2490Sstevel@tonic-gate 		    "no memory for cpu structure", cpun);
2500Sstevel@tonic-gate 		/*NOTREACHED*/
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 	procp = curthread->t_procp;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2550Sstevel@tonic-gate 	/*
2560Sstevel@tonic-gate 	 * Initialize the dispatcher first.
2570Sstevel@tonic-gate 	 */
2580Sstevel@tonic-gate 	disp_cpu_init(cp);
2590Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2600Sstevel@tonic-gate 
261414Skchow 	cpu_vm_data_init(cp);
262414Skchow 
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
2650Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
2660Sstevel@tonic-gate 	 * when the CPU starts running.
2670Sstevel@tonic-gate 	 */
2680Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
2690Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	/*
2720Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
2730Sstevel@tonic-gate 	 * as soon as the CPU comes online.
2740Sstevel@tonic-gate 	 *
2750Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
2760Sstevel@tonic-gate 	 * thread_create().
2770Sstevel@tonic-gate 	 */
2780Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
2790Sstevel@tonic-gate 	tp->t_preempt = 1;
2800Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
2810Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
2820Sstevel@tonic-gate 	tp->t_cpu = cp;
2830Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	/*
2860Sstevel@tonic-gate 	 * Setup thread to start in mp_startup.
2870Sstevel@tonic-gate 	 */
2880Sstevel@tonic-gate 	sp = tp->t_stk;
2890Sstevel@tonic-gate 	tp->t_pc = (uintptr_t)mp_startup;
2900Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	cp->cpu_id = cpun;
2930Sstevel@tonic-gate 	cp->cpu_self = cp;
2940Sstevel@tonic-gate 	cp->cpu_thread = tp;
2950Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
2960Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
2970Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	/*
3001482Ssethg 	 * cpu_base_spl must be set explicitly here to prevent any blocking
3011482Ssethg 	 * operations in mp_startup from causing the spl of the cpu to drop
3021482Ssethg 	 * to 0 (allowing device interrupts before we're ready) in resume().
3031482Ssethg 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
3041482Ssethg 	 * As an extra bit of security on DEBUG kernels, this is enforced with
3051482Ssethg 	 * an assertion in mp_startup() -- before cpu_base_spl is set to its
3061482Ssethg 	 * proper value.
3071482Ssethg 	 */
3081482Ssethg 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
3091482Ssethg 
3101482Ssethg 	/*
3110Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
3120Sstevel@tonic-gate 	 */
3130Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	tp->t_preempt = 1;
3180Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3190Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3200Sstevel@tonic-gate 	tp->t_cpu = cp;
3210Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/*
32460Sesaxe 	 * Bootstrap the CPU for CMT aware scheduling
32560Sesaxe 	 * The rest of the initialization will happen from
32660Sesaxe 	 * mp_startup()
32760Sesaxe 	 */
32860Sesaxe 	chip_bootstrap_cpu(cp);
32960Sesaxe 
33060Sesaxe 	/*
3310Sstevel@tonic-gate 	 * Perform CPC intialization on the new CPU.
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	kcpc_hw_init(cp);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3360Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
3370Sstevel@tonic-gate 	 * for each CPU.
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	/*
3430Sstevel@tonic-gate 	 * Allocate space for page directory, stack, tss, gdt and idt.
3440Sstevel@tonic-gate 	 * This assumes that kmem_alloc will return memory which is aligned
3450Sstevel@tonic-gate 	 * to the next higher power of 2 or a page(if size > MAXABIG)
3460Sstevel@tonic-gate 	 * If this assumption goes wrong at any time due to change in
3470Sstevel@tonic-gate 	 * kmem alloc, things may not work as the page directory has to be
3480Sstevel@tonic-gate 	 * page aligned
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 	if ((tablesp = kmem_zalloc(sizeof (*tablesp), KM_NOSLEEP)) == NULL)
3510Sstevel@tonic-gate 		panic("mp_startup_init: cpu%d cannot allocate tables", cpun);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if ((uintptr_t)tablesp & ~MMU_STD_PAGEMASK) {
3540Sstevel@tonic-gate 		kmem_free(tablesp, sizeof (struct cpu_tables));
3550Sstevel@tonic-gate 		size = sizeof (struct cpu_tables) + MMU_STD_PAGESIZE;
3560Sstevel@tonic-gate 		tablesp = kmem_zalloc(size, KM_NOSLEEP);
3570Sstevel@tonic-gate 		tablesp = (struct cpu_tables *)
3580Sstevel@tonic-gate 		    (((uintptr_t)tablesp + MMU_STD_PAGESIZE) &
3590Sstevel@tonic-gate 		    MMU_STD_PAGEMASK);
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	ntss = cp->cpu_tss = &tablesp->ct_tss;
3631626Srab 
3641626Srab 	if ((tablesp->ct_gdt = kmem_zalloc(PAGESIZE, KM_NOSLEEP)) == NULL)
3651626Srab 		panic("mp_startup_init: cpu%d cannot allocate GDT", cpun);
3660Sstevel@tonic-gate 	cp->cpu_gdt = tablesp->ct_gdt;
3670Sstevel@tonic-gate 	bcopy(CPU->cpu_gdt, cp->cpu_gdt, NGDT * (sizeof (user_desc_t)));
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate #if defined(__amd64)
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/*
3720Sstevel@tonic-gate 	 * #DF (double fault).
3730Sstevel@tonic-gate 	 */
3740Sstevel@tonic-gate 	ntss->tss_ist1 =
3750Sstevel@tonic-gate 	    (uint64_t)&tablesp->ct_stack[sizeof (tablesp->ct_stack)];
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate #elif defined(__i386)
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	ntss->tss_esp0 = ntss->tss_esp1 = ntss->tss_esp2 = ntss->tss_esp =
3800Sstevel@tonic-gate 	    (uint32_t)&tablesp->ct_stack[sizeof (tablesp->ct_stack)];
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	ntss->tss_ss0 = ntss->tss_ss1 = ntss->tss_ss2 = ntss->tss_ss = KDS_SEL;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	ntss->tss_eip = (uint32_t)mp_startup;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	ntss->tss_cs = KCS_SEL;
3870Sstevel@tonic-gate 	ntss->tss_fs = KFS_SEL;
3880Sstevel@tonic-gate 	ntss->tss_gs = KGS_SEL;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	/*
3910Sstevel@tonic-gate 	 * setup kernel %gs.
3920Sstevel@tonic-gate 	 */
3930Sstevel@tonic-gate 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
3940Sstevel@tonic-gate 	    SEL_KPL, 0, 1);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate #endif	/* __i386 */
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	/*
3990Sstevel@tonic-gate 	 * Set I/O bit map offset equal to size of TSS segment limit
4000Sstevel@tonic-gate 	 * for no I/O permission map. This will cause all user I/O
4010Sstevel@tonic-gate 	 * instructions to generate #gp fault.
4020Sstevel@tonic-gate 	 */
4030Sstevel@tonic-gate 	ntss->tss_bitmapbase = sizeof (*ntss);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	/*
4060Sstevel@tonic-gate 	 * setup kernel tss.
4070Sstevel@tonic-gate 	 */
4080Sstevel@tonic-gate 	set_syssegd((system_desc_t *)&cp->cpu_gdt[GDT_KTSS], cp->cpu_tss,
4090Sstevel@tonic-gate 	    sizeof (*cp->cpu_tss) -1, SDT_SYSTSS, SEL_KPL);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	/*
4120Sstevel@tonic-gate 	 * If we have more than one node, each cpu gets a copy of IDT
4130Sstevel@tonic-gate 	 * local to its node. If this is a Pentium box, we use cpu 0's
4140Sstevel@tonic-gate 	 * IDT. cpu 0's IDT has been made read-only to workaround the
4150Sstevel@tonic-gate 	 * cmpxchgl register bug
4160Sstevel@tonic-gate 	 */
4170Sstevel@tonic-gate 	cp->cpu_idt = CPU->cpu_idt;
4180Sstevel@tonic-gate 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
4190Sstevel@tonic-gate 		cp->cpu_idt = kmem_alloc(sizeof (idt0), KM_SLEEP);
4200Sstevel@tonic-gate 		bcopy(idt0, cp->cpu_idt, sizeof (idt0));
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	/*
4240Sstevel@tonic-gate 	 * Get interrupt priority data from cpu 0
4250Sstevel@tonic-gate 	 */
4260Sstevel@tonic-gate 	cp->cpu_pri_data = CPU->cpu_pri_data;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	hat_cpu_online(cp);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	/* Should remove all entries for the current process/thread here */
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/*
4330Sstevel@tonic-gate 	 * Fill up the real mode platter to make it easy for real mode code to
4340Sstevel@tonic-gate 	 * kick it off. This area should really be one passed by boot to kernel
4350Sstevel@tonic-gate 	 * and guaranteed to be below 1MB and aligned to 16 bytes. Should also
4360Sstevel@tonic-gate 	 * have identical physical and virtual address in paged mode.
4370Sstevel@tonic-gate 	 */
4380Sstevel@tonic-gate 	real_mode_platter->rm_idt_base = cp->cpu_idt;
4390Sstevel@tonic-gate 	real_mode_platter->rm_idt_lim = sizeof (idt0) - 1;
4400Sstevel@tonic-gate 	real_mode_platter->rm_gdt_base = cp->cpu_gdt;
4410Sstevel@tonic-gate 	real_mode_platter->rm_gdt_lim = sizeof (gdt0) -1;
4420Sstevel@tonic-gate 	real_mode_platter->rm_pdbr = getcr3();
4430Sstevel@tonic-gate 	real_mode_platter->rm_cpu = cpun;
4440Sstevel@tonic-gate 	real_mode_platter->rm_x86feature = x86_feature;
4450Sstevel@tonic-gate 	real_mode_platter->rm_cr4 = cr4_value;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate #if defined(__amd64)
4480Sstevel@tonic-gate 	if (getcr3() > 0xffffffffUL)
4490Sstevel@tonic-gate 		panic("Cannot initialize CPUs; kernel's 64-bit page tables\n"
4500Sstevel@tonic-gate 			"located above 4G in physical memory (@ 0x%llx).",
4510Sstevel@tonic-gate 			(unsigned long long)getcr3());
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * Setup pseudo-descriptors for temporary GDT and IDT for use ONLY
4550Sstevel@tonic-gate 	 * by code in real_mode_start():
4560Sstevel@tonic-gate 	 *
4570Sstevel@tonic-gate 	 * GDT[0]:  NULL selector
4580Sstevel@tonic-gate 	 * GDT[1]:  64-bit CS: Long = 1, Present = 1, bits 12, 11 = 1
4590Sstevel@tonic-gate 	 *
4600Sstevel@tonic-gate 	 * Clear the IDT as interrupts will be off and a limit of 0 will cause
4610Sstevel@tonic-gate 	 * the CPU to triple fault and reset on an NMI, seemingly as reasonable
4620Sstevel@tonic-gate 	 * a course of action as any other, though it may cause the entire
4630Sstevel@tonic-gate 	 * platform to reset in some cases...
4640Sstevel@tonic-gate 	 */
4650Sstevel@tonic-gate 	real_mode_platter->rm_temp_gdt[0] = 0ULL;
4660Sstevel@tonic-gate 	real_mode_platter->rm_temp_gdt[TEMPGDT_KCODE64] = 0x20980000000000ULL;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	real_mode_platter->rm_temp_gdt_lim = (ushort_t)
4690Sstevel@tonic-gate 	    (sizeof (real_mode_platter->rm_temp_gdt) - 1);
4700Sstevel@tonic-gate 	real_mode_platter->rm_temp_gdt_base = rm_platter_pa +
4710Sstevel@tonic-gate 	    (uint32_t)(&((rm_platter_t *)0)->rm_temp_gdt);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	real_mode_platter->rm_temp_idt_lim = 0;
4740Sstevel@tonic-gate 	real_mode_platter->rm_temp_idt_base = 0;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/*
4770Sstevel@tonic-gate 	 * Since the CPU needs to jump to protected mode using an identity
4780Sstevel@tonic-gate 	 * mapped address, we need to calculate it here.
4790Sstevel@tonic-gate 	 */
4800Sstevel@tonic-gate 	real_mode_platter->rm_longmode64_addr = rm_platter_pa +
4810Sstevel@tonic-gate 	    ((uint32_t)long_mode_64 - (uint32_t)real_mode_start);
4820Sstevel@tonic-gate #endif	/* __amd64 */
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate #ifdef TRAPTRACE
4850Sstevel@tonic-gate 	/*
4860Sstevel@tonic-gate 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers for this
4870Sstevel@tonic-gate 	 * CPU.
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
4900Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
4910Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
4920Sstevel@tonic-gate #endif
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/*
4950Sstevel@tonic-gate 	 * Record that we have another CPU.
4960Sstevel@tonic-gate 	 */
4970Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
4980Sstevel@tonic-gate 	/*
4990Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
5000Sstevel@tonic-gate 	 */
5011455Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
5040Sstevel@tonic-gate 	 * after mp_startup().
5050Sstevel@tonic-gate 	 */
5060Sstevel@tonic-gate 	cpu_add_unit(cp);
5070Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate /*
5110Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
5120Sstevel@tonic-gate  *
5130Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
5140Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
5150Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
5160Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
5170Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
5180Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
5190Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
5200Sstevel@tonic-gate  * system.
5210Sstevel@tonic-gate  *
522938Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
523938Sesaxe  * mp_startup() for all slave CPUs. Slaves process workaround_errata prior
524938Sesaxe  * to acknowledging their readiness to the master, so this routine will
525938Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
526938Sesaxe  * global data safe.
527938Sesaxe  *
528359Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
529359Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
5300Sstevel@tonic-gate  */
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
5330Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
5340Sstevel@tonic-gate #endif
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
5370Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
5380Sstevel@tonic-gate #endif
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
5410Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
5420Sstevel@tonic-gate #endif
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
5450Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
5460Sstevel@tonic-gate #endif
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
5490Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
5500Sstevel@tonic-gate #endif
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
5530Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
5540Sstevel@tonic-gate #endif
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
5570Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
5580Sstevel@tonic-gate #endif
5590Sstevel@tonic-gate 
560359Skucharsk #if defined(OPTERON_ERRATUM_131)
561359Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
562359Skucharsk #endif
5630Sstevel@tonic-gate 
564938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
565938Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
566938Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
567938Sesaxe #endif
568938Sesaxe 
5691582Skchow #if defined(OPTERON_WORKAROUND_6323525)
5701582Skchow int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
5711582Skchow #endif
5721582Skchow 
5730Sstevel@tonic-gate #define	WARNING(cpu, n)						\
5740Sstevel@tonic-gate 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %d",	\
5750Sstevel@tonic-gate 	    (cpu)->cpu_id, (n))
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate uint_t
5780Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	uint_t missing = 0;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	ASSERT(cpu == CPU);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	/*LINTED*/
5850Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
5860Sstevel@tonic-gate 		/*
5870Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
5880Sstevel@tonic-gate 		 */
5890Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
5900Sstevel@tonic-gate 		/*
5910Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
5920Sstevel@tonic-gate 		 */
5930Sstevel@tonic-gate #else
5940Sstevel@tonic-gate 		WARNING(cpu, 88);
5950Sstevel@tonic-gate 		missing++;
5960Sstevel@tonic-gate #endif
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
6000Sstevel@tonic-gate 		/*
6010Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
6020Sstevel@tonic-gate 		 */
6030Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
6040Sstevel@tonic-gate 		/*
6050Sstevel@tonic-gate 		 * fix is in trap.c
6060Sstevel@tonic-gate 		 */
6070Sstevel@tonic-gate 		opteron_erratum_91++;
6080Sstevel@tonic-gate #else
6090Sstevel@tonic-gate 		WARNING(cpu, 91);
6100Sstevel@tonic-gate 		missing++;
6110Sstevel@tonic-gate #endif
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
6150Sstevel@tonic-gate 		/*
6160Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
6170Sstevel@tonic-gate 		 */
6180Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
6190Sstevel@tonic-gate 		/*
6200Sstevel@tonic-gate 		 * fix is in trap.c
6210Sstevel@tonic-gate 		 */
6220Sstevel@tonic-gate 		opteron_erratum_93++;
6230Sstevel@tonic-gate #else
6240Sstevel@tonic-gate 		WARNING(cpu, 93);
6250Sstevel@tonic-gate 		missing++;
6260Sstevel@tonic-gate #endif
6270Sstevel@tonic-gate 	}
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	/*LINTED*/
6300Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
6310Sstevel@tonic-gate 		/*
6320Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
6330Sstevel@tonic-gate 		 */
6340Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
6350Sstevel@tonic-gate #if defined(_LP64)
6360Sstevel@tonic-gate 		/*
6370Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
6380Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
6390Sstevel@tonic-gate 		 * range mod 4G.
6400Sstevel@tonic-gate 		 */
6410Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
6420Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		/*LINTED*/
6450Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
6460Sstevel@tonic-gate #endif	/* _LP64 */
6470Sstevel@tonic-gate #else
6480Sstevel@tonic-gate 		WARNING(cpu, 95);
6490Sstevel@tonic-gate 		missing++;
6500Sstevel@tonic-gate #endif	/* OPTERON_ERRATUM_95 */
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
6540Sstevel@tonic-gate 		/*
6550Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
6560Sstevel@tonic-gate 		 */
6570Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
6580Sstevel@tonic-gate 		/*
6590Sstevel@tonic-gate 		 * fix is in trap.c
6600Sstevel@tonic-gate 		 */
6610Sstevel@tonic-gate 		opteron_erratum_100++;
6620Sstevel@tonic-gate #else
6630Sstevel@tonic-gate 		WARNING(cpu, 100);
6640Sstevel@tonic-gate 		missing++;
6650Sstevel@tonic-gate #endif
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	/*LINTED*/
6690Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
6700Sstevel@tonic-gate 		/*
6710Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
6720Sstevel@tonic-gate 		 * Some Processors
6730Sstevel@tonic-gate 		 */
6740Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
6750Sstevel@tonic-gate 		/*
6760Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
6770Sstevel@tonic-gate 		 * those processors)
6780Sstevel@tonic-gate 		 */
6790Sstevel@tonic-gate #else
6800Sstevel@tonic-gate 		WARNING(cpu, 108);
6810Sstevel@tonic-gate 		missing++;
6820Sstevel@tonic-gate #endif
6830Sstevel@tonic-gate 	}
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	/*LINTED*/
6860Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 109) > 0) {
6870Sstevel@tonic-gate 		/*
6880Sstevel@tonic-gate 		 * Certain Reverse REP MOVS May Produce Unpredictable Behaviour
6890Sstevel@tonic-gate 		 */
6900Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 		/* workaround is to print a warning to upgrade BIOS */
693770Skucharsk 		if (rdmsr(MSR_AMD_PATCHLEVEL) == 0)
6940Sstevel@tonic-gate 			opteron_erratum_109++;
6950Sstevel@tonic-gate #else
6960Sstevel@tonic-gate 		WARNING(cpu, 109);
6970Sstevel@tonic-gate 		missing++;
6980Sstevel@tonic-gate #endif
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 	/*LINTED*/
7010Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
7020Sstevel@tonic-gate 		/*
7030Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
7040Sstevel@tonic-gate 		 * Processor Hang
7050Sstevel@tonic-gate 		 */
7060Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
7070Sstevel@tonic-gate 		static int	lma;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 		if (opteron_erratum_121)
7100Sstevel@tonic-gate 			opteron_erratum_121++;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		/*
7130Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
7140Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
7150Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
7160Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
7170Sstevel@tonic-gate 		 */
7180Sstevel@tonic-gate 		if (lma == 0) {
7190Sstevel@tonic-gate 			/*
7200Sstevel@tonic-gate 			 * check LMA once: assume all cpus are in long mode
7210Sstevel@tonic-gate 			 * or not.
7220Sstevel@tonic-gate 			 */
7230Sstevel@tonic-gate 			lma = 1;
7240Sstevel@tonic-gate 
725770Skucharsk 			if (rdmsr(MSR_AMD_EFER) & AMD_EFER_LMA) {
7260Sstevel@tonic-gate 				if (hole_start) {
7270Sstevel@tonic-gate 					hole_start -= PAGESIZE;
7280Sstevel@tonic-gate 				} else {
7290Sstevel@tonic-gate 					/*
7300Sstevel@tonic-gate 					 * hole_start not yet initialized by
7310Sstevel@tonic-gate 					 * mmu_init. Initialize hole_start
7320Sstevel@tonic-gate 					 * with value to be subtracted.
7330Sstevel@tonic-gate 					 */
7340Sstevel@tonic-gate 					hole_start = PAGESIZE;
7350Sstevel@tonic-gate 				}
7360Sstevel@tonic-gate 				opteron_erratum_121++;
7370Sstevel@tonic-gate 			}
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate #else
7400Sstevel@tonic-gate 		WARNING(cpu, 121);
7410Sstevel@tonic-gate 		missing++;
7420Sstevel@tonic-gate #endif
7430Sstevel@tonic-gate 	}
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	/*LINTED*/
7460Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 122) > 0) {
7470Sstevel@tonic-gate 		/*
7480Sstevel@tonic-gate 		 * TLB Flush Filter May Cause Cohenrency Problem in
7490Sstevel@tonic-gate 		 * Multiprocessor Systems
7500Sstevel@tonic-gate 		 */
7510Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
7540Sstevel@tonic-gate 		 * or multi-processor).
7550Sstevel@tonic-gate 		 */
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 		if (opteron_erratum_122 || lgrp_plat_node_cnt > 1 ||
7580Sstevel@tonic-gate 		    cpuid_get_ncpu_per_chip(cpu) > 1) {
7590Sstevel@tonic-gate 			/* disable TLB Flush Filter */
760770Skucharsk 			wrmsr(MSR_AMD_HWCR, rdmsr(MSR_AMD_HWCR) |
761770Skucharsk 			    (uint64_t)(uintptr_t)AMD_HWCR_FFDIS);
7620Sstevel@tonic-gate 			opteron_erratum_122++;
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate #else
7660Sstevel@tonic-gate 		WARNING(cpu, 122);
7670Sstevel@tonic-gate 		missing++;
7680Sstevel@tonic-gate #endif
7690Sstevel@tonic-gate 	}
770302Skchow 
771302Skchow #if defined(OPTERON_ERRATUM_123)
7720Sstevel@tonic-gate 	/*LINTED*/
7730Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 123) > 0) {
7740Sstevel@tonic-gate 		/*
7750Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
7760Sstevel@tonic-gate 		 * Dual Core Processors
7770Sstevel@tonic-gate 		 */
7780Sstevel@tonic-gate 		/*
7790Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
7800Sstevel@tonic-gate 		 */
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 		if (cpuid_get_ncpu_per_chip(cpu) > 1) {
7830Sstevel@tonic-gate 			/* workaround is to print a warning to upgrade BIOS */
784770Skucharsk 			if (rdmsr(MSR_AMD_PATCHLEVEL) == 0)
7850Sstevel@tonic-gate 				opteron_erratum_123++;
7860Sstevel@tonic-gate 		}
787302Skchow 	}
7880Sstevel@tonic-gate #endif
789359Skucharsk 
790359Skucharsk #if defined(OPTERON_ERRATUM_131)
791359Skucharsk 	/*LINTED*/
792359Skucharsk 	if (cpuid_opteron_erratum(cpu, 131) > 0) {
793359Skucharsk 		/*
794359Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
795359Skucharsk 		 * Waiting for a Probe Response
796359Skucharsk 		 */
797359Skucharsk 		/*
798359Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
799359Skucharsk 		 */
800359Skucharsk 		if ((opteron_erratum_131 == 0) && ((lgrp_plat_node_cnt *
801359Skucharsk 		    cpuid_get_ncpu_per_chip(cpu)) >= 4)) {
8022519Skchow 			uint64_t nbcfg;
8032519Skchow 			uint64_t wabits;
8042519Skchow 
805359Skucharsk 			/*
8062519Skchow 			 * Print a warning if neither of the workarounds
8072519Skchow 			 * for Erratum 131 is present.
808359Skucharsk 			 */
8092519Skchow 
8102519Skchow 			wabits = AMD_NB_CFG_SRQ_HEARTBEAT |
8112519Skchow 			    AMD_NB_CFG_SRQ_SPR;
8122519Skchow 
8132519Skchow 			nbcfg = rdmsr(MSR_AMD_NB_CFG);
8142519Skchow 			if ((nbcfg & wabits) == 0) {
815359Skucharsk 				opteron_erratum_131++;
8162519Skchow 			} else {
8172519Skchow 				/* cannot have both workarounds set */
8182519Skchow 				ASSERT((nbcfg & wabits) != wabits);
8192519Skchow 			}
820359Skucharsk 		}
821938Sesaxe 	}
822359Skucharsk #endif
823938Sesaxe 
824938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
825938Sesaxe 	/*
826938Sesaxe 	 * This isn't really erratum, but for convenience the
827938Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
828938Sesaxe 	 */
829938Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
830938Sesaxe 		int	node;
831938Sesaxe 		uint8_t data;
832938Sesaxe 
833938Sesaxe 		/*
834938Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
835938Sesaxe 		 * K8 platforms to guard against TSC drift.
836938Sesaxe 		 */
837938Sesaxe 		if (opteron_workaround_6336786) {
838938Sesaxe 			opteron_workaround_6336786++;
839938Sesaxe 		} else if ((lgrp_plat_node_cnt *
840938Sesaxe 		    cpuid_get_ncpu_per_chip(cpu) >= 2) ||
841938Sesaxe 		    opteron_workaround_6336786_UP) {
842938Sesaxe 			for (node = 0; node < lgrp_plat_node_cnt; node++) {
843938Sesaxe 				/*
844938Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
845938Sesaxe 				 * Northbridge device is the node id + 24.
846938Sesaxe 				 */
847938Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
848938Sesaxe 				data &= 0xFC;
849938Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
850938Sesaxe 			}
851938Sesaxe 			opteron_workaround_6336786++;
852938Sesaxe 		}
853359Skucharsk 	}
854938Sesaxe #endif
8551582Skchow 
8561582Skchow #if defined(OPTERON_WORKAROUND_6323525)
8571582Skchow 	/*LINTED*/
8581582Skchow 	/*
8591582Skchow 	 * Mutex primitives don't work as expected.
8601582Skchow 	 */
8611582Skchow 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
8621582Skchow 
8631582Skchow 		/*
8641582Skchow 		 * problem only occurs with 2 or more cores. If bit in
8651582Skchow 		 * MSR_BU_CFG set, then not applicable. The workaround
8661582Skchow 		 * is to patch the semaphone routines with the lfence
8671582Skchow 		 * instruction to provide necessary load memory barrier with
8681582Skchow 		 * possible subsequent read-modify-write ops.
8691582Skchow 		 *
8701582Skchow 		 * It is too early in boot to call the patch routine so
8711582Skchow 		 * set erratum variable to be done in startup_end().
8721582Skchow 		 */
8731582Skchow 		if (opteron_workaround_6323525) {
8741582Skchow 			opteron_workaround_6323525++;
8751582Skchow 		} else if ((x86_feature & X86_SSE2) && ((lgrp_plat_node_cnt *
8761582Skchow 		    cpuid_get_ncpu_per_chip(cpu)) >= 2)) {
8771582Skchow 			if ((xrdmsr(MSR_BU_CFG) & 0x02) == 0)
8781582Skchow 				opteron_workaround_6323525++;
8791582Skchow 		}
8801582Skchow 	}
8811582Skchow #endif
8820Sstevel@tonic-gate 	return (missing);
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate void
8860Sstevel@tonic-gate workaround_errata_end()
8870Sstevel@tonic-gate {
8880Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
8890Sstevel@tonic-gate 	if (opteron_erratum_109) {
890359Skucharsk 		cmn_err(CE_WARN,
891359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
892359Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
893359Skucharsk 		    " system's BIOS to a version\ncontaining this"
894359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
895359Skucharsk 		    " system\noperation may occur.\n");
8960Sstevel@tonic-gate 	}
897359Skucharsk #endif	/* OPTERON_ERRATUM_109 */
8980Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
8990Sstevel@tonic-gate 	if (opteron_erratum_123) {
900359Skucharsk 		cmn_err(CE_WARN,
901359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
902359Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
903359Skucharsk 		    " system's BIOS to a version\ncontaining this"
904359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
905359Skucharsk 		    " system\noperation may occur.\n");
9060Sstevel@tonic-gate 	}
907359Skucharsk #endif	/* OPTERON_ERRATUM_123 */
908359Skucharsk #if defined(OPTERON_ERRATUM_131)
909359Skucharsk 	if (opteron_erratum_131) {
910359Skucharsk 		cmn_err(CE_WARN,
911359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
912359Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
913359Skucharsk 		    " system's BIOS to a version\ncontaining this"
914359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
915359Skucharsk 		    " system\noperation may occur.\n");
916359Skucharsk 	}
917359Skucharsk #endif	/* OPTERON_ERRATUM_131 */
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate static ushort_t *mp_map_warm_reset_vector();
9210Sstevel@tonic-gate static void mp_unmap_warm_reset_vector(ushort_t *warm_reset_vector);
9220Sstevel@tonic-gate 
9232006Sandrei static cpuset_t procset = 1;
9242006Sandrei 
9250Sstevel@tonic-gate /*ARGSUSED*/
9260Sstevel@tonic-gate void
9270Sstevel@tonic-gate start_other_cpus(int cprboot)
9280Sstevel@tonic-gate {
9292006Sandrei 	unsigned int who;
9302006Sandrei 	int skipped = 0;
9311389Sdmick 	int cpuid = 0;
9320Sstevel@tonic-gate 	int delays = 0;
9330Sstevel@tonic-gate 	int started_cpu;
9340Sstevel@tonic-gate 	ushort_t *warm_reset_vector = NULL;
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	/*
9370Sstevel@tonic-gate 	 * Initialize our own cpu_info.
9380Sstevel@tonic-gate 	 */
9390Sstevel@tonic-gate 	init_cpu_info(CPU);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	/*
9420Sstevel@tonic-gate 	 * Initialize our syscall handlers
9430Sstevel@tonic-gate 	 */
9440Sstevel@tonic-gate 	init_cpu_syscall(CPU);
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	/*
9470Sstevel@tonic-gate 	 * if only 1 cpu or not using MP, skip the rest of this
9480Sstevel@tonic-gate 	 */
9492006Sandrei 	if (CPUSET_ISEQUAL(mp_cpus, cpu_ready_set) || use_mp == 0) {
9500Sstevel@tonic-gate 		if (use_mp == 0)
9510Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
9520Sstevel@tonic-gate 		goto done;
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/*
9560Sstevel@tonic-gate 	 * perform such initialization as is needed
9570Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
9580Sstevel@tonic-gate 	 */
9590Sstevel@tonic-gate 	cpu_pause_init();
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	xc_init();		/* initialize processor crosscalls */
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	/*
9640Sstevel@tonic-gate 	 * Copy the real mode code at "real_mode_start" to the
9650Sstevel@tonic-gate 	 * page at rm_platter_va.
9660Sstevel@tonic-gate 	 */
9670Sstevel@tonic-gate 	warm_reset_vector = mp_map_warm_reset_vector();
9680Sstevel@tonic-gate 	if (warm_reset_vector == NULL)
9690Sstevel@tonic-gate 		goto done;
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	bcopy((caddr_t)real_mode_start,
9720Sstevel@tonic-gate 	    (caddr_t)((rm_platter_t *)rm_platter_va)->rm_code,
9730Sstevel@tonic-gate 	    (size_t)real_mode_end - (size_t)real_mode_start);
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	flushes_require_xcalls = 1;
9760Sstevel@tonic-gate 
9772575Snf202958 	ASSERT(CPU_IN_SET(procset, cpuid));
9782575Snf202958 	ASSERT(CPU_IN_SET(cpu_ready_set, cpuid));
9792575Snf202958 
9802575Snf202958 	/*
9812575Snf202958 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
9822575Snf202958 	 * do their TSC syncs with the same CPU.
9832575Snf202958 	 */
9840Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
9870Sstevel@tonic-gate 		if (who == cpuid)
9880Sstevel@tonic-gate 			continue;
9892575Snf202958 
9902575Snf202958 		delays = 0;
9912575Snf202958 
9922006Sandrei 		if (!CPU_IN_SET(mp_cpus, who))
9932006Sandrei 			continue;
9940Sstevel@tonic-gate 
9952006Sandrei 		if (ncpus >= max_ncpus) {
9962006Sandrei 			skipped = who;
9970Sstevel@tonic-gate 			continue;
9982006Sandrei 		}
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 		mp_startup_init(who);
10010Sstevel@tonic-gate 		started_cpu = 1;
10020Sstevel@tonic-gate 		(*cpu_startf)(who, rm_platter_pa);
10030Sstevel@tonic-gate 
10042006Sandrei 		while (!CPU_IN_SET(procset, who)) {
10050Sstevel@tonic-gate 			delay(1);
10060Sstevel@tonic-gate 			if (++delays > (20 * hz)) {
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 				cmn_err(CE_WARN,
10090Sstevel@tonic-gate 				    "cpu%d failed to start", who);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 				mutex_enter(&cpu_lock);
10120Sstevel@tonic-gate 				cpu[who]->cpu_flags = 0;
1013414Skchow 				cpu_vm_data_destroy(cpu[who]);
10140Sstevel@tonic-gate 				cpu_del_unit(who);
10150Sstevel@tonic-gate 				mutex_exit(&cpu_lock);
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 				started_cpu = 0;
10180Sstevel@tonic-gate 				break;
10190Sstevel@tonic-gate 			}
10200Sstevel@tonic-gate 		}
10210Sstevel@tonic-gate 		if (!started_cpu)
10220Sstevel@tonic-gate 			continue;
10230Sstevel@tonic-gate 		if (tsc_gethrtime_enable)
10240Sstevel@tonic-gate 			tsc_sync_master(who);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	affinity_clear();
10290Sstevel@tonic-gate 
10302575Snf202958 	/*
10312575Snf202958 	 * Wait for all CPUs that booted (have presence in procset)
10322575Snf202958 	 * to come online (have presence in cpu_ready_set).  Note
10332575Snf202958 	 * that the start CPU already satisfies both of these, so no
10342575Snf202958 	 * special case is needed.
10352575Snf202958 	 */
10360Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
10372006Sandrei 		if (!CPU_IN_SET(procset, who))
10380Sstevel@tonic-gate 			continue;
10390Sstevel@tonic-gate 
10402006Sandrei 		while (!CPU_IN_SET(cpu_ready_set, who))
10410Sstevel@tonic-gate 			delay(1);
10420Sstevel@tonic-gate 	}
10430Sstevel@tonic-gate 
10442006Sandrei 	if (skipped) {
10452006Sandrei 		cmn_err(CE_NOTE,
10462006Sandrei 		    "System detected %d CPU(s), but "
10472006Sandrei 		    "only %d CPU(s) were enabled during boot.",
10482006Sandrei 		    skipped + 1, ncpus);
10492006Sandrei 		cmn_err(CE_NOTE,
10502006Sandrei 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
10512006Sandrei 		    "See eeprom(1M).");
10522006Sandrei 	}
10532006Sandrei 
10540Sstevel@tonic-gate done:
10550Sstevel@tonic-gate 	workaround_errata_end();
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	if (warm_reset_vector != NULL)
10580Sstevel@tonic-gate 		mp_unmap_warm_reset_vector(warm_reset_vector);
10590Sstevel@tonic-gate 	hat_unload(kas.a_hat, (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
10600Sstevel@tonic-gate 	    HAT_UNLOAD);
10611642Sgavinm 
10621642Sgavinm 	cmi_post_mpstartup();
10630Sstevel@tonic-gate }
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate /*
10660Sstevel@tonic-gate  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
10670Sstevel@tonic-gate  */
10680Sstevel@tonic-gate /*ARGSUSED*/
10690Sstevel@tonic-gate int
10700Sstevel@tonic-gate mp_cpu_configure(int cpuid)
10710Sstevel@tonic-gate {
10720Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
10730Sstevel@tonic-gate }
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate /*ARGSUSED*/
10760Sstevel@tonic-gate int
10770Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
10800Sstevel@tonic-gate }
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate /*
10830Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
10842985Sdmick  * Called from real_mode_start.
10851251Skchow  *
10861251Skchow  * WARNING: until CPU_READY is set, mp_startup and routines called by
10871251Skchow  * mp_startup should not call routines (e.g. kmem_free) that could call
10881251Skchow  * hat_unload which requires CPU_READY to be set.
10890Sstevel@tonic-gate  */
10900Sstevel@tonic-gate void
10910Sstevel@tonic-gate mp_startup(void)
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	struct cpu *cp = CPU;
10940Sstevel@tonic-gate 	uint_t new_x86_feature;
10950Sstevel@tonic-gate 
10962985Sdmick 	/*
1097*3021Sdmick 	 * We need to get TSC on this proc synced (i.e., any delta
1098*3021Sdmick 	 * from cpu0 accounted for) as soon as we can, because many
1099*3021Sdmick 	 * many things use gethrtime/pc_gethrestime, including
1100*3021Sdmick 	 * interrupts, cmn_err, etc.
1101*3021Sdmick 	 */
1102*3021Sdmick 
1103*3021Sdmick 	/* Let cpu0 continue into tsc_sync_master() */
1104*3021Sdmick 	CPUSET_ATOMIC_ADD(procset, cp->cpu_id);
1105*3021Sdmick 
1106*3021Sdmick 	if (tsc_gethrtime_enable)
1107*3021Sdmick 		tsc_sync_slave();
1108*3021Sdmick 
1109*3021Sdmick 	/*
11102985Sdmick 	 * Once this was done from assembly, but it's safer here; if
11112985Sdmick 	 * it blocks, we need to be able to swtch() to and from, and
11122985Sdmick 	 * since we get here by calling t_pc, we need to do that call
11132985Sdmick 	 * before swtch() overwrites it.
11142985Sdmick 	 */
11152985Sdmick 
11162985Sdmick 	(void) (*ap_mlsetup)();
11172985Sdmick 
11180Sstevel@tonic-gate 	new_x86_feature = cpuid_pass1(cp);
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	/*
11210Sstevel@tonic-gate 	 * We need to Sync MTRR with cpu0's MTRR. We have to do
11220Sstevel@tonic-gate 	 * this with interrupts disabled.
11230Sstevel@tonic-gate 	 */
11240Sstevel@tonic-gate 	if (x86_feature & X86_MTRR)
11250Sstevel@tonic-gate 		mtrr_sync();
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	/*
11280Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
11290Sstevel@tonic-gate 	 */
11300Sstevel@tonic-gate 	init_cpu_syscall(cp);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	/*
11330Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
11340Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
11350Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
11360Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
11370Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
11380Sstevel@tonic-gate 	 * calls before CPU_READY is set.
11390Sstevel@tonic-gate 	 */
11400Sstevel@tonic-gate 	(void) splx(ipltospl(LOCK_LEVEL));
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	/*
11430Sstevel@tonic-gate 	 * Do a sanity check to make sure this new CPU is a sane thing
11440Sstevel@tonic-gate 	 * to add to the collection of processors running this system.
11450Sstevel@tonic-gate 	 *
11460Sstevel@tonic-gate 	 * XXX	Clearly this needs to get more sophisticated, if x86
11470Sstevel@tonic-gate 	 * systems start to get built out of heterogenous CPUs; as is
11480Sstevel@tonic-gate 	 * likely to happen once the number of processors in a configuration
11490Sstevel@tonic-gate 	 * gets large enough.
11500Sstevel@tonic-gate 	 */
11510Sstevel@tonic-gate 	if ((x86_feature & new_x86_feature) != x86_feature) {
11520Sstevel@tonic-gate 		cmn_err(CE_CONT, "?cpu%d: %b\n",
11530Sstevel@tonic-gate 		    cp->cpu_id, new_x86_feature, FMT_X86_FEATURE);
11540Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	/*
11580Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
11590Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
11600Sstevel@tonic-gate 	 * answer of dieing horribly.  Provided the boot cpu is ok,
11610Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
11620Sstevel@tonic-gate 	 */
11630Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
11640Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	cpuid_pass2(cp);
11670Sstevel@tonic-gate 	cpuid_pass3(cp);
11680Sstevel@tonic-gate 	(void) cpuid_pass4(cp);
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	init_cpu_info(cp);
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
11730Sstevel@tonic-gate 	/*
11740Sstevel@tonic-gate 	 * It's unfortunate that chip_cpu_init() has to be called here.
11750Sstevel@tonic-gate 	 * It really belongs in cpu_add_unit(), but unfortunately it is
11760Sstevel@tonic-gate 	 * dependent on the cpuid probing, which must be done in the
11770Sstevel@tonic-gate 	 * context of the current CPU. Care must be taken on x86 to ensure
11780Sstevel@tonic-gate 	 * that mp_startup can safely block even though chip_cpu_init() and
11790Sstevel@tonic-gate 	 * cpu_add_active() have not yet been called.
11800Sstevel@tonic-gate 	 */
11810Sstevel@tonic-gate 	chip_cpu_init(cp);
11820Sstevel@tonic-gate 	chip_cpu_startup(cp);
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_ENABLE | CPU_EXISTS;
11850Sstevel@tonic-gate 	cpu_add_active(cp);
11862575Snf202958 
11872575Snf202958 	if (dtrace_cpu_init != NULL) {
11882575Snf202958 		(*dtrace_cpu_init)(cp->cpu_id);
11892575Snf202958 	}
11902575Snf202958 
11910Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
11920Sstevel@tonic-gate 
11931251Skchow 	add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi);
11941251Skchow 
11951482Ssethg 	/* The base spl should still be at LOCK LEVEL here */
11961482Ssethg 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
11971482Ssethg 	set_base_spl();		/* Restore the spl to its proper value */
11981482Ssethg 
11990Sstevel@tonic-gate 	(void) spl0();				/* enable interrupts */
12000Sstevel@tonic-gate 
12011414Scindi 	/*
12021414Scindi 	 * Set up the CPU module for this CPU.  This can't be done before
12031414Scindi 	 * this CPU is made CPU_READY, because we may (in heterogeneous systems)
12041414Scindi 	 * need to go load another CPU module.  The act of attempting to load
12051414Scindi 	 * a module may trigger a cross-call, which will ASSERT unless this
12061414Scindi 	 * cpu is CPU_READY.
12071414Scindi 	 */
12081414Scindi 	cmi_init();
12091414Scindi 
12101414Scindi 	if (x86_feature & X86_MCA)
12111414Scindi 		cmi_mca_init();
12121414Scindi 
12130Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
12140Sstevel@tonic-gate 		kdi_dvec_cpu_init(cp);
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	/*
12170Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
12180Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
12190Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
12200Sstevel@tonic-gate 	 */
12210Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	/*
12240Sstevel@tonic-gate 	 * Because mp_startup() gets fired off after init() starts, we
12250Sstevel@tonic-gate 	 * can't use the '?' trick to do 'boot -v' printing - so we
12260Sstevel@tonic-gate 	 * always direct the 'cpu .. online' messages to the log.
12270Sstevel@tonic-gate 	 */
12280Sstevel@tonic-gate 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
12290Sstevel@tonic-gate 	    cp->cpu_id);
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	/*
12320Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
12330Sstevel@tonic-gate 	 */
12340Sstevel@tonic-gate 	thread_exit();
12350Sstevel@tonic-gate 	panic("mp_startup: cannot return");
12360Sstevel@tonic-gate 	/*NOTREACHED*/
12370Sstevel@tonic-gate }
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate /*
12410Sstevel@tonic-gate  * Start CPU on user request.
12420Sstevel@tonic-gate  */
12430Sstevel@tonic-gate /* ARGSUSED */
12440Sstevel@tonic-gate int
12450Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
12480Sstevel@tonic-gate 	return (0);
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate /*
12520Sstevel@tonic-gate  * Stop CPU on user request.
12530Sstevel@tonic-gate  */
12540Sstevel@tonic-gate /* ARGSUSED */
12550Sstevel@tonic-gate int
12560Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
12570Sstevel@tonic-gate {
12581389Sdmick 	extern int cbe_psm_timer_mode;
12590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
12601389Sdmick 
12611389Sdmick 	/*
12621389Sdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
12631389Sdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
12641389Sdmick 	 */
12651389Sdmick 
12661389Sdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
12671389Sdmick 		return (1);
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	return (0);
12700Sstevel@tonic-gate }
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate /*
12730Sstevel@tonic-gate  * Power on CPU.
12740Sstevel@tonic-gate  */
12750Sstevel@tonic-gate /* ARGSUSED */
12760Sstevel@tonic-gate int
12770Sstevel@tonic-gate mp_cpu_poweron(struct cpu *cp)
12780Sstevel@tonic-gate {
12790Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
12800Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
12810Sstevel@tonic-gate }
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate /*
12840Sstevel@tonic-gate  * Power off CPU.
12850Sstevel@tonic-gate  */
12860Sstevel@tonic-gate /* ARGSUSED */
12870Sstevel@tonic-gate int
12880Sstevel@tonic-gate mp_cpu_poweroff(struct cpu *cp)
12890Sstevel@tonic-gate {
12900Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
12910Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate /*
12960Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
12970Sstevel@tonic-gate  */
12980Sstevel@tonic-gate int
12990Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
13000Sstevel@tonic-gate {
13010Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
13020Sstevel@tonic-gate 		return (EBUSY);
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
13050Sstevel@tonic-gate 	return (0);
13060Sstevel@tonic-gate }
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate /*
13090Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
13100Sstevel@tonic-gate  */
13110Sstevel@tonic-gate void
13120Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
13130Sstevel@tonic-gate {
13140Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
13150Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
13160Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
13170Sstevel@tonic-gate }
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate static ushort_t *
13220Sstevel@tonic-gate mp_map_warm_reset_vector()
13230Sstevel@tonic-gate {
13240Sstevel@tonic-gate 	ushort_t *warm_reset_vector;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	if (!(warm_reset_vector = (ushort_t *)psm_map_phys(WARM_RESET_VECTOR,
13270Sstevel@tonic-gate 	    sizeof (ushort_t *), PROT_READ|PROT_WRITE)))
13280Sstevel@tonic-gate 		return (NULL);
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	/*
13310Sstevel@tonic-gate 	 * setup secondary cpu bios boot up vector
13320Sstevel@tonic-gate 	 */
13330Sstevel@tonic-gate 	*warm_reset_vector = (ushort_t)((caddr_t)
13340Sstevel@tonic-gate 		((struct rm_platter *)rm_platter_va)->rm_code - rm_platter_va
13350Sstevel@tonic-gate 		+ ((ulong_t)rm_platter_va & 0xf));
13360Sstevel@tonic-gate 	warm_reset_vector++;
13370Sstevel@tonic-gate 	*warm_reset_vector = (ushort_t)(rm_platter_pa >> 4);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	--warm_reset_vector;
13400Sstevel@tonic-gate 	return (warm_reset_vector);
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate static void
13440Sstevel@tonic-gate mp_unmap_warm_reset_vector(ushort_t *warm_reset_vector)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate 	psm_unmap_phys((caddr_t)warm_reset_vector, sizeof (ushort_t *));
13470Sstevel@tonic-gate }
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate void
13500Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
13511414Scindi {
13521414Scindi 	cmi_faulted_enter(cp);
13531414Scindi }
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate void
13560Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
13571414Scindi {
13581414Scindi 	cmi_faulted_exit(cp);
13591414Scindi }
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate /*
13620Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
13630Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
13640Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
13650Sstevel@tonic-gate  * organization rather than speed.  We could avoid checking x86_feature at every
13660Sstevel@tonic-gate  * context switch by installing different context ops, depending on the
13670Sstevel@tonic-gate  * x86_feature flags, at LDT creation time -- one for each combination of fast
13680Sstevel@tonic-gate  * syscall feature flags.
13690Sstevel@tonic-gate  */
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate /*ARGSUSED*/
13720Sstevel@tonic-gate void
13730Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg)
13740Sstevel@tonic-gate {
13750Sstevel@tonic-gate 	if (x86_feature & X86_SEP)
13760Sstevel@tonic-gate 		cpu_sep_disable();
13770Sstevel@tonic-gate 	if (x86_feature & X86_ASYSC)
13780Sstevel@tonic-gate 		cpu_asysc_disable();
13790Sstevel@tonic-gate }
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate /*ARGSUSED*/
13820Sstevel@tonic-gate void
13830Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg)
13840Sstevel@tonic-gate {
13850Sstevel@tonic-gate 	if (x86_feature & X86_SEP)
13860Sstevel@tonic-gate 		cpu_sep_enable();
13870Sstevel@tonic-gate 	if (x86_feature & X86_ASYSC)
13880Sstevel@tonic-gate 		cpu_asysc_enable();
13890Sstevel@tonic-gate }
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate static void
13920Sstevel@tonic-gate cpu_sep_enable(void)
13930Sstevel@tonic-gate {
13940Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
13950Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
13960Sstevel@tonic-gate 
1397770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
13980Sstevel@tonic-gate }
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate static void
14010Sstevel@tonic-gate cpu_sep_disable(void)
14020Sstevel@tonic-gate {
14030Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
14040Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	/*
14070Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
14080Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
14090Sstevel@tonic-gate 	 */
1410770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, 0ULL);
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate static void
14140Sstevel@tonic-gate cpu_asysc_enable(void)
14150Sstevel@tonic-gate {
14160Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
14170Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
14180Sstevel@tonic-gate 
1419770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1420770Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate static void
14240Sstevel@tonic-gate cpu_asysc_disable(void)
14250Sstevel@tonic-gate {
14260Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
14270Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	/*
14300Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
14310Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
14320Sstevel@tonic-gate 	 */
1433770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1434770Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
14350Sstevel@tonic-gate }
1436