Skip to content

Commit e6d9bfd

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull kvm fixes from Paolo Bonzini: "Bug fixes, plus a new test case and the associated infrastructure for writing nested virtualization tests" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: kvm: selftests: add vmx_tsc_adjust_test kvm: x86: move MSR_IA32_TSC handling to x86.c X86/KVM: Properly update 'tsc_offset' to represent the running guest kvm: selftests: add -std=gnu99 cflags x86: Add check for APIC access address for vmentry of L2 guests KVM: X86: fix incorrect reference of trace_kvm_pi_irte_update X86/KVM: Do not allow DISABLE_EXITS_MWAIT when LAPIC ARAT is not available kvm: selftests: fix spelling mistake: "divisable" and "divisible" X86/VMX: Disable VMX preemption timer if MWAIT is not intercepted
2 parents e6f39e8 + d5edb7f commit e6d9bfd

File tree

11 files changed

+1081
-73
lines changed

11 files changed

+1081
-73
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ struct kvm_x86_ops {
10131013

10141014
bool (*has_wbinvd_exit)(void);
10151015

1016+
u64 (*read_l1_tsc_offset)(struct kvm_vcpu *vcpu);
10161017
void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
10171018

10181019
void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2);

arch/x86/kvm/svm.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,12 +1423,23 @@ static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
14231423
seg->base = 0;
14241424
}
14251425

1426+
static u64 svm_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
1427+
{
1428+
struct vcpu_svm *svm = to_svm(vcpu);
1429+
1430+
if (is_guest_mode(vcpu))
1431+
return svm->nested.hsave->control.tsc_offset;
1432+
1433+
return vcpu->arch.tsc_offset;
1434+
}
1435+
14261436
static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
14271437
{
14281438
struct vcpu_svm *svm = to_svm(vcpu);
14291439
u64 g_tsc_offset = 0;
14301440

14311441
if (is_guest_mode(vcpu)) {
1442+
/* Write L1's TSC offset. */
14321443
g_tsc_offset = svm->vmcb->control.tsc_offset -
14331444
svm->nested.hsave->control.tsc_offset;
14341445
svm->nested.hsave->control.tsc_offset = offset;
@@ -3322,6 +3333,7 @@ static int nested_svm_vmexit(struct vcpu_svm *svm)
33223333
/* Restore the original control entries */
33233334
copy_vmcb_control_area(vmcb, hsave);
33243335

3336+
svm->vcpu.arch.tsc_offset = svm->vmcb->control.tsc_offset;
33253337
kvm_clear_exception_queue(&svm->vcpu);
33263338
kvm_clear_interrupt_queue(&svm->vcpu);
33273339

@@ -3482,10 +3494,12 @@ static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
34823494
/* We don't want to see VMMCALLs from a nested guest */
34833495
clr_intercept(svm, INTERCEPT_VMMCALL);
34843496

3497+
svm->vcpu.arch.tsc_offset += nested_vmcb->control.tsc_offset;
3498+
svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
3499+
34853500
svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
34863501
svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
34873502
svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3488-
svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
34893503
svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
34903504
svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
34913505

@@ -4035,12 +4049,6 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
40354049
struct vcpu_svm *svm = to_svm(vcpu);
40364050

40374051
switch (msr_info->index) {
4038-
case MSR_IA32_TSC: {
4039-
msr_info->data = svm->vmcb->control.tsc_offset +
4040-
kvm_scale_tsc(vcpu, rdtsc());
4041-
4042-
break;
4043-
}
40444052
case MSR_STAR:
40454053
msr_info->data = svm->vmcb->save.star;
40464054
break;
@@ -4193,9 +4201,6 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
41934201
svm->vmcb->save.g_pat = data;
41944202
mark_dirty(svm->vmcb, VMCB_NPT);
41954203
break;
4196-
case MSR_IA32_TSC:
4197-
kvm_write_tsc(vcpu, msr);
4198-
break;
41994204
case MSR_IA32_SPEC_CTRL:
42004205
if (!msr->host_initiated &&
42014206
!guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
@@ -5265,9 +5270,8 @@ static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
52655270
}
52665271

52675272
if (!ret && svm) {
5268-
trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
5269-
host_irq, e->gsi,
5270-
vcpu_info.vector,
5273+
trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id,
5274+
e->gsi, vcpu_info.vector,
52715275
vcpu_info.pi_desc_addr, set);
52725276
}
52735277

@@ -7102,6 +7106,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
71027106

71037107
.has_wbinvd_exit = svm_has_wbinvd_exit,
71047108

7109+
.read_l1_tsc_offset = svm_read_l1_tsc_offset,
71057110
.write_tsc_offset = svm_write_tsc_offset,
71067111

71077112
.set_tdp_cr3 = set_tdp_cr3,

arch/x86/kvm/vmx.c

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,18 +2880,15 @@ static void setup_msrs(struct vcpu_vmx *vmx)
28802880
vmx_update_msr_bitmap(&vmx->vcpu);
28812881
}
28822882

2883-
/*
2884-
* reads and returns guest's timestamp counter "register"
2885-
* guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2886-
* -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
2887-
*/
2888-
static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
2883+
static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
28892884
{
2890-
u64 host_tsc, tsc_offset;
2885+
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
28912886

2892-
host_tsc = rdtsc();
2893-
tsc_offset = vmcs_read64(TSC_OFFSET);
2894-
return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
2887+
if (is_guest_mode(vcpu) &&
2888+
(vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
2889+
return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
2890+
2891+
return vcpu->arch.tsc_offset;
28952892
}
28962893

28972894
/*
@@ -3524,9 +3521,6 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
35243521
#endif
35253522
case MSR_EFER:
35263523
return kvm_get_msr_common(vcpu, msr_info);
3527-
case MSR_IA32_TSC:
3528-
msr_info->data = guest_read_tsc(vcpu);
3529-
break;
35303524
case MSR_IA32_SPEC_CTRL:
35313525
if (!msr_info->host_initiated &&
35323526
!guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
@@ -3646,9 +3640,6 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
36463640
return 1;
36473641
vmcs_write64(GUEST_BNDCFGS, data);
36483642
break;
3649-
case MSR_IA32_TSC:
3650-
kvm_write_tsc(vcpu, msr_info);
3651-
break;
36523643
case MSR_IA32_SPEC_CTRL:
36533644
if (!msr_info->host_initiated &&
36543645
!guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
@@ -10608,6 +10599,16 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
1060810599
return true;
1060910600
}
1061010601

10602+
static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10603+
struct vmcs12 *vmcs12)
10604+
{
10605+
if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10606+
!page_address_valid(vcpu, vmcs12->apic_access_addr))
10607+
return -EINVAL;
10608+
else
10609+
return 0;
10610+
}
10611+
1061110612
static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
1061210613
struct vmcs12 *vmcs12)
1061310614
{
@@ -11176,11 +11177,8 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
1117611177
vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
1117711178
}
1117811179

11179-
if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11180-
vmcs_write64(TSC_OFFSET,
11181-
vcpu->arch.tsc_offset + vmcs12->tsc_offset);
11182-
else
11183-
vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11180+
vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11181+
1118411182
if (kvm_has_tsc_control)
1118511183
decache_tsc_multiplier(vmx);
1118611184

@@ -11299,6 +11297,9 @@ static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
1129911297
if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
1130011298
return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
1130111299

11300+
if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11301+
return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11302+
1130211303
if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
1130311304
return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
1130411305

@@ -11420,6 +11421,7 @@ static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
1142011421
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1142111422
u32 msr_entry_idx;
1142211423
u32 exit_qual;
11424+
int r;
1142311425

1142411426
enter_guest_mode(vcpu);
1142511427

@@ -11429,26 +11431,21 @@ static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
1142911431
vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
1143011432
vmx_segment_cache_clear(vmx);
1143111433

11432-
if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11433-
leave_guest_mode(vcpu);
11434-
vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11435-
nested_vmx_entry_failure(vcpu, vmcs12,
11436-
EXIT_REASON_INVALID_STATE, exit_qual);
11437-
return 1;
11438-
}
11434+
if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11435+
vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11436+
11437+
r = EXIT_REASON_INVALID_STATE;
11438+
if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual))
11439+
goto fail;
1143911440

1144011441
nested_get_vmcs12_pages(vcpu, vmcs12);
1144111442

11443+
r = EXIT_REASON_MSR_LOAD_FAIL;
1144211444
msr_entry_idx = nested_vmx_load_msr(vcpu,
1144311445
vmcs12->vm_entry_msr_load_addr,
1144411446
vmcs12->vm_entry_msr_load_count);
11445-
if (msr_entry_idx) {
11446-
leave_guest_mode(vcpu);
11447-
vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11448-
nested_vmx_entry_failure(vcpu, vmcs12,
11449-
EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
11450-
return 1;
11451-
}
11447+
if (msr_entry_idx)
11448+
goto fail;
1145211449

1145311450
/*
1145411451
* Note no nested_vmx_succeed or nested_vmx_fail here. At this point
@@ -11457,6 +11454,14 @@ static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
1145711454
* the success flag) when L2 exits (see nested_vmx_vmexit()).
1145811455
*/
1145911456
return 0;
11457+
11458+
fail:
11459+
if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11460+
vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11461+
leave_guest_mode(vcpu);
11462+
vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11463+
nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11464+
return 1;
1146011465
}
1146111466

1146211467
/*
@@ -12028,6 +12033,9 @@ static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1202812033

1202912034
leave_guest_mode(vcpu);
1203012035

12036+
if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12037+
vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12038+
1203112039
if (likely(!vmx->fail)) {
1203212040
if (exit_reason == -1)
1203312041
sync_vmcs12(vcpu, vmcs12);
@@ -12224,10 +12232,16 @@ static inline int u64_shl_div_u64(u64 a, unsigned int shift,
1222412232

1222512233
static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
1222612234
{
12227-
struct vcpu_vmx *vmx = to_vmx(vcpu);
12228-
u64 tscl = rdtsc();
12229-
u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12230-
u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
12235+
struct vcpu_vmx *vmx;
12236+
u64 tscl, guest_tscl, delta_tsc;
12237+
12238+
if (kvm_mwait_in_guest(vcpu->kvm))
12239+
return -EOPNOTSUPP;
12240+
12241+
vmx = to_vmx(vcpu);
12242+
tscl = rdtsc();
12243+
guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12244+
delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
1223112245

1223212246
/* Convert to host delta tsc if tsc scaling is enabled */
1223312247
if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
@@ -12533,7 +12547,7 @@ static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
1253312547
vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
1253412548
vcpu_info.vector = irq.vector;
1253512549

12536-
trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
12550+
trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
1253712551
vcpu_info.vector, vcpu_info.pi_desc_addr, set);
1253812552

1253912553
if (set)
@@ -12712,6 +12726,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
1271212726

1271312727
.has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
1271412728

12729+
.read_l1_tsc_offset = vmx_read_l1_tsc_offset,
1271512730
.write_tsc_offset = vmx_write_tsc_offset,
1271612731

1271712732
.set_tdp_cr3 = vmx_set_cr3,

arch/x86/kvm/x86.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
14901490

14911491
static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
14921492
{
1493-
u64 curr_offset = vcpu->arch.tsc_offset;
1493+
u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
14941494
vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
14951495
}
14961496

@@ -1532,7 +1532,9 @@ static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
15321532

15331533
u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
15341534
{
1535-
return vcpu->arch.tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
1535+
u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1536+
1537+
return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
15361538
}
15371539
EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
15381540

@@ -2362,6 +2364,9 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
23622364
return 1;
23632365
vcpu->arch.smbase = data;
23642366
break;
2367+
case MSR_IA32_TSC:
2368+
kvm_write_tsc(vcpu, msr_info);
2369+
break;
23652370
case MSR_SMI_COUNT:
23662371
if (!msr_info->host_initiated)
23672372
return 1;
@@ -2605,6 +2610,9 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
26052610
case MSR_IA32_UCODE_REV:
26062611
msr_info->data = vcpu->arch.microcode_version;
26072612
break;
2613+
case MSR_IA32_TSC:
2614+
msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
2615+
break;
26082616
case MSR_MTRRcap:
26092617
case 0x200 ... 0x2ff:
26102618
return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
@@ -2819,7 +2827,8 @@ static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
28192827
static inline bool kvm_can_mwait_in_guest(void)
28202828
{
28212829
return boot_cpu_has(X86_FEATURE_MWAIT) &&
2822-
!boot_cpu_has_bug(X86_BUG_MONITOR);
2830+
!boot_cpu_has_bug(X86_BUG_MONITOR) &&
2831+
boot_cpu_has(X86_FEATURE_ARAT);
28232832
}
28242833

28252834
int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)

tools/testing/selftests/kvm/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ top_srcdir = ../../../../
44
UNAME_M := $(shell uname -m)
55

66
LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c
7-
LIBKVM_x86_64 = lib/x86.c
7+
LIBKVM_x86_64 = lib/x86.c lib/vmx.c
88

99
TEST_GEN_PROGS_x86_64 = set_sregs_test
1010
TEST_GEN_PROGS_x86_64 += sync_regs_test
11+
TEST_GEN_PROGS_x86_64 += vmx_tsc_adjust_test
1112

1213
TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
1314
LIBKVM += $(LIBKVM_$(UNAME_M))
1415

1516
INSTALL_HDR_PATH = $(top_srcdir)/usr
1617
LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
17-
CFLAGS += -O2 -g -I$(LINUX_HDR_PATH) -Iinclude -I$(<D)
18+
CFLAGS += -O2 -g -std=gnu99 -I$(LINUX_HDR_PATH) -Iinclude -I$(<D)
1819

1920
# After inclusion, $(OUTPUT) is defined and
2021
# $(TEST_GEN_PROGS) starts with $(OUTPUT)/

tools/testing/selftests/kvm/include/kvm_util.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,27 @@ void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
112112
vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm,
113113
vm_paddr_t paddr_min, uint32_t memslot);
114114

115-
void kvm_get_supported_cpuid(struct kvm_cpuid2 *cpuid);
115+
struct kvm_cpuid2 *kvm_get_supported_cpuid(void);
116116
void vcpu_set_cpuid(
117117
struct kvm_vm *vm, uint32_t vcpuid, struct kvm_cpuid2 *cpuid);
118118

119-
struct kvm_cpuid2 *allocate_kvm_cpuid2(void);
120119
struct kvm_cpuid_entry2 *
121-
find_cpuid_index_entry(struct kvm_cpuid2 *cpuid, uint32_t function,
122-
uint32_t index);
120+
kvm_get_supported_cpuid_index(uint32_t function, uint32_t index);
123121

124122
static inline struct kvm_cpuid_entry2 *
125-
find_cpuid_entry(struct kvm_cpuid2 *cpuid, uint32_t function)
123+
kvm_get_supported_cpuid_entry(uint32_t function)
126124
{
127-
return find_cpuid_index_entry(cpuid, function, 0);
125+
return kvm_get_supported_cpuid_index(function, 0);
128126
}
129127

130128
struct kvm_vm *vm_create_default(uint32_t vcpuid, void *guest_code);
131129
void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code);
132130

131+
typedef void (*vmx_guest_code_t)(vm_vaddr_t vmxon_vaddr,
132+
vm_paddr_t vmxon_paddr,
133+
vm_vaddr_t vmcs_vaddr,
134+
vm_paddr_t vmcs_paddr);
135+
133136
struct kvm_userspace_memory_region *
134137
kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
135138
uint64_t end);

0 commit comments

Comments
 (0)