arm: prevent compiler from using unaligned accesses
PATCH 1/1 arm: prevent compiler from using unaligned accesses
The ARM architecture requires all accesses to device memory to be naturally aligned[1][2]. Normal memory does not have this strict requirement, and in fact many systems do ignore unaligned accesses (by the means of clearing the A bit in SCTLR and accessing normal memory). So the default behaviour of GCC assumes that unaligned accesses are fine, at least if happening on the stack.
Now kvm-unit-tests runs some C code with the MMU off, which degrades the whole system memory to device memory. Now every unaligned access will fault, regardless of the A bit.
In fact there is at least one place in lib/printf.c where GCC merges two consecutive char* accesses into one "strh" instruction, writing to a potentially unaligned address.
This can be reproduced by configuring kvm-unit-tests for kvmtool, but running it on QEMU, which triggers an early printf that exercises this particular code path.
Add the -mstrict-align compiler option to the arm64 CFLAGS to fix this problem. Also add the respective -mno-unaligned-access flag for arm.
Thanks to Alexandru for helping debugging this.
[1] ARMv8 ARM DDI 0487E.a, B2.5.2
[2] ARMv7 ARM DDI 0406C.d, A3.2.1
--- arm/Makefile.arm | 1 + arm/Makefile.arm64 | 1 + 2 files changed, 2 insertions(+)
arm/Makefile.arm +1 -0
@@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y CFLAGS += $(machine) CFLAGS += -mcpu=$(PROCESSOR) +CFLAGS += -mno-unaligned-access arch_LDFLAGS = -Ttext=40010000
arm/Makefile.arm64 +1 -0
@@ -7,6 +7,7 @@ bits = 64 ldarch = elf64-littleaarch64 arch_LDFLAGS = -pie -n +CFLAGS += -mstrict-align define arch_elf_check = $(if $(shell ! $(OBJDUMP) -R $(1) >&/dev/null && echo "nok"),
Signature · 1 line
2.17.1
Quoted 58 lines · Andre Przywara
The ARM architecture requires all accesses to device memory to be naturally aligned[1][2]. Normal memory does not have this strict requirement, and in fact many systems do ignore unaligned accesses (by the means of clearing the A bit in SCTLR and accessing normal memory). So the default behaviour of GCC assumes that unaligned accesses are fine, at least if happening on the stack.
Now kvm-unit-tests runs some C code with the MMU off, which degrades the whole system memory to device memory. Now every unaligned access will fault, regardless of the A bit.
In fact there is at least one place inlib/printf.cwhere GCC merges two consecutive char* accesses into one "strh" instruction, writing to a potentially unaligned address.
This can be reproduced by configuring kvm-unit-tests for kvmtool, but running it on QEMU, which triggers an early printf that exercises this particular code path.Add the -mstrict-align compiler option to the arm64 CFLAGS to fix this problem. Also add the respective -mno-unaligned-access flag for arm.
Thanks to Alexandru for helping debugging this.
Signed-off-by Andre Przywara <[email protected]>[1] ARMv8 ARM DDI 0487E.a, B2.5.2
[2] ARMv7 ARM DDI 0406C.d, A3.2.1--- arm/Makefile.arm | 1 + arm/Makefile.arm64 | 1 + 2 files changed, 2 insertions(+)arm/Makefile.arm +1
-0@@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y CFLAGS += $(machine) CFLAGS += -mcpu=$(PROCESSOR) +CFLAGS += -mno-unaligned-access arch_LDFLAGS = -Ttext=40010000arm/Makefile.arm64 +1
-0@@ -7,6 +7,7 @@ bits = 64 ldarch = elf64-littleaarch64 arch_LDFLAGS = -pie -n +CFLAGS += -mstrict-align define arch_elf_check = $(if $(shell ! $(OBJDUMP) -R $(1) >&/dev/null && echo "nok"),Signature · 1 line
2.17.1
Quoted 49 lines · Andre Przywara
The ARM architecture requires all accesses to device memory to be naturally aligned[1][2]. Normal memory does not have this strict requirement, and in fact many systems do ignore unaligned accesses (by the means of clearing the A bit in SCTLR and accessing normal memory). So the default behaviour of GCC assumes that unaligned accesses are fine, at least if happening on the stack.
Now kvm-unit-tests runs some C code with the MMU off, which degrades the whole system memory to device memory. Now every unaligned access will fault, regardless of the A bit.
In fact there is at least one place inlib/printf.cwhere GCC merges two consecutive char* accesses into one "strh" instruction, writing to a potentially unaligned address.
This can be reproduced by configuring kvm-unit-tests for kvmtool, but running it on QEMU, which triggers an early printf that exercises this particular code path.Add the -mstrict-align compiler option to the arm64 CFLAGS to fix this problem. Also add the respective -mno-unaligned-access flag for arm.
Thanks to Alexandru for helping debugging this.
Signed-off-by Andre Przywara <[email protected]>[1] ARMv8 ARM DDI 0487E.a, B2.5.2
[2] ARMv7 ARM DDI 0406C.d, A3.2.1--- arm/Makefile.arm | 1 + arm/Makefile.arm64 | 1 + 2 files changed, 2 insertions(+)arm/Makefile.arm +1
-0@@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y CFLAGS += $(machine) CFLAGS += -mcpu=$(PROCESSOR) +CFLAGS += -mno-unaligned-access arch_LDFLAGS = -Ttext=40010000arm/Makefile.arm64 +0
-0
arm/Makefile.arm64 bits = 64 · line 13
Quoted 7 lines
arm/Makefile.arm64 +1
-0@@ -7,6 +7,7 @@ bits = 64 ldarch = elf64-littleaarch64 arch_LDFLAGS = -pie -n +CFLAGS += -mstrict-align define arch_elf_check = $(if $(shell ! $(OBJDUMP) -R $(1) >&/dev/null && echo "nok"),
Queued, thanks.
Paolo
Quoted 48 lines · Andre Przywara
The ARM architecture requires all accesses to device memory to be naturally aligned[1][2]. Normal memory does not have this strict requirement, and in fact many systems do ignore unaligned accesses (by the means of clearing the A bit in SCTLR and accessing normal memory). So the default behaviour of GCC assumes that unaligned accesses are fine, at least if happening on the stack.
Now kvm-unit-tests runs some C code with the MMU off, which degrades the whole system memory to device memory. Now every unaligned access will fault, regardless of the A bit.
In fact there is at least one place inlib/printf.cwhere GCC merges two consecutive char* accesses into one "strh" instruction, writing to a potentially unaligned address.
This can be reproduced by configuring kvm-unit-tests for kvmtool, but running it on QEMU, which triggers an early printf that exercises this particular code path.Add the -mstrict-align compiler option to the arm64 CFLAGS to fix this problem. Also add the respective -mno-unaligned-access flag for arm.
Thanks to Alexandru for helping debugging this.
Signed-off-by Andre Przywara <[email protected]>[1] ARMv8 ARM DDI 0487E.a, B2.5.2
[2] ARMv7 ARM DDI 0406C.d, A3.2.1--- arm/Makefile.arm | 1 + arm/Makefile.arm64 | 1 + 2 files changed, 2 insertions(+)arm/Makefile.arm +1
-0@@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y CFLAGS += $(machine) CFLAGS += -mcpu=$(PROCESSOR) +CFLAGS += -mno-unaligned-access arch_LDFLAGS = -Ttext=40010000arm/Makefile.arm64 +0
-0
arm/Makefile.arm64 bits = 64 · line 10
Quoted 4 lines
arm/Makefile.arm64 +1
-0@@ -7,6 +7,7 @@ bits = 64 ldarch = elf64-littleaarch64 arch_LDFLAGS = -pie -n +CFLAGS += -mstrict-align
Instead of adding it to both, Makefile.arm and Makefile.arm64, you could also simply add it to Makefile.common instead.
Thomas
On Tue, 10 Sep 2019 20:15:19 +0200
Thomas Huth <[email protected]> wrote:
Hi,
Quoted 51 lines · Andre Przywara
Quoted 50 lines
The ARM architecture requires all accesses to device memory to be naturally aligned[1][2]. Normal memory does not have this strict requirement, and in fact many systems do ignore unaligned accesses (by the means of clearing the A bit in SCTLR and accessing normal memory). So the default behaviour of GCC assumes that unaligned accesses are fine, at least if happening on the stack.
Now kvm-unit-tests runs some C code with the MMU off, which degrades the whole system memory to device memory. Now every unaligned access will fault, regardless of the A bit.
In fact there is at least one place inlib/printf.cwhere GCC merges two consecutive char* accesses into one "strh" instruction, writing to a potentially unaligned address.
This can be reproduced by configuring kvm-unit-tests for kvmtool, but running it on QEMU, which triggers an early printf that exercises this particular code path.Add the -mstrict-align compiler option to the arm64 CFLAGS to fix this problem. Also add the respective -mno-unaligned-access flag for arm.
Thanks to Alexandru for helping debugging this.
Signed-off-by Andre Przywara <[email protected]>[1] ARMv8 ARM DDI 0487E.a, B2.5.2
[2] ARMv7 ARM DDI 0406C.d, A3.2.1--- arm/Makefile.arm | 1 + arm/Makefile.arm64 | 1 + 2 files changed, 2 insertions(+)arm/Makefile.arm +1
-0@@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y CFLAGS += $(machine) CFLAGS += -mcpu=$(PROCESSOR) +CFLAGS += -mno-unaligned-access arch_LDFLAGS = -Ttext=40010000arm/Makefile.arm64 +0
-0@@ -7,6 +7,7 @@ bits = 64 ldarch = elf64-littleaarch64
Quoted 5 lines
arm/Makefile.arm64 bits = 64 · line 10
Quoted 2 lines
arm/Makefile.arm64 +1
-0arch_LDFLAGS = -pie -n +CFLAGS += -mstrict-alignInstead of adding it to both, Makefile.arm and Makefile.arm64, you could also simply add it to Makefile.common instead.
But the arguments are not the same (admittedly against intuition)?
I thought about defining arch_CFLAGS in both files, then adding that to Makefile.common, but didn't see the advantage over this straightforward approach here.
Cheers,
Andre.
Quoted 56 lines · Andre Przywara
On Tue, 10 Sep 2019 20:15:19 +0200
Thomas Huth <[email protected]> wrote:Hi,
Quoted 51 lines · Andre Przywara
Quoted 50 lines
The ARM architecture requires all accesses to device memory to be naturally aligned[1][2]. Normal memory does not have this strict requirement, and in fact many systems do ignore unaligned accesses (by the means of clearing the A bit in SCTLR and accessing normal memory). So the default behaviour of GCC assumes that unaligned accesses are fine, at least if happening on the stack.
Now kvm-unit-tests runs some C code with the MMU off, which degrades the whole system memory to device memory. Now every unaligned access will fault, regardless of the A bit.
In fact there is at least one place inlib/printf.cwhere GCC merges two consecutive char* accesses into one "strh" instruction, writing to a potentially unaligned address.
This can be reproduced by configuring kvm-unit-tests for kvmtool, but running it on QEMU, which triggers an early printf that exercises this particular code path.Add the -mstrict-align compiler option to the arm64 CFLAGS to fix this problem. Also add the respective -mno-unaligned-access flag for arm.
Thanks to Alexandru for helping debugging this.
Signed-off-by Andre Przywara <[email protected]>[1] ARMv8 ARM DDI 0487E.a, B2.5.2
[2] ARMv7 ARM DDI 0406C.d, A3.2.1--- arm/Makefile.arm | 1 + arm/Makefile.arm64 | 1 + 2 files changed, 2 insertions(+)arm/Makefile.arm +1
-0@@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y CFLAGS += $(machine) CFLAGS += -mcpu=$(PROCESSOR) +CFLAGS += -mno-unaligned-access arch_LDFLAGS = -Ttext=40010000arm/Makefile.arm64 +0
-0@@ -7,6 +7,7 @@ bits = 64 ldarch = elf64-littleaarch64
Quoted 8 lines
Quoted 5 lines
arm/Makefile.arm64 bits = 64 · line 10
Quoted 2 lines
arm/Makefile.arm64 +1
-0arch_LDFLAGS = -pie -n +CFLAGS += -mstrict-alignInstead of adding it to both, Makefile.arm and Makefile.arm64, you could also simply add it to Makefile.common instead.
But the arguments are not the same (admittedly against intuition)?
I thought about definingarch_CFLAGSin both files, then adding that to Makefile.common, but didn't see the advantage over this straightforward approach here.
D'oh, never mind, I didn't read the patch properly. I somehow thought that the arguments are the same. It's quite weird that the compiler developers chose different names here...
Thomas
Quoted 49 lines · Andre Przywara
The ARM architecture requires all accesses to device memory to be naturally aligned[1][2]. Normal memory does not have this strict requirement, and in fact many systems do ignore unaligned accesses (by the means of clearing the A bit in SCTLR and accessing normal memory). So the default behaviour of GCC assumes that unaligned accesses are fine, at least if happening on the stack.
Now kvm-unit-tests runs some C code with the MMU off, which degrades the whole system memory to device memory. Now every unaligned access will fault, regardless of the A bit.
In fact there is at least one place inlib/printf.cwhere GCC merges two consecutive char* accesses into one "strh" instruction, writing to a potentially unaligned address.
This can be reproduced by configuring kvm-unit-tests for kvmtool, but running it on QEMU, which triggers an early printf that exercises this particular code path.Add the -mstrict-align compiler option to the arm64 CFLAGS to fix this problem. Also add the respective -mno-unaligned-access flag for arm.
Thanks to Alexandru for helping debugging this.
Signed-off-by Andre Przywara <[email protected]>[1] ARMv8 ARM DDI 0487E.a, B2.5.2
[2] ARMv7 ARM DDI 0406C.d, A3.2.1--- arm/Makefile.arm | 1 + arm/Makefile.arm64 | 1 + 2 files changed, 2 insertions(+)arm/Makefile.arm +1
-0@@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y CFLAGS += $(machine) CFLAGS += -mcpu=$(PROCESSOR) +CFLAGS += -mno-unaligned-access arch_LDFLAGS = -Ttext=40010000arm/Makefile.arm64 +0
-0
arm/Makefile.arm64 bits = 64 · line 13
Quoted 7 lines
arm/Makefile.arm64 +1
-0@@ -7,6 +7,7 @@ bits = 64 ldarch = elf64-littleaarch64 arch_LDFLAGS = -pie -n +CFLAGS += -mstrict-align define arch_elf_check = $(if $(shell ! $(OBJDUMP) -R $(1) >&/dev/null && echo "nok"),
FWIW (after finally reading the patch properly ;-)) :