Skip to content

Commit

Permalink
Merge pull request #1538 from hermit-os/fd-uhyve-interface
Browse files Browse the repository at this point in the history
refactor(stdio): migrate to `uhyve-interface`
  • Loading branch information
mkroening authored Jan 3, 2025
2 parents 30517b9 + e54e427 commit 1a7e6a3
Showing 1 changed file with 17 additions and 67 deletions.
84 changes: 17 additions & 67 deletions src/fd/stdio.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1,16 @@
use alloc::boxed::Box;
use core::future;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
use core::ptr;
use core::task::Poll;

use async_trait::async_trait;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
use memory_addresses::VirtAddr;
#[cfg(target_arch = "x86_64")]
use x86::io::*;
use uhyve_interface::parameters::WriteParams;
use uhyve_interface::{GuestVirtAddr, Hypercall};
use zerocopy::IntoBytes;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
use crate::arch::mm::paging;
use crate::console::CONSOLE;
use crate::fd::{ObjectInterface, PollEvent, STDERR_FILENO, STDOUT_FILENO};
use crate::io;

const UHYVE_PORT_WRITE: u16 = 0x400;

#[repr(C, packed)]
struct SysWrite {
fd: i32,
buf: *const u8,
len: usize,
}

impl SysWrite {
pub fn new(fd: i32, buf: *const u8, len: usize) -> SysWrite {
SysWrite { fd, buf, len }
}
}

/// forward a request to the hypervisor uhyve
#[inline]
#[cfg(target_arch = "x86_64")]
fn uhyve_send<T>(port: u16, data: &mut T) {
let ptr = VirtAddr::from_ptr(ptr::from_mut(data));
let physical_address = paging::virtual_to_physical(ptr).unwrap();

unsafe {
outl(port, physical_address.as_u64() as u32);
}
}

/// forward a request to the hypervisor uhyve
#[inline]
#[cfg(target_arch = "aarch64")]
fn uhyve_send<T>(port: u16, data: &mut T) {
use core::arch::asm;

let ptr = VirtAddr::from_ptr(ptr::from_mut(data));
let physical_address = paging::virtual_to_physical(ptr).unwrap();

unsafe {
asm!(
"str x8, [{port}]",
port = in(reg) u64::from(port),
in("x8") physical_address.as_u64(),
options(nostack),
);
}
}

/// forward a request to the hypervisor uhyve
#[inline]
#[cfg(target_arch = "riscv64")]
fn uhyve_send<T>(_port: u16, _data: &mut T) {
todo!()
}
use crate::syscalls::interfaces::uhyve_hypercall;

#[derive(Debug)]
pub struct GenericStdin;
Expand Down Expand Up @@ -185,10 +127,14 @@ impl ObjectInterface for UhyveStdout {
}

async fn write(&self, buf: &[u8]) -> io::Result<usize> {
let mut syswrite = SysWrite::new(STDOUT_FILENO, buf.as_ptr(), buf.len());
uhyve_send(UHYVE_PORT_WRITE, &mut syswrite);
let write_params = WriteParams {
fd: STDOUT_FILENO,
buf: GuestVirtAddr::new(buf.as_ptr() as u64),
len: buf.len(),
};
uhyve_hypercall(Hypercall::FileWrite(&write_params));

Ok(syswrite.len)
Ok(write_params.len)
}
}

Expand All @@ -209,10 +155,14 @@ impl ObjectInterface for UhyveStderr {
}

async fn write(&self, buf: &[u8]) -> io::Result<usize> {
let mut syswrite = SysWrite::new(STDERR_FILENO, buf.as_ptr(), buf.len());
uhyve_send(UHYVE_PORT_WRITE, &mut syswrite);
let write_params = WriteParams {
fd: STDERR_FILENO,
buf: GuestVirtAddr::new(buf.as_ptr() as u64),
len: buf.len(),
};
uhyve_hypercall(Hypercall::FileWrite(&write_params));

Ok(syswrite.len)
Ok(write_params.len)
}
}

Expand Down

0 comments on commit 1a7e6a3

Please sign in to comment.