target/riscv: rework CPU extensions validation
v2 cover target/riscv: rework CPU extensions validation
Hello,
In this v2 the most notable changes were done after Liu Zhiwei review in [1], in particular the comments made in patch 17. To allow for write_misa() validation, without the need to store and restore cpu->cfg state, more design changes were required in the existing validation logic.
The validation code was split in three stages: validate misa_ext, validate cpu config and commit cpu config. riscv_cpu_validate_misa_ext() handles all validations related exclusively to env->misa_ext bits. riscv_cpu_validate_extensions() does the remaining validations with the named extensions we have. riscv_cpu_commit_cpu_cfg() is the last step, only executed after all the previous validations were ok.
All validations are done using a tentative misa_ext val, instead of env->misa_ext or cpu->cfg.ext_N props. write_misa() is then able to validate a misa_ext without having to change cpu->cfg needlesly.
Another change is that now we're forcing a sync between env->misa_ext and cpu->cfg. This was needed to allow for the validation split mentioned above. It'll also give more consistency throughout the code, granting that we're always getting the same information whether we're using cpu->cfg or an API such as riscv_has_ext().
All other premises from v1 are kept. All code changes suggested in v1 were implemented.
Patches are based on Alistair's riscv-to-apply.next.
Changes from v1:
- patch 14 ("target/riscv/cpu.c: do not allow RVE to be set"): dropped
- patch 4:
- PRIV_VERSION_LATEST is now an enum value instead of a macro
- patch 5:
- merged env->priv_ver cond assignment to the previous if clause
- a handful of patches added to allow for validate_set_extensions() to be split in three functions
- validation in write_misa() does not require commit changes to cpu->cfg beforehand
- v1 link: https://lists.gnu.org/archive/html/qemu-devel/2023-03/msg03219.html
[1] https://lists.gnu.org/archive/html/qemu-devel/2023-03/msg03219.html
Daniel Henrique Barboza (26):
target/riscv/cpu.c: add riscv_cpu_validate_v() target/riscv/cpu.c: remove set_vext_version() target/riscv/cpu.c: remove set_priv_version() target/riscv: add PRIV_VERSION_LATEST target/riscv/cpu.c: add priv_spec validate/disable_exts helpers target/riscv/cpu.c: add riscv_cpu_validate_misa_mxl() target/riscv: move pmp and epmp validations to
validate_set_extensions()
target/riscv/cpu.c: validate extensions before riscv_timer_init() target/riscv/cpu.c: remove cfg setup from riscv_cpu_init() target/riscv/cpu.c: avoid set_misa() in validate_set_extensions() target/riscv/cpu.c: set cpu config in set_misa() target/riscv/cpu.c: redesign register_cpu_props() target/riscv: put env->misa_ext <-> cpu->cfg code into helpers target/riscv: add RVG
target/riscv: do not allow RVG in write_misa() target/riscv/cpu.c: split RVG code from validate_set_extensions() target/riscv: write env->misa_ext* in register_generic_cpu_props() target/risc/cpu.c: add riscv_cpu_validate_misa_ext() target/riscv/cpu:c add misa_ext V-> D & F dependency target/riscv: move riscv_cpu_validate_v() to validate_misa_ext() target/riscv: validate_misa_ext() now validates a misa_ext val target/riscv: error out on priv failure for RVH target/riscv: split riscv_cpu_validate_set_extensions() target/riscv: use misa_ext val in riscv_cpu_validate_extensions() target/riscv: rework write_misa()
target/riscv: update cpu->cfg misa bits in commit_cpu_cfg()
target/riscv/cpu.c | 661 ++++++++++++++++++++++++++++----------------- target/riscv/cpu.h | 14 +- target/riscv/csr.c | 47 ++-- 3 files changed, 448 insertions(+), 274 deletions(-)
Signature · 1 line
2.39.2
PATCH v2 1/26 target/riscv/cpu.c: add riscv_cpu_validate_v()
The RVV verification will error out if fails and it's being done at the end of riscv_cpu_validate_set_extensions(). Let's put it in its own function and do it earlier.
We'll move it out of riscv_cpu_validate_set_extensions() in the near future, but for now this is enough to clean the code a bit.
--- target/riscv/cpu.c | 86 ++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 37 deletions(-)
target/riscv/cpu.c +49 -37
@@ -802,6 +802,46 @@ static void riscv_cpu_disas_set_info(CPUState *s, disassemble_info *info) } } +static void riscv_cpu_validate_v (CPURISCVState *env,RISCVCPUConfig *cfg, +Error **errp) +{ +int vext_version =VEXT_VERSION_1_00_0 ; + +if (!is_power_of_2 (cfg->vlen)) { +error_setg (errp,"Vector extension VLEN must be power of 2" ); +return ; + } +if (cfg->vlen >RV_VLEN_MAX || cfg->vlen <128 ) { +error_setg (errp, +"Vector extension implementation only supports VLEN " +"in the range [128, %d]" ,RV_VLEN_MAX ); +return ; + } +if (!is_power_of_2 (cfg->elen)) { +error_setg (errp,"Vector extension ELEN must be power of 2" ); +return ; + } +if (cfg->elen >64 || cfg->elen <8 ) { +error_setg (errp, +"Vector extension implementation only supports ELEN " +"in the range [8, 64]" ); +return ; + } +if (cfg->vext_spec) { +if (!g_strcmp0 (cfg->vext_spec,"v1.0" )) { + vext_version =VEXT_VERSION_1_00_0 ; + }else { +error_setg (errp,"Unsupported vector spec version '%s'" , + cfg->vext_spec); +return ; + } + }else { +qemu_log ("vector version is not specified, " +"use the default value v1.0\n" ); + } +set_vext_version (env, vext_version); +} +/* * Check consistency between chosen extensions while setting * cpu->cfg accordingly, doing a set_misa() in the end.
@@ -809,6 +849,7 @@ static void riscv_cpu_disas_set_info(CPUState *s, disassemble_info *info)static void riscv_cpu_validate_set_extensions (RISCVCPU *cpu,Error **errp) {CPURISCVState *env = &cpu->env; +Error *local_err =NULL ;uint32_t ext =0 ;/* Do some ISA extension error checking */
@@ -939,6 +980,14 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) } } +if (cpu->cfg.ext_v) { +riscv_cpu_validate_v (env, &cpu->cfg, &local_err); +if (local_err !=NULL ) { +error_propagate (errp, local_err); +return ; + } + } +if (cpu->cfg.ext_zk) { cpu->cfg.ext_zkn = true; cpu->cfg.ext_zkr = true;
@@ -993,44 +1042,7 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) ext |=RVH ; }if (cpu->cfg.ext_v) { -int vext_version =VEXT_VERSION_1_00_0 ; ext |=RVV ; -if (!is_power_of_2 (cpu->cfg.vlen)) { -error_setg (errp, -"Vector extension VLEN must be power of 2" ); -return ; - } -if (cpu->cfg.vlen >RV_VLEN_MAX || cpu->cfg.vlen <128 ) { -error_setg (errp, -"Vector extension implementation only supports VLEN " -"in the range [128, %d]" ,RV_VLEN_MAX ); -return ; - } -if (!is_power_of_2 (cpu->cfg.elen)) { -error_setg (errp, -"Vector extension ELEN must be power of 2" ); -return ; - } -if (cpu->cfg.elen >64 || cpu->cfg.elen <8 ) { -error_setg (errp, -"Vector extension implementation only supports ELEN " -"in the range [8, 64]" ); -return ; - } -if (cpu->cfg.vext_spec) { -if (!g_strcmp0 (cpu->cfg.vext_spec,"v1.0" )) { - vext_version =VEXT_VERSION_1_00_0 ; - }else { -error_setg (errp, -"Unsupported vector spec version '%s'" , - cpu->cfg.vext_spec); -return ; - } - }else { -qemu_log ("vector version is not specified, " -"use the default value v1.0\n" ); - } -set_vext_version (env, vext_version); }if (cpu->cfg.ext_j) { ext |=RVJ ;
Signature · 1 line
2.39.2
PATCH v2 2/26 target/riscv/cpu.c: remove set_vext_version()
In reply to Daniel Henrique BarbozaThis setter is doing nothing else but setting env->vext_ver. Assign the value directly.
--- target/riscv/cpu.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
target/riscv/cpu.c +1 -6
@@ -245,11 +245,6 @@ static void set_priv_version(CPURISCVState *env, int priv_ver) env->priv_ver = priv_ver; } -static void set_vext_version (CPURISCVState *env,int vext_ver) -{ - env->vext_ver = vext_ver; -} -#ifndef CONFIG_USER_ONLY static uint8_t satp_mode_from_str (const char *satp_mode_str) {
@@ -839,7 +834,7 @@ static void riscv_cpu_validate_v(CPURISCVState *env, RISCVCPUConfig *cfg,qemu_log ("vector version is not specified, " "use the default value v1.0\n" ); } -set_vext_version (env, vext_version); + env->vext_ver = vext_version; }/*
Signature · 1 line
2.39.2
PATCH v2 3/26 target/riscv/cpu.c: remove set_priv_version()
In reply to Daniel Henrique BarbozaThe setter is doing nothing special. Just set env->priv_ver directly.
--- target/riscv/cpu.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-)
target/riscv/cpu.c +13 -17
@@ -240,11 +240,6 @@ static void set_misa(CPURISCVState *env, RISCVMXL mxl, uint32_t ext) env->misa_ext_mask = env->misa_ext = ext; } -static void set_priv_version (CPURISCVState *env,int priv_ver) -{ - env->priv_ver = priv_ver; -} -#ifndef CONFIG_USER_ONLY static uint8_t satp_mode_from_str (const char *satp_mode_str) {
@@ -343,7 +338,7 @@ static void riscv_any_cpu_init(Object *obj) VM_1_10_SV32 : VM_1_10_SV57);#endif -set_priv_version (env,PRIV_VERSION_1_12_0 ); + env->priv_ver =PRIV_VERSION_1_12_0 ;register_cpu_props (obj); }
@@ -355,7 +350,7 @@ static void rv64_base_cpu_init(Object *obj)set_misa (env,MXL_RV64 ,0 );register_cpu_props (obj);/* Set latest version of privileged specification */ -set_priv_version (env,PRIV_VERSION_1_12_0 ); + env->priv_ver =PRIV_VERSION_1_12_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV57 );#endif
@@ -366,7 +361,7 @@ static void rv64_sifive_u_cpu_init(Object *obj)CPURISCVState *env = &RISCV_CPU (obj)->env;set_misa (env,MXL_RV64 ,RVI |RVM |RVA |RVF |RVD |RVC |RVS |RVU );register_cpu_props (obj); -set_priv_version (env,PRIV_VERSION_1_10_0 ); + env->priv_ver =PRIV_VERSION_1_10_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV39 );#endif
@@ -379,7 +374,7 @@ static void rv64_sifive_e_cpu_init(Object *obj)set_misa (env,MXL_RV64 ,RVI |RVM |RVA |RVC |RVU );register_cpu_props (obj); -set_priv_version (env,PRIV_VERSION_1_10_0 ); + env->priv_ver =PRIV_VERSION_1_10_0 ; cpu->cfg.mmu = false;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );
@@ -392,7 +387,7 @@ static void rv64_thead_c906_cpu_init(Object *obj)RISCVCPU *cpu =RISCV_CPU (obj);set_misa (env,MXL_RV64 ,RVI |RVM |RVA |RVF |RVD |RVC |RVS |RVU ); -set_priv_version (env,PRIV_VERSION_1_11_0 ); + env->priv_ver =PRIV_VERSION_1_11_0 ; cpu->cfg.ext_g = true; cpu->cfg.ext_c = true;
@@ -431,7 +426,7 @@ static void rv128_base_cpu_init(Object *obj)set_misa (env,MXL_RV128 ,0 );register_cpu_props (obj);/* Set latest version of privileged specification */ -set_priv_version (env,PRIV_VERSION_1_12_0 ); + env->priv_ver =PRIV_VERSION_1_12_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV57 );#endif
@@ -444,7 +439,7 @@ static void rv32_base_cpu_init(Object *obj)set_misa (env,MXL_RV32 ,0 );register_cpu_props (obj);/* Set latest version of privileged specification */ -set_priv_version (env,PRIV_VERSION_1_12_0 ); + env->priv_ver =PRIV_VERSION_1_12_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV32 );#endif
@@ -454,8 +449,9 @@ static void rv32_sifive_u_cpu_init(Object *obj) {CPURISCVState *env = &RISCV_CPU (obj)->env;set_misa (env,MXL_RV32 ,RVI |RVM |RVA |RVF |RVD |RVC |RVS |RVU ); +register_cpu_props (obj); -set_priv_version (env,PRIV_VERSION_1_10_0 ); + env->priv_ver =PRIV_VERSION_1_10_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV32 );#endif
@@ -468,7 +464,7 @@ static void rv32_sifive_e_cpu_init(Object *obj)set_misa (env,MXL_RV32 ,RVI |RVM |RVA |RVC |RVU );register_cpu_props (obj); -set_priv_version (env,PRIV_VERSION_1_10_0 ); + env->priv_ver =PRIV_VERSION_1_10_0 ; cpu->cfg.mmu = false;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );
@@ -482,7 +478,7 @@ static void rv32_ibex_cpu_init(Object *obj)set_misa (env,MXL_RV32 ,RVI |RVM |RVC |RVU );register_cpu_props (obj); -set_priv_version (env,PRIV_VERSION_1_11_0 ); + env->priv_ver =PRIV_VERSION_1_11_0 ; cpu->cfg.mmu = false;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );
@@ -497,7 +493,7 @@ static void rv32_imafcu_nommu_cpu_init(Object *obj)set_misa (env,MXL_RV32 ,RVI |RVM |RVA |RVF |RVC |RVU );register_cpu_props (obj); -set_priv_version (env,PRIV_VERSION_1_10_0 ); + env->priv_ver =PRIV_VERSION_1_10_0 ; cpu->cfg.mmu = false;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );
@@ -1160,7 +1156,7 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) }if (priv_version >=PRIV_VERSION_1_10_0 ) { -set_priv_version (env, priv_version); + env->priv_ver = priv_version; }/* Force disable extensions if priv spec version does not match */
Signature · 1 line
2.39.2
PATCH v2 4/26 target/riscv: add PRIV_VERSION_LATEST
In reply to Daniel Henrique BarbozaAll these generic CPUs are using the latest priv available, at this moment PRIV_VERSION_1_12_0:
- riscv_any_cpu_init()
- rv32_base_cpu_init()
- rv64_base_cpu_init()
- rv128_base_cpu_init()
Create a new PRIV_VERSION_LATEST enum and use it in those cases. I'll make it easier to update everything at once when a new priv version is available.
--- target/riscv/cpu.c | 8 ++++---- target/riscv/cpu.h | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-)
target/riscv/cpu.c +4 -4
@@ -338,7 +338,7 @@ static void riscv_any_cpu_init(Object *obj) VM_1_10_SV32 : VM_1_10_SV57);#endif - env->priv_ver =PRIV_VERSION_1_12_0 ; + env->priv_ver =PRIV_VERSION_LATEST ;register_cpu_props (obj); }
@@ -350,7 +350,7 @@ static void rv64_base_cpu_init(Object *obj)set_misa (env,MXL_RV64 ,0 );register_cpu_props (obj);/* Set latest version of privileged specification */ - env->priv_ver =PRIV_VERSION_1_12_0 ; + env->priv_ver =PRIV_VERSION_LATEST ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV57 );#endif
@@ -426,7 +426,7 @@ static void rv128_base_cpu_init(Object *obj)set_misa (env,MXL_RV128 ,0 );register_cpu_props (obj);/* Set latest version of privileged specification */ - env->priv_ver =PRIV_VERSION_1_12_0 ; + env->priv_ver =PRIV_VERSION_LATEST ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV57 );#endif
@@ -439,7 +439,7 @@ static void rv32_base_cpu_init(Object *obj)set_misa (env,MXL_RV32 ,0 );register_cpu_props (obj);/* Set latest version of privileged specification */ - env->priv_ver =PRIV_VERSION_1_12_0 ; + env->priv_ver =PRIV_VERSION_LATEST ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV32 );#endif
target/riscv/cpu.h +2 -0
@@ -88,6 +88,8 @@ enum {PRIV_VERSION_1_10_0 =0 ,PRIV_VERSION_1_11_0 ,PRIV_VERSION_1_12_0 , + + PRIV_VERSION_LATEST = PRIV_VERSION_1_12_0, };#define VEXT_VERSION_1_00_0 0x00010000
Signature · 1 line
2.39.2
Re: [PATCH for-8.1 v2 04/26] target/riscv: add PRIV_VERSION_LATEST
Quoted 12 lines · Daniel Henrique Barboza
All these generic CPUs are using the latest priv available, at this moment
PRIV_VERSION_1_12_0:-
riscv_any_cpu_init()
-rv32_base_cpu_init()
-rv64_base_cpu_init()
-rv128_base_cpu_init()Create a new
PRIV_VERSION_LATESTenum and use it in those cases. I'll make it easier to update everything at once when a new priv version is available.
Quoted 5 lines
Signed-off-by Daniel Henrique Barboza<[email protected]>--- target/riscv/cpu.c | 8 ++++---- target/riscv/cpu.h | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-)
r~
PATCH v2 5/26 target/riscv/cpu.c: add priv_spec validate/disable_exts helpers
In reply to Daniel Henrique BarbozaWe're doing env->priv_spec validation and assignment at the start of riscv_cpu_realize(), which is fine, but then we're doing a force disable on extensions that aren't compatible with the priv version.
This second step is being done too early. The disabled extensions might be re-enabled again in riscv_cpu_validate_set_extensions() by accident. A better place to put this code is at the end of riscv_cpu_validate_set_extensions() after all the validations are completed.
Add a new helper, riscv_cpu_disable_priv_spec_isa_exts(), to disable the extesions after the validation is done. While we're at it, create a riscv_cpu_validate_priv_spec() helper to host all env->priv_spec related validation to unclog riscv_cpu_realize a bit.
--- target/riscv/cpu.c | 91 ++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 35 deletions(-)
target/riscv/cpu.c +56 -35
@@ -833,6 +833,52 @@ static void riscv_cpu_validate_v(CPURISCVState *env, RISCVCPUConfig *cfg, env->vext_ver = vext_version; } +static void riscv_cpu_validate_priv_spec (RISCVCPU *cpu,Error **errp) +{ +CPURISCVState *env = &cpu->env; +int priv_version =-1 ; + +if (cpu->cfg.priv_spec) { +if (!g_strcmp0 (cpu->cfg.priv_spec,"v1.12.0" )) { + priv_version =PRIV_VERSION_1_12_0 ; + }else if (!g_strcmp0 (cpu->cfg.priv_spec,"v1.11.0" )) { + priv_version =PRIV_VERSION_1_11_0 ; + }else if (!g_strcmp0 (cpu->cfg.priv_spec,"v1.10.0" )) { + priv_version =PRIV_VERSION_1_10_0 ; + }else { +error_setg (errp, +"Unsupported privilege spec version '%s'" , + cpu->cfg.priv_spec); +return ; + } + + env->priv_ver = priv_version; + } +} + +static void riscv_cpu_disable_priv_spec_isa_exts (RISCVCPU *cpu) +{ +CPURISCVState *env = &cpu->env; +int i; + +/* Force disable extensions if priv spec version does not match */ +for (i =0 ; i <ARRAY_SIZE (isa_edata_arr); i++) { +if (isa_ext_is_enabled (cpu, &isa_edata_arr[i]) && + (env->priv_ver < isa_edata_arr[i].min_version)) { +isa_ext_update_enabled (cpu, &isa_edata_arr[i], false); +#ifndef CONFIG_USER_ONLY +warn_report ("disabling %s extension for hart 0x" TARGET_FMT_lx +" because privilege spec version does not match" , + isa_edata_arr[i].name, env->mhartid); +#else +warn_report ("disabling %s extension because " +"privilege spec version does not match" , + isa_edata_arr[i].name); +#endif + } + } +} +/* * Check consistency between chosen extensions while setting * cpu->cfg accordingly, doing a set_misa() in the end.
@@ -1002,6 +1048,12 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) cpu->cfg.ext_zksh = true; } +/* +* Disable isa extensions based on priv spec after we +* validated and set everything we need. +*/ +riscv_cpu_disable_priv_spec_isa_exts (cpu); +if (cpu->cfg.ext_i) { ext |=RVI ; }
@@ -1131,7 +1183,6 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)CPURISCVState *env = &cpu->env;RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (dev);CPUClass *cc =CPU_CLASS (mcc); -int i, priv_version =-1 ;Error *local_err =NULL ;cpu_exec_realizefn (cs, &local_err);
@@ -1140,40 +1191,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } -if (cpu->cfg.priv_spec) { -if (!g_strcmp0 (cpu->cfg.priv_spec,"v1.12.0" )) { - priv_version =PRIV_VERSION_1_12_0 ; - }else if (!g_strcmp0 (cpu->cfg.priv_spec,"v1.11.0" )) { - priv_version =PRIV_VERSION_1_11_0 ; - }else if (!g_strcmp0 (cpu->cfg.priv_spec,"v1.10.0" )) { - priv_version =PRIV_VERSION_1_10_0 ; - }else { -error_setg (errp, -"Unsupported privilege spec version '%s'" , - cpu->cfg.priv_spec); -return ; - } - } - -if (priv_version >=PRIV_VERSION_1_10_0 ) { - env->priv_ver = priv_version; - } - -/* Force disable extensions if priv spec version does not match */ -for (i =0 ; i <ARRAY_SIZE (isa_edata_arr); i++) { -if (isa_ext_is_enabled (cpu, &isa_edata_arr[i]) && - (env->priv_ver < isa_edata_arr[i].min_version)) { -isa_ext_update_enabled (cpu, &isa_edata_arr[i], false); -#ifndef CONFIG_USER_ONLY -warn_report ("disabling %s extension for hart 0x" TARGET_FMT_lx -" because privilege spec version does not match" , - isa_edata_arr[i].name, env->mhartid); -#else -warn_report ("disabling %s extension because " -"privilege spec version does not match" , - isa_edata_arr[i].name); -#endif - } +riscv_cpu_validate_priv_spec (cpu, &local_err); +if (local_err !=NULL ) { +error_propagate (errp, local_err); +return ; }if (cpu->cfg.epmp && !cpu->cfg.pmp) {
Signature · 1 line
2.39.2
PATCH v2 6/26 target/riscv/cpu.c: add riscv_cpu_validate_misa_mxl()
In reply to Daniel Henrique BarbozaLet's remove more code that is open coded in riscv_cpu_realize() and put it into a helper. Let's also add an error message instead of just asserting out if env->misa_mxl_max != env->misa_mlx.
--- target/riscv/cpu.c | 51 ++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 18 deletions(-)
target/riscv/cpu.c +33 -18
@@ -879,6 +879,33 @@ static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu) } } +static void riscv_cpu_validate_misa_mxl (RISCVCPU *cpu,Error **errp) +{ +RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (cpu); +CPUClass *cc =CPU_CLASS (mcc); +CPURISCVState *env = &cpu->env; + +/* Validate that MISA_MXL is set properly. */ +switch (env->misa_mxl_max) { +#ifdef TARGET_RISCV64 +case MXL_RV64 : +case MXL_RV128 : + cc->gdb_core_xml_file ="riscv-64bit-cpu.xml" ; +break ; +#endif +case MXL_RV32 : + cc->gdb_core_xml_file ="riscv-32bit-cpu.xml" ; +break ; +default : +g_assert_not_reached (); + } + +if (env->misa_mxl_max != env->misa_mxl) { +error_setg (errp,"misa_mxl_max must be equal to misa_mxl" ); +return ; + } +} +/* * Check consistency between chosen extensions while setting * cpu->cfg accordingly, doing a set_misa() in the end.
@@ -1180,9 +1207,7 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) {CPUState *cs =CPU (dev);RISCVCPU *cpu =RISCV_CPU (dev); -CPURISCVState *env = &cpu->env;RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (dev); -CPUClass *cc =CPU_CLASS (mcc);Error *local_err =NULL ;cpu_exec_realizefn (cs, &local_err);
@@ -1197,6 +1222,12 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } +riscv_cpu_validate_misa_mxl (cpu, &local_err); +if (local_err !=NULL ) { +error_propagate (errp, local_err); +return ; + } +if (cpu->cfg.epmp && !cpu->cfg.pmp) {/* * Enhanced PMP should only be available
@@ -1213,22 +1244,6 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) }#endif /* CONFIG_USER_ONLY */ -/* Validate that MISA_MXL is set properly. */ -switch (env->misa_mxl_max) { -#ifdef TARGET_RISCV64 -case MXL_RV64 : -case MXL_RV128 : - cc->gdb_core_xml_file ="riscv-64bit-cpu.xml" ; -break ; -#endif -case MXL_RV32 : - cc->gdb_core_xml_file ="riscv-32bit-cpu.xml" ; -break ; -default : -g_assert_not_reached (); - } -assert (env->misa_mxl_max == env->misa_mxl); -riscv_cpu_validate_set_extensions (cpu, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);
Signature · 1 line
2.39.2
PATCH v2 7/26 target/riscv: move pmp and epmp validations to validate_set_extensions()
In reply to Daniel Henrique BarbozaIn the near future, write_misa() will use a variation of what we have now as riscv_cpu_validate_set_extensions(). The pmp and epmp validation will be required in write_misa() and it's already required here in riscv_cpu_realize(), so move it to riscv_cpu_validate_set_extensions().
--- target/riscv/cpu.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
target/riscv/cpu.c +9 -10
@@ -916,6 +916,15 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)Error *local_err =NULL ;uint32_t ext =0 ; +if (cpu->cfg.epmp && !cpu->cfg.pmp) { +/* +* Enhanced PMP should only be available +* on harts with PMP support +*/ +error_setg (errp,"Invalid configuration: EPMP requires PMP support" ); +return ; + } +/* Do some ISA extension error checking */ if (cpu->cfg.ext_g && !(cpu->cfg.ext_i && cpu->cfg.ext_m && cpu->cfg.ext_a && cpu->cfg.ext_f &&
@@ -1228,16 +1237,6 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } -if (cpu->cfg.epmp && !cpu->cfg.pmp) { -/* -* Enhanced PMP should only be available -* on harts with PMP support -*/ -error_setg (errp,"Invalid configuration: EPMP requires PMP support" ); -return ; - } - -#ifndef CONFIG_USER_ONLY if (cpu->cfg.ext_sstc) {riscv_timer_init (cpu);
Signature · 1 line
2.39.2
PATCH v2 8/26 target/riscv/cpu.c: validate extensions before riscv_timer_init()
In reply to Daniel Henrique BarbozaThere is no need to init timers if we're not even sure that our extensions are valid. Execute riscv_cpu_validate_set_extensions() before riscv_timer_init().
--- target/riscv/cpu.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
target/riscv/cpu.c +4 -6
@@ -1237,12 +1237,6 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } -#ifndef CONFIG_USER_ONLY -if (cpu->cfg.ext_sstc) { -riscv_timer_init (cpu); - } -#endif /* CONFIG_USER_ONLY */ -riscv_cpu_validate_set_extensions (cpu, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);
@@ -1250,6 +1244,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) }#ifndef CONFIG_USER_ONLY +if (cpu->cfg.ext_sstc) { +riscv_timer_init (cpu); + } +if (cpu->cfg.pmu_num) {if (!riscv_pmu_init (cpu, cpu->cfg.pmu_num) && cpu->cfg.ext_sscofpmf) { cpu->pmu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
Signature · 1 line
2.39.2
PATCH v2 9/26 target/riscv/cpu.c: remove cfg setup from riscv_cpu_init()
In reply to Daniel Henrique BarbozaWe have 4 config settings being done in riscv_cpu_init(): ext_ifencei, ext_icsr, mmu and pmp. This is also the constructor of the "riscv-cpu" device, which happens to be the parent device of every RISC-V cpu.
The result is that these 4 configs are being set every time, and every other CPU should always account for them. CPUs such as sifive_e need to disable settings that aren't enabled simply because the parent class happens to be enabling it.
Moving all configurations from the parent class to each CPU will centralize the config of each CPU into its own init(), which is clearer than having to account to whatever happens to be set in the parent device. These settings are also being set in register_cpu_props() when no 'misa_ext' is set, so for these CPUs we don't need changes. Named CPUs will receive all cfgs that the parent were setting into their init().
--- target/riscv/cpu.c | 60 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 12 deletions(-)
target/riscv/cpu.c +48 -12
@@ -325,7 +325,8 @@ static void set_satp_mode_default_map(RISCVCPU *cpu)static void riscv_any_cpu_init (Object *obj) { -CPURISCVState *env = &RISCV_CPU (obj)->env; +RISCVCPU *cpu =RISCV_CPU (obj); +CPURISCVState *env = &cpu->env;#if defined(TARGET_RISCV32 )set_misa (env,MXL_RV32 ,RVI |RVM |RVA |RVF |RVD |RVC |RVU );#elif defined(TARGET_RISCV64 )
@@ -340,6 +341,12 @@ static void riscv_any_cpu_init(Object *obj) env->priv_ver =PRIV_VERSION_LATEST ;register_cpu_props (obj); + +/* inherited from parent obj via riscv_cpu_init() */ + cpu->cfg.ext_ifencei = true; + cpu->cfg.ext_icsr = true; + cpu->cfg.mmu = true; + cpu->cfg.pmp = true; }#if defined(TARGET_RISCV64 )
@@ -358,13 +365,20 @@ static void rv64_base_cpu_init(Object *obj)static void rv64_sifive_u_cpu_init (Object *obj) { -CPURISCVState *env = &RISCV_CPU (obj)->env; +RISCVCPU *cpu =RISCV_CPU (obj); +CPURISCVState *env = &cpu->env;set_misa (env,MXL_RV64 ,RVI |RVM |RVA |RVF |RVD |RVC |RVS |RVU );register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_10_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV39 );#endif + +/* inherited from parent obj via riscv_cpu_init() */ + cpu->cfg.ext_ifencei = true; + cpu->cfg.ext_icsr = true; + cpu->cfg.mmu = true; + cpu->cfg.pmp = true; }static void rv64_sifive_e_cpu_init (Object *obj)
@@ -375,10 +389,14 @@ static void rv64_sifive_e_cpu_init(Object *obj)set_misa (env,MXL_RV64 ,RVI |RVM |RVA |RVC |RVU );register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_10_0 ; - cpu->cfg.mmu = false;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );#endif + +/* inherited from parent obj via riscv_cpu_init() */ + cpu->cfg.ext_ifencei = true; + cpu->cfg.ext_icsr = true; + cpu->cfg.pmp = true; }static void rv64_thead_c906_cpu_init (Object *obj)
@@ -411,6 +429,10 @@ static void rv64_thead_c906_cpu_init(Object *obj)#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_SV39 );#endif + +/* inherited from parent obj via riscv_cpu_init() */ + cpu->cfg.ext_ifencei = true; + cpu->cfg.pmp = true; }static void rv128_base_cpu_init (Object *obj)
@@ -447,7 +469,8 @@ static void rv32_base_cpu_init(Object *obj)static void rv32_sifive_u_cpu_init (Object *obj) { -CPURISCVState *env = &RISCV_CPU (obj)->env; +RISCVCPU *cpu =RISCV_CPU (obj); +CPURISCVState *env = &cpu->env;set_misa (env,MXL_RV32 ,RVI |RVM |RVA |RVF |RVD |RVC |RVS |RVU );register_cpu_props (obj);
@@ -455,6 +478,12 @@ static void rv32_sifive_u_cpu_init(Object *obj)#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV32 );#endif + +/* inherited from parent obj via riscv_cpu_init() */ + cpu->cfg.ext_ifencei = true; + cpu->cfg.ext_icsr = true; + cpu->cfg.mmu = true; + cpu->cfg.pmp = true; }static void rv32_sifive_e_cpu_init (Object *obj)
@@ -465,10 +494,14 @@ static void rv32_sifive_e_cpu_init(Object *obj)set_misa (env,MXL_RV32 ,RVI |RVM |RVA |RVC |RVU );register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_10_0 ; - cpu->cfg.mmu = false;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );#endif + +/* inherited from parent obj via riscv_cpu_init() */ + cpu->cfg.ext_ifencei = true; + cpu->cfg.ext_icsr = true; + cpu->cfg.pmp = true; }static void rv32_ibex_cpu_init (Object *obj)
@@ -479,11 +512,15 @@ static void rv32_ibex_cpu_init(Object *obj)set_misa (env,MXL_RV32 ,RVI |RVM |RVC |RVU );register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_11_0 ; - cpu->cfg.mmu = false;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );#endif cpu->cfg.epmp = true; + +/* inherited from parent obj via riscv_cpu_init() */ + cpu->cfg.ext_ifencei = true; + cpu->cfg.ext_icsr = true; + cpu->cfg.pmp = true; }static void rv32_imafcu_nommu_cpu_init (Object *obj)
@@ -494,10 +531,14 @@ static void rv32_imafcu_nommu_cpu_init(Object *obj)set_misa (env,MXL_RV32 ,RVI |RVM |RVA |RVF |RVC |RVU );register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_10_0 ; - cpu->cfg.mmu = false;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );#endif + +/* inherited from parent obj via riscv_cpu_init() */ + cpu->cfg.ext_ifencei = true; + cpu->cfg.ext_icsr = true; + cpu->cfg.pmp = true; }#endif
@@ -1384,11 +1425,6 @@ static void riscv_cpu_init(Object *obj) {RISCVCPU *cpu =RISCV_CPU (obj); - cpu->cfg.ext_ifencei = true; - cpu->cfg.ext_icsr = true; - cpu->cfg.mmu = true; - cpu->cfg.pmp = true; -cpu_set_cpustate_pointers (cpu);#ifndef CONFIG_USER_ONLY
Signature · 1 line
2.39.2
PATCH v2 10/26 target/riscv/cpu.c: avoid set_misa() in validate_set_extensions()
In reply to Daniel Henrique Barbozaset_misa() will be tuned up to do more than it's already doing and it will be redundant to what riscv_cpu_validate_set_extensions() does.
Note that we don't ever change env->misa_mlx in this function, so set_misa() can be replaced by just assigning env->misa_ext and env->misa_ext_mask to 'ext'.
--- target/riscv/cpu.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
target/riscv/cpu.c +3 -2
@@ -949,7 +949,8 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)/* * Check consistency between chosen extensions while setting -* cpu->cfg accordingly, doing a set_misa() in the end. +* cpu->cfg accordingly, setting env->misa_ext and +* misa_ext_mask in the end. */ static void riscv_cpu_validate_set_extensions (RISCVCPU *cpu,Error **errp) {
@@ -1168,7 +1169,7 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) ext |=RVJ ; } -set_misa (env, env->misa_mxl, ext); + env->misa_ext_mask = env->misa_ext = ext; }#ifndef CONFIG_USER_ONLY
Signature · 1 line
2.39.2
PATCH v2 11/26 target/riscv/cpu.c: set cpu config in set_misa()
In reply to Daniel Henrique Barbozaset_misa() is setting all 'misa' related env states and nothing else. But other functions, namely riscv_cpu_validate_set_extensions(), uses the config object to do its job.
This creates a need to set the single letter extensions in the cfg object to keep both in sync. At this moment this is being done by register_cpu_props(), forcing every CPU to do a call to this function.
Let's beef up set_misa() and make the function do the sync for us. This will relieve named CPUs to having to call register_cpu_props(), which will then be redesigned to a more specialized role next.
--- target/riscv/cpu.c | 40 ++++++++++++++++++++++++++++++++-------- target/riscv/cpu.h | 4 ++-- 2 files changed, 34 insertions(+), 10 deletions(-)
target/riscv/cpu.c +32 -8
@@ -236,8 +236,40 @@ const char *riscv_cpu_get_trap_name(target_ulong cause, bool async)static void set_misa (CPURISCVState *env,RISCVMXL mxl,uint32_t ext) { +RISCVCPU *cpu; + env->misa_mxl_max = env->misa_mxl = mxl; env->misa_ext_mask = env->misa_ext = ext; + +/* +* ext = 0 will only be a thing during cpu_init() functions +* as a way of setting an extension-agnostic CPU. We do +* not support clearing misa_ext* and the ext_N flags in +* RISCVCPUConfig in regular circunstances. +*/ +if (ext ==0 ) { +return ; + } + +/* +* We can't use riscv_cpu_cfg() in this case because it is +* a read-only inline and we're going to change the values +* of cpu->cfg. +*/ + cpu =env_archcpu (env); + + cpu->cfg.ext_i = ext &RVI ; + cpu->cfg.ext_e = ext &RVE ; + cpu->cfg.ext_m = ext &RVM ; + cpu->cfg.ext_a = ext &RVA ; + cpu->cfg.ext_f = ext &RVF ; + cpu->cfg.ext_d = ext &RVD ; + cpu->cfg.ext_v = ext &RVV ; + cpu->cfg.ext_c = ext &RVC ; + cpu->cfg.ext_s = ext &RVS ; + cpu->cfg.ext_u = ext &RVU ; + cpu->cfg.ext_h = ext &RVH ; + cpu->cfg.ext_j = ext &RVJ ; }#ifndef CONFIG_USER_ONLY
@@ -340,7 +372,6 @@ static void riscv_any_cpu_init(Object *obj)#endif env->priv_ver =PRIV_VERSION_LATEST ; -register_cpu_props (obj);/* inherited from parent obj via riscv_cpu_init() */ cpu->cfg.ext_ifencei = true;
@@ -368,7 +399,6 @@ static void rv64_sifive_u_cpu_init(Object *obj)RISCVCPU *cpu =RISCV_CPU (obj);CPURISCVState *env = &cpu->env;set_misa (env,MXL_RV64 ,RVI |RVM |RVA |RVF |RVD |RVC |RVS |RVU ); -register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_10_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV39 );
@@ -387,7 +417,6 @@ static void rv64_sifive_e_cpu_init(Object *obj)RISCVCPU *cpu =RISCV_CPU (obj);set_misa (env,MXL_RV64 ,RVI |RVM |RVA |RVC |RVU ); -register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_10_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );
@@ -472,8 +501,6 @@ static void rv32_sifive_u_cpu_init(Object *obj)RISCVCPU *cpu =RISCV_CPU (obj);CPURISCVState *env = &cpu->env;set_misa (env,MXL_RV32 ,RVI |RVM |RVA |RVF |RVD |RVC |RVS |RVU ); - -register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_10_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV32 );
@@ -492,7 +519,6 @@ static void rv32_sifive_e_cpu_init(Object *obj)RISCVCPU *cpu =RISCV_CPU (obj);set_misa (env,MXL_RV32 ,RVI |RVM |RVA |RVC |RVU ); -register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_10_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );
@@ -510,7 +536,6 @@ static void rv32_ibex_cpu_init(Object *obj)RISCVCPU *cpu =RISCV_CPU (obj);set_misa (env,MXL_RV32 ,RVI |RVM |RVC |RVU ); -register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_11_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );
@@ -529,7 +554,6 @@ static void rv32_imafcu_nommu_cpu_init(Object *obj)RISCVCPU *cpu =RISCV_CPU (obj);set_misa (env,MXL_RV32 ,RVI |RVM |RVA |RVF |RVC |RVU ); -register_cpu_props (obj); env->priv_ver =PRIV_VERSION_1_10_0 ;#ifndef CONFIG_USER_ONLY set_satp_mode_max_supported (cpu,VM_1_10_MBARE );
target/riscv/cpu.h +2 -2
@@ -66,8 +66,8 @@#define RV (x) ((target_ulong)1 << (x - 'A'))/* -* Consider updating register_cpu_props() when adding -* new MISA bits here. +* Consider updating set_misa() when adding new +* MISA bits here. */ #define RVI RV('I')#define RVE RV('E')/* E and I are mutually exclusive */
Signature · 1 line
2.39.2
PATCH v2 12/26 target/riscv/cpu.c: redesign register_cpu_props()
In reply to Daniel Henrique BarbozaNow that the function is a no-op if 'env.misa_ext != 0', and no one that are setting misa_ext != 0 is calling it because set_misa() is setting the cpu cfg accordingly, remove the now deprecated code and rename the function to register_generic_cpu_props().
This function is now doing exactly what the name says: it is creating user-facing properties to allow changes in the CPU cfg via the QEMU command line, setting default values if no user input is provided.
Note that there's the possibility of a CPU to set a certain misa value and, at the same, also want user-facing flags and defaults from this function. This is not the case since commit 26b2bc58599c ("target/riscv:
Don't expose the CPU properties on names CPUs"), but given that this is also a possibility, clarify in the function that using this function will overwrite existing values in cpu->cfg.
--- target/riscv/cpu.c | 48 ++++++++++------------------------------------ 1 file changed, 10 insertions(+), 38 deletions(-)
target/riscv/cpu.c +10 -38
@@ -221,7 +221,7 @@ static const char * const riscv_intr_names[] = { "reserved" }; -static void register_cpu_props (Object *obj); +static void register_generic_cpu_props (Object *obj);const char *riscv_cpu_get_trap_name (target_ulong cause,bool async) {
@@ -386,7 +386,7 @@ static void rv64_base_cpu_init(Object *obj)CPURISCVState *env = &RISCV_CPU (obj)->env;/* We set this in the realise function */ set_misa (env,MXL_RV64 ,0 ); -register_cpu_props (obj); +register_generic_cpu_props (obj);/* Set latest version of privileged specification */ env->priv_ver =PRIV_VERSION_LATEST ;#ifndef CONFIG_USER_ONLY
@@ -475,7 +475,7 @@ static void rv128_base_cpu_init(Object *obj)CPURISCVState *env = &RISCV_CPU (obj)->env;/* We set this in the realise function */ set_misa (env,MXL_RV128 ,0 ); -register_cpu_props (obj); +register_generic_cpu_props (obj);/* Set latest version of privileged specification */ env->priv_ver =PRIV_VERSION_LATEST ;#ifndef CONFIG_USER_ONLY
@@ -488,7 +488,7 @@ static void rv32_base_cpu_init(Object *obj)CPURISCVState *env = &RISCV_CPU (obj)->env;/* We set this in the realise function */ set_misa (env,MXL_RV32 ,0 ); -register_cpu_props (obj); +register_generic_cpu_props (obj);/* Set latest version of privileged specification */ env->priv_ver =PRIV_VERSION_LATEST ;#ifndef CONFIG_USER_ONLY
@@ -575,7 +575,7 @@ static void riscv_host_cpu_init(Object *obj)#elif defined(TARGET_RISCV64 )set_misa (env,MXL_RV64 ,0 );#endif - register_cpu_props(obj); + register_generic_cpu_props(obj); }#endif
@@ -1557,44 +1557,16 @@ static Property riscv_cpu_extensions[] = { };/* -* Register CPU props based on env.misa_ext. If a non-zero -* value was set, register only the required cpu->cfg.ext_* -* properties and leave. env.misa_ext = 0 means that we want -* all the default properties to be registered. +* Register generic CPU props with user-facing flags declared +* in riscv_cpu_extensions[]. +* +* Note that this will overwrite existing values in cpu->cfg. */ -static void register_cpu_props (Object *obj) +static void register_generic_cpu_props (Object *obj) { -RISCVCPU *cpu =RISCV_CPU (obj); -uint32_t misa_ext = cpu->env.misa_ext;Property *prop;DeviceState *dev =DEVICE (obj); -/* -* If misa_ext is not zero, set cfg properties now to -* allow them to be read during riscv_cpu_realize() -* later on. -*/ -if (cpu->env.misa_ext !=0 ) { - cpu->cfg.ext_i = misa_ext &RVI ; - cpu->cfg.ext_e = misa_ext &RVE ; - cpu->cfg.ext_m = misa_ext &RVM ; - cpu->cfg.ext_a = misa_ext &RVA ; - cpu->cfg.ext_f = misa_ext &RVF ; - cpu->cfg.ext_d = misa_ext &RVD ; - cpu->cfg.ext_v = misa_ext &RVV ; - cpu->cfg.ext_c = misa_ext &RVC ; - cpu->cfg.ext_s = misa_ext &RVS ; - cpu->cfg.ext_u = misa_ext &RVU ; - cpu->cfg.ext_h = misa_ext &RVH ; - cpu->cfg.ext_j = misa_ext &RVJ ; - -/* -* We don't want to set the default riscv_cpu_extensions -* in this case. -*/ -return ; - } -for (prop = riscv_cpu_extensions; prop && prop->name; prop++) {qdev_property_add_static (dev, prop); }
Signature · 1 line
2.39.2
PATCH v2 13/26 target/riscv: put env->misa_ext <-> cpu->cfg code into helpers
In reply to Daniel Henrique BarbozaThe extremely tedious code that sets cpu->cfg based on misa_ext, and vice-versa, is scattered around riscv_cpu_validate_set_extensions() and set_misa().
Introduce helpers to do this work, cleaning up the logic of both functions a bit. While we're at it, add a note in cpu.h informing that any future change in MISA RV* bits should also be reflected in the helpers as well.
We'll want to keep env->misa_ext changes in sync with cpu->cfg during realize() in the next patches, and both helpers will have a role to play in that.
--- target/riscv/cpu.c | 120 ++++++++++++++++++++++++--------------------- target/riscv/cpu.h | 3 +- 2 files changed, 65 insertions(+), 58 deletions(-)
target/riscv/cpu.c +63 -57
@@ -234,10 +234,69 @@ const char *riscv_cpu_get_trap_name(target_ulong cause, bool async) } } -static void set_misa (CPURISCVState *env,RISCVMXL mxl,uint32_t ext) +static uint32_t riscv_get_misa_ext_with_cpucfg (RISCVCPUConfig *cfg) { -RISCVCPU *cpu; +uint32_t ext =0 ; +if (cfg->ext_i) { + ext |=RVI ; + } +if (cfg->ext_e) { + ext |=RVE ; + } +if (cfg->ext_m) { + ext |=RVM ; + } +if (cfg->ext_a) { + ext |=RVA ; + } +if (cfg->ext_f) { + ext |=RVF ; + } +if (cfg->ext_d) { + ext |=RVD ; + } +if (cfg->ext_c) { + ext |=RVC ; + } +if (cfg->ext_s) { + ext |=RVS ; + } +if (cfg->ext_u) { + ext |=RVU ; + } +if (cfg->ext_h) { + ext |=RVH ; + } +if (cfg->ext_v) { + ext |=RVV ; + } +if (cfg->ext_j) { + ext |=RVJ ; + } + +return ext; +} + +static void riscv_set_cpucfg_with_misa (RISCVCPUConfig *cfg, +uint32_t misa_ext) +{ + cfg->ext_i = misa_ext &RVI ; + cfg->ext_e = misa_ext &RVE ; + cfg->ext_m = misa_ext &RVM ; + cfg->ext_a = misa_ext &RVA ; + cfg->ext_f = misa_ext &RVF ; + cfg->ext_d = misa_ext &RVD ; + cfg->ext_v = misa_ext &RVV ; + cfg->ext_c = misa_ext &RVC ; + cfg->ext_s = misa_ext &RVS ; + cfg->ext_u = misa_ext &RVU ; + cfg->ext_h = misa_ext &RVH ; + cfg->ext_j = misa_ext &RVJ ; +} + +static void set_misa (CPURISCVState *env,RISCVMXL mxl,uint32_t ext) +{ env->misa_mxl_max = env->misa_mxl = mxl; env->misa_ext_mask = env->misa_ext = ext;
@@ -251,25 +310,7 @@ static void set_misa(CPURISCVState *env, RISCVMXL mxl, uint32_t ext)return ; } -/* -* We can't use riscv_cpu_cfg() in this case because it is -* a read-only inline and we're going to change the values -* of cpu->cfg. -*/ - cpu =env_archcpu (env); - - cpu->cfg.ext_i = ext &RVI ; - cpu->cfg.ext_e = ext &RVE ; - cpu->cfg.ext_m = ext &RVM ; - cpu->cfg.ext_a = ext &RVA ; - cpu->cfg.ext_f = ext &RVF ; - cpu->cfg.ext_d = ext &RVD ; - cpu->cfg.ext_v = ext &RVV ; - cpu->cfg.ext_c = ext &RVC ; - cpu->cfg.ext_s = ext &RVS ; - cpu->cfg.ext_u = ext &RVU ; - cpu->cfg.ext_h = ext &RVH ; - cpu->cfg.ext_j = ext &RVJ ; +riscv_set_cpucfg_with_misa (&env_archcpu (env)->cfg, ext); }#ifndef CONFIG_USER_ONLY
@@ -1156,42 +1197,7 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)*/ riscv_cpu_disable_priv_spec_isa_exts (cpu); -if (cpu->cfg.ext_i) { - ext |=RVI ; - } -if (cpu->cfg.ext_e) { - ext |=RVE ; - } -if (cpu->cfg.ext_m) { - ext |=RVM ; - } -if (cpu->cfg.ext_a) { - ext |=RVA ; - } -if (cpu->cfg.ext_f) { - ext |=RVF ; - } -if (cpu->cfg.ext_d) { - ext |=RVD ; - } -if (cpu->cfg.ext_c) { - ext |=RVC ; - } -if (cpu->cfg.ext_s) { - ext |=RVS ; - } -if (cpu->cfg.ext_u) { - ext |=RVU ; - } -if (cpu->cfg.ext_h) { - ext |=RVH ; - } -if (cpu->cfg.ext_v) { - ext |=RVV ; - } -if (cpu->cfg.ext_j) { - ext |=RVJ ; - } + ext =riscv_get_misa_ext_with_cpucfg (&cpu->cfg); env->misa_ext_mask = env->misa_ext = ext; }
target/riscv/cpu.h +2 -1
@@ -66,7 +66,8 @@#define RV (x) ((target_ulong)1 << (x - 'A'))/* -* Consider updating set_misa() when adding new +* Consider updating riscv_get_misa_ext_with_cpucfg() +* and riscv_set_cpucfg_with_misa() when adding new * MISA bits here. */ #define RVI RV('I')
Signature · 1 line
2.39.2
PATCH v2 14/26 target/riscv: add RVG
In reply to Daniel Henrique BarbozaThe 'G' bit in misa_ext is a virtual extension that enables a set of extensions (i, m, a, f, d, icsr and ifencei). We'll want to avoid setting it for write_misa(). Add it so we can gate write_misa() properly against it.
--- target/riscv/cpu.c | 4 ++++ target/riscv/cpu.h | 1 + 2 files changed, 5 insertions(+)
target/riscv/cpu.c +4 -0
@@ -274,6 +274,9 @@ static uint32_t riscv_get_misa_ext_with_cpucfg(RISCVCPUConfig *cfg)if (cfg->ext_j) { ext |=RVJ ; } +if (cfg->ext_g) { + ext |=RVG ; + }return ext; }
@@ -293,6 +296,7 @@ static void riscv_set_cpucfg_with_misa(RISCVCPUConfig *cfg, cfg->ext_u = misa_ext &RVU ; cfg->ext_h = misa_ext &RVH ; cfg->ext_j = misa_ext &RVJ ; + cfg->ext_g = misa_ext &RVG ; }static void set_misa (CPURISCVState *env,RISCVMXL mxl,uint32_t ext)
target/riscv/cpu.h +1 -0
@@ -82,6 +82,7 @@#define RVU RV('U')#define RVH RV('H')#define RVJ RV('J') +#define RVG RV('G')/* Privileged specification version */
Signature · 1 line
2.39.2
PATCH v2 15/26 target/riscv: do not allow RVG in write_misa()
In reply to Daniel Henrique BarbozaWe're getting ready to use riscv_cpu_validate_set_extensions() to unify the handling of write_misa() with the rest of the code base. But first we need to deal with RVG.
The 'G' virtual extension enables a set of extensions in the CPU. At this moment, this is done at the start of our validation step in riscv_cpu_validate_set_extensions(). This means that enabling G will enable other extensions in the CPU before resuming the validation.
This also means that, in case a write_misa() validation fails, we're going to set cpu->cfg attributes that are unrelated to misa_ext bits (icsr and ifencei). These would be 2 extra states that we would need to store to fallback from a validation failure.
Since write_misa() is still on experimental state let's make our lives easier for now and disable RVG updates.
--- target/riscv/csr.c | 5 +++++ 1 file changed, 5 insertions(+)
target/riscv/csr.c +5 -0
@@ -1348,6 +1348,11 @@ static RISCVException write_misa(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +/* Changing 'G' state is unsupported */ +if (val &RVG ) { +return RISCV_EXCP_NONE ; + } +/* 'I' or 'E' must be present */ if (!(val & (RVI |RVE ))) {/* It is not, drop write to misa */
Signature · 1 line
2.39.2
Re: [PATCH for-8.1 v2 15/26] target/riscv: do not allow RVG in write_misa()
Quoted 27 lines · Daniel Henrique Barboza
We're getting ready to use
riscv_cpu_validate_set_extensions()to unify the handling ofwrite_misa()with the rest of the code base. But first we need to deal with RVG.The 'G' virtual extension enables a set of extensions in the CPU. At this moment, this is done at the start of our validation step in
riscv_cpu_validate_set_extensions(). This means that enabling G will enable other extensions in the CPU before resuming the validation.This also means that, in case a
write_misa()validation fails, we're going to setcpu->cfgattributes that are unrelated tomisa_extbits (icsr and ifencei). These would be 2 extra states that we would need to store to fallback from a validation failure.Since
write_misa()is still on experimental state let's make our lives easier for now and disable RVG updates.Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/csr.c | 5 +++++ 1 file changed, 5 insertions(+)target/riscv/csr.c +0
-0
target/riscv/csr.c static RISCVException write_misa(CPURISCVState *env, int csrno, · line 1355
Quoted 8 lines
target/riscv/csr.c +5
-0@@ -1348,6 +1348,11 @@ static RISCVException write_misa(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +/* Changing 'G' state is unsupported */ +if (val &RVG ) { +return RISCV_EXCP_NONE ; + } +
'val & G' is not equal "Changing 'G'" .
Regards,
Weiwei Li
Quoted 3 lines
target/riscv/csr.c +0
-0/* 'I' or 'E' must be present */ if (!(val & (RVI |RVE ))) {/* It is not, drop write to misa */
PATCH v2 16/26 target/riscv/cpu.c: split RVG code from validate_set_extensions()
In reply to Daniel Henrique BarbozaWe can set all RVG related extensions during realize time, before validate_set_extensions() itself. It will also avoid re-enabling RVG via write_misa() when the CSR start to using the same validation code realize() does.
Note that we're setting both cfg->ext_N and env->misa_ext bits, instead of just setting cfg->ext_N. The intention here is to start syncing all misa_ext operations with its cpu->cfg flags, in preparation to allow for the validate function to operate using a misa_ext. This doesn't make any difference for the current code state, but will be a requirement for write_misa() later on.
--- target/riscv/cpu.c | 55 +++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 15 deletions(-)
target/riscv/cpu.c +40 -15
@@ -281,6 +281,42 @@ static uint32_t riscv_get_misa_ext_with_cpucfg(RISCVCPUConfig *cfg)return ext; } +static void riscv_set_G_virt_ext (RISCVCPU *cpu) +{ +CPURISCVState *env = &cpu->env; +RISCVCPUConfig *cfg = &cpu->cfg; + +if (!(cfg->ext_i && cfg->ext_m && cfg->ext_a && + cfg->ext_f && cfg->ext_d && + cfg->ext_icsr && cfg->ext_ifencei)) { + +warn_report ("Setting G will also set IMAFD_Zicsr_Zifencei" ); + cfg->ext_i = true; + env->misa_ext |=RVI ; + + cfg->ext_m = true; + env->misa_ext |=RVM ; + + cfg->ext_a = true; + env->misa_ext |=RVA ; + + cfg->ext_f = true; + env->misa_ext |=RVF ; + + cfg->ext_d = true; + env->misa_ext |=RVD ; + + cfg->ext_icsr = true; + cfg->ext_ifencei = true; + +/* +* Update misa_ext_mask since this is called +* only during riscv_cpu_realize(). +*/ + env->misa_ext_mask = env->misa_ext; + } +} +static void riscv_set_cpucfg_with_misa (RISCVCPUConfig *cfg,uint32_t misa_ext) {
@@ -1036,21 +1072,6 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)return ; } -/* Do some ISA extension error checking */ -if (cpu->cfg.ext_g && !(cpu->cfg.ext_i && cpu->cfg.ext_m && - cpu->cfg.ext_a && cpu->cfg.ext_f && - cpu->cfg.ext_d && - cpu->cfg.ext_icsr && cpu->cfg.ext_ifencei)) { -warn_report ("Setting G will also set IMAFD_Zicsr_Zifencei" ); - cpu->cfg.ext_i = true; - cpu->cfg.ext_m = true; - cpu->cfg.ext_a = true; - cpu->cfg.ext_f = true; - cpu->cfg.ext_d = true; - cpu->cfg.ext_icsr = true; - cpu->cfg.ext_ifencei = true; - } -if (cpu->cfg.ext_i && cpu->cfg.ext_e) {error_setg (errp,"I and E extensions are incompatible" );
@@ -1313,6 +1334,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } +if (cpu->cfg.ext_g) { +riscv_set_G_virt_ext (cpu); + } +riscv_cpu_validate_set_extensions (cpu, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);
Signature · 1 line
2.39.2
Re: [PATCH for-8.1 v2 16/26] target/riscv/cpu.c: split RVG code from validate_set_extensions()
Quoted 49 lines · Daniel Henrique Barboza
We can set all RVG related extensions during realize time, before
validate_set_extensions()itself. It will also avoid re-enabling RVG viawrite_misa()when the CSR start to using the same validation coderealize()does.Note that we're setting both
cfg->ext_Nandenv->misa_extbits, instead of just settingcfg->ext_N. The intention here is to start syncing allmisa_extoperations with itscpu->cfgflags, in preparation to allow for the validate function to operate using amisa_ext. This doesn't make any difference for the current code state, but will be a requirement forwrite_misa()later on.Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/cpu.c | 55 +++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 15 deletions(-)target/riscv/cpu.c +24
-0@@ -281,6 +281,42 @@ static uint32_t riscv_get_misa_ext_with_cpucfg(RISCVCPUConfig *cfg)return ext; } +static void riscv_set_G_virt_ext (RISCVCPU *cpu) +{ +CPURISCVState *env = &cpu->env; +RISCVCPUConfig *cfg = &cpu->cfg; + +if (!(cfg->ext_i && cfg->ext_m && cfg->ext_a && + cfg->ext_f && cfg->ext_d && + cfg->ext_icsr && cfg->ext_ifencei)) { + +warn_report ("Setting G will also set IMAFD_Zicsr_Zifencei" ); + cfg->ext_i = true; + env->misa_ext |=RVI ; + + cfg->ext_m = true; + env->misa_ext |=RVM ; + + cfg->ext_a = true; + env->misa_ext |=RVA ; + + cfg->ext_f = true; + env->misa_ext |=RVF ; + + cfg->ext_d = true; + env->misa_ext |=RVD ;
target/riscv/cpu.c static uint32_t riscv_get_misa_ext_with_cpucfg(RISCVCPUConfig *cfg) · line 317
Quoted 10 lines
target/riscv/cpu.c +10
-0+ + cfg->ext_icsr = true; + cfg->ext_ifencei = true; + +/* +* Update misa_ext_mask since this is called +* only during riscv_cpu_realize(). +*/ + env->misa_ext_mask = env->misa_ext; + }
Another two question:
- whether we should set 'G' when all these extensions are supported?
- whether 'G'should be disabled if some of the extensions are disabled by write_misa?
Regards,
Weiwei Li
Quoted 38 lines
target/riscv/cpu.c +6
-15+} +static void riscv_set_cpucfg_with_misa (RISCVCPUConfig *cfg,uint32_t misa_ext) {@@ -1036,21 +1072,6 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)return ; } -/* Do some ISA extension error checking */ -if (cpu->cfg.ext_g && !(cpu->cfg.ext_i && cpu->cfg.ext_m && - cpu->cfg.ext_a && cpu->cfg.ext_f && - cpu->cfg.ext_d && - cpu->cfg.ext_icsr && cpu->cfg.ext_ifencei)) { -warn_report ("Setting G will also set IMAFD_Zicsr_Zifencei" ); - cpu->cfg.ext_i = true; - cpu->cfg.ext_m = true; - cpu->cfg.ext_a = true; - cpu->cfg.ext_f = true; - cpu->cfg.ext_d = true; - cpu->cfg.ext_icsr = true; - cpu->cfg.ext_ifencei = true; - } -if (cpu->cfg.ext_i && cpu->cfg.ext_e) {error_setg (errp,"I and E extensions are incompatible" );@@ -1313,6 +1334,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } +if (cpu->cfg.ext_g) { +riscv_set_G_virt_ext (cpu); + } +riscv_cpu_validate_set_extensions (cpu, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);
Re: [PATCH for-8.1 v2 16/26] target/riscv/cpu.c: split RVG code from validate_set_extensions()
Quoted 63 lines · liweiwei
Quoted 48 lines · Daniel Henrique Barboza
We can set all RVG related extensions during realize time, before
validate_set_extensions()itself. It will also avoid re-enabling RVG viawrite_misa()when the CSR start to using the same validation coderealize()does.Note that we're setting both
cfg->ext_Nandenv->misa_extbits, instead of just settingcfg->ext_N. The intention here is to start syncing allmisa_extoperations with itscpu->cfgflags, in preparation to allow for the validate function to operate using amisa_ext. This doesn't make any difference for the current code state, but will be a requirement forwrite_misa()later on.Signed-off-by Daniel Henrique Barboza <[email protected]>---
target/riscv/cpu.c| 55 +++++++++++++++++++++++++++++++++-------------1 file changed, 40 insertions(+), 15 deletions(-)target/riscv/cpu.c +24
-0@@ -281,6 +281,42 @@ static uint32_t riscv_get_misa_ext_with_cpucfg(RISCVCPUConfig *cfg) return ext; } +static void riscv_set_G_virt_ext (RISCVCPU *cpu) +{ + CPURISCVState *env = &cpu->env; + RISCVCPUConfig *cfg = &cpu->cfg; + + if (!(cfg->ext_i && cfg->ext_m && cfg->ext_a && + cfg->ext_f && cfg->ext_d && + cfg->ext_icsr && cfg->ext_ifencei)) { + + warn_report("Setting G will also set IMAFD_Zicsr_Zifencei"); + cfg->ext_i = true; + env->misa_ext |= RVI; + + cfg->ext_m = true; + env->misa_ext |= RVM; + + cfg->ext_a = true; + env->misa_ext |= RVA; + + cfg->ext_f = true; + env->misa_ext |= RVF; + + cfg->ext_d = true; + env->misa_ext |= RVD;target/riscv/cpu.c static uint32_t riscv_get_misa_ext_with_cpucfg(RISCVCPUConfig *cfg) · line 316
Quoted 10 lines
target/riscv/cpu.c +10
-0+ + cfg->ext_icsr = true; + cfg->ext_ifencei = true; + + /* + * Update misa_ext_mask since this is called + * only during riscv_cpu_realize(). + */ + env->misa_ext_mask = env->misa_ext; + }Another two question:
Quoted 1 line
- whether we should set 'G' when all these extensions are supported?
As far as I can tell, no. RVG is being treated as a shortcut to enabled this set of extensions, but it doesn't mean that if the user happens to chose them manually we should enable RVG.
Quoted 2 lines
- whether 'G'should be disabled if some of the extensions are disabled by
write_misa?
Good point. Yes, we would need to disable RVG if RVG is enabled in the hart but then another letter extension to the group (I,M,A F or D) is disabled. Which is something that isn't being handled now.
A simple solution is, in patch 15, forbid IMAFD to be disabled if RVG is already set.
In fact, this kind of logic is something that we would need to do for the future profile extension, so in a way RVG is being handled almost like a profile now.
Thanks,
Daniel
Quoted 42 lines
Regards,
Weiwei Li
Quoted 36 lines
target/riscv/cpu.c +6
-15+} + static void riscv_set_cpucfg_with_misa(RISCVCPUConfig *cfg, uint32_t misa_ext) {@@ -1036,21 +1072,6 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) return; } - /* Do some ISA extension error checking */ - if (cpu->cfg.ext_g && !(cpu->cfg.ext_i && cpu->cfg.ext_m && - cpu->cfg.ext_a && cpu->cfg.ext_f && - cpu->cfg.ext_d && - cpu->cfg.ext_icsr && cpu->cfg.ext_ifencei)) { - warn_report("Setting G will also set IMAFD_Zicsr_Zifencei"); - cpu->cfg.ext_i = true; - cpu->cfg.ext_m = true; - cpu->cfg.ext_a = true; - cpu->cfg.ext_f = true; - cpu->cfg.ext_d = true; - cpu->cfg.ext_icsr = true; - cpu->cfg.ext_ifencei = true; - } - if (cpu->cfg.ext_i && cpu->cfg.ext_e) { error_setg(errp, "I and E extensions are incompatible");@@ -1313,6 +1334,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp) return; } + if (cpu->cfg.ext_g) { + riscv_set_G_virt_ext(cpu); + } + riscv_cpu_validate_set_extensions(cpu, &local_err); if (local_err != NULL) { error_propagate(errp, local_err);
PATCH v2 17/26 target/riscv: write env->misa_ext* in register_generic_cpu_props()
In reply to Daniel Henrique BarbozaIn the process of creating the user-facing flags in register_generic_cpu_props() we're also setting default values for the cpu->cfg flags that represents MISA bits.
Leaving it as is will cause a discrepancy between users of this function (at this moment the non-named CPUs) and named CPUs. Named CPUs are using set_misa() with a non-zero 'ext' value, writing cpu->cfg in the process. They'll reach riscv_cpu_realize() in a state where env->misa_ext will reflect cpu->cfg, allowing functions to choose whether to use env->misa_ext or cpu->cfg to validate MISA bits.
If we guarantee that env->misa_ext will always reflect cpu->cfg at the start of riscv_cpu_realize(), functions will be able to no longer rely on cpu->cfg for MISA validation. This happens to be one blocker we have to properly support write_misa().
Sync env->misa_ext* in register_generic_cpu_props(). This will leave only a single place where there's a cpu->cfg change that needs to be converted back to env->misa_ext*: right after disabling priv spec extensions, at the end of riscv_cpu_validate_set_extensions(). We'll deal with it shortly.
--- target/riscv/cpu.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
target/riscv/cpu.c +7 -1
@@ -1595,10 +1595,12 @@ static Property riscv_cpu_extensions[] = {* Register generic CPU props with user-facing flags declared * in riscv_cpu_extensions[]. * -* Note that this will overwrite existing values in cpu->cfg. +* Note that this will overwrite existing values in cpu->cfg +* and MISA. */ static void register_generic_cpu_props (Object *obj) { +RISCVCPU *cpu =RISCV_CPU (obj);Property *prop;DeviceState *dev =DEVICE (obj);
@@ -1609,6 +1611,10 @@ static void register_generic_cpu_props(Object *obj)#ifndef CONFIG_USER_ONLY riscv_add_satp_mode_properties (obj);#endif + +/* Keep env->misa_ext and misa_ext_mask updated */ + cpu->env.misa_ext =riscv_get_misa_ext_with_cpucfg (&cpu->cfg); + cpu->env.misa_ext_mask = cpu->env.misa_ext; }static Property riscv_cpu_properties[] = {
Signature · 1 line
2.39.2
PATCH v2 18/26 target/risc/cpu.c: add riscv_cpu_validate_misa_ext()
In reply to Daniel Henrique BarbozaEven after taking RVG off from riscv_cpu_validate_set_extensions(), the function is still doing too much. It is validating misa bits, then validating named extensions, and if the validation succeeds it's doing more changes in both cpu->cfg and MISA bits.
It works for the support we have today, since we'll error out during realize() time. This is not enough to support write_misa() though - we don't want to error out if userspace writes something odd in the CSR.
This patch starts splitting riscv_cpu_validate_set_extensions() into a three step process: validate misa_ext, validate cpu->cfg, then commit the configuration. This separation will allow us to use these functions in write_misa() without having to worry about saving CPU state during runtime because the function changed state but failed to validate.
riscv_cpu_validate_misa_ext() will host all validations related to misa bits only. Validations using misa bits + name extensions will remain in validate_set_extensions().
--- target/riscv/cpu.c | 77 ++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 34 deletions(-)
target/riscv/cpu.c +43 -34
@@ -1025,6 +1025,43 @@ static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu) } } +static void riscv_cpu_validate_misa_ext (RISCVCPU *cpu,Error **errp) +{ +if (cpu->cfg.ext_i && cpu->cfg.ext_e) { +error_setg (errp, +"I and E extensions are incompatible" ); +return ; + } + +if (!cpu->cfg.ext_i && !cpu->cfg.ext_e) { +error_setg (errp, +"Either I or E extension must be set" ); +return ; + } + +if (cpu->cfg.ext_s && !cpu->cfg.ext_u) { +error_setg (errp, +"Setting S extension without U extension is illegal" ); +return ; + } + +if (cpu->cfg.ext_h && !cpu->cfg.ext_i) { +error_setg (errp, +"H depends on an I base integer ISA with 32 x registers" ); +return ; + } + +if (cpu->cfg.ext_h && !cpu->cfg.ext_s) { +error_setg (errp,"H extension implicitly requires S-mode" ); +return ; + } + +if (cpu->cfg.ext_d && !cpu->cfg.ext_f) { +error_setg (errp,"D extension requires F extension" ); +return ; + } +} +static void riscv_cpu_validate_misa_mxl (RISCVCPU *cpu,Error **errp) {RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (cpu);
@@ -1072,35 +1109,6 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)return ; } -if (cpu->cfg.ext_i && cpu->cfg.ext_e) { -error_setg (errp, -"I and E extensions are incompatible" ); -return ; - } - -if (!cpu->cfg.ext_i && !cpu->cfg.ext_e) { -error_setg (errp, -"Either I or E extension must be set" ); -return ; - } - -if (cpu->cfg.ext_s && !cpu->cfg.ext_u) { -error_setg (errp, -"Setting S extension without U extension is illegal" ); -return ; - } - -if (cpu->cfg.ext_h && !cpu->cfg.ext_i) { -error_setg (errp, -"H depends on an I base integer ISA with 32 x registers" ); -return ; - } - -if (cpu->cfg.ext_h && !cpu->cfg.ext_s) { -error_setg (errp,"H extension implicitly requires S-mode" ); -return ; - } -if (cpu->cfg.ext_f && !cpu->cfg.ext_icsr) {error_setg (errp,"F extension requires Zicsr" );return ;
@@ -1120,11 +1128,6 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)return ; } -if (cpu->cfg.ext_d && !cpu->cfg.ext_f) { -error_setg (errp,"D extension requires F extension" ); -return ; - } -/* The V vector extension depends on the Zve64d extension */ if (cpu->cfg.ext_v) { cpu->cfg.ext_zve64d = true;
@@ -1338,6 +1341,12 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)riscv_set_G_virt_ext (cpu); } +riscv_cpu_validate_misa_ext (cpu, &local_err); +if (local_err !=NULL ) { +error_propagate (errp, local_err); +return ; + } +riscv_cpu_validate_set_extensions (cpu, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);
Signature · 1 line
2.39.2
PATCH v2 19/26 target/riscv/cpu:c add misa_ext V-> D & F dependency
In reply to Daniel Henrique BarbozaWe have a chained dependency in riscv_cpu_validate_set_extensions() related to RVV. If RVV is set, we enable other extensions such as Zve64d, Zve64f and Zve32f, and these depends on misa bits RVD and RVF. Thus, we're making RVV depend on RVD and RVF.
Let's add this dependency in riscv_cpu_validate_misa_ext() to fail earlier.
--- target/riscv/cpu.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
target/riscv/cpu.c +14 -0
@@ -1060,6 +1060,20 @@ static void riscv_cpu_validate_misa_ext(RISCVCPU *cpu, Error **errp)error_setg (errp,"D extension requires F extension" );return ; } + +if (cpu->cfg.ext_v) { +/* +* V depends on Zve64d, which requires D. It also +* depends on Zve64f, which depends on Zve32f, +* which requires F. +* +* This means that V depends on both D and F. +*/ +if (!(cpu->cfg.ext_d && cpu->cfg.ext_f)) { +error_setg (errp,"V extension requires D and F extensions" ); +return ; + } + } }static void riscv_cpu_validate_misa_mxl (RISCVCPU *cpu,Error **errp)
Signature · 1 line
2.39.2
Re: [PATCH for-8.1 v2 19/26] target/riscv/cpu:c add misa_ext V-> D & F dependency
Quoted 26 lines · Daniel Henrique Barboza
We have a chained dependency in
riscv_cpu_validate_set_extensions()related to RVV. If RVV is set, we enable other extensions such as Zve64d, Zve64f and Zve32f, and these depends on misa bits RVD and RVF. Thus, we're making RVV depend on RVD and RVF.Let's add this dependency in
riscv_cpu_validate_misa_ext()to fail earlier.Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/cpu.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)target/riscv/cpu.c +5
-0@@ -1060,6 +1060,20 @@ static void riscv_cpu_validate_misa_ext(RISCVCPU *cpu, Error **errp)error_setg (errp,"D extension requires F extension" );return ; } + +if (cpu->cfg.ext_v) { +/* +* V depends on Zve64d, which requires D. It also +* depends on Zve64f, which depends on Zve32f,
target/riscv/cpu.c static void riscv_cpu_validate_misa_ext(RISCVCPU *cpu, Error **errp) · line 1077
Quoted 10 lines
target/riscv/cpu.c +9
-0+* which requires F. +* +* This means that V depends on both D and F. +*/ +if (!(cpu->cfg.ext_d && cpu->cfg.ext_f)) { +error_setg (errp,"V extension requires D and F extensions" ); +return ; + } + } }
It seems not necessary to add this check here, since "zve64d requires D" will be checked later.
By the way, "D requires F" is checked before, so check on F is redundant here.
Regards,
Weiwei Li
Quoted 2 lines
target/riscv/cpu.c +0
-0static void riscv_cpu_validate_misa_mxl (RISCVCPU *cpu,Error **errp)
PATCH v2 20/26 target/riscv: move riscv_cpu_validate_v() to validate_misa_ext()
In reply to Daniel Henrique Barbozariscv_cpu_validate_v() consists of checking RVV related attributes, such as vlen and elen, and setting env->vext_spec.
This can be done during riscv_cpu_validate_misa_ext() time, allowing us to fail earlier if RVV constrains are not met.
--- target/riscv/cpu.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
target/riscv/cpu.c +9 -9
@@ -1027,6 +1027,9 @@ static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu)static void riscv_cpu_validate_misa_ext (RISCVCPU *cpu,Error **errp) { +CPURISCVState *env = &cpu->env; +Error *local_err =NULL ; +if (cpu->cfg.ext_i && cpu->cfg.ext_e) {error_setg (errp,"I and E extensions are incompatible" );
@@ -1073,6 +1076,12 @@ static void riscv_cpu_validate_misa_ext(RISCVCPU *cpu, Error **errp)error_setg (errp,"V extension requires D and F extensions" );return ; } + +riscv_cpu_validate_v (env, &cpu->cfg, &local_err); +if (local_err !=NULL ) { +error_propagate (errp, local_err); +return ; + } } }
@@ -1111,7 +1120,6 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)static void riscv_cpu_validate_set_extensions (RISCVCPU *cpu,Error **errp) {CPURISCVState *env = &cpu->env; -Error *local_err =NULL ;uint32_t ext =0 ;if (cpu->cfg.epmp && !cpu->cfg.pmp) {
@@ -1202,14 +1210,6 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) } } -if (cpu->cfg.ext_v) { -riscv_cpu_validate_v (env, &cpu->cfg, &local_err); -if (local_err !=NULL ) { -error_propagate (errp, local_err); -return ; - } - } -if (cpu->cfg.ext_zk) { cpu->cfg.ext_zkn = true; cpu->cfg.ext_zkr = true;
Signature · 1 line
2.39.2
PATCH v2 21/26 target/riscv: validate_misa_ext() now validates a misa_ext val
In reply to Daniel Henrique BarbozaWe have all MISA specific validations in riscv_cpu_validate_misa_ext(), and we have a guarantee that env->misa_ext will always be in sync with cpu->cfg at this point during realize time, so let's convert it to use a 'misa_ext' parameter instead of reading cpu->cfg.
This will prepare the function to be used in write_misa() where we won't have an updated cpu->cfg object to work with. riscv_cpu_validate_v() is changed to receive a const pointer to the cpu->cfg object via riscv_cpu_cfg().
--- target/riscv/cpu.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-)
target/riscv/cpu.c +17 -13
@@ -939,7 +939,8 @@ static void riscv_cpu_disas_set_info(CPUState *s, disassemble_info *info) } } -static void riscv_cpu_validate_v (CPURISCVState *env,RISCVCPUConfig *cfg, +static void riscv_cpu_validate_v (CPURISCVState *env, +const RISCVCPUConfig *cfg,Error **errp) {int vext_version =VEXT_VERSION_1_00_0 ;
@@ -1025,46 +1026,48 @@ static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu) } } -static void riscv_cpu_validate_misa_ext (RISCVCPU *cpu,Error **errp) + +static void riscv_cpu_validate_misa_ext (CPURISCVState *env, +uint32_t misa_ext, +Error **errp) { -CPURISCVState *env = &cpu->env;Error *local_err =NULL ; -if (cpu->cfg.ext_i && cpu->cfg.ext_e) { +if (misa_ext &RVI && misa_ext &RVE ) {error_setg (errp,"I and E extensions are incompatible" );return ; } -if (!cpu->cfg.ext_i && !cpu->cfg.ext_e) { +if (!(misa_ext &RVI ) && !(misa_ext &RVE )) {error_setg (errp,"Either I or E extension must be set" );return ; } -if (cpu->cfg.ext_s && !cpu->cfg.ext_u) { +if (misa_ext &RVS && !(misa_ext &RVU )) {error_setg (errp,"Setting S extension without U extension is illegal" );return ; } -if (cpu->cfg.ext_h && !cpu->cfg.ext_i) { +if (misa_ext &RVH && !(misa_ext &RVI )) {error_setg (errp,"H depends on an I base integer ISA with 32 x registers" );return ; } -if (cpu->cfg.ext_h && !cpu->cfg.ext_s) { +if (misa_ext &RVH && !(misa_ext &RVS )) {error_setg (errp,"H extension implicitly requires S-mode" );return ; } -if (cpu->cfg.ext_d && !cpu->cfg.ext_f) { +if (misa_ext &RVD && !(misa_ext &RVF )) {error_setg (errp,"D extension requires F extension" );return ; } -if (cpu->cfg.ext_v) { +if (misa_ext &RVV ) {/* * V depends on Zve64d, which requires D. It also * depends on Zve64f, which depends on Zve32f,
@@ -1072,12 +1075,12 @@ static void riscv_cpu_validate_misa_ext(RISCVCPU *cpu, Error **errp)* * This means that V depends on both D and F. */ -if (!(cpu->cfg.ext_d && cpu->cfg.ext_f)) { +if (!(misa_ext &RVD && misa_ext &RVF )) {error_setg (errp,"V extension requires D and F extensions" );return ; } -riscv_cpu_validate_v (env, &cpu->cfg, &local_err); +riscv_cpu_validate_v (env,riscv_cpu_cfg (env), &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);return ;
@@ -1331,6 +1334,7 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)CPUState *cs =CPU (dev);RISCVCPU *cpu =RISCV_CPU (dev);RISCVCPUClass *mcc =RISCV_CPU_GET_CLASS (dev); +CPURISCVState *env = &cpu->env;Error *local_err =NULL ;cpu_exec_realizefn (cs, &local_err);
@@ -1355,7 +1359,7 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)riscv_set_G_virt_ext (cpu); } -riscv_cpu_validate_misa_ext (cpu, &local_err); +riscv_cpu_validate_misa_ext (env, env->misa_ext, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);return ;
Signature · 1 line
2.39.2
PATCH v2 22/26 target/riscv: error out on priv failure for RVH
In reply to Daniel Henrique BarbozaWe have one last case where we're changing env->misa_ext* during validation. riscv_cpu_disable_priv_spec_isa_exts(), at the end of riscv_cpu_validate_set_extensions(), will disable cpu->cfg.ext_h and cpu->cfg.ext_v if priv_ver check fails.
This check can be done in riscv_cpu_validate_misa_ext(). The difference here is that we're not silently disable it: we'll error out. Silently disabling a MISA extension after all the validation is completed can can cause inconsistencies that we don't have to deal with. Verify ealier and fail faster.
Note that we're ignoring RVV priv_ver validation since its minimal priv is also the minimal value we support. RVH will error out if enabled under priv_ver under 1_12_0.
As a bonus, we're guaranteeing that all env->misa_ext* changes will happen up until riscv_set_G_virt_ext(). We don't have to worry about keeping env->misa_ext in sync with cpu->cfg.
--- target/riscv/cpu.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
target/riscv/cpu.c +19 -9
@@ -1067,6 +1067,20 @@ static void riscv_cpu_validate_misa_ext(CPURISCVState *env,return ; } +/* +* Check for priv spec version. RVH is 1_12_0, RVV is 1_10_0. +* We don't support anything under 1_10_0 so skip checking +* priv for RVV. +* +* We're hardcoding it here to avoid looping into the +* 50+ entries of isa_edata_arr[] just to check the RVH +* entry. +*/ +if (misa_ext &RVH && env->priv_ver <PRIV_VERSION_1_12_0 ) { +error_setg (errp,"H extension requires priv spec 1.12.0" ); +return ; + } +if (misa_ext &RVV ) {/* * V depends on Zve64d, which requires D. It also
@@ -1117,14 +1131,10 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)/* * Check consistency between chosen extensions while setting -* cpu->cfg accordingly, setting env->misa_ext and -* misa_ext_mask in the end. +* cpu->cfg accordingly. */ static void riscv_cpu_validate_set_extensions (RISCVCPU *cpu,Error **errp) { -CPURISCVState *env = &cpu->env; -uint32_t ext =0 ; -if (cpu->cfg.epmp && !cpu->cfg.pmp) {/* * Enhanced PMP should only be available
@@ -1241,10 +1251,6 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)* validated and set everything we need. */ riscv_cpu_disable_priv_spec_isa_exts (cpu); - - ext =riscv_get_misa_ext_with_cpucfg (&cpu->cfg); - - env->misa_ext_mask = env->misa_ext = ext; }#ifndef CONFIG_USER_ONLY
@@ -1355,6 +1361,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } +/* +* This is the last point where env->misa_ext* can +* be changed. +*/ if (cpu->cfg.ext_g) {riscv_set_G_virt_ext (cpu); }
Signature · 1 line
2.39.2
Re: [PATCH for-8.1 v2 22/26] target/riscv: error out on priv failure for RVH
Quoted 66 lines · Daniel Henrique Barboza
We have one last case where we're changing env->misa_ext* during validation.
riscv_cpu_disable_priv_spec_isa_exts(), at the end ofriscv_cpu_validate_set_extensions(), will disablecpu->cfg.ext_handcpu->cfg.ext_vifpriv_vercheck fails.This check can be done in
riscv_cpu_validate_misa_ext(). The difference here is that we're not silently disable it: we'll error out. Silently disabling a MISA extension after all the validation is completed can can cause inconsistencies that we don't have to deal with. Verify ealier and fail faster.Note that we're ignoring RVV
priv_vervalidation since its minimal priv is also the minimal value we support. RVH will error out if enabled underpriv_verunder 1_12_0.As a bonus, we're guaranteeing that all env->misa_ext* changes will happen up until
riscv_set_G_virt_ext(). We don't have to worry about keepingenv->misa_extin sync withcpu->cfg.Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/cpu.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)target/riscv/cpu.c +15
-5@@ -1067,6 +1067,20 @@ static void riscv_cpu_validate_misa_ext(CPURISCVState *env,return ; } +/* +* Check for priv spec version. RVH is 1_12_0, RVV is 1_10_0. +* We don't support anything under 1_10_0 so skip checking +* priv for RVV. +* +* We're hardcoding it here to avoid looping into the +* 50+ entries of isa_edata_arr[] just to check the RVH +* entry. +*/ +if (misa_ext &RVH && env->priv_ver <PRIV_VERSION_1_12_0 ) { +error_setg (errp,"H extension requires priv spec 1.12.0" ); +return ; + } +if (misa_ext &RVV ) {/* * V depends on Zve64d, which requires D. It also @@ -1117,14 +1131,10 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)/* * Check consistency between chosen extensions while setting -* cpu->cfg accordingly, setting env->misa_ext and -* misa_ext_mask in the end. +* cpu->cfg accordingly. */ static void riscv_cpu_validate_set_extensions (RISCVCPU *cpu,Error **errp) { -CPURISCVState *env = &cpu->env; -uint32_t ext =0 ; -if (cpu->cfg.epmp && !cpu->cfg.pmp) {/* * Enhanced PMP should only be available
target/riscv/cpu.c static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) · line 1247
Quoted 7 lines
target/riscv/cpu.c +0
-4@@ -1241,10 +1251,6 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)* validated and set everything we need. */ riscv_cpu_disable_priv_spec_isa_exts (cpu); - - ext =riscv_get_misa_ext_with_cpucfg (&cpu->cfg); - - env->misa_ext_mask = env->misa_ext = ext;
This can be removed in the patch 17.
Regards,
Weiwei Li
Quoted 14 lines
target/riscv/cpu.c +4
-0}#ifndef CONFIG_USER_ONLY @@ -1355,6 +1361,10 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } +/* +* This is the last point where env->misa_ext* can +* be changed. +*/ if (cpu->cfg.ext_g) {riscv_set_G_virt_ext (cpu); }
PATCH v2 23/26 target/riscv: split riscv_cpu_validate_set_extensions()
In reply to Daniel Henrique BarbozaWe're now ready to split riscv_cpu_validate_set_extensions() in two. None of these steps are going to touch env->misa_ext*.
riscv_cpu_validate_extensions() will take care of all validations based on cpu->cfg values. cpu->cfg changes that are required for the validation are being tolerated here. This is the case of extensions such as ext_zfh enabling ext_zfhmin.
The RVV chain enablement (ext_zve64d, ext_zve64f and ext_zve32f) is also being tolerated because the risk of failure is being mitigated by the RVV -> RVD && RVF dependency in validate_misa_ext() done prior.
In an ideal world we would have all these extensions declared as object properties, with getters and setters, and we would be able to, e.g., enable ext_zfhmin as soon as ext_zfh is enabled. This would avoid cpu->cfg changes during riscv_cpu_validate_extensions(). Easier said than done, not just because of the hundreds of lines involved in it, but also because we want these properties to be available just for generic CPUs (named CPUs don't want these properties exposed for users). For now we'll work with that we have.
riscv_cpu_commit_cpu_cfg() is the last step of the validation where more cpu->cfg properties are set and disabling of extensions due to priv spec happens. We're already validated everything we wanted, so any cpu->cfg change made here is valid.
--- target/riscv/cpu.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
target/riscv/cpu.c +9 -4
@@ -1130,10 +1130,10 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp) }/* -* Check consistency between chosen extensions while setting -* cpu->cfg accordingly. +* Check consistency between chosen extensions. No changes +* in env->misa_ext are made. */ -static void riscv_cpu_validate_set_extensions (RISCVCPU *cpu,Error **errp) +static void riscv_cpu_validate_extensions (RISCVCPU *cpu,Error **errp) {if (cpu->cfg.epmp && !cpu->cfg.pmp) {/*
@@ -1222,7 +1222,10 @@ static void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)return ; } } +} +static void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu) +{if (cpu->cfg.ext_zk) { cpu->cfg.ext_zkn = true; cpu->cfg.ext_zkr = true;
@@ -1375,12 +1378,14 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } -riscv_cpu_validate_set_extensions (cpu, &local_err); +riscv_cpu_validate_extensions (cpu, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);return ; } +riscv_cpu_commit_cpu_cfg (cpu); +#ifndef CONFIG_USER_ONLY if (cpu->cfg.ext_sstc) {riscv_timer_init (cpu);
Signature · 1 line
2.39.2
PATCH v2 24/26 target/riscv: use misa_ext val in riscv_cpu_validate_extensions()
In reply to Daniel Henrique BarbozaSimilar to what we did with riscv_cpu_validate_misa_ext(), let's read all MISA bits from a misa_ext val instead of reading from the cpu->cfg object.
This will allow write_misa() to use riscv_cpu_validate_extensions().
--- target/riscv/cpu.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-)
target/riscv/cpu.c +14 -11
@@ -1130,10 +1130,13 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp) }/* -* Check consistency between chosen extensions. No changes -* in env->misa_ext are made. +* Check consistency between cpu->cfg extensions and a +* candidate misa_ext value. No changes in env->misa_ext +* are made. */ -static void riscv_cpu_validate_extensions (RISCVCPU *cpu,Error **errp) +static void riscv_cpu_validate_extensions (RISCVCPU *cpu, +uint32_t misa_ext, +Error **errp) {if (cpu->cfg.epmp && !cpu->cfg.pmp) {/*
@@ -1144,12 +1147,12 @@ static void riscv_cpu_validate_extensions(RISCVCPU *cpu, Error **errp)return ; } -if (cpu->cfg.ext_f && !cpu->cfg.ext_icsr) { +if (misa_ext &RVF && !cpu->cfg.ext_icsr) {error_setg (errp,"F extension requires Zicsr" );return ; } -if ((cpu->cfg.ext_zawrs) && !cpu->cfg.ext_a) { +if ((cpu->cfg.ext_zawrs) && !(misa_ext &RVA )) {error_setg (errp,"Zawrs extension requires A extension" );return ; }
@@ -1158,13 +1161,13 @@ static void riscv_cpu_validate_extensions(RISCVCPU *cpu, Error **errp) cpu->cfg.ext_zfhmin = true; } -if (cpu->cfg.ext_zfhmin && !cpu->cfg.ext_f) { +if (cpu->cfg.ext_zfhmin && !(misa_ext &RVF )) {error_setg (errp,"Zfh/Zfhmin extensions require F extension" );return ; }/* The V vector extension depends on the Zve64d extension */ -if (cpu->cfg.ext_v) { +if (misa_ext &RVV ) { cpu->cfg.ext_zve64d = true; }
@@ -1178,12 +1181,12 @@ static void riscv_cpu_validate_extensions(RISCVCPU *cpu, Error **errp) cpu->cfg.ext_zve32f = true; } -if (cpu->cfg.ext_zve64d && !cpu->cfg.ext_d) { +if (cpu->cfg.ext_zve64d && !(misa_ext &RVD )) {error_setg (errp,"Zve64d/V extensions require D extension" );return ; } -if (cpu->cfg.ext_zve32f && !cpu->cfg.ext_f) { +if (cpu->cfg.ext_zve32f && !(misa_ext &RVF )) {error_setg (errp,"Zve32f/Zve64f extensions require F extension" );return ; }
@@ -1216,7 +1219,7 @@ static void riscv_cpu_validate_extensions(RISCVCPU *cpu, Error **errp)error_setg (errp,"Zfinx extension requires Zicsr" );return ; } -if (cpu->cfg.ext_f) { +if (misa_ext &RVF ) {error_setg (errp,"Zfinx cannot be supported together with F extension" );return ;
@@ -1378,7 +1381,7 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } -riscv_cpu_validate_extensions (cpu, &local_err); +riscv_cpu_validate_extensions (cpu, env->misa_ext, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);return ;
Signature · 1 line
2.39.2
PATCH v2 25/26 target/riscv: rework write_misa()
In reply to Daniel Henrique Barbozawrite_misa() must use as much common logic as possible. We want to open code just the bits that are exclusive to the CSR write operation and TCG internals.
Rewrite write_misa() to work as follows:
- supress RVC right after verifying that we're not updating RVG;
- mask the write using misa_ext_mask to avoid enabling unsupported extensions;
- emulate the steps done by realize(): validate the candidate misa_ext val, then validate the configuration with the candidate misa_ext val, and finally commit the changes to cpu->cfg.
If any of the validation steps fails simply ignore the write operation.
Let's keep write_misa() as experimental for now until this logic gains enough mileage.
--- target/riscv/cpu.c | 12 +++++------- target/riscv/cpu.h | 6 ++++++ target/riscv/csr.c | 47 +++++++++++++++++++++------------------------- 3 files changed, 32 insertions(+), 33 deletions(-)
target/riscv/cpu.c +5 -7
@@ -1027,9 +1027,8 @@ static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu) } -static void riscv_cpu_validate_misa_ext (CPURISCVState *env, -uint32_t misa_ext, -Error **errp) +void riscv_cpu_validate_misa_ext (CPURISCVState *env,uint32_t misa_ext, +Error **errp) {Error *local_err =NULL ;
@@ -1134,9 +1133,8 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)* candidate misa_ext value. No changes in env->misa_ext * are made. */ -static void riscv_cpu_validate_extensions (RISCVCPU *cpu, -uint32_t misa_ext, -Error **errp) +void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext, +Error **errp) {if (cpu->cfg.epmp && !cpu->cfg.pmp) {/*
@@ -1227,7 +1225,7 @@ static void riscv_cpu_validate_extensions(RISCVCPU *cpu, } } -static void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu) +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu) {if (cpu->cfg.ext_zk) { cpu->cfg.ext_zkn = true;
target/riscv/cpu.h +6 -0
@@ -593,6 +593,12 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,char *riscv_isa_string (RISCVCPU *cpu);void riscv_cpu_list (void ); +void riscv_cpu_validate_misa_ext (CPURISCVState *env,uint32_t misa_ext, +Error **errp); +void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext, +Error **errp); +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu); +#define cpu_list riscv_cpu_list#define cpu_mmu_index riscv_cpu_mmu_index
target/riscv/csr.c +21 -26
74 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH for-8.1 v2 25/26] target/riscv: rework write_misa()
Quoted 148 lines · Daniel Henrique Barboza
write_misa()must use as much common logic as possible. We want to open code just the bits that are exclusive to the CSR write operation and TCG internals.Rewrite
write_misa()to work as follows:- supress RVC right after verifying that we're not updating RVG;
- mask the write using
misa_ext_maskto avoid enabling unsupported extensions;- emulate the steps done by
realize(): validate the candidatemisa_extval, then validate the configuration with the candidatemisa_extval, and finally commit the changes tocpu->cfg.If any of the validation steps fails simply ignore the write operation.
Let's keep
write_misa()as experimental for now until this logic gains enough mileage.Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/cpu.c | 12 +++++------- target/riscv/cpu.h | 6 ++++++ target/riscv/csr.c | 47 +++++++++++++++++++++------------------------- 3 files changed, 32 insertions(+), 33 deletions(-)target/riscv/cpu.c +5
-7@@ -1027,9 +1027,8 @@ static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu) } -static void riscv_cpu_validate_misa_ext (CPURISCVState *env, -uint32_t misa_ext, -Error **errp) +void riscv_cpu_validate_misa_ext (CPURISCVState *env,uint32_t misa_ext, +Error **errp) {Error *local_err =NULL ;@@ -1134,9 +1133,8 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)* candidate misa_ext value. No changes in env->misa_ext * are made. */ -static void riscv_cpu_validate_extensions (RISCVCPU *cpu, -uint32_t misa_ext, -Error **errp) +void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext, +Error **errp) {if (cpu->cfg.epmp && !cpu->cfg.pmp) {/* @@ -1227,7 +1225,7 @@ static void riscv_cpu_validate_extensions(RISCVCPU *cpu, } } -static void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu) +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu) {if (cpu->cfg.ext_zk) { cpu->cfg.ext_zkn = true;target/riscv/cpu.h +6
-0@@ -593,6 +593,12 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,char *riscv_isa_string (RISCVCPU *cpu);void riscv_cpu_list (void ); +void riscv_cpu_validate_misa_ext (CPURISCVState *env,uint32_t misa_ext, +Error **errp); +void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext, +Error **errp); +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu); +#define cpu_list riscv_cpu_list#define cpu_mmu_index riscv_cpu_mmu_indextarget/riscv/csr.c +17
-24@@ -1343,6 +1343,9 @@ static RISCVException read_misa(CPURISCVState *env, int csrno,static RISCVException write_misa (CPURISCVState *env,int csrno,target_ulong val) { +RISCVCPU *cpu =env_archcpu (env); +Error *local_err =NULL ; +if (!riscv_cpu_cfg (env)->misa_w) {/* drop write to misa */ return RISCV_EXCP_NONE ;@@ -1353,47 +1356,39 @@ static RISCVException write_misa(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } -/* 'I' or 'E' must be present */ -if (!(val & (RVI |RVE ))) { -/* It is not, drop write to misa */ -return RISCV_EXCP_NONE ; - } - -/* 'E' excludes all other extensions */ -if (val &RVE ) { -/* -* when we support 'E' we can do "val = RVE;" however -* for now we just drop writes if 'E' is present. -*/ -return RISCV_EXCP_NONE ; - } -/* -* misa.MXL writes are not supported by QEMU. -* Drop writes to those bits. +* Suppress 'C' if next instruction is not aligned +* TODO: this should check next_pc */ +if ((val &RVC ) && (GETPC () & ~3 ) !=0 ) { + val &= ~RVC ; + }/* Mask extensions that are not supported by this hart */ val &= env->misa_ext_mask; -/* 'D' depends on 'F', so clear 'D' if 'F' is not present */ -if ((val &RVD ) && !(val &RVF )) { - val &= ~RVD ; +/* If nothing changed, do nothing. */ +if (val == env->misa_ext) { +return RISCV_EXCP_NONE ; }/* -* Suppress 'C' if next instruction is not aligned -* TODO: this should check next_pc +* This flow is similar to what riscv_cpu_realize() does, +* with the difference that we will update env->misa_ext +* value if everything is ok. */ -if ((val &RVC ) && (GETPC () & ~3 ) !=0 ) { - val &= ~RVC ; +riscv_cpu_validate_misa_ext (env, val, &local_err); +if (local_err !=NULL ) { +return RISCV_EXCP_NONE ; }
target/riscv/csr.c static RISCVException write_misa(CPURISCVState *env, int csrno, · line 1391
Quoted 10 lines
target/riscv/csr.c +4
-2-/* If nothing changed, do nothing. */ -if (val == env->misa_ext) { +riscv_cpu_validate_extensions (cpu, val, &local_err); +if (local_err !=NULL ) {return RISCV_EXCP_NONE ; } +riscv_cpu_commit_cpu_cfg (cpu); +
In this way, it seems that Disabling V in misa may be enabled but will not work, since Zve64d/f... is still true.
The similar questions for C when Zc* extension is supported.
And in this way, if multi-letter extensions(such as Zfh) which depend on misa extensions(F) are supported, whether the misa extensions can be disabled? The answer is 'NO' in current implementation.
Regards,
Weiwei Li
Quoted 3 lines
target/riscv/csr.c +0
-0if (!(val &RVF )) { env->mstatus &= ~MSTATUS_FS ; }
Re: [PATCH for-8.1 v2 25/26] target/riscv: rework write_misa()
Quoted 151 lines · liweiwei
Quoted 136 lines · Daniel Henrique Barboza
write_misa()must use as much common logic as possible. We want to open code just the bits that are exclusive to the CSR write operation and TCG internals.Rewrite
write_misa()to work as follows:- supress RVC right after verifying that we're not updating RVG;
- mask the write using
misa_ext_maskto avoid enabling unsupported extensions;- emulate the steps done by
realize(): validate the candidatemisa_extval, then validate the configuration with the candidatemisa_extval, and finally commit the changes tocpu->cfg.If any of the validation steps fails simply ignore the write operation.
Let's keep
write_misa()as experimental for now until this logic gains enough mileage.Signed-off-by Daniel Henrique Barboza <[email protected]>---
target/riscv/cpu.c | 12 +++++------- target/riscv/cpu.h | 6 ++++++ target/riscv/csr.c | 47 +++++++++++++++++++++------------------------- 3 files changed, 32 insertions(+), 33 deletions(-)target/riscv/cpu.c +5
-7@@ -1027,9 +1027,8 @@ static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu) } -static void riscv_cpu_validate_misa_ext (CPURISCVState *env, - uint32_t misa_ext, - Error **errp) +void riscv_cpu_validate_misa_ext (CPURISCVState *env,uint32_t misa_ext, + Error **errp) { Error *local_err = NULL;@@ -1134,9 +1133,8 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)* candidate misa_ext value. No changes in env->misa_ext * are made. */ -static void riscv_cpu_validate_extensions (RISCVCPU *cpu, - uint32_t misa_ext, - Error **errp) +void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext, + Error **errp) { if (cpu->cfg.epmp && !cpu->cfg.pmp) { /*@@ -1227,7 +1225,7 @@ static void riscv_cpu_validate_extensions(RISCVCPU *cpu, } } -static void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu) +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu) { if (cpu->cfg.ext_zk) { cpu->cfg.ext_zkn = true;target/riscv/cpu.h +6
-0@@ -593,6 +593,12 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, char *riscv_isa_string(RISCVCPU *cpu); void riscv_cpu_list(void); +void riscv_cpu_validate_misa_ext (CPURISCVState *env,uint32_t misa_ext, + Error **errp); +void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext, + Error **errp); +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu); + #define cpu_list riscv_cpu_list #define cpu_mmu_index riscv_cpu_mmu_indextarget/riscv/csr.c +16
-24@@ -1343,6 +1343,9 @@ static RISCVException read_misa(CPURISCVState *env, int csrno, static RISCVException write_misa(CPURISCVState *env, int csrno, target_ulong val) { + RISCVCPU *cpu = env_archcpu(env); + Error *local_err = NULL; + if (!riscv_cpu_cfg(env)->misa_w) { /* drop write to misa */ return RISCV_EXCP_NONE;@@ -1353,47 +1356,39 @@ static RISCVException write_misa(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } - /* 'I' or 'E' must be present */ - if (!(val & (RVI | RVE))) { - /* It is not, drop write to misa */ - return RISCV_EXCP_NONE; - } - - /* 'E' excludes all other extensions */ - if (val & RVE) { - /* - * when we support 'E' we can do "val = RVE;" however - * for now we just drop writes if 'E' is present. - */ - return RISCV_EXCP_NONE; - } - /* - * misa.MXL writes are not supported by QEMU. - * Drop writes to those bits. + * Suppress 'C' if next instruction is not aligned + * TODO: this should check next_pc */ + if ((val & RVC) && (GETPC() & ~3) != 0) { + val &= ~RVC; + } /* Mask extensions that are not supported by this hart */ val &= env->misa_ext_mask; - /* 'D' depends on 'F', so clear 'D' if 'F' is not present */ - if ((val & RVD) && !(val & RVF)) { - val &= ~RVD; + /* If nothing changed, do nothing. */ + if (val == env->misa_ext) { + return RISCV_EXCP_NONE; } /* - * Suppress 'C' if next instruction is not aligned - * TODO: this should check next_pc + * This flow is similar to what riscv_cpu_realize() does, + * with the difference that we will update env->misa_ext + * value if everything is ok. */ - if ((val & RVC) && (GETPC() & ~3) != 0) { - val &= ~RVC; + riscv_cpu_validate_misa_ext(env, val, &local_err); + if (local_err != NULL) {target/riscv/csr.c static RISCVException write_misa(CPURISCVState *env, int csrno, · line 1385
Quoted 10 lines
target/riscv/csr.c +5
-2+ return RISCV_EXCP_NONE; } - /* If nothing changed, do nothing. */ - if (val == env->misa_ext) { + riscv_cpu_validate_extensions(cpu, val, &local_err); + if (local_err != NULL) { return RISCV_EXCP_NONE; } + riscv_cpu_commit_cpu_cfg(cpu); +In this way, it seems that Disabling V in misa may be enabled but will not work, since Zve64d/f... is still true.
Quoted 3 lines
The similar questions for C when Zc* extension is supported.
And in this way, if multi-letter extensions(such as Zfh) which depend on misa extensions(F) are supported, whether the misa extensions can be disabled? The answer is 'NO' in current implementation.
One problem we have here is that we can't re-enable Z extensions via CSR writes (at least as far as I'm aware of). This means that if write_misa() disables a Z extension we don't have a reliable way of bringing it back. Enabling a Z extension via a write_misa() call is less problematic.
So I believe we have this hard rule: we don't disable Z extensions in write_misa().
With this in mind, I took a look at all MISA bits. I believe it's good to have some special cases that would be prioritized in the CSR write. By special cases I mean bits that would cause more bits/z extensions to be enabled. We would prioritize handling them in write_misa(), dropping the changes of all other bits. I.e. if a special case is detected, we handle it and we finish the CSR write. This would spare us from covering a lot of weird cases, e.g. RVG being enabled while RVA is being disabled. In this case RVG takes precedence.
- RVE
RVE. RVE requires everything else to be disabled. IMO we can let the user at least try - perhaps the hart doesn't have Z extensions enabled at that point. If write_misa() tries to enable RVE, and only RVE, we proceed with the validation. This would be our first check: RVE being enabled, and enabled by itself. If a RVE write has any other bits enabled, drop the write.
- RVG
All things considered, it's not that much of a big deal to support it. Enabling RVG, assuming it wasn't enabled already, would cause us to mass enable IMAFD_Zicsr_Zifencei. The only problem here is with F, which is mutually exclusive with Zfinx. If Zfinx is enabled we can't enable F, thus we can't enable RVG, and we would simply drop the write. Enabling RVG would also be a standalone action in write_misa().
Disabling RVG has no side effects and it's not a special case.
- RVV
Enabling RVV requires enabling D, F, ext_zve64d, ext_zve64f and ext_zve32f. The same F constraint (Zfinx) applies here as well. Assuming we can enable F, we can enable all these extensions, commit the RVV bit change and finish the write.
Disabling RVV has no side-effects, at least as far as I can tell, because all these other extensions can exist without RVV, so it's not a special case.
These are all the special cases that I can think of. RVE, then RVG, and then RVV. If any of these bits are enabled we should just handle them standalone and finish the write. I don't think we need to go through the regular validation workflow for them.
The remaining cases would go through regular validation. We have certain bits that would deactivate Z extensions if disabled:
- RVA: would disable Zawrs
- RVD: would disable zve64d
- RVF: would disable Zfh/Zfhmin, zve64f, zve32f, zve64d
We can allow these bits to be disabled, as long as there's no Z extension being disabled in the process. If an enabled Z extension is impacted, we drop the misa write.
Finally, we have I, M, A, F and D and their relation with RVG. RVG would be disabled if any of these bits are disabled (and validation succeeds).
That's all the caveats that I can think of. The code that enables a certain MISA bit can be shared with the existing code that riscv_cpu_realize() uses. Code that disables MISA bits would be new code that only write_misa() would use.
Let me know what you all think. I intend to go this direction in v3.
Thanks,
Daniel
Quoted 9 lines
Regards,
Weiwei Li
Quoted 3 lines
target/riscv/csr.c +0
-0if (!(val & RVF)) { env->mstatus &= ~MSTATUS_FS; }
Re: [PATCH for-8.1 v2 25/26] target/riscv: rework write_misa()
Quoted 199 lines · Daniel Henrique Barboza
Quoted 160 lines · liweiwei
Quoted 145 lines · Daniel Henrique Barboza
write_misa()must use as much common logic as possible. We want to open code just the bits that are exclusive to the CSR write operation and TCG internals.Rewrite
write_misa()to work as follows:- supress RVC right after verifying that we're not updating RVG;
- mask the write using
misa_ext_maskto avoid enabling unsupported extensions;- emulate the steps done by
realize(): validate the candidatemisa_extval, then validate the configuration with the candidatemisa_extval, and finally commit the changes tocpu->cfg.If any of the validation steps fails simply ignore the write operation.
Let's keep
write_misa()as experimental for now until this logic gains enough mileage.Signed-off-by Daniel Henrique Barboza <[email protected]>---
target/riscv/cpu.c | 12 +++++------- target/riscv/cpu.h | 6 ++++++ target/riscv/csr.c | 47 +++++++++++++++++++++------------------------- 3 files changed, 32 insertions(+), 33 deletions(-)target/riscv/cpu.c +4
-6@@ -1027,9 +1027,8 @@ static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu) } -static void riscv_cpu_validate_misa_ext (CPURISCVState *env, - uint32_t misa_ext, - Error **errp) +void riscv_cpu_validate_misa_ext (CPURISCVState *env,uint32_t misa_ext, + Error **errp) { Error *local_err = NULL;@@ -1134,9 +1133,8 @@ static voidriscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp) * candidate misa_ext value. No changes in env->misa_ext * are made. */ -static void riscv_cpu_validate_extensions (RISCVCPU *cpu, - uint32_t misa_ext, - Error **errp) +void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext, + Error **errp) { if (cpu->cfg.epmp && !cpu->cfg.pmp) {/*
target/riscv/cpu.c +1
-1@@ -1227,7 +1225,7 @@ static void riscv_cpu_validate_extensions(RISCVCPU *cpu, } } -static void riscv_cpu_commit_cpu_cfg(RISCVCPU *cpu) +void riscv_cpu_commit_cpu_cfg(RISCVCPU *cpu) { if (cpu->cfg.ext_zk) { cpu->cfg.ext_zkn = true;target/riscv/cpu.h +6
-0@@ -593,6 +593,12 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, char *riscv_isa_string(RISCVCPU *cpu); void riscv_cpu_list(void); +void riscv_cpu_validate_misa_ext (CPURISCVState *env,uint32_t misa_ext, + Error **errp); +void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext, + Error **errp); +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu); + #define cpu_list riscv_cpu_list #define cpu_mmu_index riscv_cpu_mmu_indextarget/riscv/csr.c +3
-0@@ -1343,6 +1343,9 @@ static RISCVException read_misa(CPURISCVState *env, int csrno, static RISCVException write_misa(CPURISCVState *env, int csrno, target_ulong val) { + RISCVCPU *cpu = env_archcpu(env); + Error *local_err = NULL; + if (!riscv_cpu_cfg(env)->misa_w) { /* drop write to misa */return
RISCV_EXCP_NONE;target/riscv/csr.c +13
-24@@ -1353,47 +1356,39 @@ static RISCVException write_misa(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } - /* 'I' or 'E' must be present */ - if (!(val & (RVI | RVE))) { - /* It is not, drop write to misa */ - return RISCV_EXCP_NONE; - } - - /* 'E' excludes all other extensions */ - if (val & RVE) { - /* - * when we support 'E' we can do "val = RVE;" however - * for now we just drop writes if 'E' is present. - */ - return RISCV_EXCP_NONE; - } - /* - * misa.MXL writes are not supported by QEMU. - * Drop writes to those bits. + * Suppress 'C' if next instruction is not aligned + * TODO: this should check next_pc */ + if ((val & RVC) && (GETPC() & ~3) != 0) { + val &= ~RVC; + } /* Mask extensions that are not supported by this hart */ val &= env->misa_ext_mask; - /* 'D' depends on 'F', so clear 'D' if 'F' is not present */ - if ((val & RVD) && !(val & RVF)) { - val &= ~RVD; + /* If nothing changed, do nothing. */ + if (val == env->misa_ext) { + return RISCV_EXCP_NONE; } /* - * Suppress 'C' if next instruction is not aligned - * TODO: this should check next_pc + * This flow is similar to what riscv_cpu_realize() does, + * with the difference that we will update env->misa_ext + * value if everything is ok. */ - if ((val & RVC) && (GETPC() & ~3) != 0) { - val &= ~RVC; + riscv_cpu_validate_misa_ext(env, val, &local_err); + if (local_err != NULL) {target/riscv/csr.c static RISCVException · line 1386
Quoted 10 lines
target/riscv/csr.c +5
-2+ return RISCV_EXCP_NONE; } - /* If nothing changed, do nothing. */ - if (val == env->misa_ext) { + riscv_cpu_validate_extensions(cpu, val, &local_err); + if (local_err != NULL) { return RISCV_EXCP_NONE; } + riscv_cpu_commit_cpu_cfg(cpu); +In this way, it seems that Disabling V in misa may be enabled but will not work, since Zve64d/f... is still true.
Quoted 3 lines
The similar questions for C when Zc* extension is supported.
And in this way, if multi-letter extensions(such as Zfh) which depend on misa extensions(F) are supported, whether the misa extensions can be disabled? The answer is 'NO' in current implementation.
One problem we have here is that we can't re-enable Z extensions via CSR writes (at least as far as I'm aware of). This means that if
write_misa()disables a Z extension we don't have a reliable way of bringing it back. Enabling a Z extension via awrite_misa()call is less problematic.So I believe we have this hard rule: we don't disable Z extensions in
write_misa().With this in mind, I took a look at all MISA bits. I believe it's good to have some special cases that would be prioritized in the CSR write. By special cases I mean bits that would cause more bits/z extensions to be enabled. We would prioritize handling them in
write_misa(), dropping the changes of all other bits. I.e. if a special case is detected, we handle it and we finish the CSR write. This would spare us from covering a lot of weird cases, e.g. RVG being enabled while RVA is being disabled. In this case RVG takes precedence.- RVE
RVE. RVE requires everything else to be disabled. IMO we can let the user at least try - perhaps the hart doesn't have Z extensions enabled at that point. If
write_misa()tries to enable RVE, and only RVE, we proceed with the validation. This would be our first check: RVE being enabled, and enabled by itself. If a RVE write has any other bits enabled, drop the write.- RVG
All things considered, it's not that much of a big deal to support it. Enabling RVG, assuming it wasn't enabled already, would cause us to mass enable
IMAFD_Zicsr_Zifencei. The only problem here is with F, which is mutually exclusive with Zfinx. If Zfinx is enabled we can't enable F, thus we can't enable RVG, and we would simply drop the write. Enabling RVG would also be a standalone action inwrite_misa().Disabling RVG has no side effects and it's not a special case.
Quoted 9 lines
- RVV
Enabling RVV requires enabling D, F,
ext_zve64d,ext_zve64fandext_zve32f. The same F constraint (Zfinx) applies here as well. Assuming we can enable F, we can enable all these extensions, commit the RVV bit change and finish the write.Disabling RVV has no side-effects, at least as far as I can tell, because all these other extensions can exist without RVV, so it's not a special case.
Vector instructions will never be really disabled in this way, only misa.V bit is cleared, since
zve64d/f will be implicitly enabled when RVV is enabled, they will continue to work even if misa.V is cleared.
And we can never disabled F/D when V is firstly enabled, even if we disable them together with V.
Quoted 19 lines
These are all the special cases that I can think of. RVE, then RVG, and then RVV. If any of these bits are enabled we should just handle them standalone and finish the write. I don't think we need to go through the regular validation workflow for them.
The remaining cases would go through regular validation. We have certain bits that would deactivate Z extensions if disabled:
- RVA: would disable Zawrs
- RVD: would disable zve64d
- RVF: would disable Zfh/Zfhmin, zve64f, zve32f, zve64dWe can allow these bits to be disabled, as long as there's no Z extension being disabled in the process. If an enabled Z extension is impacted, we drop the misa write. Finally, we have I, M, A, F and D and their relation with RVG. RVG would be disabled if any of these bits are disabled (and validation succeeds).
Quoted 7 lines
That's all the caveats that I can think of. The code that enables a certain MISA bit can be shared with the existing code that
riscv_cpu_realize()uses. Code that disables MISA bits would be new code that onlywrite_misa()would use.Let me know what you all think. I intend to go this direction in v3.
Yeah, I agree this is an acceptable way for write_misa. However, I think it's better if we can distinguish
the Z extensions implicitly enabled by misa extension(which can be disabled/re-enabled by write_misa) with
explicitly enabled Z extensions(which cannot).
Regards,
Weiwei Li
Quoted 16 lines
Thanks,
Daniel
Quoted 9 lines
Regards,
Weiwei Li
Quoted 3 lines
target/riscv/csr.c +0
-0if (!(val & RVF)) { env->mstatus &= ~MSTATUS_FS; }
Re: [PATCH for-8.1 v2 25/26] target/riscv: rework write_misa()
Quoted 231 lines · liweiwei
Quoted 190 lines · Daniel Henrique Barboza
Quoted 151 lines · liweiwei
Quoted 136 lines · Daniel Henrique Barboza
write_misa()must use as much common logic as possible. We want to open code just the bits that are exclusive to the CSR write operation and TCG internals.Rewrite
write_misa()to work as follows:- supress RVC right after verifying that we're not updating RVG;
- mask the write using
misa_ext_maskto avoid enabling unsupported extensions;- emulate the steps done by
realize(): validate the candidatemisa_extval, then validate the configuration with the candidatemisa_extval, and finally commit the changes tocpu->cfg.If any of the validation steps fails simply ignore the write operation.
Let's keep
write_misa()as experimental for now until this logic gains enough mileage.Signed-off-by Daniel Henrique Barboza <[email protected]>---
target/riscv/cpu.c | 12 +++++------- target/riscv/cpu.h | 6 ++++++ target/riscv/csr.c | 47 +++++++++++++++++++++------------------------- 3 files changed, 32 insertions(+), 33 deletions(-)target/riscv/cpu.c +5
-7@@ -1027,9 +1027,8 @@ static void riscv_cpu_disable_priv_spec_isa_exts(RISCVCPU *cpu) } -static void riscv_cpu_validate_misa_ext (CPURISCVState *env, - uint32_t misa_ext, - Error **errp) +void riscv_cpu_validate_misa_ext (CPURISCVState *env,uint32_t misa_ext, + Error **errp) { Error *local_err = NULL;@@ -1134,9 +1133,8 @@ static void riscv_cpu_validate_misa_mxl(RISCVCPU *cpu, Error **errp)* candidate misa_ext value. No changes in env->misa_ext * are made. */ -static void riscv_cpu_validate_extensions (RISCVCPU *cpu, - uint32_t misa_ext, - Error **errp) +void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext, + Error **errp) { if (cpu->cfg.epmp && !cpu->cfg.pmp) { /*@@ -1227,7 +1225,7 @@ static void riscv_cpu_validate_extensions(RISCVCPU *cpu, } } -static void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu) +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu) { if (cpu->cfg.ext_zk) { cpu->cfg.ext_zkn = true;target/riscv/cpu.h +6
-0@@ -593,6 +593,12 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, char *riscv_isa_string(RISCVCPU *cpu); void riscv_cpu_list(void); +void riscv_cpu_validate_misa_ext (CPURISCVState *env,uint32_t misa_ext, + Error **errp); +void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext, + Error **errp); +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu); + #define cpu_list riscv_cpu_list #define cpu_mmu_index riscv_cpu_mmu_indextarget/riscv/csr.c +16
-24@@ -1343,6 +1343,9 @@ static RISCVException read_misa(CPURISCVState *env, int csrno, static RISCVException write_misa(CPURISCVState *env, int csrno, target_ulong val) { + RISCVCPU *cpu = env_archcpu(env); + Error *local_err = NULL; + if (!riscv_cpu_cfg(env)->misa_w) { /* drop write to misa */ return RISCV_EXCP_NONE;@@ -1353,47 +1356,39 @@ static RISCVException write_misa(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } - /* 'I' or 'E' must be present */ - if (!(val & (RVI | RVE))) { - /* It is not, drop write to misa */ - return RISCV_EXCP_NONE; - } - - /* 'E' excludes all other extensions */ - if (val & RVE) { - /* - * when we support 'E' we can do "val = RVE;" however - * for now we just drop writes if 'E' is present. - */ - return RISCV_EXCP_NONE; - } - /* - * misa.MXL writes are not supported by QEMU. - * Drop writes to those bits. + * Suppress 'C' if next instruction is not aligned + * TODO: this should check next_pc */ + if ((val & RVC) && (GETPC() & ~3) != 0) { + val &= ~RVC; + } /* Mask extensions that are not supported by this hart */ val &= env->misa_ext_mask; - /* 'D' depends on 'F', so clear 'D' if 'F' is not present */ - if ((val & RVD) && !(val & RVF)) { - val &= ~RVD; + /* If nothing changed, do nothing. */ + if (val == env->misa_ext) { + return RISCV_EXCP_NONE; } /* - * Suppress 'C' if next instruction is not aligned - * TODO: this should check next_pc + * This flow is similar to what riscv_cpu_realize() does, + * with the difference that we will update env->misa_ext + * value if everything is ok. */ - if ((val & RVC) && (GETPC() & ~3) != 0) { - val &= ~RVC; + riscv_cpu_validate_misa_ext(env, val, &local_err); + if (local_err != NULL) {target/riscv/csr.c static RISCVException write_misa(CPURISCVState *env, int csrno, · line 1385
Quoted 10 lines
target/riscv/csr.c +5
-2+ return RISCV_EXCP_NONE; } - /* If nothing changed, do nothing. */ - if (val == env->misa_ext) { + riscv_cpu_validate_extensions(cpu, val, &local_err); + if (local_err != NULL) { return RISCV_EXCP_NONE; } + riscv_cpu_commit_cpu_cfg(cpu); +In this way, it seems that Disabling V in misa may be enabled but will not work, since Zve64d/f... is still true.
Quoted 3 lines
The similar questions for C when Zc* extension is supported.
And in this way, if multi-letter extensions(such as Zfh) which depend on misa extensions(F) are supported, whether the misa extensions can be disabled? The answer is 'NO' in current implementation.
One problem we have here is that we can't re-enable Z extensions via CSR writes (at least as far as I'm aware of). This means that if
write_misa()disables a Z extension we don't have a reliable way of bringing it back. Enabling a Z extension via awrite_misa()call is less problematic.So I believe we have this hard rule: we don't disable Z extensions in
write_misa().With this in mind, I took a look at all MISA bits. I believe it's good to have some special cases that would be prioritized in the CSR write. By special cases I mean bits that would cause more bits/z extensions to be enabled. We would prioritize handling them in
write_misa(), dropping the changes of all other bits. I.e. if a special case is detected, we handle it and we finish the CSR write. This would spare us from covering a lot of weird cases, e.g. RVG being enabled while RVA is being disabled. In this case RVG takes precedence.- RVE
RVE. RVE requires everything else to be disabled. IMO we can let the user at least try - perhaps the hart doesn't have Z extensions enabled at that point. If
write_misa()tries to enable RVE, and only RVE, we proceed with the validation. This would be our first check: RVE being enabled, and enabled by itself. If a RVE write has any other bits enabled, drop the write.- RVG
All things considered, it's not that much of a big deal to support it. Enabling RVG, assuming it wasn't enabled already, would cause us to mass enable
IMAFD_Zicsr_Zifencei. The only problem here is with F, which is mutually exclusive with Zfinx. If Zfinx is enabled we can't enable F, thus we can't enable RVG, and we would simply drop the write. Enabling RVG would also be a standalone action inwrite_misa().Disabling RVG has no side effects and it's not a special case.
Quoted 9 lines
- RVV
Enabling RVV requires enabling D, F,
ext_zve64d,ext_zve64fandext_zve32f. The same F constraint (Zfinx) applies here as well. Assuming we can enable F, we can enable all these extensions, commit the RVV bit change and finish the write.Disabling RVV has no side-effects, at least as far as I can tell, because all these other extensions can exist without RVV, so it's not a special case.
Vector instructions will never be really disabled in this way, only misa.V bit is cleared, since
zve64d/f will be implicitly enabled when RVV is enabled, they will continue to work even if misa.V is cleared.
And we can never disabled F/D when V is firstly enabled, even if we disable them together with V.
Quoted 24 lines
These are all the special cases that I can think of. RVE, then RVG, and then RVV. If any of these bits are enabled we should just handle them standalone and finish the write. I don't think we need to go through the regular validation workflow for them.
The remaining cases would go through regular validation. We have certain bits that would deactivate Z extensions if disabled:
- RVA: would disable Zawrs
- RVD: would disable zve64d
- RVF: would disable Zfh/Zfhmin, zve64f, zve32f, zve64dWe can allow these bits to be disabled, as long as there's no Z extension being disabled in the process. If an enabled Z extension is impacted, we drop the misa write. Finally, we have I, M, A, F and D and their relation with RVG. RVG would be disabled if any of these bits are disabled (and validation succeeds).
That's all the caveats that I can think of. The code that enables a certain MISA bit can be shared with the existing code that
riscv_cpu_realize()uses. Code that disables MISA bits would be new code that onlywrite_misa()would use.
Quoted 7 lines
Quoted 2 lines
Let me know what you all think. I intend to go this direction in v3.
Yeah, I agree this is an acceptable way for
write_misa. However, I think it's better if we can distinguishthe Z extensions implicitly enabled by misa
extension(which can be disabled/re-enabled by write_misa)withexplicitly enabled Z extensions(which cannot).
I have some ideas on how to do that. This series is growing a bit too large though, so I'd explore that on a follow-up.
Thanks,
Daniel
Quoted 22 lines
Regards,
Weiwei Li
Quoted 16 lines
Thanks,
Daniel
Quoted 9 lines
Regards,
Weiwei Li
Quoted 3 lines
target/riscv/csr.c +0
-0if (!(val & RVF)) { env->mstatus &= ~MSTATUS_FS; }
PATCH v2 26/26 target/riscv: update cpu->cfg misa bits in commit_cpu_cfg()
In reply to Daniel Henrique Barbozawrite_misa() is able to use the same validation workflow riscv_cpu_realize() uses. But it's still not capable of updating cpu->cfg misa props yet.
We have no way of blocking future (and current) code from checking env->misa_ext (via riscv_has_ext()) or reading cpu->cfg directly, so our best alternative is to keep everything in sync.
riscv_cpu_commit_cpu_cfg() now receives an extra 'misa_ext' parameter. If this val is different from the existing env->misa_ext, update env->misa and cpu->cfg with the new value. riscv_cpu_realize() will ignore this code since env->misa_ext isn't touched during validation, but write_misa() will use it to keep cpu->cfg in sync with the new env->misa_ext value.
--- target/riscv/cpu.c | 16 ++++++++++++++-- target/riscv/cpu.h | 2 +- target/riscv/csr.c | 3 +-- 3 files changed, 16 insertions(+), 5 deletions(-)
target/riscv/cpu.c +14 -2
@@ -1225,8 +1225,20 @@ void riscv_cpu_validate_extensions(RISCVCPU *cpu, uint32_t misa_ext, } } -void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu) +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu,uint32_t misa_ext) { +CPURISCVState *env = &cpu->env; + +/* +* write_misa() needs to update cpu->cfg with the new +* MISA bits. This is a no-op for the riscv_cpu_realize() +* path. +*/ +if (env->misa_ext != misa_ext) { + env->misa_ext = misa_ext; +riscv_set_cpucfg_with_misa (&cpu->cfg, misa_ext); + } +if (cpu->cfg.ext_zk) { cpu->cfg.ext_zkn = true; cpu->cfg.ext_zkr = true;
@@ -1385,7 +1397,7 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)return ; } -riscv_cpu_commit_cpu_cfg (cpu); +riscv_cpu_commit_cpu_cfg (cpu, env->misa_ext);#ifndef CONFIG_USER_ONLY if (cpu->cfg.ext_sstc) {
target/riscv/cpu.h +1 -1
@@ -597,7 +597,7 @@ void riscv_cpu_validate_misa_ext(CPURISCVState *env, uint32_t misa_ext, Error **errp);void riscv_cpu_validate_extensions (RISCVCPU *cpu,uint32_t misa_ext,Error **errp); -void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu); +void riscv_cpu_commit_cpu_cfg (RISCVCPU *cpu,uint32_t misa_ext);#define cpu_list riscv_cpu_list#define cpu_mmu_index riscv_cpu_mmu_index
target/riscv/csr.c +1 -2
@@ -1387,7 +1387,7 @@ static RISCVException write_misa(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } -riscv_cpu_commit_cpu_cfg (cpu); +riscv_cpu_commit_cpu_cfg (cpu, val);if (!(val &RVF )) { env->mstatus &= ~MSTATUS_FS ;
@@ -1395,7 +1395,6 @@ static RISCVException write_misa(CPURISCVState *env, int csrno,/* flush translation cache */ tb_flush (env_cpu (env)); - env->misa_ext = val; env->xl =riscv_cpu_mxl (env);return RISCV_EXCP_NONE ; }
Signature · 1 line
2.39.2