# Safety `userspace_addr` and `memory_size` must be and remain valid until `remove_userspace_mapping` is called.
(
&mut self,
guest_phys_addr: u64,
memory_size: usize,
userspace_addr: *mut u8,
mergeable: bool,
readonly: bool,
log_dirty: bool,
)
| 2286 | /// `userspace_addr` and `memory_size` must be and remain valid |
| 2287 | /// until `remove_userspace_mapping` is called. |
| 2288 | pub unsafe fn create_userspace_mapping( |
| 2289 | &mut self, |
| 2290 | guest_phys_addr: u64, |
| 2291 | memory_size: usize, |
| 2292 | userspace_addr: *mut u8, |
| 2293 | mergeable: bool, |
| 2294 | readonly: bool, |
| 2295 | log_dirty: bool, |
| 2296 | ) -> Result<u32, Error> { |
| 2297 | let slot = self.allocate_memory_slot(); |
| 2298 | |
| 2299 | info!( |
| 2300 | "Creating userspace mapping: {guest_phys_addr:x} -> {userspace_addr_:x} {memory_size:x}, slot {slot}", |
| 2301 | userspace_addr_ = userspace_addr as u64 |
| 2302 | ); |
| 2303 | |
| 2304 | // SAFETY: caller promises parameters are correct. |
| 2305 | unsafe { |
| 2306 | self.vm |
| 2307 | .create_user_memory_region( |
| 2308 | slot, |
| 2309 | guest_phys_addr, |
| 2310 | memory_size, |
| 2311 | userspace_addr, |
| 2312 | readonly, |
| 2313 | log_dirty, |
| 2314 | ) |
| 2315 | .map_err(Error::CreateUserMemoryRegion)?; |
| 2316 | } |
| 2317 | |
| 2318 | // SAFETY: the address and size are valid since the |
| 2319 | // mmap succeeded. |
| 2320 | let ret = unsafe { |
| 2321 | libc::madvise( |
| 2322 | userspace_addr.cast(), |
| 2323 | memory_size as libc::size_t, |
| 2324 | libc::MADV_DONTDUMP, |
| 2325 | ) |
| 2326 | }; |
| 2327 | if ret != 0 { |
| 2328 | let e = io::Error::last_os_error(); |
| 2329 | warn!("Failed to mark mapping as MADV_DONTDUMP: {e}"); |
| 2330 | } |
| 2331 | |
| 2332 | // Mark the pages as mergeable if explicitly asked for. |
| 2333 | if mergeable { |
| 2334 | // SAFETY: the address and size are valid since the |
| 2335 | // mmap succeeded. |
| 2336 | let ret = unsafe { |
| 2337 | libc::madvise( |
| 2338 | userspace_addr.cast(), |
| 2339 | memory_size as libc::size_t, |
| 2340 | libc::MADV_MERGEABLE, |
| 2341 | ) |
| 2342 | }; |
| 2343 | if ret != 0 { |
| 2344 | let err = io::Error::last_os_error(); |
| 2345 | // Safe to unwrap because the error is constructed with |
no test coverage detected