A high-performance, capability-based bare-metal operating system microkernel and multi-server userland written entirely in C# using Native AOT compilation, targeting modern 64-bit x86-64 hardware without any dependencies on the standard runtime (CoreCLR), glibc, or external bootloaders.
The system boots directly from UEFI firmware into higher-half virtual memory, enforces hardware privilege separation (Ring 0 supervisor vs. Ring 3 userland), routes communications through a capability-secured synchronous and asynchronous IPC engine, and provides hardware-accelerated graphics (AVX2), high-throughput storage (NVMe DMA), a dedicated FAT32 filesystem server with a Virtual File System (System.IO.File), modern VirtIO network acceleration, fault-tolerant supervisor supervision, and an interactive graphical terminal shell.
graph TD
subgraph Ring3_Userland ["Ring 3: Isolated Userland Processes (CPL = 3)"]
Shell["apps/shell<br/><i>Micro-GC Runtime | PSF2 Text Grid | History</i>"]
StorageDriver["storage.nvme<br/><i>ZeroAlloc Runtime | SPSC DMA Queues</i>"]
Fat32Server["fs.fat32<br/><i>ZeroAlloc Runtime | BPB & Cluster Chains</i>"]
NetDriver["net.virtio<br/><i>ZeroAlloc Runtime | Modern PCIe Capabilities</i>"]
InputDriver["input.hid<br/><i>ZeroAlloc Runtime | PS/2 ANSI Translation</i>"]
DisplayServer["display_server<br/><i>AVX2 Vector Blitter | Alpha Blending</i>"]
PciServer["pci_server<br/><i>PCIe ECAM Discovery | FLR | MSI/MSI-X</i>"]
Supervisor["supervisor<br/><i>Watchdog | Fault Reincarnation</i>"]
Roottask["roottask<br/><i>Bootstrap Initrd | CSpace Delegator</i>"]
end
subgraph IPC_Layer ["Roslyn Source-Generated Zero-Alloc RPC"]
ClientProxies["Devirtualized Client Proxies"] <-->|Hardware Syscall FastPath| ServerDispatchers["Type-Safe Server Dispatchers"]
end
subgraph Ring0_Kernel ["Ring 0: Higher-Half C# Microkernel (CPL = 0)"]
SyscallDispatcher["Hardware SYSCALL/SYSRET Engine"]
CSpace["seL4-Style Capability Space (CNode / CSpace / CDT)"]
Scheduler["Preemptive MLFQ Scheduler & Timeslice Donation"]
Paging["4-Level Paging (PML4) & HHDM (0xFFFF_8000_0000_0000)"]
MemoryAlloc["Slab Allocator (Kmem) & PMM Bitmap & DMA Arena"]
Arch["GDT, IDT (256 Gates), TSS (RSP0 Stack Isolation), LAPIC, PAT"]
end
subgraph Hardware ["x86-64 Bare-Metal Hardware & Peripherals"]
CPU["x86-64 CPU (AVX2, FS/GS, SYSCALL)"]
GOP["UEFI Graphics Output Protocol (GOP FB)"]
NVMeHW["PCIe NVMe Block Device (Direct DMA / MSI Vector 0x30)"]
NetHW["VirtIO-Net PCIe Controller (MSI Vector 0x31)"]
KBHW["PS/2 Keyboard Controller (Port 0x60/0x64)"]
PCIeECAM["PCIe ECAM Memory-Mapped Config Space"]
end
Shell -->|RegisterSurface / CommitSurface| DisplayServer
Shell -->|ReadAllText / ReadAllBytes| Fat32Server
Shell -->|SendPacket / ReceivePacket| NetDriver
Fat32Server -->|ReadBlock / WriteBlock| StorageDriver
Shell -->|Enumerate Topology| PciServer
Supervisor -->|FLR Reset / Reincarnate| PciServer
Supervisor -->|Health Check| DisplayServer
Roottask -->|Spawn / Synthesize CSpace| Ring3_Userland
Ring3_Userland <-->|FastPath SYSCALL| SyscallDispatcher
SyscallDispatcher --> CSpace
SyscallDispatcher --> Scheduler
SyscallDispatcher --> Paging
SyscallDispatcher --> MemoryAlloc
Ring0_Kernel --> CPU
DisplayServer --> GOP
StorageDriver --> NVMeHW
NetDriver --> NetHW
InputDriver --> KBHW
PciServer --> PCIeECAM
| Layer | Component | Description |
|---|---|---|
| 1 | Firmware Boot & Memory Map | Direct UEFI 2.x application boot (BOOTX64.EFI) via EfiMain.cs. Resolves GOP framebuffer, parses ACPI RSDP, locates INITRD.IMG, extracts memory descriptors, and executes ExitBootServices with zero post-exit allocations. |
| 2 | Higher-Half Handover & Paging | Creates identity and higher-half direct map (HHDM) 4-level page tables at 0xFFFF_8000_0000_0000. Programs IA32_PAT for Write-Combining (WC) on GOP framebuffer and jumps to higher-half KernelMainHigh. |
| 3 | Hardware Descriptors & Slab Heap | Installs 64-bit Global Descriptor Table (GDT), 256-gate Interrupt Descriptor Table (IDT), Task State Segment (TSS) with isolated RSP0 stacks, masks 8259 PIC, programs Local APIC timer, and initializes multi-pool slab allocator. |
| 4 | Threading & Preemptive MLFQ | Implements preemptive Multi-Level Feedback Queue scheduler with 4 priority levels, round-robin timeslices, hardware context switching in NASM assembly, and MSR configuration (STAR, LSTAR, FMASK) for SYSCALL/SYSRET. |
| 5 | Capability Space (CSpace) & CDT | seL4-inspired authorization model. Resources (threads, endpoints, notifications, page frames, CNodes) are referenced via guarded capability pointers (cptr) with cryptographic badges and access rights (Read, Write, Call, Grant). Features a zero-alloc Capability Derivation Tree (CDT) enforcing recursive capability revocation and synchronous virtual memory unmapping/TLB invalidation. |
| 6 | Unified IPC Engine | Dual-mode IPC supporting zero-copy synchronous rendezvous with timeslice donation (sys_call/sys_reply), 64-bit atomic asynchronous notifications (sys_notify), and unified dual-wait reactors (sys_recv_any). |
| 7 | Userland Bootstrap & Root Task | roottask is loaded from INITRD.IMG. Microkernel synthesizes an isolated 4-level page directory (PML4) with user bits (Paging.User), populates the root CNode, delegates capabilities across servers, and drops to Ring 3 (CPL = 3) via iretq. |
| 8 | Dual Runtimes & Roslyn RPC | Userland.Runtime.ZeroAlloc: Freestanding, allocation-free runtime backed by NativeArena with dynamic chunk-linked expansion (ArenaChunk) via high DMA aperture 0x0000_7000_0000_0000UL for high-throughput driver workloads.Userland.Runtime.Gc: Generational mark-sweep micro-GC for user applications.Microkernel.RpcGenerator: Roslyn Source Generator emitting devirtualized zero-alloc RPC proxies. |
| 9 | PCIe Discovery, NVMe & VirtIO-Net | pci_server maps ECAM space (0xE0000000) and programs PCI MSI/MSI-X vectors (NVMe vector 0x30, VirtIO-Net vector 0x31). storage.nvme sets up 4 KiB contiguous rings (ASQ/ACQ, IOSQ/IOCQ) with interrupt notification dispatch. net.virtio drives modern VirtIO-Net via assigned MSI vectors. |
| 10 | FAT32 Filesystem Server & VFS | fs.fat32 mounts root block storage, parses BPB/FAT32 structures, and provides cluster-chain lookups. Microkernel.Vfs exposes clean System.IO.File APIs (ReadAllText, ReadAllBytes) using shared DMA pages. |
| 11 | Fault Recovery & Supervisor | supervisor acts as a watchdog process. Intercepts crashed or faulty driver states, performs PCIe Function-Level Resets (FLR), reincarnates child server execution, and reconstructs IPC capability bindings. |
| 12 | Compositor & Graphic Shell | display_server composites surfaces directly using AVX2 SIMD vector instructions (vmovdqu) with 8-bit alpha blending and dirty region clipping. apps/shell provides command history ring buffering, PSF2 font rendering, and integration commands. |
baremetal-csharp-microkernel/
βββ build/
β βββ config/ # MSBuild compiler configuration props
β β βββ Kernel.props # Kernel freestanding build properties
β β βββ UserlandApp.props # Userland service compilation props
β βββ scripts/ # Build, disk image, and testing scripts
β βββ Make-DiskImage.sh # Native AOT pipeline, packaging, and GPT/FAT32 staging
β βββ Make-UsbBootable.sh # Safe flashing script for bare-metal USB drives
β βββ Pack-Initrd.py # Serializes system server binaries into initial ramdisk
β βββ Run-Qemu.sh # Launch QEMU with OVMF firmware, NVMe, and serial stdio
β βββ Test-Harness.py # Automated streaming verification suite with milestone regex checks
βββ src/
β βββ apps/
β β βββ shell/ # Layer 12: Interactive graphic terminal shell (Micro-GC)
β βββ common/
β β βββ Microkernel.Abstractions/ # Syscall numbers, RPC contracts, capability definitions
β β βββ Microkernel.Collections/ # Intrusive linked lists, bitmaps, and ring buffers
β β βββ Microkernel.Vfs/ # Layer 10: Virtual File System & System.IO.File abstraction
β β βββ MiniCoreLib/ # Freestanding BCL implementation (no external stdlib)
β βββ compiler-plugins/
β β βββ Microkernel.RpcGenerator/ # Roslyn Source Generator for type-safe IPC dispatchers
β βββ kernel/ # Ring 0 Higher-Half Microkernel Core
β β βββ Arch/x86_64/ # CPU structures, GDT/IDT/TSS, PAT, LAPIC, assembly thunks
β β βββ Boot/ # UEFI entry point (EfiMain), memory parser, ACPI discovery
β β βββ Capabilities/ # CNode, CSpace, CapabilityDerivationTree authorization engine
β β βββ Diagnostics/ # 16550 UART early serial logger
β β βββ Ipc/ # Synchronous rendezvous, FastPath IPC, SyscallDispatcher
β β βββ Memory/ # PMM bitmap, 4-level paging, HHDM, DMA arena, Slab allocator
β β βββ Scheduling/ # Preemptive MLFQ scheduler, TCB, context switching
β βββ libs/
β β βββ Microkernel.Drawing/ # ARGB32 surface blitter, PSF2 font rasterization, alpha blend
β β βββ Microkernel.Sdk/ # Userland IPC channels and namespace resolution
β βββ runtime/
β β βββ Userland.PieLoader/ # Relocatable position-independent ELF loader
β β βββ Userland.Runtime.Gc/ # Layer 8: Managed heap, mark-sweep micro-garbage collector
β β βββ Userland.Runtime.ZeroAlloc/ # Layer 8: Allocation-free driver runtime with dynamic NativeArena
β βββ servers/ # Ring 3 Isolated System Servers
β βββ display_server/ # Layer 12: AVX2 hardware framebuffer compositor & dirty clipper
β βββ drivers/
β β βββ input.hid/ # Layer 10: PS/2 keyboard driver & ANSI escape sequence translator
β β βββ net.virtio/ # Layer 9: VirtIO-Net modern PCIe controller driver
β β βββ storage.nvme/ # Layer 9: High-throughput NVMe DMA storage driver
β βββ fs.fat32/ # Layer 10: Ring 3 FAT32 filesystem server
β βββ pci_server/ # Layer 9: PCIe ECAM topology discovery, MSI-X routing, and FLR control
β βββ roottask/ # Layer 7: Initial bootstrap task and CSpace delegator
β βββ supervisor/ # Layer 11: Process watchdog and fault recovery supervisor
βββ .gitignore # Git exclusion rules for native & managed artifacts
βββ Directory.Build.props # Workspace-wide Roslyn and compilation flags
βββ README.md # Comprehensive architectural documentation
Standard .NET relies on CoreCLR, which assumes an underlying operating system (POSIX libc or Win32 API). This microkernel builds against MiniCoreLib, a custom, zero-dependency BCL defining:
- Core primitive types (
object,string,Array,ValueType,Enum,IntPtr,UIntPtr). - Hardware attributes (
[UnmanagedCallersOnly],[StructLayout],[MethodImpl]). - Strict memory-safety abstractions (
Span<T>, pointer arithmetic, and intrinsic bit manipulation). - Zero static
.cctorinitializers or runtime metadata tables in freestanding binary sections.
Inter-process communication between isolated Ring 3 servers uses the Roslyn source generator Microkernel.RpcGenerator. Interfaces decorated with [RpcContract] generate:
- Devirtualized client proxy structs mapping method calls into hardware registers (
d0..d3) and issuingsys_call. - Server dispatchers matching RPC method identifiers in tight switch statements with zero heap allocations.
[RpcContract]
public interface IBlockStorageService
{
[RpcMethod(1)]
ulong ReadBlock(ulong lba, ulong shmCptr);
[RpcMethod(2)]
ulong WriteBlock(ulong lba, ulong shmCptr);
}The NVMe driver operates entirely in userland:
- Allocates strictly 4096-byte aligned physical frames from the microkernel's contiguous DMA arena for Admin Submission/Completion Queues (ASQ/ACQ) and I/O Queues (IOSQ/IOCQ).
- Configures 64-bit BAR0 and enables Bus Mastering (Bit 2) and Memory Space (Bit 1) in the PCI Command Register.
- Safe controller shutdown and initialization sequence obeying the NVM Express 1.4 specification (
CC = 0x00460001: 4 KiB page size, 64-byte SQEs, 16-byte CQEs).
The display server composites graphical windows and text surfaces onto the UEFI GOP framebuffer using 256-bit AVX2 SIMD instructions. Unaligned sub-rectangles are processed using vmovdqu to prevent #GP alignment exceptions:
Avx2Blit:
.blit32:
cmp rcx, 32
jb .blit1
vmovdqu ymm0, [rsi]
vmovdqu [rdi], ymm0
add rsi, 32
add rdi, 32
sub rcx, 32
jmp .blit32Rather than burning CPU cycles in driver polling loops, pci_server parses PCI capability linked lists (CapID 0x05 and 0x11) and programs Message-Signaled Interrupts directly:
- NVMe completion queues route to vector
0x30, VirtIO-Net TX/RX rings route to vector0x31. - Hardware interrupts are dispatched via
InterruptDispatcherdirectly into the target driver's bound asynchronousNotificationcapability viaNotification.Signal(badge)using collision-free vector lookup tables.
To eliminate dangling frame pointers and stale address translations:
- Memory capabilities register parent-child lineages inside a zero-alloc
CapabilityDerivationTree(CDT). - Revoking or deleting a frame capability synchronously traverses the owning process's 4-level paging hierarchy (PML4 -> PDPT -> PD -> PT), zeros the PTE, strips
PTE_GLOBAL, and issuesCpu.Invlpgshootdowns before reclaiming the capability slot.
To run the microkernel immediately without compiling the source code:
# 1. Download pre-built disk image
curl -LO [https://github.com/sadik00789/SharpMetal/releases/download/v1.0.0/disk.img](https://github.com/sadik00789/SharpMetal/releases/download/v1.0.0/disk.img)
# 2. Create backing image for NVMe benchmark storage
qemu-img create -f raw nvme.img 64M
# 3. Launch QEMU (Universal / Emulated AVX2)
qemu-system-x86_64 -machine q35 -cpu max -m 1G \
-drive if=pflash,format=raw,readonly=on,file=/usr/share/OVMF/OVMF_CODE.fd \
-drive file=disk.img,format=raw \
-drive file=nvme.img,format=raw,if=none,id=nvm \
-device nvme,serial=nvme01,drive=nvm \
-netdev user,id=net0 -device virtio-net-pci,netdev=net0To build the microkernel from source, ensure your host build system (Linux x86-64) has the following packages installed:
# Ubuntu / Debian
sudo apt-get update
sudo apt-get install -y dotnet-sdk-9.0 nasm lld qemu-system-x86 ovmf parted mtools xorriso python3
# Arch Linux
sudo pacman -S dotnet-sdk nasm lld qemu-system-x86 edk2-ovmf parted mtools xorriso pythonRun the unified build pipeline script:
bash build/scripts/Make-DiskImage.shThis script automatically:
- Compiles
MiniCoreLib, abstractions, and Roslyn generators. - Builds IL binaries for the microkernel and all 7 userland servers.
- Invokes RyuJIT /
ilc(Native AOT) to emit freestanding COFF object files. - Assembles assembly thunks (
nasm -f win64). - Links
BOOTX64.EFIand server binaries usinglld-link. - Packages all servers into
INITRD.IMG. - Creates a 32 MiB raw NVMe storage image (
build/images/nvme.img). - Creates a 64 MiB GPT-partitioned disk image (
build/images/disk.img) with FAT32 EFI System Partition.
Launch the microkernel under QEMU with UEFI firmware, NVMe emulation, and serial console redirection:
bash build/scripts/Run-Qemu.shTip (Display Mode):
Run-Qemu.shdefaults to-display nonefor clean headless execution and CI logging. To view the live AVX2 graphical desktop window directly on your screen, run:bash build/scripts/Run-Qemu.sh -display default # or: -display gtk / -display sdl
Execute the automated regression harness:
python3 build/scripts/Test-Harness.py --headlessThe test harness builds the system, boots QEMU headlessly, streams serial logs, sequentially verifies all boot milestones (CSpace root, PCIe ECAM discovery, NVMe canary block write/read, VirtIO-Net packet transmission, FAT32 VFS read, and Shell interactive readiness), and terminates with a clean exit code 0.
Generate an optimized, palette-quantized boot demonstration GIF:
python3 build/scripts/Record-Demo.pyThis script launches headless QEMU with a UNIX monitor socket, captures screendump PPM frames every 150ms, holds the final terminal screen for 3.0 seconds, and compiles docs/assets/demo.gif using ffmpeg with lanczos scaling and palette optimization.
Flash a bootable UEFI drive for physical bare-metal hardware testing:
sudo bash build/scripts/Make-UsbBootable.sh /dev/sdXThe script features safety protections to prevent host disk loss:
- Block Device Validation: Enforces valid device paths (
-b). - Removable Drive Check: Rejects non-removable drives (
RM == 0) unless explicit--i-know-what-i-am-doingis passed. - Mount Point Protection: Actively detects and refuses to flash root (
/) or boot (/boot) partitions. - Interactive Confirmation: Prompts with device model, capacity, and requires typing
"YES". - Partition Table Settling: Executes
partprobe "$TARGET" && udevadm settleafter creating a 128 MiB GPT EFI System Partition, checks for both${TARGET}1and${TARGET}p1, formats withmkfs.vfat -F 32 -n "SHARPMETAL", and stagesBOOTX64.EFI,INITRD.IMG, andNVME.IMG.
Upon completing initialization, apps/shell registers an ARGB32 console surface with display_server and supports the following commands:
| Command | Action | Output / Behavior |
|---|---|---|
help |
Display command list | Shows available shell commands and syntax |
pci |
Enumerate PCIe ECAM devices | Scans buses 0..3 and lists discovered Host Bridges, Display Controllers, and NVMe drives |
nvme |
Execute NVMe benchmark | Performs verified block write and read to LBA 65535 with canary validation (0xA55A1234) |
cat <file> |
Read file via VFS | Uses System.IO.File.ReadAllText over IPC to read and display FAT32 filesystem contents (e.g. cat /HELLO.TXT) |
net |
VirtIO-Net TX benchmark | Builds a 64-byte Ethernet broadcast frame (EtherType 0x88B5) and transmits via split virtqueue descriptor staging |
caps |
Inspect CSpace capability slots | Lists all root CNode slots and assigned access rights |
ps |
Display process thread table | Displays active thread IDs, execution states, and priority levels |
exit |
Microkernel shutdown | Invokes sys_exit(0), triggers clean ACPI poweroff / VM shutdown, and exits execution |
The headless test harness confirms operational integrity across all 12 microkernel layers:
=================================================================
SharpMetal Microkernel Headless CI Automation Harness
=================================================================
[OVMF] Verified firmware image at: /usr/share/OVMF/OVMF_CODE.fd
[STEP 1] Running Make-DiskImage.sh...
[PASS] Kernel built, drivers packaged, and disk image staged successfully.
[STEP 2] Launching QEMU headless test harness...
[ROOTTASK] Initial root CNode initialized with 10 core capabilities.
[PCI] Scanning PCIe ECAM bus topology...
[PCI] Found Host Bridge / Display Controller / Storage Controller.
[NVME] Controller initialized. Admin and I/O queues online.
[NVME] Verified block write to LBA 65535 (Canary: 0xA55A1234).
[NVME] Verified block read from LBA 65535 matches canary.
[NVME] Block I/O benchmark passed (Write & Read Verified).
[FAT32] Volume mounted. Found root directory entry: HELLO.TXT
[VIRTIO-NET] Modern PCI VirtIO Network device detected.
[VIRTIO] VirtIO-Net controller online. MAC: 52:54:00:12:34:56
[SHELL] History ring buffer initialized (32 slots).
[SHELL] SharpMetal Bare-Metal Shell online.
[VFS] File.ReadAllText('/HELLO.TXT') -> "SharpMetal BareMetal OS"
[SUCCESS] Phase 10 fully operational. Exiting QEMU...
[+] All boot milestones successfully verified.
This project is open-source software licensed under the MIT License.
