30 Commits

Author SHA1 Message Date
Pooya Parsa
75694e278f kernel: optimize the lowered kernel with Binaryen -O2 (about 4% smaller, Debian shell commands about 3.5% faster in Node) and build kwemu with bulk memory, sign-ext, nontrapping-fptoint and mutable globals, which the kernel's C objects already need 2026-11-24 22:09:38 +00:00
Adam Meghji
c28018f766 a64/jit: inline scalar FP zero, IXC, FMIN/FMAX, and FMADD fast paths (#20) 2026-11-24 17:40:14 +00:00
Adam Meghji
01567f9b65 a64/jit: translate AdvSIMD FP fast paths with WASM SIMD (#21) 2026-11-24 15:31:08 +00: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
893434222c kwemu: give the JIT a 32 KiB emit buffer, grow regions towards their own page and self-loops, and test the closed bit inline: 7-Zip's LZMA benchmark 677 to 780 MIPS
Exits from closed regions dominated 7-Zip's match finder: regions closed when
the 16 KiB buffer filled, so a hot page's blocks spread over many modules and
compiled code returned to run once every 34 guest instructions. A self-loop now
joins a region as a guest (its back edge branches to the member's own loop),
with eager NZCV like every member; the A64 block fuzz found a stale NZCV with
lazy flags there. The stats build's generic-op table shrinks to 256 entries so
it still fits KW_CPU_SIZE; no ABI change.
2026-11-23 18:12:15 +00:00
Pooya Parsa
44ff5a8dd9 a64: poll the code epoch with a plain load, and give it and each CPU's pending word 128 bytes: JavaScriptCore runs an atomic load as a read-modify-write, so 4 CPUs ran user code at 0.35x of one in Safari 2026-11-23 09:01:43 +00:00
Pooya Parsa
c46e019741 kwemu: a region takes the blocks of its page that have counted a quarter of the compile threshold (pageWarm), and exit growth waits for 32768 exits per KiB of module instead of 8192: a test/app-bench.mjs boot compiles about 4900 modules instead of 6500, 21% less compile time, and Node, Bun and Claude Code run 2-9% faster or unchanged on 1 and 2 CPUs; record the 2026-11-21 measurements and the rejected variants in the agent docs 2026-11-21 19:01:57 +00:00
Pooya Parsa
fe8c977333 test: add app-bench (Node, Bun and Claude Code timings of several bundles on the profile disk, with --stats counters and per-variant guest variables); kwemu: count freed modules by reason in the stats build (Jx.free_why, the CLI's [kwemu-free] line); record the 2026-11-21 findings in the agent docs: the tenured ring and the slot pool are stacked limits, a 512K ring with 4096 slots is no faster, and a shared op store keyed by physical page is rejected 2026-11-21 14:13:01 +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
bcf1782092 Name the file and offset of each profile sample, so that the same code adds up across processes, and symbolize sites from a copy of the guest root (--profile-root, symbolizeProfile) 2026-11-20 18:02:53 +00:00
Pooya Parsa
ccf57e51a9 Add a guest pc sampling profiler driven from the host: time samples through a profile IRQ bit set by a sampler thread (SMP kernel) and instruction samples through the budget, per-CPU rings in the kernel, the tier and the block before each stop from kwemu, and --profile in the CLI 2026-11-20 16:38:47 +00:00
Pooya Parsa
f13df7328d a64: count where compiled passes go next (Jx.next_by) in stats builds 2026-11-19 17:34:36 +00:00
Pooya Parsa
c087d31856 Move the kwemu test guests from guest/ to test/guest/ 2026-11-19 07:37:16 +00:00
Pooya Parsa
bb8756ec67 update docs 2026-11-19 07:30:14 +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
81ecb6f56d a64: translate atomics inline, make cold code cheaper
Exclusives, LDAR/STLR and the LSE atomics run inline (wasm atomics on
SMP), as do DC ZVA, barriers, loads into XZR and USHL/SSHL. The
code-page filter scans the write TLB only when a mark slot changes
state, and runOps has one copy of the path that leaves a block.
2026-11-19 00:12:38 +00:00
Pooya Parsa
3414200e47 a64: translate AdvSIMD, AES, PMULL and SHA with wasm SIMD
With a host that compiles wasm SIMD (a JIT host capability; the scalar
form remains as the fallback), V registers live in v128 locals and the
integer AdvSIMD ops, AES rounds, PMULL and the SHA families are
translated inline. The image's libcrypto picks its code paths from
HWCAP again, and SHA512 is advertised by default.
2026-11-19 00:12:38 +00:00
Pooya Parsa
1f9f0c5e5f a64: keep compiled code across op-arena resets, translate scalar FP inline
The op arena is a nursery plus a tenured ring that holds the ops of
compiled blocks, so a reset keeps compiled modules and every block's
compile countdown; the block table replaces the entry with less
progress. Scalar FP arithmetic and conversions run inline where the
result is exact. Marking more chunks of a code page that is already
marked no longer scans the write TLB.
2026-11-19 00:12:38 +00:00
Pooya Parsa
0169bdbb57 a64: call the interpreter from compiled code, translate the frequent generic ops
Ops the JIT does not translate no longer end a compiled block: compiled
code calls the reference interpreter for them through the kernel's
function table (a JIT host capability). FMOV, UMOV/INS/DUP, MOVI, vector
logic, MRS of FPCR/FPSR/NZCV/DCZID and FCMP are translated inline.
2026-11-19 00:12:38 +00:00
Pooya Parsa
5f3a2b3688 Rebuild the guest packages for aarch64, add ~/.local/bin and the stand-ins
busybox, zlib, openssl and apk-tools are rebuilt from pinned aports
commits for kwemu and installed by default. ~/.local/bin is first on
PATH for login shells and kwtty's programs, and the page writes fx,
claude and opencode stand-ins there that install the real program on
first use.
2026-11-19 00:12:38 +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
0573a18e47 a64: compile hot code to wasm in the JIT
The JIT's ISA-neutral core (regions, thresholds, entry, growth) is
factored out behind ISA hooks, and the A64 emitter sits on it with one
file per op family: ALU, bitfield, multiply and divide kinds with lazy
NZCV, every load and store kind, and every branch kind with side exits.
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
3b94faa2c4 Run kwemu's JIT on every CPU of an SMP kernel (SMP step 4)
Each CPU's thread now has a JitHost of its own, bound to its instance's
function table, so each CPU's kwemu compiles what it finds hot into its
own table; nothing compiled on one CPU is reachable from another. The
host no longer turns the JIT off with more than one CPU. A secondary's
JIT host mirrors its counters into a SharedArrayBuffer: stats().jit is
the sum over CPUs, stats().jitCpus and kwemuCpus are per CPU, and the
CLI's --stats prints a line per CPU.

A secondary CPU's thread never reached its event loop, so V8 ran neither
FinalizationRegistry callbacks nor the last pass of its collections
there: the JIT host saw nothing collected, prompted collections with
ever larger allocations (one stalled a CPU for 0.58 s while a TLB
shootdown waited for it) and would have refused to compile after 2048
frees. Every CPU now yields to its host every 20 ms of user code (the
kernel side is folded into wasm: SMP), and a secondary's sched_yield
returns through its event loop.

kwemu: compiled code reads the interrupt word with i32.atomic.load when
the memory is shared (a plain load in a loop that stores nothing may be
hoisted, and an IPI would go unseen), and an interpreted self-loop that
an interrupt ends now counts its passes towards the compile threshold
(under a stream of IPIs hot loops were never compiled). A unit test
covers the latter.

kwtest gains jit: checks for SMP kernels: hot() and fphot() on every CPU
at once under signals, byte and halfword stores beside AMOs and LR/SC in
one cache line, a thread moved every 4 ms while in compiled loops, and
code that one CPU runs compiled while another rewrites it (with
__builtin___clear_cache), replaces it by mremap() or makes it
non-executable. The docs describe the guarantees (kwemu.md, JIT on
several harts) and the numbers.
2026-11-16 19:38:24 +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
fccde8ff93 Build kwemu with atomics and boot a shared-memory kernel (SMP step 1, WIP)
-Dkwemu-atomics adds the wasm atomics and bulk-memory features to the
wasm64 objects, so wasm-ld can link them into a kernel built with
--shared-memory. The JIT declares its env.memory import shared when the
host reports caps bit 3, the host creates a shared WebAssembly.Memory
when the module's import is shared (found by retrying the instantiation
on a LinkError that says so), and the WASI imports and the JIT host copy
out of the memory where browsers refuse shared views.
2026-11-16 13:12:22 +00:00
Pooya Parsa
26d08730cc Initial commit 2026-11-15 23:27:04 +00:00