Skip to main content

Memory

Trait Memory 

Source
pub trait Memory {
    // Required methods
    fn read_u8(&self, addr: u32) -> Option<u8>;
    fn read_u16_le(&self, addr: u32) -> Option<u16>;
    fn read_u32_le(&self, addr: u32) -> Option<u32>;
    fn read_u64_le(&self, addr: u32) -> Option<u64>;
    fn write_u8(&mut self, addr: u32, val: u8) -> bool;
    fn write_u16_le(&mut self, addr: u32, val: u16) -> bool;
    fn write_u32_le(&mut self, addr: u32, val: u32) -> bool;
    fn write_u64_le(&mut self, addr: u32, val: u64) -> bool;
    fn map_region(
        &mut self,
        start: u64,
        size: u64,
        access: Access,
        init: Option<&[u8]>,
    ) -> Result<(), MapError>;
    fn perm_of(&self, addr: u32) -> u8;
    fn read(&self, addr: u32, len: usize) -> Result<Vec<u8>, MemAccess>;
    fn write(&mut self, addr: u32, data: &[u8]) -> Result<(), MemAccess>;
}
Expand description

Memory backend abstraction.

The interpreter is generic over M: Memory so the same source compiles for two substrates:

  • Software-copy: CopyingMemory (this module) — an owning Vec<u8> with per-page permissions. Runs in-process.
  • Hardware-paged: a future bare-metal impl in nub-arch-x86 that maps PVM pages onto real CPU pages via the in-guest IDT + page tables.

Hot-path methods (read_u*/write_u*) return Option<T> or bool to keep the interpreter loop branch-free. Implementations should mark these #[inline] (or #[inline(always)]) — the interpreter calls them through trait dispatch, and we want monomorphisation to collapse to direct function calls.

Required Methods§

Source

fn read_u8(&self, addr: u32) -> Option<u8>

Source

fn read_u16_le(&self, addr: u32) -> Option<u16>

Source

fn read_u32_le(&self, addr: u32) -> Option<u32>

Source

fn read_u64_le(&self, addr: u32) -> Option<u64>

Source

fn write_u8(&mut self, addr: u32, val: u8) -> bool

Source

fn write_u16_le(&mut self, addr: u32, val: u16) -> bool

Source

fn write_u32_le(&mut self, addr: u32, val: u32) -> bool

Source

fn write_u64_le(&mut self, addr: u32, val: u64) -> bool

Source

fn map_region( &mut self, start: u64, size: u64, access: Access, init: Option<&[u8]>, ) -> Result<(), MapError>

Declare a mapped region. See CopyingMemory::map_region for the canonical semantics.

Source

fn perm_of(&self, addr: u32) -> u8

Per-page permission byte for the page containing addr. Returns perm::NONE if addr is out of range.

Source

fn read(&self, addr: u32, len: usize) -> Result<Vec<u8>, MemAccess>

Read dst.len() bytes starting at addr into dst.

Source

fn write(&mut self, addr: u32, data: &[u8]) -> Result<(), MemAccess>

Write data.len() bytes starting at addr. Per-page perm checks apply; out-of-range or RO-page writes return Err.

Implementors§