Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup usage of cfg(target_arch = "...") in doc tests #293

Merged
merged 8 commits into from
Nov 4, 2024
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Upcoming Release

### Added

- [[#288](https://github.com/rust-vmm/kvm-ioctls/pull/288)]: Introduce `Cap::GuestMemfd`, `Cap::MemoryAttributes` and
`Cap::UserMemory2` capabilities enum variants for use with `VmFd::check_extension`.

## v0.19.0

### Added
Expand Down
3 changes: 3 additions & 0 deletions src/cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,7 @@ pub enum Cap {
ExitHypercall = KVM_CAP_EXIT_HYPERCALL,
#[cfg(target_arch = "x86_64")]
MemoryFaultInfo = KVM_CAP_MEMORY_FAULT_INFO,
UserMemory2 = KVM_CAP_USER_MEMORY2,
GuestMemfd = KVM_CAP_GUEST_MEMFD,
MemoryAttributes = KVM_CAP_MEMORY_ATTRIBUTES,
}
106 changes: 53 additions & 53 deletions src/ioctls/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,33 @@ impl DeviceFd {
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
///
/// #[cfg(not(target_arch = "riscv64"))]
/// {
/// # use kvm_bindings::{
/// # kvm_device_type_KVM_DEV_TYPE_VFIO,
/// # KVM_DEV_VFIO_GROUP, KVM_DEV_VFIO_GROUP_ADD, KVM_CREATE_DEVICE_TEST
/// # };
/// let mut device = kvm_bindings::kvm_create_device {
/// type_: kvm_device_type_KVM_DEV_TYPE_VFIO,
/// fd: 0,
/// flags: KVM_CREATE_DEVICE_TEST,
/// };
/// # #[cfg(not(target_arch = "riscv64"))]
/// # {
/// # use kvm_bindings::{
/// # kvm_device_type_KVM_DEV_TYPE_VFIO,
/// # KVM_DEV_VFIO_GROUP, KVM_DEV_VFIO_GROUP_ADD, KVM_CREATE_DEVICE_TEST
/// # };
/// let mut device = kvm_bindings::kvm_create_device {
/// type_: kvm_device_type_KVM_DEV_TYPE_VFIO,
/// fd: 0,
/// flags: KVM_CREATE_DEVICE_TEST,
/// };
///
/// let device_fd = vm
/// .create_device(&mut device)
/// .expect("Cannot create KVM device");
/// let device_fd = vm
/// .create_device(&mut device)
/// .expect("Cannot create KVM device");
///
/// let dist_attr = kvm_bindings::kvm_device_attr {
/// group: KVM_DEV_VFIO_GROUP,
/// attr: u64::from(KVM_DEV_VFIO_GROUP_ADD),
/// addr: 0x0,
/// flags: 0,
/// };
/// let dist_attr = kvm_bindings::kvm_device_attr {
/// group: KVM_DEV_VFIO_GROUP,
/// attr: u64::from(KVM_DEV_VFIO_GROUP_ADD),
/// addr: 0x0,
/// flags: 0,
/// };
///
/// if (device_fd.has_device_attr(&dist_attr).is_ok()) {
/// device_fd.set_device_attr(&dist_attr).unwrap();
/// }
/// if (device_fd.has_device_attr(&dist_attr).is_ok()) {
/// device_fd.set_device_attr(&dist_attr).unwrap();
/// }
/// # }
/// ```
pub fn set_device_attr(&self, device_attr: &kvm_device_attr) -> Result<()> {
// SAFETY: We are calling this function with a Device fd, and we trust the kernel.
Expand Down Expand Up @@ -120,46 +120,46 @@ impl DeviceFd {
/// including that it is safe to write to the `addr` member.
///
/// # Examples
///
/// Getting the number of IRQs for a GICv2 device on an aarch64 platform
///
/// ```rust
/// # extern crate kvm_ioctls;
/// # extern crate kvm_bindings;
/// # use kvm_ioctls::Kvm;
///
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
///
/// // As on x86_64, `get_device_attr` is not necessarily needed. Therefore here
/// // the code example is only for AArch64.
/// #[cfg(any(target_arch = "aarch64"))]
/// {
/// use kvm_bindings::{
/// kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2, kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V3,
/// KVM_DEV_ARM_VGIC_GRP_NR_IRQS,
/// };
/// # #[cfg(target_arch = "aarch64")]
/// # {
/// use kvm_bindings::{
/// kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2, kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V3,
/// KVM_DEV_ARM_VGIC_GRP_NR_IRQS,
/// };
///
/// // Create a GIC device.
/// let mut gic_device = kvm_bindings::kvm_create_device {
/// type_: kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V3,
/// fd: 0,
/// flags: 0,
/// };
/// let device_fd = match vm.create_device(&mut gic_device) {
/// Ok(fd) => fd,
/// Err(_) => {
/// gic_device.type_ = kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2;
/// vm.create_device(&mut gic_device)
/// .expect("Cannot create KVM vGIC device")
/// }
/// };
/// // Create a GIC device.
/// let mut gic_device = kvm_bindings::kvm_create_device {
/// type_: kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V3,
/// fd: 0,
/// flags: 0,
/// };
/// let device_fd = match vm.create_device(&mut gic_device) {
/// Ok(fd) => fd,
/// Err(_) => {
/// gic_device.type_ = kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2;
/// vm.create_device(&mut gic_device)
/// .expect("Cannot create KVM vGIC device")
/// }
/// };
///
/// let mut data: u32 = 0;
/// let mut gic_attr = kvm_bindings::kvm_device_attr::default();
/// gic_attr.group = KVM_DEV_ARM_VGIC_GRP_NR_IRQS;
/// gic_attr.addr = &mut data as *mut u32 as u64;
/// let mut data: u32 = 0;
/// let mut gic_attr = kvm_bindings::kvm_device_attr::default();
/// gic_attr.group = KVM_DEV_ARM_VGIC_GRP_NR_IRQS;
/// gic_attr.addr = &mut data as *mut u32 as u64;
///
/// // SAFETY: gic_attr.addr is safe to write to.
/// unsafe { device_fd.get_device_attr(&mut gic_attr) }.unwrap();
/// }
/// // SAFETY: gic_attr.addr is safe to write to.
/// unsafe { device_fd.get_device_attr(&mut gic_attr) }.unwrap();
/// # }
/// ```
pub unsafe fn get_device_attr(&self, device_attr: &mut kvm_device_attr) -> Result<()> {
// SAFETY: Caller has ensured device_attr.addr is safe to write to.
Expand Down
8 changes: 4 additions & 4 deletions src/ioctls/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ impl Kvm {
/// // Check that the VM mmap size is the same reported by `KVM_GET_VCPU_MMAP_SIZE`.
/// assert!(vm.run_size() == kvm.get_vcpu_mmap_size().unwrap());
/// ```
#[cfg(not(any(target_arch = "aarch64")))]
#[cfg(not(target_arch = "aarch64"))]
pub fn create_vm(&self) -> Result<VmFd> {
self.create_vm_with_type(0) // Create using default VM type
}
Expand All @@ -574,7 +574,7 @@ impl Kvm {
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// // Check that the VM mmap size is the same reported by `KVM_GET_VCPU_MMAP_SIZE`.
/// assert!(vm.run_size() == kvm.get_vcpu_mmap_size().unwrap());
/// assert_eq!(vm.run_size(), kvm.get_vcpu_mmap_size().unwrap());
/// ```
#[cfg(target_arch = "aarch64")]
pub fn create_vm(&self) -> Result<VmFd> {
Expand Down Expand Up @@ -612,7 +612,7 @@ impl Kvm {
/// let host_ipa_limit = kvm.get_host_ipa_limit();
/// let vm = kvm.create_vm_with_ipa_size(host_ipa_limit as u32).unwrap();
/// // Check that the VM mmap size is the same reported by `KVM_GET_VCPU_MMAP_SIZE`.
/// assert!(vm.run_size() == kvm.get_vcpu_mmap_size().unwrap());
/// assert_eq!(vm.run_size(), kvm.get_vcpu_mmap_size().unwrap());
/// }
/// ```
#[cfg(target_arch = "aarch64")]
Expand All @@ -635,7 +635,7 @@ impl Kvm {
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm_with_type(0).unwrap();
/// // Check that the VM mmap size is the same reported by `KVM_GET_VCPU_MMAP_SIZE`.
/// assert!(vm.run_size() == kvm.get_vcpu_mmap_size().unwrap());
/// assert_eq!(vm.run_size(), kvm.get_vcpu_mmap_size().unwrap());
/// ```
pub fn create_vm_with_type(&self, vm_type: u64) -> Result<VmFd> {
// SAFETY: Safe because we know `self.kvm` is a real KVM fd as this module is the only one
Expand Down
Loading