xref: /onnv-gate/usr/src/uts/i86pc/os/mp_startup.c (revision 6749:22c537726e3e)
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  */
213446Smrj 
220Sstevel@tonic-gate /*
235893Sesaxe  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/thread.h>
310Sstevel@tonic-gate #include <sys/cpuvar.h>
320Sstevel@tonic-gate #include <sys/t_lock.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/proc.h>
350Sstevel@tonic-gate #include <sys/disp.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 <vm/as.h>
460Sstevel@tonic-gate #include <vm/seg_kmem.h>
473446Smrj #include <vm/seg_kp.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>
573434Sesaxe #include <sys/pg.h>
583434Sesaxe #include <sys/cmt.h>
590Sstevel@tonic-gate #include <sys/dtrace.h>
600Sstevel@tonic-gate #include <sys/archsystm.h>
610Sstevel@tonic-gate #include <sys/fp.h>
620Sstevel@tonic-gate #include <sys/reboot.h>
633446Smrj #include <sys/kdi_machimpl.h>
640Sstevel@tonic-gate #include <vm/hat_i86.h>
650Sstevel@tonic-gate #include <sys/memnode.h>
66938Sesaxe #include <sys/pci_cfgspace.h>
673446Smrj #include <sys/mach_mmu.h>
683446Smrj #include <sys/sysmacros.h>
695084Sjohnlev #if defined(__xpv)
705084Sjohnlev #include <sys/hypervisor.h>
715084Sjohnlev #endif
721414Scindi #include <sys/cpu_module.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate struct cpu	cpus[1];			/* CPU data */
750Sstevel@tonic-gate struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
760Sstevel@tonic-gate cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
793446Smrj  * Useful for disabling MP bring-up on a MP capable system.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate int use_mp = 1;
820Sstevel@tonic-gate 
832006Sandrei /*
843446Smrj  * to be set by a PSM to indicate what cpus
853446Smrj  * are sitting around on the system.
862006Sandrei  */
873446Smrj cpuset_t mp_cpus;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * This variable is used by the hat layer to decide whether or not
910Sstevel@tonic-gate  * critical sections are needed to prevent race conditions.  For sun4m,
920Sstevel@tonic-gate  * this variable is set once enough MP initialization has been done in
930Sstevel@tonic-gate  * order to allow cross calls.
940Sstevel@tonic-gate  */
953446Smrj int flushes_require_xcalls;
966336Sbholler 
976336Sbholler cpuset_t cpu_ready_set;		/* initialized in startup() */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static 	void	mp_startup(void);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static void cpu_sep_enable(void);
1020Sstevel@tonic-gate static void cpu_sep_disable(void);
1030Sstevel@tonic-gate static void cpu_asysc_enable(void);
1040Sstevel@tonic-gate static void cpu_asysc_disable(void);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * Init CPU info - get CPU type info for processor_info system call.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate void
1100Sstevel@tonic-gate init_cpu_info(struct cpu *cp)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	processor_info_t *pi = &cp->cpu_type_info;
1130Sstevel@tonic-gate 	char buf[CPU_IDSTRLEN];
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/*
1160Sstevel@tonic-gate 	 * Get clock-frequency property for the CPU.
1170Sstevel@tonic-gate 	 */
1180Sstevel@tonic-gate 	pi->pi_clock = cpu_freq;
1190Sstevel@tonic-gate 
1204667Smh27603 	/*
1214667Smh27603 	 * Current frequency in Hz.
1224667Smh27603 	 */
1234718Smh27603 	cp->cpu_curr_clock = cpu_freq_hz;
1244667Smh27603 
1254877Smh27603 	/*
1264877Smh27603 	 * Supported frequencies.
1274877Smh27603 	 */
1284877Smh27603 	cpu_set_supp_freqs(cp, NULL);
1294877Smh27603 
1300Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1310Sstevel@tonic-gate 	if (fpu_exists)
1320Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	(void) cpuid_getidstr(cp, buf, sizeof (buf));
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1370Sstevel@tonic-gate 	(void) strcpy(cp->cpu_idstr, buf);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	(void) cpuid_getbrandstr(cp, buf, sizeof (buf));
1420Sstevel@tonic-gate 	cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1430Sstevel@tonic-gate 	(void) strcpy(cp->cpu_brandstr, buf);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * Configure syscall support on this CPU.
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate /*ARGSUSED*/
1525295Srandyf void
1530Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	kpreempt_disable();
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate #if defined(__amd64)
1583446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC)) {
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate #if !defined(__lint)
1610Sstevel@tonic-gate 		/*
1620Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1630Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1640Sstevel@tonic-gate 		 * here.
1650Sstevel@tonic-gate 		 */
1660Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1670Sstevel@tonic-gate 		ASSERT(UDS_SEL == U32CS_SEL + 8);
1680Sstevel@tonic-gate 		ASSERT(UCS_SEL == U32CS_SEL + 16);
1690Sstevel@tonic-gate #endif
1700Sstevel@tonic-gate 		/*
1710Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1720Sstevel@tonic-gate 		 */
1730Sstevel@tonic-gate 		cpu_asysc_enable();
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		/*
1760Sstevel@tonic-gate 		 * Program the magic registers ..
1770Sstevel@tonic-gate 		 */
1783446Smrj 		wrmsr(MSR_AMD_STAR,
1793446Smrj 		    ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
180770Skucharsk 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
181770Skucharsk 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		/*
1840Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
1850Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
1860Sstevel@tonic-gate 		 */
187770Skucharsk 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate #endif
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
1930Sstevel@tonic-gate 	 * hard to use syscall/sysret, and it is more portable anyway.
1940Sstevel@tonic-gate 	 *
1950Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
1960Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
1970Sstevel@tonic-gate 	 */
1983446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP)) {
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate #if !defined(__lint)
2010Sstevel@tonic-gate 		/*
2020Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
2030Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
2040Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
2050Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
2060Sstevel@tonic-gate 		 * Instruction Set Reference"
2070Sstevel@tonic-gate 		 */
2080Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
2110Sstevel@tonic-gate 		ASSERT32(UDS_SEL == UCS_SEL + 8);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
2140Sstevel@tonic-gate 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
2150Sstevel@tonic-gate #endif
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 		cpu_sep_enable();
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		/*
2200Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2210Sstevel@tonic-gate 		 * via a context handler.
2220Sstevel@tonic-gate 		 */
2233446Smrj 		wrmsr(MSR_INTC_SEP_ESP, 0);
224770Skucharsk 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	kpreempt_enable();
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * Multiprocessor initialization.
2320Sstevel@tonic-gate  *
2330Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2340Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
2350Sstevel@tonic-gate  */
2363446Smrj struct cpu *
2370Sstevel@tonic-gate mp_startup_init(int cpun)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	struct cpu *cp;
2400Sstevel@tonic-gate 	kthread_id_t tp;
2410Sstevel@tonic-gate 	caddr_t	sp;
2420Sstevel@tonic-gate 	proc_t *procp;
2435084Sjohnlev #if !defined(__xpv)
2445045Sbholler 	extern int idle_cpu_prefer_mwait;
2455084Sjohnlev #endif
2460Sstevel@tonic-gate 	extern void idle();
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate #ifdef TRAPTRACE
2490Sstevel@tonic-gate 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
2500Sstevel@tonic-gate #endif
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
2530Sstevel@tonic-gate 
2543446Smrj 	cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
2555084Sjohnlev #if !defined(__xpv)
2565045Sbholler 	if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait)
2575045Sbholler 		cp->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
2585084Sjohnlev #endif
2594481Sbholler 
2600Sstevel@tonic-gate 	procp = curthread->t_procp;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * Initialize the dispatcher first.
2650Sstevel@tonic-gate 	 */
2660Sstevel@tonic-gate 	disp_cpu_init(cp);
2670Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2680Sstevel@tonic-gate 
269414Skchow 	cpu_vm_data_init(cp);
270414Skchow 
2710Sstevel@tonic-gate 	/*
2720Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
2730Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
2740Sstevel@tonic-gate 	 * when the CPU starts running.
2750Sstevel@tonic-gate 	 */
2760Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
2770Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
2810Sstevel@tonic-gate 	 * as soon as the CPU comes online.
2820Sstevel@tonic-gate 	 *
2830Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
2840Sstevel@tonic-gate 	 * thread_create().
2850Sstevel@tonic-gate 	 */
2860Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
2870Sstevel@tonic-gate 	tp->t_preempt = 1;
2880Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
2890Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
2900Sstevel@tonic-gate 	tp->t_cpu = cp;
2910Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * Setup thread to start in mp_startup.
2950Sstevel@tonic-gate 	 */
2960Sstevel@tonic-gate 	sp = tp->t_stk;
2970Sstevel@tonic-gate 	tp->t_pc = (uintptr_t)mp_startup;
2980Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
2993446Smrj #if defined(__amd64)
3003446Smrj 	tp->t_sp -= STACK_ENTRY_ALIGN;		/* fake a call */
3013446Smrj #endif
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	cp->cpu_id = cpun;
3040Sstevel@tonic-gate 	cp->cpu_self = cp;
3050Sstevel@tonic-gate 	cp->cpu_thread = tp;
3060Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
3070Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
3080Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	/*
3111482Ssethg 	 * cpu_base_spl must be set explicitly here to prevent any blocking
3121482Ssethg 	 * operations in mp_startup from causing the spl of the cpu to drop
3131482Ssethg 	 * to 0 (allowing device interrupts before we're ready) in resume().
3141482Ssethg 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
3151482Ssethg 	 * As an extra bit of security on DEBUG kernels, this is enforced with
3161482Ssethg 	 * an assertion in mp_startup() -- before cpu_base_spl is set to its
3171482Ssethg 	 * proper value.
3181482Ssethg 	 */
3191482Ssethg 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
3201482Ssethg 
3211482Ssethg 	/*
3220Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
3230Sstevel@tonic-gate 	 */
3240Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	tp->t_preempt = 1;
3290Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3300Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3310Sstevel@tonic-gate 	tp->t_cpu = cp;
3320Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	/*
3353434Sesaxe 	 * Bootstrap the CPU's PG data
33660Sesaxe 	 */
3373434Sesaxe 	pg_cpu_bootstrap(cp);
33860Sesaxe 
33960Sesaxe 	/*
3403446Smrj 	 * Perform CPC initialization on the new CPU.
3410Sstevel@tonic-gate 	 */
3420Sstevel@tonic-gate 	kcpc_hw_init(cp);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/*
3450Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
3460Sstevel@tonic-gate 	 * for each CPU.
3470Sstevel@tonic-gate 	 */
3480Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/*
3513446Smrj 	 * Allocate page for new GDT and initialize from current GDT.
3520Sstevel@tonic-gate 	 */
3533446Smrj #if !defined(__lint)
3543446Smrj 	ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
3553446Smrj #endif
3565460Sjosephb 	cp->cpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
3575460Sjosephb 	bcopy(CPU->cpu_gdt, cp->cpu_gdt, (sizeof (*cp->cpu_gdt) * NGDT));
3581626Srab 
3593446Smrj #if defined(__i386)
3600Sstevel@tonic-gate 	/*
3610Sstevel@tonic-gate 	 * setup kernel %gs.
3620Sstevel@tonic-gate 	 */
3630Sstevel@tonic-gate 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
3640Sstevel@tonic-gate 	    SEL_KPL, 0, 1);
3653446Smrj #endif
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	/*
3680Sstevel@tonic-gate 	 * If we have more than one node, each cpu gets a copy of IDT
3690Sstevel@tonic-gate 	 * local to its node. If this is a Pentium box, we use cpu 0's
3700Sstevel@tonic-gate 	 * IDT. cpu 0's IDT has been made read-only to workaround the
3710Sstevel@tonic-gate 	 * cmpxchgl register bug
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
3745460Sjosephb #if !defined(__lint)
3755460Sjosephb 		ASSERT((sizeof (*CPU->cpu_idt) * NIDT) <= PAGESIZE);
3765460Sjosephb #endif
3775460Sjosephb 		cp->cpu_idt = kmem_zalloc(PAGESIZE, KM_SLEEP);
3785460Sjosephb 		bcopy(CPU->cpu_idt, cp->cpu_idt, PAGESIZE);
3793446Smrj 	} else {
3805460Sjosephb 		cp->cpu_idt = CPU->cpu_idt;
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/*
3843446Smrj 	 * Get interrupt priority data from cpu 0.
3850Sstevel@tonic-gate 	 */
3860Sstevel@tonic-gate 	cp->cpu_pri_data = CPU->cpu_pri_data;
3870Sstevel@tonic-gate 
3883446Smrj 	/*
3893446Smrj 	 * alloc space for cpuid info
3903446Smrj 	 */
3913446Smrj 	cpuid_alloc_space(cp);
3923446Smrj 
3935084Sjohnlev #if !defined(__xpv)
3944581Ssherrym 	/*
3954581Ssherrym 	 * alloc space for ucode_info
3964581Ssherrym 	 */
3974581Ssherrym 	ucode_alloc_space(cp);
3985084Sjohnlev #endif
3994581Ssherrym 
4000Sstevel@tonic-gate 	hat_cpu_online(cp);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate #ifdef TRAPTRACE
4030Sstevel@tonic-gate 	/*
4043446Smrj 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers
4050Sstevel@tonic-gate 	 */
4060Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
4070Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
4080Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
4090Sstevel@tonic-gate #endif
4100Sstevel@tonic-gate 	/*
4110Sstevel@tonic-gate 	 * Record that we have another CPU.
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
4140Sstevel@tonic-gate 	/*
4150Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
4160Sstevel@tonic-gate 	 */
4171455Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
4200Sstevel@tonic-gate 	 * after mp_startup().
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	cpu_add_unit(cp);
4230Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
4243446Smrj 
4253446Smrj 	return (cp);
4263446Smrj }
4273446Smrj 
4283446Smrj /*
4293446Smrj  * Undo what was done in mp_startup_init
4303446Smrj  */
4313446Smrj static void
4323446Smrj mp_startup_fini(struct cpu *cp, int error)
4333446Smrj {
4343446Smrj 	mutex_enter(&cpu_lock);
4353446Smrj 
4363446Smrj 	/*
4373446Smrj 	 * Remove the CPU from the list of available CPUs.
4383446Smrj 	 */
4393446Smrj 	cpu_del_unit(cp->cpu_id);
4403446Smrj 
4413446Smrj 	if (error == ETIMEDOUT) {
4423446Smrj 		/*
4433446Smrj 		 * The cpu was started, but never *seemed* to run any
4443446Smrj 		 * code in the kernel; it's probably off spinning in its
4453446Smrj 		 * own private world, though with potential references to
4463446Smrj 		 * our kmem-allocated IDTs and GDTs (for example).
4473446Smrj 		 *
4483446Smrj 		 * Worse still, it may actually wake up some time later,
4493446Smrj 		 * so rather than guess what it might or might not do, we
4503446Smrj 		 * leave the fundamental data structures intact.
4513446Smrj 		 */
4523446Smrj 		cp->cpu_flags = 0;
4533446Smrj 		mutex_exit(&cpu_lock);
4543446Smrj 		return;
4553446Smrj 	}
4563446Smrj 
4573446Smrj 	/*
4583446Smrj 	 * At this point, the only threads bound to this CPU should
4593446Smrj 	 * special per-cpu threads: it's idle thread, it's pause threads,
4603446Smrj 	 * and it's interrupt threads.  Clean these up.
4613446Smrj 	 */
4623446Smrj 	cpu_destroy_bound_threads(cp);
4633446Smrj 	cp->cpu_idle_thread = NULL;
4643446Smrj 
4653446Smrj 	/*
4663446Smrj 	 * Free the interrupt stack.
4673446Smrj 	 */
4683446Smrj 	segkp_release(segkp,
4693446Smrj 	    cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
4703446Smrj 
4713446Smrj 	mutex_exit(&cpu_lock);
4723446Smrj 
4733446Smrj #ifdef TRAPTRACE
4743446Smrj 	/*
4753446Smrj 	 * Discard the trap trace buffer
4763446Smrj 	 */
4773446Smrj 	{
4783446Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
4793446Smrj 
4803446Smrj 		kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
4813446Smrj 		ttc->ttc_first = NULL;
4823446Smrj 	}
4833446Smrj #endif
4843446Smrj 
4853446Smrj 	hat_cpu_offline(cp);
4863446Smrj 
4873446Smrj 	cpuid_free_space(cp);
4883446Smrj 
4895084Sjohnlev #if !defined(__xpv)
4904581Ssherrym 	ucode_free_space(cp);
4915084Sjohnlev #endif
4924581Ssherrym 
4935460Sjosephb 	if (cp->cpu_idt != CPU->cpu_idt)
4945460Sjosephb 		kmem_free(cp->cpu_idt, PAGESIZE);
4955460Sjosephb 	cp->cpu_idt = NULL;
4963446Smrj 
4975460Sjosephb 	kmem_free(cp->cpu_gdt, PAGESIZE);
4985460Sjosephb 	cp->cpu_gdt = NULL;
4993446Smrj 
5003446Smrj 	teardown_vaddr_for_ppcopy(cp);
5013446Smrj 
5023446Smrj 	kcpc_hw_fini(cp);
5033446Smrj 
5043446Smrj 	cp->cpu_dispthread = NULL;
5053446Smrj 	cp->cpu_thread = NULL;	/* discarded by cpu_destroy_bound_threads() */
5063446Smrj 
5073446Smrj 	cpu_vm_data_destroy(cp);
5083446Smrj 
5093446Smrj 	mutex_enter(&cpu_lock);
5103446Smrj 	disp_cpu_fini(cp);
5113446Smrj 	mutex_exit(&cpu_lock);
5123446Smrj 
5135084Sjohnlev #if !defined(__xpv)
5145045Sbholler 	if (cp->cpu_m.mcpu_mwait != NULL)
5155045Sbholler 		cpuid_mwait_free(cp);
5165084Sjohnlev #endif
5173446Smrj 	kmem_free(cp, sizeof (*cp));
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate /*
5210Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
5220Sstevel@tonic-gate  *
5230Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
5240Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
5250Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
5260Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
5270Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
5280Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
5290Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
5300Sstevel@tonic-gate  * system.
5310Sstevel@tonic-gate  *
532938Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
533938Sesaxe  * mp_startup() for all slave CPUs. Slaves process workaround_errata prior
534938Sesaxe  * to acknowledging their readiness to the master, so this routine will
535938Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
536938Sesaxe  * global data safe.
537938Sesaxe  *
538359Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
539359Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate 
5423446Smrj #if defined(OPTERON_ERRATUM_88)
5433446Smrj int opteron_erratum_88;		/* if non-zero -> at least one cpu has it */
5443446Smrj #endif
5453446Smrj 
5460Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
5470Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
5480Sstevel@tonic-gate #endif
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
5510Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
5520Sstevel@tonic-gate #endif
5530Sstevel@tonic-gate 
5543446Smrj #if defined(OPTERON_ERRATUM_95)
5553446Smrj int opteron_erratum_95;		/* if non-zero -> at least one cpu has it */
5563446Smrj #endif
5573446Smrj 
5580Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
5590Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
5600Sstevel@tonic-gate #endif
5610Sstevel@tonic-gate 
5623446Smrj #if defined(OPTERON_ERRATUM_108)
5633446Smrj int opteron_erratum_108;	/* if non-zero -> at least one cpu has it */
5643446Smrj #endif
5653446Smrj 
5660Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
5670Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
5680Sstevel@tonic-gate #endif
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
5710Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
5720Sstevel@tonic-gate #endif
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
5750Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
5760Sstevel@tonic-gate #endif
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
5790Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
5800Sstevel@tonic-gate #endif
5810Sstevel@tonic-gate 
582359Skucharsk #if defined(OPTERON_ERRATUM_131)
583359Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
584359Skucharsk #endif
5850Sstevel@tonic-gate 
586938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
587938Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
588938Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
589938Sesaxe #endif
590938Sesaxe 
5911582Skchow #if defined(OPTERON_WORKAROUND_6323525)
5921582Skchow int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
5931582Skchow #endif
5941582Skchow 
5956691Skchow #if defined(OPTERON_ERRATUM_298)
5966691Skchow int opteron_erratum_298;
5976691Skchow #endif
5986691Skchow 
5993446Smrj static void
6003446Smrj workaround_warning(cpu_t *cp, uint_t erratum)
6013446Smrj {
6023446Smrj 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
6033446Smrj 	    cp->cpu_id, erratum);
6043446Smrj }
6053446Smrj 
6063446Smrj static void
6073446Smrj workaround_applied(uint_t erratum)
6083446Smrj {
6093446Smrj 	if (erratum > 1000000)
6103446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
6113446Smrj 		    erratum);
6123446Smrj 	else
6133446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
6143446Smrj 		    erratum);
6153446Smrj }
6163446Smrj 
6173446Smrj static void
6183446Smrj msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
6193446Smrj {
6203446Smrj 	cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
6213446Smrj 	    cp->cpu_id, rw, msr, error);
6223446Smrj }
6230Sstevel@tonic-gate 
6245893Sesaxe /*
6255893Sesaxe  * Determine the number of nodes in an Opteron / Greyhound family system.
6265893Sesaxe  */
6275893Sesaxe static uint_t
6285893Sesaxe opteron_get_nnodes(void)
6295893Sesaxe {
6305893Sesaxe 	static uint_t nnodes = 0;
6315893Sesaxe 
6325893Sesaxe #ifdef	DEBUG
6335893Sesaxe 	uint_t family;
6345893Sesaxe 
6355893Sesaxe 	family = cpuid_getfamily(CPU);
6365893Sesaxe 	ASSERT(family == 0xf || family == 0x10);
6375893Sesaxe #endif	/* DEBUG */
6385893Sesaxe 
6395893Sesaxe 	if (nnodes == 0) {
6405893Sesaxe 		/*
6415893Sesaxe 		 * Obtain the number of nodes in the system from
6425893Sesaxe 		 * bits [6:4] of the Node ID register on node 0.
6435893Sesaxe 		 *
6445893Sesaxe 		 * The actual node count is NodeID[6:4] + 1
6455893Sesaxe 		 *
6465893Sesaxe 		 * The Node ID register is accessed via function 0,
6475893Sesaxe 		 * offset 0x60. Node 0 is device 24.
6485893Sesaxe 		 */
6495893Sesaxe 		nnodes = ((pci_getl_func(0, 24, 0, 0x60) & 0x70) >> 4) + 1;
6505893Sesaxe 	}
6515893Sesaxe 	return (nnodes);
6525893Sesaxe }
6535893Sesaxe 
6545084Sjohnlev #if defined(__xpv)
6555084Sjohnlev 
6565084Sjohnlev /*
6575084Sjohnlev  * On dom0, we can determine the number of physical cpus on the machine.
6585084Sjohnlev  * This number is important when figuring out what workarounds are
6595084Sjohnlev  * appropriate, so compute it now.
6605084Sjohnlev  */
6616670Stariq uint_t
6625084Sjohnlev xen_get_nphyscpus(void)
6635084Sjohnlev {
6645084Sjohnlev 	static uint_t nphyscpus = 0;
6655084Sjohnlev 
6665084Sjohnlev 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
6675084Sjohnlev 
6685084Sjohnlev 	if (nphyscpus == 0) {
6695084Sjohnlev 		xen_sysctl_t op;
6705084Sjohnlev 		xen_sysctl_physinfo_t *pi = &op.u.physinfo;
6715084Sjohnlev 
6725084Sjohnlev 		op.cmd = XEN_SYSCTL_physinfo;
6735084Sjohnlev 		op.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
6745084Sjohnlev 		if (HYPERVISOR_sysctl(&op) == 0)
6755084Sjohnlev 			nphyscpus = pi->threads_per_core *
6765084Sjohnlev 			    pi->cores_per_socket * pi->sockets_per_node *
6775084Sjohnlev 			    pi->nr_nodes;
6785084Sjohnlev 	}
6795084Sjohnlev 	return (nphyscpus);
6805084Sjohnlev }
6815084Sjohnlev #endif
6825084Sjohnlev 
6830Sstevel@tonic-gate uint_t
6846691Skchow do_erratum_298(struct cpu *cpu)
6856691Skchow {
6866691Skchow 	static int	osvwrc = -3;
6876691Skchow 	extern int	osvw_opteron_erratum(cpu_t *, uint_t);
6886691Skchow 
6896691Skchow 	/*
6906691Skchow 	 * L2 Eviction May Occur During Processor Operation To Set
6916691Skchow 	 * Accessed or Dirty Bit.
6926691Skchow 	 */
6936691Skchow 	if (osvwrc == -3) {
6946691Skchow 		osvwrc = osvw_opteron_erratum(cpu, 298);
6956691Skchow 	} else {
6966691Skchow 		/* osvw return codes should be consistent for all cpus */
6976691Skchow 		ASSERT(osvwrc == osvw_opteron_erratum(cpu, 298));
6986691Skchow 	}
6996691Skchow 
7006691Skchow 	switch (osvwrc) {
7016691Skchow 	case 0:		/* erratum is not present: do nothing */
7026691Skchow 		break;
7036691Skchow 	case 1:		/* erratum is present: BIOS workaround applied */
7046691Skchow 		/*
7056691Skchow 		 * check if workaround is actually in place and issue warning
7066691Skchow 		 * if not.
7076691Skchow 		 */
7086691Skchow 		if (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
7096691Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0)) {
7106691Skchow #if defined(OPTERON_ERRATUM_298)
7116691Skchow 			opteron_erratum_298++;
7126691Skchow #else
7136691Skchow 			workaround_warning(cpu, 298);
7146691Skchow 			return (1);
7156691Skchow #endif
7166691Skchow 		}
7176691Skchow 		break;
7186691Skchow 	case -1:	/* cannot determine via osvw: check cpuid */
7196691Skchow 		if ((cpuid_opteron_erratum(cpu, 298) > 0) &&
7206691Skchow 		    (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
7216691Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0))) {
7226691Skchow #if defined(OPTERON_ERRATUM_298)
7236691Skchow 			opteron_erratum_298++;
7246691Skchow #else
7256691Skchow 			workaround_warning(cpu, 298);
7266691Skchow 			return (1);
7276691Skchow #endif
7286691Skchow 		}
7296691Skchow 		break;
7306691Skchow 	}
7316691Skchow 	return (0);
7326691Skchow }
7336691Skchow 
7346691Skchow uint_t
7350Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	uint_t missing = 0;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	ASSERT(cpu == CPU);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	/*LINTED*/
7420Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
7430Sstevel@tonic-gate 		/*
7440Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
7450Sstevel@tonic-gate 		 */
7460Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
7470Sstevel@tonic-gate 		/*
7480Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
7490Sstevel@tonic-gate 		 */
7503446Smrj 		opteron_erratum_88++;
7510Sstevel@tonic-gate #else
7523446Smrj 		workaround_warning(cpu, 88);
7530Sstevel@tonic-gate 		missing++;
7540Sstevel@tonic-gate #endif
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
7580Sstevel@tonic-gate 		/*
7590Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
7600Sstevel@tonic-gate 		 */
7610Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
7620Sstevel@tonic-gate 		/*
7630Sstevel@tonic-gate 		 * fix is in trap.c
7640Sstevel@tonic-gate 		 */
7650Sstevel@tonic-gate 		opteron_erratum_91++;
7660Sstevel@tonic-gate #else
7673446Smrj 		workaround_warning(cpu, 91);
7680Sstevel@tonic-gate 		missing++;
7690Sstevel@tonic-gate #endif
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
7730Sstevel@tonic-gate 		/*
7740Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
7750Sstevel@tonic-gate 		 */
7760Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
7770Sstevel@tonic-gate 		/*
7780Sstevel@tonic-gate 		 * fix is in trap.c
7790Sstevel@tonic-gate 		 */
7800Sstevel@tonic-gate 		opteron_erratum_93++;
7810Sstevel@tonic-gate #else
7823446Smrj 		workaround_warning(cpu, 93);
7830Sstevel@tonic-gate 		missing++;
7840Sstevel@tonic-gate #endif
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	/*LINTED*/
7880Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
7890Sstevel@tonic-gate 		/*
7900Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
7910Sstevel@tonic-gate 		 */
7920Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
7930Sstevel@tonic-gate #if defined(_LP64)
7940Sstevel@tonic-gate 		/*
7950Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
7960Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
7970Sstevel@tonic-gate 		 * range mod 4G.
7980Sstevel@tonic-gate 		 */
7990Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
8000Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		/*LINTED*/
8030Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
8043446Smrj 		opteron_erratum_95++;
8050Sstevel@tonic-gate #endif	/* _LP64 */
8060Sstevel@tonic-gate #else
8073446Smrj 		workaround_warning(cpu, 95);
8080Sstevel@tonic-gate 		missing++;
8093446Smrj #endif
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
8130Sstevel@tonic-gate 		/*
8140Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
8150Sstevel@tonic-gate 		 */
8160Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
8170Sstevel@tonic-gate 		/*
8180Sstevel@tonic-gate 		 * fix is in trap.c
8190Sstevel@tonic-gate 		 */
8200Sstevel@tonic-gate 		opteron_erratum_100++;
8210Sstevel@tonic-gate #else
8223446Smrj 		workaround_warning(cpu, 100);
8230Sstevel@tonic-gate 		missing++;
8240Sstevel@tonic-gate #endif
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	/*LINTED*/
8280Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
8290Sstevel@tonic-gate 		/*
8300Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
8310Sstevel@tonic-gate 		 * Some Processors
8320Sstevel@tonic-gate 		 */
8330Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
8340Sstevel@tonic-gate 		/*
8350Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
8360Sstevel@tonic-gate 		 * those processors)
8370Sstevel@tonic-gate 		 */
8380Sstevel@tonic-gate #else
8393446Smrj 		workaround_warning(cpu, 108);
8400Sstevel@tonic-gate 		missing++;
8410Sstevel@tonic-gate #endif
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	/*LINTED*/
8453446Smrj 	if (cpuid_opteron_erratum(cpu, 109) > 0) do {
8460Sstevel@tonic-gate 		/*
8470Sstevel@tonic-gate 		 * Certain Reverse REP MOVS May Produce Unpredictable Behaviour
8480Sstevel@tonic-gate 		 */
8490Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
8503446Smrj 		/*
8513446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
8523446Smrj 		 */
8533446Smrj 		uint64_t value;
8543446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
8553446Smrj 		int err;
8560Sstevel@tonic-gate 
8573446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
8583446Smrj 			msr_warning(cpu, "rd", msr, err);
8593446Smrj 			workaround_warning(cpu, 109);
8603446Smrj 			missing++;
8613446Smrj 		}
8623446Smrj 		if (value == 0)
8630Sstevel@tonic-gate 			opteron_erratum_109++;
8640Sstevel@tonic-gate #else
8653446Smrj 		workaround_warning(cpu, 109);
8660Sstevel@tonic-gate 		missing++;
8670Sstevel@tonic-gate #endif
8683446Smrj 	/*CONSTANTCONDITION*/
8693446Smrj 	} while (0);
8703446Smrj 
8710Sstevel@tonic-gate 	/*LINTED*/
8720Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
8730Sstevel@tonic-gate 		/*
8740Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
8750Sstevel@tonic-gate 		 * Processor Hang
8760Sstevel@tonic-gate 		 */
8770Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
8783446Smrj #if defined(_LP64)
8790Sstevel@tonic-gate 		/*
8800Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
8810Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
8820Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
8830Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
8840Sstevel@tonic-gate 		 */
8853446Smrj 		if (opteron_erratum_121)
8863446Smrj 			opteron_erratum_121++;
8873446Smrj 		else {
8883446Smrj 			if (hole_start) {
8893446Smrj 				hole_start -= PAGESIZE;
8903446Smrj 			} else {
8913446Smrj 				/*
8923446Smrj 				 * hole_start not yet initialized by
8933446Smrj 				 * mmu_init. Initialize hole_start
8943446Smrj 				 * with value to be subtracted.
8953446Smrj 				 */
8963446Smrj 				hole_start = PAGESIZE;
8970Sstevel@tonic-gate 			}
8983446Smrj 			opteron_erratum_121++;
8990Sstevel@tonic-gate 		}
9003446Smrj #endif	/* _LP64 */
9010Sstevel@tonic-gate #else
9023446Smrj 		workaround_warning(cpu, 121);
9030Sstevel@tonic-gate 		missing++;
9040Sstevel@tonic-gate #endif
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	/*LINTED*/
9083446Smrj 	if (cpuid_opteron_erratum(cpu, 122) > 0) do {
9090Sstevel@tonic-gate 		/*
9103446Smrj 		 * TLB Flush Filter May Cause Coherency Problem in
9110Sstevel@tonic-gate 		 * Multiprocessor Systems
9120Sstevel@tonic-gate 		 */
9130Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
9143446Smrj 		uint64_t value;
9153446Smrj 		const uint_t msr = MSR_AMD_HWCR;
9163446Smrj 		int error;
9173446Smrj 
9180Sstevel@tonic-gate 		/*
9190Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
9200Sstevel@tonic-gate 		 * or multi-processor).
9210Sstevel@tonic-gate 		 */
9225084Sjohnlev #if defined(__xpv)
9235084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
9245084Sjohnlev 			break;
9255084Sjohnlev 		if (!opteron_erratum_122 && xen_get_nphyscpus() == 1)
9265084Sjohnlev 			break;
9275084Sjohnlev #else
9285893Sesaxe 		if (!opteron_erratum_122 && opteron_get_nnodes() == 1 &&
9293446Smrj 		    cpuid_get_ncpu_per_chip(cpu) == 1)
9303446Smrj 			break;
9315084Sjohnlev #endif
9323446Smrj 		/* disable TLB Flush Filter */
9333446Smrj 
9343446Smrj 		if ((error = checked_rdmsr(msr, &value)) != 0) {
9353446Smrj 			msr_warning(cpu, "rd", msr, error);
9363446Smrj 			workaround_warning(cpu, 122);
9373446Smrj 			missing++;
9383446Smrj 		} else {
9393446Smrj 			value |= (uint64_t)AMD_HWCR_FFDIS;
9403446Smrj 			if ((error = checked_wrmsr(msr, value)) != 0) {
9413446Smrj 				msr_warning(cpu, "wr", msr, error);
9423446Smrj 				workaround_warning(cpu, 122);
9433446Smrj 				missing++;
9443446Smrj 			}
9450Sstevel@tonic-gate 		}
9463446Smrj 		opteron_erratum_122++;
9470Sstevel@tonic-gate #else
9483446Smrj 		workaround_warning(cpu, 122);
9490Sstevel@tonic-gate 		missing++;
9500Sstevel@tonic-gate #endif
9513446Smrj 	/*CONSTANTCONDITION*/
9523446Smrj 	} while (0);
953302Skchow 
9540Sstevel@tonic-gate 	/*LINTED*/
9553446Smrj 	if (cpuid_opteron_erratum(cpu, 123) > 0) do {
9560Sstevel@tonic-gate 		/*
9570Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
9580Sstevel@tonic-gate 		 * Dual Core Processors
9590Sstevel@tonic-gate 		 */
9603446Smrj #if defined(OPTERON_ERRATUM_123)
9613446Smrj 		uint64_t value;
9623446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
9633446Smrj 		int err;
9643446Smrj 
9650Sstevel@tonic-gate 		/*
9660Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
9670Sstevel@tonic-gate 		 */
9683446Smrj 		if (cpuid_get_ncpu_per_chip(cpu) < 2)
9693446Smrj 			break;
9705084Sjohnlev #if defined(__xpv)
9715084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
9725084Sjohnlev 			break;
9735084Sjohnlev #endif
9743446Smrj 		/*
9753446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
9763446Smrj 		 */
9773446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
9783446Smrj 			msr_warning(cpu, "rd", msr, err);
9793446Smrj 			workaround_warning(cpu, 123);
9803446Smrj 			missing++;
9810Sstevel@tonic-gate 		}
9823446Smrj 		if (value == 0)
9833446Smrj 			opteron_erratum_123++;
9843446Smrj #else
9853446Smrj 		workaround_warning(cpu, 123);
9863446Smrj 		missing++;
987359Skucharsk 
9883446Smrj #endif
9893446Smrj 	/*CONSTANTCONDITION*/
9903446Smrj 	} while (0);
9913446Smrj 
992359Skucharsk 	/*LINTED*/
9933446Smrj 	if (cpuid_opteron_erratum(cpu, 131) > 0) do {
994359Skucharsk 		/*
995359Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
996359Skucharsk 		 * Waiting for a Probe Response
997359Skucharsk 		 */
9983446Smrj #if defined(OPTERON_ERRATUM_131)
9993446Smrj 		uint64_t nbcfg;
10003446Smrj 		const uint_t msr = MSR_AMD_NB_CFG;
10013446Smrj 		const uint64_t wabits =
10023446Smrj 		    AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
10033446Smrj 		int error;
10043446Smrj 
1005359Skucharsk 		/*
1006359Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
1007359Skucharsk 		 */
10083446Smrj 		if (opteron_erratum_131)
10093446Smrj 			break;
10105084Sjohnlev #if defined(__xpv)
10115084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
10125084Sjohnlev 			break;
10135084Sjohnlev 		if (xen_get_nphyscpus() < 4)
10145084Sjohnlev 			break;
10155084Sjohnlev #else
10165893Sesaxe 		if (opteron_get_nnodes() * cpuid_get_ncpu_per_chip(cpu) < 4)
10173446Smrj 			break;
10185084Sjohnlev #endif
10193446Smrj 		/*
10203446Smrj 		 * Print a warning if neither of the workarounds for
10213446Smrj 		 * erratum 131 is present.
10223446Smrj 		 */
10233446Smrj 		if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
10243446Smrj 			msr_warning(cpu, "rd", msr, error);
10253446Smrj 			workaround_warning(cpu, 131);
10263446Smrj 			missing++;
10273446Smrj 		} else if ((nbcfg & wabits) == 0) {
10283446Smrj 			opteron_erratum_131++;
10293446Smrj 		} else {
10303446Smrj 			/* cannot have both workarounds set */
10313446Smrj 			ASSERT((nbcfg & wabits) != wabits);
1032359Skucharsk 		}
10333446Smrj #else
10343446Smrj 		workaround_warning(cpu, 131);
10353446Smrj 		missing++;
1036359Skucharsk #endif
10373446Smrj 	/*CONSTANTCONDITION*/
10383446Smrj 	} while (0);
1039938Sesaxe 
1040938Sesaxe 	/*
10413446Smrj 	 * This isn't really an erratum, but for convenience the
1042938Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
1043938Sesaxe 	 */
1044938Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
10453446Smrj #if defined(OPTERON_WORKAROUND_6336786)
1046938Sesaxe 		/*
1047938Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
1048938Sesaxe 		 * K8 platforms to guard against TSC drift.
1049938Sesaxe 		 */
1050938Sesaxe 		if (opteron_workaround_6336786) {
1051938Sesaxe 			opteron_workaround_6336786++;
10525084Sjohnlev #if defined(__xpv)
10535084Sjohnlev 		} else if ((DOMAIN_IS_INITDOMAIN(xen_info) &&
10545084Sjohnlev 		    xen_get_nphyscpus() > 1) ||
10555084Sjohnlev 		    opteron_workaround_6336786_UP) {
10565084Sjohnlev 			/*
10575893Sesaxe 			 * XXPV	Hmm.  We can't walk the Northbridges on
10585084Sjohnlev 			 *	the hypervisor; so just complain and drive
10595084Sjohnlev 			 *	on.  This probably needs to be fixed in
10605084Sjohnlev 			 *	the hypervisor itself.
10615084Sjohnlev 			 */
10625084Sjohnlev 			opteron_workaround_6336786++;
10635084Sjohnlev 			workaround_warning(cpu, 6336786);
10645084Sjohnlev #else	/* __xpv */
10655893Sesaxe 		} else if ((opteron_get_nnodes() *
10665894Sesaxe 		    cpuid_get_ncpu_per_chip(cpu) > 1) ||
1067938Sesaxe 		    opteron_workaround_6336786_UP) {
10685893Sesaxe 
10695893Sesaxe 			uint_t	node, nnodes;
10703446Smrj 			uint8_t data;
10713446Smrj 
10725893Sesaxe 			nnodes = opteron_get_nnodes();
10735893Sesaxe 			for (node = 0; node < nnodes; node++) {
1074938Sesaxe 				/*
1075938Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
1076938Sesaxe 				 * Northbridge device is the node id + 24.
1077938Sesaxe 				 */
1078938Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
1079938Sesaxe 				data &= 0xFC;
1080938Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
1081938Sesaxe 			}
1082938Sesaxe 			opteron_workaround_6336786++;
10835084Sjohnlev #endif	/* __xpv */
1084938Sesaxe 		}
10853446Smrj #else
10863446Smrj 		workaround_warning(cpu, 6336786);
10873446Smrj 		missing++;
1088938Sesaxe #endif
10893446Smrj 	}
10901582Skchow 
10911582Skchow 	/*LINTED*/
10921582Skchow 	/*
10931582Skchow 	 * Mutex primitives don't work as expected.
10941582Skchow 	 */
10951582Skchow 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
10963446Smrj #if defined(OPTERON_WORKAROUND_6323525)
10971582Skchow 		/*
10983446Smrj 		 * This problem only occurs with 2 or more cores. If bit in
10996691Skchow 		 * MSR_AMD_BU_CFG set, then not applicable. The workaround
11001582Skchow 		 * is to patch the semaphone routines with the lfence
11011582Skchow 		 * instruction to provide necessary load memory barrier with
11021582Skchow 		 * possible subsequent read-modify-write ops.
11031582Skchow 		 *
11041582Skchow 		 * It is too early in boot to call the patch routine so
11051582Skchow 		 * set erratum variable to be done in startup_end().
11061582Skchow 		 */
11071582Skchow 		if (opteron_workaround_6323525) {
11081582Skchow 			opteron_workaround_6323525++;
11095084Sjohnlev #if defined(__xpv)
11105084Sjohnlev 		} else if (x86_feature & X86_SSE2) {
11115084Sjohnlev 			if (DOMAIN_IS_INITDOMAIN(xen_info)) {
11125084Sjohnlev 				/*
11135084Sjohnlev 				 * XXPV	Use dom0_msr here when extended
11145084Sjohnlev 				 *	operations are supported?
11155084Sjohnlev 				 */
11165084Sjohnlev 				if (xen_get_nphyscpus() > 1)
11175084Sjohnlev 					opteron_workaround_6323525++;
11185084Sjohnlev 			} else {
11195084Sjohnlev 				/*
11205084Sjohnlev 				 * We have no way to tell how many physical
11215084Sjohnlev 				 * cpus there are, or even if this processor
11225084Sjohnlev 				 * has the problem, so enable the workaround
11235084Sjohnlev 				 * unconditionally (at some performance cost).
11245084Sjohnlev 				 */
11255084Sjohnlev 				opteron_workaround_6323525++;
11265084Sjohnlev 			}
11275084Sjohnlev #else	/* __xpv */
11285893Sesaxe 		} else if ((x86_feature & X86_SSE2) && ((opteron_get_nnodes() *
11293446Smrj 		    cpuid_get_ncpu_per_chip(cpu)) > 1)) {
11306691Skchow 			if ((xrdmsr(MSR_AMD_BU_CFG) & 0x02) == 0)
11311582Skchow 				opteron_workaround_6323525++;
11325084Sjohnlev #endif	/* __xpv */
11331582Skchow 		}
11343446Smrj #else
11353446Smrj 		workaround_warning(cpu, 6323525);
11363446Smrj 		missing++;
11373446Smrj #endif
11381582Skchow 	}
11393446Smrj 
11406691Skchow 	missing += do_erratum_298(cpu);
11416691Skchow 
11425084Sjohnlev #ifdef __xpv
11435084Sjohnlev 	return (0);
11445084Sjohnlev #else
11450Sstevel@tonic-gate 	return (missing);
11465084Sjohnlev #endif
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate void
11500Sstevel@tonic-gate workaround_errata_end()
11510Sstevel@tonic-gate {
11523446Smrj #if defined(OPTERON_ERRATUM_88)
11533446Smrj 	if (opteron_erratum_88)
11543446Smrj 		workaround_applied(88);
11553446Smrj #endif
11563446Smrj #if defined(OPTERON_ERRATUM_91)
11573446Smrj 	if (opteron_erratum_91)
11583446Smrj 		workaround_applied(91);
11593446Smrj #endif
11603446Smrj #if defined(OPTERON_ERRATUM_93)
11613446Smrj 	if (opteron_erratum_93)
11623446Smrj 		workaround_applied(93);
11633446Smrj #endif
11643446Smrj #if defined(OPTERON_ERRATUM_95)
11653446Smrj 	if (opteron_erratum_95)
11663446Smrj 		workaround_applied(95);
11673446Smrj #endif
11683446Smrj #if defined(OPTERON_ERRATUM_100)
11693446Smrj 	if (opteron_erratum_100)
11703446Smrj 		workaround_applied(100);
11713446Smrj #endif
11723446Smrj #if defined(OPTERON_ERRATUM_108)
11733446Smrj 	if (opteron_erratum_108)
11743446Smrj 		workaround_applied(108);
11753446Smrj #endif
11760Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
11770Sstevel@tonic-gate 	if (opteron_erratum_109) {
1178359Skucharsk 		cmn_err(CE_WARN,
1179359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1180359Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
1181359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1182359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1183359Skucharsk 		    " system\noperation may occur.\n");
11840Sstevel@tonic-gate 	}
11853446Smrj #endif
11863446Smrj #if defined(OPTERON_ERRATUM_121)
11873446Smrj 	if (opteron_erratum_121)
11883446Smrj 		workaround_applied(121);
11893446Smrj #endif
11903446Smrj #if defined(OPTERON_ERRATUM_122)
11913446Smrj 	if (opteron_erratum_122)
11923446Smrj 		workaround_applied(122);
11933446Smrj #endif
11940Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
11950Sstevel@tonic-gate 	if (opteron_erratum_123) {
1196359Skucharsk 		cmn_err(CE_WARN,
1197359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1198359Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
1199359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1200359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1201359Skucharsk 		    " system\noperation may occur.\n");
12020Sstevel@tonic-gate 	}
12033446Smrj #endif
1204359Skucharsk #if defined(OPTERON_ERRATUM_131)
1205359Skucharsk 	if (opteron_erratum_131) {
1206359Skucharsk 		cmn_err(CE_WARN,
1207359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1208359Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
1209359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1210359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1211359Skucharsk 		    " system\noperation may occur.\n");
1212359Skucharsk 	}
12133446Smrj #endif
12143446Smrj #if defined(OPTERON_WORKAROUND_6336786)
12153446Smrj 	if (opteron_workaround_6336786)
12163446Smrj 		workaround_applied(6336786);
12173446Smrj #endif
12183446Smrj #if defined(OPTERON_WORKAROUND_6323525)
12193446Smrj 	if (opteron_workaround_6323525)
12203446Smrj 		workaround_applied(6323525);
12213446Smrj #endif
12226691Skchow #if defined(OPTERON_ERRATUM_298)
12236691Skchow 	if (opteron_erratum_298) {
12246691Skchow 		cmn_err(CE_WARN,
12256691Skchow 		    "BIOS microcode patch for AMD 64/Opteron(tm)"
12266691Skchow 		    " processor\nerratum 298 was not detected; updating your"
12276691Skchow 		    " system's BIOS to a version\ncontaining this"
12286691Skchow 		    " microcode patch is HIGHLY recommended or erroneous"
12296691Skchow 		    " system\noperation may occur.\n");
12306691Skchow 	}
12316691Skchow #endif
12320Sstevel@tonic-gate }
12330Sstevel@tonic-gate 
12343446Smrj static cpuset_t procset;
12353446Smrj 
12363446Smrj /*
12373446Smrj  * Start a single cpu, assuming that the kernel context is available
12383446Smrj  * to successfully start another cpu.
12393446Smrj  *
12403446Smrj  * (For example, real mode code is mapped into the right place
12413446Smrj  * in memory and is ready to be run.)
12423446Smrj  */
12433446Smrj int
12443446Smrj start_cpu(processorid_t who)
12453446Smrj {
12463446Smrj 	void *ctx;
12473446Smrj 	cpu_t *cp;
12483446Smrj 	int delays;
12493446Smrj 	int error = 0;
12503446Smrj 
12513446Smrj 	ASSERT(who != 0);
12523446Smrj 
12533446Smrj 	/*
12543446Smrj 	 * Check if there's at least a Mbyte of kmem available
12553446Smrj 	 * before attempting to start the cpu.
12563446Smrj 	 */
12573446Smrj 	if (kmem_avail() < 1024 * 1024) {
12583446Smrj 		/*
12593446Smrj 		 * Kick off a reap in case that helps us with
12603446Smrj 		 * later attempts ..
12613446Smrj 		 */
12623446Smrj 		kmem_reap();
12633446Smrj 		return (ENOMEM);
12643446Smrj 	}
12653446Smrj 
12663446Smrj 	cp = mp_startup_init(who);
12673446Smrj 	if ((ctx = mach_cpucontext_alloc(cp)) == NULL ||
12683446Smrj 	    (error = mach_cpu_start(cp, ctx)) != 0) {
12693446Smrj 
12703446Smrj 		/*
12713446Smrj 		 * Something went wrong before we even started it
12723446Smrj 		 */
12733446Smrj 		if (ctx)
12743446Smrj 			cmn_err(CE_WARN,
12753446Smrj 			    "cpu%d: failed to start error %d",
12763446Smrj 			    cp->cpu_id, error);
12773446Smrj 		else
12783446Smrj 			cmn_err(CE_WARN,
12793446Smrj 			    "cpu%d: failed to allocate context", cp->cpu_id);
12800Sstevel@tonic-gate 
12813446Smrj 		if (ctx)
12823446Smrj 			mach_cpucontext_free(cp, ctx, error);
12833446Smrj 		else
12843446Smrj 			error = EAGAIN;		/* hmm. */
12853446Smrj 		mp_startup_fini(cp, error);
12863446Smrj 		return (error);
12873446Smrj 	}
12883446Smrj 
12893446Smrj 	for (delays = 0; !CPU_IN_SET(procset, who); delays++) {
12903446Smrj 		if (delays == 500) {
12913446Smrj 			/*
12923446Smrj 			 * After five seconds, things are probably looking
12933446Smrj 			 * a bit bleak - explain the hang.
12943446Smrj 			 */
12953446Smrj 			cmn_err(CE_NOTE, "cpu%d: started, "
12963446Smrj 			    "but not running in the kernel yet", who);
12973446Smrj 		} else if (delays > 2000) {
12983446Smrj 			/*
12993446Smrj 			 * We waited at least 20 seconds, bail ..
13003446Smrj 			 */
13013446Smrj 			error = ETIMEDOUT;
13023446Smrj 			cmn_err(CE_WARN, "cpu%d: timed out", who);
13033446Smrj 			mach_cpucontext_free(cp, ctx, error);
13043446Smrj 			mp_startup_fini(cp, error);
13053446Smrj 			return (error);
13063446Smrj 		}
13073446Smrj 
13083446Smrj 		/*
13093446Smrj 		 * wait at least 10ms, then check again..
13103446Smrj 		 */
13113446Smrj 		delay(USEC_TO_TICK_ROUNDUP(10000));
13123446Smrj 	}
13133446Smrj 
13143446Smrj 	mach_cpucontext_free(cp, ctx, 0);
13153446Smrj 
13165084Sjohnlev #ifndef __xpv
13173446Smrj 	if (tsc_gethrtime_enable)
13183446Smrj 		tsc_sync_master(who);
13195084Sjohnlev #endif
13203446Smrj 
13213446Smrj 	if (dtrace_cpu_init != NULL) {
13223446Smrj 		/*
13233446Smrj 		 * DTrace CPU initialization expects cpu_lock to be held.
13243446Smrj 		 */
13253446Smrj 		mutex_enter(&cpu_lock);
13263446Smrj 		(*dtrace_cpu_init)(who);
13273446Smrj 		mutex_exit(&cpu_lock);
13283446Smrj 	}
13293446Smrj 
13303446Smrj 	while (!CPU_IN_SET(cpu_ready_set, who))
13313446Smrj 		delay(1);
13323446Smrj 
13333446Smrj 	return (0);
13343446Smrj }
13353446Smrj 
13362006Sandrei 
13370Sstevel@tonic-gate /*ARGSUSED*/
13380Sstevel@tonic-gate void
13390Sstevel@tonic-gate start_other_cpus(int cprboot)
13400Sstevel@tonic-gate {
13413446Smrj 	uint_t who;
13423446Smrj 	uint_t skipped = 0;
13433446Smrj 	uint_t bootcpuid = 0;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	/*
13460Sstevel@tonic-gate 	 * Initialize our own cpu_info.
13470Sstevel@tonic-gate 	 */
13480Sstevel@tonic-gate 	init_cpu_info(CPU);
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	/*
13510Sstevel@tonic-gate 	 * Initialize our syscall handlers
13520Sstevel@tonic-gate 	 */
13530Sstevel@tonic-gate 	init_cpu_syscall(CPU);
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	/*
13563446Smrj 	 * Take the boot cpu out of the mp_cpus set because we know
13573446Smrj 	 * it's already running.  Add it to the cpu_ready_set for
13583446Smrj 	 * precisely the same reason.
13593446Smrj 	 */
13603446Smrj 	CPUSET_DEL(mp_cpus, bootcpuid);
13613446Smrj 	CPUSET_ADD(cpu_ready_set, bootcpuid);
13623446Smrj 
13633446Smrj 	/*
13640Sstevel@tonic-gate 	 * if only 1 cpu or not using MP, skip the rest of this
13650Sstevel@tonic-gate 	 */
13663446Smrj 	if (CPUSET_ISNULL(mp_cpus) || use_mp == 0) {
13670Sstevel@tonic-gate 		if (use_mp == 0)
13680Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
13690Sstevel@tonic-gate 		goto done;
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	/*
13730Sstevel@tonic-gate 	 * perform such initialization as is needed
13740Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
13750Sstevel@tonic-gate 	 */
13760Sstevel@tonic-gate 	cpu_pause_init();
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	xc_init();		/* initialize processor crosscalls */
13790Sstevel@tonic-gate 
13803446Smrj 	if (mach_cpucontext_init() != 0)
13810Sstevel@tonic-gate 		goto done;
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	flushes_require_xcalls = 1;
13840Sstevel@tonic-gate 
13852575Snf202958 	/*
13862575Snf202958 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
13872575Snf202958 	 * do their TSC syncs with the same CPU.
13882575Snf202958 	 */
13890Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
13922575Snf202958 
13932006Sandrei 		if (!CPU_IN_SET(mp_cpus, who))
13942006Sandrei 			continue;
13953446Smrj 		ASSERT(who != bootcpuid);
13962006Sandrei 		if (ncpus >= max_ncpus) {
13972006Sandrei 			skipped = who;
13980Sstevel@tonic-gate 			continue;
13992006Sandrei 		}
14003446Smrj 		if (start_cpu(who) != 0)
14013446Smrj 			CPUSET_DEL(mp_cpus, who);
14020Sstevel@tonic-gate 	}
14030Sstevel@tonic-gate 
14045084Sjohnlev #if !defined(__xpv)
14054581Ssherrym 	/* Free the space allocated to hold the microcode file */
14064581Ssherrym 	ucode_free();
14075084Sjohnlev #endif
14084581Ssherrym 
14090Sstevel@tonic-gate 	affinity_clear();
14100Sstevel@tonic-gate 
14112006Sandrei 	if (skipped) {
14122006Sandrei 		cmn_err(CE_NOTE,
14133446Smrj 		    "System detected %d cpus, but "
14143446Smrj 		    "only %d cpu(s) were enabled during boot.",
14152006Sandrei 		    skipped + 1, ncpus);
14162006Sandrei 		cmn_err(CE_NOTE,
14172006Sandrei 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
14182006Sandrei 		    "See eeprom(1M).");
14192006Sandrei 	}
14202006Sandrei 
14210Sstevel@tonic-gate done:
14220Sstevel@tonic-gate 	workaround_errata_end();
14233446Smrj 	mach_cpucontext_fini();
14241642Sgavinm 
14251642Sgavinm 	cmi_post_mpstartup();
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate /*
14290Sstevel@tonic-gate  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
14300Sstevel@tonic-gate  */
14310Sstevel@tonic-gate /*ARGSUSED*/
14320Sstevel@tonic-gate int
14330Sstevel@tonic-gate mp_cpu_configure(int cpuid)
14340Sstevel@tonic-gate {
14350Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate /*ARGSUSED*/
14390Sstevel@tonic-gate int
14400Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
14410Sstevel@tonic-gate {
14420Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
14430Sstevel@tonic-gate }
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate /*
14460Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
14472985Sdmick  * Called from real_mode_start.
14481251Skchow  *
14491251Skchow  * WARNING: until CPU_READY is set, mp_startup and routines called by
14501251Skchow  * mp_startup should not call routines (e.g. kmem_free) that could call
14511251Skchow  * hat_unload which requires CPU_READY to be set.
14520Sstevel@tonic-gate  */
14530Sstevel@tonic-gate void
14540Sstevel@tonic-gate mp_startup(void)
14550Sstevel@tonic-gate {
14560Sstevel@tonic-gate 	struct cpu *cp = CPU;
14570Sstevel@tonic-gate 	uint_t new_x86_feature;
14580Sstevel@tonic-gate 
14592985Sdmick 	/*
14603021Sdmick 	 * We need to get TSC on this proc synced (i.e., any delta
14613021Sdmick 	 * from cpu0 accounted for) as soon as we can, because many
14623021Sdmick 	 * many things use gethrtime/pc_gethrestime, including
14633021Sdmick 	 * interrupts, cmn_err, etc.
14643021Sdmick 	 */
14653021Sdmick 
14663021Sdmick 	/* Let cpu0 continue into tsc_sync_master() */
14673021Sdmick 	CPUSET_ATOMIC_ADD(procset, cp->cpu_id);
14683021Sdmick 
14695084Sjohnlev #ifndef __xpv
14703021Sdmick 	if (tsc_gethrtime_enable)
14713021Sdmick 		tsc_sync_slave();
14725084Sjohnlev #endif
14733021Sdmick 
14743021Sdmick 	/*
14752985Sdmick 	 * Once this was done from assembly, but it's safer here; if
14762985Sdmick 	 * it blocks, we need to be able to swtch() to and from, and
14772985Sdmick 	 * since we get here by calling t_pc, we need to do that call
14782985Sdmick 	 * before swtch() overwrites it.
14792985Sdmick 	 */
14802985Sdmick 	(void) (*ap_mlsetup)();
14812985Sdmick 
14820Sstevel@tonic-gate 	new_x86_feature = cpuid_pass1(cp);
14830Sstevel@tonic-gate 
14845084Sjohnlev #ifndef __xpv
14850Sstevel@tonic-gate 	/*
14865159Sjohnlev 	 * Program this cpu's PAT
14870Sstevel@tonic-gate 	 */
14885159Sjohnlev 	if (x86_feature & X86_PAT)
14895159Sjohnlev 		pat_sync();
14905084Sjohnlev #endif
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	/*
14933446Smrj 	 * Set up TSC_AUX to contain the cpuid for this processor
14943446Smrj 	 * for the rdtscp instruction.
14953446Smrj 	 */
14963446Smrj 	if (x86_feature & X86_TSCP)
14973446Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
14983446Smrj 
14993446Smrj 	/*
15000Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
15010Sstevel@tonic-gate 	 */
15020Sstevel@tonic-gate 	init_cpu_syscall(cp);
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	/*
15050Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
15060Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
15070Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
15080Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
15090Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
15100Sstevel@tonic-gate 	 * calls before CPU_READY is set.
15110Sstevel@tonic-gate 	 */
15123446Smrj 	splx(ipltospl(LOCK_LEVEL));
15133446Smrj 	sti();
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	/*
15160Sstevel@tonic-gate 	 * Do a sanity check to make sure this new CPU is a sane thing
15170Sstevel@tonic-gate 	 * to add to the collection of processors running this system.
15180Sstevel@tonic-gate 	 *
15190Sstevel@tonic-gate 	 * XXX	Clearly this needs to get more sophisticated, if x86
15200Sstevel@tonic-gate 	 * systems start to get built out of heterogenous CPUs; as is
15210Sstevel@tonic-gate 	 * likely to happen once the number of processors in a configuration
15220Sstevel@tonic-gate 	 * gets large enough.
15230Sstevel@tonic-gate 	 */
15240Sstevel@tonic-gate 	if ((x86_feature & new_x86_feature) != x86_feature) {
15250Sstevel@tonic-gate 		cmn_err(CE_CONT, "?cpu%d: %b\n",
15260Sstevel@tonic-gate 		    cp->cpu_id, new_x86_feature, FMT_X86_FEATURE);
15270Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
15280Sstevel@tonic-gate 	}
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	/*
15314481Sbholler 	 * We do not support cpus with mixed monitor/mwait support if the
15324481Sbholler 	 * boot cpu supports monitor/mwait.
15334481Sbholler 	 */
15344481Sbholler 	if ((x86_feature & ~new_x86_feature) & X86_MWAIT)
15354481Sbholler 		panic("unsupported mixed cpu monitor/mwait support detected");
15364481Sbholler 
15374481Sbholler 	/*
15380Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
15390Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
15400Sstevel@tonic-gate 	 * answer of dieing horribly.  Provided the boot cpu is ok,
15410Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
15420Sstevel@tonic-gate 	 */
15430Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
15440Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	cpuid_pass2(cp);
15470Sstevel@tonic-gate 	cpuid_pass3(cp);
15480Sstevel@tonic-gate 	(void) cpuid_pass4(cp);
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	init_cpu_info(cp);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
15530Sstevel@tonic-gate 	/*
15543434Sesaxe 	 * Processor group initialization for this CPU is dependent on the
15553434Sesaxe 	 * cpuid probing, which must be done in the context of the current
15563434Sesaxe 	 * CPU.
15570Sstevel@tonic-gate 	 */
15583434Sesaxe 	pghw_physid_create(cp);
15593434Sesaxe 	pg_cpu_init(cp);
15603434Sesaxe 	pg_cmt_cpu_startup(cp);
15610Sstevel@tonic-gate 
1562*6749Ssherrym 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_EXISTS;
15632575Snf202958 
15642575Snf202958 	if (dtrace_cpu_init != NULL) {
15652575Snf202958 		(*dtrace_cpu_init)(cp->cpu_id);
15662575Snf202958 	}
15672575Snf202958 
15685084Sjohnlev #if !defined(__xpv)
15694581Ssherrym 	/*
15704581Ssherrym 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
15714581Ssherrym 	 */
15724581Ssherrym 	ucode_check(cp);
15735084Sjohnlev #endif
15744581Ssherrym 
15750Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
15760Sstevel@tonic-gate 
15773029Ssethg 	/*
15783029Ssethg 	 * Enable preemption here so that contention for any locks acquired
15793029Ssethg 	 * later in mp_startup may be preempted if the thread owning those
15803029Ssethg 	 * locks is continously executing on other CPUs (for example, this
15813029Ssethg 	 * CPU must be preemptible to allow other CPUs to pause it during their
15823029Ssethg 	 * startup phases).  It's safe to enable preemption here because the
15833029Ssethg 	 * CPU state is pretty-much fully constructed.
15843029Ssethg 	 */
15853029Ssethg 	curthread->t_preempt = 0;
15863029Ssethg 
15871482Ssethg 	/* The base spl should still be at LOCK LEVEL here */
15881482Ssethg 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
15891482Ssethg 	set_base_spl();		/* Restore the spl to its proper value */
15901482Ssethg 
1591*6749Ssherrym 	/* Enable interrupts */
1592*6749Ssherrym 	(void) spl0();
1593*6749Ssherrym 	mutex_enter(&cpu_lock);
1594*6749Ssherrym 	cpu_enable_intr(cp);
1595*6749Ssherrym 	cpu_add_active(cp);
1596*6749Ssherrym 	mutex_exit(&cpu_lock);
1597*6749Ssherrym 
1598*6749Ssherrym 	add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi);
15990Sstevel@tonic-gate 
16005254Sgavinm #ifndef __xpv
16015254Sgavinm 	{
16025254Sgavinm 		/*
16035254Sgavinm 		 * Set up the CPU module for this CPU.  This can't be done
16045254Sgavinm 		 * before this CPU is made CPU_READY, because we may (in
16055254Sgavinm 		 * heterogeneous systems) need to go load another CPU module.
16065254Sgavinm 		 * The act of attempting to load a module may trigger a
16075254Sgavinm 		 * cross-call, which will ASSERT unless this cpu is CPU_READY.
16085254Sgavinm 		 */
16095254Sgavinm 		cmi_hdl_t hdl;
16101414Scindi 
16115254Sgavinm 		if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
16125254Sgavinm 		    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
16135254Sgavinm 			if (x86_feature & X86_MCA)
16145254Sgavinm 				cmi_mca_init(hdl);
16155254Sgavinm 		}
16165254Sgavinm 	}
16175254Sgavinm #endif /* __xpv */
16181414Scindi 
16190Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
16203446Smrj 		kdi_cpu_init();
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	/*
16230Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
16240Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
16250Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
16260Sstevel@tonic-gate 	 */
16270Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	/*
16300Sstevel@tonic-gate 	 * Because mp_startup() gets fired off after init() starts, we
16310Sstevel@tonic-gate 	 * can't use the '?' trick to do 'boot -v' printing - so we
16320Sstevel@tonic-gate 	 * always direct the 'cpu .. online' messages to the log.
16330Sstevel@tonic-gate 	 */
16340Sstevel@tonic-gate 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
16350Sstevel@tonic-gate 	    cp->cpu_id);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	/*
16380Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
16390Sstevel@tonic-gate 	 */
16400Sstevel@tonic-gate 	thread_exit();
16410Sstevel@tonic-gate 	panic("mp_startup: cannot return");
16420Sstevel@tonic-gate 	/*NOTREACHED*/
16430Sstevel@tonic-gate }
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate /*
16470Sstevel@tonic-gate  * Start CPU on user request.
16480Sstevel@tonic-gate  */
16490Sstevel@tonic-gate /* ARGSUSED */
16500Sstevel@tonic-gate int
16510Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
16520Sstevel@tonic-gate {
16530Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16540Sstevel@tonic-gate 	return (0);
16550Sstevel@tonic-gate }
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate /*
16580Sstevel@tonic-gate  * Stop CPU on user request.
16590Sstevel@tonic-gate  */
16600Sstevel@tonic-gate /* ARGSUSED */
16610Sstevel@tonic-gate int
16620Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
16630Sstevel@tonic-gate {
16641389Sdmick 	extern int cbe_psm_timer_mode;
16650Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16661389Sdmick 
16675084Sjohnlev #ifdef __xpv
16685084Sjohnlev 	/*
16695084Sjohnlev 	 * We can't offline vcpu0.
16705084Sjohnlev 	 */
16715084Sjohnlev 	if (cp->cpu_id == 0)
16725084Sjohnlev 		return (EBUSY);
16735084Sjohnlev #endif
16745084Sjohnlev 
16751389Sdmick 	/*
16761389Sdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
16771389Sdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
16781389Sdmick 	 */
16791389Sdmick 
16801389Sdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
16815084Sjohnlev 		return (EBUSY);
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	return (0);
16840Sstevel@tonic-gate }
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate /*
16870Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
16880Sstevel@tonic-gate  */
16890Sstevel@tonic-gate int
16900Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
16910Sstevel@tonic-gate {
16920Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
16930Sstevel@tonic-gate 		return (EBUSY);
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
16960Sstevel@tonic-gate 	return (0);
16970Sstevel@tonic-gate }
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate /*
17000Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
17010Sstevel@tonic-gate  */
17020Sstevel@tonic-gate void
17030Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
17040Sstevel@tonic-gate {
17050Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
17060Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
17070Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 
17115254Sgavinm /*ARGSUSED*/
17120Sstevel@tonic-gate void
17130Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
17141414Scindi {
17155254Sgavinm #ifndef __xpv
17165254Sgavinm 	cmi_hdl_t hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
17175254Sgavinm 	    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
17185254Sgavinm 
17195254Sgavinm 	if (hdl != NULL) {
17205254Sgavinm 		cmi_faulted_enter(hdl);
17215254Sgavinm 		cmi_hdl_rele(hdl);
17225254Sgavinm 	}
17235254Sgavinm #endif
17241414Scindi }
17250Sstevel@tonic-gate 
17265254Sgavinm /*ARGSUSED*/
17270Sstevel@tonic-gate void
17280Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
17291414Scindi {
17305254Sgavinm #ifndef __xpv
17315254Sgavinm 	cmi_hdl_t hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
17325254Sgavinm 	    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
17335254Sgavinm 
17345254Sgavinm 	if (hdl != NULL) {
17355254Sgavinm 		cmi_faulted_exit(hdl);
17365254Sgavinm 		cmi_hdl_rele(hdl);
17375254Sgavinm 	}
17385254Sgavinm #endif
17391414Scindi }
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate /*
17420Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
17430Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
17440Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
17450Sstevel@tonic-gate  * organization rather than speed.  We could avoid checking x86_feature at every
17460Sstevel@tonic-gate  * context switch by installing different context ops, depending on the
17470Sstevel@tonic-gate  * x86_feature flags, at LDT creation time -- one for each combination of fast
17480Sstevel@tonic-gate  * syscall feature flags.
17490Sstevel@tonic-gate  */
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate /*ARGSUSED*/
17520Sstevel@tonic-gate void
17530Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg)
17540Sstevel@tonic-gate {
17553446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
17560Sstevel@tonic-gate 		cpu_sep_disable();
17573446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
17580Sstevel@tonic-gate 		cpu_asysc_disable();
17590Sstevel@tonic-gate }
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate /*ARGSUSED*/
17620Sstevel@tonic-gate void
17630Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg)
17640Sstevel@tonic-gate {
17653446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
17660Sstevel@tonic-gate 		cpu_sep_enable();
17673446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
17680Sstevel@tonic-gate 		cpu_asysc_enable();
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate static void
17720Sstevel@tonic-gate cpu_sep_enable(void)
17730Sstevel@tonic-gate {
17740Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
17750Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
17760Sstevel@tonic-gate 
1777770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
17780Sstevel@tonic-gate }
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate static void
17810Sstevel@tonic-gate cpu_sep_disable(void)
17820Sstevel@tonic-gate {
17830Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
17840Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	/*
17870Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
17880Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
17890Sstevel@tonic-gate 	 */
17903446Smrj 	wrmsr(MSR_INTC_SEP_CS, 0);
17910Sstevel@tonic-gate }
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate static void
17940Sstevel@tonic-gate cpu_asysc_enable(void)
17950Sstevel@tonic-gate {
17960Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
17970Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
17980Sstevel@tonic-gate 
1799770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1800770Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
18010Sstevel@tonic-gate }
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate static void
18040Sstevel@tonic-gate cpu_asysc_disable(void)
18050Sstevel@tonic-gate {
18060Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
18070Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	/*
18100Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
18110Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
18120Sstevel@tonic-gate 	 */
1813770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1814770Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
18150Sstevel@tonic-gate }
1816