xref: /illumos-gate/usr/src/uts/i86pc/os/mp_startup.c (revision df5a0c57c8c46976f94e3d1ffa07b420f2b24501)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5100b72f4Sandrei  * Common Development and Distribution License (the "License").
6100b72f4Sandrei  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ae115bc7Smrj 
227c478bd9Sstevel@tonic-gate /*
237417cfdeSKuriakose Kuruvilla  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
25a3114836SGerry Liu /*
26a3114836SGerry Liu  * Copyright (c) 2010, Intel Corporation.
27a3114836SGerry Liu  * All rights reserved.
28a3114836SGerry Liu  */
29ebb8ac07SRobert Mustacchi /*
30e25cb0e7SJohn Levon  * Copyright 2020 Joyent, Inc.
31850ad55aSHans Rosenfeld  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
3239df76fbSAndy Fiddaman  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
33*df5a0c57SLuqman Aden  * Copyright 2024 Oxide Computer Company
34ebb8ac07SRobert Mustacchi  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/thread.h>
387c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
39a3114836SGerry Liu #include <sys/cpu.h>
407c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/proc.h>
437c478bd9Sstevel@tonic-gate #include <sys/disp.h>
447c478bd9Sstevel@tonic-gate #include <sys/class.h>
457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
467c478bd9Sstevel@tonic-gate #include <sys/debug.h>
47a3114836SGerry Liu #include <sys/note.h>
487c478bd9Sstevel@tonic-gate #include <sys/asm_linkage.h>
497c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
507c478bd9Sstevel@tonic-gate #include <sys/systm.h>
517c478bd9Sstevel@tonic-gate #include <sys/var.h>
527c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
537c478bd9Sstevel@tonic-gate #include <vm/hat.h>
547c478bd9Sstevel@tonic-gate #include <vm/as.h>
557c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
56ae115bc7Smrj #include <vm/seg_kp.h>
577c478bd9Sstevel@tonic-gate #include <sys/segments.h>
587c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
597c478bd9Sstevel@tonic-gate #include <sys/stack.h>
607c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
617c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
627c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
637c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
647c478bd9Sstevel@tonic-gate #include <sys/clock.h>
657c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
66fb2f18f8Sesaxe #include <sys/pg.h>
67fb2f18f8Sesaxe #include <sys/cmt.h>
687c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
697c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
707c478bd9Sstevel@tonic-gate #include <sys/fp.h>
717c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
72ae115bc7Smrj #include <sys/kdi_machimpl.h>
737c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
74a3114836SGerry Liu #include <vm/vm_dep.h>
757c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
76ef50d8c0Sesaxe #include <sys/pci_cfgspace.h>
77ae115bc7Smrj #include <sys/mach_mmu.h>
78ae115bc7Smrj #include <sys/sysmacros.h>
79843e1988Sjohnlev #if defined(__xpv)
80843e1988Sjohnlev #include <sys/hypervisor.h>
81761dea5eSPatrick Mooney #else
82761dea5eSPatrick Mooney #include <sys/hma.h>
83843e1988Sjohnlev #endif
847aec1d6eScindi #include <sys/cpu_module.h>
85850ad55aSHans Rosenfeld #include <sys/ontrap.h>
867c478bd9Sstevel@tonic-gate 
8774ecdb51SJohn Levon struct cpu	cpus[1] __aligned(MMU_PAGESIZE);
8874ecdb51SJohn Levon struct cpu	*cpu[NCPU] = {&cpus[0]};
8974ecdb51SJohn Levon struct cpu	*cpu_free_list;
9074ecdb51SJohn Levon cpu_core_t	cpu_core[NCPU];
917c478bd9Sstevel@tonic-gate 
92a3114836SGerry Liu #define	cpu_next_free	cpu_prev
93a3114836SGerry Liu 
947c478bd9Sstevel@tonic-gate /*
95ae115bc7Smrj  * Useful for disabling MP bring-up on a MP capable system.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate int use_mp = 1;
987c478bd9Sstevel@tonic-gate 
9941791439Sandrei /*
100ae115bc7Smrj  * to be set by a PSM to indicate what cpus
101ae115bc7Smrj  * are sitting around on the system.
10241791439Sandrei  */
103ae115bc7Smrj cpuset_t mp_cpus;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * This variable is used by the hat layer to decide whether or not
1077c478bd9Sstevel@tonic-gate  * critical sections are needed to prevent race conditions.  For sun4m,
1087c478bd9Sstevel@tonic-gate  * this variable is set once enough MP initialization has been done in
1097c478bd9Sstevel@tonic-gate  * order to allow cross calls.
1107c478bd9Sstevel@tonic-gate  */
111ae115bc7Smrj int flushes_require_xcalls;
112a563a037Sbholler 
113a563a037Sbholler cpuset_t cpu_ready_set;		/* initialized in startup() */
1147c478bd9Sstevel@tonic-gate 
115a3114836SGerry Liu static void mp_startup_boot(void);
116a3114836SGerry Liu static void mp_startup_hotplug(void);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static void cpu_sep_enable(void);
1197c478bd9Sstevel@tonic-gate static void cpu_sep_disable(void);
1207c478bd9Sstevel@tonic-gate static void cpu_asysc_enable(void);
1217c478bd9Sstevel@tonic-gate static void cpu_asysc_disable(void);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Init CPU info - get CPU type info for processor_info system call.
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate void
init_cpu_info(struct cpu * cp)1277c478bd9Sstevel@tonic-gate init_cpu_info(struct cpu *cp)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	processor_info_t *pi = &cp->cpu_type_info;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * Get clock-frequency property for the CPU.
1337c478bd9Sstevel@tonic-gate 	 */
1347c478bd9Sstevel@tonic-gate 	pi->pi_clock = cpu_freq;
1357c478bd9Sstevel@tonic-gate 
1365cff7825Smh27603 	/*
1375cff7825Smh27603 	 * Current frequency in Hz.
1385cff7825Smh27603 	 */
139cf74e62bSmh27603 	cp->cpu_curr_clock = cpu_freq_hz;
1405cff7825Smh27603 
14137d22dc0SAnup Pemmaiah 	/*
14237d22dc0SAnup Pemmaiah 	 * Supported frequencies.
14337d22dc0SAnup Pemmaiah 	 */
14437d22dc0SAnup Pemmaiah 	if (cp->cpu_supp_freqs == NULL) {
14537d22dc0SAnup Pemmaiah 		cpu_set_supp_freqs(cp, NULL);
14637d22dc0SAnup Pemmaiah 	}
14737d22dc0SAnup Pemmaiah 
1487c478bd9Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1497c478bd9Sstevel@tonic-gate 	if (fpu_exists)
1507c478bd9Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1517c478bd9Sstevel@tonic-gate 
152a3114836SGerry Liu 	cp->cpu_idstr = kmem_zalloc(CPU_IDSTRLEN, KM_SLEEP);
153a3114836SGerry Liu 	cp->cpu_brandstr = kmem_zalloc(CPU_IDSTRLEN, KM_SLEEP);
1547c478bd9Sstevel@tonic-gate 
155a3114836SGerry Liu 	/*
156a3114836SGerry Liu 	 * If called for the BSP, cp is equal to current CPU.
157a3114836SGerry Liu 	 * For non-BSPs, cpuid info of cp is not ready yet, so use cpuid info
158a3114836SGerry Liu 	 * of current CPU as default values for cpu_idstr and cpu_brandstr.
159ab5bb018SKeith M Wesolowski 	 * They will be corrected in mp_startup_common() after
160ab5bb018SKeith M Wesolowski 	 * CPUID_PASS_DYNAMIC has been invoked on target CPU.
161a3114836SGerry Liu 	 */
162a3114836SGerry Liu 	(void) cpuid_getidstr(CPU, cp->cpu_idstr, CPU_IDSTRLEN);
163a3114836SGerry Liu 	(void) cpuid_getbrandstr(CPU, cp->cpu_brandstr, CPU_IDSTRLEN);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * Configure syscall support on this CPU.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1702df1fe9cSrandyf void
init_cpu_syscall(struct cpu * cp)1717c478bd9Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	kpreempt_disable();
1747c478bd9Sstevel@tonic-gate 
1757417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
1767417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_ASYSC)) {
1773ce2fcdcSRobert Mustacchi 		uint64_t flags;
1787c478bd9Sstevel@tonic-gate 
17974ecdb51SJohn Levon #if !defined(__xpv)
1807c478bd9Sstevel@tonic-gate 		/*
1817c478bd9Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1827c478bd9Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1837c478bd9Sstevel@tonic-gate 		 * here.
1847c478bd9Sstevel@tonic-gate 		 */
18574ecdb51SJohn Levon 		CTASSERT(KDS_SEL == KCS_SEL + 8);
18674ecdb51SJohn Levon 		CTASSERT(UDS_SEL == U32CS_SEL + 8);
18774ecdb51SJohn Levon 		CTASSERT(UCS_SEL == U32CS_SEL + 16);
1887c478bd9Sstevel@tonic-gate #endif
18974ecdb51SJohn Levon 
1907c478bd9Sstevel@tonic-gate 		/*
1917c478bd9Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1927c478bd9Sstevel@tonic-gate 		 */
1937c478bd9Sstevel@tonic-gate 		cpu_asysc_enable();
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		/*
1967c478bd9Sstevel@tonic-gate 		 * Program the magic registers ..
1977c478bd9Sstevel@tonic-gate 		 */
198ae115bc7Smrj 		wrmsr(MSR_AMD_STAR,
199ae115bc7Smrj 		    ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
20074ecdb51SJohn Levon 		if (kpti_enable == 1) {
20174ecdb51SJohn Levon 			wrmsr(MSR_AMD_LSTAR,
20274ecdb51SJohn Levon 			    (uint64_t)(uintptr_t)tr_sys_syscall);
20374ecdb51SJohn Levon 			wrmsr(MSR_AMD_CSTAR,
20474ecdb51SJohn Levon 			    (uint64_t)(uintptr_t)tr_sys_syscall32);
20574ecdb51SJohn Levon 		} else {
20674ecdb51SJohn Levon 			wrmsr(MSR_AMD_LSTAR,
20774ecdb51SJohn Levon 			    (uint64_t)(uintptr_t)sys_syscall);
20874ecdb51SJohn Levon 			wrmsr(MSR_AMD_CSTAR,
20974ecdb51SJohn Levon 			    (uint64_t)(uintptr_t)sys_syscall32);
21074ecdb51SJohn Levon 		}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		/*
2137c478bd9Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
2147c478bd9Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
2157c478bd9Sstevel@tonic-gate 		 */
2163ce2fcdcSRobert Mustacchi 		flags = PS_IE | PS_T;
2173ce2fcdcSRobert Mustacchi 		if (is_x86_feature(x86_featureset, X86FSET_SMAP) == B_TRUE)
2183ce2fcdcSRobert Mustacchi 			flags |= PS_ACHK;
2193ce2fcdcSRobert Mustacchi 		wrmsr(MSR_AMD_SFMASK, flags);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
2247c478bd9Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
2257c478bd9Sstevel@tonic-gate 	 */
2267417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
2277417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_SEP)) {
2287c478bd9Sstevel@tonic-gate 
22974ecdb51SJohn Levon #if !defined(__xpv)
2307c478bd9Sstevel@tonic-gate 		/*
2317c478bd9Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
2327c478bd9Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
2337c478bd9Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
2347c478bd9Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
2357c478bd9Sstevel@tonic-gate 		 * Instruction Set Reference"
2367c478bd9Sstevel@tonic-gate 		 */
23774ecdb51SJohn Levon 		CTASSERT(KDS_SEL == KCS_SEL + 8);
2387c478bd9Sstevel@tonic-gate 
23974ecdb51SJohn Levon 		CTASSERT(U32CS_SEL == ((KCS_SEL + 16) | 3));
24074ecdb51SJohn Levon 		CTASSERT(UDS_SEL == U32CS_SEL + 8);
2417c478bd9Sstevel@tonic-gate #endif
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		cpu_sep_enable();
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		/*
2467c478bd9Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2477c478bd9Sstevel@tonic-gate 		 * via a context handler.
2487c478bd9Sstevel@tonic-gate 		 */
249ae115bc7Smrj 		wrmsr(MSR_INTC_SEP_ESP, 0);
25074ecdb51SJohn Levon 
25174ecdb51SJohn Levon 		if (kpti_enable == 1) {
25274ecdb51SJohn Levon 			wrmsr(MSR_INTC_SEP_EIP,
25374ecdb51SJohn Levon 			    (uint64_t)(uintptr_t)tr_sys_sysenter);
25474ecdb51SJohn Levon 		} else {
25574ecdb51SJohn Levon 			wrmsr(MSR_INTC_SEP_EIP,
25674ecdb51SJohn Levon 			    (uint64_t)(uintptr_t)sys_sysenter);
25774ecdb51SJohn Levon 		}
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	kpreempt_enable();
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2632428aad8SPatrick Mooney #if !defined(__xpv)
2642428aad8SPatrick Mooney /*
2652428aad8SPatrick Mooney  * Configure per-cpu ID GDT
2662428aad8SPatrick Mooney  */
2672428aad8SPatrick Mooney static void
init_cpu_id_gdt(struct cpu * cp)2682428aad8SPatrick Mooney init_cpu_id_gdt(struct cpu *cp)
2692428aad8SPatrick Mooney {
2702428aad8SPatrick Mooney 	/* Write cpu_id into limit field of GDT for usermode retrieval */
2712428aad8SPatrick Mooney 	set_usegd(&cp->cpu_gdt[GDT_CPUID], SDP_SHORT, NULL, cp->cpu_id,
2722428aad8SPatrick Mooney 	    SDT_MEMRODA, SEL_UPL, SDP_BYTES, SDP_OP32);
2732428aad8SPatrick Mooney }
2742428aad8SPatrick Mooney #endif /* !defined(__xpv) */
2752428aad8SPatrick Mooney 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * Multiprocessor initialization.
2787c478bd9Sstevel@tonic-gate  *
2797c478bd9Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2807c478bd9Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
281a3114836SGerry Liu  * Parameter boot is true for boot time operations and is false for CPU
282a3114836SGerry Liu  * DR operations.
2837c478bd9Sstevel@tonic-gate  */
284a3114836SGerry Liu static struct cpu *
mp_cpu_configure_common(int cpun,boolean_t boot)285a3114836SGerry Liu mp_cpu_configure_common(int cpun, boolean_t boot)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	struct cpu *cp;
2887c478bd9Sstevel@tonic-gate 	kthread_id_t tp;
2897c478bd9Sstevel@tonic-gate 	caddr_t	sp;
2907c478bd9Sstevel@tonic-gate 	proc_t *procp;
291843e1988Sjohnlev #if !defined(__xpv)
2925b8a6efeSbholler 	extern int idle_cpu_prefer_mwait;
2930e751525SEric Saxe 	extern void cpu_idle_mwait();
294843e1988Sjohnlev #endif
2957c478bd9Sstevel@tonic-gate 	extern void idle();
2960e751525SEric Saxe 	extern void cpu_idle();
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
2997c478bd9Sstevel@tonic-gate 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
3007c478bd9Sstevel@tonic-gate #endif
3017c478bd9Sstevel@tonic-gate 
302a3114836SGerry Liu 	ASSERT(MUTEX_HELD(&cpu_lock));
3037c478bd9Sstevel@tonic-gate 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
3047c478bd9Sstevel@tonic-gate 
305a3114836SGerry Liu 	if (cpu_free_list == NULL) {
306ae115bc7Smrj 		cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
307a3114836SGerry Liu 	} else {
308a3114836SGerry Liu 		cp = cpu_free_list;
309a3114836SGerry Liu 		cpu_free_list = cp->cpu_next_free;
310a3114836SGerry Liu 	}
311f98fbcecSbholler 
3123006ae82SFrank Van Der Linden 	cp->cpu_m.mcpu_istamp = cpun << 16;
3133006ae82SFrank Van Der Linden 
314a3114836SGerry Liu 	/* Create per CPU specific threads in the process p0. */
315a3114836SGerry Liu 	procp = &p0;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/*
3187c478bd9Sstevel@tonic-gate 	 * Initialize the dispatcher first.
3197c478bd9Sstevel@tonic-gate 	 */
3207c478bd9Sstevel@tonic-gate 	disp_cpu_init(cp);
3217c478bd9Sstevel@tonic-gate 
322affbd3ccSkchow 	cpu_vm_data_init(cp);
323affbd3ccSkchow 
3247c478bd9Sstevel@tonic-gate 	/*
3257c478bd9Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
3267c478bd9Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
3277c478bd9Sstevel@tonic-gate 	 * when the CPU starts running.
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
3307c478bd9Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
3347c478bd9Sstevel@tonic-gate 	 * as soon as the CPU comes online.
3357c478bd9Sstevel@tonic-gate 	 *
3367c478bd9Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
3377c478bd9Sstevel@tonic-gate 	 * thread_create().
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
3407c478bd9Sstevel@tonic-gate 	tp->t_preempt = 1;
3417c478bd9Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3427c478bd9Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3437c478bd9Sstevel@tonic-gate 	tp->t_cpu = cp;
3447c478bd9Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/*
347a3114836SGerry Liu 	 * Setup thread to start in mp_startup_common.
3487c478bd9Sstevel@tonic-gate 	 */
3497c478bd9Sstevel@tonic-gate 	sp = tp->t_stk;
3507c478bd9Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
351ae115bc7Smrj 	tp->t_sp -= STACK_ENTRY_ALIGN;		/* fake a call */
352a3114836SGerry Liu 	/*
353a3114836SGerry Liu 	 * Setup thread start entry point for boot or hotplug.
354a3114836SGerry Liu 	 */
355a3114836SGerry Liu 	if (boot) {
356a3114836SGerry Liu 		tp->t_pc = (uintptr_t)mp_startup_boot;
357a3114836SGerry Liu 	} else {
358a3114836SGerry Liu 		tp->t_pc = (uintptr_t)mp_startup_hotplug;
359a3114836SGerry Liu 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	cp->cpu_id = cpun;
3627c478bd9Sstevel@tonic-gate 	cp->cpu_self = cp;
3637c478bd9Sstevel@tonic-gate 	cp->cpu_thread = tp;
3647c478bd9Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
3657c478bd9Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
3667c478bd9Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/*
369da43ceabSsethg 	 * cpu_base_spl must be set explicitly here to prevent any blocking
370a3114836SGerry Liu 	 * operations in mp_startup_common from causing the spl of the cpu
371a3114836SGerry Liu 	 * to drop to 0 (allowing device interrupts before we're ready) in
372a3114836SGerry Liu 	 * resume().
373da43ceabSsethg 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
374da43ceabSsethg 	 * As an extra bit of security on DEBUG kernels, this is enforced with
375a3114836SGerry Liu 	 * an assertion in mp_startup_common() -- before cpu_base_spl is set
376a3114836SGerry Liu 	 * to its proper value.
377da43ceabSsethg 	 */
378da43ceabSsethg 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
379da43ceabSsethg 
380da43ceabSsethg 	/*
3817c478bd9Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
3827c478bd9Sstevel@tonic-gate 	 */
3837c478bd9Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	tp->t_preempt = 1;
3887c478bd9Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3897c478bd9Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3907c478bd9Sstevel@tonic-gate 	tp->t_cpu = cp;
3917c478bd9Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/*
394fb2f18f8Sesaxe 	 * Bootstrap the CPU's PG data
395394b433dSesaxe 	 */
396fb2f18f8Sesaxe 	pg_cpu_bootstrap(cp);
397394b433dSesaxe 
398394b433dSesaxe 	/*
399ae115bc7Smrj 	 * Perform CPC initialization on the new CPU.
4007c478bd9Sstevel@tonic-gate 	 */
4017c478bd9Sstevel@tonic-gate 	kcpc_hw_init(cp);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/*
4047c478bd9Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
4057c478bd9Sstevel@tonic-gate 	 * for each CPU.
4067c478bd9Sstevel@tonic-gate 	 */
4077c478bd9Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/*
410ae115bc7Smrj 	 * Allocate page for new GDT and initialize from current GDT.
4117c478bd9Sstevel@tonic-gate 	 */
412ae115bc7Smrj #if !defined(__lint)
413ae115bc7Smrj 	ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
414ae115bc7Smrj #endif
4150cfdb603Sjosephb 	cp->cpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
4160cfdb603Sjosephb 	bcopy(CPU->cpu_gdt, cp->cpu_gdt, (sizeof (*cp->cpu_gdt) * NGDT));
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/*
42074ecdb51SJohn Levon 	 * Allocate pages for the CPU LDT.
4217c478bd9Sstevel@tonic-gate 	 */
42274ecdb51SJohn Levon 	cp->cpu_m.mcpu_ldt = kmem_zalloc(LDT_CPU_SIZE, KM_SLEEP);
42374ecdb51SJohn Levon 	cp->cpu_m.mcpu_ldt_len = 0;
42474ecdb51SJohn Levon 
42574ecdb51SJohn Levon 	/*
42674ecdb51SJohn Levon 	 * Allocate a per-CPU IDT and initialize the new IDT to the currently
42774ecdb51SJohn Levon 	 * runing CPU.
42874ecdb51SJohn Levon 	 */
4290cfdb603Sjosephb #if !defined(__lint)
4300cfdb603Sjosephb 	ASSERT((sizeof (*CPU->cpu_idt) * NIDT) <= PAGESIZE);
4310cfdb603Sjosephb #endif
43274ecdb51SJohn Levon 	cp->cpu_idt = kmem_alloc(PAGESIZE, KM_SLEEP);
4330cfdb603Sjosephb 	bcopy(CPU->cpu_idt, cp->cpu_idt, PAGESIZE);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/*
436ae115bc7Smrj 	 * alloc space for cpuid info
437ae115bc7Smrj 	 */
438ae115bc7Smrj 	cpuid_alloc_space(cp);
439a3114836SGerry Liu #if !defined(__xpv)
4407417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MWAIT) &&
4417417cfdeSKuriakose Kuruvilla 	    idle_cpu_prefer_mwait) {
442a3114836SGerry Liu 		cp->cpu_m.mcpu_mwait = cpuid_mwait_alloc(cp);
443a3114836SGerry Liu 		cp->cpu_m.mcpu_idle_cpu = cpu_idle_mwait;
444a3114836SGerry Liu 	} else
445a3114836SGerry Liu #endif
446a3114836SGerry Liu 		cp->cpu_m.mcpu_idle_cpu = cpu_idle;
447a3114836SGerry Liu 
448a3114836SGerry Liu 	init_cpu_info(cp);
449ae115bc7Smrj 
4502428aad8SPatrick Mooney #if !defined(__xpv)
4512428aad8SPatrick Mooney 	init_cpu_id_gdt(cp);
4522428aad8SPatrick Mooney #endif
4532428aad8SPatrick Mooney 
4542449e17fSsherrym 	/*
4552449e17fSsherrym 	 * alloc space for ucode_info
4562449e17fSsherrym 	 */
4572449e17fSsherrym 	ucode_alloc_space(cp);
458f34a7178SJoe Bonasera 	xc_init_cpu(cp);
4597c478bd9Sstevel@tonic-gate 	hat_cpu_online(cp);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
4627c478bd9Sstevel@tonic-gate 	/*
463ae115bc7Smrj 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers
4647c478bd9Sstevel@tonic-gate 	 */
4657c478bd9Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
4667c478bd9Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
4677c478bd9Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
4687c478bd9Sstevel@tonic-gate #endif
469a3114836SGerry Liu 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Record that we have another CPU.
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
4757c478bd9Sstevel@tonic-gate 	 */
476100b72f4Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
477a3114836SGerry Liu 
478a3114836SGerry Liu 	cp->cpu_flags = CPU_OFFLINE | CPU_QUIESCED | CPU_POWEROFF;
479a3114836SGerry Liu 	cpu_set_state(cp);
480a3114836SGerry Liu 
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
483a3114836SGerry Liu 	 * after mp_startup_common().
4847c478bd9Sstevel@tonic-gate 	 */
4857c478bd9Sstevel@tonic-gate 	cpu_add_unit(cp);
486ae115bc7Smrj 
487ae115bc7Smrj 	return (cp);
488ae115bc7Smrj }
489ae115bc7Smrj 
490ae115bc7Smrj /*
491a3114836SGerry Liu  * Undo what was done in mp_cpu_configure_common
492ae115bc7Smrj  */
493ae115bc7Smrj static void
mp_cpu_unconfigure_common(struct cpu * cp,int error)494a3114836SGerry Liu mp_cpu_unconfigure_common(struct cpu *cp, int error)
495ae115bc7Smrj {
496a3114836SGerry Liu 	ASSERT(MUTEX_HELD(&cpu_lock));
497ae115bc7Smrj 
498ae115bc7Smrj 	/*
499ae115bc7Smrj 	 * Remove the CPU from the list of available CPUs.
500ae115bc7Smrj 	 */
501ae115bc7Smrj 	cpu_del_unit(cp->cpu_id);
502ae115bc7Smrj 
503ae115bc7Smrj 	if (error == ETIMEDOUT) {
504ae115bc7Smrj 		/*
505ae115bc7Smrj 		 * The cpu was started, but never *seemed* to run any
506ae115bc7Smrj 		 * code in the kernel; it's probably off spinning in its
507ae115bc7Smrj 		 * own private world, though with potential references to
508ae115bc7Smrj 		 * our kmem-allocated IDTs and GDTs (for example).
509ae115bc7Smrj 		 *
510ae115bc7Smrj 		 * Worse still, it may actually wake up some time later,
511ae115bc7Smrj 		 * so rather than guess what it might or might not do, we
512ae115bc7Smrj 		 * leave the fundamental data structures intact.
513ae115bc7Smrj 		 */
514ae115bc7Smrj 		cp->cpu_flags = 0;
515ae115bc7Smrj 		return;
516ae115bc7Smrj 	}
517ae115bc7Smrj 
518ae115bc7Smrj 	/*
519ae115bc7Smrj 	 * At this point, the only threads bound to this CPU should
520ae115bc7Smrj 	 * special per-cpu threads: it's idle thread, it's pause threads,
521ae115bc7Smrj 	 * and it's interrupt threads.  Clean these up.
522ae115bc7Smrj 	 */
523ae115bc7Smrj 	cpu_destroy_bound_threads(cp);
524ae115bc7Smrj 	cp->cpu_idle_thread = NULL;
525ae115bc7Smrj 
526ae115bc7Smrj 	/*
527ae115bc7Smrj 	 * Free the interrupt stack.
528ae115bc7Smrj 	 */
529ae115bc7Smrj 	segkp_release(segkp,
530ae115bc7Smrj 	    cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
531a3114836SGerry Liu 	cp->cpu_intr_stack = NULL;
532ae115bc7Smrj 
533ae115bc7Smrj #ifdef TRAPTRACE
534ae115bc7Smrj 	/*
535ae115bc7Smrj 	 * Discard the trap trace buffer
536ae115bc7Smrj 	 */
537ae115bc7Smrj 	{
538ae115bc7Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
539ae115bc7Smrj 
540ae115bc7Smrj 		kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
5418f22c1dfSToomas Soome 		ttc->ttc_first = (uintptr_t)NULL;
542ae115bc7Smrj 	}
543ae115bc7Smrj #endif
544ae115bc7Smrj 
545ae115bc7Smrj 	hat_cpu_offline(cp);
546ae115bc7Smrj 
5472449e17fSsherrym 	ucode_free_space(cp);
5482449e17fSsherrym 
549a3114836SGerry Liu 	/* Free CPU ID string and brand string. */
550a3114836SGerry Liu 	if (cp->cpu_idstr) {
551a3114836SGerry Liu 		kmem_free(cp->cpu_idstr, CPU_IDSTRLEN);
552a3114836SGerry Liu 		cp->cpu_idstr = NULL;
553a3114836SGerry Liu 	}
554a3114836SGerry Liu 	if (cp->cpu_brandstr) {
555a3114836SGerry Liu 		kmem_free(cp->cpu_brandstr, CPU_IDSTRLEN);
556a3114836SGerry Liu 		cp->cpu_brandstr = NULL;
557a3114836SGerry Liu 	}
558a3114836SGerry Liu 
559a3114836SGerry Liu #if !defined(__xpv)
560a3114836SGerry Liu 	if (cp->cpu_m.mcpu_mwait != NULL) {
561a3114836SGerry Liu 		cpuid_mwait_free(cp);
562a3114836SGerry Liu 		cp->cpu_m.mcpu_mwait = NULL;
563a3114836SGerry Liu 	}
564a3114836SGerry Liu #endif
565a3114836SGerry Liu 	cpuid_free_space(cp);
566a3114836SGerry Liu 
5670cfdb603Sjosephb 	if (cp->cpu_idt != CPU->cpu_idt)
5680cfdb603Sjosephb 		kmem_free(cp->cpu_idt, PAGESIZE);
5690cfdb603Sjosephb 	cp->cpu_idt = NULL;
570ae115bc7Smrj 
57174ecdb51SJohn Levon 	kmem_free(cp->cpu_m.mcpu_ldt, LDT_CPU_SIZE);
57274ecdb51SJohn Levon 	cp->cpu_m.mcpu_ldt = NULL;
57374ecdb51SJohn Levon 	cp->cpu_m.mcpu_ldt_len = 0;
57474ecdb51SJohn Levon 
5750cfdb603Sjosephb 	kmem_free(cp->cpu_gdt, PAGESIZE);
5760cfdb603Sjosephb 	cp->cpu_gdt = NULL;
577ae115bc7Smrj 
578a3114836SGerry Liu 	if (cp->cpu_supp_freqs != NULL) {
579a3114836SGerry Liu 		size_t len = strlen(cp->cpu_supp_freqs) + 1;
580a3114836SGerry Liu 		kmem_free(cp->cpu_supp_freqs, len);
581a3114836SGerry Liu 		cp->cpu_supp_freqs = NULL;
582a3114836SGerry Liu 	}
583a3114836SGerry Liu 
584ae115bc7Smrj 	teardown_vaddr_for_ppcopy(cp);
585ae115bc7Smrj 
586ae115bc7Smrj 	kcpc_hw_fini(cp);
587ae115bc7Smrj 
588ae115bc7Smrj 	cp->cpu_dispthread = NULL;
589ae115bc7Smrj 	cp->cpu_thread = NULL;	/* discarded by cpu_destroy_bound_threads() */
590ae115bc7Smrj 
591ae115bc7Smrj 	cpu_vm_data_destroy(cp);
592ae115bc7Smrj 
593a3114836SGerry Liu 	xc_fini_cpu(cp);
594ae115bc7Smrj 	disp_cpu_fini(cp);
595ae115bc7Smrj 
596a3114836SGerry Liu 	ASSERT(cp != CPU0);
597a3114836SGerry Liu 	bzero(cp, sizeof (*cp));
598a3114836SGerry Liu 	cp->cpu_next_free = cpu_free_list;
599a3114836SGerry Liu 	cpu_free_list = cp;
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
6047c478bd9Sstevel@tonic-gate  *
6057c478bd9Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
6067c478bd9Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
6077c478bd9Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
6087c478bd9Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
6097c478bd9Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
6107c478bd9Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
6117c478bd9Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
6127c478bd9Sstevel@tonic-gate  * system.
6137c478bd9Sstevel@tonic-gate  *
614ef50d8c0Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
615a3114836SGerry Liu  * mp_startup_common() for all slave CPUs. Slaves process workaround_errata
616a3114836SGerry Liu  * prior to acknowledging their readiness to the master, so this routine will
617ef50d8c0Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
618ef50d8c0Sesaxe  * global data safe.
619ef50d8c0Sesaxe  *
6202201b277Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
6212201b277Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate 
624ae115bc7Smrj #if defined(OPTERON_ERRATUM_88)
625ae115bc7Smrj int opteron_erratum_88;		/* if non-zero -> at least one cpu has it */
626ae115bc7Smrj #endif
627ae115bc7Smrj 
6287c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
6297c478bd9Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
6307c478bd9Sstevel@tonic-gate #endif
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
6337c478bd9Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
6347c478bd9Sstevel@tonic-gate #endif
6357c478bd9Sstevel@tonic-gate 
636ae115bc7Smrj #if defined(OPTERON_ERRATUM_95)
637ae115bc7Smrj int opteron_erratum_95;		/* if non-zero -> at least one cpu has it */
638ae115bc7Smrj #endif
639ae115bc7Smrj 
6407c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
6417c478bd9Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
6427c478bd9Sstevel@tonic-gate #endif
6437c478bd9Sstevel@tonic-gate 
644ae115bc7Smrj #if defined(OPTERON_ERRATUM_108)
645ae115bc7Smrj int opteron_erratum_108;	/* if non-zero -> at least one cpu has it */
646ae115bc7Smrj #endif
647ae115bc7Smrj 
6487c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
6497c478bd9Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
6507c478bd9Sstevel@tonic-gate #endif
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
6537c478bd9Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
6547c478bd9Sstevel@tonic-gate #endif
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
6577c478bd9Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
6587c478bd9Sstevel@tonic-gate #endif
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
6617c478bd9Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
6627c478bd9Sstevel@tonic-gate #endif
6637c478bd9Sstevel@tonic-gate 
6642201b277Skucharsk #if defined(OPTERON_ERRATUM_131)
6652201b277Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
6662201b277Skucharsk #endif
6677c478bd9Sstevel@tonic-gate 
668ef50d8c0Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
669ef50d8c0Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
670ef50d8c0Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
671ef50d8c0Sesaxe #endif
672ef50d8c0Sesaxe 
673ee6ee36aSPatrick Mooney #if defined(OPTERON_ERRATUM_147)
674ee6ee36aSPatrick Mooney int opteron_erratum_147;	/* if non-zero -> at least one cpu has it */
675ee88d2b9Skchow #endif
676ee88d2b9Skchow 
677512cf780Skchow #if defined(OPTERON_ERRATUM_298)
678512cf780Skchow int opteron_erratum_298;
679512cf780Skchow #endif
680512cf780Skchow 
6815e54b56dSHans Rosenfeld #if defined(OPTERON_ERRATUM_721)
6825e54b56dSHans Rosenfeld int opteron_erratum_721;
6835e54b56dSHans Rosenfeld #endif
6845e54b56dSHans Rosenfeld 
685ae115bc7Smrj static void
workaround_warning(cpu_t * cp,uint_t erratum)686ae115bc7Smrj workaround_warning(cpu_t *cp, uint_t erratum)
687ae115bc7Smrj {
688ae115bc7Smrj 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
689ae115bc7Smrj 	    cp->cpu_id, erratum);
690ae115bc7Smrj }
691ae115bc7Smrj 
692ae115bc7Smrj static void
workaround_applied(uint_t erratum)693ae115bc7Smrj workaround_applied(uint_t erratum)
694ae115bc7Smrj {
695ae115bc7Smrj 	if (erratum > 1000000)
696ae115bc7Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
697ae115bc7Smrj 		    erratum);
698ae115bc7Smrj 	else
699ae115bc7Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
700ae115bc7Smrj 		    erratum);
701ae115bc7Smrj }
702ae115bc7Smrj 
703ae115bc7Smrj static void
msr_warning(cpu_t * cp,const char * rw,uint_t msr,int error)704ae115bc7Smrj msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
705ae115bc7Smrj {
706ae115bc7Smrj 	cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
707ae115bc7Smrj 	    cp->cpu_id, rw, msr, error);
708ae115bc7Smrj }
7097c478bd9Sstevel@tonic-gate 
71092564cb1Sesaxe /*
711d2aeaf66SEric Saxe  * Determine the number of nodes in a Hammer / Greyhound / Griffin family
712d2aeaf66SEric Saxe  * system.
71392564cb1Sesaxe  */
71492564cb1Sesaxe static uint_t
opteron_get_nnodes(void)71592564cb1Sesaxe opteron_get_nnodes(void)
71692564cb1Sesaxe {
71792564cb1Sesaxe 	static uint_t nnodes = 0;
71892564cb1Sesaxe 
719d2aeaf66SEric Saxe 	if (nnodes == 0) {
72092564cb1Sesaxe #ifdef	DEBUG
72192564cb1Sesaxe 		uint_t family;
72292564cb1Sesaxe 
723d2aeaf66SEric Saxe 		/*
724d2aeaf66SEric Saxe 		 * This routine uses a PCI config space based mechanism
725d2aeaf66SEric Saxe 		 * for retrieving the number of nodes in the system.
726d2aeaf66SEric Saxe 		 * Device 24, function 0, offset 0x60 as used here is not
727d2aeaf66SEric Saxe 		 * AMD processor architectural, and may not work on processor
728d2aeaf66SEric Saxe 		 * families other than those listed below.
729d2aeaf66SEric Saxe 		 *
730d2aeaf66SEric Saxe 		 * Callers of this routine must ensure that we're running on
731d2aeaf66SEric Saxe 		 * a processor which supports this mechanism.
732d2aeaf66SEric Saxe 		 * The assertion below is meant to catch calls on unsupported
733d2aeaf66SEric Saxe 		 * processors.
734d2aeaf66SEric Saxe 		 */
73592564cb1Sesaxe 		family = cpuid_getfamily(CPU);
736d2aeaf66SEric Saxe 		ASSERT(family == 0xf || family == 0x10 || family == 0x11);
73792564cb1Sesaxe #endif	/* DEBUG */
73892564cb1Sesaxe 
73992564cb1Sesaxe 		/*
74092564cb1Sesaxe 		 * Obtain the number of nodes in the system from
74192564cb1Sesaxe 		 * bits [6:4] of the Node ID register on node 0.
74292564cb1Sesaxe 		 *
74392564cb1Sesaxe 		 * The actual node count is NodeID[6:4] + 1
74492564cb1Sesaxe 		 *
74592564cb1Sesaxe 		 * The Node ID register is accessed via function 0,
74692564cb1Sesaxe 		 * offset 0x60. Node 0 is device 24.
74792564cb1Sesaxe 		 */
74892564cb1Sesaxe 		nnodes = ((pci_getl_func(0, 24, 0, 0x60) & 0x70) >> 4) + 1;
74992564cb1Sesaxe 	}
75092564cb1Sesaxe 	return (nnodes);
75192564cb1Sesaxe }
75292564cb1Sesaxe 
7537c478bd9Sstevel@tonic-gate uint_t
do_erratum_298(struct cpu * cpu)754512cf780Skchow do_erratum_298(struct cpu *cpu)
755512cf780Skchow {
756512cf780Skchow 	static int	osvwrc = -3;
757512cf780Skchow 	extern int	osvw_opteron_erratum(cpu_t *, uint_t);
758512cf780Skchow 
759512cf780Skchow 	/*
760512cf780Skchow 	 * L2 Eviction May Occur During Processor Operation To Set
761512cf780Skchow 	 * Accessed or Dirty Bit.
762512cf780Skchow 	 */
763512cf780Skchow 	if (osvwrc == -3) {
764512cf780Skchow 		osvwrc = osvw_opteron_erratum(cpu, 298);
765512cf780Skchow 	} else {
766512cf780Skchow 		/* osvw return codes should be consistent for all cpus */
767512cf780Skchow 		ASSERT(osvwrc == osvw_opteron_erratum(cpu, 298));
768512cf780Skchow 	}
769512cf780Skchow 
770512cf780Skchow 	switch (osvwrc) {
771512cf780Skchow 	case 0:		/* erratum is not present: do nothing */
772512cf780Skchow 		break;
773512cf780Skchow 	case 1:		/* erratum is present: BIOS workaround applied */
774512cf780Skchow 		/*
775512cf780Skchow 		 * check if workaround is actually in place and issue warning
776512cf780Skchow 		 * if not.
777512cf780Skchow 		 */
778512cf780Skchow 		if (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
779512cf780Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0)) {
780512cf780Skchow #if defined(OPTERON_ERRATUM_298)
781512cf780Skchow 			opteron_erratum_298++;
782512cf780Skchow #else
783512cf780Skchow 			workaround_warning(cpu, 298);
784512cf780Skchow 			return (1);
785512cf780Skchow #endif
786512cf780Skchow 		}
787512cf780Skchow 		break;
788512cf780Skchow 	case -1:	/* cannot determine via osvw: check cpuid */
789512cf780Skchow 		if ((cpuid_opteron_erratum(cpu, 298) > 0) &&
790512cf780Skchow 		    (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
791512cf780Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0))) {
792512cf780Skchow #if defined(OPTERON_ERRATUM_298)
793512cf780Skchow 			opteron_erratum_298++;
794512cf780Skchow #else
795512cf780Skchow 			workaround_warning(cpu, 298);
796512cf780Skchow 			return (1);
797512cf780Skchow #endif
798512cf780Skchow 		}
799512cf780Skchow 		break;
800512cf780Skchow 	}
801512cf780Skchow 	return (0);
802512cf780Skchow }
803512cf780Skchow 
804512cf780Skchow uint_t
workaround_errata(struct cpu * cpu)8057c478bd9Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
8067c478bd9Sstevel@tonic-gate {
8073df2e8b2SRobert Mustacchi 	volatile uint_t missing = 0;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	ASSERT(cpu == CPU);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/*LINTED*/
8127c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
8137c478bd9Sstevel@tonic-gate 		/*
8147c478bd9Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
8157c478bd9Sstevel@tonic-gate 		 */
8167c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
8177c478bd9Sstevel@tonic-gate 		/*
8187c478bd9Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
8197c478bd9Sstevel@tonic-gate 		 */
820ae115bc7Smrj 		opteron_erratum_88++;
8217c478bd9Sstevel@tonic-gate #else
822ae115bc7Smrj 		workaround_warning(cpu, 88);
8237c478bd9Sstevel@tonic-gate 		missing++;
8247c478bd9Sstevel@tonic-gate #endif
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
8287c478bd9Sstevel@tonic-gate 		/*
8297c478bd9Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
8307c478bd9Sstevel@tonic-gate 		 */
8317c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
8327c478bd9Sstevel@tonic-gate 		/*
8337c478bd9Sstevel@tonic-gate 		 * fix is in trap.c
8347c478bd9Sstevel@tonic-gate 		 */
8357c478bd9Sstevel@tonic-gate 		opteron_erratum_91++;
8367c478bd9Sstevel@tonic-gate #else
837ae115bc7Smrj 		workaround_warning(cpu, 91);
8387c478bd9Sstevel@tonic-gate 		missing++;
8397c478bd9Sstevel@tonic-gate #endif
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
8437c478bd9Sstevel@tonic-gate 		/*
8447c478bd9Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
8457c478bd9Sstevel@tonic-gate 		 */
8467c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
8477c478bd9Sstevel@tonic-gate 		/*
8487c478bd9Sstevel@tonic-gate 		 * fix is in trap.c
8497c478bd9Sstevel@tonic-gate 		 */
8507c478bd9Sstevel@tonic-gate 		opteron_erratum_93++;
8517c478bd9Sstevel@tonic-gate #else
852ae115bc7Smrj 		workaround_warning(cpu, 93);
8537c478bd9Sstevel@tonic-gate 		missing++;
8547c478bd9Sstevel@tonic-gate #endif
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	/*LINTED*/
8587c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
8597c478bd9Sstevel@tonic-gate 		/*
8607c478bd9Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
8617c478bd9Sstevel@tonic-gate 		 */
8627c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
8637c478bd9Sstevel@tonic-gate #if defined(_LP64)
8647c478bd9Sstevel@tonic-gate 		/*
8657c478bd9Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
8667c478bd9Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
8677c478bd9Sstevel@tonic-gate 		 * range mod 4G.
8687c478bd9Sstevel@tonic-gate 		 */
8697c478bd9Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
8707c478bd9Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		/*LINTED*/
8737c478bd9Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
874ae115bc7Smrj 		opteron_erratum_95++;
8757c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
8767c478bd9Sstevel@tonic-gate #else
877ae115bc7Smrj 		workaround_warning(cpu, 95);
8787c478bd9Sstevel@tonic-gate 		missing++;
879ae115bc7Smrj #endif
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
8837c478bd9Sstevel@tonic-gate 		/*
8847c478bd9Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
8857c478bd9Sstevel@tonic-gate 		 */
8867c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
8877c478bd9Sstevel@tonic-gate 		/*
8887c478bd9Sstevel@tonic-gate 		 * fix is in trap.c
8897c478bd9Sstevel@tonic-gate 		 */
8907c478bd9Sstevel@tonic-gate 		opteron_erratum_100++;
8917c478bd9Sstevel@tonic-gate #else
892ae115bc7Smrj 		workaround_warning(cpu, 100);
8937c478bd9Sstevel@tonic-gate 		missing++;
8947c478bd9Sstevel@tonic-gate #endif
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	/*LINTED*/
8987c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
8997c478bd9Sstevel@tonic-gate 		/*
9007c478bd9Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
9017c478bd9Sstevel@tonic-gate 		 * Some Processors
9027c478bd9Sstevel@tonic-gate 		 */
9037c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
9047c478bd9Sstevel@tonic-gate 		/*
9057c478bd9Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
9067c478bd9Sstevel@tonic-gate 		 * those processors)
9077c478bd9Sstevel@tonic-gate 		 */
9087c478bd9Sstevel@tonic-gate #else
909ae115bc7Smrj 		workaround_warning(cpu, 108);
9107c478bd9Sstevel@tonic-gate 		missing++;
9117c478bd9Sstevel@tonic-gate #endif
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/*LINTED*/
915ae115bc7Smrj 	if (cpuid_opteron_erratum(cpu, 109) > 0) do {
9167c478bd9Sstevel@tonic-gate 		/*
917fb2caebeSRandy Fishel 		 * Certain Reverse REP MOVS May Produce Unpredictable Behavior
9187c478bd9Sstevel@tonic-gate 		 */
9197c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
920ae115bc7Smrj 		/*
921ae115bc7Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
922ae115bc7Smrj 		 */
923ae115bc7Smrj 		uint64_t value;
924ae115bc7Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
925ae115bc7Smrj 		int err;
9267c478bd9Sstevel@tonic-gate 
927ae115bc7Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
928ae115bc7Smrj 			msr_warning(cpu, "rd", msr, err);
929ae115bc7Smrj 			workaround_warning(cpu, 109);
930ae115bc7Smrj 			missing++;
931ae115bc7Smrj 		}
932ae115bc7Smrj 		if (value == 0)
9337c478bd9Sstevel@tonic-gate 			opteron_erratum_109++;
9347c478bd9Sstevel@tonic-gate #else
935ae115bc7Smrj 		workaround_warning(cpu, 109);
9367c478bd9Sstevel@tonic-gate 		missing++;
9377c478bd9Sstevel@tonic-gate #endif
938ae115bc7Smrj 	/*CONSTANTCONDITION*/
939ae115bc7Smrj 	} while (0);
940ae115bc7Smrj 
9417c478bd9Sstevel@tonic-gate 	/*LINTED*/
9427c478bd9Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
9437c478bd9Sstevel@tonic-gate 		/*
9447c478bd9Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
9457c478bd9Sstevel@tonic-gate 		 * Processor Hang
9467c478bd9Sstevel@tonic-gate 		 */
9477c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
948ae115bc7Smrj #if defined(_LP64)
9497c478bd9Sstevel@tonic-gate 		/*
9507c478bd9Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
9517c478bd9Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
9527c478bd9Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
9537c478bd9Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
9547c478bd9Sstevel@tonic-gate 		 */
955ae115bc7Smrj 		if (opteron_erratum_121)
956ae115bc7Smrj 			opteron_erratum_121++;
957ae115bc7Smrj 		else {
9587c478bd9Sstevel@tonic-gate 			if (hole_start) {
9597c478bd9Sstevel@tonic-gate 				hole_start -= PAGESIZE;
9607c478bd9Sstevel@tonic-gate 			} else {
9617c478bd9Sstevel@tonic-gate 				/*
9627c478bd9Sstevel@tonic-gate 				 * hole_start not yet initialized by
9637c478bd9Sstevel@tonic-gate 				 * mmu_init. Initialize hole_start
9647c478bd9Sstevel@tonic-gate 				 * with value to be subtracted.
9657c478bd9Sstevel@tonic-gate 				 */
9667c478bd9Sstevel@tonic-gate 				hole_start = PAGESIZE;
9677c478bd9Sstevel@tonic-gate 			}
9687c478bd9Sstevel@tonic-gate 			opteron_erratum_121++;
9697c478bd9Sstevel@tonic-gate 		}
970ae115bc7Smrj #endif	/* _LP64 */
9717c478bd9Sstevel@tonic-gate #else
972ae115bc7Smrj 		workaround_warning(cpu, 121);
9737c478bd9Sstevel@tonic-gate 		missing++;
9747c478bd9Sstevel@tonic-gate #endif
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	/*LINTED*/
978ae115bc7Smrj 	if (cpuid_opteron_erratum(cpu, 122) > 0) do {
9797c478bd9Sstevel@tonic-gate 		/*
980ae115bc7Smrj 		 * TLB Flush Filter May Cause Coherency Problem in
9817c478bd9Sstevel@tonic-gate 		 * Multiprocessor Systems
9827c478bd9Sstevel@tonic-gate 		 */
9837c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
984ae115bc7Smrj 		uint64_t value;
985ae115bc7Smrj 		const uint_t msr = MSR_AMD_HWCR;
986ae115bc7Smrj 		int error;
987ae115bc7Smrj 
9887c478bd9Sstevel@tonic-gate 		/*
9897c478bd9Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
9907c478bd9Sstevel@tonic-gate 		 * or multi-processor).
9917c478bd9Sstevel@tonic-gate 		 */
992843e1988Sjohnlev #if defined(__xpv)
993843e1988Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
994843e1988Sjohnlev 			break;
995349b53ddSStuart Maybee 		if (!opteron_erratum_122 && xpv_nr_phys_cpus() == 1)
996843e1988Sjohnlev 			break;
997843e1988Sjohnlev #else
99892564cb1Sesaxe 		if (!opteron_erratum_122 && opteron_get_nnodes() == 1 &&
999ae115bc7Smrj 		    cpuid_get_ncpu_per_chip(cpu) == 1)
1000ae115bc7Smrj 			break;
1001843e1988Sjohnlev #endif
10027c478bd9Sstevel@tonic-gate 		/* disable TLB Flush Filter */
10037c478bd9Sstevel@tonic-gate 
1004ae115bc7Smrj 		if ((error = checked_rdmsr(msr, &value)) != 0) {
1005ae115bc7Smrj 			msr_warning(cpu, "rd", msr, error);
1006ae115bc7Smrj 			workaround_warning(cpu, 122);
1007ae115bc7Smrj 			missing++;
1008ae115bc7Smrj 		} else {
1009ae115bc7Smrj 			value |= (uint64_t)AMD_HWCR_FFDIS;
1010ae115bc7Smrj 			if ((error = checked_wrmsr(msr, value)) != 0) {
1011ae115bc7Smrj 				msr_warning(cpu, "wr", msr, error);
1012ae115bc7Smrj 				workaround_warning(cpu, 122);
1013ae115bc7Smrj 				missing++;
1014ae115bc7Smrj 			}
1015ae115bc7Smrj 		}
1016ae115bc7Smrj 		opteron_erratum_122++;
10177c478bd9Sstevel@tonic-gate #else
1018ae115bc7Smrj 		workaround_warning(cpu, 122);
10197c478bd9Sstevel@tonic-gate 		missing++;
10207c478bd9Sstevel@tonic-gate #endif
1021ae115bc7Smrj 	/*CONSTANTCONDITION*/
1022ae115bc7Smrj 	} while (0);
1023403c216aSkchow 
10247c478bd9Sstevel@tonic-gate 	/*LINTED*/
1025ae115bc7Smrj 	if (cpuid_opteron_erratum(cpu, 123) > 0) do {
10267c478bd9Sstevel@tonic-gate 		/*
10277c478bd9Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
10287c478bd9Sstevel@tonic-gate 		 * Dual Core Processors
10297c478bd9Sstevel@tonic-gate 		 */
1030ae115bc7Smrj #if defined(OPTERON_ERRATUM_123)
1031ae115bc7Smrj 		uint64_t value;
1032ae115bc7Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
1033ae115bc7Smrj 		int err;
1034ae115bc7Smrj 
10357c478bd9Sstevel@tonic-gate 		/*
10367c478bd9Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
10377c478bd9Sstevel@tonic-gate 		 */
1038ae115bc7Smrj 		if (cpuid_get_ncpu_per_chip(cpu) < 2)
1039ae115bc7Smrj 			break;
1040843e1988Sjohnlev #if defined(__xpv)
1041843e1988Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
1042843e1988Sjohnlev 			break;
1043843e1988Sjohnlev #endif
1044ae115bc7Smrj 		/*
1045ae115bc7Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
1046ae115bc7Smrj 		 */
1047ae115bc7Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
1048ae115bc7Smrj 			msr_warning(cpu, "rd", msr, err);
1049ae115bc7Smrj 			workaround_warning(cpu, 123);
1050ae115bc7Smrj 			missing++;
1051ae115bc7Smrj 		}
1052ae115bc7Smrj 		if (value == 0)
10537c478bd9Sstevel@tonic-gate 			opteron_erratum_123++;
1054ae115bc7Smrj #else
1055ae115bc7Smrj 		workaround_warning(cpu, 123);
1056ae115bc7Smrj 		missing++;
10572201b277Skucharsk 
1058ae115bc7Smrj #endif
1059ae115bc7Smrj 	/*CONSTANTCONDITION*/
1060ae115bc7Smrj 	} while (0);
1061ae115bc7Smrj 
10622201b277Skucharsk 	/*LINTED*/
1063ae115bc7Smrj 	if (cpuid_opteron_erratum(cpu, 131) > 0) do {
10642201b277Skucharsk 		/*
10652201b277Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
10662201b277Skucharsk 		 * Waiting for a Probe Response
10672201b277Skucharsk 		 */
1068ae115bc7Smrj #if defined(OPTERON_ERRATUM_131)
1069ae115bc7Smrj 		uint64_t nbcfg;
1070ae115bc7Smrj 		const uint_t msr = MSR_AMD_NB_CFG;
1071ae115bc7Smrj 		const uint64_t wabits =
1072ae115bc7Smrj 		    AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
1073ae115bc7Smrj 		int error;
1074ae115bc7Smrj 
10752201b277Skucharsk 		/*
10762201b277Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
10772201b277Skucharsk 		 */
1078ae115bc7Smrj 		if (opteron_erratum_131)
1079ae115bc7Smrj 			break;
1080843e1988Sjohnlev #if defined(__xpv)
1081843e1988Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
1082843e1988Sjohnlev 			break;
1083349b53ddSStuart Maybee 		if (xpv_nr_phys_cpus() < 4)
1084843e1988Sjohnlev 			break;
1085843e1988Sjohnlev #else
108692564cb1Sesaxe 		if (opteron_get_nnodes() * cpuid_get_ncpu_per_chip(cpu) < 4)
1087ae115bc7Smrj 			break;
1088843e1988Sjohnlev #endif
10892201b277Skucharsk 		/*
1090ae115bc7Smrj 		 * Print a warning if neither of the workarounds for
1091ae115bc7Smrj 		 * erratum 131 is present.
10922201b277Skucharsk 		 */
1093ae115bc7Smrj 		if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
1094ae115bc7Smrj 			msr_warning(cpu, "rd", msr, error);
1095ae115bc7Smrj 			workaround_warning(cpu, 131);
1096ae115bc7Smrj 			missing++;
1097ae115bc7Smrj 		} else if ((nbcfg & wabits) == 0) {
10982201b277Skucharsk 			opteron_erratum_131++;
1099cb9f16ebSkchow 		} else {
1100cb9f16ebSkchow 			/* cannot have both workarounds set */
1101cb9f16ebSkchow 			ASSERT((nbcfg & wabits) != wabits);
1102cb9f16ebSkchow 		}
1103ae115bc7Smrj #else
1104ae115bc7Smrj 		workaround_warning(cpu, 131);
1105ae115bc7Smrj 		missing++;
1106ef50d8c0Sesaxe #endif
1107ae115bc7Smrj 	/*CONSTANTCONDITION*/
1108ae115bc7Smrj 	} while (0);
1109ef50d8c0Sesaxe 
1110ef50d8c0Sesaxe 	/*
1111ae115bc7Smrj 	 * This isn't really an erratum, but for convenience the
1112ef50d8c0Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
1113bf9b145bSRobert Mustacchi 	 * Note, the technique only is valid on families before 12h and
1114bf9b145bSRobert Mustacchi 	 * certainly doesn't work when we're virtualized. This is checked for in
1115bf9b145bSRobert Mustacchi 	 * the erratum workaround.
1116ef50d8c0Sesaxe 	 */
1117ef50d8c0Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
1118ae115bc7Smrj #if defined(OPTERON_WORKAROUND_6336786)
1119ef50d8c0Sesaxe 		/*
1120ef50d8c0Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
1121ef50d8c0Sesaxe 		 * K8 platforms to guard against TSC drift.
1122ef50d8c0Sesaxe 		 */
1123ef50d8c0Sesaxe 		if (opteron_workaround_6336786) {
1124ef50d8c0Sesaxe 			opteron_workaround_6336786++;
1125843e1988Sjohnlev #if defined(__xpv)
1126843e1988Sjohnlev 		} else if ((DOMAIN_IS_INITDOMAIN(xen_info) &&
1127349b53ddSStuart Maybee 		    xpv_nr_phys_cpus() > 1) ||
1128843e1988Sjohnlev 		    opteron_workaround_6336786_UP) {
1129843e1988Sjohnlev 			/*
113092564cb1Sesaxe 			 * XXPV	Hmm.  We can't walk the Northbridges on
1131843e1988Sjohnlev 			 *	the hypervisor; so just complain and drive
1132843e1988Sjohnlev 			 *	on.  This probably needs to be fixed in
1133843e1988Sjohnlev 			 *	the hypervisor itself.
1134843e1988Sjohnlev 			 */
1135843e1988Sjohnlev 			opteron_workaround_6336786++;
1136843e1988Sjohnlev 			workaround_warning(cpu, 6336786);
1137843e1988Sjohnlev #else	/* __xpv */
113892564cb1Sesaxe 		} else if ((opteron_get_nnodes() *
1139ae115bc7Smrj 		    cpuid_get_ncpu_per_chip(cpu) > 1) ||
1140ef50d8c0Sesaxe 		    opteron_workaround_6336786_UP) {
114192564cb1Sesaxe 
114292564cb1Sesaxe 			uint_t	node, nnodes;
1143ae115bc7Smrj 			uint8_t data;
1144ae115bc7Smrj 
114592564cb1Sesaxe 			nnodes = opteron_get_nnodes();
114692564cb1Sesaxe 			for (node = 0; node < nnodes; node++) {
1147ef50d8c0Sesaxe 				/*
1148ef50d8c0Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
1149ef50d8c0Sesaxe 				 * Northbridge device is the node id + 24.
1150ef50d8c0Sesaxe 				 */
1151ef50d8c0Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
1152ef50d8c0Sesaxe 				data &= 0xFC;
1153ef50d8c0Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
1154ef50d8c0Sesaxe 			}
1155ef50d8c0Sesaxe 			opteron_workaround_6336786++;
1156843e1988Sjohnlev #endif	/* __xpv */
1157ef50d8c0Sesaxe 		}
1158ae115bc7Smrj #else
1159ae115bc7Smrj 		workaround_warning(cpu, 6336786);
1160ae115bc7Smrj 		missing++;
1161ef50d8c0Sesaxe #endif
1162ae115bc7Smrj 	}
1163ee88d2b9Skchow 
1164ee88d2b9Skchow 	/*LINTED*/
1165ee88d2b9Skchow 	/*
1166bf9b145bSRobert Mustacchi 	 * Mutex primitives don't work as expected. This is erratum #147 from
1167bf9b145bSRobert Mustacchi 	 * 'Revision Guide for AMD Athlon 64 and AMD Opteron Processors'
1168bf9b145bSRobert Mustacchi 	 * document 25759.
1169ee88d2b9Skchow 	 */
1170ee6ee36aSPatrick Mooney 	if (cpuid_opteron_erratum(cpu, 147) > 0) {
1171ee6ee36aSPatrick Mooney #if defined(OPTERON_ERRATUM_147)
1172ee88d2b9Skchow 		/*
1173ae115bc7Smrj 		 * This problem only occurs with 2 or more cores. If bit in
1174512cf780Skchow 		 * MSR_AMD_BU_CFG set, then not applicable. The workaround
1175ee88d2b9Skchow 		 * is to patch the semaphone routines with the lfence
1176ee88d2b9Skchow 		 * instruction to provide necessary load memory barrier with
1177ee88d2b9Skchow 		 * possible subsequent read-modify-write ops.
1178ee88d2b9Skchow 		 *
1179ee88d2b9Skchow 		 * It is too early in boot to call the patch routine so
1180ee88d2b9Skchow 		 * set erratum variable to be done in startup_end().
1181ee88d2b9Skchow 		 */
1182ee6ee36aSPatrick Mooney 		if (opteron_erratum_147) {
1183ee6ee36aSPatrick Mooney 			opteron_erratum_147++;
1184843e1988Sjohnlev #if defined(__xpv)
11857417cfdeSKuriakose Kuruvilla 		} else if (is_x86_feature(x86_featureset, X86FSET_SSE2)) {
1186843e1988Sjohnlev 			if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1187843e1988Sjohnlev 				/*
1188843e1988Sjohnlev 				 * XXPV	Use dom0_msr here when extended
1189843e1988Sjohnlev 				 *	operations are supported?
1190843e1988Sjohnlev 				 */
1191349b53ddSStuart Maybee 				if (xpv_nr_phys_cpus() > 1)
1192ee6ee36aSPatrick Mooney 					opteron_erratum_147++;
1193843e1988Sjohnlev 			} else {
1194843e1988Sjohnlev 				/*
1195843e1988Sjohnlev 				 * We have no way to tell how many physical
1196843e1988Sjohnlev 				 * cpus there are, or even if this processor
1197843e1988Sjohnlev 				 * has the problem, so enable the workaround
1198843e1988Sjohnlev 				 * unconditionally (at some performance cost).
1199843e1988Sjohnlev 				 */
1200ee6ee36aSPatrick Mooney 				opteron_erratum_147++;
1201843e1988Sjohnlev 			}
1202843e1988Sjohnlev #else	/* __xpv */
12037417cfdeSKuriakose Kuruvilla 		} else if (is_x86_feature(x86_featureset, X86FSET_SSE2) &&
12047417cfdeSKuriakose Kuruvilla 		    ((opteron_get_nnodes() *
1205ae115bc7Smrj 		    cpuid_get_ncpu_per_chip(cpu)) > 1)) {
120648b2bf45SKit Chow 			if ((xrdmsr(MSR_AMD_BU_CFG) & (UINT64_C(1) << 33)) == 0)
1207ee6ee36aSPatrick Mooney 				opteron_erratum_147++;
1208843e1988Sjohnlev #endif	/* __xpv */
1209ee88d2b9Skchow 		}
1210ae115bc7Smrj #else
1211ee6ee36aSPatrick Mooney 		workaround_warning(cpu, 147);
1212ae115bc7Smrj 		missing++;
1213ee88d2b9Skchow #endif
1214ae115bc7Smrj 	}
1215ae115bc7Smrj 
1216512cf780Skchow 	missing += do_erratum_298(cpu);
1217512cf780Skchow 
12185e54b56dSHans Rosenfeld 	if (cpuid_opteron_erratum(cpu, 721) > 0) {
12195e54b56dSHans Rosenfeld #if defined(OPTERON_ERRATUM_721)
1220850ad55aSHans Rosenfeld 		on_trap_data_t otd;
1221850ad55aSHans Rosenfeld 
1222850ad55aSHans Rosenfeld 		if (!on_trap(&otd, OT_DATA_ACCESS))
1223850ad55aSHans Rosenfeld 			wrmsr(MSR_AMD_DE_CFG,
1224850ad55aSHans Rosenfeld 			    rdmsr(MSR_AMD_DE_CFG) | AMD_DE_CFG_E721);
1225850ad55aSHans Rosenfeld 		no_trap();
1226850ad55aSHans Rosenfeld 
12275e54b56dSHans Rosenfeld 		opteron_erratum_721++;
12285e54b56dSHans Rosenfeld #else
12295e54b56dSHans Rosenfeld 		workaround_warning(cpu, 721);
12305e54b56dSHans Rosenfeld 		missing++;
12315e54b56dSHans Rosenfeld #endif
12325e54b56dSHans Rosenfeld 	}
12335e54b56dSHans Rosenfeld 
1234843e1988Sjohnlev #ifdef __xpv
1235843e1988Sjohnlev 	return (0);
1236843e1988Sjohnlev #else
12377c478bd9Sstevel@tonic-gate 	return (missing);
1238843e1988Sjohnlev #endif
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate void
workaround_errata_end()12427c478bd9Sstevel@tonic-gate workaround_errata_end()
12437c478bd9Sstevel@tonic-gate {
1244ae115bc7Smrj #if defined(OPTERON_ERRATUM_88)
1245ae115bc7Smrj 	if (opteron_erratum_88)
1246ae115bc7Smrj 		workaround_applied(88);
1247ae115bc7Smrj #endif
1248ae115bc7Smrj #if defined(OPTERON_ERRATUM_91)
1249ae115bc7Smrj 	if (opteron_erratum_91)
1250ae115bc7Smrj 		workaround_applied(91);
1251ae115bc7Smrj #endif
1252ae115bc7Smrj #if defined(OPTERON_ERRATUM_93)
1253ae115bc7Smrj 	if (opteron_erratum_93)
1254ae115bc7Smrj 		workaround_applied(93);
1255ae115bc7Smrj #endif
1256ae115bc7Smrj #if defined(OPTERON_ERRATUM_95)
1257ae115bc7Smrj 	if (opteron_erratum_95)
1258ae115bc7Smrj 		workaround_applied(95);
1259ae115bc7Smrj #endif
1260ae115bc7Smrj #if defined(OPTERON_ERRATUM_100)
1261ae115bc7Smrj 	if (opteron_erratum_100)
1262ae115bc7Smrj 		workaround_applied(100);
1263ae115bc7Smrj #endif
1264ae115bc7Smrj #if defined(OPTERON_ERRATUM_108)
1265ae115bc7Smrj 	if (opteron_erratum_108)
1266ae115bc7Smrj 		workaround_applied(108);
1267ae115bc7Smrj #endif
12687c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
12697c478bd9Sstevel@tonic-gate 	if (opteron_erratum_109) {
12702201b277Skucharsk 		cmn_err(CE_WARN,
12712201b277Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
12722201b277Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
12732201b277Skucharsk 		    " system's BIOS to a version\ncontaining this"
12742201b277Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
12752201b277Skucharsk 		    " system\noperation may occur.\n");
12767c478bd9Sstevel@tonic-gate 	}
1277ae115bc7Smrj #endif
1278ae115bc7Smrj #if defined(OPTERON_ERRATUM_121)
1279ae115bc7Smrj 	if (opteron_erratum_121)
1280ae115bc7Smrj 		workaround_applied(121);
1281ae115bc7Smrj #endif
1282ae115bc7Smrj #if defined(OPTERON_ERRATUM_122)
1283ae115bc7Smrj 	if (opteron_erratum_122)
1284ae115bc7Smrj 		workaround_applied(122);
1285ae115bc7Smrj #endif
12867c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
12877c478bd9Sstevel@tonic-gate 	if (opteron_erratum_123) {
12882201b277Skucharsk 		cmn_err(CE_WARN,
12892201b277Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
12902201b277Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
12912201b277Skucharsk 		    " system's BIOS to a version\ncontaining this"
12922201b277Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
12932201b277Skucharsk 		    " system\noperation may occur.\n");
12947c478bd9Sstevel@tonic-gate 	}
1295ae115bc7Smrj #endif
12962201b277Skucharsk #if defined(OPTERON_ERRATUM_131)
12972201b277Skucharsk 	if (opteron_erratum_131) {
12982201b277Skucharsk 		cmn_err(CE_WARN,
12992201b277Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
13002201b277Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
13012201b277Skucharsk 		    " system's BIOS to a version\ncontaining this"
13022201b277Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
13032201b277Skucharsk 		    " system\noperation may occur.\n");
13042201b277Skucharsk 	}
1305ae115bc7Smrj #endif
1306ae115bc7Smrj #if defined(OPTERON_WORKAROUND_6336786)
1307ae115bc7Smrj 	if (opteron_workaround_6336786)
1308ae115bc7Smrj 		workaround_applied(6336786);
1309ae115bc7Smrj #endif
1310ee6ee36aSPatrick Mooney #if defined(OPTERON_ERRATUM_147)
1311ee6ee36aSPatrick Mooney 	if (opteron_erratum_147)
1312ee6ee36aSPatrick Mooney 		workaround_applied(147);
1313ae115bc7Smrj #endif
1314512cf780Skchow #if defined(OPTERON_ERRATUM_298)
1315512cf780Skchow 	if (opteron_erratum_298) {
1316512cf780Skchow 		cmn_err(CE_WARN,
1317512cf780Skchow 		    "BIOS microcode patch for AMD 64/Opteron(tm)"
1318512cf780Skchow 		    " processor\nerratum 298 was not detected; updating your"
1319512cf780Skchow 		    " system's BIOS to a version\ncontaining this"
1320512cf780Skchow 		    " microcode patch is HIGHLY recommended or erroneous"
1321512cf780Skchow 		    " system\noperation may occur.\n");
1322512cf780Skchow 	}
1323512cf780Skchow #endif
13245e54b56dSHans Rosenfeld #if defined(OPTERON_ERRATUM_721)
13255e54b56dSHans Rosenfeld 	if (opteron_erratum_721)
13265e54b56dSHans Rosenfeld 		workaround_applied(721);
13275e54b56dSHans Rosenfeld #endif
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate 
1330a3114836SGerry Liu /*
1331a3114836SGerry Liu  * The procset_slave and procset_master are used to synchronize
1332a3114836SGerry Liu  * between the control CPU and the target CPU when starting CPUs.
1333a3114836SGerry Liu  */
1334a3114836SGerry Liu static cpuset_t procset_slave, procset_master;
1335a3114836SGerry Liu 
1336a3114836SGerry Liu static void
mp_startup_wait(cpuset_t * sp,processorid_t cpuid)1337a3114836SGerry Liu mp_startup_wait(cpuset_t *sp, processorid_t cpuid)
1338a3114836SGerry Liu {
1339a3114836SGerry Liu 	cpuset_t tempset;
1340a3114836SGerry Liu 
1341a3114836SGerry Liu 	for (tempset = *sp; !CPU_IN_SET(tempset, cpuid);
1342a3114836SGerry Liu 	    tempset = *(volatile cpuset_t *)sp) {
1343a3114836SGerry Liu 		SMT_PAUSE();
1344a3114836SGerry Liu 	}
1345a3114836SGerry Liu 	CPUSET_ATOMIC_DEL(*(cpuset_t *)sp, cpuid);
1346a3114836SGerry Liu }
1347a3114836SGerry Liu 
1348a3114836SGerry Liu static void
mp_startup_signal(cpuset_t * sp,processorid_t cpuid)1349a3114836SGerry Liu mp_startup_signal(cpuset_t *sp, processorid_t cpuid)
1350a3114836SGerry Liu {
1351a3114836SGerry Liu 	cpuset_t tempset;
1352a3114836SGerry Liu 
1353a3114836SGerry Liu 	CPUSET_ATOMIC_ADD(*(cpuset_t *)sp, cpuid);
1354a3114836SGerry Liu 	for (tempset = *sp; CPU_IN_SET(tempset, cpuid);
1355a3114836SGerry Liu 	    tempset = *(volatile cpuset_t *)sp) {
1356a3114836SGerry Liu 		SMT_PAUSE();
1357a3114836SGerry Liu 	}
1358a3114836SGerry Liu }
1359a3114836SGerry Liu 
1360a3114836SGerry Liu int
mp_start_cpu_common(cpu_t * cp,boolean_t boot)1361a3114836SGerry Liu mp_start_cpu_common(cpu_t *cp, boolean_t boot)
1362a3114836SGerry Liu {
1363a3114836SGerry Liu 	_NOTE(ARGUNUSED(boot));
1364a3114836SGerry Liu 
1365a3114836SGerry Liu 	void *ctx;
1366a3114836SGerry Liu 	int delays;
1367a3114836SGerry Liu 	int error = 0;
1368a3114836SGerry Liu 	cpuset_t tempset;
1369a3114836SGerry Liu 	processorid_t cpuid;
1370a3114836SGerry Liu #ifndef __xpv
1371a3114836SGerry Liu 	extern void cpupm_init(cpu_t *);
1372a3114836SGerry Liu #endif
1373a3114836SGerry Liu 
1374a3114836SGerry Liu 	ASSERT(cp != NULL);
1375a3114836SGerry Liu 	cpuid = cp->cpu_id;
1376a3114836SGerry Liu 	ctx = mach_cpucontext_alloc(cp);
1377a3114836SGerry Liu 	if (ctx == NULL) {
1378a3114836SGerry Liu 		cmn_err(CE_WARN,
1379a3114836SGerry Liu 		    "cpu%d: failed to allocate context", cp->cpu_id);
1380a3114836SGerry Liu 		return (EAGAIN);
1381a3114836SGerry Liu 	}
1382a3114836SGerry Liu 	error = mach_cpu_start(cp, ctx);
1383a3114836SGerry Liu 	if (error != 0) {
1384a3114836SGerry Liu 		cmn_err(CE_WARN,
1385a3114836SGerry Liu 		    "cpu%d: failed to start, error %d", cp->cpu_id, error);
1386a3114836SGerry Liu 		mach_cpucontext_free(cp, ctx, error);
1387a3114836SGerry Liu 		return (error);
1388a3114836SGerry Liu 	}
1389a3114836SGerry Liu 
1390a3114836SGerry Liu 	for (delays = 0, tempset = procset_slave; !CPU_IN_SET(tempset, cpuid);
1391a3114836SGerry Liu 	    delays++) {
1392a3114836SGerry Liu 		if (delays == 500) {
1393a3114836SGerry Liu 			/*
1394a3114836SGerry Liu 			 * After five seconds, things are probably looking
1395a3114836SGerry Liu 			 * a bit bleak - explain the hang.
1396a3114836SGerry Liu 			 */
1397a3114836SGerry Liu 			cmn_err(CE_NOTE, "cpu%d: started, "
1398a3114836SGerry Liu 			    "but not running in the kernel yet", cpuid);
1399a3114836SGerry Liu 		} else if (delays > 2000) {
1400a3114836SGerry Liu 			/*
1401a3114836SGerry Liu 			 * We waited at least 20 seconds, bail ..
1402a3114836SGerry Liu 			 */
1403a3114836SGerry Liu 			error = ETIMEDOUT;
1404a3114836SGerry Liu 			cmn_err(CE_WARN, "cpu%d: timed out", cpuid);
1405a3114836SGerry Liu 			mach_cpucontext_free(cp, ctx, error);
1406a3114836SGerry Liu 			return (error);
1407a3114836SGerry Liu 		}
1408a3114836SGerry Liu 
1409a3114836SGerry Liu 		/*
1410a3114836SGerry Liu 		 * wait at least 10ms, then check again..
1411a3114836SGerry Liu 		 */
1412a3114836SGerry Liu 		delay(USEC_TO_TICK_ROUNDUP(10000));
1413a3114836SGerry Liu 		tempset = *((volatile cpuset_t *)&procset_slave);
1414a3114836SGerry Liu 	}
1415a3114836SGerry Liu 	CPUSET_ATOMIC_DEL(procset_slave, cpuid);
1416a3114836SGerry Liu 
1417a3114836SGerry Liu 	mach_cpucontext_free(cp, ctx, 0);
1418a3114836SGerry Liu 
1419a3114836SGerry Liu #ifndef __xpv
1420a3114836SGerry Liu 	if (tsc_gethrtime_enable)
1421a3114836SGerry Liu 		tsc_sync_master(cpuid);
1422a3114836SGerry Liu #endif
1423a3114836SGerry Liu 
1424*df5a0c57SLuqman Aden 	/*
1425*df5a0c57SLuqman Aden 	 * At this point, the CPU in question is past the IDENT cpuid phase and
1426*df5a0c57SLuqman Aden 	 * grabbed the current microcode revision so we can now look for any
1427*df5a0c57SLuqman Aden 	 * relevant microcode updates it should load.  We'll fill out
1428*df5a0c57SLuqman Aden 	 * cpu_ucode_info for it along with the microcode to load, if any,
1429*df5a0c57SLuqman Aden 	 * before signaling back to the CPU to continue startup.
1430*df5a0c57SLuqman Aden 	 */
1431*df5a0c57SLuqman Aden 	mp_startup_wait(&procset_slave, cpuid);
1432*df5a0c57SLuqman Aden 	ucode_locate(cp);
1433*df5a0c57SLuqman Aden 	mp_startup_signal(&procset_master, cpuid);
1434*df5a0c57SLuqman Aden 
1435a3114836SGerry Liu 	if (dtrace_cpu_init != NULL) {
1436a3114836SGerry Liu 		(*dtrace_cpu_init)(cpuid);
1437a3114836SGerry Liu 	}
1438a3114836SGerry Liu 
1439a3114836SGerry Liu 	/*
1440a3114836SGerry Liu 	 * During CPU DR operations, the cpu_lock is held by current
1441a3114836SGerry Liu 	 * (the control) thread. We can't release the cpu_lock here
1442a3114836SGerry Liu 	 * because that will break the CPU DR logic.
1443a3114836SGerry Liu 	 * On the other hand, CPUPM and processor group initialization
1444a3114836SGerry Liu 	 * routines need to access the cpu_lock. So we invoke those
1445a3114836SGerry Liu 	 * routines here on behalf of mp_startup_common().
1446a3114836SGerry Liu 	 *
1447a3114836SGerry Liu 	 * CPUPM and processor group initialization routines depend
1448a3114836SGerry Liu 	 * on the cpuid probing results. Wait for mp_startup_common()
1449a3114836SGerry Liu 	 * to signal that cpuid probing is done.
1450a3114836SGerry Liu 	 */
1451a3114836SGerry Liu 	mp_startup_wait(&procset_slave, cpuid);
1452a3114836SGerry Liu #ifndef __xpv
1453a3114836SGerry Liu 	cpupm_init(cp);
1454a3114836SGerry Liu #endif
1455a3114836SGerry Liu 	(void) pg_cpu_init(cp, B_FALSE);
1456a3114836SGerry Liu 	cpu_set_state(cp);
1457a3114836SGerry Liu 	mp_startup_signal(&procset_master, cpuid);
1458a3114836SGerry Liu 
1459a3114836SGerry Liu 	return (0);
1460a3114836SGerry Liu }
14617c478bd9Sstevel@tonic-gate 
1462ae115bc7Smrj /*
1463ae115bc7Smrj  * Start a single cpu, assuming that the kernel context is available
1464ae115bc7Smrj  * to successfully start another cpu.
1465ae115bc7Smrj  *
1466ae115bc7Smrj  * (For example, real mode code is mapped into the right place
1467ae115bc7Smrj  * in memory and is ready to be run.)
1468ae115bc7Smrj  */
1469ae115bc7Smrj int
start_cpu(processorid_t who)1470ae115bc7Smrj start_cpu(processorid_t who)
1471ae115bc7Smrj {
1472ae115bc7Smrj 	cpu_t *cp;
1473ae115bc7Smrj 	int error = 0;
1474a3114836SGerry Liu 	cpuset_t tempset;
1475ae115bc7Smrj 
1476ae115bc7Smrj 	ASSERT(who != 0);
1477ae115bc7Smrj 
1478ae115bc7Smrj 	/*
1479ae115bc7Smrj 	 * Check if there's at least a Mbyte of kmem available
1480ae115bc7Smrj 	 * before attempting to start the cpu.
1481ae115bc7Smrj 	 */
1482ae115bc7Smrj 	if (kmem_avail() < 1024 * 1024) {
1483ae115bc7Smrj 		/*
1484ae115bc7Smrj 		 * Kick off a reap in case that helps us with
1485ae115bc7Smrj 		 * later attempts ..
1486ae115bc7Smrj 		 */
1487ae115bc7Smrj 		kmem_reap();
1488ae115bc7Smrj 		return (ENOMEM);
1489ae115bc7Smrj 	}
1490ae115bc7Smrj 
1491a3114836SGerry Liu 	/*
1492a3114836SGerry Liu 	 * First configure cpu.
1493a3114836SGerry Liu 	 */
1494a3114836SGerry Liu 	cp = mp_cpu_configure_common(who, B_TRUE);
1495a3114836SGerry Liu 	ASSERT(cp != NULL);
1496ae115bc7Smrj 
1497ae115bc7Smrj 	/*
1498a3114836SGerry Liu 	 * Then start cpu.
1499ae115bc7Smrj 	 */
1500a3114836SGerry Liu 	error = mp_start_cpu_common(cp, B_TRUE);
1501a3114836SGerry Liu 	if (error != 0) {
1502a3114836SGerry Liu 		mp_cpu_unconfigure_common(cp, error);
1503ae115bc7Smrj 		return (error);
1504ae115bc7Smrj 	}
1505ae115bc7Smrj 
1506ae115bc7Smrj 	mutex_exit(&cpu_lock);
1507a3114836SGerry Liu 	tempset = cpu_ready_set;
1508a3114836SGerry Liu 	while (!CPU_IN_SET(tempset, who)) {
1509a3114836SGerry Liu 		drv_usecwait(1);
1510a3114836SGerry Liu 		tempset = *((volatile cpuset_t *)&cpu_ready_set);
1511ae115bc7Smrj 	}
1512a3114836SGerry Liu 	mutex_enter(&cpu_lock);
1513ae115bc7Smrj 
1514ae115bc7Smrj 	return (0);
1515ae115bc7Smrj }
1516ae115bc7Smrj 
15177c478bd9Sstevel@tonic-gate void
start_other_cpus(int cprboot)15187c478bd9Sstevel@tonic-gate start_other_cpus(int cprboot)
15197c478bd9Sstevel@tonic-gate {
1520a3114836SGerry Liu 	_NOTE(ARGUNUSED(cprboot));
1521a3114836SGerry Liu 
1522ae115bc7Smrj 	uint_t who;
1523ae115bc7Smrj 	uint_t bootcpuid = 0;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	/*
15267c478bd9Sstevel@tonic-gate 	 * Initialize our own cpu_info.
15277c478bd9Sstevel@tonic-gate 	 */
15287c478bd9Sstevel@tonic-gate 	init_cpu_info(CPU);
15297c478bd9Sstevel@tonic-gate 
15302428aad8SPatrick Mooney #if !defined(__xpv)
15312428aad8SPatrick Mooney 	init_cpu_id_gdt(CPU);
15322428aad8SPatrick Mooney #endif
15332428aad8SPatrick Mooney 
153419397407SSherry Moore 	cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_idstr);
153519397407SSherry Moore 	cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_brandstr);
153619397407SSherry Moore 
15377c478bd9Sstevel@tonic-gate 	/*
153839df76fbSAndy Fiddaman 	 * KPTI initialisation happens very early in boot, before logging is
153939df76fbSAndy Fiddaman 	 * set up. Output a status message now as the boot CPU comes online.
154039df76fbSAndy Fiddaman 	 */
154139df76fbSAndy Fiddaman 	cmn_err(CE_CONT, "?KPTI %s (PCID %s, INVPCID %s)\n",
154239df76fbSAndy Fiddaman 	    kpti_enable ? "enabled" : "disabled",
154339df76fbSAndy Fiddaman 	    x86_use_pcid == 1 ? "in use" :
154439df76fbSAndy Fiddaman 	    (is_x86_feature(x86_featureset, X86FSET_PCID) ? "disabled" :
154539df76fbSAndy Fiddaman 	    "not supported"),
154639df76fbSAndy Fiddaman 	    x86_use_pcid == 1 && x86_use_invpcid == 1 ? "in use" :
154739df76fbSAndy Fiddaman 	    (is_x86_feature(x86_featureset, X86FSET_INVPCID) ? "disabled" :
154839df76fbSAndy Fiddaman 	    "not supported"));
154939df76fbSAndy Fiddaman 
155039df76fbSAndy Fiddaman 	/*
15517c478bd9Sstevel@tonic-gate 	 * Initialize our syscall handlers
15527c478bd9Sstevel@tonic-gate 	 */
15537c478bd9Sstevel@tonic-gate 	init_cpu_syscall(CPU);
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	/*
1556ae115bc7Smrj 	 * Take the boot cpu out of the mp_cpus set because we know
1557ae115bc7Smrj 	 * it's already running.  Add it to the cpu_ready_set for
1558ae115bc7Smrj 	 * precisely the same reason.
1559ae115bc7Smrj 	 */
1560ae115bc7Smrj 	CPUSET_DEL(mp_cpus, bootcpuid);
1561ae115bc7Smrj 	CPUSET_ADD(cpu_ready_set, bootcpuid);
1562ae115bc7Smrj 
1563ae115bc7Smrj 	/*
1564a3114836SGerry Liu 	 * skip the rest of this if
1565a3114836SGerry Liu 	 * . only 1 cpu dectected and system isn't hotplug-capable
1566a3114836SGerry Liu 	 * . not using MP
15677c478bd9Sstevel@tonic-gate 	 */
1568a3114836SGerry Liu 	if ((CPUSET_ISNULL(mp_cpus) && plat_dr_support_cpu() == 0) ||
1569a3114836SGerry Liu 	    use_mp == 0) {
15707c478bd9Sstevel@tonic-gate 		if (use_mp == 0)
15717c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
15727c478bd9Sstevel@tonic-gate 		goto done;
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	/*
15767c478bd9Sstevel@tonic-gate 	 * perform such initialization as is needed
15777c478bd9Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
15787c478bd9Sstevel@tonic-gate 	 */
15797c478bd9Sstevel@tonic-gate 	cpu_pause_init();
15807c478bd9Sstevel@tonic-gate 
1581f34a7178SJoe Bonasera 	xc_init_cpu(CPU);		/* initialize processor crosscalls */
15827c478bd9Sstevel@tonic-gate 
1583ae115bc7Smrj 	if (mach_cpucontext_init() != 0)
15847c478bd9Sstevel@tonic-gate 		goto done;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	flushes_require_xcalls = 1;
15877c478bd9Sstevel@tonic-gate 
15885205ae23Snf202958 	/*
15895205ae23Snf202958 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
15905205ae23Snf202958 	 * do their TSC syncs with the same CPU.
15915205ae23Snf202958 	 */
15927c478bd9Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
159541791439Sandrei 		if (!CPU_IN_SET(mp_cpus, who))
15967c478bd9Sstevel@tonic-gate 			continue;
1597ae115bc7Smrj 		ASSERT(who != bootcpuid);
1598b9e93c10SJonathan Haslam 
1599b9e93c10SJonathan Haslam 		mutex_enter(&cpu_lock);
1600a3114836SGerry Liu 		if (start_cpu(who) != 0)
1601a3114836SGerry Liu 			CPUSET_DEL(mp_cpus, who);
1602b9e93c10SJonathan Haslam 		cpu_state_change_notify(who, CPU_SETUP);
1603b9e93c10SJonathan Haslam 		mutex_exit(&cpu_lock);
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 
16062449e17fSsherrym 	/* Free the space allocated to hold the microcode file */
1607adc586deSMark Johnson 	ucode_cleanup();
16082449e17fSsherrym 
16097c478bd9Sstevel@tonic-gate 	affinity_clear();
16107c478bd9Sstevel@tonic-gate 
1611a3114836SGerry Liu 	mach_cpucontext_fini();
161241791439Sandrei 
16137c478bd9Sstevel@tonic-gate done:
1614ad09f8b8SMark Johnson 	if (get_hwenv() == HW_NATIVE)
16157c478bd9Sstevel@tonic-gate 		workaround_errata_end();
16163ad553a7Sgavinm 	cmi_post_mpstartup();
1617a3114836SGerry Liu 
1618761dea5eSPatrick Mooney #if !defined(__xpv)
1619761dea5eSPatrick Mooney 	/*
1620761dea5eSPatrick Mooney 	 * Once other CPUs have completed startup procedures, perform
1621761dea5eSPatrick Mooney 	 * initialization of hypervisor resources for HMA.
1622761dea5eSPatrick Mooney 	 */
1623761dea5eSPatrick Mooney 	hma_init();
1624761dea5eSPatrick Mooney #endif
1625761dea5eSPatrick Mooney 
1626a3114836SGerry Liu 	if (use_mp && ncpus != boot_max_ncpus) {
1627a3114836SGerry Liu 		cmn_err(CE_NOTE,
1628a3114836SGerry Liu 		    "System detected %d cpus, but "
1629a3114836SGerry Liu 		    "only %d cpu(s) were enabled during boot.",
1630a3114836SGerry Liu 		    boot_max_ncpus, ncpus);
1631a3114836SGerry Liu 		cmn_err(CE_NOTE,
1632a3114836SGerry Liu 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
1633bbf21555SRichard Lowe 		    "See eeprom(8).");
1634a3114836SGerry Liu 	}
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate int
mp_cpu_configure(int cpuid)16387c478bd9Sstevel@tonic-gate mp_cpu_configure(int cpuid)
16397c478bd9Sstevel@tonic-gate {
1640a3114836SGerry Liu 	cpu_t *cp;
1641a3114836SGerry Liu 
1642a3114836SGerry Liu 	if (use_mp == 0 || plat_dr_support_cpu() == 0) {
1643a3114836SGerry Liu 		return (ENOTSUP);
16447c478bd9Sstevel@tonic-gate 	}
16457c478bd9Sstevel@tonic-gate 
1646a3114836SGerry Liu 	cp = cpu_get(cpuid);
1647a3114836SGerry Liu 	if (cp != NULL) {
1648a3114836SGerry Liu 		return (EALREADY);
1649a3114836SGerry Liu 	}
1650a3114836SGerry Liu 
1651a3114836SGerry Liu 	/*
1652a3114836SGerry Liu 	 * Check if there's at least a Mbyte of kmem available
1653a3114836SGerry Liu 	 * before attempting to start the cpu.
1654a3114836SGerry Liu 	 */
1655a3114836SGerry Liu 	if (kmem_avail() < 1024 * 1024) {
1656a3114836SGerry Liu 		/*
1657a3114836SGerry Liu 		 * Kick off a reap in case that helps us with
1658a3114836SGerry Liu 		 * later attempts ..
1659a3114836SGerry Liu 		 */
1660a3114836SGerry Liu 		kmem_reap();
1661a3114836SGerry Liu 		return (ENOMEM);
1662a3114836SGerry Liu 	}
1663a3114836SGerry Liu 
1664a3114836SGerry Liu 	cp = mp_cpu_configure_common(cpuid, B_FALSE);
1665a3114836SGerry Liu 	ASSERT(cp != NULL && cpu_get(cpuid) == cp);
1666a3114836SGerry Liu 
1667a3114836SGerry Liu 	return (cp != NULL ? 0 : EAGAIN);
1668a3114836SGerry Liu }
1669a3114836SGerry Liu 
16707c478bd9Sstevel@tonic-gate int
mp_cpu_unconfigure(int cpuid)16717c478bd9Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
16727c478bd9Sstevel@tonic-gate {
1673a3114836SGerry Liu 	cpu_t *cp;
1674a3114836SGerry Liu 
1675a3114836SGerry Liu 	if (use_mp == 0 || plat_dr_support_cpu() == 0) {
1676a3114836SGerry Liu 		return (ENOTSUP);
1677a3114836SGerry Liu 	} else if (cpuid < 0 || cpuid >= max_ncpus) {
1678a3114836SGerry Liu 		return (EINVAL);
1679a3114836SGerry Liu 	}
1680a3114836SGerry Liu 
1681a3114836SGerry Liu 	cp = cpu_get(cpuid);
1682a3114836SGerry Liu 	if (cp == NULL) {
1683a3114836SGerry Liu 		return (ENODEV);
1684a3114836SGerry Liu 	}
1685a3114836SGerry Liu 	mp_cpu_unconfigure_common(cp, 0);
1686a3114836SGerry Liu 
1687a3114836SGerry Liu 	return (0);
16887c478bd9Sstevel@tonic-gate }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate /*
16917c478bd9Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
1692498697c5Sdmick  * Called from real_mode_start.
1693b4b46911Skchow  *
1694a3114836SGerry Liu  * WARNING: until CPU_READY is set, mp_startup_common and routines called by
1695a3114836SGerry Liu  * mp_startup_common should not call routines (e.g. kmem_free) that could call
1696b4b46911Skchow  * hat_unload which requires CPU_READY to be set.
16977c478bd9Sstevel@tonic-gate  */
1698a3114836SGerry Liu static void
mp_startup_common(boolean_t boot)1699a3114836SGerry Liu mp_startup_common(boolean_t boot)
17007c478bd9Sstevel@tonic-gate {
1701a3114836SGerry Liu 	cpu_t *cp = CPU;
1702dfea898aSKuriakose Kuruvilla 	uchar_t new_x86_featureset[BT_SIZEOFMAP(NUM_X86_FEATURES)];
1703a3114836SGerry Liu 	extern void cpu_event_init_cpu(cpu_t *);
17047c478bd9Sstevel@tonic-gate 
1705498697c5Sdmick 	/*
170624a74e86Sdmick 	 * We need to get TSC on this proc synced (i.e., any delta
170724a74e86Sdmick 	 * from cpu0 accounted for) as soon as we can, because many
170824a74e86Sdmick 	 * many things use gethrtime/pc_gethrestime, including
17094948216cSKeith M Wesolowski 	 * interrupts, cmn_err, etc.  Before we can do that, we want to
17104948216cSKeith M Wesolowski 	 * clear TSC if we're on a buggy Sandy/Ivy Bridge CPU, so do that
171162ce774cSKeith M Wesolowski 	 * right away.  Note that the TSC sync procedure run by
171262ce774cSKeith M Wesolowski 	 * tsc_sync_{master,slave} will not yield reliable results if caching is
171362ce774cSKeith M Wesolowski 	 * disabled on either CPU.  We rely on code in mpcore.S to guarantee
171462ce774cSKeith M Wesolowski 	 * that it is enabled before this function is called.  Caching has
171562ce774cSKeith M Wesolowski 	 * already been enabled on the BSP long before APs are started.
171624a74e86Sdmick 	 */
17174948216cSKeith M Wesolowski 	bzero(new_x86_featureset, BT_SIZEOFMAP(NUM_X86_FEATURES));
1718ab5bb018SKeith M Wesolowski 	cpuid_execpass(cp, CPUID_PASS_PRELUDE, new_x86_featureset);
1719ab5bb018SKeith M Wesolowski 	cpuid_execpass(cp, CPUID_PASS_IDENT, NULL);
17204948216cSKeith M Wesolowski 
1721*df5a0c57SLuqman Aden 	/*
1722*df5a0c57SLuqman Aden 	 * We want to apply any microcode updates before the BASIC cpuid pass,
1723*df5a0c57SLuqman Aden 	 * but as per the above comment, we want to make sure TSC is synced
1724*df5a0c57SLuqman Aden 	 * ASAP.  Thus we check for TSC support in the boot CPU's feature set
1725*df5a0c57SLuqman Aden 	 * instead -- this should be fine as we'd expect TSC support to be
1726*df5a0c57SLuqman Aden 	 * consistent across all CPUs (and certainly for the buggy CPUs we're
1727*df5a0c57SLuqman Aden 	 * concerned about here).
1728*df5a0c57SLuqman Aden 	 */
17294948216cSKeith M Wesolowski 	if (boot && get_hwenv() == HW_NATIVE &&
17304948216cSKeith M Wesolowski 	    cpuid_getvendor(CPU) == X86_VENDOR_Intel &&
17314948216cSKeith M Wesolowski 	    cpuid_getfamily(CPU) == 6 &&
17324948216cSKeith M Wesolowski 	    (cpuid_getmodel(CPU) == 0x2d || cpuid_getmodel(CPU) == 0x3e) &&
1733*df5a0c57SLuqman Aden 	    is_x86_feature(x86_featureset, X86FSET_TSC)) {
17344948216cSKeith M Wesolowski 		(void) wrmsr(REG_TSC, 0UL);
17354948216cSKeith M Wesolowski 	}
173624a74e86Sdmick 
1737a3114836SGerry Liu 	/* Let the control CPU continue into tsc_sync_master() */
1738a3114836SGerry Liu 	mp_startup_signal(&procset_slave, cp->cpu_id);
173924a74e86Sdmick 
1740843e1988Sjohnlev #ifndef __xpv
174124a74e86Sdmick 	if (tsc_gethrtime_enable)
174224a74e86Sdmick 		tsc_sync_slave();
1743843e1988Sjohnlev #endif
174424a74e86Sdmick 
174524a74e86Sdmick 	/*
1746*df5a0c57SLuqman Aden 	 * As with the boot CPU, we may have a more recent update compared to
1747*df5a0c57SLuqman Aden 	 * whatever the BIOS may have already applied.  If so, we want to apply
1748*df5a0c57SLuqman Aden 	 * it here before the BASIC cpuid pass so that any architecturally
1749*df5a0c57SLuqman Aden 	 * visible changes (e.g., changed MSR or CPUID bits) happen before we
1750*df5a0c57SLuqman Aden 	 * start querying the CPU for its capabilities.
1751*df5a0c57SLuqman Aden 	 *
1752*df5a0c57SLuqman Aden 	 * Since we're still in the early stages of bringing up this CPU, we're
1753*df5a0c57SLuqman Aden 	 * limited in what we can do (e.g., no kmem_alloc/free), so after
1754*df5a0c57SLuqman Aden 	 * reading the current microcode revision we have the control CPU do the
1755*df5a0c57SLuqman Aden 	 * work of locating the microcode file and setting up the cpu_ucode_info
1756*df5a0c57SLuqman Aden 	 * structure via ucode_locate().  With that done, we can apply the
1757*df5a0c57SLuqman Aden 	 * microcode to this CPU (if any) and proceed with the BASIC cpuid pass.
1758*df5a0c57SLuqman Aden 	 */
1759*df5a0c57SLuqman Aden 	ucode_read_rev(cp);
1760*df5a0c57SLuqman Aden 	mp_startup_signal(&procset_slave, cp->cpu_id);
1761*df5a0c57SLuqman Aden 	mp_startup_wait(&procset_master, cp->cpu_id);
1762*df5a0c57SLuqman Aden 	ucode_apply(cp);
1763*df5a0c57SLuqman Aden 	cpuid_execpass(cp, CPUID_PASS_BASIC, new_x86_featureset);
1764*df5a0c57SLuqman Aden 
1765*df5a0c57SLuqman Aden 	/*
1766498697c5Sdmick 	 * Once this was done from assembly, but it's safer here; if
1767498697c5Sdmick 	 * it blocks, we need to be able to swtch() to and from, and
1768498697c5Sdmick 	 * since we get here by calling t_pc, we need to do that call
1769498697c5Sdmick 	 * before swtch() overwrites it.
1770498697c5Sdmick 	 */
1771498697c5Sdmick 	(void) (*ap_mlsetup)();
1772498697c5Sdmick 
1773843e1988Sjohnlev #ifndef __xpv
17747c478bd9Sstevel@tonic-gate 	/*
17751d03c31eSjohnlev 	 * Program this cpu's PAT
17767c478bd9Sstevel@tonic-gate 	 */
17771d03c31eSjohnlev 	pat_sync();
1778843e1988Sjohnlev #endif
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	/*
1781ae115bc7Smrj 	 * Set up TSC_AUX to contain the cpuid for this processor
1782ae115bc7Smrj 	 * for the rdtscp instruction.
1783ae115bc7Smrj 	 */
1784*df5a0c57SLuqman Aden 	if (is_x86_feature(new_x86_featureset, X86FSET_TSCP))
1785ae115bc7Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
1786ae115bc7Smrj 
1787ae115bc7Smrj 	/*
17887c478bd9Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
17897c478bd9Sstevel@tonic-gate 	 */
17907c478bd9Sstevel@tonic-gate 	init_cpu_syscall(cp);
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	/*
17937c478bd9Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
17947c478bd9Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
17957c478bd9Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
17967c478bd9Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
17977c478bd9Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
17987c478bd9Sstevel@tonic-gate 	 * calls before CPU_READY is set.
17997c478bd9Sstevel@tonic-gate 	 */
1800ae115bc7Smrj 	splx(ipltospl(LOCK_LEVEL));
1801ae115bc7Smrj 	sti();
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	/*
18044d4b4953SPatrick Mooney 	 * There exists a small subset of systems which expose differing
18054d4b4953SPatrick Mooney 	 * MWAIT/MONITOR support between CPUs.  If MWAIT support is absent from
18064d4b4953SPatrick Mooney 	 * the boot CPU, but is found on a later CPU, the system continues to
18074d4b4953SPatrick Mooney 	 * operate as if no MWAIT support is available.
18084d4b4953SPatrick Mooney 	 *
18094d4b4953SPatrick Mooney 	 * The reverse case, where MWAIT is available on the boot CPU but not
18104d4b4953SPatrick Mooney 	 * on a subsequently initialized CPU, is not presently allowed and will
18114d4b4953SPatrick Mooney 	 * result in a panic.
1812f98fbcecSbholler 	 */
18137417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MWAIT) !=
18144d4b4953SPatrick Mooney 	    is_x86_feature(new_x86_featureset, X86FSET_MWAIT)) {
18154d4b4953SPatrick Mooney 		if (!is_x86_feature(x86_featureset, X86FSET_MWAIT)) {
18164d4b4953SPatrick Mooney 			remove_x86_feature(new_x86_featureset, X86FSET_MWAIT);
18174d4b4953SPatrick Mooney 		} else {
18184d4b4953SPatrick Mooney 			panic("unsupported mixed cpu mwait support detected");
18194d4b4953SPatrick Mooney 		}
18204d4b4953SPatrick Mooney 	}
1821f98fbcecSbholler 
1822f98fbcecSbholler 	/*
18237c478bd9Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
18247c478bd9Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
1825fb2caebeSRandy Fishel 	 * answer of dying horribly.  Provided the boot cpu is ok,
18267c478bd9Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
18277c478bd9Sstevel@tonic-gate 	 */
18287c478bd9Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
18297c478bd9Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
18307c478bd9Sstevel@tonic-gate 
1831a3114836SGerry Liu 	/*
1832a3114836SGerry Liu 	 * We can touch cpu_flags here without acquiring the cpu_lock here
1833a3114836SGerry Liu 	 * because the cpu_lock is held by the control CPU which is running
1834a3114836SGerry Liu 	 * mp_start_cpu_common().
1835a3114836SGerry Liu 	 * Need to clear CPU_QUIESCED flag before calling any function which
1836a3114836SGerry Liu 	 * may cause thread context switching, such as kmem_alloc() etc.
1837a3114836SGerry Liu 	 * The idle thread checks for CPU_QUIESCED flag and loops for ever if
1838a3114836SGerry Liu 	 * it's set. So the startup thread may have no chance to switch back
1839a3114836SGerry Liu 	 * again if it's switched away with CPU_QUIESCED set.
1840a3114836SGerry Liu 	 */
1841a3114836SGerry Liu 	cp->cpu_flags &= ~(CPU_POWEROFF | CPU_QUIESCED);
1842a3114836SGerry Liu 
184374ecdb51SJohn Levon 	enable_pcid();
184474ecdb51SJohn Levon 
18457af88ac7SKuriakose Kuruvilla 	/*
18467af88ac7SKuriakose Kuruvilla 	 * Setup this processor for XSAVE.
18477af88ac7SKuriakose Kuruvilla 	 */
18487af88ac7SKuriakose Kuruvilla 	if (fp_save_mech == FP_XSAVE) {
18497af88ac7SKuriakose Kuruvilla 		xsave_setup_msr(cp);
18507af88ac7SKuriakose Kuruvilla 	}
18517af88ac7SKuriakose Kuruvilla 
1852ab5bb018SKeith M Wesolowski 	cpuid_execpass(cp, CPUID_PASS_EXTENDED, NULL);
1853ab5bb018SKeith M Wesolowski 	cpuid_execpass(cp, CPUID_PASS_DYNAMIC, NULL);
1854ab5bb018SKeith M Wesolowski 	cpuid_execpass(cp, CPUID_PASS_RESOLVE, NULL);
18557c478bd9Sstevel@tonic-gate 
1856a3114836SGerry Liu 	/*
1857a3114836SGerry Liu 	 * Correct cpu_idstr and cpu_brandstr on target CPU after
1858ab5bb018SKeith M Wesolowski 	 * CPUID_PASS_DYNAMIC is done.
1859a3114836SGerry Liu 	 */
1860a3114836SGerry Liu 	(void) cpuid_getidstr(cp, cp->cpu_idstr, CPU_IDSTRLEN);
1861a3114836SGerry Liu 	(void) cpuid_getbrandstr(cp, cp->cpu_brandstr, CPU_IDSTRLEN);
18627c478bd9Sstevel@tonic-gate 
1863afbc4541Ssherrym 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_EXISTS;
18645205ae23Snf202958 
1865e774b42bSBill Holler 	post_startup_cpu_fixups();
1866e774b42bSBill Holler 
1867a3114836SGerry Liu 	cpu_event_init_cpu(cp);
1868a3114836SGerry Liu 
1869aa7b6435Ssethg 	/*
1870aa7b6435Ssethg 	 * Enable preemption here so that contention for any locks acquired
1871a3114836SGerry Liu 	 * later in mp_startup_common may be preempted if the thread owning
1872a3114836SGerry Liu 	 * those locks is continuously executing on other CPUs (for example,
1873a3114836SGerry Liu 	 * this CPU must be preemptible to allow other CPUs to pause it during
1874a3114836SGerry Liu 	 * their startup phases).  It's safe to enable preemption here because
1875a3114836SGerry Liu 	 * the CPU state is pretty-much fully constructed.
1876aa7b6435Ssethg 	 */
1877aa7b6435Ssethg 	curthread->t_preempt = 0;
1878aa7b6435Ssethg 
1879da43ceabSsethg 	/* The base spl should still be at LOCK LEVEL here */
1880da43ceabSsethg 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
1881da43ceabSsethg 	set_base_spl();		/* Restore the spl to its proper value */
1882da43ceabSsethg 
18830e751525SEric Saxe 	pghw_physid_create(cp);
1884a3114836SGerry Liu 	/*
1885a3114836SGerry Liu 	 * Delegate initialization tasks, which need to access the cpu_lock,
1886a3114836SGerry Liu 	 * to mp_start_cpu_common() because we can't acquire the cpu_lock here
1887a3114836SGerry Liu 	 * during CPU DR operations.
1888a3114836SGerry Liu 	 */
1889a3114836SGerry Liu 	mp_startup_signal(&procset_slave, cp->cpu_id);
1890a3114836SGerry Liu 	mp_startup_wait(&procset_master, cp->cpu_id);
18910e751525SEric Saxe 	pg_cmt_cpu_startup(cp);
1892a3114836SGerry Liu 
1893a3114836SGerry Liu 	if (boot) {
1894a3114836SGerry Liu 		mutex_enter(&cpu_lock);
1895a3114836SGerry Liu 		cp->cpu_flags &= ~CPU_OFFLINE;
1896a3114836SGerry Liu 		cpu_enable_intr(cp);
1897a3114836SGerry Liu 		cpu_add_active(cp);
18980e751525SEric Saxe 		mutex_exit(&cpu_lock);
1899a3114836SGerry Liu 	}
19000e751525SEric Saxe 
1901afbc4541Ssherrym 	/* Enable interrupts */
1902afbc4541Ssherrym 	(void) spl0();
19030e751525SEric Saxe 
1904a3114836SGerry Liu 	/*
1905*df5a0c57SLuqman Aden 	 * Clear the microcode update buffer allocated via ucode_locate(), if
1906*df5a0c57SLuqman Aden 	 * any, for this CPU.
1907a3114836SGerry Liu 	 */
1908*df5a0c57SLuqman Aden 	ucode_finish(cp);
190901add34aSRobert Mustacchi 
191001add34aSRobert Mustacchi 	/*
191101add34aSRobert Mustacchi 	 * Do a sanity check to make sure this new CPU is a sane thing
191201add34aSRobert Mustacchi 	 * to add to the collection of processors running this system.
191301add34aSRobert Mustacchi 	 *
191401add34aSRobert Mustacchi 	 * XXX	Clearly this needs to get more sophisticated, if x86
191501add34aSRobert Mustacchi 	 * systems start to get built out of heterogenous CPUs; as is
191601add34aSRobert Mustacchi 	 * likely to happen once the number of processors in a configuration
191701add34aSRobert Mustacchi 	 * gets large enough.
191801add34aSRobert Mustacchi 	 */
191901add34aSRobert Mustacchi 	if (compare_x86_featureset(x86_featureset, new_x86_featureset) ==
192001add34aSRobert Mustacchi 	    B_FALSE) {
192101add34aSRobert Mustacchi 		cmn_err(CE_CONT, "cpu%d: featureset\n", cp->cpu_id);
192201add34aSRobert Mustacchi 		print_x86_featureset(new_x86_featureset);
192301add34aSRobert Mustacchi 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
192401add34aSRobert Mustacchi 	}
1925afbc4541Ssherrym 
192620c794b3Sgavinm #ifndef __xpv
192720c794b3Sgavinm 	{
19287aec1d6eScindi 		/*
192920c794b3Sgavinm 		 * Set up the CPU module for this CPU.  This can't be done
193020c794b3Sgavinm 		 * before this CPU is made CPU_READY, because we may (in
193120c794b3Sgavinm 		 * heterogeneous systems) need to go load another CPU module.
193220c794b3Sgavinm 		 * The act of attempting to load a module may trigger a
193320c794b3Sgavinm 		 * cross-call, which will ASSERT unless this cpu is CPU_READY.
19347aec1d6eScindi 		 */
193520c794b3Sgavinm 		cmi_hdl_t hdl;
19367aec1d6eScindi 
193720c794b3Sgavinm 		if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
1938e4b86885SCheng Sean Ye 		    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
19397417cfdeSKuriakose Kuruvilla 			if (is_x86_feature(x86_featureset, X86FSET_MCA))
194020c794b3Sgavinm 				cmi_mca_init(hdl);
1941a3114836SGerry Liu 			cp->cpu_m.mcpu_cmi_hdl = hdl;
194220c794b3Sgavinm 		}
194320c794b3Sgavinm 	}
194420c794b3Sgavinm #endif /* __xpv */
19457aec1d6eScindi 
19467c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
1947ae115bc7Smrj 		kdi_cpu_init();
19487c478bd9Sstevel@tonic-gate 
1949af1da5d8SJason King 	(void) mach_cpu_create_device_node(cp, NULL);
1950af1da5d8SJason King 
19517c478bd9Sstevel@tonic-gate 	/*
19527c478bd9Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
19537c478bd9Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
19547c478bd9Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
19557c478bd9Sstevel@tonic-gate 	 */
19567c478bd9Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
19577c478bd9Sstevel@tonic-gate 
1958a3114836SGerry Liu 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
1959a3114836SGerry Liu 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
1960a3114836SGerry Liu 	cmn_err(CE_CONT, "?cpu%d initialization complete - online\n",
1961a3114836SGerry Liu 	    cp->cpu_id);
1962a3114836SGerry Liu 
19637c478bd9Sstevel@tonic-gate 	/*
19647c478bd9Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
19657c478bd9Sstevel@tonic-gate 	 */
19667c478bd9Sstevel@tonic-gate 	thread_exit();
19677c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
19687c478bd9Sstevel@tonic-gate }
19697c478bd9Sstevel@tonic-gate 
1970a3114836SGerry Liu /*
1971a3114836SGerry Liu  * Startup function for 'other' CPUs at boot time (besides boot cpu).
1972a3114836SGerry Liu  */
1973a3114836SGerry Liu static void
mp_startup_boot(void)1974a3114836SGerry Liu mp_startup_boot(void)
1975a3114836SGerry Liu {
1976a3114836SGerry Liu 	mp_startup_common(B_TRUE);
1977a3114836SGerry Liu }
1978a3114836SGerry Liu 
1979a3114836SGerry Liu /*
1980a3114836SGerry Liu  * Startup function for hotplug CPUs at runtime.
1981a3114836SGerry Liu  */
1982a3114836SGerry Liu void
mp_startup_hotplug(void)1983a3114836SGerry Liu mp_startup_hotplug(void)
1984a3114836SGerry Liu {
1985a3114836SGerry Liu 	mp_startup_common(B_FALSE);
1986a3114836SGerry Liu }
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate /*
19897c478bd9Sstevel@tonic-gate  * Start CPU on user request.
19907c478bd9Sstevel@tonic-gate  */
19917c478bd9Sstevel@tonic-gate /* ARGSUSED */
19927c478bd9Sstevel@tonic-gate int
mp_cpu_start(struct cpu * cp)19937c478bd9Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
19947c478bd9Sstevel@tonic-gate {
19957c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
19967c478bd9Sstevel@tonic-gate 	return (0);
19977c478bd9Sstevel@tonic-gate }
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate /*
20007c478bd9Sstevel@tonic-gate  * Stop CPU on user request.
20017c478bd9Sstevel@tonic-gate  */
20027c478bd9Sstevel@tonic-gate int
mp_cpu_stop(struct cpu * cp)20037c478bd9Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
20047c478bd9Sstevel@tonic-gate {
2005d90554ebSdmick 	extern int cbe_psm_timer_mode;
20067c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
2007d90554ebSdmick 
2008843e1988Sjohnlev #ifdef __xpv
2009843e1988Sjohnlev 	/*
2010843e1988Sjohnlev 	 * We can't offline vcpu0.
2011843e1988Sjohnlev 	 */
2012843e1988Sjohnlev 	if (cp->cpu_id == 0)
2013843e1988Sjohnlev 		return (EBUSY);
2014843e1988Sjohnlev #endif
2015843e1988Sjohnlev 
2016d90554ebSdmick 	/*
2017d90554ebSdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
2018d90554ebSdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
2019d90554ebSdmick 	 */
2020d90554ebSdmick 
2021d90554ebSdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
2022843e1988Sjohnlev 		return (EBUSY);
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	return (0);
20257c478bd9Sstevel@tonic-gate }
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate /*
20287c478bd9Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
2029c3377ee9SJohn Levon  *
2030c3377ee9SJohn Levon  * Usually, we hold cpu_lock. But we cannot assert as such due to the
2031c3377ee9SJohn Levon  * exception - i_cpr_save_context() - where we have mutual exclusion via a
2032c3377ee9SJohn Levon  * separate mechanism.
20337c478bd9Sstevel@tonic-gate  */
20347c478bd9Sstevel@tonic-gate int
cpu_disable_intr(struct cpu * cp)20357c478bd9Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
20367c478bd9Sstevel@tonic-gate {
20377c478bd9Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
20387c478bd9Sstevel@tonic-gate 		return (EBUSY);
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
2041c3377ee9SJohn Levon 	ncpus_intr_enabled--;
20427c478bd9Sstevel@tonic-gate 	return (0);
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate /*
20467c478bd9Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
20477c478bd9Sstevel@tonic-gate  */
20487c478bd9Sstevel@tonic-gate void
cpu_enable_intr(struct cpu * cp)20497c478bd9Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
20507c478bd9Sstevel@tonic-gate {
20517c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
20527c478bd9Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
2053c3377ee9SJohn Levon 	ncpus_intr_enabled++;
20547c478bd9Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
20557c478bd9Sstevel@tonic-gate }
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate void
mp_cpu_faulted_enter(struct cpu * cp)20587c478bd9Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
20597aec1d6eScindi {
2060a3114836SGerry Liu #ifdef __xpv
2061a3114836SGerry Liu 	_NOTE(ARGUNUSED(cp));
2062a3114836SGerry Liu #else
2063a3114836SGerry Liu 	cmi_hdl_t hdl = cp->cpu_m.mcpu_cmi_hdl;
206420c794b3Sgavinm 
206520c794b3Sgavinm 	if (hdl != NULL) {
2066a3114836SGerry Liu 		cmi_hdl_hold(hdl);
2067a3114836SGerry Liu 	} else {
2068a3114836SGerry Liu 		hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
2069a3114836SGerry Liu 		    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
2070a3114836SGerry Liu 	}
2071a3114836SGerry Liu 	if (hdl != NULL) {
207220c794b3Sgavinm 		cmi_faulted_enter(hdl);
207320c794b3Sgavinm 		cmi_hdl_rele(hdl);
207420c794b3Sgavinm 	}
207520c794b3Sgavinm #endif
20767aec1d6eScindi }
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate void
mp_cpu_faulted_exit(struct cpu * cp)20797c478bd9Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
20807aec1d6eScindi {
2081a3114836SGerry Liu #ifdef __xpv
2082a3114836SGerry Liu 	_NOTE(ARGUNUSED(cp));
2083a3114836SGerry Liu #else
2084a3114836SGerry Liu 	cmi_hdl_t hdl = cp->cpu_m.mcpu_cmi_hdl;
208520c794b3Sgavinm 
208620c794b3Sgavinm 	if (hdl != NULL) {
2087a3114836SGerry Liu 		cmi_hdl_hold(hdl);
2088a3114836SGerry Liu 	} else {
2089a3114836SGerry Liu 		hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
2090a3114836SGerry Liu 		    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
2091a3114836SGerry Liu 	}
2092a3114836SGerry Liu 	if (hdl != NULL) {
209320c794b3Sgavinm 		cmi_faulted_exit(hdl);
209420c794b3Sgavinm 		cmi_hdl_rele(hdl);
209520c794b3Sgavinm 	}
209620c794b3Sgavinm #endif
20977aec1d6eScindi }
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate /*
21007c478bd9Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
21017c478bd9Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
21027c478bd9Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
21037417cfdeSKuriakose Kuruvilla  * organization rather than speed.  We could avoid checking x86_featureset at
21047417cfdeSKuriakose Kuruvilla  * every context switch by installing different context ops, depending on
21057417cfdeSKuriakose Kuruvilla  * x86_featureset, at LDT creation time -- one for each combination of fast
21067417cfdeSKuriakose Kuruvilla  * syscall features.
21077c478bd9Sstevel@tonic-gate  */
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate void
cpu_fast_syscall_disable(void)2110a0955b86SJohn Levon cpu_fast_syscall_disable(void)
21117c478bd9Sstevel@tonic-gate {
21127417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
21137417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_SEP))
21147c478bd9Sstevel@tonic-gate 		cpu_sep_disable();
21157417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
21167417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_ASYSC))
21177c478bd9Sstevel@tonic-gate 		cpu_asysc_disable();
21187c478bd9Sstevel@tonic-gate }
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate void
cpu_fast_syscall_enable(void)2121a0955b86SJohn Levon cpu_fast_syscall_enable(void)
21227c478bd9Sstevel@tonic-gate {
21237417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
21247417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_SEP))
21257c478bd9Sstevel@tonic-gate 		cpu_sep_enable();
21267417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_MSR) &&
21277417cfdeSKuriakose Kuruvilla 	    is_x86_feature(x86_featureset, X86FSET_ASYSC))
21287c478bd9Sstevel@tonic-gate 		cpu_asysc_enable();
21297c478bd9Sstevel@tonic-gate }
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate static void
cpu_sep_enable(void)21327c478bd9Sstevel@tonic-gate cpu_sep_enable(void)
21337c478bd9Sstevel@tonic-gate {
21347417cfdeSKuriakose Kuruvilla 	ASSERT(is_x86_feature(x86_featureset, X86FSET_SEP));
21357c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
21367c478bd9Sstevel@tonic-gate 
21370ac7d7d8Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
21388515d723SPatrick Mooney 
21398515d723SPatrick Mooney 	CPU->cpu_m.mcpu_fast_syscall_state |= FSS_SEP_ENABLED;
21407c478bd9Sstevel@tonic-gate }
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate static void
cpu_sep_disable(void)21437c478bd9Sstevel@tonic-gate cpu_sep_disable(void)
21447c478bd9Sstevel@tonic-gate {
21457417cfdeSKuriakose Kuruvilla 	ASSERT(is_x86_feature(x86_featureset, X86FSET_SEP));
21467c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 	/*
21497c478bd9Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
21507c478bd9Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
21517c478bd9Sstevel@tonic-gate 	 */
2152ae115bc7Smrj 	wrmsr(MSR_INTC_SEP_CS, 0);
21538515d723SPatrick Mooney 
21548515d723SPatrick Mooney 	CPU->cpu_m.mcpu_fast_syscall_state &= ~FSS_SEP_ENABLED;
21557c478bd9Sstevel@tonic-gate }
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate static void
cpu_asysc_enable(void)21587c478bd9Sstevel@tonic-gate cpu_asysc_enable(void)
21597c478bd9Sstevel@tonic-gate {
21607417cfdeSKuriakose Kuruvilla 	ASSERT(is_x86_feature(x86_featureset, X86FSET_ASYSC));
21617c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
21627c478bd9Sstevel@tonic-gate 
21630ac7d7d8Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
21640ac7d7d8Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
21658515d723SPatrick Mooney 
21668515d723SPatrick Mooney 	CPU->cpu_m.mcpu_fast_syscall_state |= FSS_ASYSC_ENABLED;
21677c478bd9Sstevel@tonic-gate }
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate static void
cpu_asysc_disable(void)21707c478bd9Sstevel@tonic-gate cpu_asysc_disable(void)
21717c478bd9Sstevel@tonic-gate {
21727417cfdeSKuriakose Kuruvilla 	ASSERT(is_x86_feature(x86_featureset, X86FSET_ASYSC));
21737c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	/*
21767c478bd9Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
21777c478bd9Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
21787c478bd9Sstevel@tonic-gate 	 */
21790ac7d7d8Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
21800ac7d7d8Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
21818515d723SPatrick Mooney 
21828515d723SPatrick Mooney 	CPU->cpu_m.mcpu_fast_syscall_state &= ~FSS_ASYSC_ENABLED;
21837c478bd9Sstevel@tonic-gate }
2184