Skip to contentSkip to conversation

arm64: compat: Implement misalignment fixups for multiword loads

RFC PATCH Superseded Ard Biesheuvel 2 participants 3 messages Jun 23, 2022 – Jun 28, 2022 arm64

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 arm64: compat: Implement misalignment fixups for multiword loads

The 32-bit ARM kernel implements fixups on behalf of user space when using LDM/STM or LDRD/STRD instructions on addresses that are not 32-bit aligned. This is not something that is supported by the architecture, but was done anyway to increase compatibility with user space software, which mostly targeted x86 at the time and did not care about aligned accesses.

This feature is one of the remaining impediments to being able to switch to 64-bit kernels on 64-bit capable hardware running 32-bit user space, so let's implement it for the arm64 compat layer as well.

Note that the intent is to implement the exact same handling of misaligned multi-word loads and stores as the 32-bit kernel does, including what appears to be missing support for user space programs that rely on SETEND to switch to a different byte order and back. Also, like the 32-bit ARM version, we rely on the faulting address reported by the CPU to infer the memory address, instead of decoding the instruction fully to obtain this information.

This implementation is taken from the 32-bit ARM tree, with all pieces removed that deal with instructions other than LDRD/STRD and LDM/STM, or that deal with alignment exceptions taken in kernel mode.

---
 arch/arm64/Kconfig                   |   4 +
 arch/arm64/include/asm/exception.h   |   1 +
 arch/arm64/kernel/Makefile           |   1 +
 arch/arm64/kernel/compat_alignment.c | 388 ++++++++++++++++++++
 arch/arm64/mm/fault.c                |   3 +
 5 files changed, 397 insertions(+)
arch/arm64/Kconfig +4 -0
index 1652a9800ebe..401e4f8fa149 100644
@@ -1508,6 +1508,10 @@ config THUMB2_COMPAT_VDSO
 	  Compile the compat vDSO with '-mthumb -fomit-frame-pointer' if y,
 	  otherwise with '-marm'.
 
+config COMPAT_ALIGNMENT_FIXUPS
+	bool "Fix up misaligned multi-word loads and stores in user space"
+	default y
+
 menuconfig ARMV8_DEPRECATED
 	bool "Emulate deprecated/obsolete ARMv8 instructions"
 	depends on SYSCTL
arch/arm64/include/asm/exception.h +1 -0
index d94aecff9690..e92ca08f754c 100644
@@ -70,6 +70,7 @@ void do_sysinstr(unsigned long esr, struct pt_regs *regs);
 void do_sp_pc_abort(unsigned long addr, unsigned long esr, struct pt_regs *regs);
 void bad_el0_sync(struct pt_regs *regs, int reason, unsigned long esr);
 void do_cp15instr(unsigned long esr, struct pt_regs *regs);
+int do_compat_alignment_fixup(unsigned long addr, struct pt_regs *regs);
 void do_el0_svc(struct pt_regs *regs);
 void do_el0_svc_compat(struct pt_regs *regs);
 void do_ptrauth_fault(struct pt_regs *regs, unsigned long esr);
arch/arm64/kernel/Makefile +1 -0
index fa7981d0d917..58b472fa34fe 100644
@@ -40,6 +40,7 @@ $(obj)/%.stub.o: $(obj)/%.o FORCE
 obj-$(CONFIG_COMPAT)			+= sys32.o signal32.o			\
 					   sys_compat.o
 obj-$(CONFIG_COMPAT)			+= sigreturn32.o
+obj-$(CONFIG_COMPAT_ALIGNMENT_FIXUPS)	+= compat_alignment.o
 obj-$(CONFIG_KUSER_HELPERS)		+= kuser32.o
 obj-$(CONFIG_FUNCTION_TRACER)		+= ftrace.o entry-ftrace.o
 obj-$(CONFIG_MODULES)			+= module.o
arch/arm64/kernel/compat_alignment.c new file +388 -0

388 lines not on this page · raw message

arch/arm64/mm/fault.c +3 -0
index c5e11768e5c1..b25119b4beca 100644
@@ -687,6 +687,9 @@ static int __kprobes do_translation_fault(unsigned long far,
 static int do_alignment_fault(unsigned long far, unsigned long esr,
 			      struct pt_regs *regs)
 {
+	if (IS_ENABLED(CONFIG_COMPAT_ALIGNMENT_FIXUPS) &&
+	    compat_user_mode(regs))
+		return do_compat_alignment_fixup(far, regs);
 	do_bad_area(far, esr, regs);
 	return 0;
 }
Signature · 7 lines
2.35.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#1 lore raw
Quoted 20 lines · Ard Biesheuvel

The 32-bit ARM kernel implements fixups on behalf of user space when using LDM/STM or LDRD/STRD instructions on addresses that are not 32-bit aligned. This is not something that is supported by the architecture, but was done anyway to increase compatibility with user space software, which mostly targeted x86 at the time and did not care about aligned accesses.

This feature is one of the remaining impediments to being able to switch to 64-bit kernels on 64-bit capable hardware running 32-bit user space, so let's implement it for the arm64 compat layer as well.

Note that the intent is to implement the exact same handling of misaligned multi-word loads and stores as the 32-bit kernel does, including what appears to be missing support for user space programs that rely on SETEND to switch to a different byte order and back. Also, like the 32-bit ARM version, we rely on the faulting address reported by the CPU to infer the memory address, instead of decoding the instruction fully to obtain this information.

Quoted 5 lines

This implementation is taken from the 32-bit ARM tree, with all pieces removed that deal with instructions other than LDRD/STRD and LDM/STM, or that deal with alignment exceptions taken in kernel mode.

Thanks a lot for implementing this! I know this has been a problem in particular for Debian, as the lack of this emulation has prevented them from migrating the build environment to modern 64-bit machines while the remaining 32-bit boxes in the build infrastructure are suffering from age.

I've added the debian arm list and some developers that are probably interested in it.

arch/arm64/Kconfig config THUMB2_COMPAT_VDSO · line 1513

Quoted 3 lines
arch/arm64/Kconfig +3 -0
+config COMPAT_ALIGNMENT_FIXUPS
+       bool "Fix up misaligned multi-word loads and stores in user space"
+       default y

My initial thought was that we probably want both compile-time and runtime switches for this, the same way that ARMV8_DEPRECATED does, but keeping it simple with just a compile-time option is totally fine as well, as far as I'm concerned.

If we end up wanting an runtime switch after all, it should probably follow the interface from Documentation/arm64/legacy_instructions.rst, though it's not obvious how to best do it, since the instructions are otherwise available for aligned data as designed, and not deprecated at all.

Adding calls to trace_instruction_emulation() might be helpful to allow tracing the fixups the say way we do for setend, swp and the cp15 barriers.

Quoted 3 lines

+ * *** NOTE ***
+ * This code is not portable to processors with late data abort handling.
+ */

I see the comment is copied from the arm32 version. Is it actually relevant for arm64 though?

Quoted 17 lines
arch/arm64/kernel/compat_alignment.c new file +17 -0
+static void *
+do_alignment_t32_to_handler(u32 *pinstr, struct pt_regs *regs,
+                           union offset_union *poffset)
+{
+       u32 instr = *pinstr;
+       u16 tinst1 = (instr >> 16) & 0xffff;
+       u16 tinst2 = instr & 0xffff;
+
+       switch (tinst1 & 0xffe0) {
+       /* A6.3.5 Load/Store multiple */
+       case 0xe880:            /* STM/STMIA/STMEA,LDM/LDMIA, PUSH/POP T2 */
+       case 0xe8a0:            /* ...above writeback version */
+       case 0xe900:            /* STMDB/STMFD, LDMDB/LDMEA */
+       case 0xe920:            /* ...above writeback version */
+               /* no need offset decision since handler calculates it */
+               return do_alignment_ldmstm;
+

arch/arm64/kernel/compat_alignment.c line 260

Quoted 10 lines
arch/arm64/kernel/compat_alignment.c new file +10 -0
+       case 0xf840:            /* POP/PUSH T3 (single register) */
+               if (RN_BITS(instr) == 13 && (tinst2 & 0x09ff) == 0x0904) {
+                       u32 L = !!(LDST_L_BIT(instr));
+                       const u32 subset[2] = {
+                               0xe92d0000,     /* STMDB sp!,{registers} */
+                               0xe8bd0000,     /* LDMIA sp!,{registers} */
+                       };
+                       *pinstr = subset[L] | (1<<RD_BITS(instr));
+                       return do_alignment_ldmstm;
+               }

The code clearly shows its age here, I doubt we'd do the function pointer handling the same way these days, but I think you made the right call here in keeping close to the original version while removing most of the irrelevant cases.

Quoted 6 lines
arch/arm64/kernel/compat_alignment.c new file +6 -0
+static int alignment_get_arm(struct pt_regs *regs, u32 __user *ip, u32 *inst)
+{
+       u32 instr = 0;
+       int fault;
+
+       fault = get_user(instr, ip);

arch/arm64/kernel/compat_alignment.c line 301

Quoted 10 lines
arch/arm64/kernel/compat_alignment.c new file +10 -0
+       if (fault)
+               return fault;
+
+       *inst = __le32_to_cpu(instr);
+       return 0;
+}
+
+static int alignment_get_thumb(struct pt_regs *regs, u16 __user *ip, u16 *inst)
+{
+       u16 instr = 0;

I think the types need to be adjusted, e.g. s/u32/__le32/ to avoid sparse warnings.

Arnd

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

#2 lore raw
Quoted 52 lines · Arnd Bergmann
Quoted 20 lines · Ard Biesheuvel

The 32-bit ARM kernel implements fixups on behalf of user space when using LDM/STM or LDRD/STRD instructions on addresses that are not 32-bit aligned. This is not something that is supported by the architecture, but was done anyway to increase compatibility with user space software, which mostly targeted x86 at the time and did not care about aligned accesses.

This feature is one of the remaining impediments to being able to switch to 64-bit kernels on 64-bit capable hardware running 32-bit user space, so let's implement it for the arm64 compat layer as well.

Note that the intent is to implement the exact same handling of misaligned multi-word loads and stores as the 32-bit kernel does, including what appears to be missing support for user space programs that rely on SETEND to switch to a different byte order and back. Also, like the 32-bit ARM version, we rely on the faulting address reported by the CPU to infer the memory address, instead of decoding the instruction fully to obtain this information.

Quoted 5 lines

This implementation is taken from the 32-bit ARM tree, with all pieces removed that deal with instructions other than LDRD/STRD and LDM/STM, or that deal with alignment exceptions taken in kernel mode.

Thanks a lot for implementing this! I know this has been a problem in particular for Debian, as the lack of this emulation has prevented them from migrating the build environment to modern 64-bit machines while the remaining 32-bit boxes in the build infrastructure are suffering from age.

I've added the debian arm list and some developers that are probably interested in it.

arch/arm64/Kconfig config THUMB2_COMPAT_VDSO · line 1513

Quoted 3 lines
arch/arm64/Kconfig +3 -0
+config COMPAT_ALIGNMENT_FIXUPS
+       bool "Fix up misaligned multi-word loads and stores in user space"
+       default y

My initial thought was that we probably want both compile-time and runtime switches for this, the same way that ARMV8_DEPRECATED does, but keeping it simple with just a compile-time option is totally fine as well, as far as I'm concerned.

If we end up wanting an runtime switch after all, it should probably follow the interface from Documentation/arm64/legacy_instructions.rst, though it's not obvious how to best do it, since the instructions are otherwise available for aligned data as designed, and not deprecated at all.

Quoted 4 lines

Adding calls to trace_instruction_emulation() might be helpful to allow tracing the fixups the say way we do for setend, swp and the cp15 barriers.

Yeah and I noticed some perf accounting as well, which might also be useful.

Quoted 7 lines
Quoted 3 lines

+ * *** NOTE ***
+ * This code is not portable to processors with late data abort handling.
+ */

I see the comment is copied from the arm32 version. Is it actually relevant for arm64 though?

I'll drop that - I don't know what it means anyway

Quoted 24 lines
Quoted 24 lines
arch/arm64/kernel/compat_alignment.c new file +24 -0
+static void *
+do_alignment_t32_to_handler(u32 *pinstr, struct pt_regs *regs,
+                           union offset_union *poffset)
+{
+       u32 instr = *pinstr;
+       u16 tinst1 = (instr >> 16) & 0xffff;
+       u16 tinst2 = instr & 0xffff;
+
+       switch (tinst1 & 0xffe0) {
+       /* A6.3.5 Load/Store multiple */
+       case 0xe880:            /* STM/STMIA/STMEA,LDM/LDMIA, PUSH/POP T2 */
+       case 0xe8a0:            /* ...above writeback version */
+       case 0xe900:            /* STMDB/STMFD, LDMDB/LDMEA */
+       case 0xe920:            /* ...above writeback version */
+               /* no need offset decision since handler calculates it */
+               return do_alignment_ldmstm;
+
+       case 0xf840:            /* POP/PUSH T3 (single register) */
+               if (RN_BITS(instr) == 13 && (tinst2 & 0x09ff) == 0x0904) {
+                       u32 L = !!(LDST_L_BIT(instr));
+                       const u32 subset[2] = {
+                               0xe92d0000,     /* STMDB sp!,{registers} */
+                               0xe8bd0000,     /* LDMIA sp!,{registers} */
+                       };
Quoted 9 lines

arch/arm64/kernel/compat_alignment.c line 260

Quoted 3 lines
arch/arm64/kernel/compat_alignment.c new file +3 -0
+                       *pinstr = subset[L] | (1<<RD_BITS(instr));
+                       return do_alignment_ldmstm;
+               }

The code clearly shows its age here, I doubt we'd do the function pointer handling the same way these days, but I think you made the right call here in keeping close to the original version while removing most of the irrelevant cases.

Yeah.

I did notice just now that the original code does not advance the IT state when emulating conditional Thumb instructions. Fortunately, if the fault is taken, we now that condition check passed, so we don't have to bother with that, but we should make sure that the IT state does not get out of sync with the subsequent instructions.

Quoted 11 lines
Quoted 11 lines
arch/arm64/kernel/compat_alignment.c new file +11 -0
+static int alignment_get_arm(struct pt_regs *regs, u32 __user *ip, u32 *inst)
+{
+       u32 instr = 0;
+       int fault;
+
+       fault = get_user(instr, ip);
+       if (fault)
+               return fault;
+
+       *inst = __le32_to_cpu(instr);
+       return 0;
Quoted 9 lines

arch/arm64/kernel/compat_alignment.c line 301

Quoted 5 lines
arch/arm64/kernel/compat_alignment.c new file +5 -0
+}
+
+static int alignment_get_thumb(struct pt_regs *regs, u16 __user *ip, u16 *inst)
+{
+       u16 instr = 0;

I think the types need to be adjusted, e.g. s/u32/__le32/ to avoid sparse warnings.

I'll check with sparse, and fix accordingly.

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