target/riscv: Fix no-TCG build
Subsystems (2)
v3 cover target/riscv: Fix no-TCG build
This series fixes the riscv64-softmmu --disable-tcg build by keeping TCG-only code out of no-TCG builds while preserving common RISC-V CPU helpers for non-TCG configurations.
Compared with v2, this version replaces the no-TCG stub for riscv_cpu_validate_set_extensions() with a common cpu-validate.c split. The validator is used by common CSR code and should not live in TCG-only code.
Changes in v3:
- Replace the no-TCG stub with a new cpu-validate.c common source file.
- Move common RISC-V extension validation and user-option tracking out of target/riscv/tcg/tcg-cpu.c.
- Move fflags and exception helpers out of TCG-only helper files.
- Reject x-misa-w outside TCG.
- Build RISC-V TCG-only sources only when CONFIG_TCG is enabled.
- Add GitLab CI coverage for riscv64 KVM-only --disable-tcg builds.
Testing:
- ../configure --target-list=riscv64-softmmu --enable-kvm --disable-tcg \
--enable-debug
ninja
- ../configure --target-list=riscv64-softmmu,riscv64-linux-user \
--enable-debug
ninja
- In the no-TCG build:
qemu-system-riscv64 -M virt,accel=qtest -cpu rv64,x-misa-w=true \
-nographic -S
reports "x-misa-w requires TCG".
- In the TCG build:
qemu-system-riscv64 -M virt -accel tcg -cpu rv64,x-misa-w=true \
-nographic -S
starts without reporting "x-misa-w requires TCG".
Zephyr Li (6):
target/riscv: move extension validation out of TCG-only code target/riscv: move fflags helpers to common code target/riscv: move riscv_raise_exception() out of op_helper.c target/riscv: reject x-misa-w outside TCG target/riscv: build TCG-only sources only with TCG gitlab-ci.d/crossbuilds: add riscv64 KVM-only build job
.gitlab-ci.d/crossbuilds.yml | 8 + target/riscv/cpu-validate.c | 630 ++++++++++++++++++++++++++++++++++ target/riscv/cpu.c | 13 +- target/riscv/cpu.h | 7 - target/riscv/cpu_helper.c | 53 +++ target/riscv/csr.c | 12 +- target/riscv/debug.c | 1 + target/riscv/fpu_helper.c | 27 -- target/riscv/internals.h | 26 ++ target/riscv/meson.build | 10 +- target/riscv/op_helper.c | 15 - target/riscv/tcg/tcg-cpu.c | 637 ++--------------------------------- target/riscv/tcg/tcg-cpu.h | 1 - target/riscv/zce_helper.c | 1 + 14 files changed, 769 insertions(+), 672 deletions(-) create mode 100644 target/riscv/cpu-validate.c
base-commit: 5611a9268dae7b7ff99d478ed134052a9fc7e9f7
Signature · 1 line
2.43.0
PATCH v3 1/6 target/riscv: move extension validation out of TCG-only code
riscv_cpu_validate_set_extensions() contains architectural ISA extension dependency checks and is also used by common CSR code when misa is written. However, it currently lives in target/riscv/tcg/tcg-cpu.c, which is not built when TCG is disabled.
Move the extension validation code and its user-option tracking helpers to a new cpu-validate.c file. Keep TCG-specific finalize and TB state handling in tcg-cpu.c, and expose the validator through internals.h for target/riscv internal users.
Keep riscv_cpu_validate_misa_priv() and riscv_cpu_update_cfg() as separate internal helpers so TCG feature finalization preserves its original order and CSR write_misa() continues to run only extension consistency checks.
The move also gives the helpers used by tcg-cpu.c a riscv_cpu_ prefix and adds lazy initialization/NULL guards for the user-option hash tables, so the validator can be linked from non-TCG builds without relying on TCG CPU instance initialization.
--- target/riscv/cpu-validate.c | 630 +++++++++++++++++++++++++++++++++++ target/riscv/csr.c | 1 - target/riscv/internals.h | 19 ++ target/riscv/meson.build | 1 + target/riscv/tcg/tcg-cpu.c | 637 ++---------------------------------- target/riscv/tcg/tcg-cpu.h | 1 - 6 files changed, 672 insertions(+), 617 deletions(-) create mode 100644 target/riscv/cpu-validate.c
target/riscv/cpu-validate.c new file +630 -0
@@ -0,0 +1,630 @@ +/* +* RISC-V CPU extension validation +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "cpu.h" +#include "internals.h" +#include "qapi/error.h" +#include "qemu/error-report.h" + +/* Hash that stores user set extensions */ +static GHashTable *multi_ext_user_opts; +static GHashTable *misa_ext_user_opts; + +void riscv_cpu_ext_user_opts_init (void ) +{ +g_clear_pointer (&multi_ext_user_opts, g_hash_table_unref); +g_clear_pointer (&misa_ext_user_opts, g_hash_table_unref); + + multi_ext_user_opts =g_hash_table_new (NULL , g_direct_equal); + misa_ext_user_opts =g_hash_table_new (NULL , g_direct_equal); +} + +static bool cpu_cfg_ext_is_user_set (uint32_t ext_offset) +{ +if (!multi_ext_user_opts) { +return false; + } + +return g_hash_table_contains (multi_ext_user_opts, +GUINT_TO_POINTER (ext_offset)); +} + +bool riscv_cpu_misa_ext_is_user_set (uint32_t misa_bit) +{ +if (!misa_ext_user_opts) { +return false; + } + +return g_hash_table_contains (misa_ext_user_opts, +GUINT_TO_POINTER (misa_bit)); +} + +void riscv_cpu_cfg_ext_add_user_opt (uint32_t ext_offset,bool value) +{ +if (!multi_ext_user_opts) { +riscv_cpu_ext_user_opts_init (); + } + +g_hash_table_insert (multi_ext_user_opts, +GUINT_TO_POINTER (ext_offset), + (gpointer )value); +} + +void riscv_cpu_misa_ext_add_user_opt (uint32_t bit,bool value) +{ +if (!misa_ext_user_opts) { +riscv_cpu_ext_user_opts_init (); + } + +g_hash_table_insert (misa_ext_user_opts, +GUINT_TO_POINTER (bit), + (gpointer )value); +} + +void riscv_cpu_write_misa_bit (RISCVCPU *cpu,uint32_t bit, +bool enabled) +{ +CPURISCVState *env = &cpu->env; + +if (enabled) { + env->misa_ext |= bit; + env->misa_ext_mask |= bit; + }else { + env->misa_ext &= ~bit; + env->misa_ext_mask &= ~bit; + } +} + +static int cpu_cfg_ext_get_min_version (uint32_t ext_offset) +{ +const RISCVIsaExtData *edata; + +for (edata = isa_edata_arr; edata && edata->name; edata++) { +if (edata->ext_enable_offset != ext_offset) { +continue ; + } + +return edata->min_version; + } + +g_assert_not_reached (); +} + +const char *riscv_cpu_cfg_ext_get_name (uint32_t ext_offset) +{ +const RISCVIsaExtData *edata; + +for (edata = isa_edata_arr; edata->name !=NULL ; edata++) { +if (edata->ext_enable_offset == ext_offset) { +return edata->name; + } + } + +g_assert_not_reached (); +} + +void riscv_cpu_bump_multi_ext_priv_ver (CPURISCVState *env, +uint32_t ext_offset) +{ +int ext_priv_ver; + +if (env->priv_ver ==PRIV_VERSION_LATEST ) { +return ; + } + + ext_priv_ver =cpu_cfg_ext_get_min_version (ext_offset); + +if (env->priv_ver < ext_priv_ver) { +/* +* Note: the 'priv_spec' command line option, if present, +* will take precedence over this priv_ver bump. +*/ + env->priv_ver = ext_priv_ver; + } +} + +void riscv_cpu_cfg_ext_auto_update (RISCVCPU *cpu,uint32_t ext_offset, +bool value) +{ +CPURISCVState *env = &cpu->env; +bool prev_val =isa_ext_is_enabled (cpu, ext_offset); +int min_version; + +if (prev_val == value) { +return ; + } + +if (cpu_cfg_ext_is_user_set (ext_offset)) { +return ; + } + +if (value && env->priv_ver !=PRIV_VERSION_LATEST ) { +/* Do not enable it if priv_ver is older than min_version */ + min_version =cpu_cfg_ext_get_min_version (ext_offset); +if (env->priv_ver < min_version) { +return ; + } + } + +isa_ext_update_enabled (cpu, ext_offset, value); +} + +void riscv_cpu_validate_misa_priv (CPURISCVState *env,Error **errp) +{ +if (riscv_has_ext (env,RVH ) && env->priv_ver <PRIV_VERSION_1_12_0 ) { +error_setg (errp,"H extension requires priv spec 1.12.0" ); +return ; + } +} + +static void riscv_cpu_validate_v (CPURISCVState *env,RISCVCPUConfig *cfg, +Error **errp) +{ +uint32_t min_vlen; +uint32_t vlen = cfg->vlenb <<3 ; + +if (riscv_has_ext (env,RVV )) { + min_vlen =128 ; + }else if (cfg->ext_zve64x) { + min_vlen =64 ; + }else if (cfg->ext_zve32x) { + min_vlen =32 ; + }else { +return ; + } + +if (vlen >RV_VLEN_MAX || vlen < min_vlen) { +error_setg (errp, +"Vector extension implementation only supports VLEN " +"in the range [%d, %d]" , min_vlen,RV_VLEN_MAX ); +return ; + } + +if (cfg->elen >64 || cfg->elen <8 ) { +error_setg (errp, +"Vector extension implementation only supports ELEN " +"in the range [8, 64]" ); +return ; + } + +if (vlen < cfg->elen) { +error_setg (errp,"Vector extension implementation requires VLEN " +"to be greater than or equal to ELEN" ); +return ; + } +} + +static void riscv_cpu_disable_priv_spec_isa_exts (RISCVCPU *cpu) +{ +CPURISCVState *env = &cpu->env; +const RISCVIsaExtData *edata; + +/* Force disable extensions if priv spec version does not match */ +for (edata = isa_edata_arr; edata && edata->name; edata++) { +if (isa_ext_is_enabled (cpu, edata->ext_enable_offset) && + (env->priv_ver < edata->min_version)) { +/* +* These two extensions are always enabled as they were supported +* by QEMU before they were added as extensions in the ISA. +*/ +if (!strcmp (edata->name,"zicntr" ) || + !strcmp (edata->name,"zihpm" )) { +continue ; + } + +/* +* cpu.debug = true is marked as 'sdtrig', priv spec 1.12. +* Skip this warning since existing CPUs with older priv +* spec and debug = true will be impacted. +*/ +if (!strcmp (edata->name,"sdtrig" )) { +continue ; + } + +isa_ext_update_enabled (cpu, edata->ext_enable_offset, false); +#ifndef CONFIG_USER_ONLY +warn_report ("disabling %s extension for hart 0x%" PRIx64 +" because privilege spec version does not match" , + edata->name, env->mhartid); +#else +warn_report ("disabling %s extension because " +"privilege spec version does not match" , + edata->name); +#endif + } + } +} + +void riscv_cpu_update_cfg (RISCVCPU *cpu) +{ +if (cpu->env.priv_ver >=PRIV_VERSION_1_11_0 ) { + cpu->cfg.has_priv_1_11 = true; + } + +if (cpu->env.priv_ver >=PRIV_VERSION_1_12_0 ) { + cpu->cfg.has_priv_1_12 = true; + } + +if (cpu->env.priv_ver >=PRIV_VERSION_1_13_0 ) { + cpu->cfg.has_priv_1_13 = true; + } + +/* zic64b is 1.12 or later */ + cpu->cfg.ext_zic64b = cpu->cfg.cbom_blocksize ==64 && + cpu->cfg.cbop_blocksize ==64 && + cpu->cfg.cboz_blocksize ==64 && + cpu->cfg.has_priv_1_12; + + cpu->cfg.ext_ssstateen = cpu->cfg.ext_smstateen; + + cpu->cfg.ext_sha =riscv_has_ext (&cpu->env,RVH ) && + cpu->cfg.ext_ssstateen; + + cpu->cfg.ext_ziccrse = cpu->cfg.has_priv_1_11; +} + +static void riscv_cpu_validate_g (RISCVCPU *cpu) +{ +const char *warn_msg ="RVG mandates disabled extension %s" ; +uint32_t g_misa_bits[] = {RVI ,RVM ,RVA ,RVF ,RVD }; +bool send_warn =riscv_cpu_misa_ext_is_user_set (RVG ); + +for (int i =0 ; i <ARRAY_SIZE (g_misa_bits); i++) { +uint32_t bit = g_misa_bits[i]; + +if (riscv_has_ext (&cpu->env, bit)) { +continue ; + } + +if (!riscv_cpu_misa_ext_is_user_set (bit)) { +riscv_cpu_write_misa_bit (cpu, bit, true); +continue ; + } + +if (send_warn) { +warn_report (warn_msg,riscv_get_misa_ext_name (bit)); + } + } + +if (!cpu->cfg.ext_zicsr) { +if (!cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zicsr))) { + cpu->cfg.ext_zicsr = true; + }else if (send_warn) { +warn_report (warn_msg,"zicsr" ); + } + } + +if (!cpu->cfg.ext_zifencei) { +if (!cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zifencei))) { + cpu->cfg.ext_zifencei = true; + }else if (send_warn) { +warn_report (warn_msg,"zifencei" ); + } + } +} + +static void riscv_cpu_validate_b (RISCVCPU *cpu) +{ +const char *warn_msg ="RVB mandates disabled extension %s" ; + +if (!cpu->cfg.ext_zba) { +if (!cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zba))) { + cpu->cfg.ext_zba = true; + }else { +warn_report (warn_msg,"zba" ); + } + } + +if (!cpu->cfg.ext_zbb) { +if (!cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zbb))) { + cpu->cfg.ext_zbb = true; + }else { +warn_report (warn_msg,"zbb" ); + } + } + +if (!cpu->cfg.ext_zbs) { +if (!cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zbs))) { + cpu->cfg.ext_zbs = true; + }else { +warn_report (warn_msg,"zbs" ); + } + } +} + +/* +* Check consistency between chosen extensions while setting +* cpu->cfg accordingly. +*/ +void riscv_cpu_validate_set_extensions (RISCVCPU *cpu,Error **errp) +{ +RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (cpu); +CPURISCVState *env = &cpu->env; +Error *local_err =NULL ; + +if (riscv_has_ext (env,RVG )) { +riscv_cpu_validate_g (cpu); + } + +if (riscv_has_ext (env,RVB )) { +riscv_cpu_validate_b (cpu); + } + +if (riscv_has_ext (env,RVI ) &&riscv_has_ext (env,RVE )) { +error_setg (errp, +"I and E extensions are incompatible" ); +return ; + } + +if (!riscv_has_ext (env,RVI ) && !riscv_has_ext (env,RVE )) { +error_setg (errp, +"Either I or E extension must be set" ); +return ; + } + +if (riscv_has_ext (env,RVS ) && !riscv_has_ext (env,RVU )) { +error_setg (errp, +"Setting S extension without U extension is illegal" ); +return ; + } + +if (riscv_has_ext (env,RVH ) && !riscv_has_ext (env,RVI )) { +error_setg (errp, +"H depends on an I base integer ISA with 32 x registers" ); +return ; + } + +if (riscv_has_ext (env,RVH ) && !riscv_has_ext (env,RVS )) { +error_setg (errp,"H extension implicitly requires S-mode" ); +return ; + } + +if (riscv_has_ext (env,RVF ) && !cpu->cfg.ext_zicsr) { +error_setg (errp,"F extension requires Zicsr" ); +return ; + } + +if ((cpu->cfg.ext_zacas) && !riscv_has_ext (env,RVA )) { +error_setg (errp,"Zacas extension requires A extension" ); +return ; + } + +if ((cpu->cfg.ext_zawrs) && !riscv_has_ext (env,RVA )) { +error_setg (errp,"Zawrs extension requires A extension" ); +return ; + } + +if (cpu->cfg.ext_zfa && !riscv_has_ext (env,RVF )) { +error_setg (errp,"Zfa extension requires F extension" ); +return ; + } + +if (cpu->cfg.ext_zfhmin && !riscv_has_ext (env,RVF )) { +error_setg (errp,"Zfh/Zfhmin extensions require F extension" ); +return ; + } + +if (cpu->cfg.ext_zfbfmin && !riscv_has_ext (env,RVF )) { +error_setg (errp,"Zfbfmin extension depends on F extension" ); +return ; + } + +if (riscv_has_ext (env,RVD ) && !riscv_has_ext (env,RVF )) { +error_setg (errp,"D extension requires F extension" ); +return ; + } + +riscv_cpu_validate_v (env, &cpu->cfg, &local_err); +if (local_err !=NULL ) { +error_propagate (errp, local_err); +return ; + } + +/* The Zve64d extension depends on the Zve64f extension */ +if (cpu->cfg.ext_zve64d) { +if (!riscv_has_ext (env,RVD )) { +error_setg (errp,"Zve64d/V extensions require D extension" ); +return ; + } + } + +/* The Zve32f extension depends on the Zve32x extension */ +if (cpu->cfg.ext_zve32f) { +if (!riscv_has_ext (env,RVF )) { +error_setg (errp,"Zve32f/Zve64f extensions require F extension" ); +return ; + } + } + +if (cpu->cfg.ext_zvfhmin && !cpu->cfg.ext_zve32f) { +error_setg (errp,"Zvfh/Zvfhmin extensions require Zve32f extension" ); +return ; + } + +if (cpu->cfg.ext_zvfh && !cpu->cfg.ext_zfhmin) { +error_setg (errp,"Zvfh extensions requires Zfhmin extension" ); +return ; + } + +if (cpu->cfg.ext_zvfbfmin && !cpu->cfg.ext_zve32f) { +error_setg (errp,"Zvfbfmin extension depends on Zve32f extension" ); +return ; + } + +if (cpu->cfg.ext_zvfbfwma && !cpu->cfg.ext_zvfbfmin) { +error_setg (errp,"Zvfbfwma extension depends on Zvfbfmin extension" ); +return ; + } + +if (cpu->cfg.ext_zvfbfa) { +if (!cpu->cfg.ext_zve32f || !cpu->cfg.ext_zfbfmin) { +error_setg (errp,"Zvfbfa extension requires Zve32f extension " +"and Zfbfmin extension" ); +return ; + } + } + +if ((cpu->cfg.ext_zdinx || cpu->cfg.ext_zhinxmin) && !cpu->cfg.ext_zfinx) { +error_setg (errp,"Zdinx/Zhinx/Zhinxmin extensions require Zfinx" ); +return ; + } + +if (cpu->cfg.ext_zfinx) { +if (!cpu->cfg.ext_zicsr) { +error_setg (errp,"Zfinx extension requires Zicsr" ); +return ; + } +if (riscv_has_ext (env,RVF )) { +error_setg (errp, +"Zfinx cannot be supported together with F extension" ); +return ; + } + } + +if (cpu->cfg.ext_zcmop && !cpu->cfg.ext_zca) { +error_setg (errp,"Zcmop extensions require Zca" ); +return ; + } + +if (mcc->def->misa_mxl_max !=MXL_RV32 && cpu->cfg.ext_zcf) { +error_setg (errp,"Zcf extension is only relevant to RV32" ); +return ; + } + +if (!riscv_has_ext (env,RVF ) && cpu->cfg.ext_zcf) { +error_setg (errp,"Zcf extension requires F extension" ); +return ; + } + +if (!riscv_has_ext (env,RVD ) && cpu->cfg.ext_zcd) { +error_setg (errp,"Zcd extension requires D extension" ); +return ; + } + +if ((cpu->cfg.ext_zcf || cpu->cfg.ext_zcd || cpu->cfg.ext_zcb || + cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt) && !cpu->cfg.ext_zca) { +error_setg (errp,"Zcf/Zcd/Zcb/Zcmp/Zcmt extensions require Zca " +"extension" ); +return ; + } + +if (cpu->cfg.ext_zcd && (cpu->cfg.ext_zcmp || cpu->cfg.ext_zcmt)) { +error_setg (errp,"Zcmp/Zcmt extensions are incompatible with " +"Zcd extension" ); +return ; + } + +if (cpu->cfg.ext_zcmt && !cpu->cfg.ext_zicsr) { +error_setg (errp,"Zcmt extension requires Zicsr extension" ); +return ; + } + +if ((cpu->cfg.ext_zvbb || cpu->cfg.ext_zvkb || cpu->cfg.ext_zvkg || + cpu->cfg.ext_zvkned || cpu->cfg.ext_zvknha || cpu->cfg.ext_zvksed || + cpu->cfg.ext_zvksh) && !cpu->cfg.ext_zve32x) { +error_setg (errp, +"Vector crypto extensions require V or Zve* extensions" ); +return ; + } + +if ((cpu->cfg.ext_zvbc || cpu->cfg.ext_zvknhb) && !cpu->cfg.ext_zve64x) { +error_setg ( + errp, +"Zvbc and Zvknhb extensions require V or Zve64x extensions" ); +return ; + } + +if (cpu->cfg.ext_zicntr && !cpu->cfg.ext_zicsr) { +if (cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zicntr))) { +error_setg (errp,"zicntr requires zicsr" ); +return ; + } + cpu->cfg.ext_zicntr = false; + } + +if (cpu->cfg.ext_zihpm && !cpu->cfg.ext_zicsr) { +if (cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zihpm))) { +error_setg (errp,"zihpm requires zicsr" ); +return ; + } + cpu->cfg.ext_zihpm = false; + } + +if (cpu->cfg.ext_zicfiss) { +if (!cpu->cfg.ext_zicsr) { +error_setg (errp,"zicfiss extension requires zicsr extension" ); +return ; + } +if (!riscv_has_ext (env,RVA )) { +error_setg (errp,"zicfiss extension requires A extension" ); +return ; + } +if (!riscv_has_ext (env,RVS )) { +error_setg (errp,"zicfiss extension requires S" ); +return ; + } +if (!cpu->cfg.ext_zimop) { +error_setg (errp,"zicfiss extension requires zimop extension" ); +return ; + } +if (cpu->cfg.ext_zca && !cpu->cfg.ext_zcmop) { +error_setg (errp,"zicfiss with zca requires zcmop extension" ); +return ; + } + } + +if (!cpu->cfg.ext_zihpm) { + cpu->cfg.pmu_mask =0 ; + cpu->pmu_avail_ctrs =0 ; + } + +if (cpu->cfg.ext_zclsd) { +if (riscv_has_ext (env,RVC ) &&riscv_has_ext (env,RVF )) { +error_setg (errp, +"Zclsd cannot be supported together with C and F extension" ); +return ; + } +if (cpu->cfg.ext_zcf) { +error_setg (errp, +"Zclsd cannot be supported together with Zcf extension" ); +return ; + } + } + +if (cpu->cfg.ext_zicfilp && !cpu->cfg.ext_zicsr) { +error_setg (errp,"zicfilp extension requires zicsr extension" ); +return ; + } + +if (mcc->def->misa_mxl_max ==MXL_RV32 && cpu->cfg.ext_svukte) { +error_setg (errp,"svukte is not supported for RV32" ); +return ; + } + +if ((cpu->cfg.ext_smctr || cpu->cfg.ext_ssctr) && + (!riscv_has_ext (env,RVS ) || !cpu->cfg.ext_sscsrind)) { +if (cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_smctr)) || +cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_ssctr))) { +error_setg (errp,"Smctr and Ssctr require S-mode and Sscsrind" ); +return ; + } + cpu->cfg.ext_smctr = false; + cpu->cfg.ext_ssctr = false; + } + +if (cpu->cfg.ext_svrsw60t59b && + (!cpu->cfg.mmu || mcc->def->misa_mxl_max ==MXL_RV32 )) { +error_setg (errp,"svrsw60t59b is not supported on RV32 and MMU-less platforms" ); +return ; + } + +/* +* Disable isa extensions based on priv spec after we +* validated and set everything we need. +*/ +riscv_cpu_disable_priv_spec_isa_exts (cpu); +}
target/riscv/csr.c +0 -1
@@ -21,7 +21,6 @@#include "qemu/log.h" #include "qemu/timer.h" #include "cpu.h" -#include "tcg/tcg-cpu.h" #include "pmu.h" #include "time_helper.h" #include "exec/cputlb.h"
target/riscv/internals.h +19 -0
@@ -193,6 +193,25 @@ static inline target_ulong get_xepc_mask(CPURISCVState *env)bool riscv_cpu_has_work (CPUState *cs);#endif +void riscv_cpu_ext_user_opts_init (void ); +void riscv_cpu_cfg_ext_add_user_opt (uint32_t ext_offset,bool value); +void riscv_cpu_misa_ext_add_user_opt (uint32_t bit,bool value); +bool riscv_cpu_misa_ext_is_user_set (uint32_t bit); + +void riscv_cpu_write_misa_bit (RISCVCPU *cpu,uint32_t bit,bool enabled); + +void riscv_cpu_validate_misa_priv (CPURISCVState *env,Error **errp); +void riscv_cpu_update_cfg (RISCVCPU *cpu); + +void riscv_cpu_bump_multi_ext_priv_ver (CPURISCVState *env, +uint32_t ext_offset); +void riscv_cpu_cfg_ext_auto_update (RISCVCPU *cpu, +uint32_t ext_offset, +bool value); +const char *riscv_cpu_cfg_ext_get_name (uint32_t ext_offset); + +void riscv_cpu_validate_set_extensions (RISCVCPU *cpu,Error **errp); +/* Zjpm addr masking routine */ static inline target_ulong adjust_addr_body(CPURISCVState *env, target_ulong addr,
target/riscv/meson.build +1 -0
@@ -16,6 +16,7 @@ riscv_ss.add(when: 'CONFIG_ARM_COMPATIBLE_SEMIHOSTING', riscv_ss.add(files( 'cpu.c', + 'cpu-validate.c', 'cpu_helper.c', 'csr.c', 'fpu_helper.c',
target/riscv/tcg/tcg-cpu.c +22 -615
741 lines not on this page · raw message
target/riscv/tcg/tcg-cpu.h +0 -1
@@ -22,7 +22,6 @@#include "cpu.h" -void riscv_cpu_validate_set_extensions (RISCVCPU *cpu,Error **errp);void riscv_tcg_cpu_finalize_features (RISCVCPU *cpu,Error **errp);bool riscv_cpu_tcg_compatible (RISCVCPU *cpu);
Signature · 1 line
2.43.0
PATCH v3 2/6 target/riscv: move fflags helpers to common code
In reply to Zephyr Liriscv_cpu_get_fflags() and riscv_cpu_set_fflags() are used by CSR/FCSR handling and operate on architectural CPU state rather than translated floating-point instructions.
Move them out of fpu_helper.c so fpu_helper.c can be built only when TCG is enabled.
--- target/riscv/cpu.h | 3 --- target/riscv/cpu_helper.c | 28 ++++++++++++++++++++++++++++ target/riscv/fpu_helper.c | 27 --------------------------- target/riscv/internals.h | 3 +++ 4 files changed, 31 insertions(+), 30 deletions(-)
target/riscv/cpu.h +0 -3
@@ -666,9 +666,6 @@ G_NORETURN void riscv_raise_exception(CPURISCVState *env,RISCVException exception, uintptr_t pc); -target_ulong riscv_cpu_get_fflags (CPURISCVState *env); -void riscv_cpu_set_fflags (CPURISCVState *env,target_ulong ); -#ifndef CONFIG_USER_ONLY void cpu_set_exception_base (int vp_index,target_ulong address);#endif
target/riscv/cpu_helper.c +28 -0
@@ -21,6 +21,7 @@#include "qemu/log.h" #include "qemu/main-loop.h" #include "cpu.h" +#include "fpu/softfloat.h" #include "internals.h" #include "pmu.h" #include "exec/cputlb.h"
@@ -38,6 +39,33 @@#include "pmp.h" #include "qemu/plugin.h" +target_ulong riscv_cpu_get_fflags (CPURISCVState *env) +{ +int soft =get_float_exception_flags (&env->fp_status); +target_ulong hard =0 ; + + hard |= (soft & float_flag_inexact) ?FPEXC_NX :0 ; + hard |= (soft & float_flag_underflow) ?FPEXC_UF :0 ; + hard |= (soft & float_flag_overflow) ?FPEXC_OF :0 ; + hard |= (soft & float_flag_divbyzero) ?FPEXC_DZ :0 ; + hard |= (soft & float_flag_invalid) ?FPEXC_NV :0 ; + +return hard; +} + +void riscv_cpu_set_fflags (CPURISCVState *env,target_ulong hard) +{ +int soft =0 ; + + soft |= (hard &FPEXC_NX ) ? float_flag_inexact :0 ; + soft |= (hard &FPEXC_UF ) ? float_flag_underflow :0 ; + soft |= (hard &FPEXC_OF ) ? float_flag_overflow :0 ; + soft |= (hard &FPEXC_DZ ) ? float_flag_divbyzero :0 ; + soft |= (hard &FPEXC_NV ) ? float_flag_invalid :0 ; + +set_float_exception_flags (soft, &env->fp_status); +} +int riscv_env_mmu_index (CPURISCVState *env,bool ifetch) {#ifdef CONFIG_USER_ONLY
target/riscv/fpu_helper.c +0 -27
@@ -23,33 +23,6 @@#include "fpu/softfloat.h" #include "internals.h" -target_ulong riscv_cpu_get_fflags (CPURISCVState *env) -{ -int soft =get_float_exception_flags (&env->fp_status); -target_ulong hard =0 ; - - hard |= (soft & float_flag_inexact) ?FPEXC_NX :0 ; - hard |= (soft & float_flag_underflow) ?FPEXC_UF :0 ; - hard |= (soft & float_flag_overflow) ?FPEXC_OF :0 ; - hard |= (soft & float_flag_divbyzero) ?FPEXC_DZ :0 ; - hard |= (soft & float_flag_invalid) ?FPEXC_NV :0 ; - -return hard; -} - -void riscv_cpu_set_fflags (CPURISCVState *env,target_ulong hard) -{ -int soft =0 ; - - soft |= (hard &FPEXC_NX ) ? float_flag_inexact :0 ; - soft |= (hard &FPEXC_UF ) ? float_flag_underflow :0 ; - soft |= (hard &FPEXC_OF ) ? float_flag_overflow :0 ; - soft |= (hard &FPEXC_DZ ) ? float_flag_divbyzero :0 ; - soft |= (hard &FPEXC_NV ) ? float_flag_invalid :0 ; - -set_float_exception_flags (soft, &env->fp_status); -} -void helper_set_rounding_mode (CPURISCVState *env,uint32_t rm) {int softrm;
target/riscv/internals.h +3 -0
@@ -193,6 +193,9 @@ static inline target_ulong get_xepc_mask(CPURISCVState *env)bool riscv_cpu_has_work (CPUState *cs);#endif +target_ulong riscv_cpu_get_fflags (CPURISCVState *env); +void riscv_cpu_set_fflags (CPURISCVState *env,target_ulong val); +void riscv_cpu_ext_user_opts_init (void );void riscv_cpu_cfg_ext_add_user_opt (uint32_t ext_offset,bool value);void riscv_cpu_misa_ext_add_user_opt (uint32_t bit,bool value);
Signature · 1 line
2.43.0
PATCH v3 3/6 target/riscv: move riscv_raise_exception() out of op_helper.c
In reply to Zephyr Liriscv_raise_exception() is referenced by RISC-V internal code, but its current implementation relies on TCG's cpu_loop_exit_restore(). Move it to cpu_helper.c so op_helper.c can become TCG-only. The actual exception unwind still relies on TCG's cpu_loop_exit_restore(), so the non-TCG path remains unreachable.
--- target/riscv/cpu.h | 4 ---- target/riscv/cpu_helper.c | 19 +++++++++++++++++++ target/riscv/debug.c | 1 + target/riscv/internals.h | 4 ++++ target/riscv/op_helper.c | 15 --------------- target/riscv/zce_helper.c | 1 + 6 files changed, 25 insertions(+), 19 deletions(-)
target/riscv/cpu.h +0 -4
@@ -662,10 +662,6 @@ void riscv_translate_init(void);void riscv_translate_code (CPUState *cs,TranslationBlock *tb,int *max_insns,vaddr pc,void *host_pc); -G_NORETURN void riscv_raise_exception(CPURISCVState *env, -RISCVException exception, -uintptr_t pc); -#ifndef CONFIG_USER_ONLY void cpu_set_exception_base (int vp_index,target_ulong address);#endif
target/riscv/cpu_helper.c +19 -0
@@ -39,6 +39,25 @@#include "pmp.h" #include "qemu/plugin.h" +/* Exceptions processing helpers */ +G_NORETURN void riscv_raise_exception(CPURISCVState *env, +RISCVException exception, +uintptr_t pc) +{ +CPUState *cs =env_cpu (env); + +trace_riscv_exception (exception, +riscv_cpu_get_trap_name (exception, false), + env->pc); + + cs->exception_index = exception; +#ifdef CONFIG_TCG +cpu_loop_exit_restore (cs, pc); +#else +qemu_build_not_reached (); +#endif +} +target_ulong riscv_cpu_get_fflags (CPURISCVState *env) {int soft =get_float_exception_flags (&env->fp_status);
target/riscv/debug.c +1 -0
@@ -27,6 +27,7 @@#include "qemu/log.h" #include "qapi/error.h" #include "cpu.h" +#include "internals.h" #include "trace.h" #include "exec/helper-proto.h" #include "exec/watchpoint.h"
target/riscv/internals.h +4 -0
@@ -193,6 +193,10 @@ static inline target_ulong get_xepc_mask(CPURISCVState *env)bool riscv_cpu_has_work (CPUState *cs);#endif +G_NORETURN void riscv_raise_exception(CPURISCVState *env, +RISCVException exception, +uintptr_t pc); +target_ulong riscv_cpu_get_fflags (CPURISCVState *env);void riscv_cpu_set_fflags (CPURISCVState *env,target_ulong val);
target/riscv/op_helper.c +0 -15
@@ -28,21 +28,6 @@#include "exec/tlb-flags.h" #include "trace.h" -/* Exceptions processing helpers */ -G_NORETURN void riscv_raise_exception(CPURISCVState *env, -RISCVException exception, -uintptr_t pc) -{ -CPUState *cs =env_cpu (env); - -trace_riscv_exception (exception, -riscv_cpu_get_trap_name (exception, false), - env->pc); - - cs->exception_index = exception; -cpu_loop_exit_restore (cs, pc); -} -void helper_raise_exception (CPURISCVState *env,uint32_t exception) {riscv_raise_exception (env, exception,0 );
target/riscv/zce_helper.c +1 -0
@@ -18,6 +18,7 @@#include "qemu/osdep.h" #include "cpu.h" +#include "internals.h" #include "exec/helper-proto.h" #include "accel/tcg/cpu-ldst.h"
Signature · 1 line
2.43.0
PATCH v3 4/6 target/riscv: reject x-misa-w outside TCG
In reply to Zephyr Lix-misa-w controls dynamic writes to misa in the emulated CPU path and is only meaningful when TCG is available. Reject it explicitly outside TCG instead of relying on TCG-only finalize code.
This also covers CONFIG_TCG builds that are running with a non-TCG accelerator. It also rejects CPU models that require writable misa by default when the selected accelerator is not TCG.
--- target/riscv/cpu.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
target/riscv/cpu.c +12 -0
@@ -956,6 +956,18 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (dev);Error *local_err =NULL ; +if (cpu->cfg.misa_w) { +#ifdef CONFIG_TCG +if (!tcg_enabled ()) { +error_setg (errp,"x-misa-w requires TCG" ); +return ; + } +#else +error_setg (errp,"x-misa-w requires TCG" ); +return ; +#endif + } +cpu_exec_realizefn (cs, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);
Signature · 1 line
2.43.0
PATCH v3 5/6 target/riscv: build TCG-only sources only with TCG
In reply to Zephyr LiSeveral RISC-V source files contain translated-code helpers, decoders, or TCG-only instruction implementations, but are currently part of the common RISC-V source set. This breaks --disable-tcg builds once TCG headers and helpers are unavailable.
After moving common helpers out of TCG-only files, build the remaining TCG-only RISC-V sources only when CONFIG_TCG is enabled. Keep common CSR misa handling from relying on TCG unwind state in no-TCG builds by falling back to env->pc.
Drop the stale tcg/tcg.h include from common CPU code so no-TCG builds do not include a TCG-private header.
--- target/riscv/cpu.c | 1 - target/riscv/cpu_helper.c | 6 ++++++ target/riscv/csr.c | 11 +++++++++-- target/riscv/meson.build | 9 ++++++--- 4 files changed, 21 insertions(+), 6 deletions(-)
target/riscv/cpu.c +0 -1
@@ -36,7 +36,6 @@#include "system/tcg.h" #include "kvm/kvm_riscv.h" #include "tcg/tcg-cpu.h" -#include "tcg/tcg.h" /* RISC-V CPU definitions */ static const char riscv_single_letter_exts[] ="IEMAFDQCBPVH" ;
target/riscv/cpu_helper.c +6 -0
@@ -29,8 +29,10 @@#include "exec/target_page.h" #include "system/memory.h" #include "instmap.h" +#ifdef CONFIG_TCG #include "tcg/tcg-op.h" #include "accel/tcg/cpu-ops.h" +#endif #include "trace.h" #include "semihosting/common-semi.h" #include "exec/icount.h"
@@ -1714,6 +1716,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,return TRANSLATE_SUCCESS ; } +#ifdef CONFIG_TCG static void raise_mmu_exception(CPURISCVState *env, target_ulong address, MMUAccessType access_type, bool pmp_violation, bool first_stage, bool two_stage,
@@ -1756,6 +1759,7 @@ static void raise_mmu_exception(CPURISCVState *env, target_ulong address, env->two_stage_lookup = two_stage; env->two_stage_indirect_lookup = two_stage_indirect; } +#endif hwaddr riscv_cpu_get_phys_addr_debug (CPUState *cs,vaddr addr) {
@@ -1780,6 +1784,7 @@ hwaddr riscv_cpu_get_phys_addr_debug(CPUState *cs, vaddr addr)return phys_addr; } +#ifdef CONFIG_TCG void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr, vaddr addr, unsigned size, MMUAccessType access_type,
@@ -2004,6 +2009,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,return true; } +#endif static target_ulong riscv_transformed_insn(CPURISCVState *env, target_ulong insn,
target/riscv/csr.c +9 -2
@@ -2134,15 +2134,22 @@ static RISCVException read_misa(CPURISCVState *env, int csrno,static target_ulong get_next_pc (CPURISCVState *env,uintptr_t ra) { +/* Outside of a running cpu, env contains the next pc. */ +if (ra ==0 ) { +return env->pc; + } +#ifdef CONFIG_TCG uint64_t data[INSN_START_WORDS ]; -/* Outside of a running cpu, env contains the next pc. */ -if (ra ==0 || !cpu_unwind_state_data (env_cpu (env), ra, data)) { +if (!cpu_unwind_state_data (env_cpu (env), ra, data)) {return env->pc; }/* Within unwind data, [0] is pc and [1] is the opcode. */ return data[0 ] +insn_len (data[1 ]); +#else +return env->pc; +#endif } static RISCVException write_misa(CPURISCVState *env, int csrno,
target/riscv/meson.build +6 -3
@@ -9,7 +9,7 @@ gen = [ ] riscv_ss = ss.source_set() -riscv_ss.add(gen) +riscv_ss.add(when: 'CONFIG_TCG', if_true: gen) riscv_ss.add(when: 'CONFIG_ARM_COMPATIBLE_SEMIHOSTING', if_true: files('common-semi-target.c'))
@@ -19,11 +19,14 @@ riscv_ss.add(files( 'cpu-validate.c', 'cpu_helper.c', 'csr.c', - 'fpu_helper.c', 'gdbstub.c', + 'vector_internals.c', +)) + +riscv_ss.add(when: 'CONFIG_TCG', if_true: files( + 'fpu_helper.c', 'op_helper.c', 'vector_helper.c', - 'vector_internals.c', 'bitmanip_helper.c', 'translate.c', 'm128_helper.c',
Signature · 1 line
2.43.0
PATCH v3 6/6 gitlab-ci.d/crossbuilds: add riscv64 KVM-only build job
In reply to Zephyr LiAdd CI coverage for a riscv64 --disable-tcg cross build. This ensures that RISC-V no-TCG/KVM-only builds keep working and prevents TCG-only code from being accidentally pulled into common RISC-V sources again.
The cross_accel_build_job template enables KVM by default via --enable-${ACCEL:-kvm}, so the extra options only need to disable TCG.
--- .gitlab-ci.d/crossbuilds.yml | 8 ++++++++ 1 file changed, 8 insertions(+)
.gitlab-ci.d/crossbuilds.yml +8 -0
@@ -59,6 +59,14 @@ cross-riscv64-user: variables: IMAGE: debian-riscv64-cross +cross-riscv64-kvm-only: + extends: .cross_accel_build_job + needs: + - job: riscv64-debian-cross-container + variables: + IMAGE: debian-riscv64-cross + EXTRA_CONFIGURE_OPTS: --disable-tcg --without-default-features + cross-s390x-system: extends: .cross_system_build_job needs:
Signature · 1 line
2.43.0
Hi Zephyr,
Sorry to take this long to take a look at this work. I did it last Thursday. The overall idea is correct: we need to remove TCG exclusive bits out of the way of the KVM build. Our current code layout is awful for it though, and this is the root cause that needs addressing.
We have too much stuff at the target/riscv dir which is supposed to be code that is accel agnostic, and unfortunately this led you to wrong conclusions. For example, in patch 1, where you're moving validation code to from tcg-cpu.c a cpu-validate.c common file. KVM doesn't care about any of that - KVM validation is done at init time by the kernel module. It doesn't care about riscv_cpu_validate_set_extensions() and any of that.
For the record we had a series in 2023 that tried to fix --disable-tcg:
https://lore.kernel.org/qemu-devel/[email protected]/
but at that time we had other stuff going in parallel and the series eventually stopped applying and that was that. Which is a shame because that series was closer to what we need, but I won't ask Phil to re-send that again because it's a rather unpleasant rebase.
Unfortunately I don't see a sane way forward aside from doing the grunt and gruesome work first, i.e. putting TCG specific files in the TCG dir, like x86 and loongarch is doing, then go through each problem that --disable-tcg points out, fix it, and do that over and over until we have a working build. And there's a high chance that we might break some unintended stuff in the KVM driver regardless.
I'll send such a series later today. I ended up using 2 patches from your series (patch 1 from v2 and patch 6 from v3). Hopefully we'll be able to put this --disable-tcg business to rest.
Thanks,
Daniel
Quoted 64 lines · Zephyr Li
This series fixes the riscv64-softmmu --disable-tcg build by keeping TCG-only code out of no-TCG builds while preserving common RISC-V CPU helpers for non-TCG configurations.
Compared with v2, this version replaces the no-TCG stub for
riscv_cpu_validate_set_extensions()with a commoncpu-validate.csplit. The validator is used by common CSR code and should not live in TCG-only code.Changes in v3:
- Replace the no-TCG stub with a newcpu-validate.ccommon source file.
- Move common RISC-V extension validation and user-option tracking out oftarget/riscv/tcg/tcg-cpu.c.
- Move fflags and exception helpers out of TCG-only helper files.
- Reject x-misa-w outside TCG.
- Build RISC-V TCG-only sources only whenCONFIG_TCGis enabled.
- Add GitLab CI coverage for riscv64 KVM-only --disable-tcg builds.Testing:
- ../configure --target-list=riscv64-softmmu --enable-kvm --disable-tcg \
--enable-debug
ninja- ../configure --target-list=riscv64-softmmu,riscv64-linux-user \
--enable-debug
ninja- In the no-TCG build:
qemu-system-riscv64 -M virt,accel=qtest -cpu rv64,x-misa-w=true \
-nographic -S
reports "x-misa-w requires TCG".- In the TCG build:
qemu-system-riscv64 -M virt -accel tcg -cpu rv64,x-misa-w=true \
-nographic -S
starts without reporting "x-misa-w requires TCG".Zephyr Li (6):
target/riscv: move extension validation out of TCG-only code target/riscv: move fflags helpers to common code target/riscv: moveriscv_raise_exception()out ofop_helper.ctarget/riscv: reject x-misa-w outside TCG target/riscv: build TCG-only sources only with TCG gitlab-ci.d/crossbuilds: add riscv64 KVM-only build job.gitlab-ci.d/crossbuilds.yml | 8 + target/riscv/cpu-validate.c | 630 ++++++++++++++++++++++++++++++++++ target/riscv/cpu.c | 13 +- target/riscv/cpu.h | 7 - target/riscv/cpu_helper.c | 53 +++ target/riscv/csr.c | 12 +- target/riscv/debug.c | 1 + target/riscv/fpu_helper.c | 27 -- target/riscv/internals.h | 26 ++ target/riscv/meson.build | 10 +- target/riscv/op_helper.c | 15 - target/riscv/tcg/tcg-cpu.c | 637 ++--------------------------------- target/riscv/tcg/tcg-cpu.h | 1 - target/riscv/zce_helper.c | 1 + 14 files changed, 769 insertions(+), 672 deletions(-)create mode 100644
target/riscv/cpu-validate.cbase-commit:
5611a9268dae7b7ff99d478ed134052a9fc7e9f7