From 0d801c9cd255055e2ce4b8ba6e0abd818d15ff33 Mon Sep 17 00:00:00 2001 From: Keiichi Watanabe Date: Tue, 7 Dec 2021 20:31:48 +0900 Subject: [PATCH] devices: vfio: Avoid arch-dependent type conversion for IommuType Since ioctl_with_val takes c_ulong, whose size is 64-bit in x86 but 32-bit in ARM, we must not assume it can be converted to u64. BUG=none TEST=cargo check Change-Id: Ib0060bbbe2e06c94bf8802c8e775c808c07ff6a6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3320123 Reviewed-by: Alexandre Courbot Auto-Submit: Keiichi Watanabe Tested-by: kokoro Commit-Queue: Dennis Kempin --- devices/src/vfio.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/devices/src/vfio.rs b/devices/src/vfio.rs index c550f2f9b1..0c52bb550c 100644 --- a/devices/src/vfio.rs +++ b/devices/src/vfio.rs @@ -8,6 +8,7 @@ use std::ffi::CString; use std::fs::{File, OpenOptions}; use std::io; use std::mem; +use std::os::raw::c_ulong; use std::os::unix::prelude::FileExt; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -90,9 +91,10 @@ enum KvmVfioGroupOps { Delete, } +#[repr(u32)] enum IommuType { - Type1V2 = VFIO_TYPE1v2_IOMMU as isize, - NoIommu = VFIO_NOIOMMU_IOMMU as isize, + Type1V2 = VFIO_TYPE1v2_IOMMU, + NoIommu = VFIO_NOIOMMU_IOMMU, } /// VfioContainer contain multi VfioGroup, and delegate an IOMMU domain table @@ -140,13 +142,13 @@ impl VfioContainer { fn check_extension(&self, val: IommuType) -> bool { // Safe as file is vfio container and make sure val is valid. - let ret = unsafe { ioctl_with_val(self, VFIO_CHECK_EXTENSION(), val as u64) }; + let ret = unsafe { ioctl_with_val(self, VFIO_CHECK_EXTENSION(), val as c_ulong) }; ret == 1 } fn set_iommu(&self, val: IommuType) -> i32 { // Safe as file is vfio container and make sure val is valid. - unsafe { ioctl_with_val(self, VFIO_SET_IOMMU(), val as u64) } + unsafe { ioctl_with_val(self, VFIO_SET_IOMMU(), val as c_ulong) } } pub unsafe fn vfio_dma_map(