4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
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 "qemu/cpu-float.h"
27 #include "qom/object.h"
28 #include "qemu/int128.h"
30 #include "qapi/qapi-types-common.h"
33 #define TCG_GUEST_DEFAULT_MO 0
36 * RISC-V-specific extra insn start words:
37 * 1: Original instruction opcode
39 #define TARGET_INSN_START_EXTRA_WORDS 1
41 #define RV(x) ((target_ulong)1 << (x - 'A'))
43 /* Consider updating misa_ext_cfgs[] when adding new MISA bits here */
45 #define RVE RV('E') /* E and I are mutually exclusive */
59 /* Privileged specification version */
61 PRIV_VERSION_1_10_0
= 0,
66 #define VEXT_VERSION_1_00_0 0x00010000
72 TRANSLATE_G_STAGE_FAIL
75 /* Extension context status */
77 EXT_STATUS_DISABLED
= 0,
83 #define MMU_USER_IDX 3
85 #define MAX_RISCV_PMPS (16)
87 #if !defined(CONFIG_USER_ONLY)
92 #define RV_VLEN_MAX 1024
93 #define RV_MAX_MHPMEVENTS 32
94 #define RV_MAX_MHPMCOUNTERS 32
96 FIELD(VTYPE
, VLMUL
, 0, 3)
97 FIELD(VTYPE
, VSEW
, 3, 3)
98 FIELD(VTYPE
, VTA
, 6, 1)
99 FIELD(VTYPE
, VMA
, 7, 1)
100 FIELD(VTYPE
, VEDIV
, 8, 2)
101 FIELD(VTYPE
, RESERVED
, 10, sizeof(target_ulong
) * 8 - 11)
103 typedef struct PMUCTRState
{
104 /* Current value of a counter */
105 target_ulong mhpmcounter_val
;
106 /* Current value of a counter in RV32 */
107 target_ulong mhpmcounterh_val
;
108 /* Snapshot values of counter */
109 target_ulong mhpmcounter_prev
;
110 /* Snapshort value of a counter in RV32 */
111 target_ulong mhpmcounterh_prev
;
113 /* Value beyond UINT32_MAX/UINT64_MAX before overflow interrupt trigger */
114 target_ulong irq_overflow_left
;
117 struct CPUArchState
{
118 target_ulong gpr
[32];
119 target_ulong gprh
[32]; /* 64 top bits of the 128-bit registers */
121 /* vector coprocessor state. */
122 uint64_t vreg
[32 * RV_VLEN_MAX
/ 64] QEMU_ALIGNED(16);
131 target_ulong load_res
;
132 target_ulong load_val
;
134 /* Floating-Point state */
135 uint64_t fpr
[32]; /* assume both F and D extensions */
137 float_status fp_status
;
139 target_ulong badaddr
;
142 target_ulong guest_phys_fault_addr
;
144 target_ulong priv_ver
;
145 target_ulong bext_ver
;
146 target_ulong vext_ver
;
148 /* RISCVMXL, but uint32_t for vmstate migration */
149 uint32_t misa_mxl
; /* current mxl */
150 uint32_t misa_mxl_max
; /* max mxl for this cpu */
151 uint32_t misa_ext
; /* current extensions */
152 uint32_t misa_ext_mask
; /* max ext for this cpu */
153 uint32_t xl
; /* current xlen */
155 /* 128-bit helpers upper part return value */
160 #ifdef CONFIG_USER_ONLY
164 #ifndef CONFIG_USER_ONLY
166 /* This contains QEMU specific information about the virt state. */
171 target_ulong mhartid
;
173 * For RV32 this is 32-bit mstatus and 32-bit mstatush.
174 * For RV64 this is a 64-bit mstatus.
180 * MIP contains the software writable version of SEIP ORed with the
181 * external interrupt value. The MIP register is always up-to-date.
182 * To keep track of the current source, we also save booleans of the values
193 target_ulong satp
; /* since: priv-1.10.0 */
195 target_ulong medeleg
;
204 target_ulong mtval
; /* since: priv-1.10.0 */
206 /* Machine and Supervisor interrupt priorities */
211 target_ulong miselect
;
212 target_ulong siselect
;
214 /* Hypervisor CSRs */
215 target_ulong hstatus
;
216 target_ulong hedeleg
;
218 target_ulong hcounteren
;
226 /* Hypervisor controlled virtual interrupt priorities */
230 /* Upper 64-bits of 128-bit CSRs */
236 * For RV32 this is 32-bit vsstatus and 32-bit vsstatush.
237 * For RV64 this is a 64-bit vsstatus.
241 target_ulong vsscratch
;
243 target_ulong vscause
;
247 /* AIA VS-mode CSRs */
248 target_ulong vsiselect
;
254 target_ulong stvec_hs
;
255 target_ulong sscratch_hs
;
256 target_ulong sepc_hs
;
257 target_ulong scause_hs
;
258 target_ulong stval_hs
;
259 target_ulong satp_hs
;
263 * Signals whether the current exception occurred with two-stage address
264 * translation active.
266 bool two_stage_lookup
;
268 * Signals whether the current exception occurred while doing two-stage
269 * address translation for the VS-stage page table walk.
271 bool two_stage_indirect_lookup
;
273 target_ulong scounteren
;
274 target_ulong mcounteren
;
276 target_ulong mcountinhibit
;
278 /* PMU counter state */
279 PMUCTRState pmu_ctrs
[RV_MAX_MHPMCOUNTERS
];
281 /* PMU event selector configured values. First three are unused */
282 target_ulong mhpmevent_val
[RV_MAX_MHPMEVENTS
];
284 /* PMU event selector configured values for RV32 */
285 target_ulong mhpmeventh_val
[RV_MAX_MHPMEVENTS
];
287 target_ulong sscratch
;
288 target_ulong mscratch
;
295 /* physical memory protection */
296 pmp_table_t pmp_state
;
297 target_ulong mseccfg
;
300 target_ulong trigger_cur
;
301 target_ulong tdata1
[RV_MAX_TRIGGERS
];
302 target_ulong tdata2
[RV_MAX_TRIGGERS
];
303 target_ulong tdata3
[RV_MAX_TRIGGERS
];
304 struct CPUBreakpoint
*cpu_breakpoint
[RV_MAX_TRIGGERS
];
305 struct CPUWatchpoint
*cpu_watchpoint
[RV_MAX_TRIGGERS
];
306 QEMUTimer
*itrigger_timer
[RV_MAX_TRIGGERS
];
308 bool itrigger_enabled
;
310 /* machine specific rdtime callback */
311 uint64_t (*rdtime_fn
)(void *);
314 /* machine specific AIA ireg read-modify-write callback */
315 #define AIA_MAKE_IREG(__isel, __priv, __virt, __vgein, __xlen) \
316 ((((__xlen) & 0xff) << 24) | \
317 (((__vgein) & 0x3f) << 20) | \
318 (((__virt) & 0x1) << 18) | \
319 (((__priv) & 0x3) << 16) | \
321 #define AIA_IREG_ISEL(__ireg) ((__ireg) & 0xffff)
322 #define AIA_IREG_PRIV(__ireg) (((__ireg) >> 16) & 0x3)
323 #define AIA_IREG_VIRT(__ireg) (((__ireg) >> 18) & 0x1)
324 #define AIA_IREG_VGEIN(__ireg) (((__ireg) >> 20) & 0x3f)
325 #define AIA_IREG_XLEN(__ireg) (((__ireg) >> 24) & 0xff)
326 int (*aia_ireg_rmw_fn
[4])(void *arg
, target_ulong reg
,
327 target_ulong
*val
, target_ulong new_val
, target_ulong write_mask
);
328 void *aia_ireg_rmw_fn_arg
[4];
330 /* True if in debugger mode. */
334 * CSRs for PointerMasking extension
337 target_ulong mpmmask
;
338 target_ulong mpmbase
;
339 target_ulong spmmask
;
340 target_ulong spmbase
;
341 target_ulong upmmask
;
342 target_ulong upmbase
;
344 /* CSRs for execution enviornment configuration */
346 uint64_t mstateen
[SMSTATEEN_MAX_COUNT
];
347 uint64_t hstateen
[SMSTATEEN_MAX_COUNT
];
348 uint64_t sstateen
[SMSTATEEN_MAX_COUNT
];
349 target_ulong senvcfg
;
352 target_ulong cur_pmmask
;
353 target_ulong cur_pmbase
;
355 /* Fields from here on are preserved across CPU reset. */
356 QEMUTimer
*stimer
; /* Internal timer for S-mode interrupt */
357 QEMUTimer
*vstimer
; /* Internal timer for VS-mode interrupt */
364 bool kvm_timer_dirty
;
365 uint64_t kvm_timer_time
;
366 uint64_t kvm_timer_compare
;
367 uint64_t kvm_timer_state
;
368 uint64_t kvm_timer_frequency
;
372 * map is a 16-bit bitmap: the most significant set bit in map is the maximum
373 * satp mode that is supported. It may be chosen by the user and must respect
374 * what qemu implements (valid_1_10_32/64) and what the hw is capable of
375 * (supported bitmap below).
377 * init is a 16-bit bitmap used to make sure the user selected a correct
378 * configuration as per the specification.
380 * supported is a 16-bit bitmap used to reflect the hw capabilities.
383 uint16_t map
, init
, supported
;
386 struct RISCVCPUConfig
{
416 bool ext_zihintpause
;
446 /* Vendor-specific custom extensions */
451 bool ext_xtheadcondmov
;
452 bool ext_xtheadfmemidx
;
455 bool ext_xtheadmemidx
;
456 bool ext_xtheadmempair
;
458 bool ext_XVentanaCondOps
;
467 uint16_t cbom_blocksize
;
468 uint16_t cboz_blocksize
;
475 bool short_isa_string
;
477 #ifndef CONFIG_USER_ONLY
478 RISCVSATPMap satp_mode
;
482 typedef struct RISCVCPUConfig RISCVCPUConfig
;
486 * @env: #CPURISCVState
494 CPUNegativeOffsetState neg
;
500 /* Configuration Settings */
503 QEMUTimer
*pmu_timer
;
504 /* A bitmask of Available programmable counters */
505 uint32_t pmu_avail_ctrs
;
506 /* Mapping of events to counters */
507 GHashTable
*pmu_event_ctr_map
;
510 static inline int riscv_has_ext(CPURISCVState
*env
, target_ulong ext
)
512 return (env
->misa_ext
& ext
) != 0;
515 #include "cpu_user.h"
517 extern const char * const riscv_int_regnames
[];
518 extern const char * const riscv_int_regnamesh
[];
519 extern const char * const riscv_fpr_regnames
[];
521 const char *riscv_cpu_get_trap_name(target_ulong cause
, bool async
);
522 void riscv_cpu_do_interrupt(CPUState
*cpu
);
523 int riscv_cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cs
,
524 int cpuid
, DumpState
*s
);
525 int riscv_cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cs
,
526 int cpuid
, DumpState
*s
);
527 int riscv_cpu_gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
);
528 int riscv_cpu_gdb_write_register(CPUState
*cpu
, uint8_t *buf
, int reg
);
529 int riscv_cpu_hviprio_index2irq(int index
, int *out_irq
, int *out_rdzero
);
530 uint8_t riscv_cpu_default_priority(int irq
);
531 uint64_t riscv_cpu_all_pending(CPURISCVState
*env
);
532 int riscv_cpu_mirq_pending(CPURISCVState
*env
);
533 int riscv_cpu_sirq_pending(CPURISCVState
*env
);
534 int riscv_cpu_vsirq_pending(CPURISCVState
*env
);
535 bool riscv_cpu_fp_enabled(CPURISCVState
*env
);
536 target_ulong
riscv_cpu_get_geilen(CPURISCVState
*env
);
537 void riscv_cpu_set_geilen(CPURISCVState
*env
, target_ulong geilen
);
538 bool riscv_cpu_vector_enabled(CPURISCVState
*env
);
539 void riscv_cpu_set_virt_enabled(CPURISCVState
*env
, bool enable
);
540 int riscv_cpu_mmu_index(CPURISCVState
*env
, bool ifetch
);
541 G_NORETURN
void riscv_cpu_do_unaligned_access(CPUState
*cs
, vaddr addr
,
542 MMUAccessType access_type
,
543 int mmu_idx
, uintptr_t retaddr
);
544 bool riscv_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
545 MMUAccessType access_type
, int mmu_idx
,
546 bool probe
, uintptr_t retaddr
);
547 char *riscv_isa_string(RISCVCPU
*cpu
);
548 void riscv_cpu_list(void);
550 #define cpu_list riscv_cpu_list
551 #define cpu_mmu_index riscv_cpu_mmu_index
553 #ifndef CONFIG_USER_ONLY
554 void riscv_cpu_do_transaction_failed(CPUState
*cs
, hwaddr physaddr
,
555 vaddr addr
, unsigned size
,
556 MMUAccessType access_type
,
557 int mmu_idx
, MemTxAttrs attrs
,
558 MemTxResult response
, uintptr_t retaddr
);
559 hwaddr
riscv_cpu_get_phys_page_debug(CPUState
*cpu
, vaddr addr
);
560 bool riscv_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
);
561 void riscv_cpu_swap_hypervisor_regs(CPURISCVState
*env
);
562 int riscv_cpu_claim_interrupts(RISCVCPU
*cpu
, uint64_t interrupts
);
563 uint64_t riscv_cpu_update_mip(CPURISCVState
*env
, uint64_t mask
,
565 #define BOOL_TO_MASK(x) (-!!(x)) /* helper for riscv_cpu_update_mip value */
566 void riscv_cpu_set_rdtime_fn(CPURISCVState
*env
, uint64_t (*fn
)(void *),
568 void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState
*env
, uint32_t priv
,
569 int (*rmw_fn
)(void *arg
,
572 target_ulong new_val
,
573 target_ulong write_mask
),
576 RISCVException
smstateen_acc_ok(CPURISCVState
*env
, int index
, uint64_t bit
);
578 void riscv_cpu_set_mode(CPURISCVState
*env
, target_ulong newpriv
);
580 void riscv_translate_init(void);
581 G_NORETURN
void riscv_raise_exception(CPURISCVState
*env
,
582 uint32_t exception
, uintptr_t pc
);
584 target_ulong
riscv_cpu_get_fflags(CPURISCVState
*env
);
585 void riscv_cpu_set_fflags(CPURISCVState
*env
, target_ulong
);
587 #include "exec/cpu-all.h"
589 FIELD(TB_FLAGS
, MEM_IDX
, 0, 3)
590 FIELD(TB_FLAGS
, FS
, 3, 2)
592 FIELD(TB_FLAGS
, VS
, 5, 2)
593 FIELD(TB_FLAGS
, LMUL
, 7, 3)
594 FIELD(TB_FLAGS
, SEW
, 10, 3)
595 FIELD(TB_FLAGS
, VL_EQ_VLMAX
, 13, 1)
596 FIELD(TB_FLAGS
, VILL
, 14, 1)
597 FIELD(TB_FLAGS
, VSTART_EQ_ZERO
, 15, 1)
598 /* The combination of MXL/SXL/UXL that applies to the current cpu mode. */
599 FIELD(TB_FLAGS
, XL
, 16, 2)
600 /* If PointerMasking should be applied */
601 FIELD(TB_FLAGS
, PM_MASK_ENABLED
, 18, 1)
602 FIELD(TB_FLAGS
, PM_BASE_ENABLED
, 19, 1)
603 FIELD(TB_FLAGS
, VTA
, 20, 1)
604 FIELD(TB_FLAGS
, VMA
, 21, 1)
605 /* Native debug itrigger */
606 FIELD(TB_FLAGS
, ITRIGGER
, 22, 1)
607 /* Virtual mode enabled */
608 FIELD(TB_FLAGS
, VIRT_ENABLED
, 23, 1)
609 FIELD(TB_FLAGS
, PRIV
, 24, 2)
611 #ifdef TARGET_RISCV32
612 #define riscv_cpu_mxl(env) ((void)(env), MXL_RV32)
614 static inline RISCVMXL
riscv_cpu_mxl(CPURISCVState
*env
)
616 return env
->misa_mxl
;
619 #define riscv_cpu_mxl_bits(env) (1UL << (4 + riscv_cpu_mxl(env)))
621 static inline const RISCVCPUConfig
*riscv_cpu_cfg(CPURISCVState
*env
)
623 return &env_archcpu(env
)->cfg
;
626 #if defined(TARGET_RISCV32)
627 #define cpu_recompute_xl(env) ((void)(env), MXL_RV32)
629 static inline RISCVMXL
cpu_recompute_xl(CPURISCVState
*env
)
631 RISCVMXL xl
= env
->misa_mxl
;
632 #if !defined(CONFIG_USER_ONLY)
634 * When emulating a 32-bit-only cpu, use RV32.
635 * When emulating a 64-bit cpu, and MXL has been reduced to RV32,
636 * MSTATUSH doesn't have UXL/SXL, therefore XLEN cannot be widened
637 * back to RV64 for lower privs.
639 if (xl
!= MXL_RV32
) {
644 xl
= get_field(env
->mstatus
, MSTATUS64_UXL
);
647 xl
= get_field(env
->mstatus
, MSTATUS64_SXL
);
656 static inline int riscv_cpu_xlen(CPURISCVState
*env
)
658 return 16 << env
->xl
;
661 #ifdef TARGET_RISCV32
662 #define riscv_cpu_sxl(env) ((void)(env), MXL_RV32)
664 static inline RISCVMXL
riscv_cpu_sxl(CPURISCVState
*env
)
666 #ifdef CONFIG_USER_ONLY
667 return env
->misa_mxl
;
669 return get_field(env
->mstatus
, MSTATUS64_SXL
);
675 * Encode LMUL to lmul as follows:
686 * then, we can calculate VLMAX = vlen >> (vsew + 3 - lmul)
687 * e.g. vlen = 256 bits, SEW = 16, LMUL = 1/8
688 * => VLMAX = vlen >> (1 + 3 - (-3))
692 static inline uint32_t vext_get_vlmax(RISCVCPU
*cpu
, target_ulong vtype
)
694 uint8_t sew
= FIELD_EX64(vtype
, VTYPE
, VSEW
);
695 int8_t lmul
= sextract32(FIELD_EX64(vtype
, VTYPE
, VLMUL
), 0, 3);
696 return cpu
->cfg
.vlen
>> (sew
+ 3 - lmul
);
699 void cpu_get_tb_cpu_state(CPURISCVState
*env
, target_ulong
*pc
,
700 target_ulong
*cs_base
, uint32_t *pflags
);
702 void riscv_cpu_update_mask(CPURISCVState
*env
);
704 RISCVException
riscv_csrrw(CPURISCVState
*env
, int csrno
,
705 target_ulong
*ret_value
,
706 target_ulong new_value
, target_ulong write_mask
);
707 RISCVException
riscv_csrrw_debug(CPURISCVState
*env
, int csrno
,
708 target_ulong
*ret_value
,
709 target_ulong new_value
,
710 target_ulong write_mask
);
712 static inline void riscv_csr_write(CPURISCVState
*env
, int csrno
,
715 riscv_csrrw(env
, csrno
, NULL
, val
, MAKE_64BIT_MASK(0, TARGET_LONG_BITS
));
718 static inline target_ulong
riscv_csr_read(CPURISCVState
*env
, int csrno
)
720 target_ulong val
= 0;
721 riscv_csrrw(env
, csrno
, &val
, 0, 0);
725 typedef RISCVException (*riscv_csr_predicate_fn
)(CPURISCVState
*env
,
727 typedef RISCVException (*riscv_csr_read_fn
)(CPURISCVState
*env
, int csrno
,
728 target_ulong
*ret_value
);
729 typedef RISCVException (*riscv_csr_write_fn
)(CPURISCVState
*env
, int csrno
,
730 target_ulong new_value
);
731 typedef RISCVException (*riscv_csr_op_fn
)(CPURISCVState
*env
, int csrno
,
732 target_ulong
*ret_value
,
733 target_ulong new_value
,
734 target_ulong write_mask
);
736 RISCVException
riscv_csrrw_i128(CPURISCVState
*env
, int csrno
,
738 Int128 new_value
, Int128 write_mask
);
740 typedef RISCVException (*riscv_csr_read128_fn
)(CPURISCVState
*env
, int csrno
,
742 typedef RISCVException (*riscv_csr_write128_fn
)(CPURISCVState
*env
, int csrno
,
747 riscv_csr_predicate_fn predicate
;
748 riscv_csr_read_fn read
;
749 riscv_csr_write_fn write
;
751 riscv_csr_read128_fn read128
;
752 riscv_csr_write128_fn write128
;
753 /* The default priv spec version should be PRIV_VERSION_1_10_0 (i.e 0) */
754 uint32_t min_priv_ver
;
755 } riscv_csr_operations
;
757 /* CSR function table constants */
759 CSR_TABLE_SIZE
= 0x1000
763 * The event id are encoded based on the encoding specified in the
764 * SBI specification v0.3
767 enum riscv_pmu_event_idx
{
768 RISCV_PMU_EVENT_HW_CPU_CYCLES
= 0x01,
769 RISCV_PMU_EVENT_HW_INSTRUCTIONS
= 0x02,
770 RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS
= 0x10019,
771 RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS
= 0x1001B,
772 RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS
= 0x10021,
775 /* CSR function table */
776 extern riscv_csr_operations csr_ops
[CSR_TABLE_SIZE
];
778 extern const bool valid_vm_1_10_32
[], valid_vm_1_10_64
[];
780 void riscv_get_csr_ops(int csrno
, riscv_csr_operations
*ops
);
781 void riscv_set_csr_ops(int csrno
, riscv_csr_operations
*ops
);
783 void riscv_cpu_register_gdb_regs_for_features(CPUState
*cs
);
785 uint8_t satp_mode_max_from_map(uint32_t map
);
786 const char *satp_mode_str(uint8_t satp_mode
, bool is_32_bit
);
788 #endif /* RISCV_CPU_H */