Re: [Qemu-devel] [PATCH 15/15] kvm: x86: Introduce kvmclock device to save/restore its state

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Feb 7, 2011 at 1:19 PM, Jan Kiszka <jan.kiszka@xxxxxxxxxxx> wrote:
> If kvmclock is used, which implies the kernel supports it, register a
> kvmclock device with the sysbus. Its main purpose is to save and restore
> the kernel state on migration, but this will also allow to visualize it
> one day.
>
> Signed-off-by: Jan Kiszka <jan.kiszka@xxxxxxxxxxx>
> CC: Glauber Costa <glommer@xxxxxxxxxx>
> ---
> ÂMakefile.target | Â Â4 +-
> Âhw/kvmclock.c  | Â125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
> Âhw/kvmclock.h  |  14 ++++++
> Âhw/pc_piix.c  Â|  31 +++++++++++---
> Â4 files changed, 165 insertions(+), 9 deletions(-)
> Âcreate mode 100644 hw/kvmclock.c
> Âcreate mode 100644 hw/kvmclock.h
>
> diff --git a/Makefile.target b/Makefile.target
> index b0ba95f..30232fa 100644
> --- a/Makefile.target
> +++ b/Makefile.target
> @@ -37,7 +37,7 @@ ifndef CONFIG_HAIKU
> ÂLIBS+=-lm
> Âendif
>
> -kvm.o kvm-all.o vhost.o vhost_net.o: QEMU_CFLAGS+=$(KVM_CFLAGS)
> +kvm.o kvm-all.o vhost.o vhost_net.o kvmclock.o: QEMU_CFLAGS+=$(KVM_CFLAGS)
>
> Âconfig-target.h: config-target.h-timestamp
> Âconfig-target.h-timestamp: config-target.mak
> @@ -218,7 +218,7 @@ obj-i386-y += cirrus_vga.o apic.o ioapic.o piix_pci.o
> Âobj-i386-y += vmmouse.o vmport.o hpet.o applesmc.o
> Âobj-i386-y += device-hotplug.o pci-hotplug.o smbios.o wdt_ib700.o
> Âobj-i386-y += debugcon.o multiboot.o
> -obj-i386-y += pc_piix.o
> +obj-i386-y += pc_piix.o kvmclock.o

Please build kvmclock.o conditionally to CONFIG_something...

> Âobj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
>
> Â# shared objects
> diff --git a/hw/kvmclock.c b/hw/kvmclock.c
> new file mode 100644
> index 0000000..b6ceddf
> --- /dev/null
> +++ b/hw/kvmclock.c
> @@ -0,0 +1,125 @@
> +/*
> + * QEMU KVM support, paravirtual clock device
> + *
> + * Copyright (C) 2011 Siemens AG
> + *
> + * Authors:
> + * ÂJan Kiszka    Â<jan.kiszka@xxxxxxxxxxx>
> + *
> + * This work is licensed under the terms of the GNU GPL version 2.
> + * See the COPYING file in the top-level directory.
> + *
> + */
> +
> +#include "qemu-common.h"
> +#include "sysemu.h"
> +#include "sysbus.h"
> +#include "kvm.h"
> +#include "kvmclock.h"
> +
> +#if defined(CONFIG_KVM_PARA) && defined(KVM_CAP_ADJUST_CLOCK)
> +
> +#include <linux/kvm.h>
> +#include <linux/kvm_para.h>
> +
> +typedef struct KVMClockState {
> + Â ÂSysBusDevice busdev;
> + Â Âuint64_t clock;
> + Â Âbool clock_valid;
> +} KVMClockState;
> +
> +static void kvmclock_pre_save(void *opaque)
> +{
> + Â ÂKVMClockState *s = opaque;
> + Â Âstruct kvm_clock_data data;
> + Â Âint ret;
> +
> + Â Âif (s->clock_valid) {
> + Â Â Â Âreturn;
> + Â Â}
> + Â Âret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
> + Â Âif (ret < 0) {
> + Â Â Â Âfprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
> + Â Â Â Âdata.clock = 0;
> + Â Â}
> + Â Âs->clock = data.clock;
> + Â Â/*
> + Â Â * If the VM is stopped, declare the clock state valid to avoid re-reading
> + Â Â * it on next vmsave (which would return a different value). Will be reset
> + Â Â * when the VM is continued.
> + Â Â */
> + Â Âs->clock_valid = !vm_running;
> +}
> +
> +static int kvmclock_post_load(void *opaque, int version_id)
> +{
> + Â ÂKVMClockState *s = opaque;
> + Â Âstruct kvm_clock_data data;
> +
> + Â Âdata.clock = s->clock;
> + Â Âdata.flags = 0;
> + Â Âreturn kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
> +}
> +
> +static void kvmclock_vm_state_change(void *opaque, int running, int reason)
> +{
> + Â ÂKVMClockState *s = opaque;
> +
> + Â Âif (running) {
> + Â Â Â Âs->clock_valid = false;
> + Â Â}
> +}
> +
> +static int kvmclock_init(SysBusDevice *dev)
> +{
> + Â ÂKVMClockState *s = FROM_SYSBUS(KVMClockState, dev);
> +
> + Â Âqemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
> + Â Âreturn 0;
> +}
> +
> +static const VMStateDescription kvmclock_vmsd = {
> + Â Â.name = "kvmclock",
> + Â Â.version_id = 1,
> + Â Â.minimum_version_id = 1,
> + Â Â.minimum_version_id_old = 1,
> + Â Â.pre_save = kvmclock_pre_save,
> + Â Â.post_load = kvmclock_post_load,
> + Â Â.fields = (VMStateField[]) {
> + Â Â Â ÂVMSTATE_UINT64(clock, KVMClockState),
> + Â Â Â ÂVMSTATE_END_OF_LIST()
> + Â Â}
> +};
> +
> +static SysBusDeviceInfo kvmclock_info = {
> + Â Â.qdev.name = "kvmclock",
> + Â Â.qdev.size = sizeof(KVMClockState),
> + Â Â.qdev.vmsd = &kvmclock_vmsd,
> + Â Â.qdev.no_user = 1,
> + Â Â.init = kvmclock_init,
> +};
> +
> +/* Note: Must be called after VCPU initialization. */
> +void kvmclock_create(void)
> +{
> + Â Âif (kvm_enabled() &&
> + Â Â Â Âfirst_cpu->cpuid_kvm_features & (1ULL << KVM_FEATURE_CLOCKSOURCE)) {
> + Â Â Â Âsysbus_create_simple("kvmclock", -1, NULL);
> + Â Â}
> +}

... and with this moved to a header as a static inline function, it
should be possible to use sysbus_try_create() (coming soon) to try to
create the device. Then it's not fatal if the device can't be created,
that just means that the capability was not available at build time.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux