1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Functions to scan the PCI bus for VirtIO device.
16 
17 use alloc::alloc::{alloc_zeroed, dealloc, handle_alloc_error, Layout};
18 use core::{mem::size_of, ptr::NonNull};
19 use log::{debug, info};
20 use virtio_drivers::{
21     device::console::VirtIOConsole,
22     transport::{
23         pci::{bus::PciRoot, PciTransport},
24         DeviceType, Transport,
25     },
26     BufferDirection, Error, Hal, PhysAddr, PAGE_SIZE,
27 };
28 use vmbase::virtio::pci::{self, PciTransportIterator};
29 
30 /// The standard sector size of a VirtIO block device, in bytes.
31 const SECTOR_SIZE_BYTES: usize = 512;
32 
33 /// The size in sectors of the test block device we expect.
34 const EXPECTED_SECTOR_COUNT: usize = 4;
35 
check_pci(pci_root: &mut PciRoot)36 pub fn check_pci(pci_root: &mut PciRoot) {
37     let mut checked_virtio_device_count = 0;
38     let mut block_device_count = 0;
39     let mut socket_device_count = 0;
40     for mut transport in PciTransportIterator::<HalImpl>::new(pci_root) {
41         info!(
42             "Detected virtio PCI device with device type {:?}, features {:#018x}",
43             transport.device_type(),
44             transport.read_device_features(),
45         );
46         match transport.device_type() {
47             DeviceType::Block => {
48                 check_virtio_block_device(transport, block_device_count);
49                 block_device_count += 1;
50                 checked_virtio_device_count += 1;
51             }
52             DeviceType::Console => {
53                 check_virtio_console_device(transport);
54                 checked_virtio_device_count += 1;
55             }
56             DeviceType::Socket => {
57                 check_virtio_socket_device(transport);
58                 socket_device_count += 1;
59                 checked_virtio_device_count += 1;
60             }
61             _ => {}
62         }
63     }
64 
65     assert_eq!(checked_virtio_device_count, 6);
66     assert_eq!(block_device_count, 2);
67     assert_eq!(socket_device_count, 1);
68 }
69 
70 /// Checks the given VirtIO block device.
check_virtio_block_device(transport: PciTransport, index: usize)71 fn check_virtio_block_device(transport: PciTransport, index: usize) {
72     let mut blk = pci::VirtIOBlk::<HalImpl>::new(transport).expect("failed to create blk driver");
73     info!("Found {} KiB block device.", blk.capacity() * SECTOR_SIZE_BYTES as u64 / 1024);
74     match index {
75         0 => {
76             assert_eq!(blk.capacity(), EXPECTED_SECTOR_COUNT as u64);
77             let mut data = [0; SECTOR_SIZE_BYTES * EXPECTED_SECTOR_COUNT];
78             for i in 0..EXPECTED_SECTOR_COUNT {
79                 blk.read_blocks(i, &mut data[i * SECTOR_SIZE_BYTES..(i + 1) * SECTOR_SIZE_BYTES])
80                     .expect("Failed to read block device.");
81             }
82             for (i, chunk) in data.chunks(size_of::<u32>()).enumerate() {
83                 assert_eq!(chunk, &(i as u32).to_le_bytes());
84             }
85             info!("Read expected data from block device.");
86         }
87         1 => {
88             assert_eq!(blk.capacity(), 0);
89             let mut data = [0; SECTOR_SIZE_BYTES];
90             assert_eq!(blk.read_blocks(0, &mut data), Err(Error::IoError));
91         }
92         _ => panic!("Unexpected VirtIO block device index {}.", index),
93     }
94 }
95 
96 /// Checks the given VirtIO socket device.
check_virtio_socket_device(transport: PciTransport)97 fn check_virtio_socket_device(transport: PciTransport) {
98     let socket = pci::VirtIOSocket::<HalImpl>::new(transport)
99         .expect("Failed to create VirtIO socket driver");
100     info!("Found socket device: guest_cid={}", socket.guest_cid());
101 }
102 
103 /// Checks the given VirtIO console device.
check_virtio_console_device(transport: PciTransport)104 fn check_virtio_console_device(transport: PciTransport) {
105     let mut console = VirtIOConsole::<HalImpl, PciTransport>::new(transport)
106         .expect("Failed to create VirtIO console driver");
107     info!("Found console device: {:?}", console.info());
108     for &c in b"Hello VirtIO console\n" {
109         console.send(c).expect("Failed to send character to VirtIO console device");
110     }
111     info!("Wrote to VirtIO console.");
112 }
113 
114 struct HalImpl;
115 
116 /// SAFETY: See the 'Implementation Safety' comments on methods below for how they fulfill the
117 /// safety requirements of the unsafe `Hal` trait.
118 unsafe impl Hal for HalImpl {
119     /// # Implementation Safety
120     ///
121     /// `dma_alloc` ensures the returned DMA buffer is not aliased with any other allocation or
122     /// reference in the program until it is deallocated by `dma_dealloc` by allocating a unique
123     /// block of memory using `alloc_zeroed`, which is guaranteed to allocate valid, unique and
124     /// zeroed memory. We request an alignment of at least `PAGE_SIZE` from `alloc_zeroed`.
dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>)125     fn dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>) {
126         debug!("dma_alloc: pages={}", pages);
127         let layout =
128             Layout::from_size_align(pages.checked_mul(PAGE_SIZE).unwrap(), PAGE_SIZE).unwrap();
129         assert_ne!(layout.size(), 0);
130         // SAFETY: We just checked that the layout has a non-zero size.
131         let vaddr = unsafe { alloc_zeroed(layout) };
132         let vaddr =
133             if let Some(vaddr) = NonNull::new(vaddr) { vaddr } else { handle_alloc_error(layout) };
134         let paddr = virt_to_phys(vaddr);
135         (paddr, vaddr)
136     }
137 
dma_dealloc(paddr: PhysAddr, vaddr: NonNull<u8>, pages: usize) -> i32138     unsafe fn dma_dealloc(paddr: PhysAddr, vaddr: NonNull<u8>, pages: usize) -> i32 {
139         debug!("dma_dealloc: paddr={:#x}, pages={}", paddr, pages);
140         let layout = Layout::from_size_align(pages * PAGE_SIZE, PAGE_SIZE).unwrap();
141         // SAFETY: The memory was allocated by `dma_alloc` above using the same allocator, and the
142         // layout is the same as was used then.
143         unsafe {
144             dealloc(vaddr.as_ptr(), layout);
145         }
146         0
147     }
148 
149     /// # Implementation Safety
150     ///
151     /// The returned pointer must be valid because the `paddr` describes a valid MMIO region, and we
152     /// previously mapped the entire PCI MMIO range. It can't alias any other allocations because
153     /// the PCI MMIO range doesn't overlap with any other memory ranges.
mmio_phys_to_virt(paddr: PhysAddr, _size: usize) -> NonNull<u8>154     unsafe fn mmio_phys_to_virt(paddr: PhysAddr, _size: usize) -> NonNull<u8> {
155         NonNull::new(paddr as _).unwrap()
156     }
157 
share(buffer: NonNull<[u8]>, _direction: BufferDirection) -> PhysAddr158     unsafe fn share(buffer: NonNull<[u8]>, _direction: BufferDirection) -> PhysAddr {
159         let vaddr = buffer.cast();
160         // Nothing to do, as the host already has access to all memory.
161         virt_to_phys(vaddr)
162     }
163 
unshare(_paddr: PhysAddr, _buffer: NonNull<[u8]>, _direction: BufferDirection)164     unsafe fn unshare(_paddr: PhysAddr, _buffer: NonNull<[u8]>, _direction: BufferDirection) {
165         // Nothing to do, as the host already has access to all memory and we didn't copy the buffer
166         // anywhere else.
167     }
168 }
169 
virt_to_phys(vaddr: NonNull<u8>) -> PhysAddr170 fn virt_to_phys(vaddr: NonNull<u8>) -> PhysAddr {
171     vaddr.as_ptr() as _
172 }
173