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
use super::*;
use byteorder::ReadBytesExt;
use std::io::Seek;
use std::{fs, io};
#[derive(Debug)]
pub struct VirtualPage {
data: u64,
}
impl VirtualPage {
pub fn page_frame(&self) -> Option<u64> {
let page_frame = self.data & (u64::max_value() >> 9);
if self.is_present() && page_frame > 0 {
Some(page_frame)
} else {
None
}
}
pub fn swap_type(&self) -> Option<u8> {
let swap_type = (self.data & 0x0F) as u8;
match self.is_swapped() {
true => Some(swap_type),
false => None,
}
}
pub fn swap_offset(&self) -> Option<u64> {
let swap_offset = (self.data & (u64::max_value() >> 9)) >> 5;
match self.is_swapped() {
true => Some(swap_offset),
false => None,
}
}
pub fn is_soft_dirty(&self) -> bool {
self.bit_set(55)
}
pub fn is_exclusively_mapped(&self) -> bool {
self.bit_set(56)
}
pub fn is_file_page_shared_anon(&self) -> bool {
self.bit_set(61)
}
pub fn is_swapped(&self) -> bool {
self.bit_set(62)
}
pub fn is_present(&self) -> bool {
self.bit_set(63)
}
fn bit_set(&self, bit_index: u8) -> bool {
(self.data & (1 << bit_index)) > 0
}
}
pub fn read_page_map(pid: ProcessId, virtual_page_num: usize) -> io::Result<VirtualPage> {
let path = match pid {
ProcessId::SelfPid => String::from("/proc/self/pagemap"),
ProcessId::Num(n) => format!("/proc/{}/pagemap", n),
};
let mut f = fs::File::open(path)?;
let offset = virtual_page_num as u64 * 8;
f.seek(io::SeekFrom::Start(offset))?;
let data = f.read_u64::<byteorder::NativeEndian>()?;
Ok(VirtualPage { data })
}