2017-08-27 12:10:38 +02:00
|
|
|
/*******************************************************************************
|
2023-04-22 17:04:41 +02:00
|
|
|
* Copyright (C) 2019 - 2023 MINRES Technologies GmbH
|
2017-08-27 12:10:38 +02:00
|
|
|
* All rights reserved.
|
2017-09-22 11:23:23 +02:00
|
|
|
*
|
2017-08-27 12:10:38 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
2017-09-22 11:23:23 +02:00
|
|
|
*
|
2017-08-27 12:10:38 +02:00
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
2017-09-22 11:23:23 +02:00
|
|
|
*
|
2017-08-27 12:10:38 +02:00
|
|
|
* 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.
|
2017-09-22 11:23:23 +02:00
|
|
|
*
|
2017-08-27 12:10:38 +02:00
|
|
|
* 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.
|
2017-09-22 11:23:23 +02:00
|
|
|
*
|
2017-08-27 12:10:38 +02:00
|
|
|
* 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.
|
2017-09-22 11:23:23 +02:00
|
|
|
*
|
2017-08-27 12:10:38 +02:00
|
|
|
* Contributors:
|
2017-11-27 00:14:41 +01:00
|
|
|
* eyck@minres.com - initial implementation
|
2017-08-27 12:10:38 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2021-05-16 15:06:42 +02:00
|
|
|
#ifndef _RISCV_HART_M_P_H
|
|
|
|
#define _RISCV_HART_M_P_H
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2018-03-30 17:59:40 +02:00
|
|
|
#include "iss/arch/traits.h"
|
2018-11-08 13:31:28 +01:00
|
|
|
#include "iss/instrumentation_if.h"
|
2018-03-30 17:59:40 +02:00
|
|
|
#include "iss/log_categories.h"
|
|
|
|
#include "iss/vm_if.h"
|
2024-07-28 15:36:12 +02:00
|
|
|
#include "iss/vm_types.h"
|
2023-10-29 17:06:56 +01:00
|
|
|
#include "riscv_hart_common.h"
|
2024-03-19 11:02:03 +01:00
|
|
|
#include <stdexcept>
|
2019-07-14 16:51:14 +02:00
|
|
|
#ifndef FMT_HEADER_ONLY
|
2019-04-11 05:40:02 +00:00
|
|
|
#define FMT_HEADER_ONLY
|
2019-07-14 16:51:14 +02:00
|
|
|
#endif
|
2018-11-08 13:31:28 +01:00
|
|
|
#include <array>
|
2017-08-27 12:10:38 +02:00
|
|
|
#include <elfio/elfio.hpp>
|
2020-09-04 15:37:21 +02:00
|
|
|
#include <fmt/format.h>
|
2023-10-29 17:06:56 +01:00
|
|
|
#include <functional>
|
2017-09-22 11:23:23 +02:00
|
|
|
#include <iomanip>
|
2017-08-27 12:10:38 +02:00
|
|
|
#include <sstream>
|
2018-11-08 13:31:28 +01:00
|
|
|
#include <type_traits>
|
2017-09-21 13:13:01 +02:00
|
|
|
#include <unordered_map>
|
2018-11-08 13:31:28 +01:00
|
|
|
#include <util/bit_field.h>
|
2017-09-22 11:23:23 +02:00
|
|
|
#include <util/ities.h>
|
|
|
|
#include <util/sparse_array.h>
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2024-01-08 17:17:59 +01:00
|
|
|
#include <iss/semihosting/semihosting.h>
|
|
|
|
|
2017-08-27 12:10:38 +02:00
|
|
|
namespace iss {
|
|
|
|
namespace arch {
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT = FEAT_NONE, typename LOGCAT = logging::disass>
|
|
|
|
class riscv_hart_m_p : public BASE, public riscv_hart_common {
|
2021-05-16 15:06:42 +02:00
|
|
|
protected:
|
|
|
|
const std::array<const char, 4> lvl = {{'U', 'S', 'H', 'M'}};
|
2023-10-29 17:06:56 +01:00
|
|
|
const std::array<const char*, 16> trap_str = {{""
|
|
|
|
"Instruction address misaligned", // 0
|
|
|
|
"Instruction access fault", // 1
|
|
|
|
"Illegal instruction", // 2
|
|
|
|
"Breakpoint", // 3
|
|
|
|
"Load address misaligned", // 4
|
|
|
|
"Load access fault", // 5
|
|
|
|
"Store/AMO address misaligned", // 6
|
|
|
|
"Store/AMO access fault", // 7
|
|
|
|
"Environment call from U-mode", // 8
|
|
|
|
"Environment call from S-mode", // 9
|
|
|
|
"Reserved", // a
|
|
|
|
"Environment call from M-mode", // b
|
|
|
|
"Instruction page fault", // c
|
|
|
|
"Load page fault", // d
|
|
|
|
"Reserved", // e
|
|
|
|
"Store/AMO page fault"}};
|
|
|
|
const std::array<const char*, 12> irq_str = {{"User software interrupt", "Supervisor software interrupt", "Reserved",
|
|
|
|
"Machine software interrupt", "User timer interrupt", "Supervisor timer interrupt",
|
|
|
|
"Reserved", "Machine timer interrupt", "User external interrupt",
|
|
|
|
"Supervisor external interrupt", "Reserved", "Machine external interrupt"}};
|
|
|
|
|
2017-10-04 23:10:29 +02:00
|
|
|
public:
|
2021-06-26 14:30:36 +02:00
|
|
|
using core = BASE;
|
2024-07-24 12:20:38 +02:00
|
|
|
using this_class = riscv_hart_m_p<BASE, FEAT, LOGCAT>;
|
2021-06-26 14:30:36 +02:00
|
|
|
using phys_addr_t = typename core::phys_addr_t;
|
|
|
|
using reg_t = typename core::reg_t;
|
|
|
|
using addr_t = typename core::addr_t;
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
using rd_csr_f = iss::status (this_class::*)(unsigned addr, reg_t&);
|
|
|
|
using wr_csr_f = iss::status (this_class::*)(unsigned addr, reg_t);
|
|
|
|
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);
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2017-10-04 23:10:29 +02:00
|
|
|
// primary template
|
2018-11-08 13:31:28 +01:00
|
|
|
template <class T, class Enable = void> struct hart_state {};
|
2017-10-04 23:10:29 +02:00
|
|
|
// specialization 32bit
|
2018-11-08 13:31:28 +01:00
|
|
|
template <typename T> class hart_state<T, typename std::enable_if<std::is_same<T, uint32_t>::value>::type> {
|
2017-10-12 22:41:37 +02:00
|
|
|
public:
|
2017-10-04 23:10:29 +02:00
|
|
|
BEGIN_BF_DECL(mstatus_t, T);
|
2023-10-29 17:06:56 +01:00
|
|
|
// 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)))
|
2017-10-04 23:10:29 +02:00
|
|
|
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);
|
2023-10-29 17:06:56 +01:00
|
|
|
// status of additional user-mode extensions and associated state, All off/None dirty or clean, some on/None
|
|
|
|
// dirty, some clean/Some dirty
|
2017-10-04 23:10:29 +02:00
|
|
|
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;
|
|
|
|
|
2021-07-09 07:37:12 +02:00
|
|
|
static const reg_t mstatus_reset_val = 0x1800;
|
2017-11-18 00:42:33 +01:00
|
|
|
|
2020-09-07 11:54:45 +02:00
|
|
|
void write_mstatus(T val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
auto mask = get_mask() & 0xff; // MPP is hardcode as 0x3
|
2021-03-01 06:36:27 +00:00
|
|
|
auto new_val = (mstatus.backing.val & ~mask) | (val & mask);
|
2018-11-08 13:31:28 +01:00
|
|
|
mstatus = new_val;
|
2017-11-18 00:42:33 +01:00
|
|
|
}
|
|
|
|
|
2020-09-07 11:54:45 +02:00
|
|
|
static constexpr uint32_t get_mask() {
|
2023-10-29 17:06:56 +01:00
|
|
|
// return 0x807ff988UL; // 0b1000 0000 0111 1111 1111 1000 1000 1000 // only machine mode is supported
|
2021-07-06 21:19:36 +02:00
|
|
|
// +-SD
|
|
|
|
// | +-TSR
|
|
|
|
// | |+-TW
|
|
|
|
// | ||+-TVM
|
|
|
|
// | |||+-MXR
|
|
|
|
// | ||||+-SUM
|
|
|
|
// | |||||+-MPRV
|
|
|
|
// | |||||| +-XS
|
|
|
|
// | |||||| | +-FS
|
|
|
|
// | |||||| | | +-MPP
|
|
|
|
// | |||||| | | | +-SPP
|
|
|
|
// | |||||| | | | |+-MPIE
|
|
|
|
// | ||||||/|/|/| || +-MIE
|
|
|
|
return 0b00000000000000000001100010001000;
|
2017-10-04 23:10:29 +02:00
|
|
|
}
|
|
|
|
};
|
2023-03-23 07:47:21 +01:00
|
|
|
|
|
|
|
// 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);
|
2023-10-29 17:06:56 +01:00
|
|
|
// 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)))
|
2023-03-23 07:47:21 +01:00
|
|
|
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);
|
2023-10-29 17:06:56 +01:00
|
|
|
// status of additional user-mode extensions and associated state, All off/None dirty or clean, some on/None
|
|
|
|
// dirty, some clean/Some dirty
|
2023-03-23 07:47:21 +01:00
|
|
|
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;
|
|
|
|
|
2023-03-25 09:14:56 +01:00
|
|
|
static const reg_t mstatus_reset_val = 0x1800;
|
2023-03-23 07:47:21 +01:00
|
|
|
|
|
|
|
void write_mstatus(T val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
auto mask = get_mask() & 0xff; // MPP is hardcode as 0x3
|
2023-03-23 07:47:21 +01:00
|
|
|
auto new_val = (mstatus.backing.val & ~mask) | (val & mask);
|
|
|
|
mstatus = new_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr T get_mask() {
|
2023-10-29 17:06:56 +01:00
|
|
|
// return 0x8000000f007ff9ddULL; // 0b1...0 1111 0000 0000 0111 1111 1111 1001 1011 1011
|
2023-03-25 09:14:56 +01:00
|
|
|
//
|
|
|
|
// +-TSR
|
|
|
|
// |+-TW
|
|
|
|
// ||+-TVM
|
|
|
|
// |||+-MXR
|
|
|
|
// ||||+-SUM
|
|
|
|
// |||||+-MPRV
|
|
|
|
// |||||| +-XS
|
|
|
|
// |||||| | +-FS
|
|
|
|
// |||||| | | +-MPP
|
|
|
|
// |||||| | | | +-SPP
|
|
|
|
// |||||| | | | |+-MPIE
|
|
|
|
// ||||||/|/|/| || +-MIE
|
|
|
|
return 0b00000000000000000001100010001000;
|
2023-03-23 07:47:21 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-16 15:06:42 +02:00
|
|
|
using hart_state_type = hart_state<reg_t>;
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2020-09-07 11:54:45 +02:00
|
|
|
constexpr reg_t get_irq_mask() {
|
2021-07-06 21:19:36 +02:00
|
|
|
return 0b100010001000; // only machine mode is supported
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
constexpr bool has_compressed() { return traits<BASE>::MISA_VAL & 0b0100; }
|
|
|
|
constexpr reg_t get_pc_mask() { return has_compressed() ? (reg_t)~1 : (reg_t)~3; }
|
2021-07-07 11:30:00 +02:00
|
|
|
|
2022-10-05 08:58:57 +02:00
|
|
|
riscv_hart_m_p(feature_config cfg = feature_config{});
|
2020-08-24 15:01:54 +02:00
|
|
|
virtual ~riscv_hart_m_p() = default;
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2017-11-18 00:42:33 +01:00
|
|
|
void reset(uint64_t address) override;
|
|
|
|
|
2018-11-08 13:31:28 +01:00
|
|
|
std::pair<uint64_t, bool> load_file(std::string name, int type = -1) override;
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
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) override;
|
|
|
|
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) override;
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2022-10-05 08:58:57 +02:00
|
|
|
uint64_t enter_trap(uint64_t flags) override { return riscv_hart_m_p::enter_trap(flags, fault_data, fault_data); }
|
|
|
|
uint64_t enter_trap(uint64_t flags, uint64_t addr, uint64_t instr) override;
|
|
|
|
uint64_t leave_trap(uint64_t flags) override;
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
const reg_t& get_mhartid() const { return mhartid_reg; }
|
|
|
|
void set_mhartid(reg_t mhartid) { mhartid_reg = mhartid; };
|
2020-09-04 15:37:21 +02:00
|
|
|
|
2017-10-22 19:29:37 +02:00
|
|
|
void disass_output(uint64_t pc, const std::string instr) override {
|
2024-07-24 12:20:38 +02:00
|
|
|
NSCLOG(INFO, LOGCAT) << fmt::format("0x{:016x} {:40} [s:0x{:x};c:{}]", pc, instr, (reg_t)state.mstatus,
|
2024-07-24 14:48:50 +02:00
|
|
|
this->reg.icount + cycle_offset);
|
2017-08-27 12:10:38 +02:00
|
|
|
};
|
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
iss::instrumentation_if* get_instrumentation_if() override { return &instr_if; }
|
2021-03-06 07:17:42 +00:00
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
void set_csr(unsigned addr, reg_t val) { csr[addr & csr.page_addr_mask] = val; }
|
|
|
|
|
|
|
|
void set_irq_num(unsigned i) { mcause_max_irq = 1 << util::ilog2(i); }
|
2021-03-07 10:51:00 +00:00
|
|
|
|
2024-05-31 10:43:38 +02:00
|
|
|
void set_semihosting_callback(semihosting_cb_t<reg_t> cb) { semihosting_cb = cb; };
|
2024-01-08 17:17:59 +01:00
|
|
|
|
2017-08-27 12:10:38 +02:00
|
|
|
protected:
|
2018-11-08 13:31:28 +01:00
|
|
|
struct riscv_instrumentation_if : public iss::instrumentation_if {
|
2018-03-30 17:59:40 +02:00
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
riscv_instrumentation_if(riscv_hart_m_p<BASE, FEAT, LOGCAT>& arch)
|
2018-11-08 13:31:28 +01:00
|
|
|
: arch(arch) {}
|
2018-03-30 17:59:40 +02:00
|
|
|
/**
|
|
|
|
* get the name of this architecture
|
|
|
|
*
|
|
|
|
* @return the name of this architecture
|
|
|
|
*/
|
2018-11-08 13:31:28 +01:00
|
|
|
const std::string core_type_name() const override { return traits<BASE>::core_type; }
|
2018-03-30 17:59:40 +02:00
|
|
|
|
2023-09-22 12:40:35 +02:00
|
|
|
uint64_t get_pc() override { return arch.reg.PC; }
|
2018-03-30 17:59:40 +02:00
|
|
|
|
2023-09-22 12:40:35 +02:00
|
|
|
uint64_t get_next_pc() override { return arch.reg.NEXT_PC; }
|
2022-04-25 14:18:19 +02:00
|
|
|
|
2023-05-27 10:20:49 +02:00
|
|
|
uint64_t get_instr_word() override { return arch.reg.instruction; }
|
2018-03-30 17:59:40 +02:00
|
|
|
|
2023-05-27 10:20:49 +02:00
|
|
|
uint64_t get_instr_count() override { return arch.reg.icount; }
|
2022-04-26 17:14:33 +02:00
|
|
|
|
2023-05-27 10:20:49 +02:00
|
|
|
uint64_t get_pendig_traps() override { return arch.reg.trap_state; }
|
2022-02-09 21:01:17 +01:00
|
|
|
|
2023-05-27 10:20:49 +02:00
|
|
|
uint64_t get_total_cycles() override { return arch.reg.icount + arch.cycle_offset; }
|
2022-02-09 21:01:17 +01:00
|
|
|
|
2023-09-22 12:40:35 +02:00
|
|
|
void update_last_instr_cycles(unsigned cycles) override { arch.cycle_offset += cycles - 1; }
|
2023-04-22 17:04:41 +02:00
|
|
|
|
2023-09-22 12:40:35 +02:00
|
|
|
bool is_branch_taken() override { return arch.reg.last_branch; }
|
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
unsigned get_reg_num() override { return traits<BASE>::NUM_REGS; }
|
2018-03-30 17:59:40 +02:00
|
|
|
|
2023-11-20 11:45:52 +01:00
|
|
|
unsigned get_reg_size(unsigned num) override { return traits<BASE>::reg_bit_widths[num]; }
|
|
|
|
|
2024-03-19 11:02:03 +01:00
|
|
|
std::unordered_map<std::string, uint64_t> get_symbol_table(std::string name) override { return arch.get_sym_table(name); }
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
riscv_hart_m_p<BASE, FEAT, LOGCAT>& arch;
|
2018-03-30 17:59:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
friend struct riscv_instrumentation_if;
|
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
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);
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
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);
|
2021-11-02 11:13:29 +01:00
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
virtual iss::status read_csr(unsigned addr, reg_t& val);
|
2017-09-22 11:23:23 +02:00
|
|
|
virtual iss::status write_csr(unsigned addr, reg_t val);
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2021-05-16 15:06:42 +02:00
|
|
|
hart_state_type state;
|
2021-06-29 11:51:19 +02:00
|
|
|
int64_t cycle_offset{0};
|
|
|
|
uint64_t mcycle_csr{0};
|
|
|
|
int64_t instret_offset{0};
|
|
|
|
uint64_t minstret_csr{0};
|
2018-03-30 17:59:40 +02:00
|
|
|
reg_t fault_data;
|
2017-08-27 12:10:38 +02:00
|
|
|
uint64_t tohost = tohost_dflt;
|
|
|
|
uint64_t fromhost = fromhost_dflt;
|
2024-01-08 17:17:59 +01:00
|
|
|
bool tohost_lower_written = false;
|
2018-03-30 17:59:40 +02:00
|
|
|
riscv_instrumentation_if instr_if;
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2024-05-31 10:43:38 +02:00
|
|
|
semihosting_cb_t<reg_t> semihosting_cb;
|
2024-01-08 17:17:59 +01:00
|
|
|
|
2017-09-22 11:23:23 +02:00
|
|
|
using mem_type = util::sparse_array<uint8_t, 1ULL << 32>;
|
|
|
|
using csr_type = util::sparse_array<typename traits<BASE>::reg_t, 1ULL << 12, 12>;
|
2017-08-27 12:10:38 +02:00
|
|
|
using csr_page_type = typename csr_type::page_type;
|
|
|
|
mem_type mem;
|
|
|
|
csr_type csr;
|
|
|
|
std::stringstream uart_buf;
|
|
|
|
std::unordered_map<reg_t, uint64_t> ptw;
|
|
|
|
std::unordered_map<uint64_t, uint8_t> atomic_reservation;
|
|
|
|
std::unordered_map<unsigned, rd_csr_f> csr_rd_cb;
|
|
|
|
std::unordered_map<unsigned, wr_csr_f> csr_wr_cb;
|
2021-11-02 11:13:29 +01:00
|
|
|
uint8_t clic_cfg_reg{0};
|
|
|
|
std::array<uint32_t, 32> clic_inttrig_reg;
|
|
|
|
union clic_int_reg_t {
|
2023-10-29 17:06:56 +01:00
|
|
|
struct {
|
2021-11-02 11:13:29 +01:00
|
|
|
uint8_t ip;
|
|
|
|
uint8_t ie;
|
|
|
|
uint8_t attr;
|
|
|
|
uint8_t ctl;
|
|
|
|
};
|
|
|
|
uint32_t raw;
|
|
|
|
};
|
|
|
|
std::vector<clic_int_reg_t> clic_int_reg;
|
2023-03-17 09:09:09 +01:00
|
|
|
uint8_t clic_mprev_lvl{0};
|
|
|
|
uint8_t clic_mact_lvl{0};
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2022-03-27 15:38:18 +02:00
|
|
|
std::vector<uint8_t> tcm;
|
|
|
|
|
2024-08-02 08:59:22 +02:00
|
|
|
iss::status read_plain(unsigned addr, reg_t& val);
|
|
|
|
iss::status write_plain(unsigned addr, reg_t val);
|
2023-10-29 17:06:56 +01:00
|
|
|
iss::status read_null(unsigned addr, reg_t& val);
|
|
|
|
iss::status write_null(unsigned addr, reg_t val) { return iss::status::Ok; }
|
|
|
|
iss::status read_cycle(unsigned addr, reg_t& val);
|
2021-06-29 11:51:19 +02:00
|
|
|
iss::status write_cycle(unsigned addr, reg_t val);
|
2023-10-29 17:06:56 +01:00
|
|
|
iss::status read_instret(unsigned addr, reg_t& val);
|
2021-06-29 11:51:19 +02:00
|
|
|
iss::status write_instret(unsigned addr, reg_t val);
|
2023-10-29 17:06:56 +01:00
|
|
|
iss::status read_tvec(unsigned addr, reg_t& val);
|
|
|
|
iss::status read_time(unsigned addr, reg_t& val);
|
|
|
|
iss::status read_status(unsigned addr, reg_t& val);
|
2017-08-27 12:10:38 +02:00
|
|
|
iss::status write_status(unsigned addr, reg_t val);
|
2023-10-29 17:06:56 +01:00
|
|
|
iss::status read_cause(unsigned addr, reg_t& val);
|
2021-07-28 09:09:08 +02:00
|
|
|
iss::status write_cause(unsigned addr, reg_t val);
|
2023-10-29 17:06:56 +01:00
|
|
|
iss::status read_ie(unsigned addr, reg_t& val);
|
2017-08-27 12:10:38 +02:00
|
|
|
iss::status write_ie(unsigned addr, reg_t val);
|
2023-10-29 17:06:56 +01:00
|
|
|
iss::status read_ip(unsigned addr, reg_t& val);
|
|
|
|
iss::status read_hartid(unsigned addr, reg_t& val);
|
2021-08-01 17:23:22 +02:00
|
|
|
iss::status write_epc(unsigned addr, reg_t val);
|
2023-03-17 10:23:05 +01:00
|
|
|
iss::status read_intstatus(unsigned addr, reg_t& val);
|
2021-11-07 16:45:10 +01:00
|
|
|
iss::status write_intthresh(unsigned addr, reg_t val);
|
2023-03-11 14:03:03 +01:00
|
|
|
iss::status write_xtvt(unsigned addr, reg_t val);
|
2024-08-02 08:59:22 +02:00
|
|
|
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);
|
|
|
|
iss::status read_fcsr(unsigned addr, reg_t& val);
|
|
|
|
iss::status write_fcsr(unsigned addr, reg_t val);
|
2021-11-07 16:45:10 +01:00
|
|
|
|
2024-08-02 08:59:22 +02:00
|
|
|
virtual iss::status read_custom_csr(unsigned addr, reg_t& val) { return iss::status::Err; };
|
|
|
|
virtual iss::status write_custom_csr(unsigned addr, reg_t val) { return iss::status::Err; };
|
2022-03-25 11:33:44 +01:00
|
|
|
|
2024-08-02 08:59:22 +02:00
|
|
|
void register_custom_csr_rd(unsigned addr) { csr_rd_cb[addr] = &this_class::read_custom_csr; }
|
|
|
|
void register_custom_csr_wr(unsigned addr) { csr_wr_cb[addr] = &this_class::write_custom_csr; }
|
2022-03-25 11:33:44 +01:00
|
|
|
|
2021-03-11 16:12:28 +00:00
|
|
|
reg_t mhartid_reg{0x0};
|
2018-11-08 13:31:28 +01:00
|
|
|
|
2017-08-27 12:10:38 +02:00
|
|
|
void check_interrupt();
|
2021-11-02 11:13:29 +01:00
|
|
|
bool pmp_check(const access_type type, const uint64_t addr, const unsigned len);
|
2022-03-27 15:38:18 +02:00
|
|
|
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>);
|
2022-10-05 08:58:57 +02:00
|
|
|
feature_config cfg;
|
2023-11-30 08:49:02 +01:00
|
|
|
unsigned mcause_max_irq{(FEAT & features_e::FEAT_CLIC) ? std::max(16U, static_cast<unsigned>(traits<BASE>::CLIC_NUM_IRQ)) : 16U};
|
2023-10-29 17:06:56 +01:00
|
|
|
inline bool debug_mode_active() { return this->reg.PRIV & 0x4; }
|
2024-01-08 17:17:59 +01:00
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
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) {
|
2023-04-27 12:20:30 +02:00
|
|
|
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;
|
2017-08-27 12:10:38 +02:00
|
|
|
};
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
riscv_hart_m_p<BASE, FEAT, LOGCAT>::riscv_hart_m_p(feature_config cfg)
|
2018-11-08 13:31:28 +01:00
|
|
|
: state()
|
2022-10-05 08:58:57 +02:00
|
|
|
, instr_if(*this)
|
|
|
|
, cfg(cfg) {
|
2021-07-06 21:19:36 +02:00
|
|
|
// reset values
|
2021-04-09 11:20:51 +02:00
|
|
|
csr[misa] = traits<BASE>::MISA_VAL;
|
2021-07-06 21:19:36 +02:00
|
|
|
csr[mvendorid] = 0x669;
|
2021-09-30 19:27:03 +02:00
|
|
|
csr[marchid] = traits<BASE>::MARCHID_VAL;
|
2021-07-06 21:19:36 +02:00
|
|
|
csr[mimpid] = 1;
|
|
|
|
|
2017-08-27 12:10:38 +02:00
|
|
|
uart_buf.str("");
|
2024-08-02 08:59:22 +02:00
|
|
|
if(traits<BASE>::FLEN > 0)
|
|
|
|
csr_rd_cb[fcsr] = &this_class::read_fcsr;
|
|
|
|
csr_wr_cb[fcsr] = &this_class::write_fcsr;
|
2023-10-29 17:06:56 +01:00
|
|
|
for(unsigned addr = mhpmcounter3; addr <= mhpmcounter31; ++addr) {
|
2021-06-29 11:51:19 +02:00
|
|
|
csr_rd_cb[addr] = &this_class::read_null;
|
2024-08-02 08:59:22 +02:00
|
|
|
csr_wr_cb[addr] = &this_class::write_plain;
|
2021-06-29 11:51:19 +02:00
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
if(traits<BASE>::XLEN == 32)
|
|
|
|
for(unsigned addr = mhpmcounter3h; addr <= mhpmcounter31h; ++addr) {
|
|
|
|
csr_rd_cb[addr] = &this_class::read_null;
|
2024-08-02 08:59:22 +02:00
|
|
|
csr_wr_cb[addr] = &this_class::write_plain;
|
2023-10-29 17:06:56 +01:00
|
|
|
}
|
|
|
|
for(unsigned addr = mhpmevent3; addr <= mhpmevent31; ++addr) {
|
2021-06-29 11:51:19 +02:00
|
|
|
csr_rd_cb[addr] = &this_class::read_null;
|
2024-08-02 08:59:22 +02:00
|
|
|
csr_wr_cb[addr] = &this_class::write_plain;
|
2021-06-29 11:51:19 +02:00
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
for(unsigned addr = hpmcounter3; addr <= hpmcounter31; ++addr) {
|
2021-06-29 11:51:19 +02:00
|
|
|
csr_rd_cb[addr] = &this_class::read_null;
|
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
if(traits<BASE>::XLEN == 32)
|
|
|
|
for(unsigned addr = hpmcounter3h; addr <= hpmcounter31h; ++addr) {
|
|
|
|
csr_rd_cb[addr] = &this_class::read_null;
|
|
|
|
}
|
2021-03-07 10:51:00 +00:00
|
|
|
// common regs
|
2023-04-04 09:23:08 +02:00
|
|
|
const std::array<unsigned, 4> roaddrs{{misa, mvendorid, marchid, mimpid}};
|
2023-10-29 17:06:56 +01:00
|
|
|
for(auto addr : roaddrs) {
|
2024-08-02 08:59:22 +02:00
|
|
|
csr_rd_cb[addr] = &this_class::read_plain;
|
2023-04-04 09:23:08 +02:00
|
|
|
csr_wr_cb[addr] = &this_class::write_null;
|
|
|
|
}
|
|
|
|
const std::array<unsigned, 4> rwaddrs{{mepc, mtvec, mscratch, mtval}};
|
2023-10-29 17:06:56 +01:00
|
|
|
for(auto addr : rwaddrs) {
|
2024-08-02 08:59:22 +02:00
|
|
|
csr_rd_cb[addr] = &this_class::read_plain;
|
|
|
|
csr_wr_cb[addr] = &this_class::write_plain;
|
2021-03-07 10:51:00 +00:00
|
|
|
}
|
2021-06-29 11:51:19 +02:00
|
|
|
// special handling & overrides
|
|
|
|
csr_rd_cb[time] = &this_class::read_time;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(traits<BASE>::XLEN == 32)
|
|
|
|
csr_rd_cb[timeh] = &this_class::read_time;
|
2021-07-07 11:30:00 +02:00
|
|
|
csr_rd_cb[cycle] = &this_class::read_cycle;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(traits<BASE>::XLEN == 32)
|
|
|
|
csr_rd_cb[cycleh] = &this_class::read_cycle;
|
2021-07-07 11:30:00 +02:00
|
|
|
csr_rd_cb[instret] = &this_class::read_instret;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(traits<BASE>::XLEN == 32)
|
|
|
|
csr_rd_cb[instreth] = &this_class::read_instret;
|
2021-07-07 11:30:00 +02:00
|
|
|
|
2021-06-29 11:51:19 +02:00
|
|
|
csr_rd_cb[mcycle] = &this_class::read_cycle;
|
|
|
|
csr_wr_cb[mcycle] = &this_class::write_cycle;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(traits<BASE>::XLEN == 32)
|
|
|
|
csr_rd_cb[mcycleh] = &this_class::read_cycle;
|
|
|
|
if(traits<BASE>::XLEN == 32)
|
|
|
|
csr_wr_cb[mcycleh] = &this_class::write_cycle;
|
2021-06-29 11:51:19 +02:00
|
|
|
csr_rd_cb[minstret] = &this_class::read_instret;
|
|
|
|
csr_wr_cb[minstret] = &this_class::write_instret;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(traits<BASE>::XLEN == 32)
|
|
|
|
csr_rd_cb[minstreth] = &this_class::read_instret;
|
|
|
|
if(traits<BASE>::XLEN == 32)
|
|
|
|
csr_wr_cb[minstreth] = &this_class::write_instret;
|
2021-06-29 11:51:19 +02:00
|
|
|
csr_rd_cb[mstatus] = &this_class::read_status;
|
|
|
|
csr_wr_cb[mstatus] = &this_class::write_status;
|
2023-03-15 12:27:39 +01:00
|
|
|
csr_rd_cb[mcause] = &this_class::read_cause;
|
2021-07-28 09:09:08 +02:00
|
|
|
csr_wr_cb[mcause] = &this_class::write_cause;
|
2021-08-01 17:23:22 +02:00
|
|
|
csr_rd_cb[mtvec] = &this_class::read_tvec;
|
|
|
|
csr_wr_cb[mepc] = &this_class::write_epc;
|
2021-06-29 11:51:19 +02:00
|
|
|
csr_rd_cb[mip] = &this_class::read_ip;
|
2022-03-31 20:33:12 +02:00
|
|
|
csr_wr_cb[mip] = &this_class::write_null;
|
2021-06-29 11:51:19 +02:00
|
|
|
csr_rd_cb[mie] = &this_class::read_ie;
|
|
|
|
csr_wr_cb[mie] = &this_class::write_ie;
|
|
|
|
csr_rd_cb[mhartid] = &this_class::read_hartid;
|
|
|
|
csr_wr_cb[misa] = &this_class::write_null;
|
2021-07-06 21:19:36 +02:00
|
|
|
csr_wr_cb[mvendorid] = &this_class::write_null;
|
|
|
|
csr_wr_cb[marchid] = &this_class::write_null;
|
|
|
|
csr_wr_cb[mimpid] = &this_class::write_null;
|
2022-03-27 15:38:18 +02:00
|
|
|
if(FEAT & FEAT_CLIC) {
|
2024-08-02 08:59:22 +02:00
|
|
|
csr_rd_cb[mtvt] = &this_class::read_plain;
|
2023-03-11 14:03:03 +01:00
|
|
|
csr_wr_cb[mtvt] = &this_class::write_xtvt;
|
2023-10-29 17:06:56 +01:00
|
|
|
// csr_rd_cb[mxnti] = &this_class::read_csr_reg;
|
|
|
|
// csr_wr_cb[mxnti] = &this_class::write_csr_reg;
|
2023-03-17 10:23:05 +01:00
|
|
|
csr_rd_cb[mintstatus] = &this_class::read_intstatus;
|
2022-03-27 15:38:18 +02:00
|
|
|
csr_wr_cb[mintstatus] = &this_class::write_null;
|
2023-10-29 17:06:56 +01:00
|
|
|
// csr_rd_cb[mscratchcsw] = &this_class::read_csr_reg;
|
|
|
|
// csr_wr_cb[mscratchcsw] = &this_class::write_csr_reg;
|
|
|
|
// csr_rd_cb[mscratchcswl] = &this_class::read_csr_reg;
|
|
|
|
// csr_wr_cb[mscratchcswl] = &this_class::write_csr_reg;
|
2024-08-02 08:59:22 +02:00
|
|
|
csr_rd_cb[mintthresh] = &this_class::read_plain;
|
2022-03-27 15:38:18 +02:00
|
|
|
csr_wr_cb[mintthresh] = &this_class::write_intthresh;
|
2023-10-29 17:06:56 +01:00
|
|
|
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;
|
|
|
|
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); });
|
2022-03-27 15:38:18 +02:00
|
|
|
}
|
|
|
|
if(FEAT & FEAT_TCM) {
|
2022-10-05 08:58:57 +02:00
|
|
|
tcm.resize(cfg.tcm_size);
|
2023-10-29 17:06:56 +01:00
|
|
|
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;
|
2022-03-27 15:38:18 +02:00
|
|
|
std::copy(tcm.data() + offset, tcm.data() + offset + length, data);
|
|
|
|
return iss::Ok;
|
|
|
|
};
|
2023-10-29 17:06:56 +01:00
|
|
|
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;
|
2022-03-27 15:38:18 +02:00
|
|
|
std::copy(data, data + length, tcm.data() + offset);
|
|
|
|
return iss::Ok;
|
|
|
|
};
|
2022-10-05 08:58:57 +02:00
|
|
|
insert_mem_range(cfg.tcm_base, cfg.tcm_size, read_clic_cb, write_clic_cb);
|
2022-03-27 15:38:18 +02:00
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
if(FEAT & FEAT_DEBUG) {
|
2024-08-02 08:59:22 +02:00
|
|
|
csr_wr_cb[dscratch0] = &this_class::write_dscratch;
|
|
|
|
csr_rd_cb[dscratch0] = &this_class::read_debug;
|
|
|
|
csr_wr_cb[dscratch1] = &this_class::write_dscratch;
|
|
|
|
csr_rd_cb[dscratch1] = &this_class::read_debug;
|
|
|
|
csr_wr_cb[dpc] = &this_class::write_dpc;
|
|
|
|
csr_rd_cb[dpc] = &this_class::read_dpc;
|
|
|
|
csr_wr_cb[dcsr] = &this_class::write_dcsr;
|
|
|
|
csr_rd_cb[dcsr] = &this_class::read_debug;
|
2021-11-02 11:13:29 +01:00
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
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); };
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
std::pair<uint64_t, bool> riscv_hart_m_p<BASE, FEAT, LOGCAT>::load_file(std::string name, int type) {
|
2024-03-19 11:02:03 +01:00
|
|
|
get_sym_table(name);
|
|
|
|
try {
|
|
|
|
tohost = symbol_table.at("tohost");
|
|
|
|
fromhost = symbol_table.at("fromhost");
|
|
|
|
} catch(std::out_of_range& e) {
|
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
FILE* fp = fopen(name.c_str(), "r");
|
|
|
|
if(fp) {
|
2018-11-08 13:31:28 +01:00
|
|
|
std::array<char, 5> buf;
|
|
|
|
auto n = fread(buf.data(), 1, 4, fp);
|
2022-07-23 13:48:55 +02:00
|
|
|
fclose(fp);
|
2023-10-29 17:06:56 +01:00
|
|
|
if(n != 4)
|
|
|
|
throw std::runtime_error("input file has insufficient size");
|
2017-09-22 11:23:23 +02:00
|
|
|
buf[4] = 0;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(strcmp(buf.data() + 1, "ELF") == 0) {
|
2017-09-22 11:23:23 +02:00
|
|
|
// Create elfio reader
|
2017-08-27 12:10:38 +02:00
|
|
|
ELFIO::elfio reader;
|
|
|
|
// Load ELF data
|
2023-10-29 17:06:56 +01:00
|
|
|
if(!reader.load(name))
|
|
|
|
throw std::runtime_error("could not process elf file");
|
2017-08-27 12:10:38 +02:00
|
|
|
// check elf properties
|
2023-10-29 17:06:56 +01:00
|
|
|
if(reader.get_class() != ELFCLASS32)
|
|
|
|
if(sizeof(reg_t) == 4)
|
|
|
|
throw std::runtime_error("wrong elf class in file");
|
|
|
|
if(reader.get_type() != ET_EXEC)
|
|
|
|
throw std::runtime_error("wrong elf type in file");
|
|
|
|
if(reader.get_machine() != EM_RISCV)
|
|
|
|
throw std::runtime_error("wrong elf machine in file");
|
2021-08-19 10:49:33 +02:00
|
|
|
auto entry = reader.get_entry();
|
2023-10-29 17:06:56 +01:00
|
|
|
for(const auto pseg : reader.segments) {
|
2017-09-22 11:23:23 +02:00
|
|
|
const auto fsize = pseg->get_file_size(); // 0x42c/0x0
|
|
|
|
const auto seg_data = pseg->get_data();
|
2024-07-05 12:18:36 +02:00
|
|
|
const auto type = pseg->get_type();
|
2024-07-09 13:57:11 +02:00
|
|
|
if(type == 1 && fsize > 0) {
|
2023-10-29 17:06:56 +01:00
|
|
|
auto res = this->write(iss::address_type::PHYSICAL, iss::access_type::DEBUG_WRITE, traits<BASE>::MEM,
|
|
|
|
pseg->get_physical_address(), fsize, reinterpret_cast<const uint8_t* const>(seg_data));
|
|
|
|
if(res != iss::Ok)
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(ERR) << "problem writing " << fsize << "bytes to 0x" << std::hex << pseg->get_physical_address();
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-19 10:49:33 +02:00
|
|
|
for(const auto sec : reader.sections) {
|
2024-03-19 11:02:03 +01:00
|
|
|
if(sec->get_name() == ".tohost") {
|
2017-09-22 11:23:23 +02:00
|
|
|
tohost = sec->get_address();
|
|
|
|
fromhost = tohost + 0x40;
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
2021-08-19 10:49:33 +02:00
|
|
|
}
|
|
|
|
return std::make_pair(entry, true);
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
throw std::runtime_error(fmt::format("memory load file {} is not a valid elf file", name));
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
2023-06-05 15:17:16 +02:00
|
|
|
throw std::runtime_error(fmt::format("memory load file not found, check if {} is a valid file", name));
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
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,
|
2024-07-24 14:48:50 +02:00
|
|
|
std::function<mem_write_f> wr_fn) {
|
2022-03-27 15:38:18 +02:00
|
|
|
std::tuple<uint64_t, uint64_t> entry{base, size};
|
2023-10-29 17:06:56 +01:00
|
|
|
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); });
|
2022-03-27 15:38:18 +02:00
|
|
|
auto idx = std::distance(memfn_range.begin(), it);
|
|
|
|
memfn_range.insert(it, entry);
|
2023-10-29 17:06:56 +01:00
|
|
|
memfn_read.insert(std::begin(memfn_read) + idx, rd_f);
|
|
|
|
memfn_write.insert(std::begin(memfn_write) + idx, wr_fn);
|
2022-03-27 15:38:18 +02:00
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
2024-07-24 14:48:50 +02:00
|
|
|
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) {
|
2017-08-27 12:10:38 +02:00
|
|
|
#ifndef NDEBUG
|
2023-10-29 17:06:56 +01:00
|
|
|
if(access && iss::access_type::DEBUG) {
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(TRACEALL) << "debug read of " << length << " bytes @addr 0x" << std::hex << addr;
|
2023-10-29 17:06:56 +01:00
|
|
|
} else if(access && iss::access_type::FETCH) {
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(TRACEALL) << "fetch of " << length << " bytes @addr 0x" << std::hex << addr;
|
2017-08-27 12:10:38 +02:00
|
|
|
} else {
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(TRACE) << "read of " << length << " bytes @addr 0x" << std::hex << addr;
|
2023-10-29 17:06:56 +01:00
|
|
|
}
|
2017-08-27 12:10:38 +02:00
|
|
|
#endif
|
2017-11-18 00:42:33 +01:00
|
|
|
try {
|
2023-10-29 17:06:56 +01:00
|
|
|
switch(space) {
|
2017-11-18 00:42:33 +01:00
|
|
|
case traits<BASE>::MEM: {
|
2023-10-29 17:06:56 +01:00
|
|
|
auto alignment = is_fetch(access) ? (has_compressed() ? 2 : 4) : length;
|
|
|
|
if(unlikely(is_fetch(access) && (addr & (alignment - 1)))) {
|
2018-11-12 19:34:19 +01:00
|
|
|
fault_data = addr;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(is_debug(access))
|
|
|
|
throw trap_access(0, addr);
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.trap_state = (1UL << 31); // issue trap 0
|
2017-11-18 00:42:33 +01:00
|
|
|
return iss::Err;
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
2017-11-18 00:42:33 +01:00
|
|
|
try {
|
2023-10-29 17:06:56 +01:00
|
|
|
if(!is_debug(access) && (addr & (alignment - 1))) {
|
|
|
|
this->reg.trap_state = (1UL << 31) | 4 << 16;
|
|
|
|
fault_data = addr;
|
2021-07-06 21:19:36 +02:00
|
|
|
return iss::Err;
|
|
|
|
}
|
2023-08-04 13:08:10 +02:00
|
|
|
phys_addr_t phys_addr{access, space, addr};
|
2022-03-27 15:38:18 +02:00
|
|
|
auto res = iss::Err;
|
2024-08-02 08:59:22 +02:00
|
|
|
if(!is_fetch(access) && memfn_range.size()) {
|
2023-10-29 17:06:56 +01:00
|
|
|
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)) {
|
2022-03-27 15:38:18 +02:00
|
|
|
auto idx = std::distance(std::begin(memfn_range), it);
|
|
|
|
res = memfn_read[idx](phys_addr, length, data);
|
|
|
|
} else
|
2023-10-29 17:06:56 +01:00
|
|
|
res = hart_mem_rd_delegate(phys_addr, length, data);
|
2022-03-27 15:38:18 +02:00
|
|
|
} else {
|
2023-10-29 17:06:56 +01:00
|
|
|
res = hart_mem_rd_delegate(phys_addr, length, data);
|
2022-03-27 15:38:18 +02:00
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
if(unlikely(res != iss::Ok && (access & access_type::DEBUG) == 0)) {
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.trap_state = (1UL << 31) | (5 << 16); // issue trap 5 (load access fault
|
2023-10-29 17:06:56 +01:00
|
|
|
fault_data = addr;
|
2021-07-06 21:19:36 +02:00
|
|
|
}
|
2017-11-18 00:42:33 +01:00
|
|
|
return res;
|
2023-10-29 17:06:56 +01:00
|
|
|
} catch(trap_access& ta) {
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.trap_state = (1UL << 31) | ta.id;
|
2023-10-29 17:06:56 +01:00
|
|
|
fault_data = ta.addr;
|
2017-08-27 12:10:38 +02:00
|
|
|
return iss::Err;
|
|
|
|
}
|
2017-11-18 00:42:33 +01:00
|
|
|
} break;
|
|
|
|
case traits<BASE>::CSR: {
|
2023-10-29 17:06:56 +01:00
|
|
|
if(length != sizeof(reg_t))
|
|
|
|
return iss::Err;
|
|
|
|
return read_csr(addr, *reinterpret_cast<reg_t* const>(data));
|
2017-11-18 00:42:33 +01:00
|
|
|
} break;
|
|
|
|
case traits<BASE>::FENCE: {
|
2023-10-29 17:06:56 +01:00
|
|
|
if((addr + length) > mem.size())
|
|
|
|
return iss::Err;
|
2020-09-07 11:54:45 +02:00
|
|
|
return iss::Ok;
|
2017-11-18 00:42:33 +01:00
|
|
|
} break;
|
|
|
|
case traits<BASE>::RES: {
|
2018-11-12 19:34:19 +01:00
|
|
|
auto it = atomic_reservation.find(addr);
|
2023-10-29 17:06:56 +01:00
|
|
|
if(it != atomic_reservation.end() && it->second != 0) {
|
2017-11-18 00:42:33 +01:00
|
|
|
memset(data, 0xff, length);
|
2018-11-12 19:34:19 +01:00
|
|
|
atomic_reservation.erase(addr);
|
2017-11-18 00:42:33 +01:00
|
|
|
} else
|
|
|
|
memset(data, 0, length);
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
return iss::Err; // assert("Not supported");
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
2017-11-18 00:42:33 +01:00
|
|
|
return iss::Ok;
|
2023-10-29 17:06:56 +01:00
|
|
|
} catch(trap_access& ta) {
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.trap_state = (1UL << 31) | ta.id;
|
2023-10-29 17:06:56 +01:00
|
|
|
fault_data = ta.addr;
|
2017-11-18 00:42:33 +01:00
|
|
|
return iss::Err;
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
2024-07-24 14:48:50 +02:00
|
|
|
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) {
|
2017-08-27 12:10:38 +02:00
|
|
|
#ifndef NDEBUG
|
2023-10-29 17:06:56 +01:00
|
|
|
const char* prefix = (access && iss::access_type::DEBUG) ? "debug " : "";
|
|
|
|
switch(length) {
|
2017-08-27 12:10:38 +02:00
|
|
|
case 8:
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(TRACE) << prefix << "write of " << length << " bytes (0x" << std::hex << *(uint64_t*)&data[0] << std::dec << ") @addr 0x"
|
2024-03-19 11:47:12 +01:00
|
|
|
<< std::hex << addr;
|
2017-08-27 12:10:38 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(TRACE) << prefix << "write of " << length << " bytes (0x" << std::hex << *(uint32_t*)&data[0] << std::dec << ") @addr 0x"
|
2024-03-19 11:47:12 +01:00
|
|
|
<< std::hex << addr;
|
2017-08-27 12:10:38 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(TRACE) << prefix << "write of " << length << " bytes (0x" << std::hex << *(uint16_t*)&data[0] << std::dec << ") @addr 0x"
|
2024-03-19 11:47:12 +01:00
|
|
|
<< std::hex << addr;
|
2017-08-27 12:10:38 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(TRACE) << prefix << "write of " << length << " bytes (0x" << std::hex << (uint16_t)data[0] << std::dec << ") @addr 0x"
|
2024-03-19 11:47:12 +01:00
|
|
|
<< std::hex << addr;
|
2017-08-27 12:10:38 +02:00
|
|
|
break;
|
|
|
|
default:
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(TRACE) << prefix << "write of " << length << " bytes @addr " << addr;
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
try {
|
2023-10-29 17:06:56 +01:00
|
|
|
switch(space) {
|
2017-09-22 11:23:23 +02:00
|
|
|
case traits<BASE>::MEM: {
|
2023-10-29 17:06:56 +01:00
|
|
|
if(unlikely((access && iss::access_type::FETCH) && (addr & 0x1) == 1)) {
|
2018-11-12 19:34:19 +01:00
|
|
|
fault_data = addr;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(access && iss::access_type::DEBUG)
|
|
|
|
throw trap_access(0, addr);
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.trap_state = (1UL << 31); // issue trap 0
|
2017-10-04 10:31:11 +02:00
|
|
|
return iss::Err;
|
|
|
|
}
|
|
|
|
try {
|
2023-10-29 17:06:56 +01:00
|
|
|
if(length > 1 && (addr & (length - 1)) && (access & access_type::DEBUG) != access_type::DEBUG) {
|
|
|
|
this->reg.trap_state = (1UL << 31) | 6 << 16;
|
|
|
|
fault_data = addr;
|
2021-07-06 21:19:36 +02:00
|
|
|
return iss::Err;
|
|
|
|
}
|
2023-08-04 13:08:10 +02:00
|
|
|
phys_addr_t phys_addr{access, space, addr};
|
2022-03-27 15:38:18 +02:00
|
|
|
auto res = iss::Err;
|
2024-08-02 08:59:22 +02:00
|
|
|
if(!is_fetch(access) && memfn_range.size()) {
|
2023-10-29 17:06:56 +01:00
|
|
|
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)) {
|
2022-03-27 15:38:18 +02:00
|
|
|
auto idx = std::distance(std::begin(memfn_range), it);
|
2023-10-29 17:06:56 +01:00
|
|
|
res = memfn_write[idx](phys_addr, length, data);
|
2022-03-27 15:38:18 +02:00
|
|
|
} else
|
2023-10-29 17:06:56 +01:00
|
|
|
res = write_mem(phys_addr, length, data);
|
2022-03-27 15:38:18 +02:00
|
|
|
} else {
|
2023-10-29 17:06:56 +01:00
|
|
|
res = write_mem(phys_addr, length, data);
|
2022-03-27 15:38:18 +02:00
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
if(unlikely(res != iss::Ok && (access & access_type::DEBUG) == 0)) {
|
2024-01-08 17:17:59 +01:00
|
|
|
this->reg.trap_state = (1UL << 31) | (7UL << 16); // issue trap 7 (Store/AMO access fault)
|
2023-10-29 17:06:56 +01:00
|
|
|
fault_data = addr;
|
2021-07-06 21:19:36 +02:00
|
|
|
}
|
2017-10-04 10:31:11 +02:00
|
|
|
return res;
|
2023-10-29 17:06:56 +01:00
|
|
|
} catch(trap_access& ta) {
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.trap_state = (1UL << 31) | ta.id;
|
2023-10-29 17:06:56 +01:00
|
|
|
fault_data = ta.addr;
|
2017-10-04 10:31:11 +02:00
|
|
|
return iss::Err;
|
|
|
|
}
|
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
if((addr + length) > mem.size())
|
|
|
|
return iss::Err;
|
|
|
|
switch(addr) {
|
2017-08-27 12:10:38 +02:00
|
|
|
case 0x10013000: // UART0 base, TXFIFO reg
|
|
|
|
case 0x10023000: // UART1 base, TXFIFO reg
|
2017-09-22 11:23:23 +02:00
|
|
|
uart_buf << (char)data[0];
|
2023-10-29 17:06:56 +01:00
|
|
|
if(((char)data[0]) == '\n' || data[0] == 0) {
|
2017-09-22 11:23:23 +02:00
|
|
|
std::cout << uart_buf.str();
|
2017-08-27 12:10:38 +02:00
|
|
|
uart_buf.str("");
|
|
|
|
}
|
|
|
|
return iss::Ok;
|
2017-09-22 11:23:23 +02:00
|
|
|
case 0x10008000: { // HFROSC base, hfrosccfg reg
|
2023-10-29 17:06:56 +01:00
|
|
|
auto& p = mem(addr / mem.page_size);
|
2023-08-04 13:08:10 +02:00
|
|
|
auto offs = addr & mem.page_addr_mask;
|
2017-09-22 11:23:23 +02:00
|
|
|
std::copy(data, data + length, p.data() + offs);
|
2023-10-29 17:06:56 +01:00
|
|
|
auto& x = *(p.data() + offs + 3);
|
|
|
|
if(x & 0x40)
|
|
|
|
x |= 0x80; // hfroscrdy = 1 if hfroscen==1
|
2017-08-27 12:10:38 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
2017-09-22 11:23:23 +02:00
|
|
|
case 0x10008008: { // HFROSC base, pllcfg reg
|
2023-10-29 17:06:56 +01:00
|
|
|
auto& p = mem(addr / mem.page_size);
|
2023-08-04 13:08:10 +02:00
|
|
|
auto offs = addr & mem.page_addr_mask;
|
2017-09-22 11:23:23 +02:00
|
|
|
std::copy(data, data + length, p.data() + offs);
|
2023-10-29 17:06:56 +01:00
|
|
|
auto& x = *(p.data() + offs + 3);
|
2017-09-22 11:23:23 +02:00
|
|
|
x |= 0x80; // set pll lock upon writing
|
2017-08-27 12:10:38 +02:00
|
|
|
return iss::Ok;
|
2017-09-22 11:23:23 +02:00
|
|
|
} break;
|
2023-12-01 14:50:54 +01:00
|
|
|
default: {
|
|
|
|
}
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
2017-09-22 11:23:23 +02:00
|
|
|
} break;
|
|
|
|
case traits<BASE>::CSR: {
|
2023-10-29 17:06:56 +01:00
|
|
|
if(length != sizeof(reg_t))
|
|
|
|
return iss::Err;
|
|
|
|
return write_csr(addr, *reinterpret_cast<const reg_t*>(data));
|
2017-09-22 11:23:23 +02:00
|
|
|
} break;
|
|
|
|
case traits<BASE>::FENCE: {
|
2023-10-29 17:06:56 +01:00
|
|
|
if((addr + length) > mem.size())
|
|
|
|
return iss::Err;
|
|
|
|
switch(addr) {
|
2017-08-27 12:10:38 +02:00
|
|
|
case 2:
|
2017-09-22 11:23:23 +02:00
|
|
|
case 3: {
|
2017-08-27 12:10:38 +02:00
|
|
|
ptw.clear();
|
2017-10-04 23:10:29 +02:00
|
|
|
auto tvm = state.mstatus.TVM;
|
2017-08-27 12:10:38 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 11:23:23 +02:00
|
|
|
} break;
|
|
|
|
case traits<BASE>::RES: {
|
2018-11-12 19:34:19 +01:00
|
|
|
atomic_reservation[addr] = data[0];
|
2017-09-22 11:23:23 +02:00
|
|
|
} break;
|
2017-08-27 12:10:38 +02:00
|
|
|
default:
|
|
|
|
return iss::Err;
|
|
|
|
}
|
|
|
|
return iss::Ok;
|
2023-10-29 17:06:56 +01:00
|
|
|
} catch(trap_access& ta) {
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.trap_state = (1UL << 31) | ta.id;
|
2023-10-29 17:06:56 +01:00
|
|
|
fault_data = ta.addr;
|
2017-08-27 12:10:38 +02:00
|
|
|
return iss::Err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_csr(unsigned addr, reg_t& val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
if(addr >= csr.size())
|
|
|
|
return iss::Err;
|
2020-05-31 16:41:04 +02:00
|
|
|
auto req_priv_lvl = (addr >> 8) & 0x3;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(this->reg.PRIV < req_priv_lvl) // not having required privileges
|
2021-03-07 10:51:00 +00:00
|
|
|
throw illegal_instruction_fault(this->fault_data);
|
2017-08-27 12:10:38 +02:00
|
|
|
auto it = csr_rd_cb.find(addr);
|
2023-10-29 17:06:56 +01:00
|
|
|
if(it == csr_rd_cb.end() || !it->second) // non existent register
|
2021-03-07 10:51:00 +00:00
|
|
|
throw illegal_instruction_fault(this->fault_data);
|
|
|
|
return (this->*(it->second))(addr, val);
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_csr(unsigned addr, reg_t val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
if(addr >= csr.size())
|
|
|
|
return iss::Err;
|
2020-05-31 16:41:04 +02:00
|
|
|
auto req_priv_lvl = (addr >> 8) & 0x3;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(this->reg.PRIV < req_priv_lvl) // not having required privileges
|
2020-05-31 16:41:04 +02:00
|
|
|
throw illegal_instruction_fault(this->fault_data);
|
2023-10-29 17:06:56 +01:00
|
|
|
if((addr & 0xc00) == 0xc00) // writing to read-only region
|
2020-05-31 16:41:04 +02:00
|
|
|
throw illegal_instruction_fault(this->fault_data);
|
2017-08-27 12:10:38 +02:00
|
|
|
auto it = csr_wr_cb.find(addr);
|
2023-10-29 17:06:56 +01:00
|
|
|
if(it == csr_wr_cb.end() || !it->second) // non existent register
|
2021-03-07 10:51:00 +00:00
|
|
|
throw illegal_instruction_fault(this->fault_data);
|
|
|
|
return (this->*(it->second))(addr, val);
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
2024-08-02 08:59:22 +02:00
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_null(unsigned addr, reg_t& val) {
|
|
|
|
val = 0;
|
2021-03-07 10:51:00 +00:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
2024-08-02 08:59:22 +02:00
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_plain(unsigned addr, reg_t& val) {
|
|
|
|
val = csr[addr];
|
2021-06-29 11:51:19 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
2024-08-02 08:59:22 +02:00
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_plain(unsigned addr, reg_t val) {
|
2021-03-07 10:51:00 +00:00
|
|
|
csr[addr] = val;
|
|
|
|
return iss::Ok;
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_cycle(unsigned addr, reg_t& val) {
|
2023-05-27 10:20:49 +02:00
|
|
|
auto cycle_val = this->reg.icount + cycle_offset;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(addr == mcycle) {
|
2017-12-31 11:27:51 +01:00
|
|
|
val = static_cast<reg_t>(cycle_val);
|
2023-10-29 17:06:56 +01:00
|
|
|
} else if(addr == mcycleh) {
|
2017-12-31 11:27:51 +01:00
|
|
|
val = static_cast<reg_t>(cycle_val >> 32);
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_cycle(unsigned addr, reg_t val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
if(sizeof(typename traits<BASE>::reg_t) != 4) {
|
2021-06-29 11:51:19 +02:00
|
|
|
mcycle_csr = static_cast<uint64_t>(val);
|
|
|
|
} else {
|
2023-10-29 17:06:56 +01:00
|
|
|
if(addr == mcycle) {
|
2021-06-29 11:51:19 +02:00
|
|
|
mcycle_csr = (mcycle_csr & 0xffffffff00000000) + val;
|
2023-10-29 17:06:56 +01:00
|
|
|
} else {
|
|
|
|
mcycle_csr = (static_cast<uint64_t>(val) << 32) + (mcycle_csr & 0xffffffff);
|
2021-06-29 11:51:19 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
cycle_offset = mcycle_csr - this->reg.icount; // TODO: relying on wrap-around
|
2021-06-29 11:51:19 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_instret(unsigned addr, reg_t& val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
if((addr & 0xff) == (minstret & 0xff)) {
|
2023-05-27 10:20:49 +02:00
|
|
|
val = static_cast<reg_t>(this->reg.instret);
|
2023-10-29 17:06:56 +01:00
|
|
|
} else if((addr & 0xff) == (minstreth & 0xff)) {
|
2023-05-27 10:20:49 +02:00
|
|
|
val = static_cast<reg_t>(this->reg.instret >> 32);
|
2021-06-29 11:51:19 +02:00
|
|
|
}
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_instret(unsigned addr, reg_t val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
if(sizeof(typename traits<BASE>::reg_t) != 4) {
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.instret = static_cast<uint64_t>(val);
|
2021-06-29 11:51:19 +02:00
|
|
|
} else {
|
2023-10-29 17:06:56 +01:00
|
|
|
if((addr & 0xff) == (minstret & 0xff)) {
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.instret = (this->reg.instret & 0xffffffff00000000) + val;
|
2023-10-29 17:06:56 +01:00
|
|
|
} else {
|
|
|
|
this->reg.instret = (static_cast<uint64_t>(val) << 32) + (this->reg.instret & 0xffffffff);
|
2021-06-29 11:51:19 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.instret--;
|
2021-06-29 11:51:19 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_time(unsigned addr, reg_t& val) {
|
2023-05-27 10:20:49 +02:00
|
|
|
uint64_t time_val = this->reg.icount / (100000000 / 32768 - 1); //-> ~3052;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(addr == time) {
|
2017-10-25 22:05:31 +02:00
|
|
|
val = static_cast<reg_t>(time_val);
|
2023-10-29 17:06:56 +01:00
|
|
|
} else if(addr == timeh) {
|
|
|
|
if(sizeof(typename traits<BASE>::reg_t) != 4)
|
|
|
|
return iss::Err;
|
2017-10-25 22:05:31 +02:00
|
|
|
val = static_cast<reg_t>(time_val >> 32);
|
|
|
|
}
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_tvec(unsigned addr, reg_t& val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
val = FEAT & features_e::FEAT_CLIC ? csr[addr] : csr[addr] & ~2;
|
2021-06-29 11:51:19 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_status(unsigned addr, reg_t& val) {
|
2021-05-16 15:06:42 +02:00
|
|
|
val = state.mstatus & hart_state_type::get_mask();
|
2017-08-27 12:10:38 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_status(unsigned addr, reg_t val) {
|
2020-09-07 11:54:45 +02:00
|
|
|
state.write_mstatus(val);
|
2017-08-27 12:10:38 +02:00
|
|
|
check_interrupt();
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_cause(unsigned addr, reg_t& val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
if((FEAT & features_e::FEAT_CLIC) && (csr[mtvec] & 0x3) == 3) {
|
|
|
|
val = 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;
|
2023-03-17 09:09:09 +01:00
|
|
|
} else
|
2023-10-29 17:06:56 +01:00
|
|
|
val = csr[addr] & ((1UL << (traits<BASE>::XLEN - 1)) | (mcause_max_irq - 1));
|
2023-03-15 12:27:39 +01:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_cause(unsigned addr, reg_t val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
if((FEAT & features_e::FEAT_CLIC) && (csr[mtvec] & 0x3) == 3) {
|
|
|
|
auto mask = ((1UL << (traits<BASE>::XLEN - 1)) | (mcause_max_irq - 1) | (0xfUL << 16));
|
2023-03-17 09:09:09 +01:00
|
|
|
csr[addr] = (val & mask) | (csr[addr] & ~mask);
|
2023-10-29 17:06:56 +01:00
|
|
|
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;
|
2023-03-17 09:09:09 +01:00
|
|
|
} else {
|
2023-10-29 17:06:56 +01:00
|
|
|
auto mask = ((1UL << (traits<BASE>::XLEN - 1)) | (mcause_max_irq - 1));
|
2023-03-17 09:09:09 +01:00
|
|
|
csr[addr] = (val & mask) | (csr[addr] & ~mask);
|
2023-03-15 12:27:39 +01:00
|
|
|
}
|
2021-07-28 09:09:08 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_hartid(unsigned addr, reg_t& val) {
|
2021-11-09 19:47:34 +01:00
|
|
|
val = mhartid_reg;
|
2017-08-27 12:10:38 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_ie(unsigned addr, reg_t& val) {
|
2021-11-09 19:47:34 +01:00
|
|
|
auto mask = get_irq_mask();
|
|
|
|
val = csr[mie] & mask;
|
2020-09-04 15:37:21 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_ie(unsigned addr, reg_t val) {
|
2020-09-07 11:54:45 +02:00
|
|
|
auto mask = get_irq_mask();
|
2017-08-27 12:10:38 +02:00
|
|
|
csr[mie] = (csr[mie] & ~mask) | (val & mask);
|
|
|
|
check_interrupt();
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_ip(unsigned addr, reg_t& val) {
|
2021-11-09 19:47:34 +01:00
|
|
|
auto mask = get_irq_mask();
|
|
|
|
val = csr[mip] & mask;
|
2017-08-27 12:10:38 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_epc(unsigned addr, reg_t val) {
|
2021-07-09 13:01:22 +02:00
|
|
|
csr[addr] = val & get_pc_mask();
|
2021-07-07 11:30:00 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
2024-08-02 08:59:22 +02:00
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_dcsr(unsigned addr, reg_t val) {
|
2021-11-07 17:48:44 +01:00
|
|
|
if(!debug_mode_active())
|
2021-11-07 16:45:10 +01:00
|
|
|
throw illegal_instruction_fault(this->fault_data);
|
2021-11-02 11:13:29 +01:00
|
|
|
// +-------------- ebreakm
|
|
|
|
// | +---------- stepi
|
|
|
|
// | | +++----- cause
|
|
|
|
// | | ||| +- step
|
|
|
|
csr[addr] = val & 0b1000100111000100U;
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
2024-08-02 08:59:22 +02:00
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_debug(unsigned addr, reg_t& val) {
|
2021-11-07 17:48:44 +01:00
|
|
|
if(!debug_mode_active())
|
2021-11-07 16:45:10 +01:00
|
|
|
throw illegal_instruction_fault(this->fault_data);
|
|
|
|
val = csr[addr];
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
2024-08-02 08:59:22 +02:00
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_dscratch(unsigned addr, reg_t val) {
|
2021-11-07 17:48:44 +01:00
|
|
|
if(!debug_mode_active())
|
2021-11-07 16:45:10 +01:00
|
|
|
throw illegal_instruction_fault(this->fault_data);
|
|
|
|
csr[addr] = val;
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
2024-08-02 08:59:22 +02:00
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_dpc(unsigned addr, reg_t& val) {
|
2021-11-07 17:48:44 +01:00
|
|
|
if(!debug_mode_active())
|
|
|
|
throw illegal_instruction_fault(this->fault_data);
|
|
|
|
val = this->reg.DPC;
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
2024-08-02 08:59:22 +02:00
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_dpc(unsigned addr, reg_t val) {
|
2021-11-07 17:48:44 +01:00
|
|
|
if(!debug_mode_active())
|
|
|
|
throw illegal_instruction_fault(this->fault_data);
|
|
|
|
this->reg.DPC = val;
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_intstatus(unsigned addr, reg_t& val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
val = (clic_mact_lvl & 0xff) << 24;
|
2023-03-17 10:23:05 +01:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-08-02 08:59:22 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_fcsr(unsigned addr, reg_t& val) {
|
|
|
|
val = this->get_fcsr();
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_fcsr(unsigned addr, reg_t val) {
|
|
|
|
this->set_fcsr(val);
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_intthresh(unsigned addr, reg_t val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
csr[addr] = (val & 0xff) | (1 << (cfg.clic_int_ctl_bits)) - 1;
|
2022-03-27 15:38:18 +02:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT>
|
|
|
|
iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_xtvt(unsigned addr, reg_t val) {
|
2023-10-29 17:06:56 +01:00
|
|
|
csr[addr] = val & ~0x3fULL;
|
2023-03-11 14:03:03 +01:00
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
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) {
|
2023-10-29 17:06:56 +01:00
|
|
|
switch(paddr.val) {
|
2017-10-04 10:31:11 +02:00
|
|
|
default: {
|
2023-10-29 17:06:56 +01:00
|
|
|
for(auto offs = 0U; offs < length; ++offs) {
|
|
|
|
*(data + offs) = mem[(paddr.val + offs) % mem.size()];
|
2021-06-29 11:51:19 +02:00
|
|
|
}
|
2017-10-04 10:31:11 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-04 14:30:25 +02:00
|
|
|
return iss::Ok;
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
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) {
|
2023-10-29 17:06:56 +01:00
|
|
|
switch(paddr.val) {
|
2024-01-08 17:17:59 +01:00
|
|
|
// TODO remove UART, Peripherals should not be part of the ISS
|
2023-05-27 10:20:49 +02:00
|
|
|
case 0xFFFF0000: // UART0 base, TXFIFO reg
|
2023-10-29 17:06:56 +01:00
|
|
|
if(((char)data[0]) == '\n' || data[0] == 0) {
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(INFO) << "UART" << ((paddr.val >> 12) & 0x3) << " send '" << uart_buf.str() << "'";
|
2017-10-04 10:31:11 +02:00
|
|
|
uart_buf.str("");
|
2023-05-27 10:20:49 +02:00
|
|
|
} else if(((char)data[0]) != '\r')
|
|
|
|
uart_buf << (char)data[0];
|
2017-10-04 14:30:25 +02:00
|
|
|
break;
|
2017-10-04 10:31:11 +02:00
|
|
|
default: {
|
2023-10-29 17:06:56 +01:00
|
|
|
mem_type::page_type& p = mem(paddr.val / mem.page_size);
|
2017-10-04 10:31:11 +02:00
|
|
|
std::copy(data, data + length, p.data() + (paddr.val & mem.page_addr_mask));
|
|
|
|
// tohost handling in case of riscv-test
|
2023-10-29 17:06:56 +01:00
|
|
|
if(paddr.access && iss::access_type::FUNC) {
|
|
|
|
auto tohost_upper =
|
|
|
|
(traits<BASE>::XLEN == 32 && paddr.val == (tohost + 4)) || (traits<BASE>::XLEN == 64 && paddr.val == tohost);
|
|
|
|
auto tohost_lower = (traits<BASE>::XLEN == 32 && paddr.val == tohost) || (traits<BASE>::XLEN == 64 && paddr.val == tohost);
|
|
|
|
if(tohost_lower || tohost_upper) {
|
|
|
|
uint64_t hostvar = *reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask));
|
2024-01-08 17:17:59 +01:00
|
|
|
// in case of 32 bit system, two writes to tohost are needed, only evaluate on the second (high) write
|
|
|
|
if(tohost_upper && (tohost_lower || tohost_lower_written)) {
|
2023-10-29 17:06:56 +01:00
|
|
|
switch(hostvar >> 48) {
|
2017-10-04 10:31:11 +02:00
|
|
|
case 0:
|
2023-10-29 17:06:56 +01:00
|
|
|
if(hostvar != 0x1) {
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(FATAL) << "tohost value is 0x" << std::hex << hostvar << std::dec << " (" << hostvar
|
2024-03-19 11:47:12 +01:00
|
|
|
<< "), stopping simulation";
|
2018-11-08 13:31:28 +01:00
|
|
|
} else {
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(INFO) << "tohost value is 0x" << std::hex << hostvar << std::dec << " (" << hostvar
|
2024-03-19 11:47:12 +01:00
|
|
|
<< "), stopping simulation";
|
2017-11-23 14:48:18 +01:00
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
this->reg.trap_state = std::numeric_limits<uint32_t>::max();
|
|
|
|
this->interrupt_sim = hostvar;
|
2020-04-17 19:23:43 +02:00
|
|
|
break;
|
2017-10-04 10:31:11 +02:00
|
|
|
case 0x0101: {
|
|
|
|
char c = static_cast<char>(hostvar & 0xff);
|
2023-10-29 17:06:56 +01:00
|
|
|
if(c == '\n' || c == 0) {
|
2024-03-14 09:43:08 +01:00
|
|
|
CPPLOG(INFO) << "tohost send '" << uart_buf.str() << "'";
|
2017-10-04 10:31:11 +02:00
|
|
|
uart_buf.str("");
|
|
|
|
} else
|
|
|
|
uart_buf << c;
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2024-01-08 17:17:59 +01:00
|
|
|
tohost_lower_written = false;
|
2023-10-29 17:06:56 +01:00
|
|
|
} else if(tohost_lower)
|
2024-01-08 17:17:59 +01:00
|
|
|
tohost_lower_written = true;
|
2023-10-29 17:06:56 +01:00
|
|
|
} else if((traits<BASE>::XLEN == 32 && paddr.val == fromhost + 4) || (traits<BASE>::XLEN == 64 && paddr.val == fromhost)) {
|
|
|
|
uint64_t fhostvar = *reinterpret_cast<uint64_t*>(p.data() + (fromhost & mem.page_addr_mask));
|
|
|
|
*reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask)) = fhostvar;
|
2017-10-04 10:31:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
2017-10-04 14:30:25 +02:00
|
|
|
return iss::Ok;
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
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) {
|
2023-10-29 17:06:56 +01:00
|
|
|
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;
|
2022-03-27 15:38:18 +02:00
|
|
|
read_reg_uint32(addr, clic_inttrig_reg[offset], data, length);
|
2023-10-29 17:06:56 +01:00
|
|
|
} 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;
|
2022-03-27 15:38:18 +02:00
|
|
|
read_reg_uint32(addr, clic_int_reg[offset].raw, data, length);
|
|
|
|
} else {
|
2023-10-29 17:06:56 +01:00
|
|
|
for(auto i = 0U; i < length; ++i)
|
|
|
|
*(data + i) = 0;
|
2022-03-27 15:38:18 +02:00
|
|
|
}
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
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) {
|
2023-10-29 17:06:56 +01:00
|
|
|
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;
|
2022-03-27 15:38:18 +02:00
|
|
|
write_reg_uint32(addr, clic_inttrig_reg[offset], data, length);
|
2023-10-29 17:06:56 +01:00
|
|
|
} 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;
|
2022-03-27 15:38:18 +02:00
|
|
|
write_reg_uint32(addr, clic_int_reg[offset].raw, data, length);
|
2022-10-05 08:58:57 +02:00
|
|
|
clic_int_reg[offset].raw &= 0xf0c70101; // clicIntCtlBits->0xf0, clicintattr->0xc7, clicintie->0x1, clicintip->0x1
|
2022-03-27 15:38:18 +02:00
|
|
|
}
|
|
|
|
return iss::Ok;
|
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT> inline void riscv_hart_m_p<BASE, FEAT, LOGCAT>::reset(uint64_t address) {
|
2017-11-18 00:42:33 +01:00
|
|
|
BASE::reset(address);
|
2021-05-16 15:06:42 +02:00
|
|
|
state.mstatus = hart_state_type::mstatus_reset_val;
|
2017-11-18 00:42:33 +01:00
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT> void riscv_hart_m_p<BASE, FEAT, LOGCAT>::check_interrupt() {
|
2023-10-29 17:06:56 +01:00
|
|
|
// TODO: Implement CLIC functionality
|
|
|
|
// auto ideleg = csr[mideleg];
|
2017-09-22 11:23:23 +02:00
|
|
|
// 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.
|
2020-09-07 13:29:45 +02:00
|
|
|
auto ena_irq = csr[mip] & csr[mie];
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2022-10-05 08:58:57 +02:00
|
|
|
bool mstatus_mie = state.mstatus.MIE;
|
|
|
|
auto m_enabled = this->reg.PRIV < PRIV_M || mstatus_mie;
|
2021-07-07 11:30:00 +02:00
|
|
|
auto enabled_interrupts = m_enabled ? ena_irq : 0;
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2023-10-29 17:06:56 +01:00
|
|
|
if(enabled_interrupts != 0) {
|
2017-08-27 12:10:38 +02:00
|
|
|
int res = 0;
|
2023-10-29 17:06:56 +01:00
|
|
|
while((enabled_interrupts & 1) == 0) {
|
|
|
|
enabled_interrupts >>= 1;
|
|
|
|
res++;
|
2021-04-07 17:42:08 +02:00
|
|
|
}
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.pending_trap = res << 16 | 1; // 0x80 << 24 | (cause << 16) | trap_id
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:48:50 +02:00
|
|
|
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) {
|
2017-10-04 23:10:29 +02:00
|
|
|
// flags are ACTIVE[31:31], CAUSE[30:16], TRAPID[15:0]
|
2017-08-27 12:10:38 +02:00
|
|
|
// calculate and write mcause val
|
2023-03-10 20:40:21 +01:00
|
|
|
auto const trap_id = bit_sub<0, 16>(flags);
|
2017-10-04 23:10:29 +02:00
|
|
|
auto cause = bit_sub<16, 15>(flags);
|
2017-08-27 12:10:38 +02:00
|
|
|
// calculate effective privilege level
|
2022-04-13 11:41:01 +02:00
|
|
|
unsigned new_priv = PRIV_M;
|
2023-10-29 17:06:56 +01:00
|
|
|
if(trap_id == 0) { // exception
|
|
|
|
if(cause == 11)
|
|
|
|
cause = 0x8 + PRIV_M; // adjust environment call cause
|
2017-08-27 12:10:38 +02:00
|
|
|
// store ret addr in xepc register
|
2021-07-09 13:01:22 +02:00
|
|
|
csr[mepc] = static_cast<reg_t>(addr) & get_pc_mask(); // store actual address instruction of exception
|
2021-11-02 15:10:20 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2023-10-29 17:06:56 +01:00
|
|
|
switch(cause) {
|
2021-09-23 21:09:36 +02:00
|
|
|
case 0:
|
2024-07-16 15:32:35 +02:00
|
|
|
csr[mtval] = static_cast<reg_t>(tval);
|
2021-09-23 21:09:36 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2024-07-16 15:32:35 +02:00
|
|
|
csr[mtval] = (!has_compressed() || (tval & 0x3) == 3) ? tval : tval & 0xffff;
|
2021-09-23 21:09:36 +02:00
|
|
|
break;
|
2021-11-02 11:13:29 +01:00
|
|
|
case 3:
|
2022-04-13 11:41:01 +02:00
|
|
|
if((FEAT & FEAT_DEBUG) && (csr[dcsr] & 0x8000)) {
|
|
|
|
this->reg.DPC = addr;
|
2023-10-29 17:06:56 +01:00
|
|
|
csr[dcsr] = (csr[dcsr] & ~0x1c3) | (1 << 6) | PRIV_M; // FIXME: cause should not be 4 (stepi)
|
2022-04-13 11:41:01 +02:00
|
|
|
new_priv = this->reg.PRIV | PRIV_D;
|
|
|
|
} else {
|
|
|
|
csr[mtval] = addr;
|
|
|
|
}
|
2024-01-08 17:17:59 +01:00
|
|
|
if(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
|
2024-07-24 12:20:38 +02:00
|
|
|
NSCLOG(INFO, LOGCAT) << "Semihosting call at address " << buffer.data() << " occurred ";
|
2024-01-08 17:17:59 +01:00
|
|
|
|
2024-05-31 07:27:47 +02:00
|
|
|
semihosting_cb(this, &(this->reg.X10) /*a0*/, &(this->reg.X11) /*a1*/);
|
2024-01-08 17:17:59 +01:00
|
|
|
return this->reg.NEXT_PC;
|
|
|
|
}
|
|
|
|
}
|
2021-11-02 11:13:29 +01:00
|
|
|
break;
|
2021-11-02 15:10:20 +01:00
|
|
|
case 4:
|
|
|
|
case 6:
|
2021-09-23 21:09:36 +02:00
|
|
|
csr[mtval] = fault_data;
|
2021-11-02 15:10:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
csr[mtval] = 0;
|
2021-09-23 21:09:36 +02:00
|
|
|
}
|
2017-09-22 11:23:23 +02:00
|
|
|
fault_data = 0;
|
|
|
|
} else {
|
2021-07-09 13:01:22 +02:00
|
|
|
csr[mepc] = this->reg.NEXT_PC & get_pc_mask(); // store next address if interrupt
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.pending_trap = 0;
|
2017-08-27 12:10:38 +02:00
|
|
|
}
|
2023-10-29 17:06:56 +01:00
|
|
|
csr[mcause] = (trap_id << (traits<BASE>::XLEN - 1)) + cause;
|
2017-08-27 12:10:38 +02:00
|
|
|
// update mstatus
|
2017-09-22 11:23:23 +02:00
|
|
|
// 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
|
2017-08-27 12:10:38 +02:00
|
|
|
// is cleared
|
2017-10-04 23:10:29 +02:00
|
|
|
// store the actual privilege level in yPP and store interrupt enable flags
|
2020-09-07 11:54:45 +02:00
|
|
|
state.mstatus.MPP = PRIV_M;
|
|
|
|
state.mstatus.MPIE = state.mstatus.MIE;
|
|
|
|
state.mstatus.MIE = false;
|
2017-08-27 12:10:38 +02:00
|
|
|
|
|
|
|
// get trap vector
|
2023-03-10 20:40:21 +01:00
|
|
|
auto xtvec = csr[mtvec];
|
|
|
|
// calculate adds// set NEXT_PC to trap addressess to jump to based on MODE
|
2023-10-29 17:06:56 +01:00
|
|
|
if((FEAT & features_e::FEAT_CLIC) && trap_id != 0 && (xtvec & 0x3UL) == 3UL) {
|
2023-03-10 20:40:21 +01:00
|
|
|
reg_t data;
|
|
|
|
auto ret = read(address_type::LOGICAL, access_type::READ, 0, 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;
|
2023-10-29 17:06:56 +01:00
|
|
|
if((xtvec & 0x1) == 1 && trap_id != 0)
|
|
|
|
this->reg.NEXT_PC += 4 * cause;
|
2023-03-10 20:40:21 +01:00
|
|
|
}
|
2017-08-27 12:10:38 +02:00
|
|
|
// reset trap state
|
2022-04-13 11:41:01 +02:00
|
|
|
this->reg.PRIV = new_priv;
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.trap_state = 0;
|
2018-11-08 13:31:28 +01:00
|
|
|
std::array<char, 32> buffer;
|
2021-10-10 19:06:41 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
sprintf(buffer.data(), "0x%016llx", addr);
|
|
|
|
#else
|
2018-11-08 13:31:28 +01:00
|
|
|
sprintf(buffer.data(), "0x%016lx", addr);
|
2021-10-10 19:06:41 +02:00
|
|
|
#endif
|
2023-10-29 17:06:56 +01:00
|
|
|
if((flags & 0xffffffff) != 0xffffffff)
|
2024-07-24 12:20:38 +02:00
|
|
|
NSCLOG(INFO, LOGCAT) << (trap_id ? "Interrupt" : "Trap") << " with cause '" << (trap_id ? irq_str[cause] : trap_str[cause]) << "' ("
|
2024-07-24 14:48:50 +02:00
|
|
|
<< cause << ")"
|
|
|
|
<< " at address " << buffer.data() << " occurred";
|
2017-08-27 12:10:38 +02:00
|
|
|
return this->reg.NEXT_PC;
|
|
|
|
}
|
|
|
|
|
2024-07-24 12:20:38 +02:00
|
|
|
template <typename BASE, features_e FEAT, typename LOGCAT> uint64_t riscv_hart_m_p<BASE, FEAT, LOGCAT>::leave_trap(uint64_t flags) {
|
2021-04-07 17:42:08 +02:00
|
|
|
state.mstatus.MIE = state.mstatus.MPIE;
|
2021-07-06 21:19:36 +02:00
|
|
|
state.mstatus.MPIE = 1;
|
2017-08-27 12:10:38 +02:00
|
|
|
// sets the pc to the value stored in the x epc register.
|
2021-07-07 11:30:00 +02:00
|
|
|
this->reg.NEXT_PC = csr[mepc] & get_pc_mask();
|
2024-07-24 12:20:38 +02:00
|
|
|
NSCLOG(INFO, LOGCAT) << "Executing xRET";
|
2021-07-06 21:19:36 +02:00
|
|
|
check_interrupt();
|
2023-05-27 10:20:49 +02:00
|
|
|
this->reg.trap_state = this->reg.pending_trap;
|
2017-08-27 12:10:38 +02:00
|
|
|
return this->reg.NEXT_PC;
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:37:21 +02:00
|
|
|
} // namespace arch
|
|
|
|
} // namespace iss
|
2017-08-27 12:10:38 +02:00
|
|
|
|
2021-05-16 15:06:42 +02:00
|
|
|
#endif /* _RISCV_HART_M_P_H */
|