Re: [PATCH v6 5/5] x86/gsseg: use the LKGS instruction if available for load_gs_index()

From: Borislav Petkov
Date: Sun Jun 30 2024 - 12:05:27 EST


On Wed, Jan 11, 2023 at 11:20:32PM -0800, Xin Li wrote:
> From: "H. Peter Anvin (Intel)" <hpa@xxxxxxxxx>
>
> 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) <hpa@xxxxxxxxx>
> Signed-off-by: Xin Li <xin3.li@xxxxxxxxx>
> ---
>
> Changes since v5:
> * Remove reviewers' SOBs (Borislav Petkov).
>
> Changes since v4:
> * Clear the LKGS feature from Xen PV guests (Juergen Gross).
>
> Changes since v3:
> * We want less ASM not more, thus keep local_irq_{save,restore}() inside
> native_load_gs_index() (Thomas Gleixner).
> * For paravirt enabled kernels, initialize pv_ops.cpu.load_gs_index to
> native_lkgs (Thomas Gleixner).
>
> Changes since v2:
> * Mark DI as input and output (+D) as in v1, since the exception handler
> modifies it (Brian Gerst).
>
> Changes since v1:
> * Use EX_TYPE_ZERO_REG instead of fixup code in the obsolete .fixup code
> section (Peter Zijlstra).
> * Add a comment that states the LKGS_DI macro will be replaced with "lkgs %di"
> once the binutils support the LKGS instruction (Peter Zijlstra).
> ---
> arch/x86/include/asm/gsseg.h | 33 +++++++++++++++++++++++++++++----
> arch/x86/kernel/cpu/common.c | 1 +
> arch/x86/xen/enlighten_pv.c | 1 +
> 3 files changed, 31 insertions(+), 4 deletions(-)
>
> diff --git a/arch/x86/include/asm/gsseg.h b/arch/x86/include/asm/gsseg.h
> index d15577c39e8d..ab6a595cea70 100644
> --- a/arch/x86/include/asm/gsseg.h
> +++ b/arch/x86/include/asm/gsseg.h
> @@ -14,17 +14,42 @@
>
> extern asmlinkage void asm_load_gs_index(u16 selector);
>
> +/* Replace with "lkgs %di" once binutils support LKGS instruction */
> +#define LKGS_DI _ASM_BYTES(0xf2,0x0f,0x00,0xf7)

Right, so this is %di only and not %edi.

FRED spec says:

"LKGS takes a single 16-bit operand (in a register or memory) and uses it to
load a descriptor from the GDT or an LDT."

However, latest objdump (GNU objdump (GNU Binutils) 2.42.50.20240630) says

ffffffff81026dfb: f2 0f 00 f7 lkgs %edi

objdump needs fixing, I'd say...

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette