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.
21 lines
1.0 KiB
TypeScript
21 lines
1.0 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// The kernel worker's entry on Node (started by worker.ts): serve the
|
|
// kernel (core/worker-serve.ts) over parentPort, reading file: URLs from
|
|
// disk and building disks and the network from specs (devices.ts). It finds nothing through import.meta.url: in the package it runs
|
|
// from a string. The build bundles it with everything it imports except
|
|
// Node built-ins.
|
|
|
|
import { parentPort } from "node:worker_threads";
|
|
import { serveKernel } from "../core/worker-serve.ts";
|
|
import { spawnNodeCpu } from "./cpu.ts";
|
|
import { nodeDevices } from "./devices.ts";
|
|
import { nodeReadFile } from "./platform.ts";
|
|
|
|
const port = parentPort;
|
|
if (!port) throw new Error("unkernel: the kernel worker runs only in a worker thread (createKernel({ worker: true }))");
|
|
serveKernel({
|
|
post: (msg, transfer) => port.postMessage(msg, transfer),
|
|
listen: (handler) => port.on("message", handler),
|
|
}, { readFile: nodeReadFile(), devices: nodeDevices, spawnCpu: spawnNodeCpu });
|