git: d3af72040679 - stable/13 - riscv: Use PMAP_MAPDEV_EARLY_SIZE in locore and pmap_bootstrap

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Fri, 09 Jun 2023 19:58:23 UTC
The branch stable/13 has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=d3af72040679a1cd75889a69898e53bb699f4212

commit d3af72040679a1cd75889a69898e53bb699f4212
Author:     Alfredo Mazzinghi <am2419@cam.ac.uk>
AuthorDate: 2023-05-25 15:33:12 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2023-06-09 19:48:06 +0000

    riscv: Use PMAP_MAPDEV_EARLY_SIZE in locore and pmap_bootstrap
    
    Use PMAP_MAPDEV_EARLY_SIZE instead of assuming that its value is always
    L2_SIZE. Add compile-time assertions to check that the size matches the
    expectations in locore.
    
    Reviewed by:    mhorne
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D40110
    
    (cherry picked from commit ef0a711fd5d0d389205c18c869c78b9a4d639ddb)
---
 sys/riscv/include/vmparam.h |  2 +-
 sys/riscv/riscv/genassym.c  |  1 +
 sys/riscv/riscv/locore.S    |  6 ++++--
 sys/riscv/riscv/pmap.c      | 13 +++++++++++--
 4 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/sys/riscv/include/vmparam.h b/sys/riscv/include/vmparam.h
index 6e1c9e11a3cc..5f76b312c7fd 100644
--- a/sys/riscv/include/vmparam.h
+++ b/sys/riscv/include/vmparam.h
@@ -250,7 +250,7 @@ extern vm_offset_t init_pt_va;
 #define	ZERO_REGION_SIZE	(64 * 1024)	/* 64KB */
 
 #define	DEVMAP_MAX_VADDR	VM_MAX_KERNEL_ADDRESS
-#define	PMAP_MAPDEV_EARLY_SIZE	(L2_SIZE * 2)
+#define	PMAP_MAPDEV_EARLY_SIZE	L2_SIZE
 
 /*
  * No non-transparent large page support in the pmap.
diff --git a/sys/riscv/riscv/genassym.c b/sys/riscv/riscv/genassym.c
index ff5e8fa01ddf..22b8cf3b8dbc 100644
--- a/sys/riscv/riscv/genassym.c
+++ b/sys/riscv/riscv/genassym.c
@@ -62,6 +62,7 @@ ASSYM(KERNBASE, KERNBASE);
 ASSYM(VM_MAXUSER_ADDRESS, VM_MAXUSER_ADDRESS);
 ASSYM(VM_MAX_KERNEL_ADDRESS, VM_MAX_KERNEL_ADDRESS);
 ASSYM(VM_EARLY_DTB_ADDRESS, VM_EARLY_DTB_ADDRESS);
+ASSYM(PMAP_MAPDEV_EARLY_SIZE, PMAP_MAPDEV_EARLY_SIZE);
 ASSYM(TDF_ASTPENDING, TDF_ASTPENDING);
 ASSYM(TDF_NEEDRESCHED, TDF_NEEDRESCHED);
 
diff --git a/sys/riscv/riscv/locore.S b/sys/riscv/riscv/locore.S
index bf0ac8e122be..32d1b6198de8 100644
--- a/sys/riscv/riscv/locore.S
+++ b/sys/riscv/riscv/locore.S
@@ -162,7 +162,7 @@ pagetables:
 	lla	s2, pagetable_l2_devmap	/* Link to next level PN */
 	srli	s2, s2, PAGE_SHIFT
 
-	li	a5, (VM_MAX_KERNEL_ADDRESS - L2_SIZE)
+	li	a5, (VM_MAX_KERNEL_ADDRESS - PMAP_MAPDEV_EARLY_SIZE)
 	srli	a5, a5, L1_SHIFT	/* >> L1_SHIFT */
 	andi	a5, a5, Ln_ADDR_MASK	/* & Ln_ADDR_MASK */
 	li	t4, PTE_V
@@ -191,7 +191,9 @@ pagetables:
 
 	/* Store the L2 table entry for the DTB */
 	li	a6, PTE_SIZE
-	li	a5, 510
+	li	a5, VM_EARLY_DTB_ADDRESS
+	srli	a5, a5, L2_SHIFT    /* >> L2_SHIFT */
+	andi	a5, a5, Ln_ADDR_MASK    /* & Ln_ADDR_MASK */
 	mulw	a5, a5, a6
 	add	t1, s1, a5
 	sd	t0, (t1)
diff --git a/sys/riscv/riscv/pmap.c b/sys/riscv/riscv/pmap.c
index df473acef0e7..a6029f5d642d 100644
--- a/sys/riscv/riscv/pmap.c
+++ b/sys/riscv/riscv/pmap.c
@@ -254,6 +254,15 @@ vm_offset_t dmap_max_addr;	/* The virtual address limit of the dmap */
 CTASSERT((DMAP_MIN_ADDRESS  & ~L1_OFFSET) == DMAP_MIN_ADDRESS);
 CTASSERT((DMAP_MAX_ADDRESS  & ~L1_OFFSET) == DMAP_MAX_ADDRESS);
 
+/*
+ * This code assumes that the early DEVMAP is L2_SIZE aligned and is fully
+ * contained within a single L2 entry. The early DTB is mapped immediately
+ * before the devmap L2 entry.
+ */
+CTASSERT((PMAP_MAPDEV_EARLY_SIZE & L2_OFFSET) == 0);
+CTASSERT((VM_EARLY_DTB_ADDRESS & L2_OFFSET) == 0);
+CTASSERT(VM_EARLY_DTB_ADDRESS < (VM_MAX_KERNEL_ADDRESS - PMAP_MAPDEV_EARLY_SIZE));
+
 static struct rwlock_padalign pvh_global_lock;
 static struct mtx_padalign allpmaps_lock;
 
@@ -684,7 +693,7 @@ pmap_bootstrap(vm_offset_t l1pt, vm_paddr_t kernstart, vm_size_t kernlen)
 
 	/* Create the l3 tables for the early devmap */
 	freemempos = pmap_bootstrap_l3(l1pt,
-	    VM_MAX_KERNEL_ADDRESS - L2_SIZE, freemempos);
+	    VM_MAX_KERNEL_ADDRESS - PMAP_MAPDEV_EARLY_SIZE, freemempos);
 
 	/*
 	 * Invalidate the mapping we created for the DTB. At this point a copy
@@ -738,7 +747,7 @@ pmap_bootstrap(vm_offset_t l1pt, vm_paddr_t kernstart, vm_size_t kernlen)
 	msgbufp = (void *)msgbufpv;
 
 	virtual_avail = roundup2(freemempos, L2_SIZE);
-	virtual_end = VM_MAX_KERNEL_ADDRESS - L2_SIZE;
+	virtual_end = VM_MAX_KERNEL_ADDRESS - PMAP_MAPDEV_EARLY_SIZE;
 	kernel_vm_end = virtual_avail;
 
 	pa = pmap_early_vtophys(l1pt, freemempos);