mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-18 23:52:14 -06:00

commit 33cd52b5d7
unset
cannot_instantiate_with_device_add_yet in TYPE_SYSBUS, making all
sysbus devices appear on "-device help" and lack the "no-user"
flag in "info qdm".
To fix this, we can set user_creatable=false by default on
TYPE_SYS_BUS_DEVICE, but this requires setting
user_creatable=true explicitly on the sysbus devices that
actually work with -device.
Fortunately today we have just a few has_dynamic_sysbus=1
machines: virt, pc-q35-*, ppce500, and spapr.
virt, ppce500, and spapr have extra checks to ensure just a few
device types can be instantiated:
* virt supports only TYPE_VFIO_CALXEDA_XGMAC, TYPE_VFIO_AMD_XGBE.
* ppce500 supports only TYPE_ETSEC_COMMON.
* spapr supports only TYPE_SPAPR_PCI_HOST_BRIDGE.
This patch sets user_creatable=true explicitly on those 4 device
classes.
Now, the more complex cases:
pc-q35-*: q35 has no sysbus device whitelist yet (which is a
separate bug). We are in the process of fixing it and building a
sysbus whitelist on q35, but in the meantime we can fix the
"-device help" and "info qdm" bugs mentioned above. Also, despite
not being strictly necessary for fixing the q35 bug, reducing the
list of user_creatable=true devices will help us be more
confident when building the q35 whitelist.
xen: We also have a hack at xen_set_dynamic_sysbus(), that sets
has_dynamic_sysbus=true at runtime when using the Xen
accelerator. This hack is only used to allow xen-backend devices
to be dynamically plugged/unplugged.
This means today we can use -device with the following 22 device
types, that are the ones compiled into the qemu-system-x86_64 and
qemu-system-i386 binaries:
* allwinner-ahci
* amd-iommu
* cfi.pflash01
* esp
* fw_cfg_io
* fw_cfg_mem
* generic-sdhci
* hpet
* intel-iommu
* ioapic
* isabus-bridge
* kvmclock
* kvm-ioapic
* kvmvapic
* SUNW,fdtwo
* sysbus-ahci
* sysbus-fdc
* sysbus-ohci
* unimplemented-device
* virtio-mmio
* xen-backend
* xen-sysdev
This patch adds user_creatable=true explicitly to those devices,
temporarily, just to keep 100% compatibility with existing
behavior of q35. Subsequent patches will remove
user_creatable=true from the devices that are really not meant to
user-creatable on any machine, and remove the FIXME comment from
the ones that are really supposed to be user-creatable. This is
being done in separate patches because we still don't have an
obvious list of devices that will be whitelisted by q35, and I
would like to get each device reviewed individually.
Cc: Alexander Graf <agraf@suse.de>
Cc: Alex Williamson <alex.williamson@redhat.com>
Cc: Alistair Francis <alistair.francis@xilinx.com>
Cc: Beniamino Galvani <b.galvani@gmail.com>
Cc: Christian Borntraeger <borntraeger@de.ibm.com>
Cc: Cornelia Huck <cornelia.huck@de.ibm.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Cc: "Edgar E. Iglesias" <edgar.iglesias@gmail.com>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Cc: Frank Blaschka <frank.blaschka@de.ibm.com>
Cc: Gabriel L. Somlo <somlo@cmu.edu>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: John Snow <jsnow@redhat.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Kevin Wolf <kwolf@redhat.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Marcel Apfelbaum <marcel@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
Cc: Max Reitz <mreitz@redhat.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Maydell <peter.maydell@linaro.org>
Cc: Pierre Morel <pmorel@linux.vnet.ibm.com>
Cc: Prasad J Pandit <pjp@fedoraproject.org>
Cc: qemu-arm@nongnu.org
Cc: qemu-block@nongnu.org
Cc: qemu-ppc@nongnu.org
Cc: Richard Henderson <rth@twiddle.net>
Cc: Rob Herring <robh@kernel.org>
Cc: Shannon Zhao <zhaoshenglong@huawei.com>
Cc: sstabellini@kernel.org
Cc: Thomas Huth <thuth@redhat.com>
Cc: Yi Min Zhao <zyimin@linux.vnet.ibm.com>
Acked-by: John Snow <jsnow@redhat.com>
Acked-by: Juergen Gross <jgross@suse.com>
Acked-by: Marcel Apfelbaum <marcel@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Message-Id: <20170503203604.31462-3-ehabkost@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[ehabkost: Small changes at sysbus_device_class_init() comments]
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
320 lines
8.9 KiB
C
320 lines
8.9 KiB
C
/*
|
|
* QEMU KVM support, paravirtual clock device
|
|
*
|
|
* Copyright (C) 2011 Siemens AG
|
|
*
|
|
* Authors:
|
|
* Jan Kiszka <jan.kiszka@siemens.com>
|
|
*
|
|
* This work is licensed under the terms of the GNU GPL version 2.
|
|
* See the COPYING file in the top-level directory.
|
|
*
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
|
*/
|
|
|
|
#include "qemu/osdep.h"
|
|
#include "qemu-common.h"
|
|
#include "cpu.h"
|
|
#include "qemu/host-utils.h"
|
|
#include "sysemu/sysemu.h"
|
|
#include "sysemu/kvm.h"
|
|
#include "kvm_i386.h"
|
|
#include "hw/sysbus.h"
|
|
#include "hw/kvm/clock.h"
|
|
#include "qapi/error.h"
|
|
|
|
#include <linux/kvm.h>
|
|
#include <linux/kvm_para.h>
|
|
|
|
#define TYPE_KVM_CLOCK "kvmclock"
|
|
#define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
|
|
|
|
typedef struct KVMClockState {
|
|
/*< private >*/
|
|
SysBusDevice busdev;
|
|
/*< public >*/
|
|
|
|
uint64_t clock;
|
|
bool clock_valid;
|
|
|
|
/* whether machine type supports reliable KVM_GET_CLOCK */
|
|
bool mach_use_reliable_get_clock;
|
|
|
|
/* whether the 'clock' value was obtained in a host with
|
|
* reliable KVM_GET_CLOCK */
|
|
bool clock_is_reliable;
|
|
} KVMClockState;
|
|
|
|
struct pvclock_vcpu_time_info {
|
|
uint32_t version;
|
|
uint32_t pad0;
|
|
uint64_t tsc_timestamp;
|
|
uint64_t system_time;
|
|
uint32_t tsc_to_system_mul;
|
|
int8_t tsc_shift;
|
|
uint8_t flags;
|
|
uint8_t pad[2];
|
|
} __attribute__((__packed__)); /* 32 bytes */
|
|
|
|
static uint64_t kvmclock_current_nsec(KVMClockState *s)
|
|
{
|
|
CPUState *cpu = first_cpu;
|
|
CPUX86State *env = cpu->env_ptr;
|
|
hwaddr kvmclock_struct_pa = env->system_time_msr & ~1ULL;
|
|
uint64_t migration_tsc = env->tsc;
|
|
struct pvclock_vcpu_time_info time;
|
|
uint64_t delta;
|
|
uint64_t nsec_lo;
|
|
uint64_t nsec_hi;
|
|
uint64_t nsec;
|
|
|
|
if (!(env->system_time_msr & 1ULL)) {
|
|
/* KVM clock not active */
|
|
return 0;
|
|
}
|
|
|
|
cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
|
|
|
|
assert(time.tsc_timestamp <= migration_tsc);
|
|
delta = migration_tsc - time.tsc_timestamp;
|
|
if (time.tsc_shift < 0) {
|
|
delta >>= -time.tsc_shift;
|
|
} else {
|
|
delta <<= time.tsc_shift;
|
|
}
|
|
|
|
mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul);
|
|
nsec = (nsec_lo >> 32) | (nsec_hi << 32);
|
|
return nsec + time.system_time;
|
|
}
|
|
|
|
static void kvm_update_clock(KVMClockState *s)
|
|
{
|
|
struct kvm_clock_data data;
|
|
int ret;
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
|
|
if (ret < 0) {
|
|
fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
|
|
abort();
|
|
}
|
|
s->clock = data.clock;
|
|
|
|
/* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
|
|
* essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
|
|
* can drift from the TSC-based value that is computed by the guest,
|
|
* so we need to go through kvmclock_current_nsec(). If
|
|
* kvm_has_adjust_clock_stable() is true, and the flags contain
|
|
* KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
|
|
* and kvmclock_current_nsec() is not necessary.
|
|
*
|
|
* Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
|
|
*
|
|
* - if the host has disabled the kvmclock master clock, the guest already
|
|
* has protection against time going backwards. This "safety net" is only
|
|
* absent when kvmclock is stable;
|
|
*
|
|
* - therefore, we can replace a check like
|
|
*
|
|
* if last KVM_GET_CLOCK was not reliable then
|
|
* read from memory
|
|
*
|
|
* with
|
|
*
|
|
* if last KVM_GET_CLOCK was not reliable && masterclock is enabled
|
|
* read from memory
|
|
*
|
|
* However:
|
|
*
|
|
* - if kvm_has_adjust_clock_stable() returns false, the left side is
|
|
* always true (KVM_GET_CLOCK is never reliable), and the right side is
|
|
* unknown (because we don't have data.flags). We must assume it's true
|
|
* and read from memory.
|
|
*
|
|
* - if kvm_has_adjust_clock_stable() returns true, the result of the &&
|
|
* is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
|
|
*
|
|
* So we can just use this instead:
|
|
*
|
|
* if !kvm_has_adjust_clock_stable() then
|
|
* read from memory
|
|
*/
|
|
s->clock_is_reliable = kvm_has_adjust_clock_stable();
|
|
}
|
|
|
|
static void kvmclock_vm_state_change(void *opaque, int running,
|
|
RunState state)
|
|
{
|
|
KVMClockState *s = opaque;
|
|
CPUState *cpu;
|
|
int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
|
|
int ret;
|
|
|
|
if (running) {
|
|
struct kvm_clock_data data = {};
|
|
|
|
/*
|
|
* If the host where s->clock was read did not support reliable
|
|
* KVM_GET_CLOCK, read kvmclock value from memory.
|
|
*/
|
|
if (!s->clock_is_reliable) {
|
|
uint64_t pvclock_via_mem = kvmclock_current_nsec(s);
|
|
/* We can't rely on the saved clock value, just discard it */
|
|
if (pvclock_via_mem) {
|
|
s->clock = pvclock_via_mem;
|
|
}
|
|
}
|
|
|
|
s->clock_valid = false;
|
|
|
|
data.clock = s->clock;
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
|
|
if (ret < 0) {
|
|
fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(ret));
|
|
abort();
|
|
}
|
|
|
|
if (!cap_clock_ctrl) {
|
|
return;
|
|
}
|
|
CPU_FOREACH(cpu) {
|
|
ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
|
|
if (ret) {
|
|
if (ret != -EINVAL) {
|
|
fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
} else {
|
|
|
|
if (s->clock_valid) {
|
|
return;
|
|
}
|
|
|
|
kvm_synchronize_all_tsc();
|
|
|
|
kvm_update_clock(s);
|
|
/*
|
|
* 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 = true;
|
|
}
|
|
}
|
|
|
|
static void kvmclock_realize(DeviceState *dev, Error **errp)
|
|
{
|
|
KVMClockState *s = KVM_CLOCK(dev);
|
|
|
|
if (!kvm_enabled()) {
|
|
error_setg(errp, "kvmclock device requires KVM");
|
|
return;
|
|
}
|
|
|
|
kvm_update_clock(s);
|
|
|
|
qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
|
|
}
|
|
|
|
static bool kvmclock_clock_is_reliable_needed(void *opaque)
|
|
{
|
|
KVMClockState *s = opaque;
|
|
|
|
return s->mach_use_reliable_get_clock;
|
|
}
|
|
|
|
static const VMStateDescription kvmclock_reliable_get_clock = {
|
|
.name = "kvmclock/clock_is_reliable",
|
|
.version_id = 1,
|
|
.minimum_version_id = 1,
|
|
.needed = kvmclock_clock_is_reliable_needed,
|
|
.fields = (VMStateField[]) {
|
|
VMSTATE_BOOL(clock_is_reliable, KVMClockState),
|
|
VMSTATE_END_OF_LIST()
|
|
}
|
|
};
|
|
|
|
/*
|
|
* When migrating, read the clock just before migration,
|
|
* so that the guest clock counts during the events
|
|
* between:
|
|
*
|
|
* * vm_stop()
|
|
* *
|
|
* * pre_save()
|
|
*
|
|
* This reduces kvmclock difference on migration from 5s
|
|
* to 0.1s (when max_downtime == 5s), because sending the
|
|
* final pages of memory (which happens between vm_stop()
|
|
* and pre_save()) takes max_downtime.
|
|
*/
|
|
static void kvmclock_pre_save(void *opaque)
|
|
{
|
|
KVMClockState *s = opaque;
|
|
|
|
kvm_update_clock(s);
|
|
}
|
|
|
|
static const VMStateDescription kvmclock_vmsd = {
|
|
.name = "kvmclock",
|
|
.version_id = 1,
|
|
.minimum_version_id = 1,
|
|
.pre_save = kvmclock_pre_save,
|
|
.fields = (VMStateField[]) {
|
|
VMSTATE_UINT64(clock, KVMClockState),
|
|
VMSTATE_END_OF_LIST()
|
|
},
|
|
.subsections = (const VMStateDescription * []) {
|
|
&kvmclock_reliable_get_clock,
|
|
NULL
|
|
}
|
|
};
|
|
|
|
static Property kvmclock_properties[] = {
|
|
DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState,
|
|
mach_use_reliable_get_clock, true),
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
};
|
|
|
|
static void kvmclock_class_init(ObjectClass *klass, void *data)
|
|
{
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
dc->realize = kvmclock_realize;
|
|
dc->vmsd = &kvmclock_vmsd;
|
|
dc->props = kvmclock_properties;
|
|
/*
|
|
* FIXME: Set only because we are not sure yet if this device
|
|
* will be outside the q35 sysbus whitelist.
|
|
*/
|
|
dc->user_creatable = true;
|
|
}
|
|
|
|
static const TypeInfo kvmclock_info = {
|
|
.name = TYPE_KVM_CLOCK,
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
.instance_size = sizeof(KVMClockState),
|
|
.class_init = kvmclock_class_init,
|
|
};
|
|
|
|
/* Note: Must be called after VCPU initialization. */
|
|
void kvmclock_create(void)
|
|
{
|
|
X86CPU *cpu = X86_CPU(first_cpu);
|
|
|
|
if (kvm_enabled() &&
|
|
cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
|
|
(1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
|
|
sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
|
|
}
|
|
}
|
|
|
|
static void kvmclock_register_types(void)
|
|
{
|
|
type_register_static(&kvmclock_info);
|
|
}
|
|
|
|
type_init(kvmclock_register_types)
|