d5a581d84a
As described in:77b0bf55bc
: ("kbuild/Makefile: Prepare for using macros in inline assembly code to work around asm() related GCC inlining bugs") GCC's inlining heuristics are broken with common asm() patterns used in kernel code, resulting in the effective disabling of inlining. The workaround is to set an assembly macro and call it from the inline assembly block - which is pretty pointless indirection in the static_cpu_has() case, but is worth it to improve overall inlining quality. The patch slightly increases the kernel size: text data bss dec hex filename 18162879 10226256 2957312 31346447 1de4f0f ./vmlinux before 18163528 10226300 2957312 313471401de51c4
./vmlinux after (+693) And enables the inlining of function such as free_ldt_pgtables(). Tested-by: Kees Cook <keescook@chromium.org> Signed-off-by: Nadav Amit <namit@vmware.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/20181005202718.229565-3-namit@vmware.com Link: https://lore.kernel.org/lkml/20181003213100.189959-10-namit@vmware.com/T/#u Signed-off-by: Ingo Molnar <mingo@kernel.org>
243 lines
7.9 KiB
C
243 lines
7.9 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef _ASM_X86_CPUFEATURE_H
|
|
#define _ASM_X86_CPUFEATURE_H
|
|
|
|
#ifdef __KERNEL__
|
|
#ifndef __ASSEMBLY__
|
|
|
|
#include <asm/processor.h>
|
|
#include <asm/asm.h>
|
|
#include <linux/bitops.h>
|
|
|
|
enum cpuid_leafs
|
|
{
|
|
CPUID_1_EDX = 0,
|
|
CPUID_8000_0001_EDX,
|
|
CPUID_8086_0001_EDX,
|
|
CPUID_LNX_1,
|
|
CPUID_1_ECX,
|
|
CPUID_C000_0001_EDX,
|
|
CPUID_8000_0001_ECX,
|
|
CPUID_LNX_2,
|
|
CPUID_LNX_3,
|
|
CPUID_7_0_EBX,
|
|
CPUID_D_1_EAX,
|
|
CPUID_F_0_EDX,
|
|
CPUID_F_1_EDX,
|
|
CPUID_8000_0008_EBX,
|
|
CPUID_6_EAX,
|
|
CPUID_8000_000A_EDX,
|
|
CPUID_7_ECX,
|
|
CPUID_8000_0007_EBX,
|
|
CPUID_7_EDX,
|
|
};
|
|
|
|
#ifdef CONFIG_X86_FEATURE_NAMES
|
|
extern const char * const x86_cap_flags[NCAPINTS*32];
|
|
extern const char * const x86_power_flags[32];
|
|
#define X86_CAP_FMT "%s"
|
|
#define x86_cap_flag(flag) x86_cap_flags[flag]
|
|
#else
|
|
#define X86_CAP_FMT "%d:%d"
|
|
#define x86_cap_flag(flag) ((flag) >> 5), ((flag) & 31)
|
|
#endif
|
|
|
|
/*
|
|
* In order to save room, we index into this array by doing
|
|
* X86_BUG_<name> - NCAPINTS*32.
|
|
*/
|
|
extern const char * const x86_bug_flags[NBUGINTS*32];
|
|
|
|
#define test_cpu_cap(c, bit) \
|
|
test_bit(bit, (unsigned long *)((c)->x86_capability))
|
|
|
|
/*
|
|
* There are 32 bits/features in each mask word. The high bits
|
|
* (selected with (bit>>5) give us the word number and the low 5
|
|
* bits give us the bit/feature number inside the word.
|
|
* (1UL<<((bit)&31) gives us a mask for the feature_bit so we can
|
|
* see if it is set in the mask word.
|
|
*/
|
|
#define CHECK_BIT_IN_MASK_WORD(maskname, word, bit) \
|
|
(((bit)>>5)==(word) && (1UL<<((bit)&31) & maskname##word ))
|
|
|
|
#define REQUIRED_MASK_BIT_SET(feature_bit) \
|
|
( CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 0, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 1, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 2, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 3, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 4, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 5, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 6, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 7, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 8, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 9, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 10, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 11, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 12, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 13, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 14, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 15, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 16, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 17, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(REQUIRED_MASK, 18, feature_bit) || \
|
|
REQUIRED_MASK_CHECK || \
|
|
BUILD_BUG_ON_ZERO(NCAPINTS != 19))
|
|
|
|
#define DISABLED_MASK_BIT_SET(feature_bit) \
|
|
( CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 0, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 1, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 2, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 3, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 4, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 5, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 6, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 7, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 8, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 9, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 10, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 11, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 12, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 13, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 14, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 15, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 16, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 17, feature_bit) || \
|
|
CHECK_BIT_IN_MASK_WORD(DISABLED_MASK, 18, feature_bit) || \
|
|
DISABLED_MASK_CHECK || \
|
|
BUILD_BUG_ON_ZERO(NCAPINTS != 19))
|
|
|
|
#define cpu_has(c, bit) \
|
|
(__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 : \
|
|
test_cpu_cap(c, bit))
|
|
|
|
#define this_cpu_has(bit) \
|
|
(__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 : \
|
|
x86_this_cpu_test_bit(bit, (unsigned long *)&cpu_info.x86_capability))
|
|
|
|
/*
|
|
* This macro is for detection of features which need kernel
|
|
* infrastructure to be used. It may *not* directly test the CPU
|
|
* itself. Use the cpu_has() family if you want true runtime
|
|
* testing of CPU features, like in hypervisor code where you are
|
|
* supporting a possible guest feature where host support for it
|
|
* is not relevant.
|
|
*/
|
|
#define cpu_feature_enabled(bit) \
|
|
(__builtin_constant_p(bit) && DISABLED_MASK_BIT_SET(bit) ? 0 : static_cpu_has(bit))
|
|
|
|
#define boot_cpu_has(bit) cpu_has(&boot_cpu_data, bit)
|
|
|
|
#define set_cpu_cap(c, bit) set_bit(bit, (unsigned long *)((c)->x86_capability))
|
|
|
|
extern void setup_clear_cpu_cap(unsigned int bit);
|
|
extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
|
|
|
|
#define setup_force_cpu_cap(bit) do { \
|
|
set_cpu_cap(&boot_cpu_data, bit); \
|
|
set_bit(bit, (unsigned long *)cpu_caps_set); \
|
|
} while (0)
|
|
|
|
#define setup_force_cpu_bug(bit) setup_force_cpu_cap(bit)
|
|
|
|
#if defined(__clang__) && !defined(CC_HAVE_ASM_GOTO)
|
|
|
|
/*
|
|
* Workaround for the sake of BPF compilation which utilizes kernel
|
|
* headers, but clang does not support ASM GOTO and fails the build.
|
|
*/
|
|
#ifndef __BPF_TRACING__
|
|
#warning "Compiler lacks ASM_GOTO support. Add -D __BPF_TRACING__ to your compiler arguments"
|
|
#endif
|
|
|
|
#define static_cpu_has(bit) boot_cpu_has(bit)
|
|
|
|
#else
|
|
|
|
/*
|
|
* Static testing of CPU features. Used the same as boot_cpu_has().
|
|
* These will statically patch the target code for additional
|
|
* performance.
|
|
*/
|
|
static __always_inline __pure bool _static_cpu_has(u16 bit)
|
|
{
|
|
asm_volatile_goto("STATIC_CPU_HAS bitnum=%[bitnum] "
|
|
"cap_byte=\"%[cap_byte]\" "
|
|
"feature=%P[feature] t_yes=%l[t_yes] "
|
|
"t_no=%l[t_no] always=%P[always]"
|
|
: : [feature] "i" (bit),
|
|
[always] "i" (X86_FEATURE_ALWAYS),
|
|
[bitnum] "i" (1 << (bit & 7)),
|
|
[cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
|
|
: : t_yes, t_no);
|
|
t_yes:
|
|
return true;
|
|
t_no:
|
|
return false;
|
|
}
|
|
|
|
#define static_cpu_has(bit) \
|
|
( \
|
|
__builtin_constant_p(boot_cpu_has(bit)) ? \
|
|
boot_cpu_has(bit) : \
|
|
_static_cpu_has(bit) \
|
|
)
|
|
#endif
|
|
|
|
#define cpu_has_bug(c, bit) cpu_has(c, (bit))
|
|
#define set_cpu_bug(c, bit) set_cpu_cap(c, (bit))
|
|
#define clear_cpu_bug(c, bit) clear_cpu_cap(c, (bit))
|
|
|
|
#define static_cpu_has_bug(bit) static_cpu_has((bit))
|
|
#define boot_cpu_has_bug(bit) cpu_has_bug(&boot_cpu_data, (bit))
|
|
#define boot_cpu_set_bug(bit) set_cpu_cap(&boot_cpu_data, (bit))
|
|
|
|
#define MAX_CPU_FEATURES (NCAPINTS * 32)
|
|
#define cpu_have_feature boot_cpu_has
|
|
|
|
#define CPU_FEATURE_TYPEFMT "x86,ven%04Xfam%04Xmod%04X"
|
|
#define CPU_FEATURE_TYPEVAL boot_cpu_data.x86_vendor, boot_cpu_data.x86, \
|
|
boot_cpu_data.x86_model
|
|
|
|
#else /* __ASSEMBLY__ */
|
|
|
|
.macro STATIC_CPU_HAS bitnum:req cap_byte:req feature:req t_yes:req t_no:req always:req
|
|
1:
|
|
jmp 6f
|
|
2:
|
|
.skip -(((5f-4f) - (2b-1b)) > 0) * ((5f-4f) - (2b-1b)),0x90
|
|
3:
|
|
.section .altinstructions,"a"
|
|
.long 1b - . /* src offset */
|
|
.long 4f - . /* repl offset */
|
|
.word \always /* always replace */
|
|
.byte 3b - 1b /* src len */
|
|
.byte 5f - 4f /* repl len */
|
|
.byte 3b - 2b /* pad len */
|
|
.previous
|
|
.section .altinstr_replacement,"ax"
|
|
4:
|
|
jmp \t_no
|
|
5:
|
|
.previous
|
|
.section .altinstructions,"a"
|
|
.long 1b - . /* src offset */
|
|
.long 0 /* no replacement */
|
|
.word \feature /* feature bit */
|
|
.byte 3b - 1b /* src len */
|
|
.byte 0 /* repl len */
|
|
.byte 0 /* pad len */
|
|
.previous
|
|
.section .altinstr_aux,"ax"
|
|
6:
|
|
testb \bitnum,\cap_byte
|
|
jnz \t_yes
|
|
jmp \t_no
|
|
.previous
|
|
.endm
|
|
|
|
#endif /* __ASSEMBLY__ */
|
|
|
|
#endif /* __KERNEL__ */
|
|
#endif /* _ASM_X86_CPUFEATURE_H */
|