Skip to content

Commit

Permalink
Add mstateen0 CSR
Browse files Browse the repository at this point in the history
  • Loading branch information
ved-rivos committed Jun 22, 2024
1 parent 40ed0c5 commit 2a1b94f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions model/riscv_csr_map.sail
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ mapping clause csr_name_map = 0x00F <-> "vcsr"
mapping clause csr_name_map = 0xC20 <-> "vl"
mapping clause csr_name_map = 0xC21 <-> "vtype"
mapping clause csr_name_map = 0xC22 <-> "vlenb"
/* Smstateen csrs */
mapping clause csr_name_map = 0x30C <-> "mstateen0"
mapping clause csr_name_map = 0x31C <-> "mstateen0h"

val csr_name : csreg -> string
overload to_str = {csr_name}
Expand Down
5 changes: 5 additions & 0 deletions model/riscv_insts_zicsr.sail
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ function readCSR csr : csreg -> xlenbits = {
(0x305, _) => get_mtvec(),
(0x306, _) => zero_extend(mcounteren.bits),
(0x30A, _) => menvcfg.bits[sizeof(xlen) - 1 .. 0],
(0x30C, _) => mstateen0.bits()[sizeof(xlen) - 1 .. 0],
(0x310, 32) => mstatush.bits,
(0x31A, 32) => menvcfg.bits[63 .. 32],
(0x31C, 32) => mstateen0.bits()[63 .. 32],
(0x320, _) => zero_extend(mcountinhibit.bits),

(0x340, _) => mscratch,
Expand Down Expand Up @@ -125,8 +127,11 @@ function writeCSR (csr : csreg, value : xlenbits) -> unit = {
(0x306, _) => { mcounteren = legalize_mcounteren(mcounteren, value); Some(zero_extend(mcounteren.bits)) },
(0x30A, 32) => { menvcfg = legalize_menvcfg(menvcfg, menvcfg.bits[63 .. 32] @ value); Some(menvcfg.bits[31 .. 0]) },
(0x30A, 64) => { menvcfg = legalize_menvcfg(menvcfg, value); Some(menvcfg.bits) },
(0x30C, 32) => { mstateen0 = legalize_mstateen0(mstateen0, mstateen0.bits()[63 .. 32] @ value); Some(mstateen0.bits()[31 .. 0]) },
(0x30C, 64) => { mstateen0 = legalize_mstateen0(mstateen0, value); Some(mstateen0.bits()) },
(0x310, 32) => { Some(mstatush.bits) }, // ignore writes for now
(0x31A, 32) => { menvcfg = legalize_menvcfg(menvcfg, value @ menvcfg.bits[31 .. 0]); Some(menvcfg.bits[63 .. 32]) },
(0x31C, 32) => { mstateen0 = legalize_mstateen0(mstateen0, value @ mstateen0.bits()[31 .. 0]); Some(mstateen0.bits()[63 .. 32]) },
(0x320, _) => { mcountinhibit = legalize_mcountinhibit(mcountinhibit, value); Some(zero_extend(mcountinhibit.bits)) },
(0x340, _) => { mscratch = value; Some(mscratch) },
(0x341, _) => { Some(set_xret_target(Machine, value)) },
Expand Down
5 changes: 4 additions & 1 deletion model/riscv_sys_control.sail
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ function is_CSR_defined (csr : csreg, p : Privilege) -> bool =
0x305 => p == Machine, // mtvec
0x306 => p == Machine & haveUsrMode(), // mcounteren
0x30A => p == Machine & haveUsrMode(), // menvcfg
0x30C => p == Machine & haveSupMode() & haveSmstateen(), // mstateen0
0x310 => p == Machine & (sizeof(xlen) == 32), // mstatush
0x31A => p == Machine & haveUsrMode() & (sizeof(xlen) == 32), // menvcfgh
0x31C => p == Machine & haveSupMode() & (sizeof(xlen) == 32) & haveSmstateen(), // mstateen0h
0x320 => p == Machine, // mcountinhibit
/* machine mode: trap handling */
0x340 => p == Machine, // mscratch
Expand Down Expand Up @@ -117,7 +119,6 @@ function check_Counteren(csr : csreg, p : Privilege) -> bool =
else true
}


/* Seed may only be accessed if we are doing a write, and access has been
* allowed in the current priv mode
*/
Expand Down Expand Up @@ -529,6 +530,8 @@ function init_sys() -> unit = {

menvcfg.bits = zero_extend(0b0);
senvcfg.bits = zero_extend(0b0);
mstateen0->bits() = zero_extend(0b0);

/* initialize vector csrs */
elen = 0b1; /* ELEN=64 as the common case */
vlen = 0b0100; /* VLEN=512 as a default value */
Expand Down
24 changes: 24 additions & 0 deletions model/riscv_sys_regs.sail
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ function haveZalrsc() -> bool = haveAtomics()
/* Zicond extension support */
function haveZicond() -> bool = true

/* Smstateen extension support */
function haveSmstateen() -> bool = true

/* Ssqosid extension support */
function haveSsqosid() -> bool = true

/*
* Illegal values legalized to least privileged mode supported.
* Note: the only valid combinations of supported modes are M, M+U, M+S+U.
Expand Down Expand Up @@ -858,6 +864,24 @@ function is_fiom_active() -> bool = {
User => (menvcfg[FIOM] | senvcfg[FIOM]) == 0b1,
}
}

bitfield Mstateen0 : bits(64) = {
// controls to new state introduced by the following small extensions of Priv-1.14-era:
PRIV114 : 55
}
register mstateen0 : Mstateen0
// If any extension controlled by PRIV114 bit in mstateen0 is present
// then PRIV114 bit is present
function havePriv114Extensions() -> bool = {
haveSsqosid()
}
function legalize_mstateen0(o : Mstateen0, v : bits(64)) -> Mstateen0 = {
let v = Mk_Mstateen0(v);
let o = update_PRIV114(o, if havePriv114Extensions() then v.PRIV114() else 0b0);
// Other extensions are not implemented yet so all other fields are read only zero.
o
}

/* vector csrs */
register vstart : bits(16) /* use the largest possible length of vstart */
register vxsat : bits(1)
Expand Down

0 comments on commit 2a1b94f

Please sign in to comment.