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
pub use self::page_table::*;
use addr::{VirtAddr, PhysAddr};
use core::ops::Add;
use ux::*;
mod page_table;
pub const PAGE_SIZE: u16 = 4096;
pub struct Page {
number: u64,
}
impl Page {
pub fn containing_address(address: VirtAddr) -> Page {
Page { number: address.as_u64() / u64::from(PAGE_SIZE) }
}
pub fn start_address(&self) -> VirtAddr {
VirtAddr::new(self.number * u64::from(PAGE_SIZE))
}
pub fn p4_index(&self) -> u9 {
self.start_address().p4_index()
}
pub fn p3_index(&self) -> u9 {
self.start_address().p3_index()
}
pub fn p2_index(&self) -> u9 {
self.start_address().p2_index()
}
pub fn p1_index(&self) -> u9 {
self.start_address().p1_index()
}
}
impl Add<u64> for Page {
type Output = Self;
fn add(self, rhs: u64) -> Self::Output {
Page::containing_address(self.start_address() + rhs * u64::from(PAGE_SIZE))
}
}
pub struct PhysFrame {
number: u64,
}
impl PhysFrame {
pub fn containing_address(address: PhysAddr) -> PhysFrame {
PhysFrame { number: address.as_u64() / u64::from(PAGE_SIZE) }
}
pub fn start_address(&self) -> PhysAddr {
PhysAddr::new(self.number * u64::from(PAGE_SIZE))
}
}
impl Add<u64> for PhysFrame {
type Output = Self;
fn add(self, rhs: u64) -> Self::Output {
PhysFrame::containing_address(self.start_address() + rhs * u64::from(PAGE_SIZE))
}
}