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 <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford
2021-02-15 14:00:27 +00:00
committed by Samuel Ortiz
parent 6e4c90f305
commit 38c41a5074
+31 -21
View File
@@ -1093,25 +1093,11 @@ impl MemoryManager {
Ok(start_addr) Ok(start_addr)
} }
fn hotplug_ram_region(&mut self, size: usize) -> Result<Arc<GuestRegionMmap>, Error> { pub fn add_ram_region(
info!("Hotplugging new RAM: {}", size); &mut self,
start_addr: GuestAddress,
// Check that there is a free slot size: usize,
if self.next_hotplug_slot >= HOTPLUG_COUNT { ) -> Result<Arc<GuestRegionMmap>, Error> {
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);
}
// Allocate memory for the region // Allocate memory for the region
let region = MemoryManager::create_ram_region( let region = MemoryManager::create_ram_region(
&None, &None,
@@ -1140,6 +1126,32 @@ impl MemoryManager {
slot, slot,
}); });
self.add_region(Arc::clone(&region))?;
Ok(region)
}
fn hotplug_ram_region(&mut self, size: usize) -> Result<Arc<GuestRegionMmap>, 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 // Tell the allocator
self.allocator self.allocator
.lock() .lock()
@@ -1156,8 +1168,6 @@ impl MemoryManager {
self.next_hotplug_slot += 1; self.next_hotplug_slot += 1;
self.add_region(Arc::clone(&region))?;
Ok(region) Ok(region)
} }