hal.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. use core::{
  2. ptr::NonNull,
  3. sync::atomic::{AtomicUsize, Ordering},
  4. };
  5. use lazy_static::lazy_static;
  6. use log::trace;
  7. use virtio_drivers::{BufferDirection, Hal, PhysAddr, VirtAddr, PAGE_SIZE};
  8. extern "C" {
  9. static dma_region: u8;
  10. }
  11. lazy_static! {
  12. static ref DMA_PADDR: AtomicUsize =
  13. AtomicUsize::new(unsafe { &dma_region as *const u8 as usize });
  14. }
  15. pub struct HalImpl;
  16. impl Hal for HalImpl {
  17. fn dma_alloc(pages: usize, _direction: BufferDirection) -> PhysAddr {
  18. let paddr = DMA_PADDR.fetch_add(PAGE_SIZE * pages, Ordering::SeqCst);
  19. trace!("alloc DMA: paddr={:#x}, pages={}", paddr, pages);
  20. paddr
  21. }
  22. fn dma_dealloc(paddr: PhysAddr, pages: usize) -> i32 {
  23. trace!("dealloc DMA: paddr={:#x}, pages={}", paddr, pages);
  24. 0
  25. }
  26. fn phys_to_virt(paddr: PhysAddr) -> VirtAddr {
  27. paddr
  28. }
  29. fn share(buffer: NonNull<[u8]>, _direction: BufferDirection) -> PhysAddr {
  30. let vaddr = buffer.as_ptr() as *mut u8 as usize;
  31. // Nothing to do, as the host already has access to all memory.
  32. virt_to_phys(vaddr)
  33. }
  34. fn unshare(_paddr: PhysAddr, _buffer: NonNull<[u8]>, _direction: BufferDirection) {
  35. // Nothing to do, as the host already has access to all memory and we didn't copy the buffer
  36. // anywhere else.
  37. }
  38. }
  39. fn virt_to_phys(vaddr: VirtAddr) -> PhysAddr {
  40. vaddr
  41. }