// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // Terminal and boot flow, loaded by main.ts once the preflight passed. The // SDK does the work (download, streaming compile, memory with fallback // sizes, console routing, network); this module shows it (with ?worker=1 // the kernel runs in a Web Worker, the SDK's worker mode, which gets the // module compiled here; input and frames cross through shared memory when // the page is cross-origin isolated, else as messages, and sound as messages): // a wterm terminal with a systemd-style boot transcript, the status // indicator and stages (ui.ts), and the kernel's console as terminal 0, with // more terminals over vsock (wterms.ts), the guest preview // (preview.ts: guest web servers in an iframe), and the Wayland desktop // (wayland.ts: the guest's Wayland apps as windows, through kwshm). With several CPUs (?cpus=N, // config.ts) it loads the SMP kernel instead and boots it in worker mode, // whose worker starts the other CPUs; whatever stops that before the // kernel runs reloads once on one CPU (bootUpOnce()). A suspended session // resumes on the next load, quietly, then says so in a toast (offerResume(), // sayResumed()), on the CPU count it was saved on (offerResumeCpus()). The kernel is the // base bundle, with the distro image ?distro= picks (config.ts) attached // read-only by URL (createKernel({ distro }); inline it is opened here // first, for the resume check), and the Alpine-only part (the apk index // prefetch) runs only on Alpine. A machine the user has used is suspended by // itself when the tab stays hidden (?autosuspend=, phones by default) and // resumed when it is shown, and closing the page asks first (autosuspend.ts). // With the Tailscale option (?tailscale=1) the network is a spec with the // node (tailscale.ts) in both modes; a node that cannot start leaves the boot // with the Wisp network only. import "@wterm/dom/src/terminal.css"; import { WTerm } from "@wterm/dom"; import { prefetchApkIndexes } from "./apk.ts"; import { autoHide } from "./autohide.ts"; import { AUTO_SUSPEND_HOLD_MS, AutoSuspend, AutoSuspendSkip, LEAVE_OFFER_MS, LEAVE_STAYED_MS, Pause, SuspendSaveError, allowLeave, autoSuspendLabel, pageLeaving, quietStart, resumeAskedFor, resumeChoice, resumedNote, sessionRoom, wantLeavePrompt, type SuspendFacts } from "./autosuspend.ts"; import { CPUS_ONCE_KEY, GL_DISTROS, INLINE_ONCE_KEY, REPO, UP_ONCE_KEY, dataSizeCap, vkAdapterBlocker } from "./config.ts"; import { OTHER_TAB, attachDataDisk, dataStores, detachDataDisk, diskLockFreed, downloadKwsparse, exportDataDisk, importDataDisk, resetDataDisk, resetOpfsDataDisk, runDataJob, storageState, acquireDiskLock, type DataAttach, type DiskLock } from "./data.ts"; import { createDisplayUi } from "./display.ts"; import { isPageChrome, ownsKeys } from "./focus.ts"; import { agoSince, capitalize, fmtMiB, fmtS, fmtSize, plural } from "./format.ts"; import { KW } from "./kw.ts"; import { showcaseFromUrl, waitPort } from "./launcher.ts"; import { browserNet, browserNetSpec } from "./net.ts"; import { openCommand } from "./open.ts"; import { store } from "./prefs.ts"; import { attachPreview, closePreview, openPreview, previewState, previewSupported, previewUrl } from "./preview.ts"; import { isRunning, isStopped, ui$ } from "./store.ts"; import { currentTerminal } from "./termtabs.ts"; import { ScreenTail, clearSession, loadSession, saveSession, sessionCpus, sessionInfo, sessionMismatch, type SessionDevices, type SessionMeta } from "./session.ts"; import { APPLE, CHORD, keyFor, PREFIX } from "./keys.ts"; import { dropLegacyScope } from "./sw.ts"; import { clearResumeRequest, setResumeRequest, takeResumeRequest } from "./ui.ts"; import { createTerminals, ctrlCInterrupts, pasteText, setTermFont, termText, TERM_OPTIONS } from "./wterms.ts"; import { attachWayland, waylandApi } from "./wayland.ts"; import { attachTailscale, onTailscaleEvent, tailscaleCheck, tailscaleSpec } from "./tailscale.ts"; import { attachSerial } from "./serial.ts"; import { attachBluetooth } from "./bluetooth.ts"; import { attachUsb } from "./usb.ts"; import { cameraIn, micIn, soundOut } from "./media.ts"; import { BootError, ConsoleRouter, KernelInstance, SnapshotCommitError, WorkerKernel, bundledBoot, createKernel,dataDiskInfo, fenceDataDisk, loadKernelModule, openDistroDisk, opfsAvailable, serveWasiPrograms, snapshotDiskMismatch, type BlockDevice, type DataDiskBackend, type DistroDisk, type BootProgress, type DiskSpec, type DiskState, type DownloadProgress, type Kernel, type KernelHandle, type KernelSnapshot, type SnapshotHoldDecision, type CameraColour, type GlStats, type VirglTotals, } from "./sdk.ts"; /** The virtio-gpu device's 3D counters (vm.gl.stats.gpu). */ type GpuRendererStats = NonNullable; /** An error tagged with what failed (fetch, compile, memory, boot) and the boot stage it belongs to. */ type StageError = Error & { kind?: string; stageId?: string }; const tag = (e: unknown, kind: string, stageId: string): StageError => Object.assign(e instanceof Error ? e : new Error(String(e)), { kind, stageId }); /** The camera's colour space as a load before saw it (`kw.camera-colour`, createCamera's onColorSpace), or null. */ function cameraColour(): CameraColour | null { try { const c = JSON.parse(store("camera-colour") ?? "null") as CameraColour | null; const str = (v: unknown) => v === null || typeof v === "string"; return c && str(c.primaries) && str(c.transfer) && str(c.matrix) && (c.fullRange === null || typeof c.fullRange === "boolean") ? c : null; } catch { return null; } } /** How long the toast after a resume ("Resumed the session suspended …", sayResumed()) shows. */ const RESUMED_TOAST_MS = 5000; // The terminal's colours and font are CSS custom properties (style.css, #term). const SGR = { ok: "\x1b[32m", warn: "\x1b[33m", fail: "\x1b[31m", busy: "\x1b[36m", dim: "\x1b[2m", gray: "\x1b[90m", bold: "\x1b[1m", off: "\x1b[0m" }; /** * Pip from the logo (assets/logo.svg) as 10x8 pixels in truecolor half * blocks (wterm draws U+2580-259F as solid blocks): four lines, 10 cells each. */ const PIP = ((art: string[]) => { const rgb: Record = { O: "106;84;24", Y: "255;212;90", S: "232;168;60", H: "255;244;192", E: "42;26;16", B: "255;154;138", G: "111;220;111", g: "79;176;79", }; const lines: string[] = []; for (let y = 0; y < art.length; y += 2) { const top = art[y]!, bottom = art[y + 1]!; let s = ""; for (let x = 0; x < top.length; x++) { const t = rgb[top[x]!], b = rgb[bottom[x]!]; s += !t && !b ? `${SGR.off} ` : t && b ? (t === b ? `\x1b[0;38;2;${t}m█` : `\x1b[0;38;2;${t};48;2;${b}m▀`) : t ? `\x1b[0;38;2;${t}m▀` : `\x1b[0;38;2;${b}m▄`; } lines.push(s + SGR.off); } return Object.assign(lines, { width: art[0]!.length }); })([ "...g.GG...", "....g.....", "..OOOOOO..", ".OHYYYYYO.", ".OYEYYEYO.", ".OBYYYYBO.", ".OYYYYSSO.", "..OOOOOO..", ]); const STEP: Record = { kernel: "Download the kernel", compile: "Compile the kernel", memory: "Allocate kernel memory", boot: "Boot the kernel" }; /** * A guest `reboot` (halt code 2) reloads the page, at most 3 times a * minute, so a guest that reboots in a loop ends up halted instead of * reloading forever. The count lives in sessionStorage (this tab only); * without it, auto-reboot still works but is not limited. */ const AUTO_REBOOT_KEY = "kw-auto-reboot", AUTO_REBOOT_MAX = 3, AUTO_REBOOT_WINDOW = 60000; function autoRebootAllowed(): boolean { const now = Date.now(); let times: number[] = []; try { const v: unknown = JSON.parse(sessionStorage.getItem(AUTO_REBOOT_KEY) ?? "[]"); if (Array.isArray(v)) times = v.filter((t): t is number => typeof t === "number" && now - t < AUTO_REBOOT_WINDOW); } catch { /* storage blocked or bad value: no limit */ } if (times.length >= AUTO_REBOOT_MAX) return false; try { sessionStorage.setItem(AUTO_REBOOT_KEY, JSON.stringify([...times, now])); } catch { /* see above */ } return true; } export function start(): void { const ui = KW.ui!; const cfg = KW.config; KW.started = true; const g = globalThis as unknown as { term?: WTerm; kernel?: Kernel; vm?: KernelHandle }; // A KernelInstance, or with ?worker=1 a WorkerKernel (no low-level kernel; the display, evdev and sound through messages). let vm: KernelHandle | null = null; const local = (): KernelInstance | null => (vm instanceof KernelInstance ? vm : null); let vmExited = false; /** createKernel() is running (in worker mode the worker may be opening the data disk). */ let kernelStarting = false; /** The kernel has exited (inline: the moment it does; a worker: once its exit message arrived). */ const ended = () => { const l = local(); return l ? !!l.kernel.result : vmExited; }; const send = (s: string | Uint8Array) => vm?.write(s); /** This load boots the SMP kernel on several CPUs (config.ts). */ const smp = cfg.cpus > 1; /** The kernel this load runs: the base's UP bundle, or its SMP one (sdk.ts kernelSmpUrl). */ const kernelUrl = smp ? cfg.kernelSmpUrl! : cfg.kernelUrl; /** The image this load boots (null: ?distro=none). */ const distro = cfg.distro; /** The session's distro (session.ts). */ const distroRef = distro ? { name: distro.name, url: distro.url } : null; /** Inline: the distro image, opened ahead (the snapshot's first disk, for the resume check). */ let distroDisk: DistroDisk | null = null; /** Only Alpine has apk and the index the page pushes (apk.ts). */ const alpine = cfg.guest === "alpine"; // A key typed for the guest (into a terminal, or the display), seen ahead of display.ts's routing, // which keeps the display's keys to itself: it ends a pause by hand and drops the notification that // offers one (guestKey, set with them below). The shortcut prefix's keys never get here (keys.ts). let guestKey: (() => void) | null = null; addEventListener("keydown", (e) => { if (!guestKey || !e.isTrusted || /^(Shift|Control|Alt|Meta|CapsLock|Fn)$/.test(e.key)) return; if (ownsKeys(e.target)) return; guestKey(); }, true); // --- display and input (kwfb, kwinput; ?display=0 turns them off): display.ts -------------- const display = createDisplayUi({ vm: () => (vm instanceof KernelInstance || vm instanceof WorkerKernel ? vm : null), touch: cfg.mobile, sendTty: (s) => KW.term?.send(s), appCursor: () => !!KW.term?.appCursor(), focusTerminal: () => KW.term?.focus(), }); // --- data disk (persistent /data) ------------------------------------------- // Shown in System's Environment list ("Data disk", "Storage") and Settings' // Data disk section; QuotaExceededError and other failed saves are errors. // Inline it is kept in IndexedDB (attachDataDisk()); in worker mode the // worker builds it from a spec, in OPFS by default (cfg.datastore, dataSpec()). let data: DataAttach | null = null; /** The data disk's lock, taken before anything touches the disk (claimDisk()); null: none taken (?data=0, no IndexedDB). */ let diskLock: DiskLock | null = null; /** Another tab has the data disk and this one runs without it (claimDisk(): Start without the disk). */ let otherTab = false; /** * One tab at a time (data-disk.md, Persistence in the browser): the lock * is taken first, before anything touches the data disk (its size, the * migration prompt, the stores, a suspended session that has it). Another * tab has it: a required question. Start without the disk: this tab runs * with no /data (the root's changes in memory). Close this tab: * window.close(), which browsers refuse for a tab the user opened; then * OtherTabCard, and nothing boots until Try again finds the lock free. * While the question or the card shows, the other tab closing * (diskLockFreed(), Web Locks only) boots this one with the disk. */ async function claimDisk(): Promise { if (!cfg.data || (typeof indexedDB === "undefined" && cfg.datastore === "idb")) return; diskLock = await acquireDiskLock(); if (diskLock.held) return; ui.hostLog(`[host] data disk: open in another tab (${diskLock.how})`); unquiet(); ui.bootStep("Open in another tab"); /** The lock, once the other tab let go of it (never while `signal` is aborted: it is let go again). */ const taken = async (signal: AbortSignal): Promise => { for (;;) { await diskLockFreed(signal); const l = await acquireDiskLock(); if (signal.aborted) { l.release?.(); return new Promise(() => {}); } if (l.held) return l; } }; const gotIt = (l: DiskLock) => { diskLock = l; ui.hostLog("[host] data disk: the other tab let go of it; booting with the data disk"); ui.toast("The other tab has closed: this tab has the data disk now"); }; let watch = new AbortController(); const r = await Promise.race([ ui.ask({ title: "open in another tab", msg: "The data disk (/data) is open in another tab, and only one tab at a time can use it. " + "Without it, this tab keeps its changes in memory only: they are lost when the tab closes.", ok: "Start without the disk", cancel: "Close this tab", required: true, }, { signal: watch.signal }).then((ok) => (ok ? "without" as const : "close" as const)), taken(watch.signal), ]); watch.abort(); if (typeof r !== "string") return gotIt(r); if (r === "without") { otherTab = true; ui.setDiskElsewhere(); ui.hostLog("[host] data disk: open in another tab; this tab runs without it (no /data, the root's changes in memory)"); return; } window.close(); await new Promise((r) => setTimeout(r, 500)); // a close the browser allows ends the page before this ui.hostLog("[host] data disk: the browser did not close this tab (a tab the page did not open); not booting while the other tab has the disk"); let note = ""; for (;;) { watch = new AbortController(); const retried = new Promise<"retry">((res) => ui.otherTab(note, () => res("retry"))); const t = await Promise.race([retried, taken(watch.signal)]); watch.abort(); if (t !== "retry") { ui.otherTab(null); return gotIt(t); } const l = await acquireDiskLock(); if (l.held) { diskLock = l; ui.otherTab(null); ui.hostLog("[host] data disk: Try again found it free; booting with the data disk"); return; } note = "It is still open in the other tab."; } } /** Worker mode on OPFS: the background removal of the fenced IndexedDB copy's blocks, while it runs. */ let oldRemoval: Promise | null = null; const storeName = (b: DataDiskBackend) => (b === "opfs" ? "OPFS" : "IndexedDB"); let saveErrorShown = -Infinity, storageFailed = false; /** The last failed save, as Settings' status line says it (dataStatus()). */ let saveFailure = ""; function saveError(e: unknown) { storageFailed = true; const err = e as Error | undefined; const quota = err?.name === "QuotaExceededError"; saveFailure = quota ? "the browser's storage quota is full" : `${err?.message ?? e}`; dataStatus(); ui.check("storage", "Storage", "fail", quota ? "quota exceeded" : "save failed", `${err?.name ?? ""} ${err?.message ?? e}`); if (performance.now() - saveErrorShown < 30000) return; // once per burst of failed flushes saveErrorShown = performance.now(); KW.report("storage", quota ? "The browser's storage quota is full: disk changes are NOT being saved (the guest sees I/O errors). Free space, or export the data disk and reset it." : `Saving disk changes to ${storeName(data?.backend ?? cfg.datastore)} failed: ${err?.message ?? e}`, err?.stack ?? "", false); } const dataLog = (m: string) => { console.log(m); ui.hostLog(m); }; async function showStorage({ ask = false } = {}) { const st = await storageState({ ask }); if (storageFailed) return; // keep the failure visible const usage = st.usage !== undefined ? `${fmtSize(st.usage)} used${st.quota ? ` of ${fmtSize(st.quota)}` : ""}` : "usage unknown"; const kept = data?.backend === "opfs" ? "; the data disk is in OPFS" + (oldRemoval ? ", its old IndexedDB copy being removed" : "") : ""; ui.check("storage", "Storage", st.persisted ? "pass" : "info", `${st.persisted === undefined ? "persistence unknown" : st.persisted ? "persistent" : "best effort"}, ${usage}`, (st.persisted ? "not evicted under storage pressure" : st.persisted === false ? "the browser may evict the saved disks under storage pressure (it did not grant navigator.storage.persist())" : "") + kept); } /** Worker mode: the worker's latest snapshot of the data disk (spec 0), if it was built. */ const workerData = (): DiskState | undefined => (vm instanceof WorkerKernel ? vm.disks.find((d) => d.index === 0 && d.kind === "data") : undefined); /** Where a disk is kept, as the status line says it: "IndexedDB kwdisk:kwdata-v1" or "OPFS kwdisk/kwdata-v1". */ const whereKept = (backend: string | undefined, store: string | undefined) => (backend === "opfs" ? `OPFS ${store?.replace(":", "/") ?? ""}` : `IndexedDB ${store ?? ""}`); /** * Settings' status line of the data disk: where it is kept (the store, the * size and what it holds in its details, the title), why it is not saved, or * that saving failed; the numbers are the quota line's (StorageQuota.tsx). */ function dataStatus() { const d = data?.disk; const w = d ? undefined : workerData(); const show = (persistent: boolean, backend: string | undefined, detail: string, notice: string | undefined) => { if (persistent && saveFailure) ui.dataStatus(`Saving to ${storeName(backend === "opfs" ? "opfs" : "idb")} failed: ${saveFailure}; disk changes are not saved.`, { detail, tone: "err" }); else if (persistent) ui.dataStatus(`Saved in this browser (${storeName(backend === "opfs" ? "opfs" : "idb")})`, { detail }); else ui.dataStatus(`In memory only: ${notice ?? "not saved"}.`, { tone: "warn" }); }; if (w) { const opfs = w.backend === "opfs"; const blocks = w.overlayBlocks ?? 0, used = opfs ? w.storedBytes ?? 0 : blocks * 4096; ui.diskUsage(used, w.size, w.persistent); show(w.persistent, w.backend, `${whereKept(w.backend, w.store)}, written by the kernel worker: ${fmtSize(w.size)} disk, ` + (opfs ? `${w.extents ?? 0} extent files holding ${fmtSize(used)}` : `${blocks} blocks (${fmtSize(used)}) stored`), data?.notice ?? w.notice); return; } if (!d) return; const blocks = d.overlayBlocks; ui.diskUsage(blocks * 4096, d.size, d.persistent); show(d.persistent, "idb", `IndexedDB ${d.store?.name}: ${fmtSize(d.size)} disk, ${blocks} blocks (${fmtSize(blocks * 4096)}) stored`, data?.notice ?? d.notice); } /** * Remove the blocks of the fenced IndexedDB copy in the background (a page * worker, data-worker.ts, the SDK's clearMovedDataDisk(): one transaction * per 1024 blocks, so a reload keeps what was done and the next load goes * on; the meta stays, a tombstone an IndexedDB page refuses). It touches no * OPFS file and the boot does not wait for it; a failure is logged and the * next load tries again. */ function removeOldCopy(blocks: number, again: boolean) { if (oldRemoval) return; const t0 = performance.now(); let said = t0; dataLog(`[host] data disk: ${again ? "going on removing" : "removing"} the old IndexedDB copy (${blocks} blocks) in the background; its meta stays, fenced`); oldRemoval = runDataJob({ op: "clear-moved" }, { log: dataLog, onProgress: (n) => { if (performance.now() - said < 5000) return; said = performance.now(); dataLog(`[host] data disk: removed ${n} of the old IndexedDB copy's blocks so far`); }, }).then( (n) => dataLog(`[host] data disk: removed the old IndexedDB copy's blocks (${n} now, in ${fmtS(performance.now() - t0)}); its fenced meta stays`), (e: Error) => dataLog(`[host] data disk: removing the old IndexedDB copy failed (the next load tries again): ${e?.name}: ${e?.message ?? e}`), ).finally(() => { oldRemoval = null; void showStorage(); }); } async function openData(module: WebAssembly.Module, size: number | undefined): Promise { // Opening IndexedDB, the tab lock, and loading the stored blocks (or seeding from the template). const t0 = performance.now(); if (cfg.datastoreWhy) dataLog(`[host] data disk: IndexedDB, not OPFS: ${cfg.datastoreWhy}`); let attached: DataAttach; try { attached = await attachDataDisk({ enabled: cfg.data, bundled: bundledBoot(module).datadisk, size, lock: diskLock, log: dataLog, onError: saveError }); } catch (e) { attached = { disk: null, lock: null, backend: "idb", reason: `failed: ${(e as Error).message}` }; console.error(e); } data = attached; const ms = performance.now() - t0; KW.dataDisk = attached.disk; const d = attached.disk; showData(d ? { size: d.size, seeded: d.seeded, storedBlocks: d.stored.size } : null, ms); return d ? [d] : []; } /** The data disk's rows, boot line and status, inline or (from the worker's snapshot) in worker mode. */ function showData(d: { size: number; seeded?: boolean; storedBlocks?: number; storedBytes?: number; extents?: number } | null, ms: number) { if (!data) return; if (!d) { const elsewhere = otherTab ? "; this tab runs without /data, its root changes in memory (lost when the tab closes)" : ""; ui.check("data", "Data disk", "info", "none", `${data.reason}${elsewhere}`); ui.dataStatus(`No data disk: ${data.reason}.`, cfg.data ? { tone: "warn", ...(elsewhere ? { detail: `The data disk is open in another tab${elsewhere}` } : {}) } : {}); dataLog(`[host] data disk: none (${data.reason}, ${fmtS(ms)})`); if (cfg.data && !/no IndexedDB/.test(data.reason ?? "")) warn(`No data disk: ${data.reason}`, ms); return; } const dev = distro ? "/dev/wdb" : "/dev/wda"; // the distro image is wda const opfs = data.backend === "opfs"; if (data.notice) { ui.check("data", "Data disk", "warn", `${dev}, not saved`, data.notice); warn(`Data disk ${dev}: ${data.notice}`, ms); ui.toast(`Data disk not saved: ${data.notice}`); dataLog(`[host] data disk: ${data.notice}`); } else { const stored = opfs ? `${fmtSize(d.storedBytes ?? 0)} in ${d.extents ?? 0} extent files` : `${d.storedBlocks ?? 0} blocks stored`; ui.check("data", "Data disk", "pass", `${dev} (${fmtSize(d.size)}), saved in this browser (${storeName(data.backend)})`, `${d.seeded ? `created from the template (${data.from})` : stored}; lock: ${data.lock?.how}; opened in ${fmtS(ms)}` + (cfg.worker ? "; the kernel worker writes it" : "")); ok(`Attached the data disk ${dev} (${d.seeded ? "new" : opfs ? `${fmtSize(d.storedBytes ?? 0)} in OPFS` : `${fmtSize((d.storedBlocks ?? 0) * 4096)} stored`})`, ms); void showStorage({ ask: true }); } dataStatus(); setInterval(dataStatus, 5000); // the stored block count grows as the guest writes } /** * Copy the IndexedDB disk to OPFS in a worker of its own (data-worker.ts, * the SDK's migrateDataDisk(): batches, the IndexedDB copy fenced once the * blocks are copied), with its progress on the boot line; once the OPFS * disk's complete header is durable (the job resolved), the IndexedDB * blocks go in the background. Returns why not, or null. */ async function migrateNow(): Promise { const t0 = performance.now(); let shown = 0; try { const r = await runDataJob<{ blocks: number; bytes: number; kept: boolean }>({ op: "migrate" }, { log: dataLog, onProgress: (done, total) => { const pct = total ? Math.floor((100 * done) / total) : 100; setLive(" .... ", "Copying the data disk to OPFS", `${pct}%, ${fmtSize(done * 4096)} of ${fmtSize(total * 4096)}`); if (performance.now() - shown > 1500) { shown = performance.now(); ui.toast(`Copying the data disk to OPFS: ${pct}%`); } }, }); setLive(null); ok(`Copied the data disk to OPFS (${r.blocks} blocks, ${fmtSize(r.bytes)})`, performance.now() - t0); ui.toast("The data disk is in OPFS now; its IndexedDB copy is being removed"); removeOldCopy(r.blocks, false); return null; } catch (e) { setLive(null); const err = e as Error; ui.hostLog(`[host] data disk: the copy to OPFS failed: ${err.name}: ${err.message}`); return err.name === "DataDiskSpaceError" ? `the copy to OPFS does not fit: ${err.message}` : `the copy to OPFS failed (${err.name}: ${err.message})`; } } /** * Data disk migration, one way: worker mode on OPFS, under the lock, * before the boot. A saved IndexedDB disk that is not fenced, with no * complete OPFS disk, asks once (a required question: only its buttons * answer). Migrate copies it to OPFS (its uid and generation kept, so a * suspended session resumes on the copy), Start fresh fences it and the * worker makes a new disk from the template; either way its blocks are * then removed in the background (removeOldCopy()), the fenced meta kept. * The fence is the recorded choice. A fenced copy with blocks left (a * removal a reload cut off) is removed on: set aside for a new disk, or * once its copy finished (the complete OPFS disk has its uid); never one * whose copy did not. Returns why this load stays on IndexedDB (the copy * did not fit or failed: the IndexedDB disk stays unfenced), else null. */ async function migrationCheck(): Promise { let st: Awaited>; try { st = await dataStores(); } catch (e) { ui.hostLog(`[host] data disk: cannot read what IndexedDB and OPFS hold: ${(e as Error)?.message ?? e}`); return null; } const { idb, opfs } = st; if (idb.exists && idb.fenced) { const left = idb.blocks ?? 0; const copied = idb.fencedFor === "migrated" || (opfs.complete && opfs.uid !== undefined && opfs.uid === idb.uid); if (left > 0 && (idb.fencedFor === "fresh" || copied)) removeOldCopy(left, true); else if (left > 0) ui.hostLog(`[host] data disk: the old IndexedDB copy (${left} blocks) is fenced but its copy to OPFS did not finish: it is kept`); return null; } if (!idb.exists || !idb.complete || idb.size === undefined || opfs.complete) return null; unquiet(); const move = await ui.ask({ title: "data disk", msg: `Your data disk (/data, ${fmtSize((idb.blocks ?? 0) * 4096)} saved) is in IndexedDB. The page now keeps it in OPFS, the browser's file storage for sites, which is several times faster. ` + "Migrate copies it to OPFS and then removes the IndexedDB copy. Start fresh makes a new, empty disk and deletes the old one. Neither can be undone.", ok: "Migrate", cancel: "Start fresh", required: true, }); if (!move) { try { await fenceDataDisk({ log: dataLog }); } catch (e) { // Not fenced: nothing is removed, and the next load asks again. ui.hostLog(`[host] data disk: could not set the IndexedDB disk aside: ${(e as Error)?.message ?? e}`); return null; } ui.hostLog("[host] data disk: starting fresh in OPFS; the IndexedDB disk is fenced and its blocks are removed in the background"); removeOldCopy(idb.blocks ?? 0, false); return null; } return migrateNow(); } /** * Worker mode: the data disk as a spec, under the lock claimDisk() took on * the page (the worker then is the only writer of the store); a tab * without it has no data disk (the guest's root changes stay in memory); * the worker opens the store and builds it. */ async function dataSpec(size: number | undefined): Promise { let backend: DataDiskBackend = cfg.datastore; if (cfg.datastoreWhy) ui.hostLog(`[host] data disk: IndexedDB, not OPFS: ${cfg.datastoreWhy}`); if (!cfg.data) data = { disk: null, lock: null, backend, reason: "off (?data=0)" }; else if (backend === "idb" && typeof indexedDB === "undefined") data = { disk: null, lock: null, backend, reason: "no IndexedDB in this browser" }; else if (otherTab) data = { disk: null, lock: diskLock, backend, reason: OTHER_TAB }; else { const lock = diskLock ?? await acquireDiskLock(); let why: string | null = null; if (backend === "opfs" && !(await opfsAvailable())) why = "the browser refused OPFS (navigator.storage.getDirectory(), e.g. in a private window)"; else if (backend === "opfs") why = await migrationCheck(); if (why) { backend = "idb"; warn(`Data disk: IndexedDB this load, not OPFS: ${why}`); ui.toast(`The data disk stays in IndexedDB this load: ${why}`); } if (backend === "idb" && typeof indexedDB === "undefined") { data = { disk: null, lock, backend, reason: "no IndexedDB in this browser" }; } else { data = { disk: null, lock, backend, from: "bundled in the kernel", spec: true }; return [{ kind: "data", template: "bundled", mode: "persistent", backend, size, optional: true }]; } } KW.dataDisk = null; showData(null, 0); return []; } /** Worker mode, once booted: what the worker built (or why not), shown like an inline attach. */ function workerDataAttached(w: WorkerKernel, ms: number) { if (!data?.spec || !data.lock) return; // no spec was sent KW.dataDisk = null; // the disk is in the worker: KW.vm.disks has its state const st = workerData(); const skipped = w.skippedDisks.find((d) => d.index === 0); if (!st) { data.reason = skipped ? `failed: ${skipped.reason}` : "not attached"; showData(null, ms); return; } if (!st.persistent) data.notice = st.notice ?? "not saved"; showData({ size: st.size, seeded: st.seeded, storedBlocks: st.storedBlocks, storedBytes: st.storedBytes, extents: st.extents }, ms); } /** Sync the guest's file systems to the persistent disks (the SDK's sync()). */ KW.sync = (opts) => (vm && !ended() ? vm.sync(opts) : Promise.resolve(true)); /** * Reboot discards this machine, and with it any record of it that is still * saved (held by Pause or in the background, the resumed one before an SMP * resume deleted it, one a given-up save left): the load after Reboot would * otherwise keep it or ask about it. A session kept from before is the * next load's to decide (resumeChoice()). Then the sync. */ KW.beforeReboot = async () => { const own = [holdState?.saved, resumedSaved, strandedSaved].filter((t): t is number => typeof t === "number"); await Promise.all(own.map((t) => clearSession(t).catch(() => {}))); return KW.sync!(); }; document.addEventListener("visibilitychange", () => { // A suspend that starts now seals the disks itself (autosuspend.ts); else sync them. if (document.visibilityState === "hidden") { if (!auto.hidden()) void KW.sync!().then(dataStatus); } else auto.shown(); }); addEventListener("pagehide", () => { void KW.sync!(); }); /** Download the data disk as a .kwsparse.gz file (synced first). */ KW.exportData = async () => { const d = data?.disk; const w = d ? undefined : workerData(); if (w && vm instanceof WorkerKernel) { // Worker mode: the worker reads the disk (memory and store, or the OPFS files) and hands over the parts. if (w.persistent && !(await KW.sync!())) ui.hostLog("[host] data disk: the guest did not confirm a sync; exporting what was flushed"); const { parts, blocks } = await vm.exportDisk(0); const r = await downloadKwsparse(parts, blocks); ui.hostLog(`[host] data disk: exported ${r.blocks} blocks as ${r.name} (${fmtSize(r.bytes)}), from the kernel worker`); return r; } if (!d) throw new Error(`no data disk is attached${data?.reason ? ` (${data.reason})` : ""}`); if (d.persistent && !(await KW.sync!())) ui.hostLog("[host] data disk: the guest did not confirm a sync; exporting what was flushed"); const r = await exportDataDisk(d); ui.hostLog(`[host] data disk: exported ${r.blocks} blocks as ${r.name} (${fmtSize(r.bytes)})`); return r; }; async function takeOverData() { if (data?.lock && !data.lock.held) throw new Error("the data disk is open in another tab; close it first"); if (!data?.lock && !(await acquireDiskLock()).held) throw new Error("the data disk is open in another tab; close it first"); if (cfg.worker) { // The kernel worker writes the store (or holds the OPFS files): it must stop (and close it) before this page touches it. if (!vm && kernelStarting) throw new Error("the kernel worker is still starting (it may be opening the data disk); try again once it runs"); if (vm instanceof WorkerKernel && !ended() && workerData()?.store) await vm.detachDisk(0); return; } await detachDataDisk(data?.disk); // the running guest's later writes are not saved } function reloadPage() { allowLeave(); const u = new URL(location.href); u.searchParams.delete("reset"); location.replace(u); } /** Where import and reset act: the store this load keeps the data disk in. */ const dataBackend = (): DataDiskBackend => data?.backend ?? cfg.datastore; /** Replace the saved data disk with a .kwsparse[.gz] file (File, Blob or bytes), then reload. */ KW.importData = async (file) => { const bytes = file instanceof Uint8Array ? file : new Uint8Array(await file.arrayBuffer()); await takeOverData(); // Any size: the next boot grows it to ?datasize=, if larger. OPFS: in a worker of its own (synchronous handles). const r = dataBackend() === "opfs" ? await runDataJob<{ size: number; blocks: number }>({ op: "import", bytes }, { log: dataLog }) : await importDataDisk(bytes, { log: dataLog }); ui.hostLog(`[host] data disk: imported ${r.blocks} blocks into ${storeName(dataBackend())}; reloading`); reloadPage(); return r; }; /** Forget the saved data disk (the next boot starts from the template), then reload. */ KW.resetData = async () => { await takeOverData(); if (dataBackend() === "opfs") await resetOpfsDataDisk(); else await resetDataDisk(); ui.hostLog(`[host] data disk: reset (${storeName(dataBackend())}); reloading`); reloadPage(); }; // --- terminal ------------------------------------------------------------- // wterm renders the rows as DOM text in #term, which is also the scroll // container, so scrolling (touch momentum, the scrollbar) is the browser's // own. It sizes the grid to the element (autoResize, also when the font // size changes) and onResize passes the size on to the guest tty. const termEl = document.getElementById("term")!; // wterm takes the row height from --term-row-height (it does not follow the font): // setTermFont sets both. Its font probe then refits the grid. setTermFont(termEl, cfg.fontSize); const term = new WTerm(termEl, { ...TERM_OPTIONS, onData: (d) => send(ui.applyCtrl(d)), onBinary: (d) => send(d), onResize: (cols, rows) => vm?.resize(cols, rows), // SIGWINCH }); g.term = term; ctrlCInterrupts(termEl, term); // init() instantiates wterm's parser (a small wasm module inside the package); // output written before then waits here. boot() waits for it before the kernel starts. let early: (string | Uint8Array)[] | null = []; const write = (data: string | Uint8Array) => { if (early) early.push(data); else term.write(data); }; /** * What the last load asked of this one: resume the suspended session, * "hand" (the Resume button) or "auto" (a suspend in the background), or * "fresh" (Reboot: boot fresh); null for a plain load, which resumes a * session it can (offerResume()). */ const resumeRequest = takeResumeRequest(); /** Resume without a question, also on another CPU count (offerResumeCpus()). */ const resumeAsked = resumeAskedFor(resumeRequest); /** * A quiet resume (every resume: after the Resume button, a suspend in the * background, or on a plain load; autosuspend.ts quietStart()): the * top bar's boot indicator says "Resuming", the console only its faint * placeholder, and the host's boot lines are held back (the Kernel log * has them), so the first thing the terminal shows is the screen as it * was (markRunning()). A plain load * starts so until the saved session's meta says whether there is one to * resume (boot()). A load that boots fresh or fails after all shows them * (unquiet()). */ let quietResume = quietStart(resumeRequest, cfg.verbose); let held = ""; const termReady = term.init().then(() => { termEl.querySelector("textarea")?.setAttribute("enterkeyhint", "enter"); const q = early ?? []; early = null; for (const d of q) term.write(d); if (!quietResume) ui.consoleShown(); // the first host lines are on it now if (!cfg.coarse) term.focus(); // not on touch, as everywhere the page focuses by itself: it would raise the on-screen keyboard }); // --- terminals (wterms.ts) ------------------------------------------------- // Terminal 0 is this console; 1 to 9 are terminal sessions over vsock, opened // once user space runs (guestReady) and closed when the kernel stops. let guestUp = false, stopped = false; let readyResolve!: (vm: KernelHandle) => void, readyReject!: (e: Error) => void; const guestReady = new Promise((resolve, reject) => { readyResolve = resolve; readyReject = reject; }); guestReady.catch(() => {}); const checkReady = () => { if (guestUp && vm && !stopped) readyResolve(vm); }; /** The kernel stopped or never started: no more terminals. */ const stopTerminals = (why: string) => { stopped = true; readyReject(new Error(why)); terminals.closeAll(); }; const terminals = createTerminals({ term0: term, send0: send, ready: () => guestReady, alive: () => !stopped && !ended() && !isStopped(ui$().get()), autoFocus: !cfg.coarse, }); KW.windows = terminals; // "windows": the name the browser tests use KW.ready = () => guestReady; // ?showcase: the scripted tour of the demos (launcher.ts). void guestReady.then(showcaseFromUrl, () => {}); // Fresh apk indexes, fetched by the page over Wisp and pushed in once user space runs (apk.ts). if (cfg.net && alpine) void guestReady.then((vm) => prefetchApkIndexes(vm, { servers: cfg.wisp, log: (m) => ui.log(m, "h") }), () => {}); // What the base booted (/run/kw-base: the image id, the upper layer), for the // Environment rows, About and diagnostics. void guestReady.then(showBase, () => {}); KW.preview = { open: openPreview, close: closePreview, url: previewUrl }; // Paste, the key bar, the touch controls and the shortcuts act on the current window. // The console minimizes itself once the system runs if nobody used it (autohide.ts); these count as use. autoHide.init(); KW.term = { focus: () => { autoHide.used(); terminals.focus(); }, paste: (t) => { autoHide.used(); terminals.sendCurrent(pasteText(terminals.currentTerm(), t)); }, // the key bar's Paste send: (d) => { autoHide.used(); terminals.sendCurrent(d); }, appCursor: () => !!terminals.currentTerm().bridge?.cursorKeysApp(), setFontSize: (px) => { setTermFont(termEl, px); terminals.setFont(px); }, notice: (m) => { autoHide.needed(); write(`\r\n\x1b[1;31m${m.replace(/\n/g, "\r\n")}\x1b[0m\r\n`); }, // the console's (shown again if it hid itself) }; KW.screenText = () => termText(term); // --- host boot lines, systemd style ------------------------------------------- // "[ OK ] Downloaded ..." for each host step, and one transient line // (download progress, "Compiling") redrawn in place below them. let live = ""; function hostLine(line: string) { if (quietResume) held += `${line}\r\n`; else write(`\r\x1b[K${line}\r\n${live}`); } /** A quiet resume that boots fresh or fails after all: show the boot as usual from here on. */ function unquiet() { if (!quietResume) return; quietResume = false; ui.bootResume(false); ui.consoleShown(); write(held + live); held = ""; } const took = (ms: number | null | undefined) => (ms != null ? ` ${SGR.dim}(${fmtS(ms)})${SGR.off}` : ""); const okLine = (msg: string, ms?: number | null) => `[${SGR.ok} OK ${SGR.off}] ${msg}${took(ms)}`; function status(t: string, color: string, msg: string, ms?: number | null) { if (ui.state() === "error") return; // e.g. a parallel download finishing after a failure hostLine(`[${color}${t}${SGR.off}] ${msg}${took(ms)}`); ui.hostLog(`[boot] ${t.trim()} ${msg}${ms != null ? ` (${fmtS(ms)})` : ""}`); } function ok(msg: string, ms?: number | null) { status(" OK ", SGR.ok, msg, ms); } function warn(msg: string, ms?: number | null) { status(" WARN ", SGR.warn, msg, ms); } function failed(msg: string) { setLive(null); status("FAILED", SGR.fail, msg); } /** * Show (or with null, remove) the transient line: tag (6 characters), msg, * then detail in gray. It is cut to the terminal's width, so it never wraps. */ function setLive(t: string | null, msg = "", detail = "") { if (!t && !live) return; if (!t) { live = ""; if (!quietResume) write("\r\x1b[K"); return; } const max = Math.max(1, term.cols - 9); let a = ` ${msg}`, b = detail ? ` ${detail}` : ""; if (a.length >= max) { a = `${a.slice(0, max - 1)}…`; b = ""; } else if (a.length + b.length > max) b = `${b.slice(0, max - 1 - a.length)}…`; live = `[${SGR.busy}${t}${SGR.off}]${a}${b && `${SGR.gray}${b}${SGR.off}`}`; if (!quietResume) write(`\r\x1b[K${live}`); } hostLine(`${SGR.bold}unkernel${SGR.off} ${SGR.dim}· Linux on WebAssembly${SGR.off}\r\n`); ok("Checked browser features", ui.stageTime("preflight")); // --- console output ------------------------------------------------------------ // The SDK's ConsoleRouter splits the console into kernel lines and user // output. Every kernel line goes to the Kernel log tab. Boot phase: the // newest kernel line shows, in gray, after "Starting Linux" on the transient // line (cut to the terminal's width); IMPORTANT ones (warning, oops, // panic, ...) are written in full, and so is every kernel line after the // first of those, for its context. When user space prints its first line // (or a prompt), "Started Linux" replaces the transient line, followed by a // timing summary and the user-space output so far (buffered until then). // From then on only IMPORTANT kernel lines reach the terminal. // ?verbose=1 stays in the boot phase and writes every kernel line, in gray. let sawOutput = false, running = false, loud = false; let phase: "boot" | "user" = "boot"; let replay = ""; // user-space output during the boot phase, redrawn below the summary // Each step already shows its own time: the summary is the total from page load. const summary = () => `${SGR.dim}Booted in${SGR.off} ${SGR.bold}${fmtS(Date.now() - KW.t0)}${SGR.off}` + (smp ? ` ${SGR.dim}on ${cfg.cpus} CPUs${SGR.off}` : cfg.worker ? ` ${SGR.dim}in a Web Worker${SGR.off}` : "") + "\r\n"; // Two lines under the summary of a fresh boot: the source (an OSC 8 link, // which wterm opens on Ctrl/⌘-click) and how to open the app launcher. const link = (url: string, text: string) => `\x1b]8;;${url}\x1b\\${text}\x1b]8;;\x1b\\`; const notes = () => [`${SGR.dim}Source${SGR.off} ${link(REPO, REPO.replace(/^https:\/\//, ""))}`, `${SGR.dim}Apps${SGR.off} ${cfg.coarse ? "tap the grid in the Dock" : `press ${SGR.bold}${APPLE ? "⌘ K" : `${CHORD}-O`}${SGR.off}`}`]; // Pip, glimmer's seed and the logo (assets/logo.svg), left of the summary // and the notes; left out where the lines would wrap. const PIP_W = PIP.width + 2; // with the gap before the text const visible = (s: string) => s.replace(/\x1b\]8;;[^\x1b]*\x1b\\/g, "").replace(/\x1b\[[0-9;]*m/g, "").length; const intro = () => { const lines = [summary().replace(/\r\n$/, ""), ...notes()]; if (term.cols < PIP_W + Math.max(...lines.map(visible)) + 1) return lines.join("\r\n") + "\r\n"; return ["", ...lines].map((s, i) => `${PIP[i]} ${s}`).join("\r\n") + "\r\n"; }; // The command line is shown only when it is not the default one. const cmdlineNote = () => (cfg.cmdlineGiven ? `: ${SGR.dim}${cfg.cmdline}${SGR.off}` : ""); let replayTimer: ReturnType | undefined; /** Set when the kernel resumed a suspended session: what markRunning says instead of "Started Linux", and the old screen. */ let resumed: { note: string; saved: number; screen?: string; preview?: SessionMeta["preview"]; windows?: SessionMeta["windows"]; win?: number } | null = null; function markRunning(why: string) { if (running) return; running = true; guestUp = true; checkReady(); clearTimeout(replayTimer); if (ui.state() === "error") { unquiet(); if (phase === "boot" && !cfg.verbose) { phase = "user"; setLive(null); write(replay); replay = ""; } return; } ui.stage("boot", "done"); ui.stage("userspace", "done", why); ui.setState("running"); ui.bootDone(); // the boot indicator fades out as what is below is written, in this same task if (!resumed) autoHide.arm(); // a resumed session's windows stay as they were if (phase === "boot" && !cfg.verbose) { phase = "user"; setLive(null); // "Starting Linux", replaced by its result if (resumed) { // Quiet: only the screen as it was, then the boot indicator goes (the toast says it: sayResumed()). // Else (the boot lines showed after all: a plain load whose session took long to read) a line too. if (quietResume) { quietResume = false; held = ""; } else write(okLine(resumed.note) + "\r\n\r\n" + summary() + "\r\n"); if (resumed.screen?.length) write(resumed.screen); // the console as it was (the guest does not redraw it) write(replay); } else { unquiet(); // not the resume asked for (a fresh boot): its lines after all // Two steps: the kernel until it runs /init, then the base mounting the // distro image (its first blocks come over HTTP) and the distro's init. const user = cfg.distro ? capitalize(cfg.guest) : "the base shell"; write(okLine(`Started Linux${cmdlineNote()}`, ui.stageTime("boot")) + "\r\n" + okLine(`Started ${user}`, ui.stageTime("userspace")) + "\r\n\r\n" + intro() + "\r\n" + replay); } replay = ""; } auto.update(); // a tab hidden while it booted } /** What the console showed of the guest, saved by Suspend (session.ts). */ const screenTail = new ScreenTail(); /** Text the terminal shows once user space runs (kept for the redraw until then). */ function show(t: string) { screenTail.push(t); autoHide.output(); if (phase === "user") write(t); else if (!cfg.verbose && (replay += t).length > 65536) replay = replay.slice(-49152); } const KSTAMP_RE = /^\[ *\d+\.\d+\] */; // The base's check of the root with the data disk's changes failed, so this boot // runs the plain image (safe mode), or it found no distro to boot and started its // base shell (guest/base/initramfs/init). let userTail = "", safeSeen = false, baseSeen = false; const plain = (s: string) => s.replace(/\x1b\[[0-9;]*m/g, "").trim(); function watchUser(t: string) { if (safeSeen && baseSeen) return; userTail = (userTail + t).slice(-2048); const rs = baseSeen ? null : /unkernel base shell \([^)\r\n]*\): ([^\r\n]*)\r?\n/.exec(userTail); if (rs) { baseSeen = true; const reason = plain(rs[1]!); ui.hostLog(`[host] the base started its base shell: ${reason}`); ui.baseShell(reason); autoHide.needed(); } const m = safeSeen ? null : /kw-overlay: SAFE MODE: ([^\r\n]*)\r?\n/.exec(userTail); if (!m) return; safeSeen = true; const reason = plain(m[1]!); ui.hostLog(`[host] the guest booted in safe mode: ${reason}`); ui.safeMode(reason); autoHide.needed(); } /** A whole line during the boot phase, above the transient line. */ const bootLine = (s: string) => { if (quietResume) held += s; else write(live ? `\r\x1b[K${s}${live}` : s); }; const router = new ConsoleRouter({ onKernelLine(line, info) { ui.log(line); if (/Run \S+ as init process/.test(line)) { ui.stage("boot", "done"); ui.stage("userspace", "active", line.replace(KSTAMP_RE, "").trim()); } if (phase === "boot") { if (info.important) { loud = true; bootLine(info.raw); } else if (cfg.verbose || loud) bootLine(`${SGR.gray}${line}${SGR.off}\r\n`); else setLive(" .... ", "Starting Linux", line.replace(KSTAMP_RE, "")); } else if (info.important) show(info.raw); }, onUserOutput(t) { watchUser(t); if (cfg.verbose) { setLive(null); screenTail.push(t); write(t); return; } // Boot phase: drawn once, below the summary (markRunning). The router marks // user space on a whole line or a prompt; anything else shows after 300 ms. show(t); if (phase === "boot") replayTimer ??= setTimeout(() => markRunning("first output"), 300); }, onUserspace(why) { if (why.startsWith("first output: ")) ui.log(why.slice(14)); markRunning(why); }, }); function consoleWrite(bytes: Uint8Array) { sawOutput = true; router.write(bytes); } // --- download (the SDK's loader reports it) ----------------------------------- let t0Download = 0, liveT = 0; function onDownload(d: DownloadProgress) { if (!t0Download) { t0Download = performance.now(); ui.stage("kernel", "active", d.url); } ui.download(d.loaded, d.total); // the boot indicator's ring if (d.done) { const ms = performance.now() - t0Download; const rate = `${(d.loaded / 1048576 / Math.max(ms / 1000, 0.001)).toFixed(1)} MiB/s`; ui.stage("kernel", "done", `${d.url} · ${fmtMiB(d.loaded)} · ${rate}`); ok(`Downloaded ${smp ? "unkernel-smp.wasm" : "unkernel.wasm"}, ${fmtMiB(d.loaded)}`, ms); // The kernel compiles while it downloads (compileStreaming); what is left shows as the compile step. ui.stage("compile", "active"); ui.setState("booting", "Compiling"); setLive(" .... ", `Compiling the kernel (${fmtMiB(d.loaded)} of WebAssembly)`); } else if (performance.now() - liveT >= 100) { liveT = performance.now(); setLive(" .... ", `Downloading the kernel, ${fmtMiB(d.loaded)}`); } } // --- test hooks: fake an allocation failure -------------------------------- if (cfg.simulate.includes("oom") || cfg.simulate.includes("lowmem")) { const Real = WebAssembly.Memory; const limitPages = cfg.simulate.includes("oom") ? 0 : 2048; // lowmem: nothing above 128 MiB const Fake = function Memory(desc: WebAssembly.MemoryDescriptor) { if (Number(desc?.initial) > limitPages) throw new RangeError(`WebAssembly.Memory(): could not allocate memory (simulated by ?simulate=${cfg.simulate.join(",")})`); return new Real(desc); } as unknown as typeof WebAssembly.Memory; Fake.prototype = Real.prototype; WebAssembly.Memory = Fake; } // --- the suspended session (session.ts) ------------------------------------------ /** * kwshm, for the Wayland desktop (wayland.ts): on unless ?shm=0, and for the * resume of a session the session's (adoptSessionGl()), since a restore * needs the snapshot's devices. */ let shm = cfg.shm; let shmOwn: boolean | null = null; /** The camera (kwcam): on unless ?cam=0, and for a resume the session's, as kwshm. */ let cam = cfg.cam; let camOwn: boolean | null = null; const sessionDevices = (): SessionDevices => ({ display: cfg.display, sound: cfg.sound, mic: cfg.mic, cam, net: cfg.net, data: cfg.data, gl: cfg.gl, gpu: cfg.gpu, shm }); /** * The devices a suspended session is matched against: this load's, except * that guest GL and its virtio-gpu device follow the session's (it resumes * with its own; adoptSessionGl()) unless this load's URL names ?gl= or * ?gpu=, or the session had GL and this load cannot (no WebGL2, no * renderer, another distro: the distro is matched anyway). So a session * saved before GL was on by default (no gl, no gpu) still resumes, as it * was; a fresh boot uses the options. kwshm and the camera always follow * the session's (a session saved before the camera resumes without one; * nothing asks for the user's camera until a program streams anyway). */ const sessionDevicesFor = (m: SessionMeta): SessionDevices => { const d = { ...sessionDevices(), shm: !!m.devices.shm, cam: !!m.devices.cam }; if (cfg.glFromUrl) return d; const gl = !!m.devices.gl, gpu = !!m.devices.gpu; if (gl && (cfg.glBlocked || !GL_DISTROS.includes(cfg.guest))) return d; return { ...d, gl, gpu }; }; /** This load's own GL devices, while a resume runs with the session's (a failed resume boots fresh with these). */ let glOwn: { gl: boolean; gpu: boolean; why: string | null } | null = null; /** Resume `m` with its own guest GL devices and kwshm (sessionDevicesFor()). */ const adoptSessionGl = (m: SessionMeta) => { const d = sessionDevicesFor(m); if (!!d.shm !== shm) { shmOwn = shm; shm = !!d.shm; ui.hostLog(`[host] suspended session: resumed ${shm ? "with" : "without"} kwshm (the Wayland desktop), as it was saved; a fresh boot ${cfg.shm ? "has" : "has not"} it`); } if (!!d.cam !== cam) { camOwn = cam; cam = !!d.cam; ui.hostLog(`[host] suspended session: resumed ${cam ? "with" : "without"} the camera, as it was saved; a fresh boot ${cfg.cam ? "has" : "has not"} it`); } if (!!d.gl === cfg.gl && !!d.gpu === cfg.gpu) return; glOwn = { gl: cfg.gl, gpu: cfg.gpu, why: cfg.glWhy }; cfg.gl = !!d.gl; cfg.gpu = !!d.gpu; cfg.glWhy = cfg.gl ? null : "the resumed session was saved without it; a fresh boot (Reboot) has it"; ui.hostLog(`[host] suspended session: resumed with guest GL ${cfg.gl ? `on${cfg.gpu ? "" : ", without virtio-gpu"}` : "off"}, as it was saved; a fresh boot uses the options`); }; /** The resume failed: the fresh boot gets this load's own GL devices (its command line has them). */ const dropSessionGl = () => { if (shmOwn !== null) { shm = shmOwn; shmOwn = null; } if (camOwn !== null) { cam = camOwn; camOwn = null; } if (!glOwn) return; cfg.gl = glOwn.gl; cfg.gpu = glOwn.gpu; cfg.glWhy = glOwn.why; glOwn = null; }; /** * Resume the saved session, if this page can: the same kernel and CPU * count (another count: offerResumeCpus(), before), the same devices and * (inline, where the data disk is open here) the data disk unchanged * since; in worker mode the worker checks the disk, and a refusal falls * back to a fresh boot. A plain load resumes it without a question (as * every resume, it ends in sayResumed()'s toast); the load after Reboot boots * fresh, keeping it without a data disk and asking with one * (resumeChoice()). null: boot fresh. */ async function offerResume(info: Promise, disks: BlockDevice[] | DiskSpec[]): Promise<{ snap: KernelSnapshot; meta: SessionMeta } | null> { let meta: SessionMeta | null; try { meta = await info; } catch (e) { ui.hostLog(`[host] suspended session: cannot read it: ${(e as Error)?.message ?? e}`); return null; } if (!meta || sessionCpus(meta) !== cfg.cpus) return null; const label = `saved ${agoSince(meta.saved)}, ${fmtMiB(meta.bytes)}`; // Inline: the disks as the snapshot lists them, the distro image first. const inlineDisks = !cfg.worker ? [...(distroDisk ? [distroDisk] : []), ...disks as BlockDevice[]] : null; // Its data disk open in another tab: not resumed here, and kept (it is that tab's to resume). const elsewhere = otherTab && meta.devices.data ? "the data disk is open in another tab" : null; // A data disk that changed since can never resume it: forget it then. const stale = !elsewhere && inlineDisks ? snapshotDiskMismatch(meta.disks, inlineDisks) : null; const why = sessionMismatch(meta, kernelUrl, sessionDevicesFor(meta), distroRef) ?? elsewhere ?? stale; if (why) { ui.check("session", "Suspended session", "info", `${label}; not resumed`, why); ui.hostLog(`[host] suspended session (${label}) not offered: ${why}`); if (stale) await clearSession().catch(() => {}); return null; } const choice = resumeChoice(resumeRequest, meta.devices.data); if (choice === "keep") { // After Reboot, without a data disk: nothing changes it, so it stays for the next load. ui.hostLog(`[host] suspended session (${label}): booting fresh after Reboot; it is kept, and the next load resumes it`); ui.check("session", "Suspended session", "info", `${label}; kept`, "booted fresh after Reboot; the next load resumes it"); return null; } const on = smp ? `, ${cpusText(cfg.cpus)}` : ""; if (choice === "ask" && !await ui.ask({ title: "resume", msg: `Resume the session suspended ${agoSince(meta.saved)} (${fmtMiB(meta.bytes)}, ${meta.memoryMiB} MiB of memory${on})? Boot fresh discards it: a fresh boot changes the data disk.`, ok: "Resume", cancel: "Boot fresh" })) { ui.hostLog(`[host] suspended session (${label}): booting fresh instead, discarded`); await clearSession().catch(() => {}); return null; } try { const snap = await loadSession(); adoptSessionGl(meta); return { snap, meta }; } catch (e) { warn(`Could not read the suspended session: ${(e as Error)?.message ?? e}`); await clearSession().catch(() => {}); return null; } } /** * The preview's Browser window open at the suspend, opened again on the resumed * machine once its port answers: the guest's server and its listening * socket are in the snapshot, while the old page's connections were reset * and the reload gave this kernel a new vmId (preview.ts), so the old * iframe could not have been kept. */ function reopenPreview(handle: KernelHandle, p: SessionMeta["preview"]) { if (!p || !previewSupported() || previewState()) return; waitPort(handle, p.port, 15000).then(() => { if (ended() || previewState()) return; openPreview(p.port, p.path ?? "/", { show: p.shown }); ui.hostLog(`[host] preview: port ${p.port} opened again after the resume`); }, (e: Error) => { ui.hostLog(`[host] preview: not opened again (${e.message})`); ui.toast(`The preview's port ${p.port} did not answer after the resume: ${PREFIX} ${keyFor("preview")} opens it again`); }); } /** * A snapshot the SDK refused, in words the user can act on where the * cause is known: a program holding the virtio-gpu device (guest GL: a GL * program's contexts and resources live in this page's WebGL2, which a * session cannot save; a KMS client's buffers), "virtio-gpu resources, * contexts or commands in flight" (js.md, virtio and virtio-gpu). */ function refusal(e: unknown): unknown { const m = (e as Error)?.message ?? ""; if (!/virtio-gpu (resources|contexts|commands)/.test(m)) return e; return new Error("a program is using the GPU (/dev/dri: a GL program, or a KMS client such as X, kmscube or glmark2), whose state lives in this page (its WebGL2 context and buffers) and cannot be saved in a session. Quit it and suspend again", { cause: e }); } /** A record a given-up save left behind (its removal failed): this machine's, so a later suspend may replace it. */ let strandedSaved: number | null = null; /** The Suspend in progress: settles with null once saved, or why it failed (the exit waits for it). */ let suspending: Promise | null = null; /** The size of the session the last Suspend saved (the Resume card says it). */ let suspendedBytes = 0; /** * How long the session's write may take while the kernel waits at its * snapshot point (awake time: a page frozen in the background does not * use it up); past it the machine runs on and the write is aborted. */ const SUSPEND_COMMIT_MS = 20000; /** * A suspend in the background that holds the machine (snapshot({ commit, * hold })): its session is saved (`saved`: SessionMeta.saved) and the * kernel waits at its snapshot point until `decide` says "continue" (the * tab shown again: continueHeld()) or "stop" (stopHeld()). `lost`: the * kernel ended meanwhile (its worker died, the page stopped it). */ let holdState: { saved: number; since: number; bytes: number; decide: (how: SnapshotHoldDecision) => void; lost: boolean } | null = null; /** * Suspend: save the running machine in this browser and stop it * (session.ts); the next load resumes it, on as many CPUs. The * kernel waits at its snapshot point while the session is written * (snapshot({ commit })) and stops only once that transaction committed; * if it does not, the machine runs on (SuspendSaveError) and the saved * session stays as it was. `hold` (a suspend in the background): once * saved, the kernel stays held there instead of stopping, and this * resolves with `held` (continueHeld() and stopHeld() end the hold); * kw-resume is set as soon as the session is stored, so a page the * browser kills meanwhile resumes it on its next load. Asked for while * held, Suspend turns the hold into the stop. */ KW.suspend = async ({ hold = false } = {}) => { if (holdState) { const h = holdState; await stopHeld(); return { bytes: h.bytes, ms: 0 }; } if (!vm || ended() || !running) throw new Error("the system is not running"); if (suspending) throw new Error("a suspend is in progress"); if (typeof indexedDB === "undefined") throw new Error("this browser has no IndexedDB to keep the session in"); // Its session would replace the one the tab with the disk may keep, and would claim a disk it never had. if (otherTab) throw new Error("this tab runs without the data disk, which another tab has: it cannot be suspended"); // The SMP kernel on one CPU (the worker could not start their threads): no load would boot that again. if (smp && vm.stats().cpus !== cfg.cpus) throw new Error(`the SMP kernel runs on ${vm.stats().cpus} of ${cfg.cpus} CPUs here (their threads did not start); set CPUs to 1 under Options to suspend`); const t0 = performance.now(); const target = vm; ui.setState("running", "Suspending", "saving"); let tSnap = 0, tStored = 0, bytes = 0, saved = 0, wrote = false; let nowHeld!: () => void; const holding = new Promise((r) => { nowHeld = r; }); const p = target.snapshot({ commitTimeoutMs: SUSPEND_COMMIT_MS, commit: async (snap, signal) => { tSnap = performance.now() - t0; saved = Date.now(); bytes = await saveSession(snap, { saved, kernelUrl, devices: sessionDevices(), screen: screenTail.text(), worker: cfg.worker, distro: distroRef, preview: previewState(), // The terminals' sessions, detached by the restore (wterms.ts), and the current terminal (the record's names stay: saved sessions load). windows: terminals.saved(snap.ttys ?? []), win: currentTerminal(), }, signal); wrote = true; tStored = performance.now() - t0; // Committed only after the snapshot was given up (the timeout): the machine runs on past it. if (signal.aborted) { await clearSession(saved).catch(() => { strandedSaved = saved; }); throw signal.reason; } // Stored: a page killed while held resumes it on its next load, quietly. if (hold) setResumeRequest(true); }, ...(hold ? { hold: (_snap: KernelSnapshot, signal: AbortSignal) => new Promise((decide) => { const h = { saved, since: Date.now(), bytes, decide, lost: false }; holdState = h; signal.addEventListener("abort", () => { h.lost = true; }, { once: true }); ui.setState("running", "Paused", "held"); // before the page may be frozen (the tab's title follows) nowHeld(); }), } : {}), }); suspending = p.then(() => null, (e: unknown) => (e as Error)?.message ?? String(e)); try { const how = await Promise.race([p.then(() => "stopped" as const), holding.then(() => "held" as const)]); if (how === "held") { ui.hostLog(`[host] ${autoSuspending ? "suspended in the background" : "paused"}: ${fmtMiB(bytes)} saved in this browser (parked and copied in ${fmtS(tSnap)}, stored in ${fmtS(tStored - tSnap)}); held at its snapshot point ${autoSuspending ? "while the tab is hidden" : "until Continue"}`); return { bytes, ms: performance.now() - t0, held: true }; } suspendedBytes = bytes; ui.hostLog(`[host] suspended${autoSuspending ? " in the background" : ""}: ${fmtMiB(bytes)} saved in this browser (parked and copied in ${fmtS(tSnap)}, stored in ${fmtS(tStored - tSnap)}, stopped after ${fmtS(performance.now() - t0)})`); return { bytes, ms: performance.now() - t0 }; } catch (e) { // Written, but given up after all (the commit's timeout came first): the machine runs on past it, so it is not this machine's. if (wrote && e instanceof SnapshotCommitError) await clearSession(saved).catch(() => { strandedSaved = saved; }); if (ended()) throw e; // Refused before the copy (a task did not park, I/O in flight), or not stored: it runs on. suspending = null; ui.setState("running", "Running"); if (!(e instanceof SnapshotCommitError)) throw refusal(e); const why = ((e.cause as Error | undefined)?.message ?? /not committed \((.*)\);/.exec(e.message)?.[1] ?? e.message).replace(/^snapshot: /, ""); ui.hostLog(`[host] suspend: the session was not saved (${why}) after ${fmtS(performance.now() - t0)}; the system runs on`); throw new SuspendSaveError(`the session was not saved (${why}); the system runs on`); } }; /** * The tab is shown again while held: the machine goes on where it was * (no reload: terminal windows, connections to the guest and the preview * as they were), and its saved session is deleted, since the guest writes * its disks again. kw-resume is cleared first, in the same task as the * release, so a later reload does not resume that session; the record * goes after the release (a page killed in between leaves a stale record, * which the next load resumes as a plain load does, with its toast, and * refuses once the data disk changed, as a record a given-up save could * not delete). False if the * machine is gone: its saved session resumes instead. */ async function continueHeld(byHand = false): Promise { const h = holdState; if (!h || h.lost || ended()) return false; clearResumeRequest(); h.decide("continue"); await suspending; // settles once it went on (or the kernel ended) if (h.lost || ended()) { setResumeRequest(true); return false; } holdState = null; suspending = null; ui.setState("running", "Running"); const sec = Math.round((Date.now() - h.since) / 1000); const was = sec < 120 ? `${sec} s` : sec < 7200 ? `${Math.round(sec / 60)} min` : `${Math.round(sec / 3600)} h`; if (byHand) ui.hostLog(`[host] paused for ${was}, continued; its saved session is deleted`); else { ui.hostLog(`[host] auto-suspend: held in the background for ${was}, continued; its saved session is deleted`); ui.check("autosuspend", "Suspend in the background", "pass", `held for ${was}, continued ${new Date().toLocaleTimeString()}`, "the machine went on where it was; the session saved for it was deleted"); } strandedSaved = h.saved; // this machine's own until it is gone void clearSession(h.saved).then(() => { if (strandedSaved === h.saved) strandedSaved = null; }, (e: unknown) => ui.hostLog(`[host] ${byHand ? "pause" : "auto-suspend"}: deleting the held session failed (${(e as Error)?.message ?? e}); a later suspend replaces it`)); return true; } /** The hold becomes the stop (its session stays saved; the exit then shows it suspended). */ async function stopHeld(): Promise { const h = holdState; if (!h) return; h.decide("stop"); await suspending; } // --- suspend in the background, and the question on leaving (autosuspend.ts) ------------ /** * The user has used this machine (typed, tapped or clicked outside the * page's chrome: the system apps, the bars, the question; focus.ts) since it * booted, or it is a resumed session: worth suspending or asking about. */ let interacted = false; const used = (e: Event) => { if (interacted || !e.isTrusted) return; if (isPageChrome(e.target)) return; interacted = true; }; for (const ev of ["keydown", "pointerdown", "input", "paste"]) addEventListener(ev, used, { capture: true, passive: true }); /** The resumed session's time (SessionMeta.saved): a session still saved as that one is this machine's, not one kept from before (Reboot keeps one without a data disk). */ let resumedSaved: number | null = null; /** The suspend running now was started by AutoSuspend. */ let autoSuspending = false; const facts = (): SuspendFacts => ({ running: running && !!vm && !ended() && isRunning(ui$().get()) && !suspending, suspending: !!suspending && !ended(), held: !!holdState && !ended(), interacted, hidden: document.visibilityState === "hidden", }); const autoWhat = cfg.autosuspend === null ? "off" : `${autoSuspendLabel(cfg.autosuspendParam).toLowerCase()} when the tab is hidden`; ui.check("autosuspend", "Suspend in the background", "info", autoWhat, cfg.autosuspend === null ? "Options, Suspend in the background" : "a system used since it booted; it resumes when the tab is shown"); /** Not suspended while hidden: said once the tab is shown. */ let autoNote: string | null = null; const auto = new AutoSuspend({ delayS: cfg.autosuspend, facts, async suspend() { // What KW.suspend() refuses for good (it says why when used by hand). if (typeof indexedDB === "undefined") throw new AutoSuspendSkip("this browser has no IndexedDB to keep the session in"); if (otherTab) throw new AutoSuspendSkip("this tab runs without the data disk, which another tab has"); if (smp && vm && vm.stats().cpus !== cfg.cpus) throw new AutoSuspendSkip(`the SMP kernel runs on ${vm.stats().cpus} of ${cfg.cpus} CPUs here`); // Both at once: a hidden page may have only seconds to run. const [kept, est] = await Promise.all([sessionInfo().catch(() => null), navigator.storage?.estimate?.().catch(() => undefined)]); // A session kept from before (the load after Reboot keeps one) would be replaced: only Suspend by hand does that. // Not this machine's own record (resumed from, or left by a save given up whose removal failed). if (kept && kept.saved !== resumedSaved && kept.saved !== strandedSaved) throw new AutoSuspendSkip(`the session suspended ${agoSince(kept.saved)} is kept, and this would replace it (Suspend by hand to replace it)`); // A save that cannot fit would only fail (the machine runs on), after parking it and writing for nothing. const room = sessionRoom(cfg.cpus); if (est?.quota && est.usage !== undefined && est.quota - est.usage < room) throw new AutoSuspendSkip(`the browser's storage has ${fmtMiB(est.quota - est.usage)} free, less than the ${fmtMiB(room)} a suspended session may need`); if (document.visibilityState !== "hidden") throw new AutoSuspendSkip("the tab is shown again", true); autoSuspending = true; let r: Awaited>>; try { r = await KW.suspend!({ hold: true }); } finally { autoSuspending = false; } setResumeRequest(true); // a reload after the browser discarded the tab resumes it quietly ui.check("autosuspend", "Suspend in the background", "pass", `${r.held ? "saved and held" : "suspended"} ${new Date().toLocaleTimeString()}`, r.held ? "it goes on when the tab is shown (if the browser discards the page meanwhile, the next load resumes it)" : "it resumes when the tab is shown"); return r.held ? "held" : "stopped"; }, continue: () => continueHeld(), stopHeld, holdMs: AUTO_SUSPEND_HOLD_MS, resume: () => { ui.toast("Resuming…"); ui.resumeAuto(); // the load restores the screen quietly }, log: (m) => ui.hostLog(`[host] auto-suspend: ${m}`), failed: (m) => { ui.check("autosuspend", "Suspend in the background", "warn", "not suspended", m); if (document.visibilityState === "hidden") { autoNote = m; void KW.sync!().then(dataStatus); // what hiding the tab does otherwise } else ui.toast(m); }, setTimeout: (fn, ms) => setTimeout(fn, ms), clearTimeout: (t) => clearTimeout(t as ReturnType), }); /** The resumed session's time while the tab is hidden: its toast shows once the tab is shown, once. */ let resumedPending: number | null = null; /** * Every resume of a saved session (a reload: the Resume button, a plain * load, after a suspend in the background, on another CPU count) says so * the same way, once the machine is back: this toast (informational: Reboot * is the way to a fresh boot). A hidden tab gets it when shown. */ function sayResumed(saved: number) { if (document.visibilityState === "hidden") { resumedPending = saved; return; } resumedPending = null; ui.toast(resumedNote(agoSince(saved)), { ms: RESUMED_TOAST_MS }); } document.addEventListener("visibilitychange", () => { if (document.visibilityState !== "visible") return; const note = autoNote; autoNote = null; if (resumedPending !== null) { sayResumed(resumedPending); // A note from the background too (rare): after the resume's toast, not over it. if (note) setTimeout(() => ui.toast(note), RESUMED_TOAST_MS); } else if (note) ui.toast(note); }); // Pause by hand (autosuspend.ts Pause): the same save and hold, with the tab shown, until Continue // (the notice's button, or a key for the guest); visibilitychange does not end it. const pause = new Pause({ async suspend() { const r = await KW.suspend!({ hold: true }); // kw-resume "auto" once stored: a closed tab resumes it quietly return r.held ? "held" : "stopped"; }, continue: () => continueHeld(true), stopHeld, holdMs: AUTO_SUSPEND_HOLD_MS, notice: (on) => ui.setPaused(on), log: (m) => ui.hostLog(`[host] pause: ${m}`), failed: (m) => { const why = /^the session was not saved \((.*)\); the system runs on$/.exec(m)?.[1] ?? m.replace(/; the system runs on$/, ""); ui.toast(`Not saved: ${why}; the system runs on`, { ms: 8000 }); }, setTimeout: (fn, ms) => setTimeout(fn, ms), clearTimeout: (t) => clearTimeout(t as ReturnType), }); KW.pause = () => pause.pause(); KW.continuePaused = () => pause.continue(); /** The toast that offers Pause after the user stayed on the page, while it shows. */ let offer: number | null = null; guestKey = () => { if (offer !== null) { ui.toastHide(offer); offer = null; } if (pause.state === "paused") pause.continue(); }; // The question on leaving (the browser's own text, after the user interacted; mostly not on phones). // Nothing can be saved from inside it, and a user who stays may simply go on working, so nothing // happens by itself: a small toast offers Pause (save and hold; the tab can then be closed). let leaveTimer: ReturnType | undefined; let leaveFocus: (() => void) | null = null; const leaveStop = () => { clearTimeout(leaveTimer); if (leaveFocus) removeEventListener("focus", leaveFocus); leaveFocus = null; }; addEventListener("beforeunload", (e) => { if (!wantLeavePrompt(facts(), { enabled: cfg.askLeave, leaving: pageLeaving() })) return; e.preventDefault(); e.returnValue = ""; // engines before the preventDefault() form leaveStop(); // Timers wait while Chrome and Safari ask; Firefox asks from its parent process, so the page also // waits to have the focus back (the question has it). This runs once the user stayed (or, briefly, // while a navigation commits). leaveTimer = setTimeout(() => { const stayed = () => { leaveStop(); leaveStayed(); }; if (document.hasFocus()) stayed(); else { leaveFocus = stayed; addEventListener("focus", stayed); } }, LEAVE_STAYED_MS); }); /** The user stayed on the page: offer Pause, for a moment; doing nothing keeps it running. */ function leaveStayed() { if (!facts().running || pause.state !== "idle") return; // stopped, or a suspend already runs offer = ui.toast("Pause to save this session before closing?", { action: "Pause", ms: LEAVE_OFFER_MS, run: () => { offer = null; if (facts().running) pause.pause(); // else it stopped or a suspend runs meanwhile if (!cfg.coarse) KW.term?.focus(); }, }); } // Left after all (or into the back-forward cache): nothing more to do. addEventListener("pagehide", leaveStop); // --- several CPUs: what falls back to one --------------------------------------- /** * Reload this tab once on one CPU (config.ts reads UP_ONCE_KEY; ?cpus= stays * in the URL, so the next Reboot tries again), optionally resuming the * suspended session. False if sessionStorage refuses the note, which would * make the reload try the same again: then the caller reports the failure. */ function bootUpOnce(reason: string, resume = false, inline = false): boolean { try { sessionStorage.setItem(inline ? INLINE_ONCE_KEY : UP_ONCE_KEY, reason); } catch { return false; } const what = inline ? "on the page's thread" : "on one CPU"; ui.hostLog(`[host] booting ${what} instead: ${reason}`); KW.term?.notice(`[host] booting ${what} instead: ${reason}`); if (resume) { if (resumeRequest === "auto") ui.resumeAuto(); else ui.resume(); } // sets kw-resume and reloads else { allowLeave(); location.reload(); } return true; } const cpusText = (n: number) => plural(n, "CPU"); /** The page's kernel for `n` CPUs (the SMP one is null without the bundle's patch). */ const kernelFor = (n: number) => (n > 1 ? cfg.kernelSmpUrl : cfg.kernelUrl); /** Why this page cannot start `n` CPUs now, or null (config.ts: smpBlocker(), this tab's fallback to one CPU, the host's threads). */ const cpusBlocker = (n: number): string | null => n < 2 ? null : cfg.smpBlocked ?? cfg.smpWhy ?? (n > cfg.cpuMax ? `this device reports ${cfg.cpuMax} CPU threads` : null); /** Reload this tab once on `n` CPUs (config.ts reads CPUS_ONCE_KEY) and resume the suspended session. False if sessionStorage refuses. */ function resumeOnCpus(n: number, reason: string): boolean { try { sessionStorage.setItem(CPUS_ONCE_KEY, String(n)); } catch { return false; } ui.hostLog(`[host] booting on ${n} CPUs instead: ${reason}`); // A quiet resume (its session was resumed on another count than the URL's) stays quiet. if (resumeRequest === "auto") { ui.resumeAuto(); return true; } KW.term?.notice(`[host] booting on ${n} CPUs instead: ${reason}`); ui.resume(); // sets kw-resume and reloads return true; } /** Do not boot: the suspended session stays, with its data disk as it was. Always true (the page stops here). */ function keepSession(why: string): boolean { ui.hostLog(`[host] not booting: ${why}`); ui.check("session", "Suspended session", "warn", "kept; not booted", why); ui.setState("halted", "Not started"); setLive(null); unquiet(); void termReady.then(() => KW.term?.notice(`[host] not booted: ${why} — reload the page to be asked again`), () => {}); return true; } /** * A suspended session saved on another CPU count than this load's (a * task resumes on the CPU it ran on, so it needs exactly its count; * sessions from before SMP ones ran on one): offer to reload on that * count, where this page can start it now. Where it cannot, this load * boots its own count, and asks first if the session has a data disk: * that boot changes the disk, which makes the session useless. True: * the page reloads or stops here. Asked before the kernel is needed, * since the answer picks it. */ async function offerResumeCpus(info: Promise): Promise { let meta: SessionMeta | null; try { meta = await info; } catch (e) { ui.hostLog(`[host] suspended session: cannot read it: ${(e as Error)?.message ?? e}`); return false; } const n = meta ? sessionCpus(meta) : cfg.cpus; if (!meta || n === cfg.cpus) return false; const label = `saved ${agoSince(meta.saved)}, ${fmtMiB(meta.bytes)}, ${cpusText(n)}`; const why = sessionMismatch(meta, kernelFor(n), sessionDevicesFor(meta), distroRef) ?? (otherTab && meta.devices.data ? "the data disk is open in another tab" : null); if (why) { ui.check("session", "Suspended session", "info", `${label}; not resumed`, why); ui.hostLog(`[host] suspended session (${label}) not offered: ${why}`); return false; } const m = cfg.cpus; const blocked = cpusBlocker(n); if (blocked) { const msg = `the session suspended ${agoSince(meta.saved)} runs on ${cpusText(n)}, which this page cannot start now (${blocked})`; ui.check("session", "Suspended session", "warn", `${label}; not resumed`, msg); ui.hostLog(`[host] suspended session (${label}) not offered: ${msg}`); if (meta.devices.data) { if (!await ui.ask({ title: "resume", msg: `The session suspended ${agoSince(meta.saved)} runs on ${cpusText(n)}, which this page cannot start now (${blocked}). Booting on ${cpusText(m)} discards it: that boot changes the data disk.`, ok: `Boot on ${cpusText(m)}`, cancel: "Keep it" })) return keepSession(`the session suspended ${agoSince(meta.saved)} needs ${cpusText(n)} (${blocked})`); ui.hostLog(`[host] suspended session (${label}): booting on ${cpusText(m)} instead, discarded`); await clearSession().catch(() => {}); return false; } warn(`Not resuming the session suspended ${agoSince(meta.saved)}: it runs on ${cpusText(n)}, which this page cannot start now (${blocked}); it is kept`); ui.toast(`Not resumed: the session needs ${cpusText(n)}`); return false; } const dataNote = meta.devices.data ? ` Booting on ${cpusText(m)} discards it: that boot changes the data disk.` : ` Booting on ${cpusText(m)} keeps it for later.`; if (!resumeAsked && !await ui.ask({ title: "resume", msg: `Resume the session suspended ${agoSince(meta.saved)} (${fmtMiB(meta.bytes)}, ${meta.memoryMiB} MiB of memory)? It resumes on ${cpusText(n)}, as it was saved.${dataNote}`, ok: `Resume on ${cpusText(n)}`, cancel: `Boot on ${cpusText(m)}` })) { ui.hostLog(`[host] suspended session (${label}): booting on ${cpusText(m)} instead`); ui.check("session", "Suspended session", "info", `${label}; not resumed`, `booted on ${cpusText(m)} instead${meta.devices.data ? " (discarded: the data disk changes)" : ""}`); if (meta.devices.data) await clearSession().catch(() => {}); return false; } const reason = `resuming the session suspended ${agoSince(meta.saved)}, which runs on ${cpusText(n)}`; return (n === 1 ? bootUpOnce(reason, true) : resumeOnCpus(n, reason)) || keepSession(`this browser refuses sessionStorage, which the reload on ${cpusText(n)} needs (set CPUs to ${n} under Options and Reboot to resume)`); } // --- the distro image and what the base made of it --------------------------------- /** The distro image's HttpBase counters (inline only: the worker reads it there). */ const distroStats = () => (distroDisk?.inner as { base?: { stats?: { requests: number; bytes: number } } } | undefined)?.base?.stats; /** The image as the host read it (its superblock), as a boot line and the Distro row. */ function showDistro(info: { fs: string; size: number; label: string; uuid: string }, ms: number | null) { if (!distro) return; ok(`Attached the ${capitalize(distro.name)} image (${fmtMiB(info.size)}, streamed)`, ms); ui.check("distro", "Distro", cfg.distroWhy ? "warn" : "pass", distro.name, `${cfg.distroWhy ? `${cfg.distroWhy}; ` : ""}${distro.url}: ${info.fs}, ${fmtMiB(info.size)}, label ${info.label}, uuid ${info.uuid}; read-only /dev/wda, read with Range requests as the guest needs it`); ui.hostLog(`[host] distro: ${distro.url} (${info.fs}, ${info.size} bytes, uuid ${info.uuid})${ms != null ? ` opened in ${fmtS(ms)}` : ""}`); } /** Once user space runs: /run/kw-base (guest/base/initramfs/init) says which image booted and where its changes go. */ async function showBase(vm: KernelHandle) { try { // Shell builtins only: a program (cat) would be read from the distro image, over HTTP. const r = await vm.exec("/bin/sh", ["-c", 'while IFS= read -r l; do echo "$l"; done < /run/kw-base']); if (r.exitCode !== 0) throw new Error(r.stderr.trim() || `exit ${r.exitCode}`); const kv: Record = {}; for (const l of r.stdout.split("\n")) if (l.includes("=")) kv[l.slice(0, l.indexOf("="))] = l.slice(l.indexOf("=") + 1); const st = distroStats(); const reads = st ? `; ${st.requests} range requests, ${fmtMiB(st.bytes)} of the image read so far` : ""; ui.hostLog(`[host] base: name=${kv.name ?? ""} image=${kv.image ?? ""} upper=${kv.upper ?? ""} why=${kv.why ?? ""}${reads}`); if (kv.upper === "none") return; // the base shell: watchUser() said why if (kv.image) ui.distroBooted(kv.image); if (distro) ui.check("distro", "Distro", cfg.distroWhy ? "warn" : "pass", kv.image || distro.name, `${cfg.distroWhy ? `${cfg.distroWhy}; ` : ""}${distro.url}${vm.distro ? `: ${vm.distro.fs}, ${fmtMiB(vm.distro.size)}` : ""}; read-only /dev/wda${reads}`); const why = kv.why ?? ""; if (kv.upper === "data") ui.check("overlay", "Root", "pass", "overlay, changes on the data disk", `${kv.state}/upper over the ${kv.name} image`); else if (why === "kw.root=initramfs") ui.check("overlay", "Root", "warn", "safe mode (kw.root=initramfs)", `the plain ${kv.name} image; changes in memory, ${kv.state}/upper unused this boot`); else if (!why.startsWith("safe mode")) ui.check("overlay", "Root", "info", "overlay, changes in memory", why); // safe mode: watchUser() said why } catch (e) { ui.hostLog(`[host] /run/kw-base: ${(e as Error)?.message ?? e}`); } } /** * Guest Vulkan (on by default on Debian where WebGPU is; ?vk=0): the * Environment tab's "Guest Vulkan" row. The renderer loads on the guest's * first Vulkan instance (vm.vk.status, vm.vk.loaded), so until then the * row says it is not loaded; then the WebGPU adapter and how long the load * took, and (vm.vk.stats, every 2 s while they change) the instances * served and, from the renderer's counters over the last 2 s, frames * presented a second and draws per frame, with its errors where there are * any. A software adapter (SwiftShader) is a warning; so is a lost WebGPU * device (status "lost": the programs then running were cut off, the next * one gets a new device), as the GL row's context loss. */ function showVk(vm: KernelHandle) { if (!cfg.vk) { if (cfg.vkWhy) ui.check("vk", "Guest Vulkan", cfg.vkAsked ? "warn" : "info", "off", cfg.vkWhy); return; } const vk = vm.vk; if (!vk) { ui.check("vk", "Guest Vulkan", "fail", "unavailable", "the SDK started no Vulkan renderer (the Kernel log says why)"); return; } const how = "Vulkan through the base's ICD (/usr/lib/kw/libvulkan_kw.so) on vsock port 1018; try: sudo apt-get install vulkan-tools, then vulkaninfo --summary, or vkcube --wsi wayland (the launcher's vkcube, vkmark)"; let shown = "", reported = false, frames = ""; let prev: Record | null = null, prevAt = performance.now(); const show = () => { const s = vk.status, st = vk.stats, at = performance.now(), r = st.renderer; // Rates over the last interval (at least 1.5 s), from the current renderer's counters (they restart with a new renderer). if (!r) { prev = null; frames = ""; } else if (!prev || (r.presents ?? 0) < (prev.presents ?? 0)) { prev = { ...r }; prevAt = at; } else if (at - prevAt >= 1500) { const p = (r.presents ?? 0) - (prev.presents ?? 0), d = (r.draws ?? 0) - (prev.draws ?? 0), sec = (at - prevAt) / 1000; frames = p ? `${(p / sec).toFixed(1)} frames presented a second, ${(d / p).toFixed(1)} draws a frame over the last ${fmtS(at - prevAt)}` : d ? `${(d / sec).toFixed(0)} draws a second, no presents, over the last ${fmtS(at - prevAt)}` : ""; prev = { ...r }; prevAt = at; } const total = r ? `; this renderer: ${r.presents ?? 0} presents, ${r.draws ?? 0} draws, ${r.submits ?? 0} submits, ${r.translations ?? 0} shaders translated, ${fmtMiB(r.bytesRead ?? 0)} read back, ${r.liveObjects ?? 0} live objects` : ""; const errors = r?.errors ? `; ${plural(r.errors, "renderer error")} (WebGPU validation or failed operations; the browser console has them)` : ""; const losses = st.losses ? `; the WebGPU device was lost ${st.losses === 1 ? "once" : `${st.losses} times`}` : ""; // Wayland windows: zero-copy where wland offers external buffers on the relay's link (wayland/buffers.ts) const ext = waylandApi.externalBuffers(), ws = waylandApi.stats(); const wl = ext === true ? `; Wayland windows: zero-copy presentation (external buffers: ${ws.extCommits} commits, ${ws.extImages} pictures${r ? `, ${r.externalPresents ?? 0} presents without readback` : ""})` : ext === false ? `; Wayland windows: by readback into wl_shm (${cfg.wlGpu ? "the guest's relay has no external buffers" : "?wlgpu=0"})` : ""; const now = `${plural(st.connections, "Vulkan instance")} now, ${st.opened} so far${frames ? `; ${frames}` : ""}${total}${errors}${losses}${wl}`; const key = `${s.state} ${s.adapter} ${now}`; if (key === shown) return; shown = key; const name = s.adapter || "WebGPU adapter"; const soft = /swiftshader|llvmpipe|software|warp/i.test(name); if (s.state === "ready") { const load = s.loadMs === null ? "loaded" : `loaded in ${fmtS(s.loadMs)} on the guest's first Vulkan instance, ${fmtS(s.startedAt ?? 0)} after the page opened`; if (!reported) { reported = true; console.log(`[unkernel] guest Vulkan: WebGPU adapter ${name}${soft ? " (software rendering)" : ""}; ${load}`); ui.hostLog(`[host] guest Vulkan: WebGPU adapter ${name}${soft ? " (software rendering)" : ""}; ${load}`); } ui.check("vk", "Guest Vulkan", soft || r?.errors ? "warn" : "pass", name, `WebGPU: ${name}${soft ? " (a software adapter: no GPU behind it)" : ""}; ${load}. ${now}. ${how}.`); } else if (s.state === "lost") ui.check("vk", "Guest Vulkan", "warn", "WebGPU device lost", `the browser dropped the renderer's WebGPU device (${s.error ?? "memory pressure, a GPU reset, the GPU process ended"}): ` + `the Vulkan programs that were running lost their device and must be restarted; the next one gets a new device and renderer, no page reload needed. ${now}.`); else if (s.state === "loading") ui.check("vk", "Guest Vulkan", "info", "loading", `${st.losses ? "a Vulkan program after a device loss: making a new renderer" : "the guest's first Vulkan instance: loading the renderer (a WebGPU device, kwvk-naga.wasm)"} for ${fmtS(at - (s.startedAt ?? at))}. ${how}.`); else if (s.state === "failed") ui.check("vk", "Guest Vulkan", "fail", "renderer failed", `the renderer did not load: ${s.error ?? "unknown error"}; Vulkan programs fail to create an instance. ${now}.`); else ui.check("vk", "Guest Vulkan", "info", "on, not loaded yet", `the renderer (WebGPU, and the SPIR-V to WGSL translator kwvk-naga.wasm, about 860 KB) is fetched and started on the guest's first Vulkan instance, which waits for it; nothing of it was loaded at boot. ${how}.`); }; show(); const timer = setInterval(() => (ended() ? clearInterval(timer) : show()), 2000); vk.loaded.then(show, (e: unknown) => { show(); ui.toast(`Guest Vulkan: the renderer did not load (${(e as Error)?.message ?? e})`, { ms: 8000 }); }); } /** * Guest GL (on by default on Debian; ?gl=0): the Environment tab's "Guest * GL" row. The renderer loads on the guest's first GL use (vm.gl.status, * vm.gl.loaded), so until then the row says it is not loaded (or why it * cannot be: no WebGL2 context when the SDK tried one before the boot); * once loaded, the WebGL2 renderer the guest's Mesa draws on and how long * the load took, also in the console and the Kernel log; a software * renderer (SwiftShader, llvmpipe, WARP) is a warning, since GL is then no * faster than the host's CPU. Then the bridges and clients served * (vm.gl.stats), every 2 s while they change, and where a frame's time * went in the renderer over the last 2 s with readbacks (glFrameTimes), * and the device's counters (gpuFrames). */ function showGl(vm: KernelHandle) { if (!cfg.gl) { if (cfg.glWhy) ui.check("gl", "Guest GL", cfg.glAsked && !glOwn ? "warn" : "info", "off", cfg.glWhy); return; } const gl = vm.gl; if (!gl) { ui.check("gl", "Guest GL", "fail", "unavailable", "the SDK started no GL renderer (the Kernel log says why)"); return; } const how = cfg.gpu ? "virtio-gpu: Mesa uses /dev/dri with no variables (the vtest link: GALLIUM_DRIVER=virpipe LIBGL_ALWAYS_SOFTWARE=1 cmd; ?gpu=0 for its shells); Mesa: sudo apt-get install mesa-utils (eglinfo -B -p gbm), kmscube, glmark2-es2-drm ..." : "Guest shells have GALLIUM_DRIVER=virpipe LIBGL_ALWAYS_SOFTWARE=1 (no virtio-gpu: ?gpu=0 or ?display=0); Mesa: sudo apt-get install mesa-utils (eglinfo -B), glmark2-es2-x11 ..."; const fallback = cfg.gpu ? "GL programs fall back to software rendering (llvmpipe), KMS with dumb buffers works" : "GL programs on the vtest link fail; without GALLIUM_DRIVER they use llvmpipe"; ui.check("gl", "Guest GL", "info", "starting", "starting the guest's forwarder; the renderer (virgl.mjs, virgl.wasm) loads on the guest's first GL use"); let webgl = "", soft = false, loadNote = "", linked = false, failed = false; let shown = "", times = "", prev: VirglTotals | undefined, prevAt = performance.now(); let gpuTimes = "", gpuPrev: GpuRendererStats | undefined, gpuPrevAt = prevAt; const show = () => { if (failed) return; const st = gl.stats, s = gl.status, at = performance.now(); if (st.renderer) { times = glFrameTimes(st.renderer, prev, at - prevAt) ?? times; prev = st.renderer; prevAt = at; } if (st.gpu) { gpuTimes = gpuFrames(st.gpu, gpuPrev, at - gpuPrevAt); gpuPrev = st.gpu; gpuPrevAt = at; } const now = `vtest: ${plural(st.streams, "forwarder link")}, ${plural(st.clients, "GL client")} now, ${st.opened} so far` + `${cfg.glStrictWaits ? "; busy waits answered as vtest does (?glwait=strict)" : ""}${times ? `. ${times}` : ""}${gpuTimes ? `. ${gpuTimes}` : ""}`; const key = `${s.state} ${now}`; if (key === shown) return; shown = key; const losses = s.losses ? `; the WebGL2 context was lost ${s.losses === 1 ? "once" : `${s.losses} times`} and a new renderer made ${s.renewals === 1 ? "once" : `${s.renewals} times`} (the GL programs running at a loss had to be restarted)` : ""; if (s.state === "lost" || (s.state === "ready" && st.gpu?.contextLost)) ui.check("gl", "Guest GL", "warn", "WebGL context lost", `the browser dropped the renderer's WebGL2 context (memory pressure, a GPU reset, the GPU process ended; ${s.losses} so far): ` + `every GL object of the guest went with it, so the GL programs that were running fail: restart them; the next GL program gets a new renderer, no page reload needed` + `${s.error ? ` (the last try failed: ${s.error})` : ""}. ${now}.`); else if (s.state === "ready") { const name = s.webgl ?? webgl; ui.check("gl", "Guest GL", soft ? "warn" : "pass", name, `WebGL2: ${name}${soft ? " (a software renderer: no GPU behind it)" : ""}; ${s.what}${cfg.gpu ? ", virtio-gpu 3D" : ""}; ${loadNote}${losses}. ${how} ${now}.`); } else if (s.state === "loading") ui.check("gl", "Guest GL", "info", "loading", `${s.losses ? "a GL program after the WebGL2 context was lost: making a new renderer" : "the guest's first GL use: loading the renderer (virgl.mjs, virgl.wasm, a WebGL2 context)"} for ${fmtS(at - (s.startedAt ?? at))}. ${how}`); else if (linked) ui.check("gl", "Guest GL", "info", "on, not loaded yet", `the renderer (virglrenderer in wasm, virgl.mjs and virgl.wasm, about 480 KB) is fetched and started on this browser's WebGL2 on the guest's first GL use (a program opening /dev/dri for 3D, or a vtest client), which waits for it; nothing of it was loaded at boot. ${how} ${now}.`); }; let timer: ReturnType | undefined; const tick = () => { if (ended()) clearInterval(timer); else show(); }; gl.ready.then(() => { linked = true; show(); timer ??= setInterval(tick, 2000); }, (e: unknown) => { const why = (e as Error)?.message ?? String(e); failed = true; if (gl.status.state === "failed" && gl.status.startedAt === null) { // Known before the boot (no WebGL2 context): the device is 2D, nothing was tried. ui.check("gl", "Guest GL", cfg.glAsked ? "warn" : "info", "unavailable", `${why}. The guest has no GL renderer: ${cfg.gpu ? "its virtio-gpu device is 2D (KMS with dumb buffers), and " : ""}GL programs use software rendering (llvmpipe)`); if (cfg.glAsked) ui.toast(`Guest GL is unavailable: ${why}`); return; } ui.check("gl", "Guest GL", "fail", "failed", why); ui.toast(`Guest GL failed: ${why}`); }); gl.loaded.then((what) => { const s = gl.status; webgl = s.webgl ?? what.slice(what.lastIndexOf("; ") + 2); soft = /SwiftShader|llvmpipe|softpipe|Software|Basic Render|WARP/i.test(webgl); loadNote = s.loadMs === null ? "loaded" : s.eager ? `loaded before the boot in ${fmtS(s.loadMs)} (?glload=eager)` : `loaded in ${fmtS(s.loadMs)} on the guest's first GL use, ${fmtS(s.startedAt ?? 0)} after the page opened`; console.log(`[unkernel] guest GL: WebGL2 renderer ${webgl}${soft ? " (software rendering)" : ""}; ${loadNote}`); ui.hostLog(`[host] guest GL: renderer ${what}${soft ? " (software rendering)" : ""}; ${loadNote}`); if (soft) ui.toast(`Guest GL runs on a software WebGL renderer (${webgl}), not a GPU`); show(); timer ??= setInterval(tick, 2000); }, (e: unknown) => { if (failed) return; // said above failed = true; const why = (e as Error)?.message ?? String(e); ui.check("gl", "Guest GL", "fail", "renderer failed", `the renderer did not load on the guest's first GL use: ${why}; ${fallback}`); ui.toast(`Guest GL: the renderer did not load (${why}); ${fallback}`, { ms: 8000 }); }); } /** * The virtio-gpu device's 3D counters (vm.gl.stats.gpu; js.md, virtio-gpu * 3D): presented frames (RESOURCE_FLUSH of the scanout, which Display.tsx * draws from the renderer's texture), fenced commands that waited for the * GPU (parks: by default only behind a presented frame), readbacks and * refused commands, over the last MS and in all. */ function gpuFrames(g: GpuRendererStats, p: GpuRendererStats | undefined, ms: number): string { type Counter = Exclude; const d = (k: Counter) => g[k] - (p?.[k] ?? 0); const rate = p && ms > 0 && (d("presents") || d("commands")) ? `last ${(ms / 1000).toFixed(0)} s: ${(d("presents") * 1000 / ms).toFixed(1)} presents/s, ${d("parks")} parks, ${(d("parks") ? d("parkedMs") / d("parks") : 0).toFixed(2)} ms parked each; ` : ""; return `virtio-gpu: ${rate}${g.presents} presents, ${g.commands} commands (${g.fenced} fenced: ${g.parks} parked, ${g.parkedMs.toFixed(0)} ms in all), ` + `${g.reads} readbacks, ${g.glErrors ? `${g.glErrors} WebGL errors (calls WebGL dropped; the browser console has them), ` : ""}${g.errors} errors${g.contextLost ? " (the WebGL2 context is lost: restart the GL programs; the next one gets a new renderer)" : g.lastError ? ` (last: ${g.lastError})` : ""}`; } /** * Where the renderer's time went per readback (TRANSFER_GET2, about one a * frame for a program that shows its frames) between two of its counters * (vm.gl.stats.renderer; .agents/gl.md, Stats), MS apart; null without a * readback in between. Parked: busy waits held until WebGL said the GPU * was done; reading: glReadPixels or getBufferSubData, which wait for the * GPU work they read; issuing: decoding command buffers into GL calls. The * rest of a frame is outside the renderer (the guest, Mesa, kwvsock). */ function glFrameTimes(r: VirglTotals, p: VirglTotals | undefined, ms: number): string | null { const d = (k: keyof VirglTotals) => r[k] - (p?.[k] ?? 0); const n = d("reads"); // n < 0: a new renderer after a loss counts from 0 again if (n <= 0 || ms <= 0) return null; const per = (k: keyof VirglTotals) => `${(d(k) / n).toFixed(2)} ms`; const busy = d("submitMs") + d("readMs") + d("writeMs") + d("otherMs"); const atOnce = d("waitsRead") + d("waitsDone") + d("waitsAhead"); return `Last ${(ms / 1000).toFixed(0)} s: ${(n * 1000 / ms).toFixed(0)} readbacks/s (about one a frame), ${(ms / n).toFixed(2)} ms apart: ` + `${per("parkedMs")} parked in busy waits, ${per("readMs")} reading back (waits for the GPU), ${per("submitMs")} issuing GL, ` + `${per("writeMs")} uploading; renderer busy ${(100 * busy / ms).toFixed(0)}% of the time; busy waits ${d("waits")}: ${atOnce} answered at once ` + `(${d("waitsAhead")} ahead of the GPU), ${d("parks")} parked (longest so far ${r.parkMaxMs.toFixed(1)} ms)`; } // --- boot -------------------------------------------------------------------- async function boot() { await claimDisk(); // the data disk open in another tab: asked first (and maybe waits here) ui.setState("loading", "Downloading"); setLive(" .... ", "Downloading"); const t0 = performance.now(); // The kernel downloads and compiles on this thread in both modes (the engine compiles // off-thread while it streams), so it overlaps the terminal's start and, in worker // mode, the data disk lock; the worker then gets the compiled Module. const load = (url: string) => { const p = loadKernelModule(url, { what: "kernel", onProgress: onDownload }).catch((e: unknown) => { const compile = e instanceof BootError && e.stage === "compile"; throw tag(e, compile ? "compile" : "fetch", compile ? "compile" : "kernel"); }); p.catch(() => {}); // awaited below; a failure there is the one reported return p; }; // Several CPUs: the SMP kernel, once a suspended session was offered (below). let loading = smp ? null : load(kernelUrl); // Tailscale: its module loads while the kernel downloads (null: off, blocked, or it did not load). if (cfg.tailscaleWhy) ui.hostLog(`[host] tailscale: off this boot: ${cfg.tailscaleWhy}`); const tailscaleLoading = tailscaleSpec(cfg, (m) => { console.log(m); ui.hostLog(m); }); // The image to boot (worker mode: the worker opens it from its URL). if (cfg.distroWhy) { warn(cfg.distroWhy); ui.toast(cfg.distroWhy); } ui.check("distro", "Distro", cfg.distroWhy ? "warn" : "info", distro?.name ?? "none (base shell)", cfg.distroWhy ?? (distro ? `${distro.url}, ${fmtMiB(distro.size)}` : "?distro=none: the base initramfs's shell, no distro image")); // Inline: opened while the kernel downloads (one Range request and the first 64 KiB). const tDistro = performance.now(); const distroOpening = distro && !cfg.worker ? openDistroDisk(distro.url, { log: (m) => { console.log(m); ui.hostLog(m); } }).then((d) => ({ d, ms: performance.now() - tDistro })) : null; distroOpening?.catch(() => {}); // awaited below const session = sessionInfo(); session.catch(() => {}); if (resumeRequest === null && quietResume) { // A plain load: quiet only if there is a session it can resume as it is (offerResume() decides for good, // also on the data disk); else the boot lines show at once. An IndexedDB that does not answer: shown. const settle = setTimeout(unquiet, 2000); session.then((m) => { clearTimeout(settle); if (quietResume && m && sessionCpus(m) === cfg.cpus && !sessionMismatch(m, kernelUrl, sessionDevicesFor(m), distroRef) && !(otherTab && m.devices.data)) ui.bootResume(true); else unquiet(); }, () => { clearTimeout(settle); unquiet(); }); } let dataDisks: BlockDevice[] | DiskSpec[] = []; // The data disk's size (?datasize=): a saved disk grows to it only when no suspended session // that has the data disk is waiting, as its kernel knows the disk at the old size (Reboot drops it). // Clamped to what the disk can fill (dataSizeCap(), as Settings' Disk size): the browser's quota for the // page less the site's other data; a larger disk would fail its writes once the quota is used up. The // saved disk's own bytes (part of the usage) are looked up only when the clamp would apply without them. const dataSize = async () => { if (otherTab || (await session.catch(() => null))?.devices?.data) return undefined; const est = await navigator.storage?.estimate?.().catch(() => undefined); let cap = dataSizeCap(est); if (cfg.dataSizeGiB > cap && cfg.data) { const saved = await dataDiskInfo({ backend: cfg.datastore, bytes: true }).catch(() => null); cap = dataSizeCap(est, saved?.bytes ?? 0, saved?.size ? Math.floor(saved.size / 1073741824) : 0); } const gib = Math.min(cfg.dataSizeGiB, cap); if (gib < cfg.dataSizeGiB) ui.hostLog(`[host] data disk: ${gib} GiB, not ${cfg.dataSizeGiB}: this browser's quota for the page (${fmtSize(est!.quota!)}) less its other data leaves room for ${cap} GiB`); return gib * 1073741824; }; if (cfg.worker) { // The worker builds the data disk and the network from specs. ui.check("worker", "Worker mode", "pass", cfg.workerParam ? (cfg.workerAsked ? "on (?worker=1)" : "on (default)") : `for ${cfg.cpus} CPUs (${cfg.resumeCpus ? "the suspended session's" : `?cpus=${cfg.smpParam}`})`, "the kernel, the data disk, the network, the display, input and sound devices run in a Web Worker" + (smp ? ", which starts the other CPUs as workers of its own" : "")); ui.hostLog(`[host] worker mode: the kernel runs in a Web Worker, with the data disk, the network, the display, input and sound${smp ? `; ${cfg.cpus} CPUs` : ""}`); dataDisks = await dataSpec(await dataSize()); } if (await offerResumeCpus(session)) return; // reloading to resume on the session's CPU count, or kept if (smp) loading = load(kernelUrl); let module: WebAssembly.Module; try { module = await loading!; } catch (e) { if (smp && bootUpOnce(`the SMP kernel did not load (${(e as Error)?.message ?? e})`)) return; throw e; } dropLegacyScope(); // an older build's offline copy (sw.ts) if (ui.stageState("compile") !== "active") ui.stage("compile", "active"); // no progress events (e.g. a cached module) ui.stage("compile", "done", `${WebAssembly.Module.imports(module).length} imports, ${WebAssembly.Module.exports(module).length} exports`); setLive(null); ok("Compiled the kernel", ui.stageTime("compile")); ui.hostLog(`[host] kernel downloaded and compiled in ${fmtS(performance.now() - t0)}`); const bundle = bundledBoot(module); const b = bundle.initrd; if (!b) throw tag(new Error(`${kernelUrl} has no bundled base initramfs`), "compile", "kernel"); ui.hostLog(`[host] base initramfs: bundled in ${kernelUrl} (${fmtMiB(b.length)} gzipped)`); ok(`Found the base initramfs (${fmtMiB(b.length)})`); // The bundle's own command line unless ?append= gives one, with ?root= (kw.root=) after it. if (!cfg.append && bundle.cmdline) cfg.cmdline = cfg.root ? `${bundle.cmdline} kw.root=${cfg.root}` : bundle.cmdline; // Guest GL: the guest's shells point Mesa at virgl, at the vtest link where there is no // /dev/dri render node (kw-gl.sh in the base's kw layer). if (cfg.gl) cfg.cmdline += " kw.gl=1"; // The Wayland desktop: with kwshm the page listens for wland's relay (attachWayland()), and // kw.wayland=1 has the guest start the relay at boot, which dials only when an app connects, // and give its shells the session (wland --boot and kw-wayland.sh in the base's kw layer). // This load's own kwshm, like kw.gl=1: a resume keeps the saved kernel's command line. if (cfg.shm) cfg.cmdline += " kw.wayland=1"; if (distroOpening) { try { const { d, ms } = await distroOpening; distroDisk = d; showDistro(d.info, ms); } catch (e) { throw tag(e, "distro", "kernel"); } } if (!cfg.worker) dataDisks = await openData(module, await dataSize()); await termReady.catch((e: unknown) => { throw tag(e, "terminal", ""); }); // the console needs it (and its size) const memoryStage = () => { ui.setState("booting", "Booting"); ui.stage("memory", "active", `${cfg.memMiB} MiB requested (${cfg.memWhy})`); }; if (!cfg.worker) memoryStage(); // worker mode: once the worker runs // A failed read of the distro image (the server went away, or the image was rebuilt: // the SDK's strict HttpBase refuses a changed one) is an I/O error in the guest: say so. let distroReadFailed = false; const log = (m: string) => { // The Tailscale login link is the user's to open, never in a log (Copy diagnostics): whoever opens it adds the node to their tailnet. if (m.startsWith("[tailscale] log in at ")) m = "[tailscale] log in: the link is in Settings, Network"; console.log(m); ui.hostLog(m); if (distro && !distroReadFailed && /^\[host\] block: wda op 0 /.test(m)) { distroReadFailed = true; KW.report("distro", `Reading the ${distro.name} image failed: the guest gets I/O errors from its root. If the image was rebuilt or the server stopped, reload the page.`, m, false); } }; // Worker mode: a worker that never reports back must not leave the page waiting. let workerWatchdog: ReturnType | undefined; if (cfg.worker) { setLive(" .... ", "Starting the kernel worker"); workerWatchdog = setTimeout(() => { // A default worker: the page's thread once instead (an asked one is reported). if (!cfg.workerAsked && bootUpOnce("the kernel worker did not report back within 20 s", false, true)) return; unquiet(); ui.fail("worker", "The kernel worker did not report back within 20 s (it may have failed to load; the browser console may say why)", "", { title: "Worker mode failed" }); }, 20000); } const onProgress = (p: BootProgress) => { if (p.stage === "worker") { // Worker mode: the worker runs. JSPI in a worker is checked by nobody but this. clearTimeout(workerWatchdog); ui.check("worker-jspi", "JSPI in the worker", p.jspi ? "pass" : "fail", p.jspi ? "yes" : "no", p.jspi ? "WebAssembly.Suspending and promising exist in the Web Worker" : "the page has JSPI but its Web Worker does not, so worker mode cannot run in this browser (leave out ?worker=1)"); ui.hostLog(`[host] kernel worker started; JSPI in the worker: ${p.jspi ? "yes" : "no"}`); memoryStage(); } else if (p.stage === "download") { onDownload(p); } else if (p.stage === "memory") { KW.memActualMiB = p.mib; const fell = p.failures.length > 0; const kind = p.memory64 ? "64-bit" : "32-bit"; ui.stage("memory", "done", fell ? `${p.mib} MiB ${kind} (${p.requestedMiB} MiB failed: ${p.failures.join("; ")})` : `${p.mib} MiB ${kind}`); ui.check("mem", "Kernel memory", fell ? "warn" : "pass", `${p.mib} MiB`, fell ? `wanted ${p.requestedMiB} MiB: ${p.failures.join("; ")}` : cfg.memWhy); if (fell) { ui.toast(`Using ${p.mib} MiB: ${p.requestedMiB} MiB could not be allocated`); warn(`Allocated ${p.mib} MiB of memory (${p.requestedMiB} MiB failed)`, ui.stageTime("memory")); } else ok(`Allocated ${p.mib} MiB of memory`, ui.stageTime("memory")); } else if (p.stage === "instantiate") { ui.stage("boot", "active", "instantiating the module"); } else if (p.stage === "start") { ui.stage("boot", "active", `cmdline: ${cfg.cmdline}`); // The transient line, which the kernel's messages then follow; its result, // "Started Linux", heads the screen once user space runs. ?verbose=1 never // writes that, and lists every kernel line: a header line instead. if (cfg.verbose) bootLine(` Starting Linux${cmdlineNote()}\r\n`); else setLive(" .... ", "Starting Linux"); } }; const common = { cmdline: cfg.cmdline, memoryMiB: cfg.memMiB, memoryFallbackMiB: cfg.memFallbackMiB, jit: cfg.jit, consoleSize: { cols: term.cols, rows: term.rows }, onOutput: consoleWrite, log, onProgress, display: cfg.display ? display.size : false, onDisplay: display.onDisplay, onInput: display.onInput, // The sink and sources are media.ts's, which the Sound and media applet follows. sound: cfg.sound ? soundOut(log) : false, // Web Audio (js/web/audio.ts), ?sound=0 soundIn: cfg.mic ? micIn(log) : false, // the microphone (js/web/audio-in.ts), asked for at the first recording; ?mic=0 // the camera (js/web/camera.ts), asked for when a program streams; ?cam=0. Its colour space is kept (`kw.camera-colour`) // so H.264 black frames match the camera's from the first stream of a load on (the stream's VUI stays the same) camera: cam ? cameraIn({ log, colorSpace: cameraColour(), onColorSpace: (c) => store("camera-colour", JSON.stringify(c)) }) : false, vsock: true, // the terminals (wterms.ts: kwtty sessions) onTailscale: onTailscaleEvent, // the node's state (saved), login link and status (tailscale.ts) // /dev/ttyACM0..3, for Web Serial ports attached later (serial.ts); nothing until a program opens one. serial: true, // kwbt, for the adapter chosen in Settings (bluetooth.ts); the guest has no hci0 until one is attached. bluetooth: true, // A USB root hub, for WebUSB devices attached later (usb.ts); nothing until one is. usb: true, }; // The network: Wisp (net.ts), as a spec in worker mode or with the Tailscale node (which runs on // this thread in both modes; the SDK bridges its packets to the worker), else a NetStack here. const tailscale = cfg.net ? await tailscaleLoading : null; const netSpec = () => browserNetSpec({ enabled: cfg.net, servers: cfg.wisp, routes: cfg.routes, tailscale: tailscale ?? undefined }); // Guest GL on this thread's WebGL2 (Debian, on by default; showGl()), read when the kernel is // made: a resumed session keeps its own (adoptSessionGl()). The renderer loads on the guest's // first GL use (?glload=eager: before the boot); with it virtio-gpu, the guest's /dev/dri, 3D on // that renderer (?gpu=0: none). // And kwshm, for the Wayland desktop (wayland.ts; a resumed session keeps its own too). // WebGPU without an adapter: this boot runs without guest Vulkan (config.ts vkAdapterBlocker()). if (cfg.vk) { const why = await vkAdapterBlocker(); if (why) Object.assign(cfg, { vk: false, vkBlocked: why, vkWhy: why }); } const glDevices = () => ({ gl: cfg.gl && (cfg.glStrictWaits || cfg.glEager ? { ...(cfg.glStrictWaits ? { strictWaits: true } : {}), ...(cfg.glEager ? { eager: true } : {}) } : true), gpu: cfg.gpu, shm, // Guest Vulkan on this thread's WebGPU (Debian, on by default; showVk()): nothing loads // before the guest's first Vulkan instance, and nothing of it is in a snapshot. vk: cfg.vk, // Guest VA-API on this thread's WebCodecs (Debian, on by default, ?va=0: off): nothing is // asked of WebCodecs before the guest's first VA-API connection, nothing is in a snapshot. va: cfg.va, }); // A session on this load's CPU count (another: offerResumeCpus(), above). const resume = await offerResume(session, dataDisks); if (!resume) unquiet(); // a fresh boot kernelStarting = true; try { const create = (restore?: KernelSnapshot) => { // Worker mode: the SDK posts the Module compiled above to the worker. An engine // that cannot post one (not seen so far; Safari is untested) gets the kernel's // URL instead, absolute because the worker has no base of its own, and the // worker downloads (from the HTTP cache) and compiles it. const inWorker = (kernel: WebAssembly.Module | string) => createKernel({ ...common, ...glDevices(), worker: true, kernel, compileInWorker: typeof kernel === "string", cpus: smp ? cfg.cpus : undefined, // the worker starts the other CPUs (js/web/cpu.ts) shared: cfg.shared ? undefined : false, // shared memory where the page is cross-origin isolated (?shared=0: never) disks: dataDisks as DiskSpec[], onDiskError: (_i, e) => saveError(e), distro: distro?.url, // a spec the page makes (relative to the page); the worker opens it net: netSpec(), // Wisp, in the worker (net.ts) restore, transferRestore: true, // the snapshot is read for this boot only }); return cfg.worker ? inWorker(module).catch((e: unknown) => { if (!(e instanceof BootError && e.stage === "support" && /cannot send the kernel/.test(e.message))) throw e; ui.hostLog(`[host] ${e.message}; the worker loads the kernel itself`); return inWorker(new URL(kernelUrl, location.href).href); }) : createKernel({ ...common, ...glDevices(), kernel: module, disks: dataDisks as BlockDevice[], restore, distro: distroDisk ?? undefined, net: tailscale ? netSpec() : browserNet({ enabled: cfg.net, servers: cfg.wisp, routes: cfg.routes, log }), // Wisp (net.ts) }); }; if (resume) { try { vm = await create(resume.snap); resumed = { note: resumedNote(agoSince(resume.meta.saved)), saved: resume.meta.saved, screen: resume.meta.screen, preview: resume.meta.preview ?? null, windows: resume.meta.windows, win: resume.meta.win }; interacted = true; // it was worth saving resumedSaved = resume.meta.saved; if (resume.meta.screen) screenTail.push(resume.meta.screen); ui.check("session", "Suspended session", "pass", `resumed (saved ${agoSince(resume.meta.saved)})`, `${fmtMiB(resume.meta.bytes)}; saved ${cfg.worker === resume.meta.worker ? "in this mode" : resume.meta.worker ? "in worker mode" : "inline"}`); } catch (e) { const why = (e as Error)?.message ?? String(e); // Several CPUs that did not start (the worker, its CPU threads; not a refusal of a disk or // device): the session stays, and the page reloads on one CPU, which asks before a boot // changes the session's data disk (offerResumeCpus()). const n = sessionCpus(resume.meta); if (n > 1 && (!/^snapshot: /.test(why) || /^snapshot: the snapshot has \d+ CPUs?, this machine has/.test(why)) && bootUpOnce(`resuming on ${n} CPUs failed (${why})`)) return; // Refused (a disk or device that differs, another kernel build) or failed: boot fresh. unquiet(); warn(`Could not resume the suspended session (${why}); booting fresh`); ui.toast(`Could not resume: ${why}`); ui.check("session", "Suspended session", "warn", "not resumed", why); dropSessionGl(); vm = await create(); } // Resumed or refused, it is used up: the running machine changes the disk. Resumed on several // CPUs, once user space answers: its tasks resume only after every CPU came back, so a CPU // that fails to start meanwhile leaves the disk, and so the session, as saved. if (resumed && sessionCpus(resume.meta) > 1) { const saved = resume.meta.saved; void vm.exec("/bin/sh", ["-c", ":"]).then(() => clearSession(saved)).catch(() => {}); } else await clearSession().catch(() => {}); } else vm = await create(); } catch (e) { kernelStarting = false; clearTimeout(workerWatchdog); // The distro image (worker mode opens it in the worker): not a reason to try one CPU. if (/^distro: /.test((e as Error)?.message ?? "")) throw tag(e, "distro", "kernel"); const be = e instanceof BootError ? e : undefined; // A default worker that could not start (its JSPI, the module): the page's thread, one CPU. if (cfg.worker && !cfg.workerAsked && be?.stage === "support" && bootUpOnce(`the kernel worker did not start (${be.message})`, false, true)) return; // Several CPUs that did not start (the worker, its JSPI, the shared memory): one CPU. if (smp && bootUpOnce(`booting on ${cfg.cpus} CPUs failed (${(e as Error)?.message ?? e})`)) return; if (cfg.worker && be?.stage === "support") throw tag(e, "worker", "boot"); if (cfg.worker && be?.stage === "download") throw tag(e, "fetch", "kernel"); if (cfg.worker && be?.stage === "compile") throw tag(e, "compile", "compile"); const stage = be?.stage === "memory" ? "memory" : "boot"; throw tag(e, stage, stage); } kernelStarting = false; clearTimeout(workerWatchdog); // The guest's WASI programs (.wasm files, run by kwwasi over vsock port 1021) // on the page's wasm engine, each in a Web Worker (js/web/wasi.ts). const wasi = vm.vsock ? serveWasiPrograms(vm.vsock, { log }) : undefined; void vm.exited.then(() => { vmExited = true; wasi?.close(); }); g.vm = vm; KW.vm = vm; // The Tailscale node's status (Settings, Network) and its Environment row, from its events. attachTailscale(vm, cfg); if (cfg.tailscaleParam) { let shown = ""; const row = () => { const c = tailscaleCheck(ui$().get().tailscale); if (c.r + c.value + c.note === shown) return; shown = c.r + c.value + c.note; ui.check("tailscale", "Tailscale", c.r, c.value, c.note); }; row(); ui$().subscribe(row); } // The guest preview (preview.ts) relays /vm/// to vm.fetch(). attachPreview(vm); // Serial devices (serial.ts): the Web Serial ports allowed before, and Connect in Settings. attachSerial(vm, (l) => ui.hostLog(l)); // Bluetooth (bluetooth.ts): the chosen adapter as the guest's hci0, and Add device in Settings. attachBluetooth(vm, (l) => ui.hostLog(l)); // USB devices (usb.ts): the WebUSB devices allowed before, and Connect in Settings. attachUsb(vm, (l) => ui.hostLog(l)); // Wayland apps (wayland.ts): wland's relay connects to vsock port 1023 when the guest's first Wayland app starts. const stopWayland = attachWayland(vm, (l) => ui.hostLog(l)); KW.wayland = waylandApi; // The guest's open and xdg-open (open.ts): its URLs, in the preview or a // new tab once the user confirms. Added once the kernel runs rather than // as createKernel's commands: spawn() and exec() (the windows, the demos) // would wait for those, and fail if linking them failed. const openUrl = openCommand({ ask: (q) => ui.ask(q), dismiss: () => ui.answer(false), previewSupported, preview: (port, path) => { openPreview(port, path); }, tab: (url) => { window.open(url, "_blank", "noopener,noreferrer"); }, }); for (const name of ["open", "xdg-open"]) vm.addCommand(name, openUrl, { timeout: 120000 }).catch((e: Error) => ui.hostLog(`[host] the guest's ${name}: ${e.message}`)); checkReady(); const inst = local(); if (inst) { g.kernel = inst.kernel; KW.kernel = inst.kernel; } if (cfg.worker && vm.distro) showDistro(vm.distro, null); if (vm instanceof WorkerKernel) { workerDataAttached(vm, performance.now() - t0); const shared = vm.transport === "shared"; const why = shared ? "the page is cross-origin isolated" : !cfg.shared ? "?shared=0" : "the page is not cross-origin isolated, so it has no SharedArrayBuffer"; ui.check("worker-transport", "Worker input and frames", shared ? "pass" : "info", shared ? "shared memory" : "messages", shared ? `${why}: the worker reads input and frame requests while the kernel runs (about 1 ms)` : `${why}: they wait for the kernel's next yield, up to 20 ms while the guest is busy`); ui.hostLog(`[host] kernel worker: console and evdev input and frames by ${shared ? "shared memory" : "messages"} (${why})`); } if (smp) { // The worker gives the SMP kernel one CPU where it cannot start their threads (it logs why). const got = vm.stats().cpus; ui.check("smp", "Guest CPUs", got > 1 ? "pass" : "warn", got > 1 ? `${got} (${cfg.resumeCpus ? "the suspended session's" : `?cpus=${cfg.smpParam}`})` : `1 (asked for ${cfg.cpus})`, got > 1 ? "the SMP kernel, each CPU in a Web Worker on one shared memory" : "the kernel worker could not start CPU threads (the Kernel log says why): the SMP kernel runs on one CPU"); ui.hostLog(`[host] SMP kernel on ${got} CPU${got > 1 ? "s" : ""} (${cfg.cpus} asked)`); if (got < cfg.cpus) ui.toast(`Running on ${got} CPU${got > 1 ? "s" : ""}: the worker could not start more`); } const booted = vm; KW.jitStats = () => booted.stats().jit; // kwemu's JIT counters in the System tab (and Copy diagnostics), for devices without devtools. let jitShown = ""; const showJit = () => { const j = booted.stats().jit; const value = !j.enabled ? (cfg.jit ? "unavailable" : "off (?jit=0)") : !j.requested ? "kernel without the JIT tier" : `${j.compiled} compiled, ${j.live} live (max ${j.maxLive}), ${j.freed} freed, ${j.failed} failed`; const gc = j.collected < 0 ? "" : `; ${j.collected} collected, ${j.retained} freed but uncollected (max ${j.maxRetained})` + (j.gcNudges ? `, ${j.gcNudges} GC nudges` : "") + (j.refused ? `, ${j.refused} refused` : ""); const note = j.compiled ? `${j.compileMs.toFixed(0)} ms compiling, ${fmtMiB(j.bytes)} of modules${gc}` : ""; if (value + note === jitShown) return; jitShown = value + note; ui.check("jit", "JIT tier", j.failed ? "warn" : j.compiled ? "pass" : "info", value, note); }; showJit(); showGl(vm); showVk(vm); // Not fatal, but worth flagging: the kernel started and printed nothing. const bootTime = inst ? inst.kernel.bootTime : performance.now(); const jitTimer = setInterval(() => (ended() ? clearInterval(jitTimer) : showJit()), 2000); const quiet = setInterval(() => { if (ended()) return clearInterval(quiet); if (!sawOutput && performance.now() - bootTime > 30000) { clearInterval(quiet); KW.report("watchdog", "No console output 30 s after the kernel started", `cmdline: ${cfg.cmdline}`, false); } }, 2000); if (resumed) { markRunning("resumed"); // the guest prints nothing by itself reopenPreview(vm, resumed.preview); // Terminals 1-9: their sessions wait detached in the guest (wterms.ts). if (Array.isArray(resumed.windows)) terminals.reattach(resumed.windows, resumed.win); // Every resume says so, the same way (a machine that went on in place, never stopped, says nothing). sayResumed(resumed.saved); } const res = await vm.exited; clearInterval(quiet); unquiet(); // it ended before the screen was restored attachPreview(null); // previews now get 503 stopWayland?.(); stopTerminals(`the kernel stopped (${res.kind})`); if (holdState) { // Held, the session saved: stopped after AUTO_SUSPEND_HOLD_MS (or by Suspend), or ended otherwise // (its worker died). Nothing is lost: the tab, when shown, resumes the saved session. const h = holdState; holdState = null; await suspending; ui.setState("halted", "Suspended", "stopped"); if (pause.ended()) { // Paused by hand: offered as after Suspend (the Resume card), not resumed by itself. if (res.kind !== "stopped" || h.lost) ui.hostLog(`[host] the paused system ended (${res.reason}); its session stays saved`); ui.setSuspended(h.bytes); write(`\r\n${okLine("Suspended — Resume (or reload the page) to continue")}\r\n`); return; } if (res.kind !== "stopped" || h.lost) ui.hostLog(`[host] the held system ended (${res.reason}); its saved session resumes when the tab is shown`); auto.heldEnded(); return; } if (suspending && res.kind === "stopped") { // The kernel stops only once the session is saved; failed: it was stopped meanwhile by something else. const failed = await suspending; ui.setState("halted", failed ? "Stopped" : "Suspended", failed ? null : "stopped"); // The machine is lost: say so, in red. if (failed) KW.term!.notice(`[host] the kernel stopped while it was being suspended, and the session was not saved (${failed}) — use Reboot in the top bar's Control Center to start again`); else if (auto.state !== "saving" && auto.state !== "done") { ui.setSuspended(suspendedBytes); // the Resume card write(`\r\n${okLine("Suspended — Resume (or reload the page) to continue")}\r\n`); } // Else suspended in the background: no card and nothing on the screen, which the resume // restores as it is once the tab is shown (autosuspend.ts; the Kernel log and the Environment row say it). return; } if (res.kind === "halt") { const label = res.code === 0 ? "Powered off" : `Halted (${res.code})`; ui.hostLog(`[host] kernel halted (code ${res.code})`); if (!running) { ui.stage("boot", "done"); ui.stage("userspace", "skip"); } // `reboot` in the guest (code 2): start again, unless it keeps doing that. if (res.code === 2 && autoRebootAllowed()) { ui.setState("halted", "Restarting"); KW.term!.notice("[host] restart requested — rebooting"); await data?.disk?.saving?.catch(() => {}); // the last flush's transaction ui.reboot(); return; } ui.setState("halted", label); const why = res.code === 2 ? ` (restart requested ${AUTO_REBOOT_MAX} times within a minute, so not rebooting on its own)` : ""; KW.term!.notice(`[host] ${label.toLowerCase()}${why} — use Reboot in the top bar's Control Center to start again`); return; } const titles: Record = { panic: "Kernel panic", trap: "WebAssembly trap in the kernel", fatal: "Host runtime error" }; ui.fail(res.kind, res.reason, (res.error as Error | undefined)?.stack ?? "", { title: titles[res.kind] ?? "Kernel stopped" }); } boot().catch((e: StageError) => { console.error(e); stopTerminals("the boot failed"); unquiet(); failed(e.kind === "distro" ? `Open the ${cfg.guest} image` : STEP[e.stageId ?? ""] ?? "Boot"); if (e.stageId) ui.stage(e.stageId, "fail", e.message); const titles: Record = { fetch: "Download failed", compile: "Kernel failed to compile", memory: "Not enough memory", boot: "Boot failed", terminal: "The terminal failed to start", worker: "Worker mode cannot run in this browser", distro: "The distro image could not be opened" }; const hint = e.kind === "distro" ? `The page reads ${cfg.distro?.url ?? "the image"} with Range requests, from its own server or the release mirror (web/scripts/kernel.ts picks the images; kernel/build.sh distro builds them). ` + "Pick another distro under Options and Reboot, or boot with ?distro=none for the base shell." : undefined; ui.fail(e.kind ?? "boot", e.message, (e.cause as Error | undefined)?.stack ?? e.stack, { title: titles[e.kind ?? ""] ?? "Boot failed", ...(hint ? { hint } : {}) }); }); }