upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/cpu_emulation.h
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 18:48:12 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 18:48:12 -0500
commitfb2afdccc2314d7b5a59fac10e6d6df7f5df35ab (patch)
tree9e0b7e2e39b42823f148d135ce781e57fab52155 /runtime/src/cpu_emulation.h
parent862890c893eb19eb39eb2fd95e7786e42ff4b18c (diff)
parent864337351bc11a250db77c2fcdd3ba6f1d0f455f (diff)
Merge main into RT-QUEUE18 (#403)
Merged on top of the merge made for #396 (main 507029d1e plus RT-QUEUE35, RT-ALLOC9 and RT-LIST139). No conflicts. 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.h97
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
275inline 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
285enum class ClzeroLoopResult { no_match, ok, faulted };
286
287inline 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.
305inline 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
275inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { 353inline 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).