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
pub use super::arch::{KernelLand, UserLand, RecursiveTablesLand};
use crate::mem::VirtualAddress;
use crate::error::KernelError;
use failure::Backtrace;
pub trait VirtualSpaceLand {
const START: VirtualAddress;
const END: VirtualAddress;
fn start_addr() -> VirtualAddress { Self::START }
fn end_addr() -> VirtualAddress { Self::END }
fn length() -> usize { Self::end_addr().addr() - Self::start_addr().addr() + 1 }
fn contains_address(address: VirtualAddress) -> bool {
Self::start_addr() <= address && address <= Self::end_addr()
}
fn contains_region(start_address: VirtualAddress, size: usize) -> bool {
assert!(size != 0, "contains_region : size == 0");
let sum = start_address.addr().checked_add(size - 1);
if let Some(end_address) = sum {
Self::contains_address(start_address) && Self::contains_address(VirtualAddress(end_address))
} else {
false
}
}
fn check_contains_address(address: VirtualAddress) -> Result<(), KernelError> {
Self::check_contains_region(address, 1)
}
fn check_contains_region(address: VirtualAddress, length: usize) -> Result<(), KernelError> {
if Self::contains_region(address, length) {
Ok(())
} else {
Err(KernelError::InvalidAddress { address: address.addr(), backtrace: Backtrace::new() })
}
}
}