Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp4522598ybv; Mon, 17 Feb 2020 00:32:55 -0800 (PST) X-Google-Smtp-Source: APXvYqyItxt7JJ0aOYtjs16k6KONlMioB8jftllY3hqmUu+dpir8J/Je2cjDXvmXCD0icPMx/IVx X-Received: by 2002:a9d:67c9:: with SMTP id c9mr11928054otn.144.1581928375099; Mon, 17 Feb 2020 00:32:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581928375; cv=none; d=google.com; s=arc-20160816; b=enx87zuqV2Kjj0qy4SSyYLzXBHqH5VPaqSTFVMv6VXkfIA65e+4fgvqTP+7XI5x2+N HwI8WqTYBlewodzJ7ZvYeSRlpGjqcoNz/vuhgkuJsneDmdUt5BqmKnKcTWCxzpTNt+61 KD4toEcDvBcgMU5SbcG5mxBul9NQyEVr44UBW1CFQStRic4Jke1t78DR571kxstnQIAa OM/K9C72/HrUeNl6HUjA2LWzYuzzN4myifCpSzhpdgZT8C272kXuSyTj+E35kJnM5Oaq AdstsmBAYANm45kFNpSFR3lCqEZgiYaKqfRFJdaXvJDaajQ0vr1tbWfw/h3rebm4IjN7 ydvg== 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=PEjz/RcQS+U/YywRSqm0sa6mFOHzzBZkMLYn0qcUgSA=; b=bHyk47fbi0Q/Nhvbj1qDt7MyfLsZPR2FQSmJFR7SMhy0iIKCj1N/CvKJBaj6Or6b8z dpLYcoogXx6g3f2vIgydnoLp2IJ7A+tlWuMz/qM9NW/KlNd2FTuDiIsiIrcLBRl7M3iG 5vm+yU29TMmPEA0WB4pNA9ec9JNqisZa4vJJzzHh9YyNbnhROQxlp/HwT2rFIbM4NB9g Vll1ePKWYn34ZMHAAeaLZ7YPKSnFe0XgWNWMSQxFPCf8PbI+W5MmbQL3bbuGVBZnsR1s JNEwVETn3FSnYKWuSdcMqxS1Dm66MWZZcjqWZ18Uh6C+g0JRaKaJso72RWydeF5HDtpZ ePig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=VJuw6P+7; 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 k128si6409505oih.274.2020.02.17.00.32.43; Mon, 17 Feb 2020 00:32:55 -0800 (PST) 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=VJuw6P+7; 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 S1728305AbgBQIcd (ORCPT + 99 others); Mon, 17 Feb 2020 03:32:33 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:42498 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726397AbgBQIcb (ORCPT ); Mon, 17 Feb 2020 03:32:31 -0500 Received: by mail-pf1-f193.google.com with SMTP id 4so8432787pfz.9 for ; Mon, 17 Feb 2020 00:32:31 -0800 (PST) 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=PEjz/RcQS+U/YywRSqm0sa6mFOHzzBZkMLYn0qcUgSA=; b=VJuw6P+7+i90JIaL0Udtqg6ehfR8sKg7rXYXaRE6x8BGSosYjV8zjme9062MmtGWW0 zUM3xjCDwZG4OvoTOnsX/Cmu5oDcd69h7VMSLr5QhL5+DYTHgDSYaK4h+27T8JbD3ScN dFQl6QJgkbl99r14MdaiQlxpYAznhGA9ctWUi6rKuC0T/1s7SZ2I19AmmDcjpn4szt1D R7rzbQh3rbYsNOkOd/0R6/sdj0fEiEQuOnCZmWQIT9EnG5EFC5Tlc0zM0KpQJcmFgmqQ uAT4OgjzlB3fFJRgdxX1oIH2cT/kOpz8XNOSiCWW/gzFSYrS3gXUm/70UlFp2O9WRIcO TxfQ== 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=PEjz/RcQS+U/YywRSqm0sa6mFOHzzBZkMLYn0qcUgSA=; b=FxFWMcni4DwNl/EEcUHIS2WMVcXgeqIRfdRiffsy/HZrBGg4V2bkqw9wznecTzr5w4 Q6zM0FPoi+NO6DykyJST6aP5+HjJCfIHKmUwk0yjyWwc+7wtDcCp43B24asg/0ZaedIB ho1vtEM6QbfZRnzRTp5odEgYtqTNNldKzYGIWY1XZL0nBSDFZl0WS6+y1WnOvwKe20vV 97MVjA1jDYnPFDWhCS+eoWY6x6cJzDaNxZcxWBz+WkZeWGhul4FLnhrqi+XLLSV5wpJt h4CvL/SAmDK+UNVlT9hOvNo7XJmolb53bCiDWVapuFhzL7FGvUDGOQg8Fx7Whknnp9fy C6sA== X-Gm-Message-State: APjAAAWr/1k8/4lsy3M4qcgcV4+V4syAbcgJJI/k8KW2D5TvFBvKuJ6r 0oXDzhmC7aCRsvEubP0Bi01HnA== X-Received: by 2002:a63:5848:: with SMTP id i8mr16271948pgm.438.1581928350891; Mon, 17 Feb 2020 00:32:30 -0800 (PST) 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 z10sm16989319pgz.88.2020.02.17.00.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2020 00:32:30 -0800 (PST) From: Zong Li To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH 1/8] riscv: add ARCH_HAS_SET_MEMORY support Date: Mon, 17 Feb 2020 16:32:16 +0800 Message-Id: <20200217083223.2011-2-zong.li@sifive.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200217083223.2011-1-zong.li@sifive.com> References: <20200217083223.2011-1-zong.li@sifive.com> 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 | 17 ++++ arch/riscv/mm/Makefile | 1 + arch/riscv/mm/pageattr.c | 150 ++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+) 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 6e81da55b5e4..76ed36543b3a 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..936f08063566 --- /dev/null +++ b/arch/riscv/include/asm/set_memory.h @@ -0,0 +1,17 @@ +/* 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. + */ +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); + +#endif /* _ASM_RISCV_SET_MEMORY_H */ diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile index 814e16a8d68a..b94643aee84c 100644 --- a/arch/riscv/mm/Makefile +++ b/arch/riscv/mm/Makefile @@ -10,6 +10,7 @@ obj-y += extable.o obj-$(CONFIG_MMU) += fault.o obj-y += cacheflush.o obj-y += context.o +obj-y += pageattr.o ifeq ($(CONFIG_MMU),y) obj-$(CONFIG_SMP) += tlbflush.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.0