riscv-h
RISC-V Hypervisor Extension Register Support
A Rust crate providing low-level access to RISC-V hypervisor extension registers. This crate implements the hypervisor Control and Status Registers (CSRs) defined in the RISC-V Hypervisor Extension specification, enabling virtualization support on RISC-V processors.
Features
- No-std compatible: Designed for bare-metal and OS kernel development
- Type-safe register access: Bitfield manipulation with compile-time safety
- Comprehensive CSR coverage: All hypervisor and virtual supervisor registers
- Well-tested: Extensive unit tests for all register implementations
Supported Registers
Hypervisor Control Registers
| Register | Description | CSR Address |
|---|---|---|
hstatus |
Hypervisor status register | 0x600 |
hedeleg |
Hypervisor exception delegation | 0x602 |
hideleg |
Hypervisor interrupt delegation | 0x603 |
hie |
Hypervisor interrupt enable | 0x604 |
hcounteren |
Hypervisor counter enable | 0x606 |
hgatp |
Hypervisor guest address translation and protection | 0x680 |
Virtual Supervisor Registers
| Register | Description | CSR Address |
|---|---|---|
vsstatus |
Virtual supervisor status | 0x200 |
vsie |
Virtual supervisor interrupt enable | 0x204 |
vstvec |
Virtual supervisor trap vector | 0x205 |
vsscratch |
Virtual supervisor scratch | 0x240 |
vsepc |
Virtual supervisor exception PC | 0x241 |
vscause |
Virtual supervisor cause | 0x242 |
vstval |
Virtual supervisor trap value | 0x243 |
vsatp |
Virtual supervisor address translation and protection | 0x280 |
Additional Registers
- Interrupt Management:
hip,hvip,hgeie,hgeip - Time Management:
htimedelta,htimedeltah - Trap Information:
htval,htinst - Virtual Supervisor Interrupts:
vsip
Quick Start
Add this crate to your Cargo.toml:
[]
= "0.1.0"
Basic Usage
use ;
Register Field Access
use hstatus;
// Read register value
let hstatus_val = read;
// Access individual fields
let vsxl = hstatus_val.vsxl; // Virtual supervisor XLEN
let vtw = hstatus_val.vtw; // Trap WFI
let vtsr = hstatus_val.vtsr; // Trap SRET
let vgein = hstatus_val.vgein; // Virtual guest external interrupt number
// Modify register (create new value)
let mut new_hstatus = from_bits;
new_hstatus.set_vtw; // Enable WFI trapping
new_hstatus.set_hu; // Enable hypervisor user mode
unsafe
Exception and Interrupt Delegation
use ;
unsafe
Architecture Support
- RISC-V 64-bit (RV64): Full support for all hypervisor extension registers
- Privilege Levels: HS-mode, VS-mode, VU-mode register access
- Memory Management: Two-stage address translation support
Safety
This crate provides unsafe functions for writing to CSRs, as register modifications can affect system behavior. Users must ensure:
- Proper privilege level (HS-mode) when accessing hypervisor CSRs
- Valid field values according to RISC-V specification
- Correct synchronization when modifying shared state
License
This project is licensed under multiple licenses:
- GNU General Public License v3.0 or later
- Apache License 2.0
- Mulan PSL v2
See the LICENSE files for details.
Contributing
Contributions are welcome! Please:
- Follow the existing code style and conventions
- Add tests for new register implementations
- Update documentation for any API changes
- Ensure all tests pass before submitting
References
- RISC-V Hypervisor Extension Specification
- RISC-V Privileged Architecture Specification
- riscv crate - Base RISC-V register support