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
pub use self::page_table::*;
pub use self::recursive::*;
use addr::{VirtAddr, PhysAddr};
use core::ops::{Add, AddAssign, Sub, SubAssign};
use ux::*;
mod page_table;
mod recursive;
pub const PAGE_SIZE: u16 = 4096;
#[derive(Debug, Clone, PartialEq, PartialOrd)]
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 from_page_table_indices(p4_index: u9, p3_index: u9, p2_index: u9, p1_index: u9)
-> Page
{
use bit_field::BitField;
let mut addr = 0;
addr.set_bits(39..48, u64::from(p4_index));
addr.set_bits(30..39, u64::from(p3_index));
addr.set_bits(21..30, u64::from(p2_index));
addr.set_bits(12..21, u64::from(p1_index));
Page::containing_address(VirtAddr::new(addr))
}
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))
}
}
impl AddAssign<u64> for Page {
fn add_assign(&mut self, rhs: u64) {
*self = self.clone() + rhs;
}
}
impl Sub<u64> for Page {
type Output = Self;
fn sub(self, rhs: u64) -> Self::Output {
Page::containing_address(self.start_address() - rhs * u64::from(PAGE_SIZE))
}
}
impl SubAssign<u64> for Page {
fn sub_assign(&mut self, rhs: u64) {
*self = self.clone() - rhs;
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
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))
}
}
impl AddAssign<u64> for PhysFrame {
fn add_assign(&mut self, rhs: u64) {
*self = self.clone() + rhs;
}
}
impl Sub<u64> for PhysFrame {
type Output = Self;
fn sub(self, rhs: u64) -> Self::Output {
PhysFrame::containing_address(self.start_address() - rhs * u64::from(PAGE_SIZE))
}
}
impl SubAssign<u64> for PhysFrame {
fn sub_assign(&mut self, rhs: u64) {
*self = self.clone() - rhs;
}
}