]> git.proxmox.com Git - mirror_qemu.git/blame - target/riscv/cpu.h
target/riscv: add zicbop extension flag
[mirror_qemu.git] / target / riscv / cpu.h
CommitLineData
dc5bd18f
MC
1/*
2 * QEMU RISC-V CPU
3 *
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2017-2018 SiFive, Inc.
6 *
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.
10 *
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
14 * more details.
15 *
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/>.
18 */
19
20#ifndef RISCV_CPU_H
21#define RISCV_CPU_H
22
2e5b09fd 23#include "hw/core/cpu.h"
2b7168fc 24#include "hw/registerfields.h"
32fa1776 25#include "hw/qdev-properties.h"
dc5bd18f 26#include "exec/cpu-defs.h"
69242e7e 27#include "qemu/cpu-float.h"
db1015e9 28#include "qom/object.h"
961738ff 29#include "qemu/int128.h"
e91a7227 30#include "cpu_bits.h"
b902ff29 31#include "cpu_cfg.h"
6f23aaeb 32#include "qapi/qapi-types-common.h"
85840bd2 33#include "cpu-qom.h"
dc5bd18f 34
9348028e
PMD
35typedef struct CPUArchState CPURISCVState;
36
66125f93
PMD
37#define CPU_RESOLVING_TYPE TYPE_RISCV_CPU
38
27a6e78e
PMD
39#if defined(TARGET_RISCV32)
40# define TYPE_RISCV_CPU_BASE TYPE_RISCV_CPU_BASE32
41#elif defined(TARGET_RISCV64)
42# define TYPE_RISCV_CPU_BASE TYPE_RISCV_CPU_BASE64
43#endif
44
74433bf0
RH
45#define TCG_GUEST_DEFAULT_MO 0
46
62cf0245
AP
47/*
48 * RISC-V-specific extra insn start words:
49 * 1: Original instruction opcode
50 */
51#define TARGET_INSN_START_EXTRA_WORDS 1
52
dc5bd18f
MC
53#define RV(x) ((target_ulong)1 << (x - 'A'))
54
ed7e6182 55/*
efa365b7 56 * Update misa_bits[], misa_ext_info_arr[] and misa_ext_cfgs[]
ed7e6182
DHB
57 * when adding new MISA bits here.
58 */
dc5bd18f 59#define RVI RV('I')
79f86934 60#define RVE RV('E') /* E and I are mutually exclusive */
dc5bd18f
MC
61#define RVM RV('M')
62#define RVA RV('A')
63#define RVF RV('F')
64#define RVD RV('D')
ad9e5aa2 65#define RVV RV('V')
dc5bd18f
MC
66#define RVC RV('C')
67#define RVS RV('S')
68#define RVU RV('U')
af1fa003 69#define RVH RV('H')
53dcea58 70#define RVJ RV('J')
4f13abcb 71#define RVG RV('G')
dc5bd18f 72
efa365b7 73extern const uint32_t misa_bits[];
ed7e6182
DHB
74const char *riscv_get_misa_ext_name(uint32_t bit);
75const char *riscv_get_misa_ext_description(uint32_t bit);
dc5bd18f 76
238fd586
DHB
77#define CPU_CFG_OFFSET(_prop) offsetof(struct RISCVCPUConfig, _prop)
78
a46d410c
AP
79/* Privileged specification version */
80enum {
81 PRIV_VERSION_1_10_0 = 0,
82 PRIV_VERSION_1_11_0,
3a4af26d 83 PRIV_VERSION_1_12_0,
b9a2b98e
DHB
84
85 PRIV_VERSION_LATEST = PRIV_VERSION_1_12_0,
a46d410c 86};
dc5bd18f 87
9ec6622d 88#define VEXT_VERSION_1_00_0 0x00010000
32931383 89
33a9a57d
YJ
90enum {
91 TRANSLATE_SUCCESS,
92 TRANSLATE_FAIL,
93 TRANSLATE_PMP_FAIL,
94 TRANSLATE_G_STAGE_FAIL
95};
96
42967f40
LZ
97/* Extension context status */
98typedef enum {
99 EXT_STATUS_DISABLED = 0,
100 EXT_STATUS_INITIAL,
101 EXT_STATUS_CLEAN,
102 EXT_STATUS_DIRTY,
103} RISCVExtStatus;
104
dc5bd18f
MC
105#define MMU_USER_IDX 3
106
107#define MAX_RISCV_PMPS (16)
108
bbf3d1b4 109#if !defined(CONFIG_USER_ONLY)
dc5bd18f 110#include "pmp.h"
95799e36 111#include "debug.h"
bbf3d1b4 112#endif
dc5bd18f 113
8a4b5257 114#define RV_VLEN_MAX 1024
3780e337 115#define RV_MAX_MHPMEVENTS 32
621f35bb 116#define RV_MAX_MHPMCOUNTERS 32
ad9e5aa2 117
33f1beaf
FC
118FIELD(VTYPE, VLMUL, 0, 3)
119FIELD(VTYPE, VSEW, 3, 3)
3479a814
FC
120FIELD(VTYPE, VTA, 6, 1)
121FIELD(VTYPE, VMA, 7, 1)
33f1beaf
FC
122FIELD(VTYPE, VEDIV, 8, 2)
123FIELD(VTYPE, RESERVED, 10, sizeof(target_ulong) * 8 - 11)
2b7168fc 124
3780e337
AP
125typedef struct PMUCTRState {
126 /* Current value of a counter */
127 target_ulong mhpmcounter_val;
3b57254d 128 /* Current value of a counter in RV32 */
3780e337
AP
129 target_ulong mhpmcounterh_val;
130 /* Snapshot values of counter */
131 target_ulong mhpmcounter_prev;
132 /* Snapshort value of a counter in RV32 */
133 target_ulong mhpmcounterh_prev;
134 bool started;
14664483
AP
135 /* Value beyond UINT32_MAX/UINT64_MAX before overflow interrupt trigger */
136 target_ulong irq_overflow_left;
3780e337
AP
137} PMUCTRState;
138
1ea4a06a 139struct CPUArchState {
dc5bd18f 140 target_ulong gpr[32];
2b547084 141 target_ulong gprh[32]; /* 64 top bits of the 128-bit registers */
ad9e5aa2
LZ
142
143 /* vector coprocessor state. */
144 uint64_t vreg[32 * RV_VLEN_MAX / 64] QEMU_ALIGNED(16);
145 target_ulong vxrm;
146 target_ulong vxsat;
147 target_ulong vl;
148 target_ulong vstart;
149 target_ulong vtype;
d96a271a 150 bool vill;
ad9e5aa2 151
dc5bd18f
MC
152 target_ulong pc;
153 target_ulong load_res;
154 target_ulong load_val;
155
04bc3027
PMD
156 /* Floating-Point state */
157 uint64_t fpr[32]; /* assume both F and D extensions */
dc5bd18f 158 target_ulong frm;
04bc3027 159 float_status fp_status;
dc5bd18f
MC
160
161 target_ulong badaddr;
62cf0245 162 target_ulong bins;
48eaeb56 163
36a18664 164 target_ulong guest_phys_fault_addr;
dc5bd18f 165
dc5bd18f 166 target_ulong priv_ver;
d2c1a177 167 target_ulong bext_ver;
32931383 168 target_ulong vext_ver;
e91a7227
RH
169
170 /* RISCVMXL, but uint32_t for vmstate migration */
171 uint32_t misa_mxl; /* current mxl */
172 uint32_t misa_mxl_max; /* max mxl for this cpu */
173 uint32_t misa_ext; /* current extensions */
174 uint32_t misa_ext_mask; /* max ext for this cpu */
440544e1 175 uint32_t xl; /* current xlen */
dc5bd18f 176
b3a5d1fb
FP
177 /* 128-bit helpers upper part return value */
178 target_ulong retxh;
179
ce3af0bb
WL
180 target_ulong jvt;
181
5836c3ec
KC
182#ifdef CONFIG_USER_ONLY
183 uint32_t elf_flags;
184#endif
185
dc5bd18f
MC
186#ifndef CONFIG_USER_ONLY
187 target_ulong priv;
ef6bb7b6 188 /* This contains QEMU specific information about the virt state. */
b3c5077b 189 bool virt_enabled;
cd032fe7 190 target_ulong geilen;
277b210d 191 uint64_t resetvec;
dc5bd18f
MC
192
193 target_ulong mhartid;
284d697c
YJ
194 /*
195 * For RV32 this is 32-bit mstatus and 32-bit mstatush.
196 * For RV64 this is a 64-bit mstatus.
197 */
198 uint64_t mstatus;
85ba724f 199
d028ac75 200 uint64_t mip;
33fe584f
AF
201 /*
202 * MIP contains the software writable version of SEIP ORed with the
203 * external interrupt value. The MIP register is always up-to-date.
204 * To keep track of the current source, we also save booleans of the values
205 * here.
206 */
207 bool external_seip;
208 bool software_seip;
66e594f2 209
d028ac75 210 uint64_t miclaim;
85ba724f 211
d028ac75
AP
212 uint64_t mie;
213 uint64_t mideleg;
dc5bd18f 214
1697837e
RK
215 /*
216 * When mideleg[i]=0 and mvien[i]=1, sie[i] is no more
3a4e5601 217 * alias of mie[i] and needs to be maintained separately.
1697837e
RK
218 */
219 uint64_t sie;
220
40336d5b
RK
221 /*
222 * When hideleg[i]=0 and hvien[i]=1, vsie[i] is no more
3a4e5601 223 * alias of sie[i] (mie[i]) and needs to be maintained separately.
40336d5b
RK
224 */
225 uint64_t vsie;
226
dc5bd18f 227 target_ulong satp; /* since: priv-1.10.0 */
ac12b601 228 target_ulong stval;
dc5bd18f
MC
229 target_ulong medeleg;
230
231 target_ulong stvec;
232 target_ulong sepc;
233 target_ulong scause;
234
235 target_ulong mtvec;
236 target_ulong mepc;
237 target_ulong mcause;
238 target_ulong mtval; /* since: priv-1.10.0 */
239
43dc93af
AP
240 /* Machine and Supervisor interrupt priorities */
241 uint8_t miprio[64];
242 uint8_t siprio[64];
243
d1ceff40
AP
244 /* AIA CSRs */
245 target_ulong miselect;
246 target_ulong siselect;
1697837e
RK
247 uint64_t mvien;
248 uint64_t mvip;
d1ceff40 249
bd023ce3
AF
250 /* Hypervisor CSRs */
251 target_ulong hstatus;
252 target_ulong hedeleg;
d028ac75 253 uint64_t hideleg;
bd023ce3
AF
254 target_ulong hcounteren;
255 target_ulong htval;
256 target_ulong htinst;
257 target_ulong hgatp;
cd032fe7
AP
258 target_ulong hgeie;
259 target_ulong hgeip;
c6957248 260 uint64_t htimedelta;
40336d5b
RK
261 uint64_t hvien;
262
263 /*
264 * Bits VSSIP, VSTIP and VSEIP in hvip are maintained in mip. Other bits
265 * from 0:12 are reserved. Bits 13:63 are not aliased and must be separately
266 * maintain in hvip.
267 */
268 uint64_t hvip;
bd023ce3 269
43dc93af 270 /* Hypervisor controlled virtual interrupt priorities */
2b602398 271 target_ulong hvictl;
43dc93af
AP
272 uint8_t hviprio[64];
273
2c64ab66
FP
274 /* Upper 64-bits of 128-bit CSRs */
275 uint64_t mscratchh;
276 uint64_t sscratchh;
277
bd023ce3 278 /* Virtual CSRs */
284d697c
YJ
279 /*
280 * For RV32 this is 32-bit vsstatus and 32-bit vsstatush.
281 * For RV64 this is a 64-bit vsstatus.
282 */
283 uint64_t vsstatus;
bd023ce3
AF
284 target_ulong vstvec;
285 target_ulong vsscratch;
286 target_ulong vsepc;
287 target_ulong vscause;
288 target_ulong vstval;
289 target_ulong vsatp;
290
d1ceff40
AP
291 /* AIA VS-mode CSRs */
292 target_ulong vsiselect;
293
bd023ce3
AF
294 target_ulong mtval2;
295 target_ulong mtinst;
296
66e594f2
AF
297 /* HS Backup CSRs */
298 target_ulong stvec_hs;
299 target_ulong sscratch_hs;
300 target_ulong sepc_hs;
301 target_ulong scause_hs;
302 target_ulong stval_hs;
303 target_ulong satp_hs;
284d697c 304 uint64_t mstatus_hs;
66e594f2 305
3b57254d
WL
306 /*
307 * Signals whether the current exception occurred with two-stage address
308 * translation active.
309 */
ec352d0c 310 bool two_stage_lookup;
8e2aa21b
AP
311 /*
312 * Signals whether the current exception occurred while doing two-stage
313 * address translation for the VS-stage page table walk.
314 */
315 bool two_stage_indirect_lookup;
ec352d0c 316
8c59f5c1
MC
317 target_ulong scounteren;
318 target_ulong mcounteren;
dc5bd18f 319
b1675eeb
AP
320 target_ulong mcountinhibit;
321
3780e337
AP
322 /* PMU counter state */
323 PMUCTRState pmu_ctrs[RV_MAX_MHPMCOUNTERS];
621f35bb 324
3b57254d 325 /* PMU event selector configured values. First three are unused */
621f35bb
AP
326 target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS];
327
3b57254d 328 /* PMU event selector configured values for RV32 */
14664483
AP
329 target_ulong mhpmeventh_val[RV_MAX_MHPMEVENTS];
330
dc5bd18f
MC
331 target_ulong sscratch;
332 target_ulong mscratch;
333
43888c2f
AP
334 /* Sstc CSRs */
335 uint64_t stimecmp;
336
3ec0fe18
AP
337 uint64_t vstimecmp;
338
dc5bd18f
MC
339 /* physical memory protection */
340 pmp_table_t pmp_state;
2582a95c 341 target_ulong mseccfg;
753e3fe2 342
95799e36
BM
343 /* trigger module */
344 target_ulong trigger_cur;
9495c488
FC
345 target_ulong tdata1[RV_MAX_TRIGGERS];
346 target_ulong tdata2[RV_MAX_TRIGGERS];
347 target_ulong tdata3[RV_MAX_TRIGGERS];
348 struct CPUBreakpoint *cpu_breakpoint[RV_MAX_TRIGGERS];
349 struct CPUWatchpoint *cpu_watchpoint[RV_MAX_TRIGGERS];
5a4ae64c
LZ
350 QEMUTimer *itrigger_timer[RV_MAX_TRIGGERS];
351 int64_t last_icount;
577f0286 352 bool itrigger_enabled;
95799e36 353
c6957248 354 /* machine specific rdtime callback */
e2f01f3c
FC
355 uint64_t (*rdtime_fn)(void *);
356 void *rdtime_fn_arg;
c6957248 357
69077dd6
AP
358 /* machine specific AIA ireg read-modify-write callback */
359#define AIA_MAKE_IREG(__isel, __priv, __virt, __vgein, __xlen) \
360 ((((__xlen) & 0xff) << 24) | \
361 (((__vgein) & 0x3f) << 20) | \
362 (((__virt) & 0x1) << 18) | \
363 (((__priv) & 0x3) << 16) | \
364 (__isel & 0xffff))
365#define AIA_IREG_ISEL(__ireg) ((__ireg) & 0xffff)
366#define AIA_IREG_PRIV(__ireg) (((__ireg) >> 16) & 0x3)
367#define AIA_IREG_VIRT(__ireg) (((__ireg) >> 18) & 0x1)
368#define AIA_IREG_VGEIN(__ireg) (((__ireg) >> 20) & 0x3f)
369#define AIA_IREG_XLEN(__ireg) (((__ireg) >> 24) & 0xff)
370 int (*aia_ireg_rmw_fn[4])(void *arg, target_ulong reg,
371 target_ulong *val, target_ulong new_val, target_ulong write_mask);
372 void *aia_ireg_rmw_fn_arg[4];
373
753e3fe2
JW
374 /* True if in debugger mode. */
375 bool debugger;
4bbe8033
AB
376
377 /*
378 * CSRs for PointerMasking extension
379 */
380 target_ulong mmte;
381 target_ulong mpmmask;
382 target_ulong mpmbase;
383 target_ulong spmmask;
384 target_ulong spmbase;
385 target_ulong upmmask;
386 target_ulong upmbase;
29a9ec9b 387
42fe7499 388 /* CSRs for execution environment configuration */
29a9ec9b 389 uint64_t menvcfg;
3bee0e40
MC
390 uint64_t mstateen[SMSTATEEN_MAX_COUNT];
391 uint64_t hstateen[SMSTATEEN_MAX_COUNT];
392 uint64_t sstateen[SMSTATEEN_MAX_COUNT];
29a9ec9b
AP
393 target_ulong senvcfg;
394 uint64_t henvcfg;
dc5bd18f 395#endif
40bfa5f6
LZ
396 target_ulong cur_pmmask;
397 target_ulong cur_pmbase;
dc5bd18f 398
dc5bd18f 399 /* Fields from here on are preserved across CPU reset. */
43888c2f 400 QEMUTimer *stimer; /* Internal timer for S-mode interrupt */
3ec0fe18
AP
401 QEMUTimer *vstimer; /* Internal timer for VS-mode interrupt */
402 bool vstime_irq;
ad40be27
YJ
403
404 hwaddr kernel_addr;
405 hwaddr fdt_addr;
27abe66f 406
9638cbde 407#ifdef CONFIG_KVM
27abe66f
YJ
408 /* kvm timer */
409 bool kvm_timer_dirty;
410 uint64_t kvm_timer_time;
411 uint64_t kvm_timer_compare;
412 uint64_t kvm_timer_state;
413 uint64_t kvm_timer_frequency;
9638cbde 414#endif /* CONFIG_KVM */
dc5bd18f
MC
415};
416
3b57254d 417/*
dc5bd18f
MC
418 * RISCVCPU:
419 * @env: #CPURISCVState
420 *
421 * A RISCV CPU.
422 */
b36e239e 423struct ArchCPU {
dc5bd18f 424 CPUState parent_obj;
3b3d7df5 425
dc5bd18f 426 CPURISCVState env;
c4e95030 427
b93777e1 428 char *dyn_csr_xml;
719d3561 429 char *dyn_vreg_xml;
b93777e1 430
c4e95030 431 /* Configuration Settings */
466292bd 432 RISCVCPUConfig cfg;
14664483
AP
433
434 QEMUTimer *pmu_timer;
435 /* A bitmask of Available programmable counters */
436 uint32_t pmu_avail_ctrs;
437 /* Mapping of events to counters */
438 GHashTable *pmu_event_ctr_map;
db1015e9 439};
dc5bd18f 440
9348028e
PMD
441/**
442 * RISCVCPUClass:
443 * @parent_realize: The parent class' realize handler.
444 * @parent_phases: The parent class' reset phase handlers.
445 *
446 * A RISCV CPU model.
447 */
448struct RISCVCPUClass {
449 CPUClass parent_class;
450
451 DeviceRealize parent_realize;
452 ResettablePhases parent_phases;
453};
454
dc5bd18f
MC
455static inline int riscv_has_ext(CPURISCVState *env, target_ulong ext)
456{
e91a7227 457 return (env->misa_ext & ext) != 0;
dc5bd18f
MC
458}
459
dc5bd18f 460#include "cpu_user.h"
dc5bd18f
MC
461
462extern const char * const riscv_int_regnames[];
2b547084 463extern const char * const riscv_int_regnamesh[];
dc5bd18f 464extern const char * const riscv_fpr_regnames[];
dc5bd18f 465
c51a3f5d 466const char *riscv_cpu_get_trap_name(target_ulong cause, bool async);
dc5bd18f 467void riscv_cpu_do_interrupt(CPUState *cpu);
43a96588 468int riscv_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
1af0006a 469 int cpuid, DumpState *s);
43a96588 470int riscv_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs,
1af0006a 471 int cpuid, DumpState *s);
a010bdbe 472int riscv_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
dc5bd18f 473int riscv_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
43dc93af
AP
474int riscv_cpu_hviprio_index2irq(int index, int *out_irq, int *out_rdzero);
475uint8_t riscv_cpu_default_priority(int irq);
8f42415f 476uint64_t riscv_cpu_all_pending(CPURISCVState *env);
43dc93af
AP
477int riscv_cpu_mirq_pending(CPURISCVState *env);
478int riscv_cpu_sirq_pending(CPURISCVState *env);
479int riscv_cpu_vsirq_pending(CPURISCVState *env);
b345b480 480bool riscv_cpu_fp_enabled(CPURISCVState *env);
cd032fe7
AP
481target_ulong riscv_cpu_get_geilen(CPURISCVState *env);
482void riscv_cpu_set_geilen(CPURISCVState *env, target_ulong geilen);
61b4b69d 483bool riscv_cpu_vector_enabled(CPURISCVState *env);
ef6bb7b6 484void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable);
dc5bd18f 485int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch);
8905770b 486G_NORETURN void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
246f8796
WL
487 MMUAccessType access_type,
488 int mmu_idx, uintptr_t retaddr);
8a4ca3c1
RH
489bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
490 MMUAccessType access_type, int mmu_idx,
491 bool probe, uintptr_t retaddr);
dc5bd18f 492char *riscv_isa_string(RISCVCPU *cpu);
dc5bd18f 493
dc5bd18f
MC
494#define cpu_mmu_index riscv_cpu_mmu_index
495
85ba724f 496#ifndef CONFIG_USER_ONLY
d90ebc47
PMD
497void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
498 vaddr addr, unsigned size,
499 MMUAccessType access_type,
500 int mmu_idx, MemTxAttrs attrs,
501 MemTxResult response, uintptr_t retaddr);
6d2d454a 502hwaddr riscv_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
17b3c353 503bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request);
66e594f2 504void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env);
d028ac75 505int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint64_t interrupts);
bbb9fc25
WL
506uint64_t riscv_cpu_update_mip(CPURISCVState *env, uint64_t mask,
507 uint64_t value);
1ebad505 508void riscv_cpu_interrupt(CPURISCVState *env);
85ba724f 509#define BOOL_TO_MASK(x) (-!!(x)) /* helper for riscv_cpu_update_mip value */
e2f01f3c
FC
510void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(void *),
511 void *arg);
69077dd6
AP
512void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env, uint32_t priv,
513 int (*rmw_fn)(void *arg,
514 target_ulong reg,
515 target_ulong *val,
516 target_ulong new_val,
517 target_ulong write_mask),
518 void *rmw_fn_arg);
ce3af0bb
WL
519
520RISCVException smstateen_acc_ok(CPURISCVState *env, int index, uint64_t bit);
85ba724f 521#endif
fb738839 522void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv);
dc5bd18f
MC
523
524void riscv_translate_init(void);
8905770b
MAL
525G_NORETURN void riscv_raise_exception(CPURISCVState *env,
526 uint32_t exception, uintptr_t pc);
dc5bd18f 527
fb738839
MC
528target_ulong riscv_cpu_get_fflags(CPURISCVState *env);
529void riscv_cpu_set_fflags(CPURISCVState *env, target_ulong);
dc5bd18f 530
2b7168fc
LZ
531#include "exec/cpu-all.h"
532
61d56494 533FIELD(TB_FLAGS, MEM_IDX, 0, 3)
ebd47648
LZ
534FIELD(TB_FLAGS, FS, 3, 2)
535/* Vector flags */
536FIELD(TB_FLAGS, VS, 5, 2)
537FIELD(TB_FLAGS, LMUL, 7, 3)
538FIELD(TB_FLAGS, SEW, 10, 3)
539FIELD(TB_FLAGS, VL_EQ_VLMAX, 13, 1)
540FIELD(TB_FLAGS, VILL, 14, 1)
0f58cbbe 541FIELD(TB_FLAGS, VSTART_EQ_ZERO, 15, 1)
92371bd9 542/* The combination of MXL/SXL/UXL that applies to the current cpu mode. */
25f3ddff 543FIELD(TB_FLAGS, XL, 16, 2)
0774a7a1 544/* If PointerMasking should be applied */
25f3ddff
RH
545FIELD(TB_FLAGS, PM_MASK_ENABLED, 18, 1)
546FIELD(TB_FLAGS, PM_BASE_ENABLED, 19, 1)
547FIELD(TB_FLAGS, VTA, 20, 1)
548FIELD(TB_FLAGS, VMA, 21, 1)
2c9d7471 549/* Native debug itrigger */
25f3ddff 550FIELD(TB_FLAGS, ITRIGGER, 22, 1)
f1966390 551/* Virtual mode enabled */
25f3ddff 552FIELD(TB_FLAGS, VIRT_ENABLED, 23, 1)
0f58cbbe 553FIELD(TB_FLAGS, PRIV, 24, 2)
3a610f54 554FIELD(TB_FLAGS, AXL, 26, 2)
2b7168fc 555
db23e5d9
RH
556#ifdef TARGET_RISCV32
557#define riscv_cpu_mxl(env) ((void)(env), MXL_RV32)
558#else
559static inline RISCVMXL riscv_cpu_mxl(CPURISCVState *env)
560{
561 return env->misa_mxl;
562}
563#endif
2b602398 564#define riscv_cpu_mxl_bits(env) (1UL << (4 + riscv_cpu_mxl(env)))
51ae0cab 565
d4ea7117
DHB
566static inline const RISCVCPUConfig *riscv_cpu_cfg(CPURISCVState *env)
567{
568 return &env_archcpu(env)->cfg;
569}
570
3a610f54
WL
571#if !defined(CONFIG_USER_ONLY)
572static inline int cpu_address_mode(CPURISCVState *env)
573{
574 int mode = env->priv;
575
576 if (mode == PRV_M && get_field(env->mstatus, MSTATUS_MPRV)) {
577 mode = get_field(env->mstatus, MSTATUS_MPP);
578 }
579 return mode;
580}
581
582static inline RISCVMXL cpu_get_xl(CPURISCVState *env, target_ulong mode)
440544e1
LZ
583{
584 RISCVMXL xl = env->misa_mxl;
440544e1
LZ
585 /*
586 * When emulating a 32-bit-only cpu, use RV32.
587 * When emulating a 64-bit cpu, and MXL has been reduced to RV32,
588 * MSTATUSH doesn't have UXL/SXL, therefore XLEN cannot be widened
589 * back to RV64 for lower privs.
590 */
591 if (xl != MXL_RV32) {
3a610f54 592 switch (mode) {
440544e1
LZ
593 case PRV_M:
594 break;
595 case PRV_U:
596 xl = get_field(env->mstatus, MSTATUS64_UXL);
597 break;
44b8f74b 598 default: /* PRV_S */
440544e1
LZ
599 xl = get_field(env->mstatus, MSTATUS64_SXL);
600 break;
601 }
602 }
440544e1
LZ
603 return xl;
604}
605#endif
606
3a610f54
WL
607#if defined(TARGET_RISCV32)
608#define cpu_recompute_xl(env) ((void)(env), MXL_RV32)
609#else
610static inline RISCVMXL cpu_recompute_xl(CPURISCVState *env)
611{
612#if !defined(CONFIG_USER_ONLY)
613 return cpu_get_xl(env, env->priv);
614#else
615 return env->misa_mxl;
616#endif
617}
618#endif
619
620#if defined(TARGET_RISCV32)
621#define cpu_address_xl(env) ((void)(env), MXL_RV32)
622#else
623static inline RISCVMXL cpu_address_xl(CPURISCVState *env)
624{
625#ifdef CONFIG_USER_ONLY
626 return env->xl;
627#else
628 int mode = cpu_address_mode(env);
629
630 return cpu_get_xl(env, mode);
631#endif
632}
633#endif
634
31961cfe
LZ
635static inline int riscv_cpu_xlen(CPURISCVState *env)
636{
637 return 16 << env->xl;
638}
639
05e6ca5e
GR
640#ifdef TARGET_RISCV32
641#define riscv_cpu_sxl(env) ((void)(env), MXL_RV32)
642#else
643static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
644{
645#ifdef CONFIG_USER_ONLY
646 return env->misa_mxl;
647#else
648 return get_field(env->mstatus, MSTATUS64_SXL);
649#endif
650}
651#endif
652
2b7168fc 653/*
a689a82b
FC
654 * Encode LMUL to lmul as follows:
655 * LMUL vlmul lmul
656 * 1 000 0
657 * 2 001 1
658 * 4 010 2
659 * 8 011 3
660 * - 100 -
661 * 1/8 101 -3
662 * 1/4 110 -2
663 * 1/2 111 -1
664 *
665 * then, we can calculate VLMAX = vlen >> (vsew + 3 - lmul)
666 * e.g. vlen = 256 bits, SEW = 16, LMUL = 1/8
667 * => VLMAX = vlen >> (1 + 3 - (-3))
668 * = 256 >> 7
669 * = 2
2b7168fc
LZ
670 */
671static inline uint32_t vext_get_vlmax(RISCVCPU *cpu, target_ulong vtype)
672{
a689a82b
FC
673 uint8_t sew = FIELD_EX64(vtype, VTYPE, VSEW);
674 int8_t lmul = sextract32(FIELD_EX64(vtype, VTYPE, VLMUL), 0, 3);
2b7168fc
LZ
675 return cpu->cfg.vlen >> (sew + 3 - lmul);
676}
677
bb5de525
AJ
678void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc,
679 uint64_t *cs_base, uint32_t *pflags);
dc5bd18f 680
40bfa5f6
LZ
681void riscv_cpu_update_mask(CPURISCVState *env);
682
533c91e8
AF
683RISCVException riscv_csrrw(CPURISCVState *env, int csrno,
684 target_ulong *ret_value,
685 target_ulong new_value, target_ulong write_mask);
686RISCVException riscv_csrrw_debug(CPURISCVState *env, int csrno,
687 target_ulong *ret_value,
688 target_ulong new_value,
689 target_ulong write_mask);
c7b95171 690
fb738839
MC
691static inline void riscv_csr_write(CPURISCVState *env, int csrno,
692 target_ulong val)
c7b95171
MC
693{
694 riscv_csrrw(env, csrno, NULL, val, MAKE_64BIT_MASK(0, TARGET_LONG_BITS));
695}
696
fb738839 697static inline target_ulong riscv_csr_read(CPURISCVState *env, int csrno)
c7b95171
MC
698{
699 target_ulong val = 0;
700 riscv_csrrw(env, csrno, &val, 0, 0);
701 return val;
702}
703
0e62f92e
AF
704typedef RISCVException (*riscv_csr_predicate_fn)(CPURISCVState *env,
705 int csrno);
605def6e
AF
706typedef RISCVException (*riscv_csr_read_fn)(CPURISCVState *env, int csrno,
707 target_ulong *ret_value);
708typedef RISCVException (*riscv_csr_write_fn)(CPURISCVState *env, int csrno,
709 target_ulong new_value);
710typedef RISCVException (*riscv_csr_op_fn)(CPURISCVState *env, int csrno,
711 target_ulong *ret_value,
712 target_ulong new_value,
713 target_ulong write_mask);
c7b95171 714
961738ff
FP
715RISCVException riscv_csrrw_i128(CPURISCVState *env, int csrno,
716 Int128 *ret_value,
717 Int128 new_value, Int128 write_mask);
718
457c360f
FP
719typedef RISCVException (*riscv_csr_read128_fn)(CPURISCVState *env, int csrno,
720 Int128 *ret_value);
721typedef RISCVException (*riscv_csr_write128_fn)(CPURISCVState *env, int csrno,
722 Int128 new_value);
723
c7b95171 724typedef struct {
8ceac5dc 725 const char *name;
a88365c1 726 riscv_csr_predicate_fn predicate;
c7b95171
MC
727 riscv_csr_read_fn read;
728 riscv_csr_write_fn write;
729 riscv_csr_op_fn op;
457c360f
FP
730 riscv_csr_read128_fn read128;
731 riscv_csr_write128_fn write128;
a4b2fa43
AP
732 /* The default priv spec version should be PRIV_VERSION_1_10_0 (i.e 0) */
733 uint32_t min_priv_ver;
c7b95171
MC
734} riscv_csr_operations;
735
56118ee8
BM
736/* CSR function table constants */
737enum {
738 CSR_TABLE_SIZE = 0x1000
739};
740
3b57254d 741/*
14664483
AP
742 * The event id are encoded based on the encoding specified in the
743 * SBI specification v0.3
744 */
745
746enum riscv_pmu_event_idx {
747 RISCV_PMU_EVENT_HW_CPU_CYCLES = 0x01,
748 RISCV_PMU_EVENT_HW_INSTRUCTIONS = 0x02,
749 RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS = 0x10019,
750 RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS = 0x1001B,
751 RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS = 0x10021,
752};
753
36c1118d
DHB
754/* used by tcg/tcg-cpu.c*/
755void isa_ext_update_enabled(RISCVCPU *cpu, uint32_t ext_offset, bool en);
36c1118d 756bool isa_ext_is_enabled(RISCVCPU *cpu, uint32_t ext_offset);
f51d03b0 757void riscv_cpu_set_misa(CPURISCVState *env, RISCVMXL mxl, uint32_t ext);
36c1118d 758
32fa1776
DHB
759typedef struct RISCVCPUMultiExtConfig {
760 const char *name;
761 uint32_t offset;
762 bool enabled;
763} RISCVCPUMultiExtConfig;
764
765extern const RISCVCPUMultiExtConfig riscv_cpu_extensions[];
766extern const RISCVCPUMultiExtConfig riscv_cpu_vendor_exts[];
767extern const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[];
8043effd 768extern const RISCVCPUMultiExtConfig riscv_cpu_deprecated_exts[];
32fa1776
DHB
769extern Property riscv_cpu_options[];
770
7935e2c4
DHB
771typedef struct isa_ext_data {
772 const char *name;
773 int min_version;
774 int ext_enable_offset;
775} RISCVIsaExtData;
776extern const RISCVIsaExtData isa_edata_arr[];
b933720b 777char *riscv_cpu_get_name(RISCVCPU *cpu);
7935e2c4 778
a13a6082 779void riscv_cpu_finalize_features(RISCVCPU *cpu, Error **errp);
7d0c302c 780void riscv_add_satp_mode_properties(Object *obj);
ef58fad0 781bool riscv_cpu_accelerator_compatible(RISCVCPU *cpu);
32fa1776 782
56118ee8 783/* CSR function table */
6f03770d 784extern riscv_csr_operations csr_ops[CSR_TABLE_SIZE];
56118ee8 785
6f23aaeb
AG
786extern const bool valid_vm_1_10_32[], valid_vm_1_10_64[];
787
c7b95171
MC
788void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops);
789void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
dc5bd18f 790
5371f5cd
JW
791void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
792
6f23aaeb
AG
793uint8_t satp_mode_max_from_map(uint32_t map);
794const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
795
dc5bd18f 796#endif /* RISCV_CPU_H */