Skip to content
Open
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
3 changes: 3 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,9 @@ The copies:
read-only (the geometry watch, another view's shadow): the fault handler asks every tracker over the address, this
one included, and on the thread whose copy faulted it does not take that lock again (an SRW lock is not
recursive; on 621.40 the title's submitting thread hung there for good, the main loop frozen with no device loss).
A page the geometry watch makes writable with no fault to answer (the last cell over the tile is dropped) reopens
every shadow over that tile before the next copy-back, so a page both guarded is not taken for unchanged and
overwritten with the copy's older bytes.
Up to 8,192 ranges and 64 MiB a submission (a V# has no read/write bit, so every view
reached is compared; past the caps the ranges are left behind, logged once). `LEONIDA_TITLE_WRITEBACK=0` turns it off. A batch is fenced: it is kept with the ticket
of the submission that will use it and copied back only when the context reports that ticket retired, so a range
Expand Down
11 changes: 11 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
target_compile_definitions(cpu_emulation_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(cpu_emulation_test PRIVATE /W4 /permissive- /utf-8)

# The geometry watch's cells (src/title_geometry_watch.h): a cell dropped while it guarded pages that a slot-133
# shadow also guards opens them with no write fault, so release() tells the reopener the layer installs. No GPU, no
# game data: pages of a private allocation.
add_executable(geometry_writes_test test/geometry_writes_test.cpp)
target_include_directories(geometry_writes_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_compile_definitions(geometry_writes_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(geometry_writes_test PRIVATE /W4 /permissive- /utf-8)

# Xbox DirectStorage (DStorageGetFactory under dstorage_xs.dll, xcrdapi.dll and dstorage.dll) over synthetic files:
# GAME: paths through the file shims, queues, status arrays, fences and errors. No GPU, no game data.
set(dstorage_vectors "${CMAKE_CURRENT_BINARY_DIR}/dstorage_vectors.inc")
Expand Down Expand Up @@ -1665,6 +1673,9 @@ if(BUILD_TESTING)
if(WIN32 AND TARGET cpu_emulation_test)
add_test(NAME cpu_emulation COMMAND cpu_emulation_test)
endif()
if(WIN32 AND TARGET geometry_writes_test)
add_test(NAME geometry_writes COMMAND geometry_writes_test)
endif()
if(WIN32 AND TARGET xmem_test)
add_test(NAME xmem COMMAND xmem_test)
# The pool read test executes its read from a PE at the title's image base.
Expand Down
15 changes: 12 additions & 3 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,14 @@ LONG CALLBACK shadow_write_fault(EXCEPTION_POINTERS* info) {
return retry ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
}

// The shadow half of a reopen: every tracker covering [from, to) makes its pages writable and forgets that it guarded
// them, so the title's write is not copied over from an older copy at the next write-back.
void reopen_shadow_ranges(std::uintptr_t from, std::uintptr_t to) {
AcquireSRWLockShared(&g_shadow_pages_lock);
for (ShadowPages* pages : shadow_pages_list()) pages->reopen_range(from, to);
ReleaseSRWLockShared(&g_shadow_pages_lock);
}

// leonida::kernel_writes' opener: a file shim is about to let the kernel write [from, to).
void open_shadow_pages(std::uintptr_t from, std::uintptr_t to) {
bool tiles_reopened = false;
Expand All @@ -1552,9 +1560,7 @@ void open_shadow_pages(std::uintptr_t from, std::uintptr_t to) {
from &= ~(kTile - 1);
to = (to + kTile - 1) & ~(kTile - 1);
}
AcquireSRWLockShared(&g_shadow_pages_lock);
for (ShadowPages* pages : shadow_pages_list()) pages->reopen_range(from, to);
ReleaseSRWLockShared(&g_shadow_pages_lock);
reopen_shadow_ranges(from, to);
}

// fsdraw's backing hook: whether a slot-133 shadow keeps the GPU copy of a title VA (LEONIDA_FSDRAW_PROBE only).
Expand All @@ -1571,6 +1577,9 @@ bool watch_shadow_writes() {
static const bool installed = [] {
if (!AddVectoredExceptionHandler(1, &shadow_write_fault)) return false;
leonida::kernel_writes::set_opener(&open_shadow_pages);
// A geometry cell dropped while it guarded a page a shadow also guards: the shadows reopen that tile now,
// before a write-back can take their pages for unchanged (title_geometry_watch.h, release).
leonida::geometry_writes::set_reopener(&reopen_shadow_ranges);
return true;
}();
return installed;
Expand Down
27 changes: 22 additions & 5 deletions runtime/src/title_geometry_watch.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ namespace leonida::geometry_writes {
constexpr std::uintptr_t tile_size = 65536, page_size = 4096;
using Aliases = void (*)(std::uintptr_t, std::vector<std::uintptr_t>&);
inline std::atomic<Aliases> aliases{};
// Who makes the layer's slot-133 shadows reopen [from, to) when a tile's pages are made writable here with no write
// fault to tell them (release). A page of a geometry tile can also be one a shadow keeps read-only, and a shadow that
// is not told takes it for unchanged and writes its older copy back over the title's write. Called with this watch's
// mutex released, so the handler's order (shadow lock, then this mutex) is not inverted.
using Reopener = void (*)(std::uintptr_t from, std::uintptr_t to);
inline std::atomic<Reopener> reopener{};
inline void set_reopener(Reopener reopen_ranges) { reopener.store(reopen_ranges, std::memory_order_release); }

struct Cell {
std::atomic<std::uint64_t> version{1}, mapping{1};
Expand Down Expand Up @@ -155,15 +162,25 @@ inline bool reopen(std::uintptr_t va, Cell& c) {
}
inline void release(std::uintptr_t va) {
auto& s = state();
std::lock_guard lock(s.mutex);
auto it = s.cells.find(va);
if (it != s.cells.end() && !--it->second.refs) {
bool opened = false;
{
std::lock_guard lock(s.mutex);
auto it = s.cells.find(va);
if (it == s.cells.end() || --it->second.refs) return;
// A page reopened here may also be one a slot-133 shadow keeps read-only (both guard the title's geometry):
// it is writable now with no fault to tell that shadow, which then walks its pages again.
if (reopen(va, it->second)) title_memory_map::changed(va, va + tile_size);
// it is writable now with no fault to tell that shadow, which then walks its pages again. The journal entry
// below reaches it at its next refresh, and the reopener reaches it now: a write-back between the two would
// otherwise copy the shadow's older bytes over the title's write. Both run outside s.mutex, so a fault that
// holds a shadow's lock and asks this watch cannot deadlock against this one.
opened = reopen(va, it->second);
s.cells.erase(it);
s.count.store(s.cells.size(), std::memory_order_release);
}
if (opened) {
if (const auto reopen_ranges = reopener.load(std::memory_order_acquire))
reopen_ranges(va, va + tile_size);
title_memory_map::changed(va, va + tile_size);
}
}
enum class Event { write, open, fault, mapping, replaced };
// `restored` (open and fault): set when a tile's pages were made writable again. A tile reopens all of its pages, and
Expand Down
124 changes: 124 additions & 0 deletions runtime/test/geometry_writes_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// A geometry cell guards a title tile read-only so the watch sees the title's writes to it. A page of that tile can
// also be one a slot-133 shadow keeps read-only (both guard the title's geometry). When the last reference to the cell
// goes (release), the pages are made writable again with no write fault to tell the shadow: the shadow takes them for
// unchanged and writes its older copy back over the title's write at the next submission
// (RT-NULL-VTABLE-P4E683D6: a live object's vtable word came back zero). release() therefore calls the reopener the
// layer installs, with the tile's range and this watch's mutex released.
//
// No GPU and no game data: pages of a private allocation.

#include "../src/title_geometry_watch.h"

#include <windows.h>

#include <cstdio>

namespace gw = leonida::geometry_writes;

namespace {

int failures = 0;

#define CHECK(what) \
do { \
if (!(what)) { \
std::printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #what); \
++failures; \
} \
} while (0)

int calls = 0;
std::uintptr_t seen_from = 0, seen_to = 0;
bool watch_mutex_free = false;

// The layer's reopener shape: called for a tile whose pages it just made writable. It asks the watch again, which
// takes the watch's mutex, so a release() that called this with the mutex held deadlocks here (ctest: a timeout).
void recorder(std::uintptr_t from, std::uintptr_t to) {
++calls;
seen_from = from;
seen_to = to;
gw::notify(from, from + 1, gw::Event::write);
watch_mutex_free = true;
}

DWORD protection(std::uintptr_t at) {
MEMORY_BASIC_INFORMATION m{};
return VirtualQuery(reinterpret_cast<const void*>(at), &m, sizeof m) ? (m.Protect & 0xff) : 0;
}

struct Tile {
void* base = VirtualAlloc(nullptr, gw::tile_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
std::uintptr_t at() const { return reinterpret_cast<std::uintptr_t>(base); }
~Tile() {
if (base) VirtualFree(base, 0, MEM_RELEASE);
}
};

void reset() {
calls = 0;
seen_from = seen_to = 0;
watch_mutex_free = false;
}

} // namespace

int main() {
gw::set_reopener(&recorder);

// 1. An armed cell is read-only; dropping it opens the page and tells the reopener about the whole tile, with the
// watch's mutex released.
{
Tile tile;
CHECK(tile.base != nullptr);
gw::Cell* cell = gw::acquire(tile.at());
CHECK(gw::arm(tile.at(), *cell));
CHECK(protection(tile.at()) == PAGE_READONLY);
const auto generation = leonida::title_memory_map::generation();
reset();
gw::release(tile.at());
CHECK(calls == 1);
CHECK(seen_from == tile.at());
CHECK(seen_to == tile.at() + gw::tile_size);
CHECK(watch_mutex_free);
CHECK(protection(tile.at()) == PAGE_READWRITE);
CHECK(gw::state().count.load() == 0);
CHECK(leonida::title_memory_map::generation() > generation); // the shadows' next refresh sees it too
}

// 2. A cell that never guarded anything (no arm, or its pages were already writable) opens nothing, so there is
// nothing to tell: no call, and no journal entry either.
{
Tile tile;
gw::Cell* cell = gw::acquire(tile.at());
(void)cell;
const auto generation = leonida::title_memory_map::generation();
reset();
gw::release(tile.at());
CHECK(calls == 0);
CHECK(leonida::title_memory_map::generation() == generation);
CHECK(protection(tile.at()) == PAGE_READWRITE);
}

// 3. Two holders: the page is opened, and the reopener told, only when the last one goes.
{
Tile tile;
gw::Cell* cell = gw::acquire(tile.at());
gw::acquire(tile.at());
CHECK(gw::arm(tile.at(), *cell));
reset();
gw::release(tile.at());
CHECK(calls == 0);
CHECK(protection(tile.at()) == PAGE_READONLY);
gw::release(tile.at());
CHECK(calls == 1);
CHECK(protection(tile.at()) == PAGE_READWRITE);
}

gw::set_reopener(nullptr);
if (failures) {
std::printf("geometry_writes_test: %d failure(s)\n", failures);
return 1;
}
std::printf("geometry_writes_test: OK\n");
return 0;
}
Loading