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