/* StarForth — Steady-State Virtual Machine Runtime Copyright (c) 2023–2025 Robert A. James All rights reserved. Licensed under the StarForth License, Version 1.0 */ #ifndef __STARKERNEL__ #error "capsule_console.c is kernel-only" #endif #include "starkernel/capsule_console.h" #include "starkernel/capsule.h" #include "starkernel/capsule_birth.h" #include "starkernel/xxhash64.h" #include "starkernel/kmalloc.h" #include /* Real, minimal, working content -- just enough vocabulary to send/ * receive messages (common:messaging.4th, same as every other VM). * No COMMON-CH subscription: a console's own traffic is direct 1:1 * with its paired user VM (CONSOLE-CMD-EVENT), not broadcast, so * there's no need to resolve an index in Hermes's own routing table * for it. Needs the "Block NNNN" header capsule_exec_payload() * requires (§F.18) -- not optional. 4997 is an unused block number. */ static const char CONSOLE_IDENTITY_SRC[] = "Block 4997\n" "S\" common:messaging.4th\" EXEC\n" "MSG-CD-INIT\n"; CapsuleRunResult capsule_console_birth(const char *console_name, VMUuid parent, VMUuid *out_vm_id, void **out_vm_ctx) { if (!console_name) return CAPSULE_RUN_ERR_INVALID; size_t source_len = sizeof(CONSOLE_IDENTITY_SRC) - 1u; uint8_t *arena = (uint8_t *)kmalloc(source_len); if (!arena) return CAPSULE_RUN_ERR_STILLBORN; memcpy(arena, CONSOLE_IDENTITY_SRC, source_len); /* Heap-built single-entry directory -- same shape RUNCAP already * established (§F.6/§F.18); never freed, matches that precedent * (a VM's IDENTITY exec reads directly from this arena). */ CapsuleNameEntry name_entry; memset(name_entry.name, 0, sizeof(name_entry.name)); { size_t n = strlen(console_name); if (n >= CAPSULE_NAME_MAX) n = CAPSULE_NAME_MAX - 1u; memcpy(name_entry.name, console_name, n); } CapsuleDesc desc; memset(&desc, 0, sizeof(desc)); desc.magic = CAPSULE_MAGIC_PACK(CAPSULE_VERSION_0, CAPSULE_HASH_XXHASH64); desc.content_hash = xxhash64_capsule(arena, source_len); desc.capsule_id = desc.content_hash; desc.offset = 0; desc.length = source_len; desc.flags = CAPSULE_FLAG_ACTIVE | CAPSULE_FLAG_PRODUCTION; desc.owner_vm = 0; desc.birth_count = 0; desc.created_ns = 0; CapsuleDirHeader dir; memset(&dir, 0, sizeof(dir)); dir.magic = CAPSULE_DIR_MAGIC; dir.arena_base = (uint64_t)(uintptr_t)arena; dir.arena_size = source_len; dir.desc_count = 1; dir.desc_capacity = 1; dir.name_count = 1; dir.dir_hash = 0; CapsuleRunResult r = capsule_birth_baby( console_name, &dir, &desc, &name_entry, arena, parent, 1 /* skip_pki_sig -- not build-time content, same rationale as RUNCAP */, out_vm_id, out_vm_ctx); /* capsule_birth_baby() never sets the registry entry's own .name -- * found live in RUNCAP (§F.18), same fix needed here. */ if (r == CAPSULE_RUN_OK && out_vm_id) { capsule_vm_registry_set_name(*out_vm_id, console_name); } return r; }