This adds support to issue the set irqfd ioctl via the privcmd file.
Signed-off-by: Viresh Kumar viresh.kumar@linaro.org --- V3: - Based on changes merged in kernel.
V2: - Fixed comment
crates/xen-ioctls/src/private.rs | 14 +++++- crates/xen-ioctls/src/xdm/types.rs | 14 ++++++ crates/xen-ioctls/src/xdm/xdm.rs | 68 ++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-)
diff --git a/crates/xen-ioctls/src/private.rs b/crates/xen-ioctls/src/private.rs index 2d3379213e03..59a75f67f18d 100644 --- a/crates/xen-ioctls/src/private.rs +++ b/crates/xen-ioctls/src/private.rs @@ -14,8 +14,8 @@ use std::io::Error; use std::os::unix::io::AsRawFd; use vmm_sys_util::ioctl::_IOC_NONE;
+use crate::xdm::types::{PrivcmdDeviceModelIrqFd, PrivcmdDeviceModelOp}; use crate::xfm::types::{PrivCmdMmapBatchV2, PrivCmdMmapResource}; -use crate::xdm::types::PrivcmdDeviceModelOp;
pub const PAGE_SHIFT: u32 = 12; pub const PAGE_SIZE: u32 = 1 << PAGE_SHIFT; @@ -73,6 +73,18 @@ ioctl_ioc_nr!( std::mem::size_of::<PrivCmdMmapResource>() as u32 );
+/* + * #define IOCTL_PRIVCMD_IRQFD \ + * _IOC(_IOC_NONE, 'P', 8, sizeof(privcmd_irqfd_t)) + */ +ioctl_ioc_nr!( + IOCTL_PRIVCMD_IRQFD, + _IOC_NONE, + XEN_PRIVCMD_TYPE, + 8 as u32, + std::mem::size_of::<PrivcmdDeviceModelIrqFd>() as u32 +); + pub const HYPERCALL_EVTCHN: &str = "/dev/xen/evtchn"; pub const HYPERCALL_PRIVCMD: &str = "/dev/xen/privcmd"; pub const HYPERCALL_BUFFER_FILE: &str = "/dev/xen/hypercall"; diff --git a/crates/xen-ioctls/src/xdm/types.rs b/crates/xen-ioctls/src/xdm/types.rs index ea429558b852..b000a68f0cf1 100644 --- a/crates/xen-ioctls/src/xdm/types.rs +++ b/crates/xen-ioctls/src/xdm/types.rs @@ -143,3 +143,17 @@ impl PrivcmdDeviceModelOp { PrivcmdDeviceModelOp { domid, num, ubufs } } } + +pub const PRIVCMD_IRQFD_FLAG_DEASSIGN: u32 = 1; + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +// tools/include/xen-sys/Linux/privcmd.h::privcmd_irqfd +pub struct PrivcmdDeviceModelIrqFd { + pub dm_op: *mut c_void, + pub size: u32, + pub fd: u32, + pub flags: u32, + pub domid: u16, + pub pad: [u8; 2], +} diff --git a/crates/xen-ioctls/src/xdm/xdm.rs b/crates/xen-ioctls/src/xdm/xdm.rs index 115724284b7e..3aa5289794fe 100644 --- a/crates/xen-ioctls/src/xdm/xdm.rs +++ b/crates/xen-ioctls/src/xdm/xdm.rs @@ -15,6 +15,8 @@ use std::fs::{File, OpenOptions}; use std::io::Error; use std::os::unix::io::AsRawFd;
+use vmm_sys_util::eventfd::EventFd; + #[cfg(target_arch = "aarch64")] use crate::aarch64::types::*; #[cfg(target_arch = "x86_64")] @@ -239,4 +241,70 @@ impl XenDeviceModelHandle {
do_dm_op(&self.fd, domid, &mut privcmd_dm_op_buffers) } + + pub fn config_irqfd( + &self, + fd: EventFd, + domid: u16, + irq: u32, + level: u8, + flags: u32, + ) -> Result<(), std::io::Error> { + let mut dm_op = XenDeviceModelOp { + op: XEN_DMOP_set_irq_level, + pad: 0, + u: XenDeviceModelOpPayload { + xen_set_irq_level: XenDeviceModelSetIrqLevel { + irq, + level, + ..Default::default() + }, + }, + }; + + let mut irqfd = PrivcmdDeviceModelIrqFd { + dm_op: &mut dm_op as *mut _ as *mut c_void, + size: std::mem::size_of::<XenDeviceModelOp>() as u32, + fd: fd.as_raw_fd() as u32, + flags, + domid, + pad: [0; 2], + }; + + let ret = unsafe { + // The expression "&mut irqfd as *mut _" creates a reference to irqfd before casting it + // to a *mut c_void. + ioctl( + self.fd.as_raw_fd(), + IOCTL_PRIVCMD_IRQFD(), + &mut irqfd as *mut _ as *mut c_void, + ) + }; + + if ret < 0 { + return Err(Error::last_os_error()); + } + + Ok(()) + } + + pub fn set_irqfd( + &self, + fd: EventFd, + domid: u16, + irq: u32, + level: u8, + ) -> Result<(), std::io::Error> { + self.config_irqfd(fd, domid, irq, level, 0) + } + + pub fn clear_irqfd( + &self, + fd: EventFd, + domid: u16, + irq: u32, + level: u8, + ) -> Result<(), std::io::Error> { + self.config_irqfd(fd, domid, irq, level, PRIVCMD_IRQFD_FLAG_DEASSIGN) + } }
On Wed, Aug 23, 2023 at 12:19:09PM +0530, Viresh Kumar wrote:
This adds support to issue the set irqfd ioctl via the privcmd file.
Signed-off-by: Viresh Kumar viresh.kumar@linaro.org
V3:
- Based on changes merged in kernel.
V2:
- Fixed comment
crates/xen-ioctls/src/private.rs | 14 +++++- crates/xen-ioctls/src/xdm/types.rs | 14 ++++++ crates/xen-ioctls/src/xdm/xdm.rs | 68 ++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-)
Applied.
Thanks, Mathieu
diff --git a/crates/xen-ioctls/src/private.rs b/crates/xen-ioctls/src/private.rs index 2d3379213e03..59a75f67f18d 100644 --- a/crates/xen-ioctls/src/private.rs +++ b/crates/xen-ioctls/src/private.rs @@ -14,8 +14,8 @@ use std::io::Error; use std::os::unix::io::AsRawFd; use vmm_sys_util::ioctl::_IOC_NONE; +use crate::xdm::types::{PrivcmdDeviceModelIrqFd, PrivcmdDeviceModelOp}; use crate::xfm::types::{PrivCmdMmapBatchV2, PrivCmdMmapResource}; -use crate::xdm::types::PrivcmdDeviceModelOp; pub const PAGE_SHIFT: u32 = 12; pub const PAGE_SIZE: u32 = 1 << PAGE_SHIFT; @@ -73,6 +73,18 @@ ioctl_ioc_nr!( std::mem::size_of::<PrivCmdMmapResource>() as u32 ); +/*
- #define IOCTL_PRIVCMD_IRQFD \
_IOC(_IOC_NONE, 'P', 8, sizeof(privcmd_irqfd_t))
- */
+ioctl_ioc_nr!(
- IOCTL_PRIVCMD_IRQFD,
- _IOC_NONE,
- XEN_PRIVCMD_TYPE,
- 8 as u32,
- std::mem::size_of::<PrivcmdDeviceModelIrqFd>() as u32
+);
pub const HYPERCALL_EVTCHN: &str = "/dev/xen/evtchn"; pub const HYPERCALL_PRIVCMD: &str = "/dev/xen/privcmd"; pub const HYPERCALL_BUFFER_FILE: &str = "/dev/xen/hypercall"; diff --git a/crates/xen-ioctls/src/xdm/types.rs b/crates/xen-ioctls/src/xdm/types.rs index ea429558b852..b000a68f0cf1 100644 --- a/crates/xen-ioctls/src/xdm/types.rs +++ b/crates/xen-ioctls/src/xdm/types.rs @@ -143,3 +143,17 @@ impl PrivcmdDeviceModelOp { PrivcmdDeviceModelOp { domid, num, ubufs } } }
+pub const PRIVCMD_IRQFD_FLAG_DEASSIGN: u32 = 1;
+#[repr(C)] +#[derive(Debug, Copy, Clone)] +// tools/include/xen-sys/Linux/privcmd.h::privcmd_irqfd +pub struct PrivcmdDeviceModelIrqFd {
- pub dm_op: *mut c_void,
- pub size: u32,
- pub fd: u32,
- pub flags: u32,
- pub domid: u16,
- pub pad: [u8; 2],
+} diff --git a/crates/xen-ioctls/src/xdm/xdm.rs b/crates/xen-ioctls/src/xdm/xdm.rs index 115724284b7e..3aa5289794fe 100644 --- a/crates/xen-ioctls/src/xdm/xdm.rs +++ b/crates/xen-ioctls/src/xdm/xdm.rs @@ -15,6 +15,8 @@ use std::fs::{File, OpenOptions}; use std::io::Error; use std::os::unix::io::AsRawFd; +use vmm_sys_util::eventfd::EventFd;
#[cfg(target_arch = "aarch64")] use crate::aarch64::types::*; #[cfg(target_arch = "x86_64")] @@ -239,4 +241,70 @@ impl XenDeviceModelHandle { do_dm_op(&self.fd, domid, &mut privcmd_dm_op_buffers) }
- pub fn config_irqfd(
&self,
fd: EventFd,
domid: u16,
irq: u32,
level: u8,
flags: u32,
- ) -> Result<(), std::io::Error> {
let mut dm_op = XenDeviceModelOp {
op: XEN_DMOP_set_irq_level,
pad: 0,
u: XenDeviceModelOpPayload {
xen_set_irq_level: XenDeviceModelSetIrqLevel {
irq,
level,
..Default::default()
},
},
};
let mut irqfd = PrivcmdDeviceModelIrqFd {
dm_op: &mut dm_op as *mut _ as *mut c_void,
size: std::mem::size_of::<XenDeviceModelOp>() as u32,
fd: fd.as_raw_fd() as u32,
flags,
domid,
pad: [0; 2],
};
let ret = unsafe {
// The expression "&mut irqfd as *mut _" creates a reference to irqfd before casting it
// to a *mut c_void.
ioctl(
self.fd.as_raw_fd(),
IOCTL_PRIVCMD_IRQFD(),
&mut irqfd as *mut _ as *mut c_void,
)
};
if ret < 0 {
return Err(Error::last_os_error());
}
Ok(())
- }
- pub fn set_irqfd(
&self,
fd: EventFd,
domid: u16,
irq: u32,
level: u8,
- ) -> Result<(), std::io::Error> {
self.config_irqfd(fd, domid, irq, level, 0)
- }
- pub fn clear_irqfd(
&self,
fd: EventFd,
domid: u16,
irq: u32,
level: u8,
- ) -> Result<(), std::io::Error> {
self.config_irqfd(fd, domid, irq, level, PRIVCMD_IRQFD_FLAG_DEASSIGN)
- }
}
2.31.1.272.g89b43f80a514
stratos-dev@op-lists.linaro.org