Skip to content

Commit a836839

Browse files
bysuibonzini
authored andcommitted
KVM: x86/emulator: Emulate RDPID only if it is enabled in guest
When RDTSCP is supported but RDPID is not supported in host, RDPID emulation is available. However, __kvm_get_msr() would only fail when RDTSCP/RDPID both are disabled in guest, so the emulator wouldn't inject a #UD when RDPID is disabled but RDTSCP is enabled in guest. Fixes: fb6d4d3 ("KVM: x86: emulate RDPID") Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com> Message-Id: <1dfd46ae5b76d3ed87bde3154d51c64ea64c99c1.1646226788.git.houwenlong.hwl@antgroup.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent e644896 commit a836839

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

arch/x86/kvm/emulate.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3521,8 +3521,10 @@ static int em_rdpid(struct x86_emulate_ctxt *ctxt)
35213521
{
35223522
u64 tsc_aux = 0;
35233523

3524-
if (ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux))
3524+
if (!ctxt->ops->guest_has_rdpid(ctxt))
35253525
return emulate_ud(ctxt);
3526+
3527+
ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux);
35263528
ctxt->dst.val = tsc_aux;
35273529
return X86EMUL_CONTINUE;
35283530
}

arch/x86/kvm/kvm_emulate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ struct x86_emulate_ops {
226226
bool (*guest_has_long_mode)(struct x86_emulate_ctxt *ctxt);
227227
bool (*guest_has_movbe)(struct x86_emulate_ctxt *ctxt);
228228
bool (*guest_has_fxsr)(struct x86_emulate_ctxt *ctxt);
229+
bool (*guest_has_rdpid)(struct x86_emulate_ctxt *ctxt);
229230

230231
void (*set_nmi_mask)(struct x86_emulate_ctxt *ctxt, bool masked);
231232

arch/x86/kvm/x86.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7728,6 +7728,11 @@ static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt)
77287728
return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR);
77297729
}
77307730

7731+
static bool emulator_guest_has_rdpid(struct x86_emulate_ctxt *ctxt)
7732+
{
7733+
return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_RDPID);
7734+
}
7735+
77317736
static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
77327737
{
77337738
return kvm_register_read_raw(emul_to_vcpu(ctxt), reg);
@@ -7810,6 +7815,7 @@ static const struct x86_emulate_ops emulate_ops = {
78107815
.guest_has_long_mode = emulator_guest_has_long_mode,
78117816
.guest_has_movbe = emulator_guest_has_movbe,
78127817
.guest_has_fxsr = emulator_guest_has_fxsr,
7818+
.guest_has_rdpid = emulator_guest_has_rdpid,
78137819
.set_nmi_mask = emulator_set_nmi_mask,
78147820
.get_hflags = emulator_get_hflags,
78157821
.exiting_smm = emulator_exiting_smm,

0 commit comments

Comments
 (0)