mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-04 23:09:31 +08:00
A second kernel build (kw_smp.config) runs one instance of the module per CPU, each in a worker thread on a shared memory. The host's Kernel is CPU 0 with every device; wasm_cpu_start starts the others through Platform.spawnCpu (js/node/cpu.ts), which run js/core/cpu.ts: their own JSPI task switching, the clocks on CPU 0's time base, and console and exit forwarded to CPU 0's host. The host sets IRQ bits atomically and ends CPU 0's idle on Atomics.waitAsync of its word, which other CPUs' IPIs notify. createKernel's cpus option and the CLI's --smp N (which implies --worker) ask for CPUs; a UP kernel ignores them with a log line, and more than one CPU runs the interpreter only. kwemu built with -Dkwemu-atomics makes AMOs atomic, SC a compare-and-swap against the value LR read, and FENCE a barrier (ABI 7). kwtest gains an smp check (threads on several CPUs counting through AMOs, LR/SC and a mutex) and skips the park check on an SMP kernel, which has no parking. The kernel side is in five new patches.
25 lines
902 B
TypeScript
25 lines
902 B
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// Start a secondary CPU of an SMP kernel (cpu-entry.ts, core/cpu.ts) on a
|
|
// worker_threads Worker: Platform.spawnCpu on Node. Called from the
|
|
// kernel's own thread (the main thread inline, the kernel worker in worker
|
|
// mode). The worker gets the compiled module and the shared memory in its
|
|
// first message.
|
|
|
|
import { Worker } from "node:worker_threads";
|
|
import type { CpuEndpoint, CpuInit, CpuMessage } from "../core/cpu.ts";
|
|
|
|
export function spawnNodeCpu(init: CpuInit): CpuEndpoint {
|
|
const w = new Worker(new URL("./cpu-entry.ts", import.meta.url));
|
|
// The CPU never keeps the process alive: CPU 0's exit ends the session.
|
|
w.unref();
|
|
w.postMessage(init);
|
|
return {
|
|
listen(h) {
|
|
w.on("message", (m: CpuMessage) => h.message(m));
|
|
w.on("error", h.error);
|
|
},
|
|
terminate: () => void w.terminate(),
|
|
};
|
|
}
|