mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 04:50:56 +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.
14 lines
523 B
TypeScript
14 lines
523 B
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// A secondary CPU's thread on Node (started by cpu.ts): run it
|
|
// (core/cpu.ts) with what the first message brings.
|
|
|
|
import { parentPort } from "node:worker_threads";
|
|
import { runCpu, type CpuInit } from "../core/cpu.ts";
|
|
|
|
const port = parentPort;
|
|
if (!port) throw new Error("unkernel: a CPU runs only in a worker thread (cpu.ts)");
|
|
port.once("message", (init: CpuInit) => {
|
|
void runCpu(init, { post: (msg) => port.postMessage(msg) });
|
|
});
|