From nobody Sat Oct 25 21:46:14 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1521229835632980.1834513918503; Fri, 16 Mar 2018 12:50:35 -0700 (PDT) Received: from localhost ([::1]:59234 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ewvMz-0005F2-BW for importer@patchew.org; Fri, 16 Mar 2018 15:50:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40326) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ewvFH-0007Tj-5X for qemu-devel@nongnu.org; Fri, 16 Mar 2018 15:42:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ewvFE-0003ll-7x for qemu-devel@nongnu.org; Fri, 16 Mar 2018 15:42:31 -0400 Received: from mail-pl0-x242.google.com ([2607:f8b0:400e:c01::242]:44988) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ewvFD-0003lV-Vu for qemu-devel@nongnu.org; Fri, 16 Mar 2018 15:42:28 -0400 Received: by mail-pl0-x242.google.com with SMTP id 9-v6so6503826ple.11 for ; Fri, 16 Mar 2018 12:42:27 -0700 (PDT) Received: from monty.com ([12.206.222.5]) by smtp.gmail.com with ESMTPSA id k24sm13780469pff.77.2018.03.16.12.42.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 16 Mar 2018 12:42:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8UQ+uSIzoZngR0+Oi4ohUDvu7FmGbnMTnM4v6VLcH4Q=; b=GZhPtMe4xEMVzZwzYPO+WWdJ2L2czb/88EhKsRlADA0JkJQPn4up8mVS3+lnshtU25 B7/YmA2+38Dv/6ml2NoGnpQtH5VxN9UqAvB3depfKk/vJFqrUSI27A9HFz6UeEAawJYC 5KWRM0GhhFJj9o4/Z/q2jr3XdFLDjzoDyQ4JmJs8lenAv22XeehgEBSbmwwdT0tZOuAT msSK0K65YLdTbqRwUDfRLvmJjcIVB16n1kjXV+zZ4slwO7ePdf9odxPmXHRAhx+fsvxE wimcrLhtoaL/3xgOlTzSWIjX36ffqLDRPkncCcRJSum6CdCI0fuD29pIxZ0NiBjw/PsV g/Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=8UQ+uSIzoZngR0+Oi4ohUDvu7FmGbnMTnM4v6VLcH4Q=; b=QRlXK4/4PaD55AG0VBa24I7vaSN9pZ8NZx5bOdcV2uxUd6yeMboZagIymHyW50q0l6 m1JEMhzpwKqkPXi434VnrYsiBGBXHBKsRhvCTbX8a1P8jHvWQAfaV1wcbMDw0uOcgEC8 BuJ3I3yeJM0GNykEEw6B//2m+T2mYrEilnsr+6ETwb2VPFjUkiaZyCgP3Qql92qA1fQK 2rBhmU0alWlaKBR0hX1GQwBFBp/fjgpdjPCgBfhKe5T2LhcBU3T1I4VOSgbWkmMi4qLV o1xc40mslqAxovQ5aPtGxnbtNvrqpI93b7fZECaxCVbLhUREKEe349S7ablPbTxne3Ah nNVA== X-Gm-Message-State: AElRT7FRKKaaLDdMxLmvAAUrt31+ph6EuJapC+6wlUkXtZHHAq8JwLi2 IQay5D3Oo21AKgZHA6THYdRYjIxGRco= X-Google-Smtp-Source: AG47ELuJFnjBvwFY77/2uYzh+jlEe7xrMjpOjCZeRherwBt5Ct7Tz9b4U2gKssqtPqYaQI2NPlAklw== X-Received: by 2002:a17:902:9a0a:: with SMTP id v10-v6mr3373444plp.35.1521229346917; Fri, 16 Mar 2018 12:42:26 -0700 (PDT) From: Michael Clark To: qemu-devel@nongnu.org Date: Fri, 16 Mar 2018 12:41:08 -0700 Message-Id: <1521229281-73637-12-git-send-email-mjc@sifive.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1521229281-73637-1-git-send-email-mjc@sifive.com> References: <1521229281-73637-1-git-send-email-mjc@sifive.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c01::242 Subject: [Qemu-devel] [PATCH v3 11/24] RISC-V: Improve page table walker spec compliance X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: patches@groups.riscv.org, Michael Clark , Palmer Dabbelt , Sagar Karandikar , Bastian Koppelmann Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" - Inline PTE_TABLE check for better readability - Improve readibility of User page U mode and SUM test - Disallow non U mode from fetching from User pages - Add reserved PTE flag check: W or W|X - Add misaligned PPN check - Set READ flag for PTE X flag if mstatus.mxr is in effect - Change access checks from ternary operator to if statements - Improves page walker comments - No measurable performance impact on dd test Cc: Sagar Karandikar Cc: Bastian Koppelmann Signed-off-by: Michael Clark Signed-off-by: Palmer Dabbelt --- target/riscv/cpu_bits.h | 2 -- target/riscv/helper.c | 59 ++++++++++++++++++++++++++++++++++-----------= ---- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 64aa097..12b4757 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -407,5 +407,3 @@ #define PTE_SOFT 0x300 /* Reserved for Software */ =20 #define PTE_PPN_SHIFT 10 - -#define PTE_TABLE(PTE) (((PTE) & (PTE_V | PTE_R | PTE_W | PTE_X)) =3D=3D P= TE_V) diff --git a/target/riscv/helper.c b/target/riscv/helper.c index e71633a..523a275 100644 --- a/target/riscv/helper.c +++ b/target/riscv/helper.c @@ -185,16 +185,36 @@ restart: #endif target_ulong ppn =3D pte >> PTE_PPN_SHIFT; =20 - if (PTE_TABLE(pte)) { /* next level of page table */ + if (!(pte & PTE_V)) { + /* Invalid PTE */ + return TRANSLATE_FAIL; + } else if (!(pte & (PTE_R | PTE_W | PTE_X))) { + /* Inner PTE, continue walking */ base =3D ppn << PGSHIFT; - } else if ((pte & PTE_U) ? (mode =3D=3D PRV_S) && !sum : !(mode = =3D=3D PRV_S)) { - break; - } else if (!(pte & PTE_V) || (!(pte & PTE_R) && (pte & PTE_W))) { - break; - } else if (access_type =3D=3D MMU_INST_FETCH ? !(pte & PTE_X) : - access_type =3D=3D MMU_DATA_LOAD ? !(pte & PTE_R) && - !(mxr && (pte & PTE_X)) : !((pte & PTE_R) && (pte & PTE_= W))) { - break; + } else if ((pte & (PTE_R | PTE_W | PTE_X)) =3D=3D PTE_W) { + /* Reserved leaf PTE flags: PTE_W */ + return TRANSLATE_FAIL; + } else if ((pte & (PTE_R | PTE_W | PTE_X)) =3D=3D (PTE_W | PTE_X))= { + /* Reserved leaf PTE flags: PTE_W + PTE_X */ + return TRANSLATE_FAIL; + } else if ((pte & PTE_U) && ((mode !=3D PRV_U) && + (!sum || access_type =3D=3D MMU_INST_FETCH))) { + /* User PTE flags when not U mode and mstatus.SUM is not set, + or the access type is an instruction fetch */ + return TRANSLATE_FAIL; + } else if (ppn & ((1ULL << ptshift) - 1)) { + /* Misasligned PPN */ + return TRANSLATE_FAIL; + } else if (access_type =3D=3D MMU_DATA_LOAD && !((pte & PTE_R) || + ((pte & PTE_X) && mxr))) { + /* Read access check failed */ + return TRANSLATE_FAIL; + } else if (access_type =3D=3D MMU_DATA_STORE && !(pte & PTE_W)) { + /* Write access check failed */ + return TRANSLATE_FAIL; + } else if (access_type =3D=3D MMU_INST_FETCH && !(pte & PTE_X)) { + /* Fetch access check failed */ + return TRANSLATE_FAIL; } else { /* if necessary, set accessed and dirty bits. */ target_ulong updated_pte =3D pte | PTE_A | @@ -202,11 +222,14 @@ restart: =20 /* Page table updates need to be atomic with MTTCG enabled */ if (updated_pte !=3D pte) { - /* if accessed or dirty bits need updating, and the PTE is - * in RAM, then we do so atomically with a compare and swa= p. - * if the PTE is in IO space, then it can't be updated. - * if the PTE changed, then we must re-walk the page table - as the PTE is no longer valid */ + /* + * - if accessed or dirty bits need updating, and the PTE = is + * in RAM, then we do so atomically with a compare and s= wap. + * - if the PTE is in IO space or ROM, then it can't be up= dated + * and we return TRANSLATE_FAIL. + * - if the PTE changed by the time we went to update it, = then + * it is no longer valid and we must re-walk the page ta= ble. + */ MemoryRegion *mr; hwaddr l =3D sizeof(target_ulong), addr1; enum { success, translate_fail, restart_walk} action =3D s= uccess; @@ -249,15 +272,15 @@ restart: target_ulong vpn =3D addr >> PGSHIFT; *physical =3D (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT; =20 - if ((pte & PTE_R)) { + /* set permissions on the TLB entry */ + if ((pte & PTE_R) || (mode !=3D PRV_U && (pte & PTE_X) && mxr)= ) { *prot |=3D PAGE_READ; } if ((pte & PTE_X)) { *prot |=3D PAGE_EXEC; } - /* only add write permission on stores or if the page - is already dirty, so that we don't miss further - page table walks to update the dirty bit */ + /* add write permission on stores or if the page is already di= rty, + so that we TLB miss on later writes to update the dirty bit= */ if ((pte & PTE_W) && (access_type =3D=3D MMU_DATA_STORE || (pte & PTE_D))) { *prot |=3D PAGE_WRITE; --=20 2.7.0