xref: /titanic_44/usr/src/uts/i86pc/os/mp_startup.c (revision f841f6ad96ea6675d6c6b35c749eaac601799fdf)
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 			/*
803 			 * Workaround is to print a warning to upgrade
804 			 * the BIOS
805 			 */
806 			if (!(rdmsr(MSR_AMD_NB_CFG) & AMD_NB_CFG_SRQ_HEARTBEAT))
807 				opteron_erratum_131++;
808 		}
809 	}
810 #endif
811 
812 #if defined(OPTERON_WORKAROUND_6336786)
813 	/*
814 	 * This isn't really erratum, but for convenience the
815 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
816 	 */
817 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
818 		int	node;
819 		uint8_t data;
820 
821 		/*
822 		 * Disable C1-Clock ramping on multi-core/multi-processor
823 		 * K8 platforms to guard against TSC drift.
824 		 */
825 		if (opteron_workaround_6336786) {
826 			opteron_workaround_6336786++;
827 		} else if ((lgrp_plat_node_cnt *
828 		    cpuid_get_ncpu_per_chip(cpu) >= 2) ||
829 		    opteron_workaround_6336786_UP) {
830 			for (node = 0; node < lgrp_plat_node_cnt; node++) {
831 				/*
832 				 * Clear PMM7[1:0] (function 3, offset 0x87)
833 				 * Northbridge device is the node id + 24.
834 				 */
835 				data = pci_getb_func(0, node + 24, 3, 0x87);
836 				data &= 0xFC;
837 				pci_putb_func(0, node + 24, 3, 0x87, data);
838 			}
839 			opteron_workaround_6336786++;
840 		}
841 	}
842 #endif
843 
844 #if defined(OPTERON_WORKAROUND_6323525)
845 	/*LINTED*/
846 	/*
847 	 * Mutex primitives don't work as expected.
848 	 */
849 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
850 
851 		/*
852 		 * problem only occurs with 2 or more cores. If bit in
853 		 * MSR_BU_CFG set, then not applicable. The workaround
854 		 * is to patch the semaphone routines with the lfence
855 		 * instruction to provide necessary load memory barrier with
856 		 * possible subsequent read-modify-write ops.
857 		 *
858 		 * It is too early in boot to call the patch routine so
859 		 * set erratum variable to be done in startup_end().
860 		 */
861 		if (opteron_workaround_6323525) {
862 			opteron_workaround_6323525++;
863 		} else if ((x86_feature & X86_SSE2) && ((lgrp_plat_node_cnt *
864 		    cpuid_get_ncpu_per_chip(cpu)) >= 2)) {
865 			if ((xrdmsr(MSR_BU_CFG) & 0x02) == 0)
866 				opteron_workaround_6323525++;
867 		}
868 	}
869 #endif
870 	return (missing);
871 }
872 
873 void
874 workaround_errata_end()
875 {
876 #if defined(OPTERON_ERRATUM_109)
877 	if (opteron_erratum_109) {
878 		cmn_err(CE_WARN,
879 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
880 		    " processor\nerratum 109 was not detected; updating your"
881 		    " system's BIOS to a version\ncontaining this"
882 		    " microcode patch is HIGHLY recommended or erroneous"
883 		    " system\noperation may occur.\n");
884 	}
885 #endif	/* OPTERON_ERRATUM_109 */
886 #if defined(OPTERON_ERRATUM_123)
887 	if (opteron_erratum_123) {
888 		cmn_err(CE_WARN,
889 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
890 		    " processor\nerratum 123 was not detected; updating your"
891 		    " system's BIOS to a version\ncontaining this"
892 		    " microcode patch is HIGHLY recommended or erroneous"
893 		    " system\noperation may occur.\n");
894 	}
895 #endif	/* OPTERON_ERRATUM_123 */
896 #if defined(OPTERON_ERRATUM_131)
897 	if (opteron_erratum_131) {
898 		cmn_err(CE_WARN,
899 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
900 		    " processor\nerratum 131 was not detected; updating your"
901 		    " system's BIOS to a version\ncontaining this"
902 		    " microcode patch is HIGHLY recommended or erroneous"
903 		    " system\noperation may occur.\n");
904 	}
905 #endif	/* OPTERON_ERRATUM_131 */
906 }
907 
908 static ushort_t *mp_map_warm_reset_vector();
909 static void mp_unmap_warm_reset_vector(ushort_t *warm_reset_vector);
910 
911 static cpuset_t procset = 1;
912 
913 /*ARGSUSED*/
914 void
915 start_other_cpus(int cprboot)
916 {
917 	unsigned int who;
918 	int skipped = 0;
919 	int cpuid = 0;
920 	int delays = 0;
921 	int started_cpu;
922 	ushort_t *warm_reset_vector = NULL;
923 
924 	/*
925 	 * Initialize our own cpu_info.
926 	 */
927 	init_cpu_info(CPU);
928 
929 	/*
930 	 * Initialize our syscall handlers
931 	 */
932 	init_cpu_syscall(CPU);
933 
934 	/*
935 	 * if only 1 cpu or not using MP, skip the rest of this
936 	 */
937 	if (CPUSET_ISEQUAL(mp_cpus, cpu_ready_set) || use_mp == 0) {
938 		if (use_mp == 0)
939 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
940 		goto done;
941 	}
942 
943 	/*
944 	 * perform such initialization as is needed
945 	 * to be able to take CPUs on- and off-line.
946 	 */
947 	cpu_pause_init();
948 
949 	xc_init();		/* initialize processor crosscalls */
950 
951 	/*
952 	 * Copy the real mode code at "real_mode_start" to the
953 	 * page at rm_platter_va.
954 	 */
955 	warm_reset_vector = mp_map_warm_reset_vector();
956 	if (warm_reset_vector == NULL)
957 		goto done;
958 
959 	bcopy((caddr_t)real_mode_start,
960 	    (caddr_t)((rm_platter_t *)rm_platter_va)->rm_code,
961 	    (size_t)real_mode_end - (size_t)real_mode_start);
962 
963 	flushes_require_xcalls = 1;
964 
965 	affinity_set(CPU_CURRENT);
966 
967 	for (who = 0; who < NCPU; who++) {
968 		if (who == cpuid)
969 			continue;
970 		if (!CPU_IN_SET(mp_cpus, who))
971 			continue;
972 
973 		if (ncpus >= max_ncpus) {
974 			skipped = who;
975 			continue;
976 		}
977 
978 		mp_startup_init(who);
979 		started_cpu = 1;
980 		(*cpu_startf)(who, rm_platter_pa);
981 
982 		while (!CPU_IN_SET(procset, who)) {
983 
984 			delay(1);
985 			if (++delays > (20 * hz)) {
986 
987 				cmn_err(CE_WARN,
988 				    "cpu%d failed to start", who);
989 
990 				mutex_enter(&cpu_lock);
991 				cpu[who]->cpu_flags = 0;
992 				cpu_vm_data_destroy(cpu[who]);
993 				cpu_del_unit(who);
994 				mutex_exit(&cpu_lock);
995 
996 				started_cpu = 0;
997 				break;
998 			}
999 		}
1000 		if (!started_cpu)
1001 			continue;
1002 		if (tsc_gethrtime_enable)
1003 			tsc_sync_master(who);
1004 
1005 		if (dtrace_cpu_init != NULL) {
1006 			/*
1007 			 * DTrace CPU initialization expects cpu_lock
1008 			 * to be held.
1009 			 */
1010 			mutex_enter(&cpu_lock);
1011 			(*dtrace_cpu_init)(who);
1012 			mutex_exit(&cpu_lock);
1013 		}
1014 	}
1015 
1016 	affinity_clear();
1017 
1018 	for (who = 0; who < NCPU; who++) {
1019 		if (who == cpuid)
1020 			continue;
1021 
1022 		if (!CPU_IN_SET(procset, who))
1023 			continue;
1024 
1025 		while (!CPU_IN_SET(cpu_ready_set, who))
1026 			delay(1);
1027 	}
1028 
1029 	if (skipped) {
1030 		cmn_err(CE_NOTE,
1031 		    "System detected %d CPU(s), but "
1032 		    "only %d CPU(s) were enabled during boot.",
1033 		    skipped + 1, ncpus);
1034 		cmn_err(CE_NOTE,
1035 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
1036 		    "See eeprom(1M).");
1037 	}
1038 
1039 done:
1040 	workaround_errata_end();
1041 
1042 	if (warm_reset_vector != NULL)
1043 		mp_unmap_warm_reset_vector(warm_reset_vector);
1044 	hat_unload(kas.a_hat, (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE,
1045 	    HAT_UNLOAD);
1046 
1047 	cmi_post_mpstartup();
1048 }
1049 
1050 /*
1051  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
1052  */
1053 /*ARGSUSED*/
1054 int
1055 mp_cpu_configure(int cpuid)
1056 {
1057 	return (ENOTSUP);		/* not supported */
1058 }
1059 
1060 /*ARGSUSED*/
1061 int
1062 mp_cpu_unconfigure(int cpuid)
1063 {
1064 	return (ENOTSUP);		/* not supported */
1065 }
1066 
1067 /*
1068  * Startup function for 'other' CPUs (besides boot cpu).
1069  * Called from real_mode_start (after *ap_mlsetup).
1070  *
1071  * WARNING: until CPU_READY is set, mp_startup and routines called by
1072  * mp_startup should not call routines (e.g. kmem_free) that could call
1073  * hat_unload which requires CPU_READY to be set.
1074  */
1075 void
1076 mp_startup(void)
1077 {
1078 	struct cpu *cp = CPU;
1079 	uint_t new_x86_feature;
1080 
1081 	new_x86_feature = cpuid_pass1(cp);
1082 
1083 	/*
1084 	 * We need to Sync MTRR with cpu0's MTRR. We have to do
1085 	 * this with interrupts disabled.
1086 	 */
1087 	if (x86_feature & X86_MTRR)
1088 		mtrr_sync();
1089 
1090 	/*
1091 	 * Initialize this CPU's syscall handlers
1092 	 */
1093 	init_cpu_syscall(cp);
1094 
1095 	/*
1096 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
1097 	 * highest level at which a routine is permitted to block on
1098 	 * an adaptive mutex (allows for cpu poke interrupt in case
1099 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
1100 	 * device interrupts that may end up in the hat layer issuing cross
1101 	 * calls before CPU_READY is set.
1102 	 */
1103 	(void) splx(ipltospl(LOCK_LEVEL));
1104 
1105 	/*
1106 	 * Do a sanity check to make sure this new CPU is a sane thing
1107 	 * to add to the collection of processors running this system.
1108 	 *
1109 	 * XXX	Clearly this needs to get more sophisticated, if x86
1110 	 * systems start to get built out of heterogenous CPUs; as is
1111 	 * likely to happen once the number of processors in a configuration
1112 	 * gets large enough.
1113 	 */
1114 	if ((x86_feature & new_x86_feature) != x86_feature) {
1115 		cmn_err(CE_CONT, "?cpu%d: %b\n",
1116 		    cp->cpu_id, new_x86_feature, FMT_X86_FEATURE);
1117 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
1118 	}
1119 
1120 	/*
1121 	 * We could be more sophisticated here, and just mark the CPU
1122 	 * as "faulted" but at this point we'll opt for the easier
1123 	 * answer of dieing horribly.  Provided the boot cpu is ok,
1124 	 * the system can be recovered by booting with use_mp set to zero.
1125 	 */
1126 	if (workaround_errata(cp) != 0)
1127 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
1128 
1129 	cpuid_pass2(cp);
1130 	cpuid_pass3(cp);
1131 	(void) cpuid_pass4(cp);
1132 
1133 	init_cpu_info(cp);
1134 
1135 	mutex_enter(&cpu_lock);
1136 	CPUSET_ADD(procset, cp->cpu_id);
1137 	mutex_exit(&cpu_lock);
1138 
1139 	if (tsc_gethrtime_enable)
1140 		tsc_sync_slave();
1141 
1142 	mutex_enter(&cpu_lock);
1143 	/*
1144 	 * It's unfortunate that chip_cpu_init() has to be called here.
1145 	 * It really belongs in cpu_add_unit(), but unfortunately it is
1146 	 * dependent on the cpuid probing, which must be done in the
1147 	 * context of the current CPU. Care must be taken on x86 to ensure
1148 	 * that mp_startup can safely block even though chip_cpu_init() and
1149 	 * cpu_add_active() have not yet been called.
1150 	 */
1151 	chip_cpu_init(cp);
1152 	chip_cpu_startup(cp);
1153 
1154 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_ENABLE | CPU_EXISTS;
1155 	cpu_add_active(cp);
1156 	mutex_exit(&cpu_lock);
1157 
1158 	add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi);
1159 
1160 	/* The base spl should still be at LOCK LEVEL here */
1161 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
1162 	set_base_spl();		/* Restore the spl to its proper value */
1163 
1164 	(void) spl0();				/* enable interrupts */
1165 
1166 	/*
1167 	 * Set up the CPU module for this CPU.  This can't be done before
1168 	 * this CPU is made CPU_READY, because we may (in heterogeneous systems)
1169 	 * need to go load another CPU module.  The act of attempting to load
1170 	 * a module may trigger a cross-call, which will ASSERT unless this
1171 	 * cpu is CPU_READY.
1172 	 */
1173 	cmi_init();
1174 
1175 	if (x86_feature & X86_MCA)
1176 		cmi_mca_init();
1177 
1178 	if (boothowto & RB_DEBUG)
1179 		kdi_dvec_cpu_init(cp);
1180 
1181 	/*
1182 	 * Setting the bit in cpu_ready_set must be the last operation in
1183 	 * processor initialization; the boot CPU will continue to boot once
1184 	 * it sees this bit set for all active CPUs.
1185 	 */
1186 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
1187 
1188 	/*
1189 	 * Because mp_startup() gets fired off after init() starts, we
1190 	 * can't use the '?' trick to do 'boot -v' printing - so we
1191 	 * always direct the 'cpu .. online' messages to the log.
1192 	 */
1193 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
1194 	    cp->cpu_id);
1195 
1196 	/*
1197 	 * Now we are done with the startup thread, so free it up.
1198 	 */
1199 	thread_exit();
1200 	panic("mp_startup: cannot return");
1201 	/*NOTREACHED*/
1202 }
1203 
1204 
1205 /*
1206  * Start CPU on user request.
1207  */
1208 /* ARGSUSED */
1209 int
1210 mp_cpu_start(struct cpu *cp)
1211 {
1212 	ASSERT(MUTEX_HELD(&cpu_lock));
1213 	return (0);
1214 }
1215 
1216 /*
1217  * Stop CPU on user request.
1218  */
1219 /* ARGSUSED */
1220 int
1221 mp_cpu_stop(struct cpu *cp)
1222 {
1223 	extern int cbe_psm_timer_mode;
1224 	ASSERT(MUTEX_HELD(&cpu_lock));
1225 
1226 	/*
1227 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
1228 	 * can't stop it.  (This is true only for machines with no TSC.)
1229 	 */
1230 
1231 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
1232 		return (1);
1233 
1234 	return (0);
1235 }
1236 
1237 /*
1238  * Power on CPU.
1239  */
1240 /* ARGSUSED */
1241 int
1242 mp_cpu_poweron(struct cpu *cp)
1243 {
1244 	ASSERT(MUTEX_HELD(&cpu_lock));
1245 	return (ENOTSUP);		/* not supported */
1246 }
1247 
1248 /*
1249  * Power off CPU.
1250  */
1251 /* ARGSUSED */
1252 int
1253 mp_cpu_poweroff(struct cpu *cp)
1254 {
1255 	ASSERT(MUTEX_HELD(&cpu_lock));
1256 	return (ENOTSUP);		/* not supported */
1257 }
1258 
1259 
1260 /*
1261  * Take the specified CPU out of participation in interrupts.
1262  */
1263 int
1264 cpu_disable_intr(struct cpu *cp)
1265 {
1266 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
1267 		return (EBUSY);
1268 
1269 	cp->cpu_flags &= ~CPU_ENABLE;
1270 	return (0);
1271 }
1272 
1273 /*
1274  * Allow the specified CPU to participate in interrupts.
1275  */
1276 void
1277 cpu_enable_intr(struct cpu *cp)
1278 {
1279 	ASSERT(MUTEX_HELD(&cpu_lock));
1280 	cp->cpu_flags |= CPU_ENABLE;
1281 	psm_enable_intr(cp->cpu_id);
1282 }
1283 
1284 
1285 
1286 static ushort_t *
1287 mp_map_warm_reset_vector()
1288 {
1289 	ushort_t *warm_reset_vector;
1290 
1291 	if (!(warm_reset_vector = (ushort_t *)psm_map_phys(WARM_RESET_VECTOR,
1292 	    sizeof (ushort_t *), PROT_READ|PROT_WRITE)))
1293 		return (NULL);
1294 
1295 	/*
1296 	 * setup secondary cpu bios boot up vector
1297 	 */
1298 	*warm_reset_vector = (ushort_t)((caddr_t)
1299 		((struct rm_platter *)rm_platter_va)->rm_code - rm_platter_va
1300 		+ ((ulong_t)rm_platter_va & 0xf));
1301 	warm_reset_vector++;
1302 	*warm_reset_vector = (ushort_t)(rm_platter_pa >> 4);
1303 
1304 	--warm_reset_vector;
1305 	return (warm_reset_vector);
1306 }
1307 
1308 static void
1309 mp_unmap_warm_reset_vector(ushort_t *warm_reset_vector)
1310 {
1311 	psm_unmap_phys((caddr_t)warm_reset_vector, sizeof (ushort_t *));
1312 }
1313 
1314 void
1315 mp_cpu_faulted_enter(struct cpu *cp)
1316 {
1317 	cmi_faulted_enter(cp);
1318 }
1319 
1320 void
1321 mp_cpu_faulted_exit(struct cpu *cp)
1322 {
1323 	cmi_faulted_exit(cp);
1324 }
1325 
1326 /*
1327  * The following two routines are used as context operators on threads belonging
1328  * to processes with a private LDT (see sysi86).  Due to the rarity of such
1329  * processes, these routines are currently written for best code readability and
1330  * organization rather than speed.  We could avoid checking x86_feature at every
1331  * context switch by installing different context ops, depending on the
1332  * x86_feature flags, at LDT creation time -- one for each combination of fast
1333  * syscall feature flags.
1334  */
1335 
1336 /*ARGSUSED*/
1337 void
1338 cpu_fast_syscall_disable(void *arg)
1339 {
1340 	if (x86_feature & X86_SEP)
1341 		cpu_sep_disable();
1342 	if (x86_feature & X86_ASYSC)
1343 		cpu_asysc_disable();
1344 }
1345 
1346 /*ARGSUSED*/
1347 void
1348 cpu_fast_syscall_enable(void *arg)
1349 {
1350 	if (x86_feature & X86_SEP)
1351 		cpu_sep_enable();
1352 	if (x86_feature & X86_ASYSC)
1353 		cpu_asysc_enable();
1354 }
1355 
1356 static void
1357 cpu_sep_enable(void)
1358 {
1359 	ASSERT(x86_feature & X86_SEP);
1360 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1361 
1362 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
1363 }
1364 
1365 static void
1366 cpu_sep_disable(void)
1367 {
1368 	ASSERT(x86_feature & X86_SEP);
1369 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1370 
1371 	/*
1372 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
1373 	 * the sysenter or sysexit instruction to trigger a #gp fault.
1374 	 */
1375 	wrmsr(MSR_INTC_SEP_CS, 0ULL);
1376 }
1377 
1378 static void
1379 cpu_asysc_enable(void)
1380 {
1381 	ASSERT(x86_feature & X86_ASYSC);
1382 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1383 
1384 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1385 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
1386 }
1387 
1388 static void
1389 cpu_asysc_disable(void)
1390 {
1391 	ASSERT(x86_feature & X86_ASYSC);
1392 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1393 
1394 	/*
1395 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
1396 	 * executing syscall or sysret with this bit off will incur a #ud trap.
1397 	 */
1398 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1399 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
1400 }
1401