Document the shared-memory kernel build and its cost, export its kernel patch

This commit is contained in:
Pooya Parsa
2026-11-16 13:23:51 +00:00
parent fccde8ff93
commit 2c3f3c10aa
3 changed files with 64 additions and 1 deletions

View File

@@ -139,7 +139,14 @@ patch re-export.
- uaccess (`copy_{to,from}_user`, `clear_user`, futex ops) walks the user
page tables in software and faults pages in. `get_user`/`put_user` and
`strn*_user` are the generic ones on top of that.
- Kernel memory is not shared (no SharedArrayBuffer). Guest RAM is below
- Kernel memory is not shared (no SharedArrayBuffer) by default. The
arch Makefile's experimental `KW_ATOMICS=1` (wasm atomics, so the
`__atomic_*` builtins become atomic instructions) and
`KW_SHARED_MEMORY=1` (also `--shared-memory`: passive data segments,
initialised by the start function `__wasm_init_memory`) make variables
build a UP kernel that boots on a shared memory; kwemu must then be
built with `zig build -Dkwemu-atomics=true`, and the host detects the
shared import (status.md, SMP). Guest RAM is below
4 GiB, which makes the lowering exact.
## Tasks: JSPI on one thread

View File

@@ -79,6 +79,22 @@ qemu's TCG keeps writing new code, which kwemu interprets or recompiles.
a syscall stay pinned. Parking's user resume (`kw_park_resume()`,
kernel.md, Parking) is that entry; parking itself assumes UP in three
places listed there.
Step 1, toolchain (2026-11-16, on the `smp` branches): the UP kernel
builds with atomics and links with `--shared-memory` (kernel.md,
Memory model), survives Binaryen's lowering, and passes kwtest (JIT
and `--no-jit`, lowered and wasm64) and a snapshot round trip on a
shared `WebAssembly.Memory` in Node. The JIT declares its memory
import shared (caps bit 3, kwemu.md). Cost on one CPU (Node, kwbench
medians of 15 alternating runs): page faults +21% (458 to 562 ns) and
kwbench's fork of an 8 MiB process +31%, both from the kernel's
atomic RMW instructions (kwemu built with atomics alone costs
nothing, and making `atomic_read`/`atomic_set` plain did not help);
sharing the memory adds nothing measurable beyond that. Syscalls,
pipes, futex wake and user code are within noise. The lowered kernel
is 41 KB smaller (9 KB gzip). Open: `__wasm_init_memory_flag` sits
after `_end`, in memory the kernel hands out, so a second instance
(a secondary CPU) would read a stale flag; browsers were not tried
(block, display and sound views of a shared memory are unchecked).
9. **Listening-port detection** for the preview chip and CLI
auto-forward: a `kwtty ports` subcommand in C that re-reads
`/proc/net/tcp` every 3 s (a stdin byte rescans at once) and prints

View File

@@ -0,0 +1,40 @@
From 912b7e36832242929338acd30080b6425d8ce718 Mon Sep 17 00:00:00 2001
From: kernel-wasm <kernel-wasm@localhost>
Date: Mon, 16 Nov 2026 13:12:25 +0000
Subject: [PATCH] wasm: optional atomics and shared-memory builds (KW_ATOMICS,
KW_SHARED_MEMORY)
---
arch/wasm/Makefile | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/arch/wasm/Makefile b/arch/wasm/Makefile
index 4492361d97d..34e941e06b6 100644
--- a/arch/wasm/Makefile
+++ b/arch/wasm/Makefile
@@ -10,6 +10,22 @@ KCFLAGS += -march=wasm64
KAFLAGS += -march=wasm64
KBUILD_LDFLAGS += -mwasm64
+# Experimental (SMP groundwork; the kernel stays UP): KW_ATOMICS=1 builds
+# with the wasm atomics feature, so the __atomic_* builtins behind
+# atomic.h, cmpxchg.h and barrier.h become real atomic instructions.
+# KW_SHARED_MEMORY=1 (implies KW_ATOMICS=1) also links vmlinux against a
+# shared imported memory: every data segment becomes passive and
+# __wasm_init_memory (the start function) initialises them once. kwemu
+# (KWEMU=) must be built with atomics too (zig build -Dkwemu-atomics=true).
+ifeq ($(KW_SHARED_MEMORY),1)
+KW_ATOMICS := 1
+LDFLAGS_vmlinux += --shared-memory
+endif
+ifeq ($(KW_ATOMICS),1)
+KCFLAGS += -matomics -mbulk-memory
+KAFLAGS += -matomics -mbulk-memory
+endif
+
core-y += arch/wasm/kernel/
core-y += arch/wasm/mm/
libs-y += arch/wasm/lib/
--
2.56.0