Skip to content

Commit deb4414

Browse files
committed
Do com/debug output through DeviceEventResponse
1 parent 5dd4ec8 commit deb4414

File tree

4 files changed

+26
-46
lines changed

4 files changed

+26
-46
lines changed

mythril/src/kmain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ fn default_vm(
5656
.register_device(virtdev::acpi::AcpiRuntime::new(0xb000).unwrap())
5757
.unwrap();
5858
device_map
59-
.register_device(virtdev::debug::DebugPort::new(core as u64, 0x402))
59+
.register_device(virtdev::debug::DebugPort::new(0x402))
6060
.unwrap();
6161
device_map
62-
.register_device(virtdev::com::Uart8250::new(core as u64, 0x3F8))
62+
.register_device(virtdev::com::Uart8250::new(0x3F8))
6363
.unwrap();
6464
device_map
6565
.register_device(virtdev::vga::VgaController::new())

mythril/src/vcpu.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,15 @@ impl VCpu {
540540
virtdev::DeviceEventResponse::Interrupt((vector, kind)) => {
541541
self.inject_interrupt(vector, kind);
542542
}
543-
_ => (),
543+
virtdev::DeviceEventResponse::GuestUartTransmitted(val) => {
544+
let vm = self.vm.read();
545+
if vm.config.physical_devices().serial.is_some() {
546+
//TODO: This should be a write to the physical serial device
547+
let buff = &[val];
548+
let s = alloc::string::String::from_utf8_lossy(buff);
549+
crate::logger::write_console(&s);
550+
}
551+
}
544552
}
545553
}
546554

mythril/src/virtdev/com.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
use crate::error::Result;
2-
use crate::logger;
32
use crate::physdev::com::*;
43
use crate::vcpu;
54
use crate::virtdev::{
65
DeviceEvent, DeviceEventResponse, DeviceRegion, EmulatedDevice, Event, Port,
76
};
8-
use alloc::string::String;
97
use alloc::sync::Arc;
108
use alloc::vec::Vec;
119
use core::convert::TryInto;
1210
use spin::RwLock;
1311

1412
pub struct Uart8250 {
15-
id: u64,
1613
base_port: Port,
17-
is_newline: bool,
1814
divisor: u16,
1915
receive_buffer: Option<u8>,
2016
interrupt_enable_register: IerFlags,
@@ -27,12 +23,10 @@ pub struct Uart8250 {
2723
}
2824

2925
impl Uart8250 {
30-
pub fn new(vmid: u64, base_port: Port) -> Arc<RwLock<Self>> {
26+
pub fn new(base_port: Port) -> Arc<RwLock<Self>> {
3127
Arc::new(RwLock::new(Self {
32-
id: vmid,
3328
base_port: base_port,
3429
divisor: 0,
35-
is_newline: true,
3630
receive_buffer: None,
3731
interrupt_identification_register: 0x01,
3832
interrupt_enable_register: IerFlags::empty(),
@@ -123,19 +117,9 @@ impl EmulatedDevice for Uart8250 {
123117
if self.divisor_latch_bit_set() {
124118
self.divisor &= 0xff00 | val as u16;
125119
} else {
126-
if self.is_newline {
127-
logger::write_console(&format!(
128-
"GUEST{}: ",
129-
self.id
130-
));
131-
}
132-
133-
let buff = &[val];
134-
let s = String::from_utf8_lossy(buff);
135-
logger::write_console(&s);
136-
137-
self.is_newline = val == 10;
138-
120+
event.responses.push(
121+
DeviceEventResponse::GuestUartTransmitted(val),
122+
);
139123
if self
140124
.interrupt_enable_register
141125
.contains(IerFlags::THR_EMPTY_INTERRUPT)

mythril/src/virtdev/debug.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
use crate::error::Result;
2-
use crate::logger;
3-
use crate::virtdev::{DeviceEvent, DeviceRegion, EmulatedDevice, Event, Port};
4-
use alloc::string::String;
2+
use crate::virtdev::{
3+
DeviceEvent, DeviceEventResponse, DeviceRegion, EmulatedDevice, Event, Port,
4+
};
55
use alloc::sync::Arc;
66
use alloc::vec::Vec;
7+
use core::convert::TryInto;
78
use spin::RwLock;
89

910
pub struct DebugPort {
10-
id: u64,
1111
port: Port,
12-
buff: Vec<u8>,
1312
}
1413

1514
impl DebugPort {
16-
pub fn new(vmid: u64, port: Port) -> Arc<RwLock<Self>> {
17-
Arc::new(RwLock::new(Self {
18-
port,
19-
buff: vec![],
20-
id: vmid,
21-
}))
15+
pub fn new(port: Port) -> Arc<RwLock<Self>> {
16+
Arc::new(RwLock::new(Self { port }))
2217
}
2318
}
2419

@@ -29,20 +24,13 @@ impl EmulatedDevice for DebugPort {
2924

3025
fn on_event(&mut self, event: Event) -> Result<()> {
3126
match event.kind {
32-
DeviceEvent::PortRead((port, mut val)) => {
27+
DeviceEvent::PortRead((_port, mut val)) => {
3328
val.copy_from_u32(0xe9);
3429
}
35-
DeviceEvent::PortWrite((port, val)) => {
36-
self.buff.extend_from_slice(val.as_slice());
37-
38-
// Flush on newlines
39-
if val.as_slice().iter().filter(|b| **b == 10).next().is_some()
40-
{
41-
let s = String::from_utf8_lossy(&self.buff);
42-
43-
logger::write_console(&format!("GUEST{}: {}", self.id, s));
44-
self.buff.clear();
45-
}
30+
DeviceEvent::PortWrite((_port, val)) => {
31+
event.responses.push(
32+
DeviceEventResponse::GuestUartTransmitted(val.try_into()?),
33+
);
4634
}
4735
_ => (),
4836
}

0 commit comments

Comments
 (0)