Skip to content

Commit c37f0db

Browse files
committed
add satp mode guard
1 parent e72c501 commit c37f0db

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

config/default.json

+12
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@
184184
},
185185
"Svinval" : {
186186
"supported" : true
187+
},
188+
"Sv32" : {
189+
"supported" : true
190+
},
191+
"Sv39" : {
192+
"supported" : true
193+
},
194+
"Sv48" : {
195+
"supported" : true
196+
},
197+
"Sv57" : {
198+
"supported" : true
187199
}
188200
}
189201
}

model/riscv_extensions.sail

+10
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ function clause hartSupports(Ext_Svnapot) = false // Not supported yet
196196
enum clause extension = Ext_Svpbmt
197197
function clause hartSupports(Ext_Svpbmt) = false // Not supported yet
198198

199+
// Supervisor-Level Address Translation Modes
200+
enum clause extension = Ext_Svbare
201+
enum clause extension = Ext_Sv32
202+
function clause hartSupports(Ext_Sv32) = config extensions.Sv32.supported : bool & (xlen == 32)
203+
enum clause extension = Ext_Sv39
204+
function clause hartSupports(Ext_Sv39) = config extensions.Sv39.supported : bool & (xlen == 64)
205+
enum clause extension = Ext_Sv48
206+
function clause hartSupports(Ext_Sv48) = config extensions.Sv48.supported : bool & (xlen == 64)
207+
enum clause extension = Ext_Sv57
208+
function clause hartSupports(Ext_Sv57) = config extensions.Sv57.supported : bool & (xlen == 64)
199209
// Cycle and Instret Privilege Mode Filtering
200210
enum clause extension = Ext_Smcntrpmf
201211
function clause hartSupports(Ext_Smcntrpmf) = config extensions.Smcntrpmf.supported

model/riscv_sys_regs.sail

+27-4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ function clause write_CSR(0x301, value) = { misa = legalize_misa(misa, value); m
140140
function clause currentlyEnabled(Ext_U) = hartSupports(Ext_U) & misa[U] == 0b1
141141
function clause currentlyEnabled(Ext_S) = hartSupports(Ext_S) & misa[S] == 0b1
142142

143+
function clause currentlyEnabled(Ext_Svbare) = currentlyEnabled(Ext_S)
144+
function clause currentlyEnabled(Ext_Sv32) = currentlyEnabled(Ext_S) & hartSupports(Ext_Sv32)
145+
function clause currentlyEnabled(Ext_Sv39) = currentlyEnabled(Ext_S) & hartSupports(Ext_Sv39)
146+
function clause currentlyEnabled(Ext_Sv48) = currentlyEnabled(Ext_S) & hartSupports(Ext_Sv48)
147+
function clause currentlyEnabled(Ext_Sv57) = currentlyEnabled(Ext_S) & hartSupports(Ext_Sv57)
148+
149+
function virtual_memory_supported() -> bool = {
150+
currentlyEnabled(Ext_Sv32) | currentlyEnabled(Ext_Sv39) | currentlyEnabled(Ext_Sv48) | currentlyEnabled(Ext_Sv57)
151+
}
152+
143153
/*
144154
* Illegal values legalized to least privileged mode supported.
145155
* Note: the only valid combinations of supported modes are M, M+U, M+S+U.
@@ -235,7 +245,7 @@ function legalize_mstatus(o : Mstatus, v : bits(64)) -> Mstatus = {
235245
TW = if currentlyEnabled(Ext_U) then v[TW] else 0b0,
236246
TVM = if currentlyEnabled(Ext_S) then v[TVM] else 0b0,
237247
MXR = if currentlyEnabled(Ext_S) then v[MXR] else 0b0,
238-
SUM = if currentlyEnabled(Ext_S) then v[SUM] else 0b0, // TODO: Should also be disabled if satp.MODE is read-only 0
248+
SUM = if virtual_memory_supported() then v[SUM] else 0b0,
239249
MPRV = if currentlyEnabled(Ext_U) then v[MPRV] else 0b0,
240250
/* We don't have any extension context yet. */
241251
XS = extStatus_to_bits(Off),
@@ -855,13 +865,26 @@ function legalize_satp(
855865
written_value : xlenbits,
856866
) -> xlenbits = {
857867
if xlen == 32 then {
858-
/* all 32-bit satp modes are valid */
859-
written_value
868+
let s = Mk_Satp32(written_value);
869+
match satpMode_of_bits(arch, 0b000 @ s[Mode]) {
870+
None() => prev_value,
871+
Some(Sv_mode) => match Sv_mode {
872+
Bare if currentlyEnabled(Ext_Svbare) => s.bits,
873+
Sv32 if currentlyEnabled(Ext_Sv32) => s.bits,
874+
_ => prev_value,
875+
}
876+
}
860877
} else if xlen == 64 then {
861878
let s = Mk_Satp64(written_value);
862879
match satpMode_of_bits(arch, s[Mode]) {
863880
None() => prev_value,
864-
Some(_) => s.bits
881+
Some(Sv_mode) => match Sv_mode {
882+
Bare if currentlyEnabled(Ext_Svbare) => s.bits,
883+
Sv39 if currentlyEnabled(Ext_Sv39) => s.bits,
884+
Sv48 if currentlyEnabled(Ext_Sv48) => s.bits,
885+
Sv57 if currentlyEnabled(Ext_Sv57) => s.bits,
886+
_ => prev_value,
887+
}
865888
}
866889
} else {
867890
internal_error(__FILE__, __LINE__, "Unsupported xlen" ^ dec_str(xlen))

0 commit comments

Comments
 (0)