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
10 changes: 7 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,10 @@ jobs:
CARGO_BUILD_TARGET: riscv64gc-unknown-linux-gnu
CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER: riscv64-linux-gnu-gcc
CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_RUNNER: qemu-riscv64 -L /usr/riscv64-linux-gnu
# Tests that re-execute themselves run through binfmt_misc, which needs the RV64 loader too.
QEMU_LD_PREFIX: /usr/riscv64-linux-gnu
# Grow this list as each crate gains RV64 support.
RISCV64_CRATES: -p litebox -p litebox_platform -p litebox_trampoline -p litebox_common_linux
RISCV64_CRATES: -p litebox -p litebox_platform -p litebox_trampoline -p litebox_common_linux -p litebox_platform_linux_userland
steps:
- name: Check out repo
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
Expand All @@ -297,9 +299,11 @@ jobs:
- name: Install RV64 cross toolchain and emulator
run: |
sudo apt-get update -qq
sudo apt-get install -y --no-install-recommends gcc-riscv64-linux-gnu libc6-dev-riscv64-cross qemu-user
sudo apt-get install -y --no-install-recommends gcc-riscv64-linux-gnu libc6-dev-riscv64-cross qemu-user qemu-user-binfmt
- run: ./.github/tools/github_actions_run_cargo clippy --all-targets --all-features $RISCV64_CRATES
- run: ./.github/tools/github_actions_run_cargo nextest --features litebox_platform/lock_tracing $RISCV64_CRATES
# QEMU user mode does not emulate seccomp (`prctl(PR_SET_SECCOMP)` fails with `EINVAL`,
# `seccomp` with `ENOSYS`), so the platform tests that install a filter run only on RV64 Linux.
- run: ./.github/tools/github_actions_run_cargo nextest --features litebox_platform/lock_tracing $RISCV64_CRATES -E 'not (package(litebox_platform_linux_userland) and test(/seccomp|filtered_process/))'
- name: Execute an ahead-of-time rewritten RV64 guest
run: ./.github/tools/github_actions_run_cargo nextest --target x86_64-unknown-linux-gnu -p litebox_syscall_rewriter --test riscv64_tests --run-ignored only

Expand Down
39 changes: 39 additions & 0 deletions litebox/src/arch/riscv64/fp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//! Host-independent RV64 floating-point register storage.

use core::mem::offset_of;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

/// A thread's F and D extension state, laid out as Linux
/// `struct __riscv_d_ext_state`: `f0`-`f31` at `8 * i`, then `fcsr` at
/// [`Self::FCSR_OFFSET`]. The V extension is not supported.
#[repr(C)]
#[derive(
Clone, Copy, Debug, Default, PartialEq, Eq, FromBytes, IntoBytes, Immutable, KnownLayout,
)]
pub struct FpState {
/// `f0`-`f31` as `fsd` stores them; single-precision values are NaN-boxed.
pub f: [u64; 32],
/// The floating-point control and status register.
pub fcsr: u32,
/// The kernel structure's trailing padding.
reserved: u32,
}

impl FpState {
/// Byte offset of [`Self::fcsr`], for transition assembly.
pub const FCSR_OFFSET: usize = offset_of!(Self, fcsr);

/// Returns the state with the given registers.
pub const fn new(f: [u64; 32], fcsr: u32) -> Self {
Self {
f,
fcsr,
reserved: 0,
}
}
}

const _: () = assert!(size_of::<FpState>() == 264 && FpState::FCSR_OFFSET == 256);
4 changes: 3 additions & 1 deletion litebox/src/arch/riscv64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//! RISC-V guest address bounds.
//! RISC-V helpers independent of host operating systems and guest ABIs.

pub mod fp;

/// Inclusive lower bound for user instruction and stack pointers, excluding the null guard.
pub const USER_ADDR_MIN: usize = 0x1_0000;
Expand Down
9 changes: 6 additions & 3 deletions litebox/src/platform/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ pub trait GuestVectorStateProvider {
fn get_guest_sve_vl(&self) -> Option<crate::arch::aarch64::vector::SveVectorLength>;
}

// TODO: replace this blanket with architecture-specific providers before
// implementing guest vector-state preservation outside AArch64.
#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
// Architectures without guest FP state preservation.
#[cfg(not(any(
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "x86_64"
)))]
impl<T> GuestVectorStateProvider for T {
type GuestVectorState = ();

Expand Down
26 changes: 26 additions & 0 deletions litebox/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,32 @@ pub struct ExceptionInfo {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Exception(pub usize);

#[cfg(target_arch = "riscv64")]
impl Exception {
/// Instruction address misaligned.
pub const INSTRUCTION_ADDRESS_MISALIGNED: Self = Self(0);
/// Instruction access fault.
pub const INSTRUCTION_ACCESS_FAULT: Self = Self(1);
/// Illegal instruction.
pub const ILLEGAL_INSTRUCTION: Self = Self(2);
/// Breakpoint (`ebreak`).
pub const BREAKPOINT: Self = Self(3);
/// Load address misaligned.
pub const LOAD_ADDRESS_MISALIGNED: Self = Self(4);
/// Load access fault.
pub const LOAD_ACCESS_FAULT: Self = Self(5);
/// Store/AMO address misaligned.
pub const STORE_ADDRESS_MISALIGNED: Self = Self(6);
/// Store/AMO access fault.
pub const STORE_ACCESS_FAULT: Self = Self(7);
/// Instruction page fault.
pub const INSTRUCTION_PAGE_FAULT: Self = Self(12);
/// Load page fault.
pub const LOAD_PAGE_FAULT: Self = Self(13);
/// Store/AMO page fault.
pub const STORE_PAGE_FAULT: Self = Self(15);
}

/// An x86 exception type.
#[cfg(target_arch = "x86_64")]
#[repr(transparent)]
Expand Down
2 changes: 2 additions & 0 deletions litebox_common_linux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ mod guest_vector_state {
pub type Selected = litebox::arch::x86_64::xstate::XsaveArea;
} else if #[cfg(target_arch = "aarch64")] {
pub type Selected = litebox::arch::aarch64::vector::VectorArea;
} else if #[cfg(target_arch = "riscv64")] {
pub type Selected = litebox::arch::riscv64::fp::FpState;
} else {
pub type Selected = ();
}
Expand Down
51 changes: 46 additions & 5 deletions litebox_common_linux/src/program_startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ impl LinuxForkStartup {
output.extend_from_slice(self.signal_actions.as_bytes());
output.extend_from_slice(self.alternate_signal_stack.as_bytes());
output.extend_from_slice(self.registers.as_bytes());
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
#[cfg(any(
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "x86_64"
))]
push_vector_state(&mut output, &self.vector_state);
#[cfg(target_arch = "aarch64")]
output.push(u8::from(self.sve_vl_inherit));
Expand Down Expand Up @@ -446,7 +450,11 @@ impl LinuxForkStartup {
let signal_actions = read_value(&mut input)?;
let alternate_signal_stack = read_value(&mut input)?;
let registers = read_value(&mut input)?;
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
#[cfg(any(
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "x86_64"
))]
let vector_state = read_vector_state(&mut input)?;
#[cfg(target_arch = "aarch64")]
let sve_vl_inherit = read_bool(&mut input)?;
Expand Down Expand Up @@ -492,9 +500,17 @@ impl LinuxForkStartup {
signal_actions,
alternate_signal_stack,
registers,
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
#[cfg(any(
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "x86_64"
))]
vector_state,
#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
#[cfg(not(any(
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "x86_64"
)))]
vector_state: (),
#[cfg(target_arch = "aarch64")]
sve_vl_inherit,
Expand Down Expand Up @@ -690,6 +706,17 @@ fn read_vector_state(input: &mut &[u8]) -> Result<GuestVectorState, LinuxProgram
GuestVectorState::from_bytes(bytes, layout).ok_or(LinuxProgramStartupError::Malformed)
}

/// Appends the fixed-size F/D state.
#[cfg(target_arch = "riscv64")]
fn push_vector_state(output: &mut Vec<u8>, state: &GuestVectorState) {
output.extend_from_slice(state.as_bytes());
}

#[cfg(target_arch = "riscv64")]
fn read_vector_state(input: &mut &[u8]) -> Result<GuestVectorState, LinuxProgramStartupError> {
read_value(input)
}

fn read_u32(input: &mut &[u8]) -> Result<u32, LinuxProgramStartupError> {
let bytes = take_bytes(input, size_of::<u32>())?;
Ok(u32::from_le_bytes(
Expand Down Expand Up @@ -899,7 +926,19 @@ mod tests {
GuestVectorState::initial(litebox::arch::x86_64::xstate::XsaveLayout::get())
}

#[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))]
#[cfg(target_arch = "riscv64")]
fn fork_vector_state() -> GuestVectorState {
GuestVectorState::new(
core::array::from_fn(|index| u64::MAX / 3 - index as u64),
0xe1,
)
}

#[cfg(not(any(
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "x86_64"
)))]
fn fork_vector_state() -> GuestVectorState {}

fn fork_startup() -> LinuxForkStartup {
Expand Down Expand Up @@ -992,6 +1031,8 @@ mod tests {
assert_eq!(decoded.vector_state, not_live.vector_state);
assert_eq!(decoded.encode().unwrap(), not_live_encoded);
}
#[cfg(target_arch = "riscv64")]
assert_eq!(decoded.vector_state, startup.vector_state);
assert_eq!(decoded.encode().unwrap(), encoded);
}

Expand Down
Loading
Loading