diff options
Diffstat (limited to 'runtime/shims/xapu_chad.cpp')
| -rw-r--r-- | runtime/shims/xapu_chad.cpp | 123 |
1 files changed, 5 insertions, 118 deletions
diff --git a/runtime/shims/xapu_chad.cpp b/runtime/shims/xapu_chad.cpp index 35bda2b72..e46a61c97 100644 --- a/runtime/shims/xapu_chad.cpp +++ b/runtime/shims/xapu_chad.cpp | |||
| @@ -1,24 +1,8 @@ | |||
| 1 | // xapu.dll and chad.dll: the console's audio processing unit (APU) and audio DSP command queues. No PC has either, | 1 | // chad.dll: the console's audio DSP command queue. No PC has one, so this gives the title a connection whose calls |
| 2 | // so these give the title connections whose calls succeed, a zeroed shared buffer, a result for each stream open and | 2 | // succeed and zeroed DSP status: nothing the title submits is processed. xapu.dll, the APU command queue, is in |
| 3 | // close, and zeroed DSP status: the audio the APU would render stays silent, and nothing the title submits is processed. | 3 | // xapu_queue.cpp. |
| 4 | // | 4 | // |
| 5 | // Read from the executable's call sites (counts in docs/runtime_imports.md), statically, never run: | 5 | // Read from the executable's call sites (counts in docs/runtime_imports.md), statically, never run: |
| 6 | // XApuConnect(block24*, out16*, handle*) 1 site; success is SUCCEEDED(hr). The first qword of the 16-byte | ||
| 7 | // output is read later as the base of a CPU-visible buffer: per-stream | ||
| 8 | // input at base + slot * 0x280 and output at base + 0x14000 + | ||
| 9 | // slot * 0x1E00. The first argument is a 24-byte global of the title | ||
| 10 | // that other title code also addresses; whether it is an input or an | ||
| 11 | // output is not known, so it is left untouched. | ||
| 12 | // XApuEnqueueCommand(handle, cmd*, 0) 4 sites; the return value is never read. cmd's first dword is a type | ||
| 13 | // (1, 2 and 3 seen), the second a stream slot. | ||
| 14 | // XApuGetResultCount(handle, 0) 1 site; 0 skips the loop that dequeues. | ||
| 15 | // XApuDequeueResult(handle, out*) 1 site, called count times into a zeroed 0x50-byte local; a negative | ||
| 16 | // HRESULT skips that result. The loop reads a type at +0, a stream slot | ||
| 17 | // at +4 and a dword at +0x34. Its per-slot state machine takes a type 1 | ||
| 18 | // result as "opened" and returns the slot to the title's free list only | ||
| 19 | // on a type 2 result, after the title enqueued a type 2 command. | ||
| 20 | // XApuDisconnect(handle, 0x20) 1 site; the return value is never read. The constant second | ||
| 21 | // argument is not declared here: nothing reads it. | ||
| 22 | // XDspConnectWithMaximumStreamLimit(buffer, u32, u32, 0x80, 1, handle*) 1 site; success is a return of 0. The | 6 | // XDspConnectWithMaximumStreamLimit(buffer, u32, u32, 0x80, 1, handle*) 1 site; success is a return of 0. The |
| 23 | // buffer comes from the title's XMemAlloc and stays the title's. | 7 | // buffer comes from the title's XMemAlloc and stays the title's. |
| 24 | // XDspActivate(handle, desc*, out*, out*) 1 site; success is a return of 0, else the caller returns -1. The two | 8 | // XDspActivate(handle, desc*, out*, out*) 1 site; success is a return of 0, else the caller returns -1. The two |
| @@ -30,12 +14,6 @@ | |||
| 30 | // at +4 equals *id. | 14 | // at +4 equals *id. |
| 31 | // XDspDeactivate, XDspDisconnect 1 site each, tail calls: the caller returns what they return. | 15 | // XDspDeactivate, XDspDisconnect 1 site each, tail calls: the caller returns what they return. |
| 32 | // | 16 | // |
| 33 | // HYPOTHESIS: the shared buffer is 0x14000 + 128 * 0x1E00 bytes. 128 slots is what fits 0x14000 at the 0x280 input | ||
| 34 | // stride, and the DSP connect passes 0x80 as its stream limit; no call site shows a bound. The 16-byte output is | ||
| 35 | // written as {base, size}; only the base is measured. | ||
| 36 | // HYPOTHESIS: a type 1 (open) or type 2 (close) command produces one result of the same type for the same slot; the | ||
| 37 | // title's state machine fits that, the console is not measured. Without the close result the title's 256 stream ids | ||
| 38 | // run out. Only the two dwords are written; other command types are accepted with S_OK and produce no result. | ||
| 39 | // HYPOTHESIS: XDspActivate's outputs point at zeroed memory owned by the connection. XDspSubmitCommand leaves *id at | 17 | // HYPOTHESIS: XDspActivate's outputs point at zeroed memory owned by the connection. XDspSubmitCommand leaves *id at |
| 40 | // the title's 0, so the zeroed status reads as "command 0 done, no error" and the title does not wait. | 18 | // the title's 0, so the zeroed status reads as "command 0 done, no error" and the title does not wait. |
| 41 | #include <windows.h> | 19 | #include <windows.h> |
| @@ -43,7 +21,6 @@ | |||
| 43 | 21 | ||
| 44 | #include <cstdint> | 22 | #include <cstdint> |
| 45 | #include <cstring> | 23 | #include <cstring> |
| 46 | #include <deque> | ||
| 47 | #include <mutex> | 24 | #include <mutex> |
| 48 | #include <new> | 25 | #include <new> |
| 49 | #include <set> | 26 | #include <set> |
| @@ -54,30 +31,13 @@ | |||
| 54 | 31 | ||
| 55 | namespace { | 32 | namespace { |
| 56 | 33 | ||
| 57 | constexpr size_t kApuBufferSize = 0x14000 + 128 * 0x1E00; | ||
| 58 | |||
| 59 | // shortcut: results past this cap are dropped (each leaks one stream id); raise it if a title that never dequeues | ||
| 60 | // must still work. | ||
| 61 | constexpr size_t kMaxResults = 4096; | ||
| 62 | |||
| 63 | struct ApuResult { | ||
| 64 | uint32_t type; | ||
| 65 | uint32_t slot; | ||
| 66 | }; | ||
| 67 | |||
| 68 | struct ApuConnection { | ||
| 69 | void* buffer; | ||
| 70 | std::mutex mutex; | ||
| 71 | std::deque<ApuResult> results; | ||
| 72 | }; | ||
| 73 | |||
| 74 | struct DspConnection { | 34 | struct DspConnection { |
| 75 | uint8_t status[256]; | 35 | uint8_t status[256]; |
| 76 | uint8_t stream[256]; | 36 | uint8_t stream[256]; |
| 77 | }; | 37 | }; |
| 78 | 38 | ||
| 79 | // gta6rt --trace FILE: the first call of each function and the first XApuEnqueueCommand of each command type, with | 39 | // gta6rt --trace FILE: the first call of each function, with the caller, so the first boot shows what the title sends |
| 80 | // the caller, so the first boot shows what the title sends without a line per call. | 40 | // without a line per call. |
| 81 | void note(const char* api, uint64_t value, const void* caller) { | 41 | void note(const char* api, uint64_t value, const void* caller) { |
| 82 | if (!rt::startup_trace::enabled()) return; | 42 | if (!rt::startup_trace::enabled()) return; |
| 83 | static std::mutex mutex; | 43 | static std::mutex mutex; |
| @@ -92,71 +52,6 @@ void note(const char* api, uint64_t value, const void* caller) { | |||
| 92 | rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller)))); | 52 | rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller)))); |
| 93 | } | 53 | } |
| 94 | 54 | ||
| 95 | HRESULT __stdcall XApuConnect(void* block24, void* out16, void** handle) { | ||
| 96 | note("XApuConnect", 0, _ReturnAddress()); | ||
| 97 | if (!handle) return E_INVALIDARG; | ||
| 98 | *handle = nullptr; | ||
| 99 | void* buffer = VirtualAlloc(nullptr, kApuBufferSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); // zeroed | ||
| 100 | if (!buffer) return E_OUTOFMEMORY; | ||
| 101 | auto* c = new (std::nothrow) ApuConnection; // a throwing new would unwind into the title | ||
| 102 | if (!c) { | ||
| 103 | VirtualFree(buffer, 0, MEM_RELEASE); | ||
| 104 | return E_OUTOFMEMORY; | ||
| 105 | } | ||
| 106 | c->buffer = buffer; | ||
| 107 | *handle = c; | ||
| 108 | (void)block24; | ||
| 109 | if (out16) { | ||
| 110 | const uint64_t info[2] = {reinterpret_cast<uint64_t>(buffer), kApuBufferSize}; | ||
| 111 | std::memcpy(out16, info, sizeof info); | ||
| 112 | } | ||
| 113 | return S_OK; | ||
| 114 | } | ||
| 115 | |||
| 116 | HRESULT __stdcall XApuDisconnect(void* handle) { | ||
| 117 | note("XApuDisconnect", 0, _ReturnAddress()); | ||
| 118 | auto* c = static_cast<ApuConnection*>(handle); | ||
| 119 | if (!c) return E_INVALIDARG; | ||
| 120 | VirtualFree(c->buffer, 0, MEM_RELEASE); | ||
| 121 | delete c; | ||
| 122 | return S_OK; | ||
| 123 | } | ||
| 124 | |||
| 125 | HRESULT __stdcall XApuEnqueueCommand(void* handle, const uint32_t* command, uint64_t flags) { | ||
| 126 | note("XApuEnqueueCommand", command ? command[0] : ~0ull, _ReturnAddress()); | ||
| 127 | (void)flags; | ||
| 128 | auto* c = static_cast<ApuConnection*>(handle); | ||
| 129 | if (!c || !command) return E_INVALIDARG; | ||
| 130 | if (command[0] == 1 || command[0] == 2) { | ||
| 131 | try { | ||
| 132 | const std::lock_guard lock(c->mutex); | ||
| 133 | if (c->results.size() < kMaxResults) c->results.push_back({command[0], command[1]}); | ||
| 134 | } catch (...) { // out of memory: the result is lost, nothing may unwind into the title | ||
| 135 | } | ||
| 136 | } | ||
| 137 | return S_OK; | ||
| 138 | } | ||
| 139 | |||
| 140 | uint32_t __stdcall XApuGetResultCount(void* handle, uint64_t flags) { | ||
| 141 | note("XApuGetResultCount", 0, _ReturnAddress()); | ||
| 142 | (void)flags; | ||
| 143 | auto* c = static_cast<ApuConnection*>(handle); | ||
| 144 | if (!c) return 0; | ||
| 145 | const std::lock_guard lock(c->mutex); | ||
| 146 | return static_cast<uint32_t>(c->results.size()); | ||
| 147 | } | ||
| 148 | |||
| 149 | HRESULT __stdcall XApuDequeueResult(void* handle, void* result) { | ||
| 150 | note("XApuDequeueResult", 0, _ReturnAddress()); | ||
| 151 | auto* c = static_cast<ApuConnection*>(handle); | ||
| 152 | if (!c || !result) return E_INVALIDARG; | ||
| 153 | const std::lock_guard lock(c->mutex); | ||
| 154 | if (c->results.empty()) return E_FAIL; | ||
| 155 | std::memcpy(result, &c->results.front(), sizeof(ApuResult)); // the rest stays as the caller zeroed it | ||
| 156 | c->results.pop_front(); | ||
| 157 | return S_OK; | ||
| 158 | } | ||
| 159 | |||
| 160 | HRESULT __stdcall XDspConnectWithMaximumStreamLimit(void* buffer, uint32_t a, uint32_t b, uint32_t max_streams, | 55 | HRESULT __stdcall XDspConnectWithMaximumStreamLimit(void* buffer, uint32_t a, uint32_t b, uint32_t max_streams, |
| 161 | uint32_t flags, void** handle) { | 56 | uint32_t flags, void** handle) { |
| 162 | note("XDspConnectWithMaximumStreamLimit", max_streams, _ReturnAddress()); | 57 | note("XDspConnectWithMaximumStreamLimit", max_streams, _ReturnAddress()); |
| @@ -199,14 +94,6 @@ HRESULT __stdcall XDspSubmitCommand(void*, void*, uint32_t*) { | |||
| 199 | 94 | ||
| 200 | } // namespace | 95 | } // namespace |
| 201 | 96 | ||
| 202 | SHIM_TABLE(xapu, | ||
| 203 | {"xapu.dll", "XApuConnect", reinterpret_cast<void*>(&XApuConnect)}, | ||
| 204 | {"xapu.dll", "XApuDequeueResult", reinterpret_cast<void*>(&XApuDequeueResult)}, | ||
| 205 | {"xapu.dll", "XApuDisconnect", reinterpret_cast<void*>(&XApuDisconnect)}, | ||
| 206 | {"xapu.dll", "XApuEnqueueCommand", reinterpret_cast<void*>(&XApuEnqueueCommand)}, | ||
| 207 | {"xapu.dll", "XApuGetResultCount", reinterpret_cast<void*>(&XApuGetResultCount)}, | ||
| 208 | ); | ||
| 209 | |||
| 210 | SHIM_TABLE(chad, | 97 | SHIM_TABLE(chad, |
| 211 | {"chad.dll", "XDspActivate", reinterpret_cast<void*>(&XDspActivate)}, | 98 | {"chad.dll", "XDspActivate", reinterpret_cast<void*>(&XDspActivate)}, |
| 212 | {"chad.dll", "XDspConnectWithMaximumStreamLimit", reinterpret_cast<void*>(&XDspConnectWithMaximumStreamLimit)}, | 99 | {"chad.dll", "XDspConnectWithMaximumStreamLimit", reinterpret_cast<void*>(&XDspConnectWithMaximumStreamLimit)}, |