Day 95 - ioremap() Internals¶
Objective¶
Implement a simplified MMIO mapping manager to understand how Linux maps physical MMIO regions into the kernel virtual address space.
Unlike the real Linux kernel, this lab does not modify page tables. Instead, it simulates virtual mappings and address translation entirely in userspace.
Background¶
Linux drivers cannot directly access hardware using physical addresses.
Instead, they create a virtual mapping through ioremap().
In the simulator, page-table translation is replaced by an address translation layer.
Lab 1 - MMIO Region Registration¶
Goal¶
Understand how MMIO regions are registered and managed.
Verification¶
- Register a valid region
- Reject duplicate regions
- Reject overlapping regions
- Allow adjacent regions
- Reject zero-sized regions
- Reject address overflow
- Unregister and register again
Expected Result¶
Learning Points¶
- MMIO regions use half-open intervals:
- Adjacent regions are not considered overlapping.
- A region must uniquely represent one hardware address range.
Lab 2 - ioremap()¶
Goal¶
Understand virtual address allocation.
Verification¶
- Map registered regions
- Reject unregistered regions
- Reject mappings outside the region
- Support multiple mappings
- Allocate page-aligned virtual addresses
Expected Result¶
Learning Points¶
Linux returns a kernel virtual address.
The simulator allocates a fake kernel virtual address while maintaining an internal mapping table.
Lab 3 - Address Translation¶
Goal¶
Understand how virtual addresses reach simulated hardware registers.
Verification¶
- Translate virtual addresses
- Verify mapping offsets
- Verify physical region offsets
- Reject out-of-range accesses
- Verify shared backing storage
Translation Flow¶
Learning Points¶
Unlike Linux, the simulator cannot rely on the MMU.
Instead, io_mapping_translate() performs software address translation.
Lab 4 - Mapping Lifecycle¶
Goal¶
Validate mapping ownership and cleanup.
Verification¶
- Prevent unregister while mappings exist
- Remove mappings one by one
- Allow unregister after the final mapping
- Reject invalid
iounmap() - Verify manager cleanup
- Reinitialize the manager
Lifecycle¶
Learning Points¶
A registered MMIO region cannot be removed while active mappings still exist.
This ownership model is similar to Linux resource management.
Summary¶
This lab builds a simplified model of Linux MMIO mapping.
The simulator separates:
Although Linux uses the MMU to perform virtual-to-physical translation, the simulator replaces the MMU with io_mapping_translate(), allowing the complete mapping workflow to be observed in userspace.