-
Notifications
You must be signed in to change notification settings - Fork 15
Resolve #78, EmulatedDevice::services should access the VM config #113
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
Open
AntoineRR
wants to merge
11
commits into
mythril-hypervisor:master
Choose a base branch
from
maelgui:antoine.78
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5ee09ed
Adds a DeviceMapBuilder struct
AntoineRR ac97235
Bug fixes
paulcacheux 1574e13
Re-add commented out test working now with config
paulcacheux 46b1de9
Corrects virtdev tests
AntoineRR 88d84cd
Default config for test module
maelgui ad85cb9
Fix fmt errors
paulcacheux a4742fd
Modifies the default VirtualMachineConfig method used for tests
AntoineRR c3f4f59
Resolves merge conflicts with master
AntoineRR eaa86f9
Passes check fmt test
AntoineRR 1655c93
Moves unused import into test module
AntoineRR b2342e6
Adds missing doc
AntoineRR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
use crate::error::{Error, Result}; | ||
use crate::memory::{GuestAddressSpaceViewMut, GuestPhysAddr}; | ||
use crate::vcpu; | ||
use crate::{ | ||
error::{Error, Result}, | ||
vm::VirtualMachineConfig, | ||
}; | ||
use alloc::collections::btree_map::BTreeMap; | ||
use alloc::sync::Arc; | ||
use alloc::vec::Vec; | ||
|
@@ -160,6 +163,22 @@ impl DeviceInteraction for GuestPhysAddr { | |
} | ||
} | ||
|
||
pub struct DeviceMapBuilder<'config> { | ||
pub vm_config: &'config mut VirtualMachineConfig, | ||
} | ||
|
||
impl<'config> DeviceMapBuilder<'config> { | ||
pub fn register_device( | ||
&mut self, | ||
dev: Arc<RwLock<dyn EmulatedDevice>>, | ||
) -> Result<()> { | ||
let services = dev.read().services(&self.vm_config); | ||
self.vm_config | ||
.virtual_devices_mut() | ||
.register_services(services, dev) | ||
} | ||
} | ||
|
||
/// A structure for looking up `EmulatedDevice`s by port or address | ||
#[derive(Default)] | ||
pub struct DeviceMap { | ||
|
@@ -176,11 +195,11 @@ impl DeviceMap { | |
op.find_device(self) | ||
} | ||
|
||
pub fn register_device( | ||
pub fn register_services( | ||
&mut self, | ||
services: Vec<DeviceRegion>, | ||
dev: Arc<RwLock<dyn EmulatedDevice>>, | ||
) -> Result<()> { | ||
let services = dev.read().services(); | ||
for region in services.into_iter() { | ||
match region { | ||
DeviceRegion::PortIo(val) => { | ||
|
@@ -221,7 +240,7 @@ impl DeviceMap { | |
} | ||
|
||
pub trait EmulatedDevice: Send + Sync { | ||
fn services(&self) -> Vec<DeviceRegion>; | ||
fn services(&self, vm_config: &VirtualMachineConfig) -> Vec<DeviceRegion>; | ||
|
||
fn on_event(&mut self, _event: Event) -> Result<()> { | ||
Ok(()) | ||
|
@@ -480,7 +499,7 @@ impl<'a> fmt::Display for MemReadRequest<'a> { | |
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::virtdev::com::*; | ||
use crate::{virtdev::com::*, vm::PhysicalDeviceConfig}; | ||
use core::convert::TryInto; | ||
|
||
// This is just a dummy device so we can have arbitrary port ranges | ||
|
@@ -497,8 +516,22 @@ mod test { | |
} | ||
} | ||
|
||
impl VirtualMachineConfig { | ||
// Default config used for testing | ||
pub fn default() -> VirtualMachineConfig { | ||
|
||
VirtualMachineConfig::new( | ||
vec![0.into()], | ||
1024, | ||
PhysicalDeviceConfig::default(), | ||
) | ||
} | ||
} | ||
|
||
impl EmulatedDevice for DummyDevice { | ||
fn services(&self) -> Vec<DeviceRegion> { | ||
fn services( | ||
&self, | ||
_vm_config: &VirtualMachineConfig, | ||
) -> Vec<DeviceRegion> { | ||
self.services | ||
.iter() | ||
.map(|x| DeviceRegion::PortIo(x.clone())) | ||
|
@@ -508,9 +541,13 @@ mod test { | |
|
||
#[test] | ||
fn test_device_map() { | ||
let mut map = DeviceMap::default(); | ||
let mut config = VirtualMachineConfig::default(); | ||
|
||
let mut builder = config.device_map_builder(); | ||
let com = Uart8250::new(0); | ||
map.register_device(com).unwrap(); | ||
builder.register_device(com).unwrap(); | ||
|
||
let map = config.virtual_devices(); | ||
let _dev = map.find_device(0u16).unwrap(); | ||
|
||
assert_eq!(map.find_device(10u16).is_none(), true); | ||
|
@@ -544,32 +581,38 @@ mod test { | |
|
||
#[test] | ||
fn test_conflicting_portio_device() { | ||
let mut map = DeviceMap::default(); | ||
let mut config = VirtualMachineConfig::default(); | ||
|
||
let mut builder = config.device_map_builder(); | ||
let com = Uart8250::new(0); | ||
map.register_device(com).unwrap(); | ||
builder.register_device(com).unwrap(); | ||
let com = Uart8250::new(0); | ||
|
||
assert!(map.register_device(com).is_err()); | ||
assert!(builder.register_device(com).is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_fully_overlapping_portio_device() { | ||
// region 2 fully inside region 1 | ||
let services = vec![0..=10, 2..=8]; | ||
let dummy = DummyDevice::new(services); | ||
let mut map = DeviceMap::default(); | ||
let mut config = VirtualMachineConfig::default(); | ||
|
||
assert!(map.register_device(dummy).is_err()); | ||
let mut builder = config.device_map_builder(); | ||
|
||
assert!(builder.register_device(dummy).is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_fully_encompassing_portio_device() { | ||
// region 1 fully inside region 2 | ||
let services = vec![2..=8, 0..=10]; | ||
let dummy = DummyDevice::new(services); | ||
let mut map = DeviceMap::default(); | ||
let mut config = VirtualMachineConfig::default(); | ||
|
||
let mut builder = config.device_map_builder(); | ||
|
||
assert!(map.register_device(dummy).is_err()); | ||
assert!(builder.register_device(dummy).is_err()); | ||
} | ||
|
||
#[test] | ||
|
@@ -578,9 +621,11 @@ mod test { | |
// the start of region 2 | ||
let services = vec![0..=4, 3..=8]; | ||
let dummy = DummyDevice::new(services); | ||
let mut map = DeviceMap::default(); | ||
let mut config = VirtualMachineConfig::default(); | ||
|
||
let mut builder = config.device_map_builder(); | ||
|
||
assert!(map.register_device(dummy).is_err()); | ||
assert!(builder.register_device(dummy).is_err()); | ||
} | ||
|
||
#[test] | ||
|
@@ -589,18 +634,22 @@ mod test { | |
// the tail of region 2 | ||
let services = vec![3..=8, 0..=4]; | ||
let dummy = DummyDevice::new(services); | ||
let mut map = DeviceMap::default(); | ||
let mut config = VirtualMachineConfig::default(); | ||
|
||
assert!(map.register_device(dummy).is_err()); | ||
let mut builder = config.device_map_builder(); | ||
|
||
assert!(builder.register_device(dummy).is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_non_overlapping_portio_device() { | ||
// region 1 and region 2 don't overlap | ||
let services = vec![0..=3, 4..=8]; | ||
let dummy = DummyDevice::new(services); | ||
let mut map = DeviceMap::default(); | ||
let mut config = VirtualMachineConfig::default(); | ||
|
||
let mut builder = config.device_map_builder(); | ||
|
||
assert!(map.register_device(dummy).is_ok()); | ||
assert!(builder.register_device(dummy).is_ok()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this comment.