@samitouri / QOSamiQemu / commits / f0433a8bc4

target/riscv: Update MISA.C for Zc* extensions

MISA.C is set if the following extensions are selected: * Zca and not F. * Zca, Zcf and F (but not D) is specified (RV32 only). * Zca, Zcf and Zcd if D is specified (RV32 only). * Zca, Zcd if D is specified (RV64 only). Therefore, MISA.C must be set according to the Zc* extension rules. Warn the user if RVC is explicitly disabled but MISA.C is required by the rules above. Signed-off-by: Frank Chang <frank.chang@sifive.com> Reviewed-by: Max Chou <max.chou@sifive.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Message-ID: <20260424050509.3935180-2-frank.chang@sifive.com> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>

Frank Chang committed Apr 24, 2026 at 13:05 UTC f0433a8bc4ac5626499ff09ba5d165dbf7a4a980
1 file changed +39
target/riscv/tcg/tcg-cpu.c
+39
@@ -1163,6 +1163,44 @@ static void riscv_cpu_enable_implied_rules(RISCVCPU *cpu)
1163 }
1164 }
1165
1166 +/*
1167 + * MISA.C is set if the following extensions are selected:
1168 + * - Zca and not F.
1169 + * - Zca, Zcf and F (but not D) is specified on RV32.
1170 + * - Zca, Zcf and Zcd if D is specified on RV32.
1171 + * - Zca, Zcd if D is specified on RV64.
1172 + */
1173 +static void riscv_cpu_update_misa_c(RISCVCPU *cpu)
1174 +{
1175 + CPURISCVState *env = &cpu->env;
1176 + bool set_misa_c = false;
1177 +
1178 + if (riscv_has_ext(env, RVC)) {
1179 + return;
1180 + }
1181 +
1182 + if (cpu->cfg.ext_zca && !riscv_has_ext(env, RVF)) {
1183 + set_misa_c = true;
1184 + } else if (riscv_cpu_mxl(env) == MXL_RV32 &&
1185 + cpu->cfg.ext_zca && cpu->cfg.ext_zcf &&
1186 + (riscv_has_ext(env, RVD) ? cpu->cfg.ext_zcd :
1187 + riscv_has_ext(env, RVF))) {
1188 + set_misa_c = true;
1189 + } else if (riscv_cpu_mxl(env) == MXL_RV64 &&
1190 + cpu->cfg.ext_zca && cpu->cfg.ext_zcd) {
1191 + set_misa_c = true;
1192 + }
1193 +
1194 + if (set_misa_c) {
1195 + if (cpu_misa_ext_is_user_set(RVC)) {
1196 + warn_report("RVC mandated by Zca/Zcf/Zcd extensions");
1197 + return;
1198 + }
1199 +
1200 + riscv_cpu_set_misa_ext(env, env->misa_ext | RVC);
1201 + }
1202 +}
1203 +
1204 void riscv_tcg_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
1205 {
1206 CPURISCVState *env = &cpu->env;
@@ -1170,6 +1208,7 @@ void riscv_tcg_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
1208
1209 riscv_cpu_init_implied_exts_rules();
1210 riscv_cpu_enable_implied_rules(cpu);
1211 + riscv_cpu_update_misa_c(cpu);
1212
1213 riscv_cpu_validate_misa_priv(env, &local_err);
1214 if (local_err != NULL) {