xref: /onnv-gate/usr/src/uts/i86pc/os/mp_startup.c (revision 8803:8c01b39012c9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51455Sandrei  * Common Development and Distribution License (the "License").
61455Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213446Smrj 
220Sstevel@tonic-gate /*
23*8803SJonathan.Haslam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/thread.h>
290Sstevel@tonic-gate #include <sys/cpuvar.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/disp.h>
340Sstevel@tonic-gate #include <sys/class.h>
350Sstevel@tonic-gate #include <sys/cmn_err.h>
360Sstevel@tonic-gate #include <sys/debug.h>
370Sstevel@tonic-gate #include <sys/asm_linkage.h>
380Sstevel@tonic-gate #include <sys/x_call.h>
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/var.h>
410Sstevel@tonic-gate #include <sys/vtrace.h>
420Sstevel@tonic-gate #include <vm/hat.h>
430Sstevel@tonic-gate #include <vm/as.h>
440Sstevel@tonic-gate #include <vm/seg_kmem.h>
453446Smrj #include <vm/seg_kp.h>
460Sstevel@tonic-gate #include <sys/segments.h>
470Sstevel@tonic-gate #include <sys/kmem.h>
480Sstevel@tonic-gate #include <sys/stack.h>
490Sstevel@tonic-gate #include <sys/smp_impldefs.h>
500Sstevel@tonic-gate #include <sys/x86_archext.h>
510Sstevel@tonic-gate #include <sys/machsystm.h>
520Sstevel@tonic-gate #include <sys/traptrace.h>
530Sstevel@tonic-gate #include <sys/clock.h>
540Sstevel@tonic-gate #include <sys/cpc_impl.h>
553434Sesaxe #include <sys/pg.h>
563434Sesaxe #include <sys/cmt.h>
570Sstevel@tonic-gate #include <sys/dtrace.h>
580Sstevel@tonic-gate #include <sys/archsystm.h>
590Sstevel@tonic-gate #include <sys/fp.h>
600Sstevel@tonic-gate #include <sys/reboot.h>
613446Smrj #include <sys/kdi_machimpl.h>
620Sstevel@tonic-gate #include <vm/hat_i86.h>
630Sstevel@tonic-gate #include <sys/memnode.h>
64938Sesaxe #include <sys/pci_cfgspace.h>
653446Smrj #include <sys/mach_mmu.h>
663446Smrj #include <sys/sysmacros.h>
675084Sjohnlev #if defined(__xpv)
685084Sjohnlev #include <sys/hypervisor.h>
695084Sjohnlev #endif
701414Scindi #include <sys/cpu_module.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate struct cpu	cpus[1];			/* CPU data */
730Sstevel@tonic-gate struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
740Sstevel@tonic-gate cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
773446Smrj  * Useful for disabling MP bring-up on a MP capable system.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate int use_mp = 1;
800Sstevel@tonic-gate 
812006Sandrei /*
823446Smrj  * to be set by a PSM to indicate what cpus
833446Smrj  * are sitting around on the system.
842006Sandrei  */
853446Smrj cpuset_t mp_cpus;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * This variable is used by the hat layer to decide whether or not
890Sstevel@tonic-gate  * critical sections are needed to prevent race conditions.  For sun4m,
900Sstevel@tonic-gate  * this variable is set once enough MP initialization has been done in
910Sstevel@tonic-gate  * order to allow cross calls.
920Sstevel@tonic-gate  */
933446Smrj int flushes_require_xcalls;
946336Sbholler 
956336Sbholler cpuset_t cpu_ready_set;		/* initialized in startup() */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static 	void	mp_startup(void);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static void cpu_sep_enable(void);
1000Sstevel@tonic-gate static void cpu_sep_disable(void);
1010Sstevel@tonic-gate static void cpu_asysc_enable(void);
1020Sstevel@tonic-gate static void cpu_asysc_disable(void);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * Init CPU info - get CPU type info for processor_info system call.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate void
1080Sstevel@tonic-gate init_cpu_info(struct cpu *cp)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	processor_info_t *pi = &cp->cpu_type_info;
1110Sstevel@tonic-gate 	char buf[CPU_IDSTRLEN];
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * Get clock-frequency property for the CPU.
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	pi->pi_clock = cpu_freq;
1170Sstevel@tonic-gate 
1184667Smh27603 	/*
1194667Smh27603 	 * Current frequency in Hz.
1204667Smh27603 	 */
1214718Smh27603 	cp->cpu_curr_clock = cpu_freq_hz;
1224667Smh27603 
1234877Smh27603 	/*
1244877Smh27603 	 * Supported frequencies.
1254877Smh27603 	 */
1264877Smh27603 	cpu_set_supp_freqs(cp, NULL);
1274877Smh27603 
1280Sstevel@tonic-gate 	(void) strcpy(pi->pi_processor_type, "i386");
1290Sstevel@tonic-gate 	if (fpu_exists)
1300Sstevel@tonic-gate 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	(void) cpuid_getidstr(cp, buf, sizeof (buf));
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1350Sstevel@tonic-gate 	(void) strcpy(cp->cpu_idstr, buf);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	(void) cpuid_getbrandstr(cp, buf, sizeof (buf));
1380Sstevel@tonic-gate 	cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
1390Sstevel@tonic-gate 	(void) strcpy(cp->cpu_brandstr, buf);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * Configure syscall support on this CPU.
1440Sstevel@tonic-gate  */
1450Sstevel@tonic-gate /*ARGSUSED*/
1465295Srandyf void
1470Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	kpreempt_disable();
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate #if defined(__amd64)
1523446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC)) {
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #if !defined(__lint)
1550Sstevel@tonic-gate 		/*
1560Sstevel@tonic-gate 		 * The syscall instruction imposes a certain ordering on
1570Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1580Sstevel@tonic-gate 		 * here.
1590Sstevel@tonic-gate 		 */
1600Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
1610Sstevel@tonic-gate 		ASSERT(UDS_SEL == U32CS_SEL + 8);
1620Sstevel@tonic-gate 		ASSERT(UCS_SEL == U32CS_SEL + 16);
1630Sstevel@tonic-gate #endif
1640Sstevel@tonic-gate 		/*
1650Sstevel@tonic-gate 		 * Turn syscall/sysret extensions on.
1660Sstevel@tonic-gate 		 */
1670Sstevel@tonic-gate 		cpu_asysc_enable();
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		/*
1700Sstevel@tonic-gate 		 * Program the magic registers ..
1710Sstevel@tonic-gate 		 */
1723446Smrj 		wrmsr(MSR_AMD_STAR,
1733446Smrj 		    ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
174770Skucharsk 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
175770Skucharsk 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		/*
1780Sstevel@tonic-gate 		 * This list of flags is masked off the incoming
1790Sstevel@tonic-gate 		 * %rfl when we enter the kernel.
1800Sstevel@tonic-gate 		 */
181770Skucharsk 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate #endif
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
1870Sstevel@tonic-gate 	 * hard to use syscall/sysret, and it is more portable anyway.
1880Sstevel@tonic-gate 	 *
1890Sstevel@tonic-gate 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
1900Sstevel@tonic-gate 	 * variant isn't available to 32-bit applications, but sysenter is.
1910Sstevel@tonic-gate 	 */
1923446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP)) {
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate #if !defined(__lint)
1950Sstevel@tonic-gate 		/*
1960Sstevel@tonic-gate 		 * The sysenter instruction imposes a certain ordering on
1970Sstevel@tonic-gate 		 * segment selectors, so we double-check that ordering
1980Sstevel@tonic-gate 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
1990Sstevel@tonic-gate 		 * Intel Architecture Software Developer's Manual Volume 2:
2000Sstevel@tonic-gate 		 * Instruction Set Reference"
2010Sstevel@tonic-gate 		 */
2020Sstevel@tonic-gate 		ASSERT(KDS_SEL == KCS_SEL + 8);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
2050Sstevel@tonic-gate 		ASSERT32(UDS_SEL == UCS_SEL + 8);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
2080Sstevel@tonic-gate 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
2090Sstevel@tonic-gate #endif
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		cpu_sep_enable();
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		/*
2140Sstevel@tonic-gate 		 * resume() sets this value to the base of the threads stack
2150Sstevel@tonic-gate 		 * via a context handler.
2160Sstevel@tonic-gate 		 */
2173446Smrj 		wrmsr(MSR_INTC_SEP_ESP, 0);
218770Skucharsk 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	kpreempt_enable();
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate  * Multiprocessor initialization.
2260Sstevel@tonic-gate  *
2270Sstevel@tonic-gate  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
2280Sstevel@tonic-gate  * startup and idle threads for the specified CPU.
2290Sstevel@tonic-gate  */
2303446Smrj struct cpu *
2310Sstevel@tonic-gate mp_startup_init(int cpun)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	struct cpu *cp;
2340Sstevel@tonic-gate 	kthread_id_t tp;
2350Sstevel@tonic-gate 	caddr_t	sp;
2360Sstevel@tonic-gate 	proc_t *procp;
2375084Sjohnlev #if !defined(__xpv)
2385045Sbholler 	extern int idle_cpu_prefer_mwait;
2395084Sjohnlev #endif
2400Sstevel@tonic-gate 	extern void idle();
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate #ifdef TRAPTRACE
2430Sstevel@tonic-gate 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
2440Sstevel@tonic-gate #endif
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
2470Sstevel@tonic-gate 
2483446Smrj 	cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
2495084Sjohnlev #if !defined(__xpv)
2505045Sbholler 	if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait)
2515045Sbholler 		cp->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
2525084Sjohnlev #endif
2534481Sbholler 
2540Sstevel@tonic-gate 	procp = curthread->t_procp;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * Initialize the dispatcher first.
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	disp_cpu_init(cp);
2610Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2620Sstevel@tonic-gate 
263414Skchow 	cpu_vm_data_init(cp);
264414Skchow 
2650Sstevel@tonic-gate 	/*
2660Sstevel@tonic-gate 	 * Allocate and initialize the startup thread for this CPU.
2670Sstevel@tonic-gate 	 * Interrupt and process switch stacks get allocated later
2680Sstevel@tonic-gate 	 * when the CPU starts running.
2690Sstevel@tonic-gate 	 */
2700Sstevel@tonic-gate 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
2710Sstevel@tonic-gate 	    TS_STOPPED, maxclsyspri);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	/*
2740Sstevel@tonic-gate 	 * Set state to TS_ONPROC since this thread will start running
2750Sstevel@tonic-gate 	 * as soon as the CPU comes online.
2760Sstevel@tonic-gate 	 *
2770Sstevel@tonic-gate 	 * All the other fields of the thread structure are setup by
2780Sstevel@tonic-gate 	 * thread_create().
2790Sstevel@tonic-gate 	 */
2800Sstevel@tonic-gate 	THREAD_ONPROC(tp, cp);
2810Sstevel@tonic-gate 	tp->t_preempt = 1;
2820Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
2830Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
2840Sstevel@tonic-gate 	tp->t_cpu = cp;
2850Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/*
2880Sstevel@tonic-gate 	 * Setup thread to start in mp_startup.
2890Sstevel@tonic-gate 	 */
2900Sstevel@tonic-gate 	sp = tp->t_stk;
2910Sstevel@tonic-gate 	tp->t_pc = (uintptr_t)mp_startup;
2920Sstevel@tonic-gate 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
2933446Smrj #if defined(__amd64)
2943446Smrj 	tp->t_sp -= STACK_ENTRY_ALIGN;		/* fake a call */
2953446Smrj #endif
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	cp->cpu_id = cpun;
2980Sstevel@tonic-gate 	cp->cpu_self = cp;
2990Sstevel@tonic-gate 	cp->cpu_thread = tp;
3000Sstevel@tonic-gate 	cp->cpu_lwp = NULL;
3010Sstevel@tonic-gate 	cp->cpu_dispthread = tp;
3020Sstevel@tonic-gate 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/*
3051482Ssethg 	 * cpu_base_spl must be set explicitly here to prevent any blocking
3061482Ssethg 	 * operations in mp_startup from causing the spl of the cpu to drop
3071482Ssethg 	 * to 0 (allowing device interrupts before we're ready) in resume().
3081482Ssethg 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
3091482Ssethg 	 * As an extra bit of security on DEBUG kernels, this is enforced with
3101482Ssethg 	 * an assertion in mp_startup() -- before cpu_base_spl is set to its
3111482Ssethg 	 * proper value.
3121482Ssethg 	 */
3131482Ssethg 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
3141482Ssethg 
3151482Ssethg 	/*
3160Sstevel@tonic-gate 	 * Now, initialize per-CPU idle thread for this CPU.
3170Sstevel@tonic-gate 	 */
3180Sstevel@tonic-gate 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	cp->cpu_idle_thread = tp;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	tp->t_preempt = 1;
3230Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
3240Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
3250Sstevel@tonic-gate 	tp->t_cpu = cp;
3260Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	/*
3293434Sesaxe 	 * Bootstrap the CPU's PG data
33060Sesaxe 	 */
3313434Sesaxe 	pg_cpu_bootstrap(cp);
33260Sesaxe 
33360Sesaxe 	/*
3343446Smrj 	 * Perform CPC initialization on the new CPU.
3350Sstevel@tonic-gate 	 */
3360Sstevel@tonic-gate 	kcpc_hw_init(cp);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	/*
3390Sstevel@tonic-gate 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
3400Sstevel@tonic-gate 	 * for each CPU.
3410Sstevel@tonic-gate 	 */
3420Sstevel@tonic-gate 	setup_vaddr_for_ppcopy(cp);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/*
3453446Smrj 	 * Allocate page for new GDT and initialize from current GDT.
3460Sstevel@tonic-gate 	 */
3473446Smrj #if !defined(__lint)
3483446Smrj 	ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
3493446Smrj #endif
3505460Sjosephb 	cp->cpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
3515460Sjosephb 	bcopy(CPU->cpu_gdt, cp->cpu_gdt, (sizeof (*cp->cpu_gdt) * NGDT));
3521626Srab 
3533446Smrj #if defined(__i386)
3540Sstevel@tonic-gate 	/*
3550Sstevel@tonic-gate 	 * setup kernel %gs.
3560Sstevel@tonic-gate 	 */
3570Sstevel@tonic-gate 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
3580Sstevel@tonic-gate 	    SEL_KPL, 0, 1);
3593446Smrj #endif
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * If we have more than one node, each cpu gets a copy of IDT
3630Sstevel@tonic-gate 	 * local to its node. If this is a Pentium box, we use cpu 0's
3640Sstevel@tonic-gate 	 * IDT. cpu 0's IDT has been made read-only to workaround the
3650Sstevel@tonic-gate 	 * cmpxchgl register bug
3660Sstevel@tonic-gate 	 */
3670Sstevel@tonic-gate 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
3685460Sjosephb #if !defined(__lint)
3695460Sjosephb 		ASSERT((sizeof (*CPU->cpu_idt) * NIDT) <= PAGESIZE);
3705460Sjosephb #endif
3715460Sjosephb 		cp->cpu_idt = kmem_zalloc(PAGESIZE, KM_SLEEP);
3725460Sjosephb 		bcopy(CPU->cpu_idt, cp->cpu_idt, PAGESIZE);
3733446Smrj 	} else {
3745460Sjosephb 		cp->cpu_idt = CPU->cpu_idt;
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	/*
3783446Smrj 	 * Get interrupt priority data from cpu 0.
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	cp->cpu_pri_data = CPU->cpu_pri_data;
3810Sstevel@tonic-gate 
3823446Smrj 	/*
3833446Smrj 	 * alloc space for cpuid info
3843446Smrj 	 */
3853446Smrj 	cpuid_alloc_space(cp);
3863446Smrj 
3874581Ssherrym 	/*
3884581Ssherrym 	 * alloc space for ucode_info
3894581Ssherrym 	 */
3904581Ssherrym 	ucode_alloc_space(cp);
3914581Ssherrym 
3920Sstevel@tonic-gate 	hat_cpu_online(cp);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate #ifdef TRAPTRACE
3950Sstevel@tonic-gate 	/*
3963446Smrj 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers
3970Sstevel@tonic-gate 	 */
3980Sstevel@tonic-gate 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
3990Sstevel@tonic-gate 	ttc->ttc_next = ttc->ttc_first;
4000Sstevel@tonic-gate 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
4010Sstevel@tonic-gate #endif
4020Sstevel@tonic-gate 	/*
4030Sstevel@tonic-gate 	 * Record that we have another CPU.
4040Sstevel@tonic-gate 	 */
4050Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * Initialize the interrupt threads for this CPU
4080Sstevel@tonic-gate 	 */
4091455Sandrei 	cpu_intr_alloc(cp, NINTR_THREADS);
4100Sstevel@tonic-gate 	/*
4110Sstevel@tonic-gate 	 * Add CPU to list of available CPUs.  It'll be on the active list
4120Sstevel@tonic-gate 	 * after mp_startup().
4130Sstevel@tonic-gate 	 */
4140Sstevel@tonic-gate 	cpu_add_unit(cp);
4150Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
4163446Smrj 
4173446Smrj 	return (cp);
4183446Smrj }
4193446Smrj 
4203446Smrj /*
4213446Smrj  * Undo what was done in mp_startup_init
4223446Smrj  */
4233446Smrj static void
4243446Smrj mp_startup_fini(struct cpu *cp, int error)
4253446Smrj {
4263446Smrj 	mutex_enter(&cpu_lock);
4273446Smrj 
4283446Smrj 	/*
4293446Smrj 	 * Remove the CPU from the list of available CPUs.
4303446Smrj 	 */
4313446Smrj 	cpu_del_unit(cp->cpu_id);
4323446Smrj 
4333446Smrj 	if (error == ETIMEDOUT) {
4343446Smrj 		/*
4353446Smrj 		 * The cpu was started, but never *seemed* to run any
4363446Smrj 		 * code in the kernel; it's probably off spinning in its
4373446Smrj 		 * own private world, though with potential references to
4383446Smrj 		 * our kmem-allocated IDTs and GDTs (for example).
4393446Smrj 		 *
4403446Smrj 		 * Worse still, it may actually wake up some time later,
4413446Smrj 		 * so rather than guess what it might or might not do, we
4423446Smrj 		 * leave the fundamental data structures intact.
4433446Smrj 		 */
4443446Smrj 		cp->cpu_flags = 0;
4453446Smrj 		mutex_exit(&cpu_lock);
4463446Smrj 		return;
4473446Smrj 	}
4483446Smrj 
4493446Smrj 	/*
4503446Smrj 	 * At this point, the only threads bound to this CPU should
4513446Smrj 	 * special per-cpu threads: it's idle thread, it's pause threads,
4523446Smrj 	 * and it's interrupt threads.  Clean these up.
4533446Smrj 	 */
4543446Smrj 	cpu_destroy_bound_threads(cp);
4553446Smrj 	cp->cpu_idle_thread = NULL;
4563446Smrj 
4573446Smrj 	/*
4583446Smrj 	 * Free the interrupt stack.
4593446Smrj 	 */
4603446Smrj 	segkp_release(segkp,
4613446Smrj 	    cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
4623446Smrj 
4633446Smrj 	mutex_exit(&cpu_lock);
4643446Smrj 
4653446Smrj #ifdef TRAPTRACE
4663446Smrj 	/*
4673446Smrj 	 * Discard the trap trace buffer
4683446Smrj 	 */
4693446Smrj 	{
4703446Smrj 		trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
4713446Smrj 
4723446Smrj 		kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
4733446Smrj 		ttc->ttc_first = NULL;
4743446Smrj 	}
4753446Smrj #endif
4763446Smrj 
4773446Smrj 	hat_cpu_offline(cp);
4783446Smrj 
4793446Smrj 	cpuid_free_space(cp);
4803446Smrj 
4814581Ssherrym 	ucode_free_space(cp);
4824581Ssherrym 
4835460Sjosephb 	if (cp->cpu_idt != CPU->cpu_idt)
4845460Sjosephb 		kmem_free(cp->cpu_idt, PAGESIZE);
4855460Sjosephb 	cp->cpu_idt = NULL;
4863446Smrj 
4875460Sjosephb 	kmem_free(cp->cpu_gdt, PAGESIZE);
4885460Sjosephb 	cp->cpu_gdt = NULL;
4893446Smrj 
4903446Smrj 	teardown_vaddr_for_ppcopy(cp);
4913446Smrj 
4923446Smrj 	kcpc_hw_fini(cp);
4933446Smrj 
4943446Smrj 	cp->cpu_dispthread = NULL;
4953446Smrj 	cp->cpu_thread = NULL;	/* discarded by cpu_destroy_bound_threads() */
4963446Smrj 
4973446Smrj 	cpu_vm_data_destroy(cp);
4983446Smrj 
4993446Smrj 	mutex_enter(&cpu_lock);
5003446Smrj 	disp_cpu_fini(cp);
5013446Smrj 	mutex_exit(&cpu_lock);
5023446Smrj 
5035084Sjohnlev #if !defined(__xpv)
5045045Sbholler 	if (cp->cpu_m.mcpu_mwait != NULL)
5055045Sbholler 		cpuid_mwait_free(cp);
5065084Sjohnlev #endif
5073446Smrj 	kmem_free(cp, sizeof (*cp));
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate /*
5110Sstevel@tonic-gate  * Apply workarounds for known errata, and warn about those that are absent.
5120Sstevel@tonic-gate  *
5130Sstevel@tonic-gate  * System vendors occasionally create configurations which contain different
5140Sstevel@tonic-gate  * revisions of the CPUs that are almost but not exactly the same.  At the
5150Sstevel@tonic-gate  * time of writing, this meant that their clock rates were the same, their
5160Sstevel@tonic-gate  * feature sets were the same, but the required workaround were -not-
5170Sstevel@tonic-gate  * necessarily the same.  So, this routine is invoked on -every- CPU soon
5180Sstevel@tonic-gate  * after starting to make sure that the resulting system contains the most
5190Sstevel@tonic-gate  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
5200Sstevel@tonic-gate  * system.
5210Sstevel@tonic-gate  *
522938Sesaxe  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
523938Sesaxe  * mp_startup() for all slave CPUs. Slaves process workaround_errata prior
524938Sesaxe  * to acknowledging their readiness to the master, so this routine will
525938Sesaxe  * never be executed by multiple CPUs in parallel, thus making updates to
526938Sesaxe  * global data safe.
527938Sesaxe  *
528359Skucharsk  * These workarounds are based on Rev 3.57 of the Revision Guide for
529359Skucharsk  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
5300Sstevel@tonic-gate  */
5310Sstevel@tonic-gate 
5323446Smrj #if defined(OPTERON_ERRATUM_88)
5333446Smrj int opteron_erratum_88;		/* if non-zero -> at least one cpu has it */
5343446Smrj #endif
5353446Smrj 
5360Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
5370Sstevel@tonic-gate int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
5380Sstevel@tonic-gate #endif
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
5410Sstevel@tonic-gate int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
5420Sstevel@tonic-gate #endif
5430Sstevel@tonic-gate 
5443446Smrj #if defined(OPTERON_ERRATUM_95)
5453446Smrj int opteron_erratum_95;		/* if non-zero -> at least one cpu has it */
5463446Smrj #endif
5473446Smrj 
5480Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
5490Sstevel@tonic-gate int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
5500Sstevel@tonic-gate #endif
5510Sstevel@tonic-gate 
5523446Smrj #if defined(OPTERON_ERRATUM_108)
5533446Smrj int opteron_erratum_108;	/* if non-zero -> at least one cpu has it */
5543446Smrj #endif
5553446Smrj 
5560Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
5570Sstevel@tonic-gate int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
5580Sstevel@tonic-gate #endif
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
5610Sstevel@tonic-gate int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
5620Sstevel@tonic-gate #endif
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
5650Sstevel@tonic-gate int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
5660Sstevel@tonic-gate #endif
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
5690Sstevel@tonic-gate int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
5700Sstevel@tonic-gate #endif
5710Sstevel@tonic-gate 
572359Skucharsk #if defined(OPTERON_ERRATUM_131)
573359Skucharsk int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
574359Skucharsk #endif
5750Sstevel@tonic-gate 
576938Sesaxe #if defined(OPTERON_WORKAROUND_6336786)
577938Sesaxe int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
578938Sesaxe int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
579938Sesaxe #endif
580938Sesaxe 
5811582Skchow #if defined(OPTERON_WORKAROUND_6323525)
5821582Skchow int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
5831582Skchow #endif
5841582Skchow 
5856691Skchow #if defined(OPTERON_ERRATUM_298)
5866691Skchow int opteron_erratum_298;
5876691Skchow #endif
5886691Skchow 
5893446Smrj static void
5903446Smrj workaround_warning(cpu_t *cp, uint_t erratum)
5913446Smrj {
5923446Smrj 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
5933446Smrj 	    cp->cpu_id, erratum);
5943446Smrj }
5953446Smrj 
5963446Smrj static void
5973446Smrj workaround_applied(uint_t erratum)
5983446Smrj {
5993446Smrj 	if (erratum > 1000000)
6003446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
6013446Smrj 		    erratum);
6023446Smrj 	else
6033446Smrj 		cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
6043446Smrj 		    erratum);
6053446Smrj }
6063446Smrj 
6073446Smrj static void
6083446Smrj msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
6093446Smrj {
6103446Smrj 	cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
6113446Smrj 	    cp->cpu_id, rw, msr, error);
6123446Smrj }
6130Sstevel@tonic-gate 
6145893Sesaxe /*
6158232SEric.Saxe@Sun.COM  * Determine the number of nodes in a Hammer / Greyhound / Griffin family
6168232SEric.Saxe@Sun.COM  * system.
6175893Sesaxe  */
6185893Sesaxe static uint_t
6195893Sesaxe opteron_get_nnodes(void)
6205893Sesaxe {
6215893Sesaxe 	static uint_t nnodes = 0;
6225893Sesaxe 
6238232SEric.Saxe@Sun.COM 	if (nnodes == 0) {
6245893Sesaxe #ifdef	DEBUG
6258232SEric.Saxe@Sun.COM 		uint_t family;
6265893Sesaxe 
6278232SEric.Saxe@Sun.COM 		/*
6288232SEric.Saxe@Sun.COM 		 * This routine uses a PCI config space based mechanism
6298232SEric.Saxe@Sun.COM 		 * for retrieving the number of nodes in the system.
6308232SEric.Saxe@Sun.COM 		 * Device 24, function 0, offset 0x60 as used here is not
6318232SEric.Saxe@Sun.COM 		 * AMD processor architectural, and may not work on processor
6328232SEric.Saxe@Sun.COM 		 * families other than those listed below.
6338232SEric.Saxe@Sun.COM 		 *
6348232SEric.Saxe@Sun.COM 		 * Callers of this routine must ensure that we're running on
6358232SEric.Saxe@Sun.COM 		 * a processor which supports this mechanism.
6368232SEric.Saxe@Sun.COM 		 * The assertion below is meant to catch calls on unsupported
6378232SEric.Saxe@Sun.COM 		 * processors.
6388232SEric.Saxe@Sun.COM 		 */
6398232SEric.Saxe@Sun.COM 		family = cpuid_getfamily(CPU);
6408232SEric.Saxe@Sun.COM 		ASSERT(family == 0xf || family == 0x10 || family == 0x11);
6415893Sesaxe #endif	/* DEBUG */
6425893Sesaxe 
6435893Sesaxe 		/*
6445893Sesaxe 		 * Obtain the number of nodes in the system from
6455893Sesaxe 		 * bits [6:4] of the Node ID register on node 0.
6465893Sesaxe 		 *
6475893Sesaxe 		 * The actual node count is NodeID[6:4] + 1
6485893Sesaxe 		 *
6495893Sesaxe 		 * The Node ID register is accessed via function 0,
6505893Sesaxe 		 * offset 0x60. Node 0 is device 24.
6515893Sesaxe 		 */
6525893Sesaxe 		nnodes = ((pci_getl_func(0, 24, 0, 0x60) & 0x70) >> 4) + 1;
6535893Sesaxe 	}
6545893Sesaxe 	return (nnodes);
6555893Sesaxe }
6565893Sesaxe 
6575084Sjohnlev #if defined(__xpv)
6585084Sjohnlev 
6595084Sjohnlev /*
6605084Sjohnlev  * On dom0, we can determine the number of physical cpus on the machine.
6615084Sjohnlev  * This number is important when figuring out what workarounds are
6625084Sjohnlev  * appropriate, so compute it now.
6635084Sjohnlev  */
6646670Stariq uint_t
6655084Sjohnlev xen_get_nphyscpus(void)
6665084Sjohnlev {
6675084Sjohnlev 	static uint_t nphyscpus = 0;
6685084Sjohnlev 
6695084Sjohnlev 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
6705084Sjohnlev 
6715084Sjohnlev 	if (nphyscpus == 0) {
6725084Sjohnlev 		xen_sysctl_t op;
6735084Sjohnlev 		xen_sysctl_physinfo_t *pi = &op.u.physinfo;
6745084Sjohnlev 
6755084Sjohnlev 		op.cmd = XEN_SYSCTL_physinfo;
6765084Sjohnlev 		op.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
6775084Sjohnlev 		if (HYPERVISOR_sysctl(&op) == 0)
6785084Sjohnlev 			nphyscpus = pi->threads_per_core *
6795084Sjohnlev 			    pi->cores_per_socket * pi->sockets_per_node *
6805084Sjohnlev 			    pi->nr_nodes;
6815084Sjohnlev 	}
6825084Sjohnlev 	return (nphyscpus);
6835084Sjohnlev }
6845084Sjohnlev #endif
6855084Sjohnlev 
6860Sstevel@tonic-gate uint_t
6876691Skchow do_erratum_298(struct cpu *cpu)
6886691Skchow {
6896691Skchow 	static int	osvwrc = -3;
6906691Skchow 	extern int	osvw_opteron_erratum(cpu_t *, uint_t);
6916691Skchow 
6926691Skchow 	/*
6936691Skchow 	 * L2 Eviction May Occur During Processor Operation To Set
6946691Skchow 	 * Accessed or Dirty Bit.
6956691Skchow 	 */
6966691Skchow 	if (osvwrc == -3) {
6976691Skchow 		osvwrc = osvw_opteron_erratum(cpu, 298);
6986691Skchow 	} else {
6996691Skchow 		/* osvw return codes should be consistent for all cpus */
7006691Skchow 		ASSERT(osvwrc == osvw_opteron_erratum(cpu, 298));
7016691Skchow 	}
7026691Skchow 
7036691Skchow 	switch (osvwrc) {
7046691Skchow 	case 0:		/* erratum is not present: do nothing */
7056691Skchow 		break;
7066691Skchow 	case 1:		/* erratum is present: BIOS workaround applied */
7076691Skchow 		/*
7086691Skchow 		 * check if workaround is actually in place and issue warning
7096691Skchow 		 * if not.
7106691Skchow 		 */
7116691Skchow 		if (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
7126691Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0)) {
7136691Skchow #if defined(OPTERON_ERRATUM_298)
7146691Skchow 			opteron_erratum_298++;
7156691Skchow #else
7166691Skchow 			workaround_warning(cpu, 298);
7176691Skchow 			return (1);
7186691Skchow #endif
7196691Skchow 		}
7206691Skchow 		break;
7216691Skchow 	case -1:	/* cannot determine via osvw: check cpuid */
7226691Skchow 		if ((cpuid_opteron_erratum(cpu, 298) > 0) &&
7236691Skchow 		    (((rdmsr(MSR_AMD_HWCR) & AMD_HWCR_TLBCACHEDIS) == 0) ||
7246691Skchow 		    ((rdmsr(MSR_AMD_BU_CFG) & AMD_BU_CFG_E298) == 0))) {
7256691Skchow #if defined(OPTERON_ERRATUM_298)
7266691Skchow 			opteron_erratum_298++;
7276691Skchow #else
7286691Skchow 			workaround_warning(cpu, 298);
7296691Skchow 			return (1);
7306691Skchow #endif
7316691Skchow 		}
7326691Skchow 		break;
7336691Skchow 	}
7346691Skchow 	return (0);
7356691Skchow }
7366691Skchow 
7376691Skchow uint_t
7380Sstevel@tonic-gate workaround_errata(struct cpu *cpu)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	uint_t missing = 0;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	ASSERT(cpu == CPU);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/*LINTED*/
7450Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
7460Sstevel@tonic-gate 		/*
7470Sstevel@tonic-gate 		 * SWAPGS May Fail To Read Correct GS Base
7480Sstevel@tonic-gate 		 */
7490Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88)
7500Sstevel@tonic-gate 		/*
7510Sstevel@tonic-gate 		 * The workaround is an mfence in the relevant assembler code
7520Sstevel@tonic-gate 		 */
7533446Smrj 		opteron_erratum_88++;
7540Sstevel@tonic-gate #else
7553446Smrj 		workaround_warning(cpu, 88);
7560Sstevel@tonic-gate 		missing++;
7570Sstevel@tonic-gate #endif
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
7610Sstevel@tonic-gate 		/*
7620Sstevel@tonic-gate 		 * Software Prefetches May Report A Page Fault
7630Sstevel@tonic-gate 		 */
7640Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91)
7650Sstevel@tonic-gate 		/*
7660Sstevel@tonic-gate 		 * fix is in trap.c
7670Sstevel@tonic-gate 		 */
7680Sstevel@tonic-gate 		opteron_erratum_91++;
7690Sstevel@tonic-gate #else
7703446Smrj 		workaround_warning(cpu, 91);
7710Sstevel@tonic-gate 		missing++;
7720Sstevel@tonic-gate #endif
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
7760Sstevel@tonic-gate 		/*
7770Sstevel@tonic-gate 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
7780Sstevel@tonic-gate 		 */
7790Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93)
7800Sstevel@tonic-gate 		/*
7810Sstevel@tonic-gate 		 * fix is in trap.c
7820Sstevel@tonic-gate 		 */
7830Sstevel@tonic-gate 		opteron_erratum_93++;
7840Sstevel@tonic-gate #else
7853446Smrj 		workaround_warning(cpu, 93);
7860Sstevel@tonic-gate 		missing++;
7870Sstevel@tonic-gate #endif
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	/*LINTED*/
7910Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
7920Sstevel@tonic-gate 		/*
7930Sstevel@tonic-gate 		 * RET Instruction May Return to Incorrect EIP
7940Sstevel@tonic-gate 		 */
7950Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95)
7960Sstevel@tonic-gate #if defined(_LP64)
7970Sstevel@tonic-gate 		/*
7980Sstevel@tonic-gate 		 * Workaround this by ensuring that 32-bit user code and
7990Sstevel@tonic-gate 		 * 64-bit kernel code never occupy the same address
8000Sstevel@tonic-gate 		 * range mod 4G.
8010Sstevel@tonic-gate 		 */
8020Sstevel@tonic-gate 		if (_userlimit32 > 0xc0000000ul)
8030Sstevel@tonic-gate 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 		/*LINTED*/
8060Sstevel@tonic-gate 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
8073446Smrj 		opteron_erratum_95++;
8080Sstevel@tonic-gate #endif	/* _LP64 */
8090Sstevel@tonic-gate #else
8103446Smrj 		workaround_warning(cpu, 95);
8110Sstevel@tonic-gate 		missing++;
8123446Smrj #endif
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
8160Sstevel@tonic-gate 		/*
8170Sstevel@tonic-gate 		 * Compatibility Mode Branches Transfer to Illegal Address
8180Sstevel@tonic-gate 		 */
8190Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100)
8200Sstevel@tonic-gate 		/*
8210Sstevel@tonic-gate 		 * fix is in trap.c
8220Sstevel@tonic-gate 		 */
8230Sstevel@tonic-gate 		opteron_erratum_100++;
8240Sstevel@tonic-gate #else
8253446Smrj 		workaround_warning(cpu, 100);
8260Sstevel@tonic-gate 		missing++;
8270Sstevel@tonic-gate #endif
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	/*LINTED*/
8310Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
8320Sstevel@tonic-gate 		/*
8330Sstevel@tonic-gate 		 * CPUID Instruction May Return Incorrect Model Number In
8340Sstevel@tonic-gate 		 * Some Processors
8350Sstevel@tonic-gate 		 */
8360Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108)
8370Sstevel@tonic-gate 		/*
8380Sstevel@tonic-gate 		 * (Our cpuid-handling code corrects the model number on
8390Sstevel@tonic-gate 		 * those processors)
8400Sstevel@tonic-gate 		 */
8410Sstevel@tonic-gate #else
8423446Smrj 		workaround_warning(cpu, 108);
8430Sstevel@tonic-gate 		missing++;
8440Sstevel@tonic-gate #endif
8450Sstevel@tonic-gate 	}
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	/*LINTED*/
8483446Smrj 	if (cpuid_opteron_erratum(cpu, 109) > 0) do {
8490Sstevel@tonic-gate 		/*
8500Sstevel@tonic-gate 		 * Certain Reverse REP MOVS May Produce Unpredictable Behaviour
8510Sstevel@tonic-gate 		 */
8520Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
8533446Smrj 		/*
8543446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
8553446Smrj 		 */
8563446Smrj 		uint64_t value;
8573446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
8583446Smrj 		int err;
8590Sstevel@tonic-gate 
8603446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
8613446Smrj 			msr_warning(cpu, "rd", msr, err);
8623446Smrj 			workaround_warning(cpu, 109);
8633446Smrj 			missing++;
8643446Smrj 		}
8653446Smrj 		if (value == 0)
8660Sstevel@tonic-gate 			opteron_erratum_109++;
8670Sstevel@tonic-gate #else
8683446Smrj 		workaround_warning(cpu, 109);
8690Sstevel@tonic-gate 		missing++;
8700Sstevel@tonic-gate #endif
8713446Smrj 	/*CONSTANTCONDITION*/
8723446Smrj 	} while (0);
8733446Smrj 
8740Sstevel@tonic-gate 	/*LINTED*/
8750Sstevel@tonic-gate 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
8760Sstevel@tonic-gate 		/*
8770Sstevel@tonic-gate 		 * Sequential Execution Across Non_Canonical Boundary Caused
8780Sstevel@tonic-gate 		 * Processor Hang
8790Sstevel@tonic-gate 		 */
8800Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
8813446Smrj #if defined(_LP64)
8820Sstevel@tonic-gate 		/*
8830Sstevel@tonic-gate 		 * Erratum 121 is only present in long (64 bit) mode.
8840Sstevel@tonic-gate 		 * Workaround is to include the page immediately before the
8850Sstevel@tonic-gate 		 * va hole to eliminate the possibility of system hangs due to
8860Sstevel@tonic-gate 		 * sequential execution across the va hole boundary.
8870Sstevel@tonic-gate 		 */
8883446Smrj 		if (opteron_erratum_121)
8893446Smrj 			opteron_erratum_121++;
8903446Smrj 		else {
8913446Smrj 			if (hole_start) {
8923446Smrj 				hole_start -= PAGESIZE;
8933446Smrj 			} else {
8943446Smrj 				/*
8953446Smrj 				 * hole_start not yet initialized by
8963446Smrj 				 * mmu_init. Initialize hole_start
8973446Smrj 				 * with value to be subtracted.
8983446Smrj 				 */
8993446Smrj 				hole_start = PAGESIZE;
9000Sstevel@tonic-gate 			}
9013446Smrj 			opteron_erratum_121++;
9020Sstevel@tonic-gate 		}
9033446Smrj #endif	/* _LP64 */
9040Sstevel@tonic-gate #else
9053446Smrj 		workaround_warning(cpu, 121);
9060Sstevel@tonic-gate 		missing++;
9070Sstevel@tonic-gate #endif
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/*LINTED*/
9113446Smrj 	if (cpuid_opteron_erratum(cpu, 122) > 0) do {
9120Sstevel@tonic-gate 		/*
9133446Smrj 		 * TLB Flush Filter May Cause Coherency Problem in
9140Sstevel@tonic-gate 		 * Multiprocessor Systems
9150Sstevel@tonic-gate 		 */
9160Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122)
9173446Smrj 		uint64_t value;
9183446Smrj 		const uint_t msr = MSR_AMD_HWCR;
9193446Smrj 		int error;
9203446Smrj 
9210Sstevel@tonic-gate 		/*
9220Sstevel@tonic-gate 		 * Erratum 122 is only present in MP configurations (multi-core
9230Sstevel@tonic-gate 		 * or multi-processor).
9240Sstevel@tonic-gate 		 */
9255084Sjohnlev #if defined(__xpv)
9265084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
9275084Sjohnlev 			break;
9285084Sjohnlev 		if (!opteron_erratum_122 && xen_get_nphyscpus() == 1)
9295084Sjohnlev 			break;
9305084Sjohnlev #else
9315893Sesaxe 		if (!opteron_erratum_122 && opteron_get_nnodes() == 1 &&
9323446Smrj 		    cpuid_get_ncpu_per_chip(cpu) == 1)
9333446Smrj 			break;
9345084Sjohnlev #endif
9353446Smrj 		/* disable TLB Flush Filter */
9363446Smrj 
9373446Smrj 		if ((error = checked_rdmsr(msr, &value)) != 0) {
9383446Smrj 			msr_warning(cpu, "rd", msr, error);
9393446Smrj 			workaround_warning(cpu, 122);
9403446Smrj 			missing++;
9413446Smrj 		} else {
9423446Smrj 			value |= (uint64_t)AMD_HWCR_FFDIS;
9433446Smrj 			if ((error = checked_wrmsr(msr, value)) != 0) {
9443446Smrj 				msr_warning(cpu, "wr", msr, error);
9453446Smrj 				workaround_warning(cpu, 122);
9463446Smrj 				missing++;
9473446Smrj 			}
9480Sstevel@tonic-gate 		}
9493446Smrj 		opteron_erratum_122++;
9500Sstevel@tonic-gate #else
9513446Smrj 		workaround_warning(cpu, 122);
9520Sstevel@tonic-gate 		missing++;
9530Sstevel@tonic-gate #endif
9543446Smrj 	/*CONSTANTCONDITION*/
9553446Smrj 	} while (0);
956302Skchow 
9570Sstevel@tonic-gate 	/*LINTED*/
9583446Smrj 	if (cpuid_opteron_erratum(cpu, 123) > 0) do {
9590Sstevel@tonic-gate 		/*
9600Sstevel@tonic-gate 		 * Bypassed Reads May Cause Data Corruption of System Hang in
9610Sstevel@tonic-gate 		 * Dual Core Processors
9620Sstevel@tonic-gate 		 */
9633446Smrj #if defined(OPTERON_ERRATUM_123)
9643446Smrj 		uint64_t value;
9653446Smrj 		const uint_t msr = MSR_AMD_PATCHLEVEL;
9663446Smrj 		int err;
9673446Smrj 
9680Sstevel@tonic-gate 		/*
9690Sstevel@tonic-gate 		 * Erratum 123 applies only to multi-core cpus.
9700Sstevel@tonic-gate 		 */
9713446Smrj 		if (cpuid_get_ncpu_per_chip(cpu) < 2)
9723446Smrj 			break;
9735084Sjohnlev #if defined(__xpv)
9745084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
9755084Sjohnlev 			break;
9765084Sjohnlev #endif
9773446Smrj 		/*
9783446Smrj 		 * The "workaround" is to print a warning to upgrade the BIOS
9793446Smrj 		 */
9803446Smrj 		if ((err = checked_rdmsr(msr, &value)) != 0) {
9813446Smrj 			msr_warning(cpu, "rd", msr, err);
9823446Smrj 			workaround_warning(cpu, 123);
9833446Smrj 			missing++;
9840Sstevel@tonic-gate 		}
9853446Smrj 		if (value == 0)
9863446Smrj 			opteron_erratum_123++;
9873446Smrj #else
9883446Smrj 		workaround_warning(cpu, 123);
9893446Smrj 		missing++;
990359Skucharsk 
9913446Smrj #endif
9923446Smrj 	/*CONSTANTCONDITION*/
9933446Smrj 	} while (0);
9943446Smrj 
995359Skucharsk 	/*LINTED*/
9963446Smrj 	if (cpuid_opteron_erratum(cpu, 131) > 0) do {
997359Skucharsk 		/*
998359Skucharsk 		 * Multiprocessor Systems with Four or More Cores May Deadlock
999359Skucharsk 		 * Waiting for a Probe Response
1000359Skucharsk 		 */
10013446Smrj #if defined(OPTERON_ERRATUM_131)
10023446Smrj 		uint64_t nbcfg;
10033446Smrj 		const uint_t msr = MSR_AMD_NB_CFG;
10043446Smrj 		const uint64_t wabits =
10053446Smrj 		    AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
10063446Smrj 		int error;
10073446Smrj 
1008359Skucharsk 		/*
1009359Skucharsk 		 * Erratum 131 applies to any system with four or more cores.
1010359Skucharsk 		 */
10113446Smrj 		if (opteron_erratum_131)
10123446Smrj 			break;
10135084Sjohnlev #if defined(__xpv)
10145084Sjohnlev 		if (!DOMAIN_IS_INITDOMAIN(xen_info))
10155084Sjohnlev 			break;
10165084Sjohnlev 		if (xen_get_nphyscpus() < 4)
10175084Sjohnlev 			break;
10185084Sjohnlev #else
10195893Sesaxe 		if (opteron_get_nnodes() * cpuid_get_ncpu_per_chip(cpu) < 4)
10203446Smrj 			break;
10215084Sjohnlev #endif
10223446Smrj 		/*
10233446Smrj 		 * Print a warning if neither of the workarounds for
10243446Smrj 		 * erratum 131 is present.
10253446Smrj 		 */
10263446Smrj 		if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
10273446Smrj 			msr_warning(cpu, "rd", msr, error);
10283446Smrj 			workaround_warning(cpu, 131);
10293446Smrj 			missing++;
10303446Smrj 		} else if ((nbcfg & wabits) == 0) {
10313446Smrj 			opteron_erratum_131++;
10323446Smrj 		} else {
10333446Smrj 			/* cannot have both workarounds set */
10343446Smrj 			ASSERT((nbcfg & wabits) != wabits);
1035359Skucharsk 		}
10363446Smrj #else
10373446Smrj 		workaround_warning(cpu, 131);
10383446Smrj 		missing++;
1039359Skucharsk #endif
10403446Smrj 	/*CONSTANTCONDITION*/
10413446Smrj 	} while (0);
1042938Sesaxe 
1043938Sesaxe 	/*
10443446Smrj 	 * This isn't really an erratum, but for convenience the
1045938Sesaxe 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
1046938Sesaxe 	 */
1047938Sesaxe 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
10483446Smrj #if defined(OPTERON_WORKAROUND_6336786)
1049938Sesaxe 		/*
1050938Sesaxe 		 * Disable C1-Clock ramping on multi-core/multi-processor
1051938Sesaxe 		 * K8 platforms to guard against TSC drift.
1052938Sesaxe 		 */
1053938Sesaxe 		if (opteron_workaround_6336786) {
1054938Sesaxe 			opteron_workaround_6336786++;
10555084Sjohnlev #if defined(__xpv)
10565084Sjohnlev 		} else if ((DOMAIN_IS_INITDOMAIN(xen_info) &&
10575084Sjohnlev 		    xen_get_nphyscpus() > 1) ||
10585084Sjohnlev 		    opteron_workaround_6336786_UP) {
10595084Sjohnlev 			/*
10605893Sesaxe 			 * XXPV	Hmm.  We can't walk the Northbridges on
10615084Sjohnlev 			 *	the hypervisor; so just complain and drive
10625084Sjohnlev 			 *	on.  This probably needs to be fixed in
10635084Sjohnlev 			 *	the hypervisor itself.
10645084Sjohnlev 			 */
10655084Sjohnlev 			opteron_workaround_6336786++;
10665084Sjohnlev 			workaround_warning(cpu, 6336786);
10675084Sjohnlev #else	/* __xpv */
10685893Sesaxe 		} else if ((opteron_get_nnodes() *
10695894Sesaxe 		    cpuid_get_ncpu_per_chip(cpu) > 1) ||
1070938Sesaxe 		    opteron_workaround_6336786_UP) {
10715893Sesaxe 
10725893Sesaxe 			uint_t	node, nnodes;
10733446Smrj 			uint8_t data;
10743446Smrj 
10755893Sesaxe 			nnodes = opteron_get_nnodes();
10765893Sesaxe 			for (node = 0; node < nnodes; node++) {
1077938Sesaxe 				/*
1078938Sesaxe 				 * Clear PMM7[1:0] (function 3, offset 0x87)
1079938Sesaxe 				 * Northbridge device is the node id + 24.
1080938Sesaxe 				 */
1081938Sesaxe 				data = pci_getb_func(0, node + 24, 3, 0x87);
1082938Sesaxe 				data &= 0xFC;
1083938Sesaxe 				pci_putb_func(0, node + 24, 3, 0x87, data);
1084938Sesaxe 			}
1085938Sesaxe 			opteron_workaround_6336786++;
10865084Sjohnlev #endif	/* __xpv */
1087938Sesaxe 		}
10883446Smrj #else
10893446Smrj 		workaround_warning(cpu, 6336786);
10903446Smrj 		missing++;
1091938Sesaxe #endif
10923446Smrj 	}
10931582Skchow 
10941582Skchow 	/*LINTED*/
10951582Skchow 	/*
10961582Skchow 	 * Mutex primitives don't work as expected.
10971582Skchow 	 */
10981582Skchow 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
10993446Smrj #if defined(OPTERON_WORKAROUND_6323525)
11001582Skchow 		/*
11013446Smrj 		 * This problem only occurs with 2 or more cores. If bit in
11026691Skchow 		 * MSR_AMD_BU_CFG set, then not applicable. The workaround
11031582Skchow 		 * is to patch the semaphone routines with the lfence
11041582Skchow 		 * instruction to provide necessary load memory barrier with
11051582Skchow 		 * possible subsequent read-modify-write ops.
11061582Skchow 		 *
11071582Skchow 		 * It is too early in boot to call the patch routine so
11081582Skchow 		 * set erratum variable to be done in startup_end().
11091582Skchow 		 */
11101582Skchow 		if (opteron_workaround_6323525) {
11111582Skchow 			opteron_workaround_6323525++;
11125084Sjohnlev #if defined(__xpv)
11135084Sjohnlev 		} else if (x86_feature & X86_SSE2) {
11145084Sjohnlev 			if (DOMAIN_IS_INITDOMAIN(xen_info)) {
11155084Sjohnlev 				/*
11165084Sjohnlev 				 * XXPV	Use dom0_msr here when extended
11175084Sjohnlev 				 *	operations are supported?
11185084Sjohnlev 				 */
11195084Sjohnlev 				if (xen_get_nphyscpus() > 1)
11205084Sjohnlev 					opteron_workaround_6323525++;
11215084Sjohnlev 			} else {
11225084Sjohnlev 				/*
11235084Sjohnlev 				 * We have no way to tell how many physical
11245084Sjohnlev 				 * cpus there are, or even if this processor
11255084Sjohnlev 				 * has the problem, so enable the workaround
11265084Sjohnlev 				 * unconditionally (at some performance cost).
11275084Sjohnlev 				 */
11285084Sjohnlev 				opteron_workaround_6323525++;
11295084Sjohnlev 			}
11305084Sjohnlev #else	/* __xpv */
11315893Sesaxe 		} else if ((x86_feature & X86_SSE2) && ((opteron_get_nnodes() *
11323446Smrj 		    cpuid_get_ncpu_per_chip(cpu)) > 1)) {
11338374SKit.Chow@Sun.COM 			if ((xrdmsr(MSR_AMD_BU_CFG) & (UINT64_C(1) << 33)) == 0)
11341582Skchow 				opteron_workaround_6323525++;
11355084Sjohnlev #endif	/* __xpv */
11361582Skchow 		}
11373446Smrj #else
11383446Smrj 		workaround_warning(cpu, 6323525);
11393446Smrj 		missing++;
11403446Smrj #endif
11411582Skchow 	}
11423446Smrj 
11436691Skchow 	missing += do_erratum_298(cpu);
11446691Skchow 
11455084Sjohnlev #ifdef __xpv
11465084Sjohnlev 	return (0);
11475084Sjohnlev #else
11480Sstevel@tonic-gate 	return (missing);
11495084Sjohnlev #endif
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate void
11530Sstevel@tonic-gate workaround_errata_end()
11540Sstevel@tonic-gate {
11553446Smrj #if defined(OPTERON_ERRATUM_88)
11563446Smrj 	if (opteron_erratum_88)
11573446Smrj 		workaround_applied(88);
11583446Smrj #endif
11593446Smrj #if defined(OPTERON_ERRATUM_91)
11603446Smrj 	if (opteron_erratum_91)
11613446Smrj 		workaround_applied(91);
11623446Smrj #endif
11633446Smrj #if defined(OPTERON_ERRATUM_93)
11643446Smrj 	if (opteron_erratum_93)
11653446Smrj 		workaround_applied(93);
11663446Smrj #endif
11673446Smrj #if defined(OPTERON_ERRATUM_95)
11683446Smrj 	if (opteron_erratum_95)
11693446Smrj 		workaround_applied(95);
11703446Smrj #endif
11713446Smrj #if defined(OPTERON_ERRATUM_100)
11723446Smrj 	if (opteron_erratum_100)
11733446Smrj 		workaround_applied(100);
11743446Smrj #endif
11753446Smrj #if defined(OPTERON_ERRATUM_108)
11763446Smrj 	if (opteron_erratum_108)
11773446Smrj 		workaround_applied(108);
11783446Smrj #endif
11790Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109)
11800Sstevel@tonic-gate 	if (opteron_erratum_109) {
1181359Skucharsk 		cmn_err(CE_WARN,
1182359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1183359Skucharsk 		    " processor\nerratum 109 was not detected; updating your"
1184359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1185359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1186359Skucharsk 		    " system\noperation may occur.\n");
11870Sstevel@tonic-gate 	}
11883446Smrj #endif
11893446Smrj #if defined(OPTERON_ERRATUM_121)
11903446Smrj 	if (opteron_erratum_121)
11913446Smrj 		workaround_applied(121);
11923446Smrj #endif
11933446Smrj #if defined(OPTERON_ERRATUM_122)
11943446Smrj 	if (opteron_erratum_122)
11953446Smrj 		workaround_applied(122);
11963446Smrj #endif
11970Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123)
11980Sstevel@tonic-gate 	if (opteron_erratum_123) {
1199359Skucharsk 		cmn_err(CE_WARN,
1200359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1201359Skucharsk 		    " processor\nerratum 123 was not detected; updating your"
1202359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1203359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1204359Skucharsk 		    " system\noperation may occur.\n");
12050Sstevel@tonic-gate 	}
12063446Smrj #endif
1207359Skucharsk #if defined(OPTERON_ERRATUM_131)
1208359Skucharsk 	if (opteron_erratum_131) {
1209359Skucharsk 		cmn_err(CE_WARN,
1210359Skucharsk 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1211359Skucharsk 		    " processor\nerratum 131 was not detected; updating your"
1212359Skucharsk 		    " system's BIOS to a version\ncontaining this"
1213359Skucharsk 		    " microcode patch is HIGHLY recommended or erroneous"
1214359Skucharsk 		    " system\noperation may occur.\n");
1215359Skucharsk 	}
12163446Smrj #endif
12173446Smrj #if defined(OPTERON_WORKAROUND_6336786)
12183446Smrj 	if (opteron_workaround_6336786)
12193446Smrj 		workaround_applied(6336786);
12203446Smrj #endif
12213446Smrj #if defined(OPTERON_WORKAROUND_6323525)
12223446Smrj 	if (opteron_workaround_6323525)
12233446Smrj 		workaround_applied(6323525);
12243446Smrj #endif
12256691Skchow #if defined(OPTERON_ERRATUM_298)
12266691Skchow 	if (opteron_erratum_298) {
12276691Skchow 		cmn_err(CE_WARN,
12286691Skchow 		    "BIOS microcode patch for AMD 64/Opteron(tm)"
12296691Skchow 		    " processor\nerratum 298 was not detected; updating your"
12306691Skchow 		    " system's BIOS to a version\ncontaining this"
12316691Skchow 		    " microcode patch is HIGHLY recommended or erroneous"
12326691Skchow 		    " system\noperation may occur.\n");
12336691Skchow 	}
12346691Skchow #endif
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate 
12373446Smrj static cpuset_t procset;
12383446Smrj 
12393446Smrj /*
12403446Smrj  * Start a single cpu, assuming that the kernel context is available
12413446Smrj  * to successfully start another cpu.
12423446Smrj  *
12433446Smrj  * (For example, real mode code is mapped into the right place
12443446Smrj  * in memory and is ready to be run.)
12453446Smrj  */
12463446Smrj int
12473446Smrj start_cpu(processorid_t who)
12483446Smrj {
12493446Smrj 	void *ctx;
12503446Smrj 	cpu_t *cp;
12513446Smrj 	int delays;
12523446Smrj 	int error = 0;
12533446Smrj 
12543446Smrj 	ASSERT(who != 0);
12553446Smrj 
12563446Smrj 	/*
12573446Smrj 	 * Check if there's at least a Mbyte of kmem available
12583446Smrj 	 * before attempting to start the cpu.
12593446Smrj 	 */
12603446Smrj 	if (kmem_avail() < 1024 * 1024) {
12613446Smrj 		/*
12623446Smrj 		 * Kick off a reap in case that helps us with
12633446Smrj 		 * later attempts ..
12643446Smrj 		 */
12653446Smrj 		kmem_reap();
12663446Smrj 		return (ENOMEM);
12673446Smrj 	}
12683446Smrj 
12693446Smrj 	cp = mp_startup_init(who);
12703446Smrj 	if ((ctx = mach_cpucontext_alloc(cp)) == NULL ||
12713446Smrj 	    (error = mach_cpu_start(cp, ctx)) != 0) {
12723446Smrj 
12733446Smrj 		/*
12743446Smrj 		 * Something went wrong before we even started it
12753446Smrj 		 */
12763446Smrj 		if (ctx)
12773446Smrj 			cmn_err(CE_WARN,
12783446Smrj 			    "cpu%d: failed to start error %d",
12793446Smrj 			    cp->cpu_id, error);
12803446Smrj 		else
12813446Smrj 			cmn_err(CE_WARN,
12823446Smrj 			    "cpu%d: failed to allocate context", cp->cpu_id);
12830Sstevel@tonic-gate 
12843446Smrj 		if (ctx)
12853446Smrj 			mach_cpucontext_free(cp, ctx, error);
12863446Smrj 		else
12873446Smrj 			error = EAGAIN;		/* hmm. */
12883446Smrj 		mp_startup_fini(cp, error);
12893446Smrj 		return (error);
12903446Smrj 	}
12913446Smrj 
12923446Smrj 	for (delays = 0; !CPU_IN_SET(procset, who); delays++) {
12933446Smrj 		if (delays == 500) {
12943446Smrj 			/*
12953446Smrj 			 * After five seconds, things are probably looking
12963446Smrj 			 * a bit bleak - explain the hang.
12973446Smrj 			 */
12983446Smrj 			cmn_err(CE_NOTE, "cpu%d: started, "
12993446Smrj 			    "but not running in the kernel yet", who);
13003446Smrj 		} else if (delays > 2000) {
13013446Smrj 			/*
13023446Smrj 			 * We waited at least 20 seconds, bail ..
13033446Smrj 			 */
13043446Smrj 			error = ETIMEDOUT;
13053446Smrj 			cmn_err(CE_WARN, "cpu%d: timed out", who);
13063446Smrj 			mach_cpucontext_free(cp, ctx, error);
13073446Smrj 			mp_startup_fini(cp, error);
13083446Smrj 			return (error);
13093446Smrj 		}
13103446Smrj 
13113446Smrj 		/*
13123446Smrj 		 * wait at least 10ms, then check again..
13133446Smrj 		 */
13143446Smrj 		delay(USEC_TO_TICK_ROUNDUP(10000));
13153446Smrj 	}
13163446Smrj 
13173446Smrj 	mach_cpucontext_free(cp, ctx, 0);
13183446Smrj 
13195084Sjohnlev #ifndef __xpv
13203446Smrj 	if (tsc_gethrtime_enable)
13213446Smrj 		tsc_sync_master(who);
13225084Sjohnlev #endif
13233446Smrj 
13243446Smrj 	if (dtrace_cpu_init != NULL) {
13253446Smrj 		/*
13263446Smrj 		 * DTrace CPU initialization expects cpu_lock to be held.
13273446Smrj 		 */
13283446Smrj 		mutex_enter(&cpu_lock);
13293446Smrj 		(*dtrace_cpu_init)(who);
13303446Smrj 		mutex_exit(&cpu_lock);
13313446Smrj 	}
13323446Smrj 
13333446Smrj 	while (!CPU_IN_SET(cpu_ready_set, who))
13343446Smrj 		delay(1);
13353446Smrj 
13363446Smrj 	return (0);
13373446Smrj }
13383446Smrj 
13392006Sandrei 
13400Sstevel@tonic-gate /*ARGSUSED*/
13410Sstevel@tonic-gate void
13420Sstevel@tonic-gate start_other_cpus(int cprboot)
13430Sstevel@tonic-gate {
13443446Smrj 	uint_t who;
13453446Smrj 	uint_t skipped = 0;
13463446Smrj 	uint_t bootcpuid = 0;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	/*
13490Sstevel@tonic-gate 	 * Initialize our own cpu_info.
13500Sstevel@tonic-gate 	 */
13510Sstevel@tonic-gate 	init_cpu_info(CPU);
13520Sstevel@tonic-gate 
13537656SSherry.Moore@Sun.COM 	cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_idstr);
13547656SSherry.Moore@Sun.COM 	cmn_err(CE_CONT, "?cpu%d: %s\n", CPU->cpu_id, CPU->cpu_brandstr);
13557656SSherry.Moore@Sun.COM 
13560Sstevel@tonic-gate 	/*
13570Sstevel@tonic-gate 	 * Initialize our syscall handlers
13580Sstevel@tonic-gate 	 */
13590Sstevel@tonic-gate 	init_cpu_syscall(CPU);
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	/*
13623446Smrj 	 * Take the boot cpu out of the mp_cpus set because we know
13633446Smrj 	 * it's already running.  Add it to the cpu_ready_set for
13643446Smrj 	 * precisely the same reason.
13653446Smrj 	 */
13663446Smrj 	CPUSET_DEL(mp_cpus, bootcpuid);
13673446Smrj 	CPUSET_ADD(cpu_ready_set, bootcpuid);
13683446Smrj 
13693446Smrj 	/*
13700Sstevel@tonic-gate 	 * if only 1 cpu or not using MP, skip the rest of this
13710Sstevel@tonic-gate 	 */
13723446Smrj 	if (CPUSET_ISNULL(mp_cpus) || use_mp == 0) {
13730Sstevel@tonic-gate 		if (use_mp == 0)
13740Sstevel@tonic-gate 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
13750Sstevel@tonic-gate 		goto done;
13760Sstevel@tonic-gate 	}
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	/*
13790Sstevel@tonic-gate 	 * perform such initialization as is needed
13800Sstevel@tonic-gate 	 * to be able to take CPUs on- and off-line.
13810Sstevel@tonic-gate 	 */
13820Sstevel@tonic-gate 	cpu_pause_init();
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	xc_init();		/* initialize processor crosscalls */
13850Sstevel@tonic-gate 
13863446Smrj 	if (mach_cpucontext_init() != 0)
13870Sstevel@tonic-gate 		goto done;
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	flushes_require_xcalls = 1;
13900Sstevel@tonic-gate 
13912575Snf202958 	/*
13922575Snf202958 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
13932575Snf202958 	 * do their TSC syncs with the same CPU.
13942575Snf202958 	 */
13950Sstevel@tonic-gate 	affinity_set(CPU_CURRENT);
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	for (who = 0; who < NCPU; who++) {
13982575Snf202958 
13992006Sandrei 		if (!CPU_IN_SET(mp_cpus, who))
14002006Sandrei 			continue;
14013446Smrj 		ASSERT(who != bootcpuid);
14022006Sandrei 		if (ncpus >= max_ncpus) {
14032006Sandrei 			skipped = who;
14040Sstevel@tonic-gate 			continue;
14052006Sandrei 		}
14063446Smrj 		if (start_cpu(who) != 0)
14073446Smrj 			CPUSET_DEL(mp_cpus, who);
1408*8803SJonathan.Haslam@Sun.COM 
1409*8803SJonathan.Haslam@Sun.COM 		mutex_enter(&cpu_lock);
1410*8803SJonathan.Haslam@Sun.COM 		cpu_state_change_notify(who, CPU_SETUP);
1411*8803SJonathan.Haslam@Sun.COM 		mutex_exit(&cpu_lock);
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate 
14144581Ssherrym 	/* Free the space allocated to hold the microcode file */
14157605SMark.Johnson@Sun.COM 	ucode_cleanup();
14164581Ssherrym 
14170Sstevel@tonic-gate 	affinity_clear();
14180Sstevel@tonic-gate 
14192006Sandrei 	if (skipped) {
14202006Sandrei 		cmn_err(CE_NOTE,
14213446Smrj 		    "System detected %d cpus, but "
14223446Smrj 		    "only %d cpu(s) were enabled during boot.",
14232006Sandrei 		    skipped + 1, ncpus);
14242006Sandrei 		cmn_err(CE_NOTE,
14252006Sandrei 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
14262006Sandrei 		    "See eeprom(1M).");
14272006Sandrei 	}
14282006Sandrei 
14290Sstevel@tonic-gate done:
14300Sstevel@tonic-gate 	workaround_errata_end();
14313446Smrj 	mach_cpucontext_fini();
14321642Sgavinm 
14331642Sgavinm 	cmi_post_mpstartup();
14340Sstevel@tonic-gate }
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate /*
14370Sstevel@tonic-gate  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
14380Sstevel@tonic-gate  */
14390Sstevel@tonic-gate /*ARGSUSED*/
14400Sstevel@tonic-gate int
14410Sstevel@tonic-gate mp_cpu_configure(int cpuid)
14420Sstevel@tonic-gate {
14430Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate /*ARGSUSED*/
14470Sstevel@tonic-gate int
14480Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid)
14490Sstevel@tonic-gate {
14500Sstevel@tonic-gate 	return (ENOTSUP);		/* not supported */
14510Sstevel@tonic-gate }
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate /*
14540Sstevel@tonic-gate  * Startup function for 'other' CPUs (besides boot cpu).
14552985Sdmick  * Called from real_mode_start.
14561251Skchow  *
14571251Skchow  * WARNING: until CPU_READY is set, mp_startup and routines called by
14581251Skchow  * mp_startup should not call routines (e.g. kmem_free) that could call
14591251Skchow  * hat_unload which requires CPU_READY to be set.
14600Sstevel@tonic-gate  */
14610Sstevel@tonic-gate void
14620Sstevel@tonic-gate mp_startup(void)
14630Sstevel@tonic-gate {
14640Sstevel@tonic-gate 	struct cpu *cp = CPU;
14650Sstevel@tonic-gate 	uint_t new_x86_feature;
14660Sstevel@tonic-gate 
14672985Sdmick 	/*
14683021Sdmick 	 * We need to get TSC on this proc synced (i.e., any delta
14693021Sdmick 	 * from cpu0 accounted for) as soon as we can, because many
14703021Sdmick 	 * many things use gethrtime/pc_gethrestime, including
14713021Sdmick 	 * interrupts, cmn_err, etc.
14723021Sdmick 	 */
14733021Sdmick 
14743021Sdmick 	/* Let cpu0 continue into tsc_sync_master() */
14753021Sdmick 	CPUSET_ATOMIC_ADD(procset, cp->cpu_id);
14763021Sdmick 
14775084Sjohnlev #ifndef __xpv
14783021Sdmick 	if (tsc_gethrtime_enable)
14793021Sdmick 		tsc_sync_slave();
14805084Sjohnlev #endif
14813021Sdmick 
14823021Sdmick 	/*
14832985Sdmick 	 * Once this was done from assembly, but it's safer here; if
14842985Sdmick 	 * it blocks, we need to be able to swtch() to and from, and
14852985Sdmick 	 * since we get here by calling t_pc, we need to do that call
14862985Sdmick 	 * before swtch() overwrites it.
14872985Sdmick 	 */
14882985Sdmick 	(void) (*ap_mlsetup)();
14892985Sdmick 
14900Sstevel@tonic-gate 	new_x86_feature = cpuid_pass1(cp);
14910Sstevel@tonic-gate 
14925084Sjohnlev #ifndef __xpv
14930Sstevel@tonic-gate 	/*
14945159Sjohnlev 	 * Program this cpu's PAT
14950Sstevel@tonic-gate 	 */
14965159Sjohnlev 	if (x86_feature & X86_PAT)
14975159Sjohnlev 		pat_sync();
14985084Sjohnlev #endif
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	/*
15013446Smrj 	 * Set up TSC_AUX to contain the cpuid for this processor
15023446Smrj 	 * for the rdtscp instruction.
15033446Smrj 	 */
15043446Smrj 	if (x86_feature & X86_TSCP)
15053446Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
15063446Smrj 
15073446Smrj 	/*
15080Sstevel@tonic-gate 	 * Initialize this CPU's syscall handlers
15090Sstevel@tonic-gate 	 */
15100Sstevel@tonic-gate 	init_cpu_syscall(cp);
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	/*
15130Sstevel@tonic-gate 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
15140Sstevel@tonic-gate 	 * highest level at which a routine is permitted to block on
15150Sstevel@tonic-gate 	 * an adaptive mutex (allows for cpu poke interrupt in case
15160Sstevel@tonic-gate 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
15170Sstevel@tonic-gate 	 * device interrupts that may end up in the hat layer issuing cross
15180Sstevel@tonic-gate 	 * calls before CPU_READY is set.
15190Sstevel@tonic-gate 	 */
15203446Smrj 	splx(ipltospl(LOCK_LEVEL));
15213446Smrj 	sti();
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	/*
15240Sstevel@tonic-gate 	 * Do a sanity check to make sure this new CPU is a sane thing
15250Sstevel@tonic-gate 	 * to add to the collection of processors running this system.
15260Sstevel@tonic-gate 	 *
15270Sstevel@tonic-gate 	 * XXX	Clearly this needs to get more sophisticated, if x86
15280Sstevel@tonic-gate 	 * systems start to get built out of heterogenous CPUs; as is
15290Sstevel@tonic-gate 	 * likely to happen once the number of processors in a configuration
15300Sstevel@tonic-gate 	 * gets large enough.
15310Sstevel@tonic-gate 	 */
15320Sstevel@tonic-gate 	if ((x86_feature & new_x86_feature) != x86_feature) {
15330Sstevel@tonic-gate 		cmn_err(CE_CONT, "?cpu%d: %b\n",
15340Sstevel@tonic-gate 		    cp->cpu_id, new_x86_feature, FMT_X86_FEATURE);
15350Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	/*
15394481Sbholler 	 * We do not support cpus with mixed monitor/mwait support if the
15404481Sbholler 	 * boot cpu supports monitor/mwait.
15414481Sbholler 	 */
15424481Sbholler 	if ((x86_feature & ~new_x86_feature) & X86_MWAIT)
15434481Sbholler 		panic("unsupported mixed cpu monitor/mwait support detected");
15444481Sbholler 
15454481Sbholler 	/*
15460Sstevel@tonic-gate 	 * We could be more sophisticated here, and just mark the CPU
15470Sstevel@tonic-gate 	 * as "faulted" but at this point we'll opt for the easier
15480Sstevel@tonic-gate 	 * answer of dieing horribly.  Provided the boot cpu is ok,
15490Sstevel@tonic-gate 	 * the system can be recovered by booting with use_mp set to zero.
15500Sstevel@tonic-gate 	 */
15510Sstevel@tonic-gate 	if (workaround_errata(cp) != 0)
15520Sstevel@tonic-gate 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	cpuid_pass2(cp);
15550Sstevel@tonic-gate 	cpuid_pass3(cp);
15560Sstevel@tonic-gate 	(void) cpuid_pass4(cp);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	init_cpu_info(cp);
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
15610Sstevel@tonic-gate 	/*
15623434Sesaxe 	 * Processor group initialization for this CPU is dependent on the
15633434Sesaxe 	 * cpuid probing, which must be done in the context of the current
15643434Sesaxe 	 * CPU.
15650Sstevel@tonic-gate 	 */
15663434Sesaxe 	pghw_physid_create(cp);
15673434Sesaxe 	pg_cpu_init(cp);
15683434Sesaxe 	pg_cmt_cpu_startup(cp);
15690Sstevel@tonic-gate 
15706749Ssherrym 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_EXISTS;
15712575Snf202958 
15727656SSherry.Moore@Sun.COM 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
15737656SSherry.Moore@Sun.COM 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
15747656SSherry.Moore@Sun.COM 
15752575Snf202958 	if (dtrace_cpu_init != NULL) {
15762575Snf202958 		(*dtrace_cpu_init)(cp->cpu_id);
15772575Snf202958 	}
15782575Snf202958 
15794581Ssherrym 	/*
15804581Ssherrym 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
15814581Ssherrym 	 */
15824581Ssherrym 	ucode_check(cp);
15834581Ssherrym 
15840Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
15850Sstevel@tonic-gate 
15863029Ssethg 	/*
15873029Ssethg 	 * Enable preemption here so that contention for any locks acquired
15883029Ssethg 	 * later in mp_startup may be preempted if the thread owning those
15893029Ssethg 	 * locks is continously executing on other CPUs (for example, this
15903029Ssethg 	 * CPU must be preemptible to allow other CPUs to pause it during their
15913029Ssethg 	 * startup phases).  It's safe to enable preemption here because the
15923029Ssethg 	 * CPU state is pretty-much fully constructed.
15933029Ssethg 	 */
15943029Ssethg 	curthread->t_preempt = 0;
15953029Ssethg 
15961482Ssethg 	/* The base spl should still be at LOCK LEVEL here */
15971482Ssethg 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
15981482Ssethg 	set_base_spl();		/* Restore the spl to its proper value */
15991482Ssethg 
16006749Ssherrym 	/* Enable interrupts */
16016749Ssherrym 	(void) spl0();
16026749Ssherrym 	mutex_enter(&cpu_lock);
16036749Ssherrym 	cpu_enable_intr(cp);
16046749Ssherrym 	cpu_add_active(cp);
16056749Ssherrym 	mutex_exit(&cpu_lock);
16066749Ssherrym 
16076749Ssherrym 	add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi);
16080Sstevel@tonic-gate 
16095254Sgavinm #ifndef __xpv
16105254Sgavinm 	{
16115254Sgavinm 		/*
16125254Sgavinm 		 * Set up the CPU module for this CPU.  This can't be done
16135254Sgavinm 		 * before this CPU is made CPU_READY, because we may (in
16145254Sgavinm 		 * heterogeneous systems) need to go load another CPU module.
16155254Sgavinm 		 * The act of attempting to load a module may trigger a
16165254Sgavinm 		 * cross-call, which will ASSERT unless this cpu is CPU_READY.
16175254Sgavinm 		 */
16185254Sgavinm 		cmi_hdl_t hdl;
16191414Scindi 
16205254Sgavinm 		if ((hdl = cmi_init(CMI_HDL_NATIVE, cmi_ntv_hwchipid(CPU),
16217532SSean.Ye@Sun.COM 		    cmi_ntv_hwcoreid(CPU), cmi_ntv_hwstrandid(CPU))) != NULL) {
16225254Sgavinm 			if (x86_feature & X86_MCA)
16235254Sgavinm 				cmi_mca_init(hdl);
16245254Sgavinm 		}
16255254Sgavinm 	}
16265254Sgavinm #endif /* __xpv */
16271414Scindi 
16280Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
16293446Smrj 		kdi_cpu_init();
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	/*
16320Sstevel@tonic-gate 	 * Setting the bit in cpu_ready_set must be the last operation in
16330Sstevel@tonic-gate 	 * processor initialization; the boot CPU will continue to boot once
16340Sstevel@tonic-gate 	 * it sees this bit set for all active CPUs.
16350Sstevel@tonic-gate 	 */
16360Sstevel@tonic-gate 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	/*
16390Sstevel@tonic-gate 	 * Because mp_startup() gets fired off after init() starts, we
16400Sstevel@tonic-gate 	 * can't use the '?' trick to do 'boot -v' printing - so we
16410Sstevel@tonic-gate 	 * always direct the 'cpu .. online' messages to the log.
16420Sstevel@tonic-gate 	 */
16430Sstevel@tonic-gate 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
16440Sstevel@tonic-gate 	    cp->cpu_id);
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	/*
16470Sstevel@tonic-gate 	 * Now we are done with the startup thread, so free it up.
16480Sstevel@tonic-gate 	 */
16490Sstevel@tonic-gate 	thread_exit();
16500Sstevel@tonic-gate 	panic("mp_startup: cannot return");
16510Sstevel@tonic-gate 	/*NOTREACHED*/
16520Sstevel@tonic-gate }
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate /*
16560Sstevel@tonic-gate  * Start CPU on user request.
16570Sstevel@tonic-gate  */
16580Sstevel@tonic-gate /* ARGSUSED */
16590Sstevel@tonic-gate int
16600Sstevel@tonic-gate mp_cpu_start(struct cpu *cp)
16610Sstevel@tonic-gate {
16620Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16630Sstevel@tonic-gate 	return (0);
16640Sstevel@tonic-gate }
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate /*
16670Sstevel@tonic-gate  * Stop CPU on user request.
16680Sstevel@tonic-gate  */
16690Sstevel@tonic-gate /* ARGSUSED */
16700Sstevel@tonic-gate int
16710Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp)
16720Sstevel@tonic-gate {
16731389Sdmick 	extern int cbe_psm_timer_mode;
16740Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16751389Sdmick 
16765084Sjohnlev #ifdef __xpv
16775084Sjohnlev 	/*
16785084Sjohnlev 	 * We can't offline vcpu0.
16795084Sjohnlev 	 */
16805084Sjohnlev 	if (cp->cpu_id == 0)
16815084Sjohnlev 		return (EBUSY);
16825084Sjohnlev #endif
16835084Sjohnlev 
16841389Sdmick 	/*
16851389Sdmick 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
16861389Sdmick 	 * can't stop it.  (This is true only for machines with no TSC.)
16871389Sdmick 	 */
16881389Sdmick 
16891389Sdmick 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
16905084Sjohnlev 		return (EBUSY);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	return (0);
16930Sstevel@tonic-gate }
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate /*
16960Sstevel@tonic-gate  * Take the specified CPU out of participation in interrupts.
16970Sstevel@tonic-gate  */
16980Sstevel@tonic-gate int
16990Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp)
17000Sstevel@tonic-gate {
17010Sstevel@tonic-gate 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
17020Sstevel@tonic-gate 		return (EBUSY);
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	cp->cpu_flags &= ~CPU_ENABLE;
17050Sstevel@tonic-gate 	return (0);
17060Sstevel@tonic-gate }
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate /*
17090Sstevel@tonic-gate  * Allow the specified CPU to participate in interrupts.
17100Sstevel@tonic-gate  */
17110Sstevel@tonic-gate void
17120Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp)
17130Sstevel@tonic-gate {
17140Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
17150Sstevel@tonic-gate 	cp->cpu_flags |= CPU_ENABLE;
17160Sstevel@tonic-gate 	psm_enable_intr(cp->cpu_id);
17170Sstevel@tonic-gate }
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 
17205254Sgavinm /*ARGSUSED*/
17210Sstevel@tonic-gate void
17220Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp)
17231414Scindi {
17245254Sgavinm #ifndef __xpv
17255254Sgavinm 	cmi_hdl_t hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
17265254Sgavinm 	    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
17275254Sgavinm 
17285254Sgavinm 	if (hdl != NULL) {
17295254Sgavinm 		cmi_faulted_enter(hdl);
17305254Sgavinm 		cmi_hdl_rele(hdl);
17315254Sgavinm 	}
17325254Sgavinm #endif
17331414Scindi }
17340Sstevel@tonic-gate 
17355254Sgavinm /*ARGSUSED*/
17360Sstevel@tonic-gate void
17370Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp)
17381414Scindi {
17395254Sgavinm #ifndef __xpv
17405254Sgavinm 	cmi_hdl_t hdl = cmi_hdl_lookup(CMI_HDL_NATIVE, cmi_ntv_hwchipid(cp),
17415254Sgavinm 	    cmi_ntv_hwcoreid(cp), cmi_ntv_hwstrandid(cp));
17425254Sgavinm 
17435254Sgavinm 	if (hdl != NULL) {
17445254Sgavinm 		cmi_faulted_exit(hdl);
17455254Sgavinm 		cmi_hdl_rele(hdl);
17465254Sgavinm 	}
17475254Sgavinm #endif
17481414Scindi }
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate /*
17510Sstevel@tonic-gate  * The following two routines are used as context operators on threads belonging
17520Sstevel@tonic-gate  * to processes with a private LDT (see sysi86).  Due to the rarity of such
17530Sstevel@tonic-gate  * processes, these routines are currently written for best code readability and
17540Sstevel@tonic-gate  * organization rather than speed.  We could avoid checking x86_feature at every
17550Sstevel@tonic-gate  * context switch by installing different context ops, depending on the
17560Sstevel@tonic-gate  * x86_feature flags, at LDT creation time -- one for each combination of fast
17570Sstevel@tonic-gate  * syscall feature flags.
17580Sstevel@tonic-gate  */
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate /*ARGSUSED*/
17610Sstevel@tonic-gate void
17620Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg)
17630Sstevel@tonic-gate {
17643446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
17650Sstevel@tonic-gate 		cpu_sep_disable();
17663446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
17670Sstevel@tonic-gate 		cpu_asysc_disable();
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate /*ARGSUSED*/
17710Sstevel@tonic-gate void
17720Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg)
17730Sstevel@tonic-gate {
17743446Smrj 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
17750Sstevel@tonic-gate 		cpu_sep_enable();
17763446Smrj 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
17770Sstevel@tonic-gate 		cpu_asysc_enable();
17780Sstevel@tonic-gate }
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate static void
17810Sstevel@tonic-gate cpu_sep_enable(void)
17820Sstevel@tonic-gate {
17830Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
17840Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
17850Sstevel@tonic-gate 
1786770Skucharsk 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
17870Sstevel@tonic-gate }
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate static void
17900Sstevel@tonic-gate cpu_sep_disable(void)
17910Sstevel@tonic-gate {
17920Sstevel@tonic-gate 	ASSERT(x86_feature & X86_SEP);
17930Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	/*
17960Sstevel@tonic-gate 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
17970Sstevel@tonic-gate 	 * the sysenter or sysexit instruction to trigger a #gp fault.
17980Sstevel@tonic-gate 	 */
17993446Smrj 	wrmsr(MSR_INTC_SEP_CS, 0);
18000Sstevel@tonic-gate }
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate static void
18030Sstevel@tonic-gate cpu_asysc_enable(void)
18040Sstevel@tonic-gate {
18050Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
18060Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
18070Sstevel@tonic-gate 
1808770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1809770Skucharsk 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
18100Sstevel@tonic-gate }
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate static void
18130Sstevel@tonic-gate cpu_asysc_disable(void)
18140Sstevel@tonic-gate {
18150Sstevel@tonic-gate 	ASSERT(x86_feature & X86_ASYSC);
18160Sstevel@tonic-gate 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	/*
18190Sstevel@tonic-gate 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
18200Sstevel@tonic-gate 	 * executing syscall or sysret with this bit off will incur a #ud trap.
18210Sstevel@tonic-gate 	 */
1822770Skucharsk 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1823770Skucharsk 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
18240Sstevel@tonic-gate }
1825