Skip to content

Commit 06535b8

Browse files
committed
Store VCPU in percore variable
1 parent f5229f8 commit 06535b8

File tree

15 files changed

+52
-181
lines changed

15 files changed

+52
-181
lines changed

mythril/src/kmain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn build_vm(
126126
.unwrap();
127127

128128
virtual_devices.push(RwLock::new(virtdev::DynamicVirtualDevice::Qemu(
129-
fw_cfg_builder.build(),
129+
fw_cfg_builder.build().expect("Failed to build FW Cfg"),
130130
)));
131131

132132
vm::VirtualMachine::new(vm_id, config, info).expect("Failed to create vm")

mythril/src/vcpu.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ extern "C" {
2626
const PER_CORE_HOST_STACK_SIZE: usize = 1024 * 1024;
2727

2828
declare_per_core! {
29+
// NOTE: The per-core stack cannot be part of the VCpu type because an
30+
// instance of VCpu will (briefly) reside _on_ the stack
2931
static mut HOST_STACK: [u8; PER_CORE_HOST_STACK_SIZE]
3032
= [0u8; PER_CORE_HOST_STACK_SIZE];
33+
34+
static mut VCPU: Option<VCpu> = None;
3135
}
3236

3337
/// The post-startup point where a core begins executing its statically
@@ -47,7 +51,7 @@ pub fn mp_entry_point() -> ! {
4751
vm
4852
};
4953

50-
let mut vcpu = VCpu::new(vm).expect("Failed to create vcpu");
54+
let vcpu = VCpu::new(vm).expect("Failed to create vcpu");
5155

5256
let vm_id = vm.id;
5357
let is_vm_bsp = vm.bsp_id() == core_id;
@@ -108,17 +112,23 @@ impl VCpu {
108112
/// Note that the result must be `Pin`, as the `VCpu` pushes its own
109113
/// address on to the per-core host stack so it can be retrieved on
110114
/// VMEXIT.
111-
pub fn new(vm: &'static VirtualMachine) -> Result<Pin<Box<Self>>> {
115+
pub fn new(vm: &'static VirtualMachine) -> Result<&'static mut Self> {
112116
let vmx = vmx::Vmx::enable()?;
113117
let vmcs = vmcs::Vmcs::new()?.activate(vmx)?;
114118

115-
let mut vcpu = Box::pin(Self {
119+
let vcpu = Self {
116120
vm: vm,
117121
vmcs: vmcs,
118122
local_apic: virtdev::lapic::LocalApic::new(),
119123
stack: get_per_core_mut!(HOST_STACK),
120124
pending_interrupts: BTreeMap::new(),
121-
});
125+
};
126+
127+
unsafe {
128+
// Move the VCpu off the stack to the final static location
129+
*get_per_core_mut!(VCPU) = Some(vcpu);
130+
}
131+
let vcpu = get_per_core_mut!(VCPU).as_mut().unwrap();
122132

123133
// All VCpus in a VM must share the same address space
124134
let eptp = vcpu.vm.guest_space.eptp();
@@ -136,13 +146,13 @@ impl VCpu {
136146
- mem::size_of::<*const Self>() as u64;
137147

138148
// 'push' the address of this VCpu to the host stack for the vmexit
139-
let raw_vcpu: *mut Self = (&mut *vcpu) as *mut Self;
149+
let raw_vcpu = vcpu as *mut Self;
140150
unsafe {
141151
core::ptr::write(stack_base as *mut *mut Self, raw_vcpu);
142152
}
143153

144154
Self::initialize_host_vmcs(&mut vcpu.vmcs, stack_base)?;
145-
Self::initialize_guest_vmcs(&mut vcpu)?;
155+
Self::initialize_guest_vmcs(vcpu)?;
146156
Self::initialize_ctrl_vmcs(&mut vcpu.vmcs)?;
147157

148158
Ok(vcpu)
@@ -157,7 +167,7 @@ impl VCpu {
157167
}
158168

159169
/// Begin execution in the guest context for this core
160-
pub fn launch(self: Pin<Box<Self>>) -> Result<!> {
170+
pub fn launch(&mut self) -> Result<!> {
161171
let rflags = unsafe { vmlaunch_wrapper() };
162172
error::check_vm_insruction(rflags, "Failed to launch vm".into())?;
163173

mythril/src/virtdev/dma.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

mythril/src/virtdev/ignore.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

mythril/src/virtdev/ioapic.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ use crate::error::Result;
22
use crate::memory::GuestPhysAddr;
33
use crate::virtdev::{DeviceRegion, EmulatedDevice, Event};
44
use alloc::vec::Vec;
5-
use spin::RwLock;
65

76
#[derive(Default)]
87
pub struct IoApic;
98

109
impl IoApic {
11-
pub fn new() -> Result<RwLock<Self>> {
12-
Ok(RwLock::new(IoApic {}))
10+
pub fn new() -> Result<Self> {
11+
Ok(IoApic {})
1312
}
1413
}
1514

mythril/src/virtdev/keyboard.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::error::Result;
22
use crate::virtdev::{DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port};
33
use alloc::vec::Vec;
4-
use spin::RwLock;
54

65
#[derive(Default, Debug)]
76
pub struct Keyboard8042;
@@ -10,8 +9,8 @@ impl Keyboard8042 {
109
const PS2_DATA: Port = 0x0060;
1110
const PS2_STATUS: Port = 0x0064;
1211

13-
pub fn new() -> Result<RwLock<Self>> {
14-
Ok(RwLock::new(Self::default()))
12+
pub fn new() -> Result<Self> {
13+
Ok(Self::default())
1514
}
1615
}
1716

mythril/src/virtdev/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ use spin::RwLock;
1212
pub mod acpi;
1313
pub mod com;
1414
pub mod debug;
15-
pub mod dma;
16-
pub mod ignore;
1715
pub mod ioapic;
1816
pub mod keyboard;
1917
pub mod lapic;
2018
pub mod pci;
2119
pub mod pic;
2220
pub mod pit;
23-
pub mod pos;
2421
pub mod qemu_fw_cfg;
2522
pub mod rtc;
2623
pub mod vga;

mythril/src/virtdev/pci.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use alloc::collections::btree_map::BTreeMap;
44
use alloc::vec::Vec;
55
use core::convert::TryInto;
66
use num_enum::TryFromPrimitive;
7-
use spin::RwLock;
87
use ux;
98

109
#[derive(Clone, Copy, Debug, TryFromPrimitive)]
@@ -153,7 +152,7 @@ impl PciRootComplex {
153152
const PCI_CONFIG_DATA: Port = 0xcfc;
154153
const PCI_CONFIG_DATA_MAX: Port = Self::PCI_CONFIG_DATA + 3;
155154

156-
pub fn new() -> Result<RwLock<Self>> {
155+
pub fn new() -> Result<Self> {
157156
let mut devices = BTreeMap::new();
158157

159158
let host_bridge = PciDevice {
@@ -180,10 +179,10 @@ impl PciRootComplex {
180179
};
181180
devices.insert(ich9.bdf.into(), ich9);
182181

183-
Ok(RwLock::new(Self {
182+
Ok(Self {
184183
current_address: 0,
185184
devices: devices,
186-
}))
185+
})
187186
}
188187
}
189188

@@ -274,7 +273,7 @@ mod test {
274273
GuestAddressSpaceView::new(GuestPhysAddr::new(0), space)
275274
}
276275

277-
fn complex_ready_for_reg_read(reg: u8) -> RwLock<PciRootComplex> {
276+
fn complex_ready_for_reg_read(reg: u8) -> PciRootComplex {
278277
let view = define_test_view();
279278
let complex = PciRootComplex::new().unwrap();
280279
let addr = ((reg << 2) as u32).to_be_bytes();
@@ -286,10 +285,7 @@ mod test {
286285
&mut responses,
287286
)
288287
.unwrap();
289-
{
290-
let mut complex = complex.write();
291-
complex.on_event(event).unwrap();
292-
}
288+
complex.on_event(event).unwrap();
293289
complex
294290
}
295291

mythril/src/virtdev/pic.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::error::Result;
22
use crate::virtdev::{DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port};
33
use alloc::vec::Vec;
44
use core::convert::TryInto;
5-
use spin::RwLock;
65

76
#[derive(Default, Debug)]
87
pub struct PicState {
@@ -23,8 +22,8 @@ impl Pic8259 {
2322
const PIC_ECLR_COMMAND: Port = 0x4d0;
2423
const PIC_ECLR_DATA: Port = Self::PIC_ECLR_COMMAND + 1;
2524

26-
pub fn new() -> Result<RwLock<Self>> {
27-
Ok(RwLock::new(Pic8259::default()))
25+
pub fn new() -> Result<Self> {
26+
Ok(Pic8259::default())
2827
}
2928
}
3029

mythril/src/virtdev/pit.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::virtdev::{
99

1010
use alloc::vec::Vec;
1111
use core::convert::TryFrom;
12-
use spin::RwLock;
1312

1413
#[derive(Debug)]
1514
enum OperatingModeState {
@@ -60,8 +59,8 @@ pub struct Pit8254 {
6059
}
6160

6261
impl Pit8254 {
63-
pub fn new() -> Result<RwLock<Self>> {
64-
Ok(RwLock::new(Pit8254::default()))
62+
pub fn new() -> Result<Self> {
63+
Ok(Pit8254::default())
6564
}
6665

6766
fn on_port_read(

0 commit comments

Comments
 (0)