Skip to content

Commit

Permalink
Merge pull request #99 from Freax13/fix/unmap-kvm-run
Browse files Browse the repository at this point in the history
unmap KVM run block
  • Loading branch information
Freax13 authored Nov 13, 2024
2 parents c1766a1 + d011d8b commit ca57941
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions host/mushroom/src/insecure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ fn run_kernel_vcpu(
ap.set_cpuid(&cpuid_entries).unwrap();

let kvm_run = ap.get_kvm_run_block().unwrap();
let kvm_run = kvm_run.as_ptr();

let mut sregs = ap.get_sregs().unwrap();
sregs.es = KvmSegment::DATA64;
Expand Down
25 changes: 21 additions & 4 deletions host/mushroom/src/kvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

use std::{
array::{self, from_fn},
ffi::c_void,
fmt,
fs::OpenOptions,
mem::{size_of, size_of_val},
num::{NonZeroU32, NonZeroUsize},
os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd},
ptr::NonNull,
};

use anyhow::{ensure, Context, Result};
Expand Down Expand Up @@ -827,8 +829,7 @@ impl VcpuHandle {
Ok(())
}

pub fn get_kvm_run_block(&self) -> Result<VolatilePtr<KvmRun>> {
// FIXME: unmap the memory
pub fn get_kvm_run_block(&self) -> Result<KvmRunBox> {
let res = unsafe {
nix::sys::mman::mmap(
None,
Expand All @@ -840,8 +841,7 @@ impl VcpuHandle {
)
};
let ptr = res.context("failed to map vcpu kvm_run block")?;
let ptr = unsafe { VolatilePtr::new(ptr.cast()) };
Ok(ptr)
Ok(KvmRunBox { ptr })
}

/// Returns `true` if the cpu ran uninterrupted or returns `false` if the
Expand All @@ -863,6 +863,23 @@ impl VcpuHandle {
}
}

pub struct KvmRunBox {
ptr: NonNull<c_void>,
}

impl KvmRunBox {
pub fn as_ptr(&self) -> VolatilePtr<'_, KvmRun> {
unsafe { VolatilePtr::new(self.ptr.cast()) }
}
}

impl Drop for KvmRunBox {
fn drop(&mut self) {
let res = unsafe { nix::sys::mman::munmap(self.ptr, size_of::<KvmRun>()) };
res.unwrap();
}
}

#[derive(Clone, Copy, Debug, Pod, Zeroable)]
#[repr(C)]
pub struct KvmRegs {
Expand Down
2 changes: 2 additions & 0 deletions host/mushroom/src/snp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ impl VmContext {
pub fn run_supervisor(&mut self) -> Result<MushroomResult> {
let mut output = Vec::new();
let kvm_run = self.bsp.get_kvm_run_block()?;
let kvm_run = kvm_run.as_ptr();

loop {
let exit = kvm_run.read().exit();
Expand Down Expand Up @@ -414,6 +415,7 @@ impl VmContext {
ap.set_msr(DEBUG_CTL, 1).unwrap();

let kvm_run = ap.get_kvm_run_block().unwrap();
let kvm_run = kvm_run.as_ptr();

std::thread::park();

Expand Down
1 change: 1 addition & 0 deletions host/mushroom/src/tdx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ impl VmContext {
sender: &Sender<OutputEvent>,
) -> Result<()> {
let kvm_run = bsp.get_kvm_run_block()?;
let kvm_run = kvm_run.as_ptr();

while !done.load(Ordering::Relaxed) {
let exit = kvm_run.read().exit();
Expand Down

0 comments on commit ca57941

Please sign in to comment.