Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 128 additions & 23 deletions runtime/shims/xmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <malloc.h>
#include <psapi.h>

#include <algorithm>
#include <atomic>
#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -270,6 +271,9 @@ struct MappedPage {
uint64_t page;
std::shared_ptr<Section> section;
uint64_t offset;
uintptr_t view_base = 0;
uint64_t view_pages = 1;
DWORD view_protect = PAGE_READWRITE;
};

std::unordered_map<uintptr_t, MappedPage> g_mapped;
Expand Down Expand Up @@ -451,19 +455,101 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type,
return VirtualAlloc(address, size, host_type, host_prot);
}

// With g_phys_mutex held. Unmaps the page at `address` if one is mapped there; the address becomes a placeholder again.
bool unmap_page(uintptr_t address) {
// With g_phys_mutex held. Maps a contiguous run of `count` pages from `section` starting at `first_offset`.
bool map_view_run(uintptr_t start, uint64_t count, const std::shared_ptr<Section>& section, uint64_t first_offset,
const uint64_t* pages, DWORD protect) {
if (count == 0) return true;
void* const target = reinterpret_cast<void*>(start);
const SIZE_T total_size = static_cast<SIZE_T>(count) * kPhysicalPageSize;
if (!isolate_placeholder(target, total_size)) return false;
const FaultLockExclusive lock;
const auto it = g_mapped.find(address);
if (it == g_mapped.end()) return true;
if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(address),
MEM_PRESERVE_PLACEHOLDER)) {
return false;
void* const view = placeholder_api().map_view3(section->handle, GetCurrentProcess(), target, first_offset,
total_size, MEM_REPLACE_PLACEHOLDER, protect, nullptr, 0);
if (!view) return false;
g_mapped.reserve(g_mapped.size() + static_cast<std::size_t>(count));
for (uint64_t k = 0; k < count; ++k) {
g_mapped[start + k * kPhysicalPageSize] = MappedPage{
pages[k], section, first_offset + k * kPhysicalPageSize, start, count, protect};
}
return true;
}

// With g_phys_mutex held. Unmaps all mapped pages in [start, start + count * kPhysicalPageSize), splitting any
// partially intersecting coalesced views at most once into surviving prefix/suffix runs.
bool unmap_range(uintptr_t start, uint64_t count) {
if (count == 0) return true;
const uintptr_t range_end = start + count * kPhysicalPageSize;
for (uintptr_t cur = start; cur < range_end;) {
const auto it = g_mapped.find(cur);
if (it == g_mapped.end()) {
cur += kPhysicalPageSize;
continue;
}
const uintptr_t view_base = it->second.view_base ? it->second.view_base : cur;
const uint64_t view_pages = it->second.view_pages ? it->second.view_pages : 1;
const uintptr_t view_end = view_base + view_pages * kPhysicalPageSize;
const uintptr_t overlap_end = std::min(view_end, range_end);
if (view_pages == 1) {
const FaultLockExclusive lock;
if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(view_base),
MEM_PRESERVE_PLACEHOLDER)) {
return false;
}
g_mapped.erase(it);
cur = overlap_end;
continue;
}
const auto section = it->second.section;
const DWORD protect = it->second.view_protect;
const auto base_it = g_mapped.find(view_base);
const uint64_t base_offset = (base_it != g_mapped.end()) ? base_it->second.offset : 0;
const uint64_t prefix_pages = (cur - view_base) / kPhysicalPageSize;
const uint64_t suffix_start_idx = (overlap_end - view_base) / kPhysicalPageSize;
const uint64_t suffix_pages = view_pages - suffix_start_idx;

std::vector<uint64_t> prefix_run;
if (prefix_pages > 0) {
prefix_run.resize(static_cast<std::size_t>(prefix_pages));
for (uint64_t k = 0; k < prefix_pages; ++k) {
const auto p_it = g_mapped.find(view_base + k * kPhysicalPageSize);
prefix_run[static_cast<std::size_t>(k)] = (p_it != g_mapped.end()) ? p_it->second.page : 0;
}
}
std::vector<uint64_t> suffix_run;
if (suffix_pages > 0) {
suffix_run.resize(static_cast<std::size_t>(suffix_pages));
for (uint64_t k = 0; k < suffix_pages; ++k) {
const auto p_it = g_mapped.find(view_base + (suffix_start_idx + k) * kPhysicalPageSize);
suffix_run[static_cast<std::size_t>(k)] = (p_it != g_mapped.end()) ? p_it->second.page : 0;
}
}
{
const FaultLockExclusive lock;
if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(view_base),
MEM_PRESERVE_PLACEHOLDER)) {
return false;
}
for (uint64_t k = 0; k < view_pages; ++k) {
g_mapped.erase(view_base + k * kPhysicalPageSize);
}
}
if (prefix_pages > 0 &&
!map_view_run(view_base, prefix_pages, section, base_offset, prefix_run.data(), protect)) {
return false;
}
if (suffix_pages > 0 &&
!map_view_run(overlap_end, suffix_pages, section,
base_offset + suffix_start_idx * kPhysicalPageSize, suffix_run.data(), protect)) {
return false;
}
cur = overlap_end;
}
g_mapped.erase(it);
return true;
}

// With g_phys_mutex held. Unmaps the page at `address` if one is mapped there; the address becomes a placeholder again.
bool unmap_page(uintptr_t address) { return unmap_range(address, 1); }

// With g_phys_mutex held. Maps physical page `page` (0: unmap) at the 64 KiB-aligned `address`, replacing what was
// mapped there. The caller has found the window.
bool map_page(uintptr_t address, uint64_t page, DWORD protect) {
Expand All @@ -474,15 +560,7 @@ bool map_page(uintptr_t address, uint64_t page, DWORD protect) {
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
void* const target = reinterpret_cast<void*>(address);
if (!isolate_placeholder(target, kPhysicalPageSize)) return false;
const FaultLockExclusive lock;
void* const view = placeholder_api().map_view3(it->second.section->handle, GetCurrentProcess(), target,
it->second.offset, kPhysicalPageSize, MEM_REPLACE_PLACEHOLDER,
protect, nullptr, 0);
if (!view) return false;
g_mapped[address] = MappedPage{page, it->second.section, it->second.offset};
return true;
return map_view_run(address, 1, it->second.section, it->second.offset, &page, protect);
}

enum class PageCommit { not_ours, committed_before, committed_now, failed };
Expand Down Expand Up @@ -679,19 +757,46 @@ void* WINAPI XMemMapPhysicalPages(void* address, uint64_t page_count, const uint
}
const std::lock_guard lock(g_phys_mutex);
if (!page_array) {
for (uint64_t i = 0; i < page_count; ++i) {
if (!unmap_page(start + i * kPhysicalPageSize)) return nullptr;
}
return address;
return unmap_range(start, page_count) ? address : nullptr;
}
const Window* const window = find_window(start, page_count * kPhysicalPageSize);
if (!window) {
SetLastError(ERROR_INVALID_ADDRESS);
return nullptr;
}
const DWORD protect = view_protect(*window);
for (uint64_t i = 0; i < page_count; ++i) {
if (!map_page(start + i * kPhysicalPageSize, page_array[i], protect)) return nullptr;
for (uint64_t i = 0; i < page_count;) {
if (page_array[i] == 0) {
uint64_t zero_run = 1;
while (i + zero_run < page_count && page_array[i + zero_run] == 0) ++zero_run;
if (!unmap_range(start + i * kPhysicalPageSize, zero_run)) return nullptr;
i += zero_run;
continue;
}
const auto first_it = g_pages.find(page_array[i]);
if (first_it == g_pages.end()) {
SetLastError(ERROR_INVALID_PARAMETER);
return nullptr;
}
const auto& section = first_it->second.section;
const uint64_t first_offset = first_it->second.offset;
uint64_t run = 1;
while (i + run < page_count && page_array[i + run] != 0) {
const auto next_it = g_pages.find(page_array[i + run]);
if (next_it == g_pages.end() || next_it->second.section != section ||
next_it->second.offset != first_offset + run * kPhysicalPageSize) {
break;
}
++run;
}
if (!unmap_range(start + i * kPhysicalPageSize, run)) return nullptr;
if (!map_view_run(start + i * kPhysicalPageSize, run, section, first_offset, page_array + i, protect)) {
if (run == 1) return nullptr;
for (uint64_t k = 0; k < run; ++k) {
if (!map_page(start + (i + k) * kPhysicalPageSize, page_array[i + k], protect)) return nullptr;
}
}
i += run;
}
return address;
}
Expand Down
35 changes: 35 additions & 0 deletions runtime/test/xmem_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,40 @@ void test_write_combine_window(const PhysicalApi& api) {
check(api.free_pages(1, &page) == TRUE, "free the write-combine window's page");
}

void test_coalesced_run_partial_unmap(const PhysicalApi& api) {
auto* const window = static_cast<uint8_t*>(
api.virtual_alloc(nullptr, 4 * kPage64k, MEM_RESERVE | kXmem64kPages, 0x27, PAGE_READWRITE));
check(window != nullptr, "XMemVirtualAlloc reserves a 4-page window for coalesced run test");
if (!window) return;

uint64_t pages[4]{};
uint64_t count = 4;
check(api.allocate(0x280, &count, pages) == TRUE && count == 4, "allocate 4 contiguous physical pages");
check(api.map(window, 4, pages) == window, "map 4 contiguous physical pages in one call");
for (uint32_t i = 0; i < 4; ++i) {
fill_page(window + static_cast<std::size_t>(i) * kPage64k, 0xA0000000u + (i << 16));
}
// Unmap only page 1 in the middle of the coalesced view.
uint64_t zero = 0;
check(api.map(window + kPage64k, 1, &zero) == window + kPage64k,
"unmap a middle page inside a coalesced run");
check(page_holds(window, 0xA0000000u), "prefix page 0 keeps its data after middle unmap");
check(page_holds(window + 2 * kPage64k, 0xA0020000u), "suffix page 2 keeps its data after middle unmap");
check(page_holds(window + 3 * kPage64k, 0xA0030000u), "suffix page 3 keeps its data after middle unmap");
// Remap page 1 back and verify its backing contents survived.
check(api.map(window + kPage64k, 1, &pages[1]) == window + kPage64k,
"remap the split middle page back");
check(page_holds(window + kPage64k, 0xA0010000u), "remapped middle page keeps its data");
// Unmap a multi-page middle sub-range [1..3) via null page array and verify prefix/suffix survival.
check(api.map(window, 4, pages) == window, "remap 4 contiguous physical pages");
check(api.map(window + kPage64k, 2, nullptr) == window + kPage64k,
"range-unmap middle 2 pages of a 4-page coalesced run");
check(page_holds(window, 0xA0000000u), "prefix page 0 keeps its data after range unmap");
check(page_holds(window + 3 * kPage64k, 0xA0030000u), "suffix page 3 keeps its data after range unmap");
check(api.map(window, 4, nullptr) == window, "unmap the remaining 4-page window");
check(api.free_pages(4, pages) == TRUE, "free the 4 physical pages");
}

// gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off.
void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem, XMemVirtualAllocFn virtual_alloc) {
wchar_t temp[MAX_PATH];
Expand Down Expand Up @@ -973,6 +1007,7 @@ int main() {
test_first_touch_during_remap(physical);
test_placeholders(physical);
test_write_combine_window(physical);
test_coalesced_run_partial_unmap(physical);
}

print_result();
Expand Down