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 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
use super::*;
use crate::util::constants::{BYTES_IN_PAGE, BYTES_IN_WORD, LOG_BITS_IN_BYTE};
use crate::util::conversions::raw_align_up;
use crate::util::heap::layout::vm_layout::BYTES_IN_CHUNK;
use crate::util::memory::{self, MmapAnnotation};
use crate::util::metadata::metadata_val_traits::*;
#[cfg(feature = "vo_bit")]
use crate::util::metadata::vo_bit::VO_BIT_SIDE_METADATA_SPEC;
use crate::util::Address;
use num_traits::FromPrimitive;
use ranges::BitByteRange;
use std::fmt;
use std::io::Result;
use std::sync::atomic::{AtomicU8, Ordering};
/// This struct stores the specification of a side metadata bit-set.
/// It is used as an input to the (inline) functions provided by the side metadata module.
///
/// Each plan or policy which uses a metadata bit-set, needs to create an instance of this struct.
///
/// For performance reasons, objects of this struct should be constants.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct SideMetadataSpec {
/// The name for this side metadata.
pub name: &'static str,
/// Is this side metadata global? Local metadata is used by certain spaces,
/// while global metadata is used by all the spaces.
pub is_global: bool,
/// The offset for this side metadata.
pub offset: SideMetadataOffset,
/// Number of bits needed per region. E.g. 0 = 1 bit, 1 = 2 bit.
pub log_num_of_bits: usize,
/// Number of bytes of the region. E.g. 3 = 8 bytes, 12 = 4096 bytes (page).
pub log_bytes_in_region: usize,
}
impl SideMetadataSpec {
/// Is this spec using contiguous side metadata? If not, it uses chunked side metadata.
pub const fn uses_contiguous_side_metadata(&self) -> bool {
self.is_global || cfg!(target_pointer_width = "64")
}
/// Is offset for this spec Address?
pub const fn is_absolute_offset(&self) -> bool {
self.uses_contiguous_side_metadata()
}
/// If offset for this spec relative? (chunked side metadata for local specs in 32 bits)
pub const fn is_rel_offset(&self) -> bool {
!self.is_absolute_offset()
}
/// Get the absolute offset for the spec.
pub const fn get_absolute_offset(&self) -> Address {
debug_assert!(self.is_absolute_offset());
unsafe { self.offset.addr }
}
/// Get the relative offset for the spec.
pub const fn get_rel_offset(&self) -> usize {
debug_assert!(self.is_rel_offset());
unsafe { self.offset.rel_offset }
}
/// Return the upperbound offset for the side metadata. The next side metadata should be laid out at this offset.
#[cfg(target_pointer_width = "64")]
pub const fn upper_bound_offset(&self) -> SideMetadataOffset {
debug_assert!(self.is_absolute_offset());
SideMetadataOffset {
addr: unsafe { self.offset.addr }
.add(crate::util::metadata::side_metadata::metadata_address_range_size(self)),
}
}
/// Return the upperbound offset for the side metadata. The next side metadata should be laid out at this offset.
#[cfg(target_pointer_width = "32")]
pub const fn upper_bound_offset(&self) -> SideMetadataOffset {
if self.is_absolute_offset() {
SideMetadataOffset {
addr: unsafe { self.offset.addr }
.add(crate::util::metadata::side_metadata::metadata_address_range_size(self)),
}
} else {
SideMetadataOffset {
rel_offset: unsafe { self.offset.rel_offset }
+ crate::util::metadata::side_metadata::metadata_bytes_per_chunk(
self.log_bytes_in_region,
self.log_num_of_bits,
),
}
}
}
/// The upper bound address for metadata address computed for this global spec. The computed metadata address
/// should never be larger than this address. Otherwise, we are accessing the metadata that is laid out
/// after this spec. This spec must be a contiguous side metadata spec (which uses address
/// as offset).
pub const fn upper_bound_address_for_contiguous(&self) -> Address {
debug_assert!(self.is_absolute_offset());
unsafe { self.upper_bound_offset().addr }
}
/// The upper bound address for metadata address computed for this global spec. The computed metadata address
/// should never be larger than this address. Otherwise, we are accessing the metadata that is laid out
/// after this spec. This spec must be a chunked side metadata spec (which uses relative offset). Only 32 bit local
/// side metadata uses chunked metadata.
#[cfg(target_pointer_width = "32")]
pub const fn upper_bound_address_for_chunked(&self, data_addr: Address) -> Address {
debug_assert!(self.is_rel_offset());
address_to_meta_chunk_addr(data_addr).add(unsafe { self.upper_bound_offset().rel_offset })
}
/// Used only for debugging.
/// This panics if the required metadata is not mapped
#[cfg(debug_assertions)]
pub(crate) fn assert_metadata_mapped(&self, data_addr: Address) {
let meta_start = address_to_meta_address(self, data_addr).align_down(BYTES_IN_PAGE);
trace!(
"ensure_metadata_is_mapped({}).meta_start({})",
data_addr,
meta_start
);
memory::panic_if_unmapped(
meta_start,
BYTES_IN_PAGE,
&MmapAnnotation::Misc {
name: "assert_metadata_mapped",
},
);
}
/// Used only for debugging.
/// * Assert if the given MetadataValue type matches the spec.
/// * Assert if the provided value is valid in the spec.
#[cfg(debug_assertions)]
fn assert_value_type<T: MetadataValue>(&self, val: Option<T>) {
let log_b = self.log_num_of_bits;
match log_b {
_ if log_b < 3 => {
assert_eq!(T::LOG2, 3);
if let Some(v) = val {
assert!(
v.to_u8().unwrap() < (1 << (1 << log_b)),
"Input value {:?} is invalid for the spec {:?}",
v,
self
);
}
}
3..=6 => assert_eq!(T::LOG2, log_b as u32),
_ => unreachable!("side metadata > {}-bits is not supported", 1 << log_b),
}
}
/// Check with the mmapper to see if side metadata is mapped for the spec for the data address.
pub(crate) fn is_mapped(&self, data_addr: Address) -> bool {
use crate::MMAPPER;
let meta_addr = address_to_meta_address(self, data_addr);
MMAPPER.is_mapped_address(meta_addr)
}
/// This method is used for bulk zeroing side metadata for a data address range.
pub(crate) fn zero_meta_bits(
meta_start_addr: Address,
meta_start_bit: u8,
meta_end_addr: Address,
meta_end_bit: u8,
) {
let mut visitor = |range| {
match range {
BitByteRange::Bytes { start, end } => {
memory::zero(start, end - start);
false
}
BitByteRange::BitsInByte {
addr,
bit_start,
bit_end,
} => {
// we are zeroing selected bit in one byte
// Get a mask that the bits we need to zero are set to zero, and the other bits are 1.
let mask: u8 =
u8::MAX.checked_shl(bit_end as u32).unwrap_or(0) | !(u8::MAX << bit_start);
unsafe { addr.as_ref::<AtomicU8>() }.fetch_and(mask, Ordering::SeqCst);
false
}
}
};
ranges::break_bit_range(
meta_start_addr,
meta_start_bit,
meta_end_addr,
meta_end_bit,
true,
&mut visitor,
);
}
/// This method is used for bulk setting side metadata for a data address range.
pub(crate) fn set_meta_bits(
meta_start_addr: Address,
meta_start_bit: u8,
meta_end_addr: Address,
meta_end_bit: u8,
) {
let mut visitor = |range| {
match range {
BitByteRange::Bytes { start, end } => {
memory::set(start, 0xff, end - start);
false
}
BitByteRange::BitsInByte {
addr,
bit_start,
bit_end,
} => {
// we are setting selected bits in one byte
// Get a mask that the bits we need to set are 1, and the other bits are 0.
let mask: u8 = !(u8::MAX.checked_shl(bit_end as u32).unwrap_or(0))
& (u8::MAX << bit_start);
unsafe { addr.as_ref::<AtomicU8>() }.fetch_or(mask, Ordering::SeqCst);
false
}
}
};
ranges::break_bit_range(
meta_start_addr,
meta_start_bit,
meta_end_addr,
meta_end_bit,
true,
&mut visitor,
);
}
/// This method does bulk update for the given data range. It calculates the metadata bits for the given data range,
/// and invoke the given method to update the metadata bits.
pub(super) fn bulk_update_metadata(
&self,
start: Address,
size: usize,
update_meta_bits: &impl Fn(Address, u8, Address, u8),
) {
// Update bits for a contiguous side metadata spec. We can simply calculate the data end address, and
// calculate the metadata address for the data end.
let update_contiguous = |data_start: Address, data_bytes: usize| {
if data_bytes == 0 {
return;
}
let meta_start = address_to_meta_address(self, data_start);
let meta_start_shift = meta_byte_lshift(self, data_start);
let meta_end = address_to_meta_address(self, data_start + data_bytes);
let meta_end_shift = meta_byte_lshift(self, data_start + data_bytes);
update_meta_bits(meta_start, meta_start_shift, meta_end, meta_end_shift);
};
// Update bits for a discontiguous side metadata spec (chunked metadata). The side metadata for different
// chunks are stored in discontiguous memory. For example, Chunk #2 follows Chunk #1, but the side metadata
// for Chunk #2 does not immediately follow the side metadata for Chunk #1. So when we bulk update metadata for Chunk #1,
// we cannot update up to the metadata address for the Chunk #2 start. Otherwise it may modify unrelated metadata
// between the two chunks' metadata.
// Instead, we compute how many bytes/bits we need to update.
// The data for which the metadata will be updates has to be in the same chunk.
#[cfg(target_pointer_width = "32")]
let update_discontiguous = |data_start: Address, data_bytes: usize| {
use crate::util::constants::BITS_IN_BYTE;
if data_bytes == 0 {
return;
}
debug_assert_eq!(
data_start.align_down(BYTES_IN_CHUNK),
(data_start + data_bytes - 1).align_down(BYTES_IN_CHUNK),
"The data to be zeroed in discontiguous specs needs to be in the same chunk"
);
let meta_start = address_to_meta_address(self, data_start);
let meta_start_shift = meta_byte_lshift(self, data_start);
// How many bits we need to zero for data_bytes
let meta_total_bits = (data_bytes >> self.log_bytes_in_region) << self.log_num_of_bits;
let meta_delta_bytes = meta_total_bits >> LOG_BITS_IN_BYTE;
let meta_delta_bits: u8 = (meta_total_bits % BITS_IN_BYTE) as u8;
// Calculate the end byte/addr and end bit
let (meta_end, meta_end_shift) = {
let mut end_addr = meta_start + meta_delta_bytes;
let mut end_bit = meta_start_shift + meta_delta_bits;
if end_bit >= BITS_IN_BYTE as u8 {
end_bit -= BITS_IN_BYTE as u8;
end_addr += 1usize;
}
(end_addr, end_bit)
};
update_meta_bits(meta_start, meta_start_shift, meta_end, meta_end_shift);
};
if cfg!(target_pointer_width = "64") || self.is_global {
update_contiguous(start, size);
}
#[cfg(target_pointer_width = "32")]
if !self.is_global {
// per chunk policy-specific metadata for 32-bits targets
let chunk_num = ((start + size).align_down(BYTES_IN_CHUNK)
- start.align_down(BYTES_IN_CHUNK))
/ BYTES_IN_CHUNK;
if chunk_num == 0 {
update_discontiguous(start, size);
} else {
let second_data_chunk = start.align_up(BYTES_IN_CHUNK);
// bzero the first sub-chunk
update_discontiguous(start, second_data_chunk - start);
let last_data_chunk = (start + size).align_down(BYTES_IN_CHUNK);
// bzero the last sub-chunk
update_discontiguous(last_data_chunk, start + size - last_data_chunk);
let mut next_data_chunk = second_data_chunk;
// bzero all chunks in the middle
while next_data_chunk != last_data_chunk {
update_discontiguous(next_data_chunk, BYTES_IN_CHUNK);
next_data_chunk += BYTES_IN_CHUNK;
}
}
}
}
/// Bulk-zero a specific metadata for a memory region. Note that this method is more sophisiticated than a simple memset, especially in the following
/// cases:
/// * the metadata for the range includes partial bytes (a few bits in the same byte).
/// * for 32 bits local side metadata, the side metadata is stored in discontiguous chunks, we will have to bulk zero for each chunk's side metadata.
///
/// # Arguments
///
/// * `start`: The starting address of a memory region. The side metadata starting from this data address will be zeroed.
/// * `size`: The size of the memory region.
pub fn bzero_metadata(&self, start: Address, size: usize) {
#[cfg(feature = "extreme_assertions")]
let _lock = sanity::SANITY_LOCK.lock().unwrap();
#[cfg(feature = "extreme_assertions")]
sanity::verify_bzero(self, start, size);
self.bulk_update_metadata(start, size, &Self::zero_meta_bits)
}
/// Bulk set a specific metadata for a memory region. Note that this method is more sophisiticated than a simple memset, especially in the following
/// cases:
/// * the metadata for the range includes partial bytes (a few bits in the same byte).
/// * for 32 bits local side metadata, the side metadata is stored in discontiguous chunks, we will have to bulk set for each chunk's side metadata.
///
/// # Arguments
///
/// * `start`: The starting address of a memory region. The side metadata starting from this data address will be set to all 1s in the bits.
/// * `size`: The size of the memory region.
pub fn bset_metadata(&self, start: Address, size: usize) {
#[cfg(feature = "extreme_assertions")]
let _lock = sanity::SANITY_LOCK.lock().unwrap();
#[cfg(feature = "extreme_assertions")]
sanity::verify_bset(self, start, size);
self.bulk_update_metadata(start, size, &Self::set_meta_bits)
}
/// Bulk copy the `other` side metadata for a memory region to this side metadata.
///
/// This function only works for contiguous metadata.
/// Curently all global metadata are contiguous.
/// It also requires the other metadata to have the same number of bits per region
/// and the same region size.
///
/// # Arguments
///
/// * `start`: The starting address of a memory region.
/// * `size`: The size of the memory region.
/// * `other`: The other metadata to copy from.
pub fn bcopy_metadata_contiguous(&self, start: Address, size: usize, other: &SideMetadataSpec) {
#[cfg(feature = "extreme_assertions")]
let _lock = sanity::SANITY_LOCK.lock().unwrap();
#[cfg(feature = "extreme_assertions")]
sanity::verify_bcopy(self, start, size, other);
debug_assert_eq!(other.log_bytes_in_region, self.log_bytes_in_region);
debug_assert_eq!(other.log_num_of_bits, self.log_num_of_bits);
let dst_meta_start_addr = address_to_meta_address(self, start);
let dst_meta_start_bit = meta_byte_lshift(self, start);
let dst_meta_end_addr = address_to_meta_address(self, start + size);
let dst_meta_end_bit = meta_byte_lshift(self, start + size);
let src_meta_start_addr = address_to_meta_address(other, start);
let src_meta_start_bit = meta_byte_lshift(other, start);
debug_assert_eq!(dst_meta_start_bit, src_meta_start_bit);
let mut visitor = |range| {
match range {
BitByteRange::Bytes {
start: dst_start,
end: dst_end,
} => unsafe {
let byte_offset = dst_start - dst_meta_start_addr;
let src_start = src_meta_start_addr + byte_offset;
let size = dst_end - dst_start;
std::ptr::copy::<u8>(src_start.to_ptr(), dst_start.to_mut_ptr(), size);
false
},
BitByteRange::BitsInByte {
addr: dst,
bit_start,
bit_end,
} => {
let byte_offset = dst - dst_meta_start_addr;
let src = src_meta_start_addr + byte_offset;
// we are setting selected bits in one byte
let mask: u8 = !(u8::MAX.checked_shl(bit_end as u32).unwrap_or(0))
& (u8::MAX << bit_start); // Get a mask that the bits we need to set are 1, and the other bits are 0.
let old_src = unsafe { src.as_ref::<AtomicU8>() }.load(Ordering::Relaxed);
let old_dst = unsafe { dst.as_ref::<AtomicU8>() }.load(Ordering::Relaxed);
let new = (old_src & mask) | (old_dst & !mask);
unsafe { dst.as_ref::<AtomicU8>() }.store(new, Ordering::Relaxed);
false
}
}
};
ranges::break_bit_range(
dst_meta_start_addr,
dst_meta_start_bit,
dst_meta_end_addr,
dst_meta_end_bit,
true,
&mut visitor,
);
}
/// This is a wrapper method for implementing side metadata access. It does nothing other than
/// calling the access function with no overhead, but in debug builds,
/// it includes multiple checks to make sure the access is sane.
/// * check whether the given value type matches the number of bits for the side metadata.
/// * check if the side metadata memory is mapped.
/// * check if the side metadata content is correct based on a sanity map (only for extreme assertions).
#[allow(unused_variables)] // data_addr/input is not used in release build
fn side_metadata_access<
const CHECK_VALUE: bool,
T: MetadataValue,
R: Copy,
F: FnOnce() -> R,
V: FnOnce(R),
>(
&self,
data_addr: Address,
input: Option<T>,
access_func: F,
verify_func: V,
) -> R {
// With extreme assertions, we maintain a sanity table for each side metadata access. For whatever we store in
// side metadata, we store in the sanity table. So we can use that table to check if its results are conssitent
// with the actual side metadata.
// To achieve this, we need to apply a lock when we access side metadata. This will hide some concurrency bugs,
// but makes it possible for us to assert our side metadata implementation is correct.
#[cfg(feature = "extreme_assertions")]
let _lock = sanity::SANITY_LOCK.lock().unwrap();
// A few checks
#[cfg(debug_assertions)]
{
if CHECK_VALUE {
self.assert_value_type::<T>(input);
}
#[cfg(feature = "extreme_assertions")]
self.assert_metadata_mapped(data_addr);
}
// Actual access to the side metadata
let ret = access_func();
// Verifying the side metadata: checks the result with the sanity table, or store some results to the sanity table
if CHECK_VALUE {
verify_func(ret);
}
ret
}
/// Non-atomic load of metadata.
///
/// # Safety
///
/// This is unsafe because:
///
/// 1. Concurrent access to this operation is undefined behaviour.
/// 2. Interleaving Non-atomic and atomic operations is undefined behaviour.
pub unsafe fn load<T: MetadataValue>(&self, data_addr: Address) -> T {
self.side_metadata_access::<true, T, _, _, _>(
data_addr,
None,
|| {
let meta_addr = address_to_meta_address(self, data_addr);
let bits_num_log = self.log_num_of_bits;
if bits_num_log < 3 {
let lshift = meta_byte_lshift(self, data_addr);
let mask = meta_byte_mask(self) << lshift;
let byte_val = meta_addr.load::<u8>();
FromPrimitive::from_u8((byte_val & mask) >> lshift).unwrap()
} else {
meta_addr.load::<T>()
}
},
|_v| {
#[cfg(feature = "extreme_assertions")]
sanity::verify_load(self, data_addr, _v);
},
)
}
/// Non-atomic store of metadata.
///
/// # Safety
///
/// This is unsafe because:
///
/// 1. Concurrent access to this operation is undefined behaviour.
/// 2. Interleaving Non-atomic and atomic operations is undefined behaviour.
pub unsafe fn store<T: MetadataValue>(&self, data_addr: Address, metadata: T) {
self.side_metadata_access::<true, T, _, _, _>(
data_addr,
Some(metadata),
|| {
let meta_addr = address_to_meta_address(self, data_addr);
let bits_num_log = self.log_num_of_bits;
if bits_num_log < 3 {
let lshift = meta_byte_lshift(self, data_addr);
let mask = meta_byte_mask(self) << lshift;
let old_val = meta_addr.load::<u8>();
let new_val = (old_val & !mask) | (metadata.to_u8().unwrap() << lshift);
meta_addr.store::<u8>(new_val);
} else {
meta_addr.store::<T>(metadata);
}
},
|_| {
#[cfg(feature = "extreme_assertions")]
sanity::verify_store(self, data_addr, metadata);
},
)
}
/// Loads a value from the side metadata for the given address.
/// This method has similar semantics to `store` in Rust atomics.
pub fn load_atomic<T: MetadataValue>(&self, data_addr: Address, order: Ordering) -> T {
self.side_metadata_access::<true, T, _, _, _>(
data_addr,
None,
|| {
let meta_addr = address_to_meta_address(self, data_addr);
let bits_num_log = self.log_num_of_bits;
if bits_num_log < 3 {
let lshift = meta_byte_lshift(self, data_addr);
let mask = meta_byte_mask(self) << lshift;
let byte_val = unsafe { meta_addr.atomic_load::<AtomicU8>(order) };
FromPrimitive::from_u8((byte_val & mask) >> lshift).unwrap()
} else {
unsafe { T::load_atomic(meta_addr, order) }
}
},
|_v| {
#[cfg(feature = "extreme_assertions")]
sanity::verify_load(self, data_addr, _v);
},
)
}
/// Store the given value to the side metadata for the given address.
/// This method has similar semantics to `store` in Rust atomics.
pub fn store_atomic<T: MetadataValue>(&self, data_addr: Address, metadata: T, order: Ordering) {
self.side_metadata_access::<true, T, _, _, _>(
data_addr,
Some(metadata),
|| {
let meta_addr = address_to_meta_address(self, data_addr);
let bits_num_log = self.log_num_of_bits;
if bits_num_log < 3 {
let lshift = meta_byte_lshift(self, data_addr);
let mask = meta_byte_mask(self) << lshift;
let metadata_u8 = metadata.to_u8().unwrap();
let _ = unsafe {
<u8 as MetadataValue>::fetch_update(meta_addr, order, order, |v: u8| {
Some((v & !mask) | (metadata_u8 << lshift))
})
};
} else {
unsafe {
T::store_atomic(meta_addr, metadata, order);
}
}
},
|_| {
#[cfg(feature = "extreme_assertions")]
sanity::verify_store(self, data_addr, metadata);
},
)
}
/// Non-atomically store zero to the side metadata for the given address.
/// This method mainly facilitates clearing multiple metadata specs for the same address in a loop.
///
/// # Safety
///
/// This is unsafe because:
///
/// 1. Concurrent access to this operation is undefined behaviour.
/// 2. Interleaving Non-atomic and atomic operations is undefined behaviour.
pub unsafe fn set_zero(&self, data_addr: Address) {
use num_traits::Zero;
match self.log_num_of_bits {
0..=3 => self.store(data_addr, u8::zero()),
4 => self.store(data_addr, u16::zero()),
5 => self.store(data_addr, u32::zero()),
6 => self.store(data_addr, u64::zero()),
_ => unreachable!(),
}
}
/// Atomiccally store zero to the side metadata for the given address.
/// This method mainly facilitates clearing multiple metadata specs for the same address in a loop.
pub fn set_zero_atomic(&self, data_addr: Address, order: Ordering) {
use num_traits::Zero;
match self.log_num_of_bits {
0..=3 => self.store_atomic(data_addr, u8::zero(), order),
4 => self.store_atomic(data_addr, u16::zero(), order),
5 => self.store_atomic(data_addr, u32::zero(), order),
6 => self.store_atomic(data_addr, u64::zero(), order),
_ => unreachable!(),
}
}
/// Atomically store one to the side metadata for the data address with the _possible_ side effect of corrupting
/// and setting the entire byte in the side metadata to 0xff. This can only be used for side metadata smaller
/// than a byte.
/// This means it does not only set the side metadata for the data address, and it may also have a side effect of
/// corrupting and setting the side metadata for the adjacent data addresses. This method is only intended to be
/// used as an optimization to skip masking and setting bits in some scenarios where setting adjancent bits to 1 is benign.
///
/// # Safety
/// This method _may_ corrupt and set adjacent bits in the side metadata as a side effect. The user must
/// make sure that this behavior is correct and must not rely on the side effect of this method to set bits.
pub unsafe fn set_raw_byte_atomic(&self, data_addr: Address, order: Ordering) {
debug_assert!(self.log_num_of_bits < 3);
cfg_if::cfg_if! {
if #[cfg(feature = "extreme_assertions")] {
// For extreme assertions, we only set 1 to the given address.
self.store_atomic::<u8>(data_addr, 1, order)
} else {
self.side_metadata_access::<false, u8, _, _, _>(
data_addr,
Some(1u8),
|| {
let meta_addr = address_to_meta_address(self, data_addr);
u8::store_atomic(meta_addr, 0xffu8, order);
},
|_| {}
)
}
}
}
/// Load the raw byte in the side metadata byte that is mapped to the data address.
///
/// # Safety
/// This is unsafe because:
///
/// 1. Concurrent access to this operation is undefined behaviour.
/// 2. Interleaving Non-atomic and atomic operations is undefined behaviour.
pub unsafe fn load_raw_byte(&self, data_addr: Address) -> u8 {
debug_assert!(self.log_num_of_bits < 3);
self.side_metadata_access::<false, u8, _, _, _>(
data_addr,
None,
|| {
let meta_addr = address_to_meta_address(self, data_addr);
meta_addr.load::<u8>()
},
|_| {},
)
}
/// Load the raw word that includes the side metadata byte mapped to the data address.
///
/// # Safety
/// This is unsafe because:
///
/// 1. Concurrent access to this operation is undefined behaviour.
/// 2. Interleaving Non-atomic and atomic operations is undefined behaviour.
pub unsafe fn load_raw_word(&self, data_addr: Address) -> usize {
use crate::util::constants::*;
debug_assert!(self.log_num_of_bits < (LOG_BITS_IN_BYTE + LOG_BYTES_IN_ADDRESS) as usize);
self.side_metadata_access::<false, usize, _, _, _>(
data_addr,
None,
|| {
let meta_addr = address_to_meta_address(self, data_addr);
let aligned_meta_addr = meta_addr.align_down(BYTES_IN_ADDRESS);
aligned_meta_addr.load::<usize>()
},
|_| {},
)
}
/// Stores the new value into the side metadata for the gien address if the current value is the same as the old value.
/// This method has similar semantics to `compare_exchange` in Rust atomics.
/// The return value is a result indicating whether the new value was written and containing the previous value.
/// On success this value is guaranteed to be equal to current.
pub fn compare_exchange_atomic<T: MetadataValue>(
&self,
data_addr: Address,
old_metadata: T,
new_metadata: T,
success_order: Ordering,
failure_order: Ordering,
) -> std::result::Result<T, T> {
self.side_metadata_access::<true, T, _, _, _>(
data_addr,
Some(new_metadata),
|| {
let meta_addr = address_to_meta_address(self, data_addr);
let bits_num_log = self.log_num_of_bits;
if bits_num_log < 3 {
let lshift = meta_byte_lshift(self, data_addr);
let mask = meta_byte_mask(self) << lshift;
let real_old_byte = unsafe { meta_addr.atomic_load::<AtomicU8>(success_order) };
let expected_old_byte =
(real_old_byte & !mask) | ((old_metadata.to_u8().unwrap()) << lshift);
let expected_new_byte =
(expected_old_byte & !mask) | ((new_metadata.to_u8().unwrap()) << lshift);
unsafe {
meta_addr.compare_exchange::<AtomicU8>(
expected_old_byte,
expected_new_byte,
success_order,
failure_order,
)
}
.map(|x| FromPrimitive::from_u8((x & mask) >> lshift).unwrap())
.map_err(|x| FromPrimitive::from_u8((x & mask) >> lshift).unwrap())
} else {
unsafe {
T::compare_exchange(
meta_addr,
old_metadata,
new_metadata,
success_order,
failure_order,
)
}
}
},
|_res| {
#[cfg(feature = "extreme_assertions")]
if _res.is_ok() {
sanity::verify_store(self, data_addr, new_metadata);
}
},
)
}
/// This is used to implement fetch_add/sub for bits.
/// For fetch_and/or, we don't necessarily need this method. We could directly do fetch_and/or on the u8.
fn fetch_ops_on_bits<F: Fn(u8) -> u8>(
&self,
data_addr: Address,
meta_addr: Address,
set_order: Ordering,
fetch_order: Ordering,
update: F,
) -> u8 {
let lshift = meta_byte_lshift(self, data_addr);
let mask = meta_byte_mask(self) << lshift;
let old_raw_byte = unsafe {
<u8 as MetadataValue>::fetch_update(
meta_addr,
set_order,
fetch_order,
|raw_byte: u8| {
let old_val = (raw_byte & mask) >> lshift;
let new_val = update(old_val);
let new_raw_byte = (raw_byte & !mask) | ((new_val << lshift) & mask);
Some(new_raw_byte)
},
)
}
.unwrap();
(old_raw_byte & mask) >> lshift
}
/// Adds the value to the current value for this side metadata for the given address.
/// This method has similar semantics to `fetch_add` in Rust atomics.
/// Returns the previous value.
pub fn fetch_add_atomic<T: MetadataValue>(
&self,
data_addr: Address,
val: T,
order: Ordering,
) -> T {
self.side_metadata_access::<true, T, _, _, _>(
data_addr,
Some(val),
|| {
let meta_addr = address_to_meta_address(self, data_addr);
let bits_num_log = self.log_num_of_bits;
if bits_num_log < 3 {
FromPrimitive::from_u8(self.fetch_ops_on_bits(
data_addr,
meta_addr,
order,
order,
|x: u8| x.wrapping_add(val.to_u8().unwrap()),
))
.unwrap()
} else {
unsafe { T::fetch_add(meta_addr, val, order) }
}
},
|_old_val| {
#[cfg(feature = "extreme_assertions")]
sanity::verify_update::<T>(self, data_addr, _old_val, _old_val.wrapping_add(&val))
},
)
}
/// Subtracts the value from the current value for this side metadata for the given address.
/// This method has similar semantics to `fetch_sub` in Rust atomics.
/// Returns the previous value.
pub fn fetch_sub_atomic<T: MetadataValue>(
&self,
data_addr: Address,
val: T,
order: Ordering,
) -> T {
self.side_metadata_access::<true, T, _, _, _>(
data_addr,
Some(val),
|| {
let meta_addr = address_to_meta_address(self, data_addr);
if self.log_num_of_bits < 3 {
FromPrimitive::from_u8(self.fetch_ops_on_bits(
data_addr,
meta_addr,
order,
order,
|x: u8| x.wrapping_sub(val.to_u8().unwrap()),
))
.unwrap()
} else {
unsafe { T::fetch_sub(meta_addr, val, order) }
}
},
|_old_val| {
#[cfg(feature = "extreme_assertions")]
sanity::verify_update::<T>(self, data_addr, _old_val, _old_val.wrapping_sub(&val))
},
)
}
/// Bitwise 'and' the value with the current value for this side metadata for the given address.
/// This method has similar semantics to `fetch_and` in Rust atomics.
/// Returns the previous value.
pub fn fetch_and_atomic<T: MetadataValue>(
&self,
data_addr: Address,
val: T,
order: Ordering,
) -> T {
self.side_metadata_access::<true, T, _, _, _>(
data_addr,
Some(val),
|| {
let meta_addr = address_to_meta_address(self, data_addr);
if self.log_num_of_bits < 3 {
let lshift = meta_byte_lshift(self, data_addr);
let mask = meta_byte_mask(self) << lshift;
// We do not need to use fetch_ops_on_bits(), we can just set irrelavent bits to 1, and do fetch_and
let rhs = (val.to_u8().unwrap() << lshift) | !mask;
let old_raw_byte =
unsafe { <u8 as MetadataValue>::fetch_and(meta_addr, rhs, order) };
let old_val = (old_raw_byte & mask) >> lshift;
FromPrimitive::from_u8(old_val).unwrap()
} else {
unsafe { T::fetch_and(meta_addr, val, order) }
}
},
|_old_val| {
#[cfg(feature = "extreme_assertions")]
sanity::verify_update::<T>(self, data_addr, _old_val, _old_val.bitand(val))
},
)
}
/// Bitwise 'or' the value with the current value for this side metadata for the given address.
/// This method has similar semantics to `fetch_or` in Rust atomics.
/// Returns the previous value.
pub fn fetch_or_atomic<T: MetadataValue>(
&self,
data_addr: Address,
val: T,
order: Ordering,
) -> T {
self.side_metadata_access::<true, T, _, _, _>(
data_addr,
Some(val),
|| {
let meta_addr = address_to_meta_address(self, data_addr);
if self.log_num_of_bits < 3 {
let lshift = meta_byte_lshift(self, data_addr);
let mask = meta_byte_mask(self) << lshift;
// We do not need to use fetch_ops_on_bits(), we can just set irrelavent bits to 0, and do fetch_or
let rhs = (val.to_u8().unwrap() << lshift) & mask;
let old_raw_byte =
unsafe { <u8 as MetadataValue>::fetch_or(meta_addr, rhs, order) };
let old_val = (old_raw_byte & mask) >> lshift;
FromPrimitive::from_u8(old_val).unwrap()
} else {
unsafe { T::fetch_or(meta_addr, val, order) }
}
},
|_old_val| {
#[cfg(feature = "extreme_assertions")]
sanity::verify_update::<T>(self, data_addr, _old_val, _old_val.bitor(val))
},
)
}
/// Fetches the value for this side metadata for the given address, and applies a function to it that returns an optional new value.
/// This method has similar semantics to `fetch_update` in Rust atomics.
/// Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).
pub fn fetch_update_atomic<T: MetadataValue, F: FnMut(T) -> Option<T> + Copy>(
&self,
data_addr: Address,
set_order: Ordering,
fetch_order: Ordering,
mut f: F,
) -> std::result::Result<T, T> {
self.side_metadata_access::<true, T, _, _, _>(
data_addr,
None,
move || -> std::result::Result<T, T> {
let meta_addr = address_to_meta_address(self, data_addr);
if self.log_num_of_bits < 3 {
let lshift = meta_byte_lshift(self, data_addr);
let mask = meta_byte_mask(self) << lshift;
unsafe {
<u8 as MetadataValue>::fetch_update(
meta_addr,
set_order,
fetch_order,
|raw_byte: u8| {
let old_val = (raw_byte & mask) >> lshift;
f(FromPrimitive::from_u8(old_val).unwrap()).map(|new_val| {
(raw_byte & !mask)
| ((new_val.to_u8().unwrap() << lshift) & mask)
})
},
)
}
.map(|x| FromPrimitive::from_u8((x & mask) >> lshift).unwrap())
.map_err(|x| FromPrimitive::from_u8((x & mask) >> lshift).unwrap())
} else {
unsafe { T::fetch_update(meta_addr, set_order, fetch_order, f) }
}
},
|_result| {
#[cfg(feature = "extreme_assertions")]
if let Ok(old_val) = _result {
sanity::verify_update::<T>(self, data_addr, old_val, f(old_val).unwrap())
}
},
)
}
/// Search for a data address that has a non zero value in the side metadata. The search starts from the given data address (including this address),
/// and iterates backwards for the given bytes (non inclusive) before the data address.
///
/// The data_addr and the corresponding side metadata address may not be mapped. Thus when this function checks the given data address, and
/// when it searches back, it needs to check if the address is mapped or not to avoid loading from an unmapped address.
///
/// This function returns an address that is aligned to the region of this side metadata (`log_bytes_per_region`), and the side metadata
/// for the address is non zero.
///
/// # Safety
///
/// This function uses non-atomic load for the side metadata. The user needs to make sure
/// that there is no other thread that is mutating the side metadata.
#[allow(clippy::let_and_return)]
pub unsafe fn find_prev_non_zero_value<T: MetadataValue>(
&self,
data_addr: Address,
search_limit_bytes: usize,
) -> Option<Address> {
debug_assert!(search_limit_bytes > 0);
if self.uses_contiguous_side_metadata() {
// Contiguous side metadata
let result = self.find_prev_non_zero_value_fast::<T>(data_addr, search_limit_bytes);
#[cfg(debug_assertions)]
{
// Double check if the implementation is correct
let result2 =
self.find_prev_non_zero_value_simple::<T>(data_addr, search_limit_bytes);
assert_eq!(result, result2, "find_prev_non_zero_value_fast returned a diffrent result from the naive implementation.");
}
result
} else {
// TODO: We should be able to optimize further for this case. However, we need to be careful that the side metadata
// is not contiguous, and we need to skip to the next chunk's side metadata when we search to a different chunk.
// This won't be used for VO bit, as VO bit is global and is always contiguous. So for now, I am not bothered to do it.
warn!("We are trying to search non zero bits in an discontiguous side metadata. The performance is slow, as MMTk does not optimize for this case.");
self.find_prev_non_zero_value_simple::<T>(data_addr, search_limit_bytes)
}
}
fn find_prev_non_zero_value_simple<T: MetadataValue>(
&self,
data_addr: Address,
search_limit_bytes: usize,
) -> Option<Address> {
let region_bytes = 1 << self.log_bytes_in_region;
// Figure out the range that we need to search.
let start_addr = data_addr.align_down(region_bytes);
let end_addr = data_addr.saturating_sub(search_limit_bytes) + 1usize;
let mut cursor = start_addr;
while cursor >= end_addr {
// We encounter an unmapped address. Just return None.
if !cursor.is_mapped() {
return None;
}
// If we find non-zero value, just return it.
if !unsafe { self.load::<T>(cursor).is_zero() } {
return Some(cursor);
}
cursor -= region_bytes;
}
None
}
#[allow(clippy::let_and_return)]
fn find_prev_non_zero_value_fast<T: MetadataValue>(
&self,
data_addr: Address,
search_limit_bytes: usize,
) -> Option<Address> {
debug_assert!(self.uses_contiguous_side_metadata());
// Quick check if the data address is mapped at all.
if !data_addr.is_mapped() {
return None;
}
// Quick check if the current data_addr has a non zero value.
if !unsafe { self.load::<T>(data_addr).is_zero() } {
return Some(data_addr.align_down(1 << self.log_bytes_in_region));
}
// Figure out the start and end data address.
let start_addr = data_addr.saturating_sub(search_limit_bytes) + 1usize;
let end_addr = data_addr;
// Then figure out the start and end metadata address and bits.
// The start bit may not be accurate, as we map any address in the region to the same bit.
// We will filter the result at the end to make sure the found address is in the search range.
let start_meta_addr = address_to_contiguous_meta_address(self, start_addr);
let start_meta_shift = meta_byte_lshift(self, start_addr);
let end_meta_addr = address_to_contiguous_meta_address(self, end_addr);
let end_meta_shift = meta_byte_lshift(self, end_addr);
let mut res = None;
let mut visitor = |range: BitByteRange| {
match range {
BitByteRange::Bytes { start, end } => {
match helpers::find_last_non_zero_bit_in_metadata_bytes(start, end) {
helpers::FindMetaBitResult::Found { addr, bit } => {
let (addr, bit) = align_metadata_address(self, addr, bit);
res = Some(contiguous_meta_address_to_address(self, addr, bit));
// Return true to abort the search. We found the bit.
true
}
// If we see unmapped metadata, we don't need to search any more.
helpers::FindMetaBitResult::UnmappedMetadata => true,
// Return false to continue searching.
helpers::FindMetaBitResult::NotFound => false,
}
}
BitByteRange::BitsInByte {
addr,
bit_start,
bit_end,
} => {
match helpers::find_last_non_zero_bit_in_metadata_bits(addr, bit_start, bit_end)
{
helpers::FindMetaBitResult::Found { addr, bit } => {
let (addr, bit) = align_metadata_address(self, addr, bit);
res = Some(contiguous_meta_address_to_address(self, addr, bit));
// Return true to abort the search. We found the bit.
true
}
// If we see unmapped metadata, we don't need to search any more.
helpers::FindMetaBitResult::UnmappedMetadata => true,
// Return false to continue searching.
helpers::FindMetaBitResult::NotFound => false,
}
}
}
};
ranges::break_bit_range(
start_meta_addr,
start_meta_shift,
end_meta_addr,
end_meta_shift,
false,
&mut visitor,
);
// We have to filter the result. We search between [start_addr, end_addr). But we actually
// search with metadata bits. It is possible the metadata bit for start_addr is the same bit
// as an address that is before start_addr. E.g. 0x2010f026360 and 0x2010f026361 are mapped
// to the same bit, 0x2010f026361 is the start address and 0x2010f026360 is outside the search range.
res.map(|addr| addr.align_down(1 << self.log_bytes_in_region))
.filter(|addr| *addr >= start_addr && *addr < end_addr)
}
/// Search for data addresses that have non zero values in the side metadata. This method is
/// primarily used for heap traversal by scanning the VO bits.
///
/// This function searches the side metadata for the data address range from `data_start_addr`
/// (inclusive) to `data_end_addr` (exclusive). The data address range must be fully mapped.
///
/// For each data region that has non-zero side metadata, `visit_data` is called with the lowest
/// address of that region. Note that it may not be the original address used to set the
/// metadata bits.
pub fn scan_non_zero_values<T: MetadataValue>(
&self,
data_start_addr: Address,
data_end_addr: Address,
visit_data: &mut impl FnMut(Address),
) {
if self.uses_contiguous_side_metadata() && self.log_num_of_bits == 0 {
// Contiguous one-bit-per-region side metadata
// TODO: VO bits is one-bit-per-word. But if we want to scan other metadata (such as
// the forwarding bits which has two bits per word), we will need to refactor the
// algorithm of `scan_non_zero_values_fast`.
self.scan_non_zero_values_fast(data_start_addr, data_end_addr, visit_data);
} else {
// TODO: VO bits are always contiguous. But if we want to scan other metadata, such as
// side mark bits, we need to refactor `bulk_update_metadata` to support `FnMut`, too,
// and use it to apply `scan_non_zero_values_fast` on each contiguous side metadata
// range.
warn!(
"We are trying to search for non zero bits in a discontiguous side metadata \
or the metadata has more than one bit per region. \
The performance is slow, as MMTk does not optimize for this case."
);
self.scan_non_zero_values_simple::<T>(data_start_addr, data_end_addr, visit_data);
}
}
fn scan_non_zero_values_simple<T: MetadataValue>(
&self,
data_start_addr: Address,
data_end_addr: Address,
visit_data: &mut impl FnMut(Address),
) {
let region_bytes = 1usize << self.log_bytes_in_region;
let mut cursor = data_start_addr;
while cursor < data_end_addr {
debug_assert!(cursor.is_mapped());
// If we find non-zero value, just call back.
if !unsafe { self.load::<T>(cursor).is_zero() } {
visit_data(cursor);
}
cursor += region_bytes;
}
}
fn scan_non_zero_values_fast(
&self,
data_start_addr: Address,
data_end_addr: Address,
visit_data: &mut impl FnMut(Address),
) {
debug_assert!(self.uses_contiguous_side_metadata());
debug_assert_eq!(self.log_num_of_bits, 0);
// Then figure out the start and end metadata address and bits.
let start_meta_addr = address_to_contiguous_meta_address(self, data_start_addr);
let start_meta_shift = meta_byte_lshift(self, data_start_addr);
let end_meta_addr = address_to_contiguous_meta_address(self, data_end_addr);
let end_meta_shift = meta_byte_lshift(self, data_end_addr);
let mut visitor = |range| {
match range {
BitByteRange::Bytes { start, end } => {
helpers::scan_non_zero_bits_in_metadata_bytes(start, end, &mut |addr, bit| {
visit_data(helpers::contiguous_meta_address_to_address(self, addr, bit));
});
}
BitByteRange::BitsInByte {
addr,
bit_start,
bit_end,
} => helpers::scan_non_zero_bits_in_metadata_bits(
addr,
bit_start,
bit_end,
&mut |addr, bit| {
visit_data(helpers::contiguous_meta_address_to_address(self, addr, bit));
},
),
}
false
};
ranges::break_bit_range(
start_meta_addr,
start_meta_shift,
end_meta_addr,
end_meta_shift,
false,
&mut visitor,
);
}
}
impl fmt::Debug for SideMetadataSpec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"SideMetadataSpec {} {{ \
**is_global: {:?} \
**offset: {} \
**log_num_of_bits: 0x{:x} \
**log_bytes_in_region: 0x{:x} \
}}",
self.name,
self.is_global,
unsafe {
if self.is_absolute_offset() {
format!("0x{:x}", self.offset.addr)
} else {
format!("0x{:x}", self.offset.rel_offset)
}
},
self.log_num_of_bits,
self.log_bytes_in_region
))
}
}
/// A union of Address or relative offset (usize) used to store offset for a side metadata spec.
/// If a spec is contiguous side metadata, it uses address. Othrewise it uses usize.
// The fields are made private on purpose. They can only be accessed from SideMetadata which knows whether it is Address or usize.
#[derive(Clone, Copy)]
pub union SideMetadataOffset {
addr: Address,
rel_offset: usize,
}
impl SideMetadataOffset {
/// Get an offset for a fixed address. This is usually used to set offset for the first spec (subsequent ones can be laid out with `layout_after`).
pub const fn addr(addr: Address) -> Self {
SideMetadataOffset { addr }
}
/// Get an offset for a relative offset (usize). This is usually used to set offset for the first spec (subsequent ones can be laid out with `layout_after`).
pub const fn rel(rel_offset: usize) -> Self {
SideMetadataOffset { rel_offset }
}
/// Get an offset after a spec. This is used to layout another spec immediately after this one.
pub const fn layout_after(spec: &SideMetadataSpec) -> SideMetadataOffset {
// Some metadata may be so small that its size is not a multiple of byte size. One example
// is `CHUNK_MARK`. It is one byte per chunk. However, on 32-bit architectures, we
// allocate side metadata per chunk. In that case, it will only occupy one byte. If we
// do not align the upper bound offset up, subsequent local metadata that need to be
// accessed at, for example, word granularity will be misaligned.
// TODO: Currently we align metadata to word size so that it is safe to access the metadata
// one word at a time. In the future, we may allow each metadata to specify its own
// alignment requirement.
let upper_bound_offset = spec.upper_bound_offset();
if spec.is_absolute_offset() {
let addr = unsafe { upper_bound_offset.addr };
let aligned_addr = addr.align_up(BYTES_IN_WORD);
SideMetadataOffset::addr(aligned_addr)
} else {
let rel_offset = unsafe { upper_bound_offset.rel_offset };
let aligned_rel_offset = raw_align_up(rel_offset, BYTES_IN_WORD);
SideMetadataOffset::rel(aligned_rel_offset)
}
}
}
// Address and usize has the same layout, so we use usize for implementing these traits.
impl PartialEq for SideMetadataOffset {
fn eq(&self, other: &Self) -> bool {
unsafe { self.rel_offset == other.rel_offset }
}
}
impl Eq for SideMetadataOffset {}
impl std::hash::Hash for SideMetadataOffset {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
unsafe { self.rel_offset }.hash(state);
}
}
/// This struct stores all the side metadata specs for a policy. Generally a policy needs to know its own
/// side metadata spec as well as the plan's specs.
pub(crate) struct SideMetadataContext {
// For plans
pub global: Vec<SideMetadataSpec>,
// For policies
pub local: Vec<SideMetadataSpec>,
}
impl SideMetadataContext {
#[allow(clippy::vec_init_then_push)] // allow this, as we conditionally push based on features.
pub fn new_global_specs(specs: &[SideMetadataSpec]) -> Vec<SideMetadataSpec> {
let mut ret = vec![];
#[cfg(feature = "vo_bit")]
ret.push(VO_BIT_SIDE_METADATA_SPEC);
if let Some(spec) = crate::mmtk::SFT_MAP.get_side_metadata() {
if spec.is_global {
ret.push(*spec);
}
}
ret.extend_from_slice(specs);
ret
}
pub fn get_local_specs(&self) -> &[SideMetadataSpec] {
&self.local
}
/// Return the pages reserved for side metadata based on the data pages we used.
// We used to use PageAccouting to count pages used in side metadata. However,
// that means we always count pages while we may reserve less than a page each time.
// This could lead to overcount. I think the easier way is to not account
// when we allocate for sidemetadata, but to calculate the side metadata usage based on
// how many data pages we use when reporting.
pub fn calculate_reserved_pages(&self, data_pages: usize) -> usize {
let mut total = 0;
for spec in self.global.iter() {
let rshift = addr_rshift(spec);
total += (data_pages + ((1 << rshift) - 1)) >> rshift;
}
for spec in self.local.iter() {
let rshift = addr_rshift(spec);
total += (data_pages + ((1 << rshift) - 1)) >> rshift;
}
total
}
// ** NOTE: **
// Regardless of the number of bits in a metadata unit, we always represent its content as a word.
/// Tries to map the required metadata space and returns `true` is successful.
/// This can be called at page granularity.
pub fn try_map_metadata_space(
&self,
start: Address,
size: usize,
space_name: &str,
) -> Result<()> {
debug!(
"try_map_metadata_space({}, 0x{:x}, {}, {})",
start,
size,
self.global.len(),
self.local.len()
);
// Page aligned
debug_assert!(start.is_aligned_to(BYTES_IN_PAGE));
debug_assert!(size % BYTES_IN_PAGE == 0);
self.map_metadata_internal(start, size, false, space_name)
}
/// Tries to map the required metadata address range, without reserving swap-space/physical memory for it.
/// This will make sure the address range is exclusive to the caller. This should be called at chunk granularity.
///
/// NOTE: Accessing addresses in this range will produce a segmentation fault if swap-space is not mapped using the `try_map_metadata_space` function.
pub fn try_map_metadata_address_range(
&self,
start: Address,
size: usize,
name: &str,
) -> Result<()> {
debug!(
"try_map_metadata_address_range({}, 0x{:x}, {}, {})",
start,
size,
self.global.len(),
self.local.len()
);
// Chunk aligned
debug_assert!(start.is_aligned_to(BYTES_IN_CHUNK));
debug_assert!(size % BYTES_IN_CHUNK == 0);
self.map_metadata_internal(start, size, true, name)
}
/// The internal function to mmap metadata
///
/// # Arguments
/// * `start` - The starting address of the source data.
/// * `size` - The size of the source data (in bytes).
/// * `no_reserve` - whether to invoke mmap with a noreserve flag (we use this flag to quarantine address range)
/// * `space_name`: The name of the space, used for annotating the mmap.
fn map_metadata_internal(
&self,
start: Address,
size: usize,
no_reserve: bool,
space_name: &str,
) -> Result<()> {
for spec in self.global.iter() {
let anno = MmapAnnotation::SideMeta {
space: space_name,
meta: spec.name,
};
match try_mmap_contiguous_metadata_space(start, size, spec, no_reserve, &anno) {
Ok(_) => {}
Err(e) => return Result::Err(e),
}
}
#[cfg(target_pointer_width = "32")]
let mut lsize: usize = 0;
for spec in self.local.iter() {
// For local side metadata, we always have to reserve address space for all local
// metadata required by all policies in MMTk to be able to calculate a constant offset
// for each local metadata at compile-time (it's like assigning an ID to each policy).
//
// As the plan is chosen at run-time, we will never know which subset of policies will
// be used during run-time. We can't afford this much address space in 32-bits.
// So, we switch to the chunk-based approach for this specific case.
//
// The global metadata is different in that for each plan, we can calculate its constant
// base addresses at compile-time. Using the chunk-based approach will need the same
// address space size as the current not-chunked approach.
#[cfg(target_pointer_width = "64")]
{
let anno = MmapAnnotation::SideMeta {
space: space_name,
meta: spec.name,
};
match try_mmap_contiguous_metadata_space(start, size, spec, no_reserve, &anno) {
Ok(_) => {}
Err(e) => return Result::Err(e),
}
}
#[cfg(target_pointer_width = "32")]
{
lsize += metadata_bytes_per_chunk(spec.log_bytes_in_region, spec.log_num_of_bits);
}
}
#[cfg(target_pointer_width = "32")]
if lsize > 0 {
let max = BYTES_IN_CHUNK >> super::constants::LOG_LOCAL_SIDE_METADATA_WORST_CASE_RATIO;
debug_assert!(
lsize <= max,
"local side metadata per chunk (0x{:x}) must be less than (0x{:x})",
lsize,
max
);
// We are creating a mmap for all side metadata instead of one specific metadata. We
// just annotate it as "all" here.
let anno = MmapAnnotation::SideMeta {
space: space_name,
meta: "all",
};
match try_map_per_chunk_metadata_space(start, size, lsize, no_reserve, &anno) {
Ok(_) => {}
Err(e) => return Result::Err(e),
}
}
Ok(())
}
/// Unmap the corresponding metadata space or panic.
///
/// Note-1: This function is only used for test and debug right now.
///
/// Note-2: This function uses munmap() which works at page granularity.
/// If the corresponding metadata space's size is not a multiple of page size,
/// the actual unmapped space will be bigger than what you specify.
#[cfg(test)]
pub fn ensure_unmap_metadata_space(&self, start: Address, size: usize) {
trace!("ensure_unmap_metadata_space({}, 0x{:x})", start, size);
debug_assert!(start.is_aligned_to(BYTES_IN_PAGE));
debug_assert!(size % BYTES_IN_PAGE == 0);
for spec in self.global.iter() {
ensure_munmap_contiguous_metadata_space(start, size, spec);
}
for spec in self.local.iter() {
#[cfg(target_pointer_width = "64")]
{
ensure_munmap_contiguous_metadata_space(start, size, spec);
}
#[cfg(target_pointer_width = "32")]
{
ensure_munmap_chunked_metadata_space(start, size, spec);
}
}
}
}
/// A byte array in side-metadata
pub struct MetadataByteArrayRef<const ENTRIES: usize> {
#[cfg(feature = "extreme_assertions")]
heap_range_start: Address,
#[cfg(feature = "extreme_assertions")]
spec: SideMetadataSpec,
data: &'static [u8; ENTRIES],
}
impl<const ENTRIES: usize> MetadataByteArrayRef<ENTRIES> {
/// Get a piece of metadata address range as a byte array.
///
/// # Arguments
///
/// * `metadata_spec` - The specification of the target side metadata.
/// * `start` - The starting address of the heap range.
/// * `bytes` - The size of the heap range.
///
pub fn new(metadata_spec: &SideMetadataSpec, start: Address, bytes: usize) -> Self {
debug_assert_eq!(
metadata_spec.log_num_of_bits, LOG_BITS_IN_BYTE as usize,
"Each heap entry should map to a byte in side-metadata"
);
debug_assert_eq!(
bytes >> metadata_spec.log_bytes_in_region,
ENTRIES,
"Heap range size and MetadataByteArray size does not match"
);
Self {
#[cfg(feature = "extreme_assertions")]
heap_range_start: start,
#[cfg(feature = "extreme_assertions")]
spec: *metadata_spec,
// # Safety
// The metadata memory is assumed to be mapped when accessing.
data: unsafe { &*address_to_meta_address(metadata_spec, start).to_ptr() },
}
}
/// Get the length of the array.
#[allow(clippy::len_without_is_empty)]
pub const fn len(&self) -> usize {
ENTRIES
}
/// Get a byte from the metadata byte array at the given index.
#[allow(clippy::let_and_return)]
pub fn get(&self, index: usize) -> u8 {
#[cfg(feature = "extreme_assertions")]
let _lock = sanity::SANITY_LOCK.lock().unwrap();
let value = self.data[index];
#[cfg(feature = "extreme_assertions")]
{
let data_addr = self.heap_range_start + (index << self.spec.log_bytes_in_region);
sanity::verify_load::<u8>(&self.spec, data_addr, value);
}
value
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mmap_anno_test;
use crate::util::metadata::side_metadata::SideMetadataContext;
// offset is not used in these tests.
pub const ZERO_OFFSET: SideMetadataOffset = SideMetadataOffset { rel_offset: 0 };
#[test]
fn calculate_reserved_pages_one_spec() {
// 1 bit per 8 bytes - 1:64
let spec = SideMetadataSpec {
name: "test_spec",
is_global: true,
offset: ZERO_OFFSET,
log_num_of_bits: 0,
log_bytes_in_region: 3,
};
let side_metadata = SideMetadataContext {
global: vec![spec],
local: vec![],
};
assert_eq!(side_metadata.calculate_reserved_pages(0), 0);
assert_eq!(side_metadata.calculate_reserved_pages(63), 1);
assert_eq!(side_metadata.calculate_reserved_pages(64), 1);
assert_eq!(side_metadata.calculate_reserved_pages(65), 2);
assert_eq!(side_metadata.calculate_reserved_pages(1024), 16);
}
#[test]
fn calculate_reserved_pages_multi_specs() {
// 1 bit per 8 bytes - 1:64
let gspec = SideMetadataSpec {
name: "gspec",
is_global: true,
offset: ZERO_OFFSET,
log_num_of_bits: 0,
log_bytes_in_region: 3,
};
// 2 bits per page - 2 / (4k * 8) = 1:16k
let lspec = SideMetadataSpec {
name: "lspec",
is_global: false,
offset: ZERO_OFFSET,
log_num_of_bits: 1,
log_bytes_in_region: 12,
};
let side_metadata = SideMetadataContext {
global: vec![gspec],
local: vec![lspec],
};
assert_eq!(side_metadata.calculate_reserved_pages(1024), 16 + 1);
}
use crate::util::heap::layout::vm_layout;
use crate::util::test_util::{serial_test, with_cleanup};
use memory::MmapStrategy;
use paste::paste;
const TEST_LOG_BYTES_IN_REGION: usize = 12;
fn test_side_metadata(
log_bits: usize,
f: impl Fn(&SideMetadataSpec, Address, Address) + std::panic::RefUnwindSafe,
) {
serial_test(|| {
let spec = SideMetadataSpec {
name: "Test Spec $tname",
is_global: true,
offset: SideMetadataOffset::addr(GLOBAL_SIDE_METADATA_BASE_ADDRESS),
log_num_of_bits: log_bits,
log_bytes_in_region: TEST_LOG_BYTES_IN_REGION, // page size
};
let context = SideMetadataContext {
global: vec![spec],
local: vec![],
};
let mut sanity = SideMetadataSanity::new();
sanity.verify_metadata_context("TestPolicy", &context);
let data_addr = vm_layout::vm_layout().heap_start;
// Make sure the address is mapped.
crate::MMAPPER
.ensure_mapped(data_addr, 1, MmapStrategy::TEST, mmap_anno_test!())
.unwrap();
let meta_addr = address_to_meta_address(&spec, data_addr);
with_cleanup(
|| {
let mmap_result =
context.try_map_metadata_space(data_addr, BYTES_IN_PAGE, "test_space");
assert!(mmap_result.is_ok());
f(&spec, data_addr, meta_addr);
},
|| {
// Clear the metadata -- use u64 (max length we support)
assert!(log_bits <= 6);
let meta_ptr: *mut u64 = meta_addr.to_mut_ptr();
unsafe { *meta_ptr = 0 };
sanity::reset();
},
)
})
}
fn max_value(log_bits: usize) -> u64 {
(0..(1 << log_bits)).fold(0, |accum, x| accum + (1 << x))
}
#[test]
fn test_max_value() {
assert_eq!(max_value(0), 1);
assert_eq!(max_value(1), 0b11);
assert_eq!(max_value(2), 0b1111);
assert_eq!(max_value(3), 255);
assert_eq!(max_value(4), 65535);
}
macro_rules! test_side_metadata_access {
($tname: ident, $type: ty, $log_bits: expr) => {
paste!{
#[test]
fn [<$tname _load>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
// Initial value should be 0
assert_eq!(unsafe { spec.load::<$type>(data_addr) }, 0);
assert_eq!(spec.load_atomic::<$type>(data_addr, Ordering::SeqCst), 0);
// Set to max
let max_value: $type = max_value($log_bits) as _;
unsafe { spec.store::<$type>(data_addr, max_value); }
assert_eq!(unsafe { spec.load::<$type>(data_addr) }, max_value);
assert_eq!(spec.load_atomic::<$type>(data_addr, Ordering::SeqCst), max_value);
assert_eq!(unsafe { *meta_ptr }, max_value);
});
}
#[test]
fn [<$tname _store>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store 0 to the side metadata
unsafe { spec.store::<$type>(data_addr, 0); }
assert_eq!(unsafe { spec.load::<$type>(data_addr) }, 0);
// Only the affected bits are set to 0
assert_eq!(unsafe { *meta_ptr }, <$type>::MAX & (!max_value));
});
}
#[test]
fn [<$tname _atomic_store>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store 0 to the side metadata
spec.store_atomic::<$type>(data_addr, 0, Ordering::SeqCst);
assert_eq!(unsafe { spec.load::<$type>(data_addr) }, 0);
// Only the affected bits are set to 0
assert_eq!(unsafe { *meta_ptr }, <$type>::MAX & (!max_value));
});
}
#[test]
fn [<$tname _compare_exchange_success>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store 1 to the side metadata
spec.store_atomic::<$type>(data_addr, 1, Ordering::SeqCst);
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
assert_eq!(old_val, 1);
let new_val = 0;
let res = spec.compare_exchange_atomic::<$type>(data_addr, old_val, new_val, Ordering::SeqCst, Ordering::SeqCst);
assert!(res.is_ok());
assert_eq!(res.unwrap(), old_val, "old vals do not match");
let after_update = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
assert_eq!(after_update, new_val);
// Only the affected bits are set to 0
assert_eq!(unsafe { *meta_ptr }, <$type>::MAX & (!max_value));
});
}
#[test]
fn [<$tname _compare_exchange_fail>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store 1 to the side metadata
spec.store_atomic::<$type>(data_addr, 1, Ordering::SeqCst);
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
assert_eq!(old_val, 1);
// make old_val outdated
spec.store_atomic::<$type>(data_addr, 0, Ordering::SeqCst);
let bits_before_cas = unsafe { *meta_ptr };
let new_val = 0;
let res = spec.compare_exchange_atomic::<$type>(data_addr, old_val, new_val, Ordering::SeqCst, Ordering::SeqCst);
assert!(res.is_err());
assert_eq!(res.err().unwrap(), 0);
let bits_after_cas = unsafe { *meta_ptr };
assert_eq!(bits_before_cas, bits_after_cas);
});
}
#[test]
fn [<$tname _fetch_add_1>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store 0 to the side metadata
spec.store_atomic::<$type>(data_addr, 0, Ordering::SeqCst);
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
let old_val_from_fetch = spec.fetch_add_atomic::<$type>(data_addr, 1, Ordering::SeqCst);
assert_eq!(old_val_from_fetch, old_val);
let new_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
assert_eq!(new_val, 1);
});
}
#[test]
fn [<$tname _fetch_add_max>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store 0 to the side metadata
spec.store_atomic::<$type>(data_addr, 0, Ordering::SeqCst);
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
let old_val_from_fetch = spec.fetch_add_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
assert_eq!(old_val_from_fetch, old_val);
let new_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
assert_eq!(new_val, max_value);
});
}
#[test]
fn [<$tname _fetch_add_overflow>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store max to the side metadata
spec.store_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
// add 1 to max value will cause overflow and wrap around to 0
let old_val_from_fetch = spec.fetch_add_atomic::<$type>(data_addr, 1, Ordering::SeqCst);
assert_eq!(old_val_from_fetch, old_val);
let new_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
assert_eq!(new_val, 0);
});
}
#[test]
fn [<$tname _fetch_sub_1>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store 1 to the side metadata
spec.store_atomic::<$type>(data_addr, 1, Ordering::SeqCst);
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
let old_val_from_fetch = spec.fetch_sub_atomic::<$type>(data_addr, 1, Ordering::SeqCst);
assert_eq!(old_val_from_fetch, old_val);
let new_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
assert_eq!(new_val, 0);
});
}
#[test]
fn [<$tname _fetch_sub_max>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store max to the side metadata
spec.store_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
let old_val_from_fetch = spec.fetch_sub_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
assert_eq!(old_val_from_fetch, old_val);
let new_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
assert_eq!(new_val, 0);
});
}
#[test]
fn [<$tname _fetch_sub_overflow>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store 0 to the side metadata
spec.store_atomic::<$type>(data_addr, 0, Ordering::SeqCst);
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
// sub 1 from 0 will cause overflow, and wrap around to max
let old_val_from_fetch = spec.fetch_sub_atomic::<$type>(data_addr, 1, Ordering::SeqCst);
assert_eq!(old_val_from_fetch, old_val);
let new_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
assert_eq!(new_val, max_value);
});
}
#[test]
fn [<$tname _fetch_and>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store all 1s to the side metadata
spec.store_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
// max and max should be max
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
let old_val_from_fetch = spec.fetch_and_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
assert_eq!(old_val_from_fetch, old_val, "old values do not match");
assert_eq!(spec.load_atomic::<$type>(data_addr, Ordering::SeqCst), max_value, "load values do not match");
assert_eq!(unsafe { *meta_ptr }, <$type>::MAX, "raw values do not match");
// max and last_bit_zero should last_bit_zero
let last_bit_zero = max_value - 1;
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
let old_val_from_fetch = spec.fetch_and_atomic::<$type>(data_addr, last_bit_zero, Ordering::SeqCst);
assert_eq!(old_val_from_fetch, old_val);
assert_eq!(spec.load_atomic::<$type>(data_addr, Ordering::SeqCst), last_bit_zero);
assert_eq!(unsafe { *meta_ptr }, <$type>::MAX - 1);
});
}
#[test]
fn [<$tname _fetch_or>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 0s
unsafe { *meta_ptr = 0; }
// Store 0 to the side metadata
spec.store_atomic::<$type>(data_addr, 0, Ordering::SeqCst);
// 0 or 0 should be 0
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
let old_val_from_fetch = spec.fetch_or_atomic::<$type>(data_addr, 0, Ordering::SeqCst);
assert_eq!(old_val_from_fetch, old_val);
assert_eq!(spec.load_atomic::<$type>(data_addr, Ordering::SeqCst), 0);
assert_eq!(unsafe { *meta_ptr }, 0);
// 0 and max should max
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
let old_val_from_fetch = spec.fetch_or_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
assert_eq!(old_val_from_fetch, old_val);
assert_eq!(spec.load_atomic::<$type>(data_addr, Ordering::SeqCst), max_value);
assert_eq!(unsafe { *meta_ptr }, max_value);
});
}
#[test]
fn [<$tname _fetch_update_success>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store all 1s to the side metadata
spec.store_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
// update from max to zero
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
let fetch_res = spec.fetch_update_atomic::<$type, _>(data_addr, Ordering::SeqCst, Ordering::SeqCst, |_x: $type| Some(0));
assert!(fetch_res.is_ok());
assert_eq!(fetch_res.unwrap(), old_val);
assert_eq!(spec.load_atomic::<$type>(data_addr, Ordering::SeqCst), 0);
// Only the affected bits are set to 0
assert_eq!(unsafe { *meta_ptr }, <$type>::MAX & (!max_value));
});
}
#[test]
fn [<$tname _fetch_update_fail>]() {
test_side_metadata($log_bits, |spec, data_addr, meta_addr| {
let meta_ptr: *mut $type = meta_addr.to_mut_ptr();
let max_value: $type = max_value($log_bits) as _;
// Set the metadata byte(s) to all 1s
unsafe { *meta_ptr = <$type>::MAX; }
// Store all 1s to the side metadata
spec.store_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
// update from max to zero
let old_val = spec.load_atomic::<$type>(data_addr, Ordering::SeqCst);
let fetch_res = spec.fetch_update_atomic::<$type, _>(data_addr, Ordering::SeqCst, Ordering::SeqCst, |_x: $type| None);
assert!(fetch_res.is_err());
assert_eq!(fetch_res.err().unwrap(), old_val);
assert_eq!(spec.load_atomic::<$type>(data_addr, Ordering::SeqCst), max_value);
// Only the affected bits are set to 0
assert_eq!(unsafe { *meta_ptr }, <$type>::MAX);
});
}
#[test]
fn [<$tname _find_prev_non_zero_value_easy>]() {
test_side_metadata($log_bits, |spec, data_addr, _meta_addr| {
let max_value: $type = max_value($log_bits) as _;
// Store non zero value at data_addr
spec.store_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
// Find the value starting from data_addr, at max 8 bytes.
// We should find data_addr
let res_addr = unsafe { spec.find_prev_non_zero_value::<$type>(data_addr, 8) };
assert!(res_addr.is_some());
assert_eq!(res_addr.unwrap(), data_addr);
});
}
#[test]
fn [<$tname _find_prev_non_zero_value_arbitrary_bytes>]() {
test_side_metadata($log_bits, |spec, data_addr, _meta_addr| {
let max_value: $type = max_value($log_bits) as _;
// Store non zero value at data_addr
spec.store_atomic::<$type>(data_addr, max_value, Ordering::SeqCst);
// Start from data_addr, we offset arbitrary length, and search back to find data_addr
let test_region = (1 << TEST_LOG_BYTES_IN_REGION);
for len in 1..(test_region*4) {
let start_addr = data_addr + len;
// Use len+1, as len is non inclusive.
let res_addr = unsafe { spec.find_prev_non_zero_value::<$type>(start_addr, len + 1) };
assert!(res_addr.is_some());
assert_eq!(res_addr.unwrap(), data_addr);
}
});
}
#[test]
fn [<$tname _find_prev_non_zero_value_arbitrary_start>]() {
test_side_metadata($log_bits, |spec, data_addr, _meta_addr| {
let max_value: $type = max_value($log_bits) as _;
// data_addr has a non-aligned offset
for offset in 0..7usize {
// Apply offset and test with the new data addr
let test_data_addr = data_addr + offset;
spec.store_atomic::<$type>(test_data_addr, max_value, Ordering::SeqCst);
// The return result should be aligned
let res_addr = unsafe { spec.find_prev_non_zero_value::<$type>(test_data_addr, 4096) };
assert!(res_addr.is_some());
assert_eq!(res_addr.unwrap(), data_addr);
// Clear whatever is set
spec.store_atomic::<$type>(test_data_addr, 0, Ordering::SeqCst);
}
});
}
#[test]
fn [<$tname _find_prev_non_zero_value_no_find>]() {
test_side_metadata($log_bits, |spec, data_addr, _meta_addr| {
// Store zero value at data_addr -- so we won't find anything
spec.store_atomic::<$type>(data_addr, 0, Ordering::SeqCst);
// Start from data_addr, we offset arbitrary length, and search back
let test_region = (1 << TEST_LOG_BYTES_IN_REGION);
for len in 1..(test_region*4) {
let start_addr = data_addr + len;
// Use len+1, as len is non inclusive.
let res_addr = unsafe { spec.find_prev_non_zero_value::<$type>(start_addr, len + 1) };
assert!(res_addr.is_none());
}
});
}
}
}
}
test_side_metadata_access!(test_u1, u8, 0);
test_side_metadata_access!(test_u2, u8, 1);
test_side_metadata_access!(test_u4, u8, 2);
test_side_metadata_access!(test_u8, u8, 3);
test_side_metadata_access!(test_u16, u16, 4);
test_side_metadata_access!(test_u32, u32, 5);
test_side_metadata_access!(test_u64, u64, 6);
test_side_metadata_access!(
test_usize,
usize,
if cfg!(target_pointer_width = "64") {
6
} else if cfg!(target_pointer_width = "32") {
5
} else {
unreachable!()
}
);
#[test]
fn test_bulk_update_meta_bits() {
let raw_mem =
unsafe { std::alloc::alloc_zeroed(std::alloc::Layout::from_size_align(8, 8).unwrap()) };
let addr = Address::from_mut_ptr(raw_mem);
SideMetadataSpec::set_meta_bits(addr, 0, addr, 4);
assert_eq!(unsafe { addr.load::<u64>() }, 0b1111);
SideMetadataSpec::zero_meta_bits(addr, 1, addr, 3);
assert_eq!(unsafe { addr.load::<u64>() }, 0b1001);
SideMetadataSpec::set_meta_bits(addr, 2, addr, 6);
assert_eq!(unsafe { addr.load::<u64>() }, 0b0011_1101);
SideMetadataSpec::zero_meta_bits(addr, 0, addr + 1usize, 0);
assert_eq!(unsafe { addr.load::<u64>() }, 0b0);
SideMetadataSpec::set_meta_bits(addr, 2, addr + 1usize, 2);
assert_eq!(unsafe { addr.load::<u64>() }, 0b11_1111_1100);
SideMetadataSpec::set_meta_bits(addr, 0, addr + 1usize, 2);
assert_eq!(unsafe { addr.load::<u64>() }, 0b11_1111_1111);
}
}