pub trait IMemoryProtection {
    type MemoryRegion;

    fn enable_memory_protection();
    fn disable_memory_protection();
    fn enable_memory_region(region: u8, config: Config);
    fn disable_memory_region(region: u8);
    fn prepare_memory_region(region: u8, config: Config) -> Self::MemoryRegion;
    fn prepare_unused_region(region: u8) -> Self::MemoryRegion;
    fn apply_regions(memory_regions: &[Self::MemoryRegion; 3]);
    fn min_region_size() -> Byte;
    fn n_memory_regions() -> u8;
}
Expand description

Memory Protection.

Implementation

In addition to the trait the following features have to be implemented:

Memory Protection Exception

A violation of a memory rule will trigger an exception. The exception must call memory_protection_exception() to notify the kernel, i.e.:

extern "Rust" {
    pub fn memory_protection_exception();
}

#[allow(non_snake_case)]
#[exception]
fn MemoryManagement() -> () {
    unsafe {
        memory_protection_exception();
    }
}

Size and Alignment

Different memory protection implementation have different requirements in terms of sizes and alignment. A hardware implementation must provide

  • Alignment structs for all available alignments with naming convention A<size number><unit prefix (_,K,M,G)>, i.e.

    #[repr(align(4_096))]
    pub struct A4K;
  • A macro alignment_from_size!() that returns valid alignment for given memory size, i.e.

    #[macro_export]
    macro_rules! alignment_from_size {
        (4_096) => { $crate::arch::memory_protection::A4K };
        ($x:expr) => {
            compile_error!("Size does not meet alignment requirements from the MPU. \
            Compatible sizes are: 4KB");
         };
    }
  • A macro size_from_raw!() that returns a valid size type from raw number, i.e.

    #[macro_export]
    macro_rules! size_from_raw {
        (4_096) => { $crate::arch::memory_protection::Size::S4K };
        ($x:expr) => {
            compile_error!("Size cannot be protected by MPU. \
            Compatible sizes are: 4KB");
         };
    }

Required Associated Types

Precalculated memory region configuration.

Required Methods

Enable memory protection hardware.

Disable memory protection hardware.

Setup and enable one memory region.

Example

Protect all flash memory from write access, instruction fetch allowed.

Arch::enable_memory_region(
   0,
   Config {
        addr: 0x0800_0000 as *const _,
        memory: Type::Flash,
        size: Size::S512K,
        access: Access { user: Permission::ReadOnly, system: Permission::ReadOnly },
        executable: true
});

Disable one memory region.

Compile register values from configuration and store in MemoryRegion.

Same as Self::enable_memory_region() but return the register configuration instead of applying it to the actual registers.

Compile register values for an unused memory region.

Apply 3 precompiled memory regions.

Minimal region size that can be protected.

Returns the number of memory regions.

Implementors