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

WIP: vioapic: add vioapic support #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions mythril/src/virtdev/hpet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::error::Result;
use crate::memory::GuestPhysAddr;
use crate::virtdev::{DeviceRegion, EmulatedDevice, Event};
use alloc::sync::Arc;
use alloc::vec::Vec;
use spin::RwLock;

#[derive(Default)]
pub struct Hpet;

impl Hpet {
pub fn new() -> Arc<RwLock<Self>> {
Arc::new(RwLock::new(Hpet {}))
}
}

impl EmulatedDevice for Hpet {
fn services(&self) -> Vec<DeviceRegion> {
vec![
DeviceRegion::MemIo(
GuestPhysAddr::new(0xfed00000)..=GuestPhysAddr::new(0xfed010f0),
),
]
}

fn on_event(&mut self, _event: Event) -> Result<()> {
Ok(())
}
}
114 changes: 99 additions & 15 deletions mythril/src/virtdev/ioapic.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,115 @@
use crate::error::Result;
use crate::error::{Error, Result};
use crate::memory::GuestPhysAddr;
use crate::virtdev::{DeviceRegion, EmulatedDevice, Event};
use crate::virtdev::{DeviceEvent, DeviceRegion};
use crate::virtdev::{EmulatedDevice, Event};
use crate::virtdev::{MemReadRequest, MemWriteRequest};
use alloc::vec::Vec;

use byteorder::{ByteOrder, NativeEndian};

#[derive(Debug)]
enum Request<'a> {
Read(u8, MemReadRequest<'a>),
Write(u8, MemWriteRequest<'a>),
}

#[derive(Clone, Copy, Debug)]
enum RequestState {
Pending,
RegSel(u8),
}

impl RequestState {
fn reset(&mut self) {
*self = RequestState::Pending;
}
}

impl Default for RequestState {
fn default() -> RequestState {
RequestState::Pending
}
}

#[derive(Default)]
pub struct IoApic;
pub struct IoApic {
state: RequestState,
version: u32,
}

impl IoApic {
pub fn new() -> Result<Self> {
Ok(IoApic {})
Ok(IoApic {
state: RequestState::Pending,
version: 0x11,
})
}
}

impl EmulatedDevice for IoApic {
fn services(&self) -> Vec<DeviceRegion> {
vec![
DeviceRegion::MemIo(
GuestPhysAddr::new(0xfec00000)..=GuestPhysAddr::new(0xfec010f0),
),
//FIXME: this is actually the 1st HPET
DeviceRegion::MemIo(
GuestPhysAddr::new(0xfed00000)..=GuestPhysAddr::new(0xfed010f0),
),
]
vec![DeviceRegion::MemIo(
GuestPhysAddr::new(0xfec00000)..=GuestPhysAddr::new(0xfec010f0),
)]
}

fn on_event(&mut self, _event: Event) -> Result<()> {
Ok(())
fn on_event<'a>(&mut self, event: Event) -> Result<()> {
// Parse the event into a register request based on the address given,
// input, and the current request state.
let mut req = match self.state {
RequestState::Pending => match event.kind {
DeviceEvent::MemWrite(addr, val)
if addr.as_u64() == 0xfec00000 =>
{
if val.as_slice().len() != 4 {
Err(Error::NotSupported)
} else {
let reg = NativeEndian::read_u32(val.as_slice());
self.state = RequestState::RegSel(reg as u8);
return Ok(());
}
}
_ => Err(Error::NotSupported),
},
RequestState::RegSel(reg) => match event.kind {
DeviceEvent::MemWrite(addr, val)
if addr.as_u64() == 0xfec00010 =>
{
if val.as_slice().len() != 4 {
Err(Error::NotSupported)
} else {
self.state.reset();
Ok(Request::Write(reg, val))
}
}
DeviceEvent::MemRead(addr, val)
if addr.as_u64() == 0xfec00010 =>
{
if val.as_slice().len() != 4 {
Err(Error::NotSupported)
} else {
self.state.reset();
Ok(Request::Read(reg, val))
}
}
_ => Err(Error::NotSupported),
},
}?;

info!("I/O APIC Request {:?}", req);
match req {
Request::Read(0, ref mut data) => {
NativeEndian::write_u32(data.as_mut_slice(), self.version);
Ok(())
}
Request::Write(0, ref data) => {
self.version = NativeEndian::read_u32(data.as_slice());
Ok(())
}
_ => {
info!("I/O APIC Unsupported Request: {:?}", req);
Err(Error::NotSupported)
}
}
}
}