arch/x86/kvm/reverse_cpuid.h

Source file repositories/reference/linux-study-clean/arch/x86/kvm/reverse_cpuid.h

File Facts

System
Linux kernel
Corpus path
arch/x86/kvm/reverse_cpuid.h
Extension
.h
Size
9538 bytes
Lines
265
Domain
Architecture Layer
Bucket
arch/x86
Inferred role
Architecture Layer: implementation source
Status
source implementation candidate

Why This File Exists

CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.

Dependency Surface

Detected Declarations

Annotated Snippet

struct cpuid_reg {
	u32 function;
	u32 index;
	int reg;
};

static const struct cpuid_reg reverse_cpuid[] = {
	[CPUID_1_EDX]         = {         1, 0, CPUID_EDX},
	[CPUID_8000_0001_EDX] = {0x80000001, 0, CPUID_EDX},
	[CPUID_8086_0001_EDX] = {0x80860001, 0, CPUID_EDX},
	[CPUID_1_ECX]         = {         1, 0, CPUID_ECX},
	[CPUID_C000_0001_EDX] = {0xc0000001, 0, CPUID_EDX},
	[CPUID_8000_0001_ECX] = {0x80000001, 0, CPUID_ECX},
	[CPUID_7_0_EBX]       = {         7, 0, CPUID_EBX},
	[CPUID_D_1_EAX]       = {       0xd, 1, CPUID_EAX},
	[CPUID_8000_0008_EBX] = {0x80000008, 0, CPUID_EBX},
	[CPUID_6_EAX]         = {         6, 0, CPUID_EAX},
	[CPUID_8000_000A_EDX] = {0x8000000a, 0, CPUID_EDX},
	[CPUID_7_ECX]         = {         7, 0, CPUID_ECX},
	[CPUID_7_EDX]         = {         7, 0, CPUID_EDX},
	[CPUID_7_1_EAX]       = {         7, 1, CPUID_EAX},
	[CPUID_12_EAX]        = {0x00000012, 0, CPUID_EAX},
	[CPUID_8000_001F_EAX] = {0x8000001f, 0, CPUID_EAX},
	[CPUID_7_1_EDX]       = {         7, 1, CPUID_EDX},
	[CPUID_8000_0007_EDX] = {0x80000007, 0, CPUID_EDX},
	[CPUID_8000_0021_EAX] = {0x80000021, 0, CPUID_EAX},
	[CPUID_8000_0022_EAX] = {0x80000022, 0, CPUID_EAX},
	[CPUID_7_2_EDX]       = {         7, 2, CPUID_EDX},
	[CPUID_24_0_EBX]      = {      0x24, 0, CPUID_EBX},
	[CPUID_8000_0021_ECX] = {0x80000021, 0, CPUID_ECX},
	[CPUID_7_1_ECX]       = {         7, 1, CPUID_ECX},
	[CPUID_1E_1_EAX]      = {      0x1e, 1, CPUID_EAX},
	[CPUID_24_1_ECX]      = {      0x24, 1, CPUID_ECX},
};

/*
 * Reverse CPUID and its derivatives can only be used for hardware-defined
 * feature words, i.e. words whose bits directly correspond to a CPUID leaf.
 * Retrieving a feature bit or masking guest CPUID from a Linux-defined word
 * is nonsensical as the bit number/mask is an arbitrary software-defined value
 * and can't be used by KVM to query/control guest capabilities.  And obviously
 * the leaf being queried must have an entry in the lookup table.
 */
static __always_inline void reverse_cpuid_check(unsigned int x86_leaf)
{
	BUILD_BUG_ON(NR_CPUID_WORDS != NCAPINTS);
	BUILD_BUG_ON(x86_leaf == CPUID_LNX_1);
	BUILD_BUG_ON(x86_leaf == CPUID_LNX_2);
	BUILD_BUG_ON(x86_leaf == CPUID_LNX_3);
	BUILD_BUG_ON(x86_leaf == CPUID_LNX_4);
	BUILD_BUG_ON(x86_leaf == CPUID_LNX_5);
	BUILD_BUG_ON(x86_leaf >= ARRAY_SIZE(reverse_cpuid));
	BUILD_BUG_ON(reverse_cpuid[x86_leaf].function == 0);
}

/*
 * Translate feature bits that are scattered in the kernel's cpufeatures word
 * into KVM feature words that align with hardware's definitions.
 */
static __always_inline u32 __feature_translate(int x86_feature)
{
#define KVM_X86_TRANSLATE_FEATURE(f)	\
	case X86_FEATURE_##f: return KVM_X86_FEATURE_##f

	switch (x86_feature) {
	KVM_X86_TRANSLATE_FEATURE(SGX1);
	KVM_X86_TRANSLATE_FEATURE(SGX2);
	KVM_X86_TRANSLATE_FEATURE(SGX_EDECCSSA);
	KVM_X86_TRANSLATE_FEATURE(CONSTANT_TSC);
	KVM_X86_TRANSLATE_FEATURE(PERFMON_V2);
	KVM_X86_TRANSLATE_FEATURE(RRSBA_CTRL);
	KVM_X86_TRANSLATE_FEATURE(BHI_CTRL);
	KVM_X86_TRANSLATE_FEATURE(TSA_SQ_NO);
	KVM_X86_TRANSLATE_FEATURE(TSA_L1_NO);
	KVM_X86_TRANSLATE_FEATURE(MSR_IMM);
	default:
		return x86_feature;
	}
}

static __always_inline u32 __feature_leaf(int x86_feature)
{
	u32 x86_leaf = __feature_translate(x86_feature) / 32;

	reverse_cpuid_check(x86_leaf);
	return x86_leaf;
}

/*
 * Retrieve the bit mask from an X86_FEATURE_* definition.  Features contain

Annotation

Implementation Notes