Skip to contentSkip to conversation

x86: stack alignment for boot code and clang

PATCH Superseded Matthias Kaehlcke 4 participants 14 messages Jun 13, 2017 – Jun 14, 2017

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover x86: stack alignment for boot code and clang

This series fixes an issue with the stack of the x86 boot code not being aligned as intended. Further it adapts the Makefile to account for the fact that clang uses a different option to configure the stack alignment than gcc (-mstack-alignment=N vs -mpreferred-stack-boundary=N)

Collaterally the series adds the new kbuild macros cc-option-raw and hostcc-option, it also reworks cc-option to make use of cc-option-raw.

Matthias Kaehlcke (4):
kbuild: Add cc-option-raw macro
kbuild: Add hostcc-option macro
x86/build: Use cc-option-raw for boot code compiler options x86/build: Specify stack alignment for clang

 Makefile               |  2 +-
 arch/x86/Makefile      | 33 +++++++++++++++++++++++++--------
 scripts/Kbuild.include | 13 +++++++++++--
 3 files changed, 37 insertions(+), 11 deletions(-)
Signature · 1 line
2.13.1.518.g3df882009-goog
#1 lore raw

PATCH v2 1/4 kbuild: Add cc-option-raw macro

cc-option uses KBUILD_CFLAGS and KBUILD_CPPFLAGS when it determines whether an option is supported or not. This is fine for options used to build the kernel itself, however some components like the x86 boot code use a different set of flags.

Add the new macro cc-option-raw which serves the same purpose as cc-option but has additional parameters. One parameter is the compiler with which the check should be performed, the other the compiler options to be used instead KBUILD_C*FLAGS. The compiler parameter allows other macros like hostcc-option to be implemented on top of cc-option-raw.

Also rework cc-option to make use of cc-option-raw.

---
Changes in v2:
- Changed macro name from Add cc-option-no-kbuild to cc-option-raw
- Added compiler as parameter to the macro
- Reworked cc-option to make use of cc-option-raw
- Updated commit message

 scripts/Kbuild.include | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
scripts/Kbuild.include +7 -2
index 61f87a99bf0a..6dd0d1a921d6 100644
@@ -108,6 +108,11 @@ as-option = $(call try-run,\
 as-instr = $(call try-run,\
 	printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3))
 
+# cc-option-raw
+# Usage: MY_CFLAGS += $(call cc-option-raw,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
+cc-option-raw = $(call try-run,\
+	$(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
+
 # Do not attempt to build with gcc plugins during cc-option tests.
 # (And this uses delayed resolution so the flags will be up to date.)
 CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
@@ -115,8 +120,8 @@ CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
 # cc-option
 # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
 
-cc-option = $(call try-run,\
-	$(CC) -Werror $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+cc-option = $(call cc-option-raw, $(CC), $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS),\
+	$(1), $(2))
 
 # cc-option-yn
 # Usage: flag := $(call cc-option-yn,-march=winchip-c6)
Signature · 1 line
2.13.1.518.g3df882009-goog
Acked-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] kbuild: Add cc-option-raw macro

2017-06-14 8:08 GMT+09:00 Matthias Kaehlcke <[email protected]>:

Quoted 23 lines

cc-option uses KBUILD_CFLAGS and KBUILD_CPPFLAGS when it determines whether an option is supported or not. This is fine for options used to build the kernel itself, however some components like the x86 boot code use a different set of flags.

Add the new macro cc-option-raw which serves the same purpose as cc-option but has additional parameters. One parameter is the compiler with which the check should be performed, the other the compiler options to be used instead KBUILD_C*FLAGS. The compiler parameter allows other macros like hostcc-option to be implemented on top of cc-option-raw.

Also rework cc-option to make use of cc-option-raw.

---
Changes in v2:
- Changed macro name from Add cc-option-no-kbuild to cc-option-raw
- Added compiler as parameter to the macro
- Reworked cc-option to make use of cc-option-raw
- Updated commit message

Quoted 3 lines
 scripts/Kbuild.include | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

(if nothing better pops up)

Signature · 2 lines
Best Regards
Masahiro Yamada
Acked-by
#3 lore raw

Re: [PATCH v2 1/4] kbuild: Add cc-option-raw macro

Quoted 24 lines · Masahiro Yamada

2017-06-14 8:08 GMT+09:00 Matthias Kaehlcke <[email protected]>:

Quoted 23 lines

cc-option uses KBUILD_CFLAGS and KBUILD_CPPFLAGS when it determines whether an option is supported or not. This is fine for options used to build the kernel itself, however some components like the x86 boot code use a different set of flags.

Add the new macro cc-option-raw which serves the same purpose as cc-option but has additional parameters. One parameter is the compiler with which the check should be performed, the other the compiler options to be used instead KBUILD_C*FLAGS. The compiler parameter allows other macros like hostcc-option to be implemented on top of cc-option-raw.

Also rework cc-option to make use of cc-option-raw.

---
Changes in v2:
- Changed macro name from Add cc-option-no-kbuild to cc-option-raw
- Added compiler as parameter to the macro
- Reworked cc-option to make use of cc-option-raw
- Updated commit message

Quoted 6 lines
Quoted 3 lines
 scripts/Kbuild.include | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

(if nothing better pops up)

Regarding the naming, __cc-option might be better than cc-option-raw, but the current version is fine too.

Arnd

#4 lore raw

Re: [PATCH v2 1/4] kbuild: Add cc-option-raw macro

Hi Arnd,

2017-06-14 16:31 GMT+09:00 Arnd Bergmann <[email protected]>:

Quoted 30 lines
Quoted 28 lines · Masahiro Yamada

2017-06-14 8:08 GMT+09:00 Matthias Kaehlcke <[email protected]>:

Quoted 26 lines

cc-option uses KBUILD_CFLAGS and KBUILD_CPPFLAGS when it determines whether an option is supported or not. This is fine for options used to build the kernel itself, however some components like the x86 boot code use a different set of flags.

Add the new macro cc-option-raw which serves the same purpose as cc-option but has additional parameters. One parameter is the compiler with which the check should be performed, the other the compiler options to be used instead KBUILD_C*FLAGS. The compiler parameter allows other macros like hostcc-option to be implemented on top of cc-option-raw.

Also rework cc-option to make use of cc-option-raw.

---
Changes in v2:
- Changed macro name from Add cc-option-no-kbuild to cc-option-raw
- Added compiler as parameter to the macro
- Reworked cc-option to make use of cc-option-raw
- Updated commit message

 scripts/Kbuild.include | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
Quoted 7 lines
Quoted 2 lines

(if nothing better pops up)

Regarding the naming, __cc-option might be better than cc-option-raw, but the current version is fine too.

Thank you for your suggestion.

Yes, double-underscore prefix is often used in the kernel coding, so I like your idea.

I will leave it up to Matthias, though.
Either will be fine for me.

Thanks!

Signature · 2 lines
Best Regards
Masahiro Yamada
Acked-by
#5 lore raw

Re: [PATCH v2 1/4] kbuild: Add cc-option-raw macro

In reply to Arnd Bergmann

Dne 14.6.2017 v 09:31 Arnd Bergmann napsal(a):

Quoted 26 lines
Quoted 24 lines · Masahiro Yamada

2017-06-14 8:08 GMT+09:00 Matthias Kaehlcke <[email protected]>:

Quoted 23 lines

cc-option uses KBUILD_CFLAGS and KBUILD_CPPFLAGS when it determines whether an option is supported or not. This is fine for options used to build the kernel itself, however some components like the x86 boot code use a different set of flags.

Add the new macro cc-option-raw which serves the same purpose as cc-option but has additional parameters. One parameter is the compiler with which the check should be performed, the other the compiler options to be used instead KBUILD_C*FLAGS. The compiler parameter allows other macros like hostcc-option to be implemented on top of cc-option-raw.

Also rework cc-option to make use of cc-option-raw.

---
Changes in v2:
- Changed macro name from Add cc-option-no-kbuild to cc-option-raw
- Added compiler as parameter to the macro
- Reworked cc-option to make use of cc-option-raw
- Updated commit message

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
 scripts/Kbuild.include | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

(if nothing better pops up)

Quoted 2 lines

Regarding the naming, __cc-option might be better than cc-option-raw, but the current version is fine too.

I have no strong opinion either way :).

Michal

#6 lore raw

Re: [PATCH v2 1/4] kbuild: Add cc-option-raw macro

El Wed, Jun 14, 2017 at 10:32:20AM +0200 Michal Marek ha dit:

Quoted 36 lines

Dne 14.6.2017 v 09:31 Arnd Bergmann napsal(a):

Quoted 26 lines
Quoted 24 lines · Masahiro Yamada

2017-06-14 8:08 GMT+09:00 Matthias Kaehlcke <[email protected]>:

Quoted 23 lines

cc-option uses KBUILD_CFLAGS and KBUILD_CPPFLAGS when it determines whether an option is supported or not. This is fine for options used to build the kernel itself, however some components like the x86 boot code use a different set of flags.

Add the new macro cc-option-raw which serves the same purpose as cc-option but has additional parameters. One parameter is the compiler with which the check should be performed, the other the compiler options to be used instead KBUILD_C*FLAGS. The compiler parameter allows other macros like hostcc-option to be implemented on top of cc-option-raw.

Also rework cc-option to make use of cc-option-raw.

---
Changes in v2:
- Changed macro name from Add cc-option-no-kbuild to cc-option-raw
- Added compiler as parameter to the macro
- Reworked cc-option to make use of cc-option-raw
- Updated commit message

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
 scripts/Kbuild.include | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

(if nothing better pops up)

Quoted 7 lines
Quoted 2 lines

Regarding the naming, __cc-option might be better than cc-option-raw, but the current version is fine too.

I have no strong opinion either way :).

I'm going to change it to __cc-option since there is a slight preference for it and I have to respin the series anyway. I'll wait a bit for feedback from the x86 folks before sending the new version.

#7 lore raw

PATCH v2 2/4 kbuild: Add hostcc-option macro

In reply to Matthias Kaehlcke

hostcc-option is equivalent to cc-option, but uses the host compiler and HOSTCFLAGS. Change HOSTCFLAGS to a simple expanded variable to allow for HOSTCFLAGS += $(call hostcc-option, ...).

---
Changes in v2:
- Patch added in v2

 Makefile               | 2 +-
 scripts/Kbuild.include | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)
Makefile +1 -1
index 83f6d9972cab..b234bba6d652 100644
@@ -303,7 +303,7 @@ CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
 
 HOSTCC       = gcc
 HOSTCXX      = g++
-HOSTCFLAGS   = -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 -fomit-frame-pointer -std=gnu89
+HOSTCFLAGS   := -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 -fomit-frame-pointer -std=gnu89
 HOSTCXXFLAGS = -O2
 
 ifeq ($(shell $(HOSTCC) -v 2>&1 | grep -c "clang version"), 1)
scripts/Kbuild.include +4 -0
index 6dd0d1a921d6..4965ed92fc1e 100644
@@ -123,6 +123,10 @@ CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
 cc-option = $(call cc-option-raw, $(CC), $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS),\
 	$(1), $(2))
 
+# hostcc-option
+# Usage: HOSTCFLAGS += $(call hostcc-option, -fno-delete-null-pointer-checks,)
+hostcc-option = $(call cc-option-raw, $(HOSTCC), $(HOSTCFLAGS), $(1), $(2))
+
 # cc-option-yn
 # Usage: flag := $(call cc-option-yn,-march=winchip-c6)
 cc-option-yn = $(call try-run,\
Signature · 1 line
2.13.1.518.g3df882009-goog
Acked-by on v2 2/4
#8 lore raw

Re: [PATCH v2 2/4] kbuild: Add hostcc-option macro

2017-06-14 8:08 GMT+09:00 Matthias Kaehlcke <[email protected]>:

Quoted 8 lines

hostcc-option is equivalent to cc-option, but uses the host compiler and HOSTCFLAGS. Change HOSTCFLAGS to a simple expanded variable to allow for HOSTCFLAGS += $(call hostcc-option, ...).

---

Signature · 2 lines
Best Regards
Masahiro Yamada
#9 lore raw

Re: [PATCH v2 2/4] kbuild: Add hostcc-option macro

In reply to Matthias Kaehlcke
Quoted 7 lines · Matthias Kaehlcke

hostcc-option is equivalent to cc-option, but uses the host compiler and HOSTCFLAGS. Change HOSTCFLAGS to a simple expanded variable to allow for HOSTCFLAGS += $(call hostcc-option, ...).

Actually, we already have a hostcc-option macro. What I meant with my suggestion was that we could redefine the existing one using cc-option-raw.

I checked again now and found that while this was added in 2016 by Emese Revfy, we apparently don't have any users of the helper in the kernel.

Arnd

#10 lore raw

Re: [PATCH v2 2/4] kbuild: Add hostcc-option macro

Hi Arnd,

2017-06-14 16:26 GMT+09:00 Arnd Bergmann <[email protected]>:

Quoted 9 lines · Matthias Kaehlcke
Quoted 7 lines

hostcc-option is equivalent to cc-option, but uses the host compiler and HOSTCFLAGS. Change HOSTCFLAGS to a simple expanded variable to allow for HOSTCFLAGS += $(call hostcc-option, ...).

Quoted 6 lines

Actually, we already have a hostcc-option macro. What I meant with my suggestion was that we could redefine the existing one using cc-option-raw.

I checked again now and found that while this was added in 2016 by Emese Revfy, we apparently don't have any users of the helper in the kernel.

Oops, I missed it because
it is defined in scripts/Makefile.host instead of scripts/Kbuild.include

$(call cc-option, ...) is generally used in the top Makefile (or arch/*/Makefile).

However, scripts/Makefile.host is not included from the top Makefile, so $(call hostcc-option, ...) is available only under sub-directories. I guess that is why we had no user of it.

I think the right thing to do
is to remove the former implementation from Makefile.host

Thanks!

Signature · 2 lines
Best Regards
Masahiro Yamada
#11 lore raw

PATCH v2 3/4 x86/build: Use cc-option-raw for boot code compiler options

In reply to Matthias Kaehlcke

cc-option uses KBUILD_CFLAGS and KBUILD_CPPFLAGS when checking if a compiler option is available. These flags aren't used to build the boot code, in consequence cc-option can yield wrong results. For example -mpreferred-stack-boundary=2 is never set with a 64 bit compiler, since the setting is only valid for 16 and 32 bit binaries. This is also the case for 32 bit kernel builds, because the option -m32 is added to KBUILD_CFLAGS after the assignment of REALMODE_CFLAGS.

Use cc-option-raw instead of cc-option for the boot mode options. The macro receives the compiler options as parameter instead of using KBUILD_C*FLAGS. For the boot code we pass REALMODE_CFLAGS.

Also use separate statements for the cc-option-raw checks instead of performing them in the initial assignment of REALMODE_CFLAGS since the variable is an input of the macro.

---
Changes in v2:
- Adapted to use kbuild macro cc-option-raw instead of cc-option-no-kbuild

 arch/x86/Makefile | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/Makefile +5 -4
index bf240b920473..6d3ae4b20a5b 100644
@@ -24,10 +24,11 @@ REALMODE_CFLAGS	:= $(M16_CFLAGS) -g -Os -D__KERNEL__ \
 		   -DDISABLE_BRANCH_PROFILING \
 		   -Wall -Wstrict-prototypes -march=i386 -mregparm=3 \
 		   -fno-strict-aliasing -fomit-frame-pointer -fno-pic \
-		   -mno-mmx -mno-sse \
-		   $(call cc-option, -ffreestanding) \
-		   $(call cc-option, -fno-stack-protector) \
-		   $(call cc-option, -mpreferred-stack-boundary=2)
+		   -mno-mmx -mno-sse
+
+REALMODE_CFLAGS += $(call cc-option-raw, $(CC), $(REALMODE_CFLAGS), -ffreestanding)
+REALMODE_CFLAGS += $(call cc-option-raw, $(CC), $(REALMODE_CFLAGS), -fno-stack-protector)
+REALMODE_CFLAGS += $(call cc-option-raw, $(CC), $(REALMODE_CFLAGS), -mpreferred-stack-boundary=2)
 export REALMODE_CFLAGS
 
 # BITS is used as extension for files which are available in a 32 bit
Signature · 1 line
2.13.1.518.g3df882009-goog
#12 lore raw

PATCH v2 4/4 x86/build: Specify stack alignment for clang

In reply to Matthias Kaehlcke

For gcc stack alignment is configured with -mpreferred-stack-boundary=N, clang has the option -mstack-alignment=N for that purpose. Use the same alignment as with gcc.

If the alignment is not specified clang assumes an alignment of 16 bytes, as required by the standard ABI. However as mentioned in d9b0cde91c60 ("x86-64, gcc: Use -mpreferred-stack-boundary=3 if supported") the standard kernel entry on x86-64 leaves the stack on an 8-byte boundary, as a consequence clang will keep the stack misaligned.

---
Changes in v2:
- Changed option name to cc_stack_align_opt
- Updated comments about stack alignment

 arch/x86/Makefile | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
arch/x86/Makefile +21 -5
index 6d3ae4b20a5b..9c3d13999dd3 100644
@@ -11,6 +11,14 @@ else
         KBUILD_DEFCONFIG := $(ARCH)_defconfig
 endif
 
+# Handle different option names for specifying stack alignment with gcc and
+# clang.
+ifeq ($(cc-name),clang)
+	cc_stack_align_opt := -mstack-alignment
+else
+	cc_stack_align_opt := -mpreferred-stack-boundary
+endif
+
 # How to compile the 16-bit code.  Note we always compile for -march=i386;
 # that way we can complain to the user if the CPU is insufficient.
 #
@@ -28,7 +36,7 @@ REALMODE_CFLAGS	:= $(M16_CFLAGS) -g -Os -D__KERNEL__ \
 
 REALMODE_CFLAGS += $(call cc-option-raw, $(CC), $(REALMODE_CFLAGS), -ffreestanding)
 REALMODE_CFLAGS += $(call cc-option-raw, $(CC), $(REALMODE_CFLAGS), -fno-stack-protector)
-REALMODE_CFLAGS += $(call cc-option-raw, $(CC), $(REALMODE_CFLAGS), -mpreferred-stack-boundary=2)
+REALMODE_CFLAGS += $(call cc-option-raw, $(CC), $(REALMODE_CFLAGS), $(cc_stack_align_opt)=2)
 export REALMODE_CFLAGS
 
 # BITS is used as extension for files which are available in a 32 bit
@@ -65,8 +73,10 @@ ifeq ($(CONFIG_X86_32),y)
         # with nonstandard options
         KBUILD_CFLAGS += -fno-pic
 
-        # prevent gcc from keeping the stack 16 byte aligned
-        KBUILD_CFLAGS += $(call cc-option,-mpreferred-stack-boundary=2)
+        # Align the stack to the register width instead of using the default
+        # alignment of 16 bytes. This reduces stack usage and the number of
+        # alignment instructions.
+        KBUILD_CFLAGS += $(call cc-option,$(cc_stack_align_opt)=2)
 
         # Disable unit-at-a-time mode on pre-gcc-4.0 compilers, it makes gcc use
         # a lot more stack due to the lack of sharing of stacklots:
@@ -98,8 +108,14 @@ else
         KBUILD_CFLAGS += $(call cc-option,-mno-80387)
         KBUILD_CFLAGS += $(call cc-option,-mno-fp-ret-in-387)
 
-	# Use -mpreferred-stack-boundary=3 if supported.
-	KBUILD_CFLAGS += $(call cc-option,-mpreferred-stack-boundary=3)
+        # By default gcc and clang use a stack alignment of 16 bytes for x86.
+        # However the standard kernel entry on x86-64 leaves the stack on an
+        # 8-byte boundary. If the compiler isn't informed about the actual
+        # alignment it will generate extra alignment instructions for the
+        # default alignment which keep the stack *mis*aligned.
+        # Furthermore an alignment to the register width reduces stack usage
+        # and the number of alignment instructions.
+        KBUILD_CFLAGS += $(call cc-option,$(cc_stack_align_opt)=3)
 
 	# Use -mskip-rax-setup if supported.
 	KBUILD_CFLAGS += $(call cc-option,-mskip-rax-setup)
Signature · 1 line
2.13.1.518.g3df882009-goog
#13 lore raw
In reply to Matthias Kaehlcke

2017-06-14 8:08 GMT+09:00 Matthias Kaehlcke <[email protected]>:

Quoted 15 lines

This series fixes an issue with the stack of the x86 boot code not being aligned as intended. Further it adapts the Makefile to account for the fact that clang uses a different option to configure the stack alignment than gcc (-mstack-alignment=N vs -mpreferred-stack-boundary=N)

Collaterally the series adds the new kbuild macros cc-option-raw and hostcc-option, it also reworks cc-option to make use of cc-option-raw.

Matthias Kaehlcke (4):
kbuild: Add cc-option-raw macro
kbuild: Add hostcc-option macro
x86/build: Use cc-option-raw for boot code compiler options x86/build: Specify stack alignment for clang

Quoted 4 lines
 Makefile               |  2 +-
 arch/x86/Makefile      | 33 +++++++++++++++++++++++++--------
 scripts/Kbuild.include | 13 +++++++++++--
 3 files changed, 37 insertions(+), 11 deletions(-)

I gave Acked-by to 1/4 and 2/4
so that the whole series can go to x86 tree.

Thanks!

Signature · 2 lines
Best Regards
Masahiro Yamada