Skip to contentSkip to conversation

Add Zihintpause support

PATCH In review Dao Lu 4 participants 7 messages May 10, 2022 – May 12, 2022 RISC-V

lore
Share & tools
lore

Dao Lu

author
#0 lore raw

cover Add Zihintpause support

This patch adds RISC-V Zihintpause support. The extension is set to be enabled by default and opcode has been added to insn32.decode.

Added trans_pause for TCG to mainly be a no-op and break reservation.

The change can also be found in:
https://github.com/dlu42/qemu/tree/zihintpause_support_v1

Tested along with pause support added to cpu_relax function for linux, the changes I made to linux to test can be found here:
https://github.com/dlu42/linux/tree/pause_support_v1

Dao Lu (1):
Add Zihintpause support

 target/riscv/cpu.c                      |  2 ++
 target/riscv/cpu.h                      |  1 +
 target/riscv/insn32.decode              |  7 ++++++-
 target/riscv/insn_trans/trans_rvi.c.inc | 18 ++++++++++++++++++
 4 files changed, 27 insertions(+), 1 deletion(-)
Signature · 1 line
2.36.0
author
#1 lore raw

PATCH 1/1 Add Zihintpause support

Added support for RISC-V PAUSE instruction from Zihintpause extension, enabeld by default.

---
 target/riscv/cpu.c                      |  2 ++
 target/riscv/cpu.h                      |  1 +
 target/riscv/insn32.decode              |  7 ++++++-
 target/riscv/insn_trans/trans_rvi.c.inc | 18 ++++++++++++++++++
 4 files changed, 27 insertions(+), 1 deletion(-)
target/riscv/cpu.c +2 -0
index ccacdee215..183fb37fdf 100644
@@ -825,6 +825,7 @@ static Property riscv_cpu_properties[] = {
     DEFINE_PROP_BOOL("Counters", RISCVCPU, cfg.ext_counters, true),
     DEFINE_PROP_BOOL("Zifencei", RISCVCPU, cfg.ext_ifencei, true),
     DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true),
+    DEFINE_PROP_BOOL("Zihintpause", RISCVCPU, cfg.ext_zihintpause, true),
     DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),
     DEFINE_PROP_BOOL("Zfhmin", RISCVCPU, cfg.ext_zfhmin, false),
     DEFINE_PROP_BOOL("Zve32f", RISCVCPU, cfg.ext_zve32f, false),
@@ -996,6 +997,7 @@ static void riscv_isa_string_ext(RISCVCPU *cpu, char **isa_str, int max_str_len)
      *    extensions by an underscore.
      */
     struct isa_ext_data isa_edata_arr[] = {
+        ISA_EDATA_ENTRY(zihintpause, ext_zihintpause),
         ISA_EDATA_ENTRY(zfh, ext_zfh),
         ISA_EDATA_ENTRY(zfhmin, ext_zfhmin),
         ISA_EDATA_ENTRY(zfinx, ext_zfinx),
target/riscv/cpu.h +1 -0
index fe6c9a2c92..e466a04a59 100644
@@ -394,6 +394,7 @@ struct RISCVCPUConfig {
     bool ext_counters;
     bool ext_ifencei;
     bool ext_icsr;
+    bool ext_zihintpause;
     bool ext_svinval;
     bool ext_svnapot;
     bool ext_svpbmt;
target/riscv/insn32.decode +6 -1
index 4033565393..595fdcdad8 100644
@@ -149,7 +149,12 @@ srl      0000000 .....    ..... 101 ..... 0110011 @r
 sra      0100000 .....    ..... 101 ..... 0110011 @r
 or       0000000 .....    ..... 110 ..... 0110011 @r
 and      0000000 .....    ..... 111 ..... 0110011 @r
-fence    ---- pred:4 succ:4 ----- 000 ----- 0001111
+
+{
+  pause  0000 0001   0000   00000 000 00000 0001111
+  fence  ---- pred:4 succ:4 ----- 000 ----- 0001111
+}
+
 fence_i  ---- ----   ----   ----- 001 ----- 0001111
 csrrw    ............     ..... 001 ..... 1110011 @csr
 csrrs    ............     ..... 010 ..... 1110011 @csr
target/riscv/insn_trans/trans_rvi.c.inc +18 -0
index f1342f30f8..528c30c9a2 100644
@@ -18,6 +18,12 @@
  * this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define REQUIRE_ZIHINTPAUSE(ctx) do { \
+    if (!ctx->cfg_ptr->ext_zihintpause) {      \
+        return false;         \
+    }                         \
+} while (0)
+
 static bool trans_illegal(DisasContext *ctx, arg_empty *a)
 {
     gen_exception_illegal(ctx);
@@ -796,6 +802,18 @@ static bool trans_srad(DisasContext *ctx, arg_srad *a)
     return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl, NULL);
 }
 
+static bool trans_pause(DisasContext *ctx, arg_pause *a)
+{
+    REQUIRE_ZIHINTPAUSE(ctx);
+
+    /*
+     * PAUSE is a no-op in QEMU,
+     * however we need to clear the reservation
+     */
+    tcg_gen_movi_tl(load_res, -1);
+
+    return true;
+}
 
 static bool trans_fence(DisasContext *ctx, arg_fence *a)
 {
Signature · 1 line
2.36.0
#2 lore raw

target/riscv/insn_trans/trans_rvi.c.inc static bool trans_srad(DisasContext *ctx, arg_srad *a) · line 813

Quoted 9 lines · Dao Lu
target/riscv/insn_trans/trans_rvi.c.inc +9 -0
+static bool trans_pause(DisasContext *ctx, arg_pause *a)
+{
+    REQUIRE_ZIHINTPAUSE(ctx);
+
+    /*
+     * PAUSE is a no-op in QEMU,
+     * however we need to clear the reservation
+     */
+    tcg_gen_movi_tl(load_res, -1);

I recommend ending the TB and returning to the main loop. When running in round-robin mode, this will yield to the next cpu.

r~

author
#3 lore raw

Thanks Richard! I have fixed it following your advice:

(quoted diff) +0 -0

b/target/riscv/insn_trans/trans_rvi.c.inc
index 528c30c9a2..acd2548e9b 100644

target/riscv/insn_trans/trans_rvi.c.inc +0 -0
@@ -808,9 +808,13 @@ static bool trans_pause(DisasContext *ctx, arg_pause

*a)

/*
* PAUSE is a no-op in QEMU,

-     * however we need to clear the reservation
+     * however we need to clear the reservation,
+     * end the TB and return to main loop
      */
     tcg_gen_movi_tl(load_res, -1);
+    gen_set_pc_imm(ctx, ctx->pc_succ_insn);
+    tcg_gen_exit_tb(NULL, 0);
+    ctx->base.is_jmp = DISAS_NORETURN;
     return true;

}

I will wait a bit to see if there are any more comments.

Quoted 18 lines · Richard Henderson

Review

Quoted 9 lines · Dao Lu
(quoted diff) +9 -0
+static bool trans_pause(DisasContext *ctx, arg_pause *a)
+{
+    REQUIRE_ZIHINTPAUSE(ctx);
+
+    /*
+     * PAUSE is a no-op in QEMU,
+     * however we need to clear the reservation
+     */
+    tcg_gen_movi_tl(load_res, -1);

I recommend ending the TB and returning to the main loop. When running in round-robin
mode, this will yield to the next cpu.

r~

Tested-by on v1 1/1
#4 lore raw
In reply to Dao Lu

Am Dienstag, 10. Mai 2022, 08:42:31 CEST schrieb Dao Lu:

Quoted 4 lines

Added support for RISC-V PAUSE instruction from Zihintpause extension, enabeld by default.

This patch with your fixup applied to it and of course a matching kernel:

Quoted 94 lines
---
 target/riscv/cpu.c                      |  2 ++
 target/riscv/cpu.h                      |  1 +
 target/riscv/insn32.decode              |  7 ++++++-
 target/riscv/insn_trans/trans_rvi.c.inc | 18 ++++++++++++++++++
 4 files changed, 27 insertions(+), 1 deletion(-)
target/riscv/cpu.c +2 -0
index ccacdee215..183fb37fdf 100644
@@ -825,6 +825,7 @@ static Property riscv_cpu_properties[] = {
     DEFINE_PROP_BOOL("Counters", RISCVCPU, cfg.ext_counters, true),
     DEFINE_PROP_BOOL("Zifencei", RISCVCPU, cfg.ext_ifencei, true),
     DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true),
+    DEFINE_PROP_BOOL("Zihintpause", RISCVCPU, cfg.ext_zihintpause, true),
     DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),
     DEFINE_PROP_BOOL("Zfhmin", RISCVCPU, cfg.ext_zfhmin, false),
     DEFINE_PROP_BOOL("Zve32f", RISCVCPU, cfg.ext_zve32f, false),
@@ -996,6 +997,7 @@ static void riscv_isa_string_ext(RISCVCPU *cpu, char **isa_str, int max_str_len)
      *    extensions by an underscore.
      */
     struct isa_ext_data isa_edata_arr[] = {
+        ISA_EDATA_ENTRY(zihintpause, ext_zihintpause),
         ISA_EDATA_ENTRY(zfh, ext_zfh),
         ISA_EDATA_ENTRY(zfhmin, ext_zfhmin),
         ISA_EDATA_ENTRY(zfinx, ext_zfinx),
target/riscv/cpu.h +1 -0
index fe6c9a2c92..e466a04a59 100644
@@ -394,6 +394,7 @@ struct RISCVCPUConfig {
     bool ext_counters;
     bool ext_ifencei;
     bool ext_icsr;
+    bool ext_zihintpause;
     bool ext_svinval;
     bool ext_svnapot;
     bool ext_svpbmt;
target/riscv/insn32.decode +6 -1
index 4033565393..595fdcdad8 100644
@@ -149,7 +149,12 @@ srl      0000000 .....    ..... 101 ..... 0110011 @r
 sra      0100000 .....    ..... 101 ..... 0110011 @r
 or       0000000 .....    ..... 110 ..... 0110011 @r
 and      0000000 .....    ..... 111 ..... 0110011 @r
-fence    ---- pred:4 succ:4 ----- 000 ----- 0001111
+
+{
+  pause  0000 0001   0000   00000 000 00000 0001111
+  fence  ---- pred:4 succ:4 ----- 000 ----- 0001111
+}
+
 fence_i  ---- ----   ----   ----- 001 ----- 0001111
 csrrw    ............     ..... 001 ..... 1110011 @csr
 csrrs    ............     ..... 010 ..... 1110011 @csr
target/riscv/insn_trans/trans_rvi.c.inc +18 -0
index f1342f30f8..528c30c9a2 100644
@@ -18,6 +18,12 @@
  * this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define REQUIRE_ZIHINTPAUSE(ctx) do { \
+    if (!ctx->cfg_ptr->ext_zihintpause) {      \
+        return false;         \
+    }                         \
+} while (0)
+
 static bool trans_illegal(DisasContext *ctx, arg_empty *a)
 {
     gen_exception_illegal(ctx);
@@ -796,6 +802,18 @@ static bool trans_srad(DisasContext *ctx, arg_srad *a)
     return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl, NULL);
 }
 
+static bool trans_pause(DisasContext *ctx, arg_pause *a)
+{
+    REQUIRE_ZIHINTPAUSE(ctx);
+
+    /*
+     * PAUSE is a no-op in QEMU,
+     * however we need to clear the reservation
+     */
+    tcg_gen_movi_tl(load_res, -1);
+
+    return true;
+}
 
 static bool trans_fence(DisasContext *ctx, arg_fence *a)
 {
#5 lore raw
In reply to Dao Lu
Quoted 16 lines · Dao Lu

Added support for RISC-V PAUSE instruction from Zihintpause extension, enabeld by default.

---
 target/riscv/cpu.c                      |  2 ++
 target/riscv/cpu.h                      |  1 +
 target/riscv/insn32.decode              |  7 ++++++-
 target/riscv/insn_trans/trans_rvi.c.inc | 18 ++++++++++++++++++
 4 files changed, 27 insertions(+), 1 deletion(-)
target/riscv/cpu.c +0 -0
index ccacdee215..183fb37fdf 100644

target/riscv/cpu.c static Property riscv_cpu_properties[] = { · line 828

Quoted 4 lines
target/riscv/cpu.c +1 -0
@@ -825,6 +825,7 @@ static Property riscv_cpu_properties[] = {
     DEFINE_PROP_BOOL("Counters", RISCVCPU, cfg.ext_counters, true),
     DEFINE_PROP_BOOL("Zifencei", RISCVCPU, cfg.ext_ifencei, true),
     DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true),
+    DEFINE_PROP_BOOL("Zihintpause", RISCVCPU, cfg.ext_zihintpause, true),

This is not your fault but mixed upper camel case and lower case is not a good situation (e.g. "Zihintpause" and "zba"). I will raise a separate issue on qemu-riscv mailing list (no changes requested).

Quoted 46 lines
target/riscv/cpu.c +1 -0
     DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),
     DEFINE_PROP_BOOL("Zfhmin", RISCVCPU, cfg.ext_zfhmin, false),
     DEFINE_PROP_BOOL("Zve32f", RISCVCPU, cfg.ext_zve32f, false),
@@ -996,6 +997,7 @@ static void riscv_isa_string_ext(RISCVCPU *cpu, char **isa_str, int max_str_len)
      *    extensions by an underscore.
      */
     struct isa_ext_data isa_edata_arr[] = {
+        ISA_EDATA_ENTRY(zihintpause, ext_zihintpause),
         ISA_EDATA_ENTRY(zfh, ext_zfh),
         ISA_EDATA_ENTRY(zfhmin, ext_zfhmin),
         ISA_EDATA_ENTRY(zfinx, ext_zfinx),
target/riscv/cpu.h +1 -0
index fe6c9a2c92..e466a04a59 100644
@@ -394,6 +394,7 @@ struct RISCVCPUConfig {
     bool ext_counters;
     bool ext_ifencei;
     bool ext_icsr;
+    bool ext_zihintpause;
     bool ext_svinval;
     bool ext_svnapot;
     bool ext_svpbmt;
target/riscv/insn32.decode +6 -1
index 4033565393..595fdcdad8 100644
@@ -149,7 +149,12 @@ srl      0000000 .....    ..... 101 ..... 0110011 @r
 sra      0100000 .....    ..... 101 ..... 0110011 @r
 or       0000000 .....    ..... 110 ..... 0110011 @r
 and      0000000 .....    ..... 111 ..... 0110011 @r
-fence    ---- pred:4 succ:4 ----- 000 ----- 0001111
+
+{
+  pause  0000 0001   0000   00000 000 00000 0001111
+  fence  ---- pred:4 succ:4 ----- 000 ----- 0001111
+}
+
 fence_i  ---- ----   ----   ----- 001 ----- 0001111
 csrrw    ............     ..... 001 ..... 1110011 @csr
 csrrs    ............     ..... 010 ..... 1110011 @csr
target/riscv/insn_trans/trans_rvi.c.inc +0 -0
index f1342f30f8..528c30c9a2 100644

target/riscv/insn_trans/trans_rvi.c.inc line 25

Quoted 8 lines
target/riscv/insn_trans/trans_rvi.c.inc +5 -0
@@ -18,6 +18,12 @@
  * this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define REQUIRE_ZIHINTPAUSE(ctx) do { \
+    if (!ctx->cfg_ptr->ext_zihintpause) {      \
+        return false;         \
+    }                         \
+} while (0)

I think this macro is too much for one instruction. How about expanding this on trans_pause' function like trans_fence_i'?

Other than that, it LGTM.

Tsukasa,

Quoted 23 lines
target/riscv/insn_trans/trans_rvi.c.inc +13 -0
+
 static bool trans_illegal(DisasContext *ctx, arg_empty *a)
 {
     gen_exception_illegal(ctx);
@@ -796,6 +802,18 @@ static bool trans_srad(DisasContext *ctx, arg_srad *a)
     return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl, NULL);
 }
 
+static bool trans_pause(DisasContext *ctx, arg_pause *a)
+{
+    REQUIRE_ZIHINTPAUSE(ctx);
+
+    /*
+     * PAUSE is a no-op in QEMU,
+     * however we need to clear the reservation
+     */
+    tcg_gen_movi_tl(load_res, -1);
+
+    return true;
+}
 
 static bool trans_fence(DisasContext *ctx, arg_fence *a)
 {
author
#6 lore raw

Thanks to Heiko and Tsukasa.

I have made the following changes:

(quoted diff) +0 -0

b/target/riscv/insn_trans/trans_rvi.c.inc
index acd2548e9b..ca75e05f4b 100644

target/riscv/insn_trans/trans_rvi.c.inc +3 -7
@@ -18,12 +18,6 @@
  * this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#define REQUIRE_ZIHINTPAUSE(ctx) do { \
-    if (!ctx->cfg_ptr->ext_zihintpause) {      \
-        return false;         \
-    }                         \
-} while (0)
-
 static bool trans_illegal(DisasContext *ctx, arg_empty *a)
 {
     gen_exception_illegal(ctx);
@@ -804,7 +798,9 @@ static bool trans_srad(DisasContext *ctx, arg_srad *a)
 
 static bool trans_pause(DisasContext *ctx, arg_pause *a)
 {
-    REQUIRE_ZIHINTPAUSE(ctx);
+    if (!ctx->cfg_ptr->ext_zihintpause) {
+        return false;
+    }
 
     /*
      * PAUSE is a no-op in QEMU,
Quoted 113 lines · Tsukasa OI
Quoted 16 lines · Dao Lu

Added support for RISC-V PAUSE instruction from Zihintpause extension, enabeld by default.

---
 target/riscv/cpu.c                      |  2 ++
 target/riscv/cpu.h                      |  1 +
 target/riscv/insn32.decode              |  7 ++++++-
 target/riscv/insn_trans/trans_rvi.c.inc | 18 ++++++++++++++++++
 4 files changed, 27 insertions(+), 1 deletion(-)
target/riscv/cpu.c +0 -0
index ccacdee215..183fb37fdf 100644

target/riscv/cpu.c static Property riscv_cpu_properties[] = { · line 828

Quoted 4 lines
target/riscv/cpu.c +1 -0
@@ -825,6 +825,7 @@ static Property riscv_cpu_properties[] = {
     DEFINE_PROP_BOOL("Counters", RISCVCPU, cfg.ext_counters, true),
     DEFINE_PROP_BOOL("Zifencei", RISCVCPU, cfg.ext_ifencei, true),
     DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true),
+    DEFINE_PROP_BOOL("Zihintpause", RISCVCPU, cfg.ext_zihintpause, true),

This is not your fault but mixed upper camel case and lower case is not a good situation (e.g. "Zihintpause" and "zba"). I will raise a separate issue on qemu-riscv mailing list (no changes requested).

Quoted 46 lines
target/riscv/cpu.c +1 -0
     DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),
     DEFINE_PROP_BOOL("Zfhmin", RISCVCPU, cfg.ext_zfhmin, false),
     DEFINE_PROP_BOOL("Zve32f", RISCVCPU, cfg.ext_zve32f, false),
@@ -996,6 +997,7 @@ static void riscv_isa_string_ext(RISCVCPU *cpu, char **isa_str, int max_str_len)
      *    extensions by an underscore.
      */
     struct isa_ext_data isa_edata_arr[] = {
+        ISA_EDATA_ENTRY(zihintpause, ext_zihintpause),
         ISA_EDATA_ENTRY(zfh, ext_zfh),
         ISA_EDATA_ENTRY(zfhmin, ext_zfhmin),
         ISA_EDATA_ENTRY(zfinx, ext_zfinx),
target/riscv/cpu.h +1 -0
index fe6c9a2c92..e466a04a59 100644
@@ -394,6 +394,7 @@ struct RISCVCPUConfig {
     bool ext_counters;
     bool ext_ifencei;
     bool ext_icsr;
+    bool ext_zihintpause;
     bool ext_svinval;
     bool ext_svnapot;
     bool ext_svpbmt;
target/riscv/insn32.decode +6 -1
index 4033565393..595fdcdad8 100644
@@ -149,7 +149,12 @@ srl      0000000 .....    ..... 101 ..... 0110011 @r
 sra      0100000 .....    ..... 101 ..... 0110011 @r
 or       0000000 .....    ..... 110 ..... 0110011 @r
 and      0000000 .....    ..... 111 ..... 0110011 @r
-fence    ---- pred:4 succ:4 ----- 000 ----- 0001111
+
+{
+  pause  0000 0001   0000   00000 000 00000 0001111
+  fence  ---- pred:4 succ:4 ----- 000 ----- 0001111
+}
+
 fence_i  ---- ----   ----   ----- 001 ----- 0001111
 csrrw    ............     ..... 001 ..... 1110011 @csr
 csrrs    ............     ..... 010 ..... 1110011 @csr
target/riscv/insn_trans/trans_rvi.c.inc +0 -0
index f1342f30f8..528c30c9a2 100644

target/riscv/insn_trans/trans_rvi.c.inc line 25

Quoted 8 lines
target/riscv/insn_trans/trans_rvi.c.inc +5 -0
@@ -18,6 +18,12 @@
  * this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define REQUIRE_ZIHINTPAUSE(ctx) do { \
+    if (!ctx->cfg_ptr->ext_zihintpause) {      \
+        return false;         \
+    }                         \
+} while (0)

I think this macro is too much for one instruction. How about expanding this on trans_pause' function like trans_fence_i'?

Other than that, it LGTM.

Tsukasa,

Quoted 23 lines
target/riscv/insn_trans/trans_rvi.c.inc +13 -0
+
 static bool trans_illegal(DisasContext *ctx, arg_empty *a)
 {
     gen_exception_illegal(ctx);
@@ -796,6 +802,18 @@ static bool trans_srad(DisasContext *ctx, arg_srad *a)
     return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl, NULL);
 }
 
+static bool trans_pause(DisasContext *ctx, arg_pause *a)
+{
+    REQUIRE_ZIHINTPAUSE(ctx);
+
+    /*
+     * PAUSE is a no-op in QEMU,
+     * however we need to clear the reservation
+     */
+    tcg_gen_movi_tl(load_res, -1);
+
+    return true;
+}
 
 static bool trans_fence(DisasContext *ctx, arg_fence *a)
 {