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 /* 233434Sesaxe * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/thread.h> 310Sstevel@tonic-gate #include <sys/cpuvar.h> 320Sstevel@tonic-gate #include <sys/t_lock.h> 330Sstevel@tonic-gate #include <sys/param.h> 340Sstevel@tonic-gate #include <sys/proc.h> 350Sstevel@tonic-gate #include <sys/disp.h> 360Sstevel@tonic-gate #include <sys/class.h> 370Sstevel@tonic-gate #include <sys/cmn_err.h> 380Sstevel@tonic-gate #include <sys/debug.h> 390Sstevel@tonic-gate #include <sys/asm_linkage.h> 400Sstevel@tonic-gate #include <sys/x_call.h> 410Sstevel@tonic-gate #include <sys/systm.h> 420Sstevel@tonic-gate #include <sys/var.h> 430Sstevel@tonic-gate #include <sys/vtrace.h> 440Sstevel@tonic-gate #include <vm/hat.h> 450Sstevel@tonic-gate #include <vm/as.h> 460Sstevel@tonic-gate #include <vm/seg_kmem.h> 473446Smrj #include <vm/seg_kp.h> 480Sstevel@tonic-gate #include <sys/segments.h> 490Sstevel@tonic-gate #include <sys/kmem.h> 500Sstevel@tonic-gate #include <sys/stack.h> 510Sstevel@tonic-gate #include <sys/smp_impldefs.h> 520Sstevel@tonic-gate #include <sys/x86_archext.h> 530Sstevel@tonic-gate #include <sys/machsystm.h> 540Sstevel@tonic-gate #include <sys/traptrace.h> 550Sstevel@tonic-gate #include <sys/clock.h> 560Sstevel@tonic-gate #include <sys/cpc_impl.h> 573434Sesaxe #include <sys/pg.h> 583434Sesaxe #include <sys/cmt.h> 590Sstevel@tonic-gate #include <sys/dtrace.h> 600Sstevel@tonic-gate #include <sys/archsystm.h> 610Sstevel@tonic-gate #include <sys/fp.h> 620Sstevel@tonic-gate #include <sys/reboot.h> 633446Smrj #include <sys/kdi_machimpl.h> 640Sstevel@tonic-gate #include <vm/hat_i86.h> 650Sstevel@tonic-gate #include <sys/memnode.h> 66938Sesaxe #include <sys/pci_cfgspace.h> 673446Smrj #include <sys/mach_mmu.h> 683446Smrj #include <sys/sysmacros.h> 691414Scindi #include <sys/cpu_module.h> 700Sstevel@tonic-gate 710Sstevel@tonic-gate struct cpu cpus[1]; /* CPU data */ 720Sstevel@tonic-gate struct cpu *cpu[NCPU] = {&cpus[0]}; /* pointers to all CPUs */ 730Sstevel@tonic-gate cpu_core_t cpu_core[NCPU]; /* cpu_core structures */ 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 763446Smrj * Useful for disabling MP bring-up on a MP capable system. 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate int use_mp = 1; 790Sstevel@tonic-gate 802006Sandrei /* 813446Smrj * to be set by a PSM to indicate what cpus 823446Smrj * are sitting around on the system. 832006Sandrei */ 843446Smrj cpuset_t mp_cpus; 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* 870Sstevel@tonic-gate * This variable is used by the hat layer to decide whether or not 880Sstevel@tonic-gate * critical sections are needed to prevent race conditions. For sun4m, 890Sstevel@tonic-gate * this variable is set once enough MP initialization has been done in 900Sstevel@tonic-gate * order to allow cross calls. 910Sstevel@tonic-gate */ 923446Smrj int flushes_require_xcalls; 933446Smrj cpuset_t cpu_ready_set = 1; 940Sstevel@tonic-gate 950Sstevel@tonic-gate static void mp_startup(void); 960Sstevel@tonic-gate 970Sstevel@tonic-gate static void cpu_sep_enable(void); 980Sstevel@tonic-gate static void cpu_sep_disable(void); 990Sstevel@tonic-gate static void cpu_asysc_enable(void); 1000Sstevel@tonic-gate static void cpu_asysc_disable(void); 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate extern int tsc_gethrtime_enable; 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 123*4877Smh27603 /* 124*4877Smh27603 * Supported frequencies. 125*4877Smh27603 */ 126*4877Smh27603 cpu_set_supp_freqs(cp, NULL); 127*4877Smh27603 1280Sstevel@tonic-gate (void) strcpy(pi->pi_processor_type, "i386"); 1290Sstevel@tonic-gate if (fpu_exists) 1300Sstevel@tonic-gate (void) strcpy(pi->pi_fputypes, "i387 compatible"); 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate (void) cpuid_getidstr(cp, buf, sizeof (buf)); 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP); 1350Sstevel@tonic-gate (void) strcpy(cp->cpu_idstr, buf); 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr); 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate (void) cpuid_getbrandstr(cp, buf, sizeof (buf)); 1400Sstevel@tonic-gate cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP); 1410Sstevel@tonic-gate (void) strcpy(cp->cpu_brandstr, buf); 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr); 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* 1470Sstevel@tonic-gate * Configure syscall support on this CPU. 1480Sstevel@tonic-gate */ 1490Sstevel@tonic-gate /*ARGSUSED*/ 1500Sstevel@tonic-gate static void 1510Sstevel@tonic-gate init_cpu_syscall(struct cpu *cp) 1520Sstevel@tonic-gate { 1530Sstevel@tonic-gate kpreempt_disable(); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate #if defined(__amd64) 1563446Smrj if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC)) { 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate #if !defined(__lint) 1590Sstevel@tonic-gate /* 1600Sstevel@tonic-gate * The syscall instruction imposes a certain ordering on 1610Sstevel@tonic-gate * segment selectors, so we double-check that ordering 1620Sstevel@tonic-gate * here. 1630Sstevel@tonic-gate */ 1640Sstevel@tonic-gate ASSERT(KDS_SEL == KCS_SEL + 8); 1650Sstevel@tonic-gate ASSERT(UDS_SEL == U32CS_SEL + 8); 1660Sstevel@tonic-gate ASSERT(UCS_SEL == U32CS_SEL + 16); 1670Sstevel@tonic-gate #endif 1680Sstevel@tonic-gate /* 1690Sstevel@tonic-gate * Turn syscall/sysret extensions on. 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate cpu_asysc_enable(); 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * Program the magic registers .. 1750Sstevel@tonic-gate */ 1763446Smrj wrmsr(MSR_AMD_STAR, 1773446Smrj ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32); 178770Skucharsk wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall); 179770Skucharsk wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32); 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* 1820Sstevel@tonic-gate * This list of flags is masked off the incoming 1830Sstevel@tonic-gate * %rfl when we enter the kernel. 1840Sstevel@tonic-gate */ 185770Skucharsk wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T)); 1860Sstevel@tonic-gate } 1870Sstevel@tonic-gate #endif 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * On 32-bit kernels, we use sysenter/sysexit because it's too 1910Sstevel@tonic-gate * hard to use syscall/sysret, and it is more portable anyway. 1920Sstevel@tonic-gate * 1930Sstevel@tonic-gate * On 64-bit kernels on Nocona machines, the 32-bit syscall 1940Sstevel@tonic-gate * variant isn't available to 32-bit applications, but sysenter is. 1950Sstevel@tonic-gate */ 1963446Smrj if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP)) { 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate #if !defined(__lint) 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * The sysenter instruction imposes a certain ordering on 2010Sstevel@tonic-gate * segment selectors, so we double-check that ordering 2020Sstevel@tonic-gate * here. See "sysenter" in Intel document 245471-012, "IA-32 2030Sstevel@tonic-gate * Intel Architecture Software Developer's Manual Volume 2: 2040Sstevel@tonic-gate * Instruction Set Reference" 2050Sstevel@tonic-gate */ 2060Sstevel@tonic-gate ASSERT(KDS_SEL == KCS_SEL + 8); 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3)); 2090Sstevel@tonic-gate ASSERT32(UDS_SEL == UCS_SEL + 8); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3)); 2120Sstevel@tonic-gate ASSERT64(UDS_SEL == U32CS_SEL + 8); 2130Sstevel@tonic-gate #endif 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate cpu_sep_enable(); 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* 2180Sstevel@tonic-gate * resume() sets this value to the base of the threads stack 2190Sstevel@tonic-gate * via a context handler. 2200Sstevel@tonic-gate */ 2213446Smrj wrmsr(MSR_INTC_SEP_ESP, 0); 222770Skucharsk wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate kpreempt_enable(); 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* 2290Sstevel@tonic-gate * Multiprocessor initialization. 2300Sstevel@tonic-gate * 2310Sstevel@tonic-gate * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the 2320Sstevel@tonic-gate * startup and idle threads for the specified CPU. 2330Sstevel@tonic-gate */ 2343446Smrj struct cpu * 2350Sstevel@tonic-gate mp_startup_init(int cpun) 2360Sstevel@tonic-gate { 2370Sstevel@tonic-gate struct cpu *cp; 2380Sstevel@tonic-gate kthread_id_t tp; 2390Sstevel@tonic-gate caddr_t sp; 2400Sstevel@tonic-gate proc_t *procp; 2410Sstevel@tonic-gate extern void idle(); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate #ifdef TRAPTRACE 2440Sstevel@tonic-gate trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun]; 2450Sstevel@tonic-gate #endif 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate ASSERT(cpun < NCPU && cpu[cpun] == NULL); 2480Sstevel@tonic-gate 2493446Smrj cp = kmem_zalloc(sizeof (*cp), KM_SLEEP); 2504481Sbholler if (x86_feature & X86_MWAIT) 2514481Sbholler cp->cpu_m.mcpu_mwait = mach_alloc_mwait(CPU); 2524481Sbholler 2530Sstevel@tonic-gate procp = curthread->t_procp; 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate mutex_enter(&cpu_lock); 2560Sstevel@tonic-gate /* 2570Sstevel@tonic-gate * Initialize the dispatcher first. 2580Sstevel@tonic-gate */ 2590Sstevel@tonic-gate disp_cpu_init(cp); 2600Sstevel@tonic-gate mutex_exit(&cpu_lock); 2610Sstevel@tonic-gate 262414Skchow cpu_vm_data_init(cp); 263414Skchow 2640Sstevel@tonic-gate /* 2650Sstevel@tonic-gate * Allocate and initialize the startup thread for this CPU. 2660Sstevel@tonic-gate * Interrupt and process switch stacks get allocated later 2670Sstevel@tonic-gate * when the CPU starts running. 2680Sstevel@tonic-gate */ 2690Sstevel@tonic-gate tp = thread_create(NULL, 0, NULL, NULL, 0, procp, 2700Sstevel@tonic-gate TS_STOPPED, maxclsyspri); 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate /* 2730Sstevel@tonic-gate * Set state to TS_ONPROC since this thread will start running 2740Sstevel@tonic-gate * as soon as the CPU comes online. 2750Sstevel@tonic-gate * 2760Sstevel@tonic-gate * All the other fields of the thread structure are setup by 2770Sstevel@tonic-gate * thread_create(). 2780Sstevel@tonic-gate */ 2790Sstevel@tonic-gate THREAD_ONPROC(tp, cp); 2800Sstevel@tonic-gate tp->t_preempt = 1; 2810Sstevel@tonic-gate tp->t_bound_cpu = cp; 2820Sstevel@tonic-gate tp->t_affinitycnt = 1; 2830Sstevel@tonic-gate tp->t_cpu = cp; 2840Sstevel@tonic-gate tp->t_disp_queue = cp->cpu_disp; 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate /* 2870Sstevel@tonic-gate * Setup thread to start in mp_startup. 2880Sstevel@tonic-gate */ 2890Sstevel@tonic-gate sp = tp->t_stk; 2900Sstevel@tonic-gate tp->t_pc = (uintptr_t)mp_startup; 2910Sstevel@tonic-gate tp->t_sp = (uintptr_t)(sp - MINFRAME); 2923446Smrj #if defined(__amd64) 2933446Smrj tp->t_sp -= STACK_ENTRY_ALIGN; /* fake a call */ 2943446Smrj #endif 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate cp->cpu_id = cpun; 2970Sstevel@tonic-gate cp->cpu_self = cp; 2980Sstevel@tonic-gate cp->cpu_thread = tp; 2990Sstevel@tonic-gate cp->cpu_lwp = NULL; 3000Sstevel@tonic-gate cp->cpu_dispthread = tp; 3010Sstevel@tonic-gate cp->cpu_dispatch_pri = DISP_PRIO(tp); 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate /* 3041482Ssethg * cpu_base_spl must be set explicitly here to prevent any blocking 3051482Ssethg * operations in mp_startup from causing the spl of the cpu to drop 3061482Ssethg * to 0 (allowing device interrupts before we're ready) in resume(). 3071482Ssethg * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY. 3081482Ssethg * As an extra bit of security on DEBUG kernels, this is enforced with 3091482Ssethg * an assertion in mp_startup() -- before cpu_base_spl is set to its 3101482Ssethg * proper value. 3111482Ssethg */ 3121482Ssethg cp->cpu_base_spl = ipltospl(LOCK_LEVEL); 3131482Ssethg 3141482Ssethg /* 3150Sstevel@tonic-gate * Now, initialize per-CPU idle thread for this CPU. 3160Sstevel@tonic-gate */ 3170Sstevel@tonic-gate tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1); 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate cp->cpu_idle_thread = tp; 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate tp->t_preempt = 1; 3220Sstevel@tonic-gate tp->t_bound_cpu = cp; 3230Sstevel@tonic-gate tp->t_affinitycnt = 1; 3240Sstevel@tonic-gate tp->t_cpu = cp; 3250Sstevel@tonic-gate tp->t_disp_queue = cp->cpu_disp; 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate /* 3283434Sesaxe * Bootstrap the CPU's PG data 32960Sesaxe */ 3303434Sesaxe pg_cpu_bootstrap(cp); 33160Sesaxe 33260Sesaxe /* 3333446Smrj * Perform CPC initialization on the new CPU. 3340Sstevel@tonic-gate */ 3350Sstevel@tonic-gate kcpc_hw_init(cp); 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2 3390Sstevel@tonic-gate * for each CPU. 3400Sstevel@tonic-gate */ 3410Sstevel@tonic-gate setup_vaddr_for_ppcopy(cp); 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate /* 3443446Smrj * Allocate page for new GDT and initialize from current GDT. 3450Sstevel@tonic-gate */ 3463446Smrj #if !defined(__lint) 3473446Smrj ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE); 3483446Smrj #endif 3493446Smrj cp->cpu_m.mcpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP); 3503446Smrj bcopy(CPU->cpu_m.mcpu_gdt, cp->cpu_m.mcpu_gdt, 3513446Smrj (sizeof (*cp->cpu_m.mcpu_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) { 3683446Smrj struct machcpu *mcpu = &cp->cpu_m; 3693446Smrj 3703446Smrj mcpu->mcpu_idt = kmem_alloc(sizeof (idt0), KM_SLEEP); 3713446Smrj bcopy(idt0, mcpu->mcpu_idt, sizeof (idt0)); 3723446Smrj } else { 3733446Smrj cp->cpu_m.mcpu_idt = CPU->cpu_m.mcpu_idt; 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* 3773446Smrj * Get interrupt priority data from cpu 0. 3780Sstevel@tonic-gate */ 3790Sstevel@tonic-gate cp->cpu_pri_data = CPU->cpu_pri_data; 3800Sstevel@tonic-gate 3813446Smrj /* 3823446Smrj * alloc space for cpuid info 3833446Smrj */ 3843446Smrj cpuid_alloc_space(cp); 3853446Smrj 3864581Ssherrym /* 3874581Ssherrym * alloc space for ucode_info 3884581Ssherrym */ 3894581Ssherrym ucode_alloc_space(cp); 3904581Ssherrym 3910Sstevel@tonic-gate hat_cpu_online(cp); 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate #ifdef TRAPTRACE 3940Sstevel@tonic-gate /* 3953446Smrj * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers 3960Sstevel@tonic-gate */ 3970Sstevel@tonic-gate ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP); 3980Sstevel@tonic-gate ttc->ttc_next = ttc->ttc_first; 3990Sstevel@tonic-gate ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize; 4000Sstevel@tonic-gate #endif 4010Sstevel@tonic-gate /* 4020Sstevel@tonic-gate * Record that we have another CPU. 4030Sstevel@tonic-gate */ 4040Sstevel@tonic-gate mutex_enter(&cpu_lock); 4050Sstevel@tonic-gate /* 4060Sstevel@tonic-gate * Initialize the interrupt threads for this CPU 4070Sstevel@tonic-gate */ 4081455Sandrei cpu_intr_alloc(cp, NINTR_THREADS); 4090Sstevel@tonic-gate /* 4100Sstevel@tonic-gate * Add CPU to list of available CPUs. It'll be on the active list 4110Sstevel@tonic-gate * after mp_startup(). 4120Sstevel@tonic-gate */ 4130Sstevel@tonic-gate cpu_add_unit(cp); 4140Sstevel@tonic-gate mutex_exit(&cpu_lock); 4153446Smrj 4163446Smrj return (cp); 4173446Smrj } 4183446Smrj 4193446Smrj /* 4203446Smrj * Undo what was done in mp_startup_init 4213446Smrj */ 4223446Smrj static void 4233446Smrj mp_startup_fini(struct cpu *cp, int error) 4243446Smrj { 4253446Smrj mutex_enter(&cpu_lock); 4263446Smrj 4273446Smrj /* 4283446Smrj * Remove the CPU from the list of available CPUs. 4293446Smrj */ 4303446Smrj cpu_del_unit(cp->cpu_id); 4313446Smrj 4323446Smrj if (error == ETIMEDOUT) { 4333446Smrj /* 4343446Smrj * The cpu was started, but never *seemed* to run any 4353446Smrj * code in the kernel; it's probably off spinning in its 4363446Smrj * own private world, though with potential references to 4373446Smrj * our kmem-allocated IDTs and GDTs (for example). 4383446Smrj * 4393446Smrj * Worse still, it may actually wake up some time later, 4403446Smrj * so rather than guess what it might or might not do, we 4413446Smrj * leave the fundamental data structures intact. 4423446Smrj */ 4433446Smrj cp->cpu_flags = 0; 4443446Smrj mutex_exit(&cpu_lock); 4453446Smrj return; 4463446Smrj } 4473446Smrj 4483446Smrj /* 4493446Smrj * At this point, the only threads bound to this CPU should 4503446Smrj * special per-cpu threads: it's idle thread, it's pause threads, 4513446Smrj * and it's interrupt threads. Clean these up. 4523446Smrj */ 4533446Smrj cpu_destroy_bound_threads(cp); 4543446Smrj cp->cpu_idle_thread = NULL; 4553446Smrj 4563446Smrj /* 4573446Smrj * Free the interrupt stack. 4583446Smrj */ 4593446Smrj segkp_release(segkp, 4603446Smrj cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME))); 4613446Smrj 4623446Smrj mutex_exit(&cpu_lock); 4633446Smrj 4643446Smrj #ifdef TRAPTRACE 4653446Smrj /* 4663446Smrj * Discard the trap trace buffer 4673446Smrj */ 4683446Smrj { 4693446Smrj trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id]; 4703446Smrj 4713446Smrj kmem_free((void *)ttc->ttc_first, trap_trace_bufsize); 4723446Smrj ttc->ttc_first = NULL; 4733446Smrj } 4743446Smrj #endif 4753446Smrj 4763446Smrj hat_cpu_offline(cp); 4773446Smrj 4783446Smrj cpuid_free_space(cp); 4793446Smrj 4804581Ssherrym ucode_free_space(cp); 4814581Ssherrym 4823446Smrj if (cp->cpu_m.mcpu_idt != CPU->cpu_m.mcpu_idt) 4833446Smrj kmem_free(cp->cpu_m.mcpu_idt, sizeof (idt0)); 4843446Smrj cp->cpu_m.mcpu_idt = NULL; 4853446Smrj 4863446Smrj kmem_free(cp->cpu_m.mcpu_gdt, PAGESIZE); 4873446Smrj cp->cpu_m.mcpu_gdt = NULL; 4883446Smrj 4893446Smrj teardown_vaddr_for_ppcopy(cp); 4903446Smrj 4913446Smrj kcpc_hw_fini(cp); 4923446Smrj 4933446Smrj cp->cpu_dispthread = NULL; 4943446Smrj cp->cpu_thread = NULL; /* discarded by cpu_destroy_bound_threads() */ 4953446Smrj 4963446Smrj cpu_vm_data_destroy(cp); 4973446Smrj 4983446Smrj mutex_enter(&cpu_lock); 4993446Smrj disp_cpu_fini(cp); 5003446Smrj mutex_exit(&cpu_lock); 5013446Smrj 5023446Smrj kmem_free(cp, sizeof (*cp)); 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate /* 5060Sstevel@tonic-gate * Apply workarounds for known errata, and warn about those that are absent. 5070Sstevel@tonic-gate * 5080Sstevel@tonic-gate * System vendors occasionally create configurations which contain different 5090Sstevel@tonic-gate * revisions of the CPUs that are almost but not exactly the same. At the 5100Sstevel@tonic-gate * time of writing, this meant that their clock rates were the same, their 5110Sstevel@tonic-gate * feature sets were the same, but the required workaround were -not- 5120Sstevel@tonic-gate * necessarily the same. So, this routine is invoked on -every- CPU soon 5130Sstevel@tonic-gate * after starting to make sure that the resulting system contains the most 5140Sstevel@tonic-gate * pessimal set of workarounds needed to cope with *any* of the CPUs in the 5150Sstevel@tonic-gate * system. 5160Sstevel@tonic-gate * 517938Sesaxe * workaround_errata is invoked early in mlsetup() for CPU 0, and in 518938Sesaxe * mp_startup() for all slave CPUs. Slaves process workaround_errata prior 519938Sesaxe * to acknowledging their readiness to the master, so this routine will 520938Sesaxe * never be executed by multiple CPUs in parallel, thus making updates to 521938Sesaxe * global data safe. 522938Sesaxe * 523359Skucharsk * These workarounds are based on Rev 3.57 of the Revision Guide for 524359Skucharsk * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005. 5250Sstevel@tonic-gate */ 5260Sstevel@tonic-gate 5273446Smrj #if defined(OPTERON_ERRATUM_88) 5283446Smrj int opteron_erratum_88; /* if non-zero -> at least one cpu has it */ 5293446Smrj #endif 5303446Smrj 5310Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91) 5320Sstevel@tonic-gate int opteron_erratum_91; /* if non-zero -> at least one cpu has it */ 5330Sstevel@tonic-gate #endif 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) 5360Sstevel@tonic-gate int opteron_erratum_93; /* if non-zero -> at least one cpu has it */ 5370Sstevel@tonic-gate #endif 5380Sstevel@tonic-gate 5393446Smrj #if defined(OPTERON_ERRATUM_95) 5403446Smrj int opteron_erratum_95; /* if non-zero -> at least one cpu has it */ 5413446Smrj #endif 5423446Smrj 5430Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) 5440Sstevel@tonic-gate int opteron_erratum_100; /* if non-zero -> at least one cpu has it */ 5450Sstevel@tonic-gate #endif 5460Sstevel@tonic-gate 5473446Smrj #if defined(OPTERON_ERRATUM_108) 5483446Smrj int opteron_erratum_108; /* if non-zero -> at least one cpu has it */ 5493446Smrj #endif 5503446Smrj 5510Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109) 5520Sstevel@tonic-gate int opteron_erratum_109; /* if non-zero -> at least one cpu has it */ 5530Sstevel@tonic-gate #endif 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121) 5560Sstevel@tonic-gate int opteron_erratum_121; /* if non-zero -> at least one cpu has it */ 5570Sstevel@tonic-gate #endif 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122) 5600Sstevel@tonic-gate int opteron_erratum_122; /* if non-zero -> at least one cpu has it */ 5610Sstevel@tonic-gate #endif 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123) 5640Sstevel@tonic-gate int opteron_erratum_123; /* if non-zero -> at least one cpu has it */ 5650Sstevel@tonic-gate #endif 5660Sstevel@tonic-gate 567359Skucharsk #if defined(OPTERON_ERRATUM_131) 568359Skucharsk int opteron_erratum_131; /* if non-zero -> at least one cpu has it */ 569359Skucharsk #endif 5700Sstevel@tonic-gate 571938Sesaxe #if defined(OPTERON_WORKAROUND_6336786) 572938Sesaxe int opteron_workaround_6336786; /* non-zero -> WA relevant and applied */ 573938Sesaxe int opteron_workaround_6336786_UP = 0; /* Not needed for UP */ 574938Sesaxe #endif 575938Sesaxe 5761582Skchow #if defined(OPTERON_WORKAROUND_6323525) 5771582Skchow int opteron_workaround_6323525; /* if non-zero -> at least one cpu has it */ 5781582Skchow #endif 5791582Skchow 5803446Smrj static void 5813446Smrj workaround_warning(cpu_t *cp, uint_t erratum) 5823446Smrj { 5833446Smrj cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u", 5843446Smrj cp->cpu_id, erratum); 5853446Smrj } 5863446Smrj 5873446Smrj static void 5883446Smrj workaround_applied(uint_t erratum) 5893446Smrj { 5903446Smrj if (erratum > 1000000) 5913446Smrj cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n", 5923446Smrj erratum); 5933446Smrj else 5943446Smrj cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n", 5953446Smrj erratum); 5963446Smrj } 5973446Smrj 5983446Smrj static void 5993446Smrj msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error) 6003446Smrj { 6013446Smrj cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d", 6023446Smrj cp->cpu_id, rw, msr, error); 6033446Smrj } 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate uint_t 6060Sstevel@tonic-gate workaround_errata(struct cpu *cpu) 6070Sstevel@tonic-gate { 6080Sstevel@tonic-gate uint_t missing = 0; 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate ASSERT(cpu == CPU); 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate /*LINTED*/ 6130Sstevel@tonic-gate if (cpuid_opteron_erratum(cpu, 88) > 0) { 6140Sstevel@tonic-gate /* 6150Sstevel@tonic-gate * SWAPGS May Fail To Read Correct GS Base 6160Sstevel@tonic-gate */ 6170Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_88) 6180Sstevel@tonic-gate /* 6190Sstevel@tonic-gate * The workaround is an mfence in the relevant assembler code 6200Sstevel@tonic-gate */ 6213446Smrj opteron_erratum_88++; 6220Sstevel@tonic-gate #else 6233446Smrj workaround_warning(cpu, 88); 6240Sstevel@tonic-gate missing++; 6250Sstevel@tonic-gate #endif 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate if (cpuid_opteron_erratum(cpu, 91) > 0) { 6290Sstevel@tonic-gate /* 6300Sstevel@tonic-gate * Software Prefetches May Report A Page Fault 6310Sstevel@tonic-gate */ 6320Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_91) 6330Sstevel@tonic-gate /* 6340Sstevel@tonic-gate * fix is in trap.c 6350Sstevel@tonic-gate */ 6360Sstevel@tonic-gate opteron_erratum_91++; 6370Sstevel@tonic-gate #else 6383446Smrj workaround_warning(cpu, 91); 6390Sstevel@tonic-gate missing++; 6400Sstevel@tonic-gate #endif 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate if (cpuid_opteron_erratum(cpu, 93) > 0) { 6440Sstevel@tonic-gate /* 6450Sstevel@tonic-gate * RSM Auto-Halt Restart Returns to Incorrect RIP 6460Sstevel@tonic-gate */ 6470Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_93) 6480Sstevel@tonic-gate /* 6490Sstevel@tonic-gate * fix is in trap.c 6500Sstevel@tonic-gate */ 6510Sstevel@tonic-gate opteron_erratum_93++; 6520Sstevel@tonic-gate #else 6533446Smrj workaround_warning(cpu, 93); 6540Sstevel@tonic-gate missing++; 6550Sstevel@tonic-gate #endif 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate /*LINTED*/ 6590Sstevel@tonic-gate if (cpuid_opteron_erratum(cpu, 95) > 0) { 6600Sstevel@tonic-gate /* 6610Sstevel@tonic-gate * RET Instruction May Return to Incorrect EIP 6620Sstevel@tonic-gate */ 6630Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_95) 6640Sstevel@tonic-gate #if defined(_LP64) 6650Sstevel@tonic-gate /* 6660Sstevel@tonic-gate * Workaround this by ensuring that 32-bit user code and 6670Sstevel@tonic-gate * 64-bit kernel code never occupy the same address 6680Sstevel@tonic-gate * range mod 4G. 6690Sstevel@tonic-gate */ 6700Sstevel@tonic-gate if (_userlimit32 > 0xc0000000ul) 6710Sstevel@tonic-gate *(uintptr_t *)&_userlimit32 = 0xc0000000ul; 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /*LINTED*/ 6740Sstevel@tonic-gate ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u); 6753446Smrj opteron_erratum_95++; 6760Sstevel@tonic-gate #endif /* _LP64 */ 6770Sstevel@tonic-gate #else 6783446Smrj workaround_warning(cpu, 95); 6790Sstevel@tonic-gate missing++; 6803446Smrj #endif 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate if (cpuid_opteron_erratum(cpu, 100) > 0) { 6840Sstevel@tonic-gate /* 6850Sstevel@tonic-gate * Compatibility Mode Branches Transfer to Illegal Address 6860Sstevel@tonic-gate */ 6870Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_100) 6880Sstevel@tonic-gate /* 6890Sstevel@tonic-gate * fix is in trap.c 6900Sstevel@tonic-gate */ 6910Sstevel@tonic-gate opteron_erratum_100++; 6920Sstevel@tonic-gate #else 6933446Smrj workaround_warning(cpu, 100); 6940Sstevel@tonic-gate missing++; 6950Sstevel@tonic-gate #endif 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate /*LINTED*/ 6990Sstevel@tonic-gate if (cpuid_opteron_erratum(cpu, 108) > 0) { 7000Sstevel@tonic-gate /* 7010Sstevel@tonic-gate * CPUID Instruction May Return Incorrect Model Number In 7020Sstevel@tonic-gate * Some Processors 7030Sstevel@tonic-gate */ 7040Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_108) 7050Sstevel@tonic-gate /* 7060Sstevel@tonic-gate * (Our cpuid-handling code corrects the model number on 7070Sstevel@tonic-gate * those processors) 7080Sstevel@tonic-gate */ 7090Sstevel@tonic-gate #else 7103446Smrj workaround_warning(cpu, 108); 7110Sstevel@tonic-gate missing++; 7120Sstevel@tonic-gate #endif 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate /*LINTED*/ 7163446Smrj if (cpuid_opteron_erratum(cpu, 109) > 0) do { 7170Sstevel@tonic-gate /* 7180Sstevel@tonic-gate * Certain Reverse REP MOVS May Produce Unpredictable Behaviour 7190Sstevel@tonic-gate */ 7200Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109) 7213446Smrj /* 7223446Smrj * The "workaround" is to print a warning to upgrade the BIOS 7233446Smrj */ 7243446Smrj uint64_t value; 7253446Smrj const uint_t msr = MSR_AMD_PATCHLEVEL; 7263446Smrj int err; 7270Sstevel@tonic-gate 7283446Smrj if ((err = checked_rdmsr(msr, &value)) != 0) { 7293446Smrj msr_warning(cpu, "rd", msr, err); 7303446Smrj workaround_warning(cpu, 109); 7313446Smrj missing++; 7323446Smrj } 7333446Smrj if (value == 0) 7340Sstevel@tonic-gate opteron_erratum_109++; 7350Sstevel@tonic-gate #else 7363446Smrj workaround_warning(cpu, 109); 7370Sstevel@tonic-gate missing++; 7380Sstevel@tonic-gate #endif 7393446Smrj /*CONSTANTCONDITION*/ 7403446Smrj } while (0); 7413446Smrj 7420Sstevel@tonic-gate /*LINTED*/ 7430Sstevel@tonic-gate if (cpuid_opteron_erratum(cpu, 121) > 0) { 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * Sequential Execution Across Non_Canonical Boundary Caused 7460Sstevel@tonic-gate * Processor Hang 7470Sstevel@tonic-gate */ 7480Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121) 7493446Smrj #if defined(_LP64) 7500Sstevel@tonic-gate /* 7510Sstevel@tonic-gate * Erratum 121 is only present in long (64 bit) mode. 7520Sstevel@tonic-gate * Workaround is to include the page immediately before the 7530Sstevel@tonic-gate * va hole to eliminate the possibility of system hangs due to 7540Sstevel@tonic-gate * sequential execution across the va hole boundary. 7550Sstevel@tonic-gate */ 7563446Smrj if (opteron_erratum_121) 7573446Smrj opteron_erratum_121++; 7583446Smrj else { 7593446Smrj if (hole_start) { 7603446Smrj hole_start -= PAGESIZE; 7613446Smrj } else { 7623446Smrj /* 7633446Smrj * hole_start not yet initialized by 7643446Smrj * mmu_init. Initialize hole_start 7653446Smrj * with value to be subtracted. 7663446Smrj */ 7673446Smrj hole_start = PAGESIZE; 7680Sstevel@tonic-gate } 7693446Smrj opteron_erratum_121++; 7700Sstevel@tonic-gate } 7713446Smrj #endif /* _LP64 */ 7720Sstevel@tonic-gate #else 7733446Smrj workaround_warning(cpu, 121); 7740Sstevel@tonic-gate missing++; 7750Sstevel@tonic-gate #endif 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate /*LINTED*/ 7793446Smrj if (cpuid_opteron_erratum(cpu, 122) > 0) do { 7800Sstevel@tonic-gate /* 7813446Smrj * TLB Flush Filter May Cause Coherency Problem in 7820Sstevel@tonic-gate * Multiprocessor Systems 7830Sstevel@tonic-gate */ 7840Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_122) 7853446Smrj uint64_t value; 7863446Smrj const uint_t msr = MSR_AMD_HWCR; 7873446Smrj int error; 7883446Smrj 7890Sstevel@tonic-gate /* 7900Sstevel@tonic-gate * Erratum 122 is only present in MP configurations (multi-core 7910Sstevel@tonic-gate * or multi-processor). 7920Sstevel@tonic-gate */ 7933446Smrj if (!opteron_erratum_122 && lgrp_plat_node_cnt == 1 && 7943446Smrj cpuid_get_ncpu_per_chip(cpu) == 1) 7953446Smrj break; 7960Sstevel@tonic-gate 7973446Smrj /* disable TLB Flush Filter */ 7983446Smrj 7993446Smrj if ((error = checked_rdmsr(msr, &value)) != 0) { 8003446Smrj msr_warning(cpu, "rd", msr, error); 8013446Smrj workaround_warning(cpu, 122); 8023446Smrj missing++; 8033446Smrj } else { 8043446Smrj value |= (uint64_t)AMD_HWCR_FFDIS; 8053446Smrj if ((error = checked_wrmsr(msr, value)) != 0) { 8063446Smrj msr_warning(cpu, "wr", msr, error); 8073446Smrj workaround_warning(cpu, 122); 8083446Smrj missing++; 8093446Smrj } 8100Sstevel@tonic-gate } 8113446Smrj opteron_erratum_122++; 8120Sstevel@tonic-gate #else 8133446Smrj workaround_warning(cpu, 122); 8140Sstevel@tonic-gate missing++; 8150Sstevel@tonic-gate #endif 8163446Smrj /*CONSTANTCONDITION*/ 8173446Smrj } while (0); 818302Skchow 8190Sstevel@tonic-gate /*LINTED*/ 8203446Smrj if (cpuid_opteron_erratum(cpu, 123) > 0) do { 8210Sstevel@tonic-gate /* 8220Sstevel@tonic-gate * Bypassed Reads May Cause Data Corruption of System Hang in 8230Sstevel@tonic-gate * Dual Core Processors 8240Sstevel@tonic-gate */ 8253446Smrj #if defined(OPTERON_ERRATUM_123) 8263446Smrj uint64_t value; 8273446Smrj const uint_t msr = MSR_AMD_PATCHLEVEL; 8283446Smrj int err; 8293446Smrj 8300Sstevel@tonic-gate /* 8310Sstevel@tonic-gate * Erratum 123 applies only to multi-core cpus. 8320Sstevel@tonic-gate */ 8333446Smrj if (cpuid_get_ncpu_per_chip(cpu) < 2) 8343446Smrj break; 8350Sstevel@tonic-gate 8363446Smrj /* 8373446Smrj * The "workaround" is to print a warning to upgrade the BIOS 8383446Smrj */ 8393446Smrj if ((err = checked_rdmsr(msr, &value)) != 0) { 8403446Smrj msr_warning(cpu, "rd", msr, err); 8413446Smrj workaround_warning(cpu, 123); 8423446Smrj missing++; 8430Sstevel@tonic-gate } 8443446Smrj if (value == 0) 8453446Smrj opteron_erratum_123++; 8463446Smrj #else 8473446Smrj workaround_warning(cpu, 123); 8483446Smrj missing++; 849359Skucharsk 8503446Smrj #endif 8513446Smrj /*CONSTANTCONDITION*/ 8523446Smrj } while (0); 8533446Smrj 854359Skucharsk /*LINTED*/ 8553446Smrj if (cpuid_opteron_erratum(cpu, 131) > 0) do { 856359Skucharsk /* 857359Skucharsk * Multiprocessor Systems with Four or More Cores May Deadlock 858359Skucharsk * Waiting for a Probe Response 859359Skucharsk */ 8603446Smrj #if defined(OPTERON_ERRATUM_131) 8613446Smrj uint64_t nbcfg; 8623446Smrj const uint_t msr = MSR_AMD_NB_CFG; 8633446Smrj const uint64_t wabits = 8643446Smrj AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR; 8653446Smrj int error; 8663446Smrj 867359Skucharsk /* 868359Skucharsk * Erratum 131 applies to any system with four or more cores. 869359Skucharsk */ 8703446Smrj if (opteron_erratum_131) 8713446Smrj break; 8722519Skchow 8733446Smrj if (lgrp_plat_node_cnt * cpuid_get_ncpu_per_chip(cpu) < 4) 8743446Smrj break; 8752519Skchow 8763446Smrj /* 8773446Smrj * Print a warning if neither of the workarounds for 8783446Smrj * erratum 131 is present. 8793446Smrj */ 8803446Smrj if ((error = checked_rdmsr(msr, &nbcfg)) != 0) { 8813446Smrj msr_warning(cpu, "rd", msr, error); 8823446Smrj workaround_warning(cpu, 131); 8833446Smrj missing++; 8843446Smrj } else if ((nbcfg & wabits) == 0) { 8853446Smrj opteron_erratum_131++; 8863446Smrj } else { 8873446Smrj /* cannot have both workarounds set */ 8883446Smrj ASSERT((nbcfg & wabits) != wabits); 889359Skucharsk } 8903446Smrj #else 8913446Smrj workaround_warning(cpu, 131); 8923446Smrj missing++; 893359Skucharsk #endif 8943446Smrj /*CONSTANTCONDITION*/ 8953446Smrj } while (0); 896938Sesaxe 897938Sesaxe /* 8983446Smrj * This isn't really an erratum, but for convenience the 899938Sesaxe * detection/workaround code lives here and in cpuid_opteron_erratum. 900938Sesaxe */ 901938Sesaxe if (cpuid_opteron_erratum(cpu, 6336786) > 0) { 9023446Smrj #if defined(OPTERON_WORKAROUND_6336786) 903938Sesaxe /* 904938Sesaxe * Disable C1-Clock ramping on multi-core/multi-processor 905938Sesaxe * K8 platforms to guard against TSC drift. 906938Sesaxe */ 907938Sesaxe if (opteron_workaround_6336786) { 908938Sesaxe opteron_workaround_6336786++; 909938Sesaxe } else if ((lgrp_plat_node_cnt * 9103446Smrj cpuid_get_ncpu_per_chip(cpu) > 1) || 911938Sesaxe opteron_workaround_6336786_UP) { 9123446Smrj int node; 9133446Smrj uint8_t data; 9143446Smrj 915938Sesaxe for (node = 0; node < lgrp_plat_node_cnt; node++) { 916938Sesaxe /* 917938Sesaxe * Clear PMM7[1:0] (function 3, offset 0x87) 918938Sesaxe * Northbridge device is the node id + 24. 919938Sesaxe */ 920938Sesaxe data = pci_getb_func(0, node + 24, 3, 0x87); 921938Sesaxe data &= 0xFC; 922938Sesaxe pci_putb_func(0, node + 24, 3, 0x87, data); 923938Sesaxe } 924938Sesaxe opteron_workaround_6336786++; 925938Sesaxe } 9263446Smrj #else 9273446Smrj workaround_warning(cpu, 6336786); 9283446Smrj missing++; 929938Sesaxe #endif 9303446Smrj } 9311582Skchow 9321582Skchow /*LINTED*/ 9331582Skchow /* 9341582Skchow * Mutex primitives don't work as expected. 9351582Skchow */ 9361582Skchow if (cpuid_opteron_erratum(cpu, 6323525) > 0) { 9373446Smrj #if defined(OPTERON_WORKAROUND_6323525) 9381582Skchow /* 9393446Smrj * This problem only occurs with 2 or more cores. If bit in 9401582Skchow * MSR_BU_CFG set, then not applicable. The workaround 9411582Skchow * is to patch the semaphone routines with the lfence 9421582Skchow * instruction to provide necessary load memory barrier with 9431582Skchow * possible subsequent read-modify-write ops. 9441582Skchow * 9451582Skchow * It is too early in boot to call the patch routine so 9461582Skchow * set erratum variable to be done in startup_end(). 9471582Skchow */ 9481582Skchow if (opteron_workaround_6323525) { 9491582Skchow opteron_workaround_6323525++; 9501582Skchow } else if ((x86_feature & X86_SSE2) && ((lgrp_plat_node_cnt * 9513446Smrj cpuid_get_ncpu_per_chip(cpu)) > 1)) { 9521582Skchow if ((xrdmsr(MSR_BU_CFG) & 0x02) == 0) 9531582Skchow opteron_workaround_6323525++; 9541582Skchow } 9553446Smrj #else 9563446Smrj workaround_warning(cpu, 6323525); 9573446Smrj missing++; 9583446Smrj #endif 9591582Skchow } 9603446Smrj 9610Sstevel@tonic-gate return (missing); 9620Sstevel@tonic-gate } 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate void 9650Sstevel@tonic-gate workaround_errata_end() 9660Sstevel@tonic-gate { 9673446Smrj #if defined(OPTERON_ERRATUM_88) 9683446Smrj if (opteron_erratum_88) 9693446Smrj workaround_applied(88); 9703446Smrj #endif 9713446Smrj #if defined(OPTERON_ERRATUM_91) 9723446Smrj if (opteron_erratum_91) 9733446Smrj workaround_applied(91); 9743446Smrj #endif 9753446Smrj #if defined(OPTERON_ERRATUM_93) 9763446Smrj if (opteron_erratum_93) 9773446Smrj workaround_applied(93); 9783446Smrj #endif 9793446Smrj #if defined(OPTERON_ERRATUM_95) 9803446Smrj if (opteron_erratum_95) 9813446Smrj workaround_applied(95); 9823446Smrj #endif 9833446Smrj #if defined(OPTERON_ERRATUM_100) 9843446Smrj if (opteron_erratum_100) 9853446Smrj workaround_applied(100); 9863446Smrj #endif 9873446Smrj #if defined(OPTERON_ERRATUM_108) 9883446Smrj if (opteron_erratum_108) 9893446Smrj workaround_applied(108); 9903446Smrj #endif 9910Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_109) 9920Sstevel@tonic-gate if (opteron_erratum_109) { 993359Skucharsk cmn_err(CE_WARN, 994359Skucharsk "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)" 995359Skucharsk " processor\nerratum 109 was not detected; updating your" 996359Skucharsk " system's BIOS to a version\ncontaining this" 997359Skucharsk " microcode patch is HIGHLY recommended or erroneous" 998359Skucharsk " system\noperation may occur.\n"); 9990Sstevel@tonic-gate } 10003446Smrj #endif 10013446Smrj #if defined(OPTERON_ERRATUM_121) 10023446Smrj if (opteron_erratum_121) 10033446Smrj workaround_applied(121); 10043446Smrj #endif 10053446Smrj #if defined(OPTERON_ERRATUM_122) 10063446Smrj if (opteron_erratum_122) 10073446Smrj workaround_applied(122); 10083446Smrj #endif 10090Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_123) 10100Sstevel@tonic-gate if (opteron_erratum_123) { 1011359Skucharsk cmn_err(CE_WARN, 1012359Skucharsk "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)" 1013359Skucharsk " processor\nerratum 123 was not detected; updating your" 1014359Skucharsk " system's BIOS to a version\ncontaining this" 1015359Skucharsk " microcode patch is HIGHLY recommended or erroneous" 1016359Skucharsk " system\noperation may occur.\n"); 10170Sstevel@tonic-gate } 10183446Smrj #endif 1019359Skucharsk #if defined(OPTERON_ERRATUM_131) 1020359Skucharsk if (opteron_erratum_131) { 1021359Skucharsk cmn_err(CE_WARN, 1022359Skucharsk "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)" 1023359Skucharsk " processor\nerratum 131 was not detected; updating your" 1024359Skucharsk " system's BIOS to a version\ncontaining this" 1025359Skucharsk " microcode patch is HIGHLY recommended or erroneous" 1026359Skucharsk " system\noperation may occur.\n"); 1027359Skucharsk } 10283446Smrj #endif 10293446Smrj #if defined(OPTERON_WORKAROUND_6336786) 10303446Smrj if (opteron_workaround_6336786) 10313446Smrj workaround_applied(6336786); 10323446Smrj #endif 10333446Smrj #if defined(OPTERON_WORKAROUND_6323525) 10343446Smrj if (opteron_workaround_6323525) 10353446Smrj workaround_applied(6323525); 10363446Smrj #endif 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate 10393446Smrj static cpuset_t procset; 10403446Smrj 10413446Smrj /* 10423446Smrj * Start a single cpu, assuming that the kernel context is available 10433446Smrj * to successfully start another cpu. 10443446Smrj * 10453446Smrj * (For example, real mode code is mapped into the right place 10463446Smrj * in memory and is ready to be run.) 10473446Smrj */ 10483446Smrj int 10493446Smrj start_cpu(processorid_t who) 10503446Smrj { 10513446Smrj void *ctx; 10523446Smrj cpu_t *cp; 10533446Smrj int delays; 10543446Smrj int error = 0; 10553446Smrj 10563446Smrj ASSERT(who != 0); 10573446Smrj 10583446Smrj /* 10593446Smrj * Check if there's at least a Mbyte of kmem available 10603446Smrj * before attempting to start the cpu. 10613446Smrj */ 10623446Smrj if (kmem_avail() < 1024 * 1024) { 10633446Smrj /* 10643446Smrj * Kick off a reap in case that helps us with 10653446Smrj * later attempts .. 10663446Smrj */ 10673446Smrj kmem_reap(); 10683446Smrj return (ENOMEM); 10693446Smrj } 10703446Smrj 10713446Smrj cp = mp_startup_init(who); 10723446Smrj if ((ctx = mach_cpucontext_alloc(cp)) == NULL || 10733446Smrj (error = mach_cpu_start(cp, ctx)) != 0) { 10743446Smrj 10753446Smrj /* 10763446Smrj * Something went wrong before we even started it 10773446Smrj */ 10783446Smrj if (ctx) 10793446Smrj cmn_err(CE_WARN, 10803446Smrj "cpu%d: failed to start error %d", 10813446Smrj cp->cpu_id, error); 10823446Smrj else 10833446Smrj cmn_err(CE_WARN, 10843446Smrj "cpu%d: failed to allocate context", cp->cpu_id); 10850Sstevel@tonic-gate 10863446Smrj if (ctx) 10873446Smrj mach_cpucontext_free(cp, ctx, error); 10883446Smrj else 10893446Smrj error = EAGAIN; /* hmm. */ 10903446Smrj mp_startup_fini(cp, error); 10913446Smrj return (error); 10923446Smrj } 10933446Smrj 10943446Smrj for (delays = 0; !CPU_IN_SET(procset, who); delays++) { 10953446Smrj if (delays == 500) { 10963446Smrj /* 10973446Smrj * After five seconds, things are probably looking 10983446Smrj * a bit bleak - explain the hang. 10993446Smrj */ 11003446Smrj cmn_err(CE_NOTE, "cpu%d: started, " 11013446Smrj "but not running in the kernel yet", who); 11023446Smrj } else if (delays > 2000) { 11033446Smrj /* 11043446Smrj * We waited at least 20 seconds, bail .. 11053446Smrj */ 11063446Smrj error = ETIMEDOUT; 11073446Smrj cmn_err(CE_WARN, "cpu%d: timed out", who); 11083446Smrj mach_cpucontext_free(cp, ctx, error); 11093446Smrj mp_startup_fini(cp, error); 11103446Smrj return (error); 11113446Smrj } 11123446Smrj 11133446Smrj /* 11143446Smrj * wait at least 10ms, then check again.. 11153446Smrj */ 11163446Smrj delay(USEC_TO_TICK_ROUNDUP(10000)); 11173446Smrj } 11183446Smrj 11193446Smrj mach_cpucontext_free(cp, ctx, 0); 11203446Smrj 11213446Smrj if (tsc_gethrtime_enable) 11223446Smrj tsc_sync_master(who); 11233446Smrj 11243446Smrj if (dtrace_cpu_init != NULL) { 11253446Smrj /* 11263446Smrj * DTrace CPU initialization expects cpu_lock to be held. 11273446Smrj */ 11283446Smrj mutex_enter(&cpu_lock); 11293446Smrj (*dtrace_cpu_init)(who); 11303446Smrj mutex_exit(&cpu_lock); 11313446Smrj } 11323446Smrj 11333446Smrj while (!CPU_IN_SET(cpu_ready_set, who)) 11343446Smrj delay(1); 11353446Smrj 11363446Smrj return (0); 11373446Smrj } 11383446Smrj 11392006Sandrei 11400Sstevel@tonic-gate /*ARGSUSED*/ 11410Sstevel@tonic-gate void 11420Sstevel@tonic-gate start_other_cpus(int cprboot) 11430Sstevel@tonic-gate { 11443446Smrj uint_t who; 11453446Smrj uint_t skipped = 0; 11463446Smrj uint_t bootcpuid = 0; 11470Sstevel@tonic-gate 11480Sstevel@tonic-gate /* 11490Sstevel@tonic-gate * Initialize our own cpu_info. 11500Sstevel@tonic-gate */ 11510Sstevel@tonic-gate init_cpu_info(CPU); 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate /* 11540Sstevel@tonic-gate * Initialize our syscall handlers 11550Sstevel@tonic-gate */ 11560Sstevel@tonic-gate init_cpu_syscall(CPU); 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate /* 11593446Smrj * Take the boot cpu out of the mp_cpus set because we know 11603446Smrj * it's already running. Add it to the cpu_ready_set for 11613446Smrj * precisely the same reason. 11623446Smrj */ 11633446Smrj CPUSET_DEL(mp_cpus, bootcpuid); 11643446Smrj CPUSET_ADD(cpu_ready_set, bootcpuid); 11653446Smrj 11663446Smrj /* 11670Sstevel@tonic-gate * if only 1 cpu or not using MP, skip the rest of this 11680Sstevel@tonic-gate */ 11693446Smrj if (CPUSET_ISNULL(mp_cpus) || use_mp == 0) { 11700Sstevel@tonic-gate if (use_mp == 0) 11710Sstevel@tonic-gate cmn_err(CE_CONT, "?***** Not in MP mode\n"); 11720Sstevel@tonic-gate goto done; 11730Sstevel@tonic-gate } 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate /* 11760Sstevel@tonic-gate * perform such initialization as is needed 11770Sstevel@tonic-gate * to be able to take CPUs on- and off-line. 11780Sstevel@tonic-gate */ 11790Sstevel@tonic-gate cpu_pause_init(); 11800Sstevel@tonic-gate 11810Sstevel@tonic-gate xc_init(); /* initialize processor crosscalls */ 11820Sstevel@tonic-gate 11833446Smrj if (mach_cpucontext_init() != 0) 11840Sstevel@tonic-gate goto done; 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate flushes_require_xcalls = 1; 11870Sstevel@tonic-gate 11882575Snf202958 /* 11892575Snf202958 * We lock our affinity to the master CPU to ensure that all slave CPUs 11902575Snf202958 * do their TSC syncs with the same CPU. 11912575Snf202958 */ 11920Sstevel@tonic-gate affinity_set(CPU_CURRENT); 11930Sstevel@tonic-gate 11940Sstevel@tonic-gate for (who = 0; who < NCPU; who++) { 11952575Snf202958 11962006Sandrei if (!CPU_IN_SET(mp_cpus, who)) 11972006Sandrei continue; 11983446Smrj ASSERT(who != bootcpuid); 11992006Sandrei if (ncpus >= max_ncpus) { 12002006Sandrei skipped = who; 12010Sstevel@tonic-gate continue; 12022006Sandrei } 12033446Smrj if (start_cpu(who) != 0) 12043446Smrj CPUSET_DEL(mp_cpus, who); 12050Sstevel@tonic-gate } 12060Sstevel@tonic-gate 12074581Ssherrym /* Free the space allocated to hold the microcode file */ 12084581Ssherrym ucode_free(); 12094581Ssherrym 12100Sstevel@tonic-gate affinity_clear(); 12110Sstevel@tonic-gate 12122006Sandrei if (skipped) { 12132006Sandrei cmn_err(CE_NOTE, 12143446Smrj "System detected %d cpus, but " 12153446Smrj "only %d cpu(s) were enabled during boot.", 12162006Sandrei skipped + 1, ncpus); 12172006Sandrei cmn_err(CE_NOTE, 12182006Sandrei "Use \"boot-ncpus\" parameter to enable more CPU(s). " 12192006Sandrei "See eeprom(1M)."); 12202006Sandrei } 12212006Sandrei 12220Sstevel@tonic-gate done: 12230Sstevel@tonic-gate workaround_errata_end(); 12243446Smrj mach_cpucontext_fini(); 12251642Sgavinm 12261642Sgavinm cmi_post_mpstartup(); 12270Sstevel@tonic-gate } 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate /* 12300Sstevel@tonic-gate * Dummy functions - no i86pc platforms support dynamic cpu allocation. 12310Sstevel@tonic-gate */ 12320Sstevel@tonic-gate /*ARGSUSED*/ 12330Sstevel@tonic-gate int 12340Sstevel@tonic-gate mp_cpu_configure(int cpuid) 12350Sstevel@tonic-gate { 12360Sstevel@tonic-gate return (ENOTSUP); /* not supported */ 12370Sstevel@tonic-gate } 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate /*ARGSUSED*/ 12400Sstevel@tonic-gate int 12410Sstevel@tonic-gate mp_cpu_unconfigure(int cpuid) 12420Sstevel@tonic-gate { 12430Sstevel@tonic-gate return (ENOTSUP); /* not supported */ 12440Sstevel@tonic-gate } 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate /* 12470Sstevel@tonic-gate * Startup function for 'other' CPUs (besides boot cpu). 12482985Sdmick * Called from real_mode_start. 12491251Skchow * 12501251Skchow * WARNING: until CPU_READY is set, mp_startup and routines called by 12511251Skchow * mp_startup should not call routines (e.g. kmem_free) that could call 12521251Skchow * hat_unload which requires CPU_READY to be set. 12530Sstevel@tonic-gate */ 12540Sstevel@tonic-gate void 12550Sstevel@tonic-gate mp_startup(void) 12560Sstevel@tonic-gate { 12570Sstevel@tonic-gate struct cpu *cp = CPU; 12580Sstevel@tonic-gate uint_t new_x86_feature; 12590Sstevel@tonic-gate 12602985Sdmick /* 12613021Sdmick * We need to get TSC on this proc synced (i.e., any delta 12623021Sdmick * from cpu0 accounted for) as soon as we can, because many 12633021Sdmick * many things use gethrtime/pc_gethrestime, including 12643021Sdmick * interrupts, cmn_err, etc. 12653021Sdmick */ 12663021Sdmick 12673021Sdmick /* Let cpu0 continue into tsc_sync_master() */ 12683021Sdmick CPUSET_ATOMIC_ADD(procset, cp->cpu_id); 12693021Sdmick 12703021Sdmick if (tsc_gethrtime_enable) 12713021Sdmick tsc_sync_slave(); 12723021Sdmick 12733021Sdmick /* 12742985Sdmick * Once this was done from assembly, but it's safer here; if 12752985Sdmick * it blocks, we need to be able to swtch() to and from, and 12762985Sdmick * since we get here by calling t_pc, we need to do that call 12772985Sdmick * before swtch() overwrites it. 12782985Sdmick */ 12792985Sdmick 12802985Sdmick (void) (*ap_mlsetup)(); 12812985Sdmick 12820Sstevel@tonic-gate new_x86_feature = cpuid_pass1(cp); 12830Sstevel@tonic-gate 12840Sstevel@tonic-gate /* 12850Sstevel@tonic-gate * We need to Sync MTRR with cpu0's MTRR. We have to do 12860Sstevel@tonic-gate * this with interrupts disabled. 12870Sstevel@tonic-gate */ 12880Sstevel@tonic-gate if (x86_feature & X86_MTRR) 12890Sstevel@tonic-gate mtrr_sync(); 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate /* 12923446Smrj * Set up TSC_AUX to contain the cpuid for this processor 12933446Smrj * for the rdtscp instruction. 12943446Smrj */ 12953446Smrj if (x86_feature & X86_TSCP) 12963446Smrj (void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id); 12973446Smrj 12983446Smrj /* 12990Sstevel@tonic-gate * Initialize this CPU's syscall handlers 13000Sstevel@tonic-gate */ 13010Sstevel@tonic-gate init_cpu_syscall(cp); 13020Sstevel@tonic-gate 13030Sstevel@tonic-gate /* 13040Sstevel@tonic-gate * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the 13050Sstevel@tonic-gate * highest level at which a routine is permitted to block on 13060Sstevel@tonic-gate * an adaptive mutex (allows for cpu poke interrupt in case 13070Sstevel@tonic-gate * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks 13080Sstevel@tonic-gate * device interrupts that may end up in the hat layer issuing cross 13090Sstevel@tonic-gate * calls before CPU_READY is set. 13100Sstevel@tonic-gate */ 13113446Smrj splx(ipltospl(LOCK_LEVEL)); 13123446Smrj sti(); 13130Sstevel@tonic-gate 13140Sstevel@tonic-gate /* 13150Sstevel@tonic-gate * Do a sanity check to make sure this new CPU is a sane thing 13160Sstevel@tonic-gate * to add to the collection of processors running this system. 13170Sstevel@tonic-gate * 13180Sstevel@tonic-gate * XXX Clearly this needs to get more sophisticated, if x86 13190Sstevel@tonic-gate * systems start to get built out of heterogenous CPUs; as is 13200Sstevel@tonic-gate * likely to happen once the number of processors in a configuration 13210Sstevel@tonic-gate * gets large enough. 13220Sstevel@tonic-gate */ 13230Sstevel@tonic-gate if ((x86_feature & new_x86_feature) != x86_feature) { 13240Sstevel@tonic-gate cmn_err(CE_CONT, "?cpu%d: %b\n", 13250Sstevel@tonic-gate cp->cpu_id, new_x86_feature, FMT_X86_FEATURE); 13260Sstevel@tonic-gate cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id); 13270Sstevel@tonic-gate } 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate /* 13304481Sbholler * We do not support cpus with mixed monitor/mwait support if the 13314481Sbholler * boot cpu supports monitor/mwait. 13324481Sbholler */ 13334481Sbholler if ((x86_feature & ~new_x86_feature) & X86_MWAIT) 13344481Sbholler panic("unsupported mixed cpu monitor/mwait support detected"); 13354481Sbholler 13364481Sbholler /* 13370Sstevel@tonic-gate * We could be more sophisticated here, and just mark the CPU 13380Sstevel@tonic-gate * as "faulted" but at this point we'll opt for the easier 13390Sstevel@tonic-gate * answer of dieing horribly. Provided the boot cpu is ok, 13400Sstevel@tonic-gate * the system can be recovered by booting with use_mp set to zero. 13410Sstevel@tonic-gate */ 13420Sstevel@tonic-gate if (workaround_errata(cp) != 0) 13430Sstevel@tonic-gate panic("critical workaround(s) missing for cpu%d", cp->cpu_id); 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate cpuid_pass2(cp); 13460Sstevel@tonic-gate cpuid_pass3(cp); 13470Sstevel@tonic-gate (void) cpuid_pass4(cp); 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate init_cpu_info(cp); 13500Sstevel@tonic-gate 13510Sstevel@tonic-gate mutex_enter(&cpu_lock); 13520Sstevel@tonic-gate /* 13533434Sesaxe * Processor group initialization for this CPU is dependent on the 13543434Sesaxe * cpuid probing, which must be done in the context of the current 13553434Sesaxe * CPU. 13560Sstevel@tonic-gate */ 13573434Sesaxe pghw_physid_create(cp); 13583434Sesaxe pg_cpu_init(cp); 13593434Sesaxe pg_cmt_cpu_startup(cp); 13600Sstevel@tonic-gate 13610Sstevel@tonic-gate cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_ENABLE | CPU_EXISTS; 13620Sstevel@tonic-gate cpu_add_active(cp); 13632575Snf202958 13642575Snf202958 if (dtrace_cpu_init != NULL) { 13652575Snf202958 (*dtrace_cpu_init)(cp->cpu_id); 13662575Snf202958 } 13672575Snf202958 13684581Ssherrym /* 13694581Ssherrym * Fill out cpu_ucode_info. Update microcode if necessary. 13704581Ssherrym */ 13714581Ssherrym ucode_check(cp); 13724581Ssherrym 13730Sstevel@tonic-gate mutex_exit(&cpu_lock); 13740Sstevel@tonic-gate 13753029Ssethg /* 13763029Ssethg * Enable preemption here so that contention for any locks acquired 13773029Ssethg * later in mp_startup may be preempted if the thread owning those 13783029Ssethg * locks is continously executing on other CPUs (for example, this 13793029Ssethg * CPU must be preemptible to allow other CPUs to pause it during their 13803029Ssethg * startup phases). It's safe to enable preemption here because the 13813029Ssethg * CPU state is pretty-much fully constructed. 13823029Ssethg */ 13833029Ssethg curthread->t_preempt = 0; 13843029Ssethg 13851251Skchow add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi); 13861251Skchow 13871482Ssethg /* The base spl should still be at LOCK LEVEL here */ 13881482Ssethg ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL)); 13891482Ssethg set_base_spl(); /* Restore the spl to its proper value */ 13901482Ssethg 13910Sstevel@tonic-gate (void) spl0(); /* enable interrupts */ 13920Sstevel@tonic-gate 13931414Scindi /* 13941414Scindi * Set up the CPU module for this CPU. This can't be done before 13951414Scindi * this CPU is made CPU_READY, because we may (in heterogeneous systems) 13961414Scindi * need to go load another CPU module. The act of attempting to load 13971414Scindi * a module may trigger a cross-call, which will ASSERT unless this 13981414Scindi * cpu is CPU_READY. 13991414Scindi */ 14001414Scindi cmi_init(); 14011414Scindi 14021414Scindi if (x86_feature & X86_MCA) 14031414Scindi cmi_mca_init(); 14041414Scindi 14050Sstevel@tonic-gate if (boothowto & RB_DEBUG) 14063446Smrj kdi_cpu_init(); 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate /* 14090Sstevel@tonic-gate * Setting the bit in cpu_ready_set must be the last operation in 14100Sstevel@tonic-gate * processor initialization; the boot CPU will continue to boot once 14110Sstevel@tonic-gate * it sees this bit set for all active CPUs. 14120Sstevel@tonic-gate */ 14130Sstevel@tonic-gate CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id); 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate /* 14160Sstevel@tonic-gate * Because mp_startup() gets fired off after init() starts, we 14170Sstevel@tonic-gate * can't use the '?' trick to do 'boot -v' printing - so we 14180Sstevel@tonic-gate * always direct the 'cpu .. online' messages to the log. 14190Sstevel@tonic-gate */ 14200Sstevel@tonic-gate cmn_err(CE_CONT, "!cpu%d initialization complete - online\n", 14210Sstevel@tonic-gate cp->cpu_id); 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate /* 14240Sstevel@tonic-gate * Now we are done with the startup thread, so free it up. 14250Sstevel@tonic-gate */ 14260Sstevel@tonic-gate thread_exit(); 14270Sstevel@tonic-gate panic("mp_startup: cannot return"); 14280Sstevel@tonic-gate /*NOTREACHED*/ 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate /* 14330Sstevel@tonic-gate * Start CPU on user request. 14340Sstevel@tonic-gate */ 14350Sstevel@tonic-gate /* ARGSUSED */ 14360Sstevel@tonic-gate int 14370Sstevel@tonic-gate mp_cpu_start(struct cpu *cp) 14380Sstevel@tonic-gate { 14390Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 14400Sstevel@tonic-gate return (0); 14410Sstevel@tonic-gate } 14420Sstevel@tonic-gate 14430Sstevel@tonic-gate /* 14440Sstevel@tonic-gate * Stop CPU on user request. 14450Sstevel@tonic-gate */ 14460Sstevel@tonic-gate /* ARGSUSED */ 14470Sstevel@tonic-gate int 14480Sstevel@tonic-gate mp_cpu_stop(struct cpu *cp) 14490Sstevel@tonic-gate { 14501389Sdmick extern int cbe_psm_timer_mode; 14510Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 14521389Sdmick 14531389Sdmick /* 14541389Sdmick * If TIMER_PERIODIC mode is used, CPU0 is the one running it; 14551389Sdmick * can't stop it. (This is true only for machines with no TSC.) 14561389Sdmick */ 14571389Sdmick 14581389Sdmick if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0)) 14591389Sdmick return (1); 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate return (0); 14620Sstevel@tonic-gate } 14630Sstevel@tonic-gate 14640Sstevel@tonic-gate /* 14650Sstevel@tonic-gate * Take the specified CPU out of participation in interrupts. 14660Sstevel@tonic-gate */ 14670Sstevel@tonic-gate int 14680Sstevel@tonic-gate cpu_disable_intr(struct cpu *cp) 14690Sstevel@tonic-gate { 14700Sstevel@tonic-gate if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS) 14710Sstevel@tonic-gate return (EBUSY); 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate cp->cpu_flags &= ~CPU_ENABLE; 14740Sstevel@tonic-gate return (0); 14750Sstevel@tonic-gate } 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate /* 14780Sstevel@tonic-gate * Allow the specified CPU to participate in interrupts. 14790Sstevel@tonic-gate */ 14800Sstevel@tonic-gate void 14810Sstevel@tonic-gate cpu_enable_intr(struct cpu *cp) 14820Sstevel@tonic-gate { 14830Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 14840Sstevel@tonic-gate cp->cpu_flags |= CPU_ENABLE; 14850Sstevel@tonic-gate psm_enable_intr(cp->cpu_id); 14860Sstevel@tonic-gate } 14870Sstevel@tonic-gate 14880Sstevel@tonic-gate 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate void 14910Sstevel@tonic-gate mp_cpu_faulted_enter(struct cpu *cp) 14921414Scindi { 14931414Scindi cmi_faulted_enter(cp); 14941414Scindi } 14950Sstevel@tonic-gate 14960Sstevel@tonic-gate void 14970Sstevel@tonic-gate mp_cpu_faulted_exit(struct cpu *cp) 14981414Scindi { 14991414Scindi cmi_faulted_exit(cp); 15001414Scindi } 15010Sstevel@tonic-gate 15020Sstevel@tonic-gate /* 15030Sstevel@tonic-gate * The following two routines are used as context operators on threads belonging 15040Sstevel@tonic-gate * to processes with a private LDT (see sysi86). Due to the rarity of such 15050Sstevel@tonic-gate * processes, these routines are currently written for best code readability and 15060Sstevel@tonic-gate * organization rather than speed. We could avoid checking x86_feature at every 15070Sstevel@tonic-gate * context switch by installing different context ops, depending on the 15080Sstevel@tonic-gate * x86_feature flags, at LDT creation time -- one for each combination of fast 15090Sstevel@tonic-gate * syscall feature flags. 15100Sstevel@tonic-gate */ 15110Sstevel@tonic-gate 15120Sstevel@tonic-gate /*ARGSUSED*/ 15130Sstevel@tonic-gate void 15140Sstevel@tonic-gate cpu_fast_syscall_disable(void *arg) 15150Sstevel@tonic-gate { 15163446Smrj if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP)) 15170Sstevel@tonic-gate cpu_sep_disable(); 15183446Smrj if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC)) 15190Sstevel@tonic-gate cpu_asysc_disable(); 15200Sstevel@tonic-gate } 15210Sstevel@tonic-gate 15220Sstevel@tonic-gate /*ARGSUSED*/ 15230Sstevel@tonic-gate void 15240Sstevel@tonic-gate cpu_fast_syscall_enable(void *arg) 15250Sstevel@tonic-gate { 15263446Smrj if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP)) 15270Sstevel@tonic-gate cpu_sep_enable(); 15283446Smrj if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC)) 15290Sstevel@tonic-gate cpu_asysc_enable(); 15300Sstevel@tonic-gate } 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate static void 15330Sstevel@tonic-gate cpu_sep_enable(void) 15340Sstevel@tonic-gate { 15350Sstevel@tonic-gate ASSERT(x86_feature & X86_SEP); 15360Sstevel@tonic-gate ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL); 15370Sstevel@tonic-gate 1538770Skucharsk wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL); 15390Sstevel@tonic-gate } 15400Sstevel@tonic-gate 15410Sstevel@tonic-gate static void 15420Sstevel@tonic-gate cpu_sep_disable(void) 15430Sstevel@tonic-gate { 15440Sstevel@tonic-gate ASSERT(x86_feature & X86_SEP); 15450Sstevel@tonic-gate ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL); 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate /* 15480Sstevel@tonic-gate * Setting the SYSENTER_CS_MSR register to 0 causes software executing 15490Sstevel@tonic-gate * the sysenter or sysexit instruction to trigger a #gp fault. 15500Sstevel@tonic-gate */ 15513446Smrj wrmsr(MSR_INTC_SEP_CS, 0); 15520Sstevel@tonic-gate } 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate static void 15550Sstevel@tonic-gate cpu_asysc_enable(void) 15560Sstevel@tonic-gate { 15570Sstevel@tonic-gate ASSERT(x86_feature & X86_ASYSC); 15580Sstevel@tonic-gate ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL); 15590Sstevel@tonic-gate 1560770Skucharsk wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) | 1561770Skucharsk (uint64_t)(uintptr_t)AMD_EFER_SCE); 15620Sstevel@tonic-gate } 15630Sstevel@tonic-gate 15640Sstevel@tonic-gate static void 15650Sstevel@tonic-gate cpu_asysc_disable(void) 15660Sstevel@tonic-gate { 15670Sstevel@tonic-gate ASSERT(x86_feature & X86_ASYSC); 15680Sstevel@tonic-gate ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL); 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate /* 15710Sstevel@tonic-gate * Turn off the SCE (syscall enable) bit in the EFER register. Software 15720Sstevel@tonic-gate * executing syscall or sysret with this bit off will incur a #ud trap. 15730Sstevel@tonic-gate */ 1574770Skucharsk wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) & 1575770Skucharsk ~((uint64_t)(uintptr_t)AMD_EFER_SCE)); 15760Sstevel@tonic-gate } 1577