最小 x86 内核 Zig
Minimal x86 Kernel Zig

原始链接: https://github.com/lopespm/zig-minimal-kernel-x86

这个项目是一个极简的、裸机内核,完全用 Zig 编写,设计用于在 32 位 x86 机器上使用 Multiboot 1 协议启动。它会在 VGA 文本模式显示器上打印一个彩色问候语,然后停止。 一个关键特性是它的易于交叉编译——它可以在任何宿主机操作系统(包括 macOS ARM)上工作,无需 ISO 镜像、GRUB 或引导加载程序二进制文件。QEMU 直接加载生成的 ELF 二进制文件,利用其内置的 Multiboot 1 支持。 内核初始化一个堆栈,然后调用 `kmain`,后者清除屏幕并显示消息。Zig 处理低级细节,包括直接访问 VGA 缓冲区的内存(地址为 0xB8000),而无需依赖汇编代码或外部库。它使用简单的 `zig build` 和 `zig build run` 命令构建和运行,或者使用提供的辅助脚本,使测试变得简单明了。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Minimal x86 内核 Zig (github.com/lopespm) 9 分,来自 lopespm 2 小时前 | 隐藏 | 过去 | 收藏 | 1 条评论 帮助 ajxs 12 分钟前 [–] 参见:https://wiki.osdev.org/Zig_Bare_Bones 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

A minimal bare-metal kernel written entirely in Zig (zero assembly files). It boots on an x86 (i386) machine via the Multiboot 1 protocol and prints a coloured greeting to the VGA text-mode display, then halts the CPU.

The project is designed to be cross-compiled from any host (including Apple Silicon Macs) and tested instantly with QEMU — no ISO image, no GRUB installation, no bootloader binaries required.

  1. QEMU loads the ELF binary using its built-in Multiboot 1 support.
  2. The CPU starts in 32-bit protected mode at the _start entry point.
  3. _start sets up a 16 KiB stack and jumps to kmain.
  4. kmain clears the VGA text buffer and writes a message to the screen.
  5. The CPU enters an infinite hlt loop.
Tool Version Install
Zig 0.14.0+ ziglang.org/download or brew install zig
QEMU any recent brew install qemu / nix-env -iA nixpkgs.qemu

No other dependencies. Zig bundles its own LLVM back-end and linker, so cross-compilation to x86-freestanding-none works out of the box on any host OS and architecture (macOS ARM, Linux x86_64, etc.).

# Build the kernel (produces zig-out/bin/kernel)
zig build

# Boot it in QEMU (opens a graphical VGA window)
zig build run

# Or use the helper script (curses mode, auto-kills after a few seconds)
chmod +x run.sh
./run.sh

To run QEMU manually with custom flags:

qemu-system-i386 -kernel zig-out/bin/kernel

You should see this:

Screenshot 2026-02-17 at 23 58 16
zig-kernel/
├── build.zig          Zig build script (target, linker, QEMU run step)
├── build.zig.zon      Package manifest
├── linker.ld          Linker script (section layout, entry point)
├── run.sh             Quick-test shell script
└── src/
    └── main.zig       Entire kernel: Multiboot header, VGA driver, kmain
 HOST (macOS ARM / any OS)                    EMULATED x86 MACHINE (QEMU)
 ─────────────────────────                    ──────────────────────────────

 ┌──────────────┐   zig build    ┌────────────────────┐
 │  src/main.zig│───────────────▶│  zig-out/bin/kernel│  (i386 ELF binary)
 │  linker.ld   │  cross-compile │  Multiboot 1 magic │
 │  build.zig   │  x86-free-     │  at offset 0       │
 └──────────────┘  standing-none └─────────┬──────────┘
                                          │
                               qemu-system-i386 -kernel
                                          │
                                          ▼
                               ┌──────────────────────┐
                               │      QEMU / TCG      │
                               │  (x86 CPU emulation) │
                               └─────────┬────────────┘
                                          │
                    ┌─────────────────────┼───────────────────────┐
                    │   Emulated i386 hardware                    │
                    │                     │                       │
                    │   1. Multiboot      │                       │
                    │      loader reads   ▼                       │
                    │      ELF, puts   ┌───────────┐              │
                    │      CPU in      │  _start   │  32-bit      │
                    │      protected   │  (naked)  │  protected   │
                    │      mode        └────┬──────┘  mode        │
                    │                       │                     │
                    │              set up   │ stack               │
                    │                       ▼                     │
                    │                 ┌──────────┐                │
                    │                 │  kmain   │                │
                    │                 └────┬─────┘                │
                    │                      │                      │
                    │          ┌───────────┼───────────┐          │
                    │          │           │           │          │
                    │          ▼           ▼           ▼          │
                    │   clearScreen()  print(...)   hlt loop      │
                    │          │           │                      │
                    │          ▼           ▼                      │
                    │   ┌──────────────────────────────────┐      │
                    │   │  VGA Text Buffer at 0xB8000      │      │
                    │   │  80×25 grid, 16-bit per cell     │      │
                    │   │  (ASCII byte + colour attribute) │      │
                    │   └──────────────────────────────────┘      │
                    │                     │                       │
                    └─────────────────────┼───────────────────────┘
                                          │
                                          ▼
                               ┌──────────────────────┐
                               │   QEMU VGA Window    │
                               │                      │
                               │  ════════════════    │
                               │  Hello from the      │
                               │    Zig Kernel!       │
                               │  ════════════════    │
                               │                      │
                               └──────────────────────┘
  • Target: x86-freestanding-none — 32-bit, no OS, no libc
  • Boot protocol: Multiboot 1 — a 12-byte header (magic 0x1BADB002, flags, checksum) placed in the first 8 KiB of the ELF
  • VGA output: Direct memory-mapped I/O to 0xB8000 using Zig's volatile pointer semantics — no drivers, no BIOS calls
  • Red zone: Disabled — the System V ABI red zone would be corrupted by hardware interrupts
  • SSE/AVX: Disabled — avoids the need to save/restore FPU state
  • No assembly files: The Multiboot header is a Zig extern struct exported to a linker section; the entry point uses inline asm volatile
联系我们 contact @ memedata.com