diff --git a/docs/boot.md b/docs/boot.md index 7ec69f0314..f012a910fa 100644 --- a/docs/boot.md +++ b/docs/boot.md @@ -86,6 +86,10 @@ Useful switches: implicit layers add swapchain usage that shows up as validation errors ([testing.md](testing.md), "Known problems"). * `--overlay DIR` sends the guest's writes to a private folder instead of the package. +* `LEONIDA_CONSOLE_DESCRIPTOR_HEAP=1` (Vulkan backend) makes device slot 14 read a console address and size after + the 16-byte heap description and report that address as the heap's start, which the retail executable asserts + on ([retail_boot_chain.md](retail_boot_chain.md), stop 13). Off by default: a caller that passes only the PC + description has unrelated bytes there, and the descriptors would be written to whatever address they spell. ## 3. Triage order diff --git a/runtime/shims/acphal.cpp b/runtime/shims/acphal.cpp index 553f57f207..6014fab980 100644 --- a/runtime/shims/acphal.cpp +++ b/runtime/shims/acphal.cpp @@ -141,7 +141,9 @@ struct SHAPE_XMA_CONTEXT { uint32_t reserved1 : 6; uint32_t loopStartOffset : 26; uint32_t loopEndOffset : 6; - uint32_t reserved[10]; + // The bit-fields above fill five words (20 bytes); the rest of the GDK's 64 bytes is reserved here. Only the + // size is used (the context array's stride); the field offsets are not checked against a console. + uint32_t reserved[11]; }; static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK"); @@ -162,7 +164,9 @@ struct SHAPE_PCM_CONTEXT { uint32_t reserved6 : 15; uint32_t format : 2; uint32_t reserved7 : 14; - uint32_t reserved[1]; + // The bit-fields above fill six words (24 bytes); the rest of the GDK's 32 bytes is reserved here. Only the + // size is used (the context array's stride). + uint32_t reserved[2]; }; static_assert(sizeof(SHAPE_PCM_CONTEXT) == 32, "SHAPE_PCM_CONTEXT is 32 bytes per GDK"); diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index fc4b966c24..da6375e9c1 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5012,6 +5012,17 @@ class Device final // standalone device gta6rt --run uses both come through here (found in review: registering only in // create_vulkan_device left the retail path without it). rt::d3d12_xs::set_placed_at_va_handler(&create_buffer_at_host_address); + // Slot 14's console-extended description reaches create_console_descriptor_heap through a handler too: the + // proxy is also built into programs without this layer, so it cannot call it by name. It is registered on + // request (LEONIDA_CONSOLE_DESCRIPTOR_HEAP=1), not always: the proxy tells a console description from a PC + // one by the 16 bytes after it, and after a PC caller's 16-byte description those are whatever follows it + // in memory. Always on, 21 of 294 runtime tests fail (9 crash) writing descriptors to such an address. + { + char text[8] = {}; + size_t length = 0; + if (getenv_s(&length, text, "LEONIDA_CONSOLE_DESCRIPTOR_HEAP") == 0 && length > 1 && text[0] != '0') + rt::d3d12_xs::set_console_descriptor_heap_handler(&rt::d3d12::create_console_descriptor_heap); + } state_.info = info; queue_users_.resize(info.command_queues.size(), 0); compute_users_.resize(info.compute_queues.size(), 0); @@ -5969,6 +5980,20 @@ class Device final return d3d12_trace::result(method, hand_out(heap, riid, out)); } + // The heap of create_console_descriptor_heap (Xbox device slot 14): it reports `console_address` as its start. + // A member because the heap takes the device's state and identity, as CreateDescriptorHeap's does; the caller + // has applied CreateDescriptorHeap's rules to `desc`. + HRESULT console_descriptor_heap(const D3D12_DESCRIPTOR_HEAP_DESC& desc, std::uint64_t console_address, + std::uint64_t console_size, REFIID riid, void** out) { + auto* heap = new DescriptorHeap(state_, desc, console_address, console_size); + heap->attach_device(identity()); + if (!heap->ok()) { + heap->Release(); + return E_OUTOFMEMORY; + } + return hand_out(heap, riid, out); + } + HRESULT STDMETHODCALLTYPE CreateQueryHeap(const D3D12_QUERY_HEAP_DESC* desc, REFIID riid, void** out) override { if (out) *out = nullptr; // The host runtime's order (compare_invalid_arguments, "query heap:"): the description first, refused with @@ -9991,14 +10016,7 @@ HRESULT create_console_descriptor_heap(ID3D12Device* device, const D3D12_DESCRIP ID3D12Device* real = rt::d3d12_xs::device_proxy_real_device(device); auto* d = Device::from(real ? real : device); if (!d) return E_INVALIDARG; - - auto* heap = new DescriptorHeap(d->state(), *desc, console_address, console_size); - heap->attach_device(d->identity()); - if (!heap->ok()) { - heap->Release(); - return E_OUTOFMEMORY; - } - return hand_out(heap, riid, out); + return d->console_descriptor_heap(*desc, console_address, console_size, riid, out); } } // namespace rt::d3d12 diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 187406e3d8..e866e5cb4d 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -1579,6 +1579,9 @@ static bool readable_range(const void* ptr, std::size_t bytes) { return true; } +// The handler slot 14 calls for a console-extended description (set_console_descriptor_heap_handler). +std::atomic g_console_descriptor_heap{nullptr}; + HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_HEAP_DESC* desc, const IID* riid, void** out) { note_slot(14); @@ -1601,11 +1604,13 @@ HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_H std::memcpy(&size, bytes + sizeof(D3D12_DESCRIPTOR_HEAP_DESC) + sizeof(address), sizeof(size)); // Console-extended slot 14 (issue #313): address/size follow the PC desc. Only take this path when a // non-zero console VA is present; normal PC 16-byte descs never enter it. - if (address != 0) { + // The heap at a console address is made by the device behind the proxy, through the handler it + // registered (set_console_descriptor_heap_handler); without one the PC path below creates the heap. + const ConsoleDescriptorHeapHandler handler = g_console_descriptor_heap.load(std::memory_order_acquire); + if (address != 0 && handler) { console_extended = true; - hr = rt::d3d12::create_console_descriptor_heap( - self->real, desc, static_cast(address), size, - __uuidof(ID3D12DescriptorHeap), &created); + hr = handler(self->real, desc, static_cast(address), size, + __uuidof(ID3D12DescriptorHeap), &created); } } } @@ -2041,6 +2046,10 @@ void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook) { g_placed_at_va_pages.store(hook, std::memory_order_release); } +void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler) { + g_console_descriptor_heap.store(handler, std::memory_order_release); +} + ID3D12Device* device_proxy_real_device(const void* device) { if (!is_device_proxy(device)) return nullptr; return static_cast(device)->real; diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index ac0437215d..86fee802ee 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -108,6 +108,17 @@ void set_placed_at_va_handler(PlacedAtVaHandler handler); using PlacedAtVaPagesHook = bool (*)(const void* va, std::size_t bytes); void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook); +// Device slot 14 with the console-extended description (a console address and a size after the 16 PC bytes) makes +// the heap at that address. The proxy does not create it itself: the device behind it does, through this handler, +// which the Vulkan layer registers on request (rt::d3d12::create_console_descriptor_heap, with +// LEONIDA_CONSOLE_DESCRIPTOR_HEAP=1). This file is also built into programs without that layer, so it cannot call +// the layer by name. With none registered the slot creates the heap as the PC CreateDescriptorHeap does, from the +// 16 PC bytes alone: the bytes after a PC caller's description are not an address. +using ConsoleDescriptorHeapHandler = HRESULT (*)(ID3D12Device* real, const D3D12_DESCRIPTOR_HEAP_DESC* desc, + D3D12_GPU_VIRTUAL_ADDRESS console_address, + std::uint64_t console_size, REFIID riid, void** out); +void set_console_descriptor_heap_handler(ConsoleDescriptorHeapHandler handler); + // True for slots the proxy handles itself rather than forwarding verbatim: IUnknown (0-2), 8 and 75 (queue // proxies), 9 (allocator proxies), 14, 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 48/73 and // 78/79/80 (the Xbox slots above, at their configured indices), 62, 67 and 76. Slot 10 is intentionally false: the diff --git a/runtime/test/d3d12_console_descriptor_heap_test.cpp b/runtime/test/d3d12_console_descriptor_heap_test.cpp index 5f694b5a2a..17e1498a1b 100644 --- a/runtime/test/d3d12_console_descriptor_heap_test.cpp +++ b/runtime/test/d3d12_console_descriptor_heap_test.cpp @@ -24,6 +24,7 @@ struct Ref { return &p; } T* get() const { return p; } + T* operator->() const { return p; } }; bool check_console_addressed_heap(ID3D12Device* device) { @@ -63,9 +64,13 @@ bool check_console_addressed_heap(ID3D12Device* device) { return true; } -bool check_xs_proxy_slot14(ID3D12Device* real_device) { +// `console_path`: whether the layer's handler is registered, so slot 14 takes the address after the description. +bool check_xs_proxy_slot14(ID3D12Device* real_device, bool console_path) { + // create_device_proxy takes over one reference to the device on success; the caller keeps its own. + real_device->AddRef(); void* proxy_raw = nullptr; - if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, IID_PPV_ARGS(&proxy_raw))) || !proxy_raw) { + if (FAILED(rt::d3d12_xs::create_device_proxy(real_device, __uuidof(ID3D12Device), &proxy_raw)) || !proxy_raw) { + real_device->Release(); std::fprintf(stderr, "create_device_proxy failed\n"); return false; } @@ -90,7 +95,22 @@ bool check_xs_proxy_slot14(ID3D12Device* real_device) { static_cast(hr)); return false; } - const auto cpu = heap->GetCPUDescriptorHandleForHeapStart(); + // Slot 14 hands out a heap proxy, whose slots 9 and 10 return the handle in RAX as the console does: a PC call + // of them would read a result buffer the proxy never writes. The heap behind it is read instead. + if (!rt::d3d12_xs::is_heap_proxy(heap.get())) { + std::fprintf(stderr, "proxy CreateDescriptorHeap(console) did not return a heap proxy\n"); + return false; + } + const auto cpu = rt::d3d12_xs::heap_proxy_real_heap(heap.get())->GetCPUDescriptorHandleForHeapStart(); + if (!console_path) { + // No handler: the 16 PC bytes make an ordinary heap, with storage of its own. + if (cpu.ptr == 0 || cpu.ptr == static_cast(console.address)) { + std::fprintf(stderr, "proxy heap without the handler starts at 0x%llx\n", + static_cast(cpu.ptr)); + return false; + } + return true; + } if (cpu.ptr != static_cast(console.address)) { std::fprintf(stderr, "proxy CPU heap start mismatch: got 0x%llx want 0x%llx\n", static_cast(cpu.ptr), @@ -142,7 +162,14 @@ int main() { if (!check_normal_heap_unchanged(device.get())) return 1; if (!check_console_addressed_heap(device.get())) return 1; - if (!check_xs_proxy_slot14(device.get())) return 1; + // The proxy takes the console path only through the handler the layer registers on request + // (LEONIDA_CONSOLE_DESCRIPTOR_HEAP=1). Both states are checked here, whatever the environment holds. + rt::d3d12_xs::set_console_descriptor_heap_handler(nullptr); + if (!check_xs_proxy_slot14(device.get(), false)) return 1; + rt::d3d12_xs::set_console_descriptor_heap_handler(&rt::d3d12::create_console_descriptor_heap); + const bool console_ok = check_xs_proxy_slot14(device.get(), true); + rt::d3d12_xs::set_console_descriptor_heap_handler(nullptr); + if (!console_ok) return 1; std::fprintf(stderr, "ok\n"); return 0; }