Skip to content

Commit

Permalink
removed GUEST_ADDRESS global static
Browse files Browse the repository at this point in the history
  • Loading branch information
jounathaen committed Dec 10, 2024
1 parent 5215dd8 commit e203338
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 33 deletions.
22 changes: 8 additions & 14 deletions src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use x86_64::structures::paging::{
PageTable, PageTableIndex,
};

use crate::{consts::PML4_OFFSET, mem::MmapMemory, paging::PagetableError};
use crate::{mem::MmapMemory, paging::PagetableError};

pub const RAM_START: GuestPhysAddr = GuestPhysAddr::new(0x00);

Expand Down Expand Up @@ -111,20 +111,16 @@ pub fn get_cpu_frequency_from_os() -> std::result::Result<u32, FrequencyDetectio
pub fn virt_to_phys(
addr: GuestVirtAddr,
mem: &MmapMemory,
pml4: GuestPhysAddr,
) -> Result<GuestPhysAddr, PagetableError> {
/// Number of Offset bits of a virtual address for a 4 KiB page, which are shifted away to get its Page Frame Number (PFN).
pub const PAGE_BITS: u64 = 12;

/// Number of bits of the index in each table (PML4, PDPT, PDT, PGT).
pub const PAGE_MAP_BITS: usize = 9;

let guest_address = (*crate::vm::GUEST_ADDRESS.get().unwrap()).as_u64();
let mut page_table = unsafe {
(mem.host_address(GuestPhysAddr::new(guest_address + PML4_OFFSET))
.unwrap() as *mut PageTable)
.as_mut()
}
.unwrap();
let mut page_table =
unsafe { (mem.host_address(pml4).unwrap() as *mut PageTable).as_mut() }.unwrap();
let mut page_bits = 39;
let mut entry = PageTableEntry::new();

Expand Down Expand Up @@ -252,9 +248,7 @@ mod tests {
.is_test(true)
.try_init();

use crate::vm::GUEST_ADDRESS;
let guest_address = GuestPhysAddr::new(0x11111000);
let _ = *GUEST_ADDRESS.get_or_init(|| guest_address);

let mem = MmapMemory::new(0, MIN_PHYSMEM_SIZE * 2, guest_address, true, true);
println!("mmap memory created {mem:x?}");
Expand All @@ -267,12 +261,12 @@ mod tests {

// Get the address of the first entry in PML4 (the address of the PML4 itself)
let virt_addr = GuestVirtAddr::new(0xFFFFFFFFFFFFF000);
let p_addr = virt_to_phys(virt_addr, &mem).unwrap();
let p_addr = virt_to_phys(virt_addr, &mem, guest_address + PML4_OFFSET).unwrap();
assert_eq!(p_addr, guest_address + PML4_OFFSET);

// The last entry on the PML4 is the address of the PML4 with flags
let virt_addr = GuestVirtAddr::new(0xFFFFFFFFFFFFF000 | (4096 - 8));
let p_addr = virt_to_phys(virt_addr, &mem).unwrap();
let p_addr = virt_to_phys(virt_addr, &mem, guest_address + PML4_OFFSET).unwrap();
assert_eq!(
mem.read::<u64>(p_addr).unwrap(),
(guest_address + PML4_OFFSET).as_u64()
Expand All @@ -281,15 +275,15 @@ mod tests {

// the first entry on the 3rd level entry in the pagetables is the address of the boot pdpte
let virt_addr = GuestVirtAddr::new(0xFFFFFFFFFFE00000);
let p_addr = virt_to_phys(virt_addr, &mem).unwrap();
let p_addr = virt_to_phys(virt_addr, &mem, guest_address + PML4_OFFSET).unwrap();
assert!(p_addr.as_u64() - guest_address.as_u64() >= PAGETABLES_OFFSET);
assert!(p_addr.as_u64() - guest_address.as_u64() <= PAGETABLES_END);

// the idx2 entry on the 2rd level entry in the pagetables is the address of the boot pde
let idx2 = GuestVirtAddr::new(guest_address.as_u64()).p2_index();
let virt_addr = GuestVirtAddr::new(0xFFFFFFFFC0000000)
+ u64::from(idx2) * size_of::<PageTableEntry>() as u64;
let p_addr = virt_to_phys(virt_addr, &mem).unwrap();
let p_addr = virt_to_phys(virt_addr, &mem, guest_address + PML4_OFFSET).unwrap();
assert!(p_addr.as_u64() - guest_address.as_u64() >= PAGETABLES_OFFSET);
assert!(p_addr.as_u64() - guest_address.as_u64() <= PAGETABLES_END);
// That address points to a huge page
Expand Down
7 changes: 4 additions & 3 deletions src/hypercall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ pub fn close(sysclose: &mut CloseParams) {
}

/// Handles an read syscall on the host.
pub fn read(mem: &MmapMemory, sysread: &mut ReadParams) {
pub fn read(mem: &MmapMemory, sysread: &mut ReadParams, root_pt: GuestPhysAddr) {
unsafe {
let bytes_read = libc::read(
sysread.fd,
mem.host_address(virt_to_phys(sysread.buf, mem).unwrap())
mem.host_address(virt_to_phys(sysread.buf, mem, root_pt).unwrap())
.unwrap() as *mut libc::c_void,
sysread.len,
);
Expand All @@ -166,11 +166,12 @@ pub fn read(mem: &MmapMemory, sysread: &mut ReadParams) {
pub fn write<B: VirtualizationBackend>(
parent_vm: &UhyveVm<B>,
syswrite: &WriteParams,
root_pt: GuestPhysAddr,
) -> io::Result<()> {
let mut bytes_written: usize = 0;
while bytes_written != syswrite.len {
let guest_phys_addr =
virt_to_phys(syswrite.buf + bytes_written as u64, &parent_vm.mem).unwrap();
virt_to_phys(syswrite.buf + bytes_written as u64, &parent_vm.mem, root_pt).unwrap();

if syswrite.fd == 1 {
// fd 0 is stdout
Expand Down
14 changes: 12 additions & 2 deletions src/linux/gdb/breakpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ impl target::ext::breakpoints::SwBreakpoint for GdbUhyve {
// Safety: mem is not altered during the lifetime of `instructions`
let instructions = unsafe {
self.vm.mem.slice_at_mut(
virt_to_phys(GuestVirtAddr::new(addr), &self.vm.mem).map_err(|_err| ())?,
virt_to_phys(
GuestVirtAddr::new(addr),
&self.vm.mem,
self.vcpu.get_root_pagetable(),
)
.map_err(|_err| ())?,
kind,
)
}
Expand All @@ -72,7 +77,12 @@ impl target::ext::breakpoints::SwBreakpoint for GdbUhyve {
// Safety: mem is not altered during the lifetime of `instructions`
let instructions = unsafe {
self.vm.mem.slice_at_mut(
virt_to_phys(GuestVirtAddr::new(addr), &self.vm.mem).map_err(|_err| ())?,
virt_to_phys(
GuestVirtAddr::new(addr),
&self.vm.mem,
self.vcpu.get_root_pagetable(),
)
.map_err(|_err| ())?,
kind,
)
}
Expand Down
10 changes: 8 additions & 2 deletions src/linux/gdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ impl SingleThreadBase for GdbUhyve {
// Safety: mem is copied to data before mem can be modified.
let src = unsafe {
self.vm.mem.slice_at(
virt_to_phys(guest_addr, &self.vm.mem).map_err(|_err| ())?,
virt_to_phys(guest_addr, &self.vm.mem, self.vcpu.get_root_pagetable())
.map_err(|_err| ())?,
data.len(),
)
}
Expand All @@ -146,7 +147,12 @@ impl SingleThreadBase for GdbUhyve {
// Safety: self.vm.mem is not altered during the lifetime of mem.
let mem = unsafe {
self.vm.mem.slice_at_mut(
virt_to_phys(GuestVirtAddr::new(start_addr), &self.vm.mem).map_err(|_err| ())?,
virt_to_phys(
GuestVirtAddr::new(start_addr),
&self.vm.mem,
self.vcpu.get_root_pagetable(),
)
.map_err(|_err| ())?,
data.len(),
)
}
Expand Down
24 changes: 16 additions & 8 deletions src/linux/x86_64/kvm_cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl VirtualizationBackend for KvmVm {
slot: 1,
flags: mem.flags,
memory_size: (mem.memory_size - KVM_32BIT_GAP_START - KVM_32BIT_GAP_SIZE) as u64,
guest_phys_addr: (*crate::vm::GUEST_ADDRESS.get().unwrap()).as_u64()
guest_phys_addr: mem.guest_address.as_u64()
+ (KVM_32BIT_GAP_START + KVM_32BIT_GAP_SIZE) as u64,
userspace_addr: (mem.host_address as usize
+ KVM_32BIT_GAP_START
Expand Down Expand Up @@ -340,6 +340,10 @@ impl KvmCpu {
&mut self.vcpu
}

pub fn get_root_pagetable(&self) -> GuestPhysAddr {
GuestPhysAddr::new(self.vcpu.get_sregs().unwrap().cr3)
}

fn init(
&mut self,
entry_point: GuestPhysAddr,
Expand Down Expand Up @@ -462,13 +466,17 @@ impl VirtualCPU for KvmCpu {
sysopen,
&mut self.parent_vm.file_mapping.lock().unwrap(),
),
Hypercall::FileRead(sysread) => {
hypercall::read(&self.parent_vm.mem, sysread)
}
Hypercall::FileWrite(syswrite) => {
hypercall::write(&self.parent_vm, syswrite)
.map_err(|_e| HypervisorError::new(libc::EFAULT))?
}
Hypercall::FileRead(sysread) => hypercall::read(
&self.parent_vm.mem,
sysread,
self.get_root_pagetable(),
),
Hypercall::FileWrite(syswrite) => hypercall::write(
&self.parent_vm,
syswrite,
self.get_root_pagetable(),
)
.map_err(|_e| HypervisorError::new(libc::EFAULT))?,
Hypercall::FileUnlink(sysunlink) => hypercall::unlink(
&self.parent_vm.mem,
sysunlink,
Expand Down
5 changes: 1 addition & 4 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
num::NonZeroU32,
path::PathBuf,
ptr, str,
sync::{Arc, Mutex, OnceLock},
sync::{Arc, Mutex},
time::SystemTime,
};

Expand Down Expand Up @@ -36,8 +36,6 @@ use crate::{

pub type HypervisorResult<T> = Result<T, HypervisorError>;

pub static GUEST_ADDRESS: OnceLock<GuestPhysAddr> = OnceLock::new();

#[derive(Error, Debug)]
pub enum LoadKernelError {
#[error(transparent)]
Expand Down Expand Up @@ -192,7 +190,6 @@ impl<VirtBackend: VirtualizationBackend> UhyveVm<VirtBackend> {

dbg!(GuestPhysAddr::new(offset));
dbg!(guest_address);
let _ = *GUEST_ADDRESS.get_or_init(|| guest_address);

#[cfg(target_os = "linux")]
#[cfg(target_arch = "x86_64")]
Expand Down

0 comments on commit e203338

Please sign in to comment.