Skip to content

Commit 4f7ff70

Browse files
committed
Merge tag 'kvm-x86-misc-6.14' of https://github.com/kvm-x86/linux into HEAD
KVM x86 misc changes for 6.14: - Overhaul KVM's CPUID feature infrastructure to track all vCPU capabilities instead of just those where KVM needs to manage state and/or explicitly enable the feature in hardware. Along the way, refactor the code to make it easier to add features, and to make it more self-documenting how KVM is handling each feature. - Rework KVM's handling of VM-Exits during event vectoring; this plugs holes where KVM unintentionally puts the vCPU into infinite loops in some scenarios (e.g. if emulation is triggered by the exit), and brings parity between VMX and SVM. - Add pending request and interrupt injection information to the kvm_exit and kvm_entry tracepoints respectively. - Fix a relatively benign flaw where KVM would end up redoing RDPKRU when loading guest/host PKRU, due to a refactoring of the kernel helpers that didn't account for KVM's pre-checking of the need to do WRPKRU.
2 parents 892e7b8 + 4c20cd4 commit 4f7ff70

File tree

35 files changed

+1189
-721
lines changed

35 files changed

+1189
-721
lines changed

Documentation/virt/kvm/api.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,15 +1825,18 @@ emulate them efficiently. The fields in each entry are defined as follows:
18251825
the values returned by the cpuid instruction for
18261826
this function/index combination
18271827

1828-
The TSC deadline timer feature (CPUID leaf 1, ecx[24]) is always returned
1829-
as false, since the feature depends on KVM_CREATE_IRQCHIP for local APIC
1830-
support. Instead it is reported via::
1828+
x2APIC (CPUID leaf 1, ecx[21) and TSC deadline timer (CPUID leaf 1, ecx[24])
1829+
may be returned as true, but they depend on KVM_CREATE_IRQCHIP for in-kernel
1830+
emulation of the local APIC. TSC deadline timer support is also reported via::
18311831

18321832
ioctl(KVM_CHECK_EXTENSION, KVM_CAP_TSC_DEADLINE_TIMER)
18331833

18341834
if that returns true and you use KVM_CREATE_IRQCHIP, or if you emulate the
18351835
feature in userspace, then you can enable the feature for KVM_SET_CPUID2.
18361836

1837+
Enabling x2APIC in KVM_SET_CPUID2 requires KVM_CREATE_IRQCHIP as KVM doesn't
1838+
support forwarding x2APIC MSR accesses to userspace, i.e. KVM does not support
1839+
emulating x2APIC in userspace.
18371840

18381841
4.47 KVM_PPC_GET_PVINFO
18391842
-----------------------
@@ -7673,6 +7676,7 @@ branch to guests' 0x200 interrupt vector.
76737676
:Architectures: x86
76747677
:Parameters: args[0] defines which exits are disabled
76757678
:Returns: 0 on success, -EINVAL when args[0] contains invalid exits
7679+
or if any vCPUs have already been created
76767680

76777681
Valid bits in args[0] are::
76787682

arch/x86/include/asm/kvm-x86-ops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ KVM_X86_OP(get_l2_tsc_multiplier)
9999
KVM_X86_OP(write_tsc_offset)
100100
KVM_X86_OP(write_tsc_multiplier)
101101
KVM_X86_OP(get_exit_info)
102+
KVM_X86_OP(get_entry_info)
102103
KVM_X86_OP(check_intercept)
103104
KVM_X86_OP(handle_exit_irqoff)
104105
KVM_X86_OP_OPTIONAL(update_cpu_dirty_logging)

arch/x86/include/asm/kvm_host.h

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,23 @@ struct kvm_queued_exception {
739739
bool has_payload;
740740
};
741741

742+
/*
743+
* Hardware-defined CPUID leafs that are either scattered by the kernel or are
744+
* unknown to the kernel, but need to be directly used by KVM. Note, these
745+
* word values conflict with the kernel's "bug" caps, but KVM doesn't use those.
746+
*/
747+
enum kvm_only_cpuid_leafs {
748+
CPUID_12_EAX = NCAPINTS,
749+
CPUID_7_1_EDX,
750+
CPUID_8000_0007_EDX,
751+
CPUID_8000_0022_EAX,
752+
CPUID_7_2_EDX,
753+
CPUID_24_0_EBX,
754+
NR_KVM_CPU_CAPS,
755+
756+
NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS,
757+
};
758+
742759
struct kvm_vcpu_arch {
743760
/*
744761
* rip and regs accesses must go through
@@ -854,27 +871,23 @@ struct kvm_vcpu_arch {
854871

855872
int cpuid_nent;
856873
struct kvm_cpuid_entry2 *cpuid_entries;
857-
struct kvm_hypervisor_cpuid kvm_cpuid;
858874
bool is_amd_compatible;
859875

860876
/*
861-
* FIXME: Drop this macro and use KVM_NR_GOVERNED_FEATURES directly
862-
* when "struct kvm_vcpu_arch" is no longer defined in an
863-
* arch/x86/include/asm header. The max is mostly arbitrary, i.e.
864-
* can be increased as necessary.
865-
*/
866-
#define KVM_MAX_NR_GOVERNED_FEATURES BITS_PER_LONG
867-
868-
/*
869-
* Track whether or not the guest is allowed to use features that are
870-
* governed by KVM, where "governed" means KVM needs to manage state
871-
* and/or explicitly enable the feature in hardware. Typically, but
872-
* not always, governed features can be used by the guest if and only
873-
* if both KVM and userspace want to expose the feature to the guest.
877+
* cpu_caps holds the effective guest capabilities, i.e. the features
878+
* the vCPU is allowed to use. Typically, but not always, features can
879+
* be used by the guest if and only if both KVM and userspace want to
880+
* expose the feature to the guest.
881+
*
882+
* A common exception is for virtualization holes, i.e. when KVM can't
883+
* prevent the guest from using a feature, in which case the vCPU "has"
884+
* the feature regardless of what KVM or userspace desires.
885+
*
886+
* Note, features that don't require KVM involvement in any way are
887+
* NOT enforced/sanitized by KVM, i.e. are taken verbatim from the
888+
* guest CPUID provided by userspace.
874889
*/
875-
struct {
876-
DECLARE_BITMAP(enabled, KVM_MAX_NR_GOVERNED_FEATURES);
877-
} governed_features;
890+
u32 cpu_caps[NR_KVM_CPU_CAPS];
878891

879892
u64 reserved_gpa_bits;
880893
int maxphyaddr;
@@ -1756,12 +1769,15 @@ struct kvm_x86_ops {
17561769
void (*write_tsc_multiplier)(struct kvm_vcpu *vcpu);
17571770

17581771
/*
1759-
* Retrieve somewhat arbitrary exit information. Intended to
1772+
* Retrieve somewhat arbitrary exit/entry information. Intended to
17601773
* be used only from within tracepoints or error paths.
17611774
*/
17621775
void (*get_exit_info)(struct kvm_vcpu *vcpu, u32 *reason,
17631776
u64 *info1, u64 *info2,
1764-
u32 *exit_int_info, u32 *exit_int_info_err_code);
1777+
u32 *intr_info, u32 *error_code);
1778+
1779+
void (*get_entry_info)(struct kvm_vcpu *vcpu,
1780+
u32 *intr_info, u32 *error_code);
17651781

17661782
int (*check_intercept)(struct kvm_vcpu *vcpu,
17671783
struct x86_instruction_info *info,
@@ -2018,8 +2034,8 @@ u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu);
20182034
* VMware backdoor emulation handles select instructions
20192035
* and reinjects the #GP for all other cases.
20202036
*
2021-
* EMULTYPE_PF - Set when emulating MMIO by way of an intercepted #PF, in which
2022-
* case the CR2/GPA value pass on the stack is valid.
2037+
* EMULTYPE_PF - Set when an intercepted #PF triggers the emulation, in which case
2038+
* the CR2/GPA value pass on the stack is valid.
20232039
*
20242040
* EMULTYPE_COMPLETE_USER_EXIT - Set when the emulator should update interruptibility
20252041
* state and inject single-step #DBs after skipping
@@ -2054,13 +2070,20 @@ u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu);
20542070
#define EMULTYPE_COMPLETE_USER_EXIT (1 << 7)
20552071
#define EMULTYPE_WRITE_PF_TO_SP (1 << 8)
20562072

2073+
static inline bool kvm_can_emulate_event_vectoring(int emul_type)
2074+
{
2075+
return !(emul_type & EMULTYPE_PF);
2076+
}
2077+
20572078
int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type);
20582079
int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
20592080
void *insn, int insn_len);
20602081
void __kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu,
20612082
u64 *data, u8 ndata);
20622083
void kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu);
20632084

2085+
void kvm_prepare_event_vectoring_exit(struct kvm_vcpu *vcpu, gpa_t gpa);
2086+
20642087
void kvm_enable_efer_bits(u64);
20652088
bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer);
20662089
int kvm_get_msr_with_filter(struct kvm_vcpu *vcpu, u32 index, u64 *data);

0 commit comments

Comments
 (0)