xref: /illumos-gate/usr/src/uts/i86pc/os/mp_startup.c (revision 29e362da24db33a6650152985ef5626b4e6a810f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/thread.h>
30 #include <sys/cpuvar.h>
31 #include <sys/t_lock.h>
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/disp.h>
35 #include <sys/mmu.h>
36 #include <sys/class.h>
37 #include <sys/cmn_err.h>
38 #include <sys/debug.h>
39 #include <sys/asm_linkage.h>
40 #include <sys/x_call.h>
41 #include <sys/systm.h>
42 #include <sys/var.h>
43 #include <sys/vtrace.h>
44 #include <vm/hat.h>
45 #include <sys/mmu.h>
46 #include <vm/as.h>
47 #include <vm/seg_kmem.h>
48 #include <sys/segments.h>
49 #include <sys/kmem.h>
50 #include <sys/stack.h>
51 #include <sys/smp_impldefs.h>
52 #include <sys/x86_archext.h>
53 #include <sys/machsystm.h>
54 #include <sys/traptrace.h>
55 #include <sys/clock.h>
56 #include <sys/cpc_impl.h>
57 #include <sys/chip.h>
58 #include <sys/dtrace.h>
59 #include <sys/archsystm.h>
60 #include <sys/fp.h>
61 #include <sys/reboot.h>
62 #include <sys/kdi.h>
63 #include <vm/hat_i86.h>
64 #include <sys/memnode.h>
65 #include <sys/pci_cfgspace.h>
66 #include <sys/cpu_module.h>
67 
68 struct cpu	cpus[1];			/* CPU data */
69 struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
70 cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
71 
72 /*
73  * Useful for disabling MP bring-up for an MP capable kernel
74  * (a kernel that was built with MP defined)
75  */
76 int use_mp = 1;
77 
78 int mp_cpus = 0x1;	/* to be set by platform specific module	*/
79 
80 /*
81  * This variable is used by the hat layer to decide whether or not
82  * critical sections are needed to prevent race conditions.  For sun4m,
83  * this variable is set once enough MP initialization has been done in
84  * order to allow cross calls.
85  */
86 int flushes_require_xcalls = 0;
87 ulong_t	cpu_ready_set = 1;
88 
89 extern	void	real_mode_start(void);
90 extern	void	real_mode_end(void);
91 static 	void	mp_startup(void);
92 
93 static void cpu_sep_enable(void);
94 static void cpu_sep_disable(void);
95 static void cpu_asysc_enable(void);
96 static void cpu_asysc_disable(void);
97 
98 extern int tsc_gethrtime_enable;
99 
100 /*
101  * Init CPU info - get CPU type info for processor_info system call.
102  */
103 void
104 init_cpu_info(struct cpu *cp)
105 {
106 	processor_info_t *pi = &cp->cpu_type_info;
107 	char buf[CPU_IDSTRLEN];
108 
109 	/*
110 	 * Get clock-frequency property for the CPU.
111 	 */
112 	pi->pi_clock = cpu_freq;
113 
114 	(void) strcpy(pi->pi_processor_type, "i386");
115 	if (fpu_exists)
116 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
117 
118 	(void) cpuid_getidstr(cp, buf, sizeof (buf));
119 
120 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
121 	(void) strcpy(cp->cpu_idstr, buf);
122 
123 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
124 
125 	(void) cpuid_getbrandstr(cp, buf, sizeof (buf));
126 	cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
127 	(void) strcpy(cp->cpu_brandstr, buf);
128 
129 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
130 }
131 
132 /*
133  * Configure syscall support on this CPU.
134  */
135 /*ARGSUSED*/
136 static void
137 init_cpu_syscall(struct cpu *cp)
138 {
139 	kpreempt_disable();
140 
141 #if defined(__amd64)
142 	if (x86_feature & X86_ASYSC) {
143 
144 #if !defined(__lint)
145 		/*
146 		 * The syscall instruction imposes a certain ordering on
147 		 * segment selectors, so we double-check that ordering
148 		 * here.
149 		 */
150 		ASSERT(KDS_SEL == KCS_SEL + 8);
151 		ASSERT(UDS_SEL == U32CS_SEL + 8);
152 		ASSERT(UCS_SEL == U32CS_SEL + 16);
153 #endif
154 		/*
155 		 * Turn syscall/sysret extensions on.
156 		 */
157 		cpu_asysc_enable();
158 
159 		/*
160 		 * Program the magic registers ..
161 		 */
162 		wrmsr(MSR_AMD_STAR, ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) <<
163 		    32);
164 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
165 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
166 
167 		/*
168 		 * This list of flags is masked off the incoming
169 		 * %rfl when we enter the kernel.
170 		 */
171 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
172 	}
173 #endif
174 
175 	/*
176 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
177 	 * hard to use syscall/sysret, and it is more portable anyway.
178 	 *
179 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
180 	 * variant isn't available to 32-bit applications, but sysenter is.
181 	 */
182 	if (x86_feature & X86_SEP) {
183 
184 #if !defined(__lint)
185 		/*
186 		 * The sysenter instruction imposes a certain ordering on
187 		 * segment selectors, so we double-check that ordering
188 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
189 		 * Intel Architecture Software Developer's Manual Volume 2:
190 		 * Instruction Set Reference"
191 		 */
192 		ASSERT(KDS_SEL == KCS_SEL + 8);
193 
194 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
195 		ASSERT32(UDS_SEL == UCS_SEL + 8);
196 
197 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
198 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
199 #endif
200 
201 		cpu_sep_enable();
202 
203 		/*
204 		 * resume() sets this value to the base of the threads stack
205 		 * via a context handler.
206 		 */
207 		wrmsr(MSR_INTC_SEP_ESP, 0ULL);
208 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
209 	}
210 
211 	kpreempt_enable();
212 }
213 
214 /*
215  * Multiprocessor initialization.
216  *
217  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
218  * startup and idle threads for the specified CPU.
219  */
220 static void
221 mp_startup_init(int cpun)
222 {
223 #if defined(__amd64)
224 extern void *long_mode_64(void);
225 #endif	/* __amd64 */
226 
227 	struct cpu *cp;
228 	struct tss *ntss;
229 	kthread_id_t tp;
230 	caddr_t	sp;
231 	int size;
232 	proc_t *procp;
233 	extern void idle();
234 
235 	struct cpu_tables *tablesp;
236 	rm_platter_t *real_mode_platter = (rm_platter_t *)rm_platter_va;
237 
238 #ifdef TRAPTRACE
239 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
240 #endif
241 
242 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
243 
244 	if ((cp = kmem_zalloc(sizeof (*cp), KM_NOSLEEP)) == NULL) {
245 		panic("mp_startup_init: cpu%d: "
246 		    "no memory for cpu structure", cpun);
247 		/*NOTREACHED*/
248 	}
249 	procp = curthread->t_procp;
250 
251 	mutex_enter(&cpu_lock);
252 	/*
253 	 * Initialize the dispatcher first.
254 	 */
255 	disp_cpu_init(cp);
256 	mutex_exit(&cpu_lock);
257 
258 	cpu_vm_data_init(cp);
259 
260 	/*
261 	 * Allocate and initialize the startup thread for this CPU.
262 	 * Interrupt and process switch stacks get allocated later
263 	 * when the CPU starts running.
264 	 */
265 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
266 	    TS_STOPPED, maxclsyspri);
267 
268 	/*
269 	 * Set state to TS_ONPROC since this thread will start running
270 	 * as soon as the CPU comes online.
271 	 *
272 	 * All the other fields of the thread structure are setup by
273 	 * thread_create().
274 	 */
275 	THREAD_ONPROC(tp, cp);
276 	tp->t_preempt = 1;
277 	tp->t_bound_cpu = cp;
278 	tp->t_affinitycnt = 1;
279 	tp->t_cpu = cp;
280 	tp->t_disp_queue = cp->cpu_disp;
281 
282 	/*
283 	 * Setup thread to start in mp_startup.
284 	 */
285 	sp = tp->t_stk;
286 	tp->t_pc = (uintptr_t)mp_startup;
287 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
288 
289 	cp->cpu_id = cpun;
290 	cp->cpu_self = cp;
291 	cp->cpu_mask = 1 << cpun;
292 	cp->cpu_thread = tp;
293 	cp->cpu_lwp = NULL;
294 	cp->cpu_dispthread = tp;
295 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
296 
297 	/*
298 	 * cpu_base_spl must be set explicitly here to prevent any blocking
299 	 * operations in mp_startup from causing the spl of the cpu to drop
300 	 * to 0 (allowing device interrupts before we're ready) in resume().
301 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
302 	 * As an extra bit of security on DEBUG kernels, this is enforced with
303 	 * an assertion in mp_startup() -- before cpu_base_spl is set to its
304 	 * proper value.
305 	 */
306 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
307 
308 	/*
309 	 * Now, initialize per-CPU idle thread for this CPU.
310 	 */
311 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
312 
313 	cp->cpu_idle_thread = tp;
314 
315 	tp->t_preempt = 1;
316 	tp->t_bound_cpu = cp;
317 	tp->t_affinitycnt = 1;
318 	tp->t_cpu = cp;
319 	tp->t_disp_queue = cp->cpu_disp;
320 
321 	/*
322 	 * Bootstrap the CPU for CMT aware scheduling
323 	 * The rest of the initialization will happen from
324 	 * mp_startup()
325 	 */
326 	chip_bootstrap_cpu(cp);
327 
328 	/*
329 	 * Perform CPC intialization on the new CPU.
330 	 */
331 	kcpc_hw_init(cp);
332 
333 	/*
334 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
335 	 * for each CPU.
336 	 */
337 
338 	setup_vaddr_for_ppcopy(cp);
339 
340 	/*
341 	 * Allocate space for page directory, stack, tss, gdt and idt.
342 	 * This assumes that kmem_alloc will return memory which is aligned
343 	 * to the next higher power of 2 or a page(if size > MAXABIG)
344 	 * If this assumption goes wrong at any time due to change in
345 	 * kmem alloc, things may not work as the page directory has to be
346 	 * page aligned
347 	 */
348 	if ((tablesp = kmem_zalloc(sizeof (*tablesp), KM_NOSLEEP)) == NULL)
349 		panic("mp_startup_init: cpu%d cannot allocate tables", cpun);
350 
351 	if ((uintptr_t)tablesp & ~MMU_STD_PAGEMASK) {
352 		kmem_free(tablesp, sizeof (struct cpu_tables));
353 		size = sizeof (struct cpu_tables) + MMU_STD_PAGESIZE;
354 		tablesp = kmem_zalloc(size, KM_NOSLEEP);
355 		tablesp = (struct cpu_tables *)
356 		    (((uintptr_t)tablesp + MMU_STD_PAGESIZE) &
357 		    MMU_STD_PAGEMASK);
358 	}
359 
360 	ntss = cp->cpu_tss = &tablesp->ct_tss;
361 
362 	if ((tablesp->ct_gdt = kmem_zalloc(PAGESIZE, KM_NOSLEEP)) == NULL)
363 		panic("mp_startup_init: cpu%d cannot allocate GDT", cpun);
364 	cp->cpu_gdt = tablesp->ct_gdt;
365 	bcopy(CPU->cpu_gdt, cp->cpu_gdt, NGDT * (sizeof (user_desc_t)));
366 
367 #if defined(__amd64)
368 
369 	/*
370 	 * #DF (double fault).
371 	 */
372 	ntss->tss_ist1 =
373 	    (uint64_t)&tablesp->ct_stack[sizeof (tablesp->ct_stack)];
374 
375 #elif defined(__i386)
376 
377 	ntss->tss_esp0 = ntss->tss_esp1 = ntss->tss_esp2 = ntss->tss_esp =
378 	    (uint32_t)&tablesp->ct_stack[sizeof (tablesp->ct_stack)];
379 
380 	ntss->tss_ss0 = ntss->tss_ss1 = ntss->tss_ss2 = ntss->tss_ss = KDS_SEL;
381 
382 	ntss->tss_eip = (uint32_t)mp_startup;
383 
384 	ntss->tss_cs = KCS_SEL;
385 	ntss->tss_fs = KFS_SEL;
386 	ntss->tss_gs = KGS_SEL;
387 
388 	/*
389 	 * setup kernel %gs.
390 	 */
391 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
392 	    SEL_KPL, 0, 1);
393 
394 #endif	/* __i386 */
395 
396 	/*
397 	 * Set I/O bit map offset equal to size of TSS segment limit
398 	 * for no I/O permission map. This will cause all user I/O
399 	 * instructions to generate #gp fault.
400 	 */
401 	ntss->tss_bitmapbase = sizeof (*ntss);
402 
403 	/*
404 	 * setup kernel tss.
405 	 */
406 	set_syssegd((system_desc_t *)&cp->cpu_gdt[GDT_KTSS], cp->cpu_tss,
407 	    sizeof (*cp->cpu_tss) -1, SDT_SYSTSS, SEL_KPL);
408 
409 	/*
410 	 * If we have more than one node, each cpu gets a copy of IDT
411 	 * local to its node. If this is a Pentium box, we use cpu 0's
412 	 * IDT. cpu 0's IDT has been made read-only to workaround the
413 	 * cmpxchgl register bug
414 	 */
415 	cp->cpu_idt = CPU->cpu_idt;
416 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
417 		cp->cpu_idt = kmem_alloc(sizeof (idt0), KM_SLEEP);
418 		bcopy(idt0, cp->cpu_idt, sizeof (idt0));
419 	}
420 
421 	/*
422 	 * Get interrupt priority data from cpu 0
423 	 */
424 	cp->cpu_pri_data = CPU->cpu_pri_data;
425 
426 	hat_cpu_online(cp);
427 
428 	/* Should remove all entries for the current process/thread here */
429 
430 	/*
431 	 * Fill up the real mode platter to make it easy for real mode code to
432 	 * kick it off. This area should really be one passed by boot to kernel
433 	 * and guaranteed to be below 1MB and aligned to 16 bytes. Should also
434 	 * have identical physical and virtual address in paged mode.
435 	 */
436 	real_mode_platter->rm_idt_base = cp->cpu_idt;
437 	real_mode_platter->rm_idt_lim = sizeof (idt0) - 1;
438 	real_mode_platter->rm_gdt_base = cp->cpu_gdt;
439 	real_mode_platter->rm_gdt_lim = sizeof (gdt0) -1;
440 	real_mode_platter->rm_pdbr = getcr3();
441 	real_mode_platter->rm_cpu = cpun;
442 	real_mode_platter->rm_x86feature = x86_feature;
443 	real_mode_platter->rm_cr4 = cr4_value;
444 
445 #if defined(__amd64)
446 	if (getcr3() > 0xffffffffUL)
447 		panic("Cannot initialize CPUs; kernel's 64-bit page tables\n"
448 			"located above 4G in physical memory (@ 0x%llx).",
449 			(unsigned long long)getcr3());
450 
451 	/*
452 	 * Setup pseudo-descriptors for temporary GDT and IDT for use ONLY
453 	 * by code in real_mode_start():
454 	 *
455 	 * GDT[0]:  NULL selector
456 	 * GDT[1]:  64-bit CS: Long = 1, Present = 1, bits 12, 11 = 1
457 	 *
458 	 * Clear the IDT as interrupts will be off and a limit of 0 will cause
459 	 * the CPU to triple fault and reset on an NMI, seemingly as reasonable
460 	 * a course of action as any other, though it may cause the entire
461 	 * platform to reset in some cases...
462 	 */
463 	real_mode_platter->rm_temp_gdt[0] = 0ULL;
464 	real_mode_platter->rm_temp_gdt[TEMPGDT_KCODE64] = 0x20980000000000ULL;
465 
466 	real_mode_platter->rm_temp_gdt_lim = (ushort_t)
467 	    (sizeof (real_mode_platter->rm_temp_gdt) - 1);
468 	real_mode_platter->rm_temp_gdt_base = rm_platter_pa +
469 	    (uint32_t)(&((rm_platter_t *)0)->rm_temp_gdt);
470 
471 	real_mode_platter->rm_temp_idt_lim = 0;
472 	real_mode_platter->rm_temp_idt_base = 0;
473 
474 	/*
475 	 * Since the CPU needs to jump to protected mode using an identity
476 	 * mapped address, we need to calculate it here.
477 	 */
478 	real_mode_platter->rm_longmode64_addr = rm_platter_pa +
479 	    ((uint32_t)long_mode_64 - (uint32_t)real_mode_start);
480 #endif	/* __amd64 */
481 
482 #ifdef TRAPTRACE
483 	/*
484 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers for this
485 	 * CPU.
486 	 */
487 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
488 	ttc->ttc_next = ttc->ttc_first;
489 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
490 #endif
491 
492 	/*
493 	 * Record that we have another CPU.
494 	 */
495 	mutex_enter(&cpu_lock);
496 	/*
497 	 * Initialize the interrupt threads for this CPU
498 	 */
499 	cpu_intr_alloc(cp, NINTR_THREADS);
500 	/*
501 	 * Add CPU to list of available CPUs.  It'll be on the active list
502 	 * after mp_startup().
503 	 */
504 	cpu_add_unit(cp);
505 	mutex_exit(&cpu_lock);
506 }
507 
508 /*
509  * Apply workarounds for known errata, and warn about those that are absent.
510  *
511  * System vendors occasionally create configurations which contain different
512  * revisions of the CPUs that are almost but not exactly the same.  At the
513  * time of writing, this meant that their clock rates were the same, their
514  * feature sets were the same, but the required workaround were -not-
515  * necessarily the same.  So, this routine is invoked on -every- CPU soon
516  * after starting to make sure that the resulting system contains the most
517  * pessimal set of workarounds needed to cope with *any* of the CPUs in the
518  * system.
519  *
520  * workaround_errata is invoked early in mlsetup() for CPU 0, and in
521  * mp_startup() for all slave CPUs. Slaves process workaround_errata prior
522  * to acknowledging their readiness to the master, so this routine will
523  * never be executed by multiple CPUs in parallel, thus making updates to
524  * global data safe.
525  *
526  * These workarounds are based on Rev 3.57 of the Revision Guide for
527  * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005.
528  */
529 
530 #if defined(OPTERON_ERRATUM_91)
531 int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
532 #endif
533 
534 #if defined(OPTERON_ERRATUM_93)
535 int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
536 #endif
537 
538 #if defined(OPTERON_ERRATUM_100)
539 int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
540 #endif
541 
542 #if defined(OPTERON_ERRATUM_109)
543 int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
544 #endif
545 
546 #if defined(OPTERON_ERRATUM_121)
547 int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
548 #endif
549 
550 #if defined(OPTERON_ERRATUM_122)
551 int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
552 #endif
553 
554 #if defined(OPTERON_ERRATUM_123)
555 int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
556 #endif
557 
558 #if defined(OPTERON_ERRATUM_131)
559 int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
560 #endif
561 
562 #if defined(OPTERON_WORKAROUND_6336786)
563 int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
564 int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
565 #endif
566 
567 #if defined(OPTERON_WORKAROUND_6323525)
568 int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
569 #endif
570 
571 #define	WARNING(cpu, n)						\
572 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %d",	\
573 	    (cpu)->cpu_id, (n))
574 
575 uint_t
576 workaround_errata(struct cpu *cpu)
577 {
578 	uint_t missing = 0;
579 
580 	ASSERT(cpu == CPU);
581 
582 	/*LINTED*/
583 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
584 		/*
585 		 * SWAPGS May Fail To Read Correct GS Base
586 		 */
587 #if defined(OPTERON_ERRATUM_88)
588 		/*
589 		 * The workaround is an mfence in the relevant assembler code
590 		 */
591 #else
592 		WARNING(cpu, 88);
593 		missing++;
594 #endif
595 	}
596 
597 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
598 		/*
599 		 * Software Prefetches May Report A Page Fault
600 		 */
601 #if defined(OPTERON_ERRATUM_91)
602 		/*
603 		 * fix is in trap.c
604 		 */
605 		opteron_erratum_91++;
606 #else
607 		WARNING(cpu, 91);
608 		missing++;
609 #endif
610 	}
611 
612 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
613 		/*
614 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
615 		 */
616 #if defined(OPTERON_ERRATUM_93)
617 		/*
618 		 * fix is in trap.c
619 		 */
620 		opteron_erratum_93++;
621 #else
622 		WARNING(cpu, 93);
623 		missing++;
624 #endif
625 	}
626 
627 	/*LINTED*/
628 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
629 		/*
630 		 * RET Instruction May Return to Incorrect EIP
631 		 */
632 #if defined(OPTERON_ERRATUM_95)
633 #if defined(_LP64)
634 		/*
635 		 * Workaround this by ensuring that 32-bit user code and
636 		 * 64-bit kernel code never occupy the same address
637 		 * range mod 4G.
638 		 */
639 		if (_userlimit32 > 0xc0000000ul)
640 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
641 
642 		/*LINTED*/
643 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
644 #endif	/* _LP64 */
645 #else
646 		WARNING(cpu, 95);
647 		missing++;
648 #endif	/* OPTERON_ERRATUM_95 */
649 	}
650 
651 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
652 		/*
653 		 * Compatibility Mode Branches Transfer to Illegal Address
654 		 */
655 #if defined(OPTERON_ERRATUM_100)
656 		/*
657 		 * fix is in trap.c
658 		 */
659 		opteron_erratum_100++;
660 #else
661 		WARNING(cpu, 100);
662 		missing++;
663 #endif
664 	}
665 
666 	/*LINTED*/
667 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
668 		/*
669 		 * CPUID Instruction May Return Incorrect Model Number In
670 		 * Some Processors
671 		 */
672 #if defined(OPTERON_ERRATUM_108)
673 		/*
674 		 * (Our cpuid-handling code corrects the model number on
675 		 * those processors)
676 		 */
677 #else
678 		WARNING(cpu, 108);
679 		missing++;
680 #endif
681 	}
682 
683 	/*LINTED*/
684 	if (cpuid_opteron_erratum(cpu, 109) > 0) {
685 		/*
686 		 * Certain Reverse REP MOVS May Produce Unpredictable Behaviour
687 		 */
688 #if defined(OPTERON_ERRATUM_109)
689 
690 		/* workaround is to print a warning to upgrade BIOS */
691 		if (rdmsr(MSR_AMD_PATCHLEVEL) == 0)
692 			opteron_erratum_109++;
693 #else
694 		WARNING(cpu, 109);
695 		missing++;
696 #endif
697 	}
698 	/*LINTED*/
699 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
700 		/*
701 		 * Sequential Execution Across Non_Canonical Boundary Caused
702 		 * Processor Hang
703 		 */
704 #if defined(OPTERON_ERRATUM_121)
705 		static int	lma;
706 
707 		if (opteron_erratum_121)
708 			opteron_erratum_121++;
709 
710 		/*
711 		 * Erratum 121 is only present in long (64 bit) mode.
712 		 * Workaround is to include the page immediately before the
713 		 * va hole to eliminate the possibility of system hangs due to
714 		 * sequential execution across the va hole boundary.
715 		 */
716 		if (lma == 0) {
717 			/*
718 			 * check LMA once: assume all cpus are in long mode
719 			 * or not.
720 			 */
721 			lma = 1;
722 
723 			if (rdmsr(MSR_AMD_EFER) & AMD_EFER_LMA) {
724 				if (hole_start) {
725 					hole_start -= PAGESIZE;
726 				} else {
727 					/*
728 					 * hole_start not yet initialized by
729 					 * mmu_init. Initialize hole_start
730 					 * with value to be subtracted.
731 					 */
732 					hole_start = PAGESIZE;
733 				}
734 				opteron_erratum_121++;
735 			}
736 		}
737 #else
738 		WARNING(cpu, 121);
739 		missing++;
740 #endif
741 	}
742 
743 	/*LINTED*/
744 	if (cpuid_opteron_erratum(cpu, 122) > 0) {
745 		/*
746 		 * TLB Flush Filter May Cause Cohenrency Problem in
747 		 * Multiprocessor Systems
748 		 */
749 #if defined(OPTERON_ERRATUM_122)
750 		/*
751 		 * Erratum 122 is only present in MP configurations (multi-core
752 		 * or multi-processor).
753 		 */
754 
755 		if (opteron_erratum_122 || lgrp_plat_node_cnt > 1 ||
756 		    cpuid_get_ncpu_per_chip(cpu) > 1) {
757 			/* disable TLB Flush Filter */
758 			wrmsr(MSR_AMD_HWCR, rdmsr(MSR_AMD_HWCR) |
759 			    (uint64_t)(uintptr_t)AMD_HWCR_FFDIS);
760 			opteron_erratum_122++;
761 		}
762 
763 #else
764 		WARNING(cpu, 122);
765 		missing++;
766 #endif
767 	}
768 
769 #if defined(OPTERON_ERRATUM_123)
770 	/*LINTED*/
771 	if (cpuid_opteron_erratum(cpu, 123) > 0) {
772 		/*
773 		 * Bypassed Reads May Cause Data Corruption of System Hang in
774 		 * Dual Core Processors
775 		 */
776 		/*
777 		 * Erratum 123 applies only to multi-core cpus.
778 		 */
779 
780 		if (cpuid_get_ncpu_per_chip(cpu) > 1) {
781 			/* workaround is to print a warning to upgrade BIOS */
782 			if (rdmsr(MSR_AMD_PATCHLEVEL) == 0)
783 				opteron_erratum_123++;
784 		}
785 	}
786 #endif
787 
788 #if defined(OPTERON_ERRATUM_131)
789 	/*LINTED*/
790 	if (cpuid_opteron_erratum(cpu, 131) > 0) {
791 		/*
792 		 * Multiprocessor Systems with Four or More Cores May Deadlock
793 		 * Waiting for a Probe Response
794 		 */
795 		/*
796 		 * Erratum 131 applies to any system with four or more cores.
797 		 */
798 		if ((opteron_erratum_131 == 0) && ((lgrp_plat_node_cnt *
799 		    cpuid_get_ncpu_per_chip(cpu)) >= 4)) {
800 			/*
801 			 * Workaround is to print a warning to upgrade
802 			 * the BIOS
803 			 */
804 			if (!(rdmsr(MSR_AMD_NB_CFG) & AMD_NB_CFG_SRQ_HEARTBEAT))
805 				opteron_erratum_131++;
806 		}
807 	}
808 #endif
809 
810 #if defined(OPTERON_WORKAROUND_6336786)
811 	/*
812 	 * This isn't really erratum, but for convenience the
813 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
814 	 */
815 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
816 		int	node;
817 		uint8_t data;
818 
819 		/*
820 		 * Disable C1-Clock ramping on multi-core/multi-processor
821 		 * K8 platforms to guard against TSC drift.
822 		 */
823 		if (opteron_workaround_6336786) {
824 			opteron_workaround_6336786++;
825 		} else if ((lgrp_plat_node_cnt *
826 		    cpuid_get_ncpu_per_chip(cpu) >= 2) ||
827 		    opteron_workaround_6336786_UP) {
828 			for (node = 0; node < lgrp_plat_node_cnt; node++) {
829 				/*
830 				 * Clear PMM7[1:0] (function 3, offset 0x87)
831 				 * Northbridge device is the node id + 24.
832 				 */
833 				data = pci_getb_func(0, node + 24, 3, 0x87);
834 				data &= 0xFC;
835 				pci_putb_func(0, node + 24, 3, 0x87, data);
836 			}
837 			opteron_workaround_6336786++;
838 		}
839 	}
840 #endif
841 
842 #if defined(OPTERON_WORKAROUND_6323525)
843 	/*LINTED*/
844 	/*
845 	 * Mutex primitives don't work as expected.
846 	 */
847 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
848 
849 		/*
850 		 * problem only occurs with 2 or more cores. If bit in
851 		 * MSR_BU_CFG set, then not applicable. The workaround
852 		 * is to patch the semaphone routines with the lfence
853 		 * instruction to provide necessary load memory barrier with
854 		 * possible subsequent read-modify-write ops.
855 		 *
856 		 * It is too early in boot to call the patch routine so
857 		 * set erratum variable to be done in startup_end().
858 		 */
859 		if (opteron_workaround_6323525) {
860 			opteron_workaround_6323525++;
861 		} else if ((x86_feature & X86_SSE2) && ((lgrp_plat_node_cnt *
862 		    cpuid_get_ncpu_per_chip(cpu)) >= 2)) {
863 			if ((xrdmsr(MSR_BU_CFG) & 0x02) == 0)
864 				opteron_workaround_6323525++;
865 		}
866 	}
867 #endif
868 	return (missing);
869 }
870 
871 void
872 workaround_errata_end()
873 {
874 #if defined(OPTERON_ERRATUM_109)
875 	if (opteron_erratum_109) {
876 		cmn_err(CE_WARN,
877 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
878 		    " processor\nerratum 109 was not detected; updating your"
879 		    " system's BIOS to a version\ncontaining this"
880 		    " microcode patch is HIGHLY recommended or erroneous"
881 		    " system\noperation may occur.\n");
882 	}
883 #endif	/* OPTERON_ERRATUM_109 */
884 #if defined(OPTERON_ERRATUM_123)
885 	if (opteron_erratum_123) {
886 		cmn_err(CE_WARN,
887 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
888 		    " processor\nerratum 123 was not detected; updating your"
889 		    " system's BIOS to a version\ncontaining this"
890 		    " microcode patch is HIGHLY recommended or erroneous"
891 		    " system\noperation may occur.\n");
892 	}
893 #endif	/* OPTERON_ERRATUM_123 */
894 #if defined(OPTERON_ERRATUM_131)
895 	if (opteron_erratum_131) {
896 		cmn_err(CE_WARN,
897 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
898 		    " processor\nerratum 131 was not detected; updating your"
899 		    " system's BIOS to a version\ncontaining this"
900 		    " microcode patch is HIGHLY recommended or erroneous"
901 		    " system\noperation may occur.\n");
902 	}
903 #endif	/* OPTERON_ERRATUM_131 */
904 }
905 
906 static ushort_t *mp_map_warm_reset_vector();
907 static void mp_unmap_warm_reset_vector(ushort_t *warm_reset_vector);
908 
909 /*ARGSUSED*/
910 void
911 start_other_cpus(int cprboot)
912 {
913 	unsigned who;
914 	int cpuid = 0;
915 	int delays = 0;
916 	int started_cpu;
917 	ushort_t *warm_reset_vector = NULL;
918 	extern int procset;
919 
920 	/*
921 	 * Initialize our own cpu_info.
922 	 */
923 	init_cpu_info(CPU);
924 
925 	/*
926 	 * Initialize our syscall handlers
927 	 */
928 	init_cpu_syscall(CPU);
929 
930 	/*
931 	 * if only 1 cpu or not using MP, skip the rest of this
932 	 */
933 	if (!(mp_cpus & ~(1 << cpuid)) || use_mp == 0) {
934 		if (use_mp == 0)
935 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
936 		goto done;
937 	}
938 
939 	/*
940 	 * perform such initialization as is needed
941 	 * to be able to take CPUs on- and off-line.
942 	 */
943 	cpu_pause_init();
944 
945 	xc_init();		/* initialize processor crosscalls */
946 
947 	/*
948 	 * Copy the real mode code at "real_mode_start" to the
949 	 * page at rm_platter_va.
950 	 */
951 	warm_reset_vector = mp_map_warm_reset_vector();
952 	if (warm_reset_vector == NULL)
953 		goto done;
954 
955 	bcopy((caddr_t)real_mode_start,
956 	    (caddr_t)((rm_platter_t *)rm_platter_va)->rm_code,
957 	    (size_t)real_mode_end - (size_t)real_mode_start);
958 
959 	flushes_require_xcalls = 1;
960 
961 	affinity_set(CPU_CURRENT);
962 
963 	for (who = 0; who < NCPU; who++) {
964 		if (who == cpuid)
965 			continue;
966 
967 		if ((mp_cpus & (1 << who)) == 0)
968 			continue;
969 
970 		mp_startup_init(who);
971 		started_cpu = 1;
972 		(*cpu_startf)(who, rm_platter_pa);
973 
974 		while ((procset & (1 << who)) == 0) {
975 
976 			delay(1);
977 			if (++delays > (20 * hz)) {
978 
979 				cmn_err(CE_WARN,
980 				    "cpu%d failed to start", who);
981 
982 				mutex_enter(&cpu_lock);
983 				cpu[who]->cpu_flags = 0;
984 				cpu_vm_data_destroy(cpu[who]);
985 				cpu_del_unit(who);
986 				mutex_exit(&cpu_lock);
987 
988 				started_cpu = 0;
989 				break;
990 			}
991 		}
992 		if (!started_cpu)
993 			continue;
994 		if (tsc_gethrtime_enable)
995 			tsc_sync_master(who);
996 
997 
998 		if (dtrace_cpu_init != NULL) {
999 			/*
1000 			 * DTrace CPU initialization expects cpu_lock
1001 			 * to be held.
1002 			 */
1003 			mutex_enter(&cpu_lock);
1004 			(*dtrace_cpu_init)(who);
1005 			mutex_exit(&cpu_lock);
1006 		}
1007 	}
1008 
1009 	affinity_clear();
1010 
1011 	for (who = 0; who < NCPU; who++) {
1012 		if (who == cpuid)
1013 			continue;
1014 
1015 		if (!(procset & (1 << who)))
1016 			continue;
1017 
1018 		while (!(cpu_ready_set & (1 << who)))
1019 			delay(1);
1020 	}
1021 
1022 done:
1023 	workaround_errata_end();
1024 
1025 	if (warm_reset_vector != NULL)
1026 		mp_unmap_warm_reset_vector(warm_reset_vector);
1027 	hat_unload(kas.a_hat, (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
1028 	    HAT_UNLOAD);
1029 }
1030 
1031 /*
1032  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
1033  */
1034 /*ARGSUSED*/
1035 int
1036 mp_cpu_configure(int cpuid)
1037 {
1038 	return (ENOTSUP);		/* not supported */
1039 }
1040 
1041 /*ARGSUSED*/
1042 int
1043 mp_cpu_unconfigure(int cpuid)
1044 {
1045 	return (ENOTSUP);		/* not supported */
1046 }
1047 
1048 /*
1049  * Startup function for 'other' CPUs (besides boot cpu).
1050  * Called from real_mode_start (after *ap_mlsetup).
1051  *
1052  * WARNING: until CPU_READY is set, mp_startup and routines called by
1053  * mp_startup should not call routines (e.g. kmem_free) that could call
1054  * hat_unload which requires CPU_READY to be set.
1055  */
1056 void
1057 mp_startup(void)
1058 {
1059 	struct cpu *cp = CPU;
1060 	extern int procset;
1061 	uint_t new_x86_feature;
1062 
1063 	new_x86_feature = cpuid_pass1(cp);
1064 
1065 	/*
1066 	 * We need to Sync MTRR with cpu0's MTRR. We have to do
1067 	 * this with interrupts disabled.
1068 	 */
1069 	if (x86_feature & X86_MTRR)
1070 		mtrr_sync();
1071 
1072 	/*
1073 	 * Initialize this CPU's syscall handlers
1074 	 */
1075 	init_cpu_syscall(cp);
1076 
1077 	/*
1078 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
1079 	 * highest level at which a routine is permitted to block on
1080 	 * an adaptive mutex (allows for cpu poke interrupt in case
1081 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
1082 	 * device interrupts that may end up in the hat layer issuing cross
1083 	 * calls before CPU_READY is set.
1084 	 */
1085 	(void) splx(ipltospl(LOCK_LEVEL));
1086 
1087 	/*
1088 	 * Do a sanity check to make sure this new CPU is a sane thing
1089 	 * to add to the collection of processors running this system.
1090 	 *
1091 	 * XXX	Clearly this needs to get more sophisticated, if x86
1092 	 * systems start to get built out of heterogenous CPUs; as is
1093 	 * likely to happen once the number of processors in a configuration
1094 	 * gets large enough.
1095 	 */
1096 	if ((x86_feature & new_x86_feature) != x86_feature) {
1097 		cmn_err(CE_CONT, "?cpu%d: %b\n",
1098 		    cp->cpu_id, new_x86_feature, FMT_X86_FEATURE);
1099 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
1100 	}
1101 
1102 	/*
1103 	 * We could be more sophisticated here, and just mark the CPU
1104 	 * as "faulted" but at this point we'll opt for the easier
1105 	 * answer of dieing horribly.  Provided the boot cpu is ok,
1106 	 * the system can be recovered by booting with use_mp set to zero.
1107 	 */
1108 	if (workaround_errata(cp) != 0)
1109 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
1110 
1111 	cpuid_pass2(cp);
1112 	cpuid_pass3(cp);
1113 	(void) cpuid_pass4(cp);
1114 
1115 	init_cpu_info(cp);
1116 
1117 	mutex_enter(&cpu_lock);
1118 	procset |= 1 << cp->cpu_id;
1119 	mutex_exit(&cpu_lock);
1120 
1121 	if (tsc_gethrtime_enable)
1122 		tsc_sync_slave();
1123 
1124 	mutex_enter(&cpu_lock);
1125 	/*
1126 	 * It's unfortunate that chip_cpu_init() has to be called here.
1127 	 * It really belongs in cpu_add_unit(), but unfortunately it is
1128 	 * dependent on the cpuid probing, which must be done in the
1129 	 * context of the current CPU. Care must be taken on x86 to ensure
1130 	 * that mp_startup can safely block even though chip_cpu_init() and
1131 	 * cpu_add_active() have not yet been called.
1132 	 */
1133 	chip_cpu_init(cp);
1134 	chip_cpu_startup(cp);
1135 
1136 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_ENABLE | CPU_EXISTS;
1137 	cpu_add_active(cp);
1138 	mutex_exit(&cpu_lock);
1139 
1140 	add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi);
1141 
1142 	/* The base spl should still be at LOCK LEVEL here */
1143 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
1144 	set_base_spl();		/* Restore the spl to its proper value */
1145 
1146 	(void) spl0();				/* enable interrupts */
1147 
1148 	/*
1149 	 * Set up the CPU module for this CPU.  This can't be done before
1150 	 * this CPU is made CPU_READY, because we may (in heterogeneous systems)
1151 	 * need to go load another CPU module.  The act of attempting to load
1152 	 * a module may trigger a cross-call, which will ASSERT unless this
1153 	 * cpu is CPU_READY.
1154 	 */
1155 	cmi_init();
1156 
1157 	if (x86_feature & X86_MCA)
1158 		cmi_mca_init();
1159 
1160 	if (boothowto & RB_DEBUG)
1161 		kdi_dvec_cpu_init(cp);
1162 
1163 	/*
1164 	 * Setting the bit in cpu_ready_set must be the last operation in
1165 	 * processor initialization; the boot CPU will continue to boot once
1166 	 * it sees this bit set for all active CPUs.
1167 	 */
1168 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
1169 
1170 	/*
1171 	 * Because mp_startup() gets fired off after init() starts, we
1172 	 * can't use the '?' trick to do 'boot -v' printing - so we
1173 	 * always direct the 'cpu .. online' messages to the log.
1174 	 */
1175 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
1176 	    cp->cpu_id);
1177 
1178 	/*
1179 	 * Now we are done with the startup thread, so free it up.
1180 	 */
1181 	thread_exit();
1182 	panic("mp_startup: cannot return");
1183 	/*NOTREACHED*/
1184 }
1185 
1186 
1187 /*
1188  * Start CPU on user request.
1189  */
1190 /* ARGSUSED */
1191 int
1192 mp_cpu_start(struct cpu *cp)
1193 {
1194 	ASSERT(MUTEX_HELD(&cpu_lock));
1195 	return (0);
1196 }
1197 
1198 /*
1199  * Stop CPU on user request.
1200  */
1201 /* ARGSUSED */
1202 int
1203 mp_cpu_stop(struct cpu *cp)
1204 {
1205 	extern int cbe_psm_timer_mode;
1206 	ASSERT(MUTEX_HELD(&cpu_lock));
1207 
1208 	/*
1209 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
1210 	 * can't stop it.  (This is true only for machines with no TSC.)
1211 	 */
1212 
1213 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
1214 		return (1);
1215 
1216 	return (0);
1217 }
1218 
1219 /*
1220  * Power on CPU.
1221  */
1222 /* ARGSUSED */
1223 int
1224 mp_cpu_poweron(struct cpu *cp)
1225 {
1226 	ASSERT(MUTEX_HELD(&cpu_lock));
1227 	return (ENOTSUP);		/* not supported */
1228 }
1229 
1230 /*
1231  * Power off CPU.
1232  */
1233 /* ARGSUSED */
1234 int
1235 mp_cpu_poweroff(struct cpu *cp)
1236 {
1237 	ASSERT(MUTEX_HELD(&cpu_lock));
1238 	return (ENOTSUP);		/* not supported */
1239 }
1240 
1241 
1242 /*
1243  * Take the specified CPU out of participation in interrupts.
1244  */
1245 int
1246 cpu_disable_intr(struct cpu *cp)
1247 {
1248 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
1249 		return (EBUSY);
1250 
1251 	cp->cpu_flags &= ~CPU_ENABLE;
1252 	return (0);
1253 }
1254 
1255 /*
1256  * Allow the specified CPU to participate in interrupts.
1257  */
1258 void
1259 cpu_enable_intr(struct cpu *cp)
1260 {
1261 	ASSERT(MUTEX_HELD(&cpu_lock));
1262 	cp->cpu_flags |= CPU_ENABLE;
1263 	psm_enable_intr(cp->cpu_id);
1264 }
1265 
1266 
1267 
1268 static ushort_t *
1269 mp_map_warm_reset_vector()
1270 {
1271 	ushort_t *warm_reset_vector;
1272 
1273 	if (!(warm_reset_vector = (ushort_t *)psm_map_phys(WARM_RESET_VECTOR,
1274 	    sizeof (ushort_t *), PROT_READ|PROT_WRITE)))
1275 		return (NULL);
1276 
1277 	/*
1278 	 * setup secondary cpu bios boot up vector
1279 	 */
1280 	*warm_reset_vector = (ushort_t)((caddr_t)
1281 		((struct rm_platter *)rm_platter_va)->rm_code - rm_platter_va
1282 		+ ((ulong_t)rm_platter_va & 0xf));
1283 	warm_reset_vector++;
1284 	*warm_reset_vector = (ushort_t)(rm_platter_pa >> 4);
1285 
1286 	--warm_reset_vector;
1287 	return (warm_reset_vector);
1288 }
1289 
1290 static void
1291 mp_unmap_warm_reset_vector(ushort_t *warm_reset_vector)
1292 {
1293 	psm_unmap_phys((caddr_t)warm_reset_vector, sizeof (ushort_t *));
1294 }
1295 
1296 void
1297 mp_cpu_faulted_enter(struct cpu *cp)
1298 {
1299 	cmi_faulted_enter(cp);
1300 }
1301 
1302 void
1303 mp_cpu_faulted_exit(struct cpu *cp)
1304 {
1305 	cmi_faulted_exit(cp);
1306 }
1307 
1308 /*
1309  * The following two routines are used as context operators on threads belonging
1310  * to processes with a private LDT (see sysi86).  Due to the rarity of such
1311  * processes, these routines are currently written for best code readability and
1312  * organization rather than speed.  We could avoid checking x86_feature at every
1313  * context switch by installing different context ops, depending on the
1314  * x86_feature flags, at LDT creation time -- one for each combination of fast
1315  * syscall feature flags.
1316  */
1317 
1318 /*ARGSUSED*/
1319 void
1320 cpu_fast_syscall_disable(void *arg)
1321 {
1322 	if (x86_feature & X86_SEP)
1323 		cpu_sep_disable();
1324 	if (x86_feature & X86_ASYSC)
1325 		cpu_asysc_disable();
1326 }
1327 
1328 /*ARGSUSED*/
1329 void
1330 cpu_fast_syscall_enable(void *arg)
1331 {
1332 	if (x86_feature & X86_SEP)
1333 		cpu_sep_enable();
1334 	if (x86_feature & X86_ASYSC)
1335 		cpu_asysc_enable();
1336 }
1337 
1338 static void
1339 cpu_sep_enable(void)
1340 {
1341 	ASSERT(x86_feature & X86_SEP);
1342 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1343 
1344 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
1345 }
1346 
1347 static void
1348 cpu_sep_disable(void)
1349 {
1350 	ASSERT(x86_feature & X86_SEP);
1351 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1352 
1353 	/*
1354 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
1355 	 * the sysenter or sysexit instruction to trigger a #gp fault.
1356 	 */
1357 	wrmsr(MSR_INTC_SEP_CS, 0ULL);
1358 }
1359 
1360 static void
1361 cpu_asysc_enable(void)
1362 {
1363 	ASSERT(x86_feature & X86_ASYSC);
1364 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1365 
1366 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1367 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
1368 }
1369 
1370 static void
1371 cpu_asysc_disable(void)
1372 {
1373 	ASSERT(x86_feature & X86_ASYSC);
1374 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1375 
1376 	/*
1377 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
1378 	 * executing syscall or sysret with this bit off will incur a #ud trap.
1379 	 */
1380 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1381 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
1382 }
1383