[PATCH v3 0/2] x86: enforce and cleanup RIP-relative accesses in early boot code

From: Kevin Loughlin
Date: Tue Jan 30 2024 - 17:09:03 EST


SEV/SME code can execute prior to page table fixups for kernel
relocation. However, as with global variables accessed in
__startup_64(), the compiler is not required to generate RIP-relative
accesses for SEV/SME global variables, causing certain flavors of SEV
hosts and guests built with clang to crash during boot.

These crashes highlight a broader problem wherein the toolchain does
not guarantee that early x86-64 code executes correctly at any offset.
While Ard has been looking into overhauling the early x86-64 code
going forward [0], the signficant proposed changes are unfortunately
not backport-friendly.

Instead, this patchset continues the approach of fixing the immediate
problem of SEV-SNP boots crashing when built by clang, providing a
backport-friendly set of changes needed to successfully boot SEV-SNP
hosts and guests. In particular, this patchset is a cleanup of V2 [1],
which introduces a macro to force RIP-relative addressing in early
SEV/SME global variable accesses and existing head64 global accesses.

V2 -> V3: Rename RIP_RELATIVE_ADDR(), remove fixup_*(), cleanup style
V1 -> V2: Use GET_RIP_RELATIVE_PTR() macro to avoid -fPIE compilation

[0] https://lore.kernel.org/lkml/20240129180502.4069817-23-ardb+git@xxxxxxxxxx/T/
[1] https://lore.kernel.org/lkml/20240111223650.3502633-1-kevinloughlin@xxxxxxxxxx/

Kevin Loughlin (2):
x86/sev: enforce RIP-relative accesses in early SEV/SME code
x86/head64: Replace pointer fixups with RIP_RELATIVE_ADDR()

arch/x86/coco/core.c | 22 +++++---
arch/x86/include/asm/mem_encrypt.h | 32 +++++++++--
arch/x86/kernel/head64.c | 88 +++++++++++++-----------------
arch/x86/kernel/head_64.S | 4 +-
arch/x86/kernel/sev-shared.c | 52 +++++++++---------
arch/x86/kernel/sev.c | 13 +++--
arch/x86/mm/mem_encrypt_identity.c | 50 +++++++++--------
7 files changed, 143 insertions(+), 118 deletions(-)

--
2.43.0.429.g432eaa2c6b-goog