git: 938e4b131c48 - main - arm64: Use the PAN msr mnemonic rather than .inst

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Thu, 12 Dec 2024 18:19:00 UTC
The branch main has been updated by andrew:

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

commit 938e4b131c483a05d5e5ea8f2bb44dfd1514868b
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2024-11-27 16:17:07 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2024-12-12 18:00:22 +0000

    arm64: Use the PAN msr mnemonic rather than .inst
    
    Switch from creating the msr instructions to manage PAN to use the
    "msr pan, #1" instruction directly. When this was added clang didn't
    have support to assemble the instructions. This appears to have been
    added to clang 13 which is sufficiently old enough.
    
    Binutils releases from around the same time appear to have added this
    instruction so any modern gcc should also support this instruction.
    
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D47817
---
 sys/arm64/arm64/machdep.c | 10 ++++------
 sys/arm64/include/asm.h   | 12 +++++++++---
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/sys/arm64/arm64/machdep.c b/sys/arm64/arm64/machdep.c
index b61fec3fdd11..03f041851c8f 100644
--- a/sys/arm64/arm64/machdep.c
+++ b/sys/arm64/arm64/machdep.c
@@ -187,11 +187,6 @@ pan_enable(void)
 {
 
 	/*
-	 * The LLVM integrated assembler doesn't understand the PAN
-	 * PSTATE field. Because of this we need to manually create
-	 * the instruction in an asm block. This is equivalent to:
-	 * msr pan, #1
-	 *
 	 * This sets the PAN bit, stopping the kernel from accessing
 	 * memory when userspace can also access it unless the kernel
 	 * uses the userspace load/store instructions.
@@ -199,7 +194,10 @@ pan_enable(void)
 	if (has_pan) {
 		WRITE_SPECIALREG(sctlr_el1,
 		    READ_SPECIALREG(sctlr_el1) & ~SCTLR_SPAN);
-		__asm __volatile(".inst 0xd500409f | (0x1 << 8)");
+		__asm __volatile(
+		    ".arch_extension pan	\n"
+		    "msr pan, #1		\n"
+		    ".arch_extension nopan	\n");
 	}
 }
 
diff --git a/sys/arm64/include/asm.h b/sys/arm64/include/asm.h
index 16be39b3eae4..cc0a7d8293c9 100644
--- a/sys/arm64/include/asm.h
+++ b/sys/arm64/include/asm.h
@@ -87,19 +87,25 @@
 	ldr	tmp, =has_pan;			/* Get the addr of has_pan */ \
 	ldr	reg, [tmp];			/* Read it */		\
 	cbz	reg, 997f;			/* If no PAN skip */	\
-	.inst	0xd500409f | (0 << 8);		/* Clear PAN */		\
+	.arch_extension pan;						\
+	msr pan, #0;				/* Disable PAN checks */ \
+	.arch_extension nopan;						\
 	997:
 
 #define	EXIT_USER_ACCESS(reg)						\
 	cbz	reg, 998f;			/* If no PAN skip */	\
-	.inst	0xd500409f | (1 << 8);		/* Set PAN */		\
+	.arch_extension pan;						\
+	msr pan, #1;				/* Enable PAN checks */ \
+	.arch_extension nopan;						\
 	998:
 
 #define	EXIT_USER_ACCESS_CHECK(reg, tmp)				\
 	ldr	tmp, =has_pan;			/* Get the addr of has_pan */ \
 	ldr	reg, [tmp];			/* Read it */		\
 	cbz	reg, 999f;			/* If no PAN skip */	\
-	.inst	0xd500409f | (1 << 8);		/* Set PAN */		\
+	.arch_extension pan;						\
+	msr pan, #1;				/* Enable PAN checks */ \
+	.arch_extension nopan;						\
 	999:
 
 /*