SimpleKernel

March 20, 2026 Β· View on GitHub

codecov workflow commit-activity MIT License LICENSE 996.icu

English | δΈ­ζ–‡

SimpleKernel

Interface-Driven OS Kernel for AI-Assisted Learning | Multi-Architecture: RISC-V 64, AArch64

πŸ€– Design Philosophy: Define clear kernel interfaces, let AI generate the implementation β€” a new paradigm for learning operating systems

πŸ“– Table of Contents

✨ Project Overview

SimpleKernel is a modern OS kernel project designed for AI-assisted learning. Written in C++23, it supports RISC-V 64 and AArch64 architectures.

Unlike traditional OS teaching projects, SimpleKernel adopts an Interface-Driven design:

  • The project body is interface definitions β€” complete header files (.h/.hpp) containing class declarations, pure virtual interfaces, type definitions, and Doxygen documentation
  • Implementation is done by AI β€” you only need to understand the interface contracts, and let AI generate .cpp implementations from the interface docs
  • Reference implementations for comparison β€” the project provides complete reference implementations to verify the correctness of AI-generated code

🌟 Core Highlights

FeatureDescription
πŸ€– AI-First DesignInterface docs serve as prompts β€” AI can generate complete implementations directly from header files
πŸ“ Interface-Implementation SeparationHeaders contain only declarations and contracts; implementations live in separate .cpp files
🌐 Two-Architecture SupportRISC-V 64, AArch64 β€” one set of interfaces adapting to different hardware
πŸ§ͺ Test-Driven VerificationGoogleTest test suites verify whether AI-generated implementations conform to interface contracts
πŸ“– Complete Doxygen DocumentationEvery interface has responsibility descriptions, preconditions, postconditions, and usage examples
πŸ—οΈ Engineering InfrastructureCMake build, Dev Container environment, CI/CD, clang-format/clang-tidy

πŸ€– AI-Oriented Design Philosophy

Why "AI-Oriented"?

Traditional OS teaching projects follow: read code β†’ understand principles β†’ mimic and modify. This approach has several problems:

  1. Kernel codebases are large β€” beginners easily get lost in implementation details
  2. Modules are tightly coupled β€” difficult to understand individual subsystems independently
  3. Implementing a module from scratch has a high barrier with long feedback cycles

SimpleKernel proposes a new paradigm: read interface β†’ understand contract β†’ AI implements β†’ test verifies

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 SimpleKernel Learning Flow                β”‚
β”‚                                                         β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚
β”‚   β”‚ πŸ“ Inter- │───▢│ πŸ€– AI    │───▢│ πŸ§ͺ Test  β”‚         β”‚
β”‚   β”‚ face Hdrs β”‚    β”‚ Generatesβ”‚    β”‚ Verifies β”‚         β”‚
β”‚   β”‚ + Doxygen β”‚    β”‚ Impl     β”‚    β”‚ Contract β”‚         β”‚
β”‚   β”‚           β”‚    β”‚ (.cpp)   β”‚    β”‚ GoogleTestβ”‚         β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
β”‚        β”‚                               β”‚                β”‚
β”‚        β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚                β”‚
β”‚        └────────▢│ πŸ“š Ref   β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                β”‚
β”‚                  β”‚ Impl     β”‚                           β”‚
β”‚                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Core Workflow

1️⃣ Read Interface, Understand Contract

Each module's header file contains complete interface documentation:

/**
 * @brief Interrupt subsystem abstract base class
 *
 * All architecture interrupt handlers must implement this interface.
 *
 * @pre  Hardware interrupt controller has been initialized
 * @post Can register interrupt handlers via RegisterInterruptFunc
 *
 * Known implementations: PLIC (RISC-V), GIC (AArch64)
 */
class InterruptBase {
public:
  virtual ~InterruptBase() = default;

  /// Execute interrupt handling
  virtual void Do(uint64_t cause, cpu_io::TrapContext* context) = 0;

  /// Register interrupt handler function
  virtual void RegisterInterruptFunc(uint64_t cause, InterruptFunc func) = 0;
};

2️⃣ Let AI Implement

Provide the header file as context to an AI (e.g., GitHub Copilot, ChatGPT, Claude) and ask it to generate the .cpp implementation. The Doxygen comments in the interface are the best prompt.

3️⃣ Test and Verify

Run the project's built-in test suite to verify the AI-generated implementation conforms to the interface contract:

cmake --preset build_riscv64
cd build_riscv64 && make unit-test

4️⃣ Compare with Reference Implementation

If tests fail, refer to the project's reference implementation for comparison and learning.

Integration with AI Tools

ScenarioUsage
GitHub CopilotOpen the header file, let Copilot auto-complete the implementation in the corresponding .cpp
ChatGPT / ClaudePaste header file contents as context, request a complete .cpp implementation
Copilot Chat / CursorSelect the interface in the IDE, ask AI to explain contract meaning or generate implementation
Self-StudyThink about the implementation first, then let AI generate it, and compare differences

πŸ›οΈ Interface Architecture Overview

SimpleKernel's interfaces are organized into the following layers:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          Application / Syscall Layer      β”‚
β”‚         syscall.h Β· SyscallInit          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚            Task Management Layer          β”‚
β”‚  TaskManager Β· SchedulerBase Β· Mutex     β”‚
β”‚  CfsScheduler Β· FifoScheduler Β· RR ...   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚          Memory Management Layer          β”‚
β”‚  VirtualMemory Β· PhysicalMemory          β”‚
β”‚  MapPage Β· UnmapPage Β· AllocFrame        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚          Interrupt / Exception Layer      β”‚
β”‚  InterruptBase Β· RegisterInterruptFunc   β”‚
β”‚  TimerInit Β· InterruptInit               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚               Device Framework Layer           β”‚
β”‚  DeviceManager Β· DriverRegistry               β”‚
β”‚  PlatformBus Β· Ns16550aDriver Β· VirtioBlk     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚       Architecture Abstraction (arch.h)   β”‚
β”‚  ArchInit Β· InterruptInit Β· TimerInit    β”‚
β”‚  EarlyConsole (auto-set during global    β”‚
β”‚               construction phase)         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         Runtime Support Libraries         β”‚
β”‚  libc (sk_stdio.h, sk_string.h, ...)     β”‚
β”‚  libcxx (kstd_vector, __cxa_*, ...)      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚            Hardware / QEMU                β”‚
β”‚  RISC-V 64 Β· AArch64                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Interface Files

Interface FileResponsibilityImplementation File
src/arch/arch.hArchitecture-independent unified entryEach src/arch/{arch}/ directory
src/include/interrupt_base.hInterrupt subsystem abstract base classsrc/arch/{arch}/interrupt.cpp
src/device/include/device_manager.hppDevice managerheader-only
src/device/include/driver_registry.hppDriver registryheader-only
src/device/include/platform_bus.hppPlatform bus (FDT enumeration)header-only
src/device/include/driver/ns16550a_driver.hppNS16550A UART driverheader-only (Probe/Remove pattern)
src/include/virtual_memory.hppVirtual memory management interfacesrc/virtual_memory.cpp
src/include/kernel_fdt.hppDevice tree parsing interfacesrc/kernel_fdt.cpp
src/include/kernel_elf.hppELF parsing interfacesrc/kernel_elf.cpp
src/task/include/scheduler_base.hppScheduler abstract base classcfs_scheduler.cpp etc.
src/include/spinlock.hppSpinlock interfaceheader-only (performance)
src/include/mutex.hppMutex interfacesrc/task/mutex.cpp

πŸ“‹ See docs/TODO_interface_refactor.md for the complete interface refactoring plan.

πŸ—οΈ Supported Architectures

ArchitectureBoot ChainSerialInterrupt ControllerTimer
RISC-V 64U-Boot + OpenSBISBI CallDirect ModeSBI Timer
AArch64U-Boot + ATF + OP-TEEPL011GICv3Generic Timer

πŸš€ Quick Start

πŸ“‹ System Requirements

  • Operating System: Linux (Ubuntu 24.04 recommended) or macOS
  • Container Engine: Docker or compatible container runtime
  • Toolchain: Included in Dev Container (GCC 14 cross-compilers, CMake, QEMU, etc.)
  • AI Tools (recommended): GitHub Copilot / ChatGPT / Claude

πŸ› οΈ Environment Setup

Option 1: Using Dev Container (Recommended)

# 1. Clone the project
git clone https://github.com/simple-xx/SimpleKernel.git
cd SimpleKernel

# 2. Open in VS Code and reopen in container
#    Install Dev Containers extension, click the >< icon at bottom-left
#    Select "Reopen in Container"

# Or use CLI
npm install -g @devcontainers/cli
devcontainer up --workspace-folder .
devcontainer exec --workspace-folder . bash

Also supports GitHub Codespaces: Click Code β†’ Codespaces β†’ Create codespace on main

See Dev Container documentation for details.

Option 2: Local Environment

Refer to Toolchain Documentation for local development environment setup.

⚑ Build and Run

cd SimpleKernel

# Select target architecture (RISC-V 64 example)
cmake --preset build_riscv64
cd build_riscv64

# Build kernel
make SimpleKernel

# Run in QEMU emulator
make run

# Run unit tests (verify your implementation)
make unit-test

Supported Architecture Presets:

  • build_riscv64 - RISC-V 64-bit architecture
  • build_aarch64 - ARM 64-bit architecture

🎯 AI-Assisted Development Workflow

# 1. Open project in VS Code (GitHub Copilot extension recommended)
code ./SimpleKernel

# 2. Read interface definitions in header files (e.g., src/include/virtual_memory.hpp)

# 3. Create/edit the corresponding .cpp file, let AI generate implementation from the interface

# 4. Build and verify
cd build_riscv64 && make SimpleKernel

# 5. Run tests
make unit-test

# 6. Run in QEMU, observe behavior
make run

πŸ“‚ Project Structure

SimpleKernel/
β”œβ”€β”€ src/                        # Kernel source code
β”‚   β”œβ”€β”€ include/                # πŸ“ Public interface headers (project core)
β”‚   β”‚   β”œβ”€β”€ virtual_memory.hpp  #   Virtual memory management interface
β”‚   β”‚   β”œβ”€β”€ kernel_fdt.hpp      #   Device tree parsing interface
β”‚   β”‚   β”œβ”€β”€ kernel_elf.hpp      #   ELF parsing interface
β”‚   β”‚   β”œβ”€β”€ spinlock.hpp        #   Spinlock interface
β”‚   β”‚   β”œβ”€β”€ mutex.hpp           #   Mutex interface
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ arch/                   # Architecture-specific code
β”‚   β”‚   β”œβ”€β”€ arch.h              # πŸ“ Architecture-independent unified interface
β”‚   β”‚   β”œβ”€β”€ aarch64/            #   AArch64 implementation
β”‚   β”‚   └── riscv64/            #   RISC-V 64 implementation
β”‚   β”œβ”€β”€ device/                 # Device management framework
β”‚   β”‚   β”œβ”€β”€ include/            # πŸ“ Device framework interfaces (DeviceManager, DriverRegistry, Bus, etc.)
β”‚   β”‚   β”‚   └── driver/         #   Concrete drivers (ns16550a_driver.hpp, virtio_blk_driver.hpp)
β”‚   β”‚   └── device.cpp          #   Device initialization entry (DeviceInit)
β”‚   β”œβ”€β”€ task/                   # Task management
β”‚   β”‚   β”œβ”€β”€ include/            # πŸ“ Scheduler interfaces (SchedulerBase, etc.)
β”‚   β”‚   └── ...                 #   Scheduler implementations
β”‚   β”œβ”€β”€ libc/                   # Kernel C standard library
β”‚   └── libcxx/                 # Kernel C++ runtime
β”œβ”€β”€ tests/                      # πŸ§ͺ Test suite
β”‚   β”œβ”€β”€ unit_test/              #   Unit tests
β”‚   β”œβ”€β”€ integration_test/       #   Integration tests
β”‚   └── system_test/            #   System tests (QEMU-based)
β”œβ”€β”€ docs/                        # πŸ“š Documentation
β”‚   β”œβ”€β”€ TODO_interface_refactor.md  # Interface refactoring plan
β”‚   └── ...
β”œβ”€β”€ cmake/                      # CMake build configuration
β”œβ”€β”€ 3rd/                        # Third-party dependencies (Git Submodule)
└── tools/                      # Build tools and templates

Directories/files marked with πŸ“ are interface definitions β€” these are what you should focus on reading.

🎯 Learning Path

We recommend learning and implementing modules in the following order:

Phase 1: Infrastructure (Boot)

ModuleInterface FileDifficultyDescription
Early Consolesrc/arch/arch.h comments⭐Earliest output, understand global construction
Serial Driverns16550a_driver.hpp⭐⭐Implement Probe/Remove, understand device framework and MMIO
Device Tree Parsingkernel_fdt.hpp⭐⭐Parse hardware info, understand FDT format
ELF Parsingkernel_elf.hpp⭐⭐Symbol table parsing, used for stack backtrace

Phase 2: Interrupt System

ModuleInterface FileDifficultyDescription
Interrupt Baseinterrupt_base.h⭐⭐Understand unified interrupt abstraction
Interrupt ControllerPer-arch driver headers⭐⭐⭐GIC/PLIC hardware programming
Timer Interruptarch.h β†’ TimerInit⭐⭐Timer configuration, tick-driven

Phase 3: Memory Management

ModuleInterface FileDifficultyDescription
Virtual Memoryvirtual_memory.hpp⭐⭐⭐Page table management, address mapping
Physical MemoryRelated interfaces⭐⭐⭐Frame allocator, buddy system

Phase 4: Task Management (Thread/Task)

ModuleInterface FileDifficultyDescription
Spinlockspinlock.hpp⭐⭐Atomic operations, multi-core synchronization
Mutexmutex.hpp⭐⭐⭐Task-blocking based lock
Schedulerscheduler_base.hpp⭐⭐⭐CFS/FIFO/RR scheduling algorithms

Phase 5: System Calls

ModuleInterface FileDifficultyDescription
System Callsarch.h β†’ SyscallInit⭐⭐⭐User/kernel mode switching

πŸ“¦ Third-Party Dependencies

DependencyPurpose
google/googletestTesting framework
charlesnicholson/nanoprintfprintf implementation
MRNIU/cpu_ioCPU I/O operations
riscv-software-src/opensbiRISC-V SBI implementation
MRNIU/opensbi_interfaceOpenSBI interface
u-boot/u-bootUniversal bootloader
OP-TEE/optee_osOP-TEE operating system
ARM-software/arm-trusted-firmwareARM Trusted Firmware
dtc/dtcDevice Tree Compiler
MRNIU/bmallocMemory allocator
MRNIU/MPMCQueueLock-free MPMC queue
MRNIU/device_frameworkDevice management framework

πŸ“ Development Guide

🎨 Code Style

  • Language Standard: C23 / C++23
  • Coding Standard: Google C++ Style Guide
  • Auto Formatting: .clang-format + .clang-tidy
  • Comment Standard: Doxygen style; interface files must contain complete contract documentation

Naming Conventions

TypeStyleExample
Fileslower_snake_casekernel_log.hpp
Classes/StructsPascalCaseTaskManager
FunctionsPascalCase / snake_caseArchInit / sys_yield
Variablessnake_caseper_cpu_data
MacrosSCREAMING_SNAKESIMPLEKERNEL_DEBUG
ConstantskCamelCasekPageSize
Kernel libc/libc++ headerslibc: sk_ prefix, libcxx: kstd_ prefixsk_stdio.h / kstd_vector

πŸ“‹ Git Commit Convention

<type>(<scope>): <subject>

type: feat|fix|docs|style|refactor|perf|test|build|revert
scope: optional, affected module (arch, device, libc)
subject: max 50 chars, no period

πŸ“š Documentation

🀝 Contributing

We welcome all forms of contributions!

🎯 Ways to Contribute

MethodDescription
πŸ› Report IssuesReport bugs via GitHub Issues
πŸ“ Improve InterfacesSuggest better interface abstractions and documentation improvements
πŸ§ͺ Add TestsWrite more comprehensive test cases for existing interfaces
πŸ“– Improve DocumentationEnhance Doxygen comments, add usage examples
πŸ”§ Submit ImplementationsSubmit reference or alternative implementations of interfaces

πŸ”§ Code Contribution Workflow

  1. Fork this repository
  2. Create a feature branch: git checkout -b feat/amazing-feature
  3. Follow coding standards during development
  4. Ensure all tests pass
  5. Commit changes: git commit -m 'feat(scope): add amazing feature'
  6. Create a Pull Request

πŸ“„ License

This project is dual-licensed:


⭐ If this project helps you, please give us a Star!

πŸ€– Let AI write the kernel, so you can focus on understanding OS principles!

🌟 Star the Project β€’ πŸ› Report Issues β€’ πŸ’¬ Join Discussions