forked from rust-embedded/riscv
-
Notifications
You must be signed in to change notification settings - Fork 0
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
CSR: hstatus: add register module #20
Open
clementleger
wants to merge
1
commit into
rivos/main
Choose a base branch
from
dev/cleger/hstatus
base: rivos/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
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
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,144 @@ | ||
//! hstatus register | ||
|
||
#[cfg(target_pointer_width = "64")] | ||
use crate::register::misa; | ||
use bit_field::BitField; | ||
|
||
/// Hypervisor Status Register | ||
#[derive(Clone, Copy, Debug)] | ||
pub struct Hstatus { | ||
bits: usize, | ||
} | ||
|
||
/// Supervisor Previous Virtual Privilege Mode | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub enum SPVP { | ||
Supervisor = 1, | ||
User = 0, | ||
} | ||
|
||
impl Hstatus { | ||
/// Virtual Supervisor Big-endian Enable | ||
#[inline] | ||
pub fn vsbe(&self) -> bool { | ||
self.bits.get_bit(5) | ||
} | ||
|
||
/// Guest Virtual Address | ||
#[inline] | ||
pub fn gva(&self) -> bool { | ||
self.bits.get_bit(6) | ||
} | ||
|
||
/// Supervisor Previous Virtualization mode | ||
#[inline] | ||
pub fn spv(&self) -> bool { | ||
self.bits.get_bit(7) | ||
} | ||
|
||
/// Supervisor Previous Virtual Privilege Mode | ||
#[inline] | ||
pub fn spvp(&self) -> SPVP { | ||
match self.bits.get_bit(8) { | ||
true => SPVP::Supervisor, | ||
false => SPVP::User, | ||
} | ||
} | ||
|
||
/// Hypervisor in U-mode | ||
#[inline] | ||
pub fn hu(&self) -> bool { | ||
self.bits.get_bit(7) | ||
} | ||
|
||
/// Virtual Guest External Interrupt Number | ||
#[inline] | ||
pub fn vgein(&self) -> usize { | ||
self.bits.get_bits(12..17) | ||
} | ||
|
||
/// Virtual Mode Trap Virtual Memory | ||
#[inline] | ||
pub fn vtvm(&self) -> bool { | ||
self.bits.get_bit(20) | ||
} | ||
|
||
/// Virtual Mode Timeout Wait | ||
#[inline] | ||
pub fn vtw(&self) -> bool { | ||
self.bits.get_bit(21) | ||
} | ||
|
||
/// Virtual Mode Trap SRET | ||
#[inline] | ||
pub fn vtsr(&self) -> bool { | ||
self.bits.get_bit(22) | ||
} | ||
|
||
/// VS-Mode XLEN | ||
#[inline] | ||
#[cfg(target_pointer_width = "64")] | ||
pub fn vsxl(&self) -> misa::MXL { | ||
match self.bits.get_bits(32..33) { | ||
1 => misa::MXL::XLEN32, | ||
2 => misa::MXL::XLEN64, | ||
3 => misa::MXL::XLEN128, | ||
_ => unreachable!(), | ||
} | ||
} | ||
} | ||
|
||
read_csr_as!(Hstatus, 0x600); | ||
write_csr!(0x600); | ||
set!(0x600); | ||
clear!(0x600); | ||
|
||
set_clear_csr!( | ||
/// Virtual Supervisor Big-endian Enable | ||
, set_vsbe, clear_vsbe, 1 << 5); | ||
set_clear_csr!( | ||
/// Guest Virtual Address | ||
, set_gva, clear_gva, 1 << 6); | ||
set_clear_csr!( | ||
/// Supervisor Previous Virtualization mode | ||
, set_spv, clear_spv, 1 << 7); | ||
|
||
#[inline] | ||
/// Supervisor Previous Virtual Privilege Mode | ||
pub unsafe fn set_spvp(spvp: SPVP) { | ||
match spvp { | ||
SPVP::Supervisor => _set(1 << 8), | ||
SPVP::User => _clear(1 << 8), | ||
} | ||
} | ||
|
||
set_clear_csr!( | ||
/// Hypervisor in U-mode | ||
, set_hu, clear_hu, 1 << 9); | ||
|
||
#[inline] | ||
/// Virtual Guest External Interrupt Number | ||
pub unsafe fn set_vgein(vgein: usize) { | ||
let mut value = _read(); | ||
value.set_bits(12..17, vgein); | ||
_write(value); | ||
} | ||
|
||
set_clear_csr!( | ||
/// Virtual Mode Trap Virtual Memory | ||
, set_vtvm, clear_vtvm, 1 << 20); | ||
set_clear_csr!( | ||
/// Virtual Mode Timeout Wait | ||
, set_vtw, clear_vtw, 1 << 21); | ||
set_clear_csr!( | ||
/// Virtual Mode Trap SRET | ||
, set_vtsr, clear_vtsr, 1 << 22); | ||
|
||
#[inline] | ||
#[cfg(target_pointer_width = "64")] | ||
/// VS-Mode XLEN | ||
pub unsafe fn set_vsxl(vsxl: misa::MXL) { | ||
let mut value = _read(); | ||
value.set_bits(32..33, vsxl as usize); | ||
_write(value); | ||
} |
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.
I think you need to guard this with:
since the usage of misa is guarded by the above.
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.
Nice catch !