The branch main has been updated by mhorne:

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

commit ef0a711fd5d0d389205c18c869c78b9a4d639ddb
Author:     Alfredo Mazzinghi <[email protected]>
AuthorDate: 2023-05-25 15:33:12 +0000
Commit:     Mitchell Horne <[email protected]>
CommitDate: 2023-05-25 17:06:16 +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
---
 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 e45b7c288676..ce2c2e3964cf 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(PCB_ONFAULT, offsetof(struct pcb, pcb_onfault));
 ASSYM(PCB_SIZE, sizeof(struct pcb));
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 007ec1e678fc..5d35488c415c 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);

Reply via email to