Trait bern_arch::memory_protection::IMemoryProtection
source · [−]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
type MemoryRegion
type MemoryRegion
Precalculated memory region configuration.
Required Methods
Enable memory protection hardware.
Disable memory protection hardware.
fn enable_memory_region(region: u8, config: Config)
fn enable_memory_region(region: u8, config: Config)
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
});
fn disable_memory_region(region: u8)
fn disable_memory_region(region: u8)
Disable one memory region.
fn prepare_memory_region(region: u8, config: Config) -> Self::MemoryRegion
fn prepare_memory_region(region: u8, config: Config) -> Self::MemoryRegion
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.
fn prepare_unused_region(region: u8) -> Self::MemoryRegion
fn prepare_unused_region(region: u8) -> Self::MemoryRegion
Compile register values for an unused memory region.
fn apply_regions(memory_regions: &[Self::MemoryRegion; 3])
fn apply_regions(memory_regions: &[Self::MemoryRegion; 3])
Apply 3 precompiled memory regions.
fn min_region_size() -> Byte
fn min_region_size() -> Byte
Minimal region size that can be protected.
fn n_memory_regions() -> u8
fn n_memory_regions() -> u8
Returns the number of memory regions.