Skip to content

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().

Physical Address
ioremap()
Kernel Virtual Address
readl()/writel()

In the simulator, page-table translation is replaced by an address translation layer.

Virtual Address
io_mapping_translate()
Backing Storage

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

Region A
Register
PASS

Duplicate
FAIL

Overlap
FAIL

Adjacent
PASS

Learning Points

  • MMIO regions use half-open intervals:
[start, end)
  • 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

Physical Address          Virtual Address

0x10000000      --->      0x20000000
0x10000800      --->      0x20001000

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

Virtual Address
Find Mapping
Calculate Offset
Backing Storage

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

io_region_register()
ioremap()
MMIO Access
iounmap()
io_region_unregister()

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:

Physical Region
Virtual Mapping
Address Translation
Backing Storage

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.