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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::mem::{self, ALLOC};
use crate::spinlock::Mutex;
use crate::{print, println, panic};
use crate::symbols::*;
use alloc::boxed::Box;
use crate::process::my_cpu;
const TABLE_ENTRY_CNT: usize = 512;
#[repr(C)]
#[repr(align(4096))]
pub struct Table {
pub entries: [Entry; TABLE_ENTRY_CNT],
}
#[repr(C)]
#[repr(align(4096))]
pub struct Page {
pub data: [u8; PAGE_SIZE]
}
impl Page {
pub fn new() -> Box<Self> {
Box::new(Self {
data: [0; PAGE_SIZE]
})
}
}
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Entry(usize);
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct VPN(usize);
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct PPN(usize);
pub enum EntryAttributes {
D = 1 << 7,
A = 1 << 6,
G = 1 << 5,
U = 1 << 4,
X = 1 << 3,
W = 1 << 2,
R = 1 << 1,
V = 1 << 0,
RW = 0b11 << 1,
RX = 0b101 << 1,
UR = 0b10010,
URW = 0b10110,
URX = 0b11010,
}
impl Entry {
pub fn is_d(&self) -> bool {
self.0 & EntryAttributes::D as usize != 0
}
pub fn is_a(&self) -> bool {
self.0 & EntryAttributes::A as usize != 0
}
pub fn is_g(&self) -> bool {
self.0 & EntryAttributes::G as usize != 0
}
pub fn is_u(&self) -> bool {
self.0 & EntryAttributes::U as usize != 0
}
pub fn is_x(&self) -> bool {
self.0 & EntryAttributes::X as usize != 0
}
pub fn is_w(&self) -> bool {
self.0 & EntryAttributes::W as usize != 0
}
pub fn is_r(&self) -> bool {
self.0 & EntryAttributes::R as usize != 0
}
pub fn is_v(&self) -> bool {
self.0 & EntryAttributes::V as usize != 0
}
pub fn is_leaf(&self) -> bool {
self.0 & 0xe != 0
}
pub fn paddr(&self) -> PPN {
PPN((self.0 & !0x3ff) << 2)
}
pub fn flags(&self) -> usize {
self.0 & 0x3ff
}
pub const fn new(ppn: usize, flags: usize) -> Self {
Self(((ppn & !0xfff) >> 2) | flags)
}
}
impl PPN {
pub fn ppn0(&self) -> usize {
(self.0 >> 12) & 0x1ff
}
pub fn ppn1(&self) -> usize {
(self.0 >> 21) & 0x1ff
}
pub fn ppn2(&self) -> usize {
(self.0 >> 30) & 0x3ff_ffff
}
pub fn idx(&self, id: usize) -> usize {
match id {
0 => self.ppn0(),
1 => self.ppn1(),
2 => self.ppn2(),
_ => unreachable!(),
}
}
pub fn clone_page(&self) -> Box<Page> {
let mut pg = Page::new();
unsafe { core::ptr::copy(self.0 as *const u8, pg.data.as_mut_ptr(), PAGE_SIZE); }
pg
}
}
impl VPN {
pub fn vpn0(&self) -> usize {
(self.0 >> 12) & 0x1ff
}
pub fn vpn1(&self) -> usize {
(self.0 >> 21) & 0x1ff
}
pub fn vpn2(&self) -> usize {
(self.0 >> 30) & 0x1ff
}
pub fn idx(&self, id: usize) -> usize {
match id {
0 => self.vpn0(),
1 => self.vpn1(),
2 => self.vpn2(),
_ => unreachable!(),
}
}
}
impl Table {
pub const fn new() -> Self {
Table {
entries: [Entry(0); TABLE_ENTRY_CNT],
}
}
pub const fn len(&self) -> usize {
TABLE_ENTRY_CNT
}
pub fn map(&mut self, vaddr: usize, pg: Box<Page>, flags: usize) {
if flags & EntryAttributes::U as usize == 0 {
panic!("you may only map user page");
}
self.map_addr(vaddr, Box::into_raw(pg) as usize, flags, 0);
}
pub fn kernel_map(&mut self, vaddr: usize, paddr: usize, flags: usize) {
if flags & EntryAttributes::U as usize != 0 {
panic!("you may only map kernel page");
}
self.map_addr(vaddr, paddr, flags, 0);
}
fn map_addr(&mut self, vaddr: usize, paddr: usize, flags: usize, level: usize) {
if paddr % PAGE_SIZE != 0 {
panic!("paddr {:x} not aligned", paddr);
}
if vaddr % PAGE_SIZE != 0 {
panic!("vaddr {:x} not aligned", vaddr);
}
let vpn = VPN(vaddr);
let mut v = &mut self.entries[vpn.vpn2()];
for lvl in (level..2).rev() {
if !v.is_v() {
let page = box Table::new();
*v = Entry::new(Box::into_raw(page) as usize, EntryAttributes::V as usize);
}
let entry = v.paddr().0 as *mut Entry;
v = unsafe { entry.add(vpn.idx(lvl)).as_mut().unwrap() };
}
*v = Entry::new(paddr, flags | EntryAttributes::V as usize)
}
pub fn paddr_of(&self, vaddr: usize) -> Option<usize> {
let vpn = VPN(vaddr);
let mut v = &self.entries[vpn.vpn2()];
if vaddr % PAGE_SIZE != 0 {
panic!("vaddr {:x} not aligned", vaddr);
}
for lvl in (0..2).rev() {
if !v.is_v() {
return None;
}
let entry = v.paddr().0 as *mut Entry;
v = unsafe { entry.add(vpn.idx(lvl)).as_mut().unwrap() };
}
Some(v.paddr().0)
}
fn _walk(&self, level: usize, vpn: usize) {
for i in 0..self.len() {
let v = &self.entries[i];
if v.is_v() {
if !v.is_leaf() {
for _j in 0..(2 - level) {
print!(".");
}
println!(
"{}: 0x{:X} -> 0x{:X}",
i,
(vpn << 9 | i) << (9 * level + 12),
v.paddr().0
);
let table = v.paddr().0 as *const Table;
let table = unsafe { table.as_ref().unwrap() };
table._walk(level - 1, (vpn << 9) | i);
} else {
let u_flag = if v.is_u() { "U" } else { "" };
let r_flag = if v.is_r() { "R" } else { "" };
let w_flag = if v.is_w() { "W" } else { "" };
let x_flag = if v.is_x() { "X" } else { "" };
let vaddr = (vpn << 9 | i) << 12;
if vaddr != v.paddr().0 || true {
for _j in 0..(2 - level) {
print!(".");
}
println!("{}: 0x{:X} -> 0x{:X} {}{}{}{}", i, vaddr, v.paddr().0, u_flag, r_flag, w_flag, x_flag);
}
}
}
}
}
pub fn walk(&self) {
self._walk(2, 0);
}
pub fn id_map_range(&mut self, start: usize, end: usize, bits: usize) {
let mut memaddr = mem::align_val_down(start, PAGE_ORDER);
let num_kb_pages = (mem::align_val(end, 12) - memaddr) / PAGE_SIZE;
for _ in 0..num_kb_pages {
self.map_addr(memaddr, memaddr, bits, 0);
memaddr += PAGE_SIZE;
}
}
pub fn map_range(&mut self, start: usize, end: usize, vaddr_start: usize, bits: usize) {
let mut memaddr = start & !(PAGE_SIZE - 1);
let mut vaddr_start = vaddr_start & !(PAGE_SIZE - 1);
let num_kb_pages = (mem::align_val(end, 12) - memaddr) / PAGE_SIZE;
for _ in 0..num_kb_pages {
self.map_addr(vaddr_start, memaddr, bits, 0);
memaddr += 1 << 12;
vaddr_start += 1 << 12;
}
}
fn drop_walk(&mut self, level: usize) {
for i in 0..self.len() {
let v = &mut self.entries[i];
if v.is_v() {
if v.is_leaf() {
if v.is_u() {
let _pg = unsafe { Box::from_raw(v.paddr().0 as *mut Page) };
}
} else {
let mut table = unsafe { Box::from_raw(v.paddr().0 as *mut Table) };
table.drop_walk(level - 1);
}
}
}
}
fn clone_walk(&self, level: usize) -> Box<Self> {
let mut pgtable = Table::new();
for i in 0..self.len() {
let v = &self.entries[i];
if v.is_v() {
if v.is_leaf() {
if v.is_u() {
let pg = v.paddr().clone_page();
pgtable.entries[i] = Entry::new(Box::into_raw(pg) as usize, v.flags());
}
} else {
let table = unsafe { (v.paddr().0 as *mut Table).as_mut().unwrap() };
let pg = table.clone_walk(level - 1);
pgtable.entries[i] = Entry::new(Box::into_raw(pg) as usize, v.flags());
}
}
}
box pgtable
}
pub fn unmap_user(&mut self) {
for i in 0..self.len() {
let v = &mut self.entries[i];
if v.is_v() {
if v.is_leaf() {
if v.is_u() {
let _pg = unsafe { Box::from_raw(v.paddr().0 as *mut Page) };
*v = Entry(0);
}
} else {
let table = unsafe { (v.paddr().0 as *mut Table).as_mut().unwrap() };
table.unmap_user();
}
}
}
}
}
impl Drop for Table {
fn drop(&mut self) {
self.drop_walk(2);
}
}
impl Clone for Box<Table> {
fn clone(&self) -> Self {
self.clone_walk(2)
}
}
pub static KERNEL_PGTABLE: Table = Table::new();