From 79ec0077b4eefc4b9d47f435168d1dbca51d02c8 Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Sun, 30 Jun 2024 16:27:17 -0700 Subject: [PATCH] fix(mem/aarch64): create guest memory with memfd Shared memory is required to make vhost-user devices work. Signed-off-by: Changyuan Lyu --- alioth/src/board/aarch64.rs | 39 ++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/alioth/src/board/aarch64.rs b/alioth/src/board/aarch64.rs index 93376cb..d5415c3 100644 --- a/alioth/src/board/aarch64.rs +++ b/alioth/src/board/aarch64.rs @@ -95,30 +95,29 @@ where pub fn create_ram(&self) -> Result<()> { let mem_size = self.config.mem_size; let memory = &self.memory; - if mem_size > RAM_32_SIZE { - memory.add_region( - AddrOpt::Fixed(RAM_32_START), - Arc::new(MemRegion::with_mapped( - ArcMemPages::from_anonymous(RAM_32_SIZE as usize, None)?, - MemRegionType::Ram, - )), - )?; + + let low_mem_size = std::cmp::min(mem_size, RAM_32_SIZE); + #[cfg(target_os = "linux")] + let pages_low = ArcMemPages::from_memfd(low_mem_size as usize, None, Some(c"ram-low"))?; + #[cfg(not(target_os = "linux"))] + let pages_low = ArcMemPages::from_anonymous(low_mem_size as usize, None)?; + memory.add_region( + AddrOpt::Fixed(RAM_32_START), + Arc::new(MemRegion::with_mapped(pages_low, MemRegionType::Ram)), + )?; + + let high_mem_size = mem_size.saturating_sub(RAM_32_SIZE) as usize; + if high_mem_size > 0 { + #[cfg(target_os = "linux")] + let pages_high = ArcMemPages::from_memfd(high_mem_size, None, Some(c"ram-high"))?; + #[cfg(not(target_os = "linux"))] + let pages_high = ArcMemPages::from_anonymous(high_mem_size, None)?; memory.add_region( AddrOpt::Fixed(MEM_64_START), - Arc::new(MemRegion::with_mapped( - ArcMemPages::from_anonymous((mem_size - RAM_32_SIZE) as usize, None)?, - MemRegionType::Ram, - )), - )?; - } else { - memory.add_region( - AddrOpt::Fixed(RAM_32_START), - Arc::new(MemRegion::with_mapped( - ArcMemPages::from_anonymous(mem_size as usize, None)?, - MemRegionType::Ram, - )), + Arc::new(MemRegion::with_mapped(pages_high, MemRegionType::Ram)), )?; } + Ok(()) }