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
use crate::mem::VirtualAddress;
use crate::paging::lands::{UserLand, KernelLand, RecursiveTablesLand, VirtualSpaceLand};
use crate::paging::mapping::MappingFrames;
use crate::paging::MappingAccessRights;
use sunrise_libkern::MemoryType;
use alloc::collections::BTreeMap;
use crate::error::KernelError;
use crate::utils::check_nonzero_length;
use failure::Backtrace;
use super::mapping::Mapping;
#[derive(Debug)]
pub struct UserspaceBookkeeping {
mappings: BTreeMap<VirtualAddress, Mapping>
}
#[derive(Debug)]
#[allow(missing_docs)]
pub enum QueryMemory<'a> {
Available(Mapping),
Used(&'a Mapping)
}
impl<'a> QueryMemory<'a> {
pub fn mapping(&self) -> &Mapping {
match self {
QueryMemory::Available(mem) => mem,
QueryMemory::Used(mem) => mem,
}
}
}
impl UserspaceBookkeeping {
pub fn new() -> Self {
let mut mappings = BTreeMap::new();
let kl = Mapping::new(KernelLand::start_addr(), MappingFrames::None, 0, KernelLand::length(), MemoryType::Reserved, MappingAccessRights::empty())
.expect("Cannot create KernelLand system_reserved mapping");
let rtl = Mapping::new(RecursiveTablesLand::start_addr(), MappingFrames::None, 0, RecursiveTablesLand::length(), MemoryType::Reserved, MappingAccessRights::empty())
.expect("Cannot create RecursiveTableLand system_reserved mapping");
mappings.insert(kl.address(), kl);
mappings.insert(rtl.address(), rtl);
UserspaceBookkeeping { mappings }
}
pub fn mapping_at_or_following(&self, address: VirtualAddress) -> Option<&Mapping> {
self.mappings.range(address..).next()
.map(|(_, mapping)| mapping)
}
pub fn mapping_at_or_preceding(&self, address: VirtualAddress) -> Option<&Mapping> {
self.mappings.range(VirtualAddress(0)..=address).rev().next()
.map(|(_, mapping)| mapping)
}
pub fn mapping_at(&self, address: VirtualAddress) -> QueryMemory<'_> {
let start_addr = match self.mapping_at_or_preceding(address) {
Some(m) if m.length() - 1 + m.address() >= address => return QueryMemory::Used(m),
Some(m) => m.address() + m.length(),
None => VirtualAddress(0x00000000),
};
let length = match self.mapping_at_or_following(address) {
Some(m) => m.address() - start_addr,
None => usize::max_value() - start_addr.addr() + 1
};
QueryMemory::Available(
Mapping::new(start_addr, MappingFrames::None, 0, length, MemoryType::Unmapped, MappingAccessRights::empty())
.expect("Failed creating an available mapping")
)
}
pub fn occupied_mapping_at(&self, address: VirtualAddress) -> Result<&Mapping, KernelError> {
match self.mapping_at_or_preceding(address) {
Some(m) if m.length() - 1 + m.address() >= address => Ok(m),
_ => Err(KernelError::InvalidAddress { address: address.addr(), backtrace: Backtrace::new() })
}
}
pub fn is_vacant(&self, address: VirtualAddress, length: usize) -> Result<bool, KernelError> {
check_nonzero_length(length)?;
let end_addr = address.checked_add(length - 1)
.ok_or_else(|| KernelError::InvalidAddress { address: address.addr(), backtrace: Backtrace::new()})?;
Ok(self.mappings.range(address..=end_addr).next().is_none())
}
pub fn check_vacant(&self, address: VirtualAddress, length: usize) -> Result<(), KernelError> {
if !self.is_vacant(address, length)? {
Err(KernelError::InvalidAddress { address: address.addr(), backtrace: Backtrace::new() })
} else {
Ok(())
}
}
pub fn add_mapping(&mut self, mapping: Mapping) -> Result<(), KernelError> {
self.check_vacant(mapping.address(), mapping.length())?;
self.mappings.insert(mapping.address(), mapping);
Ok(())
}
pub fn remove_mapping(&mut self, address: VirtualAddress, length: usize) -> Result<Mapping, KernelError> {
if self.mappings.get(&address)
.ok_or_else(|| KernelError::InvalidAddress { address: address.addr(), backtrace: Backtrace::new() })?
.length() != length {
Err(KernelError::InvalidSize { size: length, backtrace: Backtrace::new() })
} else {
Ok(self.mappings.remove(&address).unwrap())
}
}
pub fn remove_mapping_split(&mut self, _address: VirtualAddress, _length: usize) -> Result<Mapping, KernelError> {
unimplemented!()
}
pub fn find_available_space(&self, length: usize) -> Result<VirtualAddress, KernelError> {
check_nonzero_length(length)?;
let mut last_address = UserLand::START;
for m in self.mappings.values() {
if m.address() - last_address >= length {
return Ok(last_address)
}
last_address = VirtualAddress(m.address().addr().wrapping_add(m.length()));
}
Err(KernelError::VirtualMemoryExhaustion { backtrace: Backtrace::new() })
}
}