diff --git a/.agents/a64/isa.md b/.agents/a64/isa.md index 55a0ae6..557caaa 100644 --- a/.agents/a64/isa.md +++ b/.agents/a64/isa.md @@ -288,6 +288,6 @@ of busybox, musl, apk, zlib, libssl and libcrypto, 209286): are scalar SCVTF/UCVTF/FCVTZS/FCVTZU on D registers (busybox `sleep`) and `fneg v.2d` (musl's libm), implemented in `simd_fp.zig`. -The image's busybox, zlib, libcrypto, libssl and apk are rebuilt -(guest-packages.md): no vectorized or FP-spilling code in hot paths; -libcrypto picks its AES, PMULL, SHA and NEON code from HWCAP. +The image's busybox, zlib, libcrypto, libssl and apk are Alpine's builds +(`-Os`, almost free of vectorized code); libcrypto picks its AES, PMULL, +SHA and NEON code from HWCAP. diff --git a/.agents/a64/performance.md b/.agents/a64/performance.md index 87c6b38..686bc18 100644 --- a/.agents/a64/performance.md +++ b/.agents/a64/performance.md @@ -86,8 +86,9 @@ runs): `npm --version` 3.91 and 3.54, `npx --version` 3.53 and 3.55, `npx `--cpus 2`: `npm --version` 3.47 and 2.99. Compiled share: npm 44%, `node -e 0` 63%, V8 loops 80-92%. -**OpenSSL** (2026-11-18, UP bundle, the image's rebuilt libcrypto, `openssl -speed -evp ALG -bytes 16384 -seconds 2`, MB/s, medians of 3 boots; +**OpenSSL** (2026-11-18, UP bundle, libcrypto built with gcc `-O2`; the +image now has Alpine's `-Os` build, whose NEON and crypto paths are the +same assembly; `openssl speed -evp ALG -bytes 16384 -seconds 2`, MB/s, medians of 3 boots; "integer" is `OPENSSL_armcap=0`, "NEON + crypto" what HWCAP selects on a SIMD host, "no SIMD" the same code as interpreter calls): diff --git a/.agents/data-disk.md b/.agents/data-disk.md index a4c1aeb..5083a74 100644 --- a/.agents/data-disk.md +++ b/.agents/data-disk.md @@ -63,7 +63,7 @@ missing). `.apk.` temporaries and up to 17 MiB of unpacked but unregistered files; other apk commands worked meanwhile, and running the same `apk add` again installed the packages, removed every temporary, and left - `apk audit` clean (apart from the rebuilt packages, guest-packages.md). + `apk audit` clean. What stays until then is unowned files, harmless to apk, which overwrites unowned files. - At poweroff the distro's init runs `/usr/lib/kw/shutdown`: with `/data` diff --git a/.agents/guest-packages.md b/.agents/guest-packages.md deleted file mode 100644 index dbdb3b7..0000000 --- a/.agents/guest-packages.md +++ /dev/null @@ -1,408 +0,0 @@ -# Rebuilt guest packages - -The Alpine image ships the minirootfs packages as Alpine builds them, -except four that sit on the guest's hot paths, which -`guest/distro/alpine/opt/build.sh` (below: the script; `guest/build.sh opt -alpine [PKG...]` runs it) rebuilds for kwemu's CPU: - -| aports package | files replaced in the image | -| --- | --- | -| busybox | `/bin/busybox` | -| zlib | `/usr/lib/libz.so.1.3.2` | -| openssl | `/usr/lib/libcrypto.so.3`, `/usr/lib/libssl.so.3` | -| apk-tools | `/usr/lib/libapk.so.3.0.0`, `/sbin/apk` | - -musl stays Alpine's, and so does everything else, including ssl_client and -scanelf. - -What the rebuild avoids: kwemu's JIT compiles integer, load/store and -branch instructions best; FP and AdvSIMD code is translated only in part -(scalar FP with a fast path, and where the host has wasm SIMD the integer -AdvSIMD and crypto instructions; a64/jit.md), and the rest are calls of -the interpreter (`generic` ops). Alpine's `-Os` code is almost free of -them, but gcc's `-O2` is not (the vectorizer, FP registers as spill -space, scalar AdvSIMD idioms such as `uqsub`). libcrypto keeps -upstream's choice of its NEON and crypto-extension code from AT_HWCAP: -with the JIT's AdvSIMD and crypto translation those paths are the fast -ones (Measurements). - -## How it is built - -- **Same input as Alpine.** For each package the script pins the aports - commit recorded in the image's apk database (the `c:` field of - `/lib/apk/db/installed`) and the APKBUILD's SHA-256. It checks every - source and patch against the APKBUILD's sha512sums, and applies the - patches in the APKBUILD's order. aports files come from its GitHub - mirror at the same commit. Upstream sources fall back to Alpine's - distfiles mirror, as abuild does. Configuration as the APKBUILD's: - - busybox: `busyboxconfig`, with utmps linked statically; - - openssl: Configure options, with threads on as in the native build; - - apk-tools: abuild-meson's options and the APKBUILD's, without the lua - and python modules and the man pages, which the image does not have. -- **The compiler.** gcc 16.2 from Bootlin's aarch64 musl toolchain - (`vendor/toolchain-gcc/`). It builds against a sysroot of Alpine's own - aarch64 packages (`musl-dev`, `linux-headers`, the utmps and skalibs - static libs, and the musl runtime). musl-dev is the same version as the - image's musl, so the binaries link to the image's - `libc.musl-aarch64.so.1`. -- **The flags.** Alpine uses gcc 15 at `-Os`; busybox's own - Makefile.flags adds `-Oz`, `-finline-limit=0` and - `-fno-guess-branch-probability`. The rebuild uses - `-O2 -march=armv8-a -fno-tree-vectorize` and drops those busybox lines. - Every C file that allows it is compiled with `-mgeneral-regs-only` too: - the `cc` wrapper tries that first and compiles again without it when it - fails, which is what a file with float types does (awk, printf, seq, - sleep and the like). Of busybox's 518 objects 29 use FP or AdvSIMD - registers at all, of libcrypto's 782 C objects 12, of apk-tools' 69 six, - of zlib's 19 one. - gcc's outline atomics stay (`-moutline-atomics`, the default: on musl - they always take the exclusive path, a64/isa.md, LSE atomics). -- **Variadic functions keep their FP registers.** A variadic function - compiled with `-mgeneral-regs-only` does not save the FP argument - registers in its prologue, so a `double` passed to it reads as garbage - (busybox `awk 'BEGIN{printf "%.2f", 1.25}'` printed `0.00` through - libbb's formatting helpers). The wrapper force-includes `kw-gro.h` - on the first attempt, whose `va_start` uses an FP register operand; a - file that defines a variadic function then fails that attempt and is - built without `-mgeneral-regs-only` (busybox's `ash.c` among them). -- **libcrypto picks its code from AT_HWCAP**, unpatched: AES, PMULL - (GHASH), SHA-1, SHA-256, SHA-512 and NEON (ChaCha20, Poly1305) as - kwemu advertises them (`OPENSSL_armcap=0x8fd`; `OPENSSL_armcap` in the - environment still overrides). Until 2026-11-18 a patch here returned no - capabilities, because those paths were interpreter calls and slower - than the integer code; with the JIT's translation (a64/jit.md, AdvSIMD and - crypto) they are 1.2-8x faster (Measurements). A host - without wasm SIMD (or `--no-jit`) runs them as interpreter calls again, - which is slower for most of them (Measurements). -- **The linker.** lld (zig's `ld.lld`, through `-fuse-ld=lld -B`), not - GNU ld, with `-z pack-relative-relocs` (RELR; musl supports it) and - `--undefined-version`. busybox is not PIE. apk is PIE (gcc's default - and abuild-meson's `b_pie`, as in Alpine). -- **Stripping** as abuild does: libraries `--strip-unneeded`, programs - fully. -- **Reproducible.** `SOURCE_DATE_EPOCH` is fixed, so busybox's and - openssl's embedded build times do not change, and openssl's recorded - compiler command (`openssl version -a`) names the wrapper `cc-openssl`, - not a host path. Two clean builds gave byte-identical image files - (2026-11-18), so a rebuild keeps the image id the overlay root keys on. -- **Into the image.** `guest/distro/alpine/build.sh` (the distro image) - calls the script with `--install "$stage"` after unpacking the - minirootfs. That builds first - when `vendor/build/alpine-opt/.stamp` (`KW_ALPINE_OPT_DIR` moves the - cache) is stale: it records the hash of the script without its comment - lines, and `ALPINE_VERSION` (`guest/distro/alpine/pins.sh`). A full build takes about 80 s - on 16 cores (2026-11-18). Building needs an x86_64 host (Bootlin's gcc); - elsewhere, or to keep Alpine's builds, set `KW_ALPINE_OPT=0`. A stage - from an `ALPINE_TARBALL` override with other package versions keeps - Alpine's builds, with a warning. -- **Size.** The image grows by about 570 KiB (busybox 995 KiB against - 919 KiB, libcrypto 4.9 MiB against 4.4 MiB). -- The script without arguments rebuilds everything; with package names it - rebuilds only those. -- **Host tools:** meson, ninja, pkg-config, patch, lua 5.x (apk's help - text), and perl with FindBin, IPC::Cmd and File::Compare (openssl's - Configure). busybox's host tools build with `zig cc`. `setup.sh - --check` checks them. -- **CI.** The release workflow installs those tools with apt in its host - tools step. It caches what `--install` reads on a hit (`root/`, - `.stamp`, the APKBUILDs; about 9 MB), under the key - `alpine-opt-`, exact match only. On a - miss the rebuild adds a few minutes (riscv64's took 233 s on a 4-core - runner, 2026-11-16; the aarch64 one has not run in CI yet, and took - 79 s on 16 cores locally, 2026-11-18). -- **Keep the other caches.** The kernel cache key hashes - `kernel/build.sh`, and a kernel cache miss costs about an hour. So the rebuild is not a `build.sh` step. Before - "Save downloads", the workflow deletes the rebuild's downloads except - the APKBUILDs, so they stay out of that cache. -- **The apk database is unchanged.** It keeps Alpine's file checksums, - so `apk audit` lists the six files. Upgrading one of these packages - installs Alpine's build again, which is correct but slower (2026-11-18, - on a data disk: `apk upgrade` replaced the rebuilt zlib 1.3.2-r1 with - Alpine's 1.4-r0 in the overlay's upper layer, and `apk audit` then - listed the other five). - -## Alpine updates - -`--install` compares the stage's apk database with the versions it built -(`pkgver-rpkgrel` from the pinned APKBUILDs). On a mismatch it stops with -"update guest/distro/alpine/opt/build.sh". After bumping `ALPINE_VERSION`: - -1. Take each package's new `c:` commit from the new image's apk database. -2. Update `commit`, `apkbuild_sha256` and the sysroot apks (versions and - SHA-256) in the script. -3. Check that the APKBUILD's configure options still match the script's. - -## Measurements - -All 2026-11-18, i7-10700K, Node CLI, UP kernel with the JIT. The guest -timed itself with `/proc/uptime` (10 ms steps). Boots interleaved the -variants, rotating their order; medians. - -**The image** (5 boots per image, interleaved; guest seconds, timed by -`/proc/uptime`; each command once per boot, in this order, network on, -apk's cache off; a busybox testsuite boot ran alongside). riscv64 is main's -image (its own rebuilt packages, its JIT), for reference. - -| command | riscv64 | aarch64, Alpine's builds | **aarch64, rebuilt** | -| --- | ---: | ---: | ---: | -| `apk update` | 1.17 | 1.38 | **1.33** | -| `apk search -q busybox` | 0.77 | 0.86 | **0.86** | -| `apk search` | 0.77 | 0.83 | **0.87** | -| `apk search -d compiler` | 0.82 | 0.95 | **1.00** | -| `apk info -v` | 0.63 | 0.71 | **0.72** | -| `apk add --simulate git` | 0.65 | 0.73 | **0.72** | -| `apk add jq` | 1.30 | 1.50 | **1.39** (-7%) | -| `apk del jq` | 0.67 | 0.77 | **0.78** | -| `apk add git` | 2.24 | 2.80 | **2.46** (-12%) | -| `apk add python3` | 3.73 | 4.84 | **4.32** (-11%) | -| python3 loop | 0.79 | 0.76 | **0.74** | -| ash loop, 30000 iterations | 1.86 | 2.53 | **2.24** (-11%) | -| `seq 100000 \| sort -r` | 2.78 | 2.98 | **3.05** | -| `seq 250000 \| gzip \| gunzip` | 2.20 | 3.70 | **2.55** (-31%) | -| `sha256sum`, 8 MB | 0.51 | 0.54 | **0.54** | -| `find / -xdev` | 0.10 | 0.10 | **0.09** | - -The apk commands that only parse the index stay within the noise -(their libraries are no faster, below); downloads (TLS) and package -extraction (zlib, SHA-256) are where the installs gain. `sort` and -`gzip` here include busybox `seq`, whose `double` arithmetic is -generic ops in every build. - -With libcrypto on its HWCAP paths and the JIT translating them -(2026-11-18, UP bundle, medians of 5 boots interleaved against the same -JIT with the image's libcrypto still patched to its integer paths and -SHA512 off): `apk add python3` 4.08 to 3.36 s, `apk add git` 2.34 to -2.10, `apk update` 1.26 to 1.23, the other rows within noise; sum of the -20 rows of `test/guest-bench.sh` 23.71 to 22.55 s. Package -installs verify SHA-256 and download over TLS (AES-GCM). - -**OpenSSL in the image** (`openssl speed -evp ALG -bytes 16384 --seconds 2`, MB/s; public key in operations/s; the rebuild's `openssl` -command against the image's libcrypto). riscv64 is main's rebuilt -libcrypto with its Zkn/Zbkb code; "integer paths" is the rebuilt -libcrypto while a patch hid AT_HWCAP from it (2 boots, medians), "HWCAP -paths" the image now (2026-11-18, the JIT with AdvSIMD and crypto -translation and SHA512 on, `OPENSSL_armcap=0x8fd`; medians of 3 boots, -a64/jit.md, AdvSIMD and crypto). - -| | riscv64 | aarch64, Alpine's (before the translation) | aarch64, integer paths | **aarch64, HWCAP paths** | -| --- | ---: | ---: | ---: | ---: | -| sha256 | 46 | 26 | 59 | **157** | -| sha512 | 137 | 117 | 125 | **217** | -| sha1 | 118 | 33 | 151 | **324** | -| aes-128-gcm | 53 | 14 | 24 | **99** | -| aes-256-gcm | 48 | 11 | 20 | **80** | -| aes-256-ctr | 52 | 14 | 25 | **137** | -| aes-128-cbc | 92 | 21 | 43 | **190** | -| chacha20-poly1305 | 71 | 5 | 162 | **203** | -| rsa2048 sign / verify | 49 / 2380 | 83 / 3070 | 55-79 / 3060 | **81 / 3060** (one boot; 82 / 3120 with `OPENSSL_armcap=0`) | -| ecdsa p256 sign / verify | 39 / 51 | 1950 / 800 | 2030 / 715 | 2030 / 715 (no NEON code; not rerun) | -| x25519 | 2410 | 2600 | 3130 | 3130 (no NEON code; not rerun) | - -Without wasm SIMD in the host (the JIT calls the interpreter for AdvSIMD -and crypto) the HWCAP paths are the slow ones (2026-11-18, one boot, -`-seconds 1`): aes-128-gcm 14 against 24 MB/s on the integer path, -sha256 24 against 57, sha512 10 against 122, chacha20-poly1305 5 -against 159. With `--no-jit` (interpreter only) they are faster for AES -(13 against 3) and SHA-256 (24 against 7), even for SHA-512 (10 against -11) and slower for ChaCha20-Poly1305 (5 against 12). Every engine with -JSPI has SIMD; the trade is made for it. - -**Correctness** (2026-11-18): digests (md5, sha1, sha224 to sha512, -sha512-256, sha3-256, shake128, blake2b512) and `openssl enc` (AES CBC, -CTR, CFB, OFB, ChaCha20) of a 3 MB file under qemu-aarch64, with the -default and with `OPENSSL_armcap` set, equal the host's openssl, and -decryption round-trips; TLS 1.3 (AES-128-GCM, AES-256-GCM, -ChaCha20-Poly1305) and TLS 1.2 (ECDHE-ECDSA with AES-GCM, ChaCha20, -AES-CBC) against a host `openssl s_server` carried 270 KB each way -intact. With the HWCAP paths compiled by the JIT (2026-11-18, the image's -unpatched libcrypto, SHA512 on, UP bundle): in the guest, the digests -(md5, sha1, sha224, sha256, sha384, sha512, sha512-256, sha3-256, -shake128), `openssl enc` (AES-128/256-CBC, AES-128/192/256-CTR, ECB, CFB, -OFB, ChaCha20, each also decrypted again: 135 round trips), GMAC -(AES-128-GCM, AES-256-GCM) and HMAC-SHA256 of 15 lengths (1 byte to 2.9 -MB, around the 16, 64 and 128-byte block sizes) equal the host's openssl -line for line, by default and with `OPENSSL_armcap=0`; TLS 1.3 -(AES-128-GCM, AES-256-GCM, ChaCha20-Poly1305) and TLS 1.2 (ECDHE-RSA with -AES-128-GCM, AES-256-CBC, ChaCha20-Poly1305) carried 2.9 MB intact in -both directions (the guest as client of a host `s_server -WWW`, and as -server for a host `s_client`). busybox's testsuite in the guest: the -same 13 failures as -Alpine's busybox (wget without a network, `hostname -d`, timezone data, -three sed cases, cpio hardlinks, unzip, strings, an awk gsub case), all -other tests pass with both. - -**busybox** (ms; 4 runs of 2 boots × 3 rounds each, every run with -stock, `-O2` and some of the others: 24 samples for stock and `-O2`, 18 -for the choice, 12 for "no vectorizer", 6 for the rest; each variant's -busybox copied into `/tmp` and run directly). "GRO" is -`-mgeneral-regs-only` per file, "guarded" with the `va_start` check -(above); the unguarded build is wrong (awk's `printf "%.2f"`) and only -shows what the guard costs. - -| command | stock | gcc 16 -Os | -O2 | -O2, no vectorizer | **-O2, no vectorizer, guarded GRO** | -O2, guarded GRO | -O2, no vectorizer, GRO unguarded | -| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | -| ash loop, 20000 iterations | 1760 | 1735 | 1380 | 1500 | **1545** (-12%) | 1490 | 1370 | -| 150 × fork + exec | 240 | 220 | 230 | 230 | **230** (-4%) | 240 | 220 | -| `awk` sqrt loop, 150000 | 2360 | 2115 | 1910 | 2050 | **2070** (-12%) | 2080 | 2015 | -| `seq 60000 \| awk` | 1780 | 1625 | 1565 | 1645 | **1650** (-7%) | 1655 | 1605 | -| `sort -rn`, 30000 lines | 2805 | 2745 | 2740 | 2815 | **2875** (+2%) | 2715 | 2670 | -| `sha256sum`, 4 MB | 100 | 100 | 2350 | 100 | **100** | 100 | 95 | -| `tar \| gzip -6 \| gunzip \| md5sum`, 1.3 MB | 1570 | 1485 | 895 | 1000 | **785** (-50%) | 890 | 750 | -| `sed \| grep -c`, 30000 lines | 940 | 1005 | 900 | 910 | **915** (-3%) | 915 | 890 | - -Plain `-O2` is the fastest build for ash and awk and was kept out only -by its vectorized code: `sha256sum` 23 times slower than stock, and -`sha512sum` 14% generic ops in qemu. Its ash advantage is not in the -instructions: an ash loop runs the same count (qemu: 40.6M against 40.7M) -and the same generic share (0.29%) as the chosen build, but on a -`-Dkwemu-stats` kernel 90.1% of its retired instructions ran compiled -against 85.5%, with fewer hand-backs in the chosen build (191K against -233K); the cause, maybe how the code falls on pages (a decoded block -ends at a page boundary), is not known. - -**Instruction mix** (qemu-aarch64 `-d in_asm,exec,nochain` traces, -words classified by `kwemu-a64-classify`): `-O2` busybox `sha256sum` -ran 43.7% generic ops (`fmov` 25%: general registers spilled to FP -registers; vectorized message schedule: `shl`, `usra`, `eor`, `add` on -`.2s`); without the vectorizer, 0%. Stock `gzip` runs 0.59% generic -(`uqsub h`, scalar saturating subtract in `fill_window`), the `-O2` -build without the vectorizer the same, with `-mgeneral-regs-only` 0%. -The total instruction counts drop 2-16% from stock to the rebuild (ash -45.5M to 40.7M, sort 194M to 178M). - -**Libraries** (3 boots × 2 rounds, ms or MB/s). Each variant is a -directory with libz, libcrypto, libssl, libapk and apk, used through -`LD_LIBRARY_PATH` and its own apk binary over the stock image; apk's -package cache on, so `apk add` reads no network. `stock, armcap=0` is -Alpine's libcrypto with `OPENSSL_armcap=0`. - -| command | stock | stock, armcap=0 | **-O2** | -O2 -mno-outline-atomics | -Os | -| --- | ---: | ---: | ---: | ---: | ---: | -| 10 × `apk --version` | 510 | 505 | **450** (-12%) | 480 | 525 | -| `apk search` (main) | 810 | 825 | **805** | 805 | 815 | -| `apk search -d compiler` | 915 | 950 | **930** | 940 | 955 | -| `apk info -v` | 660 | 695 | **685** | 690 | 730 | -| `apk add --simulate git` | 720 | 750 | **710** | 725 | 780 | -| `apk add jq` (cached) | 820 | 870 | **810** | 810 | 845 | -| `apk del jq` | 765 | 760 | **720** (-6%) | 720 | 775 | -| python3 `zlib`: 4 MB compress, 4 × decompress | 1070 | 1080 | **770** (-28%) | 770 | 870 | -| `wget https://` APKINDEX (TLS) | 650 | 620 | **575** (-12%) | 610 | 620 | -| `openssl speed -evp sha256` | 25 | 59 | **58** | 63 | 63 | -| aes-128-gcm | 14 | 20 | **24** | 23 | 20 | -| chacha20-poly1305 | 5 | 164 | **167** | 146 | 164 | -| sha512 | 114 | 113 | **122** | 99 | 115 | - -The apk commands that parse the index are flat: they run no generic ops -to speak of on either build (qemu: 0.41% stock, 0.05% rebuilt, the -SHA-1 crypto instructions of the index signature check) and execute -about the same number of instructions (232M against 234M for `apk search --q busybox`). Their time is the JIT's own (below). - -**OpenSSL per capability** (2026-11-18, `openssl speed -evp ALG -bytes -16384 -seconds 1`, MB/s, the image's libcrypto, one run each, -`OPENSSL_armcap` set in the environment, the JIT with AdvSIMD and crypto -translation, SHA512 on; 0x8fd is what kwemu's HWCAP gives it now): - -| algorithm | **0x8fd** (NEON + AES, PMULL, SHA1, SHA2, SHA512, SHA3) | 0x1 (NEON) | 0x25 (NEON, AES, PMULL) | 0x3d (0x25 + SHA1, SHA2) | 0x10 (SHA2) | 0 | -| --- | ---: | ---: | ---: | ---: | ---: | ---: | -| sha256 | **163** | 88 | 89 | 153 | 163 | 61 | -| sha1 | **350** | 156 | 155 | 354 | 147 | 158 | -| sha512 | **215** | 107 | 119 | 117 | 117 | 118 | -| aes-128-gcm | **90** | 42 | 94 | 94 | 24 | 23 | -| aes-256-ctr | **138** | 159 | 137 | 136 | 24 | 25 | -| aes-128-cbc | **194** | 126 | 194 | 184 | 44 | 44 | -| chacha20-poly1305 | **186** | 199 | 207 | 205 | 163 | 167 | -| md5 | **331** | 343 | 341 | 338 | 330 | 335 | - -Every algorithm is fastest, or within one run's noise of it, with the -full set; only AES-CTR is faster on NEON alone (bitsliced AES, 159 -against 138), which the AES bit cannot be taken away for without losing -GCM (42 against 90) and CBC. Before the translation (the same table with -Alpine's libcrypto, 2026-11-18) every capability bit made OpenSSL slower -(full set: sha256 25, aes-128-gcm 14, chacha20-poly1305 4.4 MB/s against -57, 20 and 167 with none), because each AdvSIMD and crypto instruction -was an interpreter call. - -## What did not help, or was worse - -- **`-O2` with the vectorizer** (gcc's default since gcc 12): busybox - `sha256sum` 23 times slower than stock, `sha512sum` 14% generic ops - (above). Fastest for ash and awk otherwise. -- **`-O2 -fno-tree-loop-vectorize`** (the SLP vectorizer left on), no - GRO: `sha256sum` still 27% generic ops (`fmov` spills 21%, SLP `add`, - `eor`, `and` on V registers), `gzip` 0.6% (`uqsub`). -- **`-O2` with guarded GRO**, vectorizer on (it can only act in the files - that keep the FP registers): ash 1490 against 1545 ms, gzip 890 - against 785; no clear winner, and the vectorizer stays a risk in those - files. -- **`-Os` (gcc 16).** busybox: ash as stock, gzip -5% against -50%; - libraries: zlib -19% against -28%, apk startup +3%. -- **`-mno-outline-atomics`.** Inline LL/SC instead of the helper calls: - no gain (apk startup 480 against 450 ms, the rest flat). Exclusives are - generic ops either way. -- **Hiding capabilities from OpenSSL** (a patch that left - `OPENSSL_armcap` empty, until 2026-11-18): right while AdvSIMD and - crypto instructions were interpreter calls, 1.2-8x slower than - upstream's choice since the JIT translates them (per-capability table - above). Masking single bits gains nothing either. -- **GNU ld** (stock Alpine binaries): 150 execs of busybox 230 ms - against 220 ms with lld, about 0.07 ms per exec on aarch64 (riscv64 - measured 0.5 ms, 2026-11-16). -- **musl** is not rebuilt. Its generic ops show up in the profiles - (status.md, Open issues): `dup`/`movi` in memset (0.2% of ash's - instructions) and `ldaxr`/`stlxr` in malloc's locks (0.06% in ash, 0.4% - in awk), each ending a compiled block. - -## Traps - -- **gitlab.alpinelinux.org from GitHub-hosted runners.** It answers raw - file requests with HTTP 418 (it works from other hosts), hence the - GitHub mirror of aports. -- **Bootlin's gcc and Alpine's sysroot.** Pass `--sysroot` explicitly; - Bootlin's wrapper then skips its own. -- **zlib's configure.** Under lld it decides "No shared library support" - unless `--undefined-version` is set (its version-script test). The - script fails if `libz.so.1` is missing. -- **busybox's host tools.** make exports `CC` (the cross compiler), and - zig cc finds the host's libc by running `$CC`, so the host-cc wrapper - unsets it. -- **`-mgeneral-regs-only` and variadic functions** (above): a whole-build - `-mgeneral-regs-only` does not compile busybox or libcrypto, and a - per-file one without the `va_start` guard compiles and silently - corrupts `double` varargs. -- **Rebuilding one openssl object.** Do it through make (a CC wrapper - that appends flags), not by hand: make otherwise recompiles the object - with its own flags. The shared library links `libcrypto-shlib-*.o`, - not `libcrypto-lib-*.o`. - -## Measuring a change - -Rebuild, then boot the bundle with -`--mount :/mnt/x:ro --input '{10}sh /mnt/x/bench.sh ...; poweroff\n'`. - -- **Binaries:** copy them into `/tmp` in the guest first, each busybox - as `/tmp//busybox` (busybox picks the applet from its file - name). Run them directly (`$B awk ...`) and time them with - `/proc/uptime`; busybox's `date` has no `%N`. -- **Libraries:** point `LD_LIBRARY_PATH` at a directory holding only the - library under test. The image has no `openssl` command; the rebuild's - (`root/usr/bin/openssl`) works against either libcrypto. -- **OpenSSL capabilities:** `OPENSSL_armcap=` in the environment - (`openssl info -cpusettings` prints the one in effect). -- **Instruction mix:** `qemu-aarch64 -L -d in_asm,exec,nochain - -D log `, count each translated block's words by its executions, - and classify them with `zig-out/bin/kwemu-a64-classify` (`generic` = - interpreted). -- **Where kwemu spends it:** a `-Dkwemu-stats` kernel and the CLI's - `--stats` (kwemu.md): compiled share, hand-backs per op kind, - interpreted ops per kind. -- **Correctness:** compare digests and `openssl enc` output with the - host's openssl (also with `OPENSSL_armcap` set, so the other paths stay - right), run TLS 1.2 and 1.3 suites against a host `openssl s_server`, - and run busybox's testsuite in the guest against stock busybox. -- **musl:** run its loader as a command - (`ld-musl-aarch64.so.1 --library-path DIR PROG`). -- **Noise:** interleave the variants within a boot, rotate their order - across boots, and compare medians. Boot-to-boot noise is several - percent (kwemu.md). diff --git a/.agents/kernel.md b/.agents/kernel.md index 2d7b252..0f76f10 100644 --- a/.agents/kernel.md +++ b/.agents/kernel.md @@ -435,9 +435,8 @@ the quick loop and never builds SMP. `KWEMU=` or `KWEMU_SMP=` count as built from the checkout's sources. - **Overrides**: `KSRC` (the tree), `KOUT`, `KOUT_SMP`, and `KW_BUILD` (the output root, default `vendor/build`: kernels, kwemu-smp and kwemu-stats, - initramfs, the data disk template, bundle, distro images; the toolchain's `vendor/build/lld22` and - the Alpine package rebuild's cache stay; `KW_BASE_OUT` and - `KW_DISTRO_OUT` move the base and the distro images, guest/README.md). + initramfs, the data disk template, bundle, distro images; the toolchain's + `vendor/build/lld22` stays; `KW_BASE_OUT` and `KW_DISTRO_OUT` move the base and the distro images, guest/README.md). The CLI, `pnpm build` and the tests read `vendor/build`, so pass them paths (`--kernel`, `--distro`, `UNKERNEL_TEST_KERNEL`, `UNKERNEL_TEST_DISTRO_DIR`). `KWEMU=path` takes a prebuilt diff --git a/.agents/kernel/guest.md b/.agents/kernel/guest.md index d56a108..657c276 100644 --- a/.agents/kernel/guest.md +++ b/.agents/kernel/guest.md @@ -5,9 +5,8 @@ What goes into the guest (`guest/`, layout and conventions in initramfs; the default of the SDK, the CLI and the npm package) boots a distro image, a read-only erofs or ext4 file system the host attaches, under an overlay root, or the base shell without one. Below: base -initramfs, kw layer, distro images, guest tools. The rebuilt Alpine -packages are in guest-packages.md, the data disk and overlay root in -data-disk.md. +initramfs, kw layer, distro images, guest tools. The data disk and +overlay root are in data-disk.md. ## Base initramfs (`kernel/build.sh base`, `guest/base/`) @@ -116,8 +115,8 @@ network). unofficial-builds.nodejs.org (the project's own builder, linux-arm64-musl). The musl build links `libgcc_s.so.1`, the glibc one `libatomic.so.1`; when one is missing the shim installs its package - first, as root, with apk (`libgcc`) or apt-get (`libatomic1`); both - images have them (ALPINE_PACKAGES, DEBIAN_PACKAGES). The + first, as root, with apk (`libgcc`) or apt-get (`libatomic1`); Debian's + image has libatomic1 (DEBIAN_PACKAGES), Alpine's has no libgcc. The `.tar.gz` (63 MB for 26.13.1, against 33 MB `.tar.xz`), unpacked with pigz on Alpine; about 230 MB unpacked. Node CLI, 1 GiB, no data disk, 2026-11-19, node 26.13.1: Alpine first call 18.3 s (libgcc and pigz @@ -142,8 +141,7 @@ network). C code (`OPENSSL_armcap=0`) made a 50 MB TLS 1.3 download over the host loopback no faster (AES-128-GCM 10.5-13.1 s against 9.2-11.6 s, ChaCha20-Poly1305 9.4-9.9 against 9.6-10.7; the network path and the - pipeline dominate) and costs AES 4x in `openssl speed` - (guest-packages.md). + pipeline dominate) and costs AES 4x in `openssl speed`. - Earlier pages wrote shims into `~/.local/bin` (`# kw-shim N` on line 2), which the data disk keeps on Alpine. They still work, come first on PATH, and are replaced by the program they install; the kw layer's @@ -189,17 +187,15 @@ debugfs from the stream). Each image has label `kwroot`, layer: boot to a prompt 0.25 s (Alpine) and 0.32 s (Debian) from `createKernel()` (Node, 2026-11-19). -- **Alpine** (`distro/alpine/`): the minirootfs with the rebuilt packages - (`opt/build.sh`), busybox init with `etc/inittab` (the layer's rc, +- **Alpine** (`distro/alpine/`): the minirootfs as Alpine builds it + (stock busybox, zlib, openssl and apk-tools), busybox init with `etc/inittab` (the layer's rc, welcome and net, `/etc/kw-binfmt.sh` for qemu-user, a shell on hvc0, `kwtty serve`, the layer's shutdown), and what "In the Alpine image" below lists. OpenSSL's engines, `legacy.so` and `*.dist` stay (the image is paged in on demand, not RAM-resident). An `apk add` cut off - mid-install needs no cleanup (data-disk.md). `ALPINE_PACKAGES` in - `pins.sh` (libgcc) are installed at build time as Debian's (below): the - image's apk in a build VM adds the pinned `.apk` files the host fetched, - and the changes to the overlay's upper layer are copied back into the - tree. + mid-install needs no cleanup (data-disk.md). The build adds no + packages and boots no VM; what else a program needs comes from apk on + first use (the shim's libgcc, above). - **Debian** (`distro/debian/`): debian:trixie-slim (pinned in `pins.sh`), Debian's own builds in full. `/sbin/init` -> `/usr/local/sbin/kw-init`, a bash PID 1: the layer's rc, welcome and diff --git a/.agents/status.md b/.agents/status.md index c23440b..65be7d1 100644 --- a/.agents/status.md +++ b/.agents/status.md @@ -360,9 +360,7 @@ Open: - **aarch64: the release workflow has not run its build path yet.** New in CI are the arm64 patch series (a new kernel tree and build: every - kernel cache misses), the package rebuild with Bootlin's aarch64 gcc - (guest/distro/alpine/opt/build.sh downloads 163 MB; 79 s on 16 cores locally, 2026-11-18), - the aarch64 minirootfs, the guest programs from `zig cc`, and abitest. + kernel cache misses), the aarch64 minirootfs, the guest programs from `zig cc`, and abitest. Before the first aarch64 release it needs one smoke run (`release` unchecked), which shows its time on a hosted runner and whether kwtest's timing checks hold there with the A64 JIT. @@ -384,7 +382,7 @@ Open: host.** The image's libcrypto now picks its code from HWCAP and the kernel enables SHA512 by default, because the JIT translates those paths (aes-128-gcm 99 MB/s, sha256 157, sha512 217 against 24, 59, 131 - on the integer paths; 2026-11-18, guest-packages.md). A host whose + on the integer paths; 2026-11-18, a64/performance.md). A host whose engine has no SIMD (V8 on an x86-64 without SSE4.1) runs them as interpreter calls, 2-13x slower than the integer paths would be; such a host could boot with `kw.isa=-sha512` or set `OPENSSL_armcap=0`. diff --git a/.agents/testing.md b/.agents/testing.md index ea6607a..2b0ec48 100644 --- a/.agents/testing.md +++ b/.agents/testing.md @@ -151,9 +151,8 @@ are `kernel/build.sh all`'s, split so that each cache is saved after its part. Re-running a release skips what exists (the tag, the npm version) and uploads only the assets an existing GitHub release lacks, publishing it if a cut-off run left it a draft. Caches: the -toolchain, the kernel tree and builds, the downloads, and the rebuilt -Alpine packages (keyed on `guest/distro/alpine/opt/build.sh` and the -Alpine pins); the workflow's header lists their keys. +toolchain, the kernel tree and builds, and the downloads; the workflow's +header lists their keys. Not in the workflow, so only the full matrix covers them: kwtest without the JIT on 2 CPUs, the api tests with `UNKERNEL_TEST_CPUS=2`, `zig build diff --git a/.agents/toolchain.md b/.agents/toolchain.md index b33a5e7..d640f56 100644 --- a/.agents/toolchain.md +++ b/.agents/toolchain.md @@ -70,11 +70,6 @@ The API differs from 0.13-0.15. Examples of all of these are in `timeconst.h` with `HZ == 0` ("Totally bogus HZ value!"). Put the real tools first on `PATH` for such runs. - Do not hard-code `/usr/bin` paths or home-directory fallbacks. -- `guest/distro/alpine/opt/build.sh` (run by the Alpine image's build; - `KW_ALPINE_OPT=0` skips it) also needs meson, ninja, pkg-config, patch, - lua 5.x, perl with FindBin, IPC::Cmd and File::Compare, xz (Bootlin's - tarball) and sha512sum (the APKBUILDs' source sums) (guest-packages.md), - and an x86_64 host for Bootlin's gcc. - `setup.sh` runs only ELF binaries for its version checks, so a wrapper that installs packages on first use is never triggered. @@ -88,7 +83,3 @@ data in tar order and the metadata zone in front, for the distro images' layout; guest/README.md) (`ALPINE_TARBALL=file` overrides the download). wterm (`@wterm/dom` 0.5.10, Apache-2.0) is an npm dependency of `web/`, pinned exactly and locked in `pnpm-lock.yaml` (see web.md). -`guest/distro/alpine/opt/build.sh` pins Bootlin's aarch64 musl gcc -(bleeding-edge 2026.08-1, gcc 16.2), the Alpine apks of its sysroot, and -per package the aports commit and APKBUILD (whose sha512sums cover the -sources). diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a5446c9..4351d71 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -37,9 +37,6 @@ # systems, erofs-utils, lz4 and zstd (checksums are verified # anyway); key: env.sh, build.sh and the guest's pins # (guest/**/pins.sh) -# alpine-opt the rebuilt busybox, zlib, openssl and apk-tools (installed -# files only, ~9 MB; ~4 min to rebuild); key: -# guest/distro/alpine/opt/build.sh and the Alpine pins # zig, pnpm by their setup actions name: Release @@ -139,8 +136,7 @@ jobs: sudo tee /etc/apt/sources.list.d/llvm-22.list >/dev/null sudo apt-get update -q sudo apt-get install -y -q --no-install-recommends \ - clang-22 lld-22 llvm-22 cmake ninja-build flex bison bc m4 e2fsprogs \ - meson pkg-config lua5.4 patch + clang-22 lld-22 llvm-22 cmake ninja-build flex bison bc m4 e2fsprogs patch echo /usr/lib/llvm-22/bin >> "$GITHUB_PATH" - uses: mlugg/setup-zig@d1434d08867e3ee9daa34448df10607b98908d29 # v2.2.1 @@ -281,24 +277,6 @@ jobs: ls -l vendor/build/bundle/unkernel.wasm vendor/build/initramfs/base.cpio vendor/build/initramfs/base-test.cpio sha256sum vendor/build/bundle/unkernel.wasm - # busybox, zlib, openssl and apk-tools rebuilt for kwemu - # (guest/distro/alpine/opt/build.sh, run by the Alpine image's build). - # Its stamp is the hash of the script without its comments plus - # ALPINE_VERSION (guest/distro/alpine/pins.sh); the key hashes both - # files, so on a hit --install only copies files: it reads root/, .stamp - # and the APKBUILDs. Exact key only: a fallback restore could bring a - # stale build. Its host tools (meson, lua, ...) are in the host tools - # step, which setup.sh --check verifies; the gcc toolchain is downloaded. - - name: Restore the rebuilt Alpine packages - id: alpine-opt - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: | - vendor/build/alpine-opt/root - vendor/build/alpine-opt/.stamp - vendor/downloads/alpine-opt/*/APKBUILD - key: alpine-opt-${{ runner.os }}-${{ hashFiles('guest/distro/alpine/opt/build.sh', 'guest/distro/alpine/pins.sh') }} - # alpine.erofs and debian.erofs (and .ext4, not released), their ids # and distros.json, after the base bundle: the Debian image boots on it # to make apt's source cache, and distros.json takes its ABI and base @@ -331,20 +309,6 @@ jobs: console.log(`ok: abi ${j.abi}, ${j.base}, ${j.distros.map((d) => `${d.file} ${d.image}`).join(", ")}`); ' - - name: Save the rebuilt Alpine packages - if: steps.alpine-opt.outputs.cache-hit != 'true' - uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: | - vendor/build/alpine-opt/root - vendor/build/alpine-opt/.stamp - vendor/downloads/alpine-opt/*/APKBUILD - key: ${{ steps.alpine-opt.outputs.cache-primary-key }} - # Keep the rebuild's downloads (~165 MB: gcc, sources) out of the - # downloads cache below; only the APKBUILDs are needed again. - - name: Drop the package rebuild's downloads - run: find vendor/downloads/alpine-opt -type f ! -name APKBUILD -delete 2>/dev/null || true - - name: Save downloads if: steps.downloads.outputs.cache-hit != 'true' uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 diff --git a/AGENTS.md b/AGENTS.md index 3347a05..fd16c6c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -24,7 +24,6 @@ programs). | [.agents/js.md](.agents/js.md) | the SDK (`js/`): rules, build checks, disks, network stack and Wisp | | [.agents/web.md](.agents/web.md) | the browser page, releasing its kernel, engine quirks | | [.agents/data-disk.md](.agents/data-disk.md) | the persistent data disk, overlay root, safe mode, image updates | -| [.agents/guest-packages.md](.agents/guest-packages.md) | busybox, zlib, openssl, apk-tools rebuilt for kwemu (`guest/distro/alpine/opt/build.sh`): build, numbers, traps | | [.agents/toolchain.md](.agents/toolchain.md) | patched wasm-ld, Binaryen, Zig 0.16, host tools, pinned downloads | | [.agents/testing.md](.agents/testing.md) | quick checks per area, CLI exit codes, the full matrix, release CI | | [.agents/status.md](.agents/status.md) | roadmap and open issues; read only for planning, or when asked | diff --git a/README.md b/README.md index c4befbf..01f8533 100644 --- a/README.md +++ b/README.md @@ -555,9 +555,7 @@ the DOM's `WebAssembly` types, so a Node-only TypeScript project needs `"lib": ## 🛠️ Building from source You need Linux (x86-64 or arm64), Zig 0.16, Node 24, clang 21+ with the -wasm target, and the usual build tools, plus meson, lua and perl for the -guest packages that are rebuilt for kwemu (on x86-64; elsewhere -`KW_ALPINE_OPT=0` keeps Alpine's builds). `./setup.sh --check` lists what's +wasm target, and the usual build tools. `./setup.sh --check` lists what's missing. The first build takes about 12 minutes, most of it spent on a patched `wasm-ld`, and uses 5 GiB of disk. diff --git a/guest/README.md b/guest/README.md index f1cc69c..57b15a3 100644 --- a/guest/README.md +++ b/guest/README.md @@ -15,7 +15,6 @@ is unrelated: kwemu's own test programs. guest/build.sh base # vendor/build/initramfs/base.cpio (+ base-test.cpio) guest/build.sh distro [NAME...] # vendor/build/distro/NAME.{erofs,ext4,id}; default: all; then index guest/build.sh index # vendor/build/distro/distros.json for the images there -guest/build.sh opt [NAME [ARG...]] # a distro's package rebuild (distro/NAME/opt/build.sh) guest/build.sh list # the distros and their versions kernel/build.sh base # base, then the bundle vendor/build/bundle/unkernel.wasm kernel/build.sh distro [NAME...] # the same as guest/build.sh distro @@ -30,7 +29,7 @@ kernel/build.sh distro [NAME...] # the same as guest/build.sh distro | `tools/` | host tools: `mkcpio.py` (cpio archives), `mktree.py` (an image's tar stream and id), `erofs-utils.sh` (pinned mkfs.erofs, with `erofs-utils-patches/`), `erofs-trace.py` (the files a traced boot read), `distros-json.mjs` (the index), `vm-run.ts` (run a command in an image at build time); `pins.sh` (erofs-utils, lz4, zstd) | | `src/` | the guest programs in the base's kw layer: `kwtty`, `kw9p`, `kwvsock`, `kwshm`, `kwwasi`; `glimmer`, which every distro image gets (`install_common`, `lib/stage.sh`); `src/test/`: `kwtest`, `kwbench`, `abitest` (base-test.cpio) | | `base/` | `build.sh`, `pins.sh` (static busybox), `initramfs/` (the cpio root: `/init`, the base shell), `rootfs/` (the kw layer, `/kw/layer`: on top of every distro's root) | -| `distro/NAME/` | `build.sh`, `pins.sh`, `rootfs/` (files added to the image); Alpine: `opt/build.sh` (busybox, zlib, openssl, apk-tools rebuilt for kwemu); Debian: `tools/` (apt-lz4) | +| `distro/NAME/` | `build.sh`, `pins.sh`, `rootfs/` (files added to the image); Debian: `tools/` (apt-lz4) | `distros.json` (written by `guest/build.sh distro` and `index`, and refreshed by `kernel/build.sh base`, `kernel`, `smp`, `lower` and @@ -92,12 +91,10 @@ base's cpio files and, with `kernel/build.sh base`, `unkernel.wasm`; not copied to `web/kernel/`), `KW_DISTRO_OUT` (images and staging trees), `KW_DISTRO_FORMATS` (`erofs ext4`), `KW_EROFS_COMPRESS`, `KW_EROFS_OPTS`, `KW_EXT4_SLACK`, `KW_RELEASE=1` (base.cpio without the test programs), -`NAME_TARBALL=file` (another upstream tarball), `KW_ALPINE_OPT=0` (Alpine's -own builds), `KW_ALPINE_OPT_DIR` (the rebuild's cache, default -`vendor/build/alpine-opt`), `KW_DEBIAN_APT_CACHE=0`, `KW_DISTRO_HINT=0` -(no distro header, no boot layout), `KW_DISTRO_TRACE=1` (boot the image -here and record `distro/NAME/boot-files` again: what a boot reads, laid -out first in the erofs image), `KW_PREFETCH_WHOLE_MAX`. A build beside the +`NAME_TARBALL=file` (another upstream tarball), `KW_DEBIAN_APT_CACHE=0`, +`KW_DISTRO_HINT=0` (no distro header, no boot layout), `KW_DISTRO_TRACE=1` +(boot the image here and record `distro/NAME/boot-files` again: what a +boot reads, laid out first in the erofs image), `KW_PREFETCH_WHOLE_MAX`. A build beside the main outputs: ```sh diff --git a/guest/base/rootfs/usr/lib/kw/shim b/guest/base/rootfs/usr/lib/kw/shim index 53b5971..fa30ba1 100755 --- a/guest/base/rootfs/usr/lib/kw/shim +++ b/guest/base/rootfs/usr/lib/kw/shim @@ -32,7 +32,7 @@ # the newest LTS in its index.json (or KW_NODE_VERSION=lts| # latest|MAJOR|X.Y.Z); the tree in ~/.local/share/node/vVERSION, # its bin/node, npm and npx symlinked from ~/.local/bin. Alpine -# needs libgcc for it (in the image; else installed with apk). +# needs libgcc for it (installed with apk). # npm install -g goes into that tree. # bun https://registry.npmjs.org/@oven/bun-PLATFORM/-/bun-PLATFORM-VERSION.tgz # (what npm install -g bun installs: the same program as @@ -195,8 +195,8 @@ node) url=$N/v$ver/$top.tar.gz dir=$HOME/.local/share/node file=v$ver size=240000000 links="node npm npx" unpack() { tar -xzof - -C "$part"; } # -o: not the tarball's owners - # The musl build links libgcc_s, the glibc one libatomic (both images - # have them: ALPINE_PACKAGES, DEBIAN_PACKAGES). + # The musl build links libgcc_s (not in Alpine's image), the glibc one + # libatomic (in Debian's: DEBIAN_PACKAGES). if [ -n "$libc" ]; then needlib libgcc_s.so.1 libgcc libgcc-s1; else needlib libatomic.so.1 libatomic libatomic1; fi ;; bun) t=linux-$bunarch$libc diff --git a/guest/build.sh b/guest/build.sh index 26f6c4f..8fa9e9e 100755 --- a/guest/build.sh +++ b/guest/build.sh @@ -14,9 +14,6 @@ # index of every image there, with the # base bundle's ABI and base id # (tools/distros-json.mjs) -# guest/build.sh opt [NAME [ARG...]] a distro's package rebuild -# (distro/NAME/opt/build.sh ARG...), -# default: every distro that has one # guest/build.sh list the distros and their pinned versions # # A distro is a directory distro/NAME/ with build.sh and pins.sh; nothing @@ -67,18 +64,6 @@ distro) index ;; index) index ;; -opt) - if [ $# -gt 0 ]; then - n=$1 - shift - [ -x "$guest/distro/$n/opt/build.sh" ] || { echo "guest/build.sh: distro '$n' has no opt/build.sh" >&2; exit 2; } - "$guest/distro/$n/opt/build.sh" "$@" - else - for n in $(distros); do - [ ! -x "$guest/distro/$n/opt/build.sh" ] || "$guest/distro/$n/opt/build.sh" - done - fi - ;; list) for n in $(distros); do v=$(N=$(echo "$n" | tr a-z A-Z); . "$guest/distro/$n/pins.sh"; eval echo "\${${N}_VERSION:-?}") @@ -86,7 +71,7 @@ list) done ;; *) - echo "usage: guest/build.sh base | distro [NAME...] | index | opt [NAME [ARG...]] | list" >&2 + echo "usage: guest/build.sh base | distro [NAME...] | index | list" >&2 exit 2 ;; esac diff --git a/guest/distro/alpine/build.sh b/guest/distro/alpine/build.sh index f21fc3e..9b77e00 100755 --- a/guest/distro/alpine/build.sh +++ b/guest/distro/alpine/build.sh @@ -3,14 +3,14 @@ # Copyright (C) 2026 Pooya Parsa # The Alpine distro image: vendor/build/distro/alpine.erofs and alpine.ext4 # (guest/lib/image.sh), the read-only root that the base initramfs mounts -# under the root overlay: the aarch64 minirootfs, the packages opt/build.sh -# rebuilds for kwemu, ALPINE_PACKAGES (pins.sh), apk with lazy repositories -# and command-not-found installs, qemu-user for foreign binaries, the curl -# and bwrap shims, the X.Org config. The kw guest tools, the runtime its init calls -# (/usr/lib/kw/) and the overlay root's boot logic come from the base. An -# `apk add` that was cut off needs no cleanup: run it again, and apk -# replaces what it left. The files unkernel adds are in rootfs/, at their -# installed paths (guest/lib/stage.sh: install_tree). +# under the root overlay: the aarch64 minirootfs as Alpine builds it, apk +# with lazy repositories and command-not-found installs, qemu-user for +# foreign binaries, the curl and bwrap shims, the X.Org config. The kw guest +# tools, the runtime its init calls (/usr/lib/kw/) and the overlay root's +# boot logic come from the base. An `apk add` that was cut off needs no +# cleanup: run it again, and apk replaces what it left. The files unkernel +# adds are in rootfs/, at their installed paths (guest/lib/stage.sh: +# install_tree). # # /sbin/init busybox init (the minirootfs's), etc/inittab: # the kw layer's rc, welcome and net (/usr/lib/kw/), @@ -21,10 +21,10 @@ # /usr/lib/kw-image alpine-- # # OpenSSL's engines, legacy.so and *.dist stay (the image is not -# RAM-resident), and the image stays what apk installed (`apk audit` clean -# apart from the rebuilt packages). -# ALPINE_TARBALL=file overrides the pinned release (pins.sh); -# KW_ALPINE_OPT=0 keeps Alpine's builds (opt/build.sh). +# RAM-resident), and the image stays what apk installed (`apk audit` +# clean). Packages beyond the minirootfs come from apk on first use (the +# kw layer's shim installs libgcc for node, for instance). +# ALPINE_TARBALL=file overrides the pinned release (pins.sh). set -euo pipefail . "$(dirname "$0")/../../lib/env.sh" # root, build, guest, dl, fetch_verified @@ -35,7 +35,6 @@ umask 022 here="$guest/distro/alpine" distro_unpack alpine ALPINE # $stage, $ALPINE_TARBALL -"$here/opt/build.sh" --install "$stage" # apk starts with main only. Every apk command that loads the indexes parses # all of them, and community (21k packages to main's 6k) makes that ~2.7 s # instead of ~0.65 s under kwemu. Its line moves to /etc/kw-apk-lazy, and @@ -49,59 +48,4 @@ install_tree "$here/rootfs" "$stage" install_common "$stage" echo 'hvc0' >> "$stage/etc/securetty" -# --- in the image: the baked packages ---------------------------------------- -# ALPINE_PACKAGES (pins.sh), installed by the image's own apk, as -# distro/debian/build.sh does with apt: the host fetches the pinned .apk -# files (checked against their SHA-256) into /kw-apks of a first image -# (erofs, uncompressed), which boots on the distroless bundle -# (guest/tools/vm-run.ts). apk adds them with no repositories and no network -# (their signatures checked against the image's keys), and /etc/apk/world -# gets their names (apk would pin the files' checksums there). What that -# created or changed in the root overlay's upper layer (a new ctime, from a -# listing before and after) comes out as a tar and goes into the tree, and -# what it deleted (whiteouts) is deleted there; mktree.py then gives the new -# files root:root and the upstream epoch. Without the bundle the image has -# none of them. -kernel=$base_bundle -gen=alpine-apkgen -if [ ! -f "$kernel" ] || ! command -v node >/dev/null; then - echo "alpine: WARNING: no $kernel (kernel/build.sh base) or no node: the image has not" $ALPINE_PACKAGES >&2 -else - mkdir "$stage/kw-apks" - names= - while read -r sum name; do - [ -n "$name" ] || continue - fetch_verified "$ALPINE_MAIN_URL/$name.apk" "$dl/apk/$name.apk" "$sum" - cp "$dl/apk/$name.apk" "$stage/kw-apks/" - names+=" ${name%-*-*}" - done <<< "$ALPINE_PACKAGES" - echo "alpine: installing$names in the image" >&2 - (KW_DISTRO_FORMATS=erofs KW_EROFS_COMPRESS=none KW_DISTRO_HINT=0 \ - distro_images "$gen" "$stage" "$ALPINE_TARBALL" "alpine-$ALPINE_VERSION") >/dev/null - node "$guest/tools/vm-run.ts" "$kernel" "$distro_out/$gen.erofs" --timeout 300 \ - --get /tmp/kw-pkgs.tar="$distro_out/$gen.pkgs.tar" --get /tmp/kw-gone="$distro_out/$gen.gone" " - set -e - u=/run/kw-lower/kw/tmp/upper - [ -d \$u ] - snap() { (cd \$u && find . -mindepth 1 -exec stat -c '%Z %n' {} + | sort); } - snap > /tmp/kw-before - sleep 1 # a change now gets a ctime that is not in the listing - cp /etc/apk/world /tmp/kw-world - apk add -q --no-progress --no-network --repositories-file /dev/null /kw-apks/*.apk > /tmp/kw-log 2>&1 || - { cat /tmp/kw-log; exit 1; } - apk info -e$names > /dev/null - { cat /tmp/kw-world; printf '%s\n'$names; } | sort -u > /etc/apk/world - snap | awk 'NR == FNR { had[\$0]; next } !(\$0 in had) { sub(/^[0-9]+ /, \"\"); print }' /tmp/kw-before - | - grep -v -e '^\./var/log/' -e '^\./kw-apks' -e '^\./lib/apk/db/lock\$' > /tmp/kw-new || true - cd \$u - : > /tmp/kw-gone; : > /tmp/kw-list - while IFS= read -r p; do if [ -c \"\$p\" ]; then echo \"\$p\" >> /tmp/kw-gone; else echo \"\$p\" >> /tmp/kw-list; fi; done < /tmp/kw-new - tar -cf /tmp/kw-pkgs.tar -T /tmp/kw-list --no-recursion --numeric-owner" || - { echo "alpine: installing$names in the image failed" >&2; exit 1; } - rm -rf "$stage/kw-apks" - while read -r p; do rm -rf "${stage:?}/${p#./}"; done < "$distro_out/$gen.gone" - tar -xpf "$distro_out/$gen.pkgs.tar" -C "$stage" --no-same-owner - rm -f "$distro_out/$gen".{erofs,tar,id,debugfs,pkgs.tar,gone} -fi - distro_images alpine "$stage" "$ALPINE_TARBALL" "alpine-$ALPINE_VERSION" diff --git a/guest/distro/alpine/opt/build.sh b/guest/distro/alpine/opt/build.sh deleted file mode 100755 index 1c62fc8..0000000 --- a/guest/distro/alpine/opt/build.sh +++ /dev/null @@ -1,377 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: GPL-2.0-or-later -# Copyright (C) 2026 Pooya Parsa -# -# The Alpine packages on the guest's hot paths, rebuilt for kwemu's CPU: -# busybox, zlib, openssl (libcrypto3, libssl3) and apk-tools (libapk, apk). -# Same source, patches and configuration as the image's packages (aports at -# the commit recorded for each in the image's apk database), built as their -# APKBUILDs build them, except: -# - gcc 16 (Bootlin's aarch64 musl toolchain, used with a sysroot of -# Alpine's own packages: musl-dev is the image's musl) at -O2 for -# armv8-a without the vectorizer, and with -mgeneral-regs-only for every -# file that has no float types, where Alpine uses gcc 15 at -Os -# (busybox: -Oz and -finline-limit=0): kwemu's JIT compiles integer -# code best and calls its interpreter for the FP and vectorized code -# it does not translate; -# - linked with lld, not GNU ld (whose one R+X segment for code and -# read-only data makes each exec slower under kwemu), with RELR -# relocations; busybox is not PIE (apk is, as in Alpine). -# Numbers, traps and the update procedure: .agents/guest-packages.md. -# -# guest/distro/alpine/opt/build.sh build all (again) -# guest/distro/alpine/opt/build.sh PKG... build some (busybox zlib -# openssl apk-tools) -# guest/distro/alpine/opt/build.sh --install STAGE put the files into a root -# fs stage (../build.sh); -# builds first if out of -# date -# (guest/build.sh opt alpine [PKG...] runs it too.) -# -# KW_ALPINE_OPT=0 keeps Alpine's builds (--install does nothing); so does a -# stage from an ALPINE_TARBALL override whose versions differ. Building -# needs an x86_64 host (the gcc toolchain). -# -# Output: vendor/build/alpine-opt/root/ (installed files, as in the image; -# the build trees are under vendor/build/alpine-opt/src/; KW_ALPINE_OPT_DIR -# puts all of it elsewhere). zlib and openssl also go into -# vendor/build/alpine-opt/sysroot, which apk-tools builds against. The -# stamp (.stamp) is this script without its comment lines, and the Alpine -# release: a change to the comments does not rebuild. --install refuses a stage whose apk database lists other -# versions of these packages (after an Alpine update: update the commits -# and hashes below). - -set -euo pipefail -. "$(dirname "$0")/../../../lib/env.sh" # root, vendor, zig, fetch_verified, need_zig -. "$guest/distro/alpine/pins.sh" # ALPINE_VERSION -need_zig - -# aports commit and APKBUILD sha256 per package. The APKBUILD lists the -# sources with their sha512sums, which are checked. -declare -A commit=( - [busybox]=c3ef5d10e6ef6528852c51f0564963e2f8c1be19 - [zlib]=0afa2da0e8c8051c6f8f64a7a388e5a259904245 - [openssl]=29b9ec24b1b5b39aeef51fa2a044210e2ec5258e - [apk-tools]=13685e8f2cf1c103cbba2fb9efcfcf19f4918df0 -) -declare -A apkbuild_sha256=( - [busybox]=98f019f62291aff63cb798f046fb59dfd43340885de63ca2f127651fc21d0450 - [zlib]=c9348892bf3857602fe74fcc1d411e263b408b7d70344170396f5993a62bbe26 - [openssl]=ea459c051ee8d846c06f1d47d09d0f737778502c01de88c9696e96a208de5943 - [apk-tools]=092d5c1a4957bf585a4c9d27a7c68cfa07e54cffeac57b1eec154675d54d50b0 -) -pkgs="busybox zlib openssl apk-tools" -# The sysroot: Alpine packages of the image's release (musl is the image's). -APK_URL="https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_VERSION%.*}/main/aarch64" -apks=" -2e17864102278d9049c3a56f1bceed9e8cf287065617e579325123443daed8a5 musl-1.2.6-r3 -45afe0a9f78de3402b6b5d11f5153ed955bf59d59b45344d9c3fed9f4bbf2f66 musl-dev-1.2.6-r3 -d61656e81116040ebac0bc8d9046a3ba97f01a00ee085809103576e8a437468f linux-headers-7.0.0-r1 -7af699bfbe389d9eaaf94a0ba1a9eadc22c27e69e876edc69eec18c2df303a44 utmps-dev-0.1.3.3-r0 -efbecee796ef55f90f1e8f8d63d4f1fbaf218a4491751863ebda381f80259aa4 utmps-static-0.1.3.3-r0 -48ca905e32549caad4ae01b96f0965d12e1492734f16c9855d7e2754f9c81530 skalibs-dev-2.15.0.0-r0 -db0cbcda40f8ba934630772b4301ab40f7a50e9d14131e673b357d772a81d59c skalibs-static-2.15.0.0-r0 -" -# gcc 16.2.0, binutils 2.46.1, for x86_64 hosts. -GCC_TC=aarch64--musl--bleeding-edge-2026.08-1 -GCC_TC_URL="https://toolchains.bootlin.com/downloads/releases/toolchains/aarch64/tarballs/$GCC_TC.tar.xz" -GCC_TC_SHA256=aa465cba276340e377b881dfdd1429db01d8b2f170a309ed2c25158d3b44f447 - -# ARMv8.0 (what kwemu runs). kwemu's JIT compiles integer, load/store and -# branch instructions; FP and AdvSIMD data processing are `generic` ops that -# it hands back to the interpreter one by one, and a compiled block stops at -# the first. gcc -O2 vectorizes (-ftree-vectorize, gcc 12 and later) and -# spills general registers to FP registers (fmov): busybox's sha256sum ran -# 44% generic ops and ten times slower than Alpine's -Os build. So: no -# vectorizer, and -mgeneral-regs-only for every file that compiles with it -# (the cc wrapper, setup()); files with float types build without it. -OPT_CFLAGS="-O2 -march=armv8-a -fno-tree-vectorize" -# --undefined-version: lld rejects version scripts naming symbols a link -# does not define, which GNU ld allows (zlib's configure test does that). -OPT_LDFLAGS="-Wl,--as-needed,-O1,--sort-common -Wl,-z,pack-relative-relocs -Wl,--undefined-version" - -dl="$vendor/downloads/alpine-opt" -b="$(realpath -m "${KW_ALPINE_OPT_DIR:-$vendor/build/alpine-opt}")" -dest="$b/root" -sysroot="$b/sysroot" -jobs="-j${JOBS:-$(nproc)}" -# A change to this script (but its comments) or to the Alpine release means -# a rebuild. -stamp="$(grep -v '^[[:space:]]*#' "${BASH_SOURCE[0]}" | sha256sum | cut -c1-16)-$ALPINE_VERSION" - -# fetch PKG: the APKBUILD (pinned) and its sources (checked against its -# sha512sums); sets $srcs (the source list) and $ver (pkgver-rpkgrel). -# aports comes from its GitHub mirror (same commits): gitlab.alpinelinux.org -# answers raw file requests from GitHub's hosted runners with HTTP 418. -# Upstream sources fall back to Alpine's distfiles mirror, as abuild does -# (the builders' copies of them), also when they answer with other bytes -# (zlib.net did, to GitHub's hosted runners). -fetch() { - local p=$1 s f u sum url="https://raw.githubusercontent.com/alpinelinux/aports/${commit[$1]}/main/$1" - local mirror="https://distfiles.alpinelinux.org/distfiles/v${ALPINE_VERSION%.*}" - local -A sha512 - mkdir -p "$dl/$p" - fetch_verified "$url/APKBUILD" "$dl/$p/APKBUILD" "${apkbuild_sha256[$p]}" - mapfile -t srcs < <(set +u; . "$dl/$p/APKBUILD"; for s in $source; do echo "$s"; done) - ver=$(set +u; . "$dl/$p/APKBUILD"; echo "$pkgver-r$pkgrel") - while read -r sum s; do sha512[$s]=$sum; done < <(set +u; . "$dl/$p/APKBUILD"; echo "$sha512sums" | awk 'NF == 2') - for s in "${srcs[@]}"; do - case "$s" in - https://*|http://*) f="$dl/$p/${s##*/}"; set -- "$s" "$mirror/${s##*/}" ;; - *) f="$dl/$p/$s"; set -- "$url/$s" ;; - esac - sum=${sha512[${f##*/}]:-} - [ -n "$sum" ] || { echo "alpine-opt: $p: no sha512sum for ${f##*/}" >&2; exit 1; } - [ -f "$f" ] && [ "$(sha512sum < "$f" | cut -d' ' -f1)" = "$sum" ] && continue - rm -f "$f" - for u; do - if curl -fsSL --retry 3 -o "$f.part" "$u"; then - [ "$(sha512sum < "$f.part" | cut -d' ' -f1)" = "$sum" ] && mv "$f.part" "$f" && break - echo "alpine-opt: $u: sha512 mismatch${2:+, trying the next source}" >&2 - else - echo "alpine-opt: $u failed${2:+, trying the next source}" >&2 - fi - rm -f "$f.part" - shift - done - [ -f "$f" ] || { echo "alpine-opt: cannot download ${f##*/}" >&2; exit 1; } - done -} - -# unpack PKG TARBALL DIR: a fresh source tree with the APKBUILD's patches, -# in its order; cd into it. -unpack() { - local p=$1 s - rm -rf "$b/src/$3" - mkdir -p "$b/src" - tar -xf "$dl/$p/$2" -C "$b/src" - for s in "${srcs[@]}"; do - case "$s" in *.patch) patch -s -d "$b/src/$3" -p1 -i "$dl/$p/$s" ;; esac - done - cd "$b/src/$3" -} - -setup() { - mkdir -p "$b/bin/lld" "$dl" - tc="$vendor/toolchain-gcc/$GCC_TC/bin/aarch64-linux" - if [ ! -x "$tc-gcc" ]; then - fetch_verified "$GCC_TC_URL" "$dl/$GCC_TC.tar.xz" "$GCC_TC_SHA256" - rm -rf "$vendor/toolchain-gcc/$GCC_TC" - mkdir -p "$vendor/toolchain-gcc" - tar -xJf "$dl/$GCC_TC.tar.xz" -C "$vendor/toolchain-gcc" - fi - # .done holds the apk list, so a changed list rebuilds the sysroot. - if [ "$(cat "$sysroot/.done" 2> /dev/null)" != "$(echo "$apks" | sha256sum)" ]; then - rm -rf "$sysroot" - mkdir -p "$sysroot" - local sum name - while read -r sum name; do - [ -n "$name" ] || continue - fetch_verified "$APK_URL/$name.apk" "$dl/apk/$name.apk" "$sum" - tar -xzf "$dl/apk/$name.apk" -C "$sysroot" --exclude='.PKGINFO' \ - --exclude='.SIGN.*' --exclude='.pre-*' --exclude='.post-*' 2>/dev/null || true - done <<< "$apks" - echo "$apks" | sha256sum > "$sysroot/.done" - fi - # gcc against the sysroot, linking with zig's lld. The host compiler - # (busybox's build tools) is zig cc, which finds the host's libc by - # asking $CC, so its wrapper unsets that. - printf '#!/bin/sh\nexec "%s" ld.lld "$@"\n' "$zig" > "$b/bin/lld/ld.lld" - # Each C compile (-c of a .c file) first with -mgeneral-regs-only (no FP - # or AdvSIMD registers in the code gcc generates), and again without it - # if that fails, which is what a file using float types does. A - # variadic function built that way does not save the FP argument - # registers, so a double passed to it is lost (busybox awk's printf - # printed 0.00): kw-gro.h makes va_start fail to compile under - # -mgeneral-regs-only, which sends such files down the second path too. - # The first attempt's messages are shown only when it succeeds. - cat > "$b/bin/kw-gro.h" <<-'EOF' - /* guest/distro/alpine/opt/build.sh: va_start needs the FP registers (see there) */ - #include - static inline __attribute__((always_inline)) void kw_gro_va_start(void) - { - __asm__ volatile("" :: "w"(0.0)); - } - #undef va_start - #if (defined __STDC_VERSION__ && __STDC_VERSION__ > 201710L) - #define va_start(...) (kw_gro_va_start(), __builtin_c23_va_start(__VA_ARGS__)) - #else - #define va_start(v, l) (kw_gro_va_start(), __builtin_va_start(v, l)) - #endif - EOF - cat > "$b/bin/cc" <<-EOF - #!/bin/sh - set -- --sysroot="$sysroot" -fuse-ld=lld -B"$b/bin/lld/" "\$@" - c= src= - for a; do case "\$a" in -c) c=1 ;; *.c) src=1 ;; esac; done - if [ -n "\$c" ] && [ -n "\$src" ]; then - log=\$(mktemp) - if "$tc-gcc" -mgeneral-regs-only -include "$b/bin/kw-gro.h" "\$@" 2> "\$log"; then - cat "\$log" >&2; rm -f "\$log"; exit 0 - fi - rm -f "\$log" - fi - exec "$tc-gcc" "\$@" - EOF - printf '#!/bin/sh\nunset CC\nexec "%s" cc "$@"\n' "$zig" > "$b/bin/host-cc" - chmod +x "$b/bin/cc" "$b/bin/host-cc" "$b/bin/lld/ld.lld" - export CC="$b/bin/cc" AR="$tc-ar" RANLIB="$tc-ranlib" NM="$tc-nm" STRIP="$tc-strip" - export CFLAGS="$OPT_CFLAGS" LDFLAGS="$OPT_LDFLAGS" - # busybox and openssl embed a build time, and the image id (the overlay - # root's key) hashes the files: a fixed time keeps a rebuild's output, - # and so the id, the same. - export SOURCE_DATE_EPOCH=1767225600 # 2026-01-01 -} - -build_busybox() { - fetch busybox - unpack busybox "busybox-${ver%-r*}.tar.bz2" "busybox-${ver%-r*}" - # Makefile.flags forces -Oz and, for gcc, -finline-limit=0 and - # -fno-guess-branch-probability; CONFIG_EXTRA_CFLAGS brings ours. - local f=Makefile.flags n re='^CFLAGS \+= \$\(call cc-option,(-Oz,|-finline-limit=0,|-fno-guess-branch-probability,)' - n=$(grep -c -E "$re" $f) - [ "$n" = 4 ] || { echo "busybox: $f: expected 4 lines to drop, found $n" >&2; exit 1; } - sed -i -E "/$re/d" $f - local out="$b/src/busybox-build" - rm -rf "$out" - mkdir -p "$out" - # The APKBUILD's dynamic build: busyboxconfig, utmps linked statically. - sed -e 's/.*CONFIG_PIE.*/# CONFIG_PIE is not set/' "$dl/busybox/busyboxconfig" > "$out/.config" - local mk=(make O="$out" $jobs CC="$CC" HOSTCC="$b/bin/host-cc" LD="$tc-ld" - AR="$AR" STRIP="$STRIP" - CONFIG_EXTRA_CFLAGS="$OPT_CFLAGS -fno-pie -I$sysroot/usr/include/utmps" - CONFIG_EXTRA_LDFLAGS="$OPT_LDFLAGS -no-pie" - CONFIG_EXTRA_LDLIBS="-L$sysroot/usr/lib -l:libutmps.a -l:libskarnet.a") - CFLAGS= LDFLAGS= "${mk[@]}" silentoldconfig > /dev/null - CFLAGS= LDFLAGS= "${mk[@]}" - install -Dm755 "$out/busybox" "$dest/bin/busybox" -} - -build_zlib() { - fetch zlib - unpack zlib "zlib-${ver%-r*}.tar.gz" "zlib-${ver%-r*}" - ./configure --prefix=/usr --shared --disable-crcvx - make $jobs - make install DESTDIR="$dest" - make install DESTDIR="$sysroot" - [ -e "$dest/usr/lib/libz.so.1" ] || { echo "zlib: no shared library" >&2; exit 1; } -} - -build_openssl() { - fetch openssl - unpack openssl "openssl-${ver%-r*}.tar.gz" "openssl-${ver%-r*}" - # libcrypto picks its aarch64 code from AT_HWCAP, as upstream: kwemu's - # JIT compiles the AES, PMULL, SHA and NEON instructions, and those - # paths are faster than the integer ones (.agents/guest-packages.md). - # As the APKBUILD's native build (threads on). - cat > "$b/bin/cc-openssl" <<-EOF - #!/bin/sh - exec "$b/bin/cc" "\$@" - EOF - chmod +x "$b/bin/cc-openssl" - # The flags go in as arguments, as in the APKBUILD, not also from the - # environment. CC by name (on PATH): Configure records the compiler - # command in libcrypto (`openssl version -a`), so a path would put the - # build host's directory into the image. - PATH="$b/bin:$PATH" CC=cc-openssl CFLAGS= LDFLAGS= perl ./Configure linux-aarch64 \ - --prefix=/usr --libdir=lib --openssldir=/etc/ssl enable-fips enable-ktls shared \ - no-zlib no-async no-comp no-idea no-mdc2 no-rc5 no-ec2m no-ssl3 no-seed \ - no-weak-ssl-ciphers $OPT_CFLAGS $OPT_LDFLAGS -Wa,--noexecstack - PATH="$b/bin:$PATH" make $jobs build_sw - make install_sw DESTDIR="$dest" - make install_sw DESTDIR="$sysroot" -} - -build_apk_tools() { - fetch apk-tools - unpack apk-tools "apk-tools-v${ver%-r*}.tar.gz" "apk-tools-v${ver%-r*}" - cat > cross.ini <<-EOF - [binaries] - c = '$CC' - ar = '$AR' - strip = '$STRIP' - pkg-config = 'pkg-config' - [properties] - sys_root = '$sysroot' - pkg_config_libdir = '$sysroot/usr/lib/pkgconfig' - [host_machine] - system = 'linux' - cpu_family = 'aarch64' - cpu = 'aarch64' - endian = 'little' - EOF - # abuild-meson's options, then the APKBUILD's, without the lua and - # python modules and the man pages (not in the image); the help text - # needs only a lua interpreter. - VERSION="$ver" meson setup --cross-file cross.ini \ - --prefix=/usr --libdir=/usr/lib --sysconfdir=/etc --localstatedir=/var \ - --buildtype=plain --wrap-mode=nofallback -Db_staticpic=true -Db_pie=true \ - --auto-features=disabled --bindir=/sbin -Darch=aarch64 -Ddocs=disabled \ - -Dhelp=enabled -Dlua_bin="$lua" -Dzstd=disabled -Dlua=disabled -Dpython=disabled \ - -Dtests=disabled output - ninja -C output - DESTDIR="$dest" meson install --no-rebuild -C output -} - -build() { - local p t f - [ "$(uname -m)" = x86_64 ] || { echo "alpine-opt: building needs an x86_64 host (Bootlin's gcc); KW_ALPINE_OPT=0 keeps Alpine's builds" >&2; exit 1; } - for t in meson ninja pkg-config patch perl; do - command -v $t > /dev/null || { echo "alpine-opt: need $t" >&2; exit 1; } - done - lua=$(command -v lua5.4 || command -v lua || true) - [ -n "$lua" ] || { echo "alpine-opt: need lua (5.x)" >&2; exit 1; } - perl -MFindBin -MIPC::Cmd -MFile::Compare -e 1 2> /dev/null || - { echo "alpine-opt: need perl with FindBin, IPC::Cmd, File::Compare (openssl)" >&2; exit 1; } - setup - rm -f "$b/.stamp" - for p in "$@"; do - echo "alpine-opt: $p" - (set -e; "build_${p//-/_}") - done - # Strip as abuild does: libraries --strip-unneeded, programs fully. - find "$dest" -type f | while read -r f; do - case "$(head -c 4 "$f" | od -An -tx1 | tr -d ' ')" in 7f454c46) ;; *) continue ;; esac - case "$f" in *.so|*.so.*) "$STRIP" --strip-unneeded "$f" ;; *.o|*.a) ;; *) "$STRIP" "$f" ;; esac - done - [ "$*" = "$pkgs" ] && echo "$stamp" > "$b/.stamp" - echo "alpine-opt: $dest" -} - -# install_into STAGE: replace the image's files of these packages with the -# rebuilt ones. The apk database keeps Alpine's checksums for them (`apk -# audit` lists them); upgrading a package brings Alpine's build back. -install_into() { - local stage=$1 db="$1/lib/apk/db/installed" p f want have - if [ "${KW_ALPINE_OPT:-1}" = 0 ]; then - echo "alpine-opt: KW_ALPINE_OPT=0, keeping Alpine's builds" - return - fi - [ "$(cat "$b/.stamp" 2> /dev/null)" = "$stamp" ] || build $pkgs - for p in $pkgs; do - want=$(set +u; . "$dl/$p/APKBUILD"; echo "$pkgver-r$pkgrel") - # The package's V: in the db (records end at an empty line). - have=$(awk -v o="o:$p" '/^$/ { if (m) print v; m = 0; v = ""; next } - /^V:/ { v = substr($0, 3) } $0 == o { m = 1 } END { if (m) print v }' "$db" | sort -u) - [ "$have" = "$want" ] && continue - if [ -n "${ALPINE_TARBALL:-}" ]; then - echo "alpine-opt: warning: the stage (ALPINE_TARBALL) has $p ${have:-(none)}, the rebuild is $want; keeping Alpine's builds" >&2 - return - fi - echo "alpine-opt: the stage has $p ${have:-(none)}, the rebuild is $want (update guest/distro/alpine/opt/build.sh)" >&2 - exit 1 - done - for f in bin/busybox "usr/lib/libz.so.$(set +u; . "$dl/zlib/APKBUILD"; echo "$pkgver")" \ - usr/lib/libcrypto.so.3 usr/lib/libssl.so.3 usr/lib/libapk.so.3.0.0 sbin/apk; do - [ -f "$stage/$f" ] && [ ! -L "$stage/$f" ] && [ -f "$dest/$f" ] || - { echo "alpine-opt: $f missing in the stage or the build" >&2; exit 1; } - cat "$dest/$f" > "$stage/$f" - done - echo "alpine-opt: installed into $stage" -} - -if [ "${1:-}" = --install ]; then - install_into "${2:?usage: $0 --install STAGE}" -else - build ${@:-$pkgs} -fi diff --git a/guest/distro/alpine/pins.sh b/guest/distro/alpine/pins.sh index e471fdc..4500489 100644 --- a/guest/distro/alpine/pins.sh +++ b/guest/distro/alpine/pins.sh @@ -1,16 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2026 Pooya Parsa -# Pinned inputs of the Alpine image (build.sh, opt/build.sh), sourced: the -# aarch64 minirootfs. ALPINE_TARBALL=file overrides the download. +# Pinned inputs of the Alpine image (build.sh), sourced: the aarch64 +# minirootfs. ALPINE_TARBALL=file overrides the download. ALPINE_VERSION=3.24.4 ALPINE_TARBALL_NAME="alpine-minirootfs-$ALPINE_VERSION-aarch64.tar.gz" ALPINE_URL="https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_VERSION%.*}/releases/aarch64/$ALPINE_TARBALL_NAME" ALPINE_SHA256=4b14c18f7e90a063b61462ea1462a5732c154bf6adad594974fe6487cd98a8de -# The packages the image comes with on top of the minirootfs (build.sh), from -# main: "SHA-256 NAME-VERSION" each. apk installs them without the network, -# so their dependencies must be in the minirootfs or in this list. -# libgcc: the musl Node.js build links libgcc_s (the kw layer's node shim). -ALPINE_MAIN_URL="https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_VERSION%.*}/main/aarch64" -ALPINE_PACKAGES=" -369aaa6e9d099a737bad6dd3e6c2fe7bb1547ca26d22b94ee0411228f709b403 libgcc-15.2.0-r5 -" diff --git a/kernel/build.sh b/kernel/build.sh index 5f0f679..3f3d92f 100755 --- a/kernel/build.sh +++ b/kernel/build.sh @@ -69,10 +69,8 @@ # # Environment: # KW_BUILD=dir build output root instead of vendor/build (every output above but -# the toolchain's vendor/build/lld22; the Alpine package rebuild -# (guest/distro/alpine/opt/build.sh) also stays in vendor/build/alpine-opt, -# a cache that its stamp keys, unless KW_ALPINE_OPT_DIR=dir), -# e.g. with KSRC to build a branch without touching the main outputs. +# the toolchain's vendor/build/lld22), e.g. with KSRC to build a branch +# without touching the main outputs. # The CLI, the SDK build and the tests read vendor/build: give them # paths (--kernel, --distro, UNKERNEL_TEST_KERNEL, UNKERNEL_TEST_DISTRO_DIR) # KW_BASE_OUT=dir `base` (and the bundle refresh of `kernel`, `smp`, `lower`, `datadisk`) diff --git a/setup.sh b/setup.sh index d9e6d8d..5382a2e 100755 --- a/setup.sh +++ b/setup.sh @@ -77,44 +77,17 @@ check() { for t in clang++ ld.lld llvm-ar llvm-nm llvm-objcopy llvm-objdump llvm-ranlib llvm-readelf llvm-strip; do tool "$t" "part of LLVM (clang, lld, llvm packages)" && note "$t" ok done - for t in cmake ninja gcc make flex bison bc m4 python3 git curl tar gzip bzip2 xz zstd sha256sum sha512sum; do + for t in cmake ninja gcc make flex bison bc m4 python3 git curl tar gzip bzip2 xz zstd sha256sum; do tool "$t" && note "$t" ok done for t in mke2fs debugfs e2fsck; do tool "$t" "e2fsprogs >= 1.43" && note "$t" ok done - # guest/distro/alpine/opt/build.sh, the guest packages rebuilt for kwemu (run by - # the Alpine image's build): meson for apk-tools (a Python program: only looked up), - # lua for apk's help text, perl modules for openssl's Configure. + # patch applies erofs-utils' patches (guest/tools/erofs-utils.sh); perl runs + # in the kernel build. for t in patch perl; do tool "$t" && note "$t" ok done - # Fedora's pkg-config is a shell script around the pkgconf binary. - t="$(real_bin pkgconf)"; [ -n "$t" ] || t="$(real_bin pkg-config)" - if [ -n "$t" ] && [ "$(head -c 4 "$t" | od -An -tx1 | tr -d ' \n')" = 7f454c46 ]; then - note pkg-config ok - else - fail pkg-config "install pkg-config or pkgconf" - fi - if [ -n "$(type -P meson)" ]; then - note meson ok - else - fail meson "install meson (guest/distro/alpine/opt/build.sh builds apk-tools with it)" - fi - if [ "$(uname -m)" != x86_64 ]; then - note alpine-opt "WARNING: its gcc toolchain is for x86_64 hosts; build with KW_ALPINE_OPT=0 (Alpine's builds)" - fi - t="$(real_bin lua5.4)"; [ -n "$t" ] || t="$(real_bin lua)" - if [ -n "$t" ] && [ "$(head -c 4 "$t" | od -An -tx1 | tr -d ' \n')" = 7f454c46 ]; then - note lua ok - else - fail lua "install lua 5.x (apk's help text)" - fi - if [ -n "$(real_bin perl)" ] && "$(real_bin perl)" -MFindBin -MIPC::Cmd -MFile::Compare -e 1 2>/dev/null; then - note perl-modules ok - else - fail perl-modules "perl with FindBin, IPC::Cmd and File::Compare (openssl's Configure)" - fi avail="$(df -Pk "$root" | awk 'NR == 2 {print int($4 / 1048576)}')" if [ ! -d "$vendor/src/llvm22" ] && [ "${avail:-0}" -lt 10 ]; then diff --git a/test/guest-bench.sh b/test/guest-bench.sh index a2b343b..208676b 100755 --- a/test/guest-bench.sh +++ b/test/guest-bench.sh @@ -2,7 +2,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2026 Pooya Parsa # -# The apk and shell bench that .agents/a64/performance.md and guest-packages.md quote: +# The apk and shell bench that .agents/a64/performance.md quotes: # 20 commands, each timed in the guest from /proc/uptime (10 ms steps). # Run it inside the guest (a network for apk; it installs jq, git and # python3), e.g. with the directory mounted: