diff --git a/gpio/src/gpio.rs b/gpio/src/gpio.rs index 0a6f3ca..f14bfb3 100644 --- a/gpio/src/gpio.rs +++ b/gpio/src/gpio.rs @@ -14,7 +14,7 @@ use libgpiod::{ RequestConfig, }; use thiserror::Error as ThisError; -use vm_memory::{Le16, Le32}; +use vm_memory::{ByteValued, Le16, Le32}; type Result = std::result::Result; @@ -79,7 +79,7 @@ const VIRTIO_GPIO_IRQ_TYPE_ALL: u16 = VIRTIO_GPIO_IRQ_TYPE_EDGE_BOTH | VIRTIO_GPIO_IRQ_TYPE_LEVEL_LOW; /// Virtio GPIO Configuration -#[derive(Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, Default, PartialEq)] #[repr(C)] pub(crate) struct VirtioGpioConfig { pub(crate) ngpio: Le16, @@ -87,6 +87,8 @@ pub(crate) struct VirtioGpioConfig { pub(crate) gpio_names_size: Le32, } +unsafe impl ByteValued for VirtioGpioConfig {} + /// Trait that represents an GPIO Device. /// /// This trait is introduced for development purposes only, and should not diff --git a/gpio/src/vhu_gpio.rs b/gpio/src/vhu_gpio.rs index 5f82def..67774f5 100644 --- a/gpio/src/vhu_gpio.rs +++ b/gpio/src/vhu_gpio.rs @@ -27,7 +27,7 @@ use vm_memory::{ use vmm_sys_util::epoll::EventSet; use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; -use crate::gpio::{GpioController, GpioDevice, VirtioGpioConfig, VIRTIO_GPIO_IRQ_TYPE_NONE}; +use crate::gpio::{GpioController, GpioDevice, VIRTIO_GPIO_IRQ_TYPE_NONE}; /// Possible values of the status field const VIRTIO_GPIO_STATUS_OK: u8 = 0x0; @@ -394,11 +394,15 @@ impl VhostUserBackendMut VhostUserProtocolFeatures::MQ | VhostUserProtocolFeatures::CONFIG } - fn get_config(&self, _offset: u32, _size: u32) -> Vec { + fn get_config(&self, offset: u32, size: u32) -> Vec { unsafe { from_raw_parts( - self.controller.get_config() as *const _ as *const _, - size_of::(), + self.controller + .get_config() + .as_slice() + .as_ptr() + .offset(offset as isize) as *const _ as *const _, + size as usize, ) .to_vec() } @@ -1110,13 +1114,16 @@ mod tests { gpio_names_size: From::from(names_size as u32), }; - assert_eq!(backend.get_config(0, 0), unsafe { - from_raw_parts( - &config as *const _ as *const _, - size_of::(), - ) - .to_vec() - }); + assert_eq!( + backend.get_config(0, size_of::() as u32), + unsafe { + from_raw_parts( + &config as *const _ as *const _, + size_of::(), + ) + .to_vec() + } + ); let mem = GuestMemoryAtomic::new( GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), 0x1000)]).unwrap(),