diff options
Diffstat (limited to 'runtime/shims/acphal.cpp')
| -rw-r--r-- | runtime/shims/acphal.cpp | 568 |
1 files changed, 568 insertions, 0 deletions
diff --git a/runtime/shims/acphal.cpp b/runtime/shims/acphal.cpp new file mode 100644 index 000000000..553f57f20 --- /dev/null +++ b/runtime/shims/acphal.cpp | |||
| @@ -0,0 +1,568 @@ | |||
| 1 | // acphal.dll: Audio Control Processor HAL. Scalable Hardware Audio Processing Engine | ||
| 2 | // (SHAPE) and APU (Audio Processing Unit) physical memory management for Xbox Series. | ||
| 3 | // | ||
| 4 | // On a PC there is no SHAPE/ACP hardware and APU physical memory does not exist. | ||
| 5 | // This shim creates the APU heap from process memory and manages allocations within it. | ||
| 6 | // The SHAPE context arrays are allocated on the process heap; the ACP interface | ||
| 7 | // accepts and acknowledges commands to allow audio bring-up to proceed cleanly. | ||
| 8 | // | ||
| 9 | // Signatures from the public GDK reference (devdocs.xbox.com): | ||
| 10 | // AcpHal, APU reference, SHAPE_CONTEXTS, APU_ADDRESS. | ||
| 11 | // Measured call-site shapes and findings from the retail executable (1.0.2.1): | ||
| 12 | // - docs/retail_boot_chain.md, docs/retail_boot_frame_loop.md (Stop 28) | ||
| 13 | // - PR #6885 (call sites and parameter analysis), PR #7009 (audio/HAL bring-up). | ||
| 14 | |||
| 15 | #include <windows.h> | ||
| 16 | |||
| 17 | #include <atomic> | ||
| 18 | #include <array> | ||
| 19 | #include <cstddef> | ||
| 20 | #include <cstdint> | ||
| 21 | #include <cstdio> | ||
| 22 | #include <cstdlib> | ||
| 23 | #include <cstring> | ||
| 24 | #include <map> | ||
| 25 | #include <mutex> | ||
| 26 | #include <set> | ||
| 27 | #include <string> | ||
| 28 | #include <vector> | ||
| 29 | |||
| 30 | #include "shims.h" | ||
| 31 | #include "startup_trace.h" | ||
| 32 | |||
| 33 | namespace { | ||
| 34 | |||
| 35 | // ---- APU_ADDRESS (from the GDK) ---- | ||
| 36 | |||
| 37 | // The console APU uses a 40-bit physical address space. | ||
| 38 | // On PC we translate to a 32-bit APU address offset. | ||
| 39 | struct APU_ADDRESS { | ||
| 40 | uint64_t value; // 40-bit physical address | ||
| 41 | }; | ||
| 42 | |||
| 43 | // ---- SHAPE context structures (from the public GDK documentation at devdocs.xbox.com) ---- | ||
| 44 | // | ||
| 45 | // These structures are defined here with their documented GDK field layouts so that | ||
| 46 | // sizeof() returns the correct per-context byte count. Without the real layouts the | ||
| 47 | // empty structs would be 1 byte each, and the title's writes (e.g. memset with the | ||
| 48 | // real stride) would overflow the allocation. | ||
| 49 | |||
| 50 | // UINT32 bitfields packed into 32-bit words | ||
| 51 | struct SHAPE_SRC_CONTEXT { | ||
| 52 | uint32_t timestamp : 21; | ||
| 53 | uint32_t blocksToSkip : 2; | ||
| 54 | uint32_t internalSaturate : 1; | ||
| 55 | uint32_t mixBufPeakMag : 4; | ||
| 56 | uint32_t peakMag : 4; | ||
| 57 | uint32_t samplingIncrement : 21; | ||
| 58 | uint32_t reserved0 : 1; | ||
| 59 | uint32_t cmd : 2; | ||
| 60 | uint32_t mixBufPeakMag2 : 4; | ||
| 61 | uint32_t peakMag2 : 4; | ||
| 62 | uint32_t sampleCount; | ||
| 63 | uint32_t samplePointer : 25; | ||
| 64 | uint32_t reserved1 : 7; | ||
| 65 | uint32_t samplingIncrementTarget : 21; | ||
| 66 | uint32_t reserved2 : 11; | ||
| 67 | uint32_t reserved[3]; | ||
| 68 | }; | ||
| 69 | static_assert(sizeof(SHAPE_SRC_CONTEXT) == 32, "SHAPE_SRC_CONTEXT is 32 bytes per GDK"); | ||
| 70 | |||
| 71 | // The EQ/Comp context: 88 UINT32s = 352 bytes per GDK definition. | ||
| 72 | #pragma pack(push, 1) | ||
| 73 | struct alignas(4) SHAPE_EQCOMP_CONTEXT { | ||
| 74 | uint8_t data[352]; | ||
| 75 | }; | ||
| 76 | #pragma pack(pop) | ||
| 77 | static_assert(sizeof(SHAPE_EQCOMP_CONTEXT) == 352, "SHAPE_EQCOMP_CONTEXT is 352 bytes per GDK"); | ||
| 78 | |||
| 79 | struct SHAPE_FILTVOL_CONTEXT { | ||
| 80 | uint32_t timestamp : 21; | ||
| 81 | uint32_t reserved0 : 2; | ||
| 82 | uint32_t internalSaturate : 1; | ||
| 83 | uint32_t mixBufPeakMag : 4; | ||
| 84 | uint32_t peakMag : 4; | ||
| 85 | uint32_t delay1 : 30; | ||
| 86 | uint32_t headroom : 2; | ||
| 87 | uint32_t delay2 : 30; | ||
| 88 | uint32_t svfMode : 2; | ||
| 89 | uint32_t gainTarget : 16; | ||
| 90 | uint32_t gain : 16; | ||
| 91 | uint32_t qRecip : 24; | ||
| 92 | uint32_t reserved1 : 8; | ||
| 93 | uint32_t qRecipTarget : 24; | ||
| 94 | uint32_t reserved2 : 8; | ||
| 95 | uint32_t fc : 24; | ||
| 96 | uint32_t reserved3 : 8; | ||
| 97 | uint32_t fcTarget : 24; | ||
| 98 | uint32_t reserved4 : 8; | ||
| 99 | }; | ||
| 100 | static_assert(sizeof(SHAPE_FILTVOL_CONTEXT) == 32, "SHAPE_FILTVOL_CONTEXT is 32 bytes per GDK"); | ||
| 101 | |||
| 102 | struct SHAPE_DMA_CONTEXT { | ||
| 103 | uint32_t timestamp : 21; | ||
| 104 | uint32_t reserved0 : 3; | ||
| 105 | uint32_t mixBufPeakMag : 4; | ||
| 106 | uint32_t peakMag : 4; | ||
| 107 | uint32_t readPointer : 5; | ||
| 108 | uint32_t reserved1 : 3; | ||
| 109 | uint32_t writePointer : 5; | ||
| 110 | uint32_t reserved2 : 3; | ||
| 111 | uint32_t full : 1; | ||
| 112 | uint32_t reserved3 : 15; | ||
| 113 | uint32_t reserved4 : 9; | ||
| 114 | uint32_t address : 23; | ||
| 115 | uint32_t numFrames : 5; | ||
| 116 | uint32_t reserved5 : 3; | ||
| 117 | uint32_t floatConvert : 1; | ||
| 118 | uint32_t reserved6 : 7; | ||
| 119 | uint32_t numChannels : 3; | ||
| 120 | uint32_t reserved7 : 5; | ||
| 121 | uint32_t channel : 3; | ||
| 122 | uint32_t reserved8 : 5; | ||
| 123 | }; | ||
| 124 | static_assert(sizeof(SHAPE_DMA_CONTEXT) == 16, "SHAPE_DMA_CONTEXT is 16 bytes per GDK"); | ||
| 125 | |||
| 126 | struct SHAPE_XMA_CONTEXT { | ||
| 127 | uint32_t sizeRead0 : 12; | ||
| 128 | uint32_t numLoops : 8; | ||
| 129 | uint32_t validBuffer : 2; | ||
| 130 | uint32_t sizeWrite : 5; | ||
| 131 | uint32_t offsetWrite : 5; | ||
| 132 | uint32_t sizeRead1 : 12; | ||
| 133 | uint32_t sequenceNumber : 12; | ||
| 134 | uint32_t stopSampleAddress : 8; | ||
| 135 | uint32_t subframeOffset : 4; | ||
| 136 | uint32_t reserved0 : 1; | ||
| 137 | uint32_t isLooping : 1; | ||
| 138 | uint32_t subframeSkip : 2; | ||
| 139 | uint32_t packetMetadata : 16; | ||
| 140 | uint32_t currentByteOffset : 26; | ||
| 141 | uint32_t reserved1 : 6; | ||
| 142 | uint32_t loopStartOffset : 26; | ||
| 143 | uint32_t loopEndOffset : 6; | ||
| 144 | uint32_t reserved[10]; | ||
| 145 | }; | ||
| 146 | static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK"); | ||
| 147 | |||
| 148 | struct SHAPE_PCM_CONTEXT { | ||
| 149 | uint32_t timestamp : 21; | ||
| 150 | uint32_t reserved0 : 11; | ||
| 151 | uint32_t loopCount : 8; | ||
| 152 | uint32_t reserved1 : 8; | ||
| 153 | uint32_t full : 1; | ||
| 154 | uint32_t reserved2 : 15; | ||
| 155 | uint32_t readPointer : 21; | ||
| 156 | uint32_t reserved3 : 11; | ||
| 157 | uint32_t loopStartWritePointer : 21; | ||
| 158 | uint32_t reserved4 : 11; | ||
| 159 | uint32_t loopEnd : 21; | ||
| 160 | uint32_t reserved5 : 11; | ||
| 161 | uint32_t mode : 1; | ||
| 162 | uint32_t reserved6 : 15; | ||
| 163 | uint32_t format : 2; | ||
| 164 | uint32_t reserved7 : 14; | ||
| 165 | uint32_t reserved[1]; | ||
| 166 | }; | ||
| 167 | static_assert(sizeof(SHAPE_PCM_CONTEXT) == 32, "SHAPE_PCM_CONTEXT is 32 bytes per GDK"); | ||
| 168 | |||
| 169 | // The SHAPE_CONTEXTS structure (from the GDK) | ||
| 170 | struct SHAPE_CONTEXTS { | ||
| 171 | uint32_t numSrcContexts; | ||
| 172 | uint32_t numEqCompContexts; | ||
| 173 | uint32_t numFiltVolContexts; | ||
| 174 | uint32_t numDmaContexts; | ||
| 175 | uint32_t numXmaContexts; | ||
| 176 | uint32_t numPcmContexts; | ||
| 177 | SHAPE_SRC_CONTEXT* srcContextArray; | ||
| 178 | SHAPE_EQCOMP_CONTEXT* eqCompContextArray; | ||
| 179 | SHAPE_FILTVOL_CONTEXT* filtVolContextArray; | ||
| 180 | SHAPE_DMA_CONTEXT* dmaContextArray; | ||
| 181 | SHAPE_XMA_CONTEXT* xmaContextArray; | ||
| 182 | SHAPE_PCM_CONTEXT* pcmContextArray; | ||
| 183 | APU_ADDRESS apuSrcContextArray; | ||
| 184 | APU_ADDRESS apuEqCompContextArray; | ||
| 185 | APU_ADDRESS apuFiltVolContextArray; | ||
| 186 | APU_ADDRESS apuDmaContextArray; | ||
| 187 | APU_ADDRESS apuXmaContextArray; | ||
| 188 | APU_ADDRESS apuPcmContextArray; | ||
| 189 | }; | ||
| 190 | |||
| 191 | static_assert(sizeof(SHAPE_CONTEXTS) == 120, "SHAPE_CONTEXTS is 120 bytes per GDK"); | ||
| 192 | static_assert(offsetof(SHAPE_CONTEXTS, xmaContextArray) == 0x38, | ||
| 193 | "xmaContextArray is at offset +0x38 per retail binary measurement"); | ||
| 194 | |||
| 195 | // ---- Startup trace helper ---- | ||
| 196 | |||
| 197 | void trace_call(const char* api, const std::string& detail) { | ||
| 198 | if (!rt::startup_trace::enabled()) return; | ||
| 199 | static std::mutex mutex; | ||
| 200 | static std::set<std::string> seen; | ||
| 201 | { | ||
| 202 | const std::lock_guard lock(mutex); | ||
| 203 | if (!seen.insert(std::string(api) + detail).second) return; | ||
| 204 | } | ||
| 205 | rt::startup_trace::event("acphal", | ||
| 206 | std::string("\"api\":\"") + api + "\"" + detail); | ||
| 207 | } | ||
| 208 | |||
| 209 | // ---- Genuine APU Heap Implementation ---- | ||
| 210 | |||
| 211 | constexpr uint32_t kApuBaseAddress = 0x40000000u; | ||
| 212 | |||
| 213 | struct ApuRegion { | ||
| 214 | void* base = nullptr; | ||
| 215 | uint64_t size = 0; | ||
| 216 | std::map<uint32_t, uint32_t> blocks; // offset -> size of active allocations | ||
| 217 | |||
| 218 | bool contains(const void* ptr) const { | ||
| 219 | if (!base || !ptr) return false; | ||
| 220 | const uintptr_t p = reinterpret_cast<uintptr_t>(ptr); | ||
| 221 | const uintptr_t b = reinterpret_cast<uintptr_t>(base); | ||
| 222 | return p >= b && p < b + size; | ||
| 223 | } | ||
| 224 | |||
| 225 | bool is_valid_address(const void* ptr, uint32_t alignment) const { | ||
| 226 | if (!base || !ptr) return false; | ||
| 227 | const uintptr_t p = reinterpret_cast<uintptr_t>(ptr); | ||
| 228 | const uintptr_t b = reinterpret_cast<uintptr_t>(base); | ||
| 229 | if (p < b || p >= b + size) return false; | ||
| 230 | if (alignment == 0) alignment = 1; | ||
| 231 | return ((p - b) & (static_cast<uintptr_t>(alignment) - 1)) == 0; | ||
| 232 | } | ||
| 233 | |||
| 234 | uint32_t to_offset(const void* ptr) const { | ||
| 235 | return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr) - | ||
| 236 | reinterpret_cast<uintptr_t>(base)); | ||
| 237 | } | ||
| 238 | |||
| 239 | void* from_offset(uint32_t offset) const { | ||
| 240 | if (offset >= size) return nullptr; | ||
| 241 | return static_cast<uint8_t*>(base) + offset; | ||
| 242 | } | ||
| 243 | |||
| 244 | void* allocate(uint32_t alloc_size, uint32_t alignment, uint32_t* out_offset) { | ||
| 245 | if (alloc_size == 0 || alignment == 0) return nullptr; | ||
| 246 | uint64_t at = 0; | ||
| 247 | for (auto it = blocks.begin();; ++it) { | ||
| 248 | at = (at + alignment - 1) & ~static_cast<uint64_t>(alignment - 1); | ||
| 249 | const uint64_t limit = (it == blocks.end()) ? size : it->first; | ||
| 250 | if (at + alloc_size <= limit) break; | ||
| 251 | if (it == blocks.end()) return nullptr; // out of memory | ||
| 252 | at = static_cast<uint64_t>(it->first) + it->second; | ||
| 253 | } | ||
| 254 | const uint32_t offset = static_cast<uint32_t>(at); | ||
| 255 | blocks.emplace(offset, alloc_size); | ||
| 256 | uint8_t* ptr = static_cast<uint8_t*>(base) + offset; | ||
| 257 | std::memset(ptr, 0, alloc_size); | ||
| 258 | if (out_offset) *out_offset = offset; | ||
| 259 | return ptr; | ||
| 260 | } | ||
| 261 | |||
| 262 | bool free_block(void* ptr) { | ||
| 263 | if (!contains(ptr)) return false; | ||
| 264 | const uint32_t offset = to_offset(ptr); | ||
| 265 | return blocks.erase(offset) > 0; | ||
| 266 | } | ||
| 267 | |||
| 268 | void release() { | ||
| 269 | if (base) { | ||
| 270 | VirtualFree(base, 0, MEM_RELEASE); | ||
| 271 | base = nullptr; | ||
| 272 | } | ||
| 273 | size = 0; | ||
| 274 | blocks.clear(); | ||
| 275 | } | ||
| 276 | }; | ||
| 277 | |||
| 278 | std::mutex g_apu_mutex; | ||
| 279 | bool g_apu_heap_created = false; | ||
| 280 | ApuRegion g_cached_heap; | ||
| 281 | ApuRegion g_noncached_heap; | ||
| 282 | |||
| 283 | // Tracked SHAPE context arrays | ||
| 284 | std::vector<void*> g_active_shape_arrays; | ||
| 285 | |||
| 286 | // ---- IAcpHal Interface Implementation ---- | ||
| 287 | |||
| 288 | struct AcpHalImpl { | ||
| 289 | // Slot 0: Connect (called at boot with (0x1000, 0x1000), result checked) | ||
| 290 | virtual HRESULT __stdcall connect(uint32_t, uint32_t) { | ||
| 291 | trace_call("AcpHal::Connect", ""); | ||
| 292 | return S_OK; | ||
| 293 | } | ||
| 294 | |||
| 295 | // Slot 1: Disconnect (called at shutdown) | ||
| 296 | virtual HRESULT __stdcall disconnect() { | ||
| 297 | trace_call("AcpHal::Disconnect", ""); | ||
| 298 | return S_OK; | ||
| 299 | } | ||
| 300 | |||
| 301 | // Slot 2: SubmitCommand | ||
| 302 | // Critical: The title calls slot 2 as (2, 0xf0f0f0f0, -1, config, 0) and | ||
| 303 | // a negative result triggers a fatal int3 kernel panic (+0x43ad94e). | ||
| 304 | virtual HRESULT __stdcall submit_command(uint32_t cmd, uint32_t d1, uint32_t d2, | ||
| 305 | const void* config, uint32_t d3) { | ||
| 306 | (void)cmd; | ||
| 307 | (void)d1; | ||
| 308 | (void)d2; | ||
| 309 | (void)config; | ||
| 310 | (void)d3; | ||
| 311 | static std::atomic<bool> warned{false}; | ||
| 312 | if (!warned.exchange(true, std::memory_order_relaxed)) { | ||
| 313 | std::fprintf(stderr, | ||
| 314 | "[rt] acphal: command accepted and dropped: no audio DSP hardware on PC\n"); | ||
| 315 | } | ||
| 316 | trace_call("AcpHal::SubmitCommand", ""); | ||
| 317 | return S_OK; | ||
| 318 | } | ||
| 319 | |||
| 320 | // Slots 3 and 4: PopMessage and GetNumMessages (message polling, not called by title) | ||
| 321 | virtual uint32_t __stdcall pop_message() { return 0; } | ||
| 322 | virtual uint32_t __stdcall get_num_messages() { return 0; } | ||
| 323 | |||
| 324 | // Slot 5: Release | ||
| 325 | virtual HRESULT __stdcall release() { | ||
| 326 | trace_call("AcpHal::Release", ""); | ||
| 327 | delete this; | ||
| 328 | return S_OK; | ||
| 329 | } | ||
| 330 | }; | ||
| 331 | |||
| 332 | // ---- AcpHalCreate ---- | ||
| 333 | |||
| 334 | HRESULT __stdcall AcpHalCreate(void** ppAcpHal) { | ||
| 335 | if (!ppAcpHal) return E_POINTER; | ||
| 336 | *ppAcpHal = new AcpHalImpl(); | ||
| 337 | trace_call("AcpHalCreate", ""); | ||
| 338 | return S_OK; | ||
| 339 | } | ||
| 340 | |||
| 341 | // ---- AcpHalAllocateShapeContexts ---- | ||
| 342 | |||
| 343 | HRESULT __stdcall AcpHalAllocateShapeContexts(SHAPE_CONTEXTS* ctx) { | ||
| 344 | if (!ctx) return E_POINTER; | ||
| 345 | |||
| 346 | std::lock_guard lock(g_apu_mutex); | ||
| 347 | |||
| 348 | auto alloc_array = [](uint32_t count, size_t elem_size) -> void* { | ||
| 349 | if (count == 0) return nullptr; | ||
| 350 | void* ptr = std::calloc(count, elem_size); | ||
| 351 | if (ptr) g_active_shape_arrays.push_back(ptr); | ||
| 352 | return ptr; | ||
| 353 | }; | ||
| 354 | |||
| 355 | ctx->srcContextArray = static_cast<SHAPE_SRC_CONTEXT*>( | ||
| 356 | alloc_array(ctx->numSrcContexts, sizeof(SHAPE_SRC_CONTEXT))); | ||
| 357 | ctx->eqCompContextArray = static_cast<SHAPE_EQCOMP_CONTEXT*>( | ||
| 358 | alloc_array(ctx->numEqCompContexts, sizeof(SHAPE_EQCOMP_CONTEXT))); | ||
| 359 | ctx->filtVolContextArray = static_cast<SHAPE_FILTVOL_CONTEXT*>( | ||
| 360 | alloc_array(ctx->numFiltVolContexts, sizeof(SHAPE_FILTVOL_CONTEXT))); | ||
| 361 | ctx->dmaContextArray = static_cast<SHAPE_DMA_CONTEXT*>( | ||
| 362 | alloc_array(ctx->numDmaContexts, sizeof(SHAPE_DMA_CONTEXT))); | ||
| 363 | ctx->xmaContextArray = static_cast<SHAPE_XMA_CONTEXT*>( | ||
| 364 | alloc_array(ctx->numXmaContexts, sizeof(SHAPE_XMA_CONTEXT))); | ||
| 365 | ctx->pcmContextArray = static_cast<SHAPE_PCM_CONTEXT*>( | ||
| 366 | alloc_array(ctx->numPcmContexts, sizeof(SHAPE_PCM_CONTEXT))); | ||
| 367 | |||
| 368 | // On PC there is no physical APU address translation for host context arrays; | ||
| 369 | // assign unique synthetic addresses from the APU base. | ||
| 370 | ctx->apuSrcContextArray = {ctx->srcContextArray ? kApuBaseAddress : 0}; | ||
| 371 | ctx->apuEqCompContextArray = {ctx->eqCompContextArray ? kApuBaseAddress + 0x1000 : 0}; | ||
| 372 | ctx->apuFiltVolContextArray = {ctx->filtVolContextArray ? kApuBaseAddress + 0x2000 : 0}; | ||
| 373 | ctx->apuDmaContextArray = {ctx->dmaContextArray ? kApuBaseAddress + 0x3000 : 0}; | ||
| 374 | ctx->apuXmaContextArray = {ctx->xmaContextArray ? kApuBaseAddress + 0x4000 : 0}; | ||
| 375 | ctx->apuPcmContextArray = {ctx->pcmContextArray ? kApuBaseAddress + 0x5000 : 0}; | ||
| 376 | |||
| 377 | trace_call("AcpHalAllocateShapeContexts", | ||
| 378 | ",\"src\":" + std::to_string(ctx->numSrcContexts) + | ||
| 379 | ",\"xma\":" + std::to_string(ctx->numXmaContexts) + | ||
| 380 | ",\"pcm\":" + std::to_string(ctx->numPcmContexts)); | ||
| 381 | return S_OK; | ||
| 382 | } | ||
| 383 | |||
| 384 | // ---- AcpHalReleaseShapeContexts ---- | ||
| 385 | |||
| 386 | HRESULT __stdcall AcpHalReleaseShapeContexts(SHAPE_CONTEXTS* ctx = nullptr) { | ||
| 387 | std::lock_guard lock(g_apu_mutex); | ||
| 388 | |||
| 389 | // Free all recorded context allocations | ||
| 390 | for (void* p : g_active_shape_arrays) { | ||
| 391 | if (p) std::free(p); | ||
| 392 | } | ||
| 393 | g_active_shape_arrays.clear(); | ||
| 394 | |||
| 395 | if (ctx) { | ||
| 396 | ctx->srcContextArray = nullptr; | ||
| 397 | ctx->eqCompContextArray = nullptr; | ||
| 398 | ctx->filtVolContextArray = nullptr; | ||
| 399 | ctx->dmaContextArray = nullptr; | ||
| 400 | ctx->xmaContextArray = nullptr; | ||
| 401 | ctx->pcmContextArray = nullptr; | ||
| 402 | } | ||
| 403 | |||
| 404 | trace_call("AcpHalReleaseShapeContexts", ""); | ||
| 405 | return S_OK; | ||
| 406 | } | ||
| 407 | |||
| 408 | // ---- ApuCreateHeap ---- | ||
| 409 | |||
| 410 | HRESULT __stdcall ApuCreateHeap(uint32_t cachedSizeInBytes, | ||
| 411 | uint32_t nonCachedSizeInBytes) { | ||
| 412 | std::lock_guard lock(g_apu_mutex); | ||
| 413 | if (g_apu_heap_created) return E_FAIL; | ||
| 414 | |||
| 415 | if (cachedSizeInBytes > 0) { | ||
| 416 | g_cached_heap.base = VirtualAlloc( | ||
| 417 | nullptr, cachedSizeInBytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | ||
| 418 | if (!g_cached_heap.base) return E_OUTOFMEMORY; | ||
| 419 | g_cached_heap.size = cachedSizeInBytes; | ||
| 420 | } | ||
| 421 | |||
| 422 | if (nonCachedSizeInBytes > 0) { | ||
| 423 | g_noncached_heap.base = VirtualAlloc( | ||
| 424 | nullptr, nonCachedSizeInBytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | ||
| 425 | if (!g_noncached_heap.base) { | ||
| 426 | g_cached_heap.release(); | ||
| 427 | return E_OUTOFMEMORY; | ||
| 428 | } | ||
| 429 | g_noncached_heap.size = nonCachedSizeInBytes; | ||
| 430 | } | ||
| 431 | |||
| 432 | g_apu_heap_created = true; | ||
| 433 | trace_call("ApuCreateHeap", | ||
| 434 | ",\"cachedSize\":" + std::to_string(cachedSizeInBytes) + | ||
| 435 | ",\"nonCachedSize\":" + std::to_string(nonCachedSizeInBytes)); | ||
| 436 | return S_OK; | ||
| 437 | } | ||
| 438 | |||
| 439 | // ---- ApuAlloc ---- | ||
| 440 | |||
| 441 | HRESULT __stdcall ApuAlloc(void** virtualAddress, uint32_t* physicalAddress, | ||
| 442 | uint32_t sizeInBytes, uint32_t alignmentInBytes, | ||
| 443 | uint32_t flags) { | ||
| 444 | if (!virtualAddress) return E_POINTER; | ||
| 445 | *virtualAddress = nullptr; | ||
| 446 | if (physicalAddress) *physicalAddress = 0; | ||
| 447 | |||
| 448 | if (sizeInBytes == 0) return E_INVALIDARG; | ||
| 449 | if (alignmentInBytes == 0) alignmentInBytes = 1; | ||
| 450 | if ((alignmentInBytes & (alignmentInBytes - 1)) != 0) return E_INVALIDARG; | ||
| 451 | |||
| 452 | std::lock_guard lock(g_apu_mutex); | ||
| 453 | |||
| 454 | // Routing: flags=2 routes to non-cached heap; flags=0 or 1 routes to cached heap. | ||
| 455 | // If non-cached heap wasn't created or has no room, fall back to cached heap | ||
| 456 | // because PC memory is unified and cache-coherent. | ||
| 457 | const bool prefer_noncached = (flags == 2); | ||
| 458 | ApuRegion* target = prefer_noncached ? &g_noncached_heap : &g_cached_heap; | ||
| 459 | |||
| 460 | if (!target->base) { | ||
| 461 | if (!prefer_noncached) { | ||
| 462 | // Lazy default cached heap if ApuCreateHeap wasn't called yet | ||
| 463 | constexpr uint32_t kDefaultHeapSize = 64 * 1024 * 1024; | ||
| 464 | target->base = VirtualAlloc(nullptr, kDefaultHeapSize, | ||
| 465 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | ||
| 466 | if (!target->base) return E_OUTOFMEMORY; | ||
| 467 | target->size = kDefaultHeapSize; | ||
| 468 | g_apu_heap_created = true; | ||
| 469 | } else { | ||
| 470 | // Fall back to cached heap | ||
| 471 | target = &g_cached_heap; | ||
| 472 | if (!target->base) return E_OUTOFMEMORY; | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | uint32_t offset = 0; | ||
| 477 | void* ptr = target->allocate(sizeInBytes, alignmentInBytes, &offset); | ||
| 478 | if (!ptr && prefer_noncached && g_cached_heap.base) { | ||
| 479 | // Fall back to cached heap if non-cached heap is full | ||
| 480 | target = &g_cached_heap; | ||
| 481 | ptr = target->allocate(sizeInBytes, alignmentInBytes, &offset); | ||
| 482 | } | ||
| 483 | |||
| 484 | if (!ptr) return E_OUTOFMEMORY; | ||
| 485 | |||
| 486 | *virtualAddress = ptr; | ||
| 487 | if (physicalAddress) { | ||
| 488 | // Disjoint APU address ranges: | ||
| 489 | // cached: 0x40000000 .. 0x40000000 + cached_size - 1 | ||
| 490 | // non-cached: 0x40000000 + cached_size .. 0x40000000 + cached_size + noncached_size - 1 | ||
| 491 | uint32_t apu = kApuBaseAddress + offset; | ||
| 492 | if (target == &g_noncached_heap) { | ||
| 493 | apu += static_cast<uint32_t>(g_cached_heap.size); | ||
| 494 | } | ||
| 495 | *physicalAddress = apu; | ||
| 496 | } | ||
| 497 | |||
| 498 | return S_OK; | ||
| 499 | } | ||
| 500 | |||
| 501 | // ---- ApuFree ---- | ||
| 502 | |||
| 503 | void __stdcall ApuFree(void* virtualAddress) { | ||
| 504 | if (!virtualAddress) return; | ||
| 505 | std::lock_guard lock(g_apu_mutex); | ||
| 506 | if (!g_cached_heap.free_block(virtualAddress)) { | ||
| 507 | g_noncached_heap.free_block(virtualAddress); | ||
| 508 | } | ||
| 509 | trace_call("ApuFree", ""); | ||
| 510 | } | ||
| 511 | |||
| 512 | // ---- ApuIsVirtualAddressValid ---- | ||
| 513 | |||
| 514 | BOOL __stdcall ApuIsVirtualAddressValid(void* virtualAddress, uint32_t alignment) { | ||
| 515 | if (!virtualAddress) return FALSE; | ||
| 516 | if (alignment == 0) alignment = 1; | ||
| 517 | std::lock_guard lock(g_apu_mutex); | ||
| 518 | if (g_cached_heap.is_valid_address(virtualAddress, alignment)) return TRUE; | ||
| 519 | if (g_noncached_heap.is_valid_address(virtualAddress, alignment)) return TRUE; | ||
| 520 | return FALSE; | ||
| 521 | } | ||
| 522 | |||
| 523 | // ---- ApuMapApuAddress ---- | ||
| 524 | |||
| 525 | void* __stdcall ApuMapApuAddress(uint32_t apu) { | ||
| 526 | if (apu == 0 || apu < kApuBaseAddress) return nullptr; | ||
| 527 | std::lock_guard lock(g_apu_mutex); | ||
| 528 | const uint32_t offset = apu - kApuBaseAddress; | ||
| 529 | if (g_cached_heap.base && offset < g_cached_heap.size) { | ||
| 530 | return g_cached_heap.from_offset(offset); | ||
| 531 | } | ||
| 532 | if (g_noncached_heap.base) { | ||
| 533 | const uint32_t cached_span = static_cast<uint32_t>(g_cached_heap.size); | ||
| 534 | if (offset >= cached_span && offset - cached_span < g_noncached_heap.size) { | ||
| 535 | return g_noncached_heap.from_offset(offset - cached_span); | ||
| 536 | } | ||
| 537 | } | ||
| 538 | return nullptr; | ||
| 539 | } | ||
| 540 | |||
| 541 | // ---- ApuMapVirtualAddress ---- | ||
| 542 | |||
| 543 | uint32_t __stdcall ApuMapVirtualAddress(void* virtualAddress) { | ||
| 544 | if (!virtualAddress) return 0; | ||
| 545 | std::lock_guard lock(g_apu_mutex); | ||
| 546 | if (g_cached_heap.contains(virtualAddress)) { | ||
| 547 | return kApuBaseAddress + g_cached_heap.to_offset(virtualAddress); | ||
| 548 | } | ||
| 549 | if (g_noncached_heap.contains(virtualAddress)) { | ||
| 550 | return kApuBaseAddress + static_cast<uint32_t>(g_cached_heap.size) + | ||
| 551 | g_noncached_heap.to_offset(virtualAddress); | ||
| 552 | } | ||
| 553 | return 0; | ||
| 554 | } | ||
| 555 | |||
| 556 | } // namespace | ||
| 557 | |||
| 558 | SHIM_TABLE(acphal, | ||
| 559 | {"acphal.dll", "AcpHalAllocateShapeContexts", reinterpret_cast<void*>(&AcpHalAllocateShapeContexts)}, | ||
| 560 | {"acphal.dll", "AcpHalCreate", reinterpret_cast<void*>(&AcpHalCreate)}, | ||
| 561 | {"acphal.dll", "AcpHalReleaseShapeContexts", reinterpret_cast<void*>(&AcpHalReleaseShapeContexts)}, | ||
| 562 | {"acphal.dll", "ApuAlloc", reinterpret_cast<void*>(&ApuAlloc)}, | ||
| 563 | {"acphal.dll", "ApuCreateHeap", reinterpret_cast<void*>(&ApuCreateHeap)}, | ||
| 564 | {"acphal.dll", "ApuFree", reinterpret_cast<void*>(&ApuFree)}, | ||
| 565 | {"acphal.dll", "ApuIsVirtualAddressValid", reinterpret_cast<void*>(&ApuIsVirtualAddressValid)}, | ||
| 566 | {"acphal.dll", "ApuMapApuAddress", reinterpret_cast<void*>(&ApuMapApuAddress)}, | ||
| 567 | {"acphal.dll", "ApuMapVirtualAddress", reinterpret_cast<void*>(&ApuMapVirtualAddress)}, | ||
| 568 | ); | ||