Skip to content

Commit 8b3e34e

Browse files
Xiao Guangrongbonzini
authored andcommitted
KVM: x86: add pcommit support
Pass PCOMMIT CPU feature to guest to enable PCOMMIT instruction Currently we do not catch pcommit instruction for L1 guest and allow L1 to catch this instruction for L2 if, as required by the spec, L1 can enumerate the PCOMMIT instruction via CPUID: | IA32_VMX_PROCBASED_CTLS2[53] (which enumerates support for the | 1-setting of PCOMMIT exiting) is always the same as | CPUID.07H:EBX.PCOMMIT[bit 22]. Thus, software can set PCOMMIT exiting | to 1 if and only if the PCOMMIT instruction is enumerated via CPUID The spec can be found at https://software.intel.com/sites/default/files/managed/0d/53/319433-022.pdf Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent eb1c31b commit 8b3e34e

5 files changed

Lines changed: 42 additions & 8 deletions

File tree

‎arch/x86/include/asm/vmx.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
#define SECONDARY_EXEC_SHADOW_VMCS 0x00004000
7373
#define SECONDARY_EXEC_ENABLE_PML 0x00020000
7474
#define SECONDARY_EXEC_XSAVES 0x00100000
75-
75+
#define SECONDARY_EXEC_PCOMMIT 0x00200000
7676

7777
#define PIN_BASED_EXT_INTR_MASK 0x00000001
7878
#define PIN_BASED_NMI_EXITING 0x00000008

‎arch/x86/include/uapi/asm/vmx.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#define EXIT_REASON_PML_FULL 62
7979
#define EXIT_REASON_XSAVES 63
8080
#define EXIT_REASON_XRSTORS 64
81+
#define EXIT_REASON_PCOMMIT 65
8182

8283
#define VMX_EXIT_REASONS \
8384
{ EXIT_REASON_EXCEPTION_NMI, "EXCEPTION_NMI" }, \
@@ -126,7 +127,8 @@
126127
{ EXIT_REASON_INVVPID, "INVVPID" }, \
127128
{ EXIT_REASON_INVPCID, "INVPCID" }, \
128129
{ EXIT_REASON_XSAVES, "XSAVES" }, \
129-
{ EXIT_REASON_XRSTORS, "XRSTORS" }
130+
{ EXIT_REASON_XRSTORS, "XRSTORS" }, \
131+
{ EXIT_REASON_PCOMMIT, "PCOMMIT" }
130132

131133
#define VMX_ABORT_SAVE_GUEST_MSR_FAIL 1
132134
#define VMX_ABORT_LOAD_HOST_MSR_FAIL 4

‎arch/x86/kvm/cpuid.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
348348
F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
349349
F(BMI2) | F(ERMS) | f_invpcid | F(RTM) | f_mpx | F(RDSEED) |
350350
F(ADX) | F(SMAP) | F(AVX512F) | F(AVX512PF) | F(AVX512ER) |
351-
F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB);
351+
F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(PCOMMIT);
352352

353353
/* cpuid 0xD.1.eax */
354354
const u32 kvm_supported_word10_x86_features =

‎arch/x86/kvm/cpuid.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,12 @@ static inline bool guest_cpuid_has_mpx(struct kvm_vcpu *vcpu)
133133
best = kvm_find_cpuid_entry(vcpu, 7, 0);
134134
return best && (best->ebx & bit(X86_FEATURE_MPX));
135135
}
136+
137+
static inline bool guest_cpuid_has_pcommit(struct kvm_vcpu *vcpu)
138+
{
139+
struct kvm_cpuid_entry2 *best;
140+
141+
best = kvm_find_cpuid_entry(vcpu, 7, 0);
142+
return best && (best->ebx & bit(X86_FEATURE_PCOMMIT));
143+
}
136144
#endif

‎arch/x86/kvm/vmx.c‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,7 +2475,8 @@ static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
24752475
SECONDARY_EXEC_APIC_REGISTER_VIRT |
24762476
SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
24772477
SECONDARY_EXEC_WBINVD_EXITING |
2478-
SECONDARY_EXEC_XSAVES;
2478+
SECONDARY_EXEC_XSAVES |
2479+
SECONDARY_EXEC_PCOMMIT;
24792480

24802481
if (enable_ept) {
24812482
/* nested EPT: emulate EPT also to L1 */
@@ -3016,7 +3017,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
30163017
SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
30173018
SECONDARY_EXEC_SHADOW_VMCS |
30183019
SECONDARY_EXEC_XSAVES |
3019-
SECONDARY_EXEC_ENABLE_PML;
3020+
SECONDARY_EXEC_ENABLE_PML |
3021+
SECONDARY_EXEC_PCOMMIT;
30203022
if (adjust_vmx_controls(min2, opt2,
30213023
MSR_IA32_VMX_PROCBASED_CTLS2,
30223024
&_cpu_based_2nd_exec_control) < 0)
@@ -4571,6 +4573,9 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
45714573
/* PML is enabled/disabled in creating/destorying vcpu */
45724574
exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
45734575

4576+
/* Currently, we allow L1 guest to directly run pcommit instruction. */
4577+
exec_control &= ~SECONDARY_EXEC_PCOMMIT;
4578+
45744579
return exec_control;
45754580
}
45764581

@@ -4614,10 +4619,9 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
46144619

46154620
vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
46164621

4617-
if (cpu_has_secondary_exec_ctrls()) {
4622+
if (cpu_has_secondary_exec_ctrls())
46184623
vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
46194624
vmx_secondary_exec_control(vmx));
4620-
}
46214625

46224626
if (vmx_cpu_uses_apicv(&vmx->vcpu)) {
46234627
vmcs_write64(EOI_EXIT_BITMAP0, 0);
@@ -7212,6 +7216,13 @@ static int handle_pml_full(struct kvm_vcpu *vcpu)
72127216
return 1;
72137217
}
72147218

7219+
static int handle_pcommit(struct kvm_vcpu *vcpu)
7220+
{
7221+
/* we never catch pcommit instruct for L1 guest. */
7222+
WARN_ON(1);
7223+
return 1;
7224+
}
7225+
72157226
/*
72167227
* The exit handlers return 1 if the exit was handled fully and guest execution
72177228
* may resume. Otherwise they set the kvm_run parameter to indicate what needs
@@ -7262,6 +7273,7 @@ static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
72627273
[EXIT_REASON_XSAVES] = handle_xsaves,
72637274
[EXIT_REASON_XRSTORS] = handle_xrstors,
72647275
[EXIT_REASON_PML_FULL] = handle_pml_full,
7276+
[EXIT_REASON_PCOMMIT] = handle_pcommit,
72657277
};
72667278

72677279
static const int kvm_vmx_max_exit_handlers =
@@ -7563,6 +7575,8 @@ static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
75637575
* the XSS exit bitmap in vmcs12.
75647576
*/
75657577
return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
7578+
case EXIT_REASON_PCOMMIT:
7579+
return nested_cpu_has2(vmcs12, SECONDARY_EXEC_PCOMMIT);
75667580
default:
75677581
return true;
75687582
}
@@ -8697,6 +8711,15 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
86978711
if (best)
86988712
best->ebx &= ~bit(X86_FEATURE_INVPCID);
86998713
}
8714+
8715+
if (static_cpu_has(X86_FEATURE_PCOMMIT) && nested) {
8716+
if (guest_cpuid_has_pcommit(vcpu))
8717+
vmx->nested.nested_vmx_secondary_ctls_high |=
8718+
SECONDARY_EXEC_PCOMMIT;
8719+
else
8720+
vmx->nested.nested_vmx_secondary_ctls_high &=
8721+
~SECONDARY_EXEC_PCOMMIT;
8722+
}
87008723
}
87018724

87028725
static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
@@ -9310,7 +9333,8 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
93109333
exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
93119334
SECONDARY_EXEC_RDTSCP |
93129335
SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
9313-
SECONDARY_EXEC_APIC_REGISTER_VIRT);
9336+
SECONDARY_EXEC_APIC_REGISTER_VIRT |
9337+
SECONDARY_EXEC_PCOMMIT);
93149338
if (nested_cpu_has(vmcs12,
93159339
CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
93169340
exec_control |= vmcs12->secondary_vm_exec_control;

0 commit comments

Comments
 (0)