feat(kernel): v4 boots in the kernel's boot -- the Stadium, its checks, the opcode patrons

#189, ENGINE step 4, Phase 1 (plan 2026-11-13-engine-step4-phase1), Tasks
1-4. Captain Bob, 2026-11-13: Q1 "A new node key", Q2 "Per heartbeat
tick", Q3 "All of v3's", Q4 "In Phase 1" / "Full births now", who runs
next "Hera by compudynamics now" (Task 5, to come).

- stadium.c/.h: stadium_node_key(node) ("NODE" | number),
  stadium_boot_init_for(first), stadium_birth_node(key, name);
  stadium_boot_init() and stadium_birth_hera() are them for Hera's key, so
  v3's path is as it was (only an impossible panic's words changed).
- kernel_main.c: the system is chosen and said first, then the Stadium,
  sessions, switch-signal and kernel-Hermes tables for both systems in
  v3's order; v4: Hera, node 10, patron zero under her node key, the word,
  block and opcode layers. The quota self-test and the heartbeat's line
  are functions now, called by both systems.
- sk_v4.c/.h: sk_v4_run() goes -- sk_v4_boot() returns, the kernel's
  checks after POST (opcodes, words, conservation, quota, heartbeat), then
  sk_v4_prompt(). sk_v4_charge_opcodes() at the heartbeat's service points
  (the key wait, after each line) and once after POST.
- stadium_ops.c/.h: a node's 32 opcodes as Stadium patrons (HEAT-OP's
  feed, "Both"): retirements charged as the word layer's dispatches, a
  quantum each, the reservoir's floor kept, shared by retirements when
  short; cooling per tick as words. ~0.5 us a charge on the host: kept.
- qemu_session.py: a v4 acceptance boot fails without "Stadium: N cells",
  "Stadium conservation: CONSERVED" and the heartbeat's ticks (#189).

Tests first (logs/20261113-122142): the physics check failed on the v4
boot; test_stadium_node, test_stadium_ops (and its share case, after a
real boot showed ";" taking all the heat) failed before. Now: kernel host
tests all pass; amd64 v4 boot passes, POST 539/539, dict_hash unchanged,
"Stadium opcodes: 30 resident, hottest opcode 8", CONSERVED; a system=v3
boot prints its Stadium lines as on 2026-11-06.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-13 12:50:02 +00:00
parent 3f4988e19e
commit b1cac339c1
35 changed files with 959 additions and 85 deletions
+117 -63
View File
@@ -16,7 +16,7 @@
* M0-M6: Complete (build, boot, PMM, VMM, interrupts, timer, kmalloc) -- v1.5.1-FINAL
* M7: Complete (the StarForth VM integrated, parity validated)
* M7.1: In progress (capsule birth, the Mama vocabulary, the Tripod fleet, the word ACL)
* The system booted by default is v4, StarForth's F18 engine (sk_v4_run); system=v3 boots v3
* The system booted by default is v4, StarForth's F18 engine (sk_v4_boot, sk_v4_prompt); system=v3 boots v3
* (docs/fabric/FABRIC-3.md XLIII).
*/
@@ -59,6 +59,7 @@ EFI_RUNTIME_SERVICES *g_sk_runtime_services = NULL;
#include "starkernel/vm/bootstrap/sk_vm_bootstrap.h"
#include "starkernel/vm/parity.h"
#include "starkernel/vm/stadium.h"
#include "starkernel/vm/stadium_ops.h"
#include "starkernel/vm/stadium_words.h"
#include "starkernel/vm/stadium_blocks.h"
#include "starkernel/vm/kernel_hermes.h"
@@ -88,6 +89,9 @@ EFI_RUNTIME_SERVICES *g_sk_runtime_services = NULL;
#include "starkernel/v4/sk_v4.h"
#endif
#endif
#ifndef SK_V4_HERA_NODE
#define SK_V4_HERA_NODE 10u /* a kernel without v4 never keys the Stadium by node (#189) */
#endif
/* Forward declaration — kernel_main_deep contains everything from heartbeat
* init onward. The 2 MB BSS stack is set up by kernel_entry.S before
@@ -606,6 +610,71 @@ static void sk_v4_block_chain(void)
*
* @param boot_info The @c BootInfo passed from @c kernel_main_impl().
*/
/* The boot's checks after the system is up, the same on both systems (#189, plan
* 2026-11-13-engine-step4-phase1, Task 2; Q3: "All of v3's"). They were inline in
* kernel_main_deep, v3's path; v4's calls them too, under Hera's node key. */
/* item 4.1a self-test: stadium_grant_quota() with a synthetic identity, from `parent`'s quota. */
#ifdef STARFORTH_ENABLE_VM
static void sk_boot_quota_selftest(VMUuid parent, const char *name) {
{
VMUuid test_id;
test_id.hi = 0;
test_id.lo = 1; /* distinct from vm_uuid_hera() (all-zero) and
* vm_uuid_none() (all-ones) */
int grant_rc = stadium_grant_quota(test_id, parent);
console_puts("Stadium quota grant self-test: ");
console_println(grant_rc == 0 ? "OK" : "REFUSED");
if (grant_rc == 0) {
console_puts(" "); console_puts(name); print_uint(" reservoir=", stadium_reservoir_peek(parent));
print_uint(" test-vm reservoir=", stadium_reservoir_peek(test_id));
}
}
}
#endif
/* Phase 0 acceptance (section 25.1 item 0.10): a few real ticks, then the heartbeat's line. */
static void sk_boot_heartbeat_line(void) {
{
uint64_t wait_start = heartbeat_ticks();
uint64_t spins = 0;
while (heartbeat_ticks() - wait_start < 3 && spins < 100000000ULL) {
arch_relax();
spins++;
}
}
console_puts("Heartbeat: ");
{
char buf[24]; uint64_t v = heartbeat_ticks(); int i = 0, j = 0; char t[24];
if (v == 0) buf[i++] = '0';
else { while (v > 0) { t[j++] = (char)('0' + (v % 10)); v /= 10; } while (j > 0) buf[i++] = t[--j]; }
buf[i] = '\0';
console_puts(buf);
}
console_puts(" ticks, trust=0x");
{
char buf[9]; uint32_t v = (uint32_t)heartbeat_trust();
for (int k = 7; k >= 0; k--) {
int nib = (int)((v >> (k * 4)) & 0xF);
buf[7 - k] = (char)(nib < 10 ? '0' + nib : 'a' + nib - 10);
}
buf[8] = '\0';
console_puts(buf);
}
console_puts(", variance=0x");
{
char buf[9]; uint32_t v = (uint32_t)heartbeat_state()->variance;
for (int k = 7; k >= 0; k--) {
int nib = (int)((v >> (k * 4)) & 0xF);
buf[7 - k] = (char)(nib < 10 ? '0' + nib : 'a' + nib - 10);
}
buf[8] = '\0';
console_puts(buf);
}
console_println("");
}
static void kernel_main_deep(BootInfo *boot_info) {
/* M5: Initialize heartbeat subsystem */
console_println("Heartbeat: init...");
@@ -631,25 +700,25 @@ static void kernel_main_deep(BootInfo *boot_info) {
* Task 3). An ISO, CD or ESP-image boot is not a stick. */
console_println(boot_info->boot_disk.is_stick ? "Boot disk: a StarshipOS stick"
: "Boot disk: not a StarshipOS stick");
/* Which system runs, worked out before the Stadium is made: its first quota is keyed by it --
* Hera's all-zero key for v3, the first node's for v4 (#189, plan 2026-11-13-engine-step4-phase1,
* Q1: "A new node key"). Said first, as before. */
int sk_use_v4 = 0;
#ifdef STARFORTH_V4
{
const int v3_refused = boot_info->boot_disk.is_stick && boot_info->args.system == KARGS_SYSTEM_V3;
if (boot_info->args.system == KARGS_SYSTEM_V4 || v3_refused ||
(boot_info->args.system == KARGS_SYSTEM_DEFAULT && SK_V4_DEFAULT)) {
sk_use_v4 = boot_info->args.system == KARGS_SYSTEM_V4 || v3_refused ||
(boot_info->args.system == KARGS_SYSTEM_DEFAULT && SK_V4_DEFAULT);
if (sk_use_v4)
console_println(v3_refused ? "System: v3 is not on this stick -- StarForth v4"
: boot_info->args.system == KARGS_SYSTEM_V4
? "System: StarForth v4 (system=v4)"
: "System: StarForth v4 (this build's default)");
/* StarForth v4 at a single prompt: one host node of the F18-derived
* engine, in place of the v3 VM and everything below. It does not
* return. */
sk_v4_boot_info = boot_info;
sk_v4_run(sk_v4_block_chain, &boot_info->boot_disk);
else
console_println(boot_info->args.system == KARGS_SYSTEM_V3
? "System: StarForth v3 (system=v3)"
: "System: StarForth v3 (this build's default; system=v4 for v4)");
}
}
console_println(boot_info->args.system == KARGS_SYSTEM_V3
? "System: StarForth v3 (system=v3)"
: "System: StarForth v3 (this build's default; system=v4 for v4)");
#else
console_println(boot_info->args.system == KARGS_SYSTEM_V4
? "System: StarForth v3 -- system=v4 asked for, and this kernel has no v4"
@@ -657,10 +726,13 @@ static void kernel_main_deep(BootInfo *boot_info) {
#endif
#ifdef STARFORTH_ENABLE_VM
/* The Stadium and the kernel's tables are the kernel's, for both systems, in v3's order (#189,
* Phase 1 Task 1; Q3: "All of v3's"). */
/* Stadium: boot-time allocation (FABRIC-0.md item 3.2), before any VM
* exists (§6). Soft failure -- nothing downstream consumes the Stadium
* yet, so a failed allocation logs and boot continues. */
(void)stadium_boot_init();
(void)(sk_use_v4 ? stadium_boot_init_for(stadium_node_key(SK_V4_HERA_NODE)) /* #189: keyed by node */
: stadium_boot_init());
/* Session: boot-time allocation (FABRIC-2.md §H.12 step 4), sized from
* stadium_max_vm_count() so it must run after stadium_boot_init() above
@@ -693,6 +765,36 @@ static void kernel_main_deep(BootInfo *boot_info) {
* table just above. Publish (task 3.3) enqueues here; nothing drains
* it yet (task 3.4). */
(void)sk_hermes_queues_boot_init();
#endif
#ifdef STARFORTH_V4
if (sk_use_v4) {
#ifdef STARFORTH_ENABLE_VM
/* Hera, the first node, is patron zero under her node key; then the word and block layers,
* as on v3's path below (#189). */
(void)stadium_birth_node(stadium_node_key(SK_V4_HERA_NODE), "Hera");
stadium_words_init();
stadium_blocks_init();
stadium_ops_init(); /* the node's 32 opcode patrons (#189) */
#endif
/* StarForth v4: one host node of the F18-derived engine, in place of
* the v3 VM, brought up in the kernel's boot (#189, Phase 1): the
* node and POST, then the kernel's checks as v3's, then the prompt,
* which does not return. */
sk_v4_boot_info = boot_info;
sk_v4_boot(sk_v4_block_chain, &boot_info->boot_disk);
#ifdef STARFORTH_ENABLE_VM
sk_v4_charge_opcodes(); /* what POST retired */
stadium_ops_print_boot_diagnostics(stadium_node_key(SK_V4_HERA_NODE));
stadium_words_print_boot_diagnostics(stadium_node_key(SK_V4_HERA_NODE));
sk_boot_quota_selftest(stadium_node_key(SK_V4_HERA_NODE), "Hera");
#endif
sk_boot_heartbeat_line();
sk_v4_prompt();
}
#endif
#ifdef STARFORTH_ENABLE_VM
/* item 4.1, FABRIC-0.md item 3.6/§17.7: actually enforce "Hera is patron
* zero" before anything else can land on cell 0 via the free list, then
@@ -986,19 +1088,7 @@ static void kernel_main_deep(BootInfo *boot_info) {
* capsule birth (item 0.1 pruned automatic Hermes birth from init.4th;
* restoring it is item 4.2's job, not this one's). Diagnostic only --
* the synthetic VM is never used for anything else. */
{
VMUuid test_id;
test_id.hi = 0;
test_id.lo = 1; /* distinct from vm_uuid_hera() (all-zero) and
* vm_uuid_none() (all-ones) */
int grant_rc = stadium_grant_quota(test_id, vm_uuid_hera());
console_puts("Stadium quota grant self-test: ");
console_println(grant_rc == 0 ? "OK" : "REFUSED");
if (grant_rc == 0) {
print_uint(" Hera reservoir=", stadium_reservoir_peek(vm_uuid_hera()));
print_uint(" test-vm reservoir=", stadium_reservoir_peek(test_id));
}
}
sk_boot_quota_selftest(vm_uuid_hera(), "Hera");
/* FABRIC-3.6.md task 2.2 (item 28) self-test: sk_hermes_alloc()'s
* heat-coupled allocate, against its own synthetic VM (lo=3, distinct
@@ -1347,43 +1437,7 @@ static void kernel_main_deep(BootInfo *boot_info) {
* (not a virtual-tick construct; §16.4/§18.5 govern patron state, not
* this one-time boot diagnostic) rather than reporting whatever count
* happened to land. */
{
uint64_t wait_start = heartbeat_ticks();
uint64_t spins = 0;
while (heartbeat_ticks() - wait_start < 3 && spins < 100000000ULL) {
arch_relax();
spins++;
}
}
console_puts("Heartbeat: ");
{
char buf[24]; uint64_t v = heartbeat_ticks(); int i = 0, j = 0; char t[24];
if (v == 0) buf[i++] = '0';
else { while (v > 0) { t[j++] = (char)('0' + (v % 10)); v /= 10; } while (j > 0) buf[i++] = t[--j]; }
buf[i] = '\0';
console_puts(buf);
}
console_puts(" ticks, trust=0x");
{
char buf[9]; uint32_t v = (uint32_t)heartbeat_trust();
for (int k = 7; k >= 0; k--) {
int nib = (int)((v >> (k * 4)) & 0xF);
buf[7 - k] = (char)(nib < 10 ? '0' + nib : 'a' + nib - 10);
}
buf[8] = '\0';
console_puts(buf);
}
console_puts(", variance=0x");
{
char buf[9]; uint32_t v = (uint32_t)heartbeat_state()->variance;
for (int k = 7; k >= 0; k--) {
int nib = (int)((v >> (k * 4)) & 0xF);
buf[7 - k] = (char)(nib < 10 ? '0' + nib : 'a' + nib - 10);
}
buf[8] = '\0';
console_puts(buf);
}
console_println("");
sk_boot_heartbeat_line();
/* FABRIC-3.md §XXVIII, Stage 3 (2026-09-13): register the Tripod fleet
* as preemptive-switch-signal participants now, only after all three
+26 -5
View File
@@ -18,8 +18,8 @@
* and a console exists only while Zuse's drive is attached and checked
* (sk_v4_identity.c; ENGINE.md 2, ruled 2026-10-20).
*
* This is the lone node, started before the kernel's fleet and beside it.
* It goes when the node is brought up in the VM's place (ENGINE.md step 4).
* The kernel brings it up in its own boot, after the Stadium (#189, ENGINE.md step 4,
* Phase 1): sk_v4_boot(), the kernel's checks after POST, then sk_v4_prompt().
*
* Nothing of the v3 VM is started. docs/v4.0.0/DECOMPOSITION.md.
*/
@@ -33,6 +33,10 @@
#include "starkernel/timer.h"
#include "v4/boot.h"
#include "block_subsystem.h"
#ifdef STARFORTH_ENABLE_VM
#include "starkernel/vm/stadium.h"
#include "starkernel/vm/stadium_ops.h"
#endif
#define SK_V4_LINE 1024u /* what the line editor here takes: a line, as the boot takes one (v4/system/boot.c) */
@@ -52,6 +56,16 @@ static char sk_v4_text[SK_V4_LINE + 1u];
* above them, and the kernel's real chain is set up afterwards. */
static uint8_t sk_v4_post_blk_ram[BLK_RAM_BLOCKS * BLK_FORTH_SIZE];
/* HEAT-OP into the Stadium (#189; Captain Bob: "Per heartbeat tick"): the node's opcode counts are
* charged to its opcode patrons at the heartbeat's service points -- where its host waits for a key,
* a tick at a time, and after each line. */
void sk_v4_charge_opcodes(void)
{
#ifdef STARFORTH_ENABLE_VM
stadium_ops_charge(stadium_node_key(SK_V4_HERA_NODE), sk_v4_heat.op, heartbeat_ticks());
#endif
}
static void sk_v4_out(const char *text, unsigned len)
{
unsigned i;
@@ -123,6 +137,7 @@ static int sk_v4_readline(void)
} else {
heartbeat_service();
}
sk_v4_charge_opcodes();
/* Nothing typed: wait for the next interrupt, not in a spin (#151 -- the spin kept a
* CPU core 100% busy at the prompt, and the demo's host with it). The heartbeat's
* timer wakes it at 100 Hz, so a key is seen within a tick, as the blink and the
@@ -148,10 +163,11 @@ static int sk_v4_readline(void)
}
}
void sk_v4_run(void (*chain)(void), const BootDisk *boot_disk)
{
v4_boot boot;
/* The node's boot record: sk_v4_boot() fills it, sk_v4_prompt() hands it lines. */
static v4_boot boot;
void sk_v4_boot(void (*chain)(void), const BootDisk *boot_disk)
{
console_println("StarForth v4: one host node, the F18-derived engine");
sk_v4_image = v4_map_image;
boot.n = &sk_v4_node; boot.es = &sk_v4_es; boot.h = &sk_v4_heat; boot.im = &sk_v4_image;
@@ -177,7 +193,10 @@ void sk_v4_run(void (*chain)(void), const BootDisk *boot_disk)
console_fb_mirror(1);
console_fb_enable_ttf();
#endif
}
void sk_v4_prompt(void)
{
/* The prompt is the host's: read a line, hand it to the node, say how it
* ended. */
for (;;) {
@@ -185,6 +204,8 @@ void sk_v4_run(void (*chain)(void), const BootDisk *boot_disk)
int how;
int logged_out = 0;
sk_v4_charge_opcodes(); /* what the last line retired (#189) */
#if !EMERGENCY_CONSOLE_ENABLED
/* no identity, no console: headless until Zuse's drive is checked.
* The drives are looked at first, now: one pulled while the last
+1 -1
View File
@@ -6,7 +6,7 @@
#include "starkernel/arch.h"
/* Interrupts off, then halt for ever: the core sleeps instead of spinning at 100% (#177), and no
* timer or device wakes it to do anything. sk_v4_run's three stops each said why first. */
* timer or device wakes it to do anything. sk_v4_boot's and sk_v4_prompt's stops (sk_v4_run's, until #189's Phase 1) each say why first. */
void sk_v4_stop(void)
{
arch_disable_interrupts();
+21 -6
View File
@@ -183,7 +183,18 @@ static void console_put_u64(uint64_t v) {
console_puts(&buf[i]);
}
VMUuid stadium_node_key(uint32_t node) {
VMUuid k;
k.hi = STADIUM_NODE_KEY_HI;
k.lo = (uint64_t)node;
return k;
}
int stadium_boot_init(void) {
return stadium_boot_init_for(vm_uuid_hera());
}
int stadium_boot_init_for(VMUuid first) {
/* Budgeted from kmalloc_get_stats(), not pmm_get_stats() (corrected
* 2026-08-15 -- see this function's doc in stadium.h): kmalloc_init()
* (M6) already carved its own fixed-size heap out of PMM before this
@@ -283,7 +294,7 @@ int stadium_boot_init(void) {
quotas[i].consumed = 0;
}
}
quotas[0].vm_id = vm_uuid_hera();
quotas[0].vm_id = first; /* Hera on v3's path, the first node on v4's (#189) */
quotas[0].in_use = 1;
quotas[0].free_head = 0;
quotas[0].free_count = ncells;
@@ -811,6 +822,10 @@ int stadium_conserved(VMUuid vm_id) {
* must not break that.
*/
int stadium_birth_hera(void) {
return stadium_birth_node(vm_uuid_hera(), "Hera");
}
int stadium_birth_node(VMUuid key, const char *name) {
StadiumPatronHeader candidate;
size_t idx;
@@ -831,19 +846,19 @@ int stadium_birth_hera(void) {
candidate.flags = 0;
candidate.behaviour = (uint8_t)STADIUM_BEHAVIOUR_COOL;
idx = stadium_admit(vm_uuid_hera(), &candidate);
idx = stadium_admit(key, &candidate);
if (idx == STADIUM_CELL_NONE) return -1; /* refused; should not happen (quota is fresh and empty) */
if (idx != STADIUM_HERA_CELL_INDEX) {
sk_hal_panic("Stadium: birth_hera did not land on cell 0 -- patron-zero invariant broken");
sk_hal_panic("Stadium: the first patron did not land on cell 0 -- patron-zero invariant broken");
}
{
Session *s = session_register(vm_uuid_hera(), vm_uuid_hera(), "Hera");
Session *s = session_register(key, key, name);
if (s) {
s->stadium_cell = idx;
session_set_pinned(vm_uuid_hera(), 1);
session_set_pinned(key, 1);
} else {
console_println("Stadium: session_register failed for Hera (session.c not yet initialized?)");
console_println("Stadium: session_register failed for the first patron (session.c not yet initialized?)");
}
}
+174
View File
@@ -0,0 +1,174 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* stadium_ops.c -- a v4 node's opcodes as Stadium patrons. See stadium_ops.h (#189). */
#include "starkernel/vm/stadium_ops.h"
#ifdef __STARKERNEL__
#include "starkernel/vm/stadium.h"
#include "starkernel/console.h"
#include "starkernel/kmalloc.h"
#include "starkernel/q48_16.h"
#include "starforth_config.h" /* STADIUM_WORD_HEAT_QUANTUM, STADIUM_WORD_COOL_RATE_Q48 */
typedef struct {
size_t cell_index; /* STADIUM_CELL_NONE: not resident */
uint64_t last_tick; /* when it was last cooled */
uint64_t last_count; /* the opcode's retirements at the last charge */
} StadiumOpSlot;
static StadiumOpSlot **op_slots = (StadiumOpSlot **)0; /* [quota slot][opcode] */
static int ops_initialized = 0;
void stadium_ops_init(void)
{
size_t count = stadium_max_vm_count(), slot;
unsigned k;
if (ops_initialized) return;
if (count == 0) { console_println("Stadium opcodes: init skipped (no Stadium)"); return; }
op_slots = (StadiumOpSlot **)kmalloc(count * sizeof(StadiumOpSlot *));
if (!op_slots) { console_println("Stadium opcodes: kmalloc failed"); return; }
for (slot = 0; slot < count; slot++) {
op_slots[slot] = (StadiumOpSlot *)kmalloc(STADIUM_OPS_COUNT * sizeof(StadiumOpSlot));
if (!op_slots[slot]) {
size_t j;
console_println("Stadium opcodes: kmalloc failed");
for (j = 0; j < slot; j++) kfree(op_slots[j]);
kfree(op_slots);
op_slots = (StadiumOpSlot **)0;
return;
}
for (k = 0; k < STADIUM_OPS_COUNT; k++) {
op_slots[slot][k].cell_index = STADIUM_CELL_NONE;
op_slots[slot][k].last_tick = 0;
op_slots[slot][k].last_count = 0;
}
}
ops_initialized = 1;
}
/* The opcode's cell, if it is still its own (the word layer's self-healing lookup). */
static size_t op_cell(int slot, unsigned k)
{
size_t cell = op_slots[slot][k].cell_index;
const uint8_t *bm = stadium_header_bitmap();
if (cell == STADIUM_CELL_NONE) return STADIUM_CELL_NONE;
if (cell >= stadium_cell_count() || !bm || !((bm[cell / 8u] >> (cell % 8u)) & 1u)
|| stadium_cells()[cell].header.identity != (STADIUM_OPS_IDENTITY | k)) {
op_slots[slot][k].cell_index = STADIUM_CELL_NONE;
return STADIUM_CELL_NONE;
}
return cell;
}
void stadium_ops_charge(VMUuid vm_id, const uint64_t counts[STADIUM_OPS_COUNT], uint64_t tick)
{
int slot;
unsigned k;
uint64_t retired[STADIUM_OPS_COUNT], total = 0, want, pullable, floor = Q48_ONE / 3, avail;
const uint64_t q = (uint64_t)STADIUM_WORD_HEAT_QUANTUM;
if (!ops_initialized) return;
slot = stadium_quota_slot_for_vm(vm_id);
if (slot < 0) return;
/* First: every resident opcode cools by the ticks since, as a word does (a fraction of its own
* heat a tick, back to the reservoir); and each opcode's retirements since the last charge. */
for (k = 0; k < STADIUM_OPS_COUNT; k++) {
StadiumOpSlot *o = &op_slots[slot][k];
size_t cell = op_cell(slot, k);
retired[k] = counts[k] - o->last_count;
o->last_count = counts[k];
total = (total + retired[k] < total) ? UINT64_MAX : total + retired[k];
if (cell != STADIUM_CELL_NONE) {
StadiumPatronHeader *h = &stadium_cells()[cell].header;
uint64_t elapsed = tick - o->last_tick;
if (elapsed > 0) {
uint64_t cooled = ((h->heat * (uint64_t)STADIUM_WORD_COOL_RATE_Q48) >> 16) * elapsed;
if (cooled > h->heat) cooled = h->heat;
if (cooled > 0) { h->heat -= cooled; stadium_reservoir_push(vm_id, cooled); }
}
o->last_tick = tick;
}
}
if (total == 0) return;
/* Then: the retirements are that many of the word layer's dispatches -- a quantum each, never
* leaving the reservoir below a third. When they want more than there is, what there is is
* shared by retirements, not by the order the opcodes are taken in (#189). */
avail = stadium_reservoir_peek(vm_id);
pullable = avail > floor ? avail - floor : 0;
want = total > UINT64_MAX / q ? UINT64_MAX : total * q;
{
unsigned shift = 0;
while ((total >> shift) >= ((uint64_t)1 << 32)) shift++; /* pullable * share fits 64 bits */
for (k = 0; k < STADIUM_OPS_COUNT; k++) {
StadiumOpSlot *o = &op_slots[slot][k];
uint64_t give;
size_t cell;
if (retired[k] == 0) continue;
give = want <= pullable ? retired[k] * q
: (total >> shift) ? pullable * (retired[k] >> shift) / (total >> shift) : 0;
give = stadium_reservoir_pull(vm_id, give);
cell = op_cell(slot, k);
if (cell != STADIUM_CELL_NONE) { stadium_cells()[cell].header.heat += give; continue; }
/* not resident: admitted on its first retirements, with what they pull */
{
StadiumPatronHeader c;
uint8_t *raw = (uint8_t *)&c;
size_t i, idx;
for (i = 0; i < sizeof c; i++) raw[i] = 0;
c.identity = STADIUM_OPS_IDENTITY | k;
c.heat = give;
c.link = 0;
c.contains = STADIUM_CONTAINS_NONE;
c.mass = 1;
c.behaviour = (uint8_t)STADIUM_BEHAVIOUR_COOL;
idx = stadium_admit(vm_id, &c);
if (idx == STADIUM_CELL_NONE) { stadium_reservoir_push(vm_id, give); continue; }
o->cell_index = idx;
o->last_tick = tick;
}
}
}
}
uint64_t stadium_ops_heat(VMUuid vm_id, unsigned op)
{
int slot;
size_t cell;
if (!ops_initialized || op >= STADIUM_OPS_COUNT) return 0;
slot = stadium_quota_slot_for_vm(vm_id);
if (slot < 0) return 0;
cell = op_cell(slot, op);
return cell == STADIUM_CELL_NONE ? 0 : stadium_cells()[cell].header.heat;
}
void stadium_ops_print_boot_diagnostics(VMUuid vm_id)
{
unsigned k, resident = 0, hottest = 0;
uint64_t best = 0;
char line[64];
unsigned at = 0;
const char *p;
for (k = 0; k < STADIUM_OPS_COUNT; k++) {
uint64_t h = stadium_ops_heat(vm_id, k);
if (h > 0) resident++;
if (h > best) { best = h; hottest = k; }
}
for (p = "Stadium opcodes: "; *p; p++) line[at++] = *p;
if (resident >= 10) line[at++] = (char)('0' + resident / 10);
line[at++] = (char)('0' + resident % 10);
for (p = " resident, hottest opcode "; *p; p++) line[at++] = *p;
if (hottest >= 10) line[at++] = (char)('0' + hottest / 10);
line[at++] = (char)('0' + hottest % 10);
line[at] = 0;
console_println(line);
}
#endif /* __STARKERNEL__ */