diff --git a/alioth/src/arch/x86_64/intr.rs b/alioth/src/arch/x86_64/intr.rs index f0eb68d1..874ff324 100644 --- a/alioth/src/arch/x86_64/intr.rs +++ b/alioth/src/arch/x86_64/intr.rs @@ -29,16 +29,16 @@ consts! { } consts! { - pub struct TriggerMode(bool) { - EDGE = false; - LEVEL = true; + pub struct TriggerMode(u8) { + EDGE = 0; + LEVEL = 1; } } consts! { - pub struct DestinationMode(bool) { - PHYSICAL = false; - LOGICAL = true; + pub struct DestinationMode(u8) { + PHYSICAL = 0; + LOGICAL = 1; } } @@ -46,7 +46,7 @@ bitfield! { #[derive(Copy, Clone, Default, PartialEq, Eq, Hash)] pub struct MsiAddrLo(u32); impl Debug; - pub mode, set_mode : 2; + pub u8, from into DestinationMode, mode, set_mode : 2, 2; pub redirection, set_redirection : 3; pub remappable, set_remappable : 4; pub u8, virt_dest_id_hi, set_virt_dest_id_hi : 11, 5; @@ -67,7 +67,7 @@ bitfield! { impl Debug; impl new; pub u8, vector, set_vector : 7, 0; - pub u8, from into DeliveryMode, delivery_mode, set_delivery_mode : 11, 8; + pub u8, from into DeliveryMode, delivery_mode, set_delivery_mode : 10, 8; pub u8, level, set_level : 14; - pub trigger_mode, set_trigger_mode : 15; + pub u8, from into TriggerMode, trigger_mode, set_trigger_mode : 15, 15; } diff --git a/alioth/src/arch/x86_64/ioapic.rs b/alioth/src/arch/x86_64/ioapic.rs index 32999f3a..05a97f50 100644 --- a/alioth/src/arch/x86_64/ioapic.rs +++ b/alioth/src/arch/x86_64/ioapic.rs @@ -14,7 +14,7 @@ use bitfield::bitfield; -use crate::arch::x86_64::intr::DeliveryMode; +use crate::arch::x86_64::intr::{DeliveryMode, DestinationMode, TriggerMode}; pub const IOREGSEL: u64 = 0x00; pub const IOWIN: u64 = 0x10; @@ -52,11 +52,11 @@ bitfield! { impl Debug; pub u8, vector, set_vector : 7, 0; pub u8, from into DeliveryMode, delivery_mode, set_delivery_mode : 10, 8; - pub dest_mode, set_dest_mode : 11; + pub u8, from into DestinationMode, dest_mode, set_dest_mode : 11, 11; pub delivery_status, set_delivery_status : 12; pub riority, set_priority : 13; pub irr, set_irr : 14; - pub trigger_mode, set_trigger_mode : 15; + pub u8, from into TriggerMode, trigger_mode, set_trigger_mode : 15, 15; pub masked, set_masked : 16; pub u8, virt_dest_id_hi, set_virt_dest_id_hi : 55, 49; pub u8, dest_id, set_dest_id : 63, 56; diff --git a/alioth/src/arch/x86_64/msr.rs b/alioth/src/arch/x86_64/msr.rs index c21a6e38..1802bfd0 100644 --- a/alioth/src/arch/x86_64/msr.rs +++ b/alioth/src/arch/x86_64/msr.rs @@ -34,7 +34,6 @@ consts! { } bitflags! { - #[derive(Default)] pub struct Efer(u64) { /// SYSCALL enable SCE = 1 << 0; diff --git a/alioth/src/arch/x86_64/reg.rs b/alioth/src/arch/x86_64/reg.rs index b72b1ff3..ffe3bbf3 100644 --- a/alioth/src/arch/x86_64/reg.rs +++ b/alioth/src/arch/x86_64/reg.rs @@ -58,7 +58,6 @@ bitflags! { } bitflags! { - #[derive(Default)] pub struct Cr0(u64) { /// CarryProtected Mode Enable PE = 1 << 0; @@ -86,7 +85,6 @@ bitflags! { } bitflags! { - #[derive(Default)] pub struct Cr3(u64) { /// CarryPage-level write-through PWT = 1 << 3; @@ -96,7 +94,6 @@ bitflags! { } bitflags! { - #[derive(Default)] pub struct Cr4(u64) { /// CarryVirtual 8086 Mode Extensions VME = 1 << 0; diff --git a/alioth/src/arch/x86_64/sev.rs b/alioth/src/arch/x86_64/sev.rs index dc4c66a8..7d3cf4e9 100644 --- a/alioth/src/arch/x86_64/sev.rs +++ b/alioth/src/arch/x86_64/sev.rs @@ -89,7 +89,6 @@ consts! { /// AMD SEV-SNP launch update page type. /// /// From SEV SNP Firmware ABI Specification, Revision 1.55, Table 67. - #[derive(Default)] pub struct SnpPageType(u8) { /// A normal data page. NORMAL = 1; diff --git a/alioth/src/arch/x86_64/tdx.rs b/alioth/src/arch/x86_64/tdx.rs index d21fdf63..318f5743 100644 --- a/alioth/src/arch/x86_64/tdx.rs +++ b/alioth/src/arch/x86_64/tdx.rs @@ -12,12 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::bitflags; -use serde::{Deserialize, Serialize}; use serde_aco::Help; +use crate::bitflags; + bitflags! { - #[derive(Default, Serialize, Deserialize, Help)] + #[derive(Help)] pub struct TdAttr(u64) { DEBUG = 1 << 0; SEPT_VE_DISABLE = 1 << 28; diff --git a/alioth/src/blk/qcow2.rs b/alioth/src/blk/qcow2.rs index b4090eeb..10a17037 100644 --- a/alioth/src/blk/qcow2.rs +++ b/alioth/src/blk/qcow2.rs @@ -67,7 +67,6 @@ bitflags! { } consts! { - #[derive(Default, Immutable, KnownLayout, FromBytes, IntoBytes)] pub struct Qcow2Compression(u8) { DEFLATE = 0; ZSTD = 1; diff --git a/alioth/src/device/ioapic.rs b/alioth/src/device/ioapic.rs index c017596f..4925d2c1 100644 --- a/alioth/src/device/ioapic.rs +++ b/alioth/src/device/ioapic.rs @@ -60,11 +60,11 @@ impl IoApic { return Ok(()); } - if entry.dest_mode() == DestinationMode::LOGICAL.raw() { + if entry.dest_mode() == DestinationMode::LOGICAL { log::warn!("IOAPIC: logical destination is not supported"); return Ok(()); } - if entry.trigger_mode() == TriggerMode::LEVEL.raw() { + if entry.trigger_mode() == TriggerMode::LEVEL { log::warn!("IOAPIC: level-triggered interrupts are not supported"); return Ok(()); } diff --git a/alioth/src/device/pl011.rs b/alioth/src/device/pl011.rs index a4a04e5b..e87a8edb 100644 --- a/alioth/src/device/pl011.rs +++ b/alioth/src/device/pl011.rs @@ -78,7 +78,6 @@ const PERIPH_ID: [u32; 4] = [0x11, 0x10, 0x14, 0x00]; const PCELL_ID: [u32; 4] = [0x0d, 0xf0, 0x05, 0xb1]; bitflags! { - #[derive(Default)] pub struct Flag(u16) { RI = 1 << 8; /// Transmit FIFO empty @@ -97,7 +96,6 @@ bitflags! { } bitflags! { - #[derive(Default)] pub struct Interrupt(u16) { /// Overrun error interrupt status. OERIS = 1 << 10; diff --git a/alioth/src/device/pl031.rs b/alioth/src/device/pl031.rs index 3ca2b416..dc5b5b5c 100644 --- a/alioth/src/device/pl031.rs +++ b/alioth/src/device/pl031.rs @@ -44,7 +44,6 @@ const PERIPH_ID: [u8; 4] = [0x31, 0x10, 0x04, 0x00]; const PCELL_ID: [u8; 4] = [0x0d, 0xf0, 0x05, 0xb1]; bitflags! { - #[derive(Default)] struct Interrupt(u32) { RTCINTR = 1 << 0; } diff --git a/alioth/src/device/serial.rs b/alioth/src/device/serial.rs index 7db516d5..78e18ca7 100644 --- a/alioth/src/device/serial.rs +++ b/alioth/src/device/serial.rs @@ -41,7 +41,6 @@ const SCRATCH_REGISTER: u64 = 0x7; // offset 0x1, Interrupt Enable Register (IER) bitflags! { - #[derive(Default)] pub struct InterruptEnable(u8) { MODEM_STATUS = 1 << 3; RECEIVER_LINE_STATUS = 1 << 2; @@ -160,12 +159,6 @@ bitflags! { } } -impl Default for LineStatus { - fn default() -> Self { - LineStatus::TX_EMPTY | LineStatus::TX_HOLDING_REGISTER_EMPTY - } -} - #[derive(Default, Debug)] struct SerialReg { interrupt_enable: InterruptEnable, // 0x1, Interrupt Enable Register (IER) @@ -181,6 +174,15 @@ struct SerialReg { data: VecDeque, } +impl SerialReg { + pub fn new() -> Self { + SerialReg { + line_status: LineStatus::TX_EMPTY | LineStatus::TX_HOLDING_REGISTER_EMPTY, + ..Default::default() + } + } +} + #[derive(Debug)] pub struct Serial { name: Arc, @@ -338,7 +340,7 @@ where for<'a> &'a C: Read + Write, { pub fn new(base_port: u16, io_apci: Arc>, pin: u8, console: C) -> Result { - let reg = Arc::new(Mutex::new(SerialReg::default())); + let reg = Arc::new(Mutex::new(SerialReg::new())); let console = Arc::new(console); let name: Arc = Arc::from(format!("serial_{base_port:#x}")); let uart_recv = SerialRecv { diff --git a/alioth/src/firmware/acpi/bindings.rs b/alioth/src/firmware/acpi/bindings.rs index 3ffbb741..f7e4fc6a 100644 --- a/alioth/src/firmware/acpi/bindings.rs +++ b/alioth/src/firmware/acpi/bindings.rs @@ -13,7 +13,7 @@ // limitations under the License. use bitfield::bitfield; -use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout}; +use zerocopy::{FromBytes, Immutable, IntoBytes}; use crate::bitflags; @@ -85,7 +85,6 @@ pub const FADT_MAJOR_VERSION: u8 = 6; pub const FADT_MINOR_VERSION: u8 = 4; bitflags! { - #[derive(Default, KnownLayout, Immutable, FromBytes, IntoBytes)] pub struct AcpiFadtFlag(u32) { TMR_VAL_EXT = 1 << 8; RESET_REG_SUP = 1 << 10; diff --git a/alioth/src/firmware/ovmf/ovmf_x86_64/sev.rs b/alioth/src/firmware/ovmf/ovmf_x86_64/sev.rs index fa5ed602..bf3457c0 100644 --- a/alioth/src/firmware/ovmf/ovmf_x86_64/sev.rs +++ b/alioth/src/firmware/ovmf/ovmf_x86_64/sev.rs @@ -37,7 +37,6 @@ pub struct SevMetaData { } consts! { - #[derive(KnownLayout, Immutable, FromBytes, IntoBytes)] pub struct SevDescType(u32) { SNP_DESC_MEM = 1; SNP_SECRETS = 2; diff --git a/alioth/src/firmware/ovmf/ovmf_x86_64/tdx.rs b/alioth/src/firmware/ovmf/ovmf_x86_64/tdx.rs index 12520525..31cb145f 100644 --- a/alioth/src/firmware/ovmf/ovmf_x86_64/tdx.rs +++ b/alioth/src/firmware/ovmf/ovmf_x86_64/tdx.rs @@ -43,7 +43,6 @@ pub struct TdvfMetadata { } consts! { - #[derive(Default, KnownLayout, Immutable, FromBytes, IntoBytes)] pub struct TdvfSectionType(u32) { BFV = 0; CFV = 1; @@ -53,7 +52,6 @@ consts! { } bitflags! { - #[derive(Default, KnownLayout, Immutable, FromBytes, IntoBytes)] pub struct TdvfSectionAttr(u32) { MR_EXTEND = 1 << 0; PAGE_AUG = 1 << 1; diff --git a/alioth/src/firmware/uefi/uefi.rs b/alioth/src/firmware/uefi/uefi.rs index 90afd189..320ead07 100644 --- a/alioth/src/firmware/uefi/uefi.rs +++ b/alioth/src/firmware/uefi/uefi.rs @@ -17,7 +17,6 @@ use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout}; use crate::{bitflags, consts}; consts! { - #[derive(Default, KnownLayout, Immutable, FromBytes, IntoBytes)] pub struct HobType(u16) { HANDOFF = 0x0001; RESOURCE_DESCRIPTOR = 0x0003; @@ -49,7 +48,6 @@ pub struct HobHandoffInfoTable { } consts! { - #[derive(Default, KnownLayout, Immutable, FromBytes, IntoBytes)] pub struct HobResourceType(u32) { SYSTEM_MEMORY = 0x00000000; MEMORY_UNACCEPTED = 0x00000007; @@ -57,7 +55,6 @@ consts! { } bitflags! { - #[derive(Default, KnownLayout, Immutable, FromBytes, IntoBytes)] pub struct ResourceAttr(u32) { PRESENT = 1 << 0; INIT = 1 << 1; diff --git a/alioth/src/fuse/bindings.rs b/alioth/src/fuse/bindings.rs index 0317437e..7a878966 100644 --- a/alioth/src/fuse/bindings.rs +++ b/alioth/src/fuse/bindings.rs @@ -262,7 +262,6 @@ pub struct FuseFileLock { } consts! { - #[derive(FromBytes, Immutable, IntoBytes)] pub struct FuseOpcode(u32) { LOOKUP = 1; FORGET = 2; @@ -724,7 +723,6 @@ pub struct FuseOutHeader { } consts! { - #[derive(FromBytes, KnownLayout, Immutable, IntoBytes)] pub struct FuseDirentType(u32) { UNKNOWN = 0x0; FIFO = 0x1; @@ -901,7 +899,6 @@ pub struct FuseSecctxHeader { } consts! { - #[derive(Default, FromBytes, KnownLayout, Immutable, IntoBytes)] pub struct FuseExtType(u32) { MAX_NR_SECCTX = 31; EXT_GROUPS = 32; diff --git a/alioth/src/pci/cap.rs b/alioth/src/pci/cap.rs index 7bb272af..eb38ad3f 100644 --- a/alioth/src/pci/cap.rs +++ b/alioth/src/pci/cap.rs @@ -31,7 +31,6 @@ use crate::utils::truncate_u64; use crate::{align_up, consts, impl_mmio_for_zerocopy, mask_bits, mem}; consts! { - #[derive(Default, FromBytes, Immutable, IntoBytes, KnownLayout)] pub struct PciCapId(u8) { MSI = 0x05; VENDOR = 0x09; diff --git a/alioth/src/pci/config.rs b/alioth/src/pci/config.rs index d3e6b517..9f90d2b5 100644 --- a/alioth/src/pci/config.rs +++ b/alioth/src/pci/config.rs @@ -53,7 +53,6 @@ impl SlotBackend for Box { } bitflags! { - #[derive(Default, FromBytes, Immutable, KnownLayout, IntoBytes)] pub struct Command(u16) { INTX_DISABLE = 1 << 10; SERR = 1 << 8; @@ -71,7 +70,6 @@ bitflags! { } bitflags! { - #[derive(Default, FromBytes, Immutable, KnownLayout, IntoBytes)] pub struct Status(u16) { PARITY_ERR = 1 << 15; SYSTEM_ERR = 1 << 14; @@ -92,7 +90,6 @@ bitflags! { } consts! { - #[derive(Default, FromBytes, Immutable, KnownLayout, IntoBytes)] pub struct HeaderType(u8) { DEVICE = 0; BRIDGE = 1; diff --git a/alioth/src/sys/linux/kvm.rs b/alioth/src/sys/linux/kvm.rs index 680efb1e..b8e0e42f 100644 --- a/alioth/src/sys/linux/kvm.rs +++ b/alioth/src/sys/linux/kvm.rs @@ -44,7 +44,6 @@ pub struct KvmVmType(#[allow(dead_code)] pub u64); pub const KVM_MAX_CPUID_ENTRIES: usize = 256; bitflags! { - #[derive(Default)] pub struct KvmCpuid2Flag(u32) { SIGNIFCANT_INDEX = 1 << 0; } @@ -115,7 +114,6 @@ pub struct KvmMsrs { pub const MAX_IO_MSRS: usize = 256; bitflags! { - #[derive(Default)] pub struct KvmMemFlag(u32) { LOG_DIRTY_PAGES = 1 << 0; READONLY = 1 << 1; @@ -148,7 +146,6 @@ pub struct KvmUserspaceMemoryRegion2 { } bitflags! { - #[derive(Default)] pub struct KvmMemoryAttribute(u64) { PRIVATE = 1 << 3; } @@ -383,7 +380,6 @@ pub union KvmSyncRegsBlock { } bitflags! { - #[derive(Default)] pub struct KvmIrqfdFlag(u32) { DEASSIGN = 1 << 0; RESAMPLE = 1 << 1; @@ -484,7 +480,6 @@ impl Debug for KvmIrqRouting { } bitflags! { - #[derive(Default)] pub struct KvmMsiFlag(u32) { VALID_DEVID = 1 << 0; } @@ -532,7 +527,6 @@ bitflags! { } bitflags! { - #[derive(Default)] pub struct KvmIoEventFdFlag(u32) { DATA_MATCH = 1 << 0; PIO = 1 << 1; diff --git a/alioth/src/sys/linux/tdx.rs b/alioth/src/sys/linux/tdx.rs index 476ceaa9..49b07190 100644 --- a/alioth/src/sys/linux/tdx.rs +++ b/alioth/src/sys/linux/tdx.rs @@ -17,7 +17,6 @@ use crate::sys::kvm::{KVM_MAX_CPUID_ENTRIES, KvmCpuid2}; use crate::{bitflags, consts}; consts! { - #[derive(Default)] pub struct KvmTdxCmdId(u32) { CAPABILITIES = 0; INIT_VM = 1; diff --git a/alioth/src/sys/linux/vfio.rs b/alioth/src/sys/linux/vfio.rs index 3926dc84..ea3e7c1d 100644 --- a/alioth/src/sys/linux/vfio.rs +++ b/alioth/src/sys/linux/vfio.rs @@ -30,7 +30,6 @@ pub struct VfioInfoCapHeader { } bitflags! { - #[derive(Default)] pub struct VfioDeviceInfoFlag(u32) { RESET = 1 << 0; PCI = 1 << 1; @@ -56,7 +55,6 @@ pub struct VfioDeviceInfo { } bitflags! { - #[derive(Default)] pub struct VfioRegionInfoFlag(u32) { READ = 1 << 0; WRITE = 1 << 1; @@ -91,14 +89,12 @@ pub struct VfioRegionInfo { } consts! { - #[derive(Default)] pub struct VfioRegionInfoCap(u16) { MSIX_MAPPABLE = 3; } } bitflags! { - #[derive(Default)] pub struct VfioIrqInfoFlag(u32) { EVENTFD = 1 << 0; MASKABLE = 1 << 1; @@ -108,7 +104,6 @@ bitflags! { } consts! { - #[derive(Default)] pub struct VfioPciIrq(u32) { INTX = 0; MSI = 1; @@ -196,7 +191,6 @@ pub struct IommuIoasAlloc { } bitflags! { - #[derive(Default)] pub struct IommuIoasMapFlag(u32) { FIXED_IOVA = 1 << 0; WRITEABLE = 1 << 1; @@ -226,7 +220,6 @@ pub struct IommuIoasUnmap { } bitflags! { - #[derive(Default)] pub struct VfioDmaMapFlag(u32) { READ = 1 << 0; WRITE = 1 << 1; @@ -245,7 +238,6 @@ pub struct VfioIommuType1DmaMap { } bitflags! { - #[derive(Default)] pub struct VfioDmaUnmapFlag(u32) { GET_DIRTY_BITMAP = 1 << 0; ALL = 1 << 1; diff --git a/alioth/src/sys/macos/block.rs b/alioth/src/sys/macos/block.rs index e75f0690..45807c56 100644 --- a/alioth/src/sys/macos/block.rs +++ b/alioth/src/sys/macos/block.rs @@ -25,7 +25,6 @@ pub struct BlockDescriptor { } bitflags! { - #[derive(Default)] pub struct BlockFlag(c_int) { HAS_STRET = 1 << 29; } diff --git a/alioth/src/sys/macos/hvf.rs b/alioth/src/sys/macos/hvf.rs index dc63d9c6..c1b3232f 100644 --- a/alioth/src/sys/macos/hvf.rs +++ b/alioth/src/sys/macos/hvf.rs @@ -58,7 +58,6 @@ consts! { } consts! { - #[derive(Default)] pub struct HvExitReason(u32) { CANCEL = 0; EXCEPTION = 1; diff --git a/alioth/src/utils/utils.rs b/alioth/src/utils/utils.rs index f72adcdd..e13245e4 100644 --- a/alioth/src/utils/utils.rs +++ b/alioth/src/utils/utils.rs @@ -127,7 +127,20 @@ macro_rules! consts { } ) => { #[repr(transparent)] - #[derive(PartialEq, Eq, Copy, Clone)] + #[derive( + Copy, + Clone, + Default, + PartialEq, + Eq, + Hash, + ::zerocopy::KnownLayout, + ::zerocopy::Immutable, + ::zerocopy::FromBytes, + ::zerocopy::IntoBytes, + ::serde::Serialize, + ::serde::Deserialize, + )] $(#[$attr])* $vs struct $EnumName($TyName); @@ -194,7 +207,20 @@ macro_rules! bitflags { } ) => { #[repr(transparent)] - #[derive(PartialEq, Eq, Copy, Clone, Hash)] + #[derive( + Copy, + Clone, + Default, + PartialEq, + Eq, + Hash, + ::zerocopy::KnownLayout, + ::zerocopy::Immutable, + ::zerocopy::FromBytes, + ::zerocopy::IntoBytes, + ::serde::Serialize, + ::serde::Deserialize, + )] $(#[$attr])* $vs struct $FlagTy($TyName); diff --git a/alioth/src/virtio/dev/blk.rs b/alioth/src/virtio/dev/blk.rs index 79022458..460c0c2c 100644 --- a/alioth/src/virtio/dev/blk.rs +++ b/alioth/src/virtio/dev/blk.rs @@ -48,7 +48,6 @@ use crate::virtio::{DeviceId, FEATURE_BUILT_IN, IrqSender, Result, error}; use crate::{bitflags, consts, impl_mmio_for_zerocopy}; consts! { - #[derive(FromBytes)] pub struct RequestType(u32) { IN = 0; OUT = 1; @@ -62,7 +61,6 @@ consts! { } consts! { - #[derive(FromBytes)] pub struct Status(u8) { OK = 0; IOERR = 1; diff --git a/alioth/src/virtio/dev/fs/fs.rs b/alioth/src/virtio/dev/fs/fs.rs index 4005fa07..649310af 100644 --- a/alioth/src/virtio/dev/fs/fs.rs +++ b/alioth/src/virtio/dev/fs/fs.rs @@ -44,6 +44,8 @@ use crate::virtio::worker::mio::{ActiveMio, Mio, VirtioMio}; use crate::virtio::{DeviceId, FEATURE_BUILT_IN, IrqSender}; use crate::{bitflags, ffi, impl_mmio_for_zerocopy}; +pub const DAX_SHMEM_ID: u8 = 0; + impl DaxRegion for ArcMemPages { fn map( &self, diff --git a/alioth/src/virtio/dev/fs/vu.rs b/alioth/src/virtio/dev/fs/vu.rs index a9945fef..e4f51b6f 100644 --- a/alioth/src/virtio/dev/fs/vu.rs +++ b/alioth/src/virtio/dev/fs/vu.rs @@ -14,7 +14,6 @@ use std::fs::File; use std::io::ErrorKind; -use std::iter::zip; use std::mem::size_of_val; use std::os::fd::{AsFd, AsRawFd}; use std::path::Path; @@ -37,10 +36,12 @@ use crate::hv::IoeventFd; use crate::mem::mapped::{ArcMemPages, RamBus}; use crate::mem::{LayoutChanged, MemRegion, MemRegionType}; use crate::sync::notifier::Notifier; -use crate::virtio::dev::fs::{FsConfig, FsFeature}; +use crate::virtio::dev::fs::{DAX_SHMEM_ID, FsConfig, FsFeature}; use crate::virtio::dev::{DevParam, Virtio, WakeEvent}; use crate::virtio::queue::{QueueReg, VirtQueue}; -use crate::virtio::vu::bindings::{DeviceConfig, FsMap, VuBackMsg, VuFeature}; +use crate::virtio::vu::bindings::{ + DeviceConfig, VhostUserMmap, VhostUserMmapFlag, VuBackMsg, VuFeature, +}; use crate::virtio::vu::conn::VuChannel; use crate::virtio::vu::frontend::VuFrontend; use crate::virtio::vu::{Error, error as vu_error}; @@ -59,7 +60,7 @@ impl VuFs { pub fn new(param: VuFsParam, name: impl Into>) -> Result { let mut extra_features = VuFeature::empty(); if param.dax_window > 0 { - extra_features |= VuFeature::BACKEND_REQ | VuFeature::BACKEND_SEND_FD + extra_features |= VuFeature::BACKEND_REQ | VuFeature::BACKEND_SEND_FD | VuFeature::SHMEM }; if param.tag.is_none() { extra_features |= VuFeature::CONFIG; @@ -234,66 +235,67 @@ impl VirtioMio for VuFs { .fail()?; }; loop { - let mut fds = [const { None }; 8]; + let mut fds = [None]; let msg = channel.recv_msg(&mut fds); let (request, size) = match msg { Ok(m) => (m.request, m.size), Err(Error::System { error, .. }) if error.kind() == ErrorKind::WouldBlock => break, Err(e) => return Err(e)?, }; - let fs_map: FsMap = channel.recv_payload()?; + let payload: VhostUserMmap = channel.recv_payload()?; - if size as usize != size_of_val(&fs_map) { + if size as usize != size_of_val(&payload) { return vu_error::PayloadSize { - want: size_of_val(&fs_map), + want: size_of_val(&payload), got: size, } .fail()?; } match VuBackMsg::from(request) { - VuBackMsg::SHARED_OBJECT_ADD => { - for (index, fd) in fds.iter().enumerate() { - let Some(fd) = fd else { - break; - }; - let raw_fd = fd.as_raw_fd(); - let map_addr = dax_region.addr() + fs_map.cache_offset[index] as usize; - log::trace!( - "{}: mapping fd {raw_fd} to offset {:#x}", - self.name(), - fs_map.cache_offset[index] - ); - ffi!( - unsafe { - mmap( - map_addr as _, - fs_map.len[index] as _, - fs_map.flags[index] as _, - MAP_SHARED | MAP_FIXED, - raw_fd, - fs_map.fd_offset[index] as _, - ) - }, - MAP_FAILED - )?; - } + VuBackMsg::SHMEM_MAP => { + let [Some(fd)] = fds else { + return vu_error::MissingFd { req: request }.fail()?; + }; + let shm_offset = payload.shm_offset; + let map_addr = dax_region.addr() + shm_offset as usize; + let prot = if payload.flags.contains(VhostUserMmapFlag::RW) { + libc::PROT_READ | libc::PROT_WRITE + } else { + libc::PROT_READ + }; + log::trace!( + "{}: mapping fd {} to offset {shm_offset:#x}, size {:#x}", + self.name(), + fd.as_raw_fd(), + payload.len, + ); + ffi!( + unsafe { + mmap( + map_addr as _, + payload.len as _, + prot, + MAP_SHARED | MAP_FIXED, + fd.as_raw_fd(), + payload.fd_offset as _, + ) + }, + MAP_FAILED + )?; } - VuBackMsg::SHARED_OBJECT_REMOVE => { - for (len, offset) in zip(fs_map.len, fs_map.cache_offset) { - if len == 0 { - continue; - } - log::trace!( - "{}: unmapping offset {offset:#x}, size {len:#x}", - self.name() - ); - let map_addr = dax_region.addr() + offset as usize; - let flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED; - ffi!( - unsafe { mmap(map_addr as _, len as _, PROT_NONE, flags, -1, 0) }, - MAP_FAILED - )?; - } + VuBackMsg::SHMEM_UNMAP => { + let shm_offset = payload.shm_offset; + let len = payload.len; + log::trace!( + "{}: unmapping offset {shm_offset:#x}, size {len:#x}", + self.name(), + ); + let map_addr = dax_region.addr() + shm_offset as usize; + let flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED; + ffi!( + unsafe { mmap(map_addr as _, len as _, PROT_NONE, flags, -1, 0) }, + MAP_FAILED + )?; } _ => unimplemented!("{}: unknown request {request:#x}", self.name()), } @@ -334,32 +336,34 @@ impl DaxRegion for VuDaxRegion { len: u64, flag: FuseSetupmappingFlag, ) -> fuse::Result<()> { - let mut fs_map = FsMap::new_zeroed(); - fs_map.fd_offset[0] = f_offset; - fs_map.cache_offset[0] = m_offset; - - let mut prot = 0; - if flag.contains(FuseSetupmappingFlag::READ) { - prot |= libc::PROT_READ; + let flags = if flag.contains(FuseSetupmappingFlag::WRITE) { + VhostUserMmapFlag::RW + } else { + VhostUserMmapFlag::empty() }; - if flag.contains(FuseSetupmappingFlag::WRITE) { - prot |= libc::PROT_WRITE; - } - fs_map.flags[0] = prot as _; - fs_map.len[0] = len; - let fds = [fd.as_fd()]; + let payload = VhostUserMmap { + shmid: DAX_SHMEM_ID, + fd_offset: f_offset, + shm_offset: m_offset, + len, + flags, + ..Default::default() + }; self.channel - .fs_map(&fs_map, &fds) + .shmem_map(&payload, fd.as_fd()) .box_trace(fuse::error::DaxMapping) } fn unmap(&self, m_offset: u64, len: u64) -> fuse::Result<()> { - let mut fs_map = FsMap::new_zeroed(); - fs_map.cache_offset[0] = m_offset; - fs_map.len[0] = len; + let payload = VhostUserMmap { + shmid: DAX_SHMEM_ID, + shm_offset: m_offset, + len, + ..Default::default() + }; self.channel - .fs_unmap(&fs_map) + .shmem_unmap(&payload) .box_trace(fuse::error::DaxMapping) } } diff --git a/alioth/src/virtio/dev/net/net.rs b/alioth/src/virtio/dev/net/net.rs index 9aeacd4d..1b626f0a 100644 --- a/alioth/src/virtio/dev/net/net.rs +++ b/alioth/src/virtio/dev/net/net.rs @@ -41,7 +41,6 @@ pub struct NetConfig { impl_mmio_for_zerocopy!(NetConfig); consts! { - #[derive(Default, FromBytes, Immutable, IntoBytes)] pub struct CtrlAck(u8) { OK = 0; ERR = 1; @@ -49,14 +48,12 @@ consts! { } consts! { - #[derive(Default, FromBytes, Immutable, IntoBytes)] pub struct CtrlClass(u8) { MQ = 4; } } consts! { - #[derive(Default, FromBytes, Immutable, IntoBytes)] pub struct CtrlMq(u8) { VQ_PARIS_SET = 0; } diff --git a/alioth/src/virtio/dev/vsock/vsock.rs b/alioth/src/virtio/dev/vsock/vsock.rs index 783609b6..dedf3aaf 100644 --- a/alioth/src/virtio/dev/vsock/vsock.rs +++ b/alioth/src/virtio/dev/vsock/vsock.rs @@ -27,7 +27,6 @@ pub use self::uds_vsock::{UdsVsock, UdsVsockParam}; pub use self::vhost_vsock::{VhostVsock, VhostVsockParam}; consts! { - #[derive(Default, FromBytes, Immutable, IntoBytes)] pub struct VsockVirtq(u16) { RX = 0; TX = 1; @@ -54,7 +53,6 @@ bitflags! { } consts! { - #[derive(Default, FromBytes, Immutable, IntoBytes)] pub struct VsockOp(u16) { INVALID = 0; REQUEST = 1; @@ -68,7 +66,6 @@ consts! { } consts! { - #[derive(Default, FromBytes, Immutable, IntoBytes)] pub struct VsockType(u16) { STREAM = 1; SEQPACKET = 2; diff --git a/alioth/src/virtio/pci.rs b/alioth/src/virtio/pci.rs index 4a9c8375..13619d5b 100644 --- a/alioth/src/virtio/pci.rs +++ b/alioth/src/virtio/pci.rs @@ -585,7 +585,6 @@ fn get_class(id: DeviceId) -> (u8, u8) { } consts! { - #[derive(Default, FromZeros, Immutable, IntoBytes)] pub struct VirtioPciCfg(u8) { COMMON = 1; NOTIFY = 2; diff --git a/alioth/src/virtio/virtio.rs b/alioth/src/virtio/virtio.rs index 08f5a0be..bbbbf08b 100644 --- a/alioth/src/virtio/virtio.rs +++ b/alioth/src/virtio/virtio.rs @@ -114,7 +114,6 @@ consts! { } bitflags! { - #[derive(Default)] pub struct DevStatus(u8) { ACK = 1 << 0; DRIVER = 1 << 1; diff --git a/alioth/src/virtio/vu/backend.rs b/alioth/src/virtio/vu/backend.rs index 0a5c744f..ea753672 100644 --- a/alioth/src/virtio/vu/backend.rs +++ b/alioth/src/virtio/vu/backend.rs @@ -271,7 +271,8 @@ impl VuBackend { | VuFeature::BACKEND_REQ | VuFeature::BACKEND_SEND_FD | VuFeature::CONFIG - | VuFeature::STATUS; + | VuFeature::STATUS + | VuFeature::SHMEM; self.session.reply(req, &feature.bits(), &[])?; msg.flag.set_need_reply(false); log::debug!("{name}: get protocol feature: {feature:x?}"); diff --git a/alioth/src/virtio/vu/bindings.rs b/alioth/src/virtio/vu/bindings.rs index 65a57c05..4bf744cc 100644 --- a/alioth/src/virtio/vu/bindings.rs +++ b/alioth/src/virtio/vu/bindings.rs @@ -39,6 +39,7 @@ bitflags! { XEN_MMAP = 1 << 17; SHARED_OBJECT = 1 << 18; DEVICE_STATE = 1 << 19; + SHMEM = 1 << 21; } } @@ -89,12 +90,6 @@ consts! { } } -consts! { - pub struct VuFrontMsgSize((u32, usize)) { - GET_FEATURES = (0, size_of::()); - } -} - consts! { pub struct VuBackMsg(u32) { IOTLB_MSG = 1; @@ -105,6 +100,8 @@ consts! { SHARED_OBJECT_ADD = 6; SHARED_OBJECT_REMOVE = 7; SHARED_OBJECT_LOOKUP = 8; + SHMEM_MAP = 9; + SHMEM_UNMAP = 10; } } @@ -184,15 +181,6 @@ pub struct DeviceConfig { pub flags: u32, } -#[derive(Debug, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)] -#[repr(C)] -pub struct FsMap { - pub fd_offset: [u64; 8], - pub cache_offset: [u64; 8], - pub len: [u64; 8], - pub flags: [u64; 8], -} - #[derive(Debug, IntoBytes, FromBytes, Immutable, KnownLayout)] #[repr(C)] pub struct Message { @@ -200,3 +188,20 @@ pub struct Message { pub flag: MessageFlag, pub size: u32, } + +bitflags! { + pub struct VhostUserMmapFlag(u64) { + RW = 1 << 0; + } +} + +#[derive(Debug, Clone, Default, FromBytes, Immutable, IntoBytes, KnownLayout)] +#[repr(C)] +pub struct VhostUserMmap { + pub shmid: u8, + pub padding: [u8; 7], + pub fd_offset: u64, + pub shm_offset: u64, + pub len: u64, + pub flags: VhostUserMmapFlag, +} diff --git a/alioth/src/virtio/vu/conn.rs b/alioth/src/virtio/vu/conn.rs index 7eb76095..7a3b342d 100644 --- a/alioth/src/virtio/vu/conn.rs +++ b/alioth/src/virtio/vu/conn.rs @@ -23,8 +23,8 @@ use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes}; use crate::ffi; use crate::utils::uds::{recv_msg_with_fds, send_msg_with_fds}; use crate::virtio::vu::bindings::{ - DeviceConfig, FsMap, MAX_CONFIG_SIZE, MemorySingleRegion, Message, MessageFlag, VirtqAddr, - VirtqState, VuBackMsg, VuFrontMsg, + DeviceConfig, MAX_CONFIG_SIZE, MemorySingleRegion, Message, MessageFlag, VhostUserMmap, + VirtqAddr, VirtqState, VuBackMsg, VuFrontMsg, }; use crate::virtio::vu::{Result, error}; @@ -363,11 +363,11 @@ impl VuChannel { send(&self.conn, req.raw(), payload, &[], &mut [], fds) } - pub fn fs_map(&self, payload: &FsMap, fds: &[BorrowedFd]) -> Result<()> { - self.send(VuBackMsg::SHARED_OBJECT_ADD, payload, fds) + pub fn shmem_map(&self, payload: &VhostUserMmap, fd: BorrowedFd) -> Result<()> { + self.send(VuBackMsg::SHMEM_MAP, payload, &[fd]) } - pub fn fs_unmap(&self, payload: &FsMap) -> Result<()> { - self.send(VuBackMsg::SHARED_OBJECT_REMOVE, payload, &[]) + pub fn shmem_unmap(&self, payload: &VhostUserMmap) -> Result<()> { + self.send(VuBackMsg::SHMEM_UNMAP, payload, &[]) } }