Writing an OS Kernel from Scratch — A Book to Understand the Operating System Core
About This Book
This is a hands-on tutorial about writing an operating system kernel from scratch.
The operating system textbooks on the market are either thick theory books that put you to sleep after a few pages, or directly dump code without the reader understanding “why it’s designed that way.” This book is different — every chapter is a complete coding session, starting from the lowest-level hardware initialization, building the kernel line by line.
Our ultimate goal is to create a micro-operating system that can boot on real x86_64 hardware, supporting virtual memory, process scheduling, file systems, device drivers, and even a simple command-line Shell. It’s not a toy, but a structurally complete, clearly thought-out real kernel prototype.
What You Will Learn
- CPU Boot and Firmware Protocol: BIOS/MBR → UEFI + GPT, understanding what happens after the computer powers on
- Paging and Virtual Memory: From real mode to protected mode, building process-isolated address spaces
- Physical Memory Management: Buddy system, Slab allocator — principles and implementation of industrial-grade memory management
- Process and Scheduling: Context switching, scheduling algorithms, multi-core support — making multiple tasks truly concurrent
- File System: VFS abstraction layer, ext2 implementation — establishing a unified file interface
- Device Driver Framework: Device model, devfs, character/block devices — writing drivers is no longer mysterious
- Graphics and Windows: Framebuffer, Display Server, LVGL integration — creating a graphical interface with windows
- Networking Basics: TCP/IP protocol stack introduction, using socket to fetch the Baidu homepage
- User-Mode Toolchain: Implementing basic commands like ls, cat, mkdir yourself
Content Structure
The book is divided into six volumes:
Volume I: Boot and Infrastructure (Chapters 1–6)
Starting from the bottom: Multiboot protocol, entering protected mode, enabling paging, building stack and heap. Your kernel will gain the basic ability to “stay alive” in this volume.
Volume II: Process and Memory (Chapters 7–17)
How are processes created? What is the essence of fork/exec? How are Virtual Memory Areas (VMA) managed? This volume delves into the operating system’s two most core subsystems.
Volume III: File System (Chapters 18–21)
Building the VFS abstraction layer, implementing the ext2 file system, giving the kernel persistent storage capability. User program loading, dynamic linking — all based on this file system.
Volume IV: Graphics and Windows (Chapters 22–30)
From Framebuffer to Display Server to LVGL integration — creating a graphical desktop prototype with windows and mouse support. Window manager, event dispatch, component layout — all written by hand.
Volume V: Devices and Drivers (Chapters 31–35)
Device model, devfs, PCIe driver framework, e1000 NIC driver. Understanding the design philosophy of the Linux device driver model and applying it to your own kernel.
Volume VI: Networking and Advanced Topics (Chapters 36–Final)
TCP/IP protocol stack implementation, using socket to read HTTP pages, multi-core and concurrency advanced. The final chapters compare the hand-written kernel with real Linux design, helping you build a broader global perspective.
Suggested Learning Path
Beginner Path (Chapter 1 → Chapter 35, follow along sequentially)
Read in order; each chapter has complete runnable code. It’s recommended to debug step by step in QEMU + GDB, understanding register state changes at each step.
Engineer Path (Read theory chapters first, then fill in code)
Some chapters are more principle-oriented (memory management, scheduler); you can quickly read through the design ideas first, then return to specific implementations for side-by-side reading.
Advanced Path (After Chapter 35 + comparison with Linux source)
The final chapters provide extensive comparison with the Linux kernel, suitable for students with some foundation to learn through comparison.
Prerequisites
- C Language: Able to read pointers, structs, bit operations — no need for template metaprogramming expertise
- Computer Organization: Intermediate level is enough — know what CPU, memory, and registers are
- Command-Line Basics: Know how to compile with gcc and start with qemu — everything else is taught step by step in the book
You don’t need to know assembly (this book teaches x86 assembly as needed), you don’t need to have studied operating systems (but curiosity helps), you don’t need any particular “mastery” — just a computer and the willingness to take action.
Code Notes
All code in this book is complete, runnable, production-ready code. The code for each chapter has been verified on actual startup (dual-verified on QEMU + real hardware). Each chapter begins with the code repository version and ends with solutions to common pitfalls.
Code repository URL: [GitHub Repository] (to be added after publication)
Join the Discussion
If you encounter problems during learning, feel free to leave comments. Each chapter is accompanied by corresponding WeChat public account articles; scan the QR code to join the discussion.
The original intention of writing this book was simple: when I was learning OS, I found that all materials either went too deep (directly analyzing Linux source code) or were too shallow (just explaining concepts, nothing hands-on). This book fills that gap — starting from the hardware, building from scratch, implementing every feature for real.
I hope this book helps you truly understand operating systems — not just to pass exams, but to write code.
Let’s start at the very beginning.
Comments