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