mp_x86.c revision 234208
1/*- 2 * Copyright (c) 1996, by Steve Passe 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. The name of the developer may NOT be used to endorse or promote products 11 * derived from this software without specific prior written permission. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26#include <sys/cdefs.h> 27__FBSDID("$FreeBSD: head/sys/i386/i386/mp_machdep.c 234208 2012-04-13 07:18:19Z avg $"); 28 29#include "opt_apic.h" 30#include "opt_cpu.h" 31#include "opt_kstack_pages.h" 32#include "opt_pmap.h" 33#include "opt_sched.h" 34#include "opt_smp.h" 35 36#if !defined(lint) 37#if !defined(SMP) 38#error How did you get here? 39#endif 40 41#ifndef DEV_APIC 42#error The apic device is required for SMP, add "device apic" to your config file. 43#endif 44#if defined(CPU_DISABLE_CMPXCHG) && !defined(COMPILING_LINT) 45#error SMP not supported with CPU_DISABLE_CMPXCHG 46#endif 47#endif /* not lint */ 48 49#include <sys/param.h> 50#include <sys/systm.h> 51#include <sys/bus.h> 52#include <sys/cons.h> /* cngetc() */ 53#include <sys/cpuset.h> 54#ifdef GPROF 55#include <sys/gmon.h> 56#endif 57#include <sys/kernel.h> 58#include <sys/ktr.h> 59#include <sys/lock.h> 60#include <sys/malloc.h> 61#include <sys/memrange.h> 62#include <sys/mutex.h> 63#include <sys/pcpu.h> 64#include <sys/proc.h> 65#include <sys/sched.h> 66#include <sys/smp.h> 67#include <sys/sysctl.h> 68 69#include <vm/vm.h> 70#include <vm/vm_param.h> 71#include <vm/pmap.h> 72#include <vm/vm_kern.h> 73#include <vm/vm_extern.h> 74 75#include <x86/apicreg.h> 76#include <machine/clock.h> 77#include <machine/cputypes.h> 78#include <x86/mca.h> 79#include <machine/md_var.h> 80#include <machine/pcb.h> 81#include <machine/psl.h> 82#include <machine/smp.h> 83#include <machine/specialreg.h> 84 85#define WARMBOOT_TARGET 0 86#define WARMBOOT_OFF (KERNBASE + 0x0467) 87#define WARMBOOT_SEG (KERNBASE + 0x0469) 88 89#define CMOS_REG (0x70) 90#define CMOS_DATA (0x71) 91#define BIOS_RESET (0x0f) 92#define BIOS_WARM (0x0a) 93 94/* 95 * this code MUST be enabled here and in mpboot.s. 96 * it follows the very early stages of AP boot by placing values in CMOS ram. 97 * it NORMALLY will never be needed and thus the primitive method for enabling. 98 * 99#define CHECK_POINTS 100 */ 101 102#if defined(CHECK_POINTS) && !defined(PC98) 103#define CHECK_READ(A) (outb(CMOS_REG, (A)), inb(CMOS_DATA)) 104#define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D))) 105 106#define CHECK_INIT(D); \ 107 CHECK_WRITE(0x34, (D)); \ 108 CHECK_WRITE(0x35, (D)); \ 109 CHECK_WRITE(0x36, (D)); \ 110 CHECK_WRITE(0x37, (D)); \ 111 CHECK_WRITE(0x38, (D)); \ 112 CHECK_WRITE(0x39, (D)); 113 114#define CHECK_PRINT(S); \ 115 printf("%s: %d, %d, %d, %d, %d, %d\n", \ 116 (S), \ 117 CHECK_READ(0x34), \ 118 CHECK_READ(0x35), \ 119 CHECK_READ(0x36), \ 120 CHECK_READ(0x37), \ 121 CHECK_READ(0x38), \ 122 CHECK_READ(0x39)); 123 124#else /* CHECK_POINTS */ 125 126#define CHECK_INIT(D) 127#define CHECK_PRINT(S) 128#define CHECK_WRITE(A, D) 129 130#endif /* CHECK_POINTS */ 131 132/* lock region used by kernel profiling */ 133int mcount_lock; 134 135int mp_naps; /* # of Applications processors */ 136int boot_cpu_id = -1; /* designated BSP */ 137 138extern struct pcpu __pcpu[]; 139 140/* AP uses this during bootstrap. Do not staticize. */ 141char *bootSTK; 142static int bootAP; 143 144/* Free these after use */ 145void *bootstacks[MAXCPU]; 146static void *dpcpu; 147 148struct pcb stoppcbs[MAXCPU]; 149 150/* Variables needed for SMP tlb shootdown. */ 151vm_offset_t smp_tlb_addr1; 152vm_offset_t smp_tlb_addr2; 153volatile int smp_tlb_wait; 154 155#ifdef COUNT_IPIS 156/* Interrupt counts. */ 157static u_long *ipi_preempt_counts[MAXCPU]; 158static u_long *ipi_ast_counts[MAXCPU]; 159u_long *ipi_invltlb_counts[MAXCPU]; 160u_long *ipi_invlrng_counts[MAXCPU]; 161u_long *ipi_invlpg_counts[MAXCPU]; 162u_long *ipi_invlcache_counts[MAXCPU]; 163u_long *ipi_rendezvous_counts[MAXCPU]; 164u_long *ipi_lazypmap_counts[MAXCPU]; 165static u_long *ipi_hardclock_counts[MAXCPU]; 166#endif 167 168/* 169 * Local data and functions. 170 */ 171 172static volatile cpuset_t ipi_nmi_pending; 173 174/* used to hold the AP's until we are ready to release them */ 175static struct mtx ap_boot_mtx; 176 177/* Set to 1 once we're ready to let the APs out of the pen. */ 178static volatile int aps_ready = 0; 179 180/* 181 * Store data from cpu_add() until later in the boot when we actually setup 182 * the APs. 183 */ 184struct cpu_info { 185 int cpu_present:1; 186 int cpu_bsp:1; 187 int cpu_disabled:1; 188 int cpu_hyperthread:1; 189} static cpu_info[MAX_APIC_ID + 1]; 190int cpu_apic_ids[MAXCPU]; 191int apic_cpuids[MAX_APIC_ID + 1]; 192 193/* Holds pending bitmap based IPIs per CPU */ 194static volatile u_int cpu_ipi_pending[MAXCPU]; 195 196static u_int boot_address; 197static int cpu_logical; /* logical cpus per core */ 198static int cpu_cores; /* cores per package */ 199 200static void assign_cpu_ids(void); 201static void install_ap_tramp(void); 202static void set_interrupt_apic_ids(void); 203static int start_all_aps(void); 204static int start_ap(int apic_id); 205static void release_aps(void *dummy); 206 207static u_int hyperthreading_cpus; /* logical cpus sharing L1 cache */ 208static int hyperthreading_allowed = 1; 209 210static void 211mem_range_AP_init(void) 212{ 213 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP) 214 mem_range_softc.mr_op->initAP(&mem_range_softc); 215} 216 217static void 218topo_probe_amd(void) 219{ 220 int core_id_bits; 221 int id; 222 223 /* AMD processors do not support HTT. */ 224 cpu_logical = 1; 225 226 if ((amd_feature2 & AMDID2_CMP) == 0) { 227 cpu_cores = 1; 228 return; 229 } 230 231 core_id_bits = (cpu_procinfo2 & AMDID_COREID_SIZE) >> 232 AMDID_COREID_SIZE_SHIFT; 233 if (core_id_bits == 0) { 234 cpu_cores = (cpu_procinfo2 & AMDID_CMP_CORES) + 1; 235 return; 236 } 237 238 /* Fam 10h and newer should get here. */ 239 for (id = 0; id <= MAX_APIC_ID; id++) { 240 /* Check logical CPU availability. */ 241 if (!cpu_info[id].cpu_present || cpu_info[id].cpu_disabled) 242 continue; 243 /* Check if logical CPU has the same package ID. */ 244 if ((id >> core_id_bits) != (boot_cpu_id >> core_id_bits)) 245 continue; 246 cpu_cores++; 247 } 248} 249 250/* 251 * Round up to the next power of two, if necessary, and then 252 * take log2. 253 * Returns -1 if argument is zero. 254 */ 255static __inline int 256mask_width(u_int x) 257{ 258 259 return (fls(x << (1 - powerof2(x))) - 1); 260} 261 262static void 263topo_probe_0x4(void) 264{ 265 u_int p[4]; 266 int pkg_id_bits; 267 int core_id_bits; 268 int max_cores; 269 int max_logical; 270 int id; 271 272 /* Both zero and one here mean one logical processor per package. */ 273 max_logical = (cpu_feature & CPUID_HTT) != 0 ? 274 (cpu_procinfo & CPUID_HTT_CORES) >> 16 : 1; 275 if (max_logical <= 1) 276 return; 277 278 /* 279 * Because of uniformity assumption we examine only 280 * those logical processors that belong to the same 281 * package as BSP. Further, we count number of 282 * logical processors that belong to the same core 283 * as BSP thus deducing number of threads per core. 284 */ 285 if (cpu_high >= 0x4) { 286 cpuid_count(0x04, 0, p); 287 max_cores = ((p[0] >> 26) & 0x3f) + 1; 288 } else 289 max_cores = 1; 290 core_id_bits = mask_width(max_logical/max_cores); 291 if (core_id_bits < 0) 292 return; 293 pkg_id_bits = core_id_bits + mask_width(max_cores); 294 295 for (id = 0; id <= MAX_APIC_ID; id++) { 296 /* Check logical CPU availability. */ 297 if (!cpu_info[id].cpu_present || cpu_info[id].cpu_disabled) 298 continue; 299 /* Check if logical CPU has the same package ID. */ 300 if ((id >> pkg_id_bits) != (boot_cpu_id >> pkg_id_bits)) 301 continue; 302 cpu_cores++; 303 /* Check if logical CPU has the same package and core IDs. */ 304 if ((id >> core_id_bits) == (boot_cpu_id >> core_id_bits)) 305 cpu_logical++; 306 } 307 308 KASSERT(cpu_cores >= 1 && cpu_logical >= 1, 309 ("topo_probe_0x4 couldn't find BSP")); 310 311 cpu_cores /= cpu_logical; 312 hyperthreading_cpus = cpu_logical; 313} 314 315static void 316topo_probe_0xb(void) 317{ 318 u_int p[4]; 319 int bits; 320 int cnt; 321 int i; 322 int logical; 323 int type; 324 int x; 325 326 /* We only support three levels for now. */ 327 for (i = 0; i < 3; i++) { 328 cpuid_count(0x0b, i, p); 329 330 /* Fall back if CPU leaf 11 doesn't really exist. */ 331 if (i == 0 && p[1] == 0) { 332 topo_probe_0x4(); 333 return; 334 } 335 336 bits = p[0] & 0x1f; 337 logical = p[1] &= 0xffff; 338 type = (p[2] >> 8) & 0xff; 339 if (type == 0 || logical == 0) 340 break; 341 /* 342 * Because of uniformity assumption we examine only 343 * those logical processors that belong to the same 344 * package as BSP. 345 */ 346 for (cnt = 0, x = 0; x <= MAX_APIC_ID; x++) { 347 if (!cpu_info[x].cpu_present || 348 cpu_info[x].cpu_disabled) 349 continue; 350 if (x >> bits == boot_cpu_id >> bits) 351 cnt++; 352 } 353 if (type == CPUID_TYPE_SMT) 354 cpu_logical = cnt; 355 else if (type == CPUID_TYPE_CORE) 356 cpu_cores = cnt; 357 } 358 if (cpu_logical == 0) 359 cpu_logical = 1; 360 cpu_cores /= cpu_logical; 361} 362 363/* 364 * Both topology discovery code and code that consumes topology 365 * information assume top-down uniformity of the topology. 366 * That is, all physical packages must be identical and each 367 * core in a package must have the same number of threads. 368 * Topology information is queried only on BSP, on which this 369 * code runs and for which it can query CPUID information. 370 * Then topology is extrapolated on all packages using the 371 * uniformity assumption. 372 */ 373static void 374topo_probe(void) 375{ 376 static int cpu_topo_probed = 0; 377 378 if (cpu_topo_probed) 379 return; 380 381 CPU_ZERO(&logical_cpus_mask); 382 if (mp_ncpus <= 1) 383 cpu_cores = cpu_logical = 1; 384 else if (cpu_vendor_id == CPU_VENDOR_AMD) 385 topo_probe_amd(); 386 else if (cpu_vendor_id == CPU_VENDOR_INTEL) { 387 /* 388 * See Intel(R) 64 Architecture Processor 389 * Topology Enumeration article for details. 390 * 391 * Note that 0x1 <= cpu_high < 4 case should be 392 * compatible with topo_probe_0x4() logic when 393 * CPUID.1:EBX[23:16] > 0 (cpu_cores will be 1) 394 * or it should trigger the fallback otherwise. 395 */ 396 if (cpu_high >= 0xb) 397 topo_probe_0xb(); 398 else if (cpu_high >= 0x1) 399 topo_probe_0x4(); 400 } 401 402 /* 403 * Fallback: assume each logical CPU is in separate 404 * physical package. That is, no multi-core, no SMT. 405 */ 406 if (cpu_cores == 0 || cpu_logical == 0) 407 cpu_cores = cpu_logical = 1; 408 cpu_topo_probed = 1; 409} 410 411struct cpu_group * 412cpu_topo(void) 413{ 414 int cg_flags; 415 416 /* 417 * Determine whether any threading flags are 418 * necessry. 419 */ 420 topo_probe(); 421 if (cpu_logical > 1 && hyperthreading_cpus) 422 cg_flags = CG_FLAG_HTT; 423 else if (cpu_logical > 1) 424 cg_flags = CG_FLAG_SMT; 425 else 426 cg_flags = 0; 427 if (mp_ncpus % (cpu_cores * cpu_logical) != 0) { 428 printf("WARNING: Non-uniform processors.\n"); 429 printf("WARNING: Using suboptimal topology.\n"); 430 return (smp_topo_none()); 431 } 432 /* 433 * No multi-core or hyper-threaded. 434 */ 435 if (cpu_logical * cpu_cores == 1) 436 return (smp_topo_none()); 437 /* 438 * Only HTT no multi-core. 439 */ 440 if (cpu_logical > 1 && cpu_cores == 1) 441 return (smp_topo_1level(CG_SHARE_L1, cpu_logical, cg_flags)); 442 /* 443 * Only multi-core no HTT. 444 */ 445 if (cpu_cores > 1 && cpu_logical == 1) 446 return (smp_topo_1level(CG_SHARE_L2, cpu_cores, cg_flags)); 447 /* 448 * Both HTT and multi-core. 449 */ 450 return (smp_topo_2level(CG_SHARE_L2, cpu_cores, 451 CG_SHARE_L1, cpu_logical, cg_flags)); 452} 453 454 455/* 456 * Calculate usable address in base memory for AP trampoline code. 457 */ 458u_int 459mp_bootaddress(u_int basemem) 460{ 461 462 boot_address = trunc_page(basemem); /* round down to 4k boundary */ 463 if ((basemem - boot_address) < bootMP_size) 464 boot_address -= PAGE_SIZE; /* not enough, lower by 4k */ 465 466 return boot_address; 467} 468 469void 470cpu_add(u_int apic_id, char boot_cpu) 471{ 472 473 if (apic_id > MAX_APIC_ID) { 474 panic("SMP: APIC ID %d too high", apic_id); 475 return; 476 } 477 KASSERT(cpu_info[apic_id].cpu_present == 0, ("CPU %d added twice", 478 apic_id)); 479 cpu_info[apic_id].cpu_present = 1; 480 if (boot_cpu) { 481 KASSERT(boot_cpu_id == -1, 482 ("CPU %d claims to be BSP, but CPU %d already is", apic_id, 483 boot_cpu_id)); 484 boot_cpu_id = apic_id; 485 cpu_info[apic_id].cpu_bsp = 1; 486 } 487 if (mp_ncpus < MAXCPU) { 488 mp_ncpus++; 489 mp_maxid = mp_ncpus - 1; 490 } 491 if (bootverbose) 492 printf("SMP: Added CPU %d (%s)\n", apic_id, boot_cpu ? "BSP" : 493 "AP"); 494} 495 496void 497cpu_mp_setmaxid(void) 498{ 499 500 /* 501 * mp_maxid should be already set by calls to cpu_add(). 502 * Just sanity check its value here. 503 */ 504 if (mp_ncpus == 0) 505 KASSERT(mp_maxid == 0, 506 ("%s: mp_ncpus is zero, but mp_maxid is not", __func__)); 507 else if (mp_ncpus == 1) 508 mp_maxid = 0; 509 else 510 KASSERT(mp_maxid >= mp_ncpus - 1, 511 ("%s: counters out of sync: max %d, count %d", __func__, 512 mp_maxid, mp_ncpus)); 513} 514 515int 516cpu_mp_probe(void) 517{ 518 519 /* 520 * Always record BSP in CPU map so that the mbuf init code works 521 * correctly. 522 */ 523 CPU_SETOF(0, &all_cpus); 524 if (mp_ncpus == 0) { 525 /* 526 * No CPUs were found, so this must be a UP system. Setup 527 * the variables to represent a system with a single CPU 528 * with an id of 0. 529 */ 530 mp_ncpus = 1; 531 return (0); 532 } 533 534 /* At least one CPU was found. */ 535 if (mp_ncpus == 1) { 536 /* 537 * One CPU was found, so this must be a UP system with 538 * an I/O APIC. 539 */ 540 mp_maxid = 0; 541 return (0); 542 } 543 544 /* At least two CPUs were found. */ 545 return (1); 546} 547 548/* 549 * Initialize the IPI handlers and start up the AP's. 550 */ 551void 552cpu_mp_start(void) 553{ 554 int i; 555 556 /* Initialize the logical ID to APIC ID table. */ 557 for (i = 0; i < MAXCPU; i++) { 558 cpu_apic_ids[i] = -1; 559 cpu_ipi_pending[i] = 0; 560 } 561 562 /* Install an inter-CPU IPI for TLB invalidation */ 563 setidt(IPI_INVLTLB, IDTVEC(invltlb), 564 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 565 setidt(IPI_INVLPG, IDTVEC(invlpg), 566 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 567 setidt(IPI_INVLRNG, IDTVEC(invlrng), 568 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 569 570 /* Install an inter-CPU IPI for cache invalidation. */ 571 setidt(IPI_INVLCACHE, IDTVEC(invlcache), 572 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 573 574 /* Install an inter-CPU IPI for lazy pmap release */ 575 setidt(IPI_LAZYPMAP, IDTVEC(lazypmap), 576 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 577 578 /* Install an inter-CPU IPI for all-CPU rendezvous */ 579 setidt(IPI_RENDEZVOUS, IDTVEC(rendezvous), 580 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 581 582 /* Install generic inter-CPU IPI handler */ 583 setidt(IPI_BITMAP_VECTOR, IDTVEC(ipi_intr_bitmap_handler), 584 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 585 586 /* Install an inter-CPU IPI for CPU stop/restart */ 587 setidt(IPI_STOP, IDTVEC(cpustop), 588 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 589 590 591 /* Set boot_cpu_id if needed. */ 592 if (boot_cpu_id == -1) { 593 boot_cpu_id = PCPU_GET(apic_id); 594 cpu_info[boot_cpu_id].cpu_bsp = 1; 595 } else 596 KASSERT(boot_cpu_id == PCPU_GET(apic_id), 597 ("BSP's APIC ID doesn't match boot_cpu_id")); 598 599 /* Probe logical/physical core configuration. */ 600 topo_probe(); 601 602 assign_cpu_ids(); 603 604 /* Start each Application Processor */ 605 start_all_aps(); 606 607 set_interrupt_apic_ids(); 608} 609 610 611/* 612 * Print various information about the SMP system hardware and setup. 613 */ 614void 615cpu_mp_announce(void) 616{ 617 const char *hyperthread; 618 int i; 619 620 printf("FreeBSD/SMP: %d package(s) x %d core(s)", 621 mp_ncpus / (cpu_cores * cpu_logical), cpu_cores); 622 if (hyperthreading_cpus > 1) 623 printf(" x %d HTT threads", cpu_logical); 624 else if (cpu_logical > 1) 625 printf(" x %d SMT threads", cpu_logical); 626 printf("\n"); 627 628 /* List active CPUs first. */ 629 printf(" cpu0 (BSP): APIC ID: %2d\n", boot_cpu_id); 630 for (i = 1; i < mp_ncpus; i++) { 631 if (cpu_info[cpu_apic_ids[i]].cpu_hyperthread) 632 hyperthread = "/HT"; 633 else 634 hyperthread = ""; 635 printf(" cpu%d (AP%s): APIC ID: %2d\n", i, hyperthread, 636 cpu_apic_ids[i]); 637 } 638 639 /* List disabled CPUs last. */ 640 for (i = 0; i <= MAX_APIC_ID; i++) { 641 if (!cpu_info[i].cpu_present || !cpu_info[i].cpu_disabled) 642 continue; 643 if (cpu_info[i].cpu_hyperthread) 644 hyperthread = "/HT"; 645 else 646 hyperthread = ""; 647 printf(" cpu (AP%s): APIC ID: %2d (disabled)\n", hyperthread, 648 i); 649 } 650} 651 652/* 653 * AP CPU's call this to initialize themselves. 654 */ 655void 656init_secondary(void) 657{ 658 struct pcpu *pc; 659 vm_offset_t addr; 660 int gsel_tss; 661 int x, myid; 662 u_int cpuid, cr0; 663 664 /* bootAP is set in start_ap() to our ID. */ 665 myid = bootAP; 666 667 /* Get per-cpu data */ 668 pc = &__pcpu[myid]; 669 670 /* prime data page for it to use */ 671 pcpu_init(pc, myid, sizeof(struct pcpu)); 672 dpcpu_init(dpcpu, myid); 673 pc->pc_apic_id = cpu_apic_ids[myid]; 674 pc->pc_prvspace = pc; 675 pc->pc_curthread = 0; 676 677 gdt_segs[GPRIV_SEL].ssd_base = (int) pc; 678 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss; 679 680 for (x = 0; x < NGDT; x++) { 681 ssdtosd(&gdt_segs[x], &gdt[myid * NGDT + x].sd); 682 } 683 684 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1; 685 r_gdt.rd_base = (int) &gdt[myid * NGDT]; 686 lgdt(&r_gdt); /* does magic intra-segment return */ 687 688 lidt(&r_idt); 689 690 lldt(_default_ldt); 691 PCPU_SET(currentldt, _default_ldt); 692 693 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL); 694 gdt[myid * NGDT + GPROC0_SEL].sd.sd_type = SDT_SYS386TSS; 695 PCPU_SET(common_tss.tss_esp0, 0); /* not used until after switch */ 696 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL)); 697 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16); 698 PCPU_SET(tss_gdt, &gdt[myid * NGDT + GPROC0_SEL].sd); 699 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt)); 700 ltr(gsel_tss); 701 702 PCPU_SET(fsgs_gdt, &gdt[myid * NGDT + GUFS_SEL].sd); 703 704 /* 705 * Set to a known state: 706 * Set by mpboot.s: CR0_PG, CR0_PE 707 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM 708 */ 709 cr0 = rcr0(); 710 cr0 &= ~(CR0_CD | CR0_NW | CR0_EM); 711 load_cr0(cr0); 712 CHECK_WRITE(0x38, 5); 713 714 /* Disable local APIC just to be sure. */ 715 lapic_disable(); 716 717 /* signal our startup to the BSP. */ 718 mp_naps++; 719 CHECK_WRITE(0x39, 6); 720 721 /* Spin until the BSP releases the AP's. */ 722 while (!aps_ready) 723 ia32_pause(); 724 725 /* BSP may have changed PTD while we were waiting */ 726 invltlb(); 727 for (addr = 0; addr < NKPT * NBPDR - 1; addr += PAGE_SIZE) 728 invlpg(addr); 729 730#if defined(I586_CPU) && !defined(NO_F00F_HACK) 731 lidt(&r_idt); 732#endif 733 734 /* Initialize the PAT MSR if present. */ 735 pmap_init_pat(); 736 737 /* set up CPU registers and state */ 738 cpu_setregs(); 739 740 /* set up FPU state on the AP */ 741 npxinit(); 742 743 /* set up SSE registers */ 744 enable_sse(); 745 746#ifdef PAE 747 /* Enable the PTE no-execute bit. */ 748 if ((amd_feature & AMDID_NX) != 0) { 749 uint64_t msr; 750 751 msr = rdmsr(MSR_EFER) | EFER_NXE; 752 wrmsr(MSR_EFER, msr); 753 } 754#endif 755 756 /* A quick check from sanity claus */ 757 cpuid = PCPU_GET(cpuid); 758 if (PCPU_GET(apic_id) != lapic_id()) { 759 printf("SMP: cpuid = %d\n", cpuid); 760 printf("SMP: actual apic_id = %d\n", lapic_id()); 761 printf("SMP: correct apic_id = %d\n", PCPU_GET(apic_id)); 762 panic("cpuid mismatch! boom!!"); 763 } 764 765 /* Initialize curthread. */ 766 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread")); 767 PCPU_SET(curthread, PCPU_GET(idlethread)); 768 769 mca_init(); 770 771 mtx_lock_spin(&ap_boot_mtx); 772 773 /* Init local apic for irq's */ 774 lapic_setup(1); 775 776 /* Set memory range attributes for this CPU to match the BSP */ 777 mem_range_AP_init(); 778 779 smp_cpus++; 780 781 CTR1(KTR_SMP, "SMP: AP CPU #%d Launched", cpuid); 782 printf("SMP: AP CPU #%d Launched!\n", cpuid); 783 784 /* Determine if we are a logical CPU. */ 785 /* XXX Calculation depends on cpu_logical being a power of 2, e.g. 2 */ 786 if (cpu_logical > 1 && PCPU_GET(apic_id) % cpu_logical != 0) 787 CPU_SET(cpuid, &logical_cpus_mask); 788 789 if (bootverbose) 790 lapic_dump("AP"); 791 792 if (smp_cpus == mp_ncpus) { 793 /* enable IPI's, tlb shootdown, freezes etc */ 794 atomic_store_rel_int(&smp_started, 1); 795 smp_active = 1; /* historic */ 796 } 797 798 mtx_unlock_spin(&ap_boot_mtx); 799 800 /* Wait until all the AP's are up. */ 801 while (smp_started == 0) 802 ia32_pause(); 803 804 /* Start per-CPU event timers. */ 805 cpu_initclocks_ap(); 806 807 /* Enter the scheduler. */ 808 sched_throw(NULL); 809 810 panic("scheduler returned us to %s", __func__); 811 /* NOTREACHED */ 812} 813 814/******************************************************************* 815 * local functions and data 816 */ 817 818/* 819 * We tell the I/O APIC code about all the CPUs we want to receive 820 * interrupts. If we don't want certain CPUs to receive IRQs we 821 * can simply not tell the I/O APIC code about them in this function. 822 */ 823static void 824set_interrupt_apic_ids(void) 825{ 826 u_int i, apic_id; 827 828 for (i = 0; i < MAXCPU; i++) { 829 apic_id = cpu_apic_ids[i]; 830 if (apic_id == -1) 831 continue; 832 if (cpu_info[apic_id].cpu_disabled) 833 continue; 834 835 /* Don't let hyperthreads service interrupts. */ 836 if (hyperthreading_cpus > 1 && 837 apic_id % hyperthreading_cpus != 0) 838 continue; 839 840 intr_add_cpu(i); 841 } 842} 843 844/* 845 * Assign logical CPU IDs to local APICs. 846 */ 847static void 848assign_cpu_ids(void) 849{ 850 u_int i; 851 852 TUNABLE_INT_FETCH("machdep.hyperthreading_allowed", 853 &hyperthreading_allowed); 854 855 /* Check for explicitly disabled CPUs. */ 856 for (i = 0; i <= MAX_APIC_ID; i++) { 857 if (!cpu_info[i].cpu_present || cpu_info[i].cpu_bsp) 858 continue; 859 860 if (hyperthreading_cpus > 1 && i % hyperthreading_cpus != 0) { 861 cpu_info[i].cpu_hyperthread = 1; 862 863 /* 864 * Don't use HT CPU if it has been disabled by a 865 * tunable. 866 */ 867 if (hyperthreading_allowed == 0) { 868 cpu_info[i].cpu_disabled = 1; 869 continue; 870 } 871 } 872 873 /* Don't use this CPU if it has been disabled by a tunable. */ 874 if (resource_disabled("lapic", i)) { 875 cpu_info[i].cpu_disabled = 1; 876 continue; 877 } 878 } 879 880 if (hyperthreading_allowed == 0 && hyperthreading_cpus > 1) { 881 hyperthreading_cpus = 0; 882 cpu_logical = 1; 883 } 884 885 /* 886 * Assign CPU IDs to local APIC IDs and disable any CPUs 887 * beyond MAXCPU. CPU 0 is always assigned to the BSP. 888 * 889 * To minimize confusion for userland, we attempt to number 890 * CPUs such that all threads and cores in a package are 891 * grouped together. For now we assume that the BSP is always 892 * the first thread in a package and just start adding APs 893 * starting with the BSP's APIC ID. 894 */ 895 mp_ncpus = 1; 896 cpu_apic_ids[0] = boot_cpu_id; 897 apic_cpuids[boot_cpu_id] = 0; 898 for (i = boot_cpu_id + 1; i != boot_cpu_id; 899 i == MAX_APIC_ID ? i = 0 : i++) { 900 if (!cpu_info[i].cpu_present || cpu_info[i].cpu_bsp || 901 cpu_info[i].cpu_disabled) 902 continue; 903 904 if (mp_ncpus < MAXCPU) { 905 cpu_apic_ids[mp_ncpus] = i; 906 apic_cpuids[i] = mp_ncpus; 907 mp_ncpus++; 908 } else 909 cpu_info[i].cpu_disabled = 1; 910 } 911 KASSERT(mp_maxid >= mp_ncpus - 1, 912 ("%s: counters out of sync: max %d, count %d", __func__, mp_maxid, 913 mp_ncpus)); 914} 915 916/* 917 * start each AP in our list 918 */ 919/* Lowest 1MB is already mapped: don't touch*/ 920#define TMPMAP_START 1 921static int 922start_all_aps(void) 923{ 924#ifndef PC98 925 u_char mpbiosreason; 926#endif 927 u_int32_t mpbioswarmvec; 928 int apic_id, cpu, i; 929 930 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN); 931 932 /* install the AP 1st level boot code */ 933 install_ap_tramp(); 934 935 /* save the current value of the warm-start vector */ 936 mpbioswarmvec = *((u_int32_t *) WARMBOOT_OFF); 937#ifndef PC98 938 outb(CMOS_REG, BIOS_RESET); 939 mpbiosreason = inb(CMOS_DATA); 940#endif 941 942 /* set up temporary P==V mapping for AP boot */ 943 /* XXX this is a hack, we should boot the AP on its own stack/PTD */ 944 for (i = TMPMAP_START; i < NKPT; i++) 945 PTD[i] = PTD[KPTDI + i]; 946 invltlb(); 947 948 /* start each AP */ 949 for (cpu = 1; cpu < mp_ncpus; cpu++) { 950 apic_id = cpu_apic_ids[cpu]; 951 952 /* allocate and set up a boot stack data page */ 953 bootstacks[cpu] = 954 (char *)kmem_alloc(kernel_map, KSTACK_PAGES * PAGE_SIZE); 955 dpcpu = (void *)kmem_alloc(kernel_map, DPCPU_SIZE); 956 /* setup a vector to our boot code */ 957 *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET; 958 *((volatile u_short *) WARMBOOT_SEG) = (boot_address >> 4); 959#ifndef PC98 960 outb(CMOS_REG, BIOS_RESET); 961 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */ 962#endif 963 964 bootSTK = (char *)bootstacks[cpu] + KSTACK_PAGES * PAGE_SIZE - 4; 965 bootAP = cpu; 966 967 /* attempt to start the Application Processor */ 968 CHECK_INIT(99); /* setup checkpoints */ 969 if (!start_ap(apic_id)) { 970 printf("AP #%d (PHY# %d) failed!\n", cpu, apic_id); 971 CHECK_PRINT("trace"); /* show checkpoints */ 972 /* better panic as the AP may be running loose */ 973 printf("panic y/n? [y] "); 974 if (cngetc() != 'n') 975 panic("bye-bye"); 976 } 977 CHECK_PRINT("trace"); /* show checkpoints */ 978 979 CPU_SET(cpu, &all_cpus); /* record AP in CPU map */ 980 } 981 982 /* restore the warmstart vector */ 983 *(u_int32_t *) WARMBOOT_OFF = mpbioswarmvec; 984 985#ifndef PC98 986 outb(CMOS_REG, BIOS_RESET); 987 outb(CMOS_DATA, mpbiosreason); 988#endif 989 990 /* Undo V==P hack from above */ 991 for (i = TMPMAP_START; i < NKPT; i++) 992 PTD[i] = 0; 993 pmap_invalidate_range(kernel_pmap, 0, NKPT * NBPDR - 1); 994 995 /* number of APs actually started */ 996 return mp_naps; 997} 998 999/* 1000 * load the 1st level AP boot code into base memory. 1001 */ 1002 1003/* targets for relocation */ 1004extern void bigJump(void); 1005extern void bootCodeSeg(void); 1006extern void bootDataSeg(void); 1007extern void MPentry(void); 1008extern u_int MP_GDT; 1009extern u_int mp_gdtbase; 1010 1011static void 1012install_ap_tramp(void) 1013{ 1014 int x; 1015 int size = *(int *) ((u_long) & bootMP_size); 1016 vm_offset_t va = boot_address + KERNBASE; 1017 u_char *src = (u_char *) ((u_long) bootMP); 1018 u_char *dst = (u_char *) va; 1019 u_int boot_base = (u_int) bootMP; 1020 u_int8_t *dst8; 1021 u_int16_t *dst16; 1022 u_int32_t *dst32; 1023 1024 KASSERT (size <= PAGE_SIZE, 1025 ("'size' do not fit into PAGE_SIZE, as expected.")); 1026 pmap_kenter(va, boot_address); 1027 pmap_invalidate_page (kernel_pmap, va); 1028 for (x = 0; x < size; ++x) 1029 *dst++ = *src++; 1030 1031 /* 1032 * modify addresses in code we just moved to basemem. unfortunately we 1033 * need fairly detailed info about mpboot.s for this to work. changes 1034 * to mpboot.s might require changes here. 1035 */ 1036 1037 /* boot code is located in KERNEL space */ 1038 dst = (u_char *) va; 1039 1040 /* modify the lgdt arg */ 1041 dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base)); 1042 *dst32 = boot_address + ((u_int) & MP_GDT - boot_base); 1043 1044 /* modify the ljmp target for MPentry() */ 1045 dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1); 1046 *dst32 = ((u_int) MPentry - KERNBASE); 1047 1048 /* modify the target for boot code segment */ 1049 dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base)); 1050 dst8 = (u_int8_t *) (dst16 + 1); 1051 *dst16 = (u_int) boot_address & 0xffff; 1052 *dst8 = ((u_int) boot_address >> 16) & 0xff; 1053 1054 /* modify the target for boot data segment */ 1055 dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base)); 1056 dst8 = (u_int8_t *) (dst16 + 1); 1057 *dst16 = (u_int) boot_address & 0xffff; 1058 *dst8 = ((u_int) boot_address >> 16) & 0xff; 1059} 1060 1061/* 1062 * This function starts the AP (application processor) identified 1063 * by the APIC ID 'physicalCpu'. It does quite a "song and dance" 1064 * to accomplish this. This is necessary because of the nuances 1065 * of the different hardware we might encounter. It isn't pretty, 1066 * but it seems to work. 1067 */ 1068static int 1069start_ap(int apic_id) 1070{ 1071 int vector, ms; 1072 int cpus; 1073 1074 /* calculate the vector */ 1075 vector = (boot_address >> 12) & 0xff; 1076 1077 /* used as a watchpoint to signal AP startup */ 1078 cpus = mp_naps; 1079 1080 /* 1081 * first we do an INIT/RESET IPI this INIT IPI might be run, reseting 1082 * and running the target CPU. OR this INIT IPI might be latched (P5 1083 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be 1084 * ignored. 1085 */ 1086 1087 /* do an INIT IPI: assert RESET */ 1088 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE | 1089 APIC_LEVEL_ASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, apic_id); 1090 1091 /* wait for pending status end */ 1092 lapic_ipi_wait(-1); 1093 1094 /* do an INIT IPI: deassert RESET */ 1095 lapic_ipi_raw(APIC_DEST_ALLESELF | APIC_TRIGMOD_LEVEL | 1096 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, 0); 1097 1098 /* wait for pending status end */ 1099 DELAY(10000); /* wait ~10mS */ 1100 lapic_ipi_wait(-1); 1101 1102 /* 1103 * next we do a STARTUP IPI: the previous INIT IPI might still be 1104 * latched, (P5 bug) this 1st STARTUP would then terminate 1105 * immediately, and the previously started INIT IPI would continue. OR 1106 * the previous INIT IPI has already run. and this STARTUP IPI will 1107 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI 1108 * will run. 1109 */ 1110 1111 /* do a STARTUP IPI */ 1112 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE | 1113 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP | 1114 vector, apic_id); 1115 lapic_ipi_wait(-1); 1116 DELAY(200); /* wait ~200uS */ 1117 1118 /* 1119 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF 1120 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR 1121 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is 1122 * recognized after hardware RESET or INIT IPI. 1123 */ 1124 1125 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE | 1126 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP | 1127 vector, apic_id); 1128 lapic_ipi_wait(-1); 1129 DELAY(200); /* wait ~200uS */ 1130 1131 /* Wait up to 5 seconds for it to start. */ 1132 for (ms = 0; ms < 5000; ms++) { 1133 if (mp_naps > cpus) 1134 return 1; /* return SUCCESS */ 1135 DELAY(1000); 1136 } 1137 return 0; /* return FAILURE */ 1138} 1139 1140#ifdef COUNT_XINVLTLB_HITS 1141u_int xhits_gbl[MAXCPU]; 1142u_int xhits_pg[MAXCPU]; 1143u_int xhits_rng[MAXCPU]; 1144static SYSCTL_NODE(_debug, OID_AUTO, xhits, CTLFLAG_RW, 0, ""); 1145SYSCTL_OPAQUE(_debug_xhits, OID_AUTO, global, CTLFLAG_RW, &xhits_gbl, 1146 sizeof(xhits_gbl), "IU", ""); 1147SYSCTL_OPAQUE(_debug_xhits, OID_AUTO, page, CTLFLAG_RW, &xhits_pg, 1148 sizeof(xhits_pg), "IU", ""); 1149SYSCTL_OPAQUE(_debug_xhits, OID_AUTO, range, CTLFLAG_RW, &xhits_rng, 1150 sizeof(xhits_rng), "IU", ""); 1151 1152u_int ipi_global; 1153u_int ipi_page; 1154u_int ipi_range; 1155u_int ipi_range_size; 1156SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_global, CTLFLAG_RW, &ipi_global, 0, ""); 1157SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_page, CTLFLAG_RW, &ipi_page, 0, ""); 1158SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_range, CTLFLAG_RW, &ipi_range, 0, ""); 1159SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_range_size, CTLFLAG_RW, &ipi_range_size, 1160 0, ""); 1161 1162u_int ipi_masked_global; 1163u_int ipi_masked_page; 1164u_int ipi_masked_range; 1165u_int ipi_masked_range_size; 1166SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_masked_global, CTLFLAG_RW, 1167 &ipi_masked_global, 0, ""); 1168SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_masked_page, CTLFLAG_RW, 1169 &ipi_masked_page, 0, ""); 1170SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_masked_range, CTLFLAG_RW, 1171 &ipi_masked_range, 0, ""); 1172SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_masked_range_size, CTLFLAG_RW, 1173 &ipi_masked_range_size, 0, ""); 1174#endif /* COUNT_XINVLTLB_HITS */ 1175 1176/* 1177 * Send an IPI to specified CPU handling the bitmap logic. 1178 */ 1179static void 1180ipi_send_cpu(int cpu, u_int ipi) 1181{ 1182 u_int bitmap, old_pending, new_pending; 1183 1184 KASSERT(cpu_apic_ids[cpu] != -1, ("IPI to non-existent CPU %d", cpu)); 1185 1186 if (IPI_IS_BITMAPED(ipi)) { 1187 bitmap = 1 << ipi; 1188 ipi = IPI_BITMAP_VECTOR; 1189 do { 1190 old_pending = cpu_ipi_pending[cpu]; 1191 new_pending = old_pending | bitmap; 1192 } while (!atomic_cmpset_int(&cpu_ipi_pending[cpu], 1193 old_pending, new_pending)); 1194 if (old_pending) 1195 return; 1196 } 1197 lapic_ipi_vectored(ipi, cpu_apic_ids[cpu]); 1198} 1199 1200/* 1201 * Flush the TLB on all other CPU's 1202 */ 1203static void 1204smp_tlb_shootdown(u_int vector, vm_offset_t addr1, vm_offset_t addr2) 1205{ 1206 u_int ncpu; 1207 1208 ncpu = mp_ncpus - 1; /* does not shootdown self */ 1209 if (ncpu < 1) 1210 return; /* no other cpus */ 1211 if (!(read_eflags() & PSL_I)) 1212 panic("%s: interrupts disabled", __func__); 1213 mtx_lock_spin(&smp_ipi_mtx); 1214 smp_tlb_addr1 = addr1; 1215 smp_tlb_addr2 = addr2; 1216 atomic_store_rel_int(&smp_tlb_wait, 0); 1217 ipi_all_but_self(vector); 1218 while (smp_tlb_wait < ncpu) 1219 ia32_pause(); 1220 mtx_unlock_spin(&smp_ipi_mtx); 1221} 1222 1223static void 1224smp_targeted_tlb_shootdown(cpuset_t mask, u_int vector, vm_offset_t addr1, vm_offset_t addr2) 1225{ 1226 int cpu, ncpu, othercpus; 1227 1228 othercpus = mp_ncpus - 1; 1229 if (CPU_ISFULLSET(&mask)) { 1230 if (othercpus < 1) 1231 return; 1232 } else { 1233 CPU_CLR(PCPU_GET(cpuid), &mask); 1234 if (CPU_EMPTY(&mask)) 1235 return; 1236 } 1237 if (!(read_eflags() & PSL_I)) 1238 panic("%s: interrupts disabled", __func__); 1239 mtx_lock_spin(&smp_ipi_mtx); 1240 smp_tlb_addr1 = addr1; 1241 smp_tlb_addr2 = addr2; 1242 atomic_store_rel_int(&smp_tlb_wait, 0); 1243 if (CPU_ISFULLSET(&mask)) { 1244 ncpu = othercpus; 1245 ipi_all_but_self(vector); 1246 } else { 1247 ncpu = 0; 1248 while ((cpu = cpusetobj_ffs(&mask)) != 0) { 1249 cpu--; 1250 CPU_CLR(cpu, &mask); 1251 CTR3(KTR_SMP, "%s: cpu: %d ipi: %x", __func__, cpu, 1252 vector); 1253 ipi_send_cpu(cpu, vector); 1254 ncpu++; 1255 } 1256 } 1257 while (smp_tlb_wait < ncpu) 1258 ia32_pause(); 1259 mtx_unlock_spin(&smp_ipi_mtx); 1260} 1261 1262void 1263smp_cache_flush(void) 1264{ 1265 1266 if (smp_started) 1267 smp_tlb_shootdown(IPI_INVLCACHE, 0, 0); 1268} 1269 1270void 1271smp_invltlb(void) 1272{ 1273 1274 if (smp_started) { 1275 smp_tlb_shootdown(IPI_INVLTLB, 0, 0); 1276#ifdef COUNT_XINVLTLB_HITS 1277 ipi_global++; 1278#endif 1279 } 1280} 1281 1282void 1283smp_invlpg(vm_offset_t addr) 1284{ 1285 1286 if (smp_started) { 1287 smp_tlb_shootdown(IPI_INVLPG, addr, 0); 1288#ifdef COUNT_XINVLTLB_HITS 1289 ipi_page++; 1290#endif 1291 } 1292} 1293 1294void 1295smp_invlpg_range(vm_offset_t addr1, vm_offset_t addr2) 1296{ 1297 1298 if (smp_started) { 1299 smp_tlb_shootdown(IPI_INVLRNG, addr1, addr2); 1300#ifdef COUNT_XINVLTLB_HITS 1301 ipi_range++; 1302 ipi_range_size += (addr2 - addr1) / PAGE_SIZE; 1303#endif 1304 } 1305} 1306 1307void 1308smp_masked_invltlb(cpuset_t mask) 1309{ 1310 1311 if (smp_started) { 1312 smp_targeted_tlb_shootdown(mask, IPI_INVLTLB, 0, 0); 1313#ifdef COUNT_XINVLTLB_HITS 1314 ipi_masked_global++; 1315#endif 1316 } 1317} 1318 1319void 1320smp_masked_invlpg(cpuset_t mask, vm_offset_t addr) 1321{ 1322 1323 if (smp_started) { 1324 smp_targeted_tlb_shootdown(mask, IPI_INVLPG, addr, 0); 1325#ifdef COUNT_XINVLTLB_HITS 1326 ipi_masked_page++; 1327#endif 1328 } 1329} 1330 1331void 1332smp_masked_invlpg_range(cpuset_t mask, vm_offset_t addr1, vm_offset_t addr2) 1333{ 1334 1335 if (smp_started) { 1336 smp_targeted_tlb_shootdown(mask, IPI_INVLRNG, addr1, addr2); 1337#ifdef COUNT_XINVLTLB_HITS 1338 ipi_masked_range++; 1339 ipi_masked_range_size += (addr2 - addr1) / PAGE_SIZE; 1340#endif 1341 } 1342} 1343 1344void 1345ipi_bitmap_handler(struct trapframe frame) 1346{ 1347 struct trapframe *oldframe; 1348 struct thread *td; 1349 int cpu = PCPU_GET(cpuid); 1350 u_int ipi_bitmap; 1351 1352 critical_enter(); 1353 td = curthread; 1354 td->td_intr_nesting_level++; 1355 oldframe = td->td_intr_frame; 1356 td->td_intr_frame = &frame; 1357 ipi_bitmap = atomic_readandclear_int(&cpu_ipi_pending[cpu]); 1358 if (ipi_bitmap & (1 << IPI_PREEMPT)) { 1359#ifdef COUNT_IPIS 1360 (*ipi_preempt_counts[cpu])++; 1361#endif 1362 sched_preempt(td); 1363 } 1364 if (ipi_bitmap & (1 << IPI_AST)) { 1365#ifdef COUNT_IPIS 1366 (*ipi_ast_counts[cpu])++; 1367#endif 1368 /* Nothing to do for AST */ 1369 } 1370 if (ipi_bitmap & (1 << IPI_HARDCLOCK)) { 1371#ifdef COUNT_IPIS 1372 (*ipi_hardclock_counts[cpu])++; 1373#endif 1374 hardclockintr(); 1375 } 1376 td->td_intr_frame = oldframe; 1377 td->td_intr_nesting_level--; 1378 critical_exit(); 1379} 1380 1381/* 1382 * send an IPI to a set of cpus. 1383 */ 1384void 1385ipi_selected(cpuset_t cpus, u_int ipi) 1386{ 1387 int cpu; 1388 1389 /* 1390 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit 1391 * of help in order to understand what is the source. 1392 * Set the mask of receiving CPUs for this purpose. 1393 */ 1394 if (ipi == IPI_STOP_HARD) 1395 CPU_OR_ATOMIC(&ipi_nmi_pending, &cpus); 1396 1397 while ((cpu = cpusetobj_ffs(&cpus)) != 0) { 1398 cpu--; 1399 CPU_CLR(cpu, &cpus); 1400 CTR3(KTR_SMP, "%s: cpu: %d ipi: %x", __func__, cpu, ipi); 1401 ipi_send_cpu(cpu, ipi); 1402 } 1403} 1404 1405/* 1406 * send an IPI to a specific CPU. 1407 */ 1408void 1409ipi_cpu(int cpu, u_int ipi) 1410{ 1411 1412 /* 1413 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit 1414 * of help in order to understand what is the source. 1415 * Set the mask of receiving CPUs for this purpose. 1416 */ 1417 if (ipi == IPI_STOP_HARD) 1418 CPU_SET_ATOMIC(cpu, &ipi_nmi_pending); 1419 1420 CTR3(KTR_SMP, "%s: cpu: %d ipi: %x", __func__, cpu, ipi); 1421 ipi_send_cpu(cpu, ipi); 1422} 1423 1424/* 1425 * send an IPI to all CPUs EXCEPT myself 1426 */ 1427void 1428ipi_all_but_self(u_int ipi) 1429{ 1430 cpuset_t other_cpus; 1431 1432 other_cpus = all_cpus; 1433 CPU_CLR(PCPU_GET(cpuid), &other_cpus); 1434 if (IPI_IS_BITMAPED(ipi)) { 1435 ipi_selected(other_cpus, ipi); 1436 return; 1437 } 1438 1439 /* 1440 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit 1441 * of help in order to understand what is the source. 1442 * Set the mask of receiving CPUs for this purpose. 1443 */ 1444 if (ipi == IPI_STOP_HARD) 1445 CPU_OR_ATOMIC(&ipi_nmi_pending, &other_cpus); 1446 1447 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi); 1448 lapic_ipi_vectored(ipi, APIC_IPI_DEST_OTHERS); 1449} 1450 1451int 1452ipi_nmi_handler() 1453{ 1454 u_int cpuid; 1455 1456 /* 1457 * As long as there is not a simple way to know about a NMI's 1458 * source, if the bitmask for the current CPU is present in 1459 * the global pending bitword an IPI_STOP_HARD has been issued 1460 * and should be handled. 1461 */ 1462 cpuid = PCPU_GET(cpuid); 1463 if (!CPU_ISSET(cpuid, &ipi_nmi_pending)) 1464 return (1); 1465 1466 CPU_CLR_ATOMIC(cpuid, &ipi_nmi_pending); 1467 cpustop_handler(); 1468 return (0); 1469} 1470 1471/* 1472 * Handle an IPI_STOP by saving our current context and spinning until we 1473 * are resumed. 1474 */ 1475void 1476cpustop_handler(void) 1477{ 1478 u_int cpu; 1479 1480 cpu = PCPU_GET(cpuid); 1481 1482 savectx(&stoppcbs[cpu]); 1483 1484 /* Indicate that we are stopped */ 1485 CPU_SET_ATOMIC(cpu, &stopped_cpus); 1486 1487 /* Wait for restart */ 1488 while (!CPU_ISSET(cpu, &started_cpus)) 1489 ia32_pause(); 1490 1491 CPU_CLR_ATOMIC(cpu, &started_cpus); 1492 CPU_CLR_ATOMIC(cpu, &stopped_cpus); 1493 1494 if (cpu == 0 && cpustop_restartfunc != NULL) { 1495 cpustop_restartfunc(); 1496 cpustop_restartfunc = NULL; 1497 } 1498} 1499 1500/* 1501 * This is called once the rest of the system is up and running and we're 1502 * ready to let the AP's out of the pen. 1503 */ 1504static void 1505release_aps(void *dummy __unused) 1506{ 1507 1508 if (mp_ncpus == 1) 1509 return; 1510 atomic_store_rel_int(&aps_ready, 1); 1511 while (smp_started == 0) 1512 ia32_pause(); 1513} 1514SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL); 1515 1516#ifdef COUNT_IPIS 1517/* 1518 * Setup interrupt counters for IPI handlers. 1519 */ 1520static void 1521mp_ipi_intrcnt(void *dummy) 1522{ 1523 char buf[64]; 1524 int i; 1525 1526 CPU_FOREACH(i) { 1527 snprintf(buf, sizeof(buf), "cpu%d:invltlb", i); 1528 intrcnt_add(buf, &ipi_invltlb_counts[i]); 1529 snprintf(buf, sizeof(buf), "cpu%d:invlrng", i); 1530 intrcnt_add(buf, &ipi_invlrng_counts[i]); 1531 snprintf(buf, sizeof(buf), "cpu%d:invlpg", i); 1532 intrcnt_add(buf, &ipi_invlpg_counts[i]); 1533 snprintf(buf, sizeof(buf), "cpu%d:invlcache", i); 1534 intrcnt_add(buf, &ipi_invlcache_counts[i]); 1535 snprintf(buf, sizeof(buf), "cpu%d:preempt", i); 1536 intrcnt_add(buf, &ipi_preempt_counts[i]); 1537 snprintf(buf, sizeof(buf), "cpu%d:ast", i); 1538 intrcnt_add(buf, &ipi_ast_counts[i]); 1539 snprintf(buf, sizeof(buf), "cpu%d:rendezvous", i); 1540 intrcnt_add(buf, &ipi_rendezvous_counts[i]); 1541 snprintf(buf, sizeof(buf), "cpu%d:lazypmap", i); 1542 intrcnt_add(buf, &ipi_lazypmap_counts[i]); 1543 snprintf(buf, sizeof(buf), "cpu%d:hardclock", i); 1544 intrcnt_add(buf, &ipi_hardclock_counts[i]); 1545 } 1546} 1547SYSINIT(mp_ipi_intrcnt, SI_SUB_INTR, SI_ORDER_MIDDLE, mp_ipi_intrcnt, NULL); 1548#endif 1549