Removes a guest physical memory region. # Safety `userspace_addr` must point to `memory_size` bytes of memory, and `add_user_memory_region()` must have been successfully called.
(
&self,
slot: u32,
guest_phys_addr: u64,
memory_size: usize,
userspace_addr: *mut u8,
readonly: bool,
log_dirty_pages: bool,
)
| 1120 | /// `userspace_addr` must point to `memory_size` bytes of memory, |
| 1121 | /// and `add_user_memory_region()` must have been successfully called. |
| 1122 | unsafe fn remove_user_memory_region( |
| 1123 | &self, |
| 1124 | slot: u32, |
| 1125 | guest_phys_addr: u64, |
| 1126 | memory_size: usize, |
| 1127 | userspace_addr: *mut u8, |
| 1128 | readonly: bool, |
| 1129 | log_dirty_pages: bool, |
| 1130 | ) -> vm::Result<()> { |
| 1131 | let mut flags = 0; |
| 1132 | if readonly { |
| 1133 | flags |= KVM_MEM_READONLY; |
| 1134 | } |
| 1135 | if log_dirty_pages { |
| 1136 | flags |= KVM_MEM_LOG_DIRTY_PAGES; |
| 1137 | } |
| 1138 | |
| 1139 | const _: () = assert!(core::mem::size_of::<usize>() <= core::mem::size_of::<u64>()); |
| 1140 | |
| 1141 | let mut region = kvm_userspace_memory_region2 { |
| 1142 | slot, |
| 1143 | guest_phys_addr, |
| 1144 | memory_size: memory_size as u64, |
| 1145 | userspace_addr: userspace_addr as usize as u64, |
| 1146 | flags, |
| 1147 | ..Default::default() |
| 1148 | }; |
| 1149 | |
| 1150 | // Remove the corresponding entry from "self.dirty_log_slots" if needed |
| 1151 | self.dirty_log_slots.write().unwrap().remove(®ion.slot); |
| 1152 | |
| 1153 | // Setting the size to 0 means "remove" |
| 1154 | region.memory_size = 0; |
| 1155 | // SAFETY: Safe because caller promised this is safe. |
| 1156 | unsafe { |
| 1157 | self.set_user_memory_region(region) |
| 1158 | .map_err(|e| vm::HypervisorVmError::RemoveUserMemory(e.into()))?; |
| 1159 | } |
| 1160 | |
| 1161 | // Close the per-region guest_memfd if one was created for this slot |
| 1162 | if let Some(slots) = &self.memory_slots { |
| 1163 | slots.write().unwrap().remove(&slot); |
| 1164 | } |
| 1165 | |
| 1166 | Ok(()) |
| 1167 | } |
| 1168 | |
| 1169 | /// |
| 1170 | /// Returns the preferred CPU target type which can be emulated by KVM on underlying host. |
no test coverage detected