This repository was archived by the owner on Jul 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdebug.rs
More file actions
409 lines (362 loc) · 16.8 KB
/
debug.rs
File metadata and controls
409 lines (362 loc) · 16.8 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright © 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use core::mem::transmute;
use core::pin::Pin;
use fallible_collections::{FallibleVec, FallibleVecGlobal};
use log::info;
use x86::controlregs;
use x86::current::paging::*;
use super::page_table::PageTable;
use crate::arch::memory::{paddr_to_kernel_vaddr, PAddr, VAddr};
use crate::error::KError;
use crate::graphviz as dot;
impl PageTable {
const INITIAL_EDGES_CAPACITY: usize = 128;
const INITIAL_NODES_CAPACITY: usize = 128;
fn parse_nodes_edges<'a>(
&'a self,
pml4_table: Pin<&'a [PML4Entry; 512]>,
) -> Result<(dot::Nodes<'a, Nd<'a>>, dot::Edges<'a, Ed<'a>>), KError> {
let mut nodes = Vec::try_with_capacity(PageTable::INITIAL_NODES_CAPACITY)?;
let mut edges = Vec::try_with_capacity(PageTable::INITIAL_EDGES_CAPACITY)?;
nodes.try_push(Nd::PML4(pml4_table, None))?;
unsafe {
for (pml_idx, pml_item) in pml4_table.iter().enumerate() {
let from = Nd::PML4(pml4_table, None);
if pml_item.is_present() {
let pdpt_table =
transmute::<VAddr, &mut PDPT>(VAddr::from_u64(pml_item.address().as_u64()));
let to = Nd::PDPT(pdpt_table, None);
nodes.try_push(to.clone())?;
edges.try_push(((from.clone(), pml_idx), (to.clone(), 0)))?;
let from = to;
for (pdpt_idx, pdpt_item) in pdpt_table.iter().enumerate() {
if pdpt_item.is_present() {
let pd_table = transmute::<VAddr, &mut PD>(VAddr::from_u64(
pdpt_item.address().as_u64(),
));
if pdpt_item.is_page() {
let _vaddr: usize = (512 * (512 * (512 * 0x1000))) * pml_idx
+ (512 * (512 * 0x1000)) * pdpt_idx;
let _to = Nd::HugePage(pdpt_item.address());
//nodes.try_push(to.clone())?;
//edges.try_push((from.clone(), to.clone()))?;
} else {
let to = Nd::PD(pd_table, None);
nodes.try_push(to.clone())?;
edges.try_push(((from.clone(), pdpt_idx), (to.clone(), 0)))?;
let from = to;
for (pd_idx, pd_item) in pd_table.iter().enumerate() {
if pd_item.is_present() {
let ptes = transmute::<VAddr, &mut PT>(VAddr::from_u64(
pd_item.address().as_u64(),
));
if pd_item.is_page() {
let _vaddr: usize = (512 * (512 * (512 * 0x1000)))
* pml_idx
+ (512 * (512 * 0x1000)) * pdpt_idx
+ (512 * 0x1000) * pd_idx;
//let to = Nd::LargePage(pd_item.address());
//nodes.try_push(to.clone())?;
//edges.try_push((from.clone(), to.clone()))?;
} else {
let to = Nd::PT(ptes, None);
nodes.try_push(to.clone())?;
edges.try_push((
(from.clone(), pd_idx),
(to.clone(), 0),
))?;
/*let from = to.clone();
assert!(!pd_item.is_page());
for (pte_idx, pte) in ptes.iter().enumerate() {
let vaddr: usize = (512 * (512 * (512 * 0x1000)))
* pml_idx
+ (512 * (512 * 0x1000)) * pdpt_idx
+ (512 * 0x1000) * pd_idx
+ (0x1000) * pte_idx;
if pte.is_present() {
let to = Nd::Page(pte.address());
nodes.try_push(to.clone())?;
edges.try_push((from.clone(), to.clone()))?;
}
}*/
}
}
}
}
}
}
}
}
}
Ok((nodes.into(), edges.into()))
}
}
#[allow(unused)]
pub unsafe fn dump_current_table(log_level: usize) {
let cr_three: u64 = controlregs::cr3();
let pml4: PAddr = PAddr::from(cr_three);
let pml4_table = transmute::<VAddr, &PML4>(paddr_to_kernel_vaddr(pml4));
dump_table(pml4_table, log_level);
}
#[allow(unused)]
pub unsafe fn dump_table(pml4_table: &PML4, log_level: usize) {
for (pml_idx, pml_item) in pml4_table.iter().enumerate() {
if pml_item.is_present() {
info!("PML4 item#{}: maps to {:?}", pml_idx, pml_item);
let pdpt_table =
transmute::<VAddr, &mut PDPT>(VAddr::from_u64(pml_item.address().as_u64()));
if log_level <= 1 {
continue;
}
for (pdpt_idx, pdpt_item) in pdpt_table.iter().enumerate() {
info!("PDPT item#{}: maps to {:?}", pdpt_idx, pdpt_item);
if pdpt_item.is_present() {
let pd_table =
transmute::<VAddr, &mut PD>(VAddr::from_u64(pdpt_item.address().as_u64()));
if pdpt_item.is_page() {
let vaddr: usize = (512 * (512 * (512 * 0x1000))) * pml_idx
+ (512 * (512 * 0x1000)) * pdpt_idx;
info!("PDPT item: vaddr 0x{:x} maps to {:?}", vaddr, pdpt_item);
} else {
for (pd_idx, pd_item) in pd_table.iter().enumerate() {
info!("PD item#{}: maps to {:?}", pd_idx, pd_item);
if pd_item.is_present() {
let ptes = transmute::<VAddr, &mut PT>(VAddr::from_u64(
pd_item.address().as_u64(),
));
if pd_item.is_page() {
let vaddr: usize = (512 * (512 * (512 * 0x1000))) * pml_idx
+ (512 * (512 * 0x1000)) * pdpt_idx
+ (512 * 0x1000) * pd_idx;
info!("PD item: vaddr 0x{:x} maps to {:?}", vaddr, pd_item);
} else {
assert!(!pd_item.is_page());
for (pte_idx, pte) in ptes.iter().enumerate() {
let vaddr: usize = (512 * (512 * (512 * 0x1000))) * pml_idx
+ (512 * (512 * 0x1000)) * pdpt_idx
+ (512 * 0x1000) * pd_idx
+ (0x1000) * pte_idx;
if pte.is_present() {
info!(
"PT item: vaddr 0x{:x} maps to flags {:?}",
vaddr, pte
);
}
}
}
}
}
}
}
}
}
}
}
#[allow(clippy::upper_case_acronyms)]
#[derive(Copy, Clone)]
pub(crate) enum Nd<'a> {
HugePage(PAddr),
PT(&'a PT, Option<usize>),
PD(&'a PD, Option<usize>),
PDPT(&'a PDPT, Option<usize>),
PML4(Pin<&'a PML4>, Option<usize>),
}
/// Edge is connection of two nodes and slot within the page-table.
type Ed<'a> = ((Nd<'a>, usize), (Nd<'a>, usize));
impl<'a> dot::Labeller<'a> for PageTable {
type Node = Nd<'a>;
type Edge = Ed<'a>;
fn graph_id(&'a self) -> dot::Id<'a> {
dot::Id::new("vspace").unwrap()
}
fn node_shape(&'a self, n: &Self::Node) -> Option<dot::LabelText<'a>> {
match n {
Nd::PT(_pt, _) => Some(dot::LabelText::label("record")),
Nd::PD(_pd, _) => Some(dot::LabelText::label("record")),
Nd::PDPT(_pdpt, _) => Some(dot::LabelText::label("record")),
Nd::PML4(_pml4, _) => Some(dot::LabelText::label("record")),
Nd::HugePage(_addr) => None,
}
}
/// Generate a label that looks like this:
/// `<f0> PML4_0x400035c00008 | <f1> PML4_0x400035c000016 | (nil) | ... | (nil) `
fn node_label(&'a self, n: &Self::Node) -> dot::LabelText<'a> {
let mut node_label = String::with_capacity(512 * 8);
enum Printer {
EmitLine,
EmitDots,
Skip,
}
let label = match n {
Nd::PT(pt, _) => {
let mut state = Printer::EmitLine;
for pt_idx in 0..pt.len() {
if pt_idx == 511 {
state = Printer::EmitLine;
}
let pt_item = pt[pt_idx];
match state {
Printer::EmitLine => {
if pt_item.is_present() {
if node_label.len() > 0 {
node_label += " | "
}
node_label +=
format!("<f{}> {:#x}", pt_idx, pt_item.address(),).as_str();
if pt_idx < 511 && pt[pt_idx + 1].is_present() {
state = Printer::EmitDots;
} else {
state = Printer::EmitLine;
}
}
}
Printer::EmitDots => {
if node_label.len() > 0 {
node_label += " | "
}
node_label += "...";
if pt_idx < 511 && pt[pt_idx + 1].is_present() {
state = Printer::Skip;
} else {
state = Printer::EmitLine;
}
}
Printer::Skip => {
if pt_idx < 511 && pt[pt_idx + 1].is_present() {
state = Printer::Skip;
} else {
state = Printer::EmitLine;
}
}
}
}
node_label
}
Nd::PD(pd, _) => {
let mut state = Printer::EmitLine;
for pd_idx in 0..pd.len() {
if pd_idx == 511 {
state = Printer::EmitLine;
}
let pd_item = pd[pd_idx];
match state {
Printer::EmitLine => {
if pd_item.is_present() {
if node_label.len() > 0 {
node_label += " | "
}
node_label +=
format!("<f{}> {:#x}", pd_idx, pd_item.address(),).as_str();
if pd_idx < 511 && pd[pd_idx + 1].is_present() {
state = Printer::EmitDots;
} else {
state = Printer::EmitLine;
}
}
}
Printer::EmitDots => {
if node_label.len() > 0 {
node_label += " | "
}
node_label += "...";
if pd_idx < 511 && pd[pd_idx + 1].is_present() {
state = Printer::Skip;
} else {
state = Printer::EmitLine;
}
}
Printer::Skip => {
if pd_idx < 511 && pd[pd_idx + 1].is_present() {
state = Printer::Skip;
} else {
state = Printer::EmitLine;
}
}
}
}
node_label
}
Nd::PDPT(pdpt, _) => {
for (pdpt_idx, pdpt_item) in pdpt.iter().enumerate() {
if pdpt_item.is_present() {
if node_label.len() > 0 {
node_label += " | "
}
node_label +=
format!("<f{}> {:#x}", pdpt_idx, pdpt_item.address(),).as_str();
}
}
node_label
}
Nd::PML4(pml4, _) => {
for (pml_idx, pml_item) in pml4.iter().enumerate() {
if pml_item.is_present() {
if node_label.len() > 0 {
node_label += " | "
}
node_label += format!("<f{}> {:#x}", pml_idx, pml_item.address(),).as_str();
}
}
node_label
}
Nd::HugePage(addr) => format!("Page1GiB_{:#x}", addr),
};
dot::LabelText::label(label)
}
fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
let label = match n {
Nd::PT(pt, None) => format!("PT_{:p}", *pt),
Nd::PD(pd, None) => format!("PD_{:p}", *pd),
Nd::PDPT(pdpt, None) => format!("PDPT_{:p}", *pdpt),
Nd::PML4(pml4, None) => format!("PDPT_{:p}", *pml4),
Nd::PT(pt, Some(slot)) => format!("PT_{:p}:f{}", *pt, slot),
Nd::PD(pd, Some(slot)) => format!("PD_{:p}:f{}", *pd, slot),
Nd::PDPT(pdpt, Some(slot)) => format!("PDPT_{:p}:f{}", *pdpt, slot),
Nd::PML4(pml4, Some(slot)) => format!("PML4_{:p}:f{}", *pml4, slot),
Nd::HugePage(addr) => format!("Page1GiB_{:#x}", addr),
};
dot::Id::new(label).expect("Can't make label")
}
}
impl<'a> dot::GraphWalk<'a> for PageTable {
type Node = Nd<'a>;
type Edge = Ed<'a>;
fn nodes(&self) -> dot::Nodes<'a, Nd> {
// Failure ok this is only used for debugging
let (nodes, _) = self
.parse_nodes_edges(self.pml4())
.expect("Can't parse nodes");
nodes.into()
}
fn edges(&'a self) -> dot::Edges<'a, Ed> {
// Failure ok this is only used for debugging
let (_, edges) = self
.parse_nodes_edges(self.pml4())
.expect("Can't parse edges");
edges.into()
}
fn source(&self, e: &Ed<'a>) -> Nd<'a> {
match (e.0).0 {
Nd::HugePage(_) => (e.0).0,
Nd::PT(ptr, None) => Nd::PT(ptr, Some((e.0).1)),
Nd::PD(ptr, None) => Nd::PD(ptr, Some((e.0).1)),
Nd::PDPT(ptr, None) => Nd::PDPT(ptr, Some((e.0).1)),
Nd::PML4(ptr, None) => Nd::PML4(ptr, Some((e.0).1)),
_ => unimplemented!(),
}
}
fn target(&self, e: &Ed<'a>) -> Nd<'a> {
match (e.1).0 {
Nd::HugePage(_) => (e.1).0,
Nd::PT(ptr, None) => Nd::PT(ptr, Some((e.1).1)),
Nd::PD(ptr, None) => Nd::PD(ptr, Some((e.1).1)),
Nd::PDPT(ptr, None) => Nd::PDPT(ptr, Some((e.1).1)),
Nd::PML4(ptr, None) => Nd::PML4(ptr, Some((e.1).1)),
_ => unimplemented!(),
}
}
}