From nobody Sun Nov 29 02:09:51 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1795679731; cv=none; d=zohomail.com; s=zohoarc; b=gjsvcpU26ekI+EYPldT2puiwX7OfWt+DI/ySktJBZzeLoZvxf5j+galrJ7bioxOZ6DiyFsm4kGjG7rh86scuOxhozIudaHx6ebYKOWMZV8773wYrjOyKvWwAaRpZW1250JM72CfjE4D4GZ9kKkeLiCo71HId0gFcDylgvsqdy28= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795679731; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=6pNvz7x1u8pQoZrpE2TOiP6mSp9NgqA10X8VgRReMYY=; b=mWwBCWEKYRY8eABek0P3aXQ2AKYN2oqsOpmlUr1tUH1oefwWJCNonmV21Nh9oyiYx2rBLNSyK/xSHDmhLCtSgoolJUz9MAOE61zN7kzEykQHPEYZMbb9Pcex2AHQRfY1a5WC2OZfwbPpflyUkQRshD7sB3U8HEJzGOpN9Jro4EE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1795679731462919.0947598334894; Wed, 25 Nov 2026 23:55:31 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xWUJG-0006ZI-TY; Thu, 26 Nov 2026 02:54:26 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJE-0006VL-3m; Thu, 26 Nov 2026 02:54:24 -0500 Received: from [115.124.30.100] (helo=out30-100.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJ7-0002Y3-Ff; Thu, 26 Nov 2026 02:54:23 -0500 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0XFTpGYV_1795679638 cluster:ay36) by smtp.aliyun-inc.com; Thu, 26 Nov 2026 15:53:59 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1795679639; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=6pNvz7x1u8pQoZrpE2TOiP6mSp9NgqA10X8VgRReMYY=; b=MkSC31MrSjNHZ+hd/eOJuouvQEuH9gd1nujDMjpn8hr4uXceVkEQXq5F4vUipRvqG3BOTz3u+DZ9Fm2exHAcWU433rYb3oKw/dsP6w9boLZd4ehrTwwdDoyvqCGqnb5mIYAu5XAk64CgAxlZ2WcHZQDaT+nNpjIsaInTjWMUWDA= X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R421e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=maildocker-contentspam011083073210; MF=zhiwei_liu@linux.alibaba.com; NM=1; PH=DS; RN=8; SR=0; TI=SMTPD_---0XFTpGYV_1795679638; From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , qemu-riscv@nongnu.org Subject: [PATCH 1/6] target/riscv: Preserve access faults while translating VS PTE reads Date: Thu, 26 Nov 2026 15:53:45 +0800 Message-Id: <20261126075350.13022-2-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> References: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.100 (deferred) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=115.124.30.100; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-100.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1795679733447158500 Content-Type: text/plain; charset="utf-8" With the H extension, the VS page-table walker calls G-stage translation to obtain the physical address of each VS PTE. A physical access to a G-stage PTE can fail because PMP rejects it or the memory access returns a PMA error. The caller(get_physical_address) turns this failure into TRANSLATE_G_STAGE_FAIL and records the VS PTE's guest-physical address. This loses the physical access error. The original instruction fetch, load or store then raises a guest-page fault, with misleading fault address information, instead of its corresponding access fault. Commit 8e090ecfa9e5 ("target/riscv/cpu_helper.c: add PMA access fault") introduced TRANSLATE_PMA_FAIL for failed physical PTE reads. That PMA failure result, like the existing PMP failure result, is still lost at this call site. Add is_physical_access_fault() to recognize both results. Return either error unchanged from the recursive VS-PTE read translation before setting fault_pte_addr. Reuse the helper in the existing TLB-fill classification. The preserved result sets pmp_pma_violation, so the existing exception code reports an access fault for the original access type. The guest physical fault address remains zero. Other translation failures still use TRANSLATE_G_STAGE_FAIL and retain their guest-page-fault behavior. The next commit adds a regression for this read path. A/D update translation and direct G-stage fault-address handling are addressed by later commits, each with its own regression extension. Signed-off-by: LIU Zhiwei --- target/riscv/tcg/cpu_helper.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c index 58e98e975c..90a3731214 100644 --- a/target/riscv/tcg/cpu_helper.c +++ b/target/riscv/tcg/cpu_helper.c @@ -1069,6 +1069,11 @@ static bool check_svukte_addr(CPURISCVState *env, va= ddr addr) return !high_bit; } =20 +static bool is_physical_access_fault(int ret) +{ + return ret =3D=3D TRANSLATE_PMP_FAIL || ret =3D=3D TRANSLATE_PMA_FAIL; +} + /* * get_physical_address - get the physical address for this virtual address * @@ -1257,6 +1262,9 @@ static int get_physical_address(CPURISCVState *env, h= waddr *physical, is_debug, false); =20 if (vbase_ret !=3D TRANSLATE_SUCCESS) { + if (is_physical_access_fault(vbase_ret)) { + return vbase_ret; + } if (fault_pte_addr) { *fault_pte_addr =3D (base + idx * ptesize) >> 2; } @@ -1875,7 +1883,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, = int size, } } =20 - if (ret =3D=3D TRANSLATE_PMP_FAIL || ret =3D=3D TRANSLATE_PMA_FAIL) { + if (is_physical_access_fault(ret)) { pmp_pma_violation =3D true; } =20 --=20 2.43.0 From nobody Sun Nov 29 02:09:51 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1795679737; cv=none; d=zohomail.com; s=zohoarc; b=LM5bSOyzet0l+G8XF6U5Y+uzBxU56dPbn5yYpSmb/g7upGdoJJgTpJzakixnG81BYYKRbrV678NexV4rHHkepsEmV/261O7AU6uSDMTYtruKZ9bBWx4b9b5rkkJslmopW/lzK+A1wX9Yd2zjHmYhL5nvbMyow+5+RjHbzhzGWUA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795679737; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=KS5Mdf8GznZV0nyu+Jd6sGT2VO9cFWm1bOa0+Ya6Y+8=; b=HaL3bnT7+V7usjLdYy0NEeCg47PCFE7XGtiuHdW8L3VYrOJIAwMXQqfoEWD8kCkpBxqg0VPC6ivZ/cnkwsOImxgNUeOrQJ2S3I+7NuwBROI3uxeF6nORcTvc5Nqqu9V4RCVuE4jKQGssTvXdT6+MYje9tlAsPywcO6LnM0ZxTp8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1795679737877816.5028703699185; Wed, 25 Nov 2026 23:55:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xWUJH-0006ap-F5; Thu, 26 Nov 2026 02:54:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJE-0006VN-7N; Thu, 26 Nov 2026 02:54:24 -0500 Received: from [115.124.30.98] (helo=out30-98.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJ7-0002aA-NB; Thu, 26 Nov 2026 02:54:23 -0500 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0XFTpGYa_1795679639 cluster:ay36) by smtp.aliyun-inc.com; Thu, 26 Nov 2026 15:53:59 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1795679640; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=KS5Mdf8GznZV0nyu+Jd6sGT2VO9cFWm1bOa0+Ya6Y+8=; b=o2mAoCB67y1U/nk3Hp51ON+s6NdfRH2RG+OpYgDVfNiMJsizJ/PcpAXUzjMqWbEX8bBPWfitGdjME4U2w/RqzfQl9Oak2IS6DrVYt7Dl08jiDdVUTJ+rHzayhwmTZJyrIVLNoccmJnAtJEZWUfNAfekMjn/d0Tc5umJEMfJctN4= X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R681e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=maildocker-contentspam033045098064; MF=zhiwei_liu@linux.alibaba.com; NM=1; PH=DS; RN=8; SR=0; TI=SMTPD_---0XFTpGYa_1795679639; From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , qemu-riscv@nongnu.org Subject: [PATCH 2/6] tests/tcg/riscv64: Add nested-PTE-read access-fault regression Date: Thu, 26 Nov 2026 15:53:46 +0800 Message-Id: <20261126075350.13022-3-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> References: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.98 (deferred) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=115.124.30.98; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-98.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1795679739518158500 Content-Type: text/plain; charset="utf-8" Add an H-extension regression test for the preceding VS PTE read fix. While translating a guest virtual address, the VS-stage walker may need to read a PTE. Reading that PTE requires a G-stage translation. A PMP or PMA failure during this recursive walk must raise an access fault for the original fetch, load or store. It must not become a guest-page fault. The test builds a small bare-metal setup and checks both outcomes: - PMP/PMA failures during nested PTE reads report the original access-fault type. - Genuine G-stage translation failures report guest-page faults. For access faults, also check that mtval holds the original virtual address and mtval2 is zero. Before the fix, the first PMP case incorrectly reported a guest-page fault and the test exited with failure code 2. Signed-off-by: LIU Zhiwei AI-used-for: tests --- tests/tcg/riscv64/system/meson.build | 7 + tests/tcg/riscv64/test-gstage-access-fault.S | 225 +++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 tests/tcg/riscv64/test-gstage-access-fault.S diff --git a/tests/tcg/riscv64/system/meson.build b/tests/tcg/riscv64/syste= m/meson.build index 3c8b018d9e..645bc8576f 100644 --- a/tests/tcg/riscv64/system/meson.build +++ b/tests/tcg/riscv64/system/meson.build @@ -16,6 +16,13 @@ qemu_args =3D ['-M', 'virt', =20 setup =3D {'cflags': cflags, 'qemu_args': qemu_args} =20 +tests +=3D { + 'test-gstage-access-fault.S': { + 'cflags': cflags + ['-march=3Drv64ima_zicsr'], + 'qemu_args': ['-cpu', 'rv64,h=3Dtrue,svade=3Dfalse,svadu=3Dfalse', qem= u_args], + }, +} + tests +=3D { 'doubletrap.S': setup + {'plugin_test': {'plugin': 'discons'}}, 'interruptedmemory.S': setup + {'plugin_test': {'plugin': 'discons'}}, diff --git a/tests/tcg/riscv64/test-gstage-access-fault.S b/tests/tcg/riscv= 64/test-gstage-access-fault.S new file mode 100644 index 0000000000..61a0957270 --- /dev/null +++ b/tests/tcg/riscv64/test-gstage-access-fault.S @@ -0,0 +1,225 @@ +/* + * Distinguish physical access faults from G-stage page faults. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +/* + * Nested-PTE-read test flow (virt with the H extension): + * + * 1. Set up the test environment. + * Install an M-mode trap handler and disable delegation so every + * exception is observed in M-mode. The handler records mcause in s1 + * and returns to the per-case continuation in s2. A failed CHECK + * exits through semihosting with a unique code; success exits zero. + * + * 2. Create working VS-stage and G-stage mappings. + * Build Sv39 VS tables mapping guest virtual address 0x4000 to guest + * physical address 0x8000. Build Sv39x4 G-stage tables mapping the VS + * page-table pages and final data page into RAM. Pre-set leaf A/D + * bits so these read cases do not enter A/D write-back handling. + * First verify that a normal load from 0x4000 succeeds. + * + * 3. Check PMP failures during nested PTE reads. + * TRY_DATA uses MPRV/MPV for guest loads and stores. TRY_FETCH uses + * mret to enter VS-mode and fetch from the same virtual address. + * Deny reads of the G-stage root page, G_ROOT, with PMP. A load, + * store and fetch must report access-fault causes 5, 7 and 1, + * respectively. CHECK also requires mtval =3D=3D 0x4000 and mtval2 =3D= =3D 0. + * As a control, deny reads of the translated VS leaf PTE page and + * require the guest load to report the same load access fault. + * + * 4. Check genuine G-stage translation failures. + * Restore PMP access, then invalidate the G-stage mapping of the VS + * root page. Require guest-page-fault causes 21, 23 and 20 for load, + * store and fetch. Restore the mapping afterward. WRITE_PTE issues + * HFENCE.VVMA and HFENCE.GVMA so stale translations cannot hide the + * page-table changes. + * + * 5. Check PMA failures during nested PTE reads. + * Point hgatp at the unmapped physical root address 0x90000000 and + * repeat the load, store and fetch checks. The recursive walk must + * return TRANSLATE_PMA_FAIL for the inaccessible physical PTE read, + * rather than a normal G-stage mapping failure. Require the original + * access-fault causes and the same mtval/mtval2 checks as in step 3. + * + * 6. Verify recovery. + * Restore valid VS-stage and G-stage translation state, then confirm + * that a normal load succeeds again before exiting with zero. + * Keep the shared helpers and failure-code layout suitable for later + * A/D-update and direct G-stage test extensions. + */ + + .option norvc + .equ MPRV, (1 << 17) + .equ MPV, (1 << 39) + .equ MPP_S, (1 << 11) + .equ MPP_M, (3 << 11) + .equ VS_ROOT, 0x80400000 + .equ VS_L1, 0x80401000 + .equ VS_L0, 0x80402000 + .equ G_ROOT, 0x80404000 + .equ G_L1, 0x80408000 + .equ G_L0, 0x80409000 + .equ DATA, 0x80500000 + + .macro PMP_ALL + csrw pmpcfg0, zero + li t0, -1 + csrw pmpaddr0, t0 + li t0, 0x1f + csrw pmpcfg0, t0 + .endm + + /* A higher-priority 4 KiB region, with a catch-all RWX region. */ + .macro PMP_PAGE address, permissions + csrw pmpcfg0, zero + li t0, ((\address >> 2) | 0x1ff) + csrw pmpaddr0, t0 + li t0, -1 + csrw pmpaddr1, t0 + li t0, (0x1f00 | 0x18 | \permissions) + csrw pmpcfg0, t0 + .endm + + .macro WRITE_PTE address, value + li t0, \address + li t1, \value + sd t1, 0(t0) + /* HFENCE.VVMA and HFENCE.GVMA for VS- and G-stage changes. */ + .insn r 0x73, 0, 0x11, x0, x0, x0 + .insn r 0x73, 0, 0x31, x0, x0, x0 + .endm + + /* The original faulting address is always GVA 0x4000. */ + .macro CHECK cause, code + li a0, \code + li t0, \cause + bne s1, t0, fail + .if \cause !=3D 0 + csrr t0, mtval + li t1, 0x4000 + bne t0, t1, fail + .endif + .if (\cause =3D=3D 1) || (\cause =3D=3D 5) || (\cause =3D=3D 7) + csrr t0, mtval2 + bnez t0, fail + .endif + .endm + + .macro TRY_DATA store, cause, code + li s1, 0 + lla s2, .Lresume\@ + li a3, 0x4000 + li t0, MPP_M + csrc mstatus, t0 + li t0, MPP_S | MPRV | MPV + csrs mstatus, t0 + .if \store + sw zero, 0(a3) + .else + lw t4, 0(a3) + .endif +.Lresume\@: + li t0, MPRV | MPV + csrc mstatus, t0 + CHECK \cause, \code + .endm + + .macro TRY_FETCH cause, code + li s1, 0 + lla s2, .Lresume\@ + li t0, MPP_M | MPRV | MPV + csrc mstatus, t0 + li t0, MPP_S | MPV + csrs mstatus, t0 + li t0, 0x4000 + csrw mepc, t0 + mret +.Lresume\@: + CHECK \cause, \code + .endm + + .text + .global _start +_start: + lla t0, trap + csrw mtvec, t0 + csrw medeleg, zero + PMP_ALL + + /* VS Sv39 maps VA 0x4000 to GPA 0x8000, using guest PPNs 4/5/6. */ + WRITE_PTE VS_ROOT, 0x1401 + WRITE_PTE VS_L1, 0x1801 + WRITE_PTE (VS_L0 + 0x20), 0x20cf + + /* Sv39x4 maps the three VS page tables and the final data page. */ + WRITE_PTE G_ROOT, (((G_L1 >> 12) << 10) | 1) + WRITE_PTE G_L1, (((G_L0 >> 12) << 10) | 1) + WRITE_PTE (G_L0 + 0x20), (((VS_ROOT >> 12) << 10) | 0xdf) + WRITE_PTE (G_L0 + 0x28), (((VS_L1 >> 12) << 10) | 0xdf) + WRITE_PTE (G_L0 + 0x30), (((VS_L0 >> 12) << 10) | 0xdf) + WRITE_PTE (G_L0 + 0x40), (((DATA >> 12) << 10) | 0xdf) + li t0, 0x8000000000000004 + csrw vsatp, t0 + li t0, (0x8000000000000000 | (G_ROOT >> 12)) + csrw hgatp, t0 + TRY_DATA 0, 0, 1 + + /* PMP denial while recursively reading a G-stage PTE. */ + PMP_PAGE G_ROOT, 0 + TRY_DATA 0, 5, 2 + TRY_DATA 1, 7, 3 + TRY_FETCH 1, 4 + + /* A denial of the translated VS PTE is also an access fault. */ + PMP_PAGE VS_L0, 0 + TRY_DATA 0, 5, 5 + PMP_ALL + + /* A genuine G-stage mapping failure must remain a guest-page faul= t. */ + WRITE_PTE (G_L0 + 0x20), 0 + TRY_DATA 0, 21, 6 + TRY_DATA 1, 23, 7 + TRY_FETCH 20, 8 + WRITE_PTE (G_L0 + 0x20), (((VS_ROOT >> 12) << 10) | 0xdf) + + /* An unmapped physical G-stage root produces PMA access faults. */ + li t0, 0x8000000000090000 + csrw hgatp, t0 + TRY_DATA 0, 5, 12 + TRY_DATA 1, 7, 13 + TRY_FETCH 1, 14 + + li t0, 0x8000000000000004 + csrw vsatp, t0 + li t0, (0x8000000000000000 | (G_ROOT >> 12)) + csrw hgatp, t0 + TRY_DATA 0, 0, 16 + li a0, 0 +fail: + lla a1, semiargs + li t0, 0x20026 + sd t0, 0(a1) + sd a0, 8(a1) + li a0, 0x20 + .balign 16 + slli zero, zero, 0x1f + ebreak + srai zero, zero, 0x7 + j . + + .balign 4 +trap: + csrr s1, mcause + csrw mepc, s2 + li t0, MPP_M | MPRV | MPV + csrc mstatus, t0 + li t0, MPP_M + csrs mstatus, t0 + mret + + .data + .balign 8 +semiargs: + .space 16 --=20 2.43.0 From nobody Sun Nov 29 02:09:51 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1795679739; cv=none; d=zohomail.com; s=zohoarc; b=bi2wfP5u9+ZraGHJHnXNw5BuGlPXYGJxF316Znk53w9XdWBkPNC7aDayrkiUz+E8DnOq6DbL9HxkvOHiReh3t3DWNST8iT/qDxtwQkO65dyV0ub0CjSMqKjeiTMT97zgfrN81g1olHLyFoalMxNIIqNQtUf9sEmz45z5vQD6fUo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795679739; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=YB7zpQZ5dthJk1KtAbItcRut6aFLG1DUMiC1rMDzGR8=; b=cikRw/AhMjj83txn0BBVN4rwj234tOv6OQuBQbxdNjc9HjPKl86zQB4f8IQVjwfFxJ9qSC2EJZTNRJOMB13a1sFSHC9ess6IzHz2PXDxXlaGehPqbqSwAa2WJVrj01OjhxZujtu7wVK5Jaysvi6CeQwT+c4UtQFvrWAKC6gFic0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179567973911630.152743470169753; Wed, 25 Nov 2026 23:55:39 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xWUJE-0006Wi-TC; Thu, 26 Nov 2026 02:54:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJD-0006U4-09; Thu, 26 Nov 2026 02:54:23 -0500 Received: from [115.124.30.98] (helo=out30-98.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJ7-0002aC-O9; Thu, 26 Nov 2026 02:54:22 -0500 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0XFTpGYj_1795679640 cluster:ay36) by smtp.aliyun-inc.com; Thu, 26 Nov 2026 15:54:00 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1795679641; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=YB7zpQZ5dthJk1KtAbItcRut6aFLG1DUMiC1rMDzGR8=; b=Oyb6gVwsDlYkVJ5dV2kI3WukLMDlurKxY0gTdIHB4tc06le/mYJvLLoUUHAeX5GkAdUdzxdo6WhwHy/nEcpzd0NGIQOLJrbbUI6x2Y+N37FxFLecMDmKpYrPCUfLJLzwToq+N6vkdfnTk0gLEBFBKmx2jqUM/h+GkYkKr9nipvM= X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R491e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=maildocker-contentspam033032089153; MF=zhiwei_liu@linux.alibaba.com; NM=1; PH=DS; RN=8; SR=0; TI=SMTPD_---0XFTpGYj_1795679640; From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , qemu-riscv@nongnu.org Subject: [PATCH 3/6] target/riscv: Preserve access faults during VS PTE A/D translation Date: Thu, 26 Nov 2026 15:53:47 +0800 Message-Id: <20261126075350.13022-4-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> References: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.98 (deferred) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=115.124.30.98; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-98.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1795679741261158500 Content-Type: text/plain; charset="utf-8" Updating a VS PTE's A/D bits requires G-stage translation of that PTE's guest-physical address with store access. This translation can itself need to set a G-stage PTE's D bit. A PMP denial of that physical write returns TRANSLATE_PMP_FAIL, but the VS walker replaces the result with TRANSLATE_G_STAGE_FAIL and records the VS PTE guest-physical address. The original access then reports a guest-page fault even though the mapping is valid and a physical page-table access was rejected. A PMA error returned by this recursive call is lost in the same way. Apply the physical-access-fault helper to the A/D translation result. Return PMP and PMA failures before setting fault_pte_addr, as in the VS PTE read path. Keeping the result lets the existing exception code select the access fault for the original operation. For example, a load whose implicit A/D update fails must still raise a load access fault, even though the recursive translation used store access. The guest physical fault address stays zero for this physical access error. Leave other G-stage failures on the existing guest-page-fault path. This changes error propagation, not the conditions for updating A/D bits or the atomic PTE write. The next commit extends the regression with a PMP-denied G-stage D-bit update after a successful read walk. Signed-off-by: LIU Zhiwei --- target/riscv/tcg/cpu_helper.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c index 90a3731214..548879ee6a 100644 --- a/target/riscv/tcg/cpu_helper.c +++ b/target/riscv/tcg/cpu_helper.c @@ -1549,6 +1549,9 @@ static int get_physical_address(CPURISCVState *env, h= waddr *physical, MMUIdx_U, false, true, is_debug, false); if (gpa_ret !=3D TRANSLATE_SUCCESS) { + if (is_physical_access_fault(gpa_ret)) { + return gpa_ret; + } if (fault_pte_addr) { *fault_pte_addr =3D pte_gpa >> 2; } --=20 2.43.0 From nobody Sun Nov 29 02:09:51 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1795679677; cv=none; d=zohomail.com; s=zohoarc; b=ng3vCODPZBhN6fKIA78oogOEyrGtoFL8XZ2LjlHAF7f3aXmQHp3bk7mTXJoJOegzw9sMH1X2iK00eQZ5fHKIRXyJZXNbTufZUG+sxV3pf7Y94BUQpB5GvW1vTOAxDFRLsJZ2uHAoo0xr1819XuY5vbB5yvcNQuqXsaDeZXI4ABE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795679677; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Qn405w8Fk25gD85AWEa0clQi1Oxu/bxaWlWUiQBI7OE=; b=Cjm6KdeOt3nX/UQPyzjwQQI2R/xaDYuS7DMBlnC/h2blz/X1KLJepdFDUyR4anSnE4w2lD8aEcTemZHUxkETBZNoyYxVAcx4H9mHMmGeLkS2DH20olXugc7PnmOy9i65DcpvCP+nnvrt0xp2Bsu/54tWupN7eEfqShdFG9xgTAc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1795679677653688.5665736425075; Wed, 25 Nov 2026 23:54:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xWUJC-0006TP-D3; Thu, 26 Nov 2026 02:54:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJA-0006Sp-PT; Thu, 26 Nov 2026 02:54:20 -0500 Received: from [115.124.30.118] (helo=out30-118.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJ6-0002aD-IH; Thu, 26 Nov 2026 02:54:20 -0500 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0XFTpGYt_1795679640 cluster:ay36) by smtp.aliyun-inc.com; Thu, 26 Nov 2026 15:54:01 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1795679642; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=Qn405w8Fk25gD85AWEa0clQi1Oxu/bxaWlWUiQBI7OE=; b=mfH72ltZ9clh4Oy0pNe8t/f/lrHKobY4szi4aDdW/6mQWVXrHZDP+XeK+mKK9xgS2Y8REavgdBBn2F8anLHJ6uSsJa2sxaAsDEYWxuI0KL18odJTw+0JGSWRhSEp+gcbm1+/wwMj33A0BqD6dM0z2iONt2gFxGEere3kxgP8zxw= X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R161e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=maildocker-contentspam033037009110; MF=zhiwei_liu@linux.alibaba.com; NM=1; PH=DS; RN=8; SR=0; TI=SMTPD_---0XFTpGYt_1795679640; From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , qemu-riscv@nongnu.org Subject: [PATCH 4/6] tests/tcg/riscv64: Check access faults during VS A/D updates Date: Thu, 26 Nov 2026 15:53:48 +0800 Message-Id: <20261126075350.13022-5-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> References: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.118 (deferred) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=115.124.30.118; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-118.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1795679678932158500 Content-Type: text/plain; charset="utf-8" Extend the nested-PTE-read test case to cover the recent A/D error propagat= ion fix. The initial read translation succeeds, and the failure must occur only= when VS A/D bit updating retries the G-stage translation with store access. Implement the scenario in the following steps: 1. Setup PTEs Clear A and D bits in the VS leaf PTE. For the G-stage PTE mapping that VS leaf-table page, keep A set but D clear. This allows the initial G-stage lo= ad to succeed without updating the G-stage PTE. 2. Configure PMP Set a higher-priority PMP entry for G_L0 to Read-Only, while keeping other physical memory accessible. Reading G-stage PTEs still works, but setting t= he leaf PTE's D bit is denied. 3. Test Guest Load Issue a guest load. Updating the VS PTE's A bit triggers a G-stage translat= ion retry with store access. This retry attempts to set the G-stage D bit and h= its the PMP denial. Verify that a Load Access Fault (cause 5) is raised, with m= tval holding the faulting address and mtval2 set to zero (failure code 9). 4. Test Guest Store Repeat with a guest store. Expect a Store Access Fault (cause 7) with the s= ame address checks (failure code 10). This verifies that the fault cause reflec= ts the original guest operation, rather than the implicit PTE access. 5. Verify & Cleanup Confirm that the G-stage leaf PTE's D bit remains clear (failure code 11). Restore PMP permissions and both leaf PTEs to maintain valid translation states for subsequent tests. Without the preceding fix, the first new load reports a guest-page fault and exits with code 9. This extension injects a PMP failure; it does not add a separate PMA-injection case for the A/D path. All prior read-path and genuine guest-page-fault checks remain in the test. Signed-off-by: LIU Zhiwei AI-used-for: tests --- tests/tcg/riscv64/test-gstage-access-fault.S | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/tcg/riscv64/test-gstage-access-fault.S b/tests/tcg/riscv= 64/test-gstage-access-fault.S index 61a0957270..e8ebb4b684 100644 --- a/tests/tcg/riscv64/test-gstage-access-fault.S +++ b/tests/tcg/riscv64/test-gstage-access-fault.S @@ -184,6 +184,24 @@ _start: TRY_FETCH 20, 8 WRITE_PTE (G_L0 + 0x20), (((VS_ROOT >> 12) << 10) | 0xdf) =20 + /* + * VS A/D updating re-runs G-stage with store semantics. Its leaf D + * update is rejected by PMP, after the initial read walk succeede= d. + */ + WRITE_PTE (VS_L0 + 0x20), 0x200f + WRITE_PTE (G_L0 + 0x30), (((VS_L0 >> 12) << 10) | 0x5f) + PMP_PAGE G_L0, 1 + TRY_DATA 0, 5, 9 + TRY_DATA 1, 7, 10 + li t0, (G_L0 + 0x30) + ld t1, 0(t0) + andi t1, t1, 0x80 + li a0, 11 + bnez t1, fail + PMP_ALL + WRITE_PTE (VS_L0 + 0x20), 0x20cf + WRITE_PTE (G_L0 + 0x30), (((VS_L0 >> 12) << 10) | 0xdf) + /* An unmapped physical G-stage root produces PMA access faults. */ li t0, 0x8000000000090000 csrw hgatp, t0 --=20 2.43.0 From nobody Sun Nov 29 02:09:51 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1795679679; cv=none; d=zohomail.com; s=zohoarc; b=HT8EFTwTcwLzU6gEfM+DVdDzK+0rwW7KojLeRWCpRKbdknix1bWpdts6yQKiI3c3j6ttVyqTW/Zv5iI4+CFzGzmDyGysZFazc96TBA6MI64OrDhKbAlD/q3cEtZ6gsYcyw2VGLdPEDjVmuuPJPv7zjYseEfsAdXlBgO7okOF0Mo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795679679; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=vYCedF9JL7Fe+P0lctusPYrpB5YtEkyEfMNwiYrUvuc=; b=M1TMG8/yfdGjDDC3dT4nzsqu45sZm1wDLaFXrdj3m08vLiJ5QZhu5kwHvQSGnzHQBs4IdujfU+5WDC8/y/8C0VkhPTXH4SlsOpxOHuHEqGxtwCSA0v1tW2p3FZ4JPUt5YVRZADAihhcIaPXdQhDsQJZv4+ug8ssQYlvfCoTOXaE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1795679679071733.707461135991; Wed, 25 Nov 2026 23:54:39 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xWUJE-0006WC-Hb; Thu, 26 Nov 2026 02:54:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJC-0006Tm-FS; Thu, 26 Nov 2026 02:54:22 -0500 Received: from [115.124.30.101] (helo=out30-101.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJ7-0002aa-BU; Thu, 26 Nov 2026 02:54:22 -0500 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0XFTpGZF_1795679641 cluster:ay36) by smtp.aliyun-inc.com; Thu, 26 Nov 2026 15:54:01 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1795679642; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=vYCedF9JL7Fe+P0lctusPYrpB5YtEkyEfMNwiYrUvuc=; b=Npq3Ay/pB2UhDRGEf2EWd2atETk5WuitRJIg4utWVKCqQnkqrSgBnef7grWYqKPyiLK2k7f5JaFrR0IgKY/+jMLuOuYIu57l4oGh8+shZ9ga7BIIUIMygzLDv+K8APVnv3qtLSjABVyyRFoM3UjS49jIxcXIgCMefr+jQVuw3vE= X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R121e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=maildocker-contentspam033045133197; MF=zhiwei_liu@linux.alibaba.com; NM=1; PH=DS; RN=8; SR=0; TI=SMTPD_---0XFTpGZF_1795679641; From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , qemu-riscv@nongnu.org Subject: [PATCH 5/6] target/riscv: Keep the guest fault address zero for G-stage PMA errors Date: Thu, 26 Nov 2026 15:53:49 +0800 Message-Id: <20261126075350.13022-6-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> References: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.101 (deferred) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=115.124.30.101; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-101.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1795679680881158500 Content-Type: text/plain; charset="utf-8" In the direct G-stage path, a failed physical PTE read returns TRANSLATE_PMA_FAIL. TLB fill already classifies it as an access fault. However, the code that fills guest_phys_fault_addr excludes only TRANSLATE_PMP_FAIL. It therefore records a guest-physical address for a PMA access fault. The trap receives the right cause but an incorrect nonzero htval or mtval2, suggesting a guest-page-fault address. Commit 8e090ecfa9e5 ("target/riscv/cpu_helper.c: add PMA access fault") added the PMA result and access-fault classification. The direct G-stage fault-address condition was not updated to match it. Use is_physical_access_fault() in that condition so PMP and PMA errors both skip the guest address assignment. TLB fill clears the field on entry, so skipping the assignment leaves it zero through trap delivery. Real G-stage translation failures still record their guest address. The PMA detection rules and access-fault cause selection do not change. The next commit tests this branch with VS-stage translation set to Bare and an inaccessible G-stage root. This bypasses the recursive VS PTE paths and isolates the fault-address problem from the earlier fixes. Signed-off-by: LIU Zhiwei --- target/riscv/tcg/cpu_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c index 548879ee6a..0755582402 100644 --- a/target/riscv/tcg/cpu_helper.c +++ b/target/riscv/tcg/cpu_helper.c @@ -1854,7 +1854,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, = int size, * level exception */ first_stage_error =3D false; - if (ret !=3D TRANSLATE_PMP_FAIL) { + if (!is_physical_access_fault(ret)) { env->guest_phys_fault_addr =3D (im_address | (address & (TARGET_PAGE_SIZE - 1))= ) >> 2; --=20 2.43.0 From nobody Sun Nov 29 02:09:51 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1795679722; cv=none; d=zohomail.com; s=zohoarc; b=QCT1qeKVgR0v0jV3Df0PAXj+1hCqSg3nicCw+ECwn/lyCbk6mrUpA+ufzYqiJZBt3EFKofJSARLVJTtnMvpurNygLCQuWfGtwdO9mf5hAbUYZUbd1nkqWbzGi6WfrdvIaFUBYynW91sOF2szKQbCQDU2poTxBc6HdRx/Ekt+cM0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795679722; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=D2A8UaPCHWKKyi8URUDzAN7fmTLjtcYvtIq1z7D4N04=; b=EfdPrsdK0s76SeD+DmCiaRFeP7M8FL2S04NAXpuMORI7tjliMlu+vPiEevOrxhVOfxrFFUCd+4DN8wnhym2nsSJgEf7/KItLyS/W599LXpmtGDPRAMLov7R6pt9xha9JF7WOkAxtZaG1dKCK7ORHo8PqnOZp700iTUWWpw5qRqM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179567972290447.919681643179274; Wed, 25 Nov 2026 23:55:22 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xWUJE-0006Uo-0S; Thu, 26 Nov 2026 02:54:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJC-0006Tk-EQ; Thu, 26 Nov 2026 02:54:22 -0500 Received: from [115.124.30.111] (helo=out30-111.freemail.mail.aliyun.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWUJ7-0002d0-FE; Thu, 26 Nov 2026 02:54:22 -0500 Received: from localhost.localdomain(mailfrom:zhiwei_liu@linux.alibaba.com fp:SMTPD_---0XFTpGZk_1795679642 cluster:ay36) by smtp.aliyun-inc.com; Thu, 26 Nov 2026 15:54:02 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1795679643; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=D2A8UaPCHWKKyi8URUDzAN7fmTLjtcYvtIq1z7D4N04=; b=L3vZ7szUv+1kC/jd8PXfc5MNpC7UDRp/N8Z3Y3SVgL13t3Q7H+Mtjcr1edT08+oNy16UKQ3SwBXPhClYnuU0Wu1kHshkM8Oae1bx+6Lk5L/UBrdJK4VyJ6q8XbAyxS7qsztYgttYZ6tYpQ68zdoZmoSD/2Pds02e0UN/9pyymss= X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R281e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=maildocker-contentspam033037026112; MF=zhiwei_liu@linux.alibaba.com; NM=1; PH=DS; RN=8; SR=0; TI=SMTPD_---0XFTpGZk_1795679642; From: LIU Zhiwei To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , qemu-riscv@nongnu.org Subject: [PATCH 6/6] tests/tcg/riscv64: Check PMA fault metadata with VS-stage Bare Date: Thu, 26 Nov 2026 15:53:50 +0800 Message-Id: <20261126075350.13022-7-zhiwei_liu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> References: <20261126075350.13022-1-zhiwei_liu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 115.124.30.111 (deferred) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=115.124.30.111; envelope-from=zhiwei_liu@linux.alibaba.com; helo=out30-111.freemail.mail.aliyun.com X-Spam_score_int: -166 X-Spam_score: -16.7 X-Spam_bar: ---------------- X-Spam_report: (-16.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.alibaba.com) X-ZM-MESSAGEID: 1795679725407158500 Content-Type: text/plain; charset="utf-8" Extend the existing H-extension regression to cover the preceding direct G-stage fault-address fix. The nested read tests already use an unmapped physical G-stage root. Disabling VS translation while that root remains selected reaches the direct G-stage error path without first reading a VS PTE. Implement the case in these steps: 1. Reuse the unmapped G-stage root at physical address 0x90000000 left by the preceding PMA read cases. Set vsatp to zero to select Bare. Keep the common trap handler and guest access helpers unchanged. 2. Load from virtual address 0x4000 with MPRV/MPV. The Bare first stage passes that address directly to G-stage. Reading its inaccessible root PTE returns a PMA failure. 3. Use CHECK to require load access fault 5, mtval equal to 0x4000 and mtval2 zero. Use failure code 15 for this case. Before the fix, the cause is already 5, but mtval2 is incorrectly 0x1000 (0x4000 >> 2), so the new case detects the metadata error specifically. 4. Restore VS Sv39 and the valid G-stage root. Confirm that the final normal load succeeds and return zero through semihosting. Retain all earlier nested-read, A/D-update and guest-page-fault checks. Signed-off-by: LIU Zhiwei AI-used-for: tests --- tests/tcg/riscv64/test-gstage-access-fault.S | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/tcg/riscv64/test-gstage-access-fault.S b/tests/tcg/riscv= 64/test-gstage-access-fault.S index e8ebb4b684..28d425808c 100644 --- a/tests/tcg/riscv64/test-gstage-access-fault.S +++ b/tests/tcg/riscv64/test-gstage-access-fault.S @@ -209,6 +209,10 @@ _start: TRY_DATA 1, 7, 13 TRY_FETCH 1, 14 =20 + /* Also check PMA failure during the non-recursive G-stage path. */ + csrw vsatp, zero + TRY_DATA 0, 5, 15 + li t0, 0x8000000000000004 csrw vsatp, t0 li t0, (0x8000000000000000 | (G_ROOT >> 12)) --=20 2.43.0