xref: /openbmc/qemu/target/riscv/cpu.c (revision e2fa85f4)
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 #include "qemu/osdep.h"
21 #include "qemu/qemu-print.h"
22 #include "qemu/ctype.h"
23 #include "qemu/log.h"
24 #include "cpu.h"
25 #include "cpu_vendorid.h"
26 #include "pmu.h"
27 #include "internals.h"
28 #include "time_helper.h"
29 #include "exec/exec-all.h"
30 #include "qapi/error.h"
31 #include "qapi/visitor.h"
32 #include "qemu/error-report.h"
33 #include "hw/qdev-properties.h"
34 #include "migration/vmstate.h"
35 #include "fpu/softfloat-helpers.h"
36 #include "sysemu/kvm.h"
37 #include "kvm_riscv.h"
38 #include "tcg/tcg.h"
39 
40 /* RISC-V CPU definitions */
41 
42 #define RISCV_CPU_MARCHID   ((QEMU_VERSION_MAJOR << 16) | \
43                              (QEMU_VERSION_MINOR << 8)  | \
44                              (QEMU_VERSION_MICRO))
45 #define RISCV_CPU_MIMPID    RISCV_CPU_MARCHID
46 
47 static const char riscv_single_letter_exts[] = "IEMAFDQCPVH";
48 
49 struct isa_ext_data {
50     const char *name;
51     int min_version;
52     int ext_enable_offset;
53 };
54 
55 #define ISA_EXT_DATA_ENTRY(_name, _min_ver, _prop) \
56     {#_name, _min_ver, offsetof(struct RISCVCPUConfig, _prop)}
57 
58 /*
59  * Here are the ordering rules of extension naming defined by RISC-V
60  * specification :
61  * 1. All extensions should be separated from other multi-letter extensions
62  *    by an underscore.
63  * 2. The first letter following the 'Z' conventionally indicates the most
64  *    closely related alphabetical extension category, IMAFDQLCBKJTPVH.
65  *    If multiple 'Z' extensions are named, they should be ordered first
66  *    by category, then alphabetically within a category.
67  * 3. Standard supervisor-level extensions (starts with 'S') should be
68  *    listed after standard unprivileged extensions.  If multiple
69  *    supervisor-level extensions are listed, they should be ordered
70  *    alphabetically.
71  * 4. Non-standard extensions (starts with 'X') must be listed after all
72  *    standard extensions. They must be separated from other multi-letter
73  *    extensions by an underscore.
74  *
75  * Single letter extensions are checked in riscv_cpu_validate_misa_priv()
76  * instead.
77  */
78 static const struct isa_ext_data isa_edata_arr[] = {
79     ISA_EXT_DATA_ENTRY(zicbom, PRIV_VERSION_1_12_0, ext_icbom),
80     ISA_EXT_DATA_ENTRY(zicboz, PRIV_VERSION_1_12_0, ext_icboz),
81     ISA_EXT_DATA_ENTRY(zicond, PRIV_VERSION_1_12_0, ext_zicond),
82     ISA_EXT_DATA_ENTRY(zicsr, PRIV_VERSION_1_10_0, ext_icsr),
83     ISA_EXT_DATA_ENTRY(zifencei, PRIV_VERSION_1_10_0, ext_ifencei),
84     ISA_EXT_DATA_ENTRY(zihintpause, PRIV_VERSION_1_10_0, ext_zihintpause),
85     ISA_EXT_DATA_ENTRY(zawrs, PRIV_VERSION_1_12_0, ext_zawrs),
86     ISA_EXT_DATA_ENTRY(zfh, PRIV_VERSION_1_11_0, ext_zfh),
87     ISA_EXT_DATA_ENTRY(zfhmin, PRIV_VERSION_1_11_0, ext_zfhmin),
88     ISA_EXT_DATA_ENTRY(zfinx, PRIV_VERSION_1_12_0, ext_zfinx),
89     ISA_EXT_DATA_ENTRY(zdinx, PRIV_VERSION_1_12_0, ext_zdinx),
90     ISA_EXT_DATA_ENTRY(zca, PRIV_VERSION_1_12_0, ext_zca),
91     ISA_EXT_DATA_ENTRY(zcb, PRIV_VERSION_1_12_0, ext_zcb),
92     ISA_EXT_DATA_ENTRY(zcf, PRIV_VERSION_1_12_0, ext_zcf),
93     ISA_EXT_DATA_ENTRY(zcd, PRIV_VERSION_1_12_0, ext_zcd),
94     ISA_EXT_DATA_ENTRY(zce, PRIV_VERSION_1_12_0, ext_zce),
95     ISA_EXT_DATA_ENTRY(zcmp, PRIV_VERSION_1_12_0, ext_zcmp),
96     ISA_EXT_DATA_ENTRY(zcmt, PRIV_VERSION_1_12_0, ext_zcmt),
97     ISA_EXT_DATA_ENTRY(zba, PRIV_VERSION_1_12_0, ext_zba),
98     ISA_EXT_DATA_ENTRY(zbb, PRIV_VERSION_1_12_0, ext_zbb),
99     ISA_EXT_DATA_ENTRY(zbc, PRIV_VERSION_1_12_0, ext_zbc),
100     ISA_EXT_DATA_ENTRY(zbkb, PRIV_VERSION_1_12_0, ext_zbkb),
101     ISA_EXT_DATA_ENTRY(zbkc, PRIV_VERSION_1_12_0, ext_zbkc),
102     ISA_EXT_DATA_ENTRY(zbkx, PRIV_VERSION_1_12_0, ext_zbkx),
103     ISA_EXT_DATA_ENTRY(zbs, PRIV_VERSION_1_12_0, ext_zbs),
104     ISA_EXT_DATA_ENTRY(zk, PRIV_VERSION_1_12_0, ext_zk),
105     ISA_EXT_DATA_ENTRY(zkn, PRIV_VERSION_1_12_0, ext_zkn),
106     ISA_EXT_DATA_ENTRY(zknd, PRIV_VERSION_1_12_0, ext_zknd),
107     ISA_EXT_DATA_ENTRY(zkne, PRIV_VERSION_1_12_0, ext_zkne),
108     ISA_EXT_DATA_ENTRY(zknh, PRIV_VERSION_1_12_0, ext_zknh),
109     ISA_EXT_DATA_ENTRY(zkr, PRIV_VERSION_1_12_0, ext_zkr),
110     ISA_EXT_DATA_ENTRY(zks, PRIV_VERSION_1_12_0, ext_zks),
111     ISA_EXT_DATA_ENTRY(zksed, PRIV_VERSION_1_12_0, ext_zksed),
112     ISA_EXT_DATA_ENTRY(zksh, PRIV_VERSION_1_12_0, ext_zksh),
113     ISA_EXT_DATA_ENTRY(zkt, PRIV_VERSION_1_12_0, ext_zkt),
114     ISA_EXT_DATA_ENTRY(zve32f, PRIV_VERSION_1_10_0, ext_zve32f),
115     ISA_EXT_DATA_ENTRY(zve64f, PRIV_VERSION_1_10_0, ext_zve64f),
116     ISA_EXT_DATA_ENTRY(zve64d, PRIV_VERSION_1_10_0, ext_zve64d),
117     ISA_EXT_DATA_ENTRY(zvfh, PRIV_VERSION_1_12_0, ext_zvfh),
118     ISA_EXT_DATA_ENTRY(zvfhmin, PRIV_VERSION_1_12_0, ext_zvfhmin),
119     ISA_EXT_DATA_ENTRY(zhinx, PRIV_VERSION_1_12_0, ext_zhinx),
120     ISA_EXT_DATA_ENTRY(zhinxmin, PRIV_VERSION_1_12_0, ext_zhinxmin),
121     ISA_EXT_DATA_ENTRY(smaia, PRIV_VERSION_1_12_0, ext_smaia),
122     ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
123     ISA_EXT_DATA_ENTRY(sscofpmf, PRIV_VERSION_1_12_0, ext_sscofpmf),
124     ISA_EXT_DATA_ENTRY(sstc, PRIV_VERSION_1_12_0, ext_sstc),
125     ISA_EXT_DATA_ENTRY(svadu, PRIV_VERSION_1_12_0, ext_svadu),
126     ISA_EXT_DATA_ENTRY(svinval, PRIV_VERSION_1_12_0, ext_svinval),
127     ISA_EXT_DATA_ENTRY(svnapot, PRIV_VERSION_1_12_0, ext_svnapot),
128     ISA_EXT_DATA_ENTRY(svpbmt, PRIV_VERSION_1_12_0, ext_svpbmt),
129     ISA_EXT_DATA_ENTRY(xtheadba, PRIV_VERSION_1_11_0, ext_xtheadba),
130     ISA_EXT_DATA_ENTRY(xtheadbb, PRIV_VERSION_1_11_0, ext_xtheadbb),
131     ISA_EXT_DATA_ENTRY(xtheadbs, PRIV_VERSION_1_11_0, ext_xtheadbs),
132     ISA_EXT_DATA_ENTRY(xtheadcmo, PRIV_VERSION_1_11_0, ext_xtheadcmo),
133     ISA_EXT_DATA_ENTRY(xtheadcondmov, PRIV_VERSION_1_11_0, ext_xtheadcondmov),
134     ISA_EXT_DATA_ENTRY(xtheadfmemidx, PRIV_VERSION_1_11_0, ext_xtheadfmemidx),
135     ISA_EXT_DATA_ENTRY(xtheadfmv, PRIV_VERSION_1_11_0, ext_xtheadfmv),
136     ISA_EXT_DATA_ENTRY(xtheadmac, PRIV_VERSION_1_11_0, ext_xtheadmac),
137     ISA_EXT_DATA_ENTRY(xtheadmemidx, PRIV_VERSION_1_11_0, ext_xtheadmemidx),
138     ISA_EXT_DATA_ENTRY(xtheadmempair, PRIV_VERSION_1_11_0, ext_xtheadmempair),
139     ISA_EXT_DATA_ENTRY(xtheadsync, PRIV_VERSION_1_11_0, ext_xtheadsync),
140     ISA_EXT_DATA_ENTRY(xventanacondops, PRIV_VERSION_1_12_0, ext_XVentanaCondOps),
141 };
142 
143 static bool isa_ext_is_enabled(RISCVCPU *cpu,
144                                const struct isa_ext_data *edata)
145 {
146     bool *ext_enabled = (void *)&cpu->cfg + edata->ext_enable_offset;
147 
148     return *ext_enabled;
149 }
150 
151 static void isa_ext_update_enabled(RISCVCPU *cpu,
152                                    const struct isa_ext_data *edata, bool en)
153 {
154     bool *ext_enabled = (void *)&cpu->cfg + edata->ext_enable_offset;
155 
156     *ext_enabled = en;
157 }
158 
159 const char * const riscv_int_regnames[] = {
160     "x0/zero", "x1/ra",  "x2/sp",  "x3/gp",  "x4/tp",  "x5/t0",   "x6/t1",
161     "x7/t2",   "x8/s0",  "x9/s1",  "x10/a0", "x11/a1", "x12/a2",  "x13/a3",
162     "x14/a4",  "x15/a5", "x16/a6", "x17/a7", "x18/s2", "x19/s3",  "x20/s4",
163     "x21/s5",  "x22/s6", "x23/s7", "x24/s8", "x25/s9", "x26/s10", "x27/s11",
164     "x28/t3",  "x29/t4", "x30/t5", "x31/t6"
165 };
166 
167 const char * const riscv_int_regnamesh[] = {
168     "x0h/zeroh", "x1h/rah",  "x2h/sph",   "x3h/gph",   "x4h/tph",  "x5h/t0h",
169     "x6h/t1h",   "x7h/t2h",  "x8h/s0h",   "x9h/s1h",   "x10h/a0h", "x11h/a1h",
170     "x12h/a2h",  "x13h/a3h", "x14h/a4h",  "x15h/a5h",  "x16h/a6h", "x17h/a7h",
171     "x18h/s2h",  "x19h/s3h", "x20h/s4h",  "x21h/s5h",  "x22h/s6h", "x23h/s7h",
172     "x24h/s8h",  "x25h/s9h", "x26h/s10h", "x27h/s11h", "x28h/t3h", "x29h/t4h",
173     "x30h/t5h",  "x31h/t6h"
174 };
175 
176 const char * const riscv_fpr_regnames[] = {
177     "f0/ft0",   "f1/ft1",  "f2/ft2",   "f3/ft3",   "f4/ft4",  "f5/ft5",
178     "f6/ft6",   "f7/ft7",  "f8/fs0",   "f9/fs1",   "f10/fa0", "f11/fa1",
179     "f12/fa2",  "f13/fa3", "f14/fa4",  "f15/fa5",  "f16/fa6", "f17/fa7",
180     "f18/fs2",  "f19/fs3", "f20/fs4",  "f21/fs5",  "f22/fs6", "f23/fs7",
181     "f24/fs8",  "f25/fs9", "f26/fs10", "f27/fs11", "f28/ft8", "f29/ft9",
182     "f30/ft10", "f31/ft11"
183 };
184 
185 static const char * const riscv_excp_names[] = {
186     "misaligned_fetch",
187     "fault_fetch",
188     "illegal_instruction",
189     "breakpoint",
190     "misaligned_load",
191     "fault_load",
192     "misaligned_store",
193     "fault_store",
194     "user_ecall",
195     "supervisor_ecall",
196     "hypervisor_ecall",
197     "machine_ecall",
198     "exec_page_fault",
199     "load_page_fault",
200     "reserved",
201     "store_page_fault",
202     "reserved",
203     "reserved",
204     "reserved",
205     "reserved",
206     "guest_exec_page_fault",
207     "guest_load_page_fault",
208     "reserved",
209     "guest_store_page_fault",
210 };
211 
212 static const char * const riscv_intr_names[] = {
213     "u_software",
214     "s_software",
215     "vs_software",
216     "m_software",
217     "u_timer",
218     "s_timer",
219     "vs_timer",
220     "m_timer",
221     "u_external",
222     "s_external",
223     "vs_external",
224     "m_external",
225     "reserved",
226     "reserved",
227     "reserved",
228     "reserved"
229 };
230 
231 static void riscv_cpu_add_user_properties(Object *obj);
232 
233 const char *riscv_cpu_get_trap_name(target_ulong cause, bool async)
234 {
235     if (async) {
236         return (cause < ARRAY_SIZE(riscv_intr_names)) ?
237                riscv_intr_names[cause] : "(unknown)";
238     } else {
239         return (cause < ARRAY_SIZE(riscv_excp_names)) ?
240                riscv_excp_names[cause] : "(unknown)";
241     }
242 }
243 
244 static void set_misa(CPURISCVState *env, RISCVMXL mxl, uint32_t ext)
245 {
246     env->misa_mxl_max = env->misa_mxl = mxl;
247     env->misa_ext_mask = env->misa_ext = ext;
248 }
249 
250 #ifndef CONFIG_USER_ONLY
251 static uint8_t satp_mode_from_str(const char *satp_mode_str)
252 {
253     if (!strncmp(satp_mode_str, "mbare", 5)) {
254         return VM_1_10_MBARE;
255     }
256 
257     if (!strncmp(satp_mode_str, "sv32", 4)) {
258         return VM_1_10_SV32;
259     }
260 
261     if (!strncmp(satp_mode_str, "sv39", 4)) {
262         return VM_1_10_SV39;
263     }
264 
265     if (!strncmp(satp_mode_str, "sv48", 4)) {
266         return VM_1_10_SV48;
267     }
268 
269     if (!strncmp(satp_mode_str, "sv57", 4)) {
270         return VM_1_10_SV57;
271     }
272 
273     if (!strncmp(satp_mode_str, "sv64", 4)) {
274         return VM_1_10_SV64;
275     }
276 
277     g_assert_not_reached();
278 }
279 
280 uint8_t satp_mode_max_from_map(uint32_t map)
281 {
282     /* map here has at least one bit set, so no problem with clz */
283     return 31 - __builtin_clz(map);
284 }
285 
286 const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit)
287 {
288     if (is_32_bit) {
289         switch (satp_mode) {
290         case VM_1_10_SV32:
291             return "sv32";
292         case VM_1_10_MBARE:
293             return "none";
294         }
295     } else {
296         switch (satp_mode) {
297         case VM_1_10_SV64:
298             return "sv64";
299         case VM_1_10_SV57:
300             return "sv57";
301         case VM_1_10_SV48:
302             return "sv48";
303         case VM_1_10_SV39:
304             return "sv39";
305         case VM_1_10_MBARE:
306             return "none";
307         }
308     }
309 
310     g_assert_not_reached();
311 }
312 
313 static void set_satp_mode_max_supported(RISCVCPU *cpu,
314                                         uint8_t satp_mode)
315 {
316     bool rv32 = riscv_cpu_mxl(&cpu->env) == MXL_RV32;
317     const bool *valid_vm = rv32 ? valid_vm_1_10_32 : valid_vm_1_10_64;
318 
319     for (int i = 0; i <= satp_mode; ++i) {
320         if (valid_vm[i]) {
321             cpu->cfg.satp_mode.supported |= (1 << i);
322         }
323     }
324 }
325 
326 /* Set the satp mode to the max supported */
327 static void set_satp_mode_default_map(RISCVCPU *cpu)
328 {
329     cpu->cfg.satp_mode.map = cpu->cfg.satp_mode.supported;
330 }
331 #endif
332 
333 static void riscv_any_cpu_init(Object *obj)
334 {
335     CPURISCVState *env = &RISCV_CPU(obj)->env;
336 #if defined(TARGET_RISCV32)
337     set_misa(env, MXL_RV32, RVI | RVM | RVA | RVF | RVD | RVC | RVU);
338 #elif defined(TARGET_RISCV64)
339     set_misa(env, MXL_RV64, RVI | RVM | RVA | RVF | RVD | RVC | RVU);
340 #endif
341 
342 #ifndef CONFIG_USER_ONLY
343     set_satp_mode_max_supported(RISCV_CPU(obj),
344         riscv_cpu_mxl(&RISCV_CPU(obj)->env) == MXL_RV32 ?
345         VM_1_10_SV32 : VM_1_10_SV57);
346 #endif
347 
348     env->priv_ver = PRIV_VERSION_LATEST;
349 }
350 
351 #if defined(TARGET_RISCV64)
352 static void rv64_base_cpu_init(Object *obj)
353 {
354     CPURISCVState *env = &RISCV_CPU(obj)->env;
355     /* We set this in the realise function */
356     set_misa(env, MXL_RV64, 0);
357     riscv_cpu_add_user_properties(obj);
358     /* Set latest version of privileged specification */
359     env->priv_ver = PRIV_VERSION_LATEST;
360 #ifndef CONFIG_USER_ONLY
361     set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV57);
362 #endif
363 }
364 
365 static void rv64_sifive_u_cpu_init(Object *obj)
366 {
367     CPURISCVState *env = &RISCV_CPU(obj)->env;
368     set_misa(env, MXL_RV64, RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
369     env->priv_ver = PRIV_VERSION_1_10_0;
370 #ifndef CONFIG_USER_ONLY
371     set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV39);
372 #endif
373 }
374 
375 static void rv64_sifive_e_cpu_init(Object *obj)
376 {
377     CPURISCVState *env = &RISCV_CPU(obj)->env;
378     RISCVCPU *cpu = RISCV_CPU(obj);
379 
380     set_misa(env, MXL_RV64, RVI | RVM | RVA | RVC | RVU);
381     env->priv_ver = PRIV_VERSION_1_10_0;
382     cpu->cfg.mmu = false;
383 #ifndef CONFIG_USER_ONLY
384     set_satp_mode_max_supported(cpu, VM_1_10_MBARE);
385 #endif
386 }
387 
388 static void rv64_thead_c906_cpu_init(Object *obj)
389 {
390     CPURISCVState *env = &RISCV_CPU(obj)->env;
391     RISCVCPU *cpu = RISCV_CPU(obj);
392 
393     set_misa(env, MXL_RV64, RVG | RVC | RVS | RVU);
394     env->priv_ver = PRIV_VERSION_1_11_0;
395 
396     cpu->cfg.ext_zfh = true;
397     cpu->cfg.mmu = true;
398     cpu->cfg.ext_xtheadba = true;
399     cpu->cfg.ext_xtheadbb = true;
400     cpu->cfg.ext_xtheadbs = true;
401     cpu->cfg.ext_xtheadcmo = true;
402     cpu->cfg.ext_xtheadcondmov = true;
403     cpu->cfg.ext_xtheadfmemidx = true;
404     cpu->cfg.ext_xtheadmac = true;
405     cpu->cfg.ext_xtheadmemidx = true;
406     cpu->cfg.ext_xtheadmempair = true;
407     cpu->cfg.ext_xtheadsync = true;
408 
409     cpu->cfg.mvendorid = THEAD_VENDOR_ID;
410 #ifndef CONFIG_USER_ONLY
411     set_satp_mode_max_supported(cpu, VM_1_10_SV39);
412 #endif
413 }
414 
415 static void rv64_veyron_v1_cpu_init(Object *obj)
416 {
417     CPURISCVState *env = &RISCV_CPU(obj)->env;
418     RISCVCPU *cpu = RISCV_CPU(obj);
419 
420     set_misa(env, MXL_RV64, RVG | RVC | RVS | RVU | RVH);
421     env->priv_ver = PRIV_VERSION_1_12_0;
422 
423     /* Enable ISA extensions */
424     cpu->cfg.mmu = true;
425     cpu->cfg.ext_icbom = true;
426     cpu->cfg.cbom_blocksize = 64;
427     cpu->cfg.cboz_blocksize = 64;
428     cpu->cfg.ext_icboz = true;
429     cpu->cfg.ext_smaia = true;
430     cpu->cfg.ext_ssaia = true;
431     cpu->cfg.ext_sscofpmf = true;
432     cpu->cfg.ext_sstc = true;
433     cpu->cfg.ext_svinval = true;
434     cpu->cfg.ext_svnapot = true;
435     cpu->cfg.ext_svpbmt = true;
436     cpu->cfg.ext_smstateen = true;
437     cpu->cfg.ext_zba = true;
438     cpu->cfg.ext_zbb = true;
439     cpu->cfg.ext_zbc = true;
440     cpu->cfg.ext_zbs = true;
441     cpu->cfg.ext_XVentanaCondOps = true;
442 
443     cpu->cfg.mvendorid = VEYRON_V1_MVENDORID;
444     cpu->cfg.marchid = VEYRON_V1_MARCHID;
445     cpu->cfg.mimpid = VEYRON_V1_MIMPID;
446 
447 #ifndef CONFIG_USER_ONLY
448     set_satp_mode_max_supported(cpu, VM_1_10_SV48);
449 #endif
450 }
451 
452 static void rv128_base_cpu_init(Object *obj)
453 {
454     if (qemu_tcg_mttcg_enabled()) {
455         /* Missing 128-bit aligned atomics */
456         error_report("128-bit RISC-V currently does not work with Multi "
457                      "Threaded TCG. Please use: -accel tcg,thread=single");
458         exit(EXIT_FAILURE);
459     }
460     CPURISCVState *env = &RISCV_CPU(obj)->env;
461     /* We set this in the realise function */
462     set_misa(env, MXL_RV128, 0);
463     riscv_cpu_add_user_properties(obj);
464     /* Set latest version of privileged specification */
465     env->priv_ver = PRIV_VERSION_LATEST;
466 #ifndef CONFIG_USER_ONLY
467     set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV57);
468 #endif
469 }
470 #else
471 static void rv32_base_cpu_init(Object *obj)
472 {
473     CPURISCVState *env = &RISCV_CPU(obj)->env;
474     /* We set this in the realise function */
475     set_misa(env, MXL_RV32, 0);
476     riscv_cpu_add_user_properties(obj);
477     /* Set latest version of privileged specification */
478     env->priv_ver = PRIV_VERSION_LATEST;
479 #ifndef CONFIG_USER_ONLY
480     set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV32);
481 #endif
482 }
483 
484 static void rv32_sifive_u_cpu_init(Object *obj)
485 {
486     CPURISCVState *env = &RISCV_CPU(obj)->env;
487     set_misa(env, MXL_RV32, RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
488     env->priv_ver = PRIV_VERSION_1_10_0;
489 #ifndef CONFIG_USER_ONLY
490     set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV32);
491 #endif
492 }
493 
494 static void rv32_sifive_e_cpu_init(Object *obj)
495 {
496     CPURISCVState *env = &RISCV_CPU(obj)->env;
497     RISCVCPU *cpu = RISCV_CPU(obj);
498 
499     set_misa(env, MXL_RV32, RVI | RVM | RVA | RVC | RVU);
500     env->priv_ver = PRIV_VERSION_1_10_0;
501     cpu->cfg.mmu = false;
502 #ifndef CONFIG_USER_ONLY
503     set_satp_mode_max_supported(cpu, VM_1_10_MBARE);
504 #endif
505 }
506 
507 static void rv32_ibex_cpu_init(Object *obj)
508 {
509     CPURISCVState *env = &RISCV_CPU(obj)->env;
510     RISCVCPU *cpu = RISCV_CPU(obj);
511 
512     set_misa(env, MXL_RV32, RVI | RVM | RVC | RVU);
513     env->priv_ver = PRIV_VERSION_1_11_0;
514     cpu->cfg.mmu = false;
515 #ifndef CONFIG_USER_ONLY
516     set_satp_mode_max_supported(cpu, VM_1_10_MBARE);
517 #endif
518     cpu->cfg.epmp = true;
519 }
520 
521 static void rv32_imafcu_nommu_cpu_init(Object *obj)
522 {
523     CPURISCVState *env = &RISCV_CPU(obj)->env;
524     RISCVCPU *cpu = RISCV_CPU(obj);
525 
526     set_misa(env, MXL_RV32, RVI | RVM | RVA | RVF | RVC | RVU);
527     env->priv_ver = PRIV_VERSION_1_10_0;
528     cpu->cfg.mmu = false;
529 #ifndef CONFIG_USER_ONLY
530     set_satp_mode_max_supported(cpu, VM_1_10_MBARE);
531 #endif
532 }
533 #endif
534 
535 #if defined(CONFIG_KVM)
536 static void riscv_host_cpu_init(Object *obj)
537 {
538     CPURISCVState *env = &RISCV_CPU(obj)->env;
539 #if defined(TARGET_RISCV32)
540     set_misa(env, MXL_RV32, 0);
541 #elif defined(TARGET_RISCV64)
542     set_misa(env, MXL_RV64, 0);
543 #endif
544     riscv_cpu_add_user_properties(obj);
545 }
546 #endif
547 
548 static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
549 {
550     ObjectClass *oc;
551     char *typename;
552     char **cpuname;
553 
554     cpuname = g_strsplit(cpu_model, ",", 1);
555     typename = g_strdup_printf(RISCV_CPU_TYPE_NAME("%s"), cpuname[0]);
556     oc = object_class_by_name(typename);
557     g_strfreev(cpuname);
558     g_free(typename);
559     if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) ||
560         object_class_is_abstract(oc)) {
561         return NULL;
562     }
563     return oc;
564 }
565 
566 static void riscv_cpu_dump_state(CPUState *cs, FILE *f, int flags)
567 {
568     RISCVCPU *cpu = RISCV_CPU(cs);
569     CPURISCVState *env = &cpu->env;
570     int i;
571 
572 #if !defined(CONFIG_USER_ONLY)
573     if (riscv_has_ext(env, RVH)) {
574         qemu_fprintf(f, " %s %d\n", "V      =  ", env->virt_enabled);
575     }
576 #endif
577     qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "pc      ", env->pc);
578 #ifndef CONFIG_USER_ONLY
579     {
580         static const int dump_csrs[] = {
581             CSR_MHARTID,
582             CSR_MSTATUS,
583             CSR_MSTATUSH,
584             /*
585              * CSR_SSTATUS is intentionally omitted here as its value
586              * can be figured out by looking at CSR_MSTATUS
587              */
588             CSR_HSTATUS,
589             CSR_VSSTATUS,
590             CSR_MIP,
591             CSR_MIE,
592             CSR_MIDELEG,
593             CSR_HIDELEG,
594             CSR_MEDELEG,
595             CSR_HEDELEG,
596             CSR_MTVEC,
597             CSR_STVEC,
598             CSR_VSTVEC,
599             CSR_MEPC,
600             CSR_SEPC,
601             CSR_VSEPC,
602             CSR_MCAUSE,
603             CSR_SCAUSE,
604             CSR_VSCAUSE,
605             CSR_MTVAL,
606             CSR_STVAL,
607             CSR_HTVAL,
608             CSR_MTVAL2,
609             CSR_MSCRATCH,
610             CSR_SSCRATCH,
611             CSR_SATP,
612             CSR_MMTE,
613             CSR_UPMBASE,
614             CSR_UPMMASK,
615             CSR_SPMBASE,
616             CSR_SPMMASK,
617             CSR_MPMBASE,
618             CSR_MPMMASK,
619         };
620 
621         for (int i = 0; i < ARRAY_SIZE(dump_csrs); ++i) {
622             int csrno = dump_csrs[i];
623             target_ulong val = 0;
624             RISCVException res = riscv_csrrw_debug(env, csrno, &val, 0, 0);
625 
626             /*
627              * Rely on the smode, hmode, etc, predicates within csr.c
628              * to do the filtering of the registers that are present.
629              */
630             if (res == RISCV_EXCP_NONE) {
631                 qemu_fprintf(f, " %-8s " TARGET_FMT_lx "\n",
632                              csr_ops[csrno].name, val);
633             }
634         }
635     }
636 #endif
637 
638     for (i = 0; i < 32; i++) {
639         qemu_fprintf(f, " %-8s " TARGET_FMT_lx,
640                      riscv_int_regnames[i], env->gpr[i]);
641         if ((i & 3) == 3) {
642             qemu_fprintf(f, "\n");
643         }
644     }
645     if (flags & CPU_DUMP_FPU) {
646         for (i = 0; i < 32; i++) {
647             qemu_fprintf(f, " %-8s %016" PRIx64,
648                          riscv_fpr_regnames[i], env->fpr[i]);
649             if ((i & 3) == 3) {
650                 qemu_fprintf(f, "\n");
651             }
652         }
653     }
654 }
655 
656 static void riscv_cpu_set_pc(CPUState *cs, vaddr value)
657 {
658     RISCVCPU *cpu = RISCV_CPU(cs);
659     CPURISCVState *env = &cpu->env;
660 
661     if (env->xl == MXL_RV32) {
662         env->pc = (int32_t)value;
663     } else {
664         env->pc = value;
665     }
666 }
667 
668 static vaddr riscv_cpu_get_pc(CPUState *cs)
669 {
670     RISCVCPU *cpu = RISCV_CPU(cs);
671     CPURISCVState *env = &cpu->env;
672 
673     /* Match cpu_get_tb_cpu_state. */
674     if (env->xl == MXL_RV32) {
675         return env->pc & UINT32_MAX;
676     }
677     return env->pc;
678 }
679 
680 static void riscv_cpu_synchronize_from_tb(CPUState *cs,
681                                           const TranslationBlock *tb)
682 {
683     RISCVCPU *cpu = RISCV_CPU(cs);
684     CPURISCVState *env = &cpu->env;
685     RISCVMXL xl = FIELD_EX32(tb->flags, TB_FLAGS, XL);
686 
687     tcg_debug_assert(!(cs->tcg_cflags & CF_PCREL));
688 
689     if (xl == MXL_RV32) {
690         env->pc = (int32_t) tb->pc;
691     } else {
692         env->pc = tb->pc;
693     }
694 }
695 
696 static bool riscv_cpu_has_work(CPUState *cs)
697 {
698 #ifndef CONFIG_USER_ONLY
699     RISCVCPU *cpu = RISCV_CPU(cs);
700     CPURISCVState *env = &cpu->env;
701     /*
702      * Definition of the WFI instruction requires it to ignore the privilege
703      * mode and delegation registers, but respect individual enables
704      */
705     return riscv_cpu_all_pending(env) != 0;
706 #else
707     return true;
708 #endif
709 }
710 
711 static void riscv_restore_state_to_opc(CPUState *cs,
712                                        const TranslationBlock *tb,
713                                        const uint64_t *data)
714 {
715     RISCVCPU *cpu = RISCV_CPU(cs);
716     CPURISCVState *env = &cpu->env;
717     RISCVMXL xl = FIELD_EX32(tb->flags, TB_FLAGS, XL);
718 
719     if (xl == MXL_RV32) {
720         env->pc = (int32_t)data[0];
721     } else {
722         env->pc = data[0];
723     }
724     env->bins = data[1];
725 }
726 
727 static void riscv_cpu_reset_hold(Object *obj)
728 {
729 #ifndef CONFIG_USER_ONLY
730     uint8_t iprio;
731     int i, irq, rdzero;
732 #endif
733     CPUState *cs = CPU(obj);
734     RISCVCPU *cpu = RISCV_CPU(cs);
735     RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu);
736     CPURISCVState *env = &cpu->env;
737 
738     if (mcc->parent_phases.hold) {
739         mcc->parent_phases.hold(obj);
740     }
741 #ifndef CONFIG_USER_ONLY
742     env->misa_mxl = env->misa_mxl_max;
743     env->priv = PRV_M;
744     env->mstatus &= ~(MSTATUS_MIE | MSTATUS_MPRV);
745     if (env->misa_mxl > MXL_RV32) {
746         /*
747          * The reset status of SXL/UXL is undefined, but mstatus is WARL
748          * and we must ensure that the value after init is valid for read.
749          */
750         env->mstatus = set_field(env->mstatus, MSTATUS64_SXL, env->misa_mxl);
751         env->mstatus = set_field(env->mstatus, MSTATUS64_UXL, env->misa_mxl);
752         if (riscv_has_ext(env, RVH)) {
753             env->vsstatus = set_field(env->vsstatus,
754                                       MSTATUS64_SXL, env->misa_mxl);
755             env->vsstatus = set_field(env->vsstatus,
756                                       MSTATUS64_UXL, env->misa_mxl);
757             env->mstatus_hs = set_field(env->mstatus_hs,
758                                         MSTATUS64_SXL, env->misa_mxl);
759             env->mstatus_hs = set_field(env->mstatus_hs,
760                                         MSTATUS64_UXL, env->misa_mxl);
761         }
762     }
763     env->mcause = 0;
764     env->miclaim = MIP_SGEIP;
765     env->pc = env->resetvec;
766     env->bins = 0;
767     env->two_stage_lookup = false;
768 
769     env->menvcfg = (cpu->cfg.ext_svpbmt ? MENVCFG_PBMTE : 0) |
770                    (cpu->cfg.ext_svadu ? MENVCFG_HADE : 0);
771     env->henvcfg = (cpu->cfg.ext_svpbmt ? HENVCFG_PBMTE : 0) |
772                    (cpu->cfg.ext_svadu ? HENVCFG_HADE : 0);
773 
774     /* Initialized default priorities of local interrupts. */
775     for (i = 0; i < ARRAY_SIZE(env->miprio); i++) {
776         iprio = riscv_cpu_default_priority(i);
777         env->miprio[i] = (i == IRQ_M_EXT) ? 0 : iprio;
778         env->siprio[i] = (i == IRQ_S_EXT) ? 0 : iprio;
779         env->hviprio[i] = 0;
780     }
781     i = 0;
782     while (!riscv_cpu_hviprio_index2irq(i, &irq, &rdzero)) {
783         if (!rdzero) {
784             env->hviprio[irq] = env->miprio[irq];
785         }
786         i++;
787     }
788     /* mmte is supposed to have pm.current hardwired to 1 */
789     env->mmte |= (EXT_STATUS_INITIAL | MMTE_M_PM_CURRENT);
790 #endif
791     env->xl = riscv_cpu_mxl(env);
792     riscv_cpu_update_mask(env);
793     cs->exception_index = RISCV_EXCP_NONE;
794     env->load_res = -1;
795     set_default_nan_mode(1, &env->fp_status);
796 
797 #ifndef CONFIG_USER_ONLY
798     if (cpu->cfg.debug) {
799         riscv_trigger_init(env);
800     }
801 
802     if (kvm_enabled()) {
803         kvm_riscv_reset_vcpu(cpu);
804     }
805 #endif
806 }
807 
808 static void riscv_cpu_disas_set_info(CPUState *s, disassemble_info *info)
809 {
810     RISCVCPU *cpu = RISCV_CPU(s);
811 
812     switch (riscv_cpu_mxl(&cpu->env)) {
813     case MXL_RV32:
814         info->print_insn = print_insn_riscv32;
815         break;
816     case MXL_RV64:
817         info->print_insn = print_insn_riscv64;
818         break;
819     case MXL_RV128:
820         info->print_insn = print_insn_riscv128;
821         break;
822     default:
823         g_assert_not_reached();
824     }
825 }
826 
827 static void riscv_cpu_validate_v(CPURISCVState *env, RISCVCPUConfig *cfg,
828                                  Error **errp)
829 {
830     int vext_version = VEXT_VERSION_1_00_0;
831 
832     if (!is_power_of_2(cfg->vlen)) {
833         error_setg(errp, "Vector extension VLEN must be power of 2");
834         return;
835     }
836     if (cfg->vlen > RV_VLEN_MAX || cfg->vlen < 128) {
837         error_setg(errp,
838                    "Vector extension implementation only supports VLEN "
839                    "in the range [128, %d]", RV_VLEN_MAX);
840         return;
841     }
842     if (!is_power_of_2(cfg->elen)) {
843         error_setg(errp, "Vector extension ELEN must be power of 2");
844         return;
845     }
846     if (cfg->elen > 64 || cfg->elen < 8) {
847         error_setg(errp,
848                    "Vector extension implementation only supports ELEN "
849                    "in the range [8, 64]");
850         return;
851     }
852     if (cfg->vext_spec) {
853         if (!g_strcmp0(cfg->vext_spec, "v1.0")) {
854             vext_version = VEXT_VERSION_1_00_0;
855         } else {
856             error_setg(errp, "Unsupported vector spec version '%s'",
857                        cfg->vext_spec);
858             return;
859         }
860     } else {
861         qemu_log("vector version is not specified, "
862                  "use the default value v1.0\n");
863     }
864     env->vext_ver = vext_version;
865 }
866 
867 static void riscv_cpu_validate_priv_spec(RISCVCPU *cpu, Error **errp)
868 {
869     CPURISCVState *env = &cpu->env;
870     int priv_version = -1;
871 
872     if (cpu->cfg.priv_spec) {
873         if (!g_strcmp0(cpu->cfg.priv_spec, "v1.12.0")) {
874             priv_version = PRIV_VERSION_1_12_0;
875         } else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.11.0")) {
876             priv_version = PRIV_VERSION_1_11_0;
877         } else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.10.0")) {
878             priv_version = PRIV_VERSION_1_10_0;
879         } else {
880             error_setg(errp,
881                        "Unsupported privilege spec version '%s'",
882                        cpu->cfg.priv_spec);
883             return;
884         }
885 
886         env->priv_ver = priv_version;
887     }
888 }
889 
890 static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu)
891 {
892     CPURISCVState *env = &cpu->env;
893     int i;
894 
895     /* Force disable extensions if priv spec version does not match */
896     for (i = 0; i < ARRAY_SIZE(isa_edata_arr); i++) {
897         if (isa_ext_is_enabled(cpu, &isa_edata_arr[i]) &&
898             (env->priv_ver < isa_edata_arr[i].min_version)) {
899             isa_ext_update_enabled(cpu, &isa_edata_arr[i], false);
900 #ifndef CONFIG_USER_ONLY
901             warn_report("disabling %s extension for hart 0x" TARGET_FMT_lx
902                         " because privilege spec version does not match",
903                         isa_edata_arr[i].name, env->mhartid);
904 #else
905             warn_report("disabling %s extension because "
906                         "privilege spec version does not match",
907                         isa_edata_arr[i].name);
908 #endif
909         }
910     }
911 }
912 
913 static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)
914 {
915     RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu);
916     CPUClass *cc = CPU_CLASS(mcc);
917     CPURISCVState *env = &cpu->env;
918 
919     /* Validate that MISA_MXL is set properly. */
920     switch (env->misa_mxl_max) {
921 #ifdef TARGET_RISCV64
922     case MXL_RV64:
923     case MXL_RV128:
924         cc->gdb_core_xml_file = "riscv-64bit-cpu.xml";
925         break;
926 #endif
927     case MXL_RV32:
928         cc->gdb_core_xml_file = "riscv-32bit-cpu.xml";
929         break;
930     default:
931         g_assert_not_reached();
932     }
933 
934     if (env->misa_mxl_max != env->misa_mxl) {
935         error_setg(errp, "misa_mxl_max must be equal to misa_mxl");
936         return;
937     }
938 }
939 
940 /*
941  * Check consistency between chosen extensions while setting
942  * cpu->cfg accordingly.
943  */
944 static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)
945 {
946     CPURISCVState *env = &cpu->env;
947     Error *local_err = NULL;
948 
949     /* Do some ISA extension error checking */
950     if (riscv_has_ext(env, RVG) &&
951         !(riscv_has_ext(env, RVI) && riscv_has_ext(env, RVM) &&
952           riscv_has_ext(env, RVA) && riscv_has_ext(env, RVF) &&
953           riscv_has_ext(env, RVD) &&
954           cpu->cfg.ext_icsr && cpu->cfg.ext_ifencei)) {
955         warn_report("Setting G will also set IMAFD_Zicsr_Zifencei");
956         cpu->cfg.ext_icsr = true;
957         cpu->cfg.ext_ifencei = true;
958 
959         env->misa_ext |= RVI | RVM | RVA | RVF | RVD;
960         env->misa_ext_mask = env->misa_ext;
961     }
962 
963     if (riscv_has_ext(env, RVI) && riscv_has_ext(env, RVE)) {
964         error_setg(errp,
965                    "I and E extensions are incompatible");
966         return;
967     }
968 
969     if (!riscv_has_ext(env, RVI) && !riscv_has_ext(env, RVE)) {
970         error_setg(errp,
971                    "Either I or E extension must be set");
972         return;
973     }
974 
975     if (riscv_has_ext(env, RVS) && !riscv_has_ext(env, RVU)) {
976         error_setg(errp,
977                    "Setting S extension without U extension is illegal");
978         return;
979     }
980 
981     if (riscv_has_ext(env, RVH) && !riscv_has_ext(env, RVI)) {
982         error_setg(errp,
983                    "H depends on an I base integer ISA with 32 x registers");
984         return;
985     }
986 
987     if (riscv_has_ext(env, RVH) && !riscv_has_ext(env, RVS)) {
988         error_setg(errp, "H extension implicitly requires S-mode");
989         return;
990     }
991 
992     if (riscv_has_ext(env, RVF) && !cpu->cfg.ext_icsr) {
993         error_setg(errp, "F extension requires Zicsr");
994         return;
995     }
996 
997     if ((cpu->cfg.ext_zawrs) && !riscv_has_ext(env, RVA)) {
998         error_setg(errp, "Zawrs extension requires A extension");
999         return;
1000     }
1001 
1002     if (cpu->cfg.ext_zfh) {
1003         cpu->cfg.ext_zfhmin = true;
1004     }
1005 
1006     if (cpu->cfg.ext_zfhmin && !riscv_has_ext(env, RVF)) {
1007         error_setg(errp, "Zfh/Zfhmin extensions require F extension");
1008         return;
1009     }
1010 
1011     if (riscv_has_ext(env, RVD) && !riscv_has_ext(env, RVF)) {
1012         error_setg(errp, "D extension requires F extension");
1013         return;
1014     }
1015 
1016     if (riscv_has_ext(env, RVV)) {
1017         riscv_cpu_validate_v(env, &cpu->cfg, &local_err);
1018         if (local_err != NULL) {
1019             error_propagate(errp, local_err);
1020             return;
1021         }
1022 
1023         /* The V vector extension depends on the Zve64d extension */
1024         cpu->cfg.ext_zve64d = true;
1025     }
1026 
1027     /* The Zve64d extension depends on the Zve64f extension */
1028     if (cpu->cfg.ext_zve64d) {
1029         cpu->cfg.ext_zve64f = true;
1030     }
1031 
1032     /* The Zve64f extension depends on the Zve32f extension */
1033     if (cpu->cfg.ext_zve64f) {
1034         cpu->cfg.ext_zve32f = true;
1035     }
1036 
1037     if (cpu->cfg.ext_zve64d && !riscv_has_ext(env, RVD)) {
1038         error_setg(errp, "Zve64d/V extensions require D extension");
1039         return;
1040     }
1041 
1042     if (cpu->cfg.ext_zve32f && !riscv_has_ext(env, RVF)) {
1043         error_setg(errp, "Zve32f/Zve64f extensions require F extension");
1044         return;
1045     }
1046 
1047     if (cpu->cfg.ext_zvfh) {
1048         cpu->cfg.ext_zvfhmin = true;
1049     }
1050 
1051     if (cpu->cfg.ext_zvfhmin && !cpu->cfg.ext_zve32f) {
1052         error_setg(errp, "Zvfh/Zvfhmin extensions require Zve32f extension");
1053         return;
1054     }
1055 
1056     if (cpu->cfg.ext_zvfh && !cpu->cfg.ext_zfhmin) {
1057         error_setg(errp, "Zvfh extensions requires Zfhmin extension");
1058         return;
1059     }
1060 
1061     /* Set the ISA extensions, checks should have happened above */
1062     if (cpu->cfg.ext_zhinx) {
1063         cpu->cfg.ext_zhinxmin = true;
1064     }
1065 
1066     if ((cpu->cfg.ext_zdinx || cpu->cfg.ext_zhinxmin) && !cpu->cfg.ext_zfinx) {
1067         error_setg(errp, "Zdinx/Zhinx/Zhinxmin extensions require Zfinx");
1068         return;
1069     }
1070 
1071     if (cpu->cfg.ext_zfinx) {
1072         if (!cpu->cfg.ext_icsr) {
1073             error_setg(errp, "Zfinx extension requires Zicsr");
1074             return;
1075         }
1076         if (riscv_has_ext(env, RVF)) {
1077             error_setg(errp,
1078                        "Zfinx cannot be supported together with F extension");
1079             return;
1080         }
1081     }
1082 
1083     if (cpu->cfg.ext_zce) {
1084         cpu->cfg.ext_zca = true;
1085         cpu->cfg.ext_zcb = true;
1086         cpu->cfg.ext_zcmp = true;
1087         cpu->cfg.ext_zcmt = true;
1088         if (riscv_has_ext(env, RVF) && env->misa_mxl_max == MXL_RV32) {
1089             cpu->cfg.ext_zcf = true;
1090         }
1091     }
1092 
1093     if (riscv_has_ext(env, RVC)) {
1094         cpu->cfg.ext_zca = true;
1095         if (riscv_has_ext(env, RVF) && env->misa_mxl_max == MXL_RV32) {
1096             cpu->cfg.ext_zcf = true;
1097         }
1098         if (riscv_has_ext(env, RVD)) {
1099             cpu->cfg.ext_zcd = true;
1100         }
1101     }
1102 
1103     if (env->misa_mxl_max != MXL_RV32 && cpu->cfg.ext_zcf) {
1104         error_setg(errp, "Zcf extension is only relevant to RV32");
1105         return;
1106     }
1107 
1108     if (!riscv_has_ext(env, RVF) && cpu->cfg.ext_zcf) {
1109         error_setg(errp, "Zcf extension requires F extension");
1110         return;
1111     }
1112 
1113     if (!riscv_has_ext(env, RVD) && cpu->cfg.ext_zcd) {
1114         error_setg(errp, "Zcd extension requires D extension");
1115         return;
1116     }
1117 
1118     if ((cpu->cfg.ext_zcf || cpu->cfg.ext_zcd || cpu->cfg.ext_zcb ||
1119          cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt) && !cpu->cfg.ext_zca) {
1120         error_setg(errp, "Zcf/Zcd/Zcb/Zcmp/Zcmt extensions require Zca "
1121                          "extension");
1122         return;
1123     }
1124 
1125     if (cpu->cfg.ext_zcd && (cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt)) {
1126         error_setg(errp, "Zcmp/Zcmt extensions are incompatible with "
1127                          "Zcd extension");
1128         return;
1129     }
1130 
1131     if (cpu->cfg.ext_zcmt && !cpu->cfg.ext_icsr) {
1132         error_setg(errp, "Zcmt extension requires Zicsr extension");
1133         return;
1134     }
1135 
1136     if (cpu->cfg.ext_zk) {
1137         cpu->cfg.ext_zkn = true;
1138         cpu->cfg.ext_zkr = true;
1139         cpu->cfg.ext_zkt = true;
1140     }
1141 
1142     if (cpu->cfg.ext_zkn) {
1143         cpu->cfg.ext_zbkb = true;
1144         cpu->cfg.ext_zbkc = true;
1145         cpu->cfg.ext_zbkx = true;
1146         cpu->cfg.ext_zkne = true;
1147         cpu->cfg.ext_zknd = true;
1148         cpu->cfg.ext_zknh = true;
1149     }
1150 
1151     if (cpu->cfg.ext_zks) {
1152         cpu->cfg.ext_zbkb = true;
1153         cpu->cfg.ext_zbkc = true;
1154         cpu->cfg.ext_zbkx = true;
1155         cpu->cfg.ext_zksed = true;
1156         cpu->cfg.ext_zksh = true;
1157     }
1158 
1159     /*
1160      * Disable isa extensions based on priv spec after we
1161      * validated and set everything we need.
1162      */
1163     riscv_cpu_disable_priv_spec_isa_exts(cpu);
1164 }
1165 
1166 #ifndef CONFIG_USER_ONLY
1167 static void riscv_cpu_satp_mode_finalize(RISCVCPU *cpu, Error **errp)
1168 {
1169     bool rv32 = riscv_cpu_mxl(&cpu->env) == MXL_RV32;
1170     uint8_t satp_mode_map_max;
1171     uint8_t satp_mode_supported_max =
1172                         satp_mode_max_from_map(cpu->cfg.satp_mode.supported);
1173 
1174     if (cpu->cfg.satp_mode.map == 0) {
1175         if (cpu->cfg.satp_mode.init == 0) {
1176             /* If unset by the user, we fallback to the default satp mode. */
1177             set_satp_mode_default_map(cpu);
1178         } else {
1179             /*
1180              * Find the lowest level that was disabled and then enable the
1181              * first valid level below which can be found in
1182              * valid_vm_1_10_32/64.
1183              */
1184             for (int i = 1; i < 16; ++i) {
1185                 if ((cpu->cfg.satp_mode.init & (1 << i)) &&
1186                     (cpu->cfg.satp_mode.supported & (1 << i))) {
1187                     for (int j = i - 1; j >= 0; --j) {
1188                         if (cpu->cfg.satp_mode.supported & (1 << j)) {
1189                             cpu->cfg.satp_mode.map |= (1 << j);
1190                             break;
1191                         }
1192                     }
1193                     break;
1194                 }
1195             }
1196         }
1197     }
1198 
1199     satp_mode_map_max = satp_mode_max_from_map(cpu->cfg.satp_mode.map);
1200 
1201     /* Make sure the user asked for a supported configuration (HW and qemu) */
1202     if (satp_mode_map_max > satp_mode_supported_max) {
1203         error_setg(errp, "satp_mode %s is higher than hw max capability %s",
1204                    satp_mode_str(satp_mode_map_max, rv32),
1205                    satp_mode_str(satp_mode_supported_max, rv32));
1206         return;
1207     }
1208 
1209     /*
1210      * Make sure the user did not ask for an invalid configuration as per
1211      * the specification.
1212      */
1213     if (!rv32) {
1214         for (int i = satp_mode_map_max - 1; i >= 0; --i) {
1215             if (!(cpu->cfg.satp_mode.map & (1 << i)) &&
1216                 (cpu->cfg.satp_mode.init & (1 << i)) &&
1217                 (cpu->cfg.satp_mode.supported & (1 << i))) {
1218                 error_setg(errp, "cannot disable %s satp mode if %s "
1219                            "is enabled", satp_mode_str(i, false),
1220                            satp_mode_str(satp_mode_map_max, false));
1221                 return;
1222             }
1223         }
1224     }
1225 
1226     /* Finally expand the map so that all valid modes are set */
1227     for (int i = satp_mode_map_max - 1; i >= 0; --i) {
1228         if (cpu->cfg.satp_mode.supported & (1 << i)) {
1229             cpu->cfg.satp_mode.map |= (1 << i);
1230         }
1231     }
1232 }
1233 #endif
1234 
1235 static void riscv_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
1236 {
1237 #ifndef CONFIG_USER_ONLY
1238     Error *local_err = NULL;
1239 
1240     riscv_cpu_satp_mode_finalize(cpu, &local_err);
1241     if (local_err != NULL) {
1242         error_propagate(errp, local_err);
1243         return;
1244     }
1245 #endif
1246 }
1247 
1248 static void riscv_cpu_validate_misa_priv(CPURISCVState *env, Error **errp)
1249 {
1250     if (riscv_has_ext(env, RVH) && env->priv_ver < PRIV_VERSION_1_12_0) {
1251         error_setg(errp, "H extension requires priv spec 1.12.0");
1252         return;
1253     }
1254 }
1255 
1256 static void riscv_cpu_realize(DeviceState *dev, Error **errp)
1257 {
1258     CPUState *cs = CPU(dev);
1259     RISCVCPU *cpu = RISCV_CPU(dev);
1260     CPURISCVState *env = &cpu->env;
1261     RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(dev);
1262     Error *local_err = NULL;
1263 
1264     cpu_exec_realizefn(cs, &local_err);
1265     if (local_err != NULL) {
1266         error_propagate(errp, local_err);
1267         return;
1268     }
1269 
1270     riscv_cpu_validate_misa_mxl(cpu, &local_err);
1271     if (local_err != NULL) {
1272         error_propagate(errp, local_err);
1273         return;
1274     }
1275 
1276     riscv_cpu_validate_priv_spec(cpu, &local_err);
1277     if (local_err != NULL) {
1278         error_propagate(errp, local_err);
1279         return;
1280     }
1281 
1282     riscv_cpu_validate_misa_priv(env, &local_err);
1283     if (local_err != NULL) {
1284         error_propagate(errp, local_err);
1285         return;
1286     }
1287 
1288     if (cpu->cfg.epmp && !cpu->cfg.pmp) {
1289         /*
1290          * Enhanced PMP should only be available
1291          * on harts with PMP support
1292          */
1293         error_setg(errp, "Invalid configuration: EPMP requires PMP support");
1294         return;
1295     }
1296 
1297     riscv_cpu_validate_set_extensions(cpu, &local_err);
1298     if (local_err != NULL) {
1299         error_propagate(errp, local_err);
1300         return;
1301     }
1302 
1303 #ifndef CONFIG_USER_ONLY
1304     if (cpu->cfg.ext_sstc) {
1305         riscv_timer_init(cpu);
1306     }
1307 
1308     if (cpu->cfg.pmu_num) {
1309         if (!riscv_pmu_init(cpu, cpu->cfg.pmu_num) && cpu->cfg.ext_sscofpmf) {
1310             cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1311                                           riscv_pmu_timer_cb, cpu);
1312         }
1313      }
1314 #endif
1315 
1316     riscv_cpu_finalize_features(cpu, &local_err);
1317     if (local_err != NULL) {
1318         error_propagate(errp, local_err);
1319         return;
1320     }
1321 
1322     riscv_cpu_register_gdb_regs_for_features(cs);
1323 
1324     qemu_init_vcpu(cs);
1325     cpu_reset(cs);
1326 
1327     mcc->parent_realize(dev, errp);
1328 }
1329 
1330 #ifndef CONFIG_USER_ONLY
1331 static void cpu_riscv_get_satp(Object *obj, Visitor *v, const char *name,
1332                                void *opaque, Error **errp)
1333 {
1334     RISCVSATPMap *satp_map = opaque;
1335     uint8_t satp = satp_mode_from_str(name);
1336     bool value;
1337 
1338     value = satp_map->map & (1 << satp);
1339 
1340     visit_type_bool(v, name, &value, errp);
1341 }
1342 
1343 static void cpu_riscv_set_satp(Object *obj, Visitor *v, const char *name,
1344                                void *opaque, Error **errp)
1345 {
1346     RISCVSATPMap *satp_map = opaque;
1347     uint8_t satp = satp_mode_from_str(name);
1348     bool value;
1349 
1350     if (!visit_type_bool(v, name, &value, errp)) {
1351         return;
1352     }
1353 
1354     satp_map->map = deposit32(satp_map->map, satp, 1, value);
1355     satp_map->init |= 1 << satp;
1356 }
1357 
1358 static void riscv_add_satp_mode_properties(Object *obj)
1359 {
1360     RISCVCPU *cpu = RISCV_CPU(obj);
1361 
1362     if (cpu->env.misa_mxl == MXL_RV32) {
1363         object_property_add(obj, "sv32", "bool", cpu_riscv_get_satp,
1364                             cpu_riscv_set_satp, NULL, &cpu->cfg.satp_mode);
1365     } else {
1366         object_property_add(obj, "sv39", "bool", cpu_riscv_get_satp,
1367                             cpu_riscv_set_satp, NULL, &cpu->cfg.satp_mode);
1368         object_property_add(obj, "sv48", "bool", cpu_riscv_get_satp,
1369                             cpu_riscv_set_satp, NULL, &cpu->cfg.satp_mode);
1370         object_property_add(obj, "sv57", "bool", cpu_riscv_get_satp,
1371                             cpu_riscv_set_satp, NULL, &cpu->cfg.satp_mode);
1372         object_property_add(obj, "sv64", "bool", cpu_riscv_get_satp,
1373                             cpu_riscv_set_satp, NULL, &cpu->cfg.satp_mode);
1374     }
1375 }
1376 
1377 static void riscv_cpu_set_irq(void *opaque, int irq, int level)
1378 {
1379     RISCVCPU *cpu = RISCV_CPU(opaque);
1380     CPURISCVState *env = &cpu->env;
1381 
1382     if (irq < IRQ_LOCAL_MAX) {
1383         switch (irq) {
1384         case IRQ_U_SOFT:
1385         case IRQ_S_SOFT:
1386         case IRQ_VS_SOFT:
1387         case IRQ_M_SOFT:
1388         case IRQ_U_TIMER:
1389         case IRQ_S_TIMER:
1390         case IRQ_VS_TIMER:
1391         case IRQ_M_TIMER:
1392         case IRQ_U_EXT:
1393         case IRQ_VS_EXT:
1394         case IRQ_M_EXT:
1395             if (kvm_enabled()) {
1396                 kvm_riscv_set_irq(cpu, irq, level);
1397             } else {
1398                 riscv_cpu_update_mip(env, 1 << irq, BOOL_TO_MASK(level));
1399             }
1400              break;
1401         case IRQ_S_EXT:
1402             if (kvm_enabled()) {
1403                 kvm_riscv_set_irq(cpu, irq, level);
1404             } else {
1405                 env->external_seip = level;
1406                 riscv_cpu_update_mip(env, 1 << irq,
1407                                      BOOL_TO_MASK(level | env->software_seip));
1408             }
1409             break;
1410         default:
1411             g_assert_not_reached();
1412         }
1413     } else if (irq < (IRQ_LOCAL_MAX + IRQ_LOCAL_GUEST_MAX)) {
1414         /* Require H-extension for handling guest local interrupts */
1415         if (!riscv_has_ext(env, RVH)) {
1416             g_assert_not_reached();
1417         }
1418 
1419         /* Compute bit position in HGEIP CSR */
1420         irq = irq - IRQ_LOCAL_MAX + 1;
1421         if (env->geilen < irq) {
1422             g_assert_not_reached();
1423         }
1424 
1425         /* Update HGEIP CSR */
1426         env->hgeip &= ~((target_ulong)1 << irq);
1427         if (level) {
1428             env->hgeip |= (target_ulong)1 << irq;
1429         }
1430 
1431         /* Update mip.SGEIP bit */
1432         riscv_cpu_update_mip(env, MIP_SGEIP,
1433                              BOOL_TO_MASK(!!(env->hgeie & env->hgeip)));
1434     } else {
1435         g_assert_not_reached();
1436     }
1437 }
1438 #endif /* CONFIG_USER_ONLY */
1439 
1440 static void riscv_cpu_init(Object *obj)
1441 {
1442     RISCVCPU *cpu = RISCV_CPU(obj);
1443 
1444     cpu->cfg.ext_ifencei = true;
1445     cpu->cfg.ext_icsr = true;
1446     cpu->cfg.mmu = true;
1447     cpu->cfg.pmp = true;
1448 
1449     cpu_set_cpustate_pointers(cpu);
1450 
1451 #ifndef CONFIG_USER_ONLY
1452     qdev_init_gpio_in(DEVICE(cpu), riscv_cpu_set_irq,
1453                       IRQ_LOCAL_MAX + IRQ_LOCAL_GUEST_MAX);
1454 #endif /* CONFIG_USER_ONLY */
1455 }
1456 
1457 typedef struct RISCVCPUMisaExtConfig {
1458     const char *name;
1459     const char *description;
1460     target_ulong misa_bit;
1461     bool enabled;
1462 } RISCVCPUMisaExtConfig;
1463 
1464 static void cpu_set_misa_ext_cfg(Object *obj, Visitor *v, const char *name,
1465                                  void *opaque, Error **errp)
1466 {
1467     const RISCVCPUMisaExtConfig *misa_ext_cfg = opaque;
1468     target_ulong misa_bit = misa_ext_cfg->misa_bit;
1469     RISCVCPU *cpu = RISCV_CPU(obj);
1470     CPURISCVState *env = &cpu->env;
1471     bool value;
1472 
1473     if (!visit_type_bool(v, name, &value, errp)) {
1474         return;
1475     }
1476 
1477     if (value) {
1478         env->misa_ext |= misa_bit;
1479         env->misa_ext_mask |= misa_bit;
1480     } else {
1481         env->misa_ext &= ~misa_bit;
1482         env->misa_ext_mask &= ~misa_bit;
1483     }
1484 }
1485 
1486 static void cpu_get_misa_ext_cfg(Object *obj, Visitor *v, const char *name,
1487                                  void *opaque, Error **errp)
1488 {
1489     const RISCVCPUMisaExtConfig *misa_ext_cfg = opaque;
1490     target_ulong misa_bit = misa_ext_cfg->misa_bit;
1491     RISCVCPU *cpu = RISCV_CPU(obj);
1492     CPURISCVState *env = &cpu->env;
1493     bool value;
1494 
1495     value = env->misa_ext & misa_bit;
1496 
1497     visit_type_bool(v, name, &value, errp);
1498 }
1499 
1500 static const RISCVCPUMisaExtConfig misa_ext_cfgs[] = {
1501     {.name = "a", .description = "Atomic instructions",
1502      .misa_bit = RVA, .enabled = true},
1503     {.name = "c", .description = "Compressed instructions",
1504      .misa_bit = RVC, .enabled = true},
1505     {.name = "d", .description = "Double-precision float point",
1506      .misa_bit = RVD, .enabled = true},
1507     {.name = "f", .description = "Single-precision float point",
1508      .misa_bit = RVF, .enabled = true},
1509     {.name = "i", .description = "Base integer instruction set",
1510      .misa_bit = RVI, .enabled = true},
1511     {.name = "e", .description = "Base integer instruction set (embedded)",
1512      .misa_bit = RVE, .enabled = false},
1513     {.name = "m", .description = "Integer multiplication and division",
1514      .misa_bit = RVM, .enabled = true},
1515     {.name = "s", .description = "Supervisor-level instructions",
1516      .misa_bit = RVS, .enabled = true},
1517     {.name = "u", .description = "User-level instructions",
1518      .misa_bit = RVU, .enabled = true},
1519     {.name = "h", .description = "Hypervisor",
1520      .misa_bit = RVH, .enabled = true},
1521     {.name = "x-j", .description = "Dynamic translated languages",
1522      .misa_bit = RVJ, .enabled = false},
1523     {.name = "v", .description = "Vector operations",
1524      .misa_bit = RVV, .enabled = false},
1525     {.name = "g", .description = "General purpose (IMAFD_Zicsr_Zifencei)",
1526      .misa_bit = RVG, .enabled = false},
1527 };
1528 
1529 static void riscv_cpu_add_misa_properties(Object *cpu_obj)
1530 {
1531     int i;
1532 
1533     for (i = 0; i < ARRAY_SIZE(misa_ext_cfgs); i++) {
1534         const RISCVCPUMisaExtConfig *misa_cfg = &misa_ext_cfgs[i];
1535 
1536         object_property_add(cpu_obj, misa_cfg->name, "bool",
1537                             cpu_get_misa_ext_cfg,
1538                             cpu_set_misa_ext_cfg,
1539                             NULL, (void *)misa_cfg);
1540         object_property_set_description(cpu_obj, misa_cfg->name,
1541                                         misa_cfg->description);
1542         object_property_set_bool(cpu_obj, misa_cfg->name,
1543                                  misa_cfg->enabled, NULL);
1544     }
1545 }
1546 
1547 static Property riscv_cpu_extensions[] = {
1548     /* Defaults for standard extensions */
1549     DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16),
1550     DEFINE_PROP_BOOL("sscofpmf", RISCVCPU, cfg.ext_sscofpmf, false),
1551     DEFINE_PROP_BOOL("Zifencei", RISCVCPU, cfg.ext_ifencei, true),
1552     DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true),
1553     DEFINE_PROP_BOOL("Zihintpause", RISCVCPU, cfg.ext_zihintpause, true),
1554     DEFINE_PROP_BOOL("Zawrs", RISCVCPU, cfg.ext_zawrs, true),
1555     DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),
1556     DEFINE_PROP_BOOL("Zfhmin", RISCVCPU, cfg.ext_zfhmin, false),
1557     DEFINE_PROP_BOOL("Zve32f", RISCVCPU, cfg.ext_zve32f, false),
1558     DEFINE_PROP_BOOL("Zve64f", RISCVCPU, cfg.ext_zve64f, false),
1559     DEFINE_PROP_BOOL("Zve64d", RISCVCPU, cfg.ext_zve64d, false),
1560     DEFINE_PROP_BOOL("mmu", RISCVCPU, cfg.mmu, true),
1561     DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true),
1562     DEFINE_PROP_BOOL("sstc", RISCVCPU, cfg.ext_sstc, true),
1563 
1564     DEFINE_PROP_STRING("priv_spec", RISCVCPU, cfg.priv_spec),
1565     DEFINE_PROP_STRING("vext_spec", RISCVCPU, cfg.vext_spec),
1566     DEFINE_PROP_UINT16("vlen", RISCVCPU, cfg.vlen, 128),
1567     DEFINE_PROP_UINT16("elen", RISCVCPU, cfg.elen, 64),
1568 
1569     DEFINE_PROP_BOOL("svadu", RISCVCPU, cfg.ext_svadu, true),
1570 
1571     DEFINE_PROP_BOOL("svinval", RISCVCPU, cfg.ext_svinval, false),
1572     DEFINE_PROP_BOOL("svnapot", RISCVCPU, cfg.ext_svnapot, false),
1573     DEFINE_PROP_BOOL("svpbmt", RISCVCPU, cfg.ext_svpbmt, false),
1574 
1575     DEFINE_PROP_BOOL("zba", RISCVCPU, cfg.ext_zba, true),
1576     DEFINE_PROP_BOOL("zbb", RISCVCPU, cfg.ext_zbb, true),
1577     DEFINE_PROP_BOOL("zbc", RISCVCPU, cfg.ext_zbc, true),
1578     DEFINE_PROP_BOOL("zbkb", RISCVCPU, cfg.ext_zbkb, false),
1579     DEFINE_PROP_BOOL("zbkc", RISCVCPU, cfg.ext_zbkc, false),
1580     DEFINE_PROP_BOOL("zbkx", RISCVCPU, cfg.ext_zbkx, false),
1581     DEFINE_PROP_BOOL("zbs", RISCVCPU, cfg.ext_zbs, true),
1582     DEFINE_PROP_BOOL("zk", RISCVCPU, cfg.ext_zk, false),
1583     DEFINE_PROP_BOOL("zkn", RISCVCPU, cfg.ext_zkn, false),
1584     DEFINE_PROP_BOOL("zknd", RISCVCPU, cfg.ext_zknd, false),
1585     DEFINE_PROP_BOOL("zkne", RISCVCPU, cfg.ext_zkne, false),
1586     DEFINE_PROP_BOOL("zknh", RISCVCPU, cfg.ext_zknh, false),
1587     DEFINE_PROP_BOOL("zkr", RISCVCPU, cfg.ext_zkr, false),
1588     DEFINE_PROP_BOOL("zks", RISCVCPU, cfg.ext_zks, false),
1589     DEFINE_PROP_BOOL("zksed", RISCVCPU, cfg.ext_zksed, false),
1590     DEFINE_PROP_BOOL("zksh", RISCVCPU, cfg.ext_zksh, false),
1591     DEFINE_PROP_BOOL("zkt", RISCVCPU, cfg.ext_zkt, false),
1592 
1593     DEFINE_PROP_BOOL("zdinx", RISCVCPU, cfg.ext_zdinx, false),
1594     DEFINE_PROP_BOOL("zfinx", RISCVCPU, cfg.ext_zfinx, false),
1595     DEFINE_PROP_BOOL("zhinx", RISCVCPU, cfg.ext_zhinx, false),
1596     DEFINE_PROP_BOOL("zhinxmin", RISCVCPU, cfg.ext_zhinxmin, false),
1597 
1598     DEFINE_PROP_BOOL("zicbom", RISCVCPU, cfg.ext_icbom, true),
1599     DEFINE_PROP_UINT16("cbom_blocksize", RISCVCPU, cfg.cbom_blocksize, 64),
1600     DEFINE_PROP_BOOL("zicboz", RISCVCPU, cfg.ext_icboz, true),
1601     DEFINE_PROP_UINT16("cboz_blocksize", RISCVCPU, cfg.cboz_blocksize, 64),
1602 
1603     DEFINE_PROP_BOOL("zmmul", RISCVCPU, cfg.ext_zmmul, false),
1604 
1605     DEFINE_PROP_BOOL("zca", RISCVCPU, cfg.ext_zca, false),
1606     DEFINE_PROP_BOOL("zcb", RISCVCPU, cfg.ext_zcb, false),
1607     DEFINE_PROP_BOOL("zcd", RISCVCPU, cfg.ext_zcd, false),
1608     DEFINE_PROP_BOOL("zce", RISCVCPU, cfg.ext_zce, false),
1609     DEFINE_PROP_BOOL("zcf", RISCVCPU, cfg.ext_zcf, false),
1610     DEFINE_PROP_BOOL("zcmp", RISCVCPU, cfg.ext_zcmp, false),
1611     DEFINE_PROP_BOOL("zcmt", RISCVCPU, cfg.ext_zcmt, false),
1612 
1613     /* Vendor-specific custom extensions */
1614     DEFINE_PROP_BOOL("xtheadba", RISCVCPU, cfg.ext_xtheadba, false),
1615     DEFINE_PROP_BOOL("xtheadbb", RISCVCPU, cfg.ext_xtheadbb, false),
1616     DEFINE_PROP_BOOL("xtheadbs", RISCVCPU, cfg.ext_xtheadbs, false),
1617     DEFINE_PROP_BOOL("xtheadcmo", RISCVCPU, cfg.ext_xtheadcmo, false),
1618     DEFINE_PROP_BOOL("xtheadcondmov", RISCVCPU, cfg.ext_xtheadcondmov, false),
1619     DEFINE_PROP_BOOL("xtheadfmemidx", RISCVCPU, cfg.ext_xtheadfmemidx, false),
1620     DEFINE_PROP_BOOL("xtheadfmv", RISCVCPU, cfg.ext_xtheadfmv, false),
1621     DEFINE_PROP_BOOL("xtheadmac", RISCVCPU, cfg.ext_xtheadmac, false),
1622     DEFINE_PROP_BOOL("xtheadmemidx", RISCVCPU, cfg.ext_xtheadmemidx, false),
1623     DEFINE_PROP_BOOL("xtheadmempair", RISCVCPU, cfg.ext_xtheadmempair, false),
1624     DEFINE_PROP_BOOL("xtheadsync", RISCVCPU, cfg.ext_xtheadsync, false),
1625     DEFINE_PROP_BOOL("xventanacondops", RISCVCPU, cfg.ext_XVentanaCondOps, false),
1626 
1627     /* These are experimental so mark with 'x-' */
1628     DEFINE_PROP_BOOL("x-zicond", RISCVCPU, cfg.ext_zicond, false),
1629 
1630     /* ePMP 0.9.3 */
1631     DEFINE_PROP_BOOL("x-epmp", RISCVCPU, cfg.epmp, false),
1632     DEFINE_PROP_BOOL("x-smaia", RISCVCPU, cfg.ext_smaia, false),
1633     DEFINE_PROP_BOOL("x-ssaia", RISCVCPU, cfg.ext_ssaia, false),
1634 
1635     DEFINE_PROP_BOOL("x-zvfh", RISCVCPU, cfg.ext_zvfh, false),
1636     DEFINE_PROP_BOOL("x-zvfhmin", RISCVCPU, cfg.ext_zvfhmin, false),
1637 
1638     DEFINE_PROP_END_OF_LIST(),
1639 };
1640 
1641 /*
1642  * Add CPU properties with user-facing flags.
1643  *
1644  * This will overwrite existing env->misa_ext values with the
1645  * defaults set via riscv_cpu_add_misa_properties().
1646  */
1647 static void riscv_cpu_add_user_properties(Object *obj)
1648 {
1649     Property *prop;
1650     DeviceState *dev = DEVICE(obj);
1651 
1652     riscv_cpu_add_misa_properties(obj);
1653 
1654     for (prop = riscv_cpu_extensions; prop && prop->name; prop++) {
1655         qdev_property_add_static(dev, prop);
1656     }
1657 
1658 #ifndef CONFIG_USER_ONLY
1659     riscv_add_satp_mode_properties(obj);
1660 #endif
1661 }
1662 
1663 static Property riscv_cpu_properties[] = {
1664     DEFINE_PROP_BOOL("debug", RISCVCPU, cfg.debug, true),
1665 
1666     DEFINE_PROP_UINT32("mvendorid", RISCVCPU, cfg.mvendorid, 0),
1667     DEFINE_PROP_UINT64("marchid", RISCVCPU, cfg.marchid, RISCV_CPU_MARCHID),
1668     DEFINE_PROP_UINT64("mimpid", RISCVCPU, cfg.mimpid, RISCV_CPU_MIMPID),
1669 
1670 #ifndef CONFIG_USER_ONLY
1671     DEFINE_PROP_UINT64("resetvec", RISCVCPU, env.resetvec, DEFAULT_RSTVEC),
1672 #endif
1673 
1674     DEFINE_PROP_BOOL("short-isa-string", RISCVCPU, cfg.short_isa_string, false),
1675 
1676     DEFINE_PROP_BOOL("rvv_ta_all_1s", RISCVCPU, cfg.rvv_ta_all_1s, false),
1677     DEFINE_PROP_BOOL("rvv_ma_all_1s", RISCVCPU, cfg.rvv_ma_all_1s, false),
1678 
1679     /*
1680      * write_misa() is marked as experimental for now so mark
1681      * it with -x and default to 'false'.
1682      */
1683     DEFINE_PROP_BOOL("x-misa-w", RISCVCPU, cfg.misa_w, false),
1684     DEFINE_PROP_END_OF_LIST(),
1685 };
1686 
1687 static gchar *riscv_gdb_arch_name(CPUState *cs)
1688 {
1689     RISCVCPU *cpu = RISCV_CPU(cs);
1690     CPURISCVState *env = &cpu->env;
1691 
1692     switch (riscv_cpu_mxl(env)) {
1693     case MXL_RV32:
1694         return g_strdup("riscv:rv32");
1695     case MXL_RV64:
1696     case MXL_RV128:
1697         return g_strdup("riscv:rv64");
1698     default:
1699         g_assert_not_reached();
1700     }
1701 }
1702 
1703 static const char *riscv_gdb_get_dynamic_xml(CPUState *cs, const char *xmlname)
1704 {
1705     RISCVCPU *cpu = RISCV_CPU(cs);
1706 
1707     if (strcmp(xmlname, "riscv-csr.xml") == 0) {
1708         return cpu->dyn_csr_xml;
1709     } else if (strcmp(xmlname, "riscv-vector.xml") == 0) {
1710         return cpu->dyn_vreg_xml;
1711     }
1712 
1713     return NULL;
1714 }
1715 
1716 #ifndef CONFIG_USER_ONLY
1717 static int64_t riscv_get_arch_id(CPUState *cs)
1718 {
1719     RISCVCPU *cpu = RISCV_CPU(cs);
1720 
1721     return cpu->env.mhartid;
1722 }
1723 
1724 #include "hw/core/sysemu-cpu-ops.h"
1725 
1726 static const struct SysemuCPUOps riscv_sysemu_ops = {
1727     .get_phys_page_debug = riscv_cpu_get_phys_page_debug,
1728     .write_elf64_note = riscv_cpu_write_elf64_note,
1729     .write_elf32_note = riscv_cpu_write_elf32_note,
1730     .legacy_vmsd = &vmstate_riscv_cpu,
1731 };
1732 #endif
1733 
1734 #include "hw/core/tcg-cpu-ops.h"
1735 
1736 static const struct TCGCPUOps riscv_tcg_ops = {
1737     .initialize = riscv_translate_init,
1738     .synchronize_from_tb = riscv_cpu_synchronize_from_tb,
1739     .restore_state_to_opc = riscv_restore_state_to_opc,
1740 
1741 #ifndef CONFIG_USER_ONLY
1742     .tlb_fill = riscv_cpu_tlb_fill,
1743     .cpu_exec_interrupt = riscv_cpu_exec_interrupt,
1744     .do_interrupt = riscv_cpu_do_interrupt,
1745     .do_transaction_failed = riscv_cpu_do_transaction_failed,
1746     .do_unaligned_access = riscv_cpu_do_unaligned_access,
1747     .debug_excp_handler = riscv_cpu_debug_excp_handler,
1748     .debug_check_breakpoint = riscv_cpu_debug_check_breakpoint,
1749     .debug_check_watchpoint = riscv_cpu_debug_check_watchpoint,
1750 #endif /* !CONFIG_USER_ONLY */
1751 };
1752 
1753 static void riscv_cpu_class_init(ObjectClass *c, void *data)
1754 {
1755     RISCVCPUClass *mcc = RISCV_CPU_CLASS(c);
1756     CPUClass *cc = CPU_CLASS(c);
1757     DeviceClass *dc = DEVICE_CLASS(c);
1758     ResettableClass *rc = RESETTABLE_CLASS(c);
1759 
1760     device_class_set_parent_realize(dc, riscv_cpu_realize,
1761                                     &mcc->parent_realize);
1762 
1763     resettable_class_set_parent_phases(rc, NULL, riscv_cpu_reset_hold, NULL,
1764                                        &mcc->parent_phases);
1765 
1766     cc->class_by_name = riscv_cpu_class_by_name;
1767     cc->has_work = riscv_cpu_has_work;
1768     cc->dump_state = riscv_cpu_dump_state;
1769     cc->set_pc = riscv_cpu_set_pc;
1770     cc->get_pc = riscv_cpu_get_pc;
1771     cc->gdb_read_register = riscv_cpu_gdb_read_register;
1772     cc->gdb_write_register = riscv_cpu_gdb_write_register;
1773     cc->gdb_num_core_regs = 33;
1774     cc->gdb_stop_before_watchpoint = true;
1775     cc->disas_set_info = riscv_cpu_disas_set_info;
1776 #ifndef CONFIG_USER_ONLY
1777     cc->sysemu_ops = &riscv_sysemu_ops;
1778     cc->get_arch_id = riscv_get_arch_id;
1779 #endif
1780     cc->gdb_arch_name = riscv_gdb_arch_name;
1781     cc->gdb_get_dynamic_xml = riscv_gdb_get_dynamic_xml;
1782     cc->tcg_ops = &riscv_tcg_ops;
1783 
1784     device_class_set_props(dc, riscv_cpu_properties);
1785 }
1786 
1787 static void riscv_isa_string_ext(RISCVCPU *cpu, char **isa_str,
1788                                  int max_str_len)
1789 {
1790     char *old = *isa_str;
1791     char *new = *isa_str;
1792     int i;
1793 
1794     for (i = 0; i < ARRAY_SIZE(isa_edata_arr); i++) {
1795         if (cpu->env.priv_ver >= isa_edata_arr[i].min_version &&
1796             isa_ext_is_enabled(cpu, &isa_edata_arr[i])) {
1797             new = g_strconcat(old, "_", isa_edata_arr[i].name, NULL);
1798             g_free(old);
1799             old = new;
1800         }
1801     }
1802 
1803     *isa_str = new;
1804 }
1805 
1806 char *riscv_isa_string(RISCVCPU *cpu)
1807 {
1808     int i;
1809     const size_t maxlen = sizeof("rv128") + sizeof(riscv_single_letter_exts);
1810     char *isa_str = g_new(char, maxlen);
1811     char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
1812     for (i = 0; i < sizeof(riscv_single_letter_exts) - 1; i++) {
1813         if (cpu->env.misa_ext & RV(riscv_single_letter_exts[i])) {
1814             *p++ = qemu_tolower(riscv_single_letter_exts[i]);
1815         }
1816     }
1817     *p = '\0';
1818     if (!cpu->cfg.short_isa_string) {
1819         riscv_isa_string_ext(cpu, &isa_str, maxlen);
1820     }
1821     return isa_str;
1822 }
1823 
1824 static gint riscv_cpu_list_compare(gconstpointer a, gconstpointer b)
1825 {
1826     ObjectClass *class_a = (ObjectClass *)a;
1827     ObjectClass *class_b = (ObjectClass *)b;
1828     const char *name_a, *name_b;
1829 
1830     name_a = object_class_get_name(class_a);
1831     name_b = object_class_get_name(class_b);
1832     return strcmp(name_a, name_b);
1833 }
1834 
1835 static void riscv_cpu_list_entry(gpointer data, gpointer user_data)
1836 {
1837     const char *typename = object_class_get_name(OBJECT_CLASS(data));
1838     int len = strlen(typename) - strlen(RISCV_CPU_TYPE_SUFFIX);
1839 
1840     qemu_printf("%.*s\n", len, typename);
1841 }
1842 
1843 void riscv_cpu_list(void)
1844 {
1845     GSList *list;
1846 
1847     list = object_class_get_list(TYPE_RISCV_CPU, false);
1848     list = g_slist_sort(list, riscv_cpu_list_compare);
1849     g_slist_foreach(list, riscv_cpu_list_entry, NULL);
1850     g_slist_free(list);
1851 }
1852 
1853 #define DEFINE_CPU(type_name, initfn)      \
1854     {                                      \
1855         .name = type_name,                 \
1856         .parent = TYPE_RISCV_CPU,          \
1857         .instance_init = initfn            \
1858     }
1859 
1860 #define DEFINE_DYNAMIC_CPU(type_name, initfn) \
1861     {                                         \
1862         .name = type_name,                    \
1863         .parent = TYPE_RISCV_DYNAMIC_CPU,     \
1864         .instance_init = initfn               \
1865     }
1866 
1867 static const TypeInfo riscv_cpu_type_infos[] = {
1868     {
1869         .name = TYPE_RISCV_CPU,
1870         .parent = TYPE_CPU,
1871         .instance_size = sizeof(RISCVCPU),
1872         .instance_align = __alignof__(RISCVCPU),
1873         .instance_init = riscv_cpu_init,
1874         .abstract = true,
1875         .class_size = sizeof(RISCVCPUClass),
1876         .class_init = riscv_cpu_class_init,
1877     },
1878     {
1879         .name = TYPE_RISCV_DYNAMIC_CPU,
1880         .parent = TYPE_RISCV_CPU,
1881         .abstract = true,
1882     },
1883     DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_ANY,      riscv_any_cpu_init),
1884 #if defined(CONFIG_KVM)
1885     DEFINE_CPU(TYPE_RISCV_CPU_HOST,             riscv_host_cpu_init),
1886 #endif
1887 #if defined(TARGET_RISCV32)
1888     DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE32,   rv32_base_cpu_init),
1889     DEFINE_CPU(TYPE_RISCV_CPU_IBEX,             rv32_ibex_cpu_init),
1890     DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31,       rv32_sifive_e_cpu_init),
1891     DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34,       rv32_imafcu_nommu_cpu_init),
1892     DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34,       rv32_sifive_u_cpu_init),
1893 #elif defined(TARGET_RISCV64)
1894     DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE64,   rv64_base_cpu_init),
1895     DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E51,       rv64_sifive_e_cpu_init),
1896     DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U54,       rv64_sifive_u_cpu_init),
1897     DEFINE_CPU(TYPE_RISCV_CPU_SHAKTI_C,         rv64_sifive_u_cpu_init),
1898     DEFINE_CPU(TYPE_RISCV_CPU_THEAD_C906,       rv64_thead_c906_cpu_init),
1899     DEFINE_CPU(TYPE_RISCV_CPU_VEYRON_V1,        rv64_veyron_v1_cpu_init),
1900     DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE128,  rv128_base_cpu_init),
1901 #endif
1902 };
1903 
1904 DEFINE_TYPES(riscv_cpu_type_infos)
1905