xref: /onnv-gate/usr/src/uts/i86pc/os/mp_startup.c (revision 7605:b4a19682e632)
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 #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/thread.h>
290Sstevel@tonic-gate #include <sys/cpuvar.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/disp.h>
340Sstevel@tonic-gate #include <sys/class.h>
350Sstevel@tonic-gate #include <sys/cmn_err.h>
360Sstevel@tonic-gate #include <sys/debug.h>
370Sstevel@tonic-gate #include <sys/asm_linkage.h>
380Sstevel@tonic-gate #include <sys/x_call.h>
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/var.h>
410Sstevel@tonic-gate #include <sys/vtrace.h>
420Sstevel@tonic-gate #include <vm/hat.h>
430Sstevel@tonic-gate #include <vm/as.h>
440Sstevel@tonic-gate #include <vm/seg_kmem.h>
453446Smrj #include <vm/seg_kp.h>
460Sstevel@tonic-gate #include <sys/segments.h>
470Sstevel@tonic-gate #include <sys/kmem.h>
480Sstevel@tonic-gate #include <sys/stack.h>
490Sstevel@tonic-gate #include <sys/smp_impldefs.h>
500Sstevel@tonic-gate #include <sys/x86_archext.h>
510Sstevel@tonic-gate #include <sys/machsystm.h>
520Sstevel@tonic-gate #include <sys/traptrace.h>
530Sstevel@tonic-gate #include <sys/clock.h>
540Sstevel@tonic-gate #include <sys/cpc_impl.h>
553434Sesaxe #include <sys/pg.h>
563434Sesaxe #include <sys/cmt.h>
570Sstevel@tonic-gate #include <sys/dtrace.h>
580Sstevel@tonic-gate #include <sys/archsystm.h>
590Sstevel@tonic-gate #include <sys/fp.h>
600Sstevel@tonic-gate #include <sys/reboot.h>
613446Smrj #include <sys/kdi_machimpl.h>
620Sstevel@tonic-gate #include <vm/hat_i86.h>
630Sstevel@tonic-gate #include <sys/memnode.h>
64938Sesaxe #include <sys/pci_cfgspace.h>
653446Smrj #include <sys/mach_mmu.h>
663446Smrj #include <sys/sysmacros.h>
675084Sjohnlev #if defined(__xpv)
685084Sjohnlev #include <sys/hypervisor.h>
695084Sjohnlev #endif
701414Scindi #include <sys/cpu_module.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate struct cpu	cpus[1];			/* CPU data */
730Sstevel@tonic-gate struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
740Sstevel@tonic-gate cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
773446Smrj  * Useful for disabling MP bring-up on a MP capable system.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate int use_mp = 1;
800Sstevel@tonic-gate 
812006Sandrei /*
823446Smrj  * to be set by a PSM to indicate what cpus
833446Smrj  * are sitting around on the system.
842006Sandrei  */
853446Smrj cpuset_t mp_cpus;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * This variable is used by the hat layer to decide whether or not
890Sstevel@tonic-gate  * critical sections are needed to prevent race conditions.  For sun4m,
900Sstevel@tonic-gate  * this variable is set once enough MP initialization has been done in
910Sstevel@tonic-gate  * order to allow cross calls.
920Sstevel@tonic-gate  */
933446Smrj int flushes_require_xcalls;
946336Sbholler 
956336Sbholler cpuset_t cpu_ready_set;		/* initialized in startup() */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static 	void	mp_startup(void);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static void cpu_sep_enable(void);
1000Sstevel@tonic-gate static void cpu_sep_disable(void);
1010Sstevel@tonic-gate static void cpu_asysc_enable(void);
1020Sstevel@tonic-gate static void cpu_asysc_disable(void);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * Init CPU info - get CPU type info for processor_info system call.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate void
1080Sstevel@tonic-gate init_cpu_info(struct cpu *cp)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	processor_info_t *pi = &cp->cpu_type_info;
1110Sstevel@tonic-gate 	char buf[CPU_IDSTRLEN];
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * Get clock-frequency property for the CPU.
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	pi->pi_clock = cpu_freq;
1170Sstevel@tonic-gate 
1184667Smh27603 	/*
1194667Smh27603 	 * Current frequency in Hz.
1204667Smh27603 	 */
1214718Smh27603 	cp->cpu_curr_clock = cpu_freq_hz;
1224667Smh27603 
1234877Smh27603 	/*
1244877Smh27603 	 * Supported frequencies.
1254877Smh27603 	 */
1264877Smh27603 	cpu_set_supp_freqs(cp, NULL);
1274877Smh27603 
1280Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1290Sstevel@tonic-gate 	if (fpu_exists)
1300Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	(void) cpuid_getidstr(cp, buf, sizeof (buf));
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1350Sstevel@tonic-gate 	(void) strcpy(cp->cpu_idstr, buf);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	(void) cpuid_getbrandstr(cp, buf, sizeof (buf));
1400Sstevel@tonic-gate 	cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1410Sstevel@tonic-gate 	(void) strcpy(cp->cpu_brandstr, buf);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate  * Configure syscall support on this CPU.
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate /*ARGSUSED*/
1505295Srandyf void
1510Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	kpreempt_disable();
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate #if defined(__amd64)
1563446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC)) {
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate #if !defined(__lint)
1590Sstevel@tonic-gate 		/*
1600Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1610Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1620Sstevel@tonic-gate 		 * here.
1630Sstevel@tonic-gate 		 */
1640Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1650Sstevel@tonic-gate 		ASSERT(UDS_SEL == U32CS_SEL + 8);
1660Sstevel@tonic-gate 		ASSERT(UCS_SEL == U32CS_SEL + 16);
1670Sstevel@tonic-gate #endif
1680Sstevel@tonic-gate 		/*
1690Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1700Sstevel@tonic-gate 		 */
1710Sstevel@tonic-gate 		cpu_asysc_enable();
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 		/*
1740Sstevel@tonic-gate 		 * Program the magic registers ..
1750Sstevel@tonic-gate 		 */
1763446Smrj 		wrmsr(MSR_AMD_STAR,
1773446Smrj 		    ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
178770Skucharsk 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
179770Skucharsk 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		/*
1820Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
1830Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
1840Sstevel@tonic-gate 		 */
185770Skucharsk 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate #endif
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
1910Sstevel@tonic-gate 	 * hard to use syscall/sysret, and it is more portable anyway.
1920Sstevel@tonic-gate 	 *
1930Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
1940Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
1950Sstevel@tonic-gate 	 */
1963446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP)) {
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate #if !defined(__lint)
1990Sstevel@tonic-gate 		/*
2000Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
2010Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
2020Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
2030Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
2040Sstevel@tonic-gate 		 * Instruction Set Reference"
2050Sstevel@tonic-gate 		 */
2060Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
2090Sstevel@tonic-gate 		ASSERT32(UDS_SEL == UCS_SEL + 8);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
2120Sstevel@tonic-gate 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
2130Sstevel@tonic-gate #endif
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 		cpu_sep_enable();
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 		/*
2180Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2190Sstevel@tonic-gate 		 * via a context handler.
2200Sstevel@tonic-gate 		 */
2213446Smrj 		wrmsr(MSR_INTC_SEP_ESP, 0);
222770Skucharsk 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	kpreempt_enable();
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * Multiprocessor initialization.
2300Sstevel@tonic-gate  *
2310Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2320Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
2330Sstevel@tonic-gate  */
2343446Smrj struct cpu *
2350Sstevel@tonic-gate mp_startup_init(int cpun)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	struct cpu *cp;
2380Sstevel@tonic-gate 	kthread_id_t tp;
2390Sstevel@tonic-gate 	caddr_t	sp;
2400Sstevel@tonic-gate 	proc_t *procp;
2415084Sjohnlev #if !defined(__xpv)
2425045Sbholler 	extern int idle_cpu_prefer_mwait;
2435084Sjohnlev #endif
2440Sstevel@tonic-gate 	extern void idle();
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate #ifdef TRAPTRACE
2470Sstevel@tonic-gate 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
2480Sstevel@tonic-gate #endif
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
2510Sstevel@tonic-gate 
2523446Smrj 	cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
2535084Sjohnlev #if !defined(__xpv)
2545045Sbholler 	if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait)
2555045Sbholler 		cp->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
2565084Sjohnlev #endif
2574481Sbholler 
2580Sstevel@tonic-gate 	procp = curthread->t_procp;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * Initialize the dispatcher first.
2630Sstevel@tonic-gate 	 */
2640Sstevel@tonic-gate 	disp_cpu_init(cp);
2650Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2660Sstevel@tonic-gate 
267414Skchow 	cpu_vm_data_init(cp);
268414Skchow 
2690Sstevel@tonic-gate 	/*
2700Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
2710Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
2720Sstevel@tonic-gate 	 * when the CPU starts running.
2730Sstevel@tonic-gate 	 */
2740Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
2750Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
2790Sstevel@tonic-gate 	 * as soon as the CPU comes online.
2800Sstevel@tonic-gate 	 *
2810Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
2820Sstevel@tonic-gate 	 * thread_create().
2830Sstevel@tonic-gate 	 */
2840Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
2850Sstevel@tonic-gate 	tp->t_preempt = 1;
2860Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
2870Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
2880Sstevel@tonic-gate 	tp->t_cpu = cp;
2890Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	/*
2920Sstevel@tonic-gate 	 * Setup thread to start in mp_startup.
2930Sstevel@tonic-gate 	 */
2940Sstevel@tonic-gate 	sp = tp->t_stk;
2950Sstevel@tonic-gate 	tp->t_pc = (uintptr_t)mp_startup;
2960Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
2973446Smrj #if defined(__amd64)
2983446Smrj 	tp->t_sp -= STACK_ENTRY_ALIGN;		/* fake a call */
2993446Smrj #endif
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	cp->cpu_id = cpun;
3020Sstevel@tonic-gate 	cp->cpu_self = cp;
3030Sstevel@tonic-gate 	cp->cpu_thread = tp;
3040Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
3050Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
3060Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/*
3091482Ssethg 	 * cpu_base_spl must be set explicitly here to prevent any blocking
3101482Ssethg 	 * operations in mp_startup from causing the spl of the cpu to drop
3111482Ssethg 	 * to 0 (allowing device interrupts before we're ready) in resume().
3121482Ssethg 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
3131482Ssethg 	 * As an extra bit of security on DEBUG kernels, this is enforced with
3141482Ssethg 	 * an assertion in mp_startup() -- before cpu_base_spl is set to its
3151482Ssethg 	 * proper value.
3161482Ssethg 	 */
3171482Ssethg 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
3181482Ssethg 
3191482Ssethg 	/*
3200Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
3210Sstevel@tonic-gate 	 */
3220Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	tp->t_preempt = 1;
3270Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3280Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3290Sstevel@tonic-gate 	tp->t_cpu = cp;
3300Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3333434Sesaxe 	 * Bootstrap the CPU's PG data
33460Sesaxe 	 */
3353434Sesaxe 	pg_cpu_bootstrap(cp);
33660Sesaxe 
33760Sesaxe 	/*
3383446Smrj 	 * Perform CPC initialization on the new CPU.
3390Sstevel@tonic-gate 	 */
3400Sstevel@tonic-gate 	kcpc_hw_init(cp);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	/*
3430Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
3440Sstevel@tonic-gate 	 * for each CPU.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/*
3493446Smrj 	 * Allocate page for new GDT and initialize from current GDT.
3500Sstevel@tonic-gate 	 */
3513446Smrj #if !defined(__lint)
3523446Smrj 	ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
3533446Smrj #endif
3545460Sjosephb 	cp->cpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
3555460Sjosephb 	bcopy(CPU->cpu_gdt, cp->cpu_gdt, (sizeof (*cp->cpu_gdt) * NGDT));
3561626Srab 
3573446Smrj #if defined(__i386)
3580Sstevel@tonic-gate 	/*
3590Sstevel@tonic-gate 	 * setup kernel %gs.
3600Sstevel@tonic-gate 	 */
3610Sstevel@tonic-gate 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
3620Sstevel@tonic-gate 	    SEL_KPL, 0, 1);
3633446Smrj #endif
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/*
3660Sstevel@tonic-gate 	 * If we have more than one node, each cpu gets a copy of IDT
3670Sstevel@tonic-gate 	 * local to its node. If this is a Pentium box, we use cpu 0's
3680Sstevel@tonic-gate 	 * IDT. cpu 0's IDT has been made read-only to workaround the
3690Sstevel@tonic-gate 	 * cmpxchgl register bug
3700Sstevel@tonic-gate 	 */
3710Sstevel@tonic-gate 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
3725460Sjosephb #if !defined(__lint)
3735460Sjosephb 		ASSERT((sizeof (*CPU->cpu_idt) * NIDT) <= PAGESIZE);
3745460Sjosephb #endif
3755460Sjosephb 		cp->cpu_idt = kmem_zalloc(PAGESIZE, KM_SLEEP);
3765460Sjosephb 		bcopy(CPU->cpu_idt, cp->cpu_idt, PAGESIZE);
3773446Smrj 	} else {
3785460Sjosephb 		cp->cpu_idt = CPU->cpu_idt;
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/*
3823446Smrj 	 * Get interrupt priority data from cpu 0.
3830Sstevel@tonic-gate 	 */
3840Sstevel@tonic-gate 	cp->cpu_pri_data = CPU->cpu_pri_data;
3850Sstevel@tonic-gate 
3863446Smrj 	/*
3873446Smrj 	 * alloc space for cpuid info
3883446Smrj 	 */
3893446Smrj 	cpuid_alloc_space(cp);
3903446Smrj 
3914581Ssherrym 	/*
3924581Ssherrym 	 * alloc space for ucode_info
3934581Ssherrym 	 */
3944581Ssherrym 	ucode_alloc_space(cp);
3954581Ssherrym 
3960Sstevel@tonic-gate 	hat_cpu_online(cp);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate #ifdef TRAPTRACE
3990Sstevel@tonic-gate 	/*
4003446Smrj 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
4030Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
4040Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
4050Sstevel@tonic-gate #endif
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * Record that we have another CPU.
4080Sstevel@tonic-gate 	 */
4090Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
4100Sstevel@tonic-gate 	/*
4110Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
4120Sstevel@tonic-gate 	 */
4131455Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
4140Sstevel@tonic-gate 	/*
4150Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
4160Sstevel@tonic-gate 	 * after mp_startup().
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 	cpu_add_unit(cp);
4190Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
4203446Smrj 
4213446Smrj 	return (cp);
4223446Smrj }
4233446Smrj 
4243446Smrj /*
4253446Smrj  * Undo what was done in mp_startup_init
4263446Smrj  */
4273446Smrj static void
4283446Smrj mp_startup_fini(struct cpu *cp, int error)
4293446Smrj {
4303446Smrj 	mutex_enter(&cpu_lock);
4313446Smrj 
4323446Smrj 	/*
4333446Smrj 	 * Remove the CPU from the list of available CPUs.
4343446Smrj 	 */
4353446Smrj 	cpu_del_unit(cp->cpu_id);
4363446Smrj 
4373446Smrj 	if (error == ETIMEDOUT) {
4383446Smrj 		/*
4393446Smrj 		 * The cpu was started, but never *seemed* to run any
4403446Smrj 		 * code in the kernel; it's probably off spinning in its
4413446Smrj 		 * own private world, though with potential references to
4423446Smrj 		 * our kmem-allocated IDTs and GDTs (for example).
4433446Smrj 		 *
4443446Smrj 		 * Worse still, it may actually wake up some time later,
4453446Smrj 		 * so rather than guess what it might or might not do, we
4463446Smrj 		 * leave the fundamental data structures intact.
4473446Smrj 		 */
4483446Smrj 		cp->cpu_flags = 0;
4493446Smrj 		mutex_exit(&cpu_lock);
4503446Smrj 		return;
4513446Smrj 	}
4523446Smrj 
4533446Smrj 	/*
4543446Smrj 	 * At this point, the only threads bound to this CPU should
4553446Smrj 	 * special per-cpu threads: it's idle thread, it's pause threads,
4563446Smrj 	 * and it's interrupt threads.  Clean these up.
4573446Smrj 	 */
4583446Smrj 	cpu_destroy_bound_threads(cp);
4593446Smrj 	cp->cpu_idle_thread = NULL;
4603446Smrj 
4613446Smrj 	/*
4623446Smrj 	 * Free the interrupt stack.
4633446Smrj 	 */
4643446Smrj 	segkp_release(segkp,
4653446Smrj 	    cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
4663446Smrj 
4673446Smrj 	mutex_exit(&cpu_lock);
4683446Smrj 
4693446Smrj #ifdef TRAPTRACE
4703446Smrj 	/*
4713446Smrj 	 * Discard the trap trace buffer
4723446Smrj 	 */
4733446Smrj 	{
4743446Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
4753446Smrj 
4763446Smrj 		kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
4773446Smrj 		ttc->ttc_first = NULL;
4783446Smrj 	}
4793446Smrj #endif
4803446Smrj 
4813446Smrj 	hat_cpu_offline(cp);
4823446Smrj 
4833446Smrj 	cpuid_free_space(cp);
4843446Smrj 
4854581Ssherrym 	ucode_free_space(cp);
4864581Ssherrym 
4875460Sjosephb 	if (cp->cpu_idt != CPU->cpu_idt)
4885460Sjosephb 		kmem_free(cp->cpu_idt, PAGESIZE);
4895460Sjosephb 	cp->cpu_idt = NULL;
4903446Smrj 
4915460Sjosephb 	kmem_free(cp->cpu_gdt, PAGESIZE);
4925460Sjosephb 	cp->cpu_gdt = NULL;
4933446Smrj 
4943446Smrj 	teardown_vaddr_for_ppcopy(cp);
4953446Smrj 
4963446Smrj 	kcpc_hw_fini(cp);
4973446Smrj 
4983446Smrj 	cp->cpu_dispthread = NULL;
4993446Smrj 	cp->cpu_thread = NULL;	/* discarded by cpu_destroy_bound_threads() */
5003446Smrj 
5013446Smrj 	cpu_vm_data_destroy(cp);
5023446Smrj 
5033446Smrj 	mutex_enter(&cpu_lock);
5043446Smrj 	disp_cpu_fini(cp);
5053446Smrj 	mutex_exit(&cpu_lock);
5063446Smrj 
5075084Sjohnlev #if !defined(__xpv)
5085045Sbholler 	if (cp->cpu_m.mcpu_mwait != NULL)
5095045Sbholler 		cpuid_mwait_free(cp);
5105084Sjohnlev #endif
5113446Smrj 	kmem_free(cp, sizeof (*cp));
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate /*
5150Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
5160Sstevel@tonic-gate  *
5170Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
5180Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
5190Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
5200Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
5210Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
5220Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
5230Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
5240Sstevel@tonic-gate  * system.
5250Sstevel@tonic-gate  *
526938Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
527938Sesaxe  * mp_startup() for all slave CPUs. Slaves process workaround_errata prior
528938Sesaxe  * to acknowledging their readiness to the master, so this routine will
529938Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
530938Sesaxe  * global data safe.
531938Sesaxe  *
532359Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
533359Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
5340Sstevel@tonic-gate  */
5350Sstevel@tonic-gate 
5363446Smrj #if defined(OPTERON_ERRATUM_88)
5373446Smrj int opteron_erratum_88;		/* if non-zero -> at least one cpu has it */
5383446Smrj #endif
5393446Smrj 
5400Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
5410Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
5420Sstevel@tonic-gate #endif
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
5450Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
5460Sstevel@tonic-gate #endif
5470Sstevel@tonic-gate 
5483446Smrj #if defined(OPTERON_ERRATUM_95)
5493446Smrj int opteron_erratum_95;		/* if non-zero -> at least one cpu has it */
5503446Smrj #endif
5513446Smrj 
5520Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
5530Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
5540Sstevel@tonic-gate #endif
5550Sstevel@tonic-gate 
5563446Smrj #if defined(OPTERON_ERRATUM_108)
5573446Smrj int opteron_erratum_108;	/* if non-zero -> at least one cpu has it */
5583446Smrj #endif
5593446Smrj 
5600Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
5610Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
5620Sstevel@tonic-gate #endif
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
5650Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
5660Sstevel@tonic-gate #endif
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
5690Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
5700Sstevel@tonic-gate #endif
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
5730Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
5740Sstevel@tonic-gate #endif
5750Sstevel@tonic-gate 
576359Skucharsk #if defined(OPTERON_ERRATUM_131)
577359Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
578359Skucharsk #endif
5790Sstevel@tonic-gate 
580938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
581938Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
582938Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
583938Sesaxe #endif
584938Sesaxe 
5851582Skchow #if defined(OPTERON_WORKAROUND_6323525)
5861582Skchow int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
5871582Skchow #endif
5881582Skchow 
5896691Skchow #if defined(OPTERON_ERRATUM_298)
5906691Skchow int opteron_erratum_298;
5916691Skchow #endif
5926691Skchow 
5933446Smrj static void
5943446Smrj workaround_warning(cpu_t *cp, uint_t erratum)
5953446Smrj {
5963446Smrj 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
5973446Smrj 	    cp->cpu_id, erratum);
5983446Smrj }
5993446Smrj 
6003446Smrj static void
6013446Smrj workaround_applied(uint_t erratum)
6023446Smrj {
6033446Smrj 	if (erratum > 1000000)
6043446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
6053446Smrj 		    erratum);
6063446Smrj 	else
6073446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
6083446Smrj 		    erratum);
6093446Smrj }
6103446Smrj 
6113446Smrj static void
6123446Smrj msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
6133446Smrj {
6143446Smrj 	cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
6153446Smrj 	    cp->cpu_id, rw, msr, error);
6163446Smrj }
6170Sstevel@tonic-gate 
6185893Sesaxe /*
6195893Sesaxe  * Determine the number of nodes in an Opteron / Greyhound family system.
6205893Sesaxe  */
6215893Sesaxe static uint_t
6225893Sesaxe opteron_get_nnodes(void)
6235893Sesaxe {
6245893Sesaxe 	static uint_t nnodes = 0;
6255893Sesaxe 
6265893Sesaxe #ifdef	DEBUG
6275893Sesaxe 	uint_t family;
6285893Sesaxe 
6295893Sesaxe 	family = cpuid_getfamily(CPU);
6305893Sesaxe 	ASSERT(family == 0xf || family == 0x10);
6315893Sesaxe #endif	/* DEBUG */
6325893Sesaxe 
6335893Sesaxe 	if (nnodes == 0) {
6345893Sesaxe 		/*
6355893Sesaxe 		 * Obtain the number of nodes in the system from
6365893Sesaxe 		 * bits [6:4] of the Node ID register on node 0.
6375893Sesaxe 		 *
6385893Sesaxe 		 * The actual node count is NodeID[6:4] + 1
6395893Sesaxe 		 *
6405893Sesaxe 		 * The Node ID register is accessed via function 0,
6415893Sesaxe 		 * offset 0x60. Node 0 is device 24.
6425893Sesaxe 		 */
6435893Sesaxe 		nnodes = ((pci_getl_func(0, 24, 0, 0x60) & 0x70) >> 4) + 1;
6445893Sesaxe 	}
6455893Sesaxe 	return (nnodes);
6465893Sesaxe }
6475893Sesaxe 
6485084Sjohnlev #if defined(__xpv)
6495084Sjohnlev 
6505084Sjohnlev /*
6515084Sjohnlev  * On dom0, we can determine the number of physical cpus on the machine.
6525084Sjohnlev  * This number is important when figuring out what workarounds are
6535084Sjohnlev  * appropriate, so compute it now.
6545084Sjohnlev  */
6556670Stariq uint_t
6565084Sjohnlev xen_get_nphyscpus(void)
6575084Sjohnlev {
6585084Sjohnlev 	static uint_t nphyscpus = 0;
6595084Sjohnlev 
6605084Sjohnlev 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
6615084Sjohnlev 
6625084Sjohnlev 	if (nphyscpus == 0) {
6635084Sjohnlev 		xen_sysctl_t op;
6645084Sjohnlev 		xen_sysctl_physinfo_t *pi = &op.u.physinfo;
6655084Sjohnlev 
6665084Sjohnlev 		op.cmd = XEN_SYSCTL_physinfo;
6675084Sjohnlev 		op.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
6685084Sjohnlev 		if (HYPERVISOR_sysctl(&op) == 0)
6695084Sjohnlev 			nphyscpus = pi->threads_per_core *
6705084Sjohnlev 			    pi->cores_per_socket * pi->sockets_per_node *
6715084Sjohnlev 			    pi->nr_nodes;
6725084Sjohnlev 	}
6735084Sjohnlev 	return (nphyscpus);
6745084Sjohnlev }
6755084Sjohnlev #endif
6765084Sjohnlev 
6770Sstevel@tonic-gate uint_t
6786691Skchow do_erratum_298(struct cpu *cpu)
6796691Skchow {
6806691Skchow 	static int	osvwrc = -3;
6816691Skchow 	extern int	osvw_opteron_erratum(cpu_t *, uint_t);
6826691Skchow 
6836691Skchow 	/*
6846691Skchow 	 * L2 Eviction May Occur During Processor Operation To Set
6856691Skchow 	 * Accessed or Dirty Bit.
6866691Skchow 	 */
6876691Skchow 	if (osvwrc == -3) {
6886691Skchow 		osvwrc = osvw_opteron_erratum(cpu, 298);
6896691Skchow 	} else {
6906691Skchow 		/* osvw return codes should be consistent for all cpus */
6916691Skchow 		ASSERT(osvwrc == osvw_opteron_erratum(cpu, 298));
6926691Skchow 	}
6936691Skchow 
6946691Skchow 	switch (osvwrc) {
6956691Skchow 	case 0:		/* erratum is not present: do nothing */
6966691Skchow 		break;
6976691Skchow 	case 1:		/* erratum is present: BIOS workaround applied */
6986691Skchow 		/*
6996691Skchow 		 * check if workaround is actually in place and issue warning
7006691Skchow 		 * if not.
7016691Skchow 		 */
7026691Skchow 		if (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
7036691Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0)) {
7046691Skchow #if defined(OPTERON_ERRATUM_298)
7056691Skchow 			opteron_erratum_298++;
7066691Skchow #else
7076691Skchow 			workaround_warning(cpu, 298);
7086691Skchow 			return (1);
7096691Skchow #endif
7106691Skchow 		}
7116691Skchow 		break;
7126691Skchow 	case -1:	/* cannot determine via osvw: check cpuid */
7136691Skchow 		if ((cpuid_opteron_erratum(cpu, 298) > 0) &&
7146691Skchow 		    (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
7156691Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0))) {
7166691Skchow #if defined(OPTERON_ERRATUM_298)
7176691Skchow 			opteron_erratum_298++;
7186691Skchow #else
7196691Skchow 			workaround_warning(cpu, 298);
7206691Skchow 			return (1);
7216691Skchow #endif
7226691Skchow 		}
7236691Skchow 		break;
7246691Skchow 	}
7256691Skchow 	return (0);
7266691Skchow }
7276691Skchow 
7286691Skchow uint_t
7290Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	uint_t missing = 0;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	ASSERT(cpu == CPU);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	/*LINTED*/
7360Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
7370Sstevel@tonic-gate 		/*
7380Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
7390Sstevel@tonic-gate 		 */
7400Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
7410Sstevel@tonic-gate 		/*
7420Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
7430Sstevel@tonic-gate 		 */
7443446Smrj 		opteron_erratum_88++;
7450Sstevel@tonic-gate #else
7463446Smrj 		workaround_warning(cpu, 88);
7470Sstevel@tonic-gate 		missing++;
7480Sstevel@tonic-gate #endif
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
7540Sstevel@tonic-gate 		 */
7550Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
7560Sstevel@tonic-gate 		/*
7570Sstevel@tonic-gate 		 * fix is in trap.c
7580Sstevel@tonic-gate 		 */
7590Sstevel@tonic-gate 		opteron_erratum_91++;
7600Sstevel@tonic-gate #else
7613446Smrj 		workaround_warning(cpu, 91);
7620Sstevel@tonic-gate 		missing++;
7630Sstevel@tonic-gate #endif
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
7670Sstevel@tonic-gate 		/*
7680Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
7690Sstevel@tonic-gate 		 */
7700Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
7710Sstevel@tonic-gate 		/*
7720Sstevel@tonic-gate 		 * fix is in trap.c
7730Sstevel@tonic-gate 		 */
7740Sstevel@tonic-gate 		opteron_erratum_93++;
7750Sstevel@tonic-gate #else
7763446Smrj 		workaround_warning(cpu, 93);
7770Sstevel@tonic-gate 		missing++;
7780Sstevel@tonic-gate #endif
7790Sstevel@tonic-gate 	}
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	/*LINTED*/
7820Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
7830Sstevel@tonic-gate 		/*
7840Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
7850Sstevel@tonic-gate 		 */
7860Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
7870Sstevel@tonic-gate #if defined(_LP64)
7880Sstevel@tonic-gate 		/*
7890Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
7900Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
7910Sstevel@tonic-gate 		 * range mod 4G.
7920Sstevel@tonic-gate 		 */
7930Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
7940Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 		/*LINTED*/
7970Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
7983446Smrj 		opteron_erratum_95++;
7990Sstevel@tonic-gate #endif	/* _LP64 */
8000Sstevel@tonic-gate #else
8013446Smrj 		workaround_warning(cpu, 95);
8020Sstevel@tonic-gate 		missing++;
8033446Smrj #endif
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
8070Sstevel@tonic-gate 		/*
8080Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
8090Sstevel@tonic-gate 		 */
8100Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
8110Sstevel@tonic-gate 		/*
8120Sstevel@tonic-gate 		 * fix is in trap.c
8130Sstevel@tonic-gate 		 */
8140Sstevel@tonic-gate 		opteron_erratum_100++;
8150Sstevel@tonic-gate #else
8163446Smrj 		workaround_warning(cpu, 100);
8170Sstevel@tonic-gate 		missing++;
8180Sstevel@tonic-gate #endif
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	/*LINTED*/
8220Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
8230Sstevel@tonic-gate 		/*
8240Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
8250Sstevel@tonic-gate 		 * Some Processors
8260Sstevel@tonic-gate 		 */
8270Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
8280Sstevel@tonic-gate 		/*
8290Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
8300Sstevel@tonic-gate 		 * those processors)
8310Sstevel@tonic-gate 		 */
8320Sstevel@tonic-gate #else
8333446Smrj 		workaround_warning(cpu, 108);
8340Sstevel@tonic-gate 		missing++;
8350Sstevel@tonic-gate #endif
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	/*LINTED*/
8393446Smrj 	if (cpuid_opteron_erratum(cpu, 109) > 0) do {
8400Sstevel@tonic-gate 		/*
8410Sstevel@tonic-gate 		 * Certain Reverse REP MOVS May Produce Unpredictable Behaviour
8420Sstevel@tonic-gate 		 */
8430Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
8443446Smrj 		/*
8453446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
8463446Smrj 		 */
8473446Smrj 		uint64_t value;
8483446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
8493446Smrj 		int err;
8500Sstevel@tonic-gate 
8513446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
8523446Smrj 			msr_warning(cpu, "rd", msr, err);
8533446Smrj 			workaround_warning(cpu, 109);
8543446Smrj 			missing++;
8553446Smrj 		}
8563446Smrj 		if (value == 0)
8570Sstevel@tonic-gate 			opteron_erratum_109++;
8580Sstevel@tonic-gate #else
8593446Smrj 		workaround_warning(cpu, 109);
8600Sstevel@tonic-gate 		missing++;
8610Sstevel@tonic-gate #endif
8623446Smrj 	/*CONSTANTCONDITION*/
8633446Smrj 	} while (0);
8643446Smrj 
8650Sstevel@tonic-gate 	/*LINTED*/
8660Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
8670Sstevel@tonic-gate 		/*
8680Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
8690Sstevel@tonic-gate 		 * Processor Hang
8700Sstevel@tonic-gate 		 */
8710Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
8723446Smrj #if defined(_LP64)
8730Sstevel@tonic-gate 		/*
8740Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
8750Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
8760Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
8770Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
8780Sstevel@tonic-gate 		 */
8793446Smrj 		if (opteron_erratum_121)
8803446Smrj 			opteron_erratum_121++;
8813446Smrj 		else {
8823446Smrj 			if (hole_start) {
8833446Smrj 				hole_start -= PAGESIZE;
8843446Smrj 			} else {
8853446Smrj 				/*
8863446Smrj 				 * hole_start not yet initialized by
8873446Smrj 				 * mmu_init. Initialize hole_start
8883446Smrj 				 * with value to be subtracted.
8893446Smrj 				 */
8903446Smrj 				hole_start = PAGESIZE;
8910Sstevel@tonic-gate 			}
8923446Smrj 			opteron_erratum_121++;
8930Sstevel@tonic-gate 		}
8943446Smrj #endif	/* _LP64 */
8950Sstevel@tonic-gate #else
8963446Smrj 		workaround_warning(cpu, 121);
8970Sstevel@tonic-gate 		missing++;
8980Sstevel@tonic-gate #endif
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	/*LINTED*/
9023446Smrj 	if (cpuid_opteron_erratum(cpu, 122) > 0) do {
9030Sstevel@tonic-gate 		/*
9043446Smrj 		 * TLB Flush Filter May Cause Coherency Problem in
9050Sstevel@tonic-gate 		 * Multiprocessor Systems
9060Sstevel@tonic-gate 		 */
9070Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
9083446Smrj 		uint64_t value;
9093446Smrj 		const uint_t msr = MSR_AMD_HWCR;
9103446Smrj 		int error;
9113446Smrj 
9120Sstevel@tonic-gate 		/*
9130Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
9140Sstevel@tonic-gate 		 * or multi-processor).
9150Sstevel@tonic-gate 		 */
9165084Sjohnlev #if defined(__xpv)
9175084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
9185084Sjohnlev 			break;
9195084Sjohnlev 		if (!opteron_erratum_122 && xen_get_nphyscpus() == 1)
9205084Sjohnlev 			break;
9215084Sjohnlev #else
9225893Sesaxe 		if (!opteron_erratum_122 && opteron_get_nnodes() == 1 &&
9233446Smrj 		    cpuid_get_ncpu_per_chip(cpu) == 1)
9243446Smrj 			break;
9255084Sjohnlev #endif
9263446Smrj 		/* disable TLB Flush Filter */
9273446Smrj 
9283446Smrj 		if ((error = checked_rdmsr(msr, &value)) != 0) {
9293446Smrj 			msr_warning(cpu, "rd", msr, error);
9303446Smrj 			workaround_warning(cpu, 122);
9313446Smrj 			missing++;
9323446Smrj 		} else {
9333446Smrj 			value |= (uint64_t)AMD_HWCR_FFDIS;
9343446Smrj 			if ((error = checked_wrmsr(msr, value)) != 0) {
9353446Smrj 				msr_warning(cpu, "wr", msr, error);
9363446Smrj 				workaround_warning(cpu, 122);
9373446Smrj 				missing++;
9383446Smrj 			}
9390Sstevel@tonic-gate 		}
9403446Smrj 		opteron_erratum_122++;
9410Sstevel@tonic-gate #else
9423446Smrj 		workaround_warning(cpu, 122);
9430Sstevel@tonic-gate 		missing++;
9440Sstevel@tonic-gate #endif
9453446Smrj 	/*CONSTANTCONDITION*/
9463446Smrj 	} while (0);
947302Skchow 
9480Sstevel@tonic-gate 	/*LINTED*/
9493446Smrj 	if (cpuid_opteron_erratum(cpu, 123) > 0) do {
9500Sstevel@tonic-gate 		/*
9510Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
9520Sstevel@tonic-gate 		 * Dual Core Processors
9530Sstevel@tonic-gate 		 */
9543446Smrj #if defined(OPTERON_ERRATUM_123)
9553446Smrj 		uint64_t value;
9563446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
9573446Smrj 		int err;
9583446Smrj 
9590Sstevel@tonic-gate 		/*
9600Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
9610Sstevel@tonic-gate 		 */
9623446Smrj 		if (cpuid_get_ncpu_per_chip(cpu) < 2)
9633446Smrj 			break;
9645084Sjohnlev #if defined(__xpv)
9655084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
9665084Sjohnlev 			break;
9675084Sjohnlev #endif
9683446Smrj 		/*
9693446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
9703446Smrj 		 */
9713446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
9723446Smrj 			msr_warning(cpu, "rd", msr, err);
9733446Smrj 			workaround_warning(cpu, 123);
9743446Smrj 			missing++;
9750Sstevel@tonic-gate 		}
9763446Smrj 		if (value == 0)
9773446Smrj 			opteron_erratum_123++;
9783446Smrj #else
9793446Smrj 		workaround_warning(cpu, 123);
9803446Smrj 		missing++;
981359Skucharsk 
9823446Smrj #endif
9833446Smrj 	/*CONSTANTCONDITION*/
9843446Smrj 	} while (0);
9853446Smrj 
986359Skucharsk 	/*LINTED*/
9873446Smrj 	if (cpuid_opteron_erratum(cpu, 131) > 0) do {
988359Skucharsk 		/*
989359Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
990359Skucharsk 		 * Waiting for a Probe Response
991359Skucharsk 		 */
9923446Smrj #if defined(OPTERON_ERRATUM_131)
9933446Smrj 		uint64_t nbcfg;
9943446Smrj 		const uint_t msr = MSR_AMD_NB_CFG;
9953446Smrj 		const uint64_t wabits =
9963446Smrj 		    AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
9973446Smrj 		int error;
9983446Smrj 
999359Skucharsk 		/*
1000359Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
1001359Skucharsk 		 */
10023446Smrj 		if (opteron_erratum_131)
10033446Smrj 			break;
10045084Sjohnlev #if defined(__xpv)
10055084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
10065084Sjohnlev 			break;
10075084Sjohnlev 		if (xen_get_nphyscpus() < 4)
10085084Sjohnlev 			break;
10095084Sjohnlev #else
10105893Sesaxe 		if (opteron_get_nnodes() * cpuid_get_ncpu_per_chip(cpu) < 4)
10113446Smrj 			break;
10125084Sjohnlev #endif
10133446Smrj 		/*
10143446Smrj 		 * Print a warning if neither of the workarounds for
10153446Smrj 		 * erratum 131 is present.
10163446Smrj 		 */
10173446Smrj 		if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
10183446Smrj 			msr_warning(cpu, "rd", msr, error);
10193446Smrj 			workaround_warning(cpu, 131);
10203446Smrj 			missing++;
10213446Smrj 		} else if ((nbcfg & wabits) == 0) {
10223446Smrj 			opteron_erratum_131++;
10233446Smrj 		} else {
10243446Smrj 			/* cannot have both workarounds set */
10253446Smrj 			ASSERT((nbcfg & wabits) != wabits);
1026359Skucharsk 		}
10273446Smrj #else
10283446Smrj 		workaround_warning(cpu, 131);
10293446Smrj 		missing++;
1030359Skucharsk #endif
10313446Smrj 	/*CONSTANTCONDITION*/
10323446Smrj 	} while (0);
1033938Sesaxe 
1034938Sesaxe 	/*
10353446Smrj 	 * This isn't really an erratum, but for convenience the
1036938Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
1037938Sesaxe 	 */
1038938Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
10393446Smrj #if defined(OPTERON_WORKAROUND_6336786)
1040938Sesaxe 		/*
1041938Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
1042938Sesaxe 		 * K8 platforms to guard against TSC drift.
1043938Sesaxe 		 */
1044938Sesaxe 		if (opteron_workaround_6336786) {
1045938Sesaxe 			opteron_workaround_6336786++;
10465084Sjohnlev #if defined(__xpv)
10475084Sjohnlev 		} else if ((DOMAIN_IS_INITDOMAIN(xen_info) &&
10485084Sjohnlev 		    xen_get_nphyscpus() > 1) ||
10495084Sjohnlev 		    opteron_workaround_6336786_UP) {
10505084Sjohnlev 			/*
10515893Sesaxe 			 * XXPV	Hmm.  We can't walk the Northbridges on
10525084Sjohnlev 			 *	the hypervisor; so just complain and drive
10535084Sjohnlev 			 *	on.  This probably needs to be fixed in
10545084Sjohnlev 			 *	the hypervisor itself.
10555084Sjohnlev 			 */
10565084Sjohnlev 			opteron_workaround_6336786++;
10575084Sjohnlev 			workaround_warning(cpu, 6336786);
10585084Sjohnlev #else	/* __xpv */
10595893Sesaxe 		} else if ((opteron_get_nnodes() *
10605894Sesaxe 		    cpuid_get_ncpu_per_chip(cpu) > 1) ||
1061938Sesaxe 		    opteron_workaround_6336786_UP) {
10625893Sesaxe 
10635893Sesaxe 			uint_t	node, nnodes;
10643446Smrj 			uint8_t data;
10653446Smrj 
10665893Sesaxe 			nnodes = opteron_get_nnodes();
10675893Sesaxe 			for (node = 0; node < nnodes; node++) {
1068938Sesaxe 				/*
1069938Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
1070938Sesaxe 				 * Northbridge device is the node id + 24.
1071938Sesaxe 				 */
1072938Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
1073938Sesaxe 				data &= 0xFC;
1074938Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
1075938Sesaxe 			}
1076938Sesaxe 			opteron_workaround_6336786++;
10775084Sjohnlev #endif	/* __xpv */
1078938Sesaxe 		}
10793446Smrj #else
10803446Smrj 		workaround_warning(cpu, 6336786);
10813446Smrj 		missing++;
1082938Sesaxe #endif
10833446Smrj 	}
10841582Skchow 
10851582Skchow 	/*LINTED*/
10861582Skchow 	/*
10871582Skchow 	 * Mutex primitives don't work as expected.
10881582Skchow 	 */
10891582Skchow 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
10903446Smrj #if defined(OPTERON_WORKAROUND_6323525)
10911582Skchow 		/*
10923446Smrj 		 * This problem only occurs with 2 or more cores. If bit in
10936691Skchow 		 * MSR_AMD_BU_CFG set, then not applicable. The workaround
10941582Skchow 		 * is to patch the semaphone routines with the lfence
10951582Skchow 		 * instruction to provide necessary load memory barrier with
10961582Skchow 		 * possible subsequent read-modify-write ops.
10971582Skchow 		 *
10981582Skchow 		 * It is too early in boot to call the patch routine so
10991582Skchow 		 * set erratum variable to be done in startup_end().
11001582Skchow 		 */
11011582Skchow 		if (opteron_workaround_6323525) {
11021582Skchow 			opteron_workaround_6323525++;
11035084Sjohnlev #if defined(__xpv)
11045084Sjohnlev 		} else if (x86_feature & X86_SSE2) {
11055084Sjohnlev 			if (DOMAIN_IS_INITDOMAIN(xen_info)) {
11065084Sjohnlev 				/*
11075084Sjohnlev 				 * XXPV	Use dom0_msr here when extended
11085084Sjohnlev 				 *	operations are supported?
11095084Sjohnlev 				 */
11105084Sjohnlev 				if (xen_get_nphyscpus() > 1)
11115084Sjohnlev 					opteron_workaround_6323525++;
11125084Sjohnlev 			} else {
11135084Sjohnlev 				/*
11145084Sjohnlev 				 * We have no way to tell how many physical
11155084Sjohnlev 				 * cpus there are, or even if this processor
11165084Sjohnlev 				 * has the problem, so enable the workaround
11175084Sjohnlev 				 * unconditionally (at some performance cost).
11185084Sjohnlev 				 */
11195084Sjohnlev 				opteron_workaround_6323525++;
11205084Sjohnlev 			}
11215084Sjohnlev #else	/* __xpv */
11225893Sesaxe 		} else if ((x86_feature & X86_SSE2) && ((opteron_get_nnodes() *
11233446Smrj 		    cpuid_get_ncpu_per_chip(cpu)) > 1)) {
11246691Skchow 			if ((xrdmsr(MSR_AMD_BU_CFG) & 0x02) == 0)
11251582Skchow 				opteron_workaround_6323525++;
11265084Sjohnlev #endif	/* __xpv */
11271582Skchow 		}
11283446Smrj #else
11293446Smrj 		workaround_warning(cpu, 6323525);
11303446Smrj 		missing++;
11313446Smrj #endif
11321582Skchow 	}
11333446Smrj 
11346691Skchow 	missing += do_erratum_298(cpu);
11356691Skchow 
11365084Sjohnlev #ifdef __xpv
11375084Sjohnlev 	return (0);
11385084Sjohnlev #else
11390Sstevel@tonic-gate 	return (missing);
11405084Sjohnlev #endif
11410Sstevel@tonic-gate }
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate void
11440Sstevel@tonic-gate workaround_errata_end()
11450Sstevel@tonic-gate {
11463446Smrj #if defined(OPTERON_ERRATUM_88)
11473446Smrj 	if (opteron_erratum_88)
11483446Smrj 		workaround_applied(88);
11493446Smrj #endif
11503446Smrj #if defined(OPTERON_ERRATUM_91)
11513446Smrj 	if (opteron_erratum_91)
11523446Smrj 		workaround_applied(91);
11533446Smrj #endif
11543446Smrj #if defined(OPTERON_ERRATUM_93)
11553446Smrj 	if (opteron_erratum_93)
11563446Smrj 		workaround_applied(93);
11573446Smrj #endif
11583446Smrj #if defined(OPTERON_ERRATUM_95)
11593446Smrj 	if (opteron_erratum_95)
11603446Smrj 		workaround_applied(95);
11613446Smrj #endif
11623446Smrj #if defined(OPTERON_ERRATUM_100)
11633446Smrj 	if (opteron_erratum_100)
11643446Smrj 		workaround_applied(100);
11653446Smrj #endif
11663446Smrj #if defined(OPTERON_ERRATUM_108)
11673446Smrj 	if (opteron_erratum_108)
11683446Smrj 		workaround_applied(108);
11693446Smrj #endif
11700Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
11710Sstevel@tonic-gate 	if (opteron_erratum_109) {
1172359Skucharsk 		cmn_err(CE_WARN,
1173359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1174359Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
1175359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1176359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1177359Skucharsk 		    " system\noperation may occur.\n");
11780Sstevel@tonic-gate 	}
11793446Smrj #endif
11803446Smrj #if defined(OPTERON_ERRATUM_121)
11813446Smrj 	if (opteron_erratum_121)
11823446Smrj 		workaround_applied(121);
11833446Smrj #endif
11843446Smrj #if defined(OPTERON_ERRATUM_122)
11853446Smrj 	if (opteron_erratum_122)
11863446Smrj 		workaround_applied(122);
11873446Smrj #endif
11880Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
11890Sstevel@tonic-gate 	if (opteron_erratum_123) {
1190359Skucharsk 		cmn_err(CE_WARN,
1191359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1192359Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
1193359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1194359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1195359Skucharsk 		    " system\noperation may occur.\n");
11960Sstevel@tonic-gate 	}
11973446Smrj #endif
1198359Skucharsk #if defined(OPTERON_ERRATUM_131)
1199359Skucharsk 	if (opteron_erratum_131) {
1200359Skucharsk 		cmn_err(CE_WARN,
1201359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1202359Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
1203359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1204359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1205359Skucharsk 		    " system\noperation may occur.\n");
1206359Skucharsk 	}
12073446Smrj #endif
12083446Smrj #if defined(OPTERON_WORKAROUND_6336786)
12093446Smrj 	if (opteron_workaround_6336786)
12103446Smrj 		workaround_applied(6336786);
12113446Smrj #endif
12123446Smrj #if defined(OPTERON_WORKAROUND_6323525)
12133446Smrj 	if (opteron_workaround_6323525)
12143446Smrj 		workaround_applied(6323525);
12153446Smrj #endif
12166691Skchow #if defined(OPTERON_ERRATUM_298)
12176691Skchow 	if (opteron_erratum_298) {
12186691Skchow 		cmn_err(CE_WARN,
12196691Skchow 		    "BIOS microcode patch for AMD 64/Opteron(tm)"
12206691Skchow 		    " processor\nerratum 298 was not detected; updating your"
12216691Skchow 		    " system's BIOS to a version\ncontaining this"
12226691Skchow 		    " microcode patch is HIGHLY recommended or erroneous"
12236691Skchow 		    " system\noperation may occur.\n");
12246691Skchow 	}
12256691Skchow #endif
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate 
12283446Smrj static cpuset_t procset;
12293446Smrj 
12303446Smrj /*
12313446Smrj  * Start a single cpu, assuming that the kernel context is available
12323446Smrj  * to successfully start another cpu.
12333446Smrj  *
12343446Smrj  * (For example, real mode code is mapped into the right place
12353446Smrj  * in memory and is ready to be run.)
12363446Smrj  */
12373446Smrj int
12383446Smrj start_cpu(processorid_t who)
12393446Smrj {
12403446Smrj 	void *ctx;
12413446Smrj 	cpu_t *cp;
12423446Smrj 	int delays;
12433446Smrj 	int error = 0;
12443446Smrj 
12453446Smrj 	ASSERT(who != 0);
12463446Smrj 
12473446Smrj 	/*
12483446Smrj 	 * Check if there's at least a Mbyte of kmem available
12493446Smrj 	 * before attempting to start the cpu.
12503446Smrj 	 */
12513446Smrj 	if (kmem_avail() < 1024 * 1024) {
12523446Smrj 		/*
12533446Smrj 		 * Kick off a reap in case that helps us with
12543446Smrj 		 * later attempts ..
12553446Smrj 		 */
12563446Smrj 		kmem_reap();
12573446Smrj 		return (ENOMEM);
12583446Smrj 	}
12593446Smrj 
12603446Smrj 	cp = mp_startup_init(who);
12613446Smrj 	if ((ctx = mach_cpucontext_alloc(cp)) == NULL ||
12623446Smrj 	    (error = mach_cpu_start(cp, ctx)) != 0) {
12633446Smrj 
12643446Smrj 		/*
12653446Smrj 		 * Something went wrong before we even started it
12663446Smrj 		 */
12673446Smrj 		if (ctx)
12683446Smrj 			cmn_err(CE_WARN,
12693446Smrj 			    "cpu%d: failed to start error %d",
12703446Smrj 			    cp->cpu_id, error);
12713446Smrj 		else
12723446Smrj 			cmn_err(CE_WARN,
12733446Smrj 			    "cpu%d: failed to allocate context", cp->cpu_id);
12740Sstevel@tonic-gate 
12753446Smrj 		if (ctx)
12763446Smrj 			mach_cpucontext_free(cp, ctx, error);
12773446Smrj 		else
12783446Smrj 			error = EAGAIN;		/* hmm. */
12793446Smrj 		mp_startup_fini(cp, error);
12803446Smrj 		return (error);
12813446Smrj 	}
12823446Smrj 
12833446Smrj 	for (delays = 0; !CPU_IN_SET(procset, who); delays++) {
12843446Smrj 		if (delays == 500) {
12853446Smrj 			/*
12863446Smrj 			 * After five seconds, things are probably looking
12873446Smrj 			 * a bit bleak - explain the hang.
12883446Smrj 			 */
12893446Smrj 			cmn_err(CE_NOTE, "cpu%d: started, "
12903446Smrj 			    "but not running in the kernel yet", who);
12913446Smrj 		} else if (delays > 2000) {
12923446Smrj 			/*
12933446Smrj 			 * We waited at least 20 seconds, bail ..
12943446Smrj 			 */
12953446Smrj 			error = ETIMEDOUT;
12963446Smrj 			cmn_err(CE_WARN, "cpu%d: timed out", who);
12973446Smrj 			mach_cpucontext_free(cp, ctx, error);
12983446Smrj 			mp_startup_fini(cp, error);
12993446Smrj 			return (error);
13003446Smrj 		}
13013446Smrj 
13023446Smrj 		/*
13033446Smrj 		 * wait at least 10ms, then check again..
13043446Smrj 		 */
13053446Smrj 		delay(USEC_TO_TICK_ROUNDUP(10000));
13063446Smrj 	}
13073446Smrj 
13083446Smrj 	mach_cpucontext_free(cp, ctx, 0);
13093446Smrj 
13105084Sjohnlev #ifndef __xpv
13113446Smrj 	if (tsc_gethrtime_enable)
13123446Smrj 		tsc_sync_master(who);
13135084Sjohnlev #endif
13143446Smrj 
13153446Smrj 	if (dtrace_cpu_init != NULL) {
13163446Smrj 		/*
13173446Smrj 		 * DTrace CPU initialization expects cpu_lock to be held.
13183446Smrj 		 */
13193446Smrj 		mutex_enter(&cpu_lock);
13203446Smrj 		(*dtrace_cpu_init)(who);
13213446Smrj 		mutex_exit(&cpu_lock);
13223446Smrj 	}
13233446Smrj 
13243446Smrj 	while (!CPU_IN_SET(cpu_ready_set, who))
13253446Smrj 		delay(1);
13263446Smrj 
13273446Smrj 	return (0);
13283446Smrj }
13293446Smrj 
13302006Sandrei 
13310Sstevel@tonic-gate /*ARGSUSED*/
13320Sstevel@tonic-gate void
13330Sstevel@tonic-gate start_other_cpus(int cprboot)
13340Sstevel@tonic-gate {
13353446Smrj 	uint_t who;
13363446Smrj 	uint_t skipped = 0;
13373446Smrj 	uint_t bootcpuid = 0;
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	/*
13400Sstevel@tonic-gate 	 * Initialize our own cpu_info.
13410Sstevel@tonic-gate 	 */
13420Sstevel@tonic-gate 	init_cpu_info(CPU);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	/*
13450Sstevel@tonic-gate 	 * Initialize our syscall handlers
13460Sstevel@tonic-gate 	 */
13470Sstevel@tonic-gate 	init_cpu_syscall(CPU);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	/*
13503446Smrj 	 * Take the boot cpu out of the mp_cpus set because we know
13513446Smrj 	 * it's already running.  Add it to the cpu_ready_set for
13523446Smrj 	 * precisely the same reason.
13533446Smrj 	 */
13543446Smrj 	CPUSET_DEL(mp_cpus, bootcpuid);
13553446Smrj 	CPUSET_ADD(cpu_ready_set, bootcpuid);
13563446Smrj 
13573446Smrj 	/*
13580Sstevel@tonic-gate 	 * if only 1 cpu or not using MP, skip the rest of this
13590Sstevel@tonic-gate 	 */
13603446Smrj 	if (CPUSET_ISNULL(mp_cpus) || use_mp == 0) {
13610Sstevel@tonic-gate 		if (use_mp == 0)
13620Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
13630Sstevel@tonic-gate 		goto done;
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	/*
13670Sstevel@tonic-gate 	 * perform such initialization as is needed
13680Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
13690Sstevel@tonic-gate 	 */
13700Sstevel@tonic-gate 	cpu_pause_init();
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	xc_init();		/* initialize processor crosscalls */
13730Sstevel@tonic-gate 
13743446Smrj 	if (mach_cpucontext_init() != 0)
13750Sstevel@tonic-gate 		goto done;
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	flushes_require_xcalls = 1;
13780Sstevel@tonic-gate 
13792575Snf202958 	/*
13802575Snf202958 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
13812575Snf202958 	 * do their TSC syncs with the same CPU.
13822575Snf202958 	 */
13830Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
13862575Snf202958 
13872006Sandrei 		if (!CPU_IN_SET(mp_cpus, who))
13882006Sandrei 			continue;
13893446Smrj 		ASSERT(who != bootcpuid);
13902006Sandrei 		if (ncpus >= max_ncpus) {
13912006Sandrei 			skipped = who;
13920Sstevel@tonic-gate 			continue;
13932006Sandrei 		}
13943446Smrj 		if (start_cpu(who) != 0)
13953446Smrj 			CPUSET_DEL(mp_cpus, who);
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 
13984581Ssherrym 	/* Free the space allocated to hold the microcode file */
1399*7605SMark.Johnson@Sun.COM 	ucode_cleanup();
14004581Ssherrym 
14010Sstevel@tonic-gate 	affinity_clear();
14020Sstevel@tonic-gate 
14032006Sandrei 	if (skipped) {
14042006Sandrei 		cmn_err(CE_NOTE,
14053446Smrj 		    "System detected %d cpus, but "
14063446Smrj 		    "only %d cpu(s) were enabled during boot.",
14072006Sandrei 		    skipped + 1, ncpus);
14082006Sandrei 		cmn_err(CE_NOTE,
14092006Sandrei 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
14102006Sandrei 		    "See eeprom(1M).");
14112006Sandrei 	}
14122006Sandrei 
14130Sstevel@tonic-gate done:
14140Sstevel@tonic-gate 	workaround_errata_end();
14153446Smrj 	mach_cpucontext_fini();
14161642Sgavinm 
14171642Sgavinm 	cmi_post_mpstartup();
14180Sstevel@tonic-gate }
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate /*
14210Sstevel@tonic-gate  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
14220Sstevel@tonic-gate  */
14230Sstevel@tonic-gate /*ARGSUSED*/
14240Sstevel@tonic-gate int
14250Sstevel@tonic-gate mp_cpu_configure(int cpuid)
14260Sstevel@tonic-gate {
14270Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
14280Sstevel@tonic-gate }
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate /*ARGSUSED*/
14310Sstevel@tonic-gate int
14320Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
14330Sstevel@tonic-gate {
14340Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
14350Sstevel@tonic-gate }
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate /*
14380Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
14392985Sdmick  * Called from real_mode_start.
14401251Skchow  *
14411251Skchow  * WARNING: until CPU_READY is set, mp_startup and routines called by
14421251Skchow  * mp_startup should not call routines (e.g. kmem_free) that could call
14431251Skchow  * hat_unload which requires CPU_READY to be set.
14440Sstevel@tonic-gate  */
14450Sstevel@tonic-gate void
14460Sstevel@tonic-gate mp_startup(void)
14470Sstevel@tonic-gate {
14480Sstevel@tonic-gate 	struct cpu *cp = CPU;
14490Sstevel@tonic-gate 	uint_t new_x86_feature;
14500Sstevel@tonic-gate 
14512985Sdmick 	/*
14523021Sdmick 	 * We need to get TSC on this proc synced (i.e., any delta
14533021Sdmick 	 * from cpu0 accounted for) as soon as we can, because many
14543021Sdmick 	 * many things use gethrtime/pc_gethrestime, including
14553021Sdmick 	 * interrupts, cmn_err, etc.
14563021Sdmick 	 */
14573021Sdmick 
14583021Sdmick 	/* Let cpu0 continue into tsc_sync_master() */
14593021Sdmick 	CPUSET_ATOMIC_ADD(procset, cp->cpu_id);
14603021Sdmick 
14615084Sjohnlev #ifndef __xpv
14623021Sdmick 	if (tsc_gethrtime_enable)
14633021Sdmick 		tsc_sync_slave();
14645084Sjohnlev #endif
14653021Sdmick 
14663021Sdmick 	/*
14672985Sdmick 	 * Once this was done from assembly, but it's safer here; if
14682985Sdmick 	 * it blocks, we need to be able to swtch() to and from, and
14692985Sdmick 	 * since we get here by calling t_pc, we need to do that call
14702985Sdmick 	 * before swtch() overwrites it.
14712985Sdmick 	 */
14722985Sdmick 	(void) (*ap_mlsetup)();
14732985Sdmick 
14740Sstevel@tonic-gate 	new_x86_feature = cpuid_pass1(cp);
14750Sstevel@tonic-gate 
14765084Sjohnlev #ifndef __xpv
14770Sstevel@tonic-gate 	/*
14785159Sjohnlev 	 * Program this cpu's PAT
14790Sstevel@tonic-gate 	 */
14805159Sjohnlev 	if (x86_feature & X86_PAT)
14815159Sjohnlev 		pat_sync();
14825084Sjohnlev #endif
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	/*
14853446Smrj 	 * Set up TSC_AUX to contain the cpuid for this processor
14863446Smrj 	 * for the rdtscp instruction.
14873446Smrj 	 */
14883446Smrj 	if (x86_feature & X86_TSCP)
14893446Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
14903446Smrj 
14913446Smrj 	/*
14920Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
14930Sstevel@tonic-gate 	 */
14940Sstevel@tonic-gate 	init_cpu_syscall(cp);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	/*
14970Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
14980Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
14990Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
15000Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
15010Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
15020Sstevel@tonic-gate 	 * calls before CPU_READY is set.
15030Sstevel@tonic-gate 	 */
15043446Smrj 	splx(ipltospl(LOCK_LEVEL));
15053446Smrj 	sti();
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	/*
15080Sstevel@tonic-gate 	 * Do a sanity check to make sure this new CPU is a sane thing
15090Sstevel@tonic-gate 	 * to add to the collection of processors running this system.
15100Sstevel@tonic-gate 	 *
15110Sstevel@tonic-gate 	 * XXX	Clearly this needs to get more sophisticated, if x86
15120Sstevel@tonic-gate 	 * systems start to get built out of heterogenous CPUs; as is
15130Sstevel@tonic-gate 	 * likely to happen once the number of processors in a configuration
15140Sstevel@tonic-gate 	 * gets large enough.
15150Sstevel@tonic-gate 	 */
15160Sstevel@tonic-gate 	if ((x86_feature & new_x86_feature) != x86_feature) {
15170Sstevel@tonic-gate 		cmn_err(CE_CONT, "?cpu%d: %b\n",
15180Sstevel@tonic-gate 		    cp->cpu_id, new_x86_feature, FMT_X86_FEATURE);
15190Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
15200Sstevel@tonic-gate 	}
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	/*
15234481Sbholler 	 * We do not support cpus with mixed monitor/mwait support if the
15244481Sbholler 	 * boot cpu supports monitor/mwait.
15254481Sbholler 	 */
15264481Sbholler 	if ((x86_feature & ~new_x86_feature) & X86_MWAIT)
15274481Sbholler 		panic("unsupported mixed cpu monitor/mwait support detected");
15284481Sbholler 
15294481Sbholler 	/*
15300Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
15310Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
15320Sstevel@tonic-gate 	 * answer of dieing horribly.  Provided the boot cpu is ok,
15330Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
15340Sstevel@tonic-gate 	 */
15350Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
15360Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	cpuid_pass2(cp);
15390Sstevel@tonic-gate 	cpuid_pass3(cp);
15400Sstevel@tonic-gate 	(void) cpuid_pass4(cp);
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	init_cpu_info(cp);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
15450Sstevel@tonic-gate 	/*
15463434Sesaxe 	 * Processor group initialization for this CPU is dependent on the
15473434Sesaxe 	 * cpuid probing, which must be done in the context of the current
15483434Sesaxe 	 * CPU.
15490Sstevel@tonic-gate 	 */
15503434Sesaxe 	pghw_physid_create(cp);
15513434Sesaxe 	pg_cpu_init(cp);
15523434Sesaxe 	pg_cmt_cpu_startup(cp);
15530Sstevel@tonic-gate 
15546749Ssherrym 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_EXISTS;
15552575Snf202958 
15562575Snf202958 	if (dtrace_cpu_init != NULL) {
15572575Snf202958 		(*dtrace_cpu_init)(cp->cpu_id);
15582575Snf202958 	}
15592575Snf202958 
15604581Ssherrym 	/*
15614581Ssherrym 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
15624581Ssherrym 	 */
15634581Ssherrym 	ucode_check(cp);
15644581Ssherrym 
15650Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
15660Sstevel@tonic-gate 
15673029Ssethg 	/*
15683029Ssethg 	 * Enable preemption here so that contention for any locks acquired
15693029Ssethg 	 * later in mp_startup may be preempted if the thread owning those
15703029Ssethg 	 * locks is continously executing on other CPUs (for example, this
15713029Ssethg 	 * CPU must be preemptible to allow other CPUs to pause it during their
15723029Ssethg 	 * startup phases).  It's safe to enable preemption here because the
15733029Ssethg 	 * CPU state is pretty-much fully constructed.
15743029Ssethg 	 */
15753029Ssethg 	curthread->t_preempt = 0;
15763029Ssethg 
15771482Ssethg 	/* The base spl should still be at LOCK LEVEL here */
15781482Ssethg 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
15791482Ssethg 	set_base_spl();		/* Restore the spl to its proper value */
15801482Ssethg 
15816749Ssherrym 	/* Enable interrupts */
15826749Ssherrym 	(void) spl0();
15836749Ssherrym 	mutex_enter(&cpu_lock);
15846749Ssherrym 	cpu_enable_intr(cp);
15856749Ssherrym 	cpu_add_active(cp);
15866749Ssherrym 	mutex_exit(&cpu_lock);
15876749Ssherrym 
15886749Ssherrym 	add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi);
15890Sstevel@tonic-gate 
15905254Sgavinm #ifndef __xpv
15915254Sgavinm 	{
15925254Sgavinm 		/*
15935254Sgavinm 		 * Set up the CPU module for this CPU.  This can't be done
15945254Sgavinm 		 * before this CPU is made CPU_READY, because we may (in
15955254Sgavinm 		 * heterogeneous systems) need to go load another CPU module.
15965254Sgavinm 		 * The act of attempting to load a module may trigger a
15975254Sgavinm 		 * cross-call, which will ASSERT unless this cpu is CPU_READY.
15985254Sgavinm 		 */
15995254Sgavinm 		cmi_hdl_t hdl;
16001414Scindi 
16015254Sgavinm 		if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
16027532SSean.Ye@Sun.COM 		    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
16035254Sgavinm 			if (x86_feature & X86_MCA)
16045254Sgavinm 				cmi_mca_init(hdl);
16055254Sgavinm 		}
16065254Sgavinm 	}
16075254Sgavinm #endif /* __xpv */
16081414Scindi 
16090Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
16103446Smrj 		kdi_cpu_init();
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	/*
16130Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
16140Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
16150Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
16160Sstevel@tonic-gate 	 */
16170Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	/*
16200Sstevel@tonic-gate 	 * Because mp_startup() gets fired off after init() starts, we
16210Sstevel@tonic-gate 	 * can't use the '?' trick to do 'boot -v' printing - so we
16220Sstevel@tonic-gate 	 * always direct the 'cpu .. online' messages to the log.
16230Sstevel@tonic-gate 	 */
16240Sstevel@tonic-gate 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
16250Sstevel@tonic-gate 	    cp->cpu_id);
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	/*
16280Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
16290Sstevel@tonic-gate 	 */
16300Sstevel@tonic-gate 	thread_exit();
16310Sstevel@tonic-gate 	panic("mp_startup: cannot return");
16320Sstevel@tonic-gate 	/*NOTREACHED*/
16330Sstevel@tonic-gate }
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate /*
16370Sstevel@tonic-gate  * Start CPU on user request.
16380Sstevel@tonic-gate  */
16390Sstevel@tonic-gate /* ARGSUSED */
16400Sstevel@tonic-gate int
16410Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
16420Sstevel@tonic-gate {
16430Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16440Sstevel@tonic-gate 	return (0);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate /*
16480Sstevel@tonic-gate  * Stop CPU on user request.
16490Sstevel@tonic-gate  */
16500Sstevel@tonic-gate /* ARGSUSED */
16510Sstevel@tonic-gate int
16520Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
16530Sstevel@tonic-gate {
16541389Sdmick 	extern int cbe_psm_timer_mode;
16550Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16561389Sdmick 
16575084Sjohnlev #ifdef __xpv
16585084Sjohnlev 	/*
16595084Sjohnlev 	 * We can't offline vcpu0.
16605084Sjohnlev 	 */
16615084Sjohnlev 	if (cp->cpu_id == 0)
16625084Sjohnlev 		return (EBUSY);
16635084Sjohnlev #endif
16645084Sjohnlev 
16651389Sdmick 	/*
16661389Sdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
16671389Sdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
16681389Sdmick 	 */
16691389Sdmick 
16701389Sdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
16715084Sjohnlev 		return (EBUSY);
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	return (0);
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate /*
16770Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
16780Sstevel@tonic-gate  */
16790Sstevel@tonic-gate int
16800Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
16810Sstevel@tonic-gate {
16820Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
16830Sstevel@tonic-gate 		return (EBUSY);
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
16860Sstevel@tonic-gate 	return (0);
16870Sstevel@tonic-gate }
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate /*
16900Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
16910Sstevel@tonic-gate  */
16920Sstevel@tonic-gate void
16930Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
16940Sstevel@tonic-gate {
16950Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16960Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
16970Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
16980Sstevel@tonic-gate }
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 
17015254Sgavinm /*ARGSUSED*/
17020Sstevel@tonic-gate void
17030Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
17041414Scindi {
17055254Sgavinm #ifndef __xpv
17065254Sgavinm 	cmi_hdl_t hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
17075254Sgavinm 	    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
17085254Sgavinm 
17095254Sgavinm 	if (hdl != NULL) {
17105254Sgavinm 		cmi_faulted_enter(hdl);
17115254Sgavinm 		cmi_hdl_rele(hdl);
17125254Sgavinm 	}
17135254Sgavinm #endif
17141414Scindi }
17150Sstevel@tonic-gate 
17165254Sgavinm /*ARGSUSED*/
17170Sstevel@tonic-gate void
17180Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
17191414Scindi {
17205254Sgavinm #ifndef __xpv
17215254Sgavinm 	cmi_hdl_t hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
17225254Sgavinm 	    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
17235254Sgavinm 
17245254Sgavinm 	if (hdl != NULL) {
17255254Sgavinm 		cmi_faulted_exit(hdl);
17265254Sgavinm 		cmi_hdl_rele(hdl);
17275254Sgavinm 	}
17285254Sgavinm #endif
17291414Scindi }
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate /*
17320Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
17330Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
17340Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
17350Sstevel@tonic-gate  * organization rather than speed.  We could avoid checking x86_feature at every
17360Sstevel@tonic-gate  * context switch by installing different context ops, depending on the
17370Sstevel@tonic-gate  * x86_feature flags, at LDT creation time -- one for each combination of fast
17380Sstevel@tonic-gate  * syscall feature flags.
17390Sstevel@tonic-gate  */
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate /*ARGSUSED*/
17420Sstevel@tonic-gate void
17430Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg)
17440Sstevel@tonic-gate {
17453446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
17460Sstevel@tonic-gate 		cpu_sep_disable();
17473446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
17480Sstevel@tonic-gate 		cpu_asysc_disable();
17490Sstevel@tonic-gate }
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate /*ARGSUSED*/
17520Sstevel@tonic-gate void
17530Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg)
17540Sstevel@tonic-gate {
17553446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
17560Sstevel@tonic-gate 		cpu_sep_enable();
17573446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
17580Sstevel@tonic-gate 		cpu_asysc_enable();
17590Sstevel@tonic-gate }
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate static void
17620Sstevel@tonic-gate cpu_sep_enable(void)
17630Sstevel@tonic-gate {
17640Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
17650Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
17660Sstevel@tonic-gate 
1767770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate static void
17710Sstevel@tonic-gate cpu_sep_disable(void)
17720Sstevel@tonic-gate {
17730Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
17740Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	/*
17770Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
17780Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
17790Sstevel@tonic-gate 	 */
17803446Smrj 	wrmsr(MSR_INTC_SEP_CS, 0);
17810Sstevel@tonic-gate }
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate static void
17840Sstevel@tonic-gate cpu_asysc_enable(void)
17850Sstevel@tonic-gate {
17860Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
17870Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
17880Sstevel@tonic-gate 
1789770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1790770Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
17910Sstevel@tonic-gate }
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate static void
17940Sstevel@tonic-gate cpu_asysc_disable(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 
17990Sstevel@tonic-gate 	/*
18000Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
18010Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
18020Sstevel@tonic-gate 	 */
1803770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1804770Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
18050Sstevel@tonic-gate }
1806