Skip to content

Commit 92cbbad

Browse files
H. Peter Anvin (Intel)Ingo Molnar
authored andcommitted
x86/gsseg: Use the LKGS instruction if available for load_gs_index()
The LKGS instruction atomically loads a segment descriptor into the %gs descriptor registers, *except* that %gs.base is unchanged, and the base is instead loaded into MSR_IA32_KERNEL_GS_BASE, which is exactly what we want this function to do. Signed-off-by: H. Peter Anvin (Intel) <[email protected]> Signed-off-by: Xin Li <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Cc: Andy Lutomirski <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Linus Torvalds <[email protected]>
1 parent ae53fa1 commit 92cbbad

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

arch/x86/include/asm/gsseg.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,42 @@
1414

1515
extern asmlinkage void asm_load_gs_index(u16 selector);
1616

17+
/* Replace with "lkgs %di" once binutils support LKGS instruction */
18+
#define LKGS_DI _ASM_BYTES(0xf2,0x0f,0x00,0xf7)
19+
20+
static inline void native_lkgs(unsigned int selector)
21+
{
22+
u16 sel = selector;
23+
asm_inline volatile("1: " LKGS_DI
24+
_ASM_EXTABLE_TYPE_REG(1b, 1b, EX_TYPE_ZERO_REG, %k[sel])
25+
: [sel] "+D" (sel));
26+
}
27+
1728
static inline void native_load_gs_index(unsigned int selector)
1829
{
19-
unsigned long flags;
30+
if (cpu_feature_enabled(X86_FEATURE_LKGS)) {
31+
native_lkgs(selector);
32+
} else {
33+
unsigned long flags;
2034

21-
local_irq_save(flags);
22-
asm_load_gs_index(selector);
23-
local_irq_restore(flags);
35+
local_irq_save(flags);
36+
asm_load_gs_index(selector);
37+
local_irq_restore(flags);
38+
}
2439
}
2540

2641
#endif /* CONFIG_X86_64 */
2742

43+
static inline void __init lkgs_init(void)
44+
{
45+
#ifdef CONFIG_PARAVIRT_XXL
46+
#ifdef CONFIG_X86_64
47+
if (cpu_feature_enabled(X86_FEATURE_LKGS))
48+
pv_ops.cpu.load_gs_index = native_lkgs;
49+
#endif
50+
#endif
51+
}
52+
2853
#ifndef CONFIG_PARAVIRT_XXL
2954

3055
static inline void load_gs_index(unsigned int selector)

arch/x86/kernel/cpu/common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,7 @@ void __init identify_boot_cpu(void)
19601960
setup_cr_pinning();
19611961

19621962
tsx_init();
1963+
lkgs_init();
19631964
}
19641965

19651966
void identify_secondary_cpu(struct cpuinfo_x86 *c)

arch/x86/xen/enlighten_pv.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ static void __init xen_init_capabilities(void)
276276
setup_clear_cpu_cap(X86_FEATURE_ACC);
277277
setup_clear_cpu_cap(X86_FEATURE_X2APIC);
278278
setup_clear_cpu_cap(X86_FEATURE_SME);
279+
setup_clear_cpu_cap(X86_FEATURE_LKGS);
279280

280281
/*
281282
* Xen PV would need some work to support PCID: CR3 handling as well

0 commit comments

Comments
 (0)