arch/x86/kvm/cpuid.c
Source file repositories/reference/linux-study-clean/arch/x86/kvm/cpuid.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/kvm/cpuid.c- Extension
.c- Size
- 58127 bytes
- Lines
- 2179
- Domain
- Architecture Layer
- Bucket
- arch/x86
- Inferred role
- Architecture Layer: exported/initcall integration point
- Status
- integration 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.
- CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kvm_host.hlinux/lockdep.hlinux/export.hlinux/vmalloc.hlinux/uaccess.hlinux/sched/stat.hasm/processor.hasm/user.hasm/fpu/xstate.hasm/sgx.hasm/cpuid/api.hcpuid.hlapic.hmmu.htrace.hpmu.hxen.h
Detected Declarations
struct cpuid_xstate_sizesstruct kvm_cpuid_arrayfunction kvm_init_xstate_sizesfunction xstate_required_sizefunction lookupfunction kvm_check_cpuidfunction kvm_cpuid_check_equalfunction kvm_get_hypervisor_cpuidfunction for_each_possible_cpuid_base_hypervisorfunction kvm_apply_cpuid_pv_features_quirkfunction cpuid_get_supported_xcr0function cpuid_get_supported_xssfunction kvm_update_feature_runtimefunction kvm_update_cpuid_runtimefunction kvm_cpuid_has_hypervfunction guest_cpuid_is_amd_or_hygonfunction __cpuid_entry_get_regfunction kvm_vcpu_after_set_cpuidfunction cpuid_query_maxphyaddrfunction cpuid_query_maxguestphyaddrfunction kvm_vcpu_reserved_gpa_bits_rawfunction kvm_set_cpuidfunction VMMsfunction kvm_vcpu_ioctl_set_cpuidfunction kvm_vcpu_ioctl_set_cpuid2function kvm_vcpu_ioctl_get_cpuid2function raw_cpuid_getfunction kvm_initialize_cpu_capsfunction kvm_cpu_cap_hasfunction cpuid_func_emulatedfunction __do_cpuid_func_emulatedfunction __do_cpuid_funcfunction TDPfunction do_cpuid_funcfunction get_cpuid_funcfunction sanity_check_entriesfunction kvm_dev_ioctl_get_cpuidfunction leaffunction kvm_cpuidfunction kvm_xen_is_tsc_leaffunction kvm_emulate_cpuid
Annotated Snippet
struct cpuid_xstate_sizes {
u32 eax;
u32 ebx;
u32 ecx;
};
static struct cpuid_xstate_sizes xstate_sizes[XFEATURE_MAX] __ro_after_init;
void __init kvm_init_xstate_sizes(void)
{
u32 ign;
int i;
for (i = XFEATURE_YMM; i < ARRAY_SIZE(xstate_sizes); i++) {
struct cpuid_xstate_sizes *xs = &xstate_sizes[i];
cpuid_count(0xD, i, &xs->eax, &xs->ebx, &xs->ecx, &ign);
}
}
u32 xstate_required_size(u64 xstate_bv, bool compacted)
{
u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
int i;
xstate_bv &= XFEATURE_MASK_EXTEND;
for (i = XFEATURE_YMM; i < ARRAY_SIZE(xstate_sizes) && xstate_bv; i++) {
struct cpuid_xstate_sizes *xs = &xstate_sizes[i];
u32 offset;
if (!(xstate_bv & BIT_ULL(i)))
continue;
/* ECX[1]: 64B alignment in compacted form */
if (compacted)
offset = (xs->ecx & 0x2) ? ALIGN(ret, 64) : ret;
else
offset = xs->ebx;
ret = max(ret, offset + xs->eax);
xstate_bv &= ~BIT_ULL(i);
}
return ret;
}
struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2(
struct kvm_cpuid_entry2 *entries, int nent, u32 function, u64 index)
{
struct kvm_cpuid_entry2 *e;
int i;
/*
* KVM has a semi-arbitrary rule that querying the guest's CPUID model
* with IRQs disabled is disallowed. The CPUID model can legitimately
* have over one hundred entries, i.e. the lookup is slow, and IRQs are
* typically disabled in KVM only when KVM is in a performance critical
* path, e.g. the core VM-Enter/VM-Exit run loop. Nothing will break
* if this rule is violated, this assertion is purely to flag potential
* performance issues. If this fires, consider moving the lookup out
* of the hotpath, e.g. by caching information during CPUID updates.
*/
lockdep_assert_irqs_enabled();
for (i = 0; i < nent; i++) {
e = &entries[i];
if (e->function != function)
continue;
/*
* If the index isn't significant, use the first entry with a
* matching function. It's userspace's responsibility to not
* provide "duplicate" entries in all cases.
*/
if (!(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) || e->index == index)
return e;
/*
* Similarly, use the first matching entry if KVM is doing a
* lookup (as opposed to emulating CPUID) for a function that's
* architecturally defined as not having a significant index.
*/
if (index == KVM_CPUID_INDEX_NOT_SIGNIFICANT) {
/*
* Direct lookups from KVM should not diverge from what
* KVM defines internally (the architectural behavior).
*/
WARN_ON_ONCE(cpuid_function_is_indexed(function));
return e;
Annotation
- Immediate include surface: `linux/kvm_host.h`, `linux/lockdep.h`, `linux/export.h`, `linux/vmalloc.h`, `linux/uaccess.h`, `linux/sched/stat.h`, `asm/processor.h`, `asm/user.h`.
- Detected declarations: `struct cpuid_xstate_sizes`, `struct kvm_cpuid_array`, `function kvm_init_xstate_sizes`, `function xstate_required_size`, `function lookup`, `function kvm_check_cpuid`, `function kvm_cpuid_check_equal`, `function kvm_get_hypervisor_cpuid`, `function for_each_possible_cpuid_base_hypervisor`, `function kvm_apply_cpuid_pv_features_quirk`.
- Atlas domain: Architecture Layer / arch/x86.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.