]> git.proxmox.com Git - mirror_qemu.git/blame - target/riscv/cpu.c
target/riscv/kvm.c: read/write (cbom|cboz)_blocksize in KVM
[mirror_qemu.git] / target / riscv / cpu.c
CommitLineData
dc5bd18f
MC
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"
0442428a 21#include "qemu/qemu-print.h"
856dfd8a 22#include "qemu/ctype.h"
dc5bd18f
MC
23#include "qemu/log.h"
24#include "cpu.h"
95bd8daa 25#include "cpu_vendorid.h"
14664483 26#include "pmu.h"
f7697f0e 27#include "internals.h"
43888c2f 28#include "time_helper.h"
dc5bd18f
MC
29#include "exec/exec-all.h"
30#include "qapi/error.h"
6f23aaeb 31#include "qapi/visitor.h"
b55d7d34 32#include "qemu/error-report.h"
c4e95030 33#include "hw/qdev-properties.h"
dc5bd18f 34#include "migration/vmstate.h"
135b03cb 35#include "fpu/softfloat-helpers.h"
ad40be27 36#include "sysemu/kvm.h"
eddabb6b 37#include "sysemu/tcg.h"
ad40be27 38#include "kvm_riscv.h"
0489d5bd 39#include "tcg/tcg.h"
dc5bd18f
MC
40
41/* RISC-V CPU definitions */
0e2c3770 42static const char riscv_single_letter_exts[] = "IEMAFDQCPVH";
dc5bd18f 43
a775398b
AP
44struct isa_ext_data {
45 const char *name;
9a1f054d
AP
46 int min_version;
47 int ext_enable_offset;
a775398b
AP
48};
49
ccc84a75
DHB
50#define ISA_EXT_DATA_ENTRY(_name, _min_ver, _prop) \
51 {#_name, _min_ver, offsetof(struct RISCVCPUConfig, _prop)}
9a1f054d 52
b227f6a8
IK
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
3b57254d 64/*
9a1f054d
AP
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.
6508272a
DHB
80 *
81 * Single letter extensions are checked in riscv_cpu_validate_misa_priv()
82 * instead.
9a1f054d
AP
83 */
84static const struct isa_ext_data isa_edata_arr[] = {
ccc84a75
DHB
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(zihintpause, PRIV_VERSION_1_10_0, ext_zihintpause),
91 ISA_EXT_DATA_ENTRY(zawrs, PRIV_VERSION_1_12_0, ext_zawrs),
889caa44 92 ISA_EXT_DATA_ENTRY(zfbfmin, PRIV_VERSION_1_12_0, ext_zfbfmin),
ccc84a75
DHB
93 ISA_EXT_DATA_ENTRY(zfh, PRIV_VERSION_1_11_0, ext_zfh),
94 ISA_EXT_DATA_ENTRY(zfhmin, PRIV_VERSION_1_11_0, ext_zfhmin),
95 ISA_EXT_DATA_ENTRY(zfinx, PRIV_VERSION_1_12_0, ext_zfinx),
96 ISA_EXT_DATA_ENTRY(zdinx, PRIV_VERSION_1_12_0, ext_zdinx),
97 ISA_EXT_DATA_ENTRY(zca, PRIV_VERSION_1_12_0, ext_zca),
98 ISA_EXT_DATA_ENTRY(zcb, PRIV_VERSION_1_12_0, ext_zcb),
99 ISA_EXT_DATA_ENTRY(zcf, PRIV_VERSION_1_12_0, ext_zcf),
100 ISA_EXT_DATA_ENTRY(zcd, PRIV_VERSION_1_12_0, ext_zcd),
101 ISA_EXT_DATA_ENTRY(zce, PRIV_VERSION_1_12_0, ext_zce),
102 ISA_EXT_DATA_ENTRY(zcmp, PRIV_VERSION_1_12_0, ext_zcmp),
103 ISA_EXT_DATA_ENTRY(zcmt, PRIV_VERSION_1_12_0, ext_zcmt),
104 ISA_EXT_DATA_ENTRY(zba, PRIV_VERSION_1_12_0, ext_zba),
105 ISA_EXT_DATA_ENTRY(zbb, PRIV_VERSION_1_12_0, ext_zbb),
106 ISA_EXT_DATA_ENTRY(zbc, PRIV_VERSION_1_12_0, ext_zbc),
107 ISA_EXT_DATA_ENTRY(zbkb, PRIV_VERSION_1_12_0, ext_zbkb),
108 ISA_EXT_DATA_ENTRY(zbkc, PRIV_VERSION_1_12_0, ext_zbkc),
109 ISA_EXT_DATA_ENTRY(zbkx, PRIV_VERSION_1_12_0, ext_zbkx),
110 ISA_EXT_DATA_ENTRY(zbs, PRIV_VERSION_1_12_0, ext_zbs),
111 ISA_EXT_DATA_ENTRY(zk, PRIV_VERSION_1_12_0, ext_zk),
112 ISA_EXT_DATA_ENTRY(zkn, PRIV_VERSION_1_12_0, ext_zkn),
113 ISA_EXT_DATA_ENTRY(zknd, PRIV_VERSION_1_12_0, ext_zknd),
114 ISA_EXT_DATA_ENTRY(zkne, PRIV_VERSION_1_12_0, ext_zkne),
115 ISA_EXT_DATA_ENTRY(zknh, PRIV_VERSION_1_12_0, ext_zknh),
116 ISA_EXT_DATA_ENTRY(zkr, PRIV_VERSION_1_12_0, ext_zkr),
117 ISA_EXT_DATA_ENTRY(zks, PRIV_VERSION_1_12_0, ext_zks),
118 ISA_EXT_DATA_ENTRY(zksed, PRIV_VERSION_1_12_0, ext_zksed),
119 ISA_EXT_DATA_ENTRY(zksh, PRIV_VERSION_1_12_0, ext_zksh),
120 ISA_EXT_DATA_ENTRY(zkt, PRIV_VERSION_1_12_0, ext_zkt),
121 ISA_EXT_DATA_ENTRY(zve32f, PRIV_VERSION_1_10_0, ext_zve32f),
122 ISA_EXT_DATA_ENTRY(zve64f, PRIV_VERSION_1_10_0, ext_zve64f),
123 ISA_EXT_DATA_ENTRY(zve64d, PRIV_VERSION_1_10_0, ext_zve64d),
889caa44
WL
124 ISA_EXT_DATA_ENTRY(zvfbfmin, PRIV_VERSION_1_12_0, ext_zvfbfmin),
125 ISA_EXT_DATA_ENTRY(zvfbfwma, PRIV_VERSION_1_12_0, ext_zvfbfwma),
ccc84a75
DHB
126 ISA_EXT_DATA_ENTRY(zvfh, PRIV_VERSION_1_12_0, ext_zvfh),
127 ISA_EXT_DATA_ENTRY(zvfhmin, PRIV_VERSION_1_12_0, ext_zvfhmin),
128 ISA_EXT_DATA_ENTRY(zhinx, PRIV_VERSION_1_12_0, ext_zhinx),
129 ISA_EXT_DATA_ENTRY(zhinxmin, PRIV_VERSION_1_12_0, ext_zhinxmin),
130 ISA_EXT_DATA_ENTRY(smaia, PRIV_VERSION_1_12_0, ext_smaia),
3594e3e5 131 ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
ccc84a75
DHB
132 ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
133 ISA_EXT_DATA_ENTRY(sscofpmf, PRIV_VERSION_1_12_0, ext_sscofpmf),
134 ISA_EXT_DATA_ENTRY(sstc, PRIV_VERSION_1_12_0, ext_sstc),
135 ISA_EXT_DATA_ENTRY(svadu, PRIV_VERSION_1_12_0, ext_svadu),
136 ISA_EXT_DATA_ENTRY(svinval, PRIV_VERSION_1_12_0, ext_svinval),
137 ISA_EXT_DATA_ENTRY(svnapot, PRIV_VERSION_1_12_0, ext_svnapot),
138 ISA_EXT_DATA_ENTRY(svpbmt, PRIV_VERSION_1_12_0, ext_svpbmt),
139 ISA_EXT_DATA_ENTRY(xtheadba, PRIV_VERSION_1_11_0, ext_xtheadba),
140 ISA_EXT_DATA_ENTRY(xtheadbb, PRIV_VERSION_1_11_0, ext_xtheadbb),
141 ISA_EXT_DATA_ENTRY(xtheadbs, PRIV_VERSION_1_11_0, ext_xtheadbs),
142 ISA_EXT_DATA_ENTRY(xtheadcmo, PRIV_VERSION_1_11_0, ext_xtheadcmo),
143 ISA_EXT_DATA_ENTRY(xtheadcondmov, PRIV_VERSION_1_11_0, ext_xtheadcondmov),
144 ISA_EXT_DATA_ENTRY(xtheadfmemidx, PRIV_VERSION_1_11_0, ext_xtheadfmemidx),
145 ISA_EXT_DATA_ENTRY(xtheadfmv, PRIV_VERSION_1_11_0, ext_xtheadfmv),
146 ISA_EXT_DATA_ENTRY(xtheadmac, PRIV_VERSION_1_11_0, ext_xtheadmac),
147 ISA_EXT_DATA_ENTRY(xtheadmemidx, PRIV_VERSION_1_11_0, ext_xtheadmemidx),
148 ISA_EXT_DATA_ENTRY(xtheadmempair, PRIV_VERSION_1_11_0, ext_xtheadmempair),
149 ISA_EXT_DATA_ENTRY(xtheadsync, PRIV_VERSION_1_11_0, ext_xtheadsync),
150 ISA_EXT_DATA_ENTRY(xventanacondops, PRIV_VERSION_1_12_0, ext_XVentanaCondOps),
9a1f054d
AP
151};
152
153static bool isa_ext_is_enabled(RISCVCPU *cpu,
154 const struct isa_ext_data *edata)
155{
156 bool *ext_enabled = (void *)&cpu->cfg + edata->ext_enable_offset;
157
158 return *ext_enabled;
159}
160
161static void isa_ext_update_enabled(RISCVCPU *cpu,
162 const struct isa_ext_data *edata, bool en)
163{
164 bool *ext_enabled = (void *)&cpu->cfg + edata->ext_enable_offset;
165
166 *ext_enabled = en;
167}
168
dc5bd18f 169const char * const riscv_int_regnames[] = {
c45eff30
WL
170 "x0/zero", "x1/ra", "x2/sp", "x3/gp", "x4/tp", "x5/t0", "x6/t1",
171 "x7/t2", "x8/s0", "x9/s1", "x10/a0", "x11/a1", "x12/a2", "x13/a3",
172 "x14/a4", "x15/a5", "x16/a6", "x17/a7", "x18/s2", "x19/s3", "x20/s4",
173 "x21/s5", "x22/s6", "x23/s7", "x24/s8", "x25/s9", "x26/s10", "x27/s11",
174 "x28/t3", "x29/t4", "x30/t5", "x31/t6"
dc5bd18f
MC
175};
176
2b547084 177const char * const riscv_int_regnamesh[] = {
c45eff30
WL
178 "x0h/zeroh", "x1h/rah", "x2h/sph", "x3h/gph", "x4h/tph", "x5h/t0h",
179 "x6h/t1h", "x7h/t2h", "x8h/s0h", "x9h/s1h", "x10h/a0h", "x11h/a1h",
180 "x12h/a2h", "x13h/a3h", "x14h/a4h", "x15h/a5h", "x16h/a6h", "x17h/a7h",
181 "x18h/s2h", "x19h/s3h", "x20h/s4h", "x21h/s5h", "x22h/s6h", "x23h/s7h",
182 "x24h/s8h", "x25h/s9h", "x26h/s10h", "x27h/s11h", "x28h/t3h", "x29h/t4h",
183 "x30h/t5h", "x31h/t6h"
2b547084
FP
184};
185
dc5bd18f 186const char * const riscv_fpr_regnames[] = {
c45eff30
WL
187 "f0/ft0", "f1/ft1", "f2/ft2", "f3/ft3", "f4/ft4", "f5/ft5",
188 "f6/ft6", "f7/ft7", "f8/fs0", "f9/fs1", "f10/fa0", "f11/fa1",
189 "f12/fa2", "f13/fa3", "f14/fa4", "f15/fa5", "f16/fa6", "f17/fa7",
190 "f18/fs2", "f19/fs3", "f20/fs4", "f21/fs5", "f22/fs6", "f23/fs7",
191 "f24/fs8", "f25/fs9", "f26/fs10", "f27/fs11", "f28/ft8", "f29/ft9",
192 "f30/ft10", "f31/ft11"
dc5bd18f
MC
193};
194
b227f6a8
IK
195const char * const riscv_rvv_regnames[] = {
196 "v0", "v1", "v2", "v3", "v4", "v5", "v6",
197 "v7", "v8", "v9", "v10", "v11", "v12", "v13",
198 "v14", "v15", "v16", "v17", "v18", "v19", "v20",
199 "v21", "v22", "v23", "v24", "v25", "v26", "v27",
200 "v28", "v29", "v30", "v31"
201};
202
9a575d33 203static const char * const riscv_excp_names[] = {
dc5bd18f
MC
204 "misaligned_fetch",
205 "fault_fetch",
206 "illegal_instruction",
207 "breakpoint",
208 "misaligned_load",
209 "fault_load",
210 "misaligned_store",
211 "fault_store",
212 "user_ecall",
213 "supervisor_ecall",
214 "hypervisor_ecall",
215 "machine_ecall",
216 "exec_page_fault",
217 "load_page_fault",
218 "reserved",
fd990e86 219 "store_page_fault",
ab67a1d0
AF
220 "reserved",
221 "reserved",
222 "reserved",
223 "reserved",
224 "guest_exec_page_fault",
225 "guest_load_page_fault",
226 "reserved",
fd990e86 227 "guest_store_page_fault",
dc5bd18f
MC
228};
229
9a575d33 230static const char * const riscv_intr_names[] = {
dc5bd18f
MC
231 "u_software",
232 "s_software",
205377f8 233 "vs_software",
dc5bd18f
MC
234 "m_software",
235 "u_timer",
236 "s_timer",
205377f8 237 "vs_timer",
dc5bd18f
MC
238 "m_timer",
239 "u_external",
6cfcf775 240 "s_external",
205377f8 241 "vs_external",
dc5bd18f 242 "m_external",
426f0348
MC
243 "reserved",
244 "reserved",
245 "reserved",
246 "reserved"
dc5bd18f
MC
247};
248
dd8f244f 249static void riscv_cpu_add_user_properties(Object *obj);
26b2bc58 250
c51a3f5d
YJ
251const char *riscv_cpu_get_trap_name(target_ulong cause, bool async)
252{
253 if (async) {
254 return (cause < ARRAY_SIZE(riscv_intr_names)) ?
255 riscv_intr_names[cause] : "(unknown)";
256 } else {
257 return (cause < ARRAY_SIZE(riscv_excp_names)) ?
258 riscv_excp_names[cause] : "(unknown)";
259 }
260}
261
e91a7227 262static void set_misa(CPURISCVState *env, RISCVMXL mxl, uint32_t ext)
dc5bd18f 263{
e91a7227
RH
264 env->misa_mxl_max = env->misa_mxl = mxl;
265 env->misa_ext_mask = env->misa_ext = ext;
dc5bd18f
MC
266}
267
6f23aaeb
AG
268#ifndef CONFIG_USER_ONLY
269static uint8_t satp_mode_from_str(const char *satp_mode_str)
270{
271 if (!strncmp(satp_mode_str, "mbare", 5)) {
272 return VM_1_10_MBARE;
273 }
274
275 if (!strncmp(satp_mode_str, "sv32", 4)) {
276 return VM_1_10_SV32;
277 }
278
279 if (!strncmp(satp_mode_str, "sv39", 4)) {
280 return VM_1_10_SV39;
281 }
282
283 if (!strncmp(satp_mode_str, "sv48", 4)) {
284 return VM_1_10_SV48;
285 }
286
287 if (!strncmp(satp_mode_str, "sv57", 4)) {
288 return VM_1_10_SV57;
289 }
290
291 if (!strncmp(satp_mode_str, "sv64", 4)) {
292 return VM_1_10_SV64;
293 }
294
295 g_assert_not_reached();
296}
297
298uint8_t satp_mode_max_from_map(uint32_t map)
299{
300 /* map here has at least one bit set, so no problem with clz */
301 return 31 - __builtin_clz(map);
302}
303
304const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit)
305{
306 if (is_32_bit) {
307 switch (satp_mode) {
308 case VM_1_10_SV32:
309 return "sv32";
310 case VM_1_10_MBARE:
311 return "none";
312 }
313 } else {
314 switch (satp_mode) {
315 case VM_1_10_SV64:
316 return "sv64";
317 case VM_1_10_SV57:
318 return "sv57";
319 case VM_1_10_SV48:
320 return "sv48";
321 case VM_1_10_SV39:
322 return "sv39";
323 case VM_1_10_MBARE:
324 return "none";
325 }
326 }
327
328 g_assert_not_reached();
329}
330
6df3747a
AG
331static void set_satp_mode_max_supported(RISCVCPU *cpu,
332 uint8_t satp_mode)
6f23aaeb
AG
333{
334 bool rv32 = riscv_cpu_mxl(&cpu->env) == MXL_RV32;
6df3747a 335 const bool *valid_vm = rv32 ? valid_vm_1_10_32 : valid_vm_1_10_64;
6f23aaeb 336
6df3747a
AG
337 for (int i = 0; i <= satp_mode; ++i) {
338 if (valid_vm[i]) {
339 cpu->cfg.satp_mode.supported |= (1 << i);
340 }
6f23aaeb
AG
341 }
342}
6df3747a
AG
343
344/* Set the satp mode to the max supported */
345static void set_satp_mode_default_map(RISCVCPU *cpu)
346{
347 cpu->cfg.satp_mode.map = cpu->cfg.satp_mode.supported;
348}
6f23aaeb
AG
349#endif
350
dc5bd18f
MC
351static void riscv_any_cpu_init(Object *obj)
352{
7f0bdfb5
DHB
353 RISCVCPU *cpu = RISCV_CPU(obj);
354 CPURISCVState *env = &cpu->env;
3820602f 355#if defined(TARGET_RISCV32)
e91a7227 356 set_misa(env, MXL_RV32, RVI | RVM | RVA | RVF | RVD | RVC | RVU);
3820602f 357#elif defined(TARGET_RISCV64)
e91a7227 358 set_misa(env, MXL_RV64, RVI | RVM | RVA | RVF | RVD | RVC | RVU);
3820602f 359#endif
6df3747a
AG
360
361#ifndef CONFIG_USER_ONLY
362 set_satp_mode_max_supported(RISCV_CPU(obj),
c45eff30
WL
363 riscv_cpu_mxl(&RISCV_CPU(obj)->env) == MXL_RV32 ?
364 VM_1_10_SV32 : VM_1_10_SV57);
6df3747a
AG
365#endif
366
b9a2b98e 367 env->priv_ver = PRIV_VERSION_LATEST;
7f0bdfb5
DHB
368
369 /* inherited from parent obj via riscv_cpu_init() */
370 cpu->cfg.ext_ifencei = true;
371 cpu->cfg.ext_icsr = true;
372 cpu->cfg.mmu = true;
373 cpu->cfg.pmp = true;
dc5bd18f
MC
374}
375
094b072c
AF
376#if defined(TARGET_RISCV64)
377static void rv64_base_cpu_init(Object *obj)
8903bf6e
AF
378{
379 CPURISCVState *env = &RISCV_CPU(obj)->env;
b55d7d34 380 /* We set this in the realise function */
e91a7227 381 set_misa(env, MXL_RV64, 0);
dd8f244f 382 riscv_cpu_add_user_properties(obj);
18800095 383 /* Set latest version of privileged specification */
b9a2b98e 384 env->priv_ver = PRIV_VERSION_LATEST;
6df3747a
AG
385#ifndef CONFIG_USER_ONLY
386 set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV57);
387#endif
8903bf6e
AF
388}
389
114baaca 390static void rv64_sifive_u_cpu_init(Object *obj)
dc5bd18f 391{
7f0bdfb5
DHB
392 RISCVCPU *cpu = RISCV_CPU(obj);
393 CPURISCVState *env = &cpu->env;
e91a7227 394 set_misa(env, MXL_RV64, RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
8c6eeb50 395 env->priv_ver = PRIV_VERSION_1_10_0;
6df3747a
AG
396#ifndef CONFIG_USER_ONLY
397 set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV39);
398#endif
7f0bdfb5
DHB
399
400 /* inherited from parent obj via riscv_cpu_init() */
401 cpu->cfg.ext_ifencei = true;
402 cpu->cfg.ext_icsr = true;
403 cpu->cfg.mmu = true;
404 cpu->cfg.pmp = true;
dc5bd18f
MC
405}
406
114baaca 407static void rv64_sifive_e_cpu_init(Object *obj)
36b80ad9
AF
408{
409 CPURISCVState *env = &RISCV_CPU(obj)->env;
26b2bc58
AF
410 RISCVCPU *cpu = RISCV_CPU(obj);
411
e91a7227 412 set_misa(env, MXL_RV64, RVI | RVM | RVA | RVC | RVU);
8c6eeb50 413 env->priv_ver = PRIV_VERSION_1_10_0;
6df3747a
AG
414#ifndef CONFIG_USER_ONLY
415 set_satp_mode_max_supported(cpu, VM_1_10_MBARE);
416#endif
7f0bdfb5
DHB
417
418 /* inherited from parent obj via riscv_cpu_init() */
419 cpu->cfg.ext_ifencei = true;
420 cpu->cfg.ext_icsr = true;
421 cpu->cfg.pmp = true;
36b80ad9 422}
332dab68 423
95bd8daa
CM
424static void rv64_thead_c906_cpu_init(Object *obj)
425{
426 CPURISCVState *env = &RISCV_CPU(obj)->env;
427 RISCVCPU *cpu = RISCV_CPU(obj);
428
4f13abcb 429 set_misa(env, MXL_RV64, RVG | RVC | RVS | RVU);
8c6eeb50 430 env->priv_ver = PRIV_VERSION_1_11_0;
95bd8daa 431
95bd8daa
CM
432 cpu->cfg.ext_zfh = true;
433 cpu->cfg.mmu = true;
434 cpu->cfg.ext_xtheadba = true;
435 cpu->cfg.ext_xtheadbb = true;
436 cpu->cfg.ext_xtheadbs = true;
437 cpu->cfg.ext_xtheadcmo = true;
438 cpu->cfg.ext_xtheadcondmov = true;
439 cpu->cfg.ext_xtheadfmemidx = true;
440 cpu->cfg.ext_xtheadmac = true;
441 cpu->cfg.ext_xtheadmemidx = true;
442 cpu->cfg.ext_xtheadmempair = true;
443 cpu->cfg.ext_xtheadsync = true;
444
445 cpu->cfg.mvendorid = THEAD_VENDOR_ID;
6df3747a
AG
446#ifndef CONFIG_USER_ONLY
447 set_satp_mode_max_supported(cpu, VM_1_10_SV39);
448#endif
7f0bdfb5
DHB
449
450 /* inherited from parent obj via riscv_cpu_init() */
451 cpu->cfg.pmp = true;
95bd8daa
CM
452}
453
e1d084a8
RP
454static void rv64_veyron_v1_cpu_init(Object *obj)
455{
456 CPURISCVState *env = &RISCV_CPU(obj)->env;
457 RISCVCPU *cpu = RISCV_CPU(obj);
458
459 set_misa(env, MXL_RV64, RVG | RVC | RVS | RVU | RVH);
460 env->priv_ver = PRIV_VERSION_1_12_0;
461
462 /* Enable ISA extensions */
463 cpu->cfg.mmu = true;
029f5fee
DHB
464 cpu->cfg.ext_ifencei = true;
465 cpu->cfg.ext_icsr = true;
466 cpu->cfg.pmp = true;
e1d084a8
RP
467 cpu->cfg.ext_icbom = true;
468 cpu->cfg.cbom_blocksize = 64;
469 cpu->cfg.cboz_blocksize = 64;
470 cpu->cfg.ext_icboz = true;
471 cpu->cfg.ext_smaia = true;
472 cpu->cfg.ext_ssaia = true;
473 cpu->cfg.ext_sscofpmf = true;
474 cpu->cfg.ext_sstc = true;
475 cpu->cfg.ext_svinval = true;
476 cpu->cfg.ext_svnapot = true;
477 cpu->cfg.ext_svpbmt = true;
478 cpu->cfg.ext_smstateen = true;
479 cpu->cfg.ext_zba = true;
480 cpu->cfg.ext_zbb = true;
481 cpu->cfg.ext_zbc = true;
482 cpu->cfg.ext_zbs = true;
483 cpu->cfg.ext_XVentanaCondOps = true;
484
485 cpu->cfg.mvendorid = VEYRON_V1_MVENDORID;
486 cpu->cfg.marchid = VEYRON_V1_MARCHID;
487 cpu->cfg.mimpid = VEYRON_V1_MIMPID;
488
489#ifndef CONFIG_USER_ONLY
490 set_satp_mode_max_supported(cpu, VM_1_10_SV48);
491#endif
492}
493
332dab68
FP
494static void rv128_base_cpu_init(Object *obj)
495{
496 if (qemu_tcg_mttcg_enabled()) {
497 /* Missing 128-bit aligned atomics */
498 error_report("128-bit RISC-V currently does not work with Multi "
499 "Threaded TCG. Please use: -accel tcg,thread=single");
500 exit(EXIT_FAILURE);
501 }
502 CPURISCVState *env = &RISCV_CPU(obj)->env;
503 /* We set this in the realise function */
504 set_misa(env, MXL_RV128, 0);
dd8f244f 505 riscv_cpu_add_user_properties(obj);
18800095 506 /* Set latest version of privileged specification */
b9a2b98e 507 env->priv_ver = PRIV_VERSION_LATEST;
6df3747a
AG
508#ifndef CONFIG_USER_ONLY
509 set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV57);
510#endif
332dab68 511}
114baaca 512#else
094b072c
AF
513static void rv32_base_cpu_init(Object *obj)
514{
515 CPURISCVState *env = &RISCV_CPU(obj)->env;
516 /* We set this in the realise function */
e91a7227 517 set_misa(env, MXL_RV32, 0);
dd8f244f 518 riscv_cpu_add_user_properties(obj);
18800095 519 /* Set latest version of privileged specification */
b9a2b98e 520 env->priv_ver = PRIV_VERSION_LATEST;
6df3747a
AG
521#ifndef CONFIG_USER_ONLY
522 set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV32);
523#endif
094b072c
AF
524}
525
114baaca
AF
526static void rv32_sifive_u_cpu_init(Object *obj)
527{
7f0bdfb5
DHB
528 RISCVCPU *cpu = RISCV_CPU(obj);
529 CPURISCVState *env = &cpu->env;
e91a7227 530 set_misa(env, MXL_RV32, RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU);
8c6eeb50 531 env->priv_ver = PRIV_VERSION_1_10_0;
6df3747a
AG
532#ifndef CONFIG_USER_ONLY
533 set_satp_mode_max_supported(RISCV_CPU(obj), VM_1_10_SV32);
534#endif
7f0bdfb5
DHB
535
536 /* inherited from parent obj via riscv_cpu_init() */
537 cpu->cfg.ext_ifencei = true;
538 cpu->cfg.ext_icsr = true;
539 cpu->cfg.mmu = true;
540 cpu->cfg.pmp = true;
114baaca 541}
36b80ad9 542
114baaca
AF
543static void rv32_sifive_e_cpu_init(Object *obj)
544{
545 CPURISCVState *env = &RISCV_CPU(obj)->env;
26b2bc58
AF
546 RISCVCPU *cpu = RISCV_CPU(obj);
547
e91a7227 548 set_misa(env, MXL_RV32, RVI | RVM | RVA | RVC | RVU);
8c6eeb50 549 env->priv_ver = PRIV_VERSION_1_10_0;
6df3747a
AG
550#ifndef CONFIG_USER_ONLY
551 set_satp_mode_max_supported(cpu, VM_1_10_MBARE);
552#endif
7f0bdfb5
DHB
553
554 /* inherited from parent obj via riscv_cpu_init() */
555 cpu->cfg.ext_ifencei = true;
556 cpu->cfg.ext_icsr = true;
557 cpu->cfg.pmp = true;
114baaca 558}
d8e72bd1 559
e8905c6c 560static void rv32_ibex_cpu_init(Object *obj)
dc5bd18f
MC
561{
562 CPURISCVState *env = &RISCV_CPU(obj)->env;
26b2bc58
AF
563 RISCVCPU *cpu = RISCV_CPU(obj);
564
e91a7227 565 set_misa(env, MXL_RV32, RVI | RVM | RVC | RVU);
8c6eeb50 566 env->priv_ver = PRIV_VERSION_1_11_0;
6df3747a
AG
567#ifndef CONFIG_USER_ONLY
568 set_satp_mode_max_supported(cpu, VM_1_10_MBARE);
569#endif
26b2bc58 570 cpu->cfg.epmp = true;
7f0bdfb5
DHB
571
572 /* inherited from parent obj via riscv_cpu_init() */
573 cpu->cfg.ext_ifencei = true;
574 cpu->cfg.ext_icsr = true;
575 cpu->cfg.pmp = true;
dc5bd18f
MC
576}
577
2fdd2c09 578static void rv32_imafcu_nommu_cpu_init(Object *obj)
d784733b
CW
579{
580 CPURISCVState *env = &RISCV_CPU(obj)->env;
26b2bc58
AF
581 RISCVCPU *cpu = RISCV_CPU(obj);
582
e91a7227 583 set_misa(env, MXL_RV32, RVI | RVM | RVA | RVF | RVC | RVU);
8c6eeb50 584 env->priv_ver = PRIV_VERSION_1_10_0;
6df3747a
AG
585#ifndef CONFIG_USER_ONLY
586 set_satp_mode_max_supported(cpu, VM_1_10_MBARE);
587#endif
7f0bdfb5
DHB
588
589 /* inherited from parent obj via riscv_cpu_init() */
590 cpu->cfg.ext_ifencei = true;
591 cpu->cfg.ext_icsr = true;
592 cpu->cfg.pmp = true;
d784733b 593}
eab15862 594#endif
dc5bd18f 595
10f1ca27
YJ
596#if defined(CONFIG_KVM)
597static void riscv_host_cpu_init(Object *obj)
598{
599 CPURISCVState *env = &RISCV_CPU(obj)->env;
600#if defined(TARGET_RISCV32)
601 set_misa(env, MXL_RV32, 0);
602#elif defined(TARGET_RISCV64)
603 set_misa(env, MXL_RV64, 0);
604#endif
dd8f244f 605 riscv_cpu_add_user_properties(obj);
10f1ca27 606}
9638cbde 607#endif /* CONFIG_KVM */
10f1ca27 608
dc5bd18f
MC
609static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
610{
611 ObjectClass *oc;
612 char *typename;
613 char **cpuname;
614
615 cpuname = g_strsplit(cpu_model, ",", 1);
616 typename = g_strdup_printf(RISCV_CPU_TYPE_NAME("%s"), cpuname[0]);
617 oc = object_class_by_name(typename);
618 g_strfreev(cpuname);
619 g_free(typename);
620 if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) ||
621 object_class_is_abstract(oc)) {
622 return NULL;
623 }
624 return oc;
625}
626
90c84c56 627static void riscv_cpu_dump_state(CPUState *cs, FILE *f, int flags)
dc5bd18f
MC
628{
629 RISCVCPU *cpu = RISCV_CPU(cs);
630 CPURISCVState *env = &cpu->env;
b227f6a8
IK
631 int i, j;
632 uint8_t *p;
dc5bd18f 633
df30e652
AF
634#if !defined(CONFIG_USER_ONLY)
635 if (riscv_has_ext(env, RVH)) {
38256529 636 qemu_fprintf(f, " %s %d\n", "V = ", env->virt_enabled);
df30e652
AF
637 }
638#endif
90c84c56 639 qemu_fprintf(f, " %s " TARGET_FMT_lx "\n", "pc ", env->pc);
dc5bd18f 640#ifndef CONFIG_USER_ONLY
665b90d8
RH
641 {
642 static const int dump_csrs[] = {
643 CSR_MHARTID,
644 CSR_MSTATUS,
645 CSR_MSTATUSH,
bc7dca13
BM
646 /*
647 * CSR_SSTATUS is intentionally omitted here as its value
648 * can be figured out by looking at CSR_MSTATUS
649 */
665b90d8
RH
650 CSR_HSTATUS,
651 CSR_VSSTATUS,
652 CSR_MIP,
653 CSR_MIE,
654 CSR_MIDELEG,
655 CSR_HIDELEG,
656 CSR_MEDELEG,
657 CSR_HEDELEG,
658 CSR_MTVEC,
659 CSR_STVEC,
660 CSR_VSTVEC,
661 CSR_MEPC,
662 CSR_SEPC,
663 CSR_VSEPC,
664 CSR_MCAUSE,
665 CSR_SCAUSE,
666 CSR_VSCAUSE,
667 CSR_MTVAL,
668 CSR_STVAL,
669 CSR_HTVAL,
670 CSR_MTVAL2,
671 CSR_MSCRATCH,
672 CSR_SSCRATCH,
673 CSR_SATP,
bd5594ca
AB
674 CSR_MMTE,
675 CSR_UPMBASE,
676 CSR_UPMMASK,
677 CSR_SPMBASE,
678 CSR_SPMMASK,
679 CSR_MPMBASE,
680 CSR_MPMMASK,
665b90d8
RH
681 };
682
683 for (int i = 0; i < ARRAY_SIZE(dump_csrs); ++i) {
684 int csrno = dump_csrs[i];
685 target_ulong val = 0;
686 RISCVException res = riscv_csrrw_debug(env, csrno, &val, 0, 0);
687
688 /*
689 * Rely on the smode, hmode, etc, predicates within csr.c
690 * to do the filtering of the registers that are present.
691 */
692 if (res == RISCV_EXCP_NONE) {
693 qemu_fprintf(f, " %-8s " TARGET_FMT_lx "\n",
694 csr_ops[csrno].name, val);
695 }
696 }
df30e652 697 }
dc5bd18f
MC
698#endif
699
700 for (i = 0; i < 32; i++) {
e573a7f3 701 qemu_fprintf(f, " %-8s " TARGET_FMT_lx,
90c84c56 702 riscv_int_regnames[i], env->gpr[i]);
dc5bd18f 703 if ((i & 3) == 3) {
90c84c56 704 qemu_fprintf(f, "\n");
dc5bd18f
MC
705 }
706 }
86ea1880
RH
707 if (flags & CPU_DUMP_FPU) {
708 for (i = 0; i < 32; i++) {
e573a7f3 709 qemu_fprintf(f, " %-8s %016" PRIx64,
90c84c56 710 riscv_fpr_regnames[i], env->fpr[i]);
86ea1880 711 if ((i & 3) == 3) {
90c84c56 712 qemu_fprintf(f, "\n");
86ea1880 713 }
dc5bd18f
MC
714 }
715 }
b227f6a8
IK
716 if (riscv_has_ext(env, RVV) && (flags & CPU_DUMP_VPU)) {
717 static const int dump_rvv_csrs[] = {
718 CSR_VSTART,
719 CSR_VXSAT,
720 CSR_VXRM,
721 CSR_VCSR,
722 CSR_VL,
723 CSR_VTYPE,
724 CSR_VLENB,
725 };
726 for (int i = 0; i < ARRAY_SIZE(dump_rvv_csrs); ++i) {
727 int csrno = dump_rvv_csrs[i];
728 target_ulong val = 0;
729 RISCVException res = riscv_csrrw_debug(env, csrno, &val, 0, 0);
730
731 /*
732 * Rely on the smode, hmode, etc, predicates within csr.c
733 * to do the filtering of the registers that are present.
734 */
735 if (res == RISCV_EXCP_NONE) {
736 qemu_fprintf(f, " %-8s " TARGET_FMT_lx "\n",
737 csr_ops[csrno].name, val);
738 }
739 }
740 uint16_t vlenb = cpu->cfg.vlen >> 3;
741
742 for (i = 0; i < 32; i++) {
743 qemu_fprintf(f, " %-8s ", riscv_rvv_regnames[i]);
744 p = (uint8_t *)env->vreg;
745 for (j = vlenb - 1 ; j >= 0; j--) {
746 qemu_fprintf(f, "%02x", *(p + i * vlenb + BYTE(j)));
747 }
748 qemu_fprintf(f, "\n");
749 }
750 }
dc5bd18f
MC
751}
752
753static void riscv_cpu_set_pc(CPUState *cs, vaddr value)
754{
755 RISCVCPU *cpu = RISCV_CPU(cs);
756 CPURISCVState *env = &cpu->env;
bf9e776e
LZ
757
758 if (env->xl == MXL_RV32) {
759 env->pc = (int32_t)value;
760 } else {
761 env->pc = value;
762 }
dc5bd18f
MC
763}
764
e4fdf9df
RH
765static vaddr riscv_cpu_get_pc(CPUState *cs)
766{
767 RISCVCPU *cpu = RISCV_CPU(cs);
768 CPURISCVState *env = &cpu->env;
769
770 /* Match cpu_get_tb_cpu_state. */
771 if (env->xl == MXL_RV32) {
772 return env->pc & UINT32_MAX;
773 }
774 return env->pc;
775}
776
04a37d4c
RH
777static void riscv_cpu_synchronize_from_tb(CPUState *cs,
778 const TranslationBlock *tb)
dc5bd18f 779{
356c13f9
WL
780 if (!(tb_cflags(tb) & CF_PCREL)) {
781 RISCVCPU *cpu = RISCV_CPU(cs);
782 CPURISCVState *env = &cpu->env;
783 RISCVMXL xl = FIELD_EX32(tb->flags, TB_FLAGS, XL);
bf9e776e 784
356c13f9 785 tcg_debug_assert(!(cs->tcg_cflags & CF_PCREL));
0489d5bd 786
356c13f9
WL
787 if (xl == MXL_RV32) {
788 env->pc = (int32_t) tb->pc;
789 } else {
790 env->pc = tb->pc;
791 }
bf9e776e 792 }
dc5bd18f
MC
793}
794
795static bool riscv_cpu_has_work(CPUState *cs)
796{
797#ifndef CONFIG_USER_ONLY
798 RISCVCPU *cpu = RISCV_CPU(cs);
799 CPURISCVState *env = &cpu->env;
800 /*
801 * Definition of the WFI instruction requires it to ignore the privilege
802 * mode and delegation registers, but respect individual enables
803 */
8f42415f 804 return riscv_cpu_all_pending(env) != 0;
dc5bd18f
MC
805#else
806 return true;
807#endif
808}
809
ad1e84f5
RH
810static void riscv_restore_state_to_opc(CPUState *cs,
811 const TranslationBlock *tb,
812 const uint64_t *data)
dc5bd18f 813{
ad1e84f5
RH
814 RISCVCPU *cpu = RISCV_CPU(cs);
815 CPURISCVState *env = &cpu->env;
bf9e776e 816 RISCVMXL xl = FIELD_EX32(tb->flags, TB_FLAGS, XL);
356c13f9
WL
817 target_ulong pc;
818
819 if (tb_cflags(tb) & CF_PCREL) {
820 pc = (env->pc & TARGET_PAGE_MASK) | data[0];
821 } else {
822 pc = data[0];
823 }
ad1e84f5 824
bf9e776e 825 if (xl == MXL_RV32) {
356c13f9 826 env->pc = (int32_t)pc;
bf9e776e 827 } else {
356c13f9 828 env->pc = pc;
bf9e776e 829 }
62cf0245 830 env->bins = data[1];
dc5bd18f
MC
831}
832
4fa485a7 833static void riscv_cpu_reset_hold(Object *obj)
dc5bd18f 834{
43dc93af
AP
835#ifndef CONFIG_USER_ONLY
836 uint8_t iprio;
837 int i, irq, rdzero;
838#endif
4fa485a7 839 CPUState *cs = CPU(obj);
dc5bd18f
MC
840 RISCVCPU *cpu = RISCV_CPU(cs);
841 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu);
842 CPURISCVState *env = &cpu->env;
843
4fa485a7
PM
844 if (mcc->parent_phases.hold) {
845 mcc->parent_phases.hold(obj);
846 }
dc5bd18f 847#ifndef CONFIG_USER_ONLY
e91a7227 848 env->misa_mxl = env->misa_mxl_max;
dc5bd18f
MC
849 env->priv = PRV_M;
850 env->mstatus &= ~(MSTATUS_MIE | MSTATUS_MPRV);
92371bd9
RH
851 if (env->misa_mxl > MXL_RV32) {
852 /*
853 * The reset status of SXL/UXL is undefined, but mstatus is WARL
854 * and we must ensure that the value after init is valid for read.
855 */
856 env->mstatus = set_field(env->mstatus, MSTATUS64_SXL, env->misa_mxl);
857 env->mstatus = set_field(env->mstatus, MSTATUS64_UXL, env->misa_mxl);
5a2ae235
LZ
858 if (riscv_has_ext(env, RVH)) {
859 env->vsstatus = set_field(env->vsstatus,
860 MSTATUS64_SXL, env->misa_mxl);
861 env->vsstatus = set_field(env->vsstatus,
862 MSTATUS64_UXL, env->misa_mxl);
863 env->mstatus_hs = set_field(env->mstatus_hs,
864 MSTATUS64_SXL, env->misa_mxl);
865 env->mstatus_hs = set_field(env->mstatus_hs,
866 MSTATUS64_UXL, env->misa_mxl);
867 }
92371bd9 868 }
dc5bd18f 869 env->mcause = 0;
881df35d 870 env->miclaim = MIP_SGEIP;
dc5bd18f 871 env->pc = env->resetvec;
62cf0245 872 env->bins = 0;
ec352d0c 873 env->two_stage_lookup = false;
43dc93af 874
0af3f115
WL
875 env->menvcfg = (cpu->cfg.ext_svpbmt ? MENVCFG_PBMTE : 0) |
876 (cpu->cfg.ext_svadu ? MENVCFG_HADE : 0);
877 env->henvcfg = (cpu->cfg.ext_svpbmt ? HENVCFG_PBMTE : 0) |
878 (cpu->cfg.ext_svadu ? HENVCFG_HADE : 0);
7a6613da 879
43dc93af
AP
880 /* Initialized default priorities of local interrupts. */
881 for (i = 0; i < ARRAY_SIZE(env->miprio); i++) {
882 iprio = riscv_cpu_default_priority(i);
883 env->miprio[i] = (i == IRQ_M_EXT) ? 0 : iprio;
884 env->siprio[i] = (i == IRQ_S_EXT) ? 0 : iprio;
885 env->hviprio[i] = 0;
886 }
887 i = 0;
888 while (!riscv_cpu_hviprio_index2irq(i, &irq, &rdzero)) {
889 if (!rdzero) {
890 env->hviprio[irq] = env->miprio[irq];
891 }
892 i++;
893 }
4bbe8033 894 /* mmte is supposed to have pm.current hardwired to 1 */
42967f40 895 env->mmte |= (EXT_STATUS_INITIAL | MMTE_M_PM_CURRENT);
dc5bd18f 896#endif
440544e1 897 env->xl = riscv_cpu_mxl(env);
40bfa5f6 898 riscv_cpu_update_mask(env);
330d2ae3 899 cs->exception_index = RISCV_EXCP_NONE;
c13b169f 900 env->load_res = -1;
dc5bd18f 901 set_default_nan_mode(1, &env->fp_status);
ad40be27
YJ
902
903#ifndef CONFIG_USER_ONLY
cdfb2905 904 if (cpu->cfg.debug) {
b6092544
BM
905 riscv_trigger_init(env);
906 }
907
ad40be27
YJ
908 if (kvm_enabled()) {
909 kvm_riscv_reset_vcpu(cpu);
910 }
911#endif
dc5bd18f
MC
912}
913
914static void riscv_cpu_disas_set_info(CPUState *s, disassemble_info *info)
915{
5c5a47f1 916 RISCVCPU *cpu = RISCV_CPU(s);
94692c3a 917 CPURISCVState *env = &cpu->env;
454c2201 918 info->target_info = &cpu->cfg;
db23e5d9 919
94692c3a 920 switch (env->xl) {
db23e5d9 921 case MXL_RV32:
5c5a47f1 922 info->print_insn = print_insn_riscv32;
db23e5d9
RH
923 break;
924 case MXL_RV64:
5c5a47f1 925 info->print_insn = print_insn_riscv64;
db23e5d9 926 break;
332dab68
FP
927 case MXL_RV128:
928 info->print_insn = print_insn_riscv128;
929 break;
db23e5d9
RH
930 default:
931 g_assert_not_reached();
5c5a47f1 932 }
dc5bd18f
MC
933}
934
d63be184
DHB
935static void riscv_cpu_validate_v(CPURISCVState *env, RISCVCPUConfig *cfg,
936 Error **errp)
937{
938 int vext_version = VEXT_VERSION_1_00_0;
939
940 if (!is_power_of_2(cfg->vlen)) {
941 error_setg(errp, "Vector extension VLEN must be power of 2");
942 return;
943 }
944 if (cfg->vlen > RV_VLEN_MAX || cfg->vlen < 128) {
945 error_setg(errp,
946 "Vector extension implementation only supports VLEN "
947 "in the range [128, %d]", RV_VLEN_MAX);
948 return;
949 }
950 if (!is_power_of_2(cfg->elen)) {
951 error_setg(errp, "Vector extension ELEN must be power of 2");
952 return;
953 }
954 if (cfg->elen > 64 || cfg->elen < 8) {
955 error_setg(errp,
956 "Vector extension implementation only supports ELEN "
957 "in the range [8, 64]");
958 return;
959 }
960 if (cfg->vext_spec) {
961 if (!g_strcmp0(cfg->vext_spec, "v1.0")) {
962 vext_version = VEXT_VERSION_1_00_0;
963 } else {
964 error_setg(errp, "Unsupported vector spec version '%s'",
965 cfg->vext_spec);
966 return;
967 }
968 } else {
969 qemu_log("vector version is not specified, "
970 "use the default value v1.0\n");
971 }
2238c9d1 972 env->vext_ver = vext_version;
d63be184
DHB
973}
974
bd305595
DHB
975static void riscv_cpu_validate_priv_spec(RISCVCPU *cpu, Error **errp)
976{
977 CPURISCVState *env = &cpu->env;
978 int priv_version = -1;
979
980 if (cpu->cfg.priv_spec) {
981 if (!g_strcmp0(cpu->cfg.priv_spec, "v1.12.0")) {
982 priv_version = PRIV_VERSION_1_12_0;
983 } else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.11.0")) {
984 priv_version = PRIV_VERSION_1_11_0;
985 } else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.10.0")) {
986 priv_version = PRIV_VERSION_1_10_0;
987 } else {
988 error_setg(errp,
989 "Unsupported privilege spec version '%s'",
990 cpu->cfg.priv_spec);
991 return;
992 }
993
994 env->priv_ver = priv_version;
995 }
996}
997
998static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu)
999{
1000 CPURISCVState *env = &cpu->env;
1001 int i;
1002
1003 /* Force disable extensions if priv spec version does not match */
1004 for (i = 0; i < ARRAY_SIZE(isa_edata_arr); i++) {
1005 if (isa_ext_is_enabled(cpu, &isa_edata_arr[i]) &&
1006 (env->priv_ver < isa_edata_arr[i].min_version)) {
1007 isa_ext_update_enabled(cpu, &isa_edata_arr[i], false);
1008#ifndef CONFIG_USER_ONLY
1009 warn_report("disabling %s extension for hart 0x" TARGET_FMT_lx
1010 " because privilege spec version does not match",
1011 isa_edata_arr[i].name, env->mhartid);
1012#else
1013 warn_report("disabling %s extension because "
1014 "privilege spec version does not match",
1015 isa_edata_arr[i].name);
1016#endif
1017 }
1018 }
1019}
1020
f5664064
DHB
1021static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)
1022{
1023 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu);
1024 CPUClass *cc = CPU_CLASS(mcc);
1025 CPURISCVState *env = &cpu->env;
1026
1027 /* Validate that MISA_MXL is set properly. */
1028 switch (env->misa_mxl_max) {
1029#ifdef TARGET_RISCV64
1030 case MXL_RV64:
1031 case MXL_RV128:
1032 cc->gdb_core_xml_file = "riscv-64bit-cpu.xml";
1033 break;
1034#endif
1035 case MXL_RV32:
1036 cc->gdb_core_xml_file = "riscv-32bit-cpu.xml";
1037 break;
1038 default:
1039 g_assert_not_reached();
1040 }
1041
1042 if (env->misa_mxl_max != env->misa_mxl) {
1043 error_setg(errp, "misa_mxl_max must be equal to misa_mxl");
1044 return;
1045 }
1046}
1047
5ab10952
DHB
1048/*
1049 * Check consistency between chosen extensions while setting
1ffa805c 1050 * cpu->cfg accordingly.
5ab10952 1051 */
faf3b5d8 1052void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)
5ab10952
DHB
1053{
1054 CPURISCVState *env = &cpu->env;
d63be184 1055 Error *local_err = NULL;
5ab10952
DHB
1056
1057 /* Do some ISA extension error checking */
4f13abcb
DHB
1058 if (riscv_has_ext(env, RVG) &&
1059 !(riscv_has_ext(env, RVI) && riscv_has_ext(env, RVM) &&
1060 riscv_has_ext(env, RVA) && riscv_has_ext(env, RVF) &&
1061 riscv_has_ext(env, RVD) &&
1062 cpu->cfg.ext_icsr && cpu->cfg.ext_ifencei)) {
5ab10952 1063 warn_report("Setting G will also set IMAFD_Zicsr_Zifencei");
5ab10952
DHB
1064 cpu->cfg.ext_icsr = true;
1065 cpu->cfg.ext_ifencei = true;
1ffa805c
DHB
1066
1067 env->misa_ext |= RVI | RVM | RVA | RVF | RVD;
faf3b5d8 1068 env->misa_ext_mask |= RVI | RVM | RVA | RVF | RVD;
5ab10952
DHB
1069 }
1070
427d8e7d 1071 if (riscv_has_ext(env, RVI) && riscv_has_ext(env, RVE)) {
5ab10952
DHB
1072 error_setg(errp,
1073 "I and E extensions are incompatible");
1074 return;
1075 }
1076
427d8e7d 1077 if (!riscv_has_ext(env, RVI) && !riscv_has_ext(env, RVE)) {
5ab10952
DHB
1078 error_setg(errp,
1079 "Either I or E extension must be set");
1080 return;
1081 }
1082
e17801e1 1083 if (riscv_has_ext(env, RVS) && !riscv_has_ext(env, RVU)) {
5ab10952
DHB
1084 error_setg(errp,
1085 "Setting S extension without U extension is illegal");
1086 return;
1087 }
1088
b5c042e8 1089 if (riscv_has_ext(env, RVH) && !riscv_has_ext(env, RVI)) {
5ab10952
DHB
1090 error_setg(errp,
1091 "H depends on an I base integer ISA with 32 x registers");
1092 return;
1093 }
1094
b5c042e8 1095 if (riscv_has_ext(env, RVH) && !riscv_has_ext(env, RVS)) {
5ab10952
DHB
1096 error_setg(errp, "H extension implicitly requires S-mode");
1097 return;
1098 }
1099
4b33598f 1100 if (riscv_has_ext(env, RVF) && !cpu->cfg.ext_icsr) {
5ab10952
DHB
1101 error_setg(errp, "F extension requires Zicsr");
1102 return;
1103 }
1104
4c759943 1105 if ((cpu->cfg.ext_zawrs) && !riscv_has_ext(env, RVA)) {
5ab10952
DHB
1106 error_setg(errp, "Zawrs extension requires A extension");
1107 return;
1108 }
1109
1d2cb5a8
WL
1110 if (cpu->cfg.ext_zfh) {
1111 cpu->cfg.ext_zfhmin = true;
1112 }
1113
4b33598f 1114 if (cpu->cfg.ext_zfhmin && !riscv_has_ext(env, RVF)) {
5ab10952
DHB
1115 error_setg(errp, "Zfh/Zfhmin extensions require F extension");
1116 return;
1117 }
1118
4556fdaa
WL
1119 if (cpu->cfg.ext_zfbfmin && !riscv_has_ext(env, RVF)) {
1120 error_setg(errp, "Zfbfmin extension depends on F extension");
1121 return;
1122 }
1123
4b33598f 1124 if (riscv_has_ext(env, RVD) && !riscv_has_ext(env, RVF)) {
5ab10952
DHB
1125 error_setg(errp, "D extension requires F extension");
1126 return;
1127 }
1128
3e7674fd 1129 if (riscv_has_ext(env, RVV)) {
d63be184
DHB
1130 riscv_cpu_validate_v(env, &cpu->cfg, &local_err);
1131 if (local_err != NULL) {
1132 error_propagate(errp, local_err);
1133 return;
1134 }
1135
1136 /* The V vector extension depends on the Zve64d extension */
e7f0a803
WL
1137 cpu->cfg.ext_zve64d = true;
1138 }
1139
1140 /* The Zve64d extension depends on the Zve64f extension */
1141 if (cpu->cfg.ext_zve64d) {
1142 cpu->cfg.ext_zve64f = true;
1143 }
1144
1145 /* The Zve64f extension depends on the Zve32f extension */
1146 if (cpu->cfg.ext_zve64f) {
1147 cpu->cfg.ext_zve32f = true;
1148 }
1149
ffffd954 1150 if (cpu->cfg.ext_zve64d && !riscv_has_ext(env, RVD)) {
e7f0a803 1151 error_setg(errp, "Zve64d/V extensions require D extension");
5ab10952
DHB
1152 return;
1153 }
1154
4b33598f 1155 if (cpu->cfg.ext_zve32f && !riscv_has_ext(env, RVF)) {
5ab10952
DHB
1156 error_setg(errp, "Zve32f/Zve64f extensions require F extension");
1157 return;
1158 }
1159
2e60f9ec
WL
1160 if (cpu->cfg.ext_zvfh) {
1161 cpu->cfg.ext_zvfhmin = true;
1162 }
1163
1164 if (cpu->cfg.ext_zvfhmin && !cpu->cfg.ext_zve32f) {
1165 error_setg(errp, "Zvfh/Zvfhmin extensions require Zve32f extension");
1166 return;
1167 }
1168
1169 if (cpu->cfg.ext_zvfh && !cpu->cfg.ext_zfhmin) {
1170 error_setg(errp, "Zvfh extensions requires Zfhmin extension");
1171 return;
1172 }
1173
4556fdaa
WL
1174 if (cpu->cfg.ext_zvfbfmin && !cpu->cfg.ext_zfbfmin) {
1175 error_setg(errp, "Zvfbfmin extension depends on Zfbfmin extension");
1176 return;
1177 }
1178
1179 if (cpu->cfg.ext_zvfbfmin && !cpu->cfg.ext_zve32f) {
1180 error_setg(errp, "Zvfbfmin extension depends on Zve32f extension");
1181 return;
1182 }
1183
1184 if (cpu->cfg.ext_zvfbfwma && !cpu->cfg.ext_zvfbfmin) {
1185 error_setg(errp, "Zvfbfwma extension depends on Zvfbfmin extension");
1186 return;
1187 }
1188
5ab10952 1189 /* Set the ISA extensions, checks should have happened above */
a0d805f0
WL
1190 if (cpu->cfg.ext_zhinx) {
1191 cpu->cfg.ext_zhinxmin = true;
1192 }
1193
9ba63f94
WL
1194 if ((cpu->cfg.ext_zdinx || cpu->cfg.ext_zhinxmin) && !cpu->cfg.ext_zfinx) {
1195 error_setg(errp, "Zdinx/Zhinx/Zhinxmin extensions require Zfinx");
1196 return;
5ab10952
DHB
1197 }
1198
1199 if (cpu->cfg.ext_zfinx) {
1200 if (!cpu->cfg.ext_icsr) {
1201 error_setg(errp, "Zfinx extension requires Zicsr");
1202 return;
1203 }
4b33598f 1204 if (riscv_has_ext(env, RVF)) {
5ab10952 1205 error_setg(errp,
51f33081 1206 "Zfinx cannot be supported together with F extension");
5ab10952
DHB
1207 return;
1208 }
1209 }
1210
00d312bd
WL
1211 if (cpu->cfg.ext_zce) {
1212 cpu->cfg.ext_zca = true;
1213 cpu->cfg.ext_zcb = true;
1214 cpu->cfg.ext_zcmp = true;
1215 cpu->cfg.ext_zcmt = true;
4b33598f 1216 if (riscv_has_ext(env, RVF) && env->misa_mxl_max == MXL_RV32) {
00d312bd
WL
1217 cpu->cfg.ext_zcf = true;
1218 }
1219 }
1220
c00226e1 1221 if (riscv_has_ext(env, RVC)) {
2288a5ce 1222 cpu->cfg.ext_zca = true;
4b33598f 1223 if (riscv_has_ext(env, RVF) && env->misa_mxl_max == MXL_RV32) {
2288a5ce
WL
1224 cpu->cfg.ext_zcf = true;
1225 }
ffffd954 1226 if (riscv_has_ext(env, RVD)) {
2288a5ce
WL
1227 cpu->cfg.ext_zcd = true;
1228 }
1229 }
1230
1231 if (env->misa_mxl_max != MXL_RV32 && cpu->cfg.ext_zcf) {
1232 error_setg(errp, "Zcf extension is only relevant to RV32");
1233 return;
1234 }
1235
4b33598f 1236 if (!riscv_has_ext(env, RVF) && cpu->cfg.ext_zcf) {
2288a5ce
WL
1237 error_setg(errp, "Zcf extension requires F extension");
1238 return;
1239 }
1240
ffffd954 1241 if (!riscv_has_ext(env, RVD) && cpu->cfg.ext_zcd) {
2288a5ce
WL
1242 error_setg(errp, "Zcd extension requires D extension");
1243 return;
1244 }
1245
1246 if ((cpu->cfg.ext_zcf || cpu->cfg.ext_zcd || cpu->cfg.ext_zcb ||
1247 cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt) && !cpu->cfg.ext_zca) {
1248 error_setg(errp, "Zcf/Zcd/Zcb/Zcmp/Zcmt extensions require Zca "
1249 "extension");
1250 return;
1251 }
1252
1253 if (cpu->cfg.ext_zcd && (cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt)) {
1254 error_setg(errp, "Zcmp/Zcmt extensions are incompatible with "
1255 "Zcd extension");
1256 return;
1257 }
1258
1259 if (cpu->cfg.ext_zcmt && !cpu->cfg.ext_icsr) {
1260 error_setg(errp, "Zcmt extension requires Zicsr extension");
1261 return;
1262 }
1263
5ab10952
DHB
1264 if (cpu->cfg.ext_zk) {
1265 cpu->cfg.ext_zkn = true;
1266 cpu->cfg.ext_zkr = true;
1267 cpu->cfg.ext_zkt = true;
1268 }
1269
1270 if (cpu->cfg.ext_zkn) {
1271 cpu->cfg.ext_zbkb = true;
1272 cpu->cfg.ext_zbkc = true;
1273 cpu->cfg.ext_zbkx = true;
1274 cpu->cfg.ext_zkne = true;
1275 cpu->cfg.ext_zknd = true;
1276 cpu->cfg.ext_zknh = true;
1277 }
1278
1279 if (cpu->cfg.ext_zks) {
1280 cpu->cfg.ext_zbkb = true;
1281 cpu->cfg.ext_zbkc = true;
1282 cpu->cfg.ext_zbkx = true;
1283 cpu->cfg.ext_zksed = true;
1284 cpu->cfg.ext_zksh = true;
1285 }
bd305595
DHB
1286
1287 /*
1288 * Disable isa extensions based on priv spec after we
1289 * validated and set everything we need.
1290 */
1291 riscv_cpu_disable_priv_spec_isa_exts(cpu);
5ab10952
DHB
1292}
1293
6f23aaeb
AG
1294#ifndef CONFIG_USER_ONLY
1295static void riscv_cpu_satp_mode_finalize(RISCVCPU *cpu, Error **errp)
1296{
1297 bool rv32 = riscv_cpu_mxl(&cpu->env) == MXL_RV32;
6df3747a
AG
1298 uint8_t satp_mode_map_max;
1299 uint8_t satp_mode_supported_max =
1300 satp_mode_max_from_map(cpu->cfg.satp_mode.supported);
6f23aaeb
AG
1301
1302 if (cpu->cfg.satp_mode.map == 0) {
1303 if (cpu->cfg.satp_mode.init == 0) {
1304 /* If unset by the user, we fallback to the default satp mode. */
1305 set_satp_mode_default_map(cpu);
1306 } else {
1307 /*
1308 * Find the lowest level that was disabled and then enable the
1309 * first valid level below which can be found in
1310 * valid_vm_1_10_32/64.
1311 */
1312 for (int i = 1; i < 16; ++i) {
6df3747a
AG
1313 if ((cpu->cfg.satp_mode.init & (1 << i)) &&
1314 (cpu->cfg.satp_mode.supported & (1 << i))) {
6f23aaeb 1315 for (int j = i - 1; j >= 0; --j) {
6df3747a 1316 if (cpu->cfg.satp_mode.supported & (1 << j)) {
6f23aaeb
AG
1317 cpu->cfg.satp_mode.map |= (1 << j);
1318 break;
1319 }
1320 }
1321 break;
1322 }
1323 }
1324 }
1325 }
1326
6df3747a
AG
1327 satp_mode_map_max = satp_mode_max_from_map(cpu->cfg.satp_mode.map);
1328
1329 /* Make sure the user asked for a supported configuration (HW and qemu) */
1330 if (satp_mode_map_max > satp_mode_supported_max) {
1331 error_setg(errp, "satp_mode %s is higher than hw max capability %s",
1332 satp_mode_str(satp_mode_map_max, rv32),
1333 satp_mode_str(satp_mode_supported_max, rv32));
1334 return;
6f23aaeb
AG
1335 }
1336
1337 /*
1338 * Make sure the user did not ask for an invalid configuration as per
1339 * the specification.
1340 */
6f23aaeb 1341 if (!rv32) {
6df3747a 1342 for (int i = satp_mode_map_max - 1; i >= 0; --i) {
6f23aaeb
AG
1343 if (!(cpu->cfg.satp_mode.map & (1 << i)) &&
1344 (cpu->cfg.satp_mode.init & (1 << i)) &&
6df3747a 1345 (cpu->cfg.satp_mode.supported & (1 << i))) {
6f23aaeb
AG
1346 error_setg(errp, "cannot disable %s satp mode if %s "
1347 "is enabled", satp_mode_str(i, false),
6df3747a 1348 satp_mode_str(satp_mode_map_max, false));
6f23aaeb
AG
1349 return;
1350 }
1351 }
1352 }
1353
1354 /* Finally expand the map so that all valid modes are set */
6df3747a
AG
1355 for (int i = satp_mode_map_max - 1; i >= 0; --i) {
1356 if (cpu->cfg.satp_mode.supported & (1 << i)) {
6f23aaeb
AG
1357 cpu->cfg.satp_mode.map |= (1 << i);
1358 }
1359 }
1360}
1361#endif
1362
1363static void riscv_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
1364{
1365#ifndef CONFIG_USER_ONLY
1366 Error *local_err = NULL;
1367
1368 riscv_cpu_satp_mode_finalize(cpu, &local_err);
1369 if (local_err != NULL) {
1370 error_propagate(errp, local_err);
1371 return;
1372 }
1373#endif
1374}
1375
6508272a
DHB
1376static void riscv_cpu_validate_misa_priv(CPURISCVState *env, Error **errp)
1377{
1378 if (riscv_has_ext(env, RVH) && env->priv_ver < PRIV_VERSION_1_12_0) {
1379 error_setg(errp, "H extension requires priv spec 1.12.0");
1380 return;
1381 }
1382}
1383
eddabb6b 1384static void riscv_cpu_realize_tcg(DeviceState *dev, Error **errp)
dc5bd18f 1385{
c4e95030
AF
1386 RISCVCPU *cpu = RISCV_CPU(dev);
1387 CPURISCVState *env = &cpu->env;
dc5bd18f
MC
1388 Error *local_err = NULL;
1389
f5664064
DHB
1390 riscv_cpu_validate_misa_mxl(cpu, &local_err);
1391 if (local_err != NULL) {
1392 error_propagate(errp, local_err);
1393 return;
1394 }
1395
bd305595
DHB
1396 riscv_cpu_validate_priv_spec(cpu, &local_err);
1397 if (local_err != NULL) {
1398 error_propagate(errp, local_err);
1399 return;
a8b37120 1400 }
c4e95030 1401
6508272a
DHB
1402 riscv_cpu_validate_misa_priv(env, &local_err);
1403 if (local_err != NULL) {
1404 error_propagate(errp, local_err);
1405 return;
1406 }
1407
6a3ffda2 1408 if (cpu->cfg.epmp && !cpu->cfg.pmp) {
5da9514e
HW
1409 /*
1410 * Enhanced PMP should only be available
1411 * on harts with PMP support
1412 */
6a3ffda2
DHB
1413 error_setg(errp, "Invalid configuration: EPMP requires PMP support");
1414 return;
1acdb3b0
BM
1415 }
1416
5ab10952
DHB
1417 riscv_cpu_validate_set_extensions(cpu, &local_err);
1418 if (local_err != NULL) {
1419 error_propagate(errp, local_err);
1420 return;
b55d7d34
AF
1421 }
1422
14664483 1423#ifndef CONFIG_USER_ONLY
eddabb6b 1424 CPU(dev)->tcg_cflags |= CF_PCREL;
356c13f9 1425
e2fa85f4
DHB
1426 if (cpu->cfg.ext_sstc) {
1427 riscv_timer_init(cpu);
1428 }
1429
14664483
AP
1430 if (cpu->cfg.pmu_num) {
1431 if (!riscv_pmu_init(cpu, cpu->cfg.pmu_num) && cpu->cfg.ext_sscofpmf) {
1432 cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1433 riscv_pmu_timer_cb, cpu);
1434 }
1435 }
1436#endif
eddabb6b
DHB
1437}
1438
1439static void riscv_cpu_realize(DeviceState *dev, Error **errp)
1440{
1441 CPUState *cs = CPU(dev);
1442 RISCVCPU *cpu = RISCV_CPU(dev);
1443 RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(dev);
1444 Error *local_err = NULL;
1445
1446 cpu_exec_realizefn(cs, &local_err);
1447 if (local_err != NULL) {
1448 error_propagate(errp, local_err);
1449 return;
1450 }
1451
1452 if (tcg_enabled()) {
1453 riscv_cpu_realize_tcg(dev, &local_err);
1454 if (local_err != NULL) {
1455 error_propagate(errp, local_err);
1456 return;
1457 }
1458 }
14664483 1459
6f23aaeb
AG
1460 riscv_cpu_finalize_features(cpu, &local_err);
1461 if (local_err != NULL) {
1462 error_propagate(errp, local_err);
1463 return;
1464 }
1465
5371f5cd
JW
1466 riscv_cpu_register_gdb_regs_for_features(cs);
1467
dc5bd18f
MC
1468 qemu_init_vcpu(cs);
1469 cpu_reset(cs);
1470
1471 mcc->parent_realize(dev, errp);
1472}
1473
0f0b70ee 1474#ifndef CONFIG_USER_ONLY
6f23aaeb
AG
1475static void cpu_riscv_get_satp(Object *obj, Visitor *v, const char *name,
1476 void *opaque, Error **errp)
1477{
1478 RISCVSATPMap *satp_map = opaque;
1479 uint8_t satp = satp_mode_from_str(name);
1480 bool value;
1481
1482 value = satp_map->map & (1 << satp);
1483
1484 visit_type_bool(v, name, &value, errp);
1485}
1486
1487static void cpu_riscv_set_satp(Object *obj, Visitor *v, const char *name,
1488 void *opaque, Error **errp)
1489{
1490 RISCVSATPMap *satp_map = opaque;
1491 uint8_t satp = satp_mode_from_str(name);
1492 bool value;
1493
1494 if (!visit_type_bool(v, name, &value, errp)) {
1495 return;
1496 }
1497
1498 satp_map->map = deposit32(satp_map->map, satp, 1, value);
1499 satp_map->init |= 1 << satp;
1500}
1501
1502static void riscv_add_satp_mode_properties(Object *obj)
1503{
1504 RISCVCPU *cpu = RISCV_CPU(obj);
1505
1506 if (cpu->env.misa_mxl == MXL_RV32) {
1507 object_property_add(obj, "sv32", "bool", cpu_riscv_get_satp,
1508 cpu_riscv_set_satp, NULL, &cpu->cfg.satp_mode);
1509 } else {
1510 object_property_add(obj, "sv39", "bool", cpu_riscv_get_satp,
1511 cpu_riscv_set_satp, NULL, &cpu->cfg.satp_mode);
1512 object_property_add(obj, "sv48", "bool", cpu_riscv_get_satp,
1513 cpu_riscv_set_satp, NULL, &cpu->cfg.satp_mode);
1514 object_property_add(obj, "sv57", "bool", cpu_riscv_get_satp,
1515 cpu_riscv_set_satp, NULL, &cpu->cfg.satp_mode);
1516 object_property_add(obj, "sv64", "bool", cpu_riscv_get_satp,
1517 cpu_riscv_set_satp, NULL, &cpu->cfg.satp_mode);
1518 }
1519}
1520
0f0b70ee
AF
1521static void riscv_cpu_set_irq(void *opaque, int irq, int level)
1522{
1523 RISCVCPU *cpu = RISCV_CPU(opaque);
cd032fe7 1524 CPURISCVState *env = &cpu->env;
0f0b70ee 1525
cd032fe7
AP
1526 if (irq < IRQ_LOCAL_MAX) {
1527 switch (irq) {
1528 case IRQ_U_SOFT:
1529 case IRQ_S_SOFT:
1530 case IRQ_VS_SOFT:
1531 case IRQ_M_SOFT:
1532 case IRQ_U_TIMER:
1533 case IRQ_S_TIMER:
1534 case IRQ_VS_TIMER:
1535 case IRQ_M_TIMER:
1536 case IRQ_U_EXT:
cd032fe7
AP
1537 case IRQ_VS_EXT:
1538 case IRQ_M_EXT:
8b5c807b 1539 if (kvm_enabled()) {
cd032fe7 1540 kvm_riscv_set_irq(cpu, irq, level);
8b5c807b 1541 } else {
bbb9fc25 1542 riscv_cpu_update_mip(env, 1 << irq, BOOL_TO_MASK(level));
8b5c807b 1543 }
cd032fe7 1544 break;
33fe584f
AF
1545 case IRQ_S_EXT:
1546 if (kvm_enabled()) {
1547 kvm_riscv_set_irq(cpu, irq, level);
1548 } else {
1549 env->external_seip = level;
bbb9fc25 1550 riscv_cpu_update_mip(env, 1 << irq,
33fe584f
AF
1551 BOOL_TO_MASK(level | env->software_seip));
1552 }
1553 break;
cd032fe7
AP
1554 default:
1555 g_assert_not_reached();
2b650fbb 1556 }
cd032fe7
AP
1557 } else if (irq < (IRQ_LOCAL_MAX + IRQ_LOCAL_GUEST_MAX)) {
1558 /* Require H-extension for handling guest local interrupts */
1559 if (!riscv_has_ext(env, RVH)) {
1560 g_assert_not_reached();
1561 }
1562
1563 /* Compute bit position in HGEIP CSR */
1564 irq = irq - IRQ_LOCAL_MAX + 1;
1565 if (env->geilen < irq) {
1566 g_assert_not_reached();
1567 }
1568
1569 /* Update HGEIP CSR */
1570 env->hgeip &= ~((target_ulong)1 << irq);
1571 if (level) {
1572 env->hgeip |= (target_ulong)1 << irq;
1573 }
1574
1575 /* Update mip.SGEIP bit */
bbb9fc25 1576 riscv_cpu_update_mip(env, MIP_SGEIP,
cd032fe7
AP
1577 BOOL_TO_MASK(!!(env->hgeie & env->hgeip)));
1578 } else {
0f0b70ee
AF
1579 g_assert_not_reached();
1580 }
1581}
1582#endif /* CONFIG_USER_ONLY */
1583
dc5bd18f
MC
1584static void riscv_cpu_init(Object *obj)
1585{
dc5bd18f
MC
1586 RISCVCPU *cpu = RISCV_CPU(obj);
1587
7506ed90 1588 cpu_set_cpustate_pointers(cpu);
0f0b70ee
AF
1589
1590#ifndef CONFIG_USER_ONLY
cd032fe7
AP
1591 qdev_init_gpio_in(DEVICE(cpu), riscv_cpu_set_irq,
1592 IRQ_LOCAL_MAX + IRQ_LOCAL_GUEST_MAX);
0f0b70ee 1593#endif /* CONFIG_USER_ONLY */
dc5bd18f
MC
1594}
1595
b3df64c8
DHB
1596typedef struct RISCVCPUMisaExtConfig {
1597 const char *name;
1598 const char *description;
1599 target_ulong misa_bit;
1600 bool enabled;
1601} RISCVCPUMisaExtConfig;
1602
1603static void cpu_set_misa_ext_cfg(Object *obj, Visitor *v, const char *name,
1604 void *opaque, Error **errp)
1605{
1606 const RISCVCPUMisaExtConfig *misa_ext_cfg = opaque;
1607 target_ulong misa_bit = misa_ext_cfg->misa_bit;
1608 RISCVCPU *cpu = RISCV_CPU(obj);
1609 CPURISCVState *env = &cpu->env;
1610 bool value;
1611
1612 if (!visit_type_bool(v, name, &value, errp)) {
1613 return;
1614 }
1615
1616 if (value) {
1617 env->misa_ext |= misa_bit;
1618 env->misa_ext_mask |= misa_bit;
1619 } else {
1620 env->misa_ext &= ~misa_bit;
1621 env->misa_ext_mask &= ~misa_bit;
1622 }
1623}
1624
1625static void cpu_get_misa_ext_cfg(Object *obj, Visitor *v, const char *name,
1626 void *opaque, Error **errp)
1627{
1628 const RISCVCPUMisaExtConfig *misa_ext_cfg = opaque;
1629 target_ulong misa_bit = misa_ext_cfg->misa_bit;
1630 RISCVCPU *cpu = RISCV_CPU(obj);
1631 CPURISCVState *env = &cpu->env;
1632 bool value;
1633
1634 value = env->misa_ext & misa_bit;
1635
1636 visit_type_bool(v, name, &value, errp);
1637}
1638
ed7e6182
DHB
1639typedef struct misa_ext_info {
1640 const char *name;
1641 const char *description;
1642} MISAExtInfo;
1643
1644#define MISA_INFO_IDX(_bit) \
1645 __builtin_ctz(_bit)
1646
1647#define MISA_EXT_INFO(_bit, _propname, _descr) \
1648 [MISA_INFO_IDX(_bit)] = {.name = _propname, .description = _descr}
1649
1650static const MISAExtInfo misa_ext_info_arr[] = {
1651 MISA_EXT_INFO(RVA, "a", "Atomic instructions"),
1652 MISA_EXT_INFO(RVC, "c", "Compressed instructions"),
1653 MISA_EXT_INFO(RVD, "d", "Double-precision float point"),
1654 MISA_EXT_INFO(RVF, "f", "Single-precision float point"),
1655 MISA_EXT_INFO(RVI, "i", "Base integer instruction set"),
1656 MISA_EXT_INFO(RVE, "e", "Base integer instruction set (embedded)"),
1657 MISA_EXT_INFO(RVM, "m", "Integer multiplication and division"),
1658 MISA_EXT_INFO(RVS, "s", "Supervisor-level instructions"),
1659 MISA_EXT_INFO(RVU, "u", "User-level instructions"),
1660 MISA_EXT_INFO(RVH, "h", "Hypervisor"),
1661 MISA_EXT_INFO(RVJ, "x-j", "Dynamic translated languages"),
1662 MISA_EXT_INFO(RVV, "v", "Vector operations"),
1663 MISA_EXT_INFO(RVG, "g", "General purpose (IMAFD_Zicsr_Zifencei)"),
1664};
1665
1666static int riscv_validate_misa_info_idx(uint32_t bit)
1667{
1668 int idx;
1669
1670 /*
1671 * Our lowest valid input (RVA) is 1 and
1672 * __builtin_ctz() is UB with zero.
1673 */
1674 g_assert(bit != 0);
1675 idx = MISA_INFO_IDX(bit);
1676
1677 g_assert(idx < ARRAY_SIZE(misa_ext_info_arr));
1678 return idx;
1679}
1680
1681const char *riscv_get_misa_ext_name(uint32_t bit)
1682{
1683 int idx = riscv_validate_misa_info_idx(bit);
1684 const char *val = misa_ext_info_arr[idx].name;
1685
1686 g_assert(val != NULL);
1687 return val;
1688}
1689
1690const char *riscv_get_misa_ext_description(uint32_t bit)
1691{
1692 int idx = riscv_validate_misa_info_idx(bit);
1693 const char *val = misa_ext_info_arr[idx].description;
1694
1695 g_assert(val != NULL);
1696 return val;
1697}
1698
1699#define MISA_CFG(_bit, _enabled) \
1700 {.misa_bit = _bit, .enabled = _enabled}
1701
1702static RISCVCPUMisaExtConfig misa_ext_cfgs[] = {
1703 MISA_CFG(RVA, true),
1704 MISA_CFG(RVC, true),
1705 MISA_CFG(RVD, true),
1706 MISA_CFG(RVF, true),
1707 MISA_CFG(RVI, true),
1708 MISA_CFG(RVE, false),
1709 MISA_CFG(RVM, true),
1710 MISA_CFG(RVS, true),
1711 MISA_CFG(RVU, true),
1712 MISA_CFG(RVH, true),
1713 MISA_CFG(RVJ, false),
1714 MISA_CFG(RVV, false),
1715 MISA_CFG(RVG, false),
4c759943 1716};
b3df64c8
DHB
1717
1718static void riscv_cpu_add_misa_properties(Object *cpu_obj)
1719{
1720 int i;
1721
1722 for (i = 0; i < ARRAY_SIZE(misa_ext_cfgs); i++) {
ed7e6182
DHB
1723 RISCVCPUMisaExtConfig *misa_cfg = &misa_ext_cfgs[i];
1724 int bit = misa_cfg->misa_bit;
1725
1726 misa_cfg->name = riscv_get_misa_ext_name(bit);
1727 misa_cfg->description = riscv_get_misa_ext_description(bit);
b3df64c8 1728
92becce5
DHB
1729 /* Check if KVM already created the property */
1730 if (object_property_find(cpu_obj, misa_cfg->name)) {
1731 continue;
1732 }
1733
b3df64c8
DHB
1734 object_property_add(cpu_obj, misa_cfg->name, "bool",
1735 cpu_get_misa_ext_cfg,
1736 cpu_set_misa_ext_cfg,
1737 NULL, (void *)misa_cfg);
1738 object_property_set_description(cpu_obj, misa_cfg->name,
1739 misa_cfg->description);
1740 object_property_set_bool(cpu_obj, misa_cfg->name,
1741 misa_cfg->enabled, NULL);
1742 }
1743}
1744
26b2bc58 1745static Property riscv_cpu_extensions[] = {
9d3d60b7 1746 /* Defaults for standard extensions */
18d6d89e 1747 DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16),
14664483 1748 DEFINE_PROP_BOOL("sscofpmf", RISCVCPU, cfg.ext_sscofpmf, false),
9d3d60b7
AF
1749 DEFINE_PROP_BOOL("Zifencei", RISCVCPU, cfg.ext_ifencei, true),
1750 DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true),
4696f0ab 1751 DEFINE_PROP_BOOL("Zihintpause", RISCVCPU, cfg.ext_zihintpause, true),
260b594d 1752 DEFINE_PROP_BOOL("Zawrs", RISCVCPU, cfg.ext_zawrs, true),
13fb8c7b 1753 DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),
e5237730 1754 DEFINE_PROP_BOOL("Zfhmin", RISCVCPU, cfg.ext_zfhmin, false),
2fc1b44d 1755 DEFINE_PROP_BOOL("Zve32f", RISCVCPU, cfg.ext_zve32f, false),
bfefe406 1756 DEFINE_PROP_BOOL("Zve64f", RISCVCPU, cfg.ext_zve64f, false),
058d9d30 1757 DEFINE_PROP_BOOL("Zve64d", RISCVCPU, cfg.ext_zve64d, false),
9d3d60b7
AF
1758 DEFINE_PROP_BOOL("mmu", RISCVCPU, cfg.mmu, true),
1759 DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true),
43888c2f 1760 DEFINE_PROP_BOOL("sstc", RISCVCPU, cfg.ext_sstc, true),
9d3d60b7
AF
1761
1762 DEFINE_PROP_STRING("priv_spec", RISCVCPU, cfg.priv_spec),
9ec6622d
FC
1763 DEFINE_PROP_STRING("vext_spec", RISCVCPU, cfg.vext_spec),
1764 DEFINE_PROP_UINT16("vlen", RISCVCPU, cfg.vlen, 128),
1765 DEFINE_PROP_UINT16("elen", RISCVCPU, cfg.elen, 64),
9d3d60b7 1766
3594e3e5 1767 DEFINE_PROP_BOOL("smstateen", RISCVCPU, cfg.ext_smstateen, false),
62108f05 1768 DEFINE_PROP_BOOL("svadu", RISCVCPU, cfg.ext_svadu, true),
c5d77ddd 1769 DEFINE_PROP_BOOL("svinval", RISCVCPU, cfg.ext_svinval, false),
2bacb224 1770 DEFINE_PROP_BOOL("svnapot", RISCVCPU, cfg.ext_svnapot, false),
bbce8ba8 1771 DEFINE_PROP_BOOL("svpbmt", RISCVCPU, cfg.ext_svpbmt, false),
2bacb224 1772
0643c12e
VG
1773 DEFINE_PROP_BOOL("zba", RISCVCPU, cfg.ext_zba, true),
1774 DEFINE_PROP_BOOL("zbb", RISCVCPU, cfg.ext_zbb, true),
1775 DEFINE_PROP_BOOL("zbc", RISCVCPU, cfg.ext_zbc, true),
cf7ed971
WL
1776 DEFINE_PROP_BOOL("zbkb", RISCVCPU, cfg.ext_zbkb, false),
1777 DEFINE_PROP_BOOL("zbkc", RISCVCPU, cfg.ext_zbkc, false),
1778 DEFINE_PROP_BOOL("zbkx", RISCVCPU, cfg.ext_zbkx, false),
0643c12e 1779 DEFINE_PROP_BOOL("zbs", RISCVCPU, cfg.ext_zbs, true),
cf7ed971
WL
1780 DEFINE_PROP_BOOL("zk", RISCVCPU, cfg.ext_zk, false),
1781 DEFINE_PROP_BOOL("zkn", RISCVCPU, cfg.ext_zkn, false),
1782 DEFINE_PROP_BOOL("zknd", RISCVCPU, cfg.ext_zknd, false),
1783 DEFINE_PROP_BOOL("zkne", RISCVCPU, cfg.ext_zkne, false),
1784 DEFINE_PROP_BOOL("zknh", RISCVCPU, cfg.ext_zknh, false),
1785 DEFINE_PROP_BOOL("zkr", RISCVCPU, cfg.ext_zkr, false),
1786 DEFINE_PROP_BOOL("zks", RISCVCPU, cfg.ext_zks, false),
1787 DEFINE_PROP_BOOL("zksed", RISCVCPU, cfg.ext_zksed, false),
1788 DEFINE_PROP_BOOL("zksh", RISCVCPU, cfg.ext_zksh, false),
1789 DEFINE_PROP_BOOL("zkt", RISCVCPU, cfg.ext_zkt, false),
dfdb46a3 1790
6b1accef
WL
1791 DEFINE_PROP_BOOL("zdinx", RISCVCPU, cfg.ext_zdinx, false),
1792 DEFINE_PROP_BOOL("zfinx", RISCVCPU, cfg.ext_zfinx, false),
1793 DEFINE_PROP_BOOL("zhinx", RISCVCPU, cfg.ext_zhinx, false),
1794 DEFINE_PROP_BOOL("zhinxmin", RISCVCPU, cfg.ext_zhinxmin, false),
1795
e05da09b
CM
1796 DEFINE_PROP_BOOL("zicbom", RISCVCPU, cfg.ext_icbom, true),
1797 DEFINE_PROP_UINT16("cbom_blocksize", RISCVCPU, cfg.cbom_blocksize, 64),
a939c500
CM
1798 DEFINE_PROP_BOOL("zicboz", RISCVCPU, cfg.ext_icboz, true),
1799 DEFINE_PROP_UINT16("cboz_blocksize", RISCVCPU, cfg.cboz_blocksize, 64),
1800
6d00ffad
WL
1801 DEFINE_PROP_BOOL("zmmul", RISCVCPU, cfg.ext_zmmul, false),
1802
6672e29d
WL
1803 DEFINE_PROP_BOOL("zca", RISCVCPU, cfg.ext_zca, false),
1804 DEFINE_PROP_BOOL("zcb", RISCVCPU, cfg.ext_zcb, false),
1805 DEFINE_PROP_BOOL("zcd", RISCVCPU, cfg.ext_zcd, false),
1806 DEFINE_PROP_BOOL("zce", RISCVCPU, cfg.ext_zce, false),
1807 DEFINE_PROP_BOOL("zcf", RISCVCPU, cfg.ext_zcf, false),
1808 DEFINE_PROP_BOOL("zcmp", RISCVCPU, cfg.ext_zcmp, false),
1809 DEFINE_PROP_BOOL("zcmt", RISCVCPU, cfg.ext_zcmt, false),
1810
0d429bd2 1811 /* Vendor-specific custom extensions */
c9410a68 1812 DEFINE_PROP_BOOL("xtheadba", RISCVCPU, cfg.ext_xtheadba, false),
426c0491 1813 DEFINE_PROP_BOOL("xtheadbb", RISCVCPU, cfg.ext_xtheadbb, false),
fa134585 1814 DEFINE_PROP_BOOL("xtheadbs", RISCVCPU, cfg.ext_xtheadbs, false),
49a7f3aa 1815 DEFINE_PROP_BOOL("xtheadcmo", RISCVCPU, cfg.ext_xtheadcmo, false),
32909338 1816 DEFINE_PROP_BOOL("xtheadcondmov", RISCVCPU, cfg.ext_xtheadcondmov, false),
d4d90115 1817 DEFINE_PROP_BOOL("xtheadfmemidx", RISCVCPU, cfg.ext_xtheadfmemidx, false),
578086ba 1818 DEFINE_PROP_BOOL("xtheadfmv", RISCVCPU, cfg.ext_xtheadfmv, false),
b8a5832b 1819 DEFINE_PROP_BOOL("xtheadmac", RISCVCPU, cfg.ext_xtheadmac, false),
45f9df86 1820 DEFINE_PROP_BOOL("xtheadmemidx", RISCVCPU, cfg.ext_xtheadmemidx, false),
af99aa72 1821 DEFINE_PROP_BOOL("xtheadmempair", RISCVCPU, cfg.ext_xtheadmempair, false),
134c3ffa 1822 DEFINE_PROP_BOOL("xtheadsync", RISCVCPU, cfg.ext_xtheadsync, false),
0d429bd2
PT
1823 DEFINE_PROP_BOOL("xventanacondops", RISCVCPU, cfg.ext_XVentanaCondOps, false),
1824
dfdb46a3 1825 /* These are experimental so mark with 'x-' */
b8e1f32c 1826 DEFINE_PROP_BOOL("x-zicond", RISCVCPU, cfg.ext_zicond, false),
d364c0ab 1827
a44da25a 1828 /* ePMP 0.9.3 */
5da9514e 1829 DEFINE_PROP_BOOL("x-epmp", RISCVCPU, cfg.epmp, false),
dc9acc9c
AP
1830 DEFINE_PROP_BOOL("x-smaia", RISCVCPU, cfg.ext_smaia, false),
1831 DEFINE_PROP_BOOL("x-ssaia", RISCVCPU, cfg.ext_ssaia, false),
5da9514e 1832
058d9d30
WL
1833 DEFINE_PROP_BOOL("x-zvfh", RISCVCPU, cfg.ext_zvfh, false),
1834 DEFINE_PROP_BOOL("x-zvfhmin", RISCVCPU, cfg.ext_zvfhmin, false),
1835
889caa44
WL
1836 DEFINE_PROP_BOOL("x-zfbfmin", RISCVCPU, cfg.ext_zfbfmin, false),
1837 DEFINE_PROP_BOOL("x-zvfbfmin", RISCVCPU, cfg.ext_zvfbfmin, false),
1838 DEFINE_PROP_BOOL("x-zvfbfwma", RISCVCPU, cfg.ext_zvfbfwma, false),
1839
26b2bc58
AF
1840 DEFINE_PROP_END_OF_LIST(),
1841};
1842
56f0e992
DHB
1843
1844#ifndef CONFIG_USER_ONLY
1845static void cpu_set_cfg_unavailable(Object *obj, Visitor *v,
1846 const char *name,
1847 void *opaque, Error **errp)
1848{
1849 const char *propname = opaque;
1850 bool value;
1851
1852 if (!visit_type_bool(v, name, &value, errp)) {
1853 return;
1854 }
1855
1856 if (value) {
1857 error_setg(errp, "extension %s is not available with KVM",
1858 propname);
1859 }
1860}
1861#endif
1862
c66ffcd5 1863/*
dd8f244f
DHB
1864 * Add CPU properties with user-facing flags.
1865 *
1866 * This will overwrite existing env->misa_ext values with the
1867 * defaults set via riscv_cpu_add_misa_properties().
c66ffcd5 1868 */
dd8f244f 1869static void riscv_cpu_add_user_properties(Object *obj)
26b2bc58
AF
1870{
1871 Property *prop;
c01756a7 1872 DeviceState *dev = DEVICE(obj);
26b2bc58 1873
492265ae 1874#ifndef CONFIG_USER_ONLY
b71f9dca
DHB
1875 riscv_add_satp_mode_properties(obj);
1876
492265ae
DHB
1877 if (kvm_enabled()) {
1878 kvm_riscv_init_user_properties(obj);
1879 }
1880#endif
1881
b3df64c8
DHB
1882 riscv_cpu_add_misa_properties(obj);
1883
26b2bc58 1884 for (prop = riscv_cpu_extensions; prop && prop->name; prop++) {
f7a69fa6
DHB
1885#ifndef CONFIG_USER_ONLY
1886 if (kvm_enabled()) {
1887 /* Check if KVM created the property already */
1888 if (object_property_find(obj, prop->name)) {
1889 continue;
1890 }
56f0e992
DHB
1891
1892 /*
1893 * Set the default to disabled for every extension
1894 * unknown to KVM and error out if the user attempts
1895 * to enable any of them.
1896 *
1897 * We're giving a pass for non-bool properties since they're
1898 * not related to the availability of extensions and can be
1899 * safely ignored as is.
1900 */
1901 if (prop->info == &qdev_prop_bool) {
1902 object_property_add(obj, prop->name, "bool",
1903 NULL, cpu_set_cfg_unavailable,
1904 NULL, (void *)prop->name);
1905 continue;
1906 }
f7a69fa6
DHB
1907 }
1908#endif
26b2bc58
AF
1909 qdev_property_add_static(dev, prop);
1910 }
1911}
1912
1913static Property riscv_cpu_properties[] = {
1914 DEFINE_PROP_BOOL("debug", RISCVCPU, cfg.debug, true),
1915
277b210d
AF
1916#ifndef CONFIG_USER_ONLY
1917 DEFINE_PROP_UINT64("resetvec", RISCVCPU, env.resetvec, DEFAULT_RSTVEC),
1918#endif
a4a9a443
TO
1919
1920 DEFINE_PROP_BOOL("short-isa-string", RISCVCPU, cfg.short_isa_string, false),
b8312675 1921
1922 DEFINE_PROP_BOOL("rvv_ta_all_1s", RISCVCPU, cfg.rvv_ta_all_1s, false),
1ad3f9bd 1923 DEFINE_PROP_BOOL("rvv_ma_all_1s", RISCVCPU, cfg.rvv_ma_all_1s, false),
54bd9b6e
DHB
1924
1925 /*
1926 * write_misa() is marked as experimental for now so mark
1927 * it with -x and default to 'false'.
1928 */
1929 DEFINE_PROP_BOOL("x-misa-w", RISCVCPU, cfg.misa_w, false),
c4e95030
AF
1930 DEFINE_PROP_END_OF_LIST(),
1931};
1932
edf64786
SP
1933static gchar *riscv_gdb_arch_name(CPUState *cs)
1934{
1935 RISCVCPU *cpu = RISCV_CPU(cs);
1936 CPURISCVState *env = &cpu->env;
1937
db23e5d9
RH
1938 switch (riscv_cpu_mxl(env)) {
1939 case MXL_RV32:
edf64786 1940 return g_strdup("riscv:rv32");
db23e5d9 1941 case MXL_RV64:
332dab68 1942 case MXL_RV128:
edf64786 1943 return g_strdup("riscv:rv64");
db23e5d9
RH
1944 default:
1945 g_assert_not_reached();
edf64786
SP
1946 }
1947}
1948
b93777e1
BM
1949static const char *riscv_gdb_get_dynamic_xml(CPUState *cs, const char *xmlname)
1950{
1951 RISCVCPU *cpu = RISCV_CPU(cs);
1952
1953 if (strcmp(xmlname, "riscv-csr.xml") == 0) {
1954 return cpu->dyn_csr_xml;
719d3561
HW
1955 } else if (strcmp(xmlname, "riscv-vector.xml") == 0) {
1956 return cpu->dyn_vreg_xml;
b93777e1
BM
1957 }
1958
1959 return NULL;
1960}
1961
8b80bd28 1962#ifndef CONFIG_USER_ONLY
f1bd6f8e
MC
1963static int64_t riscv_get_arch_id(CPUState *cs)
1964{
1965 RISCVCPU *cpu = RISCV_CPU(cs);
1966
1967 return cpu->env.mhartid;
1968}
1969
8b80bd28
PMD
1970#include "hw/core/sysemu-cpu-ops.h"
1971
1972static const struct SysemuCPUOps riscv_sysemu_ops = {
08928c6d 1973 .get_phys_page_debug = riscv_cpu_get_phys_page_debug,
715e3c1a
PMD
1974 .write_elf64_note = riscv_cpu_write_elf64_note,
1975 .write_elf32_note = riscv_cpu_write_elf32_note,
feece4d0 1976 .legacy_vmsd = &vmstate_riscv_cpu,
8b80bd28
PMD
1977};
1978#endif
1979
78271684
CF
1980#include "hw/core/tcg-cpu-ops.h"
1981
11906557 1982static const struct TCGCPUOps riscv_tcg_ops = {
78271684
CF
1983 .initialize = riscv_translate_init,
1984 .synchronize_from_tb = riscv_cpu_synchronize_from_tb,
ad1e84f5 1985 .restore_state_to_opc = riscv_restore_state_to_opc,
78271684
CF
1986
1987#ifndef CONFIG_USER_ONLY
263e2ab2 1988 .tlb_fill = riscv_cpu_tlb_fill,
17b3c353 1989 .cpu_exec_interrupt = riscv_cpu_exec_interrupt,
78271684
CF
1990 .do_interrupt = riscv_cpu_do_interrupt,
1991 .do_transaction_failed = riscv_cpu_do_transaction_failed,
1992 .do_unaligned_access = riscv_cpu_do_unaligned_access,
b5f6379d
BM
1993 .debug_excp_handler = riscv_cpu_debug_excp_handler,
1994 .debug_check_breakpoint = riscv_cpu_debug_check_breakpoint,
1995 .debug_check_watchpoint = riscv_cpu_debug_check_watchpoint,
78271684
CF
1996#endif /* !CONFIG_USER_ONLY */
1997};
1998
1e341500
DHB
1999static bool riscv_cpu_is_dynamic(Object *cpu_obj)
2000{
2001 return object_dynamic_cast(cpu_obj, TYPE_RISCV_DYNAMIC_CPU) != NULL;
2002}
2003
2004static void cpu_set_mvendorid(Object *obj, Visitor *v, const char *name,
2005 void *opaque, Error **errp)
2006{
2007 bool dynamic_cpu = riscv_cpu_is_dynamic(obj);
2008 RISCVCPU *cpu = RISCV_CPU(obj);
2009 uint32_t prev_val = cpu->cfg.mvendorid;
2010 uint32_t value;
2011
2012 if (!visit_type_uint32(v, name, &value, errp)) {
2013 return;
2014 }
2015
2016 if (!dynamic_cpu && prev_val != value) {
2017 error_setg(errp, "Unable to change %s mvendorid (0x%x)",
2018 object_get_typename(obj), prev_val);
2019 return;
2020 }
2021
2022 cpu->cfg.mvendorid = value;
2023}
2024
2025static void cpu_get_mvendorid(Object *obj, Visitor *v, const char *name,
2026 void *opaque, Error **errp)
2027{
2028 bool value = RISCV_CPU(obj)->cfg.mvendorid;
2029
2030 visit_type_bool(v, name, &value, errp);
2031}
2032
a1863ad3
DHB
2033static void cpu_set_mimpid(Object *obj, Visitor *v, const char *name,
2034 void *opaque, Error **errp)
2035{
2036 bool dynamic_cpu = riscv_cpu_is_dynamic(obj);
2037 RISCVCPU *cpu = RISCV_CPU(obj);
2038 uint64_t prev_val = cpu->cfg.mimpid;
2039 uint64_t value;
2040
2041 if (!visit_type_uint64(v, name, &value, errp)) {
2042 return;
2043 }
2044
2045 if (!dynamic_cpu && prev_val != value) {
2046 error_setg(errp, "Unable to change %s mimpid (0x%" PRIu64 ")",
2047 object_get_typename(obj), prev_val);
2048 return;
2049 }
2050
2051 cpu->cfg.mimpid = value;
2052}
2053
2054static void cpu_get_mimpid(Object *obj, Visitor *v, const char *name,
2055 void *opaque, Error **errp)
2056{
2057 bool value = RISCV_CPU(obj)->cfg.mimpid;
2058
2059 visit_type_bool(v, name, &value, errp);
2060}
2061
d6a427e2
DHB
2062static void cpu_set_marchid(Object *obj, Visitor *v, const char *name,
2063 void *opaque, Error **errp)
2064{
2065 bool dynamic_cpu = riscv_cpu_is_dynamic(obj);
2066 RISCVCPU *cpu = RISCV_CPU(obj);
2067 uint64_t prev_val = cpu->cfg.marchid;
2068 uint64_t value, invalid_val;
2069 uint32_t mxlen = 0;
2070
2071 if (!visit_type_uint64(v, name, &value, errp)) {
2072 return;
2073 }
2074
2075 if (!dynamic_cpu && prev_val != value) {
2076 error_setg(errp, "Unable to change %s marchid (0x%" PRIu64 ")",
2077 object_get_typename(obj), prev_val);
2078 return;
2079 }
2080
2081 switch (riscv_cpu_mxl(&cpu->env)) {
2082 case MXL_RV32:
2083 mxlen = 32;
2084 break;
2085 case MXL_RV64:
2086 case MXL_RV128:
2087 mxlen = 64;
2088 break;
2089 default:
2090 g_assert_not_reached();
2091 }
2092
2093 invalid_val = 1LL << (mxlen - 1);
2094
2095 if (value == invalid_val) {
2096 error_setg(errp, "Unable to set marchid with MSB (%u) bit set "
2097 "and the remaining bits zero", mxlen);
2098 return;
2099 }
2100
2101 cpu->cfg.marchid = value;
2102}
2103
2104static void cpu_get_marchid(Object *obj, Visitor *v, const char *name,
2105 void *opaque, Error **errp)
2106{
2107 bool value = RISCV_CPU(obj)->cfg.marchid;
2108
2109 visit_type_bool(v, name, &value, errp);
2110}
2111
dc5bd18f
MC
2112static void riscv_cpu_class_init(ObjectClass *c, void *data)
2113{
2114 RISCVCPUClass *mcc = RISCV_CPU_CLASS(c);
2115 CPUClass *cc = CPU_CLASS(c);
2116 DeviceClass *dc = DEVICE_CLASS(c);
4fa485a7 2117 ResettableClass *rc = RESETTABLE_CLASS(c);
dc5bd18f 2118
41fbbba7
MZ
2119 device_class_set_parent_realize(dc, riscv_cpu_realize,
2120 &mcc->parent_realize);
dc5bd18f 2121
4fa485a7
PM
2122 resettable_class_set_parent_phases(rc, NULL, riscv_cpu_reset_hold, NULL,
2123 &mcc->parent_phases);
dc5bd18f
MC
2124
2125 cc->class_by_name = riscv_cpu_class_by_name;
2126 cc->has_work = riscv_cpu_has_work;
dc5bd18f
MC
2127 cc->dump_state = riscv_cpu_dump_state;
2128 cc->set_pc = riscv_cpu_set_pc;
e4fdf9df 2129 cc->get_pc = riscv_cpu_get_pc;
dc5bd18f
MC
2130 cc->gdb_read_register = riscv_cpu_gdb_read_register;
2131 cc->gdb_write_register = riscv_cpu_gdb_write_register;
5371f5cd 2132 cc->gdb_num_core_regs = 33;
dc5bd18f
MC
2133 cc->gdb_stop_before_watchpoint = true;
2134 cc->disas_set_info = riscv_cpu_disas_set_info;
8a4ca3c1 2135#ifndef CONFIG_USER_ONLY
8b80bd28 2136 cc->sysemu_ops = &riscv_sysemu_ops;
f1bd6f8e 2137 cc->get_arch_id = riscv_get_arch_id;
dc5bd18f 2138#endif
edf64786 2139 cc->gdb_arch_name = riscv_gdb_arch_name;
b93777e1 2140 cc->gdb_get_dynamic_xml = riscv_gdb_get_dynamic_xml;
78271684 2141 cc->tcg_ops = &riscv_tcg_ops;
6a3d2e7c 2142
1e341500
DHB
2143 object_class_property_add(c, "mvendorid", "uint32", cpu_get_mvendorid,
2144 cpu_set_mvendorid, NULL, NULL);
a1863ad3
DHB
2145
2146 object_class_property_add(c, "mimpid", "uint64", cpu_get_mimpid,
2147 cpu_set_mimpid, NULL, NULL);
d6a427e2
DHB
2148
2149 object_class_property_add(c, "marchid", "uint64", cpu_get_marchid,
2150 cpu_set_marchid, NULL, NULL);
1e341500 2151
4f67d30b 2152 device_class_set_props(dc, riscv_cpu_properties);
dc5bd18f
MC
2153}
2154
246f8796
WL
2155static void riscv_isa_string_ext(RISCVCPU *cpu, char **isa_str,
2156 int max_str_len)
a775398b
AP
2157{
2158 char *old = *isa_str;
2159 char *new = *isa_str;
2160 int i;
2161
a775398b 2162 for (i = 0; i < ARRAY_SIZE(isa_edata_arr); i++) {
63c7eedc 2163 if (isa_ext_is_enabled(cpu, &isa_edata_arr[i])) {
a775398b
AP
2164 new = g_strconcat(old, "_", isa_edata_arr[i].name, NULL);
2165 g_free(old);
2166 old = new;
2167 }
2168 }
2169
2170 *isa_str = new;
2171}
2172
dc5bd18f
MC
2173char *riscv_isa_string(RISCVCPU *cpu)
2174{
2175 int i;
0e2c3770 2176 const size_t maxlen = sizeof("rv128") + sizeof(riscv_single_letter_exts);
d1fd31f8
MC
2177 char *isa_str = g_new(char, maxlen);
2178 char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
0e2c3770
TO
2179 for (i = 0; i < sizeof(riscv_single_letter_exts) - 1; i++) {
2180 if (cpu->env.misa_ext & RV(riscv_single_letter_exts[i])) {
2181 *p++ = qemu_tolower(riscv_single_letter_exts[i]);
dc5bd18f
MC
2182 }
2183 }
d1fd31f8 2184 *p = '\0';
a4a9a443
TO
2185 if (!cpu->cfg.short_isa_string) {
2186 riscv_isa_string_ext(cpu, &isa_str, maxlen);
2187 }
d1fd31f8 2188 return isa_str;
dc5bd18f
MC
2189}
2190
eab15862 2191static gint riscv_cpu_list_compare(gconstpointer a, gconstpointer b)
dc5bd18f 2192{
eab15862
MC
2193 ObjectClass *class_a = (ObjectClass *)a;
2194 ObjectClass *class_b = (ObjectClass *)b;
2195 const char *name_a, *name_b;
dc5bd18f 2196
eab15862
MC
2197 name_a = object_class_get_name(class_a);
2198 name_b = object_class_get_name(class_b);
2199 return strcmp(name_a, name_b);
dc5bd18f
MC
2200}
2201
eab15862 2202static void riscv_cpu_list_entry(gpointer data, gpointer user_data)
dc5bd18f 2203{
eab15862
MC
2204 const char *typename = object_class_get_name(OBJECT_CLASS(data));
2205 int len = strlen(typename) - strlen(RISCV_CPU_TYPE_SUFFIX);
dc5bd18f 2206
0442428a 2207 qemu_printf("%.*s\n", len, typename);
eab15862 2208}
dc5bd18f 2209
0442428a 2210void riscv_cpu_list(void)
eab15862 2211{
eab15862
MC
2212 GSList *list;
2213
2214 list = object_class_get_list(TYPE_RISCV_CPU, false);
2215 list = g_slist_sort(list, riscv_cpu_list_compare);
0442428a 2216 g_slist_foreach(list, riscv_cpu_list_entry, NULL);
eab15862 2217 g_slist_free(list);
dc5bd18f
MC
2218}
2219
eab15862
MC
2220#define DEFINE_CPU(type_name, initfn) \
2221 { \
2222 .name = type_name, \
2223 .parent = TYPE_RISCV_CPU, \
2224 .instance_init = initfn \
2225 }
2226
9e1a30d3
DHB
2227#define DEFINE_DYNAMIC_CPU(type_name, initfn) \
2228 { \
2229 .name = type_name, \
2230 .parent = TYPE_RISCV_DYNAMIC_CPU, \
2231 .instance_init = initfn \
2232 }
2233
eab15862
MC
2234static const TypeInfo riscv_cpu_type_infos[] = {
2235 {
2236 .name = TYPE_RISCV_CPU,
2237 .parent = TYPE_CPU,
2238 .instance_size = sizeof(RISCVCPU),
5de5b99b 2239 .instance_align = __alignof__(RISCVCPU),
eab15862
MC
2240 .instance_init = riscv_cpu_init,
2241 .abstract = true,
2242 .class_size = sizeof(RISCVCPUClass),
2243 .class_init = riscv_cpu_class_init,
2244 },
9e1a30d3
DHB
2245 {
2246 .name = TYPE_RISCV_DYNAMIC_CPU,
2247 .parent = TYPE_RISCV_CPU,
2248 .abstract = true,
2249 },
2250 DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_ANY, riscv_any_cpu_init),
10f1ca27
YJ
2251#if defined(CONFIG_KVM)
2252 DEFINE_CPU(TYPE_RISCV_CPU_HOST, riscv_host_cpu_init),
2253#endif
eab15862 2254#if defined(TARGET_RISCV32)
9e1a30d3 2255 DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE32, rv32_base_cpu_init),
e8905c6c 2256 DEFINE_CPU(TYPE_RISCV_CPU_IBEX, rv32_ibex_cpu_init),
114baaca 2257 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31, rv32_sifive_e_cpu_init),
2fdd2c09 2258 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34, rv32_imafcu_nommu_cpu_init),
114baaca 2259 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34, rv32_sifive_u_cpu_init),
eab15862 2260#elif defined(TARGET_RISCV64)
9e1a30d3 2261 DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE64, rv64_base_cpu_init),
114baaca
AF
2262 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E51, rv64_sifive_e_cpu_init),
2263 DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U54, rv64_sifive_u_cpu_init),
6ddc7069 2264 DEFINE_CPU(TYPE_RISCV_CPU_SHAKTI_C, rv64_sifive_u_cpu_init),
95bd8daa 2265 DEFINE_CPU(TYPE_RISCV_CPU_THEAD_C906, rv64_thead_c906_cpu_init),
e1d084a8 2266 DEFINE_CPU(TYPE_RISCV_CPU_VEYRON_V1, rv64_veyron_v1_cpu_init),
9e1a30d3 2267 DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE128, rv128_base_cpu_init),
eab15862
MC
2268#endif
2269};
2270
2271DEFINE_TYPES(riscv_cpu_type_infos)