All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zong Li <zong.li@sifive.com>
To: palmer@dabbelt.com, paul.walmsley@sifive.com, alex@ghiti.fr,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Cc: Zong Li <zong.li@sifive.com>
Subject: [PATCH RFC 2/8] riscv/kaslr: introduce functions to clear page table
Date: Tue, 24 Mar 2020 15:30:47 +0800	[thread overview]
Message-ID: <286a940ef8fbc4480c63679271eea440d167a258.1584352425.git.zong.li@sifive.com> (raw)
In-Reply-To: <cover.1584352425.git.zong.li@sifive.com>

In KASLR, we need to re-create page table after getting a random
destination. Introduce clear function to clear old content. Also, the
page table entries allow writing value when it's empty, so we have to
clear the early page table.

This patch is a preparation to support KASLR.

Signed-off-by: Zong Li <zong.li@sifive.com>
---
 arch/riscv/mm/init.c | 54 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index ace5d74fd939..51e263c04fa2 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -315,6 +315,7 @@ static void __init create_pmd_mapping(pmd_t *pmdp,
 #define get_pgd_next_virt(__pa)	get_pmd_virt(__pa)
 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot)	\
 	create_pmd_mapping(__nextp, __va, __pa, __sz, __prot)
+#define clear_pgd_next_mapping(__nextp)	clear_pmd(__nextp)
 #define fixmap_pgd_next		fixmap_pmd
 #else
 #define pgd_next_t		pte_t
@@ -322,6 +323,7 @@ static void __init create_pmd_mapping(pmd_t *pmdp,
 #define get_pgd_next_virt(__pa)	get_pte_virt(__pa)
 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot)	\
 	create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
+#define clear_pgd_next_mapping(__nextp)	clear_pte(__nextp)
 #define fixmap_pgd_next		fixmap_pte
 #endif
 
@@ -361,6 +363,58 @@ static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
 	return PMD_SIZE;
 }
 
+#ifdef CONFIG_RANDOMIZE_BASE
+static void __init clear_pte(pte_t *ptep)
+{
+	unsigned int i;
+
+	for (i = 0; i < PTRS_PER_PTE; i++)
+		if (!pte_none(ptep[i]))
+			ptep[i] = __pte(0);
+}
+
+static void __init clear_pmd(pmd_t *pmdp)
+{
+	unsigned int i;
+	pte_t *ptep;
+	phys_addr_t pte_phys;
+	uintptr_t kaslr_offset = get_kaslr_offset();
+
+	for (i = 0; i < PTRS_PER_PMD; i++)
+		if (!pmd_none(pmdp[i])) {
+			if (pmd_leaf(pmdp[i])) {
+				pmd_clear(&pmdp[i]);
+			} else {
+				pte_phys = PFN_PHYS(_pmd_pfn(pmdp[i]));
+				ptep = get_pte_virt(pte_phys + kaslr_offset);
+				clear_pte(ptep);
+				pmd_clear(&pmdp[i]);
+			}
+		}
+}
+
+static void __init clear_pgd(pgd_t *pgdp)
+{
+	unsigned int i;
+	pgd_next_t *nextp;
+	phys_addr_t next_phys;
+	uintptr_t kaslr_offset = get_kaslr_offset();
+
+	for (i = 0; i < PTRS_PER_PGD; i++)
+		if (pgd_val(pgdp[i]) != 0) {
+			if (pgd_leaf(pgd_val(pgdp[i]))) {
+				set_pgd(&pgdp[i], __pgd(0));
+			} else {
+				next_phys = PFN_PHYS(_pgd_pfn(pgdp[i]));
+				nextp = get_pgd_next_virt(next_phys +
+							  kaslr_offset);
+				clear_pgd_next_mapping(nextp);
+				set_pgd(&pgdp[i], __pgd(0));
+			}
+		}
+}
+#endif
+
 /*
  * setup_vm() is called from head.S with MMU-off.
  *
-- 
2.25.1


  parent reply	other threads:[~2020-03-24  7:31 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-24  7:30 [PATCH RFC 0/8] Support KASLR for RISC-V Zong Li
2020-03-24  7:30 ` [PATCH RFC 1/8] riscv/kaslr: add interface to get kaslr offset Zong Li
2020-04-07  5:08   ` Alex Ghiti
2020-03-24  7:30 ` Zong Li [this message]
2020-04-07  5:09   ` [PATCH RFC 2/8] riscv/kaslr: introduce functions to clear page table Alex Ghiti
2020-04-07  9:17     ` Zong Li
2020-04-07  9:17       ` Zong Li
2020-03-24  7:30 ` [PATCH RFC 3/8] riscv/kaslr: support KASLR infrastructure Zong Li
2020-04-07  5:10   ` Alex Ghiti
2020-04-07 10:34     ` Zong Li
2020-04-07 10:34       ` Zong Li
2020-04-09  5:53       ` Alex Ghiti
2020-04-09  5:53         ` Alex Ghiti
2020-04-09 11:08         ` Zong Li
2020-04-09 11:08           ` Zong Li
2020-03-24  7:30 ` [PATCH RFC 4/8] riscv/kaslr: randomize the kernel image offset Zong Li
2020-04-07  5:11   ` Alex Ghiti
2020-04-07 10:53     ` Zong Li
2020-04-07 10:53       ` Zong Li
2020-04-09  5:51       ` Alex Ghiti
2020-04-09  5:51         ` Alex Ghiti
2020-04-09 10:31         ` Zong Li
2020-04-09 10:31           ` Zong Li
2020-04-10 15:58           ` Alex Ghiti
2020-04-10 15:58             ` Alex Ghiti
2020-04-11  8:20             ` Zong Li
2020-04-11  8:20               ` Zong Li
2020-04-12  6:53               ` Alex Ghiti
2020-04-12  6:53                 ` Alex Ghiti
2020-04-14  2:46                 ` Zong Li
2020-04-14  2:46                   ` Zong Li
2020-04-14  5:43                   ` Alex Ghiti
2020-04-14  5:43                     ` Alex Ghiti
2020-04-14  7:00                     ` Zong Li
2020-04-14  7:00                       ` Zong Li
2020-03-24  7:30 ` [PATCH RFC 5/8] riscv/kaslr: support sparse memory model Zong Li
2020-03-24  7:30 ` [PATCH RFC 6/8] riscv/kaslr: clear the original kernel image Zong Li
2020-04-07  5:11   ` Alex Ghiti
2020-04-07 11:18     ` Zong Li
2020-04-07 11:18       ` Zong Li
2020-04-09  5:53       ` Alex Ghiti
2020-04-09  5:53         ` Alex Ghiti
2020-04-09  8:14         ` Alex Ghiti
2020-04-09  8:14           ` Alex Ghiti
2020-03-24  7:30 ` [PATCH RFC 7/8] riscv/kaslr: add cmdline support to disable KASLR Zong Li
2020-03-24  7:30 ` [PATCH RFC 8/8] riscv/kaslr: dump out kernel offset information on panic Zong Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=286a940ef8fbc4480c63679271eea440d167a258.1584352425.git.zong.li@sifive.com \
    --to=zong.li@sifive.com \
    --cc=alex@ghiti.fr \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.