1 use crate::arch::mm::kernel_page_flags; 2 3 use crate::arch::MMArch; 4 5 use crate::mm::kernel_mapper::KernelMapper; 6 use crate::mm::page::PageFlags; 7 use crate::mm::{ 8 allocator::page_frame::{ 9 allocate_page_frames, deallocate_page_frames, PageFrameCount, PhysPageFrame, 10 }, 11 MemoryManagementArch, PhysAddr, VirtAddr, 12 }; 13 use core::ptr::NonNull; 14 use virtio_drivers::{BufferDirection, Hal, PAGE_SIZE}; 15 16 pub struct HalImpl; 17 unsafe impl Hal for HalImpl { 18 /// @brief 申请用于DMA的内存页 19 /// @param pages 页数(4k一页) 20 /// @return PhysAddr 获得的内存页的初始物理地址 21 fn dma_alloc( 22 pages: usize, 23 _direction: BufferDirection, 24 ) -> (virtio_drivers::PhysAddr, NonNull<u8>) { 25 let page_num = PageFrameCount::new( 26 ((pages * PAGE_SIZE + MMArch::PAGE_SIZE - 1) / MMArch::PAGE_SIZE).next_power_of_two(), 27 ); 28 unsafe { 29 let (paddr, count) = 30 allocate_page_frames(page_num).expect("VirtIO Impl: alloc page failed"); 31 let virt = MMArch::phys_2_virt(paddr).unwrap(); 32 // 清空这块区域,防止出现脏数据 33 core::ptr::write_bytes(virt.data() as *mut u8, 0, count.data() * MMArch::PAGE_SIZE); 34 35 let dma_flags: PageFlags<MMArch> = PageFlags::mmio_flags(); 36 37 let mut kernel_mapper = KernelMapper::lock(); 38 let kernel_mapper = kernel_mapper.as_mut().unwrap(); 39 let flusher = kernel_mapper 40 .remap(virt, dma_flags) 41 .expect("VirtIO Impl: remap failed"); 42 flusher.flush(); 43 return ( 44 paddr.data(), 45 NonNull::new(MMArch::phys_2_virt(paddr).unwrap().data() as _).unwrap(), 46 ); 47 } 48 } 49 /// @brief 释放用于DMA的内存页 50 /// @param paddr 起始物理地址 pages 页数(4k一页) 51 /// @return i32 0表示成功 52 unsafe fn dma_dealloc( 53 paddr: virtio_drivers::PhysAddr, 54 vaddr: NonNull<u8>, 55 pages: usize, 56 ) -> i32 { 57 let page_count = PageFrameCount::new( 58 ((pages * PAGE_SIZE + MMArch::PAGE_SIZE - 1) / MMArch::PAGE_SIZE).next_power_of_two(), 59 ); 60 61 // 恢复页面属性 62 let vaddr = VirtAddr::new(vaddr.as_ptr() as usize); 63 let mut kernel_mapper = KernelMapper::lock(); 64 let kernel_mapper = kernel_mapper.as_mut().unwrap(); 65 let flusher = kernel_mapper 66 .remap(vaddr, kernel_page_flags(vaddr)) 67 .expect("VirtIO Impl: remap failed"); 68 flusher.flush(); 69 70 unsafe { 71 deallocate_page_frames(PhysPageFrame::new(PhysAddr::new(paddr)), page_count); 72 } 73 return 0; 74 } 75 /// @brief mmio物理地址转换为虚拟地址,不需要使用 76 /// @param paddr 起始物理地址 77 /// @return NonNull<u8> 虚拟地址的指针 78 unsafe fn mmio_phys_to_virt(_paddr: virtio_drivers::PhysAddr, _size: usize) -> NonNull<u8> { 79 NonNull::new((0) as _).unwrap() 80 } 81 /// @brief 与真实物理设备共享 82 /// @param buffer 要共享的buffer _direction:设备到driver或driver到设备 83 /// @return buffer在内存中的物理地址 84 unsafe fn share( 85 buffer: NonNull<[u8]>, 86 _direction: BufferDirection, 87 ) -> virtio_drivers::PhysAddr { 88 let vaddr = VirtAddr::new(buffer.as_ptr() as *mut u8 as usize); 89 //kdebug!("virt:{:x}", vaddr); 90 // Nothing to do, as the host already has access to all memory. 91 return MMArch::virt_2_phys(vaddr).unwrap().data(); 92 } 93 /// @brief 停止共享(让主机可以访问全部内存的话什么都不用做) 94 unsafe fn unshare( 95 _paddr: virtio_drivers::PhysAddr, 96 _buffer: NonNull<[u8]>, 97 _direction: BufferDirection, 98 ) { 99 // Nothing to do, as the host already has access to all memory and we didn't copy the buffer 100 // anywhere else. 101 } 102 } 103