Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e79f245

Browse files
KarimAllah Ahmedbonzini
authored andcommitted
X86/KVM: Properly update 'tsc_offset' to represent the running guest
Update 'tsc_offset' on vmentry/vmexit of L2 guests to ensure that it always captures the TSC_OFFSET of the running guest whether it is the L1 or L2 guest. Cc: Paolo Bonzini <[email protected]> Cc: Radim Krčmář <[email protected]> Cc: [email protected] Cc: [email protected] Reviewed-by: Jim Mattson <[email protected]> Suggested-by: Paolo Bonzini <[email protected]> Signed-off-by: KarimAllah Ahmed <[email protected]> [AMD changes, fix update_ia32_tsc_adjust_msr. - Paolo] Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 4e1acd7 commit e79f245

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,12 +1424,23 @@ static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
14241424
seg->base = 0;
14251425
}
14261426

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

14321442
if (is_guest_mode(vcpu)) {
1443+
/* Write L1's TSC offset. */
14331444
g_tsc_offset = svm->vmcb->control.tsc_offset -
14341445
svm->nested.hsave->control.tsc_offset;
14351446
svm->nested.hsave->control.tsc_offset = offset;
@@ -3323,6 +3334,7 @@ static int nested_svm_vmexit(struct vcpu_svm *svm)
33233334
/* Restore the original control entries */
33243335
copy_vmcb_control_area(vmcb, hsave);
33253336

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

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

3498+
svm->vcpu.arch.tsc_offset += nested_vmcb->control.tsc_offset;
3499+
svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
3500+
34863501
svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
34873502
svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
34883503
svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3489-
svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
34903504
svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
34913505
svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
34923506

@@ -7102,6 +7116,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
71027116

71037117
.has_wbinvd_exit = svm_has_wbinvd_exit,
71047118

7119+
.read_l1_tsc_offset = svm_read_l1_tsc_offset,
71057120
.write_tsc_offset = svm_write_tsc_offset,
71067121

71077122
.set_tdp_cr3 = set_tdp_cr3,

arch/x86/kvm/vmx.c

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,6 +2874,17 @@ static void setup_msrs(struct vcpu_vmx *vmx)
28742874
vmx_update_msr_bitmap(&vmx->vcpu);
28752875
}
28762876

2877+
static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
2878+
{
2879+
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2880+
2881+
if (is_guest_mode(vcpu) &&
2882+
(vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
2883+
return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
2884+
2885+
return vcpu->arch.tsc_offset;
2886+
}
2887+
28772888
/*
28782889
* reads and returns guest's timestamp counter "register"
28792890
* guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
@@ -11175,11 +11186,8 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
1117511186
vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
1117611187
}
1117711188

11178-
if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11179-
vmcs_write64(TSC_OFFSET,
11180-
vcpu->arch.tsc_offset + vmcs12->tsc_offset);
11181-
else
11182-
vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11189+
vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11190+
1118311191
if (kvm_has_tsc_control)
1118411192
decache_tsc_multiplier(vmx);
1118511193

@@ -11427,6 +11435,7 @@ static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
1142711435
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1142811436
u32 msr_entry_idx;
1142911437
u32 exit_qual;
11438+
int r;
1143011439

1143111440
enter_guest_mode(vcpu);
1143211441

@@ -11436,26 +11445,21 @@ static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
1143611445
vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
1143711446
vmx_segment_cache_clear(vmx);
1143811447

11439-
if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11440-
leave_guest_mode(vcpu);
11441-
vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11442-
nested_vmx_entry_failure(vcpu, vmcs12,
11443-
EXIT_REASON_INVALID_STATE, exit_qual);
11444-
return 1;
11445-
}
11448+
if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11449+
vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11450+
11451+
r = EXIT_REASON_INVALID_STATE;
11452+
if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual))
11453+
goto fail;
1144611454

1144711455
nested_get_vmcs12_pages(vcpu, vmcs12);
1144811456

11457+
r = EXIT_REASON_MSR_LOAD_FAIL;
1144911458
msr_entry_idx = nested_vmx_load_msr(vcpu,
1145011459
vmcs12->vm_entry_msr_load_addr,
1145111460
vmcs12->vm_entry_msr_load_count);
11452-
if (msr_entry_idx) {
11453-
leave_guest_mode(vcpu);
11454-
vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11455-
nested_vmx_entry_failure(vcpu, vmcs12,
11456-
EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
11457-
return 1;
11458-
}
11461+
if (msr_entry_idx)
11462+
goto fail;
1145911463

1146011464
/*
1146111465
* Note no nested_vmx_succeed or nested_vmx_fail here. At this point
@@ -11464,6 +11468,14 @@ static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
1146411468
* the success flag) when L2 exits (see nested_vmx_vmexit()).
1146511469
*/
1146611470
return 0;
11471+
11472+
fail:
11473+
if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11474+
vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11475+
leave_guest_mode(vcpu);
11476+
vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11477+
nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11478+
return 1;
1146711479
}
1146811480

1146911481
/*
@@ -12035,6 +12047,9 @@ static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1203512047

1203612048
leave_guest_mode(vcpu);
1203712049

12050+
if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12051+
vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12052+
1203812053
if (likely(!vmx->fail)) {
1203912054
if (exit_reason == -1)
1204012055
sync_vmcs12(vcpu, vmcs12);
@@ -12725,6 +12740,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
1272512740

1272612741
.has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
1272712742

12743+
.read_l1_tsc_offset = vmx_read_l1_tsc_offset,
1272812744
.write_tsc_offset = vmx_write_tsc_offset,
1272912745

1273012746
.set_tdp_cr3 = vmx_set_cr3,

arch/x86/kvm/x86.c

Lines changed: 4 additions & 2 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

0 commit comments

Comments
 (0)