From patchwork Sun Jul 16 03:56:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ram Pai X-Patchwork-Id: 789022 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3x9DWt3VWqz9s4s for ; Sun, 16 Jul 2017 14:50:50 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="YF1InTJ2"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3x9DWt2KJqzDqmc for ; Sun, 16 Jul 2017 14:50:50 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="YF1InTJ2"; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Received: from mail-qk0-x243.google.com (mail-qk0-x243.google.com [IPv6:2607:f8b0:400d:c09::243]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3x9CNF4TSnzDqmf for ; Sun, 16 Jul 2017 13:59:09 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="YF1InTJ2"; dkim-atps=neutral Received: by mail-qk0-x243.google.com with SMTP id v17so14724463qka.3 for ; Sat, 15 Jul 2017 20:59:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=fXokGeCb1w9ldQl+wzQbYiN0Hi1IcNVgDHENebLUFco=; b=YF1InTJ2rTG3FfRK1sDr52KogwVNTe6PIsxMMRVUzf0KA+gapG/wuj0Lrh/ZgTJYPD EHakkv1E3zru9qvdEzlYyuSo3kw1LZC0Xeu5y9mtzJNJtIcq8lHLr0FVreBgigZb/uxC F3S3ijdfX2HslmRrzALF2B2SL4baEr6Zb/hBes/RQfepi1MgslQ/yw9m8LUvOXKYE3qK lSwrI4L1bDgUDR4UHAr48BRoaly1LJgVPL8BfteMavh7HVi7t0F0wnfIW1wXBEnTHhNT 7zxN+1Ut/QQ1kXnz0BJsPWPLSZP89mIwrM7HAwjyrrT/wKbY+eJ+HtY0/T7fDLP/Nm14 AQEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=fXokGeCb1w9ldQl+wzQbYiN0Hi1IcNVgDHENebLUFco=; b=FCElC5eLy8gaeeByoE6PAU3zoZ89KWWwk5LwxBRghxFEl61V03fDoJpRM8kDt34K10 OEcos/bczE/8PGpo6PxjgOhtFIOR9Roso1M6sOvRYZHIr/7ofU7HKnZr9sC99n8q8Z1F uuMFjG6ss+ZxVX4tv5tBUl3Ut+5fuy1mrPcBoZ8XDiRNX5MzWlWAJo+tAtZyIkCwEp9S C/vP/ds8x7il7MYS7weFEhmTBuIIn2a7kT7UtrbsQlTnqmaxb1mWOwVetOB0O0g03fky 9QxEuTmELiJwV56Rd/twcpcWy7uSNXBc2FypcncfGp2Gl5aruVthU+4jS1Epe+OmU4DX 9ogA== X-Gm-Message-State: AIVw111JpUWRDUfTLK8yY+FYsS4q+6xPAKyiaS6IIOJYSPOrPf5a4+p6 Pyz5wovY9kyeK7HZfrU= X-Received: by 10.55.3.130 with SMTP id 124mr18470166qkd.31.1500177547664; Sat, 15 Jul 2017 20:59:07 -0700 (PDT) Received: from localhost.localdomain (50-39-103-96.bvtn.or.frontiernet.net. [50.39.103.96]) by smtp.gmail.com with ESMTPSA id q46sm2290345qtc.54.2017.07.15.20.59.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 15 Jul 2017 20:59:07 -0700 (PDT) From: Ram Pai To: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, x86@kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC v6 27/62] powerpc: helper to validate key-access permissions of a pte Date: Sat, 15 Jul 2017 20:56:29 -0700 Message-Id: <1500177424-13695-28-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500177424-13695-1-git-send-email-linuxram@us.ibm.com> References: <1500177424-13695-1-git-send-email-linuxram@us.ibm.com> X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: arnd@arndb.de, corbet@lwn.net, linuxram@us.ibm.com, mhocko@kernel.org, dave.hansen@intel.com, mingo@redhat.com, paulus@samba.org, aneesh.kumar@linux.vnet.ibm.com, akpm@linux-foundation.org, khandual@linux.vnet.ibm.com Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" helper function that checks if the read/write/execute is allowed on the pte. Signed-off-by: Ram Pai --- arch/powerpc/include/asm/book3s/64/pgtable.h | 4 +++ arch/powerpc/include/asm/pkeys.h | 12 +++++++++ arch/powerpc/mm/pkeys.c | 33 ++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 0 deletions(-) diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h b/arch/powerpc/include/asm/book3s/64/pgtable.h index 30d7f55..0056e58 100644 --- a/arch/powerpc/include/asm/book3s/64/pgtable.h +++ b/arch/powerpc/include/asm/book3s/64/pgtable.h @@ -472,6 +472,10 @@ static inline void write_uamor(u64 value) mtspr(SPRN_UAMOR, value); } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +extern bool arch_pte_access_permitted(u64 pte, bool write, bool execute); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ + #define __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h index bbb5d85..7a9aade 100644 --- a/arch/powerpc/include/asm/pkeys.h +++ b/arch/powerpc/include/asm/pkeys.h @@ -53,6 +53,18 @@ static inline u64 pte_to_hpte_pkey_bits(u64 pteflags) ((pteflags & H_PAGE_PKEY_BIT4) ? HPTE_R_KEY_BIT4 : 0x0UL)); } +static inline u16 pte_to_pkey_bits(u64 pteflags) +{ + if (!pkey_inited) + return 0x0UL; + + return (((pteflags & H_PAGE_PKEY_BIT0) ? 0x10 : 0x0UL) | + ((pteflags & H_PAGE_PKEY_BIT1) ? 0x8 : 0x0UL) | + ((pteflags & H_PAGE_PKEY_BIT2) ? 0x4 : 0x0UL) | + ((pteflags & H_PAGE_PKEY_BIT3) ? 0x2 : 0x0UL) | + ((pteflags & H_PAGE_PKEY_BIT4) ? 0x1 : 0x0UL)); +} + static inline int vma_pkey(struct vm_area_struct *vma) { if (!pkey_inited) diff --git a/arch/powerpc/mm/pkeys.c b/arch/powerpc/mm/pkeys.c index 403f5ae..1794e17 100644 --- a/arch/powerpc/mm/pkeys.c +++ b/arch/powerpc/mm/pkeys.c @@ -201,3 +201,36 @@ int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, */ return vma_pkey(vma); } + +static bool pkey_access_permitted(int pkey, bool write, bool execute) +{ + int pkey_shift; + u64 amr; + + if (!pkey) + return true; + + pkey_shift = pkeyshift(pkey); + if (!(read_uamor() & (0x3UL << pkey_shift))) + return true; + + if (execute && !(read_iamr() & (IAMR_EX_BIT << pkey_shift))) + return true; + + if (!write) { + amr = read_amr(); + if (!(amr & (AMR_RD_BIT << pkey_shift))) + return true; + } + + amr = read_amr(); /* delay reading amr uptil absolutely needed */ + return (write && !(amr & (AMR_WR_BIT << pkey_shift))); +} + +bool arch_pte_access_permitted(u64 pte, bool write, bool execute) +{ + if (!pkey_inited) + return true; + return pkey_access_permitted(pte_to_pkey_bits(pte), + write, execute); +}