riscv: Fix IMSIC CSR write and add tests
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
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.
--- target/riscv/csr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
target/riscv/csr.c +6 -2
@@ -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
Re: [PATCH 1/3] target/riscv: Avoid NULL deref in IMSIC CSR write
Quoted 6 lines · Joel Stanley
rmw_xireg_aia()andrmw_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")Signed-off-by Joel Stanley <[email protected]>---
Quoted 29 lines
target/riscv/csr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)target/riscv/csr.c +6
-2@@ -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) {
Re: [PATCH 1/3] target/riscv: Avoid NULL deref in IMSIC CSR write
In reply to Joel StanleyQuoted 22 lines · Joel Stanley
rmw_xireg_aia()andrmw_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")Signed-off-by Joel Stanley <[email protected]>--- target/riscv/csr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)target/riscv/csr.c +0
-0
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; + }
Re: [PATCH 1/3] target/riscv: Avoid NULL deref in IMSIC CSR write
In reply to Joel StanleyQuoted 7 lines · Joel Stanley
rmw_xireg_aia()andrmw_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")Signed-off-by Joel Stanley <[email protected]>
Alistair
Quoted 34 lines
--- target/riscv/csr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)target/riscv/csr.c +6
-3@@ -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
PATCH 2/3 tests/functional/riscv64: Use newer kernel for tuxrun boot
In reply to Joel StanleyThe 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
@@ -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
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.30sSigned-off-by Joel Stanley <[email protected]>---
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@@ -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')
Re: [PATCH 2/3] tests/functional/riscv64: Use newer kernel for tuxrun boot
In reply to Joel StanleyQuoted 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.30sSigned-off-by Joel Stanley <[email protected]>
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@@ -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
PATCH 3/3 tests/functional/riscv64: Add virt machine AIA boot test
In reply to Joel StanleyAdd 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
@@ -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
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.
Signed-off-by Joel Stanley <[email protected]>---
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@@ -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'
Re: [PATCH 3/3] tests/functional/riscv64: Add virt machine AIA boot test
In reply to Joel StanleyQuoted 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.
Signed-off-by Joel Stanley <[email protected]>
Alistair
Quoted 26 lines
--- tests/functional/riscv64/test_tuxrun.py | 7 +++++++ 1 file changed, 7 insertions(+)tests/functional/riscv64/test_tuxrun.py +7
-1@@ -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
Alistair Francis
AppliedQuoted 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