Skip to contentSkip to conversation

Add the implied rules for G and B extensions

PATCH Superseded [email protected] 2 participants 6 messages Apr 21 – Apr 24 RISC-V

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add the implied rules for G and B extensions

From: Frank Chang <[email protected]>

Add the implied rules for G and B extensions to enable the implied extensions.

Changelog:
v2: Remove riscv_cpu_validate_g() and riscv_cpu_validate_b() as the
implied extensions can be enabled by the implied rules.

Frank Chang (1):
target/riscv: Add standard B extension implied rule

Jim Shu (1):
target/riscv: Add the implied rule for G extension

 target/riscv/cpu.c         | 26 ++++++++++++-
 target/riscv/tcg/tcg-cpu.c | 77 --------------------------------------
 2 files changed, 25 insertions(+), 78 deletions(-)

--
2.43.0

#1 lore raw

PATCH v2 1/2 target/riscv: Add the implied rule for G extension

From: Jim Shu <[email protected]>

Add the missing implied rule from G to imafd_zicsr_zifencei. riscv_cpu_validate_g() is also removed as imafd_zicsr_zifencei can be enabled by the implied rule.

---
 target/riscv/cpu.c         | 14 +++++++++++-
 target/riscv/tcg/tcg-cpu.c | 44 --------------------------------------
 2 files changed, 13 insertions(+), 45 deletions(-)
target/riscv/cpu.c +13 -1
index 72c6f4f0f14..7978b4fad43 100644
@@ -2247,6 +2247,18 @@ static RISCVCPUImpliedExtsRule RVV_IMPLIED = {
     },
 };
 
+static RISCVCPUImpliedExtsRule RVG_IMPLIED = {
+    .is_misa = true,
+    .ext = RVG,
+    .implied_misa_exts = RVI | RVM | RVA | RVF | RVD,
+    .implied_multi_exts = {
+        CPU_CFG_OFFSET(ext_zicsr),
+        CPU_CFG_OFFSET(ext_zifencei),
+
+        RISCV_IMPLIED_EXTS_RULE_END
+    },
+};
+
 static RISCVCPUImpliedExtsRule ZCB_IMPLIED = {
     .ext = CPU_CFG_OFFSET(ext_zcb),
     .implied_multi_exts = {
@@ -2634,7 +2646,7 @@ static RISCVCPUImpliedExtsRule ZVFBFA_IMPLIED = {
 
 RISCVCPUImpliedExtsRule *riscv_misa_ext_implied_rules[] = {
     &RVA_IMPLIED, &RVD_IMPLIED, &RVF_IMPLIED,
-    &RVM_IMPLIED, &RVV_IMPLIED, NULL
+    &RVM_IMPLIED, &RVV_IMPLIED, &RVG_IMPLIED, NULL
 };
 
 RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = {
target/riscv/tcg/tcg-cpu.c +0 -44

56 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/2] target/riscv: Add the implied rule for G extension

Quoted 8 lines · On Tue, Apr 21, 2026 at 5:43 PM <[email protected]> wrote:

From: Jim Shu <[email protected]>

Add the missing implied rule from G to imafd_zicsr_zifencei.

riscv_cpu_validate_g() is also removed as imafd_zicsr_zifencei can be enabled by the implied rule.

But what if a user enables G and disables one of these implied rules?

Alistair

Quoted 106 lines
---
 target/riscv/cpu.c         | 14 +++++++++++-
 target/riscv/tcg/tcg-cpu.c | 44 --------------------------------------
 2 files changed, 13 insertions(+), 45 deletions(-)
target/riscv/cpu.c +13 -1
index 72c6f4f0f14..7978b4fad43 100644
@@ -2247,6 +2247,18 @@ static RISCVCPUImpliedExtsRule RVV_IMPLIED = {
     },
 };
 
+static RISCVCPUImpliedExtsRule RVG_IMPLIED = {
+    .is_misa = true,
+    .ext = RVG,
+    .implied_misa_exts = RVI | RVM | RVA | RVF | RVD,
+    .implied_multi_exts = {
+        CPU_CFG_OFFSET(ext_zicsr),
+        CPU_CFG_OFFSET(ext_zifencei),
+
+        RISCV_IMPLIED_EXTS_RULE_END
+    },
+};
+
 static RISCVCPUImpliedExtsRule ZCB_IMPLIED = {
     .ext = CPU_CFG_OFFSET(ext_zcb),
     .implied_multi_exts = {
@@ -2634,7 +2646,7 @@ static RISCVCPUImpliedExtsRule ZVFBFA_IMPLIED = {
 
 RISCVCPUImpliedExtsRule *riscv_misa_ext_implied_rules[] = {
     &RVA_IMPLIED, &RVD_IMPLIED, &RVF_IMPLIED,
-    &RVM_IMPLIED, &RVV_IMPLIED, NULL
+    &RVM_IMPLIED, &RVV_IMPLIED, &RVG_IMPLIED, NULL
 };
 
 RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = {
target/riscv/tcg/tcg-cpu.c +0 -45
index f3f78088956..840ef82350d 100644
@@ -532,46 +532,6 @@ static void riscv_cpu_update_named_features(RISCVCPU *cpu)
     cpu->cfg.ext_ziccrse = cpu->cfg.has_priv_1_11;
 }
 
-static void riscv_cpu_validate_g(RISCVCPU *cpu)
-{
-    const char *warn_msg = "RVG mandates disabled extension %s";
-    uint32_t g_misa_bits[] = {RVI, RVM, RVA, RVF, RVD};
-    bool send_warn = cpu_misa_ext_is_user_set(RVG);
-
-    for (int i = 0; i < ARRAY_SIZE(g_misa_bits); i++) {
-        uint32_t bit = g_misa_bits[i];
-
-        if (riscv_has_ext(&cpu->env, bit)) {
-            continue;
-        }
-
-        if (!cpu_misa_ext_is_user_set(bit)) {
-            riscv_cpu_write_misa_bit(cpu, bit, true);
-            continue;
-        }
-
-        if (send_warn) {
-            warn_report(warn_msg, riscv_get_misa_ext_name(bit));
-        }
-    }
-
-    if (!cpu->cfg.ext_zicsr) {
-        if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zicsr))) {
-            cpu->cfg.ext_zicsr = true;
-        } else if (send_warn) {
-            warn_report(warn_msg, "zicsr");
-        }
-    }
-
-    if (!cpu->cfg.ext_zifencei) {
-        if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zifencei))) {
-            cpu->cfg.ext_zifencei = true;
-        } else if (send_warn) {
-            warn_report(warn_msg, "zifencei");
-        }
-    }
-}
-
 static void riscv_cpu_validate_b(RISCVCPU *cpu)
 {
     const char *warn_msg = "RVB mandates disabled extension %s";
@@ -611,10 +571,6 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)
     CPURISCVState *env = &cpu->env;
     Error *local_err = NULL;
 
-    if (riscv_has_ext(env, RVG)) {
-        riscv_cpu_validate_g(cpu);
-    }
-
     if (riscv_has_ext(env, RVB)) {
         riscv_cpu_validate_b(cpu);
     }
--

2.43.0

#3 lore raw

PATCH v2 2/2 target/riscv: Add standard B extension implied rule

In reply to [email protected]

From: Frank Chang <[email protected]>

Add the missing implied rule for standard B extension. Standard B extension implies Zba, Zbb, Zbs extensions. riscv_cpu_validate_b() is also removed as Zba, Zbb, Zbs extensions can be enabled by the implied rule.

RISC-V B spec: https://github.com/riscv/riscv-b

---
 target/riscv/cpu.c         | 14 +++++++++++++-
 target/riscv/tcg/tcg-cpu.c | 33 ---------------------------------
 2 files changed, 13 insertions(+), 34 deletions(-)
target/riscv/cpu.c +13 -1
index 7978b4fad43..37a328f2df7 100644
@@ -2259,6 +2259,17 @@ static RISCVCPUImpliedExtsRule RVG_IMPLIED = {
     },
 };
 
+static RISCVCPUImpliedExtsRule RVB_IMPLIED = {
+    .is_misa = true,
+    .ext = RVB,
+    .implied_multi_exts = {
+        CPU_CFG_OFFSET(ext_zba), CPU_CFG_OFFSET(ext_zbb),
+        CPU_CFG_OFFSET(ext_zbs),
+
+        RISCV_IMPLIED_EXTS_RULE_END
+    },
+};
+
 static RISCVCPUImpliedExtsRule ZCB_IMPLIED = {
     .ext = CPU_CFG_OFFSET(ext_zcb),
     .implied_multi_exts = {
@@ -2646,7 +2657,8 @@ static RISCVCPUImpliedExtsRule ZVFBFA_IMPLIED = {
 
 RISCVCPUImpliedExtsRule *riscv_misa_ext_implied_rules[] = {
     &RVA_IMPLIED, &RVD_IMPLIED, &RVF_IMPLIED,
-    &RVM_IMPLIED, &RVV_IMPLIED, &RVG_IMPLIED, NULL
+    &RVM_IMPLIED, &RVV_IMPLIED, &RVG_IMPLIED,
+    &RVB_IMPLIED, NULL
 };
 
 RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = {
target/riscv/tcg/tcg-cpu.c +0 -33
index 840ef82350d..7255f73f5a2 100644
@@ -532,35 +532,6 @@ static void riscv_cpu_update_named_features(RISCVCPU *cpu)
     cpu->cfg.ext_ziccrse = cpu->cfg.has_priv_1_11;
 }
 
-static void riscv_cpu_validate_b(RISCVCPU *cpu)
-{
-    const char *warn_msg = "RVB mandates disabled extension %s";
-
-    if (!cpu->cfg.ext_zba) {
-        if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zba))) {
-            cpu->cfg.ext_zba = true;
-        } else {
-            warn_report(warn_msg, "zba");
-        }
-    }
-
-    if (!cpu->cfg.ext_zbb) {
-        if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zbb))) {
-            cpu->cfg.ext_zbb = true;
-        } else {
-            warn_report(warn_msg, "zbb");
-        }
-    }
-
-    if (!cpu->cfg.ext_zbs) {
-        if (!cpu_cfg_ext_is_user_set(CPU_CFG_OFFSET(ext_zbs))) {
-            cpu->cfg.ext_zbs = true;
-        } else {
-            warn_report(warn_msg, "zbs");
-        }
-    }
-}
-
 /*
  * Check consistency between chosen extensions while setting
  * cpu->cfg accordingly.
@@ -571,10 +542,6 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)
     CPURISCVState *env = &cpu->env;
     Error *local_err = NULL;
 
-    if (riscv_has_ext(env, RVB)) {
-        riscv_cpu_validate_b(cpu);
-    }
-
     if (riscv_has_ext(env, RVI) && riscv_has_ext(env, RVE)) {
         error_setg(errp,
                    "I and E extensions are incompatible");
Signature · 1 line
2.43.0
#4 lore raw
In reply to [email protected]
Quoted 9 lines · On Tue, Apr 21, 2026 at 5:42 PM <[email protected]> wrote:

From: Frank Chang <[email protected]>

Add the implied rules for G and B extensions to enable the implied extensions.

Changelog:
v2: Remove riscv_cpu_validate_g() and riscv_cpu_validate_b() as the
implied extensions can be enabled by the implied rules.

Can they though?

The original commit 8b3b3451052cd7a115c9352de1bb8c74ee54a62e says

For every RVG MISA bit (IMAFD), zicsr and zifencei, the logic then becomes:

- if enabled, do nothing;
- if disabled and not user set, enable it;
- if disabled and user set, throw a warning that it's a RVG mandatory extension.

Isn't that what we want?

Alistair

Quoted 15 lines

Frank Chang (1):
target/riscv: Add standard B extension implied rule

Jim Shu (1):
target/riscv: Add the implied rule for G extension

 target/riscv/cpu.c         | 26 ++++++++++++-
 target/riscv/tcg/tcg-cpu.c | 77 --------------------------------------
 2 files changed, 25 insertions(+), 78 deletions(-)

--
2.43.0

#5 lore raw
Quoted 23 lines · Alistair Francis
Quoted 9 lines · On Tue, Apr 21, 2026 at 5:42 PM <[email protected]> wrote:

From: Frank Chang <[email protected]>

Add the implied rules for G and B extensions to enable the implied extensions.

Changelog:
v2: Remove riscv_cpu_validate_g() and riscv_cpu_validate_b() as the
implied extensions can be enabled by the implied rules.

Can they though?

The original commit 8b3b3451052cd7a115c9352de1bb8c74ee54a62e says

For every RVG MISA bit (IMAFD), zicsr and zifencei, the logic then becomes:

- if enabled, do nothing;
- if disabled and not user set, enable it;
- if disabled and user set, throw a warning that it's a RVG mandatory extension.

Quoted 5 lines

Isn't that what we want?

Alistair

Hi Alistair,

Thanks for the review.
You are right. I think the proper way is to remove the auto-enables in riscv_cpu_validate_g() and riscv_cpu_validate_b() only, but keep the warnings.

Implied rule won't enable the extensions that are set by the user. So, if users disable the implied extension explicitly, we should warn them without enabling the implied extension.

For examples,

$ -cpu rv64,g=true,a=false
qemu-system-riscv64: warning: RVG mandates disabled extension a
$ -cpu rv64,g=true,zicsr=false
qemu-system-riscv64: warning: RVG mandates disabled extension zicsr
qemu-system-riscv64: F extension requires Zicsr
$ -cpu rv64,b=true,zbb=false
qemu-system-riscv64: warning: RVB mandates disabled extension zbb

However, if user sets"g=true" or "b=true", the implied extensions will be enabled by the implied rules.

I will fix it and send out a new patchset.

Regards,
Frank Chang

Quoted 17 lines
Quoted 16 lines

Frank Chang (1):
target/riscv: Add standard B extension implied rule

Jim Shu (1):
target/riscv: Add the implied rule for G extension

 target/riscv/cpu.c         | 26 ++++++++++++-
 target/riscv/tcg/tcg-cpu.c | 77 --------------------------------------
 2 files changed, 25 insertions(+), 78 deletions(-)

--
2.43.0