Commit Graph

19 Commits

Author SHA1 Message Date
João Lucas
6901f8a225 js: allocate an SMP kernel's memory once, shared, at a boot and a restore, so that memoryFallbackMiB applies to it (#10)
An SMP kernel imports a shared memory, which the JS API does not show.
boot() assumed an unshared one, allocated it, got a LinkError and
allocated again, shared. It now guesses the sharing from the atomics
feature in the module's target_features section, as moduleIsMemory64()
does for the memory type; the retry stays for a wrong guess (a module
with atomics on an unshared memory).

memoryFallbackMiB only applied to the unshared allocation: where the
shared one then failed, boot was rejected with the LinkError and no
fallback was tried. It now applies to the shared allocation.

Co-authored-by: Pooya Parsa <pooya@pi0.io>
2026-11-25 01:30:03 +09:00
Pooya Parsa
a0506f405b js: unwind the JSPI stack of an exited task and of one started afresh (a move to another CPU, a park cycle) instead of parking it for good: the suspended import throws STACK_DROPPED, so the engine frees the stack at once rather than at a garbage collection, which Firefox's limit of about 2000 stacks per thread did not wait for ("too many continuation stacks" after a while of kmscube on 2 CPUs) 2026-11-22 12:48:12 +00:00
Pooya Parsa
7d4272fc4f js: log an error with its name and message where the engine's stack leaves them out (errorText): on Firefox and Safari a trap on another CPU was logged as its frames alone 2026-11-22 12:29:17 +00:00
Pooya Parsa
178298148d kernel, js: sync and drop file caches before a snapshot that stops the machine (SnapshotOptions.dropCaches, IRQ 11), and encode and restore a snapshot from its used chunks instead of scanning the machine's whole memory 2026-11-22 11:03:31 +00:00
Pooya Parsa
d4c9b459f7 js, web: default memory 2 GiB on desktops and Node and 512 MiB on phones, fallback sizes from 1024, 2 and 4 GiB on the page's slider, and a sparse copy of a snapshot on its way to a worker 2026-11-22 09:48:43 +00:00
Pooya Parsa
2781a13253 js: restore a snapshot without touching its zero pages (a resumed 4 GiB machine took 4 GiB of host memory), and take a 4 GiB snapshot across the worker boundary (Node 24 clones such a view with length 0) 2026-11-22 09:32:12 +00:00
Pooya Parsa
84d70b9b83 gl: load the renderer on the guest's first GL use (the device advertises virgl's capsets from fixed sizes; gl.eager, ?glload=eager load before boot) and turn guest GL on by default for Debian in the page, with the virtio-gpu device (?gpu=0: the vtest link only); the kw layer ships /etc/X11/kw-kms.conf (modesetting with glamor on /dev/dri/card0) and sets the vtest variables only without the device; virtio-gpu waits up to 30 s, killably, for a capset; a suspend while a program uses the GPU is refused at once with a message 2026-11-21 22:57:45 +00:00
Pooya Parsa
4d20370925 Give the guest /dev/dri: kwvirtio, a virtio transport over host imports, with the stock virtio_gpu driver (createKernel({ gpu }), --gpu); the SDK's device model shows dumb-buffer scanouts on the host display and, with gl, runs virgl 3D on the page's renderer through virglrenderer's API (patch 13), presents a KMS scanout from the renderer's texture without readback and gives host code a resource's texture (vm.gl.gpu.texture); the kernel leaves out I2C, the HDMI infoframe library, the panel bridge and NET_DEVMEM (four generic switches) and backports the virtio-gpu damage-clip fix 2026-11-21 20:27:49 +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
18cbbe2a3e js: hold the kernel at its snapshot point for a commit and an optional hold, so that it stops only once the snapshot is stored and else goes on; answer every snapshot before the worker's exit; record detachable terminal sessions in a snapshot and attach to them after a restore 2026-11-20 12:04:45 +00:00
Pooya Parsa
a30598df0c Park, snapshot and resume the SMP kernel: a rendezvous of every CPU at the consistent point, a restore that starts the other CPUs again and restarts each idle task, the CPU count in the snapshot, and Suspend on the page with several CPUs 2026-11-20 08:27:54 +00:00
Pooya Parsa
70a0e0a82e SDK: send every CPU's console output through a shared ring
With more than one CPU, each CPU's fd_write puts its bytes into one
shared ring (js/core/console-ring.ts), reserving its place inside the
import, so the ring keeps the kernel's order. With the shared transport
the owner reads it (Atomics.waitAsync), independent of CPU 0, and a full
ring makes writers wait; inline and with --worker=messages CPU 0's host
reads it, and another CPU that finds it full posts the records as a
numbered batch instead of waiting for CPU 0.

Output from a secondary CPU no longer arrives as one message per write
nor waits for CPU 0 to suspend: seq 1 200000 on CPU 1 takes as long as
on CPU 0, and a shell on CPU 1 echoes in under a millisecond while CPU 0
runs user code (10 ms before).

ConsoleOutput's unread backlog dropped old chunks with Array.shift(),
which copied a long array per chunk: drop them by index.
2026-11-17 14:02:29 +00:00
Pooya Parsa
82c7d36d92 SDK: start an SMP kernel's CPUs as Web Workers in browsers
js/web/cpu.ts is the browser Platform.spawnCpu: each CPU is a module
Web Worker running runCpu(), nested in worker mode's kernel worker.
webCpuBlocker() (Platform.whyNoCpus) gives the kernel one CPU where a
thread cannot run CPUs: no cross-origin isolation, no Worker, no
Atomics.waitAsync, or a page's main thread, where CPU 0's
memory.atomic.wait32 would trap. stats().cpus says how many CPUs the
kernel got. The dist inlines the CPU worker as a Blob URL, in the web
entry and inside the web kernel worker.
2026-11-17 11:26:41 +00:00
Pooya Parsa
19c0d2aba4 Keep console output from different CPUs in the kernel's order (SMP)
A secondary CPU's console writes reached the host as messages, which CPU
0's host takes only when CPU 0 suspends, while CPU 0's own writes went
out at once, so lines printed on different CPUs interleaved. Every
fd_write now takes a number from a shared counter inside the import,
while the kernel still holds the lock that orders the writes, and CPU
0's host writes them by number (ConsoleOrder in js/core/cpu.ts). The
kernel is unchanged. An api test prints lines in turn from four
processes and checks their order.
2026-11-17 10:44:57 +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
bc9403d6be Move tasks between CPUs where their stack can be dropped (SMP step 3)
A user task may now move to another CPU at the reschedule on its way to
user mode, where its state is all in pt_regs, and starts there on a
fresh stack; switched in on its own CPU it resumes its stack as before.
The load balancer and affinity changes move tasks: sched_setaffinity()
records the mask at once and the task moves on its way to user mode,
instead of failing with EBUSY. A task asleep in a system call stays
until it returns. A workqueue rescuer that serves another CPU's per-CPU
pool moves there instead of running that CPU's work on its own.

The CPU that held a moved task's stack calls the new import
wasm_stack_drop(task), and its host forgets that stack. A secondary
CPU's host also raises the stop IPI on CPU 0 when the session ends there
(exit, trap, fatal error), which the kernel takes even while spinning
with interrupts off, so CPU 0 suspends and its host ends the session.

kwtest gains move checks (threads spreading from one CPU, register, FP
and TLS state across 300 self-moves, affinity changes of a busy and a
sleeping thread, queued signals and pipe and futex ping-pong across
moves) and `kwtest smp`, which runs only the SMP checks. The kernel side
is folded into the sched, kthread and workqueue, and wasm: SMP patches.
2026-11-16 17:41:34 +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