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

cap: add KvmCapStealTime capability #239

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
## Changed
- [[#234](https://github.com/rust-vmm/kvm-ioctls/issues/234)] vcpu: export
reg_size as a public method.
- [[#239](https://github.com/rust-vmm/kvm-ioctls/pull/239)] Add Cap::KvmCapStealTime
capability.

# v0.15.0

Expand Down
2 changes: 2 additions & 0 deletions src/cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,6 @@ pub enum Cap {
ArmPtrAuthAddress = KVM_CAP_ARM_PTRAUTH_ADDRESS,
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
ArmPtrAuthGeneric = KVM_CAP_ARM_PTRAUTH_GENERIC,
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
KvmCapStealTime = KVM_CAP_STEAL_TIME,
}
44 changes: 44 additions & 0 deletions src/ioctls/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2251,4 +2251,48 @@ mod tests {
assert!(vm.register_enc_memory_region(&memory_region).is_ok());
assert!(vm.unregister_enc_memory_region(&memory_region).is_ok());
}

#[test]
#[cfg(target_arch = "aarch64")]
fn test_kvm_steal_time_capability() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
create_gic_device(&vm, 0);

if kvm.check_extension(Cap::KvmCapStealTime) {
const PVTIME_REGION_SIZE: u64 = 0x10000;
const TEST_KVM_MEM_SLOT: u32 = 0;
const TEST_IPA: u64 = 0x20000000;
let hva = unsafe {
libc::mmap(
std::ptr::null_mut(),
PVTIME_REGION_SIZE as usize,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
0,
0,
)
} as *const u8 as u64;

let mem_region = kvm_bindings::kvm_userspace_memory_region {
slot: TEST_KVM_MEM_SLOT,
guest_phys_addr: TEST_IPA,
memory_size: PVTIME_REGION_SIZE,
userspace_addr: hva,
flags: 0,
};
unsafe {
vm.set_user_memory_region(mem_region).unwrap();
}

let device_attribute = kvm_device_attr {
group: KVM_ARM_VCPU_PVTIME_CTRL,
attr: KVM_ARM_VCPU_PVTIME_IPA as u64,
addr: &TEST_IPA as *const _ as u64,
..Default::default()
};
assert!(vcpu.set_device_attr(&device_attribute).is_ok())
}
}
}