From 097623dce41a6fa4a23e41cb4790e3a66f48fcb6 Mon Sep 17 00:00:00 2001 From: David Stevens Date: Tue, 1 Mar 2022 14:33:55 +0900 Subject: [PATCH] arch: return system allocator config Have the arch code return the SystemAllocatorConfig instead of a SystemAllocator. This will be used to allow the core code to apply additional restrictions on top of the arch code's restrictions. BUG=b:181736020 TEST=compiles Change-Id: I4d9ca277f039586e664648492c8744967dcd2ee5 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3516665 Reviewed-by: Daniel Verkamp Reviewed-by: Junichi Uekawa Tested-by: kokoro Commit-Queue: David Stevens --- aarch64/src/lib.rs | 19 ++++++++++++------- arch/src/lib.rs | 12 ++++++++---- resources/src/system_allocator.rs | 8 ++++++++ src/linux/mod.rs | 3 ++- x86_64/src/lib.rs | 7 +++---- x86_64/src/test_integration.rs | 4 +++- 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/aarch64/src/lib.rs b/aarch64/src/lib.rs index 39a0e00937..2678735e44 100644 --- a/aarch64/src/lib.rs +++ b/aarch64/src/lib.rs @@ -207,8 +207,11 @@ impl arch::LinuxArch for AArch64 { Ok(arch_memory_regions(components.memory_size)) } - fn create_system_allocator(vm: &V) -> SystemAllocator { - Self::get_resource_allocator(vm.get_memory().memory_size(), vm.get_guest_phys_addr_bits()) + fn get_system_allocator_config(vm: &V) -> SystemAllocatorConfig { + Self::get_resource_allocator_config( + vm.get_memory().memory_size(), + vm.get_guest_phys_addr_bits(), + ) } fn build_vm( @@ -523,13 +526,16 @@ impl AArch64 { cmdline } - /// Returns a system resource allocator. + /// Returns a system resource allocator configuration. /// /// # Arguments /// /// * `mem_size` - Size of guest memory (RAM) in bytes. /// * `guest_phys_addr_bits` - Size of guest physical addresses (IPA) in bits. - fn get_resource_allocator(mem_size: u64, guest_phys_addr_bits: u8) -> SystemAllocator { + fn get_resource_allocator_config( + mem_size: u64, + guest_phys_addr_bits: u8, + ) -> SystemAllocatorConfig { let guest_phys_end = 1u64 << guest_phys_addr_bits; // The platform MMIO region is immediately past the end of RAM. let plat_mmio_base = AARCH64_PHYS_MEM_START + mem_size; @@ -544,7 +550,7 @@ impl AArch64 { guest_phys_end, high_mmio_base, ); }); - SystemAllocator::new(SystemAllocatorConfig { + SystemAllocatorConfig { io: None, low_mmio: MemRegion { base: AARCH64_MMIO_BASE, @@ -559,8 +565,7 @@ impl AArch64 { size: plat_mmio_size, }), first_irq: AARCH64_IRQ_BASE, - }) - .unwrap() + } } /// This adds any early platform devices for this architecture. diff --git a/arch/src/lib.rs b/arch/src/lib.rs index afe259b62e..6900ee24d1 100644 --- a/arch/src/lib.rs +++ b/arch/src/lib.rs @@ -26,7 +26,7 @@ use devices::{ use hypervisor::{IoEventAddress, ProtectionType, Vm}; use minijail::Minijail; use remain::sorted; -use resources::{MmioType, SystemAllocator}; +use resources::{MmioType, SystemAllocator, SystemAllocatorConfig}; use sync::Mutex; use thiserror::Error; use vm_control::{BatControl, BatteryType, PmResource}; @@ -151,12 +151,16 @@ pub trait LinuxArch { components: &VmComponents, ) -> std::result::Result, Self::Error>; - /// Creates a new `SystemAllocator` that fits the given `Vm`'s memory layout. + /// Gets the configuration for a new `SystemAllocator` that fits the given `Vm`'s memory layout. + /// + /// This is the per-architecture template for constructing the `SystemAllocator`. Platform + /// agnostic modifications may be made to this configuration, but the final `SystemAllocator` + /// will be at least as strict as this configuration. /// /// # Arguments /// /// * `vm` - The virtual machine to be used as a template for the `SystemAllocator`. - fn create_system_allocator(vm: &V) -> SystemAllocator; + fn get_system_allocator_config(vm: &V) -> SystemAllocatorConfig; /// Takes `VmComponents` and generates a `RunnableLinuxVm`. /// @@ -168,7 +172,7 @@ pub trait LinuxArch { /// * `reset_evt` - Event used by sub-devices to request that crosvm exit because guest /// requested reset. /// * `system_allocator` - Allocator created by this trait's implementation of - /// `create_system_allocator`. + /// `get_system_allocator_config`. /// * `serial_parameters` - Definitions for how the serial devices should be configured. /// * `serial_jail` - Jail used for serial devices created here. /// * `battery` - Defines what battery device will be created. diff --git a/resources/src/system_allocator.rs b/resources/src/system_allocator.rs index e54a9d9c19..ad4a2d5c97 100644 --- a/resources/src/system_allocator.rs +++ b/resources/src/system_allocator.rs @@ -29,8 +29,16 @@ pub struct SystemAllocatorConfig { /// IO ports. Only for x86_64. pub io: Option, /// Low (<=4GB) MMIO region. + /// + /// Parts of this region may be reserved or otherwise excluded from the + /// created SystemAllocator's MmioType::Low allocator. However, no new + /// regions will be added. pub low_mmio: MemRegion, /// High (>4GB) MMIO region. + /// + /// Parts of this region may be reserved or otherwise excluded from the + /// created SystemAllocator's MmioType::High allocator. However, no new + /// regions will be added. pub high_mmio: MemRegion, /// Platform MMIO space. Only for ARM. pub platform_mmio: Option, diff --git a/src/linux/mod.rs b/src/linux/mod.rs index 110164d22f..e2d059386c 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -1156,7 +1156,8 @@ where let reset_evt = Event::new().context("failed to create event")?; let crash_evt = Event::new().context("failed to create event")?; let (panic_rdtube, panic_wrtube) = Tube::pair().context("failed to create tube")?; - let mut sys_allocator = Arch::create_system_allocator(&vm); + let mut sys_allocator = SystemAllocator::new(Arch::get_system_allocator_config(&vm)) + .context("failed to create system allocator")?; // Allocate the ramoops region first. AArch64::build_vm() assumes this. let ramoops_region = match &components.pstore { diff --git a/x86_64/src/lib.rs b/x86_64/src/lib.rs index fc2152ceed..a7ee071a53 100644 --- a/x86_64/src/lib.rs +++ b/x86_64/src/lib.rs @@ -391,11 +391,11 @@ impl arch::LinuxArch for X8664arch { Ok(arch_memory_regions(components.memory_size, bios_size)) } - fn create_system_allocator(vm: &V) -> SystemAllocator { + fn get_system_allocator_config(vm: &V) -> SystemAllocatorConfig { let guest_mem = vm.get_memory(); let high_mmio_start = Self::get_high_mmio_base(guest_mem); let high_mmio_size = Self::get_high_mmio_size(vm); - SystemAllocator::new(SystemAllocatorConfig { + SystemAllocatorConfig { io: Some(MemRegion { base: 0xc000, size: 0x4000, @@ -410,8 +410,7 @@ impl arch::LinuxArch for X8664arch { }, platform_mmio: None, first_irq: X86_64_IRQ_BASE, - }) - .unwrap() + } } fn build_vm( diff --git a/x86_64/src/test_integration.rs b/x86_64/src/test_integration.rs index 90eec51efe..ddb3d3cb7e 100644 --- a/x86_64/src/test_integration.rs +++ b/x86_64/src/test_integration.rs @@ -7,6 +7,7 @@ use arch::LinuxArch; use devices::IrqChipX86_64; use hypervisor::{HypervisorX86_64, ProtectionType, VcpuExit, VcpuX86_64, VmX86_64}; +use resources::SystemAllocator; use vm_memory::{GuestAddress, GuestMemory}; use super::cpuid::setup_cpuid; @@ -102,7 +103,8 @@ where let guest_mem = GuestMemory::new(&arch_mem_regions).unwrap(); let (hyp, mut vm) = create_vm(guest_mem.clone()); - let mut resources = X8664arch::create_system_allocator(&vm); + let mut resources = SystemAllocator::new(X8664arch::get_system_allocator_config(&vm)) + .expect("failed to create system allocator"); let (irqchip_tube, device_tube) = Tube::pair().expect("failed to create irq tube"); let mut irq_chip = create_irq_chip(vm.try_clone().expect("failed to clone vm"), 1, device_tube);