From nobody Mon Nov 23 04:25:56 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 ARC-Seal: i=1; a=rsa-sha256; t=1793691079; cv=none; d=zohomail.com; s=zohoarc; b=NqzlOwTwZ9mMIi249cr92+ijrh/+DSXj37AX/DPTw+JyixfszMvCZkfeBWE3MAuwXkHrwb1ZxZMF7JP7FwHyOCvQBG5/Gx8VVsLnYUB6VQlaKrU3rZtysa1SepYGwIIxca0X3cSXKVzuRhNosS5wMgQ5z99FKYgePJzvCZg9AU4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793691079; 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=uMDgBHFCfShr3L6FJGE9YvoXrYO8PUeN2gLlvwmxBXw=; b=gBK+l55zP1XruCC9+GEYSkR2TcMNI2umn0l7bclvgN50MlKJ6NJ3vQc9XRuFo25yvWCs8Iz86vcKfTOrMx80CL1ZGtxVZ761tGzHihMtJKvcm3tJA8K/F5UuT5E5nto/Nn1q1L8ompHSsJ73zx0MaGK5h9Bl6pZlmG4l7MPoY/I= 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793691079253430.94954911555715; Mon, 2 Nov 2026 23:31:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xO8yD-00032o-FT; Tue, 03 Nov 2026 02:30:13 -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 1xO8yC-00032O-9q for qemu-devel@nongnu.org; Tue, 03 Nov 2026 02:30:12 -0500 Received: from mail-dl2-x03.google.com ([2607:f8b0:4864:38::3]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xO8yA-00068k-AK for qemu-devel@nongnu.org; Tue, 03 Nov 2026 02:30:11 -0500 Received: by mail-dl2-x03.google.com with SMTP id a92af1059eb24-1b22fcac215so43284c88.0 for ; Mon, 02 Nov 2026 23:30:09 -0800 (PST) Received: from tewaros-MacBook-Pro.local ([2601:647:6300:1230:9da3:3d82:7292:c937]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b106edc7f8sm23226592c88.9.2026.11.02.23.30.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2026 23:30:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=theorem-dev.20260707.gappssmtp.com; s=20260707; t=1793691009; x=1794295809; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uMDgBHFCfShr3L6FJGE9YvoXrYO8PUeN2gLlvwmxBXw=; b=WSketthnBtHAdY6DOg43+bAdjqwp+8agAIlah6EseUfmcB6ejyaWaAh1kSFWxc8JUV S8RItEmIx8vdHY45b/R6HM9RPDjR4bE+73yJOeNf0RWM+Iz30B5b5bvXXVC5jGCwRGN9 NA4V4/EeBt3LddTB7I3lI38fmfO2N5SHHbY0d+r2gC1FKau1Jm6rea72ekeWZMmYyngM XXnQCMbfFvmbCz3o6r5XxwLneQfN5jwvmYv5s0GUr/jMHxgKttgII44lKcBPzCetmRJn n4QUjngPsLaBVZtyEyPqvNxfCsTxGXqXrtJUvpVkjWStAHkGtt0wMNYWvhqGm/D2BU3G S1LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793691009; x=1794295809; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=uMDgBHFCfShr3L6FJGE9YvoXrYO8PUeN2gLlvwmxBXw=; b=1V7+31jLUWHtcJ3ogbPB455zgO3JAJ2sjN7RuqzihWU792QN8bKkc7k0spH2i6FU26 iGU5Zvsoaw6V89kJN7iNHKl21AjREWQLi+ZBiltgbqwqwa/onU8ssJmVH14IQLA0fgOA Z8JRoXEyraVnihOyk4T1O4RXGJvbhT+o27lMUIJGzHIQNOJJhEHpUKrqF7USLXLV+/pp k76AjgtWLXBndfnqLq3Q1XL9x5AVqJr0hv3az7TkMp755g+9jqCuHig4nIpBfvbHreRU j+AH8d5/5mwLVodqFc8PZx4XFheN1mgUpujS8p2kVpKdsIYJ43GvhBn7wi4Bd3jXm6QY By3A== X-Gm-Message-State: APuU4Z93CsA0qPdj7SxxKInclmpjThLDQWLG9pxNj3BlsY+YQddqBXvd qtcOv9+d1hZmJ8N5gc+Ksf27TzxPPe+95wJ/lC4Y20oYAvRc7EU//0nxbsEkuj35Q+SVk4bFivz +D21F7zViwm8P X-Gm-Gg: AYBFou0lo7Sseq6e3siutzzCwB+S1jP/r/Nxa//YKLACOZX5Um56Ja7KwzomJ6Fu7IX lXkpv36uIE9kij4DiJRcP1VGjPDbLxmgKN6JJvgjTk5BRRHrVE0uC/8AcVqpwf9Ae35PtI/IntZ GPBFd5oBrDZHoYzy7r2/e7aKBpCs2YLvR7kw2PtCrCEcFActLL8fpHRqB4g3Z3ezPbG9oECYqMp tHUEMH/YUtlbJ4dFwGJwm2VwJiowR0z3Qr1840HDgge6rSVyLZujNQE+XWpOE0TI7ro/0/FEwWD uiFcmMMyq1uSgAwR4knwMYNzw9QV76uXeJW1aClWZA6ZkGWeGAehLKmBmnyS60tSQTD38tBMzp1 43X2kZF/Vthc2Cq4oqZD20T+GMguuixGWt54TnC0z9ffrJyHzCJ/t8U9ySyww3yfC33rU8+bYHY p3icitinoy4k7VyOy6jsC+Ju+huzJOPmKmyocLfpnhp8HIMIeRM6E+EOD2NKRW2pwT5O68Fx6qk ppxkpHT3zfBRmLwXHHqnk+re8t2TuL28iBqLkgxtFFlkfl/i0X/WANZnstQKE9C4G+ocgqvlQmD RBVHtaDhRkqSPLnTK77EJf1P6xGomPyhTYF1jTo0a5HHj9QsQ7DbS/ceNTW6KvkhV/LwZQE= X-Received: by 2002:a05:7023:903:b0:17f:7280:1332 with SMTP id a92af1059eb24-1ac5c92f6e4mr20648389c88.40.1793691008732; Mon, 02 Nov 2026 23:30:08 -0800 (PST) From: tewaro To: qemu-devel@nongnu.org Cc: disclosures@theorem.dev, tewaro , imaginos32@gmail.com, Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , qemu-riscv@nongnu.org (open list:RISC-V TCG CPUs) Subject: [PATCH 1/2] target/riscv: report the write pseudoinstruction for a failed VS-stage A/D update Date: Mon, 2 Nov 2026 23:29:52 -0800 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: <20260705140508.17692-1-imaginos32@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:38::3; envelope-from=tewaro@theorem.dev; helo=mail-dl2-x03.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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 @theorem-dev.20260707.gappssmtp.com) X-ZM-MESSAGEID: 1793691083305158500 Content-Type: text/plain; charset="utf-8" When a G-stage fault is taken during a VS-stage page-table walk, riscv_cpu_do_interrupt() writes a pseudoinstruction to mtinst/htinst. This instruction says if the walk's access was a read (0x3000) or a write (0x3020) of the PTE [1]. Commit 366b4fba0fae made the A/D update check G-stage write permission, and sets the write bit when the trap cause is a store guest-page fault. The trap cause is the wrong signal. The cause follows the original access [2], but the pseudoinstruction describes the implicit access that failed. The two disagree in two cases: original access failed access Spike reports QEMU reports load or fetch A/D write 0x3020 0x3000 store PTE read 0x3000 0x3020 They agree only for a store that fails on the A/D write, which is the case 366b4fba0fae was tested with. Record in the CPU state that the G-stage fault came from the A/D write, and use that for both the data and the instruction tinst sites. Clear it on entry to riscv_cpu_tlb_fill() and after the trap. [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 Fixes: 366b4fba0fae ("target/riscv: check G-stage write permission for VS-s= tage A/D updates") Signed-off-by: tewaro Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu.h | 1 + target/riscv/tcg/cpu_helper.c | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 49a1186bdb..d56566d8c8 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -445,6 +445,7 @@ struct CPUArchState { * address translation for the VS-stage page table walk. */ bool two_stage_indirect_lookup; + bool two_stage_indirect_write; =20 uint32_t scounteren; uint32_t mcounteren; diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c index 58e98e975c..063525cfc5 100644 --- a/target/riscv/tcg/cpu_helper.c +++ b/target/riscv/tcg/cpu_helper.c @@ -1544,6 +1544,7 @@ static int get_physical_address(CPURISCVState *env, h= waddr *physical, if (fault_pte_addr) { *fault_pte_addr =3D pte_gpa >> 2; } + env->two_stage_indirect_write =3D true; return TRANSLATE_G_STAGE_FAIL; } } @@ -1785,6 +1786,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, = int size, hwaddr tlb_size =3D TARGET_PAGE_SIZE; =20 env->guest_phys_fault_addr =3D 0; + env->two_stage_indirect_write =3D false; =20 qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n", __func__, address, access_type, mmu_idx); @@ -2233,7 +2235,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) */ tinst =3D (riscv_cpu_xlen(env) =3D=3D 32) ? 0x00002000 : 0= x00003000; =20 - if (cause =3D=3D RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT) { + if (env->two_stage_indirect_write) { tinst |=3D 0x20; } } else { @@ -2256,6 +2258,9 @@ void riscv_cpu_do_interrupt(CPUState *cs) * doing VS-stage page table walk. */ tinst =3D (riscv_cpu_xlen(env) =3D=3D 32) ? 0x00002000 : 0= x00003000; + if (env->two_stage_indirect_write) { + tinst |=3D 0x20; + } } break; case RISCV_EXCP_ILLEGAL_INST: @@ -2506,6 +2511,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) =20 env->two_stage_lookup =3D false; env->two_stage_indirect_lookup =3D false; + env->two_stage_indirect_write =3D false; } =20 #endif /* !CONFIG_USER_ONLY */ --=20 2.54.0 (Apple Git-157) From nobody Mon Nov 23 04:25:56 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 ARC-Seal: i=1; a=rsa-sha256; t=1793691079; cv=none; d=zohomail.com; s=zohoarc; b=Rybz1eYB7pKq7I7lmfYg/9xRFnkhJ5kXQHZ8Fy/tWvZJ2rjJmaWyUtnL5q4hW+jjWCI/xylC9x7jzcjwDGxtFuJzMljXVxNtOm11+Aun3stV72NEB/U8RjZwmN3bLIoOA3zP04VXLbFuskXWFPox5E5nLYmnSZ8Jm6usNqFGHus= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793691079; 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=tdxloZwBKDAgrM5BDk8OY8vLGqrFHrCnnVzbdNEEzCQ=; b=JFS1PotxuFDs/6g4ZJnF+uJZ9u/BRhqOt6AskjSxocM+egFOjKLHUh5HLZN6A9QaUOBf6tcIG+TGUCu4l0l7z07VfL2HZMU1Tuq8cvdsQMzoPMXcsubfM3NCEcc7RGpqmJOlWS0c/SSDxy4vntS5h68J2wPYY8q8NcrIn44983o= 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793691079286259.9878828583552; Mon, 2 Nov 2026 23:31:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xO8yN-00036V-8i; Tue, 03 Nov 2026 02:30:23 -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 1xO8yL-00034r-4P for qemu-devel@nongnu.org; Tue, 03 Nov 2026 02:30:21 -0500 Received: from mail-dy1-x1335.google.com ([2607:f8b0:4864:20::1335]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xO8yI-00069K-F8 for qemu-devel@nongnu.org; Tue, 03 Nov 2026 02:30:20 -0500 Received: by mail-dy1-x1335.google.com with SMTP id 5a478bee46e88-35c76e9614aso5346901eec.0 for ; Mon, 02 Nov 2026 23:30:18 -0800 (PST) Received: from tewaros-MacBook-Pro.local ([2601:647:6300:1230:9da3:3d82:7292:c937]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-36a42935b4asm59114352eec.16.2026.11.02.23.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2026 23:30:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=theorem-dev.20260707.gappssmtp.com; s=20260707; t=1793691017; x=1794295817; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=tdxloZwBKDAgrM5BDk8OY8vLGqrFHrCnnVzbdNEEzCQ=; b=gNSzJNRX+rMFG2nsc2kVQPUehvEp8BYWV0BeZzQ54nmqM6vIiOGsBmKuTwjz3nmkAm SV3Fza8Mm8nswzLROSknwVDu406V7toJRTL/cqgjPh+SnisUy3+CJRWbgopiuc6Jpcoj 9LmbiV6J32suUqf5jT4Uhdjub+abGrwoHHk5sBdu3LvPXL0k27uDAemrm/7sGr/bHsFi LU36jSG/PWjBDi8b7GcO5UJ8nC9W9Zm9VF88k/GzxqpZsCJDXXv8Q5QhAOiKWya4Px0M h4I+L4AnsXE/Db66my1/oIOGuXtTjTk2/YFRqWMO3FtKIFcKdgnLApzrH9U8dsPL9KAo 13EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793691017; x=1794295817; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=tdxloZwBKDAgrM5BDk8OY8vLGqrFHrCnnVzbdNEEzCQ=; b=KLb27lZz4nZ2QhNhzHosJW8i0fOOjLPryPCQELIwuqZg8opaUyml4GdKNEICzwbH2G iUIjtaEVKjwUEUeC3sonBs1fmLlEntNTH4jOqMgWliDw91Vo3UHMApzacMU1p8jISpRG fIAuaMhNcTOyxRiSjSifAGmHLj0SBjRACtQ0/KPGrk1SvIaK+lJZ4ULRhKrsM6yrv6Zb gOFxD9b60JvUZpqsn6P0HsPMCRQt7ZGt/G+lNK3M9MN1+3A795ntcnEnG6mWni/aE7YT uZzwi9HsRzgOmiw4cS/UQj7IJ+X6VJUmawWFembu+KWLR2Ljzza2ZEXI+6pEh3S5HPE5 8j9w== X-Gm-Message-State: APuU4Z/fL9oO8AfiZpNUnbsMPkSpa4nlDnH5KYlw9KWkitEgbSX9CSvm OrcO9bBqdUQIEp++M5pbTNu1+VpGNMHihTXCJAx8GbqAuHv4pi8rx62PjGiz/sneWLWaZa1LQ+Z sR1WA2X2iPA== X-Gm-Gg: AYBFou33uLUhnSbahno+XGJSq9j2ES7wNjh2JK3TAWQi26wzkEYxY2+evvIyA3q73lQ 3+b3m7eExwUoz3DMm2jugKyyzd9UMRF1YICjSjNQl3eTeaq3m2LUQIfDv4eDJjzBWEyry4pSWZY mK1a3jlHP6zGu+FvJGYmOX3eYyVJ/ZsXW32/Gb+QkCsZr8VwJy0V+SlOiBfopSyItIBZKWEeFVo FiSHuPV0hC1rV0ofZkIcMIkC8u72N3ciX2rt5ngFU4of2vkle2uDuHCwLsUIFxTCXBtBCGVBIri S6MGqAO4H2sz3tgXEOm+lcjxcjssjP9r+A26wErFv6WhH5gGOplURdTJVKdNYspoDeVUiSPRdFw gS+D0pd6yklVxz4jHcVot2MpQu+1Gw4iV/l/ck4xZzmJ6hLRNo7N/d/voJTN4SFCXq9JkxY70Lt Hg+1/+3cpm1t9Y73xujrxBjsbQYYLloBWELmPUf1oYfKQcKn1zn5JxpoNnIbbndo49JG6QRCs22 sKjpVgNZkJY2kgt/a/4IyUCCz+4tEI7EMFlJkUFUzfGLWuYd2C6RHM1BUSPtXReCmpqYha4puqS UwhDmeDmOtvsavYD+AeD8zxElc/13FRbUxo33aFCg0d+XcS1dNIWbD1oFrz//ztPgAqFIQo= X-Received: by 2002:a05:7301:4545:b0:36c:9ac2:475b with SMTP id 5a478bee46e88-36c9ac25267mr244778eec.0.1793691016380; Mon, 02 Nov 2026 23:30:16 -0800 (PST) From: tewaro To: qemu-devel@nongnu.org Cc: disclosures@theorem.dev, tewaro , imaginos32@gmail.com, Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Chao Liu , qemu-riscv@nongnu.org (open list:RISC-V TCG CPUs) Subject: [PATCH 2/2] tests/tcg/riscv64: test faults on implicit VS-stage page-table accesses Date: Mon, 2 Nov 2026 23:29:53 -0800 Message-ID: <32390ac8968372d34e299974d20db5fe25bbe2f1.1793677944.git.tewaro@theorem.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: References: <20260705140508.17692-1-imaginos32@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::1335; envelope-from=tewaro@theorem.dev; helo=mail-dy1-x1335.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @theorem-dev.20260707.gappssmtp.com) X-ZM-MESSAGEID: 1793691083343158500 Content-Type: text/plain; charset="utf-8" When a guest accesses memory, the hardware walks the guest's page tables. The guest reads the entries and, with Svadu, writes the A/D bits. The G-stage checks these reads and writes like any other guest access. If it refuses one, the guest-page fault reports: - cause: the type of the guest's original access - mtval2/htval: the guest-physical address of the entry, >> 2 - mtinst/htinst: 0x3000 if the walk was reading, 0x3020 if writing test-gstage-tinst checks 19 cases: case access from G-stage refuses reports ------------ ------ ---- --------------- ----------------- load ld VS A write load GPF, 0x3020 fetch jr VS A write fetch GPF, 0x3020 store sd VS D write store GPF, 0x3020 store-ad sd VS A/D write store GPF, 0x3020 amo amoadd VS D write store GPF, 0x3020 load-4k ld VS A write load GPF, 0x3020 load-1g ld VS A write load GPF, 0x3020 load-vu ld VU A write load GPF, 0x3020 hlv hlv.d HS A write load GPF, 0x3020 hsv hsv.d HS D write store GPF, 0x3020 load-hs ld VS A write load GPF, 0x3020 store-hs sd VS D write store GPF, 0x3020 hlv-noread hlv.d HS root PTE read load GPF, 0x3000 hsv-noread hsv.d HS root PTE read store GPF, 0x3000 fetch-noread fetch VS root PTE read fetch GPF, 0x3000 noadue-h ld VS - load PF noadue-m ld VS - load PF noadue-store sd VS - store PF control sd VS the store store GPF - Leaves are 2 MiB, except in load-4k and load-1g. - Faults trap to M-mode, except in load-hs and store-hs. - The noadue cases turn ADUE off, so nothing is written and the fault is an ordinary page fault. - In control the guest itself stores into its read-only tables. That is not a walk, so tinst must be neither 0x3000 nor 0x3020. Booting, the page tables and entering the guest are in hyp-guest.h and hyp-guest.S, for reuse by other hypervisor tests. The trap handlers, which make the checks, are in the test. Each case is its own test, built with the -D flags listed in meson.build, and exits with 0 or the number of the first check that failed. Happy to test in a different way if that is desired. Signed-off-by: tewaro Reviewed-by: Daniel Henrique Barboza --- 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 ++++++++++++++++++++++++++ 4 files changed, 393 insertions(+) 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 diff --git a/tests/tcg/riscv64/hyp-guest.S b/tests/tcg/riscv64/hyp-guest.S new file mode 100644 index 0000000000..31dc37810c --- /dev/null +++ b/tests/tcg/riscv64/hyp-guest.S @@ -0,0 +1,110 @@ +/* + * Routines for the riscv64 hypervisor-extension system tests targeting + * page-table translations between the host and guest. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#include "hyp-guest.h" + +#define FLUSH fence rw, rw; sfence.vma; hfence.vvma; hfence.gvma; fence.i + + .option norvc + .text + .global _start, set_tables_perm, run_guest, exit +_start: + lla sp, stack_top + lla t0, trap + csrw mtvec, t0 + lla t0, hs_trap + csrw stvec, t0 + li t0, -1 /* no PMP restrictions */ + csrw pmpaddr0, t0 + li t0, 0x1f + csrw pmpcfg0, t0 + li t0, ADUE /* hardware A/D updating */ + csrw menvcfg, t0 + csrw henvcfg, t0 + li t0, 0x100 /* hstatus.SPVP: hlv/hsv act as VS= */ + csrs hstatus, t0 + + /* G-stage: regions 0-24, 1:1, all accessible */ + li t0, G_ROOT + 2 * 8 + li t1, table(G_L1) + sd t1, 0(t0) + li t0, G_L1 + li t1, leaf(REGION(0), G_RW) + li t2, 0x200000 >> 2 + li t3, 25 +1: sd t1, 0(t0) + add t1, t1, t2 + addi t0, t0, 8 + addi t3, t3, -1 + bnez t3, 1b + + /* guest: the code, the code again for VU, the 4 KiB table */ + li t0, VS_ROOT + 2 * 8 + li t1, table(VS_L1) + sd t1, 0(t0) + li t0, VS_L1 + li t1, leaf(REGION(0), G_RW & ~PTE_U) + sd t1, 0(t0) + li t1, leaf(REGION(0), G_RO & ~PTE_W) + sd t1, 24 * 8(t0) + li t1, table(VS_L0) + sd t1, 8 * 8(t0) + + li t0, (8 << 60) | (G_ROOT >> 12) + csrw hgatp, t0 + li t0, (8 << 60) | (VS_ROOT >> 12) + csrw vsatp, t0 + FLUSH + + call test + li a0, 1 /* test returned: no trap */ + +/* exit through semihosting with status a0 */ +exit: + lla a1, semiargs + li t0, 0x20026 /* ADP_Stopped_ApplicationExit */ + sd t0, 0(a1) + sd a0, 8(a1) + li a0, 0x20 /* SYS_EXIT_EXTENDED */ + .balign 16 + slli zero, zero, 0x1f + ebreak + srai zero, zero, 0x7 +1: j 1b + +set_tables_perm: + li t0, leaf(REGION(2), 0) + or t1, a0, t0 + li t0, G_L1 + 2 * 8 + sd t1, 0(t0) + FLUSH + ret + +/* mret into the mode: mstatus.MPV =3D 1 for a guest, MPP =3D S or U */ +run_guest: + li t0, (3 << 11) | (1 << 39) + csrc mstatus, t0 + li t0, HS + beq a2, t0, 2f + li t0, 1 << 39 /* MPV: a guest mode */ + csrs mstatus, t0 + li t0, VU + bne a2, t0, 2f + li t0, REGION(24) - REGION(0) /* VU: the user copy */ + add a1, a1, t0 + j 3f +2: li t0, 1 << 11 /* MPP =3D S */ + csrs mstatus, t0 +3: csrw mepc, a1 + mret + + .data + .balign 8 +semiargs: .dword 0, 0 + .bss + .balign 16 + .space 4096 +stack_top: diff --git a/tests/tcg/riscv64/hyp-guest.h b/tests/tcg/riscv64/hyp-guest.h new file mode 100644 index 0000000000..20ecef70a5 --- /dev/null +++ b/tests/tcg/riscv64/hyp-guest.h @@ -0,0 +1,71 @@ +/* + * Shared definitions for the riscv64 hypervisor-extension system tests, + * whose routines are in hyp-guest.S. hyp-guest.S: + * + * 1. Boots the machine in M-mode, sets up the stack and tells the CPU + * which trap handlers to use. + * 2. Builds the guest's page tables and the G-stage (host) page tables. + * 3. Calls `test`, which starts the test. + * + * The test file provides: + * - test: where the test starts, called by _start after setup + * - trap: the M-mode trap handler + * - hs_trap: the HS-mode trap handler + * + * Helpers a test can call: + * - set_tables_perm(a0): set what the G-stage lets the guest do to its + * own page tables. a0 is G_RW, G_RO or G_NOREAD. + * - run_guest(a0, a1, a2): run the code at address a1 in mode a2 (VS, VU + * or HS), with a0 passed on to it in a0. It does not return: the + * code's trap goes to the test's handler. + * - exit(a0): end the test with status a0, 0 for a pass, otherwise the + * number of the check that failed. + * + * Memory is split into 2 MiB regions; region n starts at + * REGION(n) =3D 0x80000000 + n * 2 MiB. Both stages map each region to + * itself. + * + * 0 code and the G-stage page tables + * 1 data and the stack + * 2 the guest's page tables + * 3-23 free for the test + * 24 region 0's code again, user-accessible, for VU-mode + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#define PTE_V 0x01 +#define PTE_R 0x02 +#define PTE_W 0x04 +#define PTE_X 0x08 +#define PTE_U 0x10 +#define PTE_A 0x40 +#define PTE_D 0x80 +#define leaf(pa, flags) (((pa) >> 2) | PTE_V | (flags)) +#define table(pa) (((pa) >> 2) | PTE_V) + +#define REGION(n) (0x80000000 + (n) * 0x200000) +#define G_ROOT 0x80104000 +#define G_L1 0x80108000 +#define VS_ROOT 0x80400000 +#define VS_L1 0x80401000 +#define VS_L0 0x80402000 /* 4 KiB table for region 8 */ + +/* G-stage permission of the guest's tables */ +#define G_RW (PTE_R | PTE_W | PTE_X | PTE_U | PTE_A | PTE_D) +#define G_RO (G_RW & ~PTE_W) +#define G_NOREAD (G_RO & ~PTE_R) + +/* run_guest modes */ +#define VU 0 +#define HS 1 +#define VS 2 + +#define LOAD_PF 13 +#define STORE_PF 15 +#define FETCH_GPF 20 +#define LOAD_GPF 21 +#define STORE_GPF 23 +#define TINST_READ 0x3000 /* walk was reading a page-table entry */ +#define TINST_WRITE 0x3020 /* walk was writing A/D */ +#define ADUE (1 << 61) diff --git a/tests/tcg/riscv64/system/meson.build b/tests/tcg/riscv64/syste= m/meson.build index 3c8b018d9e..05ed8b4c8f 100644 --- a/tests/tcg/riscv64/system/meson.build +++ b/tests/tcg/riscv64/system/meson.build @@ -53,6 +53,83 @@ tests +=3D { } } =20 +hyp_guest =3D files('../hyp-guest.S')[0] + +# test-gstage-tinst.S: one build per case; the flags are documented there +gstage_tinst =3D { + # the guest's tables are read-only, so the walk's A/D write is refused + 'load': ['-DMODE=3DVS', '-DACCESS=3Dld t0, 0(a0)', '-DREG=3D3', + '-DFLAGS=3DPTE_R', '-DCAUSE=3DLOAD_GPF'], + 'fetch': ['-DMODE=3DVS', '-DACCESS=3Djr a0', '-DREG=3D4', + '-DFLAGS=3D(PTE_R|PTE_X)', '-DCAUSE=3DFETCH_GPF'], + 'store': ['-DMODE=3DVS', '-DACCESS=3Dsd zero, 0(a0)', '-DREG=3D5', + '-DFLAGS=3D(PTE_R|PTE_W|PTE_A)', + '-DCAUSE=3DSTORE_GPF'], + 'store-ad': ['-DMODE=3DVS', '-DACCESS=3Dsd zero, 0(a0)', '-DREG=3D6', + '-DFLAGS=3D(PTE_R|PTE_W)', + '-DCAUSE=3DSTORE_GPF'], + 'amo': ['-DMODE=3DVS', '-DACCESS=3Damoadd.d t0, zero, (a0)', '-DRE= G=3D7', + '-DFLAGS=3D(PTE_R|PTE_W|PTE_A)', + '-DCAUSE=3DSTORE_GPF'], + 'load-4k': ['-DMODE=3DVS', '-DACCESS=3Dld t0, 0(a0)', '-DREG=3D8', + '-DPTE_ADDR=3DVS_L0', '-DFLAGS=3DPTE_R', + '-DCAUSE=3DLOAD_GPF'], + 'load-1g': ['-DMODE=3DVS', '-DACCESS=3Dld t0, 0(a0)', + '-DADDR=3D(0xC0000000+9*0x200000)', '-DPTE_ADDR=3D(VS_ROOT= +3*8)', + '-DPTE_PA=3DREGION(0)', '-DFLAGS=3DPTE_R', + '-DCAUSE=3DLOAD_GPF'], + 'load-vu': ['-DMODE=3DVU', '-DACCESS=3Dld t0, 0(a0)', '-DREG=3D10', + '-DFLAGS=3D(PTE_R|PTE_U)', '-DCAUSE=3DLOAD_GPF'], + 'hlv': ['-DMODE=3DHS', '-DACCESS=3Dhlv.d t0, (a0)', '-DREG=3D11', + '-DFLAGS=3DPTE_R', '-DCAUSE=3DLOAD_GPF'], + 'hsv': ['-DMODE=3DHS', '-DACCESS=3Dhsv.d zero, (a0)', '-DREG=3D12', + '-DFLAGS=3D(PTE_R|PTE_W|PTE_A)', + '-DCAUSE=3DSTORE_GPF'], + 'load-hs': ['-DMODE=3DVS', '-DACCESS=3Dld t0, 0(a0)', '-DREG=3D13', + '-DFLAGS=3DPTE_R', '-DCAUSE=3DLOAD_GPF', + '-DTO_HS'], + 'store-hs': ['-DMODE=3DVS', '-DACCESS=3Dsd zero, 0(a0)', '-DREG=3D14', + '-DFLAGS=3D(PTE_R|PTE_W|PTE_A)', + '-DCAUSE=3DSTORE_GPF', '-DTO_HS'], + # the tables are unreadable, so the walk fails reading root entry 2 + 'hsv-noread': ['-DMODE=3DHS', '-DACCESS=3Dhsv.d zero, (a0)', '-DREG=3D15= ', + '-DFLAGS=3D(PTE_R|PTE_W|PTE_A)', '-DPERM=3DG_NOREAD', + '-DENTRY=3D(VS_ROOT+2*8)', '-DCAUSE=3DSTORE_GPF', + '-DTINST=3DTINST_READ'], + 'hlv-noread': ['-DMODE=3DHS', '-DACCESS=3Dhlv.d t0, (a0)', '-DREG=3D16', + '-DFLAGS=3D(PTE_R|PTE_A)', '-DPERM=3DG_NOREAD', + '-DENTRY=3D(VS_ROOT+2*8)', '-DCAUSE=3DLOAD_GPF', + '-DTINST=3DTINST_READ'], + 'fetch-noread': ['-DMODE=3DVS', '-DREG=3D17', '-DADDR=3Dprobe', + '-DFLAGS=3D(PTE_R|PTE_X|PTE_A)', '-DPERM=3DG_NOREAD', + '-DENTRY=3D(VS_ROOT+2*8)', '-DCAUSE=3DFETCH_GPF', + '-DTINST=3DTINST_READ'], + # A/D updating off: an ordinary page fault, nothing written + 'noadue-h': ['-DMODE=3DVS', '-DACCESS=3Dld t0, 0(a0)', '-DREG=3D21', + '-DFLAGS=3DPTE_R', '-DHENVCFG=3D0', '-DCAUSE=3DLOAD_PF', + '-DENTRY=3D0', '-DTINST_NOT_WALK'], + 'noadue-m': ['-DMODE=3DVS', '-DACCESS=3Dld t0, 0(a0)', '-DREG=3D22', + '-DFLAGS=3DPTE_R', '-DMENVCFG=3D0', '-DCAUSE=3DLOAD_PF', + '-DENTRY=3D0', '-DTINST_NOT_WALK'], + 'noadue-store': ['-DMODE=3DVS', '-DACCESS=3Dsd zero, 0(a0)', '-DREG=3D23= ', + '-DFLAGS=3D(PTE_R|PTE_W|PTE_A)', '-DHENVCFG=3D0', + '-DCAUSE=3DSTORE_PF', '-DENTRY=3D0', '-DTINST_NOT_WALK'= ], + # control: the guest itself stores into its read-only tables + 'control': ['-DMODE=3DVS', '-DACCESS=3Dsd zero, 0(a0)', '-DREG=3D2', + '-DFLAGS=3D(PTE_R|PTE_W|PTE_A|PTE_D)', '-DADDR=3D(VS_ROOT+= 0x800)', + '-DENTRY=3D(VS_ROOT+0x800)', '-DCAUSE=3DSTORE_GPF', + '-DTINST_NOT_WALK'], +} +foreach name, flags : gstage_tinst + tests +=3D { + 'test-gstage-tinst.S': { + 'exe_name': 'test-gstage-tinst-' + name, + 'cflags': cflags + ['-march=3Drv64gch', hyp_guest] + flags, + 'qemu_args': ['-cpu', 'rv64,h=3Dtrue,svadu=3Dtrue', qemu_args], + } + } +endforeach + tests +=3D { 'test-vle32ff.S': { 'cflags': cflags + ['-march=3Drv64gcv'], diff --git a/tests/tcg/riscv64/test-gstage-tinst.S b/tests/tcg/riscv64/test= -gstage-tinst.S new file mode 100644 index 0000000000..8c9c7b831f --- /dev/null +++ b/tests/tcg/riscv64/test-gstage-tinst.S @@ -0,0 +1,135 @@ +/* + * Test what a fault reports when the G-stage refuses the guest's + * page-table walk: its read of a page-table entry, or its write of the + * A/D bits. + * + * Each build is one case, set by -D flags in meson.build: + * - MODE: VS, VU or HS (HS runs hlv/hsv on the guest's behalf) + * - ACCESS: the instruction run on ADDR + * - REG: the region accessed; ADDR defaults to its start + * - FLAGS: the guest's page-table entry for ADDR + * - PERM: the G-stage permission of the guest's tables (default G_RO) + * - TO_HS: take the fault in HS-mode instead of M-mode + * - MENVCFG, HENVCFG: turn ADUE off + * - PTE_ADDR, PTE_PA: where the entry is and where it points, for the + * 4 KiB and 1 GiB cases + * - CAUSE, ENTRY, TINST, TINST_NOT_WALK: what the fault must report + * + * The trap handler checks, and the exit status is the first that fails: + * 1 cause CAUSE + * 2 tval ADDR + * 3 tval2 ENTRY >> 2, the entry the walk failed on (default: the + * guest's entry for ADDR) + * 4 tinst TINST: 0x3020 if the walk was writing A/D (default), + * 0x3000 if reading; with TINST_NOT_WALK, neither + * 5 the entry is unchanged, so the refused A/D write did not happen + * + * `trap` checks a fault taken in M-mode. `hs_trap` checks one taken in + * HS-mode, using scause, stval, htval and htinst. + * + * Privileged ISA v1.12: 8.5.1, 8.6.3 Table 8.12. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#include "hyp-guest.h" + +/* exit with status n unless reg =3D=3D want */ +#define CHECK(n, reg, want) li t6, want; li a0, n; bne reg, t6, exit + +#ifdef REG +#ifndef ADDR +#define ADDR REGION(REG) +#endif +#ifndef PTE_ADDR +#define PTE_ADDR (VS_L1 + (REG) * 8) +#endif +#ifndef PTE_PA +#define PTE_PA REGION(REG) +#endif +#endif +#ifndef ENTRY +#define ENTRY PTE_ADDR +#endif +#ifndef PERM +#define PERM G_RO +#endif +#ifndef TINST +#define TINST TINST_WRITE +#endif + + .option norvc + .text + .global test, trap, hs_trap +test: +#ifdef MENVCFG + li t0, MENVCFG /* A/D updating off */ + csrw menvcfg, t0 +#endif +#ifdef HENVCFG + li t0, HENVCFG + csrw henvcfg, t0 +#endif +#ifdef TO_HS + li t0, 1 << CAUSE + csrw medeleg, t0 +#endif + li t0, PTE_ADDR /* the guest's entry for ADDR */ + li t1, leaf(PTE_PA, FLAGS) + sd t1, 0(t0) +#if REG >=3D 3 + li t0, REGION(REG) /* an ecall for a jump to land on = */ + li t1, 0x00000073 + sw t1, 0(t0) +#endif + li a0, PERM + call set_tables_perm + + ld a0, addr + lla a1, probe + li a2, MODE + call run_guest + +probe: +#ifdef ACCESS + ACCESS +#endif + ecall + +/* the fault, as reported by CSRs cause, tval, tval2 and tinst */ +.macro check_fault cause, tval, tval2, tinst + csrr t5, \cause + CHECK(1, t5, CAUSE) + csrr t5, \tval + ld t6, addr + li a0, 2 + bne t5, t6, exit + csrr t5, \tval2 + CHECK(3, t5, (ENTRY) >> 2) + csrr t5, \tinst +#ifdef TINST_NOT_WALK + li a0, 4 + li t6, TINST_READ + beq t5, t6, exit + li t6, TINST_WRITE + beq t5, t6, exit +#else + CHECK(4, t5, TINST) +#endif + li t0, PTE_ADDR + ld t5, 0(t0) + CHECK(5, t5, leaf(PTE_PA, FLAGS)) + li a0, 0 + j exit +.endm + + .balign 4 +trap: + check_fault mcause, mtval, mtval2, mtinst + + .balign 4 +hs_trap: + check_fault scause, stval, htval, htinst + + .data + .balign 8 +addr: .dword ADDR --=20 2.54.0 (Apple Git-157)