From 0f92f8e49f6ddd75447b3a3e10a88ca6c67f31d5 Mon Sep 17 00:00:00 2001 From: Bader Zaidan Date: Thu, 6 Jul 2023 16:00:39 +0200 Subject: [PATCH] Use vm_memory crate for guest allocation --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/linux/uhyve.rs | 37 +++++++++++++++++++------------------ 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ade1d0e0..95ef8a61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1462,6 +1462,7 @@ dependencies = [ "tun-tap", "uhyve-interface", "virtio-bindings", + "vm-memory", "vmm-sys-util", "x86_64", "xhypervisor", @@ -1511,6 +1512,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "878bcb1b2812a10c30d53b0ed054999de3d98f25ece91fc173973f9c57aaae86" +[[package]] +name = "vm-memory" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dc276f0d00c17b9aeb584da0f1e1c673df0d183cc2539e3636ec8cbc5eae99b" +dependencies = [ + "libc", + "thiserror", + "winapi", +] + [[package]] name = "vmm-sys-util" version = "0.12.1" diff --git a/Cargo.toml b/Cargo.toml index d5bf6814..1604d0e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,7 @@ mac_address = "1.1" nix = { version = "0.27", features = ["mman", "pthread", "signal"] } tun-tap = { version = "0.1", default-features = false } virtio-bindings = { version = "0.2", features = ["virtio-v4_14_0"] } +vm-memory = { version = "0.12", features = ["backend-mmap"] } vmm-sys-util = "0.12" [target.'cfg(target_os = "macos")'.dependencies] diff --git a/src/linux/uhyve.rs b/src/linux/uhyve.rs index e636d639..f7fa9825 100755 --- a/src/linux/uhyve.rs +++ b/src/linux/uhyve.rs @@ -5,7 +5,7 @@ use std::{ cmp, ffi::OsString, fmt, mem, - os::{fd::BorrowedFd, raw::c_void}, + os::raw::c_void, path::{Path, PathBuf}, ptr, sync::{Arc, Mutex}, @@ -17,6 +17,10 @@ use kvm_ioctls::VmFd; use log::debug; use nix::sys::mman::*; use vmm_sys_util::eventfd::EventFd; +use vm_memory::{ + GuestAddress, GuestMemoryMmap, GuestMemoryRegion, GuestRegionMmap, MemoryRegionAddress, + MmapRegion, +}; use x86_64::{ structures::paging::{Page, PageTable, PageTableFlags, Size2MiB}, PhysAddr, @@ -302,6 +306,7 @@ struct MmapMemory { memory_size: usize, guest_address: usize, host_address: usize, + pub mmap: vm_memory::GuestMemoryMmap, } impl MmapMemory { @@ -312,37 +317,33 @@ impl MmapMemory { huge_pages: bool, mergeable: bool, ) -> MmapMemory { - let host_address = unsafe { - mmap::>( - None, - memory_size.try_into().unwrap(), - ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, - MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS | MapFlags::MAP_NORESERVE, - None, - 0, - ) - .expect("mmap failed") - }; + let regionmap = GuestRegionMmap::new( + MmapRegion::new(memory_size).unwrap(), + GuestAddress(guest_address), + ) + .unwrap(); if mergeable { debug!("Enable kernel feature to merge same pages"); - unsafe { - madvise(host_address, memory_size, MmapAdvise::MADV_MERGEABLE).unwrap(); - } + // TODO: implement flags + assert!(regionmap.flags() | MmapAdvise::MADV_MERGEABLE as i32 != 0); } if huge_pages { debug!("Uhyve uses huge pages"); - unsafe { - madvise(host_address, memory_size, MmapAdvise::MADV_HUGEPAGE).unwrap(); - } + // TODO: implement flags + assert!(regionmap.flags() | MmapAdvise::MADV_HUGEPAGE as i32 != 0); } + let host_address = regionmap.get_host_address(MemoryRegionAddress(0)).unwrap(); + let mmap = GuestMemoryMmap::from_regions(vec![regionmap]).unwrap(); + MmapMemory { flags, memory_size, guest_address: guest_address as usize, host_address: host_address as usize, + mmap, } }