Commit Graph

87 Commits

Author SHA1 Message Date
Pooya Parsa
a962f5cf9e Add guest swap: zram, or zswap in front of a swap file on the data disk, as SDK, CLI and page options 2026-11-30 23:44:09 +00:00
Pooya Parsa
bf46d833d0 kernel: give the Kubernetes options a bpf(2) for device filters only
BPF_SYSCALL with CGROUP_BPF was 70% of what the options cost, for runc's device filter. A new generic patch, BPF_CGDEV_ONLY, loads and attaches cgroup device programs only and runs them on the interpreter, with a verifier of its own: the SMP kernel grows 128 KB gzip instead of 460 KB. Checked with a runc container on cgroup v2.
2026-11-30 14:25:37 +01:00
Pooya Parsa
ed876d7f79 kernel: make kwdiff take a far match only when it is clearly longer, so the SMP patch compresses better
A match more than 4 KiB away from where the current one continues now has to be 32 bytes longer instead of 8. New code otherwise became tens of thousands of short copies from unrelated places, each a seek a compressor cannot predict. The SMP patch with the Kubernetes options goes from 1.51 to 1.30 MB gzip; the format, the loader and the time to make it are unchanged.
2026-11-30 14:25:37 +01:00
Pooya Parsa
708a18ef58 kernel: enable what a Kubernetes node needs in the SMP kernel
The cgroup v2 memory, CPU and cpuset controllers with CPU limits, bpf(2)
with cgroup BPF, keys, bridge netfilter, the iptables pieces kube-proxy and
flannel use, nft ct and masq, the conntrack table in procfs, and VXLAN, in
kw_smp.config only: k3s needs cpuset, which needs SMP, so the UP kernel
stays as it is.

Co-authored-by: Greg Bell <code@gregbell.ca>
2026-11-30 14:25:37 +01:00
pi0x
7e4f3d73cd kernel: leave BPF arena maps out with a flat vmalloc (#107)
A BPF arena needs get_vm_area(), which VMALLOC_FLAT does not have; with
the patch, arena maps fail with EINVAL as on kernels without an MMU.
BPF_SYSCALL stays off in kw_defconfig, so the patch is dormant by
default; it lets a kernel turn BPF on without importing get_vm_area.

Co-authored-by: Greg Bell <code@gregbell.ca>
2026-11-30 10:42:27 +01:00
Pooya Parsa
0097a7989a kernel: export only what the host and the build tools read instead of every symbol, so the lowering drops unused code (the bundle is 1.35 MB smaller) 2026-11-29 20:33:57 +00:00
Pooya Parsa
5425a685a4 kernel: enable FAT, exFAT and iso9660 (Joliet, zisofs), with UTF-8 as the default NLS 2026-11-29 18:46:40 +00:00
Pooya Parsa
ace904ec78 kernel: enable squashfs (zlib, lz4, lzo, xz, zstd, xattrs) and erofs microLZMA 2026-11-29 18:24:50 +00:00
Pooya Parsa
495fe30a91 Add guest USB on WebUSB: kwusb (a USB host controller whose transfers the host serves, with usbfs and the FTDI, CP210x, CH341 and PL2303 serial drivers), the SDK's usb option and vm.usb with a fake test device, and a USB devices section in the page's Settings 2026-11-29 15:43:56 +00:00
Pooya Parsa
42b91da2a8 Add guest Bluetooth on Web Bluetooth: kwbt (an HCI transport, hci0 while a controller is attached), the SDK's bluetooth option and vm.bluetooth with an emulated LE controller, Bluetooth and Serial ports sections in the page's Settings with Terminal buttons, and the Bluetooth and Serial Monitor launcher apps 2026-11-29 15:22:20 +00:00
Pooya Parsa
ab334389af Add guest serial ports on Web Serial: kwserial (/dev/ttyACM0..3), the SDK's serial option and vm.serial, Connect in the page's Settings and from a toast, and the Thonny and GTKTerm launcher apps 2026-11-29 13:31:16 +00:00
Pooya Parsa
a7daa0443d kernel: create no dummy0 at boot (DUMMY_NUMDUMMIES=0); dummy links are added when needed, as tailscale0 is 2026-11-29 12:15:06 +00:00
Pooya Parsa
1b229902b2 Add a guest microphone and camera: kwsnd capture, kwcam (V4L2, raw and host-encoded H.264), and camera frames named to the kwva encoder
Microphone: kwsnd gains a capture substream fed by the host (SDK option
soundIn, the page's Microphone setting and ?mic=0, the CLI's
--sound-in-wav and --sound-in).

Camera: kwcam is a V4L2 capture device (/dev/video0) fed by the host: I420,
NV12 and YUYV at 320x240 to 1280x720, and H.264 encoded by the host's
WebCodecs encoder where it has one, with bitrate, GOP and key frame
controls (SDK option camera, the page's Camera setting and ?cam=0, the
CLI's --camera). The base's rc writes the udev database entry of
/dev/video*, so programs that list cameras through libudev find it.

kwva (protocol 5): an encode whose input surface is a camera buffer's
dma-buf names the frame to the host instead of sending its pixels, and
pixels an app writes into an exported surface reach the host (put).
2026-11-29 10:23:01 +00:00
pi0x
45a024e879 kernel: let busy workqueue workers finish before kernel threads park on SMP, and serve a requested grace period first, so that a park cycle right after a netns teardown no longer fails (#76)
On the SMP kernel a workqueue worker in the middle of an item could wait
for kernel threads that had already parked, never reach its own park
point, and end the cycle with -EBUSY naming it. A netns teardown's
cleanup_net() does that: it waits for grace periods, the expedited
grace-period worker and other work items. A cycle started right after an
`unshare -n` exit failed 13 times in 15 on 2 CPUs and 12 in 15 on 4; beside
a loop of netns teardowns 22 of 30 and 20 of 30 failed. Docker's
containers (#59) make such teardowns routine in Debian guests.

- The TREE_RCU grace-period kthread parks only while no grace period is
  in progress or requested, so that a kernel thread in synchronize_rcu()
  or rcu_barrier() can reach its park point (a fixup of the park points
  patch).
- Between the user and the kernel-thread phases kwparkd waits, at most
  HZ/2, until every workqueue worker is idle (in TASK_IDLE), while every
  kernel thread still runs (kw_park_settle_work(), a fixup of the arch
  patch).

With both, every cycle of those runs passed (120 on 2 and 4 CPUs). An idle
cycle's median went from 17.9 ms to 0.3 ms on 2 CPUs; a cycle that waits
for a teardown takes 60-230 ms. kwtest's park check is unchanged (2-5 ms
mean). parking.md describes the settle step and the grace-period rule.

Based on Martin Brook's patches on the android branch of
martinbrook/unkernel (cc78ca9, 4849ed9). His settle step also ran
rcu_barrier() and waited only for workers in an uninterruptible sleep:
here the barrier cost 40-50 ms in every cycle, and a worker between two
waits (runnable) ended the wait early, so 11 of 15 cycles still failed.

Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: Martin Brook <martin.brook100@googlemail.com>
2026-11-28 22:33:07 +01:00
pi0x
0059e61af5 kernel: read and write aligned user words in one access; lock kwfb mode changes against mmap (#75)
get_user() and put_user() of an aligned 1, 2, 4 or 8 bytes are now one
load or store through kw_user_addr() instead of a memcpy (memory.copy,
which the Wasm spec does not make single-copy atomic on a shared
memory), so a futex word read beside another CPU's stores cannot tear;
an unaligned access still goes through raw_copy_*_user().

kwfb's set_par checks the mappings, allocates and switches the buffer
under info->mm_lock, which fbdev's mmap holds (check_var reads the count
under it too): an mmap racing FBIOPUT_VSCREENINFO from another thread
could map the old buffer.

kwtest checks both (uaccess: FUTEX_WAIT and getcpu beside another CPU's
stores, fault-in, EFAULT and a put_user across a page; kwfb: 200 mmaps
beside a thread changing the mode), and `kwtest smp` runs the kwfb
checks. Ported from VOD-Studio/unkernel@25e90b3f.

Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: xfy <i@rua.plus>
2026-11-28 22:22:20 +01:00
abeer0
68e463dbc5 chore: fix a typo in the fake-llvm wrapper comment (#40) 2026-11-28 20:08:23 +01:00
chungchihhan
6a6a0e396c Enable Docker IPv4 bridge networking in Debian guests (#59)
* Enable Docker IPv4 bridge networking in Debian guests

* kernel: leave out the netfilter and bridge defaults Docker does not use

NETFILTER_INGRESS and NETFILTER_EGRESS (hooks for nft's netdev family,
which is not built), NF_CT_PROTO_SCTP and BRIDGE_IGMP_SNOOPING come on
by default with the options the Docker bridge needs; turning them off
saves 78,289 bytes of vmlinux32.wasm (25,917 bytes gzip). Re-export the
series: 0049, 0052 and 0055 carried index lines for the old
kw_defconfig.

* kernel: fix the conntrack hash size at 1024 buckets

Built-in conntrack allocates its hash table and the NAT bysource table,
which takes the same size, at boot also without rules, sized from RAM:
512 KiB each above 1 GiB, as on the page's 2 GiB default. A new generic
patch adds NF_CONNTRACK_HASHSIZE; kw_defconfig sets it to 1024, 8 KiB
per table, with nf_conntrack_max 8192. A 2 GiB guest keeps about 940 KiB
more free after boot.

---------

Co-authored-by: Pooya Parsa <pooya@pi0.io>
2026-11-28 20:04:04 +01:00
Pooya Parsa
25e0add13f kernel: fold the patch series and export it without churn
Fold the 56 kernel patches into 32: one arch patch (which now includes
the riscv ISA patch, parking, atomics, SMP, blocking waits, unfair
spinlocks, the arm64 user ABI, pc sampling, ptrace, single-step and core
dumps), each driver with its SMP part, the generic changes one per piece
(the cond_resched() poll split out of the arch patch), and kw_defconfig
last. The messages describe the code as it is; every patch that was
kernel-wasm@localhost's is Pooya Parsa's. The tree is unchanged.

kernel/build.sh patches (kernel/tools/export-patches.sh) exports the
series without what changed in every later patch on each edit: no
numbers in the file names (the order is the new series file, which
build.sh applies), a zero commit hash, no git version signature and no
index lines, with the diff options fixed.
2026-11-28 18:55:49 +00:00
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
Pooya Parsa
49f03d0c61 kernel: poll the timer and pending interrupts in cond_resched(), so that direct reclaim lets the OOM victim run (#6) 2026-11-24 18:04:25 +00:00
Pooya Parsa
951bb8e5dd kernel: let kwsnd periods go down to 2 ms, in a buffer of 10 ms or more (#22)
The shortest period and tick drop from 5 ms to 2 ms for programs that
ask for low latency. A buffer is 10 ms or more as before (two 5 ms
periods), and a late tick still hands over up to 15 ms' worth when ticks
are shorter than 5 ms, so a short period tolerates the same host stall.
Programs that leave the period to alsa-lib, plain aplay, OSS without
SETFRAGMENT and requests of 256 frames or more negotiate what they did;
snd_pcm_set_params() with a latency under 20 ms now gets four periods of
2.5 to 3.75 ms (measured on Node, 2026-11-24).

Co-authored-by: Adam Meghji <566793+adammeghji@users.noreply.github.com>
2026-11-24 16:25:03 +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
7fc4e1ff6f kernel: support ptrace debuggers: arm64's register sets, single-step in software, kernel writes into user code flushed for kwemu, and core dumps (23.5 KB), so strace, gdb, gdbserver and ltrace work in the guest; check them in kwtest 2026-11-23 23:22:19 +00:00
Pooya Parsa
2c376d6d66 kernel: place CPU 0's word of pending interrupts inside the image: unplaced by the linker script, it lay past _end in the page allocator's memory, and its writes zeroed a word of the page cache (busybox's udhcpc died of SIGILL) 2026-11-23 22:23:03 +00:00
Pooya Parsa
5a4a324044 kernel: turn on UDMABUF (/dev/udmabuf, 4 KB), which Mesa's lavapipe needs to export its images as dma-bufs 2026-11-23 21:59:46 +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
0b65e72c36 Enable seccomp filters in the kernel (arm64 ABI, 12 KB), check them in kwtest, and drop the Alpine image's bwrap shim, which removed bwrap's --seccomp 2026-11-23 21:18:38 +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
74e8c88957 guest, kernel: let terminal sessions renice their audio threads, and have kwsnd report a period only when one ends
kwtty's sessions get RLIMIT_NICE 40, as pam_limits gives a desktop's audio users: SDL asks for nice -20 for its audio thread, which at nice 0 shares the emulated CPU with the game's busy threads and misses its periods (ALSA underruns, gaps in the sound). kwsnd called snd_pcm_period_elapsed() on every tick, and the core takes a report that falls short of the next period more than half a buffer after the pointer last moved (the first tick after a stall of the machine) for a missed buffer and stops the stream; it now reports when the pointer crosses a period boundary or reaches appl_ptr (the end of a drain, an underrun), and works from its own position between reports.
2026-11-22 22:13:39 +00:00
Pooya Parsa
2abdc598e2 guest, kernel: boot faster and carry less in the base: /init looks for the data disk and the distro image on kwblk's disks only (wd*) and the kernel has no RAM disks (CONFIG_BLK_DEV_RAM off: probing brd's 16 took about 130 ms a boot); /init and rc read files with the shell instead of head; DHCP runs in the background (net background, net wait: the console's shell starts before the lease, kwtty serve and Debian's services with start links wait for it); the bundle stores the initrd uncompressed; wlandr moves from the base to the Debian image (wland says so where there is none); the checkout's CLI turns on Node's compile cache before it loads the SDK 2026-11-22 14:46:21 +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
9f90e86085 kernel: backport virtio-gpu's damage clips for rendered resources; guest: X draws its own pointer on the DRM device (SWcursor in kw-kms.conf), and the Debian README names kmscube and X with glamor 2026-11-21 23:29:44 +00:00
Pooya Parsa
31bff45e60 kernel: place the virtio-gpu capset wait with the generic patches 2026-11-21 22:58:24 +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
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
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
2ce4c972ef guest: build the Alpine image from stock packages, without the kwemu rebuild 2026-11-19 17:07:49 +00:00
Pooya Parsa
6065c4397c kernel: allow 16 CPUs on the SMP kernel
kw_smp.config sets NR_CPUS to 16 (the wasm SMP patch). A boot on fewer CPUs costs only a few KiB of static per-CPU arrays: the possible CPUs are the ones the host offers. kwtest's spread check starts two threads per CPU up to 32, so it covers more than 8 CPUs.
2026-11-19 16:36:29 +00:00
Pooya Parsa
b7d1c6d8bf cli: rename --smp to --cpus 2026-11-19 16:04:15 +00:00
Pooya Parsa
97f8d9e5f1 guest: distroless base initramfs and separate Alpine and Debian erofs images
The bundle (unkernel.wasm) carries a small busybox initramfs that mounts a
distro image labelled kwroot under an overlay root, or starts the base shell
without one. guest/ replaces kernel/guest/: base/, distro/NAME/, shared lib/
and tools/. Images are laid out for their boot by a patched mkfs.erofs and a
recorded boot file list, with a prefetch hint in the image header. Debian
bakes its apt lists and cache, a set of everyday packages and a
command-not-found installer.
2026-11-19 14:46:13 +00:00
Pooya Parsa
21ef47685a kernel: enable erofs with zstd for the distro images 2026-11-19 14:46:13 +00:00
Pooya Parsa
07024216b7 chore: update docs 2026-11-19 07:58:25 +00:00
Pooya Parsa
bb8756ec67 update docs 2026-11-19 07:30:14 +00:00
Pooya Parsa
b5b75b53de Run WASI programs from the guest on the host's wasm engine (prototype)
binfmt_misc hands a wasm module to kwwasi (kernel/guest/kwwasi.c), which
connects to the host over AF_VSOCK port 1021 and serves the program's
file I/O with its own descriptors, cwd and credentials. The host
(js/core/wasi-exec.ts, serveWasiPrograms(), the CLI's --wasi) compiles
the module once and runs each program in a worker of its own
(js/core/wasi-program.ts) with WASI preview 1, or preview 0 adapted.
Requests cross through a SharedArrayBuffer where one can be used, else
with JSPI and messages. kwwasi sends a module only when the host has not
compiled it for that file, resolves paths against the cwd or / by what
exists, and maps open flags from their asm-generic values to the
guest's.

alpine.sh builds kwwasi and kw-binfmt.sh registers it; no image with
them has been built yet, so test/wasi-binfmt.ts brings kwwasi in through
a mount.
2026-11-19 07:08:32 +00:00
Pooya Parsa
ff43220c6b docs: describe the aarch64 system and its full test matrix 2026-11-19 00:12:52 +00:00
Pooya Parsa
5190bf4a86 kwtest: pin the threads of lse smp and excl smp to their own CPUs
Both checks last tens of milliseconds. When fork placement put both
threads on one CPU (the other one busy for a moment), they could end
before the balancer's first pull, one or two ticks at HZ=100, and the
check failed on its CPU mask: 3 of 35 runs with --smp 2. Each thread
now pins itself to CPU id % online (the main thread restores its mask
afterwards); move: still checks the spreading. 40 runs on 2 and 4 CPUs
pass.
2026-11-19 00:12:48 +00:00
Pooya Parsa
20f9de7add kernel: build abitest into the test archive, and complete the build path's checks
alpine.sh builds kernel/guest/abitest.c (freestanding) beside kwtest and
kwbench, and the release workflow runs it on the UP kernel and on 2 CPUs.
Its SA_RESTART check waits for the parent's alarm with a sleep instead of
a busy loop, which the JIT finished before the alarm.

setup.sh checks xz and sha512sum (the package rebuild's toolchain and
source sums) and reports qemu-aarch64 (optional, zig build test's
comparison). build.sh's disk check also covers alpine-opt.sh, kw-path.sh
and the bwrap shim, which alpine-disk.sh now installs like the initramfs.
2026-11-19 00:12:48 +00:00