mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-01-03 03:15:20 +00:00
vmm, hypervisor: introduce and use make_user_memory_region
This removes the last KVM-ism from memory_manager. Also make use of that method in other places. Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
parent
8d97d628c3
commit
cfa758fbb1
@ -172,6 +172,25 @@ impl vm::Vm for KvmVm {
|
||||
.map_err(|e| vm::HypervisorVmError::SetGsiRouting(e.into()))
|
||||
}
|
||||
///
|
||||
/// Creates a memory region structure that can be used with set_user_memory_region
|
||||
///
|
||||
fn make_user_memory_region(
|
||||
&self,
|
||||
slot: u32,
|
||||
guest_phys_addr: u64,
|
||||
memory_size: u64,
|
||||
userspace_addr: u64,
|
||||
readonly: bool,
|
||||
) -> MemoryRegion {
|
||||
MemoryRegion {
|
||||
slot,
|
||||
guest_phys_addr,
|
||||
memory_size,
|
||||
userspace_addr,
|
||||
flags: if readonly { KVM_MEM_READONLY } else { 0 },
|
||||
}
|
||||
}
|
||||
///
|
||||
/// Creates/modifies a guest physical memory slot.
|
||||
///
|
||||
fn set_user_memory_region(&self, user_memory_region: MemoryRegion) -> vm::Result<()> {
|
||||
|
@ -143,6 +143,15 @@ pub trait Vm: Send + Sync {
|
||||
fn unregister_ioevent(&self, fd: &EventFd, addr: &IoEventAddress) -> Result<()>;
|
||||
/// Sets the GSI routing table entries, overwriting any previously set
|
||||
fn set_gsi_routing(&self, irq_routing: &IrqRouting) -> Result<()>;
|
||||
/// Creates a memory region structure that can be used with set_user_memory_region
|
||||
fn make_user_memory_region(
|
||||
&self,
|
||||
slot: u32,
|
||||
guest_phys_addr: u64,
|
||||
memory_size: u64,
|
||||
userspace_addr: u64,
|
||||
readonly: bool,
|
||||
) -> MemoryRegion;
|
||||
/// Creates/modifies a guest physical memory slot.
|
||||
fn set_user_memory_region(&self, user_memory_region: MemoryRegion) -> Result<()>;
|
||||
/// Creates an emulated device in the kernel.
|
||||
|
@ -588,13 +588,13 @@ impl DeviceRelocation for AddressManager {
|
||||
if let Some(mut shm_regions) = virtio_dev.get_shm_regions() {
|
||||
if shm_regions.addr.raw_value() == old_base {
|
||||
// Remove old region from KVM by passing a size of 0.
|
||||
let mut mem_region = hypervisor::MemoryRegion {
|
||||
slot: shm_regions.mem_slot,
|
||||
guest_phys_addr: old_base,
|
||||
memory_size: 0,
|
||||
userspace_addr: shm_regions.host_addr,
|
||||
flags: 0,
|
||||
};
|
||||
let mut mem_region = self.vm.make_user_memory_region(
|
||||
shm_regions.mem_slot,
|
||||
old_base,
|
||||
0,
|
||||
shm_regions.host_addr,
|
||||
false,
|
||||
);
|
||||
|
||||
self.vm.set_user_memory_region(mem_region).map_err(|e| {
|
||||
io::Error::new(
|
||||
|
@ -676,17 +676,13 @@ impl MemoryManager {
|
||||
readonly: bool,
|
||||
) -> Result<u32, Error> {
|
||||
let slot = self.allocate_memory_slot();
|
||||
let mem_region = hypervisor::MemoryRegion {
|
||||
let mem_region = self.vm.make_user_memory_region(
|
||||
slot,
|
||||
guest_phys_addr,
|
||||
memory_size,
|
||||
userspace_addr,
|
||||
flags: if readonly {
|
||||
hypervisor::kvm::KVM_MEM_READONLY
|
||||
} else {
|
||||
0
|
||||
},
|
||||
};
|
||||
readonly,
|
||||
);
|
||||
|
||||
self.vm
|
||||
.set_user_memory_region(mem_region)
|
||||
@ -733,13 +729,13 @@ impl MemoryManager {
|
||||
mergeable: bool,
|
||||
slot: u32,
|
||||
) -> Result<(), Error> {
|
||||
let mem_region = hypervisor::MemoryRegion {
|
||||
let mem_region = self.vm.make_user_memory_region(
|
||||
slot,
|
||||
guest_phys_addr,
|
||||
memory_size: 0,
|
||||
0, /* memory_size -- using 0 removes this slot */
|
||||
userspace_addr,
|
||||
flags: 0,
|
||||
};
|
||||
false, /* readonly -- don't care */
|
||||
);
|
||||
|
||||
self.vm
|
||||
.set_user_memory_region(mem_region)
|
||||
|
@ -1451,13 +1451,13 @@ pub fn test_vm() {
|
||||
let vm = hv.create_vm().expect("new VM creation failed");
|
||||
|
||||
mem.with_regions(|index, region| {
|
||||
let mem_region = hypervisor::MemoryRegion {
|
||||
slot: index as u32,
|
||||
guest_phys_addr: region.start_addr().raw_value(),
|
||||
memory_size: region.len() as u64,
|
||||
userspace_addr: region.as_ptr() as u64,
|
||||
flags: 0,
|
||||
};
|
||||
let mem_region = vm.make_user_memory_region(
|
||||
index as u32,
|
||||
region.start_addr().raw_value(),
|
||||
region.len() as u64,
|
||||
region.as_ptr() as u64,
|
||||
false,
|
||||
);
|
||||
|
||||
vm.set_user_memory_region(mem_region)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user