|
| 1 | +//! Utilities for reading /proc/self/pagemap to track dirty pages. |
| 2 | +
|
| 3 | +#![allow(clippy::cast_possible_wrap)] |
| 4 | + |
| 5 | +use std::fs::File; |
| 6 | +use std::os::unix::io::AsRawFd; |
| 7 | + |
| 8 | +use crate::arch::host_page_size; |
| 9 | + |
| 10 | +const PAGEMAP_ENTRY_SIZE: usize = 8; |
| 11 | + |
| 12 | +/// Errors related to pagemap operations |
| 13 | +#[derive(Debug, thiserror::Error, displaydoc::Display)] |
| 14 | +pub enum PagemapError { |
| 15 | + /// Failed to open /proc/self/pagemap: {0} |
| 16 | + OpenPagemap(#[source] std::io::Error), |
| 17 | + /// Failed to read pagemap entry: {0} |
| 18 | + ReadEntry(#[source] std::io::Error), |
| 19 | + /// Failed to open /proc/self/clear_refs: {0} |
| 20 | + OpenClearRefs(#[source] std::io::Error), |
| 21 | + /// Failed to clear soft-dirty bits: {0} |
| 22 | + ClearSoftDirty(#[source] std::io::Error), |
| 23 | +} |
| 24 | + |
| 25 | +/// Represents a single entry in /proc/pid/pagemap. |
| 26 | +/// |
| 27 | +/// Each virtual page has an 8-byte entry with the following layout: |
| 28 | +/// - Bits 0-54: Page frame number (PFN) if present |
| 29 | +/// - Bit 55: Page is soft-dirty (written to since last clear) |
| 30 | +/// - Bit 56: Page is exclusively mapped |
| 31 | +/// - Bit 57: Page is write-protected via userfaultfd |
| 32 | +/// - Bit 58: Unused |
| 33 | +/// - Bit 59-60: Unused |
| 34 | +/// - Bit 61: Page is file-page or shared-anon |
| 35 | +/// - Bit 62: Page is swapped |
| 36 | +/// - Bit 63: Page is present in RAM |
| 37 | +#[derive(Debug, Clone, Copy)] |
| 38 | +pub struct PagemapEntry { |
| 39 | + raw: u64, |
| 40 | +} |
| 41 | + |
| 42 | +impl PagemapEntry { |
| 43 | + /// Create a PagemapEntry from bytes (little-endian) |
| 44 | + pub fn from_bytes(bytes: [u8; 8]) -> Self { |
| 45 | + Self { |
| 46 | + raw: u64::from_ne_bytes(bytes), |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// Check if page is write-protected via userfaultfd |
| 51 | + pub fn is_write_protected(&self) -> bool { |
| 52 | + (self.raw & (1u64 << 57)) != 0 |
| 53 | + } |
| 54 | + |
| 55 | + /// Check if page is present in RAM (bit 63) |
| 56 | + pub fn is_present(&self) -> bool { |
| 57 | + (self.raw & (1u64 << 63)) != 0 |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// Reader for /proc/self/pagemap |
| 62 | +#[derive(Debug)] |
| 63 | +pub struct PagemapReader { |
| 64 | + pagemap_fd: File, |
| 65 | +} |
| 66 | + |
| 67 | +impl PagemapReader { |
| 68 | + /// Create a new PagemapReader |
| 69 | + pub fn new(_page_size: usize) -> Result<Self, PagemapError> { |
| 70 | + let pagemap_fd = File::open("/proc/self/pagemap").map_err(PagemapError::OpenPagemap)?; |
| 71 | + |
| 72 | + Ok(Self { pagemap_fd }) |
| 73 | + } |
| 74 | + |
| 75 | + /// Check if a single page is dirty (write-protected bit cleared). |
| 76 | + /// |
| 77 | + /// Checks the first host page (4K) of the guest page at the given address. |
| 78 | + /// For huge pages, all host pages within the huge page typically have the same |
| 79 | + /// dirty status, so sampling the first is sufficient. |
| 80 | + /// |
| 81 | + /// # Arguments |
| 82 | + /// * `virt_addr` - Virtual address of the page to check |
| 83 | + /// |
| 84 | + /// # Returns |
| 85 | + /// True if the page is present and write-protected bit is cleared (dirty). |
| 86 | + pub fn is_page_dirty(&self, virt_addr: usize) -> Result<bool, PagemapError> { |
| 87 | + // Pagemap always uses host (4K) page size |
| 88 | + let host_page_size = host_page_size(); |
| 89 | + |
| 90 | + // Calculate offset for this virtual page (using host page size) |
| 91 | + let host_vpn = virt_addr / host_page_size; |
| 92 | + let offset = (host_vpn * PAGEMAP_ENTRY_SIZE) as i64; |
| 93 | + |
| 94 | + let mut entry_bytes = [0u8; 8]; |
| 95 | + |
| 96 | + // SAFETY: pread is safe as long as the fd is valid and the buffer is properly sized |
| 97 | + let ret = unsafe { |
| 98 | + libc::pread( |
| 99 | + self.pagemap_fd.as_raw_fd(), |
| 100 | + entry_bytes.as_mut_ptr().cast(), |
| 101 | + PAGEMAP_ENTRY_SIZE, |
| 102 | + offset, |
| 103 | + ) |
| 104 | + }; |
| 105 | + |
| 106 | + if ret != PAGEMAP_ENTRY_SIZE as isize { |
| 107 | + return Err(PagemapError::ReadEntry(std::io::Error::last_os_error())); |
| 108 | + } |
| 109 | + |
| 110 | + let entry = PagemapEntry::from_bytes(entry_bytes); |
| 111 | + |
| 112 | + // Page must be present and the write_protected bit cleared (indicating it was written to) |
| 113 | + Ok(entry.is_present() && !entry.is_write_protected()) |
| 114 | + } |
| 115 | +} |
0 commit comments