Skip to content

Commit 03e322f

Browse files
committed
add satp mode guard
1 parent 8ff2a70 commit 03e322f

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
@@ -195,6 +195,18 @@
195195
},
196196
"Svinval": {
197197
"supported": true
198+
},
199+
"Sv32": {
200+
"supported": true
201+
},
202+
"Sv39": {
203+
"supported": true
204+
},
205+
"Sv48": {
206+
"supported": true
207+
},
208+
"Sv57": {
209+
"supported": true
198210
}
199211
}
200212
}

model/riscv_extensions.sail

+10
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ function clause hartSupports(Ext_Svnapot) = false // Not supported yet
202202
enum clause extension = Ext_Svpbmt
203203
function clause hartSupports(Ext_Svpbmt) = false // Not supported yet
204204

205+
// Supervisor-Level Address Translation Modes
206+
enum clause extension = Ext_Svbare
207+
enum clause extension = Ext_Sv32
208+
function clause hartSupports(Ext_Sv32) = config extensions.Sv32.supported : bool & (xlen == 32)
209+
enum clause extension = Ext_Sv39
210+
function clause hartSupports(Ext_Sv39) = config extensions.Sv39.supported : bool & (xlen == 64)
211+
enum clause extension = Ext_Sv48
212+
function clause hartSupports(Ext_Sv48) = config extensions.Sv48.supported : bool & (xlen == 64)
213+
enum clause extension = Ext_Sv57
214+
function clause hartSupports(Ext_Sv57) = config extensions.Sv57.supported : bool & (xlen == 64)
205215
// Cycle and Instret Privilege Mode Filtering
206216
enum clause extension = Ext_Smcntrpmf
207217
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) = hartSupports(Ext_Sv32) & currentlyEnabled(Ext_S)
145+
function clause currentlyEnabled(Ext_Sv39) = hartSupports(Ext_Sv39) & currentlyEnabled(Ext_S)
146+
function clause currentlyEnabled(Ext_Sv48) = hartSupports(Ext_Sv48) & currentlyEnabled(Ext_S)
147+
function clause currentlyEnabled(Ext_Sv57) = hartSupports(Ext_Sv57) & currentlyEnabled(Ext_S)
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),
@@ -851,13 +861,26 @@ function legalize_satp(
851861
written_value : xlenbits,
852862
) -> xlenbits = {
853863
if xlen == 32 then {
854-
/* all 32-bit satp modes are valid */
855-
written_value
864+
let s = Mk_Satp32(written_value);
865+
match satpMode_of_bits(arch, 0b000 @ s[Mode]) {
866+
None() => prev_value,
867+
Some(Sv_mode) => match Sv_mode {
868+
Bare if currentlyEnabled(Ext_Svbare) => s.bits,
869+
Sv32 if currentlyEnabled(Ext_Sv32) => s.bits,
870+
_ => prev_value,
871+
}
872+
}
856873
} else if xlen == 64 then {
857874
let s = Mk_Satp64(written_value);
858875
match satpMode_of_bits(arch, s[Mode]) {
859876
None() => prev_value,
860-
Some(_) => s.bits
877+
Some(Sv_mode) => match Sv_mode {
878+
Bare if currentlyEnabled(Ext_Svbare) => s.bits,
879+
Sv39 if currentlyEnabled(Ext_Sv39) => s.bits,
880+
Sv48 if currentlyEnabled(Ext_Sv48) => s.bits,
881+
Sv57 if currentlyEnabled(Ext_Sv57) => s.bits,
882+
_ => prev_value,
883+
}
861884
}
862885
} else {
863886
internal_error(__FILE__, __LINE__, "Unsupported xlen" ^ dec_str(xlen))

0 commit comments

Comments
 (0)