perf(mmu): reuse registered RAM extents within descriptor walks

This commit is contained in:
Tyler Smith committed 2026-11-05 05:16:31 +00:00
1 parent 9a41e531fa
commit fc8b53b932
6 files changed
+104 -27

No files matched your search

+22 -7
View File
@@ -1140,22 +1140,37 @@ impl State {
}
/// Guest page tables read through the RAM map.
struct GuestTables<'a>(&'a State);
struct GuestTables<'a> {
state: &'a State,
descriptors: Option<stela_engine::memory::system_ram::DescriptorReader<'a>>,
}
impl<'a> GuestTables<'a> {
fn new(state: &'a State) -> Self {
Self {
state,
descriptors: state
.registered_ram
.as_ref()
.map(|ram| ram.descriptor_reader()),
}
}
}
impl mmu::TableMemory for GuestTables<'_> {
fn read_descriptor(&mut self, physical: u64) -> Option<u64> {
if let Some(ram) = &self.0.registered_ram {
return ram.read_descriptor(physical).ok();
if let Some(descriptors) = &mut self.descriptors {
return descriptors.read_descriptor(physical).ok();
}
let host = self.0.host_physical(physical)?;
let host = self.state.host_physical(physical)?;
// SAFETY: guest RAM through the physmap.
Some(unsafe { (layout::physical(host) as *const u64).read_volatile() })
}
fn update_descriptor(&mut self, physical: u64, old: u64, new: u64) -> Option<bool> {
if let Some(ram) = &self.0.registered_ram {
return ram.update_descriptor(physical, old, new).ok();
if let Some(descriptors) = &self.descriptors {
return descriptors.update_descriptor(physical, old, new).ok();
}
let host = self.0.host_physical(physical)?;
let host = self.state.host_physical(physical)?;
let slot = layout::physical(host) as *mut u64;
// SAFETY: guest RAM through the physmap; one PE, no concurrent walker.
unsafe {
+1 -1
View File
@@ -175,7 +175,7 @@ pub(super) fn resolve(
address,
access,
requester,
&mut GuestTables(state),
&mut GuestTables::new(state),
) {
Outcome::Mapped(translation) => translation,
Outcome::Fault(fault) => return Err(FaultCause::Guest(fault)),
@@ -3,6 +3,7 @@ use super::*;
use crate::memory::{
backing::AccessKind,
domain::{DataAccess, DataOperation, DataPart, DataPolicy, DataRequest},
system_ram::DescriptorReader,
};
use stela_compiler::context::memory::{
InstructionRequirements, MemoryOperationClass, SpAlignmentRequirement, flags,
@@ -407,7 +408,7 @@ impl DataPolicy for Policy {
}
}
pub(super) struct Tables<'a> {
ram: &'a RegisteredSystemRam,
descriptors: DescriptorReader<'a>,
pub(super) failure: Option<MemoryStatus>,
#[cfg(feature = "native-profile")]
pub(super) reads: u64,
@@ -415,7 +416,7 @@ pub(super) struct Tables<'a> {
impl<'a> Tables<'a> {
pub(super) fn new(ram: &'a RegisteredSystemRam) -> Self {
Self {
ram,
descriptors: ram.descriptor_reader(),
failure: None,
#[cfg(feature = "native-profile")]
reads: 0,
@@ -428,7 +429,7 @@ impl mmu::TableMemory for Tables<'_> {
{
self.reads = self.reads.saturating_add(1);
}
match self.ram.read_descriptor(physical) {
match self.descriptors.read_descriptor(physical) {
Ok(value) => Some(value),
Err(MemoryStatus::Translation) => None,
Err(status) => {
@@ -438,7 +439,7 @@ impl mmu::TableMemory for Tables<'_> {
}
}
fn update_descriptor(&mut self, physical: u64, old: u64, new: u64) -> Option<bool> {
match self.ram.update_descriptor(physical, old, new) {
match self.descriptors.update_descriptor(physical, old, new) {
Ok(changed) => Some(changed),
Err(MemoryStatus::Translation) => None,
Err(status) => {
@@ -16,6 +16,7 @@ use stela_compiler::{TargetCpu, context::memory::MemoryStatus};
mod completion;
mod transfer;
pub use completion::CanonicalRamService;
pub use transfer::DescriptorReader;
const GRANULE: usize = 16;
const MAX_RANGES: usize = 4096;
@@ -486,6 +486,19 @@ fn physical_host_copy_preflights_holes_and_keeps_descriptor_prefix_precise() {
registered.read_descriptor(0x4038),
Ok(0x5353_5353_5353_5353)
);
let mut descriptors = registered.descriptor_reader();
assert_eq!(
descriptors.read_descriptor(0x4000),
Ok(0x5353_5353_5353_5353)
);
assert_eq!(
descriptors.read_descriptor(0x4001),
Err(MemoryStatus::InvalidRequest)
);
assert_eq!(
descriptors.read_descriptor(0x4040),
Err(MemoryStatus::Translation)
);
assert_eq!(
registered.update_descriptor(0x4001, 0, 1),
Err(MemoryStatus::InvalidRequest)
@@ -503,6 +516,7 @@ fn physical_host_copy_preflights_holes_and_keeps_descriptor_prefix_precise() {
Ok(false)
);
assert_eq!(registered.read_descriptor(0x4000), Ok(0x1234));
assert_eq!(descriptors.read_descriptor(0x4000), Ok(0x1234));
assert_eq!(
registered.read_descriptor(0x4008),
Ok(0x5353_5353_5353_5353)
@@ -511,6 +525,8 @@ fn physical_host_copy_preflights_holes_and_keeps_descriptor_prefix_precise() {
// registered.
unsafe { registered.publish_physical(0x4080, &42u64.to_le_bytes()) }.unwrap();
assert_eq!(registered.read_descriptor(0x4080), Ok(42));
assert_eq!(descriptors.read_descriptor(0x4080), Ok(42));
assert_eq!(descriptors.read_descriptor(0x4000), Ok(0x1234));
}
#[test]
@@ -1,8 +1,15 @@
//! Native coherent host/table-walker transfers through the canonical RAM owner.
use super::RegisteredSystemRam;
use super::{Region, RegisteredSystemRam};
use crate::memory::{access, backing::AccessKind, domain::PublicationFault};
use stela_compiler::{TargetCpu, context::memory::MemoryStatus};
/// A page-table walk's view of one registered RAM owner.
/// Keeps only the last immutable extent, never descriptor values.
pub struct DescriptorReader<'a> {
ram: &'a RegisteredSystemRam,
last: Option<&'a Region>,
}
impl RegisteredSystemRam {
/// Publish private host bytes through every reached backing's same writer
/// owner. Whole physical geometry is checked before the first byte effect;
@@ -48,21 +55,15 @@ impl RegisteredSystemRam {
/// Read a single aligned descriptor through the canonical RAM owner.
pub fn read_descriptor(&self, physical: u64) -> Result<u64, MemoryStatus> {
if !physical.is_multiple_of(8) {
return Err(MemoryStatus::InvalidRequest);
self.descriptor_reader().read_descriptor(physical)
}
/// Read a succession of descriptors through this registered owner.
pub fn descriptor_reader(&self) -> DescriptorReader<'_> {
DescriptorReader {
ram: self,
last: None,
}
self.domain.check_execution()?;
let (region, offset) = self.region(physical).ok_or(MemoryStatus::Translation)?;
let pointer = region
.backing
.host_pointer(offset, 8, AccessKind::Read)
.map_err(|_| MemoryStatus::Domain)?;
// SAFETY: registration aligns both ends of each extent to at least
// eight bytes. The pointer covers one aligned doubleword in its
// pinned, coherent backing, with no Rust reference into guest RAM.
unsafe { access::read_part(pointer.as_ptr(), 8, TargetCpu::Baseline) }
.map(|value| value as u64)
.map_err(|_| MemoryStatus::Domain)
}
pub fn update_descriptor(
@@ -88,3 +89,46 @@ impl RegisteredSystemRam {
}
}
}
impl DescriptorReader<'_> {
/// Read one aligned descriptor through its live canonical backing.
pub fn read_descriptor(&mut self, physical: u64) -> Result<u64, MemoryStatus> {
if !physical.is_multiple_of(8) {
return Err(MemoryStatus::InvalidRequest);
}
self.ram.domain.check_execution()?;
let last = self.last.and_then(|region| {
// Registration rejects extents that wrap, so an address below
// the base always gives an offset beyond the extent's end.
let offset = usize::try_from(physical.wrapping_sub(region.source_pa)).ok()?;
(offset < region.bytes).then_some((region, offset))
});
let (region, offset) = last
.or_else(|| {
let (region, offset) = self.ram.region(physical)?;
self.last = Some(region);
Some((region, offset))
})
.ok_or(MemoryStatus::Translation)?;
let pointer = region
.backing
.host_pointer(offset, 8, AccessKind::Read)
.map_err(|_| MemoryStatus::Domain)?;
// SAFETY: registration aligns both ends of each extent to at least
// eight bytes. The pointer covers one aligned doubleword in its
// pinned, coherent backing, with no Rust reference into guest RAM.
unsafe { access::read_part(pointer.as_ptr(), 8, TargetCpu::Baseline) }
.map(|value| value as u64)
.map_err(|_| MemoryStatus::Domain)
}
/// Publish through the same canonical owner as a standalone update.
pub fn update_descriptor(
&self,
physical: u64,
old: u64,
new: u64,
) -> Result<bool, MemoryStatus> {
self.ram.update_descriptor(physical, old, new)
}
}