sys/platform/pc64/x86_64/mp_machdep.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 | /* * Copyright (c) 1996, by Steve Passe * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. The name of the developer may NOT be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD: src/sys/i386/i386/mp_machdep.c,v 1.115.2.15 2003/03/14 21:22:35 jhb Exp $ */ #include "opt_cpu.h" #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/sysctl.h> #include <sys/malloc.h> #include <sys/memrange.h> #include <sys/cons.h> /* cngetc() */ #include <sys/machintr.h> #include <sys/cpu_topology.h> #include <sys/mplock2.h> #include <vm/vm.h> #include <vm/vm_param.h> #include <vm/pmap.h> #include <vm/vm_kern.h> #include <vm/vm_extern.h> #include <sys/lock.h> #include <vm/vm_map.h> #include <machine/smp.h> #include <machine_base/apic/apicreg.h> #include <machine/atomic.h> #include <machine/cpufunc.h> #include <machine/cputypes.h> #include <machine_base/apic/lapic.h> #include <machine_base/apic/ioapic.h> #include <machine_base/acpica/acpi_md_cpu.h> #include <machine/psl.h> #include <machine/segments.h> #include <machine/tss.h> #include <machine/specialreg.h> #include <machine/globaldata.h> #include <machine/pmap_inval.h> #include <machine/clock.h> #include <machine/md_var.h> /* setidt() */ #include <machine_base/icu/icu.h> /* IPIs */ #include <machine_base/icu/icu_var.h> #include <machine_base/apic/ioapic_abi.h> #include <machine/intr_machdep.h> /* IPIs */ #define WARMBOOT_TARGET 0 #define WARMBOOT_OFF (KERNBASE + 0x0467) #define WARMBOOT_SEG (KERNBASE + 0x0469) #define CMOS_REG (0x70) #define CMOS_DATA (0x71) #define BIOS_RESET (0x0f) #define BIOS_WARM (0x0a) #define INVLPG_TIMEOUT_DEFAULT 10 #define INVLPG_TIMEOUT_VM 60 /* * this code MUST be enabled here and in mpboot.s. * it follows the very early stages of AP boot by placing values in CMOS ram. * it NORMALLY will never be needed and thus the primitive method for enabling. * */ #if defined(CHECK_POINTS) #define CHECK_READ(A) (outb(CMOS_REG, (A)), inb(CMOS_DATA)) #define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D))) #define CHECK_INIT(D); \ CHECK_WRITE(0x34, (D)); \ CHECK_WRITE(0x35, (D)); \ CHECK_WRITE(0x36, (D)); \ CHECK_WRITE(0x37, (D)); \ CHECK_WRITE(0x38, (D)); \ CHECK_WRITE(0x39, (D)); #define CHECK_PRINT(S); \ kprintf("%s: %d, %d, %d, %d, %d, %d\n", \ (S), \ CHECK_READ(0x34), \ CHECK_READ(0x35), \ CHECK_READ(0x36), \ CHECK_READ(0x37), \ CHECK_READ(0x38), \ CHECK_READ(0x39)); #else /* CHECK_POINTS */ #define CHECK_INIT(D) #define CHECK_PRINT(S) #endif /* CHECK_POINTS */ /* * Values to send to the POST hardware. */ #define MP_BOOTADDRESS_POST 0x10 #define MP_PROBE_POST 0x11 #define MPTABLE_PASS1_POST 0x12 #define MP_START_POST 0x13 #define MP_ENABLE_POST 0x14 #define MPTABLE_PASS2_POST 0x15 #define START_ALL_APS_POST 0x16 #define INSTALL_AP_TRAMP_POST 0x17 #define START_AP_POST 0x18 #define MP_ANNOUNCE_POST 0x19 /** XXX FIXME: where does this really belong, isa.h/isa.c perhaps? */ int current_postcode; /** XXX FIXME: what system files declare these??? */ extern int naps; extern int _udatasel; int64_t tsc0_offset; extern int64_t tsc_offsets[]; /* AP uses this during bootstrap. Do not staticize. */ char *bootSTK; static int bootAP; struct pcb stoppcbs[MAXCPU]; extern inthand_t IDTVEC(fast_syscall), IDTVEC(fast_syscall32); /* * Local data and functions. */ static u_int boot_address; static int mp_finish; static int mp_finish_lapic; static int start_all_aps(u_int boot_addr); #if 0 static void install_ap_tramp(u_int boot_addr); #endif static int start_ap(struct mdglobaldata *gd, u_int boot_addr, int smibest); static int smitest(void); static void mp_bsp_simple_setup(void); /* which cpus have been started */ __read_mostly static cpumask_t smp_startup_mask = CPUMASK_INITIALIZER_ONLYONE; /* which cpus have lapic been inited */ __read_mostly static cpumask_t smp_lapic_mask = CPUMASK_INITIALIZER_ONLYONE; /* which cpus are ready for IPIs etc? */ __read_mostly cpumask_t smp_active_mask = CPUMASK_INITIALIZER_ONLYONE; __read_mostly cpumask_t smp_finalize_mask = CPUMASK_INITIALIZER_ONLYONE; SYSCTL_OPAQUE(_machdep, OID_AUTO, smp_active, CTLFLAG_RD, &smp_active_mask, sizeof(smp_active_mask), "LU", ""); static u_int bootMP_size; __read_mostly static u_int report_invlpg_src; SYSCTL_INT(_machdep, OID_AUTO, report_invlpg_src, CTLFLAG_RW, &report_invlpg_src, 0, ""); __read_mostly static u_int report_invltlb_src; SYSCTL_INT(_machdep, OID_AUTO, report_invltlb_src, CTLFLAG_RW, &report_invltlb_src, 0, ""); __read_mostly static int optimized_invltlb; SYSCTL_INT(_machdep, OID_AUTO, optimized_invltlb, CTLFLAG_RW, &optimized_invltlb, 0, ""); __read_mostly static int all_but_self_ipi_enable = 1; SYSCTL_INT(_machdep, OID_AUTO, all_but_self_ipi_enable, CTLFLAG_RW, &all_but_self_ipi_enable, 0, ""); __read_mostly static int invlpg_timeout = INVLPG_TIMEOUT_DEFAULT; SYSCTL_INT(_machdep, OID_AUTO, invlpg_timeout, CTLFLAG_RW, &invlpg_timeout, 0, ""); /* Local data for detecting CPU TOPOLOGY */ static int core_bits = 0; static int logical_CPU_bits = 0; /* * Calculate usable address in base memory for AP trampoline code. */ u_int mp_bootaddress(u_int basemem) { POSTCODE(MP_BOOTADDRESS_POST); bootMP_size = mptramp_end - mptramp_start; boot_address = trunc_page(basemem * 1024); /* round down to 4k boundary */ if (((basemem * 1024) - boot_address) < bootMP_size) boot_address -= PAGE_SIZE; /* not enough, lower by 4k */ /* 3 levels of page table pages */ mptramp_pagetables = boot_address - (PAGE_SIZE * 3); return mptramp_pagetables; } /* * Print various information about the SMP system hardware and setup. */ void mp_announce(void) { int x; POSTCODE(MP_ANNOUNCE_POST); kprintf("DragonFly/MP: Multiprocessor motherboard\n"); kprintf(" cpu0 (BSP): apic id: %2d\n", CPUID_TO_APICID(0)); for (x = 1; x <= naps; ++x) kprintf(" cpu%d (AP): apic id: %2d\n", x, CPUID_TO_APICID(x)); if (!ioapic_enable) kprintf(" Warning: APIC I/O disabled\n"); } /* * AP cpu's call this to sync up protected mode. * * WARNING! %gs is not set up on entry. This routine sets up %gs. */ void init_secondary(void) { int gsel_tss; int x, myid = bootAP; u_int64_t msr, cr0; struct mdglobaldata *md; struct privatespace *ps; struct user_segment_descriptor *gdt; ps = CPU_prvspace[myid]; gdt = ps->mdglobaldata.gd_gdt; gdt_segs[GPROC0_SEL].ssd_base = (long)&ps->common_tss; ps->mdglobaldata.mi.gd_prvspace = ps; /* We fill the 32-bit segment descriptors */ for (x = 0; x < NGDT; x++) { if (x != GPROC0_SEL && x != (GPROC0_SEL + 1)) ssdtosd(&gdt_segs[x], &gdt[x]); } /* And now a 64-bit one */ ssdtosyssd(&gdt_segs[GPROC0_SEL], (struct system_segment_descriptor *)&gdt[GPROC0_SEL]); r_gdt.rd_limit = MAXGDT_LIMIT - 1; r_gdt.rd_base = (long)(intptr_t)gdt; lgdt(&r_gdt); /* does magic intra-segment return */ /* lgdt() destroys the GSBASE value, so we load GSBASE after lgdt() */ wrmsr(MSR_FSBASE, 0); /* User value */ wrmsr(MSR_GSBASE, (u_int64_t)ps); wrmsr(MSR_KGSBASE, 0); /* XXX User value while we're in the kernel */ lidt(&r_idt_arr[mdcpu->mi.gd_cpuid]); load_ds(_udatasel); load_es(_udatasel); load_fs(_udatasel); #if 0 lldt(_default_ldt); mdcpu->gd_currentldt = _default_ldt; #endif gsel_tss = GSEL(GPROC0_SEL, SEL_KPL); gdt[GPROC0_SEL].sd_type = SDT_SYSTSS; md = mdcpu; /* loaded through %gs:0 (mdglobaldata.mi.gd_prvspace)*/ /* * TSS entry point for interrupts, traps, and exceptions * (sans NMI). This will always go to near the top of the pcpu * trampoline area. Hardware-pushed data will be copied into * the trap-frame on entry, and (if necessary) returned to the * trampoline on exit. * * We store some pcb data for the trampoline code above the * stack the cpu hw pushes into, and arrange things so the * address of tr_pcb_rsp is the same as the desired top of * stack. */ ps->common_tss.tss_rsp0 = (register_t)&ps->trampoline.tr_pcb_rsp; ps->trampoline.tr_pcb_rsp = ps->common_tss.tss_rsp0; ps->trampoline.tr_pcb_gs_kernel = (register_t)md; ps->trampoline.tr_pcb_cr3 = KPML4phys; /* adj to user cr3 live */ ps->dbltramp.tr_pcb_gs_kernel = (register_t)md; ps->dbltramp.tr_pcb_cr3 = KPML4phys; ps->dbgtramp.tr_pcb_gs_kernel = (register_t)md; ps->dbgtramp.tr_pcb_cr3 = KPML4phys; #if 0 /* JG XXX */ ps->common_tss.tss_ioopt = (sizeof ps->common_tss) << 16; #endif md->gd_tss_gdt = &gdt[GPROC0_SEL]; md->gd_common_tssd = *md->gd_tss_gdt; /* double fault stack */ ps->common_tss.tss_ist1 = (register_t)&ps->dbltramp.tr_pcb_rsp; ps->common_tss.tss_ist2 = (register_t)&ps->dbgtramp.tr_pcb_rsp; ltr(gsel_tss); /* * Set to a known state: * Set by mpboot.s: CR0_PG, CR0_PE * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM */ cr0 = rcr0(); cr0 &= ~(CR0_CD | CR0_NW | CR0_EM); load_cr0(cr0); /* Set up the fast syscall stuff */ msr = rdmsr(MSR_EFER) | EFER_SCE; wrmsr(MSR_EFER, msr); wrmsr(MSR_LSTAR, (u_int64_t)IDTVEC(fast_syscall)); wrmsr(MSR_CSTAR, (u_int64_t)IDTVEC(fast_syscall32)); msr = ((u_int64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) | ((u_int64_t)GSEL(GUCODE32_SEL, SEL_UPL) << 48); wrmsr(MSR_STAR, msr); wrmsr(MSR_SF_MASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D|PSL_IOPL|PSL_AC); pmap_set_opt(); /* PSE/4MB pages, etc */ pmap_init_pat(); /* Page Attribute Table */ /* set up CPU registers and state */ cpu_setregs(); /* set up SSE/NX registers */ initializecpu(myid); /* set up FPU state on the AP */ npxinit(); /* If BSP is in the X2APIC mode, put the AP into the X2APIC mode. */ if (x2apic_enable) lapic_x2apic_enter(FALSE); /* disable the APIC, just to be SURE */ LAPIC_WRITE(svr, (LAPIC_READ(svr) & ~APIC_SVR_ENABLE)); } /******************************************************************* * local functions and data */ /* * Start the SMP system */ static void mp_start_aps(void *dummy __unused) { if (lapic_enable) { /* start each Application Processor */ start_all_aps(boot_address); } else { mp_bsp_simple_setup(); } } SYSINIT(startaps, SI_BOOT2_START_APS, SI_ORDER_FIRST, mp_start_aps, NULL); /* * start each AP in our list */ static int start_all_aps(u_int boot_addr) { vm_offset_t va = boot_address + KERNBASE; u_int64_t *pt4, *pt3, *pt2; int pssize; int x, i; int shift; int smicount; int smibest; int smilast; u_char mpbiosreason; u_long mpbioswarmvec; struct mdglobaldata *gd; struct privatespace *ps; size_t ipiq_size; POSTCODE(START_ALL_APS_POST); /* install the AP 1st level boot code */ pmap_kenter(va, boot_address); cpu_invlpg((void *)va); /* JG XXX */ bcopy(mptramp_start, (void *)va, bootMP_size); /* Locate the page tables, they'll be below the trampoline */ pt4 = (u_int64_t *)(uintptr_t)(mptramp_pagetables + KERNBASE); pt3 = pt4 + (PAGE_SIZE) / sizeof(u_int64_t); pt2 = pt3 + (PAGE_SIZE) / sizeof(u_int64_t); /* Create the initial 1GB replicated page tables */ for (i = 0; i < 512; i++) { /* Each slot of the level 4 pages points to the same level 3 page */ pt4[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + PAGE_SIZE); pt4[i] |= kernel_pmap->pmap_bits[PG_V_IDX] | kernel_pmap->pmap_bits[PG_RW_IDX] | kernel_pmap->pmap_bits[PG_U_IDX]; /* Each slot of the level 3 pages points to the same level 2 page */ pt3[i] = (u_int64_t)(uintptr_t)(mptramp_pagetables + (2 * PAGE_SIZE)); pt3[i] |= kernel_pmap->pmap_bits[PG_V_IDX] | kernel_pmap->pmap_bits[PG_RW_IDX] | kernel_pmap->pmap_bits[PG_U_IDX]; /* The level 2 page slots are mapped with 2MB pages for 1GB. */ pt2[i] = i * (2 * 1024 * 1024); pt2[i] |= kernel_pmap->pmap_bits[PG_V_IDX] | kernel_pmap->pmap_bits[PG_RW_IDX] | kernel_pmap->pmap_bits[PG_PS_IDX] | kernel_pmap->pmap_bits[PG_U_IDX]; } /* save the current value of the warm-start vector */ mpbioswarmvec = *((u_int32_t *) WARMBOOT_OFF); outb(CMOS_REG, BIOS_RESET); mpbiosreason = inb(CMOS_DATA); /* setup a vector to our boot code */ *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET; *((volatile u_short *) WARMBOOT_SEG) = (boot_address >> 4); outb(CMOS_REG, BIOS_RESET); outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */ /* * If we have a TSC we can figure out the SMI interrupt rate. * The SMI does not necessarily use a constant rate. Spend * up to 250ms trying to figure it out. */ smibest = 0; if (cpu_feature & CPUID_TSC) { set_apic_timer(275000); smilast = read_apic_timer(); for (x = 0; x < 20 && read_apic_timer(); ++x) { smicount = smitest(); if (smibest == 0 || smilast - smicount < smibest) smibest = smilast - smicount; smilast = smicount; } if (smibest > 250000) smibest = 0; } if (smibest) kprintf("SMI Frequency (worst case): %d Hz (%d us)\n", 1000000 / smibest, smibest); /* * This is nasty but if we are a guest in a virtual machine, * give the smpinvl synchronization code up to 60 seconds */ if (vmm_guest != VMM_GUEST_NONE) invlpg_timeout = INVLPG_TIMEOUT_VM; /* start each AP */ for (x = 1; x <= naps; ++x) { /* This is a bit verbose, it will go away soon. */ pssize = sizeof(struct privatespace); ps = (void *) kmem_alloc3(kernel_map, pssize, VM_SUBSYS_GD, KM_CPU(x)); bzero(ps, pssize); CPU_prvspace[x] = ps; gd = &ps->mdglobaldata; gd->mi.gd_prvspace = ps; gd->gd_gdt = (void *) kmem_alloc3(kernel_map, MAXGDT_LIMIT, VM_SUBSYS_GD, KM_CPU(x)); bzero(gd->gd_gdt, MAXGDT_LIMIT); #if 0 kprintf("ps %d %p %d\n", x, ps, pssize); #endif /* prime data page for it to use */ mi_gdinit(&gd->mi, x); cpu_gdinit(gd, x); ipiq_size = sizeof(struct lwkt_ipiq) * (naps + 1); gd->mi.gd_ipiq = (void *)kmem_alloc3(kernel_map, ipiq_size, VM_SUBSYS_IPIQ, KM_CPU(x)); bzero(gd->mi.gd_ipiq, ipiq_size); gd->gd_acpi_id = CPUID_TO_ACPIID(gd->mi.gd_cpuid); /* initialize arc4random. */ arc4_init_pcpu(x); /* setup a vector to our boot code */ *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET; *((volatile u_short *) WARMBOOT_SEG) = (boot_addr >> 4); outb(CMOS_REG, BIOS_RESET); outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */ /* * Setup the AP boot stack */ bootSTK = &ps->idlestack[UPAGES * PAGE_SIZE - PAGE_SIZE]; bootAP = x; /* attempt to start the Application Processor */ CHECK_INIT(99); /* setup checkpoints */ if (!start_ap(gd, boot_addr, smibest)) { kprintf("\nAP #%d (PHY# %d) failed!\n", x, CPUID_TO_APICID(x)); CHECK_PRINT("trace"); /* show checkpoints */ /* better panic as the AP may be running loose */ kprintf("panic y/n? [y] "); cnpoll(TRUE); if (cngetc() != 'n') panic("bye-bye"); cnpoll(FALSE); } CHECK_PRINT("trace"); /* show checkpoints */ } /* set ncpus to 1 + highest logical cpu. Not all may have come up */ ncpus = x; for (shift = 0; (1 << shift) <= ncpus; ++shift) ; --shift; /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */ if ((1 << shift) < ncpus) ++shift; ncpus_fit = 1 << shift; ncpus_fit_mask = ncpus_fit - 1; /* build our map of 'other' CPUs */ mycpu->gd_other_cpus = smp_startup_mask; CPUMASK_NANDBIT(mycpu->gd_other_cpus, mycpu->gd_cpuid); malloc_reinit_ncpus(); gd = (struct mdglobaldata *)mycpu; gd->gd_acpi_id = CPUID_TO_ACPIID(mycpu->gd_cpuid); ipiq_size = sizeof(struct lwkt_ipiq) * ncpus; mycpu->gd_ipiq = (void *)kmem_alloc3(kernel_map, ipiq_size, VM_SUBSYS_IPIQ, KM_CPU(0)); bzero(mycpu->gd_ipiq, ipiq_size); /* initialize arc4random. */ arc4_init_pcpu(0); /* restore the warmstart vector */ *(u_long *) WARMBOOT_OFF = mpbioswarmvec; outb(CMOS_REG, BIOS_RESET); outb(CMOS_DATA, mpbiosreason); /* * NOTE! The idlestack for the BSP was setup by locore. Finish * up, clean out the P==V mapping we did earlier. */ pmap_set_opt(); /* * Wait all APs to finish initializing LAPIC */ if (bootverbose) kprintf("SMP: Waiting APs LAPIC initialization\n"); if (cpu_feature & CPUID_TSC) tsc0_offset = rdtsc(); tsc_offsets[0] = 0; mp_finish_lapic = 1; rel_mplock(); while (CPUMASK_CMPMASKNEQ(smp_lapic_mask, smp_startup_mask)) { cpu_pause(); cpu_lfence(); if (cpu_feature & CPUID_TSC) tsc0_offset = rdtsc(); } while (try_mplock() == 0) { cpu_pause(); cpu_lfence(); } /* number of APs actually started */ return ncpus - 1; } /* * load the 1st level AP boot code into base memory. */ /* targets for relocation */ extern void bigJump(void); extern void bootCodeSeg(void); extern void bootDataSeg(void); extern void MPentry(void); extern u_int MP_GDT; extern u_int mp_gdtbase; #if 0 static void install_ap_tramp(u_int boot_addr) { int x; int size = *(int *) ((u_long) & bootMP_size); u_char *src = (u_char *) ((u_long) bootMP); u_char *dst = (u_char *) boot_addr + KERNBASE; u_int boot_base = (u_int) bootMP; u_int8_t *dst8; u_int16_t *dst16; u_int32_t *dst32; POSTCODE(INSTALL_AP_TRAMP_POST); for (x = 0; x < size; ++x) *dst++ = *src++; /* * modify addresses in code we just moved to basemem. unfortunately we * need fairly detailed info about mpboot.s for this to work. changes * to mpboot.s might require changes here. */ /* boot code is located in KERNEL space */ dst = (u_char *) boot_addr + KERNBASE; /* modify the lgdt arg */ dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base)); *dst32 = boot_addr + ((u_int) & MP_GDT - boot_base); /* modify the ljmp target for MPentry() */ dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1); *dst32 = ((u_int) MPentry - KERNBASE); /* modify the target for boot code segment */ dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base)); dst8 = (u_int8_t *) (dst16 + 1); *dst16 = (u_int) boot_addr & 0xffff; *dst8 = ((u_int) boot_addr >> 16) & 0xff; /* modify the target for boot data segment */ dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base)); dst8 = (u_int8_t *) (dst16 + 1); *dst16 = (u_int) boot_addr & 0xffff; *dst8 = ((u_int) boot_addr >> 16) & 0xff; } #endif /* * This function starts the AP (application processor) identified * by the APIC ID 'physicalCpu'. It does quite a "song and dance" * to accomplish this. This is necessary because of the nuances * of the different hardware we might encounter. It ain't pretty, * but it seems to work. * * NOTE: eventually an AP gets to ap_init(), which is called just * before the AP goes into the LWKT scheduler's idle loop. */ static int start_ap(struct mdglobaldata *gd, u_int boot_addr, int smibest) { int physical_cpu; int vector; POSTCODE(START_AP_POST); /* get the PHYSICAL APIC ID# */ physical_cpu = CPUID_TO_APICID(gd->mi.gd_cpuid); /* calculate the vector */ vector = (boot_addr >> 12) & 0xff; /* We don't want anything interfering */ cpu_disable_intr(); /* Make sure the target cpu sees everything */ wbinvd(); /* * Try to detect when a SMI has occurred, wait up to 200ms. * * If a SMI occurs during an AP reset but before we issue * the STARTUP command, the AP may brick. To work around * this problem we hold off doing the AP startup until * after we have detected the SMI. Hopefully another SMI * will not occur before we finish the AP startup. * * Retries don't seem to help. SMIs have a window of opportunity * and if USB->legacy keyboard emulation is enabled in the BIOS * the interrupt rate can be quite high. * * NOTE: Don't worry about the L1 cache load, it might bloat * ldelta a little but ndelta will be so huge when the SMI * occurs the detection logic will still work fine. */ if (smibest) { set_apic_timer(200000); smitest(); } /* * first we do an INIT/RESET IPI this INIT IPI might be run, reseting * and running the target CPU. OR this INIT IPI might be latched (P5 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be * ignored. * * see apic/apicreg.h for icr bit definitions. * * TIME CRITICAL CODE, DO NOT DO ANY KPRINTFS IN THE HOT PATH. */ /* * Do an INIT IPI: assert RESET * * Use edge triggered mode to assert INIT */ lapic_seticr_sync(physical_cpu, APIC_DESTMODE_PHY | APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE | APIC_LEVEL_ASSERT | APIC_DELMODE_INIT); /* * The spec calls for a 10ms delay but we may have to use a * MUCH lower delay to avoid bricking an AP due to a fast SMI * interrupt. We have other loops here too and dividing by 2 * doesn't seem to be enough even after subtracting 350us, * so we divide by 4. * * Our minimum delay is 150uS, maximum is 10ms. If no SMI * interrupt was detected we use the full 10ms. */ if (smibest == 0) u_sleep(10000); else if (smibest < 150 * 4 + 350) u_sleep(150); else if ((smibest - 350) / 4 < 10000) u_sleep((smibest - 350) / 4); else u_sleep(10000); /* * Do an INIT IPI: deassert RESET * * Use level triggered mode to deassert. It is unclear * why we need to do this. */ lapic_seticr_sync(physical_cpu, APIC_DESTMODE_PHY | APIC_DEST_DESTFLD | APIC_TRIGMOD_LEVEL | APIC_LEVEL_DEASSERT | APIC_DELMODE_INIT); u_sleep(150); /* wait 150us */ /* * Next we do a STARTUP IPI: the previous INIT IPI might still be * latched, (P5 bug) this 1st STARTUP would then terminate * immediately, and the previously started INIT IPI would continue. OR * the previous INIT IPI has already run. and this STARTUP IPI will * run. OR the previous INIT IPI was ignored. and this STARTUP IPI * will run. * * XXX set APIC_LEVEL_ASSERT */ lapic_seticr_sync(physical_cpu, APIC_DESTMODE_PHY | APIC_DEST_DESTFLD | APIC_DELMODE_STARTUP | vector); u_sleep(200); /* wait ~200uS */ /* * Finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is * recognized after hardware RESET or INIT IPI. * * XXX set APIC_LEVEL_ASSERT */ lapic_seticr_sync(physical_cpu, APIC_DESTMODE_PHY | APIC_DEST_DESTFLD | APIC_DELMODE_STARTUP | vector); /* Resume normal operation */ cpu_enable_intr(); /* wait for it to start, see ap_init() */ set_apic_timer(5000000);/* == 5 seconds */ while (read_apic_timer()) { if (CPUMASK_TESTBIT(smp_startup_mask, gd->mi.gd_cpuid)) return 1; /* return SUCCESS */ } return 0; /* return FAILURE */ } static int smitest(void) { int64_t ltsc; int64_t ntsc; int64_t ldelta; int64_t ndelta; int count; ldelta = 0; ndelta = 0; while (read_apic_timer()) { ltsc = rdtsc(); for (count = 0; count < 100; ++count) ntsc = rdtsc(); /* force loop to occur */ if (ldelta) { ndelta = ntsc - ltsc; if (ldelta > ndelta) ldelta = ndelta; if (ndelta > ldelta * 2) break; } else { ldelta = ntsc - ltsc; } } return(read_apic_timer()); } /* * Synchronously flush the TLB on all other CPU's. The current cpu's * TLB is not flushed. If the caller wishes to flush the current cpu's * TLB the caller must call cpu_invltlb() in addition to smp_invltlb(). * * This routine may be called concurrently from multiple cpus. When this * happens, smp_invltlb() can wind up sticking around in the confirmation * while() loop at the end as additional cpus are added to the global * cpumask, until they are acknowledged by another IPI. * * NOTE: If for some reason we were unable to start all cpus we cannot * safely use broadcast IPIs. */ cpumask_t smp_smurf_mask; static cpumask_t smp_invltlb_mask; #define LOOPRECOVER #define LOOPMASK_IN #ifdef LOOPMASK_IN cpumask_t smp_in_mask; #endif cpumask_t smp_invmask; extern cpumask_t smp_idleinvl_mask; extern cpumask_t smp_idleinvl_reqs; /* * Atomically OR bits in *mask to smp_smurf_mask. Adjust *mask to remove * bits that do not need to be IPId. These bits are still part of the command, * but the target cpus have already been signalled and do not need to be * sigalled again. */ #include <sys/spinlock.h> #include <sys/spinlock2.h> static __noinline void smp_smurf_fetchset(cpumask_t *mask) { cpumask_t omask; int i; __uint64_t obits; __uint64_t nbits; i = 0; while (i < CPUMASK_ELEMENTS) { obits = smp_smurf_mask.ary[i]; cpu_ccfence(); nbits = obits | mask->ary[i]; if (atomic_cmpset_long(&smp_smurf_mask.ary[i], obits, nbits)) { omask.ary[i] = obits; ++i; } } CPUMASK_NANDMASK(*mask, omask); } /* * This is a mechanism which guarantees that cpu_invltlb() will be executed * on idle cpus without having to signal or wake them up. The invltlb will be * executed when they wake up, prior to any scheduling or interrupt thread. * * (*mask) is modified to remove the cpus we successfully negotiate this * function with. This function may only be used with semi-synchronous * commands (typically invltlb's or semi-synchronous invalidations which * are usually associated only with kernel memory). */ void smp_smurf_idleinvlclr(cpumask_t *mask) { if (optimized_invltlb) { ATOMIC_CPUMASK_ORMASK(smp_idleinvl_reqs, *mask); /* cpu_lfence() not needed */ CPUMASK_NANDMASK(*mask, smp_idleinvl_mask); } } /* * Issue cpu_invltlb() across all cpus except the current cpu. * * This function will arrange to avoid idle cpus, but still gurantee that * invltlb is run on them when they wake up prior to any scheduling or * nominal interrupt. */ void smp_invltlb(void) { struct mdglobaldata *md = mdcpu; cpumask_t mask; unsigned long rflags; #ifdef LOOPRECOVER tsc_uclock_t tsc_base = rdtsc(); int repeats = 0; #endif if (report_invltlb_src > 0) { if (--report_invltlb_src <= 0) print_backtrace(8); } /* * Disallow normal interrupts, set all active cpus except our own * in the global smp_invltlb_mask. */ ++md->mi.gd_cnt.v_smpinvltlb; crit_enter_gd(&md->mi); /* * Bits we want to set in smp_invltlb_mask. We do not want to signal * our own cpu. Also try to remove bits associated with idle cpus * that we can flag for auto-invltlb. */ mask = smp_active_mask; CPUMASK_NANDBIT(mask, md->mi.gd_cpuid); smp_smurf_idleinvlclr(&mask); rflags = read_rflags(); cpu_disable_intr(); ATOMIC_CPUMASK_ORMASK(smp_invltlb_mask, mask); /* * IPI non-idle cpus represented by mask. The omask calculation * removes cpus from the mask which already have a Xinvltlb IPI * pending (avoid double-queueing the IPI). * * We must disable real interrupts when setting the smurf flags or * we might race a XINVLTLB before we manage to send the ipi's for * the bits we set. * * NOTE: We are not signalling ourselves, mask already does NOT * include our own cpu. */ smp_smurf_fetchset(&mask); /* * Issue the IPI. Note that the XINVLTLB IPI runs regardless of * the critical section count on the target cpus. */ CPUMASK_ORMASK(mask, md->mi.gd_cpumask); if (all_but_self_ipi_enable && (all_but_self_ipi_enable >= 2 || CPUMASK_CMPMASKEQ(smp_startup_mask, mask))) { all_but_self_ipi(XINVLTLB_OFFSET); } else { CPUMASK_NANDMASK(mask, md->mi.gd_cpumask); selected_apic_ipi(mask, XINVLTLB_OFFSET, APIC_DELMODE_FIXED); } /* * Wait for acknowledgement by all cpus. smp_inval_intr() will * temporarily enable interrupts to avoid deadlocking the lapic, * and will also handle running cpu_invltlb() and remote invlpg * command son our cpu if some other cpu requests it of us. * * WARNING! I originally tried to implement this as a hard loop * checking only smp_invltlb_mask (and issuing a local * cpu_invltlb() if requested), with interrupts enabled * and without calling smp_inval_intr(). This DID NOT WORK. * It resulted in weird races where smurf bits would get * cleared without any action being taken. */ smp_inval_intr(); CPUMASK_ASSZERO(mask); while (CPUMASK_CMPMASKNEQ(smp_invltlb_mask, mask)) { smp_inval_intr(); cpu_pause(); #ifdef LOOPRECOVER if (tsc_frequency && rdtsc() - tsc_base > tsc_frequency) { /* * cpuid - cpu doing the waiting * invltlb_mask - IPI in progress */ kprintf("smp_invltlb %2d: WARNING blocked %d sec: " "inv=%08jx " "smurf=%08jx " #ifdef LOOPMASK_IN "in=%08jx " #endif "idle=%08jx/%08jx\n", md->mi.gd_cpuid, repeats + 1, smp_invltlb_mask.ary[0], smp_smurf_mask.ary[0], #ifdef LOOPMASK_IN smp_in_mask.ary[0], #endif smp_idleinvl_mask.ary[0], smp_idleinvl_reqs.ary[0]); mdcpu->gd_xinvaltlb = 0; ATOMIC_CPUMASK_NANDMASK(smp_smurf_mask, smp_invltlb_mask); smp_invlpg(&smp_active_mask); /* * Reload tsc_base for retry, give up after * 10 seconds (60 seconds if in VM). */ tsc_base = rdtsc(); if (++repeats > invlpg_timeout) { kprintf("smp_invltlb: giving up\n"); CPUMASK_ASSZERO(smp_invltlb_mask); } } #endif } write_rflags(rflags); crit_exit_gd(&md->mi); } /* * Called from a critical section with interrupts hard-disabled. * This function issues an XINVLTLB IPI and then executes any pending * command on the current cpu before returning. */ void smp_invlpg(cpumask_t *cmdmask) { struct mdglobaldata *md = mdcpu; cpumask_t mask; if (report_invlpg_src > 0) { if (--report_invlpg_src <= 0) print_backtrace(8); } /* * Disallow normal interrupts, set all active cpus in the pmap, * plus our own for completion processing (it might or might not * be part of the set). */ mask = smp_active_mask; CPUMASK_ANDMASK(mask, *cmdmask); CPUMASK_ORMASK(mask, md->mi.gd_cpumask); /* * Avoid double-queuing IPIs, which can deadlock us. We must disable * real interrupts when setting the smurf flags or we might race a * XINVLTLB before we manage to send the ipi's for the bits we set. * * NOTE: We might be including our own cpu in the smurf mask. */ smp_smurf_fetchset(&mask); /* * Issue the IPI. Note that the XINVLTLB IPI runs regardless of * the critical section count on the target cpus. * * We do not include our own cpu when issuing the IPI. */ if (all_but_self_ipi_enable && (all_but_self_ipi_enable >= 2 || CPUMASK_CMPMASKEQ(smp_startup_mask, mask))) { all_but_self_ipi(XINVLTLB_OFFSET); } else { CPUMASK_NANDMASK(mask, md->mi.gd_cpumask); selected_apic_ipi(mask, XINVLTLB_OFFSET, APIC_DELMODE_FIXED); } /* * This will synchronously wait for our command to complete, * as well as process commands from other cpus. It also handles * reentrancy. * * (interrupts are disabled and we are in a critical section here) */ smp_inval_intr(); } /* * Issue rip/rsp sniffs */ void smp_sniff(void) { globaldata_t gd = mycpu; int dummy; register_t rflags; /* * Ignore all_but_self_ipi_enable here and just use it. */ rflags = read_rflags(); cpu_disable_intr(); all_but_self_ipi(XSNIFF_OFFSET); gd->gd_sample_pc = smp_sniff; gd->gd_sample_sp = &dummy; write_rflags(rflags); } void cpu_sniff(int dcpu) { globaldata_t rgd = globaldata_find(dcpu); register_t rflags; int dummy; /* * Ignore all_but_self_ipi_enable here and just use it. */ rflags = read_rflags(); cpu_disable_intr(); single_apic_ipi(dcpu, XSNIFF_OFFSET, APIC_DELMODE_FIXED); rgd->gd_sample_pc = cpu_sniff; rgd->gd_sample_sp = &dummy; write_rflags(rflags); } /* * Called from Xinvltlb assembly with interrupts hard-disabled and in a * critical section. gd_intr_nesting_level may or may not be bumped * depending on entry. * * THIS CODE IS INTENDED TO EXPLICITLY IGNORE THE CRITICAL SECTION COUNT. * THAT IS, THE INTERRUPT IS INTENDED TO FUNCTION EVEN WHEN MAINLINE CODE * IS IN A CRITICAL SECTION. */ void smp_inval_intr(void) { struct mdglobaldata *md = mdcpu; cpumask_t cpumask; #ifdef LOOPRECOVER tsc_uclock_t tsc_base = rdtsc(); #endif #if 0 /* * The idle code is in a critical section, but that doesn't stop * Xinvltlb from executing, so deal with the race which can occur * in that situation. Otherwise r-m-w operations by pmap_inval_intr() * may have problems. */ if (ATOMIC_CPUMASK_TESTANDCLR(smp_idleinvl_reqs, md->mi.gd_cpuid)) { ATOMIC_CPUMASK_NANDBIT(smp_invltlb_mask, md->mi.gd_cpuid); cpu_invltlb(); cpu_mfence(); } #endif /* * This is a real mess. I'd like to just leave interrupts disabled * but it can cause the lapic to deadlock if too many interrupts queue * to it, due to the idiotic design of the lapic. So instead we have * to enter a critical section so normal interrupts are made pending * and track whether this one was reentered. */ if (md->gd_xinvaltlb) { /* reentrant on cpu */ md->gd_xinvaltlb = 2; return; } md->gd_xinvaltlb = 1; /* * Check only those cpus with active Xinvl* commands pending. * * We are going to enable interrupts so make sure we are in a * critical section. This is necessary to avoid deadlocking * the lapic and to ensure that we execute our commands prior to * any nominal interrupt or preemption. * * WARNING! It is very important that we only clear out but in * smp_smurf_mask once for each interrupt we take. In * this case, we clear it on initial entry and only loop * on the reentrancy detect (caused by another interrupt). */ cpumask = smp_invmask; #ifdef LOOPMASK_IN ATOMIC_CPUMASK_ORBIT(smp_in_mask, md->mi.gd_cpuid); #endif loop: cpu_enable_intr(); ATOMIC_CPUMASK_NANDBIT(smp_smurf_mask, md->mi.gd_cpuid); /* * Specific page request(s), and we can't return until all bits * are zero. */ for (;;) { int toolong; /* * Also execute any pending full invalidation request in * this loop. */ if (CPUMASK_TESTBIT(smp_invltlb_mask, md->mi.gd_cpuid)) { ATOMIC_CPUMASK_NANDBIT(smp_invltlb_mask, md->mi.gd_cpuid); cpu_invltlb(); cpu_mfence(); } #ifdef LOOPRECOVER if (tsc_frequency && rdtsc() - tsc_base > tsc_frequency) { /* * cpuid - cpu doing the waiting * invmask - IPI in progress * invltlb_mask - which ones are TLB invalidations? */ kprintf("smp_inval_intr %2d, WARNING blocked >1 sec " "inv=%08jx tlbm=%08jx " "smurf=%08jx " #ifdef LOOPMASK_IN "in=%08jx " #endif "idle=%08jx/%08jx\n", md->mi.gd_cpuid, smp_invmask.ary[0], smp_invltlb_mask.ary[0], smp_smurf_mask.ary[0], #ifdef LOOPMASK_IN smp_in_mask.ary[0], #endif smp_idleinvl_mask.ary[0], smp_idleinvl_reqs.ary[0]); tsc_base = rdtsc(); toolong = 1; } else { toolong = 0; } #else toolong = 0; #endif /* * We can only add bits to the cpumask to test during the * loop because the smp_invmask bit is cleared once the * originator completes the command (the targets may still * be cycling their own completions in this loop, afterwords). * * lfence required prior to all tests as this Xinvltlb * interrupt could race the originator (already be in progress * wnen the originator decides to issue, due to an issue by * another cpu). */ cpu_lfence(); CPUMASK_ORMASK(cpumask, smp_invmask); /*cpumask = smp_active_mask;*/ /* XXX */ cpu_lfence(); if (pmap_inval_intr(&cpumask, toolong) == 0) { /* * Clear our smurf mask to allow new IPIs, but deal * with potential races. */ break; } /* * Test if someone sent us another invalidation IPI, break * out so we can take it to avoid deadlocking the lapic * interrupt queue (? stupid intel, amd). */ if (md->gd_xinvaltlb == 2) break; /* if (CPUMASK_TESTBIT(smp_smurf_mask, md->mi.gd_cpuid)) break; */ } /* * Full invalidation request */ if (CPUMASK_TESTBIT(smp_invltlb_mask, md->mi.gd_cpuid)) { ATOMIC_CPUMASK_NANDBIT(smp_invltlb_mask, md->mi.gd_cpuid); cpu_invltlb(); cpu_mfence(); } /* * Check to see if another Xinvltlb interrupt occurred and loop up * if it did. */ cpu_disable_intr(); if (md->gd_xinvaltlb == 2) { md->gd_xinvaltlb = 1; goto loop; } #ifdef LOOPMASK_IN ATOMIC_CPUMASK_NANDBIT(smp_in_mask, md->mi.gd_cpuid); #endif md->gd_xinvaltlb = 0; } void cpu_wbinvd_on_all_cpus_callback(void *arg) { wbinvd(); } /* * When called the executing CPU will send an IPI to all other CPUs * requesting that they halt execution. * * Usually (but not necessarily) called with 'other_cpus' as its arg. * * - Signals all CPUs in map to stop. * - Waits for each to stop. * * Returns: * -1: error * 0: NA * 1: ok * * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs * from executing at same time. */ int stop_cpus(cpumask_t map) { cpumask_t mask; CPUMASK_ANDMASK(map, smp_active_mask); /* send the Xcpustop IPI to all CPUs in map */ selected_apic_ipi(map, XCPUSTOP_OFFSET, APIC_DELMODE_FIXED); do { mask = stopped_cpus; CPUMASK_ANDMASK(mask, map); /* spin */ } while (CPUMASK_CMPMASKNEQ(mask, map)); return 1; } /* * Called by a CPU to restart stopped CPUs. * * Usually (but not necessarily) called with 'stopped_cpus' as its arg. * * - Signals all CPUs in map to restart. * - Waits for each to restart. * * Returns: * -1: error * 0: NA * 1: ok */ int restart_cpus(cpumask_t map) { cpumask_t mask; /* signal other cpus to restart */ mask = map; CPUMASK_ANDMASK(mask, smp_active_mask); cpu_ccfence(); started_cpus = mask; cpu_ccfence(); /* wait for each to clear its bit */ while (CPUMASK_CMPMASKNEQ(stopped_cpus, map)) cpu_pause(); return 1; } /* * This is called once the mpboot code has gotten us properly relocated * and the MMU turned on, etc. ap_init() is actually the idle thread, * and when it returns the scheduler will call the real cpu_idle() main * loop for the idlethread. Interrupts are disabled on entry and should * remain disabled at return. */ void ap_init(void) { int cpu_id; /* * Adjust smp_startup_mask to signal the BSP that we have started * up successfully. Note that we do not yet hold the BGL. The BSP * is waiting for our signal. * * We can't set our bit in smp_active_mask yet because we are holding * interrupts physically disabled and remote cpus could deadlock * trying to send us an IPI. */ ATOMIC_CPUMASK_ORBIT(smp_startup_mask, mycpu->gd_cpuid); cpu_mfence(); /* * Interlock for LAPIC initialization. Wait until mp_finish_lapic is * non-zero, then get the MP lock. * * Note: We are in a critical section. * * Note: we are the idle thread, we can only spin. * * Note: The load fence is memory volatile and prevents the compiler * from improperly caching mp_finish_lapic, and the cpu from improperly * caching it. */ while (mp_finish_lapic == 0) { cpu_pause(); cpu_lfence(); } #if 0 while (try_mplock() == 0) { cpu_pause(); cpu_lfence(); } #endif if (cpu_feature & CPUID_TSC) { /* * The BSP is constantly updating tsc0_offset, figure out * the relative difference to synchronize ktrdump. */ tsc_offsets[mycpu->gd_cpuid] = rdtsc() - tsc0_offset; } /* BSP may have changed PTD while we're waiting for the lock */ cpu_invltlb(); /* Build our map of 'other' CPUs. */ mycpu->gd_other_cpus = smp_startup_mask; ATOMIC_CPUMASK_NANDBIT(mycpu->gd_other_cpus, mycpu->gd_cpuid); /* A quick check from sanity claus */ cpu_id = APICID_TO_CPUID(LAPIC_READID); if (mycpu->gd_cpuid != cpu_id) { kprintf("SMP: assigned cpuid = %d\n", mycpu->gd_cpuid); kprintf("SMP: actual cpuid = %d lapicid %d\n", cpu_id, LAPIC_READID); #if 0 /* JGXXX */ kprintf("PTD[MPPTDI] = %p\n", (void *)PTD[MPPTDI]); #endif panic("cpuid mismatch! boom!!"); } /* Initialize AP's local APIC for irq's */ lapic_init(FALSE); /* LAPIC initialization is done */ ATOMIC_CPUMASK_ORBIT(smp_lapic_mask, mycpu->gd_cpuid); cpu_mfence(); #if 0 /* Let BSP move onto the next initialization stage */ rel_mplock(); #endif /* * Interlock for finalization. Wait until mp_finish is non-zero, * then get the MP lock. * * Note: We are in a critical section. * * Note: we are the idle thread, we can only spin. * * Note: The load fence is memory volatile and prevents the compiler * from improperly caching mp_finish, and the cpu from improperly * caching it. */ while (mp_finish == 0) { cpu_pause(); cpu_lfence(); } /* BSP may have changed PTD while we're waiting for the lock */ cpu_invltlb(); /* Set memory range attributes for this CPU to match the BSP */ mem_range_AP_init(); /* * Once we go active we must process any IPIQ messages that may * have been queued, because no actual IPI will occur until we * set our bit in the smp_active_mask. If we don't the IPI * message interlock could be left set which would also prevent * further IPIs. * * The idle loop doesn't expect the BGL to be held and while * lwkt_switch() normally cleans things up this is a special case * because we returning almost directly into the idle loop. * * The idle thread is never placed on the runq, make sure * nothing we've done put it there. */ /* * Hold a critical section and allow real interrupts to occur. Zero * any spurious interrupts which have accumulated, then set our * smp_active_mask indicating that we are fully operational. */ crit_enter(); __asm __volatile("sti; pause; pause"::); bzero(mdcpu->gd_ipending, sizeof(mdcpu->gd_ipending)); ATOMIC_CPUMASK_ORBIT(smp_active_mask, mycpu->gd_cpuid); /* * Wait until all cpus have set their smp_active_mask and have fully * operational interrupts before proceeding. * * We need a final cpu_invltlb() because we would not have received * any until we set our bit in smp_active_mask. */ while (mp_finish == 1) { cpu_pause(); cpu_lfence(); } cpu_invltlb(); /* * Initialize per-cpu clocks and do other per-cpu initialization. * At this point code is expected to be able to use the full kernel * API. */ initclocks_pcpu(); /* clock interrupts (via IPIs) */ /* * Since we may have cleaned up the interrupt triggers, manually * process any pending IPIs before exiting our critical section. * Once the critical section has exited, normal interrupt processing * may occur. */ atomic_swap_int(&mycpu->gd_npoll, 0); lwkt_process_ipiq(); crit_exit(); /* * Final final, allow the waiting BSP to resume the boot process, * return 'into' the idle thread bootstrap. */ ATOMIC_CPUMASK_ORBIT(smp_finalize_mask, mycpu->gd_cpuid); KKASSERT((curthread->td_flags & TDF_RUNQ) == 0); } /* * Get SMP fully working before we start initializing devices. */ static void ap_finish(void) { if (bootverbose) kprintf("Finish MP startup\n"); rel_mplock(); /* * Wait for the active mask to complete, after which all cpus will * be accepting interrupts. */ mp_finish = 1; while (CPUMASK_CMPMASKNEQ(smp_active_mask, smp_startup_mask)) { cpu_pause(); cpu_lfence(); } /* * Wait for the finalization mask to complete, after which all cpus * have completely finished initializing and are entering or are in * their idle thread. * * BSP should have received all required invltlbs but do another * one just in case. */ cpu_invltlb(); mp_finish = 2; while (CPUMASK_CMPMASKNEQ(smp_finalize_mask, smp_startup_mask)) { cpu_pause(); cpu_lfence(); } while (try_mplock() == 0) { cpu_pause(); cpu_lfence(); } if (bootverbose) { kprintf("Active CPU Mask: %016jx\n", (uintmax_t)CPUMASK_LOWMASK(smp_active_mask)); } } SYSINIT(finishsmp, SI_BOOT2_FINISH_SMP, SI_ORDER_FIRST, ap_finish, NULL); /* * Interrupts must be hard-disabled by caller */ void cpu_send_ipiq(int dcpu) { if (CPUMASK_TESTBIT(smp_active_mask, dcpu)) single_apic_ipi(dcpu, XIPIQ_OFFSET, APIC_DELMODE_FIXED); } #if 0 /* single_apic_ipi_passive() not working yet */ /* * Returns 0 on failure, 1 on success */ int cpu_send_ipiq_passive(int dcpu) { int r = 0; if (CPUMASK_TESTBIT(smp_active_mask, dcpu)) { r = single_apic_ipi_passive(dcpu, XIPIQ_OFFSET, APIC_DELMODE_FIXED); } return(r); } #endif static void mp_bsp_simple_setup(void) { struct mdglobaldata *gd; size_t ipiq_size; /* build our map of 'other' CPUs */ mycpu->gd_other_cpus = smp_startup_mask; CPUMASK_NANDBIT(mycpu->gd_other_cpus, mycpu->gd_cpuid); gd = (struct mdglobaldata *)mycpu; gd->gd_acpi_id = CPUID_TO_ACPIID(mycpu->gd_cpuid); ipiq_size = sizeof(struct lwkt_ipiq) * ncpus; mycpu->gd_ipiq = (void *)kmem_alloc(kernel_map, ipiq_size, VM_SUBSYS_IPIQ); bzero(mycpu->gd_ipiq, ipiq_size); /* initialize arc4random. */ arc4_init_pcpu(0); pmap_set_opt(); if (cpu_feature & CPUID_TSC) tsc0_offset = rdtsc(); } /* * CPU TOPOLOGY DETECTION FUNCTIONS */ /* Detect intel topology using CPUID * Ref: http://www.intel.com/Assets/PDF/appnote/241618.pdf, pg 41 */ static void detect_intel_topology(int count_htt_cores) { int shift = 0; int ecx_index = 0; int core_plus_logical_bits = 0; int cores_per_package; int logical_per_package; int logical_per_core; unsigned int p[4]; if (cpu_high >= 0xb) { goto FUNC_B; } else if (cpu_high >= 0x4) { goto FUNC_4; } else { core_bits = 0; for (shift = 0; (1 << shift) < count_htt_cores; ++shift) ; logical_CPU_bits = 1 << shift; return; } FUNC_B: cpuid_count(0xb, FUNC_B_THREAD_LEVEL, p); /* if 0xb not supported - fallback to 0x4 */ if (p[1] == 0 || (FUNC_B_TYPE(p[2]) != FUNC_B_THREAD_TYPE)) { goto FUNC_4; } logical_CPU_bits = FUNC_B_BITS_SHIFT_NEXT_LEVEL(p[0]); ecx_index = FUNC_B_THREAD_LEVEL + 1; do { cpuid_count(0xb, ecx_index, p); /* Check for the Core type in the implemented sub leaves. */ if (FUNC_B_TYPE(p[2]) == FUNC_B_CORE_TYPE) { core_plus_logical_bits = FUNC_B_BITS_SHIFT_NEXT_LEVEL(p[0]); break; } ecx_index++; } while (FUNC_B_TYPE(p[2]) != FUNC_B_INVALID_TYPE); core_bits = core_plus_logical_bits - logical_CPU_bits; return; FUNC_4: cpuid_count(0x4, 0, p); cores_per_package = FUNC_4_MAX_CORE_NO(p[0]) + 1; logical_per_package = count_htt_cores; logical_per_core = logical_per_package / cores_per_package; for (shift = 0; (1 << shift) < logical_per_core; ++shift) ; logical_CPU_bits = shift; for (shift = 0; (1 << shift) < cores_per_package; ++shift) ; core_bits = shift; return; } /* Detect AMD topology using CPUID * Ref: http://support.amd.com/us/Embedded_TechDocs/25481.pdf, last page */ static void detect_amd_topology(int count_htt_cores) { int shift = 0; if ((cpu_feature & CPUID_HTT) && (amd_feature2 & AMDID2_CMP)) { if (cpu_procinfo2 & AMDID_COREID_SIZE) { core_bits = (cpu_procinfo2 & AMDID_COREID_SIZE) >> AMDID_COREID_SIZE_SHIFT; } else { core_bits = (cpu_procinfo2 & AMDID_CMP_CORES) + 1; for (shift = 0; (1 << shift) < core_bits; ++shift) ; core_bits = shift; } logical_CPU_bits = count_htt_cores >> core_bits; for (shift = 0; (1 << shift) < logical_CPU_bits; ++shift) ; logical_CPU_bits = shift; kprintf("core_bits %d logical_CPU_bits %d\n", core_bits - logical_CPU_bits, logical_CPU_bits); if (amd_feature2 & AMDID2_TOPOEXT) { u_int p[4]; /* eax,ebx,ecx,edx */ int nodes; cpuid_count(0x8000001e, 0, p); switch(((p[1] >> 8) & 3) + 1) { case 1: logical_CPU_bits = 0; break; case 2: logical_CPU_bits = 1; break; case 3: case 4: logical_CPU_bits = 2; break; } /* * Nodes are kind of a stand-in for packages*sockets, * but can be thought of in terms of Numa domains. */ nodes = ((p[2] >> 8) & 7) + 1; switch(nodes) { case 8: case 7: case 6: case 5: --core_bits; /* fallthrough */ case 4: case 3: --core_bits; /* fallthrough */ case 2: --core_bits; /* fallthrough */ case 1: break; } core_bits -= logical_CPU_bits; kprintf("%d-way htt, %d Nodes, %d cores/node\n", (int)(((p[1] >> 8) & 3) + 1), nodes, 1 << core_bits); } #if 0 if (amd_feature2 & AMDID2_TOPOEXT) { u_int p[4]; int i; int type; int level; int share_count; logical_CPU_bits = 0; core_bits = 0; for (i = 0; i < 256; ++i) { cpuid_count(0x8000001d, i, p); type = p[0] & 0x1f; level = (p[0] >> 5) & 0x7; share_count = 1 + ((p[0] >> 14) & 0xfff); if (type == 0) break; kprintf("Topology probe i=%2d type=%d " "level=%d share_count=%d\n", i, type, level, share_count); shift = 0; while ((1 << shift) < share_count) ++shift; switch(type) { case 1: /* * CPUID_TYPE_SMT * * Logical CPU (SMT) */ logical_CPU_bits = shift; break; case 2: /* * CPUID_TYPE_CORE * * Physical subdivision of a package */ core_bits = logical_CPU_bits + shift; break; case 3: /* * CPUID_TYPE_CACHE * * CPU L1/L2/L3 cache */ break; case 4: /* * CPUID_TYPE_PKG * * Package aka chip, equivalent to * socket */ break; } } } #endif } else { for (shift = 0; (1 << shift) < count_htt_cores; ++shift) ; core_bits = shift; logical_CPU_bits = 0; } } static void amd_get_compute_unit_id(void *arg) { u_int regs[4]; do_cpuid(0x8000001e, regs); cpu_node_t * mynode = get_cpu_node_by_cpuid(mycpuid); /* * AMD - CPUID Specification September 2010 * page 34 - //ComputeUnitID = ebx[0:7]// */ mynode->compute_unit_id = regs[1] & 0xff; } int fix_amd_topology(void) { cpumask_t mask; if (cpu_vendor_id != CPU_VENDOR_AMD) return -1; if ((amd_feature2 & AMDID2_TOPOEXT) == 0) return -1; CPUMASK_ASSALLONES(mask); lwkt_cpusync_simple(mask, amd_get_compute_unit_id, NULL); kprintf("Compute unit iDS:\n"); int i; for (i = 0; i < ncpus; i++) { kprintf("%d-%d; \n", i, get_cpu_node_by_cpuid(i)->compute_unit_id); } return 0; } /* * Calculate * - logical_CPU_bits * - core_bits * With the values above (for AMD or INTEL) we are able to generally * detect the CPU topology (number of cores for each level): * Ref: http://wiki.osdev.org/Detecting_CPU_Topology_(80x86) * Ref: http://www.multicoreinfo.com/research/papers/whitepapers/Intel-detect-topology.pdf */ void detect_cpu_topology(void) { static int topology_detected = 0; int count = 0; if (topology_detected) goto OUT; if ((cpu_feature & CPUID_HTT) == 0) { core_bits = 0; logical_CPU_bits = 0; goto OUT; } count = (cpu_procinfo & CPUID_HTT_CORES) >> CPUID_HTT_CORE_SHIFT; if (cpu_vendor_id == CPU_VENDOR_INTEL) detect_intel_topology(count); else if (cpu_vendor_id == CPU_VENDOR_AMD) detect_amd_topology(count); topology_detected = 1; OUT: if (bootverbose) { kprintf("Bits within APICID: logical_CPU_bits: %d; " "core_bits: %d\n", logical_CPU_bits, core_bits); } } /* * Interface functions to calculate chip_ID, * core_number and logical_number * Ref: http://wiki.osdev.org/Detecting_CPU_Topology_(80x86) */ int get_chip_ID(int cpuid) { return get_apicid_from_cpuid(cpuid) >> (logical_CPU_bits + core_bits); } int get_chip_ID_from_APICID(int apicid) { return apicid >> (logical_CPU_bits + core_bits); } int get_core_number_within_chip(int cpuid) { return ((get_apicid_from_cpuid(cpuid) >> logical_CPU_bits) & ((1 << core_bits) - 1)); } int get_logical_CPU_number_within_core(int cpuid) { return (get_apicid_from_cpuid(cpuid) & ((1 << logical_CPU_bits) - 1)); } |