xref: /openbmc/qemu/target/riscv/tcg/tcg-cpu.c (revision 078189b327ae5c5727b51ec714d9663b1d0ca3df)
1 /*
2  * riscv TCG cpu class initialization
3  *
4  * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5  * Copyright (c) 2017-2018 SiFive, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU General Public License,
9  * version 2 or later, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "exec/exec-all.h"
22 #include "tcg-cpu.h"
23 #include "cpu.h"
24 #include "pmu.h"
25 #include "time_helper.h"
26 #include "qapi/error.h"
27 #include "qapi/visitor.h"
28 #include "qemu/accel.h"
29 #include "qemu/error-report.h"
30 #include "qemu/log.h"
31 #include "hw/core/accel-cpu.h"
32 #include "hw/core/tcg-cpu-ops.h"
33 #include "tcg/tcg.h"
34 
35 /* Hash that stores user set extensions */
36 static GHashTable *multi_ext_user_opts;
37 static GHashTable *misa_ext_user_opts;
38 
39 static bool cpu_cfg_ext_is_user_set(uint32_t ext_offset)
40 {
41     return g_hash_table_contains(multi_ext_user_opts,
42                                  GUINT_TO_POINTER(ext_offset));
43 }
44 
45 static bool cpu_misa_ext_is_user_set(uint32_t misa_bit)
46 {
47     return g_hash_table_contains(misa_ext_user_opts,
48                                  GUINT_TO_POINTER(misa_bit));
49 }
50 
51 static void cpu_cfg_ext_add_user_opt(uint32_t ext_offset, bool value)
52 {
53     g_hash_table_insert(multi_ext_user_opts, GUINT_TO_POINTER(ext_offset),
54                         (gpointer)value);
55 }
56 
57 static void cpu_misa_ext_add_user_opt(uint32_t bit, bool value)
58 {
59     g_hash_table_insert(misa_ext_user_opts, GUINT_TO_POINTER(bit),
60                         (gpointer)value);
61 }
62 
63 static void riscv_cpu_write_misa_bit(RISCVCPU *cpu, uint32_t bit,
64                                      bool enabled)
65 {
66     CPURISCVState *env = &cpu->env;
67 
68     if (enabled) {
69         env->misa_ext |= bit;
70         env->misa_ext_mask |= bit;
71     } else {
72         env->misa_ext &= ~bit;
73         env->misa_ext_mask &= ~bit;
74     }
75 }
76 
77 static const char *cpu_priv_ver_to_str(int priv_ver)
78 {
79     switch (priv_ver) {
80     case PRIV_VERSION_1_10_0:
81         return "v1.10.0";
82     case PRIV_VERSION_1_11_0:
83         return "v1.11.0";
84     case PRIV_VERSION_1_12_0:
85         return "v1.12.0";
86     }
87 
88     g_assert_not_reached();
89 }
90 
91 static void riscv_cpu_synchronize_from_tb(CPUState *cs,
92                                           const TranslationBlock *tb)
93 {
94     if (!(tb_cflags(tb) & CF_PCREL)) {
95         RISCVCPU *cpu = RISCV_CPU(cs);
96         CPURISCVState *env = &cpu->env;
97         RISCVMXL xl = FIELD_EX32(tb->flags, TB_FLAGS, XL);
98 
99         tcg_debug_assert(!(cs->tcg_cflags & CF_PCREL));
100 
101         if (xl == MXL_RV32) {
102             env->pc = (int32_t) tb->pc;
103         } else {
104             env->pc = tb->pc;
105         }
106     }
107 }
108 
109 static void riscv_restore_state_to_opc(CPUState *cs,
110                                        const TranslationBlock *tb,
111                                        const uint64_t *data)
112 {
113     RISCVCPU *cpu = RISCV_CPU(cs);
114     CPURISCVState *env = &cpu->env;
115     RISCVMXL xl = FIELD_EX32(tb->flags, TB_FLAGS, XL);
116     target_ulong pc;
117 
118     if (tb_cflags(tb) & CF_PCREL) {
119         pc = (env->pc & TARGET_PAGE_MASK) | data[0];
120     } else {
121         pc = data[0];
122     }
123 
124     if (xl == MXL_RV32) {
125         env->pc = (int32_t)pc;
126     } else {
127         env->pc = pc;
128     }
129     env->bins = data[1];
130 }
131 
132 static const TCGCPUOps riscv_tcg_ops = {
133     .initialize = riscv_translate_init,
134     .synchronize_from_tb = riscv_cpu_synchronize_from_tb,
135     .restore_state_to_opc = riscv_restore_state_to_opc,
136 
137 #ifndef CONFIG_USER_ONLY
138     .tlb_fill = riscv_cpu_tlb_fill,
139     .cpu_exec_interrupt = riscv_cpu_exec_interrupt,
140     .do_interrupt = riscv_cpu_do_interrupt,
141     .do_transaction_failed = riscv_cpu_do_transaction_failed,
142     .do_unaligned_access = riscv_cpu_do_unaligned_access,
143     .debug_excp_handler = riscv_cpu_debug_excp_handler,
144     .debug_check_breakpoint = riscv_cpu_debug_check_breakpoint,
145     .debug_check_watchpoint = riscv_cpu_debug_check_watchpoint,
146 #endif /* !CONFIG_USER_ONLY */
147 };
148 
149 static int cpu_cfg_ext_get_min_version(uint32_t ext_offset)
150 {
151     const RISCVIsaExtData *edata;
152 
153     for (edata = isa_edata_arr; edata && edata->name; edata++) {
154         if (edata->ext_enable_offset != ext_offset) {
155             continue;
156         }
157 
158         return edata->min_version;
159     }
160 
161     g_assert_not_reached();
162 }
163 
164 static const char *cpu_cfg_ext_get_name(uint32_t ext_offset)
165 {
166     const RISCVCPUMultiExtConfig *feat;
167     const RISCVIsaExtData *edata;
168 
169     for (edata = isa_edata_arr; edata->name != NULL; edata++) {
170         if (edata->ext_enable_offset == ext_offset) {
171             return edata->name;
172         }
173     }
174 
175     for (feat = riscv_cpu_named_features; feat->name != NULL; feat++) {
176         if (feat->offset == ext_offset) {
177             return feat->name;
178         }
179     }
180 
181     g_assert_not_reached();
182 }
183 
184 static bool cpu_cfg_offset_is_named_feat(uint32_t ext_offset)
185 {
186     const RISCVCPUMultiExtConfig *feat;
187 
188     for (feat = riscv_cpu_named_features; feat->name != NULL; feat++) {
189         if (feat->offset == ext_offset) {
190             return true;
191         }
192     }
193 
194     return false;
195 }
196 
197 static void riscv_cpu_enable_named_feat(RISCVCPU *cpu, uint32_t feat_offset)
198 {
199      /*
200       * All other named features are already enabled
201       * in riscv_tcg_cpu_instance_init().
202       */
203     if (feat_offset == CPU_CFG_OFFSET(ext_zic64b)) {
204         cpu->cfg.cbom_blocksize = 64;
205         cpu->cfg.cbop_blocksize = 64;
206         cpu->cfg.cboz_blocksize = 64;
207     }
208 }
209 
210 static void cpu_bump_multi_ext_priv_ver(CPURISCVState *env,
211                                         uint32_t ext_offset)
212 {
213     int ext_priv_ver;
214 
215     if (env->priv_ver == PRIV_VERSION_LATEST) {
216         return;
217     }
218 
219     ext_priv_ver = cpu_cfg_ext_get_min_version(ext_offset);
220 
221     if (env->priv_ver < ext_priv_ver) {
222         /*
223          * Note: the 'priv_spec' command line option, if present,
224          * will take precedence over this priv_ver bump.
225          */
226         env->priv_ver = ext_priv_ver;
227     }
228 }
229 
230 static void cpu_cfg_ext_auto_update(RISCVCPU *cpu, uint32_t ext_offset,
231                                     bool value)
232 {
233     CPURISCVState *env = &cpu->env;
234     bool prev_val = isa_ext_is_enabled(cpu, ext_offset);
235     int min_version;
236 
237     if (prev_val == value) {
238         return;
239     }
240 
241     if (cpu_cfg_ext_is_user_set(ext_offset)) {
242         return;
243     }
244 
245     if (value && env->priv_ver != PRIV_VERSION_LATEST) {
246         /* Do not enable it if priv_ver is older than min_version */
247         min_version = cpu_cfg_ext_get_min_version(ext_offset);
248         if (env->priv_ver < min_version) {
249             return;
250         }
251     }
252 
253     isa_ext_update_enabled(cpu, ext_offset, value);
254 }
255 
256 static void riscv_cpu_validate_misa_priv(CPURISCVState *env, Error **errp)
257 {
258     if (riscv_has_ext(env, RVH) && env->priv_ver < PRIV_VERSION_1_12_0) {
259         error_setg(errp, "H extension requires priv spec 1.12.0");
260         return;
261     }
262 }
263 
264 static void riscv_cpu_validate_v(CPURISCVState *env, RISCVCPUConfig *cfg,
265                                  Error **errp)
266 {
267     uint32_t vlen = cfg->vlenb << 3;
268 
269     if (vlen > RV_VLEN_MAX || vlen < 128) {
270         error_setg(errp,
271                    "Vector extension implementation only supports VLEN "
272                    "in the range [128, %d]", RV_VLEN_MAX);
273         return;
274     }
275 
276     if (cfg->elen > 64 || cfg->elen < 8) {
277         error_setg(errp,
278                    "Vector extension implementation only supports ELEN "
279                    "in the range [8, 64]");
280         return;
281     }
282 }
283 
284 static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu)
285 {
286     CPURISCVState *env = &cpu->env;
287     const RISCVIsaExtData *edata;
288 
289     /* Force disable extensions if priv spec version does not match */
290     for (edata = isa_edata_arr; edata && edata->name; edata++) {
291         if (isa_ext_is_enabled(cpu, edata->ext_enable_offset) &&
292             (env->priv_ver < edata->min_version)) {
293             /*
294              * These two extensions are always enabled as they were supported
295              * by QEMU before they were added as extensions in the ISA.
296              */
297             if (!strcmp(edata->name, "zicntr") ||
298                 !strcmp(edata->name, "zihpm")) {
299                 continue;
300             }
301 
302             isa_ext_update_enabled(cpu, edata->ext_enable_offset, false);
303 #ifndef CONFIG_USER_ONLY
304             warn_report("disabling %s extension for hart 0x" TARGET_FMT_lx
305                         " because privilege spec version does not match",
306                         edata->name, env->mhartid);
307 #else
308             warn_report("disabling %s extension because "
309                         "privilege spec version does not match",
310                         edata->name);
311 #endif
312         }
313     }
314 }
315 
316 static void riscv_cpu_update_named_features(RISCVCPU *cpu)
317 {
318     if (cpu->env.priv_ver >= PRIV_VERSION_1_11_0) {
319         cpu->cfg.has_priv_1_11 = true;
320     }
321 
322     if (cpu->env.priv_ver >= PRIV_VERSION_1_12_0) {
323         cpu->cfg.has_priv_1_12 = true;
324     }
325 
326     /* zic64b is 1.12 or later */
327     cpu->cfg.ext_zic64b = cpu->cfg.cbom_blocksize == 64 &&
328                           cpu->cfg.cbop_blocksize == 64 &&
329                           cpu->cfg.cboz_blocksize == 64 &&
330                           cpu->cfg.has_priv_1_12;
331 }
332 
333 static void riscv_cpu_validate_g(RISCVCPU *cpu)
334 {
335     const char *warn_msg = "RVG mandates disabled extension %s";
336     uint32_t g_misa_bits[] = {RVI, RVM, RVA, RVF, RVD};
337     bool send_warn = cpu_misa_ext_is_user_set(RVG);
338 
339     for (int i = 0; i < ARRAY_SIZE(g_misa_bits); i++) {
340         uint32_t bit = g_misa_bits[i];
341 
342         if (riscv_has_ext(&cpu->env, bit)) {
343             continue;
344         }
345 
346         if (!cpu_misa_ext_is_user_set(bit)) {
347             riscv_cpu_write_misa_bit(cpu, bit, true);
348             continue;
349         }
350 
351         if (send_warn) {
352             warn_report(warn_msg, riscv_get_misa_ext_name(bit));
353         }
354     }
355 
356     if (!cpu->cfg.ext_zicsr) {
357         if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zicsr))) {
358             cpu->cfg.ext_zicsr = true;
359         } else if (send_warn) {
360             warn_report(warn_msg, "zicsr");
361         }
362     }
363 
364     if (!cpu->cfg.ext_zifencei) {
365         if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zifencei))) {
366             cpu->cfg.ext_zifencei = true;
367         } else if (send_warn) {
368             warn_report(warn_msg, "zifencei");
369         }
370     }
371 }
372 
373 static void riscv_cpu_validate_b(RISCVCPU *cpu)
374 {
375     const char *warn_msg = "RVB mandates disabled extension %s";
376 
377     if (!cpu->cfg.ext_zba) {
378         if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zba))) {
379             cpu->cfg.ext_zba = true;
380         } else {
381             warn_report(warn_msg, "zba");
382         }
383     }
384 
385     if (!cpu->cfg.ext_zbb) {
386         if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zbb))) {
387             cpu->cfg.ext_zbb = true;
388         } else {
389             warn_report(warn_msg, "zbb");
390         }
391     }
392 
393     if (!cpu->cfg.ext_zbs) {
394         if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zbs))) {
395             cpu->cfg.ext_zbs = true;
396         } else {
397             warn_report(warn_msg, "zbs");
398         }
399     }
400 }
401 
402 /*
403  * Check consistency between chosen extensions while setting
404  * cpu->cfg accordingly.
405  */
406 void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)
407 {
408     RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu);
409     CPURISCVState *env = &cpu->env;
410     Error *local_err = NULL;
411 
412     if (riscv_has_ext(env, RVG)) {
413         riscv_cpu_validate_g(cpu);
414     }
415 
416     if (riscv_has_ext(env, RVB)) {
417         riscv_cpu_validate_b(cpu);
418     }
419 
420     if (riscv_has_ext(env, RVI) && riscv_has_ext(env, RVE)) {
421         error_setg(errp,
422                    "I and E extensions are incompatible");
423         return;
424     }
425 
426     if (!riscv_has_ext(env, RVI) && !riscv_has_ext(env, RVE)) {
427         error_setg(errp,
428                    "Either I or E extension must be set");
429         return;
430     }
431 
432     if (riscv_has_ext(env, RVS) && !riscv_has_ext(env, RVU)) {
433         error_setg(errp,
434                    "Setting S extension without U extension is illegal");
435         return;
436     }
437 
438     if (riscv_has_ext(env, RVH) && !riscv_has_ext(env, RVI)) {
439         error_setg(errp,
440                    "H depends on an I base integer ISA with 32 x registers");
441         return;
442     }
443 
444     if (riscv_has_ext(env, RVH) && !riscv_has_ext(env, RVS)) {
445         error_setg(errp, "H extension implicitly requires S-mode");
446         return;
447     }
448 
449     if (riscv_has_ext(env, RVF) && !cpu->cfg.ext_zicsr) {
450         error_setg(errp, "F extension requires Zicsr");
451         return;
452     }
453 
454     if ((cpu->cfg.ext_zacas) && !riscv_has_ext(env, RVA)) {
455         error_setg(errp, "Zacas extension requires A extension");
456         return;
457     }
458 
459     if ((cpu->cfg.ext_zawrs) && !riscv_has_ext(env, RVA)) {
460         error_setg(errp, "Zawrs extension requires A extension");
461         return;
462     }
463 
464     if (cpu->cfg.ext_zfa && !riscv_has_ext(env, RVF)) {
465         error_setg(errp, "Zfa extension requires F extension");
466         return;
467     }
468 
469     if (cpu->cfg.ext_zfh) {
470         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zfhmin), true);
471     }
472 
473     if (cpu->cfg.ext_zfhmin && !riscv_has_ext(env, RVF)) {
474         error_setg(errp, "Zfh/Zfhmin extensions require F extension");
475         return;
476     }
477 
478     if (cpu->cfg.ext_zfbfmin && !riscv_has_ext(env, RVF)) {
479         error_setg(errp, "Zfbfmin extension depends on F extension");
480         return;
481     }
482 
483     if (riscv_has_ext(env, RVD) && !riscv_has_ext(env, RVF)) {
484         error_setg(errp, "D extension requires F extension");
485         return;
486     }
487 
488     if (riscv_has_ext(env, RVV)) {
489         riscv_cpu_validate_v(env, &cpu->cfg, &local_err);
490         if (local_err != NULL) {
491             error_propagate(errp, local_err);
492             return;
493         }
494 
495         /* The V vector extension depends on the Zve64d extension */
496         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zve64d), true);
497     }
498 
499     /* The Zve64d extension depends on the Zve64f extension */
500     if (cpu->cfg.ext_zve64d) {
501         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zve64f), true);
502     }
503 
504     /* The Zve64f extension depends on the Zve32f extension */
505     if (cpu->cfg.ext_zve64f) {
506         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zve32f), true);
507     }
508 
509     if (cpu->cfg.ext_zve64d && !riscv_has_ext(env, RVD)) {
510         error_setg(errp, "Zve64d/V extensions require D extension");
511         return;
512     }
513 
514     if (cpu->cfg.ext_zve32f && !riscv_has_ext(env, RVF)) {
515         error_setg(errp, "Zve32f/Zve64f extensions require F extension");
516         return;
517     }
518 
519     if (cpu->cfg.ext_zvfh) {
520         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvfhmin), true);
521     }
522 
523     if (cpu->cfg.ext_zvfhmin && !cpu->cfg.ext_zve32f) {
524         error_setg(errp, "Zvfh/Zvfhmin extensions require Zve32f extension");
525         return;
526     }
527 
528     if (cpu->cfg.ext_zvfh && !cpu->cfg.ext_zfhmin) {
529         error_setg(errp, "Zvfh extensions requires Zfhmin extension");
530         return;
531     }
532 
533     if (cpu->cfg.ext_zvfbfmin && !cpu->cfg.ext_zfbfmin) {
534         error_setg(errp, "Zvfbfmin extension depends on Zfbfmin extension");
535         return;
536     }
537 
538     if (cpu->cfg.ext_zvfbfmin && !cpu->cfg.ext_zve32f) {
539         error_setg(errp, "Zvfbfmin extension depends on Zve32f extension");
540         return;
541     }
542 
543     if (cpu->cfg.ext_zvfbfwma && !cpu->cfg.ext_zvfbfmin) {
544         error_setg(errp, "Zvfbfwma extension depends on Zvfbfmin extension");
545         return;
546     }
547 
548     /* Set the ISA extensions, checks should have happened above */
549     if (cpu->cfg.ext_zhinx) {
550         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zca), true);
551     }
552 
553     if ((cpu->cfg.ext_zdinx || cpu->cfg.ext_zhinxmin) && !cpu->cfg.ext_zfinx) {
554         error_setg(errp, "Zdinx/Zhinx/Zhinxmin extensions require Zfinx");
555         return;
556     }
557 
558     if (cpu->cfg.ext_zfinx) {
559         if (!cpu->cfg.ext_zicsr) {
560             error_setg(errp, "Zfinx extension requires Zicsr");
561             return;
562         }
563         if (riscv_has_ext(env, RVF)) {
564             error_setg(errp,
565                        "Zfinx cannot be supported together with F extension");
566             return;
567         }
568     }
569 
570     if (cpu->cfg.ext_zce) {
571         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zca), true);
572         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcb), true);
573         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcmp), true);
574         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcmt), true);
575         if (riscv_has_ext(env, RVF) && mcc->misa_mxl_max == MXL_RV32) {
576             cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcf), true);
577         }
578     }
579 
580     /* zca, zcd and zcf has a PRIV 1.12.0 restriction */
581     if (riscv_has_ext(env, RVC) && env->priv_ver >= PRIV_VERSION_1_12_0) {
582         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zca), true);
583         if (riscv_has_ext(env, RVF) && mcc->misa_mxl_max == MXL_RV32) {
584             cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcf), true);
585         }
586         if (riscv_has_ext(env, RVD)) {
587             cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zcd), true);
588         }
589     }
590 
591     if (mcc->misa_mxl_max != MXL_RV32 && cpu->cfg.ext_zcf) {
592         error_setg(errp, "Zcf extension is only relevant to RV32");
593         return;
594     }
595 
596     if (!riscv_has_ext(env, RVF) && cpu->cfg.ext_zcf) {
597         error_setg(errp, "Zcf extension requires F extension");
598         return;
599     }
600 
601     if (!riscv_has_ext(env, RVD) && cpu->cfg.ext_zcd) {
602         error_setg(errp, "Zcd extension requires D extension");
603         return;
604     }
605 
606     if ((cpu->cfg.ext_zcf || cpu->cfg.ext_zcd || cpu->cfg.ext_zcb ||
607          cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt) && !cpu->cfg.ext_zca) {
608         error_setg(errp, "Zcf/Zcd/Zcb/Zcmp/Zcmt extensions require Zca "
609                          "extension");
610         return;
611     }
612 
613     if (cpu->cfg.ext_zcd && (cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt)) {
614         error_setg(errp, "Zcmp/Zcmt extensions are incompatible with "
615                          "Zcd extension");
616         return;
617     }
618 
619     if (cpu->cfg.ext_zcmt && !cpu->cfg.ext_zicsr) {
620         error_setg(errp, "Zcmt extension requires Zicsr extension");
621         return;
622     }
623 
624     /*
625      * Shorthand vector crypto extensions
626      */
627     if (cpu->cfg.ext_zvknc) {
628         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvkn), true);
629         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvbc), true);
630     }
631 
632     if (cpu->cfg.ext_zvkng) {
633         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvkn), true);
634         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvkg), true);
635     }
636 
637     if (cpu->cfg.ext_zvkn) {
638         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvkned), true);
639         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvknhb), true);
640         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvkb), true);
641         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvkt), true);
642     }
643 
644     if (cpu->cfg.ext_zvksc) {
645         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvks), true);
646         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvbc), true);
647     }
648 
649     if (cpu->cfg.ext_zvksg) {
650         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvks), true);
651         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvkg), true);
652     }
653 
654     if (cpu->cfg.ext_zvks) {
655         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvksed), true);
656         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvksh), true);
657         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvkb), true);
658         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvkt), true);
659     }
660 
661     if (cpu->cfg.ext_zvkt) {
662         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvbb), true);
663         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zvbc), true);
664     }
665 
666     /*
667      * In principle Zve*x would also suffice here, were they supported
668      * in qemu
669      */
670     if ((cpu->cfg.ext_zvbb || cpu->cfg.ext_zvkb || cpu->cfg.ext_zvkg ||
671          cpu->cfg.ext_zvkned || cpu->cfg.ext_zvknha || cpu->cfg.ext_zvksed ||
672          cpu->cfg.ext_zvksh) && !cpu->cfg.ext_zve32f) {
673         error_setg(errp,
674                    "Vector crypto extensions require V or Zve* extensions");
675         return;
676     }
677 
678     if ((cpu->cfg.ext_zvbc || cpu->cfg.ext_zvknhb) && !cpu->cfg.ext_zve64f) {
679         error_setg(
680             errp,
681             "Zvbc and Zvknhb extensions require V or Zve64{f,d} extensions");
682         return;
683     }
684 
685     if (cpu->cfg.ext_zk) {
686         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zkn), true);
687         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zkr), true);
688         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zkt), true);
689     }
690 
691     if (cpu->cfg.ext_zkn) {
692         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zbkb), true);
693         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zbkc), true);
694         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zbkx), true);
695         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zkne), true);
696         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zknd), true);
697         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zknh), true);
698     }
699 
700     if (cpu->cfg.ext_zks) {
701         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zbkb), true);
702         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zbkc), true);
703         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zbkx), true);
704         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zksed), true);
705         cpu_cfg_ext_auto_update(cpu, CPU_CFG_OFFSET(ext_zksh), true);
706     }
707 
708     if (cpu->cfg.ext_zicntr && !cpu->cfg.ext_zicsr) {
709         if (cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zicntr))) {
710             error_setg(errp, "zicntr requires zicsr");
711             return;
712         }
713         cpu->cfg.ext_zicntr = false;
714     }
715 
716     if (cpu->cfg.ext_zihpm && !cpu->cfg.ext_zicsr) {
717         if (cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zihpm))) {
718             error_setg(errp, "zihpm requires zicsr");
719             return;
720         }
721         cpu->cfg.ext_zihpm = false;
722     }
723 
724     if (!cpu->cfg.ext_zihpm) {
725         cpu->cfg.pmu_mask = 0;
726         cpu->pmu_avail_ctrs = 0;
727     }
728 
729     /*
730      * Disable isa extensions based on priv spec after we
731      * validated and set everything we need.
732      */
733     riscv_cpu_disable_priv_spec_isa_exts(cpu);
734 }
735 
736 #ifndef CONFIG_USER_ONLY
737 static bool riscv_cpu_validate_profile_satp(RISCVCPU *cpu,
738                                             RISCVCPUProfile *profile,
739                                             bool send_warn)
740 {
741     int satp_max = satp_mode_max_from_map(cpu->cfg.satp_mode.supported);
742 
743     if (profile->satp_mode > satp_max) {
744         if (send_warn) {
745             bool is_32bit = riscv_cpu_is_32bit(cpu);
746             const char *req_satp = satp_mode_str(profile->satp_mode, is_32bit);
747             const char *cur_satp = satp_mode_str(satp_max, is_32bit);
748 
749             warn_report("Profile %s requires satp mode %s, "
750                         "but satp mode %s was set", profile->name,
751                         req_satp, cur_satp);
752         }
753 
754         return false;
755     }
756 
757     return true;
758 }
759 #endif
760 
761 static void riscv_cpu_validate_profile(RISCVCPU *cpu,
762                                        RISCVCPUProfile *profile)
763 {
764     CPURISCVState *env = &cpu->env;
765     const char *warn_msg = "Profile %s mandates disabled extension %s";
766     bool send_warn = profile->user_set && profile->enabled;
767     bool parent_enabled, profile_impl = true;
768     int i;
769 
770 #ifndef CONFIG_USER_ONLY
771     if (profile->satp_mode != RISCV_PROFILE_ATTR_UNUSED) {
772         profile_impl = riscv_cpu_validate_profile_satp(cpu, profile,
773                                                        send_warn);
774     }
775 #endif
776 
777     if (profile->priv_spec != RISCV_PROFILE_ATTR_UNUSED &&
778         profile->priv_spec != env->priv_ver) {
779         profile_impl = false;
780 
781         if (send_warn) {
782             warn_report("Profile %s requires priv spec %s, "
783                         "but priv ver %s was set", profile->name,
784                         cpu_priv_ver_to_str(profile->priv_spec),
785                         cpu_priv_ver_to_str(env->priv_ver));
786         }
787     }
788 
789     for (i = 0; misa_bits[i] != 0; i++) {
790         uint32_t bit = misa_bits[i];
791 
792         if (!(profile->misa_ext & bit)) {
793             continue;
794         }
795 
796         if (!riscv_has_ext(&cpu->env, bit)) {
797             profile_impl = false;
798 
799             if (send_warn) {
800                 warn_report(warn_msg, profile->name,
801                             riscv_get_misa_ext_name(bit));
802             }
803         }
804     }
805 
806     for (i = 0; profile->ext_offsets[i] != RISCV_PROFILE_EXT_LIST_END; i++) {
807         int ext_offset = profile->ext_offsets[i];
808 
809         if (!isa_ext_is_enabled(cpu, ext_offset)) {
810             profile_impl = false;
811 
812             if (send_warn) {
813                 warn_report(warn_msg, profile->name,
814                             cpu_cfg_ext_get_name(ext_offset));
815             }
816         }
817     }
818 
819     profile->enabled = profile_impl;
820 
821     if (profile->parent != NULL) {
822         parent_enabled = object_property_get_bool(OBJECT(cpu),
823                                                   profile->parent->name,
824                                                   NULL);
825         profile->enabled = profile->enabled && parent_enabled;
826     }
827 }
828 
829 static void riscv_cpu_validate_profiles(RISCVCPU *cpu)
830 {
831     for (int i = 0; riscv_profiles[i] != NULL; i++) {
832         riscv_cpu_validate_profile(cpu, riscv_profiles[i]);
833     }
834 }
835 
836 void riscv_tcg_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
837 {
838     CPURISCVState *env = &cpu->env;
839     Error *local_err = NULL;
840 
841     riscv_cpu_validate_misa_priv(env, &local_err);
842     if (local_err != NULL) {
843         error_propagate(errp, local_err);
844         return;
845     }
846 
847     riscv_cpu_update_named_features(cpu);
848     riscv_cpu_validate_profiles(cpu);
849 
850     if (cpu->cfg.ext_smepmp && !cpu->cfg.pmp) {
851         /*
852          * Enhanced PMP should only be available
853          * on harts with PMP support
854          */
855         error_setg(errp, "Invalid configuration: Smepmp requires PMP support");
856         return;
857     }
858 
859     riscv_cpu_validate_set_extensions(cpu, &local_err);
860     if (local_err != NULL) {
861         error_propagate(errp, local_err);
862         return;
863     }
864 }
865 
866 bool riscv_cpu_tcg_compatible(RISCVCPU *cpu)
867 {
868     return object_dynamic_cast(OBJECT(cpu), TYPE_RISCV_CPU_HOST) == NULL;
869 }
870 
871 static bool riscv_cpu_is_generic(Object *cpu_obj)
872 {
873     return object_dynamic_cast(cpu_obj, TYPE_RISCV_DYNAMIC_CPU) != NULL;
874 }
875 
876 /*
877  * We'll get here via the following path:
878  *
879  * riscv_cpu_realize()
880  *   -> cpu_exec_realizefn()
881  *      -> tcg_cpu_realize() (via accel_cpu_common_realize())
882  */
883 static bool riscv_tcg_cpu_realize(CPUState *cs, Error **errp)
884 {
885     RISCVCPU *cpu = RISCV_CPU(cs);
886 
887     if (!riscv_cpu_tcg_compatible(cpu)) {
888         g_autofree char *name = riscv_cpu_get_name(cpu);
889         error_setg(errp, "'%s' CPU is not compatible with TCG acceleration",
890                    name);
891         return false;
892     }
893 
894 #ifndef CONFIG_USER_ONLY
895     CPURISCVState *env = &cpu->env;
896     Error *local_err = NULL;
897 
898     CPU(cs)->tcg_cflags |= CF_PCREL;
899 
900     if (cpu->cfg.ext_sstc) {
901         riscv_timer_init(cpu);
902     }
903 
904     if (cpu->cfg.pmu_mask) {
905         riscv_pmu_init(cpu, &local_err);
906         if (local_err != NULL) {
907             error_propagate(errp, local_err);
908             return false;
909         }
910 
911         if (cpu->cfg.ext_sscofpmf) {
912             cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
913                                           riscv_pmu_timer_cb, cpu);
914         }
915     }
916 
917     /* With H-Ext, VSSIP, VSTIP, VSEIP and SGEIP are hardwired to one. */
918     if (riscv_has_ext(env, RVH)) {
919         env->mideleg = MIP_VSSIP | MIP_VSTIP | MIP_VSEIP | MIP_SGEIP;
920     }
921 #endif
922 
923     return true;
924 }
925 
926 typedef struct RISCVCPUMisaExtConfig {
927     target_ulong misa_bit;
928     bool enabled;
929 } RISCVCPUMisaExtConfig;
930 
931 static void cpu_set_misa_ext_cfg(Object *obj, Visitor *v, const char *name,
932                                  void *opaque, Error **errp)
933 {
934     const RISCVCPUMisaExtConfig *misa_ext_cfg = opaque;
935     target_ulong misa_bit = misa_ext_cfg->misa_bit;
936     RISCVCPU *cpu = RISCV_CPU(obj);
937     CPURISCVState *env = &cpu->env;
938     bool vendor_cpu = riscv_cpu_is_vendor(obj);
939     bool prev_val, value;
940 
941     if (!visit_type_bool(v, name, &value, errp)) {
942         return;
943     }
944 
945     cpu_misa_ext_add_user_opt(misa_bit, value);
946 
947     prev_val = env->misa_ext & misa_bit;
948 
949     if (value == prev_val) {
950         return;
951     }
952 
953     if (value) {
954         if (vendor_cpu) {
955             g_autofree char *cpuname = riscv_cpu_get_name(cpu);
956             error_setg(errp, "'%s' CPU does not allow enabling extensions",
957                        cpuname);
958             return;
959         }
960 
961         if (misa_bit == RVH && env->priv_ver < PRIV_VERSION_1_12_0) {
962             /*
963              * Note: the 'priv_spec' command line option, if present,
964              * will take precedence over this priv_ver bump.
965              */
966             env->priv_ver = PRIV_VERSION_1_12_0;
967         }
968     }
969 
970     riscv_cpu_write_misa_bit(cpu, misa_bit, value);
971 }
972 
973 static void cpu_get_misa_ext_cfg(Object *obj, Visitor *v, const char *name,
974                                  void *opaque, Error **errp)
975 {
976     const RISCVCPUMisaExtConfig *misa_ext_cfg = opaque;
977     target_ulong misa_bit = misa_ext_cfg->misa_bit;
978     RISCVCPU *cpu = RISCV_CPU(obj);
979     CPURISCVState *env = &cpu->env;
980     bool value;
981 
982     value = env->misa_ext & misa_bit;
983 
984     visit_type_bool(v, name, &value, errp);
985 }
986 
987 #define MISA_CFG(_bit, _enabled) \
988     {.misa_bit = _bit, .enabled = _enabled}
989 
990 static const RISCVCPUMisaExtConfig misa_ext_cfgs[] = {
991     MISA_CFG(RVA, true),
992     MISA_CFG(RVC, true),
993     MISA_CFG(RVD, true),
994     MISA_CFG(RVF, true),
995     MISA_CFG(RVI, true),
996     MISA_CFG(RVE, false),
997     MISA_CFG(RVM, true),
998     MISA_CFG(RVS, true),
999     MISA_CFG(RVU, true),
1000     MISA_CFG(RVH, true),
1001     MISA_CFG(RVJ, false),
1002     MISA_CFG(RVV, false),
1003     MISA_CFG(RVG, false),
1004     MISA_CFG(RVB, false),
1005 };
1006 
1007 /*
1008  * We do not support user choice tracking for MISA
1009  * extensions yet because, so far, we do not silently
1010  * change MISA bits during realize() (RVG enables MISA
1011  * bits but the user is warned about it).
1012  */
1013 static void riscv_cpu_add_misa_properties(Object *cpu_obj)
1014 {
1015     bool use_def_vals = riscv_cpu_is_generic(cpu_obj);
1016     int i;
1017 
1018     for (i = 0; i < ARRAY_SIZE(misa_ext_cfgs); i++) {
1019         const RISCVCPUMisaExtConfig *misa_cfg = &misa_ext_cfgs[i];
1020         int bit = misa_cfg->misa_bit;
1021         const char *name = riscv_get_misa_ext_name(bit);
1022         const char *desc = riscv_get_misa_ext_description(bit);
1023 
1024         /* Check if KVM already created the property */
1025         if (object_property_find(cpu_obj, name)) {
1026             continue;
1027         }
1028 
1029         object_property_add(cpu_obj, name, "bool",
1030                             cpu_get_misa_ext_cfg,
1031                             cpu_set_misa_ext_cfg,
1032                             NULL, (void *)misa_cfg);
1033         object_property_set_description(cpu_obj, name, desc);
1034         if (use_def_vals) {
1035             riscv_cpu_write_misa_bit(RISCV_CPU(cpu_obj), bit,
1036                                      misa_cfg->enabled);
1037         }
1038     }
1039 }
1040 
1041 static void cpu_set_profile(Object *obj, Visitor *v, const char *name,
1042                             void *opaque, Error **errp)
1043 {
1044     RISCVCPUProfile *profile = opaque;
1045     RISCVCPU *cpu = RISCV_CPU(obj);
1046     bool value;
1047     int i, ext_offset;
1048 
1049     if (riscv_cpu_is_vendor(obj)) {
1050         error_setg(errp, "Profile %s is not available for vendor CPUs",
1051                    profile->name);
1052         return;
1053     }
1054 
1055     if (cpu->env.misa_mxl != MXL_RV64) {
1056         error_setg(errp, "Profile %s only available for 64 bit CPUs",
1057                    profile->name);
1058         return;
1059     }
1060 
1061     if (!visit_type_bool(v, name, &value, errp)) {
1062         return;
1063     }
1064 
1065     profile->user_set = true;
1066     profile->enabled = value;
1067 
1068     if (profile->parent != NULL) {
1069         object_property_set_bool(obj, profile->parent->name,
1070                                  profile->enabled, NULL);
1071     }
1072 
1073     if (profile->enabled) {
1074         cpu->env.priv_ver = profile->priv_spec;
1075     }
1076 
1077 #ifndef CONFIG_USER_ONLY
1078     if (profile->satp_mode != RISCV_PROFILE_ATTR_UNUSED) {
1079         object_property_set_bool(obj, "mmu", true, NULL);
1080         const char *satp_prop = satp_mode_str(profile->satp_mode,
1081                                               riscv_cpu_is_32bit(cpu));
1082         object_property_set_bool(obj, satp_prop, profile->enabled, NULL);
1083     }
1084 #endif
1085 
1086     for (i = 0; misa_bits[i] != 0; i++) {
1087         uint32_t bit = misa_bits[i];
1088 
1089         if  (!(profile->misa_ext & bit)) {
1090             continue;
1091         }
1092 
1093         if (bit == RVI && !profile->enabled) {
1094             /*
1095              * Disabling profiles will not disable the base
1096              * ISA RV64I.
1097              */
1098             continue;
1099         }
1100 
1101         cpu_misa_ext_add_user_opt(bit, profile->enabled);
1102         riscv_cpu_write_misa_bit(cpu, bit, profile->enabled);
1103     }
1104 
1105     for (i = 0; profile->ext_offsets[i] != RISCV_PROFILE_EXT_LIST_END; i++) {
1106         ext_offset = profile->ext_offsets[i];
1107 
1108         if (profile->enabled) {
1109             if (cpu_cfg_offset_is_named_feat(ext_offset)) {
1110                 riscv_cpu_enable_named_feat(cpu, ext_offset);
1111             }
1112 
1113             cpu_bump_multi_ext_priv_ver(&cpu->env, ext_offset);
1114         }
1115 
1116         cpu_cfg_ext_add_user_opt(ext_offset, profile->enabled);
1117         isa_ext_update_enabled(cpu, ext_offset, profile->enabled);
1118     }
1119 }
1120 
1121 static void cpu_get_profile(Object *obj, Visitor *v, const char *name,
1122                             void *opaque, Error **errp)
1123 {
1124     RISCVCPUProfile *profile = opaque;
1125     bool value = profile->enabled;
1126 
1127     visit_type_bool(v, name, &value, errp);
1128 }
1129 
1130 static void riscv_cpu_add_profiles(Object *cpu_obj)
1131 {
1132     for (int i = 0; riscv_profiles[i] != NULL; i++) {
1133         const RISCVCPUProfile *profile = riscv_profiles[i];
1134 
1135         object_property_add(cpu_obj, profile->name, "bool",
1136                             cpu_get_profile, cpu_set_profile,
1137                             NULL, (void *)profile);
1138 
1139         /*
1140          * CPUs might enable a profile right from the start.
1141          * Enable its mandatory extensions right away in this
1142          * case.
1143          */
1144         if (profile->enabled) {
1145             object_property_set_bool(cpu_obj, profile->name, true, NULL);
1146         }
1147     }
1148 }
1149 
1150 static bool cpu_ext_is_deprecated(const char *ext_name)
1151 {
1152     return isupper(ext_name[0]);
1153 }
1154 
1155 /*
1156  * String will be allocated in the heap. Caller is responsible
1157  * for freeing it.
1158  */
1159 static char *cpu_ext_to_lower(const char *ext_name)
1160 {
1161     char *ret = g_malloc0(strlen(ext_name) + 1);
1162 
1163     strcpy(ret, ext_name);
1164     ret[0] = tolower(ret[0]);
1165 
1166     return ret;
1167 }
1168 
1169 static void cpu_set_multi_ext_cfg(Object *obj, Visitor *v, const char *name,
1170                                   void *opaque, Error **errp)
1171 {
1172     const RISCVCPUMultiExtConfig *multi_ext_cfg = opaque;
1173     RISCVCPU *cpu = RISCV_CPU(obj);
1174     bool vendor_cpu = riscv_cpu_is_vendor(obj);
1175     bool prev_val, value;
1176 
1177     if (!visit_type_bool(v, name, &value, errp)) {
1178         return;
1179     }
1180 
1181     if (cpu_ext_is_deprecated(multi_ext_cfg->name)) {
1182         g_autofree char *lower = cpu_ext_to_lower(multi_ext_cfg->name);
1183 
1184         warn_report("CPU property '%s' is deprecated. Please use '%s' instead",
1185                     multi_ext_cfg->name, lower);
1186     }
1187 
1188     cpu_cfg_ext_add_user_opt(multi_ext_cfg->offset, value);
1189 
1190     prev_val = isa_ext_is_enabled(cpu, multi_ext_cfg->offset);
1191 
1192     if (value == prev_val) {
1193         return;
1194     }
1195 
1196     if (value && vendor_cpu) {
1197         g_autofree char *cpuname = riscv_cpu_get_name(cpu);
1198         error_setg(errp, "'%s' CPU does not allow enabling extensions",
1199                    cpuname);
1200         return;
1201     }
1202 
1203     if (value) {
1204         cpu_bump_multi_ext_priv_ver(&cpu->env, multi_ext_cfg->offset);
1205     }
1206 
1207     isa_ext_update_enabled(cpu, multi_ext_cfg->offset, value);
1208 }
1209 
1210 static void cpu_get_multi_ext_cfg(Object *obj, Visitor *v, const char *name,
1211                                   void *opaque, Error **errp)
1212 {
1213     const RISCVCPUMultiExtConfig *multi_ext_cfg = opaque;
1214     bool value = isa_ext_is_enabled(RISCV_CPU(obj), multi_ext_cfg->offset);
1215 
1216     visit_type_bool(v, name, &value, errp);
1217 }
1218 
1219 static void cpu_add_multi_ext_prop(Object *cpu_obj,
1220                                    const RISCVCPUMultiExtConfig *multi_cfg)
1221 {
1222     bool generic_cpu = riscv_cpu_is_generic(cpu_obj);
1223     bool deprecated_ext = cpu_ext_is_deprecated(multi_cfg->name);
1224 
1225     object_property_add(cpu_obj, multi_cfg->name, "bool",
1226                         cpu_get_multi_ext_cfg,
1227                         cpu_set_multi_ext_cfg,
1228                         NULL, (void *)multi_cfg);
1229 
1230     if (!generic_cpu || deprecated_ext) {
1231         return;
1232     }
1233 
1234     /*
1235      * Set def val directly instead of using
1236      * object_property_set_bool() to save the set()
1237      * callback hash for user inputs.
1238      */
1239     isa_ext_update_enabled(RISCV_CPU(cpu_obj), multi_cfg->offset,
1240                            multi_cfg->enabled);
1241 }
1242 
1243 static void riscv_cpu_add_multiext_prop_array(Object *obj,
1244                                         const RISCVCPUMultiExtConfig *array)
1245 {
1246     const RISCVCPUMultiExtConfig *prop;
1247 
1248     g_assert(array);
1249 
1250     for (prop = array; prop && prop->name; prop++) {
1251         cpu_add_multi_ext_prop(obj, prop);
1252     }
1253 }
1254 
1255 /*
1256  * Add CPU properties with user-facing flags.
1257  *
1258  * This will overwrite existing env->misa_ext values with the
1259  * defaults set via riscv_cpu_add_misa_properties().
1260  */
1261 static void riscv_cpu_add_user_properties(Object *obj)
1262 {
1263 #ifndef CONFIG_USER_ONLY
1264     riscv_add_satp_mode_properties(obj);
1265 #endif
1266 
1267     riscv_cpu_add_misa_properties(obj);
1268 
1269     riscv_cpu_add_multiext_prop_array(obj, riscv_cpu_extensions);
1270     riscv_cpu_add_multiext_prop_array(obj, riscv_cpu_vendor_exts);
1271     riscv_cpu_add_multiext_prop_array(obj, riscv_cpu_experimental_exts);
1272 
1273     riscv_cpu_add_multiext_prop_array(obj, riscv_cpu_deprecated_exts);
1274 
1275     riscv_cpu_add_profiles(obj);
1276 }
1277 
1278 /*
1279  * The 'max' type CPU will have all possible ratified
1280  * non-vendor extensions enabled.
1281  */
1282 static void riscv_init_max_cpu_extensions(Object *obj)
1283 {
1284     RISCVCPU *cpu = RISCV_CPU(obj);
1285     CPURISCVState *env = &cpu->env;
1286     const RISCVCPUMultiExtConfig *prop;
1287 
1288     /* Enable RVG, RVJ and RVV that are disabled by default */
1289     riscv_cpu_set_misa_ext(env, env->misa_ext | RVG | RVJ | RVV);
1290 
1291     for (prop = riscv_cpu_extensions; prop && prop->name; prop++) {
1292         isa_ext_update_enabled(cpu, prop->offset, true);
1293     }
1294 
1295     /*
1296      * Some extensions can't be added without backward compatibilty concerns.
1297      * Disable those, the user can still opt in to them on the command line.
1298      */
1299     cpu->cfg.ext_svade = false;
1300 
1301     /* set vector version */
1302     env->vext_ver = VEXT_VERSION_1_00_0;
1303 
1304     /* Zfinx is not compatible with F. Disable it */
1305     isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zfinx), false);
1306     isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zdinx), false);
1307     isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zhinx), false);
1308     isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zhinxmin), false);
1309 
1310     isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zce), false);
1311     isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zcmp), false);
1312     isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zcmt), false);
1313 
1314     if (env->misa_mxl != MXL_RV32) {
1315         isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zcf), false);
1316     }
1317 }
1318 
1319 static bool riscv_cpu_has_max_extensions(Object *cpu_obj)
1320 {
1321     return object_dynamic_cast(cpu_obj, TYPE_RISCV_CPU_MAX) != NULL;
1322 }
1323 
1324 static void riscv_tcg_cpu_instance_init(CPUState *cs)
1325 {
1326     RISCVCPU *cpu = RISCV_CPU(cs);
1327     Object *obj = OBJECT(cpu);
1328 
1329     misa_ext_user_opts = g_hash_table_new(NULL, g_direct_equal);
1330     multi_ext_user_opts = g_hash_table_new(NULL, g_direct_equal);
1331     riscv_cpu_add_user_properties(obj);
1332 
1333     if (riscv_cpu_has_max_extensions(obj)) {
1334         riscv_init_max_cpu_extensions(obj);
1335     }
1336 }
1337 
1338 static void riscv_tcg_cpu_init_ops(AccelCPUClass *accel_cpu, CPUClass *cc)
1339 {
1340     /*
1341      * All cpus use the same set of operations.
1342      */
1343     cc->tcg_ops = &riscv_tcg_ops;
1344 }
1345 
1346 static void riscv_tcg_cpu_class_init(CPUClass *cc)
1347 {
1348     cc->init_accel_cpu = riscv_tcg_cpu_init_ops;
1349 }
1350 
1351 static void riscv_tcg_cpu_accel_class_init(ObjectClass *oc, void *data)
1352 {
1353     AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);
1354 
1355     acc->cpu_class_init = riscv_tcg_cpu_class_init;
1356     acc->cpu_instance_init = riscv_tcg_cpu_instance_init;
1357     acc->cpu_target_realize = riscv_tcg_cpu_realize;
1358 }
1359 
1360 static const TypeInfo riscv_tcg_cpu_accel_type_info = {
1361     .name = ACCEL_CPU_NAME("tcg"),
1362 
1363     .parent = TYPE_ACCEL_CPU,
1364     .class_init = riscv_tcg_cpu_accel_class_init,
1365     .abstract = true,
1366 };
1367 
1368 static void riscv_tcg_cpu_accel_register_types(void)
1369 {
1370     type_register_static(&riscv_tcg_cpu_accel_type_info);
1371 }
1372 type_init(riscv_tcg_cpu_accel_register_types);
1373