Commit f4e1b3c8 authored by Zachary Amsden's avatar Zachary Amsden Committed by Avi Kivity

KVM: x86: Convert TSC writes to TSC offset writes

Change svm / vmx to be the same internally and write TSC offset
instead of bare TSC in helper functions.  Isolated as a single
patch to contain code movement.
Signed-off-by: default avatarZachary Amsden <zamsden@redhat.com>
Signed-off-by: default avatarMarcelo Tosatti <mtosatti@redhat.com>
parent ae38436b
...@@ -701,6 +701,20 @@ static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) ...@@ -701,6 +701,20 @@ static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
seg->base = 0; seg->base = 0;
} }
static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
{
struct vcpu_svm *svm = to_svm(vcpu);
u64 g_tsc_offset = 0;
if (is_nested(svm)) {
g_tsc_offset = svm->vmcb->control.tsc_offset -
svm->nested.hsave->control.tsc_offset;
svm->nested.hsave->control.tsc_offset = offset;
}
svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
}
static void init_vmcb(struct vcpu_svm *svm) static void init_vmcb(struct vcpu_svm *svm)
{ {
struct vmcb_control_area *control = &svm->vmcb->control; struct vmcb_control_area *control = &svm->vmcb->control;
...@@ -901,7 +915,7 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) ...@@ -901,7 +915,7 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT; svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
svm->asid_generation = 0; svm->asid_generation = 0;
init_vmcb(svm); init_vmcb(svm);
svm->vmcb->control.tsc_offset = 0-native_read_tsc(); svm_write_tsc_offset(&svm->vcpu, 0-native_read_tsc());
err = fx_init(&svm->vcpu); err = fx_init(&svm->vcpu);
if (err) if (err)
...@@ -2566,20 +2580,9 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data) ...@@ -2566,20 +2580,9 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
struct vcpu_svm *svm = to_svm(vcpu); struct vcpu_svm *svm = to_svm(vcpu);
switch (ecx) { switch (ecx) {
case MSR_IA32_TSC: { case MSR_IA32_TSC:
u64 tsc_offset = data - native_read_tsc(); svm_write_tsc_offset(vcpu, data - native_read_tsc());
u64 g_tsc_offset = 0;
if (is_nested(svm)) {
g_tsc_offset = svm->vmcb->control.tsc_offset -
svm->nested.hsave->control.tsc_offset;
svm->nested.hsave->control.tsc_offset = tsc_offset;
}
svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
break; break;
}
case MSR_STAR: case MSR_STAR:
svm->vmcb->save.star = data; svm->vmcb->save.star = data;
break; break;
......
...@@ -1149,9 +1149,9 @@ static u64 guest_read_tsc(void) ...@@ -1149,9 +1149,9 @@ static u64 guest_read_tsc(void)
* writes 'guest_tsc' into guest's timestamp counter "register" * writes 'guest_tsc' into guest's timestamp counter "register"
* guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
*/ */
static void guest_write_tsc(u64 guest_tsc, u64 host_tsc) static void vmx_write_tsc_offset(u64 offset)
{ {
vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc); vmcs_write64(TSC_OFFSET, offset);
} }
/* /*
...@@ -1255,7 +1255,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) ...@@ -1255,7 +1255,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
break; break;
case MSR_IA32_TSC: case MSR_IA32_TSC:
rdtscll(host_tsc); rdtscll(host_tsc);
guest_write_tsc(data, host_tsc); vmx_write_tsc_offset(data - host_tsc);
break; break;
case MSR_IA32_CR_PAT: case MSR_IA32_CR_PAT:
if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
...@@ -2512,7 +2512,7 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx) ...@@ -2512,7 +2512,7 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
{ {
u32 host_sysenter_cs, msr_low, msr_high; u32 host_sysenter_cs, msr_low, msr_high;
u32 junk; u32 junk;
u64 host_pat, tsc_this; u64 host_pat;
unsigned long a; unsigned long a;
struct desc_ptr dt; struct desc_ptr dt;
int i; int i;
...@@ -2653,8 +2653,7 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx) ...@@ -2653,8 +2653,7 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
tsc_this = native_read_tsc(); vmx_write_tsc_offset(0-native_read_tsc());
guest_write_tsc(0, tsc_this);
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment