x86/hweight: Get rid of the special calling convention
Subsystems (3)
Borislav Petkov
maintainerPATCH 1/1 x86/hweight: Get rid of the special calling convention
From: Borislav Petkov <[email protected]>
People complained about ARCH_HWEIGHT_CFLAGS and how it throws a wrench into kcov, lto, etc, experimentations.
Add asm versions for __sw_hweight{32,64}() and do explicit saving and restoring of clobbered registers. This gets rid of the special calling convention. We get to call those functions on !X86_FEATURE_POPCNT CPUs.
We still need to hardcode POPCNT and register operands as some old gas versions which we support, do not know about POPCNT.
Btw, remove redundant REX prefix from 32-bit POPCNT because alternatives can do padding now.
--- arch/x86/Kconfig | 5 --- arch/x86/include/asm/arch_hweight.h | 24 +++++------- arch/x86/kernel/i386_ksyms_32.c | 2 + arch/x86/kernel/x8664_ksyms_64.c | 3 ++ arch/x86/lib/Makefile | 2 +- arch/x86/lib/hweight.S | 77 +++++++++++++++++++++++++++++++++++++ lib/Makefile | 5 --- lib/hweight.c | 4 ++ 8 files changed, 97 insertions(+), 25 deletions(-) create mode 100644 arch/x86/lib/hweight.S
arch/x86/Kconfig +0 -5
@@ -294,11 +294,6 @@ config X86_32_LAZY_GS def_bool y depends on X86_32 && !CC_STACKPROTECTOR -config ARCH_HWEIGHT_CFLAGS - string - default "-fcall-saved-ecx -fcall-saved-edx" if X86_32 - default "-fcall-saved-rdi -fcall-saved-rsi -fcall-saved-rdx -fcall-saved-rcx -fcall-saved-r8 -fcall-saved-r9 -fcall-saved-r10 -fcall-saved-r11" if X86_64 - config ARCH_SUPPORTS_UPROBES def_bool y
arch/x86/include/asm/arch_hweight.h +10 -14
@@ -4,8 +4,8 @@#include <asm/cpufeatures.h> #ifdef CONFIG_64BIT -/* popcnt %edi, %eax -- redundant REX prefix for alignment */ -#define POPCNT32 ".byte 0xf3,0x40,0x0f,0xb8,0xc7" +/* popcnt %edi, %eax */ +#define POPCNT32 ".byte 0xf3,0x0f,0xb8,0xc7"/* popcnt %rdi, %rax */ #define POPCNT64 ".byte 0xf3,0x48,0x0f,0xb8,0xc7"#define REG_IN "D"
@@ -17,19 +17,15 @@#define REG_OUT "a"#endif -/* -* __sw_hweightXX are called from within the alternatives below -* and callee-clobbered registers need to be taken care of. See -* ARCH_HWEIGHT_CFLAGS in <arch/x86/Kconfig> for the respective -* compiler switches. -*/ +#define __HAVE_ARCH_SW_HWEIGHT + static __always_inline unsigned int __arch_hweight32(unsigned int w) { -unsigned int res =0 ; +unsigned int res; asm (ALTERNATIVE("call __sw_hweight32", POPCNT32, X86_FEATURE_POPCNT) - : "="REG_OUT (res) - : REG_IN (w)); + : "="REG_OUT (res) + : REG_IN (w));return res; }
@@ -53,11 +49,11 @@ static inline unsigned long __arch_hweight64(__u64 w)#else static __always_inline unsigned long __arch_hweight64 (__u64 w) { -unsigned long res =0 ; +unsigned long res; asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT) - : "="REG_OUT (res) - : REG_IN (w)); + : "="REG_OUT (res) + : REG_IN (w));return res; }
arch/x86/kernel/i386_ksyms_32.c +2 -0
@@ -42,3 +42,5 @@ EXPORT_SYMBOL(empty_zero_page);EXPORT_SYMBOL (___preempt_schedule);EXPORT_SYMBOL (___preempt_schedule_notrace);#endif + +EXPORT_SYMBOL (__sw_hweight32);
arch/x86/kernel/x8664_ksyms_64.c +3 -0
@@ -44,6 +44,9 @@ EXPORT_SYMBOL(clear_page);EXPORT_SYMBOL (csum_partial); +EXPORT_SYMBOL (__sw_hweight32); +EXPORT_SYMBOL (__sw_hweight64); +/* * Export string functions. We normally rely on gcc builtin for most of these, * but gcc sometimes decides not to inline them.
arch/x86/lib/Makefile +1 -1
@@ -25,7 +25,7 @@ lib-y += memcpy_$(BITS).o lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o -obj-y += msr.o msr-reg.o msr-reg-export.o +obj-y += msr.o msr-reg.o msr-reg-export.o hweight.o ifeq ($(CONFIG_X86_32),y) obj-y += atomic64_32.o
arch/x86/lib/hweight.S new file +77 -0
77 lines not on this page · raw message
lib/Makefile +0 -5
@@ -15,9 +15,6 @@ KCOV_INSTRUMENT_rbtree.o := n KCOV_INSTRUMENT_list_debug.o := n KCOV_INSTRUMENT_debugobjects.o := n KCOV_INSTRUMENT_dynamic_debug.o := n -# Kernel does not boot if we instrument this file as it uses custom calling -# convention (see CONFIG_ARCH_HWEIGHT_CFLAGS). -KCOV_INSTRUMENT_hweight.o := n lib-y := ctype.o string.o vsprintf.o cmdline.o \ rbtree.o radix-tree.o dump_stack.o timerqueue.o\
@@ -73,8 +70,6 @@ obj-$(CONFIG_HAS_IOMEM) += iomap_copy.o devres.o obj-$(CONFIG_CHECK_SIGNATURE) += check_signature.o obj-$(CONFIG_DEBUG_LOCKING_API_SELFTESTS) += locking-selftest.o -GCOV_PROFILE_hweight.o := n -CFLAGS_hweight.o = $(subst $(quote),,$(CONFIG_ARCH_HWEIGHT_CFLAGS)) obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o obj-$(CONFIG_BTREE) += btree.o
lib/hweight.c +4 -0
@@ -9,6 +9,7 @@* The Hamming Weight of a number is the total number of bits set in it. */ +#ifndef __HAVE_ARCH_SW_HWEIGHTunsigned int __sw_hweight32 (unsigned int w) {#ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER
@@ -25,6 +26,7 @@ unsigned int __sw_hweight32(unsigned int w) #endif }EXPORT_SYMBOL (__sw_hweight32); +#endif unsigned int __sw_hweight16 (unsigned int w) {
@@ -43,6 +45,7 @@ unsigned int __sw_hweight8(unsigned int w) }EXPORT_SYMBOL (__sw_hweight8); +#ifndef __HAVE_ARCH_SW_HWEIGHTunsigned long __sw_hweight64 (__u64 w) {#if BITS_PER_LONG ==32
@@ -65,3 +68,4 @@ unsigned long __sw_hweight64(__u64 w) #endif }EXPORT_SYMBOL (__sw_hweight64); +#endif
Signature · 1 line
2.7.3
Quoted 12 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
People complained about
ARCH_HWEIGHT_CFLAGSand how it throws a wrench into kcov, lto, etc, experimentations.Add asm versions for __sw_hweight{32,64}() and do explicit saving and restoring of clobbered registers. This gets rid of the special calling convention. We get to call those functions on !X86_FEATURE_POPCNT CPUs.
We still need to hardcode POPCNT and register operands as some old gas versions which we support, do not know about POPCNT.
Quoted 6 lines
Btw, remove redundant REX prefix from 32-bit POPCNT because alternatives can do padding now.
Suggested-by "H. Peter Anvin" <[email protected]>Signed-off-by Borislav Petkov <[email protected]>Cc Andy Lutomirski <[email protected]>
tip-bot for Borislav Petkov
merge botx86/hweight: Get rid of the special calling convention
Commit-ID: f5967101e9de12addcda4510dfbac66d7c5779c3 Gitweb: http://git.kernel.org/tip/f5967101e9de12addcda4510dfbac66d7c5779c3 Author: Borislav Petkov <[email protected]> AuthorDate: Mon, 30 May 2016 12:56:27 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 8 Jun 2016 15:01:02 +0200
x86/hweight: Get rid of the special calling convention
People complained about ARCH_HWEIGHT_CFLAGS and how it throws a wrench into kcov, lto, etc, experimentations.
Add asm versions for __sw_hweight{32,64}() and do explicit saving and restoring of clobbered registers. This gets rid of the special calling convention. We get to call those functions on !X86_FEATURE_POPCNT CPUs.
We still need to hardcode POPCNT and register operands as some old gas versions which we support, do not know about POPCNT.
Btw, remove redundant REX prefix from 32-bit POPCNT because alternatives can do padding now.
--- arch/x86/Kconfig | 5 --- arch/x86/include/asm/arch_hweight.h | 24 +++++------- arch/x86/kernel/i386_ksyms_32.c | 2 + arch/x86/kernel/x8664_ksyms_64.c | 3 ++ arch/x86/lib/Makefile | 2 +- arch/x86/lib/hweight.S | 77 +++++++++++++++++++++++++++++++++++++ lib/Makefile | 5 --- lib/hweight.c | 4 ++ 8 files changed, 97 insertions(+), 25 deletions(-)
arch/x86/Kconfig +0 -5
@@ -294,11 +294,6 @@ config X86_32_LAZY_GS def_bool y depends on X86_32 && !CC_STACKPROTECTOR -config ARCH_HWEIGHT_CFLAGS - string - default "-fcall-saved-ecx -fcall-saved-edx" if X86_32 - default "-fcall-saved-rdi -fcall-saved-rsi -fcall-saved-rdx -fcall-saved-rcx -fcall-saved-r8 -fcall-saved-r9 -fcall-saved-r10 -fcall-saved-r11" if X86_64 - config ARCH_SUPPORTS_UPROBES def_bool y
arch/x86/include/asm/arch_hweight.h +10 -14
@@ -4,8 +4,8 @@#include <asm/cpufeatures.h> #ifdef CONFIG_64BIT -/* popcnt %edi, %eax -- redundant REX prefix for alignment */ -#define POPCNT32 ".byte 0xf3,0x40,0x0f,0xb8,0xc7" +/* popcnt %edi, %eax */ +#define POPCNT32 ".byte 0xf3,0x0f,0xb8,0xc7"/* popcnt %rdi, %rax */ #define POPCNT64 ".byte 0xf3,0x48,0x0f,0xb8,0xc7"#define REG_IN "D"
@@ -17,19 +17,15 @@#define REG_OUT "a"#endif -/* -* __sw_hweightXX are called from within the alternatives below -* and callee-clobbered registers need to be taken care of. See -* ARCH_HWEIGHT_CFLAGS in <arch/x86/Kconfig> for the respective -* compiler switches. -*/ +#define __HAVE_ARCH_SW_HWEIGHT + static __always_inline unsigned int __arch_hweight32(unsigned int w) { -unsigned int res =0 ; +unsigned int res; asm (ALTERNATIVE("call __sw_hweight32", POPCNT32, X86_FEATURE_POPCNT) - : "="REG_OUT (res) - : REG_IN (w)); + : "="REG_OUT (res) + : REG_IN (w));return res; }
@@ -53,11 +49,11 @@ static inline unsigned long __arch_hweight64(__u64 w)#else static __always_inline unsigned long __arch_hweight64 (__u64 w) { -unsigned long res =0 ; +unsigned long res; asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT) - : "="REG_OUT (res) - : REG_IN (w)); + : "="REG_OUT (res) + : REG_IN (w));return res; }
arch/x86/kernel/i386_ksyms_32.c +2 -0
@@ -42,3 +42,5 @@ EXPORT_SYMBOL(empty_zero_page);EXPORT_SYMBOL (___preempt_schedule);EXPORT_SYMBOL (___preempt_schedule_notrace);#endif + +EXPORT_SYMBOL (__sw_hweight32);
arch/x86/kernel/x8664_ksyms_64.c +3 -0
@@ -44,6 +44,9 @@ EXPORT_SYMBOL(clear_page);EXPORT_SYMBOL (csum_partial); +EXPORT_SYMBOL (__sw_hweight32); +EXPORT_SYMBOL (__sw_hweight64); +/* * Export string functions. We normally rely on gcc builtin for most of these, * but gcc sometimes decides not to inline them.
arch/x86/lib/Makefile +1 -1
@@ -25,7 +25,7 @@ lib-y += memcpy_$(BITS).o lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o -obj-y += msr.o msr-reg.o msr-reg-export.o +obj-y += msr.o msr-reg.o msr-reg-export.o hweight.o ifeq ($(CONFIG_X86_32),y) obj-y += atomic64_32.o
arch/x86/lib/hweight.S new file +77 -0
77 lines not on this page · raw message
lib/Makefile +0 -5
@@ -15,9 +15,6 @@ KCOV_INSTRUMENT_rbtree.o := n KCOV_INSTRUMENT_list_debug.o := n KCOV_INSTRUMENT_debugobjects.o := n KCOV_INSTRUMENT_dynamic_debug.o := n -# Kernel does not boot if we instrument this file as it uses custom calling -# convention (see CONFIG_ARCH_HWEIGHT_CFLAGS). -KCOV_INSTRUMENT_hweight.o := n lib-y := ctype.o string.o vsprintf.o cmdline.o \ rbtree.o radix-tree.o dump_stack.o timerqueue.o\
@@ -74,8 +71,6 @@ obj-$(CONFIG_HAS_IOMEM) += iomap_copy.o devres.o obj-$(CONFIG_CHECK_SIGNATURE) += check_signature.o obj-$(CONFIG_DEBUG_LOCKING_API_SELFTESTS) += locking-selftest.o -GCOV_PROFILE_hweight.o := n -CFLAGS_hweight.o = $(subst $(quote),,$(CONFIG_ARCH_HWEIGHT_CFLAGS)) obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o obj-$(CONFIG_BTREE) += btree.o
lib/hweight.c +4 -0
@@ -9,6 +9,7 @@* The Hamming Weight of a number is the total number of bits set in it. */ +#ifndef __HAVE_ARCH_SW_HWEIGHTunsigned int __sw_hweight32 (unsigned int w) {#ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER
@@ -25,6 +26,7 @@ unsigned int __sw_hweight32(unsigned int w) #endif }EXPORT_SYMBOL (__sw_hweight32); +#endif unsigned int __sw_hweight16 (unsigned int w) {
@@ -43,6 +45,7 @@ unsigned int __sw_hweight8(unsigned int w) }EXPORT_SYMBOL (__sw_hweight8); +#ifndef __HAVE_ARCH_SW_HWEIGHTunsigned long __sw_hweight64 (__u64 w) {#if BITS_PER_LONG ==32
@@ -65,3 +68,4 @@ unsigned long __sw_hweight64(__u64 w) #endif }EXPORT_SYMBOL (__sw_hweight64); +#endif