Skip to content

Commit

Permalink
Add Cap::KvmCapStealTime capability
Browse files Browse the repository at this point in the history
This capability is used for kvm steal time feature.

Signed-off-by: xuejun-xj <[email protected]>
  • Loading branch information
xuejun-xj committed Oct 13, 2023
1 parent 998887e commit de24816
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
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())
}
}
}

0 comments on commit de24816

Please sign in to comment.