系统与基础设施github.com
x86 裸机编程示例集
x86 Bare Metal Examples
2019 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这个 GitHub 仓库收集 x86 裸机编程示例,展示在没有操作系统的情况下运行的最小低层代码,常配合 QEMU 等模拟器实验。它面向想从代码层面理解启动、中断和硬件交互的开发者,评论也围绕学习路径、32/64 位差异和现代平台复杂性展开。
为什么经久不衰:因为它把抽象的裸机概念落实为可运行的小例子,是低层编程少见而实用的入口,多年后仍适合拿来动手实验。
HN 评论精选
呼吁提供更多关于现代 x86 平台事务路由、MSI/DMA 等机制的系统性概览,而不只讲遗留接口。
这些例子很好,可能有点离题:我希望有更多资源讲现代 x86 平台实际如何工作,而不是这些遗留接口——它们要么在 SMM 中通过 IO 端口 trapping 模拟,要么得靠芯片组 hack 维持(如 PCI 规范里的 VGA 设备 hack,或芯片组遗留 IO 端口解码到 LPC bridge)。真正理解当今平台每个方面几乎不可能,但研究平台如何路由事务(uncore/芯片组如何用基址寄存器和解码窗口路由 TLP,无论是原始 PCIe 还是被 IF/HyperTransport/DMI/QPI 等包装)能建立很多直觉,也有助于理解现代 MSI 中断和 DMA。深入 PCI 配置、ACPI、UEFI、现代启动、DMA/中断重映射、DRAM、安全与电源管理等是多年功课,但核心思想不必分散各处才能让人熟悉。
emily-c查看英文原文
These are good examples and this might be slightly off topic but I wish that there were more resources on how modern x86 platforms actually work instead of using these legacy interfaces that are either emulated in SMM with IO port trapping or relegated to requiring chipset hacks to keep them working (such as VGA device hacks in the PCI spec or well known chipset legacy IO ports decoding to the LPC bridge). While it is essentially impossible to truly understand every aspect of todays platforms I think that one can build a lot of intuition about how things work by studying how transaction routing is handled in the platform, e.g. how the uncore and chipset routes TLPs (whether it's raw PCIe or packets essentially wrapped by other interconnect protocols like IF/HyperTransport/DMI/QPI/etc) through the system with various base address registers and decoding windows. This gives way to understanding how modern MSI interrupts and DMA works as well. While I understand that truly going into things like PCI configuration, ACPI, UEFI, chipset interfaces on bus 0, modern booting, DMA and interrupt remapping, DRAM, register locking and security, security processors, power management, various types of runtime firmware, etc is a a many-years-long exercise I think that the core ideas don't need to be spread around all over the place for one to get familiarity with the ideas and patterns that underpin our PCs. I think that a general conceptual overview on which one can hang more detailed knowledge acquired by reading specs is needed with the amount of complexity that exists today. There are some really good resources that I've collected over the years but there is still something missing. Maybe I or someone else will get around to it some day.
建议直接学 x86_64,并提醒模拟器的简化行为会掩盖真实硬件复杂性。
如今最好只用 x86_64。32 位 x86 有很多缺点,64 位在很多方面更干净更好:没有分段、平坦地址空间、更好的中断处理、更好的系统调用、更多寄存器等。另外,QEMU 等虚拟化技术可能掩盖一些硬件问题:每个硬件设备(键盘、USB、显示、磁盘等)都会被简化,如果在 x86_64 主机上模拟 ARM CPU,真实硬件上的缓存一致性会完全乱掉。
andy_threos_io查看英文原文
Nowadays, it's better to use x86_64 only. The 32 bit x86 has many drawbacks. The 64 bit is much cleaner and better in many many ways. -no segmentation, flat address space -better interrupt handling -better system call -more registers etc. Also we found that Qemu or other Virtualization technology may cover up some of the hardware issues. Eg. Beside, that every hardware device (keyboard, usb, display, disk etc.) will have some simplified behavior, if you emulate an ARM cpu on x86_64 host, than the cache coherency will totally messed up on real hardware.
询问是否有 GRUB2 加载 64 位 ELF 长模式 Hello World 的最小示例。
你有用 GRUB2 引导加载 64 位 ELF 内核的 hello-world 示例吗?不是 Linux 内核,我字面意思就是一个只打印“hello world”的简单程序,类似 [1] 但运行在长模式下。
mammalutte查看英文原文
Do you have any hello-world examples of how to load a 64bit ELF kernel with a GRUB2 bootloader? I don't mean Linux kernel, I literally mean a simple program that only prints "hello world". Something like this [1] but in long mode. [1] https://wiki.osdev.org/Bare_Bones
底层编程初学者容易不知所措,向作者寻求入门建议。
我看到 Ciro Santilli 在这里,所以提个问题。我想开始学习底层编程,但每次尝试都感到非常不知所措。你有什么建议吗?
_chiliconcarne查看英文原文
I'll drop a question since I see Ciro Santilli is around. I want to start learning low level programming. Yet, I feel awfully overwhelmed every single time I try. Any recommendations you might have?
历次讨论
同类还值得读
TTY 揭秘(2008)
The TTY demystified (2008)
经典文章深入剖析了 Unix/Linux 中 TTY 子系统的工作原理,包括线路规程、终端驱动、信号与控制字符等机制。
系统与基础设施上首页 11 次 · 2009–2025linusakesson.net编辑精选为什么 GNU grep 这么快
Why GNU grep is Fast
FreeBSD 邮件列表上一篇广为流传的帖子,解释 GNU grep 为什么快:核心在于 Boyer-Moore 字符串搜索算法配合精心优化的内部循环和 I/O,让程序在搜索时“…
系统与基础设施上首页 9 次 · 2010–2022lists.freebsd.orgXplain:为普通人讲解 X11
Xplain – Explaining X11 for the rest of us
《Xplain》是作者(HN 用户 Jasper_)写的一系列 X11 长文,用他在浏览器中重建的 X11 绘图模型做交互演示,讲解 X11 的绘图模型与窗口系统设计。
系统与基础设施上首页 6 次 · 2013–2020magcius.github.io编辑精选旋风教程:为 Linux 创建极小 ELF 可执行文件(1999)
A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux (1999)
breadbox 写于 1999 年的经典教程,主题是如何为 Linux 手工构造并裁剪出体积极小的 ELF 可执行文件。
系统与基础设施上首页 6 次 · 2007–2025muppetlabs.comPostgreSQL 内部原理
The internals of PostgreSQL
《The Internals of PostgreSQL》是一本免费在线书籍,系统讲解 PostgreSQL 的内部实现,包括查询处理、并发控制、WAL、缓冲区管理等核心机制。
系统与基础设施上首页 5 次 · 2017–2025interdb.jp编辑精选