From patchwork Mon Mar 19 21:18:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Clark X-Patchwork-Id: 10295231 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9414760386 for ; Mon, 19 Mar 2018 21:29:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8277F29433 for ; Mon, 19 Mar 2018 21:29:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 75A2429444; Mon, 19 Mar 2018 21:29:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B43AE29433 for ; Mon, 19 Mar 2018 21:29:32 +0000 (UTC) Received: from localhost ([::1]:44028 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ey2LT-0001sk-Ky for patchwork-qemu-devel@patchwork.kernel.org; Mon, 19 Mar 2018 17:29:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33243) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ey2C9-0001Ng-N7 for qemu-devel@nongnu.org; Mon, 19 Mar 2018 17:19:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ey2C8-0002ez-74 for qemu-devel@nongnu.org; Mon, 19 Mar 2018 17:19:53 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:41568) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ey2C7-0002eA-SM for qemu-devel@nongnu.org; Mon, 19 Mar 2018 17:19:52 -0400 Received: by mail-pg0-x241.google.com with SMTP id m24so1750449pgv.8 for ; Mon, 19 Mar 2018 14:19:51 -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=d+r4xPkbAJrkiFVFuCCGsm8x868xSaeLvaLnPtnLoW8=; b=Aihcn3aR0VMP3CAv1LVnuHN1fey590vSZHFcewVvql8r+zqS6S0r8nQ86Yf58PutSy bYIgzLglC0EqOm6gsLmWYsrQk5jRWgZSurbFJWmeJVrQXOSnbbBnPT+8wxzdeFox/iBo ZRxjWHU8q2kkZLgn9Mzljm8z3yZXBA+vGcIk3du2m3QWca+b35bxQIAUNcCskpFxacHM Z2fe7Yrk92ElkAAMhZ7KZW0FUFP1q+86WKkZoI0hqIqI4UH2ajXC7qajqGcw+kT3qZhV CWQRXIC15w3DSVsFgRHwSeCZsp9Gx9MUSTm/SVlNXdZ4aIm8b+b04tklr45jkPsxYwmA oFQQ== 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=d+r4xPkbAJrkiFVFuCCGsm8x868xSaeLvaLnPtnLoW8=; b=NQhjxlw9OMlfI7V1EG3ZygU3Q64JEkoryRtgDUqxyuRycjm1r0jxJmGd6szFDjhDo0 WjQc9FzFCqL1Xdwc1Lvs/Zxed0yUSghlZt+AoE332RznJ6tv2pTjexFtLYVC1nQol1Tu 5U9c1NZWNA7xREwkPBn9jhXBffGOGjWgmCcVZYbxgo3QxIF/5ttqlnwAJ9RWPOAbG0up 6YXZM3KqxV1ZNnUPruIwaCNW3UtgOIv/etX+Wn3iNHSi/e0HNQkUWIAjDBv4TNNQK3Gr whgBFMJv5nwjv+v4Qz/vKqMXUacacOo2t7/GWX6PFRinVKcZqBq8SqhjhuOQnd5GoVi7 WzHA== X-Gm-Message-State: AElRT7GNheqazbJev+/XRkcyyYNR4Px4v0NS0Uu6eEhtXVw1TrDIzG0y GxnmWdpMYsGFp9Wsk9gXr2JcI34HjEg= X-Google-Smtp-Source: AG47ELtPWbVDKBfPyPAwiX760Kl64f+4JWdz6GigMjLX5to/N/dANvTiCqSbAewxBci/xmZMUN5gCw== X-Received: by 10.99.150.2 with SMTP id c2mr10270017pge.352.1521494390580; Mon, 19 Mar 2018 14:19:50 -0700 (PDT) Received: from monty.com ([12.206.222.5]) by smtp.gmail.com with ESMTPSA id i127sm120500pfc.136.2018.03.19.14.19.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 19 Mar 2018 14:19:49 -0700 (PDT) From: Michael Clark To: qemu-devel@nongnu.org Date: Mon, 19 Mar 2018 14:18:34 -0700 Message-Id: <1521494329-19546-12-git-send-email-mjc@sifive.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1521494329-19546-1-git-send-email-mjc@sifive.com> References: <1521494329-19546-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:c05::241 Subject: [Qemu-devel] [PATCH v4 11/26] 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP - 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 */ #define PTE_PPN_SHIFT 10 - -#define PTE_TABLE(PTE) (((PTE) & (PTE_V | PTE_R | PTE_W | PTE_X)) == PTE_V) diff --git a/target/riscv/helper.c b/target/riscv/helper.c index d148bed..c68826b 100644 --- a/target/riscv/helper.c +++ b/target/riscv/helper.c @@ -185,16 +185,36 @@ restart: #endif target_ulong ppn = pte >> PTE_PPN_SHIFT; - 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 = ppn << PGSHIFT; - } else if ((pte & PTE_U) ? (mode == PRV_S) && !sum : !(mode == PRV_S)) { - break; - } else if (!(pte & PTE_V) || (!(pte & PTE_R) && (pte & PTE_W))) { - break; - } else if (access_type == MMU_INST_FETCH ? !(pte & PTE_X) : - access_type == 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)) == PTE_W) { + /* Reserved leaf PTE flags: PTE_W */ + return TRANSLATE_FAIL; + } else if ((pte & (PTE_R | PTE_W | PTE_X)) == (PTE_W | PTE_X)) { + /* Reserved leaf PTE flags: PTE_W + PTE_X */ + return TRANSLATE_FAIL; + } else if ((pte & PTE_U) && ((mode != PRV_U) && + (!sum || access_type == 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 == MMU_DATA_LOAD && !((pte & PTE_R) || + ((pte & PTE_X) && mxr))) { + /* Read access check failed */ + return TRANSLATE_FAIL; + } else if (access_type == MMU_DATA_STORE && !(pte & PTE_W)) { + /* Write access check failed */ + return TRANSLATE_FAIL; + } else if (access_type == 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 = pte | PTE_A | @@ -202,11 +222,14 @@ restart: /* Page table updates need to be atomic with MTTCG enabled */ if (updated_pte != pte) { - /* if accessed or dirty bits need updating, and the PTE is - * in RAM, then we do so atomically with a compare and swap. - * 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 swap. + * - if the PTE is in IO space or ROM, then it can't be updated + * 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 table. + */ MemoryRegion *mr; hwaddr l = sizeof(target_ulong), addr1; enum { success, translate_fail, restart_walk} action = success; @@ -252,15 +275,15 @@ restart: target_ulong vpn = addr >> PGSHIFT; *physical = (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT; - if ((pte & PTE_R)) { + /* set permissions on the TLB entry */ + if ((pte & PTE_R) || (mode != PRV_U && (pte & PTE_X) && mxr)) { *prot |= PAGE_READ; } if ((pte & PTE_X)) { *prot |= 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 dirty, + so that we TLB miss on later writes to update the dirty bit */ if ((pte & PTE_W) && (access_type == MMU_DATA_STORE || (pte & PTE_D))) { *prot |= PAGE_WRITE;