2018-03-02 13:31:10 +01:00
|
|
|
/*
|
|
|
|
* QEMU RISC-V CPU
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
|
|
|
|
* Copyright (c) 2017-2018 SiFive, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2 or later, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef RISCV_CPU_H
|
|
|
|
#define RISCV_CPU_H
|
|
|
|
|
2019-07-09 17:20:52 +02:00
|
|
|
#include "hw/core/cpu.h"
|
2018-03-02 13:31:10 +01:00
|
|
|
#include "exec/cpu-defs.h"
|
2019-08-08 18:29:41 +02:00
|
|
|
#include "fpu/softfloat-types.h"
|
2018-03-02 13:31:10 +01:00
|
|
|
|
2019-03-22 19:51:19 +01:00
|
|
|
#define TCG_GUEST_DEFAULT_MO 0
|
|
|
|
|
2018-03-02 13:31:10 +01:00
|
|
|
#define TYPE_RISCV_CPU "riscv-cpu"
|
|
|
|
|
|
|
|
#define RISCV_CPU_TYPE_SUFFIX "-" TYPE_RISCV_CPU
|
|
|
|
#define RISCV_CPU_TYPE_NAME(name) (name RISCV_CPU_TYPE_SUFFIX)
|
2018-02-07 11:40:25 +01:00
|
|
|
#define CPU_RESOLVING_TYPE TYPE_RISCV_CPU
|
2018-03-02 13:31:10 +01:00
|
|
|
|
|
|
|
#define TYPE_RISCV_CPU_ANY RISCV_CPU_TYPE_NAME("any")
|
2019-04-20 04:24:09 +02:00
|
|
|
#define TYPE_RISCV_CPU_BASE32 RISCV_CPU_TYPE_NAME("rv32")
|
|
|
|
#define TYPE_RISCV_CPU_BASE64 RISCV_CPU_TYPE_NAME("rv64")
|
2018-03-02 13:31:10 +01:00
|
|
|
#define TYPE_RISCV_CPU_SIFIVE_E31 RISCV_CPU_TYPE_NAME("sifive-e31")
|
|
|
|
#define TYPE_RISCV_CPU_SIFIVE_E51 RISCV_CPU_TYPE_NAME("sifive-e51")
|
|
|
|
#define TYPE_RISCV_CPU_SIFIVE_U34 RISCV_CPU_TYPE_NAME("sifive-u34")
|
|
|
|
#define TYPE_RISCV_CPU_SIFIVE_U54 RISCV_CPU_TYPE_NAME("sifive-u54")
|
2019-06-18 03:31:02 +02:00
|
|
|
/* Deprecated */
|
|
|
|
#define TYPE_RISCV_CPU_RV32IMACU_NOMMU RISCV_CPU_TYPE_NAME("rv32imacu-nommu")
|
|
|
|
#define TYPE_RISCV_CPU_RV32GCSU_V1_09_1 RISCV_CPU_TYPE_NAME("rv32gcsu-v1.9.1")
|
|
|
|
#define TYPE_RISCV_CPU_RV32GCSU_V1_10_0 RISCV_CPU_TYPE_NAME("rv32gcsu-v1.10.0")
|
|
|
|
#define TYPE_RISCV_CPU_RV64IMACU_NOMMU RISCV_CPU_TYPE_NAME("rv64imacu-nommu")
|
|
|
|
#define TYPE_RISCV_CPU_RV64GCSU_V1_09_1 RISCV_CPU_TYPE_NAME("rv64gcsu-v1.9.1")
|
|
|
|
#define TYPE_RISCV_CPU_RV64GCSU_V1_10_0 RISCV_CPU_TYPE_NAME("rv64gcsu-v1.10.0")
|
2018-03-02 13:31:10 +01:00
|
|
|
|
|
|
|
#define RV32 ((target_ulong)1 << (TARGET_LONG_BITS - 2))
|
|
|
|
#define RV64 ((target_ulong)2 << (TARGET_LONG_BITS - 2))
|
|
|
|
|
|
|
|
#if defined(TARGET_RISCV32)
|
|
|
|
#define RVXLEN RV32
|
|
|
|
#elif defined(TARGET_RISCV64)
|
|
|
|
#define RVXLEN RV64
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define RV(x) ((target_ulong)1 << (x - 'A'))
|
|
|
|
|
|
|
|
#define RVI RV('I')
|
2018-03-05 01:28:00 +01:00
|
|
|
#define RVE RV('E') /* E and I are mutually exclusive */
|
2018-03-02 13:31:10 +01:00
|
|
|
#define RVM RV('M')
|
|
|
|
#define RVA RV('A')
|
|
|
|
#define RVF RV('F')
|
|
|
|
#define RVD RV('D')
|
|
|
|
#define RVC RV('C')
|
|
|
|
#define RVS RV('S')
|
|
|
|
#define RVU RV('U')
|
|
|
|
|
|
|
|
/* S extension denotes that Supervisor mode exists, however it is possible
|
|
|
|
to have a core that support S mode but does not have an MMU and there
|
|
|
|
is currently no bit in misa to indicate whether an MMU exists or not
|
2019-01-05 00:24:14 +01:00
|
|
|
so a cpu features bitfield is required, likewise for optional PMP support */
|
2018-03-02 13:31:10 +01:00
|
|
|
enum {
|
2019-01-05 00:24:14 +01:00
|
|
|
RISCV_FEATURE_MMU,
|
2019-01-15 00:59:00 +01:00
|
|
|
RISCV_FEATURE_PMP,
|
|
|
|
RISCV_FEATURE_MISA
|
2018-03-02 13:31:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#define PRIV_VERSION_1_09_1 0x00010901
|
|
|
|
#define PRIV_VERSION_1_10_0 0x00011000
|
2019-06-18 03:31:05 +02:00
|
|
|
#define PRIV_VERSION_1_11_0 0x00011100
|
2018-03-02 13:31:10 +01:00
|
|
|
|
2019-06-14 14:19:02 +02:00
|
|
|
#define TRANSLATE_PMP_FAIL 2
|
2018-03-02 13:31:10 +01:00
|
|
|
#define TRANSLATE_FAIL 1
|
|
|
|
#define TRANSLATE_SUCCESS 0
|
|
|
|
#define MMU_USER_IDX 3
|
|
|
|
|
|
|
|
#define MAX_RISCV_PMPS (16)
|
|
|
|
|
|
|
|
typedef struct CPURISCVState CPURISCVState;
|
|
|
|
|
|
|
|
#include "pmp.h"
|
|
|
|
|
|
|
|
struct CPURISCVState {
|
|
|
|
target_ulong gpr[32];
|
|
|
|
uint64_t fpr[32]; /* assume both F and D extensions */
|
|
|
|
target_ulong pc;
|
|
|
|
target_ulong load_res;
|
|
|
|
target_ulong load_val;
|
|
|
|
|
|
|
|
target_ulong frm;
|
|
|
|
|
|
|
|
target_ulong badaddr;
|
|
|
|
|
|
|
|
target_ulong priv_ver;
|
|
|
|
target_ulong misa;
|
2019-01-15 00:59:00 +01:00
|
|
|
target_ulong misa_mask;
|
2018-03-02 13:31:10 +01:00
|
|
|
|
|
|
|
uint32_t features;
|
|
|
|
|
2019-03-16 02:20:46 +01:00
|
|
|
#ifdef CONFIG_USER_ONLY
|
|
|
|
uint32_t elf_flags;
|
|
|
|
#endif
|
|
|
|
|
2018-03-02 13:31:10 +01:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
target_ulong priv;
|
|
|
|
target_ulong resetvec;
|
|
|
|
|
|
|
|
target_ulong mhartid;
|
|
|
|
target_ulong mstatus;
|
2018-04-08 23:25:25 +02:00
|
|
|
|
2018-03-02 13:31:10 +01:00
|
|
|
/*
|
|
|
|
* CAUTION! Unlike the rest of this struct, mip is accessed asynchonously
|
2018-04-08 23:25:25 +02:00
|
|
|
* by I/O threads. It should be read with atomic_read. It should be updated
|
|
|
|
* using riscv_cpu_update_mip with the iothread mutex held. The iothread
|
|
|
|
* mutex must be held because mip must be consistent with the CPU inturrept
|
|
|
|
* state. riscv_cpu_update_mip calls cpu_interrupt or cpu_reset_interrupt
|
|
|
|
* wuth the invariant that CPU_INTERRUPT_HARD is set iff mip is non-zero.
|
|
|
|
* mip is 32-bits to allow atomic_read on 32-bit hosts.
|
2018-03-02 13:31:10 +01:00
|
|
|
*/
|
2018-04-08 23:25:25 +02:00
|
|
|
uint32_t mip;
|
2019-03-16 02:20:20 +01:00
|
|
|
uint32_t miclaim;
|
2018-04-08 23:25:25 +02:00
|
|
|
|
2018-03-02 13:31:10 +01:00
|
|
|
target_ulong mie;
|
|
|
|
target_ulong mideleg;
|
|
|
|
|
|
|
|
target_ulong sptbr; /* until: priv-1.9.1 */
|
|
|
|
target_ulong satp; /* since: priv-1.10.0 */
|
|
|
|
target_ulong sbadaddr;
|
|
|
|
target_ulong mbadaddr;
|
|
|
|
target_ulong medeleg;
|
|
|
|
|
|
|
|
target_ulong stvec;
|
|
|
|
target_ulong sepc;
|
|
|
|
target_ulong scause;
|
|
|
|
|
|
|
|
target_ulong mtvec;
|
|
|
|
target_ulong mepc;
|
|
|
|
target_ulong mcause;
|
|
|
|
target_ulong mtval; /* since: priv-1.10.0 */
|
|
|
|
|
2018-04-09 01:33:05 +02:00
|
|
|
target_ulong scounteren;
|
|
|
|
target_ulong mcounteren;
|
2018-03-02 13:31:10 +01:00
|
|
|
|
|
|
|
target_ulong sscratch;
|
|
|
|
target_ulong mscratch;
|
|
|
|
|
|
|
|
/* temporary htif regs */
|
|
|
|
uint64_t mfromhost;
|
|
|
|
uint64_t mtohost;
|
|
|
|
uint64_t timecmp;
|
|
|
|
|
|
|
|
/* physical memory protection */
|
|
|
|
pmp_table_t pmp_state;
|
2019-03-15 11:26:58 +01:00
|
|
|
|
|
|
|
/* True if in debugger mode. */
|
|
|
|
bool debugger;
|
2018-03-02 13:31:10 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
float_status fp_status;
|
|
|
|
|
|
|
|
/* Fields from here on are preserved across CPU reset. */
|
|
|
|
QEMUTimer *timer; /* Internal timer */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define RISCV_CPU_CLASS(klass) \
|
|
|
|
OBJECT_CLASS_CHECK(RISCVCPUClass, (klass), TYPE_RISCV_CPU)
|
|
|
|
#define RISCV_CPU(obj) \
|
|
|
|
OBJECT_CHECK(RISCVCPU, (obj), TYPE_RISCV_CPU)
|
|
|
|
#define RISCV_CPU_GET_CLASS(obj) \
|
|
|
|
OBJECT_GET_CLASS(RISCVCPUClass, (obj), TYPE_RISCV_CPU)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RISCVCPUClass:
|
|
|
|
* @parent_realize: The parent class' realize handler.
|
|
|
|
* @parent_reset: The parent class' reset handler.
|
|
|
|
*
|
|
|
|
* A RISCV CPU model.
|
|
|
|
*/
|
|
|
|
typedef struct RISCVCPUClass {
|
|
|
|
/*< private >*/
|
|
|
|
CPUClass parent_class;
|
|
|
|
/*< public >*/
|
|
|
|
DeviceRealize parent_realize;
|
|
|
|
void (*parent_reset)(CPUState *cpu);
|
|
|
|
} RISCVCPUClass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RISCVCPU:
|
|
|
|
* @env: #CPURISCVState
|
|
|
|
*
|
|
|
|
* A RISCV CPU.
|
|
|
|
*/
|
|
|
|
typedef struct RISCVCPU {
|
|
|
|
/*< private >*/
|
|
|
|
CPUState parent_obj;
|
|
|
|
/*< public >*/
|
2019-03-23 01:16:06 +01:00
|
|
|
CPUNegativeOffsetState neg;
|
2018-03-02 13:31:10 +01:00
|
|
|
CPURISCVState env;
|
2019-04-20 04:24:01 +02:00
|
|
|
|
|
|
|
/* Configuration Settings */
|
|
|
|
struct {
|
2019-05-07 00:49:53 +02:00
|
|
|
bool ext_i;
|
|
|
|
bool ext_e;
|
|
|
|
bool ext_g;
|
|
|
|
bool ext_m;
|
|
|
|
bool ext_a;
|
|
|
|
bool ext_f;
|
|
|
|
bool ext_d;
|
|
|
|
bool ext_c;
|
|
|
|
bool ext_s;
|
|
|
|
bool ext_u;
|
2019-06-18 03:31:22 +02:00
|
|
|
bool ext_counters;
|
2019-06-24 10:59:05 +02:00
|
|
|
bool ext_ifencei;
|
2019-06-24 10:59:51 +02:00
|
|
|
bool ext_icsr;
|
2019-05-07 00:49:53 +02:00
|
|
|
|
2019-04-20 04:24:01 +02:00
|
|
|
char *priv_spec;
|
|
|
|
char *user_spec;
|
|
|
|
bool mmu;
|
|
|
|
bool pmp;
|
|
|
|
} cfg;
|
2018-03-02 13:31:10 +01:00
|
|
|
} RISCVCPU;
|
|
|
|
|
|
|
|
static inline int riscv_has_ext(CPURISCVState *env, target_ulong ext)
|
|
|
|
{
|
|
|
|
return (env->misa & ext) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool riscv_feature(CPURISCVState *env, int feature)
|
|
|
|
{
|
|
|
|
return env->features & (1ULL << feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "cpu_user.h"
|
|
|
|
#include "cpu_bits.h"
|
|
|
|
|
|
|
|
extern const char * const riscv_int_regnames[];
|
|
|
|
extern const char * const riscv_fpr_regnames[];
|
|
|
|
extern const char * const riscv_excp_names[];
|
|
|
|
extern const char * const riscv_intr_names[];
|
|
|
|
|
|
|
|
void riscv_cpu_do_interrupt(CPUState *cpu);
|
|
|
|
int riscv_cpu_gdb_read_register(CPUState *cpu, uint8_t *buf, int reg);
|
|
|
|
int riscv_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
|
|
|
|
bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request);
|
|
|
|
int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch);
|
|
|
|
hwaddr riscv_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
|
|
|
|
void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
|
|
|
|
MMUAccessType access_type, int mmu_idx,
|
|
|
|
uintptr_t retaddr);
|
2019-04-02 12:12:38 +02:00
|
|
|
bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
|
|
|
|
MMUAccessType access_type, int mmu_idx,
|
|
|
|
bool probe, uintptr_t retaddr);
|
2019-05-18 00:11:06 +02:00
|
|
|
void riscv_cpu_unassigned_access(CPUState *cpu, hwaddr addr, bool is_write,
|
|
|
|
bool is_exec, int unused, unsigned size);
|
2018-03-02 13:31:10 +01:00
|
|
|
char *riscv_isa_string(RISCVCPU *cpu);
|
2019-04-17 21:17:57 +02:00
|
|
|
void riscv_cpu_list(void);
|
2018-03-02 13:31:10 +01:00
|
|
|
|
2019-01-15 00:58:23 +01:00
|
|
|
#define cpu_signal_handler riscv_cpu_signal_handler
|
2018-03-02 13:31:10 +01:00
|
|
|
#define cpu_list riscv_cpu_list
|
|
|
|
#define cpu_mmu_index riscv_cpu_mmu_index
|
|
|
|
|
2018-04-08 23:25:25 +02:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2019-03-16 02:20:20 +01:00
|
|
|
int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts);
|
2018-04-08 23:25:25 +02:00
|
|
|
uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value);
|
|
|
|
#define BOOL_TO_MASK(x) (-!!(x)) /* helper for riscv_cpu_update_mip value */
|
|
|
|
#endif
|
2019-01-15 00:58:23 +01:00
|
|
|
void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv);
|
2018-03-02 13:31:10 +01:00
|
|
|
|
|
|
|
void riscv_translate_init(void);
|
2019-01-15 00:58:23 +01:00
|
|
|
int riscv_cpu_signal_handler(int host_signum, void *pinfo, void *puc);
|
|
|
|
void QEMU_NORETURN riscv_raise_exception(CPURISCVState *env,
|
|
|
|
uint32_t exception, uintptr_t pc);
|
2018-03-02 13:31:10 +01:00
|
|
|
|
2019-01-15 00:58:23 +01:00
|
|
|
target_ulong riscv_cpu_get_fflags(CPURISCVState *env);
|
|
|
|
void riscv_cpu_set_fflags(CPURISCVState *env, target_ulong);
|
2018-03-02 13:31:10 +01:00
|
|
|
|
2019-01-15 00:57:50 +01:00
|
|
|
#define TB_FLAGS_MMU_MASK 3
|
|
|
|
#define TB_FLAGS_MSTATUS_FS MSTATUS_FS
|
2018-03-02 13:31:10 +01:00
|
|
|
|
|
|
|
static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
|
|
|
|
target_ulong *cs_base, uint32_t *flags)
|
|
|
|
{
|
|
|
|
*pc = env->pc;
|
|
|
|
*cs_base = 0;
|
|
|
|
#ifdef CONFIG_USER_ONLY
|
2019-01-15 00:57:50 +01:00
|
|
|
*flags = TB_FLAGS_MSTATUS_FS;
|
2018-03-02 13:31:10 +01:00
|
|
|
#else
|
|
|
|
*flags = cpu_mmu_index(env, 0) | (env->mstatus & MSTATUS_FS);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-01-05 00:23:55 +01:00
|
|
|
int riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value,
|
|
|
|
target_ulong new_value, target_ulong write_mask);
|
2019-03-15 11:26:58 +01:00
|
|
|
int riscv_csrrw_debug(CPURISCVState *env, int csrno, target_ulong *ret_value,
|
|
|
|
target_ulong new_value, target_ulong write_mask);
|
2019-01-05 00:23:55 +01:00
|
|
|
|
2019-01-15 00:58:23 +01:00
|
|
|
static inline void riscv_csr_write(CPURISCVState *env, int csrno,
|
|
|
|
target_ulong val)
|
2019-01-05 00:23:55 +01:00
|
|
|
{
|
|
|
|
riscv_csrrw(env, csrno, NULL, val, MAKE_64BIT_MASK(0, TARGET_LONG_BITS));
|
|
|
|
}
|
|
|
|
|
2019-01-15 00:58:23 +01:00
|
|
|
static inline target_ulong riscv_csr_read(CPURISCVState *env, int csrno)
|
2019-01-05 00:23:55 +01:00
|
|
|
{
|
|
|
|
target_ulong val = 0;
|
|
|
|
riscv_csrrw(env, csrno, &val, 0, 0);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef int (*riscv_csr_predicate_fn)(CPURISCVState *env, int csrno);
|
|
|
|
typedef int (*riscv_csr_read_fn)(CPURISCVState *env, int csrno,
|
|
|
|
target_ulong *ret_value);
|
|
|
|
typedef int (*riscv_csr_write_fn)(CPURISCVState *env, int csrno,
|
|
|
|
target_ulong new_value);
|
|
|
|
typedef int (*riscv_csr_op_fn)(CPURISCVState *env, int csrno,
|
|
|
|
target_ulong *ret_value, target_ulong new_value, target_ulong write_mask);
|
|
|
|
|
|
|
|
typedef struct {
|
2019-01-05 00:24:14 +01:00
|
|
|
riscv_csr_predicate_fn predicate;
|
2019-01-05 00:23:55 +01:00
|
|
|
riscv_csr_read_fn read;
|
|
|
|
riscv_csr_write_fn write;
|
|
|
|
riscv_csr_op_fn op;
|
|
|
|
} riscv_csr_operations;
|
|
|
|
|
|
|
|
void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops);
|
|
|
|
void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
|
2018-03-02 13:31:10 +01:00
|
|
|
|
2019-03-15 11:26:59 +01:00
|
|
|
void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
|
|
|
|
|
2019-03-22 23:32:23 +01:00
|
|
|
typedef CPURISCVState CPUArchState;
|
2019-03-22 23:56:19 +01:00
|
|
|
typedef RISCVCPU ArchCPU;
|
2019-03-22 23:32:23 +01:00
|
|
|
|
2018-03-02 13:31:10 +01:00
|
|
|
#include "exec/cpu-all.h"
|
|
|
|
|
|
|
|
#endif /* RISCV_CPU_H */
|