Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp2223434ybh; Mon, 9 Mar 2020 01:23:15 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtAt3aZvQHZJYPgS5VofF8zLpzJ/kBEMniq0KFKy3DRo1V2fdNYIeyrQ4ccHbg2zVyUtBfF X-Received: by 2002:a9d:3e89:: with SMTP id b9mr12323024otc.3.1583742195325; Mon, 09 Mar 2020 01:23:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583742195; cv=none; d=google.com; s=arc-20160816; b=j3kNky5OimeKYoCMKfElcNlaEfj04efTm8BUV9JsvoNKJKpyxSRE5VC5ckIbvAJL/7 p/J6WpB4DFiuV3rR5udNAoupQo1MyfkX1+P/qcSqyJTipYDHh7ogKlGG/cR+foI8bOBF GKMokoerfNe2ItG2GbFaspHc4QmpaY5JJ6gB5SAQyIJqQRYm3loDc5s4O3wqKk4Oc36O goQBa4Q+j3amwmM1HJy+Nqv9CqHx3awXHuraiKKPnWhi7Py99AkMRP+ECfXBAVCT6PNu m+RqOCvuQCXqOucuc3P/wl2vQMZmybLShWOCgg/JGMG0p2WjwmY+pjI90W/3+FP/MtVq Nwag== 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=M9JWCYyM6e/yrlSY6Kqbp3PYw6HxEa4SPxOUoFQCyjA=; b=n+Yp1IBu37/Iy9b+Hd3kPFBreTgCgdydgKjk1yvI5OdFRVg1sXMHnONZ5OchMsrC7e swecXIhCA75Y80yiUcaXU4nsnQ/Od8ycv8E4fTzJP7p4ZiPr0/XUI6MxwNKd9IcSO3KZ CXdXqUKWoYbFqa5LFinkoxztIvBorhOlfOg2aHi8TuHXBYvr6OeRYbF+z/rY0Xv6B3Wh fTWFi952lz7wTJkXEExmTqYaM45FZjUOemAoYgjnkohIT/KieBh2jWPd0paIxWJsQY0z fZfSuI70ZDCaGBk5jrhJZcdbZUafQ2wZ63DcLHlCeH4W+oUTIQEcWNwCh+nGA4d125Kz QaAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=blx9MR5K; 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 k11si5008520otb.252.2020.03.09.01.23.03; Mon, 09 Mar 2020 01:23:15 -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=blx9MR5K; 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 S1726466AbgCIIWn (ORCPT + 99 others); Mon, 9 Mar 2020 04:22:43 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:35934 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725796AbgCIIWl (ORCPT ); Mon, 9 Mar 2020 04:22:41 -0400 Received: by mail-pg1-f195.google.com with SMTP id d9so4378456pgu.3 for ; Mon, 09 Mar 2020 01:22:40 -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=M9JWCYyM6e/yrlSY6Kqbp3PYw6HxEa4SPxOUoFQCyjA=; b=blx9MR5KFVCUxLjT9llTQCauTb6eUfWvGxqJ0UeCKDbGCa/udE9KMHfYSpdnfLVagE Jg7HyNT6N6pkit4ryUrhBupCYRU+qvsC8wRx1UFkD86bO8OdCFYapACvYJbfXjxDYW/j HgK6vUDwdEjeTldm8r0hzdHnsRWZKM4i2yf+UOpkXTZKTHWX0PjEb84pKdUHlz7cO0HV 1CaXZrZq5WRjb2jgD8Qk3h4AoDKmHwEpLxim8pcAbp0o9A8P2hoB5NgfXLa5SGRZOvYx nRT7AKxKxF20EdBqy9rN+9LoNRXTkbUH5BVG2arkIokQWsrPJpNEpepxbOeZ6Vq6BugA Cf/A== 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=M9JWCYyM6e/yrlSY6Kqbp3PYw6HxEa4SPxOUoFQCyjA=; b=mH1897pR6PB2Zek7skuxNGr7ZX77+TSVTjJ+Rrul/UxvFheyYQGnq12EAkRBgH0BqL tGAEzupsCMMh6plP4P0dMM/h/rtkEAlTGb0SJzDvOdm7NQklHlJWdWHznek19MtZGEyy Sew/EiMdj7TCDLG3IReHs2g0p3NnLMRIMxn0IclJdKbtlY67y6R/AUOlMpBavhlAQllM /pYbPixIeP2/TWO10ZXdjOFqg7plUK5XYBKEkug8tsMr1qfcoyOPDapxL+IbBJtMg34f Pfn9bJePkBxGaH4dAkoYG4/+ZnSVh8PjBW3SyBIF4z8472RFfxNyjTpXIfN/Xxbt/L6m Tacw== X-Gm-Message-State: ANhLgQ0C4uLWmf3ZMyyGy3QnbgxbdhfcsazEZqVaU2nn4IIyl+0Yuvhq K3DvhmwL1CzJw40mUhZ79Jal8w== X-Received: by 2002:a62:8144:: with SMTP id t65mr7656857pfd.188.1583742160050; Mon, 09 Mar 2020 01:22:40 -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 v5sm18364779pfn.64.2020.03.09.01.22.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Mar 2020 01:22:39 -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 v2 1/9] riscv: add ARCH_HAS_SET_MEMORY support Date: Mon, 9 Mar 2020 16:22:21 +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 | 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.1