Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/vmm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ bench = false
[features]
default = []
tracing = ["log-instrument"]
gdb = ["arrayvec", "gdbstub", "gdbstub_arch"]
gdb = ["gdbstub", "gdbstub_arch"]
fuzzing = []

[dependencies]

acpi_tables = { path = "../acpi-tables" }
arrayvec = { version = "0.7.6", optional = true }
arrayvec = { version = "0.7.6" }
aws-lc-rs = "1.17.0"
base64 = "0.22.1"
bitcode = { version = "0.6.9", features = ["serde"] }
Expand Down
84 changes: 84 additions & 0 deletions src/vmm/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use std::path::Path;

use libc::O_NONBLOCK;

use crate::arch::host_page_size;

/// How many bits to left-shift by to convert MiB to bytes
const MIB_TO_BYTES_SHIFT: usize = 20;

Expand Down Expand Up @@ -78,6 +80,43 @@ macro_rules! align_down {
}};
}

/// Is the address aligned to the specified boundary.
///
/// Works with any integer type (`u64`, `usize`, etc.).
/// `$align` must be a power of two.
#[macro_export]
macro_rules! is_aligned {
($addr:expr, $align:expr) => {{
assert!($align.is_power_of_two());
($addr & ($align - 1)) == 0
}};
}

/// Calculate the difference between the current address
/// and the nearest lower page boundary.
pub fn offset_from_lower_host_page(addr: u64) -> u64 {
Comment thread
ilstam marked this conversation as resolved.
let align = usize_to_u64(host_page_size());
addr & (align - 1)
}

/// Align address up to the host page boundary.
pub fn align_up_host_page(addr: u64) -> u64 {
let align = usize_to_u64(host_page_size());
align_up!(addr, align)
}

/// Align address down to the host page boundary.
pub fn align_down_host_page(addr: u64) -> u64 {
let align = usize_to_u64(host_page_size());
align_down!(addr, align)
}

/// Is address host page aligned
pub fn is_host_page_aligned(addr: u64) -> bool {
let align = usize_to_u64(host_page_size());
is_aligned!(addr, align)
}

/// Create and open a file for both reading and writing to it with a O_NONBLOCK flag.
/// In case we open a FIFO, we need all READ, WRITE and O_NONBLOCK in order to not block the process
/// if nobody is consuming the message. Otherwise opening the FIFO with only WRITE and O_NONBLOCK
Expand Down Expand Up @@ -204,4 +243,49 @@ mod tests {
}
}
}

#[test]
fn test_is_aligned() {
assert!(is_aligned!(8_u32, 2_u32));
assert!(is_aligned!(8_u32, 4_u32));
assert!(is_aligned!(8_u32, 8_u32));
assert!(!is_aligned!(8_u32, 16_u32));
}

#[test]
fn test_offset_from_lower_host_page() {
let host_page_size = usize_to_u64(host_page_size());
let offset = host_page_size - 0x100;
assert_eq!(offset_from_lower_host_page(offset), offset);
let offset = host_page_size + 0x100;
assert_eq!(offset_from_lower_host_page(offset), 0x100);
}

#[test]
fn test_align_up_host_page() {
let host_page_size = usize_to_u64(host_page_size());
let offset = host_page_size - 0x100;
assert_eq!(align_up_host_page(offset), host_page_size);
let offset = host_page_size + 0x100;
assert_eq!(align_up_host_page(offset), host_page_size * 2);
}

#[test]
fn test_align_down_host_page() {
let host_page_size = usize_to_u64(host_page_size());
let offset = host_page_size - 0x100;
assert_eq!(align_down_host_page(offset), 0);
let offset = host_page_size + 0x100;
assert_eq!(align_down_host_page(offset), host_page_size);
}

#[test]
fn test_is_host_page_aligned() {
let host_page_size = usize_to_u64(host_page_size());
assert!(is_host_page_aligned(host_page_size));
let offset = host_page_size - 0x100;
assert!(!is_host_page_aligned(offset));
let offset = host_page_size + 0x100;
assert!(!is_host_page_aligned(offset));
}
}
Loading
Loading