@@ -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 ,
0 commit comments