5 * Copyright (c) 2017-2018 SiFive, Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2 or later, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "hw/core/cpu.h"
24 #include "hw/registerfields.h"
25 #include "exec/cpu-defs.h"
26 #include "fpu/softfloat-types.h"
27 #include "qom/object.h"
29 #define TCG_GUEST_DEFAULT_MO 0
31 #define TYPE_RISCV_CPU "riscv-cpu"
33 #define RISCV_CPU_TYPE_SUFFIX "-" TYPE_RISCV_CPU
34 #define RISCV_CPU_TYPE_NAME(name) (name RISCV_CPU_TYPE_SUFFIX)
35 #define CPU_RESOLVING_TYPE TYPE_RISCV_CPU
37 #define TYPE_RISCV_CPU_ANY RISCV_CPU_TYPE_NAME("any")
38 #define TYPE_RISCV_CPU_BASE32 RISCV_CPU_TYPE_NAME("rv32")
39 #define TYPE_RISCV_CPU_BASE64 RISCV_CPU_TYPE_NAME("rv64")
40 #define TYPE_RISCV_CPU_IBEX RISCV_CPU_TYPE_NAME("lowrisc-ibex")
41 #define TYPE_RISCV_CPU_SHAKTI_C RISCV_CPU_TYPE_NAME("shakti-c")
42 #define TYPE_RISCV_CPU_SIFIVE_E31 RISCV_CPU_TYPE_NAME("sifive-e31")
43 #define TYPE_RISCV_CPU_SIFIVE_E34 RISCV_CPU_TYPE_NAME("sifive-e34")
44 #define TYPE_RISCV_CPU_SIFIVE_E51 RISCV_CPU_TYPE_NAME("sifive-e51")
45 #define TYPE_RISCV_CPU_SIFIVE_U34 RISCV_CPU_TYPE_NAME("sifive-u34")
46 #define TYPE_RISCV_CPU_SIFIVE_U54 RISCV_CPU_TYPE_NAME("sifive-u54")
48 #if defined(TARGET_RISCV32)
49 # define TYPE_RISCV_CPU_BASE TYPE_RISCV_CPU_BASE32
50 #elif defined(TARGET_RISCV64)
51 # define TYPE_RISCV_CPU_BASE TYPE_RISCV_CPU_BASE64
54 #define RV32 ((target_ulong)1 << (TARGET_LONG_BITS - 2))
55 #define RV64 ((target_ulong)2 << (TARGET_LONG_BITS - 2))
57 #define RV(x) ((target_ulong)1 << (x - 'A'))
60 #define RVE RV('E') /* E and I are mutually exclusive */
71 /* S extension denotes that Supervisor mode exists, however it is possible
72 to have a core that support S mode but does not have an MMU and there
73 is currently no bit in misa to indicate whether an MMU exists or not
74 so a cpu features bitfield is required, likewise for optional PMP support */
82 #define PRIV_VERSION_1_10_0 0x00011000
83 #define PRIV_VERSION_1_11_0 0x00011100
85 #define VEXT_VERSION_0_07_1 0x00000701
91 TRANSLATE_G_STAGE_FAIL
94 #define MMU_USER_IDX 3
96 #define MAX_RISCV_PMPS (16)
98 typedef struct CPURISCVState CPURISCVState;
100 #if !defined(CONFIG_USER_ONLY)
104 #define RV_VLEN_MAX 256
106 FIELD(VTYPE, VLMUL, 0, 2)
107 FIELD(VTYPE, VSEW, 2, 3)
108 FIELD(VTYPE, VEDIV, 5, 2)
109 FIELD(VTYPE, RESERVED, 7, sizeof(target_ulong) * 8 - 9)
110 FIELD(VTYPE, VILL, sizeof(target_ulong) * 8 - 1, 1)
112 struct CPURISCVState {
113 target_ulong gpr[32];
114 uint64_t fpr[32]; /* assume both F and D extensions */
116 /* vector coprocessor state. */
117 uint64_t vreg[32 * RV_VLEN_MAX / 64] QEMU_ALIGNED(16);
125 target_ulong load_res;
126 target_ulong load_val;
130 target_ulong badaddr;
131 target_ulong guest_phys_fault_addr;
133 target_ulong priv_ver;
134 target_ulong bext_ver;
135 target_ulong vext_ver;
137 target_ulong misa_mask;
141 #ifdef CONFIG_USER_ONLY
145 #ifndef CONFIG_USER_ONLY
147 /* This contains QEMU specific information about the virt state. */
149 target_ulong resetvec;
151 target_ulong mhartid;
153 * For RV32 this is 32-bit mstatus and 32-bit mstatush.
154 * For RV64 this is a 64-bit mstatus.
163 target_ulong mideleg;
165 target_ulong satp; /* since: priv-1.10.0 */
167 target_ulong medeleg;
176 target_ulong mtval; /* since: priv-1.10.0 */
178 /* Hypervisor CSRs */
179 target_ulong hstatus;
180 target_ulong hedeleg;
181 target_ulong hideleg;
182 target_ulong hcounteren;
190 * For RV32 this is 32-bit vsstatus and 32-bit vsstatush.
191 * For RV64 this is a 64-bit vsstatus.
195 target_ulong vsscratch;
197 target_ulong vscause;
205 target_ulong stvec_hs;
206 target_ulong sscratch_hs;
207 target_ulong sepc_hs;
208 target_ulong scause_hs;
209 target_ulong stval_hs;
210 target_ulong satp_hs;
213 /* Signals whether the current exception occurred with two-stage address
214 translation active. */
215 bool two_stage_lookup;
217 target_ulong scounteren;
218 target_ulong mcounteren;
220 target_ulong sscratch;
221 target_ulong mscratch;
223 /* temporary htif regs */
228 /* physical memory protection */
229 pmp_table_t pmp_state;
230 target_ulong mseccfg;
232 /* machine specific rdtime callback */
233 uint64_t (*rdtime_fn)(uint32_t);
234 uint32_t rdtime_fn_arg;
236 /* True if in debugger mode. */
240 float_status fp_status;
242 /* Fields from here on are preserved across CPU reset. */
243 QEMUTimer *timer; /* Internal timer */
246 OBJECT_DECLARE_TYPE(RISCVCPU, RISCVCPUClass,
251 * @parent_realize: The parent class' realize handler.
252 * @parent_reset: The parent class' reset handler.
256 struct RISCVCPUClass {
258 CPUClass parent_class;
260 DeviceRealize parent_realize;
261 DeviceReset parent_reset;
266 * @env: #CPURISCVState
274 CPUNegativeOffsetState neg;
279 /* Configuration Settings */
314 static inline int riscv_has_ext(CPURISCVState *env, target_ulong ext)
316 return (env->misa & ext) != 0;
319 static inline bool riscv_feature(CPURISCVState *env, int feature)
321 return env->features & (1ULL << feature);
324 #include "cpu_user.h"
325 #include "cpu_bits.h"
327 extern const char * const riscv_int_regnames[];
328 extern const char * const riscv_fpr_regnames[];
330 const char *riscv_cpu_get_trap_name(target_ulong cause, bool async);
331 void riscv_cpu_do_interrupt(CPUState *cpu);
332 int riscv_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
333 int cpuid, void *opaque);
334 int riscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
335 int cpuid, void *opaque);
336 int riscv_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
337 int riscv_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
338 bool riscv_cpu_fp_enabled(CPURISCVState *env);
339 bool riscv_cpu_virt_enabled(CPURISCVState *env);
340 void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable);
341 bool riscv_cpu_force_hs_excep_enabled(CPURISCVState *env);
342 void riscv_cpu_set_force_hs_excep(CPURISCVState *env, bool enable);
343 bool riscv_cpu_two_stage_lookup(int mmu_idx);
344 int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch);
345 hwaddr riscv_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
346 void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
347 MMUAccessType access_type, int mmu_idx,
348 uintptr_t retaddr) QEMU_NORETURN;
349 bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
350 MMUAccessType access_type, int mmu_idx,
351 bool probe, uintptr_t retaddr);
352 void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
353 vaddr addr, unsigned size,
354 MMUAccessType access_type,
355 int mmu_idx, MemTxAttrs attrs,
356 MemTxResult response, uintptr_t retaddr);
357 char *riscv_isa_string(RISCVCPU *cpu);
358 void riscv_cpu_list(void);
360 #define cpu_list riscv_cpu_list
361 #define cpu_mmu_index riscv_cpu_mmu_index
363 #ifndef CONFIG_USER_ONLY
364 bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request);
365 void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env);
366 int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint32_t interrupts);
367 uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value);
368 #define BOOL_TO_MASK(x) (-!!(x)) /* helper for riscv_cpu_update_mip value */
369 void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(uint32_t),
372 void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv);
374 void riscv_translate_init(void);
375 void QEMU_NORETURN riscv_raise_exception(CPURISCVState *env,
376 uint32_t exception, uintptr_t pc);
378 target_ulong riscv_cpu_get_fflags(CPURISCVState *env);
379 void riscv_cpu_set_fflags(CPURISCVState *env, target_ulong);
381 #define TB_FLAGS_MMU_MASK 7
382 #define TB_FLAGS_PRIV_MMU_MASK 3
383 #define TB_FLAGS_PRIV_HYP_ACCESS_MASK (1 << 2)
384 #define TB_FLAGS_MSTATUS_FS MSTATUS_FS
386 typedef CPURISCVState CPUArchState;
387 typedef RISCVCPU ArchCPU;
388 #include "exec/cpu-all.h"
390 FIELD(TB_FLAGS, VL_EQ_VLMAX, 2, 1)
391 FIELD(TB_FLAGS, LMUL, 3, 2)
392 FIELD(TB_FLAGS, SEW, 5, 3)
393 FIELD(TB_FLAGS, VILL, 8, 1)
394 /* Is a Hypervisor instruction load/store allowed? */
395 FIELD(TB_FLAGS, HLSX, 9, 1)
397 bool riscv_cpu_is_32bit(CPURISCVState *env);
400 * A simplification for VLMAX
401 * = (1 << LMUL) * VLEN / (8 * (1 << SEW))
402 * = (VLEN << LMUL) / (8 << SEW)
403 * = (VLEN << LMUL) >> (SEW + 3)
404 * = VLEN >> (SEW + 3 - LMUL)
406 static inline uint32_t vext_get_vlmax(RISCVCPU *cpu, target_ulong vtype)
410 sew = FIELD_EX64(vtype, VTYPE, VSEW);
411 lmul = FIELD_EX64(vtype, VTYPE, VLMUL);
412 return cpu->cfg.vlen >> (sew + 3 - lmul);
415 static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
416 target_ulong *cs_base, uint32_t *pflags)
423 if (riscv_has_ext(env, RVV)) {
424 uint32_t vlmax = vext_get_vlmax(env_archcpu(env), env->vtype);
425 bool vl_eq_vlmax = (env->vstart == 0) && (vlmax == env->vl);
426 flags = FIELD_DP32(flags, TB_FLAGS, VILL,
427 FIELD_EX64(env->vtype, VTYPE, VILL));
428 flags = FIELD_DP32(flags, TB_FLAGS, SEW,
429 FIELD_EX64(env->vtype, VTYPE, VSEW));
430 flags = FIELD_DP32(flags, TB_FLAGS, LMUL,
431 FIELD_EX64(env->vtype, VTYPE, VLMUL));
432 flags = FIELD_DP32(flags, TB_FLAGS, VL_EQ_VLMAX, vl_eq_vlmax);
434 flags = FIELD_DP32(flags, TB_FLAGS, VILL, 1);
437 #ifdef CONFIG_USER_ONLY
438 flags |= TB_FLAGS_MSTATUS_FS;
440 flags |= cpu_mmu_index(env, 0);
441 if (riscv_cpu_fp_enabled(env)) {
442 flags |= env->mstatus & MSTATUS_FS;
445 if (riscv_has_ext(env, RVH)) {
446 if (env->priv == PRV_M ||
447 (env->priv == PRV_S && !riscv_cpu_virt_enabled(env)) ||
448 (env->priv == PRV_U && !riscv_cpu_virt_enabled(env) &&
449 get_field(env->hstatus, HSTATUS_HU))) {
450 flags = FIELD_DP32(flags, TB_FLAGS, HLSX, 1);
458 RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
459 target_ulong *ret_value,
460 target_ulong new_value, target_ulong write_mask);
461 RISCVException riscv_csrrw_debug(CPURISCVState *env, int csrno,
462 target_ulong *ret_value,
463 target_ulong new_value,
464 target_ulong write_mask);
466 static inline void riscv_csr_write(CPURISCVState *env, int csrno,
469 riscv_csrrw(env, csrno, NULL, val, MAKE_64BIT_MASK(0, TARGET_LONG_BITS));
472 static inline target_ulong riscv_csr_read(CPURISCVState *env, int csrno)
474 target_ulong val = 0;
475 riscv_csrrw(env, csrno, &val, 0, 0);
479 typedef RISCVException (*riscv_csr_predicate_fn)(CPURISCVState *env,
481 typedef RISCVException (*riscv_csr_read_fn)(CPURISCVState *env, int csrno,
482 target_ulong *ret_value);
483 typedef RISCVException (*riscv_csr_write_fn)(CPURISCVState *env, int csrno,
484 target_ulong new_value);
485 typedef RISCVException (*riscv_csr_op_fn)(CPURISCVState *env, int csrno,
486 target_ulong *ret_value,
487 target_ulong new_value,
488 target_ulong write_mask);
492 riscv_csr_predicate_fn predicate;
493 riscv_csr_read_fn read;
494 riscv_csr_write_fn write;
496 } riscv_csr_operations;
498 /* CSR function table constants */
500 CSR_TABLE_SIZE = 0x1000
503 /* CSR function table */
504 extern riscv_csr_operations csr_ops[CSR_TABLE_SIZE];
506 void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops);
507 void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
509 void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
511 #endif /* RISCV_CPU_H */