OpenCloudOS-Kernel/arch/x86
Jim Mattson 51138f1f35 KVM: x86: Use a switch statement and macros in __feature_translate()
commit 80c883db87d9ffe2d685e91ba07a087b1c246c78 upstream.

Use a switch statement with macro-generated case statements to handle
translating feature flags in order to reduce the probability of runtime
errors due to copy+paste goofs, to make compile-time errors easier to
debug, and to make the code more readable.

E.g. the compiler won't directly generate an error for duplicate if
statements

	if (x86_feature == X86_FEATURE_SGX1)
		return KVM_X86_FEATURE_SGX1;
	else if (x86_feature == X86_FEATURE_SGX2)
		return KVM_X86_FEATURE_SGX1;

and so instead reverse_cpuid_check() will fail due to the untranslated
entry pointing at a Linux-defined leaf, which provides practically no
hint as to what is broken

  arch/x86/kvm/reverse_cpuid.h:108:2: error: call to __compiletime_assert_450 declared with 'error' attribute:
                                      BUILD_BUG_ON failed: x86_leaf == CPUID_LNX_4
          BUILD_BUG_ON(x86_leaf == CPUID_LNX_4);
          ^
whereas duplicate case statements very explicitly point at the offending
code:

  arch/x86/kvm/reverse_cpuid.h:125:2: error: duplicate case value '361'
          KVM_X86_TRANSLATE_FEATURE(SGX2);
          ^
  arch/x86/kvm/reverse_cpuid.h:124:2: error: duplicate case value '360'
          KVM_X86_TRANSLATE_FEATURE(SGX1);
          ^

And without macros, the opposite type of copy+paste goof doesn't generate
any error at compile-time, e.g. this yields no complaints:

        case X86_FEATURE_SGX1:
                return KVM_X86_FEATURE_SGX1;
        case X86_FEATURE_SGX2:
                return KVM_X86_FEATURE_SGX1;

Note, __feature_translate() is forcibly inlined and the feature is known
at compile-time, so the code generation between an if-elif sequence and a
switch statement should be identical.

Signed-off-by: Jim Mattson <jmattson@google.com>
Link: https://lore.kernel.org/r/20231024001636.890236-2-jmattson@google.com
[sean: use a macro, rewrite changelog]
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-04-03 15:28:15 +02:00
..
boot x86/efistub: Use 1:1 file:memory mapping for PE/COFF .compat section 2024-02-23 09:25:27 +01:00
coco x86/tdx: Allow 32-bit emulation by default 2023-12-13 18:45:02 +01: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/sha - load modules based on CPU features 2023-11-28 17:19:56 +00:00
entry x86/entry_32: Add VERW just before userspace transition 2024-03-06 14:48:44 +00:00
events perf/x86/amd/core: Avoid register reset when CPU is dead 2024-03-26 18:20:03 -04:00
hyperv x86/hyperv: Use per cpu initial stack for vtl context 2024-03-26 18:20:06 -04:00
ia32
include x86/mm: Ensure input to pfn_to_kaddr() is treated as a 64-bit type 2024-03-26 18:19:19 -04:00
kernel ACPI: CPPC: enable AMD CPPC V2 support for family 17h processors 2024-03-26 18:19:36 -04:00
kvm KVM: x86: Use a switch statement and macros in __feature_translate() 2024-04-03 15:28:15 +02:00
lib x86/lib: Revert to _ASM_EXTABLE_UA() for {get,put}_user() fixups 2024-02-16 19:10:54 +01:00
math-emu
mm x86/sme: Fix memory encryption setting if enabled by default and not overridden 2024-03-26 18:19:19 -04:00
net bpf: Fix prog_array_map_poke_run map poke update 2024-01-01 12:42:23 +00:00
pci x86/pci: Reserve ECAM if BIOS didn't include it in PNP0C02 _CRS 2024-01-25 15:35:48 -08:00
platform efi/x86: Move EFI runtime call setup/teardown helpers out of line 2023-09-11 06:37:50 +00:00
power
purgatory x86/purgatory: Remove LTO flags 2023-09-17 09:49:03 +02:00
ras
realmode
tools x86, relocs: Ignore relocations in .notes section 2024-03-26 18:19:35 -04: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: Add some null pointer checking to smp.c 2024-03-26 18:19:10 -04:00
.gitignore
Kbuild
Kconfig x86/rfds: Mitigate Register File Data Sampling (RFDS) 2024-03-15 10:48:22 -04:00
Kconfig.assembler
Kconfig.cpu x86/Kconfig: Transmeta Crusoe is CPU family 5, not 6 2024-02-23 09:25:10 +01:00
Kconfig.debug
Makefile Kbuild updates for v6.6 2023-09-05 11:01:47 -07:00
Makefile.postlink
Makefile.um
Makefile_32.cpu