Skip to contentSkip to conversation

in-kernel irqchip, new spin

PATCH In review Glauber Costa 4 participants 18 messages Dec 2, 2009 – Dec 3, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

cover in-kernel irqchip, new spin

Avi/Marcelo

Please include this in a branch in qemu-kvm, for future inclusion in qemu.git

This is basically the same thing that was sitting in staging for weeks, just with a build bug fix on non-x86 hosts.

Glauber Costa (9):
introduce VMSTATE_U64
Provide ioapic-kvm
provide apic_set_irq_delivered
provide i8259-kvm
Don't call apic functions directly from kvm code export kvm_put_mp_state
provide apic-kvm
Initialize in-kernel irqchip
Do GSI routing

 Makefile.target   |    2 +
 hw/apic-kvm.c     |  157 ++++++++++++++++++++++++++++++++++++++++++++
 hw/apic.c         |    5 ++
 hw/hw.h           |   24 +++++++
 hw/i8259-kvm.c    |  112 +++++++++++++++++++++++++++++++
 hw/ioapic-kvm.c   |   89 +++++++++++++++++++++++++
 hw/pc.c           |   21 +++++-
 hw/pc.h           |    6 ++
 kvm-all.c         |   47 +++++++++++++-
 kvm.h             |   16 +++++
 savevm.c          |   23 +++++++
 target-i386/cpu.h |    9 +++
 target-i386/kvm.c |  188 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 target-ppc/kvm.c  |    5 ++
 14 files changed, 693 insertions(+), 11 deletions(-)
 create mode 100644 hw/apic-kvm.c
 create mode 100644 hw/i8259-kvm.c
 create mode 100644 hw/ioapic-kvm.c
#1 lore raw

PATCH 1/9 introduce VMSTATE_U64

Slightly modified version of a patch already included in qemu-kvm:

This is a patch actually written by Juan, which, according to him, he plans on posting to qemu.git. Problem is that linux defines u64 in a way that is type-uncompatible with uint64_t.

I am including it here, because it is a dependency to my patch series that follows.

---
 hw/hw.h  |   24 ++++++++++++++++++++++++
 savevm.c |   23 +++++++++++++++++++++++
 2 files changed, 47 insertions(+), 0 deletions(-)
hw/hw.h +24 -0
index 7889aa3..e8a53f9 100644
@@ -337,6 +337,11 @@ extern const VMStateInfo vmstate_info_uint16;
 extern const VMStateInfo vmstate_info_uint32;
 extern const VMStateInfo vmstate_info_uint64;
 
+#ifdef __linux__
+#include <linux/types.h>
+extern const VMStateInfo vmstate_info_u64;
+#endif
+
 extern const VMStateInfo vmstate_info_timer;
 extern const VMStateInfo vmstate_info_ptimer;
 extern const VMStateInfo vmstate_info_buffer;
@@ -393,6 +398,16 @@ extern const VMStateInfo vmstate_info_unused_buffer;
     .offset     = vmstate_offset_array(_state, _field, _type, _num), \
 }
 
+#define VMSTATE_ARRAY_UNSAFE(_field, _state, _num, _version, _info, _type) {\
+    .name       = (stringify(_field)),                               \
+    .version_id = (_version),                                        \
+    .num        = (_num),                                            \
+    .info       = &(_info),                                          \
+    .size       = sizeof(_type),                                     \
+    .flags      = VMS_ARRAY,                                         \
+    .offset     = offsetof(_state, _field)                           \
+}
+
 #define VMSTATE_ARRAY_TEST(_field, _state, _num, _test, _info, _type) {\
     .name         = (stringify(_field)),                              \
     .field_exists = (_test),                                          \
@@ -596,6 +611,15 @@ extern const VMStateDescription vmstate_i2c_slave;
 #define VMSTATE_UINT64(_f, _s)                                        \
     VMSTATE_UINT64_V(_f, _s, 0)
 
+/* This is needed because on linux __u64 is unsigned long long
+   and on glibc uint64_t is unsigned long on 64 bits */
+#ifdef __linux__
+#define VMSTATE_U64_V(_f, _s, _v)                                     \
+    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_u64, __u64)
+#define VMSTATE_U64(_f, _s)                                           \
+    VMSTATE_U64_V(_f, _s, 0)
+#endif
+
 #define VMSTATE_UINT8_EQUAL(_f, _s)                                   \
     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_uint8_equal, uint8_t)
 
savevm.c +23 -0
index 18c2e54..7d68ec0 100644
@@ -872,6 +872,29 @@ const VMStateInfo vmstate_info_uint64 = {
     .put  = put_uint64,
 };
 
+/* 64 bit linux kernel unsigned int */
+
+#ifdef __linux__
+static int get_u64(QEMUFile *f, void *pv, size_t size)
+{
+    __u64 *v = pv;
+    qemu_get_be64s(f, (uint64_t *)v);
+    return 0;
+}
+
+static void put_u64(QEMUFile *f, void *pv, size_t size)
+{
+    __u64 *v = pv;
+    qemu_put_be64s(f, (uint64_t *)v);
+}
+
+const VMStateInfo vmstate_info_u64 = {
+    .name = "__u64",
+    .get  = get_u64,
+    .put  = put_u64,
+};
+#endif /* __linux__ */
+
 /* 8 bit int. See that the received value is the same than the one
    in the field */
 
Signature · 1 line
1.6.5.2
#2 lore raw

PATCH 2/9 Provide ioapic-kvm

This patch provides the file ioapic-kvm.c, which implements a schim over the kvm in-kernel IO APIC.

---
 Makefile.target |    2 +
 hw/ioapic-kvm.c |   89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pc.c         |    7 ++++-
 hw/pc.h         |    2 +
 kvm-all.c       |   20 ++++++++++++
 kvm.h           |    4 ++
 6 files changed, 123 insertions(+), 1 deletions(-)
 create mode 100644 hw/ioapic-kvm.c
Makefile.target +2 -0
index 2985658..844ba46 100644
@@ -199,6 +199,8 @@ obj-i386-y += usb-uhci.o vmmouse.o vmport.o vmware_vga.o hpet.o
 obj-i386-y += device-hotplug.o pci-hotplug.o smbios.o wdt_ib700.o
 obj-i386-y += ne2000-isa.o
 
+obj-i386-$(CONFIG_KVM) += ioapic-kvm.o
+
 # shared objects
 obj-ppc-y = ppc.o ide/core.o ide/qdev.o ide/isa.o ide/pci.o ide/macio.o
 obj-ppc-y += ide/cmd646.o
hw/ioapic-kvm.c new file +89 -0

89 lines not on this page · raw message

hw/pc.c +6 -1
index 97964b2..7bff913 100644
@@ -44,6 +44,7 @@
 #include "ide.h"
 #include "loader.h"
 #include "elf.h"
+#include "kvm.h"
 
 /* output Bochs bios info messages */
 //#define DEBUG_BIOS
@@ -1144,7 +1145,11 @@ static void pc_init1(ram_addr_t ram_size,
     register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
 
     if (pci_enabled) {
-        isa_irq_state->ioapic = ioapic_init();
+        if (kvm_enabled() && kvm_irqchip_in_kernel()) {
+            isa_irq_state->ioapic = kvm_ioapic_init();
+        } else {
+            isa_irq_state->ioapic = ioapic_init();
+        }
     }
     pit = pit_init(0x40, isa_reserve_irq(0));
     pcspk_init(pit);
hw/pc.h +2 -0
index 03ffc91..a3ad931 100644
@@ -49,6 +49,8 @@ void ioapic_set_irq(void *opaque, int vector, int level);
 void apic_reset_irq_delivered(void);
 int apic_get_irq_delivered(void);
 
+qemu_irq *kvm_ioapic_init(void);
+
 /* i8254.c */
 
 #define PIT_FREQ 1193182
kvm-all.c +20 -0
index b605caa..35230c1 100644
@@ -393,6 +393,26 @@ int kvm_check_extension(KVMState *s, unsigned int extension)
     return ret;
 }
 
+#ifdef KVM_CAP_IRQCHIP
+int kvm_set_irqchip(struct kvm_irqchip *chip)
+{
+    if (!kvm_state->irqchip_in_kernel) {
+        return 0;
+    }
+
+    return kvm_vm_ioctl(kvm_state, KVM_SET_IRQCHIP, chip);
+}
+
+int kvm_get_irqchip(struct kvm_irqchip *chip)
+{
+    if (!kvm_state->irqchip_in_kernel) {
+        return 0;
+    }
+
+    return kvm_vm_ioctl(kvm_state, KVM_GET_IRQCHIP, chip);
+}
+#endif
+
 int kvm_init(int smp_cpus)
 {
     static const char upgrade_note[] =
kvm.h +4 -0
index e4cbedc..70839c2 100644
@@ -16,6 +16,7 @@
 
 #include "config.h"
 #include "qemu-queue.h"
+#include <linux/kvm.h>
 
 #ifdef CONFIG_KVM
 extern int kvm_allowed;
@@ -63,6 +64,9 @@ int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
 int kvm_pit_in_kernel(void);
 int kvm_irqchip_in_kernel(void);
 
+int kvm_set_irqchip(struct kvm_irqchip *chip);
+int kvm_get_irqchip(struct kvm_irqchip *chip);
+
 /* internal API */
 
 struct KVMState;
Signature · 1 line
1.6.5.2
#3 lore raw

PATCH 3/9 provide apic_set_irq_delivered

i8259 chip will use it, so provide it, and export it through pc.h

---
 hw/apic.c |    5 +++++
 hw/pc.h   |    1 +
 2 files changed, 6 insertions(+), 0 deletions(-)
hw/apic.c +5 -0
index 87e7dc0..482bb1e 100644
@@ -388,6 +388,11 @@ void apic_reset_irq_delivered(void)
     apic_irq_delivered = 0;
 }
 
+void apic_set_irq_delivered(void)
+{
+    apic_irq_delivered = 1;
+}
+
 int apic_get_irq_delivered(void)
 {
     return apic_irq_delivered;
hw/pc.h +1 -0
index a3ad931..4c9b4c3 100644
@@ -47,6 +47,7 @@ int apic_get_interrupt(CPUState *env);
 qemu_irq *ioapic_init(void);
 void ioapic_set_irq(void *opaque, int vector, int level);
 void apic_reset_irq_delivered(void);
+void apic_set_irq_delivered(void);
 int apic_get_irq_delivered(void);
 
 qemu_irq *kvm_ioapic_init(void);
Signature · 1 line
1.6.5.2
#4 lore raw

PATCH 4/9 provide i8259-kvm

This patch provides the file i8259-kvm.c, which implements a schim over the kvm in-kernel PIC.

---
 Makefile.target   |    2 +-
 hw/i8259-kvm.c    |  112 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pc.c           |    8 +++-
 hw/pc.h           |    1 +
 kvm-all.c         |    7 ++-
 kvm.h             |    4 ++
 target-i386/kvm.c |   26 ++++++++++++
 target-ppc/kvm.c  |    5 ++
 8 files changed, 161 insertions(+), 4 deletions(-)
 create mode 100644 hw/i8259-kvm.c
Makefile.target +1 -1
index 844ba46..8fb68a4 100644
@@ -199,7 +199,7 @@ obj-i386-y += usb-uhci.o vmmouse.o vmport.o vmware_vga.o hpet.o
 obj-i386-y += device-hotplug.o pci-hotplug.o smbios.o wdt_ib700.o
 obj-i386-y += ne2000-isa.o
 
-obj-i386-$(CONFIG_KVM) += ioapic-kvm.o
+obj-i386-$(CONFIG_KVM) += ioapic-kvm.o i8259-kvm.o
 
 # shared objects
 obj-ppc-y = ppc.o ide/core.o ide/qdev.o ide/isa.o ide/pci.o ide/macio.o
hw/i8259-kvm.c new file +112 -0

112 lines not on this page · raw message

hw/pc.c +7 -1
index 7bff913..a58562a 100644
@@ -1098,8 +1098,14 @@ static void pc_init1(ram_addr_t ram_size,
     }
 
     cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1);
-    i8259 = i8259_init(cpu_irq[0]);
     isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
+
+    if (kvm_enabled() && kvm_irqchip_in_kernel()) {
+        i8259 = kvm_i8259_init(cpu_irq[0]);
+    } else {
+        i8259 = i8259_init(cpu_irq[0]);
+    }
+
     isa_irq_state->i8259 = i8259;
     isa_irq = qemu_allocate_irqs(isa_irq_handler, isa_irq_state, 24);
 
hw/pc.h +1 -0
index 4c9b4c3..3d79b9d 100644
@@ -34,6 +34,7 @@ uint32_t pic_intack_read(PicState2 *s);
 void pic_info(Monitor *mon);
 void irq_info(Monitor *mon);
 
+qemu_irq *kvm_i8259_init(qemu_irq parent_irq);
 /* APIC */
 typedef struct IOAPICState IOAPICState;
 void apic_deliver_irq(uint8_t dest, uint8_t dest_mode,
kvm-all.c +5 -2
index 35230c1..e46093f 100644
@@ -393,7 +393,6 @@ int kvm_check_extension(KVMState *s, unsigned int extension)
     return ret;
 }
 
-#ifdef KVM_CAP_IRQCHIP
 int kvm_set_irqchip(struct kvm_irqchip *chip)
 {
     if (!kvm_state->irqchip_in_kernel) {
@@ -411,7 +410,11 @@ int kvm_get_irqchip(struct kvm_irqchip *chip)
 
     return kvm_vm_ioctl(kvm_state, KVM_GET_IRQCHIP, chip);
 }
-#endif
+
+int kvm_set_irq(int irq, int level, int *status)
+{
+    return kvm_arch_set_irq(kvm_state, irq, level, status);
+}
 
 int kvm_init(int smp_cpus)
 {
kvm.h +4 -0
index 70839c2..3b358d0 100644
@@ -67,6 +67,8 @@ int kvm_irqchip_in_kernel(void);
 int kvm_set_irqchip(struct kvm_irqchip *chip);
 int kvm_get_irqchip(struct kvm_irqchip *chip);
 
+int kvm_set_irq(int irq, int level, int *status);
+
 /* internal API */
 
 struct KVMState;
@@ -96,6 +98,8 @@ int kvm_arch_init_vcpu(CPUState *env);
 
 void kvm_arch_reset_vcpu(CPUState *env);
 
+int kvm_arch_set_irq(KVMState *s, int irq, int level, int *status);
+
 struct kvm_guest_debug;
 struct kvm_debug_exit_arch;
 
target-i386/kvm.c +26 -0
index d6bc23a..cf42f85 100644
@@ -992,3 +992,29 @@ void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
     }
 }
 #endif /* KVM_CAP_SET_GUEST_DEBUG */
+
+int kvm_arch_set_irq(KVMState *s, int irq, int level, int *status)
+{
+    struct kvm_irq_level event;
+    int r;
+
+    if (!kvm_irqchip_in_kernel()) {
+        return 0;
+    }
+
+    event.level = level;
+    event.irq = irq;
+
+    r = kvm_vm_ioctl(s, KVM_IRQ_LINE_STATUS, &event);
+
+    if (r < 0)
+        return 0;
+
+    if (status) {
+        *status = event.status;
+    }
+
+    return 1;
+}
+
+
target-ppc/kvm.c +5 -0
index 4e1c65f..d08639b 100644
@@ -57,6 +57,11 @@ void kvm_arch_reset_vcpu(CPUState *env)
 {
 }
 
+int kvm_arch_set_irq(KVMState *s, int irq, int level, int *status)
+{
+    return -ENOSYS;
+}
+
 int kvm_arch_put_registers(CPUState *env)
 {
     struct kvm_regs regs;
Signature · 1 line
1.6.5.2
#5 lore raw

PATCH 5/9 Don't call apic functions directly from kvm code

It is actually not necessary to call a tpr function to save and load cr8, as cr8 is part of the processor state, and thus, it is much easier to just add it to CPUState.

As for apic base, wrap kvm usages, so we can call either the qemu device, or the in kernel version.

---
 target-i386/cpu.h |    1 +
 target-i386/kvm.c |   25 +++++++++++++++++++------
 2 files changed, 20 insertions(+), 6 deletions(-)
target-i386/cpu.h +1 -0
index eb9532a..7c4fa47 100644
@@ -606,6 +606,7 @@ typedef struct CPUX86State {
     SegmentCache idt; /* only base and limit are used */
 
     target_ulong cr[5]; /* NOTE: cr1 is unused */
+    target_ulong cr8;
     int32_t a20_mask;
 
     /* FPU state */
target-i386/kvm.c +19 -6
index cf42f85..78fc941 100644
@@ -344,6 +344,19 @@ static void get_seg(SegmentCache *lhs, const struct kvm_segment *rhs)
 	| (rhs->avl * DESC_AVL_MASK);
 }
 
+static void kvm_set_apic_base(CPUState *env, uint64_t val)
+{
+    if (!kvm_irqchip_in_kernel())
+        cpu_set_apic_base(env, val);
+}
+
+static uint64_t kvm_get_apic_base(CPUState *env)
+{
+    if (!kvm_irqchip_in_kernel())
+        return cpu_get_apic_base(env);
+    return 0;
+}
+
 static void kvm_getput_reg(__u64 *kvm_reg, target_ulong *qemu_reg, int set)
 {
     if (set)
@@ -455,8 +468,8 @@ static int kvm_put_sregs(CPUState *env)
     sregs.cr3 = env->cr[3];
     sregs.cr4 = env->cr[4];
 
-    sregs.cr8 = cpu_get_apic_tpr(env);
-    sregs.apic_base = cpu_get_apic_base(env);
+    sregs.cr8 = env->cr8;
+    sregs.apic_base = kvm_get_apic_base(env);
 
     sregs.efer = env->efer;
 
@@ -561,7 +574,7 @@ static int kvm_get_sregs(CPUState *env)
     env->cr[3] = sregs.cr3;
     env->cr[4] = sregs.cr4;
 
-    cpu_set_apic_base(env, sregs.apic_base);
+    kvm_set_apic_base(env, sregs.apic_base);
 
     env->efer = sregs.efer;
     //cpu_set_apic_tpr(env, sregs.cr8);
@@ -777,7 +790,7 @@ int kvm_arch_pre_run(CPUState *env, struct kvm_run *run)
         run->request_interrupt_window = 0;
 
     dprintf("setting tpr\n");
-    run->cr8 = cpu_get_apic_tpr(env);
+    run->cr8 = env->cr8;
 
     return 0;
 }
@@ -789,8 +802,8 @@ int kvm_arch_post_run(CPUState *env, struct kvm_run *run)
     else
         env->eflags &= ~IF_MASK;
     
-    cpu_set_apic_tpr(env, run->cr8);
-    cpu_set_apic_base(env, run->apic_base);
+    env->cr8 = run->cr8;
+    kvm_set_apic_base(env, run->apic_base);
 
     return 0;
 }
Signature · 1 line
1.6.5.2
#6 lore raw

PATCH 6/9 export kvm_put_mp_state

We'll use it from inside the in-kernel apic chip, so get it into a header file.

---
 target-i386/cpu.h |    4 ++++
 target-i386/kvm.c |    2 +-
 2 files changed, 5 insertions(+), 1 deletions(-)
target-i386/cpu.h +4 -0
index 7c4fa47..1ebf80e 100644
@@ -921,4 +921,8 @@ void apic_init_reset(CPUState *env);
 void apic_sipi(CPUState *env);
 void do_cpu_init(CPUState *env);
 void do_cpu_sipi(CPUState *env);
+
+/* KVM hooks */
+int kvm_put_mp_state(CPUState *env);
+
 #endif /* CPU_I386_H */
target-i386/kvm.c +1 -1
index 78fc941..4eb61f1 100644
@@ -687,7 +687,7 @@ static int kvm_get_msrs(CPUState *env)
     return 0;
 }
 
-static int kvm_put_mp_state(CPUState *env)
+int kvm_put_mp_state(CPUState *env)
 {
     struct kvm_mp_state mp_state = { .mp_state = env->mp_state };
 
Signature · 1 line
1.6.5.2
#7 lore raw

PATCH 7/9 provide apic-kvm

This patch provides the file apic-kvm.c, which implements a schim over the kvm in-kernel APIC.

---
 Makefile.target   |    2 +-
 hw/apic-kvm.c     |  157 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pc.c           |    6 ++-
 hw/pc.h           |    2 +
 kvm.h             |    5 ++
 target-i386/cpu.h |    4 ++
 target-i386/kvm.c |   19 ++++++-
 7 files changed, 192 insertions(+), 3 deletions(-)
 create mode 100644 hw/apic-kvm.c
Makefile.target +1 -1
index 8fb68a4..c23aeb3 100644
@@ -199,7 +199,7 @@ obj-i386-y += usb-uhci.o vmmouse.o vmport.o vmware_vga.o hpet.o
 obj-i386-y += device-hotplug.o pci-hotplug.o smbios.o wdt_ib700.o
 obj-i386-y += ne2000-isa.o
 
-obj-i386-$(CONFIG_KVM) += ioapic-kvm.o i8259-kvm.o
+obj-i386-$(CONFIG_KVM) += ioapic-kvm.o i8259-kvm.o apic-kvm.o
 
 # shared objects
 obj-ppc-y = ppc.o ide/core.o ide/qdev.o ide/isa.o ide/pci.o ide/macio.o
hw/apic-kvm.c new file +157 -0

157 lines not on this page · raw message

hw/pc.c +5 -1
index a58562a..ea3f93f 100644
@@ -962,7 +962,11 @@ static CPUState *pc_new_cpu(const char *cpu_model)
     if ((env->cpuid_features & CPUID_APIC) || smp_cpus > 1) {
         env->cpuid_apic_id = env->cpu_index;
         /* APIC reset callback resets cpu */
-        apic_init(env);
+        if (kvm_enabled() && kvm_irqchip_in_kernel()) {
+            kvm_apic_init(env);
+        } else {
+            apic_init(env);
+        }
     } else {
         qemu_register_reset((QEMUResetHandler*)cpu_reset, env);
     }
hw/pc.h +2 -0
index 3d79b9d..6b3817a 100644
@@ -42,6 +42,8 @@ void apic_deliver_irq(uint8_t dest, uint8_t dest_mode,
                              uint8_t vector_num, uint8_t polarity,
                              uint8_t trigger_mode);
 int apic_init(CPUState *env);
+int kvm_apic_init(CPUState *env);
+
 int apic_accept_pic_intr(CPUState *env);
 void apic_deliver_pic_intr(CPUState *env, int level);
 int apic_get_interrupt(CPUState *env);
kvm.h +5 -0
index 3b358d0..cb896e3 100644
@@ -98,6 +98,11 @@ int kvm_arch_init_vcpu(CPUState *env);
 
 void kvm_arch_reset_vcpu(CPUState *env);
 
+#ifdef TARGET_I386
+int kvm_set_lapic(CPUState *env, struct kvm_lapic_state *s);
+int kvm_get_lapic(CPUState *env, struct kvm_lapic_state *s);
+#endif
+
 int kvm_arch_set_irq(KVMState *s, int irq, int level, int *status);
 
 struct kvm_guest_debug;
target-i386/cpu.h +4 -0
index 1ebf80e..747e4b2 100644
@@ -854,6 +854,10 @@ void cpu_set_apic_tpr(CPUX86State *env, uint8_t val);
 uint8_t cpu_get_apic_tpr(CPUX86State *env);
 #endif
 
+/* hw/apic-kvm.c */
+void kvm_apic_set_base(CPUX86State *env, uint64_t val);
+uint64_t kvm_apic_get_base(CPUX86State *env);
+
 /* hw/pc.c */
 void cpu_smm_update(CPUX86State *env);
 uint64_t cpu_get_tsc(CPUX86State *env);
target-i386/kvm.c +18 -1
index 4eb61f1..a908819 100644
@@ -348,13 +348,16 @@ static void kvm_set_apic_base(CPUState *env, uint64_t val)
 {
     if (!kvm_irqchip_in_kernel())
         cpu_set_apic_base(env, val);
+    else
+        kvm_apic_set_base(env, val);
 }
 
 static uint64_t kvm_get_apic_base(CPUState *env)
 {
     if (!kvm_irqchip_in_kernel())
         return cpu_get_apic_base(env);
-    return 0;
+    else
+        return kvm_apic_get_base(env);
 }
 
 static void kvm_getput_reg(__u64 *kvm_reg, target_ulong *qemu_reg, int set)
@@ -1030,4 +1033,18 @@ int kvm_arch_set_irq(KVMState *s, int irq, int level, int *status)
     return 1;
 }
 
+int kvm_set_lapic(CPUState *env, struct kvm_lapic_state *s)
+{
+    if (!kvm_irqchip_in_kernel())
+        return 0;
 
+    return kvm_vcpu_ioctl(env, KVM_SET_LAPIC, s);
+}
+
+int kvm_get_lapic(CPUState *env, struct kvm_lapic_state *s)
+{
+    if (!kvm_irqchip_in_kernel())
+        return 0;
+
+    return kvm_vcpu_ioctl(env, KVM_GET_LAPIC, s);
+}
Signature · 1 line
1.6.5.2
#8 lore raw

PATCH 8/9 Initialize in-kernel irqchip

Now that we have all devices set up, this patch initializes the irqchip. This is dependant on the io-thread, since we need someone to pull ourselves out of the halted state.

I believe this should be the default when we are running over the io-thread. Later on, I plan to post a patch that makes it optional.

---
 kvm-all.c |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)
kvm-all.c +20 -0
index e46093f..0c6aba7 100644
@@ -416,6 +416,22 @@ int kvm_set_irq(int irq, int level, int *status)
     return kvm_arch_set_irq(kvm_state, irq, level, status);
 }
 
+static int kvm_create_irqchip(KVMState *s)
+{
+    int ret = 0;
+#if defined(CONFIG_IOTHREAD)
+    if (!kvm_check_extension(s, KVM_CAP_IRQCHIP))
+        return -1;
+
+    ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
+    if (ret < 0)
+        return ret;
+
+    s->irqchip_in_kernel = 1;
+#endif
+    return ret;
+}
+
 int kvm_init(int smp_cpus)
 {
     static const char upgrade_note[] =
@@ -502,6 +518,10 @@ int kvm_init(int smp_cpus)
     }
 #endif
 
+    ret = kvm_create_irqchip(s);
+    if (ret < 0)
+        goto err;
+
     ret = kvm_arch_init(s, smp_cpus);
     if (ret < 0)
         goto err;
Signature · 1 line
1.6.5.2
#9 lore raw

PATCH 9/9 Do GSI routing

To follow correctly what our bios ACPI tables say, we have to be able to program our irqchips with GSI routing mappings. This support is already in qemu-kvm

---
 kvm-all.c         |    6 ++-
 kvm.h             |    3 +
 target-i386/kvm.c |  118 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 125 insertions(+), 2 deletions(-)
kvm-all.c +4 -2
index 0c6aba7..a5739c4 100644
@@ -66,6 +66,8 @@ struct KVMState
 #endif
     int irqchip_in_kernel;
     int pit_in_kernel;
+
+    KVMArchState *arch_state;
 };
 
 static KVMState *kvm_state;
@@ -522,12 +524,12 @@ int kvm_init(int smp_cpus)
     if (ret < 0)
         goto err;
 
+    kvm_state = s;
+
     ret = kvm_arch_init(s, smp_cpus);
     if (ret < 0)
         goto err;
 
-    kvm_state = s;
-
     return 0;
 
 err:
kvm.h +3 -0
index cb896e3..15fb34a 100644
@@ -72,7 +72,10 @@ int kvm_set_irq(int irq, int level, int *status);
 /* internal API */
 
 struct KVMState;
+struct KVMArchState;
+
 typedef struct KVMState KVMState;
+typedef struct KVMArchState KVMArchState;
 
 int kvm_ioctl(KVMState *s, int type, ...);
 
target-i386/kvm.c +118 -0

134 lines not on this page · raw message

Signature · 1 line
1.6.5.2
#10 lore raw

[Qemu-devel] Re: [PATCH 5/9] Don't call apic functions directly from kvm code

In reply to Glauber Costa
Quoted 9 lines · Glauber Costa

It is actually not necessary to call a tpr function to save and load cr8, as cr8 is part of the processor state, and thus, it is much easier to just add it to CPUState.

As for apic base, wrap kvm usages, so we can call either the qemu device, or the in kernel version.

target-i386/kvm.c int kvm_arch_post_run(CPUState *env, struct kvm_run *run) · line 808

Quoted 9 lines
target-i386/kvm.c +2 -2
@@ -789,8 +802,8 @@ int kvm_arch_post_run(CPUState *env, struct kvm_run *run)
      else
          env->eflags&= ~IF_MASK;
 
-    cpu_set_apic_tpr(env, run->cr8);
-    cpu_set_apic_base(env, run->apic_base);
+    env->cr8 = run->cr8;
+    kvm_set_apic_base(env, run->apic_base);
 
 

This will break irqchip-in-qemu, since the APIC tpr will be disconnected from the guest's cr8.

Signature · 1 line
error compiling committee.c: too many arguments to function
#11 lore raw
In reply to Glauber Costa
Quoted 29 lines · Glauber Costa

Avi/Marcelo

Please include this in a branch in qemu-kvm, for future inclusion in qemu.git

This is basically the same thing that was sitting in staging for weeks, just with a build bug fix on non-x86 hosts.

Glauber Costa (9):
introduce VMSTATE_U64
Provide ioapic-kvm
provide apic_set_irq_delivered
provide i8259-kvm
Don't call apic functions directly from kvm code export kvm_put_mp_state
provide apic-kvm
Initialize in-kernel irqchip
Do GSI routing

 Makefile.target   |    2 +
 hw/apic-kvm.c     |  157 ++++++++++++++++++++++++++++++++++++++++++++
 hw/apic.c         |    5 ++
 hw/hw.h           |   24 +++++++
 hw/i8259-kvm.c    |  112 +++++++++++++++++++++++++++++++
 hw/ioapic-kvm.c   |   89 +++++++++++++++++++++++++
 hw/pc.c           |   21 +++++-
 hw/pc.h           |    6 ++
 kvm-all.c         |   47 +++++++++++++-
 kvm.h             |   16 +++++
Quoted 9 lines
 savevm.c          |   23 +++++++
 target-i386/cpu.h |    9 +++
 target-i386/kvm.c |  188 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 target-ppc/kvm.c  |    5 ++
 14 files changed, 693 insertions(+), 11 deletions(-)
 create mode 100644 hw/apic-kvm.c
 create mode 100644 hw/i8259-kvm.c
 create mode 100644 hw/ioapic-kvm.c

Does msix work with this patchset when in-kernel irqchip is enabled?

#12 lore raw
Quoted 3 lines

Does msix work with this patchset when in-kernel irqchip is enabled?

Haven't tested. But since I see that msix need some special code in qemu-kvm, it probably won't. But I assume we can just add a patch ontop of this to add that code and make it work, right?

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#13 lore raw
Quoted 7 lines · Glauber Costa
Quoted 3 lines

Does msix work with this patchset when in-kernel irqchip is enabled?

Haven't tested. But since I see that msix need some special code in qemu-kvm, it probably won't. But I assume we can just add a patch ontop of this to add that code and make it work, right?

Sure. However - are you making in-kernel irqchip the default? If so, I think the best way to do it would be:
1. add in-kernel irqchip, off by default
2. fix msix with in-kernel
3. make in-kernel on by default

Otherwise you hurt bisectability.

Quoted 6 lines
Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#14 lore raw
Quoted 10 lines
Quoted 3 lines

Haven't tested. But since I see that msix need some special code in qemu-kvm, it probably won't. But I assume we can just add a patch ontop of this to add that code and make it work, right?

Sure. However - are you making in-kernel irqchip the default? If so, I think the best way to do it would be:
1. add in-kernel irqchip, off by default
2. fix msix with in-kernel
3. make in-kernel on by default

Right now there are no knobs to disable it, since last time I checked, people were inclined to
solve that by adding a machine type that does not do irqchip in kernel, if wanted.

However, since it is probably not going to reach 0.12 anyway, we can come up with a patch
that fixes msix, and bundle in this series before I actually enable the irqchip (which is one
of the last patches)

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#15 lore raw
Quoted 10 lines · Glauber Costa
Quoted 10 lines
Quoted 3 lines

Haven't tested. But since I see that msix need some special code in qemu-kvm, it probably won't. But I assume we can just add a patch ontop of this to add that code and make it work, right?

Sure. However - are you making in-kernel irqchip the default? If so, I think the best way to do it would be:
1. add in-kernel irqchip, off by default
2. fix msix with in-kernel
3. make in-kernel on by default

Quoted 4 lines

Right now there are no knobs to disable it, since last time I checked, people were inclined to
solve that by adding a machine type that does not do irqchip in kernel, if wanted.

Can't everything doable by machine type also doable from command line switch? Disabling irqchip used to be a valuable debugging tool.

Quoted 5 lines

However, since it is probably not going to reach 0.12 anyway, we can come up with a patch
that fixes msix, and bundle in this series before I actually enable the irqchip (which is one
of the last patches)

Fine.

Quoted 6 lines
Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#16 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines

Right now there are no knobs to disable it, since last time I checked, people were inclined to
solve that by adding a machine type that does not do irqchip in kernel, if wanted.

Can't everything doable by machine type also doable from command line switch? Disabling irqchip used to be a valuable debugging tool.

Even worse, if we throw everything into the the machine type, we end up with 2^n machine types in order to control everything.

Signature · 1 line
error compiling committee.c: too many arguments to function
#17 lore raw
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines

Right now there are no knobs to disable it, since last time I checked, people were inclined to
solve that by adding a machine type that does not do irqchip in kernel, if wanted.

Quoted 8 lines
Quoted 5 lines

Can't everything doable by machine type also doable from command line switch? Disabling irqchip used to be a valuable debugging tool.

Even worse, if we throw everything into the the machine type, we end up with 2^n machine types in order to control everything.

I am totally fine re-including the cmdline patch in the series.

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."