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; |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 18 | use crate::helpers::RangeExt as _; |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 19 | use crate::memory::{alloc_shared, dealloc_shared, phys_to_virt, virt_to_phys}; |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 20 | use core::ptr::{copy_nonoverlapping, NonNull}; |
| 21 | use log::trace; |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 22 | use virtio_drivers::{BufferDirection, Hal, PhysAddr, PAGE_SIZE}; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 23 | |
| 24 | pub struct HalImpl; |
| 25 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 26 | /// Implements the `Hal` trait for `HalImpl`. |
| 27 | /// |
| 28 | /// # Safety |
| 29 | /// |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 30 | /// Callers of this implementatation must follow the safety requirements documented in the `Hal` |
| 31 | /// trait for the unsafe methods. |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 32 | unsafe impl Hal for HalImpl { |
| 33 | /// Allocates the given number of contiguous physical pages of DMA memory for VirtIO use. |
| 34 | /// |
| 35 | /// # Implementation Safety |
| 36 | /// |
| 37 | /// `dma_alloc` ensures the returned DMA buffer is not aliased with any other allocation or |
| 38 | /// reference in the program until it is deallocated by `dma_dealloc` by allocating a unique |
| 39 | /// block of memory using `alloc_shared` and returning a non-null pointer to it that is |
| 40 | /// aligned to `PAGE_SIZE`. |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 41 | fn dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>) { |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 42 | let size = pages * PAGE_SIZE; |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 43 | let vaddr = |
| 44 | alloc_shared(size).expect("Failed to allocate and share VirtIO DMA range with host"); |
| 45 | let paddr = virt_to_phys(vaddr); |
| 46 | (paddr, vaddr) |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 49 | unsafe fn dma_dealloc(_paddr: PhysAddr, vaddr: NonNull<u8>, pages: usize) -> i32 { |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 50 | let size = pages * PAGE_SIZE; |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 51 | // SAFETY - Memory was allocated by `dma_alloc` using `alloc_shared` with the same size. |
| 52 | unsafe { dealloc_shared(vaddr, size) } |
| 53 | .expect("Failed to unshare VirtIO DMA range with host"); |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 54 | 0 |
| 55 | } |
| 56 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 57 | /// Converts a physical address used for MMIO to a virtual address which the driver can access. |
| 58 | /// |
| 59 | /// # Implementation Safety |
| 60 | /// |
| 61 | /// `mmio_phys_to_virt` satisfies the requirement by checking that the mapped memory region |
| 62 | /// is within the PCI MMIO range. |
| 63 | unsafe fn mmio_phys_to_virt(paddr: PhysAddr, size: usize) -> NonNull<u8> { |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 64 | let pci_info = PCI_INFO.get().expect("VirtIO HAL used before PCI_INFO was initialised"); |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 65 | let bar_range = { |
| 66 | let start = pci_info.bar_range.start.try_into().unwrap(); |
| 67 | let end = pci_info.bar_range.end.try_into().unwrap(); |
| 68 | |
| 69 | start..end |
| 70 | }; |
| 71 | let mmio_range = paddr..paddr.checked_add(size).expect("PCI MMIO region end overflowed"); |
| 72 | |
Andrew Walbran | b398fc8 | 2023-01-24 14:45:46 +0000 | [diff] [blame] | 73 | // Check that the region is within the PCI MMIO range that we read from the device tree. If |
| 74 | // not, the host is probably trying to do something malicious. |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 75 | assert!( |
| 76 | mmio_range.is_within(&bar_range), |
| 77 | "PCI MMIO region was outside of expected BAR range.", |
| 78 | ); |
| 79 | |
Andrew Walbran | 272bd7a | 2023-01-24 14:02:36 +0000 | [diff] [blame] | 80 | phys_to_virt(paddr) |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 83 | unsafe fn share(buffer: NonNull<[u8]>, direction: BufferDirection) -> PhysAddr { |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 84 | let size = buffer.len(); |
| 85 | |
| 86 | // TODO: Copy to a pre-shared region rather than allocating and sharing each time. |
| 87 | // Allocate a range of pages, copy the buffer if necessary, and share the new range instead. |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 88 | let bounce = alloc_shared(size) |
| 89 | .expect("Failed to allocate and share VirtIO bounce buffer with host"); |
| 90 | let paddr = virt_to_phys(bounce); |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 91 | if direction == BufferDirection::DriverToDevice { |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 92 | let src = buffer.cast::<u8>().as_ptr().cast_const(); |
| 93 | trace!("VirtIO bounce buffer at {bounce:?} (PA:{paddr:#x}) initialized from {src:?}"); |
| 94 | // SAFETY - Both regions are valid, properly aligned, and don't overlap. |
| 95 | unsafe { copy_nonoverlapping(src, bounce.as_ptr(), size) }; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 96 | } |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 97 | |
| 98 | paddr |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Alice Wang | 63e0d0d | 2023-04-20 14:15:32 +0000 | [diff] [blame] | 101 | unsafe fn unshare(paddr: PhysAddr, buffer: NonNull<[u8]>, direction: BufferDirection) { |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 102 | let bounce = phys_to_virt(paddr); |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 103 | let size = buffer.len(); |
| 104 | if direction == BufferDirection::DeviceToDriver { |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 105 | let dest = buffer.cast::<u8>().as_ptr(); |
| 106 | trace!("VirtIO bounce buffer at {bounce:?} (PA:{paddr:#x}) copied back to {dest:?}"); |
| 107 | // SAFETY - Both regions are valid, properly aligned, and don't overlap. |
| 108 | unsafe { copy_nonoverlapping(bounce.as_ptr(), dest, size) }; |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Pierre-Clément Tosi | 90238c5 | 2023-04-27 17:59:10 +0000 | [diff] [blame^] | 111 | // SAFETY - Memory was allocated by `share` using `alloc_shared` with the same size. |
| 112 | unsafe { dealloc_shared(bounce, size) } |
| 113 | .expect("Failed to unshare and deallocate VirtIO bounce buffer"); |
Andrew Walbran | 848decf | 2022-12-15 14:39:38 +0000 | [diff] [blame] | 114 | } |
| 115 | } |