osiris/mem/
vmm.rs

1use hal::mem::{PhysAddr, VirtAddr};
2
3use crate::error::Result;
4
5mod nommu;
6
7pub type AddressSpace = nommu::AddressSpace;
8
9bitflags::bitflags! {
10    #[derive(Clone, Copy)]
11    pub struct Perms: u8 {
12        const Read = 0b0001;
13        const Write = 0b0010;
14        const Exec = 0b0100;
15    }
16}
17
18#[derive(Clone)]
19#[allow(dead_code)]
20pub enum Backing {
21    Zeroed,
22    Uninit,
23    Anon(PhysAddr),
24}
25
26#[derive(Clone)]
27#[allow(dead_code)]
28pub struct Region {
29    start: Option<VirtAddr>,
30    len: usize,
31    backing: Backing,
32    perms: Perms,
33}
34
35impl Region {
36    /// Creates a new region.
37    ///
38    /// - `start` is the starting virtual address of the region. If `None`, the system will choose a suitable address.
39    /// - `len` is the length of the region in bytes.
40    /// - `backing` is the backing type of the region, which determines how the region is initialized and where its contents come from.
41    /// - `perms` is the permissions of the region, which determines how the region can be accessed.
42    ///
43    pub fn new(start: Option<VirtAddr>, len: usize, backing: Backing, perms: Perms) -> Self {
44        Self {
45            start,
46            len,
47            backing,
48            perms,
49        }
50    }
51
52    #[allow(dead_code)]
53    pub fn start(&self) -> VirtAddr {
54        self.start.unwrap_or_else(|| VirtAddr::new(0))
55    }
56
57    pub fn len(&self) -> usize {
58        self.len
59    }
60
61    #[allow(dead_code)]
62    pub fn contains(&self, addr: VirtAddr) -> bool {
63        self.start().saturating_add(self.len()) > addr && addr >= self.start()
64    }
65}
66
67#[allow(dead_code)]
68pub trait AddressSpacelike {
69    // Size is the amount of pages in the address space. On nommu systems this will be reserved.
70    fn new(pages: usize) -> Result<Self>
71    where
72        Self: Sized;
73    fn map(&mut self, region: Region) -> Result<PhysAddr>;
74    fn unmap(&mut self, region: &Region) -> Result<()>;
75    fn protect(&mut self, region: &Region, perms: Perms) -> Result<()>;
76    fn virt_to_phys(&self, addr: VirtAddr) -> Option<PhysAddr>;
77    fn phys_to_virt(&self, addr: PhysAddr) -> Option<VirtAddr>;
78    fn end(&self) -> VirtAddr;
79    fn activate(&self) -> Result<()>;
80}