Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 1 | // Copyright 2023, 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 | //! HAL for the virtio_drivers crate. |
| 16 | |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 17 | use super::pci::PCI_INFO; |
Alice Wang | eade167 | 2023-06-08 14:56:20 +0000 | [diff] [blame] | 18 | use crate::memory::{alloc_shared, dealloc_shared, phys_to_virt, virt_to_phys}; |
| 19 | use crate::util::RangeExt as _; |
Pierre-Clément Tosi | 2d5bc58 | 2023-05-03 11:23:11 +0000 | [diff] [blame] | 20 | use core::alloc::Layout; |
| 21 | use core::mem::size_of; |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 22 | use core::ptr::{copy_nonoverlapping, NonNull}; |
| 23 | use log::trace; |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 24 | use virtio_drivers::{BufferDirection, Hal, PhysAddr, PAGE_SIZE}; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 25 | |
Andrew Walbran | 2b0c7fb | 2023-05-09 12:16:20 +0000 | [diff] [blame] | 26 | /// The alignment to use for the temporary buffers allocated by `HalImpl::share`. There doesn't seem |
| 27 | /// to be any particular alignment required by VirtIO for these, so 16 bytes should be enough to |
| 28 | /// allow appropriate alignment for whatever fields are accessed. `alloc_shared` will increase the |
| 29 | /// alignment to the memory sharing granule size anyway. |
| 30 | const SHARED_BUFFER_ALIGNMENT: usize = size_of::<u128>(); |
| 31 | |
Alice Wang | eade167 | 2023-06-08 14:56:20 +0000 | [diff] [blame] | 32 | /// HAL implementation for the virtio_drivers crate. |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 33 | pub struct HalImpl; |
| 34 | |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 35 | /// SAFETY: See the 'Implementation Safety' comments on methods below for how they fulfill the |
| 36 | /// safety requirements of the unsafe `Hal` trait. |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 37 | unsafe impl Hal for HalImpl { |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 38 | /// # Implementation Safety |
| 39 | /// |
| 40 | /// `dma_alloc` ensures the returned DMA buffer is not aliased with any other allocation or |
Andrew Walbran | 2b0c7fb | 2023-05-09 12:16:20 +0000 | [diff] [blame] | 41 | /// reference in the program until it is deallocated by `dma_dealloc` by allocating a unique |
| 42 | /// block of memory using `alloc_shared`, which is guaranteed to allocate valid and unique |
Andrew Walbran | cf9333e | 2023-05-16 16:00:35 +0000 | [diff] [blame] | 43 | /// memory. We request an alignment of at least `PAGE_SIZE` from `alloc_shared`. We zero the |
| 44 | /// buffer before returning it. |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 45 | fn dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>) { |
Andrew Walbran | cf9333e | 2023-05-16 16:00:35 +0000 | [diff] [blame] | 46 | let layout = dma_layout(pages); |
| 47 | let vaddr = |
| 48 | alloc_shared(layout).expect("Failed to allocate and share VirtIO DMA range with host"); |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 49 | // SAFETY: vaddr points to a region allocated for the caller so is safe to access. |
Andrew Walbran | cf9333e | 2023-05-16 16:00:35 +0000 | [diff] [blame] | 50 | unsafe { core::ptr::write_bytes(vaddr.as_ptr(), 0, layout.size()) }; |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 51 | let paddr = virt_to_phys(vaddr); |
| 52 | (paddr, vaddr) |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 55 | unsafe fn dma_dealloc(_paddr: PhysAddr, vaddr: NonNull<u8>, pages: usize) -> i32 { |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 56 | // SAFETY: Memory was allocated by `dma_alloc` using `alloc_shared` with the same layout. |
Pierre-Clément Tosi | 2d5bc58 | 2023-05-03 11:23:11 +0000 | [diff] [blame] | 57 | unsafe { dealloc_shared(vaddr, dma_layout(pages)) } |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 58 | .expect("Failed to unshare VirtIO DMA range with host"); |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 59 | 0 |
| 60 | } |
| 61 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 62 | /// # Implementation Safety |
| 63 | /// |
Andrew Walbran | 2b0c7fb | 2023-05-09 12:16:20 +0000 | [diff] [blame] | 64 | /// The returned pointer must be valid because the `paddr` describes a valid MMIO region, we |
| 65 | /// check that it is within the PCI MMIO range, and we previously mapped the entire PCI MMIO |
| 66 | /// range. It can't alias any other allocations because we previously validated in |
| 67 | /// `map_mmio_range` that the PCI MMIO range didn't overlap with any other memory ranges. |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 68 | unsafe fn mmio_phys_to_virt(paddr: PhysAddr, size: usize) -> NonNull<u8> { |
Alice Wang | eff5839 | 2023-07-04 13:32:09 +0000 | [diff] [blame] | 69 | let pci_info = PCI_INFO.get().expect("VirtIO HAL used before PCI_INFO was initialized"); |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 70 | let bar_range = { |
| 71 | let start = pci_info.bar_range.start.try_into().unwrap(); |
| 72 | let end = pci_info.bar_range.end.try_into().unwrap(); |
| 73 | |
| 74 | start..end |
| 75 | }; |
| 76 | let mmio_range = paddr..paddr.checked_add(size).expect("PCI MMIO region end overflowed"); |
| 77 | |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 78 | // Check that the region is within the PCI MMIO range that we read from the device tree. If |
| 79 | // not, the host is probably trying to do something malicious. |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 80 | assert!( |
| 81 | mmio_range.is_within(&bar_range), |
| 82 | "PCI MMIO region was outside of expected BAR range.", |
| 83 | ); |
| 84 | |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 85 | phys_to_virt(paddr) |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 88 | unsafe fn share(buffer: NonNull<[u8]>, direction: BufferDirection) -> PhysAddr { |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 89 | let size = buffer.len(); |
| 90 | |
Pierre-Clément Tosi | 2d5bc58 | 2023-05-03 11:23:11 +0000 | [diff] [blame] | 91 | let bounce = alloc_shared(bb_layout(size)) |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 92 | .expect("Failed to allocate and share VirtIO bounce buffer with host"); |
| 93 | let paddr = virt_to_phys(bounce); |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 94 | if direction == BufferDirection::DriverToDevice { |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 95 | let src = buffer.cast::<u8>().as_ptr().cast_const(); |
| 96 | trace!("VirtIO bounce buffer at {bounce:?} (PA:{paddr:#x}) initialized from {src:?}"); |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 97 | // SAFETY: Both regions are valid, properly aligned, and don't overlap. |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 98 | unsafe { copy_nonoverlapping(src, bounce.as_ptr(), size) }; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 99 | } |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 100 | |
| 101 | paddr |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 104 | unsafe fn unshare(paddr: PhysAddr, buffer: NonNull<[u8]>, direction: BufferDirection) { |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 105 | let bounce = phys_to_virt(paddr); |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 106 | let size = buffer.len(); |
| 107 | if direction == BufferDirection::DeviceToDriver { |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 108 | let dest = buffer.cast::<u8>().as_ptr(); |
| 109 | trace!("VirtIO bounce buffer at {bounce:?} (PA:{paddr:#x}) copied back to {dest:?}"); |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 110 | // SAFETY: Both regions are valid, properly aligned, and don't overlap. |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 111 | unsafe { copy_nonoverlapping(bounce.as_ptr(), dest, size) }; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 114 | // SAFETY: Memory was allocated by `share` using `alloc_shared` with the same layout. |
Pierre-Clément Tosi | 2d5bc58 | 2023-05-03 11:23:11 +0000 | [diff] [blame] | 115 | unsafe { dealloc_shared(bounce, bb_layout(size)) } |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame] | 116 | .expect("Failed to unshare and deallocate VirtIO bounce buffer"); |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
Pierre-Clément Tosi | 2d5bc58 | 2023-05-03 11:23:11 +0000 | [diff] [blame] | 119 | |
| 120 | fn dma_layout(pages: usize) -> Layout { |
| 121 | let size = pages.checked_mul(PAGE_SIZE).unwrap(); |
| 122 | Layout::from_size_align(size, PAGE_SIZE).unwrap() |
| 123 | } |
| 124 | |
| 125 | fn bb_layout(size: usize) -> Layout { |
Andrew Walbran | 2b0c7fb | 2023-05-09 12:16:20 +0000 | [diff] [blame] | 126 | Layout::from_size_align(size, SHARED_BUFFER_ALIGNMENT).unwrap() |
Pierre-Clément Tosi | 2d5bc58 | 2023-05-03 11:23:11 +0000 | [diff] [blame] | 127 | } |