From 361f81593e1c37a13dc66eaa359eee79dcfe02db Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Thu, 25 Dec 2025 09:23:32 -0800 Subject: [PATCH 1/2] refactor(ioctl): simplify ioctl macros Signed-off-by: Changyuan Lyu --- alioth/src/sys/linux/if_tun.rs | 2 +- alioth/src/sys/linux/ioctl.rs | 75 ++++++++++++-------------------- alioth/src/sys/linux/kvm.rs | 18 ++++---- alioth/src/sys/linux/vfio.rs | 2 +- alioth/src/sys/linux/vhost.rs | 2 +- alioth/src/virtio/dev/net/tap.rs | 8 ++-- 6 files changed, 43 insertions(+), 64 deletions(-) diff --git a/alioth/src/sys/linux/if_tun.rs b/alioth/src/sys/linux/if_tun.rs index c4f32fe6..da6ef403 100644 --- a/alioth/src/sys/linux/if_tun.rs +++ b/alioth/src/sys/linux/if_tun.rs @@ -22,7 +22,7 @@ use crate::{ioctl_read, ioctl_write_ptr, ioctl_write_val}; ioctl_write_ptr!(tun_set_iff, ioctl_iow::(b'T', 202), ifreq); -ioctl_write_val!(tun_set_offload, ioctl_iow::(b'T', 208)); +ioctl_write_val!(tun_set_offload, ioctl_iow::(b'T', 208), TunFeature); ioctl_read!(tun_get_vnet_hdr_sz, b'T', 215, c_int); diff --git a/alioth/src/sys/linux/ioctl.rs b/alioth/src/sys/linux/ioctl.rs index 0e129692..bf3debbb 100644 --- a/alioth/src/sys/linux/ioctl.rs +++ b/alioth/src/sys/linux/ioctl.rs @@ -48,35 +48,19 @@ pub const fn ioctl_iowr(type_: u8, nr: u8) -> u32 { #[macro_export] macro_rules! ioctl_none { - ($name:ident, $type_:expr, $nr:expr, $val:expr) => { + ($name:ident, $type_:expr, $nr:expr) => { #[allow(clippy::missing_safety_doc)] pub unsafe fn $name(fd: &F) -> ::std::io::Result { let op = $crate::sys::ioctl::ioctl_io($type_, $nr); - let v = $val as ::libc::c_ulong; $crate::ffi!(unsafe { - ::libc::ioctl(::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()), op as _, v) + ::libc::ioctl(::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()), op as _, 0) }) } }; - ($name:ident, $type_:expr, $nr:expr) => { - $crate::ioctl_none!($name, $type_, $nr, 0); - }; } #[macro_export] macro_rules! ioctl_write_val { - ($name:ident, $code:expr) => { - #[allow(clippy::missing_safety_doc)] - pub unsafe fn $name( - fd: &F, - val: ::libc::c_ulong, - ) -> ::std::io::Result { - let op = $code; - $crate::ffi!(unsafe { - ::libc::ioctl(::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()), op as _, val) - }) - } - }; ($name:ident, $code:expr, $ty:ty) => { #[allow(clippy::missing_safety_doc)] pub unsafe fn $name( @@ -89,6 +73,9 @@ macro_rules! ioctl_write_val { }) } }; + ($name:ident, $type_:expr, $nr:expr, $ty:ty) => { + $crate::ioctl_write_val!($name, $crate::sys::ioctl::ioctl_io($type_, $nr), $ty); + }; } #[macro_export] @@ -109,22 +96,12 @@ macro_rules! ioctl_write_ptr { }) } }; - ($name:ident, $type_:expr, $nr:expr, $ty:ty) => { - #[allow(clippy::missing_safety_doc)] - pub unsafe fn $name( - fd: &F, - val: &$ty, - ) -> ::std::io::Result { - let op = $crate::sys::ioctl::ioctl_iow::<$ty>($type_, $nr); - $crate::ffi!(unsafe { - ::libc::ioctl( - ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()), - op as _, - val as *const $ty, - ) - }) - } + $crate::ioctl_write_ptr!( + $name, + $crate::sys::ioctl::ioctl_iow::<$ty>($type_, $nr), + $ty + ); }; } @@ -200,13 +177,13 @@ macro_rules! ioctl_writeread { #[macro_export] macro_rules! ioctl_writeread_buf { - ($name:ident, $type_:expr, $nr:expr, $ty:ident) => { + ($name:ident, $code:expr, $ty:ident) => { #[allow(clippy::missing_safety_doc)] pub unsafe fn $name( fd: &F, val: &mut $ty, ) -> ::std::io::Result { - let op = $crate::sys::ioctl::ioctl_iowr::<$ty<0>>($type_, $nr); + let op = $code; $crate::ffi!(unsafe { ::libc::ioctl( ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()), @@ -216,6 +193,13 @@ macro_rules! ioctl_writeread_buf { }) } }; + ($name:ident, $type_:expr, $nr:expr, $ty:ident) => { + $crate::ioctl_writeread_buf!( + $name, + $crate::sys::ioctl::ioctl_iowr::<$ty<0>>($type_, $nr), + $ty + ); + }; } #[macro_export] @@ -224,19 +208,7 @@ macro_rules! ioctl_read { #[allow(clippy::missing_safety_doc)] pub unsafe fn $name(fd: &F) -> ::std::io::Result<$ty> { let mut val = ::core::mem::MaybeUninit::<$ty>::uninit(); - $crate::ffi!(::libc::ioctl( - ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()), - $code as _, - val.as_mut_ptr() - ))?; - ::std::io::Result::Ok(val.assume_init()) - } - }; - ($name:ident, $type_:expr, $nr:expr, $ty:ty) => { - #[allow(clippy::missing_safety_doc)] - pub unsafe fn $name(fd: &F) -> ::std::io::Result<$ty> { - let mut val = ::core::mem::MaybeUninit::<$ty>::uninit(); - let op = $crate::sys::ioctl::ioctl_ior::<$ty>($type_, $nr); + let op = $code; $crate::ffi!(unsafe { ::libc::ioctl( ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd()), @@ -247,6 +219,13 @@ macro_rules! ioctl_read { ::std::io::Result::Ok(unsafe { val.assume_init() }) } }; + ($name:ident, $type_:expr, $nr:expr, $ty:ty) => { + $crate::ioctl_read!( + $name, + $crate::sys::ioctl::ioctl_ior::<$ty>($type_, $nr), + $ty + ); + }; } #[cfg(test)] diff --git a/alioth/src/sys/linux/kvm.rs b/alioth/src/sys/linux/kvm.rs index b579b382..39c534f9 100644 --- a/alioth/src/sys/linux/kvm.rs +++ b/alioth/src/sys/linux/kvm.rs @@ -20,9 +20,9 @@ use bitflags::bitflags; #[cfg(target_arch = "x86_64")] use crate::ioctl_writeread_buf; +use crate::sys::ioctl::ioctl_ior; #[cfg(target_arch = "x86_64")] use crate::sys::ioctl::ioctl_iowr; -use crate::sys::ioctl::{ioctl_io, ioctl_ior}; use crate::{ c_enum, ioctl_none, ioctl_read, ioctl_write_buf, ioctl_write_ptr, ioctl_write_val, ioctl_writeread, @@ -675,14 +675,14 @@ bitflags! { } } -ioctl_none!(kvm_get_api_version, KVMIO, 0x00, 0); -ioctl_write_val!(kvm_create_vm, ioctl_io(KVMIO, 0x01), KvmVmType); -ioctl_write_val!(kvm_check_extension, ioctl_io(KVMIO, 0x03), KvmCap); -ioctl_none!(kvm_get_vcpu_mmap_size, KVMIO, 0x04, 0); +ioctl_none!(kvm_get_api_version, KVMIO, 0x00); +ioctl_write_val!(kvm_create_vm, KVMIO, 0x01, KvmVmType); +ioctl_write_val!(kvm_check_extension, KVMIO, 0x03, KvmCap); +ioctl_none!(kvm_get_vcpu_mmap_size, KVMIO, 0x04); #[cfg(target_arch = "x86_64")] ioctl_writeread_buf!(kvm_get_supported_cpuid, KVMIO, 0x05, KvmCpuid2); -ioctl_write_val!(kvm_create_vcpu, ioctl_io(KVMIO, 0x41), u32); +ioctl_write_val!(kvm_create_vcpu, KVMIO, 0x41, u32); ioctl_write_ptr!( kvm_set_user_memory_region, KVMIO, @@ -690,7 +690,7 @@ ioctl_write_ptr!( KvmUserspaceMemoryRegion ); #[cfg(target_arch = "x86_64")] -ioctl_write_val!(kvm_set_tss_addr, ioctl_io(KVMIO, 0x47)); +ioctl_write_val!(kvm_set_tss_addr, KVMIO, 0x47, u64); #[cfg(target_arch = "x86_64")] ioctl_write_ptr!(kvm_set_identity_map_addr, KVMIO, 0x48, u64); ioctl_write_ptr!( @@ -701,13 +701,13 @@ ioctl_write_ptr!( ); #[cfg(target_arch = "x86_64")] -ioctl_none!(kvm_create_irqchip, KVMIO, 0x60, 0); +ioctl_none!(kvm_create_irqchip, KVMIO, 0x60); ioctl_write_buf!(kvm_set_gsi_routing, KVMIO, 0x6a, KvmIrqRouting); ioctl_write_ptr!(kvm_irqfd, KVMIO, 0x76, KvmIrqfd); ioctl_write_ptr!(kvm_ioeventfd, KVMIO, 0x79, KvmIoEventFd); -ioctl_none!(kvm_run, KVMIO, 0x80, 0); +ioctl_none!(kvm_run, KVMIO, 0x80); #[cfg(target_arch = "x86_64")] ioctl_read!(kvm_get_regs, KVMIO, 0x81, KvmRegs); #[cfg(target_arch = "x86_64")] diff --git a/alioth/src/sys/linux/vfio.rs b/alioth/src/sys/linux/vfio.rs index 67b5c9fc..5830dd63 100644 --- a/alioth/src/sys/linux/vfio.rs +++ b/alioth/src/sys/linux/vfio.rs @@ -301,7 +301,7 @@ ioctl_writeread!( ioctl_write_buf!(vfio_device_set_irqs, ioctl_io(VFIO_TYPE, 110), VfioIrqSet); -ioctl_none!(vfio_device_reset, VFIO_TYPE, 111, 0); +ioctl_none!(vfio_device_reset, VFIO_TYPE, 111); ioctl_write_ptr!( vfio_device_bind_iommufd, diff --git a/alioth/src/sys/linux/vhost.rs b/alioth/src/sys/linux/vhost.rs index 15db6afb..a8740c58 100644 --- a/alioth/src/sys/linux/vhost.rs +++ b/alioth/src/sys/linux/vhost.rs @@ -73,7 +73,7 @@ bitflags! { ioctl_read!(vhost_get_features, VHOST_VIRTIO, 0x00, u64); ioctl_write_ptr!(vhost_set_features, VHOST_VIRTIO, 0x00, u64); -ioctl_none!(vhost_set_owner, VHOST_VIRTIO, 0x01, 0); +ioctl_none!(vhost_set_owner, VHOST_VIRTIO, 0x01); ioctl_write_buf!( vhost_set_mem_table, diff --git a/alioth/src/virtio/dev/net/tap.rs b/alioth/src/virtio/dev/net/tap.rs index f367ff0a..902dc914 100644 --- a/alioth/src/virtio/dev/net/tap.rs +++ b/alioth/src/virtio/dev/net/tap.rs @@ -443,17 +443,17 @@ fn detect_tap_offload(tap: &impl AsFd) -> NetFeature { | NetFeature::GUEST_UFO | NetFeature::GUEST_USO4 | NetFeature::GUEST_USO6; - if unsafe { tun_set_offload(tap, tap_feature.bits()) }.is_ok() { + if unsafe { tun_set_offload(tap, tap_feature) }.is_ok() { return dev_feat; } tap_feature &= !(TunFeature::USO4 | TunFeature::USO6); dev_feat &= !(NetFeature::GUEST_USO4 | NetFeature::GUEST_USO6); - if unsafe { tun_set_offload(tap, tap_feature.bits()) }.is_ok() { + if unsafe { tun_set_offload(tap, tap_feature) }.is_ok() { return dev_feat; } tap_feature &= !(TunFeature::UFO); dev_feat &= !NetFeature::GUEST_UFO; - if unsafe { tun_set_offload(tap, tap_feature.bits()) }.is_ok() { + if unsafe { tun_set_offload(tap, tap_feature) }.is_ok() { return dev_feat; } NetFeature::empty() @@ -482,6 +482,6 @@ fn enable_tap_offload(tap: &mut File, feature: NetFeature) -> Result<()> { if feature.contains(NetFeature::GUEST_USO6) { tap_feature |= TunFeature::USO6; } - unsafe { tun_set_offload(tap, tap_feature.bits()) }?; + unsafe { tun_set_offload(tap, tap_feature) }?; Ok(()) } From b3c585943b5f11df6a6c2f017be7fc61e1de2366 Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Thu, 25 Dec 2025 09:24:31 -0800 Subject: [PATCH 2/2] feat(kvm): add definitions for KVM_KVMCLOCK_CTRL Signed-off-by: Changyuan Lyu --- alioth/src/sys/linux/kvm.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/alioth/src/sys/linux/kvm.rs b/alioth/src/sys/linux/kvm.rs index 39c534f9..a5529e57 100644 --- a/alioth/src/sys/linux/kvm.rs +++ b/alioth/src/sys/linux/kvm.rs @@ -506,6 +506,7 @@ c_enum! { pub struct KvmCap(u32); { IRQFD = 32; + KVMCLOCK_CTRL = 76; SIGNAL_MSI = 77; ARM_PSCI_0_2 = 102; X2APIC_API = 129; @@ -730,6 +731,8 @@ ioctl_write_ptr!(kvm_get_one_reg, KVMIO, 0xab, KvmOneReg); #[cfg(not(target_arch = "x86_64"))] ioctl_write_ptr!(kvm_set_one_reg, KVMIO, 0xac, KvmOneReg); +ioctl_none!(kvm_kvmclock_ctrl, KVMIO, 0xad); + #[cfg(target_arch = "aarch64")] ioctl_write_ptr!(kvm_arm_vcpu_init, KVMIO, 0xae, KvmVcpuInit); #[cfg(target_arch = "aarch64")]