Skip to contentSkip to conversation

arm64: Make NOP handling a whitelist

PATCH Superseded Mark Brown 2 participants 11 messages Apr 28, 2020 – Apr 30, 2020 arm64

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Mark Brown

author
#0 lore raw

v2 cover arm64: Make NOP handling a whitelist

Currently we default to assuming any unrecognized instruction in the hint space can be safely handled as a NOP. This is not robust and any code that really wants a NOP should be using the explicitly defined NOP so let's instead invert this and whitelist those instructions which it is safe to handle as NOPs.

Patch 2 adds defines for the HINTs for BTI landing pads which will be used by the in-kernel BTI series to generate landing pads in JITed BPF code so it'd be good if this could be applied on or merged into the BTI branch.

v2:
- Fix values for BTI HINTs.
- Rebase on v5.7-rc3+for-next/bti

Mark Brown (3):
arm64: insn: Don't assume unrecognized HINTs are NOPs
arm64: insn: Add constants for PAC and BTI instruction decode
arm64: insn: Report PAC and BTI instructions as NOPs

 arch/arm64/include/asm/insn.h | 22 ++++++++++++++++++++--
 arch/arm64/kernel/insn.c      | 32 ++++++++++++++++++++++++--------
 2 files changed, 44 insertions(+), 10 deletions(-)
Signature · 7 lines
2.20.1


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

PATCH v2 1/3 arm64: insn: Don't assume unrecognized HINTs are NOPs

Currently the kernel assumes that any HINT which it does not explicitly recognise is a NOP. This is not robust as new instructions may be added which need special handling, including recent extensions like PAC, and in any case software should only be using explicit NOP instructions for deliberate NOPs.

This has the effect of rendering PAC and BTI instructions unprobeable which means that probes can't be inserted on the first instruction of functions built with those features.

---
 arch/arm64/kernel/insn.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
arch/arm64/kernel/insn.c +3 -7
index 4a9e773a177f..535a3a7a053e 100644
@@ -58,14 +58,10 @@ bool __kprobes aarch64_insn_is_nop(u32 insn)
 		return false;
 
 	switch (insn & 0xFE0) {
-	case AARCH64_INSN_HINT_YIELD:
-	case AARCH64_INSN_HINT_WFE:
-	case AARCH64_INSN_HINT_WFI:
-	case AARCH64_INSN_HINT_SEV:
-	case AARCH64_INSN_HINT_SEVL:
-		return false;
-	default:
+	case AARCH64_INSN_HINT_NOP:
 		return true;
+	default:
+		return false;
 	}
 }
 
Signature · 7 lines
2.20.1


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

PATCH v2 2/3 arm64: insn: Add constants for PAC and BTI instruction decode

In reply to Mark Brown

Add constants for decoding the various PAC and BTI instructions defined in the HINT space. Since we are now decoding both the op2 and CRm fields rename the enum as well; this is compatible with what the existing users are doing.

---
 arch/arm64/include/asm/insn.h | 22 ++++++++++++++++++++--
 arch/arm64/kernel/insn.c      |  2 +-
 2 files changed, 21 insertions(+), 3 deletions(-)
arch/arm64/include/asm/insn.h +20 -2
index bb313dde58a4..e5a87f98a548 100644
@@ -39,13 +39,31 @@ enum aarch64_insn_encoding_class {
 					 * system instructions */
 };
 
-enum aarch64_insn_hint_op {
+enum aarch64_insn_hint_cr_op {
 	AARCH64_INSN_HINT_NOP	= 0x0 << 5,
 	AARCH64_INSN_HINT_YIELD	= 0x1 << 5,
 	AARCH64_INSN_HINT_WFE	= 0x2 << 5,
 	AARCH64_INSN_HINT_WFI	= 0x3 << 5,
 	AARCH64_INSN_HINT_SEV	= 0x4 << 5,
 	AARCH64_INSN_HINT_SEVL	= 0x5 << 5,
+
+	AARCH64_INSN_HINT_PACIA_1716 = 0x08 << 5,
+	AARCH64_INSN_HINT_PACIB_1716 = 0x0A << 5,
+	AARCH64_INSN_HINT_AUTIA_1716 = 0x0B << 5,
+	AARCH64_INSN_HINT_AUTIB_1716 = 0x0C << 5,
+	AARCH64_INSN_HINT_PACIAZ     = 0x18 << 5,
+	AARCH64_INSN_HINT_PACIASP    = 0x19 << 5,
+	AARCH64_INSN_HINT_PACIBZ     = 0x1A << 5,
+	AARCH64_INSN_HINT_PACIBSP    = 0x1B << 5,
+	AARCH64_INSN_HINT_AUTIAZ     = 0x1C << 5,
+	AARCH64_INSN_HINT_AUTIASP    = 0x1D << 5,
+	AARCH64_INSN_HINT_AUTIBZ     = 0x1E << 5,
+	AARCH64_INSN_HINT_AUTIBSP    = 0x1F << 5,
+
+	AARCH64_INSN_HINT_BTI   = 0x20 << 5,
+	AARCH64_INSN_HINT_BTIC  = 0x22 << 5,
+	AARCH64_INSN_HINT_BTIJ  = 0x24 << 5,
+	AARCH64_INSN_HINT_BTIJC = 0x2C << 5,
 };
 
 enum aarch64_insn_imm_type {
@@ -370,7 +388,7 @@ u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr,
 				     enum aarch64_insn_branch_type type);
 u32 aarch64_insn_gen_cond_branch_imm(unsigned long pc, unsigned long addr,
 				     enum aarch64_insn_condition cond);
-u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_op op);
+u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_cr_op op);
 u32 aarch64_insn_gen_nop(void);
 u32 aarch64_insn_gen_branch_reg(enum aarch64_insn_register reg,
 				enum aarch64_insn_branch_type type);
arch/arm64/kernel/insn.c +1 -1
index 535a3a7a053e..fd77cdd87c47 100644
@@ -570,7 +570,7 @@ u32 aarch64_insn_gen_cond_branch_imm(unsigned long pc, unsigned long addr,
 					     offset >> 2);
 }
 
-u32 __kprobes aarch64_insn_gen_hint(enum aarch64_insn_hint_op op)
+u32 __kprobes aarch64_insn_gen_hint(enum aarch64_insn_hint_cr_op op)
 {
 	return aarch64_insn_get_hint_value() | op;
 }
Signature · 7 lines
2.20.1


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

PATCH v2 3/3 arm64: insn: Report PAC and BTI instructions as NOPs

In reply to Mark Brown

In order to allow probing of PAC and BTI instructions without more specialized support recognize them as NOPs.

---
 arch/arm64/kernel/insn.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
arch/arm64/kernel/insn.c +20 -0
index fd77cdd87c47..82afc582d29a 100644
@@ -57,7 +57,27 @@ bool __kprobes aarch64_insn_is_nop(u32 insn)
 	if (!aarch64_insn_is_hint(insn))
 		return false;
 
+	/*
+	 * The PAC and BTI instructons are not strictly NOPs but until
+	 * better support is added we can treat them as such.
+	 */
 	switch (insn & 0xFE0) {
+	case AARCH64_INSN_HINT_PACIA_1716:
+	case AARCH64_INSN_HINT_PACIB_1716:
+	case AARCH64_INSN_HINT_AUTIA_1716:
+	case AARCH64_INSN_HINT_AUTIB_1716:
+	case AARCH64_INSN_HINT_PACIAZ:
+	case AARCH64_INSN_HINT_PACIASP:
+	case AARCH64_INSN_HINT_PACIBZ:
+	case AARCH64_INSN_HINT_PACIBSP:
+	case AARCH64_INSN_HINT_AUTIAZ:
+	case AARCH64_INSN_HINT_AUTIASP:
+	case AARCH64_INSN_HINT_AUTIBZ:
+	case AARCH64_INSN_HINT_AUTIBSP:
+	case AARCH64_INSN_HINT_BTI:
+	case AARCH64_INSN_HINT_BTIC:
+	case AARCH64_INSN_HINT_BTIJ:
+	case AARCH64_INSN_HINT_BTIJC:
 	case AARCH64_INSN_HINT_NOP:
 		return true;
 	default:
Signature · 7 lines
2.20.1


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

Re: [PATCH v2 2/3] arm64: insn: Add constants for PAC and BTI instruction decode

In reply to Mark Brown
Quoted 23 lines · Mark Brown

Add constants for decoding the various PAC and BTI instructions defined in the HINT space. Since we are now decoding both the op2 and CRm fields rename the enum as well; this is compatible with what the existing users are doing.

---
 arch/arm64/include/asm/insn.h | 22 ++++++++++++++++++++--
 arch/arm64/kernel/insn.c      |  2 +-
 2 files changed, 21 insertions(+), 3 deletions(-)
arch/arm64/include/asm/insn.h +1 -1
index bb313dde58a4..e5a87f98a548 100644
@@ -39,13 +39,31 @@ enum aarch64_insn_encoding_class {
 					 * system instructions */
 };
 
-enum aarch64_insn_hint_op {
+enum aarch64_insn_hint_cr_op {
 	AARCH64_INSN_HINT_NOP	= 0x0 << 5,

arch/arm64/include/asm/insn.h enum aarch64_insn_encoding_class { · line 53

Quoted 10 lines
arch/arm64/include/asm/insn.h +5 -0
 	AARCH64_INSN_HINT_YIELD	= 0x1 << 5,
 	AARCH64_INSN_HINT_WFE	= 0x2 << 5,
 	AARCH64_INSN_HINT_WFI	= 0x3 << 5,
 	AARCH64_INSN_HINT_SEV	= 0x4 << 5,
 	AARCH64_INSN_HINT_SEVL	= 0x5 << 5,
+
+	AARCH64_INSN_HINT_PACIA_1716 = 0x08 << 5,
+	AARCH64_INSN_HINT_PACIB_1716 = 0x0A << 5,
+	AARCH64_INSN_HINT_AUTIA_1716 = 0x0B << 5,
+	AARCH64_INSN_HINT_AUTIB_1716 = 0x0C << 5,

These still look wrong to me.

arch/arm64/include/asm/insn.h enum aarch64_insn_encoding_class { · line 59

Quoted 6 lines
arch/arm64/include/asm/insn.h +6 -0
+	AARCH64_INSN_HINT_PACIAZ     = 0x18 << 5,
+	AARCH64_INSN_HINT_PACIASP    = 0x19 << 5,
+	AARCH64_INSN_HINT_PACIBZ     = 0x1A << 5,
+	AARCH64_INSN_HINT_PACIBSP    = 0x1B << 5,
+	AARCH64_INSN_HINT_AUTIAZ     = 0x1C << 5,
+	AARCH64_INSN_HINT_AUTIASP    = 0x1D << 5,

This one is typo'd in the Arm ARM as AUTHASP. Seems nobody can get this stuff right!

arch/arm64/include/asm/insn.h enum aarch64_insn_encoding_class { · line 66

Quoted 7 lines
arch/arm64/include/asm/insn.h +7 -0
+	AARCH64_INSN_HINT_AUTIBZ     = 0x1E << 5,
+	AARCH64_INSN_HINT_AUTIBSP    = 0x1F << 5,
+
+	AARCH64_INSN_HINT_BTI   = 0x20 << 5,
+	AARCH64_INSN_HINT_BTIC  = 0x22 << 5,
+	AARCH64_INSN_HINT_BTIJ  = 0x24 << 5,
+	AARCH64_INSN_HINT_BTIJC = 0x2C << 5,

This looks wrong as well.

Will

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

maintainer
#2 lore raw

Re: [PATCH v2 1/3] arm64: insn: Don't assume unrecognized HINTs are NOPs

In reply to Mark Brown
Quoted 5 lines · Mark Brown

Currently the kernel assumes that any HINT which it does not explicitly recognise is a NOP. This is not robust as new instructions may be added which need special handling, including recent extensions like PAC, and in any case software should only be using explicit NOP instructions for deliberate NOPs.

Hmm, I'm not sure that using PAC as the justification makes tonnes of sense here, since you go ahead and whitelist the PAC hints in patch 3!

Will

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

maintainer
#7 lore raw

Re: [PATCH v2 3/3] arm64: insn: Report PAC and BTI instructions as NOPs

In reply to Mark Brown
Quoted 14 lines · Mark Brown

In order to allow probing of PAC and BTI instructions without more specialized support recognize them as NOPs.

---
 arch/arm64/kernel/insn.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
arch/arm64/kernel/insn.c +0 -0
index fd77cdd87c47..82afc582d29a 100644

arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_is_nop(u32 insn) · line 64

Quoted 8 lines
arch/arm64/kernel/insn.c +4 -0
@@ -57,7 +57,27 @@ bool __kprobes aarch64_insn_is_nop(u32 insn)
 	if (!aarch64_insn_is_hint(insn))
 		return false;
 
+	/*
+	 * The PAC and BTI instructons are not strictly NOPs but until
+	 * better support is added we can treat them as such.
+	 */
 	switch (insn & 0xFE0) {

Are you deliberately omitting XPACLRI? If so, maybe add a comment to say why, since it looks a bit weird without it.

Will

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

author
#5 lore raw

Re: [PATCH v2 2/3] arm64: insn: Add constants for PAC and BTI instruction decode

In reply to Will Deacon
Quoted 8 lines · Will Deacon

arch/arm64/include/asm/insn.h enum aarch64_insn_encoding_class { · line 53

Quoted 4 lines · Mark Brown
arch/arm64/include/asm/insn.h +4 -0
+	AARCH64_INSN_HINT_PACIA_1716 = 0x08 << 5,
+	AARCH64_INSN_HINT_PACIB_1716 = 0x0A << 5,
+	AARCH64_INSN_HINT_AUTIA_1716 = 0x0B << 5,
+	AARCH64_INSN_HINT_AUTIB_1716 = 0x0C << 5,

These still look wrong to me.

Ack, I think I must've glazed over after checking PACIA which is right or missed stuff when cut'n'pasting to verify.

Quoted 9 lines

arch/arm64/include/asm/insn.h enum aarch64_insn_encoding_class { · line 59

Quoted 6 lines
arch/arm64/include/asm/insn.h +6 -0
+	AARCH64_INSN_HINT_PACIAZ     = 0x18 << 5,
+	AARCH64_INSN_HINT_PACIASP    = 0x19 << 5,
+	AARCH64_INSN_HINT_PACIBZ     = 0x1A << 5,
+	AARCH64_INSN_HINT_PACIBSP    = 0x1B << 5,
+	AARCH64_INSN_HINT_AUTIAZ     = 0x1C << 5,
+	AARCH64_INSN_HINT_AUTIASP    = 0x1D << 5,

This one is typo'd in the Arm ARM as AUTHASP. Seems nobody can get this stuff right!

It seems to have been fixed in the current revision - or at least AUTIASP is there and text search isn't finding any instances of AUTHASP.

Quoted 6 lines

arch/arm64/include/asm/insn.h enum aarch64_insn_encoding_class { · line 66

Quoted 4 lines
arch/arm64/include/asm/insn.h +4 -0
+	AARCH64_INSN_HINT_BTI   = 0x20 << 5,
+	AARCH64_INSN_HINT_BTIC  = 0x22 << 5,
+	AARCH64_INSN_HINT_BTIJ  = 0x24 << 5,
+	AARCH64_INSN_HINT_BTIJC = 0x2C << 5,

This looks wrong as well.

_JC is wrong, yes.

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

author
#8 lore raw

Re: [PATCH v2 3/3] arm64: insn: Report PAC and BTI instructions as NOPs

In reply to Will Deacon
Quoted 10 lines · Will Deacon

arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_is_nop(u32 insn) · line 64

Quoted 5 lines · Mark Brown
arch/arm64/kernel/insn.c +4 -0
+	/*
+	 * The PAC and BTI instructons are not strictly NOPs but until
+	 * better support is added we can treat them as such.
+	 */
 	switch (insn & 0xFE0) {

Are you deliberately omitting XPACLRI? If so, maybe add a comment to say why, since it looks a bit weird without it.

Not deliberately, no - I'm not sure I'd heard of it before and it's one of those cases where the spec doesn't call out the instruction as being in the hint space.

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

maintainer
#9 lore raw

Re: [PATCH v2 3/3] arm64: insn: Report PAC and BTI instructions as NOPs

Quoted 12 lines · Mark Brown
Quoted 10 lines · Will Deacon

arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_is_nop(u32 insn) · line 64

Quoted 5 lines · Mark Brown
arch/arm64/kernel/insn.c +4 -0
+	/*
+	 * The PAC and BTI instructons are not strictly NOPs but until
+	 * better support is added we can treat them as such.
+	 */
 	switch (insn & 0xFE0) {

Are you deliberately omitting XPACLRI? If so, maybe add a comment to say why, since it looks a bit weird without it.

Quoted 3 lines

Not deliberately, no - I'm not sure I'd heard of it before and it's one of those cases where the spec doesn't call out the instruction as being in the hint space.

Hmm, so I only spotted it because of "Decode" pseudocode for HINT on p.929 of rev F.a of the Arm ARM (the one with the AUTHASP typo).

Will

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

author
#10 lore raw

Re: [PATCH v2 3/3] arm64: insn: Report PAC and BTI instructions as NOPs

Quoted 6 lines · Will Deacon
Quoted 4 lines · Mark Brown
Quoted 2 lines · Will Deacon

Are you deliberately omitting XPACLRI? If so, maybe add a comment to say why, since it looks a bit weird without it.

Quoted 6 lines
Quoted 3 lines

Not deliberately, no - I'm not sure I'd heard of it before and it's one of those cases where the spec doesn't call out the instruction as being in the hint space.

Hmm, so I only spotted it because of "Decode" pseudocode for HINT on p.929 of rev F.a of the Arm ARM (the one with the AUTHASP typo).

Ah, I see - that's indeed a useful enumeration of the currently defined HINTs. IIRC I wrote this code before I became aware that HINT was defined as an instruction rather than just being an encoding space! Thanks for the pointer.

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