From 38c41a507454ab1567c92c08d99bc6fb98cfb332 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Mon, 15 Feb 2021 14:00:27 +0000 Subject: [PATCH] vmm: memory_manager: Extract code for allocating new memory This function can then be used by the TDX code to allocate the memory at specific locations required for the TDVF to run from. Signed-off-by: Rob Bradford --- vmm/src/memory_manager.rs | 52 +++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index c20febec4..64508f448 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -1093,25 +1093,11 @@ impl MemoryManager { Ok(start_addr) } - fn hotplug_ram_region(&mut self, size: usize) -> Result, Error> { - info!("Hotplugging new RAM: {}", size); - - // Check that there is a free slot - if self.next_hotplug_slot >= HOTPLUG_COUNT { - return Err(Error::NoSlotAvailable); - } - - // "Inserted" DIMM must have a size that is a multiple of 128MiB - if size % (128 << 20) != 0 { - return Err(Error::InvalidSize); - } - - let start_addr = MemoryManager::start_addr(self.guest_memory.memory().last_addr(), true)?; - - if start_addr.checked_add(size.try_into().unwrap()).unwrap() > self.start_of_device_area() { - return Err(Error::InsufficientHotplugRAM); - } - + pub fn add_ram_region( + &mut self, + start_addr: GuestAddress, + size: usize, + ) -> Result, Error> { // Allocate memory for the region let region = MemoryManager::create_ram_region( &None, @@ -1140,6 +1126,32 @@ impl MemoryManager { slot, }); + self.add_region(Arc::clone(®ion))?; + + Ok(region) + } + + fn hotplug_ram_region(&mut self, size: usize) -> Result, Error> { + info!("Hotplugging new RAM: {}", size); + + // Check that there is a free slot + if self.next_hotplug_slot >= HOTPLUG_COUNT { + return Err(Error::NoSlotAvailable); + } + + // "Inserted" DIMM must have a size that is a multiple of 128MiB + if size % (128 << 20) != 0 { + return Err(Error::InvalidSize); + } + + let start_addr = MemoryManager::start_addr(self.guest_memory.memory().last_addr(), true)?; + + if start_addr.checked_add(size.try_into().unwrap()).unwrap() > self.start_of_device_area() { + return Err(Error::InsufficientHotplugRAM); + } + + let region = self.add_ram_region(start_addr, size)?; + // Tell the allocator self.allocator .lock() @@ -1156,8 +1168,6 @@ impl MemoryManager { self.next_hotplug_slot += 1; - self.add_region(Arc::clone(®ion))?; - Ok(region) }