Re: [RFC PATCH] x86/sev: x86/sev: enforce PC-relative addressing in clang

From: Andi Kleen
Date: Wed Jan 10 2024 - 12:50:03 EST


> On that note, I do have another version of this patch that abstracts
> snp_cpuid_get_table() into a macro along the lines of...
>
> #define GET_RIP_RELATIVE_PTR(var) \
> ({ \
> void *ptr; \
> asm ("lea "#var"(%%rip), %0" \
> : "=r" (ptr) \
> : "p" (&var)); \
> ptr; \
> })
>
> ...and uses this new macro to access all SEV/SME global variables (not
> just the cpuid_table). It's similar in nature to `fixup_pointer()`
> (currently defined in arch/x86/kernel/head64.c) but doesn't require us
> to pass around `physaddr` from `__startup64()`. This wouldn't
> introduce any new execution model changes between clang vs gcc and
> would be consistent with the kernel's current approach of relying on
> developers to manually apply fixups for global variable accesses prior
> to kernel relocation. I can send an RFC v2 for the
> GET_RIP_RELATIVE_PTR() version of this patch.

That looks like a far better solution indeed.

Ideally objtool would check for this, perhaps with a new ELF
section. But actually doing that might be far more work, so perhaps not
worth it.

Thanks,

-Andi