From nobody Mon Nov 23 04:17: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=1793217749; cv=none; d=zohomail.com; s=zohoarc; b=jgAfqaiUEaNdT/B/aMBDioGD0A4r6iW90KTAzXS3IFR+36Z9vWNVDyvCcCsbbqgNRrlBVsAPt1btnmNGR1Wocm/sBJh23TyTS3kL6rkKyXsa+rSpsyVIdLqTXjFLRtG0fVQ89oV9qSlNKkClXvfhq+WhUUo2N1Y3Hhjn7MAFGgg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793217749; 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=ayXbsNeWp2Luf8HW8I6Rc7+K+FZ7MWfouohP7qjFe2Q=; b=C7pKYCohmVJtRA7IKEVYKBxJO2ETEqToCb6eX8ZHU4pqSBTqCxFZEIMQ3OZ4y1wvVA4cXayQDNAISI5VKl0b1izJf303HCj9OY442QOm9Awv3hp7BXt8o7ZqPvLlpVXDQyb/Wtx7qYBOW7e2w8GIxYr6pY+TncJx5+6NIXZHdsI= 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 1793217749358703.7913778081128; Wed, 28 Oct 2026 13:02:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xM9qX-0001cC-5X; Wed, 28 Oct 2026 16:02:06 -0400 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 1xM9qE-0001Ya-IJ for qemu-devel@nongnu.org; Wed, 28 Oct 2026 16:01:46 -0400 Received: from mail-dl1-x122a.google.com ([2607:f8b0:4864:20::122a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xM9qA-0000q1-JF for qemu-devel@nongnu.org; Wed, 28 Oct 2026 16:01:45 -0400 Received: by mail-dl1-x122a.google.com with SMTP id a92af1059eb24-1a2af4c8d00so290340c88.0 for ; Wed, 28 Oct 2026 13:01:41 -0700 (PDT) Received: from tewaros-mbp.localdomain ([209.249.241.138]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1a75fdc1031sm8026427c88.19.2026.10.28.13.01.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Oct 2026 13:01:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=theorem-dev.20260707.gappssmtp.com; s=20260707; t=1793217700; x=1793822500; 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=ayXbsNeWp2Luf8HW8I6Rc7+K+FZ7MWfouohP7qjFe2Q=; b=X0Dxfv1Bnemo+1d8A+Wh5uUSRmHAk0tjxNg812X/Z0HHkwM3h1qhydjjSkX4wdAEN+ ZFzsBmZXy8FaPxGorOvL49nX6yjVdcskdzLoTKFQVae6l0Sp2aIEAVa/1mu9kZC5DqPp v2QnjMkqa9bIGeIZjpn/PTWvf54X4qu1J8YCQ0gsLU6YbcA4Lq/rnBBJKAN4lIpV+vfx suZsqENWxojWP9jtTGSjuJE2S4bd1ZmSoPeQ4JMaCqbjWaJdxqKqA4pFOmJyx+P4AF0d CORhM6YAfa8EDDrz6gG9ZAS08V5A4m0u88CtnSHgcvuAoxJnwG4hWsu8WyJop1/kUmrI fBNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793217700; x=1793822500; 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=ayXbsNeWp2Luf8HW8I6Rc7+K+FZ7MWfouohP7qjFe2Q=; b=IwUh0wQ186FE4QmQSmF6YnKanO1t7cTmyI2rJnBWlftelmqhdovx3M6aYlejZ57i4b p/I3gLgM90aCzAJJuDNA6HLCgBEZlfER65o23Ww7Kp4BGziioz6/7Pla4sJb1M0VaTz0 M5oyGWSEBOZSAcXp0Lb5KaBpdoOXUdcc2P/DYqsm84VcpK8s/UViCnupPa7UnpA7Dtxw GyKuy3Sj1Oi/MBG9QDfy+G0SyR0k9O7g+fbtTegbxVbAGSot50bUzXYZFvwVueS8y2el dhSfaSy2DcWeXx1vCDqa8KgLDZNTlyuotiwhEjHymb6NppcrIAu8tKbMeDzE5mcyO+OS xOmg== X-Gm-Message-State: AFq9FYKEjynjRBFNT8nMFdD/OUCi/vuzTpuf0b7jmIeb24LJiu5uQJmr c/kND/E5kyhx41+ZIEVhsSrQEMl2iYKHsGO8CyCPP3vGcRcGjTGmYHP/J1JRsfG6IKFwzsSi0IJ l5rOP X-Gm-Gg: AYBFou02rrK9p40LdhoyBJbV4rz5TF8jd/twDhRLRJET1xh3Jy4zXnvPPSlaN+Q1ekr GxKaqd4G3zY/B7+zC/hJBs5abk8sRbTDx3RwcfcecNg0VoG64/STAHqXJ/RwXKkumtTdkijYajC 0YnwaDnmaJeR2kf3/GUmquiBszz8M4H6/O5vm37kfRIv9WctDcH3VJrhG3hzu+gTZg7YWOAstBK rGFsF1g0bRrdSzWx+7yjpk507hTC1MlWmRTCL13lcuTt4+lCZ1C5cDSbpDkAuH+frDsCjY30ZeU 1soXcOVWXpUji2ncwJ6wttBOLZsyn6yFE8onSq8cLvuKuGhIGCZK9EhUS7eTh3SLObIOd5zw3ua zc5MMeILYUz9SMEeB1wzO6QycM+nj5yAttwaXj60pGu42isVC2seoqC4AE6DmiKZETwgxlqg1Ts 5j3WITaON39s6ylq2HYmR+DvuxAxbu4Jgmn9GNsdLp2PQKaAzQ4GRr2yb54k8RND6G64PoWeFBM tsrLwxOy8Vwy1OskhS6HKO9YereNFgRJg2hneI8DLPb8T2nwLiRcHsHCPZdsv5a01FDG+me6VZe CJmtF0/91BbnnfAuaQGZIpBF85fqaWcK4OqkEJEPFG63IHYjHpPmVcYch0ds X-Received: by 2002:a05:701b:2803:b0:1a7:979f:ecc0 with SMTP id a92af1059eb24-1a7979fecd8mr2710784c88.27.1793217699795; Wed, 28 Oct 2026 13:01:39 -0700 (PDT) From: tewaro To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, disclosures@theorem.dev, tewaro@tewaro.org, tewaro Subject: [PATCH v2 1/2] target/riscv: do not raise the no-PMP-rule access fault from mret Date: Wed, 28 Oct 2026 13:01:22 -0700 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: 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::122a; envelope-from=tewaro@theorem.dev; helo=mail-dl1-x122a.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: 1793217751052158500 Content-Type: text/plain; charset="utf-8" Consider a CPU that implements PMP but has no PMP entry configured. If M-mode executes mret with MPP set to S or U, QEMU raises an instruction access fault on the mret. The trap is taken in M-mode, with mepc pointing at the mret, mtval zero and MPP equal to M. The spec says the mret succeeds and the first S/U-mode fetch fails [1][2]. The trap should therefore be taken from S/U-mode, with mepc and mtval equal to the mret target [3][4] and MPP equal to S/U [5]. The early check in check_ret_from_m_mode() is what moves the fault onto the mret. Without it, the fetch at the target misses the TB and TLB caches, which are keyed by privilege level, and reaches pmp_hart_has_privs_default() through riscv_cpu_tlb_fill(). That function already denies S/U-mode when no rule matches. Nothing executes between the privilege switch and that fetch, so no other behaviour changes. As it stands, S/U-mode can never be entered on such a configuration, and a handler that steps mepc past the apparent fault resumes in M-mode at the S-mode entry point (GitLab issue #2074). The check was added by commit d102f19a208 alongside the pmp.c change that makes the no-rule path M-mode-only, and I believe it was already redundant then. Building v6.0.0 and v7.2.0 with only the check removed confirms the fetch faults as expected. Later commits (0fbb5d2d3c9, 4c48aad1, 3157a553, 16aa7771) changed its gating, cause and location but not the normal path. Remove it and the now unused prev_priv parameter. The bug was found and debugged with the help of Claude (Anthropic), used as a research and debugging aid; the patch was written by hand. [1] Privileged ISA v1.12, 3.7.1 "Physical Memory Protection CSRs", Priority and Matching Logic [2] Privileged ISA v1.12, 3.3.2 "Trap-Return Instructions" [3] Privileged ISA v1.12, 3.1.14 "Machine Exception Program Counter" [4] Privileged ISA v1.12, 3.1.16 "Machine Trap Value Register" [5] Privileged ISA v1.12, 3.1.6.1 "Privilege and Global Interrupt-Enable Stack in mstatus register" Privileged ISA v1.12 (ratified, 2021-12-03): https://github.com/riscv/riscv-isa-manual/releases/tag/Priv-v1.12 Current source, same wording: https://github.com/riscv/riscv-isa-manual/blob/4877e41a/src/priv/machine.ad= oc Fixes: d102f19a2085 ("target/riscv/pmp: Raise exception if no PMP entry is = configured") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2074 Signed-off-by: tewaro --- v2: rebased onto current master; no change. target/riscv/tcg/op_helper.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/target/riscv/tcg/op_helper.c b/target/riscv/tcg/op_helper.c index f9c79fe6b9..e46190079a 100644 --- a/target/riscv/tcg/op_helper.c +++ b/target/riscv/tcg/op_helper.c @@ -388,7 +388,6 @@ target_ulong HELPER(sret)(CPURISCVState *env) } =20 static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc, - privilege_mode_t prev_priv, uintptr_t ra) { if (!(env->priv >=3D PRV_M)) { @@ -400,11 +399,6 @@ static void check_ret_from_m_mode(CPURISCVState *env, = target_ulong retpc, env->misa_ext) && (retpc & 0x3)) { riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, ra); } - - if (riscv_cpu_cfg(env)->pmp && - !pmp_get_num_rules(env) && (prev_priv !=3D PRV_M)) { - riscv_raise_exception(env, RISCV_EXCP_INST_ACCESS_FAULT, ra); - } } static target_ulong ssdbltrp_mxret(CPURISCVState *env, target_ulong mstatu= s, privilege_mode_t prev_priv, @@ -430,7 +424,7 @@ target_ulong HELPER(mret)(CPURISCVState *env) privilege_mode_t prev_priv =3D get_field(mstatus, MSTATUS_MPP); uintptr_t ra =3D GETPC(); =20 - check_ret_from_m_mode(env, retpc, prev_priv, ra); + check_ret_from_m_mode(env, retpc, ra); =20 bool prev_virt =3D !!(get_field(env->mstatus, MSTATUS_MPV) && (prev_priv !=3D PRV_M)); @@ -548,7 +542,7 @@ target_ulong HELPER(mnret)(CPURISCVState *env) bool prev_virt; uintptr_t ra =3D GETPC(); =20 - check_ret_from_m_mode(env, retpc, prev_priv, ra); + check_ret_from_m_mode(env, retpc, ra); =20 prev_virt =3D !!(get_field(env->mnstatus, MNSTATUS_MNPV) && (prev_priv !=3D PRV_M)); --=20 2.54.0 (Apple Git-157) From nobody Mon Nov 23 04:17: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=1793217748; cv=none; d=zohomail.com; s=zohoarc; b=MiNbhREBm+RH3YjbQb3OzPSGJOFYK+iK5mXPGpadQ40QNbqfPU/FgZ8OC1rCV4I6RnuQSnB1x4eefl9vULPsp8pE7ApNzwTdU2D2kQ2hxV1mKSZOA05fJz7Azniy4aCuezUpRlZRBEnCseIKTIa8EqkOWMrQtSwxUJkYvwFEWsc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793217748; 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=XFu5zti7tL4VDp3C7aZKO6im+z/DBesDhXTe4RzRUoo=; b=IlJ+fotcalGBbRcB3ObQ7P0CSt+rEHMkd3iB80/mGomGZ8u30KADCzGPD35jXw09fLCGvlml+x9f3Kph4raFGsk3301RTtHwgfIpEcEdp31AXJRjJC1zu4g9NGjpfcmiD+2AWCGrsUusLwwC00uFF2YzyhoDLHcjeiSRoiGu12A= 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 1793217748172602.9280528214327; Wed, 28 Oct 2026 13:02:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xM9qb-0001d1-1h; Wed, 28 Oct 2026 16:02:12 -0400 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 1xM9qI-0001Yx-QM for qemu-devel@nongnu.org; Wed, 28 Oct 2026 16:02:02 -0400 Received: from mail-dl1-x122c.google.com ([2607:f8b0:4864:20::122c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xM9qG-0000qP-BF for qemu-devel@nongnu.org; Wed, 28 Oct 2026 16:01:50 -0400 Received: by mail-dl1-x122c.google.com with SMTP id a92af1059eb24-18cdcfc05ffso1991060c88.1 for ; Wed, 28 Oct 2026 13:01:46 -0700 (PDT) Received: from tewaros-mbp.localdomain ([209.249.241.138]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1a75f7fb28csm9913160c88.18.2026.10.28.13.01.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Oct 2026 13:01:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=theorem-dev.20260707.gappssmtp.com; s=20260707; t=1793217705; x=1793822505; 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=XFu5zti7tL4VDp3C7aZKO6im+z/DBesDhXTe4RzRUoo=; b=tksf7pNmyvb4zdiPDmup44I9tE5kHKRTW6L4qdpF26YnQQ3LcoR870vWfV8O4BFqL/ aRM7q6YfJwhI2D9XFVTPGs02bFzkrTcEZmIflY0RJVdh5Qbcg706W/ntfIz1J3ni05yF oryMM+7ln+jbFWsQpPUHWQrA7VIoTZ9au0IMKmU83Zi3U7OgEbOsc5o1GR1dbdL0Y0C2 +pAe9TP9TdvQKMu9eDvxmeT2R0OpjA6vWnnp6C3fbqAokfUa21pSCQPbcRvid/K2FhF/ xvy5EuMllbq/MTp7Qd8LDIJ+r1PXhM0Jll9YM1Dcm4EVS8T/4+wQ1WnwOzAW96JmWPp9 Xj6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793217705; x=1793822505; 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=XFu5zti7tL4VDp3C7aZKO6im+z/DBesDhXTe4RzRUoo=; b=ft0CwD2vAnOF76xNlbrAqu71dcFml70/FiYVTJAFZ5/kUsREsvoK2gFb38VuyDuPjE Dnj9RWsLa0YyVX84h8WoptcL0qDQiypM4HjD2iODSW9PKpjcVNxdGFYOa1guyehqkCc5 ix5jGeJ/YovBNKvMDP74YOyEicxGbe/2+lXvgMBXqilL/fL9XiqabCO9IdzYRF4kVEyB ooIrp4yFmDX5k1T18iwWbw2hApujqY6HQ/26Yw7LgAPJh8D9USBixXlHhF6AEE/FmMg4 4BBl8w5dQmpp7sQ8uIDTt6jbud64B0k6w13PHNWwanRYWhMjFR5rVRdXFBrrqureT9ag 2LfA== X-Gm-Message-State: AFq9FYKcLrYnAMrjkTdb2jBt+K30FP+Mt3qUXtnBax8JPOME7ndpFBk5 LRJ4743TMXVuf1Q5kauM0h/QJbG8/CrD7Wo4J84y1/h/BleZ50FbxgnfyXxnlynNeD+eCZrNL2+ qUz1x X-Gm-Gg: AYBFou3HMk2j+YU139OIQg13kQygDB+Rm7eBi3X53f5mLnqdu00YyN8N6QI1SwPLAKm TkmeuGfdCSMinfVEMvlr50MQQto3yHIcCuQsZrk27VKr33muqnN9tQs9x0JJom5/fhJrpkkOvvj iVWm9w8QaIZEIkugsSnBh2Cn3/bAqudI62+13fN1/FTb5NY5dU5gI9fFXsNUiPrwi6QeFjtP364 XXh73/B3+xRriYRw1Ndt8bFVJrU5nxhUQ06+H42xrczdbkpTXiUg1fnIf8IiLqpbRKF169KtmFL s04uSq/2EfOUHgA4VMJW7Qmy/ONDy1Vng1e1T27tDKtlad8PvyuTfmUlkTPjgFS5PsxlmJOmZoo 6YFUmC1FMZBxhEhU4emY/jxoWOaBr28+KNjCYOwFww4SnuFtreZBVnZCOBcyaUPaF3FdYLJkviT Qfo7px9TkJqHRgkVQ6s1Rh6zBVbo8s5d+c2umb6oUldkbStL78d8CfNtGP6f4TsaegRCWsSKzxu Q7o7TTRevCU/RdA9J8UbJ74txXfN8jlVxJnornfk9QUkhpSBK8JPN3SFCCmm7s7hSkWEWZ2XXFv 8amNo0pj4Ud2sD58WK3RlMInbYkXoORdTD/hTRr+1rMiqRjX7pH87iZDLwUXvg== X-Received: by 2002:a05:7022:618:b0:19f:527d:96da with SMTP id a92af1059eb24-1a6cdd3e006mr5991045c88.33.1793217704961; Wed, 28 Oct 2026 13:01:44 -0700 (PDT) From: tewaro To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, daniel.barboza@oss.qualcomm.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, disclosures@theorem.dev, tewaro@tewaro.org, tewaro Subject: [PATCH v2 2/2] tests/tcg/riscv64: add test for mret to S-mode with no PMP entry Date: Wed, 28 Oct 2026 13:01:23 -0700 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: 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::122c; envelope-from=tewaro@theorem.dev; helo=mail-dl1-x122c.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: 1793217750218154100 Content-Type: text/plain; charset="utf-8" Regression test for the previous commit. With PMP implemented and no entry configured, the test executes mret to S-mode and checks in the M-mode trap handler that the fault is an instruction access fault taken from S-mode on the mret target, with mcause 1, mepc and mtval equal to the target and MPP equal to S. It exits with 0 on success or the number of the failed check. Signed-off-by: tewaro Reviewed-by: Daniel Henrique Barboza --- v2: rebased onto current master; the meson.build entry moved to sit between the tests added since v1. No other change. tests/tcg/riscv64/system/meson.build | 1 + tests/tcg/riscv64/test-mret-nopmp.S | 100 +++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 tests/tcg/riscv64/test-mret-nopmp.S diff --git a/tests/tcg/riscv64/system/meson.build b/tests/tcg/riscv64/syste= m/meson.build index 3c8b018d9e..f7410c6a65 100644 --- a/tests/tcg/riscv64/system/meson.build +++ b/tests/tcg/riscv64/system/meson.build @@ -25,6 +25,7 @@ tests +=3D { 'test-minstret-ecall.S': setup + {'qemu_args': ['-icount', 'shift=3D1', = qemu_args]}, 'test-minstret-wfi.S': setup + {'qemu_args': ['-icount', 'shift=3D1', qe= mu_args]}, 'test-misa-w.S': setup + {'qemu_args': ['-cpu', 'rv64,x-misa-w=3Dtrue,c= =3Dtrue,v=3Dtrue', qemu_args]}, + 'test-mret-nopmp.S': setup + {'qemu_args': ['-cpu', 'rv64,pmp=3Dtrue', q= emu_args]}, 'test-zvabd.S': setup + {'qemu_args': ['-cpu', 'rv64,v=3Dtrue,vlen=3D256= ,x-zvabd=3Dtrue', qemu_args]}, 'test-vsepc-masking.S': setup + { 'test_name': 'test-vsepc-masking-c', diff --git a/tests/tcg/riscv64/test-mret-nopmp.S b/tests/tcg/riscv64/test-m= ret-nopmp.S new file mode 100644 index 0000000000..df20f35bc8 --- /dev/null +++ b/tests/tcg/riscv64/test-mret-nopmp.S @@ -0,0 +1,100 @@ +/* + * Test mret to S-mode when PMP is implemented but no PMP entry is + * configured. + * + * The mret must succeed and the first S-mode fetch must fail [1][2]. + * The trap is therefore taken from S-mode, with mcause 1, mepc and + * mtval equal to the mret target [3][4] and mstatus.MPP equal to S + * [5]. + * + * Exit code is 0 on success, otherwise the number of the failed check. + * + * [1] Privileged ISA v1.12, 3.7.1 "Physical Memory Protection CSRs", + * Priority and Matching Logic + * [2] Privileged ISA v1.12, 3.3.2 "Trap-Return Instructions" + * [3] Privileged ISA v1.12, 3.1.14 "Machine Exception Program Counter" + * [4] Privileged ISA v1.12, 3.1.16 "Machine Trap Value Register" + * [5] Privileged ISA v1.12, 3.1.6.1 "Privilege and Global + * Interrupt-Enable Stack in mstatus register" + * + * Privileged ISA v1.12 (ratified, 2021-12-03): + * https://github.com/riscv/riscv-isa-manual/releases/tag/Priv-v1.12 + * Current source, same wording: + * https://github.com/riscv/riscv-isa-manual/blob/4877e41a/src/priv/machin= e.adoc + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + + .option norvc + + .text + .global _start +_start: + /* Set up machine trap vector, handle everything in M-mode */ + lla t0, machine_trap_handler + csrw mtvec, t0 + csrw medeleg, zero + + /* mstatus.MPP =3D S */ + li t0, 0x1800 + csrc mstatus, t0 + li t0, 0x0800 + csrs mstatus, t0 + + /* Return to S-mode at `target` with no PMP entry configured */ + lla t0, target + csrw mepc, t0 + mret + +target: + /* Should be unreachable with no PMP */ + li a0, 5 + j _exit + +machine_trap_handler: + /* 1: mcause is instruction access fault */ + li a0, 1 + csrr t0, mcause + li t1, 1 + bne t0, t1, _exit + + /* 2: mepc =3D=3D target, not mret in _start */ + li a0, 2 + csrr t0, mepc + lla t1, target + bne t0, t1, _exit + + /* 3: mtval =3D=3D target because aligned instructions */ + li a0, 3 + csrr t0, mtval + bne t0, t1, _exit + + /* 4: trap from S-mode */ + li a0, 4 + csrr t0, mstatus + srli t0, t0, 11 + andi t0, t0, 3 + li t1, 1 + bne t0, t1, _exit + + li a0, 0 + j _exit + +/* Exit with semihosting */ +_exit: + lla a1, semiargs + li t0, 0x20026 /* ADP_Stopped_ApplicationExit */ + sd t0, 0(a1) + sd a0, 8(a1) + li a0, 0x20 /* TARGET_SYS_EXIT_EXTENDED */ + + /* Semihosting call sequence */ + slli zero, zero, 0x1f + ebreak + srai zero, zero, 0x7 + j . + + .data + .balign 8 +semiargs: + .space 16 --=20 2.54.0 (Apple Git-157)