Skip to content

Commit

Permalink
Use vm_memory crate for guest allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
BaderSZ authored and jounathaen committed Feb 19, 2024
1 parent c7462d4 commit 0f92f8e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
37 changes: 19 additions & 18 deletions src/linux/uhyve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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,
Expand Down Expand Up @@ -302,6 +306,7 @@ struct MmapMemory {
memory_size: usize,
guest_address: usize,
host_address: usize,
pub mmap: vm_memory::GuestMemoryMmap,
}

impl MmapMemory {
Expand All @@ -312,37 +317,33 @@ impl MmapMemory {
huge_pages: bool,
mergeable: bool,
) -> MmapMemory {
let host_address = unsafe {
mmap::<BorrowedFd<'_>>(
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,
}
}

Expand Down

0 comments on commit 0f92f8e

Please sign in to comment.