Morgana_VM

Two small interpreters — morgana-x86 and morgana-a64 — written in pure KernRift as a strict test instrument for ApexRift. Not a general-purpose emulator, and not an alternative to QEMU.

$ printf 'uname\nmem\npoweroff\n' | ./scripts/x86-vs-qemu.sh apexrift.img
[morgana-x86] 3,519,734 steps → clean poweroff @ rip 0x00000000004e9756
IDENTICAL: 1137 bytes (compiled in 98.30 ms ✓)
14.8k lines of pure KernRift across 10 files in src/ — zero C, zero libc, zero external crates
98 ms to compile morgana-x86 from source (20 ms for morgana-a64) on desktop x86_64
Byte-exact serial transcripts vs QEMU (x86_64 & arm64) and pixel-identical UEFI GOP frames vs OVMF
ARX morgana-a64 --preflight compiles into an arm64 ARX (<16 MiB) and runs on the phone itself

What It Is — and What It Refuses to Be

QEMU is fast and broad, which makes it an indispensable oracle — and a poor microscope when a bare-metal kernel hangs silently on a physical laptop or a phone with no serial port. Morgana exists to be the microscope: small enough to read end to end, strict enough to fail loudly, and held continuously against QEMU so it cannot drift into agreeing only with itself.

The Two Machines

Each target is a standalone KernRift binary with its own ISA decoder, MMU, interrupt controller, and device models.

morgana-x86 (x86_64)

Full 64-bit interpreter with 4-level paging, Ring 0/3 privilege transitions, int 0x80 & SYSCALL/SYSRET, Multiboot, and a built-in UEFI + GOP environment (--uefi=ESPDIR --gop) that boots ApexRift's real BOOTX64.EFI.

Modelled devices: 16550 UART, 8259 PIC, 8254 PIT, xAPIC / x2APIC, ACPI / PM timer, HPET, e1000, RTL8125, virtio-blk, ramfb, xHCI + USB hub/HID/CDC, and q35 / G815 PCI topologies.

morgana-a64 (AArch64)

64-bit AArch64 interpreter implementing EL0 / EL1, SVC, TTBR0/TTBR1 stage-1 MMU page walks, atomics/exclusives (LDXR/STXR, LSE), GICv3 CPU/distributor/redistributor, and the architectural generic timer.

Memory & firmware: Generates a real DTB for QEMU virt or handset layouts and backs guest RAM with 64 KiB demand-paged chunks (--ram-cap=) so a 4 GiB physical address space fits inside a 47 MiB arena on the phone.

Live Oracle Verification

x86_64 & arm64 — byte-for-byte against QEMU
# Full ApexRift x86_64 kernel: boot, uname, mem, poweroff
$ printf 'uname\nmem\npoweroff\n' | ./scripts/x86-vs-qemu.sh ../ApexRift/apexrift.img
IDENTICAL: 1137 bytes
# morgana-x86 compiled in 98.30 ms (71,241 tokens, 684,184 B), 3,519,734 steps

# AArch64 L3 stage-1 MMU page-walk witness against qemu-system-aarch64 (GICv3)
$ QEMU_SECS=3 EMU_STEPS=5000000 ./scripts/a64-vs-qemu.sh tests/fixtures/a64_mmu_l3_witness.kr
PASS: a64_mmu_l3_witness.kr -- qemu and morgana-a64 agree byte for byte
# morgana-a64 compiled in 20.29 ms (17,469 tokens, 203,528 B), 5,000,000 steps

For UEFI graphics, ./scripts/uefi-vs-ovmf.sh boots ApexRift's BOOTX64.EFI under both QEMU q35 + OVMF and morgana-x86 --uefi --gop, then diffs the rendered desktop framebuffer via scripts/fb-compare.py — requiring a pixel-identical first desktop frame.

Physical Digital Twins

Standard QEMU boards do not look like real consumer hardware. Morgana carries two machine profiles derived directly from hardware dumps so kernel bring-up bugs are caught before flashing or rebooting physical silicon.

Profile FlagPhysical MachineWhat Is Modelled From Hardware Captures
--machine=g815 ASUS ROG Strix G18 (G815LW) Captured ACPI tables, PCI bus layout, and boot-5 reports: xAPIC/x2APIC, HPET, xHCI + USB, RTL8125 NIC, VT-d, high-physical GOP framebuffer (>4 GiB).
--machine=begonia Redmi Note 8 Pro (MT6785) Handset physical memory window, clocks, GICv3, UART/SoC blocks, and 64 KiB demand-paged guest RAM with configurable --ram-cap=.

On-Device Stage-0 --preflight

On a phone with no UART header, a bad kernel flashed or loaded via kexec dies silently as a black screen. To eliminate blind flashes, scripts/a64-preflight-arx.sh compiles morgana-a64 into a standalone ApexRift arm64 ARX (<16 MiB) that runs inside ApexRift on the handset itself.

1. Stage Copy candidate kernel to ./staged.img on ArFS On Handset
2. Emulate Run morgana-a64 --preflight inside ApexRift (551M instructions, 47 MiB demand-paged arena) EL0 ARX
3. Verdict Writes an FNV-keyed preflight verdict bound to the staged image bytes before kexec is allowed Gate Pass

Measured on the Redmi Note 8 Pro's Cortex-A55: 551,000,000 emulated instructions to the interactive shell complete in ~19 minutes inside a 47 MiB arena — slow, deterministic, and completely independent of a host PC.

Source Layout & Mutation Campaigns

All 14,778 lines live in 10 files under src/. No code generator, no C FFI, no hidden runtime.

FileLinesRole
src/morgana-x86.kr5,341x86_64 ISA decoder, 4-level MMU, interrupts, PIC/PIT/APIC, e1000, virtio-blk, ramfb
src/morgana-a64.kr3,878AArch64 ISA decoder, TTBR0/TTBR1 stage-1 MMU, GICv3, generic timer, DTB, demand-paged RAM, --preflight
src/morgana-uefi.kr1,700UEFI Boot/Runtime Services, PE/COFF loader, memory map, ExitBootServices
src/morgana-xhci.kr1,341xHCI host controller (rings, doorbells, TRBs, transfer events)
src/morgana-g815.kr713ASUS ROG Strix G18 physical machine profile (ACPI, HPET, RTL8125, VT-d)
src/hostsys.kr499Direct Linux / ARX host syscall wrappers (file I/O, mmap, clocks, exit)
src/morgana-usb.kr354USB device models attached to xHCI
src/morgana-q35-gop.kr338UEFI Graphics Output Protocol (GOP) framebuffer & pixel dump
src/morgana-q35.kr330QEMU q35 PCI root complex & chipset registers
src/morgana-g815-pci.kr284G815 captured PCI configuration space topology

Build, Compare & Mutate

Morgana_VM — verification & non-vacuity scripts
$ git clone https://github.com/Pantelis23/Morgana_VM.git && cd Morgana_VM
$ ./scripts/x86-vs-qemu.sh /path/to/apexrift.img     # byte-exact x86_64 vs QEMU
$ ./scripts/a64-vs-qemu.sh tests/fixtures/a64_mmu_l3_witness.kr  # arm64 MMU vs QEMU
$ ./scripts/uefi-vs-ovmf.sh                          # pixel-identical UEFI GOP vs OVMF
$ ./scripts/a64-preflight-arx.sh                     # package on-device arm64 ARX

# Prove every gate row is non-vacuous (each mutation must force its gate to fail)
$ ./scripts/mutate-x86.sh
$ ./scripts/mutate-a64-dtb.sh
$ ./scripts/mutate-a64-machine.sh

Morgana_VM is free software under GPL-3.0-only. Full design notes and device-model logs live in docs/EMULATOR.md and docs/g815-provisional.md.

Bug Reports & Discussions