OSDN Git Service

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