Skip to contentSkip to conversation

riscv: Fix IMSIC CSR write and add tests

PATCH Applied Joel Stanley 4 participants 12 messages Jun 17 – Jun 22 RISC-V

lore
Share & tools
lore
#0 lore raw

cover riscv: Fix IMSIC CSR write and add tests

This fixes a segfault in master with any machine that uses the imsic. A test using the virt machine is added to catch this in the future.

Joel Stanley (3):
target/riscv: Avoid NULL deref in IMSIC CSR write tests/functional/riscv64: Use newer kernel for tuxrun boot tests/functional/riscv64: Add virt machine AIA boot test

 target/riscv/csr.c                      |  8 ++++++--
 tests/functional/riscv64/test_tuxrun.py | 11 +++++++++--
 2 files changed, 15 insertions(+), 4 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/3 target/riscv: Avoid NULL deref in IMSIC CSR write

rmw_xireg_aia() and rmw_xtopei() were changed to store the IMSIC callback's value in a local before passing it back to the caller. For write only CSR accesses that pointer is NULL, causing a crash when guest programs write to xireg or xtopei, such as when the guest sets up the IMSIC.

This only happens when setting aia=aplic-imsic so none of the existing boot tests caught it.

Fix it by guarding the pointer dereference as done for other CSRs.

Fixes 63469ad75dcc ("target/riscv: Fix arguments to board IMSIC emulation callbacks")
---
 target/riscv/csr.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
target/riscv/csr.c +6 -2
index ec931a8c3ddf..4502f2ab4788 100644
@@ -2732,7 +2732,9 @@ static RISCVException rmw_xireg_aia(CPURISCVState *env, int csrno,
                                     AIA_MAKE_IREG(isel, priv, virt, vgein,
                                                   riscv_cpu_mxl_bits(env)),
                                     &wide_val, new_val, wr_mask);
-            *val = wide_val;
+            if (val) {
+                *val = wide_val;
+            }
         }
     } else {
         isel_reserved = true;
@@ -3008,7 +3010,9 @@ static RISCVException rmw_xtopei(CPURISCVState *env, int csrno,
                     AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein,
                                   riscv_cpu_mxl_bits(env)),
                     &wide_val, new_val, wr_mask);
-    *val = wide_val;
+    if (val) {
+        *val = wide_val;
+    }
 
 done:
     if (ret) {
Signature · 1 line
2.47.3
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] target/riscv: Avoid NULL deref in IMSIC CSR write

Quoted 6 lines · Joel Stanley

rmw_xireg_aia() and rmw_xtopei() were changed to store the IMSIC callback's value in a local before passing it back to the caller. For write only CSR accesses that pointer is NULL, causing a crash when guest programs write to xireg or xtopei, such as when the guest sets up the IMSIC.

Quoted 8 lines

This only happens when setting aia=aplic-imsic so none of the existing boot tests caught it.

Fix it by guarding the pointer dereference as done for other CSRs.

Fixes 63469ad75dcc ("target/riscv: Fix arguments to board IMSIC emulation callbacks")

---

Quoted 29 lines
  target/riscv/csr.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
target/riscv/csr.c +6 -2
index ec931a8c3ddf..4502f2ab4788 100644
@@ -2732,7 +2732,9 @@ static RISCVException rmw_xireg_aia(CPURISCVState *env, int csrno,
                                      AIA_MAKE_IREG(isel, priv, virt, vgein,
                                                    riscv_cpu_mxl_bits(env)),
                                      &wide_val, new_val, wr_mask);
-            *val = wide_val;
+            if (val) {
+                *val = wide_val;
+            }
          }
      } else {
          isel_reserved = true;
@@ -3008,7 +3010,9 @@ static RISCVException rmw_xtopei(CPURISCVState *env, int csrno,
                      AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein,
                                    riscv_cpu_mxl_bits(env)),
                      &wide_val, new_val, wr_mask);
-    *val = wide_val;
+    if (val) {
+        *val = wide_val;
+    }
 
  done:
      if (ret) {
Reviewed-by Anton Johansson on v1 1/3
#3 lore raw

Re: [PATCH 1/3] target/riscv: Avoid NULL deref in IMSIC CSR write

In reply to Joel Stanley
Quoted 22 lines · Joel Stanley

rmw_xireg_aia() and rmw_xtopei() were changed to store the IMSIC callback's value in a local before passing it back to the caller. For write only CSR accesses that pointer is NULL, causing a crash when guest programs write to xireg or xtopei, such as when the guest sets up the IMSIC.

This only happens when setting aia=aplic-imsic so none of the existing boot tests caught it.

Fix it by guarding the pointer dereference as done for other CSRs.

Fixes 63469ad75dcc ("target/riscv: Fix arguments to board IMSIC emulation callbacks")
---
 target/riscv/csr.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
target/riscv/csr.c +0 -0
index ec931a8c3ddf..4502f2ab4788 100644

target/riscv/csr.c static RISCVException rmw_xireg_aia(CPURISCVState *env, int csrno, · line 2737

Quoted 7 lines
target/riscv/csr.c +3 -1
@@ -2732,7 +2732,9 @@ static RISCVException rmw_xireg_aia(CPURISCVState *env, int csrno,
                                     AIA_MAKE_IREG(isel, priv, virt, vgein,
                                                   riscv_cpu_mxl_bits(env)),
                                     &wide_val, new_val, wr_mask);
-            *val = wide_val;
+            if (val) {
+                *val = wide_val;
+            }
Reviewed-by on v1 1/3
#4 lore raw

Re: [PATCH 1/3] target/riscv: Avoid NULL deref in IMSIC CSR write

In reply to Joel Stanley
Quoted 7 lines · Joel Stanley

rmw_xireg_aia() and rmw_xtopei() were changed to store the IMSIC callback's value in a local before passing it back to the caller. For write only CSR accesses that pointer is NULL, causing a crash when guest programs write to xireg or xtopei, such as when the guest sets up the IMSIC.

Quoted 7 lines

This only happens when setting aia=aplic-imsic so none of the existing boot tests caught it.

Fix it by guarding the pointer dereference as done for other CSRs.

Fixes 63469ad75dcc ("target/riscv: Fix arguments to board IMSIC emulation callbacks")

Alistair

Quoted 34 lines
---
 target/riscv/csr.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
target/riscv/csr.c +6 -3
index ec931a8c3ddf..4502f2ab4788 100644
@@ -2732,7 +2732,9 @@ static RISCVException rmw_xireg_aia(CPURISCVState *env, int csrno,
                                     AIA_MAKE_IREG(isel, priv, virt, vgein,
                                                   riscv_cpu_mxl_bits(env)),
                                     &wide_val, new_val, wr_mask);
-            *val = wide_val;
+            if (val) {
+                *val = wide_val;
+            }
         }
     } else {
         isel_reserved = true;
@@ -3008,7 +3010,9 @@ static RISCVException rmw_xtopei(CPURISCVState *env, int csrno,
                     AIA_MAKE_IREG(ISELECT_IMSIC_TOPEI, priv, virt, vgein,
                                   riscv_cpu_mxl_bits(env)),
                     &wide_val, new_val, wr_mask);
-    *val = wide_val;
+    if (val) {
+        *val = wide_val;
+    }
 
 done:
     if (ret) {
--

2.47.3

#5 lore raw

PATCH 2/3 tests/functional/riscv64: Use newer kernel for tuxrun boot

In reply to Joel Stanley

The published tuxrun kernel is 6.4 which predates the introduction of AIA support in Linux. Update it to a 6.11 kernel, the newest build on the tuxrun site.

Boot times on a laptop are slightly longer but still reasonable:

                       | v6.4.16 | v6.11.9
  ---------------------|---------|--------
   test_riscv64        | 4.82s   | 5.56s
  ---------------------|---------|--------
   test_riscv64_maxcpu | 4.94s   | 5.30s
---
 tests/functional/riscv64/test_tuxrun.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/functional/riscv64/test_tuxrun.py +2 -2
index 0d8de36204a3..43d1242be52b 100755
@@ -17,8 +17,8 @@
 class TuxRunRiscV64Test(TuxRunBaselineTest):
 
     ASSET_RISCV64_KERNEL = Asset(
-        'https://storage.tuxboot.com/buildroot/20241119/riscv64/Image',
-        '2bd8132a3bf21570290042324fff48c987f42f2a00c08de979f43f0662ebadba')
+        'https://storage.tuxboot.com/kernels/6.11.9/riscv64/Image',
+        '174f8bb87f08961e54fa3fcd954a8e31f4645f6d6af4dd43983d5e9841490fb0')
     ASSET_RISCV64_ROOTFS = Asset(
         'https://storage.tuxboot.com/buildroot/20241119/riscv64/rootfs.ext4.zst',
         'aa4736a9872651dfc0d95e709465eedf1134fd19d42b8cb305bfd776f9801004')
Signature · 1 line
2.47.3
Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] tests/functional/riscv64: Use newer kernel for tuxrun boot

Quoted 6 lines · Joel Stanley

The published tuxrun kernel is 6.4 which predates the introduction of AIA support in Linux. Update it to a 6.11 kernel, the newest build on the tuxrun site.

Boot times on a laptop are slightly longer but still reasonable:

Quoted 8 lines
                        | v6.4.16 | v6.11.9
   ---------------------|---------|--------
    test_riscv64        | 4.82s   | 5.56s
   ---------------------|---------|--------
    test_riscv64_maxcpu | 4.94s   | 5.30s

---

And thanks for that PR on that qemu-machine-boot repo I'm using for the riscv-server-ref too btw!

Quoted 18 lines
  tests/functional/riscv64/test_tuxrun.py | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tests/functional/riscv64/test_tuxrun.py +2 -2
index 0d8de36204a3..43d1242be52b 100755
@@ -17,8 +17,8 @@
  class TuxRunRiscV64Test(TuxRunBaselineTest):
 
      ASSET_RISCV64_KERNEL = Asset(
-        'https://storage.tuxboot.com/buildroot/20241119/riscv64/Image',
-        '2bd8132a3bf21570290042324fff48c987f42f2a00c08de979f43f0662ebadba')
+        'https://storage.tuxboot.com/kernels/6.11.9/riscv64/Image',
+        '174f8bb87f08961e54fa3fcd954a8e31f4645f6d6af4dd43983d5e9841490fb0')
      ASSET_RISCV64_ROOTFS = Asset(
          'https://storage.tuxboot.com/buildroot/20241119/riscv64/rootfs.ext4.zst',
          'aa4736a9872651dfc0d95e709465eedf1134fd19d42b8cb305bfd776f9801004')
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] tests/functional/riscv64: Use newer kernel for tuxrun boot

In reply to Joel Stanley
Quoted 7 lines · Joel Stanley

The published tuxrun kernel is 6.4 which predates the introduction of AIA support in Linux. Update it to a 6.11 kernel, the newest build on the tuxrun site.

Boot times on a laptop are slightly longer but still reasonable:

Quoted 7 lines
                       | v6.4.16 | v6.11.9
  ---------------------|---------|--------
   test_riscv64        | 4.82s   | 5.56s
  ---------------------|---------|--------
   test_riscv64_maxcpu | 4.94s   | 5.30s

Alistair

Quoted 23 lines
---
 tests/functional/riscv64/test_tuxrun.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/functional/riscv64/test_tuxrun.py +2 -3
index 0d8de36204a3..43d1242be52b 100755
@@ -17,8 +17,8 @@
 class TuxRunRiscV64Test(TuxRunBaselineTest):
 
     ASSET_RISCV64_KERNEL = Asset(
-        'https://storage.tuxboot.com/buildroot/20241119/riscv64/Image',
-        '2bd8132a3bf21570290042324fff48c987f42f2a00c08de979f43f0662ebadba')
+        'https://storage.tuxboot.com/kernels/6.11.9/riscv64/Image',
+        '174f8bb87f08961e54fa3fcd954a8e31f4645f6d6af4dd43983d5e9841490fb0')
     ASSET_RISCV64_ROOTFS = Asset(
         'https://storage.tuxboot.com/buildroot/20241119/riscv64/rootfs.ext4.zst',
         'aa4736a9872651dfc0d95e709465eedf1134fd19d42b8cb305bfd776f9801004')
--

2.47.3

#8 lore raw

PATCH 3/3 tests/functional/riscv64: Add virt machine AIA boot test

In reply to Joel Stanley

Add coverage of the riscv64 virt machine's Advanced Interrupt Architecture models. With this the APLIC and IMSIC models are used by Linux, catching regressions.

This test requires a kernel >= 6.10, as AIA drivers were added to Linux in 6.10.

Boot time is ~5s on a laptop.

---
 tests/functional/riscv64/test_tuxrun.py | 7 +++++++
 1 file changed, 7 insertions(+)
tests/functional/riscv64/test_tuxrun.py +7 -0
index 43d1242be52b..6203a45060ce 100755
@@ -41,6 +41,13 @@ def test_riscv64_maxcpu(self):
         self.common_tuxrun(kernel_asset=self.ASSET_RISCV64_KERNEL,
                            rootfs_asset=self.ASSET_RISCV64_ROOTFS)
 
+    def test_riscv64_aia(self):
+        self.set_machine('virt')
+        self.vm.set_machine('virt,aia=aplic-imsic')
+        self.cpu='max'
+        self.common_tuxrun(kernel_asset=self.ASSET_RISCV64_KERNEL,
+                           rootfs_asset=self.ASSET_RISCV64_ROOTFS)
+
     def test_riscv64_rv32(self):
         self.set_machine('virt')
         self.cpu='rv32'
Signature · 1 line
2.47.3
Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH 3/3] tests/functional/riscv64: Add virt machine AIA boot test

Quoted 11 lines · Joel Stanley

Add coverage of the riscv64 virt machine's Advanced Interrupt Architecture models. With this the APLIC and IMSIC models are used by Linux, catching regressions.

This test requires a kernel >= 6.10, as AIA drivers were added to Linux in 6.10.

Boot time is ~5s on a laptop.

---

I think we need to add more 'virt' tests with all sorts of supported setups and configs catch these errors as early as possible.

This in particular is a nice test to be added since 'aplic-imsic' happens to be one of the most used aia setups out there. We should start adding variations of this one (change number of aia-guests and etc). A work for another day though.

Quoted 21 lines
  tests/functional/riscv64/test_tuxrun.py | 7 +++++++
  1 file changed, 7 insertions(+)
tests/functional/riscv64/test_tuxrun.py +7 -0
index 43d1242be52b..6203a45060ce 100755
@@ -41,6 +41,13 @@ def test_riscv64_maxcpu(self):
          self.common_tuxrun(kernel_asset=self.ASSET_RISCV64_KERNEL,
                             rootfs_asset=self.ASSET_RISCV64_ROOTFS)
 
+    def test_riscv64_aia(self):
+        self.set_machine('virt')
+        self.vm.set_machine('virt,aia=aplic-imsic')
+        self.cpu='max'
+        self.common_tuxrun(kernel_asset=self.ASSET_RISCV64_KERNEL,
+                           rootfs_asset=self.ASSET_RISCV64_ROOTFS)
+
      def test_riscv64_rv32(self):
          self.set_machine('virt')
          self.cpu='rv32'
Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] tests/functional/riscv64: Add virt machine AIA boot test

In reply to Joel Stanley
Quoted 11 lines · Joel Stanley

Add coverage of the riscv64 virt machine's Advanced Interrupt Architecture models. With this the APLIC and IMSIC models are used by Linux, catching regressions.

This test requires a kernel >= 6.10, as AIA drivers were added to Linux in 6.10.

Boot time is ~5s on a laptop.

Alistair

Quoted 26 lines
---
 tests/functional/riscv64/test_tuxrun.py | 7 +++++++
 1 file changed, 7 insertions(+)
tests/functional/riscv64/test_tuxrun.py +7 -1
index 43d1242be52b..6203a45060ce 100755
@@ -41,6 +41,13 @@ def test_riscv64_maxcpu(self):
         self.common_tuxrun(kernel_asset=self.ASSET_RISCV64_KERNEL,
                            rootfs_asset=self.ASSET_RISCV64_ROOTFS)
 
+    def test_riscv64_aia(self):
+        self.set_machine('virt')
+        self.vm.set_machine('virt,aia=aplic-imsic')
+        self.cpu='max'
+        self.common_tuxrun(kernel_asset=self.ASSET_RISCV64_KERNEL,
+                           rootfs_asset=self.ASSET_RISCV64_ROOTFS)
+
     def test_riscv64_rv32(self):
         self.set_machine('virt')
         self.cpu='rv32'
--

2.47.3

#11 lore raw
In reply to Joel Stanley
Quoted 8 lines · Joel Stanley

This fixes a segfault in master with any machine that uses the imsic. A test using the virt machine is added to catch this in the future.

Joel Stanley (3):
target/riscv: Avoid NULL deref in IMSIC CSR write tests/functional/riscv64: Use newer kernel for tuxrun boot tests/functional/riscv64: Add virt machine AIA boot test

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 9 lines
 target/riscv/csr.c                      |  8 ++++++--
 tests/functional/riscv64/test_tuxrun.py | 11 +++++++++--
 2 files changed, 15 insertions(+), 4 deletions(-)

--
2.47.3