2011-10-16 15:23:26 +04:00
|
|
|
/*
|
|
|
|
* KVM in-kernel APIC support
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-05-23 17:35:07 +03:00
|
|
|
|
2016-01-26 21:17:03 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/i386/apic_internal.h"
|
2012-12-12 16:24:50 +04:00
|
|
|
#include "hw/pci/msi.h"
|
2017-01-10 13:59:55 +03:00
|
|
|
#include "sysemu/hw_accel.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/kvm.h"
|
2020-12-12 18:55:08 +03:00
|
|
|
#include "kvm/kvm_i386.h"
|
2011-10-16 15:23:26 +04:00
|
|
|
|
|
|
|
static inline void kvm_apic_set_reg(struct kvm_lapic_state *kapic,
|
|
|
|
int reg_id, uint32_t val)
|
|
|
|
{
|
|
|
|
*((uint32_t *)(kapic->regs + (reg_id << 4))) = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t kvm_apic_get_reg(struct kvm_lapic_state *kapic,
|
|
|
|
int reg_id)
|
|
|
|
{
|
|
|
|
return *((uint32_t *)(kapic->regs + (reg_id << 4)));
|
|
|
|
}
|
|
|
|
|
2016-09-12 20:18:35 +03:00
|
|
|
static void kvm_put_apic_state(APICCommonState *s, struct kvm_lapic_state *kapic)
|
2011-10-16 15:23:26 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-05-10 20:19:47 +04:00
|
|
|
memset(kapic, 0, sizeof(*kapic));
|
2016-10-19 15:05:38 +03:00
|
|
|
if (kvm_has_x2apic_api() && s->apicbase & MSR_IA32_APICBASE_EXTD) {
|
|
|
|
kvm_apic_set_reg(kapic, 0x2, s->initial_apic_id);
|
|
|
|
} else {
|
|
|
|
kvm_apic_set_reg(kapic, 0x2, s->id << 24);
|
|
|
|
}
|
2011-10-16 15:23:26 +04:00
|
|
|
kvm_apic_set_reg(kapic, 0x8, s->tpr);
|
|
|
|
kvm_apic_set_reg(kapic, 0xd, s->log_dest << 24);
|
|
|
|
kvm_apic_set_reg(kapic, 0xe, s->dest_mode << 28 | 0x0fffffff);
|
|
|
|
kvm_apic_set_reg(kapic, 0xf, s->spurious_vec);
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
kvm_apic_set_reg(kapic, 0x10 + i, s->isr[i]);
|
|
|
|
kvm_apic_set_reg(kapic, 0x18 + i, s->tmr[i]);
|
|
|
|
kvm_apic_set_reg(kapic, 0x20 + i, s->irr[i]);
|
|
|
|
}
|
|
|
|
kvm_apic_set_reg(kapic, 0x28, s->esr);
|
|
|
|
kvm_apic_set_reg(kapic, 0x30, s->icr[0]);
|
|
|
|
kvm_apic_set_reg(kapic, 0x31, s->icr[1]);
|
|
|
|
for (i = 0; i < APIC_LVT_NB; i++) {
|
|
|
|
kvm_apic_set_reg(kapic, 0x32 + i, s->lvt[i]);
|
|
|
|
}
|
|
|
|
kvm_apic_set_reg(kapic, 0x38, s->initial_count);
|
|
|
|
kvm_apic_set_reg(kapic, 0x3e, s->divide_conf);
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:02 +04:00
|
|
|
void kvm_get_apic_state(DeviceState *dev, struct kvm_lapic_state *kapic)
|
2011-10-16 15:23:26 +04:00
|
|
|
{
|
2013-11-05 14:16:02 +04:00
|
|
|
APICCommonState *s = APIC_COMMON(dev);
|
2011-10-16 15:23:26 +04:00
|
|
|
int i, v;
|
|
|
|
|
2016-10-19 15:05:38 +03:00
|
|
|
if (kvm_has_x2apic_api() && s->apicbase & MSR_IA32_APICBASE_EXTD) {
|
|
|
|
assert(kvm_apic_get_reg(kapic, 0x2) == s->initial_apic_id);
|
|
|
|
} else {
|
|
|
|
s->id = kvm_apic_get_reg(kapic, 0x2) >> 24;
|
|
|
|
}
|
2011-10-16 15:23:26 +04:00
|
|
|
s->tpr = kvm_apic_get_reg(kapic, 0x8);
|
|
|
|
s->arb_id = kvm_apic_get_reg(kapic, 0x9);
|
|
|
|
s->log_dest = kvm_apic_get_reg(kapic, 0xd) >> 24;
|
|
|
|
s->dest_mode = kvm_apic_get_reg(kapic, 0xe) >> 28;
|
|
|
|
s->spurious_vec = kvm_apic_get_reg(kapic, 0xf);
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
s->isr[i] = kvm_apic_get_reg(kapic, 0x10 + i);
|
|
|
|
s->tmr[i] = kvm_apic_get_reg(kapic, 0x18 + i);
|
|
|
|
s->irr[i] = kvm_apic_get_reg(kapic, 0x20 + i);
|
|
|
|
}
|
|
|
|
s->esr = kvm_apic_get_reg(kapic, 0x28);
|
|
|
|
s->icr[0] = kvm_apic_get_reg(kapic, 0x30);
|
|
|
|
s->icr[1] = kvm_apic_get_reg(kapic, 0x31);
|
|
|
|
for (i = 0; i < APIC_LVT_NB; i++) {
|
|
|
|
s->lvt[i] = kvm_apic_get_reg(kapic, 0x32 + i);
|
|
|
|
}
|
|
|
|
s->initial_count = kvm_apic_get_reg(kapic, 0x38);
|
|
|
|
s->divide_conf = kvm_apic_get_reg(kapic, 0x3e);
|
|
|
|
|
|
|
|
v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
|
|
|
|
s->count_shift = (v + 1) & 7;
|
|
|
|
|
2013-08-21 19:03:08 +04:00
|
|
|
s->initial_count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2011-10-16 15:23:26 +04:00
|
|
|
apic_next_timer(s, s->initial_count_load_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_apic_set_base(APICCommonState *s, uint64_t val)
|
|
|
|
{
|
|
|
|
s->apicbase = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_apic_set_tpr(APICCommonState *s, uint8_t val)
|
|
|
|
{
|
|
|
|
s->tpr = (val & 0x0f) << 4;
|
|
|
|
}
|
|
|
|
|
2012-02-17 21:31:19 +04:00
|
|
|
static uint8_t kvm_apic_get_tpr(APICCommonState *s)
|
|
|
|
{
|
|
|
|
return s->tpr >> 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_apic_enable_tpr_reporting(APICCommonState *s, bool enable)
|
|
|
|
{
|
|
|
|
struct kvm_tpr_access_ctl ctl = {
|
|
|
|
.enabled = enable
|
|
|
|
};
|
|
|
|
|
2012-10-31 09:06:49 +04:00
|
|
|
kvm_vcpu_ioctl(CPU(s->cpu), KVM_TPR_ACCESS_REPORTING, &ctl);
|
2012-02-17 21:31:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_apic_vapic_base_update(APICCommonState *s)
|
|
|
|
{
|
|
|
|
struct kvm_vapic_addr vapid_addr = {
|
|
|
|
.vapic_addr = s->vapic_paddr,
|
|
|
|
};
|
|
|
|
int ret;
|
|
|
|
|
2012-10-31 09:06:49 +04:00
|
|
|
ret = kvm_vcpu_ioctl(CPU(s->cpu), KVM_SET_VAPIC_ADDR, &vapid_addr);
|
2012-02-17 21:31:19 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "KVM: setting VAPIC address failed (%s)\n",
|
|
|
|
strerror(-ret));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-31 12:36:08 +03:00
|
|
|
static void kvm_apic_put(CPUState *cs, run_on_cpu_data data)
|
2016-09-12 20:18:35 +03:00
|
|
|
{
|
2016-10-31 12:36:08 +03:00
|
|
|
APICCommonState *s = data.host_ptr;
|
2016-09-12 20:18:35 +03:00
|
|
|
struct kvm_lapic_state kapic;
|
|
|
|
int ret;
|
|
|
|
|
2016-09-22 15:49:17 +03:00
|
|
|
kvm_put_apicbase(s->cpu, s->apicbase);
|
2016-09-12 20:18:35 +03:00
|
|
|
kvm_put_apic_state(s, &kapic);
|
|
|
|
|
|
|
|
ret = kvm_vcpu_ioctl(CPU(s->cpu), KVM_SET_LAPIC, &kapic);
|
|
|
|
if (ret < 0) {
|
2021-05-19 14:35:28 +03:00
|
|
|
fprintf(stderr, "KVM_SET_LAPIC failed: %s\n", strerror(-ret));
|
2016-09-12 20:18:35 +03:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_apic_post_load(APICCommonState *s)
|
|
|
|
{
|
2016-10-31 12:36:08 +03:00
|
|
|
run_on_cpu(CPU(s->cpu), kvm_apic_put, RUN_ON_CPU_HOST_PTR(s));
|
2016-09-12 20:18:35 +03:00
|
|
|
}
|
|
|
|
|
2016-10-31 12:36:08 +03:00
|
|
|
static void do_inject_external_nmi(CPUState *cpu, run_on_cpu_data data)
|
2011-10-16 15:23:26 +04:00
|
|
|
{
|
2016-10-31 12:36:08 +03:00
|
|
|
APICCommonState *s = data.host_ptr;
|
2011-10-16 15:23:26 +04:00
|
|
|
uint32_t lvt;
|
|
|
|
int ret;
|
|
|
|
|
2013-05-01 16:24:52 +04:00
|
|
|
cpu_synchronize_state(cpu);
|
2011-10-16 15:23:26 +04:00
|
|
|
|
|
|
|
lvt = s->lvt[APIC_LVT_LINT1];
|
|
|
|
if (!(lvt & APIC_LVT_MASKED) && ((lvt >> 8) & 7) == APIC_DM_NMI) {
|
2012-10-31 09:06:49 +04:00
|
|
|
ret = kvm_vcpu_ioctl(cpu, KVM_NMI);
|
2011-10-16 15:23:26 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "KVM: injection failed, NMI lost (%s)\n",
|
|
|
|
strerror(-ret));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_apic_external_nmi(APICCommonState *s)
|
|
|
|
{
|
2016-10-31 12:36:08 +03:00
|
|
|
run_on_cpu(CPU(s->cpu), do_inject_external_nmi, RUN_ON_CPU_HOST_PTR(s));
|
2011-10-16 15:23:26 +04:00
|
|
|
}
|
|
|
|
|
2016-10-10 18:28:43 +03:00
|
|
|
static void kvm_send_msi(MSIMessage *msg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-10-05 17:18:19 +03:00
|
|
|
/*
|
|
|
|
* The message has already passed through interrupt remapping if enabled,
|
|
|
|
* but the legacy extended destination ID in low bits still needs to be
|
|
|
|
* handled.
|
|
|
|
*/
|
|
|
|
msg->address = kvm_swizzle_msi_ext_dest_id(msg->address);
|
|
|
|
|
2016-10-10 18:28:43 +03:00
|
|
|
ret = kvm_irqchip_send_msi(kvm_state, *msg);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "KVM: injection failed, MSI lost (%s)\n",
|
|
|
|
strerror(-ret));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t kvm_apic_mem_read(void *opaque, hwaddr addr,
|
2012-05-16 22:41:12 +04:00
|
|
|
unsigned size)
|
|
|
|
{
|
|
|
|
return ~(uint64_t)0;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void kvm_apic_mem_write(void *opaque, hwaddr addr,
|
2012-05-16 22:41:12 +04:00
|
|
|
uint64_t data, unsigned size)
|
|
|
|
{
|
|
|
|
MSIMessage msg = { .address = addr, .data = data };
|
|
|
|
|
2016-10-10 18:28:43 +03:00
|
|
|
kvm_send_msi(&msg);
|
2012-05-16 22:41:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps kvm_apic_io_ops = {
|
|
|
|
.read = kvm_apic_mem_read,
|
|
|
|
.write = kvm_apic_mem_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2014-12-10 18:56:46 +03:00
|
|
|
static void kvm_apic_reset(APICCommonState *s)
|
2011-10-16 15:23:26 +04:00
|
|
|
{
|
2014-08-28 15:18:57 +04:00
|
|
|
/* Not used by KVM, which uses the CPU mp_state instead. */
|
|
|
|
s->wait_for_sipi = 0;
|
2016-09-12 20:18:35 +03:00
|
|
|
|
2016-10-31 12:36:08 +03:00
|
|
|
run_on_cpu(CPU(s->cpu), kvm_apic_put, RUN_ON_CPU_HOST_PTR(s));
|
2014-12-10 18:56:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_apic_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
APICCommonState *s = APIC_COMMON(dev);
|
2014-08-28 15:18:57 +04:00
|
|
|
|
2016-07-14 17:58:02 +03:00
|
|
|
memory_region_init_io(&s->io_memory, OBJECT(s), &kvm_apic_io_ops, s,
|
|
|
|
"kvm-apic-msi", APIC_SPACE_SIZE);
|
2012-05-16 22:41:12 +04:00
|
|
|
|
2020-09-22 23:19:21 +03:00
|
|
|
assert(kvm_has_gsi_routing());
|
|
|
|
msi_nonbroken = true;
|
2011-10-16 15:23:26 +04:00
|
|
|
}
|
|
|
|
|
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 18:29:24 +03:00
|
|
|
static void kvm_apic_unrealize(DeviceState *dev)
|
2016-06-23 18:30:14 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static void kvm_apic_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
APICCommonClass *k = APIC_COMMON_CLASS(klass);
|
|
|
|
|
2013-11-05 14:16:03 +04:00
|
|
|
k->realize = kvm_apic_realize;
|
2016-06-23 18:30:14 +03:00
|
|
|
k->unrealize = kvm_apic_unrealize;
|
2014-12-10 18:56:46 +03:00
|
|
|
k->reset = kvm_apic_reset;
|
2012-01-24 23:12:29 +04:00
|
|
|
k->set_base = kvm_apic_set_base;
|
|
|
|
k->set_tpr = kvm_apic_set_tpr;
|
2012-02-17 21:31:19 +04:00
|
|
|
k->get_tpr = kvm_apic_get_tpr;
|
2016-09-12 20:18:35 +03:00
|
|
|
k->post_load = kvm_apic_post_load;
|
2012-02-17 21:31:19 +04:00
|
|
|
k->enable_tpr_reporting = kvm_apic_enable_tpr_reporting;
|
|
|
|
k->vapic_base_update = kvm_apic_vapic_base_update;
|
2012-01-24 23:12:29 +04:00
|
|
|
k->external_nmi = kvm_apic_external_nmi;
|
2016-10-10 18:28:43 +03:00
|
|
|
k->send_msi = kvm_send_msi;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo kvm_apic_info = {
|
2012-01-24 23:12:29 +04:00
|
|
|
.name = "kvm-apic",
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_APIC_COMMON,
|
2012-02-04 01:06:47 +04:00
|
|
|
.instance_size = sizeof(APICCommonState),
|
2012-01-24 23:12:29 +04:00
|
|
|
.class_init = kvm_apic_class_init,
|
2011-10-16 15:23:26 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void kvm_apic_register_types(void)
|
2011-10-16 15:23:26 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&kvm_apic_info);
|
2011-10-16 15:23:26 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(kvm_apic_register_types)
|