Skip to contentSkip to conversation

target/riscv: fix RV32 fixed counter accesses

PATCH Superseded Zephyr Li 2 participants 4 messages Sep 5 – Sep 9 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Zephyr Li

author
#0 lore raw

PATCH 1/1 target/riscv: fix RV32 fixed counter accesses

Since commit cfc96df65e01, riscv_pmu_ctr_get_fixed_counters_val() returns the complete 64-bit fixed-counter value. The RV32 counter access paths, however, still perform parts of the offset calculation on separately extracted 32-bit halves.

In particular, riscv_pmu_write_ctrh() deposits the low 32 bits of the complete fixed-counter value into the high half of mhpmcounter_prev. riscv_pmu_read_ctr() also subtracts a 32-bit half of the previous value from the complete 64-bit fixed-counter value. Consequently, writes to mcycleh can be lost and carries between the low and high halves are not handled correctly.

Keep the fixed-counter offset calculation entirely in 64 bits. Before a running counter is partially written, materialize its current architectural value and reset the fixed-counter baseline. On reads, calculate the complete 64-bit counter value before extracting the half requested by RV32.

Add an RV32 system TCG test for high-half writes, low-to-high carry, and preserving the carried high half across a subsequent low-half write.

Fixes cfc96df65e01 ("target/riscv: Remove upper_half from riscv_pmu_ctr_get_fixed_counters_val")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4219

---
 target/riscv/tcg/csr.c                    | 32 ++++++++--------
 tests/tcg/riscv64/Makefile.softmmu-target | 12 ++++++
 tests/tcg/riscv64/test-mcycle-rv32.S      | 45 +++++++++++++++++++++++
 3 files changed, 74 insertions(+), 15 deletions(-)
 create mode 100644 tests/tcg/riscv64/test-mcycle-rv32.S
target/riscv/tcg/csr.c +17 -15
index 002f7e69c1..ffdd5c4aa6 100644
@@ -1337,23 +1337,23 @@ static RISCVException riscv_pmu_write_ctr(CPURISCVState *env, target_ulong val,
     int deposit_size = rv32 ? 32 : 64;
     uint64_t ctr;
 
-    counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
-                                         0, deposit_size, val);
-
     if (!get_field(env->mcountinhibit, BIT(ctr_idx)) &&
         (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
          riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) {
         ctr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx);
-        counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev,
-                                              0, deposit_size, ctr);
+        counter->mhpmcounter_val += ctr - counter->mhpmcounter_prev;
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             0, deposit_size, val);
+        counter->mhpmcounter_prev = ctr;
         if (ctr_idx > 2) {
             riscv_pmu_setup_timer(env, counter->mhpmcounter_val, ctr_idx);
         }
      } else {
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             0, deposit_size, val);
         /* Other counters can keep incrementing from the given value */
         counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev,
                                               0, deposit_size, val);
-
     }
 
     return RISCV_EXCP_NONE;
@@ -1363,20 +1363,22 @@ static RISCVException riscv_pmu_write_ctrh(CPURISCVState *env, target_ulong val,
                                           uint32_t ctr_idx)
 {
     PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
-    uint64_t ctrh;
+    uint64_t ctr;
 
-    counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
-                                         32, 32, val);
     if (!get_field(env->mcountinhibit, BIT(ctr_idx)) &&
         (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
          riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) {
-        ctrh = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx);
-        counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev,
-                                              32, 32, ctrh);
+        ctr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx);
+        counter->mhpmcounter_val += ctr - counter->mhpmcounter_prev;
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             32, 32, val);
+        counter->mhpmcounter_prev = ctr;
         if (ctr_idx > 2) {
             riscv_pmu_setup_timer(env, counter->mhpmcounter_val, ctr_idx);
         }
     } else {
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             32, 32, val);
         counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev,
                                               32, 32, val);
     }
@@ -1407,12 +1409,11 @@ RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val,
     bool rv32 = riscv_cpu_mxl(env) == MXL_RV32;
     int start = upper_half ? 32 : 0;
     int length = rv32 ? 32 : 64;
-    uint64_t ctr_prev, ctr_val;
+    uint64_t ctr_val;
 
     /* Ensure upper_half is only set for XLEN == 32 */
     g_assert(rv32 || !upper_half);
 
-    ctr_prev = extract64(counter->mhpmcounter_prev, start, length);
     ctr_val  = extract64(counter->mhpmcounter_val, start, length);
 
     if (get_field(env->mcountinhibit, BIT(ctr_idx))) {
@@ -1431,7 +1432,8 @@ RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val,
     if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
         riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) {
         uint64_t cntr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx) -
-                                                             ctr_prev + ctr_val;
+                        counter->mhpmcounter_prev +
+                        counter->mhpmcounter_val;
         *val = extract64(cntr, start, length);
     } else {
         *val = ctr_val;
tests/tcg/riscv64/Makefile.softmmu-target +12 -0
index 6a219c306c..15c7371acd 100644
@@ -28,6 +28,18 @@ EXTRA_RUNS += run-test-minstret-ecall
 run-test-minstret-ecall: test-minstret-ecall
 	$(call run-test, $<, $(QEMU) -icount shift=1 $(QEMU_OPTS)$<)
 
+RV32_CFLAGS = -march=rv32im_zicsr -mabi=ilp32
+CLEANFILES += test-mcycle-rv32
+
+test-mcycle-rv32: test-mcycle-rv32.S $(LINK_SCRIPT)
+	$(CC) $(CFLAGS) $(RV32_CFLAGS) $< -Wa,--noexecstack -c -o [email protected]
+	$(LD) -m elf32lriscv $(LDFLAGS) [email protected] -o $@
+
+EXTRA_RUNS += run-test-mcycle-rv32
+run-test-mcycle-rv32: test-mcycle-rv32
+	$(call run-test, $<, \
+	  $(QEMU) -cpu rv32 -icount shift=1 $(QEMU_OPTS)$<)
+
 EXTRA_RUNS += run-plugin-doubletrap
 run-plugin-doubletrap: doubletrap
 	$(call run-test, $<, \
tests/tcg/riscv64/test-mcycle-rv32.S new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Zephyr Li

Since commit cfc96df65e01, riscv_pmu_ctr_get_fixed_counters_val() returns the complete 64-bit fixed-counter value. The RV32 counter access paths, however, still perform parts of the offset calculation on separately extracted 32-bit halves.

In particular, riscv_pmu_write_ctrh() deposits the low 32 bits of the complete fixed-counter value into the high half of mhpmcounter_prev. riscv_pmu_read_ctr() also subtracts a 32-bit half of the previous value from the complete 64-bit fixed-counter value. Consequently, writes to mcycleh can be lost and carries between the low and high halves are not handled correctly.

Keep the fixed-counter offset calculation entirely in 64 bits. Before a running counter is partially written, materialize its current architectural value and reset the fixed-counter baseline. On reads, calculate the complete 64-bit counter value before extracting the half requested by RV32.

Quoted 7 lines

Add an RV32 system TCG test for high-half writes, low-to-high carry, and preserving the carried high half across a subsequent low-half write.

Fixes cfc96df65e01 ("target/riscv: Remove upper_half from riscv_pmu_ctr_get_fixed_counters_val")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4219

---

Quoted 167 lines
  target/riscv/tcg/csr.c                    | 32 ++++++++--------
  tests/tcg/riscv64/Makefile.softmmu-target | 12 ++++++
  tests/tcg/riscv64/test-mcycle-rv32.S      | 45 +++++++++++++++++++++++
  3 files changed, 74 insertions(+), 15 deletions(-)

create mode 100644 tests/tcg/riscv64/test-mcycle-rv32.S

target/riscv/tcg/csr.c +17 -15
index 002f7e69c1..ffdd5c4aa6 100644
@@ -1337,23 +1337,23 @@ static RISCVException riscv_pmu_write_ctr(CPURISCVState *env, target_ulong val,
      int deposit_size = rv32 ? 32 : 64;
      uint64_t ctr;
 
-    counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
-                                         0, deposit_size, val);
-
      if (!get_field(env->mcountinhibit, BIT(ctr_idx)) &&
          (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
           riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) {
          ctr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx);
-        counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev,
-                                              0, deposit_size, ctr);
+        counter->mhpmcounter_val += ctr - counter->mhpmcounter_prev;
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             0, deposit_size, val);
+        counter->mhpmcounter_prev = ctr;
          if (ctr_idx > 2) {
              riscv_pmu_setup_timer(env, counter->mhpmcounter_val, ctr_idx);
          }
       } else {
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             0, deposit_size, val);
          /* Other counters can keep incrementing from the given value */
          counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev,
                                                0, deposit_size, val);
-
      }
 
      return RISCV_EXCP_NONE;
@@ -1363,20 +1363,22 @@ static RISCVException riscv_pmu_write_ctrh(CPURISCVState *env, target_ulong val,
                                            uint32_t ctr_idx)
  {
      PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
-    uint64_t ctrh;
+    uint64_t ctr;
 
-    counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
-                                         32, 32, val);
      if (!get_field(env->mcountinhibit, BIT(ctr_idx)) &&
          (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
           riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) {
-        ctrh = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx);
-        counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev,
-                                              32, 32, ctrh);
+        ctr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx);
+        counter->mhpmcounter_val += ctr - counter->mhpmcounter_prev;
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             32, 32, val);
+        counter->mhpmcounter_prev = ctr;
          if (ctr_idx > 2) {
              riscv_pmu_setup_timer(env, counter->mhpmcounter_val, ctr_idx);
          }
      } else {
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             32, 32, val);
          counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev,
                                                32, 32, val);
      }
@@ -1407,12 +1409,11 @@ RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val,
      bool rv32 = riscv_cpu_mxl(env) == MXL_RV32;
      int start = upper_half ? 32 : 0;
      int length = rv32 ? 32 : 64;
-    uint64_t ctr_prev, ctr_val;
+    uint64_t ctr_val;
 
      /* Ensure upper_half is only set for XLEN == 32 */
      g_assert(rv32 || !upper_half);
 
-    ctr_prev = extract64(counter->mhpmcounter_prev, start, length);
      ctr_val  = extract64(counter->mhpmcounter_val, start, length);
 
      if (get_field(env->mcountinhibit, BIT(ctr_idx))) {
@@ -1431,7 +1432,8 @@ RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val,
      if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
          riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) {
          uint64_t cntr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx) -
-                                                             ctr_prev + ctr_val;
+                        counter->mhpmcounter_prev +
+                        counter->mhpmcounter_val;
          *val = extract64(cntr, start, length);
      } else {
          *val = ctr_val;
tests/tcg/riscv64/Makefile.softmmu-target +12 -0
index 6a219c306c..15c7371acd 100644
@@ -28,6 +28,18 @@ EXTRA_RUNS += run-test-minstret-ecall
  run-test-minstret-ecall: test-minstret-ecall
  	$(call run-test, $<, $(QEMU) -icount shift=1 $(QEMU_OPTS)$<)
 
+RV32_CFLAGS = -march=rv32im_zicsr -mabi=ilp32
+CLEANFILES += test-mcycle-rv32
+
+test-mcycle-rv32: test-mcycle-rv32.S $(LINK_SCRIPT)
+	$(CC) $(CFLAGS) $(RV32_CFLAGS) $< -Wa,--noexecstack -c -o [email protected]
+	$(LD) -m elf32lriscv $(LDFLAGS) [email protected] -o $@
+
+EXTRA_RUNS += run-test-mcycle-rv32
+run-test-mcycle-rv32: test-mcycle-rv32
+	$(call run-test, $<, \
+	  $(QEMU) -cpu rv32 -icount shift=1 $(QEMU_OPTS)$<)
+
  EXTRA_RUNS += run-plugin-doubletrap
  run-plugin-doubletrap: doubletrap
  	$(call run-test, $<, \
tests/tcg/riscv64/test-mcycle-rv32.S new file +45 -0
new file mode 100644
index 0000000000..189d1e13a2
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+	.option	norvc
+
+	.text
+	.global	_start
+_start:
+	/* Exercise writes while mcycle is running. */
+	csrw	mcountinhibit, zero
+	csrw	mcycle, zero
+
+	/* A write to the high half must be immediately observable. */
+	li	s0, 0x1234ffff
+	csrw	mcycleh, s0
+	csrr	t0, mcycleh
+	bne	t0, s0, fail
+
+	/* Check carry from the low half into the high half. */
+	li	s0, 0x12345678
+	csrw	mcycleh, s0
+	li	t0, 0xfffffff0
+	csrw	mcycle, t0
+	.rept	32
+	nop
+	.endr
+	csrr	t0, mcycleh
+	addi	s0, s0, 1
+	bne	t0, s0, fail
+
+	/* A low-half write must preserve the carried high half. */
+	li	t0, 0x22222222
+	csrw	mcycle, t0
+	csrr	t0, mcycleh
+	bne	t0, s0, fail
+
+	li	t0, 0x100000
+	li	t1, 0x5555	/* FINISHER_PASS */
+	sw	t1, 0(t0)
+	j	.
+
+fail:
+	li	t0, 0x100000
+	li	t1, 0x13333	/* status = FINISHER_FAIL, code = 1 */
+	sw	t1, 0(t0)
+	j	.
#2 lore raw
Quoted 24 lines · Daniel Henrique Barboza
Quoted 21 lines · Zephyr Li

Since commit cfc96df65e01, riscv_pmu_ctr_get_fixed_counters_val() returns the complete 64-bit fixed-counter value. The RV32 counter access paths, however, still perform parts of the offset calculation on separately extracted 32-bit halves.

In particular, riscv_pmu_write_ctrh() deposits the low 32 bits of the complete fixed-counter value into the high half of mhpmcounter_prev. riscv_pmu_read_ctr() also subtracts a 32-bit half of the previous value from the complete 64-bit fixed-counter value. Consequently, writes to mcycleh can be lost and carries between the low and high halves are not handled correctly.

Keep the fixed-counter offset calculation entirely in 64 bits. Before a running counter is partially written, materialize its current architectural value and reset the fixed-counter baseline. On reads, calculate the complete 64-bit counter value before extracting the half requested by RV32.

Add an RV32 system TCG test for high-half writes, low-to-high carry, and preserving the carried high half across a subsequent low-half write.

Quoted 6 lines
Quoted 4 lines
Fixes cfc96df65e01 ("target/riscv: Remove upper_half from riscv_pmu_ctr_get_fixed_counters_val")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4219

---

Actually there's a test related change I would like to suggest:

Quoted 89 lines
Quoted 89 lines

  target/riscv/tcg/csr.c                    | 32 ++++++++-------- tests/tcg/riscv64/Makefile.softmmu-target | 12 ++++++ tests/tcg/riscv64/test-mcycle-rv32.S      | 45 +++++++++++++++++++++++

  3 files changed, 74 insertions(+), 15 deletions(-)

  create mode 100644 tests/tcg/riscv64/test-mcycle-rv32.S

target/riscv/tcg/csr.c +0 -3
index 002f7e69c1..ffdd5c4aa6 100644
@@ -1337,23 +1337,23 @@ static RISCVException riscv_pmu_write_ctr(CPURISCVState *env, target_ulong val,
       int deposit_size = rv32 ? 32 : 64;
       uint64_t ctr;
-    counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
-                                         0, deposit_size, val);
-

      if (!get_field(env->mcountinhibit, BIT(ctr_idx)) && (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) { ctr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx); -        counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev, -                                              0, deposit_size, ctr); +        counter->mhpmcounter_val += ctr - counter->mhpmcounter_prev; +        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val, +                                             0, deposit_size, val); +        counter->mhpmcounter_prev = ctr;
          if (ctr_idx > 2) {
              riscv_pmu_setup_timer(env, counter->mhpmcounter_val, ctr_idx); }
       } else {
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val, +                                             0, deposit_size, val); /* Other counters can keep incrementing from the given value */ counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev, 0, deposit_size, val); -
      }
      return RISCV_EXCP_NONE;

target/riscv/tcg/csr.c +1 -3
@@ -1363,20 +1363,22 @@ static RISCVException riscv_pmu_write_ctrh(CPURISCVState *env, target_ulong val,
                                             uint32_t ctr_idx)
   {
       PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
-    uint64_t ctrh;
+    uint64_t ctr;
-    counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
-                                         32, 32, val);

      if (!get_field(env->mcountinhibit, BIT(ctr_idx)) && (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) { -        ctrh = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx); -        counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev, -                                              32, 32, ctrh);
+        ctr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx); +        counter->mhpmcounter_val += ctr - counter->mhpmcounter_prev; +        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val, +                                             32, 32, val);
+        counter->mhpmcounter_prev = ctr;
          if (ctr_idx > 2) {
              riscv_pmu_setup_timer(env, counter->mhpmcounter_val, ctr_idx); }
      } else {
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val, +                                             32, 32, val); counter->mhpmcounter_prev = deposit64(counter->mhpmcounter_prev, 32, 32, val);
      }

target/riscv/tcg/csr.c +1 -2
@@ -1407,12 +1409,11 @@ RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val,
       bool rv32 = riscv_cpu_mxl(env) == MXL_RV32;
       int start = upper_half ? 32 : 0;
       int length = rv32 ? 32 : 64;
-    uint64_t ctr_prev, ctr_val;
+    uint64_t ctr_val;
       /* Ensure upper_half is only set for XLEN == 32 */
       g_assert(rv32 || !upper_half);
-    ctr_prev = extract64(counter->mhpmcounter_prev, start, length);
       ctr_val  = extract64(counter->mhpmcounter_val, start, length);
       if (get_field(env->mcountinhibit, BIT(ctr_idx))) {
@@ -1431,7 +1432,8 @@ RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val,

      if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) || riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) { uint64_t cntr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx) - -                                                             ctr_prev + ctr_val; +                        counter->mhpmcounter_prev +
+                        counter->mhpmcounter_val;
          *val = extract64(cntr, start, length);
      } else {
          *val = ctr_val;

tests/tcg/riscv64/Makefile.softmmu-target +0 -0
Quoted 8 lines

tests/tcg/riscv64/Makefile.softmmu-target EXTRA_RUNS += run-test-minstret-ecall · line 32

Quoted 8 lines

index 6a219c306c..15c7371acd 100644

tests/tcg/riscv64/Makefile.softmmu-target +0 -0
@@ -28,6 +28,18 @@ EXTRA_RUNS += run-test-minstret-ecall

  run-test-minstret-ecall: test-minstret-ecall
      $(call run-test, $<, $(QEMU) -icount shift=1 $(QEMU_OPTS)$<)

tests/tcg/riscv64/Makefile.softmmu-target +2 -0
+RV32_CFLAGS = -march=rv32im_zicsr -mabi=ilp32
+CLEANFILES += test-mcycle-rv32

Note that you're adding specific rv32 tests in tests/tcg/riscv64/Makefile.softmmu-target. A better place is the tests/tcg/riscv32/ dir - we want to avoid mixing rv32 stuff in rv64 makefiles and vice-versa.

I suggest keeping tests/tcg/riscv64/Makefile.softmmu-target untouched and use tests/tcg/riscv32/ for this new test.

Thanks,
Daniel

Quoted 65 lines
Quoted 64 lines
tests/tcg/riscv64/Makefile.softmmu-target +10 -0
+
+test-mcycle-rv32: test-mcycle-rv32.S $(LINK_SCRIPT)
+    $(CC) $(CFLAGS) $(RV32_CFLAGS) $< -Wa,--noexecstack -c -o [email protected]
+    $(LD) -m elf32lriscv $(LDFLAGS) [email protected] -o $@
+
+EXTRA_RUNS += run-test-mcycle-rv32
+run-test-mcycle-rv32: test-mcycle-rv32
+    $(call run-test, $<, \
+      $(QEMU) -cpu rv32 -icount shift=1 $(QEMU_OPTS)$<)
+

  EXTRA_RUNS += run-plugin-doubletrap
  run-plugin-doubletrap: doubletrap
      $(call run-test, $<, \

tests/tcg/riscv64/test-mcycle-rv32.S new file +45 -0
new file mode 100644
index 0000000000..189d1e13a2
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+    .option    norvc
+
+    .text
+    .global    _start
+_start:
+    /* Exercise writes while mcycle is running. */
+    csrw    mcountinhibit, zero
+    csrw    mcycle, zero
+
+    /* A write to the high half must be immediately observable. */
+    li    s0, 0x1234ffff
+    csrw    mcycleh, s0
+    csrr    t0, mcycleh
+    bne    t0, s0, fail
+
+    /* Check carry from the low half into the high half. */
+    li    s0, 0x12345678
+    csrw    mcycleh, s0
+    li    t0, 0xfffffff0
+    csrw    mcycle, t0
+    .rept    32
+    nop
+    .endr
+    csrr    t0, mcycleh
+    addi    s0, s0, 1
+    bne    t0, s0, fail
+
+    /* A low-half write must preserve the carried high half. */
+    li    t0, 0x22222222
+    csrw    mcycle, t0
+    csrr    t0, mcycleh
+    bne    t0, s0, fail
+
+    li    t0, 0x100000
+    li    t1, 0x5555    /* FINISHER_PASS */
+    sw    t1, 0(t0)
+    j    .
+
+fail:
+    li    t0, 0x100000
+    li    t1, 0x13333    /* status = FINISHER_FAIL, code = 1 */
+    sw    t1, 0(t0)
+    j    .
author
#3 lore raw

Thanks for the suggestion. I'll move the test to tests/tcg/riscv32/ in v2.

Thanks,
Zephyr

Quoted 232 lines · Daniel Henrique Barboza
Quoted 21 lines · Daniel Henrique Barboza
Quoted 18 lines · Zephyr Li

Since commit cfc96df65e01, riscv_pmu_ctr_get_fixed_counters_val() returns the complete 64-bit fixed-counter value. The RV32 counter access paths, however, still perform parts of the offset calculation on separately extracted 32-bit halves.

In particular, riscv_pmu_write_ctrh() deposits the low 32 bits of the complete fixed-counter value into the high half of mhpmcounter_prev. riscv_pmu_read_ctr() also subtracts a 32-bit half of the previous value from the complete 64-bit fixed-counter value. Consequently, writes to mcycleh can be lost and carries between the low and high halves are not handled correctly.

Keep the fixed-counter offset calculation entirely in 64 bits. Before a running counter is partially written, materialize its current architectural value and reset the fixed-counter baseline. On reads, calculate the complete 64-bit counter value before extracting the half requested by RV32.

Quoted 4 lines
Quoted 4 lines

Add an RV32 system TCG test for high-half writes, low-to-high carry, and preserving the carried high half across a subsequent low-half write.

Fixes cfc96df65e01 ("target/riscv: Remove upper_half from

riscv_pmu_ctr_get_fixed_counters_val")

Quoted 5 lines
Quoted 3 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4219

---

Actually there's a test related change I would like to suggest:

Quoted 7 lines
Quoted 7 lines
  target/riscv/tcg/csr.c                    | 32 ++++++++--------
  tests/tcg/riscv64/Makefile.softmmu-target | 12 ++++++
  tests/tcg/riscv64/test-mcycle-rv32.S      | 45 +++++++++++++++++++++++
  3 files changed, 74 insertions(+), 15 deletions(-)

create mode 100644 tests/tcg/riscv64/test-mcycle-rv32.S

Quoted 5 lines

target/riscv/tcg/csr.c static RISCVException

Quoted 5 lines
target/riscv/tcg/csr.c +0 -0
index 002f7e69c1..ffdd5c4aa6 100644
@@ -1337,23 +1337,23 @@ static RISCVException

riscv_pmu_write_ctr(CPURISCVState *env, target_ulong val,

Quoted 10 lines

target/riscv/tcg/csr.c static RISCVException riscv_pmu_write_ctr(CPURISCVState *env, target_ulong val,

Quoted 10 lines
target/riscv/tcg/csr.c +0 -4
      int deposit_size = rv32 ? 32 : 64;
      uint64_t ctr;
-    counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
-                                         0, deposit_size, val);
-
      if (!get_field(env->mcountinhibit, BIT(ctr_idx)) &&
          (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
           riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) {
          ctr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx);
-        counter->mhpmcounter_prev =

deposit64(counter->mhpmcounter_prev,

Quoted 7 lines

target/riscv/tcg/csr.c static RISCVException riscv_pmu_write_ctr(CPURISCVState *env, target_ulong val,

Quoted 7 lines
target/riscv/tcg/csr.c +4 -1
-                                              0, deposit_size, ctr);
+        counter->mhpmcounter_val += ctr - counter->mhpmcounter_prev;
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             0, deposit_size, val);
+        counter->mhpmcounter_prev = ctr;
          if (ctr_idx > 2) {
              riscv_pmu_setup_timer(env, counter->mhpmcounter_val,

ctr_idx);

Quoted 5 lines

target/riscv/tcg/csr.c static RISCVException riscv_pmu_write_ctr(CPURISCVState *env, target_ulong val,

Quoted 5 lines
target/riscv/tcg/csr.c +2 -0
          }
       } else {
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             0, deposit_size, val);
          /* Other counters can keep incrementing from the given value

*/

Quoted 1 line

target/riscv/tcg/csr.c static RISCVException · line 1354

Quoted 1 line
target/riscv/tcg/csr.c +0 -0
          counter->mhpmcounter_prev =

deposit64(counter->mhpmcounter_prev,

Quoted 5 lines

target/riscv/tcg/csr.c static RISCVException

Quoted 5 lines
target/riscv/tcg/csr.c +0 -1
                                                0, deposit_size, val);
-
      }
      return RISCV_EXCP_NONE;
@@ -1363,20 +1363,22 @@ static RISCVException

riscv_pmu_write_ctrh(CPURISCVState *env, target_ulong val,

Quoted 4 lines
Quoted 4 lines
target/riscv/tcg/csr.c +0 -1
                                            uint32_t ctr_idx)
  {
      PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
-    uint64_t ctrh;
Quoted 8 lines

target/riscv/tcg/csr.c static RISCVException riscv_pmu_write_ctrh(CPURISCVState *env, target_ulong val,

Quoted 8 lines
target/riscv/tcg/csr.c +1 -4
+    uint64_t ctr;
-    counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
-                                         32, 32, val);
      if (!get_field(env->mcountinhibit, BIT(ctr_idx)) &&
          (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
           riscv_pmu_ctr_monitor_instructions(env, ctr_idx))) {
-        ctrh = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx);
-        counter->mhpmcounter_prev =

deposit64(counter->mhpmcounter_prev,

Quoted 8 lines

target/riscv/tcg/csr.c static RISCVException riscv_pmu_write_ctrh(CPURISCVState *env, target_ulong val,

Quoted 8 lines
target/riscv/tcg/csr.c +5 -1
-                                              32, 32, ctrh);
+        ctr = riscv_pmu_ctr_get_fixed_counters_val(env, ctr_idx);
+        counter->mhpmcounter_val += ctr - counter->mhpmcounter_prev;
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             32, 32, val);
+        counter->mhpmcounter_prev = ctr;
          if (ctr_idx > 2) {
              riscv_pmu_setup_timer(env, counter->mhpmcounter_val,

ctr_idx);

Quoted 5 lines

target/riscv/tcg/csr.c static RISCVException riscv_pmu_write_ctrh(CPURISCVState *env, target_ulong val,

Quoted 5 lines
target/riscv/tcg/csr.c +2 -0
          }
      } else {
+        counter->mhpmcounter_val = deposit64(counter->mhpmcounter_val,
+                                             32, 32, val);
          counter->mhpmcounter_prev =

deposit64(counter->mhpmcounter_prev,

Quoted 3 lines

target/riscv/tcg/csr.c RISCVException riscv_pmu_read_ctr(CPURISCVState

Quoted 3 lines
target/riscv/tcg/csr.c +0 -0
                                                32, 32, val);
      }
@@ -1407,12 +1409,11 @@ RISCVException riscv_pmu_read_ctr(CPURISCVState

*env, target_ulong *val,

Quoted 11 lines

target/riscv/tcg/csr.c RISCVException riscv_pmu_read_ctr(CPURISCVState

Quoted 11 lines
target/riscv/tcg/csr.c +1 -2
      bool rv32 = riscv_cpu_mxl(env) == MXL_RV32;
      int start = upper_half ? 32 : 0;
      int length = rv32 ? 32 : 64;
-    uint64_t ctr_prev, ctr_val;
+    uint64_t ctr_val;
      /* Ensure upper_half is only set for XLEN == 32 */
      g_assert(rv32 || !upper_half);
-    ctr_prev = extract64(counter->mhpmcounter_prev, start, length);
      ctr_val  = extract64(counter->mhpmcounter_val, start, length);
      if (get_field(env->mcountinhibit, BIT(ctr_idx))) {
@@ -1431,7 +1432,8 @@ RISCVException riscv_pmu_read_ctr(CPURISCVState

*env, target_ulong *val,

Quoted 3 lines

target/riscv/tcg/csr.c RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val,

Quoted 3 lines
target/riscv/tcg/csr.c +0 -0
      if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
          riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) {
          uint64_t cntr = riscv_pmu_ctr_get_fixed_counters_val(env,

ctr_idx) -

Quoted 1 line

target/riscv/tcg/csr.c RISCVException riscv_pmu_read_ctr(CPURISCVState · line 1434

Quoted 1 line
target/riscv/tcg/csr.c +0 -1
-                                                             ctr_prev

+ ctr_val;

Quoted 6 lines

Review

Quoted 6 lines
target/riscv/tcg/csr.c +2 -0
+                        counter->mhpmcounter_prev +
+                        counter->mhpmcounter_val;
          *val = extract64(cntr, start, length);
      } else {
          *val = ctr_val;
(quoted diff) +0 -0

b/tests/tcg/riscv64/Makefile.softmmu-target

Quoted 8 lines

tests/tcg/riscv64/Makefile.softmmu-target EXTRA_RUNS += run-test-minstret-ecall · line 31

Quoted 8 lines

index 6a219c306c..15c7371acd 100644

tests/tcg/riscv64/Makefile.softmmu-target +2 -0
@@ -28,6 +28,18 @@ EXTRA_RUNS += run-test-minstret-ecall
  run-test-minstret-ecall: test-minstret-ecall
      $(call run-test, $<, $(QEMU) -icount shift=1 $(QEMU_OPTS)$<)
+RV32_CFLAGS = -march=rv32im_zicsr -mabi=ilp32
+CLEANFILES += test-mcycle-rv32

Note that you're adding specific rv32 tests in tests/tcg/riscv64/Makefile.softmmu-target.
A better place is the tests/tcg/riscv32/ dir - we want to avoid mixing rv32 stuff in
rv64 makefiles and vice-versa.

I suggest keeping tests/tcg/riscv64/Makefile.softmmu-target untouched and use tests/tcg/riscv32/ for this new test.

Thanks,
Daniel

Quoted 6 lines
Quoted 6 lines
tests/tcg/riscv64/Makefile.softmmu-target +6 -0
+
+test-mcycle-rv32: test-mcycle-rv32.S $(LINK_SCRIPT)
+    $(CC) $(CFLAGS) $(RV32_CFLAGS) $< -Wa,--noexecstack -c -o [email protected]
+    $(LD) -m elf32lriscv $(LDFLAGS) [email protected] -o $@
+
+EXTRA_RUNS += run-test-mcycle-rv32
Quoted 8 lines

Review

Quoted 8 lines
tests/tcg/riscv64/Makefile.softmmu-target +4 -0
+run-test-mcycle-rv32: test-mcycle-rv32
+    $(call run-test, $<, \
+      $(QEMU) -cpu rv32 -icount shift=1 $(QEMU_OPTS)$<)
+
  EXTRA_RUNS += run-plugin-doubletrap
  run-plugin-doubletrap: doubletrap
      $(call run-test, $<, \
(quoted diff) +0 -0

b/tests/tcg/riscv64/test-mcycle-rv32.S

Quoted 51 lines
Quoted 50 lines

new file mode 100644
index 0000000000..189d1e13a2

tests/tcg/riscv64/test-mcycle-rv32.S new file +45 -0
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+    .option    norvc
+
+    .text
+    .global    _start
+_start:
+    /* Exercise writes while mcycle is running. */
+    csrw    mcountinhibit, zero
+    csrw    mcycle, zero
+
+    /* A write to the high half must be immediately observable. */
+    li    s0, 0x1234ffff
+    csrw    mcycleh, s0
+    csrr    t0, mcycleh
+    bne    t0, s0, fail
+
+    /* Check carry from the low half into the high half. */
+    li    s0, 0x12345678
+    csrw    mcycleh, s0
+    li    t0, 0xfffffff0
+    csrw    mcycle, t0
+    .rept    32
+    nop
+    .endr
+    csrr    t0, mcycleh
+    addi    s0, s0, 1
+    bne    t0, s0, fail
+
+    /* A low-half write must preserve the carried high half. */
+    li    t0, 0x22222222
+    csrw    mcycle, t0
+    csrr    t0, mcycleh
+    bne    t0, s0, fail
+
+    li    t0, 0x100000
+    li    t1, 0x5555    /* FINISHER_PASS */
+    sw    t1, 0(t0)
+    j    .
+
+fail:
+    li    t0, 0x100000
+    li    t1, 0x13333    /* status = FINISHER_FAIL, code = 1 */
+    sw    t1, 0(t0)
+    j    .