diff --git a/Guide/src/reference/devices/firmware/linux_direct.md b/Guide/src/reference/devices/firmware/linux_direct.md index 082b5128e84..06c34ba94e6 100644 --- a/Guide/src/reference/devices/firmware/linux_direct.md +++ b/Guide/src/reference/devices/firmware/linux_direct.md @@ -70,6 +70,15 @@ hole and the live shared-status page in e820, and leaves the fixed 3-to-4-GiB MMIO aperture out of RAM. The loader enters the ELF kernel in long mode with `RSI=0x2000`, `CR3=0x4000`, and paging enabled. +The microVM virtio devices may reach only the RAM ranges of this e820 map with +DMA, which after a restore include the RAM that the restore adds. A device +refuses a queue whose rings, or a descriptor whose buffer, lies even partly +outside them, such as in the shared-status page, in the ISA hole, or across the +aperture: it reads and writes none of it, and its transport sets +`DEVICE_NEEDS_RESET` in the device status, with a configuration-change +notification if the driver has set `DRIVER_OK`. A refused ring fails +`DRIVER_OK`. Resetting the device clears the mark. + ## AArch64 Boot Flow On AArch64, OpenVMM supports two modes for presenting hardware descriptions to diff --git a/openvmm/openvmm_core/src/worker/dispatch.rs b/openvmm/openvmm_core/src/worker/dispatch.rs index abaaa637193..fb3558cfb0f 100644 --- a/openvmm/openvmm_core/src/worker/dispatch.rs +++ b/openvmm/openvmm_core/src/worker/dispatch.rs @@ -3290,7 +3290,7 @@ impl InitializedVm { let mut pci_device_number = 10; let mut virtio_mmio_index = 0; let mut microvm_virtio_slots = - microvm::VirtioMmioSlots::new(&cfg.microvm.sandbox_blocks, chipset_mmio); + microvm::VirtioMmioSlots::new(&cfg.microvm.sandbox_blocks, chipset_mmio, &mem_layout); // Avoid an ISA interrupt to avoid conflicts and to avoid needing to // configure the line as level-triggered in the MADT (necessary for diff --git a/openvmm/openvmm_core/src/worker/dispatch/microvm.rs b/openvmm/openvmm_core/src/worker/dispatch/microvm.rs index 48905004454..80b7a411dc1 100644 --- a/openvmm/openvmm_core/src/worker/dispatch/microvm.rs +++ b/openvmm/openvmm_core/src/worker/dispatch/microvm.rs @@ -39,6 +39,7 @@ use virtio::VirtioMmioInterruptMode; use virtio::resolve::ResolvedVirtioDevice; #[cfg(guest_arch = "x86_64")] use vm_loader::InitialLoad; +use vm_topology::memory::MemoryLayout; use vmcore::vm_task::VmTaskDriverSource; use vmm_core::partition_unit::StopGuard; @@ -172,11 +173,7 @@ pub(super) fn load_linux_x86_mptable( .vps_arch() .map(|vp| vp.apic_id) .collect::>(); - let reserved_memory_ranges = [MemoryRange::new( - openvmm_defs::microvm::MICROVM_SHARED_STATUS_PAGE_GPA - ..openvmm_defs::microvm::MICROVM_SHARED_STATUS_PAGE_GPA - + openvmm_defs::microvm::MICROVM_SHARED_STATUS_PAGE_SIZE, - )]; + let reserved_memory_ranges = reserved_memory_ranges(); let kernel_config = crate::worker::vm_loaders::linux::KernelConfig { kernel, initrd, @@ -539,6 +536,16 @@ fn virtio_mmio_config( Ok((start, len, irq, disabled_features, interrupt_mode)) } +/// The guest memory that the microVM e820 map reserves inside RAM besides the +/// ISA hole: the shared virtio interrupt-status page. +fn reserved_memory_ranges() -> [MemoryRange; 1] { + [MemoryRange::new( + openvmm_defs::microvm::MICROVM_SHARED_STATUS_PAGE_GPA + ..openvmm_defs::microvm::MICROVM_SHARED_STATUS_PAGE_GPA + + openvmm_defs::microvm::MICROVM_SHARED_STATUS_PAGE_SIZE, + )] +} + /// Assigns the fixed virtio-mmio slots of the microVM profile to devices, in /// device order. pub(super) struct VirtioMmioSlots<'a> { @@ -546,22 +553,46 @@ pub(super) struct VirtioMmioSlots<'a> { sandbox_block_index: usize, filesystem_index: usize, chipset_mmio: ChipsetMmioRanges, + mem_layout: &'a MemoryLayout, + /// The RAM ranges of the e820 map, computed for the first device. + dma_ranges: Option>, } impl<'a> VirtioMmioSlots<'a> { pub(super) fn new( sandbox_blocks: &'a [openvmm_defs::microvm::MicrovmSandboxBlockConfig], chipset_mmio: ChipsetMmioRanges, + mem_layout: &'a MemoryLayout, ) -> Self { Self { sandbox_blocks, sandbox_block_index: 0, filesystem_index: 0, chipset_mmio, + mem_layout, + dma_ranges: None, + } + } + + /// Returns the guest memory that a microVM device may access with DMA: + /// the RAM ranges of the e820 map, which leave out the shared + /// interrupt-status page, the ISA hole, and the MMIO aperture. After a + /// restore, they include the RAM that the restore adds. + fn dma_ranges(&mut self) -> anyhow::Result<&[MemoryRange]> { + if self.dma_ranges.is_none() { + self.dma_ranges = Some( + loader::linux::microvm::mptable_ram_ranges( + self.mem_layout, + &reserved_memory_ranges(), + ) + .context("failed to find the RAM ranges of the microVM e820 map")?, + ); } + Ok(self.dma_ranges.as_deref().unwrap_or_default()) } - /// Adds a virtio-mmio device at its fixed microVM slot. + /// Adds a virtio-mmio device at its fixed microVM slot, restricting its + /// DMA to the RAM ranges of the e820 map. pub(super) fn add_device( &mut self, chipset_builder: &vmotherboard::ChipsetBuilder<'_>, @@ -578,10 +609,15 @@ impl<'a> VirtioMmioSlots<'a> { &mut self.filesystem_index, self.chipset_mmio, )?; + let dma_ranges = self + .dma_ranges()? + .iter() + .map(|range| range.start()..range.end()) + .collect::>(); let id = format!("{id}-{mmio_start}"); let gm = gm.clone(); chipset_builder.arc_mutex_device(id).try_add(|services| { - VirtioMmioDevice::new_with_disabled_features_and_interrupt_mode( + let mut device = VirtioMmioDevice::new_with_disabled_features_and_interrupt_mode( device.0, &driver_source.simple(), gm, @@ -592,6 +628,11 @@ impl<'a> VirtioMmioSlots<'a> { disabled_features, interrupt_mode, ) + .context("failed to create the virtio-mmio transport")?; + device + .restrict_dma(dma_ranges) + .context("failed to restrict device DMA to RAM")?; + anyhow::Ok(device) })?; Ok(()) } diff --git a/vm/devices/virtio/virtio/src/queue.rs b/vm/devices/virtio/virtio/src/queue.rs index 0edf7692e3d..2d0b195e4f7 100644 --- a/vm/devices/virtio/virtio/src/queue.rs +++ b/vm/devices/virtio/virtio/src/queue.rs @@ -103,6 +103,8 @@ pub struct QueueState { pub enum QueueError { #[error("error accessing queue memory")] Memory(#[source] GuestMemoryError), + #[error("a descriptor addresses memory that the device may not access")] + BufferNotAdmitted(#[source] GuestMemoryError), #[error("an indirect descriptor had the indirect flag set")] DoubleIndirect, #[error("a descriptor chain is too long or has a cycle")] @@ -539,6 +541,15 @@ impl QueueCoreGetWork { Ok(descriptor) } + /// Fails unless the device memory admits the whole buffer of + /// `descriptor`, so that the device reads and writes none of a buffer that + /// lies even partly outside it. + fn check_admitted(&self, descriptor: &QueueDescriptor) -> Result<(), QueueError> { + self.mem + .check_admitted(descriptor.address, descriptor.length.into()) + .map_err(QueueError::BufferNotAdmitted) + } + fn size(&self) -> u16 { self.queue_size } @@ -696,6 +707,7 @@ impl<'a> DescriptorChain<'a> { if self.indirect_queue.is_some() { return Err(QueueError::DoubleIndirect); } + self.queue.check_admitted(&descriptor)?; let indirect_queue = self.indirect_queue.insert( self.queue .mem @@ -708,6 +720,8 @@ impl<'a> DescriptorChain<'a> { self.queue .descriptor(indirect_queue, 0, Some(indirect_len))? }; + // Refuse the chain before the device reads or writes any of it. + self.queue.check_admitted(&descriptor)?; self.num_read += 1; self.descriptor_index = descriptor.next; diff --git a/vm/devices/virtio/virtio/src/tests.rs b/vm/devices/virtio/virtio/src/tests.rs index 2abcdc69dac..7b3ea5025fc 100644 --- a/vm/devices/virtio/virtio/src/tests.rs +++ b/vm/devices/virtio/virtio/src/tests.rs @@ -6,6 +6,7 @@ #![expect(unsafe_code)] #![cfg(test)] +mod dma; mod interrupt; mod saved_state; @@ -82,7 +83,7 @@ const VIRTIO_ACKNOWLEDGE: u32 = 1; const VIRTIO_DRIVER: u32 = 2; const VIRTIO_DRIVER_OK: u32 = 4; const VIRTIO_FEATURES_OK: u32 = 8; -const _VIRTIO_DEVICE_NEEDS_RESET: u32 = 0x40; +const VIRTIO_DEVICE_NEEDS_RESET: u32 = 0x40; const _VIRTIO_FAILED: u32 = 0x80; async fn must_recv_in_timeout( diff --git a/vm/devices/virtio/virtio/src/tests/dma.rs b/vm/devices/virtio/virtio/src/tests/dma.rs new file mode 100644 index 00000000000..ff9d5fef589 --- /dev/null +++ b/vm/devices/virtio/virtio/src/tests/dma.rs @@ -0,0 +1,543 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +//! Tests for devices whose DMA is restricted to the RAM ranges of the microVM +//! e820 map: a device refuses a buffer or a ring in the shared +//! interrupt-status page, in the ISA hole, or across the MMIO aperture, +//! without reading or writing any of it, and the transport marks the device as +//! needing reset. + +use super::*; +use crate::transport::VirtioMmioInterruptMode; +use std::ops::Range; +use test_with_tracing::test; + +const PAGE: u64 = 0x1000; +/// The shared interrupt-status page of the microVM. +const STATUS_PAGE: u64 = 0x3_0000; +/// The legacy PC range from 640 KiB to 1 MiB. +const ISA_HOLE: Range = 0xa_0000..0x10_0000; +/// The fixed 3-to-4-GiB MMIO aperture. +const APERTURE: Range = 0xc000_0000..0x1_0000_0000; +/// The byte that guest memory holds until a device writes it. +const UNTOUCHED: u8 = 0x5a; +/// The byte that [`ReportingDevice`] writes to each writable buffer. +const WRITTEN: u8 = 0xee; + +/// The RAM ranges of the e820 map of a microVM with 4 GiB of RAM. +fn microvm_ram() -> Vec> { + vec![ + 0..STATUS_PAGE, + STATUS_PAGE + PAGE..ISA_HOLE.start, + ISA_HOLE.end..APERTURE.start, + APERTURE.end..APERTURE.end + 0x4000_0000, + ] +} + +/// Buffers that lie in, or cross into, the shared interrupt-status page, the +/// ISA hole, or the aperture, as an address and a length. +const REFUSED_BUFFERS: [(u64, u32); 6] = [ + (STATUS_PAGE, 4), + (STATUS_PAGE - 8, 16), + (ISA_HOLE.start, PAGE as u32), + (ISA_HOLE.end - 8, 16), + (APERTURE.start - 8, 16), + (APERTURE.start, 8), +]; + +/// Buffers that lie wholly inside RAM next to the ranges above. +const ADMITTED_BUFFERS: [(u64, u32); 4] = [ + (STATUS_PAGE - PAGE, PAGE as u32), + (STATUS_PAGE + PAGE, 8), + (ISA_HOLE.end, PAGE as u32), + (APERTURE.start - PAGE, PAGE as u32), +]; + +/// Maps the guest memory around the reserved ranges, so that a test can tell +/// which bytes a device wrote. The status page holds a zero shared +/// interrupt-status word. +fn map_memory_around_reserved_ranges(test_mem: &VirtioTestMemoryAccess) { + let untouched = [UNTOUCHED; PAGE as usize]; + for page in [ + STATUS_PAGE - PAGE, + STATUS_PAGE + PAGE, + ISA_HOLE.start, + ISA_HOLE.end - PAGE, + ISA_HOLE.end, + APERTURE.start - PAGE, + APERTURE.start, + ] { + test_mem.modify_memory_map(page, &untouched, true); + } + test_mem.modify_memory_map(STATUS_PAGE, &0u32.to_le_bytes(), true); + test_mem.modify_memory_map(STATUS_PAGE + 4, &untouched[4..], true); +} + +fn read_memory(test_mem: &VirtioTestMemoryAccess, address: u64, len: usize) -> Vec { + let mut memory_map = test_mem.memory_map.lock(); + let (_, data) = memory_map.get(address, len).unwrap(); + data.to_vec() +} + +/// Asserts that no device wrote `address..address + length`, apart from the +/// shared interrupt-status word, which the transport updates. +fn assert_untouched(test_mem: &VirtioTestMemoryAccess, address: u64, length: u32) { + for gpa in address..address + u64::from(length) { + if !(STATUS_PAGE..STATUS_PAGE + 4).contains(&gpa) { + assert_eq!(read_memory(test_mem, gpa, 1), [UNTOUCHED], "{gpa:#x}"); + } + } +} + +/// Makes the first free descriptor of queue 0 available as a single buffer. +fn post_buffer( + test_mem: &VirtioTestMemoryAccess, + guest: &mut VirtioTestGuest, + address: u64, + length: u32, + flags: DescriptorFlags, +) { + guest.add_to_avail_queue(0); + let descriptor = guest.get_queue_descriptor(0, 0); + test_mem.modify_memory_map(descriptor, &address.to_le_bytes(), false); + test_mem.modify_memory_map(descriptor + 8, &length.to_le_bytes(), false); + test_mem.modify_memory_map(descriptor + 12, &u16::from(flags).to_le_bytes(), false); +} + +/// Returns queue 0 of `guest` over its memory restricted to the microVM RAM +/// ranges, and the receiver of the refusals of that memory. +fn new_restricted_queue( + driver: &DefaultDriver, + guest: &VirtioTestGuest, + params: QueueParams, +) -> ( + Result, + std::sync::mpsc::Receiver<(u64, u64)>, +) { + let (refusals, refused) = std::sync::mpsc::channel(); + let mem = guest + .mem() + .restrict( + microvm_ram(), + Some(Arc::new(move |gpa, len| { + let _ = refusals.send((gpa, len)); + })), + ) + .unwrap(); + let queue = VirtioQueue::new( + guest.queue_features(), + params, + mem, + Interrupt::null(), + PolledWait::new(driver, Event::new()).unwrap(), + None, + ); + (queue, refused) +} + +#[async_test] +async fn queue_refuses_buffers_outside_ram(driver: DefaultDriver) { + for (address, length) in REFUSED_BUFFERS { + let test_mem = VirtioTestMemoryAccess::new(); + let mut guest = VirtioTestGuest::new_split(&driver, &test_mem, 1, 4, false); + let (queue, refused) = new_restricted_queue(&driver, &guest, guest.queue_params(0)); + let mut queue = queue.unwrap(); + post_buffer( + &test_mem, + &mut guest, + address, + length, + DescriptorFlags::new().with_write(true), + ); + + let Err(error) = queue.try_next() else { + panic!("the buffer at {address:#x} must be refused"); + }; + assert!( + matches!(queue_error(&error), Some(QueueError::BufferNotAdmitted(_))), + "{error:?}" + ); + assert_eq!( + refused.try_iter().collect::>(), + [(address, length.into())] + ); + // The refused chain is never consumed, so the queue stays retired. + assert!(matches!(queue.try_next(), Ok(None))); + } + + for (address, length) in ADMITTED_BUFFERS { + let test_mem = VirtioTestMemoryAccess::new(); + let mut guest = VirtioTestGuest::new_split(&driver, &test_mem, 1, 4, false); + let (queue, refused) = new_restricted_queue(&driver, &guest, guest.queue_params(0)); + let mut queue = queue.unwrap(); + post_buffer( + &test_mem, + &mut guest, + address, + length, + DescriptorFlags::new(), + ); + + let work = queue.try_next().unwrap().unwrap(); + assert_eq!(work.payload.len(), 1); + assert_eq!(work.payload[0].address, address); + assert_eq!(work.payload[0].length, length); + assert!(refused.try_recv().is_err()); + queue.complete(work, 0); + } +} + +#[async_test] +async fn queue_refuses_indirect_tables_outside_ram(driver: DefaultDriver) { + const TABLE: u64 = 0x9000; + + // A table in the ISA hole, and a table in RAM whose only buffer is the + // shared interrupt-status word. + for (table, refusal) in [ + (ISA_HOLE.start, (ISA_HOLE.start, 16)), + (TABLE, (STATUS_PAGE, 4)), + ] { + let test_mem = VirtioTestMemoryAccess::new(); + let mut entry = Vec::new(); + entry.extend_from_slice(&STATUS_PAGE.to_le_bytes()); + entry.extend_from_slice(&4u32.to_le_bytes()); + entry.extend_from_slice(&u16::from(DescriptorFlags::new().with_write(true)).to_le_bytes()); + entry.extend_from_slice(&0u16.to_le_bytes()); + test_mem.modify_memory_map(TABLE, &entry, false); + let mut guest = VirtioTestGuest::new_split(&driver, &test_mem, 1, 4, false); + let (queue, refused) = new_restricted_queue(&driver, &guest, guest.queue_params(0)); + let mut queue = queue.unwrap(); + post_buffer( + &test_mem, + &mut guest, + table, + 16, + DescriptorFlags::new().with_indirect(true), + ); + + let Err(error) = queue.try_next() else { + panic!("the indirect table at {table:#x} must be refused"); + }; + assert!( + matches!(queue_error(&error), Some(QueueError::BufferNotAdmitted(_))), + "{error:?}" + ); + assert_eq!(refused.try_iter().collect::>(), [refusal]); + } +} + +#[async_test] +async fn queue_refuses_rings_outside_ram(driver: DefaultDriver) { + let test_mem = VirtioTestMemoryAccess::new(); + let guest = VirtioTestGuest::new_split(&driver, &test_mem, 1, 4, false); + let params = guest.queue_params(0); + for params in [ + QueueParams { + desc_addr: STATUS_PAGE, + ..params + }, + QueueParams { + avail_addr: ISA_HOLE.end - 8, + ..params + }, + QueueParams { + used_addr: APERTURE.start - 8, + ..params + }, + ] { + let (queue, refused) = new_restricted_queue(&driver, &guest, params); + assert!(matches!(queue, Err(QueueError::Memory(_)))); + assert_eq!(refused.try_iter().count(), 1); + } +} + +/// What a [`ReportingDevice`] queue saw. +#[derive(Debug, PartialEq)] +enum Report { + /// A chain, by the address of its first buffer. + Work(u64), + /// The error that retired the queue, and whether the device memory refused + /// a buffer. + Error { refused: bool }, +} + +/// A device that fills each writable buffer with [`WRITTEN`], completes the +/// chain, and reports it, and that reports the error that retires its queue. +#[derive(InspectMut)] +#[inspect(skip)] +struct ReportingDevice { + driver: vmcore::vm_task::VmTaskDriver, + reports: mesh::Sender, + worker: Option>, +} + +struct ReportingTask { + reports: mesh::Sender, +} + +struct ReportingQueue { + queue: VirtioQueue, + mem: GuestMemory, +} + +impl VirtioDevice for ReportingDevice { + fn traits(&self) -> DeviceTraits { + DeviceTraits { + device_id: VirtioDeviceType::CONSOLE, + device_features: VirtioDeviceFeatures::new().with_bank(0, VIRTIO_F_RING_INDIRECT_DESC), + max_queues: 1, + device_register_length: 0, + ..Default::default() + } + } + + async fn read_registers_u32(&mut self, _offset: u16) -> u32 { + 0 + } + + async fn write_registers_u32(&mut self, _offset: u16, _val: u32) {} + + async fn start_queue( + &mut self, + _idx: u16, + resources: QueueResources, + features: &VirtioDeviceFeatures, + initial_state: Option, + ) -> anyhow::Result<()> { + let queue_event = PolledWait::new(&self.driver, resources.event)?; + let queue = VirtioQueue::new( + *features, + resources.params, + resources.guest_memory.clone(), + resources.notify, + queue_event, + initial_state, + )?; + let mut worker = TaskControl::new(ReportingTask { + reports: self.reports.clone(), + }); + worker.insert( + self.driver.clone(), + "virtio-reporting-queue", + ReportingQueue { + queue, + mem: resources.guest_memory, + }, + ); + worker.start(); + self.worker = Some(worker); + Ok(()) + } + + async fn stop_queue(&mut self, _idx: u16) -> Option { + let mut worker = self.worker.take()?; + worker.stop().await; + Some(worker.remove().queue.queue_state()) + } + + async fn reset(&mut self) { + self.worker = None; + } +} + +impl AsyncRun for ReportingTask { + async fn run( + &mut self, + stop: &mut StopTask<'_>, + state: &mut ReportingQueue, + ) -> Result<(), Cancelled> { + loop { + let work = stop.until_stopped(state.queue.next()).await?; + match work.expect("a virtio queue stream never ends") { + Ok(work) => { + let length = work.get_payload_length(true); + work.write(&state.mem, &vec![WRITTEN; length as usize]) + .unwrap(); + self.reports.send(Report::Work(work.payload[0].address)); + state.queue.complete(work, length as u32); + } + Err(error) => self.reports.send(Report::Error { + refused: matches!(queue_error(&error), Some(QueueError::BufferNotAdmitted(_))), + }), + } + } + } +} + +/// Returns a device whose DMA is restricted to the microVM RAM ranges, with +/// its shared interrupt-status word at the start of the status page, and the +/// receiver of its reports. +fn new_restricted_mmio_device( + driver: &DefaultDriver, + test_mem: &Arc, +) -> (VirtioMmioDevice, mesh::Receiver) { + let (reports, reports_recv) = mesh::channel(); + let driver_source = VmTaskDriverSource::new(SingleDriverBackend::new(driver.clone())); + let mut dev = VirtioMmioDevice::new_with_disabled_features_and_interrupt_mode( + Box::new(ReportingDevice { + driver: driver_source.simple(), + reports, + worker: None, + }), + &driver_source.simple(), + GuestMemory::new("test", test_mem.clone()), + LineInterrupt::detached(), + None, + 0, + 0x1000, + 0, + VirtioMmioInterruptMode::SharedStatus { + status_gpa: STATUS_PAGE, + }, + ) + .unwrap(); + dev.restrict_dma(microvm_ram()).unwrap(); + (dev, reports_recv) +} + +/// Polls the device until its status register reads `status`. +async fn wait_for_status(dev: &mut VirtioMmioDevice, status: u32) { + for _ in 0..1000 { + if dev.read_u32(112) == status { + return; + } + yield_and_poll_device(dev).await; + } + panic!( + "device status {:#x} never became {status:#x}", + dev.read_u32(112) + ); +} + +/// Resets the device and checks that the reset clears its needing reset and +/// its shared interrupt-status word. +async fn reset_device(test_mem: &VirtioTestMemoryAccess, dev: &mut VirtioMmioDevice) { + dev.write_u32(112, 0); + wait_for_status(dev, 0).await; + assert_eq!(test_mem.memory_map_get_u32(STATUS_PAGE), 0); +} + +#[async_test] +async fn mmio_device_refuses_buffers_outside_ram_and_needs_reset(driver: DefaultDriver) { + for (address, length) in REFUSED_BUFFERS { + let test_mem = VirtioTestMemoryAccess::new(); + map_memory_around_reserved_ranges(&test_mem); + let mut guest = VirtioTestGuest::new_split(&driver, &test_mem, 1, 4, false); + let (mut dev, mut reports) = new_restricted_mmio_device(&driver, &test_mem); + guest + .setup_chipset_device(&mut dev, guest.queue_features()) + .await; + let status = dev.read_u32(112); + + post_buffer( + &test_mem, + &mut guest, + address, + length, + DescriptorFlags::new().with_write(true), + ); + dev.write_u32(80, 0); + assert_eq!( + must_recv_in_timeout(&mut reports, Duration::from_secs(5)).await, + Report::Error { refused: true } + ); + + // The device marks itself as needing reset and notifies the driver, + // which is the only change to the status page. The device wrote none + // of the buffer, not even the part in RAM. + wait_for_status(&mut dev, status | VIRTIO_DEVICE_NEEDS_RESET).await; + assert_eq!( + test_mem.memory_map_get_u32(STATUS_PAGE), + VIRTIO_MMIO_INTERRUPT_STATUS_CONFIG_CHANGE + ); + assert_untouched(&test_mem, STATUS_PAGE, PAGE as u32); + assert_untouched(&test_mem, address, length); + + reset_device(&test_mem, &mut dev).await; + assert!(reports.try_recv().is_err()); + } +} + +#[async_test] +async fn mmio_device_writes_buffers_inside_ram(driver: DefaultDriver) { + for (address, length) in ADMITTED_BUFFERS { + let test_mem = VirtioTestMemoryAccess::new(); + map_memory_around_reserved_ranges(&test_mem); + let mut guest = VirtioTestGuest::new_split(&driver, &test_mem, 1, 4, false); + let (mut dev, mut reports) = new_restricted_mmio_device(&driver, &test_mem); + guest + .setup_chipset_device(&mut dev, guest.queue_features()) + .await; + let status = dev.read_u32(112); + + post_buffer( + &test_mem, + &mut guest, + address, + length, + DescriptorFlags::new().with_write(true), + ); + dev.write_u32(80, 0); + assert_eq!( + must_recv_in_timeout(&mut reports, Duration::from_secs(5)).await, + Report::Work(address) + ); + assert!( + read_memory(&test_mem, address, length as usize) + .iter() + .all(|&byte| byte == WRITTEN) + ); + yield_and_poll_device(&mut dev).await; + assert_eq!(dev.read_u32(112), status); + assert_eq!( + test_mem.memory_map_get_u32(STATUS_PAGE) & VIRTIO_MMIO_INTERRUPT_STATUS_CONFIG_CHANGE, + 0 + ); + + reset_device(&test_mem, &mut dev).await; + } +} + +#[async_test] +async fn mmio_device_refuses_rings_outside_ram_and_needs_reset(driver: DefaultDriver) { + const QUEUE_SIZE: u16 = 4; + + for (desc_addr, avail_addr, used_addr) in [ + (STATUS_PAGE, 0x2000, 0x3000), + (0x1000, ISA_HOLE.start, 0x3000), + (0x1000, 0x2000, APERTURE.start - 8), + ] { + let test_mem = VirtioTestMemoryAccess::new(); + map_memory_around_reserved_ranges(&test_mem); + let guest = VirtioTestGuest::new_split(&driver, &test_mem, 1, QUEUE_SIZE, false); + let (mut dev, mut reports) = new_restricted_mmio_device(&driver, &test_mem); + let features = guest.queue_features(); + + dev.write_u32(112, VIRTIO_ACKNOWLEDGE); + dev.write_u32(112, VIRTIO_DRIVER); + dev.write_u32(36, 0); + dev.write_u32(32, features.bank(0)); + dev.write_u32(36, 1); + dev.write_u32(32, features.bank(1)); + dev.write_u32(112, VIRTIO_FEATURES_OK); + dev.write_u32(48, 0); + dev.write_u32(56, QUEUE_SIZE.into()); + dev.write_u32(128, desc_addr as u32); + dev.write_u32(132, (desc_addr >> 32) as u32); + dev.write_u32(144, avail_addr as u32); + dev.write_u32(148, (avail_addr >> 32) as u32); + dev.write_u32(160, used_addr as u32); + dev.write_u32(164, (used_addr >> 32) as u32); + dev.write_u32(68, 1); + dev.write_u32(112, VIRTIO_DRIVER_OK); + + // The queue does not start, so DRIVER_OK fails and the device keeps + // only its needing reset. Without DRIVER_OK there is no notification. + wait_for_status(&mut dev, VIRTIO_DEVICE_NEEDS_RESET).await; + assert_eq!(test_mem.memory_map_get_u32(STATUS_PAGE), 0); + assert_untouched(&test_mem, STATUS_PAGE, PAGE as u32); + assert!(reports.try_recv().is_err()); + + reset_device(&test_mem, &mut dev).await; + } +} diff --git a/vm/devices/virtio/virtio/src/transport/core.rs b/vm/devices/virtio/virtio/src/transport/core.rs index 937f6a8095b..4f7fd7c510c 100644 --- a/vm/devices/virtio/virtio/src/transport/core.rs +++ b/vm/devices/virtio/virtio/src/transport/core.rs @@ -19,14 +19,20 @@ use crate::queue::QueueState; use crate::spec::VirtioDeviceFeatures; use crate::spec::VirtioDeviceStatus; use chipset_device::io::deferred::DeferredWrite; +use futures::task::AtomicWaker; use guestmem::DoorbellRegistration; use guestmem::GuestMemory; +use guestmem::RefusalHook; +use guestmem::RestrictError; use inspect::Inspect; use mesh::rpc::Rpc; use mesh::rpc::RpcSend; use pal_async::task::Spawn; use pal_async::task::Task; +use std::ops::Range; use std::sync::Arc; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; use std::task::Poll; use vmcore::interrupt::Interrupt; use vmcore::save_restore::RestoreError; @@ -64,6 +70,31 @@ pub(crate) trait TransportOps: Send { fn doorbell_region(&mut self) -> Option<(u64, u32)>; } +/// Latches a device access that the device memory refused, until the +/// transport marks the device as needing reset. +#[derive(Default)] +struct DmaRefusal { + refused: AtomicBool, + waker: AtomicWaker, +} + +impl DmaRefusal { + fn refuse(&self, gpa: u64, len: u64) { + if !self.refused.swap(true, Ordering::SeqCst) { + tracelimit::warn_ratelimited!( + gpa, + len, + "virtio device addressed memory outside its DMA ranges, marking it as needing reset" + ); + self.waker.wake(); + } + } + + fn take(&self) -> bool { + self.refused.swap(false, Ordering::SeqCst) + } +} + /// State shared by both the PCI and MMIO virtio transports. #[derive(Inspect)] pub(crate) struct VirtioTransportCore { @@ -93,6 +124,10 @@ pub(crate) struct VirtioTransportCore { pub restore: restore::TransportRestore, #[inspect(skip)] pub guest_memory: GuestMemory, + /// The refusals of the device memory, when [`Self::restrict_dma`] + /// restricted it. + #[inspect(rename = "dma_restricted", with = "Option::is_some")] + dma_refusal: Option>, #[inspect(with = "Option::is_some")] pub pending_status_deferred: Option, #[inspect(with = "Vec::len")] @@ -159,11 +194,57 @@ impl VirtioTransportCore { supports_save_restore, restore, guest_memory, + dma_refusal: None, pending_status_deferred: None, stalled_io: Vec::new(), }) } + /// Restricts the DMA of the device to `ranges` of guest memory. + /// + /// The device memory then refuses a queue whose rings, or a descriptor + /// whose buffer, does not lie wholly inside the ranges: the device reads + /// and writes none of it, and the transport marks the device as needing + /// reset, sending a configuration-change notification if the driver has + /// set `DRIVER_OK`. A refused ring fails `DRIVER_OK` and leaves only + /// `DEVICE_NEEDS_RESET` set. Resetting the device clears the mark. + /// + /// The transport itself keeps the unrestricted memory, so the ranges need + /// not hold transport structures such as a shared interrupt-status word. + /// Call this before the guest starts the device. + pub fn restrict_dma( + &mut self, + ranges: impl IntoIterator>, + ) -> Result<(), RestrictError> { + let refusal = Arc::new(DmaRefusal::default()); + let on_refused: RefusalHook = { + let refusal = refusal.clone(); + Arc::new(move |gpa, len| refusal.refuse(gpa, len)) + }; + self.guest_memory = self.guest_memory.restrict(ranges, Some(on_refused))?; + self.dma_refusal = Some(refusal); + Ok(()) + } + + /// Takes the latched refusal of the device memory, if any. + fn take_dma_refusal(&self) -> bool { + self.dma_refusal + .as_ref() + .is_some_and(|refusal| refusal.take()) + } + + /// Sets `DEVICE_NEEDS_RESET`, notifying a driver that has set + /// `DRIVER_OK`, as virtio 1.2 section 2.1.2 requires. + fn mark_needs_reset(&mut self, ops: &mut dyn TransportOps) { + if self.device_status.device_needs_reset() { + return; + } + self.device_status.set_device_needs_reset(true); + if self.device_status.driver_ok() { + ops.signal_config_change(); + } + } + /// Bump config_generation and signal a config-change interrupt if /// the device is in DRIVER_OK state. pub fn update_config_generation(&mut self, ops: &mut dyn TransportOps) { @@ -208,6 +289,7 @@ impl VirtioTransportCore { stalled_io, // Reset below. + dma_refusal, doorbells, device_status, config_generation, @@ -223,6 +305,11 @@ impl VirtioTransportCore { stalled_io.clear(); restore.reset(); + // The device no longer accesses memory, so a refusal latched before + // the reset does not carry over. + if let Some(refusal) = dma_refusal { + refusal.take(); + } doorbells.clear(); *device_status = VirtioDeviceStatus::new(); *config_generation = 0; @@ -262,7 +349,16 @@ impl VirtioTransportCore { TransportStateResult::EnableComplete(true) => { self.device_status.set_driver_ok(true); } - TransportStateResult::EnableComplete(false) | TransportStateResult::DisableComplete => { + TransportStateResult::EnableComplete(false) => { + // A refused ring fails the enable. Keep the refusal visible + // after the reset that the failure entails. + let refused = self.take_dma_refusal(); + self.reset_status(ops); + if refused { + self.mark_needs_reset(ops); + } + } + TransportStateResult::DisableComplete => { self.reset_status(ops); } } @@ -366,6 +462,15 @@ impl VirtioTransportCore { } self.apply_transport_result(ops, result); } + + if let Some(refusal) = &self.dma_refusal { + refusal.waker.register(cx.waker()); + // An enable or disable in flight applies a refusal when it + // completes. + if !self.state.is_busy() && self.take_dma_refusal() { + self.mark_needs_reset(ops); + } + } } /// `ChangeDeviceState::start()` implementation. @@ -546,6 +651,7 @@ impl VirtioTransportCore { // Verify ephemeral runtime state. assert!(!self.state.is_busy()); self.poll_waker = None; + self.take_dma_refusal(); // Reinstall doorbells. self.doorbells.clear(); diff --git a/vm/devices/virtio/virtio/src/transport/mmio.rs b/vm/devices/virtio/virtio/src/transport/mmio.rs index 3dfd82d35cd..0b1db0d2964 100644 --- a/vm/devices/virtio/virtio/src/transport/mmio.rs +++ b/vm/devices/virtio/virtio/src/transport/mmio.rs @@ -174,6 +174,20 @@ impl VirtioMmioDevice { }) } + /// Restricts the DMA of the device to `ranges` of guest memory. + /// + /// The device refuses a queue whose rings, or a descriptor whose buffer, + /// does not lie wholly inside the ranges: it reads and writes none of it, + /// and the transport marks it as needing reset. The shared + /// interrupt-status word, which the transport updates, need not lie inside + /// the ranges. Call this before the guest starts the device. + pub fn restrict_dma( + &mut self, + ranges: impl IntoIterator>, + ) -> Result<(), guestmem::RestrictError> { + self.core.restrict_dma(ranges) + } + /// Synchronous transport register read for tests. #[cfg(test)] pub(crate) fn read_u32(&mut self, address: u64) -> u32 { diff --git a/vm/loader/src/linux/microvm.rs b/vm/loader/src/linux/microvm.rs index e468aba4840..9aa8dcd2ace 100644 --- a/vm/loader/src/linux/microvm.rs +++ b/vm/loader/src/linux/microvm.rs @@ -448,6 +448,20 @@ where Ok(load_info) } +/// Returns the RAM ranges of the e820 map that [`load_x86_mptable`] builds for +/// `mem_layout` and `reserved_memory_ranges`: the RAM of the layout, less the +/// ISA hole and the reserved ranges. +pub fn mptable_ram_ranges( + mem_layout: &MemoryLayout, + reserved_memory_ranges: &[MemoryRange], +) -> Result, Error> { + Ok(build_mptable_e820(mem_layout, reserved_memory_ranges)? + .into_iter() + .filter(|entry| entry.entry_type == defs::E820_RAM) + .map(|entry| MemoryRange::new(entry.start..entry.end)) + .collect()) +} + #[cfg(test)] mod tests { use super::*; @@ -808,6 +822,28 @@ mod tests { ); } + #[test] + fn mptable_ram_ranges_exclude_status_page_isa_hole_and_mmio_gap() { + let status_page = [MemoryRange::new(0x3_0000..0x3_1000)]; + assert_eq!( + mptable_ram_ranges(&make_mptable_layout(8 * GB), &status_page).unwrap(), + [ + MemoryRange::new(0..0x3_0000), + MemoryRange::new(0x3_1000..MPTABLE_ISA_HOLE_BASE), + MemoryRange::new(MPTABLE_ISA_HOLE_END..MPTABLE_MMIO_GAP_BASE), + MemoryRange::new(MPTABLE_MMIO_GAP_END..9 * GB), + ] + ); + assert_eq!( + mptable_ram_ranges(&make_mptable_layout(256 * MB), &status_page).unwrap(), + [ + MemoryRange::new(0..0x3_0000), + MemoryRange::new(0x3_1000..MPTABLE_ISA_HOLE_BASE), + MemoryRange::new(MPTABLE_ISA_HOLE_END..256 * MB), + ] + ); + } + #[test] fn mptable_e820_rejects_invalid_reservations_and_capacity() { let layout = make_mptable_layout(256 * MB); diff --git a/vm/vmcore/guestmem/src/lib.rs b/vm/vmcore/guestmem/src/lib.rs index a18272a0ce2..8ead5ad4849 100644 --- a/vm/vmcore/guestmem/src/lib.rs +++ b/vm/vmcore/guestmem/src/lib.rs @@ -66,6 +66,10 @@ struct NotMapped; #[error("page inaccessible in bitmap")] struct BitmapFailure; +#[derive(Debug, Error)] +#[error("address outside the ranges that this view admits")] +struct NotAdmitted; + /// A trait for a guest memory backing that is fully available via a virtual /// address mapping, as opposed to the fallback functions such as /// [`GuestMemoryAccess::read_fallback`]. @@ -906,6 +910,142 @@ fn create_memory_subrange( )) } +/// A hook that a [restricted](GuestMemory::restrict) view calls with the +/// address and length of each access that it refuses. +pub type RefusalHook = Arc; + +/// An error returned by [`GuestMemory::restrict`]. +#[derive(Debug, Error)] +pub enum RestrictError { + /// A range does not begin and end on page boundaries. + #[error("range {0:#x?} does not begin and end on page boundaries")] + Unaligned(Range), + /// The memory is already a restricted view. + #[error("the memory is already a restricted view")] + AlreadyRestricted, +} + +/// The ranges that a restricted view admits. +struct Restriction { + /// Sorted, disjoint, nonadjacent, nonempty, page-aligned ranges. + ranges: Box<[Range]>, + on_refused: Option, +} + +impl Restriction { + fn admits(&self, gpa: u64, len: u64) -> bool { + if len == 0 { + return true; + } + let Some(end) = gpa.checked_add(len) else { + return false; + }; + // Only the last range that begins at or below `gpa` can hold the + // access. + let i = self.ranges.partition_point(|range| range.start <= gpa); + i > 0 && end <= self.ranges[i - 1].end + } + + fn check(&self, gpa: u64, len: u64) -> Result<(), GuestMemoryBackingError> { + if self.admits(gpa, len) { + return Ok(()); + } + if let Some(on_refused) = &self.on_refused { + on_refused(gpa, len); + } + Err(GuestMemoryBackingError::new( + GuestMemoryErrorKind::OutOfRange, + gpa, + NotAdmitted, + )) + } +} + +/// The backing of a restricted view, which is the backing of the memory that +/// it restricts. +/// +/// The view shares that memory's regions, and so its mappings, and keeps the +/// memory alive. [`GuestMemoryInner::region`] applies the restriction before +/// an access reaches a mapping or this backing. +struct RestrictedAccess { + base: Arc, +} + +impl DynGuestMemoryAccess for RestrictedAccess { + fn subrange( + &self, + _offset: u64, + _len: u64, + _allow_preemptive_locking: bool, + ) -> Result, GuestMemoryBackingError> { + // The default subrange applies the restriction, which a subrange of + // the base memory would bypass. + Ok(None) + } + + fn page_fault( + &self, + address: u64, + len: usize, + write: bool, + bitmap_failure: bool, + ) -> PageFaultAction { + self.base + .imp + .page_fault(address, len, write, bitmap_failure) + } + + unsafe fn read_fallback( + &self, + addr: u64, + dest: *mut u8, + len: usize, + ) -> Result<(), GuestMemoryBackingError> { + // SAFETY: guaranteed by caller. + unsafe { self.base.imp.read_fallback(addr, dest, len) } + } + + unsafe fn write_fallback( + &self, + addr: u64, + src: *const u8, + len: usize, + ) -> Result<(), GuestMemoryBackingError> { + // SAFETY: guaranteed by caller. + unsafe { self.base.imp.write_fallback(addr, src, len) } + } + + fn fill_fallback(&self, addr: u64, val: u8, len: usize) -> Result<(), GuestMemoryBackingError> { + self.base.imp.fill_fallback(addr, val, len) + } + + fn compare_exchange_fallback( + &self, + addr: u64, + current: &mut [u8], + new: &[u8], + ) -> Result { + self.base.imp.compare_exchange_fallback(addr, current, new) + } + + fn expose_va(&self, address: u64, len: u64) -> Result<(), GuestMemoryBackingError> { + self.base.imp.expose_va(address, len) + } + + fn lock_gpns(&self, gpns: &[u64]) -> Result { + self.base.imp.lock_gpns(gpns) + } + + fn unlock_gpns(&self, gpns: &[u64]) { + self.base.imp.unlock_gpns(gpns) + } + + fn sharing(&self) -> Option { + // Sharing would expose all of the base memory. + None + } +} + struct MultiRegionGuestMemoryAccess { imps: Vec>, region_def: RegionDefinition, @@ -1040,6 +1180,8 @@ struct GuestMemoryInner { /// Cached result of [`GuestMemoryAccess::supports_locking`], since it is /// queried on hot zero-copy paths and never changes for a given backing. supports_locking: bool, + /// The ranges that a [restricted](GuestMemory::restrict) view admits. + restriction: Option, imp: T, } @@ -1048,6 +1190,13 @@ impl Debug for GuestMemoryInner { f.debug_struct("GuestMemoryInner") .field("region_def", &self.region_def) .field("regions", &self.regions) + .field( + "admitted", + &self + .restriction + .as_ref() + .map(|restriction| &restriction.ranges), + ) .finish() } } @@ -1251,6 +1400,7 @@ impl GuestMemory { regions, allocated, supports_locking, + restriction: None, }), } } @@ -1335,6 +1485,7 @@ impl GuestMemory { imp, allocated: false, supports_locking, + restriction: None, }; Ok(Self { @@ -1456,11 +1607,88 @@ impl GuestMemory { self.subrange(offset, len, true) } + /// Returns a view of this memory that admits only the accesses that lie + /// wholly inside one of `ranges`. + /// + /// The view refuses any other access before it touches memory: the access + /// fails with [`GuestMemoryErrorKind::OutOfRange`], and the view calls + /// `on_refused`, if set, with the address and length of the access. Every + /// access path of the view and of its subranges applies the restriction, + /// including page locks, so a holder of the view reaches no memory outside + /// `ranges`. The view shares the mappings of this memory, so an admitted + /// access takes the same path that it takes through this memory. + /// + /// The view admits the union of `ranges`, which may be unsorted, overlap, + /// or abut. Each range must begin and end on a page boundary, because a + /// page lock admits a page by its first byte. Empty ranges are ignored. + pub fn restrict( + &self, + ranges: impl IntoIterator>, + on_refused: Option, + ) -> Result { + if self.inner.restriction.is_some() { + return Err(RestrictError::AlreadyRestricted); + } + let mut ranges: Vec<_> = ranges + .into_iter() + .filter(|range| !range.is_empty()) + .collect(); + if let Some(range) = ranges + .iter() + .find(|range| (range.start | range.end) % PAGE_SIZE64 != 0) + { + return Err(RestrictError::Unaligned(range.clone())); + } + ranges.sort_unstable_by_key(|range| range.start); + let mut admitted: Vec> = Vec::with_capacity(ranges.len()); + for range in ranges { + match admitted.last_mut() { + Some(last) if range.start <= last.end => last.end = last.end.max(range.end), + _ => admitted.push(range), + } + } + + let base = &self.inner; + Ok(GuestMemory { + inner: Arc::new(GuestMemoryInner { + region_def: base.region_def, + regions: base.regions.clone(), + debug_name: base.debug_name.clone(), + allocated: false, + supports_locking: base.supports_locking, + restriction: Some(Restriction { + ranges: admitted.into(), + on_refused, + }), + imp: RestrictedAccess { base: base.clone() }, + }), + }) + } + + /// Checks that this memory admits an access to `gpa..gpa + len`, without + /// touching memory. + /// + /// Only a [restricted](Self::restrict) view refuses a range, as it would + /// refuse an access to it, including the call to its refusal hook. The + /// check does not tell whether memory backs the range, which the access + /// itself checks. + pub fn check_admitted(&self, gpa: u64, len: u64) -> Result<(), GuestMemoryError> { + let Some(restriction) = &self.inner.restriction else { + return Ok(()); + }; + self.with_op(Some((gpa, len)), GuestMemoryOperation::Probe, || { + restriction.check(gpa, len) + }) + } + /// Returns the mapping for all of guest memory. /// - /// Returns `None` if there is more than one region or if the memory is not - /// mapped. + /// Returns `None` if there is more than one region, if the memory is not + /// mapped, or if the memory is a [restricted](Self::restrict) view. pub fn full_mapping(&self) -> Option<(*mut u8, usize)> { + if self.inner.restriction.is_some() { + return None; + } if let [region] = self.inner.regions.as_slice() { #[cfg(feature = "bitmap")] if region.bitmaps.is_some() { @@ -2206,6 +2434,9 @@ impl GuestMemoryInner { gpa: u64, len: u64, ) -> Result<(&MemoryRegion, u64, usize), GuestMemoryBackingError> { + if let Some(restriction) = &self.restriction { + restriction.check(gpa, len)?; + } let (index, offset) = self.region_def.region(gpa, len)?; let region = &self.regions[index]; if offset + len > region.len { @@ -2708,4 +2939,103 @@ mod tests { .unwrap(); assert!(!gm.supports_locking()); } + + #[test] + fn test_restrict() { + const P: u64 = PAGE_SIZE64; + let gm = GuestMemory::allocate(8 * PAGE_SIZE); + gm.fill_at(0, 0xaa, 8 * PAGE_SIZE).unwrap(); + let (refusals, refused) = std::sync::mpsc::channel(); + let on_refused: crate::RefusalHook = + Arc::new(move |gpa, len| refusals.send((gpa, len)).unwrap()); + // Pages 0, 1, 3, 6, and 7, with pages 0 and 1 given as abutting + // ranges and pages 6 and 7 as overlapping ones. + let view = gm + .restrict( + [6 * P..8 * P, P..2 * P, 3 * P..4 * P, 0..P, 6 * P..7 * P], + Some(on_refused), + ) + .unwrap(); + + // An access inside an admitted range, even one that spans abutting + // ranges, takes the mapping of the memory. + view.write_at(0, &[1; 2 * PAGE_SIZE]).unwrap(); + view.read_plain::(3 * P).unwrap(); + view.fill_at(6 * P, 2, 2 * PAGE_SIZE).unwrap(); + assert_eq!(gm.read_plain::(2 * P - 1).unwrap(), 1); + assert_eq!(gm.read_plain::(8 * P - 1).unwrap(), 2); + assert!(refused.try_recv().is_err()); + + // An access that lies in or crosses into a page outside the ranges + // fails without touching memory, not even its admitted part. + view.write_at(P, &[3; 2 * PAGE_SIZE]).unwrap_err(); + view.read_plain::(2 * P).unwrap_err(); + view.write_plain::(4 * P, &3).unwrap_err(); + view.compare_exchange(5 * P, 0xaau8, 3).unwrap_err(); + view.fill_at(3 * P, 3, 2 * PAGE_SIZE).unwrap_err(); + assert_eq!(gm.read_plain::(P).unwrap(), 1); + assert_eq!(gm.read_plain::(2 * P).unwrap(), 0xaa); + assert_eq!(gm.read_plain::(3 * P).unwrap(), 0xaa); + assert_eq!(gm.read_plain::(4 * P).unwrap(), 0xaa); + assert_eq!(gm.read_plain::(5 * P).unwrap(), 0xaa); + + // Page locks, subranges, and checks apply the restriction too. + view.lock_gpns(crate::AccessType::Read, false, &[0, 1, 3]) + .unwrap(); + view.lock_gpns(crate::AccessType::Write, false, &[1, 2]) + .unwrap_err(); + view.subrange(P, 2 * P, false).unwrap_err(); + let subrange = view.subrange(6 * P, 2 * P, false).unwrap(); + subrange.write_at(0, &[4; 2 * PAGE_SIZE]).unwrap(); + subrange.read_plain::(2 * P).unwrap_err(); + assert_eq!(gm.read_plain::(6 * P).unwrap(), 4); + view.check_admitted(3 * P, P).unwrap(); + view.check_admitted(2 * P, 0).unwrap(); + view.check_admitted(3 * P, P + 1).unwrap_err(); + view.check_admitted(u64::MAX, 2).unwrap_err(); + assert_eq!( + refused.try_iter().collect::>(), + [ + (P, 2 * P), + (2 * P, 1), + (4 * P, 1), + (5 * P, 1), + (3 * P, 2 * P), + (2 * P, 1), + (P, 2 * P), + (3 * P, P + 1), + (u64::MAX, 2), + ] + ); + + // The view exposes no whole mapping or sharing, while the memory that + // it restricts still admits everything. + assert!(view.full_mapping().is_none()); + assert!(view.sharing().is_none()); + assert!(gm.full_mapping().is_some()); + gm.check_admitted(2 * P, P).unwrap(); + gm.write_plain::(2 * P, &5).unwrap(); + } + + #[test] + fn test_restrict_rejects_invalid_restrictions() { + let gm = GuestMemory::allocate(4 * PAGE_SIZE); + assert!(matches!( + gm.restrict([0..PAGE_SIZE64 + 1], None), + Err(crate::RestrictError::Unaligned(_)) + )); + assert!(matches!( + gm.restrict([1..PAGE_SIZE64], None), + Err(crate::RestrictError::Unaligned(_)) + )); + let view = gm.restrict([0..PAGE_SIZE64], None).unwrap(); + assert!(matches!( + view.restrict([0..PAGE_SIZE64], None), + Err(crate::RestrictError::AlreadyRestricted) + )); + + // A view without ranges admits nothing. + let view = gm.restrict([], None).unwrap(); + view.read_plain::(0).unwrap_err(); + } }