/* 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. You may obtain a copy of the License at: https://github.com/star.4th@proton.me/StarForth/LICENSE.txt This software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. See the License for the specific language governing permissions and limitations under the License. */ /** * stadium.c - The Stadium cell definitions (item 3.1) and boot-time * allocation (item 3.2). * * Also the translation unit that makes stadium.h's compile-time assertions * actually get compiled on all three architectures, not merely included by * something that never builds. */ #include "starkernel/vm/stadium.h" #ifdef __STARKERNEL__ #include "starkernel/kmalloc.h" #include "starkernel/pmm.h" #include "starkernel/console.h" static StadiumCell *stadium_cell_array = (StadiumCell *)0; static uint8_t *stadium_bitmap = (uint8_t *)0; static size_t stadium_ncells = 0; static int stadium_initialized = 0; /* Freestanding: no libc printf. Prints an unsigned decimal, no leading zeros. */ static void console_put_u64(uint64_t v) { char buf[21]; int i = 20; buf[20] = '\0'; if (v == 0) { console_puts("0"); return; } while (v > 0 && i > 0) { buf[--i] = (char)('0' + (v % 10)); v /= 10; } console_puts(&buf[i]); } int stadium_boot_init(void) { pmm_stats_t pmm = pmm_get_stats(); uint64_t budget_bytes = (pmm.free_bytes * (uint64_t)STADIUM_MEMORY_PERCENT) / 100u; size_t ncells = (size_t)(budget_bytes / STADIUM_CELL_BYTES); size_t bitmap_bytes = (ncells + 7u) / 8u; if (ncells == 0) { console_println("Stadium: boot-time allocation skipped (0 cells from memory budget)"); return -1; } StadiumCell *cells = (StadiumCell *)kmalloc(ncells * STADIUM_CELL_BYTES); uint8_t *bitmap = (uint8_t *)kmalloc(bitmap_bytes); if (!cells || !bitmap) { console_println("Stadium: kmalloc failed for boot-time allocation"); if (cells) kfree(cells); if (bitmap) kfree(bitmap); return -1; } { uint8_t *raw = (uint8_t *)cells; size_t n = ncells * STADIUM_CELL_BYTES; size_t i; for (i = 0; i < n; i++) raw[i] = 0; } { size_t i; for (i = 0; i < bitmap_bytes; i++) bitmap[i] = 0; } stadium_cell_array = cells; stadium_bitmap = bitmap; stadium_ncells = ncells; stadium_initialized = 1; console_puts("Stadium: "); console_put_u64((uint64_t)ncells); console_puts(" cells ("); console_put_u64((uint64_t)(ncells * STADIUM_CELL_BYTES) / 1024u); console_println(" KB)"); return 0; } int stadium_is_initialized(void) { return stadium_initialized; } size_t stadium_cell_count(void) { return stadium_ncells; } StadiumCell *stadium_cells(void) { return stadium_cell_array; } uint8_t *stadium_header_bitmap(void) { return stadium_bitmap; } /* * Exhaustive switch, no default: §13/§18.3 require the behaviour set to be * closed and fixed at build time. With -Wall -Werror, omitting a case here * for a tag that exists is a build failure, not a silent gap -- the compiler * enforces closedness, not just this comment. * * Handlers are stubs: the real actions belong to subsystems not yet migrated * onto the Stadium (Phase 4, §25.5). Nothing calls this yet either -- item * 3.5 is the first consumer. */ void stadium_dispatch(size_t cell_index, StadiumBehaviour behaviour) { console_puts("Stadium: dispatch cell="); console_put_u64((uint64_t)cell_index); console_puts(" behaviour="); switch (behaviour) { case STADIUM_BEHAVIOUR_MIGRATE: console_println("MIGRATE (stub)"); break; case STADIUM_BEHAVIOUR_DELIVER: console_println("DELIVER (stub)"); break; case STADIUM_BEHAVIOUR_EXPIRE: console_println("EXPIRE (stub)"); break; case STADIUM_BEHAVIOUR_COOL: console_println("COOL (stub)"); break; } } #endif /* __STARKERNEL__ */