x86_64: Correct the start address of device memory

When guest ram >= 4G, as mmio hole exists in ram GuestMemory,
GuestMemory->end_addr() is larger than memsize, if memsize is used as
start address of device memory, device memory will overlap with Guest ram.

This patch use GuestMemory->end_addr() as the start address of device
memory.

BUG=None
TEST=Boot vm with guest memory above 4G, and enable vGPU, and run
vGPU benchmark.

Change-Id: Ifc28f3a022cb0a179d16fee4056016a192f2acbd
Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1895232
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Xiong Zhang 2019-10-30 19:14:18 +08:00 committed by Commit Bot
parent fd75d90c76
commit 97a4233860

View file

@ -313,13 +313,13 @@ impl arch::LinuxArch for X8664arch {
) -> std::result::Result<Vec<(Box<dyn PciDevice>, Option<Minijail>)>, E>,
E: StdError + 'static,
{
let mut resources =
Self::get_resource_allocator(components.memory_size, components.wayland_dmabuf);
let has_bios = match components.vm_image {
VmImage::Bios(_) => true,
_ => false,
};
let mem = Self::setup_memory(components.memory_size, has_bios)?;
let mut resources = Self::get_resource_allocator(&mem, components.wayland_dmabuf);
let kvm = Kvm::new().map_err(Error::CreateKvm)?;
let mut vm = Self::create_vm(&kvm, split_irqchip, mem.clone())?;
@ -578,17 +578,17 @@ impl X8664arch {
Ok(None)
}
/// This returns the first page frame number for use by the balloon driver.
/// This returns the start address of device memory
///
/// # Arguments
///
/// * `mem_size` - the size in bytes of physical ram for the guest
fn get_base_dev_pfn(mem_size: u64) -> u64 {
/// * mem: The memory to be used by the guest
fn get_dev_memory_base(mem: &GuestMemory) -> u64 {
// Put device memory at a 2MB boundary after physical memory or 4gb, whichever is greater.
const MB: u64 = 1024 * 1024;
const GB: u64 = 1024 * MB;
let mem_size_round_2mb = (mem_size + 2 * MB - 1) / (2 * MB) * (2 * MB);
std::cmp::max(mem_size_round_2mb, 4 * GB) / sys_util::pagesize() as u64
const MB: u64 = 1 << 20;
const GB: u64 = 1 << 30;
let ram_end_round_2mb = (mem.end_addr().offset() + 2 * MB - 1) / (2 * MB) * (2 * MB);
std::cmp::max(ram_end_round_2mb, 4 * GB)
}
/// This returns a minimal kernel command for this architecture
@ -604,9 +604,9 @@ impl X8664arch {
}
/// Returns a system resource allocator.
fn get_resource_allocator(mem_size: u64, gpu_allocation: bool) -> SystemAllocator {
fn get_resource_allocator(mem: &GuestMemory, gpu_allocation: bool) -> SystemAllocator {
const MMIO_BASE: u64 = 0xe0000000;
let device_addr_start = Self::get_base_dev_pfn(mem_size) * sys_util::pagesize() as u64;
let device_addr_start = Self::get_dev_memory_base(mem);
SystemAllocator::builder()
.add_io_addresses(0xc000, 0x10000)
.add_mmio_addresses(MMIO_BASE, 0x100000)