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