Skip to content

Commit 218274c

Browse files
committed
refactor: add align_up and align_down functions
Add `align_up` and `align_down` functions to stop reimplementing them in place all over the codebase. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 61db441 commit 218274c

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

src/vmm/src/arch/aarch64/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::arch::{BootProtocol, EntryPoint};
2828
use crate::cpu_config::aarch64::{CpuConfiguration, CpuConfigurationError};
2929
use crate::cpu_config::templates::CustomCpuTemplate;
3030
use crate::initrd::InitrdConfig;
31+
use crate::utils::{align_up, usize_to_u64};
3132
use crate::vmm_config::machine_config::MachineConfig;
3233
use crate::vstate::memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};
3334
use crate::vstate::vcpu::KvmVcpuError;
@@ -129,9 +130,11 @@ pub fn get_kernel_start() -> u64 {
129130

130131
/// Returns the memory address where the initrd could be loaded.
131132
pub fn initrd_load_addr(guest_mem: &GuestMemoryMmap, initrd_size: usize) -> Option<u64> {
132-
let round_to_pagesize =
133-
|size| (size + (super::GUEST_PAGE_SIZE - 1)) & !(super::GUEST_PAGE_SIZE - 1);
134-
match GuestAddress(get_fdt_addr(guest_mem)).checked_sub(round_to_pagesize(initrd_size) as u64) {
133+
let rounded_size = align_up(
134+
usize_to_u64(initrd_size),
135+
usize_to_u64(super::GUEST_PAGE_SIZE),
136+
);
137+
match GuestAddress(get_fdt_addr(guest_mem)).checked_sub(rounded_size) {
135138
Some(offset) => {
136139
if guest_mem.address_in_range(offset) {
137140
Some(offset.raw_value())

src/vmm/src/arch/x86_64/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use crate::arch::{BootProtocol, SYSTEM_MEM_SIZE, SYSTEM_MEM_START};
5151
use crate::cpu_config::templates::{CustomCpuTemplate, GuestConfigError};
5252
use crate::cpu_config::x86_64::CpuConfiguration;
5353
use crate::initrd::InitrdConfig;
54-
use crate::utils::{mib_to_bytes, u64_to_usize};
54+
use crate::utils::{align_down, mib_to_bytes, u64_to_usize, usize_to_u64};
5555
use crate::vmm_config::machine_config::MachineConfig;
5656
use crate::vstate::memory::{
5757
Address, GuestAddress, GuestMemory, GuestMemoryMmap, GuestMemoryRegion,
@@ -138,8 +138,10 @@ pub fn initrd_load_addr(guest_mem: &GuestMemoryMmap, initrd_size: usize) -> Opti
138138
return None;
139139
}
140140

141-
let align_to_pagesize = |address| address & !(super::GUEST_PAGE_SIZE - 1);
142-
Some(align_to_pagesize(lowmem_size - initrd_size) as u64)
141+
Some(align_down(
142+
usize_to_u64(lowmem_size - initrd_size),
143+
usize_to_u64(super::GUEST_PAGE_SIZE),
144+
))
143145
}
144146

145147
/// Configures the system for booting Linux.

src/vmm/src/devices/virtio/test_utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
1010

1111
use crate::devices::virtio::queue::Queue;
1212
use crate::test_utils::single_region_mem;
13-
use crate::utils::u64_to_usize;
13+
use crate::utils::{align_up, u64_to_usize};
1414
use crate::vstate::memory::{Address, Bytes, GuestAddress, GuestMemoryMmap};
1515

1616
#[macro_export]
@@ -250,7 +250,7 @@ impl<'a> VirtQueue<'a> {
250250
const USED_ALIGN: u64 = 4;
251251

252252
let mut x = avail.end().0;
253-
x = (x + USED_ALIGN - 1) & !(USED_ALIGN - 1);
253+
x = align_up(x, USED_ALIGN);
254254

255255
let used = VirtqUsed::new(GuestAddress(x), mem, qsize, u64_to_usize(USED_ALIGN));
256256

src/vmm/src/utils/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,15 @@ pub const fn wrap_usize_to_u32(num: usize) -> Wrapping<u32> {
5353
pub const fn mib_to_bytes(mib: usize) -> usize {
5454
mib << MIB_TO_BYTES_SHIFT
5555
}
56+
57+
/// Align address up to the aligment.
58+
pub const fn align_up(addr: u64, align: u64) -> u64 {
59+
debug_assert!(align != 0);
60+
(addr + align - 1) & !(align - 1)
61+
}
62+
63+
/// Align address down to the aligment.
64+
pub const fn align_down(addr: u64, align: u64) -> u64 {
65+
debug_assert!(align != 0);
66+
addr & !(align - 1)
67+
}

0 commit comments

Comments
 (0)