-
Notifications
You must be signed in to change notification settings - Fork 29
/
vm.rs
256 lines (222 loc) · 7.07 KB
/
vm.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use std::{
ffi::OsString,
fmt, fs, io,
marker::PhantomData,
num::NonZeroU32,
path::PathBuf,
ptr,
sync::{Arc, Mutex},
time::SystemTime,
};
use hermit_entry::{
boot_info::{BootInfo, HardwareInfo, PlatformInfo, RawBootInfo, SerialPortBase},
elf::{KernelObject, LoadedKernel, ParseKernelError},
};
use log::{error, warn};
use thiserror::Error;
#[cfg(target_arch = "x86_64")]
use crate::arch::x86_64::{
detect_freq_from_cpuid, detect_freq_from_cpuid_hypervisor_info, get_cpu_frequency_from_os,
};
#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
use crate::linux::x86_64::kvm_cpu::initialize_kvm;
use crate::{
arch, consts::*, mem::MmapMemory, os::HypervisorError, params::Params, vcpu::VirtualCPU,
virtio::*,
};
pub type HypervisorResult<T> = Result<T, HypervisorError>;
#[derive(Error, Debug)]
pub enum LoadKernelError {
#[error(transparent)]
Io(#[from] io::Error),
#[error("{0}")]
ParseKernelError(ParseKernelError),
#[error("guest memory size is not large enough")]
InsufficientMemory,
}
pub type LoadKernelResult<T> = Result<T, LoadKernelError>;
// TODO: move to architecture specific section
fn detect_cpu_freq() -> u32 {
#[cfg(target_arch = "aarch64")]
let mhz: u32 = 0;
#[cfg(target_arch = "x86_64")]
let mhz = {
let cpuid = raw_cpuid::CpuId::new();
let mhz: u32 = detect_freq_from_cpuid(&cpuid).unwrap_or_else(|_| {
debug!("Failed to detect from cpuid");
detect_freq_from_cpuid_hypervisor_info(&cpuid).unwrap_or_else(|_| {
debug!("Failed to detect from hypervisor_info");
get_cpu_frequency_from_os().unwrap_or(0)
})
});
debug!("detected a cpu frequency of {} Mhz", mhz);
mhz
};
if mhz == 0 {
warn!("Unable to determine processor frequency");
}
mhz
}
#[cfg(target_os = "linux")]
pub type VcpuDefault = crate::linux::x86_64::kvm_cpu::KvmCpu;
#[cfg(target_os = "macos")]
pub type VcpuDefault = crate::macos::XhyveCpu;
pub struct UhyveVm<VCpuType: VirtualCPU = VcpuDefault> {
/// The starting position of the image in physical memory
offset: u64,
entry_point: u64,
stack_address: u64,
pub mem: Arc<MmapMemory>,
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>> {
let memory_size = params.memory_size.get();
#[cfg(target_os = "linux")]
let mem = MmapMemory::new(0, memory_size, arch::RAM_START, params.thp, params.ksm);
#[cfg(not(target_os = "linux"))]
let mem = MmapMemory::new(0, memory_size, arch::RAM_START, false, false);
// create virtio interface
// TODO: Remove allow once fixed:
// https://github.com/rust-lang/rust-clippy/issues/11382
#[allow(clippy::arc_with_non_send_sync)]
let virtio_device = Arc::new(Mutex::new(VirtioNetPciDevice::new()));
#[cfg(target_os = "linux")]
initialize_kvm(&mem, params.pit)?;
let cpu_count = params.cpu_count.get();
assert!(
params.gdb_port.is_none() || cfg!(target_os = "linux"),
"gdb is only supported on linux (yet)"
);
assert!(
params.gdb_port.is_none() || cpu_count == 1,
"gdbstub is only supported with one CPU"
);
let mut vm = Self {
offset: 0,
entry_point: 0,
stack_address: 0,
mem: mem.into(),
num_cpus: cpu_count,
path: kernel_path,
args: params.kernel_args,
boot_info: ptr::null(),
verbose: params.verbose,
virtio_device,
gdb_port: params.gdb_port,
_vcpu_type: PhantomData,
};
vm.init_guest_mem();
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
}
pub fn get_entry_point(&self) -> u64 {
self.entry_point
}
pub fn stack_address(&self) -> u64 {
self.stack_address
}
/// Returns the number of cores for the vm.
pub fn num_cpus(&self) -> u32 {
self.num_cpus
}
pub fn kernel_path(&self) -> &PathBuf {
&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");
crate::arch::init_guest_mem(
unsafe { self.mem.as_slice_mut() } // slice only lives during this fn call
.try_into()
.expect("Guest memory is not large enough for pagetables"),
);
}
pub fn load_kernel(&mut self) -> LoadKernelResult<()> {
let elf = fs::read(self.kernel_path())?;
let object = KernelObject::parse(&elf).map_err(LoadKernelError::ParseKernelError)?;
// TODO: should be a random start address, if we have a relocatable executable
let kernel_start_address = object.start_addr().unwrap_or(0x400000) as usize;
let kernel_end_address = kernel_start_address + object.mem_size();
self.offset = kernel_start_address as u64;
if kernel_end_address > self.mem.memory_size - self.mem.guest_address.as_u64() as usize {
return Err(LoadKernelError::InsufficientMemory);
}
let LoadedKernel {
load_info,
entry_point,
} = object.load_kernel(
// Safety: Slice only lives during this fn call, so no aliasing happens
&mut unsafe { self.mem.as_slice_uninit_mut() }
[kernel_start_address..kernel_end_address],
kernel_start_address as u64,
);
self.entry_point = entry_point;
let boot_info = BootInfo {
hardware_info: HardwareInfo {
phys_addr_range: arch::RAM_START.as_u64()
..arch::RAM_START.as_u64() + self.mem.memory_size as u64,
serial_port_base: self.verbose().then(|| {
SerialPortBase::new((uhyve_interface::HypercallAddress::Uart as u16).into())
.unwrap()
}),
device_tree: None,
},
load_info,
platform_info: PlatformInfo::Uhyve {
has_pci: cfg!(target_os = "linux"),
num_cpus: u64::from(self.num_cpus()).try_into().unwrap(),
cpu_freq: NonZeroU32::new(detect_cpu_freq() * 1000),
boot_time: SystemTime::now().into(),
},
};
unsafe {
let raw_boot_info_ptr =
self.mem.host_address.add(BOOT_INFO_ADDR.as_u64() as usize) as *mut RawBootInfo;
*raw_boot_info_ptr = RawBootInfo::from(boot_info);
self.boot_info = raw_boot_info_ptr;
}
self.stack_address = (kernel_start_address as u64)
.checked_sub(KERNEL_STACK_SIZE)
.expect(
"there should be enough space for the boot stack before the kernel start address",
);
Ok(())
}
}
impl<VCpuType: VirtualCPU> fmt::Debug for UhyveVm<VCpuType> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("UhyveVm")
.field("entry_point", &self.entry_point)
.field("stack_address", &self.stack_address)
.field("mem", &self.mem)
.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()
}
}
// TODO: Investigate soundness
// https://github.com/hermitcore/uhyve/issues/229
#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl<VCpuType: VirtualCPU> Send for UhyveVm<VCpuType> {}
unsafe impl<VCpuType: VirtualCPU> Sync for UhyveVm<VCpuType> {}