From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=40495 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PmYwr-00049T-9v for qemu-devel@nongnu.org; Mon, 07 Feb 2011 16:48:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PmYwk-0005Vk-5i for qemu-devel@nongnu.org; Mon, 07 Feb 2011 16:48:39 -0500 Received: from fmmailgate02.web.de ([217.72.192.227]:34626) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PmYwj-0005VE-Q4 for qemu-devel@nongnu.org; Mon, 07 Feb 2011 16:48:34 -0500 Message-ID: <4D5068AA.7040107@web.de> Date: Mon, 07 Feb 2011 22:48:26 +0100 From: Jan Kiszka MIME-Version: 1.0 Subject: Re: [Qemu-devel] [PATCH 15/15] kvm: x86: Introduce kvmclock device to save/restore its state References: In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigE9A71BEACAC150DBAE177426" Sender: jan.kiszka@web.de List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Blue Swirl Cc: Glauber Costa , Marcelo Tosatti , Avi Kivity , kvm@vger.kernel.org, qemu-devel@nongnu.org This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigE9A71BEACAC150DBAE177426 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 2011-02-07 20:39, Blue Swirl wrote: > On Mon, Feb 7, 2011 at 1:19 PM, Jan Kiszka wro= te: >> If kvmclock is used, which implies the kernel supports it, register a >> kvmclock device with the sysbus. Its main purpose is to save and resto= re >> the kernel state on migration, but this will also allow to visualize i= t >> one day. >> >> Signed-off-by: Jan Kiszka >> CC: Glauber Costa >> --- >> 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+=3D-lm >> endif >> >> -kvm.o kvm-all.o vhost.o vhost_net.o: QEMU_CFLAGS+=3D$(KVM_CFLAGS) >> +kvm.o kvm-all.o vhost.o vhost_net.o kvmclock.o: QEMU_CFLAGS+=3D$(KVM_= CFLAGS) >> >> config-target.h: config-target.h-timestamp >> config-target.h-timestamp: config-target.mak >> @@ -218,7 +218,7 @@ obj-i386-y +=3D cirrus_vga.o apic.o ioapic.o piix_= pci.o >> obj-i386-y +=3D vmmouse.o vmport.o hpet.o applesmc.o >> obj-i386-y +=3D device-hotplug.o pci-hotplug.o smbios.o wdt_ib700.o >> obj-i386-y +=3D debugcon.o multiboot.o >> -obj-i386-y +=3D pc_piix.o >> +obj-i386-y +=3D pc_piix.o kvmclock.o >=20 > Please build kvmclock.o conditionally to CONFIG_something... >=20 >> obj-i386-$(CONFIG_SPICE) +=3D 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 >> + * >> + * 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 >> +#include >> + >> +typedef struct KVMClockState { >> + SysBusDevice busdev; >> + uint64_t clock; >> + bool clock_valid; >> +} KVMClockState; >> + >> +static void kvmclock_pre_save(void *opaque) >> +{ >> + KVMClockState *s =3D opaque; >> + struct kvm_clock_data data; >> + int ret; >> + >> + if (s->clock_valid) { >> + return; >> + } >> + ret =3D kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data); >> + if (ret < 0) { >> + fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));= >> + data.clock =3D 0; >> + } >> + s->clock =3D data.clock; >> + /* >> + * If the VM is stopped, declare the clock state valid to avoid r= e-reading >> + * it on next vmsave (which would return a different value). Will= be reset >> + * when the VM is continued. >> + */ >> + s->clock_valid =3D !vm_running; >> +} >> + >> +static int kvmclock_post_load(void *opaque, int version_id) >> +{ >> + KVMClockState *s =3D opaque; >> + struct kvm_clock_data data; >> + >> + data.clock =3D s->clock; >> + data.flags =3D 0; >> + return kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data); >> +} >> + >> +static void kvmclock_vm_state_change(void *opaque, int running, int r= eason) >> +{ >> + KVMClockState *s =3D opaque; >> + >> + if (running) { >> + s->clock_valid =3D false; >> + } >> +} >> + >> +static int kvmclock_init(SysBusDevice *dev) >> +{ >> + KVMClockState *s =3D FROM_SYSBUS(KVMClockState, dev); >> + >> + qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s); >> + return 0; >> +} >> + >> +static const VMStateDescription kvmclock_vmsd =3D { >> + .name =3D "kvmclock", >> + .version_id =3D 1, >> + .minimum_version_id =3D 1, >> + .minimum_version_id_old =3D 1, >> + .pre_save =3D kvmclock_pre_save, >> + .post_load =3D kvmclock_post_load, >> + .fields =3D (VMStateField[]) { >> + VMSTATE_UINT64(clock, KVMClockState), >> + VMSTATE_END_OF_LIST() >> + } >> +}; >> + >> +static SysBusDeviceInfo kvmclock_info =3D { >> + .qdev.name =3D "kvmclock", >> + .qdev.size =3D sizeof(KVMClockState), >> + .qdev.vmsd =3D &kvmclock_vmsd, >> + .qdev.no_user =3D 1, >> + .init =3D kvmclock_init, >> +}; >> + >> +/* Note: Must be called after VCPU initialization. */ >> +void kvmclock_create(void) >> +{ >> + if (kvm_enabled() && >> + first_cpu->cpuid_kvm_features & (1ULL << KVM_FEATURE_CLOCKSOU= RCE)) { >> + sysbus_create_simple("kvmclock", -1, NULL); >> + } >> +} >=20 > ... 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. I played with this, and while it is generally a nice thing, it doesn't help us here. We would just push the logic around, from kvmclock.c to the header or even to configure (KVM_FEATURE_CLOCKSOURCE is not unconditionally available). I rather hope we finally agree on merging the required kvm headers into qemu so that all this usually broken #ifdef KVM_CAP_* can be removed. Jan --------------enigE9A71BEACAC150DBAE177426 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.15 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/ iEYEARECAAYFAk1QaK4ACgkQitSsb3rl5xTdLQCcCC+6XEqkcgxRhjJihtPnE7+q y8wAn2CLi57QYCcQ2ei5yT/TGYD3KYNz =WlRs -----END PGP SIGNATURE----- --------------enigE9A71BEACAC150DBAE177426--