Skip to content

Commit a1d52ef

Browse files
committed
add satp mode guard
1 parent 3dd1e59 commit a1d52ef

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

config/default.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@
7676
},
7777
"Sstc" : {
7878
"supported" : false
79+
},
80+
"Sv32" : {
81+
"supported" : true
82+
},
83+
"Sv39" : {
84+
"supported" : true
85+
},
86+
"Sv48" : {
87+
"supported" : true
88+
},
89+
"Sv57" : {
90+
"supported" : true
7991
}
8092
}
8193
}

model/riscv_extensions.sail

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ function clause hartSupports(Ext_Svinval) = config extensions.Svinval.supported
150150
enum clause extension = Ext_Svnapot
151151
// Page-Based Memory Types
152152
enum clause extension = Ext_Svpbmt
153-
153+
// Supervisor-Level Address Translation Modes
154+
enum clause extension = Ext_Svbare
155+
enum clause extension = Ext_Sv32
156+
function clause hartSupports(Ext_Sv32) = config extensions.Sv32.supported
157+
enum clause extension = Ext_Sv39
158+
function clause hartSupports(Ext_Sv39) = config extensions.Sv39.supported
159+
enum clause extension = Ext_Sv48
160+
function clause hartSupports(Ext_Sv48) = config extensions.Sv48.supported
161+
enum clause extension = Ext_Sv57
162+
function clause hartSupports(Ext_Sv57) = config extensions.Sv57.supported
154163
// Cycle and Instret Privilege Mode Filtering
155164
enum clause extension = Ext_Smcntrpmf

model/riscv_sys_regs.sail

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ function sys_enable_supervisor() -> bool = true
138138
function clause currentlyEnabled(Ext_U) = misa[U] == 0b1
139139
function clause currentlyEnabled(Ext_S) = misa[S] == 0b1
140140

141+
function clause currentlyEnabled(Ext_Svbare) = currentlyEnabled(Ext_S)
142+
function clause currentlyEnabled(Ext_Sv32) = currentlyEnabled(Ext_S) & (xlen == 32) & hartSupports(Ext_Sv32)
143+
function clause currentlyEnabled(Ext_Sv39) = currentlyEnabled(Ext_S) & (xlen == 64) & hartSupports(Ext_Sv39)
144+
function clause currentlyEnabled(Ext_Sv48) = currentlyEnabled(Ext_S) & (xlen == 64) & hartSupports(Ext_Sv48)
145+
function clause currentlyEnabled(Ext_Sv57) = currentlyEnabled(Ext_S) & (xlen == 64) & hartSupports(Ext_Sv57)
146+
147+
function virtual_memory_supported() -> bool = {
148+
currentlyEnabled(Ext_Sv32) | currentlyEnabled(Ext_Sv39) | currentlyEnabled(Ext_Sv48) | currentlyEnabled(Ext_Sv57)
149+
}
150+
141151
/*
142152
* Illegal values legalized to least privileged mode supported.
143153
* Note: the only valid combinations of supported modes are M, M+U, M+S+U.
@@ -233,7 +243,7 @@ function legalize_mstatus(o : Mstatus, v : bits(64)) -> Mstatus = {
233243
TW = if currentlyEnabled(Ext_U) then v[TW] else 0b0,
234244
TVM = if currentlyEnabled(Ext_S) then v[TVM] else 0b0,
235245
MXR = if currentlyEnabled(Ext_S) then v[MXR] else 0b0,
236-
SUM = if currentlyEnabled(Ext_S) then v[SUM] else 0b0, // TODO: Should also be disabled if satp.MODE is read-only 0
246+
SUM = if virtual_memory_supported() then v[SUM] else 0b0,
237247
MPRV = if currentlyEnabled(Ext_U) then v[MPRV] else 0b0,
238248
/* We don't have any extension context yet. */
239249
XS = extStatus_to_bits(Off),
@@ -876,13 +886,26 @@ function legalize_satp(
876886
written_value : xlenbits,
877887
) -> xlenbits = {
878888
if xlen == 32 then {
879-
/* all 32-bit satp modes are valid */
880-
written_value
889+
let s = Mk_Satp32(written_value);
890+
match satpMode_of_bits(arch, 0b000 @ s[Mode]) {
891+
None() => prev_value,
892+
Some(Sv_mode) => match Sv_mode {
893+
Bare if currentlyEnabled(Ext_Svbare) => s.bits,
894+
Sv32 if currentlyEnabled(Ext_Sv32) => s.bits,
895+
_ => prev_value,
896+
}
897+
}
881898
} else if xlen == 64 then {
882899
let s = Mk_Satp64(written_value);
883900
match satpMode_of_bits(arch, s[Mode]) {
884901
None() => prev_value,
885-
Some(_) => s.bits
902+
Some(Sv_mode) => match Sv_mode {
903+
Bare if currentlyEnabled(Ext_Svbare) => s.bits,
904+
Sv39 if currentlyEnabled(Ext_Sv39) => s.bits,
905+
Sv48 if currentlyEnabled(Ext_Sv48) => s.bits,
906+
Sv57 if currentlyEnabled(Ext_Sv57) => s.bits,
907+
_ => prev_value,
908+
}
886909
}
887910
} else {
888911
internal_error(__FILE__, __LINE__, "Unsupported xlen" ^ dec_str(xlen))

0 commit comments

Comments
 (0)