4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2017-2018 SiFive, Inc.
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.
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
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/>.
20 #include "qemu/osdep.h"
21 #include "qemu/qemu-print.h"
22 #include "qemu/ctype.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_riscv.h"
39 /* RISC-V CPU definitions */
40 static const char riscv_single_letter_exts
[] = "IEMAFDQCPVH";
45 int ext_enable_offset
;
48 #define ISA_EXT_DATA_ENTRY(_name, _min_ver, _prop) \
49 {#_name, _min_ver, CPU_CFG_OFFSET(_prop)}
52 * From vector_helper.c
53 * Note that vector data is stored in host-endian 64-bit chunks,
54 * so addressing bytes needs a host-endian fixup.
57 #define BYTE(x) ((x) ^ 7)
63 * Here are the ordering rules of extension naming defined by RISC-V
65 * 1. All extensions should be separated from other multi-letter extensions
67 * 2. The first letter following the 'Z' conventionally indicates the most
68 * closely related alphabetical extension category, IMAFDQLCBKJTPVH.
69 * If multiple 'Z' extensions are named, they should be ordered first
70 * by category, then alphabetically within a category.
71 * 3. Standard supervisor-level extensions (starts with 'S') should be
72 * listed after standard unprivileged extensions. If multiple
73 * supervisor-level extensions are listed, they should be ordered
75 * 4. Non-standard extensions (starts with 'X') must be listed after all
76 * standard extensions. They must be separated from other multi-letter
77 * extensions by an underscore.
79 * Single letter extensions are checked in riscv_cpu_validate_misa_priv()
82 static const struct isa_ext_data isa_edata_arr
[] = {
83 ISA_EXT_DATA_ENTRY(zicbom
, PRIV_VERSION_1_12_0
, ext_icbom
),
84 ISA_EXT_DATA_ENTRY(zicboz
, PRIV_VERSION_1_12_0
, ext_icboz
),
85 ISA_EXT_DATA_ENTRY(zicond
, PRIV_VERSION_1_12_0
, ext_zicond
),
86 ISA_EXT_DATA_ENTRY(zicsr
, PRIV_VERSION_1_10_0
, ext_icsr
),
87 ISA_EXT_DATA_ENTRY(zifencei
, PRIV_VERSION_1_10_0
, ext_ifencei
),
88 ISA_EXT_DATA_ENTRY(zihintntl
, PRIV_VERSION_1_10_0
, ext_zihintntl
),
89 ISA_EXT_DATA_ENTRY(zihintpause
, PRIV_VERSION_1_10_0
, ext_zihintpause
),
90 ISA_EXT_DATA_ENTRY(zmmul
, PRIV_VERSION_1_12_0
, ext_zmmul
),
91 ISA_EXT_DATA_ENTRY(zawrs
, PRIV_VERSION_1_12_0
, ext_zawrs
),
92 ISA_EXT_DATA_ENTRY(zfa
, PRIV_VERSION_1_12_0
, ext_zfa
),
93 ISA_EXT_DATA_ENTRY(zfbfmin
, PRIV_VERSION_1_12_0
, ext_zfbfmin
),
94 ISA_EXT_DATA_ENTRY(zfh
, PRIV_VERSION_1_11_0
, ext_zfh
),
95 ISA_EXT_DATA_ENTRY(zfhmin
, PRIV_VERSION_1_11_0
, ext_zfhmin
),
96 ISA_EXT_DATA_ENTRY(zfinx
, PRIV_VERSION_1_12_0
, ext_zfinx
),
97 ISA_EXT_DATA_ENTRY(zdinx
, PRIV_VERSION_1_12_0
, ext_zdinx
),
98 ISA_EXT_DATA_ENTRY(zca
, PRIV_VERSION_1_12_0
, ext_zca
),
99 ISA_EXT_DATA_ENTRY(zcb
, PRIV_VERSION_1_12_0
, ext_zcb
),
100 ISA_EXT_DATA_ENTRY(zcf
, PRIV_VERSION_1_12_0
, ext_zcf
),
101 ISA_EXT_DATA_ENTRY(zcd
, PRIV_VERSION_1_12_0
, ext_zcd
),
102 ISA_EXT_DATA_ENTRY(zce
, PRIV_VERSION_1_12_0
, ext_zce
),
103 ISA_EXT_DATA_ENTRY(zcmp
, PRIV_VERSION_1_12_0
, ext_zcmp
),
104 ISA_EXT_DATA_ENTRY(zcmt
, PRIV_VERSION_1_12_0
, ext_zcmt
),
105 ISA_EXT_DATA_ENTRY(zba
, PRIV_VERSION_1_12_0
, ext_zba
),
106 ISA_EXT_DATA_ENTRY(zbb
, PRIV_VERSION_1_12_0
, ext_zbb
),
107 ISA_EXT_DATA_ENTRY(zbc
, PRIV_VERSION_1_12_0
, ext_zbc
),
108 ISA_EXT_DATA_ENTRY(zbkb
, PRIV_VERSION_1_12_0
, ext_zbkb
),
109 ISA_EXT_DATA_ENTRY(zbkc
, PRIV_VERSION_1_12_0
, ext_zbkc
),
110 ISA_EXT_DATA_ENTRY(zbkx
, PRIV_VERSION_1_12_0
, ext_zbkx
),
111 ISA_EXT_DATA_ENTRY(zbs
, PRIV_VERSION_1_12_0
, ext_zbs
),
112 ISA_EXT_DATA_ENTRY(zk
, PRIV_VERSION_1_12_0
, ext_zk
),
113 ISA_EXT_DATA_ENTRY(zkn
, PRIV_VERSION_1_12_0
, ext_zkn
),
114 ISA_EXT_DATA_ENTRY(zknd
, PRIV_VERSION_1_12_0
, ext_zknd
),
115 ISA_EXT_DATA_ENTRY(zkne
, PRIV_VERSION_1_12_0
, ext_zkne
),
116 ISA_EXT_DATA_ENTRY(zknh
, PRIV_VERSION_1_12_0
, ext_zknh
),
117 ISA_EXT_DATA_ENTRY(zkr
, PRIV_VERSION_1_12_0
, ext_zkr
),
118 ISA_EXT_DATA_ENTRY(zks
, PRIV_VERSION_1_12_0
, ext_zks
),
119 ISA_EXT_DATA_ENTRY(zksed
, PRIV_VERSION_1_12_0
, ext_zksed
),
120 ISA_EXT_DATA_ENTRY(zksh
, PRIV_VERSION_1_12_0
, ext_zksh
),
121 ISA_EXT_DATA_ENTRY(zkt
, PRIV_VERSION_1_12_0
, ext_zkt
),
122 ISA_EXT_DATA_ENTRY(zvbb
, PRIV_VERSION_1_12_0
, ext_zvbb
),
123 ISA_EXT_DATA_ENTRY(zvbc
, PRIV_VERSION_1_12_0
, ext_zvbc
),
124 ISA_EXT_DATA_ENTRY(zve32f
, PRIV_VERSION_1_10_0
, ext_zve32f
),
125 ISA_EXT_DATA_ENTRY(zve64f
, PRIV_VERSION_1_10_0
, ext_zve64f
),
126 ISA_EXT_DATA_ENTRY(zve64d
, PRIV_VERSION_1_10_0
, ext_zve64d
),
127 ISA_EXT_DATA_ENTRY(zvfbfmin
, PRIV_VERSION_1_12_0
, ext_zvfbfmin
),
128 ISA_EXT_DATA_ENTRY(zvfbfwma
, PRIV_VERSION_1_12_0
, ext_zvfbfwma
),
129 ISA_EXT_DATA_ENTRY(zvfh
, PRIV_VERSION_1_12_0
, ext_zvfh
),
130 ISA_EXT_DATA_ENTRY(zvfhmin
, PRIV_VERSION_1_12_0
, ext_zvfhmin
),
131 ISA_EXT_DATA_ENTRY(zvkg
, PRIV_VERSION_1_12_0
, ext_zvkg
),
132 ISA_EXT_DATA_ENTRY(zvkned
, PRIV_VERSION_1_12_0
, ext_zvkned
),
133 ISA_EXT_DATA_ENTRY(zvknha
, PRIV_VERSION_1_12_0
, ext_zvknha
),
134 ISA_EXT_DATA_ENTRY(zvknhb
, PRIV_VERSION_1_12_0
, ext_zvknhb
),
135 ISA_EXT_DATA_ENTRY(zvksed
, PRIV_VERSION_1_12_0
, ext_zvksed
),
136 ISA_EXT_DATA_ENTRY(zvksh
, PRIV_VERSION_1_12_0
, ext_zvksh
),
137 ISA_EXT_DATA_ENTRY(zhinx
, PRIV_VERSION_1_12_0
, ext_zhinx
),
138 ISA_EXT_DATA_ENTRY(zhinxmin
, PRIV_VERSION_1_12_0
, ext_zhinxmin
),
139 ISA_EXT_DATA_ENTRY(smaia
, PRIV_VERSION_1_12_0
, ext_smaia
),
140 ISA_EXT_DATA_ENTRY(smepmp
, PRIV_VERSION_1_12_0
, epmp
),
141 ISA_EXT_DATA_ENTRY(smstateen
, PRIV_VERSION_1_12_0
, ext_smstateen
),
142 ISA_EXT_DATA_ENTRY(ssaia
, PRIV_VERSION_1_12_0
, ext_ssaia
),
143 ISA_EXT_DATA_ENTRY(sscofpmf
, PRIV_VERSION_1_12_0
, ext_sscofpmf
),
144 ISA_EXT_DATA_ENTRY(sstc
, PRIV_VERSION_1_12_0
, ext_sstc
),
145 ISA_EXT_DATA_ENTRY(svadu
, PRIV_VERSION_1_12_0
, ext_svadu
),
146 ISA_EXT_DATA_ENTRY(svinval
, PRIV_VERSION_1_12_0
, ext_svinval
),
147 ISA_EXT_DATA_ENTRY(svnapot
, PRIV_VERSION_1_12_0
, ext_svnapot
),
148 ISA_EXT_DATA_ENTRY(svpbmt
, PRIV_VERSION_1_12_0
, ext_svpbmt
),
149 ISA_EXT_DATA_ENTRY(xtheadba
, PRIV_VERSION_1_11_0
, ext_xtheadba
),
150 ISA_EXT_DATA_ENTRY(xtheadbb
, PRIV_VERSION_1_11_0
, ext_xtheadbb
),
151 ISA_EXT_DATA_ENTRY(xtheadbs
, PRIV_VERSION_1_11_0
, ext_xtheadbs
),
152 ISA_EXT_DATA_ENTRY(xtheadcmo
, PRIV_VERSION_1_11_0
, ext_xtheadcmo
),
153 ISA_EXT_DATA_ENTRY(xtheadcondmov
, PRIV_VERSION_1_11_0
, ext_xtheadcondmov
),
154 ISA_EXT_DATA_ENTRY(xtheadfmemidx
, PRIV_VERSION_1_11_0
, ext_xtheadfmemidx
),
155 ISA_EXT_DATA_ENTRY(xtheadfmv
, PRIV_VERSION_1_11_0
, ext_xtheadfmv
),
156 ISA_EXT_DATA_ENTRY(xtheadmac
, PRIV_VERSION_1_11_0
, ext_xtheadmac
),
157 ISA_EXT_DATA_ENTRY(xtheadmemidx
, PRIV_VERSION_1_11_0
, ext_xtheadmemidx
),
158 ISA_EXT_DATA_ENTRY(xtheadmempair
, PRIV_VERSION_1_11_0
, ext_xtheadmempair
),
159 ISA_EXT_DATA_ENTRY(xtheadsync
, PRIV_VERSION_1_11_0
, ext_xtheadsync
),
160 ISA_EXT_DATA_ENTRY(xventanacondops
, PRIV_VERSION_1_12_0
, ext_XVentanaCondOps
),
163 /* Hash that stores user set extensions */
164 static GHashTable
*multi_ext_user_opts
;
166 bool isa_ext_is_enabled(RISCVCPU
*cpu
, uint32_t ext_offset
)
168 bool *ext_enabled
= (void *)&cpu
->cfg
+ ext_offset
;
173 void isa_ext_update_enabled(RISCVCPU
*cpu
, uint32_t ext_offset
, bool en
)
175 bool *ext_enabled
= (void *)&cpu
->cfg
+ ext_offset
;
180 int cpu_cfg_ext_get_min_version(uint32_t ext_offset
)
184 for (i
= 0; i
< ARRAY_SIZE(isa_edata_arr
); i
++) {
185 if (isa_edata_arr
[i
].ext_enable_offset
!= ext_offset
) {
189 return isa_edata_arr
[i
].min_version
;
192 g_assert_not_reached();
195 bool cpu_cfg_ext_is_user_set(uint32_t ext_offset
)
197 return g_hash_table_contains(multi_ext_user_opts
,
198 GUINT_TO_POINTER(ext_offset
));
201 const char * const riscv_int_regnames
[] = {
202 "x0/zero", "x1/ra", "x2/sp", "x3/gp", "x4/tp", "x5/t0", "x6/t1",
203 "x7/t2", "x8/s0", "x9/s1", "x10/a0", "x11/a1", "x12/a2", "x13/a3",
204 "x14/a4", "x15/a5", "x16/a6", "x17/a7", "x18/s2", "x19/s3", "x20/s4",
205 "x21/s5", "x22/s6", "x23/s7", "x24/s8", "x25/s9", "x26/s10", "x27/s11",
206 "x28/t3", "x29/t4", "x30/t5", "x31/t6"
209 const char * const riscv_int_regnamesh
[] = {
210 "x0h/zeroh", "x1h/rah", "x2h/sph", "x3h/gph", "x4h/tph", "x5h/t0h",
211 "x6h/t1h", "x7h/t2h", "x8h/s0h", "x9h/s1h", "x10h/a0h", "x11h/a1h",
212 "x12h/a2h", "x13h/a3h", "x14h/a4h", "x15h/a5h", "x16h/a6h", "x17h/a7h",
213 "x18h/s2h", "x19h/s3h", "x20h/s4h", "x21h/s5h", "x22h/s6h", "x23h/s7h",
214 "x24h/s8h", "x25h/s9h", "x26h/s10h", "x27h/s11h", "x28h/t3h", "x29h/t4h",
215 "x30h/t5h", "x31h/t6h"
218 const char * const riscv_fpr_regnames
[] = {
219 "f0/ft0", "f1/ft1", "f2/ft2", "f3/ft3", "f4/ft4", "f5/ft5",
220 "f6/ft6", "f7/ft7", "f8/fs0", "f9/fs1", "f10/fa0", "f11/fa1",
221 "f12/fa2", "f13/fa3", "f14/fa4", "f15/fa5", "f16/fa6", "f17/fa7",
222 "f18/fs2", "f19/fs3", "f20/fs4", "f21/fs5", "f22/fs6", "f23/fs7",
223 "f24/fs8", "f25/fs9", "f26/fs10", "f27/fs11", "f28/ft8", "f29/ft9",
224 "f30/ft10", "f31/ft11"
227 const char * const riscv_rvv_regnames
[] = {
228 "v0", "v1", "v2", "v3", "v4", "v5", "v6",
229 "v7", "v8", "v9", "v10", "v11", "v12", "v13",
230 "v14", "v15", "v16", "v17", "v18", "v19", "v20",
231 "v21", "v22", "v23", "v24", "v25", "v26", "v27",
232 "v28", "v29", "v30", "v31"
235 static const char * const riscv_excp_names
[] = {
238 "illegal_instruction",
256 "guest_exec_page_fault",
257 "guest_load_page_fault",
259 "guest_store_page_fault",
262 static const char * const riscv_intr_names
[] = {
281 static void riscv_cpu_add_user_properties(Object
*obj
);
282 static void riscv_init_max_cpu_extensions(Object
*obj
);
284 const char *riscv_cpu_get_trap_name(target_ulong cause
, bool async
)
287 return (cause
< ARRAY_SIZE(riscv_intr_names
)) ?
288 riscv_intr_names
[cause
] : "(unknown)";
290 return (cause
< ARRAY_SIZE(riscv_excp_names
)) ?
291 riscv_excp_names
[cause
] : "(unknown)";
295 static void set_misa(CPURISCVState
*env
, RISCVMXL mxl
, uint32_t ext
)
297 env
->misa_mxl_max
= env
->misa_mxl
= mxl
;
298 env
->misa_ext_mask
= env
->misa_ext
= ext
;
301 #ifndef CONFIG_USER_ONLY
302 static uint8_t satp_mode_from_str(const char *satp_mode_str
)
304 if (!strncmp(satp_mode_str
, "mbare", 5)) {
305 return VM_1_10_MBARE
;
308 if (!strncmp(satp_mode_str
, "sv32", 4)) {
312 if (!strncmp(satp_mode_str
, "sv39", 4)) {
316 if (!strncmp(satp_mode_str
, "sv48", 4)) {
320 if (!strncmp(satp_mode_str
, "sv57", 4)) {
324 if (!strncmp(satp_mode_str
, "sv64", 4)) {
328 g_assert_not_reached();
331 uint8_t satp_mode_max_from_map(uint32_t map
)
334 * 'map = 0' will make us return (31 - 32), which C will
335 * happily overflow to UINT_MAX. There's no good result to
336 * return if 'map = 0' (e.g. returning 0 will be ambiguous
337 * with the result for 'map = 1').
339 * Assert out if map = 0. Callers will have to deal with
340 * it outside of this function.
344 /* map here has at least one bit set, so no problem with clz */
345 return 31 - __builtin_clz(map
);
348 const char *satp_mode_str(uint8_t satp_mode
, bool is_32_bit
)
372 g_assert_not_reached();
375 static void set_satp_mode_max_supported(RISCVCPU
*cpu
,
378 bool rv32
= riscv_cpu_mxl(&cpu
->env
) == MXL_RV32
;
379 const bool *valid_vm
= rv32
? valid_vm_1_10_32
: valid_vm_1_10_64
;
381 for (int i
= 0; i
<= satp_mode
; ++i
) {
383 cpu
->cfg
.satp_mode
.supported
|= (1 << i
);
388 /* Set the satp mode to the max supported */
389 static void set_satp_mode_default_map(RISCVCPU
*cpu
)
391 cpu
->cfg
.satp_mode
.map
= cpu
->cfg
.satp_mode
.supported
;
395 static void riscv_any_cpu_init(Object
*obj
)
397 RISCVCPU
*cpu
= RISCV_CPU(obj
);
398 CPURISCVState
*env
= &cpu
->env
;
399 #if defined(TARGET_RISCV32)
400 set_misa(env
, MXL_RV32
, RVI
| RVM
| RVA
| RVF
| RVD
| RVC
| RVU
);
401 #elif defined(TARGET_RISCV64)
402 set_misa(env
, MXL_RV64
, RVI
| RVM
| RVA
| RVF
| RVD
| RVC
| RVU
);
405 #ifndef CONFIG_USER_ONLY
406 set_satp_mode_max_supported(RISCV_CPU(obj
),
407 riscv_cpu_mxl(&RISCV_CPU(obj
)->env
) == MXL_RV32
?
408 VM_1_10_SV32
: VM_1_10_SV57
);
411 env
->priv_ver
= PRIV_VERSION_LATEST
;
413 /* inherited from parent obj via riscv_cpu_init() */
414 cpu
->cfg
.ext_ifencei
= true;
415 cpu
->cfg
.ext_icsr
= true;
420 static void riscv_max_cpu_init(Object
*obj
)
422 RISCVCPU
*cpu
= RISCV_CPU(obj
);
423 CPURISCVState
*env
= &cpu
->env
;
424 RISCVMXL mlx
= MXL_RV64
;
426 #ifdef TARGET_RISCV32
429 set_misa(env
, mlx
, 0);
430 riscv_cpu_add_user_properties(obj
);
431 riscv_init_max_cpu_extensions(obj
);
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
);
439 #if defined(TARGET_RISCV64)
440 static void rv64_base_cpu_init(Object
*obj
)
442 CPURISCVState
*env
= &RISCV_CPU(obj
)->env
;
443 /* We set this in the realise function */
444 set_misa(env
, MXL_RV64
, 0);
445 riscv_cpu_add_user_properties(obj
);
446 /* Set latest version of privileged specification */
447 env
->priv_ver
= PRIV_VERSION_LATEST
;
448 #ifndef CONFIG_USER_ONLY
449 set_satp_mode_max_supported(RISCV_CPU(obj
), VM_1_10_SV57
);
453 static void rv64_sifive_u_cpu_init(Object
*obj
)
455 RISCVCPU
*cpu
= RISCV_CPU(obj
);
456 CPURISCVState
*env
= &cpu
->env
;
457 set_misa(env
, MXL_RV64
, 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
);
463 /* inherited from parent obj via riscv_cpu_init() */
464 cpu
->cfg
.ext_ifencei
= true;
465 cpu
->cfg
.ext_icsr
= true;
470 static void rv64_sifive_e_cpu_init(Object
*obj
)
472 CPURISCVState
*env
= &RISCV_CPU(obj
)->env
;
473 RISCVCPU
*cpu
= RISCV_CPU(obj
);
475 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
);
481 /* inherited from parent obj via riscv_cpu_init() */
482 cpu
->cfg
.ext_ifencei
= true;
483 cpu
->cfg
.ext_icsr
= true;
487 static void rv64_thead_c906_cpu_init(Object
*obj
)
489 CPURISCVState
*env
= &RISCV_CPU(obj
)->env
;
490 RISCVCPU
*cpu
= RISCV_CPU(obj
);
492 set_misa(env
, MXL_RV64
, RVG
| RVC
| RVS
| RVU
);
493 env
->priv_ver
= PRIV_VERSION_1_11_0
;
495 cpu
->cfg
.ext_zfa
= true;
496 cpu
->cfg
.ext_zfh
= 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;
509 cpu
->cfg
.mvendorid
= THEAD_VENDOR_ID
;
510 #ifndef CONFIG_USER_ONLY
511 set_satp_mode_max_supported(cpu
, VM_1_10_SV39
);
514 /* inherited from parent obj via riscv_cpu_init() */
518 static void rv64_veyron_v1_cpu_init(Object
*obj
)
520 CPURISCVState
*env
= &RISCV_CPU(obj
)->env
;
521 RISCVCPU
*cpu
= RISCV_CPU(obj
);
523 set_misa(env
, MXL_RV64
, RVG
| RVC
| RVS
| RVU
| RVH
);
524 env
->priv_ver
= PRIV_VERSION_1_12_0
;
526 /* Enable ISA extensions */
528 cpu
->cfg
.ext_ifencei
= true;
529 cpu
->cfg
.ext_icsr
= 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;
549 cpu
->cfg
.mvendorid
= VEYRON_V1_MVENDORID
;
550 cpu
->cfg
.marchid
= VEYRON_V1_MARCHID
;
551 cpu
->cfg
.mimpid
= VEYRON_V1_MIMPID
;
553 #ifndef CONFIG_USER_ONLY
554 set_satp_mode_max_supported(cpu
, VM_1_10_SV48
);
558 static void rv128_base_cpu_init(Object
*obj
)
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");
566 CPURISCVState
*env
= &RISCV_CPU(obj
)->env
;
567 /* We set this in the realise function */
568 set_misa(env
, MXL_RV128
, 0);
569 riscv_cpu_add_user_properties(obj
);
570 /* Set latest version of privileged specification */
571 env
->priv_ver
= PRIV_VERSION_LATEST
;
572 #ifndef CONFIG_USER_ONLY
573 set_satp_mode_max_supported(RISCV_CPU(obj
), VM_1_10_SV57
);
577 static void rv32_base_cpu_init(Object
*obj
)
579 CPURISCVState
*env
= &RISCV_CPU(obj
)->env
;
580 /* We set this in the realise function */
581 set_misa(env
, MXL_RV32
, 0);
582 riscv_cpu_add_user_properties(obj
);
583 /* Set latest version of privileged specification */
584 env
->priv_ver
= PRIV_VERSION_LATEST
;
585 #ifndef CONFIG_USER_ONLY
586 set_satp_mode_max_supported(RISCV_CPU(obj
), VM_1_10_SV32
);
590 static void rv32_sifive_u_cpu_init(Object
*obj
)
592 RISCVCPU
*cpu
= RISCV_CPU(obj
);
593 CPURISCVState
*env
= &cpu
->env
;
594 set_misa(env
, MXL_RV32
, RVI
| RVM
| RVA
| RVF
| RVD
| RVC
| RVS
| RVU
);
595 env
->priv_ver
= PRIV_VERSION_1_10_0
;
596 #ifndef CONFIG_USER_ONLY
597 set_satp_mode_max_supported(RISCV_CPU(obj
), VM_1_10_SV32
);
600 /* inherited from parent obj via riscv_cpu_init() */
601 cpu
->cfg
.ext_ifencei
= true;
602 cpu
->cfg
.ext_icsr
= true;
607 static void rv32_sifive_e_cpu_init(Object
*obj
)
609 CPURISCVState
*env
= &RISCV_CPU(obj
)->env
;
610 RISCVCPU
*cpu
= RISCV_CPU(obj
);
612 set_misa(env
, MXL_RV32
, RVI
| RVM
| RVA
| RVC
| RVU
);
613 env
->priv_ver
= PRIV_VERSION_1_10_0
;
614 #ifndef CONFIG_USER_ONLY
615 set_satp_mode_max_supported(cpu
, VM_1_10_MBARE
);
618 /* inherited from parent obj via riscv_cpu_init() */
619 cpu
->cfg
.ext_ifencei
= true;
620 cpu
->cfg
.ext_icsr
= true;
624 static void rv32_ibex_cpu_init(Object
*obj
)
626 CPURISCVState
*env
= &RISCV_CPU(obj
)->env
;
627 RISCVCPU
*cpu
= RISCV_CPU(obj
);
629 set_misa(env
, MXL_RV32
, RVI
| RVM
| RVC
| RVU
);
630 env
->priv_ver
= PRIV_VERSION_1_11_0
;
631 #ifndef CONFIG_USER_ONLY
632 set_satp_mode_max_supported(cpu
, VM_1_10_MBARE
);
634 cpu
->cfg
.epmp
= true;
636 /* inherited from parent obj via riscv_cpu_init() */
637 cpu
->cfg
.ext_ifencei
= true;
638 cpu
->cfg
.ext_icsr
= true;
642 static void rv32_imafcu_nommu_cpu_init(Object
*obj
)
644 CPURISCVState
*env
= &RISCV_CPU(obj
)->env
;
645 RISCVCPU
*cpu
= RISCV_CPU(obj
);
647 set_misa(env
, MXL_RV32
, RVI
| RVM
| RVA
| RVF
| RVC
| RVU
);
648 env
->priv_ver
= PRIV_VERSION_1_10_0
;
649 #ifndef CONFIG_USER_ONLY
650 set_satp_mode_max_supported(cpu
, VM_1_10_MBARE
);
653 /* inherited from parent obj via riscv_cpu_init() */
654 cpu
->cfg
.ext_ifencei
= true;
655 cpu
->cfg
.ext_icsr
= true;
660 #if defined(CONFIG_KVM)
661 static void riscv_host_cpu_init(Object
*obj
)
663 CPURISCVState
*env
= &RISCV_CPU(obj
)->env
;
664 #if defined(TARGET_RISCV32)
665 set_misa(env
, MXL_RV32
, 0);
666 #elif defined(TARGET_RISCV64)
667 set_misa(env
, MXL_RV64
, 0);
669 riscv_cpu_add_user_properties(obj
);
671 #endif /* CONFIG_KVM */
673 static ObjectClass
*riscv_cpu_class_by_name(const char *cpu_model
)
679 cpuname
= g_strsplit(cpu_model
, ",", 1);
680 typename
= g_strdup_printf(RISCV_CPU_TYPE_NAME("%s"), cpuname
[0]);
681 oc
= object_class_by_name(typename
);
684 if (!oc
|| !object_class_dynamic_cast(oc
, TYPE_RISCV_CPU
) ||
685 object_class_is_abstract(oc
)) {
691 static void riscv_cpu_dump_state(CPUState
*cs
, FILE *f
, int flags
)
693 RISCVCPU
*cpu
= RISCV_CPU(cs
);
694 CPURISCVState
*env
= &cpu
->env
;
698 #if !defined(CONFIG_USER_ONLY)
699 if (riscv_has_ext(env
, RVH
)) {
700 qemu_fprintf(f
, " %s %d\n", "V = ", env
->virt_enabled
);
703 qemu_fprintf(f
, " %s " TARGET_FMT_lx
"\n", "pc ", env
->pc
);
704 #ifndef CONFIG_USER_ONLY
706 static const int dump_csrs
[] = {
711 * CSR_SSTATUS is intentionally omitted here as its value
712 * can be figured out by looking at CSR_MSTATUS
747 for (i
= 0; i
< ARRAY_SIZE(dump_csrs
); ++i
) {
748 int csrno
= dump_csrs
[i
];
749 target_ulong val
= 0;
750 RISCVException res
= riscv_csrrw_debug(env
, csrno
, &val
, 0, 0);
753 * Rely on the smode, hmode, etc, predicates within csr.c
754 * to do the filtering of the registers that are present.
756 if (res
== RISCV_EXCP_NONE
) {
757 qemu_fprintf(f
, " %-8s " TARGET_FMT_lx
"\n",
758 csr_ops
[csrno
].name
, val
);
764 for (i
= 0; i
< 32; i
++) {
765 qemu_fprintf(f
, " %-8s " TARGET_FMT_lx
,
766 riscv_int_regnames
[i
], env
->gpr
[i
]);
768 qemu_fprintf(f
, "\n");
771 if (flags
& CPU_DUMP_FPU
) {
772 for (i
= 0; i
< 32; i
++) {
773 qemu_fprintf(f
, " %-8s %016" PRIx64
,
774 riscv_fpr_regnames
[i
], env
->fpr
[i
]);
776 qemu_fprintf(f
, "\n");
780 if (riscv_has_ext(env
, RVV
) && (flags
& CPU_DUMP_VPU
)) {
781 static const int dump_rvv_csrs
[] = {
790 for (i
= 0; i
< ARRAY_SIZE(dump_rvv_csrs
); ++i
) {
791 int csrno
= dump_rvv_csrs
[i
];
792 target_ulong val
= 0;
793 RISCVException res
= riscv_csrrw_debug(env
, csrno
, &val
, 0, 0);
796 * Rely on the smode, hmode, etc, predicates within csr.c
797 * to do the filtering of the registers that are present.
799 if (res
== RISCV_EXCP_NONE
) {
800 qemu_fprintf(f
, " %-8s " TARGET_FMT_lx
"\n",
801 csr_ops
[csrno
].name
, val
);
804 uint16_t vlenb
= cpu
->cfg
.vlen
>> 3;
806 for (i
= 0; i
< 32; i
++) {
807 qemu_fprintf(f
, " %-8s ", riscv_rvv_regnames
[i
]);
808 p
= (uint8_t *)env
->vreg
;
809 for (j
= vlenb
- 1 ; j
>= 0; j
--) {
810 qemu_fprintf(f
, "%02x", *(p
+ i
* vlenb
+ BYTE(j
)));
812 qemu_fprintf(f
, "\n");
817 static void riscv_cpu_set_pc(CPUState
*cs
, vaddr value
)
819 RISCVCPU
*cpu
= RISCV_CPU(cs
);
820 CPURISCVState
*env
= &cpu
->env
;
822 if (env
->xl
== MXL_RV32
) {
823 env
->pc
= (int32_t)value
;
829 static vaddr
riscv_cpu_get_pc(CPUState
*cs
)
831 RISCVCPU
*cpu
= RISCV_CPU(cs
);
832 CPURISCVState
*env
= &cpu
->env
;
834 /* Match cpu_get_tb_cpu_state. */
835 if (env
->xl
== MXL_RV32
) {
836 return env
->pc
& UINT32_MAX
;
841 static void riscv_cpu_synchronize_from_tb(CPUState
*cs
,
842 const TranslationBlock
*tb
)
844 if (!(tb_cflags(tb
) & CF_PCREL
)) {
845 RISCVCPU
*cpu
= RISCV_CPU(cs
);
846 CPURISCVState
*env
= &cpu
->env
;
847 RISCVMXL xl
= FIELD_EX32(tb
->flags
, TB_FLAGS
, XL
);
849 tcg_debug_assert(!(cs
->tcg_cflags
& CF_PCREL
));
851 if (xl
== MXL_RV32
) {
852 env
->pc
= (int32_t) tb
->pc
;
859 static bool riscv_cpu_has_work(CPUState
*cs
)
861 #ifndef CONFIG_USER_ONLY
862 RISCVCPU
*cpu
= RISCV_CPU(cs
);
863 CPURISCVState
*env
= &cpu
->env
;
865 * Definition of the WFI instruction requires it to ignore the privilege
866 * mode and delegation registers, but respect individual enables
868 return riscv_cpu_all_pending(env
) != 0;
874 static void riscv_restore_state_to_opc(CPUState
*cs
,
875 const TranslationBlock
*tb
,
876 const uint64_t *data
)
878 RISCVCPU
*cpu
= RISCV_CPU(cs
);
879 CPURISCVState
*env
= &cpu
->env
;
880 RISCVMXL xl
= FIELD_EX32(tb
->flags
, TB_FLAGS
, XL
);
883 if (tb_cflags(tb
) & CF_PCREL
) {
884 pc
= (env
->pc
& TARGET_PAGE_MASK
) | data
[0];
889 if (xl
== MXL_RV32
) {
890 env
->pc
= (int32_t)pc
;
897 static void riscv_cpu_reset_hold(Object
*obj
)
899 #ifndef CONFIG_USER_ONLY
903 CPUState
*cs
= CPU(obj
);
904 RISCVCPU
*cpu
= RISCV_CPU(cs
);
905 RISCVCPUClass
*mcc
= RISCV_CPU_GET_CLASS(cpu
);
906 CPURISCVState
*env
= &cpu
->env
;
908 if (mcc
->parent_phases
.hold
) {
909 mcc
->parent_phases
.hold(obj
);
911 #ifndef CONFIG_USER_ONLY
912 env
->misa_mxl
= env
->misa_mxl_max
;
914 env
->mstatus
&= ~(MSTATUS_MIE
| MSTATUS_MPRV
);
915 if (env
->misa_mxl
> MXL_RV32
) {
917 * The reset status of SXL/UXL is undefined, but mstatus is WARL
918 * and we must ensure that the value after init is valid for read.
920 env
->mstatus
= set_field(env
->mstatus
, MSTATUS64_SXL
, env
->misa_mxl
);
921 env
->mstatus
= set_field(env
->mstatus
, MSTATUS64_UXL
, env
->misa_mxl
);
922 if (riscv_has_ext(env
, RVH
)) {
923 env
->vsstatus
= set_field(env
->vsstatus
,
924 MSTATUS64_SXL
, env
->misa_mxl
);
925 env
->vsstatus
= set_field(env
->vsstatus
,
926 MSTATUS64_UXL
, env
->misa_mxl
);
927 env
->mstatus_hs
= set_field(env
->mstatus_hs
,
928 MSTATUS64_SXL
, env
->misa_mxl
);
929 env
->mstatus_hs
= set_field(env
->mstatus_hs
,
930 MSTATUS64_UXL
, env
->misa_mxl
);
934 env
->miclaim
= MIP_SGEIP
;
935 env
->pc
= env
->resetvec
;
937 env
->two_stage_lookup
= false;
939 env
->menvcfg
= (cpu
->cfg
.ext_svpbmt
? MENVCFG_PBMTE
: 0) |
940 (cpu
->cfg
.ext_svadu
? MENVCFG_ADUE
: 0);
941 env
->henvcfg
= (cpu
->cfg
.ext_svpbmt
? HENVCFG_PBMTE
: 0) |
942 (cpu
->cfg
.ext_svadu
? HENVCFG_ADUE
: 0);
944 /* Initialized default priorities of local interrupts. */
945 for (i
= 0; i
< ARRAY_SIZE(env
->miprio
); i
++) {
946 iprio
= riscv_cpu_default_priority(i
);
947 env
->miprio
[i
] = (i
== IRQ_M_EXT
) ? 0 : iprio
;
948 env
->siprio
[i
] = (i
== IRQ_S_EXT
) ? 0 : iprio
;
952 while (!riscv_cpu_hviprio_index2irq(i
, &irq
, &rdzero
)) {
954 env
->hviprio
[irq
] = env
->miprio
[irq
];
958 /* mmte is supposed to have pm.current hardwired to 1 */
959 env
->mmte
|= (EXT_STATUS_INITIAL
| MMTE_M_PM_CURRENT
);
961 env
->xl
= riscv_cpu_mxl(env
);
962 riscv_cpu_update_mask(env
);
963 cs
->exception_index
= RISCV_EXCP_NONE
;
965 set_default_nan_mode(1, &env
->fp_status
);
967 #ifndef CONFIG_USER_ONLY
968 if (cpu
->cfg
.debug
) {
969 riscv_trigger_reset_hold(env
);
973 kvm_riscv_reset_vcpu(cpu
);
978 static void riscv_cpu_disas_set_info(CPUState
*s
, disassemble_info
*info
)
980 RISCVCPU
*cpu
= RISCV_CPU(s
);
981 CPURISCVState
*env
= &cpu
->env
;
982 info
->target_info
= &cpu
->cfg
;
986 info
->print_insn
= print_insn_riscv32
;
989 info
->print_insn
= print_insn_riscv64
;
992 info
->print_insn
= print_insn_riscv128
;
995 g_assert_not_reached();
999 void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU
*cpu
)
1001 CPURISCVState
*env
= &cpu
->env
;
1004 /* Force disable extensions if priv spec version does not match */
1005 for (i
= 0; i
< ARRAY_SIZE(isa_edata_arr
); i
++) {
1006 if (isa_ext_is_enabled(cpu
, isa_edata_arr
[i
].ext_enable_offset
) &&
1007 (env
->priv_ver
< isa_edata_arr
[i
].min_version
)) {
1008 isa_ext_update_enabled(cpu
, isa_edata_arr
[i
].ext_enable_offset
,
1010 #ifndef CONFIG_USER_ONLY
1011 warn_report("disabling %s extension for hart 0x" TARGET_FMT_lx
1012 " because privilege spec version does not match",
1013 isa_edata_arr
[i
].name
, env
->mhartid
);
1015 warn_report("disabling %s extension because "
1016 "privilege spec version does not match",
1017 isa_edata_arr
[i
].name
);
1023 #ifndef CONFIG_USER_ONLY
1024 static void riscv_cpu_satp_mode_finalize(RISCVCPU
*cpu
, Error
**errp
)
1026 bool rv32
= riscv_cpu_mxl(&cpu
->env
) == MXL_RV32
;
1027 uint8_t satp_mode_map_max
, satp_mode_supported_max
;
1029 /* The CPU wants the OS to decide which satp mode to use */
1030 if (cpu
->cfg
.satp_mode
.supported
== 0) {
1034 satp_mode_supported_max
=
1035 satp_mode_max_from_map(cpu
->cfg
.satp_mode
.supported
);
1037 if (cpu
->cfg
.satp_mode
.map
== 0) {
1038 if (cpu
->cfg
.satp_mode
.init
== 0) {
1039 /* If unset by the user, we fallback to the default satp mode. */
1040 set_satp_mode_default_map(cpu
);
1043 * Find the lowest level that was disabled and then enable the
1044 * first valid level below which can be found in
1045 * valid_vm_1_10_32/64.
1047 for (int i
= 1; i
< 16; ++i
) {
1048 if ((cpu
->cfg
.satp_mode
.init
& (1 << i
)) &&
1049 (cpu
->cfg
.satp_mode
.supported
& (1 << i
))) {
1050 for (int j
= i
- 1; j
>= 0; --j
) {
1051 if (cpu
->cfg
.satp_mode
.supported
& (1 << j
)) {
1052 cpu
->cfg
.satp_mode
.map
|= (1 << j
);
1062 satp_mode_map_max
= satp_mode_max_from_map(cpu
->cfg
.satp_mode
.map
);
1064 /* Make sure the user asked for a supported configuration (HW and qemu) */
1065 if (satp_mode_map_max
> satp_mode_supported_max
) {
1066 error_setg(errp
, "satp_mode %s is higher than hw max capability %s",
1067 satp_mode_str(satp_mode_map_max
, rv32
),
1068 satp_mode_str(satp_mode_supported_max
, rv32
));
1073 * Make sure the user did not ask for an invalid configuration as per
1074 * the specification.
1077 for (int i
= satp_mode_map_max
- 1; i
>= 0; --i
) {
1078 if (!(cpu
->cfg
.satp_mode
.map
& (1 << i
)) &&
1079 (cpu
->cfg
.satp_mode
.init
& (1 << i
)) &&
1080 (cpu
->cfg
.satp_mode
.supported
& (1 << i
))) {
1081 error_setg(errp
, "cannot disable %s satp mode if %s "
1082 "is enabled", satp_mode_str(i
, false),
1083 satp_mode_str(satp_mode_map_max
, false));
1089 /* Finally expand the map so that all valid modes are set */
1090 for (int i
= satp_mode_map_max
- 1; i
>= 0; --i
) {
1091 if (cpu
->cfg
.satp_mode
.supported
& (1 << i
)) {
1092 cpu
->cfg
.satp_mode
.map
|= (1 << i
);
1098 static void riscv_cpu_finalize_features(RISCVCPU
*cpu
, Error
**errp
)
1100 #ifndef CONFIG_USER_ONLY
1101 Error
*local_err
= NULL
;
1103 riscv_cpu_satp_mode_finalize(cpu
, &local_err
);
1104 if (local_err
!= NULL
) {
1105 error_propagate(errp
, local_err
);
1111 static void riscv_cpu_realize(DeviceState
*dev
, Error
**errp
)
1113 CPUState
*cs
= CPU(dev
);
1114 RISCVCPU
*cpu
= RISCV_CPU(dev
);
1115 RISCVCPUClass
*mcc
= RISCV_CPU_GET_CLASS(dev
);
1116 Error
*local_err
= NULL
;
1118 if (object_dynamic_cast(OBJECT(dev
), TYPE_RISCV_CPU_ANY
) != NULL
) {
1119 warn_report("The 'any' CPU is deprecated and will be "
1120 "removed in the future.");
1123 cpu_exec_realizefn(cs
, &local_err
);
1124 if (local_err
!= NULL
) {
1125 error_propagate(errp
, local_err
);
1129 riscv_cpu_finalize_features(cpu
, &local_err
);
1130 if (local_err
!= NULL
) {
1131 error_propagate(errp
, local_err
);
1135 riscv_cpu_register_gdb_regs_for_features(cs
);
1137 #ifndef CONFIG_USER_ONLY
1138 if (cpu
->cfg
.debug
) {
1139 riscv_trigger_realize(&cpu
->env
);
1146 mcc
->parent_realize(dev
, errp
);
1149 #ifndef CONFIG_USER_ONLY
1150 static void cpu_riscv_get_satp(Object
*obj
, Visitor
*v
, const char *name
,
1151 void *opaque
, Error
**errp
)
1153 RISCVSATPMap
*satp_map
= opaque
;
1154 uint8_t satp
= satp_mode_from_str(name
);
1157 value
= satp_map
->map
& (1 << satp
);
1159 visit_type_bool(v
, name
, &value
, errp
);
1162 static void cpu_riscv_set_satp(Object
*obj
, Visitor
*v
, const char *name
,
1163 void *opaque
, Error
**errp
)
1165 RISCVSATPMap
*satp_map
= opaque
;
1166 uint8_t satp
= satp_mode_from_str(name
);
1169 if (!visit_type_bool(v
, name
, &value
, errp
)) {
1173 satp_map
->map
= deposit32(satp_map
->map
, satp
, 1, value
);
1174 satp_map
->init
|= 1 << satp
;
1177 static void riscv_add_satp_mode_properties(Object
*obj
)
1179 RISCVCPU
*cpu
= RISCV_CPU(obj
);
1181 if (cpu
->env
.misa_mxl
== MXL_RV32
) {
1182 object_property_add(obj
, "sv32", "bool", cpu_riscv_get_satp
,
1183 cpu_riscv_set_satp
, NULL
, &cpu
->cfg
.satp_mode
);
1185 object_property_add(obj
, "sv39", "bool", cpu_riscv_get_satp
,
1186 cpu_riscv_set_satp
, NULL
, &cpu
->cfg
.satp_mode
);
1187 object_property_add(obj
, "sv48", "bool", cpu_riscv_get_satp
,
1188 cpu_riscv_set_satp
, NULL
, &cpu
->cfg
.satp_mode
);
1189 object_property_add(obj
, "sv57", "bool", cpu_riscv_get_satp
,
1190 cpu_riscv_set_satp
, NULL
, &cpu
->cfg
.satp_mode
);
1191 object_property_add(obj
, "sv64", "bool", cpu_riscv_get_satp
,
1192 cpu_riscv_set_satp
, NULL
, &cpu
->cfg
.satp_mode
);
1196 static void riscv_cpu_set_irq(void *opaque
, int irq
, int level
)
1198 RISCVCPU
*cpu
= RISCV_CPU(opaque
);
1199 CPURISCVState
*env
= &cpu
->env
;
1201 if (irq
< IRQ_LOCAL_MAX
) {
1214 if (kvm_enabled()) {
1215 kvm_riscv_set_irq(cpu
, irq
, level
);
1217 riscv_cpu_update_mip(env
, 1 << irq
, BOOL_TO_MASK(level
));
1221 if (kvm_enabled()) {
1222 kvm_riscv_set_irq(cpu
, irq
, level
);
1224 env
->external_seip
= level
;
1225 riscv_cpu_update_mip(env
, 1 << irq
,
1226 BOOL_TO_MASK(level
| env
->software_seip
));
1230 g_assert_not_reached();
1232 } else if (irq
< (IRQ_LOCAL_MAX
+ IRQ_LOCAL_GUEST_MAX
)) {
1233 /* Require H-extension for handling guest local interrupts */
1234 if (!riscv_has_ext(env
, RVH
)) {
1235 g_assert_not_reached();
1238 /* Compute bit position in HGEIP CSR */
1239 irq
= irq
- IRQ_LOCAL_MAX
+ 1;
1240 if (env
->geilen
< irq
) {
1241 g_assert_not_reached();
1244 /* Update HGEIP CSR */
1245 env
->hgeip
&= ~((target_ulong
)1 << irq
);
1247 env
->hgeip
|= (target_ulong
)1 << irq
;
1250 /* Update mip.SGEIP bit */
1251 riscv_cpu_update_mip(env
, MIP_SGEIP
,
1252 BOOL_TO_MASK(!!(env
->hgeie
& env
->hgeip
)));
1254 g_assert_not_reached();
1257 #endif /* CONFIG_USER_ONLY */
1259 static void riscv_cpu_init(Object
*obj
)
1261 #ifndef CONFIG_USER_ONLY
1262 qdev_init_gpio_in(DEVICE(obj
), riscv_cpu_set_irq
,
1263 IRQ_LOCAL_MAX
+ IRQ_LOCAL_GUEST_MAX
);
1264 #endif /* CONFIG_USER_ONLY */
1266 multi_ext_user_opts
= g_hash_table_new(NULL
, g_direct_equal
);
1269 typedef struct RISCVCPUMisaExtConfig
{
1271 const char *description
;
1272 target_ulong misa_bit
;
1274 } RISCVCPUMisaExtConfig
;
1276 static void cpu_set_misa_ext_cfg(Object
*obj
, Visitor
*v
, const char *name
,
1277 void *opaque
, Error
**errp
)
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
;
1285 if (!visit_type_bool(v
, name
, &value
, errp
)) {
1290 env
->misa_ext
|= misa_bit
;
1291 env
->misa_ext_mask
|= misa_bit
;
1293 env
->misa_ext
&= ~misa_bit
;
1294 env
->misa_ext_mask
&= ~misa_bit
;
1298 static void cpu_get_misa_ext_cfg(Object
*obj
, Visitor
*v
, const char *name
,
1299 void *opaque
, Error
**errp
)
1301 const RISCVCPUMisaExtConfig
*misa_ext_cfg
= opaque
;
1302 target_ulong misa_bit
= misa_ext_cfg
->misa_bit
;
1303 RISCVCPU
*cpu
= RISCV_CPU(obj
);
1304 CPURISCVState
*env
= &cpu
->env
;
1307 value
= env
->misa_ext
& misa_bit
;
1309 visit_type_bool(v
, name
, &value
, errp
);
1312 typedef struct misa_ext_info
{
1314 const char *description
;
1317 #define MISA_INFO_IDX(_bit) \
1320 #define MISA_EXT_INFO(_bit, _propname, _descr) \
1321 [MISA_INFO_IDX(_bit)] = {.name = _propname, .description = _descr}
1323 static const MISAExtInfo misa_ext_info_arr
[] = {
1324 MISA_EXT_INFO(RVA
, "a", "Atomic instructions"),
1325 MISA_EXT_INFO(RVC
, "c", "Compressed instructions"),
1326 MISA_EXT_INFO(RVD
, "d", "Double-precision float point"),
1327 MISA_EXT_INFO(RVF
, "f", "Single-precision float point"),
1328 MISA_EXT_INFO(RVI
, "i", "Base integer instruction set"),
1329 MISA_EXT_INFO(RVE
, "e", "Base integer instruction set (embedded)"),
1330 MISA_EXT_INFO(RVM
, "m", "Integer multiplication and division"),
1331 MISA_EXT_INFO(RVS
, "s", "Supervisor-level instructions"),
1332 MISA_EXT_INFO(RVU
, "u", "User-level instructions"),
1333 MISA_EXT_INFO(RVH
, "h", "Hypervisor"),
1334 MISA_EXT_INFO(RVJ
, "x-j", "Dynamic translated languages"),
1335 MISA_EXT_INFO(RVV
, "v", "Vector operations"),
1336 MISA_EXT_INFO(RVG
, "g", "General purpose (IMAFD_Zicsr_Zifencei)"),
1339 static int riscv_validate_misa_info_idx(uint32_t bit
)
1344 * Our lowest valid input (RVA) is 1 and
1345 * __builtin_ctz() is UB with zero.
1348 idx
= MISA_INFO_IDX(bit
);
1350 g_assert(idx
< ARRAY_SIZE(misa_ext_info_arr
));
1354 const char *riscv_get_misa_ext_name(uint32_t bit
)
1356 int idx
= riscv_validate_misa_info_idx(bit
);
1357 const char *val
= misa_ext_info_arr
[idx
].name
;
1359 g_assert(val
!= NULL
);
1363 const char *riscv_get_misa_ext_description(uint32_t bit
)
1365 int idx
= riscv_validate_misa_info_idx(bit
);
1366 const char *val
= misa_ext_info_arr
[idx
].description
;
1368 g_assert(val
!= NULL
);
1372 #define MISA_CFG(_bit, _enabled) \
1373 {.misa_bit = _bit, .enabled = _enabled}
1375 static RISCVCPUMisaExtConfig misa_ext_cfgs
[] = {
1376 MISA_CFG(RVA
, true),
1377 MISA_CFG(RVC
, true),
1378 MISA_CFG(RVD
, true),
1379 MISA_CFG(RVF
, true),
1380 MISA_CFG(RVI
, true),
1381 MISA_CFG(RVE
, false),
1382 MISA_CFG(RVM
, true),
1383 MISA_CFG(RVS
, true),
1384 MISA_CFG(RVU
, true),
1385 MISA_CFG(RVH
, true),
1386 MISA_CFG(RVJ
, false),
1387 MISA_CFG(RVV
, false),
1388 MISA_CFG(RVG
, false),
1392 * We do not support user choice tracking for MISA
1393 * extensions yet because, so far, we do not silently
1394 * change MISA bits during realize() (RVG enables MISA
1395 * bits but the user is warned about it).
1397 static void riscv_cpu_add_misa_properties(Object
*cpu_obj
)
1401 for (i
= 0; i
< ARRAY_SIZE(misa_ext_cfgs
); i
++) {
1402 RISCVCPUMisaExtConfig
*misa_cfg
= &misa_ext_cfgs
[i
];
1403 int bit
= misa_cfg
->misa_bit
;
1405 misa_cfg
->name
= riscv_get_misa_ext_name(bit
);
1406 misa_cfg
->description
= riscv_get_misa_ext_description(bit
);
1408 /* Check if KVM already created the property */
1409 if (object_property_find(cpu_obj
, misa_cfg
->name
)) {
1413 object_property_add(cpu_obj
, misa_cfg
->name
, "bool",
1414 cpu_get_misa_ext_cfg
,
1415 cpu_set_misa_ext_cfg
,
1416 NULL
, (void *)misa_cfg
);
1417 object_property_set_description(cpu_obj
, misa_cfg
->name
,
1418 misa_cfg
->description
);
1419 object_property_set_bool(cpu_obj
, misa_cfg
->name
,
1420 misa_cfg
->enabled
, NULL
);
1424 typedef struct RISCVCPUMultiExtConfig
{
1428 } RISCVCPUMultiExtConfig
;
1430 #define MULTI_EXT_CFG_BOOL(_name, _prop, _defval) \
1431 {.name = _name, .offset = CPU_CFG_OFFSET(_prop), \
1434 static RISCVCPUMultiExtConfig riscv_cpu_extensions
[] = {
1435 /* Defaults for standard extensions */
1436 MULTI_EXT_CFG_BOOL("sscofpmf", ext_sscofpmf
, false),
1437 MULTI_EXT_CFG_BOOL("Zifencei", ext_ifencei
, true),
1438 MULTI_EXT_CFG_BOOL("Zicsr", ext_icsr
, true),
1439 MULTI_EXT_CFG_BOOL("Zihintntl", ext_zihintntl
, true),
1440 MULTI_EXT_CFG_BOOL("Zihintpause", ext_zihintpause
, true),
1441 MULTI_EXT_CFG_BOOL("Zawrs", ext_zawrs
, true),
1442 MULTI_EXT_CFG_BOOL("Zfa", ext_zfa
, true),
1443 MULTI_EXT_CFG_BOOL("Zfh", ext_zfh
, false),
1444 MULTI_EXT_CFG_BOOL("Zfhmin", ext_zfhmin
, false),
1445 MULTI_EXT_CFG_BOOL("Zve32f", ext_zve32f
, false),
1446 MULTI_EXT_CFG_BOOL("Zve64f", ext_zve64f
, false),
1447 MULTI_EXT_CFG_BOOL("Zve64d", ext_zve64d
, false),
1448 MULTI_EXT_CFG_BOOL("sstc", ext_sstc
, true),
1450 MULTI_EXT_CFG_BOOL("smstateen", ext_smstateen
, false),
1451 MULTI_EXT_CFG_BOOL("svadu", ext_svadu
, true),
1452 MULTI_EXT_CFG_BOOL("svinval", ext_svinval
, false),
1453 MULTI_EXT_CFG_BOOL("svnapot", ext_svnapot
, false),
1454 MULTI_EXT_CFG_BOOL("svpbmt", ext_svpbmt
, false),
1456 MULTI_EXT_CFG_BOOL("zba", ext_zba
, true),
1457 MULTI_EXT_CFG_BOOL("zbb", ext_zbb
, true),
1458 MULTI_EXT_CFG_BOOL("zbc", ext_zbc
, true),
1459 MULTI_EXT_CFG_BOOL("zbkb", ext_zbkb
, false),
1460 MULTI_EXT_CFG_BOOL("zbkc", ext_zbkc
, false),
1461 MULTI_EXT_CFG_BOOL("zbkx", ext_zbkx
, false),
1462 MULTI_EXT_CFG_BOOL("zbs", ext_zbs
, true),
1463 MULTI_EXT_CFG_BOOL("zk", ext_zk
, false),
1464 MULTI_EXT_CFG_BOOL("zkn", ext_zkn
, false),
1465 MULTI_EXT_CFG_BOOL("zknd", ext_zknd
, false),
1466 MULTI_EXT_CFG_BOOL("zkne", ext_zkne
, false),
1467 MULTI_EXT_CFG_BOOL("zknh", ext_zknh
, false),
1468 MULTI_EXT_CFG_BOOL("zkr", ext_zkr
, false),
1469 MULTI_EXT_CFG_BOOL("zks", ext_zks
, false),
1470 MULTI_EXT_CFG_BOOL("zksed", ext_zksed
, false),
1471 MULTI_EXT_CFG_BOOL("zksh", ext_zksh
, false),
1472 MULTI_EXT_CFG_BOOL("zkt", ext_zkt
, false),
1474 MULTI_EXT_CFG_BOOL("zdinx", ext_zdinx
, false),
1475 MULTI_EXT_CFG_BOOL("zfinx", ext_zfinx
, false),
1476 MULTI_EXT_CFG_BOOL("zhinx", ext_zhinx
, false),
1477 MULTI_EXT_CFG_BOOL("zhinxmin", ext_zhinxmin
, false),
1479 MULTI_EXT_CFG_BOOL("zicbom", ext_icbom
, true),
1480 MULTI_EXT_CFG_BOOL("zicboz", ext_icboz
, true),
1482 MULTI_EXT_CFG_BOOL("zmmul", ext_zmmul
, false),
1484 MULTI_EXT_CFG_BOOL("zca", ext_zca
, false),
1485 MULTI_EXT_CFG_BOOL("zcb", ext_zcb
, false),
1486 MULTI_EXT_CFG_BOOL("zcd", ext_zcd
, false),
1487 MULTI_EXT_CFG_BOOL("zce", ext_zce
, false),
1488 MULTI_EXT_CFG_BOOL("zcf", ext_zcf
, false),
1489 MULTI_EXT_CFG_BOOL("zcmp", ext_zcmp
, false),
1490 MULTI_EXT_CFG_BOOL("zcmt", ext_zcmt
, false),
1491 MULTI_EXT_CFG_BOOL("zicond", ext_zicond
, false),
1493 DEFINE_PROP_END_OF_LIST(),
1496 static RISCVCPUMultiExtConfig riscv_cpu_vendor_exts
[] = {
1497 MULTI_EXT_CFG_BOOL("xtheadba", ext_xtheadba
, false),
1498 MULTI_EXT_CFG_BOOL("xtheadbb", ext_xtheadbb
, false),
1499 MULTI_EXT_CFG_BOOL("xtheadbs", ext_xtheadbs
, false),
1500 MULTI_EXT_CFG_BOOL("xtheadcmo", ext_xtheadcmo
, false),
1501 MULTI_EXT_CFG_BOOL("xtheadcondmov", ext_xtheadcondmov
, false),
1502 MULTI_EXT_CFG_BOOL("xtheadfmemidx", ext_xtheadfmemidx
, false),
1503 MULTI_EXT_CFG_BOOL("xtheadfmv", ext_xtheadfmv
, false),
1504 MULTI_EXT_CFG_BOOL("xtheadmac", ext_xtheadmac
, false),
1505 MULTI_EXT_CFG_BOOL("xtheadmemidx", ext_xtheadmemidx
, false),
1506 MULTI_EXT_CFG_BOOL("xtheadmempair", ext_xtheadmempair
, false),
1507 MULTI_EXT_CFG_BOOL("xtheadsync", ext_xtheadsync
, false),
1508 MULTI_EXT_CFG_BOOL("xventanacondops", ext_XVentanaCondOps
, false),
1510 DEFINE_PROP_END_OF_LIST(),
1513 /* These are experimental so mark with 'x-' */
1514 static RISCVCPUMultiExtConfig riscv_cpu_experimental_exts
[] = {
1516 MULTI_EXT_CFG_BOOL("x-epmp", epmp
, false),
1517 MULTI_EXT_CFG_BOOL("x-smaia", ext_smaia
, false),
1518 MULTI_EXT_CFG_BOOL("x-ssaia", ext_ssaia
, false),
1520 MULTI_EXT_CFG_BOOL("x-zvfh", ext_zvfh
, false),
1521 MULTI_EXT_CFG_BOOL("x-zvfhmin", ext_zvfhmin
, false),
1523 MULTI_EXT_CFG_BOOL("x-zfbfmin", ext_zfbfmin
, false),
1524 MULTI_EXT_CFG_BOOL("x-zvfbfmin", ext_zvfbfmin
, false),
1525 MULTI_EXT_CFG_BOOL("x-zvfbfwma", ext_zvfbfwma
, false),
1527 /* Vector cryptography extensions */
1528 MULTI_EXT_CFG_BOOL("x-zvbb", ext_zvbb
, false),
1529 MULTI_EXT_CFG_BOOL("x-zvbc", ext_zvbc
, false),
1530 MULTI_EXT_CFG_BOOL("x-zvkg", ext_zvkg
, false),
1531 MULTI_EXT_CFG_BOOL("x-zvkned", ext_zvkned
, false),
1532 MULTI_EXT_CFG_BOOL("x-zvknha", ext_zvknha
, false),
1533 MULTI_EXT_CFG_BOOL("x-zvknhb", ext_zvknhb
, false),
1534 MULTI_EXT_CFG_BOOL("x-zvksed", ext_zvksed
, false),
1535 MULTI_EXT_CFG_BOOL("x-zvksh", ext_zvksh
, false),
1537 DEFINE_PROP_END_OF_LIST(),
1540 static Property riscv_cpu_options
[] = {
1541 DEFINE_PROP_UINT8("pmu-num", RISCVCPU
, cfg
.pmu_num
, 16),
1543 DEFINE_PROP_BOOL("mmu", RISCVCPU
, cfg
.mmu
, true),
1544 DEFINE_PROP_BOOL("pmp", RISCVCPU
, cfg
.pmp
, true),
1546 DEFINE_PROP_STRING("priv_spec", RISCVCPU
, cfg
.priv_spec
),
1547 DEFINE_PROP_STRING("vext_spec", RISCVCPU
, cfg
.vext_spec
),
1549 DEFINE_PROP_UINT16("vlen", RISCVCPU
, cfg
.vlen
, 128),
1550 DEFINE_PROP_UINT16("elen", RISCVCPU
, cfg
.elen
, 64),
1552 DEFINE_PROP_UINT16("cbom_blocksize", RISCVCPU
, cfg
.cbom_blocksize
, 64),
1553 DEFINE_PROP_UINT16("cboz_blocksize", RISCVCPU
, cfg
.cboz_blocksize
, 64),
1555 DEFINE_PROP_END_OF_LIST(),
1558 static void cpu_set_multi_ext_cfg(Object
*obj
, Visitor
*v
, const char *name
,
1559 void *opaque
, Error
**errp
)
1561 const RISCVCPUMultiExtConfig
*multi_ext_cfg
= opaque
;
1564 if (!visit_type_bool(v
, name
, &value
, errp
)) {
1568 isa_ext_update_enabled(RISCV_CPU(obj
), multi_ext_cfg
->offset
, value
);
1570 g_hash_table_insert(multi_ext_user_opts
,
1571 GUINT_TO_POINTER(multi_ext_cfg
->offset
),
1575 static void cpu_get_multi_ext_cfg(Object
*obj
, Visitor
*v
, const char *name
,
1576 void *opaque
, Error
**errp
)
1578 const RISCVCPUMultiExtConfig
*multi_ext_cfg
= opaque
;
1579 bool value
= isa_ext_is_enabled(RISCV_CPU(obj
), multi_ext_cfg
->offset
);
1581 visit_type_bool(v
, name
, &value
, errp
);
1584 static void cpu_add_multi_ext_prop(Object
*cpu_obj
,
1585 RISCVCPUMultiExtConfig
*multi_cfg
)
1587 object_property_add(cpu_obj
, multi_cfg
->name
, "bool",
1588 cpu_get_multi_ext_cfg
,
1589 cpu_set_multi_ext_cfg
,
1590 NULL
, (void *)multi_cfg
);
1593 * Set def val directly instead of using
1594 * object_property_set_bool() to save the set()
1595 * callback hash for user inputs.
1597 isa_ext_update_enabled(RISCV_CPU(cpu_obj
), multi_cfg
->offset
,
1598 multi_cfg
->enabled
);
1601 static void riscv_cpu_add_multiext_prop_array(Object
*obj
,
1602 RISCVCPUMultiExtConfig
*array
)
1606 for (RISCVCPUMultiExtConfig
*prop
= array
; prop
&& prop
->name
; prop
++) {
1607 cpu_add_multi_ext_prop(obj
, prop
);
1612 static void cpu_set_cfg_unavailable(Object
*obj
, Visitor
*v
,
1614 void *opaque
, Error
**errp
)
1616 const char *propname
= opaque
;
1619 if (!visit_type_bool(v
, name
, &value
, errp
)) {
1624 error_setg(errp
, "extension %s is not available with KVM",
1629 static void riscv_cpu_add_kvm_unavail_prop(Object
*obj
, const char *prop_name
)
1631 /* Check if KVM created the property already */
1632 if (object_property_find(obj
, prop_name
)) {
1637 * Set the default to disabled for every extension
1638 * unknown to KVM and error out if the user attempts
1639 * to enable any of them.
1641 object_property_add(obj
, prop_name
, "bool",
1642 NULL
, cpu_set_cfg_unavailable
,
1643 NULL
, (void *)prop_name
);
1646 static void riscv_cpu_add_kvm_unavail_prop_array(Object
*obj
,
1647 RISCVCPUMultiExtConfig
*array
)
1651 for (RISCVCPUMultiExtConfig
*prop
= array
; prop
&& prop
->name
; prop
++) {
1652 riscv_cpu_add_kvm_unavail_prop(obj
, prop
->name
);
1656 void kvm_riscv_cpu_add_kvm_properties(Object
*obj
)
1659 DeviceState
*dev
= DEVICE(obj
);
1661 kvm_riscv_init_user_properties(obj
);
1662 riscv_cpu_add_misa_properties(obj
);
1664 riscv_cpu_add_kvm_unavail_prop_array(obj
, riscv_cpu_extensions
);
1665 riscv_cpu_add_kvm_unavail_prop_array(obj
, riscv_cpu_vendor_exts
);
1666 riscv_cpu_add_kvm_unavail_prop_array(obj
, riscv_cpu_experimental_exts
);
1668 for (prop
= riscv_cpu_options
; prop
&& prop
->name
; prop
++) {
1669 /* Check if KVM created the property already */
1670 if (object_property_find(obj
, prop
->name
)) {
1673 qdev_property_add_static(dev
, prop
);
1679 * Add CPU properties with user-facing flags.
1681 * This will overwrite existing env->misa_ext values with the
1682 * defaults set via riscv_cpu_add_misa_properties().
1684 static void riscv_cpu_add_user_properties(Object
*obj
)
1686 #ifndef CONFIG_USER_ONLY
1687 riscv_add_satp_mode_properties(obj
);
1689 if (kvm_enabled()) {
1690 kvm_riscv_cpu_add_kvm_properties(obj
);
1695 riscv_cpu_add_misa_properties(obj
);
1697 riscv_cpu_add_multiext_prop_array(obj
, riscv_cpu_extensions
);
1698 riscv_cpu_add_multiext_prop_array(obj
, riscv_cpu_vendor_exts
);
1699 riscv_cpu_add_multiext_prop_array(obj
, riscv_cpu_experimental_exts
);
1701 for (Property
*prop
= riscv_cpu_options
; prop
&& prop
->name
; prop
++) {
1702 qdev_property_add_static(DEVICE(obj
), prop
);
1707 * The 'max' type CPU will have all possible ratified
1708 * non-vendor extensions enabled.
1710 static void riscv_init_max_cpu_extensions(Object
*obj
)
1712 RISCVCPU
*cpu
= RISCV_CPU(obj
);
1713 CPURISCVState
*env
= &cpu
->env
;
1714 RISCVCPUMultiExtConfig
*prop
;
1716 /* Enable RVG, RVJ and RVV that are disabled by default */
1717 set_misa(env
, env
->misa_mxl
, env
->misa_ext
| RVG
| RVJ
| RVV
);
1719 for (prop
= riscv_cpu_extensions
; prop
&& prop
->name
; prop
++) {
1720 isa_ext_update_enabled(cpu
, prop
->offset
, true);
1723 /* set vector version */
1724 env
->vext_ver
= VEXT_VERSION_1_00_0
;
1726 /* Zfinx is not compatible with F. Disable it */
1727 isa_ext_update_enabled(cpu
, CPU_CFG_OFFSET(ext_zfinx
), false);
1728 isa_ext_update_enabled(cpu
, CPU_CFG_OFFSET(ext_zdinx
), false);
1729 isa_ext_update_enabled(cpu
, CPU_CFG_OFFSET(ext_zhinx
), false);
1730 isa_ext_update_enabled(cpu
, CPU_CFG_OFFSET(ext_zhinxmin
), false);
1732 isa_ext_update_enabled(cpu
, CPU_CFG_OFFSET(ext_zce
), false);
1733 isa_ext_update_enabled(cpu
, CPU_CFG_OFFSET(ext_zcmp
), false);
1734 isa_ext_update_enabled(cpu
, CPU_CFG_OFFSET(ext_zcmt
), false);
1736 if (env
->misa_mxl
!= MXL_RV32
) {
1737 isa_ext_update_enabled(cpu
, CPU_CFG_OFFSET(ext_zcf
), false);
1741 static Property riscv_cpu_properties
[] = {
1742 DEFINE_PROP_BOOL("debug", RISCVCPU
, cfg
.debug
, true),
1744 #ifndef CONFIG_USER_ONLY
1745 DEFINE_PROP_UINT64("resetvec", RISCVCPU
, env
.resetvec
, DEFAULT_RSTVEC
),
1748 DEFINE_PROP_BOOL("short-isa-string", RISCVCPU
, cfg
.short_isa_string
, false),
1750 DEFINE_PROP_BOOL("rvv_ta_all_1s", RISCVCPU
, cfg
.rvv_ta_all_1s
, false),
1751 DEFINE_PROP_BOOL("rvv_ma_all_1s", RISCVCPU
, cfg
.rvv_ma_all_1s
, false),
1754 * write_misa() is marked as experimental for now so mark
1755 * it with -x and default to 'false'.
1757 DEFINE_PROP_BOOL("x-misa-w", RISCVCPU
, cfg
.misa_w
, false),
1758 DEFINE_PROP_END_OF_LIST(),
1761 static const gchar
*riscv_gdb_arch_name(CPUState
*cs
)
1763 RISCVCPU
*cpu
= RISCV_CPU(cs
);
1764 CPURISCVState
*env
= &cpu
->env
;
1766 switch (riscv_cpu_mxl(env
)) {
1768 return "riscv:rv32";
1771 return "riscv:rv64";
1773 g_assert_not_reached();
1777 static const char *riscv_gdb_get_dynamic_xml(CPUState
*cs
, const char *xmlname
)
1779 RISCVCPU
*cpu
= RISCV_CPU(cs
);
1781 if (strcmp(xmlname
, "riscv-csr.xml") == 0) {
1782 return cpu
->dyn_csr_xml
;
1783 } else if (strcmp(xmlname
, "riscv-vector.xml") == 0) {
1784 return cpu
->dyn_vreg_xml
;
1790 #ifndef CONFIG_USER_ONLY
1791 static int64_t riscv_get_arch_id(CPUState
*cs
)
1793 RISCVCPU
*cpu
= RISCV_CPU(cs
);
1795 return cpu
->env
.mhartid
;
1798 #include "hw/core/sysemu-cpu-ops.h"
1800 static const struct SysemuCPUOps riscv_sysemu_ops
= {
1801 .get_phys_page_debug
= riscv_cpu_get_phys_page_debug
,
1802 .write_elf64_note
= riscv_cpu_write_elf64_note
,
1803 .write_elf32_note
= riscv_cpu_write_elf32_note
,
1804 .legacy_vmsd
= &vmstate_riscv_cpu
,
1808 const struct TCGCPUOps riscv_tcg_ops
= {
1809 .initialize
= riscv_translate_init
,
1810 .synchronize_from_tb
= riscv_cpu_synchronize_from_tb
,
1811 .restore_state_to_opc
= riscv_restore_state_to_opc
,
1813 #ifndef CONFIG_USER_ONLY
1814 .tlb_fill
= riscv_cpu_tlb_fill
,
1815 .cpu_exec_interrupt
= riscv_cpu_exec_interrupt
,
1816 .do_interrupt
= riscv_cpu_do_interrupt
,
1817 .do_transaction_failed
= riscv_cpu_do_transaction_failed
,
1818 .do_unaligned_access
= riscv_cpu_do_unaligned_access
,
1819 .debug_excp_handler
= riscv_cpu_debug_excp_handler
,
1820 .debug_check_breakpoint
= riscv_cpu_debug_check_breakpoint
,
1821 .debug_check_watchpoint
= riscv_cpu_debug_check_watchpoint
,
1822 #endif /* !CONFIG_USER_ONLY */
1825 static bool riscv_cpu_is_dynamic(Object
*cpu_obj
)
1827 return object_dynamic_cast(cpu_obj
, TYPE_RISCV_DYNAMIC_CPU
) != NULL
;
1830 static void cpu_set_mvendorid(Object
*obj
, Visitor
*v
, const char *name
,
1831 void *opaque
, Error
**errp
)
1833 bool dynamic_cpu
= riscv_cpu_is_dynamic(obj
);
1834 RISCVCPU
*cpu
= RISCV_CPU(obj
);
1835 uint32_t prev_val
= cpu
->cfg
.mvendorid
;
1838 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
1842 if (!dynamic_cpu
&& prev_val
!= value
) {
1843 error_setg(errp
, "Unable to change %s mvendorid (0x%x)",
1844 object_get_typename(obj
), prev_val
);
1848 cpu
->cfg
.mvendorid
= value
;
1851 static void cpu_get_mvendorid(Object
*obj
, Visitor
*v
, const char *name
,
1852 void *opaque
, Error
**errp
)
1854 bool value
= RISCV_CPU(obj
)->cfg
.mvendorid
;
1856 visit_type_bool(v
, name
, &value
, errp
);
1859 static void cpu_set_mimpid(Object
*obj
, Visitor
*v
, const char *name
,
1860 void *opaque
, Error
**errp
)
1862 bool dynamic_cpu
= riscv_cpu_is_dynamic(obj
);
1863 RISCVCPU
*cpu
= RISCV_CPU(obj
);
1864 uint64_t prev_val
= cpu
->cfg
.mimpid
;
1867 if (!visit_type_uint64(v
, name
, &value
, errp
)) {
1871 if (!dynamic_cpu
&& prev_val
!= value
) {
1872 error_setg(errp
, "Unable to change %s mimpid (0x%" PRIu64
")",
1873 object_get_typename(obj
), prev_val
);
1877 cpu
->cfg
.mimpid
= value
;
1880 static void cpu_get_mimpid(Object
*obj
, Visitor
*v
, const char *name
,
1881 void *opaque
, Error
**errp
)
1883 bool value
= RISCV_CPU(obj
)->cfg
.mimpid
;
1885 visit_type_bool(v
, name
, &value
, errp
);
1888 static void cpu_set_marchid(Object
*obj
, Visitor
*v
, const char *name
,
1889 void *opaque
, Error
**errp
)
1891 bool dynamic_cpu
= riscv_cpu_is_dynamic(obj
);
1892 RISCVCPU
*cpu
= RISCV_CPU(obj
);
1893 uint64_t prev_val
= cpu
->cfg
.marchid
;
1894 uint64_t value
, invalid_val
;
1897 if (!visit_type_uint64(v
, name
, &value
, errp
)) {
1901 if (!dynamic_cpu
&& prev_val
!= value
) {
1902 error_setg(errp
, "Unable to change %s marchid (0x%" PRIu64
")",
1903 object_get_typename(obj
), prev_val
);
1907 switch (riscv_cpu_mxl(&cpu
->env
)) {
1916 g_assert_not_reached();
1919 invalid_val
= 1LL << (mxlen
- 1);
1921 if (value
== invalid_val
) {
1922 error_setg(errp
, "Unable to set marchid with MSB (%u) bit set "
1923 "and the remaining bits zero", mxlen
);
1927 cpu
->cfg
.marchid
= value
;
1930 static void cpu_get_marchid(Object
*obj
, Visitor
*v
, const char *name
,
1931 void *opaque
, Error
**errp
)
1933 bool value
= RISCV_CPU(obj
)->cfg
.marchid
;
1935 visit_type_bool(v
, name
, &value
, errp
);
1938 static void riscv_cpu_class_init(ObjectClass
*c
, void *data
)
1940 RISCVCPUClass
*mcc
= RISCV_CPU_CLASS(c
);
1941 CPUClass
*cc
= CPU_CLASS(c
);
1942 DeviceClass
*dc
= DEVICE_CLASS(c
);
1943 ResettableClass
*rc
= RESETTABLE_CLASS(c
);
1945 device_class_set_parent_realize(dc
, riscv_cpu_realize
,
1946 &mcc
->parent_realize
);
1948 resettable_class_set_parent_phases(rc
, NULL
, riscv_cpu_reset_hold
, NULL
,
1949 &mcc
->parent_phases
);
1951 cc
->class_by_name
= riscv_cpu_class_by_name
;
1952 cc
->has_work
= riscv_cpu_has_work
;
1953 cc
->dump_state
= riscv_cpu_dump_state
;
1954 cc
->set_pc
= riscv_cpu_set_pc
;
1955 cc
->get_pc
= riscv_cpu_get_pc
;
1956 cc
->gdb_read_register
= riscv_cpu_gdb_read_register
;
1957 cc
->gdb_write_register
= riscv_cpu_gdb_write_register
;
1958 cc
->gdb_num_core_regs
= 33;
1959 cc
->gdb_stop_before_watchpoint
= true;
1960 cc
->disas_set_info
= riscv_cpu_disas_set_info
;
1961 #ifndef CONFIG_USER_ONLY
1962 cc
->sysemu_ops
= &riscv_sysemu_ops
;
1963 cc
->get_arch_id
= riscv_get_arch_id
;
1965 cc
->gdb_arch_name
= riscv_gdb_arch_name
;
1966 cc
->gdb_get_dynamic_xml
= riscv_gdb_get_dynamic_xml
;
1968 object_class_property_add(c
, "mvendorid", "uint32", cpu_get_mvendorid
,
1969 cpu_set_mvendorid
, NULL
, NULL
);
1971 object_class_property_add(c
, "mimpid", "uint64", cpu_get_mimpid
,
1972 cpu_set_mimpid
, NULL
, NULL
);
1974 object_class_property_add(c
, "marchid", "uint64", cpu_get_marchid
,
1975 cpu_set_marchid
, NULL
, NULL
);
1977 device_class_set_props(dc
, riscv_cpu_properties
);
1980 static void riscv_isa_string_ext(RISCVCPU
*cpu
, char **isa_str
,
1983 char *old
= *isa_str
;
1984 char *new = *isa_str
;
1987 for (i
= 0; i
< ARRAY_SIZE(isa_edata_arr
); i
++) {
1988 if (isa_ext_is_enabled(cpu
, isa_edata_arr
[i
].ext_enable_offset
)) {
1989 new = g_strconcat(old
, "_", isa_edata_arr
[i
].name
, NULL
);
1998 char *riscv_isa_string(RISCVCPU
*cpu
)
2001 const size_t maxlen
= sizeof("rv128") + sizeof(riscv_single_letter_exts
);
2002 char *isa_str
= g_new(char, maxlen
);
2003 char *p
= isa_str
+ snprintf(isa_str
, maxlen
, "rv%d", TARGET_LONG_BITS
);
2004 for (i
= 0; i
< sizeof(riscv_single_letter_exts
) - 1; i
++) {
2005 if (cpu
->env
.misa_ext
& RV(riscv_single_letter_exts
[i
])) {
2006 *p
++ = qemu_tolower(riscv_single_letter_exts
[i
]);
2010 if (!cpu
->cfg
.short_isa_string
) {
2011 riscv_isa_string_ext(cpu
, &isa_str
, maxlen
);
2016 static gint
riscv_cpu_list_compare(gconstpointer a
, gconstpointer b
)
2018 ObjectClass
*class_a
= (ObjectClass
*)a
;
2019 ObjectClass
*class_b
= (ObjectClass
*)b
;
2020 const char *name_a
, *name_b
;
2022 name_a
= object_class_get_name(class_a
);
2023 name_b
= object_class_get_name(class_b
);
2024 return strcmp(name_a
, name_b
);
2027 static void riscv_cpu_list_entry(gpointer data
, gpointer user_data
)
2029 const char *typename
= object_class_get_name(OBJECT_CLASS(data
));
2030 int len
= strlen(typename
) - strlen(RISCV_CPU_TYPE_SUFFIX
);
2032 qemu_printf("%.*s\n", len
, typename
);
2035 void riscv_cpu_list(void)
2039 list
= object_class_get_list(TYPE_RISCV_CPU
, false);
2040 list
= g_slist_sort(list
, riscv_cpu_list_compare
);
2041 g_slist_foreach(list
, riscv_cpu_list_entry
, NULL
);
2045 #define DEFINE_CPU(type_name, initfn) \
2047 .name = type_name, \
2048 .parent = TYPE_RISCV_CPU, \
2049 .instance_init = initfn \
2052 #define DEFINE_DYNAMIC_CPU(type_name, initfn) \
2054 .name = type_name, \
2055 .parent = TYPE_RISCV_DYNAMIC_CPU, \
2056 .instance_init = initfn \
2059 static const TypeInfo riscv_cpu_type_infos
[] = {
2061 .name
= TYPE_RISCV_CPU
,
2063 .instance_size
= sizeof(RISCVCPU
),
2064 .instance_align
= __alignof(RISCVCPU
),
2065 .instance_init
= riscv_cpu_init
,
2067 .class_size
= sizeof(RISCVCPUClass
),
2068 .class_init
= riscv_cpu_class_init
,
2071 .name
= TYPE_RISCV_DYNAMIC_CPU
,
2072 .parent
= TYPE_RISCV_CPU
,
2075 DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_ANY
, riscv_any_cpu_init
),
2076 DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_MAX
, riscv_max_cpu_init
),
2077 #if defined(CONFIG_KVM)
2078 DEFINE_CPU(TYPE_RISCV_CPU_HOST
, riscv_host_cpu_init
),
2080 #if defined(TARGET_RISCV32)
2081 DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE32
, rv32_base_cpu_init
),
2082 DEFINE_CPU(TYPE_RISCV_CPU_IBEX
, rv32_ibex_cpu_init
),
2083 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31
, rv32_sifive_e_cpu_init
),
2084 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34
, rv32_imafcu_nommu_cpu_init
),
2085 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34
, rv32_sifive_u_cpu_init
),
2086 #elif defined(TARGET_RISCV64)
2087 DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE64
, rv64_base_cpu_init
),
2088 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E51
, rv64_sifive_e_cpu_init
),
2089 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U54
, rv64_sifive_u_cpu_init
),
2090 DEFINE_CPU(TYPE_RISCV_CPU_SHAKTI_C
, rv64_sifive_u_cpu_init
),
2091 DEFINE_CPU(TYPE_RISCV_CPU_THEAD_C906
, rv64_thead_c906_cpu_init
),
2092 DEFINE_CPU(TYPE_RISCV_CPU_VEYRON_V1
, rv64_veyron_v1_cpu_init
),
2093 DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE128
, rv128_base_cpu_init
),
2097 DEFINE_TYPES(riscv_cpu_type_infos
)