Hardware Abstraction Layer
Relevant source files
Purpose and Scope
The Hardware Abstraction Layer (HAL) provides a platform-independent interface for memory management operations within the axaddrspace crate. It abstracts low-level memory allocation, deallocation, and address translation functions that are specific to the underlying hypervisor or operating system implementation.
The HAL consists of two primary components: the AxMmHal trait that defines the interface for hardware-specific operations, and the PhysFrame struct that provides RAII-based physical memory frame management. For information about how address types are defined and used throughout the system, see Address Types and Spaces. For details on how the HAL integrates with memory mapping strategies, see Linear Backend and Allocation Backend.
AxMmHal Trait Interface
The AxMmHal trait defines the core hardware abstraction interface for memory management operations. This trait must be implemented by the host system to provide concrete implementations of memory allocation and address translation.
AxMmHal Trait Methods
flowchart TD
subgraph subGraph1["Input/Output Types"]
hpa["HostPhysAddr"]
hva["HostVirtAddr"]
opt["Option<HostPhysAddr>"]
end
subgraph subGraph0["AxMmHal Trait Interface"]
trait["AxMmHal"]
alloc["alloc_frame()"]
dealloc["dealloc_frame(paddr)"]
p2v["phys_to_virt(paddr)"]
v2p["virt_to_phys(vaddr)"]
end
alloc --> opt
dealloc --> hpa
p2v --> hpa
p2v --> hva
trait --> alloc
trait --> dealloc
trait --> p2v
trait --> v2p
v2p --> hpa
v2p --> hva
The trait provides four essential operations:
| Method | Purpose | Parameters | Return Type |
|---|---|---|---|
| alloc_frame() | Allocates a physical memory frame | None | Option |
| dealloc_frame() | Deallocates a physical memory frame | paddr: HostPhysAddr | () |
| phys_to_virt() | Converts physical to virtual address | paddr: HostPhysAddr | HostVirtAddr |
| virt_to_phys() | Converts virtual to physical address | vaddr: HostVirtAddr | HostPhysAddr |
Sources: src/hal.rs(L4 - L40)
PhysFrame RAII Management
The PhysFrame<H: AxMmHal> struct provides automatic memory management for physical memory frames using the RAII (Resource Acquisition Is Initialization) pattern. It ensures that allocated frames are automatically deallocated when the frame goes out of scope.
PhysFrame Lifecycle
flowchart TD
subgraph Cleanup["Cleanup"]
drop["Drop::drop()"]
hal_dealloc["H::dealloc_frame()"]
end
subgraph Operations["Operations"]
start["start_paddr()"]
ptr["as_mut_ptr()"]
fill["fill(byte)"]
end
subgraph subGraph1["PhysFrame State"]
frame["PhysFrame<H>"]
paddr["start_paddr: Option<HostPhysAddr>"]
marker["_marker: PhantomData<H>"]
end
subgraph subGraph0["Allocation Methods"]
alloc["PhysFrame::alloc()"]
alloc_zero["PhysFrame::alloc_zero()"]
uninit["PhysFrame::uninit()"]
end
alloc --> frame
alloc_zero --> frame
drop --> hal_dealloc
frame --> drop
frame --> fill
frame --> marker
frame --> paddr
frame --> ptr
frame --> start
uninit --> frame
Frame Allocation and Management
The PhysFrame struct provides several allocation methods:
alloc(): Allocates a new frame usingH::alloc_frame()and validates the returned address is non-zeroalloc_zero(): Allocates a frame and fills it with zeros using thefill()methoduninit(): Creates an uninitialized frame for placeholder use (unsafe)
Frame operations include:
start_paddr(): Returns the starting physical address of the frameas_mut_ptr(): Provides a mutable pointer to the frame content viaH::phys_to_virt()fill(byte): Fills the entire frame with a specified byte value (assumes 4KiB frame size)
The automatic cleanup is handled by the Drop implementation, which calls H::dealloc_frame() when the frame is dropped.
Sources: src/frame.rs(L14 - L74)
Integration with System Components
The HAL serves as the foundation for memory management throughout the axaddrspace system, providing the interface between high-level address space management and low-level hardware operations.
HAL Integration Architecture
flowchart TD
subgraph subGraph3["Address Types"]
host_phys["HostPhysAddr"]
host_virt["HostVirtAddr"]
end
subgraph subGraph2["Host Implementation"]
host_impl["Host-specific Implementation"]
frame_alloc["Frame Allocator"]
addr_trans["Address Translation"]
end
subgraph subGraph1["Hardware Abstraction Layer"]
hal_trait["AxMmHal Trait"]
physframe["PhysFrame<H>"]
end
subgraph subGraph0["Memory Backends"]
alloc_backend["Alloc Backend"]
linear_backend["Linear Backend"]
end
alloc_backend --> physframe
hal_trait --> host_impl
hal_trait --> host_phys
hal_trait --> host_virt
host_impl --> addr_trans
host_impl --> frame_alloc
linear_backend --> host_phys
physframe --> hal_trait
Key Integration Points
- Allocation Backend: The allocation backend (see Allocation Backend) uses
PhysFramefor dynamic memory allocation with lazy population strategies. - Address Translation: Both physical-to-virtual and virtual-to-physical address translation operations are abstracted through the HAL, enabling consistent address handling across different host environments.
- Frame Size Abstraction: The HAL abstracts frame size details, though the current implementation assumes 4KiB frames as defined by
PAGE_SIZE_4Kfrom thememory_addrcrate. - Error Handling: Frame allocation failures are handled gracefully through the
Option<HostPhysAddr>return type, with higher-level components responsible for error propagation.
Sources: src/frame.rs(L1 - L7) src/hal.rs(L1 - L2)
The Hardware Abstraction Layer ensures that the axaddrspace crate can operate independently of specific hypervisor implementations while maintaining efficient memory management through RAII patterns and clear separation of concerns between platform-specific and platform-independent code.