This crate provides types for safe MMIO device access, especially in systems with an MMU.
This is not an officially supported Google product.
The main type provided by this crate is UniqueMmioPointer
. A UniqueMmioPointer<T>
is roughly equivalent to an &mut T
for a memory-mapped IO device. Suppose you want to construct a pointer to the data register of some UART device, and write some character to it:
use core::ptr::NonNull; use safe_mmio::UniqueMmioPointer; let mut data_register = unsafe { UniqueMmioPointer::<u8>::new(NonNull::new(0x900_0000 as _).unwrap()) }; unsafe { data_register.write_unsafe(b'x'); }
Depending on your platform this will either use write_volatile
or some platform-dependent inline assembly to perform the MMIO write.
If you know that a particular MMIO field is safe to access, you can use the appropriate wrapper type to mark that. In this case, suppose that the UART data register should only be written to:
use core::ptr::NonNull; use safe_mmio::{fields::WriteOnly, UniqueMmioPointer}; let mut data_register: UniqueMmioPointer<WriteOnly<u8>> = unsafe { UniqueMmioPointer::new(NonNull::new(0x900_0000 as _).unwrap()) }; data_register.write(b'x');
In practice, most devices have more than one register. To model this, you can create a struct, and then use the field!
macro to project from a UniqueMmioPointer
to the struct to a pointer to one of its fields:
use core::ptr::NonNull; use safe_mmio::{ field, fields::{ReadOnly, ReadPure, ReadWrite, WriteOnly}, UniqueMmioPointer, }; #[repr(C)] struct UartRegisters { data: ReadWrite<u8>, status: ReadPure<u8>, pending_interrupt: ReadOnly<u8>, } let mut uart_registers: UniqueMmioPointer<UartRegisters> = unsafe { UniqueMmioPointer::new(NonNull::new(0x900_0000 as _).unwrap()) }; field!(uart_registers, data).write(b'x');
Methods are also provided to go from a UniqueMmioPointer
to an array or slice to its elements.
We distinguish between fields which for which MMIO reads may have side effects (e.g. popping a byte from the UART's receive FIFO or clearing an interrupt status) and those for which reads are ‘pure’ with no side-effects. Reading from a ReadOnly
or ReadWrite
field is assumed to have side-effects, whereas reading from a ReadPure
or ReadPureWrite
must not. Reading from a ReadOnly
or ReadWrite
field requires an &mut UniqueMmioPointer
(the same as writing), whereas reading from a ReadPure
or ReadPureWrite
field can be done with an &UniqueMmioPointer
or &SharedMmioPointer
.
UniqueMmioPointer
(and SharedMmioPointer
) is used for a pointer to a device which is mapped into the page table and accessible, i.e. a virtual address. Sometimes you may want to deal with the physical address of a device, which may or may not be mapped in. For this you can use the PhysicalInstance
type. A PhysicalInstance
doesn‘t let you do anything other than get the physical address and size of the device’s MMIO region, but is intended to convey ownership. There should never be more than one PhysicalInstance
pointer to the same device. This way your page table management code can take a PhysicalInstance<T>
and return a UniqueMmioPointer<T>
when a device is mapped into the page table.
There are a number of things that distinguish this crate from other crates providing abstractions for MMIO in Rust.
read_volatile
and write_volatile
, but on aarch64 this may generate instructions which can't be virtualised. This is arguably a bug in rustc, but in the meantime we work around this by using inline assembly to generate the correct instructions for MMIO reads and writes on aarch64.Crate name | Last release | Version | Avoids references | Distinguishes reads with side-effects | Works around aarch64 volatile bug | Model | Field projection | Notes |
---|---|---|---|---|---|---|---|---|
safe-mmio | March 2025 | 0.2.1 | ✅ | ✅ | ✅ | struct with field wrappers | macro | |
derive-mmio | February 2025 | 0.3.0 | ✅ | ❌ | ❌ | struct with derive macro | only one level, through derive macro | |
volatile | June 2024 | 0.6.1 | ✅ | ❌ | ❌ | struct with derive macro | macro or generated methods | |
volatile-register | October 2023 | 0.2.2 | ❌ | ❌ | ❌ | struct with field wrappers | manual (references) | |
tock-registers | September 2023 | 0.9.0 | ❌ | ❌ | ❌ | macros to define fields and structs | manual (references) | Also covers CPU registers, and bitfields |
mmio | May 2021 | 2.1.0 | ✅ | ❌ | ❌ | only deals with individual fields | ❌ | |
rumio | March 2021 | 0.2.0 | ✅ | ❌ | ❌ | macros to define fields and structs | generated methods | Also covers CPU registers, and bitfields |
vcell | January 2021 | 0.1.3 | ❌ | ❌ | ❌ | plain struct | manual (references) | |
register | January 2021 | 1.0.2 | ❌ | ❌ | ❌ | macros to define fields and structs | manual (references) | Deprecated in favour of tock-registers. Also covers CPU registers, and bitfields. |
Licensed under either of
at your option.
If you want to contribute to the project, see details of how we accept contributions.