932 lines
40 KiB
C++
932 lines
40 KiB
C++
/*******************************************************************************
|
|
* Copyright (C) 2019 - 2023 MINRES Technologies GmbH
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
* and/or other materials provided with the distribution.
|
|
*
|
|
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
* Contributors:
|
|
* eyck@minres.com - initial implementation
|
|
******************************************************************************/
|
|
|
|
#ifndef _RISCV_HART_M_P_H
|
|
#define _RISCV_HART_M_P_H
|
|
|
|
#include "iss/instrumentation_if.h"
|
|
#include "iss/vm_if.h"
|
|
#include "iss/vm_types.h"
|
|
#include "riscv_hart_common.h"
|
|
#include "util/logging.h"
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <elfio/elf_types.hpp>
|
|
#include <limits>
|
|
#include <stdexcept>
|
|
#ifndef FMT_HEADER_ONLY
|
|
#define FMT_HEADER_ONLY
|
|
#endif
|
|
#include <array>
|
|
#include <elfio/elfio.hpp>
|
|
#include <fmt/format.h>
|
|
#include <functional>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <type_traits>
|
|
#include <unordered_map>
|
|
#include <util/bit_field.h>
|
|
|
|
namespace iss {
|
|
namespace arch {
|
|
|
|
template <typename BASE, features_e FEAT = FEAT_NONE, typename LOGCAT = logging::disass>
|
|
class riscv_hart_m_p : public riscv_hart_common<BASE> {
|
|
public:
|
|
using core = BASE;
|
|
using base = riscv_hart_common<BASE>;
|
|
using this_class = riscv_hart_m_p<BASE, FEAT, LOGCAT>;
|
|
using phys_addr_t = typename core::phys_addr_t;
|
|
using reg_t = typename core::reg_t;
|
|
using addr_t = typename core::addr_t;
|
|
|
|
// primary template
|
|
template <class T, class Enable = void> struct hart_state {};
|
|
// specialization 32bit
|
|
template <typename T> class hart_state<T, typename std::enable_if<std::is_same<T, uint32_t>::value>::type> {
|
|
public:
|
|
BEGIN_BF_DECL(mstatus_t, T);
|
|
// SD bit is read-only and is set when either the FS or XS bits encode a Dirty state (i.e., SD=((FS==11) OR
|
|
// XS==11)))
|
|
BF_FIELD(SD, 31, 1);
|
|
// Trap SRET
|
|
BF_FIELD(TSR, 22, 1);
|
|
// Timeout Wait
|
|
BF_FIELD(TW, 21, 1);
|
|
// Trap Virtual Memory
|
|
BF_FIELD(TVM, 20, 1);
|
|
// Make eXecutable Readable
|
|
BF_FIELD(MXR, 19, 1);
|
|
// permit Supervisor User Memory access
|
|
BF_FIELD(SUM, 18, 1);
|
|
// Modify PRiVilege
|
|
BF_FIELD(MPRV, 17, 1);
|
|
// status of additional user-mode extensions and associated state, All off/None dirty or clean, some on/None
|
|
// dirty, some clean/Some dirty
|
|
BF_FIELD(XS, 15, 2);
|
|
// floating-point unit status Off/Initial/Clean/Dirty
|
|
BF_FIELD(FS, 13, 2);
|
|
// machine previous privilege
|
|
BF_FIELD(MPP, 11, 2);
|
|
// supervisor previous privilege
|
|
BF_FIELD(SPP, 8, 1);
|
|
// previous machine interrupt-enable
|
|
BF_FIELD(MPIE, 7, 1);
|
|
// previous supervisor interrupt-enable
|
|
BF_FIELD(SPIE, 5, 1);
|
|
// previous user interrupt-enable
|
|
BF_FIELD(UPIE, 4, 1);
|
|
// machine interrupt-enable
|
|
BF_FIELD(MIE, 3, 1);
|
|
// supervisor interrupt-enable
|
|
BF_FIELD(SIE, 1, 1);
|
|
// user interrupt-enable
|
|
BF_FIELD(UIE, 0, 1);
|
|
END_BF_DECL();
|
|
|
|
mstatus_t mstatus;
|
|
|
|
static const reg_t mstatus_reset_val = 0x1800;
|
|
|
|
void write_mstatus(T val) {
|
|
auto mask = get_mask() & 0xff; // MPP is hardcode as 0x3
|
|
auto new_val = (mstatus.backing.val & ~mask) | (val & mask);
|
|
mstatus = new_val;
|
|
}
|
|
|
|
static constexpr uint32_t get_mask() {
|
|
// return 0x807ff988UL; // 0b1000 0000 0111 1111 1111 1000 1000 1000 // only machine mode is supported
|
|
// +-SD
|
|
// | +-TSR
|
|
// | |+-TW
|
|
// | ||+-TVM
|
|
// | |||+-MXR
|
|
// | ||||+-SUM
|
|
// | |||||+-MPRV
|
|
// | |||||| +-XS
|
|
// | |||||| | +-FS
|
|
// | |||||| | | +-MPP
|
|
// | |||||| | | | +-SPP
|
|
// | |||||| | | | |+-MPIE
|
|
// | ||||||/|/|/| || +-MIE
|
|
return 0b00000000000000000001100010001000;
|
|
}
|
|
};
|
|
|
|
// specialization 64bit
|
|
template <typename T> class hart_state<T, typename std::enable_if<std::is_same<T, uint64_t>::value>::type> {
|
|
public:
|
|
BEGIN_BF_DECL(mstatus_t, T);
|
|
// SD bit is read-only and is set when either the FS or XS bits encode a Dirty state (i.e., SD=((FS==11) OR
|
|
// XS==11)))
|
|
BF_FIELD(SD, 63, 1);
|
|
// value of XLEN for S-mode
|
|
BF_FIELD(SXL, 34, 2);
|
|
// value of XLEN for U-mode
|
|
BF_FIELD(UXL, 32, 2);
|
|
// Trap SRET
|
|
BF_FIELD(TSR, 22, 1);
|
|
// Timeout Wait
|
|
BF_FIELD(TW, 21, 1);
|
|
// Trap Virtual Memory
|
|
BF_FIELD(TVM, 20, 1);
|
|
// Make eXecutable Readable
|
|
BF_FIELD(MXR, 19, 1);
|
|
// permit Supervisor User Memory access
|
|
BF_FIELD(SUM, 18, 1);
|
|
// Modify PRiVilege
|
|
BF_FIELD(MPRV, 17, 1);
|
|
// status of additional user-mode extensions and associated state, All off/None dirty or clean, some on/None
|
|
// dirty, some clean/Some dirty
|
|
BF_FIELD(XS, 15, 2);
|
|
// floating-point unit status Off/Initial/Clean/Dirty
|
|
BF_FIELD(FS, 13, 2);
|
|
// machine previous privilege
|
|
BF_FIELD(MPP, 11, 2);
|
|
// supervisor previous privilege
|
|
BF_FIELD(SPP, 8, 1);
|
|
// previous machine interrupt-enable
|
|
BF_FIELD(MPIE, 7, 1);
|
|
// previous supervisor interrupt-enable
|
|
BF_FIELD(SPIE, 5, 1);
|
|
// previous user interrupt-enable
|
|
BF_FIELD(UPIE, 4, 1);
|
|
// machine interrupt-enable
|
|
BF_FIELD(MIE, 3, 1);
|
|
// supervisor interrupt-enable
|
|
BF_FIELD(SIE, 1, 1);
|
|
// user interrupt-enable
|
|
BF_FIELD(UIE, 0, 1);
|
|
END_BF_DECL();
|
|
|
|
mstatus_t mstatus;
|
|
|
|
static const reg_t mstatus_reset_val = 0x1800;
|
|
|
|
void write_mstatus(T val) {
|
|
auto mask = get_mask() & 0xff; // MPP is hardcode as 0x3
|
|
auto new_val = (mstatus.backing.val & ~mask) | (val & mask);
|
|
mstatus = new_val;
|
|
}
|
|
|
|
static constexpr T get_mask() {
|
|
// return 0x8000000f007ff9ddULL; // 0b1...0 1111 0000 0000 0111 1111 1111 1001 1011 1011
|
|
//
|
|
// +-TSR
|
|
// |+-TW
|
|
// ||+-TVM
|
|
// |||+-MXR
|
|
// ||||+-SUM
|
|
// |||||+-MPRV
|
|
// |||||| +-XS
|
|
// |||||| | +-FS
|
|
// |||||| | | +-MPP
|
|
// |||||| | | | +-SPP
|
|
// |||||| | | | |+-MPIE
|
|
// ||||||/|/|/| || +-MIE
|
|
return 0b00000000000000000001100010001000;
|
|
}
|
|
};
|
|
|
|
using hart_state_type = hart_state<reg_t>;
|
|
|
|
constexpr reg_t get_irq_mask() {
|
|
return 0b100010001000; // only machine mode is supported
|
|
}
|
|
|
|
riscv_hart_m_p(feature_config cfg = feature_config{});
|
|
|
|
virtual ~riscv_hart_m_p() = default;
|
|
|
|
void reset(uint64_t address) override;
|
|
|
|
iss::status read(const address_type type, const access_type access, const uint32_t space, const uint64_t addr, const unsigned length,
|
|
uint8_t* const data);
|
|
iss::status write(const address_type type, const access_type access, const uint32_t space, const uint64_t addr, const unsigned length,
|
|
const uint8_t* const data);
|
|
|
|
uint64_t enter_trap(uint64_t flags) override { return riscv_hart_m_p::enter_trap(flags, this->fault_data, this->fault_data); }
|
|
uint64_t enter_trap(uint64_t flags, uint64_t addr, uint64_t instr) override;
|
|
uint64_t leave_trap(uint64_t flags) override;
|
|
|
|
void set_csr(unsigned addr, reg_t val) { this->csr[addr & this->csr.page_addr_mask] = val; }
|
|
|
|
void set_irq_num(unsigned i) { mcause_max_irq = 1 << util::ilog2(i); }
|
|
|
|
protected:
|
|
virtual iss::status read_mem(phys_addr_t addr, unsigned length, uint8_t* const data);
|
|
virtual iss::status write_mem(phys_addr_t addr, unsigned length, const uint8_t* const data);
|
|
|
|
iss::status read_clic(uint64_t addr, unsigned length, uint8_t* const data);
|
|
iss::status write_clic(uint64_t addr, unsigned length, const uint8_t* const data);
|
|
|
|
using mem_read_f = iss::status(phys_addr_t addr, unsigned, uint8_t* const);
|
|
using mem_write_f = iss::status(phys_addr_t addr, unsigned, uint8_t const* const);
|
|
|
|
hart_state_type state;
|
|
|
|
using mem_type = util::sparse_array<uint8_t, 1ULL << 32>;
|
|
mem_type mem;
|
|
std::unordered_map<reg_t, uint64_t> ptw;
|
|
std::unordered_map<uint64_t, uint8_t> atomic_reservation;
|
|
uint8_t clic_cfg_reg{0};
|
|
std::array<uint32_t, 32> clic_inttrig_reg;
|
|
union clic_int_reg_t {
|
|
struct {
|
|
uint8_t ip;
|
|
uint8_t ie;
|
|
uint8_t attr;
|
|
uint8_t ctl;
|
|
};
|
|
uint32_t raw;
|
|
};
|
|
std::vector<clic_int_reg_t> clic_int_reg;
|
|
uint8_t clic_mprev_lvl{0};
|
|
uint8_t clic_mact_lvl{0};
|
|
|
|
std::vector<uint8_t> tcm;
|
|
|
|
iss::status read_status(unsigned addr, reg_t& val);
|
|
iss::status write_status(unsigned addr, reg_t val);
|
|
iss::status read_cause(unsigned addr, reg_t& val);
|
|
iss::status write_cause(unsigned addr, reg_t val);
|
|
iss::status read_ie(unsigned addr, reg_t& val);
|
|
iss::status write_ie(unsigned addr, reg_t val);
|
|
iss::status read_ip(unsigned addr, reg_t& val);
|
|
iss::status read_intstatus(unsigned addr, reg_t& val);
|
|
iss::status write_intthresh(unsigned addr, reg_t val);
|
|
iss::status write_xtvt(unsigned addr, reg_t val);
|
|
iss::status write_dcsr(unsigned addr, reg_t val);
|
|
iss::status read_debug(unsigned addr, reg_t& val);
|
|
iss::status write_dscratch(unsigned addr, reg_t val);
|
|
iss::status read_dpc(unsigned addr, reg_t& val);
|
|
iss::status write_dpc(unsigned addr, reg_t val);
|
|
|
|
void check_interrupt();
|
|
std::vector<std::tuple<uint64_t, uint64_t>> memfn_range;
|
|
std::vector<std::function<mem_read_f>> memfn_read;
|
|
std::vector<std::function<mem_write_f>> memfn_write;
|
|
void insert_mem_range(uint64_t, uint64_t, std::function<mem_read_f>, std::function<mem_write_f>);
|
|
feature_config cfg;
|
|
unsigned mcause_max_irq{(FEAT & features_e::FEAT_CLIC) ? std::max(16U, static_cast<unsigned>(traits<BASE>::CLIC_NUM_IRQ)) : 16U};
|
|
|
|
std::pair<std::function<mem_read_f>, std::function<mem_write_f>> replace_mem_access(std::function<mem_read_f> rd,
|
|
std::function<mem_write_f> wr) {
|
|
std::pair<std::function<mem_read_f>, std::function<mem_write_f>> ret{hart_mem_rd_delegate, hart_mem_wr_delegate};
|
|
hart_mem_rd_delegate = rd;
|
|
hart_mem_wr_delegate = wr;
|
|
return ret;
|
|
}
|
|
std::function<mem_read_f> hart_mem_rd_delegate;
|
|
std::function<mem_write_f> hart_mem_wr_delegate;
|
|
};
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
riscv_hart_m_p<BASE, FEAT, LOGCAT>::riscv_hart_m_p(feature_config cfg)
|
|
: state()
|
|
, cfg(cfg) {
|
|
this->rd_func = util::delegate<arch_if::rd_func_sig>::from<this_class, &this_class::read>(this);
|
|
this->wr_func = util::delegate<arch_if::wr_func_sig>::from<this_class, &this_class::write>(this);
|
|
|
|
const std::array<unsigned, 4> rwaddrs{{mepc, mtvec, mscratch, mtval}};
|
|
for(auto addr : rwaddrs) {
|
|
this->csr_rd_cb[addr] = MK_CSR_RD_CB(read_plain);
|
|
// MK_CSR_RD_CB(read_plain(a,r);};
|
|
this->csr_wr_cb[addr] = MK_CSR_WR_CB(write_plain);
|
|
}
|
|
this->csr_rd_cb[mstatus] = MK_CSR_RD_CB(read_status);
|
|
this->csr_wr_cb[mstatus] = MK_CSR_WR_CB(write_status);
|
|
this->csr_rd_cb[mcause] = MK_CSR_RD_CB(read_cause);
|
|
this->csr_wr_cb[mcause] = MK_CSR_WR_CB(write_cause);
|
|
this->csr_rd_cb[mtvec] = MK_CSR_RD_CB(read_tvec);
|
|
this->csr_wr_cb[mepc] = MK_CSR_WR_CB(write_epc);
|
|
this->csr_rd_cb[mip] = MK_CSR_RD_CB(read_ip);
|
|
this->csr_wr_cb[mip] = MK_CSR_WR_CB(write_null);
|
|
this->csr_rd_cb[mie] = MK_CSR_RD_CB(read_ie);
|
|
this->csr_wr_cb[mie] = MK_CSR_WR_CB(write_ie);
|
|
this->csr_wr_cb[misa] = MK_CSR_WR_CB(write_null);
|
|
this->csr_wr_cb[mvendorid] = MK_CSR_WR_CB(write_null);
|
|
this->csr_wr_cb[marchid] = MK_CSR_WR_CB(write_null);
|
|
this->csr_wr_cb[mimpid] = MK_CSR_WR_CB(write_null);
|
|
|
|
if(FEAT & FEAT_CLIC) {
|
|
this->csr_rd_cb[mtvt] = MK_CSR_RD_CB(read_plain);
|
|
this->csr_wr_cb[mtvt] = MK_CSR_WR_CB(write_xtvt);
|
|
// this->csr_rd_cb[mxnti] = MK_CSR_RD_CB(read_plain(a,r);};
|
|
// this->csr_wr_cb[mxnti] = MK_CSR_WR_CB(write_plain(a,r);};
|
|
this->csr_rd_cb[mintstatus] = MK_CSR_RD_CB(read_intstatus);
|
|
this->csr_wr_cb[mintstatus] = MK_CSR_WR_CB(write_null);
|
|
// this->csr_rd_cb[mscratchcsw] = MK_CSR_RD_CB(read_plain(a,r);};
|
|
// this->csr_wr_cb[mscratchcsw] = MK_CSR_WR_CB(write_plain(a,r);};
|
|
// this->csr_rd_cb[mscratchcswl] = MK_CSR_RD_CB(read_plain(a,r);};
|
|
// this->csr_wr_cb[mscratchcswl] = MK_CSR_WR_CB(write_plain(a,r);};
|
|
this->csr_rd_cb[mintthresh] = MK_CSR_RD_CB(read_plain);
|
|
this->csr_wr_cb[mintthresh] = MK_CSR_WR_CB(write_intthresh);
|
|
clic_int_reg.resize(cfg.clic_num_irq, clic_int_reg_t{.raw = 0});
|
|
clic_cfg_reg = 0x20;
|
|
clic_mact_lvl = clic_mprev_lvl = (1 << (cfg.clic_int_ctl_bits)) - 1;
|
|
this->csr[mintthresh] = (1 << (cfg.clic_int_ctl_bits)) - 1;
|
|
insert_mem_range(
|
|
cfg.clic_base, 0x5000UL,
|
|
[this](phys_addr_t addr, unsigned length, uint8_t* const data) { return read_clic(addr.val, length, data); },
|
|
[this](phys_addr_t addr, unsigned length, uint8_t const* const data) { return write_clic(addr.val, length, data); });
|
|
}
|
|
if(FEAT & FEAT_TCM) {
|
|
tcm.resize(cfg.tcm_size);
|
|
std::function<mem_read_f> read_clic_cb = [this](phys_addr_t addr, unsigned length, uint8_t* const data) {
|
|
auto offset = addr.val - this->cfg.tcm_base;
|
|
std::copy(tcm.data() + offset, tcm.data() + offset + length, data);
|
|
return iss::Ok;
|
|
};
|
|
std::function<mem_write_f> write_clic_cb = [this](phys_addr_t addr, unsigned length, uint8_t const* const data) {
|
|
auto offset = addr.val - this->cfg.tcm_base;
|
|
std::copy(data, data + length, tcm.data() + offset);
|
|
return iss::Ok;
|
|
};
|
|
insert_mem_range(cfg.tcm_base, cfg.tcm_size, read_clic_cb, write_clic_cb);
|
|
}
|
|
if(FEAT & FEAT_DEBUG) {
|
|
this->csr_wr_cb[dscratch0] = MK_CSR_WR_CB(write_dscratch);
|
|
this->csr_rd_cb[dscratch0] = MK_CSR_RD_CB(read_debug);
|
|
this->csr_wr_cb[dscratch1] = MK_CSR_WR_CB(write_dscratch);
|
|
this->csr_rd_cb[dscratch1] = MK_CSR_RD_CB(read_debug);
|
|
this->csr_wr_cb[dpc] = MK_CSR_WR_CB(write_dpc);
|
|
this->csr_rd_cb[dpc] = MK_CSR_RD_CB(read_dpc);
|
|
this->csr_wr_cb[dcsr] = MK_CSR_WR_CB(write_dcsr);
|
|
this->csr_rd_cb[dcsr] = MK_CSR_RD_CB(read_debug);
|
|
}
|
|
hart_mem_rd_delegate = [this](phys_addr_t a, unsigned l, uint8_t* const d) -> iss::status { return this->read_mem(a, l, d); };
|
|
hart_mem_wr_delegate = [this](phys_addr_t a, unsigned l, uint8_t const* const d) -> iss::status { return this->write_mem(a, l, d); };
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
inline void riscv_hart_m_p<BASE, FEAT, LOGCAT>::insert_mem_range(uint64_t base, uint64_t size, std::function<mem_read_f> rd_f,
|
|
std::function<mem_write_f> wr_fn) {
|
|
std::tuple<uint64_t, uint64_t> entry{base, size};
|
|
auto it = std::upper_bound(
|
|
memfn_range.begin(), memfn_range.end(), entry,
|
|
[](std::tuple<uint64_t, uint64_t> const& a, std::tuple<uint64_t, uint64_t> const& b) { return std::get<0>(a) < std::get<0>(b); });
|
|
auto idx = std::distance(memfn_range.begin(), it);
|
|
memfn_range.insert(it, entry);
|
|
memfn_read.insert(std::begin(memfn_read) + idx, rd_f);
|
|
memfn_write.insert(std::begin(memfn_write) + idx, wr_fn);
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read(const address_type type, const access_type access, const uint32_t space,
|
|
const uint64_t addr, const unsigned length, uint8_t* const data) {
|
|
#ifndef NDEBUG
|
|
if(access && iss::access_type::DEBUG) {
|
|
CPPLOG(TRACEALL) << "debug read of " << length << " bytes @addr 0x" << std::hex << addr;
|
|
} else if(access && iss::access_type::FETCH) {
|
|
CPPLOG(TRACEALL) << "fetch of " << length << " bytes @addr 0x" << std::hex << addr;
|
|
} else {
|
|
CPPLOG(TRACE) << "read of " << length << " bytes @addr 0x" << std::hex << addr;
|
|
}
|
|
#endif
|
|
try {
|
|
switch(space) {
|
|
case traits<BASE>::MEM: {
|
|
auto alignment = is_fetch(access) ? (this->has_compressed() ? 2 : 4) : std::min<unsigned>(length, sizeof(reg_t));
|
|
if(unlikely(is_fetch(access) && (addr & (alignment - 1)))) {
|
|
this->fault_data = addr;
|
|
if(is_debug(access))
|
|
throw trap_access(0, addr);
|
|
this->reg.trap_state = (1UL << 31); // issue trap 0
|
|
return iss::Err;
|
|
}
|
|
try {
|
|
if(!is_debug(access) && (addr & (alignment - 1))) {
|
|
this->reg.trap_state = (1UL << 31) | 4 << 16;
|
|
this->fault_data = addr;
|
|
return iss::Err;
|
|
}
|
|
phys_addr_t phys_addr{access, space, addr};
|
|
auto res = iss::Err;
|
|
if(!is_fetch(access) && memfn_range.size()) {
|
|
auto it =
|
|
std::find_if(std::begin(memfn_range), std::end(memfn_range), [phys_addr](std::tuple<uint64_t, uint64_t> const& a) {
|
|
return std::get<0>(a) <= phys_addr.val && (std::get<0>(a) + std::get<1>(a)) > phys_addr.val;
|
|
});
|
|
if(it != std::end(memfn_range)) {
|
|
auto idx = std::distance(std::begin(memfn_range), it);
|
|
res = memfn_read[idx](phys_addr, length, data);
|
|
} else
|
|
res = hart_mem_rd_delegate(phys_addr, length, data);
|
|
} else {
|
|
res = hart_mem_rd_delegate(phys_addr, length, data);
|
|
}
|
|
if(unlikely(res != iss::Ok && (access & access_type::DEBUG) == 0)) {
|
|
this->reg.trap_state = (1UL << 31) | (5 << 16); // issue trap 5 (load access fault
|
|
this->fault_data = addr;
|
|
}
|
|
return res;
|
|
} catch(trap_access& ta) {
|
|
if((access & access_type::DEBUG) == 0) {
|
|
this->reg.trap_state = (1UL << 31) | ta.id;
|
|
this->fault_data = ta.addr;
|
|
}
|
|
return iss::Err;
|
|
}
|
|
} break;
|
|
case traits<BASE>::CSR: {
|
|
if(length != sizeof(reg_t))
|
|
return iss::Err;
|
|
return this->read_csr(addr, *reinterpret_cast<reg_t* const>(data));
|
|
} break;
|
|
case traits<BASE>::FENCE: {
|
|
if((addr + length) > mem.size())
|
|
return iss::Err;
|
|
return iss::Ok;
|
|
} break;
|
|
case traits<BASE>::RES: {
|
|
auto it = atomic_reservation.find(addr);
|
|
if(it != atomic_reservation.end() && it->second != 0) {
|
|
memset(data, 0xff, length);
|
|
atomic_reservation.erase(addr);
|
|
} else
|
|
memset(data, 0, length);
|
|
} break;
|
|
default:
|
|
return iss::Err; // assert("Not supported");
|
|
}
|
|
return iss::Ok;
|
|
} catch(trap_access& ta) {
|
|
if((access & access_type::DEBUG) == 0) {
|
|
this->reg.trap_state = (1UL << 31) | ta.id;
|
|
this->fault_data = ta.addr;
|
|
}
|
|
return iss::Err;
|
|
}
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write(const address_type type, const access_type access, const uint32_t space,
|
|
const uint64_t addr, const unsigned length, const uint8_t* const data) {
|
|
#ifndef NDEBUG
|
|
const char* prefix = (access && iss::access_type::DEBUG) ? "debug " : "";
|
|
switch(length) {
|
|
case 8:
|
|
CPPLOG(TRACE) << prefix << "write of " << length << " bytes (0x" << std::hex << *(uint64_t*)&data[0] << std::dec << ") @addr 0x"
|
|
<< std::hex << addr;
|
|
break;
|
|
case 4:
|
|
CPPLOG(TRACE) << prefix << "write of " << length << " bytes (0x" << std::hex << *(uint32_t*)&data[0] << std::dec << ") @addr 0x"
|
|
<< std::hex << addr;
|
|
break;
|
|
case 2:
|
|
CPPLOG(TRACE) << prefix << "write of " << length << " bytes (0x" << std::hex << *(uint16_t*)&data[0] << std::dec << ") @addr 0x"
|
|
<< std::hex << addr;
|
|
break;
|
|
case 1:
|
|
CPPLOG(TRACE) << prefix << "write of " << length << " bytes (0x" << std::hex << (uint16_t)data[0] << std::dec << ") @addr 0x"
|
|
<< std::hex << addr;
|
|
break;
|
|
default:
|
|
CPPLOG(TRACE) << prefix << "write of " << length << " bytes @addr 0x" << std::hex << addr;
|
|
}
|
|
#endif
|
|
try {
|
|
switch(space) {
|
|
case traits<BASE>::MEM: {
|
|
if(unlikely((access && iss::access_type::FETCH) && (addr & 0x1) == 1)) {
|
|
this->fault_data = addr;
|
|
if(access && iss::access_type::DEBUG)
|
|
throw trap_access(0, addr);
|
|
this->reg.trap_state = (1UL << 31); // issue trap 0
|
|
return iss::Err;
|
|
}
|
|
try {
|
|
auto alignment = std::min<unsigned>(length, sizeof(reg_t));
|
|
if(length > 1 && (addr & (alignment - 1)) && !is_debug(access)) {
|
|
this->reg.trap_state = (1UL << 31) | 6 << 16;
|
|
this->fault_data = addr;
|
|
return iss::Err;
|
|
}
|
|
phys_addr_t phys_addr{access, space, addr};
|
|
auto res = iss::Err;
|
|
if(!is_fetch(access) && memfn_range.size()) {
|
|
auto it =
|
|
std::find_if(std::begin(memfn_range), std::end(memfn_range), [phys_addr](std::tuple<uint64_t, uint64_t> const& a) {
|
|
return std::get<0>(a) <= phys_addr.val && (std::get<0>(a) + std::get<1>(a)) > phys_addr.val;
|
|
});
|
|
if(it != std::end(memfn_range)) {
|
|
auto idx = std::distance(std::begin(memfn_range), it);
|
|
res = memfn_write[idx](phys_addr, length, data);
|
|
} else
|
|
res = write_mem(phys_addr, length, data);
|
|
} else {
|
|
res = write_mem(phys_addr, length, data);
|
|
}
|
|
if(unlikely(res != iss::Ok && !is_debug(access))) {
|
|
this->reg.trap_state = (1UL << 31) | (7UL << 16); // issue trap 7 (Store/AMO access fault)
|
|
this->fault_data = addr;
|
|
}
|
|
return res;
|
|
} catch(trap_access& ta) {
|
|
this->reg.trap_state = (1UL << 31) | ta.id;
|
|
this->fault_data = ta.addr;
|
|
return iss::Err;
|
|
}
|
|
} break;
|
|
case traits<BASE>::CSR: {
|
|
if(length != sizeof(reg_t))
|
|
return iss::Err;
|
|
return this->write_csr(addr, *reinterpret_cast<const reg_t*>(data));
|
|
} break;
|
|
case traits<BASE>::FENCE: {
|
|
if((addr + length) > mem.size())
|
|
return iss::Err;
|
|
switch(addr) {
|
|
case 2:
|
|
case 3: {
|
|
ptw.clear();
|
|
auto tvm = state.mstatus.TVM;
|
|
return iss::Ok;
|
|
}
|
|
}
|
|
} break;
|
|
case traits<BASE>::RES: {
|
|
atomic_reservation[addr] = data[0];
|
|
} break;
|
|
default:
|
|
return iss::Err;
|
|
}
|
|
return iss::Ok;
|
|
} catch(trap_access& ta) {
|
|
if((access & access_type::DEBUG) == 0) {
|
|
this->reg.trap_state = (1UL << 31) | ta.id;
|
|
this->fault_data = ta.addr;
|
|
}
|
|
return iss::Err;
|
|
}
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_status(unsigned addr, reg_t& val) {
|
|
val = state.mstatus & hart_state_type::get_mask();
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_status(unsigned addr, reg_t val) {
|
|
state.write_mstatus(val);
|
|
check_interrupt();
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_cause(unsigned addr, reg_t& val) {
|
|
if((FEAT & features_e::FEAT_CLIC) && (this->csr[mtvec] & 0x3) == 3) {
|
|
val = this->csr[addr] & ((1UL << (traits<BASE>::XLEN - 1)) | (mcause_max_irq - 1) | (0xfUL << 16));
|
|
val |= clic_mprev_lvl << 16;
|
|
val |= state.mstatus.MPIE << 27;
|
|
val |= state.mstatus.MPP << 28;
|
|
} else
|
|
val = this->csr[addr] & ((1UL << (traits<BASE>::XLEN - 1)) | (mcause_max_irq - 1));
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_cause(unsigned addr, reg_t val) {
|
|
if((FEAT & features_e::FEAT_CLIC) && (this->csr[mtvec] & 0x3) == 3) {
|
|
auto mask = ((1UL << (traits<BASE>::XLEN - 1)) | (mcause_max_irq - 1) | (0xfUL << 16));
|
|
this->csr[addr] = (val & mask) | (this->csr[addr] & ~mask);
|
|
clic_mprev_lvl = ((val >> 16) & 0xff) | (1 << (8 - cfg.clic_int_ctl_bits)) - 1;
|
|
state.mstatus.MPIE = (val >> 27) & 0x1;
|
|
state.mstatus.MPP = (val >> 28) & 0x3;
|
|
} else {
|
|
auto mask = ((1UL << (traits<BASE>::XLEN - 1)) | (mcause_max_irq - 1));
|
|
this->csr[addr] = (val & mask) | (this->csr[addr] & ~mask);
|
|
}
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_ie(unsigned addr, reg_t& val) {
|
|
auto mask = get_irq_mask();
|
|
val = this->csr[mie] & mask;
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_ie(unsigned addr, reg_t val) {
|
|
auto mask = get_irq_mask();
|
|
this->csr[mie] = (this->csr[mie] & ~mask) | (val & mask);
|
|
check_interrupt();
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_ip(unsigned addr, reg_t& val) {
|
|
auto mask = get_irq_mask();
|
|
val = this->csr[mip] & mask;
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_intstatus(unsigned addr, reg_t& val) {
|
|
val = (clic_mact_lvl & 0xff) << 24;
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_intthresh(unsigned addr, reg_t val) {
|
|
this->csr[addr] = (val & 0xff) | (1 << (cfg.clic_int_ctl_bits)) - 1;
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_mem(phys_addr_t paddr, unsigned length, uint8_t* const data) {
|
|
switch(paddr.val) {
|
|
default: {
|
|
for(auto offs = 0U; offs < length; ++offs) {
|
|
*(data + offs) = mem[(paddr.val + offs) % mem.size()];
|
|
}
|
|
}
|
|
}
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_mem(phys_addr_t paddr, unsigned length, const uint8_t* const data) {
|
|
mem_type::page_type& p = mem(paddr.val / mem.page_size);
|
|
std::copy(data, data + length, p.data() + (paddr.val & mem.page_addr_mask));
|
|
// this->tohost handling in case of riscv-test
|
|
// according to https://github.com/riscv-software-src/riscv-isa-sim/issues/364#issuecomment-607657754:
|
|
if(paddr.access && iss::access_type::FUNC) {
|
|
if(paddr.val == this->tohost) {
|
|
reg_t cur_data = *reinterpret_cast<const reg_t*>(data);
|
|
// Extract Device (bits 63:56)
|
|
uint8_t device = traits<BASE>::XLEN == 32
|
|
? *reinterpret_cast<uint32_t*>(p.data() + ((this->tohost + 4) & mem.page_addr_mask)) >> 24
|
|
: (cur_data >> 56) & 0xFF;
|
|
// Extract Command (bits 55:48)
|
|
uint8_t command = traits<BASE>::XLEN == 32
|
|
? *reinterpret_cast<uint32_t*>(p.data() + ((this->tohost + 4) & mem.page_addr_mask)) >> 16
|
|
: (cur_data >> 48) & 0xFF;
|
|
// Extract payload (bits 47:0)
|
|
uint64_t payload_addr = cur_data & 0xFFFFFFFFFFFFULL;
|
|
if(payload_addr & 1) {
|
|
CPPLOG(FATAL) << "this->tohost value is 0x" << std::hex << payload_addr << std::dec << " (" << payload_addr
|
|
<< "), stopping simulation";
|
|
this->reg.trap_state = std::numeric_limits<uint32_t>::max();
|
|
this->interrupt_sim = payload_addr;
|
|
return iss::Ok;
|
|
} else if(device == 0 && command == 0) {
|
|
std::array<uint64_t, 8> loaded_payload;
|
|
if(read(address_type::PHYSICAL, access_type::DEBUG_READ, traits<BASE>::MEM, payload_addr, 8 * sizeof(uint64_t),
|
|
reinterpret_cast<uint8_t*>(loaded_payload.data())) == iss::Err)
|
|
CPPLOG(ERR) << "Syscall read went wrong";
|
|
uint64_t syscall_num = loaded_payload.at(0);
|
|
if(syscall_num == 64) { // SYS_WRITE
|
|
return this->execute_sys_write(this, loaded_payload, traits<BASE>::MEM);
|
|
} else {
|
|
CPPLOG(ERR) << "this->tohost syscall with number 0x" << std::hex << syscall_num << std::dec << " (" << syscall_num
|
|
<< ") not implemented";
|
|
this->reg.trap_state = std::numeric_limits<uint32_t>::max();
|
|
this->interrupt_sim = payload_addr;
|
|
return iss::Ok;
|
|
}
|
|
} else {
|
|
CPPLOG(ERR) << "this->tohost functionality not implemented for device " << device << " and command " << command;
|
|
this->reg.trap_state = std::numeric_limits<uint32_t>::max();
|
|
this->interrupt_sim = payload_addr;
|
|
return iss::Ok;
|
|
}
|
|
}
|
|
if((traits<BASE>::XLEN == 32 && paddr.val == this->fromhost + 4) || (traits<BASE>::XLEN == 64 && paddr.val == this->fromhost)) {
|
|
uint64_t fhostvar = *reinterpret_cast<uint64_t*>(p.data() + (this->fromhost & mem.page_addr_mask));
|
|
*reinterpret_cast<uint64_t*>(p.data() + (this->tohost & mem.page_addr_mask)) = fhostvar;
|
|
}
|
|
}
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_clic(uint64_t addr, unsigned length, uint8_t* const data) {
|
|
if(addr == cfg.clic_base) { // cliccfg
|
|
*data = clic_cfg_reg;
|
|
for(auto i = 1; i < length; ++i)
|
|
*(data + i) = 0;
|
|
} else if(addr >= (cfg.clic_base + 0x40) && (addr + length) <= (cfg.clic_base + 0x40 + cfg.clic_num_trigger * 4)) { // clicinttrig
|
|
auto offset = ((addr & 0x7fff) - 0x40) / 4;
|
|
read_reg_uint32(addr, clic_inttrig_reg[offset], data, length);
|
|
} else if(addr >= (cfg.clic_base + 0x1000) &&
|
|
(addr + length) <= (cfg.clic_base + 0x1000 + cfg.clic_num_irq * 4)) { // clicintip/clicintie/clicintattr/clicintctl
|
|
auto offset = ((addr & 0x7fff) - 0x1000) / 4;
|
|
read_reg_uint32(addr, clic_int_reg[offset].raw, data, length);
|
|
} else {
|
|
for(auto i = 0U; i < length; ++i)
|
|
*(data + i) = 0;
|
|
}
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_clic(uint64_t addr, unsigned length, const uint8_t* const data) {
|
|
if(addr == cfg.clic_base) { // cliccfg
|
|
clic_cfg_reg = (clic_cfg_reg & ~0x1e) | (*data & 0x1e);
|
|
} else if(addr >= (cfg.clic_base + 0x40) && (addr + length) <= (cfg.clic_base + 0x40 + cfg.clic_num_trigger * 4)) { // clicinttrig
|
|
auto offset = ((addr & 0x7fff) - 0x40) / 4;
|
|
write_reg_uint32(addr, clic_inttrig_reg[offset], data, length);
|
|
} else if(addr >= (cfg.clic_base + 0x1000) &&
|
|
(addr + length) <= (cfg.clic_base + 0x1000 + cfg.clic_num_irq * 4)) { // clicintip/clicintie/clicintattr/clicintctl
|
|
auto offset = ((addr & 0x7fff) - 0x1000) / 4;
|
|
write_reg_uint32(addr, clic_int_reg[offset].raw, data, length);
|
|
clic_int_reg[offset].raw &= 0xf0c70101; // clicIntCtlBits->0xf0, clicintattr->0xc7, clicintie->0x1, clicintip->0x1
|
|
}
|
|
return iss::Ok;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT> inline void riscv_hart_m_p<BASE, FEAT, LOGCAT>::reset(uint64_t address) {
|
|
BASE::reset(address);
|
|
state.mstatus = hart_state_type::mstatus_reset_val;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT> void riscv_hart_m_p<BASE, FEAT, LOGCAT>::check_interrupt() {
|
|
// TODO: Implement CLIC functionality
|
|
// auto ideleg = csr[mideleg];
|
|
// Multiple simultaneous interrupts and traps at the same privilege level are
|
|
// handled in the following decreasing priority order:
|
|
// external interrupts, software interrupts, timer interrupts, then finally
|
|
// any synchronous traps.
|
|
auto ena_irq = this->csr[mip] & this->csr[mie];
|
|
|
|
bool mstatus_mie = state.mstatus.MIE;
|
|
auto m_enabled = this->reg.PRIV < PRIV_M || mstatus_mie;
|
|
auto enabled_interrupts = m_enabled ? ena_irq : 0;
|
|
|
|
if(enabled_interrupts != 0) {
|
|
int res = 0;
|
|
while((enabled_interrupts & 1) == 0) {
|
|
enabled_interrupts >>= 1;
|
|
res++;
|
|
}
|
|
this->reg.pending_trap = res << 16 | 1; // 0x80 << 24 | (cause << 16) | trap_id
|
|
}
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
uint64_t riscv_hart_m_p<BASE, FEAT, LOGCAT>::enter_trap(uint64_t flags, uint64_t addr, uint64_t tval) {
|
|
// flags are ACTIVE[31:31], CAUSE[30:16], TRAPID[15:0]
|
|
// calculate and write mcause val
|
|
auto const trap_id = bit_sub<0, 16>(flags);
|
|
auto cause = bit_sub<16, 15>(flags);
|
|
// calculate effective privilege level
|
|
unsigned new_priv = PRIV_M;
|
|
if(trap_id == 0) { // exception
|
|
if(cause == 11)
|
|
cause = 0x8 + PRIV_M; // adjust environment call cause
|
|
// store ret addr in xepc register
|
|
this->csr[mepc] = static_cast<reg_t>(addr) & this->get_pc_mask(); // store actual address instruction of exception
|
|
/*
|
|
* write mtval if new_priv=M_MODE, spec says:
|
|
* When a hardware breakpoint is triggered, or an instruction-fetch, load,
|
|
* or store address-misaligned,
|
|
* access, or page-fault exception occurs, mtval is written with the
|
|
* faulting effective address.
|
|
*/
|
|
switch(cause) {
|
|
case 0:
|
|
this->csr[mtval] = static_cast<reg_t>(tval);
|
|
break;
|
|
case 2:
|
|
this->csr[mtval] = (!this->has_compressed() || (tval & 0x3) == 3) ? tval : tval & 0xffff;
|
|
break;
|
|
case 3:
|
|
if((FEAT & FEAT_DEBUG) && (this->csr[dcsr] & 0x8000)) {
|
|
this->reg.DPC = addr;
|
|
this->csr[dcsr] = (this->csr[dcsr] & ~0x1c3) | (1 << 6) | PRIV_M; // FIXME: cause should not be 4 (stepi)
|
|
new_priv = this->reg.PRIV | PRIV_D;
|
|
} else {
|
|
this->csr[mtval] = addr;
|
|
}
|
|
if(this->semihosting_cb) {
|
|
// Check for semihosting call
|
|
phys_addr_t p_addr(access_type::DEBUG_READ, traits<BASE>::MEM, addr - 4);
|
|
std::array<uint8_t, 8> data;
|
|
// check for SLLI_X0_X0_0X1F and SRAI_X0_X0_0X07
|
|
this->read_mem(p_addr, 4, data.data());
|
|
p_addr.val += 8;
|
|
this->read_mem(p_addr, 4, data.data() + 4);
|
|
|
|
const std::array<uint8_t, 8> ref_data = {0x13, 0x10, 0xf0, 0x01, 0x13, 0x50, 0x70, 0x40};
|
|
if(data == ref_data) {
|
|
this->reg.NEXT_PC = addr + 8;
|
|
|
|
std::array<char, 32> buffer;
|
|
#if defined(_MSC_VER)
|
|
sprintf(buffer.data(), "0x%016llx", addr);
|
|
#else
|
|
sprintf(buffer.data(), "0x%016lx", addr);
|
|
#endif
|
|
NSCLOG(INFO, LOGCAT) << "Semihosting call at address " << buffer.data() << " occurred ";
|
|
|
|
this->semihosting_cb(this, &(this->reg.X10) /*a0*/, &(this->reg.X11) /*a1*/);
|
|
return this->reg.NEXT_PC;
|
|
}
|
|
}
|
|
break;
|
|
case 4:
|
|
case 6:
|
|
this->csr[mtval] = this->fault_data;
|
|
break;
|
|
default:
|
|
this->csr[mtval] = 0;
|
|
}
|
|
this->fault_data = 0;
|
|
} else {
|
|
this->csr[mepc] = this->reg.NEXT_PC & this->get_pc_mask(); // store next address if interrupt
|
|
this->reg.pending_trap = 0;
|
|
}
|
|
this->csr[mcause] = (trap_id << (traits<BASE>::XLEN - 1)) + cause;
|
|
// update mstatus
|
|
// xPP field of mstatus is written with the active privilege mode at the time
|
|
// of the trap; the x PIE field of mstatus
|
|
// is written with the value of the active interrupt-enable bit at the time of
|
|
// the trap; and the x IE field of mstatus
|
|
// is cleared
|
|
// store the actual privilege level in yPP and store interrupt enable flags
|
|
state.mstatus.MPP = PRIV_M;
|
|
state.mstatus.MPIE = state.mstatus.MIE;
|
|
state.mstatus.MIE = false;
|
|
|
|
// get trap vector
|
|
auto xtvec = this->csr[mtvec];
|
|
// calculate adds// set NEXT_PC to trap addressess to jump to based on MODE
|
|
if((FEAT & features_e::FEAT_CLIC) && trap_id != 0 && (xtvec & 0x3UL) == 3UL) {
|
|
reg_t data;
|
|
auto ret = read(address_type::LOGICAL, access_type::READ, 0, this->csr[mtvt], sizeof(reg_t), reinterpret_cast<uint8_t*>(&data));
|
|
if(ret == iss::Err)
|
|
return this->reg.PC;
|
|
this->reg.NEXT_PC = data;
|
|
} else {
|
|
// bits in mtvec
|
|
this->reg.NEXT_PC = xtvec & ~0x3UL;
|
|
if((xtvec & 0x1) == 1 && trap_id != 0)
|
|
this->reg.NEXT_PC += 4 * cause;
|
|
}
|
|
// reset trap state
|
|
this->reg.PRIV = new_priv;
|
|
this->reg.trap_state = 0;
|
|
std::array<char, 32> buffer;
|
|
#if defined(_MSC_VER)
|
|
sprintf(buffer.data(), "0x%016llx", addr);
|
|
#else
|
|
sprintf(buffer.data(), "0x%016lx", addr);
|
|
#endif
|
|
if((flags & 0xffffffff) != 0xffffffff)
|
|
NSCLOG(INFO, LOGCAT) << (trap_id ? "Interrupt" : "Trap") << " with cause '"
|
|
<< (trap_id ? this->irq_str[cause] : this->trap_str[cause]) << "' (" << cause << ")"
|
|
<< " at address " << buffer.data() << " occurred";
|
|
return this->reg.NEXT_PC;
|
|
}
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT> uint64_t riscv_hart_m_p<BASE, FEAT, LOGCAT>::leave_trap(uint64_t flags) {
|
|
state.mstatus.MIE = state.mstatus.MPIE;
|
|
state.mstatus.MPIE = 1;
|
|
// sets the pc to the value stored in the x epc register.
|
|
this->reg.NEXT_PC = this->csr[mepc] & this->get_pc_mask();
|
|
NSCLOG(INFO, LOGCAT) << "Executing xRET";
|
|
check_interrupt();
|
|
this->reg.trap_state = this->reg.pending_trap;
|
|
return this->reg.NEXT_PC;
|
|
}
|
|
|
|
} // namespace arch
|
|
} // namespace iss
|
|
|
|
#endif /* _RISCV_HART_M_P_H */
|