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