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