xref: /titanic_50/usr/src/uts/i86pc/os/mp_startup.c (revision 4703203d9b3e06246d73931f07359a7ef70f47bf)
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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/thread.h>
31 #include <sys/cpuvar.h>
32 #include <sys/t_lock.h>
33 #include <sys/param.h>
34 #include <sys/proc.h>
35 #include <sys/disp.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 <vm/as.h>
46 #include <vm/seg_kmem.h>
47 #include <vm/seg_kp.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/pg.h>
58 #include <sys/cmt.h>
59 #include <sys/dtrace.h>
60 #include <sys/archsystm.h>
61 #include <sys/fp.h>
62 #include <sys/reboot.h>
63 #include <sys/kdi_machimpl.h>
64 #include <vm/hat_i86.h>
65 #include <sys/memnode.h>
66 #include <sys/pci_cfgspace.h>
67 #include <sys/mach_mmu.h>
68 #include <sys/sysmacros.h>
69 #include <sys/cpu_module.h>
70 
71 struct cpu	cpus[1];			/* CPU data */
72 struct cpu	*cpu[NCPU] = {&cpus[0]};	/* pointers to all CPUs */
73 cpu_core_t	cpu_core[NCPU];			/* cpu_core structures */
74 
75 /*
76  * Useful for disabling MP bring-up on a MP capable system.
77  */
78 int use_mp = 1;
79 
80 /*
81  * to be set by a PSM to indicate what cpus
82  * are sitting around on the system.
83  */
84 cpuset_t mp_cpus;
85 
86 /*
87  * This variable is used by the hat layer to decide whether or not
88  * critical sections are needed to prevent race conditions.  For sun4m,
89  * this variable is set once enough MP initialization has been done in
90  * order to allow cross calls.
91  */
92 int flushes_require_xcalls;
93 cpuset_t cpu_ready_set = 1;
94 
95 static 	void	mp_startup(void);
96 
97 static void cpu_sep_enable(void);
98 static void cpu_sep_disable(void);
99 static void cpu_asysc_enable(void);
100 static void cpu_asysc_disable(void);
101 
102 extern int tsc_gethrtime_enable;
103 
104 /*
105  * Init CPU info - get CPU type info for processor_info system call.
106  */
107 void
108 init_cpu_info(struct cpu *cp)
109 {
110 	processor_info_t *pi = &cp->cpu_type_info;
111 	char buf[CPU_IDSTRLEN];
112 
113 	/*
114 	 * Get clock-frequency property for the CPU.
115 	 */
116 	pi->pi_clock = cpu_freq;
117 
118 	/*
119 	 * Current frequency in Hz.
120 	 */
121 	cp->cpu_curr_clock = cpu_freq_hz;
122 
123 	/*
124 	 * Supported frequencies.
125 	 */
126 	cpu_set_supp_freqs(cp, NULL);
127 
128 	(void) strcpy(pi->pi_processor_type, "i386");
129 	if (fpu_exists)
130 		(void) strcpy(pi->pi_fputypes, "i387 compatible");
131 
132 	(void) cpuid_getidstr(cp, buf, sizeof (buf));
133 
134 	cp->cpu_idstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
135 	(void) strcpy(cp->cpu_idstr, buf);
136 
137 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_idstr);
138 
139 	(void) cpuid_getbrandstr(cp, buf, sizeof (buf));
140 	cp->cpu_brandstr = kmem_alloc(strlen(buf) + 1, KM_SLEEP);
141 	(void) strcpy(cp->cpu_brandstr, buf);
142 
143 	cmn_err(CE_CONT, "?cpu%d: %s\n", cp->cpu_id, cp->cpu_brandstr);
144 }
145 
146 /*
147  * Configure syscall support on this CPU.
148  */
149 /*ARGSUSED*/
150 static void
151 init_cpu_syscall(struct cpu *cp)
152 {
153 	kpreempt_disable();
154 
155 #if defined(__amd64)
156 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC)) {
157 
158 #if !defined(__lint)
159 		/*
160 		 * The syscall instruction imposes a certain ordering on
161 		 * segment selectors, so we double-check that ordering
162 		 * here.
163 		 */
164 		ASSERT(KDS_SEL == KCS_SEL + 8);
165 		ASSERT(UDS_SEL == U32CS_SEL + 8);
166 		ASSERT(UCS_SEL == U32CS_SEL + 16);
167 #endif
168 		/*
169 		 * Turn syscall/sysret extensions on.
170 		 */
171 		cpu_asysc_enable();
172 
173 		/*
174 		 * Program the magic registers ..
175 		 */
176 		wrmsr(MSR_AMD_STAR,
177 		    ((uint64_t)(U32CS_SEL << 16 | KCS_SEL)) << 32);
178 		wrmsr(MSR_AMD_LSTAR, (uint64_t)(uintptr_t)sys_syscall);
179 		wrmsr(MSR_AMD_CSTAR, (uint64_t)(uintptr_t)sys_syscall32);
180 
181 		/*
182 		 * This list of flags is masked off the incoming
183 		 * %rfl when we enter the kernel.
184 		 */
185 		wrmsr(MSR_AMD_SFMASK, (uint64_t)(uintptr_t)(PS_IE | PS_T));
186 	}
187 #endif
188 
189 	/*
190 	 * On 32-bit kernels, we use sysenter/sysexit because it's too
191 	 * hard to use syscall/sysret, and it is more portable anyway.
192 	 *
193 	 * On 64-bit kernels on Nocona machines, the 32-bit syscall
194 	 * variant isn't available to 32-bit applications, but sysenter is.
195 	 */
196 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP)) {
197 
198 #if !defined(__lint)
199 		/*
200 		 * The sysenter instruction imposes a certain ordering on
201 		 * segment selectors, so we double-check that ordering
202 		 * here. See "sysenter" in Intel document 245471-012, "IA-32
203 		 * Intel Architecture Software Developer's Manual Volume 2:
204 		 * Instruction Set Reference"
205 		 */
206 		ASSERT(KDS_SEL == KCS_SEL + 8);
207 
208 		ASSERT32(UCS_SEL == ((KCS_SEL + 16) | 3));
209 		ASSERT32(UDS_SEL == UCS_SEL + 8);
210 
211 		ASSERT64(U32CS_SEL == ((KCS_SEL + 16) | 3));
212 		ASSERT64(UDS_SEL == U32CS_SEL + 8);
213 #endif
214 
215 		cpu_sep_enable();
216 
217 		/*
218 		 * resume() sets this value to the base of the threads stack
219 		 * via a context handler.
220 		 */
221 		wrmsr(MSR_INTC_SEP_ESP, 0);
222 		wrmsr(MSR_INTC_SEP_EIP, (uint64_t)(uintptr_t)sys_sysenter);
223 	}
224 
225 	kpreempt_enable();
226 }
227 
228 /*
229  * Multiprocessor initialization.
230  *
231  * Allocate and initialize the cpu structure, TRAPTRACE buffer, and the
232  * startup and idle threads for the specified CPU.
233  */
234 struct cpu *
235 mp_startup_init(int cpun)
236 {
237 	struct cpu *cp;
238 	kthread_id_t tp;
239 	caddr_t	sp;
240 	proc_t *procp;
241 	extern int idle_cpu_prefer_mwait;
242 	extern void idle();
243 
244 #ifdef TRAPTRACE
245 	trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun];
246 #endif
247 
248 	ASSERT(cpun < NCPU && cpu[cpun] == NULL);
249 
250 	cp = kmem_zalloc(sizeof (*cp), KM_SLEEP);
251 	if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait)
252 		cp->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
253 
254 	procp = curthread->t_procp;
255 
256 	mutex_enter(&cpu_lock);
257 	/*
258 	 * Initialize the dispatcher first.
259 	 */
260 	disp_cpu_init(cp);
261 	mutex_exit(&cpu_lock);
262 
263 	cpu_vm_data_init(cp);
264 
265 	/*
266 	 * Allocate and initialize the startup thread for this CPU.
267 	 * Interrupt and process switch stacks get allocated later
268 	 * when the CPU starts running.
269 	 */
270 	tp = thread_create(NULL, 0, NULL, NULL, 0, procp,
271 	    TS_STOPPED, maxclsyspri);
272 
273 	/*
274 	 * Set state to TS_ONPROC since this thread will start running
275 	 * as soon as the CPU comes online.
276 	 *
277 	 * All the other fields of the thread structure are setup by
278 	 * thread_create().
279 	 */
280 	THREAD_ONPROC(tp, cp);
281 	tp->t_preempt = 1;
282 	tp->t_bound_cpu = cp;
283 	tp->t_affinitycnt = 1;
284 	tp->t_cpu = cp;
285 	tp->t_disp_queue = cp->cpu_disp;
286 
287 	/*
288 	 * Setup thread to start in mp_startup.
289 	 */
290 	sp = tp->t_stk;
291 	tp->t_pc = (uintptr_t)mp_startup;
292 	tp->t_sp = (uintptr_t)(sp - MINFRAME);
293 #if defined(__amd64)
294 	tp->t_sp -= STACK_ENTRY_ALIGN;		/* fake a call */
295 #endif
296 
297 	cp->cpu_id = cpun;
298 	cp->cpu_self = cp;
299 	cp->cpu_thread = tp;
300 	cp->cpu_lwp = NULL;
301 	cp->cpu_dispthread = tp;
302 	cp->cpu_dispatch_pri = DISP_PRIO(tp);
303 
304 	/*
305 	 * cpu_base_spl must be set explicitly here to prevent any blocking
306 	 * operations in mp_startup from causing the spl of the cpu to drop
307 	 * to 0 (allowing device interrupts before we're ready) in resume().
308 	 * cpu_base_spl MUST remain at LOCK_LEVEL until the cpu is CPU_READY.
309 	 * As an extra bit of security on DEBUG kernels, this is enforced with
310 	 * an assertion in mp_startup() -- before cpu_base_spl is set to its
311 	 * proper value.
312 	 */
313 	cp->cpu_base_spl = ipltospl(LOCK_LEVEL);
314 
315 	/*
316 	 * Now, initialize per-CPU idle thread for this CPU.
317 	 */
318 	tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1);
319 
320 	cp->cpu_idle_thread = tp;
321 
322 	tp->t_preempt = 1;
323 	tp->t_bound_cpu = cp;
324 	tp->t_affinitycnt = 1;
325 	tp->t_cpu = cp;
326 	tp->t_disp_queue = cp->cpu_disp;
327 
328 	/*
329 	 * Bootstrap the CPU's PG data
330 	 */
331 	pg_cpu_bootstrap(cp);
332 
333 	/*
334 	 * Perform CPC initialization on the new CPU.
335 	 */
336 	kcpc_hw_init(cp);
337 
338 	/*
339 	 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2
340 	 * for each CPU.
341 	 */
342 	setup_vaddr_for_ppcopy(cp);
343 
344 	/*
345 	 * Allocate page for new GDT and initialize from current GDT.
346 	 */
347 #if !defined(__lint)
348 	ASSERT((sizeof (*cp->cpu_gdt) * NGDT) <= PAGESIZE);
349 #endif
350 	cp->cpu_m.mcpu_gdt = kmem_zalloc(PAGESIZE, KM_SLEEP);
351 	bcopy(CPU->cpu_m.mcpu_gdt, cp->cpu_m.mcpu_gdt,
352 	    (sizeof (*cp->cpu_m.mcpu_gdt) * NGDT));
353 
354 #if defined(__i386)
355 	/*
356 	 * setup kernel %gs.
357 	 */
358 	set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA,
359 	    SEL_KPL, 0, 1);
360 #endif
361 
362 	/*
363 	 * If we have more than one node, each cpu gets a copy of IDT
364 	 * local to its node. If this is a Pentium box, we use cpu 0's
365 	 * IDT. cpu 0's IDT has been made read-only to workaround the
366 	 * cmpxchgl register bug
367 	 */
368 	if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) {
369 		struct machcpu *mcpu = &cp->cpu_m;
370 
371 		mcpu->mcpu_idt = kmem_alloc(sizeof (idt0), KM_SLEEP);
372 		bcopy(idt0, mcpu->mcpu_idt, sizeof (idt0));
373 	} else {
374 		cp->cpu_m.mcpu_idt = CPU->cpu_m.mcpu_idt;
375 	}
376 
377 	/*
378 	 * Get interrupt priority data from cpu 0.
379 	 */
380 	cp->cpu_pri_data = CPU->cpu_pri_data;
381 
382 	/*
383 	 * alloc space for cpuid info
384 	 */
385 	cpuid_alloc_space(cp);
386 
387 	/*
388 	 * alloc space for ucode_info
389 	 */
390 	ucode_alloc_space(cp);
391 
392 	hat_cpu_online(cp);
393 
394 #ifdef TRAPTRACE
395 	/*
396 	 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers
397 	 */
398 	ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP);
399 	ttc->ttc_next = ttc->ttc_first;
400 	ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize;
401 #endif
402 	/*
403 	 * Record that we have another CPU.
404 	 */
405 	mutex_enter(&cpu_lock);
406 	/*
407 	 * Initialize the interrupt threads for this CPU
408 	 */
409 	cpu_intr_alloc(cp, NINTR_THREADS);
410 	/*
411 	 * Add CPU to list of available CPUs.  It'll be on the active list
412 	 * after mp_startup().
413 	 */
414 	cpu_add_unit(cp);
415 	mutex_exit(&cpu_lock);
416 
417 	return (cp);
418 }
419 
420 /*
421  * Undo what was done in mp_startup_init
422  */
423 static void
424 mp_startup_fini(struct cpu *cp, int error)
425 {
426 	mutex_enter(&cpu_lock);
427 
428 	/*
429 	 * Remove the CPU from the list of available CPUs.
430 	 */
431 	cpu_del_unit(cp->cpu_id);
432 
433 	if (error == ETIMEDOUT) {
434 		/*
435 		 * The cpu was started, but never *seemed* to run any
436 		 * code in the kernel; it's probably off spinning in its
437 		 * own private world, though with potential references to
438 		 * our kmem-allocated IDTs and GDTs (for example).
439 		 *
440 		 * Worse still, it may actually wake up some time later,
441 		 * so rather than guess what it might or might not do, we
442 		 * leave the fundamental data structures intact.
443 		 */
444 		cp->cpu_flags = 0;
445 		mutex_exit(&cpu_lock);
446 		return;
447 	}
448 
449 	/*
450 	 * At this point, the only threads bound to this CPU should
451 	 * special per-cpu threads: it's idle thread, it's pause threads,
452 	 * and it's interrupt threads.  Clean these up.
453 	 */
454 	cpu_destroy_bound_threads(cp);
455 	cp->cpu_idle_thread = NULL;
456 
457 	/*
458 	 * Free the interrupt stack.
459 	 */
460 	segkp_release(segkp,
461 	    cp->cpu_intr_stack - (INTR_STACK_SIZE - SA(MINFRAME)));
462 
463 	mutex_exit(&cpu_lock);
464 
465 #ifdef TRAPTRACE
466 	/*
467 	 * Discard the trap trace buffer
468 	 */
469 	{
470 		trap_trace_ctl_t *ttc = &trap_trace_ctl[cp->cpu_id];
471 
472 		kmem_free((void *)ttc->ttc_first, trap_trace_bufsize);
473 		ttc->ttc_first = NULL;
474 	}
475 #endif
476 
477 	hat_cpu_offline(cp);
478 
479 	cpuid_free_space(cp);
480 
481 	ucode_free_space(cp);
482 
483 	if (cp->cpu_m.mcpu_idt != CPU->cpu_m.mcpu_idt)
484 		kmem_free(cp->cpu_m.mcpu_idt, sizeof (idt0));
485 	cp->cpu_m.mcpu_idt = NULL;
486 
487 	kmem_free(cp->cpu_m.mcpu_gdt, PAGESIZE);
488 	cp->cpu_m.mcpu_gdt = NULL;
489 
490 	teardown_vaddr_for_ppcopy(cp);
491 
492 	kcpc_hw_fini(cp);
493 
494 	cp->cpu_dispthread = NULL;
495 	cp->cpu_thread = NULL;	/* discarded by cpu_destroy_bound_threads() */
496 
497 	cpu_vm_data_destroy(cp);
498 
499 	mutex_enter(&cpu_lock);
500 	disp_cpu_fini(cp);
501 	mutex_exit(&cpu_lock);
502 
503 	if (cp->cpu_m.mcpu_mwait != NULL)
504 		cpuid_mwait_free(cp);
505 	kmem_free(cp, sizeof (*cp));
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_88)
531 int opteron_erratum_88;		/* if non-zero -> at least one cpu has it */
532 #endif
533 
534 #if defined(OPTERON_ERRATUM_91)
535 int opteron_erratum_91;		/* if non-zero -> at least one cpu has it */
536 #endif
537 
538 #if defined(OPTERON_ERRATUM_93)
539 int opteron_erratum_93;		/* if non-zero -> at least one cpu has it */
540 #endif
541 
542 #if defined(OPTERON_ERRATUM_95)
543 int opteron_erratum_95;		/* if non-zero -> at least one cpu has it */
544 #endif
545 
546 #if defined(OPTERON_ERRATUM_100)
547 int opteron_erratum_100;	/* if non-zero -> at least one cpu has it */
548 #endif
549 
550 #if defined(OPTERON_ERRATUM_108)
551 int opteron_erratum_108;	/* if non-zero -> at least one cpu has it */
552 #endif
553 
554 #if defined(OPTERON_ERRATUM_109)
555 int opteron_erratum_109;	/* if non-zero -> at least one cpu has it */
556 #endif
557 
558 #if defined(OPTERON_ERRATUM_121)
559 int opteron_erratum_121;	/* if non-zero -> at least one cpu has it */
560 #endif
561 
562 #if defined(OPTERON_ERRATUM_122)
563 int opteron_erratum_122;	/* if non-zero -> at least one cpu has it */
564 #endif
565 
566 #if defined(OPTERON_ERRATUM_123)
567 int opteron_erratum_123;	/* if non-zero -> at least one cpu has it */
568 #endif
569 
570 #if defined(OPTERON_ERRATUM_131)
571 int opteron_erratum_131;	/* if non-zero -> at least one cpu has it */
572 #endif
573 
574 #if defined(OPTERON_WORKAROUND_6336786)
575 int opteron_workaround_6336786;	/* non-zero -> WA relevant and applied */
576 int opteron_workaround_6336786_UP = 0;	/* Not needed for UP */
577 #endif
578 
579 #if defined(OPTERON_WORKAROUND_6323525)
580 int opteron_workaround_6323525;	/* if non-zero -> at least one cpu has it */
581 #endif
582 
583 static void
584 workaround_warning(cpu_t *cp, uint_t erratum)
585 {
586 	cmn_err(CE_WARN, "cpu%d: no workaround for erratum %u",
587 	    cp->cpu_id, erratum);
588 }
589 
590 static void
591 workaround_applied(uint_t erratum)
592 {
593 	if (erratum > 1000000)
594 		cmn_err(CE_CONT, "?workaround applied for cpu issue #%d\n",
595 		    erratum);
596 	else
597 		cmn_err(CE_CONT, "?workaround applied for cpu erratum #%d\n",
598 		    erratum);
599 }
600 
601 static void
602 msr_warning(cpu_t *cp, const char *rw, uint_t msr, int error)
603 {
604 	cmn_err(CE_WARN, "cpu%d: couldn't %smsr 0x%x, error %d",
605 	    cp->cpu_id, rw, msr, error);
606 }
607 
608 uint_t
609 workaround_errata(struct cpu *cpu)
610 {
611 	uint_t missing = 0;
612 
613 	ASSERT(cpu == CPU);
614 
615 	/*LINTED*/
616 	if (cpuid_opteron_erratum(cpu, 88) > 0) {
617 		/*
618 		 * SWAPGS May Fail To Read Correct GS Base
619 		 */
620 #if defined(OPTERON_ERRATUM_88)
621 		/*
622 		 * The workaround is an mfence in the relevant assembler code
623 		 */
624 		opteron_erratum_88++;
625 #else
626 		workaround_warning(cpu, 88);
627 		missing++;
628 #endif
629 	}
630 
631 	if (cpuid_opteron_erratum(cpu, 91) > 0) {
632 		/*
633 		 * Software Prefetches May Report A Page Fault
634 		 */
635 #if defined(OPTERON_ERRATUM_91)
636 		/*
637 		 * fix is in trap.c
638 		 */
639 		opteron_erratum_91++;
640 #else
641 		workaround_warning(cpu, 91);
642 		missing++;
643 #endif
644 	}
645 
646 	if (cpuid_opteron_erratum(cpu, 93) > 0) {
647 		/*
648 		 * RSM Auto-Halt Restart Returns to Incorrect RIP
649 		 */
650 #if defined(OPTERON_ERRATUM_93)
651 		/*
652 		 * fix is in trap.c
653 		 */
654 		opteron_erratum_93++;
655 #else
656 		workaround_warning(cpu, 93);
657 		missing++;
658 #endif
659 	}
660 
661 	/*LINTED*/
662 	if (cpuid_opteron_erratum(cpu, 95) > 0) {
663 		/*
664 		 * RET Instruction May Return to Incorrect EIP
665 		 */
666 #if defined(OPTERON_ERRATUM_95)
667 #if defined(_LP64)
668 		/*
669 		 * Workaround this by ensuring that 32-bit user code and
670 		 * 64-bit kernel code never occupy the same address
671 		 * range mod 4G.
672 		 */
673 		if (_userlimit32 > 0xc0000000ul)
674 			*(uintptr_t *)&_userlimit32 = 0xc0000000ul;
675 
676 		/*LINTED*/
677 		ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u);
678 		opteron_erratum_95++;
679 #endif	/* _LP64 */
680 #else
681 		workaround_warning(cpu, 95);
682 		missing++;
683 #endif
684 	}
685 
686 	if (cpuid_opteron_erratum(cpu, 100) > 0) {
687 		/*
688 		 * Compatibility Mode Branches Transfer to Illegal Address
689 		 */
690 #if defined(OPTERON_ERRATUM_100)
691 		/*
692 		 * fix is in trap.c
693 		 */
694 		opteron_erratum_100++;
695 #else
696 		workaround_warning(cpu, 100);
697 		missing++;
698 #endif
699 	}
700 
701 	/*LINTED*/
702 	if (cpuid_opteron_erratum(cpu, 108) > 0) {
703 		/*
704 		 * CPUID Instruction May Return Incorrect Model Number In
705 		 * Some Processors
706 		 */
707 #if defined(OPTERON_ERRATUM_108)
708 		/*
709 		 * (Our cpuid-handling code corrects the model number on
710 		 * those processors)
711 		 */
712 #else
713 		workaround_warning(cpu, 108);
714 		missing++;
715 #endif
716 	}
717 
718 	/*LINTED*/
719 	if (cpuid_opteron_erratum(cpu, 109) > 0) do {
720 		/*
721 		 * Certain Reverse REP MOVS May Produce Unpredictable Behaviour
722 		 */
723 #if defined(OPTERON_ERRATUM_109)
724 		/*
725 		 * The "workaround" is to print a warning to upgrade the BIOS
726 		 */
727 		uint64_t value;
728 		const uint_t msr = MSR_AMD_PATCHLEVEL;
729 		int err;
730 
731 		if ((err = checked_rdmsr(msr, &value)) != 0) {
732 			msr_warning(cpu, "rd", msr, err);
733 			workaround_warning(cpu, 109);
734 			missing++;
735 		}
736 		if (value == 0)
737 			opteron_erratum_109++;
738 #else
739 		workaround_warning(cpu, 109);
740 		missing++;
741 #endif
742 	/*CONSTANTCONDITION*/
743 	} while (0);
744 
745 	/*LINTED*/
746 	if (cpuid_opteron_erratum(cpu, 121) > 0) {
747 		/*
748 		 * Sequential Execution Across Non_Canonical Boundary Caused
749 		 * Processor Hang
750 		 */
751 #if defined(OPTERON_ERRATUM_121)
752 #if defined(_LP64)
753 		/*
754 		 * Erratum 121 is only present in long (64 bit) mode.
755 		 * Workaround is to include the page immediately before the
756 		 * va hole to eliminate the possibility of system hangs due to
757 		 * sequential execution across the va hole boundary.
758 		 */
759 		if (opteron_erratum_121)
760 			opteron_erratum_121++;
761 		else {
762 			if (hole_start) {
763 				hole_start -= PAGESIZE;
764 			} else {
765 				/*
766 				 * hole_start not yet initialized by
767 				 * mmu_init. Initialize hole_start
768 				 * with value to be subtracted.
769 				 */
770 				hole_start = PAGESIZE;
771 			}
772 			opteron_erratum_121++;
773 		}
774 #endif	/* _LP64 */
775 #else
776 		workaround_warning(cpu, 121);
777 		missing++;
778 #endif
779 	}
780 
781 	/*LINTED*/
782 	if (cpuid_opteron_erratum(cpu, 122) > 0) do {
783 		/*
784 		 * TLB Flush Filter May Cause Coherency Problem in
785 		 * Multiprocessor Systems
786 		 */
787 #if defined(OPTERON_ERRATUM_122)
788 		uint64_t value;
789 		const uint_t msr = MSR_AMD_HWCR;
790 		int error;
791 
792 		/*
793 		 * Erratum 122 is only present in MP configurations (multi-core
794 		 * or multi-processor).
795 		 */
796 		if (!opteron_erratum_122 && lgrp_plat_node_cnt == 1 &&
797 		    cpuid_get_ncpu_per_chip(cpu) == 1)
798 			break;
799 
800 		/* disable TLB Flush Filter */
801 
802 		if ((error = checked_rdmsr(msr, &value)) != 0) {
803 			msr_warning(cpu, "rd", msr, error);
804 			workaround_warning(cpu, 122);
805 			missing++;
806 		} else {
807 			value |= (uint64_t)AMD_HWCR_FFDIS;
808 			if ((error = checked_wrmsr(msr, value)) != 0) {
809 				msr_warning(cpu, "wr", msr, error);
810 				workaround_warning(cpu, 122);
811 				missing++;
812 			}
813 		}
814 		opteron_erratum_122++;
815 #else
816 		workaround_warning(cpu, 122);
817 		missing++;
818 #endif
819 	/*CONSTANTCONDITION*/
820 	} while (0);
821 
822 	/*LINTED*/
823 	if (cpuid_opteron_erratum(cpu, 123) > 0) do {
824 		/*
825 		 * Bypassed Reads May Cause Data Corruption of System Hang in
826 		 * Dual Core Processors
827 		 */
828 #if defined(OPTERON_ERRATUM_123)
829 		uint64_t value;
830 		const uint_t msr = MSR_AMD_PATCHLEVEL;
831 		int err;
832 
833 		/*
834 		 * Erratum 123 applies only to multi-core cpus.
835 		 */
836 		if (cpuid_get_ncpu_per_chip(cpu) < 2)
837 			break;
838 
839 		/*
840 		 * The "workaround" is to print a warning to upgrade the BIOS
841 		 */
842 		if ((err = checked_rdmsr(msr, &value)) != 0) {
843 			msr_warning(cpu, "rd", msr, err);
844 			workaround_warning(cpu, 123);
845 			missing++;
846 		}
847 		if (value == 0)
848 			opteron_erratum_123++;
849 #else
850 		workaround_warning(cpu, 123);
851 		missing++;
852 
853 #endif
854 	/*CONSTANTCONDITION*/
855 	} while (0);
856 
857 	/*LINTED*/
858 	if (cpuid_opteron_erratum(cpu, 131) > 0) do {
859 		/*
860 		 * Multiprocessor Systems with Four or More Cores May Deadlock
861 		 * Waiting for a Probe Response
862 		 */
863 #if defined(OPTERON_ERRATUM_131)
864 		uint64_t nbcfg;
865 		const uint_t msr = MSR_AMD_NB_CFG;
866 		const uint64_t wabits =
867 		    AMD_NB_CFG_SRQ_HEARTBEAT | AMD_NB_CFG_SRQ_SPR;
868 		int error;
869 
870 		/*
871 		 * Erratum 131 applies to any system with four or more cores.
872 		 */
873 		if (opteron_erratum_131)
874 			break;
875 
876 		if (lgrp_plat_node_cnt * cpuid_get_ncpu_per_chip(cpu) < 4)
877 			break;
878 
879 		/*
880 		 * Print a warning if neither of the workarounds for
881 		 * erratum 131 is present.
882 		 */
883 		if ((error = checked_rdmsr(msr, &nbcfg)) != 0) {
884 			msr_warning(cpu, "rd", msr, error);
885 			workaround_warning(cpu, 131);
886 			missing++;
887 		} else if ((nbcfg & wabits) == 0) {
888 			opteron_erratum_131++;
889 		} else {
890 			/* cannot have both workarounds set */
891 			ASSERT((nbcfg & wabits) != wabits);
892 		}
893 #else
894 		workaround_warning(cpu, 131);
895 		missing++;
896 #endif
897 	/*CONSTANTCONDITION*/
898 	} while (0);
899 
900 	/*
901 	 * This isn't really an erratum, but for convenience the
902 	 * detection/workaround code lives here and in cpuid_opteron_erratum.
903 	 */
904 	if (cpuid_opteron_erratum(cpu, 6336786) > 0) {
905 #if defined(OPTERON_WORKAROUND_6336786)
906 		/*
907 		 * Disable C1-Clock ramping on multi-core/multi-processor
908 		 * K8 platforms to guard against TSC drift.
909 		 */
910 		if (opteron_workaround_6336786) {
911 			opteron_workaround_6336786++;
912 		} else if ((lgrp_plat_node_cnt *
913 		    cpuid_get_ncpu_per_chip(cpu) > 1) ||
914 		    opteron_workaround_6336786_UP) {
915 			int	node;
916 			uint8_t data;
917 
918 			for (node = 0; node < lgrp_plat_node_cnt; node++) {
919 				/*
920 				 * Clear PMM7[1:0] (function 3, offset 0x87)
921 				 * Northbridge device is the node id + 24.
922 				 */
923 				data = pci_getb_func(0, node + 24, 3, 0x87);
924 				data &= 0xFC;
925 				pci_putb_func(0, node + 24, 3, 0x87, data);
926 			}
927 			opteron_workaround_6336786++;
928 		}
929 #else
930 		workaround_warning(cpu, 6336786);
931 		missing++;
932 #endif
933 	}
934 
935 	/*LINTED*/
936 	/*
937 	 * Mutex primitives don't work as expected.
938 	 */
939 	if (cpuid_opteron_erratum(cpu, 6323525) > 0) {
940 #if defined(OPTERON_WORKAROUND_6323525)
941 		/*
942 		 * This problem only occurs with 2 or more cores. If bit in
943 		 * MSR_BU_CFG set, then not applicable. The workaround
944 		 * is to patch the semaphone routines with the lfence
945 		 * instruction to provide necessary load memory barrier with
946 		 * possible subsequent read-modify-write ops.
947 		 *
948 		 * It is too early in boot to call the patch routine so
949 		 * set erratum variable to be done in startup_end().
950 		 */
951 		if (opteron_workaround_6323525) {
952 			opteron_workaround_6323525++;
953 		} else if ((x86_feature & X86_SSE2) && ((lgrp_plat_node_cnt *
954 		    cpuid_get_ncpu_per_chip(cpu)) > 1)) {
955 			if ((xrdmsr(MSR_BU_CFG) & 0x02) == 0)
956 				opteron_workaround_6323525++;
957 		}
958 #else
959 		workaround_warning(cpu, 6323525);
960 		missing++;
961 #endif
962 	}
963 
964 	return (missing);
965 }
966 
967 void
968 workaround_errata_end()
969 {
970 #if defined(OPTERON_ERRATUM_88)
971 	if (opteron_erratum_88)
972 		workaround_applied(88);
973 #endif
974 #if defined(OPTERON_ERRATUM_91)
975 	if (opteron_erratum_91)
976 		workaround_applied(91);
977 #endif
978 #if defined(OPTERON_ERRATUM_93)
979 	if (opteron_erratum_93)
980 		workaround_applied(93);
981 #endif
982 #if defined(OPTERON_ERRATUM_95)
983 	if (opteron_erratum_95)
984 		workaround_applied(95);
985 #endif
986 #if defined(OPTERON_ERRATUM_100)
987 	if (opteron_erratum_100)
988 		workaround_applied(100);
989 #endif
990 #if defined(OPTERON_ERRATUM_108)
991 	if (opteron_erratum_108)
992 		workaround_applied(108);
993 #endif
994 #if defined(OPTERON_ERRATUM_109)
995 	if (opteron_erratum_109) {
996 		cmn_err(CE_WARN,
997 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
998 		    " processor\nerratum 109 was not detected; updating your"
999 		    " system's BIOS to a version\ncontaining this"
1000 		    " microcode patch is HIGHLY recommended or erroneous"
1001 		    " system\noperation may occur.\n");
1002 	}
1003 #endif
1004 #if defined(OPTERON_ERRATUM_121)
1005 	if (opteron_erratum_121)
1006 		workaround_applied(121);
1007 #endif
1008 #if defined(OPTERON_ERRATUM_122)
1009 	if (opteron_erratum_122)
1010 		workaround_applied(122);
1011 #endif
1012 #if defined(OPTERON_ERRATUM_123)
1013 	if (opteron_erratum_123) {
1014 		cmn_err(CE_WARN,
1015 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1016 		    " processor\nerratum 123 was not detected; updating your"
1017 		    " system's BIOS to a version\ncontaining this"
1018 		    " microcode patch is HIGHLY recommended or erroneous"
1019 		    " system\noperation may occur.\n");
1020 	}
1021 #endif
1022 #if defined(OPTERON_ERRATUM_131)
1023 	if (opteron_erratum_131) {
1024 		cmn_err(CE_WARN,
1025 		    "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)"
1026 		    " processor\nerratum 131 was not detected; updating your"
1027 		    " system's BIOS to a version\ncontaining this"
1028 		    " microcode patch is HIGHLY recommended or erroneous"
1029 		    " system\noperation may occur.\n");
1030 	}
1031 #endif
1032 #if defined(OPTERON_WORKAROUND_6336786)
1033 	if (opteron_workaround_6336786)
1034 		workaround_applied(6336786);
1035 #endif
1036 #if defined(OPTERON_WORKAROUND_6323525)
1037 	if (opteron_workaround_6323525)
1038 		workaround_applied(6323525);
1039 #endif
1040 }
1041 
1042 static cpuset_t procset;
1043 
1044 /*
1045  * Start a single cpu, assuming that the kernel context is available
1046  * to successfully start another cpu.
1047  *
1048  * (For example, real mode code is mapped into the right place
1049  * in memory and is ready to be run.)
1050  */
1051 int
1052 start_cpu(processorid_t who)
1053 {
1054 	void *ctx;
1055 	cpu_t *cp;
1056 	int delays;
1057 	int error = 0;
1058 
1059 	ASSERT(who != 0);
1060 
1061 	/*
1062 	 * Check if there's at least a Mbyte of kmem available
1063 	 * before attempting to start the cpu.
1064 	 */
1065 	if (kmem_avail() < 1024 * 1024) {
1066 		/*
1067 		 * Kick off a reap in case that helps us with
1068 		 * later attempts ..
1069 		 */
1070 		kmem_reap();
1071 		return (ENOMEM);
1072 	}
1073 
1074 	cp = mp_startup_init(who);
1075 	if ((ctx = mach_cpucontext_alloc(cp)) == NULL ||
1076 	    (error = mach_cpu_start(cp, ctx)) != 0) {
1077 
1078 		/*
1079 		 * Something went wrong before we even started it
1080 		 */
1081 		if (ctx)
1082 			cmn_err(CE_WARN,
1083 			    "cpu%d: failed to start error %d",
1084 			    cp->cpu_id, error);
1085 		else
1086 			cmn_err(CE_WARN,
1087 			    "cpu%d: failed to allocate context", cp->cpu_id);
1088 
1089 		if (ctx)
1090 			mach_cpucontext_free(cp, ctx, error);
1091 		else
1092 			error = EAGAIN;		/* hmm. */
1093 		mp_startup_fini(cp, error);
1094 		return (error);
1095 	}
1096 
1097 	for (delays = 0; !CPU_IN_SET(procset, who); delays++) {
1098 		if (delays == 500) {
1099 			/*
1100 			 * After five seconds, things are probably looking
1101 			 * a bit bleak - explain the hang.
1102 			 */
1103 			cmn_err(CE_NOTE, "cpu%d: started, "
1104 			    "but not running in the kernel yet", who);
1105 		} else if (delays > 2000) {
1106 			/*
1107 			 * We waited at least 20 seconds, bail ..
1108 			 */
1109 			error = ETIMEDOUT;
1110 			cmn_err(CE_WARN, "cpu%d: timed out", who);
1111 			mach_cpucontext_free(cp, ctx, error);
1112 			mp_startup_fini(cp, error);
1113 			return (error);
1114 		}
1115 
1116 		/*
1117 		 * wait at least 10ms, then check again..
1118 		 */
1119 		delay(USEC_TO_TICK_ROUNDUP(10000));
1120 	}
1121 
1122 	mach_cpucontext_free(cp, ctx, 0);
1123 
1124 	if (tsc_gethrtime_enable)
1125 		tsc_sync_master(who);
1126 
1127 	if (dtrace_cpu_init != NULL) {
1128 		/*
1129 		 * DTrace CPU initialization expects cpu_lock to be held.
1130 		 */
1131 		mutex_enter(&cpu_lock);
1132 		(*dtrace_cpu_init)(who);
1133 		mutex_exit(&cpu_lock);
1134 	}
1135 
1136 	while (!CPU_IN_SET(cpu_ready_set, who))
1137 		delay(1);
1138 
1139 	return (0);
1140 }
1141 
1142 
1143 /*ARGSUSED*/
1144 void
1145 start_other_cpus(int cprboot)
1146 {
1147 	uint_t who;
1148 	uint_t skipped = 0;
1149 	uint_t bootcpuid = 0;
1150 
1151 	/*
1152 	 * Initialize our own cpu_info.
1153 	 */
1154 	init_cpu_info(CPU);
1155 
1156 	/*
1157 	 * Initialize our syscall handlers
1158 	 */
1159 	init_cpu_syscall(CPU);
1160 
1161 	/*
1162 	 * Take the boot cpu out of the mp_cpus set because we know
1163 	 * it's already running.  Add it to the cpu_ready_set for
1164 	 * precisely the same reason.
1165 	 */
1166 	CPUSET_DEL(mp_cpus, bootcpuid);
1167 	CPUSET_ADD(cpu_ready_set, bootcpuid);
1168 
1169 	/*
1170 	 * if only 1 cpu or not using MP, skip the rest of this
1171 	 */
1172 	if (CPUSET_ISNULL(mp_cpus) || use_mp == 0) {
1173 		if (use_mp == 0)
1174 			cmn_err(CE_CONT, "?***** Not in MP mode\n");
1175 		goto done;
1176 	}
1177 
1178 	/*
1179 	 * perform such initialization as is needed
1180 	 * to be able to take CPUs on- and off-line.
1181 	 */
1182 	cpu_pause_init();
1183 
1184 	xc_init();		/* initialize processor crosscalls */
1185 
1186 	if (mach_cpucontext_init() != 0)
1187 		goto done;
1188 
1189 	flushes_require_xcalls = 1;
1190 
1191 	/*
1192 	 * We lock our affinity to the master CPU to ensure that all slave CPUs
1193 	 * do their TSC syncs with the same CPU.
1194 	 */
1195 	affinity_set(CPU_CURRENT);
1196 
1197 	for (who = 0; who < NCPU; who++) {
1198 
1199 		if (!CPU_IN_SET(mp_cpus, who))
1200 			continue;
1201 		ASSERT(who != bootcpuid);
1202 		if (ncpus >= max_ncpus) {
1203 			skipped = who;
1204 			continue;
1205 		}
1206 		if (start_cpu(who) != 0)
1207 			CPUSET_DEL(mp_cpus, who);
1208 	}
1209 
1210 	/* Free the space allocated to hold the microcode file */
1211 	ucode_free();
1212 
1213 	affinity_clear();
1214 
1215 	if (skipped) {
1216 		cmn_err(CE_NOTE,
1217 		    "System detected %d cpus, but "
1218 		    "only %d cpu(s) were enabled during boot.",
1219 		    skipped + 1, ncpus);
1220 		cmn_err(CE_NOTE,
1221 		    "Use \"boot-ncpus\" parameter to enable more CPU(s). "
1222 		    "See eeprom(1M).");
1223 	}
1224 
1225 done:
1226 	workaround_errata_end();
1227 	mach_cpucontext_fini();
1228 
1229 	cmi_post_mpstartup();
1230 }
1231 
1232 /*
1233  * Dummy functions - no i86pc platforms support dynamic cpu allocation.
1234  */
1235 /*ARGSUSED*/
1236 int
1237 mp_cpu_configure(int cpuid)
1238 {
1239 	return (ENOTSUP);		/* not supported */
1240 }
1241 
1242 /*ARGSUSED*/
1243 int
1244 mp_cpu_unconfigure(int cpuid)
1245 {
1246 	return (ENOTSUP);		/* not supported */
1247 }
1248 
1249 /*
1250  * Startup function for 'other' CPUs (besides boot cpu).
1251  * Called from real_mode_start.
1252  *
1253  * WARNING: until CPU_READY is set, mp_startup and routines called by
1254  * mp_startup should not call routines (e.g. kmem_free) that could call
1255  * hat_unload which requires CPU_READY to be set.
1256  */
1257 void
1258 mp_startup(void)
1259 {
1260 	struct cpu *cp = CPU;
1261 	uint_t new_x86_feature;
1262 
1263 	/*
1264 	 * We need to get TSC on this proc synced (i.e., any delta
1265 	 * from cpu0 accounted for) as soon as we can, because many
1266 	 * many things use gethrtime/pc_gethrestime, including
1267 	 * interrupts, cmn_err, etc.
1268 	 */
1269 
1270 	/* Let cpu0 continue into tsc_sync_master() */
1271 	CPUSET_ATOMIC_ADD(procset, cp->cpu_id);
1272 
1273 	if (tsc_gethrtime_enable)
1274 		tsc_sync_slave();
1275 
1276 	/*
1277 	 * Once this was done from assembly, but it's safer here; if
1278 	 * it blocks, we need to be able to swtch() to and from, and
1279 	 * since we get here by calling t_pc, we need to do that call
1280 	 * before swtch() overwrites it.
1281 	 */
1282 
1283 	(void) (*ap_mlsetup)();
1284 
1285 	new_x86_feature = cpuid_pass1(cp);
1286 
1287 	/*
1288 	 * We need to Sync MTRR with cpu0's MTRR. We have to do
1289 	 * this with interrupts disabled.
1290 	 */
1291 	if (x86_feature & X86_MTRR)
1292 		mtrr_sync();
1293 
1294 	/*
1295 	 * Set up TSC_AUX to contain the cpuid for this processor
1296 	 * for the rdtscp instruction.
1297 	 */
1298 	if (x86_feature & X86_TSCP)
1299 		(void) wrmsr(MSR_AMD_TSCAUX, cp->cpu_id);
1300 
1301 	/*
1302 	 * Initialize this CPU's syscall handlers
1303 	 */
1304 	init_cpu_syscall(cp);
1305 
1306 	/*
1307 	 * Enable interrupts with spl set to LOCK_LEVEL. LOCK_LEVEL is the
1308 	 * highest level at which a routine is permitted to block on
1309 	 * an adaptive mutex (allows for cpu poke interrupt in case
1310 	 * the cpu is blocked on a mutex and halts). Setting LOCK_LEVEL blocks
1311 	 * device interrupts that may end up in the hat layer issuing cross
1312 	 * calls before CPU_READY is set.
1313 	 */
1314 	splx(ipltospl(LOCK_LEVEL));
1315 	sti();
1316 
1317 	/*
1318 	 * Do a sanity check to make sure this new CPU is a sane thing
1319 	 * to add to the collection of processors running this system.
1320 	 *
1321 	 * XXX	Clearly this needs to get more sophisticated, if x86
1322 	 * systems start to get built out of heterogenous CPUs; as is
1323 	 * likely to happen once the number of processors in a configuration
1324 	 * gets large enough.
1325 	 */
1326 	if ((x86_feature & new_x86_feature) != x86_feature) {
1327 		cmn_err(CE_CONT, "?cpu%d: %b\n",
1328 		    cp->cpu_id, new_x86_feature, FMT_X86_FEATURE);
1329 		cmn_err(CE_WARN, "cpu%d feature mismatch", cp->cpu_id);
1330 	}
1331 
1332 	/*
1333 	 * We do not support cpus with mixed monitor/mwait support if the
1334 	 * boot cpu supports monitor/mwait.
1335 	 */
1336 	if ((x86_feature & ~new_x86_feature) & X86_MWAIT)
1337 		panic("unsupported mixed cpu monitor/mwait support detected");
1338 
1339 	/*
1340 	 * We could be more sophisticated here, and just mark the CPU
1341 	 * as "faulted" but at this point we'll opt for the easier
1342 	 * answer of dieing horribly.  Provided the boot cpu is ok,
1343 	 * the system can be recovered by booting with use_mp set to zero.
1344 	 */
1345 	if (workaround_errata(cp) != 0)
1346 		panic("critical workaround(s) missing for cpu%d", cp->cpu_id);
1347 
1348 	cpuid_pass2(cp);
1349 	cpuid_pass3(cp);
1350 	(void) cpuid_pass4(cp);
1351 
1352 	init_cpu_info(cp);
1353 
1354 	mutex_enter(&cpu_lock);
1355 	/*
1356 	 * Processor group initialization for this CPU is dependent on the
1357 	 * cpuid probing, which must be done in the context of the current
1358 	 * CPU.
1359 	 */
1360 	pghw_physid_create(cp);
1361 	pg_cpu_init(cp);
1362 	pg_cmt_cpu_startup(cp);
1363 
1364 	cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_ENABLE | CPU_EXISTS;
1365 	cpu_add_active(cp);
1366 
1367 	if (dtrace_cpu_init != NULL) {
1368 		(*dtrace_cpu_init)(cp->cpu_id);
1369 	}
1370 
1371 	/*
1372 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
1373 	 */
1374 	ucode_check(cp);
1375 
1376 	mutex_exit(&cpu_lock);
1377 
1378 	/*
1379 	 * Enable preemption here so that contention for any locks acquired
1380 	 * later in mp_startup may be preempted if the thread owning those
1381 	 * locks is continously executing on other CPUs (for example, this
1382 	 * CPU must be preemptible to allow other CPUs to pause it during their
1383 	 * startup phases).  It's safe to enable preemption here because the
1384 	 * CPU state is pretty-much fully constructed.
1385 	 */
1386 	curthread->t_preempt = 0;
1387 
1388 	add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi);
1389 
1390 	/* The base spl should still be at LOCK LEVEL here */
1391 	ASSERT(cp->cpu_base_spl == ipltospl(LOCK_LEVEL));
1392 	set_base_spl();		/* Restore the spl to its proper value */
1393 
1394 	(void) spl0();				/* enable interrupts */
1395 
1396 	/*
1397 	 * Set up the CPU module for this CPU.  This can't be done before
1398 	 * this CPU is made CPU_READY, because we may (in heterogeneous systems)
1399 	 * need to go load another CPU module.  The act of attempting to load
1400 	 * a module may trigger a cross-call, which will ASSERT unless this
1401 	 * cpu is CPU_READY.
1402 	 */
1403 	cmi_init();
1404 
1405 	if (x86_feature & X86_MCA)
1406 		cmi_mca_init();
1407 
1408 	if (boothowto & RB_DEBUG)
1409 		kdi_cpu_init();
1410 
1411 	/*
1412 	 * Setting the bit in cpu_ready_set must be the last operation in
1413 	 * processor initialization; the boot CPU will continue to boot once
1414 	 * it sees this bit set for all active CPUs.
1415 	 */
1416 	CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id);
1417 
1418 	/*
1419 	 * Because mp_startup() gets fired off after init() starts, we
1420 	 * can't use the '?' trick to do 'boot -v' printing - so we
1421 	 * always direct the 'cpu .. online' messages to the log.
1422 	 */
1423 	cmn_err(CE_CONT, "!cpu%d initialization complete - online\n",
1424 	    cp->cpu_id);
1425 
1426 	/*
1427 	 * Now we are done with the startup thread, so free it up.
1428 	 */
1429 	thread_exit();
1430 	panic("mp_startup: cannot return");
1431 	/*NOTREACHED*/
1432 }
1433 
1434 
1435 /*
1436  * Start CPU on user request.
1437  */
1438 /* ARGSUSED */
1439 int
1440 mp_cpu_start(struct cpu *cp)
1441 {
1442 	ASSERT(MUTEX_HELD(&cpu_lock));
1443 	return (0);
1444 }
1445 
1446 /*
1447  * Stop CPU on user request.
1448  */
1449 /* ARGSUSED */
1450 int
1451 mp_cpu_stop(struct cpu *cp)
1452 {
1453 	extern int cbe_psm_timer_mode;
1454 	ASSERT(MUTEX_HELD(&cpu_lock));
1455 
1456 	/*
1457 	 * If TIMER_PERIODIC mode is used, CPU0 is the one running it;
1458 	 * can't stop it.  (This is true only for machines with no TSC.)
1459 	 */
1460 
1461 	if ((cbe_psm_timer_mode == TIMER_PERIODIC) && (cp->cpu_id == 0))
1462 		return (1);
1463 
1464 	return (0);
1465 }
1466 
1467 /*
1468  * Take the specified CPU out of participation in interrupts.
1469  */
1470 int
1471 cpu_disable_intr(struct cpu *cp)
1472 {
1473 	if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS)
1474 		return (EBUSY);
1475 
1476 	cp->cpu_flags &= ~CPU_ENABLE;
1477 	return (0);
1478 }
1479 
1480 /*
1481  * Allow the specified CPU to participate in interrupts.
1482  */
1483 void
1484 cpu_enable_intr(struct cpu *cp)
1485 {
1486 	ASSERT(MUTEX_HELD(&cpu_lock));
1487 	cp->cpu_flags |= CPU_ENABLE;
1488 	psm_enable_intr(cp->cpu_id);
1489 }
1490 
1491 
1492 
1493 void
1494 mp_cpu_faulted_enter(struct cpu *cp)
1495 {
1496 	cmi_faulted_enter(cp);
1497 }
1498 
1499 void
1500 mp_cpu_faulted_exit(struct cpu *cp)
1501 {
1502 	cmi_faulted_exit(cp);
1503 }
1504 
1505 /*
1506  * The following two routines are used as context operators on threads belonging
1507  * to processes with a private LDT (see sysi86).  Due to the rarity of such
1508  * processes, these routines are currently written for best code readability and
1509  * organization rather than speed.  We could avoid checking x86_feature at every
1510  * context switch by installing different context ops, depending on the
1511  * x86_feature flags, at LDT creation time -- one for each combination of fast
1512  * syscall feature flags.
1513  */
1514 
1515 /*ARGSUSED*/
1516 void
1517 cpu_fast_syscall_disable(void *arg)
1518 {
1519 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
1520 		cpu_sep_disable();
1521 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
1522 		cpu_asysc_disable();
1523 }
1524 
1525 /*ARGSUSED*/
1526 void
1527 cpu_fast_syscall_enable(void *arg)
1528 {
1529 	if ((x86_feature & (X86_MSR | X86_SEP)) == (X86_MSR | X86_SEP))
1530 		cpu_sep_enable();
1531 	if ((x86_feature & (X86_MSR | X86_ASYSC)) == (X86_MSR | X86_ASYSC))
1532 		cpu_asysc_enable();
1533 }
1534 
1535 static void
1536 cpu_sep_enable(void)
1537 {
1538 	ASSERT(x86_feature & X86_SEP);
1539 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1540 
1541 	wrmsr(MSR_INTC_SEP_CS, (uint64_t)(uintptr_t)KCS_SEL);
1542 }
1543 
1544 static void
1545 cpu_sep_disable(void)
1546 {
1547 	ASSERT(x86_feature & X86_SEP);
1548 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1549 
1550 	/*
1551 	 * Setting the SYSENTER_CS_MSR register to 0 causes software executing
1552 	 * the sysenter or sysexit instruction to trigger a #gp fault.
1553 	 */
1554 	wrmsr(MSR_INTC_SEP_CS, 0);
1555 }
1556 
1557 static void
1558 cpu_asysc_enable(void)
1559 {
1560 	ASSERT(x86_feature & X86_ASYSC);
1561 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1562 
1563 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) |
1564 	    (uint64_t)(uintptr_t)AMD_EFER_SCE);
1565 }
1566 
1567 static void
1568 cpu_asysc_disable(void)
1569 {
1570 	ASSERT(x86_feature & X86_ASYSC);
1571 	ASSERT(curthread->t_preempt || getpil() >= LOCK_LEVEL);
1572 
1573 	/*
1574 	 * Turn off the SCE (syscall enable) bit in the EFER register. Software
1575 	 * executing syscall or sysret with this bit off will incur a #ud trap.
1576 	 */
1577 	wrmsr(MSR_AMD_EFER, rdmsr(MSR_AMD_EFER) &
1578 	    ~((uint64_t)(uintptr_t)AMD_EFER_SCE));
1579 }
1580