Skip to content

Commit

Permalink
KVM: SVM: Handle tsc in svm_get_msr/svm_set_msr correctly
Browse files Browse the repository at this point in the history
When running nested we need to touch the l1 guests
tsc_offset. Otherwise changes will be lost or a wrong value
be read.

Cc: stable@kernel.org
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
  • Loading branch information
Joerg Roedel authored and avikivity committed Oct 4, 2009
1 parent 77b1ab1 commit 20824f3
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions arch/x86/kvm/svm.c
Expand Up @@ -2059,10 +2059,14 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)

switch (ecx) {
case MSR_IA32_TSC: {
u64 tsc;
u64 tsc_offset;

rdtscll(tsc);
*data = svm->vmcb->control.tsc_offset + tsc;
if (is_nested(svm))
tsc_offset = svm->nested.hsave->control.tsc_offset;
else
tsc_offset = svm->vmcb->control.tsc_offset;

*data = tsc_offset + native_read_tsc();
break;
}
case MSR_K6_STAR:
Expand Down Expand Up @@ -2148,10 +2152,17 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)

switch (ecx) {
case MSR_IA32_TSC: {
u64 tsc;
u64 tsc_offset = 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;

rdtscll(tsc);
svm->vmcb->control.tsc_offset = data - tsc;
break;
}
case MSR_K6_STAR:
Expand Down

0 comments on commit 20824f3

Please sign in to comment.