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 #include <sys/pci_cfgspace.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 extern void init_intr_threads(struct cpu *); 235 236 struct cpu_tables *tablesp; 237 rm_platter_t *real_mode_platter = (rm_platter_t *)rm_platter_va; 238 239 #ifdef TRAPTRACE 240 trap_trace_ctl_t *ttc = &trap_trace_ctl[cpun]; 241 #endif 242 243 ASSERT(cpun < NCPU && cpu[cpun] == NULL); 244 245 if ((cp = kmem_zalloc(sizeof (*cp), KM_NOSLEEP)) == NULL) { 246 panic("mp_startup_init: cpu%d: " 247 "no memory for cpu structure", cpun); 248 /*NOTREACHED*/ 249 } 250 procp = curthread->t_procp; 251 252 mutex_enter(&cpu_lock); 253 /* 254 * Initialize the dispatcher first. 255 */ 256 disp_cpu_init(cp); 257 mutex_exit(&cpu_lock); 258 259 cpu_vm_data_init(cp); 260 261 /* 262 * Allocate and initialize the startup thread for this CPU. 263 * Interrupt and process switch stacks get allocated later 264 * when the CPU starts running. 265 */ 266 tp = thread_create(NULL, 0, NULL, NULL, 0, procp, 267 TS_STOPPED, maxclsyspri); 268 269 /* 270 * Set state to TS_ONPROC since this thread will start running 271 * as soon as the CPU comes online. 272 * 273 * All the other fields of the thread structure are setup by 274 * thread_create(). 275 */ 276 THREAD_ONPROC(tp, cp); 277 tp->t_preempt = 1; 278 tp->t_bound_cpu = cp; 279 tp->t_affinitycnt = 1; 280 tp->t_cpu = cp; 281 tp->t_disp_queue = cp->cpu_disp; 282 283 /* 284 * Setup thread to start in mp_startup. 285 */ 286 sp = tp->t_stk; 287 tp->t_pc = (uintptr_t)mp_startup; 288 tp->t_sp = (uintptr_t)(sp - MINFRAME); 289 290 cp->cpu_id = cpun; 291 cp->cpu_self = cp; 292 cp->cpu_mask = 1 << cpun; 293 cp->cpu_thread = tp; 294 cp->cpu_lwp = NULL; 295 cp->cpu_dispthread = tp; 296 cp->cpu_dispatch_pri = DISP_PRIO(tp); 297 298 /* 299 * Now, initialize per-CPU idle thread for this CPU. 300 */ 301 tp = thread_create(NULL, PAGESIZE, idle, NULL, 0, procp, TS_ONPROC, -1); 302 303 cp->cpu_idle_thread = tp; 304 305 tp->t_preempt = 1; 306 tp->t_bound_cpu = cp; 307 tp->t_affinitycnt = 1; 308 tp->t_cpu = cp; 309 tp->t_disp_queue = cp->cpu_disp; 310 311 /* 312 * Bootstrap the CPU for CMT aware scheduling 313 * The rest of the initialization will happen from 314 * mp_startup() 315 */ 316 chip_bootstrap_cpu(cp); 317 318 /* 319 * Perform CPC intialization on the new CPU. 320 */ 321 kcpc_hw_init(cp); 322 323 /* 324 * Allocate virtual addresses for cpu_caddr1 and cpu_caddr2 325 * for each CPU. 326 */ 327 328 setup_vaddr_for_ppcopy(cp); 329 330 /* 331 * Allocate space for page directory, stack, tss, gdt and idt. 332 * This assumes that kmem_alloc will return memory which is aligned 333 * to the next higher power of 2 or a page(if size > MAXABIG) 334 * If this assumption goes wrong at any time due to change in 335 * kmem alloc, things may not work as the page directory has to be 336 * page aligned 337 */ 338 if ((tablesp = kmem_zalloc(sizeof (*tablesp), KM_NOSLEEP)) == NULL) 339 panic("mp_startup_init: cpu%d cannot allocate tables", cpun); 340 341 if ((uintptr_t)tablesp & ~MMU_STD_PAGEMASK) { 342 kmem_free(tablesp, sizeof (struct cpu_tables)); 343 size = sizeof (struct cpu_tables) + MMU_STD_PAGESIZE; 344 tablesp = kmem_zalloc(size, KM_NOSLEEP); 345 tablesp = (struct cpu_tables *) 346 (((uintptr_t)tablesp + MMU_STD_PAGESIZE) & 347 MMU_STD_PAGEMASK); 348 } 349 350 ntss = cp->cpu_tss = &tablesp->ct_tss; 351 cp->cpu_gdt = tablesp->ct_gdt; 352 bcopy(CPU->cpu_gdt, cp->cpu_gdt, NGDT * (sizeof (user_desc_t))); 353 354 #if defined(__amd64) 355 356 /* 357 * #DF (double fault). 358 */ 359 ntss->tss_ist1 = 360 (uint64_t)&tablesp->ct_stack[sizeof (tablesp->ct_stack)]; 361 362 #elif defined(__i386) 363 364 ntss->tss_esp0 = ntss->tss_esp1 = ntss->tss_esp2 = ntss->tss_esp = 365 (uint32_t)&tablesp->ct_stack[sizeof (tablesp->ct_stack)]; 366 367 ntss->tss_ss0 = ntss->tss_ss1 = ntss->tss_ss2 = ntss->tss_ss = KDS_SEL; 368 369 ntss->tss_eip = (uint32_t)mp_startup; 370 371 ntss->tss_cs = KCS_SEL; 372 ntss->tss_fs = KFS_SEL; 373 ntss->tss_gs = KGS_SEL; 374 375 /* 376 * setup kernel %gs. 377 */ 378 set_usegd(&cp->cpu_gdt[GDT_GS], cp, sizeof (struct cpu) -1, SDT_MEMRWA, 379 SEL_KPL, 0, 1); 380 381 #endif /* __i386 */ 382 383 /* 384 * Set I/O bit map offset equal to size of TSS segment limit 385 * for no I/O permission map. This will cause all user I/O 386 * instructions to generate #gp fault. 387 */ 388 ntss->tss_bitmapbase = sizeof (*ntss); 389 390 /* 391 * setup kernel tss. 392 */ 393 set_syssegd((system_desc_t *)&cp->cpu_gdt[GDT_KTSS], cp->cpu_tss, 394 sizeof (*cp->cpu_tss) -1, SDT_SYSTSS, SEL_KPL); 395 396 /* 397 * If we have more than one node, each cpu gets a copy of IDT 398 * local to its node. If this is a Pentium box, we use cpu 0's 399 * IDT. cpu 0's IDT has been made read-only to workaround the 400 * cmpxchgl register bug 401 */ 402 cp->cpu_idt = CPU->cpu_idt; 403 if (system_hardware.hd_nodes && x86_type != X86_TYPE_P5) { 404 cp->cpu_idt = kmem_alloc(sizeof (idt0), KM_SLEEP); 405 bcopy(idt0, cp->cpu_idt, sizeof (idt0)); 406 } 407 408 /* 409 * Get interrupt priority data from cpu 0 410 */ 411 cp->cpu_pri_data = CPU->cpu_pri_data; 412 413 hat_cpu_online(cp); 414 415 /* Should remove all entries for the current process/thread here */ 416 417 /* 418 * Fill up the real mode platter to make it easy for real mode code to 419 * kick it off. This area should really be one passed by boot to kernel 420 * and guaranteed to be below 1MB and aligned to 16 bytes. Should also 421 * have identical physical and virtual address in paged mode. 422 */ 423 real_mode_platter->rm_idt_base = cp->cpu_idt; 424 real_mode_platter->rm_idt_lim = sizeof (idt0) - 1; 425 real_mode_platter->rm_gdt_base = cp->cpu_gdt; 426 real_mode_platter->rm_gdt_lim = sizeof (gdt0) -1; 427 real_mode_platter->rm_pdbr = getcr3(); 428 real_mode_platter->rm_cpu = cpun; 429 real_mode_platter->rm_x86feature = x86_feature; 430 real_mode_platter->rm_cr4 = cr4_value; 431 432 #if defined(__amd64) 433 if (getcr3() > 0xffffffffUL) 434 panic("Cannot initialize CPUs; kernel's 64-bit page tables\n" 435 "located above 4G in physical memory (@ 0x%llx).", 436 (unsigned long long)getcr3()); 437 438 /* 439 * Setup pseudo-descriptors for temporary GDT and IDT for use ONLY 440 * by code in real_mode_start(): 441 * 442 * GDT[0]: NULL selector 443 * GDT[1]: 64-bit CS: Long = 1, Present = 1, bits 12, 11 = 1 444 * 445 * Clear the IDT as interrupts will be off and a limit of 0 will cause 446 * the CPU to triple fault and reset on an NMI, seemingly as reasonable 447 * a course of action as any other, though it may cause the entire 448 * platform to reset in some cases... 449 */ 450 real_mode_platter->rm_temp_gdt[0] = 0ULL; 451 real_mode_platter->rm_temp_gdt[TEMPGDT_KCODE64] = 0x20980000000000ULL; 452 453 real_mode_platter->rm_temp_gdt_lim = (ushort_t) 454 (sizeof (real_mode_platter->rm_temp_gdt) - 1); 455 real_mode_platter->rm_temp_gdt_base = rm_platter_pa + 456 (uint32_t)(&((rm_platter_t *)0)->rm_temp_gdt); 457 458 real_mode_platter->rm_temp_idt_lim = 0; 459 real_mode_platter->rm_temp_idt_base = 0; 460 461 /* 462 * Since the CPU needs to jump to protected mode using an identity 463 * mapped address, we need to calculate it here. 464 */ 465 real_mode_platter->rm_longmode64_addr = rm_platter_pa + 466 ((uint32_t)long_mode_64 - (uint32_t)real_mode_start); 467 #endif /* __amd64 */ 468 469 #ifdef TRAPTRACE 470 /* 471 * If this is a TRAPTRACE kernel, allocate TRAPTRACE buffers for this 472 * CPU. 473 */ 474 ttc->ttc_first = (uintptr_t)kmem_zalloc(trap_trace_bufsize, KM_SLEEP); 475 ttc->ttc_next = ttc->ttc_first; 476 ttc->ttc_limit = ttc->ttc_first + trap_trace_bufsize; 477 #endif 478 479 /* 480 * Record that we have another CPU. 481 */ 482 mutex_enter(&cpu_lock); 483 /* 484 * Initialize the interrupt threads for this CPU 485 */ 486 init_intr_threads(cp); 487 /* 488 * Add CPU to list of available CPUs. It'll be on the active list 489 * after mp_startup(). 490 */ 491 cpu_add_unit(cp); 492 mutex_exit(&cpu_lock); 493 } 494 495 /* 496 * Apply workarounds for known errata, and warn about those that are absent. 497 * 498 * System vendors occasionally create configurations which contain different 499 * revisions of the CPUs that are almost but not exactly the same. At the 500 * time of writing, this meant that their clock rates were the same, their 501 * feature sets were the same, but the required workaround were -not- 502 * necessarily the same. So, this routine is invoked on -every- CPU soon 503 * after starting to make sure that the resulting system contains the most 504 * pessimal set of workarounds needed to cope with *any* of the CPUs in the 505 * system. 506 * 507 * workaround_errata is invoked early in mlsetup() for CPU 0, and in 508 * mp_startup() for all slave CPUs. Slaves process workaround_errata prior 509 * to acknowledging their readiness to the master, so this routine will 510 * never be executed by multiple CPUs in parallel, thus making updates to 511 * global data safe. 512 * 513 * These workarounds are based on Rev 3.57 of the Revision Guide for 514 * AMD Athlon(tm) 64 and AMD Opteron(tm) Processors, August 2005. 515 */ 516 517 #if defined(OPTERON_ERRATUM_91) 518 int opteron_erratum_91; /* if non-zero -> at least one cpu has it */ 519 #endif 520 521 #if defined(OPTERON_ERRATUM_93) 522 int opteron_erratum_93; /* if non-zero -> at least one cpu has it */ 523 #endif 524 525 #if defined(OPTERON_ERRATUM_100) 526 int opteron_erratum_100; /* if non-zero -> at least one cpu has it */ 527 #endif 528 529 #if defined(OPTERON_ERRATUM_109) 530 int opteron_erratum_109; /* if non-zero -> at least one cpu has it */ 531 #endif 532 533 #if defined(OPTERON_ERRATUM_121) 534 int opteron_erratum_121; /* if non-zero -> at least one cpu has it */ 535 #endif 536 537 #if defined(OPTERON_ERRATUM_122) 538 int opteron_erratum_122; /* if non-zero -> at least one cpu has it */ 539 #endif 540 541 #if defined(OPTERON_ERRATUM_123) 542 int opteron_erratum_123; /* if non-zero -> at least one cpu has it */ 543 #endif 544 545 #if defined(OPTERON_ERRATUM_131) 546 int opteron_erratum_131; /* if non-zero -> at least one cpu has it */ 547 #endif 548 549 #if defined(OPTERON_WORKAROUND_6336786) 550 int opteron_workaround_6336786; /* non-zero -> WA relevant and applied */ 551 int opteron_workaround_6336786_UP = 0; /* Not needed for UP */ 552 #endif 553 554 #define WARNING(cpu, n) \ 555 cmn_err(CE_WARN, "cpu%d: no workaround for erratum %d", \ 556 (cpu)->cpu_id, (n)) 557 558 uint_t 559 workaround_errata(struct cpu *cpu) 560 { 561 uint_t missing = 0; 562 563 ASSERT(cpu == CPU); 564 565 /*LINTED*/ 566 if (cpuid_opteron_erratum(cpu, 88) > 0) { 567 /* 568 * SWAPGS May Fail To Read Correct GS Base 569 */ 570 #if defined(OPTERON_ERRATUM_88) 571 /* 572 * The workaround is an mfence in the relevant assembler code 573 */ 574 #else 575 WARNING(cpu, 88); 576 missing++; 577 #endif 578 } 579 580 if (cpuid_opteron_erratum(cpu, 91) > 0) { 581 /* 582 * Software Prefetches May Report A Page Fault 583 */ 584 #if defined(OPTERON_ERRATUM_91) 585 /* 586 * fix is in trap.c 587 */ 588 opteron_erratum_91++; 589 #else 590 WARNING(cpu, 91); 591 missing++; 592 #endif 593 } 594 595 if (cpuid_opteron_erratum(cpu, 93) > 0) { 596 /* 597 * RSM Auto-Halt Restart Returns to Incorrect RIP 598 */ 599 #if defined(OPTERON_ERRATUM_93) 600 /* 601 * fix is in trap.c 602 */ 603 opteron_erratum_93++; 604 #else 605 WARNING(cpu, 93); 606 missing++; 607 #endif 608 } 609 610 /*LINTED*/ 611 if (cpuid_opteron_erratum(cpu, 95) > 0) { 612 /* 613 * RET Instruction May Return to Incorrect EIP 614 */ 615 #if defined(OPTERON_ERRATUM_95) 616 #if defined(_LP64) 617 /* 618 * Workaround this by ensuring that 32-bit user code and 619 * 64-bit kernel code never occupy the same address 620 * range mod 4G. 621 */ 622 if (_userlimit32 > 0xc0000000ul) 623 *(uintptr_t *)&_userlimit32 = 0xc0000000ul; 624 625 /*LINTED*/ 626 ASSERT((uint32_t)COREHEAP_BASE == 0xc0000000u); 627 #endif /* _LP64 */ 628 #else 629 WARNING(cpu, 95); 630 missing++; 631 #endif /* OPTERON_ERRATUM_95 */ 632 } 633 634 if (cpuid_opteron_erratum(cpu, 100) > 0) { 635 /* 636 * Compatibility Mode Branches Transfer to Illegal Address 637 */ 638 #if defined(OPTERON_ERRATUM_100) 639 /* 640 * fix is in trap.c 641 */ 642 opteron_erratum_100++; 643 #else 644 WARNING(cpu, 100); 645 missing++; 646 #endif 647 } 648 649 /*LINTED*/ 650 if (cpuid_opteron_erratum(cpu, 108) > 0) { 651 /* 652 * CPUID Instruction May Return Incorrect Model Number In 653 * Some Processors 654 */ 655 #if defined(OPTERON_ERRATUM_108) 656 /* 657 * (Our cpuid-handling code corrects the model number on 658 * those processors) 659 */ 660 #else 661 WARNING(cpu, 108); 662 missing++; 663 #endif 664 } 665 666 /*LINTED*/ 667 if (cpuid_opteron_erratum(cpu, 109) > 0) { 668 /* 669 * Certain Reverse REP MOVS May Produce Unpredictable Behaviour 670 */ 671 #if defined(OPTERON_ERRATUM_109) 672 673 /* workaround is to print a warning to upgrade BIOS */ 674 if (rdmsr(MSR_AMD_PATCHLEVEL) == 0) 675 opteron_erratum_109++; 676 #else 677 WARNING(cpu, 109); 678 missing++; 679 #endif 680 } 681 /*LINTED*/ 682 if (cpuid_opteron_erratum(cpu, 121) > 0) { 683 /* 684 * Sequential Execution Across Non_Canonical Boundary Caused 685 * Processor Hang 686 */ 687 #if defined(OPTERON_ERRATUM_121) 688 static int lma; 689 690 if (opteron_erratum_121) 691 opteron_erratum_121++; 692 693 /* 694 * Erratum 121 is only present in long (64 bit) mode. 695 * Workaround is to include the page immediately before the 696 * va hole to eliminate the possibility of system hangs due to 697 * sequential execution across the va hole boundary. 698 */ 699 if (lma == 0) { 700 /* 701 * check LMA once: assume all cpus are in long mode 702 * or not. 703 */ 704 lma = 1; 705 706 if (rdmsr(MSR_AMD_EFER) & AMD_EFER_LMA) { 707 if (hole_start) { 708 hole_start -= PAGESIZE; 709 } else { 710 /* 711 * hole_start not yet initialized by 712 * mmu_init. Initialize hole_start 713 * with value to be subtracted. 714 */ 715 hole_start = PAGESIZE; 716 } 717 opteron_erratum_121++; 718 } 719 } 720 #else 721 WARNING(cpu, 121); 722 missing++; 723 #endif 724 } 725 726 /*LINTED*/ 727 if (cpuid_opteron_erratum(cpu, 122) > 0) { 728 /* 729 * TLB Flush Filter May Cause Cohenrency Problem in 730 * Multiprocessor Systems 731 */ 732 #if defined(OPTERON_ERRATUM_122) 733 /* 734 * Erratum 122 is only present in MP configurations (multi-core 735 * or multi-processor). 736 */ 737 738 if (opteron_erratum_122 || lgrp_plat_node_cnt > 1 || 739 cpuid_get_ncpu_per_chip(cpu) > 1) { 740 /* disable TLB Flush Filter */ 741 wrmsr(MSR_AMD_HWCR, rdmsr(MSR_AMD_HWCR) | 742 (uint64_t)(uintptr_t)AMD_HWCR_FFDIS); 743 opteron_erratum_122++; 744 } 745 746 #else 747 WARNING(cpu, 122); 748 missing++; 749 #endif 750 } 751 752 #if defined(OPTERON_ERRATUM_123) 753 /*LINTED*/ 754 if (cpuid_opteron_erratum(cpu, 123) > 0) { 755 /* 756 * Bypassed Reads May Cause Data Corruption of System Hang in 757 * Dual Core Processors 758 */ 759 /* 760 * Erratum 123 applies only to multi-core cpus. 761 */ 762 763 if (cpuid_get_ncpu_per_chip(cpu) > 1) { 764 /* workaround is to print a warning to upgrade BIOS */ 765 if (rdmsr(MSR_AMD_PATCHLEVEL) == 0) 766 opteron_erratum_123++; 767 } 768 } 769 #endif 770 771 #if defined(OPTERON_ERRATUM_131) 772 /*LINTED*/ 773 if (cpuid_opteron_erratum(cpu, 131) > 0) { 774 /* 775 * Multiprocessor Systems with Four or More Cores May Deadlock 776 * Waiting for a Probe Response 777 */ 778 /* 779 * Erratum 131 applies to any system with four or more cores. 780 */ 781 if ((opteron_erratum_131 == 0) && ((lgrp_plat_node_cnt * 782 cpuid_get_ncpu_per_chip(cpu)) >= 4)) { 783 /* 784 * Workaround is to print a warning to upgrade 785 * the BIOS 786 */ 787 if (!(rdmsr(MSR_AMD_NB_CFG) & AMD_NB_CFG_SRQ_HEARTBEAT)) 788 opteron_erratum_131++; 789 } 790 } 791 #endif 792 793 #if defined(OPTERON_WORKAROUND_6336786) 794 /* 795 * This isn't really erratum, but for convenience the 796 * detection/workaround code lives here and in cpuid_opteron_erratum. 797 */ 798 if (cpuid_opteron_erratum(cpu, 6336786) > 0) { 799 int node; 800 uint8_t data; 801 802 /* 803 * Disable C1-Clock ramping on multi-core/multi-processor 804 * K8 platforms to guard against TSC drift. 805 */ 806 if (opteron_workaround_6336786) { 807 opteron_workaround_6336786++; 808 } else if ((lgrp_plat_node_cnt * 809 cpuid_get_ncpu_per_chip(cpu) >= 2) || 810 opteron_workaround_6336786_UP) { 811 for (node = 0; node < lgrp_plat_node_cnt; node++) { 812 /* 813 * Clear PMM7[1:0] (function 3, offset 0x87) 814 * Northbridge device is the node id + 24. 815 */ 816 data = pci_getb_func(0, node + 24, 3, 0x87); 817 data &= 0xFC; 818 pci_putb_func(0, node + 24, 3, 0x87, data); 819 } 820 opteron_workaround_6336786++; 821 } 822 } 823 #endif 824 return (missing); 825 } 826 827 void 828 workaround_errata_end() 829 { 830 #if defined(OPTERON_ERRATUM_109) 831 if (opteron_erratum_109) { 832 cmn_err(CE_WARN, 833 "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)" 834 " processor\nerratum 109 was not detected; updating your" 835 " system's BIOS to a version\ncontaining this" 836 " microcode patch is HIGHLY recommended or erroneous" 837 " system\noperation may occur.\n"); 838 } 839 #endif /* OPTERON_ERRATUM_109 */ 840 #if defined(OPTERON_ERRATUM_123) 841 if (opteron_erratum_123) { 842 cmn_err(CE_WARN, 843 "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)" 844 " processor\nerratum 123 was not detected; updating your" 845 " system's BIOS to a version\ncontaining this" 846 " microcode patch is HIGHLY recommended or erroneous" 847 " system\noperation may occur.\n"); 848 } 849 #endif /* OPTERON_ERRATUM_123 */ 850 #if defined(OPTERON_ERRATUM_131) 851 if (opteron_erratum_131) { 852 cmn_err(CE_WARN, 853 "BIOS microcode patch for AMD Athlon(tm) 64/Opteron(tm)" 854 " processor\nerratum 131 was not detected; updating your" 855 " system's BIOS to a version\ncontaining this" 856 " microcode patch is HIGHLY recommended or erroneous" 857 " system\noperation may occur.\n"); 858 } 859 #endif /* OPTERON_ERRATUM_131 */ 860 } 861 862 static ushort_t *mp_map_warm_reset_vector(); 863 static void mp_unmap_warm_reset_vector(ushort_t *warm_reset_vector); 864 865 /*ARGSUSED*/ 866 void 867 start_other_cpus(int cprboot) 868 { 869 unsigned who; 870 int cpuid = getbootcpuid(); 871 int delays = 0; 872 int started_cpu; 873 ushort_t *warm_reset_vector = NULL; 874 extern int procset; 875 876 /* 877 * Initialize our own cpu_info. 878 */ 879 init_cpu_info(CPU); 880 881 /* 882 * Initialize our syscall handlers 883 */ 884 init_cpu_syscall(CPU); 885 886 /* 887 * if only 1 cpu or not using MP, skip the rest of this 888 */ 889 if (!(mp_cpus & ~(1 << cpuid)) || use_mp == 0) { 890 if (use_mp == 0) 891 cmn_err(CE_CONT, "?***** Not in MP mode\n"); 892 goto done; 893 } 894 895 /* 896 * perform such initialization as is needed 897 * to be able to take CPUs on- and off-line. 898 */ 899 cpu_pause_init(); 900 901 xc_init(); /* initialize processor crosscalls */ 902 903 /* 904 * Copy the real mode code at "real_mode_start" to the 905 * page at rm_platter_va. 906 */ 907 warm_reset_vector = mp_map_warm_reset_vector(); 908 if (warm_reset_vector == NULL) 909 goto done; 910 911 bcopy((caddr_t)real_mode_start, 912 (caddr_t)((rm_platter_t *)rm_platter_va)->rm_code, 913 (size_t)real_mode_end - (size_t)real_mode_start); 914 915 flushes_require_xcalls = 1; 916 917 affinity_set(CPU_CURRENT); 918 919 for (who = 0; who < NCPU; who++) { 920 if (who == cpuid) 921 continue; 922 923 if ((mp_cpus & (1 << who)) == 0) 924 continue; 925 926 mp_startup_init(who); 927 started_cpu = 1; 928 (*cpu_startf)(who, rm_platter_pa); 929 930 while ((procset & (1 << who)) == 0) { 931 932 delay(1); 933 if (++delays > (20 * hz)) { 934 935 cmn_err(CE_WARN, 936 "cpu%d failed to start", who); 937 938 mutex_enter(&cpu_lock); 939 cpu[who]->cpu_flags = 0; 940 cpu_vm_data_destroy(cpu[who]); 941 cpu_del_unit(who); 942 mutex_exit(&cpu_lock); 943 944 started_cpu = 0; 945 break; 946 } 947 } 948 if (!started_cpu) 949 continue; 950 if (tsc_gethrtime_enable) 951 tsc_sync_master(who); 952 953 954 if (dtrace_cpu_init != NULL) { 955 /* 956 * DTrace CPU initialization expects cpu_lock 957 * to be held. 958 */ 959 mutex_enter(&cpu_lock); 960 (*dtrace_cpu_init)(who); 961 mutex_exit(&cpu_lock); 962 } 963 } 964 965 affinity_clear(); 966 967 for (who = 0; who < NCPU; who++) { 968 if (who == cpuid) 969 continue; 970 971 if (!(procset & (1 << who))) 972 continue; 973 974 while (!(cpu_ready_set & (1 << who))) 975 delay(1); 976 } 977 978 done: 979 workaround_errata_end(); 980 981 if (warm_reset_vector != NULL) 982 mp_unmap_warm_reset_vector(warm_reset_vector); 983 hat_unload(kas.a_hat, (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE, 984 HAT_UNLOAD); 985 } 986 987 /* 988 * Dummy functions - no i86pc platforms support dynamic cpu allocation. 989 */ 990 /*ARGSUSED*/ 991 int 992 mp_cpu_configure(int cpuid) 993 { 994 return (ENOTSUP); /* not supported */ 995 } 996 997 /*ARGSUSED*/ 998 int 999 mp_cpu_unconfigure(int cpuid) 1000 { 1001 return (ENOTSUP); /* not supported */ 1002 } 1003 1004 /* 1005 * Startup function for 'other' CPUs (besides boot cpu). 1006 * Resumed from cpu_startup. 1007 */ 1008 void 1009 mp_startup(void) 1010 { 1011 struct cpu *cp = CPU; 1012 extern int procset; 1013 uint_t new_x86_feature; 1014 1015 new_x86_feature = cpuid_pass1(cp); 1016 1017 /* 1018 * We need to Sync MTRR with cpu0's MTRR. We have to do 1019 * this with interrupts disabled. 1020 */ 1021 if (x86_feature & X86_MTRR) 1022 mtrr_sync(); 1023 /* 1024 * Enable machine check architecture 1025 */ 1026 if (x86_feature & X86_MCA) 1027 setup_mca(); 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 add_cpunode2devtree(cp->cpu_id, cp->cpu_m.mcpu_cpi); 1075 1076 mutex_enter(&cpu_lock); 1077 procset |= 1 << cp->cpu_id; 1078 mutex_exit(&cpu_lock); 1079 1080 if (tsc_gethrtime_enable) 1081 tsc_sync_slave(); 1082 1083 mutex_enter(&cpu_lock); 1084 /* 1085 * It's unfortunate that chip_cpu_init() has to be called here. 1086 * It really belongs in cpu_add_unit(), but unfortunately it is 1087 * dependent on the cpuid probing, which must be done in the 1088 * context of the current CPU. Care must be taken on x86 to ensure 1089 * that mp_startup can safely block even though chip_cpu_init() and 1090 * cpu_add_active() have not yet been called. 1091 */ 1092 chip_cpu_init(cp); 1093 chip_cpu_startup(cp); 1094 1095 cp->cpu_flags |= CPU_RUNNING | CPU_READY | CPU_ENABLE | CPU_EXISTS; 1096 cpu_add_active(cp); 1097 mutex_exit(&cpu_lock); 1098 1099 (void) spl0(); /* enable interrupts */ 1100 1101 if (boothowto & RB_DEBUG) 1102 kdi_dvec_cpu_init(cp); 1103 1104 /* 1105 * Setting the bit in cpu_ready_set must be the last operation in 1106 * processor initialization; the boot CPU will continue to boot once 1107 * it sees this bit set for all active CPUs. 1108 */ 1109 CPUSET_ATOMIC_ADD(cpu_ready_set, cp->cpu_id); 1110 1111 /* 1112 * Because mp_startup() gets fired off after init() starts, we 1113 * can't use the '?' trick to do 'boot -v' printing - so we 1114 * always direct the 'cpu .. online' messages to the log. 1115 */ 1116 cmn_err(CE_CONT, "!cpu%d initialization complete - online\n", 1117 cp->cpu_id); 1118 1119 /* 1120 * Now we are done with the startup thread, so free it up. 1121 */ 1122 thread_exit(); 1123 panic("mp_startup: cannot return"); 1124 /*NOTREACHED*/ 1125 } 1126 1127 1128 /* 1129 * Start CPU on user request. 1130 */ 1131 /* ARGSUSED */ 1132 int 1133 mp_cpu_start(struct cpu *cp) 1134 { 1135 ASSERT(MUTEX_HELD(&cpu_lock)); 1136 if (cp->cpu_id == getbootcpuid()) 1137 return (EBUSY); /* Cannot start boot CPU */ 1138 return (0); 1139 } 1140 1141 /* 1142 * Stop CPU on user request. 1143 */ 1144 /* ARGSUSED */ 1145 int 1146 mp_cpu_stop(struct cpu *cp) 1147 { 1148 ASSERT(MUTEX_HELD(&cpu_lock)); 1149 if (cp->cpu_id == getbootcpuid()) 1150 return (EBUSY); /* Cannot stop boot CPU */ 1151 1152 return (0); 1153 } 1154 1155 /* 1156 * Power on CPU. 1157 */ 1158 /* ARGSUSED */ 1159 int 1160 mp_cpu_poweron(struct cpu *cp) 1161 { 1162 ASSERT(MUTEX_HELD(&cpu_lock)); 1163 return (ENOTSUP); /* not supported */ 1164 } 1165 1166 /* 1167 * Power off CPU. 1168 */ 1169 /* ARGSUSED */ 1170 int 1171 mp_cpu_poweroff(struct cpu *cp) 1172 { 1173 ASSERT(MUTEX_HELD(&cpu_lock)); 1174 return (ENOTSUP); /* not supported */ 1175 } 1176 1177 1178 /* 1179 * Take the specified CPU out of participation in interrupts. 1180 */ 1181 int 1182 cpu_disable_intr(struct cpu *cp) 1183 { 1184 /* 1185 * cannot disable interrupts on boot cpu 1186 */ 1187 if (cp == cpu[getbootcpuid()]) 1188 return (EBUSY); 1189 1190 if (psm_disable_intr(cp->cpu_id) != DDI_SUCCESS) 1191 return (EBUSY); 1192 1193 cp->cpu_flags &= ~CPU_ENABLE; 1194 return (0); 1195 } 1196 1197 /* 1198 * Allow the specified CPU to participate in interrupts. 1199 */ 1200 void 1201 cpu_enable_intr(struct cpu *cp) 1202 { 1203 ASSERT(MUTEX_HELD(&cpu_lock)); 1204 if (cp == cpu[getbootcpuid()]) 1205 return; 1206 1207 cp->cpu_flags |= CPU_ENABLE; 1208 psm_enable_intr(cp->cpu_id); 1209 } 1210 1211 1212 /* 1213 * return the cpu id of the initial startup cpu 1214 */ 1215 processorid_t 1216 getbootcpuid(void) 1217 { 1218 return (0); 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 /*ARGSUSED*/ 1250 void 1251 mp_cpu_faulted_enter(struct cpu *cp) 1252 {} 1253 1254 /*ARGSUSED*/ 1255 void 1256 mp_cpu_faulted_exit(struct cpu *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