苔藓:一个26,000行代码的Rust Linux兼容内核
Moss: a Rust Linux-compatible kernel in 26,000 lines of code

原始链接: https://github.com/hexagonal-sun/moss

## Moss:基于Rust的微内核 Moss是一个新的、与Linux兼容的内核,使用Rust和Aarch64汇编构建,专为现代硬件和安全性设计。它利用Rust的async/await模型来防止常见的内核死锁,并具有模块化架构,方便移植到其他平台,如x86_64和RISC-V。 目前,Moss拥有完整的Aarch64支持,一个功能完善的内存管理系统(包括CoW页面),并且由于与Linux用户空间的兼容性,可以运行大多数BusyBox命令。它实现了51个Linux系统调用,一个虚拟文件系统(带有只读FAT32驱动程序),以及基本设备驱动程序。 `libkernel`促进了开发,实现了基于主机的测试。一个全面的230多个测试套件确保了跨架构的功能。用户可以使用QEMU和aarch64-none-elf工具链来构建和运行Moss。 正在进行中的开发重点是扩展系统调用兼容性,实现网络堆栈,以及改进调度器。Moss积极寻求贡献,并以MIT许可证分发。

## Moss:基于Rust的内核 开发者hexagonal-sun发布了Moss,一个用Rust和汇编编写的、与Linux兼容的内核,代码量仅26,000行。经过八个月的开发,该项目现已在GitHub上公开 ([https://github.com/hexagonal-sun/moss](https://github.com/hexagonal-sun/moss))。 目前Moss的目标架构是ARM64,已成功在QEMU以及包括树莓派4和Jetson Nano在内的多个开发板上运行。它已经可以运行大部分BusyBox命令,通过控制台进行操作。 仍在开发的关键特性包括强大的文件系统驱动(目前支持只读FAT32)和网络功能。开发者欢迎社区贡献和反馈。有用户建议该帖子更适合发布在Hacker News的“ShowHN”版块。
相关文章

原文

Architecture Language License

Moss Boot Demo

moss is a Unix-like, Linux-compatible kernel written in Rust and Aarch64 assembly.

It features a modern, asynchronous core, a modular architecture abstraction layer, and binary compatibility with Linux userspace applications (currently capable of running most BusyBox commands).

  • Full support for aarch64.
  • A well-defined HAL allowing for easy porting to other architectures (e.g., x86_64, RISC-V).
  • Memory Management:
    • Full MMU enablement and page table management.
    • Copy-on-Write (CoW) pages.
    • Safe copy to/from userspace async functions.
    • Kernel and userspace page fault management.
    • Buddy allocator for physical addresses and smalloc for boot allocations and tracking memory reservations.

One of the defining features of moss is its usage of Rust's async/await model within the kernel context:

  • All non-trivial system calls are written as async functions, sleep-able functions are prefixed with .await.
  • The compiler enforces that spinlocks cannot be held over sleep points, eliminating a common class of kernel deadlocks.
  • Full task management including scheduling and task migration via IPIs.
  • Currently implements 51 Linux syscalls; sufficient to execute most BusyBox commands.
  • Advanced forking capabilities via clone().
  • Process and thread signal delivery and raising support.
  • Virtual File System with full async abstractions.
  • Drivers:
    • Ramdisk block device implementation.
    • FAT32 filesystem driver (ro).
    • devtmpfs driver for kernel character device access.

moss is built on top of libkernel, a utility library designed to be architecture-agnostic. This allows logic to be tested on a host machine (e.g., x86) before running on bare metal.

  • Address Types: Strong typing for VA (Virtual), PA (Physical), and UA (User) addresses.
  • Containers: VMA management, generic page-based ring buffer (kbuf), and waker sets.
  • Sync Primitives: spinlock, mutex, condvar, per_cpu.
  • Test Suite: A comprehensive suite of 230+ tests ensuring functionality across architectures (e.g., validating Aarch64 page table parsing logic on an x86 host).

You will need QEMU for aarch64 emulation and dosfstools to create the virtual file system.

sudo apt install qemu-system-aarch64 dosfstools

Additionally you will need a version of the aarch64-none-elf toolchain installed.

To install aarch64-none-elf on any os, download the correct release of aarch64-none-elf onto your computer, unpack it, then export the bin folder to path.

Run the following command

nix shell nixpkgs#pkgsCross.aarch64-embedded.stdenv.cc nixpkgs#pkgsCross.aarch64-embedded.stdenv.cc.bintools

First, run the following script to prepare the binaries for the image:

This will download and build the necessary dependencies for the kernel and put them into the build directory.

Once that is done, you can create the image using the following command:

sudo ./scripts/create-image.sh

This will create an image file named moss.img in the root directory of the project, format it as VFAT 32 and create the necessary files and directories for the kernel.

This script needs to run with sudo to mount the image through a loop device, which is required to properly create the image for the kernel to work.

To build the kernel and launch it in QEMU:

Because libkernel is architecturally decoupled, you can run the logic tests on your host machine:

cargo test -p libkernel --target x86_64-unknown-linux-gnu

moss is under active development. Current focus areas include:

  • Basic Linux Syscall Compatibility (Testing through BusyBox).
  • Networking Stack: TCP/IP implementation.
  • Scheduler Improvements: Task load balancing.
  • A fully read/write capable filesystem driver (e.g., ext2/4).
  • Expanding coverage beyond the current 49 calls.

Contributions are welcome! Whether you are interested in writing a driver, porting to x86, or adding syscalls.

Distributed under the MIT License. See LICENSE for more information.

联系我们 contact @ memedata.com