Skip to content

Commit

Permalink
Renamed IOPort in uhyve interface to HypercallAddress, to make it gen…
Browse files Browse the repository at this point in the history
…eric for non x86 architectures
  • Loading branch information
jounathaen committed Jan 11, 2023
1 parent bb003ed commit 14bd171
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/linux/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl VirtualCPU for UhyveCPU {
},
VcpuExit::IoOut(port, addr) => {
let data_addr: usize = unsafe { (*(addr.as_ptr() as *const u32)) as usize };
if let Some(hypercall) = self.port_to_hypercall(port, data_addr) {
if let Some(hypercall) = self.address_to_hypercall(port, data_addr) {
match hypercall {
Hypercall::Cmdsize(syssize) => self.cmdsize(syssize),
Hypercall::Cmdval(syscmdval) => self.cmdval(syscmdval),
Expand Down
3 changes: 2 additions & 1 deletion src/macos/aarch64/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ impl VirtualCPU for UhyveCPU {
let pc = self.vcpu.read_register(Register::PC)?;

let data_addr = self.vcpu.read_register(Register::X8)?;
if let Some(hypercall) = self.port_to_hypercall(addr, data_addr as usize) {
if let Some(hypercall) = self.address_to_hypercall(addr, data_addr as usize)
{
match hypercall {
Hypercall::SerialWrite(_buf) => {
let x8 = (self.vcpu.read_register(Register::X8)? & 0xFF) as u8;
Expand Down
2 changes: 1 addition & 1 deletion src/macos/x86_64/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ impl VirtualCPU for UhyveCPU {
assert!(!input, "Invalid I/O operation");

let data_addr: u64 = self.vcpu.read_register(&Register::RAX)? & 0xFFFFFFFF;
if let Some(hypercall) = self.port_to_hypercall(port, data_addr as usize) {
if let Some(hypercall) = self.address_to_hypercall(port, data_addr as usize) {
match hypercall {
Hypercall::Cmdsize(syssize) => self.cmdsize(syssize),
Hypercall::Cmdval(syscmdval) => self.cmdval(syscmdval),
Expand Down
29 changes: 15 additions & 14 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use hermit_entry::{
};
use log::{error, warn};
use thiserror::Error;
use uhyve_interface::{parameters::*, Hypercall, IoPorts, MAX_ARGC_ENVC};
use uhyve_interface::{parameters::*, Hypercall, HypercallAddress, MAX_ARGC_ENVC};

#[cfg(target_arch = "x86_64")]
use crate::arch::x86_64::{
Expand Down Expand Up @@ -72,56 +72,56 @@ pub trait VirtualCPU {
fn args(&self) -> &[OsString];

/// addr is the address of the hypercall parameter in the guest's memory space.
fn port_to_hypercall(&self, port: u16, data_addr: usize) -> Option<Hypercall<'_>> {
if let Ok(hypercall_port) = IoPorts::try_from(port) {
fn address_to_hypercall(&self, addr: u16, data_addr: usize) -> Option<Hypercall<'_>> {
if let Ok(hypercall_port) = HypercallAddress::try_from(addr) {
Some(match hypercall_port {
IoPorts::FileClose => {
HypercallAddress::FileClose => {
let sysclose =
unsafe { &mut *(self.host_address(data_addr) as *mut CloseParams) };
Hypercall::FileClose(sysclose)
}
IoPorts::FileLseek => {
HypercallAddress::FileLseek => {
let syslseek =
unsafe { &mut *(self.host_address(data_addr) as *mut LseekParams) };
Hypercall::FileLseek(syslseek)
}
IoPorts::FileOpen => {
HypercallAddress::FileOpen => {
let sysopen =
unsafe { &mut *(self.host_address(data_addr) as *mut OpenParams) };
Hypercall::FileOpen(sysopen)
}
IoPorts::FileRead => {
HypercallAddress::FileRead => {
let sysread = unsafe { &mut *(self.host_address(data_addr) as *mut ReadPrams) };
Hypercall::FileRead(sysread)
}
IoPorts::FileWrite => {
HypercallAddress::FileWrite => {
let syswrite =
unsafe { &*(self.host_address(data_addr) as *const WriteParams) };
Hypercall::FileWrite(syswrite)
}
IoPorts::FileUnlink => {
HypercallAddress::FileUnlink => {
let sysunlink =
unsafe { &mut *(self.host_address(data_addr) as *mut UnlinkParams) };
Hypercall::FileUnlink(sysunlink)
}
IoPorts::Exit => {
HypercallAddress::Exit => {
let sysexit = unsafe { &*(self.host_address(data_addr) as *const ExitParams) };
Hypercall::Exit(sysexit)
}
//HypercallPorts::Netwrite => {}
//HypercallPorts::Netread => {}
//HypercallPorts::Netstat => {}
IoPorts::Cmdsize => {
HypercallAddress::Cmdsize => {
let syssize =
unsafe { &mut *(self.host_address(data_addr) as *mut CmdsizeParams) };
Hypercall::Cmdsize(syssize)
}
IoPorts::Cmdval => {
HypercallAddress::Cmdval => {
let syscmdval =
unsafe { &*(self.host_address(data_addr) as *const CmdvalParams) };
Hypercall::Cmdval(syscmdval)
}
IoPorts::Uart => {
HypercallAddress::Uart => {
let buf = unsafe { &*(self.host_address(data_addr) as *const &[u8]) };
Hypercall::SerialWrite(buf)
}
Expand Down Expand Up @@ -353,7 +353,8 @@ pub trait Vm {
hardware_info: HardwareInfo {
phys_addr_range: arch::RAM_START..arch::RAM_START + vm_mem_len as u64,
serial_port_base: self.verbose().then(|| {
SerialPortBase::new((uhyve_interface::IoPorts::Uart as u16).into()).unwrap()
SerialPortBase::new((uhyve_interface::HypercallAddress::Uart as u16).into())
.unwrap()
}),
},
load_info,
Expand Down
8 changes: 3 additions & 5 deletions uhyve-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
//! that port is the physical memory address (of the VM) of the parameters of that hypercall.
//! - On `aarch64` you write to the respective [`HypercallAddress`]. The 64-bit value written to that location is the guest's physical memory address of the hypercall's parameter.

// TODO: only x86 allows io instructions. Other architectures should use MMIO

#![no_std]

// TODO: Throw this out, once https://github.com/rust-lang/rfcs/issues/2783 is resolved
Expand All @@ -29,7 +27,7 @@ pub const UHYVE_INTERFACE_VERSION: u32 = 1;
#[non_exhaustive]
#[repr(u16)]
#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum IoPorts {
pub enum HypercallAddress {
/// Port address = `0x400`
FileWrite = 0x400,
/// Port address = `0x440`
Expand Down Expand Up @@ -60,7 +58,7 @@ pub enum IoPorts {
/// Port address = `0x840`
FileUnlink = 0x840,
}
impl From<Hypercall<'_>> for IoPorts {
impl From<Hypercall<'_>> for HypercallAddress {
fn from(value: Hypercall) -> Self {
match value {
Hypercall::Cmdsize(_) => Self::Cmdsize,
Expand Down Expand Up @@ -101,7 +99,7 @@ pub enum Hypercall<'a> {
impl<'a> Hypercall<'a> {
/// Get a hypercall's port address.
pub fn port(self) -> u16 {
IoPorts::from(self) as u16
HypercallAddress::from(self) as u16
}
}

Expand Down

0 comments on commit 14bd171

Please sign in to comment.