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

Hypervisor extension #612

Draft
wants to merge 28 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b2e56bb
Setup for hypervisor extension
defermelowie Jul 9, 2024
5cd7e97
Add hypervisor extension state
defermelowie Jul 10, 2024
51c8120
Add read/write for hypervisor extension CSRs
defermelowie Jul 10, 2024
0e9bf01
Add emulator flag to enable hypervisor extension on boot
defermelowie Oct 21, 2024
500d667
Add privilege level switches through xret/ecall
defermelowie Oct 21, 2024
6e29e2c
Fix pre-commit issues
defermelowie Oct 21, 2024
ddd4c64
Address translation failures provide ExceptionContext
defermelowie Oct 22, 2024
67142be
Hypervisor CSRs should be accessable from HS-mode
defermelowie Oct 22, 2024
5be5427
Implemented banking of supervisor CSRs in virtualized modes
defermelowie Oct 22, 2024
15b707a
Add promotion of illegal instruction exceptions to virtual instructio…
defermelowie Oct 22, 2024
def9f18
Fix vsstatus XLEN mismatch
defermelowie Oct 22, 2024
e2c29f2
Fix missing interrupt types
defermelowie Oct 22, 2024
63bf2d3
Add hypervisor guest address translation & protection register
defermelowie Oct 23, 2024
54f30bb
Add delegation of interrupt to VS-mode
defermelowie Oct 23, 2024
3d408d8
Define hypervisor fence instructions
defermelowie Oct 23, 2024
ad7397e
Update WFI semantics
defermelowie Oct 23, 2024
158efc4
Add parameters of G-stage address translation modes
defermelowie Oct 28, 2024
b014916
Add getters for fields of HGATP
defermelowie Oct 31, 2024
2229f86
Model implicit page walk errors
defermelowie Oct 31, 2024
a54d173
Add initial 'second level address translation' implementation
defermelowie Oct 31, 2024
fb81f80
Use seperate TLB for each address translation stage
defermelowie Oct 31, 2024
353565a
Fix update of A/D bits for VS-level pages
defermelowie Oct 31, 2024
18bcd5e
Fix typecheck error when xlen == 32
defermelowie Oct 31, 2024
a808060
Report transformed instructions upon memory exceptions
defermelowie Oct 31, 2024
3505614
Add hypervisor load & store instructions
defermelowie Oct 31, 2024
feed997
Refactor TODOs and comments
defermelowie Oct 31, 2024
7419383
Refactor G-stage exception converter into PTW_error to exception conv…
defermelowie Oct 31, 2024
5d852c2
Various small fixes after manual inspection of third-party tests
defermelowie Nov 7, 2024
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
1 change: 1 addition & 0 deletions LICENCE
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Copyright (c) 2017-2024
Jan Henrik Weinstock
Jessica Clarke
Jon French
KULeuven, for contributions by Lowie Deferme
Martin Berger
Michael Sammler
Microsoft, for contributions by Robert Norton-Wright and Nathaniel Wesley Filardo
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ SAIL_DEFAULT_INST += riscv_insts_zbkx.sail

SAIL_DEFAULT_INST += riscv_insts_zicond.sail

SAIL_DEFAULT_INST += riscv_insts_hext.sail

SAIL_DEFAULT_INST += riscv_insts_vext_utils.sail
SAIL_DEFAULT_INST += riscv_insts_vext_fp_utils.sail
SAIL_DEFAULT_INST += riscv_insts_vext_vset.sail
Expand Down Expand Up @@ -74,6 +76,9 @@ SAIL_SYS_SRCS = riscv_csr_begin.sail # Start of CSR scattered definitions
SAIL_SYS_SRCS += riscv_vext_control.sail # helpers for the 'V' extension
SAIL_SYS_SRCS += riscv_sys_exceptions.sail # default basic helpers for exception handling
SAIL_SYS_SRCS += riscv_sync_exception.sail # define the exception structure used in the model
SAIL_SYS_SRCS += riscv_zicsr_control.sail # helpers for the Zicsr extension
SAIL_SYS_SRCS += riscv_hext_control.sail # helpers for the hypervisor extension
SAIL_SYS_SRCS += riscv_csr_hext.sail # scattered definition for hypervisor extension CSRs
SAIL_SYS_SRCS += riscv_softfloat_interface.sail riscv_fdext_regs.sail riscv_fdext_control.sail
SAIL_SYS_SRCS += riscv_sys_control.sail # general exception handling

Expand All @@ -97,15 +102,16 @@ SAIL_VM_SRCS += riscv_vmem.sail
PRELUDE = prelude.sail $(SAIL_XLEN) $(SAIL_FLEN) $(SAIL_VLEN) prelude_mem_metadata.sail prelude_mem.sail

SAIL_REGS_SRCS = riscv_reg_type.sail riscv_freg_type.sail riscv_regs.sail riscv_pc_access.sail riscv_sys_regs.sail
SAIL_REGS_SRCS += riscv_hext_regs.sail
SAIL_REGS_SRCS += riscv_pmp_regs.sail riscv_pmp_control.sail
SAIL_REGS_SRCS += riscv_ext_regs.sail $(SAIL_CHECK_SRCS)
SAIL_REGS_SRCS += riscv_vreg_type.sail riscv_vext_regs.sail

SAIL_ARCH_SRCS = $(PRELUDE)
SAIL_ARCH_SRCS += riscv_types_common.sail riscv_types_ext.sail riscv_types.sail
SAIL_ARCH_SRCS += riscv_vmem_types.sail $(SAIL_REGS_SRCS) $(SAIL_SYS_SRCS) riscv_platform.sail
SAIL_ARCH_SRCS += riscv_types_hext.sail riscv_vmem_types.sail $(SAIL_REGS_SRCS) $(SAIL_SYS_SRCS) riscv_platform.sail
SAIL_ARCH_SRCS += riscv_mem.sail $(SAIL_VM_SRCS)
SAIL_ARCH_RVFI_SRCS = $(PRELUDE) rvfi_dii.sail riscv_types_common.sail riscv_types_ext.sail riscv_types.sail riscv_vmem_types.sail $(SAIL_REGS_SRCS) $(SAIL_SYS_SRCS) riscv_platform.sail riscv_mem.sail $(SAIL_VM_SRCS) riscv_types_kext.sail
SAIL_ARCH_RVFI_SRCS = $(PRELUDE) rvfi_dii.sail riscv_types_common.sail riscv_types_ext.sail riscv_types.sail riscv_vmem_types.sail riscv_types_hext.sail $(SAIL_REGS_SRCS) $(SAIL_SYS_SRCS) riscv_platform.sail riscv_mem.sail $(SAIL_VM_SRCS) riscv_types_kext.sail
SAIL_ARCH_SRCS += riscv_types_kext.sail # Shared/common code for the cryptography extension.

SAIL_STEP_SRCS = riscv_step_common.sail riscv_step_ext.sail riscv_decode_ext.sail riscv_fetch.sail riscv_step.sail
Expand Down
5 changes: 5 additions & 0 deletions c_emulator/riscv_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ bool sys_enable_zicboz(unit u)
return rv_enable_zicboz;
}

bool sys_enable_hext(unit u)
{
return rv_enable_hext;
}

uint64_t sys_pmp_count(unit u)
{
return rv_pmp_count;
Expand Down
1 change: 1 addition & 0 deletions c_emulator/riscv_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bool sys_enable_vext(unit);
bool sys_enable_bext(unit);
bool sys_enable_zicbom(unit);
bool sys_enable_zicboz(unit);
bool sys_enable_hext(unit);

uint64_t sys_pmp_count(unit);
uint64_t sys_pmp_grain(unit);
Expand Down
1 change: 1 addition & 0 deletions c_emulator/riscv_platform_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bool rv_enable_vext = true;
bool rv_enable_bext = false;
bool rv_enable_zicbom = false;
bool rv_enable_zicboz = false;
bool rv_enable_hext = false;

bool rv_enable_dirty_update = false;
bool rv_enable_misaligned = false;
Expand Down
1 change: 1 addition & 0 deletions c_emulator/riscv_platform_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern bool rv_enable_vext;
extern bool rv_enable_bext;
extern bool rv_enable_zicbom;
extern bool rv_enable_zicboz;
extern bool rv_enable_hext;
extern bool rv_enable_writable_misa;
extern bool rv_enable_dirty_update;
extern bool rv_enable_misaligned;
Expand Down
6 changes: 6 additions & 0 deletions c_emulator/riscv_sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ static struct option options[] = {
{"pmp-count", required_argument, 0, OPT_PMP_COUNT },
{"pmp-grain", required_argument, 0, OPT_PMP_GRAIN },
{"enable-next", no_argument, 0, 'N' },
{"enable-hext", no_argument, 0, 'H' },
{"ram-size", required_argument, 0, 'z' },
{"disable-compressed", no_argument, 0, 'C' },
{"disable-writable-misa", no_argument, 0, 'I' },
Expand Down Expand Up @@ -272,6 +273,7 @@ static int process_args(int argc, char **argv)
c = getopt_long(argc, argv,
"a"
"B"
"H"
"d"
"m"
"P"
Expand Down Expand Up @@ -310,6 +312,10 @@ static int process_args(int argc, char **argv)
fprintf(stderr, "enabling B extension.\n");
rv_enable_bext = true;
break;
case 'H':
fprintf(stderr, "enabling hypervisor extension.\n");
rv_enable_hext = true;
break;
case 'd':
fprintf(stderr, "enabling dirty update.\n");
rv_enable_dirty_update = true;
Expand Down
11 changes: 11 additions & 0 deletions model/prelude.sail
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ overload reverse = {reverse_bits_in_byte}
overload operator / = {quot_positive_round_zero, quot_round_zero}
overload operator * = {mult_atom, mult_int}

/* helpers for bitvector option type */

val some_or : forall 'n, 'n >= 0 . (option(bits('n)), bits('n)) -> bits('n)
function some_or(o, d) = match o {
Some(v) => v,
None() => d,
}

val some_or_zero : forall 'n, 'n >= 0. (implicit('n), option(bits('n))) -> bits('n)
function some_or_zero(n, o) = some_or(o, zeros(n))

/* helper for vector extension
* 1. EEW between 8 and 64
* 2. EMUL in vmv<nr>r.v instructions between 1 and 8
Expand Down
49 changes: 49 additions & 0 deletions model/riscv_csr_hext.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*=======================================================================================*/
/* This Sail RISC-V architecture model, comprising all files and */
/* directories except where otherwise noted is subject the BSD */
/* two-clause license in the LICENSE file. */
/* */
/* SPDX-License-Identifier: BSD-2-Clause */
/*=======================================================================================*/
/* Mapping of hypervisor extension CSR addresses to their names. */

/* extra machine registers */
mapping clause csr_name_map = 0x34B <-> "mtval2"
mapping clause csr_name_map = 0x34A <-> "mtinst"

/* hypervisor trap setup */
mapping clause csr_name_map = 0x600 <-> "hstatus"
mapping clause csr_name_map = 0x602 <-> "hedeleg"
mapping clause csr_name_map = 0x603 <-> "hideleg"
mapping clause csr_name_map = 0x604 <-> "hie"
mapping clause csr_name_map = 0x606 <-> "hcounteren"
mapping clause csr_name_map = 0x607 <-> "hgeie"

/* hypervisor counter/timer virtualization */
mapping clause csr_name_map = 0x605 <-> "htimedelta"
mapping clause csr_name_map = 0x615 <-> "htimedeltah"

/* hypervisor config */
mapping clause csr_name_map = 0x60A <-> "henvcfg"
mapping clause csr_name_map = 0x61A <-> "henvcfgh"

/* hypervisor trap handling */
mapping clause csr_name_map = 0x643 <-> "htval"
mapping clause csr_name_map = 0x644 <-> "hip"
mapping clause csr_name_map = 0x645 <-> "hvip"
mapping clause csr_name_map = 0x64A <-> "htinst"
mapping clause csr_name_map = 0xE12 <-> "hgeip"

/* hypervisor protection and translation */
mapping clause csr_name_map = 0x680 <-> "hgatp"

/* virtual supervisor registers */
mapping clause csr_name_map = 0x200 <-> "vsstatus"
mapping clause csr_name_map = 0x204 <-> "vsie"
mapping clause csr_name_map = 0x205 <-> "vstvec"
mapping clause csr_name_map = 0x240 <-> "vsscratch"
mapping clause csr_name_map = 0x241 <-> "vsepc"
mapping clause csr_name_map = 0x242 <-> "vscause"
mapping clause csr_name_map = 0x243 <-> "vstval"
mapping clause csr_name_map = 0x244 <-> "vsip"
mapping clause csr_name_map = 0x280 <-> "vsatp"
10 changes: 5 additions & 5 deletions model/riscv_fetch.sail
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ function fetch() -> FetchResult =
Ext_FetchAddr_Error(e) => F_Ext_Error(e),
Ext_FetchAddr_OK(use_pc) => {
if (use_pc[0] != bitzero | (use_pc[1] != bitzero & not(extensionEnabled(Ext_Zca))))
then F_Error(E_Fetch_Addr_Align(), PC)
then F_Error(E_Fetch_Addr_Align(), mem_exception_context(PC, cur_virtualization == V1))
else match translateAddr(use_pc, Execute()) {
TR_Failure(e, _) => F_Error(e, PC),
TR_Failure(e, c, _) => F_Error(e, c),
TR_Address(ppclo, _) => {
/* split instruction fetch into 16-bit granules to handle RVC, as
* well as to generate precise fault addresses in any fetch
* exceptions.
*/
match mem_read(Execute(), ppclo, 2, false, false, false) {
MemException(e) => F_Error(e, PC),
MemException(e) => F_Error(e, mem_exception_context(PC, cur_virtualization == V1)),
MemValue(ilo) => {
if isRVC(ilo)
then F_RVC(ilo)
Expand All @@ -40,10 +40,10 @@ function fetch() -> FetchResult =
Ext_FetchAddr_Error(e) => F_Ext_Error(e),
Ext_FetchAddr_OK(use_pc_hi) => {
match translateAddr(use_pc_hi, Execute()) {
TR_Failure(e, _) => F_Error(e, PC_hi),
TR_Failure(e, c, _) => F_Error(e, c),
TR_Address(ppchi, _) => {
match mem_read(Execute(), ppchi, 2, false, false, false) {
MemException(e) => F_Error(e, PC_hi),
MemException(e) => F_Error(e, mem_exception_context(PC_hi, cur_virtualization == V1)),
MemValue(ihi) => F_Base(append(ihi, ilo))
}
}
Expand Down
6 changes: 3 additions & 3 deletions model/riscv_fetch_rvfi.sail
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ function fetch() -> FetchResult = {
Ext_FetchAddr_OK(use_pc) => {
/* then check PC alignment */
if (use_pc[0] != bitzero | (use_pc[1] != bitzero & not(extensionEnabled(Ext_Zca))))
then F_Error(E_Fetch_Addr_Align(), PC)
then F_Error(E_Fetch_Addr_Align(), mem_exception_context(PC, cur_virtualization == V1))
else match translateAddr(use_pc, Execute()) {
TR_Failure(e, _) => F_Error(e, PC),
TR_Failure(e, c, _) => F_Error(e, c),
TR_Address(_, _) => {
let i = rvfi_instruction[rvfi_insn];
rvfi_inst_data[rvfi_insn] = zero_extend(i);
Expand All @@ -33,7 +33,7 @@ function fetch() -> FetchResult = {
Ext_FetchAddr_Error(e) => F_Ext_Error(e),
Ext_FetchAddr_OK(use_pc_hi) =>
match translateAddr(use_pc_hi, Execute()) {
TR_Failure(e, _) => F_Error(e, PC),
TR_Failure(e, c, _) => F_Error(e, c),
TR_Address(_, _) => F_Base(i)
}
}
Expand Down
Loading