Skip to content

Commit

Permalink
Merge pull request #32 from Alignof/develop
Browse files Browse the repository at this point in the history
Ver 0.3.0
  • Loading branch information
Alignof authored Sep 3, 2024
2 parents 99a46fc + 8eefa5d commit a6bea58
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hikami"
version = "0.2.0"
version = "0.3.0"
edition = "2021"

[lints.clippy]
Expand Down
7 changes: 7 additions & 0 deletions src/sbi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//! Ref: [https://github.com/rustsbi/rustsbi-qemu](https://github.com/rustsbi/rustsbi-qemu)
//! Document: [https://docs.rs/rustsbi/0.4.0-alpha.1/rustsbi/derive.RustSBI.html](https://docs.rs/rustsbi/0.4.0-alpha.1/rustsbi/derive.RustSBI.html)
mod rfence;

use crate::device::{clint, uart, Device};
use fdt::Fdt;
use rustsbi::RustSBI;
Expand All @@ -17,13 +19,18 @@ pub struct Sbi {
/// For debug console.
#[rustsbi(console)]
pub uart: uart::Uart,

/// Remote fence
#[rustsbi(fence)]
pub rfence: rfence::RemoteFence,
}

impl Sbi {
pub fn new(device_tree: Fdt) -> Self {
Sbi {
uart: uart::Uart::new(&device_tree, "/soc/serial"),
clint: clint::Clint::new(&device_tree, "/soc/clint"),
rfence: rfence::RemoteFence,
}
}
}
59 changes: 59 additions & 0 deletions src/sbi/rfence.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! Remote fence implementation for `RustSBI`.
use core::arch::asm;
use rustsbi::{HartMask, SbiRet};

use crate::memmap::constant::MAX_HART_NUM;
use crate::memmap::page_table::constants::PAGE_SIZE;

/// Remote fence implementation.
/// ref: [https://docs.rs/rustsbi/0.4.0-alpha.3/rustsbi/trait.Fence.html](https://docs.rs/rustsbi/0.4.0-alpha.3/rustsbi/trait.Fence.html)
pub struct RemoteFence;

impl rustsbi::Fence for RemoteFence {
// Required methods
fn remote_fence_i(&self, hart_mask: HartMask) -> SbiRet {
// current hart must be 0.
for hart_id in 1..MAX_HART_NUM {
debug_assert!(!hart_mask.has_bit(hart_id));
}

if hart_mask.has_bit(0) {
unsafe { asm!("fence.i") }
}

SbiRet::success(0)
}

fn remote_sfence_vma(&self, hart_mask: HartMask, start_addr: usize, size: usize) -> SbiRet {
// current hart must be 0.
for hart_id in 1..MAX_HART_NUM {
debug_assert!(!hart_mask.has_bit(hart_id));
}

for addr in (start_addr..start_addr + size).step_by(PAGE_SIZE) {
unsafe { asm!("sfence.vma {vaddr}, x0", vaddr = in(reg) addr) }
}

SbiRet::success(0)
}

fn remote_sfence_vma_asid(
&self,
hart_mask: HartMask,
start_addr: usize,
size: usize,
asid: usize,
) -> SbiRet {
// current hart must be 0.
for hart_id in 1..MAX_HART_NUM {
debug_assert!(!hart_mask.has_bit(hart_id));
}

for addr in (start_addr..start_addr + size).step_by(PAGE_SIZE) {
unsafe { asm!("sfence.vma {vaddr}, {asid}", vaddr = in(reg) addr, asid = in(reg) asid) }
}

SbiRet::success(0)
}
}
12 changes: 10 additions & 2 deletions src/trap/hypervisor_supervisor/exception.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Trap VS-mode exception.
mod sbi;
mod sbi_handler;

use super::hstrap_exit;
use crate::guest;
Expand All @@ -12,7 +12,7 @@ use riscv::register::{
scause::{self, Exception},
stval,
};
use sbi::sbi_base_handler;
use sbi_handler::{sbi_base_handler, sbi_rfnc_handler};

/// Delegate exception to supervisor mode from VS-mode.
#[no_mangle]
Expand All @@ -37,9 +37,17 @@ pub extern "C" fn hs_forward_exception() {
fn sbi_vs_mode_handler(context: &mut guest::context::Context) {
let ext_id: usize = context.xreg(17) as usize;
let func_id: usize = context.xreg(16) as usize;
let arguments: &[u64; 5] = &[
context.xreg(10),
context.xreg(11),
context.xreg(12),
context.xreg(13),
context.xreg(14),
];

let sbiret = match ext_id {
sbi_spec::base::EID_BASE => sbi_base_handler(func_id),
sbi_spec::rfnc::EID_RFNC => sbi_rfnc_handler(func_id, arguments),
_ => panic!(
"Unsupported SBI call, eid: {:x}, fid: {:x}",
ext_id, func_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,27 @@ pub fn sbi_base_handler(func_id: usize) -> SbiRet {
value: result_value,
}
}

/// sbi ecall handler for RFENCE Extension (EID: #0x52464E43)
#[allow(clippy::module_name_repetitions, clippy::cast_possible_truncation)]
pub fn sbi_rfnc_handler(func_id: usize, args: &[u64; 5]) -> SbiRet {
use rustsbi::HartMask;
use sbi_spec::rfnc::{REMOTE_FENCE_I, REMOTE_SFENCE_VMA, REMOTE_SFENCE_VMA_ASID};
match func_id {
REMOTE_FENCE_I => {
sbi_rt::remote_fence_i(HartMask::from_mask_base(args[0] as usize, args[1] as usize))
}
REMOTE_SFENCE_VMA => sbi_rt::remote_sfence_vma(
HartMask::from_mask_base(args[0] as usize, args[1] as usize),
args[2] as usize,
args[3] as usize,
),
REMOTE_SFENCE_VMA_ASID => sbi_rt::remote_sfence_vma_asid(
HartMask::from_mask_base(args[0] as usize, args[1] as usize),
args[2] as usize,
args[3] as usize,
args[4] as usize,
),
_ => panic!("unsupported fid: {}", func_id),
}
}

0 comments on commit a6bea58

Please sign in to comment.