Skip to content

Commit 0260c2d

Browse files
committed
add offset argument to arch_regions
Signed-off-by: Patrick Roy <[email protected]>
1 parent bd94bd8 commit 0260c2d

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ pub const MMIO_MEM_SIZE: u64 = layout::DRAM_MEM_START - layout::MAPPED_IO_START;
4343

4444
/// Returns a Vec of the valid memory addresses for aarch64.
4545
/// See [`layout`](layout) module for a drawing of the specific memory model for this platform.
46-
pub fn arch_memory_regions(size: usize) -> Vec<(GuestAddress, usize)> {
47-
let dram_size = min(size, layout::DRAM_MEM_MAX_SIZE);
48-
vec![(GuestAddress(layout::DRAM_MEM_START), dram_size)]
46+
pub fn arch_memory_regions(offset: usize, size: usize) -> Vec<(GuestAddress, usize)> {
47+
let dram_size = min(size, layout::DRAM_MEM_MAX_SIZE - offset);
48+
vec![(
49+
GuestAddress(layout::DRAM_MEM_START + offset as u64),
50+
dram_size,
51+
)]
4952
}
5053

5154
/// Configures the system and should be called once per vm before starting vcpu threads.
@@ -130,15 +133,15 @@ mod tests {
130133

131134
#[test]
132135
fn test_regions_lt_1024gb() {
133-
let regions = arch_memory_regions(1usize << 29);
136+
let regions = arch_memory_regions(0, 1usize << 29);
134137
assert_eq!(1, regions.len());
135138
assert_eq!(GuestAddress(super::layout::DRAM_MEM_START), regions[0].0);
136139
assert_eq!(1usize << 29, regions[0].1);
137140
}
138141

139142
#[test]
140143
fn test_regions_gt_1024gb() {
141-
let regions = arch_memory_regions(1usize << 41);
144+
let regions = arch_memory_regions(0, 1usize << 41);
142145
assert_eq!(1, regions.len());
143146
assert_eq!(GuestAddress(super::layout::DRAM_MEM_START), regions[0].0);
144147
assert_eq!(super::layout::DRAM_MEM_MAX_SIZE, regions[0].1);

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,18 @@ pub const MMIO_MEM_SIZE: u64 = MEM_32BIT_GAP_SIZE;
8080
/// These should be used to configure the GuestMemoryMmap structure for the platform.
8181
/// For x86_64 all addresses are valid from the start of the kernel except a
8282
/// carve out at the end of 32bit address space.
83-
pub fn arch_memory_regions(size: usize) -> Vec<(GuestAddress, usize)> {
83+
pub fn arch_memory_regions(offset: usize, size: usize) -> Vec<(GuestAddress, usize)> {
8484
// It's safe to cast MMIO_MEM_START to usize because it fits in a u32 variable
8585
// (It points to an address in the 32 bit space).
86-
match size.checked_sub(usize::try_from(MMIO_MEM_START).unwrap()) {
86+
match (size + offset).checked_sub(u64_to_usize(MMIO_MEM_START)) {
8787
// case1: guest memory fits before the gap
88-
None | Some(0) => vec![(GuestAddress(0), size)],
88+
None | Some(0) => vec![(GuestAddress(offset as u64), size)],
8989
// case2: guest memory extends beyond the gap
9090
Some(remaining) => vec![
91-
(GuestAddress(0), usize::try_from(MMIO_MEM_START).unwrap()),
91+
(
92+
GuestAddress(offset as u64),
93+
u64_to_usize(MMIO_MEM_START) - offset,
94+
),
9295
(GuestAddress(FIRST_ADDR_PAST_32BITS), remaining),
9396
],
9497
}
@@ -363,15 +366,15 @@ mod tests {
363366

364367
#[test]
365368
fn regions_lt_4gb() {
366-
let regions = arch_memory_regions(1usize << 29);
369+
let regions = arch_memory_regions(0, 1usize << 29);
367370
assert_eq!(1, regions.len());
368371
assert_eq!(GuestAddress(0), regions[0].0);
369372
assert_eq!(1usize << 29, regions[0].1);
370373
}
371374

372375
#[test]
373376
fn regions_gt_4gb() {
374-
let regions = arch_memory_regions((1usize << 32) + 0x8000);
377+
let regions = arch_memory_regions(0, (1usize << 32) + 0x8000);
375378
assert_eq!(2, regions.len());
376379
assert_eq!(GuestAddress(0), regions[0].0);
377380
assert_eq!(GuestAddress(1u64 << 32), regions[1].0);

src/vmm/src/resources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl VmResources {
457457
// because that would require running a backend process. If in the future we converge to
458458
// a single way of backing guest memory for vhost-user and non-vhost-user cases,
459459
// that would not be worth the effort.
460-
let regions = crate::arch::arch_memory_regions(self.machine_config.mem_size_mib << MIB_TO_BYTES_SHIFT);
460+
let regions = crate::arch::arch_memory_regions(0, self.machine_config.mem_size_mib << MIB_TO_BYTES_SHIFT);
461461
if vhost_user_device_used {
462462
memory::memfd_backed(
463463
regions.as_ref(),

src/vmm/src/test_utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn multi_region_mem_raw(regions: &[(GuestAddress, usize)]) -> Vec<GuestRegio
5858
/// Creates a [`GuestMemoryMmap`] of the given size with the contained regions laid out in
5959
/// accordance with the requirements of the architecture on which the tests are being run.
6060
pub fn arch_mem(mem_size_bytes: usize) -> GuestMemoryMmap {
61-
multi_region_mem(&crate::arch::arch_memory_regions(mem_size_bytes))
61+
multi_region_mem(&crate::arch::arch_memory_regions(0, mem_size_bytes))
6262
}
6363

6464
pub fn arch_mem_raw(mem_size_bytes: usize) -> Vec<GuestRegionMmap> {

0 commit comments

Comments
 (0)