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