OpenCloudOS-Kernel/arch/x86
Ard Biesheuvel 6ae55c7fa6 x86/purgatory: Switch to the position-independent small code model
[ Upstream commit cba786af84a0f9716204e09f518ce3b7ada8555e ]

On x86, the ordinary, position dependent small and kernel code models
only support placement of the executable in 32-bit addressable memory,
due to the use of 32-bit signed immediates to generate references to
global variables. For the kernel, this implies that all global variables
must reside in the top 2 GiB of the kernel virtual address space, where
the implicit address bits 63:32 are equal to sign bit 31.

This means the kernel code model is not suitable for other bare metal
executables such as the kexec purgatory, which can be placed arbitrarily
in the physical address space, where its address may no longer be
representable as a sign extended 32-bit quantity. For this reason,
commit

  e16c2983fb ("x86/purgatory: Change compiler flags from -mcmodel=kernel to -mcmodel=large to fix kexec relocation errors")

switched to the large code model, which uses 64-bit immediates for all
symbol references, including function calls, in order to avoid relying
on any assumptions regarding proximity of symbols in the final
executable.

The large code model is rarely used, clunky and the least likely to
operate in a similar fashion when comparing GCC and Clang, so it is best
avoided. This is especially true now that Clang 18 has started to emit
executable code in two separate sections (.text and .ltext), which
triggers an issue in the kexec loading code at runtime.

The SUSE bugzilla fixes tag points to gcc 13 having issues with the
large model too and that perhaps the large model should simply not be
used at all.

Instead, use the position independent small code model, which makes no
assumptions about placement but only about proximity, where all
referenced symbols must be within -/+ 2 GiB, i.e., in range for a
RIP-relative reference. Use hidden visibility to suppress the use of a
GOT, which carries absolute addresses that are not covered by static ELF
relocations, and is therefore incompatible with the kexec loader's
relocation logic.

  [ bp: Massage commit message. ]

Fixes: e16c2983fb ("x86/purgatory: Change compiler flags from -mcmodel=kernel to -mcmodel=large to fix kexec relocation errors")
Fixes: https://bugzilla.suse.com/show_bug.cgi?id=1211853
Closes: https://github.com/ClangBuiltLinux/linux/issues/2016
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Fangrui Song <maskray@google.com>
Acked-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Link: https://lore.kernel.org/all/20240417-x86-fix-kexec-with-llvm-18-v1-0-5383121e8fb7@kernel.org/
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-06-12 11:11:43 +02:00
..
boot x86/boot/64: Clear most of CR4 in startup_64(), except PAE, MCE and LA57 2024-06-12 11:11:41 +02:00
coco x86/coco: Require seeding RNG with RDRAND on CoCo systems 2024-04-10 16:36:03 +02:00
configs - The first, cleanup part of the microcode loader reorg tglx has been 2023-08-28 15:55:20 -07:00
crypto crypto: x86/sha512-avx2 - add missing vzeroupper 2024-06-12 11:11:32 +02:00
entry x86/mm: Remove broken vsyscall emulation code from the page fault code 2024-06-12 11:11:29 +02:00
events perf/x86: Fix out of range data 2024-04-17 11:19:36 +02:00
hyperv x86/hyperv: Use per cpu initial stack for vtl context 2024-03-26 18:20:06 -04:00
ia32
include x86/pat: Introduce lookup_address_in_pgd_attr() 2024-06-12 11:11:41 +02:00
kernel x86/tsc: Trust initial offset in architectural TSC-adjust MSRs 2024-06-12 11:11:17 +02:00
kvm KVM: x86/pmu: Set enable bits for GP counters in PERF_GLOBAL_CTRL at "RESET" 2024-05-02 16:32:40 +02:00
lib x86/boot: Move mem_encrypt= parsing to the decompressor 2024-04-10 16:36:07 +02:00
math-emu x86/fpu: Include asm/fpu/regset.h 2023-05-18 11:56:18 -07:00
mm x86/pat: Fix W^X violation false-positives when running as Xen PV guest 2024-06-12 11:11:42 +02:00
net bpf, x86: Fix PROBE_MEM runtime load check 2024-06-12 11:11:27 +02:00
pci PCI: Disable D3cold on Asus B1400 PCI-NVMe bridge 2024-04-13 13:07:36 +02:00
platform x86/xen: attempt to inflate the memory balloon on PVH 2024-04-13 13:07:39 +02:00
power mm/treewide: replace pud_large() with pud_leaf() 2024-04-10 16:35:46 +02:00
purgatory x86/purgatory: Switch to the position-independent small code model 2024-06-12 11:11:43 +02:00
ras
realmode x86/realmode: Make stack lock work in trampoline_compat() 2023-05-30 14:11:47 +02:00
tools x86/boot: Ignore relocations in .notes sections in walk_relocs() too 2024-06-12 11:11:36 +02:00
um um: Hard-code the result of 'uname -s' 2023-08-26 22:40:37 +02:00
video Merge drm/drm-next into drm-misc-next 2023-07-24 15:44:47 +02:00
virt/vmx/tdx
xen x86/xen: attempt to inflate the memory balloon on PVH 2024-04-13 13:07:39 +02:00
.gitignore
Kbuild
Kconfig cpu: Ignore "mitigations" kernel parameter if CPU_MITIGATIONS=n 2024-06-12 11:11:24 +02:00
Kconfig.assembler x86/shstk: Add Kconfig option for shadow stack 2023-07-11 14:12:18 -07:00
Kconfig.cpu x86/Kconfig: Transmeta Crusoe is CPU family 5, not 6 2024-02-23 09:25:10 +01:00
Kconfig.debug docs: move x86 documentation into Documentation/arch/ 2023-03-30 12:58:51 -06:00
Makefile Kbuild updates for v6.6 2023-09-05 11:01:47 -07:00
Makefile.postlink x86/build: Avoid relocation information in final vmlinux 2023-06-14 19:54:40 +02:00
Makefile.um um: Only disable SSE on clang to work around old GCC bugs 2023-04-04 09:57:05 +02:00
Makefile_32.cpu