<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://johnwick.cc/index.php?action=history&amp;feed=atom&amp;title=Why_Writing_Device_Drivers_in_Rust_Changes_Everything</id>
	<title>Why Writing Device Drivers in Rust Changes Everything - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://johnwick.cc/index.php?action=history&amp;feed=atom&amp;title=Why_Writing_Device_Drivers_in_Rust_Changes_Everything"/>
	<link rel="alternate" type="text/html" href="https://johnwick.cc/index.php?title=Why_Writing_Device_Drivers_in_Rust_Changes_Everything&amp;action=history"/>
	<updated>2026-05-06T16:44:05Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.1</generator>
	<entry>
		<id>https://johnwick.cc/index.php?title=Why_Writing_Device_Drivers_in_Rust_Changes_Everything&amp;diff=488&amp;oldid=prev</id>
		<title>PC: Created page with &quot;500px  There’s a quiet revolution happening in the kernel space — and it’s written in Rust. For decades, device drivers have been the most crash-prone, security-sensitive, and soul-draining part of system software. A small mistake in a pointer dereference or a missing free() call could bring down an entire system. C and C++ gave us speed and control — but at a brutal cost: undefined behavior. Then came Rust — and suddenly, t...&quot;</title>
		<link rel="alternate" type="text/html" href="https://johnwick.cc/index.php?title=Why_Writing_Device_Drivers_in_Rust_Changes_Everything&amp;diff=488&amp;oldid=prev"/>
		<updated>2025-11-19T08:09:00Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&lt;a href=&quot;/index.php?title=File:Why_writing_device.jpg&quot; title=&quot;File:Why writing device.jpg&quot;&gt;500px&lt;/a&gt;  There’s a quiet revolution happening in the kernel space — and it’s written in Rust. For decades, device drivers have been the most crash-prone, security-sensitive, and soul-draining part of system software. A small mistake in a pointer dereference or a missing free() call could bring down an entire system. C and C++ gave us speed and control — but at a brutal cost: undefined behavior. Then came Rust — and suddenly, t...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[file:Why_writing_device.jpg|500px]]&lt;br /&gt;
&lt;br /&gt;
There’s a quiet revolution happening in the kernel space — and it’s written in Rust.&lt;br /&gt;
For decades, device drivers have been the most crash-prone, security-sensitive, and soul-draining part of system software. A small mistake in a pointer dereference or a missing free() call could bring down an entire system. C and C++ gave us speed and control — but at a brutal cost: undefined behavior.&lt;br /&gt;
Then came Rust — and suddenly, the idea of writing safe, fast, memory-efficient drivers didn’t sound like a fantasy anymore.&lt;br /&gt;
Let’s dive deep into how Rust is changing the game at the hardware boundary, what’s really happening under the hood, and why even Linux — the temple of C — has started letting Rust in.&lt;br /&gt;
The Core Idea: Safety Without Sacrifice&lt;br /&gt;
Writing device drivers is like walking a tightrope above a volcano. You deal with:&lt;br /&gt;
* 		Raw memory (MMIO registers, DMA buffers)&lt;br /&gt;
* 		Interrupts and race conditions&lt;br /&gt;
* 		Concurrency across threads and hardware&lt;br /&gt;
* 		Strict timing and real-time constraints&lt;br /&gt;
Traditionally, the only language trusted for this job was C. But here’s the thing: C trusts you back. It assumes you’ll never misuse a pointer, never race two threads, never access freed memory.&lt;br /&gt;
Rust doesn’t make that assumption.&lt;br /&gt;
Its ownership model and borrow checker make sure that at compile time, the same bugs that plagued decades of C drivers simply cannot compile.&lt;br /&gt;
A Minimal Rust Driver Example&lt;br /&gt;
Let’s look at how a toy Rust driver might interact with memory-mapped I/O (MMIO) registers.&lt;br /&gt;
use core::ptr::{read_volatile, write_volatile};&lt;br /&gt;
&lt;br /&gt;
const DEVICE_BASE: usize = 0x1000_0000;&lt;br /&gt;
const REG_STATUS: usize = DEVICE_BASE + 0x00;&lt;br /&gt;
const REG_CONTROL: usize = DEVICE_BASE + 0x04;&lt;br /&gt;
pub struct MyDevice;&lt;br /&gt;
impl MyDevice {&lt;br /&gt;
    pub fn read_status(&amp;amp;self) -&amp;gt; u32 {&lt;br /&gt;
        unsafe { read_volatile(REG_STATUS as *const u32) }&lt;br /&gt;
    }&lt;br /&gt;
    pub fn write_control(&amp;amp;self, value: u32) {&lt;br /&gt;
        unsafe { write_volatile(REG_CONTROL as *mut u32, value) }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
Even here, the unsafe blocks are explicit — not accidental. Rust forces you to acknowledge when you’re doing something risky.&lt;br /&gt;
That’s the big difference: Rust doesn’t remove low-level power — it just makes unsafe code visible and containable.&lt;br /&gt;
Architecture of a Rust Driver&lt;br /&gt;
A modern Rust-based driver typically follows this layered architecture:&lt;br /&gt;
┌──────────────────────────────┐&lt;br /&gt;
│      User Space Interface    │  ←  sysfs, ioctls, etc.&lt;br /&gt;
├──────────────────────────────┤&lt;br /&gt;
│   Safe Rust Abstractions     │  ←  typed wrappers for registers, DMA, buffers&lt;br /&gt;
├──────────────────────────────┤&lt;br /&gt;
│   Unsafe HAL Layer           │  ←  raw MMIO, interrupts, hardware setup&lt;br /&gt;
├──────────────────────────────┤&lt;br /&gt;
│   Kernel Integration Layer   │  ←  Linux driver APIs or RTOS hooks&lt;br /&gt;
└──────────────────────────────┘&lt;br /&gt;
Code Flow Example&lt;br /&gt;
* 		User process sends a command via /dev/my_device.&lt;br /&gt;
* 		Kernel calls into Rust driver entry point.&lt;br /&gt;
* 		Safe layer decodes command → calls into HAL.&lt;br /&gt;
* 		HAL performs memory-mapped write using controlled unsafe.&lt;br /&gt;
* 		Response bubbles back through safe abstractions to user.&lt;br /&gt;
Rust ensures that only one layer is truly unsafe — the HAL. Everything above it benefits from strong typing and ownership rules.&lt;br /&gt;
Real Reason Behind Rust in Kernel Space&lt;br /&gt;
Why did Linux — the most conservative kernel project — agree to include Rust drivers?&lt;br /&gt;
Because C’s safety debt has become unsustainable.&lt;br /&gt;
Here’s what the Linux Foundation’s security team found:&lt;br /&gt;
~70% of kernel vulnerabilities trace back to memory safety issues.&lt;br /&gt;
That’s not about logic errors or bad design — that’s about pointer mistakes. Rust eliminates those entirely for most driver logic.&lt;br /&gt;
Miguel Ojeda, the engineer leading the Rust-for-Linux project, put it best:&lt;br /&gt;
“Rust can give kernel developers memory safety by default, while still letting them go low-level when they need to.”&lt;br /&gt;
Example: Writing a Rust Driver for a UART Controller&lt;br /&gt;
Here’s a simplified Rust UART driver snippet:&lt;br /&gt;
pub struct Uart {&lt;br /&gt;
    base_addr: *mut u8,&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
impl Uart {&lt;br /&gt;
    pub fn new(base: usize) -&amp;gt; Self {&lt;br /&gt;
        Self { base_addr: base as *mut u8 }&lt;br /&gt;
    }&lt;br /&gt;
    pub fn write_byte(&amp;amp;self, byte: u8) {&lt;br /&gt;
        unsafe {&lt;br /&gt;
            core::ptr::write_volatile(self.base_addr, byte);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    pub fn read_byte(&amp;amp;self) -&amp;gt; u8 {&lt;br /&gt;
        unsafe { core::ptr::read_volatile(self.base_addr) }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
Now compare that with its C equivalent — 20 lines shorter but 10x riskier. One null pointer or concurrent access, and the system could crash.&lt;br /&gt;
In Rust, these risks are isolated and explicit — and can be further wrapped in safe abstractions.&lt;br /&gt;
Diagram: Rust vs. C Driver Safety Boundaries&lt;br /&gt;
C Driver:&lt;br /&gt;
+-----------------------------------+&lt;br /&gt;
| Application Logic  ⚠️ Unsafe Zone  |&lt;br /&gt;
+-----------------------------------+&lt;br /&gt;
| Hardware Access     ⚠️ Unsafe Zone |&lt;br /&gt;
+-----------------------------------+&lt;br /&gt;
Rust Driver:&lt;br /&gt;
+-----------------------------------+&lt;br /&gt;
| Application Logic  ✅ Safe         |&lt;br /&gt;
+-----------------------------------+&lt;br /&gt;
| HAL Layer          ⚠️ Explicitly Unsafe |&lt;br /&gt;
+-----------------------------------+&lt;br /&gt;
Rust confines unsafe to small, audited boundaries — rather than letting it leak everywhere.&lt;br /&gt;
How It Actually Works Under the Hood&lt;br /&gt;
When you write a Rust driver:&lt;br /&gt;
* 		No standard library: You’re compiling with #![no_std].&lt;br /&gt;
* 		Custom allocators: Drivers can’t rely on heap allocation, so you use alloc or fixed buffers.&lt;br /&gt;
* 		No OS dependencies: Drivers often compile to target_os = &amp;quot;none&amp;quot;.&lt;br /&gt;
* 		Linked with kernel symbols: For Linux, Rust’s bindgen generates bindings to existing kernel C APIs&lt;br /&gt;
The Rust compiler then produces object files just like C, which the kernel build system links as .o modules.&lt;br /&gt;
Real-World Use Cases&lt;br /&gt;
* 		Google’s Android team is already writing Rust HAL components.&lt;br /&gt;
* 		Microsoft built Rust drivers for Windows to replace C++ ones.&lt;br /&gt;
* 		Linux mainline kernel (v6.1+) now supports Rust driver modules.&lt;br /&gt;
* 		ESP32 and Raspberry Pi Pico have embedded Rust HAL crates (embedded-hal, rp2040-hal) in production.&lt;br /&gt;
These aren’t experiments — they’re production hardware drivers.&lt;br /&gt;
The Emotional Side: The First Time I Trusted a Driver&lt;br /&gt;
When I wrote my first driver in Rust for an embedded SPI peripheral, I did something I’d never done before: I slept peacefully.&lt;br /&gt;
No memory leaks. No dangling pointers. No random kernel panics at 2 a.m. after a long compile.&lt;br /&gt;
Just deterministic behavior — in a system that talks directly to metal.&lt;br /&gt;
That’s the psychological gift Rust gives to systems developers: fearless low-level programming.&lt;br /&gt;
The Tradeoffs&lt;br /&gt;
Rust isn’t a silver bullet.&lt;br /&gt;
* 		Compile times are longer.&lt;br /&gt;
* 		Tooling integration with Linux is still maturing.&lt;br /&gt;
* 		You still need unsafe for hardware I/O.&lt;br /&gt;
But the payoff — memory safety and long-term reliability — is worth every second.&lt;br /&gt;
The Architecture of the Future&lt;br /&gt;
The future of drivers looks like this:&lt;br /&gt;
┌────────────────────────────┐&lt;br /&gt;
│   Safe Rust Device Logic   │  ← ownership, lifetimes, no UB&lt;br /&gt;
├────────────────────────────┤&lt;br /&gt;
│   Unsafe HAL (audited)     │  ← tightly scoped&lt;br /&gt;
├────────────────────────────┤&lt;br /&gt;
│   OS Integration Layer     │  ← Linux, RTOS, etc.&lt;br /&gt;
└────────────────────────────┘&lt;br /&gt;
The entire ecosystem — from embedded to desktop — is slowly converging toward this model.&lt;br /&gt;
Final Thoughts&lt;br /&gt;
Rust didn’t invent safe systems programming — it just proved it’s possible.&lt;br /&gt;
Writing drivers in Rust doesn’t just reduce crashes; it redefines what “safe low-level programming” means. It shows that performance and reliability don’t have to fight each other.&lt;br /&gt;
If C was the language that built the hardware world, Rust might just be the one that saves it. Rust isn’t just another language for writing drivers — it’s a cultural shift. It makes “safe device driver” stop sounding like an oxymoron.&lt;br /&gt;
&lt;br /&gt;
Read the full article here: https://medium.com/@theopinionatedev/why-writing-device-drivers-in-rust-changes-everything-a59853dbd236&lt;/div&gt;</summary>
		<author><name>PC</name></author>
	</entry>
</feed>