From patchwork Sat Dec 3 11:17:38 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kiszka X-Patchwork-Id: 129064 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 93ED1B6F18 for ; Sat, 3 Dec 2011 22:40:40 +1100 (EST) Received: from localhost ([::1]:53828 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RWnc4-00040h-NY for incoming@patchwork.ozlabs.org; Sat, 03 Dec 2011 06:18:36 -0500 Received: from eggs.gnu.org ([140.186.70.92]:56265) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RWnbW-0002Rg-7j for qemu-devel@nongnu.org; Sat, 03 Dec 2011 06:18:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RWnbP-0002g1-Ag for qemu-devel@nongnu.org; Sat, 03 Dec 2011 06:18:01 -0500 Received: from fmmailgate02.web.de ([217.72.192.227]:37696) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RWnbO-0002fK-5u for qemu-devel@nongnu.org; Sat, 03 Dec 2011 06:17:54 -0500 Received: from moweb001.kundenserver.de (moweb001.kundenserver.de [172.19.20.114]) by fmmailgate02.web.de (Postfix) with ESMTP id 712661BB1CFE4 for ; Sat, 3 Dec 2011 12:17:53 +0100 (CET) Received: from localhost.localdomain ([88.64.12.252]) by smtp.web.de (mrweb002) with ESMTPA (Nemesis) id 0MHpOP-1RVyeq0RyP-003ftc; Sat, 03 Dec 2011 12:17:53 +0100 From: Jan Kiszka To: Avi Kivity , Marcelo Tosatti Date: Sat, 3 Dec 2011 12:17:38 +0100 Message-Id: <912e2de9716af2e4e9b6c7605b3ea08e9000fd76.1322910904.git.jan.kiszka@web.de> X-Mailer: git-send-email 1.7.3.4 In-Reply-To: References: In-Reply-To: References: X-Provags-ID: V02:K0:uvEhIPaZuDJ8pQEEaHnR+ZGDXqu8oQw6k5gBaICaFfV lpp5zqKupu/Sf+720vyIzvIKUidJvhap0xWr4Vxr4JX7vu0lF7 D6n1j5WKEncKQ2P5z3FVen5m47i2EV1l0zBx8vUQTIBaVTGuyt zkrRU0tjj/cVQrFS78R1uOnpAfzCsok2jVJmM2/jT4kpNMGEue KJUWTfTI6Lq4K8U4Xie0A== X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4-2.6 X-Received-From: 217.72.192.227 Cc: Blue Swirl , Anthony Liguori , qemu-devel , kvm@vger.kernel.org, "Michael S. Tsirkin" Subject: [Qemu-devel] [RFC][PATCH 13/16] kvm: x86: Add user space part for in-kernel APIC X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Jan Kiszka This introduces the alternative APIC model 'kvm-apic' which makes use of KVM's in-kernel device model. MSI is not yet supported, so we disable this when the in-kernel model is in use. Signed-off-by: Jan Kiszka --- Makefile.target | 2 +- hw/kvm/apic.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/pc.c | 15 ++++-- kvm.h | 3 + target-i386/kvm.c | 8 +++ 5 files changed, 169 insertions(+), 6 deletions(-) create mode 100644 hw/kvm/apic.c diff --git a/Makefile.target b/Makefile.target index 4cd3c0e..66b42d5 100644 --- a/Makefile.target +++ b/Makefile.target @@ -231,7 +231,7 @@ obj-i386-y += vmport.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-$(CONFIG_KVM) += kvm/clock.o +obj-i386-$(CONFIG_KVM) += kvm/clock.o kvm/apic.o obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o # shared objects diff --git a/hw/kvm/apic.c b/hw/kvm/apic.c new file mode 100644 index 0000000..fe811b6 --- /dev/null +++ b/hw/kvm/apic.c @@ -0,0 +1,147 @@ +/* + * KVM in-kernel APIC support + * + * 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 +#include + +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))); +} + +int kvm_put_apic(CPUState *env) +{ + APICState *s = DO_UPCAST(APICState, busdev.qdev, env->apic_state); + struct kvm_lapic_state kapic; + int i; + + if (s && kvm_enabled() && kvm_irqchip_in_kernel()) { + memset(&kapic, 0, sizeof(kapic)); + kvm_apic_set_reg(&kapic, 0x2, s->id << 24); + 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); + + return kvm_vcpu_ioctl(env, KVM_SET_LAPIC, &kapic); + } + + return 0; +} + +int kvm_get_apic(CPUState *env) +{ + APICState *s = DO_UPCAST(APICState, busdev.qdev, env->apic_state); + struct kvm_lapic_state kapic; + int ret, i, v; + + if (s && kvm_enabled() && kvm_irqchip_in_kernel()) { + ret = kvm_vcpu_ioctl(env, KVM_GET_LAPIC, &kapic); + if (ret < 0) { + return ret; + } + + s->id = kvm_apic_get_reg(&kapic, 0x2) >> 24; + 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; + + s->initial_count_load_time = qemu_get_clock_ns(vm_clock); + apic_next_timer(s, s->initial_count_load_time); + } + return 0; +} + +static void kvm_apic_set_base(APICState *s, uint64_t val) +{ + s->apicbase = val; +} + +static void kvm_apic_set_tpr(APICState *s, uint8_t val) +{ + s->tpr = (val & 0x0f) << 4; +} + +static int kvm_apic_init(SysBusDevice *dev) +{ + APICState *s = FROM_SYSBUS(APICState, dev); + + memory_region_init_reservation(&s->io_memory, "kvm-apic-msi", + MSI_SPACE_SIZE); + + if (apic_init_common(s) < 0) { + memory_region_destroy(&s->io_memory); + return -1; + } + + s->set_base = kvm_apic_set_base; + s->set_tpr = kvm_apic_set_tpr; + return 0; +} + +static SysBusDeviceInfo kvm_apic_info = { + .init = kvm_apic_init, + .qdev.name = "kvm-apic", + .qdev.size = sizeof(APICState), + .qdev.vmsd = &vmstate_apic, + .qdev.reset = apic_reset, + .qdev.no_user = 1, + .qdev.props = (Property[]) { + DEFINE_PROP_UINT8("id", APICState, id, -1), + DEFINE_PROP_PTR("cpu_env", APICState, cpu_env), + DEFINE_PROP_END_OF_LIST(), + } +}; + +static void kvm_apic_register_devices(void) +{ + sysbus_register_withprop(&kvm_apic_info); +} + +device_init(kvm_apic_register_devices) diff --git a/hw/pc.c b/hw/pc.c index 715cc63..0a1e51c 100644 --- a/hw/pc.c +++ b/hw/pc.c @@ -879,25 +879,30 @@ DeviceState *cpu_get_current_apic(void) static DeviceState *apic_init(void *env, uint8_t apic_id) { DeviceState *dev; - SysBusDevice *d; static int apic_mapped; - dev = qdev_create(NULL, "apic"); + if (kvm_enabled() && kvm_irqchip_in_kernel()) { + dev = qdev_create(NULL, "kvm-apic"); + } else { + dev = qdev_create(NULL, "apic"); + } qdev_prop_set_uint8(dev, "id", apic_id); qdev_prop_set_ptr(dev, "cpu_env", env); qdev_init_nofail(dev); - d = sysbus_from_qdev(dev); /* XXX: mapping more APICs at the same memory location */ if (apic_mapped == 0) { /* NOTE: the APIC is directly connected to the CPU - it is not on the global memory bus. */ /* XXX: what if the base changes? */ - sysbus_mmio_map(d, 0, MSI_ADDR_BASE); + sysbus_mmio_map(sysbus_from_qdev(dev), 0, MSI_ADDR_BASE); apic_mapped = 1; } - msi_supported = true; + /* KVM does not support MSI yet. */ + if (!kvm_enabled() || !kvm_irqchip_in_kernel()) { + msi_supported = true; + } return dev; } diff --git a/kvm.h b/kvm.h index a3c87af..446360a 100644 --- a/kvm.h +++ b/kvm.h @@ -134,6 +134,9 @@ int kvm_irqchip_set_irq(KVMState *s, int irq, int level); void kvm_irqchip_add_route(KVMState *s, int gsi, int irqchip, int pin); int kvm_irqchip_commit_routes(KVMState *s); +int kvm_put_apic(CPUState *env); +int kvm_get_apic(CPUState *env); + struct kvm_guest_debug; struct kvm_debug_exit_arch; diff --git a/target-i386/kvm.c b/target-i386/kvm.c index 232c897..537b61f 100644 --- a/target-i386/kvm.c +++ b/target-i386/kvm.c @@ -1435,6 +1435,10 @@ int kvm_arch_put_registers(CPUState *env, int level) if (ret < 0) { return ret; } + ret = kvm_put_apic(env); + if (ret < 0) { + return ret; + } } ret = kvm_put_vcpu_events(env, level); if (ret < 0) { @@ -1482,6 +1486,10 @@ int kvm_arch_get_registers(CPUState *env) if (ret < 0) { return ret; } + ret = kvm_get_apic(env); + if (ret < 0) { + return ret; + } ret = kvm_get_vcpu_events(env); if (ret < 0) { return ret;