2018-03-02 15:31:10 +03: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2019-04-17 22:17:57 +03:00
|
|
|
#include "qemu/qemu-print.h"
|
2019-05-23 17:35:06 +03:00
|
|
|
#include "qemu/ctype.h"
|
2018-03-02 15:31:10 +03:00
|
|
|
#include "qemu/log.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "exec/exec-all.h"
|
|
|
|
#include "qapi/error.h"
|
2019-05-07 01:49:53 +03:00
|
|
|
#include "qemu/error-report.h"
|
2019-04-20 05:24:01 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2018-03-02 15:31:10 +03:00
|
|
|
#include "migration/vmstate.h"
|
2019-08-08 19:29:41 +03:00
|
|
|
#include "fpu/softfloat-helpers.h"
|
2018-03-02 15:31:10 +03:00
|
|
|
|
|
|
|
/* RISC-V CPU definitions */
|
|
|
|
|
2018-03-05 03:28:00 +03:00
|
|
|
static const char riscv_exts[26] = "IEMAFDQCLBJTPVNSUHKORWXYZG";
|
2018-03-02 15:31:10 +03:00
|
|
|
|
|
|
|
const char * const riscv_int_regnames[] = {
|
2019-08-23 18:21:19 +03:00
|
|
|
"x0/zero", "x1/ra", "x2/sp", "x3/gp", "x4/tp", "x5/t0", "x6/t1",
|
|
|
|
"x7/t2", "x8/s0", "x9/s1", "x10/a0", "x11/a1", "x12/a2", "x13/a3",
|
|
|
|
"x14/a4", "x15/a5", "x16/a6", "x17/a7", "x18/s2", "x19/s3", "x20/s4",
|
|
|
|
"x21/s5", "x22/s6", "x23/s7", "x24/s8", "x25/s9", "x26/s10", "x27/s11",
|
|
|
|
"x28/t3", "x29/t4", "x30/t5", "x31/t6"
|
2018-03-02 15:31:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const char * const riscv_fpr_regnames[] = {
|
2019-08-23 18:21:19 +03:00
|
|
|
"f0/ft0", "f1/ft1", "f2/ft2", "f3/ft3", "f4/ft4", "f5/ft5",
|
|
|
|
"f6/ft6", "f7/ft7", "f8/fs0", "f9/fs1", "f10/fa0", "f11/fa1",
|
|
|
|
"f12/fa2", "f13/fa3", "f14/fa4", "f15/fa5", "f16/fa6", "f17/fa7",
|
|
|
|
"f18/fs2", "f19/fs3", "f20/fs4", "f21/fs5", "f22/fs6", "f23/fs7",
|
|
|
|
"f24/fs8", "f25/fs9", "f26/fs10", "f27/fs11", "f28/ft8", "f29/ft9",
|
|
|
|
"f30/ft10", "f31/ft11"
|
2018-03-02 15:31:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const char * const riscv_excp_names[] = {
|
|
|
|
"misaligned_fetch",
|
|
|
|
"fault_fetch",
|
|
|
|
"illegal_instruction",
|
|
|
|
"breakpoint",
|
|
|
|
"misaligned_load",
|
|
|
|
"fault_load",
|
|
|
|
"misaligned_store",
|
|
|
|
"fault_store",
|
|
|
|
"user_ecall",
|
|
|
|
"supervisor_ecall",
|
|
|
|
"hypervisor_ecall",
|
|
|
|
"machine_ecall",
|
|
|
|
"exec_page_fault",
|
|
|
|
"load_page_fault",
|
|
|
|
"reserved",
|
|
|
|
"store_page_fault"
|
|
|
|
};
|
|
|
|
|
|
|
|
const char * const riscv_intr_names[] = {
|
|
|
|
"u_software",
|
|
|
|
"s_software",
|
|
|
|
"h_software",
|
|
|
|
"m_software",
|
|
|
|
"u_timer",
|
|
|
|
"s_timer",
|
|
|
|
"h_timer",
|
|
|
|
"m_timer",
|
|
|
|
"u_external",
|
|
|
|
"s_external",
|
|
|
|
"h_external",
|
|
|
|
"m_external",
|
2018-03-06 00:51:53 +03:00
|
|
|
"reserved",
|
|
|
|
"reserved",
|
|
|
|
"reserved",
|
|
|
|
"reserved"
|
2018-03-02 15:31:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void set_misa(CPURISCVState *env, target_ulong misa)
|
|
|
|
{
|
2019-01-15 02:59:00 +03:00
|
|
|
env->misa_mask = env->misa = misa;
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2019-06-18 04:31:19 +03:00
|
|
|
static void set_priv_version(CPURISCVState *env, int priv_ver)
|
2018-03-02 15:31:10 +03:00
|
|
|
{
|
|
|
|
env->priv_ver = priv_ver;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_feature(CPURISCVState *env, int feature)
|
|
|
|
{
|
|
|
|
env->features |= (1ULL << feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_resetvec(CPURISCVState *env, int resetvec)
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
env->resetvec = resetvec;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void riscv_any_cpu_init(Object *obj)
|
|
|
|
{
|
|
|
|
CPURISCVState *env = &RISCV_CPU(obj)->env;
|
|
|
|
set_misa(env, RVXLEN | RVI | RVM | RVA | RVF | RVD | RVC | RVU);
|
2019-06-18 04:31:19 +03:00
|
|
|
set_priv_version(env, PRIV_VERSION_1_11_0);
|
2018-03-02 15:31:10 +03:00
|
|
|
set_resetvec(env, DEFAULT_RSTVEC);
|
|
|
|
}
|
|
|
|
|
2018-03-09 01:12:31 +03:00
|
|
|
#if defined(TARGET_RISCV32)
|
|
|
|
|
2019-04-20 05:24:09 +03:00
|
|
|
static void riscv_base32_cpu_init(Object *obj)
|
|
|
|
{
|
|
|
|
CPURISCVState *env = &RISCV_CPU(obj)->env;
|
2019-05-07 01:49:53 +03:00
|
|
|
/* We set this in the realise function */
|
|
|
|
set_misa(env, 0);
|
2019-04-20 05:24:09 +03:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:31:10 +03:00
|
|
|
static void rv32gcsu_priv1_09_1_cpu_init(Object *obj)
|
|
|
|
{
|
|
|
|
CPURISCVState *env = &RISCV_CPU(obj)->env;
|
|
|
|
set_misa(env, RV32 | RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
|
2019-06-18 04:31:19 +03:00
|
|
|
set_priv_version(env, PRIV_VERSION_1_09_1);
|
2018-03-02 15:31:10 +03:00
|
|
|
set_resetvec(env, DEFAULT_RSTVEC);
|
|
|
|
set_feature(env, RISCV_FEATURE_MMU);
|
2019-01-05 02:24:14 +03:00
|
|
|
set_feature(env, RISCV_FEATURE_PMP);
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rv32gcsu_priv1_10_0_cpu_init(Object *obj)
|
|
|
|
{
|
|
|
|
CPURISCVState *env = &RISCV_CPU(obj)->env;
|
|
|
|
set_misa(env, RV32 | RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
|
2019-06-18 04:31:19 +03:00
|
|
|
set_priv_version(env, PRIV_VERSION_1_10_0);
|
2018-03-02 15:31:10 +03:00
|
|
|
set_resetvec(env, DEFAULT_RSTVEC);
|
|
|
|
set_feature(env, RISCV_FEATURE_MMU);
|
2019-01-05 02:24:14 +03:00
|
|
|
set_feature(env, RISCV_FEATURE_PMP);
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rv32imacu_nommu_cpu_init(Object *obj)
|
|
|
|
{
|
|
|
|
CPURISCVState *env = &RISCV_CPU(obj)->env;
|
|
|
|
set_misa(env, RV32 | RVI | RVM | RVA | RVC | RVU);
|
2019-06-18 04:31:19 +03:00
|
|
|
set_priv_version(env, PRIV_VERSION_1_10_0);
|
2018-03-02 15:31:10 +03:00
|
|
|
set_resetvec(env, DEFAULT_RSTVEC);
|
2019-01-05 02:24:14 +03:00
|
|
|
set_feature(env, RISCV_FEATURE_PMP);
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 01:12:31 +03:00
|
|
|
#elif defined(TARGET_RISCV64)
|
|
|
|
|
2019-04-20 05:24:09 +03:00
|
|
|
static void riscv_base64_cpu_init(Object *obj)
|
|
|
|
{
|
|
|
|
CPURISCVState *env = &RISCV_CPU(obj)->env;
|
2019-05-07 01:49:53 +03:00
|
|
|
/* We set this in the realise function */
|
|
|
|
set_misa(env, 0);
|
2019-04-20 05:24:09 +03:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:31:10 +03:00
|
|
|
static void rv64gcsu_priv1_09_1_cpu_init(Object *obj)
|
|
|
|
{
|
|
|
|
CPURISCVState *env = &RISCV_CPU(obj)->env;
|
|
|
|
set_misa(env, RV64 | RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
|
2019-06-18 04:31:19 +03:00
|
|
|
set_priv_version(env, PRIV_VERSION_1_09_1);
|
2018-03-02 15:31:10 +03:00
|
|
|
set_resetvec(env, DEFAULT_RSTVEC);
|
|
|
|
set_feature(env, RISCV_FEATURE_MMU);
|
2019-01-05 02:24:14 +03:00
|
|
|
set_feature(env, RISCV_FEATURE_PMP);
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rv64gcsu_priv1_10_0_cpu_init(Object *obj)
|
|
|
|
{
|
|
|
|
CPURISCVState *env = &RISCV_CPU(obj)->env;
|
|
|
|
set_misa(env, RV64 | RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
|
2019-06-18 04:31:19 +03:00
|
|
|
set_priv_version(env, PRIV_VERSION_1_10_0);
|
2018-03-02 15:31:10 +03:00
|
|
|
set_resetvec(env, DEFAULT_RSTVEC);
|
|
|
|
set_feature(env, RISCV_FEATURE_MMU);
|
2019-01-05 02:24:14 +03:00
|
|
|
set_feature(env, RISCV_FEATURE_PMP);
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rv64imacu_nommu_cpu_init(Object *obj)
|
|
|
|
{
|
|
|
|
CPURISCVState *env = &RISCV_CPU(obj)->env;
|
|
|
|
set_misa(env, RV64 | RVI | RVM | RVA | RVC | RVU);
|
2019-06-18 04:31:19 +03:00
|
|
|
set_priv_version(env, PRIV_VERSION_1_10_0);
|
2018-03-02 15:31:10 +03:00
|
|
|
set_resetvec(env, DEFAULT_RSTVEC);
|
2019-01-05 02:24:14 +03:00
|
|
|
set_feature(env, RISCV_FEATURE_PMP);
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 01:12:31 +03:00
|
|
|
#endif
|
2018-03-02 15:31:10 +03:00
|
|
|
|
|
|
|
static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
|
|
|
|
{
|
|
|
|
ObjectClass *oc;
|
|
|
|
char *typename;
|
|
|
|
char **cpuname;
|
|
|
|
|
|
|
|
cpuname = g_strsplit(cpu_model, ",", 1);
|
|
|
|
typename = g_strdup_printf(RISCV_CPU_TYPE_NAME("%s"), cpuname[0]);
|
|
|
|
oc = object_class_by_name(typename);
|
|
|
|
g_strfreev(cpuname);
|
|
|
|
g_free(typename);
|
|
|
|
if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) ||
|
|
|
|
object_class_is_abstract(oc)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return oc;
|
|
|
|
}
|
|
|
|
|
2019-04-17 22:18:02 +03:00
|
|
|
static void riscv_cpu_dump_state(CPUState *cs, FILE *f, int flags)
|
2018-03-02 15:31:10 +03:00
|
|
|
{
|
|
|
|
RISCVCPU *cpu = RISCV_CPU(cs);
|
|
|
|
CPURISCVState *env = &cpu->env;
|
|
|
|
int i;
|
|
|
|
|
2019-04-17 22:18:02 +03:00
|
|
|
qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "pc ", env->pc);
|
2018-03-02 15:31:10 +03:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2019-04-17 22:18:02 +03:00
|
|
|
qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mhartid ", env->mhartid);
|
|
|
|
qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mstatus ", env->mstatus);
|
2019-10-09 01:04:18 +03:00
|
|
|
qemu_fprintf(f, " %s 0x%x\n", "mip ", env->mip);
|
2019-04-17 22:18:02 +03:00
|
|
|
qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mie ", env->mie);
|
|
|
|
qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mideleg ", env->mideleg);
|
|
|
|
qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "medeleg ", env->medeleg);
|
|
|
|
qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mtvec ", env->mtvec);
|
|
|
|
qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mepc ", env->mepc);
|
|
|
|
qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mcause ", env->mcause);
|
2018-03-02 15:31:10 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
2019-04-17 22:18:02 +03:00
|
|
|
qemu_fprintf(f, " %s " TARGET_FMT_lx,
|
|
|
|
riscv_int_regnames[i], env->gpr[i]);
|
2018-03-02 15:31:10 +03:00
|
|
|
if ((i & 3) == 3) {
|
2019-04-17 22:18:02 +03:00
|
|
|
qemu_fprintf(f, "\n");
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 06:31:33 +03:00
|
|
|
if (flags & CPU_DUMP_FPU) {
|
|
|
|
for (i = 0; i < 32; i++) {
|
2019-04-17 22:18:02 +03:00
|
|
|
qemu_fprintf(f, " %s %016" PRIx64,
|
|
|
|
riscv_fpr_regnames[i], env->fpr[i]);
|
2018-05-11 06:31:33 +03:00
|
|
|
if ((i & 3) == 3) {
|
2019-04-17 22:18:02 +03:00
|
|
|
qemu_fprintf(f, "\n");
|
2018-05-11 06:31:33 +03:00
|
|
|
}
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void riscv_cpu_set_pc(CPUState *cs, vaddr value)
|
|
|
|
{
|
|
|
|
RISCVCPU *cpu = RISCV_CPU(cs);
|
|
|
|
CPURISCVState *env = &cpu->env;
|
|
|
|
env->pc = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void riscv_cpu_synchronize_from_tb(CPUState *cs, TranslationBlock *tb)
|
|
|
|
{
|
|
|
|
RISCVCPU *cpu = RISCV_CPU(cs);
|
|
|
|
CPURISCVState *env = &cpu->env;
|
|
|
|
env->pc = tb->pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool riscv_cpu_has_work(CPUState *cs)
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
RISCVCPU *cpu = RISCV_CPU(cs);
|
|
|
|
CPURISCVState *env = &cpu->env;
|
|
|
|
/*
|
|
|
|
* Definition of the WFI instruction requires it to ignore the privilege
|
|
|
|
* mode and delegation registers, but respect individual enables
|
|
|
|
*/
|
2019-10-09 01:04:18 +03:00
|
|
|
return (env->mip & env->mie) != 0;
|
2018-03-02 15:31:10 +03:00
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void restore_state_to_opc(CPURISCVState *env, TranslationBlock *tb,
|
|
|
|
target_ulong *data)
|
|
|
|
{
|
|
|
|
env->pc = data[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void riscv_cpu_reset(CPUState *cs)
|
|
|
|
{
|
|
|
|
RISCVCPU *cpu = RISCV_CPU(cs);
|
|
|
|
RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu);
|
|
|
|
CPURISCVState *env = &cpu->env;
|
|
|
|
|
|
|
|
mcc->parent_reset(cs);
|
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
env->priv = PRV_M;
|
|
|
|
env->mstatus &= ~(MSTATUS_MIE | MSTATUS_MPRV);
|
|
|
|
env->mcause = 0;
|
|
|
|
env->pc = env->resetvec;
|
|
|
|
#endif
|
|
|
|
cs->exception_index = EXCP_NONE;
|
2019-06-24 21:08:38 +03:00
|
|
|
env->load_res = -1;
|
2018-03-02 15:31:10 +03:00
|
|
|
set_default_nan_mode(1, &env->fp_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void riscv_cpu_disas_set_info(CPUState *s, disassemble_info *info)
|
|
|
|
{
|
|
|
|
#if defined(TARGET_RISCV32)
|
|
|
|
info->print_insn = print_insn_riscv32;
|
|
|
|
#elif defined(TARGET_RISCV64)
|
|
|
|
info->print_insn = print_insn_riscv64;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void riscv_cpu_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
CPUState *cs = CPU(dev);
|
2019-04-20 05:24:01 +03:00
|
|
|
RISCVCPU *cpu = RISCV_CPU(dev);
|
|
|
|
CPURISCVState *env = &cpu->env;
|
2018-03-02 15:31:10 +03:00
|
|
|
RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(dev);
|
2019-06-18 04:31:11 +03:00
|
|
|
int priv_version = PRIV_VERSION_1_11_0;
|
2019-05-07 01:49:53 +03:00
|
|
|
target_ulong target_misa = 0;
|
2018-03-02 15:31:10 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
cpu_exec_realizefn(cs, &local_err);
|
|
|
|
if (local_err != NULL) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-20 05:24:01 +03:00
|
|
|
if (cpu->cfg.priv_spec) {
|
2019-06-18 04:31:11 +03:00
|
|
|
if (!g_strcmp0(cpu->cfg.priv_spec, "v1.11.0")) {
|
|
|
|
priv_version = PRIV_VERSION_1_11_0;
|
|
|
|
} else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.10.0")) {
|
2019-04-20 05:24:01 +03:00
|
|
|
priv_version = PRIV_VERSION_1_10_0;
|
|
|
|
} else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.9.1")) {
|
|
|
|
priv_version = PRIV_VERSION_1_09_1;
|
|
|
|
} else {
|
|
|
|
error_setg(errp,
|
|
|
|
"Unsupported privilege spec version '%s'",
|
|
|
|
cpu->cfg.priv_spec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 04:31:19 +03:00
|
|
|
set_priv_version(env, priv_version);
|
2019-04-20 05:24:01 +03:00
|
|
|
set_resetvec(env, DEFAULT_RSTVEC);
|
|
|
|
|
|
|
|
if (cpu->cfg.mmu) {
|
|
|
|
set_feature(env, RISCV_FEATURE_MMU);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpu->cfg.pmp) {
|
|
|
|
set_feature(env, RISCV_FEATURE_PMP);
|
|
|
|
}
|
|
|
|
|
2019-05-07 01:49:53 +03:00
|
|
|
/* If misa isn't set (rv32 and rv64 machines) set it here */
|
|
|
|
if (!env->misa) {
|
|
|
|
/* Do some ISA extension error checking */
|
|
|
|
if (cpu->cfg.ext_i && cpu->cfg.ext_e) {
|
|
|
|
error_setg(errp,
|
|
|
|
"I and E extensions are incompatible");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-18 04:31:16 +03:00
|
|
|
if (!cpu->cfg.ext_i && !cpu->cfg.ext_e) {
|
|
|
|
error_setg(errp,
|
|
|
|
"Either I or E extension must be set");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-07 01:49:53 +03:00
|
|
|
if (cpu->cfg.ext_g && !(cpu->cfg.ext_i & cpu->cfg.ext_m &
|
|
|
|
cpu->cfg.ext_a & cpu->cfg.ext_f &
|
|
|
|
cpu->cfg.ext_d)) {
|
|
|
|
warn_report("Setting G will also set IMAFD");
|
|
|
|
cpu->cfg.ext_i = true;
|
|
|
|
cpu->cfg.ext_m = true;
|
|
|
|
cpu->cfg.ext_a = true;
|
|
|
|
cpu->cfg.ext_f = true;
|
|
|
|
cpu->cfg.ext_d = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the ISA extensions, checks should have happened above */
|
|
|
|
if (cpu->cfg.ext_i) {
|
|
|
|
target_misa |= RVI;
|
|
|
|
}
|
|
|
|
if (cpu->cfg.ext_e) {
|
|
|
|
target_misa |= RVE;
|
|
|
|
}
|
|
|
|
if (cpu->cfg.ext_m) {
|
|
|
|
target_misa |= RVM;
|
|
|
|
}
|
|
|
|
if (cpu->cfg.ext_a) {
|
|
|
|
target_misa |= RVA;
|
|
|
|
}
|
|
|
|
if (cpu->cfg.ext_f) {
|
|
|
|
target_misa |= RVF;
|
|
|
|
}
|
|
|
|
if (cpu->cfg.ext_d) {
|
|
|
|
target_misa |= RVD;
|
|
|
|
}
|
|
|
|
if (cpu->cfg.ext_c) {
|
|
|
|
target_misa |= RVC;
|
|
|
|
}
|
|
|
|
if (cpu->cfg.ext_s) {
|
|
|
|
target_misa |= RVS;
|
|
|
|
}
|
|
|
|
if (cpu->cfg.ext_u) {
|
|
|
|
target_misa |= RVU;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_misa(env, RVXLEN | target_misa);
|
|
|
|
}
|
|
|
|
|
2019-03-15 13:26:59 +03:00
|
|
|
riscv_cpu_register_gdb_regs_for_features(cs);
|
|
|
|
|
2018-03-02 15:31:10 +03:00
|
|
|
qemu_init_vcpu(cs);
|
|
|
|
cpu_reset(cs);
|
|
|
|
|
|
|
|
mcc->parent_realize(dev, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void riscv_cpu_init(Object *obj)
|
|
|
|
{
|
|
|
|
RISCVCPU *cpu = RISCV_CPU(obj);
|
|
|
|
|
2019-03-29 00:26:22 +03:00
|
|
|
cpu_set_cpustate_pointers(cpu);
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_riscv_cpu = {
|
|
|
|
.name = "cpu",
|
|
|
|
.unmigratable = 1,
|
|
|
|
};
|
|
|
|
|
2019-04-20 05:24:01 +03:00
|
|
|
static Property riscv_cpu_properties[] = {
|
2019-05-07 01:49:53 +03:00
|
|
|
DEFINE_PROP_BOOL("i", RISCVCPU, cfg.ext_i, true),
|
|
|
|
DEFINE_PROP_BOOL("e", RISCVCPU, cfg.ext_e, false),
|
|
|
|
DEFINE_PROP_BOOL("g", RISCVCPU, cfg.ext_g, true),
|
|
|
|
DEFINE_PROP_BOOL("m", RISCVCPU, cfg.ext_m, true),
|
|
|
|
DEFINE_PROP_BOOL("a", RISCVCPU, cfg.ext_a, true),
|
|
|
|
DEFINE_PROP_BOOL("f", RISCVCPU, cfg.ext_f, true),
|
|
|
|
DEFINE_PROP_BOOL("d", RISCVCPU, cfg.ext_d, true),
|
|
|
|
DEFINE_PROP_BOOL("c", RISCVCPU, cfg.ext_c, true),
|
|
|
|
DEFINE_PROP_BOOL("s", RISCVCPU, cfg.ext_s, true),
|
|
|
|
DEFINE_PROP_BOOL("u", RISCVCPU, cfg.ext_u, true),
|
2019-06-18 04:31:22 +03:00
|
|
|
DEFINE_PROP_BOOL("Counters", RISCVCPU, cfg.ext_counters, true),
|
2019-06-24 11:59:05 +03:00
|
|
|
DEFINE_PROP_BOOL("Zifencei", RISCVCPU, cfg.ext_ifencei, true),
|
2019-06-24 11:59:51 +03:00
|
|
|
DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true),
|
2019-04-20 05:24:01 +03:00
|
|
|
DEFINE_PROP_STRING("priv_spec", RISCVCPU, cfg.priv_spec),
|
|
|
|
DEFINE_PROP_BOOL("mmu", RISCVCPU, cfg.mmu, true),
|
|
|
|
DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2018-03-02 15:31:10 +03:00
|
|
|
static void riscv_cpu_class_init(ObjectClass *c, void *data)
|
|
|
|
{
|
|
|
|
RISCVCPUClass *mcc = RISCV_CPU_CLASS(c);
|
|
|
|
CPUClass *cc = CPU_CLASS(c);
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(c);
|
|
|
|
|
2018-11-26 06:20:38 +03:00
|
|
|
device_class_set_parent_realize(dc, riscv_cpu_realize,
|
|
|
|
&mcc->parent_realize);
|
2018-03-02 15:31:10 +03:00
|
|
|
|
|
|
|
mcc->parent_reset = cc->reset;
|
|
|
|
cc->reset = riscv_cpu_reset;
|
|
|
|
|
|
|
|
cc->class_by_name = riscv_cpu_class_by_name;
|
|
|
|
cc->has_work = riscv_cpu_has_work;
|
|
|
|
cc->do_interrupt = riscv_cpu_do_interrupt;
|
|
|
|
cc->cpu_exec_interrupt = riscv_cpu_exec_interrupt;
|
|
|
|
cc->dump_state = riscv_cpu_dump_state;
|
|
|
|
cc->set_pc = riscv_cpu_set_pc;
|
|
|
|
cc->synchronize_from_tb = riscv_cpu_synchronize_from_tb;
|
|
|
|
cc->gdb_read_register = riscv_cpu_gdb_read_register;
|
|
|
|
cc->gdb_write_register = riscv_cpu_gdb_write_register;
|
2019-03-15 13:26:59 +03:00
|
|
|
cc->gdb_num_core_regs = 33;
|
|
|
|
#if defined(TARGET_RISCV32)
|
|
|
|
cc->gdb_core_xml_file = "riscv-32bit-cpu.xml";
|
|
|
|
#elif defined(TARGET_RISCV64)
|
|
|
|
cc->gdb_core_xml_file = "riscv-64bit-cpu.xml";
|
|
|
|
#endif
|
2018-03-02 15:31:10 +03:00
|
|
|
cc->gdb_stop_before_watchpoint = true;
|
|
|
|
cc->disas_set_info = riscv_cpu_disas_set_info;
|
2019-04-02 13:12:38 +03:00
|
|
|
#ifndef CONFIG_USER_ONLY
|
2019-10-08 23:51:52 +03:00
|
|
|
cc->do_transaction_failed = riscv_cpu_do_transaction_failed;
|
2018-03-02 15:31:10 +03:00
|
|
|
cc->do_unaligned_access = riscv_cpu_do_unaligned_access;
|
|
|
|
cc->get_phys_page_debug = riscv_cpu_get_phys_page_debug;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_TCG
|
|
|
|
cc->tcg_initialize = riscv_translate_init;
|
2019-04-02 13:12:38 +03:00
|
|
|
cc->tlb_fill = riscv_cpu_tlb_fill;
|
2018-03-02 15:31:10 +03:00
|
|
|
#endif
|
|
|
|
/* For now, mark unmigratable: */
|
|
|
|
cc->vmsd = &vmstate_riscv_cpu;
|
2019-04-20 05:24:01 +03:00
|
|
|
dc->props = riscv_cpu_properties;
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char *riscv_isa_string(RISCVCPU *cpu)
|
|
|
|
{
|
|
|
|
int i;
|
2018-03-20 00:18:49 +03:00
|
|
|
const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
|
|
|
|
char *isa_str = g_new(char, maxlen);
|
|
|
|
char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
|
2018-03-02 15:31:10 +03:00
|
|
|
for (i = 0; i < sizeof(riscv_exts); i++) {
|
|
|
|
if (cpu->env.misa & RV(riscv_exts[i])) {
|
2018-03-20 00:18:49 +03:00
|
|
|
*p++ = qemu_tolower(riscv_exts[i]);
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
}
|
2018-03-20 00:18:49 +03:00
|
|
|
*p = '\0';
|
|
|
|
return isa_str;
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 01:12:31 +03:00
|
|
|
static gint riscv_cpu_list_compare(gconstpointer a, gconstpointer b)
|
2018-03-02 15:31:10 +03:00
|
|
|
{
|
2018-03-09 01:12:31 +03:00
|
|
|
ObjectClass *class_a = (ObjectClass *)a;
|
|
|
|
ObjectClass *class_b = (ObjectClass *)b;
|
|
|
|
const char *name_a, *name_b;
|
2018-03-02 15:31:10 +03:00
|
|
|
|
2018-03-09 01:12:31 +03:00
|
|
|
name_a = object_class_get_name(class_a);
|
|
|
|
name_b = object_class_get_name(class_b);
|
|
|
|
return strcmp(name_a, name_b);
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 01:12:31 +03:00
|
|
|
static void riscv_cpu_list_entry(gpointer data, gpointer user_data)
|
2018-03-02 15:31:10 +03:00
|
|
|
{
|
2018-03-09 01:12:31 +03:00
|
|
|
const char *typename = object_class_get_name(OBJECT_CLASS(data));
|
|
|
|
int len = strlen(typename) - strlen(RISCV_CPU_TYPE_SUFFIX);
|
2018-03-02 15:31:10 +03:00
|
|
|
|
2019-04-17 22:17:57 +03:00
|
|
|
qemu_printf("%.*s\n", len, typename);
|
2018-03-09 01:12:31 +03:00
|
|
|
}
|
2018-03-02 15:31:10 +03:00
|
|
|
|
2019-04-17 22:17:57 +03:00
|
|
|
void riscv_cpu_list(void)
|
2018-03-09 01:12:31 +03:00
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
|
|
|
|
list = object_class_get_list(TYPE_RISCV_CPU, false);
|
|
|
|
list = g_slist_sort(list, riscv_cpu_list_compare);
|
2019-04-17 22:17:57 +03:00
|
|
|
g_slist_foreach(list, riscv_cpu_list_entry, NULL);
|
2018-03-09 01:12:31 +03:00
|
|
|
g_slist_free(list);
|
2018-03-02 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 01:12:31 +03:00
|
|
|
#define DEFINE_CPU(type_name, initfn) \
|
|
|
|
{ \
|
|
|
|
.name = type_name, \
|
|
|
|
.parent = TYPE_RISCV_CPU, \
|
|
|
|
.instance_init = initfn \
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo riscv_cpu_type_infos[] = {
|
|
|
|
{
|
|
|
|
.name = TYPE_RISCV_CPU,
|
|
|
|
.parent = TYPE_CPU,
|
|
|
|
.instance_size = sizeof(RISCVCPU),
|
|
|
|
.instance_init = riscv_cpu_init,
|
|
|
|
.abstract = true,
|
|
|
|
.class_size = sizeof(RISCVCPUClass),
|
|
|
|
.class_init = riscv_cpu_class_init,
|
|
|
|
},
|
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_ANY, riscv_any_cpu_init),
|
|
|
|
#if defined(TARGET_RISCV32)
|
2019-04-20 05:24:09 +03:00
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_BASE32, riscv_base32_cpu_init),
|
2018-03-09 01:12:31 +03:00
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31, rv32imacu_nommu_cpu_init),
|
2019-06-18 04:31:02 +03:00
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34, rv32gcsu_priv1_10_0_cpu_init),
|
|
|
|
/* Depreacted */
|
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_RV32IMACU_NOMMU, rv32imacu_nommu_cpu_init),
|
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_RV32GCSU_V1_09_1, rv32gcsu_priv1_09_1_cpu_init),
|
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_RV32GCSU_V1_10_0, rv32gcsu_priv1_10_0_cpu_init)
|
2018-03-09 01:12:31 +03:00
|
|
|
#elif defined(TARGET_RISCV64)
|
2019-04-20 05:24:09 +03:00
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_BASE64, riscv_base64_cpu_init),
|
2018-03-09 01:12:31 +03:00
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E51, rv64imacu_nommu_cpu_init),
|
2019-06-18 04:31:02 +03:00
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U54, rv64gcsu_priv1_10_0_cpu_init),
|
|
|
|
/* Deprecated */
|
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_RV64IMACU_NOMMU, rv64imacu_nommu_cpu_init),
|
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_RV64GCSU_V1_09_1, rv64gcsu_priv1_09_1_cpu_init),
|
|
|
|
DEFINE_CPU(TYPE_RISCV_CPU_RV64GCSU_V1_10_0, rv64gcsu_priv1_10_0_cpu_init)
|
2018-03-09 01:12:31 +03:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_TYPES(riscv_cpu_type_infos)
|