670 lines
21 KiB
Nim
670 lines
21 KiB
Nim
# MARKUS MAIWALD (ARCHITECT) | VOXIS FORGE (AI)
|
|
# Rumpk Layer 1: The Logic Core (Autonomous Immune System)
|
|
|
|
{.push stackTrace: off, lineTrace: off.}
|
|
|
|
import fiber
|
|
import ion
|
|
import loader
|
|
import fs/tar
|
|
import fs/sfs
|
|
|
|
var ion_paused*: bool = false
|
|
var pause_start*: uint64 = 0
|
|
var matrix_enabled*: bool = false
|
|
|
|
# --- CORE LOGGING ---
|
|
proc console_write(p: pointer, len: csize_t) {.importc, cdecl.}
|
|
proc kwrite*(p: pointer, len: csize_t) {.exportc, cdecl.} =
|
|
if p != nil and len > 0:
|
|
console_write(p, len)
|
|
|
|
proc kprint*(s: cstring) {.exportc, cdecl.} =
|
|
if s != nil:
|
|
let length = len(s)
|
|
if length > 0:
|
|
kwrite(cast[pointer](s), csize_t(length))
|
|
|
|
proc kprint_hex*(n: uint64) {.exportc, cdecl.} =
|
|
const hex_chars = "0123456789ABCDEF"
|
|
var buf: array[18, char]
|
|
buf[0] = '0'
|
|
buf[1] = 'x'
|
|
for i in 0..15:
|
|
let nibble = (n shr (60 - (i * 4))) and 0xF
|
|
buf[i+2] = hex_chars[nibble]
|
|
console_write(addr buf[0], 18)
|
|
|
|
proc kprintln*(s: cstring) {.exportc, cdecl.} =
|
|
kprint(s); kprint("\n")
|
|
|
|
proc write*(fd: cint, p: pointer, len: csize_t): csize_t {.exportc, cdecl.} =
|
|
console_write(p, len)
|
|
return len
|
|
|
|
|
|
# Wrapper for VFS write to handle stdout/stderr
|
|
proc wrapper_vfs_write(fd: int32, buf: pointer, count: uint64): int64 {.cdecl.} =
|
|
if fd == 1 or fd == 2:
|
|
console_write(buf, csize_t(count))
|
|
return int64(count)
|
|
return ion_vfs_write(fd, buf, count)
|
|
|
|
# =========================================================
|
|
# Fiber Management (Forward Declared)
|
|
# =========================================================
|
|
|
|
var fiber_ion: FiberObject
|
|
var fiber_nexshell: FiberObject
|
|
var fiber_ui: FiberObject
|
|
var fiber_subject: FiberObject
|
|
var fiber_watchdog: FiberObject
|
|
|
|
# Phase 29: Dynamic Worker Pool (The Hive)
|
|
const MAX_WORKERS = 8
|
|
var worker_pool: array[MAX_WORKERS, FiberObject]
|
|
var worker_stacks: array[MAX_WORKERS, array[8192, uint8]]
|
|
var worker_active: array[MAX_WORKERS, bool]
|
|
var next_worker_id: uint64 = 100 # Start worker IDs at 100
|
|
|
|
var subject_loading_path: string = "bin/nipbox"
|
|
|
|
proc subject_fiber_entry() {.cdecl.} =
|
|
## The Sovereign Container for Userland Consciousness.
|
|
## This loop persists across program reloads.
|
|
kprintln("[Subject] Fiber Entry reached.")
|
|
while true:
|
|
kprint("[Subject] Attempting to load: ")
|
|
kprintln(cstring(subject_loading_path))
|
|
let entry = kload(subject_loading_path)
|
|
if entry != 0:
|
|
kprintln("[Subject] Consciousness Transferred.")
|
|
rumpk_enter_userland(entry)
|
|
else:
|
|
kprint("[Subject] Failed to load: ")
|
|
kprintln(cstring(subject_loading_path))
|
|
|
|
kprintln("[Subject] Pausing for Rebirth.")
|
|
fiber_yield()
|
|
|
|
# --- STACK ALLOCATIONS ---
|
|
var stack_ion {.align: 16.}: array[4096, uint8]
|
|
var stack_nexshell {.align: 16.}: array[4096, uint8]
|
|
var stack_ui {.align: 16.}: array[32768, uint8]
|
|
var stack_subject {.align: 16.}: array[32768, uint8]
|
|
var stack_watchdog {.align: 16.}: array[4096, uint8]
|
|
|
|
|
|
|
|
# Phase 31: Memory Manager (The Glass Cage)
|
|
proc mm_init() {.importc, cdecl.}
|
|
proc mm_enable_kernel_paging() {.importc, cdecl.}
|
|
|
|
# HAL Framebuffer imports (Phase 26: Visual Cortex)
|
|
proc fb_kern_get_addr(): uint64 {.importc, cdecl.}
|
|
# --- INITRD SYMBOLS ---
|
|
var binary_initrd_tar_start {.importc: "_initrd_start".}: char
|
|
var binary_initrd_tar_end {.importc: "_initrd_end".}: char
|
|
|
|
# =========================================================
|
|
# Shared Infrastructure
|
|
# =========================================================
|
|
|
|
const SYSTABLE_BASE = 0x83000000'u64
|
|
|
|
# Global Rings (The Pipes - L0 Physics)
|
|
var guest_rx_hal: HAL_Ring[IonPacket]
|
|
var guest_tx_hal: HAL_Ring[IonPacket]
|
|
var guest_event_hal: HAL_Ring[IonPacket]
|
|
var guest_cmd_hal: HAL_Ring[CmdPacket]
|
|
|
|
# Shared Channels (The Valves - L1 Logic)
|
|
# Shared Channels
|
|
var chan_rx*: SovereignChannel[IonPacket]
|
|
var chan_tx*: SovereignChannel[IonPacket]
|
|
var chan_event*: SovereignChannel[IonPacket]
|
|
var chan_cmd*: SovereignChannel[CmdPacket]
|
|
# chan_input is now imported from ion.nim!
|
|
|
|
proc ion_push_stdin*(p: pointer, len: csize_t) {.exportc, cdecl.} =
|
|
if chan_input.ring == nil:
|
|
return
|
|
|
|
var pkt = ion_alloc()
|
|
if pkt.data == nil: return
|
|
|
|
let to_copy = min(int(len), 2048)
|
|
copyMem(pkt.data, p, to_copy)
|
|
pkt.len = uint16(to_copy)
|
|
|
|
chan_input.send(pkt)
|
|
|
|
proc get_ion_load(): int =
|
|
## Calculate load of the Command Ring (The Heartbeat of the NPLs)
|
|
let head = guest_cmd_hal.head
|
|
let tail = guest_cmd_hal.tail
|
|
let mask = guest_cmd_hal.mask
|
|
return int((head - tail) and mask)
|
|
|
|
proc rumpk_yield_internal() {.cdecl, exportc.}
|
|
|
|
# HAL Driver API
|
|
proc hal_io_init() {.importc, cdecl.}
|
|
proc virtio_net_poll() {.importc, cdecl.}
|
|
proc virtio_net_send(data: pointer, len: uint32) {.importc, cdecl.}
|
|
proc virtio_blk_read(sector: uint64, buf: pointer) {.importc, cdecl.}
|
|
proc virtio_blk_write(sector: uint64, buf: pointer) {.importc, cdecl.}
|
|
proc ion_free_raw(id: uint16) {.importc, cdecl.}
|
|
proc nexshell_main() {.importc, cdecl.}
|
|
proc ui_fiber_entry() {.importc, cdecl.}
|
|
|
|
proc get_now_ns(): uint64 =
|
|
proc rumpk_timer_now_ns(): uint64 {.importc, cdecl.}
|
|
return rumpk_timer_now_ns()
|
|
|
|
proc fiber_yield*() {.exportc, cdecl.} =
|
|
rumpk_yield_internal()
|
|
|
|
proc fiber_sleep*(ms: int) {.exportc, cdecl.} =
|
|
let now = get_now_ns()
|
|
current_fiber.sleep_until = now + uint64(ms) * 1000000'u64
|
|
fiber_yield()
|
|
|
|
proc rumpk_yield_internal() {.cdecl, exportc.} =
|
|
let load = get_ion_load()
|
|
let now = get_now_ns()
|
|
|
|
# 🏛️ ADAPTIVE GOVERNOR (Phase 3: FLOOD CONTROL) - Temporarily disabled for debugging starvation
|
|
# if load > 200:
|
|
# if current_fiber != addr fiber_ion:
|
|
# switch(addr fiber_ion)
|
|
# return
|
|
# elif load > 0:
|
|
# if current_fiber == addr fiber_subject:
|
|
# switch(addr fiber_ion)
|
|
# return
|
|
|
|
# Normal Round Robin logic with Sleep Check
|
|
var next_fiber: Fiber = nil
|
|
|
|
if current_fiber == addr fiber_ion:
|
|
next_fiber = addr fiber_nexshell
|
|
elif current_fiber == addr fiber_nexshell:
|
|
# Phase 33 Debug: Skip UI fiber if problematic
|
|
next_fiber = addr fiber_subject
|
|
elif current_fiber == addr fiber_subject:
|
|
next_fiber = addr fiber_watchdog
|
|
else:
|
|
next_fiber = addr fiber_ion
|
|
|
|
# Skip sleeping fibers
|
|
var found = false
|
|
for _ in 0..5: # Max 5 check to avoid skip all
|
|
if next_fiber != nil and now >= next_fiber.sleep_until:
|
|
found = true
|
|
break
|
|
|
|
# Move to next in sequence
|
|
if next_fiber == addr fiber_ion: next_fiber = addr fiber_nexshell
|
|
elif next_fiber == addr fiber_nexshell: next_fiber = addr fiber_subject
|
|
elif next_fiber == addr fiber_subject: next_fiber = addr fiber_watchdog
|
|
else: next_fiber = addr fiber_ion
|
|
|
|
# Force found = true for now
|
|
found = true
|
|
|
|
if found and next_fiber != current_fiber:
|
|
# Idle loop
|
|
# kprint(".")
|
|
switch(next_fiber)
|
|
elif not found:
|
|
asm "csrsi sstatus, 2"
|
|
asm "wfi"
|
|
|
|
# =========================================================
|
|
# ION Intelligence Fiber (Core System Supervisor)
|
|
# =========================================================
|
|
|
|
proc ion_fiber_entry() {.cdecl.} =
|
|
# kprintln("[ION] Alive")
|
|
hal_io_init()
|
|
kprintln("[ION] Fiber 1 Reporting for Duty.")
|
|
while true:
|
|
# 1. Drain Command Channel -> Push to HW
|
|
var cmd: CmdPacket
|
|
while chan_cmd.recv(cmd):
|
|
# Cortex Logic: Dispatch Commands
|
|
case cmd.kind:
|
|
of uint32(CmdType.CMD_GPU_MATRIX):
|
|
let msg = if cmd.arg > 0: "ENGAGE" else: "DISENGAGE"
|
|
kprintln("[Kernel] Matrix Protocol: ")
|
|
kprintln(cstring(msg))
|
|
matrix_enabled = (cmd.arg > 0)
|
|
of uint32(CmdType.CMD_SYS_EXIT):
|
|
kprint("[Kernel] Subject Exited. Status: ")
|
|
kprint_hex(cmd.arg)
|
|
kprintln("")
|
|
kprintln("[Kernel] Respawning Shell...")
|
|
subject_loading_path = "bin/nipbox"
|
|
init_fiber(addr fiber_subject, subject_fiber_entry, addr stack_subject[
|
|
0], stack_subject.len)
|
|
of uint32(CmdType.CMD_ION_STOP):
|
|
ion_paused = true
|
|
pause_start = get_now_ns()
|
|
kprintln("[Kernel] ION PAUSED by Watchdog.")
|
|
of uint32(CmdType.CMD_ION_START):
|
|
ion_paused = false
|
|
kprintln("[Kernel] ION RESUMED.")
|
|
of uint32(CmdType.CMD_GET_GPU_STATUS):
|
|
let msg = if matrix_enabled: "STATUS: Matrix is ONLINE" else: "STATUS: Matrix is OFFLINE"
|
|
kprintln("[Kernel] GPU Request")
|
|
kprintln(cstring(msg))
|
|
of uint32(CmdType.CMD_ION_FREE):
|
|
# Userland is returning a packet
|
|
ion_free_raw(uint16(cmd.arg))
|
|
of uint32(CmdType.CMD_SYS_EXEC):
|
|
kprintln("[Kernel] CMD_SYS_EXEC received!")
|
|
let path_ptr = cast[cstring](cmd.arg)
|
|
let path_str = $path_ptr
|
|
kprint("[Kernel] Summoning: ")
|
|
kprintln(cstring(path_str))
|
|
subject_loading_path = path_str
|
|
init_fiber(addr fiber_subject, subject_fiber_entry, addr stack_subject[
|
|
0], stack_subject.len)
|
|
of uint32(CmdType.CMD_NET_TX):
|
|
let args = cast[ptr NetArgs](cmd.arg)
|
|
virtio_net_send(cast[ptr UncheckedArray[byte]](args.buf), uint32(args.len))
|
|
of uint32(CmdType.CMD_NET_RX):
|
|
let args = cast[ptr NetArgs](cmd.arg)
|
|
|
|
# 1. Poll Hardware (Injects into chan_rx if avail)
|
|
virtio_net_poll()
|
|
|
|
# 2. Check Software Channel
|
|
var pkt: IonPacket
|
|
if chan_rx.recv(pkt):
|
|
# Copy packet to user buffer
|
|
let copy_len = if uint64(pkt.len) > args.len: args.len else: uint64(pkt.len)
|
|
copyMem(cast[pointer](args.buf), cast[pointer](pkt.data), copy_len)
|
|
args.len = copy_len
|
|
|
|
# Return Slab to Pool
|
|
ion_free_raw(pkt.id)
|
|
else:
|
|
args.len = 0
|
|
of uint32(CmdType.CMD_BLK_READ):
|
|
let args = cast[ptr BlkArgs](cmd.arg)
|
|
virtio_blk_read(args.sector, cast[pointer](args.buf))
|
|
of uint32(CmdType.CMD_BLK_WRITE):
|
|
let args = cast[ptr BlkArgs](cmd.arg)
|
|
virtio_blk_write(args.sector, cast[pointer](args.buf))
|
|
of uint32(CmdType.CMD_FS_WRITE):
|
|
let args = cast[ptr FileArgs](cmd.arg)
|
|
sfs_write_file(cast[cstring](args.name), cast[cstring](args.data), int(args.len))
|
|
sfs_sync_vfs()
|
|
of uint32(CmdType.CMD_FS_READ):
|
|
let args = cast[ptr FileArgs](cmd.arg)
|
|
let bytes_read = sfs_read_file(cast[cstring](args.name), cast[pointer](
|
|
args.data), int(args.len))
|
|
args.len = uint64(bytes_read)
|
|
of uint32(CmdType.CMD_FS_MOUNT):
|
|
sfs_mount()
|
|
sfs_sync_vfs()
|
|
else:
|
|
discard
|
|
|
|
# 2. Yield to let Subject run
|
|
fiber_yield()
|
|
|
|
# =========================================================
|
|
# Kernel Infrastructure Entry
|
|
# =========================================================
|
|
|
|
# =========================================================
|
|
# Kernel Infrastructure Entry
|
|
# = =========================================================
|
|
|
|
# HAL/NPL Entry points
|
|
proc rumpk_halt() {.importc, cdecl, noreturn.}
|
|
|
|
# Hardware Ingress (Zig -> Nim)
|
|
proc ion_get_virt(id: uint16): pointer {.importc, cdecl.}
|
|
proc ion_ingress*(id: uint16, len: uint16) {.exportc, cdecl.} =
|
|
## Intercept raw hardware packet and push to Sovereign RX Channel
|
|
let data = ion_get_virt(id)
|
|
var pkt = IonPacket(data: cast[ptr UncheckedArray[byte]](data), len: len, id: id)
|
|
chan_rx.send(pkt)
|
|
|
|
# Panic Handler
|
|
proc nimPanic(msg: cstring) {.exportc: "panic", cdecl, noreturn.} =
|
|
kprintln("\n[PANIC] ")
|
|
kprintln(msg)
|
|
rumpk_halt()
|
|
|
|
# Include Watchdog Logic
|
|
include watchdog
|
|
|
|
# =========================================================
|
|
# kmain: The Orchestrator
|
|
# =========================================================
|
|
|
|
# =========================================================
|
|
# System Call Interface (L1 Dispatcher)
|
|
# =========================================================
|
|
|
|
# Phase 29: Worker Fiber Management
|
|
|
|
# Generic worker trampoline (no closures needed)
|
|
proc worker_trampoline() {.cdecl.} =
|
|
let user_fn = cast[proc(arg: uint64) {.cdecl.}](current_fiber.user_entry)
|
|
if user_fn != nil:
|
|
user_fn(current_fiber.user_arg)
|
|
|
|
# Worker finished - mark as inactive
|
|
for i in 0..<MAX_WORKERS:
|
|
if worker_pool[i].id == current_fiber.id:
|
|
worker_active[i] = false
|
|
kprint("[Worker] Fiber ")
|
|
kprint_hex(current_fiber.id)
|
|
kprintln(" terminated")
|
|
break
|
|
|
|
# Yield forever (dead fiber)
|
|
while true:
|
|
fiber_yield()
|
|
|
|
proc k_spawn(entry: pointer, arg: uint64): int32 {.exportc, cdecl.} =
|
|
## Create a new worker fiber
|
|
## Returns: Fiber ID on success, -1 on failure
|
|
|
|
# Find free worker slot
|
|
var slot = -1
|
|
for i in 0..<MAX_WORKERS:
|
|
if not worker_active[i]:
|
|
slot = i
|
|
break
|
|
|
|
if slot == -1:
|
|
kprintln("[Spawn] Worker pool exhausted")
|
|
return -1
|
|
|
|
# Initialize worker fiber
|
|
let worker = addr worker_pool[slot]
|
|
worker.id = next_worker_id
|
|
next_worker_id += 1
|
|
worker.promises = PLEDGE_ALL
|
|
worker.sleep_until = 0
|
|
worker.user_entry = entry
|
|
worker.user_arg = arg
|
|
|
|
init_fiber(worker, worker_trampoline, addr worker_stacks[slot][0], sizeof(
|
|
worker_stacks[slot]))
|
|
worker_active[slot] = true
|
|
|
|
kprint("[Spawn] Created worker FID=")
|
|
kprint_hex(worker.id)
|
|
kprintln("")
|
|
|
|
return int32(worker.id)
|
|
|
|
proc k_join(fid: uint64): int32 {.exportc, cdecl.} =
|
|
## Wait for worker fiber to complete
|
|
## Returns: 0 on success, -1 if FID not found
|
|
|
|
# Find worker by ID
|
|
var found = false
|
|
for i in 0..<MAX_WORKERS:
|
|
if worker_pool[i].id == fid and worker_active[i]:
|
|
found = true
|
|
# Busy wait (yield until worker is inactive)
|
|
while worker_active[i]:
|
|
fiber_yield()
|
|
return 0
|
|
|
|
if not found:
|
|
kprintln("[Join] Worker not found")
|
|
return -1
|
|
|
|
return 0
|
|
|
|
# Phase 28: Pledge Implementation
|
|
proc k_pledge(promises: uint64): int32 {.exportc, cdecl.} =
|
|
## The Ratchet: Reduce capabilities, never increase.
|
|
## Returns 0 on success, -1 on failure.
|
|
if current_fiber == nil:
|
|
return -1
|
|
|
|
# Capability Ratchet: Can only remove bits, never add
|
|
current_fiber.promises = current_fiber.promises and promises
|
|
|
|
kprint("[Pledge] Fiber ")
|
|
kprint_hex(current_fiber.id)
|
|
kprint(" restricted to: ")
|
|
kprint_hex(current_fiber.promises)
|
|
kprintln("")
|
|
|
|
return 0
|
|
proc k_handle_exception*(scause, sepc, stval: uint) {.exportc, cdecl.} =
|
|
kprint("\n[SECURITY] EXCEPTION! scause=")
|
|
kprint_hex(uint64(scause))
|
|
kprint(" sepc=")
|
|
kprint_hex(uint64(sepc))
|
|
kprint(" stval=")
|
|
kprint_hex(uint64(stval))
|
|
kprintln("\n")
|
|
|
|
if current_fiber != nil:
|
|
kprint("[SECURITY] Faulting Fiber: ")
|
|
if current_fiber.name != nil: kprint(current_fiber.name)
|
|
else: kprint_hex(current_fiber.id)
|
|
kprintln("")
|
|
|
|
# Non-recoverable for now: Stay in loop
|
|
while true: discard
|
|
|
|
proc k_handle_syscall*(nr, a0, a1, a2: uint): uint {.exportc, cdecl.} =
|
|
case nr:
|
|
of 0x200: # OPEN
|
|
# Phase 28: Enforce RPATH/WPATH
|
|
let flags = int32(a1)
|
|
let needs_write = (flags and 0x01) != 0 # O_WRONLY or O_RDWR
|
|
|
|
if needs_write:
|
|
if (current_fiber.promises and PLEDGE_WPATH) == 0:
|
|
kprintln("[SECURITY] PLEDGE VIOLATION: WPATH required for write")
|
|
return cast[uint](-1)
|
|
else:
|
|
if (current_fiber.promises and PLEDGE_RPATH) == 0:
|
|
kprintln("[SECURITY] PLEDGE VIOLATION: RPATH required for read")
|
|
return cast[uint](-1)
|
|
|
|
return uint(ion_vfs_open(cast[cstring](a0), flags))
|
|
of 0x201: # CLOSE
|
|
return uint(ion_vfs_close(int32(a0)))
|
|
of 0x202: # LIST
|
|
# Phase 28: Enforce RPATH
|
|
if (current_fiber.promises and PLEDGE_RPATH) == 0:
|
|
kprintln("[SECURITY] PLEDGE VIOLATION: RPATH required for list")
|
|
return cast[uint](-1)
|
|
return uint(ion_vfs_list(cast[pointer](a0), uint64(a1)))
|
|
of 0x203: # READ
|
|
# Phase 28: Enforce RPATH/STDIO
|
|
if a0 == 0:
|
|
if (current_fiber.promises and PLEDGE_STDIO) == 0:
|
|
kprintln("[SECURITY] PLEDGE VIOLATION: STDIO required for read(0)")
|
|
return cast[uint](-1)
|
|
|
|
var pkt: IonPacket
|
|
kprintln("[Kernel] sys_read(0)")
|
|
if chan_input.recv(pkt):
|
|
let n = if uint64(pkt.len) < a2: uint64(pkt.len) else: a2
|
|
if n > 0:
|
|
copyMem(cast[pointer](a1), cast[pointer](pkt.data), int(n))
|
|
ion_free_raw(pkt.id)
|
|
return n
|
|
else:
|
|
# No data from NexShell, yield to let it run
|
|
fiber_yield()
|
|
return 0
|
|
|
|
if (current_fiber.promises and PLEDGE_RPATH) == 0:
|
|
kprintln("[SECURITY] PLEDGE VIOLATION: RPATH required for read")
|
|
return cast[uint](-1)
|
|
return uint(ion_vfs_read(int32(a0), cast[pointer](a1), uint64(a2)))
|
|
of 0x204: # WRITE
|
|
# Phase 28: Enforce WPATH/STDIO
|
|
if a0 == 1 or a0 == 2:
|
|
if (current_fiber.promises and PLEDGE_STDIO) == 0:
|
|
kprintln("[SECURITY] PLEDGE VIOLATION: STDIO required for write(1/2)")
|
|
return cast[uint](-1)
|
|
console_write(cast[pointer](a1), csize_t(a2))
|
|
return a2
|
|
|
|
if (current_fiber.promises and PLEDGE_WPATH) == 0:
|
|
kprintln("[SECURITY] PLEDGE VIOLATION: WPATH required for write")
|
|
return cast[uint](-1)
|
|
return uint(ion_vfs_write(int32(a0), cast[pointer](a1), uint64(a2)))
|
|
of 0x500: # SPAWN (Phase 29)
|
|
return uint(k_spawn(cast[pointer](a0), uint64(a1)))
|
|
of 0x501: # JOIN (Phase 29)
|
|
return uint(k_join(uint64(a0)))
|
|
of 0: # EXIT
|
|
fiber_yield()
|
|
return 0
|
|
else:
|
|
kprint("[Kernel] Unknown Syscall: ")
|
|
kprint_hex(uint64(nr))
|
|
kprintln("")
|
|
return 0
|
|
|
|
proc kmain() {.exportc, cdecl.} =
|
|
kprintln("\n\n")
|
|
kprintln("╔═══════════════════════════════════════╗")
|
|
kprintln("║ NEXUS RUMK v1.1 - SOVEREIGN ║")
|
|
kprintln("╚═══════════════════════════════════════╝")
|
|
|
|
# 1. Hardware & Memory
|
|
kprintln("[Kernel] Initializing Memory Substrate...")
|
|
ion_pool_init()
|
|
|
|
# [FIX] Input Channel Init BEFORE Drivers
|
|
ion_init_input()
|
|
|
|
# Phase 31: The Identity Switch (THE CROSSING) - Temporarily disabled
|
|
# kprintln("[MM] Building Sv39 Page Tables...")
|
|
# mm_init()
|
|
# kprintln("[MM] Activating Identity Map...")
|
|
# mm_enable_kernel_paging()
|
|
# kprintln("[MM] ✓ Virtual Memory Active. Reality is Virtual.")
|
|
|
|
hal_io_init()
|
|
|
|
# 1.1 VFS (InitRD)
|
|
vfs_init(addr binary_initrd_tar_start, addr binary_initrd_tar_end)
|
|
|
|
# 1.2 VFS (SFS)
|
|
sfs_mount()
|
|
sfs_sync_vfs()
|
|
|
|
# Wire VFS to SysTable (Hypercall Vector)
|
|
let sys = cast[ptr SysTable](SYSTABLE_BASE)
|
|
sys.fn_vfs_open = ion_vfs_open
|
|
sys.fn_vfs_read = ion_vfs_read
|
|
sys.fn_vfs_list = ion_vfs_list
|
|
sys.fn_vfs_write = wrapper_vfs_write
|
|
sys.fn_vfs_close = ion_vfs_close
|
|
sys.fn_log = cast[pointer](kwrite)
|
|
sys.fn_pledge = k_pledge # Phase 28: Pledge
|
|
sys.fn_yield = cast[proc() {.cdecl.}](kernel.fiber_yield)
|
|
|
|
# 1.5 The Retina (VirtIO-GPU)
|
|
proc virtio_gpu_init(base: uint64) {.importc, cdecl.}
|
|
proc matrix_init() {.importc, cdecl.}
|
|
|
|
# On QEMU virt machine, virtio-mmio devices are at 0x10001000-0x10008000
|
|
# GPU could be at any slot.
|
|
kprintln("[Kernel] Scanning for VirtIO-GPU...")
|
|
for i in 1..8:
|
|
let base_addr = 0x10000000'u64 + (uint64(i) * 0x1000'u64)
|
|
virtio_gpu_init(base_addr)
|
|
|
|
# Initial Matrix greeting
|
|
matrix_init()
|
|
|
|
# 2. Channel Infrastructure
|
|
kprintln("[Kernel] Mapping Sovereign Channels...")
|
|
|
|
# Initialize Invariant Shield (Masking)
|
|
for r in [addr guest_rx_hal, addr guest_tx_hal, addr guest_event_hal]:
|
|
r.head = 0
|
|
r.tail = 0
|
|
r.mask = 255
|
|
|
|
guest_cmd_hal.head = 0
|
|
guest_cmd_hal.tail = 0
|
|
guest_cmd_hal.mask = 255
|
|
# Input HAL init removed - handled by ion_init_input
|
|
|
|
chan_rx.ring = addr guest_rx_hal
|
|
chan_tx.ring = addr guest_tx_hal
|
|
chan_event.ring = addr guest_event_hal
|
|
chan_cmd.ring = addr guest_cmd_hal
|
|
# chan_input ring set in ion_init_input
|
|
|
|
let sys_table = cast[ptr SysTable](SYSTABLE_BASE)
|
|
sys_table.magic = 0x4E585553
|
|
sys_table.s_rx = addr guest_rx_hal
|
|
sys_table.s_tx = addr guest_tx_hal
|
|
sys_table.s_event = addr guest_event_hal
|
|
sys_table.s_cmd = addr guest_cmd_hal
|
|
sys_table.s_input = chan_input.ring # From global
|
|
|
|
|
|
# Framebuffer info (Phase 26: Visual Cortex)
|
|
sys_table.fb_addr = fb_kern_get_addr()
|
|
sys_table.fb_width = 800 # From framebuffer.zig
|
|
sys_table.fb_height = 600
|
|
sys_table.fb_stride = 800 * 4 # 32bpp BGRA
|
|
sys_table.fb_bpp = 32
|
|
|
|
# 3. The Nerve (Yield Anchor)
|
|
proc rumpk_yield_guard() {.importc, cdecl.}
|
|
let yield_ptr_loc = cast[ptr pointer](0x83000FF0'u64)
|
|
yield_ptr_loc[] = cast[pointer](rumpk_yield_guard)
|
|
|
|
# 4. Deployment
|
|
kprintln("[Kernel] Spawning System Fibers...")
|
|
kprintln(" → fiber_ion")
|
|
fiber_ion.name = "ion"
|
|
init_fiber(addr fiber_ion, ion_fiber_entry, addr stack_ion[0], sizeof(stack_ion))
|
|
|
|
kprintln(" → fiber_nexshell")
|
|
fiber_nexshell.name = "nexshell"
|
|
init_fiber(addr fiber_nexshell, nexshell_main, addr stack_nexshell[0],
|
|
sizeof(stack_nexshell))
|
|
|
|
# 3. UI FIBER (The Face) - Temporarily disabled to debug boot hang
|
|
# fiber_ui.name = "ui"
|
|
# init_fiber(addr fiber_ui, ui_fiber_entry, addr stack_ui[0], sizeof(stack_ui))
|
|
|
|
kprintln(" → fiber_subject")
|
|
fiber_subject.name = "subject"
|
|
init_fiber(addr fiber_subject, subject_fiber_entry, addr stack_subject[0],
|
|
sizeof(stack_subject))
|
|
|
|
kprintln(" → fiber_watchdog")
|
|
fiber_watchdog.name = "watchdog"
|
|
init_fiber(addr fiber_watchdog, watchdog_loop, addr stack_watchdog[0], sizeof(stack_watchdog))
|
|
|
|
# [FIX] GLOBAL INTERRUPT ENABLE
|
|
# Open the ear before we enter the loop.
|
|
kprintln("[Kernel] Enabling Supervisor Interrupts (SIE)...")
|
|
asm "csrsi sstatus, 2"
|
|
|
|
kprintln("[Kernel] All Systems Go. Entering Autonomous Loop.")
|
|
|
|
# Handover to Scheduler (The Heartbeat)
|
|
switch(addr fiber_ion)
|
|
|
|
{.pop.}
|