diff options
| author | Etr0p <216035314+Etr0p@users.noreply.github.com> | 2026-11-23 20:51:08 +0100 |
|---|---|---|
| committer | Etr0p <216035314+Etr0p@users.noreply.github.com> | 2026-11-23 20:51:08 +0100 |
| commit | d2f328e560dcbbdbc0e34f5d26f5855c5ffd8209 (patch) | |
| tree | c7d02861ec4da9a94a5923e7371f33b1bad29910 /runtime | |
| parent | 65668002e972259722f008617068dd8ee90c82ce (diff) | |
d3d12: only the title's memory gets a copy; the table scan covers the declared slots and is timed
The xmem shims record what they hand the title (XMemAlloc, XMemAllocDefault,
XMemVirtualAlloc, the title's VirtualAlloc); an address outside those ranges
is left as it was and counted. The submit-time scan reads only the
CBV/SRV/UAV slots the bound table's root signature declares, and its cost
is accumulated per ExecuteCommandLists. Section 3.3 describes the three
places addresses are replaced and the two kinds of copy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 24 | ||||
| -rw-r--r-- | runtime/shims/d3d12_com.h | 3 | ||||
| -rw-r--r-- | runtime/shims/d3d12_vulkan_device.cpp | 39 | ||||
| -rw-r--r-- | runtime/shims/xmem.cpp | 36 | ||||
| -rw-r--r-- | runtime/src/title_va_alias.h | 58 | ||||
| -rw-r--r-- | runtime/test/d3d12_title_va_test.cpp | 25 | ||||
| -rw-r--r-- | runtime/test/title_va_alias_test.cpp | 8 |
7 files changed, 163 insertions, 30 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 6e718cee7..1ad12e432 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -12,8 +12,10 @@ | |||
| 12 | #include <array> | 12 | #include <array> |
| 13 | #include <atomic> | 13 | #include <atomic> |
| 14 | #include <bit> | 14 | #include <bit> |
| 15 | #include <chrono> | ||
| 15 | #include <condition_variable> | 16 | #include <condition_variable> |
| 16 | #include <functional> | 17 | #include <functional> |
| 18 | #include <map> | ||
| 17 | #include <memory> | 19 | #include <memory> |
| 18 | #include <optional> | 20 | #include <optional> |
| 19 | #include <cstdio> | 21 | #include <cstdio> |
| @@ -200,7 +202,7 @@ public: | |||
| 200 | 202 | ||
| 201 | const leonida::CommandList& commands() const { return list_; } | 203 | const leonida::CommandList& commands() const { return list_; } |
| 202 | // The descriptor tables this recording binds (console VAs), for the scan before a submission. | 204 | // The descriptor tables this recording binds (console VAs), for the scan before a submission. |
| 203 | const std::vector<std::uint64_t>& tables() const { return tables_; } | 205 | const std::vector<std::pair<std::uint64_t, std::uint32_t>>& tables() const { return tables_; } |
| 204 | // The pipeline states and root signatures the recording names (it holds a reference to each until Reset). | 206 | // The pipeline states and root signatures the recording names (it holds a reference to each until Reset). |
| 205 | const std::vector<IUnknown*>& pinned() const { return held_; } | 207 | const std::vector<IUnknown*>& pinned() const { return held_; } |
| 206 | UINT front_stencil_ref() const { return front_stencil_ref_; } | 208 | UINT front_stencil_ref() const { return front_stencil_ref_; } |
| @@ -1176,8 +1178,8 @@ private: | |||
| 1176 | // of the layer's heaps: kept, and its buffer V#s are rewritten the same way before each submission. | 1178 | // of the layer's heaps: kept, and its buffer V#s are rewritten the same way before each submission. |
| 1177 | if (type == rt::rootsig::ParamType::Descriptor) | 1179 | if (type == rt::rootsig::ParamType::Descriptor) |
| 1178 | va = leonida::title_va_aliases().resolve(va, kRootViewBytes, leonida::TitleVaAliases::root_view); | 1180 | va = leonida::title_va_aliases().resolve(va, kRootViewBytes, leonida::TitleVaAliases::root_view); |
| 1179 | else if (va && (tables_.empty() || tables_.back() != va)) | 1181 | else if (va && p->table_descriptors && (tables_.empty() || tables_.back().first != va)) |
| 1180 | tables_.push_back(va); | 1182 | tables_.push_back({va, p->table_descriptors}); |
| 1181 | const std::uint32_t first = first_dword(state, *p); | 1183 | const std::uint32_t first = first_dword(state, *p); |
| 1182 | write(state, first, std::uint32_t(va)); | 1184 | write(state, first, std::uint32_t(va)); |
| 1183 | // In the compute layout a descriptor table is one dword, the 32-bit VA of its first descriptor (the heaps | 1185 | // In the compute layout a descriptor table is one dword, the 32-bit VA of its first descriptor (the heaps |
| @@ -1202,7 +1204,8 @@ private: | |||
| 1202 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED | 1204 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED |
| 1203 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; | 1205 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; |
| 1204 | leonida::IndexBufferView index_{}; | 1206 | leonida::IndexBufferView index_{}; |
| 1205 | std::vector<std::uint64_t> tables_; // SetGraphicsRootDescriptorTable / SetComputeRootDescriptorTable VAs | 1207 | // SetGraphicsRootDescriptorTable / SetComputeRootDescriptorTable: VA, CBV/SRV/UAV slots the signature declares |
| 1208 | std::vector<std::pair<std::uint64_t, std::uint32_t>> tables_; | ||
| 1206 | // The span a root view may read: D3D12's largest constant buffer (4096 x 16 bytes). | 1209 | // The span a root view may read: D3D12's largest constant buffer (4096 x 16 bytes). |
| 1207 | static constexpr std::uint64_t kRootViewBytes = 65536; | 1210 | static constexpr std::uint64_t kRootViewBytes = 65536; |
| 1208 | leonida::Viewport viewport_{}; | 1211 | leonida::Viewport viewport_{}; |
| @@ -1582,10 +1585,17 @@ public: | |||
| 1582 | // Buffer V#s the title wrote into a bound table itself may name its own memory: rewritten to a GPU copy's | 1585 | // Buffer V#s the title wrote into a bound table itself may name its own memory: rewritten to a GPU copy's |
| 1583 | // console VA before the copies are refreshed for this submission (src/title_va_alias.h). | 1586 | // console VA before the copies are refreshed for this submission (src/title_va_alias.h). |
| 1584 | { | 1587 | { |
| 1585 | std::set<std::uint64_t> scanned; | 1588 | std::map<std::uint64_t, std::uint32_t> tables; // each table once, with the most slots it was bound with |
| 1586 | for (const GraphicsCommandList* list : graphics) | 1589 | for (const GraphicsCommandList* list : graphics) |
| 1587 | for (const std::uint64_t table : list->tables()) | 1590 | for (const auto& [table, descriptors] : list->tables()) |
| 1588 | if (scanned.insert(table).second) leonida::title_va_aliases().scan_table(table); | 1591 | tables[table] = std::max(tables[table], descriptors); |
| 1592 | if (!tables.empty()) { | ||
| 1593 | const auto start = std::chrono::steady_clock::now(); | ||
| 1594 | for (const auto& [table, descriptors] : tables) leonida::title_va_aliases().scan_table(table, descriptors); | ||
| 1595 | leonida::title_va_aliases().note_scan(std::uint64_t( | ||
| 1596 | std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start) | ||
| 1597 | .count())); | ||
| 1598 | } | ||
| 1589 | } | 1599 | } |
| 1590 | last_result_ = queue_.execute(recorded, [&graphics] { return pin_lists(graphics); }); | 1600 | last_result_ = queue_.execute(recorded, [&graphics] { return pin_lists(graphics); }); |
| 1591 | d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed"); | 1601 | d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed"); |
diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index 14ead7b24..d975b800a 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h | |||
| @@ -374,6 +374,9 @@ struct RootArgumentLayout { | |||
| 374 | std::uint32_t user_offset; // graphics: first dword; unused for a parameter with no dwords | 374 | std::uint32_t user_offset; // graphics: first dword; unused for a parameter with no dwords |
| 375 | std::uint32_t constant_count; // constants only | 375 | std::uint32_t constant_count; // constants only |
| 376 | std::uint32_t compute_offset = rt::rootsig::kNoSgpr; // compute: first user SGPR | 376 | std::uint32_t compute_offset = rt::rootsig::kNoSgpr; // compute: first user SGPR |
| 377 | // A table: the CBV/SRV/UAV slots its ranges declare (an unbounded range counts as 1024), for the scan of | ||
| 378 | // title-written V#s before a submission (src/title_va_alias.h). 0: a sampler table, or not a table. | ||
| 379 | std::uint32_t table_descriptors = 0; | ||
| 377 | }; | 380 | }; |
| 378 | std::vector<Parameter> parameters; | 381 | std::vector<Parameter> parameters; |
| 379 | std::uint32_t flags = 0; // D3D12_ROOT_SIGNATURE_FLAGS from the serialized blob | 382 | std::uint32_t flags = 0; // D3D12_ROOT_SIGNATURE_FLAGS from the serialized blob |
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index fdf3cc2f0..e431cf8cf 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp | |||
| @@ -101,6 +101,22 @@ std::uint64_t vsharp_address(std::uint64_t va, std::uint64_t bytes, | |||
| 101 | return to; | 101 | return to; |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | // The CBV/SRV/UAV slots a descriptor table declares: the end of its furthest range (D3D12 appends a range at | ||
| 105 | // D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND), an unbounded range counted as 1024. 0 for a sampler table or no table. | ||
| 106 | std::uint32_t table_view_descriptors(const rt::rootsig::RootParameter& param) { | ||
| 107 | if (param.type != rt::rootsig::ParamType::Table) return 0; | ||
| 108 | constexpr std::uint32_t kUnbounded = 1024; | ||
| 109 | std::uint64_t next = 0, end = 0; | ||
| 110 | for (const auto& range : param.ranges) { | ||
| 111 | const std::uint64_t start = range.offset_in_table == D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND ? next | ||
| 112 | : range.offset_in_table; | ||
| 113 | const std::uint64_t count = range.num_descriptors == UINT_MAX ? kUnbounded : range.num_descriptors; | ||
| 114 | next = start + count; | ||
| 115 | if (range.range_type != D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER) end = std::max(end, next); | ||
| 116 | } | ||
| 117 | return std::uint32_t(std::min<std::uint64_t>(end, kUnbounded)); | ||
| 118 | } | ||
| 119 | |||
| 104 | // memcpy that a page fault ends instead of killing the process: the title may decommit a page between the | 120 | // memcpy that a page fault ends instead of killing the process: the title may decommit a page between the |
| 105 | // VirtualQuery that saw it committed and the read. No C++ objects here: it is a __try frame. | 121 | // VirtualQuery that saw it committed and the read. No C++ objects here: it is a __try frame. |
| 106 | // `streaming` reads with MOVNTDQA, for write-combined pages: ordinary loads from them are not cached, and memcpy | 122 | // `streaming` reads with MOVNTDQA, for write-combined pages: ordinary loads from them are not cached, and memcpy |
| @@ -768,11 +784,15 @@ struct DeviceState { | |||
| 768 | out = va; | 784 | out = va; |
| 769 | if (!arena.mapped || va < arena.size || !context) return false; | 785 | if (!arena.mapped || va < arena.size || !context) return false; |
| 770 | if (context->knows_buffer_va(va)) return false; // an upload or readback buffer's biased VA, say | 786 | if (context->knows_buffer_va(va)) return false; // an upload or readback buffer's biased VA, say |
| 787 | auto& aliases = leonida::title_va_aliases(); | ||
| 788 | // Only the title's own memory gets a copy (xmem's ranges): an address anywhere else, the runtime's or a host | ||
| 789 | // DLL's memory included, is left as it was and counted. | ||
| 790 | std::uint64_t title_lo = 0, title_hi = 0; | ||
| 791 | if (!aliases.title_memory(va, title_lo, title_hi)) return true; | ||
| 771 | MEMORY_BASIC_INFORMATION mbi{}; | 792 | MEMORY_BASIC_INFORMATION mbi{}; |
| 772 | if (!VirtualQuery(reinterpret_cast<const void*>(static_cast<std::uintptr_t>(va)), &mbi, sizeof(mbi)) || | 793 | if (!VirtualQuery(reinterpret_cast<const void*>(static_cast<std::uintptr_t>(va)), &mbi, sizeof(mbi)) || |
| 773 | mbi.State != MEM_COMMIT || !mbi.Protect || (mbi.Protect & (PAGE_NOACCESS | PAGE_GUARD))) | 794 | mbi.State != MEM_COMMIT || !mbi.Protect || (mbi.Protect & (PAGE_NOACCESS | PAGE_GUARD))) |
| 774 | return false; | 795 | return true; |
| 775 | auto& aliases = leonida::title_va_aliases(); | ||
| 776 | std::lock_guard lock(title_window_mutex); | 796 | std::lock_guard lock(title_window_mutex); |
| 777 | if (const std::uint64_t made = aliases.shader_va(va); made != va) { // another thread made it meanwhile | 797 | if (const std::uint64_t made = aliases.shader_va(va); made != va) { // another thread made it meanwhile |
| 778 | out = made; | 798 | out = made; |
| @@ -780,6 +800,8 @@ struct DeviceState { | |||
| 780 | } | 800 | } |
| 781 | std::uint64_t lo = 0, hi = UINT64_MAX; | 801 | std::uint64_t lo = 0, hi = UINT64_MAX; |
| 782 | aliases.gap(va, lo, hi); // never over a slot-133 buffer's range or another window | 802 | aliases.gap(va, lo, hi); // never over a slot-133 buffer's range or another window |
| 803 | lo = std::max(lo, title_lo); // nor past the title's range | ||
| 804 | hi = std::min(hi, title_hi); | ||
| 783 | const std::uint64_t want = std::clamp<std::uint64_t>(bytes, 1, kTitleWindowMax); | 805 | const std::uint64_t want = std::clamp<std::uint64_t>(bytes, 1, kTitleWindowMax); |
| 784 | const std::uint64_t start = std::max(va & ~(kTitleWindowAlign - 1), lo); | 806 | const std::uint64_t start = std::max(va & ~(kTitleWindowAlign - 1), lo); |
| 785 | std::uint64_t end = va > UINT64_MAX - want - kTitleWindowAlign ? UINT64_MAX | 807 | std::uint64_t end = va > UINT64_MAX - want - kTitleWindowAlign ? UINT64_MAX |
| @@ -824,7 +846,7 @@ struct DeviceState { | |||
| 824 | // The buffer V#s of the table at `table_va` whose base is in the title's own memory get the address of a copy, | 846 | // The buffer V#s of the table at `table_va` whose base is in the title's own memory get the address of a copy, |
| 825 | // written in place: the title wrote them itself (or copied them), so no Create*View saw them. Images (T#s) and | 847 | // written in place: the title wrote them itself (or copied them), so no Create*View saw them. Images (T#s) and |
| 826 | // null slots are skipped; so is a V# already in the arena, so a rewritten one is left alone next time. | 848 | // null slots are skipped; so is a V# already in the arena, so a rewritten one is left alone next time. |
| 827 | void scan_title_table(std::uint64_t table_va) { | 849 | void scan_title_table(std::uint64_t table_va, std::uint32_t descriptors) { |
| 828 | VkDeviceSize end = 0; | 850 | VkDeviceSize end = 0; |
| 829 | { | 851 | { |
| 830 | std::lock_guard lock(cbv_heap_mutex); | 852 | std::lock_guard lock(cbv_heap_mutex); |
| @@ -832,7 +854,9 @@ struct DeviceState { | |||
| 832 | if (it == cbv_heaps.begin()) return; | 854 | if (it == cbv_heaps.begin()) return; |
| 833 | --it; | 855 | --it; |
| 834 | if (table_va >= it->second) return; | 856 | if (table_va >= it->second) return; |
| 835 | end = std::min<VkDeviceSize>(it->second, table_va + kTableScanDescriptors * kDescriptorSize); | 857 | end = std::min<VkDeviceSize>( |
| 858 | it->second, table_va + VkDeviceSize(std::min<std::uint32_t>(descriptors, kTableScanDescriptors)) * | ||
| 859 | kDescriptorSize); | ||
| 836 | } | 860 | } |
| 837 | auto& aliases = leonida::title_va_aliases(); | 861 | auto& aliases = leonida::title_va_aliases(); |
| 838 | for (VkDeviceSize at = table_va; at + 16 <= end; at += kDescriptorSize) { | 862 | for (VkDeviceSize at = table_va; at + 16 <= end; at += kDescriptorSize) { |
| @@ -5320,7 +5344,9 @@ public: | |||
| 5320 | [](void* device, std::uint64_t va, std::uint64_t bytes, std::uint64_t& out) { | 5344 | [](void* device, std::uint64_t va, std::uint64_t bytes, std::uint64_t& out) { |
| 5321 | return static_cast<DeviceState*>(device)->resolve_title_memory(va, bytes, out); | 5345 | return static_cast<DeviceState*>(device)->resolve_title_memory(va, bytes, out); |
| 5322 | }, | 5346 | }, |
| 5323 | [](void* device, std::uint64_t table) { static_cast<DeviceState*>(device)->scan_title_table(table); }); | 5347 | [](void* device, std::uint64_t table, std::uint32_t descriptors) { |
| 5348 | static_cast<DeviceState*>(device)->scan_title_table(table, descriptors); | ||
| 5349 | }); | ||
| 5324 | if (info.physical && state_.format_properties) { | 5350 | if (info.physical && state_.format_properties) { |
| 5325 | state_.context->set_color_attachment_format( | 5351 | state_.context->set_color_attachment_format( |
| 5326 | [physical = info.physical, get_props = state_.format_properties](VkFormat format) { | 5352 | [physical = info.physical, get_props = state_.format_properties](VkFormat format) { |
| @@ -6241,7 +6267,8 @@ public: | |||
| 6241 | break; | 6267 | break; |
| 6242 | } | 6268 | } |
| 6243 | layout.parameters.push_back({parsed.sig.params[i].type, first, parsed.sig.params[i].num_values, | 6269 | layout.parameters.push_back({parsed.sig.params[i].type, first, parsed.sig.params[i].num_values, |
| 6244 | compute.ok ? compute.first_sgpr[i] : rt::rootsig::kNoSgpr}); | 6270 | compute.ok ? compute.first_sgpr[i] : rt::rootsig::kNoSgpr, |
| 6271 | table_view_descriptors(parsed.sig.params[i])}); | ||
| 6245 | } | 6272 | } |
| 6246 | register_static_samplers(parsed.sig); | 6273 | register_static_samplers(parsed.sig); |
| 6247 | auto* signature = new RootSignature( | 6274 | auto* signature = new RootSignature( |
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 37f29459f..cccb4002f 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -35,6 +35,7 @@ | |||
| 35 | 35 | ||
| 36 | #include "shims.h" | 36 | #include "shims.h" |
| 37 | #include "startup_trace.h" | 37 | #include "startup_trace.h" |
| 38 | #include "../src/title_va_alias.h" | ||
| 38 | 39 | ||
| 39 | namespace { | 40 | namespace { |
| 40 | 41 | ||
| @@ -66,14 +67,27 @@ void note_attributes(const char* api, uint64_t attributes, size_t size, const vo | |||
| 66 | 67 | ||
| 67 | void* allocate_block(size_t size) { return _aligned_malloc(size ? size : 1, kAlign); } | 68 | void* allocate_block(size_t size) { return _aligned_malloc(size ? size : 1, kAlign); } |
| 68 | 69 | ||
| 70 | // Every block and range the title gets here is recorded as the title's memory (src/title_va_alias.h): a shader | ||
| 71 | // address into it can be given a GPU copy, and an address anywhere else cannot. | ||
| 72 | void* title_block(size_t size) { | ||
| 73 | void* p = allocate_block(size); | ||
| 74 | leonida::title_va_aliases().add_title_memory(reinterpret_cast<uintptr_t>(p), size ? size : 1); | ||
| 75 | return p; | ||
| 76 | } | ||
| 77 | |||
| 78 | void free_title_block(void* p) { | ||
| 79 | if (p) leonida::title_va_aliases().remove_title_memory(reinterpret_cast<uintptr_t>(p)); | ||
| 80 | _aligned_free(p); | ||
| 81 | } | ||
| 82 | |||
| 69 | void* __stdcall XMemAlloc(size_t size, uint64_t attributes) { | 83 | void* __stdcall XMemAlloc(size_t size, uint64_t attributes) { |
| 70 | note_attributes("XMemAlloc", attributes, size, _ReturnAddress()); | 84 | note_attributes("XMemAlloc", attributes, size, _ReturnAddress()); |
| 71 | return allocate_block(size); | 85 | return title_block(size); |
| 72 | } | 86 | } |
| 73 | 87 | ||
| 74 | void __stdcall XMemFree(void* p, uint64_t attributes) { | 88 | void __stdcall XMemFree(void* p, uint64_t attributes) { |
| 75 | note_attributes("XMemFree", attributes, kNoSize, _ReturnAddress()); | 89 | note_attributes("XMemFree", attributes, kNoSize, _ReturnAddress()); |
| 76 | _aligned_free(p); | 90 | free_title_block(p); |
| 77 | } | 91 | } |
| 78 | 92 | ||
| 79 | // XMemAllocDefault / XMemFreeDefault: the GDK's own allocator behind XMemAlloc and XMemFree, which a title that hooks | 93 | // XMemAllocDefault / XMemFreeDefault: the GDK's own allocator behind XMemAlloc and XMemFree, which a title that hooks |
| @@ -82,12 +96,12 @@ void __stdcall XMemFree(void* p, uint64_t attributes) { | |||
| 82 | // released by either free. | 96 | // released by either free. |
| 83 | void* __stdcall XMemAllocDefault(size_t size, uint64_t attributes) { | 97 | void* __stdcall XMemAllocDefault(size_t size, uint64_t attributes) { |
| 84 | note_attributes("XMemAllocDefault", attributes, size, _ReturnAddress()); | 98 | note_attributes("XMemAllocDefault", attributes, size, _ReturnAddress()); |
| 85 | return allocate_block(size); | 99 | return title_block(size); |
| 86 | } | 100 | } |
| 87 | 101 | ||
| 88 | void __stdcall XMemFreeDefault(void* p, uint64_t attributes) { | 102 | void __stdcall XMemFreeDefault(void* p, uint64_t attributes) { |
| 89 | note_attributes("XMemFreeDefault", attributes, kNoSize, _ReturnAddress()); | 103 | note_attributes("XMemFreeDefault", attributes, kNoSize, _ReturnAddress()); |
| 90 | _aligned_free(p); | 104 | free_title_block(p); |
| 91 | } | 105 | } |
| 92 | 106 | ||
| 93 | // BOOL XMemIsToolsMemAvailable(void). The measured title calls it from one helper, which six out-of-memory paths reach | 107 | // BOOL XMemIsToolsMemAvailable(void). The measured title calls it from one helper, which six out-of-memory paths reach |
| @@ -320,8 +334,18 @@ PVOID reserve_placeholder(void* address, SIZE_T size, SIZE_T alignment) { | |||
| 320 | // XMemMapPhysicalPages. The guest page size sets the alignment and rounding of the region; the host's own pages stay | 334 | // XMemMapPhysicalPages. The guest page size sets the alignment and rounding of the region; the host's own pages stay |
| 321 | // 4 KiB (host large pages need SeLockMemoryPrivilege). A 2 MiB region the host places is reserved 2 MiB-aligned through | 335 | // 4 KiB (host large pages need SeLockMemoryPrivilege). A 2 MiB region the host places is reserved 2 MiB-aligned through |
| 322 | // VirtualAlloc2. | 336 | // VirtualAlloc2. |
| 337 | void* xmem_virtual_alloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect); | ||
| 338 | |||
| 323 | void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) { | 339 | void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) { |
| 324 | note_attributes("XMemVirtualAlloc", attributes, size, _ReturnAddress()); | 340 | note_attributes("XMemVirtualAlloc", attributes, size, _ReturnAddress()); |
| 341 | void* result = xmem_virtual_alloc(address, size, allocation_type, attributes, protect); | ||
| 342 | // The title's memory (src/title_va_alias.h). Never forgotten: a released range is no longer committed, which | ||
| 343 | // the copy also checks. | ||
| 344 | if (result) leonida::title_va_aliases().add_title_memory(reinterpret_cast<uintptr_t>(result), size); | ||
| 345 | return result; | ||
| 346 | } | ||
| 347 | |||
| 348 | void* xmem_virtual_alloc(void* address, SIZE_T size, DWORD allocation_type, ULONGLONG attributes, DWORD protect) { | ||
| 325 | if (size == 0) { | 349 | if (size == 0) { |
| 326 | SetLastError(ERROR_INVALID_PARAMETER); | 350 | SetLastError(ERROR_INVALID_PARAMETER); |
| 327 | return nullptr; | 351 | return nullptr; |
| @@ -689,7 +713,9 @@ constexpr DWORD kMemGraphics = 0x10000000; | |||
| 689 | constexpr DWORD kMemTitle = 0x40000000; | 713 | constexpr DWORD kMemTitle = 0x40000000; |
| 690 | 714 | ||
| 691 | LPVOID WINAPI TitleVirtualAlloc(LPVOID address, SIZE_T size, DWORD allocation_type, DWORD protect) { | 715 | LPVOID WINAPI TitleVirtualAlloc(LPVOID address, SIZE_T size, DWORD allocation_type, DWORD protect) { |
| 692 | return VirtualAlloc(address, size, allocation_type & ~(kMemGraphics | kMemTitle), host_protect(protect)); | 716 | LPVOID result = VirtualAlloc(address, size, allocation_type & ~(kMemGraphics | kMemTitle), host_protect(protect)); |
| 717 | if (result) leonida::title_va_aliases().add_title_memory(reinterpret_cast<uintptr_t>(result), size); | ||
| 718 | return result; | ||
| 693 | } | 719 | } |
| 694 | 720 | ||
| 695 | } // namespace | 721 | } // namespace |
diff --git a/runtime/src/title_va_alias.h b/runtime/src/title_va_alias.h index 6f665f0ec..1e907ea43 100644 --- a/runtime/src/title_va_alias.h +++ b/runtime/src/title_va_alias.h | |||
| @@ -37,7 +37,7 @@ public: | |||
| 37 | // An address in the title's memory: true, with `shader_va` what a shader must be given (the address itself when | 37 | // An address in the title's memory: true, with `shader_va` what a shader must be given (the address itself when |
| 38 | // no copy could be made). False: not the title's memory (an arena VA, a buffer of the layer's). | 38 | // no copy could be made). False: not the title's memory (an arena VA, a buffer of the layer's). |
| 39 | using Resolver = bool (*)(void* context, std::uint64_t va, std::uint64_t bytes, std::uint64_t& shader_va); | 39 | using Resolver = bool (*)(void* context, std::uint64_t va, std::uint64_t bytes, std::uint64_t& shader_va); |
| 40 | using TableScanner = void (*)(void* context, std::uint64_t table_va); | 40 | using TableScanner = void (*)(void* context, std::uint64_t table_va, std::uint32_t descriptors); |
| 41 | 41 | ||
| 42 | // [title_va, title_va + bytes) is reached as [shader_va, shader_va + bytes). A range that overlaps a live one | 42 | // [title_va, title_va + bytes) is reached as [shader_va, shader_va + bytes). A range that overlaps a live one |
| 43 | // replaces it (the title has placed a new buffer over pages it released). False for an empty or wrapping range. | 43 | // replaces it (the title has placed a new buffer over pages it released). False for an empty or wrapping range. |
| @@ -109,9 +109,9 @@ public: | |||
| 109 | if (va && shader_va(va) != va) untranslated_[kind].fetch_add(1, std::memory_order_relaxed); | 109 | if (va && shader_va(va) != va) untranslated_[kind].fetch_add(1, std::memory_order_relaxed); |
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | // Before a submission: the device rewrites the buffer V#s of the table at `table_va` (a console VA in one of its | 112 | // Before a submission: the device rewrites the buffer V#s of the `descriptors` slots the root signature declares |
| 113 | // shader-visible CBV/SRV/UAV heaps). | 113 | // for the table at `table_va` (a console VA in one of its shader-visible CBV/SRV/UAV heaps). |
| 114 | void scan_table(std::uint64_t table_va) { | 114 | void scan_table(std::uint64_t table_va, std::uint32_t descriptors) { |
| 115 | TableScanner scanner; | 115 | TableScanner scanner; |
| 116 | void* context; | 116 | void* context; |
| 117 | { | 117 | { |
| @@ -119,7 +119,52 @@ public: | |||
| 119 | scanner = scanner_; | 119 | scanner = scanner_; |
| 120 | context = hook_context_; | 120 | context = hook_context_; |
| 121 | } | 121 | } |
| 122 | if (scanner) scanner(context, table_va); | 122 | if (scanner) scanner(context, table_va, descriptors); |
| 123 | } | ||
| 124 | |||
| 125 | // The scan's cost, per ExecuteCommandLists that had tables to scan. | ||
| 126 | void note_scan(std::uint64_t nanoseconds) { | ||
| 127 | scans_.fetch_add(1, std::memory_order_relaxed); | ||
| 128 | scan_ns_.fetch_add(nanoseconds, std::memory_order_relaxed); | ||
| 129 | std::uint64_t seen = scan_max_ns_.load(std::memory_order_relaxed); | ||
| 130 | while (nanoseconds > seen && !scan_max_ns_.compare_exchange_weak(seen, nanoseconds)) { | ||
| 131 | } | ||
| 132 | } | ||
| 133 | std::uint64_t scans() const { return scans_.load(std::memory_order_relaxed); } | ||
| 134 | std::uint64_t scan_ns() const { return scan_ns_.load(std::memory_order_relaxed); } | ||
| 135 | std::uint64_t scan_max_ns() const { return scan_max_ns_.load(std::memory_order_relaxed); } | ||
| 136 | |||
| 137 | // The title's own memory, as the xmem shims hand it out (XMemAlloc, XMemVirtualAlloc, the title's VirtualAlloc): | ||
| 138 | // only an address in one of these ranges gets a copy. A base anywhere else (the runtime's own heap, a host DLL) | ||
| 139 | // is left as it was and counted. | ||
| 140 | void add_title_memory(std::uint64_t base, std::uint64_t bytes) { | ||
| 141 | if (!base || !bytes || base > UINT64_MAX - bytes) return; | ||
| 142 | std::uint64_t begin = base, end = base + bytes; | ||
| 143 | std::unique_lock lock(memory_mutex_); | ||
| 144 | // A commit inside a reserved range, or two that overlap, are one range. | ||
| 145 | auto it = memory_.upper_bound(begin); | ||
| 146 | if (it != memory_.begin() && std::prev(it)->second > begin) --it; | ||
| 147 | while (it != memory_.end() && it->first < end) { | ||
| 148 | begin = std::min(begin, it->first); | ||
| 149 | end = std::max(end, it->second); | ||
| 150 | it = memory_.erase(it); | ||
| 151 | } | ||
| 152 | memory_[begin] = end; | ||
| 153 | } | ||
| 154 | void remove_title_memory(std::uint64_t base) { | ||
| 155 | std::unique_lock lock(memory_mutex_); | ||
| 156 | memory_.erase(base); | ||
| 157 | } | ||
| 158 | // The title range that holds `va`: [lo, hi). False when none does. | ||
| 159 | bool title_memory(std::uint64_t va, std::uint64_t& lo, std::uint64_t& hi) const { | ||
| 160 | std::shared_lock lock(memory_mutex_); | ||
| 161 | auto it = memory_.upper_bound(va); | ||
| 162 | if (it == memory_.begin()) return false; | ||
| 163 | --it; | ||
| 164 | if (va >= it->second) return false; | ||
| 165 | lo = it->first; | ||
| 166 | hi = it->second; | ||
| 167 | return true; | ||
| 123 | } | 168 | } |
| 124 | 169 | ||
| 125 | // The first device to register keeps the hooks until it clears them. | 170 | // The first device to register keeps the hooks until it clears them. |
| @@ -166,6 +211,9 @@ private: | |||
| 166 | TableScanner scanner_ = nullptr; | 211 | TableScanner scanner_ = nullptr; |
| 167 | std::array<std::atomic<std::uint64_t>, kind_count> translated_{}; | 212 | std::array<std::atomic<std::uint64_t>, kind_count> translated_{}; |
| 168 | std::array<std::atomic<std::uint64_t>, kind_count> untranslated_{}; | 213 | std::array<std::atomic<std::uint64_t>, kind_count> untranslated_{}; |
| 214 | std::atomic<std::uint64_t> scans_{0}, scan_ns_{0}, scan_max_ns_{0}; | ||
| 215 | mutable std::shared_mutex memory_mutex_; | ||
| 216 | std::map<std::uint64_t, std::uint64_t> memory_; // the title's memory: base -> end | ||
| 169 | }; | 217 | }; |
| 170 | 218 | ||
| 171 | // The process's table: the device's title ranges and hooks, read by the command lists and descriptor writers. | 219 | // The process's table: the device's title ranges and hooks, read by the command lists and descriptor writers. |
diff --git a/runtime/test/d3d12_title_va_test.cpp b/runtime/test/d3d12_title_va_test.cpp index d971c716e..7be1e880b 100644 --- a/runtime/test/d3d12_title_va_test.cpp +++ b/runtime/test/d3d12_title_va_test.cpp | |||
| @@ -301,7 +301,7 @@ int check_kind(Context& c, const char* kind, DWORD protect, int& checks) { | |||
| 301 | // - a V# made by CreateConstantBufferView (translated when written); | 301 | // - a V# made by CreateConstantBufferView (translated when written); |
| 302 | // - a root view (translated when set). | 302 | // - a root view (translated when set). |
| 303 | // Negative controls: a raw V# whose base is an arena buffer and a T#-shaped slot whose base is the ring keep their | 303 | // Negative controls: a raw V# whose base is an arena buffer and a T#-shaped slot whose base is the ring keep their |
| 304 | // bytes; an address the ring does not hold is left alone. | 304 | // bytes; committed memory that is not the title's, and a title range with no committed page, are left alone. |
| 305 | int check_title_memory(Context& c, int& checks) { | 305 | int check_title_memory(Context& c, int& checks) { |
| 306 | constexpr UINT64 ring_bytes = 256 * 1024; | 306 | constexpr UINT64 ring_bytes = 256 * 1024; |
| 307 | auto* ring = static_cast<std::uint32_t*>(VirtualAlloc(nullptr, ring_bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 307 | auto* ring = static_cast<std::uint32_t*>(VirtualAlloc(nullptr, ring_bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| @@ -309,6 +309,7 @@ int check_title_memory(Context& c, int& checks) { | |||
| 309 | const auto ring_va = reinterpret_cast<std::uint64_t>(ring); | 309 | const auto ring_va = reinterpret_cast<std::uint64_t>(ring); |
| 310 | EXPECT(ring_va >= c.arena); | 310 | EXPECT(ring_va >= c.arena); |
| 311 | auto& aliases = leonida::title_va_aliases(); | 311 | auto& aliases = leonida::title_va_aliases(); |
| 312 | aliases.add_title_memory(ring_va, ring_bytes); // as the xmem shims record what they hand the title | ||
| 312 | const std::uint64_t table_before = aliases.translated(leonida::TitleVaAliases::table_view); | 313 | const std::uint64_t table_before = aliases.translated(leonida::TitleVaAliases::table_view); |
| 313 | const std::uint64_t created_before = aliases.translated(leonida::TitleVaAliases::created_view); | 314 | const std::uint64_t created_before = aliases.translated(leonida::TitleVaAliases::created_view); |
| 314 | const std::uint64_t root_before = aliases.translated(leonida::TitleVaAliases::root_view); | 315 | const std::uint64_t root_before = aliases.translated(leonida::TitleVaAliases::root_view); |
| @@ -433,14 +434,24 @@ int check_title_memory(Context& c, int& checks) { | |||
| 433 | static_cast<unsigned long long>(aliases.translated(leonida::TitleVaAliases::root_view) - root_before)); | 434 | static_cast<unsigned long long>(aliases.translated(leonida::TitleVaAliases::root_view) - root_before)); |
| 434 | EXPECT(raw_ok == kElements && cbv_ok == kElements && root_ok == kElements); | 435 | EXPECT(raw_ok == kElements && cbv_ok == kElements && root_ok == kElements); |
| 435 | EXPECT(rewritten && arena_kept && image_kept); | 436 | EXPECT(rewritten && arena_kept && image_kept); |
| 436 | // An address in no committed page is not title memory: left alone, nothing made for it. | 437 | // Left alone and counted: committed memory that is not the title's (the runtime's own, say), and a title range |
| 438 | // with no committed page. | ||
| 439 | const std::uint64_t untranslated_before = aliases.untranslated(leonida::TitleVaAliases::created_view); | ||
| 440 | void* host = VirtualAlloc(nullptr, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | ||
| 437 | void* gone = VirtualAlloc(nullptr, 65536, MEM_RESERVE, PAGE_READWRITE); | 441 | void* gone = VirtualAlloc(nullptr, 65536, MEM_RESERVE, PAGE_READWRITE); |
| 438 | const auto gone_va = reinterpret_cast<std::uint64_t>(gone); | 442 | const auto host_va = reinterpret_cast<std::uint64_t>(host), gone_va = reinterpret_cast<std::uint64_t>(gone); |
| 439 | const D3D12_CONSTANT_BUFFER_VIEW_DESC reserved_only{gone_va, 256}; | 443 | aliases.add_title_memory(gone_va, 65536); |
| 440 | c.device->CreateConstantBufferView(&reserved_only, D3D12_CPU_DESCRIPTOR_HANDLE{cpu + 5 * inc}); | 444 | const D3D12_CONSTANT_BUFFER_VIEW_DESC not_title{host_va, 256}, reserved_only{gone_va, 256}; |
| 441 | EXPECT(vsharp_base(cpu + 5 * inc) == (gone_va & 0xFFFFFFFFFFFFull)); | 445 | c.device->CreateConstantBufferView(¬_title, D3D12_CPU_DESCRIPTOR_HANDLE{cpu + 5 * inc}); |
| 446 | c.device->CreateConstantBufferView(&reserved_only, D3D12_CPU_DESCRIPTOR_HANDLE{cpu + 6 * inc}); | ||
| 447 | EXPECT(vsharp_base(cpu + 5 * inc) == (host_va & 0xFFFFFFFFFFFFull)); | ||
| 448 | EXPECT(vsharp_base(cpu + 6 * inc) == (gone_va & 0xFFFFFFFFFFFFull)); | ||
| 449 | EXPECT(aliases.untranslated(leonida::TitleVaAliases::created_view) - untranslated_before == 2); | ||
| 450 | aliases.remove_title_memory(gone_va); | ||
| 451 | aliases.remove_title_memory(ring_va); | ||
| 452 | VirtualFree(host, 0, MEM_RELEASE); | ||
| 442 | VirtualFree(gone, 0, MEM_RELEASE); | 453 | VirtualFree(gone, 0, MEM_RELEASE); |
| 443 | checks += 3; | 454 | checks += 4; |
| 444 | return 0; | 455 | return 0; |
| 445 | } | 456 | } |
| 446 | 457 | ||
diff --git a/runtime/test/title_va_alias_test.cpp b/runtime/test/title_va_alias_test.cpp index e27faa8f6..84758dbc9 100644 --- a/runtime/test/title_va_alias_test.cpp +++ b/runtime/test/title_va_alias_test.cpp | |||
| @@ -40,6 +40,14 @@ int main() { | |||
| 40 | a.note(title + 8, leonida::TitleVaAliases::vertex_view); | 40 | a.note(title + 8, leonida::TitleVaAliases::vertex_view); |
| 41 | a.note(title - 8, leonida::TitleVaAliases::vertex_view); | 41 | a.note(title - 8, leonida::TitleVaAliases::vertex_view); |
| 42 | EXPECT(a.untranslated(leonida::TitleVaAliases::vertex_view) == 1); | 42 | EXPECT(a.untranslated(leonida::TitleVaAliases::vertex_view) == 1); |
| 43 | // The title's memory: a commit inside a reservation is one range; outside every range is not the title's. | ||
| 44 | std::uint64_t mlo = 0, mhi = 0; | ||
| 45 | a.add_title_memory(0x40000000, 0x100000); | ||
| 46 | a.add_title_memory(0x40010000, 0x1000); | ||
| 47 | EXPECT(a.title_memory(0x40080000, mlo, mhi) && mlo == 0x40000000 && mhi == 0x40100000); | ||
| 48 | EXPECT(!a.title_memory(0x40100000, mlo, mhi) && !a.title_memory(0x3FFFFFFF, mlo, mhi)); | ||
| 49 | a.remove_title_memory(0x40000000); | ||
| 50 | EXPECT(!a.title_memory(0x40080000, mlo, mhi)); | ||
| 43 | // A wrapping target (an import below heap_base) is kept as is: modular arithmetic. | 51 | // A wrapping target (an import below heap_base) is kept as is: modular arithmetic. |
| 44 | constexpr std::uint64_t below = 0xFFFFFFFFFFF00000ull; | 52 | constexpr std::uint64_t below = 0xFFFFFFFFFFF00000ull; |
| 45 | EXPECT(a.add(title + 0x100000, size, below) && a.shader_va(title + 0x100010) == below + 0x10); | 53 | EXPECT(a.add(title + 0x100000, size, below) && a.shader_va(title + 0x100010) == below + 0x10); |