Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.
Ring -1 Kernel Surgeon — Architecture
Target Platform: x86-64 with AMD SVM + NPT (verified under nested
kvm_amd=1on QEMU/KVM + OVMF UEFI + VNC)
Safety Boundary: All tests run inside nested AMD SVM under QEMU/KVM (-cpu EPYC,+svm). The host OS is never modified or rebooted.
1. Core Concept
Ring -1 Kernel Surgeon is a bare-metal Type-1 AMD SVM hypervisor (Ring -1) that sits transparently underneath an unmodified Linux kernel (Ring 0).
When Ring 0 triggers a fatal exception (e.g., #PF NULL-pointer dereference or #UD trap in kernel mode), AMD SVM hardware traps into Ring -1 before Linux do_page_fault / oops / panic() can execute:
- Ring
-1walks the guest 4-level page tables (CR3) to resolve the faultingRIPto a guest physical address (GPA). - Ring
-1reads the raw faulting machine instruction and captures full guest CPU state (GPRs, CRs, RFLAGS, stack). - Ring
-1renders a live Surgical HUD Overlay directly into the shared linearefifb/vesafbframebuffer (visible over VNC, completely bypassing guest DRM/VT locks). - Ring
-1decides a live machine-code patch:- Stage 1 (Proof of Ring
-1): Deterministic x86-64 instruction-length decode (hv/x86_decode.c) + live physical-memory patching of the faulting instruction in guest.text(bypassing guestCR0.WPand read-only PTEs) with an exact-length0x90NOP sled. - Stage 2/3 (AI Surgeon):
surgeon_handle_fault()(hv/surgeon.c) invokes a freestanding bare-metal local LLM (MiniCPM5-2B Q4_K_Mpacked in.r1mover a 2 GiB read-only NVDIMM, executed byhv/llm_engine.cacross a 24-core LAPIC SMP pool withXSAVE/XRSTORFPU isolation) and statically validates the proposed machine-code patch before writing guest physical memory.
- Stage 1 (Proof of Ring
- Ring
-1resumes the guest viaVMRUN(or cleanly delegates viaPASS_TO_LINUXor freezes safely viaHALT_FOR_DUMP). When patched, Linux never panics and continues running with its originaluptimeand processes intact.
2. Boot Chain & Execution Flow
QEMU (-enable-kvm -cpu EPYC,+svm -vnc :1)
└── OVMF UEFI Firmware (sets GOP linear 32-bit BGRX framebuffer)
└── GRUB2 UEFI (Multiboot2)
├── Kernel: /boot/surgeon.elf (Ring -1 Hypervisor, 64-bit ELF)
├── Module 1: /boot/vmlinuz (Unmodified Linux 5.15 bzImage)
├── Module 2: /boot/initramfs.cpio.gz (Minimal BusyBox + surgeon_crash.ko)
└── [Stage 2] /boot/model.asmbin (Quantized LLM weights in reserved RAM)
Inside surgeon.elf:
hv/boot.asm(32-bit -> 64-bit Long Mode):- Multiboot2 header requests linear 32-bit framebuffer (
1280x720x32preferred; accepts GOP native resolution) and page-aligned modules. - Sets up 4-level identity-mapped host page tables (
0..4 GiBvia 2 MiB pages, plus dynamic mapping if framebuffer > 4 GiB), GDT with 64-bit code/data + TSS, transitions to 64-bit Long Mode (hv_main). - Compiled with
-mno-mmx -mno-sse -mno-sse2 -mno-avx -mno-red-zoneso Ring-1never clobbers guest FPU/XMM/AVX state during#VMEXIT.
- Multiboot2 header requests linear 32-bit framebuffer (
- Multiboot2 Parsing & Hardware Init (
hv/main.c,hv/serial.c,hv/fb.c):- Initializes COM1 (
0x3F8, 115200 8N1) serial logging. - Parses Multiboot2 tags: framebuffer (
addr,width,height,pitch,bpp, masks), E820 memory map, ACPI RSDP (v1/v2), and modules (vmlinuz,initramfs). - Renders initial Ring
-1boot banner onto the linear framebuffer.
- Initializes COM1 (
- Nested Page Table (NPT) & Memory Isolation (
hv/npt.c):- Builds a 4-level AMD NPT covering
0..4 GiB(plus any >4 GiB framebuffer BAR) withP|W|U(0x07/0x87, since AMD NPT treats all guest accesses as User-mode). - Ring
-1Protection: The hypervisor physical arena (0x00200000 .. 0x003FFFFF, 2 MiB) is:- Marked
E820_RESERVED(type 2) in Linuxboot_params.e820_table. - Unmapped (
Present = 0) in NPT, so even a rogue Ring 0 kernel write cannot corrupt Ring-1code, VMCB, HSAVE, or future LLM weights.
- Marked
- Builds a 4-level AMD NPT covering
- Linux 64-bit Direct Boot Loader (
hv/linux_boot.c):- Inspects
vmlinuzbzImagesetup_header(0x01F1), copies the protected-mode kernel payload topref_address(0x01000000), relocatesinitramfs.cpio.gzto0x08000000(using bounce-safe copy to avoid overlap with GRUB module placement). - Builds
boot_params("zero page") at0x00600000with:screen_infopopulated withVIDEO_TYPE_EFI(0x70) + exact GOP framebuffer geometry (lfb_base,lfb_width,lfb_height,lfb_depth=32,lfb_linelength,lfb_size, RGB bit positions/sizes) so Linux built-inefifbbinds immediately.acpi_rsdp_addrcopied from Multiboot2 ACPI tag.e820_tablecopied from Multiboot2 mmap with0x00200000..0x003FFFFFcarved out asE820_RESERVED.- Command line at
0x00601000:console=tty0 console=ttyS0,115200 earlyprintk=serial,ttyS0,115200 nokaslr panic=0.
- Builds initial 64-bit identity guest page tables (
0..4 GiB) at0x00610000forstartup_64.
- Inspects
- AMD SVM Activation &
VMRUNLoop (hv/svm.c,hv/svm_asm.asm):- Verifies CPUID
0x80000001ECX[2] (SVM) andMSR_VM_CR(0xC0010114) SVMDIS=0. - Enables
MSR_EFER.SVME(bit 12), programsMSR_VM_HSAVE_PA(0xC0010117) with 4KB-aligned host save area. - Configures 4KB-aligned
VMCB:guest_asid = 1,np_enable = 1,n_cr3 = npt_pml4_pa,g_pat = 0x0007040600070406ULL.- Intercepts:
VMRUN(mandatory),VMMCALL,CPUID,MSR_PROT(MSRPM intercepts ONLYRDMSR/WRMSRonMSR_EFER0xC0000080),SHUTDOWN, and exceptions#UD(vec 6),#DF(vec 8),#PF(vec 14). - Guest state initialized for Linux 64-bit entry:
RIP = 0x01000200(startup_64),RSI = 0x00600000(&boot_params),CR3 = guest_init_pml4,EFER = LME|LMA|NXE|SCE|SVME.
- Runs
CLGI -> VMSAVE host -> VMLOAD guest -> VMRUN -> VMSAVE guest -> VMLOAD hostloop.
- Verifies CPUID
3. Physical Memory Map (Stage 1, 2 GiB Guest RAM)
| Physical Range | Size | NPT Mapping | Guest E820 | Owner / Usage |
|---|---|---|---|---|
0x0000_0000 .. 0x000F_FFFF |
1 MiB | 1:1 P|W|U |
BIOS/Low | Low RAM / EBDA / ACPI |
0x0010_0000 .. 0x001F_FFFF |
1 MiB | 1:1 P|W|U |
Usable | Guest low extended RAM |
0x0020_0000 .. 0x003F_FFFF |
2 MiB | UNMAPPED (0) |
E820_RESERVED |
Ring -1 Hypervisor (ELF, stacks, VMCB, HSAVE, MSRPM, NPT) |
0x0060_0000 .. 0x0063_FFFF |
256 KiB | 1:1 P|W|U |
Usable | boot_params, cmdline, initial guest PT |
0x0100_0000 .. 0x03FF_FFFF |
48 MiB | 1:1 P|W|U |
Usable | Linux vmlinuz kernel payload & decompression arena |
0x0800_0000 .. 0x09FF_FFFF |
32 MiB | 1:1 P|W|U |
Usable | initramfs.cpio.gz |
0x0A00_0000 .. 0x7DFF_FFFF |
~1.8 GiB | 1:1 P|W|U |
Usable | Guest Linux RAM |
0x8000_0000 .. 0xFFFF_FFFF |
2 GiB | 1:1 P|W|U (+MMIO) |
Reserved/MMIO | GOP Linear Framebuffer, PCI MMIO, IOAPIC, LAPIC |
(In Stage 2, QEMU RAM increases to 6–8 GiB and an additional high physical arena is carved out of E820 and unmapped from NPT to house quantized LLM weights + KV cache.)
4. Surgical Intercept & Live-Patch Protocol
Fast-Path vs. Surgeon-Path Decision
CPUID(0x72): Execute hostcpuid, clearSVMbit (ECX[2]) onEAX == 0x80000001so Linux stays unaware of SVM; advertise"Ring1Surgeon"on leaf0x40000000; advanceRIP = next_rip.MSR(0x7Con0xC0000080EFER):RDMSR: returnvmcb.save.efer & ~EFER_SVME(transparently hidesSVME).WRMSR: storeval | EFER_SVMEintovmcb.save.efer(prevents guest from clearingSVMEand triggeringVMEXIT_INVALID).
#PF(0x4E, Vector 14):- If
CPL != 0orCR2 >= 0x1000(normal userspace page fault, demand paging, COW,copy_from_user): fast re-inject viaVMCB.eventinj(vector=14, type=3, EV=1, V=1, err=exitinfo1) andvmcb.save.cr2 = exitinfo2. - If
CPL == 0andCR2 < 0x1000(Ring 0 NULL-pointer dereference, e.g.surgeon_crash.koorsysrq-trigger): invoke Surgeon pipeline.
- If
#UD(0x46, Vector 6):- If
CPL != 0: fast re-inject#UDto guest. - If
CPL == 0: check if faulting bytes are0F 0B(ud2) from our crash target / Ring 0 fault -> invoke Surgeon pipeline (if LinuxBUG()/WARN_ONoutside test or configured passthrough, can optionally re-inject; in Stage 1 Ring 0#UDis healed cleanly when triggered viasurgeon_crashor test).
- If
VMMCALL(0x81): Diagnostic hypercall (RAX = 0x53555247"SURG"returns heal count & hypervisor status;RAX = 0x53555248forces HUD redraw).
Stage 2 Extension Point (hv/surgeon.h)
typedef struct {
uint32_t vector; // 14 (#PF) or 6 (#UD)
uint64_t error_code;
uint64_t cr2;
uint64_t rip_gva;
uint64_t rip_gpa;
uint8_t orig_bytes[16];
uint8_t insn_len;
GuestRegs *regs;
Vmcb *vmcb;
} SurgeonContext;
typedef struct {
uint8_t patch_bytes[16];
uint8_t patch_len;
const char *reason; // Shown in Ring -1 HUD overlay
} SurgeonPatch;
bool surgeon_decide_patch(const SurgeonContext *ctx, SurgeonPatch *out);
Stage 1 implements surgeon_decide_patch() deterministically (exact-length NOP sled verified by x86_decode_len()). Stage 2 swaps only this function to call the bare-metal LLM + static machine-code verifier.
5. Verification Plan (Stage 1)
- Build
surgeon.elf,surgeon_crash.ko,initramfs.cpio.gz, and UEFI bootablering1-surgeon.isoviamake. - Boot under QEMU nested AMD SVM (
scripts/run_qemu.shwith-enable-kvm -cpu EPYC,+svm -vnc :1). - Confirm in serial log + VNC framebuffer:
- Ring
-1initializes AMD SVM + NPT and launches Linux5.15.0-185-genericviaVMRUN. - Linux mounts
efifbon the shared linear framebuffer and reaches shell. insmod /lib/modules/surgeon_crash.koloads cleanly.echo pf > /proc/surgeon_crashtriggers a real Ring 0 NULL-pointer dereference (*(volatile uint32_t *)0 = 0xDEADBEEF).- Ring
-1intercepts#PF, resolvesRIPvia guest PT walk, draws the Ring-1Surgical HUD Overlay directly onto the live VNC framebuffer, live-patches the faulting instruction bytes in guest.textphysical RAM toNOPs, and resumesVMRUN. - Linux survives without panicking, prints confirmation to both VNC console and
ttyS0,uptimecontinues ticking, andecho ud > /proc/surgeon_crashsimilarly heals a Ring 0#UD(ud2) trap live.
- Ring