use std::path::PathBuf;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PageMapEntry {
pub pfn: Option<u64>,
pub present: bool,
pub swapped: bool,
pub swap_pte: bool,
pub soft_dirty: bool,
pub exclusive: bool,
pub file_page: bool,
}
impl PageMapEntry {
#[inline]
pub fn from_raw(raw: u64) -> Self {
const MASK_PFN: u64 = (1 << 55) - 1;
const BIT_SWAP_PTE: u64 = 1 << 55;
const BIT_PRESENT: u64 = 1 << 56;
const BIT_SWAPPED: u64 = 1 << 57;
const BIT_SOFT_DIRTY: u64 = 1 << 58;
const BIT_EXCLUSIVE: u64 = 1 << 59;
const BIT_FILE: u64 = 1 << 60;
let present = raw & BIT_PRESENT != 0;
let swapped = raw & BIT_SWAPPED != 0;
PageMapEntry {
pfn: (present || swapped).then_some(raw & MASK_PFN),
present,
swapped,
swap_pte: raw & BIT_SWAP_PTE != 0,
soft_dirty: raw & BIT_SOFT_DIRTY != 0,
exclusive: raw & BIT_EXCLUSIVE != 0,
file_page: raw & BIT_FILE != 0,
}
}
#[inline]
pub fn is_mapped(&self) -> bool {
self.present || self.swapped
}
}
pub(super) fn parse(bytes: &[u8]) -> Result<Vec<PageMapEntry>> {
if bytes.len() % 8 != 0 {
return Err(Error::Parse {
path: PathBuf::from("<pagemap>"),
line: 0,
msg: "size is not a multiple of 8 bytes",
});
}
Ok(bytes
.chunks_exact(8)
.map(|c| {
let raw = u64::from_ne_bytes([c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]]);
PageMapEntry::from_raw(raw)
})
.collect())
}