From 17911ba15016d1a51eafd570ddf1bb365c726444 Mon Sep 17 00:00:00 2001 From: Pepper Gray Date: Tue, 21 Oct 2025 05:53:47 +0200 Subject: [PATCH] return memory_regions and fdt_addr as expected - memory_regions: align up, then cap to the maximum allowed DRAM size - fdt_addr: put at beginning while <= FDT_MAX_SIZE then move up one page Fixes: #442 Signed-off-by: Pepper Gray --- src/arch/src/aarch64/mod.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/arch/src/aarch64/mod.rs b/src/arch/src/aarch64/mod.rs index 1a750e4f1..b2d2bcb13 100644 --- a/src/arch/src/aarch64/mod.rs +++ b/src/arch/src/aarch64/mod.rs @@ -44,7 +44,12 @@ pub fn arch_memory_regions( firmware_size: Option, ) -> (ArchMemoryInfo, Vec<(GuestAddress, usize)>) { let page_size: usize = unsafe { libc::sysconf(libc::_SC_PAGESIZE).try_into().unwrap() }; - let dram_size = align_upwards!(size, page_size); + // align up, then cap to the maximum allowed DRAM size + let aligned = align_upwards!(size, page_size); + let dram_size = { + let capped = core::cmp::min(aligned as u64, layout::DRAM_MEM_MAX_SIZE); + capped as usize + }; let ram_last_addr = layout::DRAM_MEM_START + (dram_size as u64); let shm_start_addr = ((ram_last_addr / 0x4000_0000) + 1) * 0x4000_0000; @@ -112,9 +117,16 @@ pub fn initrd_load_addr(guest_mem: &GuestMemoryMmap, initrd_size: usize) -> supe } // Auxiliary function to get the address where the device tree blob is loaded. -pub fn get_fdt_addr(_mem: &GuestMemoryMmap) -> u64 { - // Put FDT at the beginning of the DRAM - layout::DRAM_MEM_START +pub fn get_fdt_addr(mem: &GuestMemoryMmap) -> u64 { + // Put FDT at the beginning of DRAM while the RAM region is small (<= FDT_MAX_SIZE) + // For larger guests, move it by one page to avoid overlapping early allocations. + let dram_end = mem.last_addr().raw_value(); + let dram_size = dram_end.saturating_sub(layout::DRAM_MEM_START) + 1; + if dram_size > layout::FDT_MAX_SIZE as u64 { + layout::DRAM_MEM_START + 0x1000 + } else { + layout::DRAM_MEM_START + } } #[cfg(test)]