1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::Address;
use core::cmp;
pub mod page;
use mycelium_util::fmt;

/// A cross-platform representation of a memory region.
#[derive(Clone, Eq, PartialEq)]
pub struct Region<A = crate::PAddr> {
    base: A,
    // TODO(eliza): should regions be stored as (start -> end) or as
    // (base + offset)?
    size: usize,
    kind: RegionKind,
}

#[derive(Copy, Clone, Eq, PartialEq)]
#[repr(transparent)]
pub struct RegionKind(KindInner);

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u8)]
enum KindInner {
    /// For whatever reason, this memory region's kind is undetermined.
    // TODO(eliza): do we need this?
    Unknown = 0,
    /// Free memory
    Free,
    /// Memory used by the kernel
    Used,
    /// Memory containing bootloader info that may be reclaimed by the kernel.
    ///
    /// The kernel is responsible for ensuring that any required information
    /// from the bootloader is consumed before reclaiming this memory.
    ///
    /// This may include, for example, ACPI tables.
    BootReclaimable,
    /// Memory containing bootloader info that may **not** be reclaimed by the
    /// kernel.
    Boot,
    /// Bad memory.
    Bad,
    /// Kernel memory
    Kernel,
    /// Kernel stack
    KernelStack,
    /// Memory used for a page table.
    PageTable,
}

impl<A: Address> Region<A> {
    pub fn new(base: A, size: usize, kind: RegionKind) -> Self {
        Self { base, size, kind }
    }

    /// Returns the base address of the memory region
    pub fn base_addr(&self) -> A {
        self.base
    }

    /// Returns the size (in bytes) of the memory region.
    pub fn size(&self) -> usize {
        self.size
    }

    pub fn is_aligned(&self, align: impl Into<usize>) -> bool {
        self.base.is_aligned(align)
    }

    /// Returns `true` if `self` contains the specified address.
    pub fn contains(&self, addr: impl Into<A>) -> bool {
        let addr = addr.into();
        addr >= self.base && addr < self.end_addr()
    }

    /// Returns the end address (exclusive) of the memory region.
    ///
    /// This is the start address of the next memory region.
    pub fn end_addr(&self) -> A {
        self.base + self.size
    }

    pub fn kind(&self) -> RegionKind {
        self.kind
    }

    pub fn split_front(&mut self, size: usize) -> Option<Self> {
        assert!(size <= core::i32::MAX as usize);
        if size > self.size {
            return None;
        }
        let base = self.base;
        tracing::trace!(size, self.size, "splitting down by");
        self.base = self.base.offset(size as i32);
        self.size -= size;
        Some(Self {
            base,
            size,
            kind: self.kind,
        })
    }

    pub fn split_back(&mut self, size: usize) -> Option<Self> {
        assert!(size <= core::i32::MAX as usize);
        if size >= self.size {
            return None;
        }
        let rem_size = self.size - size;
        let base = self.base.offset(size as i32);
        tracing::trace!(
            size,
            self.size,
            ?self.base,
            self.addr = fmt::ptr(&self),
            rem_size,
            ?base,
            "split_back",
        );
        self.size = size;
        tracing::trace!(?self);
        Some(Self {
            base,
            size: rem_size,
            kind: self.kind,
        })
    }

    pub fn page_range<S: page::Size>(
        &self,
        size: S,
    ) -> Result<page::PageRange<A, S>, page::NotAligned<S>> {
        tracing::trace!(?self.base, self.size, self.end = ?self.end_addr(), "Region -> PageRange");
        let start = page::Page::starting_at(self.base, size)?;
        let end = page::Page::starting_at(self.end_addr(), size)?;
        Ok(start.range_to(end))
    }

    pub fn from_page_range<S: page::Size>(range: page::PageRange<A, S>, kind: RegionKind) -> Self {
        let base = range.start().base_addr();
        Self {
            base,
            size: range.size(),
            kind,
        }
    }

    pub fn merge(&mut self, other: &mut Self) {
        debug_assert_eq!(self.kind, other.kind);
        // TOOD(eliza): assert that there's no in-between space.
        self.base = cmp::min(self.base, other.base);
        self.size += other.size;
    }
}

impl<A> fmt::Debug for Region<A>
where
    A: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self { base, size, kind } = self;
        f.debug_struct("Region")
            .field("base", base)
            .field("size", size)
            .field("kind", kind)
            .finish()
    }
}

impl RegionKind {
    pub const UNKNOWN: Self = Self(KindInner::Unknown);

    /// Free memory
    pub const FREE: Self = Self(KindInner::Free);

    /// Memory used by the kernel
    pub const USED: Self = Self(KindInner::Used);

    /// Memory containing bootloader info that may be reclaimed by the kernel.
    ///
    /// The kernel is responsible for ensuring that any required information
    /// from the bootloader is consumed before reclaiming this memory.
    ///
    /// This may include, for example, ACPI tables.
    pub const BOOT_RECLAIMABLE: Self = Self(KindInner::BootReclaimable);

    /// Memory containing bootloader info that may **not** be reclaimed by the
    /// kernel.
    pub const BOOT: Self = Self(KindInner::Boot);

    /// Bad memory
    pub const BAD: Self = Self(KindInner::Bad);

    /// Kernel memory
    pub const KERNEL: Self = Self(KindInner::Kernel);

    /// Kernel stack
    pub const KERNEL_STACK: Self = Self(KindInner::KernelStack);

    /// Memory used for a page table.
    pub const PAGE_TABLE: Self = Self(KindInner::PageTable);
}

impl fmt::Debug for RegionKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            RegionKind::FREE => f.pad("FREE"),
            RegionKind::USED => f.pad("USED"),
            RegionKind::BOOT => f.pad("BOOT"),
            RegionKind::BOOT_RECLAIMABLE => f.pad("BOOT_RECLAIMABLE"),
            RegionKind::BAD => f.pad("BAD T_T"),
            RegionKind::KERNEL => f.pad("KERNEL"),
            RegionKind::KERNEL_STACK => f.pad("KERNEL_STACK"),
            RegionKind::PAGE_TABLE => f.pad("PAGE_TABLE"),
            _ => f.pad("UNKNOWN ?_?"),
        }
    }
}