-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaddress.rs
More file actions
179 lines (149 loc) · 4.34 KB
/
address.rs
File metadata and controls
179 lines (149 loc) · 4.34 KB
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
use super::page_table::PageTableEntry;
use crate::addr::{phy_addr::PhyAddr, virt_addr::VirtAddr, Addr};
use core::{
self,
fmt::{self, Debug, Formatter},
};
pub const PAGE_SIZE: usize = 0x1000; // 4 KiB
pub const PAGE_OFFSET: usize = 0xc; // 12 bits
// Memory end for QEMU virt with 1GB RAM (-m 1G)
// QEMU provides 1GB at 0x80000000 - 0xC0000000
// Using 0xC0000000 directly may be too aggressive,
// so we use 0xB0000000 (768MB from start) for safety
pub const MEMORY_END: u64 = 0xB0000000;
/// Design abstraction struct:
/// PhysAddr, VirtAddr, PhysPageNum, VirtPageNum
/// to guarantee memory safety by borrow check in compilation
// SV39 Physical Address Length
pub const PA_WIDTH: usize = 56;
// SV32 Virtual Address Length
pub const VA_WIDTH: usize = 39;
// SV39 Physical Page Number Range
pub const PPN_WIDTH: usize = PA_WIDTH - PAGE_OFFSET;
// SV39 Virtual Page Number Range
pub const VPN_WIDTH: usize = VA_WIDTH - PAGE_OFFSET;
#[repr(C)]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct PhysPageNum(pub usize);
#[repr(C)]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct VirtPageNum(pub usize);
impl Debug for PhysPageNum {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("PPN: {:#x}", self.0))
}
}
impl Debug for VirtPageNum {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("VPN: {:#x}", self.0))
}
}
impl From<usize> for PhysPageNum {
fn from(v: usize) -> Self {
Self(v & ((1 << PPN_WIDTH) - 1))
}
}
impl From<usize> for VirtPageNum {
fn from(v: usize) -> Self {
Self(v & ((1 << VPN_WIDTH) - 1))
}
}
impl From<PhysPageNum> for usize {
fn from(v: PhysPageNum) -> Self {
v.0
}
}
impl From<VirtPageNum> for usize {
fn from(v: VirtPageNum) -> Self {
v.0
}
}
impl From<VirtAddr> for VirtPageNum {
fn from(v: VirtAddr) -> Self {
assert_eq!(v.page_offset(), 0);
v.floor()
}
}
impl From<VirtPageNum> for VirtAddr {
fn from(v: VirtPageNum) -> Self {
VirtAddr::from_usize(v.0 << PAGE_OFFSET)
}
}
impl From<PhyAddr> for PhysPageNum {
fn from(v: PhyAddr) -> Self {
assert_eq!(v.page_offset(), 0);
v.floor()
}
}
impl From<PhysPageNum> for PhyAddr {
fn from(v: PhysPageNum) -> Self {
PhyAddr::from_usize(v.0 << PAGE_OFFSET)
}
}
impl VirtAddr {
pub fn floor(&self) -> VirtPageNum {
VirtPageNum(self.as_usize() / PAGE_SIZE)
}
pub fn ceil(&self) -> VirtPageNum {
if self.as_usize() == 0 {
VirtPageNum(0)
} else {
VirtPageNum((self.as_usize() - 1 + PAGE_SIZE) / PAGE_SIZE)
}
}
pub fn page_offset(&self) -> usize {
self.as_usize() & (PAGE_SIZE - 1)
}
pub fn aligned(&self) -> bool {
self.page_offset() == 0
}
}
impl PhysPageNum {
pub fn get_pte_array(&self) -> &'static mut [PageTableEntry] {
let pa: PhyAddr = (*self).into();
unsafe { core::slice::from_raw_parts_mut(pa.as_usize() as *mut PageTableEntry, 512) }
}
pub fn get_bytes_array(&self) -> &'static mut [u8] {
let pa: PhyAddr = (*self).into();
unsafe { core::slice::from_raw_parts_mut(pa.as_usize() as *mut u8, 4096) }
}
pub fn get_mut<T>(&self) -> &'static mut T {
let pa: PhyAddr = (*self).into();
pa.get_mut()
}
}
impl VirtPageNum {
pub fn indexes(&self) -> [usize; 3] {
let mut vpn = self.0;
let mut idx = [0usize; 3];
for i in (0..3).rev() {
idx[i] = vpn & 511; // 9 bits per level for Sv39
vpn >>= 9;
}
idx
}
}
impl PhyAddr {
pub fn floor(&self) -> PhysPageNum {
PhysPageNum(self.as_usize() / PAGE_SIZE)
}
pub fn ceil(&self) -> PhysPageNum {
if self.as_usize() == 0 {
PhysPageNum(0)
} else {
PhysPageNum((self.as_usize() - 1 + PAGE_SIZE) / PAGE_SIZE)
}
}
pub fn page_offset(&self) -> usize {
self.as_usize() & (PAGE_SIZE - 1)
}
pub fn aligned(&self) -> bool {
self.page_offset() == 0
}
pub fn get_ref<T>(&self) -> &'static T {
unsafe { (self.as_usize() as *const T).as_ref().unwrap() }
}
pub fn get_mut<T>(&self) -> &'static mut T {
unsafe { (self.as_usize() as *mut T).as_mut().unwrap() }
}
}