14 Commits

Author SHA1 Message Date
JeremySu0818
40ac1f059a x64: fix ISA conformance and x86 kernel regressions
Enforce VEX reserved-field and compatibility-width rules, correct BMI feature gating, and repair affected SIMD and x87 semantics. Serialize every locked x86 RMW path so an unaligned software update cannot race an overlapping aligned host atomic.

Make CONFIG_WASM_X86 kernels use a 32 KiB shadow stack. Deep reference-SIMD frames could exceed the former 16 KiB allocation and later surface as SLUB or page-cache corruption. Capture x86 page-fault present bits before fault handling changes the mapping, and bump the kwemu contract to ABI 6.

Add physical VEX encoding coverage, locked-atomic and mixed-ISA kernel workloads, a stack-bound instrumentation audit, and a disposable amd64 Alpine userland regression. Extend kernel matrix output to retain stdout separately from host stderr so native fault and RNG comparisons remain exact.

The audit covers x64/i386 differential execution, atomics, UP/SMP and JIT/no-JIT kernel runs, and real amd64 SSH and Python userland checks. It retains the documented limits around scheduler placement, non-x86 hosts, browser coverage, and broader workload matrices.
2026-11-30 14:57:58 +01:00
Pooya Parsa
936c28b3bf kwemu: one library with both front ends (arm64 and x86 per task)
The x86 front end no longer replaces A64 at build time: libkwemu exports
the A64 ABI and the x86 one (kw_x64_run, kw_x64_set_isa,
kw_x64_isa_supported, kw_x64_complete_entropy, kw_x64_load_segment) on
the same struct kw_cpu, and include/kw_emu.h (ABI 5) describes both
(struct kw_x64_regs; KW_EV_FPE, KW_EV_GPF and KW_EV_ENTROPY for x86).
kw_cpu_init and kw_host_reset set up both.

x86 blocks have their own sets in the block cache (blockSetFor: the top
set bit flipped), so the blocks of the two ISAs at one pc never share a
set and switching between an arm64 and an x86 task needs no flush. The
compile-time is_x64 switches in src/emu become comptime hooks of the
tier that looks a block up (Blocks.x86): the i386 key strip, the A64
stats hooks, and the shared x86 code-page set, which only x86 decodes
mark (A64 keeps IC IVAU); a write-TLB fill reads it only once some x86
code has been marked.

zig build test runs the A64 tests as before; zig build test-x64 runs the
x86 ones (kwemu-x64-harness*, installed with the x86 guests by
install-x64). The object bundles compiler-rt (the x86 front end's
128-bit and x87 arithmetic).

Co-authored-by: JeremySu0818 <xinghong.su0818@gmail.com>
2026-11-29 23:04:44 +00:00
JeremySu0818
e28415201a fix(x64): harden x86-64/i386 ISA semantics, kernel ABI 6, and differential audit suite
Broaden x86-64-v3 and i386 architectural conformance across the reference
interpreter, decoded tier, Wasm SIMD JIT, and Linux x64/ia32 kernel ABI
(KW_ABI_VERSION 6; AArch64 ABI unchanged):

- SIMD, AVX, MMX, and FP: implement VTESTPS/VTESTPD (including the
  reported llama.cpp c4 e2 79 0e c9 form), PHMINPOSUW, DPPS/DPPD,
  MPSADBW, CVTDQ2PD, CVTPD2DQ/CVTTPD2DQ, and MOVNTDQ; fix MMX 64-bit
  lanes, SSSE3 MMX dispatch, immediate shift saturation, INSERTPS,
  VMOVSS/VMOVSD register merging, PCMPxSTRx equal-ordered/mask rules,
  all 32 AVX comparison predicates, COMI/UCOMI NaN signaling, DAZ/FTZ
  and unmasked MXCSR preservation, FMA NaN sign/payload, ROUND,
  VCVTPS2PH, RCP/RSQRT special values, scalar/widening memory operand
  widths, and precise AVX2 gather fault restart.
- x87: fix pending-exception retirement vs WAIT/FNSTSW, memory-fault
  precedence, environment save/restore, stack faults, precision/rounding
  controls, integer/BCD conversions, constants, and 160-digit-verified
  transcendental argument reduction and status flags.
- Scalar, segmentation, control flow, and exceptions: fix zero-count
  32-bit shift zero-extension and memory write checks, read-only RMW
  flag preservation on fault, #AC scalar alignment checks with vector
  exemptions, noncanonical #GP on data/branches/FSGSBASE, overlength
  #GP, UD0 page-boundary decode, PROT_NONE fault error codes, POPF NT,
  REX.W/66 branch/stack widths, INT1/INT3 retirement, SYSCALL/SYSRET
  mode rules, BOUND/INTO, XSAVE/XRSTOR component isolation and XGETBV(1),
  i386 segment caches/limits/rights (src/x64/segment.zig) and REP
  partial progress, and fresh kernel/host entropy for RDRAND/RDSEED.
- Single-step and SS interrupt shadow: track RFLAGS.TF transitions and
  MOV/POP SS / LSS interrupt_shadow across runRef, tier/opexec, JIT
  reload/region exits, and Linux user_mode_loop (updated in
  kernel/patches/linux-x64/0003-wasm-implement-x86-64-and-ia32-user-mode-kernel-ABI.patch).
- Tests and docs: add differential audit corpora (isa-audit, fp-audit,
  integer-audit, x87-audit, x87-fp-audit, control-audit,
  descriptor-audit, fault-audit/32, rng-audit, ed25519test), offline
  Intel XED inventory tools, full-core parallelism in test/x64-test.mjs
  and vCPU-budgeted parallelism in test/x64-kernel-audit.mjs, and
  update .agents/x64.md, .agents/testing.md, and test/x64-audit.md.

Verified on desktop (24-core x86-64 host):
- x64 normal & atomics: 42/42 JIT unit tests (SIMD & nosimd), 2/2
  object-import checks, and 196/196 physical-CPU guest/runner checks.
- x64 UP/SMP kernel audit: 42/42 passed in parallel (41/42 serial,
  hitting the known intermittent kwtest-smp4 wakeup-placement check).
- AArch64 normal & atomics regressions: 138 passed / 1 expected skip /
  0 failed in JIT unit tests and 77/77 guest checks.
2026-11-30 02:53:22 +08:00
JeremySu0818
7960b6ea13 feat(x64): implement dual-mode x86-64-v3 and i386 emulator core and decoded tier
Add include/kw_emu_x64.h (KW_ABI_VERSION 5, 896-byte kw_regs with YMM, XCR0, GDT TLS slots, and x87 state), dual-mode x86-64-v3 + IA32_EMULATION instruction decoder/interpreter/decoded-op tier in src/x64/, 80-bit extended-precision x87 + MMX, AES-NI/SHA-NI/PCLMULQDQ/FMA3/F16C/BMI1/BMI2 semantics, ELF64+ELF32 harness and syscall translation in src/emu/harness_x64.zig, and differential guest + unit tests comparing bit-for-bit against physical x86-64 hardware.
2026-11-29 01:48:46 +08:00
Pooya Parsa
d5a2597183 Give user space a 48-bit address space (256 TiB), as arm64 with 48-bit VAs: four-level Sv48-format page tables in the kernel and kwemu, so Ladybird's 4 TiB GC cage fits; check it in kwtest
The user page tables gain a level: the PGD now holds PUD tables (P4D
folded), and kwemu's walker and the kernel's uaccess walk read four
levels. TASK_SIZE is 1 << 48, and kwemu faults addresses at or above it.
KW_ABI_VERSION stays 4: no release carries ABI 4 yet.
2026-11-23 23:29:55 +00:00
Pooya Parsa
5bdd68882d Give user space the whole 39-bit address space (512 GiB), as arm64 with 39-bit VAs: .NET's 256 GiB GC regions reservation now fits
TASK_SIZE was 1 << 38, the lower half of Sv39 carried over from the
riscv64 guest, so the .NET runtime failed at startup ("GC: Reserving
274877906944 bytes (256 GiB) for the regions range failed"). The kernel
never maps its own memory in the user page tables, so user space can
have every address the three levels index. kwemu now faults addresses
at or above 1 << 39 instead of accepting a sign-extended upper half,
which would alias pgd slots 256-511. KW_ABI_VERSION is 4.
2026-11-23 21:58:05 +00:00
Pooya Parsa
8282a0665c kwemu: a 128K-op tenured ring beside the 320K-op nursery (KW_CPU_SIZE 9 MiB, ABI 3), which holds the compiled code of one run of a Bun-based program so that the next run reuses it; record the 2026-11-20 profiling findings in the agent docs and drop what they contradict 2026-11-21 10:21:45 +00:00
Pooya Parsa
a1acf312e0 Remove the riscv64 CPU, reset the kwemu ABI to 2
The riscv64 interpreter, its JIT emitter, test guests and drivers are
gone; kwemu keeps the ISA-neutral core (MMU, TLBs, code marking, block
cache, JIT core) and the A64 CPU. The arm64 user ABI is one kernel
patch, the last of the series, and the contract between the kernel and
kwemu starts again at KW_ABI_VERSION 2.
2026-11-19 00:12:39 +00:00
Pooya Parsa
1153cea626 a64: LSE atomics, SHA-512 and SHA-3
FEAT_LSE, FEAT_SHA512 and FEAT_SHA3 in the interpreter and the tier,
with their HWCAP bits and ID register fields in the kernel and checks in
kwtest.
2026-11-19 00:12:38 +00:00
Pooya Parsa
e43aaddca2 a64: run user code through a decoded-op tier
The decoded-block cache is generic over an ISA's block hooks. A64 code
is decoded once into ops (one instruction, one op) that runOps executes,
with coherence on SMP through a shared code epoch. The tier is tested
against the reference interpreter.
2026-11-19 00:12:38 +00:00
Pooya Parsa
783c48c76e Switch the guest CPU to AArch64
User space is now unmodified aarch64 Linux ELF. kwemu gets an A64
interpreter (src/a64): integer, scalar and vector floating point,
AdvSIMD, exclusives and load-acquire/store-release, the EL0 system
registers, AES, PMULL, SHA1, SHA256 and CRC32. The kernel runs arm64
user space (signal frames, HWCAP, ptrace regsets, the arm64 syscall ABI),
and the guest image, the bundle, kwtest and the page's texts are built
for aarch64.
2026-11-19 00:12:38 +00:00
Pooya Parsa
1662c969f3 Let TLB flushes skip a CPU that compiles JIT code or yields to its host
On SMP a CPU inside a JIT compile (kwemu's gathering and emitting, the
host's new Module and Instance, V8's first-call compile) or inside its
20 ms yield to the host took no interrupt for 0.1 ms to tens of ms, and
every TLB shootdown to it waited that long.

kwemu (ABI 8) marks a compile with jit_busy in struct kw_cpu and does a
flush another CPU asked for through flush_all when it ends, then looks
the block up again; a compile waits while an interrupt is pending. Every
module returns at once for cpu = 0, and the host calls it so inside
kw_host_jit_compile (new flags argument), so V8's lazy compile falls in
the window. The kernel (patch 0037) skips compiling and yielding CPUs as
it skips lazy ones, and yields after taking pending interrupts. The JIT
host no longer nudges the collector on a free.

kwipi churn on 2 CPUs: shootdown p99.9 130 to 13 us, max 20 to 3.9 ms,
calls over 1 ms 194 to 3; throughput and compile counts unchanged
(kwemu.md, Numbers, Compile stalls).
2026-11-17 15:51:42 +00:00
Pooya Parsa
55095f50bf Boot an SMP kernel on several CPUs in Node (SMP step 2)
A second kernel build (kw_smp.config) runs one instance of the module
per CPU, each in a worker thread on a shared memory. The host's Kernel
is CPU 0 with every device; wasm_cpu_start starts the others through
Platform.spawnCpu (js/node/cpu.ts), which run js/core/cpu.ts: their own
JSPI task switching, the clocks on CPU 0's time base, and console and
exit forwarded to CPU 0's host. The host sets IRQ bits atomically and
ends CPU 0's idle on Atomics.waitAsync of its word, which other CPUs'
IPIs notify. createKernel's cpus option and the CLI's --smp N (which
implies --worker) ask for CPUs; a UP kernel ignores them with a log
line, and more than one CPU runs the interpreter only.

kwemu built with -Dkwemu-atomics makes AMOs atomic, SC a
compare-and-swap against the value LR read, and FENCE a barrier (ABI
7). kwtest gains an smp check (threads on several CPUs counting
through AMOs, LR/SC and a mutex) and skips the park check on an SMP
kernel, which has no parking. The kernel side is in five new patches.
2026-11-16 16:07:40 +00:00
Pooya Parsa
26d08730cc Initial commit 2026-11-15 23:27:04 +00:00