Skip to content

Commit a1e9927

Browse files
committed
Remove helper functions for sending/recving vm messages
The new approach is just to use the VirtualMachineSet functions directly
1 parent 6f0dafc commit a1e9927

File tree

5 files changed

+34
-68
lines changed

5 files changed

+34
-68
lines changed

mythril/src/kmain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ unsafe fn kmain(mut boot_info: BootInfo) -> ! {
281281
let core_id = percore::CoreId::from(idx as u32);
282282

283283
// Do not setup cores that are not allocated to any guest
284-
if !vm::is_assigned_core_id(core_id) {
284+
if !vm::virtual_machines().is_assigned_core_id(core_id) {
285285
debug!("Not starting core ID '{}' because it is not assigned to a guest", core_id);
286286
continue;
287287
}

mythril/src/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ pub fn cancel_timer(id: &TimerId) -> Result<()> {
402402
if wheel.is_local_timer(id) {
403403
wheel.remove_timer(id);
404404
} else {
405-
vm::send_vm_msg_core(
405+
vm::virtual_machines().send_msg_core(
406406
vm::VirtualMachineMsg::CancelTimer(id.clone()),
407407
id.core_id,
408408
true,

mythril/src/vcpu.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ pub fn mp_entry_point() -> ! {
3535

3636
let core_id = percore::read_core_id();
3737
let vm = unsafe {
38-
let vm = vm::get_vm_for_core_id(core_id)
38+
let vm = vm::virtual_machines()
39+
.get_by_core_id(core_id)
3940
.expect(&format!("Failed to find VM associated with {}", core_id));
4041
vm
4142
};
@@ -166,7 +167,7 @@ impl VCpu {
166167
/// received from the StartVcpu signal
167168
pub fn wait_for_init(&mut self) -> Result<()> {
168169
loop {
169-
if let Some(msg) = vm::recv_msg() {
170+
if let Some(msg) = vm::virtual_machines().recv_msg() {
170171
match msg {
171172
vm::VirtualMachineMsg::StartVcpu(addr) => {
172173
debug!(
@@ -443,7 +444,7 @@ impl VCpu {
443444
self.inject_interrupt(vector, kind);
444445
Ok(())
445446
} else {
446-
vm::send_vm_msg_core(
447+
vm::virtual_machines().send_msg_core(
447448
vm::VirtualMachineMsg::GuestInterrupt { vector, kind },
448449
destination,
449450
true,
@@ -550,7 +551,7 @@ impl VCpu {
550551
}
551552

552553
fn handle_ipc(&mut self) -> Result<()> {
553-
for msg in vm::recv_all_msgs() {
554+
for msg in vm::virtual_machines().recv_all_msgs() {
554555
match msg {
555556
vm::VirtualMachineMsg::GrantConsole(serial) => {
556557
*self.vm.config.physical_devices().serial.write() =
@@ -693,15 +694,16 @@ impl VCpu {
693694
.ok_or_else(|| Error::NotFound)?;
694695
let vmid = self.vm.id;
695696

696-
let next_vmid = (vmid + 1) % vm::max_vm_id();
697+
let next_vmid = (vmid + 1) % vm::virtual_machines().count();
697698

698-
vm::send_vm_msg(
699+
vm::virtual_machines().send_msg(
699700
vm::VirtualMachineMsg::GrantConsole(serial),
700701
next_vmid,
701702
true,
702703
)?;
703704

704-
let next_bsp = vm::get_vm_bsp_core_id(next_vmid)
705+
let next_bsp = vm::virtual_machines()
706+
.bsp_core_id(next_vmid)
705707
.ok_or_else(|| Error::NotFound)?;
706708

707709
//FIXME(alschwalm): this should be the APIC id of the bsp, not the core id

mythril/src/virtdev/lapic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl LocalApic {
131131
addr, core_id
132132
);
133133

134-
vm::send_vm_msg_core(
134+
vm::virtual_machines().send_msg_core(
135135
vm::VirtualMachineMsg::StartVcpu(addr),
136136
core_id,
137137
false,
@@ -167,7 +167,7 @@ impl LocalApic {
167167
if *core == percore::read_core_id() {
168168
continue;
169169
}
170-
vm::send_vm_msg_core(vm::VirtualMachineMsg::GuestInterrupt{
170+
vm::virtual_machines().send_msg_core(vm::VirtualMachineMsg::GuestInterrupt{
171171
kind: crate::vcpu::InjectedInterruptType::ExternalInterrupt,
172172
vector: vector as u8
173173
}, *core, true)?
@@ -190,7 +190,7 @@ impl LocalApic {
190190
if *core == percore::read_core_id() {
191191
continue;
192192
}
193-
vm::send_vm_msg_core(vm::VirtualMachineMsg::GuestInterrupt{
193+
vm::virtual_machines().send_msg_core(vm::VirtualMachineMsg::GuestInterrupt{
194194
kind: crate::vcpu::InjectedInterruptType::ExternalInterrupt,
195195
vector: vector as u8
196196
}, *core, true)?

mythril/src/vm.rs

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -31,64 +31,20 @@ static BIOS_BLOB: &'static [u8] = include_bytes!("blob/bios.bin");
3131
/// The location of the local apic in the guest address space
3232
pub const GUEST_LOCAL_APIC_ADDR: GuestPhysAddr = GuestPhysAddr::new(0xfee00000);
3333

34-
static VIRTUAL_MACHINES: RoAfterInit<VirtualMachines> =
34+
static VIRTUAL_MACHINES: RoAfterInit<VirtualMachineSet> =
3535
RoAfterInit::uninitialized();
3636

3737
/// The maximum numer of cores that can be assigned to a single VM
3838
pub const MAX_PER_VM_CORE_COUNT: usize = 32;
3939

4040
const MAX_PENDING_MSG: usize = 100;
4141

42-
pub unsafe fn init_virtual_machines(machines: VirtualMachines) {
42+
pub unsafe fn init_virtual_machines(machines: VirtualMachineSet) {
4343
RoAfterInit::init(&VIRTUAL_MACHINES, machines);
4444
}
4545

46-
/// Get the virtual machine that owns the core with the given core id
47-
///
48-
/// This method is unsafe as it should almost certainly not be used (use message
49-
/// passing instead of directly accessing the remote VM).
50-
pub unsafe fn get_vm_for_core_id(
51-
core_id: percore::CoreId,
52-
) -> Option<Arc<VirtualMachine>> {
53-
VIRTUAL_MACHINES.get_by_core_id(core_id)
54-
}
55-
56-
pub fn send_vm_msg(
57-
msg: VirtualMachineMsg,
58-
vmid: u32,
59-
notify: bool,
60-
) -> Result<()> {
61-
VIRTUAL_MACHINES.send_msg(msg, vmid, notify)
62-
}
63-
64-
pub fn get_vm_bsp_core_id(vmid: u32) -> Option<percore::CoreId> {
65-
VIRTUAL_MACHINES
66-
.get_by_vm_id(vmid)
67-
.map(|vm| vm.config.bsp_id())
68-
}
69-
70-
pub fn send_vm_msg_core(
71-
msg: VirtualMachineMsg,
72-
core_id: percore::CoreId,
73-
notify: bool,
74-
) -> Result<()> {
75-
VIRTUAL_MACHINES.send_msg_core(msg, core_id, notify)
76-
}
77-
78-
pub fn recv_msg() -> Option<VirtualMachineMsg> {
79-
VIRTUAL_MACHINES.resv_msg()
80-
}
81-
82-
pub fn recv_all_msgs() -> impl Iterator<Item = VirtualMachineMsg> {
83-
VIRTUAL_MACHINES.resv_all_msgs()
84-
}
85-
86-
pub fn max_vm_id() -> u32 {
87-
VIRTUAL_MACHINES.count()
88-
}
89-
90-
pub fn is_assigned_core_id(core_id: percore::CoreId) -> bool {
91-
VIRTUAL_MACHINES.is_assigned_core_id(core_id)
46+
pub fn virtual_machines() -> &'static VirtualMachineSet {
47+
&*VIRTUAL_MACHINES
9248
}
9349

9450
pub enum VirtualMachineMsg {
@@ -106,12 +62,12 @@ struct VirtualMachineContext {
10662
msgqueue: RwLock<ArrayDeque<[VirtualMachineMsg; MAX_PENDING_MSG]>>,
10763
}
10864

109-
pub struct VirtualMachines {
65+
pub struct VirtualMachineSet {
11066
machine_count: u32,
11167
map: BTreeMap<percore::CoreId, VirtualMachineContext>,
11268
}
11369

114-
impl VirtualMachines {
70+
impl VirtualMachineSet {
11571
fn context_by_core_id(
11672
&self,
11773
core_id: percore::CoreId,
@@ -140,7 +96,11 @@ impl VirtualMachines {
14096
self.map.contains_key(&core_id)
14197
}
14298

143-
pub fn get_by_core_id(
99+
/// Get the virtual machine that owns the core with the given core id
100+
///
101+
/// This method is unsafe as it should almost certainly not be used (use message
102+
/// passing instead of directly accessing the remote VM).
103+
pub unsafe fn get_by_core_id(
144104
&self,
145105
core_id: percore::CoreId,
146106
) -> Option<Arc<VirtualMachine>> {
@@ -152,6 +112,10 @@ impl VirtualMachines {
152112
self.context_by_vm_id(id).map(|context| context.vm.clone())
153113
}
154114

115+
pub fn bsp_core_id(&self, vmid: u32) -> Option<percore::CoreId> {
116+
self.get_by_vm_id(vmid).map(|vm| vm.config.bsp_id())
117+
}
118+
155119
/// Send the given message to a specific core
156120
///
157121
/// If 'notify' is true, an interrupt will be sent to the recipient.
@@ -204,7 +168,7 @@ impl VirtualMachines {
204168
vm_id: u32,
205169
notify: bool,
206170
) -> Result<()> {
207-
let vm_bsp = get_vm_bsp_core_id(vm_id).ok_or_else(|| {
171+
let vm_bsp = self.bsp_core_id(vm_id).ok_or_else(|| {
208172
Error::InvalidValue(format!(
209173
"Unable to find BSP for VM id '{}'",
210174
vm_id
@@ -214,15 +178,15 @@ impl VirtualMachines {
214178
}
215179

216180
/// Receive any pending message for the current core
217-
pub fn resv_msg(&self) -> Option<VirtualMachineMsg> {
181+
pub fn recv_msg(&self) -> Option<VirtualMachineMsg> {
218182
let context = self
219183
.context_by_core_id(percore::read_core_id())
220184
.expect("No VirtualMachineContext for apic id");
221185
context.msgqueue.write().pop_front()
222186
}
223187

224188
/// Receive all pending messages for the current core
225-
pub fn resv_all_msgs(&self) -> impl Iterator<Item = VirtualMachineMsg> {
189+
pub fn recv_all_msgs(&self) -> impl Iterator<Item = VirtualMachineMsg> {
226190
let context = self
227191
.context_by_core_id(percore::read_core_id())
228192
.expect("No VirtualMachineContext for apic id");
@@ -262,8 +226,8 @@ impl VirtualMachineBuilder {
262226
self.map.get(&core_id).map(|vm| vm.clone())
263227
}
264228

265-
pub fn finalize(self) -> VirtualMachines {
266-
VirtualMachines {
229+
pub fn finalize(self) -> VirtualMachineSet {
230+
VirtualMachineSet {
267231
machine_count: self.machine_count,
268232
map: self
269233
.map

0 commit comments

Comments
 (0)