rcore-os / virtio-drivers

VirtIO guest drivers in Rust.
MIT License
209 stars 63 forks source link

The shared buffer address is not in the dma allocation address range. #124

Closed CtrlZ233 closed 7 months ago

CtrlZ233 commented 7 months ago

I implemented Hal trait for VirtioHal and use it to init VirtIONet, However, the address of the input parameter buf of the share interface is not within the range of the previous dma allocated address. I am not sure whether this is the correct behavior.

[dma_alloc] paddr: 0x84256000, vaddr: 0xd6000
[dma_alloc] paddr: 0x85250000, vaddr: 0x10d0000
[share] vaddr: 0xd480

virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers", rev = "a35c6e6" }

qwandor commented 7 months ago

Right, that's expected as callers of the device driver may send data from arbitrary locations in memory, e.g. on the stack. If your platform requires all DMA to be within a previously allocated region (rather than sharing it on demand) then your Hal::share implementation can copy the data (if direction is DriverToDevice or Both) from the buffer to an appropriate allocation and return the address of the copy. Hal::unshare can then copy the result back (if direction is DeviceToDriver or Both) and deallocate the DMA memory.

You can see an implementation of this for protected VMs in AOSP: https://cs.android.com/android/platform/superproject/main/+/main:packages/modules/Virtualization/vmbase/src/virtio/hal.rs