Writing Operating Systems in Rust
{"all_nodes":"PHASE 1 fill complete; deterministic lint clean (2026-10-12)"}local graph
Writing Operating Systems in Rust — Aura Fenwick
Section titled “Writing Operating Systems in Rust — Aura Fenwick”chapter_01
Section titled “chapter_01”node-book_writing-operating-systems-in-rust_chapter-01-kernel-development-foundations-in-rust — Kernel Development Foundations in Rust
chapter_02
Section titled “chapter_02”node-book_writing-operating-systems-in-rust_chapter-02-bootloaders-and-the-first-kernel-entry-point — Bootloaders and the First Kernel Entry Point
chapter_03
Section titled “chapter_03”node-book_writing-operating-systems-in-rust_chapter-03-bare-metal-output-panic-handling-and-early-debugging — Bare-Metal Output, Panic Handling, and Early Debugging
chapter_04
Section titled “chapter_04”node-book_writing-operating-systems-in-rust_chapter-04-x86-64-cpu-setup-and-kernel-execution-state — x86_64 CPU Setup and Kernel Execution State
chapter_05
Section titled “chapter_05”node-book_writing-operating-systems-in-rust_chapter-05-interrupts-exceptions-and-fault-handling — Interrupts, Exceptions, and Fault Handling
chapter_06
Section titled “chapter_06”node-book_writing-operating-systems-in-rust_chapter-06-physical-memory-and-boot-memory-maps — Physical Memory and Boot Memory Maps
chapter_07
Section titled “chapter_07”node-book_writing-operating-systems-in-rust_chapter-07-virtual-memory-paging-and-address-spaces — Virtual Memory, Paging, and Address Spaces
chapter_08
Section titled “chapter_08”node-book_writing-operating-systems-in-rust_chapter-08-heap-allocation-and-safe-kernel-abstractions — Heap Allocation and Safe Kernel Abstractions
chapter_09
Section titled “chapter_09”node-book_writing-operating-systems-in-rust_chapter-09-hardware-interrupts-timers-and-input-devices — Hardware Interrupts, Timers, and Input Devices
chapter_10
Section titled “chapter_10”node-book_writing-operating-systems-in-rust_chapter-10-kernel-services-testing-and-debugging-workflow — Kernel Services, Testing, and Debugging Workflow
chapter_11
Section titled “chapter_11”node-book_writing-operating-systems-in-rust_chapter-11-advanced-boot-hardware-discovery-and-next-system-features — Advanced Boot, Hardware Discovery, and Next-System Features
Title Page
Section titled “Title Page”WRITING OPERATING SYSTEMS IN RUST:
Section titled “WRITING OPERATING SYSTEMS IN RUST:”KERNEL DEVELOPMENT FROM SCRATCH
Section titled “KERNEL DEVELOPMENT FROM SCRATCH”BOOT SEQUENCE
Section titled “BOOT SEQUENCE”- POWER ON
- BIOS / UEFI
- MEMORY MAP
- LOAD KERNEL
- SWITCH TO LONG MODE
- JUMP TO KERNEL
KERNEL
Section titled “KERNEL”rust #![no_std] #![no_main]
use core::panic::PanicInfo;
#[no_mangle] pub extern “C” fn _start() -> ! { kmain(); loop {} }
fn kmain() -> ! { println!(“hello kernel”); init(); hlt_loop(); }
### INTERRUPT VECTOR TABLE<table> <thead> <tr> <th>00: Divide Error</th> <th>handler_divide_error</th> </tr> </thead> <tbody> <tr> <td>01: Debug</td> <td>handler_debug</td> </tr> <tr> <td>0E: Page Fault</td> <td>handler_page_fault</td> </tr> <tr> <td>80: General Protection</td> <td>handler_gp</td> </tr> <tr> <td>20: Timer Interrupt</td> <td>handler_timer</td> </tr> <tr> <td>21: Keyboard Interrupt</td> <td>handler_keyboard</td> </tr> </tbody></table>
### VIRTUAL MEMORY<table> <thead> <tr> <th>PML4</th> <th>PDP</th> <th>PDPT</th> <th>PD</th> <th>PT</th> <th>PT</th> </tr> </thead> <tbody> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </tbody></table>
### PHYSICAL FRAMES- 0x0000_0000- 0x0001_0000- 0x0002_0000- 0x0003_0000- ...- 0xFFFF_F000
### CPU REGISTERS<table> <thead> <tr> <th>Register</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>RIP</td> <td>0xFFFF800000102A3F</td> </tr> <tr> <td>RSP</td> <td>0xFFFF8000201FF380</td> </tr> <tr> <td>RFLAGS</td> <td>0x0000000000000202</td> </tr> <tr> <td>CR0</td> <td>0x80050033</td> </tr> <tr> <td>CR3</td> <td>0x0000000123A45000</td> </tr> <tr> <td>CR4</td> <td>0x0000000003606E0</td> </tr> </tbody></table>
### STACK<table> <thead> <tr> <th>Address</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>0xFFFF8000201FF380</td> <td>0x0000000000000000</td> </tr> <tr> <td>0xFFFF8000201FF388</td> <td>0xFFFF800000102A41</td> </tr> <tr> <td>0xFFFF8000201FF3C0</td> <td>0x0000000000000000</td> </tr> <tr> <td>0xFFFF8000201FF3C8</td> <td>0x0000000000000000</td> </tr> </tbody></table>
### ASSEMBLY CODEFFFF800000102A3F: cli FFFF800000102A40: push rbp FFFF800000102A41: mov rbp, rsp FFFF800000102A44: sub rsp, 0x20 FFFF800000102A48: call init FFFF800000102A4D: hlt FFFF800000102A4E: jmp .
---
**AURA FENWICK**<!-- page: 002 -->function uses a predictable ABI at the boundary. The function returns `!`, the never type, because kernel entry does not return to a caller in the ordinary application sense. If the kernel has nothing else to do, it loops forever.
The `#[unsafe(no_mangle)]` attribute controls symbol naming. Rust normally mangles function names to support namespaces, generics, and other language features. A bootloader or linker configuration often expects a specific symbol name, such as `_start`. Preventing name mangling ensures the symbol appears with the exact name required. The attribute is unsafe because exporting a symbol under a fixed name can collide with other symbols or create ABI assumptions the compiler cannot verify.
The panic handler is required because Rust needs to know what to do when a panic occurs. In a normal application, panic behavior may unwind the stack or abort the process. In a kernel, stack unwinding is usually disabled. The kernel cannot rely on the operating system to terminate it, print a message, or clean up resources. A panic handler in early code usually stops the CPU in a controlled loop. Later, after serial output exists, the panic handler should print diagnostic information before halting.
A more useful early version prints panic information once a serial writer exists. The following code assumes that a `serial_println!` macro is available somewhere else in the crate. The important point is not the macro itself, but the behavior: panics should become visible as early as possible.
Rust
```rustuse core::panic::PanicInfo;
#[panic_handler]fn panic(info: &PanicInfo) -> ! { serial_println!("kernel panic: {}", info);
loop { x86_64::instructions::hlt(); }}This version avoids a pure spin loop and uses hlt once the architecture support is available. The hlt instruction tells the CPU to wait until the next
7
Copyright © 2026 Aura Fenwick
Section titled “Copyright © 2026 Aura Fenwick”All rights reserved.
No part of this book may be reproduced, stored in a retrieval system, transmitted, distributed, or used in any form or by any means, including electronic, mechanical, photocopying, recording, scanning, or otherwise, without prior written permission from the copyright owner, except for brief quotations used in reviews, commentary, or scholarly reference.
This book is intended for educational and informational purposes only. The material explains operating system concepts, Rust programming patterns, kernel development techniques, low-level system programming, memory management, boot processes, debugging workflows, and related technical practices. While every effort has been made to present accurate and practical information, technology, tools, libraries, hardware behavior, firmware behavior, compiler behavior, and development environments may change over time. The author and publisher make no warranties regarding the completeness, reliability, suitability, or continued accuracy of the information provided.
The code examples and technical demonstrations in this book are provided for learning purposes. They should be tested in controlled environments, such as virtual machines or isolated development setups, before any use in sensitive, production, or hardware-dependent contexts. The author and publisher are not responsible for errors, omissions, data loss, hardware issues, software failures, security vulnerabilities, or any damages arising from the use or misuse of the information, code, configurations, or techniques described in this book.
Rust, QEMU, UEFI, x86_64, and other technical terms, tools, platforms, libraries, product names, and trademarks mentioned in this book are the property of their respective owners. Their use is for identification, explanation, and educational reference only, and does not imply endorsement, sponsorship, or affiliation.
This publication is independently produced and is not affiliated with, endorsed by, sponsored by, or officially connected to any company, foundation, standards body, software project, or hardware manufacturer mentioned herein.
First Edition
Table of Contents
Section titled “Table of Contents”Table of Contents
Section titled “Table of Contents”CHAPTER 1: KERNEL DEVELOPMENT FOUNDATIONS IN RUST… 1
Section titled “CHAPTER 1: KERNEL DEVELOPMENT FOUNDATIONS IN RUST… 1”- 1.1: WHAT THE KERNEL OWNS AND WHAT THE BOOTLOADER PREPARES … 2
- 1.2: RUST IN A FREESTANDING ENVIRONMENT … 5
- 1.3: SETTING UP THE TOOLCHAIN, TARGET, AND BUILD WORKFLOW … 13
- 1.4: PROJECT STRUCTURE FOR A MAINTAINABLE RUST KERNEL … 19
CHAPTER 2: BOOTLOADERS AND THE FIRST KERNEL ENTRY POINT … 29
Section titled “CHAPTER 2: BOOTLOADERS AND THE FIRST KERNEL ENTRY POINT … 29”- 2.1: BIOS, UEFI, AND THE KERNEL HANDOFF PROCESS … 30
- 2.2: CHOOSING A PRACTICAL BOOT STRATEGY FOR THE PROJECT … 34
- 2.3: BUILDING THE FIRST BOOTABLE RUST KERNEL … 37
- 2.4: RUNNING AND INSPECTING THE KERNEL IN QEMU … 45
CHAPTER 3: BARE-METAL OUTPUT, PANIC HANDLING, AND EARLY DEBUGGING … 53
Section titled “CHAPTER 3: BARE-METAL OUTPUT, PANIC HANDLING, AND EARLY DEBUGGING … 53”- 3.1: WRITING TO SERIAL OUTPUT BEFORE SCREEN OUTPUT WORKS … 54
- 3.2: IMPLEMENTING A KERNEL PANIC HANDLER … 61
- 3.3: PRINTING TO VGA TEXT MODE AND FRAMEBUFFER OUTPUT … 68
- 3.4: DIAGNOSING BLACK SCREENS, REBOOTS, AND SILENT FAILURES … 79
CHAPTER 4: X86_64 CPU SETUP AND KERNEL EXECUTION STATE … 87
Section titled “CHAPTER 4: X86_64 CPU SETUP AND KERNEL EXECUTION STATE … 87”- 4.1: LONG MODE, PRIVILEGE LEVELS, AND CPU STATE … 88
- 4.2: SETTING UP THE GLOBAL DESCRIPTOR TABLE … 91
- 4.3: CONFIGURING THE TASK STATE SEGMENT AND SAFE INTERRUPT STACKS … 98
- 4.4: READING CONTROL REGISTERS AND UNDERSTANDING PROCESSOR STATE … 104
i
CHAPTER 5: INTERRUPTS, EXCEPTIONS, AND FAULT HANDLING
Section titled “CHAPTER 5: INTERRUPTS, EXCEPTIONS, AND FAULT HANDLING”5.1: BUILDING THE INTERRUPT DESCRIPTOR TABLE … 114
5.2: HANDLING BREAKPOINTS, PAGE FAULTS, AND GENERAL PROTECTION FAULTS … 119
5.3: PREVENTING AND DEBUGGING DOUBLE FAULTS … 125
5.4: TESTING EXCEPTION HANDLERS IN QEMU … 132
CHAPTER 6: PHYSICAL MEMORY AND BOOT MEMORY MAPS 140
Section titled “CHAPTER 6: PHYSICAL MEMORY AND BOOT MEMORY MAPS 140”6.1: READING THE BOOTLOADER MEMORY MAP … 141
6.2: IDENTIFYING USABLE, RESERVED, FIRMWARE, AND KERNEL MEMORY REGIONS … 147
6.3: DESIGNING A PHYSICAL FRAME ALLOCATOR … 153
6.4: PROTECTING KERNEL, BOOT INFO, STACK, AND FRAMEBUFFER MEMORY … 160
CHAPTER 7: VIRTUAL MEMORY, PAGING, AND ADDRESS SPACES
Section titled “CHAPTER 7: VIRTUAL MEMORY, PAGING, AND ADDRESS SPACES”7.1: PAGES, FRAMES, PAGE TABLES, AND ADDRESS TRANSLATION … 172
7.2: MAPPING AND UNMAPPING VIRTUAL PAGES … 176
7.3: HIGHER-HALF KERNELS, OFFSET MAPPING, AND IDENTITY MAPPING … 185
7.4: DEBUGGING PAGE FAULTS, TLB FLUSHES, AND MAPPING ERRORS … 193
CHAPTER 8: HEAP ALLOCATION AND SAFE KERNEL ABSTRACTIONS
Section titled “CHAPTER 8: HEAP ALLOCATION AND SAFE KERNEL ABSTRACTIONS”8.1: ENABLING DYNAMIC ALLOCATION WITH THE ALLOC CRATE … 205
8.2: IMPLEMENTING A BUMP ALLOCATOR AND UNDERSTANDING ITS LIMITS … 213
8.3: BUILDING LINKED-LIST AND FIXED-SIZE BLOCK ALLOCATORS … 220
ii
8.4: MANAGING UNSAFE CODE, GLOBAL STATE, AND KERNEL INVARIANTS 232
CHAPTER 9: HARDWARE INTERRUPTS, TIMERS, AND INPUT DEVICES 243
9.1: CONFIGURING THE PROGRAMMABLE INTERRUPT CONTROLLER 244
9.2: HANDLING TIMER INTERRUPTS AND KERNEL TICKS 249
9.3: READING KEYBOARD SCANCODES AND BUILDING AN INPUT BUFFER 254
9.4: WORKING WITH PORT I/O, MMIO, AND VOLATILE ACCESS 260
CHAPTER 10: KERNEL SERVICES, TESTING, AND DEBUGGING WORKFLOW 270
10.1: ORGANIZING KERNEL MODULES AND ARCHITECTURE-SPECIFIC CODE 271
10.2: BUILDING A MINIMAL INTERACTIVE KERNEL SHELL 278
10.3: WRITING KERNEL TESTS WITH SERIAL OUTPUT AND QEMU EXIT CODES 286
10.4: DEBUGGING WITH QEMU LOGS, GDB, REGISTERS, AND STACK INSPECTION 293
CHAPTER 11: ADVANCED BOOT, HARDWARE DISCOVERY, AND NEXT-SYSTEM FEATURES 302
11.1: UNDERSTANDING UEFI LOADERS AND EXIT BOOT SERVICES 303
11.2: COMPARING MODERN BOOT PROTOCOLS AND KERNEL HANDOFF STRUCTURES 311
11.3: INTRODUCING ACPI, APIC, SMP, AND REAL HARDWARE CONSTRAINTS 318
11.4: ROADMAP TO FILESYSTEMS, USERSPACE, SCHEDULING, AND SYSTEM CALLS 322
iii