...
 
Commits (3)
......@@ -204,7 +204,7 @@ impl VirtualCPU for EhyveCPU {
Ok(())
}
fn run(&mut self) -> Result<()> {
fn run(&mut self) -> Result<u8> {
//self.print_registers();
loop {
......@@ -221,7 +221,9 @@ impl VirtualCPU for EhyveCPU {
}
VcpuExit::IoOut(port, addr) => match port {
SHUTDOWN_PORT => {
return Ok(());
// Return the first byte written to the port as
// return value
return Ok(addr[0]);
}
_ => {
self.io_exit(port, std::str::from_utf8(addr).unwrap().to_string())?;
......@@ -236,7 +238,7 @@ impl VirtualCPU for EhyveCPU {
}
}
Ok(())
Ok(0)
}
fn print_registers(&self) {
......
......@@ -328,7 +328,7 @@ impl VirtualCPU for EhyveCPU {
Ok(())
}
fn run(&mut self) -> Result<()> {
fn run(&mut self) -> Result<u8> {
//self.print_registers();
debug!("Run vCPU {}", self.id);
......@@ -404,7 +404,10 @@ impl VirtualCPU for EhyveCPU {
match port {
SHUTDOWN_PORT => {
return Ok(());
// Read the return value from the shutdown
// port
let ret_val = (self.vcpu.read_register(&x86Reg::RAX)? & 0xFF) as u8;
return Ok(ret_val);
}
COM_PORT => {
let al = (self.vcpu.read_register(&x86Reg::RAX)? & 0xFF) as u8;
......@@ -427,6 +430,7 @@ impl VirtualCPU for EhyveCPU {
}
}
}
Ok(0)
}
fn print_registers(&self) {
......
......@@ -120,9 +120,14 @@ fn main() {
let result = cpu.run();
match result {
Ok(()) => {}
Ok(ret_code) => {
if ret_code != 0 {
std::process::exit(ret_code as i32);
}
}
Err(x) => {
error!("CPU {} crashes! {}", tid, x);
std::process::exit(255);
}
}
})
......
......@@ -54,7 +54,7 @@ impl VmParameter {
pub trait VirtualCPU {
fn init(&mut self, entry_point: u64) -> Result<()>;
fn run(&mut self) -> Result<()>;
fn run(&mut self) -> Result<u8>;
fn print_registers(&self);
fn io_exit(&self, port: u16, message: String) -> Result<()> {
......
......@@ -45,11 +45,16 @@ fn handle_cpuid_exit(vp: &mut VirtualProcessor, exit_context: &WHV_RUN_VP_EXIT_C
pub struct EhyveCPU {
id: u32,
vcpu: VirtualProcessor,
ret_val: u8,
}
impl EhyveCPU {
pub fn new(id: u32, vcpu: VirtualProcessor) -> EhyveCPU {
EhyveCPU { id: id, vcpu: vcpu }
EhyveCPU {
id: id,
vcpu: vcpu,
ret_val: 0,
}
}
}
......@@ -126,27 +131,30 @@ impl VirtualCPU for EhyveCPU {
Ok(())
}
fn run(&mut self) -> Result<()> {
fn run(&mut self) -> Result<u8> {
debug!("Run vCPU {}", self.id);
loop {
let exit_context = self.vcpu.run().unwrap();
match exit_context.ExitReason {
WHV_RUN_VP_EXIT_REASON::WHvRunVpExitReasonX64IoPortAccess => {
let mut e = Emulator::new(self).unwrap();
let io_port_access_ctx = unsafe { &exit_context.anon_union.IoPortAccess };
if io_port_access_ctx.PortNumber == SHUTDOWN_PORT {
return Ok(());
let io_port_nr = {
let mut e = Emulator::new(self).unwrap();
let io_port_access_ctx = unsafe { &exit_context.anon_union.IoPortAccess };
let _status = e
.try_io_emulation(
std::ptr::null_mut(),
&exit_context.VpContext,
io_port_access_ctx,
)
.unwrap();
io_port_access_ctx.PortNumber
};
if io_port_nr == SHUTDOWN_PORT {
return Ok(self.ret_val);
}
let _status = e
.try_io_emulation(
std::ptr::null_mut(),
&exit_context.VpContext,
io_port_access_ctx,
)
.unwrap();
}
WHV_RUN_VP_EXIT_REASON::WHvRunVpExitReasonX64Cpuid => {
handle_cpuid_exit(&mut self.vcpu.borrow_mut(), &exit_context)
......@@ -313,18 +321,31 @@ impl EmulatorCallbacks for EhyveCPU {
_context: *mut VOID,
io_access: &mut WHV_EMULATOR_IO_ACCESS_INFO,
) -> HRESULT {
if io_access.Port == 0x3f8 {
let cstr = unsafe {
std::str::from_utf8(std::slice::from_raw_parts(
&io_access.Data as *const _ as *const u8,
io_access.AccessSize as usize,
))
.unwrap()
};
self.io_exit(io_access.Port, cstr.to_string()).unwrap();
} else {
debug!("Ignore IO port 0x{:x}", io_access.Port);
match io_access.Port {
COM_PORT => {
let cstr = unsafe {
std::str::from_utf8(std::slice::from_raw_parts(
&io_access.Data as *const _ as *const u8,
io_access.AccessSize as usize,
))
.unwrap()
};
self.io_exit(io_access.Port, cstr.to_string()).unwrap();
}
SHUTDOWN_PORT => {
let sl = unsafe {
std::slice::from_raw_parts(
&io_access.Data as *const _ as *const u8,
io_access.AccessSize as usize,
)
};
dbg! {sl};
self.ret_val = sl[0];
}
_ => {
debug!("Ignore IO port 0x{:x}", io_access.Port);
}
}
S_OK
......