mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:18:37 +00:00
GPU labels reach a title page the D3D12 layer keeps read-only while it watches it
A completed label was stored only into a page that was read-write at that moment and dropped otherwise. The base makes title pages read-only while a shadow or the geometry watch waits for the title's next write, so a label for such a page was lost, and a title that waits for a completion label then waits for ever. The store now announces the write the way a kernel write is announced (kernel_writes::begin_write): the layer opens the pages it guards and copies them again at its next refresh. A page that is still read-only after that is the title's own doing and the label is dropped as before; so are guard, no-access and decommitted pages and a released allocation. The store moves out of the class as label::store_value so that it can be tested. gpu_label_store_test (CPU only, registered as gpu_label_store) covers each of those cases with a stand-in for the layer's opener. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@ -1999,6 +1999,19 @@ if(TARGET Vulkan::Headers)
|
||||
if(BUILD_TESTING)
|
||||
add_test(NAME vulkan_scratch COMMAND vulkan_scratch_test)
|
||||
endif()
|
||||
if(WIN32)
|
||||
# CPU-only: a completed GPU label is stored into a page the D3D12 layer watches (read-only until written) and
|
||||
# dropped for a page that is read-only by the title's own doing (runtime/src/vulkan_gpu_labels.h).
|
||||
add_executable(gpu_label_store_test test/gpu_label_store_test.cpp)
|
||||
target_link_libraries(gpu_label_store_test PRIVATE Vulkan::Headers)
|
||||
target_compile_definitions(gpu_label_store_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE)
|
||||
if(MSVC)
|
||||
target_compile_options(gpu_label_store_test PRIVATE /W4 /permissive- /utf-8)
|
||||
endif()
|
||||
if(BUILD_TESTING)
|
||||
add_test(NAME gpu_label_store COMMAND gpu_label_store_test)
|
||||
endif()
|
||||
endif()
|
||||
if(WIN32 AND BUILD_TESTING)
|
||||
# Whole title-region import capability: one host allocation bound as a BDA buffer. The default case uses the
|
||||
# measured 185 MiB placeholder size. A machine that cannot import (no importable usage/handle, an import-budget
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#include "title_page_watch.h"
|
||||
#endif
|
||||
|
||||
namespace leonida::label {
|
||||
@ -28,6 +29,51 @@ inline bool enabled() {
|
||||
}();
|
||||
return active;
|
||||
}
|
||||
// Stores a completed label into the title's memory: only into a committed, private, writable page of the allocation
|
||||
// the label was captured in, and only while that allocation's generation is the captured one (Memory::publish).
|
||||
//
|
||||
// A page the D3D12 layer watches for the title's writes is read-only while it waits (title_page_watch.h), and it is
|
||||
// writable as far as the title knows. Such a page is announced and opened the way a kernel write is
|
||||
// (kernel_writes::begin_write), so the label reaches it and the layer copies the page again; a page that stays
|
||||
// read-only after that is the title's own doing and the label is dropped, as before. Called with no shadow lock held.
|
||||
inline bool store_value(const Target &target, std::uint64_t value) {
|
||||
#ifdef _WIN32
|
||||
return memory().publish(target, [&](std::uintptr_t address, unsigned width, std::uintptr_t allocation_base) {
|
||||
MEMORY_BASIC_INFORMATION mbi{};
|
||||
const auto in_allocation = [&] {
|
||||
return VirtualQuery(reinterpret_cast<const void *>(address), &mbi, sizeof(mbi)) && mbi.State == MEM_COMMIT &&
|
||||
mbi.Type == MEM_PRIVATE && reinterpret_cast<std::uintptr_t>(mbi.AllocationBase) == allocation_base &&
|
||||
!(mbi.Protect & PAGE_GUARD);
|
||||
};
|
||||
if (!in_allocation()) return false;
|
||||
std::uint64_t pin = 0;
|
||||
if ((mbi.Protect & 0xff) == PAGE_READONLY) {
|
||||
pin = kernel_writes::begin_write(reinterpret_cast<const void *>(address), width, nullptr);
|
||||
if (!pin || !in_allocation()) {
|
||||
kernel_writes::end_write(pin, false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const auto begin = reinterpret_cast<std::uintptr_t>(mbi.BaseAddress);
|
||||
const bool writable = (mbi.Protect & 0xff) == PAGE_READWRITE || (mbi.Protect & 0xff) == PAGE_WRITECOPY;
|
||||
const bool inside = address >= begin && address - begin <= mbi.RegionSize &&
|
||||
mbi.RegionSize - (address - begin) >= width;
|
||||
if (writable && inside) {
|
||||
if (width == 4)
|
||||
InterlockedExchange(reinterpret_cast<volatile LONG *>(address), static_cast<LONG>(value));
|
||||
else
|
||||
InterlockedExchange64(reinterpret_cast<volatile LONG64 *>(address), static_cast<LONG64>(value));
|
||||
}
|
||||
kernel_writes::end_write(pin, false);
|
||||
return writable && inside;
|
||||
});
|
||||
#else
|
||||
(void)target;
|
||||
(void)value;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Each submitted Vulkan command buffer owns a fence and its query slots until the GPU finishes it.
|
||||
// The CPU destination is a generation-checked title data allocation. No guest pointer is a GPU command destination.
|
||||
class Completions {
|
||||
@ -85,30 +131,7 @@ class Completions {
|
||||
idle_pools_.push_back(p);
|
||||
if (b.fence) idle_fences_.push_back(b.fence);
|
||||
}
|
||||
static bool store(const Target &target, std::uint64_t value) {
|
||||
#ifdef _WIN32
|
||||
return memory().publish(target, [&](std::uintptr_t address, unsigned width, std::uintptr_t allocation_base) {
|
||||
MEMORY_BASIC_INFORMATION mbi{};
|
||||
if (!VirtualQuery(reinterpret_cast<const void *>(address), &mbi, sizeof(mbi)) || mbi.State != MEM_COMMIT ||
|
||||
mbi.Type != MEM_PRIVATE || reinterpret_cast<std::uintptr_t>(mbi.AllocationBase) != allocation_base ||
|
||||
(mbi.Protect & PAGE_GUARD) ||
|
||||
((mbi.Protect & 0xff) != PAGE_READWRITE && (mbi.Protect & 0xff) != PAGE_WRITECOPY))
|
||||
return false;
|
||||
const auto begin = reinterpret_cast<std::uintptr_t>(mbi.BaseAddress);
|
||||
if (address < begin || address - begin > mbi.RegionSize || mbi.RegionSize - (address - begin) < width)
|
||||
return false;
|
||||
if (width == 4)
|
||||
InterlockedExchange(reinterpret_cast<volatile LONG *>(address), static_cast<LONG>(value));
|
||||
else
|
||||
InterlockedExchange64(reinterpret_cast<volatile LONG64 *>(address), static_cast<LONG64>(value));
|
||||
return true;
|
||||
});
|
||||
#else
|
||||
(void)target;
|
||||
(void)value;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
static bool store(const Target &target, std::uint64_t value) { return store_value(target, value); }
|
||||
void publish(const Batch &batch) {
|
||||
for (std::size_t i = 0; i < batch.labels.size(); ++i) {
|
||||
const Write &op = batch.labels[i];
|
||||
|
||||
119
runtime/test/gpu_label_store_test.cpp
Normal file
119
runtime/test/gpu_label_store_test.cpp
Normal file
@ -0,0 +1,119 @@
|
||||
// leonida::label::store_value (runtime/src/vulkan_gpu_labels.h): where a completed GPU label may be stored in the
|
||||
// title's memory, and what it does with a page the D3D12 layer keeps read-only while it watches for the title's writes
|
||||
// (runtime/src/title_page_watch.h). The layer is stood in for by an opener that makes its "watched" pages writable
|
||||
// again, as DeviceState's opener does. CPU only: no Vulkan device, no GPU. No game data.
|
||||
#include "../src/vulkan_gpu_labels.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
namespace {
|
||||
|
||||
int g_checks = 0;
|
||||
int g_failures = 0;
|
||||
|
||||
void check(bool condition, const char* what) {
|
||||
++g_checks;
|
||||
std::printf("%s %s\n", condition ? "PASS" : "FAIL", what);
|
||||
if (!condition) ++g_failures;
|
||||
}
|
||||
|
||||
constexpr std::size_t kPage = 4096;
|
||||
|
||||
std::uintptr_t g_watched_begin = 0, g_watched_end = 0; // the pages the stand-in layer made read-only
|
||||
int g_opened = 0;
|
||||
std::uintptr_t g_asked_begin = 0, g_asked_end = 0;
|
||||
|
||||
// Stands in for the layer's opener (open_shadow_pages): only pages it made read-only itself become writable.
|
||||
void open_watched(std::uintptr_t begin, std::uintptr_t end) {
|
||||
++g_opened;
|
||||
g_asked_begin = begin;
|
||||
g_asked_end = end;
|
||||
for (std::uintptr_t page = begin & ~(kPage - 1); page < end; page += kPage) {
|
||||
if (page < g_watched_begin || page >= g_watched_end) continue;
|
||||
DWORD old = 0;
|
||||
VirtualProtect(reinterpret_cast<void*>(page), kPage, PAGE_READWRITE, &old);
|
||||
}
|
||||
}
|
||||
|
||||
DWORD protection(const void* p) {
|
||||
MEMORY_BASIC_INFORMATION mbi{};
|
||||
return VirtualQuery(p, &mbi, sizeof(mbi)) ? mbi.Protect : 0;
|
||||
}
|
||||
|
||||
bool protect(void* p, DWORD how) {
|
||||
DWORD old = 0;
|
||||
return VirtualProtect(p, kPage, how, &old) != FALSE;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
namespace label = leonida::label;
|
||||
namespace writes = leonida::kernel_writes;
|
||||
auto* const base = static_cast<std::uint8_t*>(VirtualAlloc(nullptr, 4 * kPage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
|
||||
if (!base) return 1;
|
||||
const auto address = reinterpret_cast<std::uintptr_t>(base);
|
||||
label::memory().track(address, 4 * kPage, address, false);
|
||||
auto* const wide = reinterpret_cast<std::uint64_t*>(base + 0x40);
|
||||
auto* const narrow = reinterpret_cast<std::uint32_t*>(base + kPage + 0x10);
|
||||
const label::Target wide_target = label::memory().capture(reinterpret_cast<std::uintptr_t>(wide), 8);
|
||||
const label::Target narrow_target = label::memory().capture(reinterpret_cast<std::uintptr_t>(narrow), 4);
|
||||
check(wide_target.bytes == 8 && narrow_target.bytes == 4, "labels are captured in a tracked allocation");
|
||||
|
||||
// 1. A writable page: stored, with no opener asked.
|
||||
writes::set_opener(&open_watched);
|
||||
check(label::store_value(wide_target, 0x1122334455667788ull) && *wide == 0x1122334455667788ull && g_opened == 0,
|
||||
"an 8-byte label is stored into a read-write page");
|
||||
check(label::store_value(narrow_target, 0xA1B2C3D4u) && *narrow == 0xA1B2C3D4u && narrow[1] == 0 && g_opened == 0,
|
||||
"a 4-byte label is stored and leaves the next word alone");
|
||||
|
||||
// 2. A page the layer watches (read-only until written): it is opened like a kernel write and the label lands.
|
||||
g_watched_begin = address;
|
||||
g_watched_end = address + kPage;
|
||||
check(protect(base, PAGE_READONLY), "the first page is made read-only, as a watched page is");
|
||||
check(label::store_value(wide_target, 0x0102030405060708ull), "a label for a watched read-only page is stored");
|
||||
check(*wide == 0x0102030405060708ull, "the value is in the page");
|
||||
check(g_opened == 1 && g_asked_begin == reinterpret_cast<std::uintptr_t>(wide) && g_asked_end == g_asked_begin + 8,
|
||||
"the layer was asked to open exactly the label's bytes, once");
|
||||
check(protection(base) == PAGE_READWRITE, "the page is writable again, so the layer copies it at its next refresh");
|
||||
check(writes::state().count.load() == 0, "the write's pin has ended");
|
||||
|
||||
// 3. A page that stays read-only when the layer is asked (the title's own protection): dropped, no fault.
|
||||
check(protect(base + kPage, PAGE_READONLY), "the second page is made read-only by the title");
|
||||
const int opened = g_opened;
|
||||
check(!label::store_value(narrow_target, 0x55555555u) && *narrow == 0xA1B2C3D4u,
|
||||
"a label for the title's own read-only page is dropped and the old value stays");
|
||||
check(g_opened == opened + 1 && writes::state().count.load() == 0, "the layer was asked and the pin has ended");
|
||||
check(protection(base + kPage) == PAGE_READONLY, "the title's protection is untouched");
|
||||
|
||||
// 4. No layer at all: a read-only page is dropped without asking anyone.
|
||||
writes::set_opener(nullptr);
|
||||
check(protect(base, PAGE_READONLY), "the first page is read-only again");
|
||||
check(!label::store_value(wide_target, 0x9999999999999999ull) && *wide == 0x0102030405060708ull,
|
||||
"with no opener a read-only page drops the label");
|
||||
writes::set_opener(&open_watched);
|
||||
check(label::store_value(wide_target, 0x9999999999999999ull) && *wide == 0x9999999999999999ull,
|
||||
"with the opener back the same label is stored");
|
||||
|
||||
// 5. Guard pages, no-access pages and decommitted pages are never written, opener or not.
|
||||
check(protect(base + 2 * kPage, PAGE_READWRITE | PAGE_GUARD), "the third page is a guard page");
|
||||
const label::Target guarded = label::memory().capture(address + 2 * kPage + 8, 8);
|
||||
check(!label::store_value(guarded, 1) && (protection(base + 2 * kPage) & PAGE_GUARD),
|
||||
"a label for a guard page is dropped and the guard is still armed");
|
||||
check(protect(base + 3 * kPage, PAGE_NOACCESS), "the fourth page is no-access");
|
||||
const label::Target hidden = label::memory().capture(address + 3 * kPage + 8, 8);
|
||||
check(!label::store_value(hidden, 1), "a label for a no-access page is dropped");
|
||||
check(VirtualFree(base + 3 * kPage, kPage, MEM_DECOMMIT) != FALSE, "the fourth page is decommitted");
|
||||
check(!label::store_value(hidden, 1), "a label for a decommitted page is dropped");
|
||||
|
||||
// 6. A released allocation: the captured generation is gone, so nothing is stored whatever is mapped there now.
|
||||
check(label::memory().release_pages(address, 0, true, [&] { return VirtualFree(base, 0, MEM_RELEASE) != FALSE; }),
|
||||
"the allocation is released");
|
||||
check(!label::store_value(wide_target, 2), "a label captured before the release is dropped");
|
||||
writes::set_opener(nullptr);
|
||||
|
||||
std::printf("gpu_label_store_test: %s (%d checks, %d failures)\n", g_failures ? "FAILED" : "ok", g_checks,
|
||||
g_failures);
|
||||
return g_failures ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user