It seems like we can't write RVG into misa.  The bit has been marked as
"Reserved" for priv specs 1.11+, i.e. it does not represent the
extension pack "IMAFD_Zicsr_Zifencei".  The official explanation, at
least from what I could find, is that setting RVG is redundant with
reading riscv,isa and/or other extension discovery mechanisms.

To make matters more interesting, 'g' is still supposed to be present in
riscv,isa.  Toolchains will keep using 'g' in their 'march' normally
too.  So aside from sparing one bit in the MISA every other use of RVG
is still valid.

We have a good chunk of code that uses RVG as a synonym for that
extension pack and, for now, I don't see a reason to refactor it.  We
will cross that bridge when/if RVG acquires another official use.

For now, if we're running priv spec 1.11+, filter the bit out of misa.
Update the bit description in misa_ext_info_arr too for clarity.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4811
Signed-off-by: Daniel Henrique Barboza <[email protected]>
---
 target/riscv/cpu.c                   |  7 +++-
 target/riscv/tcg/csr.c               | 17 ++++++++++
 tests/tcg/riscv64/system/meson.build |  7 ++++
 tests/tcg/riscv64/test-misa-G.S      | 51 ++++++++++++++++++++++++++++
 4 files changed, 81 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/riscv64/test-misa-G.S

diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
index 15c27f4ce4..3b32d79609 100644
--- a/target/riscv/cpu.c
+++ b/target/riscv/cpu.c
@@ -1620,7 +1620,12 @@ static const MISAExtInfo misa_ext_info_arr[] = {
     MISA_EXT_INFO(RVU, "u", "User-level instructions"),
     MISA_EXT_INFO(RVH, "h", "Hypervisor"),
     MISA_EXT_INFO(RVV, "v", "Vector operations"),
-    MISA_EXT_INFO(RVG, "g", "General purpose (IMAFD_Zicsr_Zifencei)"),
+    /*
+     * As of priv versions 1.11+ the spec marks RVG as Reserved.
+     * Internally we'll keep using it as a shortcut for
+     * IMAFD_Zicsr_Zifencei.
+     */
+    MISA_EXT_INFO(RVG, "g", "Reserved"),
     MISA_EXT_INFO(RVB, "b", "Bit manipulation (Zba_Zbb_Zbs)")
 };
 
diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..5f8f0dc8a8 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -2200,6 +2200,15 @@ static RISCVException read_misa(CPURISCVState *env, int 
csrno,
     }
 
     *val = misa | env->misa_ext;
+
+     /*
+      * RVG is reserved for priv > 1.10 but we're setting the
+      * bit internally as a shortcut for IMAFD_Zicsr_Zifencei.
+      */
+    if (env->priv_ver > PRIV_VERSION_1_10_0) {
+        *val &= ~RVG;
+    }
+
     return RISCV_EXCP_NONE;
 }
 
@@ -2228,6 +2237,14 @@ static RISCVException write_misa(CPURISCVState *env, int 
csrno,
         return RISCV_EXCP_NONE;
     }
 
+     /*
+      * RVG is reserved for priv > 1.10 but we're setting the
+      * bit internally as a shortcut for IMAFD_Zicsr_Zifencei.
+      */
+    if (env->priv_ver > PRIV_VERSION_1_10_0) {
+        val &= ~RVG;
+    }
+
     /* Mask extensions that are not supported by this hart */
     val &= env->misa_ext_mask;
 
diff --git a/tests/tcg/riscv64/system/meson.build 
b/tests/tcg/riscv64/system/meson.build
index 3c8b018d9e..8e7c29a7fa 100644
--- a/tests/tcg/riscv64/system/meson.build
+++ b/tests/tcg/riscv64/system/meson.build
@@ -89,6 +89,13 @@ tests += {
   }
 }
 
+tests += {
+  'test-misa-G.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  }
+}
+
 if 'qemu-system-riscv64' in emulators
   tcg_tests += {
     'riscv64-softmmu': {
diff --git a/tests/tcg/riscv64/test-misa-G.S b/tests/tcg/riscv64/test-misa-G.S
new file mode 100644
index 0000000000..45a9ca720b
--- /dev/null
+++ b/tests/tcg/riscv64/test-misa-G.S
@@ -0,0 +1,51 @@
+/*
+ * Test if RVG is being set in misa.  See Gitlab 4811 for more info.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+.option norvc
+       .text
+       .global _start
+
+_start:
+       lla     t0, trap
+       csrw    mtvec, t0
+
+       csrr    t0, misa
+       li      t1, 1
+       slli    t1, t1, 6
+       and     t1, t1, t0
+       bnez    t1, _fail
+
+       li      a0, 0
+       j       _exit
+
+_fail:
+       li      a0, 1
+       j       _exit
+
+trap:
+       # Any trap is a fail code 1
+       li      a0, 1
+
+# Exit code in a0
+_exit:
+       lla     a1, semiargs
+       li      t0, 0x20026     # ADP_Stopped_ApplicationExit
+       sd      t0, 0(a1)
+       sd      a0, 8(a1)
+       li      a0, 0x20        # TARGET_SYS_EXIT_EXTENDED
+
+       # Semihosting call sequence
+.balign        16
+       slli    zero, zero, 0x1f
+       ebreak
+       srai    zero, zero, 0x7
+       j       .
+
+       .data
+       .balign 16
+semiargs:
+       .space  16
-- 
2.43.0


Reply via email to