feat(v4.0.0): the system boots from a nucleus and loads its capsules

One boot, v4/system/boot.c, for both products: it starts the nucleus image,
finds each capsule in the baked capsule directory, recomputes its hash,
checks its signature, gives its blocks to the node a line at a time, and
prints PARITY:V4_NUCLEUS, PARITY:V4_CAPSULE and PARITY:OK before the prompt.
A line the node does not accept ends the boot with the capsule, block and
line named.  docs/v4.0.0/NUCLEUS.md.

- hosted Linux product for amd64, aarch64 and riscv64 (make -C v4 hosted);
  make -C v4 hosted-check boots all three and requires identical output
- the kernel's v4 entry (STARFORTH_V4=1) calls the same boot
- capsules/v4/forth79.4th, block 6000: no definitions yet
- mkimage builds the nucleus only; no FORTH source is compiled at build time
- capsule_blocks.c: the Block-header parse, free of any VM, for every loader

Verified: make -C v4 test passes; hosted-check passes on the three ISAs with
the same hashes; the kernel compiles with STARFORTH_V4=1 on the three.
Not verified: no bare-metal boot of v4 has been run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rajames
2026-10-05 15:28:30 -04:00
co-authored by Claude Opus 5.5
parent 903321e548
commit 294e69463a
18 changed files with 968 additions and 8 deletions
+65
View File
@@ -60,6 +60,9 @@ $(eval $(call kconfig_bool,PARITY_MODE,0))
# StarForth VM integration (default: enabled)
$(eval $(call kconfig_bool,STARFORTH_ENABLE_VM,1))
# StarForth v4 at a single prompt in place of the v3 VM (default: off)
$(eval $(call kconfig_bool,STARFORTH_V4,0))
# Monolithic build — loader + kernel compiled together (default: enabled)
MONOLITHIC ?= 1
@@ -597,6 +600,30 @@ LOADER_VM_OBJS := $(patsubst v3/src/%.c,$(LOADER_OBJ_DIR)/vmcore/%.o,$(VM_CORE_S
KERNEL_VM_OBJS := $(patsubst v3/src/%.c,$(KERNEL_OBJ_DIR)/vmcore/%.o,$(VM_CORE_SRCS))
endif
# ------------------------------------------------------------------------------
# StarForth v4 (STARFORTH_V4=1): the golden model of the F18-derived engine,
# v4/src, and the nucleus image v4's own Makefile builds on this machine from
# v4/capsule (docs/v4.0.0/NUCLEUS.md). The node is the host node's size, with 64-bit cells, as every
# ISA this kernel boots on has (DECOMPOSITION.md D-5), so the image is the
# same file for all three.
# ------------------------------------------------------------------------------
ifeq ($(STARFORTH_V4),1)
V4_DEFS := -DSTARFORTH_V4=1 -Iv4/include \
-DV4_CELL_BITS=64 -DV4_NODE_WORDS=16384 -DV4_DATA_RING=30 -DV4_RET_RING=31
KERNEL_CFLAGS += $(V4_DEFS)
LOADER_CFLAGS += $(V4_DEFS)
V4_ENGINE_SRCS := $(addprefix v4/src/,node.c exec.c stack.c iword.c heat.c guard.c image.c)
V4_IMAGE_C := $(BUILD_DIR)/v4_image_64.c
LOADER_EXTRA_SRCS += $(KERNEL_SRC)/v4/sk_v4.c
KERNEL_EXTRA_SRCS += $(KERNEL_SRC)/v4/sk_v4.c
# v4/system/boot.c is the boot the hosted v4 system runs too: nucleus, then
# the capsules from this kernel's own capsule directory, then the prompt.
LOADER_V4_OBJS := $(patsubst v4/src/%.c,$(LOADER_OBJ_DIR)/v4engine/%.o,$(V4_ENGINE_SRCS)) $(LOADER_OBJ_DIR)/v4engine/v4_image.o $(LOADER_OBJ_DIR)/v4system/boot.o
KERNEL_V4_OBJS := $(patsubst v4/src/%.c,$(KERNEL_OBJ_DIR)/v4engine/%.o,$(V4_ENGINE_SRCS)) $(KERNEL_OBJ_DIR)/v4engine/v4_image.o $(KERNEL_OBJ_DIR)/v4system/boot.o
endif
LOADER_SRCS := $(LOADER_SRCS_BASE) $(LOADER_EXTRA_SRCS)
KERNEL_SRCS := $(KERNEL_SRCS_BASE) $(KERNEL_EXTRA_SRCS)
@@ -605,12 +632,14 @@ LOADER_OBJS := \
$(patsubst $(KERNEL_SRC)/%.c,$(LOADER_OBJ_DIR)/%.o,$(LOADER_ARCH_SRCS)) \
$(patsubst $(KERNEL_SRC)/%.S,$(LOADER_OBJ_DIR)/%.o,$(LOADER_ASM)) \
$(LOADER_VM_OBJS) \
$(LOADER_V4_OBJS) \
$(CAPSULE_GENERATED_OBJ)
KERNEL_OBJS := \
$(patsubst $(KERNEL_SRC)/%.c,$(KERNEL_OBJ_DIR)/%.o,$(KERNEL_SRCS)) \
$(patsubst $(KERNEL_SRC)/%.S,$(KERNEL_OBJ_DIR)/%.o,$(KERNEL_ASM)) \
$(KERNEL_VM_OBJS) \
$(KERNEL_V4_OBJS) \
$(CAPSULE_GENERATED_KOBJ)
# ==============================================================================
@@ -733,6 +762,41 @@ else
@$(LOADER_CC) $(VMCORE_CFLAGS_COMMON) $(filter-out -I$(KERNEL_INC),$(LOADER_CFLAGS)) -c $< -o $@
endif
# StarForth v4: the capsule image, built on this machine by v4's Makefile, and
# the engine, compiled like any other kernel source.
ifeq ($(STARFORTH_V4),1)
$(V4_IMAGE_C): FORCE
@mkdir -p $(dir $@)
@echo " V4IMG v4/capsule -> $@"
@$(MAKE) --no-print-directory -C v4 CC=cc build/v4_image_64.c
@cmp -s v4/build/v4_image_64.c $@ || cp v4/build/v4_image_64.c $@
$(LOADER_OBJ_DIR)/v4engine/v4_image.o: $(V4_IMAGE_C) | $(LOADER_OBJ_DIR)
@mkdir -p $(dir $@)
@echo "CC (loader) $<"
@$(LOADER_CC) $(LOADER_CFLAGS) -c $< -o $@
$(KERNEL_OBJ_DIR)/v4engine/v4_image.o: $(V4_IMAGE_C) | $(KERNEL_OBJ_DIR)
@mkdir -p $(dir $@)
@echo "CC (kernel) $<"
@$(CC) $(KERNEL_CFLAGS) -c $< -o $@
$(LOADER_OBJ_DIR)/v4system/%.o: v4/system/%.c | $(LOADER_OBJ_DIR)
@mkdir -p $(dir $@)
@echo "CC (loader) $<"
@$(LOADER_CC) $(LOADER_CFLAGS) -c $< -o $@
$(KERNEL_OBJ_DIR)/v4system/%.o: v4/system/%.c | $(KERNEL_OBJ_DIR)
@mkdir -p $(dir $@)
@echo "CC (kernel) $<"
@$(CC) $(KERNEL_CFLAGS) -c $< -o $@
$(LOADER_OBJ_DIR)/v4engine/%.o: v4/src/%.c | $(LOADER_OBJ_DIR)
@mkdir -p $(dir $@)
@echo "CC (loader) $<"
@$(LOADER_CC) $(LOADER_CFLAGS) -c $< -o $@
$(KERNEL_OBJ_DIR)/v4engine/%.o: v4/src/%.c | $(KERNEL_OBJ_DIR)
@mkdir -p $(dir $@)
@echo "CC (kernel) $<"
@$(CC) $(KERNEL_CFLAGS) -c $< -o $@
endif
# Compile loader assembly sources
$(LOADER_OBJ_DIR)/%.o: $(KERNEL_SRC)/%.S | $(LOADER_OBJ_DIR)
@mkdir -p $(dir $@)
@@ -1337,6 +1401,7 @@ info:
@echo "Loader output: $(LOADER_EFI)"
@echo "Kernel output: $(KERNEL_ELF)"
@echo "VM integration: $(STARFORTH_ENABLE_VM)"
@echo "StarForth v4: $(STARFORTH_V4)"
@echo "Monolithic: $(MONOLITHIC)"
help:
@@ -0,0 +1,21 @@
/* capsule_blocks.h -- the block format of a .4th capsule payload.
*
* A .4th capsule is text: "Block <num>" header lines, each followed by that
* block's content lines (tools/mkcapsule.c, validate_forth_blocks). This is
* the one place that says what a header line is. It depends on no VM, so
* every loader of capsules can use it: the kernel's and the hosted v4
* system's (docs/v4.0.0/NUCLEUS.md 5.3).
*/
#ifndef STARKERNEL_CAPSULE_BLOCKS_H
#define STARKERNEL_CAPSULE_BLOCKS_H
#include <stdint.h>
/* Is the line that starts at p a "Block <num>" header? If so, returns 1
* with the number in *out_num and the start of the next line in *out_after;
* otherwise returns 0 and changes nothing. `end` is one past the payload's
* last byte. */
int capsule_block_header(const uint8_t *p, const uint8_t *end,
uint32_t *out_num, const uint8_t **out_after);
#endif /* STARKERNEL_CAPSULE_BLOCKS_H */
+13
View File
@@ -0,0 +1,13 @@
/* sk_v4.h -- StarForth v4 on bare metal: one host node at a prompt.
*
* Built only with STARFORTH_V4=1 (Kconfig.kernel).
*/
#ifndef STARKERNEL_V4_SK_V4_H
#define STARKERNEL_V4_SK_V4_H
/* Start one StarForth v4 host node from the capsule image linked into the
* kernel, with its console on the kernel's console, and run it. Does not
* return. */
void sk_v4_run(void);
#endif /* STARKERNEL_V4_SK_V4_H */
+29
View File
@@ -0,0 +1,29 @@
/* capsule_blocks.c -- the block format of a .4th capsule payload.
* See capsule_blocks.h. Nothing here uses the C library or a VM.
*/
#include "starkernel/capsule_blocks.h"
int capsule_block_header(const uint8_t *p, const uint8_t *end,
uint32_t *out_num, const uint8_t **out_after)
{
const uint8_t *q;
uint32_t num = 0;
if ((uint64_t)(end - p) < 7u) return 0;
if (p[0] != 'B' || p[1] != 'l' || p[2] != 'o' || p[3] != 'c' || p[4] != 'k' || p[5] != ' ')
return 0;
q = p + 6;
if (q >= end || *q < '0' || *q > '9') return 0;
while (q < end && *q >= '0' && *q <= '9') {
num = num * 10u + (uint32_t)(*q - '0');
q++;
}
while (q < end && *q != '\n') q++;
if (q < end) q++;
*out_num = num;
*out_after = q;
return 1;
}
+11
View File
@@ -81,6 +81,9 @@ EFI_RUNTIME_SERVICES *g_sk_runtime_services = NULL;
log.h's line length (LOG_MSG_LINE_MAX, 256)
are distinct names */
#include "version.h"
#ifdef STARFORTH_V4
#include "starkernel/v4/sk_v4.h"
#endif
#endif
/* Forward declaration — kernel_main_deep contains everything from heartbeat
@@ -512,6 +515,14 @@ static void kernel_main_deep(BootInfo *boot_info) {
console_println("Kernel initialization complete.");
console_println("Boot successful!\n");
#ifdef STARFORTH_V4
/* StarForth v4 at a single prompt (Kconfig STARFORTH_V4): one host node
* of the F18-derived engine, in place of the v3 VM and everything below.
* It does not return. */
(void)boot_info;
sk_v4_run();
#endif
#ifdef STARFORTH_ENABLE_VM
/* Stadium: boot-time allocation (FABRIC-0.md item 3.2), before any VM
* exists (§6). Soft failure -- nothing downstream consumes the Stadium
+86
View File
@@ -0,0 +1,86 @@
/* sk_v4.c -- StarForth v4 on bare metal: one host node at a prompt.
*
* The node is the golden model of the F18-derived engine (v4/src). It comes
* up as the hosted v4 system does, by v4_boot_run (v4/include/v4/boot.h):
* the nucleus image, then the capsules from the directory baked into this
* kernel, each checked and its parity line printed, then the prompt.
* docs/v4.0.0/NUCLEUS.md. This file is all the kernel adds:
*
* - what the node prints goes to the kernel's console;
* - what is typed is given to the node a line at a time. The node does
* not send back what it reads -- on the mesh that is the console node's
* job -- so the line is edited here: characters are echoed, backspace
* rubs one out, and Enter hands the line over;
* - blocks are kept in memory, and are gone at power-off.
*
* Nothing of the v3 VM is started. docs/v4.0.0/DECOMPOSITION.md.
*/
#include "starkernel/v4/sk_v4.h"
#include "starkernel/console.h"
#include "v4/boot.h"
#define SK_V4_BLOCKS 64u
#define SK_V4_LINE 79u /* QUERY takes 80 characters: 79 and the new-line */
static v4_node sk_v4_node;
static v4_exec_state sk_v4_es;
static v4_heat sk_v4_heat;
static unsigned char sk_v4_disk[SK_V4_BLOCKS * V4_BLOCK_BYTES];
static char sk_v4_line[SK_V4_LINE + 1u];
static unsigned sk_v4_len;
/* One character from the keyboard. A finished line goes to the node. */
static void sk_v4_key(int c)
{
if (c == '\r' || c == '\n') {
console_putc('\n');
sk_v4_line[sk_v4_len++] = '\n';
(void)v4_node_console_feed(&sk_v4_node, sk_v4_line, sk_v4_len);
sk_v4_len = 0;
} else if (c == 8 || c == 127) {
if (sk_v4_len > 0) {
sk_v4_len--;
console_putc(8); console_putc(' '); console_putc(8);
}
} else if (c >= 32 && c < 127 && sk_v4_len < SK_V4_LINE) {
sk_v4_line[sk_v4_len++] = (char)c;
console_putc((char)c);
}
}
static void sk_v4_out(const char *text, unsigned len)
{
unsigned i;
for (i = 0; i < len; i++) console_putc(text[i]);
}
void sk_v4_run(void)
{
const v4_image *im = &v4_capsule_image;
v4_boot boot;
unsigned i;
console_println("StarForth v4: one host node, the F18-derived engine");
boot.n = &sk_v4_node; boot.es = &sk_v4_es; boot.h = &sk_v4_heat; boot.im = im; boot.out = sk_v4_out;
if (!v4_boot_run(&boot, sk_v4_disk, SK_V4_BLOCKS)) {
console_println("StarForth v4: not started");
for (;;) { }
}
for (;;) {
(void)v4_exec_step_word(&sk_v4_node, &sk_v4_es, &sk_v4_heat);
if (sk_v4_node.console_len) {
for (i = 0; i < sk_v4_node.console_len; i++) console_putc((char)sk_v4_node.console[i]);
sk_v4_node.console_len = 0;
}
if (sk_v4_node.stopped) {
console_println("StarForth v4: the node stopped on a fault");
for (;;) { }
}
if (v4_image_waiting(&sk_v4_node, im)) {
int c = console_getc();
if (c >= 0) sk_v4_key(c);
}
}
}