diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 20:26:00 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 20:26:00 -0500 |
| commit | 154e4ec034902deffa4b89b2cbca9a949f38baec (patch) | |
| tree | 1b2ef5656de42c172291bc6009efa4c510650815 /runtime/src/cpu_emulation.h | |
| parent | eb6a08cd359d98b5777e70d54d0ae8a75879495a (diff) | |
| parent | 8a242efebf823aeb23c72d17c3d666daf0bbd3e2 (diff) | |
Merge the #984 result (main + RT-PROTON-STORY-LOAD) into #939
The base is 8a242efeb, the merge of main and #984. 68 files conflicted; they were resolved in seven groups by
one rule: the same change in two forms keeps the base's form, everything only #939 has is kept, and where the
two solve one problem in two ways the base's is the frame and #939's plugs into it.
A, the Xbox proxy (d3d12_xs_device.cpp/.h, d3d12_xs.cpp, d3d12_xs_pso_stream.cpp/.h, d3d12_xs_alloc_size.h,
d3d12_xs_desc_clean.h, device_proxy_test.cpp, queue_proxy_test.cpp): the base's files with #939's additions
ported in. Device slot 57 asks #939's NativeGraphicsFactory first and falls back to the base's
proxy_create_graphics_pipeline_from_native_code on E_NOTIMPL. Kept from #939: the providers (texture lease,
raw texture view, resource by address, constant view, descriptor mapping, depth-stencil UAV, texture pages),
the title-backed descriptor mirror, slot 13's feature answers, GPU labels on list slots 71/72. Dropped: #939's
duplicates of the boot ladder routes, its fallback window, its slot 133 stand-in.
A2 (xs_proxy_mock_test.cpp): the base's test with #939's slot 70 raw-buffer case; presentation cases follow
#939's rule that a device which ever had a handler has no queue fallback.
B1, the Vulkan device (d3d12_vulkan_device.cpp, d3d12_com.cpp/.h, shader_library.h, queue_families.h,
pipeline_cache_file.h, startup_trace.h): one lazy-pipeline mechanism, the base's; #939's pending_base_compile
and first_use_backend_pipelines dropped, prepare_on_bind and cold_graphics_compile kept. create_texture keeps
the base's signature; #939's deferred backing is folded into the base's placed-memory binder. #939's quad
lists as triangle indices.
B2, the D3D12 tests (d3d12_existing_heap, a8_dimensions, pipeline_cache_seed, title_va, rect_list,
shader_library, loader_tls_index0, draw, compute, xmem_placed, shader_library_corpus): the base's tests where
both carry the same case, #939's cases added (rebound tables, linear texture map, large vertex batch,
executable shaders, default A8 views).
C, the backend (vulkan_replay.h, vulkan_backend.h, vulkan_bindless.h, vulkan_scratch.h, vulkan_present.h,
rect_list_geometry.h, vk_test_device.h, vk_pipeline_check.cpp, vk_store_test.cpp, bindless_sizing_test.cpp,
replay_texture_barrier_test.cpp): the base's prepare_draw and lazy compute, its #1055 release rule and its
rectangle generator; #939's rectangle variants, GPU quad index expansion, 1D/3D and depth-stencil UAVs, the
label fence on the base's submit.
D, memory, services, presentation (xmem.cpp/.h, xg_xs.cpp, xg_xs_test.cpp, title_va_alias.h and its test,
main.cpp, frame_pacer.h, xbox_present.cpp/.h; xgameruntime_offline.cpp for one comment): the base's xmem with
#939's label hooks, file-backed sections and pin_mapped_data; both texture computers in xg_xs.cpp, the base's
answering first; --first-run-ui kept without host speech in its profile.
E, build, tools, docs (runtime/CMakeLists.txt, make_game.py, tools/rdna2spirv.py, tools/shader_disasm.py,
scripts, tests, docs; docs/README.md and tests/test_exe_shaders_stage.py beyond the conflicts): the base's
v_dot4 decode, #281's disassembler form and tests/semantic/test_fspx_dot4_op_sel_disasm.py dropped; the
base's executable-shader step with #939's cache fix; 366 registered tests.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/cpu_emulation.h')
| -rw-r--r-- | runtime/src/cpu_emulation.h | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/runtime/src/cpu_emulation.h b/runtime/src/cpu_emulation.h index fac13eeee..8df3b55d0 100644 --- a/runtime/src/cpu_emulation.h +++ b/runtime/src/cpu_emulation.h | |||
| @@ -272,6 +272,84 @@ inline bool apply(const Decoded& d, CONTEXT* c, EXCEPTION_RECORD* fault = nullpt | |||
| 272 | return true; | 272 | return true; |
| 273 | } | 273 | } |
| 274 | 274 | ||
| 275 | inline uint64_t gpr_read(const CONTEXT* c, int r) { | ||
| 276 | switch (r) { | ||
| 277 | case 0: return c->Rax; case 1: return c->Rcx; case 2: return c->Rdx; case 3: return c->Rbx; | ||
| 278 | case 4: return c->Rsp; case 5: return c->Rbp; case 6: return c->Rsi; case 7: return c->Rdi; | ||
| 279 | case 8: return c->R8; case 9: return c->R9; case 10: return c->R10; case 11: return c->R11; | ||
| 280 | case 12: return c->R12; case 13: return c->R13; case 14: return c->R14; case 15: return c->R15; | ||
| 281 | default: return 0; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | enum class ClzeroLoopResult { no_match, ok, faulted }; | ||
| 286 | |||
| 287 | inline void apply_cmp_flags(CONTEXT* c, uint64_t lhs, uint64_t rhs) { | ||
| 288 | const uint64_t diff = lhs - rhs; | ||
| 289 | uint32_t flags = c->EFlags & ~DWORD(0x8D5u); // clear CF(0), PF(2), AF(4), ZF(6), SF(7), OF(11) | ||
| 290 | if (lhs < rhs) flags |= 0x1u; // CF | ||
| 291 | uint8_t lo8 = static_cast<uint8_t>(diff); | ||
| 292 | lo8 ^= lo8 >> 4; | ||
| 293 | lo8 ^= lo8 >> 2; | ||
| 294 | lo8 ^= lo8 >> 1; | ||
| 295 | if ((lo8 & 1u) == 0) flags |= 0x4u; // PF | ||
| 296 | if ((lhs ^ rhs ^ diff) & 0x10u) flags |= 0x10u; // AF | ||
| 297 | if (diff == 0) flags |= 0x40u; // ZF | ||
| 298 | if (diff >> 63) flags |= 0x80u; // SF | ||
| 299 | if (((lhs ^ rhs) & (lhs ^ diff)) >> 63) flags |= 0x800u; // OF | ||
| 300 | c->EFlags = flags; | ||
| 301 | } | ||
| 302 | |||
| 303 | // Fast-path for the Zen 2 zeroing loop: `clzero; add rax, 0x40; cmp rax, reg64; jb -12` (12 bytes). | ||
| 304 | // Zeroes the entire writable span in one memset instead of raising #UD + VirtualQuery per 64-byte line. | ||
| 305 | inline ClzeroLoopResult try_clzero_loop(const uint8_t* code, std::size_t avail, CONTEXT* c, EXCEPTION_RECORD* fault) { | ||
| 306 | if (avail < 12) return ClzeroLoopResult::no_match; | ||
| 307 | if (code[0] != 0x0F || code[1] != 0x01 || code[2] != 0xFC) return ClzeroLoopResult::no_match; | ||
| 308 | if (code[3] != 0x48 || code[4] != 0x83 || code[5] != 0xC0 || code[6] != 0x40) return ClzeroLoopResult::no_match; | ||
| 309 | if ((code[7] != 0x48 && code[7] != 0x4C) || code[8] != 0x39 || (code[9] & 0xC7) != 0xC0) | ||
| 310 | return ClzeroLoopResult::no_match; | ||
| 311 | if (code[10] != 0x72 || code[11] != 0xF4) return ClzeroLoopResult::no_match; | ||
| 312 | |||
| 313 | const int limit_reg = ((code[9] >> 3) & 7) | ((code[7] & 4) << 1); | ||
| 314 | if (limit_reg == 0 || limit_reg == 4) return ClzeroLoopResult::no_match; | ||
| 315 | |||
| 316 | const uint64_t start = c->Rax; | ||
| 317 | const uint64_t limit = gpr_read(c, limit_reg); | ||
| 318 | if ((start & 63) != 0 || limit <= start + 64) return ClzeroLoopResult::no_match; | ||
| 319 | |||
| 320 | uint64_t total = (limit - start + 63) & ~uint64_t(63); | ||
| 321 | constexpr uint64_t kMaxBatch = uint64_t(64) << 20; | ||
| 322 | if (total > kMaxBatch) total = kMaxBatch; | ||
| 323 | const uint64_t end = start + total; | ||
| 324 | |||
| 325 | for (uint64_t cur = start; cur < end;) { | ||
| 326 | const DWORD err = writable(cur); | ||
| 327 | if (err) { | ||
| 328 | if (cur > start) { | ||
| 329 | std::memset(reinterpret_cast<void*>(start), 0, std::size_t(cur - start)); | ||
| 330 | c->Rax = cur; | ||
| 331 | apply_cmp_flags(c, cur, limit); | ||
| 332 | } | ||
| 333 | if (fault) { | ||
| 334 | std::memset(fault, 0, sizeof *fault); | ||
| 335 | fault->ExceptionCode = err; | ||
| 336 | fault->NumberParameters = 2; | ||
| 337 | fault->ExceptionInformation[0] = 1; | ||
| 338 | fault->ExceptionInformation[1] = ULONG_PTR(cur); | ||
| 339 | } | ||
| 340 | return ClzeroLoopResult::faulted; | ||
| 341 | } | ||
| 342 | const uint64_t next_page = (cur & ~uint64_t(0xFFF)) + 0x1000; | ||
| 343 | cur = next_page < end ? next_page : end; | ||
| 344 | } | ||
| 345 | |||
| 346 | std::memset(reinterpret_cast<void*>(start), 0, std::size_t(total)); | ||
| 347 | c->Rax = end; | ||
| 348 | apply_cmp_flags(c, end, limit); | ||
| 349 | if (end >= limit) c->Rip += 12; | ||
| 350 | return ClzeroLoopResult::ok; | ||
| 351 | } | ||
| 352 | |||
| 275 | inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { | 353 | inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { |
| 276 | if (info->ExceptionRecord->ExceptionCode != STATUS_ILLEGAL_INSTRUCTION) return EXCEPTION_CONTINUE_SEARCH; | 354 | if (info->ExceptionRecord->ExceptionCode != STATUS_ILLEGAL_INSTRUCTION) return EXCEPTION_CONTINUE_SEARCH; |
| 277 | CONTEXT* c = info->ContextRecord; | 355 | CONTEXT* c = info->ContextRecord; |
| @@ -281,6 +359,25 @@ inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { | |||
| 281 | const Decoded d = decode(code, avail); | 359 | const Decoded d = decode(code, avail); |
| 282 | if (d.op == Op::none) return EXCEPTION_CONTINUE_SEARCH; | 360 | if (d.op == Op::none) return EXCEPTION_CONTINUE_SEARCH; |
| 283 | EXCEPTION_RECORD fault; | 361 | EXCEPTION_RECORD fault; |
| 362 | if (d.op == Op::clzero && !d.addr32 && d.length == 3) { | ||
| 363 | const uint64_t fault_rip = c->Rip; | ||
| 364 | const ClzeroLoopResult lr = try_clzero_loop(code, avail, c, &fault); | ||
| 365 | if (lr == ClzeroLoopResult::ok) { | ||
| 366 | if (count(d.op).fetch_add(1, std::memory_order_relaxed) == 0) | ||
| 367 | std::fprintf(stderr, "[rt] cpu: this CPU has no %s; emulating it (first at %p)\n", name(d.op), | ||
| 368 | reinterpret_cast<void*>(fault_rip)); | ||
| 369 | return EXCEPTION_CONTINUE_EXECUTION; | ||
| 370 | } | ||
| 371 | if (lr == ClzeroLoopResult::faulted) { | ||
| 372 | EXCEPTION_RECORD* r = info->ExceptionRecord; | ||
| 373 | r->ExceptionCode = fault.ExceptionCode; | ||
| 374 | r->ExceptionFlags = 0; | ||
| 375 | r->NumberParameters = fault.NumberParameters; | ||
| 376 | r->ExceptionInformation[0] = fault.ExceptionInformation[0]; | ||
| 377 | r->ExceptionInformation[1] = fault.ExceptionInformation[1]; | ||
| 378 | return EXCEPTION_CONTINUE_SEARCH; | ||
| 379 | } | ||
| 380 | } | ||
| 284 | if (!apply(d, c, &fault)) { | 381 | if (!apply(d, c, &fault)) { |
| 285 | // CLZERO on a line it cannot write: the CPU would raise that fault at this instruction, so the next handlers | 382 | // CLZERO on a line it cannot write: the CPU would raise that fault at this instruction, so the next handlers |
| 286 | // see it instead of the #UD (RIP still points at the instruction). | 383 | // see it instead of the #UD (RIP still points at the instruction). |