Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp2642303ybh; Mon, 9 Mar 2020 09:58:18 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsO9anT5IMRA3o61m5faz4u///RoXrxgSF7/bWQv5H8N/Yo7pT5CKg1iCp4tthmfeLCdYjk X-Received: by 2002:a05:6830:15c6:: with SMTP id j6mr12885638otr.348.1583773097963; Mon, 09 Mar 2020 09:58:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583773097; cv=none; d=google.com; s=arc-20160816; b=VxO+4745pE12h+sVL3ciZIvg68wK6mOnx+sm/J85GPQo+XNVmBLGUICtwzKQ9urs04 BPY15jJxx9Q4ZRM1QpAOHWi5We14kDty2S/OO7OPGGHIIoVTvRiomnzRi6Q0GQBZpEYl szKk03m0U895/gDU2lSOxc0VwqMdK8nTIbNK9Kj2jlKReUcOD5cvTtjL8IIzbxaqil12 9bWU2gToYbsSaddA5lmUVeWDlna0/nfrpWNgA5DFCjEDmvju1XezkwkrYKPgR0N7qTgn EoO1vktfgFDF45lQVC5VvXPWki7v3DZ8Y1uybybBdkoEq/irXXkQMMXMDANOjJyiQrsJ nr9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=t+sOri2FxRcWtNEoCzFhDA9PpUDCsy8ZKbuRwF8DnYo=; b=tiyHzUXryeGS2ClAwbSEEVBLkBPOeAbnWU0zOz0fcA1X5qD7uxNrwFJerG3PWGXoyn fVu0a6ojWfgKusU5vnxBMtpVptgW9zK6U7rJWIwlhKmDrWd4K4/cqEPXYls4z9D4QahD jXfLI/j2YjTryH3efpckjWIXSgz8ONViUGyz6V/zzeFPutvQ30eLbAOqHEzuQKFG/+cu K1tZ6+SheC7YDrmkfXMRi80eDarJjwKeXhPUMvc62bzlrNx0E+1/WKyofCrp7Z1hZkDF QVlz1Y6Pvifa1svbxvLBkGVINP01eMLM5znNgpN8tjsIU0G/g/ilJ8iu6p3Y+3j2WLCb 9uIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b="GByup/CP"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z107si3355176otb.292.2020.03.09.09.58.06; Mon, 09 Mar 2020 09:58:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b="GByup/CP"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727270AbgCIQz4 (ORCPT + 99 others); Mon, 9 Mar 2020 12:55:56 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:36905 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726922AbgCIQzz (ORCPT ); Mon, 9 Mar 2020 12:55:55 -0400 Received: by mail-pl1-f196.google.com with SMTP id f16so2072729plj.4 for ; Mon, 09 Mar 2020 09:55:53 -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 :mime-version:content-transfer-encoding; bh=t+sOri2FxRcWtNEoCzFhDA9PpUDCsy8ZKbuRwF8DnYo=; b=GByup/CPxw6+uWC5i9i57G+tx1/0qlQ9mxB7ym/z04BJBTDe2BuGL0UaFnTuXDE0+y DxroElj9BenJf8LoT5q+Y45ceFnWVsFVkdeC4vOci7KdXsbvl+aFZpyQ/P5AnoUEw+Tz 0XcdDnt5IQ606O/Vmj7QBnpSZW68EfS9DXw6OoOnuUrp3aEqlZybVs83SkN8NuyrN6Av jNfjOx0d6BjtM//WlXKEBUwRGnwwpkW8Q1OLzxZaXpaPn1CqJb2USdY9XdB8H9V5dMee TFWDLtuTpGEvIG3ub6zYV7elzrrhfEDFcRKN+rdtzZNi3LcLRIrCGu7sXqbdGpgKlj3d g+Tw== 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:mime-version:content-transfer-encoding; bh=t+sOri2FxRcWtNEoCzFhDA9PpUDCsy8ZKbuRwF8DnYo=; b=QicTaHUefuCxnl9bVqSsQs6sJhSoTFmQTgbwetSvmPA8hUAYuwSWKr199whFjD+n9D FevjEQtTWCFO26IUXdBUT09T80/nZX2crONeh/SylF3yyJ6HyXvAc9g4jwQgwOX2hFO+ paNu36/Snt6lWmAjrDEgrr/CfIikRafs1a2KjZhyiEyqIKk5QA054VpZFMXHhxXZJSz+ d2j+32RV8oiAWr0vUPuS3nVXcH8oI4LnWGow0KPO5ybXP4PkfuS/4kQFGq3cc/N/06sI U31XWap7jdL1TvARJrT14jddSgxX6AKQasg+qgLYiyJR1erdomFm1lyP8Gj5eNotcPy8 LvyQ== X-Gm-Message-State: ANhLgQ3QpNJIyv2Q5Ij0kiNOCKChPtc/aqYamb3TAiB930EH8wHBtnVi hBWk7rGs9tnmzGINwuZXkrEBeg== X-Received: by 2002:a17:902:fe15:: with SMTP id g21mr17419647plj.215.1583772953089; Mon, 09 Mar 2020 09:55:53 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (220-132-236-182.HINET-IP.hinet.net. [220.132.236.182]) by smtp.gmail.com with ESMTPSA id cm2sm104013pjb.23.2020.03.09.09.55.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Mar 2020 09:55:52 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH v3 1/9] riscv: add ARCH_HAS_SET_MEMORY support Date: Tue, 10 Mar 2020 00:55:36 +0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add set_memory_ro/rw/x/nx architecture hooks to change the page attribution. Use own set_memory.h rather than generic set_memory.h (i.e. include/asm-generic/set_memory.h), because we want to add other function prototypes here. Signed-off-by: Zong Li --- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/set_memory.h | 24 +++++ arch/riscv/mm/Makefile | 2 +- arch/riscv/mm/pageattr.c | 150 ++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/set_memory.h create mode 100644 arch/riscv/mm/pageattr.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index af9b03609c5a..9ab592aa1b5f 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -60,6 +60,7 @@ config RISCV select HAVE_EBPF_JIT if 64BIT select EDAC_SUPPORT select ARCH_HAS_GIGANTIC_PAGE + select ARCH_HAS_SET_MEMORY select ARCH_WANT_HUGE_PMD_SHARE if 64BIT select SPARSEMEM_STATIC if 32BIT select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU diff --git a/arch/riscv/include/asm/set_memory.h b/arch/riscv/include/asm/set_memory.h new file mode 100644 index 000000000000..79a810f0f38b --- /dev/null +++ b/arch/riscv/include/asm/set_memory.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2019 SiFive + */ + +#ifndef _ASM_RISCV_SET_MEMORY_H +#define _ASM_RISCV_SET_MEMORY_H + +/* + * Functions to change memory attributes. + */ +#ifdef CONFIG_MMU +int set_memory_ro(unsigned long addr, int numpages); +int set_memory_rw(unsigned long addr, int numpages); +int set_memory_x(unsigned long addr, int numpages); +int set_memory_nx(unsigned long addr, int numpages); +#else +static inline int set_memory_ro(unsigned long addr, int numpages) { return 0; } +static inline int set_memory_rw(unsigned long addr, int numpages) { return 0; } +static inline int set_memory_x(unsigned long addr, int numpages) { return 0; } +static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; } +#endif + +#endif /* _ASM_RISCV_SET_MEMORY_H */ diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile index 814e16a8d68a..363ef01c30b1 100644 --- a/arch/riscv/mm/Makefile +++ b/arch/riscv/mm/Makefile @@ -7,7 +7,7 @@ endif obj-y += init.o obj-y += extable.o -obj-$(CONFIG_MMU) += fault.o +obj-$(CONFIG_MMU) += fault.o pageattr.o obj-y += cacheflush.o obj-y += context.o diff --git a/arch/riscv/mm/pageattr.c b/arch/riscv/mm/pageattr.c new file mode 100644 index 000000000000..fcd59ef2835b --- /dev/null +++ b/arch/riscv/mm/pageattr.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2019 SiFive + */ + +#include +#include +#include +#include + +struct pageattr_masks { + pgprot_t set_mask; + pgprot_t clear_mask; +}; + +static unsigned long set_pageattr_masks(unsigned long val, struct mm_walk *walk) +{ + struct pageattr_masks *masks = walk->private; + unsigned long new_val = val; + + new_val &= ~(pgprot_val(masks->clear_mask)); + new_val |= (pgprot_val(masks->set_mask)); + + return new_val; +} + +static int pageattr_pgd_entry(pgd_t *pgd, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pgd_t val = READ_ONCE(*pgd); + + if (pgd_leaf(val)) { + val = __pgd(set_pageattr_masks(pgd_val(val), walk)); + set_pgd(pgd, val); + } + + return 0; +} + +static int pageattr_p4d_entry(p4d_t *p4d, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + p4d_t val = READ_ONCE(*p4d); + + if (p4d_leaf(val)) { + val = __p4d(set_pageattr_masks(p4d_val(val), walk)); + set_p4d(p4d, val); + } + + return 0; +} + +static int pageattr_pud_entry(pud_t *pud, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pud_t val = READ_ONCE(*pud); + + if (pud_leaf(val)) { + val = __pud(set_pageattr_masks(pud_val(val), walk)); + set_pud(pud, val); + } + + return 0; +} + +static int pageattr_pmd_entry(pmd_t *pmd, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pmd_t val = READ_ONCE(*pmd); + + if (pmd_leaf(val)) { + val = __pmd(set_pageattr_masks(pmd_val(val), walk)); + set_pmd(pmd, val); + } + + return 0; +} + +static int pageattr_pte_entry(pte_t *pte, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pte_t val = READ_ONCE(*pte); + + val = __pte(set_pageattr_masks(pte_val(val), walk)); + set_pte(pte, val); + + return 0; +} + +static int pageattr_pte_hole(unsigned long addr, unsigned long next, + int depth, struct mm_walk *walk) +{ + /* Nothing to do here */ + return 0; +} + +const static struct mm_walk_ops pageattr_ops = { + .pgd_entry = pageattr_pgd_entry, + .p4d_entry = pageattr_p4d_entry, + .pud_entry = pageattr_pud_entry, + .pmd_entry = pageattr_pmd_entry, + .pte_entry = pageattr_pte_entry, + .pte_hole = pageattr_pte_hole, +}; + +static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask, + pgprot_t clear_mask) +{ + int ret; + unsigned long start = addr; + unsigned long end = start + PAGE_SIZE * numpages; + struct pageattr_masks masks = { + .set_mask = set_mask, + .clear_mask = clear_mask + }; + + if (!numpages) + return 0; + + down_read(&init_mm.mmap_sem); + ret = walk_page_range_novma(&init_mm, start, end, &pageattr_ops, NULL, + &masks); + up_read(&init_mm.mmap_sem); + + flush_tlb_kernel_range(start, end); + + return ret; +} + +int set_memory_ro(unsigned long addr, int numpages) +{ + return __set_memory(addr, numpages, __pgprot(_PAGE_READ), + __pgprot(_PAGE_WRITE)); +} + +int set_memory_rw(unsigned long addr, int numpages) +{ + return __set_memory(addr, numpages, __pgprot(_PAGE_READ | _PAGE_WRITE), + __pgprot(0)); +} + +int set_memory_x(unsigned long addr, int numpages) +{ + return __set_memory(addr, numpages, __pgprot(_PAGE_EXEC), __pgprot(0)); +} + +int set_memory_nx(unsigned long addr, int numpages) +{ + return __set_memory(addr, numpages, __pgprot(0), __pgprot(_PAGE_EXEC)); +} -- 2.25.1