diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 06:43:13 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 06:43:13 -0500 |
| commit | 924966e2fbde9ae0ac96dcd433803047505efa2f (patch) | |
| tree | c1a163dd8a38c0113c737ddaf005ffc34c312c8f /runtime/src | |
| parent | f3bfc039b27e0a2a70994885a54a8602cbd24d43 (diff) | |
Adapt #622 to #1723: its arena and xmem writes become the list values' fallback
#1723's list values own list slots 71 to 74 of the lists they track and
hand 71/72 to the GPU-label path only for lists they do not track. This
keeps that, and keeps what #622 adds that #1723 lacks: landing a value
in the GPU address arena and on an xmem page the title's view refuses.
- LEONIDA_GPU_LABELS stays opt-in (`1`), as on main and in #1723.
- src/value_write_sink.h: one place for the two targets a host store
cannot reach, used by both writers. The device answers for the arena
(an address below its size that is not the title's memory, the rule
resolve_title_memory uses); xmem registers write_title_bytes for its
pages. Replaces #622's label-only sink and xmem's title_write_fn.
- The pump's land(): the arena is asked before any host store, so an
arena offset is never stored to as a host address; then
kernel_writes::store as before; then xmem's mapping when that store
faults. Counted as writes_arena and writes_title_pages.
- The arena refuses its reserved start (null region, zero page, quad
indices) and a write past its end, stores an aligned value in one
access, and is unregistered when its device goes.
- xmem's write_title_bytes stores an aligned value in one access.
- The label path keeps the same order for untracked lists.
Test, no game data: xbox_list_value_fallback. A tracked list's writes to
title memory land through the host store; to a default-heap buffer's
arena offset land in the arena (read back by a GPU copy); to the arena's
reserved start are dropped; to an xmem page the title made PAGE_NOACCESS
land through xmem's mapping. LEONIDA_GPU_LABELS=1 throughout, and no
write is handed to the label path. Without the arena step it fails at
line 168, without the xmem step at line 208 (llvmpipe and the iGPU).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/value_write_sink.h | 77 | ||||
| -rw-r--r-- | runtime/src/vulkan_gpu_labels.h | 42 |
2 files changed, 96 insertions, 23 deletions
diff --git a/runtime/src/value_write_sink.h b/runtime/src/value_write_sink.h new file mode 100644 index 000000000..d6db3989b --- /dev/null +++ b/runtime/src/value_write_sink.h | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | #pragma once | ||
| 2 | // Where a console value write (list slots 71 and 72) lands when its target is not memory a plain host store reaches: | ||
| 3 | // - the GPU address arena: the title names a default-heap buffer by its arena offset, which is not a host address, so | ||
| 4 | // a host store at that number would write whatever host memory is there. The D3D12 device that owns the arena | ||
| 5 | // answers for its range (set_arena) and writes through its own mapping. | ||
| 6 | // - a page of the title's memory that xmem maps PAGE_NOACCESS (a GPU-only window): the title's view refuses the CPU, | ||
| 7 | // and xmem writes through its own mapping of the physical page (set_title_pages, rt::xmem::write_title_bytes). | ||
| 8 | // Two writers use it: the list values' pump (shims/d3d12_xs_list_values.h), which lands the writes of the lists it | ||
| 9 | // tracks, and the GPU-label path (vulkan_gpu_labels.h, LEONIDA_GPU_LABELS=1), for lists it does not track. | ||
| 10 | // Portable: no Windows or Vulkan headers, so both sides and the tests without a GPU can include it. | ||
| 11 | |||
| 12 | #include <atomic> | ||
| 13 | #include <cstddef> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <mutex> | ||
| 16 | |||
| 17 | namespace leonida::value_sink { | ||
| 18 | |||
| 19 | enum class Landing { | ||
| 20 | elsewhere, // not the arena's: the caller stores it as before | ||
| 21 | landed, // the arena took it | ||
| 22 | refused, // the arena's, but its bytes cannot be written there (reserved, sparse, past the end): dropped | ||
| 23 | }; | ||
| 24 | |||
| 25 | // `device` is the context set_arena was given; `address` the title's address of the value, `size` 4 or 8. | ||
| 26 | using ArenaWrite = Landing (*)(void* device, std::uint64_t address, const void* bytes, unsigned size); | ||
| 27 | // True when the bytes were written; false when the range is not one of the shim's pages. | ||
| 28 | using TitlePagesWrite = bool (*)(const void* address, const void* bytes, std::size_t size); | ||
| 29 | |||
| 30 | struct State { | ||
| 31 | // Held across an arena write, so a device that goes away (clear_arena) waits for one in progress. | ||
| 32 | std::mutex mutex; | ||
| 33 | ArenaWrite arena = nullptr; | ||
| 34 | void* device = nullptr; | ||
| 35 | std::atomic<TitlePagesWrite> title_pages{nullptr}; | ||
| 36 | }; | ||
| 37 | |||
| 38 | inline State& state() { | ||
| 39 | static State s; | ||
| 40 | return s; | ||
| 41 | } | ||
| 42 | |||
| 43 | // The device that owns the arena. The last device to set it answers; clear_arena(device) ends its turn. | ||
| 44 | inline void set_arena(ArenaWrite write, void* device) { | ||
| 45 | State& s = state(); | ||
| 46 | const std::lock_guard lock(s.mutex); | ||
| 47 | s.arena = write; | ||
| 48 | s.device = device; | ||
| 49 | } | ||
| 50 | |||
| 51 | inline void clear_arena(void* device) { | ||
| 52 | State& s = state(); | ||
| 53 | const std::lock_guard lock(s.mutex); | ||
| 54 | if (s.device != device) return; | ||
| 55 | s.arena = nullptr; | ||
| 56 | s.device = nullptr; | ||
| 57 | } | ||
| 58 | |||
| 59 | // Asked before any host store of a value write: an arena offset must never reach one. | ||
| 60 | inline Landing arena(std::uint64_t address, const void* bytes, unsigned size) { | ||
| 61 | State& s = state(); | ||
| 62 | const std::lock_guard lock(s.mutex); | ||
| 63 | if (!s.arena) return Landing::elsewhere; | ||
| 64 | return s.arena(s.device, address, bytes, size); | ||
| 65 | } | ||
| 66 | |||
| 67 | // xmem sets it when it is linked into the process (shims/xmem.cpp). | ||
| 68 | inline void set_title_pages(TitlePagesWrite write) { state().title_pages.store(write, std::memory_order_release); } | ||
| 69 | |||
| 70 | // The fallback after a host store into the title's memory failed: the page may be one of xmem's that the title's view | ||
| 71 | // keeps PAGE_NOACCESS. False without xmem, or when the page is not xmem's. | ||
| 72 | inline bool title_pages(const void* address, const void* bytes, std::size_t size) { | ||
| 73 | const TitlePagesWrite write = state().title_pages.load(std::memory_order_acquire); | ||
| 74 | return write && write(address, bytes, size); | ||
| 75 | } | ||
| 76 | |||
| 77 | } // namespace leonida::value_sink | ||
diff --git a/runtime/src/vulkan_gpu_labels.h b/runtime/src/vulkan_gpu_labels.h index e7005ff39..60953417f 100644 --- a/runtime/src/vulkan_gpu_labels.h +++ b/runtime/src/vulkan_gpu_labels.h | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | #include "gpu_label_memory.h" | 2 | #include "gpu_label_memory.h" |
| 3 | #include "value_write_sink.h" | ||
| 3 | #include <vulkan/vulkan.h> | 4 | #include <vulkan/vulkan.h> |
| 4 | #include <algorithm> | 5 | #include <algorithm> |
| 5 | #include <atomic> | 6 | #include <atomic> |
| @@ -22,24 +23,12 @@ | |||
| 22 | #endif | 23 | #endif |
| 23 | 24 | ||
| 24 | namespace leonida::label { | 25 | namespace leonida::label { |
| 25 | 26 | // LEONIDA_GPU_LABELS=1 records list slots 71 and 72 here, for the lists the list values (shims/d3d12_xs_list_values.h, | |
| 26 | // Where a completed label lands when the runtime's own host-visible memory is the target: the GPU address arena, | 27 | // LEONIDA_LIST_VALUES) do not track: those land their writes themselves, and one address has one writer. |
| 27 | // and the physical pages of the title's own memory that the xmem shim owns. The D3D12 device knows where those | ||
| 28 | // bytes are and installs this sink; the label path writes the value through it. | ||
| 29 | using ValueSink = bool (*)(void* context, std::uintptr_t address, const void* bytes, unsigned size); | ||
| 30 | inline std::atomic<ValueSink> value_sink{nullptr}; | ||
| 31 | inline std::atomic<void*> value_sink_context{nullptr}; | ||
| 32 | inline void set_value_sink(ValueSink sink, void* context) { | ||
| 33 | value_sink_context.store(context, std::memory_order_relaxed); | ||
| 34 | value_sink.store(sink, std::memory_order_release); | ||
| 35 | } | ||
| 36 | |||
| 37 | // The console's value writes (list slots 71 and 72) are on: the retail title calls the two slots and waits for | ||
| 38 | // the value to land, and a plain run is the case that matters. LEONIDA_GPU_LABELS=0 turns them off. | ||
| 39 | inline bool enabled() { | 28 | inline bool enabled() { |
| 40 | static const bool active = [] { | 29 | static const bool active = [] { |
| 41 | const char *v = std::getenv("LEONIDA_GPU_LABELS"); | 30 | const char *v = std::getenv("LEONIDA_GPU_LABELS"); |
| 42 | return !(v && v[0] == '0'); | 31 | return v && v[0] == '1'; |
| 43 | }(); | 32 | }(); |
| 44 | return active; | 33 | return active; |
| 45 | } | 34 | } |
| @@ -51,17 +40,24 @@ inline bool enabled() { | |||
| 51 | // (kernel_writes::begin_write), so the label reaches it and the layer copies the page again; a page that stays | 40 | // (kernel_writes::begin_write), so the label reaches it and the layer copies the page again; a page that stays |
| 52 | // read-only after that is the title's own doing and the label is dropped, as before. Called with no shadow lock held. | 41 | // read-only after that is the title's own doing and the label is dropped, as before. Called with no shadow lock held. |
| 53 | inline bool store_value(const Target &target, std::uint64_t value) { | 42 | inline bool store_value(const Target &target, std::uint64_t value) { |
| 54 | // The runtime's own host-visible memory: the GPU address arena, and the physical pages of the title's own | 43 | // The runtime's own memory first (src/value_write_sink.h): an arena offset is not a host address, so it goes to the |
| 55 | // memory that the xmem shim owns. The value is written little-endian, the way the console's x64 writes it: the | 44 | // arena and never to the store below; a page xmem maps PAGE_NOACCESS is written through xmem's own mapping. The |
| 56 | // low 'width' bytes of the word. A target the sink refuses - the title's own private pages - is left to the | 45 | // value is written little-endian, the way the console's x64 writes it: the low 'width' bytes of the word. A |
| 57 | // path below, which opens a read-only page the way a kernel write does. | 46 | // target neither takes - the title's own private pages - is left to the path below, which opens a read-only page |
| 58 | if (ValueSink sink = value_sink.load(std::memory_order_acquire)) { | 47 | // the way a kernel write does. |
| 59 | std::uint64_t word = value; | 48 | { |
| 60 | void* const context = value_sink_context.load(std::memory_order_relaxed); | 49 | const std::uint64_t word = value; |
| 50 | bool refused = false; | ||
| 61 | if (memory().publish(target, [&](std::uintptr_t address, unsigned width, std::uintptr_t) { | 51 | if (memory().publish(target, [&](std::uintptr_t address, unsigned width, std::uintptr_t) { |
| 62 | return sink(context, address, &word, width); | 52 | const value_sink::Landing landing = value_sink::arena(address, &word, width); |
| 53 | if (landing != value_sink::Landing::elsewhere) { | ||
| 54 | refused = landing == value_sink::Landing::refused; | ||
| 55 | return !refused; | ||
| 56 | } | ||
| 57 | return value_sink::title_pages(reinterpret_cast<const void *>(address), &word, width); | ||
| 63 | })) | 58 | })) |
| 64 | return true; | 59 | return true; |
| 60 | if (refused) return false; | ||
| 65 | } | 61 | } |
| 66 | #ifdef _WIN32 | 62 | #ifdef _WIN32 |
| 67 | return memory().publish(target, [&](std::uintptr_t address, unsigned width, std::uintptr_t allocation_base) { | 63 | return memory().publish(target, [&](std::uintptr_t address, unsigned width, std::uintptr_t allocation_base) { |