[PATCH 0/2] target/riscv: fix mtinst/htinst for G-stage faults on VS-stage A/D updates

tewaro posted 2 patches 2 weeks, 5 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/cover.1793677944.git.tewaro@theorem.dev
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>
target/riscv/cpu.h                    |   1 +
target/riscv/tcg/cpu_helper.c         |   8 +-
tests/tcg/riscv64/hyp-guest.S         | 110 +++++++++++++++++++++
tests/tcg/riscv64/hyp-guest.h         |  71 ++++++++++++++
tests/tcg/riscv64/system/meson.build  |  77 +++++++++++++++
tests/tcg/riscv64/test-gstage-tinst.S | 135 ++++++++++++++++++++++++++
6 files changed, 401 insertions(+), 1 deletion(-)
create mode 100644 tests/tcg/riscv64/hyp-guest.S
create mode 100644 tests/tcg/riscv64/hyp-guest.h
create mode 100644 tests/tcg/riscv64/test-gstage-tinst.S
[PATCH 0/2] target/riscv: fix mtinst/htinst for G-stage faults on VS-stage A/D updates
Posted by tewaro 2 weeks, 5 days ago
Hi,

This series is about the traps taken when the G-stage refuses one of the
implicit accesses that VS-stage address translation makes. It follows up on
commit 366b4fba0fae ("target/riscv: check G-stage write permission for
VS-stage A/D updates"), which made the A/D write respect G-stage permission,
and fixes what is still reported wrong on such a trap.

Patch 2 adds a test, test-gstage-tinst (19 cases), master fails 8. Built for
Spike's HTIF console instead of semihosting, all 19 cases pass on upstream
Spike.

Patch 1 fixes the read/write pseudoinstruction in mtinst/htinst (0x3000 or
0x3020) [1]. 366b4fba0fae sets the write bit when the trap cause is a store
guest-page fault, but the cause is the original access type [2], while the
pseudoinstruction describes the implicit access that failed:

  original access, what failed       Spike      master
  load or fetch, A/D write           0x3020     0x3000
  store, A/D write                   0x3020     0x3020
  store, read of a non-leaf PTE      0x3000     0x3020

The commit was checked against Spike for the store case, where both agree.
Patch 1 fixes all 8 failures of test-gstage-tinst.

Results on master (1c1f512972), with make check-tcg for riscv64-softmmu:

  applied        test-gstage-tinst   check-tcg
  patch 2 only   8 of 19 fail        29/37
  patches 1-2    all pass            37/37

The test is last so that every commit passes check-tcg.

This bug was found during formal verification of a sandbox on top of the xv6
OS where tests passed on qemu but verification against Sail failed. Claude
was used to diagnose the issue. All commits were human written.

[1] Privileged ISA v1.12, 8.6.3, Table 8.12
[2] Privileged ISA v1.12, 8.5.1 "Guest Physical Address Translation"
https://github.com/riscv/riscv-isa-manual/releases/tag/Priv-v1.12

tewaro (2):
  target/riscv: report the write pseudoinstruction for a failed VS-stage
    A/D update
  tests/tcg/riscv64: test faults on implicit VS-stage page-table
    accesses

 target/riscv/cpu.h                    |   1 +
 target/riscv/tcg/cpu_helper.c         |   8 +-
 tests/tcg/riscv64/hyp-guest.S         | 110 +++++++++++++++++++++
 tests/tcg/riscv64/hyp-guest.h         |  71 ++++++++++++++
 tests/tcg/riscv64/system/meson.build  |  77 +++++++++++++++
 tests/tcg/riscv64/test-gstage-tinst.S | 135 ++++++++++++++++++++++++++
 6 files changed, 401 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/riscv64/hyp-guest.S
 create mode 100644 tests/tcg/riscv64/hyp-guest.h
 create mode 100644 tests/tcg/riscv64/test-gstage-tinst.S

-- 
2.54.0 (Apple Git-157)
Re: [PATCH 0/2] target/riscv: fix mtinst/htinst for G-stage faults on VS-stage A/D updates
Posted by Alistair 2 weeks, 5 days ago
On Mon, 2026-11-02 at 23:29 -0800, tewaro wrote:
> Hi,
> 
> This series is about the traps taken when the G-stage refuses one of
> the
> implicit accesses that VS-stage address translation makes. It follows
> up on
> commit 366b4fba0fae ("target/riscv: check G-stage write permission
> for
> VS-stage A/D updates"), which made the A/D write respect G-stage
> permission,
> and fixes what is still reported wrong on such a trap.
> 
> Patch 2 adds a test, test-gstage-tinst (19 cases), master fails 8.
> Built for
> Spike's HTIF console instead of semihosting, all 19 cases pass on
> upstream
> Spike.
> 
> Patch 1 fixes the read/write pseudoinstruction in mtinst/htinst
> (0x3000 or
> 0x3020) [1]. 366b4fba0fae sets the write bit when the trap cause is a
> store
> guest-page fault, but the cause is the original access type [2],
> while the
> pseudoinstruction describes the implicit access that failed:
> 
>   original access, what failed       Spike      master
>   load or fetch, A/D write           0x3020     0x3000
>   store, A/D write                   0x3020     0x3020
>   store, read of a non-leaf PTE      0x3000     0x3020
> 
> The commit was checked against Spike for the store case, where both
> agree.
> Patch 1 fixes all 8 failures of test-gstage-tinst.
> 
> Results on master (1c1f512972), with make check-tcg for riscv64-
> softmmu:
> 
>   applied        test-gstage-tinst   check-tcg
>   patch 2 only   8 of 19 fail        29/37
>   patches 1-2    all pass            37/37
> 
> The test is last so that every commit passes check-tcg.
> 
> This bug was found during formal verification of a sandbox on top of
> the xv6
> OS where tests passed on qemu but verification against Sail failed.
> Claude
> was used to diagnose the issue. All commits were human written.
> 
> [1] Privileged ISA v1.12, 8.6.3, Table 8.12
> [2] Privileged ISA v1.12, 8.5.1 "Guest Physical Address Translation"
> https://github.com/riscv/riscv-isa-manual/releases/tag/Priv-v1.12
> 
> tewaro (2):
>   target/riscv: report the write pseudoinstruction for a failed VS-
> stage
>     A/D update
>   tests/tcg/riscv64: test faults on implicit VS-stage page-table
>     accesses

Can you please use the `./scripts/get_maintainer.pl` script on your
patches to find the  complete list of email addresses to send this to

Alistair

> 
>  target/riscv/cpu.h                    |   1 +
>  target/riscv/tcg/cpu_helper.c         |   8 +-
>  tests/tcg/riscv64/hyp-guest.S         | 110 +++++++++++++++++++++
>  tests/tcg/riscv64/hyp-guest.h         |  71 ++++++++++++++
>  tests/tcg/riscv64/system/meson.build  |  77 +++++++++++++++
>  tests/tcg/riscv64/test-gstage-tinst.S | 135
> ++++++++++++++++++++++++++
>  6 files changed, 401 insertions(+), 1 deletion(-)
>  create mode 100644 tests/tcg/riscv64/hyp-guest.S
>  create mode 100644 tests/tcg/riscv64/hyp-guest.h
>  create mode 100644 tests/tcg/riscv64/test-gstage-tinst.S