mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-10-02 11:35:46 +00:00
vmm: device_manager: Use MemoryManager to create KVM mapping
Use the newly exported funtionality to reduce the amount of duplicated code. Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
parent
962dec2913
commit
a551398135
@ -12,13 +12,13 @@
|
|||||||
extern crate vm_device;
|
extern crate vm_device;
|
||||||
|
|
||||||
use crate::config::{ConsoleOutputMode, VmConfig};
|
use crate::config::{ConsoleOutputMode, VmConfig};
|
||||||
use crate::memory_manager::MemoryManager;
|
use crate::memory_manager::{Error as MemoryManagerError, MemoryManager};
|
||||||
use crate::vm::VmInfo;
|
use crate::vm::VmInfo;
|
||||||
#[cfg(feature = "acpi")]
|
#[cfg(feature = "acpi")]
|
||||||
use acpi_tables::{aml, aml::Aml};
|
use acpi_tables::{aml, aml::Aml};
|
||||||
use arch::layout;
|
use arch::layout;
|
||||||
use devices::{ioapic, HotPlugNotificationType};
|
use devices::{ioapic, HotPlugNotificationType};
|
||||||
use kvm_bindings::{kvm_irq_routing_entry, kvm_userspace_memory_region};
|
use kvm_bindings::kvm_irq_routing_entry;
|
||||||
use kvm_ioctls::*;
|
use kvm_ioctls::*;
|
||||||
use libc::O_TMPFILE;
|
use libc::O_TMPFILE;
|
||||||
use libc::TIOCGWINSZ;
|
use libc::TIOCGWINSZ;
|
||||||
@ -180,6 +180,9 @@ pub enum DeviceManagerError {
|
|||||||
|
|
||||||
// Failed to make hotplug notification
|
// Failed to make hotplug notification
|
||||||
HotPlugNotification(io::Error),
|
HotPlugNotification(io::Error),
|
||||||
|
|
||||||
|
// Error from a memory manager operation
|
||||||
|
MemoryManager(MemoryManagerError),
|
||||||
}
|
}
|
||||||
pub type DeviceManagerResult<T> = result::Result<T, DeviceManagerError>;
|
pub type DeviceManagerResult<T> = result::Result<T, DeviceManagerError>;
|
||||||
|
|
||||||
@ -1070,15 +1073,16 @@ impl DeviceManager {
|
|||||||
|
|
||||||
mmap_regions.push((addr, fs_cache as usize));
|
mmap_regions.push((addr, fs_cache as usize));
|
||||||
|
|
||||||
let mem_region = kvm_userspace_memory_region {
|
memory_manager
|
||||||
slot: memory_manager.lock().unwrap().allocate_kvm_memory_slot(),
|
.lock()
|
||||||
guest_phys_addr: fs_guest_addr.raw_value(),
|
.unwrap()
|
||||||
memory_size: fs_cache,
|
.create_userspace_mapping(
|
||||||
userspace_addr: addr as u64,
|
fs_guest_addr.raw_value(),
|
||||||
flags: 0,
|
fs_cache,
|
||||||
};
|
addr as u64,
|
||||||
// Safe because the guest regions are guaranteed not to overlap.
|
false,
|
||||||
let _ = unsafe { vm_info.vm_fd.set_user_memory_region(mem_region) };
|
)
|
||||||
|
.map_err(DeviceManagerError::MemoryManager)?;
|
||||||
|
|
||||||
let mut region_list = Vec::new();
|
let mut region_list = Vec::new();
|
||||||
region_list.push(VirtioSharedMemory {
|
region_list.push(VirtioSharedMemory {
|
||||||
@ -1173,40 +1177,16 @@ impl DeviceManager {
|
|||||||
|
|
||||||
mmap_regions.push((addr, size as usize));
|
mmap_regions.push((addr, size as usize));
|
||||||
|
|
||||||
let mem_region = kvm_userspace_memory_region {
|
memory_manager
|
||||||
slot: memory_manager.lock().unwrap().allocate_kvm_memory_slot(),
|
.lock()
|
||||||
guest_phys_addr: pmem_guest_addr.raw_value(),
|
.unwrap()
|
||||||
memory_size: size,
|
.create_userspace_mapping(
|
||||||
userspace_addr: addr as u64,
|
pmem_guest_addr.raw_value(),
|
||||||
flags: 0,
|
size,
|
||||||
};
|
addr as u64,
|
||||||
// Safe because the guest regions are guaranteed not to overlap.
|
pmem_cfg.mergeable,
|
||||||
let _ = unsafe { vm_info.vm_fd.set_user_memory_region(mem_region) };
|
)
|
||||||
|
.map_err(DeviceManagerError::MemoryManager)?;
|
||||||
// Mark the pages as mergeable if explicitly asked for.
|
|
||||||
if pmem_cfg.mergeable {
|
|
||||||
// Safe because the address and size are valid since the
|
|
||||||
// mmap succeeded..
|
|
||||||
let ret = unsafe {
|
|
||||||
libc::madvise(
|
|
||||||
addr as *mut libc::c_void,
|
|
||||||
size as libc::size_t,
|
|
||||||
libc::MADV_MERGEABLE,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
if ret != 0 {
|
|
||||||
let err = io::Error::last_os_error();
|
|
||||||
// Safe to unwrap because the error is constructed with
|
|
||||||
// last_os_error(), which ensures the output will be Some().
|
|
||||||
let errno = err.raw_os_error().unwrap();
|
|
||||||
if errno == libc::EINVAL {
|
|
||||||
warn!("kernel not configured with CONFIG_KSM");
|
|
||||||
} else {
|
|
||||||
warn!("madvise error: {}", err);
|
|
||||||
}
|
|
||||||
warn!("failed to mark pages as mergeable");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let virtio_pmem_device = Arc::new(Mutex::new(
|
let virtio_pmem_device = Arc::new(Mutex::new(
|
||||||
vm_virtio::Pmem::new(file, pmem_guest_addr, size as GuestUsize, pmem_cfg.iommu)
|
vm_virtio::Pmem::new(file, pmem_guest_addr, size as GuestUsize, pmem_cfg.iommu)
|
||||||
|
Loading…
Reference in New Issue
Block a user