Skip to content

[riscv]: Ground work for supporting hypervisor extensions - #427

Open
vm-kit wants to merge 14 commits into
rust-embedded:masterfrom
vm-kit:hs-mode-introduction
Open

vm-kit wants to merge 14 commits into
rust-embedded:masterfrom
vm-kit:hs-mode-introduction

Conversation

@vm-kit

@vm-kit vm-kit commented Nov 24, 2026

Copy link
Copy Markdown

This pr contains the ground work for supporting hs-mode in the riscv crate.

A step is added to riscv github workflow which builds with the feature hs-mode

hs-mode, m-mode, and s-mode are completely independent of each other.

A few changes have been made to mstatus and mstatush in order to support fields that are used to indicate information if trapped from hs-mode.

Snapshot testing is used for riscv_rt for expected missing implementations of Interrupts and Exceptions. hypervisor lines were added to the snapshot.

A makefile gitignore entry was added as I prefer to run commands and tests via makefiles. less memorization for me.

Note: I am not an expert in any of this stuff. Please double check field values.

@vm-kit
vm-kit requested a review from a team as a code owner November 24, 2026 19:10
@vm-kit vm-kit changed the title Ground work for supporting hypervisor extensions in riscv crate [riscv]: Ground work for supporting hypervisor extensions Nov 24, 2026
@vm-kit

vm-kit commented Nov 24, 2026

Copy link
Copy Markdown
Author

@romancardenas ready for review :)

@romancardenas romancardenas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! I added a few comments.

Comment on lines +15 to +23
pub enum Interrupt {
SupervisorSoft = 1,
VirtSupervisorSoft = 2,
SupervisorTimer = 5,
VirtSupervisorTimer = 6,
SupervisorExternal = 9,
VirtSupervisorExternal = 10,
SupervisorGuestExternal = 12,
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you add the new variants to interrupt::machine.rs and interrupt::supervisor.rs?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@romancardenas I'm not sure I'm following here. When I look at interrupt::supervisor.rs it does not contain m-mode interrupts. also interrupt::supervisor.rs would not be active at the export on the crate level, as s-mode would not be enabled while hs-mode is. I believe m-mode could see hs-mode, so i can add them there.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

added exceptions and interrupts to machine.rs as well.

@vm-kit vm-kit Nov 28, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I started working on this logic on another branch for interrupt::hypervisor.rs and I see why you want them in interrupt::supervisor.rs now. Do you want me to ensure the new exceptions and interrupts are behind the hs-mode flag, so they do not impact any other crate users? Some might not have a catch _ arm when switching on the invariants. causing them to need to add one before they can use the new crate version.

Comment on lines +65 to +81
// User or Virtual User Environment Call
UserVEnvCall = 8,
SupervisorEnvCall = 9,
// Virtual Supervisor Environment Call
GuestSupervisorEnvCall = 10,
InstructionPageFault = 12,
LoadPageFault = 13,
StorePageFault = 15,
SoftwareCheck = 18,
HardwareError = 19,
// 2 stage translation fault on instruction fetch
InstructionGuestFault = 20,
// 2 stage translation fault on memory load
LoadGuestFault = 21,
VirtualInstructionGuest = 22,
// 2 stage translation fault on memory store or atomic op
StoreGuestPageFault = 23,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add these new variants to interrupt::machine.rs and interrupt::supervisor.rs.
Also, preserve the naming for already user-level causes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Same as here. Let me know your thoughts on supervisor receiving the new exceptions. I believe it should be fine either way, but I don't think supervisor => s-mode in this repository can ever actually trap on the hs-mode ones.


/// Disables interrupts globally in the current hart (hypervisor mode).
#[inline]
pub fn disable() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For same functions as in supervisor level, you can just reexport them from interrupt::supervisor.rs

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm not sure if I can yet. I have not worked through the logic, but I think a lot of things depend on whether the trap came from HS, or VS-VU mode. Is it okay if I tackle this later?

where
F: FnOnce() -> R,
{
let sstatus = sstatus::read();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can reuse the interrupt::supervisor::nested function and just add the code required for hypervisor mode.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm not sure if I can yet. I have not worked through the logic, but I think a lot of things depend on whether the trap came from HS, or VS-VU mode. Is it okay if I tackle this later?

Comment thread riscv/src/register/hstatus.rs Outdated
Comment on lines +5 to +8
#[cfg(target_arch = "riscv32")]
use crate::bits::bf_extract;
#[cfg(not(target_arch = "riscv32"))]
use crate::bits::{bf_extract, bf_insert};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[cfg(target_arch = "riscv32")]
use crate::bits::bf_extract;
#[cfg(not(target_arch = "riscv32"))]
use crate::bits::{bf_extract, bf_insert};
use crate::bits::bf_extract;
#[cfg(not(target_arch = "riscv32"))]
use crate::bits::bf_insert;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ahh, makes sense.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i needed a change to this as something wasn't compiling. take a look

Comment thread riscv/src/register/mstatus.rs Resolved
Comment thread riscv/src/register/mstatush.rs Outdated
mask: 0x30,
}

csr_field_enum! {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please, reexport it from mstatus. The same for GVA.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I believe I have done what you needed here. Please take a look.

Comment thread riscv/src/register/mstatush.rs Outdated
read_only_csr_field! {
Mstatush,
/// Machine Previous Virtualization
mpv: 7,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use the macro variant to use the field structure (same as in mstatus)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i used the variant { ..., mpv, MPV: [7:7] }

Comment thread riscv/src/register/mstatush.rs Outdated
read_only_csr_field! {
Mstatush,
/// Guest Virtual Address
gva: 6,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use the macro variant to use the field structure (same as in mstatus)

@vm-kit vm-kit Nov 27, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i used the variant { ..., gva, GVA: [6:6] }

Comment thread riscv/src/interrupt.rs Resolved
vm-kit and others added 10 commits November 25, 2026 21:29
Co-authored-by: Román Cárdenas Rodríguez <rcardenas.rod@gmail.com>
Co-authored-by: Román Cárdenas Rodríguez <rcardenas.rod@gmail.com>
Co-authored-by: Román Cárdenas Rodríguez <rcardenas.rod@gmail.com>
Co-authored-by: Román Cárdenas Rodríguez <rcardenas.rod@gmail.com>
Co-authored-by: Román Cárdenas Rodríguez <rcardenas.rod@gmail.com>
Co-authored-by: Román Cárdenas Rodríguez <rcardenas.rod@gmail.com>
Co-authored-by: Román Cárdenas Rodríguez <rcardenas.rod@gmail.com>
Co-authored-by: Román Cárdenas Rodríguez <rcardenas.rod@gmail.com>
@vm-kit
vm-kit requested a review from romancardenas November 27, 2026 20:29
@vm-kit

vm-kit commented Nov 27, 2026

Copy link
Copy Markdown
Author

@romancardenas I have a few open questions for yourself as well. Thank you for taking a look and going through all of this. I'm very excited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants