Skip to content

Commit 17911ba

Browse files
committed
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 <[email protected]>
1 parent 246b3f7 commit 17911ba

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/arch/src/aarch64/mod.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ pub fn arch_memory_regions(
4444
firmware_size: Option<usize>,
4545
) -> (ArchMemoryInfo, Vec<(GuestAddress, usize)>) {
4646
let page_size: usize = unsafe { libc::sysconf(libc::_SC_PAGESIZE).try_into().unwrap() };
47-
let dram_size = align_upwards!(size, page_size);
47+
// align up, then cap to the maximum allowed DRAM size
48+
let aligned = align_upwards!(size, page_size);
49+
let dram_size = {
50+
let capped = core::cmp::min(aligned as u64, layout::DRAM_MEM_MAX_SIZE);
51+
capped as usize
52+
};
4853
let ram_last_addr = layout::DRAM_MEM_START + (dram_size as u64);
4954
let shm_start_addr = ((ram_last_addr / 0x4000_0000) + 1) * 0x4000_0000;
5055

@@ -112,9 +117,16 @@ pub fn initrd_load_addr(guest_mem: &GuestMemoryMmap, initrd_size: usize) -> supe
112117
}
113118

114119
// Auxiliary function to get the address where the device tree blob is loaded.
115-
pub fn get_fdt_addr(_mem: &GuestMemoryMmap) -> u64 {
116-
// Put FDT at the beginning of the DRAM
117-
layout::DRAM_MEM_START
120+
pub fn get_fdt_addr(mem: &GuestMemoryMmap) -> u64 {
121+
// Put FDT at the beginning of DRAM while the RAM region is small (<= FDT_MAX_SIZE)
122+
// For larger guests, move it by one page to avoid overlapping early allocations.
123+
let dram_end = mem.last_addr().raw_value();
124+
let dram_size = dram_end.saturating_sub(layout::DRAM_MEM_START) + 1;
125+
if dram_size > layout::FDT_MAX_SIZE as u64 {
126+
layout::DRAM_MEM_START + 0x1000
127+
} else {
128+
layout::DRAM_MEM_START
129+
}
118130
}
119131

120132
#[cfg(test)]

0 commit comments

Comments
 (0)