Skip to content
Draft
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
51 changes: 48 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ thiserror = { version = "2", default-features = false }
time = { version = "0.3", default-features = false }
volatile = "0.6"
zerocopy = { version = "0.8", default-features = false }
uhyve-interface = "0.1.3"
uhyve-interface = "0.2.0"

[dependencies.smoltcp]
version = "0.12"
Expand Down Expand Up @@ -215,6 +215,7 @@ exclude = [
]

[patch.crates-io]
uhyve-interface = { git = "https://github.com/n0toose/uhyve", branch = 'uhyve_if_v2-mmiowrite' }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uhyve-interface = { git = "https://github.com/n0toose/uhyve", branch = 'uhyve_if_v2-mmiowrite' }
uhyve-interface = { git = "https://github.com/n0toose/uhyve", branch = "uhyve_if_v2-mmiowrite" }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be removed, this was added to 'declare' what branch must be used and allow for easier compilation (in theory...)

x86_64 = { git = "https://github.com/rust-osdev/x86_64.git" }
# FIXME: remove once merged: https://github.com/rcore-os/trapframe-rs/pull/16
trapframe = { git = "https://github.com/hermit-os/trapframe-rs", branch = "global_asm" }
Expand Down
19 changes: 15 additions & 4 deletions src/fd/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use core::task::Poll;

use async_trait::async_trait;
use embedded_io::{Read, ReadReady, Write};
use uhyve_interface::parameters::WriteParams;
use uhyve_interface::{GuestVirtAddr, Hypercall};
use memory_addresses::VirtAddr;
use uhyve_interface::GuestPhysAddr;
use uhyve_interface::v2::Hypercall;
use uhyve_interface::v2::parameters::WriteParams;

use crate::arch::mm::paging;
use crate::console::{CONSOLE, CONSOLE_WAKER};
use crate::fd::{
AccessPermission, FileAttr, ObjectInterface, PollEvent, STDERR_FILENO, STDOUT_FILENO,
Expand Down Expand Up @@ -166,7 +169,11 @@ impl ObjectInterface for UhyveStdout {
async fn write(&self, buf: &[u8]) -> io::Result<usize> {
let write_params = WriteParams {
fd: STDOUT_FILENO,
buf: GuestVirtAddr::new(buf.as_ptr() as u64),
buf: GuestPhysAddr::new(
paging::virtual_to_physical(VirtAddr::from_ptr(buf.as_ptr()))
.unwrap()
.as_u64(),
),
len: buf.len(),
};
uhyve_hypercall(Hypercall::FileWrite(&write_params));
Expand Down Expand Up @@ -206,7 +213,11 @@ impl ObjectInterface for UhyveStderr {
async fn write(&self, buf: &[u8]) -> io::Result<usize> {
let write_params = WriteParams {
fd: STDERR_FILENO,
buf: GuestVirtAddr::new(buf.as_ptr() as u64),
buf: GuestPhysAddr::new(
paging::virtual_to_physical(VirtAddr::from_ptr(buf.as_ptr()))
.unwrap()
.as_u64(),
),
len: buf.len(),
};
uhyve_hypercall(Hypercall::FileWrite(&write_params));
Expand Down
17 changes: 13 additions & 4 deletions src/fs/uhyve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use async_lock::Mutex;
use async_trait::async_trait;
use embedded_io::{ErrorType, Read, Write};
use memory_addresses::VirtAddr;
use uhyve_interface::parameters::{
use uhyve_interface::GuestPhysAddr;
use uhyve_interface::v2::Hypercall;
use uhyve_interface::v2::parameters::{
CloseParams, LseekParams, OpenParams, ReadParams, UnlinkParams, WriteParams,
};
use uhyve_interface::{GuestPhysAddr, GuestVirtAddr, Hypercall};

use crate::arch::mm::paging;
use crate::env::fdt;
Expand Down Expand Up @@ -56,7 +57,11 @@ impl Read for UhyveFileHandleInner {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let mut read_params = ReadParams {
fd: self.0,
buf: GuestVirtAddr::new(buf.as_mut_ptr() as u64),
buf: GuestPhysAddr::new(
paging::virtual_to_physical(VirtAddr::from_ptr(buf.as_mut_ptr()))
.unwrap()
.as_u64(),
),
len: buf.len(),
ret: 0,
};
Expand All @@ -74,7 +79,11 @@ impl Write for UhyveFileHandleInner {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let write_params = WriteParams {
fd: self.0,
buf: GuestVirtAddr::new(buf.as_ptr() as u64),
buf: GuestPhysAddr::new(
paging::virtual_to_physical(VirtAddr::from_ptr(buf.as_ptr()))
.unwrap()
.as_u64(),
),
len: buf.len(),
};
uhyve_hypercall(Hypercall::FileWrite(&write_params));
Expand Down
30 changes: 14 additions & 16 deletions src/syscalls/interfaces/uhyve.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use core::ptr;

use memory_addresses::VirtAddr;
use uhyve_interface::parameters::{ExitParams, SerialWriteBufferParams};
use uhyve_interface::{Hypercall, HypercallAddress};
use uhyve_interface::v2::parameters::SerialWriteBufferParams;
use uhyve_interface::v2::{Hypercall, HypercallAddress};

use crate::arch;
use crate::arch::mm::paging::{self, virtual_to_physical};
use crate::syscalls::interfaces::SyscallInterface;

/// perform a SerialWriteBuffer hypercall with `buf` as payload.
/// perform a SerialWriteBuffer hypercall with `buf` as payload
#[inline]
#[cfg_attr(target_arch = "riscv64", expect(dead_code))]
pub(crate) fn serial_buf_hypercall(buf: &[u8]) {
Expand All @@ -31,9 +31,11 @@ fn data_addr<T>(data: &T) -> u64 {
#[inline]
fn hypercall_data(hypercall: &Hypercall<'_>) -> u64 {
match hypercall {
Hypercall::Cmdsize(data) => data_addr(*data),
Hypercall::Cmdval(data) => data_addr(*data),
Hypercall::Exit(data) => data_addr(*data),
// As we are encoding an exit code (max 32 bits) into "an
// address", and memory_addresses complains if an address
// has any bits above the 48th one set to 1, we encode
// potential negative numbers into a u32, then a u64.
Hypercall::Exit(exit_code) => (*exit_code) as u32 as u64,
Hypercall::FileClose(data) => data_addr(*data),
Hypercall::FileLseek(data) => data_addr(*data),
Hypercall::FileOpen(data) => data_addr(*data),
Expand All @@ -50,24 +52,21 @@ fn hypercall_data(hypercall: &Hypercall<'_>) -> u64 {
#[inline]
#[allow(unused_variables)] // until riscv64 is implemented
pub(crate) fn uhyve_hypercall(hypercall: Hypercall<'_>) {
let ptr = HypercallAddress::from(&hypercall) as u16;
let ptr = HypercallAddress::from(&hypercall) as u64;
let data = hypercall_data(&hypercall);

#[cfg(target_arch = "x86_64")]
unsafe {
use x86_64::instructions::port::Port;

let data =
u32::try_from(data).expect("Hypercall data must lie in the first 4GiB of memory");
Port::new(ptr).write(data);
{
let ptr = ptr as *mut u64;
unsafe { ptr.write_volatile(data) };
}

#[cfg(target_arch = "aarch64")]
unsafe {
use core::arch::asm;
asm!(
"str x8, [{ptr}]",
ptr = in(reg) u64::from(ptr),
ptr = in(reg) ptr,
in("x8") data,
options(nostack),
);
Expand All @@ -81,8 +80,7 @@ pub struct Uhyve;

impl SyscallInterface for Uhyve {
fn shutdown(&self, error_code: i32) -> ! {
let sysexit = ExitParams { arg: error_code };
uhyve_hypercall(Hypercall::Exit(&sysexit));
uhyve_hypercall(Hypercall::Exit(error_code));

loop {
arch::processor::halt();
Expand Down
Loading