-
Notifications
You must be signed in to change notification settings - Fork 166
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
Added support of Smepmp #601
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,15 @@ bitfield Pmpcfg_ent : bits(8) = { | |
register pmpcfg_n : vector(64, Pmpcfg_ent) | ||
register pmpaddr_n : vector(64, xlenbits) | ||
|
||
bitfield Mseccfg_ent : xlenbits = { | ||
RLB : 2, /* Rule Locking Bypass */ | ||
MMWP : 1, /* Machine Mode Whitelist Policy */ | ||
MML : 0 /* Machine Mode Lockdown */ | ||
} | ||
|
||
register mseccfg : Mseccfg_ent | ||
register mseccfgh : bits(32) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normally we don't treat this as a separate register, we just make We have an implementation of |
||
|
||
/* Packing and unpacking pmpcfg regs for xlen-width accesses */ | ||
|
||
function pmpReadCfgReg(n : range(0, 15)) -> xlenbits = { | ||
|
@@ -89,32 +98,48 @@ function pmpReadAddrReg(n : range(0, 63)) -> xlenbits = { | |
} | ||
|
||
/* Helpers to handle locked entries */ | ||
function pmpLocked(cfg: Pmpcfg_ent) -> bool = | ||
cfg[L] == 0b1 | ||
function pmpLockBit(cfg: Pmpcfg_ent) -> bool = cfg[L] == 0b1 | ||
|
||
function pmpTORLocked(cfg: Pmpcfg_ent) -> bool = | ||
(cfg[L] == 0b1) & (pmpAddrMatchType_of_bits(cfg[A]) == TOR) | ||
function pmpLocked(cfg: Pmpcfg_ent) -> bool = (cfg[L] == 0b1) & (mseccfg[RLB] == 0b0) | ||
|
||
function pmpWriteCfg(n: range(0, 63), cfg: Pmpcfg_ent, v: bits(8)) -> Pmpcfg_ent = | ||
if pmpLocked(cfg) then cfg | ||
else { | ||
// Bits 5 and 6 are zero. | ||
let cfg = Mk_Pmpcfg_ent(v & 0x9f); | ||
function pmpTORLocked(cfg: Pmpcfg_ent) -> bool = pmpLocked(cfg) & (pmpAddrMatchType_of_bits(cfg[A]) == TOR) | ||
|
||
function pmpWriteCfg(n: range(0, 63), cfg: Pmpcfg_ent, v: bits(8)) -> Pmpcfg_ent = { | ||
/* If locked then configuration is unchanged */ | ||
if pmpLocked(cfg) | ||
then cfg | ||
/* To prevent adding a rule with execution privileges if MML is enabled unless RLB is set */ | ||
else { | ||
/* Constructing legal Pmpcfg_ent by making bit 5 and 6 zero */ | ||
let legal_v : Pmpcfg_ent = Mk_Pmpcfg_ent(v & 0x9f); | ||
// "The R, W, and X fields form a collective WARL field for which the combinations with R=0 and W=1 are reserved." | ||
// In this implementation if R=0 and W=1 then R, W and X are all set to 0. | ||
// This is the least risky option from a security perspective. | ||
let cfg = if cfg[W] == 0b1 & cfg[R] == 0b0 then [cfg with X = 0b0, W = 0b0, R = 0b0] else cfg; | ||
let legal_v : Pmpcfg_ent = match (mseccfg[MML], legal_v[R], legal_v[W]) { | ||
(0b0, 0b0, 0b1) => [legal_v with W = 0b0], | ||
(_, _, _) => legal_v | ||
}; | ||
|
||
let legal_v : Pmpcfg_ent = if (mseccfg[MML] == 0b1 & mseccfg[RLB] == 0b0 & legal_v[L] == 0b1) | ||
then { | ||
match (legal_v[R], legal_v[W], legal_v[X]) { | ||
(0b0, 0b0, 0b1) => cfg, | ||
(0b0, 0b1, 0b0) => cfg, | ||
(0b0, 0b1, 0b1) => cfg, | ||
(0b1, 0b0, 0b1) => cfg, | ||
(_, _, _) => legal_v | ||
} | ||
} else legal_v; | ||
|
||
// "When G >= 1, the NA4 mode is not selectable." | ||
// In this implementation we set it to OFF if NA4 is selected. | ||
// This is the least risky option from a security perspective. | ||
let cfg = if sys_pmp_grain() >= 1 & pmpAddrMatchType_of_bits(cfg[A]) == NA4 | ||
then [cfg with A = pmpAddrMatchType_to_bits(OFF)] | ||
else cfg; | ||
|
||
cfg | ||
let legal_v = if (sys_pmp_grain() >= 1 & pmpAddrMatchType_of_bits(legal_v[A]) == NA4) | ||
then [legal_v with A = pmpAddrMatchType_to_bits(OFF)] | ||
else legal_v; | ||
legal_v | ||
} | ||
} | ||
|
||
function pmpWriteCfgReg(n : range(0, 15), v : xlenbits) -> unit = { | ||
if xlen == 32 | ||
|
@@ -146,3 +171,27 @@ function pmpWriteAddrReg(n : range(0, 63), v : xlenbits) -> unit = { | |
v, | ||
); | ||
} | ||
|
||
function checkPmpNcfgL(n : range(0, 63)) -> bool = { | ||
if n == 0 | ||
then pmpcfg_n[0][L] == 0b1 | ||
else checkPmpNcfgL(n - 1) | (pmpcfg_n[n][L] == 0b1) | ||
} | ||
|
||
function mseccfgWrite(reg: Mseccfg_ent, v: xlenbits) -> Mseccfg_ent = { | ||
let legal_v : Mseccfg_ent = Mk_Mseccfg_ent(zero_extend(v[2 .. 0])); | ||
let reg : Mseccfg_ent = match (reg[RLB], legal_v[RLB]) { /* to set RLB, need to check PMPCFG_L */ | ||
(0b0, 0b1) => | ||
if (checkPmpNcfgL(63)) | ||
then reg | ||
else update_RLB(reg, legal_v[RLB]), | ||
(_, _) => update_RLB(reg, legal_v[RLB]) | ||
}; | ||
let reg : Mseccfg_ent = match (reg[MML], reg[MMWP]) { /* Implements stickiness of MML bit, if once set remains set */ | ||
(0b0, 0b0) => [reg with MMWP = legal_v[MMWP], MML = legal_v[MML]], | ||
(0b0, 0b1) => [reg with MML = legal_v[MML]], | ||
(0b1, 0b0) => [reg with MMWP = legal_v[MMWP]], | ||
(0b1, 0b1) => reg | ||
}; | ||
reg | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,11 @@ val sys_pmp_grain = pure "sys_pmp_grain" : unit -> range(0, 63) | |
/* Which HPM counters are supported (as a bit mask). Bits [2 .. 0] are ignored. */ | ||
val sys_writable_hpm_counters = pure "sys_writable_hpm_counters" : unit -> bits(32) | ||
|
||
/* Enable Smepmp */ | ||
val sys_enable_smepmp = pure "sys_enable_smepmp" : unit -> bool | ||
/* TODO: for extension smmpm and zkr to enable */ | ||
function sys_has_mseccfg() -> bool = sys_enable_smepmp() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can implement
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean we need to add an extra command line parameter to act as an extra switch to control the presence of mseccfg to make sure that even if we don't implement extensions that use this register, we can still implement this register, right ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah exactly. I don't think we need to actually bother hooking it up to a CLI arguments at the moment - we can wait until Alasdair's new config system exists and then it will be a whole lot less tedious. But we can just have a function that returns |
||
|
||
/* whether misa.v was enabled at boot */ | ||
val sys_enable_vext = pure "sys_enable_vext" : unit -> bool | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
//
style comments are nicer IMO. I don't see why we would change them back?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the part that was changed by @HamzaKh01, I didn't check this as I was too focused on the code implementation part, we can change it back if needed, but there are many one line comments like
/* ... */
in Sail, we might need to declare it better in the codestyle after some discussion?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah there is an experimental auto-formatter in the Sail compiler but I don't know what state it got to and I'd guess it doesn't change this sort of thing.