git: e6928c33f60c - main - arm64: Reduce the kernel executable pages

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Thu, 12 Jun 2025 09:49:36 UTC
The branch main has been updated by andrew:

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

commit e6928c33f60c0a29a5e280840b038dfcbbbbce72
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2025-06-12 09:37:27 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2025-06-12 09:48:57 +0000

    arm64: Reduce the kernel executable pages
    
    When creating the kernel page tables we create them all executable to
    allow the kernel to boot.
    
    We can reduce the number of l2 blocks or l3 pages created as executable
    by looking where the end of the .text section is and only marking
    memory up to this as executable (rounded up to the block/page size).
    
    Memory after the .text is marked as execute never as it will be data.
    
    Reviewed by:    alc
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D45063
---
 sys/arm64/arm64/locore.S | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/sys/arm64/arm64/locore.S b/sys/arm64/arm64/locore.S
index 1268d0d79193..fcc3f948f00c 100644
--- a/sys/arm64/arm64/locore.S
+++ b/sys/arm64/arm64/locore.S
@@ -598,7 +598,14 @@ common:
 #endif
 
 	/* Get the number of blocks/pages to allocate, rounded down */
-	lsr	x10, x8, #(PTE_SHIFT)
+	lsr	x14, x8, #(PTE_SHIFT)
+
+	ldr	x25, =etext
+	ldr	x8, =((1 << PTE_SHIFT) - 1)
+	add	x25, x25, x8
+	mov	x8, #(KERNBASE)
+	sub	x25, x25, x8
+	lsr	x25, x25, #(PTE_SHIFT)
 
 	/* Create the kernel space PTE table */
 	adrp	x6, LL_PAGE_TABLE
@@ -606,6 +613,16 @@ common:
 	mov	x7, #(ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK))
 	mov	x8, #(KERNBASE)
 	mov	x9, x28
+	mov	x10, x25
+	bl	BUILD_PTE_FUNC
+
+	/* Create the kernel space XN PTE table */
+	lsl	x10, x25, #(PTE_SHIFT)
+	ldr	x7, =(ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK) | ATTR_S1_XN)
+	ldr	x8, =(KERNBASE)
+	add	x8, x8, x10
+	add	x9, x28, x10
+	sub	x10, x14, x25
 	bl	BUILD_PTE_FUNC
 
 #undef PTE_SHIFT