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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use alloc::collections::BTreeMap;
#[allow(unused_imports)] // this is a weird false alarm
use alloc::vec::Vec;
use core::fmt;
use memory_addr::{AddrRange, MemoryAddr};
use crate::{MappingBackend, MappingError, MappingResult, MemoryArea};
/// A container that maintains memory mappings ([`MemoryArea`]).
pub struct MemorySet<B: MappingBackend> {
areas: BTreeMap<B::Addr, MemoryArea<B>>,
}
impl<B: MappingBackend> MemorySet<B> {
/// Creates a new memory set.
pub const fn new() -> Self {
Self {
areas: BTreeMap::new(),
}
}
/// Returns the number of memory areas in the memory set.
pub fn len(&self) -> usize {
self.areas.len()
}
/// Returns `true` if the memory set contains no memory areas.
pub fn is_empty(&self) -> bool {
self.areas.is_empty()
}
/// Returns the iterator over all memory areas.
pub fn iter(&self) -> impl Iterator<Item = &MemoryArea<B>> {
self.areas.values()
}
/// Returns whether the given address range overlaps with any existing area.
pub fn overlaps(&self, range: AddrRange<B::Addr>) -> bool {
if let Some((_, before)) = self.areas.range(..range.start).last() {
if before.va_range().overlaps(range) {
return true;
}
}
if let Some((_, after)) = self.areas.range(range.start..).next() {
if after.va_range().overlaps(range) {
return true;
}
}
false
}
/// Finds the memory area that contains the given address.
pub fn find(&self, addr: B::Addr) -> Option<&MemoryArea<B>> {
let candidate = self.areas.range(..=addr).last().map(|(_, a)| a);
candidate.filter(|a| a.va_range().contains(addr))
}
/// Finds a free area that can accommodate the given size.
///
/// The search starts from the given `hint` address, and the area should be
/// within the given `limit` range.
///
/// # Notes
/// The `align` parameter specifies the alignment of the start address and
/// the size of the area. The start address of the resulting area will
/// be aligned to this value. Also, the size of the area must be a multiple
/// of this value.
///
/// # Returns
/// Returns the start address of the free area. Returns `None` if no such
/// area is found.
pub fn find_free_area(
&self,
hint: B::Addr,
size: usize,
limit: AddrRange<B::Addr>,
align: usize,
) -> Option<B::Addr> {
if size % align != 0 {
// size must be a multiple of align.
return None;
}
// brute force: try each area's end address as the start.
let mut last_end: <B as MappingBackend>::Addr = hint.max(limit.start).align_up(align);
if let Some((_, area)) = self.areas.range(..last_end).last() {
last_end = last_end.max(area.end()).align_up(align);
}
for (&addr, area) in self.areas.range(last_end..) {
if last_end.checked_add(size).is_some_and(|end| end <= addr) {
return Some(last_end);
}
last_end = area.end().align_up(align);
}
if last_end
.checked_add(size)
.is_some_and(|end| end <= limit.end)
{
Some(last_end)
} else {
None
}
}
/// Add a new memory mapping.
///
/// The mapping is represented by a [`MemoryArea`].
///
/// If the new area overlaps with any existing area, the behavior is
/// determined by the `unmap_overlap` parameter. If it is `true`, the
/// overlapped regions will be unmapped first. Otherwise, it returns an
/// error.
pub fn map(
&mut self,
area: MemoryArea<B>,
page_table: &mut B::PageTable,
unmap_overlap: bool,
) -> MappingResult {
if area.va_range().is_empty() {
return Err(MappingError::InvalidParam);
}
if self.overlaps(area.va_range()) {
if unmap_overlap {
self.unmap(area.start(), area.size(), page_table)?;
} else {
return Err(MappingError::AlreadyExists);
}
}
area.map_area(page_table)?;
assert!(self.areas.insert(area.start(), area).is_none());
Ok(())
}
/// Remove memory mappings within the given address range.
///
/// All memory areas that are fully contained in the range will be removed
/// directly. If the area intersects with the boundary, it will be shrinked.
/// If the unmapped range is in the middle of an existing area, it will be
/// split into two areas.
pub fn unmap(
&mut self,
start: B::Addr,
size: usize,
page_table: &mut B::PageTable,
) -> MappingResult {
let range =
AddrRange::try_from_start_size(start, size).ok_or(MappingError::InvalidParam)?;
if range.is_empty() {
return Ok(());
}
let end = range.end;
// Unmap entire areas that are contained by the range.
self.areas.retain(|_, area| {
if area.va_range().contained_in(range) {
area.unmap_area(page_table).unwrap();
false
} else {
true
}
});
// Shrink right if the area intersects with the left boundary.
if let Some((&before_start, before)) = self.areas.range_mut(..start).last() {
let before_end = before.end();
if before_end > start {
if before_end <= end {
// the unmapped area is at the end of `before`.
before.shrink_right(start.sub_addr(before_start), page_table)?;
} else {
// the unmapped area is in the middle `before`, need to split.
let right_part = before.split(end).unwrap();
before.shrink_right(start.sub_addr(before_start), page_table)?;
assert_eq!(right_part.start().into(), Into::<usize>::into(end));
self.areas.insert(end, right_part);
}
}
}
// Shrink left if the area intersects with the right boundary.
if let Some((&after_start, after)) = self.areas.range_mut(start..).next() {
let after_end = after.end();
if after_start < end {
// the unmapped area is at the start of `after`.
let mut new_area = self.areas.remove(&after_start).unwrap();
new_area.shrink_left(after_end.sub_addr(end), page_table)?;
assert_eq!(new_area.start().into(), Into::<usize>::into(end));
self.areas.insert(end, new_area);
}
}
Ok(())
}
/// Remove all memory areas and the underlying mappings.
pub fn clear(&mut self, page_table: &mut B::PageTable) -> MappingResult {
for (_, area) in self.areas.iter() {
area.unmap_area(page_table)?;
}
self.areas.clear();
Ok(())
}
/// Change the flags of memory mappings within the given address range.
///
/// `update_flags` is a function that receives old flags and processes
/// new flags (e.g., some flags can not be changed through this interface).
/// It returns [`None`] if there is no bit to change.
///
/// Memory areas will be skipped according to `update_flags`. Memory areas
/// that are fully contained in the range or contains the range or
/// intersects with the boundary will be handled similarly to `munmap`.
pub fn protect(
&mut self,
start: B::Addr,
size: usize,
update_flags: impl Fn(B::Flags) -> Option<B::Flags>,
page_table: &mut B::PageTable,
) -> MappingResult {
let end = start.checked_add(size).ok_or(MappingError::InvalidParam)?;
let mut to_insert = Vec::new();
for (&area_start, area) in self.areas.iter_mut() {
let area_end = area.end();
if let Some(new_flags) = update_flags(area.flags()) {
if area_start >= end {
// [ prot ]
// [ area ]
break;
} else if area_end <= start {
// [ prot ]
// [ area ]
// Do nothing
} else if area_start >= start && area_end <= end {
// [ prot ]
// [ area ]
area.protect_area(new_flags, page_table)?;
area.set_flags(new_flags);
} else if area_start < start && area_end > end {
// [ prot ]
// [ left | area | right ]
let right_part = area.split(end).unwrap();
area.set_end(start);
let mut middle_part =
MemoryArea::new(start, size, area.flags(), area.backend().clone());
middle_part.protect_area(new_flags, page_table)?;
middle_part.set_flags(new_flags);
to_insert.push((right_part.start(), right_part));
to_insert.push((middle_part.start(), middle_part));
} else if area_end > end {
// [ prot ]
// [ area | right ]
let right_part = area.split(end).unwrap();
area.protect_area(new_flags, page_table)?;
area.set_flags(new_flags);
to_insert.push((right_part.start(), right_part));
} else {
// [ prot ]
// [ left | area ]
let mut right_part = area.split(start).unwrap();
right_part.protect_area(new_flags, page_table)?;
right_part.set_flags(new_flags);
to_insert.push((right_part.start(), right_part));
}
}
}
self.areas.extend(to_insert);
Ok(())
}
}
impl<B: MappingBackend> Default for MemorySet<B> {
fn default() -> Self {
Self::new()
}
}
impl<B: MappingBackend> fmt::Debug for MemorySet<B>
where
B::Addr: fmt::Debug,
B::Flags: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.areas.values()).finish()
}
}