Skip to content

Commit de24816

Browse files
committed
Add Cap::KvmCapStealTime capability
This capability is used for kvm steal time feature. Signed-off-by: xuejun-xj <[email protected]>
1 parent 998887e commit de24816

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
## Changed
66
- [[#234](https://github.com/rust-vmm/kvm-ioctls/issues/234)] vcpu: export
77
reg_size as a public method.
8+
- [[#239](https://github.com/rust-vmm/kvm-ioctls/pull/239)] Add Cap::KvmCapStealTime
9+
capability.
810

911
# v0.15.0
1012

src/cap.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,6 @@ pub enum Cap {
162162
ArmPtrAuthAddress = KVM_CAP_ARM_PTRAUTH_ADDRESS,
163163
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
164164
ArmPtrAuthGeneric = KVM_CAP_ARM_PTRAUTH_GENERIC,
165+
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
166+
KvmCapStealTime = KVM_CAP_STEAL_TIME,
165167
}

src/ioctls/vm.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,4 +2251,48 @@ mod tests {
22512251
assert!(vm.register_enc_memory_region(&memory_region).is_ok());
22522252
assert!(vm.unregister_enc_memory_region(&memory_region).is_ok());
22532253
}
2254+
2255+
#[test]
2256+
#[cfg(target_arch = "aarch64")]
2257+
fn test_kvm_steal_time_capability() {
2258+
let kvm = Kvm::new().unwrap();
2259+
let vm = kvm.create_vm().unwrap();
2260+
let vcpu = vm.create_vcpu(0).unwrap();
2261+
create_gic_device(&vm, 0);
2262+
2263+
if kvm.check_extension(Cap::KvmCapStealTime) {
2264+
const PVTIME_REGION_SIZE: u64 = 0x10000;
2265+
const TEST_KVM_MEM_SLOT: u32 = 0;
2266+
const TEST_IPA: u64 = 0x20000000;
2267+
let hva = unsafe {
2268+
libc::mmap(
2269+
std::ptr::null_mut(),
2270+
PVTIME_REGION_SIZE as usize,
2271+
libc::PROT_READ | libc::PROT_WRITE,
2272+
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
2273+
0,
2274+
0,
2275+
)
2276+
} as *const u8 as u64;
2277+
2278+
let mem_region = kvm_bindings::kvm_userspace_memory_region {
2279+
slot: TEST_KVM_MEM_SLOT,
2280+
guest_phys_addr: TEST_IPA,
2281+
memory_size: PVTIME_REGION_SIZE,
2282+
userspace_addr: hva,
2283+
flags: 0,
2284+
};
2285+
unsafe {
2286+
vm.set_user_memory_region(mem_region).unwrap();
2287+
}
2288+
2289+
let device_attribute = kvm_device_attr {
2290+
group: KVM_ARM_VCPU_PVTIME_CTRL,
2291+
attr: KVM_ARM_VCPU_PVTIME_IPA as u64,
2292+
addr: &TEST_IPA as *const _ as u64,
2293+
..Default::default()
2294+
};
2295+
assert!(vcpu.set_device_attr(&device_attribute).is_ok())
2296+
}
2297+
}
22542298
}

0 commit comments

Comments
 (0)