feat(hvf): map/unmap guest memory

Signed-off-by: Changyuan Lyu <changyuanl@google.com>
This commit is contained in:
Changyuan Lyu 2024-06-23 15:26:55 -07:00 committed by Lencerf
parent f0719ff1c4
commit 57c5667c12
2 changed files with 42 additions and 9 deletions

View file

@ -14,6 +14,8 @@
use std::os::raw::c_void;
use bitflags::bitflags;
use crate::arch::reg::EsrEl2;
use crate::c_enum;
@ -43,10 +45,22 @@ pub struct HvVcpuExit {
pub exception: HvVcpuExitException,
}
bitflags! {
#[derive(Debug, Clone, Copy, Default)]
#[repr(transparent)]
pub struct HvMemoryFlag: u64 {
const READ = 1 << 0;
const WRITE = 1 << 1;
const EXEC = 1 << 2;
}
}
#[link(name = "Hypervisor", kind = "framework")]
extern "C" {
pub fn hv_vm_create(config: *mut i32) -> i32;
pub fn hv_vm_destroy() -> i32;
pub fn hv_vcpu_create(vcpu: &mut u64, exit: &mut *mut HvVcpuExit, config: *mut c_void) -> i32;
pub fn hv_vcpu_destroy(vcpu: u64) -> i32;
pub fn hv_vm_map(addr: *const u8, ipa: u64, size: usize, flags: HvMemoryFlag) -> i32;
pub fn hv_vm_unmap(ipa: u64, size: usize) -> i32;
}

View file

@ -20,7 +20,9 @@ use std::thread::JoinHandle;
use parking_lot::Mutex;
use snafu::ResultExt;
use crate::hv::hvf::bindings::{hv_vcpu_create, hv_vm_destroy};
use crate::hv::hvf::bindings::{
hv_vcpu_create, hv_vm_destroy, hv_vm_map, hv_vm_unmap, HvMemoryFlag,
};
use crate::hv::hvf::check_ret;
use crate::hv::hvf::vcpu::HvfVcpu;
use crate::hv::{
@ -36,25 +38,42 @@ impl VmMemory for HvfMemory {
unimplemented!()
}
fn max_mem_slots(&self) -> Result<u32> {
unimplemented!()
error::Capability { cap: "MaxMemSlots" }.fail()
}
fn mem_map(
&self,
_slot: u32,
_gpa: u64,
_size: u64,
_hva: usize,
_option: MemMapOption,
gpa: u64,
size: u64,
hva: usize,
option: MemMapOption,
) -> Result<()> {
unimplemented!()
if option.log_dirty {
return error::Capability { cap: "log dirty" }.fail();
}
let mut flags = HvMemoryFlag::empty();
if option.read {
flags |= HvMemoryFlag::READ;
}
if option.write {
flags |= HvMemoryFlag::WRITE;
}
if option.exec {
flags |= HvMemoryFlag::EXEC;
}
let ret = unsafe { hv_vm_map(hva as *const u8, gpa, size as usize, flags) };
check_ret(ret).context(error::GuestMap { hva, gpa, size })?;
Ok(())
}
fn register_encrypted_range(&self, _range: &[u8]) -> Result<()> {
unimplemented!()
}
fn unmap(&self, _slot: u32, _gpa: u64, _size: u64) -> Result<()> {
unimplemented!()
fn unmap(&self, _slot: u32, gpa: u64, size: u64) -> Result<()> {
let ret = unsafe { hv_vm_unmap(gpa, size as usize) };
check_ret(ret).context(error::GuestUnmap { gpa, size })?;
Ok(())
}
fn mark_private_memory(&self, _gpa: u64, _size: u64, _private: bool) -> Result<()> {