Files
LithosAnanake/kernel/src/capsule/capsule_vm_hooks.c
T
rajamesandJunie a8b70e88d3 Reorganize source tree: kernel/, v3/, v4/ split and board infrastructure
Source tree reorganization:
- Move StarForth v3 engine to v3/ (src/, include/, Makefile)
- Move kernel to kernel/ (src/, include/, linker/, Makefile)
- Create v4/ skeleton for F18-ISA golden model (DECOMPOSITION.md, JUSTIFICATION.md)
- Move FABRIC-0..4.md to docs/fabric/
- Move ONTOLOGY.md and ROADMAP.md to docs/

Board infrastructure:
- Add boards/ser5/, boards/raspi/, boards/milkv/, boards/zynq7020/
- Each board has board.mk (ISA, CPU flags, boot recipe) and README.md
- Root Makefile becomes thin dispatcher: boot_image, all, clean, docs take TARGET
- make boot_image TARGET=SER5|RASPI|MILKV builds one GPT/MBR image per board
- ZYNQ7020 target exists but stops with clear error (ARMv7 port not built yet)
- scripts/mkdiskimage.sh builds disk images for all boards

Docs pipeline:
- docs/book/ with LaTeX master (main.tex) and Makefile
- pandoc converts Markdown to LaTeX at build time
- Two Lua filters: table-widths.lua (wide tables wrap), code-breaks.lua (inline code breaks)
- make docs builds single PDF (754 pages, 0 missing characters)
- make docs TARGET=<board> adds board appendix
- build/docs/<book|board>/meta.tex stamps git commit into PDF

Bug fixes:
- 42 include paths that only worked by accident now use correct relative paths
- clang-18 hardcode replaced with configurable CC variable (fixed aarch64 build)
- Pi 5: kernel_2712.img linked at 0x80000, .bss zeroed, memory reserved
- Doxyfile, .clang-tidy, README.md, Kconfig paths updated

Verified:
- Hosted v3 build passes 1012 tests, 0 failures
- SER5 image boots in QEMU (OVMF), POST passes, K exact (65536 = Q48_ONE)
- Milk-V image boots in QEMU (OpenSBI + U-Boot + bootefi), POST passes
- make clean TARGET=<board> removes only that board and its ISA objects
- make all builds all boards, hosted v3, and docs in one run

Co-authored-by: Junie <junie@jetbrains.com>
2026-10-01 15:40:09 -04:00

128 lines
4.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
StarForth — Steady-State Virtual Machine Runtime
Copyright (c) 2023–2025 Robert A. James
All rights reserved.
This file is part of the StarForth project.
Licensed under the StarForth License, Version 1.0 (the "License");
you may not use this file except in compliance with the License.
*/
/**
* capsule_vm_hooks.c - VM Hook Implementations for Capsule Birth (M7.1)
*
* Concrete implementations of the three function pointers required by
* capsule_birth_set_hooks():
*
* capsule_exec_hook -- wraps vm_interpret()
* capsule_dict_hash_hook -- xxhash64 walk of the live dictionary
* capsule_vm_alloc_hook -- allocates and initialises a fresh VM
*
* Call capsule_vm_hooks_register() once during VM bootstrap (after
* register_forth79_words) to wire everything up.
*/
#include "starkernel/capsule_birth.h"
#include "starkernel/capsule_loader.h"
#include "starkernel/xxhash64.h"
#include "vm.h"
#include "platform_alloc.h"
#include "word_source/include/mama_forth_words.h"
#ifdef __STARKERNEL__
#include "starkernel/hal/hal.h" /* sk_hal_host_services() */
#endif
/*===========================================================================
* exec hook ( vm_ctx, code, code_len ) -> 0 on success
*===========================================================================*/
static int capsule_exec_hook(void *vm_ctx, const char *code, uint64_t code_len) {
return capsule_exec_payload(vm_ctx, (const uint8_t *)code, code_len);
}
/*===========================================================================
* dict hash hook ( vm_ctx ) -> uint64_t
*
* Walks the live dictionary linked list and hashes each word's name and
* execution heat. Provides a cheap, deterministic snapshot of dictionary
* state for parity logging.
*===========================================================================*/
static uint64_t capsule_dict_hash_hook(void *vm_ctx) {
VM *vm = (VM *)vm_ctx;
if (!vm) return 0;
XXHash64State state;
xxhash64_reset(&state, 0);
DictEntry *entry = vm->latest;
while (entry) {
/* Hash: word name */
xxhash64_update(&state, entry->name, entry->name_len);
/* Hash: execution heat (captures runtime activity level) */
xxhash64_update(&state, &entry->execution_heat,
sizeof(entry->execution_heat));
entry = entry->link;
}
return xxhash64_digest(&state);
}
/*===========================================================================
* vm alloc hook () -> void* (VM*)
*
* Allocates a fresh VM on the heap, initialises it fully (including word
* registration and physics subsystems), and returns it as an opaque pointer.
* Returns NULL on allocation failure.
*===========================================================================*/
static void *capsule_vm_alloc_hook(void) {
VM *vm = (VM *)sf_malloc(sizeof(VM));
if (!vm) return (void *)0;
#ifdef __STARKERNEL__
vm_init_with_host(vm, sk_hal_host_services());
#else
vm_init(vm);
#endif
/* vm_init sets vm->error if memory allocation failed internally */
if (vm->error) {
vm_cleanup(vm);
sf_free(vm);
return (void *)0;
}
/* Enable interpreter — child VMs skip sk_vm_bootstrap_parity, must set manually */
vm_enable_interpreter(vm);
/* Register STOP + EXEC via the dedicated function in mama_forth_words.c.
* Do NOT pass mama_word_stop / mama_word_exec as function-pointer arguments
* here: cross-TU function-pointer loads generate R_X86_64_REX_GOTPCRELX,
* which the PE32+ linker resolves by dereferencing the function address
* (reading machine-code bytes) instead of loading the address itself. */
register_child_vm_words(vm);
return (void *)vm;
}
/*===========================================================================
* Registration
*===========================================================================*/
/**
* capsule_vm_hooks_register - Wire the three hooks into the capsule subsystem.
*
* Must be called after vm_init() completes on Mama's VM, before any capsule
* birth or experiment operations.
*/
void capsule_vm_hooks_register(void) {
capsule_birth_set_hooks(
capsule_exec_hook,
capsule_dict_hash_hook,
capsule_vm_alloc_hook
);
}