Skip to content
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

Use const operands in asm macro #60

Merged
merged 5 commits into from
Oct 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.81.0"
channel = "1.82.0"
components = [ "rustfmt", "clippy" ]
targets = [ "riscv64imac-unknown-none-elf" ]
3 changes: 1 addition & 2 deletions src/guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ impl Guest {
guest_dtb: &'static [u8; include_bytes!("../guest.dtb").len()],
memory_region: Range<GuestPhysicalAddress>,
) -> Self {
let stack_top_addr =
unsafe { HostPhysicalAddress(core::ptr::addr_of!(crate::_stack_start) as usize) };
let stack_top_addr = HostPhysicalAddress(core::ptr::addr_of!(crate::_stack_start) as usize);
let page_table_addr = HostPhysicalAddress(root_page_table.as_ptr() as usize);

page_table::sv39x4::initialize_page_table(page_table_addr);
Expand Down
6 changes: 4 additions & 2 deletions src/hypervisor_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::device::MmioDevice;
use crate::emulate_extension;
use crate::guest::context::ContextData;
use crate::guest::Guest;
use crate::h_extension::csrs::{
hcounteren, hedeleg, hedeleg::ExceptionKind, henvcfg, hgatp, hideleg, hie, hstateen0, hstatus,
Expand Down Expand Up @@ -211,7 +212,7 @@ fn hart_entry(hart_id: usize, dtb_addr: GuestPhysicalAddress) -> ! {

// set sp to scratch stack top
mv sp, {stack_top}
addi sp, sp, -272 // Size of ContextData = 8 * 34
addi sp, sp, -{HS_CONTEXT_SIZE}

// restore sstatus
ld t0, 32*8(sp)
Expand Down Expand Up @@ -254,11 +255,12 @@ fn hart_entry(hart_id: usize, dtb_addr: GuestPhysicalAddress) -> ! {
ld t6, 31*8(sp)

// swap HS-mode sp for original mode sp.
addi sp, sp, 272
addi sp, sp, {HS_CONTEXT_SIZE}
csrrw sp, sscratch, sp

sret
",
HS_CONTEXT_SIZE = const size_of::<ContextData>(),
in("a0") hart_id,
in("a1") dtb_addr.raw(),
stack_top = in(reg) stack_top.raw(),
Expand Down
16 changes: 6 additions & 10 deletions src/trap/hypervisor_supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod exception;
mod interrupt;

use crate::guest::context::ContextData;
use exception::trap_exception;
use interrupt::trap_interrupt;

Expand All @@ -11,9 +12,6 @@ use core::arch::asm;
use riscv::register::scause::{self, Trap};

/// Switch to original mode stack and save contexts.
///
/// # TODO
/// replace stringify macro to const when `asm_const` is stabled.
#[inline(always)]
#[allow(clippy::inline_always)]
pub unsafe fn hstrap_exit() -> ! {
Expand All @@ -29,7 +27,7 @@ pub unsafe fn hstrap_exit() -> ! {

// set to stack top
mv sp, {stack_top}
addi sp, sp, -272 // Size of ContextData = 8 * 34
addi sp, sp, -{HS_CONTEXT_SIZE}

// restore sstatus
ld t0, 32*8(sp)
Expand Down Expand Up @@ -72,11 +70,12 @@ pub unsafe fn hstrap_exit() -> ! {
ld t6, 31*8(sp)

// swap HS-mode sp for original mode sp.
addi sp, sp, 272
addi sp, sp, {HS_CONTEXT_SIZE}
csrrw sp, sscratch, sp

sret
",
HS_CONTEXT_SIZE = const size_of::<ContextData>(),
stack_top = in(reg) stack_top.raw(),
options(noreturn)
);
Expand All @@ -85,10 +84,6 @@ pub unsafe fn hstrap_exit() -> ! {
/// Trap vector for HS-mode.
/// Switch to hypervisor stack and save contexts.
///
/// # TODO
/// ## `asm_const`
/// replace stringify macro to const when `asm_const` is stabled.
///
/// ## `fn_align`
/// function alignment (feature `fn_align`).
/// See: [https://github.com/rust-lang/rust/issues/82232](https://github.com/rust-lang/rust/issues/82232).
Expand All @@ -106,7 +101,7 @@ pub unsafe extern "C" fn hstrap_vector() -> ! {

// swap original mode sp for HS-mode sp
csrrw sp, sscratch, sp
addi sp, sp, -272 // Size of ContextData = 8 * 34
addi sp, sp, -{HS_CONTEXT_SIZE}

// save registers
sd ra, 1*8(sp)
Expand Down Expand Up @@ -148,6 +143,7 @@ pub unsafe extern "C" fn hstrap_vector() -> ! {
csrr t1, sepc
sd t1, 33*8(sp)
",
HS_CONTEXT_SIZE = const size_of::<ContextData>(),
);
}

Expand Down
16 changes: 11 additions & 5 deletions src/trap/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use interrupt::trap_interrupt;
use core::arch::asm;
use riscv::register::mcause::{self, Trap};

/// Size of context data in M-mode.
const MACHINE_CONTEXT_SIZE: usize = 256;

Choose a reason for hiding this comment

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

⚠️ [clippy] clippy::missing_docs_in_private_items reported by reviewdog 🐶
missing documentation for a constant


/// Epilogue of Machine trap vector
#[inline(always)]
#[allow(clippy::inline_always)]
Expand All @@ -19,7 +22,7 @@ unsafe fn mtrap_exit() -> ! {
1:
auipc sp, %pcrel_hi(_top_m_stack)
addi sp, sp, %pcrel_lo(1b)
addi sp, sp, -256
addi sp, sp, -{M_CONTEXT_SIZE}

ld ra, 1*8(sp)
ld gp, 3*8(sp)
Expand Down Expand Up @@ -53,13 +56,14 @@ unsafe fn mtrap_exit() -> ! {
ld t6, 31*8(sp)

// revert stack pointer top
addi sp, sp, 256
addi sp, sp, {M_CONTEXT_SIZE}

// swap current sp for stored original mode sp
csrrw sp, mscratch, sp

mret
",
M_CONTEXT_SIZE = const MACHINE_CONTEXT_SIZE,
options(noreturn),
);
}
Expand All @@ -76,7 +80,7 @@ unsafe fn mtrap_exit_sbi(error: usize, value: usize) -> ! {
1:
auipc sp, %pcrel_hi(_top_m_stack)
addi sp, sp, %pcrel_lo(1b)
addi sp, sp, -256
addi sp, sp, -{M_CONTEXT_SIZE}

ld ra, 1*8(sp)
ld gp, 3*8(sp)
Expand Down Expand Up @@ -110,13 +114,14 @@ unsafe fn mtrap_exit_sbi(error: usize, value: usize) -> ! {
ld t6, 31*8(sp)

// revert stack pointer to top (0x80800000)
addi sp, sp, 256
addi sp, sp, {M_CONTEXT_SIZE}

// swap current sp for stored original mode sp
csrrw sp, mscratch, sp

mret
",
M_CONTEXT_SIZE = const MACHINE_CONTEXT_SIZE,
error = in(reg) error,
value = in(reg) value,
options(noreturn),
Expand All @@ -132,7 +137,7 @@ pub unsafe extern "C" fn mtrap_vector() -> ! {
fence.i
// swap original mode sp for machine mode sp
csrrw sp, mscratch, sp
addi sp, sp, -256
addi sp, sp, -{M_CONTEXT_SIZE}

sd ra, 1*8(sp)
sd gp, 3*8(sp)
Expand Down Expand Up @@ -165,6 +170,7 @@ pub unsafe extern "C" fn mtrap_vector() -> ! {
sd t5, 30*8(sp)
sd t6, 31*8(sp)
",
M_CONTEXT_SIZE = const MACHINE_CONTEXT_SIZE,
);

mtrap_vector2();
Expand Down
Loading