Skip to content

Commit f2181f0

Browse files
authored
Merge pull request #1732 from chihminchao/fix-zkr
Fix zkr
2 parents 34601fc + 6dbc8ca commit f2181f0

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

riscv/csrs.cc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ mseccfg_csr_t::mseccfg_csr_t(processor_t* const proc, const reg_t addr):
286286
void mseccfg_csr_t::verify_permissions(insn_t insn, bool write) const {
287287
basic_csr_t::verify_permissions(insn, write);
288288
if (!proc->extension_enabled(EXT_SMEPMP) &&
289-
!proc->extension_enabled(EXT_ZICFILP))
289+
!proc->extension_enabled(EXT_ZICFILP) &&
290+
!proc->extension_enabled(EXT_ZKR))
290291
throw trap_illegal_instruction(insn.bits());
291292
}
292293

@@ -302,6 +303,14 @@ bool mseccfg_csr_t::get_rlb() const noexcept {
302303
return (read() & MSECCFG_RLB);
303304
}
304305

306+
bool mseccfg_csr_t::get_useed() const noexcept {
307+
return (read() & MSECCFG_USEED);
308+
}
309+
310+
bool mseccfg_csr_t::get_sseed() const noexcept {
311+
return (read() & MSECCFG_SSEED);
312+
}
313+
305314
bool mseccfg_csr_t::unlogged_write(const reg_t val) noexcept {
306315
if (proc->n_pmp == 0)
307316
return false;
@@ -321,6 +330,11 @@ bool mseccfg_csr_t::unlogged_write(const reg_t val) noexcept {
321330
new_val |= (val & MSECCFG_MMWP); //MMWP is sticky
322331
new_val |= (val & MSECCFG_MML); //MML is sticky
323332

333+
if (proc->extension_enabled(EXT_ZKR)) {
334+
uint64_t mask = MSECCFG_USEED | MSECCFG_SSEED;
335+
new_val = (new_val & ~mask) | (val & mask);
336+
}
337+
324338
proc->get_mmu()->flush_tlb();
325339

326340
if (proc->extension_enabled(EXT_ZICFILP)) {
@@ -1434,6 +1448,16 @@ void seed_csr_t::verify_permissions(insn_t insn, bool write) const {
14341448
if (!proc->extension_enabled(EXT_ZKR) || !write)
14351449
throw trap_illegal_instruction(insn.bits());
14361450
csr_t::verify_permissions(insn, write);
1451+
1452+
if (state->v) {
1453+
if (state->mseccfg->get_sseed() && write)
1454+
throw trap_virtual_instruction(insn.bits());
1455+
else
1456+
throw trap_illegal_instruction(insn.bits());
1457+
} else if ((state->prv == PRV_U && !state->mseccfg->get_useed()) ||
1458+
(state->prv == PRV_S && !state->mseccfg->get_sseed())) {
1459+
throw trap_illegal_instruction(insn.bits());
1460+
}
14371461
}
14381462

14391463
reg_t seed_csr_t::read() const noexcept {

riscv/csrs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ class mseccfg_csr_t: public basic_csr_t {
150150
bool get_mml() const noexcept;
151151
bool get_mmwp() const noexcept;
152152
bool get_rlb() const noexcept;
153+
bool get_useed() const noexcept;
154+
bool get_sseed() const noexcept;
153155
protected:
154156
virtual bool unlogged_write(const reg_t val) noexcept override;
155157
};

0 commit comments

Comments
 (0)