mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-01-30 04:19:20 +00:00
kvm: add dirty log support
This add safe support for KVM's KVM_GET_DIRTY_LOG ioctl. TEST=./build_test BUG=None Change-Id: I3d0f996927844a33addd072f2bfc62361f8b7fe0 Reviewed-on: https://chromium-review.googlesource.com/848019 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
This commit is contained in:
parent
20c3c2af2f
commit
855ac29cf2
3 changed files with 130 additions and 19 deletions
|
@ -17,7 +17,7 @@ use std::collections::hash_map::Entry;
|
||||||
use std::os::raw::*;
|
use std::os::raw::*;
|
||||||
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
||||||
|
|
||||||
use libc::{open, O_RDWR, O_CLOEXEC, EINVAL, ENOSPC, ENOENT};
|
use libc::{open, sysconf, O_RDWR, O_CLOEXEC, EINVAL, ENOSPC, ENOENT, _SC_PAGESIZE};
|
||||||
|
|
||||||
use kvm_sys::*;
|
use kvm_sys::*;
|
||||||
|
|
||||||
|
@ -32,21 +32,28 @@ fn errno_result<T>() -> Result<T> {
|
||||||
Err(Error::last())
|
Err(Error::last())
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn set_user_memory_region<F: AsRawFd>(fd: &F, slot: u32, guest_addr: u64, memory_size: u64, userspace_addr: u64) -> Result<()> {
|
unsafe fn set_user_memory_region<F: AsRawFd>(fd: &F,
|
||||||
|
slot: u32,
|
||||||
|
log_dirty_pages: bool,
|
||||||
|
guest_addr: u64,
|
||||||
|
memory_size: u64,
|
||||||
|
userspace_addr: u64)
|
||||||
|
-> Result<()> {
|
||||||
|
let flags = if log_dirty_pages {
|
||||||
|
KVM_MEM_LOG_DIRTY_PAGES
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
let region = kvm_userspace_memory_region {
|
let region = kvm_userspace_memory_region {
|
||||||
slot: slot,
|
slot: slot,
|
||||||
flags: 0,
|
flags,
|
||||||
guest_phys_addr: guest_addr,
|
guest_phys_addr: guest_addr,
|
||||||
memory_size: memory_size,
|
memory_size: memory_size,
|
||||||
userspace_addr: userspace_addr,
|
userspace_addr: userspace_addr,
|
||||||
};
|
};
|
||||||
|
|
||||||
let ret = ioctl_with_ref(fd, KVM_SET_USER_MEMORY_REGION(), ®ion);
|
let ret = ioctl_with_ref(fd, KVM_SET_USER_MEMORY_REGION(), ®ion);
|
||||||
if ret == 0 {
|
if ret == 0 { Ok(()) } else { errno_result() }
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
errno_result()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A wrapper around opening and using `/dev/kvm`.
|
/// A wrapper around opening and using `/dev/kvm`.
|
||||||
|
@ -170,7 +177,7 @@ impl Vm {
|
||||||
guest_mem.with_regions(|index, guest_addr, size, host_addr| {
|
guest_mem.with_regions(|index, guest_addr, size, host_addr| {
|
||||||
unsafe {
|
unsafe {
|
||||||
// Safe because the guest regions are guaranteed not to overlap.
|
// Safe because the guest regions are guaranteed not to overlap.
|
||||||
set_user_memory_region(&vm_file, index as u32,
|
set_user_memory_region(&vm_file, index as u32, false,
|
||||||
guest_addr.offset() as u64,
|
guest_addr.offset() as u64,
|
||||||
size as u64,
|
size as u64,
|
||||||
host_addr as u64)
|
host_addr as u64)
|
||||||
|
@ -196,9 +203,13 @@ impl Vm {
|
||||||
///
|
///
|
||||||
/// Note that memory inserted into the VM's address space must not overlap with any other memory
|
/// Note that memory inserted into the VM's address space must not overlap with any other memory
|
||||||
/// slot's region.
|
/// slot's region.
|
||||||
|
///
|
||||||
|
/// If `log_dirty_pages` is true, the slot number can be used to retrieve the pages written to
|
||||||
|
/// by the guest with `get_dirty_log`.
|
||||||
pub fn add_device_memory(&mut self,
|
pub fn add_device_memory(&mut self,
|
||||||
guest_addr: GuestAddress,
|
guest_addr: GuestAddress,
|
||||||
mem: MemoryMapping)
|
mem: MemoryMapping,
|
||||||
|
log_dirty_pages: bool)
|
||||||
-> Result<u32> {
|
-> Result<u32> {
|
||||||
if guest_addr < self.guest_mem.end_addr() {
|
if guest_addr < self.guest_mem.end_addr() {
|
||||||
return Err(Error::new(ENOSPC));
|
return Err(Error::new(ENOSPC));
|
||||||
|
@ -219,7 +230,7 @@ impl Vm {
|
||||||
// this. We take ownership of the memory mapping so that it won't be unmapped until the slot
|
// this. We take ownership of the memory mapping so that it won't be unmapped until the slot
|
||||||
// is removed.
|
// is removed.
|
||||||
unsafe {
|
unsafe {
|
||||||
set_user_memory_region(&self.vm, slot,
|
set_user_memory_region(&self.vm, slot, log_dirty_pages,
|
||||||
guest_addr.offset() as u64,
|
guest_addr.offset() as u64,
|
||||||
mem.size() as u64,
|
mem.size() as u64,
|
||||||
mem.as_ptr() as u64)?;
|
mem.as_ptr() as u64)?;
|
||||||
|
@ -237,7 +248,7 @@ impl Vm {
|
||||||
Entry::Occupied(entry) => {
|
Entry::Occupied(entry) => {
|
||||||
// Safe because the slot is checked against the list of device memory slots.
|
// Safe because the slot is checked against the list of device memory slots.
|
||||||
unsafe {
|
unsafe {
|
||||||
set_user_memory_region(&self.vm, slot, 0, 0, 0)?;
|
set_user_memory_region(&self.vm, slot, false, 0, 0, 0)?;
|
||||||
}
|
}
|
||||||
// Because `mem_slot_gaps` is a max-heap, but we want to pop the min slots, we
|
// Because `mem_slot_gaps` is a max-heap, but we want to pop the min slots, we
|
||||||
// negate the slot value before insertion.
|
// negate the slot value before insertion.
|
||||||
|
@ -248,6 +259,35 @@ impl Vm {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the bitmap of dirty pages since the last call to `get_dirty_log` for the memory at
|
||||||
|
/// `slot`.
|
||||||
|
///
|
||||||
|
/// The size of `dirty_log` must be at least as many bits as there are pages in the memory
|
||||||
|
/// region `slot` represents. For example, if the size of `slot` is 16 pages, `dirty_log` must
|
||||||
|
/// be 2 bytes or greater.
|
||||||
|
pub fn get_dirty_log(&self, slot: u32, dirty_log: &mut [u8]) -> Result<()> {
|
||||||
|
let page_size = unsafe { sysconf(_SC_PAGESIZE) } as usize;
|
||||||
|
match self.device_memory.get(&slot) {
|
||||||
|
Some(mmap) => {
|
||||||
|
// Ensures that there are as many bits in dirty_log as there are pages in the mmap.
|
||||||
|
if (mmap.size() / page_size) > (dirty_log.len() << 3) {
|
||||||
|
return Err(Error::new(-EINVAL));
|
||||||
|
}
|
||||||
|
let mut dirty_log_kvm = kvm_dirty_log {
|
||||||
|
slot,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
dirty_log_kvm.__bindgen_anon_1.dirty_bitmap = dirty_log.as_ptr() as *mut c_void;
|
||||||
|
// Safe because the `dirty_bitmap` pointer assigned above is guaranteed to be valid
|
||||||
|
// (because it's from a slice) and we checked that it will be large enough to hold
|
||||||
|
// the entire log.
|
||||||
|
let ret = unsafe { ioctl_with_ref(self, KVM_GET_DIRTY_LOG(), &dirty_log_kvm) };
|
||||||
|
if ret == 0 { Ok(()) } else { errno_result() }
|
||||||
|
}
|
||||||
|
_ => Err(Error::new(-ENOENT)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets a reference to the guest memory owned by this VM.
|
/// Gets a reference to the guest memory owned by this VM.
|
||||||
///
|
///
|
||||||
/// Note that `GuestMemory` does not include any device memory that may have been added after
|
/// Note that `GuestMemory` does not include any device memory that may have been added after
|
||||||
|
@ -779,7 +819,7 @@ mod tests {
|
||||||
let mut vm = Vm::new(&kvm, gm).unwrap();
|
let mut vm = Vm::new(&kvm, gm).unwrap();
|
||||||
let mem_size = 0x1000;
|
let mem_size = 0x1000;
|
||||||
let mem = MemoryMapping::new(mem_size).unwrap();
|
let mem = MemoryMapping::new(mem_size).unwrap();
|
||||||
vm.add_device_memory(GuestAddress(0x1000), mem).unwrap();
|
vm.add_device_memory(GuestAddress(0x1000), mem, false).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -790,7 +830,7 @@ mod tests {
|
||||||
let mem_size = 0x1000;
|
let mem_size = 0x1000;
|
||||||
let mem = MemoryMapping::new(mem_size).unwrap();
|
let mem = MemoryMapping::new(mem_size).unwrap();
|
||||||
let mem_ptr = mem.as_ptr();
|
let mem_ptr = mem.as_ptr();
|
||||||
let slot = vm.add_device_memory(GuestAddress(0x1000), mem).unwrap();
|
let slot = vm.add_device_memory(GuestAddress(0x1000), mem, false).unwrap();
|
||||||
let mem = vm.remove_device_memory(slot).unwrap();
|
let mem = vm.remove_device_memory(slot).unwrap();
|
||||||
assert_eq!(mem.size(), mem_size);
|
assert_eq!(mem.size(), mem_size);
|
||||||
assert_eq!(mem.as_ptr(), mem_ptr);
|
assert_eq!(mem.as_ptr(), mem_ptr);
|
||||||
|
@ -811,7 +851,7 @@ mod tests {
|
||||||
let mut vm = Vm::new(&kvm, gm).unwrap();
|
let mut vm = Vm::new(&kvm, gm).unwrap();
|
||||||
let mem_size = 0x2000;
|
let mem_size = 0x2000;
|
||||||
let mem = MemoryMapping::new(mem_size).unwrap();
|
let mem = MemoryMapping::new(mem_size).unwrap();
|
||||||
assert!(vm.add_device_memory(GuestAddress(0x2000), mem).is_err());
|
assert!(vm.add_device_memory(GuestAddress(0x2000), mem, false).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
70
kvm/tests/dirty_log.rs
Normal file
70
kvm/tests/dirty_log.rs
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
// Copyright 2017 The Chromium OS Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||||
|
|
||||||
|
extern crate sys_util;
|
||||||
|
extern crate kvm_sys;
|
||||||
|
extern crate kvm;
|
||||||
|
|
||||||
|
use kvm::*;
|
||||||
|
use kvm_sys::kvm_regs;
|
||||||
|
use sys_util::{GuestAddress, GuestMemory, SharedMemory, MemoryMapping};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_run() {
|
||||||
|
/*
|
||||||
|
0000 881C mov [si],bl
|
||||||
|
0002 F4 hlt
|
||||||
|
*/
|
||||||
|
let code = [0x88, 0x1c, 0xf4];
|
||||||
|
let mem_size = 0x10000;
|
||||||
|
let load_addr = GuestAddress(0x1000);
|
||||||
|
let guest_mem = GuestMemory::new(&[]).unwrap();
|
||||||
|
let mut mem = SharedMemory::new(None).expect("failed to create shared memory");
|
||||||
|
mem.set_size(mem_size)
|
||||||
|
.expect("failed to set shared memory size");
|
||||||
|
let mmap = MemoryMapping::from_fd(&mem, mem_size as usize)
|
||||||
|
.expect("failed to create memory mapping");
|
||||||
|
|
||||||
|
mmap.write_slice(&code[..], load_addr.offset())
|
||||||
|
.expect("Writing code to memory failed.");
|
||||||
|
|
||||||
|
let kvm = Kvm::new().expect("new kvm failed");
|
||||||
|
let mut vm = Vm::new(&kvm, guest_mem).expect("new vm failed");
|
||||||
|
let vcpu = Vcpu::new(0, &kvm, &vm).expect("new vcpu failed");
|
||||||
|
let mut vcpu_sregs = vcpu.get_sregs().expect("get sregs failed");
|
||||||
|
vcpu_sregs.cs.base = 0;
|
||||||
|
vcpu_sregs.cs.selector = 0;
|
||||||
|
vcpu.set_sregs(&vcpu_sregs).expect("set sregs failed");
|
||||||
|
|
||||||
|
let mut vcpu_regs: kvm_regs = unsafe { std::mem::zeroed() };
|
||||||
|
vcpu_regs.rip = load_addr.offset() as u64;
|
||||||
|
vcpu_regs.rflags = 2;
|
||||||
|
// Write 0x12 to the beginning of the 9th page.
|
||||||
|
vcpu_regs.rsi = 0x8000;
|
||||||
|
vcpu_regs.rbx = 0x12;
|
||||||
|
vcpu.set_regs(&vcpu_regs).expect("set regs failed");
|
||||||
|
let slot = vm.add_device_memory(GuestAddress(0),
|
||||||
|
MemoryMapping::from_fd(&mem, mem_size as usize)
|
||||||
|
.expect("failed to create memory mapping"),
|
||||||
|
true)
|
||||||
|
.expect("failed to register memory");
|
||||||
|
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match vcpu.run().expect("run failed") {
|
||||||
|
VcpuExit::Hlt => break,
|
||||||
|
r => panic!("unexpected exit reason: {:?}", r),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut dirty_log = [0x0, 0x0];
|
||||||
|
vm.get_dirty_log(slot, &mut dirty_log[..])
|
||||||
|
.expect("failed to get dirty log");
|
||||||
|
// Tests the 9th page was written to.
|
||||||
|
assert_eq!(dirty_log[1], 0x1);
|
||||||
|
assert_eq!(mmap.read_obj::<u64>(vcpu_regs.rsi as usize).unwrap(),
|
||||||
|
vcpu_regs.rbx);
|
||||||
|
}
|
|
@ -204,10 +204,11 @@ impl VmRequest {
|
||||||
_ => return VmResponse::Err(SysError::new(-EINVAL)),
|
_ => return VmResponse::Err(SysError::new(-EINVAL)),
|
||||||
};
|
};
|
||||||
let pfn = *next_mem_pfn;
|
let pfn = *next_mem_pfn;
|
||||||
let slot = match vm.add_device_memory(GuestAddress((pfn << 12) as usize), mmap) {
|
let slot =
|
||||||
Ok(slot) => slot,
|
match vm.add_device_memory(GuestAddress((pfn << 12) as usize), mmap, false) {
|
||||||
Err(e) => return VmResponse::Err(e),
|
Ok(slot) => slot,
|
||||||
};
|
Err(e) => return VmResponse::Err(e),
|
||||||
|
};
|
||||||
// TODO(zachr): Use a smarter allocation strategy. The current strategy is just
|
// TODO(zachr): Use a smarter allocation strategy. The current strategy is just
|
||||||
// bumping this pointer, meaning the remove operation does not free any address
|
// bumping this pointer, meaning the remove operation does not free any address
|
||||||
// space. Given enough allocations, device memory may run out of address space and
|
// space. Given enough allocations, device memory may run out of address space and
|
||||||
|
|
Loading…
Reference in a new issue