Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vm): change UhyveVm struct parameters #755

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/linux/x86_64/kvm_cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,14 @@ impl VirtualCPU for KvmCpu {
hypercall::address_to_hypercall(&self.parent_vm.mem, port, data_addr)
} {
match hypercall {
Hypercall::Cmdsize(syssize) => syssize
.update(self.parent_vm.kernel_path(), self.parent_vm.args()),
Hypercall::Cmdsize(syssize) => syssize.update(
self.parent_vm.kernel_path(),
&self.parent_vm.params.kernel_args,
),
Hypercall::Cmdval(syscmdval) => {
hypercall::copy_argv(
self.parent_vm.kernel_path().as_os_str(),
self.parent_vm.args(),
&self.parent_vm.params.kernel_args,
syscmdval,
&self.parent_vm.mem,
);
Expand Down
8 changes: 5 additions & 3 deletions src/macos/aarch64/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,14 @@ impl VirtualCPU for XhyveCpu {
Hypercall::Exit(sysexit) => {
return Ok(VcpuStopReason::Exit(sysexit.arg));
}
Hypercall::Cmdsize(syssize) => syssize
.update(self.parent_vm.kernel_path(), self.parent_vm.args()),
Hypercall::Cmdsize(syssize) => syssize.update(
self.parent_vm.kernel_path(),
&self.parent_vm.params.kernel_args,
),
Hypercall::Cmdval(syscmdval) => {
copy_argv(
self.parent_vm.kernel_path().as_os_str(),
self.parent_vm.args(),
&self.parent_vm.params.kernel_args,
syscmdval,
&self.parent_vm.mem,
);
Expand Down
9 changes: 5 additions & 4 deletions src/macos/x86_64/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,13 +720,14 @@ impl VirtualCPU for XhyveCpu {
hypercall::address_to_hypercall(&self.parent_vm.mem, port, data_addr)
} {
match hypercall {
Hypercall::Cmdsize(syssize) => {
syssize.update(self.parent_vm.kernel_path(), self.parent_vm.args())
}
Hypercall::Cmdsize(syssize) => syssize.update(
self.parent_vm.kernel_path(),
&self.parent_vm.params.kernel_args,
),
Hypercall::Cmdval(syscmdval) => {
copy_argv(
self.parent_vm.kernel_path().as_os_str(),
self.parent_vm.args(),
&self.parent_vm.params.kernel_args,
syscmdval,
&self.parent_vm.mem,
);
Expand Down
51 changes: 29 additions & 22 deletions src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
ffi::OsString,
fmt, fs, io,
marker::PhantomData,
num::NonZeroU32,
Expand Down Expand Up @@ -105,19 +104,24 @@ pub struct UhyveVm<VCpuType: VirtualCPU = VcpuDefault> {
entry_point: u64,
stack_address: u64,
pub mem: Arc<MmapMemory>,
pub params: Params,
memory_size: usize,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memory_size might be useful if we move the memory initialization outside of new(...) (perhaps using a MaybeUninit - not quite sure).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you can access the memory_size via mem already?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't exactly sure whether doing this would get in the way of your work, given such a way to access this variable won't be possible after the replacement of the MmapMemory interface in #643.

There is some technical debt in #725 which does the exact same thing, but, as a trade-off, I assumed that a superfluous field would've been better than increasing that technical debt.

num_cpus: u32,
path: PathBuf,
args: Vec<OsString>,
boot_info: *const RawBootInfo,
verbose: bool,
pub virtio_device: Arc<Mutex<VirtioNetPciDevice>>,
#[allow(dead_code)] // gdb is not supported on macos
pub(super) gdb_port: Option<u16>,
_vcpu_type: PhantomData<VCpuType>,
}
impl<VCpuType: VirtualCPU> UhyveVm<VCpuType> {
pub fn new(kernel_path: PathBuf, params: Params) -> HypervisorResult<UhyveVm<VCpuType>> {
// We expose the params struct, but use some extra variables for the
// gdb_port (because of pub(super)) and memory_size (later, num_cpus)
// which require a get() to reduce overhead, and, most importantly,
// increase flexibility.
let memory_size = params.memory_size.get();
let gdb_port = params.gdb_port;

#[cfg(target_os = "linux")]
let mem = MmapMemory::new(0, memory_size, arch::RAM_START, params.thp, params.ksm);
Expand All @@ -133,14 +137,14 @@ impl<VCpuType: VirtualCPU> UhyveVm<VCpuType> {
#[cfg(target_os = "linux")]
initialize_kvm(&mem, params.pit)?;

let cpu_count = params.cpu_count.get();
let num_cpus = params.cpu_count.get();

assert!(
params.gdb_port.is_none() || cfg!(target_os = "linux"),
gdb_port.is_none() || cfg!(target_os = "linux"),
"gdb is only supported on linux (yet)"
);
assert!(
params.gdb_port.is_none() || cpu_count == 1,
gdb_port.is_none() || num_cpus == 1,
"gdbstub is only supported with one CPU"
);

Expand All @@ -149,13 +153,13 @@ impl<VCpuType: VirtualCPU> UhyveVm<VCpuType> {
entry_point: 0,
stack_address: 0,
mem: mem.into(),
num_cpus: cpu_count,
params,
memory_size,
num_cpus,
path: kernel_path,
args: params.kernel_args,
boot_info: ptr::null(),
verbose: params.verbose,
virtio_device,
gdb_port: params.gdb_port,
gdb_port,
_vcpu_type: PhantomData,
};

Expand All @@ -164,10 +168,6 @@ impl<VCpuType: VirtualCPU> UhyveVm<VCpuType> {
Ok(vm)
}

fn verbose(&self) -> bool {
self.verbose
}

/// Returns the section offsets relative to their base addresses
pub fn get_offset(&self) -> u64 {
self.offset
Expand All @@ -181,7 +181,17 @@ impl<VCpuType: VirtualCPU> UhyveVm<VCpuType> {
self.stack_address
}

/// Returns the number of cores for the vm.
// Returns the struct containing all parameters.
pub fn params(&self) -> &Params {
&self.params
}

// Returns the total memory size made available.
pub fn memory_size(&self) -> usize {
self.memory_size
}

// Returns number of cores for the VM.
pub fn num_cpus(&self) -> u32 {
self.num_cpus
}
Expand All @@ -190,10 +200,6 @@ impl<VCpuType: VirtualCPU> UhyveVm<VCpuType> {
&self.path
}

pub fn args(&self) -> &Vec<OsString> {
&self.args
}

/// Initialize the page tables for the guest
fn init_guest_mem(&mut self) {
debug!("Initialize guest memory");
Expand Down Expand Up @@ -232,7 +238,7 @@ impl<VCpuType: VirtualCPU> UhyveVm<VCpuType> {
hardware_info: HardwareInfo {
phys_addr_range: self.mem.guest_address.as_u64()
..self.mem.guest_address.as_u64() + self.mem.memory_size as u64,
serial_port_base: self.verbose().then(|| {
serial_port_base: self.params.verbose.then(|| {
SerialPortBase::new((uhyve_interface::HypercallAddress::Uart as u16).into())
.unwrap()
}),
Expand All @@ -241,7 +247,7 @@ impl<VCpuType: VirtualCPU> UhyveVm<VCpuType> {
load_info,
platform_info: PlatformInfo::Uhyve {
has_pci: cfg!(target_os = "linux"),
num_cpus: u64::from(self.num_cpus()).try_into().unwrap(),
num_cpus: u64::from(self.num_cpus).try_into().unwrap(),
cpu_freq: NonZeroU32::new(detect_cpu_freq() * 1000),
boot_time: SystemTime::now().into(),
},
Expand Down Expand Up @@ -269,10 +275,11 @@ impl<VCpuType: VirtualCPU> fmt::Debug for UhyveVm<VCpuType> {
.field("entry_point", &self.entry_point)
.field("stack_address", &self.stack_address)
.field("mem", &self.mem)
.field("params", &self.params)
.field("memory_size", &self.memory_size)
.field("num_cpus", &self.num_cpus)
.field("path", &self.path)
.field("boot_info", &self.boot_info)
.field("verbose", &self.verbose)
.field("virtio_device", &self.virtio_device)
.finish()
}
Expand Down