Skip to contentSkip to conversation

arm64: BTI cleanups

PATCH Superseded Mark Brown 2 participants 5 messages Dec 7, 2021 – Dec 8, 2021

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Mark Brown

author
#0 lore raw

v2 cover arm64: BTI cleanups

This patch series brings together a few cleanups for the assembly portions of the in kernel BTI support.

v2:
- Pull in the assembler macro for BTI proposed by Mark Rutland.
- Unconditionally override the SYM_FUNC macros.

Mark Brown (2):
arm64: Add macro version of the BTI instruction
arm64: Unconditionally override SYM_FUNC macros

 arch/arm64/crypto/aes-modes.S      | 10 +++++-----
 arch/arm64/include/asm/assembler.h | 18 ++++++++++++++++++
 arch/arm64/include/asm/linkage.h   | 23 ++++++++++++-----------
 arch/arm64/kernel/entry-ftrace.S   |  4 ----
 arch/arm64/lib/kasan_sw_tags.S     |  2 --
 5 files changed, 35 insertions(+), 22 deletions(-)

base-commit: 0fcfb00b28c0b7884635dacf38e46d60bf3d4eb1

Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
author
#1 lore raw

PATCH v2 1/2 arm64: Add macro version of the BTI instruction

BTI is only available from v8.5 so we need to encode it using HINT in generic code and for older toolchains. Add an assembler macro written by Mark Rutland which lets us use the mnemonic and update the existing users.

---
 arch/arm64/crypto/aes-modes.S      | 10 +++++-----
 arch/arm64/include/asm/assembler.h | 18 ++++++++++++++++++
 arch/arm64/include/asm/linkage.h   |  7 +------
 3 files changed, 24 insertions(+), 11 deletions(-)
arch/arm64/crypto/aes-modes.S +5 -5
index b495de22bb38..ff01f0167ba2 100644
@@ -363,15 +363,15 @@ ST5(	mov		v4.16b, vctr.16b		)
 	adr		x16, 1f
 	sub		x16, x16, x12, lsl #3
 	br		x16
-	hint		34			// bti c
+	bti		c
 	mov		v0.d[0], vctr.d[0]
-	hint		34			// bti c
+	bti		c
 	mov		v1.d[0], vctr.d[0]
-	hint		34			// bti c
+	bti		c
 	mov		v2.d[0], vctr.d[0]
-	hint		34			// bti c
+	bti		c
 	mov		v3.d[0], vctr.d[0]
-ST5(	hint		34				)
+ST5(	bti		c				)
 ST5(	mov		v4.d[0], vctr.d[0]		)
 1:	b		2f
 	.previous
arch/arm64/include/asm/assembler.h +18 -0
index 136d13f3d6e9..87f296198366 100644
@@ -790,6 +790,24 @@ alternative_endif
 .Lnoyield_\@:
 	.endm
 
+/*
+ * Branch Target Identifier (BTI)
+ */
+	.macro  bti, targets
+	.equ	.L__bti_targets_c, 1
+	.equ	.L__bti_targets_j, 2
+	.equ	.L__bti_targets_jc,3
+	.if	.L__bti_targets_\targets == .L__bti_targets_c
+	hint	#34
+	.elseif	.L__bti_targets_\targets == .L__bti_targets_j
+	hint	#36
+	.elseif	.L__bti_targets_\targets == .L__bti_targets_jc
+	hint	#38
+	.else
+	.error	"Unsupported BTI targets '\targets\()'"
+	.endif
+	.endm
+
 /*
  * This macro emits a program property note section identifying
  * architecture features which require special handling, mainly for
arch/arm64/include/asm/linkage.h +1 -6
index 9906541a6861..c5d0c11d7709 100644
@@ -6,12 +6,7 @@
 
 #if defined(CONFIG_ARM64_BTI_KERNEL) && defined(__aarch64__)
 
-/*
- * Since current versions of gas reject the BTI instruction unless we
- * set the architecture version to v8.5 we use the hint instruction
- * instead.
- */
-#define BTI_C hint 34 ;
+#define BTI_C bti c ;
 
 /*
  * When using in-kernel BTI we need to ensure that PCS-conformant assembly
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#2 lore raw

Re: [PATCH v2 1/2] arm64: Add macro version of the BTI instruction

Quoted 44 lines · Mark Brown

BTI is only available from v8.5 so we need to encode it using HINT in generic code and for older toolchains. Add an assembler macro written by Mark Rutland which lets us use the mnemonic and update the existing users.

---
 arch/arm64/crypto/aes-modes.S      | 10 +++++-----
 arch/arm64/include/asm/assembler.h | 18 ++++++++++++++++++
 arch/arm64/include/asm/linkage.h   |  7 +------
 3 files changed, 24 insertions(+), 11 deletions(-)
arch/arm64/crypto/aes-modes.S +5 -5
index b495de22bb38..ff01f0167ba2 100644
@@ -363,15 +363,15 @@ ST5(      mov             v4.16b, vctr.16b                )
        adr             x16, 1f
        sub             x16, x16, x12, lsl #3
        br              x16
-       hint            34                      // bti c
+       bti             c
        mov             v0.d[0], vctr.d[0]
-       hint            34                      // bti c
+       bti             c
        mov             v1.d[0], vctr.d[0]
-       hint            34                      // bti c
+       bti             c
        mov             v2.d[0], vctr.d[0]
-       hint            34                      // bti c
+       bti             c
        mov             v3.d[0], vctr.d[0]
-ST5(   hint            34                              )
+ST5(   bti             c                               )
 ST5(   mov             v4.d[0], vctr.d[0]              )
 1:     b               2f
        .previous
arch/arm64/include/asm/assembler.h +0 -0
index 136d13f3d6e9..87f296198366 100644

arch/arm64/include/asm/assembler.h alternative_endif · line 799

Quoted 10 lines
arch/arm64/include/asm/assembler.h +7 -0
@@ -790,6 +790,24 @@ alternative_endif
 .Lnoyield_\@:
        .endm
 
+/*
+ * Branch Target Identifier (BTI)
+ */
+       .macro  bti, targets
+       .equ    .L__bti_targets_c, 1
+       .equ    .L__bti_targets_j, 2
+       .equ    .L__bti_targets_jc,3

Can't we make these 34/36/38, respectively, and just do

hint #.Lbti_targets_\targets

here?

Quoted 33 lines
arch/arm64/include/asm/assembler.h +11 -0
+       .if     .L__bti_targets_\targets == .L__bti_targets_c
+       hint    #34
+       .elseif .L__bti_targets_\targets == .L__bti_targets_j
+       hint    #36
+       .elseif .L__bti_targets_\targets == .L__bti_targets_jc
+       hint    #38
+       .else
+       .error  "Unsupported BTI targets '\targets\()'"
+       .endif
+       .endm
+
 /*
  * This macro emits a program property note section identifying
  * architecture features which require special handling, mainly for
arch/arm64/include/asm/linkage.h +1 -7
index 9906541a6861..c5d0c11d7709 100644
@@ -6,12 +6,7 @@
 
 #if defined(CONFIG_ARM64_BTI_KERNEL) && defined(__aarch64__)
 
-/*
- * Since current versions of gas reject the BTI instruction unless we
- * set the architecture version to v8.5 we use the hint instruction
- * instead.
- */
-#define BTI_C hint 34 ;
+#define BTI_C bti c ;
 
 /*
  * When using in-kernel BTI we need to ensure that PCS-conformant assembly
--
Quoted 2 lines

2.30.2

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author
#3 lore raw

Re: [PATCH v2 1/2] arm64: Add macro version of the BTI instruction

Quoted 5 lines · Ard Biesheuvel

Can't we make these 34/36/38, respectively, and just do

hint #.Lbti_targets_\targets

here?

That works, the only downside is that it generates more hostile error messages if you typo the argument, eg:

<instantiation>:4:7: error: immediate must be an integer in range [0, 127].
hint #.L__bti_targets_d

I don't really mind either way.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author
#4 lore raw

PATCH v2 2/2 arm64: Unconditionally override SYM_FUNC macros

In reply to Mark Brown

Currently we only override the SYM_FUNC macros when we need to insert BTI C into them. Since we now unconditionally provide the BTI_C macro used to do that let's always override them, that way any issues with our overrides it'll show up more consistently.

Since this means that we now have an unconditional definition of BTI_C update the other users to remove the ifdefs around usage, making them look a bit neater.

---
 arch/arm64/include/asm/linkage.h | 16 +++++++++++-----
 arch/arm64/kernel/entry-ftrace.S |  4 ----
 arch/arm64/lib/kasan_sw_tags.S   |  2 --
 3 files changed, 11 insertions(+), 11 deletions(-)
arch/arm64/include/asm/linkage.h +11 -5
index c5d0c11d7709..1cfa8bb33edd 100644
@@ -8,10 +8,18 @@
 
 #define BTI_C bti c ;
 
+#else
+
+#define BTI_C
+
+#endif
+
 /*
- * When using in-kernel BTI we need to ensure that PCS-conformant assembly
- * functions have suitable annotations.  Override SYM_FUNC_START to insert
- * a BTI landing pad at the start of everything.
+ * When using in-kernel BTI we need to ensure that PCS-conformant
+ * assembly functions have suitable annotations.  Override
+ * SYM_FUNC_START to insert a BTI landing pad at the start of
+ * everything, the override is done unconditionally so we're more
+ * likely to notice any drift from the overridden definitions.
  */
 #define SYM_FUNC_START(name)				\
 	SYM_START(name, SYM_L_GLOBAL, SYM_A_ALIGN)	\
@@ -37,8 +45,6 @@
 	SYM_START(name, SYM_L_WEAK, SYM_A_NONE)		\
 	BTI_C
 
-#endif
-
 /*
  * Annotate a function as position independent, i.e., safe to be called before
  * the kernel virtual mapping is activated.
arch/arm64/kernel/entry-ftrace.S +0 -4
index 8cf970d219f5..46a2de864794 100644
@@ -77,17 +77,13 @@
 	.endm
 
 SYM_CODE_START(ftrace_regs_caller)
-#ifdef BTI_C
 	BTI_C
-#endif
 	ftrace_regs_entry	1
 	b	ftrace_common
 SYM_CODE_END(ftrace_regs_caller)
 
 SYM_CODE_START(ftrace_caller)
-#ifdef BTI_C
 	BTI_C
-#endif
 	ftrace_regs_entry	0
 	b	ftrace_common
 SYM_CODE_END(ftrace_caller)
arch/arm64/lib/kasan_sw_tags.S +0 -2
index 5b04464c045e..a6d6fa2f761e 100644
@@ -38,9 +38,7 @@
  * incremented by 256 prior to return).
  */
 SYM_CODE_START(__hwasan_tag_mismatch)
-#ifdef BTI_C
 	BTI_C
-#endif
 	add	x29, sp, #232
 	stp	x2, x3, [sp, #8 * 2]
 	stp	x4, x5, [sp, #8 * 4]
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel