Skip to contentSkip to conversation

target/riscv: Add Zihintpause support

PATCH Applied Dao Lu 5 participants 7 messages Jul 25, 2022 – Sep 3 RISC-V

lore
Share & tools
lore

Dao Lu

author
#0 lore raw

v5 cover target/riscv: 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 to exit the TB and return to main loop.

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

--------
Changelog:

v1 -> v2
1. Pause now also exit the TB and return to main loop
2. Move the REQUIRE_ZIHINTPAUSE macro inside the trans_pause function

v2 -> v3
No changes, v2 was lost from the list

v3 -> v4
No longer break the reservation in trans_pause

v4 -> v5
Rabase on top of https://github.com/alistair23/qemu/tree/riscv-to-apply.next

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 | 16 ++++++++++++++++
 4 files changed, 25 insertions(+), 1 deletion(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v5 1/1 target/riscv: Add Zihintpause support

Added support for RISC-V PAUSE instruction from Zihintpause extension, enabled 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 | 16 ++++++++++++++++
 4 files changed, 25 insertions(+), 1 deletion(-)
target/riscv/cpu.c +2 -0
index 966e5f2dd7..d4635c7df4 100644
@@ -73,6 +73,7 @@ static const struct isa_ext_data isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(v, false, PRIV_VERSION_1_12_0, ext_v),
     ISA_EXT_DATA_ENTRY(zicsr, true, PRIV_VERSION_1_10_0, ext_icsr),
     ISA_EXT_DATA_ENTRY(zifencei, true, PRIV_VERSION_1_10_0, ext_ifencei),
+    ISA_EXT_DATA_ENTRY(zihintpause, true, PRIV_VERSION_1_10_0, ext_zihintpause),
     ISA_EXT_DATA_ENTRY(zfh, true, PRIV_VERSION_1_12_0, ext_zfh),
     ISA_EXT_DATA_ENTRY(zfhmin, true, PRIV_VERSION_1_12_0, ext_zfhmin),
     ISA_EXT_DATA_ENTRY(zfinx, true, PRIV_VERSION_1_12_0, ext_zfinx),
@@ -987,6 +988,7 @@ static Property riscv_cpu_extensions[] = {
     DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16),
     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),
target/riscv/cpu.h +1 -0
index 561d7fa92c..4be4b82a83 100644
@@ -426,6 +426,7 @@ struct RISCVCPUConfig {
     bool ext_zkt;
     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 +16 -0
index ca8e3d1ea1..c49dbec0eb 100644
@@ -792,6 +792,22 @@ 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)
+{
+    if (!ctx->cfg_ptr->ext_zihintpause) {
+        return false;
+    }
+
+    /*
+     * PAUSE is a no-op in QEMU,
+     * end the TB and return to main loop
+     */
+    gen_set_pc_imm(ctx, ctx->pc_succ_insn);
+    tcg_gen_exit_tb(NULL, 0);
+    ctx->base.is_jmp = DISAS_NORETURN;
+
+    return true;
+}
 
 static bool trans_fence(DisasContext *ctx, arg_fence *a)
 {
Signature · 1 line
2.25.1
#2 lore raw

(now committed as 4696f0ab5c436ed53567ce6baec67c921d9b70ae)

Quoted 24 lines · Dao Lu

Added support for RISC-V PAUSE instruction from Zihintpause extension, enabled 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 | 16 ++++++++++++++++
  4 files changed, 25 insertions(+), 1 deletion(-)
target/riscv/insn_trans/trans_rvi.c.inc +2 -0
index ca8e3d1ea1..c49dbec0eb 100644
@@ -792,6 +792,22 @@ 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)
+{

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

Quoted 10 lines
target/riscv/insn_trans/trans_rvi.c.inc +10 -0
+    if (!ctx->cfg_ptr->ext_zihintpause) {
+        return false;
+    }
+
+    /*
+     * PAUSE is a no-op in QEMU,
+     * end the TB and return to main loop
+     */
+    gen_set_pc_imm(ctx, ctx->pc_succ_insn);
+    tcg_gen_exit_tb(NULL, 0);

Looking at this because I'm auditing the overall exception scheduling.

This is a clever way to quickly return the inner vcpu dispatch loop, but doing so we can not reach the outter loop to yield to another vcpu, so I wonder if we shouldn't use EXCP_YIELD here. The problem has be unnoticed because most of us use MTTCG (the default) where EXCP_YIELD is irrelevant, but I expect this to be problematic on single threaded round robin TCG (although not reproduced). I noticed that when working on the hybrid accelerator project -- which is not merged yet -- where we use MTTCG and EXCP_YIELD is returned by hw accelerators. I'm happy using this simpler helper, but since this is a big logical change and I'm not familiar with RISCV I'm am not certain:

+void helper_pause(CPURISCVState *env)
+{
+ CPUState *cs = env_cpu(env);
+
+ cs->exception_index = EXCP_YIELD;
+ cpu_loop_exit(cs);
+}

Quoted 7 lines
target/riscv/insn_trans/trans_rvi.c.inc +4 -0
+    ctx->base.is_jmp = DISAS_NORETURN;
+
+    return true;
+}
 
  static bool trans_fence(DisasContext *ctx, arg_fence *a)
  {
#3 lore raw
Quoted 57 lines · Philippe Mathieu-Daudé

(now committed as 4696f0ab5c436ed53567ce6baec67c921d9b70ae)

Quoted 27 lines · Dao Lu

Added support for RISC-V PAUSE instruction from Zihintpause extension,
enabled 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 | 16 ++++++++++++++++

  4 files changed, 25 insertions(+), 1 deletion(-)
target/riscv/insn_trans/trans_rvi.c.inc +0 -0
index ca8e3d1ea1..c49dbec0eb 100644
@@ -792,6 +792,22 @@ static bool trans_srad(DisasContext *ctx,
 arg_srad *a)
       return gen_shift(ctx, a, EXT_SIGN, tcg_gen_sar_tl, NULL);
   }

target/riscv/insn_trans/trans_rvi.c.inc +2 -0
+static bool trans_pause(DisasContext *ctx, arg_pause *a)
+{

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

Quoted 10 lines
target/riscv/insn_trans/trans_rvi.c.inc +10 -0
+    if (!ctx->cfg_ptr->ext_zihintpause) {
+        return false;
+    }
+
+    /*
+     * PAUSE is a no-op in QEMU,
+     * end the TB and return to main loop
+     */
+    gen_set_pc_imm(ctx, ctx->pc_succ_insn);
+    tcg_gen_exit_tb(NULL, 0);

Looking at this because I'm auditing the overall exception scheduling.

This is a clever way to quickly return the inner vcpu dispatch loop, but doing so we can not reach the outter loop to yield to another vcpu,
so I wonder if we shouldn't use EXCP_YIELD here. The problem has be unnoticed because most of us use MTTCG (the default) where EXCP_YIELD is irrelevant, but I expect this to be problematic on single threaded round robin TCG (although not reproduced). I noticed that when working
on the hybrid accelerator project -- which is not merged yet -- where we use MTTCG and EXCP_YIELD is returned by hw accelerators. I'm happy using this simpler helper, but since this is a big logical change and I'm not familiar with RISCV I'm am not certain:

Quoted 7 lines

   +void helper_pause(CPURISCVState *env)
   +{
   +    CPUState *cs = env_cpu(env);
   +
   +    cs->exception_index = EXCP_YIELD;
   +    cpu_loop_exit(cs);
   +}

I do think EXCP_YIELD would be fine here.

The PAUSE instruction is largely left as implementation defined, so there is a lot of flexibility (a nop is also ok).

The RISC-V spec includes:

"""
Multithreaded cores might temporarily relinquish execution resources to other harts when PAUSE is executed.
"""

and

"""
A wfi or pause mechanism (at least potentially) allows energy-saving sleep on MCUs and context switching on higher-end CPUs """

Which sounds like a EXCP_YIELD should be fine

Alistair

Quoted 9 lines
Quoted 8 lines

+    ctx->base.is_jmp = DISAS_NORETURN;

target/riscv/insn_trans/trans_rvi.c.inc +3 -0
+
+    return true;
+}


  static bool trans_fence(DisasContext *ctx, arg_fence *a) {

#4 lore raw
In reply to Dao Lu
Quoted 23 lines · Dao Lu

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 to exit the TB and return to main loop.

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

--------
Changelog:

v1 -> v2
1. Pause now also exit the TB and return to main loop
2. Move the REQUIRE_ZIHINTPAUSE macro inside the trans_pause function

v2 -> v3
No changes, v2 was lost from the list

Quoted 8 lines

v3 -> v4
No longer break the reservation in trans_pause

v4 -> v5
Rabase on top of https://github.com/alistair23/qemu/tree/riscv-to-apply.next

Dao Lu (1):
Add Zihintpause support

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 11 lines
 target/riscv/cpu.c                      |  2 ++
 target/riscv/cpu.h                      |  1 +
 target/riscv/insn32.decode              |  7 ++++++-
 target/riscv/insn_trans/trans_rvi.c.inc | 16 ++++++++++++++++
 4 files changed, 25 insertions(+), 1 deletion(-)

--
2.25.1

#5 lore raw
Quoted 31 lines · Alistair Francis
Quoted 29 lines · Dao Lu

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 to exit the TB and return to main loop.

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

--------
Changelog:

v1 -> v2
1. Pause now also exit the TB and return to main loop
2. Move the REQUIRE_ZIHINTPAUSE macro inside the trans_pause function

v2 -> v3
No changes, v2 was lost from the list

v3 -> v4
No longer break the reservation in trans_pause

v4 -> v5
Rabase on top of https://github.com/alistair23/qemu/tree/riscv-to-apply.next

Quoted 7 lines
Quoted 2 lines

Dao Lu (1):
Add Zihintpause support

Thanks!

Applied to riscv-to-apply.next

Did you overwrite your tree by mistake ? I pulled riscv-to-apply.next a few days back where this patch along with Anup's priv version fixes are there. But I can't find it anymore. I am looking at this.

https://github.com/alistair23/qemu/commits/riscv-to-apply.next

I wanted to rebase my sstc series on top of the riscv-to-apply.next. Let me know if I am missing something.

Quoted 14 lines

Alistair

Quoted 11 lines
 target/riscv/cpu.c                      |  2 ++
 target/riscv/cpu.h                      |  1 +
 target/riscv/insn32.decode              |  7 ++++++-
 target/riscv/insn_trans/trans_rvi.c.inc | 16 ++++++++++++++++
 4 files changed, 25 insertions(+), 1 deletion(-)

--
2.25.1

Signature · 2 lines
Regards,
Atish
#6 lore raw
Quoted 36 lines · Atish Patra
Quoted 34 lines · Alistair Francis
Quoted 31 lines · Dao Lu

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 to exit the TB and return to main loop.

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

--------
Changelog:

v1 -> v2
1. Pause now also exit the TB and return to main loop
2. Move the REQUIRE_ZIHINTPAUSE macro inside the trans_pause function

v2 -> v3
No changes, v2 was lost from the list

v3 -> v4
No longer break the reservation in trans_pause

v4 -> v5
Rabase on top of https://github.com/alistair23/qemu/tree/riscv-to-apply.next

Dao Lu (1):
Add Zihintpause support

Quoted 8 lines
Quoted 4 lines

Thanks!

Applied to riscv-to-apply.next

Did you overwrite your tree by mistake ? I pulled riscv-to-apply.next a few days back where this patch along with Anup's priv version fixes are there. But I can't find it anymore. I am looking at this.

Hey Atish,

I created a last minute pull request to get some fixes into 7.1. When I did that I overwrote the current riscv-to-apply.next with a version that only has a few bug fixes.

I have pushed the riscv-to-apply.next for 7.2 to my public repo again.

Alistair

Quoted 25 lines

https://github.com/alistair23/qemu/commits/riscv-to-apply.next

I wanted to rebase my sstc series on top of the riscv-to-apply.next. Let me know if I am missing something.

Quoted 14 lines

Alistair

Quoted 11 lines
 target/riscv/cpu.c                      |  2 ++
 target/riscv/cpu.h                      |  1 +
 target/riscv/insn32.decode              |  7 ++++++-
 target/riscv/insn_trans/trans_rvi.c.inc | 16 ++++++++++++++++
 4 files changed, 25 insertions(+), 1 deletion(-)

--
2.25.1

--
Regards,
Atish