diff options
| author | oscarlosu-lab <251827664+oscarlosu-lab@users.noreply.github.com> | 2026-11-24 05:54:10 +0100 |
|---|---|---|
| committer | oscarlosu-lab <251827664+oscarlosu-lab@users.noreply.github.com> | 2026-11-24 05:54:10 +0100 |
| commit | 361e8455a20aa6606fb542d86e39b552da2e60e6 (patch) | |
| tree | cc0812f1e3b090bf7c7f50f51d9e37343f3fd74b /runtime/src | |
| parent | d2cd6c0d970083eee4d7b40daabb2480a77b1790 (diff) | |
cpu_emulation: run a whole CLZERO zeroing loop in one emulated step
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/cpu_emulation.h | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/runtime/src/cpu_emulation.h b/runtime/src/cpu_emulation.h index fac13eeee..bd864c861 100644 --- a/runtime/src/cpu_emulation.h +++ b/runtime/src/cpu_emulation.h | |||
| @@ -272,6 +272,81 @@ 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 | // Fast-path for the Zen 2 zeroing loop: `clzero; add rax, 0x40; cmp rax, reg64; jb -12` (12 bytes). | ||
| 288 | // Zeroes the entire writable span in one memset instead of raising #UD + VirtualQuery per 64-byte line. | ||
| 289 | inline ClzeroLoopResult try_clzero_loop(const uint8_t* code, std::size_t avail, CONTEXT* c, EXCEPTION_RECORD* fault) { | ||
| 290 | if (avail < 12) return ClzeroLoopResult::no_match; | ||
| 291 | if (code[0] != 0x0F || code[1] != 0x01 || code[2] != 0xFC) return ClzeroLoopResult::no_match; | ||
| 292 | if (code[3] != 0x48 || code[4] != 0x83 || code[5] != 0xC0 || code[6] != 0x40) return ClzeroLoopResult::no_match; | ||
| 293 | if ((code[7] != 0x48 && code[7] != 0x4C) || code[8] != 0x39 || (code[9] & 0xC7) != 0xC0) | ||
| 294 | return ClzeroLoopResult::no_match; | ||
| 295 | if (code[10] != 0x72 || code[11] != 0xF4) return ClzeroLoopResult::no_match; | ||
| 296 | |||
| 297 | const int limit_reg = ((code[9] >> 3) & 7) | ((code[7] & 4) << 1); | ||
| 298 | if (limit_reg == 0 || limit_reg == 4) return ClzeroLoopResult::no_match; | ||
| 299 | |||
| 300 | const uint64_t start = c->Rax; | ||
| 301 | const uint64_t limit = gpr_read(c, limit_reg); | ||
| 302 | if ((start & 63) != 0 || limit <= start + 64) return ClzeroLoopResult::no_match; | ||
| 303 | |||
| 304 | uint64_t total = (limit - start + 63) & ~uint64_t(63); | ||
| 305 | constexpr uint64_t kMaxBatch = uint64_t(64) << 20; | ||
| 306 | if (total > kMaxBatch) total = kMaxBatch; | ||
| 307 | const uint64_t end = start + total; | ||
| 308 | |||
| 309 | for (uint64_t cur = start; cur < end;) { | ||
| 310 | const DWORD err = writable(cur); | ||
| 311 | if (err) { | ||
| 312 | if (cur > start) { | ||
| 313 | std::memset(reinterpret_cast<void*>(start), 0, std::size_t(cur - start)); | ||
| 314 | c->Rax = cur; | ||
| 315 | } | ||
| 316 | if (fault) { | ||
| 317 | std::memset(fault, 0, sizeof *fault); | ||
| 318 | fault->ExceptionCode = err; | ||
| 319 | fault->NumberParameters = 2; | ||
| 320 | fault->ExceptionInformation[0] = 1; | ||
| 321 | fault->ExceptionInformation[1] = ULONG_PTR(cur); | ||
| 322 | } | ||
| 323 | return ClzeroLoopResult::faulted; | ||
| 324 | } | ||
| 325 | const uint64_t next_page = (cur & ~uint64_t(0xFFF)) + 0x1000; | ||
| 326 | cur = next_page < end ? next_page : end; | ||
| 327 | } | ||
| 328 | |||
| 329 | std::memset(reinterpret_cast<void*>(start), 0, std::size_t(total)); | ||
| 330 | c->Rax = end; | ||
| 331 | if (end >= limit) { | ||
| 332 | c->Rip += 12; | ||
| 333 | const uint64_t diff = end - limit; | ||
| 334 | uint32_t flags = c->EFlags & ~DWORD(0x8D5u); // clear CF(0), PF(2), AF(4), ZF(6), SF(7), OF(11) | ||
| 335 | // CF is 0 because end >= limit (unsigned). | ||
| 336 | uint8_t lo8 = static_cast<uint8_t>(diff); | ||
| 337 | lo8 ^= lo8 >> 4; | ||
| 338 | lo8 ^= lo8 >> 2; | ||
| 339 | lo8 ^= lo8 >> 1; | ||
| 340 | if ((lo8 & 1u) == 0) flags |= 0x4u; // PF | ||
| 341 | if ((end ^ limit ^ diff) & 0x10u) flags |= 0x10u; // AF | ||
| 342 | if (diff == 0) flags |= 0x40u; // ZF | ||
| 343 | if (diff >> 63) flags |= 0x80u; // SF | ||
| 344 | if (((end ^ limit) & (end ^ diff)) >> 63) flags |= 0x800u; // OF | ||
| 345 | c->EFlags = flags; | ||
| 346 | } | ||
| 347 | return ClzeroLoopResult::ok; | ||
| 348 | } | ||
| 349 | |||
| 275 | inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { | 350 | inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { |
| 276 | if (info->ExceptionRecord->ExceptionCode != STATUS_ILLEGAL_INSTRUCTION) return EXCEPTION_CONTINUE_SEARCH; | 351 | if (info->ExceptionRecord->ExceptionCode != STATUS_ILLEGAL_INSTRUCTION) return EXCEPTION_CONTINUE_SEARCH; |
| 277 | CONTEXT* c = info->ContextRecord; | 352 | CONTEXT* c = info->ContextRecord; |
| @@ -281,6 +356,25 @@ inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { | |||
| 281 | const Decoded d = decode(code, avail); | 356 | const Decoded d = decode(code, avail); |
| 282 | if (d.op == Op::none) return EXCEPTION_CONTINUE_SEARCH; | 357 | if (d.op == Op::none) return EXCEPTION_CONTINUE_SEARCH; |
| 283 | EXCEPTION_RECORD fault; | 358 | EXCEPTION_RECORD fault; |
| 359 | if (d.op == Op::clzero && !d.addr32 && d.length == 3) { | ||
| 360 | const uint64_t fault_rip = c->Rip; | ||
| 361 | const ClzeroLoopResult lr = try_clzero_loop(code, avail, c, &fault); | ||
| 362 | if (lr == ClzeroLoopResult::ok) { | ||
| 363 | if (count(d.op).fetch_add(1, std::memory_order_relaxed) == 0) | ||
| 364 | std::fprintf(stderr, "[rt] cpu: this CPU has no %s; emulating it (first at %p)\n", name(d.op), | ||
| 365 | reinterpret_cast<void*>(fault_rip)); | ||
| 366 | return EXCEPTION_CONTINUE_EXECUTION; | ||
| 367 | } | ||
| 368 | if (lr == ClzeroLoopResult::faulted) { | ||
| 369 | EXCEPTION_RECORD* r = info->ExceptionRecord; | ||
| 370 | r->ExceptionCode = fault.ExceptionCode; | ||
| 371 | r->ExceptionFlags = 0; | ||
| 372 | r->NumberParameters = fault.NumberParameters; | ||
| 373 | r->ExceptionInformation[0] = fault.ExceptionInformation[0]; | ||
| 374 | r->ExceptionInformation[1] = fault.ExceptionInformation[1]; | ||
| 375 | return EXCEPTION_CONTINUE_SEARCH; | ||
| 376 | } | ||
| 377 | } | ||
| 284 | if (!apply(d, c, &fault)) { | 378 | 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 | 379 | // 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). | 380 | // see it instead of the #UD (RIP still points at the instruction). |