[patch 1/3] QEMU/KVM: x86: separate TSC load from kvm_arch_load_regs

Marcelo Tosatti
2008-12-09T01:25:54Z

[-- Attachment #1: kvm-load-tsc --]
[-- Type: text/plain, Size: 2490 bytes --]

kvm_load_registers is a general interface to load registers, and is
used by vmport, gdbstub, etc. The TSC MSR is continually counting, so
it can't be simply read and written back as the other registers/MSR's
(doing so overwrites the current count).

Introduce kvm_load_tsc and use it for x86's migration CPU load code.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

Index: kvm-userspace.tip/qemu/qemu-kvm-x86.c
===================================================================
--- kvm-userspace.tip.orig/qemu/qemu-kvm-x86.c
+++ kvm-userspace.tip/qemu/qemu-kvm-x86.c
@@ -257,7 +257,6 @@ void kvm_arch_load_regs(CPUState *env)
set_msr_entry(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
if (kvm_has_msr_star)
set_msr_entry(&msrs[n++], MSR_STAR, env->star);
- set_msr_entry(&msrs[n++], MSR_IA32_TSC, env->tsc);
#ifdef TARGET_X86_64
if (lm_capable_kernel) {
set_msr_entry(&msrs[n++], MSR_CSTAR, env->cstar);
@@ -272,6 +271,18 @@ void kvm_arch_load_regs(CPUState *env)
perror("kvm_set_msrs FAILED");
}

+void kvm_load_tsc(CPUState *env)
+{
+ int rc;
+ struct kvm_msr_entry msr;
+
+ set_msr_entry(&msr, MSR_IA32_TSC, env->tsc);
+
+ rc = kvm_set_msrs(kvm_context, env->cpu_index, &msr, 1);
+ if (rc == -1)
+ perror("kvm_set_tsc FAILED.\n");
+}
+
void kvm_save_mpstate(CPUState *env)
{
#ifdef KVM_CAP_MP_STATE
Index: kvm-userspace.tip/qemu/qemu-kvm-x86.h
===================================================================
--- /dev/null
+++ kvm-userspace.tip/qemu/qemu-kvm-x86.h
@@ -0,0 +1,13 @@
+/*
+ * qemu/kvm integration
+ *
+ * Copyright (C) 2006-2008 Qumranet Technologies
+ *
+ * Licensed under the terms of the GNU GPL version 2 or higher.
+ */
+#ifndef QEMU_KVM_X86
+#define QEMU_KVM_X86
+
+void kvm_load_tsc(CPUState *env);
+
+#endif
Index: kvm-userspace.tip/qemu/target-i386/machine.c
===================================================================
--- kvm-userspace.tip.orig/qemu/target-i386/machine.c
+++ kvm-userspace.tip/qemu/target-i386/machine.c
@@ -5,6 +5,7 @@

#include "exec-all.h"
#include "qemu-kvm.h"
+#include "qemu-kvm-x86.h"

void register_machines(void)
{
@@ -328,6 +329,7 @@ int cpu_load(QEMUFile *f, void *opaque,
}
qemu_get_be64s(f, &env->tsc);
kvm_load_registers(env);
+ kvm_load_tsc(env);
if (version_id >= 5) {
qemu_get_be32s(f, &env->mp_state);
kvm_load_mpstate(env);

--