-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce RISC-V architecture support #190
Open
TimePrinciple
wants to merge
7
commits into
rust-vmm:main
Choose a base branch
from
TimePrinciple:introduce-riscv-architecture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4dad6a6
loader: riscv: Initial support
endeneer 8c4bb23
riscv64: Refactor `loader`
TimePrinciple ac10ed7
riscv64: Implement `Configurator`
TimePrinciple ac02bc5
README: Add doc for configuring boot parameters
TimePrinciple 017f368
chore: Rename `config` to `config.toml`
TimePrinciple 1e09639
ci: Enable CI on riscv64
TimePrinciple e8a636f
benchmark: Add riscv64 benchmark tests
TimePrinciple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
x86_64 | ||
aarch64 | ||
riscv64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE-BSD-3-Clause file. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause | ||
extern crate criterion; | ||
extern crate linux_loader; | ||
extern crate vm_memory; | ||
|
||
use linux_loader::configurator::fdt::FdtBootConfigurator; | ||
use linux_loader::configurator::{BootConfigurator, BootParams}; | ||
use vm_memory::{ByteValued, GuestAddress, GuestMemoryMmap}; | ||
|
||
use criterion::{black_box, Criterion}; | ||
|
||
const MEM_SIZE: usize = 0x100_0000; | ||
const FDT_MAX_SIZE: usize = 0x20; | ||
|
||
fn create_guest_memory() -> GuestMemoryMmap { | ||
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), MEM_SIZE)]).unwrap() | ||
} | ||
|
||
#[derive(Clone, Copy, Default)] | ||
#[allow(dead_code)] | ||
pub struct FdtPlaceholder([u8; FDT_MAX_SIZE]); | ||
|
||
// SAFETY: The layout of the structure is fixed and can be initialized by | ||
// reading its content from byte array. | ||
unsafe impl ByteValued for FdtPlaceholder {} | ||
|
||
fn build_fdt_boot_params() -> BootParams { | ||
let fdt = FdtPlaceholder([0u8; FDT_MAX_SIZE]); | ||
let fdt_addr = GuestAddress((MEM_SIZE - FDT_MAX_SIZE - 1) as u64); | ||
BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr) | ||
} | ||
|
||
pub fn criterion_benchmark(c: &mut Criterion) { | ||
let guest_mem = create_guest_memory(); | ||
let fdt_boot_params = build_fdt_boot_params(); | ||
c.bench_function("configure_fdt", |b| { | ||
b.iter(|| { | ||
black_box(FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>( | ||
&fdt_boot_params, | ||
&guest_mem, | ||
)) | ||
.unwrap(); | ||
}) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is also a 1:1 copy of the one in the aarch64 module. Let's figure out a way to reuse these somehow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause | ||
|
||
//! Traits and structs for loading the device tree. | ||
|
||
use vm_memory::{Bytes, GuestMemory}; | ||
|
||
use std::fmt; | ||
|
||
use crate::configurator::{BootConfigurator, BootParams, Error as BootConfiguratorError, Result}; | ||
|
||
/// Errors specific to the device tree boot protocol configuration. | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum Error { | ||
/// FDT does not fit in guest memory. | ||
FDTPastRamEnd, | ||
/// Error writing FDT in memory. | ||
WriteFDTToMemory, | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
use Error::*; | ||
let desc = match self { | ||
FDTPastRamEnd => "FDT does not fit in guest memory.", | ||
WriteFDTToMemory => "error writing FDT in guest memory.", | ||
}; | ||
|
||
write!(f, "Device Tree Boot Configurator: {}", desc) | ||
} | ||
} | ||
|
||
impl std::error::Error for Error {} | ||
|
||
impl From<Error> for BootConfiguratorError { | ||
fn from(err: Error) -> Self { | ||
BootConfiguratorError::Fdt(err) | ||
} | ||
} | ||
|
||
/// Boot configurator for device tree. | ||
pub struct FdtBootConfigurator {} | ||
|
||
impl BootConfigurator for FdtBootConfigurator { | ||
/// Writes the boot parameters (configured elsewhere) into guest memory. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `params` - boot parameters containing the FDT. | ||
/// * `guest_memory` - guest's physical memory. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// # extern crate vm_memory; | ||
/// # use linux_loader::configurator::{BootConfigurator, BootParams}; | ||
/// # use linux_loader::configurator::fdt::FdtBootConfigurator; | ||
/// # use vm_memory::{Address, ByteValued, GuestMemory, GuestMemoryMmap, GuestAddress}; | ||
/// # #[derive(Clone, Copy, Default)] | ||
/// # struct FdtPlaceholder([u8; 0x20]); | ||
/// # unsafe impl ByteValued for FdtPlaceholder {} | ||
/// # fn create_guest_memory() -> GuestMemoryMmap { | ||
/// # GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (0x100_0000 as usize))]).unwrap() | ||
/// # } | ||
/// # fn create_fdt(guest_memory: &GuestMemoryMmap) -> (FdtPlaceholder, GuestAddress) { | ||
/// # let last_addr = guest_memory.last_addr().raw_value(); | ||
/// # (FdtPlaceholder([0u8; 0x20]), GuestAddress(last_addr - 0x20u64)) | ||
/// # } | ||
/// # fn main() { | ||
/// let guest_memory = create_guest_memory(); | ||
/// let (fdt, fdt_addr) = create_fdt(&guest_memory); | ||
/// FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>( | ||
/// &BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr), | ||
/// &guest_memory, | ||
/// ) | ||
/// .unwrap(); | ||
/// # } | ||
/// ``` | ||
fn write_bootparams<M>(params: &BootParams, guest_memory: &M) -> Result<()> | ||
where | ||
M: GuestMemory, | ||
{ | ||
guest_memory | ||
.checked_offset(params.header_start, params.header.len()) | ||
.ok_or(Error::FDTPastRamEnd)?; | ||
|
||
// The VMM has filled an FDT and passed it as a `ByteValued` object. | ||
guest_memory | ||
.write_slice(params.header.as_slice(), params.header_start) | ||
.map_err(|_| Error::WriteFDTToMemory.into()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#![allow(clippy::undocumented_unsafe_blocks)] | ||
use super::*; | ||
use vm_memory::{Address, ByteValued, GuestAddress, GuestMemoryMmap}; | ||
|
||
const FDT_MAX_SIZE: usize = 0x20; | ||
const MEM_SIZE: u64 = 0x100_0000; | ||
|
||
fn create_guest_mem() -> GuestMemoryMmap { | ||
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap() | ||
} | ||
|
||
#[derive(Clone, Copy, Default)] | ||
#[allow(dead_code)] // rustc thinks the field is never read, but it is via `ByteValued` impl | ||
struct FdtPlaceholder([u8; FDT_MAX_SIZE]); | ||
unsafe impl ByteValued for FdtPlaceholder {} | ||
|
||
#[test] | ||
fn test_configure_fdt_boot() { | ||
let fdt = FdtPlaceholder([0u8; FDT_MAX_SIZE]); | ||
let guest_memory = create_guest_mem(); | ||
|
||
// Error case: FDT doesn't fit in guest memory. | ||
let fdt_addr = GuestAddress(guest_memory.last_addr().raw_value() - FDT_MAX_SIZE as u64 + 1); | ||
assert_eq!( | ||
FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>( | ||
&BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr), | ||
&guest_memory, | ||
) | ||
.err(), | ||
Some(Error::FDTPastRamEnd.into()) | ||
); | ||
|
||
let fdt_addr = GuestAddress(guest_memory.last_addr().raw_value() - FDT_MAX_SIZE as u64); | ||
assert!(FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>( | ||
&BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr), | ||
&guest_memory, | ||
) | ||
.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_error_messages() { | ||
assert_eq!( | ||
format!("{}", Error::FDTPastRamEnd), | ||
"Device Tree Boot Configurator: FDT does not fit in guest memory." | ||
); | ||
assert_eq!( | ||
format!("{}", Error::WriteFDTToMemory), | ||
"Device Tree Boot Configurator: error writing FDT in guest memory." | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause | ||
|
||
//! Traits and structs for configuring and loading boot parameters on `riscv64`. | ||
|
||
pub mod fdt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of duplicating this entire benchmark, let's just rename the
aarch64.rs
file to something more appropriate (fdt.rs
, maybe), and simply have it cfg'd with `any(target_arch = "aarch64", target_arch = "riscv64")