All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
@ 2019-09-05 17:15 ` Andre Przywara
  0 siblings, 0 replies; 14+ messages in thread
From: Andre Przywara @ 2019-09-05 17:15 UTC (permalink / raw)
  To: Andrew Jones; +Cc: kvmarm, kvm, Alexandru Elisei, Paolo Bonzini

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.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>

[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(+)

diff --git a/arm/Makefile.arm b/arm/Makefile.arm
index a625267..43b4be1 100644
--- a/arm/Makefile.arm
+++ b/arm/Makefile.arm
@@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
 
 CFLAGS += $(machine)
 CFLAGS += -mcpu=$(PROCESSOR)
+CFLAGS += -mno-unaligned-access
 
 arch_LDFLAGS = -Ttext=40010000
 
diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
index 02c24e8..35de5ea 100644
--- a/arm/Makefile.arm64
+++ b/arm/Makefile.arm64
@@ -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"),
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
@ 2019-09-05 17:15 ` Andre Przywara
  0 siblings, 0 replies; 14+ messages in thread
From: Andre Przywara @ 2019-09-05 17:15 UTC (permalink / raw)
  To: Andrew Jones; +Cc: Paolo Bonzini, kvmarm, kvm

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.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>

[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(+)

diff --git a/arm/Makefile.arm b/arm/Makefile.arm
index a625267..43b4be1 100644
--- a/arm/Makefile.arm
+++ b/arm/Makefile.arm
@@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
 
 CFLAGS += $(machine)
 CFLAGS += -mcpu=$(PROCESSOR)
+CFLAGS += -mno-unaligned-access
 
 arch_LDFLAGS = -Ttext=40010000
 
diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
index 02c24e8..35de5ea 100644
--- a/arm/Makefile.arm64
+++ b/arm/Makefile.arm64
@@ -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"),
-- 
2.17.1

_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
  2019-09-05 17:15 ` Andre Przywara
@ 2019-09-06  6:30   ` Andrew Jones
  -1 siblings, 0 replies; 14+ messages in thread
From: Andrew Jones @ 2019-09-06  6:30 UTC (permalink / raw)
  To: Andre Przywara; +Cc: kvmarm, kvm, Alexandru Elisei, Paolo Bonzini

On Thu, Sep 05, 2019 at 06:15:02PM +0100, Andre Przywara wrote:
> 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.
> 
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> 
> [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(+)
> 
> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> index a625267..43b4be1 100644
> --- a/arm/Makefile.arm
> +++ b/arm/Makefile.arm
> @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
>  
>  CFLAGS += $(machine)
>  CFLAGS += -mcpu=$(PROCESSOR)
> +CFLAGS += -mno-unaligned-access
>  
>  arch_LDFLAGS = -Ttext=40010000
>  
> diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
> index 02c24e8..35de5ea 100644
> --- a/arm/Makefile.arm64
> +++ b/arm/Makefile.arm64
> @@ -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"),
> -- 
> 2.17.1
>

Reviewed-by: Andrew Jones <drjones@redhat.com>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
@ 2019-09-06  6:30   ` Andrew Jones
  0 siblings, 0 replies; 14+ messages in thread
From: Andrew Jones @ 2019-09-06  6:30 UTC (permalink / raw)
  To: Andre Przywara; +Cc: Paolo Bonzini, kvmarm, kvm

On Thu, Sep 05, 2019 at 06:15:02PM +0100, Andre Przywara wrote:
> 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.
> 
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> 
> [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(+)
> 
> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> index a625267..43b4be1 100644
> --- a/arm/Makefile.arm
> +++ b/arm/Makefile.arm
> @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
>  
>  CFLAGS += $(machine)
>  CFLAGS += -mcpu=$(PROCESSOR)
> +CFLAGS += -mno-unaligned-access
>  
>  arch_LDFLAGS = -Ttext=40010000
>  
> diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
> index 02c24e8..35de5ea 100644
> --- a/arm/Makefile.arm64
> +++ b/arm/Makefile.arm64
> @@ -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"),
> -- 
> 2.17.1
>

Reviewed-by: Andrew Jones <drjones@redhat.com>
_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
  2019-09-05 17:15 ` Andre Przywara
@ 2019-09-10 17:07   ` Paolo Bonzini
  -1 siblings, 0 replies; 14+ messages in thread
From: Paolo Bonzini @ 2019-09-10 17:07 UTC (permalink / raw)
  To: Andre Przywara, Andrew Jones; +Cc: kvmarm, kvm, Alexandru Elisei

On 05/09/19 19:15, Andre Przywara wrote:
> 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.
> 
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> 
> [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(+)
> 
> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> index a625267..43b4be1 100644
> --- a/arm/Makefile.arm
> +++ b/arm/Makefile.arm
> @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
>  
>  CFLAGS += $(machine)
>  CFLAGS += -mcpu=$(PROCESSOR)
> +CFLAGS += -mno-unaligned-access
>  
>  arch_LDFLAGS = -Ttext=40010000
>  
> diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
> index 02c24e8..35de5ea 100644
> --- a/arm/Makefile.arm64
> +++ b/arm/Makefile.arm64
> @@ -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

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
@ 2019-09-10 17:07   ` Paolo Bonzini
  0 siblings, 0 replies; 14+ messages in thread
From: Paolo Bonzini @ 2019-09-10 17:07 UTC (permalink / raw)
  To: Andre Przywara, Andrew Jones; +Cc: kvmarm, kvm

On 05/09/19 19:15, Andre Przywara wrote:
> 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.
> 
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> 
> [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(+)
> 
> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> index a625267..43b4be1 100644
> --- a/arm/Makefile.arm
> +++ b/arm/Makefile.arm
> @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
>  
>  CFLAGS += $(machine)
>  CFLAGS += -mcpu=$(PROCESSOR)
> +CFLAGS += -mno-unaligned-access
>  
>  arch_LDFLAGS = -Ttext=40010000
>  
> diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
> index 02c24e8..35de5ea 100644
> --- a/arm/Makefile.arm64
> +++ b/arm/Makefile.arm64
> @@ -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
_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
  2019-09-05 17:15 ` Andre Przywara
@ 2019-09-10 18:15   ` Thomas Huth
  -1 siblings, 0 replies; 14+ messages in thread
From: Thomas Huth @ 2019-09-10 18:15 UTC (permalink / raw)
  To: Andre Przywara, Andrew Jones; +Cc: kvmarm, kvm, Alexandru Elisei, Paolo Bonzini

On 05/09/2019 19.15, Andre Przywara wrote:
> 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.
> 
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> 
> [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(+)
> 
> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> index a625267..43b4be1 100644
> --- a/arm/Makefile.arm
> +++ b/arm/Makefile.arm
> @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
>  
>  CFLAGS += $(machine)
>  CFLAGS += -mcpu=$(PROCESSOR)
> +CFLAGS += -mno-unaligned-access
>  
>  arch_LDFLAGS = -Ttext=40010000
>  
> diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
> index 02c24e8..35de5ea 100644
> --- a/arm/Makefile.arm64
> +++ b/arm/Makefile.arm64
> @@ -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

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
@ 2019-09-10 18:15   ` Thomas Huth
  0 siblings, 0 replies; 14+ messages in thread
From: Thomas Huth @ 2019-09-10 18:15 UTC (permalink / raw)
  To: Andre Przywara, Andrew Jones; +Cc: Paolo Bonzini, kvmarm, kvm

On 05/09/2019 19.15, Andre Przywara wrote:
> 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.
> 
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> 
> [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(+)
> 
> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> index a625267..43b4be1 100644
> --- a/arm/Makefile.arm
> +++ b/arm/Makefile.arm
> @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
>  
>  CFLAGS += $(machine)
>  CFLAGS += -mcpu=$(PROCESSOR)
> +CFLAGS += -mno-unaligned-access
>  
>  arch_LDFLAGS = -Ttext=40010000
>  
> diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
> index 02c24e8..35de5ea 100644
> --- a/arm/Makefile.arm64
> +++ b/arm/Makefile.arm64
> @@ -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
_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
  2019-09-10 18:15   ` Thomas Huth
@ 2019-09-11  8:16     ` Andre Przywara
  -1 siblings, 0 replies; 14+ messages in thread
From: Andre Przywara @ 2019-09-11  8:16 UTC (permalink / raw)
  To: Thomas Huth; +Cc: Andrew Jones, kvmarm, kvm, Alexandru Elisei, Paolo Bonzini

On Tue, 10 Sep 2019 20:15:19 +0200
Thomas Huth <thuth@redhat.com> wrote:

Hi,

> On 05/09/2019 19.15, Andre Przywara wrote:
> > 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.
> > 
> > Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> > 
> > [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(+)
> > 
> > diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> > index a625267..43b4be1 100644
> > --- a/arm/Makefile.arm
> > +++ b/arm/Makefile.arm
> > @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
> >  
> >  CFLAGS += $(machine)
> >  CFLAGS += -mcpu=$(PROCESSOR)
> > +CFLAGS += -mno-unaligned-access
> >  
> >  arch_LDFLAGS = -Ttext=40010000
> >  
> > diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
> > index 02c24e8..35de5ea 100644
> > --- a/arm/Makefile.arm64
> > +++ b/arm/Makefile.arm64
> > @@ -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.

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.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
@ 2019-09-11  8:16     ` Andre Przywara
  0 siblings, 0 replies; 14+ messages in thread
From: Andre Przywara @ 2019-09-11  8:16 UTC (permalink / raw)
  To: Thomas Huth; +Cc: Paolo Bonzini, kvmarm, kvm

On Tue, 10 Sep 2019 20:15:19 +0200
Thomas Huth <thuth@redhat.com> wrote:

Hi,

> On 05/09/2019 19.15, Andre Przywara wrote:
> > 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.
> > 
> > Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> > 
> > [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(+)
> > 
> > diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> > index a625267..43b4be1 100644
> > --- a/arm/Makefile.arm
> > +++ b/arm/Makefile.arm
> > @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
> >  
> >  CFLAGS += $(machine)
> >  CFLAGS += -mcpu=$(PROCESSOR)
> > +CFLAGS += -mno-unaligned-access
> >  
> >  arch_LDFLAGS = -Ttext=40010000
> >  
> > diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
> > index 02c24e8..35de5ea 100644
> > --- a/arm/Makefile.arm64
> > +++ b/arm/Makefile.arm64
> > @@ -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.

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.
_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
  2019-09-11  8:16     ` Andre Przywara
@ 2019-09-11  8:19       ` Thomas Huth
  -1 siblings, 0 replies; 14+ messages in thread
From: Thomas Huth @ 2019-09-11  8:19 UTC (permalink / raw)
  To: Andre Przywara; +Cc: Andrew Jones, kvmarm, kvm, Alexandru Elisei, Paolo Bonzini

On 11/09/2019 10.16, Andre Przywara wrote:
> On Tue, 10 Sep 2019 20:15:19 +0200
> Thomas Huth <thuth@redhat.com> wrote:
> 
> Hi,
> 
>> On 05/09/2019 19.15, Andre Przywara wrote:
>>> 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.
>>>
>>> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
>>>
>>> [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(+)
>>>
>>> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
>>> index a625267..43b4be1 100644
>>> --- a/arm/Makefile.arm
>>> +++ b/arm/Makefile.arm
>>> @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
>>>  
>>>  CFLAGS += $(machine)
>>>  CFLAGS += -mcpu=$(PROCESSOR)
>>> +CFLAGS += -mno-unaligned-access
>>>  
>>>  arch_LDFLAGS = -Ttext=40010000
>>>  
>>> diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
>>> index 02c24e8..35de5ea 100644
>>> --- a/arm/Makefile.arm64
>>> +++ b/arm/Makefile.arm64
>>> @@ -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.
> 
> 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.

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

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
@ 2019-09-11  8:19       ` Thomas Huth
  0 siblings, 0 replies; 14+ messages in thread
From: Thomas Huth @ 2019-09-11  8:19 UTC (permalink / raw)
  To: Andre Przywara; +Cc: Paolo Bonzini, kvmarm, kvm

On 11/09/2019 10.16, Andre Przywara wrote:
> On Tue, 10 Sep 2019 20:15:19 +0200
> Thomas Huth <thuth@redhat.com> wrote:
> 
> Hi,
> 
>> On 05/09/2019 19.15, Andre Przywara wrote:
>>> 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.
>>>
>>> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
>>>
>>> [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(+)
>>>
>>> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
>>> index a625267..43b4be1 100644
>>> --- a/arm/Makefile.arm
>>> +++ b/arm/Makefile.arm
>>> @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
>>>  
>>>  CFLAGS += $(machine)
>>>  CFLAGS += -mcpu=$(PROCESSOR)
>>> +CFLAGS += -mno-unaligned-access
>>>  
>>>  arch_LDFLAGS = -Ttext=40010000
>>>  
>>> diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
>>> index 02c24e8..35de5ea 100644
>>> --- a/arm/Makefile.arm64
>>> +++ b/arm/Makefile.arm64
>>> @@ -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.
> 
> 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.

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
_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
  2019-09-05 17:15 ` Andre Przywara
@ 2019-09-11  8:24   ` Thomas Huth
  -1 siblings, 0 replies; 14+ messages in thread
From: Thomas Huth @ 2019-09-11  8:24 UTC (permalink / raw)
  To: Andre Przywara, Andrew Jones; +Cc: kvmarm, kvm, Alexandru Elisei, Paolo Bonzini

On 05/09/2019 19.15, Andre Przywara wrote:
> 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.
> 
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> 
> [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(+)
> 
> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> index a625267..43b4be1 100644
> --- a/arm/Makefile.arm
> +++ b/arm/Makefile.arm
> @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
>  
>  CFLAGS += $(machine)
>  CFLAGS += -mcpu=$(PROCESSOR)
> +CFLAGS += -mno-unaligned-access
>  
>  arch_LDFLAGS = -Ttext=40010000
>  
> diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
> index 02c24e8..35de5ea 100644
> --- a/arm/Makefile.arm64
> +++ b/arm/Makefile.arm64
> @@ -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 ;-)) :
Reviewed-by: Thomas Huth <thuth@redhat.com>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses
@ 2019-09-11  8:24   ` Thomas Huth
  0 siblings, 0 replies; 14+ messages in thread
From: Thomas Huth @ 2019-09-11  8:24 UTC (permalink / raw)
  To: Andre Przywara, Andrew Jones; +Cc: Paolo Bonzini, kvmarm, kvm

On 05/09/2019 19.15, Andre Przywara wrote:
> 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.
> 
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> 
> [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(+)
> 
> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> index a625267..43b4be1 100644
> --- a/arm/Makefile.arm
> +++ b/arm/Makefile.arm
> @@ -12,6 +12,7 @@ KEEP_FRAME_POINTER := y
>  
>  CFLAGS += $(machine)
>  CFLAGS += -mcpu=$(PROCESSOR)
> +CFLAGS += -mno-unaligned-access
>  
>  arch_LDFLAGS = -Ttext=40010000
>  
> diff --git a/arm/Makefile.arm64 b/arm/Makefile.arm64
> index 02c24e8..35de5ea 100644
> --- a/arm/Makefile.arm64
> +++ b/arm/Makefile.arm64
> @@ -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 ;-)) :
Reviewed-by: Thomas Huth <thuth@redhat.com>
_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2019-09-11  8:24 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-05 17:15 [PATCH kvm-unit-tests] arm: prevent compiler from using unaligned accesses Andre Przywara
2019-09-05 17:15 ` Andre Przywara
2019-09-06  6:30 ` Andrew Jones
2019-09-06  6:30   ` Andrew Jones
2019-09-10 17:07 ` Paolo Bonzini
2019-09-10 17:07   ` Paolo Bonzini
2019-09-10 18:15 ` Thomas Huth
2019-09-10 18:15   ` Thomas Huth
2019-09-11  8:16   ` Andre Przywara
2019-09-11  8:16     ` Andre Przywara
2019-09-11  8:19     ` Thomas Huth
2019-09-11  8:19       ` Thomas Huth
2019-09-11  8:24 ` Thomas Huth
2019-09-11  8:24   ` Thomas Huth

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.