Skip to content

Commit

Permalink
Adjust xlen for satp and vsatp
Browse files Browse the repository at this point in the history
  • Loading branch information
Weiwei Li committed Dec 8, 2022
1 parent 26958de commit 81639ec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
16 changes: 9 additions & 7 deletions riscv/csrs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,9 @@ henvcfg_csr_t::henvcfg_csr_t(processor_t* const proc, const reg_t addr, const re
}

// implement class base_atp_csr_t and family
base_atp_csr_t::base_atp_csr_t(processor_t* const proc, const reg_t addr):
basic_csr_t(proc, addr, 0) {
base_atp_csr_t::base_atp_csr_t(processor_t* const proc, const reg_t addr, uint8_t prv_index):
basic_csr_t(proc, addr, 0),
prv_index(prv_index) {
}

bool base_atp_csr_t::unlogged_write(const reg_t val) noexcept {
Expand All @@ -1012,7 +1013,7 @@ bool base_atp_csr_t::unlogged_write(const reg_t val) noexcept {
}

bool base_atp_csr_t::satp_valid(reg_t val) const noexcept {
if (proc->get_xlen() == 32) {
if (proc->get_xlen(prv_index & 3, prv_index & 4) == 32) {
switch (get_field(val, SATP32_MODE)) {
case SATP_MODE_SV32: return proc->supports_impl(IMPL_MMU_SV32);
case SATP_MODE_OFF: return true;
Expand All @@ -1032,18 +1033,19 @@ bool base_atp_csr_t::satp_valid(reg_t val) const noexcept {
reg_t base_atp_csr_t::compute_new_satp(reg_t val) const noexcept {
reg_t rv64_ppn_mask = (reg_t(1) << (MAX_PADDR_BITS - PGSHIFT)) - 1;

reg_t mode_mask = proc->get_xlen() == 32 ? SATP32_MODE : SATP64_MODE;
reg_t asid_mask_if_enabled = proc->get_xlen() == 32 ? SATP32_ASID : SATP64_ASID;
reg_t xlen = proc->get_xlen(prv_index & 3, prv_index & 4);
reg_t mode_mask = xlen == 32 ? SATP32_MODE : SATP64_MODE;
reg_t asid_mask_if_enabled = xlen == 32 ? SATP32_ASID : SATP64_ASID;
reg_t asid_mask = proc->supports_impl(IMPL_MMU_ASID) ? asid_mask_if_enabled : 0;
reg_t ppn_mask = proc->get_xlen() == 32 ? SATP32_PPN : SATP64_PPN & rv64_ppn_mask;
reg_t ppn_mask = xlen == 32 ? SATP32_PPN : SATP64_PPN & rv64_ppn_mask;
reg_t new_mask = (satp_valid(val) ? mode_mask : 0) | asid_mask | ppn_mask;
reg_t old_mask = satp_valid(val) ? 0 : mode_mask;

return (new_mask & val) | (old_mask & read());
}

satp_csr_t::satp_csr_t(processor_t* const proc, const reg_t addr):
base_atp_csr_t(proc, addr) {
base_atp_csr_t(proc, addr, PRV_S) {
}

void satp_csr_t::verify_permissions(insn_t insn, bool write) const {
Expand Down
3 changes: 2 additions & 1 deletion riscv/csrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,13 @@ class henvcfg_csr_t final: public masked_csr_t {
// These are three classes in order to handle the [V]TVM bits permission checks
class base_atp_csr_t: public basic_csr_t {
public:
base_atp_csr_t(processor_t* const proc, const reg_t addr);
base_atp_csr_t(processor_t* const proc, const reg_t addr, uint8_t prv_index);
bool satp_valid(reg_t val) const noexcept;
protected:
virtual bool unlogged_write(const reg_t val) noexcept override;
private:
reg_t compute_new_satp(reg_t val) const noexcept;
uint8_t prv_index;
};

class satp_csr_t: public base_atp_csr_t {
Expand Down
2 changes: 1 addition & 1 deletion riscv/processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ void state_t::reset(processor_t* const proc, reg_t max_isa)
csrmap[CSR_VSTVEC] = vstvec = std::make_shared<tvec_csr_t>(proc, CSR_VSTVEC);
csrmap[CSR_STVEC] = stvec = std::make_shared<virtualized_csr_t>(proc, nonvirtual_stvec, vstvec);
auto nonvirtual_satp = std::make_shared<satp_csr_t>(proc, CSR_SATP);
csrmap[CSR_VSATP] = vsatp = std::make_shared<base_atp_csr_t>(proc, CSR_VSATP);
csrmap[CSR_VSATP] = vsatp = std::make_shared<base_atp_csr_t>(proc, CSR_VSATP, PRV_S | 4);
csrmap[CSR_SATP] = satp = std::make_shared<virtualized_satp_csr_t>(proc, nonvirtual_satp, vsatp);
auto nonvirtual_scause = std::make_shared<cause_csr_t>(proc, CSR_SCAUSE, PRV_S);
csrmap[CSR_VSCAUSE] = vscause = std::make_shared<cause_csr_t>(proc, CSR_VSCAUSE, PRV_S | 4);
Expand Down

0 comments on commit 81639ec

Please sign in to comment.