Skip to content

Commit

Permalink
riscv64: Introduce basic ioctls
Browse files Browse the repository at this point in the history
Enable g/set ioctls of `kvm_mp_state`, `kvm_one_reg`;
register/unregister of `irq_fd`; `get_reg_list`, `signal_msi`,
`irq_line` and `set_gsi_routing`.

Signed-off-by: Ruoqing He <[email protected]>
  • Loading branch information
TimePrinciple committed Sep 26, 2024
1 parent f6711e8 commit 7af51e7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
10 changes: 6 additions & 4 deletions src/ioctls/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use vmm_sys_util::ioctl::{ioctl, ioctl_with_mut_ref, ioctl_with_ref};
use vmm_sys_util::ioctl::{ioctl_with_mut_ptr, ioctl_with_ptr, ioctl_with_val};

/// Helper method to obtain the size of the register through its id
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(any(target_arch = "arm", target_arch = "aarch64", target_arch = "riscv64"))]
pub fn reg_size(reg_id: u64) -> usize {
2_usize.pow(((reg_id & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT) as u32)
}
Expand Down Expand Up @@ -796,6 +796,7 @@ impl VcpuFd {
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "s390x"
))]
pub fn get_mp_state(&self) -> Result<kvm_mp_state> {
Expand Down Expand Up @@ -834,6 +835,7 @@ impl VcpuFd {
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "s390x"
))]
pub fn set_mp_state(&self, mp_state: kvm_mp_state) -> Result<()> {
Expand Down Expand Up @@ -1227,7 +1229,7 @@ impl VcpuFd {
/// vcpu.get_reg_list(&mut reg_list).unwrap();
/// assert!(reg_list.as_fam_struct_ref().n > 0);
/// ```
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(any(target_arch = "arm", target_arch = "aarch64", target_arch = "riscv64"))]
pub fn get_reg_list(&self, reg_list: &mut RegList) -> Result<()> {
let ret =
// SAFETY: This is safe because we allocated the struct and we trust the kernel will read
Expand Down Expand Up @@ -1305,7 +1307,7 @@ impl VcpuFd {
///
/// `data` should be equal or bigger then the register size
/// oterwise function will return EINVAL error
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(any(target_arch = "arm", target_arch = "aarch64", target_arch = "riscv64"))]
pub fn set_one_reg(&self, reg_id: u64, data: &[u8]) -> Result<usize> {
let reg_size = reg_size(reg_id);
if data.len() < reg_size {
Expand Down Expand Up @@ -1337,7 +1339,7 @@ impl VcpuFd {
///
/// `data` should be equal or bigger then the register size
/// oterwise function will return EINVAL error
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(any(target_arch = "arm", target_arch = "aarch64", target_arch = "riscv64"))]
pub fn get_one_reg(&self, reg_id: u64, data: &mut [u8]) -> Result<usize> {
let reg_size = reg_size(reg_id);
if data.len() < reg_size {
Expand Down
18 changes: 12 additions & 6 deletions src/ioctls/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,8 @@ impl VmFd {
target_arch = "x86",
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64"
target_arch = "aarch64",
target_arch = "riscv64"
))]
pub fn signal_msi(&self, msi: kvm_msi) -> Result<c_int> {
// SAFETY: Safe because we allocated the structure and we know the kernel
Expand Down Expand Up @@ -625,7 +626,8 @@ impl VmFd {
target_arch = "x86",
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64"
target_arch = "aarch64",
target_arch = "riscv64"
))]
pub fn set_gsi_routing(&self, irq_routing: &kvm_irq_routing) -> Result<()> {
// SAFETY: Safe because we allocated the structure and we know the kernel
Expand Down Expand Up @@ -959,7 +961,8 @@ impl VmFd {
target_arch = "x86",
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64"
target_arch = "aarch64",
target_arch = "riscv64"
))]
pub fn register_irqfd(&self, fd: &EventFd, gsi: u32) -> Result<()> {
let irqfd = kvm_irqfd {
Expand Down Expand Up @@ -1011,7 +1014,8 @@ impl VmFd {
target_arch = "x86",
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64"
target_arch = "aarch64",
target_arch = "riscv64"
))]
pub fn register_irqfd_with_resample(
&self,
Expand Down Expand Up @@ -1070,7 +1074,8 @@ impl VmFd {
target_arch = "x86",
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64"
target_arch = "aarch64",
target_arch = "riscv64"
))]
pub fn unregister_irqfd(&self, fd: &EventFd, gsi: u32) -> Result<()> {
let irqfd = kvm_irqfd {
Expand Down Expand Up @@ -1141,7 +1146,8 @@ impl VmFd {
target_arch = "x86",
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64"
target_arch = "aarch64",
target_arch = "riscv64"
))]
pub fn set_irq_line(&self, irq: u32, active: bool) -> Result<()> {
let mut irq_level = kvm_irq_level::default();
Expand Down
18 changes: 12 additions & 6 deletions src/kvm_ioctls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ ioctl_io_nr!(KVM_CREATE_IRQCHIP, KVMIO, 0x60);
target_arch = "x86",
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64"
target_arch = "aarch64",
target_arch = "riscv64"
))]
ioctl_iow_nr!(KVM_IRQ_LINE, KVMIO, 0x61, kvm_irq_level);
/* Available with KVM_CAP_COALESCED_MMIO / KVM_CAP_COALESCED_PIO */
Expand All @@ -88,7 +89,8 @@ ioctl_iow_nr!(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64"
target_arch = "aarch64",
target_arch = "riscv64"
))]
ioctl_iow_nr!(KVM_SET_GSI_ROUTING, KVMIO, 0x6a, kvm_irq_routing);
/* Available with KVM_CAP_IRQFD */
Expand All @@ -97,6 +99,7 @@ ioctl_iow_nr!(KVM_SET_GSI_ROUTING, KVMIO, 0x6a, kvm_irq_routing);
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "s390x"
))]
ioctl_iow_nr!(KVM_IRQFD, KVMIO, 0x76, kvm_irqfd);
Expand Down Expand Up @@ -182,6 +185,7 @@ ioctl_iowr_nr!(KVM_GET_CPUID2, KVMIO, 0x91, kvm_cpuid2);
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "s390x"
))]
ioctl_ior_nr!(KVM_GET_MP_STATE, KVMIO, 0x98, kvm_mp_state);
Expand All @@ -191,6 +195,7 @@ ioctl_ior_nr!(KVM_GET_MP_STATE, KVMIO, 0x98, kvm_mp_state);
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "s390x"
))]
ioctl_iow_nr!(KVM_SET_MP_STATE, KVMIO, 0x99, kvm_mp_state);
Expand Down Expand Up @@ -250,19 +255,20 @@ ioctl_iow_nr!(KVM_ENABLE_CAP, KVMIO, 0xa3, kvm_enable_cap);
target_arch = "x86",
target_arch = "x86_64",
target_arch = "arm",
target_arch = "aarch64"
target_arch = "aarch64",
target_arch = "riscv64"
))]
ioctl_iow_nr!(KVM_SIGNAL_MSI, KVMIO, 0xa5, kvm_msi);
/* Available with KVM_CAP_ONE_REG */
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(any(target_arch = "arm", target_arch = "aarch64", target_arch = "riscv64"))]
ioctl_iow_nr!(KVM_GET_ONE_REG, KVMIO, 0xab, kvm_one_reg);
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(any(target_arch = "arm", target_arch = "aarch64", target_arch = "riscv64"))]
ioctl_iow_nr!(KVM_SET_ONE_REG, KVMIO, 0xac, kvm_one_reg);
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
ioctl_iow_nr!(KVM_ARM_VCPU_INIT, KVMIO, 0xae, kvm_vcpu_init);
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
ioctl_ior_nr!(KVM_ARM_PREFERRED_TARGET, KVMIO, 0xaf, kvm_vcpu_init);
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(any(target_arch = "arm", target_arch = "aarch64", target_arch = "riscv64"))]
ioctl_iowr_nr!(KVM_GET_REG_LIST, KVMIO, 0xb0, kvm_reg_list);

/* Available with KVM_CAP_X86_SMM */
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ mod ioctls;
pub use cap::Cap;
pub use ioctls::device::DeviceFd;
pub use ioctls::system::Kvm;
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(any(target_arch = "arm", target_arch = "aarch64", target_arch = "riscv64"))]
pub use ioctls::vcpu::reg_size;
pub use ioctls::vcpu::{HypercallExit, VcpuExit, VcpuFd};

Expand Down

0 comments on commit 7af51e7

Please sign in to comment.